Skip to content

Commit 3543467

Browse files
committed
Scala.js: Fix references to fields in other compilation units.
1 parent b9792c3 commit 3543467

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,19 @@ final class JSDefinitions()(implicit ctx: Context) {
173173
@threadUnsafe lazy val Reflect_registerInstantiatableClassR = ReflectModule.requiredMethodRef("registerInstantiatableClass")
174174
def Reflect_registerInstantiatableClass(implicit ctx: Context) = Reflect_registerInstantiatableClassR.symbol
175175

176+
private[this] var allRefClassesCache: Set[Symbol] = _
177+
def allRefClasses(implicit ctx: Context): Set[Symbol] = {
178+
if (allRefClassesCache == null) {
179+
val baseNames = List("Object", "Boolean", "Character", "Byte", "Short",
180+
"Int", "Long", "Float", "Double")
181+
val fullNames = baseNames.flatMap { base =>
182+
List(s"scala.runtime.${base}Ref", s"scala.runtime.Volatile${base}Ref")
183+
}
184+
allRefClassesCache = fullNames.map(name => ctx.requiredClass(name)).toSet
185+
}
186+
allRefClassesCache
187+
}
188+
176189
/** If `cls` is a class in the scala package, its name, otherwise EmptyTypeName */
177190
private def scalajsClassName(cls: Symbol)(implicit ctx: Context): TypeName =
178191
if (cls.isClass && cls.owner == ScalaJSJSPackageClass) cls.asClass.name

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

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package dotty.tools.backend.sjs
22

3+
import scala.annotation.tailrec
4+
35
import scala.collection.mutable
46

57
import dotty.tools.FatalError
68

79
import dotty.tools.dotc.core._
10+
import Decorators._
811
import Periods._
912
import SymDenotations._
1013
import Contexts._
14+
import Flags._
1115
import Types._
1216
import Symbols._
1317
import Denotations._
@@ -103,13 +107,6 @@ object JSEncoding {
103107
js.Ident(localNames.localSymbolName(sym), Some(sym.unexpandedName.decoded))
104108
}
105109

106-
private def allRefClasses(implicit ctx: Context): Set[Symbol] = {
107-
//TODO
108-
/*(Set(ObjectRefClass, VolatileObjectRefClass) ++
109-
refClass.values ++ volatileRefClass.values)*/
110-
Set()
111-
}
112-
113110
def encodeFieldSym(sym: Symbol)(
114111
implicit ctx: Context, pos: ir.Position): js.Ident = {
115112
require(sym.owner.isClass && sym.isTerm && !sym.is(Flags.Method) && !sym.is(Flags.Module),
@@ -120,14 +117,19 @@ object JSEncoding {
120117
if (name0.charAt(name0.length()-1) != ' ') name0
121118
else name0.substring(0, name0.length()-1)
122119

120+
@tailrec
121+
def superClassCount(sym: Symbol, acc: Int): Int =
122+
if (sym == defn.ObjectClass) acc
123+
else superClassCount(sym.asClass.superClass, acc + 1)
124+
123125
/* We have to special-case fields of Ref types (IntRef, ObjectRef, etc.)
124126
* because they are emitted as private by our .scala source files, but
125127
* they are considered public at use site since their symbols come from
126128
* Java-emitted .class files.
127129
*/
128130
val idSuffix =
129-
if (sym.is(Flags.Private) || allRefClasses.contains(sym.owner))
130-
sym.owner.asClass.baseClasses.size.toString
131+
if (sym.is(Flags.Private) || jsdefn.allRefClasses.contains(sym.owner))
132+
superClassCount(sym.owner, 0).toString
131133
else
132134
"f"
133135

project/Build.scala

+1
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,7 @@ object Build {
948948
(
949949
(dir / "shared/src/test/scala/org/scalajs/testsuite/compiler" ** "IntTest.scala").get
950950
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/javalib/lang" ** "IntegerTest.scala").get
951+
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/javalib/lang" ** "ObjectTest.scala").get
951952
++ (dir / "shared/src/test/require-jdk8/org/scalajs/testsuite/javalib/util" ** "Base64Test.scala").get
952953
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/utils" ** "*.scala").get
953954
)

0 commit comments

Comments
 (0)