Skip to content

Commit

Permalink
Scala.js: Fix references to fields in other compilation units.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjrd committed Aug 27, 2019
1 parent b9792c3 commit 3543467
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
13 changes: 13 additions & 0 deletions compiler/src/dotty/tools/backend/sjs/JSDefinitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ final class JSDefinitions()(implicit ctx: Context) {
@threadUnsafe lazy val Reflect_registerInstantiatableClassR = ReflectModule.requiredMethodRef("registerInstantiatableClass")
def Reflect_registerInstantiatableClass(implicit ctx: Context) = Reflect_registerInstantiatableClassR.symbol

private[this] var allRefClassesCache: Set[Symbol] = _
def allRefClasses(implicit ctx: Context): Set[Symbol] = {
if (allRefClassesCache == null) {
val baseNames = List("Object", "Boolean", "Character", "Byte", "Short",
"Int", "Long", "Float", "Double")
val fullNames = baseNames.flatMap { base =>
List(s"scala.runtime.${base}Ref", s"scala.runtime.Volatile${base}Ref")
}
allRefClassesCache = fullNames.map(name => ctx.requiredClass(name)).toSet
}
allRefClassesCache
}

/** If `cls` is a class in the scala package, its name, otherwise EmptyTypeName */
private def scalajsClassName(cls: Symbol)(implicit ctx: Context): TypeName =
if (cls.isClass && cls.owner == ScalaJSJSPackageClass) cls.asClass.name
Expand Down
20 changes: 11 additions & 9 deletions compiler/src/dotty/tools/backend/sjs/JSEncoding.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package dotty.tools.backend.sjs

import scala.annotation.tailrec

import scala.collection.mutable

import dotty.tools.FatalError

import dotty.tools.dotc.core._
import Decorators._
import Periods._
import SymDenotations._
import Contexts._
import Flags._
import Types._
import Symbols._
import Denotations._
Expand Down Expand Up @@ -103,13 +107,6 @@ object JSEncoding {
js.Ident(localNames.localSymbolName(sym), Some(sym.unexpandedName.decoded))
}

private def allRefClasses(implicit ctx: Context): Set[Symbol] = {
//TODO
/*(Set(ObjectRefClass, VolatileObjectRefClass) ++
refClass.values ++ volatileRefClass.values)*/
Set()
}

def encodeFieldSym(sym: Symbol)(
implicit ctx: Context, pos: ir.Position): js.Ident = {
require(sym.owner.isClass && sym.isTerm && !sym.is(Flags.Method) && !sym.is(Flags.Module),
Expand All @@ -120,14 +117,19 @@ object JSEncoding {
if (name0.charAt(name0.length()-1) != ' ') name0
else name0.substring(0, name0.length()-1)

@tailrec
def superClassCount(sym: Symbol, acc: Int): Int =
if (sym == defn.ObjectClass) acc
else superClassCount(sym.asClass.superClass, acc + 1)

/* We have to special-case fields of Ref types (IntRef, ObjectRef, etc.)
* because they are emitted as private by our .scala source files, but
* they are considered public at use site since their symbols come from
* Java-emitted .class files.
*/
val idSuffix =
if (sym.is(Flags.Private) || allRefClasses.contains(sym.owner))
sym.owner.asClass.baseClasses.size.toString
if (sym.is(Flags.Private) || jsdefn.allRefClasses.contains(sym.owner))
superClassCount(sym.owner, 0).toString
else
"f"

Expand Down
1 change: 1 addition & 0 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,7 @@ object Build {
(
(dir / "shared/src/test/scala/org/scalajs/testsuite/compiler" ** "IntTest.scala").get
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/javalib/lang" ** "IntegerTest.scala").get
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/javalib/lang" ** "ObjectTest.scala").get
++ (dir / "shared/src/test/require-jdk8/org/scalajs/testsuite/javalib/util" ** "Base64Test.scala").get
++ (dir / "shared/src/test/scala/org/scalajs/testsuite/utils" ** "*.scala").get
)
Expand Down

0 comments on commit 3543467

Please sign in to comment.