Fix test_wrapper linking bug

This commit is contained in:
Brian Carroll 2021-11-17 16:45:38 +00:00
parent 751bfd842c
commit 1134b6f45c
2 changed files with 14 additions and 19 deletions

View file

@ -37,7 +37,7 @@ pub fn build_module<'a>(
env: &'a Env, env: &'a Env,
procedures: MutMap<(Symbol, ProcLayout<'a>), Proc<'a>>, procedures: MutMap<(Symbol, ProcLayout<'a>), Proc<'a>>,
) -> Result<std::vec::Vec<u8>, String> { ) -> Result<std::vec::Vec<u8>, 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); let mut buffer = std::vec::Vec::with_capacity(4096);
wasm_module.serialize_mut(&mut buffer); wasm_module.serialize_mut(&mut buffer);
Ok(buffer) Ok(buffer)
@ -46,16 +46,17 @@ pub fn build_module<'a>(
pub fn build_module_help<'a>( pub fn build_module_help<'a>(
env: &'a Env, env: &'a Env,
procedures: MutMap<(Symbol, ProcLayout<'a>), Proc<'a>>, procedures: MutMap<(Symbol, ProcLayout<'a>), Proc<'a>>,
) -> Result<WasmModule<'a>, String> { ) -> Result<(WasmModule<'a>, u32), String> {
let mut layout_ids = LayoutIds::default(); let mut layout_ids = LayoutIds::default();
let mut generated_procs = Vec::with_capacity_in(procedures.len(), env.arena); 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 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 linker_symbols = Vec::with_capacity_in(procedures.len() * 2, env.arena);
let mut exports = Vec::with_capacity_in(4, 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, // Collect the symbols & names for the procedures,
// and filter out procs we're going to inline // 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() { for ((sym, layout), proc) in procedures.into_iter() {
if LowLevel::from_inlined_wrapper(sym).is_some() { if LowLevel::from_inlined_wrapper(sym).is_some() {
continue; continue;
@ -68,14 +69,15 @@ pub fn build_module_help<'a>(
.to_symbol_string(sym, &env.interns); .to_symbol_string(sym, &env.interns);
if env.exposed_to_host.contains(&sym) { if env.exposed_to_host.contains(&sym) {
main_fn_index = Some(fn_index);
exports.push(Export { exports.push(Export {
name: fn_name.clone(), name: fn_name.clone(),
ty: ExportType::Func, 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); linker_symbols.push(linker_sym);
fn_index += 1; fn_index += 1;
@ -100,7 +102,7 @@ pub fn build_module_help<'a>(
let symbol_table = LinkingSubSection::SymbolTable(linker_symbols); let symbol_table = LinkingSubSection::SymbolTable(linker_symbols);
module.linking.subsections.push(symbol_table); module.linking.subsections.push(symbol_table);
Ok(module) Ok((module, main_fn_index.unwrap()))
} }
pub struct CopyMemoryConfig { pub struct CopyMemoryConfig {

View file

@ -98,11 +98,6 @@ pub fn helper_wasm<'a, T: Wasm32TestResult>(
// } // }
debug_assert_eq!(exposed_to_host.len(), 1); 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::<MutSet<_>>(); let exposed_to_host = exposed_to_host.keys().copied().collect::<MutSet<_>>();
@ -112,14 +107,10 @@ pub fn helper_wasm<'a, T: Wasm32TestResult>(
exposed_to_host, 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( T::insert_test_wrapper(arena, &mut wasm_module, TEST_WRAPPER_NAME, main_fn_index);
arena,
&mut wasm_module,
TEST_WRAPPER_NAME,
main_fn_index as u32,
);
// We can either generate the test platform or write an external source file, whatever works // We can either generate the test platform or write an external source file, whatever works
generate_test_platform(&mut wasm_module, arena); 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 // write the module to a file so the linker can access it
std::fs::write(&app_o_file, &module_bytes).unwrap(); std::fs::write(&app_o_file, &module_bytes).unwrap();
std::process::Command::new("zig") let _linker_output = std::process::Command::new("zig")
.args(&[ .args(&[
"wasm-ld", "wasm-ld",
// input files // input files
@ -190,6 +181,8 @@ pub fn helper_wasm<'a, T: Wasm32TestResult>(
.output() .output()
.unwrap(); .unwrap();
// dbg!(_linker_output);
Module::from_file(&store, &final_wasm_file).unwrap() Module::from_file(&store, &final_wasm_file).unwrap()
}; };