mirror of
https://github.com/servo/servo.git
synced 2025-07-22 23:03:42 +01:00
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:
parent
0d44ca8ddc
commit
3a04f4195e
2 changed files with 13 additions and 14 deletions
|
@ -11,7 +11,7 @@ use crate::DomTypes;
|
|||
use crate::dom::bindings::conversions::DerivedFrom;
|
||||
use crate::dom::bindings::root::DomRoot;
|
||||
use crate::dom::globalscope::GlobalScope;
|
||||
use crate::realms::InRealm;
|
||||
use crate::realms::{InRealm, enter_realm};
|
||||
use crate::script_runtime::CanGc;
|
||||
|
||||
/// Create the reflector for a new DOM object and yield ownership to the
|
||||
|
@ -42,7 +42,16 @@ where
|
|||
}
|
||||
|
||||
pub(crate) trait DomGlobal {
|
||||
/// Returns the [relevant global] in whatever realm is currently active.
|
||||
///
|
||||
/// [relevant global]: https://html.spec.whatwg.org/multipage/#concept-relevant-global
|
||||
fn global_(&self, realm: InRealm) -> DomRoot<GlobalScope>;
|
||||
|
||||
/// Returns the [relevant global] in the same realm as the callee object.
|
||||
/// If you know the callee's realm is already the current realm, it is
|
||||
/// more efficient to call [DomGlobal::global_] instead.
|
||||
///
|
||||
/// [relevant global]: https://html.spec.whatwg.org/multipage/#concept-relevant-global
|
||||
fn global(&self) -> DomRoot<GlobalScope>;
|
||||
}
|
||||
|
||||
|
@ -51,7 +60,8 @@ impl<T: DomGlobalGeneric<crate::DomTypeHolder>> DomGlobal for T {
|
|||
<Self as DomGlobalGeneric<crate::DomTypeHolder>>::global_(self, realm)
|
||||
}
|
||||
fn global(&self) -> DomRoot<GlobalScope> {
|
||||
<Self as DomGlobalGeneric<crate::DomTypeHolder>>::global(self)
|
||||
let realm = enter_realm(self);
|
||||
<Self as DomGlobalGeneric<crate::DomTypeHolder>>::global_(self, InRealm::entered(&realm))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue