node-addon-api 2.0.2 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/.clang-format +111 -0
  2. package/.github/workflows/ci.yml +55 -0
  3. package/.github/workflows/linter.yml +24 -0
  4. package/.github/workflows/stale.yml +18 -0
  5. package/.travis.yml +1 -5
  6. package/CHANGELOG.md +237 -23
  7. package/README.md +101 -31
  8. package/appveyor.yml +3 -14
  9. package/benchmark/README.md +47 -0
  10. package/benchmark/binding.gyp +25 -0
  11. package/benchmark/function_args.cc +217 -0
  12. package/benchmark/function_args.js +60 -0
  13. package/benchmark/index.js +34 -0
  14. package/benchmark/property_descriptor.cc +91 -0
  15. package/benchmark/property_descriptor.js +37 -0
  16. package/common.gypi +21 -0
  17. package/doc/addon.md +157 -0
  18. package/doc/array.md +81 -0
  19. package/doc/array_buffer.md +20 -0
  20. package/doc/async_context.md +1 -1
  21. package/doc/async_worker.md +34 -5
  22. package/doc/{async_progress_worker.md → async_worker_variants.md} +236 -23
  23. package/doc/bigint.md +7 -2
  24. package/doc/boolean.md +5 -1
  25. package/doc/buffer.md +4 -0
  26. package/doc/checker-tool.md +1 -1
  27. package/doc/class_property_descriptor.md +3 -3
  28. package/doc/creating_a_release.md +6 -6
  29. package/doc/dataview.md +4 -0
  30. package/doc/date.md +2 -2
  31. package/doc/env.md +69 -0
  32. package/doc/error.md +5 -0
  33. package/doc/escapable_handle_scope.md +1 -1
  34. package/doc/external.md +4 -0
  35. package/doc/function.md +111 -3
  36. package/doc/function_reference.md +1 -1
  37. package/doc/handle_scope.md +1 -1
  38. package/doc/hierarchy.md +91 -0
  39. package/doc/instance_wrap.md +408 -0
  40. package/doc/name.md +29 -0
  41. package/doc/number.md +1 -1
  42. package/doc/object.md +44 -1
  43. package/doc/object_lifetime_management.md +2 -2
  44. package/doc/object_reference.md +1 -1
  45. package/doc/object_wrap.md +220 -216
  46. package/doc/prebuild_tools.md +2 -2
  47. package/doc/promises.md +5 -0
  48. package/doc/property_descriptor.md +67 -12
  49. package/doc/setup.md +1 -2
  50. package/doc/string.md +5 -1
  51. package/doc/symbol.md +5 -1
  52. package/doc/threadsafe.md +121 -0
  53. package/doc/threadsafe_function.md +16 -46
  54. package/doc/typed_array.md +4 -0
  55. package/doc/typed_array_of.md +4 -0
  56. package/doc/typed_threadsafe_function.md +307 -0
  57. package/doc/value.md +166 -104
  58. package/doc/version_management.md +2 -2
  59. package/except.gypi +16 -0
  60. package/index.js +7 -41
  61. package/napi-inl.h +1685 -464
  62. package/napi.h +606 -141
  63. package/node_api.gyp +9 -0
  64. package/noexcept.gypi +16 -0
  65. package/{src/nothing.c → nothing.c} +0 -0
  66. package/package-support.json +21 -0
  67. package/package.json +106 -2
  68. package/tools/README.md +12 -6
  69. package/tools/clang-format.js +47 -0
  70. package/tools/conversion.js +4 -8
  71. package/doc/Doxyfile +0 -2450
  72. package/doc/basic_types.md +0 -423
  73. package/doc/working_with_javascript_values.md +0 -14
  74. package/external-napi/node_api.h +0 -7
  75. package/src/node_api.cc +0 -3655
  76. package/src/node_api.gyp +0 -21
  77. package/src/node_api.h +0 -588
  78. package/src/node_api_types.h +0 -115
  79. package/src/node_internals.cc +0 -142
  80. package/src/node_internals.h +0 -157
  81. package/src/util-inl.h +0 -38
  82. package/src/util.h +0 -7
@@ -243,7 +243,9 @@ be safely released.
243
243
  Note that `Napi::AsyncProgressWorker::ExecutionProcess::Send` merely guarantees
244
244
  **eventual** invocation of `Napi::AsyncProgressWorker::OnProgress`, which means
245
245
  multiple send might be coalesced into single invocation of `Napi::AsyncProgressWorker::OnProgress`
246
- with latest data.
246
+ with latest data. If you would like to guarantee that there is one invocation of
247
+ `OnProgress` for every `Send` call, you should use the `Napi::AsyncProgressQueueWorker`
248
+ class instead which is documented further down this page.
247
249
 
248
250
  ```cpp
249
251
  void Napi::AsyncProgressWorker::ExecutionProcess::Send(const T* data, size_t count) const;
@@ -269,40 +271,51 @@ function runs in the background out of the **event loop** thread and at the end
269
271
  the `Napi::AsyncProgressWorker::OnOK` or `Napi::AsyncProgressWorker::OnError` function will be
270
272
  called and are executed as part of the event loop.
271
273
 
272
- The code below shows a basic example of the `Napi::AsyncProgressWorker` implementation:
274
+ The code below shows a basic example of the `Napi::AsyncProgressWorker` implementation along with an
275
+ example of how the counterpart in Javascript would appear:
273
276
 
274
277
  ```cpp
275
- #include<napi.h>
278
+ #include <napi.h>
276
279
 
277
280
  #include <chrono>
278
281
  #include <thread>
279
282
 
280
- use namespace Napi;
283
+ using namespace Napi;
281
284
 
