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