* Add Wasmtime-specific C API functions to return errors This commit adds new `wasmtime_*` symbols to the C API, many of which mirror the existing counterparts in the `wasm.h` header. These APIs are enhanced in a number of respects: * Detailed error information is now available through a `wasmtime_error_t`. Currently this only exposes one function which is to extract a string version of the error. * There is a distinction now between traps and errors during instantiation and function calling. Traps only happen if wasm traps, and errors can happen for things like runtime type errors when interacting with the API. * APIs have improved safety with respect to embedders where the lengths of arrays are now taken as explicit parameters rather than assumed from other parameters. * Handle trap updates * Update C examples * Fix memory.c compile on MSVC * Update test assertions * Refactor C slightly * Bare-bones .NET update * Remove bogus nul handling
47 lines
1.1 KiB
Rust
47 lines
1.1 KiB
Rust
use crate::{wasm_name_t, wasm_trap_t};
|
|
use anyhow::{anyhow, Error, Result};
|
|
use wasmtime::Trap;
|
|
|
|
#[repr(C)]
|
|
pub struct wasmtime_error_t {
|
|
error: Error,
|
|
}
|
|
|
|
wasmtime_c_api_macros::declare_own!(wasmtime_error_t);
|
|
|
|
impl wasmtime_error_t {
|
|
pub(crate) fn to_trap(&self) -> Box<wasm_trap_t> {
|
|
Box::new(wasm_trap_t::new(Trap::new(format!("{:?}", self.error))))
|
|
}
|
|
}
|
|
|
|
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());
|
|
}
|