aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/bindings/js.rs
diff options
context:
space:
mode:
authorEli Friedman <eli.friedman@gmail.com>2015-10-14 17:48:28 -0700
committerEli Friedman <eli.friedman@gmail.com>2015-10-15 14:03:56 -0700
commit57584e74c6fc192335ed3300ac75751bada0ff93 (patch)
treea75b0f9045238a8720e31d848433635222a06b58 /components/script/dom/bindings/js.rs
parent7a08b29201d7a6ec8fd2097fc60ca47e556898d5 (diff)
downloadservo-57584e74c6fc192335ed3300ac75751bada0ff93.tar.gz
servo-57584e74c6fc192335ed3300ac75751bada0ff93.zip
Make get() and set() on MutNullableHeap use the correct types.
get() must always return a rooted value, because we have no way of ensuring the value won't be invalidated. set() takes an &T because it's convenient; there isn't any need to expose JS<T>.
Diffstat (limited to 'components/script/dom/bindings/js.rs')
-rw-r--r--components/script/dom/bindings/js.rs31
1 files changed, 17 insertions, 14 deletions
diff --git a/components/script/dom/bindings/js.rs b/components/script/dom/bindings/js.rs
index c7365a13bf8..31f6b8c4100 100644
--- a/components/script/dom/bindings/js.rs
+++ b/components/script/dom/bindings/js.rs
@@ -254,16 +254,6 @@ impl<T: HeapGCValue> MutNullableHeap<T> {
ptr: UnsafeCell::new(initial)
}
}
-
- /// Set this `MutNullableHeap` to the given value.
- pub fn set(&self, val: Option<T>) {
- unsafe { *self.ptr.get() = val; }
- }
-
- /// Retrieve a copy of the current optional inner value.
- pub fn get(&self) -> Option<T> {
- unsafe { ptr::read(self.ptr.get()) }
- }
}
impl<T: Reflectable> MutNullableHeap<JS<T>> {
@@ -273,10 +263,10 @@ impl<T: Reflectable> MutNullableHeap<JS<T>> {
where F: FnOnce() -> Root<T>
{
match self.get() {
- Some(inner) => Root::from_rooted(inner),
+ Some(inner) => inner,
None => {
let inner = cb();
- self.set(Some(JS::from_rooted(&inner)));
+ self.set(Some(&inner));
inner
},
}
@@ -289,9 +279,22 @@ impl<T: Reflectable> MutNullableHeap<JS<T>> {
}
/// Get a rooted value out of this object
- // FIXME(#6684)
+ pub fn get(&self) -> Option<Root<T>> {
+ unsafe {
+ ptr::read(self.ptr.get()).map(|o| o.root())
+ }
+ }
+
+ /// Get a rooted value out of this object
pub fn get_rooted(&self) -> Option<Root<T>> {
- self.get().map(|o| o.root())
+ self.get()
+ }
+
+ /// Set this `MutNullableHeap` to the given value.
+ pub fn set(&self, val: Option<&T>) {
+ unsafe {
+ *self.ptr.get() = val.map(|p| JS::from_ref(p));
+ }
}
}