Skip to main content

WasmEdge_FunctionInstanceCreate

Function 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 to a WasmEdge_ModuleInstanceContext. The following is an example of creating 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;
}

WasmEdge_ValType Params[2] = {WasmEdge_ValTypeGenI32(),
                              WasmEdge_ValTypeGenI32()};
WasmEdge_ValType Returns[1] = {WasmEdge_ValTypeGenI32()};
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 follows:

typedef WasmEdge_Result (*WasmEdge_HostFunc_t)(
    void *Data,
    const WasmEdge_CallingFrameContext *CallFrameCxt,
    const WasmEdge_Value *Params,
    WasmEdge_Value *Returns);

The Params array has a length guaranteed to be the same as the parameter types in the Type. The Returns array has a 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 lifetime 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.