-
Notifications
You must be signed in to change notification settings - Fork 3
Conversation
We allocate one Global for each distinct string literal encountered in the program. During the module's `start` function, we initialize them all.
This is all of the JS interop semantics at "use site". Not included are: exports, non-native JS classes and closure creation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! LGTM 👍
IRTypes.AnyType | ||
} | ||
|
||
private def genJSFunctionApply(tree: IRTrees.JSFunctionApply): IRTypes.Type = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[note]
e.g. js.eval(...)
JSFunctionApply(
JSGlobalRef(eval),
List(...) // List[TreeOrJSSpread]
)
jsFunctionApply: (f, args) => f(...args),
private def genJSGlobalRef(tree: IRTrees.JSGlobalRef): IRTypes.Type = { | ||
genLiteral(IRTrees.StringLiteral(tree.name)(tree.pos)) | ||
instrs += CALL(FuncIdx(WasmFunctionName.jsGlobalRefGet)) | ||
IRTypes.AnyType | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[note]
jsGlobalRefGet: (globalRefName) => (new Function("return " + globalRefName))(),
@@ -206,7 +295,9 @@ object WasmContext { | |||
private val fields: List[WasmFieldName], | |||
val superClass: Option[IRNames.ClassName], | |||
val interfaces: List[IRNames.ClassName], | |||
val ancestors: List[IRNames.ClassName] | |||
val ancestors: List[IRNames.ClassName], | |||
val jsNativeLoadSpec: Option[IRTrees.JSNativeLoadSpec], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[note]
JSNativeLoadSpec
will be stored for the definition with @js.native
annotation, somewheere around here
- https://github.com/scala-js/scala-js/blob/8dd02c784818d6d3d4a18ebf53cc0797212ef12e/compiler/src/main/scala/org/scalajs/nscplugin/PrepJSInterop.scala#L809-L953
- https://github.com/scala-js/scala-js/blob/8dd02c784818d6d3d4a18ebf53cc0797212ef12e/compiler/src/main/scala/org/scalajs/nscplugin/PrepJSInterop.scala#L791-L796
private def genLoadJSModule(tree: IRTrees.LoadJSModule): IRTypes.Type = { | ||
val info = ctx.getClassInfo(tree.className) | ||
val jsNativeLoadSpec = info.jsNativeLoadSpec.getOrElse { | ||
throw new AssertionError(s"Found $tree for class without jsNativeLoadSpec at ${tree.pos}") | ||
} | ||
genLoadJSNativeLoadSpec(jsNativeLoadSpec)(tree.pos) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[note]
js.Math.PI
// compiles to
JSSelect(LoadJSModule(ClassName<scala.scalajs.js.Math$>),StringLiteral(PI))
// where jsNativeLoadSpec is
Global(Math,List())
genLoadJSNativeLoadSpec
will be a set of JsGlobalRefGet
+ JsSelect
(for following the path)
IRTypes.AnyType | ||
} | ||
|
||
private def genJSMethodApply(tree: IRTrees.JSMethodApply): IRTypes.Type = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[note]
e.g.
js.Math.clz32(6548)
// compiles to
JSMethodApply(
LoadJSModule(ClassName<scala.scalajs.js.Math$>),
StringLiteral(clz32),
List(IntLiteral(6548))
)
where jsMethodApply
is
jsMethodApply: (o, m, args) => o[m](...args),
private def genLoadJSConstructor(tree: IRTrees.LoadJSConstructor): IRTypes.Type = { | ||
val info = ctx.getClassInfo(tree.className) | ||
val jsNativeLoadSpec = info.jsNativeLoadSpec.getOrElse { | ||
throw new AssertionError(s"Found $tree for class without jsNativeLoadSpec at ${tree.pos}") | ||
} | ||
genLoadJSNativeLoadSpec(jsNativeLoadSpec)(tree.pos) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new js.Date(1710190169564.0)
// compiles to
JSNew(LoadJSConstructor(ClassName<scala.scalajs.js.Date>),List(DoubleLiteral(1710190169564)))
// where jsNativeLoadSpec is
Global(Date,List())
same as genLoadJSModule
(?)
This is all of the JS interop semantics at "use site". Not included are: exports, non-native JS classes and closure creation.