/src/WasmEdge/lib/loader/ast/component/component_section.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 | | // Load component core:module section. See "include/loader/loader.h". |
10 | 5.14k | Expect<void> Loader::loadSection(AST::Component::CoreModuleSection &Sec) { |
11 | 5.14k | return loadSectionContent(Sec, [this, &Sec]() -> Expect<void> { |
12 | 5.11k | auto ReportError = [](auto E) { |
13 | 70 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_Sec_CoreMod)); |
14 | 70 | return E; |
15 | 70 | }; |
16 | 5.11k | auto ExpectedSize = Sec.getContentSize(); |
17 | 5.11k | auto StartOffset = FMgr.getOffset(); |
18 | | |
19 | 5.11k | EXPECTED_TRY(auto Preamble, Loader::loadPreamble().map_error(ReportError)); |
20 | 5.09k | auto &[WasmMagic, Ver] = Preamble; |
21 | 5.09k | if (unlikely(Ver != ModuleVersion)) { |
22 | 6 | return logLoadError(ErrCode::Value::MalformedVersion, |
23 | 6 | FMgr.getLastOffset(), ASTNodeAttr::Comp_Sec_CoreMod); |
24 | 6 | } |
25 | 5.08k | AST::Module &CoreMod = Sec.getContent(); |
26 | 5.08k | CoreMod.getMagic() = WasmMagic; |
27 | 5.08k | CoreMod.getVersion() = Ver; |
28 | | |
29 | 5.08k | auto Offset = FMgr.getOffset(); |
30 | 5.08k | if (unlikely(ExpectedSize < Offset - StartOffset)) { |
31 | 2 | return logLoadError(ErrCode::Value::UnexpectedEnd, FMgr.getLastOffset(), |
32 | 2 | ASTNodeAttr::Comp_Sec_CoreMod); |
33 | 2 | } |
34 | | |
35 | 5.08k | EXPECTED_TRY(loadModule(CoreMod, ExpectedSize - (Offset - StartOffset)) |
36 | 5.03k | .map_error(ReportError)); |
37 | 5.03k | return {}; |
38 | 5.08k | }); |
39 | 5.14k | } |
40 | | |
41 | | // Load component core:instance section. See "include/loader/loader.h". |
42 | 7.37k | Expect<void> Loader::loadSection(AST::Component::CoreInstanceSection &Sec) { |
43 | 7.37k | return loadSectionContent(Sec, [this, &Sec]() { |
44 | 7.34k | return loadSectionContentVec( |
45 | 52.6k | Sec, [this](AST::Component::CoreInstance &Instance) { |
46 | 52.6k | return loadCoreInstance(Instance); |
47 | 52.6k | }); |
48 | 7.34k | }); |
49 | 7.37k | } |
50 | | |
51 | | // Load component core:type section. See "include/loader/loader.h". |
52 | 320k | Expect<void> Loader::loadSection(AST::Component::CoreTypeSection &Sec) { |
53 | 320k | return loadSectionContent(Sec, [this, &Sec]() { |
54 | 320k | return loadSectionContentVec( |
55 | 320k | Sec, [this](AST::Component::CoreDefType &Ty) { return loadType(Ty); }); |
56 | 320k | }); |
57 | 320k | } |
58 | | |
59 | | // Load component nested-component section. See "include/loader/loader.h". |
60 | 5.29k | Expect<void> Loader::loadSection(AST::Component::ComponentSection &Sec) { |
61 | 5.29k | return loadSectionContent(Sec, [this, &Sec]() -> Expect<void> { |
62 | 5.26k | auto ReportError = [](auto E) { |
63 | 951 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_Sec_Component)); |
64 | 951 | return E; |
65 | 951 | }; |
66 | 5.26k | auto ExpectedSize = Sec.getContentSize(); |
67 | 5.26k | auto StartOffset = FMgr.getOffset(); |
68 | | |
69 | 5.26k | EXPECTED_TRY(auto Preamble, loadPreamble().map_error(ReportError)); |
70 | 5.24k | auto &[WasmMagic, Ver] = Preamble; |
71 | 5.24k | if (unlikely(Ver != ComponentVersion)) { |
72 | 7 | return logLoadError(ErrCode::Value::MalformedVersion, |
73 | 7 | FMgr.getLastOffset(), |
74 | 7 | ASTNodeAttr::Comp_Sec_Component); |
75 | 7 | } |
76 | 5.23k | auto NestedComp = std::make_unique<AST::Component::Component>(); |
77 | 5.23k | NestedComp->getMagic() = WasmMagic; |
78 | 5.23k | NestedComp->getVersion() = {Ver[0], Ver[1]}; |
79 | 5.23k | NestedComp->getLayer() = {Ver[2], Ver[3]}; |
80 | | |
81 | 5.23k | auto Offset = FMgr.getOffset(); |
82 | 5.23k | if (unlikely(ExpectedSize < Offset - StartOffset)) { |
83 | 3 | return logLoadError(ErrCode::Value::UnexpectedEnd, FMgr.getLastOffset(), |
84 | 3 | ASTNodeAttr::Component); |
85 | 3 | } |
86 | | |
87 | 5.23k | EXPECTED_TRY( |
88 | 4.30k | loadComponent(*NestedComp, ExpectedSize - (Offset - StartOffset)) |
89 | 4.30k | .map_error(ReportError)); |
90 | 4.30k | Sec.getContent() = std::move(NestedComp); |
91 | 4.30k | return {}; |
92 | 5.23k | }); |
93 | 5.29k | } |
94 | | |
95 | | // Load component instance section. See "include/loader/loader.h". |
96 | 85.3k | Expect<void> Loader::loadSection(AST::Component::InstanceSection &Sec) { |
97 | 85.3k | return loadSectionContent(Sec, [this, &Sec]() { |
98 | 85.2k | return loadSectionContentVec(Sec, |
99 | 85.2k | [this](AST::Component::Instance &Instance) { |
100 | 30.2k | return loadInstance(Instance); |
101 | 30.2k | }); |
102 | 85.2k | }); |
103 | 85.3k | } |
104 | | |
105 | | // Load component alias section. See "include/loader/loader.h". |
106 | 6.67k | Expect<void> Loader::loadSection(AST::Component::AliasSection &Sec) { |
107 | 6.67k | return loadSectionContent(Sec, [this, &Sec]() { |
108 | 6.66k | return loadSectionContentVec( |
109 | 7.29k | Sec, [this](AST::Component::Alias &Alias) { return loadAlias(Alias); }); |
110 | 6.66k | }); |
111 | 6.67k | } |
112 | | |
113 | | // Load component type section. See "include/loader/loader.h". |
114 | 1.21M | Expect<void> Loader::loadSection(AST::Component::TypeSection &Sec) { |
115 | 1.21M | return loadSectionContent(Sec, [this, &Sec]() { |
116 | 1.21M | return loadSectionContentVec( |
117 | 1.24M | Sec, [this](AST::Component::DefType &Ty) { return loadType(Ty); }); |
118 | 1.21M | }); |
119 | 1.21M | } |
120 | | |
121 | | // Load component cannon section. See "include/loader/loader.h". |
122 | 26.8k | Expect<void> Loader::loadSection(AST::Component::CanonSection &Sec) { |
123 | 26.8k | return loadSectionContent(Sec, [this, &Sec]() { |
124 | 26.8k | return loadSectionContentVec( |
125 | 26.8k | Sec, [this](AST::Component::Canonical &C) { return loadCanonical(C); }); |
126 | 26.8k | }); |
127 | 26.8k | } |
128 | | |
129 | | // Load component start section. See "include/loader/loader.h". |
130 | 1.63k | Expect<void> Loader::loadSection(AST::Component::StartSection &Sec) { |
131 | 1.63k | return loadSectionContent(Sec, [this, &Sec]() -> Expect<void> { |
132 | 1.60k | return loadStart(Sec.getContent()).map_error([](auto E) { |
133 | 45 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_Sec_Start)); |
134 | 45 | return E; |
135 | 45 | }); |
136 | 1.60k | }); |
137 | 1.63k | } |
138 | | |
139 | | // Load component import section. See "include/loader/loader.h". |
140 | 17.0k | Expect<void> Loader::loadSection(AST::Component::ImportSection &Sec) { |
141 | 17.0k | return loadSectionContent(Sec, [this, &Sec]() { |
142 | 17.0k | return loadSectionContentVec( |
143 | 17.0k | Sec, [this](AST::Component::Import &C) { return loadImport(C); }); |
144 | 17.0k | }); |
145 | 17.0k | } |
146 | | |
147 | | // Load component export section. See "include/loader/loader.h". |
148 | 5.68k | Expect<void> Loader::loadSection(AST::Component::ExportSection &Sec) { |
149 | 5.68k | return loadSectionContent(Sec, [this, &Sec]() { |
150 | 5.66k | return loadSectionContentVec( |
151 | 5.66k | Sec, [this](AST::Component::Export &C) { return loadExport(C); }); |
152 | 5.66k | }); |
153 | 5.68k | } |
154 | | |
155 | | // TODO: COMPONENT - Load component value section. |
156 | | |
157 | | } // namespace Loader |
158 | | } // namespace WasmEdge |