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

Method instantiate

lib/executor/instantiate/component/component_instance.cpp:16–80  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14using namespace std::literals;
15
16Expect<void>
17Executor::instantiate(Runtime::Instance::ComponentInstance &CompInst,
18 const AST::Component::CoreInstanceSection &CoreInstSec) {
19 // Instantiate the core module instance with the imports and add the instances
20 // into the component model index space.
21 for (const auto &Expr : CoreInstSec.getContent()) {
22 if (Expr.isInstantiateModule()) {
23 // Instantiate-with-arguments case.
24 // Create an import manager to implement the isolation of imports.
25 Runtime::Instance::ComponentImportManager ImportMgr;
26 for (const auto &Arg : Expr.getInstantiateArgs()) {
27 ImportMgr.exportCoreModuleInstance(
28 Arg.getName(), CompInst.getCoreModuleInstance(Arg.getIndex()));
29 }
30 const AST::Module &Mod = CompInst.getModule(Expr.getModuleIndex());
31 EXPECTED_TRY(auto NewModInst, instantiate(ImportMgr, Mod));
32 CompInst.addCoreModuleInstance(std::move(NewModInst));
33 } else {
34 // Inline exports case.
35 // Create a core module instance with the exports.
36 auto Mod = std::make_unique<Runtime::Instance::ModuleInstance>("");
37 uint32_t ExpIdx[4] = {0, 0, 0, 0};
38
39 for (const auto &Exp : Expr.getInlineExports()) {
40 const auto &SortIdx = Exp.getSortIdx();
41 const uint32_t Idx = SortIdx.getIdx();
42 switch (SortIdx.getSort().getCoreSortType()) {
43 case AST::Component::Sort::CoreSortType::Func:
44 Mod->importFunction(CompInst.getCoreFunction(Idx));
45 Mod->exportFunction(Exp.getName(), ExpIdx[0]);
46 ExpIdx[0]++;
47 break;
48 case AST::Component::Sort::CoreSortType::Table:
49 Mod->importTable(CompInst.getCoreTable(Idx));
50 Mod->exportTable(Exp.getName(), ExpIdx[1]);
51 ExpIdx[1]++;
52 break;
53 case AST::Component::Sort::CoreSortType::Memory:
54 Mod->importMemory(CompInst.getCoreMemory(Idx));
55 Mod->exportMemory(Exp.getName(), ExpIdx[2]);
56 ExpIdx[2]++;
57 break;
58 case AST::Component::Sort::CoreSortType::Global:
59 Mod->importGlobal(CompInst.getCoreGlobal(Idx));
60 Mod->exportGlobal(Exp.getName(), ExpIdx[3]);
61 ExpIdx[3]++;
62 break;
63 case AST::Component::Sort::CoreSortType::Type:
64 case AST::Component::Sort::CoreSortType::Module:
65 case AST::Component::Sort::CoreSortType::Instance:
66 spdlog::error(ErrCode::Value::CoreInvalidExport);
67 spdlog::error(" A module instance cannot exports types, modules,"sv
68 " or instances"sv);
69 return Unexpect(ErrCode::Value::CoreInvalidExport);
70 default:
71 assumingUnreachable();
72 }
73 }

Callers

nothing calls this directly

Calls 15

UnexpectFunction · 0.85
getCoreModuleInstanceMethod · 0.80
getModuleIndexMethod · 0.80
addCoreModuleInstanceMethod · 0.80
getCoreSortTypeMethod · 0.80
importFunctionMethod · 0.80
getCoreFunctionMethod · 0.80
importTableMethod · 0.80
getCoreTableMethod · 0.80
exportTableMethod · 0.80
importMemoryMethod · 0.80
getCoreMemoryMethod · 0.80

Tested by

no test coverage detected