| 49 | |
| 50 | |
| 51 | ==== Example |
| 52 | Check the attached main.cpp for a full working example (including the base class). |
| 53 | |
| 54 | {{{ |
| 55 | #!c++ |
| 56 | #ifndef HEAPROOTINGTEST_H_INCLUDED |
| 57 | #define HEAPROOTINGTEST_H_INCLUDED |
| 58 | |
| 59 | using namespace std; |
| 60 | |
| 61 | class CHeapRootingTest : CTestClassBase |
| 62 | { |
| 63 | public: |
| 64 | |
| 65 | CHeapRootingTest(JSRuntime* rt, JSContext* cx) : CTestClassBase(rt, cx) |
| 66 | { |
| 67 | m_HeapValue.setObject(*InitObjectInternal()); |
| 68 | JS::AddValueRoot(m_cx, &m_HeapValue); |
| 69 | } |
| 70 | |
| 71 | ~CHeapRootingTest() |
| 72 | { |
| 73 | JSAutoRequest rq(m_cx); |
| 74 | JS::RemoveValueRoot(m_cx, &m_HeapValue); |
| 75 | } |
| 76 | |
| 77 | void PrintUsecount() |
| 78 | { |
| 79 | JSAutoRequest rq(m_cx); |
| 80 | if (!m_HeapValue.isObject()) |
| 81 | cerr << "Error: m_HeapValue is not an object!" << endl; |
| 82 | |
| 83 | JS::RootedObject objStack(m_cx, &m_HeapValue.toObject()); |
| 84 | PrintUsecountBase(objStack); |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | JS::Heap<JS::Value> m_HeapValue; |
| 89 | }; |
| 90 | |
| 91 | #endif // HEAPROOTINGTEST_H_INCLUDED |
| 92 | |
| 93 | }}} |
| 107 | ==== Example |
| 108 | Check the attached main.cpp for a full working example (including the base class). |
| 109 | |
| 110 | {{{ |
| 111 | #!c++ |
| 112 | #ifndef HEAPROOTINGTEST_H_INCLUDED |
| 113 | #define HEAPROOTINGTEST_H_INCLUDED |
| 114 | |
| 115 | class CHeapRootingTest : CTestClassBase |
| 116 | { |
| 117 | public: |
| 118 | |
| 119 | static void Trace(JSTracer *trc, void *data) |
| 120 | { |
| 121 | reinterpret_cast<CHeapRootingTest*>(data)->TraceMember(trc); |
| 122 | } |
| 123 | |
| 124 | void TraceMember(JSTracer *trc) |
| 125 | { |
| 126 | // The JS_Call<T>Tracer family of functions (those without the "Heap" in the name) will be deprecate in the future. |
| 127 | JS_CallHeapValueTracer(trc, &m_HeapValue, "m_HeapValue"); |
| 128 | } |
| 129 | |
| 130 | CHeapRootingTest(JSRuntime* rt, JSContext* cx) : CTestClassBase(rt, cx) |
| 131 | { |
| 132 | m_HeapValue.setObject(*InitObjectInternal()); |
| 133 | JS_AddExtraGCRootsTracer(rt, CHeapRootingTest::Trace, this); |
| 134 | } |
| 135 | |
| 136 | ~CHeapRootingTest() |
| 137 | { |
| 138 | JS_RemoveExtraGCRootsTracer(m_rt, CHeapRootingTest::Trace, this); |
| 139 | } |
| 140 | |
| 141 | void PrintUsecount() |
| 142 | { |
| 143 | JSAutoRequest rq(m_cx); |
| 144 | if (!m_HeapValue.isObject()) |
| 145 | cerr << "Error: m_HeapValue is not an object!" << endl; |
| 146 | JS::RootedObject objStack(m_cx, &m_HeapValue.toObject()); |
| 147 | PrintUsecountBase(objStack); |
| 148 | } |
| 149 | |
| 150 | private: |
| 151 | JS::Heap<JS::Value> m_HeapValue; |
| 152 | }; |
| 153 | |
| 154 | #endif // HEAPROOTINGTEST_H_INCLUDED |
| 155 | }}} |
| 156 | |
| 167 | ==== Example |
| 168 | Check the attached main.cpp for a full working example (including the base class). |
| 169 | |
| 170 | {{{ |
| 171 | #!c++ |
| 172 | #ifndef HEAPROOTINGTEST_H_INCLUDED |
| 173 | #define HEAPROOTINGTEST_H_INCLUDED |
| 174 | |
| 175 | class CHeapRootingTest : CTestClassBase |
| 176 | { |
| 177 | public: |
| 178 | |
| 179 | CHeapRootingTest(JSRuntime* rt, JSContext* cx) : m_HeapValue(cx), CTestClassBase(rt, cx) |
| 180 | { |
| 181 | m_HeapValue = JS::ObjectValue(*InitObjectInternal()); |
| 182 | } |
| 183 | |
| 184 | void PrintUsecount() |
| 185 | { |
| 186 | if (!m_HeapValue.get().isObject()) |
| 187 | cerr << "Error: m_HeapValue is not an object!" << endl; |
| 188 | JS::RootedObject objStack(m_cx, &m_HeapValue.get().toObject()); |
| 189 | PrintUsecountBase(objStack); |
| 190 | } |
| 191 | |
| 192 | private: |
| 193 | JS::PersistentRootedValue m_HeapValue; |
| 194 | }; |
| 195 | |
| 196 | #endif // HEAPROOTINGTEST_H_INCLUDED |
| 197 | }}} |