script: Return global objects for DOM objects in the relevant realm (#37120)

DomObject::global is a tricky API because it's used pervasively but has
subtle requirements that are not documented and not yet enforced by the
type system (#36116). The method returns the relevant global object for
a given DOM object, but that operation is only meaningful if there is an
active realm. We usually, but not always, have an active realm.

This change avoids a footgun by following the principle of least
surprise. Rather than making every single caller of `something.global()`
both prove that there is an active realm and think about which realm
they want active, we implement the obvious behaviour: always activate
the realm of the callee before obtaining the relevant global.
 
Testing: Existing WPT coverage is sufficient; this method is called all
over the codebase.
Fixes: #37070 #27037

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2025-05-24 23:21:05 -04:00 committed by GitHub
parent 0d44ca8ddc
commit 3a04f4195e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 14 deletions

View file

@ -8,7 +8,7 @@ use malloc_size_of_derive::MallocSizeOf;
use crate::interfaces::GlobalScopeHelpers;
use crate::iterable::{Iterable, IterableIterator};
use crate::realms::{AlreadyInRealm, InRealm};
use crate::realms::InRealm;
use crate::root::{Dom, DomRoot, Root};
use crate::script_runtime::{CanGc, JSContext};
use crate::{DomTypes, JSTraceable};
@ -108,17 +108,6 @@ pub trait DomGlobalGeneric<D: DomTypes>: DomObject {
{
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`.
fn global(&self) -> DomRoot<D::GlobalScope>
where
Self: Sized,
{
let realm = AlreadyInRealm::assert_for_cx(D::GlobalScope::get_cx());
D::GlobalScope::from_reflector(self, InRealm::already(&realm))
}
}
impl<D: DomTypes, T: DomObject> DomGlobalGeneric<D> for T {}