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