-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add more JS initialization options #9
Comments
I'd definitely be on board with this! I like the idea of "given the set of nice imports give me a set of low-level imports" as well as the reverse for the exports. Would you be interested in trying your hand at adding these? |
Great, thanks for the quick response! Yes, I'd be happy to give it a try. It looks like the key file is crates/wasm-bindgen-cli-support/src/ts.rs. Any parallel spots I should look for? |
Nah yeah that's it! The file isn't exactly the cleanest of files, but you can see the template down at the bottom |
@TimHambourger ah so actually, you may wish to hold off! I was having some discussion today with others and actually I think the current approach to the wasm module isn't quite right. I think we'll want to generate wasm/js files differently to better respect ES6 module integration with wasm. I'll try to update this repo soon! |
As a general overview though what I'd hope to do is to actually generate a JS file file that starts with something like: import * as wasm from './low-level.wasm'; in the sense that the instantiation of the wasm module would largely be up to the bundler rather than this library. |
Ok! I've rewritten the bindgen tool at the es6-modules branch -- https://github.com/alexcrichton/wasm-bindgen/tree/es6-modules, with some notable info in the commit message. @TimHambourger mind giving that a spin and see if it works for you? |
Ah ok I went ahead and merged to master so this should be fixed now! |
@alexcrichton You move fast! Thanks for the updates and sorry to take a couple days to get back. I definitely appreciate the desire to move instantiation out of this library. I've seen other tools that take a wasm-as-module approach, e.g. wasm-loader for webpack. I like the concept - treat wasm like just another code asset. It seems like the trade-off is less control over how/when the wasm gets compiled and instantiated. Or at least, the existing bundlers I've seen don't seem to give that kind of control. I'm also seeing different APIs from the different bundlers. E.g. But those questions aside, the latest code is working great otherwise. So I'm happy leaving this one closed. And I'm excited to see where the library goes from here. All cool stuff! |
Currently, the CLI-generated TS bindings export a single entry point,
instantiate
. This internally calls WebAssembly.instantiate and then processes the returnedWebAssembly.ResultObject
likeThis is a great general starting point, but it has a couple limitations:
WebAssembly.Module
toinstantiate
, since the overload ofWebAssembly.instantiate
that takes aWebAssembly.Module
doesn't return aWebAssembly.ResultObject
but instead just aWebAssembly.Instance
. So the destructuring above will set bothmodule
andinstance
toundefined
.Would you support adding a lower-level JS API for these use cases? I'm imagining a pair of functions something like
getImports(_imports : Imports)
andinitWithInstance(instance : WebAssembly.Instance)
. It'd be up to consuming code to callWebAssembly.instantiate(Streaming)
, likeThis would also support things like fetching and compiling the WASM once via WebAssembly.compile or WebAssembly.compileStreaming and then passing the compiled
WebAssembly.Module
to multiple worker threads, each of which constructs its ownWebAssembly.Instance
using the cli-generated bindings.The text was updated successfully, but these errors were encountered: