From a527f5576861f9b1f82069ca5a5fa87f3e133c2d Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Mon, 11 Sep 2023 18:05:30 -0600 Subject: [PATCH] LibWeb: Create StructuredSerialize helpers for Bytes Call them from the helpers for strings. We'll have other object classes soon that need to serialize ByteBuffers, so let's take advantage of the encoding scheme we are already using for Strings. --- .../LibWeb/HTML/StructuredSerialize.cpp | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/StructuredSerialize.cpp b/Userland/Libraries/LibWeb/HTML/StructuredSerialize.cpp index 96bb3b60e0..0f8525492b 100644 --- a/Userland/Libraries/LibWeb/HTML/StructuredSerialize.cpp +++ b/Userland/Libraries/LibWeb/HTML/StructuredSerialize.cpp @@ -206,14 +206,13 @@ private: SerializationMemory& m_memory; // JS value -> index SerializationRecord m_serialized; - WebIDL::ExceptionOr serialize_string(Vector& vector, String const& string) + WebIDL::ExceptionOr serialize_bytes(Vector& vector, ReadonlyBytes bytes) { - u64 const size = string.code_points().byte_length(); - // Append size of the string to the serialized structure. + // Append size of the buffer to the serialized structure. + u64 const size = bytes.size(); TRY_OR_THROW_OOM(m_vm, vector.try_append(bit_cast(&size), 2)); - // Append the bytes of the string to the serialized structure. + // Append the bytes of the buffer to the serialized structure. u64 byte_position = 0; - ReadonlyBytes const bytes = { string.code_points().bytes(), string.code_points().byte_length() }; while (byte_position < size) { u32 combined_value = 0; for (u8 i = 0; i < 4; ++i) { @@ -228,6 +227,11 @@ private: return {}; } + WebIDL::ExceptionOr serialize_string(Vector& vector, String const& string) + { + return serialize_bytes(vector, { string.code_points().bytes(), string.code_points().byte_length() }); + } + WebIDL::ExceptionOr serialize_string(Vector& vector, JS::PrimitiveString const& primitive_string) { auto string = primitive_string.utf8_string(); @@ -344,25 +348,29 @@ private: JS::MarkedVector m_memory; // Index -> JS value Optional m_error; - static WebIDL::ExceptionOr> deserialize_string_primitive(JS::VM& vm, Vector const& vector, u32& position) + static WebIDL::ExceptionOr deserialize_bytes(JS::VM& vm, Vector const& vector, u32& position) { u32 size_bits[2]; size_bits[0] = vector[position++]; size_bits[1] = vector[position++]; u64 const size = *bit_cast(&size_bits); - Vector bytes; - TRY_OR_THROW_OOM(vm, bytes.try_ensure_capacity(size)); + 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.append(vector[position] >> (i * 8) & 0xFF); - byte_position++; + bytes[byte_position++] = (vector[position] >> (i * 8) & 0xFF); if (byte_position == size) break; } position++; } + return bytes; + } + + static WebIDL::ExceptionOr> deserialize_string_primitive(JS::VM& vm, Vector const& vector, u32& 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 });