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

Function callWasm

test/api/APIAOTNestedVMTest.cpp:135–174  ·  view source on GitHub ↗

Call specified function from embedded wasm

Source from the content-addressed store, hash-verified

133thread_local std::stack<WasmEdge_ExecutorContext *> ExecutorsStack;
134/// Call specified function from embedded wasm
135void callWasm(const char *FnName) {
136 Ptr<WasmEdge_ASTModuleContext> Module;
137 TRY(WasmEdge_LoaderParseFromFile)
138 (Ptr{TRY(WasmEdge_LoaderCreate)(nullptr)}, Module, PathNestedWasmCompiled);
139 TRY(WasmEdge_ValidatorValidate)
140 (Ptr{TRY(WasmEdge_ValidatorCreate)(nullptr)}, Module);
141
142 static auto Executor = Ptr{TRY(WasmEdge_ExecutorCreate)(nullptr, nullptr)};
143 auto Store = Ptr{TRY(WasmEdge_StoreCreate)()};
144 auto Host = Ptr{TRY(WasmEdge_ModuleInstanceCreate)(StringView{"env"})};
145 WasmEdge_ModuleInstanceAddFunction(
146 Host, StringView{"hostFnCheck"},
147 WasmEdge_FunctionInstanceCreate(
148 Ptr{TRY(WasmEdge_FunctionTypeCreate)(nullptr, 0, nullptr, 0)},
149 [](void *, const WasmEdge_CallingFrameContext *Frame,
150 const WasmEdge_Value *, WasmEdge_Value *) {
151 hostFnCheck(Frame);
152 return WasmEdge_Result_Success;
153 },
154 nullptr, 0));
155 WasmEdge_ModuleInstanceAddFunction(
156 Host, StringView{"hostFnOverwrite"},
157 WasmEdge_FunctionInstanceCreate(
158 Ptr{TRY(WasmEdge_FunctionTypeCreate)(nullptr, 0, nullptr, 0)},
159 [](void *, const WasmEdge_CallingFrameContext *,
160 const WasmEdge_Value *, WasmEdge_Value *) {
161 hostFnOverwrite();
162 return WasmEdge_Result_Success;
163 },
164 nullptr, 0));
165 TRY(WasmEdge_ExecutorRegisterImport)(Executor, Store, Host);
166
167 Ptr<WasmEdge_ModuleInstanceContext> Instance;
168 TRY(WasmEdge_ExecutorInstantiate)(Executor, Instance, Store, Module);
169 auto *WasmFn =
170 TRY(WasmEdge_ModuleInstanceFindFunction)(Instance, StringView{FnName});
171 ExecutorsStack.push(Executor);
172 TRY(WasmEdge_ExecutorInvoke)(Executor, WasmFn, nullptr, 0, nullptr, 0);
173 ExecutorsStack.pop();
174}
175
176size_t CalledHostFnCheck = 0;
177void hostFnCheck(const WasmEdge_CallingFrameContext *Frame) {

Callers 2

hostFnOverwriteFunction · 0.85
TESTFunction · 0.85

Calls 6

hostFnCheckFunction · 0.85
hostFnOverwriteFunction · 0.85
pushMethod · 0.80
popMethod · 0.80

Tested by

no test coverage detected