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

View file

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