diff options
Diffstat (limited to 'components/script/dom/bindings/cell.rs')
-rw-r--r-- | components/script/dom/bindings/cell.rs | 72 |
1 files changed, 34 insertions, 38 deletions
diff --git a/components/script/dom/bindings/cell.rs b/components/script/dom/bindings/cell.rs index 8e82de94381..7d567e04847 100644 --- a/components/script/dom/bindings/cell.rs +++ b/components/script/dom/bindings/cell.rs @@ -1,73 +1,69 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ //! A shareable mutable container for the DOM. -use std::cell::{BorrowError, BorrowMutError, Ref, RefCell, RefMut}; -use style::thread_state; +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"))] +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}; /// A mutable field in the DOM. /// -/// This extends the API of `core::cell::RefCell` to allow unsafe access in +/// This extends the API of `std::cell::RefCell` to allow unsafe access in /// certain situations, with dynamic checking in debug builds. -#[derive(Clone, Debug, Default, HeapSizeOf, PartialEq)] -pub struct DOMRefCell<T> { +#[derive(Clone, Debug, Default, MallocSizeOf, PartialEq)] +pub struct DomRefCell<T> { value: RefCell<T>, } -// Functionality specific to Servo's `DOMRefCell` type +// Functionality specific to Servo's `DomRefCell` type // =================================================== -impl<T> DOMRefCell<T> { +impl<T> DomRefCell<T> { /// Return a reference to the contents. /// /// 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()); - &*self.value.as_ptr() - } - - /// Borrow the contents for the purpose of GC tracing. - /// - /// This succeeds even if the object is mutably borrowed, - /// so you have to be careful in trace code! - #[allow(unsafe_code)] - pub unsafe fn borrow_for_gc_trace(&self) -> &T { - // FIXME: IN_GC isn't reliable enough - doesn't catch minor GCs - // https://github.com/servo/servo/issues/6389 - // debug_assert!(thread_state::get().contains(SCRIPT | IN_GC)); - &*self.value.as_ptr() + assert_in_layout(); + self.value + .try_borrow_unguarded() + .expect("cell is mutably borrowed") } /// Borrow the contents for the purpose of script deallocation. /// #[allow(unsafe_code)] pub unsafe fn borrow_for_script_deallocation(&self) -> &mut T { - debug_assert!(thread_state::get().contains(thread_state::SCRIPT)); + assert_in_script(); &mut *self.value.as_ptr() } - /// Version of the above that we use during restyle while the script thread - /// is blocked. - pub fn borrow_mut_for_layout(&self) -> RefMut<T> { - debug_assert!(thread_state::get().is_layout()); - self.value.borrow_mut() + /// Mutably borrow a cell for layout. Ideally this would use + /// `RefCell::try_borrow_mut_unguarded` but that doesn't exist yet. + #[allow(unsafe_code)] + pub unsafe fn borrow_mut_for_layout(&self) -> &mut T { + assert_in_layout(); + &mut *self.value.as_ptr() } } -// Functionality duplicated with `core::cell::RefCell` +// Functionality duplicated with `std::cell::RefCell` // =================================================== -impl<T> DOMRefCell<T> { - /// Create a new `DOMRefCell` containing `value`. - pub fn new(value: T) -> DOMRefCell<T> { - DOMRefCell { +impl<T> DomRefCell<T> { + /// Create a new `DomRefCell` containing `value`. + pub fn new(value: T) -> DomRefCell<T> { + DomRefCell { value: RefCell::new(value), } } - /// Immutably borrows the wrapped value. /// /// The borrow lasts until the returned `Ref` exits scope. Multiple @@ -79,7 +75,7 @@ impl<T> DOMRefCell<T> { /// /// Panics if the value is currently mutably borrowed. pub fn borrow(&self) -> Ref<T> { - self.try_borrow().expect("DOMRefCell<T> already mutably borrowed") + self.value.borrow() } /// Mutably borrows the wrapped value. @@ -93,7 +89,7 @@ impl<T> DOMRefCell<T> { /// /// Panics if the value is currently borrowed. pub fn borrow_mut(&self) -> RefMut<T> { - self.try_borrow_mut().expect("DOMRefCell<T> already borrowed") + self.value.borrow_mut() } /// Attempts to immutably borrow the wrapped value. @@ -107,7 +103,7 @@ impl<T> DOMRefCell<T> { /// /// Panics if this is called off the script thread. pub fn try_borrow(&self) -> Result<Ref<T>, BorrowError> { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); self.value.try_borrow() } @@ -122,7 +118,7 @@ impl<T> DOMRefCell<T> { /// /// Panics if this is called off the script thread. pub fn try_borrow_mut(&self) -> Result<RefMut<T>, BorrowMutError> { - debug_assert!(thread_state::get().is_script()); + assert_in_script(); self.value.try_borrow_mut() } } |