@@ -26,7 +26,7 @@ import dotty.tools.dotc.util.SourcePosition
26
26
import dotty .tools .dotc .report
27
27
28
28
import dotty .tools .sjs .ir
29
- import dotty .tools .sjs .ir .{ClassKind , Position , Names => jsNames , Trees => js , Types => jstpe }
29
+ import dotty .tools .sjs .ir .{ClassKind , Position , Trees => js , Types => jstpe , WellKnownNames => jswkn }
30
30
import dotty .tools .sjs .ir .Names .{ClassName , LocalName , MethodName , SimpleMethodName }
31
31
import dotty .tools .sjs .ir .OriginalName
32
32
import dotty .tools .sjs .ir .OriginalName .NoOriginalName
@@ -133,7 +133,7 @@ class JSCodeGen()(using genCtx: Context) {
133
133
def currentThisType : jstpe.Type = {
134
134
currentThisTypeNullable match {
135
135
case tpe @ jstpe.ClassType (cls, _) =>
136
- jstpe .BoxedClassToPrimType .getOrElse(cls, tpe.toNonNullable)
136
+ jswkn .BoxedClassToPrimType .getOrElse(cls, tpe.toNonNullable)
137
137
case tpe @ jstpe.AnyType =>
138
138
// We are in a JS class, in which even `this` is nullable
139
139
tpe
@@ -424,7 +424,7 @@ class JSCodeGen()(using genCtx: Context) {
424
424
425
425
val staticInitializerStats = reflectInit ::: staticModuleInit
426
426
if (staticInitializerStats.nonEmpty)
427
- List (genStaticConstructorWithStats(ir. Names .StaticInitializerName , js.Block (staticInitializerStats)))
427
+ List (genStaticConstructorWithStats(jswkn .StaticInitializerName , js.Block (staticInitializerStats)))
428
428
else
429
429
Nil
430
430
}
@@ -453,7 +453,7 @@ class JSCodeGen()(using genCtx: Context) {
453
453
originalName,
454
454
ClassKind .Class ,
455
455
None ,
456
- Some (js.ClassIdent (ir. Names .ObjectClass )),
456
+ Some (js.ClassIdent (jswkn .ObjectClass )),
457
457
Nil ,
458
458
None ,
459
459
None ,
@@ -574,7 +574,7 @@ class JSCodeGen()(using genCtx: Context) {
574
574
575
575
if (staticFields.nonEmpty) {
576
576
generatedMethods +=
577
- genStaticConstructorWithStats(ir. Names .ClassInitializerName , genLoadModule(companionModuleClass))
577
+ genStaticConstructorWithStats(jswkn .ClassInitializerName , genLoadModule(companionModuleClass))
578
578
}
579
579
580
580
(staticFields, staticExports)
@@ -1000,7 +1000,7 @@ class JSCodeGen()(using genCtx: Context) {
1000
1000
val fqcnArg = js.StringLiteral (sym.fullName.toString)
1001
1001
val runtimeClassArg = js.ClassOf (toTypeRef(sym.info))
1002
1002
val loadModuleFunArg =
1003
- js.Closure (arrow = true , Nil , Nil , None , genLoadModule(sym), Nil )
1003
+ js.Closure (js. ClosureFlags . arrow, Nil , Nil , None , jstpe. AnyType , genLoadModule(sym), Nil )
1004
1004
1005
1005
val stat = genApplyMethod(
1006
1006
genLoadModule(jsdefn.ReflectModule ),
@@ -1035,7 +1035,7 @@ class JSCodeGen()(using genCtx: Context) {
1035
1035
1036
1036
val paramTypesArray = js.JSArrayConstr (parameterTypes)
1037
1037
1038
- val newInstanceFun = js.Closure (arrow = true , Nil , formalParams, None , {
1038
+ val newInstanceFun = js.Closure (js. ClosureFlags . arrow, Nil , formalParams, None , jstpe. AnyType , {
1039
1039
js.New (encodeClassName(sym), encodeMethodSym(ctor), actualParams)
1040
1040
}, Nil )
1041
1041
@@ -2389,7 +2389,7 @@ class JSCodeGen()(using genCtx: Context) {
2389
2389
// Make new class def with static members
2390
2390
val newClassDef = {
2391
2391
implicit val pos = originalClassDef.pos
2392
- val parent = js.ClassIdent (jsNames .ObjectClass )
2392
+ val parent = js.ClassIdent (jswkn .ObjectClass )
2393
2393
js.ClassDef (originalClassDef.name, originalClassDef.originalName,
2394
2394
ClassKind .AbstractJSType , None , Some (parent), interfaces = Nil ,
2395
2395
jsSuperClass = None , jsNativeLoadSpec = None , fields = Nil ,
@@ -2427,7 +2427,7 @@ class JSCodeGen()(using genCtx: Context) {
2427
2427
js.VarRef (selfIdent.name)(jstpe.AnyType )
2428
2428
2429
2429
def memberLambda (params : List [js.ParamDef ], restParam : Option [js.ParamDef ], body : js.Tree )(implicit pos : ir.Position ): js.Closure =
2430
- js.Closure (arrow = false , captureParams = Nil , params, restParam, body, captureValues = Nil )
2430
+ js.Closure (js. ClosureFlags .function , captureParams = Nil , params, restParam, jstpe. AnyType , body, captureValues = Nil )
2431
2431
2432
2432
val fieldDefinitions = jsFieldDefs.toList.map { fdef =>
2433
2433
implicit val pos = fdef.pos
@@ -2539,7 +2539,8 @@ class JSCodeGen()(using genCtx: Context) {
2539
2539
beforeSuper ::: superCall ::: afterSuper
2540
2540
}
2541
2541
2542
- val closure = js.Closure (arrow = true , jsClassCaptures, Nil , None ,
2542
+ // Wrap everything in a lambda, for namespacing
2543
+ val closure = js.Closure (js.ClosureFlags .arrow, jsClassCaptures, Nil , None , jstpe.AnyType ,
2543
2544
js.Block (inlinedCtorStats, selfRef), jsSuperClassValue :: args)
2544
2545
js.JSFunctionApply (closure, Nil )
2545
2546
}
@@ -3350,7 +3351,7 @@ class JSCodeGen()(using genCtx: Context) {
3350
3351
3351
3352
// Sanity check: we can handle Ints and Strings (including `null`s), but nothing else
3352
3353
genSelector.tpe match {
3353
- case jstpe.IntType | jstpe.ClassType (jsNames .BoxedStringClass , _) | jstpe.NullType | jstpe.NothingType =>
3354
+ case jstpe.IntType | jstpe.ClassType (jswkn .BoxedStringClass , _) | jstpe.NullType | jstpe.NothingType =>
3354
3355
// ok
3355
3356
case _ =>
3356
3357
abortMatch(s " Invalid selector type ${genSelector.tpe}" )
@@ -3514,6 +3515,8 @@ class JSCodeGen()(using genCtx: Context) {
3514
3515
atPhase(elimRepeatedPhase)(samMethod.info.paramInfoss.flatten.exists(_.isRepeatedParam))
3515
3516
}
3516
3517
}
3518
+ val isFunctionXXL =
3519
+ funInterfaceSym.name == tpnme.FunctionXXL && funInterfaceSym.owner == defn.ScalaRuntimePackageClass
3517
3520
3518
3521
val formalParamNames = sym.info.paramNamess.flatten.drop(envSize)
3519
3522
val formalParamTypes = sym.info.paramInfoss.flatten.drop(envSize)
@@ -3523,8 +3526,11 @@ class JSCodeGen()(using genCtx: Context) {
3523
3526
3524
3527
val formalAndActualParams = formalParamNames.lazyZip(formalParamTypes).lazyZip(formalParamRepeateds).map {
3525
3528
(name, tpe, repeated) =>
3529
+ val formalTpe =
3530
+ if (isFunctionXXL) jstpe.ArrayType (ObjectArrayTypeRef , nullable = true )
3531
+ else jstpe.AnyType
3526
3532
val formalParam = js.ParamDef (freshLocalIdent(name),
3527
- OriginalName (name.toString), jstpe. AnyType , mutable = false )
3533
+ OriginalName (name.toString), formalTpe , mutable = false )
3528
3534
val actualParam =
3529
3535
if (repeated) genJSArrayToVarArgs(formalParam.ref)(tree.sourcePos)
3530
3536
else unbox(formalParam.ref, tpe)
@@ -3559,34 +3565,44 @@ class JSCodeGen()(using genCtx: Context) {
3559
3565
if (isThisFunction) {
3560
3566
val thisParam :: otherParams = formalParams : @ unchecked
3561
3567
js.Closure (
3562
- arrow = false ,
3568
+ js. ClosureFlags .function ,
3563
3569
formalCaptures,
3564
3570
otherParams,
3565
3571
restParam,
3572
+ jstpe.AnyType ,
3566
3573
js.Block (
3567
3574
js.VarDef (thisParam.name, thisParam.originalName,
3568
3575
thisParam.ptpe, mutable = false ,
3569
3576
js.This ()(thisParam.ptpe)(thisParam.pos))(thisParam.pos),
3570
3577
genBody),
3571
3578
actualCaptures)
3572
3579
} else {
3573
- val closure = js.Closure (arrow = true , formalCaptures, formalParams, restParam, genBody, actualCaptures)
3580
+ val closure = js.Closure (js.ClosureFlags .typed, formalCaptures,
3581
+ formalParams, restParam, jstpe.AnyType , genBody, actualCaptures)
3574
3582
3575
3583
if (! funInterfaceSym.exists || defn.isFunctionClass(funInterfaceSym)) {
3576
3584
val formalCount = formalParams.size
3577
- val cls = ClassName (" scala.scalajs.runtime.AnonFunction" + formalCount)
3578
- val ctorName = MethodName .constructor(
3579
- jstpe.ClassRef (ClassName (" scala.scalajs.js.Function" + formalCount)) :: Nil )
3580
- js.New (cls, js.MethodIdent (ctorName), List (closure))
3581
- } else if (funInterfaceSym.name == tpnme.FunctionXXL && funInterfaceSym.owner == defn.ScalaRuntimePackageClass ) {
3582
- val cls = ClassName (" scala.scalajs.runtime.AnonFunctionXXL" )
3583
- val ctorName = MethodName .constructor(
3584
- jstpe.ClassRef (ClassName (" scala.scalajs.js.Function1" )) :: Nil )
3585
- js.New (cls, js.MethodIdent (ctorName), List (closure))
3585
+ val descriptor = js.NewLambda .Descriptor (
3586
+ superClass = encodeClassName(defn.AbstractFunctionClass (formalCount)),
3587
+ interfaces = Nil ,
3588
+ methodName = MethodName (applySimpleMethodName, List .fill(formalCount)(jswkn.ObjectRef ), jswkn.ObjectRef ),
3589
+ paramTypes = List .fill(formalCount)(jstpe.AnyType ),
3590
+ resultType = jstpe.AnyType
3591
+ )
3592
+ js.NewLambda (descriptor, closure)(encodeClassType(defn.FunctionSymbol (formalCount)).toNonNullable)
3593
+ } else if (isFunctionXXL) {
3594
+ val descriptor = js.NewLambda .Descriptor (
3595
+ superClass = jswkn.ObjectClass ,
3596
+ interfaces = List (encodeClassName(defn.FunctionXXLClass )),
3597
+ methodName = MethodName (applySimpleMethodName, List (ObjectArrayTypeRef ), jswkn.ObjectRef ),
3598
+ paramTypes = List (jstpe.ArrayType (ObjectArrayTypeRef , nullable = true )),
3599
+ resultType = jstpe.AnyType
3600
+ )
3601
+ js.NewLambda (descriptor, closure)(encodeClassType(funInterfaceSym).toNonNullable)
3586
3602
} else {
3587
3603
assert(funInterfaceSym.isJSType,
3588
3604
s " Invalid functional interface $funInterfaceSym reached the back-end " )
3589
- closure
3605
+ closure.copy(flags = js. ClosureFlags .arrow)
3590
3606
}
3591
3607
}
3592
3608
}
@@ -3699,8 +3715,8 @@ class JSCodeGen()(using genCtx: Context) {
3699
3715
}
3700
3716
3701
3717
private def genThrowClassCastException ()(implicit pos : Position ): js.Tree = {
3702
- js.UnaryOp (js.UnaryOp .Throw , js.New (jsNames .ClassCastExceptionClass ,
3703
- js.MethodIdent (jsNames .NoArgConstructorName ), Nil ))
3718
+ js.UnaryOp (js.UnaryOp .Throw , js.New (jswkn .ClassCastExceptionClass ,
3719
+ js.MethodIdent (jswkn .NoArgConstructorName ), Nil ))
3704
3720
}
3705
3721
3706
3722
/** Gen JS code for an isInstanceOf test (for reference types only) */
@@ -3987,7 +4003,7 @@ class JSCodeGen()(using genCtx: Context) {
3987
4003
case arg : js.JSGlobalRef => js.JSTypeOfGlobalRef (arg)
3988
4004
case _ => js.JSUnaryOp (js.JSUnaryOp .typeof, arg)
3989
4005
}
3990
- js.AsInstanceOf (typeofExpr, jstpe.ClassType (jsNames .BoxedStringClass , nullable = true ))
4006
+ js.AsInstanceOf (typeofExpr, jstpe.ClassType (jswkn .BoxedStringClass , nullable = true ))
3991
4007
3992
4008
case STRICT_EQ =>
3993
4009
// js.special.strictEquals(arg1, arg2)
@@ -4235,7 +4251,7 @@ class JSCodeGen()(using genCtx: Context) {
4235
4251
" literal classOf[T] expressions (typically compiler-generated). " +
4236
4252
" Other uses are not supported in Scala.js." ,
4237
4253
otherTree.sourcePos)
4238
- (jstpe.AnyType , jstpe.ClassRef (jsNames .ObjectClass ))
4254
+ (jstpe.AnyType , jstpe.ClassRef (jswkn .ObjectClass ))
4239
4255
}
4240
4256
4241
4257
// Gen the actual args, downcasting them to the formal param types
@@ -4870,16 +4886,17 @@ object JSCodeGen {
4870
4886
private val JSObjectClassName = ClassName (" scala.scalajs.js.Object" )
4871
4887
private val JavaScriptExceptionClassName = ClassName (" scala.scalajs.js.JavaScriptException" )
4872
4888
4873
- private val ObjectClassRef = jstpe.ClassRef (ir. Names . ObjectClass )
4889
+ private val ObjectArrayTypeRef = jstpe.ArrayTypeRef (jswkn. ObjectRef , 1 )
4874
4890
4891
+ private val applySimpleMethodName = SimpleMethodName (" apply" )
4875
4892
private val newSimpleMethodName = SimpleMethodName (" new" )
4876
4893
4877
- private val selectedValueMethodName = MethodName (" selectedValue" , Nil , ObjectClassRef )
4894
+ private val selectedValueMethodName = MethodName (" selectedValue" , Nil , jswkn. ObjectRef )
4878
4895
4879
4896
private val JLRArrayNewInstanceMethodName =
4880
- MethodName (" newInstance" , List (jstpe.ClassRef (jsNames .ClassClass ), jstpe.ArrayTypeRef (jstpe.IntRef , 1 )), ObjectClassRef )
4897
+ MethodName (" newInstance" , List (jstpe.ClassRef (jswkn .ClassClass ), jstpe.ArrayTypeRef (jstpe.IntRef , 1 )), jswkn. ObjectRef )
4881
4898
4882
- private val ObjectArgConstructorName = MethodName .constructor(List (ObjectClassRef ))
4899
+ private val ObjectArgConstructorName = MethodName .constructor(List (jswkn. ObjectRef ))
4883
4900
4884
4901
private val thisOriginalName = OriginalName (" this" )
4885
4902
0 commit comments