LibWeb/IDB: Adjust return type of convert_a_value_to_a_key

This commit is contained in:
stelar7 2025-04-19 17:08:59 +02:00 committed by Andrew Kaster
commit 8bfbcf6d9b
Notes: github-actions[bot] 2025-04-23 18:38:48 +00:00
4 changed files with 15 additions and 30 deletions

View file

@ -105,14 +105,14 @@ WebIDL::ExceptionOr<GC::Ref<IDBOpenDBRequest>> IDBFactory::open(String const& na
WebIDL::ExceptionOr<i8> IDBFactory::cmp(JS::Value first, JS::Value second)
{
// 1. Let a be the result of converting a value to a key with first. Rethrow any exceptions.
auto a = convert_a_value_to_a_key(realm(), first);
auto a = TRY(convert_a_value_to_a_key(realm(), first));
// 2. If a is invalid, throw a "DataError" DOMException.
if (a.is_error())
return WebIDL::DataError::create(realm(), "Failed to convert a value to a key"_string);
// 3. Let b be the result of converting a value to a key with second. Rethrow any exceptions.
auto b = convert_a_value_to_a_key(realm(), second);
auto b = TRY(convert_a_value_to_a_key(realm(), second));
// 4. If b is invalid, throw a "DataError" DOMException.
if (b.is_error())

View file

@ -64,7 +64,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBKeyRange>> IDBKeyRange::only(JS::VM& vm, JS::Valu
auto& realm = *vm.current_realm();
// 1. Let key be the result of converting a value to a key with value. Rethrow any exceptions.
auto maybe_key = convert_a_value_to_a_key(realm, value);
auto maybe_key = TRY(convert_a_value_to_a_key(realm, value));
// 2. If key is invalid, throw a "DataError" DOMException.
if (maybe_key.is_error())
@ -82,7 +82,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBKeyRange>> IDBKeyRange::lower_bound(JS::VM& vm, J
auto& realm = *vm.current_realm();
// 1. Let lowerKey be the result of converting a value to a key with lower. Rethrow any exceptions.
auto lower_key = convert_a_value_to_a_key(realm, lower);
auto lower_key = TRY(convert_a_value_to_a_key(realm, lower));
// 2. If lowerKey is invalid, throw a "DataError" DOMException.
if (lower_key.is_error())
@ -98,7 +98,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBKeyRange>> IDBKeyRange::upper_bound(JS::VM& vm, J
auto& realm = *vm.current_realm();
// 1. Let upperKey be the result of converting a value to a key with upper. Rethrow any exceptions.
auto upper_key = convert_a_value_to_a_key(realm, upper);
auto upper_key = TRY(convert_a_value_to_a_key(realm, upper));
// 2. If upperKey is invalid, throw a "DataError" DOMException.
if (upper_key.is_error())
@ -114,14 +114,14 @@ WebIDL::ExceptionOr<GC::Ref<IDBKeyRange>> IDBKeyRange::bound(JS::VM& vm, JS::Val
auto& realm = *vm.current_realm();
// 1. Let lowerKey be the result of converting a value to a key with lower. Rethrow any exceptions.
auto lower_key = convert_a_value_to_a_key(realm, lower);
auto lower_key = TRY(convert_a_value_to_a_key(realm, lower));
// 2. If lowerKey is invalid, throw a "DataError" DOMException.
if (lower_key.is_error())
return WebIDL::DataError::create(realm, "Value is invalid"_string);
// 3. Let upperKey be the result of converting a value to a key with upper. Rethrow any exceptions.
auto upper_key = convert_a_value_to_a_key(realm, upper);
auto upper_key = TRY(convert_a_value_to_a_key(realm, upper));
// 4. If upperKey is invalid, throw a "DataError" DOMException.
if (upper_key.is_error())
@ -141,7 +141,7 @@ WebIDL::ExceptionOr<bool> IDBKeyRange::includes(JS::Value key)
auto& realm = this->realm();
// 1. Let k be the result of converting a value to a key with key. Rethrow any exceptions.
auto k = convert_a_value_to_a_key(realm, key);
auto k = TRY(convert_a_value_to_a_key(realm, key));
// 2. If k is invalid, throw a "DataError" DOMException.
if (k.is_error())

View file

@ -8,6 +8,7 @@
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/DataView.h>
#include <LibJS/Runtime/Date.h>
#include <LibJS/Runtime/TypedArray.h>
@ -194,7 +195,7 @@ bool fire_a_version_change_event(JS::Realm& realm, FlyString const& event_name,
}
// https://w3c.github.io/IndexedDB/#convert-value-to-key
ErrorOr<GC::Ref<Key>> convert_a_value_to_a_key(JS::Realm& realm, JS::Value input, Vector<JS::Value> seen)
WebIDL::ExceptionOr<ErrorOr<GC::Ref<Key>>> convert_a_value_to_a_key(JS::Realm& realm, JS::Value input, Vector<JS::Value> seen)
{
// 1. If seen was not given, then let seen be a new empty set.
// NOTE: This is handled by the caller.
@ -256,11 +257,7 @@ ErrorOr<GC::Ref<Key>> convert_a_value_to_a_key(JS::Realm& realm, JS::Value input
if (input.is_object() && is<JS::Array>(input.as_object())) {
// 1. Let len be ? ToLength( ? Get(input, "length")).
auto maybe_length = length_of_array_like(realm.vm(), input.as_object());
if (maybe_length.is_error())
return Error::from_string_literal("Failed to get length of array-like object");
auto length = maybe_length.release_value();
auto length = TRY(length_of_array_like(realm.vm(), input.as_object()));
// 2. Append input to seen.
seen.append(input);
@ -274,30 +271,19 @@ ErrorOr<GC::Ref<Key>> convert_a_value_to_a_key(JS::Realm& realm, JS::Value input
// 5. While index is less than len:
while (index < length) {
// 1. Let hop be ? HasOwnProperty(input, index).
auto maybe_hop = input.as_object().has_own_property(index);
if (maybe_hop.is_error())
return Error::from_string_literal("Failed to check if array-like object has property");
auto hop = maybe_hop.release_value();
auto hop = TRY(input.as_object().has_own_property(index));
// 2. If hop is false, return invalid.
if (!hop)
return Error::from_string_literal("Array-like object has no property");
// 3. Let entry be ? Get(input, index).
auto maybe_entry = input.as_object().get(index);
if (maybe_entry.is_error())
return Error::from_string_literal("Failed to get property of array-like object");
auto entry = TRY(input.as_object().get(index));
// 4. Let key be the result of converting a value to a key with arguments entry and seen.
auto maybe_key = convert_a_value_to_a_key(realm, maybe_entry.release_value(), seen);
// 5. ReturnIfAbrupt(key).
// 6. If key is invalid abort these steps and return invalid.
if (maybe_key.is_error())
return maybe_key.release_error();
auto key = maybe_key.release_value();
auto key = TRY(TRY(convert_a_value_to_a_key(realm, entry, seen)));
// 7. Append key to keys.
keys.append(key);
@ -311,7 +297,6 @@ ErrorOr<GC::Ref<Key>> convert_a_value_to_a_key(JS::Realm& realm, JS::Value input
}
// - Otherwise
// 1. Return invalid.
return Error::from_string_literal("Unknown key type");
}

View file

@ -19,7 +19,7 @@ using KeyPath = Variant<String, Vector<String>>;
WebIDL::ExceptionOr<GC::Ref<IDBDatabase>> open_a_database_connection(JS::Realm&, StorageAPI::StorageKey, String, Optional<u64>, GC::Ref<IDBRequest>);
bool fire_a_version_change_event(JS::Realm&, FlyString const&, GC::Ref<DOM::EventTarget>, u64, Optional<u64>);
ErrorOr<GC::Ref<Key>> convert_a_value_to_a_key(JS::Realm&, JS::Value, Vector<JS::Value> = {});
WebIDL::ExceptionOr<ErrorOr<GC::Ref<Key>>> convert_a_value_to_a_key(JS::Realm&, JS::Value, Vector<JS::Value> = {});
void close_a_database_connection(GC::Ref<IDBDatabase>, bool forced = false);
GC::Ref<IDBTransaction> upgrade_a_database(JS::Realm&, GC::Ref<IDBDatabase>, u64, GC::Ref<IDBRequest>);
WebIDL::ExceptionOr<u64> delete_a_database(JS::Realm&, StorageAPI::StorageKey, String, GC::Ref<IDBRequest>);