From ee2f127b8cfd8d145b8f35821a184a27e61c98f3 Mon Sep 17 00:00:00 2001 From: Xin Liu Date: Fri, 21 Jul 2023 16:13:04 +0800 Subject: [PATCH] [refactor] Update the APIs related to sync host function creation (#26) * refactor!(rust-sys): update `Function::create_sync_func` Signed-off-by: Xin Liu * refactor!(rust-sdk): update `Func`, `ImportObjectBuilder` and `PluginModuleBuilder` Signed-off-by: Xin Liu --------- Signed-off-by: Xin Liu --- .../examples/context_data_to_host_func.rs | 16 ++++++---------- crates/wasmedge-sys/src/instance/function.rs | 19 +++++++------------ src/externals/function.rs | 4 ++-- src/import.rs | 4 ++-- src/plugin.rs | 2 +- 5 files changed, 18 insertions(+), 27 deletions(-) diff --git a/crates/wasmedge-sys/examples/context_data_to_host_func.rs b/crates/wasmedge-sys/examples/context_data_to_host_func.rs index 1b466134e..936c1943c 100644 --- a/crates/wasmedge-sys/examples/context_data_to_host_func.rs +++ b/crates/wasmedge-sys/examples/context_data_to_host_func.rs @@ -1,3 +1,4 @@ +use wasmedge_macro::sys_host_function; use wasmedge_sys::{CallingFrame, Executor, FuncType, Function, WasmValue}; use wasmedge_types::{error::HostFuncError, ValType}; @@ -9,15 +10,15 @@ struct Data { _s: Vec, } +#[sys_host_function] fn real_add( _frame: CallingFrame, input: Vec, - data: *mut std::ffi::c_void, + data: &mut Data, ) -> Result, HostFuncError> { println!("Rust: Entering Rust function real_add"); - let host_data = unsafe { Box::from_raw(data as *mut T) }; - println!("host_data: {:?}", host_data); + println!("data: {data:?}"); if input.len() != 2 { return Err(HostFuncError::User(1)); @@ -43,7 +44,7 @@ fn real_add( } fn main() -> Result<(), Box> { - let data: Data = Data { + let mut data: Data = Data { _x: 12, _y: "hello".to_string(), _v: vec![1, 2, 3], @@ -55,12 +56,7 @@ fn main() -> Result<(), Box> { assert!(result.is_ok()); let func_ty = result.unwrap(); // create a host function - let result = Function::create_sync_func( - &func_ty, - Box::new(real_add::>), - Some(Box::new(data)), - 0, - ); + let result = Function::create_sync_func(&func_ty, Box::new(real_add), Some(&mut data), 0); assert!(result.is_ok()); let host_func = result.unwrap(); diff --git a/crates/wasmedge-sys/src/instance/function.rs b/crates/wasmedge-sys/src/instance/function.rs index ed033317d..975c09a02 100644 --- a/crates/wasmedge-sys/src/instance/function.rs +++ b/crates/wasmedge-sys/src/instance/function.rs @@ -380,11 +380,11 @@ impl Function { pub fn create_sync_func( ty: &FuncType, real_fn: BoxedFn, - data: Option>, + data: Option<&mut T>, cost: u64, ) -> WasmEdgeResult { let data = match data { - Some(d) => Box::into_raw(d) as *mut std::ffi::c_void, + Some(d) => d as *mut T as *mut std::os::raw::c_void, None => std::ptr::null_mut(), }; @@ -1009,22 +1009,22 @@ mod tests { _v: Vec, _s: Vec, } - let data: Data = Data { + let mut data: Data = Data { _x: 12, _y: "hello".to_string(), _v: vec![1, 2, 3], _s: vec!["macos", "linux", "windows"], }; + #[sys_host_function] fn real_add( _frame: CallingFrame, input: Vec, - data: *mut std::ffi::c_void, + data: &mut Data, ) -> Result, HostFuncError> { println!("Rust: Entering Rust function real_add"); - let host_data = unsafe { Box::from_raw(data as *mut T) }; - println!("host_data: {:?}", host_data); + println!("data: {:?}", data); if input.len() != 2 { return Err(HostFuncError::User(1)); @@ -1057,12 +1057,7 @@ mod tests { assert!(result.is_ok()); let func_ty = result.unwrap(); // create a host function - let result = Function::create_sync_func( - &func_ty, - Box::new(real_add::>), - Some(Box::new(data)), - 0, - ); + let result = Function::create_sync_func(&func_ty, Box::new(real_add), Some(&mut data), 0); assert!(result.is_ok()); let host_func = result.unwrap(); diff --git a/src/externals/function.rs b/src/externals/function.rs index 2d9e028ad..f8f4f3072 100644 --- a/src/externals/function.rs +++ b/src/externals/function.rs @@ -44,7 +44,7 @@ impl Func { + Send + Sync + 'static, - data: Option>, + data: Option<&mut T>, ) -> WasmEdgeResult { let boxed_func = Box::new(real_func); let inner = sys::Function::create_sync_func::(&ty.clone().into(), boxed_func, data, 0)?; @@ -76,7 +76,7 @@ impl Func { + Send + Sync + 'static, - data: Option>, + data: Option<&mut T>, ) -> WasmEdgeResult where Args: WasmValTypeList, diff --git a/src/import.rs b/src/import.rs index 674485f41..b2b78cede 100644 --- a/src/import.rs +++ b/src/import.rs @@ -54,7 +54,7 @@ impl ImportObjectBuilder { + Send + Sync + 'static, - data: Option>, + data: Option<&mut D>, ) -> WasmEdgeResult where Args: WasmValTypeList, @@ -98,7 +98,7 @@ impl ImportObjectBuilder { + Send + Sync + 'static, - data: Option>, + data: Option<&mut D>, ) -> WasmEdgeResult { let boxed_func = Box::new(real_func); let inner_func = sys::Function::create_sync_func::(&ty.into(), boxed_func, data, 0)?; diff --git a/src/plugin.rs b/src/plugin.rs index 89bcf9b74..79442307f 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -268,7 +268,7 @@ impl PluginModuleBuilder { + Send + Sync + 'static, - data: Option>, + data: Option<&mut D>, ) -> WasmEdgeResult where Args: WasmValTypeList,