Synchronous compilation
I'm currently porting a library to Wasm and it's users rely on synchronous instantiation. The goal is that users can just embed the library in a script tag and directly use it afterwards just as it's original in javascript.
With WebAssembly.Module I get:
WebAssembly.Compile is disallowed on the main thread, if the buffer size is larger than 4KB...
Is there any synchronous alternative? The current module's buffer weights 97kb including -O3.
It's a bit of a pain, isn't it.
I think there's a gap in the market here, where existing frameworks very much assume that module instantiation is synchronous. For example, require.js takes a "factory function" it its define() function, which instantiates the module synchronously when called.
All these sorts of frameworks will have to adjust I think, to allow libraries to return a Promise which eventually resolves to return the module's exports.
You'll basically have to change your library's API to rely on an async module loader, at the moment - I'm not aware of a realistic WebAssembly alternative.
Just FYI we have a discussion going on about sync instatiation in Webpack https://github.com/webpack/webpack/issues/6433
This limitation isn't really specified. Should it be? Is there agreement among implementations about this 4kB limit? Agreement, specifications and conformance tests here could reduce the risk of incompatibilities between implementations.
Referencing this hacky limitation bypass here.
Any updates on this?
The only update I'm aware of is that we now have better JS fallback options. You can build to JS instead of wasm using -s WASM=0 (in either fastcomp or upstream) and that will support synchronous compilation everywhere.