Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

persistent.md 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. ## Persistent references
  2. 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.
  3. 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.
  4. - <a href="#api_nan_persistent_base"><b><code>Nan::PersistentBase & v8::PersistentBase</code></b></a>
  5. - <a href="#api_nan_non_copyable_persistent_traits"><b><code>Nan::NonCopyablePersistentTraits & v8::NonCopyablePersistentTraits</code></b></a>
  6. - <a href="#api_nan_copyable_persistent_traits"><b><code>Nan::CopyablePersistentTraits & v8::CopyablePersistentTraits</code></b></a>
  7. - <a href="#api_nan_persistent"><b><code>Nan::Persistent</code></b></a>
  8. - <a href="#api_nan_global"><b><code>Nan::Global</code></b></a>
  9. - <a href="#api_nan_weak_callback_info"><b><code>Nan::WeakCallbackInfo</code></b></a>
  10. - <a href="#api_nan_weak_callback_type"><b><code>Nan::WeakCallbackType</code></b></a>
  11. Also see the V8 Embedders Guide section on [Handles and Garbage Collection](https://developers.google.com/v8/embed#handles).
  12. <a name="api_nan_persistent_base"></a>
  13. ### Nan::PersistentBase & v8::PersistentBase
  14. 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()`.
  15. Definition:
  16. _(note: this is implemented as `Nan::PersistentBase` for older versions of V8 and the native `v8::PersistentBase` is used for newer versions of V8)_
  17. ```c++
  18. template<typename T> class PersistentBase {
  19. public:
  20. /**
  21. * If non-empty, destroy the underlying storage cell
  22. */
  23. void Reset();
  24. /**
  25. * If non-empty, destroy the underlying storage cell and create a new one with
  26. * the contents of another if it is also non-empty
  27. */
  28. template<typename S> void Reset(const v8::Local<S> &other);
  29. /**
  30. * If non-empty, destroy the underlying storage cell and create a new one with
  31. * the contents of another if it is also non-empty
  32. */
  33. template<typename S> void Reset(const PersistentBase<S> &other);
  34. /** Returns true if the handle is empty. */
  35. bool IsEmpty() const;
  36. /**
  37. * If non-empty, destroy the underlying storage cell
  38. * IsEmpty() will return true after this call.
  39. */
  40. void Empty();
  41. template<typename S> bool operator==(const PersistentBase<S> &that);
  42. template<typename S> bool operator==(const v8::Local<S> &that);
  43. template<typename S> bool operator!=(const PersistentBase<S> &that);
  44. template<typename S> bool operator!=(const v8::Local<S> &that);
  45. /**
  46. * Install a finalization callback on this object.
  47. * NOTE: There is no guarantee as to *when* or even *if* the callback is
  48. * invoked. The invocation is performed solely on a best effort basis.
  49. * As always, GC-based finalization should *not* be relied upon for any
  50. * critical form of resource management! At the moment you can either
  51. * specify a parameter for the callback or the location of two internal
  52. * fields in the dying object.
  53. */
  54. template<typename P>
  55. void SetWeak(P *parameter,
  56. typename WeakCallbackInfo<P>::Callback callback,
  57. WeakCallbackType type);
  58. void ClearWeak();
  59. /**
  60. * Marks the reference to this object independent. Garbage collector is free
  61. * to ignore any object groups containing this object. Weak callback for an
  62. * independent handle should not assume that it will be preceded by a global
  63. * GC prologue callback or followed by a global GC epilogue callback.
  64. */
  65. void MarkIndependent() const;
  66. bool IsIndependent() const;
  67. /** Checks if the handle holds the only reference to an object. */
  68. bool IsNearDeath() const;
  69. /** Returns true if the handle's reference is weak. */
  70. bool IsWeak() const
  71. };
  72. ```
  73. See the V8 documentation for [`PersistentBase`](https://v8docs.nodesource.com/node-8.11/d4/dca/classv8_1_1_persistent_base.html) for further information.
  74. **Tip:** To get a `v8::Local` reference to the original object back from a `PersistentBase` or `Persistent` object:
  75. ```c++
  76. v8::Local<v8::Object> object = Nan::New(persistent);
  77. ```
  78. <a name="api_nan_non_copyable_persistent_traits"></a>
  79. ### Nan::NonCopyablePersistentTraits & v8::NonCopyablePersistentTraits
  80. 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.
  81. Definition:
  82. _(note: this is implemented as `Nan::NonCopyablePersistentTraits` for older versions of V8 and the native `v8::NonCopyablePersistentTraits` is used for newer versions of V8)_
  83. ```c++
  84. template<typename T> class NonCopyablePersistentTraits {
  85. public:
  86. typedef Persistent<T, NonCopyablePersistentTraits<T> > NonCopyablePersistent;
  87. static const bool kResetInDestructor = false;
  88. template<typename S, typename M>
  89. static void Copy(const Persistent<S, M> &source,
  90. NonCopyablePersistent *dest);
  91. template<typename O> static void Uncompilable();
  92. };
  93. ```
  94. See the V8 documentation for [`NonCopyablePersistentTraits`](https://v8docs.nodesource.com/node-8.11/de/d73/classv8_1_1_non_copyable_persistent_traits.html) for further information.
  95. <a name="api_nan_copyable_persistent_traits"></a>
  96. ### Nan::CopyablePersistentTraits & v8::CopyablePersistentTraits
  97. 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..
  98. Definition:
  99. _(note: this is implemented as `Nan::CopyablePersistentTraits` for older versions of V8 and the native `v8::NonCopyablePersistentTraits` is used for newer versions of V8)_
  100. ```c++
  101. template<typename T>
  102. class CopyablePersistentTraits {
  103. public:
  104. typedef Persistent<T, CopyablePersistentTraits<T> > CopyablePersistent;
  105. static const bool kResetInDestructor = true;
  106. template<typename S, typename M>
  107. static void Copy(const Persistent<S, M> &source,
  108. CopyablePersistent *dest);
  109. };
  110. ```
  111. See the V8 documentation for [`CopyablePersistentTraits`](https://v8docs.nodesource.com/node-8.11/da/d5c/structv8_1_1_copyable_persistent_traits.html) for further information.
  112. <a name="api_nan_persistent"></a>
  113. ### Nan::Persistent
  114. A type of `PersistentBase` which allows copy and assignment. Copy, assignment and destructor behavior is controlled by the traits class `M`.
  115. Definition:
  116. ```c++
  117. template<typename T, typename M = NonCopyablePersistentTraits<T> >
  118. class Persistent;
  119. template<typename T, typename M> class Persistent : public PersistentBase<T> {
  120. public:
  121. /**
  122. * A Persistent with no storage cell.
  123. */
  124. Persistent();
  125. /**
  126. * Construct a Persistent from a v8::Local. When the v8::Local is non-empty, a
  127. * new storage cell is created pointing to the same object, and no flags are
  128. * set.
  129. */
  130. template<typename S> Persistent(v8::Local<S> that);
  131. /**
  132. * Construct a Persistent from a Persistent. When the Persistent is non-empty,
  133. * a new storage cell is created pointing to the same object, and no flags are
  134. * set.
  135. */
  136. Persistent(const Persistent &that);
  137. /**
  138. * The copy constructors and assignment operator create a Persistent exactly
  139. * as the Persistent constructor, but the Copy function from the traits class
  140. * is called, allowing the setting of flags based on the copied Persistent.
  141. */
  142. Persistent &operator=(const Persistent &that);
  143. template <typename S, typename M2>
  144. Persistent &operator=(const Persistent<S, M2> &that);
  145. /**
  146. * The destructor will dispose the Persistent based on the kResetInDestructor
  147. * flags in the traits class. Since not calling dispose can result in a
  148. * memory leak, it is recommended to always set this flag.
  149. */
  150. ~Persistent();
  151. };
  152. ```
  153. See the V8 documentation for [`Persistent`](https://v8docs.nodesource.com/node-8.11/d2/d78/classv8_1_1_persistent.html) for further information.
  154. <a name="api_nan_global"></a>
  155. ### Nan::Global
  156. A type of `PersistentBase` which has move semantics.
  157. ```c++
  158. template<typename T> class Global : public PersistentBase<T> {
  159. public:
  160. /**
  161. * A Global with no storage cell.
  162. */
  163. Global();
  164. /**
  165. * Construct a Global from a v8::Local. When the v8::Local is non-empty, a new
  166. * storage cell is created pointing to the same object, and no flags are set.
  167. */
  168. template<typename S> Global(v8::Local<S> that);
  169. /**
  170. * Construct a Global from a PersistentBase. When the Persistent is non-empty,
  171. * a new storage cell is created pointing to the same object, and no flags are
  172. * set.
  173. */
  174. template<typename S> Global(const PersistentBase<S> &that);
  175. /**
  176. * Pass allows returning globals from functions, etc.
  177. */
  178. Global Pass();
  179. };
  180. ```
  181. See the V8 documentation for [`Global`](https://v8docs.nodesource.com/node-8.11/d5/d40/classv8_1_1_global.html) for further information.
  182. <a name="api_nan_weak_callback_info"></a>
  183. ### Nan::WeakCallbackInfo
  184. `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.
  185. Definition:
  186. ```c++
  187. template<typename T> class WeakCallbackInfo {
  188. public:
  189. typedef void (*Callback)(const WeakCallbackInfo<T>& data);
  190. v8::Isolate *GetIsolate() const;
  191. /**
  192. * Get the parameter that was associated with the weak handle.
  193. */
  194. T *GetParameter() const;
  195. /**
  196. * Get pointer from internal field, index can be 0 or 1.
  197. */
  198. void *GetInternalField(int index) const;
  199. };
  200. ```
  201. Example usage:
  202. ```c++
  203. void weakCallback(const WeakCallbackInfo<int> &data) {
  204. int *parameter = data.GetParameter();
  205. delete parameter;
  206. }
  207. Persistent<v8::Object> obj;
  208. int *data = new int(0);
  209. obj.SetWeak(data, callback, WeakCallbackType::kParameter);
  210. ```
  211. See the V8 documentation for [`WeakCallbackInfo`](https://v8docs.nodesource.com/node-8.11/d8/d06/classv8_1_1_weak_callback_info.html) for further information.
  212. <a name="api_nan_weak_callback_type"></a>
  213. ### Nan::WeakCallbackType
  214. Represents the type of a weak callback.
  215. A weak callback of type `kParameter` makes the supplied parameter to `Nan::PersistentBase::SetWeak` available through `WeakCallbackInfo::GetParameter`.
  216. 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.
  217. Note that only `v8::Object`s and derivatives can have internal fields.
  218. Definition:
  219. ```c++
  220. enum class WeakCallbackType { kParameter, kInternalFields };
  221. ```