From e1bda868615d12c267db3da8d67abea01215ebf0 Mon Sep 17 00:00:00 2001 From: Ashwin Naren Date: Sat, 26 Jul 2025 18:50:52 +0530 Subject: [PATCH] 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 --- components/script/dom/idbkeyrange.rs | 1 - components/script/indexed_db.rs | 34 +++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/components/script/dom/idbkeyrange.rs b/components/script/dom/idbkeyrange.rs index e9b951c5512..4bfdaa66466 100644 --- a/components/script/dom/idbkeyrange.rs +++ b/components/script/dom/idbkeyrange.rs @@ -35,7 +35,6 @@ impl IDBKeyRange { reflect_dom_object(Box::new(IDBKeyRange::new_inherited(inner)), global, can_gc) } - #[expect(unused)] pub fn inner(&self) -> &IndexedDBKeyRange { &self.inner } diff --git a/components/script/indexed_db.rs b/components/script/indexed_db.rs index d9538eb8548..88ac90e60c9 100644 --- a/components/script/indexed_db.rs +++ b/components/script/indexed_db.rs @@ -13,8 +13,8 @@ use js::jsapi::{ }; use js::jsval::{DoubleValue, UndefinedValue}; use js::rust::{HandleValue, MutableHandleValue}; -use net_traits::indexeddb_thread::IndexedDBKeyType; -use script_bindings::conversions::SafeToJSValConvertible; +use net_traits::indexeddb_thread::{IndexedDBKeyRange, IndexedDBKeyType}; +use script_bindings::conversions::{SafeToJSValConvertible, root_from_object}; use script_bindings::str::DOMString; 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::import::module::SafeJSContext; use crate::dom::bindings::structuredclone; +use crate::dom::idbkeyrange::IDBKeyRange; use crate::dom::idbobjectstore::KeyPath; // 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 +#[allow(unsafe_code)] pub fn convert_value_to_key( cx: SafeJSContext, input: HandleValue, @@ -144,6 +145,33 @@ pub fn convert_value_to_key( 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, +) -> Result { + 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::(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 #[allow(unsafe_code)] pub fn evaluate_key_path_on_value(