@@ -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 {
@@ -219,36 +211,34 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
219
211
inheritableCtorDef <-
220
212
globalFunctionDef(" h" , className, Nil , None , js.Skip ())
221
213
chainProto <- chainProtoWithGlobals
222
- } 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
234
- )
235
- }
214
+ } yield (
215
+ // Real constructor
216
+ js.DocComment (" @constructor" ) ::
217
+ realCtorDef :::
218
+ chainProto :::
219
+ (genIdentBracketSelect(ctorVar.prototype, " constructor" ) := ctorVar) ::
220
+
221
+ // Inheritable constructor
222
+ js.DocComment (" @constructor" ) ::
223
+ inheritableCtorDef :::
224
+ (globalVar(" h" , className).prototype := ctorVar.prototype) :: Nil
225
+ )
236
226
}
237
227
}
238
228
239
229
/** Generates the JS constructor for a JS class. */
240
230
def genJSConstructor (className : ClassName , superClass : Option [ClassIdent ],
241
231
jsSuperClass : Option [Tree ], useESClass : Boolean , jsConstructorDef : JSConstructorDef )(
242
232
implicit moduleContext : ModuleContext ,
243
- globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [js.Tree ] = {
233
+ globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [List [ js.Tree ] ] = {
244
234
245
235
val JSConstructorDef (_, params, restParam, body) = jsConstructorDef
246
236
val ctorFunWithGlobals = desugarToFunction(className, params, restParam, body)
247
237
248
238
if (useESClass) {
249
239
for (fun <- ctorFunWithGlobals) yield {
250
240
js.MethodDef (static = false , js.Ident (" constructor" ),
251
- fun.args, fun.restParam, fun.body)
241
+ fun.args, fun.restParam, fun.body) :: Nil
252
242
}
253
243
} else {
254
244
for {
@@ -259,12 +249,10 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
259
249
260
250
val ctorVar = fileLevelVar(" b" , genName(className))
261
251
262
- js.Block (
263
- js.DocComment (" @constructor" ),
264
- ctorVar := ctorFun,
265
- chainPrototypeWithLocalCtor(className, ctorVar, superCtor),
266
- genIdentBracketSelect(ctorVar.prototype, " constructor" ) := ctorVar
267
- )
252
+ js.DocComment (" @constructor" ) ::
253
+ (ctorVar := ctorFun) ::
254
+ chainPrototypeWithLocalCtor(className, ctorVar, superCtor) :::
255
+ (genIdentBracketSelect(ctorVar.prototype, " constructor" ) := ctorVar) :: Nil
268
256
}
269
257
}
270
258
}
@@ -348,12 +336,12 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
348
336
}
349
337
350
338
private def chainPrototypeWithLocalCtor (className : ClassName , ctorVar : js.Tree ,
351
- superCtor : js.Tree )(implicit pos : Position ): js.Tree = {
339
+ superCtor : js.Tree )(implicit pos : Position ): List [ js.Tree ] = {
352
340
import TreeDSL ._
353
341
354
342
val dummyCtor = fileLevelVar(" hh" , genName(className))
355
343
356
- js. Block (
344
+ List (
357
345
js.DocComment (" @constructor" ),
358
346
genConst(dummyCtor.ident, js.Function (false , Nil , None , js.Skip ())),
359
347
dummyCtor.prototype := superCtor.prototype,
@@ -396,7 +384,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
396
384
globalVarDef(" t" , varScope, value, origName.orElse(name))
397
385
}
398
386
399
- WithGlobals .list (defs)
387
+ WithGlobals .flatten (defs)
400
388
}
401
389
402
390
/** Generates the creation of the private JS field defs for a JavaScript
@@ -424,7 +412,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
424
412
}
425
413
}
426
414
427
- WithGlobals .list (defs)
415
+ WithGlobals .flatten (defs)
428
416
}
429
417
430
418
/** Generates the creation of the static fields for a JavaScript class. */
@@ -497,7 +485,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
497
485
498
486
def genStaticLikeMethod (className : ClassName , method : MethodDef )(
499
487
implicit moduleContext : ModuleContext ,
500
- globalKnowledge : GlobalKnowledge ): WithGlobals [js.Tree ] = {
488
+ globalKnowledge : GlobalKnowledge ): WithGlobals [List [ js.Tree ] ] = {
501
489
val methodBody = method.body.getOrElse(
502
490
throw new AssertionError (" Cannot generate an abstract method" ))
503
491
@@ -570,11 +558,11 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
570
558
private def genJSProperty (className : ClassName , kind : ClassKind , useESClass : Boolean ,
571
559
property : JSPropertyDef )(
572
560
implicit moduleContext : ModuleContext ,
573
- globalKnowledge : GlobalKnowledge ): WithGlobals [js.Tree ] = {
561
+ globalKnowledge : GlobalKnowledge ): WithGlobals [List [ js.Tree ] ] = {
574
562
if (useESClass)
575
563
genJSPropertyES6(className, property)
576
564
else
577
- genJSPropertyES5(className, kind, property)
565
+ genJSPropertyES5(className, kind, property).map(_ :: Nil )
578
566
}
579
567
580
568
private def genJSPropertyES5 (className : ClassName , kind : ClassKind , property : JSPropertyDef )(
@@ -612,31 +600,27 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
612
600
613
601
private def genJSPropertyES6 (className : ClassName , property : JSPropertyDef )(
614
602
implicit moduleContext : ModuleContext ,
615
- globalKnowledge : GlobalKnowledge ): WithGlobals [js.Tree ] = {
603
+ globalKnowledge : GlobalKnowledge ): WithGlobals [List [ js.Tree ] ] = {
616
604
implicit val pos = property.pos
617
605
618
606
val static = property.flags.namespace.isStatic
619
607
620
608
genMemberNameTree(property.name).flatMap { propName =>
621
- val getterWithGlobals = property.getterBody.fold {
622
- WithGlobals [js.Tree ](js.Skip ())
623
- } { body =>
609
+ val getterWithGlobals = property.getterBody.map { body =>
624
610
for (fun <- desugarToFunction(className, Nil , body, resultType = AnyType ))
625
611
yield js.GetterDef (static, propName, fun.body)
626
612
}
627
613
628
- val setterWithGlobals = property.setterArgAndBody.fold {
629
- WithGlobals [js.Tree ](js.Skip ())
630
- } { case (arg, body) =>
614
+ val setterWithGlobals = property.setterArgAndBody.map { case (arg, body) =>
631
615
for (fun <- desugarToFunction(className, arg :: Nil , body, resultType = NoType ))
632
616
yield js.SetterDef (static, propName, fun.args.head, fun.body)
633
617
}
634
618
635
619
for {
636
- getter <- getterWithGlobals
637
- setter <- setterWithGlobals
620
+ getter <- WithGlobals .option( getterWithGlobals)
621
+ setter <- WithGlobals .option( setterWithGlobals)
638
622
} yield {
639
- js. Block ( getter, setter)
623
+ getter.toList ::: setter.toList
640
624
}
641
625
}
642
626
}
@@ -756,11 +740,11 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
756
740
val createIsStatWithGlobals = if (needIsFunction) {
757
741
globalFunctionDef(" is" , className, List (objParam), None , js.Return (isExpression))
758
742
} else {
759
- WithGlobals (js. Skip ())
743
+ WithGlobals .nil
760
744
}
761
745
762
746
val createAsStatWithGlobals = if (semantics.asInstanceOfs == Unchecked ) {
763
- WithGlobals (js. Skip ())
747
+ WithGlobals .nil
764
748
} else {
765
749
globalFunctionDef(" as" , className, List (objParam), None , js.Return {
766
750
val isCond =
@@ -780,10 +764,10 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
780
764
createIsStat <- createIsStatWithGlobals
781
765
createAsStat <- createAsStatWithGlobals
782
766
} yield {
783
- List ( createIsStat, createAsStat)
767
+ createIsStat ::: createAsStat
784
768
}
785
769
} else {
786
- WithGlobals ( Nil )
770
+ WithGlobals .nil
787
771
}
788
772
}
789
773
@@ -816,7 +800,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
816
800
}
817
801
818
802
val createAsArrayOfStatWithGlobals = if (semantics.asInstanceOfs == Unchecked ) {
819
- WithGlobals (js. Skip ())
803
+ WithGlobals .nil
820
804
} else {
821
805
globalFunctionDef(" asArrayOf" , className, List (objParam, depthParam), None , {
822
806
js.Return {
@@ -835,7 +819,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
835
819
createIsArrayOfStat <- createIsArrayOfStatWithGlobals
836
820
createAsArrayOfStat <- createAsArrayOfStatWithGlobals
837
821
} yield {
838
- List ( createIsArrayOfStat, createAsArrayOfStat)
822
+ createIsArrayOfStat ::: createAsArrayOfStat
839
823
}
840
824
}
841
825
@@ -855,7 +839,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
855
839
superClass : Option [ClassIdent ], ancestors : List [ClassName ],
856
840
jsNativeLoadSpec : Option [JSNativeLoadSpec ])(
857
841
implicit moduleContext : ModuleContext ,
858
- globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [js.Tree ] = {
842
+ globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [List [ js.Tree ] ] = {
859
843
import TreeDSL ._
860
844
861
845
val isObjectClass =
@@ -959,7 +943,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
959
943
960
944
def genModuleAccessor (className : ClassName , kind : ClassKind )(
961
945
implicit moduleContext : ModuleContext ,
962
- globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [js.Tree ] = {
946
+ globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [List [ js.Tree ] ] = {
963
947
import TreeDSL ._
964
948
965
949
val tpe = ClassType (className)
@@ -1013,14 +997,14 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
1013
997
globalFunctionDef(" m" , className, Nil , None , body)
1014
998
}
1015
999
1016
- createAccessor.map(js. Block ( createModuleInstanceField, _) )
1000
+ createAccessor.map(createModuleInstanceField :: _ )
1017
1001
}
1018
1002
1019
1003
def genExportedMember (className : ClassName , kind : ClassKind , useESClass : Boolean , member : JSMethodPropDef )(
1020
1004
implicit moduleContext : ModuleContext ,
1021
- globalKnowledge : GlobalKnowledge ): WithGlobals [js.Tree ] = {
1005
+ globalKnowledge : GlobalKnowledge ): WithGlobals [List [ js.Tree ] ] = {
1022
1006
member match {
1023
- case m : JSMethodDef => genJSMethod(className, kind, useESClass, m)
1007
+ case m : JSMethodDef => genJSMethod(className, kind, useESClass, m).map(_ :: Nil )
1024
1008
case p : JSPropertyDef => genJSProperty(className, kind, useESClass, p)
1025
1009
}
1026
1010
}
0 commit comments