/src/WasmEdge/lib/loader/ast/component/component_instance.cpp
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | #include "loader/loader.h" |
5 | | |
6 | | namespace WasmEdge { |
7 | | namespace Loader { |
8 | | |
9 | 52.6k | Expect<void> Loader::loadCoreInstance(AST::Component::CoreInstance &Instance) { |
10 | 52.6k | auto ReportError = [this](auto E) { |
11 | 15 | return logLoadError(E, FMgr.getLastOffset(), |
12 | 15 | ASTNodeAttr::Comp_CoreInstance); |
13 | 15 | }; |
14 | | // core:instance ::= ie:<core:instanceexpr> |
15 | | // => (instance ie) |
16 | | // core:instanceexpr ::= 0x00 m:<moduleidx> arg*:vec(<core:instantiatearg>) |
17 | | // => (instantiate m arg*) |
18 | | // | 0x01 e*:vec(<core:inlineexport>) |
19 | | // => e* |
20 | | |
21 | 52.6k | auto LoadInstArg = |
22 | 52.6k | [this](AST::Component::InstantiateArg<uint32_t> &Arg) -> Expect<void> { |
23 | 199 | auto ReportArgError = [this](auto E) { |
24 | 50 | return logLoadError(E, FMgr.getLastOffset(), |
25 | 50 | ASTNodeAttr::Comp_CoreInstanceArg); |
26 | 50 | }; component_instance.cpp:auto WasmEdge::Loader::Loader::loadCoreInstance(WasmEdge::AST::Component::CoreInstance&)::$_0::operator()(WasmEdge::AST::Component::InstantiateArg<unsigned int>&) const::{lambda(auto:1)#1}::operator()<WasmEdge::ErrCode>(WasmEdge::ErrCode) constLine | Count | Source | 23 | 26 | auto ReportArgError = [this](auto E) { | 24 | 26 | return logLoadError(E, FMgr.getLastOffset(), | 25 | 26 | ASTNodeAttr::Comp_CoreInstanceArg); | 26 | 26 | }; |
component_instance.cpp:auto WasmEdge::Loader::Loader::loadCoreInstance(WasmEdge::AST::Component::CoreInstance&)::$_0::operator()(WasmEdge::AST::Component::InstantiateArg<unsigned int>&) const::{lambda(auto:1)#1}::operator()<WasmEdge::ErrCode::Value>(WasmEdge::ErrCode::Value) constLine | Count | Source | 23 | 24 | auto ReportArgError = [this](auto E) { | 24 | 24 | return logLoadError(E, FMgr.getLastOffset(), | 25 | 24 | ASTNodeAttr::Comp_CoreInstanceArg); | 26 | 24 | }; |
|
27 | | // core:instantiatearg ::= n:<core:name> 0x12 i:<instanceidx> |
28 | | // => (with n (instance i)) |
29 | 199 | EXPECTED_TRY(Arg.getName(), FMgr.readName().map_error(ReportArgError)); |
30 | 178 | EXPECTED_TRY(uint8_t B, FMgr.readByte().map_error(ReportArgError)); |
31 | 174 | if (B != 0x12U) { |
32 | 24 | return ReportArgError(ErrCode::Value::MalformedCoreInstance); |
33 | 24 | } |
34 | 150 | EXPECTED_TRY(Arg.getIndex(), FMgr.readU32().map_error(ReportArgError)); |
35 | 149 | return {}; |
36 | 150 | }; |
37 | | |
38 | 52.6k | auto LoadInlineExp = |
39 | 52.6k | [this](AST::Component::InlineExport &Exp) -> Expect<void> { |
40 | | // core:inlineexport ::= n:<core:name> si:<core:sortidx> => (export n si) |
41 | 5.29k | EXPECTED_TRY(Exp.getName(), FMgr.readName().map_error([this](auto E) { |
42 | 5.26k | return logLoadError(E, FMgr.getLastOffset(), |
43 | 5.26k | ASTNodeAttr::Comp_CoreInlineExport); |
44 | 5.26k | })); |
45 | 5.26k | return loadSortIndex(Exp.getSortIdx(), true).map_error([](auto E) { |
46 | 32 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_CoreInlineExport)); |
47 | 32 | return E; |
48 | 32 | }); |
49 | 5.29k | }; |
50 | | |
51 | 52.6k | EXPECTED_TRY(uint8_t Flag, FMgr.readByte().map_error(ReportError)); |
52 | 52.5k | switch (Flag) { |
53 | 46.0k | case 0x00: { |
54 | 46.0k | EXPECTED_TRY(uint32_t Idx, FMgr.readU32().map_error(ReportError)); |
55 | 46.0k | std::vector<AST::Component::InstantiateArg<uint32_t>> Args; |
56 | 46.0k | EXPECTED_TRY(loadVec<AST::Component::CoreInstance>(Args, LoadInstArg)); |
57 | 45.9k | Instance.setInstantiateArgs(Idx, std::move(Args)); |
58 | 45.9k | return {}; |
59 | 46.0k | } |
60 | 6.52k | case 0x01: { |
61 | 6.52k | std::vector<AST::Component::InlineExport> Exports; |
62 | 6.52k | EXPECTED_TRY(loadVec<AST::Component::CoreInstance>(Exports, LoadInlineExp)); |
63 | 6.44k | Instance.setInlineExports(std::move(Exports)); |
64 | 6.44k | return {}; |
65 | 6.52k | } |
66 | 21 | default: |
67 | 21 | return logLoadError(ErrCode::Value::MalformedCoreInstance, |
68 | 21 | FMgr.getLastOffset(), ASTNodeAttr::Comp_CoreInstance); |
69 | 52.5k | } |
70 | 52.5k | } |
71 | | |
72 | 30.2k | Expect<void> Loader::loadInstance(AST::Component::Instance &Instance) { |
73 | 30.2k | auto ReportError = [this](auto E) { |
74 | 19 | return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Comp_Instance); |
75 | 19 | }; |
76 | | // instance ::= ie:<instanceexpr> |
77 | | // => (instance ie) |
78 | | // instanceexpr ::= 0x00 c:<componentidx> arg*:vec(<instantiatearg>) |
79 | | // => (instantiate c arg*) |
80 | | // | 0x01 e*:vec(<inlineexport>) |
81 | | // => e* |
82 | | |
83 | 30.2k | auto LoadInstArg = |
84 | 30.2k | [this](AST::Component::InstantiateArg<AST::Component::SortIndex> &Arg) |
85 | 30.2k | -> Expect<void> { |
86 | | // instantiatearg ::= n:<string> si:<sortidx> => (with n si) |
87 | 6.02k | EXPECTED_TRY(Arg.getName(), FMgr.readName().map_error([this](auto E) { |
88 | 6.00k | return logLoadError(E, FMgr.getLastOffset(), |
89 | 6.00k | ASTNodeAttr::Comp_InstanceArg); |
90 | 6.00k | })); |
91 | 6.00k | return loadSortIndex(Arg.getIndex()).map_error([](auto E) { |
92 | 25 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_InstanceArg)); |
93 | 25 | return E; |
94 | 25 | }); |
95 | 6.02k | }; |
96 | | |
97 | 30.2k | auto LoadInlineExp = |
98 | 30.2k | [this](AST::Component::InlineExport &Exp) -> Expect<void> { |
99 | | // inlineexport ::= n:<exportname> si:<sortidx> => (export n si) |
100 | 2.81k | EXPECTED_TRY(loadExternName(Exp.getName()).map_error([this](auto E) { |
101 | 2.79k | return logLoadError(E, FMgr.getLastOffset(), |
102 | 2.79k | ASTNodeAttr::Comp_InlineExport); |
103 | 2.79k | })); |
104 | 2.79k | return loadSortIndex(Exp.getSortIdx()).map_error([](auto E) { |
105 | 7 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_InlineExport)); |
106 | 7 | return E; |
107 | 7 | }); |
108 | 2.81k | }; |
109 | | |
110 | 30.2k | EXPECTED_TRY(uint8_t Flag, FMgr.readByte().map_error(ReportError)); |
111 | 30.2k | switch (Flag) { |
112 | 4.67k | case 0x00: { |
113 | 4.67k | EXPECTED_TRY(uint32_t Idx, FMgr.readU32().map_error(ReportError)); |
114 | 4.67k | std::vector<AST::Component::InstantiateArg<AST::Component::SortIndex>> Args; |
115 | 4.67k | EXPECTED_TRY(loadVec<AST::Component::Instance>(Args, LoadInstArg)); |
116 | 4.60k | Instance.setInstantiateArgs(Idx, std::move(Args)); |
117 | 4.60k | return {}; |
118 | 4.67k | } |
119 | 25.5k | case 0x01: { |
120 | 25.5k | std::vector<AST::Component::InlineExport> Exports; |
121 | 25.5k | EXPECTED_TRY(loadVec<AST::Component::Instance>(Exports, LoadInlineExp)); |
122 | 25.5k | Instance.setInlineExports(std::move(Exports)); |
123 | 25.5k | return {}; |
124 | 25.5k | } |
125 | 16 | default: |
126 | 16 | return logLoadError(ErrCode::Value::MalformedInstance, FMgr.getLastOffset(), |
127 | 16 | ASTNodeAttr::Comp_Instance); |
128 | 30.2k | } |
129 | 30.2k | } |
130 | | |
131 | | } // namespace Loader |
132 | | } // namespace WasmEdge |