Use Cell for Reflector::object.

This commit is contained in:
Ms2ger 2014-06-12 14:19:29 +02:00
parent 4c5437c58b
commit c331b200b1
3 changed files with 16 additions and 13 deletions

View file

@ -1838,7 +1838,7 @@ class CGWrapMethod(CGAbstractMethod):
%s %s
raw.mut_reflector().set_jsobject(obj); raw.reflector().set_jsobject(obj);
return raw;""" % CreateBindingJSObject(self.descriptor, "scope") return raw;""" % CreateBindingJSObject(self.descriptor, "scope")
else: else:
@ -1848,7 +1848,7 @@ class CGWrapMethod(CGAbstractMethod):
let proto = GetProtoObject(aCx, obj, obj); let proto = GetProtoObject(aCx, obj, obj);
JS_SetPrototype(aCx, obj, proto); JS_SetPrototype(aCx, obj, proto);
}); });
raw.mut_reflector().set_jsobject(obj); raw.reflector().set_jsobject(obj);
return raw;""" % CreateBindingJSObject(self.descriptor) return raw;""" % CreateBindingJSObject(self.descriptor)

View file

@ -14,8 +14,9 @@ use servo_util::str::DOMString;
use collections::hashmap::HashMap; use collections::hashmap::HashMap;
use libc; use libc;
use libc::c_uint; use libc::c_uint;
use std::mem; use std::cell::Cell;
use std::cmp::Eq; use std::cmp::Eq;
use std::mem;
use std::ptr; use std::ptr;
use std::ptr::null; use std::ptr::null;
use std::slice; use std::slice;
@ -415,31 +416,33 @@ pub fn reflect_dom_object<T: Reflectable>
#[allow(raw_pointer_deriving)] #[allow(raw_pointer_deriving)]
#[deriving(Eq)] #[deriving(Eq)]
pub struct Reflector { pub struct Reflector {
object: *mut JSObject, object: Cell<*mut JSObject>,
} }
impl Reflector { impl Reflector {
#[inline] #[inline]
pub fn get_jsobject(&self) -> *mut JSObject { pub fn get_jsobject(&self) -> *mut JSObject {
self.object self.object.get()
} }
pub fn set_jsobject(&mut self, object: *mut JSObject) { pub fn set_jsobject(&self, object: *mut JSObject) {
assert!(self.object.is_null()); assert!(self.object.get().is_null());
assert!(object.is_not_null()); assert!(object.is_not_null());
self.object = object; self.object.set(object);
} }
/// Return a pointer to the memory location at which the JS reflector object is stored. /// Return a pointer to the memory location at which the JS reflector object is stored.
/// Used by Temporary values to root the reflector, as required by the JSAPI rooting /// Used by Temporary values to root the reflector, as required by the JSAPI rooting
/// APIs. /// APIs.
pub fn rootable<'a>(&'a mut self) -> &'a mut *mut JSObject { pub fn rootable(&self) -> *mut *mut JSObject {
&mut self.object &self.object as *Cell<*mut JSObject>
as *mut Cell<*mut JSObject>
as *mut *mut JSObject
} }
pub fn new() -> Reflector { pub fn new() -> Reflector {
Reflector { Reflector {
object: ptr::mut_null(), object: Cell::new(ptr::mut_null()),
} }
} }
} }

View file

@ -622,14 +622,14 @@ impl<'a> PrivateXMLHttpRequestHelpers for JSRef<'a, XMLHttpRequest> {
unsafe fn to_trusted(&mut self) -> TrustedXHRAddress { unsafe fn to_trusted(&mut self) -> TrustedXHRAddress {
assert!(self.pinned == false); assert!(self.pinned == false);
self.pinned = true; self.pinned = true;
JS_AddObjectRoot(self.global.root().get_cx(), self.mut_reflector().rootable()); JS_AddObjectRoot(self.global.root().get_cx(), self.reflector().rootable());
TrustedXHRAddress(self.deref() as *XMLHttpRequest as *libc::c_void) TrustedXHRAddress(self.deref() as *XMLHttpRequest as *libc::c_void)
} }
fn release(&mut self) { fn release(&mut self) {
assert!(self.pinned); assert!(self.pinned);
unsafe { unsafe {
JS_RemoveObjectRoot(self.global.root().get_cx(), self.mut_reflector().rootable()); JS_RemoveObjectRoot(self.global.root().get_cx(), self.reflector().rootable());
} }
self.pinned = false; self.pinned = false;
} }