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