Skip to content

Commit 4eb3310

Browse files
committed
Fix scala#9785: Scala.js: Handle the typeof JSGlobalRef(_) corner case.
This requires two things: - make sure that `SelectStatic` does not expand references to global scopes, and - when compiling `js.typeOf(globalRef)`, emit a `JSTypeOfGlobalRef(_)` instead of a `JSUnaryOp(typeof, JSGlobalRef(_))`.
1 parent 9b3a352 commit 4eb3310

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2932,7 +2932,11 @@ class JSCodeGen()(using genCtx: Context) {
29322932
case TYPEOF =>
29332933
// js.typeOf(arg)
29342934
val arg = genArgs1
2935-
genAsInstanceOf(js.JSUnaryOp(js.JSUnaryOp.typeof, arg), defn.StringType)
2935+
val typeofExpr = arg match {
2936+
case arg: js.JSGlobalRef => js.JSTypeOfGlobalRef(arg)
2937+
case _ => js.JSUnaryOp(js.JSUnaryOp.typeof, arg)
2938+
}
2939+
js.AsInstanceOf(typeofExpr, jstpe.ClassType(jsNames.BoxedStringClass))
29362940

29372941
case STRICT_EQ =>
29382942
// js.special.strictEquals(arg1, arg2)

compiler/src/dotty/tools/dotc/transform/SelectStatic.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import dotty.tools.dotc.core._
1111
import dotty.tools.dotc.transform.MegaPhase._
1212
import dotty.tools.dotc.transform.SymUtils._
1313

14+
import dotty.tools.backend.sjs.JSDefinitions.jsdefn
15+
1416
/** Removes selects that would be compiled into GetStatic
1517
* otherwise backend needs to be aware that some qualifiers need to be dropped.
1618
* Similar transformation seems to be performed by flatten in nsc
@@ -31,6 +33,7 @@ class SelectStatic extends MiniPhase with IdentityDenotTransformer {
3133
val tree1 =
3234
if isStaticRef && !tree.qualifier.symbol.isAllOf(JavaModule) && !tree.qualifier.isType then
3335
if isPureExpr(tree.qualifier) then ref(sym)
36+
else if ctx.settings.scalajs.value && sym.is(Module) && sym.moduleClass.hasAnnotation(jsdefn.JSGlobalScopeAnnot) then ref(sym)
3437
else Block(List(tree.qualifier), ref(sym))
3538
else tree
3639

project/Build.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,12 @@ object Build {
10091009
scalaJSLinkerConfig ~= { _.withSemantics(build.TestSuiteLinkerOptions.semantics _) },
10101010
scalaJSModuleInitializers in Test ++= build.TestSuiteLinkerOptions.moduleInitializers,
10111011

1012+
jsEnvInput in Test := {
1013+
val resourceDir = fetchScalaJSSource.value / "test-suite/js/src/test/resources"
1014+
val f = (resourceDir / "NonNativeJSTypeTestNatives.js").toPath
1015+
org.scalajs.jsenv.Input.Script(f) +: (jsEnvInput in Test).value
1016+
},
1017+
10121018
managedSources in Compile ++= {
10131019
val dir = fetchScalaJSSource.value / "test-suite/js/src/main/scala"
10141020
val filter = (
@@ -1049,7 +1055,6 @@ object Build {
10491055
-- "ExportsTest.scala" // JS exports
10501056
-- "IterableTest.scala" // non-native JS classes
10511057
-- "JSExportStaticTest.scala" // JS exports
1052-
-- "JSNativeInPackage.scala" // #9785 tests fail due to js.typeOf(globalVar) being incorrect
10531058
-- "JSOptionalTest.scala" // non-native JS classes
10541059
-- "JSSymbolTest.scala" // non-native JS classes
10551060
-- "MiscInteropTest.scala" // non-native JS classes

0 commit comments

Comments
 (0)