1use std::cell::{OnceCell, UnsafeCell};
28use std::default::Default;
29use std::hash::{Hash, Hasher};
30use std::ops::Deref;
31use std::{mem, ptr};
32
33use js::context::NoGC;
34use js::jsapi::{Heap, JSObject, JSTracer, Value};
35use js::rust::HandleValue;
36use layout_api::TrustedNodeAddress;
37use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
38use script_bindings::assert::{assert_in_layout, assert_in_script};
39use script_bindings::reflector::DomObject;
40pub(crate) use script_bindings::root::*;
41
42use crate::dom::bindings::conversions::DerivedFrom;
43use crate::dom::bindings::inheritance::Castable;
44use crate::dom::bindings::trace::JSTraceable;
45use crate::dom::node::Node;
46
47pub(crate) trait ToLayout<T> {
48 unsafe fn to_layout(&self) -> LayoutDom<'_, T>;
54}
55
56impl<T: DomObject> ToLayout<T> for Dom<T> {
57 unsafe fn to_layout(&self) -> LayoutDom<'_, T> {
58 assert_in_layout();
59 LayoutDom {
60 value: unsafe { self.as_ptr().as_ref().unwrap() },
61 }
62 }
63}
64
65#[cfg_attr(crown, crown::unrooted_must_root_lint::allow_unrooted_interior)]
68#[repr(transparent)]
69pub(crate) struct LayoutDom<'dom, T> {
70 value: &'dom T,
71}
72
73impl<'dom, T> LayoutDom<'dom, T>
74where
75 T: Castable,
76{
77 pub(crate) fn upcast<U>(&self) -> LayoutDom<'dom, U>
79 where
80 U: Castable,
81 T: DerivedFrom<U>,
82 {
83 assert_in_layout();
84 LayoutDom {
85 value: self.value.upcast::<U>(),
86 }
87 }
88
89 pub(crate) fn downcast<U>(&self) -> Option<LayoutDom<'dom, U>>
91 where
92 U: DerivedFrom<T>,
93 {
94 assert_in_layout();
95 self.value.downcast::<U>().map(|value| LayoutDom { value })
96 }
97
98 pub(crate) fn is<U>(&self) -> bool
100 where
101 U: DerivedFrom<T>,
102 {
103 assert_in_layout();
104 self.value.is::<U>()
105 }
106
107 pub(crate) unsafe fn as_ref(self) -> &'dom T {
113 self.value
114 }
115}
116
117impl<T> LayoutDom<'_, T>
118where
119 T: DomObject,
120{
121 pub(crate) unsafe fn get_jsobject(&self) -> *mut JSObject {
123 assert_in_layout();
124 self.value.reflector().get_jsobject().get()
125 }
126}
127
128impl<T> Copy for LayoutDom<'_, T> {}
129
130impl<T> PartialEq for LayoutDom<'_, T> {
131 fn eq(&self, other: &Self) -> bool {
132 std::ptr::eq(self.value, other.value)
133 }
134}
135
136impl<T> Eq for LayoutDom<'_, T> {}
137
138impl<T> Hash for LayoutDom<'_, T> {
139 fn hash<H: Hasher>(&self, state: &mut H) {
140 (self.value as *const T).hash(state)
141 }
142}
143
144#[expect(clippy::non_canonical_clone_impl)]
145impl<T> Clone for LayoutDom<'_, T> {
146 #[inline]
147 fn clone(&self) -> Self {
148 assert_in_layout();
149 *self
150 }
151}
152
153impl LayoutDom<'_, Node> {
154 pub(crate) unsafe fn from_trusted_node_address(inner: TrustedNodeAddress) -> Self {
157 assert_in_layout();
158 let TrustedNodeAddress(addr) = inner;
159 LayoutDom {
160 value: unsafe { &*(addr as *const Node) },
161 }
162 }
163}
164
165#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
171#[derive(JSTraceable)]
172pub(crate) struct MutDom<T: DomObject> {
173 val: UnsafeCell<Dom<T>>,
174}
175
176impl<T: DomObject> MutDom<T> {
177 pub(crate) fn new(initial: &T) -> MutDom<T> {
179 assert_in_script();
180 MutDom {
181 val: UnsafeCell::new(Dom::from_ref(initial)),
182 }
183 }
184
185 pub(crate) fn set(&self, val: &T) {
187 assert_in_script();
188 unsafe {
189 *self.val.get() = Dom::from_ref(val);
190 }
191 }
192
193 pub(crate) fn get(&self) -> DomRoot<T> {
195 assert_in_script();
196 unsafe { DomRoot::from_ref(&*ptr::read(self.val.get())) }
197 }
198}
199
200impl<T: DomObject> MallocSizeOf for MutDom<T> {
201 fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
202 0
204 }
205}
206
207impl<T: DomObject> PartialEq for MutDom<T> {
208 fn eq(&self, other: &Self) -> bool {
209 unsafe { *self.val.get() == *other.val.get() }
210 }
211}
212
213impl<T: DomObject + PartialEq> PartialEq<T> for MutDom<T> {
214 fn eq(&self, other: &T) -> bool {
215 unsafe { **self.val.get() == *other }
216 }
217}
218
219#[cfg_attr(crown, crown::unrooted_must_root_lint::allow_unrooted_interior)]
222pub(crate) struct UnrootedDom<'a, T: DomObject> {
223 inner: Dom<T>,
224 no_gc: &'a NoGC,
225}
226
227impl<'a, T: DomObject> UnrootedDom<'a, T> {
228 #[cfg_attr(crown, expect(crown::unrooted_must_root))]
230 pub(crate) fn from_dom(object: Dom<T>, no_gc: &'a NoGC) -> UnrootedDom<'a, T> {
231 UnrootedDom {
232 inner: object,
233 no_gc,
234 }
235 }
236}
237
238impl<'a, T: DomObject> Deref for UnrootedDom<'a, T> {
239 type Target = Dom<T>;
240
241 fn deref(&self) -> &Self::Target {
242 &self.inner
243 }
244}
245
246impl<'a, T: Castable> UnrootedDom<'a, T> {
250 pub fn upcast<U>(dom: UnrootedDom<'a, T>) -> UnrootedDom<'a, U>
252 where
253 U: Castable,
254 T: DerivedFrom<U>,
255 {
256 UnrootedDom {
257 inner: unsafe { mem::transmute::<Dom<T>, Dom<U>>(dom.inner) },
258 no_gc: dom.no_gc,
259 }
260 }
261
262 pub fn downcast<U>(dom: UnrootedDom<'a, T>) -> Option<UnrootedDom<'a, U>>
264 where
265 U: DerivedFrom<T>,
266 {
267 if dom.is::<U>() {
268 Some(UnrootedDom {
269 inner: unsafe { mem::transmute::<Dom<T>, Dom<U>>(dom.inner) },
270 no_gc: dom.no_gc,
271 })
272 } else {
273 None
274 }
275 }
276}
277
278impl<'a, T: DomObject> PartialEq<&T> for UnrootedDom<'a, T> {
279 fn eq(&self, other: &&T) -> bool {
280 self.inner == Dom::from_ref(*other)
281 }
282}
283
284#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
291#[derive(JSTraceable)]
292pub(crate) struct MutNullableDom<T: DomObject> {
293 ptr: UnsafeCell<Option<Dom<T>>>,
294}
295
296impl<T: DomObject> MutNullableDom<T> {
297 pub(crate) fn new(initial: Option<&T>) -> MutNullableDom<T> {
299 assert_in_script();
300 MutNullableDom {
301 ptr: UnsafeCell::new(initial.map(Dom::from_ref)),
302 }
303 }
304
305 pub(crate) fn or_init<F>(&self, cb: F) -> DomRoot<T>
308 where
309 F: FnOnce() -> DomRoot<T>,
310 {
311 assert_in_script();
312 match self.get() {
313 Some(inner) => inner,
314 None => {
315 let inner = cb();
316 self.set(Some(&inner));
317 inner
318 },
319 }
320 }
321
322 pub(crate) unsafe fn get_inner_as_layout(&self) -> Option<LayoutDom<'_, T>> {
325 assert_in_layout();
326 unsafe { (*self.ptr.get()).as_ref().map(|js| js.to_layout()) }
327 }
328
329 pub(crate) fn get(&self) -> Option<DomRoot<T>> {
331 assert_in_script();
332 unsafe { ptr::read(self.ptr.get()).map(|o| DomRoot::from_ref(&*o)) }
333 }
334
335 #[cfg_attr(crown, expect(crown::unrooted_must_root))]
339 pub(crate) fn get_unrooted<'a>(&self, no_gc: &'a NoGC) -> Option<UnrootedDom<'a, T>> {
340 assert_in_script();
341 let ptr = unsafe { ptr::read(self.ptr.get()) };
342 ptr.map(|o| Dom::from_ref(&*o))
343 .map(|dom| UnrootedDom { inner: dom, no_gc })
344 }
345
346 pub(crate) fn set(&self, val: Option<&T>) {
348 assert_in_script();
349 unsafe {
350 *self.ptr.get() = val.map(|p| Dom::from_ref(p));
351 }
352 }
353
354 pub(crate) fn take(&self) -> Option<DomRoot<T>> {
356 let value = self.get();
357 self.set(None);
358 value
359 }
360
361 pub(crate) fn clear(&self) {
363 self.set(None)
364 }
365
366 pub(crate) fn if_is_some<F, R>(&self, cb: F) -> Option<&R>
368 where
369 F: FnOnce(&T) -> &R,
370 {
371 unsafe {
372 if let Some(ref value) = *self.ptr.get() {
373 Some(cb(value))
374 } else {
375 None
376 }
377 }
378 }
379}
380
381impl<T: DomObject> PartialEq for MutNullableDom<T> {
382 fn eq(&self, other: &Self) -> bool {
383 unsafe { *self.ptr.get() == *other.ptr.get() }
384 }
385}
386
387impl<T: DomObject> PartialEq<Option<&T>> for MutNullableDom<T> {
388 fn eq(&self, other: &Option<&T>) -> bool {
389 unsafe { *self.ptr.get() == other.map(Dom::from_ref) }
390 }
391}
392
393impl<T: DomObject> Default for MutNullableDom<T> {
394 fn default() -> MutNullableDom<T> {
395 assert_in_script();
396 MutNullableDom {
397 ptr: UnsafeCell::new(None),
398 }
399 }
400}
401
402impl<T: DomObject> MallocSizeOf for MutNullableDom<T> {
403 fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
404 0
406 }
407}
408
409#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
416pub(crate) struct DomOnceCell<T: DomObject> {
417 ptr: OnceCell<Dom<T>>,
418}
419
420impl<T> DomOnceCell<T>
421where
422 T: DomObject,
423{
424 pub(crate) fn init_once<F>(&self, cb: F) -> &T
427 where
428 F: FnOnce() -> DomRoot<T>,
429 {
430 assert_in_script();
431 self.ptr.get_or_init(|| Dom::from_ref(&cb()))
432 }
433}
434
435impl<T: DomObject> Default for DomOnceCell<T> {
436 fn default() -> DomOnceCell<T> {
437 assert_in_script();
438 DomOnceCell {
439 ptr: OnceCell::new(),
440 }
441 }
442}
443
444impl<T: DomObject> MallocSizeOf for DomOnceCell<T> {
445 fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
446 0
448 }
449}
450
451unsafe impl<T: DomObject> JSTraceable for DomOnceCell<T> {
452 unsafe fn trace(&self, trc: *mut JSTracer) {
453 if let Some(ptr) = self.ptr.get() {
454 unsafe { ptr.trace(trc) };
455 }
456 }
457}
458
459impl<'dom, T> LayoutDom<'dom, T>
460where
461 T: 'dom + DomObject,
462{
463 pub(crate) fn unsafe_get(self) -> &'dom T {
466 assert_in_layout();
467 self.value
468 }
469
470 pub(crate) unsafe fn to_layout_slice(slice: &'dom [Dom<T>]) -> &'dom [LayoutDom<'dom, T>] {
473 let _ = mem::transmute::<Dom<T>, LayoutDom<T>>;
476 unsafe { &*(slice as *const [Dom<T>] as *const [LayoutDom<T>]) }
477 }
478}
479
480pub trait AsHandleValue<'a> {
486 fn as_handle_value(&'a self) -> HandleValue<'a>;
487}
488
489impl<'a> AsHandleValue<'a> for Heap<Value> {
490 #[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
491 fn as_handle_value(&'a self) -> HandleValue<'a> {
492 unsafe { HandleValue::from_marked_location(self.ptr.get() as *const _) }
495 }
496}