wasm_interp: make test_op_example generate a valid wasm file and dump it

This commit is contained in:
Brian Carroll 2022-11-28 08:58:09 +00:00
parent 6e8904baa8
commit 2e0dd18b82
No known key found for this signature in database
GPG Key ID: 5C7B2EC4101703C0

View File

@ -1,6 +1,8 @@
use crate::{Action, ExecutionState};
use bumpalo::{collections::Vec, Bump};
use roc_wasm_module::{opcodes::OpCode, SerialBuffer, Value, WasmModule};
use roc_wasm_module::{
opcodes::OpCode, Export, ExportType, SerialBuffer, Signature, Value, ValueType, WasmModule,
};
pub fn default_state(arena: &Bump) -> ExecutionState {
let pages = 1;
@ -40,23 +42,40 @@ where
{
let buf = &mut module.code.bytes;
let func_len_index = buf.encode_padded_u32(0);
let start = buf.len();
buf.push(0); // no locals
for arg in args {
const_value(buf, arg);
}
buf.push(op as u8);
buf.push(OpCode::END as u8); // end function
buf.overwrite_padded_u32(func_len_index, (buf.len() - start) as u32);
module.code.function_count = 1;
module.code.function_offsets.push(0);
module.add_function_signature(Signature {
param_types: Vec::new_in(&arena),
ret_type: Some(ValueType::from(expected)),
});
module.export.append(Export {
name: "test",
ty: ExportType::Func,
index: 0,
});
}
let mut state = default_state(&arena);
state.call_stack.push_frame(
0,
0,
&[],
&mut state.value_stack,
&module.code.bytes,
&mut state.program_counter,
);
// Dump the generated module to a file (this is mainly for debugging the test itself)
if std::env::var("DEBUG_WASM_INTERP_TEST").is_ok() {
let mut outfile_buf = Vec::new_in(&arena);
module.serialize(&mut outfile_buf);
let filename = format!("/tmp/roc/{:?}.wasm", op);
std::fs::write(&filename, outfile_buf).unwrap();
println!("\nWrote to {}\n", &filename);
}
let mut state = ExecutionState::for_module(&arena, &module, "test", true, []).unwrap();
while let Action::Continue = state.execute_next_instruction(&module) {}