282
285
  class EchoWorker : public AsyncProgressWorker<uint32_t> {
283
286
  public:
284
- EchoWorker(Function& callback, std::string& echo)
285
- : AsyncProgressWorker(callback), echo(echo) {}
287
+ EchoWorker(Function& okCallback, std::string& echo)
288
+ : AsyncProgressWorker(okCallback), echo(echo) {}
286
289
 
287
290
  ~EchoWorker() {}
288
- // This code will be executed on the worker thread
289
- void Execute(const ExecutionProgress& progress) {
290
- // Need to simulate cpu heavy task
291
- for (uint32_t i = 0; i < 100; ++i) {
292
- progress.Send(&i, 1)
293
- std::this_thread::sleep_for(std::chrono::seconds(1));
291
+
292
+ // This code will be executed on the worker thread
293
+ void Execute(const ExecutionProgress& progress) {
294
+ // Need to simulate cpu heavy task
295
+ // Note: This Send() call is not guaranteed to trigger an equal
296
+ // number of OnProgress calls (read documentation above for more info)
297
+ for (uint32_t i = 0; i < 100; ++i) {
298
+ progress.Send(&i, 1)
299
+ }
300
+ }
301
+
302
+ void OnError(const Error &e) {
303
+ HandleScope scope(Env());
304
+ // Pass error onto JS, no data for other parameters
305
+ Callback().Call({String::New(Env(), e.Message())});
294
306
  }
295
- }
296
307
 
297
- void OnOK() {
298
- HandleScope scope(Env());
299
- Callback().Call({Env().Null(), String::New(Env(), echo)});
300
- }
308
+ void OnOK() {
309
+ HandleScope scope(Env());
310
+ // Pass no error, give back original data
311
+ Callback().Call({Env().Null(), String::New(Env(), echo)});
312
+ }
301
313
 
302
- void OnProgress(const uint32_t* data, size_t /* count */) {
303
- HandleScope scope(Env());
304
- Callback().Call({Env().Null(), Env().Null(), Number::New(Env(), *data)});
305
- }
314
+ void OnProgress(const uint32_t* data, size_t /* count */) {
315
+ HandleScope scope(Env());
316
+ // Pass no error, no echo data, but do pass on the progress data
317
+ Callback().Call({Env().Null(), Env().Null(), Number::New(Env(), *data)});
318
+ }
306
319
 
307
320
  private:
308
321
  std::string echo;
@@ -323,16 +336,27 @@ The following code shows an example of how to create and use an `Napi::AsyncProg
323
336
  // Include EchoWorker class
324
337
  // ..
325
338
 
326
- use namespace Napi;
339
+ using namespace Napi;
327
340
 
328
341
  Value Echo(const CallbackInfo& info) {
329
342
  // We need to validate the arguments here
330
- Function cb = info[1].As<Function>();
331
343
  std::string in = info[0].As<String>();
344
+ Function cb = info[1].As<Function>();
332
345
  EchoWorker* wk = new EchoWorker(cb, in);
333
346
  wk->Queue();
334
347
  return info.Env().Undefined();
335
348
  }
349
+
350
+ // Register the native method for JS to access
351
+ Object Init(Env env, Object exports)
352
+ {
353
+ exports.Set(String::New(env, "echo"), Function::New(env, Echo));
354
+
355
+ return exports;
356
+ }
357
+
358
+ // Register our native addon
359
+ NODE_API_MODULE(nativeAddon, Init)
336
360
  ```
337
361
 
338
362
  The implementation of a `Napi::AsyncProgressWorker` can be used by creating a
@@ -341,4 +365,193 @@ asynchronous task ends and other data needed for the computation. Once created,
341
365
  the only other action needed is to call the `Napi::AsyncProgressWorker::Queue`
342
366
  method that will queue the created worker for execution.
343
367
 
368
+ Lastly, the following Javascript (ES6+) code would be associated the above example:
369
+
370
+ ```js
371
+ const { nativeAddon } = require('binding.node');
372
+
373
+ const exampleCallback = (errorResponse, okResponse, progressData) => {
374
+ // Use the data accordingly
375
+ // ...
376
+ };
377
+
378
+ // Call our native addon with the paramters of a string and a function
379
+ nativeAddon.echo("example", exampleCallback);
380
+ ```
381
+
382
+ # AsyncProgressQueueWorker
383
+
384
+ `Napi::AsyncProgressQueueWorker` acts exactly like `Napi::AsyncProgressWorker`
385
+ except that each progress committed by `Napi::AsyncProgressQueueWorker::ExecutionProgress::Send`
386
+ during `Napi::AsyncProgressQueueWorker::Execute` is guaranteed to be
387
+ processed by `Napi::AsyncProgressQueueWorker::OnProgress` on the JavaScript
388
+ thread in the order it was committed.
389
+
390
+ For the most basic use, only the `Napi::AsyncProgressQueueWorker::Execute` and
391
+ `Napi::AsyncProgressQueueWorker::OnProgress` method must be implemented in a subclass.
392
+
393
+ # AsyncProgressQueueWorker::ExecutionProcess
394
+
395
+ A bridge class created before the worker thread execution of `Napi::AsyncProgressQueueWorker::Execute`.
396
+
397
+ ## Methods
398
+
399
+ ### Send
400
+
401
+ `Napi::AsyncProgressQueueWorker::ExecutionProcess::Send` takes two arguments, a pointer
402
+ to a generic type of data, and a `size_t` to indicate how many items the pointer is
403
+ pointing to.
404
+
405
+ The data pointed to will be copied to internal slots of `Napi::AsyncProgressQueueWorker` so
406
+ after the call to `Napi::AsyncProgressQueueWorker::ExecutionProcess::Send` the data can
407
+ be safely released.
408
+
409
+ `Napi::AsyncProgressQueueWorker::ExecutionProcess::Send` guarantees invocation
410
+ of `Napi::AsyncProgressQueueWorker::OnProgress`, which means multiple `Send`
411
+ call will result in the in-order invocation of `Napi::AsyncProgressQueueWorker::OnProgress`
412
+ with each data item.
413
+
414
+ ```cpp
415
+ void Napi::AsyncProgressQueueWorker::ExecutionProcess::Send(const T* data, size_t count) const;
416
+ ```
417
+
418
+ ## Example
419
+
420
+ The code below show an example of the `Napi::AsyncProgressQueueWorker` implementation, but
421
+ also demonsrates how to use multiple `Napi::Function`'s if you wish to provide multiple
422
+ callback functions for more object oriented code:
423
+
424
+ ```cpp
425
+ #include <napi.h>
426
+
427
+ #include <chrono>
428
+ #include <thread>
429
+
430
+ using namespace Napi;
431
+
432
+ class EchoWorker : public AsyncProgressQueueWorker<uint32_t> {
433
+ public:
434
+ EchoWorker(Function& okCallback, Function& errorCallback, Function& progressCallback, std::string& echo)
435
+ : AsyncProgressQueueWorker(okCallback), echo(echo) {
436
+ // Set our function references to use them below
437
+ this->errorCallback.Reset(errorCallback, 1);
438
+ this->progressCallback.Reset(progressCallback, 1);
439
+ }
440
+
441
+ ~EchoWorker() {}
442
+
443
+ // This code will be executed on the worker thread
444
+ void Execute(const ExecutionProgress& progress) {
445
+ // Need to simulate cpu heavy task to demonstrate that
446
+ // every call to Send() will trigger an OnProgress function call
447
+ for (uint32_t i = 0; i < 100; ++i) {
448
+ progress.Send(&i, 1);
449
+ }
450
+ }
451
+
452
+ void OnOK() {
453
+ HandleScope scope(Env());
454
+ // Call our onOkCallback in javascript with the data we were given originally
455
+ Callback().Call({String::New(Env(), echo)});
456
+ }
457
+
458
+ void OnError(const Error &e) {
459
+ HandleScope scope(Env());
460
+
461
+ // We call our callback provided in the constructor with 2 parameters
462
+ if (!this->errorCallback.IsEmpty()) {
463
+ // Call our onErrorCallback in javascript with the error message
464
+ this->errorCallback.Call(Receiver().Value(), {String::New(Env(), e.Message())});
465
+ }
466
+ }
467
+
468
+ void OnProgress(const uint32_t* data, size_t /* count */) {
469
+ HandleScope scope(Env());
470
+
471
+ if (!this->progressCallback.IsEmpty()) {
472
+ // Call our onProgressCallback in javascript with each integer from 0 to 99 (inclusive)
473
+ // as this function is triggered from the above Send() calls
474
+ this->progressCallback.Call(Receiver().Value(), {Number::New(Env(), *data)});
475
+ }
476
+ }
477
+
478
+ private:
479
+ std::string echo;
480
+ FunctionReference progressCallback;
481
+ FunctionReference errorCallback;
482
+
483
+ };
484
+ ```
485
+
486
+ The `EchoWorker`'s constructor calls the base class' constructor to pass in the
487
+ callback that the `Napi::AsyncProgressQueueWorker` base class will store
488
+ persistently. When the work on the `Napi::AsyncProgressQueueWorker::Execute`
489
+ method is done the `Napi::AsyncProgressQueueWorker::OnOk` method is called and
490
+ the results are returned back to JavaScript when the stored callback is invoked
491
+ with its associated environment.
492
+
493
+ The following code shows an example of how to create and use an
494
+ `Napi::AsyncProgressQueueWorker`.
495
+
496
+ ```cpp
497
+ #include <napi.h>
498
+
499
+ // Include EchoWorker class
500
+ // ..
501
+
502
+ using namespace Napi;
503
+
504
+ Value Echo(const CallbackInfo& info) {
505
+ // We need to validate the arguments here.
506
+ std::string in = info[0].As<String>();
507
+ Function errorCb = info[1].As<Function>();
508
+ Function okCb = info[2].As<Function>();
509
+ Function progressCb = info[3].As<Function>();
510
+ EchoWorker* wk = new EchoWorker(okCb, errorCb, progressCb, in);
511
+ wk->Queue();
512
+ return info.Env().Undefined();
513
+ }
514
+
515
+ // Register the native method for JS to access
516
+ Object Init(Env env, Object exports)
517
+ {
518
+ exports.Set(String::New(env, "echo"), Function::New(env, Echo));
519
+
520
+ return exports;
521
+ }
522
+
523
+ // Register our native addon
524
+ NODE_API_MODULE(nativeAddon, Init)
525
+ ```
526
+
527
+ The implementation of a `Napi::AsyncProgressQueueWorker` can be used by creating a
528
+ new instance and passing to its constructor the callback to execute when the
529
+ asynchronous task ends and other data needed for the computation. Once created,
530
+ the only other action needed is to call the `Napi::AsyncProgressQueueWorker::Queue`
531
+ method that will queue the created worker for execution.
532
+
533
+ Lastly, the following Javascript (ES6+) code would be associated the above example:
534
+
535
+ ```js
536
+ const { nativeAddon } = require('binding.node');
537
+
538
+ const onErrorCallback = (msg) => {
539
+ // Use the data accordingly
540
+ // ...
541
+ };
542
+
543
+ const onOkCallback = (echo) => {
544
+ // Use the data accordingly
545
+ // ...
546
+ };
547
+
548
+ const onProgressCallback = (num) => {
549
+ // Use the data accordingly
550
+ // ...
551
+ };
552
+
553
+ // Call our native addon with the paramters of a string and three callback functions
554
+ nativeAddon.echo("example", onErrorCallback, onOkCallback, onProgressCallback);
555
+ ```
556
+
344
557
  [`Napi::AsyncWorker`]: ./async_worker.md
package/doc/bigint.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # BigInt
2
2
 
3
+ Class `Napi::Bigint` inherits from class [`Napi::Value`][].
4
+
3
5
  A JavaScript BigInt value.
4
6
 
5
7
  ## Methods
@@ -8,6 +10,7 @@ A JavaScript BigInt value.
8
10
 
9
11
  ```cpp
10
12
  static Napi::BigInt Napi::BigInt::New(Napi::Env env, int64_t value);
13
+ static Napi::BigInt Napi::BigInt::New(Napi::Env env, uint64_t value);
11
14
  ```
12
15
 
13
16
  - `[in] env`: The environment in which to construct the `Napi::BigInt` object.
@@ -47,7 +50,7 @@ Returns a new empty JavaScript `Napi::BigInt`.
47
50
  ### Int64Value
48
51
 
49
52
  ```cpp
50
- int64_t Napi::BitInt::Int64Value(bool* lossless) const;
53
+ int64_t Napi::BigInt::Int64Value(bool* lossless) const;
51
54
  ```
52
55
 
53
56
  - `[out] lossless`: Indicates whether the `BigInt` value was converted losslessly.
@@ -78,7 +81,7 @@ Returns the number of words needed to store this `BigInt` value.
78
81
  ### ToWords
79
82
 
80
83
  ```cpp
81
- void Napi::BigInt::ToWords(size_t* word_count, int* sign_bit, uint64_t* words);
84
+ void Napi::BigInt::ToWords(int* sign_bit, size_t* word_count, uint64_t* words);
82
85
  ```
83
86
 
84
87
  - `[out] sign_bit`: Integer representing if the JavaScript `BigInt` is positive
@@ -90,3 +93,5 @@ void Napi::BigInt::ToWords(size_t* word_count, int* sign_bit, uint64_t* words);
90
93
 
91
94
  Returns a single `BigInt` value into a sign bit, 64-bit little-endian array,
92
95
  and the number of elements in the array.
96
+
97
+ [`Napi::Value`]: ./value.md
package/doc/boolean.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Boolean
2
2
 
3
+ Class `Napi::Boolean` inherits from class [`Napi::Value`][].
4
+
3
5
  `Napi::Boolean` class is a representation of the JavaScript `Boolean` object. The
4
6
  `Napi::Boolean` class inherits its behavior from the `Napi::Value` class
5
7
  (for more info see: [`Napi::Value`](value.md)).
@@ -16,7 +18,7 @@ Napi::Boolean::Boolean();
16
18
 
17
19
  Returns a new _empty_ `Napi::Boolean` object.
18
20
 
19
- ### Contructor
21
+ ### Constructor
20
22
 
21
23
  Creates a new instance of the `Napi::Boolean` object.
22
24
 
@@ -62,3 +64,5 @@ Napi::Boolean::operator bool() const;
62
64
  ```
63
65
 
64
66
  Returns the boolean primitive type of the corresponding `Napi::Boolean` object.
67
+
68
+ [`Napi::Value`]: ./value.md
package/doc/buffer.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Buffer
2
2
 
3
+ Class `Napi::Buffer` inherits from class [`Napi::Uint8Array`][].
4
+
3
5
  The `Napi::Buffer` class creates a projection of raw data that can be consumed by
4
6
  script.
5
7
 
@@ -138,3 +140,5 @@ size_t Napi::Buffer::Length() const;
138
140
  ```
139
141
 
140
142
  Returns the number of `T` elements in the external data.
143
+
144
+ [`Napi::Uint8Array`]: ./typed_array_of.md
@@ -26,7 +26,7 @@ indicating for each addon whether it is an N-API addon.
26
26
  ```
27
27
 
28
28
  The tool accepts the root directory from which to start checking for Node.js
29
- native addons as a single optional command line parameter. If ommitted it will
29
+ native addons as a single optional command line parameter. If omitted it will
30
30
  start checking from the current directory (`.`).
31
31
 
32
32
  [checker tool]: ../tools/check-napi.js
@@ -26,9 +26,9 @@ class Example : public Napi::ObjectWrap<Example> {
26
26
  Napi::Object Example::Init(Napi::Env env, Napi::Object exports) {
27
27
  Napi::Function func = DefineClass(env, "Example", {
28
28
  // Register a class instance accessor with getter and setter functions.
29
- InstanceAccessor("value", &Example::GetValue, &Example::SetValue),
30
- // We can also register a readonly accessor by passing nullptr as the setter.
31
- InstanceAccessor("readOnlyProp", &Example::GetValue, nullptr)
29
+ InstanceAccessor<&Example::GetValue, &Example::SetValue>("value"),
30
+ // We can also register a readonly accessor by omitting the setter.
31
+ InstanceAccessor<&Example::GetValue>("readOnlyProp")
32
32
  });
33
33
 
34
34
  constructor = Napi::Persistent(func);
@@ -13,7 +13,7 @@ tools:
13
13
 
14
14
  * [Changelog maker](https://www.npmjs.com/package/changelog-maker)
15
15
 
16
- If not please follow the instruction reported in the tool's documentation to
16
+ If not please follow the instruction reported in the tool's documentation to
17
17
  install it.
18
18
 
19
19
  ## Publish new release
@@ -27,24 +27,24 @@ new release. Give people some time to comment or suggest PRs that should land fi
27
27
 
28
28
  * Update the version in **package.json** appropriately.
29
29
 
30
- * Update the [README.md](https://github.com/nodejs/node-addon-api/blob/master/README.md)
30
+ * Update the [README.md](https://github.com/nodejs/node-addon-api/blob/master/README.md)
31
31
  to show the new version as the latest.
32
32
 
33
33
  * Generate the changelog for the new version using **changelog maker** tool. From
34
34
  the route folder of the repo launch the following command:
35
35
 
36
- ```bash
36
+ ```bash
37
37
  > changelog-maker
38
38
  ```
39
- * Use the output generated by **changelog maker** to pdate the [CHANGELOG.md](https://github.com/nodejs/node-addon-api/blob/master/CHANGELOG.md)
39
+ * Use the output generated by **changelog maker** to update the [CHANGELOG.md](https://github.com/nodejs/node-addon-api/blob/master/CHANGELOG.md)
40
40
  following the style used in publishing the previous release.
41
41
 
42
42
  * Add any new contributors to the "contributors" section in the package.json
43
43
 
44
44
  * Validate all tests pass by running npm test on master.
45
45
 
46
- * Use **[CI](https://ci.nodejs.org/view/x%20-%20Abi%20stable%20module%20API/job/node-test-node-addon-api/)**
47
- to validate tests pass for latest 11, 10, 8, 6 releases (note there are still some issues on SmartOS and
46
+ * Use **[CI](https://ci.nodejs.org/view/x%20-%20Abi%20stable%20module%20API/job/node-test-node-addon-api-new/)**
47
+ to validate tests pass (note there are still some issues on SmartOS and
48
48
  Windows in the testing).
49
49
 
50
50
  * Do a clean checkout of node-addon-api.
package/doc/dataview.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # DataView
2
2
 
3
+ Class `Napi::DataView` inherits from class [`Napi::Object`][].
4
+
3
5
  The `Napi::DataView` class corresponds to the
4
6
  [JavaScript `DataView`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView)
5
7
  class.
@@ -242,3 +244,5 @@ void Napi::DataView::SetUint32(size_t byteOffset, uint32_t value) const;
242
244
 
243
245
  - `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
244
246
  - `[in] value`: The value to set.
247
+
248
+ [`Napi::Object`]: ./object.md
package/doc/date.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Date
2
2
 
3
3
  `Napi::Date` class is a representation of the JavaScript `Date` object. The
4
- `Napi::Date` class inherits its behavior from `Napi::Value` class
5
- (for more info see [`Napi::Value`](value.md))
4
+ `Napi::Date` class inherits its behavior from the `Napi::Value` class
5
+ (for more info see [`Napi::Value`](value.md)).
6
6
 
7
7
  ## Methods
8
8
 
package/doc/env.md CHANGED
@@ -61,3 +61,72 @@ Napi::Error Napi::Env::GetAndClearPendingException();
61
61
  ```
62
62
 
63
63
  Returns an `Napi::Error` object representing the environment's pending exception, if any.
64
+
65
+ ### RunScript
66
+
67
+ ```cpp
68
+ Napi::Value Napi::Env::RunScript(____ script);
69
+ ```
70
+ - `[in] script`: A string containing JavaScript code to execute.
71
+
72
+ Runs JavaScript code contained in a string and returns its result.
73
+
74
+ The `script` can be any of the following types:
75
+ - [`Napi::String`](string.md)
76
+ - `const char *`
77
+ - `const std::string &`
78
+
79
+ ### GetInstanceData
80
+ ```cpp
81
+ template <typename T> T* GetInstanceData();
82
+ ```
83
+
84
+ Returns the instance data that was previously associated with the environment,
85
+ or `nullptr` if none was associated.
86
+
87
+ ### SetInstanceData
88
+
89
+ ```cpp
90
+ template <typename T> using Finalizer = void (*)(Env, T*);
91
+ template <typename T, Finalizer<T> fini = Env::DefaultFini<T>>
92
+ void SetInstanceData(T* data);
93
+ ```
94
+
95
+ - `[template] fini`: A function to call when the instance data is to be deleted.
96
+ Accepts a function of the form `void CleanupData(Napi::Env env, T* data)`. If
97
+ not given, the default finalizer will be used, which simply uses the `delete`
98
+ operator to destroy `T*` when the addon instance is unloaded.
99
+ - `[in] data`: A pointer to data that will be associated with the instance of
100
+ the addon for the duration of its lifecycle.
101
+
102
+ Associates a data item stored at `T* data` with the current instance of the
103
+ addon. The item will be passed to the function `fini` which gets called when an
104
+ instance of the addon is unloaded.
105
+
106
+ ### SetInstanceData
107
+
108
+ ```cpp
109
+ template <typename DataType, typename HintType>
110
+ using FinalizerWithHint = void (*)(Env, DataType*, HintType*);
111
+ template <typename DataType,
112
+ typename HintType,
113
+ FinalizerWithHint<DataType, HintType> fini =
114
+ Env::DefaultFiniWithHint<DataType, HintType>>
115
+ void SetInstanceData(DataType* data, HintType* hint);
116
+ ```
117
+
118
+ - `[template] fini`: A function to call when the instance data is to be deleted.
119
+ Accepts a function of the form
120
+ `void CleanupData(Napi::Env env, DataType* data, HintType* hint)`. If not given,
121
+ the default finalizer will be used, which simply uses the `delete` operator to
122
+ destroy `T*` when the addon instance is unloaded.
123
+ - `[in] data`: A pointer to data that will be associated with the instance of
124
+ the addon for the duration of its lifecycle.
125
+ - `[in] hint`: A pointer to data that will be associated with the instance of
126
+ the addon for the duration of its lifecycle and will be passed as a hint to
127
+ `fini` when the addon instance is unloaded.
128
+
129
+ Associates a data item stored at `T* data` with the current instance of the
130
+ addon. The item will be passed to the function `fini` which gets called when an
131
+ instance of the addon is unloaded. This overload accepts an additional hint to
132
+ be passed to `fini`.
package/doc/error.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Error
2
2
 
3
+ Class `Napi::Error` inherits from class [`Napi::ObjectReference`][] and class [`std::exception`][].
4
+
3
5
  The `Napi::Error` class is a representation of the JavaScript `Error` object that is thrown
4
6
  when runtime errors occur. The Error object can also be used as a base object for
5
7
  user-defined exceptions.
@@ -113,3 +115,6 @@ const char* Napi::Error::what() const NAPI_NOEXCEPT override;
113
115
 
114
116
  Returns a pointer to a null-terminated string that is used to identify the
115
117
  exception. This method can be used only if the exception mechanism is enabled.
118
+
119
+ [`Napi::ObjectReference`]: ./object_reference.md
120
+ [`std::exception`]: https://cplusplus.com/reference/exception/exception/
@@ -59,7 +59,7 @@ Napi::EscapableHandleScope::~EscapableHandleScope();
59
59
 
60
60
  Deletes the `Napi::EscapableHandleScope` instance and allows any objects/handles created
61
61
  in the scope to be collected by the garbage collector. There is no
62
- guarantee as to when the gargbage collector will do this.
62
+ guarantee as to when the garbage collector will do this.
63
63
 
64
64
  ### Escape
65
65
 
package/doc/external.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # External (template)
2
2
 
3
+ Class `Napi::External<T>` inherits from class [`Napi::Value`][].
4
+
3
5
  The `Napi::External` template class implements the ability to create a `Napi::Value` object with arbitrary C++ data. It is the user's responsibility to manage the memory for the arbitrary C++ data.
4
6
 
5
7
  `Napi::External` objects can be created with an optional Finalizer function and optional Hint value. The Finalizer function, if specified, is called when your `Napi::External` object is released by Node's garbage collector. It gives your code the opportunity to free any dynamically created data. If you specify a Hint value, it is passed to your Finalizer function.
@@ -57,3 +59,5 @@ T* Napi::External::Data() const;
57
59
  ```
58
60
 
59
61
  Returns a pointer to the arbitrary C++ data held by the `Napi::External` object.
62
+
63
+ [`Napi::Value`]: ./value.md