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.

node_misc.md 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. ## Miscellaneous Node Helpers
  2. - <a href="#api_nan_asyncresource"><b><code>Nan::AsyncResource</code></b></a>
  3. - <a href="#api_nan_make_callback"><b><code>Nan::MakeCallback()</code></b></a>
  4. - <a href="#api_nan_module_init"><b><code>NAN_MODULE_INIT()</code></b></a>
  5. - <a href="#api_nan_export"><b><code>Nan::Export()</code></b></a>
  6. <a name="api_nan_asyncresource"></a>
  7. ### Nan::AsyncResource
  8. This class is analogous to the `AsyncResource` JavaScript class exposed by Node's [async_hooks][] API.
  9. When calling back into JavaScript asynchronously, special care must be taken to ensure that the runtime can properly track
  10. async hops. `Nan::AsyncResource` is a class that provides an RAII wrapper around `node::EmitAsyncInit`, `node::EmitAsyncDestroy`,
  11. and `node::MakeCallback`. Using this mechanism to call back into JavaScript, as opposed to `Nan::MakeCallback` or
  12. `v8::Function::Call` ensures that the callback is executed in the correct async context. This ensures that async mechanisms
  13. such as domains and [async_hooks][] function correctly.
  14. Definition:
  15. ```c++
  16. class AsyncResource {
  17. public:
  18. AsyncResource(v8::Local<v8::String> name,
  19. v8::Local<v8::Object> resource = New<v8::Object>());
  20. AsyncResource(const char* name,
  21. v8::Local<v8::Object> resource = New<v8::Object>());
  22. ~AsyncResource();
  23. v8::MaybeLocal<v8::Value> runInAsyncScope(v8::Local<v8::Object> target,
  24. v8::Local<v8::Function> func,
  25. int argc,
  26. v8::Local<v8::Value>* argv);
  27. v8::MaybeLocal<v8::Value> runInAsyncScope(v8::Local<v8::Object> target,
  28. v8::Local<v8::String> symbol,
  29. int argc,
  30. v8::Local<v8::Value>* argv);
  31. v8::MaybeLocal<v8::Value> runInAsyncScope(v8::Local<v8::Object> target,
  32. const char* method,
  33. int argc,
  34. v8::Local<v8::Value>* argv);
  35. };
  36. ```
  37. * `name`: Identifier for the kind of resource that is being provided for diagnostics information exposed by the [async_hooks][]
  38. API. This will be passed to the possible `init` hook as the `type`. To avoid name collisions with other modules we recommend
  39. that the name include the name of the owning module as a prefix. For example `mysql` module could use something like
  40. `mysql:batch-db-query-resource`.
  41. * `resource`: An optional object associated with the async work that will be passed to the possible [async_hooks][]
  42. `init` hook. If this parameter is omitted, or an empty handle is provided, this object will be created automatically.
  43. * When calling JS on behalf of this resource, one can use `runInAsyncScope`. This will ensure that the callback runs in the
  44. correct async execution context.
  45. * `AsyncDestroy` is automatically called when an AsyncResource object is destroyed.
  46. For more details, see the Node [async_hooks][] documentation. You might also want to take a look at the documentation for the
  47. [N-API counterpart][napi]. For example usage, see the `asyncresource.cpp` example in the `test/cpp` directory.
  48. <a name="api_nan_make_callback"></a>
  49. ### Nan::MakeCallback()
  50. Deprecated wrappers around the legacy `node::MakeCallback()` APIs. Node.js 10+
  51. has deprecated these legacy APIs as they do not provide a mechanism to preserve
  52. async context.
  53. We recommend that you use the `AsyncResource` class and `AsyncResource::runInAsyncScope` instead of using `Nan::MakeCallback` or
  54. `v8::Function#Call()` directly. `AsyncResource` properly takes care of running the callback in the correct async execution
  55. context – something that is essential for functionality like domains, async_hooks and async debugging.
  56. Signatures:
  57. ```c++
  58. NAN_DEPRECATED
  59. v8::Local<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target,
  60. v8::Local<v8::Function> func,
  61. int argc,
  62. v8::Local<v8::Value>* argv);
  63. NAN_DEPRECATED
  64. v8::Local<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target,
  65. v8::Local<v8::String> symbol,
  66. int argc,
  67. v8::Local<v8::Value>* argv);
  68. NAN_DEPRECATED
  69. v8::Local<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target,
  70. const char* method,
  71. int argc,
  72. v8::Local<v8::Value>* argv);
  73. ```
  74. <a name="api_nan_module_init"></a>
  75. ### NAN_MODULE_INIT()
  76. Used to define the entry point function to a Node add-on. Creates a function with a given `name` that receives a `target` object representing the equivalent of the JavaScript `exports` object.
  77. See example below.
  78. <a name="api_nan_export"></a>
  79. ### Nan::Export()
  80. A simple helper to register a `v8::FunctionTemplate` from a JavaScript-accessible method (see [Methods](./methods.md)) as a property on an object. Can be used in a way similar to assigning properties to `module.exports` in JavaScript.
  81. Signature:
  82. ```c++
  83. void Export(v8::Local<v8::Object> target, const char *name, Nan::FunctionCallback f)
  84. ```
  85. Also available as the shortcut `NAN_EXPORT` macro.
  86. Example:
  87. ```c++
  88. NAN_METHOD(Foo) {
  89. ...
  90. }
  91. NAN_MODULE_INIT(Init) {
  92. NAN_EXPORT(target, Foo);
  93. }
  94. ```
  95. [async_hooks]: https://nodejs.org/dist/latest-v9.x/docs/api/async_hooks.html
  96. [napi]: https://nodejs.org/dist/latest-v9.x/docs/api/n-api.html#n_api_custom_asynchronous_operations