mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-11-27 19:28:56 +03:00
Merge pull request #969 from alexcrichton/no-modules-moudle
Allow passing a `WebAssembly.Module` in `--no-modules`
This commit is contained in:
commit
995aefa87e
@ -418,27 +418,32 @@ impl<'a> Context<'a> {
|
|||||||
let mut js = if self.config.no_modules {
|
let mut js = if self.config.no_modules {
|
||||||
format!(
|
format!(
|
||||||
"\
|
"\
|
||||||
(function() {{
|
(function() {{
|
||||||
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);
|
||||||
.then(response => response.arrayBuffer())
|
if (typeof WebAssembly.instantiateStreaming === 'function') {{
|
||||||
.then(buffer => WebAssembly.instantiate(buffer, {{ './{module}': __exports }}));
|
instantiation = WebAssembly.instantiateStreaming(data, imports);
|
||||||
}}
|
}} else {{
|
||||||
return resultPromise.then(({{instance}}) => {{
|
instantiation = data
|
||||||
wasm = init.wasm = instance.exports;
|
.then(response => response.arrayBuffer())
|
||||||
return;
|
.then(buffer => WebAssembly.instantiate(buffer, imports));
|
||||||
}});
|
}}
|
||||||
}};
|
}}
|
||||||
self.{global_name} = Object.assign(init, __exports);
|
return instantiation.then(({{instance}}) => {{
|
||||||
}})();",
|
wasm = init.wasm = instance.exports;
|
||||||
|
return;
|
||||||
|
}});
|
||||||
|
}};
|
||||||
|
self.{global_name} = Object.assign(init, __exports);
|
||||||
|
}})();",
|
||||||
globals = self.globals,
|
globals = self.globals,
|
||||||
module = module_name,
|
module = module_name,
|
||||||
global_name = self.config.no_modules_global
|
global_name = self.config.no_modules_global
|
||||||
|
@ -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:
|
||||||
|
Loading…
Reference in New Issue
Block a user