diff --git a/lib/api/src/js/externals/function.rs b/lib/api/src/js/externals/function.rs index 7855b4e27b8..0d0bc0278a3 100644 --- a/lib/api/src/js/externals/function.rs +++ b/lib/api/src/js/externals/function.rs @@ -1000,6 +1000,14 @@ mod inner { } } + /// C-compatible `Result` with stable layout, + /// equivalent to `std::result::Result` + #[repr(C, u8)] + pub enum HostFunctionResult { + Ok(O), + Err(E), + } + macro_rules! impl_host_function { ( [$c_struct_representation:ident] $c_struct_name:ident, @@ -1116,7 +1124,8 @@ mod inner { /// This is a function that wraps the real host /// function. Its address will be used inside the /// runtime. - extern fn func_wrapper<$( $x, )* Rets, RetsAsResult, Func>( _: usize, $( $x: $x::Native, )* ) -> Rets::CStruct + extern fn func_wrapper<$( $x, )* Rets, RetsAsResult, Func>( _: usize, $( $x: $x::Native, )* ) + -> HostFunctionResult where $( $x: FromToNativeWasmType, )* Rets: WasmTypeList, @@ -1128,8 +1137,8 @@ mod inner { func( $( FromToNativeWasmType::from_native($x) ),* ).into_result() })); match result { - Ok(Ok(result)) => return result.into_c_struct(), - Ok(Err(trap)) => RuntimeError::raise(Box::new(trap)), + Ok(Ok(result)) => HostFunctionResult::Ok(result.into_c_struct()), + Ok(Err(trap)) => HostFunctionResult::Err(trap), _ => unimplemented!(), // Ok(Err(trap)) => unsafe { raise_user_trap(Box::new(trap)) }, // Err(panic) => unsafe { resume_panic(panic) }, @@ -1159,7 +1168,8 @@ mod inner { /// This is a function that wraps the real host /// function. Its address will be used inside the /// runtime. - extern fn func_wrapper<$( $x, )* Rets, RetsAsResult, Env, Func>( ptr: usize, $( $x: $x::Native, )* ) -> Rets::CStruct + extern fn func_wrapper<$( $x, )* Rets, RetsAsResult, Env, Func>( ptr: usize, $( $x: $x::Native, )* ) + -> HostFunctionResult where $( $x: FromToNativeWasmType, )* Rets: WasmTypeList, @@ -1174,8 +1184,8 @@ mod inner { func(env, $( FromToNativeWasmType::from_native($x) ),* ).into_result() })); match result { - Ok(Ok(result)) => return result.into_c_struct(), - Ok(Err(trap)) => RuntimeError::raise(Box::new(trap)), + Ok(Ok(result)) => HostFunctionResult::Ok(result.into_c_struct()), + Ok(Err(trap)) => HostFunctionResult::Err(trap), _ => unimplemented!(), // Ok(Err(trap)) => unsafe { raise_user_trap(Box::new(trap)) }, // Err(panic) => unsafe { resume_panic(panic) }, diff --git a/lib/api/src/js/trap.rs b/lib/api/src/js/trap.rs index 15211c7f750..6a46d442b35 100644 --- a/lib/api/src/js/trap.rs +++ b/lib/api/src/js/trap.rs @@ -62,14 +62,6 @@ impl RuntimeError { } } - /// Raises a custom user Error - #[deprecated(since = "2.1.1", note = "return a Result from host functions instead")] - pub fn raise(error: Box) -> ! { - let error = Self::user(error); - let js_error: JsValue = error.into(); - wasm_bindgen::throw_val(js_error) - } - /// Creates a custom user Error. /// /// This error object can be passed through Wasm frames and later retrieved diff --git a/lib/compiler/src/engine/trap/error.rs b/lib/compiler/src/engine/trap/error.rs index e2c26c031be..76ae212fb41 100644 --- a/lib/compiler/src/engine/trap/error.rs +++ b/lib/compiler/src/engine/trap/error.rs @@ -3,7 +3,7 @@ use backtrace::Backtrace; use std::error::Error; use std::fmt; use std::sync::Arc; -use wasmer_vm::{raise_user_trap, Trap, TrapCode}; +use wasmer_vm::{Trap, TrapCode}; /// A struct representing an aborted instruction execution, with a message /// indicating the cause. @@ -106,12 +106,6 @@ impl RuntimeError { } } - /// Raises a custom user Error - #[deprecated(since = "2.1.1", note = "return a Result from host functions instead")] - pub fn raise(error: Box) -> ! { - unsafe { raise_user_trap(error) } - } - /// Creates a custom user Error. /// /// This error object can be passed through Wasm frames and later retrieved