diff options
Diffstat (limited to 'src/components/script/dom/bindings/utils.rs')
-rw-r--r-- | src/components/script/dom/bindings/utils.rs | 92 |
1 files changed, 27 insertions, 65 deletions
diff --git a/src/components/script/dom/bindings/utils.rs b/src/components/script/dom/bindings/utils.rs index 99f0d9f4fa9..f1e69e32021 100644 --- a/src/components/script/dom/bindings/utils.rs +++ b/src/components/script/dom/bindings/utils.rs @@ -29,7 +29,7 @@ use js::jsapi::{JS_NewUCStringCopyN, JS_DefineFunctions, JS_DefineProperty}; use js::jsapi::{JS_ValueToString, JS_GetReservedSlot, JS_SetReservedSlot}; 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::jsapi::{JS_NewGlobalObject, JS_InitStandardClasses}; use js::jsapi::{JSString, JS_CallTracer, JSTRACE_OBJECT}; use js::jsapi::{JS_IsExceptionPending}; use js::jsfriendapi::bindgen::JS_NewObjectWithUniqueType; @@ -47,22 +47,18 @@ mod jsval { use js::glue::{RUST_JSVAL_IS_STRING, RUST_JSVAL_TO_STRING}; use js::jsapi::{JSVal, JSString}; - #[fixed_stack_segment] pub fn is_null(v: JSVal) -> bool { unsafe { RUST_JSVAL_IS_NULL(v) == 1 } } - #[fixed_stack_segment] pub fn is_undefined(v: JSVal) -> bool { unsafe { RUST_JSVAL_IS_VOID(v) == 1 } } - #[fixed_stack_segment] pub fn is_string(v: JSVal) -> bool { unsafe { RUST_JSVAL_IS_STRING(v) == 1 } } - #[fixed_stack_segment] pub unsafe fn to_string(v: JSVal) -> *JSString { RUST_JSVAL_TO_STRING(v) } @@ -149,7 +145,6 @@ fn is_dom_class(clasp: *JSClass) -> bool { } } -#[fixed_stack_segment] pub fn is_dom_proxy(obj: *JSObject) -> bool { unsafe { (js_IsObjectProxyClass(obj) || js_IsFunctionProxyClass(obj)) && @@ -157,7 +152,6 @@ pub fn is_dom_proxy(obj: *JSObject) -> bool { } } -#[fixed_stack_segment] pub unsafe fn dom_object_slot(obj: *JSObject) -> u32 { let clasp = JS_GetClass(obj); if is_dom_class(clasp) { @@ -168,14 +162,12 @@ pub unsafe fn dom_object_slot(obj: *JSObject) -> u32 { } } -#[fixed_stack_segment] pub unsafe fn unwrap<T>(obj: *JSObject) -> T { let slot = dom_object_slot(obj); let val = JS_GetReservedSlot(obj, slot); cast::transmute(RUST_JSVAL_TO_PRIVATE(val)) } -#[fixed_stack_segment] pub unsafe fn get_dom_class(obj: *JSObject) -> Result<DOMClass, ()> { let clasp = JS_GetClass(obj); if is_dom_class(clasp) { @@ -194,7 +186,7 @@ pub unsafe fn get_dom_class(obj: *JSObject) -> Result<DOMClass, ()> { pub fn unwrap_object<T>(obj: *JSObject, proto_id: PrototypeList::id::ID, proto_depth: uint) -> Result<T, ()> { unsafe { - do get_dom_class(obj).and_then |dom_class| { + get_dom_class(obj).and_then(|dom_class| { if dom_class.interface_chain[proto_depth] == proto_id { debug!("good prototype"); Ok(unwrap(obj)) @@ -202,11 +194,10 @@ pub fn unwrap_object<T>(obj: *JSObject, proto_id: PrototypeList::id::ID, proto_d debug!("bad prototype"); Err(()) } - } + }) } } -#[fixed_stack_segment] pub fn unwrap_value<T>(val: *JSVal, proto_id: PrototypeList::id::ID, proto_depth: uint) -> Result<T, ()> { unsafe { let obj = RUST_JSVAL_TO_OBJECT(*val); @@ -216,22 +207,19 @@ pub fn unwrap_value<T>(val: *JSVal, proto_id: PrototypeList::id::ID, proto_depth pub unsafe fn squirrel_away<T>(x: @mut T) -> *Box<T> { let y: *Box<T> = cast::transmute(x); - cast::forget(x); y } -#[fixed_stack_segment] pub fn jsstring_to_str(cx: *JSContext, s: *JSString) -> ~str { unsafe { let length = 0; let chars = JS_GetStringCharsAndLength(cx, s, &length); - do vec::raw::buf_as_slice(chars, length as uint) |char_vec| { + vec::raw::buf_as_slice(chars, length as uint, |char_vec| { str::from_utf16(char_vec) - } + }) } } -#[fixed_stack_segment] pub fn jsid_to_str(cx: *JSContext, id: jsid) -> ~str { unsafe { assert!(RUST_JSID_IS_STRING(id) != 0); @@ -245,7 +233,6 @@ pub enum StringificationBehavior { Empty, } -#[fixed_stack_segment] pub fn jsval_to_str(cx: *JSContext, v: JSVal, nullBehavior: StringificationBehavior) -> Result<~str, ()> { if jsval::is_null(v) && nullBehavior == Empty { @@ -261,7 +248,6 @@ pub fn jsval_to_str(cx: *JSContext, v: JSVal, } } -#[fixed_stack_segment] pub fn jsval_to_domstring(cx: *JSContext, v: JSVal) -> Result<Option<DOMString>, ()> { if jsval::is_null(v) || jsval::is_undefined(v) { Ok(None) @@ -276,20 +262,17 @@ pub fn jsval_to_domstring(cx: *JSContext, v: JSVal) -> Result<Option<DOMString>, } } -#[fixed_stack_segment] pub unsafe fn str_to_jsval(cx: *JSContext, string: DOMString) -> JSVal { - do string.to_utf16().as_imm_buf |buf, len| { - let jsstr = JS_NewUCStringCopyN(cx, buf, len as libc::size_t); - if jsstr.is_null() { - // FIXME: is there something else we should do on failure? - JSVAL_NULL - } else { - RUST_STRING_TO_JSVAL(jsstr) - } + let string_utf16 = string.to_utf16(); + let jsstr = JS_NewUCStringCopyN(cx, string_utf16.as_ptr(), string_utf16.len() as libc::size_t); + if jsstr.is_null() { + // FIXME: is there something else we should do on failure? + JSVAL_NULL + } else { + RUST_STRING_TO_JSVAL(jsstr) } } -#[fixed_stack_segment] pub unsafe fn domstring_to_jsval(cx: *JSContext, string: Option<DOMString>) -> JSVal { match string { None => JSVAL_NULL, @@ -313,7 +296,7 @@ pub static DOM_PROTOTYPE_SLOT: u32 = js::JSCLASS_GLOBAL_SLOT_COUNT; // NOTE: This is baked into the Ion JIT as 0 in codegen for LGetDOMProperty and // LSetDOMProperty. Those constants need to be changed accordingly if this value // changes. -static JSCLASS_DOM_GLOBAL: u32 = js::JSCLASS_USERBIT1; +pub static JSCLASS_DOM_GLOBAL: u32 = js::JSCLASS_USERBIT1; pub struct NativeProperties { staticMethods: *JSFunctionSpec, @@ -379,7 +362,6 @@ pub struct DOMJSClass { dom_class: DOMClass } -#[fixed_stack_segment] pub fn GetProtoOrIfaceArray(global: *JSObject) -> **JSObject { unsafe { /*assert ((*JS_GetClass(global)).flags & JSCLASS_DOM_GLOBAL) != 0;*/ @@ -387,7 +369,6 @@ pub fn GetProtoOrIfaceArray(global: *JSObject) -> **JSObject { } } -#[fixed_stack_segment] pub fn CreateInterfaceObjects2(cx: *JSContext, global: *JSObject, receiver: *JSObject, protoProto: *JSObject, protoClass: *JSClass, constructorClass: *JSClass, constructor: Option<JSNative>, @@ -415,11 +396,11 @@ pub fn CreateInterfaceObjects2(cx: *JSContext, global: *JSObject, receiver: *JSO let mut interface = ptr::null(); if constructorClass.is_not_null() || constructor.is_some() { - interface = do name.to_c_str().with_ref |s| { + interface = name.to_c_str().with_ref(|s| { CreateInterfaceObject(cx, global, receiver, constructorClass, constructor, ctorNargs, proto, staticMethods, constants, s) - }; + }); if interface.is_null() { return ptr::null(); } @@ -432,7 +413,6 @@ pub fn CreateInterfaceObjects2(cx: *JSContext, global: *JSObject, receiver: *JSO } } -#[fixed_stack_segment] fn CreateInterfaceObject(cx: *JSContext, global: *JSObject, receiver: *JSObject, constructorClass: *JSClass, constructorNative: Option<JSNative>, ctorNargs: u32, proto: *JSObject, @@ -467,11 +447,11 @@ fn CreateInterfaceObject(cx: *JSContext, global: *JSObject, receiver: *JSObject, } if constructorClass.is_not_null() { - let toString = do "toString".to_c_str().with_ref |s| { + let toString = "toString".to_c_str().with_ref(|s| { DefineFunctionWithReserved(cx, constructor, s, InterfaceObjectToString, 0, 0) - }; + }); if toString.is_null() { return ptr::null(); } @@ -511,7 +491,6 @@ fn CreateInterfaceObject(cx: *JSContext, global: *JSObject, receiver: *JSObject, } } -#[fixed_stack_segment] fn DefineConstants(cx: *JSContext, obj: *JSObject, constants: *ConstantSpec) -> bool { let mut i = 0; loop { @@ -541,21 +520,18 @@ fn DefineConstants(cx: *JSContext, obj: *JSObject, constants: *ConstantSpec) -> } } -#[fixed_stack_segment] fn DefineMethods(cx: *JSContext, obj: *JSObject, methods: *JSFunctionSpec) -> bool { unsafe { JS_DefineFunctions(cx, obj, methods) != 0 } } -#[fixed_stack_segment] fn DefineProperties(cx: *JSContext, obj: *JSObject, properties: *JSPropertySpec) -> bool { unsafe { JS_DefineProperties(cx, obj, properties) != 0 } } -#[fixed_stack_segment] fn CreateInterfacePrototypeObject(cx: *JSContext, global: *JSObject, parentProto: *JSObject, protoClass: *JSClass, methods: *JSFunctionSpec, @@ -592,17 +568,16 @@ pub trait Traceable { fn trace(&self, trc: *mut JSTracer); } -#[fixed_stack_segment] pub fn trace_reflector(tracer: *mut JSTracer, description: &str, reflector: &Reflector) { unsafe { - do description.to_c_str().with_ref |name| { + description.to_c_str().with_ref(|name| { (*tracer).debugPrinter = ptr::null(); (*tracer).debugPrintIndex = -1; (*tracer).debugPrintArg = name as *libc::c_void; debug!("tracing {:s}", description); JS_CallTracer(tracer as *JSTracer, reflector.get_jsobject(), JSTRACE_OBJECT as u32); - } + }); } } @@ -610,13 +585,12 @@ pub fn trace_option<T: Reflectable>(tracer: *mut JSTracer, description: &str, op option.map(|some| trace_reflector(tracer, description, some.reflector())); } -#[fixed_stack_segment] pub fn initialize_global(global: *JSObject) { let protoArray = @mut ([0 as *JSObject, ..PrototypeList::id::_ID_Count as uint]); unsafe { //XXXjdm we should be storing the box pointer instead of the inner - let box = squirrel_away(protoArray); - let inner = ptr::to_unsafe_ptr(&(*box).data); + let box_ = squirrel_away(protoArray); + let inner = ptr::to_unsafe_ptr(&(*box_).data); JS_SetReservedSlot(global, DOM_PROTOTYPE_SLOT, RUST_PRIVATE_TO_JSVAL(inner as *libc::c_void)); @@ -668,7 +642,6 @@ impl Reflector { } } -#[fixed_stack_segment] pub fn GetReflector(cx: *JSContext, reflector: &Reflector, vp: *mut JSVal) -> JSBool { let obj = reflector.get_jsobject(); @@ -679,7 +652,6 @@ pub fn GetReflector(cx: *JSContext, reflector: &Reflector, } } -#[fixed_stack_segment] pub fn GetPropertyOnPrototype(cx: *JSContext, proxy: *JSObject, id: jsid, found: *mut bool, vp: *JSVal) -> bool { unsafe { @@ -703,7 +675,6 @@ pub fn GetPropertyOnPrototype(cx: *JSContext, proxy: *JSObject, id: jsid, found: } } -#[fixed_stack_segment] pub fn GetArrayIndexFromId(_cx: *JSContext, id: jsid) -> Option<u32> { unsafe { if RUST_JSID_IS_INT(id) != 0 { @@ -727,7 +698,6 @@ pub fn GetArrayIndexFromId(_cx: *JSContext, id: jsid) -> Option<u32> { }*/ } -#[fixed_stack_segment] pub fn XrayResolveProperty(cx: *JSContext, wrapper: *JSObject, id: jsid, @@ -754,7 +724,7 @@ pub fn XrayResolveProperty(cx: *JSContext, RUST_SET_JITINFO(fun, attr.getter.info); let funobj = JS_GetFunctionObject(fun); - (*desc).getter = Some(funobj as JSPropertyOp); + (*desc).getter = Some(cast::transmute(funobj)); (*desc).attrs |= JSPROP_GETTER; if attr.setter.op.is_some() { let fun = JS_NewFunction(cx, attr.setter.op, 1, 0, global, ptr::null()); @@ -764,7 +734,7 @@ pub fn XrayResolveProperty(cx: *JSContext, RUST_SET_JITINFO(fun, attr.setter.info); let funobj = JS_GetFunctionObject(fun); - (*desc).setter = Some(funobj as JSStrictPropertyOp); + (*desc).setter = Some(cast::transmute(funobj)); (*desc).attrs |= JSPROP_SETTER; } else { (*desc).setter = None; @@ -777,7 +747,6 @@ pub fn XrayResolveProperty(cx: *JSContext, } } -#[fixed_stack_segment] fn InternJSString(cx: *JSContext, chars: *libc::c_char) -> Option<jsid> { unsafe { let s = JS_InternString(cx, chars); @@ -824,7 +793,6 @@ pub struct EnumEntry { length: uint } -#[fixed_stack_segment] pub fn FindEnumStringIndex(cx: *JSContext, v: JSVal, values: &[EnumEntry]) -> Result<uint, ()> { @@ -865,14 +833,12 @@ pub fn HasPropertyOnPrototype(cx: *JSContext, proxy: *JSObject, id: jsid) -> boo return !GetPropertyOnPrototype(cx, proxy, id, &mut found, ptr::null()) || found; } -#[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()); @@ -886,7 +852,6 @@ pub fn CreateDOMGlobal(cx: *JSContext, class: *JSClass) -> *JSObject { } /// Returns the global object of the realm that the given JS object was created in. -#[fixed_stack_segment] fn global_object_for_js_object(obj: *JSObject) -> *Box<window::Window> { unsafe { let global = GetGlobalForObjectCrossCompartment(obj); @@ -900,7 +865,6 @@ fn global_object_for_js_object(obj: *JSObject) -> *Box<window::Window> { } } -#[fixed_stack_segment] fn cx_for_dom_reflector(obj: *JSObject) -> *JSContext { unsafe { let win = global_object_for_js_object(obj); @@ -912,7 +876,6 @@ fn cx_for_dom_reflector(obj: *JSObject) -> *JSContext { } /// Returns the global object of the realm that the given DOM object was created in. -#[fixed_stack_segment] pub fn global_object_for_dom_object<T: Reflectable>(obj: &mut T) -> *Box<window::Window> { global_object_for_js_object(obj.reflector().get_jsobject()) } @@ -921,7 +884,6 @@ pub fn cx_for_dom_object<T: Reflectable>(obj: &mut T) -> *JSContext { cx_for_dom_reflector(obj.reflector().get_jsobject()) } -#[fixed_stack_segment] pub fn throw_method_failed_with_details<T>(cx: *JSContext, result: Result<T, Error>, interface: &'static str, @@ -929,9 +891,9 @@ pub fn throw_method_failed_with_details<T>(cx: *JSContext, assert!(result.is_err()); assert!(unsafe { JS_IsExceptionPending(cx) } == 0); let message = format!("Method failed: {}.{}", interface, member); - do message.with_c_str |string| { + message.with_c_str(|string| { unsafe { ReportError(cx, string) }; - } + }); return 0; } @@ -979,7 +941,7 @@ pub fn xml_name_type(name: &str) -> XMLName { } } - let mut iter = name.iter(); + let mut iter = name.chars(); let mut non_qname_colons = false; let mut seen_colon = false; match iter.next() { @@ -994,7 +956,7 @@ pub fn xml_name_type(name: &str) -> XMLName { } } - for c in name.iter() { + for c in name.chars() { if !is_valid_continuation(c) { return InvalidXMLName; } |