From e345d65def82b9eae7f05712a8731ca411c84335 Mon Sep 17 00:00:00 2001 From: Diego <96022404+dzfrias@users.noreply.github.com> Date: Sun, 26 May 2024 18:06:26 -0700 Subject: [PATCH] LibWasm: Use TRY macro when possible This removes a lot of the error handling boilerplate, and is more consistent with the rest of the codebase. --- Userland/Libraries/LibWasm/Parser/Parser.cpp | 394 ++++++------------- Userland/Libraries/LibWasm/Types.h | 6 +- 2 files changed, 126 insertions(+), 274 deletions(-) diff --git a/Userland/Libraries/LibWasm/Parser/Parser.cpp b/Userland/Libraries/LibWasm/Parser/Parser.cpp index bcda5e5c5c2..cfa8b591f13 100644 --- a/Userland/Libraries/LibWasm/Parser/Parser.cpp +++ b/Userland/Libraries/LibWasm/Parser/Parser.cpp @@ -78,11 +78,8 @@ static auto parse_vector(Stream& stream) static ParseResult parse_name(Stream& stream) { ScopeLogger logger; - auto data = parse_vector(stream); - if (data.is_error()) - return data.error(); - - return ByteString::copy(data.value()); + auto data = TRY(parse_vector(stream)); + return ByteString::copy(data); } template @@ -113,11 +110,9 @@ requires(requires(Stream& stream, Args... args) { T::parse(stream, args...); }) } new_stream.unread({ &byte, 1 }); - auto parse_result = T::parse(new_stream, args...); - if (parse_result.is_error()) - return parse_result.error(); + auto parse_result = TRY(T::parse(new_stream, args...)); - result.values.extend(parse_result.release_value()); + result.values.extend(parse_result); } } @@ -153,10 +148,8 @@ ParseResult ValueType::parse(Stream& stream) ParseResult ResultType::parse(Stream& stream) { ScopeLogger logger("ResultType"sv); - auto types = parse_vector(stream); - if (types.is_error()) - return types.error(); - return ResultType { types.release_value() }; + auto types = TRY(parse_vector(stream)); + return ResultType { types }; } ParseResult FunctionType::parse(Stream& stream) @@ -173,14 +166,10 @@ ParseResult FunctionType::parse(Stream& stream) return with_eof_check(stream, ParseError::InvalidTag); } - auto parameters_result = parse_vector(stream); - if (parameters_result.is_error()) - return parameters_result.error(); - auto results_result = parse_vector(stream); - if (results_result.is_error()) - return results_result.error(); + auto parameters_result = TRY(parse_vector(stream)); + auto results_result = TRY(parse_vector(stream)); - return FunctionType { parameters_result.release_value(), results_result.release_value() }; + return FunctionType { parameters_result, results_result }; } ParseResult Limits::parse(Stream& stream) @@ -214,32 +203,24 @@ ParseResult Limits::parse(Stream& stream) ParseResult MemoryType::parse(Stream& stream) { ScopeLogger logger("MemoryType"sv); - auto limits_result = Limits::parse(stream); - if (limits_result.is_error()) - return limits_result.error(); - return MemoryType { limits_result.release_value() }; + auto limits_result = TRY(Limits::parse(stream)); + return MemoryType { limits_result }; } ParseResult TableType::parse(Stream& stream) { ScopeLogger logger("TableType"sv); - auto type_result = ValueType::parse(stream); - if (type_result.is_error()) - return type_result.error(); - if (!type_result.value().is_reference()) + auto type_result = TRY(ValueType::parse(stream)); + if (!type_result.is_reference()) return with_eof_check(stream, ParseError::InvalidType); - auto limits_result = Limits::parse(stream); - if (limits_result.is_error()) - return limits_result.error(); - return TableType { type_result.release_value(), limits_result.release_value() }; + auto limits_result = TRY(Limits::parse(stream)); + return TableType { type_result, limits_result }; } ParseResult GlobalType::parse(Stream& stream) { ScopeLogger logger("GlobalType"sv); - auto type_result = ValueType::parse(stream); - if (type_result.is_error()) - return type_result.error(); + auto type_result = TRY(ValueType::parse(stream)); auto mutable_or_error = stream.read_value(); if (mutable_or_error.is_error()) @@ -250,7 +231,7 @@ ParseResult GlobalType::parse(Stream& stream) if (mutable_ > 1) return with_eof_check(stream, ParseError::InvalidTag); - return GlobalType { type_result.release_value(), mutable_ == 0x01 }; + return GlobalType { type_result, mutable_ == 0x01 }; } ParseResult BlockType::parse(Stream& stream) @@ -342,57 +323,36 @@ ParseResult> Instruction::parse(Stream& stream, InstructionP case Instructions::block.value(): case Instructions::loop.value(): case Instructions::if_.value(): { - auto block_type = BlockType::parse(stream); - if (block_type.is_error()) - return block_type.error(); - - nested_instructions.append({ move(resulting_instructions), opcode, block_type.release_value(), {}, {} }); + auto block_type = TRY(BlockType::parse(stream)); + nested_instructions.append({ move(resulting_instructions), opcode, block_type, {}, {} }); resulting_instructions = {}; break; } case Instructions::br.value(): case Instructions::br_if.value(): { // branches with a single label immediate - auto index = GenericIndexParser::parse(stream); - if (index.is_error()) - return index.error(); - - resulting_instructions.append(Instruction { opcode, index.release_value() }); + auto index = TRY(GenericIndexParser::parse(stream)); + resulting_instructions.append(Instruction { opcode, index }); break; } case Instructions::br_table.value(): { // br_table label* label - auto labels = parse_vector>(stream); - if (labels.is_error()) - return labels.error(); - - auto default_label = GenericIndexParser::parse(stream); - if (default_label.is_error()) - return default_label.error(); - - resulting_instructions.append(Instruction { opcode, TableBranchArgs { labels.release_value(), default_label.release_value() } }); + auto labels = TRY(parse_vector>(stream)); + auto default_label = TRY(GenericIndexParser::parse(stream)); + resulting_instructions.append(Instruction { opcode, TableBranchArgs { labels, default_label } }); break; } case Instructions::call.value(): { // call function - auto function_index = GenericIndexParser::parse(stream); - if (function_index.is_error()) - return function_index.error(); - - resulting_instructions.append(Instruction { opcode, function_index.release_value() }); + auto function_index = TRY(GenericIndexParser::parse(stream)); + resulting_instructions.append(Instruction { opcode, function_index }); break; } case Instructions::call_indirect.value(): { // call_indirect type table - auto type_index = GenericIndexParser::parse(stream); - if (type_index.is_error()) - return type_index.error(); - - auto table_index = GenericIndexParser::parse(stream); - if (table_index.is_error()) - return table_index.error(); - - resulting_instructions.append(Instruction { opcode, IndirectCallArgs { type_index.release_value(), table_index.release_value() } }); + auto type_index = TRY(GenericIndexParser::parse(stream)); + auto table_index = TRY(GenericIndexParser::parse(stream)); + resulting_instructions.append(Instruction { opcode, IndirectCallArgs { type_index, table_index } }); break; } case Instructions::i32_load.value(): @@ -445,20 +405,14 @@ ParseResult> Instruction::parse(Stream& stream, InstructionP case Instructions::local_get.value(): case Instructions::local_set.value(): case Instructions::local_tee.value(): { - auto index = GenericIndexParser::parse(stream); - if (index.is_error()) - return index.error(); - - resulting_instructions.append(Instruction { opcode, index.release_value() }); + auto index = TRY(GenericIndexParser::parse(stream)); + resulting_instructions.append(Instruction { opcode, index }); break; } case Instructions::global_get.value(): case Instructions::global_set.value(): { - auto index = GenericIndexParser::parse(stream); - if (index.is_error()) - return index.error(); - - resulting_instructions.append(Instruction { opcode, index.release_value() }); + auto index = TRY(GenericIndexParser::parse(stream)); + resulting_instructions.append(Instruction { opcode, index }); break; } case Instructions::memory_size.value(): @@ -517,37 +471,26 @@ ParseResult> Instruction::parse(Stream& stream, InstructionP } case Instructions::table_get.value(): case Instructions::table_set.value(): { - auto index = GenericIndexParser::parse(stream); - if (index.is_error()) - return index.error(); - - resulting_instructions.append(Instruction { opcode, index.release_value() }); + auto index = TRY(GenericIndexParser::parse(stream)); + resulting_instructions.append(Instruction { opcode, index }); break; } case Instructions::select_typed.value(): { - auto types = parse_vector(stream); - if (types.is_error()) - return types.error(); - - resulting_instructions.append(Instruction { opcode, types.release_value() }); + auto types = TRY(parse_vector(stream)); + resulting_instructions.append(Instruction { opcode, types }); break; } case Instructions::ref_null.value(): { - auto type = ValueType::parse(stream); - if (type.is_error()) - return type.error(); - if (!type.value().is_reference()) + auto type = TRY(ValueType::parse(stream)); + if (!type.is_reference()) return ParseError::InvalidType; - resulting_instructions.append(Instruction { opcode, type.release_value() }); + resulting_instructions.append(Instruction { opcode, type }); break; } case Instructions::ref_func.value(): { - auto index = GenericIndexParser::parse(stream); - if (index.is_error()) - return index.error(); - - resulting_instructions.append(Instruction { opcode, index.release_value() }); + auto index = TRY(GenericIndexParser::parse(stream)); + resulting_instructions.append(Instruction { opcode, index }); break; } case Instructions::ref_is_null.value(): @@ -707,9 +650,7 @@ ParseResult> Instruction::parse(Stream& stream, InstructionP resulting_instructions.append(Instruction { full_opcode }); break; case Instructions::memory_init.value(): { - auto index = GenericIndexParser::parse(stream); - if (index.is_error()) - return index.error(); + auto index = TRY(GenericIndexParser::parse(stream)); // Proposal "multi-memory", literal 0x00 is replaced with a memory index. auto memory_index_or_error = stream.read_value(); @@ -718,14 +659,12 @@ ParseResult> Instruction::parse(Stream& stream, InstructionP auto memory_index = memory_index_or_error.release_value(); - resulting_instructions.append(Instruction { full_opcode, MemoryInitArgs { index.release_value(), MemoryIndex(memory_index) } }); + resulting_instructions.append(Instruction { full_opcode, MemoryInitArgs { index, MemoryIndex(memory_index) } }); break; } case Instructions::data_drop.value(): { - auto index = GenericIndexParser::parse(stream); - if (index.is_error()) - return index.error(); - resulting_instructions.append(Instruction { full_opcode, index.release_value() }); + auto index = TRY(GenericIndexParser::parse(stream)); + resulting_instructions.append(Instruction { full_opcode, index }); break; } case Instructions::memory_copy.value(): { @@ -753,39 +692,27 @@ ParseResult> Instruction::parse(Stream& stream, InstructionP break; } case Instructions::table_init.value(): { - auto element_index = GenericIndexParser::parse(stream); - if (element_index.is_error()) - return element_index.error(); - auto table_index = GenericIndexParser::parse(stream); - if (table_index.is_error()) - return table_index.error(); - resulting_instructions.append(Instruction { full_opcode, TableElementArgs { element_index.release_value(), table_index.release_value() } }); + auto element_index = TRY(GenericIndexParser::parse(stream)); + auto table_index = TRY(GenericIndexParser::parse(stream)); + resulting_instructions.append(Instruction { full_opcode, TableElementArgs { element_index, table_index } }); break; } case Instructions::elem_drop.value(): { - auto element_index = GenericIndexParser::parse(stream); - if (element_index.is_error()) - return element_index.error(); - resulting_instructions.append(Instruction { full_opcode, element_index.release_value() }); + auto element_index = TRY(GenericIndexParser::parse(stream)); + resulting_instructions.append(Instruction { full_opcode, element_index }); break; } case Instructions::table_copy.value(): { - auto lhs = GenericIndexParser::parse(stream); - if (lhs.is_error()) - return lhs.error(); - auto rhs = GenericIndexParser::parse(stream); - if (rhs.is_error()) - return rhs.error(); - resulting_instructions.append(Instruction { full_opcode, TableTableArgs { lhs.release_value(), rhs.release_value() } }); + auto lhs = TRY(GenericIndexParser::parse(stream)); + auto rhs = TRY(GenericIndexParser::parse(stream)); + resulting_instructions.append(Instruction { full_opcode, TableTableArgs { lhs, rhs } }); break; } case Instructions::table_grow.value(): case Instructions::table_size.value(): case Instructions::table_fill.value(): { - auto index = GenericIndexParser::parse(stream); - if (index.is_error()) - return index.error(); - resulting_instructions.append(Instruction { full_opcode, index.release_value() }); + auto index = TRY(GenericIndexParser::parse(stream)); + resulting_instructions.append(Instruction { full_opcode, index }); break; } case Instructions::v128_load.value(): @@ -1119,9 +1046,7 @@ ParseResult> Instruction::parse(Stream& stream, InstructionP ParseResult CustomSection::parse(Stream& stream) { ScopeLogger logger("CustomSection"sv); - auto name = parse_name(stream); - if (name.is_error()) - return name.error(); + auto name = TRY(parse_name(stream)); ByteBuffer data_buffer; if (data_buffer.try_resize(64).is_error()) @@ -1139,27 +1064,21 @@ ParseResult CustomSection::parse(Stream& stream) return with_eof_check(stream, ParseError::HugeAllocationRequested); } - return CustomSection(name.release_value(), move(data_buffer)); + return CustomSection(name, move(data_buffer)); } ParseResult TypeSection::parse(Stream& stream) { ScopeLogger logger("TypeSection"sv); - auto types = parse_vector(stream); - if (types.is_error()) - return types.error(); - return TypeSection { types.release_value() }; + auto types = TRY(parse_vector(stream)); + return TypeSection { types }; } ParseResult ImportSection::Import::parse(Stream& stream) { ScopeLogger logger("Import"sv); - auto module = parse_name(stream); - if (module.is_error()) - return module.error(); - auto name = parse_name(stream); - if (name.is_error()) - return name.error(); + auto module = TRY(parse_name(stream)); + auto name = TRY(parse_name(stream)); auto tag_or_error = stream.read_value(); if (tag_or_error.is_error()) return with_eof_check(stream, ParseError::ExpectedKindTag); @@ -1168,10 +1087,8 @@ ParseResult ImportSection::Import::parse(Stream& stream) switch (tag) { case Constants::extern_function_tag: { - auto index = GenericIndexParser::parse(stream); - if (index.is_error()) - return index.error(); - return Import { module.release_value(), name.release_value(), index.release_value() }; + auto index = TRY(GenericIndexParser::parse(stream)); + return Import { module, name, index }; } case Constants::extern_table_tag: return parse_with_type(stream, module, name); @@ -1187,22 +1104,18 @@ ParseResult ImportSection::Import::parse(Stream& stream) ParseResult ImportSection::parse(Stream& stream) { ScopeLogger logger("ImportSection"sv); - auto imports = parse_vector(stream); - if (imports.is_error()) - return imports.error(); - return ImportSection { imports.release_value() }; + auto imports = TRY(parse_vector(stream)); + return ImportSection { imports }; } ParseResult FunctionSection::parse(Stream& stream) { ScopeLogger logger("FunctionSection"sv); - auto indices = parse_vector(stream); - if (indices.is_error()) - return indices.error(); + auto indices = TRY(parse_vector(stream)); Vector typed_indices; - typed_indices.ensure_capacity(indices.value().size()); - for (auto entry : indices.value()) + typed_indices.ensure_capacity(indices.size()); + for (auto entry : indices) typed_indices.append(entry); return FunctionSection { move(typed_indices) }; @@ -1211,37 +1124,29 @@ ParseResult FunctionSection::parse(Stream& stream) ParseResult TableSection::Table::parse(Stream& stream) { ScopeLogger logger("Table"sv); - auto type = TableType::parse(stream); - if (type.is_error()) - return type.error(); - return Table { type.release_value() }; + auto type = TRY(TableType::parse(stream)); + return Table { type }; } ParseResult TableSection::parse(Stream& stream) { ScopeLogger logger("TableSection"sv); - auto tables = parse_vector(stream); - if (tables.is_error()) - return tables.error(); - return TableSection { tables.release_value() }; + auto tables = TRY(parse_vector
(stream)); + return TableSection { tables }; } ParseResult MemorySection::Memory::parse(Stream& stream) { ScopeLogger logger("Memory"sv); - auto type = MemoryType::parse(stream); - if (type.is_error()) - return type.error(); - return Memory { type.release_value() }; + auto type = TRY(MemoryType::parse(stream)); + return Memory { type }; } ParseResult MemorySection::parse(Stream& stream) { ScopeLogger logger("MemorySection"sv); - auto memories = parse_vector(stream); - if (memories.is_error()) - return memories.error(); - return MemorySection { memories.release_value() }; + auto memories = TRY(parse_vector(stream)); + return MemorySection { memories }; } ParseResult Expression::parse(Stream& stream) @@ -1258,30 +1163,22 @@ ParseResult Expression::parse(Stream& stream) ParseResult GlobalSection::Global::parse(Stream& stream) { ScopeLogger logger("Global"sv); - auto type = GlobalType::parse(stream); - if (type.is_error()) - return type.error(); - auto exprs = Expression::parse(stream); - if (exprs.is_error()) - return exprs.error(); - return Global { type.release_value(), exprs.release_value() }; + auto type = TRY(GlobalType::parse(stream)); + auto exprs = TRY(Expression::parse(stream)); + return Global { type, exprs }; } ParseResult GlobalSection::parse(Stream& stream) { ScopeLogger logger("GlobalSection"sv); - auto result = parse_vector(stream); - if (result.is_error()) - return result.error(); - return GlobalSection { result.release_value() }; + auto result = TRY(parse_vector(stream)); + return GlobalSection { result }; } ParseResult ExportSection::Export::parse(Stream& stream) { ScopeLogger logger("Export"sv); - auto name = parse_name(stream); - if (name.is_error()) - return name.error(); + auto name = TRY(parse_name(stream)); auto tag_or_error = stream.read_value(); if (tag_or_error.is_error()) return with_eof_check(stream, ParseError::ExpectedKindTag); @@ -1295,13 +1192,13 @@ ParseResult ExportSection::Export::parse(Stream& stream) switch (tag) { case Constants::extern_function_tag: - return Export { name.release_value(), ExportDesc { FunctionIndex { index } } }; + return Export { name, ExportDesc { FunctionIndex { index } } }; case Constants::extern_table_tag: - return Export { name.release_value(), ExportDesc { TableIndex { index } } }; + return Export { name, ExportDesc { TableIndex { index } } }; case Constants::extern_memory_tag: - return Export { name.release_value(), ExportDesc { MemoryIndex { index } } }; + return Export { name, ExportDesc { MemoryIndex { index } } }; case Constants::extern_global_tag: - return Export { name.release_value(), ExportDesc { GlobalIndex { index } } }; + return Export { name, ExportDesc { GlobalIndex { index } } }; default: return with_eof_check(stream, ParseError::InvalidTag); } @@ -1310,40 +1207,30 @@ ParseResult ExportSection::Export::parse(Stream& stream) ParseResult ExportSection::parse(Stream& stream) { ScopeLogger logger("ExportSection"sv); - auto result = parse_vector(stream); - if (result.is_error()) - return result.error(); - return ExportSection { result.release_value() }; + auto result = TRY(parse_vector(stream)); + return ExportSection { result }; } ParseResult StartSection::StartFunction::parse(Stream& stream) { ScopeLogger logger("StartFunction"sv); - auto index = GenericIndexParser::parse(stream); - if (index.is_error()) - return index.error(); - return StartFunction { index.release_value() }; + auto index = TRY(GenericIndexParser::parse(stream)); + return StartFunction { index }; } ParseResult StartSection::parse(Stream& stream) { ScopeLogger logger("StartSection"sv); - auto result = StartFunction::parse(stream); - if (result.is_error()) - return result.error(); - return StartSection { result.release_value() }; + auto result = TRY(StartFunction::parse(stream)); + return StartSection { result }; } ParseResult ElementSection::SegmentType0::parse(Stream& stream) { - auto expression = Expression::parse(stream); - if (expression.is_error()) - return expression.error(); - auto indices = parse_vector>(stream); - if (indices.is_error()) - return indices.error(); + auto expression = TRY(Expression::parse(stream)); + auto indices = TRY(parse_vector>(stream)); - return SegmentType0 { indices.release_value(), Active { 0, expression.release_value() } }; + return SegmentType0 { indices, Active { 0, expression } }; } ParseResult ElementSection::SegmentType1::parse(Stream& stream) @@ -1355,11 +1242,9 @@ ParseResult ElementSection::SegmentType1::parse(St auto kind = kind_or_error.release_value(); if (kind != 0) return ParseError::InvalidTag; - auto indices = parse_vector>(stream); - if (indices.is_error()) - return indices.error(); + auto indices = TRY(parse_vector>(stream)); - return SegmentType1 { indices.release_value() }; + return SegmentType1 { indices }; } ParseResult ElementSection::SegmentType2::parse(Stream& stream) @@ -1378,19 +1263,15 @@ ParseResult ElementSection::SegmentType3::parse(St ParseResult ElementSection::SegmentType4::parse(Stream& stream) { - auto expression = Expression::parse(stream); - if (expression.is_error()) - return expression.error(); - auto initializers = parse_vector(stream); - if (initializers.is_error()) - return initializers.error(); + auto expression = TRY(Expression::parse(stream)); + auto initializers = TRY(parse_vector(stream)); return SegmentType4 { .mode = Active { .index = 0, - .expression = expression.release_value(), + .expression = expression, }, - .initializer = initializers.release_value(), + .initializer = initializers, }; } @@ -1487,10 +1368,8 @@ ParseResult ElementSection::Element::parse(Stream& stre ParseResult ElementSection::parse(Stream& stream) { ScopeLogger logger("ElementSection"sv); - auto result = parse_vector(stream); - if (result.is_error()) - return result.error(); - return ElementSection { result.release_value() }; + auto result = TRY(parse_vector(stream)); + return ElementSection { result }; } ParseResult Locals::parse(Stream& stream) @@ -1504,23 +1383,17 @@ ParseResult Locals::parse(Stream& stream) if (count > Constants::max_allowed_function_locals_per_type) return with_eof_check(stream, ParseError::HugeAllocationRequested); - auto type = ValueType::parse(stream); - if (type.is_error()) - return type.error(); + auto type = TRY(ValueType::parse(stream)); - return Locals { static_cast(count), type.release_value() }; + return Locals { static_cast(count), type }; } ParseResult CodeSection::Func::parse(Stream& stream) { ScopeLogger logger("Func"sv); - auto locals = parse_vector(stream); - if (locals.is_error()) - return locals.error(); - auto body = Expression::parse(stream); - if (body.is_error()) - return body.error(); - return Func { locals.release_value(), body.release_value() }; + auto locals = TRY(parse_vector(stream)); + auto body = TRY(Expression::parse(stream)); + return Func { locals, body }; } ParseResult CodeSection::Code::parse(Stream& stream) @@ -1533,20 +1406,16 @@ ParseResult CodeSection::Code::parse(Stream& stream) auto constrained_stream = ConstrainedStream { MaybeOwned(stream), size }; - auto func = Func::parse(constrained_stream); - if (func.is_error()) - return func.error(); + auto func = TRY(Func::parse(constrained_stream)); - return Code { static_cast(size), func.release_value() }; + return Code { static_cast(size), func }; } ParseResult CodeSection::parse(Stream& stream) { ScopeLogger logger("CodeSection"sv); - auto result = parse_vector(stream); - if (result.is_error()) - return result.error(); - return CodeSection { result.release_value() }; + auto result = TRY(parse_vector(stream)); + return CodeSection { result }; } ParseResult DataSection::Data::parse(Stream& stream) @@ -1562,31 +1431,19 @@ ParseResult DataSection::Data::parse(Stream& stream) return with_eof_check(stream, ParseError::InvalidTag); if (tag == 0x00) { - auto expr = Expression::parse(stream); - if (expr.is_error()) - return expr.error(); - auto init = parse_vector(stream); - if (init.is_error()) - return init.error(); - return Data { Active { init.release_value(), { 0 }, expr.release_value() } }; + auto expr = TRY(Expression::parse(stream)); + auto init = TRY(parse_vector(stream)); + return Data { Active { init, { 0 }, expr } }; } if (tag == 0x01) { - auto init = parse_vector(stream); - if (init.is_error()) - return init.error(); - return Data { Passive { init.release_value() } }; + auto init = TRY(parse_vector(stream)); + return Data { Passive { init } }; } if (tag == 0x02) { - auto index = GenericIndexParser::parse(stream); - if (index.is_error()) - return index.error(); - auto expr = Expression::parse(stream); - if (expr.is_error()) - return expr.error(); - auto init = parse_vector(stream); - if (init.is_error()) - return init.error(); - return Data { Active { init.release_value(), index.release_value(), expr.release_value() } }; + auto index = TRY(GenericIndexParser::parse(stream)); + auto expr = TRY(Expression::parse(stream)); + auto init = TRY(parse_vector(stream)); + return Data { Active { init, index, expr } }; } VERIFY_NOT_REACHED(); } @@ -1594,11 +1451,8 @@ ParseResult DataSection::Data::parse(Stream& stream) ParseResult DataSection::parse(Stream& stream) { ScopeLogger logger("DataSection"sv); - auto data = parse_vector(stream); - if (data.is_error()) - return data.error(); - - return DataSection { data.release_value() }; + auto data = TRY(parse_vector(stream)); + return DataSection { data }; } ParseResult DataCountSection::parse([[maybe_unused]] Stream& stream) diff --git a/Userland/Libraries/LibWasm/Types.h b/Userland/Libraries/LibWasm/Types.h index 547b8d4b978..4b1d9f32ee0 100644 --- a/Userland/Libraries/LibWasm/Types.h +++ b/Userland/Libraries/LibWasm/Types.h @@ -557,10 +557,8 @@ public: template static ParseResult parse_with_type(auto&& stream, auto&& module, auto&& name) { - auto result = T::parse(stream); - if (result.is_error()) - return result.error(); - return Import { module.release_value(), name.release_value(), result.release_value() }; + auto result = TRY(T::parse(stream)); + return Import { module, name, result }; } ByteString m_module;