Function wasmedge_sys::ffi::WasmEdge_FunctionInstanceCreateBinding
source · 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_FunctionInstanceContext
Expand description
Creation of the WasmEdge_FunctionInstanceContext for host functions.
This function is for the languages which 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 into a WasmEdge_ModuleInstanceContext
.
The following is an example to create 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 of host function to return 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 of `This` in the host language ...
int32_t Result = ...;
Out[0] = Result;
// Return the 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_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 following:
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
is the pointer the same as the Binding
parameter of this
function. The Params
is the input parameters array with length guaranteed
to be the same as the parameter types in the Type
, and the ParamLen
is
the length of the array. The Returns
is the output results array with
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 the
function indexing maintained by the caller which can specify the host
function. When invoking the host function, this pointer will be the first
argument of 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 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.