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::promise::PromiseHelpers;
pub(crate) use crate::realms::{AlreadyInRealm, InRealm};
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::trace::{JSTraceable, NoTrace, RootedTraceableBox};
use crate::realms::InRealm;
use crate::script_runtime::{CanGc, JSContext};
/// 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> {
pub fn global(&self) -> DomRoot<D::GlobalScope> {
<Self as DomGlobalGeneric<D>>::global(self)
pub fn global_(&self, realm: InRealm) -> DomRoot<D::GlobalScope> {
<Self as DomGlobalGeneric<D>>::global_(self, realm)
}
}
@ -70,7 +71,7 @@ impl<D: DomTypes, T: DomObjectIteratorWrap<D> + JSTraceable + Iterable + DomGlob
IterableIterator<D, T>
{
/// 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 {
reflector: Reflector::new(),
type_,
@ -78,7 +79,7 @@ impl<D: DomTypes, T: DomObjectIteratorWrap<D> + JSTraceable + Iterable + DomGlob
index: Cell::new(0),
_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.

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::trace::JSTraceable;
use crate::dom::globalscope::{GlobalScope, GlobalScopeHelpers};
use crate::realms::AlreadyInRealm;
use crate::realms::{AlreadyInRealm, InRealm};
use crate::script_runtime::{CanGc, JSContext};
/// 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 {
/// 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
/// 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`.
@ -51,17 +61,21 @@ pub(crate) trait DomGlobalGeneric<D: DomTypes>: DomObject {
Self: Sized,
{
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 {}
pub(crate) trait DomGlobal {
fn global_(&self, realm: InRealm) -> DomRoot<GlobalScope>;
fn global(&self) -> DomRoot<GlobalScope>;
}
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> {
<Self as DomGlobalGeneric<crate::DomTypeHolder>>::global(self)
}