diff options
author | Josh Matthews <josh@joshmatthews.net> | 2013-11-01 10:19:45 -0400 |
---|---|---|
committer | Josh Matthews <josh@joshmatthews.net> | 2013-11-05 12:58:28 -0500 |
commit | 7ecf5abbbdf0d75a1e794cde6ac71fe982468414 (patch) | |
tree | cd228a9204ecc7b8d4ae52f66638a19e63f08c29 /src/components/script/dom/bindings/utils.rs | |
parent | 06b1db8818c09201989b017434eef105f4d99e51 (diff) | |
download | servo-7ecf5abbbdf0d75a1e794cde6ac71fe982468414.tar.gz servo-7ecf5abbbdf0d75a1e794cde6ac71fe982468414.zip |
Generate code for handling callbacks. Implement add/removeEventListener and hacky dispatchEvent proof-of-concept.
Diffstat (limited to 'src/components/script/dom/bindings/utils.rs')
-rw-r--r-- | src/components/script/dom/bindings/utils.rs | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/src/components/script/dom/bindings/utils.rs b/src/components/script/dom/bindings/utils.rs index 42f1eb7f4fc..8e8200fb18e 100644 --- a/src/components/script/dom/bindings/utils.rs +++ b/src/components/script/dom/bindings/utils.rs @@ -4,8 +4,8 @@ use dom::bindings::codegen::PrototypeList; use dom::bindings::codegen::PrototypeList::MAX_PROTO_CHAIN_LENGTH; -use dom::window; use dom::node::{AbstractNode, ScriptView}; +use dom::window; use std::libc::c_uint; use std::cast; @@ -22,6 +22,7 @@ use js::glue::{js_IsObjectProxyClass, js_IsFunctionProxyClass, IsProxyHandlerFam use js::jsapi::{JS_AlreadyHasOwnProperty, JS_NewObject, JS_NewFunction, JS_GetGlobalObject}; use js::jsapi::{JS_DefineProperties, JS_WrapValue, JS_ForwardGetPropertyTo}; use js::jsapi::{JS_GetClass, JS_LinkConstructorAndPrototype, JS_GetStringCharsAndLength}; +use js::jsapi::{JS_ObjectIsRegExp, JS_ObjectIsDate}; use js::jsapi::{JS_GetFunctionPrototype, JS_InternString, JS_GetFunctionObject}; use js::jsapi::{JS_HasPropertyById, JS_GetPrototype, JS_GetGlobalForObject}; use js::jsapi::{JS_NewUCStringCopyN, JS_DefineFunctions, JS_DefineProperty}; @@ -30,7 +31,7 @@ use js::jsapi::{JSContext, JSObject, JSBool, jsid, JSClass, JSNative, JSTracer}; use js::jsapi::{JSFunctionSpec, JSPropertySpec, JSVal, JSPropertyDescriptor}; use js::jsapi::{JSPropertyOp, JSStrictPropertyOp, JS_NewGlobalObject, JS_InitStandardClasses}; use js::jsfriendapi::bindgen::JS_NewObjectWithUniqueType; -use js::{JSPROP_ENUMERATE, JSVAL_NULL}; +use js::{JSPROP_ENUMERATE, JSVAL_NULL, JSCLASS_IS_GLOBAL, JSCLASS_IS_DOMJSCLASS}; use js::{JSPROP_PERMANENT, JSID_VOID, JSPROP_NATIVE_ACCESSORS, JSPROP_GETTER}; use js::{JSPROP_SETTER, JSVAL_VOID, JSVAL_TRUE, JSVAL_FALSE}; use js::{JS_THIS_OBJECT, JSFUN_CONSTRUCTOR, JS_CALLEE, JSPROP_READONLY}; @@ -820,6 +821,13 @@ pub fn HasPropertyOnPrototype(cx: *JSContext, proxy: *JSObject, id: jsid) -> boo } #[fixed_stack_segment] +pub fn IsConvertibleToCallbackInterface(cx: *JSContext, obj: *JSObject) -> bool { + unsafe { + JS_ObjectIsDate(cx, obj) == 0 && JS_ObjectIsRegExp(cx, obj) == 0 + } +} + +#[fixed_stack_segment] pub fn CreateDOMGlobal(cx: *JSContext, class: *JSClass) -> *JSObject { unsafe { let obj = JS_NewGlobalObject(cx, class, ptr::null()); @@ -832,6 +840,30 @@ pub fn CreateDOMGlobal(cx: *JSContext, class: *JSClass) -> *JSObject { } } +#[fixed_stack_segment] +fn cx_for_dom_wrapper(obj: *JSObject) -> *JSContext { + unsafe { + let global = GetGlobalForObjectCrossCompartment(obj); + let clasp = JS_GetClass(global); + assert!(((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)) != 0); + //XXXjdm either don't hardcode or sanity assert prototype stuff + let win = unwrap_object::<*Box<window::Window>>(global, PrototypeList::id::Window, 1); + match win { + Ok(win) => { + match (*win).data.page.js_info { + Some(ref info) => info.js_context.ptr, + None => fail!("no JS context for DOM global") + } + } + Err(_) => fail!("found DOM global that doesn't unwrap to Window") + } + } +} + +pub fn cx_for_dom_object<T: Reflectable>(obj: @mut T) -> *JSContext { + cx_for_dom_wrapper(obj.reflector().get_jsobject()) +} + /// Check if an element name is valid. See http://www.w3.org/TR/xml/#NT-Name /// for details. pub fn is_valid_element_name(name: &str) -> bool { |