From 53a2e61fecd42d4d35b7ff5479095cf86e12c1ae Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Wed, 26 Mar 2025 21:35:02 -0400 Subject: Make DOMPoint and DOMPointReadOnly serializable (#35989) * script: Make DOMPointReadOnly serializable. Signed-off-by: Josh Matthews * script: Make DOMPoint serializable. Signed-off-by: Josh Matthews * script: Shrink worker script event. Signed-off-by: Josh Matthews * Update components/script/dom/dompoint.rs Co-authored-by: Martin Robinson Signed-off-by: Josh Matthews --------- Signed-off-by: Josh Matthews Co-authored-by: Martin Robinson --- components/script/dom/dompointreadonly.rs | 73 +++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'components/script/dom/dompointreadonly.rs') diff --git a/components/script/dom/dompointreadonly.rs b/components/script/dom/dompointreadonly.rs index d437c470cc9..fed82bd9055 100644 --- a/components/script/dom/dompointreadonly.rs +++ b/components/script/dom/dompointreadonly.rs @@ -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, ()> + where + Self: Sized, + { + Ok(Self::new( + owner, + serialized.x, + serialized.y, + serialized.z, + serialized.w, + can_gc, + )) + } + + fn serialized_storage(data: StructuredData<'_>) -> &mut Option> { + match data { + StructuredData::Reader(r) => &mut r.points, + StructuredData::Writer(w) => &mut w.points, + } + } + + fn deserialized_storage( + reader: &mut StructuredDataReader, + ) -> &mut Option>> { + &mut reader.points_read_only + } +} + +impl From 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) + } +} -- cgit v1.2.3