mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-02 02:11:06 +00:00
Tweak initialization with --no-modules
* Have the global `wasm_bindgen` variable be a function which runs initialization rather than exporting an `init` function. * Save off the wasm object on `wasm_bindgen.wasm` so the memory can be accessed * Tidy up the code slightly
This commit is contained in:
parent
98016324f7
commit
8854936599
@ -267,44 +267,36 @@ impl<'a> Context<'a> {
|
|||||||
|
|
||||||
self.rewrite_imports(module_name);
|
self.rewrite_imports(module_name);
|
||||||
|
|
||||||
let import_wasm = if self.config.nodejs {
|
|
||||||
self.footer.push_str(&format!("wasm = require('./{}_bg');",
|
|
||||||
module_name));
|
|
||||||
format!("var wasm;")
|
|
||||||
} else if self.config.no_modules {
|
|
||||||
format!("
|
|
||||||
window.wasm_bindgen.init = function(__wasm_path) {{
|
|
||||||
return fetch(__wasm_path)
|
|
||||||
.then(response => response.arrayBuffer())
|
|
||||||
.then(buffer => WebAssembly.instantiate(buffer, {{ './{module}': __exports }}))
|
|
||||||
.then(({{instance}}) => {{
|
|
||||||
wasm = instance.exports;
|
|
||||||
return;
|
|
||||||
}})
|
|
||||||
.catch(error => {{
|
|
||||||
console.log('Error loading wasm module `{module}`:', error);
|
|
||||||
throw error;
|
|
||||||
}});
|
|
||||||
}};
|
|
||||||
", module = module_name)
|
|
||||||
} else {
|
|
||||||
format!("import * as wasm from './{}_bg';", module_name)
|
|
||||||
};
|
|
||||||
|
|
||||||
let js = if self.config.no_modules {
|
let js = if self.config.no_modules {
|
||||||
format!("
|
format!("
|
||||||
(function() {{
|
(function() {{
|
||||||
let wasm;
|
var wasm;
|
||||||
const __exports = {{}};
|
const __exports = {{}};
|
||||||
{globals}
|
{globals}
|
||||||
window.wasm_bindgen = Object.assign({{}}, __exports);
|
function init(wasm_path) {{
|
||||||
{import_wasm}
|
return fetch(wasm_path)
|
||||||
|
.then(response => response.arrayBuffer())
|
||||||
|
.then(buffer => WebAssembly.instantiate(buffer, {{ './{module}': __exports }}))
|
||||||
|
.then(({{instance}}) => {{
|
||||||
|
wasm = init.wasm = instance.exports;
|
||||||
|
return;
|
||||||
|
}});
|
||||||
|
}};
|
||||||
|
window.wasm_bindgen = Object.assign(init, __exports);
|
||||||
}})();
|
}})();
|
||||||
",
|
",
|
||||||
globals = self.globals,
|
globals = self.globals,
|
||||||
import_wasm = import_wasm,
|
module = module_name,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
let import_wasm = if self.config.nodejs {
|
||||||
|
self.footer.push_str(&format!("wasm = require('./{}_bg');",
|
||||||
|
module_name));
|
||||||
|
format!("var wasm;")
|
||||||
|
} else {
|
||||||
|
format!("import * as wasm from './{}_bg';", module_name)
|
||||||
|
};
|
||||||
|
|
||||||
format!("
|
format!("
|
||||||
/* tslint:disable */
|
/* tslint:disable */
|
||||||
{import_wasm}
|
{import_wasm}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user