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

@ -8,6 +8,8 @@ use std::ptr::{self};
use std::rc::Rc;
use std::collections::HashMap;
use base::id::{MessagePortId, MessagePortIndex};
use constellation_traits::MessagePortImpl;
use dom_struct::dom_struct;
use js::conversions::ToJSValConvertible;
use js::jsapi::{Heap, JSObject};
@ -56,8 +58,6 @@ use crate::js::conversions::FromJSValConvertible;
use crate::realms::{enter_realm, InRealm};
use crate::script_runtime::{CanGc, JSContext as SafeJSContext};
use crate::dom::promisenativehandler::{Callback, PromiseNativeHandler};
use base::id::MessagePortId;
use constellation_traits::MessagePortImpl;
use crate::dom::bindings::transferable::Transferable;
use crate::dom::bindings::structuredclone::{StructuredData, StructuredDataReader};
@ -2179,7 +2179,7 @@ pub(crate) fn get_read_promise_bytes(
/// <https://streams.spec.whatwg.org/#rs-transfer>
impl Transferable for ReadableStream {
type Id = MessagePortId;
type Index = MessagePortIndex;
type Data = MessagePortImpl;
/// <https://streams.spec.whatwg.org/#ref-for-readablestream%E2%91%A1%E2%91%A0>
@ -2247,7 +2247,9 @@ impl Transferable for ReadableStream {
}
/// Note: we are relying on the port transfer, so the data returned here are related to the port.
fn serialized_storage(data: StructuredData<'_>) -> &mut Option<HashMap<Self::Id, Self::Data>> {
fn serialized_storage(
data: StructuredData<'_>,
) -> &mut Option<HashMap<MessagePortId, Self::Data>> {
match data {
StructuredData::Reader(r) => &mut r.port_impls,
StructuredData::Writer(w) => &mut w.ports,