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.
Implementations§
source§impl Func
impl Func
sourcepub fn wrap<Args, Rets, T>(
real_func: impl Fn(CallingFrame, Vec<WasmValue>, *mut c_void) -> Result<Vec<WasmValue>, HostFuncError> + Send + Sync + 'static,
data: Option<Box<T>>
) -> WasmEdgeResult<Self>where
Args: WasmValTypeList,
Rets: WasmValTypeList,
pub fn wrap<Args, Rets, T>( real_func: impl Fn(CallingFrame, Vec<WasmValue>, *mut c_void) -> Result<Vec<WasmValue>, HostFuncError> + Send + Sync + 'static, data: Option<Box<T>> ) -> 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. -
data
- The host context data used in this function.
Error
- If fail to create a Func instance, then WasmEdgeError::Func(FuncError::Create) is returned.
sourcepub fn wrap_with_type<T>(
ty: FuncType,
real_func: impl Fn(CallingFrame, Vec<WasmValue>, *mut c_void) -> Result<Vec<WasmValue>, HostFuncError> + Send + Sync + 'static,
data: Option<Box<T>>
) -> WasmEdgeResult<Self>
pub fn wrap_with_type<T>( ty: FuncType, real_func: impl Fn(CallingFrame, Vec<WasmValue>, *mut c_void) -> Result<Vec<WasmValue>, HostFuncError> + Send + Sync + 'static, data: Option<Box<T>> ) -> WasmEdgeResult<Self>
Creates a host function with 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. -
data
- The host context data used in this function.
Error
- If fail to create a Func instance, then WasmEdgeError::Func(FuncError::Create) is returned.
sourcepub fn wrap_async_func<Args, Rets, T>(
real_func: impl Fn(CallingFrame, Vec<WasmValue>, *mut c_void) -> Box<dyn Future<Output = Result<Vec<WasmValue>, HostFuncError>> + Send> + Send + Sync + 'static,
data: Option<Box<T>>
) -> WasmEdgeResult<Self>where
Args: WasmValTypeList,
Rets: WasmValTypeList,
T: Send + Sync,
Available on crate feature async
and Linux only.
pub fn wrap_async_func<Args, Rets, T>( real_func: impl Fn(CallingFrame, Vec<WasmValue>, *mut c_void) -> Box<dyn Future<Output = Result<Vec<WasmValue>, HostFuncError>> + Send> + Send + Sync + 'static, data: Option<Box<T>> ) -> WasmEdgeResult<Self>where Args: WasmValTypeList, Rets: WasmValTypeList, T: Send + Sync,
async
and Linux only.Creates an asynchronous host function by wrapping a native async function.
N.B. that this function can be used in thread-safe scenarios.
Arguments
-
real_func
- The native function to be wrapped. -
data
- The host context data used in this function.
Error
- If fail to create a Func instance, then WasmEdgeError::Func(FuncError::Create) is returned.
sourcepub fn wrap_async_func_with_type<T>(
ty: FuncType,
real_func: impl Fn(CallingFrame, Vec<WasmValue>, *mut c_void) -> Box<dyn Future<Output = Result<Vec<WasmValue>, HostFuncError>> + Send> + Send + Sync + 'static,
data: Option<Box<T>>
) -> WasmEdgeResult<Self>where
T: Send + Sync,
Available on crate feature async
and Linux only.
pub fn wrap_async_func_with_type<T>( ty: FuncType, real_func: impl Fn(CallingFrame, Vec<WasmValue>, *mut c_void) -> Box<dyn Future<Output = Result<Vec<WasmValue>, HostFuncError>> + Send> + Send + Sync + 'static, data: Option<Box<T>> ) -> WasmEdgeResult<Self>where T: Send + Sync,
async
and Linux only.Creates an asynchronous host function by wrapping a native async function with the given function type.
N.B. that this function can be used in thread-safe scenarios.
Arguments
-
ty
- The function type. -
real_func
- The native function to be wrapped. -
data
- The host context data used in this function.
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.
sourcepub fn run(
&self,
executor: &Executor,
args: impl IntoIterator<Item = WasmValue>
) -> WasmEdgeResult<Vec<WasmValue>>
pub fn run( &self, executor: &Executor, args: impl IntoIterator<Item = WasmValue> ) -> WasmEdgeResult<Vec<WasmValue>>
sourcepub fn run_with_timeout(
&self,
executor: &Executor,
args: impl IntoIterator<Item = WasmValue>,
timeout: Duration
) -> WasmEdgeResult<Vec<WasmValue>>
Available on Linux and non-musl only.
pub fn run_with_timeout( &self, executor: &Executor, args: impl IntoIterator<Item = WasmValue>, timeout: Duration ) -> WasmEdgeResult<Vec<WasmValue>>
sourcepub async fn run_async(
&self,
async_state: &AsyncState,
executor: &Executor,
args: impl IntoIterator<Item = WasmValue> + Send
) -> WasmEdgeResult<Vec<WasmValue>>
Available on crate feature async
and Linux only.
pub async fn run_async( &self, async_state: &AsyncState, executor: &Executor, args: impl IntoIterator<Item = WasmValue> + Send ) -> WasmEdgeResult<Vec<WasmValue>>
async
and Linux only.sourcepub async fn run_async_with_timeout(
&self,
async_state: &AsyncState,
executor: &Executor,
args: impl IntoIterator<Item = WasmValue> + Send,
timeout: Duration
) -> WasmEdgeResult<Vec<WasmValue>>
Available on crate feature async
and Linux and non-musl only.
pub async fn run_async_with_timeout( &self, async_state: &AsyncState, executor: &Executor, args: impl IntoIterator<Item = WasmValue> + Send, timeout: Duration ) -> WasmEdgeResult<Vec<WasmValue>>
async
and Linux and non-musl only.Asynchronously runs this host function with a timeout setting.
Arguments
-
async_state
- Used to store asynchronous state at run time. -
executor
- The Executor instance. -
args
- The arguments passed to the host function. -
timeout
- The maximum execution time of the function to be run.
Error
If fail to run the host function, then an error is returned.