Skip to content

Commit 4ae678e

Browse files
committed
Scala.js: Handle static methods in the backend.
1 parent 368ded1 commit 4ae678e

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

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

+31-17
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,13 @@ class JSCodeGen()(implicit ctx: Context) {
679679
methodName, jsParams, jstpe.NoType,
680680
Some(genStat(rhs)))(optimizerHints, None)
681681
} else {
682-
val namespace =
682+
val namespace = if (isMethodStaticInIR(sym)) {
683+
if (sym.isPrivate) js.MemberNamespace.PrivateStatic
684+
else js.MemberNamespace.PublicStatic
685+
} else {
683686
if (sym.isPrivate) js.MemberNamespace.Private
684687
else js.MemberNamespace.Public
688+
}
685689
val resultIRType = toIRType(patchedResultType(sym))
686690
genMethodDef(namespace, methodName,
687691
params, resultIRType, rhs, optimizerHints)
@@ -873,16 +877,14 @@ class JSCodeGen()(implicit ctx: Context) {
873877
genApplyDynamic(app)*/
874878

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

887889
case Select(qualifier, _) =>
888890
val sym = tree.symbol
@@ -1840,7 +1842,9 @@ class JSCodeGen()(implicit ctx: Context) {
18401842
case _ => false
18411843
}
18421844

1843-
if (isJSType(sym.owner)) {
1845+
if (isMethodStaticInIR(sym)) {
1846+
genApplyStatic(sym, genActualArgs(sym, args))
1847+
} else if (isJSType(sym.owner)) {
18441848
//if (!isScalaJSDefinedJSClass(sym.owner) || isExposed(sym))
18451849
genApplyJSMethodGeneric(tree, sym, genExprOrGlobalScope(receiver), genActualJSArgs(sym, args), isStat)
18461850
/*else
@@ -2111,9 +2115,12 @@ class JSCodeGen()(implicit ctx: Context) {
21112115
case t @ Ident(_) => (t, Nil)
21122116
}
21132117
val sym = fun.symbol
2118+
val isStaticCall = isMethodStaticInIR(sym)
21142119

21152120
val qualifier = qualifierOf(fun)
2116-
val allCaptureValues = qualifier :: env
2121+
val allCaptureValues =
2122+
if (isStaticCall) env
2123+
else qualifier :: env
21172124

21182125
val formalAndActualCaptures = allCaptureValues.map { value =>
21192126
implicit val pos = value.span
@@ -2142,9 +2149,13 @@ class JSCodeGen()(implicit ctx: Context) {
21422149
val (formalParams, actualParams) = formalAndActualParams.unzip
21432150

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

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

@@ -2886,6 +2897,9 @@ class JSCodeGen()(implicit ctx: Context) {
28862897
}
28872898
}
28882899

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

0 commit comments

Comments
 (0)