mirror of
https://github.com/servo/servo.git
synced 2025-07-23 07:13:52 +01:00
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:
parent
73e7d38a8d
commit
1df1ba58d6
7 changed files with 42 additions and 19 deletions
|
@ -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};
|
||||
}
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -33,6 +33,18 @@ pub(crate) enum InRealm<'a> {
|
|||
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<'_> {
|
||||
pub(crate) fn already(token: &AlreadyInRealm) -> InRealm {
|
||||
InRealm::Already(token)
|
||||
|
|
|
@ -3960,7 +3960,7 @@ class CGCallGenerator(CGThing):
|
|||
if static:
|
||||
glob = "global.upcast::<D::GlobalScope>()"
|
||||
else:
|
||||
glob = "&this.global()"
|
||||
glob = "&this.global_(InRealm::already(&AlreadyInRealm::assert_for_cx(cx)))"
|
||||
|
||||
self.cgRoot.append(CGGeneric(
|
||||
"let result = match result {\n"
|
||||
|
@ -8454,7 +8454,8 @@ class CGIterableMethodGenerator(CGGeneric):
|
|||
return
|
||||
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),
|
||||
ifaceName=descriptor.interface.identifier.name,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue