This is enough to get an `externref -> externref` identity function passing. However, `externref`s that are dropped by compiled Wasm code are (safely) leaked. Follow up work will leverage cranelift's stack maps to resolve this issue.
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::{Store, 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, store: &Store) -> Box<wasm_trap_t> {
|
|
Box::new(wasm_trap_t::new(store, Trap::from(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());
|
|
}
|