Debian Codegraph @

js/src/builtin/FinalizationRegistryObject.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 /*
8 * FinalizationRegistry objects allow a program to register to receive a
9 * callback after a 'target' object dies. The callback is passed a 'held value'
10 * (that hopefully doesn't entrain the target). An 'unregister token' is an
11 * object which can be used to remove multiple previous registrations in one go.
12 *
13 * To arrange this, the following data structures are used:
14 *
15 * +---------------------------------------+-------------------------------+
16 * | FinalizationRegistry compartment | Target zone / compartment |
17 * | | |
18 * | +----------------------+ | +------------------+ |
19 * | +-----+ FinalizationRegistry | | | Zone | |
20 * | | +----------+-----------+ | +---------+--------+ |
21 * | | | | | |
22 * | | v | v |
23 * | | +-------------+-------------+ | +-------------+------------+ |
24 * | | | Registrations | | | FinalizationObservers | |
25 * | | | weak map | | +-------------+------------+ |
26 * | | +---------------------------+ | | |
27 * | | | Unregister : Records | | v |
28 * | | | token : object | | +------------+------------+ |
29 * | | +--------------------+------+ | | RecordMap map | |
30 * | | | | +-------------------------+ |
31 * | | v | | Target : Finalization | |
32 * | | +--------------------+------+ | | object : RecordVector | |
33 * | | | Finalization | | +----+-------------+------+ |
34 * | | | RegistrationsObject | | | | |
35 * | | +---------------------------+ | v v |
36 * | | | RecordVector | | +----+-----+ +----+-----+ |
37 * | | +-------------+-------------+ | | Target | | (CCW if | |
38 * | | | | | JSObject | | needed) | |
39 * | | * v | +----------+ +----+-----+ |
40 * | | +-------------+-------------+ * | | |
41 * | | | FinalizationRecordObject +<--------------------------+ |
42 * | | +---------------------------+ | |
43 * | | | Queue +--+ | |
44 * | | +---------------------------+ | | |
45 * | | | Held value | | | |
46 * | | +---------------------------+ | | |
47 * | | | | |
48 * | +--------------+ +--------------+ | |
49 * | | | | |
50 * | v v | |
51 * | +----------+---+----------+ | |
52 * | | FinalizationQueueObject | | |
53 * | +-------------------------+ | |
54 * | | |
55 * +---------------------------------------+-------------------------------+
56 *
57 * A FinalizationRegistry consists of two parts: the FinalizationRegistry that
58 * consumers see and a FinalizationQueue used internally to queue and call the
59 * cleanup callbacks.
60 *
61 * Registering a target with a FinalizationRegistry creates a FinalizationRecord
62 * containing a pointer to the queue and the heldValue. This is added to a
63 * vector of records associated with the target, implemented as a map on the
64 * target's Zone. All finalization records are treated as GC roots.
65 *
66 * When a target is registered an unregister token may be supplied. If so, this
67 * is also recorded by the registry and is stored in a weak map of
68 * registrations. The values of this map are FinalizationRegistrationsObject
69 * objects. It's necessary to have another JSObject here because our weak map
70 * implementation only supports JS types as values.
71 *
72 * When targets are unregistered, the registration is looked up in the weakmap
73 * and the corresponding records are cleared.
74
75 * The finalization record maps are swept during GC to check for records that
76 * have been cleared by unregistration, for FinalizationRecords that are dead
77 * and for nuked CCWs. In all cases the record is removed and the cleanup
78 * callback is not run.
79 *
80 * Following this the targets are checked to see if they are dying. For such
81 * targets the associated record list is processed and for each record the
82 * heldValue is queued on the FinalizationQueue. At a later time this causes the
83 * client's cleanup callback to be run.
84 */
85
86 #ifndef builtin_FinalizationRegistryObject_h
87 #define builtin_FinalizationRegistryObject_h
88
89 #include "gc/Barrier.h"
90 #include "gc/WeakMap.h"
91 #include "js/GCVector.h"
92 #include "vm/NativeObject.h"
93
94 namespace js {
95
96 class FinalizationRegistryObject;
97 class FinalizationRecordObject;
98 class FinalizationQueueObject;
99
100 using HandleFinalizationRegistryObject = Handle<FinalizationRegistryObject*>;
101 using HandleFinalizationRecordObject = Handle<FinalizationRecordObject*>;
102 using HandleFinalizationQueueObject = Handle<FinalizationQueueObject*>;
103 using RootedFinalizationRegistryObject = Rooted<FinalizationRegistryObject*>;
104 using RootedFinalizationRecordObject = Rooted<FinalizationRecordObject*>;
105 using RootedFinalizationQueueObject = Rooted<FinalizationQueueObject*>;
106
107 // A finalization record: a pair of finalization queue and held value.
108 //
109 // A finalization record represents the registered interest of a finalization
110 // registry in a target's finalization.
111 //
112 // Finalization records created in the 'registered' state but may be
113 // unregistered. This happens when:
114 // - the heldValue is passed to the registry's cleanup callback
115 // - the registry's unregister method removes the registration
116 //
117 // Finalization records are added to a per-zone record map. They are removed
118 // when the record is queued for cleanup, or if the interest in finalization is
119 // cancelled. See FinalizationObservers::shouldRemoveRecord for the possible
120 // reasons.
121
122 class FinalizationRecordObject : public NativeObject {
123 enum { QueueSlot = 0, HeldValueSlot, InMapSlot, SlotCount };
124
125 public:
126 static const JSClass class_;
127
128 static FinalizationRecordObject* create(JSContext* cx,
129 HandleFinalizationQueueObject queue,
130 HandleValue heldValue);
131
132 FinalizationQueueObject* queue() const;
133 Value heldValue() const;
134 bool isRegistered() const;
135 bool isInRecordMap() const;
136
137 void setInRecordMap(bool newValue);
138 void clear();
139 };
140
141 // A vector of weakly-held FinalizationRecordObjects.
142 using WeakFinalizationRecordVector =
143 GCVector<WeakHeapPtr<FinalizationRecordObject*>, 1, js::CellAllocPolicy>;
144
145 // A JS object containing a vector of weakly-held FinalizationRecordObjects,
146 // which holds the records corresponding to the registrations for a particular
147 // registration token. These are used as the values in the registration
148 // weakmap. Since the contents of the vector are weak references they are not
149 // traced.
150 class FinalizationRegistrationsObject : public NativeObject {
151 enum { RecordsSlot = 0, SlotCount };
152
153 public:
154 static const JSClass class_;
155
156 static FinalizationRegistrationsObject* create(JSContext* cx);
157
158 WeakFinalizationRecordVector* records();
159 const WeakFinalizationRecordVector* records() const;
160
161 bool isEmpty() const;
162
163 bool append(HandleFinalizationRecordObject record);
164 void remove(HandleFinalizationRecordObject record);
165
166 bool traceWeak(JSTracer* trc);
167
168 private:
169 static const JSClassOps classOps_;
170
171 void* privatePtr() const;
172
173 static void trace(JSTracer* trc, JSObject* obj);
174 static void finalize(JS::GCContext* gcx, JSObject* obj);
175 };
176
177 using FinalizationRecordVector =
178 GCVector<HeapPtr<FinalizationRecordObject*>, 1, js::CellAllocPolicy>;
179
180 // The JS FinalizationRegistry object itself.
181 class FinalizationRegistryObject : public NativeObject {
182 enum { QueueSlot = 0, RegistrationsSlot, SlotCount };
183
184 public:
185 static const JSClass class_;
186 static const JSClass protoClass_;
187
188 FinalizationQueueObject* queue() const;
189 ObjectWeakMap* registrations() const;
190
191 void traceWeak(JSTracer* trc);
192
193 static bool unregisterRecord(FinalizationRecordObject* record);
194
195 static bool cleanupQueuedRecords(JSContext* cx,
196 HandleFinalizationRegistryObject registry,
197 HandleObject callback = nullptr);
198
199 private:
200 static const JSClassOps classOps_;
201 static const ClassSpec classSpec_;
202 static const JSFunctionSpec methods_[];
203 static const JSPropertySpec properties_[];
204
205 static bool construct(JSContext* cx, unsigned argc, Value* vp);
206 static bool register_(JSContext* cx, unsigned argc, Value* vp);
207 static bool unregister(JSContext* cx, unsigned argc, Value* vp);
208 static bool cleanupSome(JSContext* cx, unsigned argc, Value* vp);
209
210 static bool addRegistration(JSContext* cx,
211 HandleFinalizationRegistryObject registry,
212 HandleObject unregisterToken,
213 HandleFinalizationRecordObject record);
214 static void removeRegistrationOnError(
215 HandleFinalizationRegistryObject registry, HandleObject unregisterToken,
216 HandleFinalizationRecordObject record);
217
218 static bool preserveDOMWrapper(JSContext* cx, HandleObject obj);
219
220 static void trace(JSTracer* trc, JSObject* obj);
221 static void finalize(JS::GCContext* gcx, JSObject* obj);
222 };
223
224 // Contains information about the cleanup callback and the records queued to
225 // be cleaned up. This is not exposed to content JS.
226 class FinalizationQueueObject : public NativeObject {
227 enum {
228 CleanupCallbackSlot = 0,
229 HostDefinedDataSlot,
230 RecordsToBeCleanedUpSlot,
231 IsQueuedForCleanupSlot,
232 DoCleanupFunctionSlot,
233 HasRegistrySlot,
234 SlotCount
235 };
236
237 enum DoCleanupFunctionSlots {
238 DoCleanupFunction_QueueSlot = 0,
239 };
240
241 public:
242 static const JSClass class_;
243
244 JSObject* cleanupCallback() const;
245 JSObject* getHostDefinedData() const;
246 FinalizationRecordVector* recordsToBeCleanedUp() const;
247 bool isQueuedForCleanup() const;
248 JSFunction* doCleanupFunction() const;
249 bool hasRegistry() const;
250
251 void queueRecordToBeCleanedUp(FinalizationRecordObject* record);
252 void setQueuedForCleanup(bool value);
253
254 void setHasRegistry(bool newValue);
255
256 static FinalizationQueueObject* create(JSContext* cx,
257 HandleObject cleanupCallback);
258
259 static bool cleanupQueuedRecords(JSContext* cx,
260 HandleFinalizationQueueObject registry,
261 HandleObject callback = nullptr);
262
263 private:
264 static const JSClassOps classOps_;
265
266 static bool doCleanup(JSContext* cx, unsigned argc, Value* vp);
267
268 static void trace(JSTracer* trc, JSObject* obj);
269 static void finalize(JS::GCContext* gcx, JSObject* obj);
270 };
271
272 } // namespace js
273
274 #endif /* builtin_FinalizationRegistryObject_h */