script_bindings: Assert that serializable/transferable types have accurate WebIDL annotations (#38615)

These changes add compile-time assertions that:
* any type that implements the Serializable/Transferable trait has a
`[Serializable]` or `[Transferable]` annotation in the interface WebIDL
* any WebIDL interface with the `[Serializable]` or `[Transferable]`
annotation implements the corresponding trait

This is useful because it means that WebIDL definitions will be less
confusing if you're trying to figure out whether Servo supports
serializing/transferring a particular interface type. It also makes
fixing #21715 in the future a little bit easier, because the annotations
will remain up to date.

Testing: compile-time only; no point in writing tests for this since it
involves webidl codegen.

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2025-08-13 04:36:04 -04:00 committed by GitHub
parent 0d6d434e59
commit bd9bb77295
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 84 additions and 27 deletions

View file

@ -8,6 +8,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use base::id::{Index, NamespaceIndex, PipelineNamespaceId}; use base::id::{Index, NamespaceIndex, PipelineNamespaceId};
use script_bindings::structuredclone::MarkedAsSerializableInIdl;
use crate::dom::bindings::reflector::DomObject; use crate::dom::bindings::reflector::DomObject;
use crate::dom::bindings::root::DomRoot; use crate::dom::bindings::root::DomRoot;
@ -45,7 +46,7 @@ impl<T> From<StorageKey> for NamespaceIndex<T> {
/// Interface for serializable platform objects. /// Interface for serializable platform objects.
/// <https://html.spec.whatwg.org/multipage/#serializable> /// <https://html.spec.whatwg.org/multipage/#serializable>
pub(crate) trait Serializable: DomObject pub(crate) trait Serializable: DomObject + MarkedAsSerializableInIdl
where where
Self: Sized, Self: Sized,
{ {
@ -69,3 +70,5 @@ where
data: StructuredData<'a, '_>, data: StructuredData<'a, '_>,
) -> &'a mut Option<HashMap<NamespaceIndex<Self::Index>, Self::Data>>; ) -> &'a mut Option<HashMap<NamespaceIndex<Self::Index>, Self::Data>>;
} }
pub(crate) fn assert_serializable<T: Serializable>() {}

View file

@ -9,6 +9,7 @@ use std::collections::HashMap;
use std::hash::Hash; use std::hash::Hash;
use base::id::NamespaceIndex; use base::id::NamespaceIndex;
use script_bindings::structuredclone::MarkedAsTransferableInIdl;
use crate::dom::bindings::error::Fallible; use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::reflector::DomObject; 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::bindings::structuredclone::StructuredData;
use crate::dom::globalscope::GlobalScope; use crate::dom::globalscope::GlobalScope;
pub(crate) trait Transferable: DomObject pub(crate) trait Transferable: DomObject + MarkedAsTransferableInIdl
where where
Self: Sized, Self: Sized,
{ {
@ -41,3 +42,5 @@ where
data: StructuredData<'a, '_>, data: StructuredData<'a, '_>,
) -> &'a mut Option<HashMap<NamespaceIndex<Self::Index>, Self::Data>>; ) -> &'a mut Option<HashMap<NamespaceIndex<Self::Index>, Self::Data>>;
} }
pub(crate) fn assert_transferable<T: Transferable>() {}

View file

