Skip to content

Commit 17cfd32

Browse files
committed
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
1 parent 60ac091 commit 17cfd32

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

crates/c-api/include/wasmtime.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ WASM_API_EXTERN own wasmtime_interrupt_handle_t *wasmtime_interrupt_handle_new(w
167167

168168
WASM_API_EXTERN void wasmtime_interrupt_handle_interrupt(wasmtime_interrupt_handle_t *handle);
169169

170+
///////////////////////////////////////////////////////////////////////////////
171+
//
172+
// Extensions to `wasm_trap_t`
173+
174+
// Returns `true` if the trap is a WASI "exit" trap and has a return status. If
175+
// `true` is returned then the exit status is returned through the `status`
176+
// pointer. If `false` is returned then this is not a wasi exit trap.
177+
WASM_API_EXTERN bool *wasmtime_trap_exit_status(const wasm_trap_t*, int *status);
178+
170179
///////////////////////////////////////////////////////////////////////////////
171180
//
172181
// Extensions to `wasm_frame_t`

crates/c-api/src/trap.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ pub extern "C" fn wasm_trap_trace(raw: &wasm_trap_t, out: &mut wasm_frame_vec_t)
9191
out.set_buffer(vec);
9292
}
9393

94+
#[no_mangle]
95+
pub extern "C" fn wasmtime_trap_exit_status(raw: &wasm_trap_t, status: &mut i32) -> bool {
96+
let trap = raw.trap.borrow();
97+
match trap.i32_exit_status() {
98+
Some(i) => {
99+
*status = i;
100+
true
101+
}
102+
None => false,
103+
}
104+
}
105+
94106
#[no_mangle]
95107
pub extern "C" fn wasm_frame_func_index(frame: &wasm_frame_t) -> u32 {
96108
frame.trap.borrow().trace()[frame.idx].func_index()

0 commit comments

Comments
 (0)