LibWeb/IDB: Update convert_a_value_to_a_key to latest changes

This commit is contained in:
stelar7 2025-03-01 10:07:55 +01:00 committed by Jelle Raaijmakers
commit a0b252c0dd
Notes: github-actions[bot] 2025-03-13 10:24:16 +00:00
3 changed files with 35 additions and 2 deletions

View file

@ -22,6 +22,7 @@
#include <LibWeb/IndexedDB/Internal/Database.h> #include <LibWeb/IndexedDB/Internal/Database.h>
#include <LibWeb/StorageAPI/StorageKey.h> #include <LibWeb/StorageAPI/StorageKey.h>
#include <LibWeb/WebIDL/AbstractOperations.h> #include <LibWeb/WebIDL/AbstractOperations.h>
#include <LibWeb/WebIDL/Buffers.h>
namespace Web::IndexedDB { namespace Web::IndexedDB {
@ -213,10 +214,14 @@ ErrorOr<GC::Ref<Key>> convert_a_value_to_a_key(JS::Realm& realm, JS::Value input
// - If input is a buffer source type // - If input is a buffer source type
if (input.is_object() && (is<JS::TypedArrayBase>(input.as_object()) || is<JS::ArrayBuffer>(input.as_object()) || is<JS::DataView>(input.as_object()))) { if (input.is_object() && (is<JS::TypedArrayBase>(input.as_object()) || is<JS::ArrayBuffer>(input.as_object()) || is<JS::DataView>(input.as_object()))) {
// 1. Let bytes be the result of getting a copy of the bytes held by the buffer source input. Rethrow any exceptions. // 1. If input is [detached] then return invalid.
if (WebIDL::is_buffer_source_detached(input))
return Error::from_string_literal("Detached buffer is not supported as key");
// 2. Let bytes be the result of getting a copy of the bytes held by the buffer source input.
auto data_buffer = TRY(WebIDL::get_buffer_source_copy(input.as_object())); auto data_buffer = TRY(WebIDL::get_buffer_source_copy(input.as_object()));
// 2. Return a new key with type binary and value bytes. // 3. Return a new key with type binary and value bytes.
return Key::create_binary(realm, data_buffer); return Key::create_binary(realm, data_buffer);
} }

View file

@ -127,4 +127,30 @@ void ArrayBufferView::write(ReadonlyBytes bytes, u32 starting_offset)
BufferSource::~BufferSource() = default; BufferSource::~BufferSource() = default;
// https://webidl.spec.whatwg.org/#buffersource-detached
bool is_buffer_source_detached(JS::Value const& buffer_source)
{
// A buffer source type instance bufferSource is detached if the following steps return true:
// 1. Let jsArrayBuffer be the result of converting bufferSource to a JavaScript value.
// 2. If jsArrayBuffer has a [[ViewedArrayBuffer]] internal slot, then set jsArrayBuffer to jsArrayBuffer.[[ViewedArrayBuffer]].
if (!buffer_source.is_object())
return false;
JS::ArrayBuffer const* js_array_buffer = nullptr;
auto const& array_buffer_object = buffer_source.as_object();
if (auto const* array_buffer = as_if<JS::ArrayBuffer>(array_buffer_object)) {
js_array_buffer = array_buffer;
} else if (auto const* typed_array_base = as_if<JS::TypedArrayBase>(array_buffer_object)) {
js_array_buffer = typed_array_base->viewed_array_buffer();
} else if (auto const* data_view = as_if<JS::DataView>(array_buffer_object)) {
js_array_buffer = data_view->viewed_array_buffer();
} else {
return false;
}
// 3. Return IsDetachedBuffer(jsArrayBuffer).
return js_array_buffer->is_detached();
}
} }

View file

@ -14,6 +14,8 @@
namespace Web::WebIDL { namespace Web::WebIDL {
bool is_buffer_source_detached(JS::Value const&);
using BufferableObject = Variant< using BufferableObject = Variant<
GC::Ref<JS::TypedArrayBase>, GC::Ref<JS::TypedArrayBase>,
GC::Ref<JS::DataView>, GC::Ref<JS::DataView>,