@@ -212,11 +212,11 @@ using HostFuncDataPtr = std::unique_ptr<HostFuncData>;
212
212
213
213
struct HostModuleData {
214
214
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 ()));
216
216
}
217
- ~HostModuleData () { WasmEdge_ImportObjectDelete (cxt_); }
217
+ ~HostModuleData () { WasmEdge_ModuleInstanceDelete (cxt_); }
218
218
219
- WasmEdge_ImportObjectContext *cxt_;
219
+ WasmEdge_ModuleInstanceContext *cxt_;
220
220
};
221
221
222
222
using HostModuleDataPtr = std::unique_ptr<HostModuleData>;
@@ -228,6 +228,7 @@ class WasmEdge : public WasmVm {
228
228
validator_ = WasmEdge_ValidatorCreate (nullptr );
229
229
executor_ = WasmEdge_ExecutorCreate (nullptr , nullptr );
230
230
store_ = nullptr ;
231
+ ast_module_ = nullptr ;
231
232
module_ = nullptr ;
232
233
memory_ = nullptr ;
233
234
}
@@ -285,11 +286,12 @@ class WasmEdge : public WasmVm {
285
286
WasmEdgeValidatorPtr validator_;
286
287
WasmEdgeExecutorPtr executor_;
287
288
WasmEdgeStorePtr store_;
288
- WasmEdgeASTModulePtr module_;
289
+ WasmEdgeASTModulePtr ast_module_;
290
+ WasmEdgeModulePtr module_;
289
291
WasmEdge_MemoryInstanceContext *memory_;
290
292
291
293
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_ ;
293
295
std::unordered_set<std::string> module_functions_;
294
296
};
295
297
@@ -303,22 +305,25 @@ bool WasmEdge::load(std::string_view bytecode, std::string_view /*precompiled*/,
303
305
}
304
306
res = WasmEdge_ValidatorValidate (validator_.get (), mod);
305
307
if (!WasmEdge_ResultOK (res)) {
308
+ WasmEdge_ASTModuleDelete (mod);
306
309
return false ;
307
310
}
308
- module_ = mod;
311
+ ast_module_ = mod;
309
312
return true ;
310
313
}
311
314
312
315
bool WasmEdge::link (std::string_view /* debug_name*/ ) {
313
- assert (module_ != nullptr );
316
+ assert (ast_module_ != nullptr );
314
317
315
318
// Create store and register imports.
316
- store_ = WasmEdge_StoreCreate ();
319
+ if (store_ == nullptr ) {
320
+ store_ = WasmEdge_StoreCreate ();
321
+ }
317
322
if (store_ == nullptr ) {
318
323
return false ;
319
324
}
320
325
WasmEdge_Result res;
321
- for (auto &&it : import_objects_ ) {
326
+ for (auto &&it : host_modules_ ) {
322
327
res = WasmEdge_ExecutorRegisterImport (executor_.get (), store_.get (), it.second ->cxt_ );
323
328
if (!WasmEdge_ResultOK (res)) {
324
329
fail (FailState::UnableToInitializeCode,
@@ -327,30 +332,33 @@ bool WasmEdge::link(std::string_view /*debug_name*/) {
327
332
}
328
333
}
329
334
// 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 ());
331
337
if (!WasmEdge_ResultOK (res)) {
332
338
fail (FailState::UnableToInitializeCode,
333
339
std::string (" Failed to link Wasm module: " ) + std::string (WasmEdge_ResultGetMessage (res)));
334
340
return false ;
335
341
}
336
342
// Get the function and memory exports.
337
- uint32_t memory_num = WasmEdge_StoreListMemoryLength (store_. get () );
343
+ uint32_t memory_num = WasmEdge_ModuleInstanceListMemoryLength (mod );
338
344
if (memory_num > 0 ) {
339
345
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);
342
348
if (memory_ == nullptr ) {
349
+ WasmEdge_ModuleInstanceDelete (mod);
343
350
return false ;
344
351
}
345
352
}
346
- uint32_t func_num = WasmEdge_StoreListFunctionLength (store_. get () );
353
+ uint32_t func_num = WasmEdge_ModuleInstanceListFunctionLength (mod );
347
354
if (func_num > 0 ) {
348
355
std::vector<WasmEdge_String> names (func_num);
349
- WasmEdge_StoreListFunction (store_. get () , &names[0 ], func_num);
356
+ WasmEdge_ModuleInstanceListFunction (mod , &names[0 ], func_num);
350
357
for (auto i = 0 ; i < func_num; i++) {
351
358
module_functions_.insert (std::string (names[i].Buf , names[i].Length ));
352
359
}
353
360
}
361
+ module_ = mod;
354
362
return true ;
355
363
}
356
364
@@ -398,10 +406,10 @@ bool WasmEdge::setWord(uint64_t pointer, Word word) {
398
406
template <typename ... Args>
399
407
void WasmEdge::registerHostFunctionImpl (std::string_view module_name,
400
408
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));
405
413
}
406
414
407
415
auto data = std::make_unique<HostFuncData>(module_name, function_name);
@@ -435,7 +443,7 @@ void WasmEdge::registerHostFunctionImpl(std::string_view module_name,
435
443
return ;
436
444
}
437
445
438
- WasmEdge_ImportObjectAddFunction (
446
+ WasmEdge_ModuleInstanceAddFunction (
439
447
it->second ->cxt_ , WasmEdge_StringWrap (function_name.data (), function_name.length ()),
440
448
hostfunc_cxt);
441
449
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,
445
453
template <typename R, typename ... Args>
446
454
void WasmEdge::registerHostFunctionImpl (std::string_view module_name,
447
455
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));
452
460
}
453
461
454
462
auto data = std::make_unique<HostFuncData>(module_name, function_name);
@@ -482,7 +490,7 @@ void WasmEdge::registerHostFunctionImpl(std::string_view module_name,
482
490
return ;
483
491
}
484
492
485
- WasmEdge_ImportObjectAddFunction (
493
+ WasmEdge_ModuleInstanceAddFunction (
486
494
it->second ->cxt_ , WasmEdge_StringWrap (function_name.data (), function_name.length ()),
487
495
hostfunc_cxt);
488
496
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,
492
500
template <typename ... Args>
493
501
void WasmEdge::getModuleFunctionImpl (std::string_view function_name,
494
502
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 ()));
497
505
if (!func_cxt) {
498
506
*function = nullptr ;
499
507
return ;
@@ -521,7 +529,7 @@ void WasmEdge::getModuleFunctionImpl(std::string_view function_name,
521
529
return ;
522
530
}
523
531
524
- *function = [function_name, this ](ContextBase *context, Args... args) -> void {
532
+ *function = [function_name, func_cxt, this ](ContextBase *context, Args... args) -> void {
525
533
WasmEdge_Value params[] = {makeVal (args)...};
526
534
const bool log = cmpLogLevel (LogLevel::trace);
527
535
if (log ) {
@@ -530,9 +538,7 @@ void WasmEdge::getModuleFunctionImpl(std::string_view function_name,
530
538
}
531
539
SaveRestoreContext saved_context (context);
532
540
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 );
536
542
if (!WasmEdge_ResultOK (res)) {
537
543
fail (FailState::RuntimeError, " Function: " + std::string (function_name) + " failed:\n " +
538
544
WasmEdge_ResultGetMessage (res));
@@ -547,8 +553,8 @@ void WasmEdge::getModuleFunctionImpl(std::string_view function_name,
547
553
template <typename R, typename ... Args>
548
554
void WasmEdge::getModuleFunctionImpl (std::string_view function_name,
549
555
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 ()));
552
558
if (!func_cxt) {
553
559
*function = nullptr ;
554
560
return ;
@@ -576,7 +582,7 @@ void WasmEdge::getModuleFunctionImpl(std::string_view function_name,
576
582
return ;
577
583
}
578
584
579
- *function = [function_name, this ](ContextBase *context, Args... args) -> R {
585
+ *function = [function_name, func_cxt, this ](ContextBase *context, Args... args) -> R {
580
586
WasmEdge_Value params[] = {makeVal (args)...};
581
587
WasmEdge_Value results[1 ];
582
588
const bool log = cmpLogLevel (LogLevel::trace);
@@ -586,9 +592,7 @@ void WasmEdge::getModuleFunctionImpl(std::string_view function_name,
586
592
}
587
593
SaveRestoreContext saved_context (context);
588
594
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 );
592
596
if (!WasmEdge_ResultOK (res)) {
593
597
fail (FailState::RuntimeError, " Function: " + std::string (function_name) + " failed:\n " +
594
598
WasmEdge_ResultGetMessage (res));
0 commit comments