@@ -212,11 +212,11 @@ using HostFuncDataPtr = std::unique_ptr<HostFuncData>;
212212
213213struct HostModuleData {
214214 HostModuleData (const std::string_view modname) {
215- cxt_ = WasmEdge_ImportObjectCreate (WasmEdge_StringWrap (modname.data (), modname.length ()));
215+ cxt_ = WasmEdge_ModuleInstanceCreate (WasmEdge_StringWrap (modname.data (), modname.length ()));
216216 }
217- ~HostModuleData () { WasmEdge_ImportObjectDelete (cxt_); }
217+ ~HostModuleData () { WasmEdge_ModuleInstanceDelete (cxt_); }
218218
219- WasmEdge_ImportObjectContext *cxt_;
219+ WasmEdge_ModuleInstanceContext *cxt_;
220220};
221221
222222using HostModuleDataPtr = std::unique_ptr<HostModuleData>;
@@ -228,6 +228,7 @@ class WasmEdge : public WasmVm {
228228 validator_ = WasmEdge_ValidatorCreate (nullptr );
229229 executor_ = WasmEdge_ExecutorCreate (nullptr , nullptr );
230230 store_ = nullptr ;
231+ ast_module_ = nullptr ;
231232 module_ = nullptr ;
232233 memory_ = nullptr ;
233234 }
@@ -285,11 +286,12 @@ class WasmEdge : public WasmVm {
285286 WasmEdgeValidatorPtr validator_;
286287 WasmEdgeExecutorPtr executor_;
287288 WasmEdgeStorePtr store_;
288- WasmEdgeASTModulePtr module_;
289+ WasmEdgeASTModulePtr ast_module_;
290+ WasmEdgeModulePtr module_;
289291 WasmEdge_MemoryInstanceContext *memory_;
290292
291293 std::unordered_map<std::string, HostFuncDataPtr> host_functions_;
292- std::unordered_map<std::string, HostModuleDataPtr> import_objects_ ;
294+ std::unordered_map<std::string, HostModuleDataPtr> host_modules_ ;
293295 std::unordered_set<std::string> module_functions_;
294296};
295297
@@ -303,22 +305,25 @@ bool WasmEdge::load(std::string_view bytecode, std::string_view /*precompiled*/,
303305 }
304306 res = WasmEdge_ValidatorValidate (validator_.get (), mod);
305307 if (!WasmEdge_ResultOK (res)) {
308+ WasmEdge_ASTModuleDelete (mod);
306309 return false ;
307310 }
308- module_ = mod;
311+ ast_module_ = mod;
309312 return true ;
310313}
311314
312315bool WasmEdge::link (std::string_view /* debug_name*/ ) {
313- assert (module_ != nullptr );
316+ assert (ast_module_ != nullptr );
314317
315318 // Create store and register imports.
316- store_ = WasmEdge_StoreCreate ();
319+ if (store_ == nullptr ) {
320+ store_ = WasmEdge_StoreCreate ();
321+ }
317322 if (store_ == nullptr ) {
318323 return false ;
319324 }
320325 WasmEdge_Result res;
321- for (auto &&it : import_objects_ ) {
326+ for (auto &&it : host_modules_ ) {
322327 res = WasmEdge_ExecutorRegisterImport (executor_.get (), store_.get (), it.second ->cxt_ );
323328 if (!WasmEdge_ResultOK (res)) {
324329 fail (FailState::UnableToInitializeCode,
@@ -327,30 +332,33 @@ bool WasmEdge::link(std::string_view /*debug_name*/) {
327332 }
328333 }
329334 // Instantiate module.
330- res = WasmEdge_ExecutorInstantiate (executor_.get (), store_.get (), module_.get ());
335+ WasmEdge_ModuleInstanceContext *mod = nullptr ;
336+ res = WasmEdge_ExecutorInstantiate (executor_.get (), &mod, store_.get (), ast_module_.get ());
331337 if (!WasmEdge_ResultOK (res)) {
332338 fail (FailState::UnableToInitializeCode,
333339 std::string (" Failed to link Wasm module: " ) + std::string (WasmEdge_ResultGetMessage (res)));
334340 return false ;
335341 }
336342 // Get the function and memory exports.
337- uint32_t memory_num = WasmEdge_StoreListMemoryLength (store_. get () );
343+ uint32_t memory_num = WasmEdge_ModuleInstanceListMemoryLength (mod );
338344 if (memory_num > 0 ) {
339345 WasmEdge_String name;
340- WasmEdge_StoreListMemory (store_. get () , &name, 1 );
341- memory_ = WasmEdge_StoreFindMemory (store_. get () , name);
346+ WasmEdge_ModuleInstanceListMemory (mod , &name, 1 );
347+ memory_ = WasmEdge_ModuleInstanceFindMemory (mod , name);
342348 if (memory_ == nullptr ) {
349+ WasmEdge_ModuleInstanceDelete (mod);
343350 return false ;
344351 }
345352 }
346- uint32_t func_num = WasmEdge_StoreListFunctionLength (store_. get () );
353+ uint32_t func_num = WasmEdge_ModuleInstanceListFunctionLength (mod );
347354 if (func_num > 0 ) {
348355 std::vector<WasmEdge_String> names (func_num);
349- WasmEdge_StoreListFunction (store_. get () , &names[0 ], func_num);
356+ WasmEdge_ModuleInstanceListFunction (mod , &names[0 ], func_num);
350357 for (auto i = 0 ; i < func_num; i++) {
351358 module_functions_.insert (std::string (names[i].Buf , names[i].Length ));
352359 }
353360 }
361+ module_ = mod;
354362 return true ;
355363}
356364
@@ -398,10 +406,10 @@ bool WasmEdge::setWord(uint64_t pointer, Word word) {
398406template <typename ... Args>
399407void WasmEdge::registerHostFunctionImpl (std::string_view module_name,
400408 std::string_view function_name, void (*function)(Args...)) {
401- auto it = import_objects_ .find (std::string (module_name));
402- if (it == import_objects_ .end ()) {
403- import_objects_ .emplace (module_name, std::make_unique<HostModuleData>(module_name));
404- it = import_objects_ .find (std::string (module_name));
409+ auto it = host_modules_ .find (std::string (module_name));
410+ if (it == host_modules_ .end ()) {
411+ host_modules_ .emplace (module_name, std::make_unique<HostModuleData>(module_name));
412+ it = host_modules_ .find (std::string (module_name));
405413 }
406414
407415 auto data = std::make_unique<HostFuncData>(module_name, function_name);
@@ -435,7 +443,7 @@ void WasmEdge::registerHostFunctionImpl(std::string_view module_name,
435443 return ;
436444 }
437445
438- WasmEdge_ImportObjectAddFunction (
446+ WasmEdge_ModuleInstanceAddFunction (
439447 it->second ->cxt_ , WasmEdge_StringWrap (function_name.data (), function_name.length ()),
440448 hostfunc_cxt);
441449 host_functions_.insert_or_assign (std::string (module_name) + " ." + std::string (function_name),
@@ -445,10 +453,10 @@ void WasmEdge::registerHostFunctionImpl(std::string_view module_name,
445453template <typename R, typename ... Args>
446454void WasmEdge::registerHostFunctionImpl (std::string_view module_name,
447455 std::string_view function_name, R (*function)(Args...)) {
448- auto it = import_objects_ .find (std::string (module_name));
449- if (it == import_objects_ .end ()) {
450- import_objects_ .emplace (module_name, std::make_unique<HostModuleData>(module_name));
451- it = import_objects_ .find (std::string (module_name));
456+ auto it = host_modules_ .find (std::string (module_name));
457+ if (it == host_modules_ .end ()) {
458+ host_modules_ .emplace (module_name, std::make_unique<HostModuleData>(module_name));
459+ it = host_modules_ .find (std::string (module_name));
452460 }
453461
454462 auto data = std::make_unique<HostFuncData>(module_name, function_name);
@@ -482,7 +490,7 @@ void WasmEdge::registerHostFunctionImpl(std::string_view module_name,
482490 return ;
483491 }
484492
485- WasmEdge_ImportObjectAddFunction (
493+ WasmEdge_ModuleInstanceAddFunction (
486494 it->second ->cxt_ , WasmEdge_StringWrap (function_name.data (), function_name.length ()),
487495 hostfunc_cxt);
488496 host_functions_.insert_or_assign (std::string (module_name) + " ." + std::string (function_name),
@@ -492,8 +500,8 @@ void WasmEdge::registerHostFunctionImpl(std::string_view module_name,
492500template <typename ... Args>
493501void WasmEdge::getModuleFunctionImpl (std::string_view function_name,
494502 std::function<void (ContextBase *, Args...)> *function) {
495- auto *func_cxt = WasmEdge_StoreFindFunction (
496- store_ .get (), WasmEdge_StringWrap (function_name.data (), function_name.length ()));
503+ auto *func_cxt = WasmEdge_ModuleInstanceFindFunction (
504+ module_ .get (), WasmEdge_StringWrap (function_name.data (), function_name.length ()));
497505 if (!func_cxt) {
498506 *function = nullptr ;
499507 return ;
@@ -521,7 +529,7 @@ void WasmEdge::getModuleFunctionImpl(std::string_view function_name,
521529 return ;
522530 }
523531
524- *function = [function_name, this ](ContextBase *context, Args... args) -> void {
532+ *function = [function_name, func_cxt, this ](ContextBase *context, Args... args) -> void {
525533 WasmEdge_Value params[] = {makeVal (args)...};
526534 const bool log = cmpLogLevel (LogLevel::trace);
527535 if (log) {
@@ -530,9 +538,7 @@ void WasmEdge::getModuleFunctionImpl(std::string_view function_name,
530538 }
531539 SaveRestoreContext saved_context (context);
532540 WasmEdge_Result res =
533- WasmEdge_ExecutorInvoke (executor_.get (), store_.get (),
534- WasmEdge_StringWrap (function_name.data (), function_name.length ()),
535- params, sizeof ...(Args), nullptr , 0 );
541+ WasmEdge_ExecutorInvoke (executor_.get (), func_cxt, params, sizeof ...(Args), nullptr , 0 );
536542 if (!WasmEdge_ResultOK (res)) {
537543 fail (FailState::RuntimeError, " Function: " + std::string (function_name) + " failed:\n " +
538544 WasmEdge_ResultGetMessage (res));
@@ -547,8 +553,8 @@ void WasmEdge::getModuleFunctionImpl(std::string_view function_name,
547553template <typename R, typename ... Args>
548554void WasmEdge::getModuleFunctionImpl (std::string_view function_name,
549555 std::function<R(ContextBase *, Args...)> *function) {
550- auto *func_cxt = WasmEdge_StoreFindFunction (
551- store_ .get (), WasmEdge_StringWrap (function_name.data (), function_name.length ()));
556+ auto *func_cxt = WasmEdge_ModuleInstanceFindFunction (
557+ module_ .get (), WasmEdge_StringWrap (function_name.data (), function_name.length ()));
552558 if (!func_cxt) {
553559 *function = nullptr ;
554560 return ;
@@ -576,7 +582,7 @@ void WasmEdge::getModuleFunctionImpl(std::string_view function_name,
576582 return ;
577583 }
578584
579- *function = [function_name, this ](ContextBase *context, Args... args) -> R {
585+ *function = [function_name, func_cxt, this ](ContextBase *context, Args... args) -> R {
580586 WasmEdge_Value params[] = {makeVal (args)...};
581587 WasmEdge_Value results[1 ];
582588 const bool log = cmpLogLevel (LogLevel::trace);
@@ -586,9 +592,7 @@ void WasmEdge::getModuleFunctionImpl(std::string_view function_name,
586592 }
587593 SaveRestoreContext saved_context (context);
588594 WasmEdge_Result res =
589- WasmEdge_ExecutorInvoke (executor_.get (), store_.get (),
590- WasmEdge_StringWrap (function_name.data (), function_name.length ()),
591- params, sizeof ...(Args), results, 1 );
595+ WasmEdge_ExecutorInvoke (executor_.get (), func_cxt, params, sizeof ...(Args), results, 1 );
592596 if (!WasmEdge_ResultOK (res)) {
593597 fail (FailState::RuntimeError, " Function: " + std::string (function_name) + " failed:\n " +
594598 WasmEdge_ResultGetMessage (res));
0 commit comments