srctree

Robin Linden parent 07d23dc6 57c86f54
wasm: Move Global out of GlobalSection

Global needs to be easy to use in the ImportSection as well.

inlinesplit
wasm/byte_code_parser.cpp added: 25, removed: 25, total 0
@@ -118,7 +118,7 @@ std::optional<GlobalType> parse(std::istream &is) {
}
 
template<>
std::optional<GlobalSection::Global> parse(std::istream &is) {
std::optional<Global> parse(std::istream &is) {
auto type = parse<GlobalType>(is);
if (!type) {
return std::nullopt;
@@ -129,7 +129,7 @@ std::optional<GlobalSection::Global> parse(std::istream &is) {
return std::nullopt;
}
 
return GlobalSection::Global{
return Global{
.type = *std::move(type),
.init = *std::move(init),
};
@@ -314,7 +314,7 @@ std::optional<MemorySection> parse_memory_section(std::istream &is) {
}
 
std::optional<GlobalSection> parse_global_section(std::istream &is) {
if (auto maybe_globals = parse_vector<GlobalSection::Global>(is)) {
if (auto maybe_globals = parse_vector<Global>(is)) {
return GlobalSection{*std::move(maybe_globals)};
}
 
 
wasm/wasm.h added: 25, removed: 25, total 0
@@ -15,6 +15,26 @@
 
namespace wasm {
 
// https://webassembly.github.io/spec/core/binary/types.html#binary-globaltype
struct GlobalType {
enum class Mutability {
Const,
Var,
};
 
ValueType type{};
Mutability mutability{};
 
[[nodiscard]] bool operator==(GlobalType const &) const = default;
};
 
struct Global {
GlobalType type{};
std::vector<instructions::Instruction> init{};
 
[[nodiscard]] bool operator==(Global const &) const = default;
};
 
// https://webassembly.github.io/spec/core/binary/modules.html#type-section
struct TypeSection {
std::vector<FunctionType> types;
@@ -43,28 +63,8 @@ struct MemorySection {
[[nodiscard]] bool operator==(MemorySection const &) const = default;
};
 
// https://webassembly.github.io/spec/core/binary/types.html#binary-globaltype
struct GlobalType {
enum class Mutability {
Const,
Var,
};
 
ValueType type{};
Mutability mutability{};
 
[[nodiscard]] bool operator==(GlobalType const &) const = default;
};
 
// https://webassembly.github.io/spec/core/binary/modules.html#binary-globalsec
struct GlobalSection {
struct Global {
GlobalType type{};
std::vector<instructions::Instruction> init{};
 
[[nodiscard]] bool operator==(Global const &) const = default;
};
 
std::vector<Global> globals{};
 
[[nodiscard]] bool operator==(GlobalSection const &) const = default;