Struct wasmedge_sys::Function
source · pub struct Function { /* private fields */ }
Expand description
Implementations§
source§impl Function
impl Function
sourcepub fn create_sync_func<T>(
ty: &FuncType,
real_fn: Box<dyn Fn(CallingFrame, Vec<WasmValue>, *mut c_void) -> Result<Vec<WasmValue>, HostFuncError> + Send + Sync>,
data: Option<Box<T>>,
cost: u64
) -> WasmEdgeResult<Self>
pub fn create_sync_func<T>( ty: &FuncType, real_fn: Box<dyn Fn(CallingFrame, Vec<WasmValue>, *mut c_void) -> Result<Vec<WasmValue>, HostFuncError> + Send + Sync>, data: Option<Box<T>>, cost: u64 ) -> WasmEdgeResult<Self>
Creates a host function with the given function type.
N.B. that this function is used for thread-safe scenarios.
Arguments
-
ty
- The types of the arguments and returns of the target function. -
real_fn
- The pointer to the target function. -
data
- The host context data used in this function. -
cost
- The function cost in the Statistics. Pass 0 if the calculation is not needed.
Error
- If fail to create a Function, then WasmEdgeError::Func(FuncError::Create) is returned.
Example
The example defines a host function real_add
, and creates a Function binding to it by calling
the create_binding
method.
use wasmedge_macro::sys_host_function;
use wasmedge_sys::{FuncType, Function, WasmValue, CallingFrame};
use wasmedge_types::{error::HostFuncError, ValType, WasmEdgeResult, NeverType};
#[sys_host_function]
fn real_add(_frame: CallingFrame, inputs: Vec<WasmValue>) -> Result<Vec<WasmValue>, HostFuncError> {
if inputs.len() != 2 {
return Err(HostFuncError::User(1));
}
let a = if inputs[0].ty() == ValType::I32 {
inputs[0].to_i32()
} else {
return Err(HostFuncError::User(2));
};
let b = if inputs[1].ty() == ValType::I32 {
inputs[1].to_i32()
} else {
return Err(HostFuncError::User(3));
};
let c = a + b;
Ok(vec![WasmValue::from_i32(c)])
}
// create a FuncType
let func_ty = FuncType::create(vec![ValType::I32; 2], vec![ValType::I32]).expect("fail to create a FuncType");
// create a Function instance
let func = Function::create_sync_func::<NeverType>(&func_ty, Box::new(real_add), None, 0).expect("fail to create a Function instance");
sourcepub fn create_async_func<T: Send + Sync>(
ty: &FuncType,
real_fn: Box<dyn Fn(CallingFrame, Vec<WasmValue>, *mut c_void) -> Box<dyn Future<Output = Result<Vec<WasmValue>, HostFuncError>> + Send> + Send + Sync>,
data: Option<Box<T>>,
cost: u64
) -> WasmEdgeResult<Self>
Available on crate feature async
and Linux only.
pub fn create_async_func<T: Send + Sync>( ty: &FuncType, real_fn: Box<dyn Fn(CallingFrame, Vec<WasmValue>, *mut c_void) -> Box<dyn Future<Output = Result<Vec<WasmValue>, HostFuncError>> + Send> + Send + Sync>, data: Option<Box<T>>, cost: u64 ) -> WasmEdgeResult<Self>
async
and Linux only.Creates an async host function with the given function type.
Arguments
-
ty
- The types of the arguments and returns of the target function. -
real_fn
- The pointer to the target function. -
data
- The host context data used in this function. -
cost
- The function cost in the Statistics. Pass 0 if the calculation is not needed.
Error
- If fail to create a Function, then WasmEdgeError::Func(FuncError::Create) is returned.
sourcepub unsafe fn create_with_custom_wrapper(
ty: &FuncType,
fn_wrapper: CustomFnWrapper,
real_fn: *mut c_void,
data: *mut c_void,
data_owner: bool,
cost: u64
) -> WasmEdgeResult<Self>
pub unsafe fn create_with_custom_wrapper( ty: &FuncType, fn_wrapper: CustomFnWrapper, real_fn: *mut c_void, data: *mut c_void, data_owner: bool, cost: u64 ) -> WasmEdgeResult<Self>
Creates a host function with the given function type and the custom function wrapper.
Arguments
-
ty
- The types of the arguments and returns of the target function. -
fn_wrapper
- The custom function wrapper. -
real_fn
- The pointer to the target function. -
data
- The pointer to the host context data used in this function. -
data_owner
- Whether the host context data is owned by the host function. -
cost
- The function cost in the Statistics. Pass 0 if the calculation is not needed.
Error
- If fail to create a Function, then WasmEdgeError::Func(FuncError::Create) is returned.
Safety
Notice that the caller should guarantee the life cycle of both the real_fn
and the data
object.
sourcepub fn ty(&self) -> WasmEdgeResult<FuncType>
pub fn ty(&self) -> WasmEdgeResult<FuncType>
sourcepub fn call<E: Engine>(
&self,
engine: &E,
args: impl IntoIterator<Item = WasmValue>
) -> WasmEdgeResult<Vec<WasmValue>>
pub fn call<E: Engine>( &self, engine: &E, args: impl IntoIterator<Item = WasmValue> ) -> WasmEdgeResult<Vec<WasmValue>>
sourcepub async fn call_async<E: Engine + Send + Sync>(
&self,
async_state: &AsyncState,
engine: &E,
args: impl IntoIterator<Item = WasmValue> + Send
) -> WasmEdgeResult<Vec<WasmValue>>
Available on crate feature async
and Linux only.
pub async fn call_async<E: Engine + Send + Sync>( &self, async_state: &AsyncState, engine: &E, args: impl IntoIterator<Item = WasmValue> + Send ) -> WasmEdgeResult<Vec<WasmValue>>
async
and Linux only.Runs this host function asynchronously and returns the result.
Arguments
-
async_state
- Used to store asynchronous state at run time. -
engine
- The object implementing the Engine trait. -
args
- The arguments passed to the host function.
Error
If fail to run the host function, then an error is returned.
sourcepub fn as_ptr(&self) -> *const WasmEdge_FunctionInstanceContext
Available on crate feature ffi
only.
pub fn as_ptr(&self) -> *const WasmEdge_FunctionInstanceContext
ffi
only.Provides a raw pointer to the inner function context.