Skip to content

Commit 43b65e2

Browse files
committed
Add support for the primitives js.import(specifier) and js.import.meta.
These changes are not tested in this repo, because we do not have the infrastructure for tests requiring ECMAScript Modules. They should be straightforward enough that it is not really necessary. All the magic of these two features happens in the linker, not in the compiler.
1 parent 2b615f5 commit 43b65e2

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3695,6 +3695,15 @@ class JSCodeGen()(using genCtx: Context) {
36953695
// BoxedUnit.UNIT, which is the boxed version of ()
36963696
js.Undefined()
36973697

3698+
case JS_IMPORT =>
3699+
// js.import(arg)
3700+
val arg = genArgs1
3701+
js.JSImportCall(arg)
3702+
3703+
case JS_IMPORT_META =>
3704+
// js.import.meta
3705+
js.JSImportMeta()
3706+
36983707
case JS_NATIVE =>
36993708
// js.native
37003709
report.error(

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ final class JSDefinitions()(using Context) {
145145
@threadUnsafe lazy val JSConstructorTag_materializeR = JSConstructorTagModule.requiredMethodRef("materialize")
146146
def JSConstructorTag_materialize(using Context) = JSConstructorTag_materializeR.symbol
147147

148+
@threadUnsafe lazy val JSImportModuleRef = requiredModuleRef("scala.scalajs.js.import")
149+
def JSImportModule(using Context) = JSImportModuleRef.symbol
150+
@threadUnsafe lazy val JSImport_applyR = JSImportModule.requiredMethodRef(nme.apply)
151+
def JSImport_apply(using Context) = JSImport_applyR.symbol
152+
@threadUnsafe lazy val JSImport_metaR = JSImportModule.requiredMethodRef("meta")
153+
def JSImport_meta(using Context) = JSImport_metaR.symbol
154+
148155
@threadUnsafe lazy val RuntimePackageVal = requiredPackage("scala.scalajs.runtime")
149156
@threadUnsafe lazy val RuntimePackageClass = RuntimePackageVal.moduleClass.asClass
150157
@threadUnsafe lazy val RuntimePackage_wrapJavaScriptExceptionR = RuntimePackageClass.requiredMethodRef("wrapJavaScriptException")

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ object JSPrimitives {
2727

2828
final val UNITVAL = JS_NATIVE + 1 // () value, which is undefined
2929

30-
final val CONSTRUCTOROF = UNITVAL + 1 // runtime.constructorOf(clazz)
30+
final val JS_IMPORT = UNITVAL + 1 // js.import.apply(specifier)
31+
final val JS_IMPORT_META = JS_IMPORT + 1 // js.import.meta
32+
33+
final val CONSTRUCTOROF = JS_IMPORT_META + 1 // runtime.constructorOf(clazz)
3134
final val CREATE_INNER_JS_CLASS = CONSTRUCTOROF + 1 // runtime.createInnerJSClass
3235
final val CREATE_LOCAL_JS_CLASS = CREATE_INNER_JS_CLASS + 1 // runtime.createLocalJSClass
3336
final val WITH_CONTEXTUAL_JS_CLASS_VALUE = CREATE_LOCAL_JS_CLASS + 1 // runtime.withContextualJSClassValue
@@ -106,6 +109,9 @@ class JSPrimitives(ictx: Context) extends DottyPrimitives(ictx) {
106109

107110
addPrimitive(defn.BoxedUnit_UNIT, UNITVAL)
108111

112+
addPrimitive(jsdefn.JSImport_apply, JS_IMPORT)
113+
addPrimitive(jsdefn.JSImport_meta, JS_IMPORT_META)
114+
109115
addPrimitive(jsdefn.Runtime_constructorOf, CONSTRUCTOROF)
110116
addPrimitive(jsdefn.Runtime_createInnerJSClass, CREATE_INNER_JS_CLASS)
111117
addPrimitive(jsdefn.Runtime_createLocalJSClass, CREATE_LOCAL_JS_CLASS)

0 commit comments

Comments
 (0)