mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-08 10:01:53 +00:00
LibWeb/IDB: Implement generate_a_key
This commit is contained in:
parent
f8b09148be
commit
dbe0db0cab
Notes:
github-actions[bot]
2025-04-23 18:37:33 +00:00
Author: https://github.com/stelar7
Commit: dbe0db0cab
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4317
Reviewed-by: https://github.com/ADKaster ✅
Reviewed-by: https://github.com/kennethmyhra ✅
3 changed files with 33 additions and 2 deletions
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Math.h>
|
||||
#include <AK/QuickSort.h>
|
||||
#include <LibJS/Runtime/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/Array.h>
|
||||
|
@ -37,6 +38,12 @@
|
|||
|
||||
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
|
||||
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;
|
||||
}
|
||||
|
||||
// https://w3c.github.io/IndexedDB/#generate-a-key
|
||||
ErrorOr<u64> generate_a_key(GC::Ref<ObjectStore> store)
|
||||
{
|
||||
// 1. Let generator be store’s key generator.
|
||||
auto generator = store->key_generator().value();
|
||||
|
||||
// 2. Let key be generator’s 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 generator’s current number by 1.
|
||||
generator.increment(1);
|
||||
|
||||
// 5. Return key.
|
||||
return key;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue