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

Method serializeModule

lib/loader/serialize/serial_module.cpp:12–63  ·  view source on GitHub ↗

Serialize module. See "include/loader/serialize.h".

Source from the content-addressed store, hash-verified

10
11// Serialize module. See "include/loader/serialize.h".
12Expect<std::vector<uint8_t>>
13Serializer::serializeModule(const AST::Module &Mod) const noexcept {
14 std::vector<uint8_t> OutVec;
15 OutVec.reserve(Mod.getMagic().size() + Mod.getVersion().size());
16 // Serialize Magic and Version.
17 OutVec.insert(OutVec.end(), Mod.getMagic().begin(), Mod.getMagic().end());
18 OutVec.insert(OutVec.end(), Mod.getVersion().begin(), Mod.getVersion().end());
19
20 // Sort sections according to start offset.
21 using SecVariant =
22 std::variant<const AST::CustomSection *, const AST::TypeSection *,
23 const AST::ImportSection *, const AST::FunctionSection *,
24 const AST::TableSection *, const AST::MemorySection *,
25 const AST::GlobalSection *, const AST::ExportSection *,
26 const AST::StartSection *, const AST::ElementSection *,
27 const AST::CodeSection *, const AST::DataSection *,
28 const AST::DataCountSection *, const AST::TagSection *>;
29 std::vector<SecVariant> Sections;
30 Sections.reserve(Mod.getCustomSections().size() + 13);
31 for (auto &CustomSec : Mod.getCustomSections()) {
32 Sections.push_back(&CustomSec);
33 }
34 Sections.push_back(&Mod.getTypeSection());
35 Sections.push_back(&Mod.getImportSection());
36 Sections.push_back(&Mod.getFunctionSection());
37 Sections.push_back(&Mod.getTableSection());
38 Sections.push_back(&Mod.getMemorySection());
39 Sections.push_back(&Mod.getGlobalSection());
40 Sections.push_back(&Mod.getExportSection());
41 Sections.push_back(&Mod.getStartSection());
42 Sections.push_back(&Mod.getElementSection());
43 Sections.push_back(&Mod.getCodeSection());
44 Sections.push_back(&Mod.getDataSection());
45 Sections.push_back(&Mod.getDataCountSection());
46 Sections.push_back(&Mod.getTagSection());
47 std::sort(Sections.begin(), Sections.end(), [&](auto &A, auto &B) {
48 auto Getter = [](auto &Sec) { return Sec->getStartOffset(); };
49 return std::visit(Getter, A) < std::visit(Getter, B);
50 });
51
52 // Serialize sections.
53 for (auto &Sec : Sections) {
54 auto SerVisit = [&OutVec, this](auto &A) -> Expect<void> {
55 return serializeSection(*A, OutVec);
56 };
57 EXPECTED_TRY(std::visit(SerVisit, Sec).map_error([](auto E) {
58 spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Module));
59 return E;
60 }));
61 }
62 return OutVec;
63}
64
65} // namespace Loader
66} // namespace WasmEdge

Callers

nothing calls this directly

Calls 10

InfoASTClass · 0.85
insertMethod · 0.80
getCustomSectionsMethod · 0.80
getStartOffsetMethod · 0.80
EXPECTED_TRYFunction · 0.50
errorFunction · 0.50
sizeMethod · 0.45
getVersionMethod · 0.45
endMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected