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

@ -3,9 +3,8 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::collections::HashMap;
use std::num::NonZeroU32;
use base::id::{DomExceptionId, DomExceptionIndex, PipelineNamespaceId};
use base::id::{DomExceptionId, DomExceptionIndex};
use constellation_traits::DomException;
use dom_struct::dom_struct;
use js::rust::HandleObject;
@ -18,7 +17,7 @@ use crate::dom::bindings::reflector::{
Reflector, reflect_dom_object, reflect_dom_object_with_proto,
};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::serializable::{IntoStorageKey, Serializable, StorageKey};
use crate::dom::bindings::serializable::{Serializable, StorageKey};
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::structuredclone::{StructuredData, StructuredDataReader};
use crate::dom::globalscope::GlobalScope;
@ -224,11 +223,11 @@ impl DOMExceptionMethods<crate::DomTypeHolder> for DOMException {
}
impl Serializable for DOMException {
type Id = DomExceptionId;
type Index = DomExceptionIndex;
type Data = DomException;
// https://webidl.spec.whatwg.org/#idl-DOMException
fn serialize(&self) -> Result<(Self::Id, Self::Data), ()> {
fn serialize(&self) -> Result<(DomExceptionId, Self::Data), ()> {
let serialized = DomException {
message: self.message.to_string(),
name: self.name.to_string(),
@ -253,7 +252,9 @@ impl Serializable for DOMException {
))
}
fn serialized_storage(data: StructuredData<'_>) -> &mut Option<HashMap<Self::Id, Self::Data>> {
fn serialized_storage(
data: StructuredData<'_>,
) -> &mut Option<HashMap<DomExceptionId, Self::Data>> {
match data {
StructuredData::Reader(reader) => &mut reader.exceptions,
StructuredData::Writer(writer) => &mut writer.exceptions,
@ -266,24 +267,3 @@ impl Serializable for DOMException {
&mut reader.dom_exceptions
}
}
impl From<StorageKey> for DomExceptionId {
fn from(storage_key: StorageKey) -> DomExceptionId {
let namespace_id = PipelineNamespaceId(storage_key.name_space);
let index = DomExceptionIndex(
NonZeroU32::new(storage_key.index).expect("Deserialized exception index is zero"),
);
DomExceptionId {
namespace_id,
index,
}
}
}
impl IntoStorageKey for DomExceptionId {
fn into_storage_key(self) -> StorageKey {
let DomExceptionIndex(index) = self.index;
StorageKey::new(self.namespace_id, index)
}
}