From 839ffd45f32b2ad5431b570a5fdf02b386c6717a Mon Sep 17 00:00:00 2001 From: stelar7 Date: Wed, 9 Jul 2025 11:52:44 +0200 Subject: [PATCH] LibWeb/IDB: Implement retrieve_multiple_items_from_an_object_store --- .../LibWeb/IndexedDB/Internal/Algorithms.cpp | 72 +++++++++++++++++++ .../LibWeb/IndexedDB/Internal/Algorithms.h | 8 +++ .../LibWeb/IndexedDB/Internal/ObjectStore.cpp | 14 ++++ .../LibWeb/IndexedDB/Internal/ObjectStore.h | 1 + 4 files changed, 95 insertions(+) diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp index 62fb515e0ae..440e75272f1 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp +++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -1930,6 +1931,77 @@ GC::Ref retrieve_multiple_values_from_an_object_store(JS::Realm& real return list; } +// https://pr-preview.s3.amazonaws.com/w3c/IndexedDB/pull/461.html#retrieve-multiple-items-from-an-object-store +GC::Ref retrieve_multiple_items_from_an_object_store(JS::Realm& realm, GC::Ref store, GC::Ref range, RecordKind kind, Bindings::IDBCursorDirection direction, Optional count) +{ + // 1. If count is not given or is 0 (zero), let count be infinity. + if (count.has_value() && *count == 0) + count = OptionalNone(); + + // 2. Let records an empty list. + GC::ConservativeVector records(realm.heap()); + + // 3. If direction is "next" or "nextunique", set records to the first count of store’s list of records whose key is in range. + if (direction == Bindings::IDBCursorDirection::Next || direction == Bindings::IDBCursorDirection::Nextunique) { + records.extend(store->first_n_in_range(range, count)); + } + + // 4. If direction is "prev" or "prevunique", set records to the last count of store’s list of records whose key is in range. + if (direction == Bindings::IDBCursorDirection::Prev || direction == Bindings::IDBCursorDirection::Prevunique) { + records.extend(store->last_n_in_range(range, count)); + } + + // 5. Let list be an empty list. + auto list = MUST(JS::Array::create(realm, records.size())); + + // 6. For each record of records, switching on kind: + for (u32 i = 0; i < records.size(); ++i) { + auto& record = records[i]; + + switch (kind) { + case RecordKind::Key: { + // 1. Let key be the result of converting a key to a value with record’s key. + auto key = convert_a_key_to_a_value(realm, record.key); + + // 2. Append key to list. + MUST(list->create_data_property_or_throw(i, key)); + break; + } + case RecordKind::Value: { + // 1. Let serialized be record’s value. + auto serialized = record.value; + + // 2. Let value be ! StructuredDeserialize(serialized, targetRealm). + auto entry = MUST(HTML::structured_deserialize(realm.vm(), serialized, realm)); + + // 3. Append entry to list. + MUST(list->create_data_property_or_throw(i, entry)); + break; + } + case RecordKind::Record: { + // 1. Let key be the record’s key. + auto key = record.key; + + // 2. Let serialized be record’s value. + auto serialized = record.value; + + // 3. Let value be ! StructuredDeserialize(serialized, targetRealm). + auto value = MUST(HTML::structured_deserialize(realm.vm(), serialized, realm)); + + // 4. Let record snapshot be a new record snapshot with its key set to key, value set to value, and primary key set to key. + auto record_snapshot = IDBRecord::create(realm, key, value, key); + + // 5. Append record snapshot to list. + MUST(list->create_data_property_or_throw(i, record_snapshot)); + break; + } + } + } + + // 5. Return list. + return list; +} + // https://w3c.github.io/IndexedDB/#retrieve-multiple-keys-from-an-object-store GC::Ref retrieve_multiple_keys_from_an_object_store(JS::Realm& realm, GC::Ref store, GC::Ref range, Optional count) { diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h index 688faf4597a..03a0d2f41ba 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h +++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -17,6 +18,12 @@ namespace Web::IndexedDB { +enum class RecordKind { + Key, + Value, + Record +}; + using KeyPath = Variant>; using RecordSource = Variant, GC::Ref>; @@ -59,5 +66,6 @@ GC::Ref retrieve_multiple_values_from_an_index(JS::Realm&, GC::Ref>); bool cleanup_indexed_database_transactions(GC::Ref); bool is_a_potentially_valid_key_range(JS::Realm&, JS::Value); +GC::Ref retrieve_multiple_items_from_an_object_store(JS::Realm&, GC::Ref, GC::Ref, RecordKind, Bindings::IDBCursorDirection, Optional); } diff --git a/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.cpp b/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.cpp index 81e31c2338b..9c39bfd5764 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.cpp +++ b/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.cpp @@ -103,4 +103,18 @@ GC::ConservativeVector ObjectStore::first_n_in_range(GC::Ref< return records; } +GC::ConservativeVector ObjectStore::last_n_in_range(GC::Ref range, Optional count) +{ + GC::ConservativeVector records(range->heap()); + for (auto const& record : m_records.in_reverse()) { + if (range->is_in_range(record.key)) + records.append(record); + + if (count.has_value() && records.size() >= *count) + break; + } + + return records; +} + } diff --git a/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.h b/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.h index 0dd0cfba960..e9138263fcc 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.h +++ b/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.h @@ -52,6 +52,7 @@ public: Optional first_in_range(GC::Ref range); void clear_records(); GC::ConservativeVector first_n_in_range(GC::Ref range, Optional count); + GC::ConservativeVector last_n_in_range(GC::Ref range, Optional count); protected: virtual void visit_edges(Visitor&) override;