From 985d0dd2701dcd9d9b17f06535b1b66c5e361926 Mon Sep 17 00:00:00 2001 From: Kenneth Myhra Date: Thu, 22 Feb 2024 18:28:34 +0100 Subject: [PATCH] LibWeb: Move deserialize_* methods outside scope of Deserializer class These methods are useful independent of the class Deserializer, so let's move their declarations to the header file and and outside the scope of the Deserializer class. --- .../LibWeb/HTML/StructuredSerialize.cpp | 90 +++++++++---------- .../LibWeb/HTML/StructuredSerialize.h | 5 ++ 2 files changed, 50 insertions(+), 45 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/StructuredSerialize.cpp b/Userland/Libraries/LibWeb/HTML/StructuredSerialize.cpp index 62c4d43625..9b823ed519 100644 --- a/Userland/Libraries/LibWeb/HTML/StructuredSerialize.cpp +++ b/Userland/Libraries/LibWeb/HTML/StructuredSerialize.cpp @@ -894,53 +894,53 @@ private: m_position += 2; return value; } - - static WebIDL::ExceptionOr deserialize_bytes(JS::VM& vm, ReadonlySpan vector, size_t& position) - { - u32 size_bits[2]; - size_bits[0] = vector[position++]; - size_bits[1] = vector[position++]; - u64 const size = *bit_cast(&size_bits); - - auto bytes = TRY_OR_THROW_OOM(vm, ByteBuffer::create_uninitialized(size)); - u64 byte_position = 0; - while (position < vector.size() && byte_position < size) { - for (u8 i = 0; i < 4; ++i) { - bytes[byte_position++] = (vector[position] >> (i * 8) & 0xFF); - if (byte_position == size) - break; - } - position++; - } - return bytes; - } - - static WebIDL::ExceptionOr deserialize_string(JS::VM& vm, ReadonlySpan vector, size_t& position) - { - auto bytes = TRY(deserialize_bytes(vm, vector, position)); - return TRY_OR_THROW_OOM(vm, String::from_utf8(StringView { bytes })); - } - - static WebIDL::ExceptionOr> deserialize_string_primitive(JS::VM& vm, ReadonlySpan vector, size_t& position) - { - auto bytes = TRY(deserialize_bytes(vm, vector, position)); - - return TRY(Bindings::throw_dom_exception_if_needed(vm, [&vm, &bytes]() { - return JS::PrimitiveString::create(vm, StringView { bytes }); - })); - } - - static WebIDL::ExceptionOr> deserialize_big_int_primitive(JS::VM& vm, ReadonlySpan vector, size_t& position) - { - auto string = TRY(deserialize_string_primitive(vm, vector, position)); - auto string_view = TRY(Bindings::throw_dom_exception_if_needed(vm, [&string]() { - return string->utf8_string_view(); - })); - auto bigint = MUST(::Crypto::SignedBigInteger::from_base(10, string_view.substring_view(0, string_view.length() - 1))); - return JS::BigInt::create(vm, bigint); - } }; +WebIDL::ExceptionOr deserialize_bytes(JS::VM& vm, ReadonlySpan vector, size_t& position) +{ + u32 size_bits[2]; + size_bits[0] = vector[position++]; + size_bits[1] = vector[position++]; + u64 const size = *bit_cast(&size_bits); + + auto bytes = TRY_OR_THROW_OOM(vm, ByteBuffer::create_uninitialized(size)); + u64 byte_position = 0; + while (position < vector.size() && byte_position < size) { + for (u8 i = 0; i < 4; ++i) { + bytes[byte_position++] = (vector[position] >> (i * 8) & 0xFF); + if (byte_position == size) + break; + } + position++; + } + return bytes; +} + +WebIDL::ExceptionOr deserialize_string(JS::VM& vm, ReadonlySpan vector, size_t& position) +{ + auto bytes = TRY(deserialize_bytes(vm, vector, position)); + return TRY_OR_THROW_OOM(vm, String::from_utf8(StringView { bytes })); +} + +WebIDL::ExceptionOr> deserialize_string_primitive(JS::VM& vm, ReadonlySpan vector, size_t& position) +{ + auto bytes = TRY(deserialize_bytes(vm, vector, position)); + + return TRY(Bindings::throw_dom_exception_if_needed(vm, [&vm, &bytes]() { + return JS::PrimitiveString::create(vm, StringView { bytes }); + })); +} + +WebIDL::ExceptionOr> deserialize_big_int_primitive(JS::VM& vm, ReadonlySpan vector, size_t& position) +{ + auto string = TRY(deserialize_string_primitive(vm, vector, position)); + auto string_view = TRY(Bindings::throw_dom_exception_if_needed(vm, [&string]() { + return string->utf8_string_view(); + })); + auto bigint = MUST(::Crypto::SignedBigInteger::from_base(10, string_view.substring_view(0, string_view.length() - 1))); + return JS::BigInt::create(vm, bigint); +} + // https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializewithtransfer WebIDL::ExceptionOr structured_serialize_with_transfer(JS::VM& vm, JS::Value value, Vector> const& transfer_list) { diff --git a/Userland/Libraries/LibWeb/HTML/StructuredSerialize.h b/Userland/Libraries/LibWeb/HTML/StructuredSerialize.h index 6a2fe2ce11..bdf48c342d 100644 --- a/Userland/Libraries/LibWeb/HTML/StructuredSerialize.h +++ b/Userland/Libraries/LibWeb/HTML/StructuredSerialize.h @@ -50,6 +50,11 @@ WebIDL::ExceptionOr structured_serialize_internal(JS::VM& v WebIDL::ExceptionOr structured_deserialize(JS::VM& vm, SerializationRecord const& serialized, JS::Realm& target_realm, Optional); +WebIDL::ExceptionOr deserialize_bytes(JS::VM& vm, ReadonlySpan vector, size_t& position); +WebIDL::ExceptionOr deserialize_string(JS::VM& vm, ReadonlySpan vector, size_t& position); +WebIDL::ExceptionOr> deserialize_string_primitive(JS::VM& vm, ReadonlySpan vector, size_t& position); +WebIDL::ExceptionOr> deserialize_big_int_primitive(JS::VM& vm, ReadonlySpan vector, size_t& position); + WebIDL::ExceptionOr structured_serialize_with_transfer(JS::VM& vm, JS::Value value, Vector> const& transfer_list); WebIDL::ExceptionOr structured_deserialize_with_transfer(JS::VM& vm, SerializedTransferRecord&);