Function wasmedge_sys::ffi::WasmEdge_FunctionInstanceCreate
source · pub unsafe extern "C" fn WasmEdge_FunctionInstanceCreate(
Type: *const WasmEdge_FunctionTypeContext,
HostFunc: WasmEdge_HostFunc_t,
Data: *mut c_void,
Cost: u64
) -> *mut WasmEdge_FunctionInstanceContext
Expand description
Creation of the WasmEdge_FunctionInstanceContext for host functions.
The caller owns the object and should call WasmEdge_FunctionInstanceDelete
to destroy it if the returned object is not added into a
WasmEdge_ModuleInstanceContext
. The following is an example to create a
host function context.
WasmEdge_Result FuncAdd(void *Data,
const WasmEdge_CallingFrameContext *CallFrameCxt,
const WasmEdge_Value *In, WasmEdge_Value *Out) {
// Function to return A + B.
int32_t A = WasmEdge_ValueGetI32(In[0]);
int32_t B = WasmEdge_ValueGetI32(In[1]);
Out[0] = WasmEdge_ValueGenI32(A + B);
// Return execution status
return WasmEdge_Result_Success;
}
enum WasmEdge_ValType Params[2] = {WasmEdge_ValType_I32,
WasmEdge_ValType_I32};
enum WasmEdge_ValType Returns[1] = {WasmEdge_ValType_I32};
WasmEdge_FunctionTypeContext *FuncType =
WasmEdge_FunctionTypeCreate(Params, 2, Returns, 1);
WasmEdge_FunctionInstanceContext *HostFunc =
WasmEdge_FunctionInstanceCreate(FuncType, FuncAdd, NULL, 0);
WasmEdge_FunctionTypeDelete(FuncType);
...
\param Type the function type context to describe the host function signature. \param HostFunc the host function pointer. The host function signature must be as following:
typedef WasmEdge_Result (*WasmEdge_HostFunc_t)(
void *Data,
const WasmEdge_CallingFrameContext *CallFrameCxt,
const WasmEdge_Value *Params,
WasmEdge_Value *Returns);
The Params
is the input parameters array with length guaranteed to be the
same as the parameter types in the Type
. The Returns
is the output
results array with length guaranteed to be the same as the result types in
the Type
. The return value is WasmEdge_Result
for the execution status.
\param Data the additional object, such as the pointer to a data structure,
to set to this host function context. The caller should guarantee the life
cycle of the object. NULL if the additional data object is not needed.
\param Cost the function cost in statistics. Pass 0 if the calculation is
not needed.
\returns pointer to context, NULL if failed.