diff options
Diffstat (limited to 'components/script/dom/bindings/js.rs')
-rw-r--r-- | components/script/dom/bindings/js.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/components/script/dom/bindings/js.rs b/components/script/dom/bindings/js.rs index c9f3e157b75..1f3cb7814e0 100644 --- a/components/script/dom/bindings/js.rs +++ b/components/script/dom/bindings/js.rs @@ -45,11 +45,13 @@ //! - `OptionalSettable`: allows assigning `Option` values of `JSRef`/`Temporary` to fields of `Option<JS<T>>` //! - `RootedReference`: makes obtaining an `Option<JSRef<T>>` from an `Option<Root<T>>` easy +use dom::bindings::trace::JSTraceable; use dom::bindings::utils::{Reflector, Reflectable}; use dom::node::Node; use dom::xmlhttprequest::{XMLHttpRequest, TrustedXHRAddress}; use dom::worker::{Worker, TrustedWorkerAddress}; use js::jsapi::JSObject; +use js::jsval::JSVal; use layout_interface::TrustedNodeAddress; use script_task::StackRoots; @@ -194,6 +196,40 @@ impl<T: Reflectable> Reflectable for JS<T> { } } +pub trait HeapGCValue: JSTraceable { +} + +impl HeapGCValue for JSVal { +} + +impl<T: Reflectable> HeapGCValue for JS<T> { +} + +/// A mutable holder for a GC-owned SpiderMonkey value stored on the heap. +/// Must be used in place of traditional interior mutability to ensure proper +/// GC barriers are enforced. +#[must_root] +#[jstraceable] +pub struct MutHeap<T: HeapGCValue+Copy> { + val: Cell<T>, +} + +impl<T: HeapGCValue+Copy> MutHeap<T> { + pub fn new(initial: T) -> MutHeap<T> { + MutHeap { + val: Cell::new(initial), + } + } + + pub fn set(&self, val: T) { + self.val.set(val) + } + + pub fn get(&self) -> T { + self.val.get() + } +} + /// A mutable `JS<T>` value, with nullability represented by an enclosing /// Option wrapper. Must be used in place of traditional internal mutability /// to ensure that the proper GC barriers are enforced. |