From 547a08670ee841e81b9b8f66c67bab599f0ce6b4 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Wed, 12 Apr 2023 12:05:06 +0200 Subject: [PATCH] LibWasm: Replace usages of the Endian bytes accessor --- .../AbstractMachine/BytecodeInterpreter.cpp | 25 ++++++++----------- Userland/Libraries/LibWasm/Parser/Parser.cpp | 10 +++++--- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp b/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp index 088b2a8079..362446be36 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp +++ b/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp @@ -207,10 +207,9 @@ template<> struct ConvertToRaw { u32 operator()(float value) { - LittleEndian res; ReadonlyBytes bytes { &value, sizeof(float) }; FixedMemoryStream stream { bytes }; - stream.read_until_filled(res.bytes()).release_value_but_fixme_should_propagate_errors(); + auto res = stream.read_value>().release_value_but_fixme_should_propagate_errors(); return static_cast(res); } }; @@ -219,10 +218,9 @@ template<> struct ConvertToRaw { u64 operator()(double value) { - LittleEndian res; ReadonlyBytes bytes { &value, sizeof(double) }; FixedMemoryStream stream { bytes }; - stream.read_until_filled(res.bytes()).release_value_but_fixme_should_propagate_errors(); + auto res = stream.read_value>().release_value_but_fixme_should_propagate_errors(); return static_cast(res); } }; @@ -258,35 +256,34 @@ void BytecodeInterpreter::store_to_memory(Configuration& configuration, Instruct template T BytecodeInterpreter::read_value(ReadonlyBytes data) { - LittleEndian value; FixedMemoryStream stream { data }; - auto maybe_error = stream.read_until_filled(value.bytes()); - if (maybe_error.is_error()) { + auto value_or_error = stream.read_value>(); + if (value_or_error.is_error()) { dbgln("Read from {} failed", data.data()); m_trap = Trap { "Read from memory failed" }; } - return value; + return value_or_error.release_value(); } template<> float BytecodeInterpreter::read_value(ReadonlyBytes data) { - LittleEndian raw_value; FixedMemoryStream stream { data }; - auto maybe_error = stream.read_until_filled(raw_value.bytes()); - if (maybe_error.is_error()) + auto raw_value_or_error = stream.read_value>(); + if (raw_value_or_error.is_error()) m_trap = Trap { "Read from memory failed" }; + auto raw_value = raw_value_or_error.release_value(); return bit_cast(static_cast(raw_value)); } template<> double BytecodeInterpreter::read_value(ReadonlyBytes data) { - LittleEndian raw_value; FixedMemoryStream stream { data }; - auto maybe_error = stream.read_until_filled(raw_value.bytes()); - if (maybe_error.is_error()) + auto raw_value_or_error = stream.read_value>(); + if (raw_value_or_error.is_error()) m_trap = Trap { "Read from memory failed" }; + auto raw_value = raw_value_or_error.release_value(); return bit_cast(static_cast(raw_value)); } diff --git a/Userland/Libraries/LibWasm/Parser/Parser.cpp b/Userland/Libraries/LibWasm/Parser/Parser.cpp index d0a5dadc5d..6c6b343336 100644 --- a/Userland/Libraries/LibWasm/Parser/Parser.cpp +++ b/Userland/Libraries/LibWasm/Parser/Parser.cpp @@ -486,9 +486,10 @@ ParseResult> Instruction::parse(Stream& stream, InstructionP } case Instructions::f32_const.value(): { // op literal - LittleEndian value; - if (stream.read_until_filled(value.bytes()).is_error()) + auto value_or_error = stream.read_value>(); + if (value_or_error.is_error()) return with_eof_check(stream, ParseError::ExpectedFloatingImmediate); + auto value = value_or_error.release_value(); auto floating = bit_cast(static_cast(value)); resulting_instructions.append(Instruction { opcode, floating }); @@ -496,9 +497,10 @@ ParseResult> Instruction::parse(Stream& stream, InstructionP } case Instructions::f64_const.value(): { // op literal - LittleEndian value; - if (stream.read_until_filled(value.bytes()).is_error()) + auto value_or_error = stream.read_value>(); + if (value_or_error.is_error()) return with_eof_check(stream, ParseError::ExpectedFloatingImmediate); + auto value = value_or_error.release_value(); auto floating = bit_cast(static_cast(value)); resulting_instructions.append(Instruction { opcode, floating });