8000 Printer improvements to fuse JS emitting and printing by gzm0 · Pull Request #4920 · scala-js/scala-js · GitHub
[go: up one dir, main page]

Skip to content

Printer improvements to fuse JS emitting and printing #4920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 28, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private object BasicLinkerBackend {
val jsCodeWriter = new ByteArrayWriter()
val printer = new Printers.JSTreePrinter(jsCodeWriter)

printer.printTopLevelTree(tree)
printer.printStat(tree)

new PrintedTree(jsCodeWriter.toByteArray(), SourceMapWriter.Fragment.Empty)
}
Expand Down Expand Up @@ -321,7 +321,7 @@ private object BasicLinkerBackend {
val smFragmentBuilder = new SourceMapWriter.FragmentBuilder()
val printer = new Printers.JSTreePrinterWithSourceMap(jsCodeWriter, smFragmentBuilder)

printer.printTopLevelTree(tree)
printer.printStat(tree)
smFragmentBuilder.complete()

new PrintedTree(jsCodeWriter.toByteArray(), smFragmentBuilder.result())
Expand Down
8000
Original file line number Diff line number Diff line change
Expand Up @@ -683,12 +683,12 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {

def genInstanceTests(className: ClassName, kind: ClassKind)(
implicit moduleContext: ModuleContext,
globalKnowledge: GlobalKnowledge, pos: Position): WithGlobals[js.Tree] = {
globalKnowledge: GlobalKnowledge, pos: Position): WithGlobals[List[js.Tree]] = {
for {
single <- genSingleInstanceTests(className, kind)
array <- genArrayInstanceTests(className)
} yield {
js.Block(single ::: array)
single ::: array
}
}

Expand Down Expand Up @@ -1028,16 +1028,16 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
case e: TopLevelMethodExportDef =>
genTopLevelMethodExportDef(e)
case e: TopLevelFieldExportDef =>
genTopLevelFieldExportDef(topLevelExport.owningClass, e)
genTopLevelFieldExportDef(topLevelExport.owningClass, e).map(_ :: Nil)
}
}

WithGlobals.list(exportsWithGlobals)
WithGlobals.flatten(exportsWithGlobals)
}

private def genTopLevelMethodExportDef(tree: TopLevelMethodExportDef)(
implicit moduleContext: ModuleContext,
globalKnowledge: GlobalKnowledge): WithGlobals[js.Tree] = {
globalKnowledge: GlobalKnowledge): WithGlobals[List[js.Tree]] = {
import TreeDSL._

val JSMethodDef(flags, StringLiteral(exportName), args, restParam, body) =
Expand All @@ -1056,22 +1056,22 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {

private def genConstValueExportDef(exportName: String,
exportedValue: js.Tree)(
implicit pos: Position): WithGlobals[js.Tree] = {
implicit pos: Position): WithGlobals[List[js.Tree]] = {
moduleKind match {
case ModuleKind.NoModule =>
genAssignToNoModuleExportVar(exportName, exportedValue)
genAssignToNoModuleExportVar(exportName, exportedValue).map(_ :: Nil)

case ModuleKind.ESModule =>
val field = fileLevelVar(VarField.e, exportName)
val let = js.Let(field.ident, mutable = true, Some(exportedValue))
val exportStat = js.Export((field.ident -> js.ExportName(exportName)) :: Nil)
WithGlobals(js.Block(let, exportStat))
WithGlobals(List(let, exportStat))

case ModuleKind.CommonJSModule =>
globalRef("exports").map { exportsVarRef =>
js.Assign(
genBracketSelect(exportsVarRef, js.StringLiteral(exportName)),
exportedValue)
exportedValue) :: Nil
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ final class Emitter(config: Emitter.Config) {
*/

if (classEmitter.needInstanceTests(linkedClass)(classCache)) {
main += extractWithGlobals(classTreeCache.instanceTests.getOrElseUpdate(
main ++= extractWithGlobals(classTreeCache.instanceTests.getOrElseUpdate(
classEmitter.genInstanceTests(className, kind)(moduleContext, classCache, linkedClass.pos)))
}

Expand Down Expand Up @@ -1035,7 +1035,7 @@ object Emitter {

private final class DesugaredClassCache {
val privateJSFields = new OneTimeCache[WithGlobals[List[js.Tree]]]
val instanceTests = new OneTimeCache[WithGlobals[js.Tree]]
val instanceTests = new OneTimeCache[WithGlobals[List[js.Tree]]]
val typeData = new OneTimeCache[WithGlobals[List[js.Tree]]]
val setTypeData = new OneTimeCache[js.Tree]
val moduleAccessor = new OneTimeCache[WithGlobals[List[js.Tree]]]
Expand Down
Loading
0