/src/WasmEdge/lib/loader/serialize/serial_module.cpp
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | #include "loader/serialize.h" |
5 | | |
6 | | #include <algorithm> |
7 | | #include <variant> |
8 | | |
9 | | namespace WasmEdge { |
10 | | namespace Loader { |
11 | | |
12 | | // Serialize module. See "include/loader/serialize.h". |
13 | | Expect<std::vector<uint8_t>> |
14 | 0 | Serializer::serializeModule(const AST::Module &Mod) const noexcept { |
15 | 0 | std::vector<uint8_t> OutVec; |
16 | 0 | OutVec.reserve(Mod.getMagic().size() + Mod.getVersion().size()); |
17 | | // Serialize Magic and Version. |
18 | 0 | OutVec.insert(OutVec.end(), Mod.getMagic().begin(), Mod.getMagic().end()); |
19 | 0 | OutVec.insert(OutVec.end(), Mod.getVersion().begin(), Mod.getVersion().end()); |
20 | |
|
21 | 0 | using SecVariant = |
22 | 0 | std::variant<const AST::TypeSection *, const AST::ImportSection *, |
23 | 0 | const AST::FunctionSection *, const AST::TableSection *, |
24 | 0 | const AST::MemorySection *, const AST::TagSection *, |
25 | 0 | const AST::GlobalSection *, const AST::ExportSection *, |
26 | 0 | const AST::StartSection *, const AST::ElementSection *, |
27 | 0 | const AST::DataCountSection *, const AST::CodeSection *, |
28 | 0 | const AST::DataSection *>; |
29 | | |
30 | | // The known sections, in the order the binary format requires, which is the |
31 | | // order Loader::loadModule enforces. |
32 | 0 | const SecVariant Ordered[] = { |
33 | 0 | &Mod.getTypeSection(), &Mod.getImportSection(), |
34 | 0 | &Mod.getFunctionSection(), &Mod.getTableSection(), |
35 | 0 | &Mod.getMemorySection(), &Mod.getTagSection(), |
36 | 0 | &Mod.getGlobalSection(), &Mod.getExportSection(), |
37 | 0 | &Mod.getStartSection(), &Mod.getElementSection(), |
38 | 0 | &Mod.getDataCountSection(), &Mod.getCodeSection(), |
39 | 0 | &Mod.getDataSection()}; |
40 | | |
41 | | // Custom sections may appear anywhere, so they are merged into that skeleton |
42 | | // by offset, which keeps their position for a module read by the loader. |
43 | 0 | std::vector<const AST::CustomSection *> Customs; |
44 | 0 | Customs.reserve(Mod.getCustomSections().size()); |
45 | 0 | for (const auto &Custom : Mod.getCustomSections()) { |
46 | 0 | Customs.push_back(&Custom); |
47 | 0 | } |
48 | 0 | std::stable_sort(Customs.begin(), Customs.end(), |
49 | 0 | [](const auto *A, const auto *B) { |
50 | 0 | return A->getStartOffset() < B->getStartOffset(); |
51 | 0 | }); |
52 | |
|
53 | 0 | auto It = Customs.begin(); |
54 | 0 | const auto End = Customs.end(); |
55 | 0 | for (const auto &Sec : Ordered) { |
56 | 0 | const uint64_t Offset = |
57 | 0 | std::visit([](const auto *S) { return S->getStartOffset(); }, Sec);Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_0::operator()<WasmEdge::AST::TypeSection>(WasmEdge::AST::TypeSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_0::operator()<WasmEdge::AST::ImportSection>(WasmEdge::AST::ImportSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_0::operator()<WasmEdge::AST::FunctionSection>(WasmEdge::AST::FunctionSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_0::operator()<WasmEdge::AST::TableSection>(WasmEdge::AST::TableSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_0::operator()<WasmEdge::AST::MemorySection>(WasmEdge::AST::MemorySection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_0::operator()<WasmEdge::AST::TagSection>(WasmEdge::AST::TagSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_0::operator()<WasmEdge::AST::GlobalSection>(WasmEdge::AST::GlobalSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_0::operator()<WasmEdge::AST::ExportSection>(WasmEdge::AST::ExportSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_0::operator()<WasmEdge::AST::StartSection>(WasmEdge::AST::StartSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_0::operator()<WasmEdge::AST::ElementSection>(WasmEdge::AST::ElementSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_0::operator()<WasmEdge::AST::DataCountSection>(WasmEdge::AST::DataCountSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_0::operator()<WasmEdge::AST::CodeSection>(WasmEdge::AST::CodeSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_0::operator()<WasmEdge::AST::DataSection>(WasmEdge::AST::DataSection const*) const |
58 | 0 | for (; It != End && (*It)->getStartOffset() < Offset; ++It) { |
59 | 0 | serializeSection(**It, OutVec); |
60 | 0 | } |
61 | 0 | std::visit([&](const auto *S) { serializeSection(*S, OutVec); }, Sec);Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_1::operator()<WasmEdge::AST::TypeSection>(WasmEdge::AST::TypeSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_1::operator()<WasmEdge::AST::ImportSection>(WasmEdge::AST::ImportSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_1::operator()<WasmEdge::AST::FunctionSection>(WasmEdge::AST::FunctionSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_1::operator()<WasmEdge::AST::TableSection>(WasmEdge::AST::TableSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_1::operator()<WasmEdge::AST::MemorySection>(WasmEdge::AST::MemorySection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_1::operator()<WasmEdge::AST::TagSection>(WasmEdge::AST::TagSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_1::operator()<WasmEdge::AST::GlobalSection>(WasmEdge::AST::GlobalSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_1::operator()<WasmEdge::AST::ExportSection>(WasmEdge::AST::ExportSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_1::operator()<WasmEdge::AST::StartSection>(WasmEdge::AST::StartSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_1::operator()<WasmEdge::AST::ElementSection>(WasmEdge::AST::ElementSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_1::operator()<WasmEdge::AST::DataCountSection>(WasmEdge::AST::DataCountSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_1::operator()<WasmEdge::AST::CodeSection>(WasmEdge::AST::CodeSection const*) const Unexecuted instantiation: serial_module.cpp:auto WasmEdge::Loader::Serializer::serializeModule(WasmEdge::AST::Module const&) const::$_1::operator()<WasmEdge::AST::DataSection>(WasmEdge::AST::DataSection const*) const |
62 | 0 | } |
63 | 0 | for (; It != End; ++It) { |
64 | 0 | serializeSection(**It, OutVec); |
65 | 0 | } |
66 | |
|
67 | 0 | return OutVec; |
68 | 0 | } |
69 | | |
70 | | } // namespace Loader |
71 | | } // namespace WasmEdge |