Skip to content

Commit 18505cb

Browse files
committed
Store the JSDefinitions in a custom platform SJSPlatform.
This required the ability to instantiate a different `Platform` depending on settings, which, in turn, required to defer the initialization of `ContextBase.platform`.
1 parent cd4004a commit 18505cb

File tree

6 files changed

+45
-21
lines changed

6 files changed

+45
-21
lines changed

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,12 @@ import Symbols._
88
import StdNames._
99
import Decorators._
1010

11+
import dotty.tools.dotc.config.SJSPlatform
12+
1113
object JSDefinitions {
12-
@dotty.tools.sharable
13-
private val cache = new java.util.WeakHashMap[ContextBase, JSDefinitions]
14-
15-
// TODO Figure out where best to define this
16-
def jsdefn(implicit ctx: Context): JSDefinitions = cache.synchronized {
17-
val baseCtx = ctx.base
18-
val cached = cache.get(baseCtx)
19-
if (cached != null) {
20-
cached
21-
} else {
22-
val newJSDefinitions = new JSDefinitions()
23-
cache.put(baseCtx, newJSDefinitions)
24-
newJSDefinitions
25-
}
26-
}
14+
/** The Scala.js-specific definitions for the current context. */
15+
def jsdefn(implicit ctx: Context): JSDefinitions =
16+
ctx.platform.asInstanceOf[SJSPlatform].jsDefinitions
2717
}
2818

2919
final class JSDefinitions()(implicit ctx: Context) {

src/dotty/tools/dotc/Compiler.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class Compiler {
102102
* imports For each element of RootImports, an import context
103103
*/
104104
def rootContext(implicit ctx: Context): Context = {
105-
ctx.definitions.init(ctx)
105+
ctx.initialize()(ctx)
106106
val actualPhases = if (ctx.settings.scalajs.value) {
107107
phases
108108
} else {
@@ -123,7 +123,7 @@ class Compiler {
123123
.setTyper(new Typer)
124124
.setMode(Mode.ImplicitsEnabled)
125125
.setTyperState(new MutableTyperState(ctx.typerState, ctx.typerState.reporter, isCommittable = true))
126-
ctx.definitions.init(start) // set context of definitions to start
126+
ctx.initialize()(start) // re-initialize the base context with start
127127
def addImport(ctx: Context, refFn: () => TermRef) =
128128
ctx.fresh.setImportInfo(ImportInfo.rootImport(refFn)(ctx))
129129
(start.setRunInfo(new RunInfo(start)) /: defn.RootImportFns)(addImport)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package dotty.tools.dotc.config
2+
3+
import dotty.tools.dotc.core._
4+
import Contexts._
5+
6+
import dotty.tools.backend.sjs.JSDefinitions
7+
8+
class SJSPlatform()(implicit ctx: Context) extends JavaPlatform {
9+
10+
/** Scala.js-specific definitions. */
11+
val jsDefinitions: JSDefinitions = new JSDefinitions()
12+
13+
}

src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import reporting._
2626
import collection.mutable
2727
import collection.immutable.BitSet
2828
import printing._
29-
import config.{Settings, ScalaSettings, Platform, JavaPlatform}
29+
import config.{Settings, ScalaSettings, Platform, JavaPlatform, SJSPlatform}
3030
import language.implicitConversions
3131
import DenotTransformers.DenotTransformer
3232

@@ -514,8 +514,21 @@ object Contexts {
514514
/** The symbol loaders */
515515
val loaders = new SymbolLoaders
516516

517+
/** The platform, initialized by `initPlatform()`. */
518+
private var _platform: Platform = _
519+
517520
/** The platform */
518-
val platform: Platform = new JavaPlatform
521+
def platform: Platform = {
522+
if (_platform == null) {
523+
throw new IllegalStateException(
524+
"initialize() must be called before accessing platform")
525+
}
526+
_platform
527+
}
528+
529+
protected def newPlatform(implicit ctx: Context): Platform =
530+
if (settings.scalajs.value) new SJSPlatform
531+
else new JavaPlatform
519532

520533
/** The loader that loads the members of _root_ */
521534
def rootLoader(root: TermSymbol)(implicit ctx: Context): SymbolLoader = platform.rootLoader(root)
@@ -526,6 +539,14 @@ object Contexts {
526539
/** The standard definitions */
527540
val definitions = new Definitions
528541

542+
/** Initializes the `ContextBase` with a starting context.
543+
* This initializes the `platform` and the `definitions`.
544+
*/
545+
def initialize()(implicit ctx: Context): Unit = {
546+
_platform = newPlatform
547+
definitions.init
548+
}
549+
529550
def squashed(p: Phase): Phase = {
530551
allPhases.find(_.period.containsPhaseId(p.id)).getOrElse(NoPhase)
531552
}

src/dotty/tools/dotc/transform/Pickler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class Pickler extends Phase {
8080

8181
private def testUnpickler(units: List[CompilationUnit])(implicit ctx: Context): Unit = {
8282
pickling.println(i"testing unpickler at run ${ctx.runId}")
83-
ctx.definitions.init
83+
ctx.initialize()
8484
val unpicklers =
8585
for (unit <- units; (cls, pickler) <- unit.picklers) yield {
8686
val unpickler = new DottyUnpickler(pickler.assembleParts())

test/test/DottyTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class DottyTest /*extends ContextEscapeDetection*/ {
2222
val base = new ContextBase
2323
import base.settings._
2424
val ctx = base.initialCtx.fresh
25-
base.definitions.init(ctx)
25+
base.initialize()(ctx)
2626
ctx
2727
}
2828
/*

0 commit comments

Comments
 (0)