diff --git a/components/script/dom/bindings/serializable.rs b/components/script/dom/bindings/serializable.rs index d0e851b4799..6087ce49a7e 100644 --- a/components/script/dom/bindings/serializable.rs +++ b/components/script/dom/bindings/serializable.rs @@ -8,6 +8,7 @@ use std::collections::HashMap; use base::id::{Index, NamespaceIndex, PipelineNamespaceId}; +use script_bindings::structuredclone::MarkedAsSerializableInIdl; use crate::dom::bindings::reflector::DomObject; use crate::dom::bindings::root::DomRoot; @@ -45,7 +46,7 @@ impl From for NamespaceIndex { /// Interface for serializable platform objects. /// -pub(crate) trait Serializable: DomObject +pub(crate) trait Serializable: DomObject + MarkedAsSerializableInIdl where Self: Sized, { @@ -69,3 +70,5 @@ where data: StructuredData<'a, '_>, ) -> &'a mut Option, Self::Data>>; } + +pub(crate) fn assert_serializable() {} diff --git a/components/script/dom/bindings/transferable.rs b/components/script/dom/bindings/transferable.rs index 7650382b0c6..620c4f6a4d4 100644 --- a/components/script/dom/bindings/transferable.rs +++ b/components/script/dom/bindings/transferable.rs @@ -9,6 +9,7 @@ use std::collections::HashMap; use std::hash::Hash; use base::id::NamespaceIndex; +use script_bindings::structuredclone::MarkedAsTransferableInIdl; use crate::dom::bindings::error::Fallible; use crate::dom::bindings::reflector::DomObject; @@ -16,7 +17,7 @@ use crate::dom::bindings::root::DomRoot; use crate::dom::bindings::structuredclone::StructuredData; use crate::dom::globalscope::GlobalScope; -pub(crate) trait Transferable: DomObject +pub(crate) trait Transferable: DomObject + MarkedAsTransferableInIdl where Self: Sized, { @@ -41,3 +42,5 @@ where data: StructuredData<'a, '_>, ) -> &'a mut Option, Self::Data>>; } + +pub(crate) fn assert_transferable() {} diff --git a/components/script_bindings/codegen/codegen.py b/components/script_bindings/codegen/codegen.py index 545124b2409..8594b9fe45c 100644 --- a/components/script_bindings/codegen/codegen.py +++ b/components/script_bindings/codegen/codegen.py @@ -2782,6 +2782,10 @@ def DomTypes(descriptors, descriptorProvider, dictionaries, callbacks, typedefs, for parent in chain: traits += [f"crate::conversions::DerivedFrom"] + for marker in ["Serializable", "Transferable"]: + if descriptor.interface.getExtendedAttribute(marker): + traits += [f"crate::structuredclone::MarkedAs{marker}InIdl"] + iterableDecl = descriptor.interface.maplikeOrSetlikeOrIterable if iterableDecl: if iterableDecl.isMaplike(): @@ -7609,6 +7613,28 @@ class CGRegisterProxyHandlers(CGThing): return self.root.define() +class CGStructuredCloneMarker(CGThing): + """ + Generate a type assertion for inheritance + """ + def __init__(self, descriptor, marker): + CGThing.__init__(self) + self.descriptor = descriptor + self.marker = marker + self.marker_lower = marker.lower() + + def define(self): + ifaceName = self.descriptor.interface.identifier.name + return f""" +impl script_bindings::structuredclone::MarkedAs{self.marker}InIdl for {ifaceName} {{ + #[allow(path_statements)] + fn assert_{self.marker_lower}() {{ + crate::dom::bindings::{self.marker_lower}::assert_{self.marker_lower}::; + }} +}} +""" + + class CGConcreteBindingRoot(CGThing): """ Root codegen class for binding generation, specialized on the concrete @@ -7677,6 +7703,10 @@ class CGConcreteBindingRoot(CGThing): ), ] + for marker in ["Serializable", "Transferable"]: + if d.interface.getExtendedAttribute(marker): + cgthings += [CGStructuredCloneMarker(d, marker)] + if d.concrete: if not d.interface.isIteratorInterface(): cgthings.append(CGAssertInheritance(d)) diff --git a/components/script_bindings/lib.rs b/components/script_bindings/lib.rs index b2ca824a633..dde70fa47c9 100644 --- a/components/script_bindings/lib.rs +++ b/components/script_bindings/lib.rs @@ -41,6 +41,7 @@ pub mod root; pub mod script_runtime; pub mod settings_stack; pub mod str; +pub mod structuredclone; pub mod trace; pub mod utils; pub mod weakref; diff --git a/components/script_bindings/structuredclone.rs b/components/script_bindings/structuredclone.rs new file mode 100644 index 00000000000..d98e795023d --- /dev/null +++ b/components/script_bindings/structuredclone.rs @@ -0,0 +1,17 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +/// A marker to ensure that the `[Serializable]` attribute is present on +/// types that can be serialized. This trait should not be implemented manually. +pub trait MarkedAsSerializableInIdl { + /// Used to define compile-time assertions about the type implementing this trait. + fn assert_serializable(); +} + +/// A marker to ensure that the `[Transferable]` attribute is present on +/// types that can be transferred. This trait should not be implemented manually. +pub trait MarkedAsTransferableInIdl { + /// Used to define compile-time assertions about the type implementing this trait. + fn assert_transferable(); +} diff --git a/components/script_bindings/webidls/Blob.webidl b/components/script_bindings/webidls/Blob.webidl index 56c04a3be64..159a625c72f 100644 --- a/components/script_bindings/webidls/Blob.webidl +++ b/components/script_bindings/webidls/Blob.webidl @@ -4,7 +4,7 @@ // https://w3c.github.io/FileAPI/#blob -[Exposed=(Window,Worker)] +[Exposed=(Window,Worker), Serializable] interface Blob { [Throws] constructor(optional sequence blobParts, optional BlobPropertyBag options = {}); diff --git a/components/script_bindings/webidls/CanvasRenderingContext2D.webidl b/components/script_bindings/webidls/CanvasRenderingContext2D.webidl index af399842bd4..acc646f77f7 100644 --- a/components/script_bindings/webidls/CanvasRenderingContext2D.webidl +++ b/components/script_bindings/webidls/CanvasRenderingContext2D.webidl @@ -269,7 +269,7 @@ dictionary ImageDataSettings { }; [Exposed=(Window,Worker), - Serializable] + /*Serializable*/] interface ImageData { [Throws] constructor(unsigned long sw, unsigned long sh, optional ImageDataSettings settings = {}); [Throws] constructor(ImageDataArray data, unsigned long sw, diff --git a/components/script_bindings/webidls/CryptoKey.webidl b/components/script_bindings/webidls/CryptoKey.webidl index 4387ef75d98..3540a2e9db7 100644 --- a/components/script_bindings/webidls/CryptoKey.webidl +++ b/components/script_bindings/webidls/CryptoKey.webidl @@ -8,7 +8,7 @@ enum KeyType { "public", "private", "secret" }; enum KeyUsage { "encrypt", "decrypt", "sign", "verify", "deriveKey", "deriveBits", "wrapKey", "unwrapKey" }; -[SecureContext, Exposed=(Window,Worker), Serializable, Pref="dom_crypto_subtle_enabled"] +[SecureContext, Exposed=(Window,Worker), /*Serializable,*/ Pref="dom_crypto_subtle_enabled"] interface CryptoKey { readonly attribute KeyType type; readonly attribute boolean extractable; diff --git a/components/script_bindings/webidls/DOMException.webidl b/components/script_bindings/webidls/DOMException.webidl index d61d30151fa..b0ed423cc1c 100644 --- a/components/script_bindings/webidls/DOMException.webidl +++ b/components/script_bindings/webidls/DOMException.webidl @@ -9,7 +9,8 @@ [ ExceptionClass, - Exposed=(Window,Worker,Worklet,DissimilarOriginWindow) + Exposed=(Window,Worker,Worklet,DissimilarOriginWindow), + Serializable, ] interface DOMException { [Throws] constructor(optional DOMString message="", optional DOMString name="Error"); diff --git a/components/script_bindings/webidls/DOMPoint.webidl b/components/script_bindings/webidls/DOMPoint.webidl index 765c62df573..88127f6b015 100644 --- a/components/script_bindings/webidls/DOMPoint.webidl +++ b/components/script_bindings/webidls/DOMPoint.webidl @@ -10,7 +10,8 @@ */ // http://dev.w3.org/fxtf/geometry/Overview.html#dompoint -[Exposed=(Window,Worker,PaintWorklet)] +[Exposed=(Window,Worker,PaintWorklet), + Serializable] interface DOMPoint : DOMPointReadOnly { [Throws] constructor(optional unrestricted double x = 0, optional unrestricted double y = 0, optional unrestricted double z = 0, optional unrestricted double w = 1); diff --git a/components/script_bindings/webidls/DOMPointReadOnly.webidl b/components/script_bindings/webidls/DOMPointReadOnly.webidl index 98940ed3f08..3ce4bf773fc 100644 --- a/components/script_bindings/webidls/DOMPointReadOnly.webidl +++ b/components/script_bindings/webidls/DOMPointReadOnly.webidl @@ -10,7 +10,8 @@ */ // http://dev.w3.org/fxtf/geometry/Overview.html#dompointreadonly -[Exposed=(Window,Worker,PaintWorklet)] +[Exposed=(Window,Worker,PaintWorklet), + Serializable] interface DOMPointReadOnly { [Throws] constructor(optional unrestricted double x = 0, optional unrestricted double y = 0, optional unrestricted double z = 0, optional unrestricted double w = 1); diff --git a/components/script_bindings/webidls/DOMRect.webidl b/components/script_bindings/webidls/DOMRect.webidl index aee406502fc..1f987943e6d 100644 --- a/components/script_bindings/webidls/DOMRect.webidl +++ b/components/script_bindings/webidls/DOMRect.webidl @@ -5,7 +5,7 @@ // https://drafts.fxtf.org/geometry/#domrect [Exposed=(Window,Worker), - Serializable, + /*Serializable,*/ LegacyWindowAlias=SVGRect] interface DOMRect : DOMRectReadOnly { [Throws] constructor(optional unrestricted double x = 0, optional unrestricted double y = 0, diff --git a/components/script_bindings/webidls/DOMRectReadOnly.webidl b/components/script_bindings/webidls/DOMRectReadOnly.webidl index b8bc3057675..5ff06230f59 100644 --- a/components/script_bindings/webidls/DOMRectReadOnly.webidl +++ b/components/script_bindings/webidls/DOMRectReadOnly.webidl @@ -5,7 +5,7 @@ // https://drafts.fxtf.org/geometry/#domrect [Exposed=(Window,Worker), - Serializable] + /*Serializable*/] interface DOMRectReadOnly { [Throws] constructor(optional unrestricted double x = 0, optional unrestricted double y = 0, optional unrestricted double width = 0, optional unrestricted double height = 0); diff --git a/components/script_bindings/webidls/MessagePort.webidl b/components/script_bindings/webidls/MessagePort.webidl index b7082fc7fc3..06a3c740d85 100644 --- a/components/script_bindings/webidls/MessagePort.webidl +++ b/components/script_bindings/webidls/MessagePort.webidl @@ -6,7 +6,7 @@ * https://html.spec.whatwg.org/multipage/#messageport */ -[Exposed=(Window,Worker)] +[Exposed=(Window,Worker), Transferable] interface MessagePort : EventTarget { [Throws] undefined postMessage(any message, sequence transfer); [Throws] undefined postMessage(any message, optional StructuredSerializeOptions options = {}); diff --git a/components/script_bindings/webidls/ReadableStream.webidl b/components/script_bindings/webidls/ReadableStream.webidl index d1f2a69a6e0..941b5a9f4a6 100644 --- a/components/script_bindings/webidls/ReadableStream.webidl +++ b/components/script_bindings/webidls/ReadableStream.webidl @@ -4,7 +4,7 @@ // https://streams.spec.whatwg.org/#readablestream -[Exposed=*] // [Transferable] - See Bug 1562065 +[Exposed=*, Transferable] interface _ReadableStream { [Throws] constructor(optional object underlyingSource, optional QueuingStrategy strategy = {}); diff --git a/components/script_bindings/webidls/TransformStream.webidl b/components/script_bindings/webidls/TransformStream.webidl index c36a49b114a..dc16587e5bb 100644 --- a/components/script_bindings/webidls/TransformStream.webidl +++ b/components/script_bindings/webidls/TransformStream.webidl @@ -6,7 +6,7 @@ * https://streams.spec.whatwg.org/#ts-class-definition */ -[Exposed=*] // [Transferable] - See Bug 1562065 +[Exposed=*, Transferable] interface TransformStream { [Throws] constructor(optional object transformer, diff --git a/components/script_bindings/webidls/WebGPU.webidl b/components/script_bindings/webidls/WebGPU.webidl index 177a12d3563..7e561e02465 100644 --- a/components/script_bindings/webidls/WebGPU.webidl +++ b/components/script_bindings/webidls/WebGPU.webidl @@ -170,7 +170,7 @@ interface GPUDevice: EventTarget { }; GPUDevice includes GPUObjectBase; -[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] +[Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"] interface GPUBuffer { readonly attribute GPUSize64Out size; readonly attribute GPUFlagsConstant usage; @@ -222,7 +222,7 @@ namespace GPUMapMode { const GPUFlagsConstant WRITE = 0x0002; }; -[Exposed=(Window, DedicatedWorker), Serializable , Pref="dom_webgpu_enabled"] +[Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"] interface GPUTexture { [Throws, NewObject] GPUTextureView createView(optional GPUTextureViewDescriptor descriptor = {}); @@ -458,7 +458,7 @@ enum GPUCompareFunction { "always" }; -[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] +[Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"] interface GPUBindGroupLayout { }; GPUBindGroupLayout includes GPUObjectBase; @@ -479,7 +479,7 @@ dictionary GPUBindGroupLayoutEntry { }; typedef [EnforceRange] unsigned long GPUShaderStageFlags; -[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] +[Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"] interface GPUShaderStage { const GPUShaderStageFlags VERTEX = 1; const GPUShaderStageFlags FRAGMENT = 2; @@ -560,7 +560,7 @@ dictionary GPUBufferBinding { GPUSize64 size; }; -[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] +[Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"] interface GPUPipelineLayout { }; GPUPipelineLayout includes GPUObjectBase; @@ -569,7 +569,7 @@ dictionary GPUPipelineLayoutDescriptor : GPUObjectDescriptorBase { required sequence bindGroupLayouts; }; -[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] +[Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"] interface GPUShaderModule { Promise getCompilationInfo(); }; @@ -638,7 +638,7 @@ dictionary GPUProgrammableStage { typedef double GPUPipelineConstantValue; // May represent WGSL's bool, f32, i32, u32, and f16 if enabled. -[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] +[Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"] interface GPUComputePipeline { }; GPUComputePipeline includes GPUObjectBase; @@ -648,7 +648,7 @@ dictionary GPUComputePipelineDescriptor : GPUPipelineDescriptorBase { required GPUProgrammableStage compute; }; -[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] +[Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"] interface GPURenderPipeline { }; GPURenderPipeline includes GPUObjectBase; @@ -873,7 +873,7 @@ dictionary GPUImageCopyExternalImage { boolean flipY = false; }; -[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] +[Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"] interface GPUCommandBuffer { }; GPUCommandBuffer includes GPUObjectBase; @@ -881,7 +881,7 @@ GPUCommandBuffer includes GPUObjectBase; dictionary GPUCommandBufferDescriptor : GPUObjectDescriptorBase { }; -[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] +[Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"] interface GPUCommandEncoder { [NewObject] GPUComputePassEncoder beginComputePass(optional GPUComputePassDescriptor descriptor = {}); @@ -939,7 +939,7 @@ dictionary GPUCommandEncoderDescriptor : GPUObjectDescriptorBase { boolean measureExecutionTime = false; }; -[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] +[Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"] interface GPUComputePassEncoder { undefined setPipeline(GPUComputePipeline pipeline); undefined dispatchWorkgroups(GPUSize32 x, optional GPUSize32 y = 1, optional GPUSize32 z = 1); @@ -1090,7 +1090,7 @@ dictionary GPURenderBundleEncoderDescriptor : GPURenderPassLayout { boolean stencilReadOnly = false; }; -[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] +[Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"] interface GPUQueue { undefined submit(sequence buffers); @@ -1119,7 +1119,7 @@ interface GPUQueue { }; GPUQueue includes GPUObjectBase; -[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] +[Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"] interface GPUQuerySet { undefined destroy(); }; diff --git a/components/script_bindings/webidls/WritableStream.webidl b/components/script_bindings/webidls/WritableStream.webidl index 7aebc435a7a..76312936d84 100644 --- a/components/script_bindings/webidls/WritableStream.webidl +++ b/components/script_bindings/webidls/WritableStream.webidl @@ -4,7 +4,7 @@ // https://streams.spec.whatwg.org/#writablestream -[Exposed=*] // [Transferable] - See Bug 1562065 +[Exposed=*, Transferable] interface WritableStream { [Throws] constructor(optional object underlyingSink, optional QueuingStrategy strategy = {});