Bug 1690340 - Part 4: Insert the "Page Source" before the "Extensions for Developers...
[gecko.git] / xpcom / base / HoldDropJSObjects.h
blob8cc3c137c61f2d3084b62a2e42eaae9c563f718b
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 mozilla_HoldDropJSObjects_h
8 #define mozilla_HoldDropJSObjects_h
10 #include <type_traits>
11 #include "nsCycleCollectionNoteChild.h"
13 class nsISupports;
14 class nsScriptObjectTracer;
16 namespace JS {
17 class Zone;
20 // Only HoldJSObjects and DropJSObjects should be called directly.
22 namespace mozilla {
23 namespace cyclecollector {
25 void HoldJSObjectsImpl(void* aHolder, nsScriptObjectTracer* aTracer,
26 JS::Zone* aZone = nullptr);
27 void HoldJSObjectsImpl(nsISupports* aHolder);
28 void DropJSObjectsImpl(void* aHolder);
29 void DropJSObjectsImpl(nsISupports* aHolder);
31 } // namespace cyclecollector
33 template <class T, bool isISupports = std::is_base_of<nsISupports, T>::value>
34 struct HoldDropJSObjectsHelper {
35 static void Hold(T* aHolder) {
36 cyclecollector::HoldJSObjectsImpl(aHolder,
37 NS_CYCLE_COLLECTION_PARTICIPANT(T));
39 static void Drop(T* aHolder) { cyclecollector::DropJSObjectsImpl(aHolder); }
42 template <class T>
43 struct HoldDropJSObjectsHelper<T, true> {
44 static void Hold(T* aHolder) {
45 cyclecollector::HoldJSObjectsImpl(ToSupports(aHolder));
47 static void Drop(T* aHolder) {
48 cyclecollector::DropJSObjectsImpl(ToSupports(aHolder));
52 /**
53 Classes that hold strong references to JS GC things such as `JSObjects` and
54 `JS::Values` (e.g. `JS::Heap<JSObject*> mFoo;`) must use these, generally by
55 calling `HoldJSObjects(this)` and `DropJSObjects(this)` in the ctor and dtor
56 respectively.
58 For classes that are wrapper cached and hold no other strong references to JS
59 GC things, there's no need to call these; it will be taken care of
60 automatically by nsWrapperCache.
61 **/
62 template <class T>
63 void HoldJSObjects(T* aHolder) {
64 HoldDropJSObjectsHelper<T>::Hold(aHolder);
67 template <class T>
68 void DropJSObjects(T* aHolder) {
69 HoldDropJSObjectsHelper<T>::Drop(aHolder);
72 } // namespace mozilla
74 #endif // mozilla_HoldDropJSObjects_h