Bug 1850713: remove duplicated setting of early hint preloader id in `ScriptLoader...
[gecko.git] / dom / base / nsWrapperCache.h
blobf1c4e17fa31ce80ba87b56b1ce1d903d7726f245
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/. */
7 #ifndef nsWrapperCache_h___
8 #define nsWrapperCache_h___
10 #include "nsCycleCollectionParticipant.h"
11 #include "mozilla/Assertions.h"
12 #include "mozilla/ServoUtils.h"
13 #include "mozilla/RustCell.h"
14 #include "js/HeapAPI.h"
15 #include "js/TracingAPI.h"
16 #include "js/TypeDecls.h"
17 #include "nsISupports.h"
18 #include "nsISupportsUtils.h"
19 #include <type_traits>
21 namespace mozilla::dom {
22 class ContentProcessMessageManager;
23 class InProcessBrowserChildMessageManager;
24 class BrowserChildMessageManager;
25 } // namespace mozilla::dom
26 class SandboxPrivate;
27 class nsWindowRoot;
29 #define NS_WRAPPERCACHE_IID \
30 { \
31 0x6f3179a1, 0x36f7, 0x4a5c, { \
32 0x8c, 0xf1, 0xad, 0xc8, 0x7c, 0xde, 0x3e, 0x87 \
33 } \
36 // There are two sets of flags used by DOM nodes. One comes from reusing the
37 // remaining bits of the inherited nsWrapperCache flags (mFlags), and another is
38 // exclusive to nsINode (mBoolFlags).
40 // Both sets of flags are 32 bits. On 64-bit platforms, this can cause two
41 // wasted 32-bit fields due to alignment requirements. Some compilers are
42 // smart enough to coalesce the fields if we make mBoolFlags the first member
43 // of nsINode, but others (such as MSVC, but that's not officially supported
44 // by us anymore) are not. Also note that this kind of coalascing tends to
45 // interact poorly with rust's bindgen, see:
46 // https://github.com/rust-lang/rust-bindgen/issues/380
48 // So we just store mBoolFlags directly on nsWrapperCache on 64-bit platforms.
49 // This may waste space for some other nsWrapperCache-derived objects that have
50 // a 32-bit field as their first member, but those objects are unlikely to be as
51 // numerous or performance-critical as DOM nodes.
52 #ifdef HAVE_64BIT_BUILD
53 static_assert(sizeof(void*) == 8, "These architectures should be 64-bit");
54 # define BOOL_FLAGS_ON_WRAPPER_CACHE
55 #else
56 static_assert(sizeof(void*) == 4, "Only support 32-bit and 64-bit");
57 #endif
59 /**
60 * Class to store the wrapper for an object. This can only be used with objects
61 * that only have one non-security wrapper at a time (for an XPCWrappedNative
62 * this is usually ensured by setting an explicit parent in the PreCreate hook
63 * for the class).
65 * An instance of nsWrapperCache can be gotten from an object that implements
66 * a wrapper cache by calling QueryInterface on it. Note that this breaks XPCOM
67 * rules a bit (this object doesn't derive from nsISupports).
69 * The cache can store objects other than wrappers. We allow wrappers to use a
70 * separate JSObject to store their state (mostly expandos). If the wrapper is
71 * collected and we want to preserve this state we actually store the state
72 * object in the cache.
74 * The cache can store 3 types of objects: a DOM binding object (regular JS
75 * object or proxy), an nsOuterWindowProxy or an XPCWrappedNative wrapper.
77 * The finalizer for the wrapper clears the cache.
79 * A compacting GC can move the wrapper object. Pointers to moved objects are
80 * usually found and updated by tracing the heap, however non-preserved wrappers
81 * are weak references and are not traced, so another approach is
82 * necessary. Instead a class hook (objectMovedOp) is provided that is called
83 * when an object is moved and is responsible for ensuring pointers are
84 * updated. It does this by calling UpdateWrapper() on the wrapper
85 * cache. SetWrapper() asserts that the hook is implemented for any wrapper set.
87 * A number of the methods are implemented in nsWrapperCacheInlines.h because we
88 * have to include some JS headers that don't play nicely with the rest of the
89 * codebase. Include nsWrapperCacheInlines.h if you need to call those methods.
92 class JS_HAZ_ROOTED nsWrapperCache {
93 public:
94 NS_DECLARE_STATIC_IID_ACCESSOR(NS_WRAPPERCACHE_IID)
96 nsWrapperCache() = default;
97 ~nsWrapperCache() {
98 // Preserved wrappers should never end up getting cleared, but this can
99 // happen during shutdown when a leaked wrapper object is finalized, causing
100 // its wrapper to be cleared.
101 MOZ_ASSERT(!PreservingWrapper() || js::RuntimeIsBeingDestroyed(),
102 "Destroying cache with a preserved wrapper!");
106 * Get the cached wrapper.
108 * This getter clears the gray bit before handing out the JSObject which means
109 * that the object is guaranteed to be kept alive past the next CC.
111 JSObject* GetWrapper() const;
114 * Get the cached wrapper.
116 * This getter does not change the color of the JSObject meaning that the
117 * object returned is not guaranteed to be kept alive past the next CC.
119 * This should only be called if you are certain that the return value won't
120 * be passed into a JSAPI function and that it won't be stored without being
121 * rooted (or otherwise signaling the stored value to the CC).
123 JSObject* GetWrapperPreserveColor() const;
126 * Get the cached wrapper.
128 * This getter does not check whether the wrapper is dead and in the process
129 * of being finalized.
131 * This should only be called if you really need to see the raw contents of
132 * this cache, for example as part of finalization. Don't store the result
133 * anywhere or pass it into JSAPI functions that may cause the value to
134 * escape.
136 JSObject* GetWrapperMaybeDead() const { return mWrapper; }
138 #ifdef DEBUG
139 private:
140 static bool HasJSObjectMovedOp(JSObject* aWrapper);
142 static void AssertUpdatedWrapperZone(const JSObject* aNewObject,
143 const JSObject* aOldObject);
145 public:
146 #endif
148 void SetWrapper(JSObject* aWrapper) {
149 MOZ_ASSERT(!PreservingWrapper(), "Clearing a preserved wrapper!");
150 MOZ_ASSERT(aWrapper, "Use ClearWrapper!");
151 MOZ_ASSERT(HasJSObjectMovedOp(aWrapper),
152 "Object has not provided the hook to update the wrapper if it "
153 "is moved");
155 SetWrapperJSObject(aWrapper);
159 * Clear the cache.
161 void ClearWrapper() {
162 // Preserved wrappers should never end up getting cleared, but this can
163 // happen during shutdown when a leaked wrapper object is finalized, causing
164 // its wrapper to be cleared.
165 MOZ_ASSERT(!PreservingWrapper() || js::RuntimeIsBeingDestroyed(),
166 "Clearing a preserved wrapper!");
167 SetWrapperJSObject(nullptr);
171 * Clear the cache if it still contains a specific wrapper object. This should
172 * be called from the finalizer for the wrapper.
174 void ClearWrapper(JSObject* obj) {
175 if (obj == mWrapper) {
176 ClearWrapper();
181 * Update the wrapper when the object moves between globals.
183 template <typename T>
184 void UpdateWrapperForNewGlobal(T* aScriptObjectHolder, JSObject* aNewWrapper);
187 * Update the wrapper if the object it contains is moved.
189 * This method must be called from the objectMovedOp class extension hook for
190 * any wrapper cached object.
192 void UpdateWrapper(JSObject* aNewObject, const JSObject* aOldObject) {
193 #ifdef DEBUG
194 AssertUpdatedWrapperZone(aNewObject, aOldObject);
195 #endif
196 if (mWrapper) {
197 MOZ_ASSERT(mWrapper == aOldObject);
198 mWrapper = aNewObject;
202 bool PreservingWrapper() const {
203 return HasWrapperFlag(WRAPPER_BIT_PRESERVED);
207 * Wrap the object corresponding to this wrapper cache. If non-null is
208 * returned, the object has already been stored in the wrapper cache.
210 virtual JSObject* WrapObject(JSContext* cx,
211 JS::Handle<JSObject*> aGivenProto) = 0;
214 * Returns true if the object has a wrapper that is known live from the point
215 * of view of cycle collection.
217 bool HasKnownLiveWrapper() const;
220 * Returns true if the object has a known-live wrapper (from the CC point of
221 * view) and all the GC things it is keeping alive are already known-live from
222 * CC's point of view.
224 bool HasKnownLiveWrapperAndDoesNotNeedTracing(nsISupports* aThis);
226 bool HasNothingToTrace(nsISupports* aThis);
229 * Mark our wrapper, if any, as live as far as the CC is concerned.
231 void MarkWrapperLive();
233 // Only meant to be called by code that preserves a wrapper.
234 void SetPreservingWrapper(bool aPreserve) {
235 if (aPreserve) {
236 SetWrapperFlags(WRAPPER_BIT_PRESERVED);
237 } else {
238 UnsetWrapperFlags(WRAPPER_BIT_PRESERVED);
242 void TraceWrapper(const TraceCallbacks& aCallbacks, void* aClosure) {
243 if (PreservingWrapper() && mWrapper) {
244 aCallbacks.Trace(this, "Preserved wrapper", aClosure);
249 * The following methods for getting and manipulating flags allow the unused
250 * bits of mFlags to be used by derived classes.
253 using FlagsType = uint32_t;
255 FlagsType GetFlags() const {
256 MOZ_ASSERT(NS_IsMainThread());
257 MOZ_ASSERT(!mozilla::IsInServoTraversal());
258 return mFlags.Get() & ~kWrapperFlagsMask;
261 // This can be called from stylo threads too, so it needs to be atomic, as
262 // this value may be mutated from multiple threads during servo traversal from
263 // rust.
264 bool HasFlag(FlagsType aFlag) const {
265 MOZ_ASSERT((aFlag & kWrapperFlagsMask) == 0, "Bad flag mask");
266 return __atomic_load_n(mFlags.AsPtr(), __ATOMIC_RELAXED) & aFlag;
269 // Identical to HasFlag, but more explicit about its handling of multiple
270 // flags. This can be called from stylo threads too.
271 bool HasAnyOfFlags(FlagsType aFlags) const { return HasFlag(aFlags); }
273 // This can also be called from stylo, in the sequential part of the
274 // traversal, though it's probably not worth differentiating them for the
275 // purposes of assertions.
276 bool HasAllFlags(FlagsType aFlags) const {
277 MOZ_ASSERT((aFlags & kWrapperFlagsMask) == 0, "Bad flag mask");
278 return (__atomic_load_n(mFlags.AsPtr(), __ATOMIC_RELAXED) & aFlags) ==
279 aFlags;
282 void SetFlags(FlagsType aFlagsToSet) {
283 MOZ_ASSERT(NS_IsMainThread());
284 MOZ_ASSERT((aFlagsToSet & kWrapperFlagsMask) == 0, "Bad flag mask");
285 mFlags.Set(mFlags.Get() | aFlagsToSet);
288 void UnsetFlags(FlagsType aFlagsToUnset) {
289 MOZ_ASSERT(NS_IsMainThread());
290 MOZ_ASSERT((aFlagsToUnset & kWrapperFlagsMask) == 0, "Bad flag mask");
291 mFlags.Set(mFlags.Get() & ~aFlagsToUnset);
294 void PreserveWrapper(nsISupports* aScriptObjectHolder) {
295 if (PreservingWrapper()) {
296 return;
299 nsISupports* ccISupports;
300 aScriptObjectHolder->QueryInterface(NS_GET_IID(nsCycleCollectionISupports),
301 reinterpret_cast<void**>(&ccISupports));
302 MOZ_ASSERT(ccISupports);
304 nsXPCOMCycleCollectionParticipant* participant;
305 CallQueryInterface(ccISupports, &participant);
306 PreserveWrapper(ccISupports, participant);
309 void PreserveWrapper(void* aScriptObjectHolder,
310 nsScriptObjectTracer* aTracer) {
311 if (PreservingWrapper()) {
312 return;
315 JSObject* wrapper = GetWrapper(); // Read barrier for incremental GC.
316 HoldJSObjects(aScriptObjectHolder, aTracer, JS::GetObjectZone(wrapper));
317 SetPreservingWrapper(true);
318 #ifdef DEBUG
319 // Make sure the cycle collector will be able to traverse to the wrapper.
320 CheckCCWrapperTraversal(aScriptObjectHolder, aTracer);
321 #endif
324 void ReleaseWrapper(void* aScriptObjectHolder);
326 void TraceWrapper(JSTracer* aTrc, const char* name) {
327 if (mWrapper) {
328 js::UnsafeTraceManuallyBarrieredEdge(aTrc, &mWrapper, name);
332 protected:
333 void PoisonWrapper() {
334 if (mWrapper) {
335 // Set the pointer to a value that will cause a crash if it is
336 // dereferenced.
337 mWrapper = reinterpret_cast<JSObject*>(1);
341 private:
342 void SetWrapperJSObject(JSObject* aWrapper);
344 // We'd like to assert that these aren't used from servo threads, but we don't
345 // have a great way to do that because:
346 // * We can't just assert that they get used on the main thread, because
347 // these are used from workers.
348 // * We can't just assert that they aren't used when IsInServoTraversal(),
349 // because the traversal has a sequential, main-thread-only phase, where we
350 // run animations that can fiddle with JS promises.
351 FlagsType GetWrapperFlags() const { return mFlags.Get() & kWrapperFlagsMask; }
353 bool HasWrapperFlag(FlagsType aFlag) const {
354 MOZ_ASSERT((aFlag & ~kWrapperFlagsMask) == 0, "Bad wrapper flag bits");
355 return !!(mFlags.Get() & aFlag);
358 void SetWrapperFlags(FlagsType aFlagsToSet) {
359 MOZ_ASSERT((aFlagsToSet & ~kWrapperFlagsMask) == 0,
360 "Bad wrapper flag bits");
361 mFlags.Set(mFlags.Get() | aFlagsToSet);
364 void UnsetWrapperFlags(FlagsType aFlagsToUnset) {
365 MOZ_ASSERT((aFlagsToUnset & ~kWrapperFlagsMask) == 0,
366 "Bad wrapper flag bits");
367 mFlags.Set(mFlags.Get() & ~aFlagsToUnset);
370 void HoldJSObjects(void* aScriptObjectHolder, nsScriptObjectTracer* aTracer,
371 JS::Zone* aZone);
373 #ifdef DEBUG
374 public:
375 void CheckCCWrapperTraversal(void* aScriptObjectHolder,
376 nsScriptObjectTracer* aTracer);
378 private:
379 #endif // DEBUG
382 * If this bit is set then we're preserving the wrapper, which in effect ties
383 * the lifetime of the JS object stored in the cache to the lifetime of the
384 * native object. We rely on the cycle collector to break the cycle that this
385 * causes between the native object and the JS object, so it is important that
386 * any native object that supports preserving of its wrapper
387 * traces/traverses/unlinks the cached JS object (see
388 * NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER and
389 * NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER).
391 enum { WRAPPER_BIT_PRESERVED = 1 << 0 };
393 enum { kWrapperFlagsMask = WRAPPER_BIT_PRESERVED };
395 JSObject* mWrapper = nullptr;
397 // Rust code needs to read and write some flags atomically, but we don't want
398 // to make the wrapper flags atomic whole-sale because main-thread code would
399 // become more expensive (loads wouldn't change, but flag setting /
400 // unsetting could become slower enough to be noticeable). Making this an
401 // Atomic whole-sale needs more measuring.
403 // In order to not mess with aliasing rules the type should not be frozen, so
404 // we use a RustCell, which contains an UnsafeCell internally. See also the
405 // handling of ServoData (though that's a bit different).
406 mozilla::RustCell<FlagsType> mFlags{0};
408 protected:
409 #ifdef BOOL_FLAGS_ON_WRAPPER_CACHE
410 uint32_t mBoolFlags = 0;
411 #endif
414 enum { WRAPPER_CACHE_FLAGS_BITS_USED = 1 };
416 NS_DEFINE_STATIC_IID_ACCESSOR(nsWrapperCache, NS_WRAPPERCACHE_IID)
418 #define NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY \
419 if (aIID.Equals(NS_GET_IID(nsWrapperCache))) { \
420 *aInstancePtr = static_cast<nsWrapperCache*>(this); \
421 return NS_OK; \
424 #define NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY \
425 NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY \
426 else
428 // Cycle collector macros for wrapper caches.
430 // The NS_DECL_*WRAPPERCACHE_* macros make it easier to mark classes as holding
431 // just a single pointer to a JS value. That information is then used for
432 // certain GC optimizations.
434 #define NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS_AMBIGUOUS(_class, _base) \
435 class NS_CYCLE_COLLECTION_INNERCLASS \
436 : public nsXPCOMCycleCollectionParticipant { \
437 public: \
438 constexpr explicit NS_CYCLE_COLLECTION_INNERCLASS(Flags aFlags = 0) \
439 : nsXPCOMCycleCollectionParticipant(aFlags | \
440 FlagMaybeSingleZoneJSHolder) {} \
442 private: \
443 NS_DECL_CYCLE_COLLECTION_CLASS_BODY(_class, _base) \
444 NS_IMETHOD_(void) \
445 Trace(void* p, const TraceCallbacks& cb, void* closure) override; \
446 NS_IMETHOD_(void) \
447 TraceWrapper(void* aPtr, const TraceCallbacks& aCb, void* aClosure) final; \
448 NS_IMPL_GET_XPCOM_CYCLE_COLLECTION_PARTICIPANT(_class) \
449 }; \
450 NS_CHECK_FOR_RIGHT_PARTICIPANT_IMPL(_class) \
451 static NS_CYCLE_COLLECTION_INNERCLASS NS_CYCLE_COLLECTION_INNERNAME; \
452 NOT_INHERITED_CANT_OVERRIDE
454 #define NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(_class) \
455 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS_AMBIGUOUS(_class, _class)
457 #define NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS_INHERITED(_class, \
458 _base_class) \
459 class NS_CYCLE_COLLECTION_INNERCLASS \
460 : public NS_CYCLE_COLLECTION_CLASSNAME(_base_class) { \
461 public: \
462 constexpr explicit NS_CYCLE_COLLECTION_INNERCLASS(Flags aFlags) \
463 : NS_CYCLE_COLLECTION_CLASSNAME(_base_class)( \
464 aFlags | FlagMaybeSingleZoneJSHolder) {} \
466 private: \
467 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED_BODY(_class, _base_class) \
468 NS_IMETHOD_(void) \
469 Trace(void* p, const TraceCallbacks& cb, void* closure) override; \
470 NS_IMETHOD_(void) \
471 TraceWrapper(void* aPtr, const TraceCallbacks& aCb, void* aClosure) final; \
472 NS_IMPL_GET_XPCOM_CYCLE_COLLECTION_PARTICIPANT(_class) \
473 }; \
474 NS_CHECK_FOR_RIGHT_PARTICIPANT_IMPL_INHERITED(_class) \
475 static NS_CYCLE_COLLECTION_INNERCLASS NS_CYCLE_COLLECTION_INNERNAME;
477 #define NS_DECL_CYCLE_COLLECTION_SKIPPABLE_WRAPPERCACHE_CLASS_AMBIGUOUS( \
478 _class, _base) \
479 class NS_CYCLE_COLLECTION_INNERCLASS \
480 : public nsXPCOMCycleCollectionParticipant { \
481 public: \
482 constexpr explicit NS_CYCLE_COLLECTION_INNERCLASS(Flags aFlags) \
483 : nsXPCOMCycleCollectionParticipant(aFlags | FlagMightSkip | \
484 FlagMaybeSingleZoneJSHolder) {} \
486 private: \
487 NS_DECL_CYCLE_COLLECTION_CLASS_BODY(_class, _base) \
488 NS_IMETHOD_(void) \
489 Trace(void* p, const TraceCallbacks& cb, void* closure) override; \
490 NS_IMETHOD_(void) \
491 TraceWrapper(void* aPtr, const TraceCallbacks& aCb, void* aClosure) final; \
492 NS_IMETHOD_(bool) CanSkipReal(void* p, bool aRemovingAllowed) override; \
493 NS_IMETHOD_(bool) CanSkipInCCReal(void* p) override; \
494 NS_IMETHOD_(bool) CanSkipThisReal(void* p) override; \
495 NS_IMPL_GET_XPCOM_CYCLE_COLLECTION_PARTICIPANT(_class) \
496 }; \
497 NS_CHECK_FOR_RIGHT_PARTICIPANT_IMPL(_class) \
498 static NS_CYCLE_COLLECTION_INNERCLASS NS_CYCLE_COLLECTION_INNERNAME; \
499 NOT_INHERITED_CANT_OVERRIDE
501 #define NS_DECL_CYCLE_COLLECTION_SKIPPABLE_WRAPPERCACHE_CLASS(_class) \
502 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_WRAPPERCACHE_CLASS_AMBIGUOUS(_class, \
503 _class)
505 #define NS_DECL_CYCLE_COLLECTION_SKIPPABLE_WRAPPERCACHE_CLASS_INHERITED( \
506 _class, _base_class) \
507 class NS_CYCLE_COLLECTION_INNERCLASS \
508 : public NS_CYCLE_COLLECTION_CLASSNAME(_base_class) { \
509 public: \
510 constexpr explicit NS_CYCLE_COLLECTION_INNERCLASS(Flags aFlags = 0) \
511 : NS_CYCLE_COLLECTION_CLASSNAME(_base_class)( \
512 aFlags | FlagMightSkip | FlagMaybeSingleZoneJSHolder) {} \
514 private: \
515 NS_DECL_CYCLE_COLLECTION_CLASS_BODY(_class, _base_class) \
516 NS_IMETHOD_(void) \
517 Trace(void* p, const TraceCallbacks& cb, void* closure) override; \
518 NS_IMETHOD_(void) \
519 TraceWrapper(void* aPtr, const TraceCallbacks& aCb, void* aClosure) final; \
520 NS_IMETHOD_(bool) CanSkipReal(void* p, bool aRemovingAllowed) override; \
521 NS_IMETHOD_(bool) CanSkipInCCReal(void* p) override; \
522 NS_IMETHOD_(bool) CanSkipThisReal(void* p) override; \
523 NS_IMPL_GET_XPCOM_CYCLE_COLLECTION_PARTICIPANT(_class) \
524 }; \
525 NS_CHECK_FOR_RIGHT_PARTICIPANT_IMPL_INHERITED(_class) \
526 static NS_CYCLE_COLLECTION_INNERCLASS NS_CYCLE_COLLECTION_INNERNAME;
528 #define NS_DECL_CYCLE_COLLECTION_NATIVE_WRAPPERCACHE_CLASS(_class) \
529 void DeleteCycleCollectable(void) { delete this; } \
530 class NS_CYCLE_COLLECTION_INNERCLASS : public nsScriptObjectTracer { \
531 public: \
532 constexpr explicit NS_CYCLE_COLLECTION_INNERCLASS(Flags aFlags = 0) \
533 : nsScriptObjectTracer(aFlags | FlagMaybeSingleZoneJSHolder) {} \
535 private: \
536 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS_BODY(_class) \
537 NS_IMETHOD_(void) \
538 Trace(void* p, const TraceCallbacks& cb, void* closure) override; \
539 NS_IMETHOD_(void) \
540 TraceWrapper(void* aPtr, const TraceCallbacks& aCb, void* aClosure) final; \
541 static constexpr nsScriptObjectTracer* GetParticipant() { \
542 return &_class::NS_CYCLE_COLLECTION_INNERNAME; \
544 }; \
545 static NS_CYCLE_COLLECTION_INNERCLASS NS_CYCLE_COLLECTION_INNERNAME;
547 #define NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER \
548 tmp->TraceWrapper(aCallbacks, aClosure);
550 #define NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER \
551 tmp->ReleaseWrapper(p);
553 #define NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(_class) \
554 static_assert(std::is_base_of<nsWrapperCache, _class>::value, \
555 "Class should inherit nsWrapperCache"); \
556 NS_IMPL_CYCLE_COLLECTION_SINGLE_ZONE_SCRIPT_HOLDER_CLASS(_class) \
557 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(_class) \
558 TraceWrapper(p, aCallbacks, aClosure); \
559 NS_IMPL_CYCLE_COLLECTION_TRACE_END \
560 void NS_CYCLE_COLLECTION_CLASSNAME(_class)::TraceWrapper( \
561 void* p, const TraceCallbacks& aCallbacks, void* aClosure) { \
562 _class* tmp = DowncastCCParticipant<_class>(p); \
563 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER \
566 #define NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(_class) \
567 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(_class) \
568 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(_class) \
569 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER \
570 NS_IMPL_CYCLE_COLLECTION_UNLINK_END \
571 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(_class) \
572 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
574 #define NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(_class, ...) \
575 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(_class) \
576 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(_class) \
577 NS_IMPL_CYCLE_COLLECTION_UNLINK(__VA_ARGS__) \
578 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER \
579 NS_IMPL_CYCLE_COLLECTION_UNLINK_END \
580 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(_class) \
581 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(__VA_ARGS__) \
582 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
584 #define NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WEAK(_class, ...) \
585 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(_class) \
586 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(_class) \
587 NS_IMPL_CYCLE_COLLECTION_UNLINK(__VA_ARGS__) \
588 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER \
589 NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_REFERENCE \
590 NS_IMPL_CYCLE_COLLECTION_UNLINK_END \
591 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(_class) \
592 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(__VA_ARGS__) \
593 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
595 #define NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WEAK_PTR(_class, ...) \
596 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(_class) \
597 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(_class) \
598 NS_IMPL_CYCLE_COLLECTION_UNLINK(__VA_ARGS__) \
599 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER \
600 NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_PTR \
601 NS_IMPL_CYCLE_COLLECTION_UNLINK_END \
602 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(_class) \
603 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(__VA_ARGS__) \
604 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
606 // This is used for wrapper cached classes that inherit from cycle
607 // collected non-wrapper cached classes.
608 #define NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_INHERITED(_class, _base, ...) \
609 static_assert(!std::is_base_of<nsWrapperCache, _base>::value, \
610 "Base class should not inherit nsWrapperCache"); \
611 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(_class) \
612 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(_class, _base) \
613 NS_IMPL_CYCLE_COLLECTION_UNLINK(__VA_ARGS__) \
614 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER \
615 NS_IMPL_CYCLE_COLLECTION_UNLINK_END \
616 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(_class, _base) \
617 /* Assert somewhere, in this case in the traverse method, that the */ \
618 /* parent isn't a single zone holder*/ \
619 MOZ_ASSERT(!_base::NS_CYCLE_COLLECTION_INNERNAME.IsSingleZoneJSHolder()); \
620 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(__VA_ARGS__) \
621 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
623 // if NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WITH_JS_MEMBERS is used to implement
624 // a wrappercache class, one needs to use
625 // NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS and its variants in the class
626 // declaration.
627 #define NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WITH_JS_MEMBERS( \
628 class_, native_members_, js_members_) \
629 static_assert(std::is_base_of<nsWrapperCache, class_>::value, \
630 "Class should inherit nsWrapperCache"); \
631 NS_IMPL_CYCLE_COLLECTION_CLASS(class_) \
632 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(class_) \
633 using ::ImplCycleCollectionUnlink; \
634 NS_IMPL_CYCLE_COLLECTION_UNLINK( \
635 MOZ_FOR_EACH_EXPAND_HELPER native_members_) \
636 NS_IMPL_CYCLE_COLLECTION_UNLINK(MOZ_FOR_EACH_EXPAND_HELPER js_members_) \
637 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER \
638 NS_IMPL_CYCLE_COLLECTION_UNLINK_END \
639 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(class_) \
640 NS_IMPL_CYCLE_COLLECTION_TRAVERSE( \
641 MOZ_FOR_EACH_EXPAND_HELPER native_members_) \
642 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END \
643 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(class_) \
644 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBERS( \
645 MOZ_FOR_EACH_EXPAND_HELPER js_members_) \
646 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER \
647 NS_IMPL_CYCLE_COLLECTION_TRACE_END
649 #endif /* nsWrapperCache_h___ */