Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement [Func] #11308

Merged
merged 12 commits into from May 27, 2016

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

  • Loading branch information
nox committed May 26, 2016
commit 34858fd0c94d470f69f4ed206f7b79dca2dbfb2a
@@ -164,7 +164,7 @@ impl CallSetup {
/// Performs the setup needed to make a call.
#[allow(unrooted_must_root)]
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 exception_compartment = unsafe {
@@ -302,44 +302,39 @@ impl GlobalRoot {

/// 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 {
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.
#[allow(unrooted_must_root)]
pub fn global_root_from_global(global: *mut JSObject) -> GlobalRoot {
unsafe {
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(_) => (),
}

match root_from_object(global) {
Ok(worker) => return GlobalRoot::Worker(worker),
Err(_) => (),
}
unsafe fn global_root_from_global(global: *mut JSObject) -> GlobalRoot {
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(_) => (),
}

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.
#[allow(unrooted_must_root)]
pub fn global_root_from_object(obj: *mut JSObject) -> GlobalRoot {
unsafe {
let global = GetGlobalForObjectCrossCompartment(obj);
global_root_from_global(global)
}
pub unsafe fn global_root_from_object(obj: *mut JSObject) -> GlobalRoot {
assert!(!obj.is_null());
let global = GetGlobalForObjectCrossCompartment(obj);
global_root_from_global(global)
}

/// Returns the global object for the given JSContext
#[allow(unrooted_must_root)]
pub fn global_root_from_context(cx: *mut JSContext) -> GlobalRoot {
unsafe {
let global = CurrentGlobalOrNull(cx);
assert!(!global.is_null());
global_root_from_global(global)
}
pub unsafe fn global_root_from_context(cx: *mut JSContext) -> GlobalRoot {
let global = CurrentGlobalOrNull(cx);
global_root_from_global(global)
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.