Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Implement use-site JS interop. #15

Merged
merged 2 commits into from
Mar 14, 2024
Merged

Conversation

sjrd
Copy link
Collaborator

@sjrd sjrd commented Mar 13, 2024

This is all of the JS interop semantics at "use site". Not included are: exports, non-native JS classes and closure creation.

sjrd added 2 commits March 13, 2024 18:45
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.
Copy link
Owner

@tanishiking tanishiking left a 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 = {
Copy link
Owner

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),

Comment on lines +1487 to +1491
private def genJSGlobalRef(tree: IRTrees.JSGlobalRef): IRTypes.Type = {
genLiteral(IRTrees.StringLiteral(tree.name)(tree.pos))
instrs += CALL(FuncIdx(WasmFunctionName.jsGlobalRefGet))
IRTypes.AnyType
}
Copy link
Owner

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],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +1384 to +1390
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)
}
Copy link
Owner

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 = {
Copy link
Owner

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),

Comment on lines +1376 to +1382
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)
}
Copy link
Owner

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 (?)

@tanishiking tanishiking merged commit a03e78b into tanishiking:main Mar 14, 2024
1 check passed
@sjrd sjrd deleted the js-interop branch March 14, 2024 08:59
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants