From 5db00dd1267df7000ea3c3607c825c44860c3eae Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Thu, 25 Sep 2014 12:19:56 +0200 Subject: [PATCH 1/3] Move global_object_for_js_object to global.rs. This appears to be a more sensible place for it (related to #433). --- components/script/dom/bindings/callback.rs | 3 +- .../dom/bindings/codegen/CodegenRust.py | 2 +- components/script/dom/bindings/global.rs | 31 +++++++++++++++++-- components/script/dom/bindings/utils.rs | 31 +++---------------- 4 files changed, 37 insertions(+), 30 deletions(-) diff --git a/components/script/dom/bindings/callback.rs b/components/script/dom/bindings/callback.rs index 4f9c04f6e420b..d450dd25688d3 100644 --- a/components/script/dom/bindings/callback.rs +++ b/components/script/dom/bindings/callback.rs @@ -4,9 +4,10 @@ //! Base classes to work with IDL callbacks. +use dom::bindings::global::global_object_for_js_object; use dom::bindings::js::JSRef; use dom::bindings::trace::Traceable; -use dom::bindings::utils::{Reflectable, global_object_for_js_object}; +use dom::bindings::utils::Reflectable; use js::jsapi::{JSContext, JSObject, JS_WrapObject, JS_ObjectIsCallable}; use js::jsapi::JS_GetProperty; use js::jsval::{JSVal, UndefinedValue}; diff --git a/components/script/dom/bindings/codegen/CodegenRust.py b/components/script/dom/bindings/codegen/CodegenRust.py index 14acae9bfa28d..5d1187fc06b98 100644 --- a/components/script/dom/bindings/codegen/CodegenRust.py +++ b/components/script/dom/bindings/codegen/CodegenRust.py @@ -4519,6 +4519,7 @@ def __init__(self, config, prefix, webIDLFile): 'dom::types::*', 'dom::bindings', 'dom::bindings::global::GlobalRef', + 'dom::bindings::global::global_object_for_js_object', 'dom::bindings::js::{JS, JSRef, Root, RootedReference, Temporary}', 'dom::bindings::js::{OptionalRootable, OptionalRootedRootable, ResultRootable}', 'dom::bindings::js::{OptionalRootedReference, OptionalOptionalRootedRootable}', @@ -4530,7 +4531,6 @@ def __init__(self, config, prefix, webIDLFile): 'dom::bindings::utils::{GetPropertyOnPrototype, GetProtoOrIfaceArray}', 'dom::bindings::utils::{HasPropertyOnPrototype, IntVal}', 'dom::bindings::utils::{jsid_to_str}', - 'dom::bindings::utils::global_object_for_js_object', 'dom::bindings::utils::{Reflectable}', 'dom::bindings::utils::{squirrel_away_unique}', 'dom::bindings::utils::{ThrowingConstructor, unwrap, unwrap_jsmanaged}', diff --git a/components/script/dom/bindings/global.rs b/components/script/dom/bindings/global.rs index ac40de4370cdf..2a6252d4f1e59 100644 --- a/components/script/dom/bindings/global.rs +++ b/components/script/dom/bindings/global.rs @@ -7,6 +7,7 @@ //! This module contains smart pointers to global scopes, to simplify writing //! code that works in workers as well as window scopes. +use dom::bindings::conversions::FromJSValConvertible; use dom::bindings::js::{JS, JSRef, Root}; use dom::bindings::utils::{Reflectable, Reflector}; use dom::workerglobalscope::WorkerGlobalScope; @@ -15,10 +16,15 @@ use script_task::ScriptChan; use servo_net::resource_task::ResourceTask; -use js::jsapi::JSContext; - +use js::{JSCLASS_IS_GLOBAL, JSCLASS_IS_DOMJSCLASS}; +use js::glue::{GetGlobalForObjectCrossCompartment}; +use js::jsapi::{JSContext, JSObject}; +use js::jsapi::{JS_GetClass}; +use js::jsval::ObjectOrNullValue; use url::Url; +use std::ptr; + /// A freely-copyable reference to a rooted global object. pub enum GlobalRef<'a> { Window(JSRef<'a, Window>), @@ -120,3 +126,24 @@ impl GlobalField { } } } + +/// Returns the global object of the realm that the given JS object was created in. +#[allow(unrooted_must_root)] +pub fn global_object_for_js_object(obj: *mut JSObject) -> GlobalField { + unsafe { + let global = GetGlobalForObjectCrossCompartment(obj); + let clasp = JS_GetClass(global); + assert!(((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)) != 0); + match FromJSValConvertible::from_jsval(ptr::null_mut(), ObjectOrNullValue(global), ()) { + Ok(window) => return WindowField(window), + Err(_) => (), + } + + match FromJSValConvertible::from_jsval(ptr::null_mut(), ObjectOrNullValue(global), ()) { + Ok(worker) => return WorkerField(worker), + Err(_) => (), + } + + fail!("found DOM global that doesn't unwrap to Window or WorkerGlobalScope") + } +} diff --git a/components/script/dom/bindings/utils.rs b/components/script/dom/bindings/utils.rs index 8453b0f95c279..19e5f9c7245d8 100644 --- a/components/script/dom/bindings/utils.rs +++ b/components/script/dom/bindings/utils.rs @@ -6,9 +6,9 @@ use dom::bindings::codegen::PrototypeList; use dom::bindings::codegen::PrototypeList::MAX_PROTO_CHAIN_LENGTH; -use dom::bindings::conversions::{FromJSValConvertible, IDLInterface}; +use dom::bindings::conversions::IDLInterface; use dom::bindings::error::throw_type_error; -use dom::bindings::global::{GlobalRef, GlobalField, WindowField, WorkerField}; +use dom::bindings::global::{GlobalRef, global_object_for_js_object}; use dom::bindings::js::{JS, Temporary, Root}; use dom::bindings::trace::Untraceable; use dom::browsercontext; @@ -23,7 +23,7 @@ use std::cmp::PartialEq; use std::ptr; use std::slice; use js::glue::{js_IsObjectProxyClass, js_IsFunctionProxyClass, IsProxyHandlerFamily}; -use js::glue::{GetGlobalForObjectCrossCompartment, UnwrapObject, GetProxyHandlerExtra}; +use js::glue::{UnwrapObject, GetProxyHandlerExtra}; use js::glue::{IsWrapper, RUST_JSID_TO_STRING, RUST_JSID_IS_INT}; use js::glue::{RUST_JSID_IS_STRING, RUST_JSID_TO_INT}; use js::jsapi::{JS_AlreadyHasOwnProperty, JS_NewFunction}; @@ -43,10 +43,10 @@ use js::jsapi::JS_DeletePropertyById2; use js::jsfriendapi::JS_ObjectToOuterObject; use js::jsfriendapi::bindgen::JS_NewObjectWithUniqueType; use js::jsval::JSVal; -use js::jsval::{PrivateValue, ObjectValue, NullValue, ObjectOrNullValue}; +use js::jsval::{PrivateValue, ObjectValue, NullValue}; use js::jsval::{Int32Value, UInt32Value, DoubleValue, BooleanValue, UndefinedValue}; use js::rust::with_compartment; -use js::{JSPROP_ENUMERATE, JSCLASS_IS_GLOBAL, JSCLASS_IS_DOMJSCLASS}; +use js::JSPROP_ENUMERATE; use js::JSPROP_PERMANENT; use js::{JSFUN_CONSTRUCTOR, JSPROP_READONLY}; use js; @@ -664,27 +664,6 @@ pub extern fn outerize_global(_cx: *mut JSContext, obj: JSHandleObject) -> *mut } } -/// Returns the global object of the realm that the given JS object was created in. -#[allow(unrooted_must_root)] -pub fn global_object_for_js_object(obj: *mut JSObject) -> GlobalField { - unsafe { - let global = GetGlobalForObjectCrossCompartment(obj); - let clasp = JS_GetClass(global); - assert!(((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)) != 0); - match FromJSValConvertible::from_jsval(ptr::null_mut(), ObjectOrNullValue(global), ()) { - Ok(window) => return WindowField(window), - Err(_) => (), - } - - match FromJSValConvertible::from_jsval(ptr::null_mut(), ObjectOrNullValue(global), ()) { - Ok(worker) => return WorkerField(worker), - Err(_) => (), - } - - fail!("found DOM global that doesn't unwrap to Window or WorkerGlobalScope") - } -} - /// Get the `JSContext` for the `JSRuntime` associated with the thread /// this object is on. #[allow(unrooted_must_root)] From 60124e76ac2a24a3df4937814d4df311d451ecb8 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Thu, 25 Sep 2014 12:49:08 +0200 Subject: [PATCH 2/3] Regroup uses of JS constants in utils.rs. --- components/script/dom/bindings/utils.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/components/script/dom/bindings/utils.rs b/components/script/dom/bindings/utils.rs index 19e5f9c7245d8..65025973f71c8 100644 --- a/components/script/dom/bindings/utils.rs +++ b/components/script/dom/bindings/utils.rs @@ -46,9 +46,8 @@ use js::jsval::JSVal; use js::jsval::{PrivateValue, ObjectValue, NullValue}; use js::jsval::{Int32Value, UInt32Value, DoubleValue, BooleanValue, UndefinedValue}; use js::rust::with_compartment; -use js::JSPROP_ENUMERATE; -use js::JSPROP_PERMANENT; -use js::{JSFUN_CONSTRUCTOR, JSPROP_READONLY}; +use js::{JSPROP_ENUMERATE, JSPROP_READONLY, JSPROP_PERMANENT}; +use js::JSFUN_CONSTRUCTOR; use js; #[allow(raw_pointer_deriving)] From 473eefac324e4c228daebde97621d3d88322633e Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Thu, 25 Sep 2014 12:30:25 +0200 Subject: [PATCH 3/3] Remove unused cx_for_dom_object function. --- .../script/dom/bindings/codegen/CodegenRust.py | 2 +- components/script/dom/bindings/utils.rs | 17 +---------------- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/components/script/dom/bindings/codegen/CodegenRust.py b/components/script/dom/bindings/codegen/CodegenRust.py index 5d1187fc06b98..5b0ee22fe7bfd 100644 --- a/components/script/dom/bindings/codegen/CodegenRust.py +++ b/components/script/dom/bindings/codegen/CodegenRust.py @@ -4524,7 +4524,7 @@ def __init__(self, config, prefix, webIDLFile): 'dom::bindings::js::{OptionalRootable, OptionalRootedRootable, ResultRootable}', 'dom::bindings::js::{OptionalRootedReference, OptionalOptionalRootedRootable}', 'dom::bindings::utils::{CreateDOMGlobal, CreateInterfaceObjects2}', - 'dom::bindings::utils::{ConstantSpec, cx_for_dom_object}', + 'dom::bindings::utils::ConstantSpec', 'dom::bindings::utils::{dom_object_slot, DOM_OBJECT_SLOT, DOMClass}', 'dom::bindings::utils::{DOMJSClass, JSCLASS_DOM_GLOBAL}', 'dom::bindings::utils::{FindEnumStringIndex, GetArrayIndexFromId}', diff --git a/components/script/dom/bindings/utils.rs b/components/script/dom/bindings/utils.rs index 65025973f71c8..1a48cfc08d2e6 100644 --- a/components/script/dom/bindings/utils.rs +++ b/components/script/dom/bindings/utils.rs @@ -8,7 +8,7 @@ use dom::bindings::codegen::PrototypeList; use dom::bindings::codegen::PrototypeList::MAX_PROTO_CHAIN_LENGTH; use dom::bindings::conversions::IDLInterface; use dom::bindings::error::throw_type_error; -use dom::bindings::global::{GlobalRef, global_object_for_js_object}; +use dom::bindings::global::GlobalRef; use dom::bindings::js::{JS, Temporary, Root}; use dom::bindings::trace::Untraceable; use dom::browsercontext; @@ -663,21 +663,6 @@ pub extern fn outerize_global(_cx: *mut JSContext, obj: JSHandleObject) -> *mut } } -/// Get the `JSContext` for the `JSRuntime` associated with the thread -/// this object is on. -#[allow(unrooted_must_root)] -fn cx_for_dom_reflector(obj: *mut JSObject) -> *mut JSContext { - let global = global_object_for_js_object(obj); - let global = global.root(); - global.root_ref().get_cx() -} - -/// Get the `JSContext` for the `JSRuntime` associated with the thread -/// this DOM object is on. -pub fn cx_for_dom_object(obj: &T) -> *mut JSContext { - cx_for_dom_reflector(obj.reflector().get_jsobject()) -} - pub unsafe fn delete_property_by_id(cx: *mut JSContext, object: *mut JSObject, id: jsid, bp: &mut bool) -> bool { let mut value = UndefinedValue();