srctree

Robin Lindén parent 1c3b1073 c96a8bc8
wasm: Implement parsing the data count section

inlinesplit
wasm/byte_code_parser.cpp added: 40, removed: 6, total 34
@@ -550,6 +550,17 @@ tl::expected<Module, ModuleParseError> ByteCodeParser::parse_module(std::istream
return tl::unexpected{ModuleParseError::InvalidCodeSection};
}
break;
case SectionId::DataCount: {
auto count = Leb128<std::uint32_t>::decode_from(is);
if (!count) {
return tl::unexpected{ModuleParseError::InvalidDataCountSection};
}
 
module.data_count_section = DataCountSection{
.count = *count,
};
break;
}
default:
std::cerr << "Unhandled section: " << static_cast<int>(id) << '\n';
// Uncomment if you want to skip past unhandled sections for e.g. debugging.
 
wasm/byte_code_parser.h added: 40, removed: 6, total 34
@@ -29,6 +29,7 @@ enum class ModuleParseError {
InvalidExportSection,
InvalidStartSection,
InvalidCodeSection,
InvalidDataCountSection,
UnhandledSection,
};
 
@@ -64,6 +65,8 @@ constexpr std::string_view to_string(ModuleParseError e) {
return "Invalid start section";
case ModuleParseError::InvalidCodeSection:
return "Invalid code section";
case ModuleParseError::InvalidDataCountSection:
return "Invalid data count section";
case ModuleParseError::UnhandledSection:
return "Unhandled section";
}
 
wasm/byte_code_parser_test.cpp added: 40, removed: 6, total 34
@@ -74,6 +74,7 @@ void parse_error_to_string_tests() {
expect_eq(wasm::to_string(ModuleParseError::InvalidExportSection), "Invalid export section");
expect_eq(wasm::to_string(ModuleParseError::InvalidStartSection), "Invalid start section");
expect_eq(wasm::to_string(ModuleParseError::InvalidCodeSection), "Invalid code section");
expect_eq(wasm::to_string(ModuleParseError::InvalidDataCountSection), "Invalid data count section");
expect_eq(wasm::to_string(ModuleParseError::UnhandledSection), "Unhandled section");
 
auto last_error_value = static_cast<int>(ModuleParseError::UnhandledSection);
@@ -661,6 +662,18 @@ void code_section_tests() {
});
}
 
void data_count_tests() {
etest::test("data count section, 42", [] {
auto module = ByteCodeParser::parse_module(make_module_bytes(SectionId::DataCount, {42})).value();
expect_eq(module.data_count_section, wasm::DataCountSection{.count = 42});
});
 
etest::test("data count section, bad count", [] {
auto module = ByteCodeParser::parse_module(make_module_bytes(SectionId::DataCount, {0x80}));
expect_eq(module, tl::unexpected{wasm::ModuleParseError::InvalidDataCountSection});
});
}
 
} // namespace
 
int main() {
@@ -700,7 +713,7 @@ int main() {
});
 
etest::test("unhandled section", [] {
expect_eq(ByteCodeParser::parse_module(make_module_bytes(SectionId::DataCount, {})),
expect_eq(ByteCodeParser::parse_module(make_module_bytes(SectionId::Element, {})),
tl::unexpected{wasm::ModuleParseError::UnhandledSection});
});
 
@@ -715,6 +728,7 @@ int main() {
export_section_tests();
start_section_tests();
code_section_tests();
data_count_tests();
 
return etest::run_all_tests();
}
 
wasm/wasm.h added: 40, removed: 6, total 34
@@ -139,6 +139,12 @@ struct CodeSection {
[[nodiscard]] bool operator==(CodeSection const &) const = default;
};
 
struct DataCountSection {
std::uint32_t count{};
 
[[nodiscard]] bool operator==(DataCountSection const &) const = default;
};
 
// https://webassembly.github.io/spec/core/syntax/modules.html
struct Module {
std::vector<CustomSection> custom_sections{};
@@ -154,7 +160,7 @@ struct Module {
// TODO(robinlinden): element_section
std::optional<CodeSection> code_section{};
// TODO(robinlinden): data_section
// TODO(robinlinden): data_count_section
std::optional<DataCountSection> data_count_section{};
 
[[nodiscard]] bool operator==(Module const &) const = default;
};