script: Implement converting values to indexeddb key ranges (#38278)

Implements
https://www.w3.org/TR/IndexedDB-2/#convert-a-value-to-a-key-range. Part
of #38187.

Testing: Currently unused, doesn't change anything
Fixes: None

Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
This commit is contained in:
Ashwin Naren 2025-07-26 18:50:52 +05:30 committed by GitHub
parent f8d6c5646c
commit e1bda86861
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 4 deletions

View file

@ -35,7 +35,6 @@ impl IDBKeyRange {
reflect_dom_object(Box::new(IDBKeyRange::new_inherited(inner)), global, can_gc) reflect_dom_object(Box::new(IDBKeyRange::new_inherited(inner)), global, can_gc)
} }
#[expect(unused)]
pub fn inner(&self) -> &IndexedDBKeyRange { pub fn inner(&self) -> &IndexedDBKeyRange {
&self.inner &self.inner
} }

View file

@ -13,8 +13,8 @@ use js::jsapi::{
}; };
use js::jsval::{DoubleValue, UndefinedValue}; use js::jsval::{DoubleValue, UndefinedValue};
use js::rust::{HandleValue, MutableHandleValue}; use js::rust::{HandleValue, MutableHandleValue};
use net_traits::indexeddb_thread::IndexedDBKeyType; use net_traits::indexeddb_thread::{IndexedDBKeyRange, IndexedDBKeyType};
use script_bindings::conversions::SafeToJSValConvertible; use script_bindings::conversions::{SafeToJSValConvertible, root_from_object};
use script_bindings::str::DOMString; use script_bindings::str::DOMString;
use crate::dom::bindings::codegen::UnionTypes::StringOrStringSequence as StrOrStringSequence; use crate::dom::bindings::codegen::UnionTypes::StringOrStringSequence as StrOrStringSequence;
@ -22,6 +22,7 @@ use crate::dom::bindings::conversions::jsstring_to_str;
use crate::dom::bindings::error::Error; use crate::dom::bindings::error::Error;
use crate::dom::bindings::import::module::SafeJSContext; use crate::dom::bindings::import::module::SafeJSContext;
use crate::dom::bindings::structuredclone; use crate::dom::bindings::structuredclone;
use crate::dom::idbkeyrange::IDBKeyRange;
use crate::dom::idbobjectstore::KeyPath; use crate::dom::idbobjectstore::KeyPath;
// https://www.w3.org/TR/IndexedDB-2/#convert-key-to-value // https://www.w3.org/TR/IndexedDB-2/#convert-key-to-value
@ -76,8 +77,8 @@ pub fn is_valid_key_path(key_path: &StrOrStringSequence) -> bool {
} }
} }
#[allow(unsafe_code)]
// https://www.w3.org/TR/IndexedDB-2/#convert-value-to-key // https://www.w3.org/TR/IndexedDB-2/#convert-value-to-key
#[allow(unsafe_code)]
pub fn convert_value_to_key( pub fn convert_value_to_key(
cx: SafeJSContext, cx: SafeJSContext,
input: HandleValue, input: HandleValue,
@ -144,6 +145,33 @@ pub fn convert_value_to_key(
Err(Error::Data) Err(Error::Data)
} }
// https://www.w3.org/TR/IndexedDB-2/#convert-a-value-to-a-key-range
#[allow(unsafe_code)]
#[expect(unused)]
pub fn convert_value_to_key_range(
cx: SafeJSContext,
input: HandleValue,
null_disallowed: Option<bool>,
) -> Result<IndexedDBKeyRange, Error> {
let null_disallowed = null_disallowed.unwrap_or(false);
// Step 1.
if input.is_object() {
rooted!(in(*cx) let object = input.to_object());
unsafe {
if let Ok(obj) = root_from_object::<IDBKeyRange>(object.get(), *cx) {
let obj = obj.inner().clone();
return Ok(obj);
}
}
}
// Step 2.
if (input.get().is_undefined() || input.get().is_null()) && null_disallowed {
return Err(Error::Data);
}
let key = convert_value_to_key(cx, input, None)?;
Ok(IndexedDBKeyRange::only(key))
}
// https://www.w3.org/TR/IndexedDB-2/#evaluate-a-key-path-on-a-value // https://www.w3.org/TR/IndexedDB-2/#evaluate-a-key-path-on-a-value
#[allow(unsafe_code)] #[allow(unsafe_code)]
pub fn evaluate_key_path_on_value( pub fn evaluate_key_path_on_value(