script: Update indexeddb serialization (#38269)

- Dates are now serialized as f64s (like how firefox does it)
- Array buffers are serialized with structured cloning (this is not the
most correct/efficent way to serialize them, however it is currently
better than not supporting array buffers at all)
- Array types are still unimplemented.

Testing: WPT
Fixes: Some panicking

---------

Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
This commit is contained in:
Ashwin Naren 2025-07-25 16:41:22 +05:30 committed by GitHub
parent 165ede59cd
commit 3f1e170410
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 276 additions and 86 deletions

View file

@ -24,6 +24,7 @@ use crate::dom::bindings::import::module::SafeJSContext;
use crate::dom::bindings::structuredclone; use crate::dom::bindings::structuredclone;
use crate::dom::idbobjectstore::KeyPath; use crate::dom::idbobjectstore::KeyPath;
// https://www.w3.org/TR/IndexedDB-2/#convert-key-to-value
#[allow(unsafe_code)] #[allow(unsafe_code)]
pub fn key_type_to_jsval( pub fn key_type_to_jsval(
cx: SafeJSContext, cx: SafeJSContext,
@ -92,9 +93,11 @@ pub fn convert_value_to_key(
// as it does not implement PartialEq // as it does not implement PartialEq
// Step 3 // Step 3
// FIXME:(arihant2math) Accept buffer, array and date as well // FIXME:(arihant2math) Accept array as well
if input.is_number() { if input.is_number() {
// FIXME:(arihant2math) check for NaN if input.to_number().is_nan() {
return Err(Error::Data);
}
return Ok(IndexedDBKeyType::Number(input.to_number())); return Ok(IndexedDBKeyType::Number(input.to_number()));
} }
@ -114,20 +117,26 @@ pub fn convert_value_to_key(
} }
if let ESClass::Date = built_in_class { if let ESClass::Date = built_in_class {
// FIXME:(arihant2math) implement it the correct way let mut f = f64::NAN;
let key = structuredclone::write(cx, input, None).expect("Could not serialize key"); if !js::jsapi::DateGetMsecSinceEpoch(*cx, object.handle().into(), &mut f) {
return Ok(IndexedDBKeyType::Date(key.serialized.clone())); return Err(Error::Data);
}
if f.is_nan() {
return Err(Error::Data);
}
return Ok(IndexedDBKeyType::Date(f));
} }
if IsArrayBufferObject(*object) || JS_IsArrayBufferViewObject(*object) { if IsArrayBufferObject(*object) || JS_IsArrayBufferViewObject(*object) {
// FIXME:(arihant2math) // FIXME:(arihant2math) implement it the correct way (is this correct?)
error!("Array buffers as keys is currently unsupported"); let key = structuredclone::write(cx, input, None).expect("Could not serialize key");
return Err(Error::NotSupported); return Ok(IndexedDBKeyType::Binary(key.serialized.clone()));
} }
if let ESClass::Array = built_in_class { if let ESClass::Array = built_in_class {
// FIXME:(arihant2math) // FIXME:(arihant2math)
unimplemented!("Arrays as keys is currently unsupported"); error!("Arrays as keys is currently unsupported");
return Err(Error::NotSupported);
} }
} }
} }
@ -205,7 +214,7 @@ pub fn evaluate_key_path_on_value(
has_prop = true; has_prop = true;
} else { } else {
// If we get here it means the object doesn't have the property or the // If we get here it means the object doesn't have the property or the
// property is available throuch a getter. We don't want to call any // property is available through a getter. We don't want to call any
// getters to avoid potential re-entrancy. // getters to avoid potential re-entrancy.
// The blob object is special since its properties are available // The blob object is special since its properties are available
// only through getters but we still want to support them for key // only through getters but we still want to support them for key

View file

@ -22,8 +22,7 @@ pub enum IndexedDBKeyType {
Number(f64), Number(f64),
String(String), String(String),
Binary(Vec<u8>), Binary(Vec<u8>),
// FIXME:(arihant2math) Date should not be stored as a Vec<u8> Date(f64),
Date(Vec<u8>),
Array(Vec<IndexedDBKeyType>), Array(Vec<IndexedDBKeyType>),
// FIXME:(arihant2math) implment ArrayBuffer // FIXME:(arihant2math) implment ArrayBuffer
} }

View file

@ -1,4 +1,5 @@
[idb-binary-key-roundtrip.any.worker.html] [idb-binary-key-roundtrip.any.worker.html]
expected: CRASH
[Binary keys can be supplied using the view type Uint8Array] [Binary keys can be supplied using the view type Uint8Array]
expected: FAIL expected: FAIL
@ -49,6 +50,7 @@
[idb-binary-key-roundtrip.any.html] [idb-binary-key-roundtrip.any.html]
expected: CRASH
[Binary keys can be supplied using the view type Uint8Array] [Binary keys can be supplied using the view type Uint8Array]
expected: FAIL expected: FAIL

View file

@ -2,10 +2,26 @@
expected: ERROR expected: ERROR
[idbcursor-key.any.worker.html] [idbcursor-key.any.worker.html]
expected: CRASH [IDBCursor.key]
expected: FAIL
[IDBCursor.key 1]
expected: FAIL
[IDBCursor.key 2]
expected: FAIL
[idbcursor-key.any.html] [idbcursor-key.any.html]
expected: CRASH [IDBCursor.key]
expected: FAIL
[IDBCursor.key 1]
expected: FAIL
[IDBCursor.key 2]
expected: FAIL
[idbcursor-key.any.serviceworker.html] [idbcursor-key.any.serviceworker.html]
expected: ERROR expected: ERROR

View file

@ -2,76 +2,26 @@
expected: ERROR expected: ERROR
[idbfactory_cmp.any.html] [idbfactory_cmp.any.html]
expected: CRASH
[IDBFactory.cmp() - compared keys return correct value]
expected: FAIL
[IDBFactory.cmp() - null]
expected: FAIL
[IDBFactory.cmp() - NaN]
expected: FAIL
[Array vs. Binary] [Array vs. Binary]
expected: FAIL expected: FAIL
[Binary vs. String]
expected: FAIL
[String vs. Date]
expected: FAIL
[Date vs. Number]
expected: FAIL
[Compare in unsigned octet values (in the range [0, 255\])] [Compare in unsigned octet values (in the range [0, 255\])]
expected: FAIL expected: FAIL
[Compare values of the same length]
expected: FAIL
[Compare values of different lengths] [Compare values of different lengths]
expected: FAIL expected: FAIL
[Compare when values in the range of their minimal length are the same]
expected: FAIL
[idbfactory_cmp.any.worker.html] [idbfactory_cmp.any.worker.html]
expected: CRASH
[IDBFactory.cmp() - compared keys return correct value]
expected: FAIL
[IDBFactory.cmp() - null]
expected: FAIL
[IDBFactory.cmp() - NaN]
expected: FAIL
[Array vs. Binary] [Array vs. Binary]
expected: FAIL expected: FAIL
[Binary vs. String]
expected: FAIL
[String vs. Date]
expected: FAIL
[Date vs. Number]
expected: FAIL
[Compare in unsigned octet values (in the range [0, 255\])] [Compare in unsigned octet values (in the range [0, 255\])]
expected: FAIL expected: FAIL
[Compare values of the same length]
expected: FAIL
[Compare values of different lengths] [Compare values of different lengths]
expected: FAIL expected: FAIL
[Compare when values in the range of their minimal length are the same]
expected: FAIL
[idbfactory_cmp.any.serviceworker.html] [idbfactory_cmp.any.serviceworker.html]
expected: ERROR expected: ERROR

View file

@ -1,11 +1,23 @@
[idbobjectstore-clear-exception-order.any.worker.html] [idbobjectstore-clear-exception-order.any.worker.html]
expected: CRASH expected: TIMEOUT
[IDBObjectStore.clear exception order: InvalidStateError vs. TransactionInactiveError]
expected: TIMEOUT
[IDBObjectStore.clear exception order: TransactionInactiveError vs. ReadOnlyError]
expected: FAIL
[idbobjectstore-clear-exception-order.any.sharedworker.html] [idbobjectstore-clear-exception-order.any.sharedworker.html]
expected: ERROR expected: ERROR
[idbobjectstore-clear-exception-order.any.html] [idbobjectstore-clear-exception-order.any.html]
expected: CRASH expected: TIMEOUT
[IDBObjectStore.clear exception order: InvalidStateError vs. TransactionInactiveError]
expected: TIMEOUT
[IDBObjectStore.clear exception order: TransactionInactiveError vs. ReadOnlyError]
expected: FAIL
[idbobjectstore-clear-exception-order.any.serviceworker.html] [idbobjectstore-clear-exception-order.any.serviceworker.html]
expected: ERROR expected: ERROR

View file

@ -2,10 +2,21 @@
expected: ERROR expected: ERROR
[idbobjectstore-delete-exception-order.any.html] [idbobjectstore-delete-exception-order.any.html]
expected: CRASH expected: TIMEOUT
[IDBObjectStore.delete exception order: InvalidStateError vs. TransactionInactiveError]
expected: TIMEOUT
[IDBObjectStore.delete exception order: TransactionInactiveError vs. ReadOnlyError]
expected: FAIL
[idbobjectstore-delete-exception-order.any.sharedworker.html] [idbobjectstore-delete-exception-order.any.sharedworker.html]
expected: ERROR expected: ERROR
[idbobjectstore-delete-exception-order.any.worker.html] [idbobjectstore-delete-exception-order.any.worker.html]
expected: CRASH expected: TIMEOUT
[IDBObjectStore.delete exception order: InvalidStateError vs. TransactionInactiveError]
expected: TIMEOUT
[IDBObjectStore.delete exception order: TransactionInactiveError vs. ReadOnlyError]
expected: FAIL

View file

@ -2,7 +2,6 @@
expected: ERROR expected: ERROR
[key-conversion-exceptions.any.worker.html] [key-conversion-exceptions.any.worker.html]
expected: CRASH
[IDBFactory cmp() static with throwing/invalid keys] [IDBFactory cmp() static with throwing/invalid keys]
expected: FAIL expected: FAIL
@ -20,7 +19,6 @@
[key-conversion-exceptions.any.html] [key-conversion-exceptions.any.html]
expected: CRASH
[IDBFactory cmp() static with throwing/invalid keys] [IDBFactory cmp() static with throwing/invalid keys]
expected: FAIL expected: FAIL

View file

@ -2,10 +2,73 @@
expected: ERROR expected: ERROR
[key_invalid.any.worker.html] [key_invalid.any.worker.html]
expected: CRASH [Invalid key - [{}\]]
expected: FAIL
[Invalid key - [[\], [\], [\], [[ Date \]\]\]]
expected: FAIL
[Invalid key - [undefined\]]
expected: FAIL
[Invalid key - [,1\]]
expected: FAIL
[Invalid key - sparse array]
expected: FAIL
[Invalid key - sparse array 2]
expected: FAIL
[Invalid key - [[1\], [3\], [7\], [[ sparse array \]\]\]]
expected: FAIL
[Invalid key - [1,2,3,,\]]
expected: FAIL
[Invalid key - array directly contains self]
expected: FAIL
[Invalid key - array indirectly contains self]
expected: FAIL
[Invalid key - array member contains self]
expected: FAIL
[key_invalid.any.sharedworker.html] [key_invalid.any.sharedworker.html]
expected: ERROR expected: ERROR
[key_invalid.any.html] [key_invalid.any.html]
expected: CRASH [Invalid key - [{}\]]
expected: FAIL
[Invalid key - [[\], [\], [\], [[ Date \]\]\]]
expected: FAIL
[Invalid key - [undefined\]]
expected: FAIL
[Invalid key - [,1\]]
expected: FAIL
[Invalid key - sparse array]
expected: FAIL
[Invalid key - sparse array 2]
expected: FAIL
[Invalid key - [[1\], [3\], [7\], [[ sparse array \]\]\]]
expected: FAIL
[Invalid key - [1,2,3,,\]]
expected: FAIL
[Invalid key - array directly contains self]
expected: FAIL
[Invalid key - array indirectly contains self]
expected: FAIL
[Invalid key - array member contains self]
expected: FAIL

View file

