Start marking functions that can transitively trigger a GC (#33144)

* Mark JS reflector wrappers as CanGc.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* Propagate CanGc from reflect_dom_object_with_proto.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* Mark DOM constructors as GC operations.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2024-08-22 07:42:36 -04:00 committed by GitHub
parent 9a1051c917
commit 60ef6bc461
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
140 changed files with 1336 additions and 304 deletions

View file

@ -2889,7 +2889,8 @@ class CGWrapMethod(CGAbstractMethod):
args = [Argument('SafeJSContext', 'cx'),
Argument('&GlobalScope', 'scope'),
Argument('Option<HandleObject>', 'given_proto'),
Argument(f"Box<{descriptor.concreteType}>", 'object')]
Argument(f"Box<{descriptor.concreteType}>", 'object'),
Argument('CanGc', '_can_gc')]
retval = f'DomRoot<{descriptor.concreteType}>'
CGAbstractMethod.__init__(self, descriptor, 'Wrap', retval, args,
pub=True, unsafe=True)
@ -3082,6 +3083,7 @@ impl DomObjectWrap for {name} {{
&GlobalScope,
Option<HandleObject>,
Box<Self>,
CanGc,
) -> Root<Dom<Self>> = Wrap;
}}
"""
@ -3105,6 +3107,7 @@ impl DomObjectIteratorWrap for {name} {{
&GlobalScope,
Option<HandleObject>,
Box<IterableIterator<Self>>,
CanGc,
) -> Root<Dom<IterableIterator<Self>>> = Wrap;
}}
"""
@ -6234,7 +6237,7 @@ if proto_result.is_err() {{
"""
name = self.constructor.identifier.name
nativeName = MakeNativeName(self.descriptor.binaryNameFor(name))
args = ["&global", "Some(desired_proto.handle())"]
args = ["&global", "Some(desired_proto.handle())", "CanGc::note()"]
constructorCall = CGMethodCall(args, nativeName, True,
self.descriptor, self.constructor)
return CGList([CGGeneric(preamble), constructorCall])

View file

@ -145,4 +145,5 @@ pub mod module {
pub use crate::dom::types::{AnalyserNode, AudioNode, BaseAudioContext, EventTarget};
pub use crate::mem::malloc_size_of_including_raw_self;
pub use crate::realms::{AlreadyInRealm, InRealm};
pub use crate::script_runtime::CanGc;
}

View file

@ -26,7 +26,7 @@ use crate::dom::bindings::reflector::{
use crate::dom::bindings::root::{Dom, DomRoot, Root};
use crate::dom::bindings::trace::{JSTraceable, RootedTraceableBox};
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::JSContext;
use crate::script_runtime::{CanGc, JSContext};
/// The values that an iterator will iterate over.
#[derive(JSTraceable, MallocSizeOf)]
@ -126,6 +126,7 @@ impl<T: DomObjectIteratorWrap + JSTraceable + Iterable> DomObjectWrap for Iterab
&GlobalScope,
Option<HandleObject>,
Box<Self>,
CanGc,
) -> Root<Dom<Self>> = T::ITER_WRAP;
}

View file

@ -15,7 +15,7 @@ use crate::dom::bindings::root::{Dom, DomRoot, Root};
use crate::dom::bindings::trace::JSTraceable;
use crate::dom::globalscope::GlobalScope;
use crate::realms::AlreadyInRealm;
use crate::script_runtime::JSContext;
use crate::script_runtime::{CanGc, JSContext};
/// Create the reflector for a new DOM object and yield ownership to the
/// reflector.
@ -25,20 +25,29 @@ where
U: DerivedFrom<GlobalScope>,
{
let global_scope = global.upcast();
unsafe { T::WRAP(GlobalScope::get_cx(), global_scope, None, obj) }
unsafe {
T::WRAP(
GlobalScope::get_cx(),
global_scope,
None,
obj,
CanGc::note(),
)
}
}
pub fn reflect_dom_object_with_proto<T, U>(
obj: Box<T>,
global: &U,
proto: Option<HandleObject>,
can_gc: CanGc,
) -> DomRoot<T>
where
T: DomObject + DomObjectWrap,
U: DerivedFrom<GlobalScope>,
{
let global_scope = global.upcast();
unsafe { T::WRAP(GlobalScope::get_cx(), global_scope, proto, obj) }
unsafe { T::WRAP(GlobalScope::get_cx(), global_scope, proto, obj, can_gc) }
}
/// A struct to store a reference to the reflector of a DOM object.
@ -131,6 +140,7 @@ pub trait DomObjectWrap: Sized + DomObject {
&GlobalScope,
Option<HandleObject>,
Box<Self>,
CanGc,
) -> Root<Dom<Self>>;
}
@ -143,5 +153,6 @@ pub trait DomObjectIteratorWrap: DomObjectWrap + JSTraceable + Iterable {
&GlobalScope,
Option<HandleObject>,
Box<IterableIterator<Self>>,
CanGc,
) -> Root<Dom<IterableIterator<Self>>>;
}