diff --git a/components/script/dom/bindings/cell.rs b/components/script/dom/bindings/cell.rs index 5807138c948..a9aa8119fa9 100644 --- a/components/script/dom/bindings/cell.rs +++ b/components/script/dom/bindings/cell.rs @@ -4,6 +4,7 @@ //! A shareable mutable container for the DOM. +use crate::dom::bindings::root::{assert_in_layout, assert_in_script}; #[cfg(feature = "refcell_backtrace")] pub use accountable_refcell::{ref_filter_map, Ref, RefCell, RefMut}; #[cfg(not(feature = "refcell_backtrace"))] @@ -11,7 +12,6 @@ pub use ref_filter_map::ref_filter_map; use std::cell::{BorrowError, BorrowMutError}; #[cfg(not(feature = "refcell_backtrace"))] pub use std::cell::{Ref, RefCell, RefMut}; -use style::thread_state::{self, ThreadState}; /// A mutable field in the DOM. /// @@ -31,7 +31,7 @@ impl DomRefCell { /// For use in the layout thread only. #[allow(unsafe_code)] pub unsafe fn borrow_for_layout(&self) -> &T { - debug_assert!(thread_state::get().is_layout()); + assert_in_layout(); self.value .try_borrow_unguarded() .expect("cell is mutably borrowed") @@ -41,7 +41,7 @@ impl DomRefCell { /// #[allow(unsafe_code)] pub unsafe fn borrow_for_script_deallocation(&self) -> &mut T { - debug_assert!(thread_state::get().contains(ThreadState::SCRIPT)); + assert_in_script(); &mut *self.value.as_ptr() } @@ -49,7 +49,7 @@ impl DomRefCell { /// `RefCell::try_borrow_mut_unguarded` but that doesn't exist yet. #[allow(unsafe_code)] pub unsafe fn borrow_mut_for_layout(&self) -> &mut T { - debug_assert!(thread_state::get().is_layout()); + assert_in_layout(); &mut *self.value.as_ptr() } } @@ -105,7 +105,7 @@ impl DomRefCell { /// /// Panics if this is called off the script thread. pub fn try_borrow(&self) -> Result, BorrowError> { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); self.value.try_borrow() } @@ -120,7 +120,7 @@ impl DomRefCell { /// /// Panics if this is called off the script thread. pub fn try_borrow_mut(&self) -> Result, BorrowMutError> { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); self.value.try_borrow_mut() } } diff --git a/components/script/dom/bindings/root.rs b/components/script/dom/bindings/root.rs index 36bc2296ee5..b0b7eeb146d 100644 --- a/components/script/dom/bindings/root.rs +++ b/components/script/dom/bindings/root.rs @@ -64,7 +64,7 @@ where #[allow(unrooted_must_root)] pub unsafe fn new(value: T) -> Self { unsafe fn add_to_root_list(object: *const dyn JSTraceable) -> *const RootCollection { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); STACK_ROOTS.with(|ref root_list| { let root_list = &*root_list.get().unwrap(); root_list.root(object); @@ -137,7 +137,7 @@ where type Target = ::Target; fn deref(&self) -> &Self::Target { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); &self.value } } @@ -251,7 +251,7 @@ impl<'a> Drop for ThreadLocalStackRoots<'a> { impl RootCollection { /// Create an empty collection of roots pub fn new() -> RootCollection { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); RootCollection { roots: UnsafeCell::new(vec![]), } @@ -259,13 +259,13 @@ impl RootCollection { /// Starts tracking a trace object. unsafe fn root(&self, object: *const dyn JSTraceable) { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); (*self.roots.get()).push(object); } /// Stops tracking a trace object, asserting if it isn't found. unsafe fn unroot(&self, object: *const dyn JSTraceable) { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); let roots = &mut *self.roots.get(); match roots .iter() @@ -333,7 +333,7 @@ impl MallocSizeOf for Dom { impl Dom { /// Returns `LayoutDom` containing the same pointer. pub unsafe fn to_layout(&self) -> LayoutDom { - debug_assert!(thread_state::get().is_layout()); + assert_in_layout(); LayoutDom { value: self.ptr.as_ref(), } @@ -344,7 +344,7 @@ impl Dom { /// Create a Dom from a &T #[allow(unrooted_must_root)] pub fn from_ref(obj: &T) -> Dom { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); Dom { ptr: ptr::NonNull::from(obj), } @@ -355,7 +355,7 @@ impl Deref for Dom { type Target = T; fn deref(&self) -> &T { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); // We can only have &Dom from a rooted thing, so it's safe to deref // it to &T. unsafe { &*self.ptr.as_ptr() } @@ -432,7 +432,7 @@ where U: Castable, T: DerivedFrom, { - debug_assert!(thread_state::get().is_layout()); + assert_in_layout(); LayoutDom { value: self.value.upcast::(), } @@ -443,7 +443,7 @@ where where U: DerivedFrom, { - debug_assert!(thread_state::get().is_layout()); + assert_in_layout(); self.value.downcast::().map(|value| LayoutDom { value }) } @@ -452,7 +452,7 @@ where where U: DerivedFrom, { - debug_assert!(thread_state::get().is_layout()); + assert_in_layout(); self.value.is::() } } @@ -463,7 +463,7 @@ where { /// Get the reflector. pub unsafe fn get_jsobject(&self) -> *mut JSObject { - debug_assert!(thread_state::get().is_layout()); + assert_in_layout(); self.value.reflector().get_jsobject().get() } } @@ -508,7 +508,7 @@ impl Clone for Dom { #[inline] #[allow(unrooted_must_root)] fn clone(&self) -> Self { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); Dom { ptr: self.ptr.clone(), } @@ -518,7 +518,7 @@ impl Clone for Dom { impl Clone for LayoutDom<'_, T> { #[inline] fn clone(&self) -> Self { - debug_assert!(thread_state::get().is_layout()); + assert_in_layout(); LayoutDom { value: self.value } } } @@ -527,7 +527,7 @@ impl LayoutDom<'_, Node> { /// Create a new JS-owned value wrapped from an address known to be a /// `Node` pointer. pub unsafe fn from_trusted_node_address(inner: TrustedNodeAddress) -> Self { - debug_assert!(thread_state::get().is_layout()); + assert_in_layout(); let TrustedNodeAddress(addr) = inner; LayoutDom { value: &*(addr as *const Node), @@ -549,7 +549,7 @@ pub struct MutDom { impl MutDom { /// Create a new `MutDom`. pub fn new(initial: &T) -> MutDom { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); MutDom { val: UnsafeCell::new(Dom::from_ref(initial)), } @@ -557,7 +557,7 @@ impl MutDom { /// Set this `MutDom` to the given value. pub fn set(&self, val: &T) { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); unsafe { *self.val.get() = Dom::from_ref(val); } @@ -565,7 +565,7 @@ impl MutDom { /// Get the value in this `MutDom`. pub fn get(&self) -> DomRoot { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); unsafe { DomRoot::from_ref(&*ptr::read(self.val.get())) } } } @@ -589,6 +589,14 @@ impl PartialEq for MutDom { } } +pub(crate) fn assert_in_script() { + debug_assert!(thread_state::get().is_script()); +} + +pub(crate) fn assert_in_layout() { + debug_assert!(thread_state::get().is_layout()); +} + /// A holder that provides interior mutability for GC-managed values such as /// `Dom`, with nullability represented by an enclosing Option wrapper. /// Essentially a `Cell>>`, but safer. @@ -604,7 +612,7 @@ pub struct MutNullableDom { impl MutNullableDom { /// Create a new `MutNullableDom`. pub fn new(initial: Option<&T>) -> MutNullableDom { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); MutNullableDom { ptr: UnsafeCell::new(initial.map(Dom::from_ref)), } @@ -616,7 +624,7 @@ impl MutNullableDom { where F: FnOnce() -> DomRoot, { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); match self.get() { Some(inner) => inner, None => { @@ -631,20 +639,20 @@ impl MutNullableDom { /// For use by layout, which can't use safe types like Temporary. #[allow(unrooted_must_root)] pub unsafe fn get_inner_as_layout(&self) -> Option> { - debug_assert!(thread_state::get().is_layout()); + assert_in_layout(); (*self.ptr.get()).as_ref().map(|js| js.to_layout()) } /// Get a rooted value out of this object #[allow(unrooted_must_root)] pub fn get(&self) -> Option> { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); unsafe { ptr::read(self.ptr.get()).map(|o| DomRoot::from_ref(&*o)) } } /// Set this `MutNullableDom` to the given value. pub fn set(&self, val: Option<&T>) { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); unsafe { *self.ptr.get() = val.map(|p| Dom::from_ref(p)); } @@ -673,7 +681,7 @@ impl<'a, T: DomObject> PartialEq> for MutNullableDom { impl Default for MutNullableDom { #[allow(unrooted_must_root)] fn default() -> MutNullableDom { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); MutNullableDom { ptr: UnsafeCell::new(None), } @@ -709,7 +717,7 @@ where where F: FnOnce() -> DomRoot, { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); &self.ptr.init_once(|| Dom::from_ref(&cb())) } } @@ -717,7 +725,7 @@ where impl Default for DomOnceCell { #[allow(unrooted_must_root)] fn default() -> DomOnceCell { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); DomOnceCell { ptr: OnceCell::new(), } @@ -747,7 +755,7 @@ where /// Returns a reference to the interior of this JS object. The fact /// that this is unsafe is what necessitates the layout wrappers. pub unsafe fn unsafe_get(self) -> &'dom T { - debug_assert!(thread_state::get().is_layout()); + assert_in_layout(); self.value }