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 drain(&mut self) -> std::collections::hash_map::Drain<'_, K, V> {
127 self.0.drain()
128 }
129
130 #[inline]
131 pub(crate) fn is_empty(&self) -> bool {
132 self.0.is_empty()
133 }
134}
135
136impl<K, V, S> HashMapTracedValues<K, V, S>
137where
138 K: Eq + Hash,
139 S: BuildHasher,
140{
141 #[inline]
142 pub(crate) fn insert(&mut self, k: K, v: V) -> Option<V> {
143 self.0.insert(k, v)
144 }
145
146 #[inline]
147 pub(crate) fn get<Q>(&self, k: &Q) -> Option<&V>
148 where
149 K: std::borrow::Borrow<Q>,
150 Q: Hash + Eq + ?Sized,
151 {
152 self.0.get(k)
153 }
154
155 #[inline]
156 pub(crate) fn get_mut<Q: Hash + Eq + ?Sized>(&mut self, k: &Q) -> Option<&mut V>
157 where
158 K: std::borrow::Borrow<Q>,
159 {
160 self.0.get_mut(k)
161 }
162
163 #[inline]
164 pub(crate) fn contains_key<Q: Hash + Eq + ?Sized>(&self, k: &Q) -> bool
165 where
166 K: std::borrow::Borrow<Q>,
167 {
168 self.0.contains_key(k)
169 }
170
171 #[inline]
172 pub(crate) fn remove<Q: Hash + Eq + ?Sized>(&mut self, k: &Q) -> Option<V>
173 where
174 K: std::borrow::Borrow<Q>,
175 {
176 self.0.remove(k)
177 }
178
179 #[inline]
180 pub(crate) fn entry(&mut self, key: K) -> std::collections::hash_map::Entry<'_, K, V> {
181 self.0.entry(key)
182 }
183}
184
185impl<K, V, S> MallocSizeOf for HashMapTracedValues<K, V, S>
186where
187 K: Eq + Hash + MallocSizeOf,
188 V: MallocSizeOf,
189 S: BuildHasher,
190{
191 fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
192 self.0.size_of(ops)
193 }
194}
195
196impl<K, V, S> MallocConditionalSizeOf for HashMapTracedValues<K, V, S>
197where
198 K: Eq + Hash + MallocSizeOf,
199 V: MallocConditionalSizeOf,
200 S: BuildHasher,
201{
202 fn conditional_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
203 self.0.conditional_size_of(ops)
204 }
205}
206
207#[expect(unsafe_code)]
208unsafe impl<K, V: JSTraceable, S> JSTraceable for HashMapTracedValues<K, V, S> {
209 #[inline]
210 unsafe fn trace(&self, trc: *mut ::js::jsapi::JSTracer) {
211 for v in self.0.values() {
212 unsafe { v.trace(trc) };
213 }
214 }
215}
216
217unsafe_no_jsmanaged_fields!(Box<dyn TaskBox>);
218
219unsafe_no_jsmanaged_fields!(IncompleteParserContexts);
220
221#[expect(dead_code)]
222pub(crate) fn trace_script(tracer: *mut JSTracer, description: &str, script: &Heap<*mut JSScript>) {
224 unsafe {
225 trace!("tracing {}", description);
226 CallScriptTracer(
227 tracer,
228 script.ptr.get() as *mut _,
229 GCTraceKindToAscii(TraceKind::Script),
230 );
231 }
232}
233
234#[expect(dead_code)]
235pub(crate) fn trace_jsval(tracer: *mut JSTracer, description: &str, val: &Heap<JSVal>) {
237 unsafe {
238 if !val.get().is_markable() {
239 return;
240 }
241
242 trace!("tracing value {}", description);
243 CallValueTracer(
244 tracer,
245 val.ptr.get() as *mut _,
246 GCTraceKindToAscii(val.get().trace_kind()),
247 );
248 }
249}
250
251#[expect(dead_code)]
252pub(crate) fn trace_string(tracer: *mut JSTracer, description: &str, s: &Heap<*mut JSString>) {
254 unsafe {
255 trace!("tracing {}", description);
256 CallStringTracer(
257 tracer,
258 s.ptr.get() as *mut _,
259 GCTraceKindToAscii(TraceKind::String),
260 );
261 }
262}
263
264unsafe_no_jsmanaged_fields!(TrustedPromise);
265
266unsafe_no_jsmanaged_fields!(WindowProxyHandler);
267unsafe_no_jsmanaged_fields!(SourceSet);
268unsafe_no_jsmanaged_fields!(HTMLMediaElementFetchContext);
269unsafe_no_jsmanaged_fields!(StreamConsumer);
270
271unsafe impl<T: DomObject> JSTraceable for Trusted<T> {
272 #[inline]
273 unsafe fn trace(&self, _: *mut JSTracer) {
274 }
276}