1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:47:44 +00:00

LibWasm: Replace usages of the Endian bytes accessor

This commit is contained in:
Tim Schumacher 2023-04-12 12:05:06 +02:00 committed by Tim Flynn
parent e11e7309dd
commit 547a08670e
2 changed files with 17 additions and 18 deletions

View file

@ -207,10 +207,9 @@ template<>
struct ConvertToRaw<float> { struct ConvertToRaw<float> {
u32 operator()(float value) u32 operator()(float value)
{ {
LittleEndian<u32> res;
ReadonlyBytes bytes { &value, sizeof(float) }; ReadonlyBytes bytes { &value, sizeof(float) };
FixedMemoryStream stream { bytes }; FixedMemoryStream stream { bytes };
stream.read_until_filled(res.bytes()).release_value_but_fixme_should_propagate_errors(); auto res = stream.read_value<LittleEndian<u32>>().release_value_but_fixme_should_propagate_errors();
return static_cast<u32>(res); return static_cast<u32>(res);
} }
}; };
@ -219,10 +218,9 @@ template<>
struct ConvertToRaw<double> { struct ConvertToRaw<double> {
u64 operator()(double value) u64 operator()(double value)
{ {
LittleEndian<u64> res;
ReadonlyBytes bytes { &value, sizeof(double) }; ReadonlyBytes bytes { &value, sizeof(double) };
FixedMemoryStream stream { bytes }; FixedMemoryStream stream { bytes };
stream.read_until_filled(res.bytes()).release_value_but_fixme_should_propagate_errors(); auto res = stream.read_value<LittleEndian<u64>>().release_value_but_fixme_should_propagate_errors();
return static_cast<u64>(res); return static_cast<u64>(res);
} }
}; };
@ -258,35 +256,34 @@ void BytecodeInterpreter::store_to_memory(Configuration& configuration, Instruct
template<typename T> template<typename T>
T BytecodeInterpreter::read_value(ReadonlyBytes data) T BytecodeInterpreter::read_value(ReadonlyBytes data)
{ {
LittleEndian<T> value;
FixedMemoryStream stream { data }; FixedMemoryStream stream { data };
auto maybe_error = stream.read_until_filled(value.bytes()); auto value_or_error = stream.read_value<LittleEndian<T>>();
if (maybe_error.is_error()) { if (value_or_error.is_error()) {
dbgln("Read from {} failed", data.data()); dbgln("Read from {} failed", data.data());
m_trap = Trap { "Read from memory failed" }; m_trap = Trap { "Read from memory failed" };
} }
return value; return value_or_error.release_value();
} }
template<> template<>
float BytecodeInterpreter::read_value<float>(ReadonlyBytes data) float BytecodeInterpreter::read_value<float>(ReadonlyBytes data)
{ {
LittleEndian<u32> raw_value;
FixedMemoryStream stream { data }; FixedMemoryStream stream { data };
auto maybe_error = stream.read_until_filled(raw_value.bytes()); auto raw_value_or_error = stream.read_value<LittleEndian<u32>>();
if (maybe_error.is_error()) if (raw_value_or_error.is_error())
m_trap = Trap { "Read from memory failed" }; m_trap = Trap { "Read from memory failed" };
auto raw_value = raw_value_or_error.release_value();
return bit_cast<float>(static_cast<u32>(raw_value)); return bit_cast<float>(static_cast<u32>(raw_value));
} }
template<> template<>
double BytecodeInterpreter::read_value<double>(ReadonlyBytes data) double BytecodeInterpreter::read_value<double>(ReadonlyBytes data)
{ {
LittleEndian<u64> raw_value;
FixedMemoryStream stream { data }; FixedMemoryStream stream { data };
auto maybe_error = stream.read_until_filled(raw_value.bytes()); auto raw_value_or_error = stream.read_value<LittleEndian<u64>>();
if (maybe_error.is_error()) if (raw_value_or_error.is_error())
m_trap = Trap { "Read from memory failed" }; m_trap = Trap { "Read from memory failed" };
auto raw_value = raw_value_or_error.release_value();
return bit_cast<double>(static_cast<u64>(raw_value)); return bit_cast<double>(static_cast<u64>(raw_value));
} }

View file

@ -486,9 +486,10 @@ ParseResult<Vector<Instruction>> Instruction::parse(Stream& stream, InstructionP
} }
case Instructions::f32_const.value(): { case Instructions::f32_const.value(): {
// op literal // op literal
LittleEndian<u32> value; auto value_or_error = stream.read_value<LittleEndian<u32>>();
if (stream.read_until_filled(value.bytes()).is_error()) if (value_or_error.is_error())
return with_eof_check(stream, ParseError::ExpectedFloatingImmediate); return with_eof_check(stream, ParseError::ExpectedFloatingImmediate);
auto value = value_or_error.release_value();
auto floating = bit_cast<float>(static_cast<u32>(value)); auto floating = bit_cast<float>(static_cast<u32>(value));
resulting_instructions.append(Instruction { opcode, floating }); resulting_instructions.append(Instruction { opcode, floating });
@ -496,9 +497,10 @@ ParseResult<Vector<Instruction>> Instruction::parse(Stream& stream, InstructionP
} }
case Instructions::f64_const.value(): { case Instructions::f64_const.value(): {
// op literal // op literal
LittleEndian<u64> value; auto value_or_error = stream.read_value<LittleEndian<u64>>();
if (stream.read_until_filled(value.bytes()).is_error()) if (value_or_error.is_error())
return with_eof_check(stream, ParseError::ExpectedFloatingImmediate); return with_eof_check(stream, ParseError::ExpectedFloatingImmediate);
auto value = value_or_error.release_value();
auto floating = bit_cast<double>(static_cast<u64>(value)); auto floating = bit_cast<double>(static_cast<u64>(value));
resulting_instructions.append(Instruction { opcode, floating }); resulting_instructions.append(Instruction { opcode, floating });