script: Expose new methods for obtaining a global that require a realm. (#36116)

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2025-03-26 19:27:25 -04:00 committed by GitHub
parent 73e7d38a8d
commit 1df1ba58d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 42 additions and 19 deletions

View file

@ -44,6 +44,7 @@ pub(crate) mod base {
}; };
pub(crate) use crate::dom::globalscope::{GlobalScope, GlobalScopeHelpers}; pub(crate) use crate::dom::globalscope::{GlobalScope, GlobalScopeHelpers};
pub(crate) use crate::dom::promise::PromiseHelpers; pub(crate) use crate::dom::promise::PromiseHelpers;
pub(crate) use crate::realms::{AlreadyInRealm, InRealm};
pub(crate) use crate::script_runtime::{CanGc, JSContext as SafeJSContext}; pub(crate) use crate::script_runtime::{CanGc, JSContext as SafeJSContext};
} }

View file

@ -30,6 +30,7 @@ use crate::dom::bindings::reflector::{
}; };
use crate::dom::bindings::root::{Dom, DomRoot, Root}; use crate::dom::bindings::root::{Dom, DomRoot, Root};
use crate::dom::bindings::trace::{JSTraceable, NoTrace, RootedTraceableBox}; use crate::dom::bindings::trace::{JSTraceable, NoTrace, RootedTraceableBox};
use crate::realms::InRealm;
use crate::script_runtime::{CanGc, JSContext}; use crate::script_runtime::{CanGc, JSContext};
/// An iterator over the iterable entries of a given DOM interface. /// An iterator over the iterable entries of a given DOM interface.
@ -46,8 +47,8 @@ pub(crate) struct IterableIterator<
} }
impl<D: DomTypes, T: DomObjectIteratorWrap<D> + JSTraceable + Iterable> IterableIterator<D, T> { impl<D: DomTypes, T: DomObjectIteratorWrap<D> + JSTraceable + Iterable> IterableIterator<D, T> {
pub fn global(&self) -> DomRoot<D::GlobalScope> { pub fn global_(&self, realm: InRealm) -> DomRoot<D::GlobalScope> {
<Self as DomGlobalGeneric<D>>::global(self) <Self as DomGlobalGeneric<D>>::global_(self, realm)
} }
} }
@ -70,7 +71,7 @@ impl<D: DomTypes, T: DomObjectIteratorWrap<D> + JSTraceable + Iterable + DomGlob
IterableIterator<D, T> IterableIterator<D, T>
{ {
/// Create a new iterator instance for the provided iterable DOM interface. /// Create a new iterator instance for the provided iterable DOM interface.
pub(crate) fn new(iterable: &T, type_: IteratorType) -> DomRoot<Self> { pub(crate) fn new(iterable: &T, type_: IteratorType, realm: InRealm) -> DomRoot<Self> {
let iterator = Box::new(IterableIterator { let iterator = Box::new(IterableIterator {
reflector: Reflector::new(), reflector: Reflector::new(),
type_, type_,
@ -78,7 +79,7 @@ impl<D: DomTypes, T: DomObjectIteratorWrap<D> + JSTraceable + Iterable + DomGlob
index: Cell::new(0), index: Cell::new(0),
_marker: NoTrace(PhantomData), _marker: NoTrace(PhantomData),
}); });
reflect_dom_object(iterator, &*iterable.global(), CanGc::note()) reflect_dom_object(iterator, &*iterable.global_(realm), CanGc::note())
} }
/// Return the next value from the iterable object. /// Return the next value from the iterable object.

View file

@ -12,7 +12,7 @@ use crate::dom::bindings::iterable::{Iterable, IterableIterator};
use crate::dom::bindings::root::{Dom, DomRoot, Root}; use crate::dom::bindings::root::{Dom, DomRoot, Root};
use crate::dom::bindings::trace::JSTraceable; use crate::dom::bindings::trace::JSTraceable;
use crate::dom::globalscope::{GlobalScope, GlobalScopeHelpers}; use crate::dom::globalscope::{GlobalScope, GlobalScopeHelpers};
use crate::realms::AlreadyInRealm; use crate::realms::{AlreadyInRealm, InRealm};
use crate::script_runtime::{CanGc, JSContext}; use crate::script_runtime::{CanGc, JSContext};
/// Create the reflector for a new DOM object and yield ownership to the /// Create the reflector for a new DOM object and yield ownership to the
@ -43,6 +43,16 @@ where
} }
pub(crate) trait DomGlobalGeneric<D: DomTypes>: DomObject { pub(crate) trait DomGlobalGeneric<D: DomTypes>: DomObject {
/// Returns the [`GlobalScope`] of the realm that the [`DomObject`] was created in. If this
/// object is a `Node`, this will be different from it's owning `Document` if adopted by. For
/// `Node`s it's almost always better to use `NodeTraits::owning_global`.
fn global_(&self, realm: InRealm) -> DomRoot<D::GlobalScope>
where
Self: Sized,
{
D::GlobalScope::from_reflector(self, realm)
}
/// Returns the [`GlobalScope`] of the realm that the [`DomObject`] was created in. If this /// Returns the [`GlobalScope`] of the realm that the [`DomObject`] was created in. If this
/// object is a `Node`, this will be different from it's owning `Document` if adopted by. For /// object is a `Node`, this will be different from it's owning `Document` if adopted by. For
/// `Node`s it's almost always better to use `NodeTraits::owning_global`. /// `Node`s it's almost always better to use `NodeTraits::owning_global`.
@ -51,17 +61,21 @@ pub(crate) trait DomGlobalGeneric<D: DomTypes>: DomObject {
Self: Sized, Self: Sized,
{ {
let realm = AlreadyInRealm::assert_for_cx(D::GlobalScope::get_cx()); let realm = AlreadyInRealm::assert_for_cx(D::GlobalScope::get_cx());
D::GlobalScope::from_reflector(self, &realm) D::GlobalScope::from_reflector(self, InRealm::already(&realm))
} }
} }
impl<D: DomTypes, T: DomObject> DomGlobalGeneric<D> for T {} impl<D: DomTypes, T: DomObject> DomGlobalGeneric<D> for T {}
pub(crate) trait DomGlobal { pub(crate) trait DomGlobal {
fn global_(&self, realm: InRealm) -> DomRoot<GlobalScope>;
fn global(&self) -> DomRoot<GlobalScope>; fn global(&self) -> DomRoot<GlobalScope>;
} }
impl<T: DomGlobalGeneric<crate::DomTypeHolder>> DomGlobal for T { impl<T: DomGlobalGeneric<crate::DomTypeHolder>> DomGlobal for T {
fn global_(&self, realm: InRealm) -> DomRoot<GlobalScope> {
<Self as DomGlobalGeneric<crate::DomTypeHolder>>::global_(self, realm)
}
fn global(&self) -> DomRoot<GlobalScope> { fn global(&self) -> DomRoot<GlobalScope> {
<Self as DomGlobalGeneric<crate::DomTypeHolder>>::global(self) <Self as DomGlobalGeneric<crate::DomTypeHolder>>::global(self)
} }

View file

@ -2021,10 +2021,7 @@ impl GlobalScope {
/// Returns the global scope of the realm that the given DOM object's reflector /// Returns the global scope of the realm that the given DOM object's reflector
/// was created in. /// was created in.
#[allow(unsafe_code)] #[allow(unsafe_code)]
pub(crate) fn from_reflector<T: DomObject>( pub(crate) fn from_reflector<T: DomObject>(reflector: &T, _realm: InRealm) -> DomRoot<Self> {
reflector: &T,
_realm: &AlreadyInRealm,
) -> DomRoot<Self> {
unsafe { GlobalScope::from_object(*reflector.reflector().get_jsobject()) } unsafe { GlobalScope::from_object(*reflector.reflector().get_jsobject()) }
} }
@ -3337,10 +3334,7 @@ pub(crate) trait GlobalScopeHelpers<D: crate::DomTypes> {
unsafe fn from_context(cx: *mut JSContext, realm: InRealm) -> DomRoot<D::GlobalScope>; unsafe fn from_context(cx: *mut JSContext, realm: InRealm) -> DomRoot<D::GlobalScope>;
fn get_cx() -> SafeJSContext; fn get_cx() -> SafeJSContext;
unsafe fn from_object(obj: *mut JSObject) -> DomRoot<D::GlobalScope>; unsafe fn from_object(obj: *mut JSObject) -> DomRoot<D::GlobalScope>;
fn from_reflector( fn from_reflector(reflector: &impl DomObject, realm: InRealm) -> DomRoot<D::GlobalScope>;
reflector: &impl DomObject,
realm: &AlreadyInRealm,
) -> DomRoot<D::GlobalScope>;
unsafe fn from_object_maybe_wrapped( unsafe fn from_object_maybe_wrapped(
obj: *mut JSObject, obj: *mut JSObject,
@ -3370,7 +3364,7 @@ impl GlobalScopeHelpers<crate::DomTypeHolder> for GlobalScope {
GlobalScope::from_object(obj) GlobalScope::from_object(obj)
} }
fn from_reflector(reflector: &impl DomObject, realm: &AlreadyInRealm) -> DomRoot<Self> { fn from_reflector(reflector: &impl DomObject, realm: InRealm) -> DomRoot<Self> {
GlobalScope::from_reflector(reflector, realm) GlobalScope::from_reflector(reflector, realm)
} }

View file

@ -270,10 +270,10 @@ impl Promise {
pub(crate) fn append_native_handler( pub(crate) fn append_native_handler(
&self, &self,
handler: &PromiseNativeHandler, handler: &PromiseNativeHandler,
_comp: InRealm, realm: InRealm,
can_gc: CanGc, can_gc: CanGc,
) { ) {
let _ais = AutoEntryScript::new(&handler.global()); let _ais = AutoEntryScript::new(&handler.global_(realm));
let cx = GlobalScope::get_cx(); let cx = GlobalScope::get_cx();
rooted!(in(*cx) let resolve_func = rooted!(in(*cx) let resolve_func =
create_native_handler_function(*cx, create_native_handler_function(*cx,

View file

@ -33,6 +33,18 @@ pub(crate) enum InRealm<'a> {
Entered(&'a JSAutoRealm), Entered(&'a JSAutoRealm),
} }
impl<'a> From<&'a AlreadyInRealm> for InRealm<'a> {
fn from(token: &'a AlreadyInRealm) -> InRealm<'a> {
InRealm::already(token)
}
}
impl<'a> From<&'a JSAutoRealm> for InRealm<'a> {
fn from(token: &'a JSAutoRealm) -> InRealm<'a> {
InRealm::entered(token)
}
}
impl InRealm<'_> { impl InRealm<'_> {
pub(crate) fn already(token: &AlreadyInRealm) -> InRealm { pub(crate) fn already(token: &AlreadyInRealm) -> InRealm {
InRealm::Already(token) InRealm::Already(token)

View file

@ -3960,7 +3960,7 @@ class CGCallGenerator(CGThing):
if static: if static:
glob = "global.upcast::<D::GlobalScope>()" glob = "global.upcast::<D::GlobalScope>()"
else: else:
glob = "&this.global()" glob = "&this.global_(InRealm::already(&AlreadyInRealm::assert_for_cx(cx)))"
self.cgRoot.append(CGGeneric( self.cgRoot.append(CGGeneric(
"let result = match result {\n" "let result = match result {\n"
@ -8454,7 +8454,8 @@ class CGIterableMethodGenerator(CGGeneric):
return return
CGGeneric.__init__(self, fill( CGGeneric.__init__(self, fill(
""" """
let result = ${iterClass}::new(this, IteratorType::${itrMethod}); let realm = AlreadyInRealm::assert_for_cx(cx);
let result = ${iterClass}::new(this, IteratorType::${itrMethod}, InRealm::already(&realm));
""", """,
iterClass=iteratorNativeType(descriptor, True), iterClass=iteratorNativeType(descriptor, True),
ifaceName=descriptor.interface.identifier.name, ifaceName=descriptor.interface.identifier.name,