script_bindings/
weakref.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Weak-referenceable JS-managed DOM objects.
6//!
7//! IDL interfaces marked as `weakReferenceable` in `Bindings.conf`
8//! automatically implement the `WeakReferenceable` trait in codegen.
9//! The instance object is responsible for setting `None` in its own
10//! own `WeakBox` when it is collected, through the `DOM_WEAK_SLOT`
11//! slot. When all associated `WeakRef` values are dropped, the
12//! `WeakBox` itself is dropped too.
13
14use std::cell::Cell;
15use std::ops::Drop;
16use std::{mem, ptr};
17
18use js::glue::JS_GetReservedSlot;
19use js::jsapi::{JS_SetReservedSlot, JSTracer};
20use js::jsval::{PrivateValue, UndefinedValue};
21use libc::c_void;
22use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
23
24use crate::JSTraceable;
25use crate::reflector::DomObject;
26use crate::root::DomRoot;
27
28/// The index of the slot wherein a pointer to the weak holder cell is
29/// stored for weak-referenceable bindings. We use slot 1 for holding it,
30/// this is unsafe for globals, we disallow weak-referenceable globals
31/// directly in codegen.
32pub(crate) const DOM_WEAK_SLOT: u32 = 1;
33
34/// A weak reference to a JS-managed DOM object.
35#[cfg_attr(crown, crown::unrooted_must_root_lint::allow_unrooted_interior)]
36pub struct WeakRef<T: WeakReferenceable> {
37    ptr: ptr::NonNull<WeakBox<T>>,
38}
39
40/// The inner box of weak references, public for the finalization in codegen.
41#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
42pub struct WeakBox<T: WeakReferenceable> {
43    /// The reference count. When it reaches zero, the `value` field should
44    /// have already been set to `None`. The pointee contributes one to the count.
45    pub count: Cell<usize>,
46    /// The pointer to the JS-managed object, set to None when it is collected.
47    pub value: Cell<Option<ptr::NonNull<T>>>,
48}
49
50/// Trait implemented by weak-referenceable interfaces.
51pub trait WeakReferenceable: DomObject + Sized {
52    /// Downgrade a DOM object reference to a weak one.
53    fn downgrade(&self) -> WeakRef<Self> {
54        unsafe {
55            let object = self.reflector().get_jsobject().get();
56            let mut slot = UndefinedValue();
57            JS_GetReservedSlot(object, DOM_WEAK_SLOT, &mut slot);
58            let mut ptr = slot.to_private() as *mut WeakBox<Self>;
59            if ptr.is_null() {
60                trace!("Creating new WeakBox holder for {:p}.", self);
61                ptr = Box::into_raw(Box::new(WeakBox {
62                    count: Cell::new(1),
63                    value: Cell::new(Some(ptr::NonNull::from(self))),
64                }));
65                let val = PrivateValue(ptr as *const c_void);
66                JS_SetReservedSlot(object, DOM_WEAK_SLOT, &val);
67            }
68            let box_ = &*ptr;
69            assert!(box_.value.get().is_some());
70            let new_count = box_.count.get() + 1;
71            trace!(
72                "Incrementing WeakBox refcount for {:p} to {}.",
73                self, new_count
74            );
75            box_.count.set(new_count);
76            WeakRef {
77                ptr: ptr::NonNull::new_unchecked(ptr),
78            }
79        }
80    }
81}
82
83impl<T: WeakReferenceable> WeakRef<T> {
84    /// Create a new weak reference from a `WeakReferenceable` interface instance.
85    /// This is just a convenience wrapper around `<T as WeakReferenceable>::downgrade`
86    /// to not have to import `WeakReferenceable`.
87    pub fn new(value: &T) -> Self {
88        value.downgrade()
89    }
90
91    /// DomRoot a weak reference. Returns `None` if the object was already collected.
92    pub fn root(&self) -> Option<DomRoot<T>> {
93        unsafe { &*self.ptr.as_ptr() }
94            .value
95            .get()
96            .map(|ptr| unsafe { DomRoot::from_ref(&*ptr.as_ptr()) })
97    }
98
99    /// Return whether the weakly-referenced object is still alive.
100    pub fn is_alive(&self) -> bool {
101        unsafe { &*self.ptr.as_ptr() }.value.get().is_some()
102    }
103}
104
105impl<T: WeakReferenceable> Clone for WeakRef<T> {
106    fn clone(&self) -> WeakRef<T> {
107        unsafe {
108            let box_ = &*self.ptr.as_ptr();
109            let new_count = box_.count.get() + 1;
110            box_.count.set(new_count);
111            WeakRef { ptr: self.ptr }
112        }
113    }
114}
115
116impl<T: WeakReferenceable> MallocSizeOf for WeakRef<T> {
117    fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
118        0
119    }
120}
121
122impl<T: WeakReferenceable> PartialEq for WeakRef<T> {
123    fn eq(&self, other: &Self) -> bool {
124        unsafe {
125            (*self.ptr.as_ptr()).value.get().map(ptr::NonNull::as_ptr) ==
126                (*other.ptr.as_ptr()).value.get().map(ptr::NonNull::as_ptr)
127        }
128    }
129}
130
131impl<T: WeakReferenceable> PartialEq<T> for WeakRef<T> {
132    fn eq(&self, other: &T) -> bool {
133        unsafe {
134            match self.ptr.as_ref().value.get() {
135                Some(ptr) => ptr::eq(ptr.as_ptr(), other),
136                None => false,
137            }
138        }
139    }
140}
141
142unsafe impl<T: WeakReferenceable> JSTraceable for WeakRef<T> {
143    unsafe fn trace(&self, _: *mut JSTracer) {
144        // Do nothing.
145    }
146}
147
148impl<T: WeakReferenceable> Drop for WeakRef<T> {
149    fn drop(&mut self) {
150        unsafe {
151            let (count, value) = {
152                let weak_box = &*self.ptr.as_ptr();
153                assert!(weak_box.count.get() > 0);
154                let count = weak_box.count.get() - 1;
155                weak_box.count.set(count);
156                (count, weak_box.value.get())
157            };
158            if count == 0 {
159                assert!(value.is_none());
160                mem::drop(Box::from_raw(self.ptr.as_ptr()));
161            }
162        }
163    }
164}