1 ## Persistent references
3 An object reference that is independent of any `HandleScope` is a _persistent_ reference. Where a `Local` handle only lives as long as the `HandleScope` in which it was allocated, a `Persistent` handle remains valid until it is explicitly disposed.
5 Due to the evolution of the V8 API, it is necessary for NAN to provide a wrapper implementation of the `Persistent` classes to supply compatibility across the V8 versions supported.
7 - <a href="#api_nan_persistent_base"><b><code>Nan::PersistentBase & v8::PersistentBase</code></b></a>
8 - <a href="#api_nan_non_copyable_persistent_traits"><b><code>Nan::NonCopyablePersistentTraits & v8::NonCopyablePersistentTraits</code></b></a>
9 - <a href="#api_nan_copyable_persistent_traits"><b><code>Nan::CopyablePersistentTraits & v8::CopyablePersistentTraits</code></b></a>
10 - <a href="#api_nan_persistent"><b><code>Nan::Persistent</code></b></a>
11 - <a href="#api_nan_global"><b><code>Nan::Global</code></b></a>
12 - <a href="#api_nan_weak_callback_info"><b><code>Nan::WeakCallbackInfo</code></b></a>
13 - <a href="#api_nan_weak_callback_type"><b><code>Nan::WeakCallbackType</code></b></a>
15 Also see the V8 Embedders Guide section on [Handles and Garbage Collection](https://developers.google.com/v8/embed#handles).
17 <a name="api_nan_persistent_base"></a>
18 ### Nan::PersistentBase & v8::PersistentBase
20 A persistent handle contains a reference to a storage cell in V8 which holds an object value and which is updated by the garbage collector whenever the object is moved. A new storage cell can be created using the constructor or `Nan::PersistentBase::Reset()`. Existing handles can be disposed using an argument-less `Nan::PersistentBase::Reset()`.
24 _(note: this is implemented as `Nan::PersistentBase` for older versions of V8 and the native `v8::PersistentBase` is used for newer versions of V8)_
27 template<typename T> class PersistentBase {
30 * If non-empty, destroy the underlying storage cell
35 * If non-empty, destroy the underlying storage cell and create a new one with
36 * the contents of another if it is also non-empty
38 template<typename S> void Reset(const v8::Local<S> &other);
41 * If non-empty, destroy the underlying storage cell and create a new one with
42 * the contents of another if it is also non-empty
44 template<typename S> void Reset(const PersistentBase<S> &other);
47 * If non-empty, destroy the underlying storage cell
48 * IsEmpty() will return true after this call.
54 template<typename S> bool operator==(const PersistentBase<S> &that);
56 template<typename S> bool operator==(const v8::Local<S> &that);
58 template<typename S> bool operator!=(const PersistentBase<S> &that);
60 template<typename S> bool operator!=(const v8::Local<S> &that);
63 * Install a finalization callback on this object.
64 * NOTE: There is no guarantee as to *when* or even *if* the callback is
65 * invoked. The invocation is performed solely on a best effort basis.
66 * As always, GC-based finalization should *not* be relied upon for any
67 * critical form of resource management! At the moment you can either
68 * specify a parameter for the callback or the location of two internal
69 * fields in the dying object.
72 void SetWeak(P *parameter,
73 typename WeakCallbackInfo<P>::Callback callback,
74 WeakCallbackType type);
79 * Marks the reference to this object independent. Garbage collector is free
80 * to ignore any object groups containing this object. Weak callback for an
81 * independent handle should not assume that it will be preceded by a global
82 * GC prologue callback or followed by a global GC epilogue callback.
84 void MarkIndependent() const;
86 bool IsIndependent() const;
88 /** Checks if the handle holds the only reference to an object. */
89 bool IsNearDeath() const;
91 /** Returns true if the handle's reference is weak. */
96 See the V8 documentation for [`PersistentBase`](https://v8docs.nodesource.com/io.js-3.0/d4/dca/classv8_1_1_persistent_base.html) for further information.
98 **Tip:** To get a `v8::Local` reference to the original object back from a `PersistentBase` or `Persistent` object:
101 v8::Local<v8::Object> object = Nan::New(persistent);
104 <a name="api_nan_non_copyable_persistent_traits"></a>
105 ### Nan::NonCopyablePersistentTraits & v8::NonCopyablePersistentTraits
107 Default traits for `Nan::Persistent`. This class does not allow use of the a copy constructor or assignment operator. At present `kResetInDestructor` is not set, but that will change in a future version.
111 _(note: this is implemented as `Nan::NonCopyablePersistentTraits` for older versions of V8 and the native `v8::NonCopyablePersistentTraits` is used for newer versions of V8)_
114 template<typename T> class NonCopyablePersistentTraits {
116 typedef Persistent<T, NonCopyablePersistentTraits<T> > NonCopyablePersistent;
118 static const bool kResetInDestructor = false;
120 template<typename S, typename M>
121 static void Copy(const Persistent<S, M> &source,
122 NonCopyablePersistent *dest);
124 template<typename O> static void Uncompilable();
128 See the V8 documentation for [`NonCopyablePersistentTraits`](https://v8docs.nodesource.com/io.js-3.0/de/d73/classv8_1_1_non_copyable_persistent_traits.html) for further information.
130 <a name="api_nan_copyable_persistent_traits"></a>
131 ### Nan::CopyablePersistentTraits & v8::CopyablePersistentTraits
133 A helper class of traits to allow copying and assignment of `Persistent`. This will clone the contents of storage cell, but not any of the flags, etc..
137 _(note: this is implemented as `Nan::CopyablePersistentTraits` for older versions of V8 and the native `v8::NonCopyablePersistentTraits` is used for newer versions of V8)_
141 class CopyablePersistentTraits {
143 typedef Persistent<T, CopyablePersistentTraits<T> > CopyablePersistent;
145 static const bool kResetInDestructor = true;
147 template<typename S, typename M>
148 static void Copy(const Persistent<S, M> &source,
149 CopyablePersistent *dest);
153 See the V8 documentation for [`CopyablePersistentTraits`](https://v8docs.nodesource.com/io.js-3.0/da/d5c/structv8_1_1_copyable_persistent_traits.html) for further information.
155 <a name="api_nan_persistent"></a>
158 A type of `PersistentBase` which allows copy and assignment. Copy, assignment and destructor behavior is controlled by the traits class `M`.
163 template<typename T, typename M = NonCopyablePersistentTraits<T> >
166 template<typename T, typename M> class Persistent : public PersistentBase<T> {
169 * A Persistent with no storage cell.
174 * Construct a Persistent from a v8::Local. When the v8::Local is non-empty, a
175 * new storage cell is created pointing to the same object, and no flags are
178 template<typename S> Persistent(v8::Local<S> that);
181 * Construct a Persistent from a Persistent. When the Persistent is non-empty,
182 * a new storage cell is created pointing to the same object, and no flags are
185 Persistent(const Persistent &that);
188 * The copy constructors and assignment operator create a Persistent exactly
189 * as the Persistent constructor, but the Copy function from the traits class
190 * is called, allowing the setting of flags based on the copied Persistent.
192 Persistent &operator=(const Persistent &that);
194 template <typename S, typename M2>
195 Persistent &operator=(const Persistent<S, M2> &that);
198 * The destructor will dispose the Persistent based on the kResetInDestructor
199 * flags in the traits class. Since not calling dispose can result in a
200 * memory leak, it is recommended to always set this flag.
206 See the V8 documentation for [`Persistent`](https://v8docs.nodesource.com/io.js-3.0/d2/d78/classv8_1_1_persistent.html) for further information.
208 <a name="api_nan_global"></a>
211 A type of `PersistentBase` which has move semantics.
214 template<typename T> class Global : public PersistentBase<T> {
217 * A Global with no storage cell.
222 * Construct a Global from a v8::Local. When the v8::Local is non-empty, a new
223 * storage cell is created pointing to the same object, and no flags are set.
225 template<typename S> Global(v8::Local<S> that);
227 * Construct a Global from a PersistentBase. When the Persistent is non-empty,
228 * a new storage cell is created pointing to the same object, and no flags are
231 template<typename S> Global(const PersistentBase<S> &that);
234 * Pass allows returning globals from functions, etc.
240 See the V8 documentation for [`Global`](https://v8docs.nodesource.com/io.js-3.0/d5/d40/classv8_1_1_global.html) for further information.
242 <a name="api_nan_weak_callback_info"></a>
243 ### Nan::WeakCallbackInfo
245 `Nan::WeakCallbackInfo` is used as an argument when setting a persistent reference as weak. You may need to free any external resources attached to the object. It is a mirror of `v8:WeakCallbackInfo` as found in newer versions of V8.
250 template<typename T> class WeakCallbackInfo {
252 typedef void (*Callback)(const WeakCallbackInfo<T>& data);
254 v8::Isolate *GetIsolate() const;
257 * Get the parameter that was associated with the weak handle.
259 T *GetParameter() const;
262 * Get pointer from internal field, index can be 0 or 1.
264 void *GetInternalField(int index) const;
271 void weakCallback(const WeakCallbackInfo<int> &data) {
272 int *parameter = data.GetParameter();
276 Persistent<v8::Object> obj;
277 int *data = new int(0);
278 obj.SetWeak(data, callback, WeakCallbackType::kParameter);
281 See the V8 documentation for [`WeakCallbackInfo`](https://v8docs.nodesource.com/io.js-3.0/d8/d06/classv8_1_1_weak_callback_info.html) for further information.
283 <a name="api_nan_weak_callback_type"></a>
284 ### Nan::WeakCallbackType
286 Represents the type of a weak callback.
287 A weak callback of type `kParameter` makes the supplied parameter to `Nan::PersistentBase::SetWeak` available through `WeakCallbackInfo::GetParameter`.
288 A weak callback of type `kInternalFields` uses up to two internal fields at indices 0 and 1 on the `Nan::PersistentBase<v8::Object>` being made weak.
289 Note that only `v8::Object`s and derivatives can have internal fields.
294 enum class WeakCallbackType { kParameter, kInternalFields };