Skip to content

Commit 97df65f

Browse files
committed
Separate start and initialization
1 parent eb15d26 commit 97df65f

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ pub struct Context<'a> {
6262

6363
/// A flag to track if the stack pointer setter shim has been injected.
6464
stack_pointer_shim_injected: bool,
65+
66+
start_found: bool,
6567
}
6668

6769
#[derive(Default)]
@@ -93,6 +95,7 @@ impl<'a> Context<'a> {
9395
config: &'a Bindgen,
9496
wit: &'a NonstandardWitSection,
9597
aux: &'a WasmBindgenAux,
98+
start_found: bool,
9699
) -> Result<Context<'a>, Error> {
97100
Ok(Context {
98101
globals: String::new(),
@@ -113,6 +116,7 @@ impl<'a> Context<'a> {
113116
memories: Default::default(),
114117
table_indices: Default::default(),
115118
stack_pointer_shim_injected: false,
119+
start_found,
116120
})
117121
}
118122

@@ -854,9 +858,8 @@ impl<'a> Context<'a> {
854858
wasm = instance.exports;
855859
init.__wbindgen_wasm_module = module;
856860
{init_memviews}
857-
if (start == true) {{
858-
{start}
859-
}}
861+
{start}
862+
{main}
860863
return wasm;
861864
}}
862865
@@ -906,6 +909,11 @@ impl<'a> Context<'a> {
906909
} else {
907910
""
908911
},
912+
main = if needs_manual_start && self.start_found {
913+
"if (start == true) { wasm.__wbindgen_main(); }"
914+
} else {
915+
""
916+
},
909917
imports_init = imports_init,
910918
);
911919

crates/cli-support/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ impl Bindgen {
359359
// auxiliary section for all sorts of miscellaneous information and
360360
// features #[wasm_bindgen] supports that aren't covered by wasm
361361
// interface types.
362-
wit::process(
362+
let wit::ProcessResult { start_found, .. } = wit::process(
363363
&mut module,
364364
self.externref,
365365
self.wasm_interface_types,
@@ -441,7 +441,7 @@ impl Bindgen {
441441
.customs
442442
.delete_typed::<wit::NonstandardWitSection>()
443443
.unwrap();
444-
let mut cx = js::Context::new(&mut module, self, &adapters, &aux)?;
444+
let mut cx = js::Context::new(&mut module, self, &adapters, &aux, start_found)?;
445445
cx.generate()?;
446446
let (js, ts, start) = cx.finalize(stem)?;
447447
Generated::Js(JsGenerated {

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,18 @@ struct InstructionBuilder<'a, 'b> {
4343
return_position: bool,
4444
}
4545

46+
pub struct ProcessResult {
47+
pub adapters: NonstandardWitSectionId,
48+
pub aux: WasmBindgenAuxId,
49+
pub start_found: bool,
50+
}
51+
4652
pub fn process(
4753
module: &mut Module,
4854
externref_enabled: bool,
4955
wasm_interface_types: bool,
5056
support_start: bool,
51-
) -> Result<(NonstandardWitSectionId, WasmBindgenAuxId), Error> {
57+
) -> Result<ProcessResult, Error> {
5258
let mut storage = Vec::new();
5359
let programs = extract_programs(module, &mut storage)?;
5460

@@ -88,7 +94,11 @@ pub fn process(
8894

8995
let adapters = cx.module.customs.add(cx.adapters);
9096
let aux = cx.module.customs.add(cx.aux);
91-
Ok((adapters, aux))
97+
Ok(ProcessResult {
98+
adapters,
99+
aux,
100+
start_found: cx.start_found,
101+
})
92102
}
93103

94104
impl<'a> Context<'a> {
@@ -476,22 +486,7 @@ impl<'a> Context<'a> {
476486
return Ok(());
477487
}
478488

479-
let prev_start = match self.module.start {
480-
Some(f) => f,
481-
None => {
482-
self.module.start = Some(id);
483-
return Ok(());
484-
}
485-
};
486-
487-
// Note that we call the previous start function, if any, first. This is
488-
// because the start function currently only shows up when it's injected
489-
// through thread/externref transforms. These injected start functions
490-
// need to happen before user code, so we always schedule them first.
491-
let mut builder = walrus::FunctionBuilder::new(&mut self.module.types, &[], &[]);
492-
builder.func_body().call(prev_start).call(id);
493-
let new_start = builder.finish(Vec::new(), &mut self.module.funcs);
494-
self.module.start = Some(new_start);
489+
self.module.exports.add("__wbindgen_main", id);
495490
Ok(())
496491
}
497492

0 commit comments

Comments
 (0)