Skip to content

Commit 6ed3959

Browse files
committed
Scala.js: Handle static methods in the backend.
1 parent 4a16b80 commit 6ed3959

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

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

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -680,9 +680,13 @@ class JSCodeGen()(implicit ctx: Context) {
680680
methodName, jsParams, jstpe.NoType,
681681
Some(genStat(rhs)))(optimizerHints, None)
682682
} else {
683-
val namespace =
683+
val namespace = if (isMethodStaticInIR(sym)) {
684+
if (sym.isPrivate) js.MemberNamespace.PrivateStatic
685+
else js.MemberNamespace.PublicStatic
686+
} else {
684687
if (sym.isPrivate) js.MemberNamespace.Private
685688
else js.MemberNamespace.Public
689+
}
686690
val resultIRType = toIRType(patchedResultType(sym))
687691
genMethodDef(namespace, methodName,
688692
params, resultIRType, rhs, optimizerHints)
@@ -874,16 +878,14 @@ class JSCodeGen()(implicit ctx: Context) {
874878
genApplyDynamic(app)*/
875879

876880
case tree: This =>
877-
if (tree.symbol == currentClassSym.get) {
878-
genThis()
879-
} else {
880-
assert(tree.symbol.is(Module),
881-
"Trying to access the this of another class: " +
882-
"tree.symbol = " + tree.symbol +
883-
", class symbol = " + currentClassSym.get +
884-
" span:" + pos)
881+
val currentClass = currentClassSym.get
882+
val symIsModuleClass = tree.symbol.is(ModuleClass)
883+
assert(tree.symbol == currentClass || symIsModuleClass,
884+
s"Trying to access the this of another class: tree.symbol = ${tree.symbol}, class symbol = $currentClass")
885+
if (symIsModuleClass && tree.symbol != currentClass)
885886
genLoadModule(tree.symbol)
886-
}
887+
else
888+
genThis()
887889

888890
case Select(qualifier, _) =>
889891
val sym = tree.symbol
@@ -1841,7 +1843,9 @@ class JSCodeGen()(implicit ctx: Context) {
18411843
case _ => false
18421844
}
18431845

1844-
if (isJSType(sym.owner)) {
1846+
if (isMethodStaticInIR(sym)) {
1847+
genApplyStatic(sym, genActualArgs(sym, args))
1848+
} else if (isJSType(sym.owner)) {
18451849
//if (!isScalaJSDefinedJSClass(sym.owner) || isExposed(sym))
18461850
genApplyJSMethodGeneric(tree, sym, genExprOrGlobalScope(receiver), genActualJSArgs(sym, args), isStat)
18471851
/*else
@@ -2112,9 +2116,12 @@ class JSCodeGen()(implicit ctx: Context) {
21122116
case t @ Ident(_) => (t, Nil)
21132117
}
21142118
val sym = fun.symbol
2119+
val isStaticCall = isMethodStaticInIR(sym)
21152120

21162121
val qualifier = qualifierOf(fun)
2117-
val allCaptureValues = qualifier :: env
2122+
val allCaptureValues =
2123+
if (isStaticCall) env
2124+
else qualifier :: env
21182125

21192126
val formalAndActualCaptures = allCaptureValues.map { value =>
21202127
implicit val pos = value.span
@@ -2143,9 +2150,13 @@ class JSCodeGen()(implicit ctx: Context) {
21432150
val (formalParams, actualParams) = formalAndActualParams.unzip
21442151

21452152
val genBody = {
2146-
val thisCaptureRef :: argCaptureRefs = formalCaptures.map(_.ref)
2147-
val call = genApplyMethodMaybeStatically(thisCaptureRef, sym,
2148-
argCaptureRefs ::: actualParams)
2153+
val call = if (isStaticCall) {
2154+
genApplyStatic(sym, formalCaptures.map(_.ref))
2155+
} else {
2156+
val thisCaptureRef :: argCaptureRefs = formalCaptures.map(_.ref)
2157+
genApplyMethodMaybeStatically(thisCaptureRef, sym,
2158+
argCaptureRefs ::: actualParams)
2159+
}
21492160
box(call, sym.info.finalResultType)
21502161
}
21512162

@@ -2294,8 +2305,8 @@ class JSCodeGen()(implicit ctx: Context) {
22942305
/** Gen a call to a static method. */
22952306
private def genApplyStatic(method: Symbol, arguments: List[js.Tree])(
22962307
implicit pos: Position): js.Tree = {
2297-
js.ApplyStatic(js.ApplyFlags.empty, encodeClassRef(method.owner),
2298-
encodeMethodSym(method), arguments)(
2308+
js.ApplyStatic(js.ApplyFlags.empty.withPrivate(method.isPrivate),
2309+
encodeClassRef(method.owner), encodeMethodSym(method), arguments)(
22992310
toIRType(patchedResultType(method)))
23002311
}
23012312

@@ -2887,6 +2898,9 @@ class JSCodeGen()(implicit ctx: Context) {
28872898
}
28882899
}
28892900

2901+
private def isMethodStaticInIR(sym: Symbol): Boolean =
2902+
sym.is(JavaStatic, butNot = JavaDefined)
2903+
28902904
/** Generate a Class[_] value (e.g. coming from classOf[T]) */
28912905
private def genClassConstant(tpe: Type)(implicit pos: Position): js.Tree =
28922906
js.ClassOf(toTypeRef(tpe))

0 commit comments

Comments
 (0)