1use std::cell::UnsafeCell;
6use std::hash::{Hash, Hasher};
7use std::ops::Deref;
8use std::rc::Rc;
9use std::{fmt, mem, ptr};
10
11use js::gc::{Handle, Traceable as JSTraceable};
12use js::jsapi::{Heap, JSObject, JSTracer};
13use js::rust::GCMethods;
14use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
15
16use crate::assert::assert_in_script;
17use crate::conversions::DerivedFrom;
18use crate::inheritance::Castable;
19use crate::reflector::{DomObject, MutDomObject, Reflector};
20use crate::trace::trace_reflector;
21
22#[cfg_attr(crown, crown::unrooted_must_root_lint::allow_unrooted_interior)]
24pub struct Root<T: StableTraceObject> {
25 value: T,
27 root_list: *const RootCollection,
29}
30
31impl<T> Root<T>
32where
33 T: StableTraceObject + 'static,
34{
35 pub unsafe fn new(value: T) -> Self {
41 unsafe fn add_to_root_list(object: *const dyn JSTraceable) -> *const RootCollection {
42 assert_in_script();
43 STACK_ROOTS.with(|root_list| {
44 unsafe { root_list.root(object) };
45 root_list as *const _
46 })
47 }
48
49 let root_list = unsafe { add_to_root_list(value.stable_trace_object()) };
50 Root { value, root_list }
51 }
52}
53
54pub unsafe trait StableTraceObject {
65 fn stable_trace_object(&self) -> *const dyn JSTraceable;
68}
69
70unsafe impl<T> StableTraceObject for Dom<T>
71where
72 T: DomObject,
73{
74 fn stable_trace_object(&self) -> *const dyn JSTraceable {
75 #[cfg_attr(crown, expect(crown::unrooted_must_root))]
79 struct ReflectorStackRoot<T>(Reflector<T>);
80 unsafe impl<T> JSTraceable for ReflectorStackRoot<T> {
81 unsafe fn trace(&self, tracer: *mut JSTracer) {
82 unsafe { trace_reflector(tracer, "on stack", &self.0) };
83 }
84 }
85 unsafe {
86 &*(self.reflector() as *const Reflector<T::ReflectorType>
87 as *const ReflectorStackRoot<T::ReflectorType>)
88 }
89 }
90}
91
92unsafe impl<T> StableTraceObject for MaybeUnreflectedDom<T>
93where
94 T: DomObject,
95{
96 fn stable_trace_object(&self) -> *const dyn JSTraceable {
97 struct MaybeUnreflectedStackRoot<T>(T);
101 unsafe impl<T> JSTraceable for MaybeUnreflectedStackRoot<T>
102 where
103 T: DomObject,
104 {
105 unsafe fn trace(&self, tracer: *mut JSTracer) {
106 if self.0.reflector().get_jsobject().is_null() {
107 unsafe { self.0.trace(tracer) };
108 } else {
109 unsafe { trace_reflector(tracer, "on stack", self.0.reflector()) };
110 }
111 }
112 }
113 unsafe { &*(self.ptr.as_ptr() as *const T as *const MaybeUnreflectedStackRoot<T>) }
114 }
115}
116
117impl<T> Deref for Root<T>
118where
119 T: Deref + StableTraceObject,
120{
121 type Target = <T as Deref>::Target;
122
123 fn deref(&self) -> &Self::Target {
124 assert_in_script();
125 &self.value
126 }
127}
128
129impl<T> Drop for Root<T>
130where
131 T: StableTraceObject,
132{
133 fn drop(&mut self) {
134 unsafe {
135 (*self.root_list).unroot(self.value.stable_trace_object());
136 }
137 }
138}
139
140impl<T: fmt::Debug + StableTraceObject> fmt::Debug for Root<T> {
141 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
142 self.value.fmt(f)
143 }
144}
145
146impl<T: fmt::Debug + DomObject> fmt::Debug for Dom<T> {
147 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
148 (**self).fmt(f)
149 }
150}
151
152#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
160#[repr(transparent)]
161pub struct Dom<T> {
162 ptr: ptr::NonNull<T>,
163}
164
165impl<T> MallocSizeOf for Dom<T> {
168 fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
169 0
170 }
171}
172
173impl<T> PartialEq for Dom<T> {
175 fn eq(&self, other: &Dom<T>) -> bool {
176 self.ptr.as_ptr() == other.ptr.as_ptr()
177 }
178}
179
180impl<'a, T: DomObject> PartialEq<&'a T> for Dom<T> {
182 fn eq(&self, other: &&'a T) -> bool {
183 *self == Dom::from_ref(*other)
184 }
185}
186
187impl<T> Eq for Dom<T> {}
188
189impl<T> Hash for Dom<T> {
191 fn hash<H: Hasher>(&self, state: &mut H) {
192 self.ptr.as_ptr().hash(state)
193 }
194}
195
196impl<T> Clone for Dom<T> {
197 #[inline]
198 fn clone(&self) -> Self {
199 assert_in_script();
200 Dom { ptr: self.ptr }
201 }
202}
203
204impl<T: DomObject> Dom<T> {
205 pub fn from_ref(obj: &T) -> Dom<T> {
207 assert_in_script();
208 Dom {
209 ptr: ptr::NonNull::from(obj),
210 }
211 }
212
213 pub fn as_rooted(&self) -> DomRoot<T> {
215 DomRoot::from_ref(self)
216 }
217
218 pub fn as_ptr(&self) -> *const T {
219 self.ptr.as_ptr()
220 }
221}
222
223impl<T: DomObject> Deref for Dom<T> {
224 type Target = T;
225
226 fn deref(&self) -> &T {
227 assert_in_script();
228 unsafe { &*self.ptr.as_ptr() }
231 }
232}
233
234unsafe impl<T: DomObject> JSTraceable for Dom<T> {
235 unsafe fn trace(&self, tracer: *mut JSTracer) {
236 let trace_info = if cfg!(debug_assertions) {
237 std::any::type_name::<T>()
238 } else {
239 "DOM object on heap"
240 };
241 unsafe {
242 trace_reflector(tracer, trace_info, (*self.ptr.as_ptr()).reflector());
243 }
244 }
245}
246
247#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
249pub struct MaybeUnreflectedDom<T> {
250 ptr: ptr::NonNull<T>,
251}
252
253impl<T> MaybeUnreflectedDom<T>
254where
255 T: DomObject,
256{
257 pub unsafe fn from_box(value: Box<T>) -> Self {
262 Self {
263 ptr: Box::leak(value).into(),
264 }
265 }
266
267 pub unsafe fn from_rc(value: Rc<T>) -> Self {
272 Self {
273 ptr: ptr::NonNull::new(Rc::into_raw(value) as *mut T).unwrap(),
274 }
275 }
276}
277
278impl<T> Root<MaybeUnreflectedDom<T>>
279where
280 T: DomObject,
281{
282 pub fn as_ptr(&self) -> *const T {
283 self.value.ptr.as_ptr()
284 }
285}
286
287impl<T> Root<MaybeUnreflectedDom<T>>
288where
289 T: MutDomObject,
290{
291 pub unsafe fn reflect_with(self, obj: *mut JSObject) -> DomRoot<T> {
296 let ptr = self.as_ptr();
297 drop(self);
298 let root = DomRoot::from_ref(unsafe { &*ptr });
299 unsafe { root.init_reflector::<T>(obj) };
300 root
301 }
302}
303
304pub type DomRoot<T> = Root<Dom<T>>;
306
307impl<T: Castable> DomRoot<T> {
308 pub fn upcast<U>(root: DomRoot<T>) -> DomRoot<U>
310 where
311 U: Castable,
312 T: DerivedFrom<U>,
313 {
314 unsafe { mem::transmute::<DomRoot<T>, DomRoot<U>>(root) }
315 }
316
317 pub fn downcast<U>(root: DomRoot<T>) -> Option<DomRoot<U>>
319 where
320 U: DerivedFrom<T>,
321 {
322 if root.is::<U>() {
323 Some(unsafe { mem::transmute::<DomRoot<T>, DomRoot<U>>(root) })
324 } else {
325 None
326 }
327 }
328}
329
330impl<T: DomObject> DomRoot<T> {
331 pub fn from_ref(unrooted: &T) -> DomRoot<T> {
333 unsafe { DomRoot::new(Dom::from_ref(unrooted)) }
334 }
335
336 pub fn as_traced(&self) -> Dom<T> {
343 Dom::from_ref(self)
344 }
345}
346
347impl<T> MallocSizeOf for DomRoot<T>
348where
349 T: DomObject + MallocSizeOf,
350{
351 fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
352 0
353 }
354}
355
356impl<T> PartialEq for DomRoot<T>
357where
358 T: DomObject,
359{
360 fn eq(&self, other: &Self) -> bool {
361 self.value == other.value
362 }
363}
364
365impl<T: DomObject> Eq for DomRoot<T> {}
366
367impl<T: DomObject> Hash for DomRoot<T> {
368 fn hash<H: Hasher>(&self, state: &mut H) {
369 self.value.hash(state);
370 }
371}
372
373impl<T> Clone for DomRoot<T>
374where
375 T: DomObject,
376{
377 fn clone(&self) -> DomRoot<T> {
378 DomRoot::from_ref(self)
379 }
380}
381
382unsafe impl<T> JSTraceable for DomRoot<T>
383where
384 T: DomObject,
385{
386 unsafe fn trace(&self, _: *mut JSTracer) {
387 }
389}
390
391pub struct RootCollection {
398 roots: UnsafeCell<Vec<*const dyn JSTraceable>>,
399}
400
401impl RootCollection {
402 #[expect(clippy::new_without_default)]
404 pub const fn new() -> RootCollection {
405 RootCollection {
406 roots: UnsafeCell::new(vec![]),
407 }
408 }
409
410 unsafe fn root(&self, object: *const dyn JSTraceable) {
412 assert_in_script();
413 unsafe { (*self.roots.get()).push(object) };
414 }
415
416 unsafe fn unroot(&self, object: *const dyn JSTraceable) {
418 assert_in_script();
419 let roots = unsafe { &mut *self.roots.get() };
420 match roots
421 .iter()
422 .rposition(|r| std::ptr::addr_eq(*r as *const (), object as *const ()))
423 {
424 Some(idx) => {
425 unsafe {
429 let len = roots.len() - 1;
430 if len != idx {
431 let base_ptr = roots.as_mut_ptr();
432 ptr::copy_nonoverlapping(base_ptr.add(len), base_ptr.add(idx), 1);
433 }
434 roots.set_len(len);
435 }
436 },
437 None => panic!("Can't remove a root that was never rooted!"),
438 }
439 }
440}
441
442thread_local!(pub static STACK_ROOTS: RootCollection = const { RootCollection::new() });
443
444pub unsafe fn trace_roots(tracer: *mut JSTracer) {
449 trace!("tracing stack roots");
450 STACK_ROOTS.with(|collection| {
451 let collection = unsafe { &*collection.roots.get() };
452 for root in collection {
453 unsafe {
454 (**root).trace(tracer);
455 }
456 }
457 });
458}
459
460pub trait DomSlice<T>
462where
463 T: JSTraceable + DomObject,
464{
465 fn r(&self) -> &[&T];
467}
468
469impl<T> DomSlice<T> for [Dom<T>]
470where
471 T: JSTraceable + DomObject,
472{
473 #[inline]
474 fn r(&self) -> &[&T] {
475 let _ = mem::transmute::<Dom<T>, &T>;
476 unsafe { &*(self as *const [Dom<T>] as *const [&T]) }
477 }
478}
479
480pub fn rooted_heap_handle<'a, T: DomObject, U: GCMethods + Copy>(
485 object: &'a T,
486 f: impl Fn(&'a T) -> &'a Heap<U>,
487) -> Handle<'a, U> {
488 unsafe { Handle::from_raw(f(object).handle()) }
492}