Support polyfilling multi-memory #125
Merged
guybedford merged 4 commits intobytecodealliance:mainfrom Aug 8, 2023
Merged
Conversation
This commit updates the Wasmtime dependency to the, currently unreleased, Wasmtime 12.0.0 branch. A number of changes happened to component translation in Wasmtime 12 to account for resources which required changes here as well. Additionally Wasmtime 12 disallows empty types in components which WASI was previously using, so this additionally updates the preview1 shims and test components.
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.
Member
Author
guybedford
reviewed
Aug 7, 2023
Collaborator
guybedford
left a comment
There was a problem hiding this comment.
Fantastic, I've been worried about this. Will try it out first thing tomorrow.
guybedford
reviewed
Aug 7, 2023
| //! support for more instructions but such support isn't required at this time. | ||
| //! Examples of unsupported instructions are `i64.load8_u` and `memory.copy`. | ||
| //! Additionally core wasm sections such as data sections and tables are not | ||
| //! supported because, again, Wasmtime doesn't use it at this time. |
Collaborator
There was a problem hiding this comment.
I was wondering if it might make sense to use a full wasm2js solution here (eg something like https://github.com/evanw/polywasm), but this certainly seems adequate as well.
Member
Author
There was a problem hiding this comment.
I think any of those would work myself, this was just the first thing I thought of so I figured I'd take a stab implementing it while stuck delayed in an airport :)
If you'd prefer to go with those though I think it's ok to hold this open or close it, either way's fine by me
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.