mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-14 07:29:02 +03:00
wasm_interp: make test_op_example generate a valid wasm file and dump it
This commit is contained in:
parent
6e8904baa8
commit
2e0dd18b82
@ -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) {}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user