Skip to main content

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::hash::{Hash, Hasher};
15use std::rc::{Rc, Weak};
16use std::{mem, ptr};
17
18use js::jsapi::JSTracer;
19use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
20
21use crate::JSTraceable;
22use crate::reflector::DomObject;
23use crate::root::DomRoot;
24
25/// A weak reference to a JS-managed DOM object.
26#[derive(Clone)]
27#[cfg_attr(crown, crown::unrooted_must_root_lint::allow_unrooted_interior)]
28pub struct WeakRef<T: WeakReferenceable>(Weak<T>);
29
30/// Trait implemented by weak-referenceable interfaces.
31pub trait WeakReferenceable: DomObject + Sized {
32    /// Downgrade a DOM object reference to a weak one.
33    fn downgrade(&self) -> WeakRef<Self> {
34        let rc = unsafe { Rc::from_raw(self as *const Self) };
35        let weak = WeakRef(Rc::downgrade(&rc));
36        mem::forget(rc);
37        weak
38    }
39}
40
41impl<T: WeakReferenceable> Eq for WeakRef<T> {}
42
43impl<T: WeakReferenceable> Hash for WeakRef<T> {
44    fn hash<H: Hasher>(&self, state: &mut H) {
45        self.0.as_ptr().hash(state);
46    }
47}
48
49impl<T: WeakReferenceable> WeakRef<T> {
50    /// Create a new weak reference from a `WeakReferenceable` interface instance.
51    /// This is just a convenience wrapper around `<T as WeakReferenceable>::downgrade`
52    /// to not have to import `WeakReferenceable`.
53    pub fn new(value: &T) -> Self {
54        value.downgrade()
55    }
56
57    /// DomRoot a weak reference. Returns `None` if the object was already collected.
58    pub fn root(&self) -> Option<DomRoot<T>> {
59        self.0.upgrade().map(|x| DomRoot::from_ref(&*x))
60    }
61
62    /// Return whether the weakly-referenced object is still alive.
63    pub fn is_alive(&self) -> bool {
64        self.0.strong_count() > 0
65    }
66}
67
68impl<T: WeakReferenceable> MallocSizeOf for WeakRef<T> {
69    fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
70        0
71    }
72}
73
74impl<T: WeakReferenceable> PartialEq for WeakRef<T> {
75    fn eq(&self, other: &Self) -> bool {
76        self.0.ptr_eq(&other.0)
77    }
78}
79
80impl<T: WeakReferenceable> PartialEq<T> for WeakRef<T> {
81    fn eq(&self, other: &T) -> bool {
82        match self.0.upgrade() {
83            Some(ptr) => ptr::eq(Rc::as_ptr(&ptr), other),
84            None => false,
85        }
86    }
87}
88
89unsafe impl<T: WeakReferenceable> JSTraceable for WeakRef<T> {
90    unsafe fn trace(&self, _: *mut JSTracer) {
91        // Do nothing.
92    }
93}