Skip to content

Commit 8854936

Browse files
committed
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
1 parent 9801632 commit 8854936

File tree

1 file changed

+20
-28
lines changed
  • crates/cli-support/src

1 file changed

+20
-28
lines changed

crates/cli-support/src/js.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -267,44 +267,36 @@ impl<'a> Context<'a> {
267267

268268
self.rewrite_imports(module_name);
269269

270-
let import_wasm = if self.config.nodejs {
271-
self.footer.push_str(&format!("wasm = require('./{}_bg');",
272-
module_name));
273-
format!("var wasm;")
274-
} else if self.config.no_modules {
275-
format!("
276-
window.wasm_bindgen.init = function(__wasm_path) {{
277-
return fetch(__wasm_path)
278-
.then(response => response.arrayBuffer())
279-
.then(buffer => WebAssembly.instantiate(buffer, {{ './{module}': __exports }}))
280-
.then(({{instance}}) => {{
281-
wasm = instance.exports;
282-
return;
283-
}})
284-
.catch(error => {{
285-
console.log('Error loading wasm module `{module}`:', error);
286-
throw error;
287-
}});
288-
}};
289-
", module = module_name)
290-
} else {
291-
format!("import * as wasm from './{}_bg';", module_name)
292-
};
293-
294270
let js = if self.config.no_modules {
295271
format!("
296272
(function() {{
297-
let wasm;
273+
var wasm;
298274
const __exports = {{}};
299275
{globals}
300-
window.wasm_bindgen = Object.assign({{}}, __exports);
301-
{import_wasm}
276+
function init(wasm_path) {{
277+
return fetch(wasm_path)
278+
.then(response => response.arrayBuffer())
279+
.then(buffer => WebAssembly.instantiate(buffer, {{ './{module}': __exports }}))
280+
.then(({{instance}}) => {{
281+
wasm = init.wasm = instance.exports;
282+
return;
283+
}});
284+
}};
285+
window.wasm_bindgen = Object.assign(init, __exports);
302286
}})();
303287
",
304288
globals = self.globals,
305-
import_wasm = import_wasm,
289+
module = module_name,
306290
)
307291
} else {
292+
let import_wasm = if self.config.nodejs {
293+
self.footer.push_str(&format!("wasm = require('./{}_bg');",
294+
module_name));
295+
format!("var wasm;")
296+
} else {
297+
format!("import * as wasm from './{}_bg';", module_name)
298+
};
299+
308300
format!("
309301
/* tslint:disable */
310302
{import_wasm}

0 commit comments

Comments
 (0)