The following example (see our compile-streaming.html demo on GitHub, and view it live also) directly streams a .wasm module from an underlying source then compiles it to a WebAssembly.Module
object. Because the compileStreaming()
function accepts a promise for a Response
object, you can directly pass it a fetch()
call, and it will pass the response into the function when it fulfills.
const importObject = { imports: { imported_func: (arg) => console.log(arg) } };
WebAssembly.compileStreaming(fetch('simple.wasm'))
.then((module) => WebAssembly.instantiate(module, importObject))
.then((instance) => instance.exports.exported_func());
The resulting module instance is then instantiated using WebAssembly.instantiate()
, and the exported function invoked.