@@ -45,23 +45,15 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
45
45
46
46
def buildClass (className : ClassName , kind : ClassKind , jsClassCaptures : Option [List [ParamDef ]],
47
47
hasClassInitializer : Boolean ,
48
- superClass : Option [ClassIdent ], jsSuperClass : Option [Tree ], useESClass : Boolean , ctor : js.Tree ,
48
+ superClass : Option [ClassIdent ], jsSuperClass : Option [Tree ], useESClass : Boolean , ctorDefs : List [ js.Tree ] ,
49
49
memberDefs : List [js.MethodDef ], exportedDefs : List [js.Tree ])(
50
50
implicit moduleContext : ModuleContext ,
51
- globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [js.Tree ] = {
51
+ globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [List [ js.Tree ] ] = {
52
52
53
- def allES6Defs = {
54
- js.Block (ctor +: (memberDefs ++ exportedDefs)) match {
55
- case js.Block (allDefs) => allDefs
56
- case js.Skip () => Nil
57
- case oneDef => List (oneDef)
58
- }
59
- }
53
+ def allES6Defs = ctorDefs ::: memberDefs ::: exportedDefs
60
54
61
- def allES5Defs (classVar : js.Tree ) = {
62
- WithGlobals (js.Block (
63
- ctor, assignES5ClassMembers(classVar, memberDefs), js.Block (exportedDefs : _* )))
64
- }
55
+ def allES5Defs (classVar : js.Tree ) =
56
+ WithGlobals (ctorDefs ::: assignES5ClassMembers(classVar, memberDefs) ::: exportedDefs)
65
57
66
58
if (! kind.isJSClass) {
67
59
assert(jsSuperClass.isEmpty, className)
@@ -95,7 +87,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
95
87
96
88
val entireClassDefWithGlobals = if (useESClass) {
97
89
genJSSuperCtor(superClass, jsSuperClass).map { jsSuperClass =>
98
- classValueVar := js.ClassDef (Some (classValueIdent), Some (jsSuperClass), allES6Defs)
90
+ List ( classValueVar := js.ClassDef (Some (classValueIdent), Some (jsSuperClass), allES6Defs) )
99
91
}
100
92
} else {
101
93
allES5Defs(classValueVar)
@@ -106,7 +98,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
106
98
entireClassDef <- entireClassDefWithGlobals
107
99
createStaticFields <- genCreateStaticFieldsOfJSClass(className)
108
100
} yield {
109
- optStoreJSSuperClass.toList ::: entireClassDef :: createStaticFields
101
+ optStoreJSSuperClass.toList ::: entireClassDef ::: createStaticFields
110
102
}
111
103
112
104
jsClassCaptures.fold {
@@ -125,7 +117,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
125
117
)
126
118
createAccessor <- globalFunctionDef(" a" , className, Nil , None , body)
127
119
} yield {
128
- js. Block ( createClassValueVar, createAccessor)
120
+ createClassValueVar :: createAccessor
129
121
}
130
122
} { jsClassCaptures =>
131
123
val captureParamDefs = for (param <- jsClassCaptures) yield {
@@ -173,7 +165,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
173
165
def genScalaClassConstructor (className : ClassName , superClass : Option [ClassIdent ],
174
166
useESClass : Boolean , initToInline : Option [MethodDef ])(
175
167
implicit moduleContext : ModuleContext ,
176
- globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [js.Tree ] = {
168
+ globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [List [ js.Tree ] ] = {
177
169
178
170
assert(superClass.isDefined || className == ObjectClass ,
179
171
s " Class $className is missing a parent class " )
@@ -192,9 +184,9 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
192
184
}
193
185
194
186
if (args.isEmpty && isTrivialCtorBody)
195
- js. Skip ()
187
+ Nil
196
188
else
197
- js.MethodDef (static = false , js.Ident (" constructor" ), args, restParam, body)
189
+ js.MethodDef (static = false , js.Ident (" constructor" ), args, restParam, body) :: Nil
198
190
}
199
191
} else {
200
192
import TreeDSL ._
@@ -203,13 +195,13 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
203
195
204
196
val chainProtoWithGlobals = superClass match {
205
197
case None =>
206
- WithGlobals (js. Skip ())
198
+ WithGlobals .nil
207
199
208
200
case Some (_) if shouldExtendJSError(className, superClass) =>
209
201
untrackedGlobalRef(" Error" ).map(chainPrototypeWithLocalCtor(className, ctorVar, _))
210
202
211
203
case Some (parentIdent) =>
212
- WithGlobals (ctorVar.prototype := js.New (globalVar(" h" , parentIdent.name), Nil ))
204
+ WithGlobals (List ( ctorVar.prototype := js.New (globalVar(" h" , parentIdent.name), Nil ) ))
213
205
}
214
206
215
207
for {
@@ -220,17 +212,17 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
220
212
globalFunctionDef(" h" , className, Nil , None , js.Skip ())
221
213
chainProto <- chainProtoWithGlobals
222
214
} yield {
223
- js. Block (
224
- // Real constructor
225
- js.DocComment (" @constructor" ),
226
- realCtorDef,
227
- chainProto,
228
- genIdentBracketSelect(ctorVar.prototype, " constructor" ) := ctorVar,
229
-
230
- // Inheritable constructor
231
- js.DocComment (" @constructor" ),
232
- inheritableCtorDef,
233
- globalVar(" h" , className).prototype := ctorVar.prototype
215
+ (
216
+ // Real constructor
217
+ js.DocComment (" @constructor" ) ::
218
+ realCtorDef :::
219
+ chainProto :::
220
+ ( genIdentBracketSelect(ctorVar.prototype, " constructor" ) := ctorVar) ::
221
+
222
+ // Inheritable constructor
223
+ js.DocComment (" @constructor" ) ::
224
+ inheritableCtorDef :::
225
+ ( globalVar(" h" , className).prototype := ctorVar.prototype) :: Nil
234
226
)
235
227
}
236
228
}
@@ -240,15 +232,15 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
240
232
def genJSConstructor (className : ClassName , superClass : Option [ClassIdent ],
241
233
jsSuperClass : Option [Tree ], useESClass : Boolean , jsConstructorDef : JSConstructorDef )(
242
234
implicit moduleContext : ModuleContext ,
243
- globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [js.Tree ] = {
235
+ globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [List [ js.Tree ] ] = {
244
236
245
237
val JSConstructorDef (_, params, restParam, body) = jsConstructorDef
246
238
val ctorFunWithGlobals = desugarToFunction(className, params, restParam, body)
247
239
248
240
if (useESClass) {
249
241
for (fun <- ctorFunWithGlobals) yield {
250
242
js.MethodDef (static = false , js.Ident (" constructor" ),
251
- fun.args, fun.restParam, fun.body)
243
+ fun.args, fun.restParam, fun.body) :: Nil
252
244
}
253
245
} else {
254
246
for {
@@ -259,12 +251,10 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
259
251
260
252
val ctorVar = fileLevelVar(" b" , genName(className))
261
253
262
- js.Block (
263
- js.DocComment (" @constructor" ),
264
- ctorVar := ctorFun,
265
- chainPrototypeWithLocalCtor(className, ctorVar, superCtor),
266
- genIdentBracketSelect(ctorVar.prototype, " constructor" ) := ctorVar
267
- )
254
+ js.DocComment (" @constructor" ) ::
255
+ (ctorVar := ctorFun) ::
256
+ chainPrototypeWithLocalCtor(className, ctorVar, superCtor) :::
257
+ (genIdentBracketSelect(ctorVar.prototype, " constructor" ) := ctorVar) :: Nil
268
258
}
269
259
}
270
260
}
@@ -348,12 +338,12 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
348
338
}
349
339
350
340
private def chainPrototypeWithLocalCtor (className : ClassName , ctorVar : js.Tree ,
351
- superCtor : js.Tree )(implicit pos : Position ): js.Tree = {
341
+ superCtor : js.Tree )(implicit pos : Position ): List [ js.Tree ] = {
352
342
import TreeDSL ._
353
343
354
344
val dummyCtor = fileLevelVar(" hh" , genName(className))
355
345
356
- js. Block (
346
+ List (
357
347
js.DocComment (" @constructor" ),
358
348
genConst(dummyCtor.ident, js.Function (false , Nil , None , js.Skip ())),
359
349
dummyCtor.prototype := superCtor.prototype,
@@ -396,7 +386,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
396
386
globalVarDef(" t" , varScope, value, origName.orElse(name))
397
387
}
398
388
399
- WithGlobals .list (defs)
389
+ WithGlobals .flatten (defs)
400
390
}
401
391
402
392
/** Generates the creation of the private JS field defs for a JavaScript
@@ -424,7 +414,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
424
414
}
425
415
}
426
416
427
- WithGlobals .list (defs)
417
+ WithGlobals .flatten (defs)
428
418
}
429
419
430
420
/** Generates the creation of the static fields for a JavaScript class. */
@@ -497,7 +487,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
497
487
498
488
def genStaticLikeMethod (className : ClassName , method : MethodDef )(
499
489
implicit moduleContext : ModuleContext ,
500
- globalKnowledge : GlobalKnowledge ): WithGlobals [js.Tree ] = {
490
+ globalKnowledge : GlobalKnowledge ): WithGlobals [List [ js.Tree ] ] = {
501
491
val methodBody = method.body.getOrElse(
502
492
throw new AssertionError (" Cannot generate an abstract method" ))
503
493
@@ -570,11 +560,11 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
570
560
private def genJSProperty (className : ClassName , kind : ClassKind , useESClass : Boolean ,
571
561
property : JSPropertyDef )(
572
562
implicit moduleContext : ModuleContext ,
573
- globalKnowledge : GlobalKnowledge ): WithGlobals [js.Tree ] = {
563
+ globalKnowledge : GlobalKnowledge ): WithGlobals [List [ js.Tree ] ] = {
574
564
if (useESClass)
575
565
genJSPropertyES6(className, property)
576
566
else
577
- genJSPropertyES5(className, kind, property)
567
+ genJSPropertyES5(className, kind, property).map(_ :: Nil )
578
568
}
579
569
580
570
private def genJSPropertyES5 (className : ClassName , kind : ClassKind , property : JSPropertyDef )(
@@ -612,31 +602,27 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
612
602
613
603
private def genJSPropertyES6 (className : ClassName , property : JSPropertyDef )(
614
604
implicit moduleContext : ModuleContext ,
615
- globalKnowledge : GlobalKnowledge ): WithGlobals [js.Tree ] = {
605
+ globalKnowledge : GlobalKnowledge ): WithGlobals [List [ js.Tree ] ] = {
616
606
implicit val pos = property.pos
617
607
618
608
val static = property.flags.namespace.isStatic
619
609
620
610
genMemberNameTree(property.name).flatMap { propName =>
621
- val getterWithGlobals = property.getterBody.fold {
622
- WithGlobals [js.Tree ](js.Skip ())
623
- } { body =>
611
+ val getterWithGlobals = property.getterBody.map { body =>
624
612
for (fun <- desugarToFunction(className, Nil , body, resultType = AnyType ))
625
613
yield js.GetterDef (static, propName, fun.body)
626
614
}
627
615
628
- val setterWithGlobals = property.setterArgAndBody.fold {
629
- WithGlobals [js.Tree ](js.Skip ())
630
- } { case (arg, body) =>
616
+ val setterWithGlobals = property.setterArgAndBody.map { case (arg, body) =>
631
617
for (fun <- desugarToFunction(className, arg :: Nil , body, resultType = NoType ))
632
618
yield js.SetterDef (static, propName, fun.args.head, fun.body)
633
619
}
634
620
635
621
for {
636
- getter <- getterWithGlobals
637
- setter <- setterWithGlobals
622
+ getter <- WithGlobals .option( getterWithGlobals)
623
+ setter <- WithGlobals .option( setterWithGlobals)
638
624
} yield {
639
- js. Block ( getter, setter)
625
+ getter.toList ::: setter.toList
640
626
}
641
627
}
642
628
}
@@ -756,11 +742,11 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
756
742
val createIsStatWithGlobals = if (needIsFunction) {
757
743
globalFunctionDef(" is" , className, List (objParam), None , js.Return (isExpression))
758
744
} else {
759
- WithGlobals (js. Skip ())
745
+ WithGlobals .nil
760
746
}
761
747
762
748
val createAsStatWithGlobals = if (semantics.asInstanceOfs == Unchecked ) {
763
- WithGlobals (js. Skip ())
749
+ WithGlobals .nil
764
750
} else {
765
751
globalFunctionDef(" as" , className, List (objParam), None , js.Return {
766
752
val isCond =
@@ -780,10 +766,10 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
780
766
createIsStat <- createIsStatWithGlobals
781
767
createAsStat <- createAsStatWithGlobals
782
768
} yield {
783
- List ( createIsStat, createAsStat)
769
+ createIsStat ::: createAsStat
784
770
}
785
771
} else {
786
- WithGlobals ( Nil )
772
+ WithGlobals .nil
787
773
}
788
774
}
789
775
@@ -816,7 +802,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
816
802
}
817
803
818
804
val createAsArrayOfStatWithGlobals = if (semantics.asInstanceOfs == Unchecked ) {
819
- WithGlobals (js. Skip ())
805
+ WithGlobals .nil
820
806
} else {
821
807
globalFunctionDef(" asArrayOf" , className, List (objParam, depthParam), None , {
822
808
js.Return {
@@ -835,7 +821,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
835
821
createIsArrayOfStat <- createIsArrayOfStatWithGlobals
836
822
createAsArrayOfStat <- createAsArrayOfStatWithGlobals
837
823
} yield {
838
- List ( createIsArrayOfStat, createAsArrayOfStat)
824
+ createIsArrayOfStat ::: createAsArrayOfStat
839
825
}
840
826
}
841
827
@@ -855,7 +841,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
855
841
superClass : Option [ClassIdent ], ancestors : List [ClassName ],
856
842
jsNativeLoadSpec : Option [JSNativeLoadSpec ])(
857
843
implicit moduleContext : ModuleContext ,
858
- globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [js.Tree ] = {
844
+ globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [List [ js.Tree ] ] = {
859
845
import TreeDSL ._
860
846
861
847
val isObjectClass =
@@ -959,7 +945,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
959
945
960
946
def genModuleAccessor (className : ClassName , kind : ClassKind )(
961
947
implicit moduleContext : ModuleContext ,
962
- globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [js.Tree ] = {
948
+ globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [List [ js.Tree ] ] = {
963
949
import TreeDSL ._
964
950
965
951
val tpe = ClassType (className)
@@ -1013,14 +999,14 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
1013
999
globalFunctionDef(" m" , className, Nil , None , body)
1014
1000
}
1015
1001
1016
- createAccessor.map(js. Block ( createModuleInstanceField, _) )
1002
+ createAccessor.map(createModuleInstanceField :: _ )
1017
1003
}
1018
1004
1019
1005
def genExportedMember (className : ClassName , kind : ClassKind , useESClass : Boolean , member : JSMethodPropDef )(
1020
1006
implicit moduleContext : ModuleContext ,
1021
- globalKnowledge : GlobalKnowledge ): WithGlobals [js.Tree ] = {
1007
+ globalKnowledge : GlobalKnowledge ): WithGlobals [List [ js.Tree ] ] = {
1022
1008
member match {
1023
- case m : JSMethodDef => genJSMethod(className, kind, useESClass, m)
1009
+ case m : JSMethodDef => genJSMethod(className, kind, useESClass, m).map(_ :: Nil )
1024
1010
case p : JSPropertyDef => genJSProperty(className, kind, useESClass, p)
1025
1011
}
1026
1012
}
0 commit comments