diff --git a/crates/wasmedge-sys/src/executor.rs b/crates/wasmedge-sys/src/executor.rs index e64159445..166b43527 100644 --- a/crates/wasmedge-sys/src/executor.rs +++ b/crates/wasmedge-sys/src/executor.rs @@ -487,7 +487,12 @@ mod tests { let result = FuncType::create([ValType::ExternRef, ValType::I32], [ValType::I32]); assert!(result.is_ok()); let func_ty = result.unwrap(); - let result = Function::create_sync_func::(&func_ty, Box::new(real_add), None, 0); + let result = Function::create_sync_func::( + &func_ty, + Box::new(real_add), + std::ptr::null_mut(), + 0, + ); assert!(result.is_ok()); let host_func = result.unwrap(); // add the function into the import_obj module @@ -714,8 +719,12 @@ mod tests { assert!(result.is_ok()); let ty = FuncType::create([], [])?; - let async_hello_func = - Function::create_async_func::(&ty, Box::new(async_hello), None, 0)?; + let async_hello_func = Function::create_async_func::( + &ty, + Box::new(async_hello), + std::ptr::null_mut(), + 0, + )?; let mut import = ImportModule::::create("extern", None)?; import.add_func("async_hello", async_hello_func); diff --git a/crates/wasmedge-sys/src/instance/function.rs b/crates/wasmedge-sys/src/instance/function.rs index ebdb0698c..f794055fc 100644 --- a/crates/wasmedge-sys/src/instance/function.rs +++ b/crates/wasmedge-sys/src/instance/function.rs @@ -228,15 +228,10 @@ impl Function { pub fn create_sync_func( ty: &FuncType, real_fn: BoxedFn, - data: Option>, + data: *mut T, cost: u64, ) -> WasmEdgeResult { - let data = match data { - Some(d) => Box::into_raw(d) as *mut std::ffi::c_void, - None => std::ptr::null_mut(), - }; - - unsafe { Self::create_with_data(ty, real_fn, data, cost) } + unsafe { Self::create_with_data(ty, real_fn, data as _, cost) } } /// Creates a [host function](crate::Function) with the given function type. @@ -317,14 +312,9 @@ impl Function { pub fn create_async_func( ty: &FuncType, real_fn: BoxedAsyncFn, - data: Option>, + data: *mut T, cost: u64, ) -> WasmEdgeResult { - let data = match data { - Some(d) => Box::into_raw(d) as *mut std::ffi::c_void, - None => std::ptr::null_mut(), - }; - let mut map_host_func = ASYNC_HOST_FUNCS.write(); // generate key for the coming host function @@ -341,7 +331,7 @@ impl Function { ty.inner.0, Some(wrap_async_fn), key as *const usize as *mut c_void, - data, + data as _, cost, ) }; @@ -814,7 +804,7 @@ 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], @@ -828,7 +818,7 @@ mod tests { ) -> Result, HostFuncError> { println!("Rust: Entering Rust function real_add"); - let host_data = unsafe { Box::from_raw(data as *mut T) }; + let host_data = data as *mut T; println!("host_data: {:?}", host_data); if input.len() != 2 { @@ -865,7 +855,7 @@ mod tests { let result = Function::create_sync_func( &func_ty, Box::new(real_add::>), - Some(Box::new(data)), + &mut data as *mut _, 0, ); assert!(result.is_ok()); @@ -945,8 +935,12 @@ 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), None, 0); + let result = Function::create_sync_func::( + &func_ty, + Box::new(real_add), + std::ptr::null_mut(), + 0, + ); assert!(result.is_ok()); let host_func = result.unwrap(); @@ -973,7 +967,12 @@ 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(func), None, 0); + let result = Function::create_sync_func::( + &func_ty, + Box::new(func), + std::ptr::null_mut(), + 0, + ); assert!(result.is_ok()); let host_func = result.unwrap(); @@ -992,7 +991,12 @@ 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), None, 0); + let result = Function::create_sync_func::( + &func_ty, + Box::new(real_add), + std::ptr::null_mut(), + 0, + ); assert!(result.is_ok()); let host_func = result.unwrap(); @@ -1023,7 +1027,12 @@ 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), None, 0); + let result = Function::create_sync_func::( + &func_ty, + Box::new(real_add), + std::ptr::null_mut(), + 0, + ); assert!(result.is_ok()); let host_func = Arc::new(Mutex::new(result.unwrap())); @@ -1121,8 +1130,12 @@ mod tests { let func_ty = result.unwrap(); // create a host function from the closure defined above - let result = - Function::create_sync_func::(&func_ty, Box::new(real_add), None, 0); + let result = Function::create_sync_func::( + &func_ty, + Box::new(real_add), + std::ptr::null_mut(), + 0, + ); assert!(result.is_ok()); let host_func = result.unwrap(); @@ -1195,7 +1208,12 @@ mod tests { let func_ty = result.unwrap(); // create a host function - let result = Function::create_sync_func::(&func_ty, Box::new(real_add), None, 0); + let result = Function::create_sync_func::( + &func_ty, + Box::new(real_add), + std::ptr::null_mut(), + 0, + ); assert!(result.is_ok()); let host_func = result.unwrap(); @@ -1322,7 +1340,7 @@ mod tests { } } - let data: Data = Data { + let mut data: Data = Data { _x: 12, _y: "hello".to_string(), _v: vec![1, 2, 3], @@ -1359,8 +1377,7 @@ mod tests { let func_ty = result.unwrap(); // create an async host function - let result = - Function::create_async_func(&func_ty, Box::new(c), Some(Box::new(data)), 0); + let result = Function::create_async_func(&func_ty, Box::new(c), &mut data, 0); assert!(result.is_ok()); let async_hello_func = result.unwrap(); @@ -1427,7 +1444,7 @@ 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], @@ -1465,7 +1482,7 @@ mod tests { let result = Function::create_async_func( &func_ty, Box::new(f::>), - Some(Box::new(data)), + &mut data as _, 0, ); assert!(result.is_ok()); diff --git a/crates/wasmedge-sys/src/instance/module.rs b/crates/wasmedge-sys/src/instance/module.rs index 925b2ea5d..b678b8a5d 100644 --- a/crates/wasmedge-sys/src/instance/module.rs +++ b/crates/wasmedge-sys/src/instance/module.rs @@ -830,7 +830,12 @@ mod tests { let result = FuncType::create([ValType::ExternRef, ValType::I32], [ValType::I32]); assert!(result.is_ok()); let func_ty = result.unwrap(); - let result = Function::create_sync_func::(&func_ty, Box::new(real_add), None, 0); + let result = Function::create_sync_func::( + &func_ty, + Box::new(real_add), + std::ptr::null_mut(), + 0, + ); assert!(result.is_ok()); assert_eq!(HOST_FUNCS.read().len(), 1); diff --git a/crates/wasmedge-sys/src/instance/table.rs b/crates/wasmedge-sys/src/instance/table.rs index e03ae9c76..1d91f36b4 100644 --- a/crates/wasmedge-sys/src/instance/table.rs +++ b/crates/wasmedge-sys/src/instance/table.rs @@ -353,7 +353,12 @@ 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), None, 0); + let result = Function::create_sync_func::( + &func_ty, + Box::new(real_add), + std::ptr::null_mut(), + 0, + ); assert!(result.is_ok()); let host_func = result.unwrap(); diff --git a/crates/wasmedge-sys/src/statistics.rs b/crates/wasmedge-sys/src/statistics.rs index 63fee1cba..8095f1a80 100644 --- a/crates/wasmedge-sys/src/statistics.rs +++ b/crates/wasmedge-sys/src/statistics.rs @@ -462,8 +462,12 @@ mod tests { let result = FuncType::create(vec![ValType::ExternRef, ValType::I32], vec![ValType::I32]); let func_ty = result.unwrap(); - let result = - Function::create_sync_func::(&func_ty, Box::new(extern_add), None, 0); + let result = Function::create_sync_func::( + &func_ty, + Box::new(extern_add), + std::ptr::null_mut(), + 0, + ); assert!(result.is_ok()); let host_func = result.unwrap(); import.add_func("func-add", host_func); @@ -472,8 +476,12 @@ mod tests { let result = FuncType::create(vec![ValType::ExternRef, ValType::I32], vec![ValType::I32]); let func_ty = result.unwrap(); - let result = - Function::create_sync_func::(&func_ty, Box::new(extern_sub), None, 0); + let result = Function::create_sync_func::( + &func_ty, + Box::new(extern_sub), + std::ptr::null_mut(), + 0, + ); assert!(result.is_ok()); let host_func = result.unwrap(); import.add_func("func-sub", host_func); @@ -482,8 +490,12 @@ mod tests { let result = FuncType::create(vec![ValType::ExternRef, ValType::I32], vec![ValType::I32]); let func_ty = result.unwrap(); - let result = - Function::create_sync_func::(&func_ty, Box::new(extern_mul), None, 0); + let result = Function::create_sync_func::( + &func_ty, + Box::new(extern_mul), + std::ptr::null_mut(), + 0, + ); assert!(result.is_ok()); let host_func = result.unwrap(); import.add_func("func-mul", host_func); @@ -492,8 +504,12 @@ mod tests { let result = FuncType::create(vec![ValType::ExternRef, ValType::I32], vec![ValType::I32]); let func_ty = result.unwrap(); - let result = - Function::create_sync_func::(&func_ty, Box::new(extern_div), None, 0); + let result = Function::create_sync_func::( + &func_ty, + Box::new(extern_div), + std::ptr::null_mut(), + 0, + ); assert!(result.is_ok()); let host_func = result.unwrap(); import.add_func("func-div", host_func); @@ -502,8 +518,12 @@ mod tests { let result = FuncType::create([], [ValType::I32]); assert!(result.is_ok()); let func_ty = result.unwrap(); - let result = - Function::create_sync_func::(&func_ty, Box::new(extern_term), None, 0); + let result = Function::create_sync_func::( + &func_ty, + Box::new(extern_term), + std::ptr::null_mut(), + 0, + ); let host_func = result.unwrap(); import.add_func("func-term", host_func); @@ -511,8 +531,12 @@ mod tests { let result = FuncType::create([], [ValType::I32]); assert!(result.is_ok()); let func_ty = result.unwrap(); - let result = - Function::create_sync_func::(&func_ty, Box::new(extern_fail), None, 0); + let result = Function::create_sync_func::( + &func_ty, + Box::new(extern_fail), + std::ptr::null_mut(), + 0, + ); let host_func = result.unwrap(); import.add_func("func-fail", host_func); diff --git a/crates/wasmedge-sys/src/store.rs b/crates/wasmedge-sys/src/store.rs index 55c2f1d42..56390a54f 100644 --- a/crates/wasmedge-sys/src/store.rs +++ b/crates/wasmedge-sys/src/store.rs @@ -163,7 +163,12 @@ mod tests { let result = FuncType::create(vec![ValType::I32; 2], vec![ValType::I32]); assert!(result.is_ok()); let func_ty = result.unwrap(); - let result = Function::create_sync_func::(&func_ty, Box::new(real_add), None, 0); + let result = Function::create_sync_func::( + &func_ty, + Box::new(real_add), + std::ptr::null_mut(), + 0, + ); assert!(result.is_ok()); let host_func = result.unwrap(); import.add_func("add", host_func); @@ -248,8 +253,12 @@ mod tests { let result = FuncType::create(vec![ValType::I32; 2], vec![ValType::I32]); assert!(result.is_ok()); let func_ty = result.unwrap(); - let result = - Function::create_sync_func::(&func_ty, Box::new(real_add), None, 0); + let result = Function::create_sync_func::( + &func_ty, + Box::new(real_add), + std::ptr::null_mut(), + 0, + ); assert!(result.is_ok()); let host_func = result.unwrap(); import.add_func("add", host_func); diff --git a/crates/wasmedge-types/src/lib.rs b/crates/wasmedge-types/src/lib.rs index 8c8267a02..b80bb5cde 100644 --- a/crates/wasmedge-types/src/lib.rs +++ b/crates/wasmedge-types/src/lib.rs @@ -576,7 +576,5 @@ pub use wat::parse_bytes as wat2wasm; pub type WasmEdgeResult = Result>; /// This is a workaround solution to the [`never`](https://doc.rust-lang.org/std/primitive.never.html) type in Rust. It will be replaced by `!` once it is stable. -#[derive(Debug, Clone)] -pub enum NeverType {} -unsafe impl Send for NeverType {} -unsafe impl Sync for NeverType {} +#[derive(Debug, Clone, Default)] +pub struct NeverType; diff --git a/src/executor.rs b/src/executor.rs index 3ec167589..d6bf24d93 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -290,7 +290,7 @@ 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], @@ -298,8 +298,7 @@ mod tests { }; // create an async host function - let result = - Func::wrap_async_func::<(), (), Data>(async_hello, Some(Box::new(data))); + let result = Func::wrap_async_func::<(), (), Data>(async_hello, &mut data as _); assert!(result.is_ok()); let func = result.unwrap(); diff --git a/src/externals/function.rs b/src/externals/function.rs index 497e1c52e..d0c3fe135 100644 --- a/src/externals/function.rs +++ b/src/externals/function.rs @@ -42,7 +42,7 @@ impl Func { + Send + Sync + 'static, - data: Option>, + data: *mut T, ) -> WasmEdgeResult where Args: WasmValTypeList, @@ -86,7 +86,7 @@ impl Func { + Send + Sync + 'static, - data: Option>, + data: *mut T, ) -> WasmEdgeResult { let boxed_func = Box::new(real_func); let inner = sys::Function::create_sync_func::(&ty.clone().into(), boxed_func, data, 0)?; @@ -125,7 +125,7 @@ impl Func { > + Send + Sync + 'static, - data: Option>, + data: *mut T, ) -> WasmEdgeResult where Args: WasmValTypeList, @@ -175,7 +175,7 @@ impl Func { > + Send + Sync + 'static, - data: Option>, + data: *mut T, ) -> WasmEdgeResult where T: Send + Sync, @@ -452,7 +452,7 @@ mod tests { fn test_func_basic() { // create an ImportModule let result = ImportObjectBuilder::new() - .with_func::<(i32, i32), i32, NeverType>("add", real_add, None) + .with_func::<(i32, i32), i32, NeverType>("add", real_add, std::ptr::null_mut()) .expect("failed to add host func") .build::("extern", None); assert!(result.is_ok()); @@ -507,7 +507,7 @@ mod tests { #[test] fn test_func_wrap() { // create a host function - let result = Func::wrap::<(i32, i32), i32, NeverType>(real_add, None); + let result = Func::wrap::<(i32, i32), i32, NeverType>(real_add, std::ptr::null_mut()); assert!(result.is_ok()); let func = result.unwrap(); @@ -637,7 +637,7 @@ 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], @@ -646,7 +646,7 @@ mod tests { // create an ImportModule instance let result = ImportObjectBuilder::new() - .with_async_func::<(), (), Data>("async_hello", c, Some(Box::new(data)))? + .with_async_func::<(), (), Data>("async_hello", c, &mut data as _)? .build::("extern", None); assert!(result.is_ok()); let import = result.unwrap(); diff --git a/src/externals/table.rs b/src/externals/table.rs index 067235cb3..59dc64499 100644 --- a/src/externals/table.rs +++ b/src/externals/table.rs @@ -151,7 +151,7 @@ mod tests { // create an import object let result = ImportObjectBuilder::new() - .with_func::<(i32, i32), i32, NeverType>("add", real_add, None) + .with_func::<(i32, i32), i32, NeverType>("add", real_add, std::ptr::null_mut()) .expect("failed to add host func") .with_table("table", table) .build::("extern", None); diff --git a/src/import.rs b/src/import.rs index bb8bc86ce..d1a0b04ea 100644 --- a/src/import.rs +++ b/src/import.rs @@ -54,7 +54,7 @@ impl ImportObjectBuilder { + Send + Sync + 'static, - data: Option>, + data: *mut D, ) -> WasmEdgeResult where Args: WasmValTypeList, @@ -98,7 +98,7 @@ impl ImportObjectBuilder { + Send + Sync + 'static, - data: Option>, + data: *mut D, ) -> WasmEdgeResult { let boxed_func = Box::new(real_func); let inner_func = sys::Function::create_sync_func::(&ty.into(), boxed_func, data, 0)?; @@ -135,7 +135,7 @@ impl ImportObjectBuilder { > + Send + Sync + 'static, - data: Option>, + data: *mut D, ) -> WasmEdgeResult where Args: WasmValTypeList, @@ -350,7 +350,7 @@ mod tests { // create an import object let result = ImportObjectBuilder::new() - .with_func::<(i32, i32), i32, NeverType>("add", real_add, None) + .with_func::<(i32, i32), i32, NeverType>("add", real_add, std::ptr::null_mut()) .expect("failed to add host func") .build::("extern", None); assert!(result.is_ok()); @@ -596,7 +596,7 @@ mod tests { // create an import object let result = ImportObjectBuilder::new() - .with_func::<(i32, i32), i32, NeverType>("add", real_add, None) + .with_func::<(i32, i32), i32, NeverType>("add", real_add, std::ptr::null_mut()) .expect("failed to add host func") .with_table("table", table) .build::("extern", None); @@ -723,7 +723,7 @@ mod tests { // create an ImportModule instance let result = ImportObjectBuilder::new() - .with_func::<(i32, i32), i32, NeverType>("add", real_add, None) + .with_func::<(i32, i32), i32, NeverType>("add", real_add, std::ptr::null_mut()) .expect("failed to add host function") .with_global("global", global_const) .with_memory("memory", memory) @@ -841,7 +841,7 @@ mod tests { // create an import object let result = ImportObjectBuilder::new() - .with_func::<(i32, i32), i32, NeverType>("add", real_add, None) + .with_func::<(i32, i32), i32, NeverType>("add", real_add, std::ptr::null_mut()) .expect("failed to add host function") .with_global("global", global_const) .with_memory("memory", memory) diff --git a/src/instance.rs b/src/instance.rs index 4099caa49..e6fb80ab8 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -252,7 +252,7 @@ mod tests { // create an ImportModule instance let result = ImportObjectBuilder::new() - .with_func::<(i32, i32), i32, NeverType>("add", real_add, None) + .with_func::<(i32, i32), i32, NeverType>("add", real_add, std::ptr::null_mut()) .expect("failed to add host function") .with_global("global", global_const) .with_memory("mem", memory) diff --git a/src/plugin.rs b/src/plugin.rs index 6250abfab..bed291c0f 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -288,7 +288,7 @@ impl PluginModuleBuilder { + Send + Sync + 'static, - data: Option>, + data: *mut D, ) -> WasmEdgeResult where Args: WasmValTypeList, @@ -330,7 +330,7 @@ impl PluginModuleBuilder { > + Send + Sync + 'static, - data: Option>, + data: *mut D, ) -> WasmEdgeResult where Args: WasmValTypeList, diff --git a/src/store.rs b/src/store.rs index a4bf2b1ca..2a2c30683 100644 --- a/src/store.rs +++ b/src/store.rs @@ -212,7 +212,7 @@ mod tests { // create an ImportModule instance let result = ImportObjectBuilder::new() - .with_func::<(i32, i32), i32, NeverType>("add", real_add, None) + .with_func::<(i32, i32), i32, NeverType>("add", real_add, std::ptr::null_mut()) .expect("failed to add host function") .with_global("global", global_const) .with_memory("mem", memory) @@ -390,7 +390,7 @@ mod tests { // create an ImportModule instance let result = ImportObjectBuilder::new() - .with_func::<(i32, i32), i32, NeverType>("add", real_add, None) + .with_func::<(i32, i32), i32, NeverType>("add", real_add, std::ptr::null_mut()) .expect("failed to add host function") .with_global("global", global_const) .with_memory("mem", memory)