/src/WasmEdge/lib/loader/ast/component/component_instance.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: 2019-2024 Second State INC |
3 | | |
4 | | #include "loader/loader.h" |
5 | | |
6 | | namespace WasmEdge { |
7 | | namespace Loader { |
8 | | |
9 | | Expect<void> |
10 | 0 | Loader::loadCoreInstance(AST::Component::CoreInstanceExpr &InstanceExpr) { |
11 | 0 | auto ReportError = [this](auto E) { |
12 | 0 | return logLoadError(E, FMgr.getLastOffset(), |
13 | 0 | ASTNodeAttr::Comp_CoreInstance); |
14 | 0 | }; |
15 | | // core:instance ::= ie:<core:instanceexpr> |
16 | | // => (instance ie) |
17 | | // core:instanceexpr ::= 0x00 m:<moduleidx> arg*:vec(<core:instantiatearg>) |
18 | | // => (instantiate m arg*) |
19 | | // | 0x01 e*:vec(<core:inlineexport>) |
20 | | // => e* |
21 | |
|
22 | 0 | EXPECTED_TRY(uint8_t Flag, FMgr.readByte().map_error(ReportError)); |
23 | 0 | switch (Flag) { |
24 | 0 | case 0x00: { |
25 | 0 | EXPECTED_TRY(uint32_t Idx, FMgr.readU32().map_error(ReportError)); |
26 | 0 | std::vector<AST::Component::CoreInstantiateArg> Args; |
27 | 0 | EXPECTED_TRY(loadVec<AST::Component::CoreInstanceExpr>( |
28 | 0 | Args, [this](AST::Component::CoreInstantiateArg &Arg) { |
29 | 0 | return loadCoreInstantiateArg(Arg); |
30 | 0 | })); |
31 | 0 | InstanceExpr.emplace<AST::Component::CoreInstantiate>(Idx, std::move(Args)); |
32 | 0 | return {}; |
33 | 0 | } |
34 | 0 | case 0x01: { |
35 | 0 | std::vector<AST::Component::InlineExportImpl<AST::Component::CoreSort>> |
36 | 0 | Exports; |
37 | 0 | EXPECTED_TRY(loadVec<AST::Component::CoreInstanceExpr>( |
38 | 0 | Exports, |
39 | 0 | [this]( |
40 | 0 | AST::Component::InlineExportImpl<AST::Component::CoreSort> &Arg) { |
41 | 0 | return loadCoreInlineExport(Arg); |
42 | 0 | })); |
43 | 0 | InstanceExpr.emplace<AST::Component::CoreInlineExports>(std::move(Exports)); |
44 | 0 | return {}; |
45 | 0 | } |
46 | 0 | default: |
47 | 0 | return logLoadError(ErrCode::Value::MalformedCoreInstance, |
48 | 0 | FMgr.getLastOffset(), ASTNodeAttr::Comp_CoreInstance); |
49 | 0 | } |
50 | 0 | } |
51 | | |
52 | | Expect<void> |
53 | 0 | Loader::loadCoreInstantiateArg(AST::Component::CoreInstantiateArg &Arg) { |
54 | 0 | auto ReportError = [this](auto E) { |
55 | 0 | return logLoadError(E, FMgr.getLastOffset(), |
56 | 0 | ASTNodeAttr::Comp_CoreInstanceArg); |
57 | 0 | }; |
58 | | // core:instantiatearg ::= n:<core:name> 0x12 i:<instanceidx> |
59 | | // => (with n (instance i)) |
60 | |
|
61 | 0 | EXPECTED_TRY(Arg.getName(), FMgr.readName().map_error(ReportError)); |
62 | 0 | EXPECTED_TRY(uint8_t B, FMgr.readByte().map_error(ReportError)); |
63 | 0 | if (B != 0x12U) { |
64 | 0 | return logLoadError(ErrCode::Value::MalformedCoreInstance, |
65 | 0 | FMgr.getLastOffset(), |
66 | 0 | ASTNodeAttr::Comp_CoreInstanceArg); |
67 | 0 | } |
68 | 0 | EXPECTED_TRY(Arg.getIndex(), FMgr.readU32().map_error(ReportError)); |
69 | 0 | return {}; |
70 | 0 | } |
71 | | |
72 | | Expect<void> Loader::loadCoreInlineExport( |
73 | 0 | AST::Component::InlineExportImpl<AST::Component::CoreSort> &Exp) { |
74 | | // core:inlineexport ::= n:<core:name> si:<core:sortidx> => (export n si) |
75 | 0 | EXPECTED_TRY(Exp.getName(), FMgr.readName().map_error([this](auto E) { |
76 | 0 | return logLoadError(E, FMgr.getLastOffset(), |
77 | 0 | ASTNodeAttr::Comp_CoreInlineExport); |
78 | 0 | })); |
79 | 0 | return loadCoreSortIndex(Exp.getSortIdx()).map_error([](auto E) { |
80 | 0 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_CoreInlineExport)); |
81 | 0 | return E; |
82 | 0 | }); |
83 | 0 | } |
84 | | |
85 | 0 | Expect<void> Loader::loadInstance(AST::Component::InstanceExpr &InstanceExpr) { |
86 | 0 | auto ReportError = [this](auto E) { |
87 | 0 | return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Comp_Instance); |
88 | 0 | }; |
89 | | // instance ::= ie:<instanceexpr> |
90 | | // => (instance ie) |
91 | | // instanceexpr ::= 0x00 c:<componentidx> arg*:vec(<instantiatearg>) |
92 | | // => (instantiate c arg*) |
93 | | // | 0x01 e*:vec(<inlineexport>) |
94 | | // => e* |
95 | |
|
96 | 0 | EXPECTED_TRY(uint8_t Flag, FMgr.readByte().map_error(ReportError)); |
97 | 0 | switch (Flag) { |
98 | 0 | case 0x00: { |
99 | 0 | EXPECTED_TRY(uint32_t Idx, FMgr.readU32().map_error(ReportError)); |
100 | 0 | std::vector<AST::Component::InstantiateArg> Args; |
101 | 0 | EXPECTED_TRY(loadVec<AST::Component::InstanceExpr>( |
102 | 0 | Args, [this](AST::Component::InstantiateArg &Arg) -> Expect<void> { |
103 | 0 | return loadInstantiateArg(Arg); |
104 | 0 | })); |
105 | 0 | InstanceExpr.emplace<AST::Component::Instantiate>( |
106 | 0 | AST::Component::Instantiate(Idx, std::move(Args))); |
107 | 0 | return {}; |
108 | 0 | } |
109 | 0 | case 0x01: { |
110 | 0 | std::vector<AST::Component::InlineExportImpl<AST::Component::Sort>> Exports; |
111 | 0 | EXPECTED_TRY(loadVec<AST::Component::InstanceExpr>( |
112 | 0 | Exports, |
113 | 0 | [this](AST::Component::InlineExportImpl<AST::Component::Sort> &Arg) { |
114 | 0 | return loadInlineExport(Arg); |
115 | 0 | })); |
116 | 0 | InstanceExpr.emplace<AST::Component::InlineExports>(std::move(Exports)); |
117 | 0 | return {}; |
118 | 0 | } |
119 | 0 | default: |
120 | 0 | return logLoadError(ErrCode::Value::MalformedInstance, FMgr.getLastOffset(), |
121 | 0 | ASTNodeAttr::Comp_Instance); |
122 | 0 | } |
123 | 0 | } |
124 | | |
125 | 0 | Expect<void> Loader::loadInstantiateArg(AST::Component::InstantiateArg &Arg) { |
126 | | // instantiatearg ::= n:<string> si:<sortidx> => (with n si) |
127 | 0 | EXPECTED_TRY(Arg.getName(), FMgr.readName().map_error([this](auto E) { |
128 | 0 | return logLoadError(E, FMgr.getLastOffset(), |
129 | 0 | ASTNodeAttr::Comp_InlineExport); |
130 | 0 | })); |
131 | 0 | return loadSortIndex(Arg.getIndex()).map_error([](auto E) { |
132 | 0 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_InlineExport)); |
133 | 0 | return E; |
134 | 0 | }); |
135 | 0 | } |
136 | | |
137 | | Expect<void> Loader::loadInlineExport( |
138 | 0 | AST::Component::InlineExportImpl<AST::Component::Sort> &Exp) { |
139 | | // inlineexport ::= n:<exportname> si:<sortidx> => (export n si) |
140 | 0 | EXPECTED_TRY(Exp.getName(), FMgr.readName().map_error([this](auto E) { |
141 | 0 | return logLoadError(E, FMgr.getLastOffset(), |
142 | 0 | ASTNodeAttr::Comp_InlineExport); |
143 | 0 | })); |
144 | 0 | return loadSortIndex(Exp.getSortIdx()).map_error([](auto E) { |
145 | 0 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_InlineExport)); |
146 | 0 | return E; |
147 | 0 | }); |
148 | 0 | } |
149 | | |
150 | | } // namespace Loader |
151 | | } // namespace WasmEdge |