Struct wasmedge_sdk::Func
source · pub struct Func { /* private fields */ }
Expand description
Defines a host function instance.
A WasmEdge Func is a wasm function instance, which is a “wrapper” of the original function (defined in either the host or the WebAssembly module) over the runtime module instance of its originating module.
Example
The following example shows how to create a host function, and invoke it with a given executor.
// If the version of rust used is less than v1.63,
// #![feature(explicit_generic_args_with_impl_trait)]
use wasmedge_sdk::{Func, Executor, params, WasmVal, error::HostFuncError, WasmValue, ValType, Caller, host_function};
// A native function to be wrapped as a host function
#[host_function]
fn real_add(_: Caller, input: Vec<WasmValue>) -> Result<Vec<WasmValue>, HostFuncError> {
if input.len() != 2 {
return Err(HostFuncError::User(1));
}
let a = if input[0].ty() == ValType::I32 {
input[0].to_i32()
} else {
return Err(HostFuncError::User(2));
};
let b = if input[1].ty() == ValType::I32 {
input[1].to_i32()
} else {
return Err(HostFuncError::User(3));
};
let c = a + b;
Ok(vec![WasmValue::from_i32(c)])
}
// create a host function
let result = Func::wrap::<(i32, i32), i32>(real_add);
assert!(result.is_ok());
let func = result.unwrap();
// create an executor
let mut executor = Executor::new(None, None).unwrap();
// call the host function
let result = func.run(&mut executor, params!(2, 3));
assert!(result.is_ok());
let returns = result.unwrap();
assert_eq!(returns[0].to_i32(), 5);
Implementations§
source§impl Func
impl Func
sourcepub fn new(
ty: FuncType,
real_func: impl Fn(CallingFrame, Vec<WasmValue>) -> Result<Vec<WasmValue>, HostFuncError> + Send + Sync + 'static
) -> WasmEdgeResult<Self>
pub fn new(
ty: FuncType,
real_func: impl Fn(CallingFrame, Vec<WasmValue>) -> Result<Vec<WasmValue>, HostFuncError> + Send + Sync + 'static
) -> WasmEdgeResult<Self>
Creates a host function of the given func type.
N.B. that this function can be used in thread-safe scenarios.
Arguments
-
ty
- The function type. -
real_func
- The native function that will be wrapped as a host function.
Error
- If fail to create a Func instance, then WasmEdgeError::Func(FuncError::Create) is returned.
sourcepub fn wrap<Args, Rets>(
real_func: impl Fn(CallingFrame, Vec<WasmValue>) -> Result<Vec<WasmValue>, HostFuncError> + Send + Sync + 'static
) -> WasmEdgeResult<Self>where
Args: WasmValTypeList,
Rets: WasmValTypeList,
pub fn wrap<Args, Rets>(
real_func: impl Fn(CallingFrame, Vec<WasmValue>) -> Result<Vec<WasmValue>, HostFuncError> + Send + Sync + 'static
) -> WasmEdgeResult<Self>where
Args: WasmValTypeList,
Rets: WasmValTypeList,
Creates a host function by wrapping a native function.
N.B. that this function can be used in thread-safe scenarios.
Arguments
real_func
- The native function to be wrapped.
Error
- If fail to create a Func instance, then WasmEdgeError::Func(FuncError::Create) is returned.
sourcepub fn wrap_async<Args, Rets>(
real_func: impl Fn(CallingFrame, Vec<WasmValue>) -> Box<dyn Future<Output = Result<Vec<WasmValue>, HostFuncError>> + Send> + Send + Sync + 'static
) -> WasmEdgeResult<Self>where
Args: WasmValTypeList,
Rets: WasmValTypeList,
pub fn wrap_async<Args, Rets>(
real_func: impl Fn(CallingFrame, Vec<WasmValue>) -> Box<dyn Future<Output = Result<Vec<WasmValue>, HostFuncError>> + Send> + Send + Sync + 'static
) -> WasmEdgeResult<Self>where
Args: WasmValTypeList,
Rets: WasmValTypeList,
Creates an asynchronous host function by wrapping a native function.
N.B. that this function can be used in thread-safe scenarios.
Arguments
real_func
- The native function to be wrapped.
Error
- If fail to create a Func instance, then WasmEdgeError::Func(FuncError::Create) is returned.
sourcepub fn name(&self) -> Option<&str>
pub fn name(&self) -> Option<&str>
Returns the exported name of this function.
Notice that this field is meaningful only if this host function is used as an exported instance.
sourcepub fn mod_name(&self) -> Option<&str>
pub fn mod_name(&self) -> Option<&str>
Returns the name of the module instance from which this function exports.
Notice that this field is meaningful only if this host function is used as an exported instance.