From 17cfd32be76e881d44056b5862b91dd2375877bf Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 23 Jun 2020 08:04:27 -0700 Subject: [PATCH] Bind `Trap::i32_exit_status` in C API This allows embedders to take a different action when a WASI program exits depending on whether it exited with an exit status or exited with a trap. cc bytecodealliance/wasmtime-py#30 --- crates/c-api/include/wasmtime.h | 9 +++++++++ crates/c-api/src/trap.rs | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/crates/c-api/include/wasmtime.h b/crates/c-api/include/wasmtime.h index 052a114fd724..460e1971889c 100644 --- a/crates/c-api/include/wasmtime.h +++ b/crates/c-api/include/wasmtime.h @@ -167,6 +167,15 @@ WASM_API_EXTERN own wasmtime_interrupt_handle_t *wasmtime_interrupt_handle_new(w WASM_API_EXTERN void wasmtime_interrupt_handle_interrupt(wasmtime_interrupt_handle_t *handle); +/////////////////////////////////////////////////////////////////////////////// +// +// Extensions to `wasm_trap_t` + +// Returns `true` if the trap is a WASI "exit" trap and has a return status. If +// `true` is returned then the exit status is returned through the `status` +// pointer. If `false` is returned then this is not a wasi exit trap. +WASM_API_EXTERN bool *wasmtime_trap_exit_status(const wasm_trap_t*, int *status); + /////////////////////////////////////////////////////////////////////////////// // // Extensions to `wasm_frame_t` diff --git a/crates/c-api/src/trap.rs b/crates/c-api/src/trap.rs index 633577573d6b..9c43d53e9f25 100644 --- a/crates/c-api/src/trap.rs +++ b/crates/c-api/src/trap.rs @@ -91,6 +91,18 @@ 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_exit_status(raw: &wasm_trap_t, status: &mut i32) -> bool { + let trap = raw.trap.borrow(); + match trap.i32_exit_status() { + Some(i) => { + *status = i; + true + } + None => false, + } +} + #[no_mangle] pub extern "C" fn wasm_frame_func_index(frame: &wasm_frame_t) -> u32 { frame.trap.borrow().trace()[frame.idx].func_index()