Make DOM geometry structs serializable (#38828)

Makes the following DOM geometry structs serializable:
- `DOMRect`
- `DOMRectReadOnly`
- `DOMQuad`
- `DOMMatrix`
- `DOMMatrixReadOnly`

Testing: Covered by WPT (`css/geometry/structured-serialization.html`).

---------

Signed-off-by: lumiscosity <averyrudelphe@gmail.com>
This commit is contained in:
lumiscosity 2025-08-22 01:19:42 +02:00 committed by GitHub
parent e00f39d827
commit 39f3ce7a2e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 529 additions and 89 deletions

View file

@ -10,13 +10,14 @@ use std::os::raw;
use std::ptr;
use base::id::{
BlobId, DomExceptionId, DomPointId, ImageBitmapId, Index, MessagePortId, NamespaceIndex,
OffscreenCanvasId, PipelineNamespaceId, QuotaExceededErrorId,
BlobId, DomExceptionId, DomMatrixId, DomPointId, DomQuadId, DomRectId, ImageBitmapId, Index,
MessagePortId, NamespaceIndex, OffscreenCanvasId, PipelineNamespaceId, QuotaExceededErrorId,
};
use constellation_traits::{
BlobImpl, DomException, DomPoint, MessagePortImpl, Serializable as SerializableInterface,
SerializableImageBitmap, SerializableQuotaExceededError, StructuredSerializedData,
TransferableOffscreenCanvas, Transferrable as TransferrableInterface, TransformStreamData,
BlobImpl, DomException, DomMatrix, DomPoint, DomQuad, DomRect, MessagePortImpl,
Serializable as SerializableInterface, SerializableImageBitmap, SerializableQuotaExceededError,
StructuredSerializedData, TransferableOffscreenCanvas, Transferrable as TransferrableInterface,
TransformStreamData,
};
use js::gc::RootedVec;
use js::glue::{
@ -49,7 +50,10 @@ use crate::dom::imagebitmap::ImageBitmap;
use crate::dom::messageport::MessagePort;
use crate::dom::offscreencanvas::OffscreenCanvas;
use crate::dom::readablestream::ReadableStream;
use crate::dom::types::{DOMException, QuotaExceededError, TransformStream};
use crate::dom::types::{
DOMException, DOMMatrix, DOMMatrixReadOnly, DOMQuad, DOMRect, DOMRectReadOnly,
QuotaExceededError, TransformStream,
};
use crate::dom::writablestream::WritableStream;
use crate::realms::{AlreadyInRealm, InRealm, enter_realm};
use crate::script_runtime::{CanGc, JSContext as SafeJSContext};
@ -74,6 +78,11 @@ pub(super) enum StructuredCloneTags {
ImageBitmap = 0xFFFF800A,
OffscreenCanvas = 0xFFFF800B,
QuotaExceededError = 0xFFFF800C,
DomRect = 0xFFFF800D,
DomRectReadOnly = 0xFFFF800E,
DomQuad = 0xFFFF800F,
DomMatrix = 0xFFFF8010,
DomMatrixReadOnly = 0xFFFF8011,
Max = 0xFFFFFFFF,
}
@ -81,8 +90,13 @@ impl From<SerializableInterface> for StructuredCloneTags {
fn from(v: SerializableInterface) -> Self {
match v {
SerializableInterface::Blob => StructuredCloneTags::DomBlob,
SerializableInterface::DomPointReadOnly => StructuredCloneTags::DomPointReadOnly,
SerializableInterface::DomPoint => StructuredCloneTags::DomPoint,
SerializableInterface::DomPointReadOnly => StructuredCloneTags::DomPointReadOnly,
SerializableInterface::DomRect => StructuredCloneTags::DomRect,
SerializableInterface::DomRectReadOnly => StructuredCloneTags::DomRectReadOnly,
SerializableInterface::DomQuad => StructuredCloneTags::DomQuad,
SerializableInterface::DomMatrix => StructuredCloneTags::DomMatrix,
SerializableInterface::DomMatrixReadOnly => StructuredCloneTags::DomMatrixReadOnly,
SerializableInterface::DomException => StructuredCloneTags::DomException,
SerializableInterface::ImageBitmap => StructuredCloneTags::ImageBitmap,
SerializableInterface::QuotaExceededError => StructuredCloneTags::QuotaExceededError,
@ -113,8 +127,13 @@ fn reader_for_type(
) -> *mut JSObject {
match val {
SerializableInterface::Blob => read_object::<Blob>,
SerializableInterface::DomPointReadOnly => read_object::<DOMPointReadOnly>,
SerializableInterface::DomPoint => read_object::<DOMPoint>,
SerializableInterface::DomPointReadOnly => read_object::<DOMPointReadOnly>,
SerializableInterface::DomRect => read_object::<DOMRect>,
SerializableInterface::DomRectReadOnly => read_object::<DOMRectReadOnly>,
SerializableInterface::DomQuad => read_object::<DOMQuad>,
SerializableInterface::DomMatrix => read_object::<DOMMatrix>,
SerializableInterface::DomMatrixReadOnly => read_object::<DOMMatrixReadOnly>,
SerializableInterface::DomException => read_object::<DOMException>,
SerializableInterface::ImageBitmap => read_object::<ImageBitmap>,
SerializableInterface::QuotaExceededError => read_object::<QuotaExceededError>,
@ -258,8 +277,13 @@ 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>,
SerializableInterface::DomPointReadOnly => try_serialize::<DOMPointReadOnly>,
SerializableInterface::DomRect => try_serialize::<DOMRect>,
SerializableInterface::DomRectReadOnly => try_serialize::<DOMRectReadOnly>,
SerializableInterface::DomQuad => try_serialize::<DOMQuad>,
SerializableInterface::DomMatrix => try_serialize::<DOMMatrix>,
SerializableInterface::DomMatrixReadOnly => try_serialize::<DOMMatrixReadOnly>,
SerializableInterface::DomException => try_serialize::<DOMException>,
SerializableInterface::ImageBitmap => try_serialize::<ImageBitmap>,
SerializableInterface::QuotaExceededError => try_serialize::<QuotaExceededError>,
@ -572,6 +596,12 @@ pub(crate) struct StructuredDataReader<'a> {
pub(crate) blob_impls: Option<HashMap<BlobId, BlobImpl>>,
/// A map of serialized points.
pub(crate) points: Option<HashMap<DomPointId, DomPoint>>,
/// A map of serialized rects.
pub(crate) rects: Option<HashMap<DomRectId, DomRect>>,
/// A map of serialized quads.
pub(crate) quads: Option<HashMap<DomQuadId, DomQuad>>,
/// A map of serialized matrices.
pub(crate) matrices: Option<HashMap<DomMatrixId, DomMatrix>>,
/// A map of serialized exceptions.
pub(crate) exceptions: Option<HashMap<DomExceptionId, DomException>>,
/// A map of serialized quota exceeded errors.
@ -597,6 +627,12 @@ pub(crate) struct StructuredDataWriter {
pub(crate) transform_streams_port: Option<HashMap<MessagePortId, TransformStreamData>>,
/// Serialized points.
pub(crate) points: Option<HashMap<DomPointId, DomPoint>>,
/// Serialized rects.
pub(crate) rects: Option<HashMap<DomRectId, DomRect>>,
/// Serialized quads.
pub(crate) quads: Option<HashMap<DomQuadId, DomQuad>>,
/// Serialized matrices.
pub(crate) matrices: Option<HashMap<DomMatrixId, DomMatrix>>,
/// Serialized exceptions.
pub(crate) exceptions: Option<HashMap<DomExceptionId, DomException>>,
/// Serialized quota exceeded errors.
@ -665,6 +701,9 @@ pub(crate) fn write(
ports: sc_writer.ports.take(),
transform_streams: sc_writer.transform_streams_port.take(),
points: sc_writer.points.take(),
rects: sc_writer.rects.take(),
quads: sc_writer.quads.take(),
matrices: sc_writer.matrices.take(),
exceptions: sc_writer.exceptions.take(),
quota_exceeded_errors: sc_writer.quota_exceeded_errors.take(),
blobs: sc_writer.blobs.take(),
@ -694,6 +733,9 @@ pub(crate) fn read(
transform_streams_port_impls: data.transform_streams.take(),
blob_impls: data.blobs.take(),
points: data.points.take(),
rects: data.rects.take(),
quads: data.quads.take(),
matrices: data.matrices.take(),
exceptions: data.exceptions.take(),
quota_exceeded_errors: data.quota_exceeded_errors.take(),
image_bitmaps: data.image_bitmaps.take(),