1use std::collections::HashMap;
33use std::collections::hash_map::RandomState;
34use std::fmt::Display;
35use std::hash::{BuildHasher, Hash};
36
37pub(crate) use js::gc::Traceable as JSTraceable;
39use js::glue::{CallScriptTracer, CallStringTracer, CallValueTracer};
40use js::jsapi::{GCTraceKindToAscii, Heap, JSScript, JSString, JSTracer, TraceKind};
41use js::jsval::JSVal;
42use malloc_size_of::{MallocConditionalSizeOf, MallocSizeOf, MallocSizeOfOps};
43use rustc_hash::FxBuildHasher;
44use script_bindings::reflector::DomObject;
45pub(crate) use script_bindings::trace::*;
46
47use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
48use crate::dom::html::htmlimageelement::SourceSet;
49use crate::dom::html::htmlmediaelement::HTMLMediaElementFetchContext;
50use crate::dom::windowproxy::WindowProxyHandler;
51use crate::script_runtime::StreamConsumer;
52use crate::script_thread::IncompleteParserContexts;
53use crate::task::TaskBox;
54
55#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
59#[cfg_attr(crown, crown::trace_in_no_trace_lint::must_not_have_traceable)]
60pub(crate) struct NoTrace<T>(pub(crate) T);
61
62impl<T: Display> Display for NoTrace<T> {
63 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64 self.0.fmt(f)
65 }
66}
67
68impl<T> From<T> for NoTrace<T> {
69 fn from(item: T) -> Self {
70 Self(item)
71 }
72}
73
74#[expect(unsafe_code)]
75unsafe impl<T> JSTraceable for NoTrace<T> {
76 #[inline]
77 unsafe fn trace(&self, _: *mut ::js::jsapi::JSTracer) {}
78}
79
80impl<T: MallocSizeOf> MallocSizeOf for NoTrace<T> {
81 fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
82 self.0.size_of(ops)
83 }
84}
85
86#[cfg_attr(crown, crown::trace_in_no_trace_lint::must_not_have_traceable(0))]
93#[derive(Clone, Debug)]
94pub(crate) struct HashMapTracedValues<K, V, S = RandomState>(pub(crate) HashMap<K, V, S>);
95
96impl<K, V, S: Default> Default for HashMapTracedValues<K, V, S> {
97 fn default() -> Self {
98 Self(Default::default())
99 }
100}
101
102impl<K, V> HashMapTracedValues<K, V, RandomState> {
103 #[inline]
105 #[must_use]
106 pub(crate) fn new() -> HashMapTracedValues<K, V, RandomState> {
107 Self(HashMap::new())
108 }
109}
110
111impl<K, V> HashMapTracedValues<K, V, FxBuildHasher> {
112 #[inline]
113 #[must_use]
114 pub(crate) fn new_fx() -> HashMapTracedValues<K, V, FxBuildHasher> {
115 Self(HashMap::with_hasher(FxBuildHasher))
116 }
117}
118
119impl<K, V, S> HashMapTracedValues<K, V, S> {
120 #[inline]
121 pub(crate) fn iter(&self) -> std::collections::hash_map::Iter<'_, K, V> {
122 self.0.iter()
123 }
124
125 #[inline]
126 pub(crate) fn iter_mut(&mut self) -> std::collections::hash_map::IterMut<'_, K, V> {
127 self.0.iter_mut()
128 }
129
130 #[inline]
131 pub(crate) fn drain(&mut self) -> std::collections::hash_map::Drain<'_, K, V> {
132 self.0.drain()
133 }
134
135 #[inline]
136 pub(crate) fn is_empty(&self) -> bool {
137 self.0.is_empty()
138 }
139}
140
141impl<K, V, S> HashMapTracedValues<K, V, S>
142where
143 K: Eq + Hash,
144 S: BuildHasher,
145{
146 #[inline]
147 pub(crate) fn insert(&mut self, k: K, v: V) -> Option<V> {
148 self.0.insert(k, v)
149 }
150
151 #[inline]
152 pub(crate) fn get<Q>(&self, k: &Q) -> Option<&V>
153 where
154 K: std::borrow::Borrow<Q>,
155 Q: Hash + Eq + ?Sized,
156 {
157 self.0.get(k)
158 }
159
160 #[inline]
161 pub(crate) fn get_mut<Q: Hash + Eq + ?Sized>(&mut self, k: &Q) -> Option<&mut V>
162 where
163 K: std::borrow::Borrow<Q>,
164 {
165 self.0.get_mut(k)
166 }
167
168 #[inline]
169 pub(crate) fn contains_key<Q: Hash + Eq + ?Sized>(&self, k: &Q) -> bool
170 where
171 K: std::borrow::Borrow<Q>,
172 {
173 self.0.contains_key(k)
174 }
175
176 #[inline]
177 pub(crate) fn remove<Q: Hash + Eq + ?Sized>(&mut self, k: &Q) -> Option<V>
178 where
179 K: std::borrow::Borrow<Q>,
180 {
181 self.0.remove(k)
182 }
183
184 #[inline]
185 pub(crate) fn entry(&mut self, key: K) -> std::collections::hash_map::Entry<'_, K, V> {
186 self.0.entry(key)
187 }
188}
189
190impl<K, V, S> MallocSizeOf for HashMapTracedValues<K, V, S>
191where
192 K: Eq + Hash + MallocSizeOf,
193 V: MallocSizeOf,
194 S: BuildHasher,
195{
196 fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
197 self.0.size_of(ops)
198 }
199}
200
201impl<K, V, S> MallocConditionalSizeOf for HashMapTracedValues<K, V, S>
202where
203 K: Eq + Hash + MallocSizeOf,
204 V: MallocConditionalSizeOf,
205 S: BuildHasher,
206{
207 fn conditional_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
208 self.0.conditional_size_of(ops)
209 }
210}
211
212#[expect(unsafe_code)]
213unsafe impl<K, V: JSTraceable, S> JSTraceable for HashMapTracedValues<K, V, S> {
214 #[inline]
215 unsafe fn trace(&self, trc: *mut ::js::jsapi::JSTracer) {
216 for v in self.0.values() {
217 unsafe { v.trace(trc) };
218 }
219 }
220}
221
222unsafe_no_jsmanaged_fields!(Box<dyn TaskBox>);
223
224unsafe_no_jsmanaged_fields!(IncompleteParserContexts);
225
226#[expect(dead_code)]
227pub(crate) fn trace_script(tracer: *mut JSTracer, description: &str, script: &Heap<*mut JSScript>) {
229 unsafe {
230 trace!("tracing {}", description);
231 CallScriptTracer(
232 tracer,
233 script.ptr.get() as *mut _,
234 GCTraceKindToAscii(TraceKind::Script),
235 );
236 }
237}
238
239#[expect(dead_code)]
240pub(crate) fn trace_jsval(tracer: *mut JSTracer, description: &str, val: &Heap<JSVal>) {
242 unsafe {
243 if !val.get().is_markable() {
244 return;
245 }
246
247 trace!("tracing value {}", description);
248 CallValueTracer(
249 tracer,
250 val.ptr.get() as *mut _,
251 GCTraceKindToAscii(val.get().trace_kind()),
252 );
253 }
254}
255
256#[expect(dead_code)]
257pub(crate) fn trace_string(tracer: *mut JSTracer, description: &str, s: &Heap<*mut JSString>) {
259 unsafe {
260 trace!("tracing {}", description);
261 CallStringTracer(
262 tracer,
263 s.ptr.get() as *mut _,
264 GCTraceKindToAscii(TraceKind::String),
265 );
266 }
267}
268
269unsafe_no_jsmanaged_fields!(TrustedPromise);
270
271unsafe_no_jsmanaged_fields!(WindowProxyHandler);
272unsafe_no_jsmanaged_fields!(SourceSet);
273unsafe_no_jsmanaged_fields!(HTMLMediaElementFetchContext);
274unsafe_no_jsmanaged_fields!(StreamConsumer);
275
276unsafe impl<T: DomObject> JSTraceable for Trusted<T> {
277 #[inline]
278 unsafe fn trace(&self, _: *mut JSTracer) {
279 }
281}