LibWeb/IDB: Implement generate_a_key

This commit is contained in:
stelar7 2025-04-11 11:25:56 +02:00 committed by Andrew Kaster
parent f8b09148be
commit dbe0db0cab
Notes: github-actions[bot] 2025-04-23 18:37:33 +00:00
3 changed files with 33 additions and 2 deletions

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/Math.h>
#include <AK/QuickSort.h> #include <AK/QuickSort.h>
#include <LibJS/Runtime/AbstractOperations.h> #include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Array.h> #include <LibJS/Runtime/Array.h>
@ -37,6 +38,12 @@
namespace Web::IndexedDB { namespace Web::IndexedDB {
#if defined(AK_COMPILER_CLANG)
# define MAX_KEY_GENERATOR_VALUE AK::exp2(53.)
#else
constexpr double const MAX_KEY_GENERATOR_VALUE { __builtin_exp2(53) };
#endif
// https://w3c.github.io/IndexedDB/#open-a-database-connection // https://w3c.github.io/IndexedDB/#open-a-database-connection
WebIDL::ExceptionOr<GC::Ref<IDBDatabase>> open_a_database_connection(JS::Realm& realm, StorageAPI::StorageKey storage_key, String name, Optional<u64> maybe_version, GC::Ref<IDBRequest> request) WebIDL::ExceptionOr<GC::Ref<IDBDatabase>> open_a_database_connection(JS::Realm& realm, StorageAPI::StorageKey storage_key, String name, Optional<u64> maybe_version, GC::Ref<IDBRequest> request)
{ {
@ -1208,4 +1215,24 @@ GC::Ref<IDBRequest> asynchronously_execute_a_request(JS::Realm& realm, IDBReques
return request; return request;
} }
// https://w3c.github.io/IndexedDB/#generate-a-key
ErrorOr<u64> generate_a_key(GC::Ref<ObjectStore> store)
{
// 1. Let generator be stores key generator.
auto generator = store->key_generator().value();
// 2. Let key be generators current number.
auto key = generator.current_number();
// 3. If key is greater than 2^53 (9007199254740992), then return failure.
if (key > static_cast<u64>(MAX_KEY_GENERATOR_VALUE))
return Error::from_string_literal("Key is greater than 2^53");
// 4. Increase generators current number by 1.
generator.increment(1);
// 5. Return key.
return key;
}
} }

View file

@ -36,5 +36,6 @@ bool check_that_a_key_could_be_injected_into_a_value(JS::Realm&, JS::Value, KeyP
void fire_an_error_event(JS::Realm&, GC::Ref<IDBRequest>); void fire_an_error_event(JS::Realm&, GC::Ref<IDBRequest>);
void fire_a_success_event(JS::Realm&, GC::Ref<IDBRequest>); void fire_a_success_event(JS::Realm&, GC::Ref<IDBRequest>);
GC::Ref<IDBRequest> asynchronously_execute_a_request(JS::Realm&, IDBRequestSource, GC::Ref<GC::Function<WebIDL::ExceptionOr<JS::Value>()>>, GC::Ptr<IDBRequest> = nullptr); GC::Ref<IDBRequest> asynchronously_execute_a_request(JS::Realm&, IDBRequestSource, GC::Ref<GC::Function<WebIDL::ExceptionOr<JS::Value>()>>, GC::Ptr<IDBRequest> = nullptr);
ErrorOr<u64> generate_a_key(GC::Ref<ObjectStore>);
} }

View file

@ -13,13 +13,16 @@ namespace Web::IndexedDB {
// https://w3c.github.io/IndexedDB/#key-generator-construct // https://w3c.github.io/IndexedDB/#key-generator-construct
class KeyGenerator { class KeyGenerator {
public: public:
[[nodiscard]] u64 current_number() const { return m_current_number; }
void increment(u64 amount) { m_current_number += amount; }
void set(u64 value) { m_current_number = value; }
private: private:
// A key generator has a current number. // A key generator has a current number.
// The current number is always a positive integer less than or equal to 2^53 (9007199254740992) + 1. // The current number is always a positive integer less than or equal to 2^53 (9007199254740992) + 1.
// The initial value of a key generator's current number is 1, set when the associated object store is created. // The initial value of a key generator's current number is 1, set when the associated object store is created.
// The current number is incremented as keys are generated, and may be updated to a specific value by using explicit keys. // The current number is incremented as keys are generated, and may be updated to a specific value by using explicit keys.
// FIXME: Implement support for KeyGenerator in ObjectStore. u64 m_current_number { 1 };
[[maybe_unused]] u64 current_number { 1 };
}; };
} }