MCPcopy Create free account
hub / github.com/WasmEdge/WasmEdge / loadUnit

Method loadUnit

lib/loader/loader.cpp:187–232  ·  view source on GitHub ↗

Load module or component unit. See "include/loader/loader.h".

Source from the content-addressed store, hash-verified

185
186// Load module or component unit. See "include/loader/loader.h".
187Expect<std::variant<std::unique_ptr<AST::Component::Component>,
188 std::unique_ptr<AST::Module>>>
189Loader::loadUnit() {
190 EXPECTED_TRY(auto Preamble, loadPreamble());
191 auto &[WasmMagic, Ver] = Preamble;
192 if (Ver == ModuleVersion) {
193 auto Mod = std::make_unique<AST::Module>();
194 Mod->getMagic() = WasmMagic;
195 Mod->getVersion() = Ver;
196 if (Conf.getRuntimeConfigure().getRunMode() == RunMode::AOT) {
197 EXPECTED_TRY(loadModuleAOT(Mod->getAOTSection()));
198 }
199 // Seek to the position after the binary header.
200 FMgr.seek(8);
201 EXPECTED_TRY(loadModule(*Mod));
202
203 // Load library from AOT Section for the universal WASM case only when the
204 // configured run mode is AOT.
205 if (Conf.getRuntimeConfigure().getRunMode() == RunMode::AOT) {
206 if (WASMType == InputType::UniversalWASM) {
207 EXPECTED_TRY(loadUniversalWASM(*Mod));
208 } else if (WASMType == InputType::WASM) {
209 // AOT requested on a plain .wasm with no AOT artifact.
210 spdlog::warn("AOT was requested but the input has no AOT artifact, "
211 "falling back to interpreter."sv);
212 }
213 }
214 return Mod;
215 } else if (Ver == ComponentVersion) {
216 if (!Conf.hasProposal(Proposal::Component)) {
217 return logNeedProposal(ErrCode::Value::MalformedVersion,
218 Proposal::Component, FMgr.getLastOffset(),
219 ASTNodeAttr::Component);
220 }
221 spdlog::warn("component model is an experimental proposal"sv);
222 auto Comp = std::make_unique<AST::Component::Component>();
223 Comp->getMagic() = WasmMagic;
224 Comp->getVersion() = {Ver[0], Ver[1]};
225 Comp->getLayer() = {Ver[2], Ver[3]};
226 EXPECTED_TRY(loadComponent(*Comp));
227 return Comp;
228 } else {
229 return logLoadError(ErrCode::Value::MalformedVersion, FMgr.getLastOffset(),
230 ASTNodeAttr::Component);
231 }
232}
233
234// Serialize module into byte code. See "include/loader/loader.h".
235Expect<std::vector<Byte>> Loader::serializeModule(const AST::Module &Mod) {

Callers

nothing calls this directly

Calls 7

loadModuleFunction · 0.85
getRunModeMethod · 0.80
hasProposalMethod · 0.80
getLastOffsetMethod · 0.80
EXPECTED_TRYFunction · 0.50
getVersionMethod · 0.45
seekMethod · 0.45

Tested by

no test coverage detected