Update how exits are modeled in the C API (#5215)
Previously extracting an exit code was only possibly on a `wasm_trap_t` which will never successfully have an exit code on it, so the exit code extractor is moved over to `wasmtime_error_t`. Additionally extracting a wasm trace from a `wasmtime_error_t` is added since traces happen on both traps and errors now.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::wasm_name_t;
|
||||
use crate::{wasm_frame_vec_t, wasm_name_t};
|
||||
use anyhow::{anyhow, Error, Result};
|
||||
|
||||
#[repr(C)]
|
||||
@@ -37,3 +37,25 @@ pub(crate) fn bad_utf8() -> Option<Box<wasmtime_error_t>> {
|
||||
pub extern "C" fn wasmtime_error_message(error: &wasmtime_error_t, message: &mut wasm_name_t) {
|
||||
message.set_buffer(format!("{:?}", error.error).into_bytes());
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_error_exit_status(raw: &wasmtime_error_t, status: &mut i32) -> bool {
|
||||
#[cfg(feature = "wasi")]
|
||||
if let Some(exit) = raw.error.downcast_ref::<wasmtime_wasi::I32Exit>() {
|
||||
*status = exit.0;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Squash unused warnings in wasi-disabled builds.
|
||||
drop((raw, status));
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_error_wasm_trace<'a>(
|
||||
raw: &'a wasmtime_error_t,
|
||||
out: &mut wasm_frame_vec_t<'a>,
|
||||
) {
|
||||
crate::trap::error_trace(&raw.error, out)
|
||||
}
|
||||
|
||||
@@ -94,7 +94,11 @@ pub extern "C" fn wasm_trap_origin(raw: &wasm_trap_t) -> Option<Box<wasm_frame_t
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_trap_trace<'a>(raw: &'a wasm_trap_t, out: &mut wasm_frame_vec_t<'a>) {
|
||||
let trace = match raw.error.downcast_ref::<WasmBacktrace>() {
|
||||
error_trace(&raw.error, out)
|
||||
}
|
||||
|
||||
pub(crate) fn error_trace<'a>(error: &'a Error, out: &mut wasm_frame_vec_t<'a>) {
|
||||
let trace = match error.downcast_ref::<WasmBacktrace>() {
|
||||
Some(trap) => trap,
|
||||
None => return out.set_buffer(Vec::new()),
|
||||
};
|
||||
@@ -134,20 +138,6 @@ pub extern "C" fn wasmtime_trap_code(raw: &wasm_trap_t, code: &mut i32) -> bool
|
||||
true
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_trap_exit_status(raw: &wasm_trap_t, status: &mut i32) -> bool {
|
||||
#[cfg(feature = "wasi")]
|
||||
if let Some(exit) = raw.error.downcast_ref::<wasmtime_wasi::I32Exit>() {
|
||||
*status = exit.0;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Squash unused warnings in wasi-disabled builds.
|
||||
drop((raw, status));
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_frame_func_index(frame: &wasm_frame_t<'_>) -> u32 {
|
||||
frame.trace.frames()[frame.idx].func_index()
|
||||
|
||||
Reference in New Issue
Block a user