@@ -169,57 +169,51 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
169
169
}
170
170
}
171
171
172
- /** Generates the JS constructor for a class. */
173
- def genConstructor (className : ClassName , kind : ClassKind , superClass : Option [ClassIdent ],
174
- jsSuperClass : Option [Tree ], useESClass : Boolean ,
175
- initToInline : Option [MethodDef ], jsConstructorDef : Option [JSConstructorDef ])(
172
+ /** Generates the JS constructor for a Scala class. */
173
+ def genScalaClassConstructor (className : ClassName , superClass : Option [ClassIdent ],
174
+ useESClass : Boolean , initToInline : Option [MethodDef ])(
176
175
implicit moduleContext : ModuleContext ,
177
176
globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [js.Tree ] = {
178
177
179
- assert(kind.isAnyNonNativeClass)
180
178
assert(superClass.isDefined || className == ObjectClass ,
181
179
s " Class $className is missing a parent class " )
182
180
183
- if (useESClass)
184
- genES6Constructor(className, kind, superClass, initToInline, jsConstructorDef)
185
- else
186
- genES5Constructor(className, kind, superClass, jsSuperClass, initToInline, jsConstructorDef)
187
- }
181
+ val jsConstructorFunWithGlobals =
182
+ genJSConstructorFun(className, superClass, initToInline, useESClass)
188
183
189
- /** Generates the JS constructor for a class, ES5 style. */
190
- private def genES5Constructor (className : ClassName , kind : ClassKind , superClass : Option [ClassIdent ],
191
- jsSuperClass : Option [Tree ], initToInline : Option [MethodDef ], jsConstructorDef : Option [JSConstructorDef ])(
192
- implicit moduleContext : ModuleContext ,
193
- globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [js.Tree ] = {
194
- import TreeDSL ._
184
+ if (useESClass) {
185
+ for (jsConstructorFun <- jsConstructorFunWithGlobals) yield {
186
+ val js .Function (_, args, restParam, body) = jsConstructorFun
195
187
196
- def chainPrototypeWithLocalCtor (ctorVar : js.Tree , superCtor : js.Tree ): js.Tree = {
197
- val dummyCtor = fileLevelVar(" hh" , genName(className))
188
+ def isTrivialCtorBody : Boolean = body match {
189
+ case js.Skip () => true
190
+ case js.Apply (js.Super (), Nil ) => true
191
+ case _ => false
192
+ }
198
193
199
- js. Block (
200
- js.DocComment ( " @constructor " ),
201
- genConst(dummyCtor.ident, js. Function ( false , Nil , None , js. Skip ())),
202
- dummyCtor.prototype := superCtor.prototype,
203
- ctorVar.prototype := js. New (dummyCtor, Nil )
204
- )
205
- }
194
+ if (args.isEmpty && isTrivialCtorBody)
195
+ js.Skip ()
196
+ else
197
+ js. MethodDef (static = false , js. Ident ( " constructor " ), args, restParam, body)
198
+ }
199
+ } else {
200
+ import TreeDSL . _
206
201
207
- if (! kind.isJSClass) {
208
202
val ctorVar = globalVar(" c" , className)
209
203
210
204
val chainProtoWithGlobals = superClass match {
211
205
case None =>
212
206
WithGlobals (js.Skip ())
213
207
214
208
case Some (_) if shouldExtendJSError(className, superClass) =>
215
- untrackedGlobalRef(" Error" ).map(chainPrototypeWithLocalCtor(ctorVar, _))
209
+ untrackedGlobalRef(" Error" ).map(chainPrototypeWithLocalCtor(className, ctorVar, _))
216
210
217
211
case Some (parentIdent) =>
218
212
WithGlobals (ctorVar.prototype := js.New (globalVar(" h" , parentIdent.name), Nil ))
219
213
}
220
214
221
215
for {
222
- ctorFun <- genJSConstructorFun(className, superClass, initToInline, forESClass = false )
216
+ ctorFun <- jsConstructorFunWithGlobals
223
217
realCtorDef <-
224
218
globalFunctionDef(" c" , className, ctorFun.args, ctorFun.restParam, ctorFun.body)
225
219
inheritableCtorDef <-
@@ -239,50 +233,38 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
239
233
globalVar(" h" , className).prototype := ctorVar.prototype
240
234
)
241
235
}
242
- } else {
243
- for {
244
- ctorFun <- genConstructorFunForJSClass(className, jsConstructorDef)
245
- superCtor <- genJSSuperCtor(superClass, jsSuperClass)
246
- } yield {
247
- val ctorVar = fileLevelVar(" b" , genName(className))
248
-
249
- js.Block (
250
- js.DocComment (" @constructor" ),
251
- ctorVar := ctorFun,
252
- chainPrototypeWithLocalCtor(ctorVar, superCtor),
253
- genIdentBracketSelect(ctorVar.prototype, " constructor" ) := ctorVar
254
- )
255
- }
256
236
}
257
237
}
258
238
259
- /** Generates the JS constructor for a class, ES6 style . */
260
- private def genES6Constructor (className : ClassName , kind : ClassKind , superClass : Option [ClassIdent ],
261
- initToInline : Option [MethodDef ], jsConstructorDef : Option [ JSConstructorDef ] )(
239
+ /** Generates the JS constructor for a JS class . */
240
+ def genJSConstructor (className : ClassName , superClass : Option [ClassIdent ],
241
+ jsSuperClass : Option [Tree ], useESClass : Boolean , jsConstructorDef : JSConstructorDef )(
262
242
implicit moduleContext : ModuleContext ,
263
243
globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [js.Tree ] = {
264
- if (kind.isJSClass) {
265
- for (fun <- genConstructorFunForJSClass(className, jsConstructorDef)) yield {
244
+
245
+ val JSConstructorDef (_, params, restParam, body) = jsConstructorDef
246
+ val ctorFunWithGlobals = desugarToFunction(className, params, restParam, body)
247
+
248
+ if (useESClass) {
249
+ for (fun <- ctorFunWithGlobals) yield {
266
250
js.MethodDef (static = false , js.Ident (" constructor" ),
267
251
fun.args, fun.restParam, fun.body)
268
252
}
269
253
} else {
270
- val jsConstructorFunWithGlobals =
271
- genJSConstructorFun(className, superClass, initToInline, forESClass = true )
272
-
273
- for (jsConstructorFun <- jsConstructorFunWithGlobals) yield {
274
- val js . Function (_, args, restParam, body) = jsConstructorFun
254
+ for {
255
+ ctorFun <- ctorFunWithGlobals
256
+ superCtor <- genJSSuperCtor(superClass, jsSuperClass)
257
+ } yield {
258
+ import TreeDSL . _
275
259
276
- def isTrivialCtorBody : Boolean = body match {
277
- case js.Skip () => true
278
- case js.Apply (js.Super (), Nil ) => true
279
- case _ => false
280
- }
260
+ val ctorVar = fileLevelVar(" b" , genName(className))
281
261
282
- if (args.isEmpty && isTrivialCtorBody)
283
- js.Skip ()
284
- else
285
- js.MethodDef (static = false , js.Ident (" constructor" ), args, restParam, body)
262
+ js.Block (
263
+ js.DocComment (" @constructor" ),
264
+ ctorVar := ctorFun,
265
+ chainPrototypeWithLocalCtor(className, ctorVar, superCtor),
266
+ genIdentBracketSelect(ctorVar.prototype, " constructor" ) := ctorVar
267
+ )
286
268
}
287
269
}
288
270
}
@@ -365,17 +347,18 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
365
347
}
366
348
}
367
349
368
- private def genConstructorFunForJSClass (className : ClassName ,
369
- jsConstructorDef : Option [JSConstructorDef ])(
370
- implicit moduleContext : ModuleContext ,
371
- globalKnowledge : GlobalKnowledge , pos : Position ): WithGlobals [js.Function ] = {
350
+ private def chainPrototypeWithLocalCtor (className : ClassName , ctorVar : js.Tree ,
351
+ superCtor : js.Tree )(implicit pos : Position ): js.Tree = {
352
+ import TreeDSL ._
372
353
373
- val JSConstructorDef (_, params, restParam, body) = jsConstructorDef.getOrElse {
374
- throw new IllegalArgumentException (
375
- s " $className does not have an exported constructor " )
376
- }
354
+ val dummyCtor = fileLevelVar(" hh" , genName(className))
377
355
378
- desugarToFunction(className, params, restParam, body)
356
+ js.Block (
357
+ js.DocComment (" @constructor" ),
358
+ genConst(dummyCtor.ident, js.Function (false , Nil , None , js.Skip ())),
359
+ dummyCtor.prototype := superCtor.prototype,
360
+ ctorVar.prototype := js.New (dummyCtor, Nil )
361
+ )
379
362
}
380
363
381
364
/** Generates the creation of fields for a Scala class. */
0 commit comments