bytecodealliance / ComponentizeJS

JS -> WebAssembly Component
Apache License 2.0
235 stars 31 forks source link

Question - what happens if my JS is using a emscripten generated wasm file? #39

Open GordonSmith opened 1 year ago

GordonSmith commented 1 year ago

Say I have a js library that is already using an emscripten generated wasm file and I try to componentize that, given spider monkey supports wasm, can I "just" bundle the existing wasm as if it were any other resource (like a jpeg)?

guybedford commented 1 year ago

Because the component model supports core imports equally, and components can embed any number of other components or modules, in theory it should just become another core module embedded in the outer component.

There are some techniques we have in mind to support this kind of use case, it will just involve some tricks to properly handle.

So no, it's not just bundled like a jpeg, it would be wrapped specially within the component model constructs so that instead of running that Wasm on a virtual WebAssembly runtime, it would just punch through to the outer Wasm runtime and run on that.

GordonSmith commented 1 year ago

I can imagine how this would work for components embedded in components, but will this be possible for a "emscripten generated wasm file" (which was my way of saying "not a component" or more realistically a third party wasm file, which I don't have any intimate knowledge about)?

guybedford commented 1 year ago

If I'm understanding your scenario correctly, then the answer is yes - if you are componentizing JS which depends on core Wasm, as a component, then the final component should be possible to work using these techniques since components can depend on and instantiate core wasm. And this (hypothetical) technique would automatically inline that core wasm and its bindings regardless.

It would rely on being able to statically analyze the JS to determine where Wasm is being instantiated though. But that should be possible to automatically pick up for standard instantiation code patterns in JS.