feat: add can_gc argument to to_frozen_array (#36043)

* feat: add can_gc argument to to_frozen_array

Signed-off-by: Arya Nair <aryaajitnair@gmail.com>

* fix: linting issues

Signed-off-by: Arya Nair <aryaajitnair@gmail.com>

* feat: add can_gc in binding.conf

Signed-off-by: Arya Nair <aryaajitnair@gmail.com>

* fix: linting issues

Signed-off-by: Arya Nair <aryaajitnair@gmail.com>

---------

Signed-off-by: Arya Nair <aryaajitnair@gmail.com>
This commit is contained in:
Arya Nair 2025-03-19 23:33:09 +05:30 committed by GitHub
parent 0917e080df
commit cb56ac8561
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 97 additions and 43 deletions

View file

@ -9,7 +9,7 @@ use js::rust::MutableHandleValue;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::utils::to_frozen_array;
use crate::script_runtime::JSContext;
use crate::script_runtime::{CanGc, JSContext};
#[derive(JSTraceable)]
pub(crate) struct CachedFrozenArray {
@ -28,6 +28,7 @@ impl CachedFrozenArray {
f: F,
cx: JSContext,
mut retval: MutableHandleValue,
can_gc: CanGc,
) {
if let Some(inner) = &*self.frozen_value.borrow() {
retval.set(inner.get());
@ -35,7 +36,7 @@ impl CachedFrozenArray {
}
let array = f();
to_frozen_array(array.as_slice(), cx, retval);
to_frozen_array(array.as_slice(), cx, retval, can_gc);
// Safety: need to create the Heap value in its final memory location before setting it.
*self.frozen_value.borrow_mut() = Some(Heap::default());

View file

@ -123,6 +123,7 @@ pub(crate) fn to_frozen_array<T: ToJSValConvertible>(
convertibles: &[T],
cx: SafeJSContext,
rval: MutableHandleValue,
_can_gc: CanGc,
) {
unsafe { convertibles.to_jsval(*cx, rval) };

View file

@ -69,13 +69,13 @@ impl SpecificCSSRule for CSSLayerStatementRule {
impl CSSLayerStatementRuleMethods<crate::DomTypeHolder> for CSSLayerStatementRule {
/// <https://drafts.csswg.org/css-cascade-5/#dom-csslayerstatementrule-namelist>
fn NameList(&self, cx: SafeJSContext, retval: MutableHandleValue) {
fn NameList(&self, cx: SafeJSContext, can_gc: CanGc, retval: MutableHandleValue) {
let names: Vec<DOMString> = self
.layerstatementrule
.names
.iter()
.map(|name| DOMString::from_string(name.to_css_string()))
.collect();
to_frozen_array(names.as_slice(), cx, retval)
to_frozen_array(names.as_slice(), cx, retval, can_gc)
}
}

View file

@ -160,8 +160,8 @@ impl DataTransferMethods<crate::DomTypeHolder> for DataTransfer {
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-types>
fn Types(&self, cx: JSContext, retval: MutableHandleValue) {
self.items.frozen_types(cx, retval);
fn Types(&self, cx: JSContext, can_gc: CanGc, retval: MutableHandleValue) {
self.items.frozen_types(cx, retval, can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-getdata>

View file

@ -51,7 +51,7 @@ impl DataTransferItemList {
)
}
pub(crate) fn frozen_types(&self, cx: JSContext, retval: MutableHandleValue) {
pub(crate) fn frozen_types(&self, cx: JSContext, retval: MutableHandleValue, can_gc: CanGc) {
self.frozen_types.get_or_init(
|| {
self.data_store
@ -61,6 +61,7 @@ impl DataTransferItemList {
},
cx,
retval,
can_gc,
);
}

View file

@ -207,7 +207,7 @@ impl ExtendableMessageEventMethods<crate::DomTypeHolder> for ExtendableMessageEv
}
/// <https://w3c.github.io/ServiceWorker/#extendablemessage-event-ports>
fn Ports(&self, cx: JSContext, retval: MutableHandleValue) {
fn Ports(&self, cx: JSContext, can_gc: CanGc, retval: MutableHandleValue) {
self.frozen_ports.get_or_init(
|| {
self.ports
@ -217,6 +217,7 @@ impl ExtendableMessageEventMethods<crate::DomTypeHolder> for ExtendableMessageEv
},
cx,
retval,
can_gc,
);
}
}

View file

@ -126,8 +126,8 @@ impl GamepadHapticActuator {
impl GamepadHapticActuatorMethods<crate::DomTypeHolder> for GamepadHapticActuator {
/// <https://www.w3.org/TR/gamepad/#dom-gamepadhapticactuator-effects>
fn Effects(&self, cx: JSContext, retval: MutableHandleValue) {
to_frozen_array(self.effects.as_slice(), cx, retval)
fn Effects(&self, cx: JSContext, can_gc: CanGc, retval: MutableHandleValue) {
to_frozen_array(self.effects.as_slice(), cx, retval, can_gc)
}
/// <https://www.w3.org/TR/gamepad/#dom-gamepadhapticactuator-playeffect>

View file

@ -2908,6 +2908,7 @@ impl GlobalScope {
&self,
cx: SafeJSContext,
retval: MutableHandleValue,
can_gc: CanGc,
) {
self.frozen_supported_performance_entry_types.get_or_init(
|| {
@ -2918,6 +2919,7 @@ impl GlobalScope {
},
cx,
retval,
can_gc,
);
}

View file

@ -691,8 +691,8 @@ impl IntersectionObserverMethods<crate::DomTypeHolder> for IntersectionObserver
/// > constructor, or the sequence is empty, the value of this attribute will be `[0]`.
///
/// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-thresholds>
fn Thresholds(&self, context: JSContext, retval: MutableHandleValue) {
to_frozen_array(&self.thresholds.borrow(), context, retval);
fn Thresholds(&self, context: JSContext, can_gc: CanGc, retval: MutableHandleValue) {
to_frozen_array(&self.thresholds.borrow(), context, retval, can_gc);
}
/// > A number indicating the minimum delay in milliseconds between notifications from

View file

@ -302,7 +302,7 @@ impl MessageEventMethods<crate::DomTypeHolder> for MessageEvent {
}
/// <https://html.spec.whatwg.org/multipage/#dom-messageevent-ports>
fn Ports(&self, cx: JSContext, retval: MutableHandleValue) {
fn Ports(&self, cx: JSContext, can_gc: CanGc, retval: MutableHandleValue) {
self.frozen_ports.get_or_init(
|| {
self.ports
@ -313,6 +313,7 @@ impl MessageEventMethods<crate::DomTypeHolder> for MessageEvent {
},
cx,
retval,
can_gc,
);
}

View file

@ -214,8 +214,8 @@ impl NavigatorMethods<crate::DomTypeHolder> for Navigator {
// https://html.spec.whatwg.org/multipage/#dom-navigator-languages
#[allow(unsafe_code)]
fn Languages(&self, cx: JSContext, retval: MutableHandleValue) {
to_frozen_array(&[self.Language()], cx, retval)
fn Languages(&self, cx: JSContext, can_gc: CanGc, retval: MutableHandleValue) {
to_frozen_array(&[self.Language()], cx, retval, can_gc)
}
// https://html.spec.whatwg.org/multipage/#dom-navigator-plugins

View file

@ -441,7 +441,7 @@ impl NotificationMethods<crate::DomTypeHolder> for Notification {
retval.set(self.data.get());
}
/// <https://notifications.spec.whatwg.org/#dom-notification-actions>
fn Actions(&self, cx: SafeJSContext, retval: MutableHandleValue) {
fn Actions(&self, cx: SafeJSContext, can_gc: CanGc, retval: MutableHandleValue) {
// step 1: Let frozenActions be an empty list of type NotificationAction.
let mut frozen_actions: Vec<NotificationAction> = Vec::new();
@ -461,11 +461,11 @@ impl NotificationMethods<crate::DomTypeHolder> for Notification {
}
// step 3: Return the result of create a frozen array from frozenActions.
to_frozen_array(frozen_actions.as_slice(), cx, retval);
to_frozen_array(frozen_actions.as_slice(), cx, retval, can_gc);
}
/// <https://notifications.spec.whatwg.org/#dom-notification-vibrate>
fn Vibrate(&self, cx: SafeJSContext, retval: MutableHandleValue) {
to_frozen_array(self.vibration_pattern.as_slice(), cx, retval);
fn Vibrate(&self, cx: SafeJSContext, can_gc: CanGc, retval: MutableHandleValue) {
to_frozen_array(self.vibration_pattern.as_slice(), cx, retval, can_gc);
}
/// <https://notifications.spec.whatwg.org/#dom-notification-timestamp>
fn Timestamp(&self) -> u64 {

View file

@ -141,10 +141,15 @@ impl PerformanceObserverMethods<crate::DomTypeHolder> for PerformanceObserver {
}
// https://w3c.github.io/performance-timeline/#supportedentrytypes-attribute
fn SupportedEntryTypes(cx: JSContext, global: &GlobalScope, retval: MutableHandleValue) {
fn SupportedEntryTypes(
cx: JSContext,
global: &GlobalScope,
can_gc: CanGc,
retval: MutableHandleValue,
) {
// While this is exposed through a method of PerformanceObserver,
// it is specified as associated with the global scope.
global.supported_performance_entry_types(cx, retval)
global.supported_performance_entry_types(cx, retval, can_gc)
}
// https://w3c.github.io/performance-timeline/#dom-performanceobserver-observe()

View file

@ -90,32 +90,37 @@ impl ResizeObserverEntryMethods<crate::DomTypeHolder> for ResizeObserverEntry {
}
/// <https://drafts.csswg.org/resize-observer/#dom-resizeobserverentry-borderboxsize>
fn BorderBoxSize(&self, cx: SafeJSContext, retval: MutableHandleValue) {
fn BorderBoxSize(&self, cx: SafeJSContext, can_gc: CanGc, retval: MutableHandleValue) {
let sizes: Vec<DomRoot<ResizeObserverSize>> = self
.border_box_size
.iter()
.map(|size| DomRoot::from_ref(&**size))
.collect();
to_frozen_array(sizes.as_slice(), cx, retval)
to_frozen_array(sizes.as_slice(), cx, retval, can_gc);
}
/// <https://drafts.csswg.org/resize-observer/#dom-resizeobserverentry-contentboxsize>
fn ContentBoxSize(&self, cx: SafeJSContext, retval: MutableHandleValue) {
fn ContentBoxSize(&self, cx: SafeJSContext, can_gc: CanGc, retval: MutableHandleValue) {
let sizes: Vec<DomRoot<ResizeObserverSize>> = self
.content_box_size
.iter()
.map(|size| DomRoot::from_ref(&**size))
.collect();
to_frozen_array(sizes.as_slice(), cx, retval);
to_frozen_array(sizes.as_slice(), cx, retval, can_gc);
}
/// <https://drafts.csswg.org/resize-observer/#dom-resizeobserverentry-devicepixelcontentboxsize>
fn DevicePixelContentBoxSize(&self, cx: SafeJSContext, retval: MutableHandleValue) {
fn DevicePixelContentBoxSize(
&self,
cx: SafeJSContext,
can_gc: CanGc,
retval: MutableHandleValue,
) {
let sizes: Vec<DomRoot<ResizeObserverSize>> = self
.device_pixel_content_box_size
.iter()
.map(|size| DomRoot::from_ref(&**size))
.collect();
to_frozen_array(sizes.as_slice(), cx, retval);
to_frozen_array(sizes.as_slice(), cx, retval, can_gc);
}
}

View file

@ -57,7 +57,7 @@ impl GPUCompilationInfo {
impl GPUCompilationInfoMethods<crate::DomTypeHolder> for GPUCompilationInfo {
/// <https://gpuweb.github.io/gpuweb/#dom-gpucompilationinfo-messages>
fn Messages(&self, cx: JSContext, retval: MutableHandleValue) {
to_frozen_array(self.msg.as_slice(), cx, retval)
fn Messages(&self, cx: JSContext, can_gc: CanGc, retval: MutableHandleValue) {
to_frozen_array(self.msg.as_slice(), cx, retval, can_gc)
}
}

View file

@ -85,9 +85,9 @@ impl XRBoundedReferenceSpaceMethods<crate::DomTypeHolder> for XRBoundedReference
})
.collect();
to_frozen_array(&points, cx, retval)
to_frozen_array(&points, cx, retval, can_gc)
} else {
to_frozen_array::<DomRoot<DOMPointReadOnly>>(&[], cx, retval)
to_frozen_array::<DomRoot<DOMPointReadOnly>>(&[], cx, retval, can_gc)
}
}
}

View file

@ -88,9 +88,9 @@ impl XRInputSourcesChangeEvent {
let _ac = enter_realm(global);
let cx = GlobalScope::get_cx();
rooted!(in(*cx) let mut frozen_val: JSVal);
to_frozen_array(added, cx, frozen_val.handle_mut());
to_frozen_array(added, cx, frozen_val.handle_mut(), can_gc);
changeevent.added.set(*frozen_val);
to_frozen_array(removed, cx, frozen_val.handle_mut());
to_frozen_array(removed, cx, frozen_val.handle_mut(), can_gc);
changeevent.removed.set(*frozen_val);
changeevent
}

View file

@ -149,10 +149,10 @@ impl XRRenderStateMethods<crate::DomTypeHolder> for XRRenderState {
}
/// <https://immersive-web.github.io/layers/#dom-xrrenderstate-layers>
fn Layers(&self, cx: JSContext, retval: MutableHandleValue) {
fn Layers(&self, cx: JSContext, can_gc: CanGc, retval: MutableHandleValue) {
// TODO: cache this array?
let layers = self.layers.borrow();
let layers: Vec<&XRLayer> = layers.iter().map(|x| &**x).collect();
to_frozen_array(&layers[..], cx, retval)
to_frozen_array(&layers[..], cx, retval, can_gc)
}
}

View file

@ -1006,10 +1006,10 @@ impl XRSessionMethods<crate::DomTypeHolder> for XRSession {
}
/// <https://www.w3.org/TR/webxr/#dom-xrsession-enabledfeatures>
fn EnabledFeatures(&self, cx: JSContext, retval: MutableHandleValue) {
fn EnabledFeatures(&self, cx: JSContext, can_gc: CanGc, retval: MutableHandleValue) {
let session = self.session.borrow();
let features = session.granted_features();
to_frozen_array(features, cx, retval)
to_frozen_array(features, cx, retval, can_gc)
}
/// <https://www.w3.org/TR/webxr/#dom-xrsession-issystemkeyboardsupported>

View file

@ -100,8 +100,8 @@ impl WorkerNavigatorMethods<crate::DomTypeHolder> for WorkerNavigator {
// https://html.spec.whatwg.org/multipage/#dom-navigator-languages
#[allow(unsafe_code)]
fn Languages(&self, cx: JSContext, retval: MutableHandleValue) {
to_frozen_array(&[self.Language()], cx, retval)
fn Languages(&self, cx: JSContext, can_gc: CanGc, retval: MutableHandleValue) {
to_frozen_array(&[self.Language()], cx, retval, can_gc)
}
// https://w3c.github.io/permissions/#navigator-and-workernavigator-extension

View file

@ -99,6 +99,10 @@ DOMInterfaces = {
'canGc': ['AppendRule', 'CssRules', 'DeleteRule', 'FindRule'],
},
'CSSLayerStatementRule': {
'canGc': ['NameList'],
},
'CSSMediaRule': {
'canGc': ['Media'],
},
@ -121,7 +125,7 @@ DOMInterfaces = {
},
'DataTransfer': {
'canGc': ['Files']
'canGc': ['Files', 'Types']
},
'DataTransferItem': {
@ -201,6 +205,10 @@ DOMInterfaces = {
'canGc': ['DispatchEvent'],
},
'ExtendableMessageEvent': {
'canGc': ['Ports'],
},
'FakeXRDevice': {
'canGc': ['Disconnect'],
},
@ -224,7 +232,7 @@ DOMInterfaces = {
'GamepadHapticActuator': {
'inRealms': ['PlayEffect', 'Reset'],
'canGc': ['PlayEffect', 'Reset'],
'canGc': ['PlayEffect', 'Reset', 'Effects'],
},
'GlobalScope': {
@ -250,6 +258,10 @@ DOMInterfaces = {
'weakReferenceable': True,
},
'GPUCompilationInfo': {
'canGc': ['Messages'],
},
'GPUDevice': {
'inRealms': [
'CreateComputePipelineAsync',
@ -387,6 +399,10 @@ DOMInterfaces = {
'canGc': ['SetText']
},
'IntersectionObserver': {
'canGc': ['Thresholds']
},
'Location': {
'canGc': ['Assign', 'Reload', 'Replace', 'SetHash', 'SetHost', 'SetHostname', 'SetHref', 'SetPathname', 'SetPort', 'SetProtocol', 'SetSearch'],
},
@ -413,6 +429,10 @@ DOMInterfaces = {
'canGc': ['GetOnmessage'],
},
'MessageEvent': {
'canGc': ['Ports'],
},
'MouseEvent': {
'canGc': ['OffsetX', 'OffsetY'],
},
@ -424,6 +444,7 @@ DOMInterfaces = {
'Navigator': {
'inRealms': ['GetVRDisplays'],
'canGc': ['Languages'],
},
'Node': {
@ -435,7 +456,7 @@ DOMInterfaces = {
},
'Notification': {
'canGc': ['RequestPermission'],
'canGc': ['RequestPermission', 'Actions', 'Vibrate'],
},
'OfflineAudioContext': {
@ -459,6 +480,10 @@ DOMInterfaces = {
'canGc': ['Mark', 'Measure'],
},
'PerformanceObserver': {
'canGc': ['SupportedEntryTypes'],
},
'Permissions': {
'canGc': ['Query', 'Request', 'Revoke'],
},
@ -609,13 +634,17 @@ DOMInterfaces = {
'canGc': ['GetOffsetReferenceSpace'],
},
'XRRenderState': {
'canGc': ['Layers'],
},
'XRRigidTransform': {
'canGc': ['Position', 'Orientation', 'Inverse', 'Matrix'],
},
'XRSession': {
'inRealms': ['RequestReferenceSpace', 'UpdateRenderState', 'UpdateTargetFrameRate'],
'canGc': ['End', 'RequestReferenceSpace', 'UpdateTargetFrameRate', 'RequestHitTestSource', 'GetSupportedFrameRates'],
'canGc': ['End', 'RequestReferenceSpace', 'UpdateTargetFrameRate', 'RequestHitTestSource', 'GetSupportedFrameRates', 'EnabledFeatures'],
},
'XRSystem': {
@ -656,6 +685,10 @@ DOMInterfaces = {
"canGc": ["Cancel", "Read", "ReleaseLock"]
},
'ResizeObserverEntry': {
'canGc': ['BorderBoxSize', 'ContentBoxSize', 'DevicePixelContentBoxSize'],
},
'WritableStream': {
'canGc': ['Abort', 'Close', 'GetWriter'],
'inRealms': ['Abort', 'Close', 'GetWriter'],
@ -671,6 +704,10 @@ DOMInterfaces = {
'inRealms': ['Abort', 'Close', 'Write'],
},
'WorkerNavigator': {
'canGc': ['Languages'],
},
}
Dictionaries = {