srctree

Robin Linden parent 93d3851d 1c9daf19
wasm: Add to_string(ModuleParseError)

inlinesplit
wasm/byte_code_parser.h added: 61, removed: 2, total 59
@@ -9,6 +9,7 @@
 
#include <iosfwd>
#include <optional>
#include <string_view>
 
namespace wasm {
 
@@ -29,6 +30,40 @@ enum class ModuleParseError {
UnhandledSection,
};
 
constexpr std::string_view to_string(ModuleParseError e) {
switch (e) {
case ModuleParseError::UnexpectedEof:
return "Unexpected end of file";
case ModuleParseError::InvalidMagic:
return "Invalid magic number";
case ModuleParseError::UnsupportedVersion:
return "Unsupported version";
case ModuleParseError::InvalidSectionId:
return "Invalid section id";
case ModuleParseError::InvalidSize:
return "Invalid section size";
case ModuleParseError::InvalidTypeSection:
return "Invalid type section";
case ModuleParseError::InvalidFunctionSection:
return "Invalid function section";
case ModuleParseError::InvalidTableSection:
return "Invalid table section";
case ModuleParseError::InvalidMemorySection:
return "Invalid memory section";
case ModuleParseError::InvalidGlobalSection:
return "Invalid global section";
case ModuleParseError::InvalidExportSection:
return "Invalid export section";
case ModuleParseError::InvalidStartSection:
return "Invalid start section";
case ModuleParseError::InvalidCodeSection:
return "Invalid code section";
case ModuleParseError::UnhandledSection:
return "Unhandled section";
}
return "Unknown error";
}
 
class ByteCodeParser {
public:
static tl::expected<Module, ModuleParseError> parse_module(std::istream &);
 
wasm/byte_code_parser_test.cpp added: 61, removed: 2, total 59
@@ -56,6 +56,29 @@ std::stringstream make_module_bytes(SectionId id, std::vector<std::uint8_t> cons
return wasm_bytes;
}
 
void parse_error_to_string_tests() {
using wasm::ModuleParseError;
etest::test("to_string(ModuleParseError)", [] {
expect_eq(wasm::to_string(ModuleParseError::UnexpectedEof), "Unexpected end of file");
expect_eq(wasm::to_string(ModuleParseError::InvalidMagic), "Invalid magic number");
expect_eq(wasm::to_string(ModuleParseError::UnsupportedVersion), "Unsupported version");
expect_eq(wasm::to_string(ModuleParseError::InvalidSectionId), "Invalid section id");
expect_eq(wasm::to_string(ModuleParseError::InvalidSize), "Invalid section size");
expect_eq(wasm::to_string(ModuleParseError::InvalidTypeSection), "Invalid type section");
expect_eq(wasm::to_string(ModuleParseError::InvalidFunctionSection), "Invalid function section");
expect_eq(wasm::to_string(ModuleParseError::InvalidTableSection), "Invalid table section");
expect_eq(wasm::to_string(ModuleParseError::InvalidMemorySection), "Invalid memory section");
expect_eq(wasm::to_string(ModuleParseError::InvalidGlobalSection), "Invalid global section");
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::UnhandledSection), "Unhandled section");
 
auto last_error_value = static_cast<int>(ModuleParseError::UnhandledSection);
expect_eq(wasm::to_string(static_cast<ModuleParseError>(last_error_value + 1)), "Unknown error");
});
}
 
void export_section_tests() {
etest::test("export section, missing export count", [] {
auto module = ByteCodeParser::parse_module(make_module_bytes(SectionId::Export, {}));
@@ -553,6 +576,7 @@ int main() {
tl::unexpected{wasm::ModuleParseError::UnhandledSection});
});
 
parse_error_to_string_tests();
type_section_tests();
function_section_tests();
table_section_tests();