wasm_interp: dump a stack trace on error

This commit is contained in:
Brian Carroll 2022-12-04 23:26:45 +00:00
parent 5bdd1b5628
commit 6d43763ab7
No known key found for this signature in database
GPG key ID: 5C7B2EC4101703C0
4 changed files with 194 additions and 20 deletions

View file

@ -7,7 +7,7 @@ pub mod wasi;
// Main external interface
pub use instance::Instance;
use roc_wasm_module::{Value, ValueType};
use roc_wasm_module::{Value, ValueType, WasmModule};
use value_stack::ValueStack;
use wasi::WasiDispatcher;
@ -74,3 +74,18 @@ impl Error {
Error::ValueStackType(expected, ty)
}
}
// Determine which function the program counter is in
pub(crate) fn pc_to_fn_index(program_counter: usize, module: &WasmModule<'_>) -> usize {
if module.code.function_offsets.is_empty() {
0
} else {
let next_code_section_index = module
.code
.function_offsets
.iter()
.position(|o| *o as usize > program_counter)
.unwrap_or(module.code.function_offsets.len());
module.import.imports.len() + next_code_section_index - 1
}
}