Introduce Reflectable::global_scope

This commit is contained in:
Anthony Ramine 2016-10-01 18:15:15 +02:00
parent 27f100b1d4
commit ae6af5172b
30 changed files with 116 additions and 120 deletions

View file

@ -280,30 +280,44 @@ impl GlobalRoot {
}
}
/// Returns the global scope of the realm that the given DOM object's reflector was created in.
pub fn global_scope_from_reflector<T: Reflectable>(reflector: &T) -> Root<GlobalScope> {
unsafe { global_scope_from_object(*reflector.reflector().get_jsobject()) }
}
/// Returns the global object of the realm that the given DOM object's reflector was created in.
pub fn global_root_from_reflector<T: Reflectable>(reflector: &T) -> GlobalRoot {
unsafe { global_root_from_object(*reflector.reflector().get_jsobject()) }
}
/// Returns the Rust global object from a JS global object.
#[allow(unrooted_must_root)]
unsafe fn global_root_from_global(global: *mut JSObject) -> GlobalRoot {
/// Returns the Rust global scope from a JS global object.
unsafe fn global_scope_from_global(global: *mut JSObject) -> Root<GlobalScope> {
assert!(!global.is_null());
let clasp = JS_GetClass(global);
assert!(((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)) != 0);
match root_from_object(global) {
Ok(window) => return GlobalRoot::Window(window),
Err(_) => (),
}
root_from_object(global).unwrap()
}
match root_from_object(global) {
Ok(worker) => return GlobalRoot::Worker(worker),
Err(_) => (),
/// Returns the Rust global object from a JS global object.
#[allow(unrooted_must_root)]
unsafe fn global_root_from_global(global: *mut JSObject) -> GlobalRoot {
let global_scope = global_scope_from_global(global);
if let Some(window) = global_scope.downcast::<window::Window>() {
return GlobalRoot::Window(Root::from_ref(window));
}
if let Some(worker) = Root::downcast(global_scope) {
return GlobalRoot::Worker(worker);
}
panic!("found DOM global that doesn't unwrap to Window or WorkerGlobalScope")
}
/// Returns the global scope of the realm that the given JS object was created in.
pub unsafe fn global_scope_from_object(obj: *mut JSObject) -> Root<GlobalScope> {
assert!(!obj.is_null());
let global = GetGlobalForObjectCrossCompartment(obj);
global_scope_from_global(global)
}
/// Returns the global object of the realm that the given JS object was created in.
#[allow(unrooted_must_root)]
pub unsafe fn global_root_from_object(obj: *mut JSObject) -> GlobalRoot {

View file

@ -93,8 +93,7 @@ impl<T: Reflectable + JSTraceable + Iterable> IterableIterator<T> {
iterable: JS::from_ref(iterable),
index: Cell::new(0),
};
let global = iterable.global();
reflect_dom_object(iterator, global.r().as_global_scope(), wrap)
reflect_dom_object(iterator, &*iterable.global_scope(), wrap)
}
/// Return the next value from the iterable object.

View file

@ -5,7 +5,7 @@
//! The `Reflector` struct.
use dom::bindings::conversions::DerivedFrom;
use dom::bindings::global::{GlobalRoot, global_root_from_reflector};
use dom::bindings::global::{GlobalRoot, global_root_from_reflector, global_scope_from_reflector};
use dom::bindings::js::Root;
use dom::globalscope::GlobalScope;
use js::jsapi::{HandleObject, JSContext, JSObject};
@ -80,6 +80,11 @@ pub trait Reflectable {
/// Returns the receiver's reflector.
fn reflector(&self) -> &Reflector;
/// Returns the global scope of the realm that the Reflectable was created in.
fn global_scope(&self) -> Root<GlobalScope> where Self: Sized {
global_scope_from_reflector(self)
}
/// Returns the global object of the realm that the Reflectable was created in.
fn global(&self) -> GlobalRoot where Self: Sized {
global_root_from_reflector(self)