Allow passing a WebAssembly.Module in --no-modules

I've noticed this in a few cases where it's sometimes easy to have a
`WebAssembly.Module` on-hand for the `--no-modules` mode where you don't
want to necessarily `fetch`. This commit changes the exported
initialization function in `--no-modules` mode to support both!
This commit is contained in:
Alex Crichton 2018-10-16 10:48:07 -07:00
parent 0b59657603
commit 34b75c35b2
2 changed files with 32 additions and 27 deletions

View File

@ -422,17 +422,22 @@ impl<'a> Context<'a> {
var wasm; var wasm;
const __exports = {{}}; const __exports = {{}};
{globals} {globals}
function init(wasm_path) {{ function init(path_or_module) {{
const fetchPromise = fetch(wasm_path); let instantiation;
let resultPromise; const imports = {{ './{module}': __exports }};
if (typeof WebAssembly.instantiateStreaming === 'function') {{ if (path_or_module instanceof WebAssembly.Module) {{
resultPromise = WebAssembly.instantiateStreaming(fetchPromise, {{ './{module}': __exports }}); instantiation = WebAssembly.instantiate(path_or_module, imports);
}} else {{ }} else {{
resultPromise = fetchPromise const data = fetch(path_or_module);
if (typeof WebAssembly.instantiateStreaming === 'function') {{
instantiation = WebAssembly.instantiateStreaming(data, imports);
}} else {{
instantiation = data
.then(response => response.arrayBuffer()) .then(response => response.arrayBuffer())
.then(buffer => WebAssembly.instantiate(buffer, {{ './{module}': __exports }})); .then(buffer => WebAssembly.instantiate(buffer, imports));
}} }}
return resultPromise.then(({{instance}}) => {{ }}
return instantiation.then(({{instance}}) => {{
wasm = init.wasm = instance.exports; wasm = init.wasm = instance.exports;
return; return;
}}); }});

View File

@ -59,14 +59,14 @@ for the wasm-module-to-be. The page is configured with one exported global, in
this case `wasm_bindgen`. The name of this global can be configured with the this case `wasm_bindgen`. The name of this global can be configured with the
`--no-modules-global` option. `--no-modules-global` option.
The global `wasm_bindgen` is a function that takes one argument, the path to the The global `wasm_bindgen` is a function that takes one argument: either the path
wasm file. When invoked `wasm_bindgen` will return a promise for when the wasm to the wasm file to fetch or a `WebAssembly.Module`. When invoked `wasm_bindgen`
file is ready-to-go. After that all exported functionality on will return a promise for when the wasm module is ready-to-go. After that all
`wasm_bindgen` will be functional. exported functionality on `wasm_bindgen` will be functional.
In the example above, after calling `wasm_bindgen('./hello_bg.wasm')` we wait In the example above, after calling `wasm_bindgen('./hello_bg.wasm')` we wait
for the wasm module to be compiled, and afterwards we're invoking our `greet` for the wasm module to be fetched and compiled, and afterwards we're invoking
export. our `greet` export.
Note that exports are available for binding before the wasm module has been Note that exports are available for binding before the wasm module has been
instantiated, for example this would have also worked: instantiated, for example this would have also worked: