Initialize rooted dictionaries to a stable value before setting fields.

This commit is contained in:
Josh Matthews 2017-05-26 14:27:05 -04:00
parent 16166d6673
commit f5eb8445b0
2 changed files with 58 additions and 23 deletions

View file

@ -31,7 +31,8 @@ use dom::bindings::trace::JSTraceable;
use dom::bindings::trace::trace_reflector;
use dom::node::Node;
use heapsize::HeapSizeOf;
use js::jsapi::{JSObject, JSTracer};
use js::jsapi::{JSObject, JSTracer, Heap};
use js::rust::GCMethods;
use mitochondria::OnceCell;
use script_layout_interface::TrustedNodeAddress;
use script_thread::STACK_ROOTS;
@ -654,3 +655,29 @@ unsafe impl<T: DomObject> JSTraceable for Root<T> {
// Already traced.
}
}
/// Helper trait for safer manipulations of Option<Heap<T>> values.
pub trait OptionalHeapSetter {
type Value;
/// Update this optional heap value with a new value.
fn set(&mut self, v: Option<Self::Value>);
}
impl<T: GCMethods + Copy> OptionalHeapSetter for Option<Heap<T>> where Heap<T>: Default {
type Value = T;
fn set(&mut self, v: Option<T>) {
let v = match v {
None => {
*self = None;
return;
}
Some(v) => v,
};
if self.is_none() {
*self = Some(Heap::default());
}
self.as_ref().unwrap().set(v);
}
}