use std::cell::{Cell, OnceCell, UnsafeCell};
use std::default::Default;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::ops::Deref;
use std::{mem, ptr};
use js::jsapi::{JSObject, JSTracer};
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use script_layout_interface::TrustedNodeAddress;
use style::thread_state;
use crate::dom::bindings::conversions::DerivedFrom;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{DomObject, MutDomObject, Reflector};
use crate::dom::bindings::trace::{trace_reflector, JSTraceable};
use crate::dom::node::Node;
#[allow(crown::unrooted_must_root)]
#[crown::unrooted_must_root_lint::allow_unrooted_interior]
pub struct Root<T: StableTraceObject> {
value: T,
root_list: *const RootCollection,
}
impl<T> Root<T>
where
T: StableTraceObject + 'static,
{
#[allow(crown::unrooted_must_root)]
pub unsafe fn new(value: T) -> Self {
unsafe fn add_to_root_list(object: *const dyn JSTraceable) -> *const RootCollection {
assert_in_script();
STACK_ROOTS.with(|root_list| {
let root_list = &*root_list.get().unwrap();
root_list.root(object);
root_list
})
}
let root_list = add_to_root_list(value.stable_trace_object());
Root { value, root_list }
}
}
pub unsafe trait StableTraceObject {
fn stable_trace_object(&self) -> *const dyn JSTraceable;
}
unsafe impl<T> StableTraceObject for Dom<T>
where
T: DomObject,
{
fn stable_trace_object(&self) -> *const dyn JSTraceable {
#[allow(crown::unrooted_must_root)]
struct ReflectorStackRoot(Reflector);
unsafe impl JSTraceable for ReflectorStackRoot {
unsafe fn trace(&self, tracer: *mut JSTracer) {
trace_reflector(tracer, "on stack", &self.0);
}
}
unsafe { &*(self.reflector() as *const Reflector as *const ReflectorStackRoot) }
}
}
unsafe impl<T> StableTraceObject for MaybeUnreflectedDom<T>
where
T: DomObject,
{
fn stable_trace_object(&self) -> *const dyn JSTraceable {
#[allow(crown::unrooted_must_root)]
struct MaybeUnreflectedStackRoot<T>(T);
unsafe impl<T> JSTraceable for MaybeUnreflectedStackRoot<T>
where
T: DomObject,
{
unsafe fn trace(&self, tracer: *mut JSTracer) {
if self.0.reflector().get_jsobject().is_null() {
self.0.trace(tracer);
} else {
trace_reflector(tracer, "on stack", self.0.reflector());
}
}
}
unsafe { &*(self.ptr.as_ptr() as *const T as *const MaybeUnreflectedStackRoot<T>) }
}
}
impl<T> Deref for Root<T>
where
T: Deref + StableTraceObject,
{
type Target = <T as Deref>::Target;
fn deref(&self) -> &Self::Target {
assert_in_script();
&self.value
}
}
impl<T> Drop for Root<T>
where
T: StableTraceObject,
{
fn drop(&mut self) {
unsafe {
(*self.root_list).unroot(self.value.stable_trace_object());
}
}
}
pub type DomRoot<T> = Root<Dom<T>>;
impl<T: Castable> DomRoot<T> {
pub fn upcast<U>(root: DomRoot<T>) -> DomRoot<U>
where
U: Castable,
T: DerivedFrom<U>,
{
unsafe { mem::transmute(root) }
}
pub fn downcast<U>(root: DomRoot<T>) -> Option<DomRoot<U>>
where
U: DerivedFrom<T>,
{
if root.is::<U>() {
Some(unsafe { mem::transmute(root) })
} else {
None
}
}
}
impl<T: DomObject> DomRoot<T> {
pub fn from_ref(unrooted: &T) -> DomRoot<T> {
unsafe { DomRoot::new(Dom::from_ref(unrooted)) }
}
}
impl<T> MallocSizeOf for DomRoot<T>
where
T: DomObject + MallocSizeOf,
{
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
(**self).size_of(ops)
}
}
impl<T> PartialEq for DomRoot<T>
where
T: DomObject,
{
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
impl<T> Clone for DomRoot<T>
where
T: DomObject,
{
fn clone(&self) -> DomRoot<T> {
DomRoot::from_ref(self)
}
}
unsafe impl<T> JSTraceable for DomRoot<T>
where
T: DomObject,
{
unsafe fn trace(&self, _: *mut JSTracer) {
}
}
pub struct RootCollection {
roots: UnsafeCell<Vec<*const dyn JSTraceable>>,
}
thread_local!(static STACK_ROOTS: Cell<Option<*const RootCollection>> = Cell::new(None));
pub struct ThreadLocalStackRoots<'a>(PhantomData<&'a u32>);
impl<'a> ThreadLocalStackRoots<'a> {
pub fn new(roots: &'a RootCollection) -> Self {
STACK_ROOTS.with(|r| r.set(Some(roots)));
ThreadLocalStackRoots(PhantomData)
}
}
impl<'a> Drop for ThreadLocalStackRoots<'a> {
fn drop(&mut self) {
STACK_ROOTS.with(|r| r.set(None));
}
}
impl RootCollection {
pub fn new() -> RootCollection {
assert_in_script();
RootCollection {
roots: UnsafeCell::new(vec![]),
}
}
unsafe fn root(&self, object: *const dyn JSTraceable) {
assert_in_script();
(*self.roots.get()).push(object);
}
unsafe fn unroot(&self, object: *const dyn JSTraceable) {
assert_in_script();
let roots = &mut *self.roots.get();
match roots.iter().rposition(|r| std::ptr::eq(*r, object)) {
Some(idx) => {
roots.remove(idx);
},
None => panic!("Can't remove a root that was never rooted!"),
}
}
}
pub unsafe fn trace_roots(tracer: *mut JSTracer) {
debug!("tracing stack roots");
STACK_ROOTS.with(|collection| {
let collection = &*(*collection.get().unwrap()).roots.get();
for root in collection {
(**root).trace(tracer);
}
});
}
pub trait DomSlice<T>
where
T: JSTraceable + DomObject,
{
fn r(&self) -> &[&T];
}
impl<T> DomSlice<T> for [Dom<T>]
where
T: JSTraceable + DomObject,
{
#[inline]
fn r(&self) -> &[&T] {
let _ = mem::transmute::<Dom<T>, &T>;
unsafe { &*(self as *const [Dom<T>] as *const [&T]) }
}
}
#[crown::unrooted_must_root_lint::must_root]
pub struct Dom<T> {
ptr: ptr::NonNull<T>,
}
impl<T> MallocSizeOf for Dom<T> {
fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
0
}
}
impl<T> Dom<T> {
pub unsafe fn to_layout(&self) -> LayoutDom<T> {
assert_in_layout();
LayoutDom {
value: self.ptr.as_ref(),
}
}
}
impl<T: DomObject> Dom<T> {
#[allow(crown::unrooted_must_root)]
pub fn from_ref(obj: &T) -> Dom<T> {
assert_in_script();
Dom {
ptr: ptr::NonNull::from(obj),
}
}
}
impl<T: DomObject> Deref for Dom<T> {
type Target = T;
fn deref(&self) -> &T {
assert_in_script();
unsafe { &*self.ptr.as_ptr() }
}
}
unsafe impl<T: DomObject> JSTraceable for Dom<T> {
unsafe fn trace(&self, trc: *mut JSTracer) {
let trace_string;
let trace_info = if cfg!(debug_assertions) {
trace_string = format!("for {} on heap", ::std::any::type_name::<T>());
&trace_string[..]
} else {
"for DOM object on heap"
};
trace_reflector(trc, trace_info, (*self.ptr.as_ptr()).reflector());
}
}
#[crown::unrooted_must_root_lint::must_root]
pub struct MaybeUnreflectedDom<T> {
ptr: ptr::NonNull<T>,
}
impl<T> MaybeUnreflectedDom<T>
where
T: DomObject,
{
#[allow(crown::unrooted_must_root)]
pub unsafe fn from_box(value: Box<T>) -> Self {
Self {
ptr: Box::leak(value).into(),
}
}
}
impl<T> Root<MaybeUnreflectedDom<T>>
where
T: DomObject,
{
pub fn as_ptr(&self) -> *const T {
self.value.ptr.as_ptr()
}
}
impl<T> Root<MaybeUnreflectedDom<T>>
where
T: MutDomObject,
{
pub unsafe fn reflect_with(self, obj: *mut JSObject) -> DomRoot<T> {
let ptr = self.as_ptr();
drop(self);
let root = DomRoot::from_ref(&*ptr);
root.init_reflector(obj);
root
}
}
#[crown::unrooted_must_root_lint::allow_unrooted_interior]
pub struct LayoutDom<'dom, T> {
value: &'dom T,
}
impl<'dom, T> LayoutDom<'dom, T>
where
T: Castable,
{
pub fn upcast<U>(&self) -> LayoutDom<'dom, U>
where
U: Castable,
T: DerivedFrom<U>,
{
assert_in_layout();
LayoutDom {
value: self.value.upcast::<U>(),
}
}
pub fn downcast<U>(&self) -> Option<LayoutDom<'dom, U>>
where
U: DerivedFrom<T>,
{
assert_in_layout();
self.value.downcast::<U>().map(|value| LayoutDom { value })
}
pub fn is<U>(&self) -> bool
where
U: DerivedFrom<T>,
{
assert_in_layout();
self.value.is::<U>()
}
}
impl<T> LayoutDom<'_, T>
where
T: DomObject,
{
pub unsafe fn get_jsobject(&self) -> *mut JSObject {
assert_in_layout();
self.value.reflector().get_jsobject().get()
}
}
impl<T> Copy for LayoutDom<'_, T> {}
impl<T> PartialEq for Dom<T> {
fn eq(&self, other: &Dom<T>) -> bool {
self.ptr.as_ptr() == other.ptr.as_ptr()
}
}
impl<'a, T: DomObject> PartialEq<&'a T> for Dom<T> {
fn eq(&self, other: &&'a T) -> bool {
*self == Dom::from_ref(*other)
}
}
impl<T> Eq for Dom<T> {}
impl<T> PartialEq for LayoutDom<'_, T> {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self.value, other.value)
}
}
impl<T> Eq for LayoutDom<'_, T> {}
impl<T> Hash for Dom<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.ptr.as_ptr().hash(state)
}
}
impl<T> Hash for LayoutDom<'_, T> {
fn hash<H: Hasher>(&self, state: &mut H) {
(self.value as *const T).hash(state)
}
}
impl<T> Clone for Dom<T> {
#[inline]
#[allow(crown::unrooted_must_root)]
fn clone(&self) -> Self {
assert_in_script();
Dom { ptr: self.ptr }
}
}
impl<T> Clone for LayoutDom<'_, T> {
#[inline]
#[allow(clippy::non_canonical_clone_impl)]
fn clone(&self) -> Self {
assert_in_layout();
*self
}
}
impl LayoutDom<'_, Node> {
pub unsafe fn from_trusted_node_address(inner: TrustedNodeAddress) -> Self {
assert_in_layout();
let TrustedNodeAddress(addr) = inner;
LayoutDom {
value: &*(addr as *const Node),
}
}
}
#[crown::unrooted_must_root_lint::must_root]
#[derive(JSTraceable)]
pub struct MutDom<T: DomObject> {
val: UnsafeCell<Dom<T>>,
}
impl<T: DomObject> MutDom<T> {
pub fn new(initial: &T) -> MutDom<T> {
assert_in_script();
MutDom {
val: UnsafeCell::new(Dom::from_ref(initial)),
}
}
pub fn set(&self, val: &T) {
assert_in_script();
unsafe {
*self.val.get() = Dom::from_ref(val);
}
}
pub fn get(&self) -> DomRoot<T> {
assert_in_script();
unsafe { DomRoot::from_ref(&*ptr::read(self.val.get())) }
}
}
impl<T: DomObject> MallocSizeOf for MutDom<T> {
fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
0
}
}
impl<T: DomObject> PartialEq for MutDom<T> {
fn eq(&self, other: &Self) -> bool {
unsafe { *self.val.get() == *other.val.get() }
}
}
impl<T: DomObject + PartialEq> PartialEq<T> for MutDom<T> {
fn eq(&self, other: &T) -> bool {
unsafe { **self.val.get() == *other }
}
}
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());
}
#[crown::unrooted_must_root_lint::must_root]
#[derive(JSTraceable)]
pub struct MutNullableDom<T: DomObject> {
ptr: UnsafeCell<Option<Dom<T>>>,
}
impl<T: DomObject> MutNullableDom<T> {
pub fn new(initial: Option<&T>) -> MutNullableDom<T> {
assert_in_script();
MutNullableDom {
ptr: UnsafeCell::new(initial.map(Dom::from_ref)),
}
}
pub fn or_init<F>(&self, cb: F) -> DomRoot<T>
where
F: FnOnce() -> DomRoot<T>,
{
assert_in_script();
match self.get() {
Some(inner) => inner,
None => {
let inner = cb();
self.set(Some(&inner));
inner
},
}
}
#[allow(crown::unrooted_must_root)]
pub unsafe fn get_inner_as_layout(&self) -> Option<LayoutDom<T>> {
assert_in_layout();
(*self.ptr.get()).as_ref().map(|js| js.to_layout())
}
#[allow(crown::unrooted_must_root)]
pub fn get(&self) -> Option<DomRoot<T>> {
assert_in_script();
unsafe { ptr::read(self.ptr.get()).map(|o| DomRoot::from_ref(&*o)) }
}
pub fn set(&self, val: Option<&T>) {
assert_in_script();
unsafe {
*self.ptr.get() = val.map(|p| Dom::from_ref(p));
}
}
pub fn take(&self) -> Option<DomRoot<T>> {
let value = self.get();
self.set(None);
value
}
}
impl<T: DomObject> PartialEq for MutNullableDom<T> {
fn eq(&self, other: &Self) -> bool {
unsafe { *self.ptr.get() == *other.ptr.get() }
}
}
impl<'a, T: DomObject> PartialEq<Option<&'a T>> for MutNullableDom<T> {
fn eq(&self, other: &Option<&T>) -> bool {
unsafe { *self.ptr.get() == other.map(Dom::from_ref) }
}
}
impl<T: DomObject> Default for MutNullableDom<T> {
#[allow(crown::unrooted_must_root)]
fn default() -> MutNullableDom<T> {
assert_in_script();
MutNullableDom {
ptr: UnsafeCell::new(None),
}
}
}
impl<T: DomObject> MallocSizeOf for MutNullableDom<T> {
fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
0
}
}
#[crown::unrooted_must_root_lint::must_root]
pub struct DomOnceCell<T: DomObject> {
ptr: OnceCell<Dom<T>>,
}
impl<T> DomOnceCell<T>
where
T: DomObject,
{
#[allow(crown::unrooted_must_root)]
pub fn init_once<F>(&self, cb: F) -> &T
where
F: FnOnce() -> DomRoot<T>,
{
assert_in_script();
self.ptr.get_or_init(|| Dom::from_ref(&cb()))
}
}
impl<T: DomObject> Default for DomOnceCell<T> {
#[allow(crown::unrooted_must_root)]
fn default() -> DomOnceCell<T> {
assert_in_script();
DomOnceCell {
ptr: OnceCell::new(),
}
}
}
impl<T: DomObject> MallocSizeOf for DomOnceCell<T> {
fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
0
}
}
#[allow(crown::unrooted_must_root)]
unsafe impl<T: DomObject> JSTraceable for DomOnceCell<T> {
unsafe fn trace(&self, trc: *mut JSTracer) {
if let Some(ptr) = self.ptr.get() {
ptr.trace(trc);
}
}
}
impl<'dom, T> LayoutDom<'dom, T>
where
T: 'dom + DomObject,
{
pub fn unsafe_get(self) -> &'dom T {
assert_in_layout();
self.value
}
pub unsafe fn to_layout_slice(slice: &'dom [Dom<T>]) -> &'dom [LayoutDom<'dom, T>] {
let _ = mem::transmute::<Dom<T>, LayoutDom<T>>;
&*(slice as *const [Dom<T>] as *const [LayoutDom<T>])
}
}