Reduce indexing boilerplate for serializable/transferrable objects. (#36624)

Rather than creating unique types for each pipeline-namespaced index
type (eg. MessagePortId, DomExceptionId, etc.), we can create a generic
common type that uses a marker to prevent type confusion. This change
allows us to reduce the boilerplate code required when implementing
serializable/transferable interfaces, since the structured clone
implementation can rely on the common type.

Testing: Existing WPT tests for serialization and transferring provide
coverage.

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2025-04-21 22:02:02 -04:00 committed by GitHub
parent 0b8645d422
commit 7b16ae26b4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 170 additions and 337 deletions

View file

@ -6,9 +6,8 @@
//! (<https://html.spec.whatwg.org/multipage/#serializable-objects>).
use std::collections::HashMap;
use std::num::NonZeroU32;
use base::id::PipelineNamespaceId;
use base::id::{Index, NamespaceIndex, PipelineNamespaceId};
use crate::dom::bindings::reflector::DomObject;
use crate::dom::bindings::root::DomRoot;
@ -25,9 +24,9 @@ pub(crate) struct StorageKey {
}
impl StorageKey {
pub(crate) fn new(name_space: PipelineNamespaceId, index: NonZeroU32) -> StorageKey {
let name_space = name_space.0.to_ne_bytes();
let index = index.get().to_ne_bytes();
pub(crate) fn new<T>(index: NamespaceIndex<T>) -> StorageKey {
let name_space = index.namespace_id.0.to_ne_bytes();
let index = index.index.0.get().to_ne_bytes();
StorageKey {
index: u32::from_ne_bytes(index),
name_space: u32::from_ne_bytes(name_space),
@ -35,11 +34,13 @@ impl StorageKey {
}
}
pub(crate) trait IntoStorageKey
where
Self: Sized,
{
fn into_storage_key(self) -> StorageKey;
impl<T> From<StorageKey> for NamespaceIndex<T> {
fn from(key: StorageKey) -> NamespaceIndex<T> {
NamespaceIndex {
namespace_id: PipelineNamespaceId(key.name_space),
index: Index::new(key.index).expect("Index must not be zero"),
}
}
}
/// Interface for serializable platform objects.
@ -48,11 +49,11 @@ pub(crate) trait Serializable: DomObject
where
Self: Sized,
{
type Id: Copy + Eq + std::hash::Hash + IntoStorageKey + From<StorageKey>;
type Index: Copy + Eq + std::hash::Hash;
type Data;
/// <https://html.spec.whatwg.org/multipage/#serialization-steps>
fn serialize(&self) -> Result<(Self::Id, Self::Data), ()>;
fn serialize(&self) -> Result<(NamespaceIndex<Self::Index>, Self::Data), ()>;
/// <https://html.spec.whatwg.org/multipage/#deserialization-steps>
fn deserialize(
owner: &GlobalScope,
@ -64,7 +65,9 @@ where
/// Returns the field of [StructuredDataReader]/[StructuredDataWriter] that
/// should be used to read/store serialized instances of this type.
fn serialized_storage(data: StructuredData<'_>) -> &mut Option<HashMap<Self::Id, Self::Data>>;
fn serialized_storage(
data: StructuredData<'_>,
) -> &mut Option<HashMap<NamespaceIndex<Self::Index>, Self::Data>>;
/// Returns the field of [StructuredDataReader] that should be used to store
/// deserialized instances of this type.