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"
14 class nsScriptObjectTracer
;
20 // Only HoldJSObjects and DropJSObjects should be called directly.
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
); }
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
));
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
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.
63 void HoldJSObjects(T
* aHolder
) {
64 HoldDropJSObjectsHelper
<T
>::Hold(aHolder
);
68 void DropJSObjects(T
* aHolder
) {
69 HoldDropJSObjectsHelper
<T
>::Drop(aHolder
);
72 } // namespace mozilla
74 #endif // mozilla_HoldDropJSObjects_h