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