diff --git a/compiler/gen_wasm/src/lib.rs b/compiler/gen_wasm/src/lib.rs index 2e40ca9ea3..a8b19213dd 100644 --- a/compiler/gen_wasm/src/lib.rs +++ b/compiler/gen_wasm/src/lib.rs @@ -37,7 +37,7 @@ pub fn build_module<'a>( env: &'a Env, procedures: MutMap<(Symbol, ProcLayout<'a>), Proc<'a>>, ) -> Result, String> { - let mut wasm_module = build_module_help(env, procedures)?; + let (mut wasm_module, _) = build_module_help(env, procedures)?; let mut buffer = std::vec::Vec::with_capacity(4096); wasm_module.serialize_mut(&mut buffer); Ok(buffer) @@ -46,16 +46,17 @@ pub fn build_module<'a>( pub fn build_module_help<'a>( env: &'a Env, procedures: MutMap<(Symbol, ProcLayout<'a>), Proc<'a>>, -) -> Result, String> { +) -> Result<(WasmModule<'a>, u32), String> { let mut layout_ids = LayoutIds::default(); let mut generated_procs = Vec::with_capacity_in(procedures.len(), env.arena); let mut generated_symbols = Vec::with_capacity_in(procedures.len(), env.arena); let mut linker_symbols = Vec::with_capacity_in(procedures.len() * 2, env.arena); let mut exports = Vec::with_capacity_in(4, env.arena); + let mut main_fn_index = None; // Collect the symbols & names for the procedures, // and filter out procs we're going to inline - let mut fn_index = 0; + let mut fn_index: u32 = 0; for ((sym, layout), proc) in procedures.into_iter() { if LowLevel::from_inlined_wrapper(sym).is_some() { continue; @@ -68,14 +69,15 @@ pub fn build_module_help<'a>( .to_symbol_string(sym, &env.interns); if env.exposed_to_host.contains(&sym) { + main_fn_index = Some(fn_index); exports.push(Export { name: fn_name.clone(), ty: ExportType::Func, - index: fn_index as u32, + index: fn_index, }); } - let linker_sym = SymInfo::for_function(fn_index as u32, fn_name); + let linker_sym = SymInfo::for_function(fn_index, fn_name); linker_symbols.push(linker_sym); fn_index += 1; @@ -100,7 +102,7 @@ pub fn build_module_help<'a>( let symbol_table = LinkingSubSection::SymbolTable(linker_symbols); module.linking.subsections.push(symbol_table); - Ok(module) + Ok((module, main_fn_index.unwrap())) } pub struct CopyMemoryConfig { diff --git a/compiler/test_gen/src/helpers/wasm.rs b/compiler/test_gen/src/helpers/wasm.rs index 649aac8421..762fd4fc7a 100644 --- a/compiler/test_gen/src/helpers/wasm.rs +++ b/compiler/test_gen/src/helpers/wasm.rs @@ -98,11 +98,6 @@ pub fn helper_wasm<'a, T: Wasm32TestResult>( // } debug_assert_eq!(exposed_to_host.len(), 1); - let main_fn_symbol = loaded.entry_point.symbol; - let main_fn_index = procedures - .keys() - .position(|(s, _)| *s == main_fn_symbol) - .unwrap(); let exposed_to_host = exposed_to_host.keys().copied().collect::>(); @@ -112,14 +107,10 @@ pub fn helper_wasm<'a, T: Wasm32TestResult>( exposed_to_host, }; - let mut wasm_module = roc_gen_wasm::build_module_help(&env, procedures).unwrap(); + let (mut wasm_module, main_fn_index) = + roc_gen_wasm::build_module_help(&env, procedures).unwrap(); - T::insert_test_wrapper( - arena, - &mut wasm_module, - TEST_WRAPPER_NAME, - main_fn_index as u32, - ); + T::insert_test_wrapper(arena, &mut wasm_module, TEST_WRAPPER_NAME, main_fn_index); // We can either generate the test platform or write an external source file, whatever works generate_test_platform(&mut wasm_module, arena); @@ -164,7 +155,7 @@ pub fn helper_wasm<'a, T: Wasm32TestResult>( // write the module to a file so the linker can access it std::fs::write(&app_o_file, &module_bytes).unwrap(); - std::process::Command::new("zig") + let _linker_output = std::process::Command::new("zig") .args(&[ "wasm-ld", // input files @@ -190,6 +181,8 @@ pub fn helper_wasm<'a, T: Wasm32TestResult>( .output() .unwrap(); + // dbg!(_linker_output); + Module::from_file(&store, &final_wasm_file).unwrap() };