aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/bindings/refcounted.rs
diff options
context:
space:
mode:
Diffstat (limited to 'components/script/dom/bindings/refcounted.rs')
-rw-r--r--components/script/dom/bindings/refcounted.rs33
1 files changed, 20 insertions, 13 deletions
diff --git a/components/script/dom/bindings/refcounted.rs b/components/script/dom/bindings/refcounted.rs
index 046ae913b50..ad7f9239eae 100644
--- a/components/script/dom/bindings/refcounted.rs
+++ b/components/script/dom/bindings/refcounted.rs
@@ -22,11 +22,12 @@
//! is rooted when a hashmap entry is first created, and unrooted when the hashmap entry
//! is removed.
-use dom::bindings::js::{Temporary, JSRef, Unrooted};
+use dom::bindings::js::Root;
use dom::bindings::utils::{Reflector, Reflectable};
+use dom::bindings::trace::trace_reflector;
use script_task::{ScriptMsg, ScriptChan};
-use js::jsapi::{JS_AddObjectRoot, JS_RemoveObjectRoot, JSContext};
+use js::jsapi::{JSContext, JSTracer};
use libc;
use std::cell::RefCell;
@@ -35,6 +36,7 @@ use std::collections::hash_map::Entry::{Vacant, Occupied};
use std::marker::PhantomData;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
+use core::nonzero::NonZero;
thread_local!(pub static LIVE_REFERENCES: Rc<RefCell<Option<LiveDOMReferences>>> = Rc::new(RefCell::new(None)));
@@ -63,7 +65,7 @@ impl<T: Reflectable> Trusted<T> {
/// Create a new `Trusted<T>` instance from an existing DOM pointer. The DOM object will
/// be prevented from being GCed for the duration of the resulting `Trusted<T>` object's
/// lifetime.
- pub fn new(cx: *mut JSContext, ptr: JSRef<T>, script_chan: Box<ScriptChan + Send>) -> Trusted<T> {
+ pub fn new(cx: *mut JSContext, ptr: &T, script_chan: Box<ScriptChan + Send>) -> Trusted<T> {
LIVE_REFERENCES.with(|ref r| {
let r = r.borrow();
let live_references = r.as_ref().unwrap();
@@ -81,14 +83,14 @@ impl<T: Reflectable> Trusted<T> {
/// Obtain a usable DOM pointer from a pinned `Trusted<T>` value. Fails if used on
/// a different thread than the original value from which this `Trusted<T>` was
/// obtained.
- pub fn to_temporary(&self) -> Temporary<T> {
+ pub fn root(&self) -> Root<T> {
assert!(LIVE_REFERENCES.with(|ref r| {
let r = r.borrow();
let live_references = r.as_ref().unwrap();
self.owner_thread == (&*live_references) as *const _ as *const libc::c_void
}));
unsafe {
- Temporary::from_unrooted(Unrooted::from_raw(self.ptr as *const T))
+ Root::new(NonZero::new(self.ptr as *const T))
}
}
}
@@ -151,10 +153,6 @@ impl LiveDOMReferences {
refcount.clone()
}
Vacant(entry) => {
- unsafe {
- let rootable = (*ptr).reflector().rootable();
- JS_AddObjectRoot(cx, rootable);
- }
let refcount = Arc::new(Mutex::new(1));
entry.insert(refcount.clone());
refcount
@@ -168,7 +166,6 @@ impl LiveDOMReferences {
LIVE_REFERENCES.with(|ref r| {
let r = r.borrow();
let live_references = r.as_ref().unwrap();
- let reflectable = raw_reflectable as *const Reflector;
let mut table = live_references.table.borrow_mut();
match table.entry(raw_reflectable) {
Occupied(entry) => {
@@ -178,9 +175,6 @@ impl LiveDOMReferences {
return;
}
- unsafe {
- JS_RemoveObjectRoot(cx, (*reflectable).rootable());
- }
let _ = entry.remove();
}
Vacant(_) => {
@@ -194,3 +188,16 @@ impl LiveDOMReferences {
})
}
}
+
+/// A JSTraceDataOp for tracing reflectors held in LIVE_REFERENCES
+pub unsafe extern fn trace_refcounted_objects(tracer: *mut JSTracer, _data: *mut libc::c_void) {
+ LIVE_REFERENCES.with(|ref r| {
+ let r = r.borrow();
+ let live_references = r.as_ref().unwrap();
+ let table = live_references.table.borrow();
+ for obj in table.keys() {
+ let reflectable = &*(*obj as *const Reflector);
+ trace_reflector(tracer, "LIVE_REFERENCES", reflectable);
+ }
+ });
+}