c-api: add wasmtime_trap_code (#3086)

Eventually this should be added to the wasmtime-go binding, addressing
https://github.com/bytecodealliance/wasmtime-go/issues/63.

Added a snippet to examples/interrupt.c to verify that this works as
expected in manual testing.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
This commit is contained in:
Stephan Renatus
2021-07-15 17:31:03 +02:00
committed by GitHub
parent 2452a4cd74
commit f3b80ece5f
5 changed files with 84 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
use crate::{wasm_frame_vec_t, wasm_instance_t, wasm_name_t, wasm_store_t};
use once_cell::unsync::OnceCell;
use wasmtime::Trap;
use wasmtime::{Trap, TrapCode};
#[repr(C)]
#[derive(Clone)]
@@ -91,6 +91,30 @@ pub extern "C" fn wasm_trap_trace(raw: &wasm_trap_t, out: &mut wasm_frame_vec_t)
out.set_buffer(vec);
}
#[no_mangle]
pub extern "C" fn wasmtime_trap_code(raw: &wasm_trap_t, code: &mut i32) -> bool {
match raw.trap.trap_code() {
Some(c) => {
*code = match c {
TrapCode::StackOverflow => 0,
TrapCode::MemoryOutOfBounds => 1,
TrapCode::HeapMisaligned => 2,
TrapCode::TableOutOfBounds => 3,
TrapCode::IndirectCallToNull => 4,
TrapCode::BadSignature => 5,
TrapCode::IntegerOverflow => 6,
TrapCode::IntegerDivisionByZero => 7,
TrapCode::BadConversionToInteger => 8,
TrapCode::UnreachableCodeReached => 9,
TrapCode::Interrupt => 10,
_ => unreachable!(),
};
true
}
None => false,
}
}
#[no_mangle]
pub extern "C" fn wasmtime_trap_exit_status(raw: &wasm_trap_t, status: &mut i32) -> bool {
match raw.trap.i32_exit_status() {