@ -2,10 +2,152 @@
expected: ERROR expected: ERROR
[keyorder.any.worker.html] [keyorder.any.worker.html]
expected: CRASH [Database readback sort - String < Array]
expected: FAIL
[IDBKey.cmp sort - String < Array]
expected: FAIL
[Database readback sort - float < String]
expected: FAIL
[IDBKey.cmp sort - float < String]
expected: FAIL
[Database readback sort - float < Date]
expected: FAIL
[IDBKey.cmp sort - float < Date]
expected: FAIL
[Database readback sort - float < Date < String < Array]
expected: FAIL
[IDBKey.cmp sort - float < Date < String < Array]
expected: FAIL
[Database readback sort - Date(1 sec ago) < Date(now) < Date(1 minute in future)]
expected: FAIL
[IDBKey.cmp sort - Date(1 sec ago) < Date(now) < Date(1 minute in future)]
expected: FAIL
[Database readback sort - -1.1 < 1 < 1.01337 < 1.013373 < 2]
expected: FAIL
[IDBKey.cmp sort - -1.1 < 1 < 1.01337 < 1.013373 < 2]
expected: FAIL
[Database readback sort - -Infinity < -0.01 < 0 < Infinity]
expected: FAIL
[IDBKey.cmp sort - -Infinity < -0.01 < 0 < Infinity]
expected: FAIL
[Database readback sort - "" < "a" < "ab" < "b" < "ba"]
expected: FAIL
[IDBKey.cmp sort - "" < "a" < "ab" < "b" < "ba"]
expected: FAIL
[Database readback sort - Arrays]
expected: FAIL
[IDBKey.cmp sort - Arrays]
expected: FAIL
[Database readback sort - Array.length: 10,000 < Array.length: 10,001]
expected: FAIL
[IDBKey.cmp sort - Array.length: 10,000 < Array.length: 10,001]
expected: FAIL
[Database readback sort - Infinity inside arrays]
expected: FAIL
[IDBKey.cmp sort - Infinity inside arrays]
expected: FAIL
[Database readback sort - Test different stuff at once]
expected: FAIL
[IDBKey.cmp sort - Test different stuff at once]
expected: FAIL
[keyorder.any.html] [keyorder.any.html]
expected: CRASH [Database readback sort - String < Array]
expected: FAIL
[IDBKey.cmp sort - String < Array]
expected: FAIL
[Database readback sort - float < String]
expected: FAIL
[IDBKey.cmp sort - float < String]
expected: FAIL
[Database readback sort - float < Date]
expected: FAIL
[IDBKey.cmp sort - float < Date]
expected: FAIL
[Database readback sort - float < Date < String < Array]
expected: FAIL
[IDBKey.cmp sort - float < Date < String < Array]
expected: FAIL
[Database readback sort - Date(1 sec ago) < Date(now) < Date(1 minute in future)]
expected: FAIL
[IDBKey.cmp sort - Date(1 sec ago) < Date(now) < Date(1 minute in future)]
expected: FAIL
[Database readback sort - -1.1 < 1 < 1.01337 < 1.013373 < 2]
expected: FAIL
[IDBKey.cmp sort - -1.1 < 1 < 1.01337 < 1.013373 < 2]
expected: FAIL
[Database readback sort - -Infinity < -0.01 < 0 < Infinity]
expected: FAIL
[IDBKey.cmp sort - -Infinity < -0.01 < 0 < Infinity]
expected: FAIL
[Database readback sort - "" < "a" < "ab" < "b" < "ba"]
expected: FAIL
[IDBKey.cmp sort - "" < "a" < "ab" < "b" < "ba"]
expected: FAIL
[Database readback sort - Arrays]
expected: FAIL
[IDBKey.cmp sort - Arrays]
expected: FAIL
[Database readback sort - Array.length: 10,000 < Array.length: 10,001]
expected: FAIL
[IDBKey.cmp sort - Array.length: 10,000 < Array.length: 10,001]
expected: FAIL
[Database readback sort - Infinity inside arrays]
expected: FAIL
[IDBKey.cmp sort - Infinity inside arrays]
expected: FAIL
[Database readback sort - Test different stuff at once]
expected: FAIL
[IDBKey.cmp sort - Test different stuff at once]
expected: FAIL
[keyorder.any.serviceworker.html] [keyorder.any.serviceworker.html]
expected: ERROR expected: ERROR

View file

@ -1,11 +1,5 @@
[value.any.html] [value.any.html]
expected: TIMEOUT expected: TIMEOUT
[BigInts as keys in IndexedDB - primitive BigInt]
expected: FAIL
[BigInts as keys in IndexedDB - BigInt object]
expected: FAIL
[Values - Array] [Values - Array]
expected: TIMEOUT expected: TIMEOUT
@ -15,12 +9,6 @@
[value.any.worker.html] [value.any.worker.html]
expected: TIMEOUT expected: TIMEOUT
[BigInts as keys in IndexedDB - primitive BigInt]
expected: FAIL
[BigInts as keys in IndexedDB - BigInt object]
expected: FAIL
[Values - Array] [Values - Array]
expected: TIMEOUT expected: TIMEOUT