script/dom/bindings/
root.rs1use std::cell::{OnceCell, UnsafeCell};
28use std::default::Default;
29use std::hash::{Hash, Hasher};
30use std::{mem, ptr};
31
32use js::jsapi::{Heap, JSObject, JSTracer, Value};
33use js::rust::HandleValue;
34use layout_api::TrustedNodeAddress;
35use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
36pub(crate) use script_bindings::root::*;
37use style::thread_state;
38
39use crate::dom::bindings::conversions::DerivedFrom;
40use crate::dom::bindings::inheritance::Castable;
41use crate::dom::bindings::reflector::DomObject;
42use crate::dom::bindings::trace::JSTraceable;
43use crate::dom::node::Node;
44
45pub(crate) trait ToLayout<T> {
46 unsafe fn to_layout(&self) -> LayoutDom<'_, T>;
52}
53
54impl<T: DomObject> ToLayout<T> for Dom<T> {
55 unsafe fn to_layout(&self) -> LayoutDom<'_, T> {
56 assert_in_layout();
57 LayoutDom {
58 value: unsafe { self.as_ptr().as_ref().unwrap() },
59 }
60 }
61}
62
63#[cfg_attr(crown, crown::unrooted_must_root_lint::allow_unrooted_interior)]
66#[repr(transparent)]
67pub(crate) struct LayoutDom<'dom, T> {
68 value: &'dom T,
69}
70
71impl<'dom, T> LayoutDom<'dom, T>
72where
73 T: Castable,
74{
75 pub(crate) fn upcast<U>(&self) -> LayoutDom<'dom, U>
77 where
78 U: Castable,
79 T: DerivedFrom<U>,
80 {
81 assert_in_layout();
82 LayoutDom {
83 value: self.value.upcast::<U>(),
84 }
85 }
86
87 pub(crate) fn downcast<U>(&self) -> Option<LayoutDom<'dom, U>>
89 where
90 U: DerivedFrom<T>,
91 {
92 assert_in_layout();
93 self.value.downcast::<U>().map(|value| LayoutDom { value })
94 }
95
96 pub(crate) fn is<U>(&self) -> bool
98 where
99 U: DerivedFrom<T>,
100 {
101 assert_in_layout();
102 self.value.is::<U>()
103 }
104}
105
106impl<T> LayoutDom<'_, T>
107where
108 T: DomObject,
109{
110 pub(crate) unsafe fn get_jsobject(&self) -> *mut JSObject {
112 assert_in_layout();
113 self.value.reflector().get_jsobject().get()
114 }
115}
116
117impl<T> Copy for LayoutDom<'_, T> {}
118
119impl<T> PartialEq for LayoutDom<'_, T> {
120 fn eq(&self, other: &Self) -> bool {
121 std::ptr::eq(self.value, other.value)
122 }
123}
124
125impl<T> Eq for LayoutDom<'_, T> {}
126
127impl<T> Hash for LayoutDom<'_, T> {
128 fn hash<H: Hasher>(&self, state: &mut H) {
129 (self.value as *const T).hash(state)
130 }
131}
132
133impl<T> Clone for LayoutDom<'_, T> {
134 #[inline]
135 #[allow(clippy::non_canonical_clone_impl)]
136 fn clone(&self) -> Self {
137 assert_in_layout();
138 *self
139 }
140}
141
142impl LayoutDom<'_, Node> {
143 pub(crate) unsafe fn from_trusted_node_address(inner: TrustedNodeAddress) -> Self {
146 assert_in_layout();
147 let TrustedNodeAddress(addr) = inner;
148 LayoutDom {
149 value: unsafe { &*(addr as *const Node) },
150 }
151 }
152}
153
154#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
160#[derive(JSTraceable)]
161pub(crate) struct MutDom<T: DomObject> {
162 val: UnsafeCell<Dom<T>>,
163}
164
165impl<T: DomObject> MutDom<T> {
166 pub(crate) fn new(initial: &T) -> MutDom<T> {
168 assert_in_script();
169 MutDom {
170 val: UnsafeCell::new(Dom::from_ref(initial)),
171 }
172 }
173
174 pub(crate) fn set(&self, val: &T) {
176 assert_in_script();
177 unsafe {
178 *self.val.get() = Dom::from_ref(val);
179 }
180 }
181
182 pub(crate) fn get(&self) -> DomRoot<T> {
184 assert_in_script();
185 unsafe { DomRoot::from_ref(&*ptr::read(self.val.get())) }
186 }
187}
188
189impl<T: DomObject> MallocSizeOf for MutDom<T> {
190 fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
191 0
193 }
194}
195
196impl<T: DomObject> PartialEq for MutDom<T> {
197 fn eq(&self, other: &Self) -> bool {
198 unsafe { *self.val.get() == *other.val.get() }
199 }
200}
201
202impl<T: DomObject + PartialEq> PartialEq<T> for MutDom<T> {
203 fn eq(&self, other: &T) -> bool {
204 unsafe { **self.val.get() == *other }
205 }
206}
207
208pub(crate) fn assert_in_layout() {
209 debug_assert!(thread_state::get().is_layout());
210}
211
212#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
219#[derive(JSTraceable)]
220pub(crate) struct MutNullableDom<T: DomObject> {
221 ptr: UnsafeCell<Option<Dom<T>>>,
222}
223
224impl<T: DomObject> MutNullableDom<T> {
225 pub(crate) fn new(initial: Option<&T>) -> MutNullableDom<T> {
227 assert_in_script();
228 MutNullableDom {
229 ptr: UnsafeCell::new(initial.map(Dom::from_ref)),
230 }
231 }
232
233 pub(crate) fn or_init<F>(&self, cb: F) -> DomRoot<T>
236 where
237 F: FnOnce() -> DomRoot<T>,
238 {
239 assert_in_script();
240 match self.get() {
241 Some(inner) => inner,
242 None => {
243 let inner = cb();
244 self.set(Some(&inner));
245 inner
246 },
247 }
248 }
249
250 pub(crate) unsafe fn get_inner_as_layout(&self) -> Option<LayoutDom<'_, T>> {
253 assert_in_layout();
254 unsafe { (*self.ptr.get()).as_ref().map(|js| js.to_layout()) }
255 }
256
257 pub(crate) fn get(&self) -> Option<DomRoot<T>> {
259 assert_in_script();
260 unsafe { ptr::read(self.ptr.get()).map(|o| DomRoot::from_ref(&*o)) }
261 }
262
263 pub(crate) fn set(&self, val: Option<&T>) {
265 assert_in_script();
266 unsafe {
267 *self.ptr.get() = val.map(|p| Dom::from_ref(p));
268 }
269 }
270
271 pub(crate) fn take(&self) -> Option<DomRoot<T>> {
273 let value = self.get();
274 self.set(None);
275 value
276 }
277
278 pub(crate) fn if_is_some<F, R>(&self, cb: F) -> Option<&R>
280 where
281 F: FnOnce(&T) -> &R,
282 {
283 unsafe {
284 if let Some(ref value) = *self.ptr.get() {
285 Some(cb(value))
286 } else {
287 None
288 }
289 }
290 }
291}
292
293impl<T: DomObject> PartialEq for MutNullableDom<T> {
294 fn eq(&self, other: &Self) -> bool {
295 unsafe { *self.ptr.get() == *other.ptr.get() }
296 }
297}
298
299impl<T: DomObject> PartialEq<Option<&T>> for MutNullableDom<T> {
300 fn eq(&self, other: &Option<&T>) -> bool {
301 unsafe { *self.ptr.get() == other.map(Dom::from_ref) }
302 }
303}
304
305impl<T: DomObject> Default for MutNullableDom<T> {
306 fn default() -> MutNullableDom<T> {
307 assert_in_script();
308 MutNullableDom {
309 ptr: UnsafeCell::new(None),
310 }
311 }
312}
313
314impl<T: DomObject> MallocSizeOf for MutNullableDom<T> {
315 fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
316 0
318 }
319}
320
321#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
328pub(crate) struct DomOnceCell<T: DomObject> {
329 ptr: OnceCell<Dom<T>>,
330}
331
332impl<T> DomOnceCell<T>
333where
334 T: DomObject,
335{
336 pub(crate) fn init_once<F>(&self, cb: F) -> &T
339 where
340 F: FnOnce() -> DomRoot<T>,
341 {
342 assert_in_script();
343 self.ptr.get_or_init(|| Dom::from_ref(&cb()))
344 }
345}
346
347impl<T: DomObject> Default for DomOnceCell<T> {
348 fn default() -> DomOnceCell<T> {
349 assert_in_script();
350 DomOnceCell {
351 ptr: OnceCell::new(),
352 }
353 }
354}
355
356impl<T: DomObject> MallocSizeOf for DomOnceCell<T> {
357 fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
358 0
360 }
361}
362
363unsafe impl<T: DomObject> JSTraceable for DomOnceCell<T> {
364 unsafe fn trace(&self, trc: *mut JSTracer) {
365 if let Some(ptr) = self.ptr.get() {
366 unsafe { ptr.trace(trc) };
367 }
368 }
369}
370
371impl<'dom, T> LayoutDom<'dom, T>
372where
373 T: 'dom + DomObject,
374{
375 pub(crate) fn unsafe_get(self) -> &'dom T {
378 assert_in_layout();
379 self.value
380 }
381
382 pub(crate) unsafe fn to_layout_slice(slice: &'dom [Dom<T>]) -> &'dom [LayoutDom<'dom, T>] {
385 let _ = mem::transmute::<Dom<T>, LayoutDom<T>>;
388 unsafe { &*(slice as *const [Dom<T>] as *const [LayoutDom<T>]) }
389 }
390}
391
392pub trait AsHandleValue<'a> {
398 fn as_handle_value(&'a self) -> HandleValue<'a>;
399}
400
401impl<'a> AsHandleValue<'a> for Heap<Value> {
402 #[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
403 fn as_handle_value(&'a self) -> HandleValue<'a> {
404 unsafe { HandleValue::from_marked_location(self.ptr.get() as *const _) }
407 }
408}