Skip to content

Commit 816a3cd

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 abc097e commit 816a3cd

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class Compiler {
102102
* imports For each element of RootImports, an import context
103103
*/
104104
def rootContext(implicit ctx: Context): Context = {
105+
ctx.initPlatform()(ctx)
105106
ctx.definitions.init(ctx)
106107
val actualPhases = if (ctx.settings.scalajs.value) {
107108
phases
@@ -124,6 +125,7 @@ class Compiler {
124125
.setMode(Mode.ImplicitsEnabled)
125126
.setTyperState(new MutableTyperState(ctx.typerState, ctx.typerState.reporter, isCommittable = true))
126127
ctx.definitions.init(start) // set context of definitions to start
128+
ctx.initPlatform()(start) // re-initialize the platform with start
127129
def addImport(ctx: Context, refFn: () => TermRef) =
128130
ctx.fresh.setImportInfo(ImportInfo.rootImport(refFn)(ctx))
129131
(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: 17 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,23 @@ 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+
"initPlatform() must be called before accessing platform")
525+
}
526+
_platform
527+
}
528+
529+
def initPlatform()(implicit ctx: Context): Unit = {
530+
_platform =
531+
if (settings.scalajs.value) new SJSPlatform()
532+
else new JavaPlatform
533+
}
519534

520535
/** The loader that loads the members of _root_ */
521536
def rootLoader(root: TermSymbol)(implicit ctx: Context): SymbolLoader = platform.rootLoader(root)

0 commit comments

Comments
 (0)