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"
14 namespace mozilla::dom
{
17 * Class that just handles the JSObject storage and tracing for spidermonkey
20 struct SpiderMonkeyInterfaceObjectStorage
{
23 JSObject
* mWrappedObj
;
25 SpiderMonkeyInterfaceObjectStorage()
26 : mImplObj(nullptr), mWrappedObj(nullptr) {}
28 SpiderMonkeyInterfaceObjectStorage(
29 SpiderMonkeyInterfaceObjectStorage
&& aOther
)
30 : mImplObj(aOther
.mImplObj
), mWrappedObj(aOther
.mWrappedObj
) {
31 aOther
.mImplObj
= nullptr;
32 aOther
.mWrappedObj
= nullptr;
36 inline void TraceSelf(JSTracer
* trc
) {
37 JS::TraceRoot(trc
, &mImplObj
,
38 "SpiderMonkeyInterfaceObjectStorage.mImplObj");
39 JS::TraceRoot(trc
, &mWrappedObj
,
40 "SpiderMonkeyInterfaceObjectStorage.mWrappedObj");
43 inline bool inited() const { return !!mImplObj
; }
45 inline bool WrapIntoNewCompartment(JSContext
* cx
) {
47 cx
, JS::MutableHandle
<JSObject
*>::fromMarkedLocation(&mWrappedObj
));
50 inline JSObject
* Obj() const {
56 SpiderMonkeyInterfaceObjectStorage(
57 const SpiderMonkeyInterfaceObjectStorage
&) = delete;
60 // A class for rooting an existing SpiderMonkey Interface struct
61 template <typename InterfaceType
>
62 class MOZ_RAII SpiderMonkeyInterfaceRooter
: private JS::CustomAutoRooter
{
64 template <typename CX
>
65 SpiderMonkeyInterfaceRooter(const CX
& cx
, InterfaceType
* aInterface
)
66 : JS::CustomAutoRooter(cx
), mInterface(aInterface
) {}
68 virtual void trace(JSTracer
* trc
) override
{ mInterface
->TraceSelf(trc
); }
71 SpiderMonkeyInterfaceObjectStorage
* const mInterface
;
74 // And a specialization for dealing with nullable SpiderMonkey interfaces
75 template <typename Inner
>
77 template <typename InterfaceType
>
78 class MOZ_RAII SpiderMonkeyInterfaceRooter
<Nullable
<InterfaceType
>>
79 : private JS::CustomAutoRooter
{
81 template <typename CX
>
82 SpiderMonkeyInterfaceRooter(const CX
& cx
, Nullable
<InterfaceType
>* aInterface
)
83 : JS::CustomAutoRooter(cx
), mInterface(aInterface
) {}
85 virtual void trace(JSTracer
* trc
) override
{
86 if (!mInterface
->IsNull()) {
87 mInterface
->Value().TraceSelf(trc
);
92 Nullable
<InterfaceType
>* const mInterface
;
95 // Class for easily setting up a rooted SpiderMonkey interface object on the
97 template <typename InterfaceType
>
98 class MOZ_RAII RootedSpiderMonkeyInterface final
99 : public InterfaceType
,
100 private SpiderMonkeyInterfaceRooter
<InterfaceType
> {
102 template <typename CX
>
103 explicit RootedSpiderMonkeyInterface(const CX
& cx
)
104 : InterfaceType(), SpiderMonkeyInterfaceRooter
<InterfaceType
>(cx
, this) {}
106 template <typename CX
>
107 RootedSpiderMonkeyInterface(const CX
& cx
, JSObject
* obj
)
108 : InterfaceType(obj
),
109 SpiderMonkeyInterfaceRooter
<InterfaceType
>(cx
, this) {}
112 } // namespace mozilla::dom
114 #endif /* mozilla_dom_SpiderMonkeyInterface_h */