1 use crate::{wasm_frame_vec_t, wasm_name_t}; 2 use anyhow::{anyhow, Error, Result}; 3 4 #[repr(C)] 5 pub struct wasmtime_error_t { 6 error: Error, 7 } 8 9 wasmtime_c_api_macros::declare_own!(wasmtime_error_t); 10 11 impl From<Error> for wasmtime_error_t { 12 fn from(error: Error) -> wasmtime_error_t { 13 wasmtime_error_t { error } 14 } 15 } 16 17 pub(crate) fn handle_result<T>( 18 result: Result<T>, 19 ok: impl FnOnce(T), 20 ) -> Option<Box<wasmtime_error_t>> { 21 match result { 22 Ok(value) => { 23 ok(value); 24 None 25 } 26 Err(error) => Some(Box::new(wasmtime_error_t { error })), 27 } 28 } 29 30 pub(crate) fn bad_utf8() -> Option<Box<wasmtime_error_t>> { 31 Some(Box::new(wasmtime_error_t { 32 error: anyhow!("input was not valid utf-8"), 33 })) 34 } 35 36 #[no_mangle] 37 pub extern "C" fn wasmtime_error_message(error: &wasmtime_error_t, message: &mut wasm_name_t) { 38 message.set_buffer(format!("{:?}", error.error).into_bytes()); 39 } 40 41 #[no_mangle] 42 pub extern "C" fn wasmtime_error_exit_status(raw: &wasmtime_error_t, status: &mut i32) -> bool { 43 #[cfg(feature = "wasi")] 44 if let Some(exit) = raw.error.downcast_ref::<wasmtime_wasi::I32Exit>() { 45 *status = exit.0; 46 return true; 47 } 48 49 // Squash unused warnings in wasi-disabled builds. 50 drop((raw, status)); 51 52 false 53 } 54 55 #[no_mangle] 56 pub extern "C" fn wasmtime_error_wasm_trace<'a>( 57 raw: &'a wasmtime_error_t, 58 out: &mut wasm_frame_vec_t<'a>, 59 ) { 60 crate::trap::error_trace(&raw.error, out) 61 } 62