pub unsafe extern "C" fn WasmEdge_FunctionInstanceCreateBinding(
Type: *const WasmEdge_FunctionTypeContext,
WrapFunc: WasmEdge_WrapFunc_t,
Binding: *mut c_void,
Data: *mut c_void,
Cost: u64,
) -> *mut WasmEdge_FunctionInstanceContextExpand description
Creation of the WasmEdge_FunctionInstanceContext for host functions.
This function is for the languages that cannot pass the function pointer of
the host function into this shared library directly. 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 for other
languages.
// `RealFunc` is the pointer to the function in other languages.
WasmEdge_Result FuncAddWrap(
void *This, void *Data,
const WasmEdge_CallingFrameContext *CallFrameCxt,
const WasmEdge_Value *In, const uint32_t InLen, WasmEdge_Value *Out,
const uint32_t OutLen) {
// Wrapper function for a host function that returns A + B.
// `This` is the same as `RealFunc`.
int32_t A = WasmEdge_ValueGetI32(In[0]);
int32_t B = WasmEdge_ValueGetI32(In[1]);
// Call the function pointed to by `This` in the host language ...
int32_t Result = ...;
Out[0] = Result;
// Return the 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_FunctionInstanceCreateBinding(
FuncType, FuncAddWrap, RealFunc, NULL, 0);
WasmEdge_FunctionTypeDelete(FuncType);
...\param Type the function type context to describe the host function signature. \param WrapFunc the wrapper function pointer. The wrapper function signature must be as follows:
typedef WasmEdge_Result (*WasmEdge_WrapFunc_t)(
void *This,
void *Data,
WasmEdge_CallingFrameContext *FrameCxt,
const WasmEdge_Value *Params,
const uint32_t ParamLen,
WasmEdge_Value *Returns,
const uint32_t ReturnLen);The This pointer is the same as the Binding parameter of this function.
The Params array has a length guaranteed to be the same as the parameter
types in the Type, and the ParamLen is the length of the array. The
Returns array has a length guaranteed to be the same as the result types
in the Type, and the ReturnLen is the length of the array. The return
value is WasmEdge_Result for the execution status.
\param Binding the this pointer of the host function target or a function
index maintained by the caller to identify the host function. When invoking
the host function, this pointer will be passed as the first argument to the
wrapper function.
\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.