Invalid return type for key conversion (#39252)

`convert_value_to_key` returns a `ConversionResult` now, so keys can be
considered "Invalid" rather than throwing an exception.

Testing: WPT
Unblocks: #38288

---------

Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
This commit is contained in:
Ashwin Naren 2025-09-11 05:48:18 -07:00 committed by GitHub
parent 19f70dccf6
commit 96592dce44
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 41 additions and 23 deletions

View file

@ -98,8 +98,8 @@ impl IDBFactoryMethods<crate::DomTypeHolder> for IDBFactory {
// https://www.w3.org/TR/IndexedDB-2/#dom-idbfactory-cmp
fn Cmp(&self, cx: SafeJSContext, first: HandleValue, second: HandleValue) -> Fallible<i16> {
let first_key = convert_value_to_key(cx, first, None)?;
let second_key = convert_value_to_key(cx, second, None)?;
let first_key = convert_value_to_key(cx, first, None)?.into_result()?;
let second_key = convert_value_to_key(cx, second, None)?.into_result()?;
let cmp = first_key.partial_cmp(&second_key);
if let Some(cmp) = cmp {
match cmp {

View file

@ -71,7 +71,7 @@ impl IDBKeyRangeMethods<crate::DomTypeHolder> for IDBKeyRange {
global: &GlobalScope,
value: HandleValue,
) -> Fallible<DomRoot<IDBKeyRange>> {
let key = convert_value_to_key(cx, value, None)?;
let key = convert_value_to_key(cx, value, None)?.into_result()?;
let inner = IndexedDBKeyRange::only(key);
Ok(IDBKeyRange::new(global, inner, CanGc::note()))
}
@ -83,7 +83,7 @@ impl IDBKeyRangeMethods<crate::DomTypeHolder> for IDBKeyRange {
lower: HandleValue,
open: bool,
) -> Fallible<DomRoot<IDBKeyRange>> {
let key = convert_value_to_key(cx, lower, None)?;
let key = convert_value_to_key(cx, lower, None)?.into_result()?;
let inner = IndexedDBKeyRange::lower_bound(key, open);
Ok(IDBKeyRange::new(global, inner, CanGc::note()))
}
@ -95,7 +95,7 @@ impl IDBKeyRangeMethods<crate::DomTypeHolder> for IDBKeyRange {
upper: HandleValue,
open: bool,
) -> Fallible<DomRoot<IDBKeyRange>> {
let key = convert_value_to_key(cx, upper, None)?;
let key = convert_value_to_key(cx, upper, None)?.into_result()?;
let inner = IndexedDBKeyRange::upper_bound(key, open);
Ok(IDBKeyRange::new(global, inner, CanGc::note()))
}
@ -112,12 +112,12 @@ impl IDBKeyRangeMethods<crate::DomTypeHolder> for IDBKeyRange {
// Step 1. Let lowerKey be the result of running the steps to convert a value to a key with
// lower. Rethrow any exceptions.
// Step 2. If lowerKey is invalid, throw a "DataError" DOMException.
let lower_key = convert_value_to_key(cx, lower, None)?;
let lower_key = convert_value_to_key(cx, lower, None)?.into_result()?;
// Step 3. Let upperKey be the result of running the steps to convert a value to a key with
// upper. Rethrow any exceptions.
// Step 4. If upperKey is invalid, throw a "DataError" DOMException.
let upper_key = convert_value_to_key(cx, upper, None)?;
let upper_key = convert_value_to_key(cx, upper, None)?.into_result()?;
// Step 5. If lowerKey is greater than upperKey, throw a "DataError" DOMException.
if lower_key > upper_key {
@ -134,7 +134,7 @@ impl IDBKeyRangeMethods<crate::DomTypeHolder> for IDBKeyRange {
// https://www.w3.org/TR/IndexedDB-2/#dom-idbkeyrange-_includes
fn Includes(&self, cx: SafeJSContext, value: HandleValue) -> Fallible<bool> {
let key = convert_value_to_key(cx, value, None)?;
let key = convert_value_to_key(cx, value, None)?.into_result()?;
if self.inner.contains(&key) {
return Ok(true);
}

View file

@ -211,7 +211,7 @@ impl IDBObjectStore {
let serialized_key: Option<IndexedDBKeyType>;
if !key.is_undefined() {
serialized_key = Some(convert_value_to_key(cx, key, None)?);
serialized_key = Some(convert_value_to_key(cx, key, None)?.into_result()?);
} else {
// Step 11: We should use in-line keys instead
if let Some(Ok(ExtractionResult::Key(kpk))) = self
@ -287,7 +287,7 @@ impl IDBObjectStoreMethods<crate::DomTypeHolder> for IDBObjectStore {
// Step 6
// TODO: Convert to key range instead
let serialized_query = convert_value_to_key(cx, query, None);
let serialized_query = convert_value_to_key(cx, query, None)?.into_result();
// Step 7. Let operation be an algorithm to run delete records from an object store with store and range.
// Stpe 8. Return the result (an IDBRequest) of running asynchronously execute a request with this and operation.
let (sender, receiver) = indexed_db::create_channel(self.global());