Skip to content

Commit d47ab77

Browse files
committed
Implement loading static fields in the Scala.js back-end.
1 parent f8ebf77 commit d47ab77

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,9 +718,9 @@ class JSCodeGen()(implicit ctx: Context) {
718718
if (sym.is(Module)) {
719719
assert(!sym.is(Package), "Cannot use package as value: " + tree)
720720
genLoadModule(sym)
721-
} else /*if (sym.isStaticMember) {
722-
genStaticMember(sym)
723-
} else if (paramAccessorLocals contains sym) {
721+
} else if (sym.is(JavaStatic)) {
722+
genLoadStaticField(sym)
723+
} else /*if (paramAccessorLocals contains sym) {
724724
paramAccessorLocals(sym).ref
725725
} else if (isScalaJSDefinedJSClass(sym.owner)) {
726726
val genQual = genExpr(qualifier)
@@ -2328,6 +2328,24 @@ class JSCodeGen()(implicit ctx: Context) {
23282328
}
23292329
}
23302330

2331+
/** Gen JS code for loading a Java static field.
2332+
*/
2333+
private def genLoadStaticField(sym: Symbol)(implicit pos: Position): js.Tree = {
2334+
/* Actually, there is no static member in Scala.js. If we come here, that
2335+
* is because we found the symbol in a Java-emitted .class in the
2336+
* classpath. But the corresponding implementation in Scala.js will
2337+
* actually be a val in the companion module.
2338+
*/
2339+
2340+
if (sym == defn.BoxedUnit_UNIT) {
2341+
js.Undefined()
2342+
} else {
2343+
val instance = genLoadModule(sym.owner)
2344+
val method = encodeStaticMemberSym(sym)
2345+
js.Apply(instance, method, Nil)(toIRType(sym.info))
2346+
}
2347+
}
2348+
23312349
/** Gen JS code for loading a module.
23322350
*
23332351
* Can be given either the module symbol, or its module class symbol.

0 commit comments

Comments
 (0)