mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Make DOMPoint and DOMPointReadOnly serializable (#35989)
* script: Make DOMPointReadOnly serializable. Signed-off-by: Josh Matthews <josh@joshmatthews.net> * script: Make DOMPoint serializable. Signed-off-by: Josh Matthews <josh@joshmatthews.net> * script: Shrink worker script event. Signed-off-by: Josh Matthews <josh@joshmatthews.net> * Update components/script/dom/dompoint.rs Co-authored-by: Martin Robinson <mrobinson@igalia.com> Signed-off-by: Josh Matthews <josh@joshmatthews.net> --------- Signed-off-by: Josh Matthews <josh@joshmatthews.net> Co-authored-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
parent
1df1ba58d6
commit
53a2e61fec
13 changed files with 224 additions and 45 deletions
|
@ -16,7 +16,7 @@ pub(crate) enum WorkerScriptMsg {
|
|||
/// Message sent through Worker.postMessage
|
||||
DOMMessage {
|
||||
origin: ImmutableOrigin,
|
||||
data: StructuredSerializedData,
|
||||
data: Box<StructuredSerializedData>,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ use std::num::NonZeroU32;
|
|||
use std::os::raw;
|
||||
use std::ptr;
|
||||
|
||||
use base::id::{BlobId, MessagePortId, PipelineNamespaceId};
|
||||
use base::id::{BlobId, DomPointId, MessagePortId, PipelineNamespaceId};
|
||||
use js::glue::{
|
||||
CopyJSStructuredCloneData, DeleteJSAutoStructuredCloneBuffer, GetLengthOfJSStructuredCloneData,
|
||||
NewJSAutoStructuredCloneBuffer, WriteBytesToJSStructuredCloneData,
|
||||
|
@ -24,7 +24,7 @@ use js::jsval::UndefinedValue;
|
|||
use js::rust::wrappers::{JS_ReadStructuredClone, JS_WriteStructuredClone};
|
||||
use js::rust::{CustomAutoRooterGuard, HandleValue, MutableHandleValue};
|
||||
use script_bindings::conversions::IDLInterface;
|
||||
use script_traits::serializable::BlobImpl;
|
||||
use script_traits::serializable::{BlobImpl, DomPoint};
|
||||
use script_traits::transferable::MessagePortImpl;
|
||||
use script_traits::{
|
||||
Serializable as SerializableInterface, StructuredSerializedData,
|
||||
|
@ -38,6 +38,8 @@ 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::blob::Blob;
|
||||
use crate::dom::dompoint::DOMPoint;
|
||||
use crate::dom::dompointreadonly::DOMPointReadOnly;
|
||||
use crate::dom::globalscope::GlobalScope;
|
||||
use crate::dom::messageport::MessagePort;
|
||||
use crate::realms::{AlreadyInRealm, InRealm, enter_realm};
|
||||
|
@ -54,6 +56,8 @@ pub(super) enum StructuredCloneTags {
|
|||
DomBlob = 0xFFFF8001,
|
||||
MessagePort = 0xFFFF8002,
|
||||
Principals = 0xFFFF8003,
|
||||
DomPointReadOnly = 0xFFFF8004,
|
||||
DomPoint = 0xFFFF8005,
|
||||
Max = 0xFFFFFFFF,
|
||||
}
|
||||
|
||||
|
@ -61,6 +65,8 @@ impl From<SerializableInterface> for StructuredCloneTags {
|
|||
fn from(v: SerializableInterface) -> Self {
|
||||
match v {
|
||||
SerializableInterface::Blob => StructuredCloneTags::DomBlob,
|
||||
SerializableInterface::DomPointReadOnly => StructuredCloneTags::DomPointReadOnly,
|
||||
SerializableInterface::DomPoint => StructuredCloneTags::DomPoint,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -83,6 +89,8 @@ fn reader_for_type(
|
|||
) -> *mut JSObject {
|
||||
match val {
|
||||
SerializableInterface::Blob => read_object::<Blob>,
|
||||
SerializableInterface::DomPointReadOnly => read_object::<DOMPointReadOnly>,
|
||||
SerializableInterface::DomPoint => read_object::<DOMPoint>,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -214,6 +222,8 @@ type SerializeOperation = unsafe fn(
|
|||
fn serialize_for_type(val: SerializableInterface) -> SerializeOperation {
|
||||
match val {
|
||||
SerializableInterface::Blob => try_serialize::<Blob>,
|
||||
SerializableInterface::DomPointReadOnly => try_serialize::<DOMPointReadOnly>,
|
||||
SerializableInterface::DomPoint => try_serialize::<DOMPoint>,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -465,6 +475,9 @@ pub(crate) enum StructuredData<'a> {
|
|||
pub(crate) struct StructuredDataReader {
|
||||
/// A map of deserialized blobs, stored temporarily here to keep them rooted.
|
||||
pub(crate) blobs: Option<HashMap<StorageKey, DomRoot<Blob>>>,
|
||||
/// A map of deserialized points, stored temporarily here to keep them rooted.
|
||||
pub(crate) points_read_only: Option<HashMap<StorageKey, DomRoot<DOMPointReadOnly>>>,
|
||||
pub(crate) dom_points: Option<HashMap<StorageKey, DomRoot<DOMPoint>>>,
|
||||
/// A vec of transfer-received DOM ports,
|
||||
/// to be made available to script through a message event.
|
||||
pub(crate) message_ports: Option<Vec<DomRoot<MessagePort>>>,
|
||||
|
@ -476,12 +489,17 @@ pub(crate) struct StructuredDataReader {
|
|||
/// used as part of the "deserialize" steps of blobs,
|
||||
/// to produce the DOM blobs stored in `blobs` above.
|
||||
pub(crate) blob_impls: Option<HashMap<BlobId, BlobImpl>>,
|
||||
/// A map of serialized points.
|
||||
pub(crate) points: Option<HashMap<DomPointId, DomPoint>>,
|
||||
}
|
||||
|
||||
/// A data holder for transferred and serialized objects.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct StructuredDataWriter {
|
||||
/// Transferred ports.
|
||||
pub(crate) ports: Option<HashMap<MessagePortId, MessagePortImpl>>,
|
||||
/// Serialized points.
|
||||
pub(crate) points: Option<HashMap<DomPointId, DomPoint>>,
|
||||
/// Serialized blobs.
|
||||
pub(crate) blobs: Option<HashMap<BlobId, BlobImpl>>,
|
||||
}
|
||||
|
@ -497,10 +515,7 @@ pub(crate) fn write(
|
|||
if let Some(transfer) = transfer {
|
||||
transfer.to_jsval(*cx, val.handle_mut());
|
||||
}
|
||||
let mut sc_writer = StructuredDataWriter {
|
||||
ports: None,
|
||||
blobs: None,
|
||||
};
|
||||
let mut sc_writer = StructuredDataWriter::default();
|
||||
let sc_writer_ptr = &mut sc_writer as *mut _;
|
||||
|
||||
let scbuf = NewJSAutoStructuredCloneBuffer(
|
||||
|
@ -537,6 +552,7 @@ pub(crate) fn write(
|
|||
let data = StructuredSerializedData {
|
||||
serialized: data,
|
||||
ports: sc_writer.ports.take(),
|
||||
points: sc_writer.points.take(),
|
||||
blobs: sc_writer.blobs.take(),
|
||||
};
|
||||
|
||||
|
@ -556,8 +572,11 @@ pub(crate) fn read(
|
|||
let mut sc_reader = StructuredDataReader {
|
||||
blobs: None,
|
||||
message_ports: None,
|
||||
points_read_only: None,
|
||||
dom_points: None,
|
||||
port_impls: data.ports.take(),
|
||||
blob_impls: data.blobs.take(),
|
||||
points: data.points.take(),
|
||||
};
|
||||
let sc_reader_ptr = &mut sc_reader as *mut _;
|
||||
unsafe {
|
||||
|
|
|
@ -564,7 +564,8 @@ impl DedicatedWorkerGlobalScope {
|
|||
let target = self.upcast();
|
||||
let _ac = enter_realm(self);
|
||||
rooted!(in(*scope.get_cx()) let mut message = UndefinedValue());
|
||||
if let Ok(ports) = structuredclone::read(scope.upcast(), data, message.handle_mut())
|
||||
if let Ok(ports) =
|
||||
structuredclone::read(scope.upcast(), *data, message.handle_mut())
|
||||
{
|
||||
MessageEvent::dispatch_jsval(
|
||||
target,
|
||||
|
|
|
@ -2,14 +2,20 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use base::id::DomPointId;
|
||||
use dom_struct::dom_struct;
|
||||
use js::rust::HandleObject;
|
||||
use script_traits::serializable::DomPoint;
|
||||
|
||||
use crate::dom::bindings::codegen::Bindings::DOMPointBinding::{DOMPointInit, DOMPointMethods};
|
||||
use crate::dom::bindings::codegen::Bindings::DOMPointReadOnlyBinding::DOMPointReadOnlyMethods;
|
||||
use crate::dom::bindings::error::Fallible;
|
||||
use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
|
||||
use crate::dom::bindings::root::DomRoot;
|
||||
use crate::dom::bindings::serializable::{Serializable, StorageKey};
|
||||
use crate::dom::bindings::structuredclone::{StructuredData, StructuredDataReader};
|
||||
use crate::dom::dompointreadonly::{DOMPointReadOnly, DOMPointWriteMethods};
|
||||
use crate::dom::globalscope::GlobalScope;
|
||||
use crate::script_runtime::CanGc;
|
||||
|
@ -124,3 +130,49 @@ impl DOMPointMethods<crate::DomTypeHolder> for DOMPoint {
|
|||
self.point.SetW(value);
|
||||
}
|
||||
}
|
||||
|
||||
impl Serializable for DOMPoint {
|
||||
type Id = DomPointId;
|
||||
type Data = DomPoint;
|
||||
|
||||
fn serialize(&self) -> Result<(Self::Id, Self::Data), ()> {
|
||||
let serialized = DomPoint {
|
||||
x: self.X(),
|
||||
y: self.Y(),
|
||||
z: self.Z(),
|
||||
w: self.W(),
|
||||
};
|
||||
Ok((DomPointId::new(), serialized))
|
||||
}
|
||||
|
||||
fn deserialize(
|
||||
owner: &GlobalScope,
|
||||
serialized: Self::Data,
|
||||
can_gc: CanGc,
|
||||
) -> Result<DomRoot<Self>, ()>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
Ok(Self::new(
|
||||
owner,
|
||||
serialized.x,
|
||||
serialized.y,
|
||||
serialized.z,
|
||||
serialized.w,
|
||||
can_gc,
|
||||
))
|
||||
}
|
||||
|
||||
fn serialized_storage(data: StructuredData<'_>) -> &mut Option<HashMap<Self::Id, Self::Data>> {
|
||||
match data {
|
||||
StructuredData::Reader(reader) => &mut reader.points,
|
||||
StructuredData::Writer(writer) => &mut writer.points,
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialized_storage(
|
||||
reader: &mut StructuredDataReader,
|
||||
) -> &mut Option<HashMap<StorageKey, DomRoot<Self>>> {
|
||||
&mut reader.dom_points
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,15 +3,21 @@
|
|||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::collections::HashMap;
|
||||
use std::num::NonZeroU32;
|
||||
|
||||
use base::id::{DomPointId, DomPointIndex, PipelineNamespaceId};
|
||||
use dom_struct::dom_struct;
|
||||
use js::rust::HandleObject;
|
||||
use script_traits::serializable::DomPoint;
|
||||
|
||||
use crate::dom::bindings::codegen::Bindings::DOMPointBinding::DOMPointInit;
|
||||
use crate::dom::bindings::codegen::Bindings::DOMPointReadOnlyBinding::DOMPointReadOnlyMethods;
|
||||
use crate::dom::bindings::error::Fallible;
|
||||
use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};
|
||||
use crate::dom::bindings::root::DomRoot;
|
||||
use crate::dom::bindings::serializable::{IntoStorageKey, Serializable, StorageKey};
|
||||
use crate::dom::bindings::structuredclone::{StructuredData, StructuredDataReader};
|
||||
use crate::dom::globalscope::GlobalScope;
|
||||
use crate::script_runtime::CanGc;
|
||||
|
||||
|
@ -134,3 +140,70 @@ impl DOMPointWriteMethods for DOMPointReadOnly {
|
|||
self.w.set(value);
|
||||
}
|
||||
}
|
||||
|
||||
impl Serializable for DOMPointReadOnly {
|
||||
type Id = DomPointId;
|
||||
type Data = DomPoint;
|
||||
|
||||
fn serialize(&self) -> Result<(Self::Id, Self::Data), ()> {
|
||||
let serialized = DomPoint {
|
||||
x: self.x.get(),
|
||||
y: self.y.get(),
|
||||
z: self.z.get(),
|
||||
w: self.w.get(),
|
||||
};
|
||||
Ok((DomPointId::new(), serialized))
|
||||
}
|
||||
|
||||
fn deserialize(
|
||||
owner: &GlobalScope,
|
||||
serialized: Self::Data,
|
||||
can_gc: CanGc,
|
||||
) -> Result<DomRoot<Self>, ()>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
Ok(Self::new(
|
||||
owner,
|
||||
serialized.x,
|
||||
serialized.y,
|
||||
serialized.z,
|
||||
serialized.w,
|
||||
can_gc,
|
||||
))
|
||||
}
|
||||
|
||||
fn serialized_storage(data: StructuredData<'_>) -> &mut Option<HashMap<Self::Id, Self::Data>> {
|
||||
match data {
|
||||
StructuredData::Reader(r) => &mut r.points,
|
||||
StructuredData::Writer(w) => &mut w.points,
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialized_storage(
|
||||
reader: &mut StructuredDataReader,
|
||||
) -> &mut Option<HashMap<StorageKey, DomRoot<Self>>> {
|
||||
&mut reader.points_read_only
|
||||
}
|
||||
}
|
||||
|
||||
impl From<StorageKey> for DomPointId {
|
||||
fn from(storage_key: StorageKey) -> DomPointId {
|
||||
let namespace_id = PipelineNamespaceId(storage_key.name_space);
|
||||
let index = DomPointIndex(
|
||||
NonZeroU32::new(storage_key.index).expect("Deserialized point index is zero"),
|
||||
);
|
||||
|
||||
DomPointId {
|
||||
namespace_id,
|
||||
index,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoStorageKey for DomPointId {
|
||||
fn into_storage_key(self) -> StorageKey {
|
||||
let DomPointIndex(index) = self.index;
|
||||
StorageKey::new(self.namespace_id, index)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -123,8 +123,7 @@ impl History {
|
|||
Some(data) => {
|
||||
let data = StructuredSerializedData {
|
||||
serialized: data,
|
||||
ports: None,
|
||||
blobs: None,
|
||||
..Default::default()
|
||||
};
|
||||
rooted!(in(*GlobalScope::get_cx()) let mut state = UndefinedValue());
|
||||
if structuredclone::read(self.window.as_global_scope(), data, state.handle_mut())
|
||||
|
|
|
@ -443,7 +443,8 @@ impl ServiceWorkerGlobalScope {
|
|||
let target = self.upcast();
|
||||
let _ac = enter_realm(scope);
|
||||
rooted!(in(*scope.get_cx()) let mut message = UndefinedValue());
|
||||
if let Ok(ports) = structuredclone::read(scope.upcast(), data, message.handle_mut())
|
||||
if let Ok(ports) =
|
||||
structuredclone::read(scope.upcast(), *data, message.handle_mut())
|
||||
{
|
||||
ExtendableMessageEvent::dispatch_jsval(
|
||||
target,
|
||||
|
|
|
@ -148,7 +148,7 @@ impl Worker {
|
|||
address,
|
||||
WorkerScriptMsg::DOMMessage {
|
||||
origin: self.global().origin().immutable().clone(),
|
||||
data,
|
||||
data: Box::new(data),
|
||||
},
|
||||
));
|
||||
Ok(())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue