MCPcopy Create free account
hub / github.com/WasmEdge/WasmEdge / main

Function main

examples/capi/host_functions/parse_json/host_function.c:23–64  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21}
22
23int main() {
24 /* Create the VM context. */
25 WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
26
27 /* Create the import object. */
28 WasmEdge_String ExportName = WasmEdge_StringCreateByCString("extern");
29 WasmEdge_ModuleInstanceContext *ImpObj =
30 WasmEdge_ModuleInstanceCreate(ExportName);
31 WasmEdge_ValType ParamList[1] = {WasmEdge_ValTypeGenExternRef()};
32 WasmEdge_ValType ReturnList[1] = {WasmEdge_ValTypeGenExternRef()};
33 WasmEdge_FunctionTypeContext *HostFType =
34 WasmEdge_FunctionTypeCreate(ParamList, 1, ReturnList, 1);
35 WasmEdge_FunctionInstanceContext *HostFunc =
36 WasmEdge_FunctionInstanceCreate(HostFType, parseJson, NULL, 0);
37 WasmEdge_FunctionTypeDelete(HostFType);
38 WasmEdge_String HostFuncName =
39 WasmEdge_StringCreateByCString("func-parse-json");
40 WasmEdge_ModuleInstanceAddFunction(ImpObj, HostFuncName, HostFunc);
41 WasmEdge_StringDelete(HostFuncName);
42
43 WasmEdge_VMRegisterModuleFromImport(VMCxt, ImpObj);
44
45 /* The parameters and returns arrays. */
46 char *Key = "testValue";
47 WasmEdge_Value Params[1] = {WasmEdge_ValueGenExternRef(Key)};
48 WasmEdge_Value Returns[1];
49 WasmEdge_String FuncName = WasmEdge_StringCreateByCString("parseJson");
50 WasmEdge_Result Res = WasmEdge_VMRunWasmFromFile(
51 VMCxt, "parse-json.wasm", FuncName, Params, 1, Returns, 1);
52 if (WasmEdge_ResultOK(Res)) {
53 printf("Got the result: %s\n",
54 (char *)WasmEdge_ValueGetExternRef(Returns[0]));
55 } else {
56 printf("Error message: %s\n", WasmEdge_ResultGetMessage(Res));
57 }
58
59 /* Resources deallocations. */
60 WasmEdge_VMDelete(VMCxt);
61 WasmEdge_StringDelete(FuncName);
62 WasmEdge_ModuleInstanceDelete(ImpObj);
63 return 0;
64}

Callers

nothing calls this directly

Calls 12

WasmEdge_VMCreateFunction · 0.85
WasmEdge_StringDeleteFunction · 0.85
WasmEdge_ResultOKFunction · 0.85
WasmEdge_VMDeleteFunction · 0.85

Tested by

no test coverage detected