/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 | 73.9k | Expect<void> Loader::loadCoreInstance(AST::Component::CoreInstance &Instance) { |
10 | 73.9k | auto ReportError = [this](auto E) { |
11 | 24 | return logLoadError(E, FMgr.getLastOffset(), |
12 | 24 | ASTNodeAttr::Comp_CoreInstance); |
13 | 24 | }; |
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 | 73.9k | auto LoadInstArg = |
22 | 73.9k | [this](AST::Component::InstantiateArg<uint32_t> &Arg) -> Expect<void> { |
23 | 416 | auto ReportArgError = [this](auto E) { |
24 | 62 | return logLoadError(E, FMgr.getLastOffset(), |
25 | 62 | ASTNodeAttr::Comp_CoreInstanceArg); |
26 | 62 | }; 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 | 34 | auto ReportArgError = [this](auto E) { | 24 | 34 | return logLoadError(E, FMgr.getLastOffset(), | 25 | 34 | ASTNodeAttr::Comp_CoreInstanceArg); | 26 | 34 | }; |
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 | 28 | auto ReportArgError = [this](auto E) { | 24 | 28 | return logLoadError(E, FMgr.getLastOffset(), | 25 | 28 | ASTNodeAttr::Comp_CoreInstanceArg); | 26 | 28 | }; |
|
27 | | // core:instantiatearg ::= n:<core:name> 0x12 i:<instanceidx> |
28 | | // => (with n (instance i)) |
29 | 416 | EXPECTED_TRY(Arg.getName(), FMgr.readName().map_error(ReportArgError)); |
30 | 388 | EXPECTED_TRY(uint8_t B, FMgr.readByte().map_error(ReportArgError)); |
31 | 384 | if (B != 0x12U) { |
32 | 28 | return ReportArgError(ErrCode::Value::MalformedCoreInstance); |
33 | 28 | } |
34 | 356 | EXPECTED_TRY(Arg.getIndex(), FMgr.readU32().map_error(ReportArgError)); |
35 | 354 | return {}; |
36 | 356 | }; |
37 | | |
38 | 73.9k | auto LoadInlineExp = |
39 | 73.9k | [this](AST::Component::InlineExport &Exp) -> Expect<void> { |
40 | | // core:inlineexport ::= n:<core:name> si:<core:sortidx> => (export n si) |
41 | 5.76k | EXPECTED_TRY(Exp.getName(), FMgr.readName().map_error([this](auto E) { |
42 | 5.72k | return logLoadError(E, FMgr.getLastOffset(), |
43 | 5.72k | ASTNodeAttr::Comp_CoreInlineExport); |
44 | 5.72k | })); |
45 | 5.72k | return loadSortIndex(Exp.getSortIdx(), true).map_error([](auto E) { |
46 | 41 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_CoreInlineExport)); |
47 | 41 | return E; |
48 | 41 | }); |
49 | 5.76k | }; |
50 | | |
51 | 73.9k | EXPECTED_TRY(uint8_t Flag, FMgr.readByte().map_error(ReportError)); |
52 | 73.9k | switch (Flag) { |
53 | 56.6k | case 0x00: { |
54 | 56.6k | EXPECTED_TRY(uint32_t Idx, FMgr.readU32().map_error(ReportError)); |
55 | 56.6k | std::vector<AST::Component::InstantiateArg<uint32_t>> Args; |
56 | 56.6k | EXPECTED_TRY(loadVec<AST::Component::CoreInstance>(Args, LoadInstArg)); |
57 | 56.6k | Instance.setInstantiateArgs(Idx, std::move(Args)); |
58 | 56.6k | return {}; |
59 | 56.6k | } |
60 | 17.2k | case 0x01: { |
61 | 17.2k | std::vector<AST::Component::InlineExport> Exports; |
62 | 17.2k | EXPECTED_TRY(loadVec<AST::Component::CoreInstance>(Exports, LoadInlineExp)); |
63 | 17.0k | Instance.setInlineExports(std::move(Exports)); |
64 | 17.0k | return {}; |
65 | 17.2k | } |
66 | 19 | default: |
67 | 19 | return logLoadError(ErrCode::Value::MalformedCoreInstance, |
68 | 19 | FMgr.getLastOffset(), ASTNodeAttr::Comp_CoreInstance); |
69 | 73.9k | } |
70 | 73.9k | } |
71 | | |
72 | 68.3k | Expect<void> Loader::loadInstance(AST::Component::Instance &Instance) { |
73 | 68.3k | auto ReportError = [this](auto E) { |
74 | 21 | return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Comp_Instance); |
75 | 21 | }; |
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 | 68.3k | auto LoadInstArg = |
84 | 68.3k | [this](AST::Component::InstantiateArg<AST::Component::SortIndex> &Arg) |
85 | 68.3k | -> Expect<void> { |
86 | | // instantiatearg ::= n:<string> si:<sortidx> => (with n si) |
87 | 6.78k | EXPECTED_TRY(Arg.getName(), FMgr.readName().map_error([this](auto E) { |
88 | 6.75k | return logLoadError(E, FMgr.getLastOffset(), |
89 | 6.75k | ASTNodeAttr::Comp_InstanceArg); |
90 | 6.75k | })); |
91 | 6.75k | return loadSortIndex(Arg.getIndex()).map_error([](auto E) { |
92 | 39 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_InstanceArg)); |
93 | 39 | return E; |
94 | 39 | }); |
95 | 6.78k | }; |
96 | | |
97 | 68.3k | auto LoadInlineExp = |
98 | 68.3k | [this](AST::Component::InlineExport &Exp) -> Expect<void> { |
99 | | // inlineexport ::= n:<exportname> si:<sortidx> => (export n si) |
100 | 2.89k | EXPECTED_TRY(loadExternName(Exp.getName()).map_error([this](auto E) { |
101 | 2.87k | return logLoadError(E, FMgr.getLastOffset(), |
102 | 2.87k | ASTNodeAttr::Comp_InlineExport); |
103 | 2.87k | })); |
104 | 2.87k | return loadSortIndex(Exp.getSortIdx()).map_error([](auto E) { |
105 | 9 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_InlineExport)); |
106 | 9 | return E; |
107 | 9 | }); |
108 | 2.89k | }; |
109 | | |
110 | 68.3k | EXPECTED_TRY(uint8_t Flag, FMgr.readByte().map_error(ReportError)); |
111 | 68.3k | switch (Flag) { |
112 | 5.27k | case 0x00: { |
113 | 5.27k | EXPECTED_TRY(uint32_t Idx, FMgr.readU32().map_error(ReportError)); |
114 | 5.26k | std::vector<AST::Component::InstantiateArg<AST::Component::SortIndex>> Args; |
115 | 5.26k | EXPECTED_TRY(loadVec<AST::Component::Instance>(Args, LoadInstArg)); |
116 | 5.17k | Instance.setInstantiateArgs(Idx, std::move(Args)); |
117 | 5.17k | return {}; |
118 | 5.26k | } |
119 | 63.0k | case 0x01: { |
120 | 63.0k | std::vector<AST::Component::InlineExport> Exports; |
121 | 63.0k | EXPECTED_TRY(loadVec<AST::Component::Instance>(Exports, LoadInlineExp)); |
122 | 62.9k | Instance.setInlineExports(std::move(Exports)); |
123 | 62.9k | return {}; |
124 | 63.0k | } |
125 | 26 | default: |
126 | 26 | return logLoadError(ErrCode::Value::MalformedInstance, FMgr.getLastOffset(), |
127 | 26 | ASTNodeAttr::Comp_Instance); |
128 | 68.3k | } |
129 | 68.3k | } |
130 | | |
131 | | } // namespace Loader |
132 | | } // namespace WasmEdge |