mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-28 19:59:17 +00:00
LibWeb/IDB: Introduce an Invalid KeyType
This cleans up the code around failure/invalid/exception a bit
This commit is contained in:
parent
61384473ca
commit
facfcd87c2
Notes:
github-actions[bot]
2025-04-28 09:32:57 +00:00
Author: https://github.com/stelar7
Commit: facfcd87c2
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4476
Reviewed-by: https://github.com/gmta ✅
7 changed files with 68 additions and 56 deletions
|
@ -64,14 +64,12 @@ 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 = TRY(convert_a_value_to_a_key(realm, value));
|
||||
auto key = TRY(convert_a_value_to_a_key(realm, value));
|
||||
|
||||
// 2. If key is invalid, throw a "DataError" DOMException.
|
||||
if (maybe_key.is_error())
|
||||
if (key->is_invalid())
|
||||
return WebIDL::DataError::create(realm, "Value is invalid"_string);
|
||||
|
||||
auto key = maybe_key.release_value();
|
||||
|
||||
// 3. Create and return a new key range containing only key.
|
||||
return IDBKeyRange::create(realm, key, key, false, false);
|
||||
}
|
||||
|
@ -82,14 +80,14 @@ 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 = TRY(convert_a_value_to_a_key(realm, lower));
|
||||
auto key = TRY(convert_a_value_to_a_key(realm, lower));
|
||||
|
||||
// 2. If lowerKey is invalid, throw a "DataError" DOMException.
|
||||
if (lower_key.is_error())
|
||||
if (key->is_invalid())
|
||||
return WebIDL::DataError::create(realm, "Value is invalid"_string);
|
||||
|
||||
// 3. Create and return a new key range with lower bound set to lowerKey, lower open flag set to open, upper bound set to null, and upper open flag set to true.
|
||||
return IDBKeyRange::create(realm, lower_key.release_value(), {}, open, true);
|
||||
return IDBKeyRange::create(realm, key, {}, open, true);
|
||||
}
|
||||
|
||||
// https://w3c.github.io/IndexedDB/#dom-idbkeyrange-upperbound
|
||||
|
@ -98,14 +96,14 @@ 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 = TRY(convert_a_value_to_a_key(realm, upper));
|
||||
auto key = TRY(convert_a_value_to_a_key(realm, upper));
|
||||
|
||||
// 2. If upperKey is invalid, throw a "DataError" DOMException.
|
||||
if (upper_key.is_error())
|
||||
if (key->is_invalid())
|
||||
return WebIDL::DataError::create(realm, "Value is invalid"_string);
|
||||
|
||||
// 3. Create and return a new key range with lower bound set to null, lower open flag set to true, upper bound set to upperKey, and upper open flag set to open.
|
||||
return IDBKeyRange::create(realm, {}, upper_key.release_value(), true, open);
|
||||
return IDBKeyRange::create(realm, {}, key, true, open);
|
||||
}
|
||||
|
||||
// https://w3c.github.io/IndexedDB/#dom-idbkeyrange-bound
|
||||
|
@ -117,22 +115,22 @@ WebIDL::ExceptionOr<GC::Ref<IDBKeyRange>> IDBKeyRange::bound(JS::VM& vm, JS::Val
|
|||
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())
|
||||
if (lower_key->is_invalid())
|
||||
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 = TRY(convert_a_value_to_a_key(realm, upper));
|
||||
|
||||
// 4. If upperKey is invalid, throw a "DataError" DOMException.
|
||||
if (upper_key.is_error())
|
||||
if (upper_key->is_invalid())
|
||||
return WebIDL::DataError::create(realm, "Value is invalid"_string);
|
||||
|
||||
// 5. If lowerKey is greater than upperKey, throw a "DataError" DOMException.
|
||||
if (Key::less_than(upper_key.release_value(), lower_key.release_value()))
|
||||
if (Key::less_than(upper_key, lower_key))
|
||||
return WebIDL::DataError::create(realm, "Lower key is greater than upper key"_string);
|
||||
|
||||
// 6. Create and return a new key range with lower bound set to lowerKey, lower open flag set to lowerOpen, upper bound set to upperKey and upper open flag set to upperOpen.
|
||||
return IDBKeyRange::create(realm, lower_key.release_value(), upper_key.release_value(), lower_open, upper_open);
|
||||
return IDBKeyRange::create(realm, lower_key, upper_key, lower_open, upper_open);
|
||||
}
|
||||
|
||||
// https://w3c.github.io/IndexedDB/#dom-idbkeyrange-includes
|
||||
|
@ -144,11 +142,11 @@ WebIDL::ExceptionOr<bool> IDBKeyRange::includes(JS::Value 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())
|
||||
if (k->is_invalid())
|
||||
return WebIDL::DataError::create(realm, "Value is invalid"_string);
|
||||
|
||||
// 3. Return true if k is in this range, and false otherwise.
|
||||
return is_in_range(k.release_value());
|
||||
return is_in_range(k);
|
||||
}
|
||||
|
||||
// https://w3c.github.io/IndexedDB/#dom-idbkeyrange-lower
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue