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.

scopes.md 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. ## Scopes
  2. A _local handle_ is a pointer to an object. All V8 objects are accessed using handles, they are necessary because of the way the V8 garbage collector works.
  3. A handle scope can be thought of as a container for any number of handles. When you've finished with your handles, instead of deleting each one individually you can simply delete their scope.
  4. The creation of `HandleScope` objects is different across the supported versions of V8. Therefore, NAN provides its own implementations that can be used safely across these.
  5. - <a href="#api_nan_handle_scope"><b><code>Nan::HandleScope</code></b></a>
  6. - <a href="#api_nan_escapable_handle_scope"><b><code>Nan::EscapableHandleScope</code></b></a>
  7. Also see the V8 Embedders Guide section on [Handles and Garbage Collection](https://github.com/v8/v8/wiki/Embedder%27s%20Guide#handles-and-garbage-collection).
  8. <a name="api_nan_handle_scope"></a>
  9. ### Nan::HandleScope
  10. A simple wrapper around [`v8::HandleScope`](https://v8docs.nodesource.com/node-8.11/d3/d95/classv8_1_1_handle_scope.html).
  11. Definition:
  12. ```c++
  13. class Nan::HandleScope {
  14. public:
  15. Nan::HandleScope();
  16. static int NumberOfHandles();
  17. };
  18. ```
  19. Allocate a new `Nan::HandleScope` whenever you are creating new V8 JavaScript objects. Note that an implicit `HandleScope` is created for you on JavaScript-accessible methods so you do not need to insert one yourself.
  20. Example:
  21. ```c++
  22. // new object is created, it needs a new scope:
  23. void Pointless() {
  24. Nan::HandleScope scope;
  25. v8::Local<v8::Object> obj = Nan::New<v8::Object>();
  26. }
  27. // JavaScript-accessible method already has a HandleScope
  28. NAN_METHOD(Pointless2) {
  29. v8::Local<v8::Object> obj = Nan::New<v8::Object>();
  30. }
  31. ```
  32. <a name="api_nan_escapable_handle_scope"></a>
  33. ### Nan::EscapableHandleScope
  34. Similar to [`Nan::HandleScope`](#api_nan_handle_scope) but should be used in cases where a function needs to return a V8 JavaScript type that has been created within it.
  35. Definition:
  36. ```c++
  37. class Nan::EscapableHandleScope {
  38. public:
  39. Nan::EscapableHandleScope();
  40. static int NumberOfHandles();
  41. template<typename T> v8::Local<T> Escape(v8::Local<T> value);
  42. }
  43. ```
  44. Use `Escape(value)` to return the object.
  45. Example:
  46. ```c++
  47. v8::Local<v8::Object> EmptyObj() {
  48. Nan::EscapableHandleScope scope;
  49. v8::Local<v8::Object> obj = Nan::New<v8::Object>();
  50. return scope.Escape(obj);
  51. }
  52. ```