소스 검색

LibWasm: Replace usages of the Endian bytes accessor

Tim Schumacher 2 년 전
부모
커밋
547a08670e
2개의 변경된 파일17개의 추가작업 그리고 18개의 파일을 삭제
  1. 11 14
      Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp
  2. 6 4
      Userland/Libraries/LibWasm/Parser/Parser.cpp

+ 11 - 14
Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp

@@ -207,10 +207,9 @@ template<>
 struct ConvertToRaw<float> {
     u32 operator()(float value)
     {
-        LittleEndian<u32> 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<LittleEndian<u32>>().release_value_but_fixme_should_propagate_errors();
         return static_cast<u32>(res);
     }
 };
@@ -219,10 +218,9 @@ template<>
 struct ConvertToRaw<double> {
     u64 operator()(double value)
     {
-        LittleEndian<u64> 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<LittleEndian<u64>>().release_value_but_fixme_should_propagate_errors();
         return static_cast<u64>(res);
     }
 };
@@ -258,35 +256,34 @@ void BytecodeInterpreter::store_to_memory(Configuration& configuration, Instruct
 template<typename T>
 T BytecodeInterpreter::read_value(ReadonlyBytes data)
 {
-    LittleEndian<T> 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<LittleEndian<T>>();
+    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<float>(ReadonlyBytes data)
 {
-    LittleEndian<u32> 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<LittleEndian<u32>>();
+    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<float>(static_cast<u32>(raw_value));
 }
 
 template<>
 double BytecodeInterpreter::read_value<double>(ReadonlyBytes data)
 {
-    LittleEndian<u64> 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<LittleEndian<u64>>();
+    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<double>(static_cast<u64>(raw_value));
 }
 

+ 6 - 4
Userland/Libraries/LibWasm/Parser/Parser.cpp

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