Skip to content

Commit c36c27d

Browse files
committed
Scala.js: Handle SAMs for js.ThisFunction's.
1 parent 13c3210 commit c36c27d

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,20 +2472,34 @@ class JSCodeGen()(using genCtx: Context) {
24722472
box(call, sym.info.finalResultType)
24732473
}
24742474

2475-
val closure = js.Closure(arrow = true, formalCaptures, formalParams, genBody, actualCaptures)
2476-
report.debuglog(closure.toString)
2477-
24782475
val funInterfaceSym = functionalInterface.tpe.widenDealias.typeSymbol
2479-
if (jsdefn.isJSFunctionClass(funInterfaceSym)) {
2480-
closure
2476+
2477+
if (jsdefn.isJSThisFunctionClass(funInterfaceSym)) {
2478+
val thisParam :: otherParams = formalParams
2479+
js.Closure(
2480+
arrow = false,
2481+
formalCaptures,
2482+
otherParams,
2483+
js.Block(
2484+
js.VarDef(thisParam.name, thisParam.originalName,
2485+
thisParam.ptpe, mutable = false,
2486+
js.This()(thisParam.ptpe)(thisParam.pos))(thisParam.pos),
2487+
genBody),
2488+
actualCaptures)
24812489
} else {
2482-
assert(!funInterfaceSym.exists || defn.isFunctionClass(funInterfaceSym),
2483-
s"Invalid functional interface $funInterfaceSym reached the back-end")
2484-
val formalCount = formalParams.size
2485-
val cls = ClassName("scala.scalajs.runtime.AnonFunction" + formalCount)
2486-
val ctorName = MethodName.constructor(
2487-
jstpe.ClassRef(ClassName("scala.scalajs.js.Function" + formalCount)) :: Nil)
2488-
js.New(cls, js.MethodIdent(ctorName), List(closure))
2490+
val closure = js.Closure(arrow = true, formalCaptures, formalParams, genBody, actualCaptures)
2491+
2492+
if (jsdefn.isJSFunctionClass(funInterfaceSym)) {
2493+
closure
2494+
} else {
2495+
assert(!funInterfaceSym.exists || defn.isFunctionClass(funInterfaceSym),
2496+
s"Invalid functional interface $funInterfaceSym reached the back-end")
2497+
val formalCount = formalParams.size
2498+
val cls = ClassName("scala.scalajs.runtime.AnonFunction" + formalCount)
2499+
val ctorName = MethodName.constructor(
2500+
jstpe.ClassRef(ClassName("scala.scalajs.js.Function" + formalCount)) :: Nil)
2501+
js.New(cls, js.MethodIdent(ctorName), List(closure))
2502+
}
24892503
}
24902504
}
24912505

compiler/src/dotty/tools/dotc/config/SJSPlatform.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class SJSPlatform()(using Context) extends JavaPlatform {
1313

1414
/** Is the SAMType `cls` also a SAM under the rules of the Scala.js back-end? */
1515
override def isSam(cls: ClassSymbol)(using Context): Boolean =
16-
defn.isFunctionClass(cls) || jsDefinitions.isJSFunctionClass(cls)
16+
defn.isFunctionClass(cls)
17+
|| jsDefinitions.isJSFunctionClass(cls)
18+
|| jsDefinitions.isJSThisFunctionClass(cls)
1719
}
18-

project/Build.scala

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,9 +1094,8 @@ object Build {
10941094
-- "NestedJSClassTest.scala" // non-native JS classes
10951095
-- "NonNativeJSTypeTest.scala" // non-native JS classes
10961096
-- "PromiseMock.scala" // non-native JS classes
1097-
-- "SpecialTest.scala" // assertion error in ExpandSAMs
1097+
-- "SpecialTest.scala" // not yet implemented JS-specific primitive
10981098
-- "SymbolTest.scala" // IR checking errors
1099-
-- "ThisFunctionTest.scala" // assertion error in ExpandSAMs
11001099
-- "UndefOrTest.scala" // StackOverflow in the compiler
11011100
)).get
11021101

@@ -1118,11 +1117,7 @@ object Build {
11181117

11191118
++ (dir / "js/src/test/scala/org/scalajs/testsuite/niobuffer" ** "*.scala").get
11201119
++ (dir / "js/src/test/scala/org/scalajs/testsuite/scalalib" ** "*.scala").get
1121-
1122-
++ (dir / "js/src/test/scala/org/scalajs/testsuite/typedarray" ** (("*.scala": FileFilter)
1123-
-- "TypedArrayTest.scala" // assertion error in ExpandSAMs
1124-
)).get
1125-
1120+
++ (dir / "js/src/test/scala/org/scalajs/testsuite/typedarray" ** "*.scala").get
11261121
++ (dir / "js/src/test/scala/org/scalajs/testsuite/utils" ** "*.scala").get
11271122

11281123
++ (dir / "js/src/test/require-2.12" ** (("*.scala": FileFilter)

0 commit comments

Comments
 (0)