LibWeb/IDB: Implement IDBIndex::getAll

This commit is contained in:
stelar7 2025-05-13 22:41:18 +02:00 committed by Jelle Raaijmakers
commit 3fa1d1299c
Notes: github-actions[bot] 2025-05-14 15:19:24 +00:00
7 changed files with 80 additions and 1 deletions

View file

@ -1960,4 +1960,35 @@ JS::Value retrieve_a_value_from_an_index(JS::Realm& realm, GC::Ref<Index> index,
return convert_a_key_to_a_value(realm, record->value);
}
// https://w3c.github.io/IndexedDB/#retrieve-multiple-referenced-values-from-an-index
GC::Ref<JS::Array> retrieve_multiple_referenced_values_from_an_index(JS::Realm& realm, GC::Ref<Index> index, GC::Ref<IDBKeyRange> range, Optional<WebIDL::UnsignedLong> 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 be a list containing the first count records in indexs list of records whose key is in range.
auto records = index->first_n_in_range(range, count);
// 3. Let list be an empty list.
auto list = MUST(JS::Array::create(realm, records.size()));
// 4. For each record of records:
for (u32 i = 0; i < records.size(); ++i) {
auto& record = records[i];
// 1. Let serialized be records referenced value.
auto serialized = index->referenced_value(record);
// 2. Let entry 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));
}
// 5. Return list converted to a sequence<any>.
return list;
}
}

View file

@ -53,5 +53,6 @@ GC::Ref<JS::Array> retrieve_multiple_values_from_an_object_store(JS::Realm&, GC:
GC::Ref<JS::Array> retrieve_multiple_keys_from_an_object_store(JS::Realm&, GC::Ref<ObjectStore>, GC::Ref<IDBKeyRange>, Optional<WebIDL::UnsignedLong>);
JS::Value retrieve_a_referenced_value_from_an_index(JS::Realm&, GC::Ref<Index>, GC::Ref<IDBKeyRange>);
JS::Value retrieve_a_value_from_an_index(JS::Realm&, GC::Ref<Index>, GC::Ref<IDBKeyRange>);
GC::Ref<JS::Array> retrieve_multiple_referenced_values_from_an_index(JS::Realm&, GC::Ref<Index>, GC::Ref<IDBKeyRange>, Optional<WebIDL::UnsignedLong>);
}

View file

@ -83,4 +83,18 @@ Optional<IndexRecord&> Index::first_in_range(GC::Ref<IDBKeyRange> range)
});
}
GC::ConservativeVector<IndexRecord> Index::first_n_in_range(GC::Ref<IDBKeyRange> range, Optional<WebIDL::UnsignedLong> count)
{
GC::ConservativeVector<IndexRecord> records(range->heap());
for (auto const& record : m_records) {
if (range->is_in_range(record.key))
records.append(record);
if (count.has_value() && records.size() >= *count)
break;
}
return records;
}
}

View file

@ -42,6 +42,7 @@ public:
[[nodiscard]] bool has_record_with_key(GC::Ref<Key> key);
void clear_records();
Optional<IndexRecord&> first_in_range(GC::Ref<IDBKeyRange> range);
GC::ConservativeVector<IndexRecord> first_n_in_range(GC::Ref<IDBKeyRange> range, Optional<WebIDL::UnsignedLong> count);
HTML::SerializationRecord referenced_value(IndexRecord const& index_record) const;