Skip to content

Commit

Permalink
[refactor] Update the APIs related to sync host function creation (#26)
Browse files Browse the repository at this point in the history
* refactor!(rust-sys): update `Function::create_sync_func`

Signed-off-by: Xin Liu <[email protected]>

* refactor!(rust-sdk): update `Func`, `ImportObjectBuilder` and `PluginModuleBuilder`

Signed-off-by: Xin Liu <[email protected]>

---------

Signed-off-by: Xin Liu <[email protected]>
  • Loading branch information
apepkuss authored Jul 21, 2023
1 parent 0db1616 commit ee2f127
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 27 deletions.
16 changes: 6 additions & 10 deletions crates/wasmedge-sys/examples/context_data_to_host_func.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use wasmedge_macro::sys_host_function;
use wasmedge_sys::{CallingFrame, Executor, FuncType, Function, WasmValue};
use wasmedge_types::{error::HostFuncError, ValType};

Expand All @@ -9,15 +10,15 @@ struct Data<T, S> {
_s: Vec<S>,
}

#[sys_host_function]
fn real_add<T: core::fmt::Debug>(
_frame: CallingFrame,
input: Vec<WasmValue>,
data: *mut std::ffi::c_void,
data: &mut Data<i32, &str>,
) -> Result<Vec<WasmValue>, 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));
Expand All @@ -43,7 +44,7 @@ fn real_add<T: core::fmt::Debug>(
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
let data: Data<i32, &str> = Data {
let mut data: Data<i32, &str> = Data {
_x: 12,
_y: "hello".to_string(),
_v: vec![1, 2, 3],
Expand All @@ -55,12 +56,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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::<Data<i32, &str>>),
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();

Expand Down
19 changes: 7 additions & 12 deletions crates/wasmedge-sys/src/instance/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,11 @@ impl Function {
pub fn create_sync_func<T>(
ty: &FuncType,
real_fn: BoxedFn,
data: Option<Box<T>>,
data: Option<&mut T>,
cost: u64,
) -> WasmEdgeResult<Self> {
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(),
};

Expand Down Expand Up @@ -1009,22 +1009,22 @@ mod tests {
_v: Vec<T>,
_s: Vec<S>,
}
let data: Data<i32, &str> = Data {
let mut data: Data<i32, &str> = Data {
_x: 12,
_y: "hello".to_string(),
_v: vec![1, 2, 3],
_s: vec!["macos", "linux", "windows"],
};

#[sys_host_function]
fn real_add<T: core::fmt::Debug>(
_frame: CallingFrame,
input: Vec<WasmValue>,
data: *mut std::ffi::c_void,
data: &mut Data<i32, &str>,
) -> Result<Vec<WasmValue>, 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));
Expand Down Expand Up @@ -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::<Data<i32, &str>>),
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();

Expand Down
4 changes: 2 additions & 2 deletions src/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Func {
+ Send
+ Sync
+ 'static,
data: Option<Box<T>>,
data: Option<&mut T>,
) -> WasmEdgeResult<Self> {
let boxed_func = Box::new(real_func);
let inner = sys::Function::create_sync_func::<T>(&ty.clone().into(), boxed_func, data, 0)?;
Expand Down Expand Up @@ -76,7 +76,7 @@ impl Func {
+ Send
+ Sync
+ 'static,
data: Option<Box<T>>,
data: Option<&mut T>,
) -> WasmEdgeResult<Self>
where
Args: WasmValTypeList,
Expand Down
4 changes: 2 additions & 2 deletions src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl ImportObjectBuilder {
+ Send
+ Sync
+ 'static,
data: Option<Box<D>>,
data: Option<&mut D>,
) -> WasmEdgeResult<Self>
where
Args: WasmValTypeList,
Expand Down Expand Up @@ -98,7 +98,7 @@ impl ImportObjectBuilder {
+ Send
+ Sync
+ 'static,
data: Option<Box<D>>,
data: Option<&mut D>,
) -> WasmEdgeResult<Self> {
let boxed_func = Box::new(real_func);
let inner_func = sys::Function::create_sync_func::<D>(&ty.into(), boxed_func, data, 0)?;
Expand Down
2 changes: 1 addition & 1 deletion src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ impl<T: Send + Sync + Clone> PluginModuleBuilder<T> {
+ Send
+ Sync
+ 'static,
data: Option<Box<D>>,
data: Option<&mut D>,
) -> WasmEdgeResult<Self>
where
Args: WasmValTypeList,
Expand Down

0 comments on commit ee2f127

Please sign in to comment.