Files
wasmtime/crates/c-api/src/error.rs
Alex Crichton b07b0676a3 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.
2022-11-07 11:35:49 -06:00

62 lines
1.5 KiB
Rust

use crate::{wasm_frame_vec_t, wasm_name_t};
use anyhow::{anyhow, Error, Result};
#[repr(C)]
pub struct wasmtime_error_t {
error: Error,
}
wasmtime_c_api_macros::declare_own!(wasmtime_error_t);
impl From<Error> for wasmtime_error_t {
fn from(error: Error) -> wasmtime_error_t {
wasmtime_error_t { error }
}
}
pub(crate) fn handle_result<T>(
result: Result<T>,
ok: impl FnOnce(T),
) -> Option<Box<wasmtime_error_t>> {
match result {
Ok(value) => {
ok(value);
None
}
Err(error) => Some(Box::new(wasmtime_error_t { error })),
}
}
pub(crate) fn bad_utf8() -> Option<Box<wasmtime_error_t>> {
Some(Box::new(wasmtime_error_t {
error: anyhow!("input was not valid utf-8"),
}))
}
#[no_mangle]
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)
}