@ -2782,6 +2782,10 @@ def DomTypes(descriptors, descriptorProvider, dictionaries, callbacks, typedefs,
for parent in chain: for parent in chain:
traits += [f"crate::conversions::DerivedFrom<Self::{parent}>"] traits += [f"crate::conversions::DerivedFrom<Self::{parent}>"]
for marker in ["Serializable", "Transferable"]:
if descriptor.interface.getExtendedAttribute(marker):
traits += [f"crate::structuredclone::MarkedAs{marker}InIdl"]
iterableDecl = descriptor.interface.maplikeOrSetlikeOrIterable iterableDecl = descriptor.interface.maplikeOrSetlikeOrIterable
if iterableDecl: if iterableDecl:
if iterableDecl.isMaplike(): if iterableDecl.isMaplike():
@ -7609,6 +7613,28 @@ class CGRegisterProxyHandlers(CGThing):
return self.root.define() 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}::<Self>;
}}
}}
"""
class CGConcreteBindingRoot(CGThing): class CGConcreteBindingRoot(CGThing):
""" """
Root codegen class for binding generation, specialized on the concrete 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 d.concrete:
if not d.interface.isIteratorInterface(): if not d.interface.isIteratorInterface():
cgthings.append(CGAssertInheritance(d)) cgthings.append(CGAssertInheritance(d))

View file

@ -41,6 +41,7 @@ pub mod root;
pub mod script_runtime; pub mod script_runtime;
pub mod settings_stack; pub mod settings_stack;
pub mod str; pub mod str;
pub mod structuredclone;
pub mod trace; pub mod trace;
pub mod utils; pub mod utils;
pub mod weakref; pub mod weakref;

View file

@ -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();
}

View file

@ -4,7 +4,7 @@
// https://w3c.github.io/FileAPI/#blob // https://w3c.github.io/FileAPI/#blob
[Exposed=(Window,Worker)] [Exposed=(Window,Worker), Serializable]
interface Blob { interface Blob {
[Throws] constructor(optional sequence<BlobPart> blobParts, [Throws] constructor(optional sequence<BlobPart> blobParts,
optional BlobPropertyBag options = {}); optional BlobPropertyBag options = {});

View file

@ -269,7 +269,7 @@ dictionary ImageDataSettings {
}; };
[Exposed=(Window,Worker), [Exposed=(Window,Worker),
Serializable] /*Serializable*/]
interface ImageData { interface ImageData {
[Throws] constructor(unsigned long sw, unsigned long sh, optional ImageDataSettings settings = {}); [Throws] constructor(unsigned long sw, unsigned long sh, optional ImageDataSettings settings = {});
[Throws] constructor(ImageDataArray data, unsigned long sw, [Throws] constructor(ImageDataArray data, unsigned long sw,

View file

@ -8,7 +8,7 @@ enum KeyType { "public", "private", "secret" };
enum KeyUsage { "encrypt", "decrypt", "sign", "verify", "deriveKey", "deriveBits", "wrapKey", "unwrapKey" }; 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 { interface CryptoKey {
readonly attribute KeyType type; readonly attribute KeyType type;
readonly attribute boolean extractable; readonly attribute boolean extractable;

View file

@ -9,7 +9,8 @@
[ [
ExceptionClass, ExceptionClass,
Exposed=(Window,Worker,Worklet,DissimilarOriginWindow) Exposed=(Window,Worker,Worklet,DissimilarOriginWindow),
Serializable,
] ]
interface DOMException { interface DOMException {
[Throws] constructor(optional DOMString message="", optional DOMString name="Error"); [Throws] constructor(optional DOMString message="", optional DOMString name="Error");

View file

@ -10,7 +10,8 @@
*/ */
// http://dev.w3.org/fxtf/geometry/Overview.html#dompoint // http://dev.w3.org/fxtf/geometry/Overview.html#dompoint
[Exposed=(Window,Worker,PaintWorklet)] [Exposed=(Window,Worker,PaintWorklet),
Serializable]
interface DOMPoint : DOMPointReadOnly { interface DOMPoint : DOMPointReadOnly {
[Throws] constructor(optional unrestricted double x = 0, optional unrestricted double y = 0, [Throws] constructor(optional unrestricted double x = 0, optional unrestricted double y = 0,
optional unrestricted double z = 0, optional unrestricted double w = 1); optional unrestricted double z = 0, optional unrestricted double w = 1);

View file

@ -10,7 +10,8 @@
*/ */
// http://dev.w3.org/fxtf/geometry/Overview.html#dompointreadonly // http://dev.w3.org/fxtf/geometry/Overview.html#dompointreadonly
[Exposed=(Window,Worker,PaintWorklet)] [Exposed=(Window,Worker,PaintWorklet),
Serializable]
interface DOMPointReadOnly { interface DOMPointReadOnly {
[Throws] constructor(optional unrestricted double x = 0, optional unrestricted double y = 0, [Throws] constructor(optional unrestricted double x = 0, optional unrestricted double y = 0,
optional unrestricted double z = 0, optional unrestricted double w = 1); optional unrestricted double z = 0, optional unrestricted double w = 1);

View file

@ -5,7 +5,7 @@
// https://drafts.fxtf.org/geometry/#domrect // https://drafts.fxtf.org/geometry/#domrect
[Exposed=(Window,Worker), [Exposed=(Window,Worker),
Serializable, /*Serializable,*/
LegacyWindowAlias=SVGRect] LegacyWindowAlias=SVGRect]
interface DOMRect : DOMRectReadOnly { interface DOMRect : DOMRectReadOnly {
[Throws] constructor(optional unrestricted double x = 0, optional unrestricted double y = 0, [Throws] constructor(optional unrestricted double x = 0, optional unrestricted double y = 0,

View file

@ -5,7 +5,7 @@
// https://drafts.fxtf.org/geometry/#domrect // https://drafts.fxtf.org/geometry/#domrect
[Exposed=(Window,Worker), [Exposed=(Window,Worker),
Serializable] /*Serializable*/]
interface DOMRectReadOnly { interface DOMRectReadOnly {
[Throws] constructor(optional unrestricted double x = 0, optional unrestricted double y = 0, [Throws] constructor(optional unrestricted double x = 0, optional unrestricted double y = 0,
optional unrestricted double width = 0, optional unrestricted double height = 0); optional unrestricted double width = 0, optional unrestricted double height = 0);

View file

@ -6,7 +6,7 @@
* https://html.spec.whatwg.org/multipage/#messageport * https://html.spec.whatwg.org/multipage/#messageport
*/ */
[Exposed=(Window,Worker)] [Exposed=(Window,Worker), Transferable]
interface MessagePort : EventTarget { interface MessagePort : EventTarget {
[Throws] undefined postMessage(any message, sequence<object> transfer); [Throws] undefined postMessage(any message, sequence<object> transfer);
[Throws] undefined postMessage(any message, optional StructuredSerializeOptions options = {}); [Throws] undefined postMessage(any message, optional StructuredSerializeOptions options = {});

View file

@ -4,7 +4,7 @@
// https://streams.spec.whatwg.org/#readablestream // https://streams.spec.whatwg.org/#readablestream
[Exposed=*] // [Transferable] - See Bug 1562065 [Exposed=*, Transferable]
interface _ReadableStream { interface _ReadableStream {
[Throws] [Throws]
constructor(optional object underlyingSource, optional QueuingStrategy strategy = {}); constructor(optional object underlyingSource, optional QueuingStrategy strategy = {});

View file

@ -6,7 +6,7 @@
* https://streams.spec.whatwg.org/#ts-class-definition * https://streams.spec.whatwg.org/#ts-class-definition
*/ */
[Exposed=*] // [Transferable] - See Bug 1562065 [Exposed=*, Transferable]
interface TransformStream { interface TransformStream {
[Throws] [Throws]
constructor(optional object transformer, constructor(optional object transformer,

View file

@ -170,7 +170,7 @@ interface GPUDevice: EventTarget {
}; };
GPUDevice includes GPUObjectBase; GPUDevice includes GPUObjectBase;
[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] [Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"]
interface GPUBuffer { interface GPUBuffer {
readonly attribute GPUSize64Out size; readonly attribute GPUSize64Out size;
readonly attribute GPUFlagsConstant usage; readonly attribute GPUFlagsConstant usage;
@ -222,7 +222,7 @@ namespace GPUMapMode {
const GPUFlagsConstant WRITE = 0x0002; const GPUFlagsConstant WRITE = 0x0002;
}; };
[Exposed=(Window, DedicatedWorker), Serializable , Pref="dom_webgpu_enabled"] [Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"]
interface GPUTexture { interface GPUTexture {
[Throws, NewObject] [Throws, NewObject]
GPUTextureView createView(optional GPUTextureViewDescriptor descriptor = {}); GPUTextureView createView(optional GPUTextureViewDescriptor descriptor = {});
@ -458,7 +458,7 @@ enum GPUCompareFunction {
"always" "always"
}; };
[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] [Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"]
interface GPUBindGroupLayout { interface GPUBindGroupLayout {
}; };
GPUBindGroupLayout includes GPUObjectBase; GPUBindGroupLayout includes GPUObjectBase;
@ -479,7 +479,7 @@ dictionary GPUBindGroupLayoutEntry {
}; };
typedef [EnforceRange] unsigned long GPUShaderStageFlags; typedef [EnforceRange] unsigned long GPUShaderStageFlags;
[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] [Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"]
interface GPUShaderStage { interface GPUShaderStage {
const GPUShaderStageFlags VERTEX = 1; const GPUShaderStageFlags VERTEX = 1;
const GPUShaderStageFlags FRAGMENT = 2; const GPUShaderStageFlags FRAGMENT = 2;
@ -560,7 +560,7 @@ dictionary GPUBufferBinding {
GPUSize64 size; GPUSize64 size;
}; };
[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] [Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"]
interface GPUPipelineLayout { interface GPUPipelineLayout {
}; };
GPUPipelineLayout includes GPUObjectBase; GPUPipelineLayout includes GPUObjectBase;
@ -569,7 +569,7 @@ dictionary GPUPipelineLayoutDescriptor : GPUObjectDescriptorBase {
required sequence<GPUBindGroupLayout> bindGroupLayouts; required sequence<GPUBindGroupLayout> bindGroupLayouts;
}; };
[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] [Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"]
interface GPUShaderModule { interface GPUShaderModule {
Promise<GPUCompilationInfo> getCompilationInfo(); Promise<GPUCompilationInfo> getCompilationInfo();
}; };
@ -638,7 +638,7 @@ dictionary GPUProgrammableStage {
typedef double GPUPipelineConstantValue; // May represent WGSL's bool, f32, i32, u32, and f16 if enabled. 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 { interface GPUComputePipeline {
}; };
GPUComputePipeline includes GPUObjectBase; GPUComputePipeline includes GPUObjectBase;
@ -648,7 +648,7 @@ dictionary GPUComputePipelineDescriptor : GPUPipelineDescriptorBase {
required GPUProgrammableStage compute; required GPUProgrammableStage compute;
}; };
[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] [Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"]
interface GPURenderPipeline { interface GPURenderPipeline {
}; };
GPURenderPipeline includes GPUObjectBase; GPURenderPipeline includes GPUObjectBase;
@ -873,7 +873,7 @@ dictionary GPUImageCopyExternalImage {
boolean flipY = false; boolean flipY = false;
}; };
[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] [Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"]
interface GPUCommandBuffer { interface GPUCommandBuffer {
}; };
GPUCommandBuffer includes GPUObjectBase; GPUCommandBuffer includes GPUObjectBase;
@ -881,7 +881,7 @@ GPUCommandBuffer includes GPUObjectBase;
dictionary GPUCommandBufferDescriptor : GPUObjectDescriptorBase { dictionary GPUCommandBufferDescriptor : GPUObjectDescriptorBase {
}; };
[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] [Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"]
interface GPUCommandEncoder { interface GPUCommandEncoder {
[NewObject] [NewObject]
GPUComputePassEncoder beginComputePass(optional GPUComputePassDescriptor descriptor = {}); GPUComputePassEncoder beginComputePass(optional GPUComputePassDescriptor descriptor = {});
@ -939,7 +939,7 @@ dictionary GPUCommandEncoderDescriptor : GPUObjectDescriptorBase {
boolean measureExecutionTime = false; boolean measureExecutionTime = false;
}; };
[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] [Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"]
interface GPUComputePassEncoder { interface GPUComputePassEncoder {
undefined setPipeline(GPUComputePipeline pipeline); undefined setPipeline(GPUComputePipeline pipeline);
undefined dispatchWorkgroups(GPUSize32 x, optional GPUSize32 y = 1, optional GPUSize32 z = 1); undefined dispatchWorkgroups(GPUSize32 x, optional GPUSize32 y = 1, optional GPUSize32 z = 1);
@ -1090,7 +1090,7 @@ dictionary GPURenderBundleEncoderDescriptor : GPURenderPassLayout {
boolean stencilReadOnly = false; boolean stencilReadOnly = false;
}; };
[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] [Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"]
interface GPUQueue { interface GPUQueue {
undefined submit(sequence<GPUCommandBuffer> buffers); undefined submit(sequence<GPUCommandBuffer> buffers);
@ -1119,7 +1119,7 @@ interface GPUQueue {
}; };
GPUQueue includes GPUObjectBase; GPUQueue includes GPUObjectBase;
[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom_webgpu_enabled"] [Exposed=(Window, DedicatedWorker), /*Serializable,*/ Pref="dom_webgpu_enabled"]
interface GPUQuerySet { interface GPUQuerySet {
undefined destroy(); undefined destroy();
}; };

View file

@ -4,7 +4,7 @@
// https://streams.spec.whatwg.org/#writablestream // https://streams.spec.whatwg.org/#writablestream
[Exposed=*] // [Transferable] - See Bug 1562065 [Exposed=*, Transferable]
interface WritableStream { interface WritableStream {
[Throws] [Throws]
constructor(optional object underlyingSink, optional QueuingStrategy strategy = {}); constructor(optional object underlyingSink, optional QueuingStrategy strategy = {});