mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
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:
parent
0b8645d422
commit
7b16ae26b4
13 changed files with 170 additions and 337 deletions
|
@ -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.
|
||||
|
|
|
@ -6,11 +6,12 @@
|
|||
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::CStr;
|
||||
use std::num::NonZeroU32;
|
||||
use std::os::raw;
|
||||
use std::ptr;
|
||||
|
||||
use base::id::{BlobId, DomExceptionId, DomPointId, MessagePortId, PipelineNamespaceId};
|
||||
use base::id::{
|
||||
BlobId, DomExceptionId, DomPointId, Index, MessagePortId, NamespaceIndex, PipelineNamespaceId,
|
||||
};
|
||||
use constellation_traits::{
|
||||
BlobImpl, DomException, DomPoint, MessagePortImpl, Serializable as SerializableInterface,
|
||||
StructuredSerializedData, Transferrable as TransferrableInterface,
|
||||
|
@ -34,8 +35,8 @@ use strum::IntoEnumIterator;
|
|||
use crate::dom::bindings::conversions::{ToJSValConvertible, root_from_object};
|
||||
use crate::dom::bindings::error::{Error, Fallible};
|
||||
use crate::dom::bindings::root::DomRoot;
|
||||
use crate::dom::bindings::serializable::{IntoStorageKey, Serializable, StorageKey};
|
||||
use crate::dom::bindings::transferable::{ExtractComponents, IdFromComponents, Transferable};
|
||||
use crate::dom::bindings::serializable::{Serializable, StorageKey};
|
||||
use crate::dom::bindings::transferable::Transferable;
|
||||
use crate::dom::blob::Blob;
|
||||
use crate::dom::dompoint::DOMPoint;
|
||||
use crate::dom::dompointreadonly::DOMPointReadOnly;
|
||||
|
@ -120,7 +121,7 @@ unsafe fn read_object<T: Serializable>(
|
|||
|
||||
// 1. Re-build the key for the storage location
|
||||
// of the serialized object.
|
||||
let id = T::Id::from(storage_key);
|
||||
let id: NamespaceIndex<T::Index> = storage_key.into();
|
||||
|
||||
// 2. Get the transferred object from its storage, using the key.
|
||||
let objects = T::serialized_storage(StructuredData::Reader(sc_reader));
|
||||
|
@ -155,7 +156,7 @@ unsafe fn write_object<T: Serializable>(
|
|||
let objects = T::serialized_storage(StructuredData::Writer(sc_writer))
|
||||
.get_or_insert_with(HashMap::new);
|
||||
objects.insert(new_id, serialized);
|
||||
let storage_key = new_id.into_storage_key();
|
||||
let storage_key = StorageKey::new(new_id);
|
||||
|
||||
assert!(JS_WriteUint32Pair(
|
||||
w,
|
||||
|
@ -282,13 +283,13 @@ fn receive_object<T: Transferable>(
|
|||
.try_into()
|
||||
.expect("name_space to be a slice of four."),
|
||||
));
|
||||
let id = <T::Id as IdFromComponents>::from(
|
||||
let id: NamespaceIndex<T::Index> = NamespaceIndex {
|
||||
namespace_id,
|
||||
NonZeroU32::new(u32::from_ne_bytes(
|
||||
index: Index::new(u32::from_ne_bytes(
|
||||
index.try_into().expect("index to be a slice of four."),
|
||||
))
|
||||
.expect("Index to be non-zero"),
|
||||
);
|
||||
};
|
||||
|
||||
// 2. Get the transferred object from its storage, using the key.
|
||||
let storage = T::serialized_storage(StructuredData::Reader(sc_reader));
|
||||
|
@ -356,11 +357,10 @@ unsafe fn try_transfer<T: Transferable + IDLInterface>(
|
|||
.get_or_insert_with(HashMap::new);
|
||||
objects.insert(id, object);
|
||||
|
||||
let (PipelineNamespaceId(name_space), index) = id.components();
|
||||
let index = index.get();
|
||||
let index = id.index.0.get();
|
||||
|
||||
let mut big: [u8; 8] = [0; 8];
|
||||
let name_space = name_space.to_ne_bytes();
|
||||
let name_space = id.namespace_id.0.to_ne_bytes();
|
||||
let index = index.to_ne_bytes();
|
||||
|
||||
let (left, right) = big.split_at_mut(4);
|
||||
|
|
|
@ -6,44 +6,34 @@
|
|||
//! (<https://html.spec.whatwg.org/multipage/#transferable-objects>).
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::num::NonZeroU32;
|
||||
use std::hash::Hash;
|
||||
|
||||
use base::id::PipelineNamespaceId;
|
||||
use base::id::NamespaceIndex;
|
||||
|
||||
use crate::dom::bindings::reflector::DomObject;
|
||||
use crate::dom::bindings::root::DomRoot;
|
||||
use crate::dom::bindings::structuredclone::{StructuredData, StructuredDataReader};
|
||||
use crate::dom::globalscope::GlobalScope;
|
||||
|
||||
pub(crate) trait IdFromComponents
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
fn from(namespace_id: PipelineNamespaceId, index: NonZeroU32) -> Self;
|
||||
}
|
||||
|
||||
pub(crate) trait ExtractComponents {
|
||||
fn components(&self) -> (PipelineNamespaceId, NonZeroU32);
|
||||
}
|
||||
|
||||
pub(crate) trait Transferable: DomObject
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
type Id: Eq + std::hash::Hash + Copy + IdFromComponents + ExtractComponents;
|
||||
type Index: Copy + Eq + Hash;
|
||||
type Data;
|
||||
|
||||
fn can_transfer(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn transfer(&self) -> Result<(Self::Id, Self::Data), ()>;
|
||||
fn transfer(&self) -> Result<(NamespaceIndex<Self::Index>, Self::Data), ()>;
|
||||
fn transfer_receive(
|
||||
owner: &GlobalScope,
|
||||
id: Self::Id,
|
||||
id: NamespaceIndex<Self::Index>,
|
||||
serialized: Self::Data,
|
||||
) -> Result<DomRoot<Self>, ()>;
|
||||
|
||||
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>>;
|
||||
fn deserialized_storage(reader: &mut StructuredDataReader) -> &mut Option<Vec<DomRoot<Self>>>;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue