Serialize module. See "include/loader/serialize.h".
| 10 | |
| 11 | // Serialize module. See "include/loader/serialize.h". |
| 12 | Expect<std::vector<uint8_t>> |
| 13 | Serializer::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 |
nothing calls this directly
no test coverage detected