diff options
author | Josh Matthews <josh@joshmatthews.net> | 2025-03-26 19:27:25 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-26 23:27:25 +0000 |
commit | 1df1ba58d6a2fe5807a2770d4c118af79c2d5fab (patch) | |
tree | 2ca0151fb981237e9060e998b195804634aff900 /components/script/dom | |
parent | 73e7d38a8dc3aa22af4c2a6f2ff2c4f5952b814c (diff) | |
download | servo-1df1ba58d6a2fe5807a2770d4c118af79c2d5fab.tar.gz servo-1df1ba58d6a2fe5807a2770d4c118af79c2d5fab.zip |
script: Expose new methods for obtaining a global that require a realm. (#36116)
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
Diffstat (limited to 'components/script/dom')
-rw-r--r-- | components/script/dom/bindings/import.rs | 1 | ||||
-rw-r--r-- | components/script/dom/bindings/iterable.rs | 9 | ||||
-rw-r--r-- | components/script/dom/bindings/reflector.rs | 18 | ||||
-rw-r--r-- | components/script/dom/globalscope.rs | 12 | ||||
-rw-r--r-- | components/script/dom/promise.rs | 4 |
5 files changed, 27 insertions, 17 deletions
diff --git a/components/script/dom/bindings/import.rs b/components/script/dom/bindings/import.rs index 39e21f4b349..33511666d17 100644 --- a/components/script/dom/bindings/import.rs +++ b/components/script/dom/bindings/import.rs @@ -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}; } diff --git a/components/script/dom/bindings/iterable.rs b/components/script/dom/bindings/iterable.rs index 1a7be20b3da..d97dfeeddbe 100644 --- a/components/script/dom/bindings/iterable.rs +++ b/components/script/dom/bindings/iterable.rs @@ -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. diff --git a/components/script/dom/bindings/reflector.rs b/components/script/dom/bindings/reflector.rs index 779a7a5bf81..3593578a66f 100644 --- a/components/script/dom/bindings/reflector.rs +++ b/components/script/dom/bindings/reflector.rs @@ -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 @@ -46,22 +46,36 @@ 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`. fn global(&self) -> DomRoot<D::GlobalScope> where 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) } diff --git a/components/script/dom/globalscope.rs b/components/script/dom/globalscope.rs index 88d56456686..c4625895f3a 100644 --- a/components/script/dom/globalscope.rs +++ b/components/script/dom/globalscope.rs @@ -2021,10 +2021,7 @@ impl GlobalScope { /// Returns the global scope of the realm that the given DOM object's reflector /// was created in. #[allow(unsafe_code)] - pub(crate) fn from_reflector<T: DomObject>( - reflector: &T, - _realm: &AlreadyInRealm, - ) -> DomRoot<Self> { + pub(crate) fn from_reflector<T: DomObject>(reflector: &T, _realm: InRealm) -> DomRoot<Self> { 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>; fn get_cx() -> SafeJSContext; unsafe fn from_object(obj: *mut JSObject) -> DomRoot<D::GlobalScope>; - fn from_reflector( - reflector: &impl DomObject, - realm: &AlreadyInRealm, - ) -> DomRoot<D::GlobalScope>; + fn from_reflector(reflector: &impl DomObject, realm: InRealm) -> DomRoot<D::GlobalScope>; unsafe fn from_object_maybe_wrapped( obj: *mut JSObject, @@ -3370,7 +3364,7 @@ impl GlobalScopeHelpers<crate::DomTypeHolder> for GlobalScope { 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) } diff --git a/components/script/dom/promise.rs b/components/script/dom/promise.rs index b8d30bd3651..b970e7f3342 100644 --- a/components/script/dom/promise.rs +++ b/components/script/dom/promise.rs @@ -270,10 +270,10 @@ impl Promise { pub(crate) fn append_native_handler( &self, handler: &PromiseNativeHandler, - _comp: InRealm, + realm: InRealm, can_gc: CanGc, ) { - let _ais = AutoEntryScript::new(&handler.global()); + let _ais = AutoEntryScript::new(&handler.global_(realm)); let cx = GlobalScope::get_cx(); rooted!(in(*cx) let resolve_func = create_native_handler_function(*cx, |