Skip to content

Commit

Permalink
Fix scala#9785: Scala.js: Handle the typeof JSGlobalRef(_) corner c…
Browse files Browse the repository at this point in the history
…ase.

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(_))`.
  • Loading branch information
sjrd committed Sep 14, 2020
1 parent 9b3a352 commit 4eb3310
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2932,7 +2932,11 @@ class JSCodeGen()(using genCtx: Context) {
case TYPEOF =>
// js.typeOf(arg)
val arg = genArgs1
genAsInstanceOf(js.JSUnaryOp(js.JSUnaryOp.typeof, arg), defn.StringType)
val typeofExpr = arg match {
case arg: js.JSGlobalRef => js.JSTypeOfGlobalRef(arg)
case _ => js.JSUnaryOp(js.JSUnaryOp.typeof, arg)
}
js.AsInstanceOf(typeofExpr, jstpe.ClassType(jsNames.BoxedStringClass))

case STRICT_EQ =>
// js.special.strictEquals(arg1, arg2)
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/SelectStatic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import dotty.tools.dotc.core._
import dotty.tools.dotc.transform.MegaPhase._
import dotty.tools.dotc.transform.SymUtils._

import dotty.tools.backend.sjs.JSDefinitions.jsdefn

/** Removes selects that would be compiled into GetStatic
* otherwise backend needs to be aware that some qualifiers need to be dropped.
* Similar transformation seems to be performed by flatten in nsc
Expand All @@ -31,6 +33,7 @@ class SelectStatic extends MiniPhase with IdentityDenotTransformer {
val tree1 =
if isStaticRef && !tree.qualifier.symbol.isAllOf(JavaModule) && !tree.qualifier.isType then
if isPureExpr(tree.qualifier) then ref(sym)
else if ctx.settings.scalajs.value && sym.is(Module) && sym.moduleClass.hasAnnotation(jsdefn.JSGlobalScopeAnnot) then ref(sym)
else Block(List(tree.qualifier), ref(sym))
else tree

Expand Down
7 changes: 6 additions & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,12 @@ object Build {
scalaJSLinkerConfig ~= { _.withSemantics(build.TestSuiteLinkerOptions.semantics _) },
scalaJSModuleInitializers in Test ++= build.TestSuiteLinkerOptions.moduleInitializers,

jsEnvInput in Test := {
val resourceDir = fetchScalaJSSource.value / "test-suite/js/src/test/resources"
val f = (resourceDir / "NonNativeJSTypeTestNatives.js").toPath
org.scalajs.jsenv.Input.Script(f) +: (jsEnvInput in Test).value
},

managedSources in Compile ++= {
val dir = fetchScalaJSSource.value / "test-suite/js/src/main/scala"
val filter = (
Expand Down Expand Up @@ -1049,7 +1055,6 @@ object Build {
-- "ExportsTest.scala" // JS exports
-- "IterableTest.scala" // non-native JS classes
-- "JSExportStaticTest.scala" // JS exports
-- "JSNativeInPackage.scala" // #9785 tests fail due to js.typeOf(globalVar) being incorrect
-- "JSOptionalTest.scala" // non-native JS classes
-- "JSSymbolTest.scala" // non-native JS classes
-- "MiscInteropTest.scala" // non-native JS classes
Expand Down

0 comments on commit 4eb3310

Please sign in to comment.