mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Move DOMRefCell to style.
This commit is contained in:
parent
ec723057b2
commit
c87180a2fb
5 changed files with 16 additions and 17 deletions
|
@ -1,139 +0,0 @@
|
|||
/* 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/. */
|
||||
|
||||
//! A shareable mutable container for the DOM.
|
||||
|
||||
use dom::bindings::trace::JSTraceable;
|
||||
use js::jsapi::JSTracer;
|
||||
use style::refcell::{BorrowError, BorrowMutError, Ref, RefCell, RefMut};
|
||||
use style::thread_state;
|
||||
use style::thread_state::SCRIPT;
|
||||
|
||||
/// A mutable field in the DOM.
|
||||
///
|
||||
/// This extends the API of `core::cell::RefCell` to allow unsafe access in
|
||||
/// certain situations, with dynamic checking in debug builds.
|
||||
#[derive(Clone, HeapSizeOf)]
|
||||
pub struct DOMRefCell<T> {
|
||||
value: RefCell<T>,
|
||||
}
|
||||
|
||||
// Functionality specific to Servo's `DOMRefCell` type
|
||||
// ===================================================
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
/// 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(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()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: JSTraceable> JSTraceable for DOMRefCell<T> {
|
||||
fn trace(&self, trc: *mut JSTracer) {
|
||||
unsafe {
|
||||
(*self).borrow_for_gc_trace().trace(trc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Functionality duplicated with `core::cell::RefCell`
|
||||
// ===================================================
|
||||
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
|
||||
/// immutable borrows can be taken out at the same time.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if this is called off the script thread.
|
||||
///
|
||||
/// Panics if the value is currently mutably borrowed.
|
||||
pub fn borrow(&self) -> Ref<T> {
|
||||
self.try_borrow().expect("DOMRefCell<T> already mutably borrowed")
|
||||
}
|
||||
|
||||
/// Mutably borrows the wrapped value.
|
||||
///
|
||||
/// The borrow lasts until the returned `RefMut` exits scope. The value
|
||||
/// cannot be borrowed while this borrow is active.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if this is called off the script thread.
|
||||
///
|
||||
/// Panics if the value is currently borrowed.
|
||||
pub fn borrow_mut(&self) -> RefMut<T> {
|
||||
self.try_borrow_mut().expect("DOMRefCell<T> already borrowed")
|
||||
}
|
||||
|
||||
/// Attempts to immutably borrow the wrapped value.
|
||||
///
|
||||
/// The borrow lasts until the returned `Ref` exits scope. Multiple
|
||||
/// immutable borrows can be taken out at the same time.
|
||||
///
|
||||
/// Returns `None` if the value is currently mutably borrowed.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if this is called off the script thread.
|
||||
pub fn try_borrow(&self) -> Result<Ref<T>, BorrowError<T>> {
|
||||
debug_assert!(thread_state::get().is_script());
|
||||
self.value.try_borrow()
|
||||
}
|
||||
|
||||
/// Mutably borrows the wrapped value.
|
||||
///
|
||||
/// The borrow lasts until the returned `RefMut` exits scope. The value
|
||||
/// cannot be borrowed while this borrow is active.
|
||||
///
|
||||
/// Returns `None` if the value is currently borrowed.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if this is called off the script thread.
|
||||
pub fn try_borrow_mut(&self) -> Result<RefMut<T>, BorrowMutError<T>> {
|
||||
debug_assert!(thread_state::get().is_script());
|
||||
self.value.try_borrow_mut()
|
||||
}
|
||||
}
|
|
@ -128,8 +128,9 @@
|
|||
//! return `Err()` from the method with the appropriate [error value]
|
||||
//! (error/enum.Error.html).
|
||||
|
||||
pub use style::domrefcell as cell;
|
||||
|
||||
pub mod callback;
|
||||
pub mod cell;
|
||||
pub mod conversions;
|
||||
pub mod error;
|
||||
pub mod global;
|
||||
|
|
|
@ -88,6 +88,7 @@ use std::sync::mpsc::{Receiver, Sender};
|
|||
use std::time::SystemTime;
|
||||
use string_cache::{Atom, Namespace, QualName};
|
||||
use style::attr::{AttrIdentifier, AttrValue, LengthOrPercentageOrAuto};
|
||||
use style::domrefcell::DOMRefCell;
|
||||
use style::element_state::*;
|
||||
use style::properties::PropertyDeclarationBlock;
|
||||
use style::selector_impl::{PseudoElement, ElementSnapshot};
|
||||
|
@ -172,6 +173,13 @@ impl<T: JSTraceable> JSTraceable for UnsafeCell<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: JSTraceable> JSTraceable for DOMRefCell<T> {
|
||||
fn trace(&self, trc: *mut JSTracer) {
|
||||
unsafe {
|
||||
(*self).borrow_for_gc_trace().trace(trc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl JSTraceable for Heap<*mut JSObject> {
|
||||
fn trace(&self, trc: *mut JSTracer) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue