Store a Dom<T> in DomRoot<T>

This commit is contained in:
Anthony Ramine 2017-09-26 16:10:57 +02:00
parent 1ff6c4f9aa
commit 9ea645481b

View file

@ -558,10 +558,11 @@ pub unsafe fn trace_roots(tracer: *mut JSTracer) {
/// are additive, so this object's destruction will not invalidate other roots /// are additive, so this object's destruction will not invalidate other roots
/// for the same JS value. `Root`s cannot outlive the associated /// for the same JS value. `Root`s cannot outlive the associated
/// `RootCollection` object. /// `RootCollection` object.
#[allow(unrooted_must_root)]
#[allow_unrooted_interior] #[allow_unrooted_interior]
pub struct DomRoot<T: DomObject> { pub struct DomRoot<T: DomObject> {
/// Reference to rooted value that must not outlive this container /// Reference to rooted value that must not outlive this container
ptr: NonZero<*const T>, ptr: Dom<T>,
/// List that ensures correct dynamic root ordering /// List that ensures correct dynamic root ordering
root_list: *const RootCollection, root_list: *const RootCollection,
} }
@ -591,11 +592,12 @@ impl<T: DomObject> DomRoot<T> {
/// Create a new stack-bounded root for the provided JS-owned value. /// Create a new stack-bounded root for the provided JS-owned value.
/// It cannot outlive its associated `RootCollection`, and it gives /// It cannot outlive its associated `RootCollection`, and it gives
/// out references which cannot outlive this new `Root`. /// out references which cannot outlive this new `Root`.
pub unsafe fn new(unrooted: NonZero<*const T>) -> DomRoot<T> { #[allow(unrooted_must_root)]
unsafe fn new(unrooted: Dom<T>) -> DomRoot<T> {
debug_assert!(thread_state::get().is_script()); debug_assert!(thread_state::get().is_script());
STACK_ROOTS.with(|ref collection| { STACK_ROOTS.with(|ref collection| {
let RootCollectionPtr(collection) = collection.get().unwrap(); let RootCollectionPtr(collection) = collection.get().unwrap();
(*collection).root(&*(*unrooted.get()).reflector()) (*collection).root(unrooted.reflector());
DomRoot { DomRoot {
ptr: unrooted, ptr: unrooted,
root_list: collection, root_list: collection,
@ -605,7 +607,7 @@ impl<T: DomObject> DomRoot<T> {
/// Generate a new root from a reference /// Generate a new root from a reference
pub fn from_ref(unrooted: &T) -> DomRoot<T> { pub fn from_ref(unrooted: &T) -> DomRoot<T> {
unsafe { DomRoot::new(NonZero::new_unchecked(unrooted)) } unsafe { DomRoot::new(Dom::from_ref(unrooted)) }
} }
} }
@ -620,7 +622,7 @@ impl<T: DomObject> Deref for DomRoot<T> {
type Target = T; type Target = T;
fn deref(&self) -> &T { fn deref(&self) -> &T {
debug_assert!(thread_state::get().is_script()); debug_assert!(thread_state::get().is_script());
unsafe { &*self.ptr.get() } &self.ptr
} }
} }