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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_SpiderMonkeyInterface_h
8 #define mozilla_dom_SpiderMonkeyInterface_h
11 #include "js/RootingAPI.h"
12 #include "js/TracingAPI.h"
18 * Class that just handles the JSObject storage and tracing for spidermonkey
21 struct SpiderMonkeyInterfaceObjectStorage
{
24 JSObject
* mWrappedObj
;
26 SpiderMonkeyInterfaceObjectStorage()
27 : mImplObj(nullptr), mWrappedObj(nullptr) {}
29 SpiderMonkeyInterfaceObjectStorage(
30 SpiderMonkeyInterfaceObjectStorage
&& aOther
)
31 : mImplObj(aOther
.mImplObj
), mWrappedObj(aOther
.mWrappedObj
) {
32 aOther
.mImplObj
= nullptr;
33 aOther
.mWrappedObj
= nullptr;
37 inline void TraceSelf(JSTracer
* trc
) {
38 JS::TraceRoot(trc
, &mImplObj
,
39 "SpiderMonkeyInterfaceObjectStorage.mImplObj");
40 JS::TraceRoot(trc
, &mWrappedObj
,
41 "SpiderMonkeyInterfaceObjectStorage.mWrappedObj");
44 inline bool inited() const { return !!mImplObj
; }
46 inline bool WrapIntoNewCompartment(JSContext
* cx
) {
48 cx
, JS::MutableHandle
<JSObject
*>::fromMarkedLocation(&mWrappedObj
));
51 inline JSObject
* Obj() const {
57 SpiderMonkeyInterfaceObjectStorage(
58 const SpiderMonkeyInterfaceObjectStorage
&) = delete;
61 // A class for rooting an existing SpiderMonkey Interface struct
62 template <typename InterfaceType
>
63 class MOZ_RAII SpiderMonkeyInterfaceRooter
: private JS::CustomAutoRooter
{
65 template <typename CX
>
66 SpiderMonkeyInterfaceRooter(const CX
& cx
, InterfaceType
* aInterface
)
67 : JS::CustomAutoRooter(cx
), mInterface(aInterface
) {}
69 virtual void trace(JSTracer
* trc
) override
{ mInterface
->TraceSelf(trc
); }
72 SpiderMonkeyInterfaceObjectStorage
* const mInterface
;
75 // And a specialization for dealing with nullable SpiderMonkey interfaces
76 template <typename Inner
>
78 template <typename InterfaceType
>
79 class MOZ_RAII SpiderMonkeyInterfaceRooter
<Nullable
<InterfaceType
>>
80 : private JS::CustomAutoRooter
{
82 template <typename CX
>
83 SpiderMonkeyInterfaceRooter(const CX
& cx
, Nullable
<InterfaceType
>* aInterface
)
84 : JS::CustomAutoRooter(cx
), mInterface(aInterface
) {}
86 virtual void trace(JSTracer
* trc
) override
{
87 if (!mInterface
->IsNull()) {
88 mInterface
->Value().TraceSelf(trc
);
93 Nullable
<InterfaceType
>* const mInterface
;
96 // Class for easily setting up a rooted SpiderMonkey interface object on the
98 template <typename InterfaceType
>
99 class MOZ_RAII RootedSpiderMonkeyInterface final
100 : public InterfaceType
,
101 private SpiderMonkeyInterfaceRooter
<InterfaceType
> {
103 template <typename CX
>
104 explicit RootedSpiderMonkeyInterface(const CX
& cx
)
105 : InterfaceType(), SpiderMonkeyInterfaceRooter
<InterfaceType
>(cx
, this) {}
107 template <typename CX
>
108 RootedSpiderMonkeyInterface(const CX
& cx
, JSObject
* obj
)
109 : InterfaceType(obj
),
110 SpiderMonkeyInterfaceRooter
<InterfaceType
>(cx
, this) {}
114 } // namespace mozilla
116 #endif /* mozilla_dom_SpiderMonkeyInterface_h */