Skip to content

Commit

Permalink
Support polyfilling multi-memory (#125)
Browse files Browse the repository at this point in the history
* Support polyfilling multi-memory

Currently JS does not generally support the WebAssembly multi-memory
proposal. Wasmtime will, however, generate adapter modules which use
multi-memory to communicate between components. This means that composed
components are typically not compatible with the transpile process as
they produce a core module that doesn't actually run in any JS runtime.

This commit fixes this issue by adding polyfill support for
multi-memory. Whenever an adapter is produced that uses multi-memory jco
will now rewrite the module such that any references to memory that
isn't at index 0 to be indirected through functions. These imported
functions then operate on the specified memory on behalf of the wasm
itself. This is a horribly slow process because all memory reads/writes
become function calls, but given that the baseline is otherwise "does
not work" it's hopefully a bit better than before.

The end goal here is to work up towards strings between components, but
for now this just gets everything else working with multi-memory such as
transferring lists.

* Fill out comments

---------

Co-authored-by: Guy Bedford <[email protected]>
  • Loading branch information
alexcrichton and guybedford authored Aug 8, 2023
1 parent 69b4b80 commit 31d3eab
Show file tree
Hide file tree
Showing 11 changed files with 1,192 additions and 18 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/js-component-bindgen-component/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ anyhow = { workspace = true }
js-component-bindgen = { path = "../js-component-bindgen" }
wasmtime-environ = { workspace = true }
wit-bindgen = { workspace = true }
wat = { workspace = true }
1 change: 1 addition & 0 deletions crates/js-component-bindgen-component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct JsComponentBindgenComponent;

impl JsComponentBindgen for JsComponentBindgenComponent {
fn generate(component: Vec<u8>, options: GenerateOptions) -> Result<Transpiled, String> {
let component = wat::parse_bytes(&component).map_err(|e| format!("{e}"))?;
let opts = js_component_bindgen::TranspileOpts {
name: options.name,
no_typescript: options.no_typescript.unwrap_or(false),
Expand Down
1 change: 1 addition & 0 deletions crates/js-component-bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ wit-component = { workspace = true }
wit-parser = { workspace = true }
indexmap = { workspace = true }
base64 = { workspace = true }
wasm-encoder = { workspace = true }
Loading

0 comments on commit 31d3eab

Please sign in to comment.