Debian Codegraph @

js/public/TraceKind.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef js_TraceKind_h
8 #define js_TraceKind_h
9
10 #include "mozilla/UniquePtr.h"
11
12 #include "js/TypeDecls.h"
13
14 // Forward declarations of all the types a TraceKind can denote.
15 class JSLinearString;
16
17 namespace js {
18 class BaseScript;
19 class BaseShape;
20 class GetterSetter;
21 class PropMap;
22 class RegExpShared;
23 class Shape;
24 class Scope;
25 namespace gc {
26 class SmallBuffer;
27 } // namespace gc
28 namespace jit {
29 class JitCode;
30 } // namespace jit
31 } // namespace js
32
33 namespace JS {
34
35 // When tracing a thing, the GC needs to know about the layout of the object it
36 // is looking at. There are a fixed number of different layouts that the GC
37 // knows about. The "trace kind" is a static map which tells which layout a GC
38 // thing has.
39 //
40 // Although this map is public, the details are completely hidden. Not all of
41 // the matching C++ types are exposed, and those that are, are opaque.
42 //
43 // See Value::gcKind() and JSTraceCallback in Tracer.h for more details.
44 enum class TraceKind {
45 // These trace kinds have a publicly exposed, although opaque, C++ type.
46 // Note: The order here is determined by our Value packing. Other users
47 // should sort alphabetically, for consistency.
48 // Note: Nursery allocatable kinds go first. See js::gc::NurseryTraceKinds.
49 Object = 0x00,
50 BigInt = 0x01,
51 String = 0x02,
52 Symbol = 0x03,
53
54 // Shape details are exposed through JS_TraceShapeCycleCollectorChildren.
55 Shape = 0x04,
56
57 BaseShape = 0x05,
58
59 // The kind associated with a nullptr.
60 Null = 0x06,
61
62 // The following kinds do not have an exposed C++ idiom.
63 JitCode,
64 Script,
65 Scope,
66 RegExpShared,
67 GetterSetter,
68 PropMap,
69 SmallBuffer
70 };
71
72 // GCCellPtr packs the trace kind into the low bits of the pointer for common
73 // kinds.
74 const static uintptr_t OutOfLineTraceKindMask = 0x07;
75 static_assert(uintptr_t(JS::TraceKind::Null) < OutOfLineTraceKindMask,
76 "GCCellPtr requires an inline representation for nullptr");
77
78 // When this header is imported inside SpiderMonkey, the class definitions are
79 // available and we can query those definitions to find the correct kind
80 // directly from the class hierarchy.
81 template <typename T>
82 struct MapTypeToTraceKind {
83 static const JS::TraceKind kind = T::TraceKind;
84 };
85
86 // When this header is used outside SpiderMonkey, the class definitions are not
87 // available, so the following table containing all public GC types is used.
88 //
89 // canBeGray: GC can mark things of this kind gray. The cycle collector
90 // traverses gray GC things when looking for cycles.
91 // inCCGraph: Things of this kind are represented as nodes in the CC graph. This
92 // also means they can be used as a keys in WeakMap.
93
94 // clang-format off
95 #define JS_FOR_EACH_TRACEKIND(D) \
96 /* name type canBeGray inCCGraph */ \
97 D(BaseShape, js::BaseShape, true, false) \
98 D(JitCode, js::jit::JitCode, true, false) \
99 D(Scope, js::Scope, true, true) \
100 D(Object, JSObject, true, true) \
101 D(Script, js::BaseScript, true, true) \
102 D(Shape, js::Shape, true, false) \
103 D(String, JSString, false, false) \
104 D(Symbol, JS::Symbol, false, false) \
105 D(BigInt, JS::BigInt, false, false) \
106 D(RegExpShared, js::RegExpShared, true, true) \
107 D(GetterSetter, js::GetterSetter, true, true) \
108 D(PropMap, js::PropMap, false, false) \
109 D(SmallBuffer, js::gc::SmallBuffer, false, false)
110 // clang-format on
111
112 // Returns true if the JS::TraceKind is represented as a node in cycle collector
113 // graph.
114 inline constexpr bool IsCCTraceKind(JS::TraceKind aKind) {
115 switch (aKind) {
116 #define JS_EXPAND_DEF(name, _1, _2, inCCGraph) \
117 case JS::TraceKind::name: \
118 return inCCGraph;
119 JS_FOR_EACH_TRACEKIND(JS_EXPAND_DEF);
120 #undef JS_EXPAND_DEF
121 default:
122 return false;
123 }
124 }
125
126 // Helper for SFINAE to ensure certain methods are only used on appropriate base
127 // types. This avoids common footguns such as `Cell::is<JSFunction>()` which
128 // match any type of JSObject.
129 template <typename T>
130 struct IsBaseTraceType : std::false_type {};
131
132 #define JS_EXPAND_DEF(_, type, _1, _2) \
133 template <> \
134 struct IsBaseTraceType<type> : std::true_type {};
135 JS_FOR_EACH_TRACEKIND(JS_EXPAND_DEF);
136 #undef JS_EXPAND_DEF
137
138 template <typename T>
139 inline constexpr bool IsBaseTraceType_v = IsBaseTraceType<T>::value;
140
141 // Map from all public types to their trace kind.
142 #define JS_EXPAND_DEF(name, type, _, _1) \
143 template <> \
144 struct MapTypeToTraceKind<type> { \
145 static const JS::TraceKind kind = JS::TraceKind::name; \
146 };
147 JS_FOR_EACH_TRACEKIND(JS_EXPAND_DEF);
148 #undef JS_EXPAND_DEF
149
150 template <>
151 struct MapTypeToTraceKind<JSLinearString> {
152 static const JS::TraceKind kind = JS::TraceKind::String;
153 };
154 template <>
155 struct MapTypeToTraceKind<JSFunction> {
156 static const JS::TraceKind kind = JS::TraceKind::Object;
157 };
158 template <>
159 struct MapTypeToTraceKind<JSScript> {
160 static const JS::TraceKind kind = JS::TraceKind::Script;
161 };
162
163 // RootKind is closely related to TraceKind. Whereas TraceKind's indices are
164 // laid out for convenient embedding as a pointer tag, the indicies of RootKind
165 // are designed for use as array keys via EnumeratedArray.
166 enum class RootKind : int8_t {
167 // These map 1:1 with trace kinds.
168 #define EXPAND_ROOT_KIND(name, _0, _1, _2) name,
169 JS_FOR_EACH_TRACEKIND(EXPAND_ROOT_KIND)
170 #undef EXPAND_ROOT_KIND
171
172 // These tagged pointers are special-cased for performance.
173 Id,
174 Value,
175
176 // Everything else.
177 Traceable,
178
179 Limit
180 };
181
182 // Most RootKind correspond directly to a trace kind.
183 template <TraceKind traceKind>
184 struct MapTraceKindToRootKind {};
185 #define JS_EXPAND_DEF(name, _0, _1, _2) \
186 template <> \
187 struct MapTraceKindToRootKind<JS::TraceKind::name> { \
188 static const JS::RootKind kind = JS::RootKind::name; \
189 };
190 JS_FOR_EACH_TRACEKIND(JS_EXPAND_DEF)
191 #undef JS_EXPAND_DEF
192
193 // Specify the RootKind for all types. Value and jsid map to special cases;
194 // Cell pointer types we can derive directly from the TraceKind; everything else
195 // should go in the Traceable list and use GCPolicy<T>::trace for tracing.
196 template <typename T>
197 struct MapTypeToRootKind {
198 static const JS::RootKind kind = JS::RootKind::Traceable;
199 };
200 template <typename T>
201 struct MapTypeToRootKind<T*> {
202 static const JS::RootKind kind =
203 JS::MapTraceKindToRootKind<JS::MapTypeToTraceKind<T>::kind>::kind;
204 };
205 template <>
206 struct MapTypeToRootKind<JS::Realm*> {
207 // Not a pointer to a GC cell. Use GCPolicy.
208 static const JS::RootKind kind = JS::RootKind::Traceable;
209 };
210 template <typename T>
211 struct MapTypeToRootKind<mozilla::UniquePtr<T>> {
212 static const JS::RootKind kind = JS::MapTypeToRootKind<T>::kind;
213 };
214 template <>
215 struct MapTypeToRootKind<JS::Value> {
216 static const JS::RootKind kind = JS::RootKind::Value;
217 };
218 template <>
219 struct MapTypeToRootKind<jsid> {
220 static const JS::RootKind kind = JS::RootKind::Id;
221 };
222
223 // Fortunately, few places in the system need to deal with fully abstract
224 // cells. In those places that do, we generally want to move to a layout
225 // templated function as soon as possible. This template wraps the upcast
226 // for that dispatch.
227 //
228 // Given a call:
229 //
230 // DispatchTraceKindTyped(f, thing, traceKind, ... args)
231 //
232 // Downcast the |void *thing| to the specific type designated by |traceKind|,
233 // and pass it to the functor |f| along with |... args|, forwarded. Pass the
234 // type designated by |traceKind| as the functor's template argument. The
235 // |thing| parameter is optional; without it, we simply pass through |... args|.
236 template <typename F, typename... Args>
237 auto DispatchTraceKindTyped(F f, JS::TraceKind traceKind, Args&&... args) {
238 switch (traceKind) {
239 #define JS_EXPAND_DEF(name, type, _, _1) \
240 case JS::TraceKind::name: \
241 return f.template operator()<type>(std::forward<Args>(args)...);
242 JS_FOR_EACH_TRACEKIND(JS_EXPAND_DEF);
243 #undef JS_EXPAND_DEF
244 default:
245 MOZ_CRASH("Invalid trace kind in DispatchTraceKindTyped.");
246 }
247 }
248
249 // Given a GC thing specified by pointer and trace kind, calls the functor |f|
250 // with a template argument of the actual type of the pointer and returns the
251 // result.
252 template <typename F>
253 auto MapGCThingTyped(void* thing, JS::TraceKind traceKind, F&& f) {
254 switch (traceKind) {
255 #define JS_EXPAND_DEF(name, type, _, _1) \
256 case JS::TraceKind::name: \
257 return f(static_cast<type*>(thing));
258 JS_FOR_EACH_TRACEKIND(JS_EXPAND_DEF);
259 #undef JS_EXPAND_DEF
260 default:
261 MOZ_CRASH("Invalid trace kind in MapGCThingTyped.");
262 }
263 }
264
265 // Given a GC thing specified by pointer and trace kind, calls the functor |f|
266 // with a template argument of the actual type of the pointer and ignores the
267 // result.
268 template <typename F>
269 void ApplyGCThingTyped(void* thing, JS::TraceKind traceKind, F&& f) {
270 // This function doesn't do anything but is supplied for symmetry with other
271 // MapGCThingTyped/ApplyGCThingTyped implementations that have to wrap the
272 // functor to return a dummy value that is ignored.
273 MapGCThingTyped(thing, traceKind, std::move(f));
274 }
275
276 } // namespace JS
277
278 #endif // js_TraceKind_h