Clean up the functions to retrieve a global root from JS objects

This commit is contained in:
Anthony Ramine 2016-05-21 16:30:12 +02:00
parent e179cb02ff
commit 34858fd0c9
2 changed files with 23 additions and 28 deletions

View file

@ -164,7 +164,7 @@ impl CallSetup {
/// Performs the setup needed to make a call. /// Performs the setup needed to make a call.
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
pub fn new<T: CallbackContainer>(callback: &T, handling: ExceptionHandling) -> CallSetup { pub fn new<T: CallbackContainer>(callback: &T, handling: ExceptionHandling) -> CallSetup {
let global = global_root_from_object(callback.callback()); let global = unsafe { global_root_from_object(callback.callback()) };
let cx = global.r().get_cx(); let cx = global.r().get_cx();
let exception_compartment = unsafe { let exception_compartment = unsafe {

View file

@ -302,44 +302,39 @@ impl GlobalRoot {
/// Returns the global object of the realm that the given DOM object's reflector was created in. /// 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 { pub fn global_root_from_reflector<T: Reflectable>(reflector: &T) -> GlobalRoot {
global_root_from_object(*reflector.reflector().get_jsobject()) unsafe { global_root_from_object(*reflector.reflector().get_jsobject()) }
} }
/// Returns the Rust global object from a JS global object. /// Returns the Rust global object from a JS global object.
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
pub fn global_root_from_global(global: *mut JSObject) -> GlobalRoot { unsafe fn global_root_from_global(global: *mut JSObject) -> GlobalRoot {
unsafe { assert!(!global.is_null());
let clasp = JS_GetClass(global); let clasp = JS_GetClass(global);
assert!(((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)) != 0); assert!(((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)) != 0);
match root_from_object(global) { match root_from_object(global) {
Ok(window) => return GlobalRoot::Window(window), Ok(window) => return GlobalRoot::Window(window),
Err(_) => (), Err(_) => (),
}
match root_from_object(global) {
Ok(worker) => return GlobalRoot::Worker(worker),
Err(_) => (),
}
panic!("found DOM global that doesn't unwrap to Window or WorkerGlobalScope")
} }
match root_from_object(global) {
Ok(worker) => return GlobalRoot::Worker(worker),
Err(_) => (),
}
panic!("found DOM global that doesn't unwrap to Window or WorkerGlobalScope")
} }
/// Returns the global object of the realm that the given JS object was created in. /// Returns the global object of the realm that the given JS object was created in.
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
pub fn global_root_from_object(obj: *mut JSObject) -> GlobalRoot { pub unsafe fn global_root_from_object(obj: *mut JSObject) -> GlobalRoot {
unsafe { assert!(!obj.is_null());
let global = GetGlobalForObjectCrossCompartment(obj); let global = GetGlobalForObjectCrossCompartment(obj);
global_root_from_global(global) global_root_from_global(global)
}
} }
/// Returns the global object for the given JSContext /// Returns the global object for the given JSContext
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
pub fn global_root_from_context(cx: *mut JSContext) -> GlobalRoot { pub unsafe fn global_root_from_context(cx: *mut JSContext) -> GlobalRoot {
unsafe { let global = CurrentGlobalOrNull(cx);
let global = CurrentGlobalOrNull(cx); global_root_from_global(global)
assert!(!global.is_null());
global_root_from_global(global)
}
} }