Skip to content

Commit 4556d91

Browse files
committed
Add initWithoutStart to the JS shim
1 parent f75a3f8 commit 4556d91

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

crates/cli-support/src/js/mod.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ impl<'a> Context<'a> {
468468
OutputMode::Web => {
469469
self.imports_post.push_str("let wasm;\n");
470470
init = self.gen_init(needs_manual_start, Some(&mut imports))?;
471-
footer.push_str("export { initSync }\n");
471+
footer.push_str("export { initSync, initWithoutStart }\n");
472472
footer.push_str("export default init;");
473473
}
474474
}
@@ -607,6 +607,7 @@ impl<'a> Context<'a> {
607607
// Also in (at least) the NoModules, the `init()` method is renamed to `wasm_bindgen()`.
608608
let setup_function_declaration;
609609
let mut sync_init_function = String::new();
610+
let mut without_start_init_function = String::new();
610611
let declare_or_export;
611612
if self.config.mode.no_modules() {
612613
declare_or_export = "declare";
@@ -632,6 +633,23 @@ impl<'a> Context<'a> {
632633
memory_param = memory_param
633634
));
634635

636+
without_start_init_function.push_str(&format!(
637+
"\
638+
/**\n\
639+
* If `module_or_path` is {{RequestInfo}} or {{URL}}, makes a request and\n\
640+
* for everything else, calls `WebAssembly.instantiate` directly.\n\
641+
*\n\
642+
* @param {{InitInput | Promise<InitInput>}} module_or_path\n\
643+
{}\
644+
*\n\
645+
* @returns {{Promise<InitOutput>}}\n\
646+
*/\n\
647+
export function initWithoutStart \
648+
(module_or_path{}: InitInput | Promise<InitInput>{}): Promise<InitOutput>;\n\n\
649+
",
650+
memory_doc, arg_optional, memory_param,
651+
));
652+
635653
setup_function_declaration = "export default function init";
636654
}
637655
Ok(format!(
@@ -644,7 +662,8 @@ impl<'a> Context<'a> {
644662
{sync_init_function}\
645663
/**\n\
646664
* If `module_or_path` is {{RequestInfo}} or {{URL}}, makes a request and\n\
647-
* for everything else, calls `WebAssembly.instantiate` directly.\n\
665+
* for everything else, calls `WebAssembly.instantiate` directly and runs\n\
666+
* the start function.\n\
648667
*\n\
649668
* @param {{InitInput | Promise<InitInput>}} module_or_path\n\
650669
{}\
@@ -831,11 +850,13 @@ impl<'a> Context<'a> {
831850
{init_memory}
832851
}}
833852
834-
function finalizeInit(instance, module) {{
853+
function finalizeInit(instance, module, start) {{
835854
wasm = instance.exports;
836855
init.__wbindgen_wasm_module = module;
837856
{init_memviews}
838-
{start}
857+
if (start == true) {{
858+
{start}
859+
}}
839860
return wasm;
840861
}}
841862
@@ -850,10 +871,10 @@ impl<'a> Context<'a> {
850871
851872
const instance = new WebAssembly.Instance(module, imports);
852873
853-
return finalizeInit(instance, module);
874+
return finalizeInit(instance, module, true);
854875
}}
855876
856-
async function init(input{init_memory_arg}) {{
877+
async function initInternal(input{init_memory_arg}, start) {{
857878
{default_module_path}
858879
const imports = getImports();
859880
@@ -865,7 +886,15 @@ impl<'a> Context<'a> {
865886
866887
const {{ instance, module }} = await load(await input, imports);
867888
868-
return finalizeInit(instance, module);
889+
return finalizeInit(instance, module, start);
890+
}}
891+
892+
async function initWithoutStart(input{init_memory_arg}) {{
893+
return initInternal(input{init_memory_arg}, false);
894+
}}
895+
896+
async function init(input{init_memory_arg}) {{
897+
return initInternal(input{init_memory_arg}, true);
869898
}}
870899
",
871900
init_memory_arg = init_memory_arg,

crates/cli/tests/wasm-bindgen/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ fn default_module_path_target_web() {
239239
let contents = fs::read_to_string(out_dir.join("default_module_path_target_web.js")).unwrap();
240240
assert!(contents.contains(
241241
"\
242-
async function init(input) {
242+
async function initInternal(input, start) {
243243
if (typeof input === 'undefined') {
244244
input = new URL('default_module_path_target_web_bg.wasm', import.meta.url);
245245
}",
@@ -260,7 +260,7 @@ fn default_module_path_target_no_modules() {
260260
fs::read_to_string(out_dir.join("default_module_path_target_no_modules.js")).unwrap();
261261
assert!(contents.contains(
262262
"\
263-
async function init(input) {
263+
async function initInternal(input, start) {
264264
if (typeof input === 'undefined') {
265265
let src;
266266
if (typeof document === 'undefined') {
@@ -287,7 +287,7 @@ fn omit_default_module_path_target_web() {
287287
fs::read_to_string(out_dir.join("omit_default_module_path_target_web.js")).unwrap();
288288
assert!(contents.contains(
289289
"\
290-
async function init(input) {
290+
async function initInternal(input, start) {
291291
292292
const imports = getImports();",
293293
));
@@ -307,7 +307,7 @@ fn omit_default_module_path_target_no_modules() {
307307
fs::read_to_string(out_dir.join("omit_default_module_path_target_no_modules.js")).unwrap();
308308
assert!(contents.contains(
309309
"\
310-
async function init(input) {
310+
async function initInternal(input, start) {
311311
312312
const imports = getImports();",
313313
));

0 commit comments

Comments
 (0)