From 18505cb2061235dba1b064b9379164e2a48254a6 Mon Sep 17 00:00:00 2001 From: Sébastien Doeraene Date: Mon, 29 Feb 2016 12:48:06 +0100 Subject: 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`. --- src/dotty/tools/backend/sjs/JSDefinitions.scala | 20 +++++--------------- src/dotty/tools/dotc/Compiler.scala | 4 ++-- src/dotty/tools/dotc/config/SJSPlatform.scala | 13 +++++++++++++ src/dotty/tools/dotc/core/Contexts.scala | 25 +++++++++++++++++++++++-- src/dotty/tools/dotc/transform/Pickler.scala | 2 +- 5 files changed, 44 insertions(+), 20 deletions(-) create mode 100644 src/dotty/tools/dotc/config/SJSPlatform.scala (limited to 'src') diff --git a/src/dotty/tools/backend/sjs/JSDefinitions.scala b/src/dotty/tools/backend/sjs/JSDefinitions.scala index 1ee560b35..0f4415b31 100644 --- a/src/dotty/tools/backend/sjs/JSDefinitions.scala +++ b/src/dotty/tools/backend/sjs/JSDefinitions.scala @@ -8,22 +8,12 @@ import Symbols._ import StdNames._ import Decorators._ +import dotty.tools.dotc.config.SJSPlatform + object JSDefinitions { - @dotty.tools.sharable - private val cache = new java.util.WeakHashMap[ContextBase, JSDefinitions] - - // TODO Figure out where best to define this - def jsdefn(implicit ctx: Context): JSDefinitions = cache.synchronized { - val baseCtx = ctx.base - val cached = cache.get(baseCtx) - if (cached != null) { - cached - } else { - val newJSDefinitions = new JSDefinitions() - cache.put(baseCtx, newJSDefinitions) - newJSDefinitions - } - } + /** The Scala.js-specific definitions for the current context. */ + def jsdefn(implicit ctx: Context): JSDefinitions = + ctx.platform.asInstanceOf[SJSPlatform].jsDefinitions } final class JSDefinitions()(implicit ctx: Context) { diff --git a/src/dotty/tools/dotc/Compiler.scala b/src/dotty/tools/dotc/Compiler.scala index 2a3346486..a4e0998bf 100644 --- a/src/dotty/tools/dotc/Compiler.scala +++ b/src/dotty/tools/dotc/Compiler.scala @@ -102,7 +102,7 @@ class Compiler { * imports For each element of RootImports, an import context */ def rootContext(implicit ctx: Context): Context = { - ctx.definitions.init(ctx) + ctx.initialize()(ctx) val actualPhases = if (ctx.settings.scalajs.value) { phases } else { @@ -123,7 +123,7 @@ class Compiler { .setTyper(new Typer) .setMode(Mode.ImplicitsEnabled) .setTyperState(new MutableTyperState(ctx.typerState, ctx.typerState.reporter, isCommittable = true)) - ctx.definitions.init(start) // set context of definitions to start + ctx.initialize()(start) // re-initialize the base context with start def addImport(ctx: Context, refFn: () => TermRef) = ctx.fresh.setImportInfo(ImportInfo.rootImport(refFn)(ctx)) (start.setRunInfo(new RunInfo(start)) /: defn.RootImportFns)(addImport) diff --git a/src/dotty/tools/dotc/config/SJSPlatform.scala b/src/dotty/tools/dotc/config/SJSPlatform.scala new file mode 100644 index 000000000..fec9c25a1 --- /dev/null +++ b/src/dotty/tools/dotc/config/SJSPlatform.scala @@ -0,0 +1,13 @@ +package dotty.tools.dotc.config + +import dotty.tools.dotc.core._ +import Contexts._ + +import dotty.tools.backend.sjs.JSDefinitions + +class SJSPlatform()(implicit ctx: Context) extends JavaPlatform { + + /** Scala.js-specific definitions. */ + val jsDefinitions: JSDefinitions = new JSDefinitions() + +} diff --git a/src/dotty/tools/dotc/core/Contexts.scala b/src/dotty/tools/dotc/core/Contexts.scala index f0537dffa..2fc958a49 100644 --- a/src/dotty/tools/dotc/core/Contexts.scala +++ b/src/dotty/tools/dotc/core/Contexts.scala @@ -26,7 +26,7 @@ import reporting._ import collection.mutable import collection.immutable.BitSet import printing._ -import config.{Settings, ScalaSettings, Platform, JavaPlatform} +import config.{Settings, ScalaSettings, Platform, JavaPlatform, SJSPlatform} import language.implicitConversions import DenotTransformers.DenotTransformer @@ -514,8 +514,21 @@ object Contexts { /** The symbol loaders */ val loaders = new SymbolLoaders + /** The platform, initialized by `initPlatform()`. */ + private var _platform: Platform = _ + /** The platform */ - val platform: Platform = new JavaPlatform + def platform: Platform = { + if (_platform == null) { + throw new IllegalStateException( + "initialize() must be called before accessing platform") + } + _platform + } + + protected def newPlatform(implicit ctx: Context): Platform = + if (settings.scalajs.value) new SJSPlatform + else new JavaPlatform /** The loader that loads the members of _root_ */ def rootLoader(root: TermSymbol)(implicit ctx: Context): SymbolLoader = platform.rootLoader(root) @@ -526,6 +539,14 @@ object Contexts { /** The standard definitions */ val definitions = new Definitions + /** Initializes the `ContextBase` with a starting context. + * This initializes the `platform` and the `definitions`. + */ + def initialize()(implicit ctx: Context): Unit = { + _platform = newPlatform + definitions.init + } + def squashed(p: Phase): Phase = { allPhases.find(_.period.containsPhaseId(p.id)).getOrElse(NoPhase) } diff --git a/src/dotty/tools/dotc/transform/Pickler.scala b/src/dotty/tools/dotc/transform/Pickler.scala index 8040c86d4..c5b223d53 100644 --- a/src/dotty/tools/dotc/transform/Pickler.scala +++ b/src/dotty/tools/dotc/transform/Pickler.scala @@ -80,7 +80,7 @@ class Pickler extends Phase { private def testUnpickler(units: List[CompilationUnit])(implicit ctx: Context): Unit = { pickling.println(i"testing unpickler at run ${ctx.runId}") - ctx.definitions.init + ctx.initialize() val unpicklers = for (unit <- units; (cls, pickler) <- unit.picklers) yield { val unpickler = new DottyUnpickler(pickler.assembleParts()) -- cgit v1.2.3