From 47450bc15c31253dda1e821f89c9d29003d32a98 Mon Sep 17 00:00:00 2001 From: stelar7 Date: Tue, 13 May 2025 22:21:57 +0200 Subject: [PATCH] LibWeb/IDB: Implement IDBIndex::getKey --- Libraries/LibWeb/IndexedDB/IDBIndex.cpp | 31 +++++++++++++++++++ Libraries/LibWeb/IndexedDB/IDBIndex.h | 1 + Libraries/LibWeb/IndexedDB/IDBIndex.idl | 2 +- .../LibWeb/IndexedDB/Internal/Algorithms.cpp | 14 +++++++++ .../LibWeb/IndexedDB/Internal/Algorithms.h | 1 + 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/Libraries/LibWeb/IndexedDB/IDBIndex.cpp b/Libraries/LibWeb/IndexedDB/IDBIndex.cpp index e0dd15b7910..e823e4a6a0b 100644 --- a/Libraries/LibWeb/IndexedDB/IDBIndex.cpp +++ b/Libraries/LibWeb/IndexedDB/IDBIndex.cpp @@ -172,4 +172,35 @@ WebIDL::ExceptionOr> IDBIndex::get(JS::Value query) return result; } +// https://w3c.github.io/IndexedDB/#dom-idbindex-getkey +WebIDL::ExceptionOr> IDBIndex::get_key(JS::Value query) +{ + auto& realm = this->realm(); + + // 1. Let transaction be this’s transaction. + auto transaction = this->transaction(); + + // 2. Let index be this’s index. + auto index = this->index(); + + // FIXME: 3. If index or index’s object store has been deleted, throw an "InvalidStateError" DOMException. + + // 4. If transaction’s state is not active, then throw a "TransactionInactiveError" DOMException. + if (transaction->state() != IDBTransaction::TransactionState::Active) + return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while getting key"_string); + + // 5. Let range be the result of converting a value to a key range with query and true. Rethrow any exceptions. + auto range = TRY(convert_a_value_to_a_key_range(realm, query, true)); + + // 6. Let operation be an algorithm to run retrieve a value from an index with index and range. + auto operation = GC::Function()>::create(realm.heap(), [&realm, index, range] -> WebIDL::ExceptionOr { + return retrieve_a_value_from_an_index(realm, index, range); + }); + + // 7. Return the result (an IDBRequest) of running asynchronously execute a request with this and operation. + auto result = asynchronously_execute_a_request(realm, GC::Ref(*this), operation); + dbgln_if(IDB_DEBUG, "Executing request for get key with uuid {}", result->uuid()); + return result; +} + } diff --git a/Libraries/LibWeb/IndexedDB/IDBIndex.h b/Libraries/LibWeb/IndexedDB/IDBIndex.h index 6579437e13b..bb52115f31d 100644 --- a/Libraries/LibWeb/IndexedDB/IDBIndex.h +++ b/Libraries/LibWeb/IndexedDB/IDBIndex.h @@ -30,6 +30,7 @@ public: bool unique() const { return m_index->unique(); } [[nodiscard]] WebIDL::ExceptionOr> get(JS::Value); + [[nodiscard]] WebIDL::ExceptionOr> get_key(JS::Value); [[nodiscard]] WebIDL::ExceptionOr> open_cursor(JS::Value, Bindings::IDBCursorDirection = Bindings::IDBCursorDirection::Next); // The transaction of an index handle is the transaction of its associated object store handle. diff --git a/Libraries/LibWeb/IndexedDB/IDBIndex.idl b/Libraries/LibWeb/IndexedDB/IDBIndex.idl index c2f2b45c670..23126d9a6b5 100644 --- a/Libraries/LibWeb/IndexedDB/IDBIndex.idl +++ b/Libraries/LibWeb/IndexedDB/IDBIndex.idl @@ -8,7 +8,7 @@ interface IDBIndex { readonly attribute boolean multiEntry; readonly attribute boolean unique; [NewObject] IDBRequest get(any query); - [FIXME, NewObject] IDBRequest getKey(any query); + [NewObject] IDBRequest getKey(any query); [FIXME, NewObject] IDBRequest getAll(optional any query, optional [EnforceRange] unsigned long count); [FIXME, NewObject] IDBRequest getAllKeys(optional any query, optional [EnforceRange] unsigned long count); [FIXME, NewObject] IDBRequest count(optional any query); diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp index 4da8ec07058..84dfbbb0921 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp +++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp @@ -1946,4 +1946,18 @@ JS::Value retrieve_a_referenced_value_from_an_index(JS::Realm& realm, GC::Ref index, GC::Ref range) +{ + // 1. Let record be the first record in index’s list of records whose key is in range, if any. + auto record = index->first_in_range(range); + + // 2. If record was not found, return undefined. + if (!record.has_value()) + return JS::js_undefined(); + + // 3. Return the result of converting a key to a value with record’s value. + return convert_a_key_to_a_value(realm, record->value); +} + } diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h index 0bc4f76fdbc..31403641941 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h +++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h @@ -52,5 +52,6 @@ JS::Value retrieve_a_key_from_an_object_store(JS::Realm&, GC::Ref, GC::Ref retrieve_multiple_values_from_an_object_store(JS::Realm&, GC::Ref, GC::Ref, Optional); GC::Ref retrieve_multiple_keys_from_an_object_store(JS::Realm&, GC::Ref, GC::Ref, Optional); JS::Value retrieve_a_referenced_value_from_an_index(JS::Realm&, GC::Ref, GC::Ref); +JS::Value retrieve_a_value_from_an_index(JS::Realm&, GC::Ref, GC::Ref); }