Make DomRoot::new unsafe

This commit is contained in:
Anthony Ramine 2017-09-26 15:38:10 +02:00
parent 3dbb97922d
commit 1ff6c4f9aa
3 changed files with 7 additions and 6 deletions

View file

@ -22,7 +22,6 @@
//! its hash table during the next GC. During GC, the entries of the hash table are counted //! its hash table during the next GC. During GC, the entries of the hash table are counted
//! as JS roots. //! as JS roots.
use core::nonzero::NonZero;
use dom::bindings::conversions::ToJSValConvertible; use dom::bindings::conversions::ToJSValConvertible;
use dom::bindings::error::Error; use dom::bindings::error::Error;
use dom::bindings::reflector::{DomObject, Reflector}; use dom::bindings::reflector::{DomObject, Reflector};
@ -185,7 +184,7 @@ impl<T: DomObject> Trusted<T> {
self.owner_thread == (&*live_references) as *const _ as *const libc::c_void self.owner_thread == (&*live_references) as *const _ as *const libc::c_void
})); }));
unsafe { unsafe {
DomRoot::new(NonZero::new_unchecked(self.refcount.0 as *const T)) DomRoot::from_ref(&*(self.refcount.0 as *const T))
} }
} }
} }

View file

@ -591,11 +591,11 @@ 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 fn new(unrooted: NonZero<*const T>) -> DomRoot<T> { pub unsafe fn new(unrooted: NonZero<*const 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();
unsafe { (*collection).root(&*(*unrooted.get()).reflector()) } (*collection).root(&*(*unrooted.get()).reflector())
DomRoot { DomRoot {
ptr: unrooted, ptr: unrooted,
root_list: collection, root_list: collection,
@ -605,7 +605,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> {
DomRoot::new(unsafe { NonZero::new_unchecked(unrooted) }) unsafe { DomRoot::new(NonZero::new_unchecked(unrooted)) }
} }
} }

View file

@ -86,7 +86,9 @@ impl<T: WeakReferenceable> WeakRef<T> {
/// DomRoot a weak reference. Returns `None` if the object was already collected. /// DomRoot a weak reference. Returns `None` if the object was already collected.
pub fn root(&self) -> Option<DomRoot<T>> { pub fn root(&self) -> Option<DomRoot<T>> {
unsafe { &*self.ptr.get() }.value.get().map(DomRoot::new) unsafe { &*self.ptr.get() }.value.get().map(|ptr| unsafe {
DomRoot::from_ref(&*ptr.get())
})
} }
/// Return whether the weakly-referenced object is still alive. /// Return whether the weakly-referenced object is still alive.