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

Actually use our linker as the linker for sample/fastLinkJS. #47

Merged
merged 10 commits into from
Apr 1, 2024

Conversation

sjrd
Copy link
Collaborator

@sjrd sjrd commented Mar 28, 2024

Draft, just to get the CI to run.

Main changes:

  1. Cross-compile the wasm project.
  2. Turn the Compiler into a linker that actually implements the Linker and LinkerBackend interfaces of Scala.js.
  3. Set up the sample project to use our WebAssembly linker in sample/fastLinkJS (and therefore in sample/run).

@sjrd sjrd force-pushed the true-linker-backend branch 6 times, most recently from 5a392db to 4d9423d Compare March 29, 2024 18:57
@sjrd sjrd changed the title WiP True linker backend Actually use our linker as the linker for sample/fastLinkJS. Mar 29, 2024
@sjrd sjrd marked this pull request as ready for review March 29, 2024 18:59
@sjrd sjrd requested a review from tanishiking March 29, 2024 18:59
We will need the Wasm backend to be built for 2.12.x in order to
load it as a true linker from the sbt build.
This way, we do not depend on Scala.js-specific APIs.
An `OutputDirectory` is not supposed to be shared.
And delete stray files in the output directories, as is mandated by
the contract of a linker backend.
This follows the architecture of the Scala.js linker.
In addition to emitting the `.wasm` and `.wat` files in the output
directory, we now also emit emit the internal `__loader.js` file,
and the public module specified by the `ModuleSet`, such as
`main.js`.

We embed the content of the loader as a constant string in the
backend, so that it works both on the JVM and on JS. Eventually, we
will probably generate it more dynamically from
`backend.javascript.Trees` anyway, so this not really a big deal.

This change requires to adapt the loading mechanism to force URL
resolution against the base directory of the `loader.js` file,
rather than the working directly. We do this thanks to
`import.meta.url`, which is also available in browsers.

As is, this change destroys the semantics of top-level exported
`var`s. Mutations performed after the module initializers have
completed will not be reflected in the exports of `main.js`. We
will need to revisit this later on, with a technique similar to the
one used by the JS backend.

With these changes, our linker backend actually abides by the
specification of a `LinkerBackend`, which means we will be able to
directly use it from sbt.
@sjrd
Copy link
Collaborator Author

sjrd commented Mar 31, 2024

Rebased on top of #50.

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.

I left one comment, but otherwise looks good to me, great job!
feel free to merge this PR once you address the comment (if it's required)

wasm/src/main/scala/wasm4s/WasmContext.scala Show resolved Hide resolved
Comment on lines 67 to 70
val linker = WebAssemblyLinkerImpl.linker(linkerConfig)
val outputDir = "./target/sample/"
createDir(outputDir)
val output = NodeOutputDirectory(outputDir)
Copy link
Owner

Choose a reason for hiding this comment

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

This else branch is no longer needed. When we run sample/fastLinkJS, it's going to be handled by WasmLInkerPlugin.
Maybe we can remove this branch? I was a bit confused why sample/fastLinkJS doesn't generate output under ./target/sample.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point. I removed it.

Now, `sample/fastLinkJS` uses our WebAssembly linker, and
`sample/run` actually executes the `main` method of the sample
project, linked to WebAssembly.
@sjrd sjrd merged commit 515d055 into tanishiking:main Apr 1, 2024
1 check passed
@sjrd sjrd deleted the true-linker-backend branch April 1, 2024 08:49
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