node-addon-api 3.0.1 → 3.0.2

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,50 @@
1
1
  # node-addon-api Changelog
2
2
 
3
+ ## 2020-09-18 Version 3.0.2, @NickNaso
4
+
5
+ ### Notable changes:
6
+
7
+ #### API
8
+
9
+ - Introduced `include_dir` for use with **gyp** in a scalar context.
10
+ - Added `Napi::Addon` to help handle the loading of a native add-on into
11
+ multiple threads and or multiple times in the same thread.
12
+ - Concentrate callbacks provided to core N-API.
13
+ - Make sure wrapcallback is used.
14
+
15
+ #### Documentation
16
+
17
+ - Added documentation for `Napi::Addon`.
18
+ - Added documentation that reports the full class hierarchy.
19
+ - Added link to N-API tutorial website.
20
+ - Some minor corrections all over the documentation.
21
+
22
+ #### TEST
23
+
24
+ - Added tests to check the build process.
25
+ - Refactored test for threasfafe function using async/await.
26
+ - Converted tests that gc into async functions that await 10 ticks after
27
+ each gc.
28
+ - Some minor corrections all over the test suite.
29
+
30
+ ### Commits
31
+
32
+ * [[`51e25f7c39`](https://github.com/nodejs/node-addon-api/commit/51e25f7c39)] - **doc**: remove a file (#815) (Gabriel Schulhof)
33
+ * [[`8c9f1809a2`](https://github.com/nodejs/node-addon-api/commit/8c9f1809a2)] - **doc**: add inheritance links and other changes (Gabriel Schulhof) [#798](https://github.com/nodejs/node-addon-api/pull/798)
34
+ * [[`6562e6b0ab`](https://github.com/nodejs/node-addon-api/commit/6562e6b0ab)] - **test**: added tests to check the build process (NickNaso) [#808](https://github.com/nodejs/node-addon-api/pull/808)
35
+ * [[`a13b36c96e`](https://github.com/nodejs/node-addon-api/commit/a13b36c96e)] - **test**: fix the threasfafe function test (NickNaso) [#807](https://github.com/nodejs/node-addon-api/pull/807)
36
+ * [[`f27623ff61`](https://github.com/nodejs/node-addon-api/commit/f27623ff61)] - **build**: introduce include\_dir (Lovell Fuller) [#766](https://github.com/nodejs/node-addon-api/pull/766)
37
+ * [[`9aceea71fc`](https://github.com/nodejs/node-addon-api/commit/9aceea71fc)] - **src**: concentrate callbacks provided to core N-API (Gabriel Schulhof) [#786](https://github.com/nodejs/node-addon-api/pull/786)
38
+ * [[`2bc45bbffd`](https://github.com/nodejs/node-addon-api/commit/2bc45bbffd)] - **test**: refactor test to use async/await (Velmisov) [#787](https://github.com/nodejs/node-addon-api/pull/787)
39
+ * [[`518cfdcdc1`](https://github.com/nodejs/node-addon-api/commit/518cfdcdc1)] - **test**: test ObjectWrap destructor - no HandleScope (David Halls) [#729](https://github.com/nodejs/node-addon-api/pull/729)
40
+ * [[`c2cbbd9191`](https://github.com/nodejs/node-addon-api/commit/c2cbbd9191)] - **doc**: add link to n-api tutorial website (#794) (Jim Schlight) [#794](https://github.com/nodejs/node-addon-api/pull/794)
41
+ * [[`1c2a8d59b5`](https://github.com/nodejs/node-addon-api/commit/1c2a8d59b5)] - **doc**: Added required return to example (#793) (pacop) [#793](https://github.com/nodejs/node-addon-api/pull/793)
42
+ * [[`cec2c76941`](https://github.com/nodejs/node-addon-api/commit/cec2c76941)] - **src**: wrap finalizer callback (Gabriel Schulhof) [#762](https://github.com/nodejs/node-addon-api/pull/762)
43
+ * [[`4ce40d22a6`](https://github.com/nodejs/node-addon-api/commit/4ce40d22a6)] - **test**: use assert.strictEqual() (Koki Nishihara) [#777](https://github.com/nodejs/node-addon-api/pull/777)
44
+ * [[`461e3640c6`](https://github.com/nodejs/node-addon-api/commit/461e3640c6)] - **test**: string tests together (Gabriel Schulhof) [#773](https://github.com/nodejs/node-addon-api/pull/773)
45
+ * [[`5af645f649`](https://github.com/nodejs/node-addon-api/commit/5af645f649)] - **src**: add Addon\<T\> class (Gabriel Schulhof) [#749](https://github.com/nodejs/node-addon-api/pull/749)
46
+ * [[`6148fb4bcc`](https://github.com/nodejs/node-addon-api/commit/6148fb4bcc)] - Synchronise Node.js versions in Appveyor Windows CI with Travis (#768) (Lovell Fuller)
47
+
3
48
  ## 2020-07-13 Version 3.0.1, @NickNaso
4
49
 
5
50
  ### Notable changes:
package/README.md CHANGED
@@ -37,6 +37,10 @@ APIs exposed by node-addon-api are generally used to create and
37
37
  manipulate JavaScript values. Concepts and operations generally map
38
38
  to ideas specified in the **ECMA262 Language Specification**.
39
39
 
40
+ The [N-API Resource](http://nodejs.github.io/node-addon-examples/) offers an
41
+ excellent orientation and tips for developers just getting started with N-API
42
+ and node-addon-api.
43
+
40
44
  - **[Setup](#setup)**
41
45
  - **[API Documentation](#api)**
42
46
  - **[Examples](#examples)**
@@ -47,7 +51,7 @@ to ideas specified in the **ECMA262 Language Specification**.
47
51
  - **[Contributors](#contributors)**
48
52
  - **[License](#license)**
49
53
 
50
- ## **Current version: 3.0.1**
54
+ ## **Current version: 3.0.2**
51
55
 
52
56
  (See [CHANGELOG.md](CHANGELOG.md) for complete Changelog)
53
57
 
@@ -77,31 +81,25 @@ The oldest Node.js version supported by the current version of node-addon-api is
77
81
 
78
82
  The following is the documentation for node-addon-api.
79
83
 
80
- - [Basic Types](doc/basic_types.md)
81
- - [Array](doc/basic_types.md#array)
82
- - [Symbol](doc/symbol.md)
83
- - [String](doc/string.md)
84
- - [Name](doc/basic_types.md#name)
85
- - [Number](doc/number.md)
86
- - [Date](doc/date.md)
87
- - [BigInt](doc/bigint.md)
88
- - [Boolean](doc/boolean.md)
84
+ - [Full Class Hierarchy](doc/hierarchy.md)
85
+ - [Addon Structure](doc/addon.md)
86
+ - Data Types:
89
87
  - [Env](doc/env.md)
90
- - [Value](doc/value.md)
91
88
  - [CallbackInfo](doc/callbackinfo.md)
92
89
  - [Reference](doc/reference.md)
93
- - [External](doc/external.md)
94
- - [Object](doc/object.md)
95
- - [ObjectReference](doc/object_reference.md)
96
- - [PropertyDescriptor](doc/property_descriptor.md)
97
- - [Error Handling](doc/error_handling.md)
98
- - [Error](doc/error.md)
99
- - [TypeError](doc/type_error.md)
100
- - [RangeError](doc/range_error.md)
101
- - [Object Lifetime Management](doc/object_lifetime_management.md)
102
- - [HandleScope](doc/handle_scope.md)
103
- - [EscapableHandleScope](doc/escapable_handle_scope.md)
104
- - [Working with JavaScript Values](doc/working_with_javascript_values.md)
90
+ - [Value](doc/value.md)
91
+ - [Name](doc/name.md)
92
+ - [Symbol](doc/symbol.md)
93
+ - [String](doc/string.md)
94
+ - [Number](doc/number.md)
95
+ - [Date](doc/date.md)
96
+ - [BigInt](doc/bigint.md)
97
+ - [Boolean](doc/boolean.md)
98
+ - [External](doc/external.md)
99
+ - [Object](doc/object.md)
100
+ - [Array](doc/array.md)
101
+ - [ObjectReference](doc/object_reference.md)
102
+ - [PropertyDescriptor](doc/property_descriptor.md)
105
103
  - [Function](doc/function.md)
106
104
  - [FunctionReference](doc/function_reference.md)
107
105
  - [ObjectWrap](doc/object_wrap.md)
@@ -111,6 +109,13 @@ The following is the documentation for node-addon-api.
111
109
  - [TypedArray](doc/typed_array.md)
112
110
  - [TypedArrayOf](doc/typed_array_of.md)
113
111
  - [DataView](doc/dataview.md)
112
+ - [Error Handling](doc/error_handling.md)
113
+ - [Error](doc/error.md)
114
+ - [TypeError](doc/type_error.md)
115
+ - [RangeError](doc/range_error.md)
116
+ - [Object Lifetime Management](doc/object_lifetime_management.md)
117
+ - [HandleScope](doc/handle_scope.md)
118
+ - [EscapableHandleScope](doc/escapable_handle_scope.md)
114
119
  - [Memory Management](doc/memory_management.md)
115
120
  - [Async Operations](doc/async_operations.md)
116
121
  - [AsyncWorker](doc/async_worker.md)
package/appveyor.yml CHANGED
@@ -1,24 +1,13 @@
1
1
  environment:
2
2
  # https://github.com/jasongin/nvs/blob/master/doc/CI.md
3
3
  NVS_VERSION: 1.4.2
4
- fast_finish: true
5
4
  matrix:
6
- - NODEJS_VERSION: node/4
7
- - NODEJS_VERSION: node/6
8
- - NODEJS_VERSION: node/8
9
- - NODEJS_VERSION: node/9
10
5
  - NODEJS_VERSION: node/10
11
- - NODEJS_VERSION: chakracore/8
12
- - NODEJS_VERSION: chakracore/10
6
+ - NODEJS_VERSION: node/12
7
+ - NODEJS_VERSION: node/14
13
8
  - NODEJS_VERSION: nightly
14
- - NODEJS_VERSION: chakracore-nightly
15
-
16
- matrix:
17
- fast_finish: true
18
- allow_failures:
19
- - NODEJS_VERSION: nightly
20
- - NODEJS_VERSION: chakracore-nightly
21
9
 
10
+ os: Visual Studio 2017
22
11
  platform:
23
12
  - x86
24
13
  - x64
@@ -78,6 +78,66 @@ static void FourArgFunction(const Napi::CallbackInfo& info) {
78
78
  Napi::Value argv3 = info[3]; (void) argv3;
79
79
  }
80
80
 
81
+ #if NAPI_VERSION > 5
82
+ class FunctionArgsBenchmark : public Napi::Addon<FunctionArgsBenchmark> {
83
+ public:
84
+ FunctionArgsBenchmark(Napi::Env env, Napi::Object exports) {
85
+ DefineAddon(exports, {
86
+ InstanceValue("addon", DefineProperties(Napi::Object::New(env), {
87
+ InstanceMethod("noArgFunction", &FunctionArgsBenchmark::NoArgFunction),
88
+ InstanceMethod("oneArgFunction",
89
+ &FunctionArgsBenchmark::OneArgFunction),
90
+ InstanceMethod("twoArgFunction",
91
+ &FunctionArgsBenchmark::TwoArgFunction),
92
+ InstanceMethod("threeArgFunction",
93
+ &FunctionArgsBenchmark::ThreeArgFunction),
94
+ InstanceMethod("fourArgFunction",
95
+ &FunctionArgsBenchmark::FourArgFunction),
96
+ }), napi_enumerable),
97
+ InstanceValue("addon_templated",
98
+ DefineProperties(Napi::Object::New(env), {
99
+ InstanceMethod<&FunctionArgsBenchmark::NoArgFunction>(
100
+ "noArgFunction"),
101
+ InstanceMethod<&FunctionArgsBenchmark::OneArgFunction>(
102
+ "oneArgFunction"),
103
+ InstanceMethod<&FunctionArgsBenchmark::TwoArgFunction>(
104
+ "twoArgFunction"),
105
+ InstanceMethod<&FunctionArgsBenchmark::ThreeArgFunction>(
106
+ "threeArgFunction"),
107
+ InstanceMethod<&FunctionArgsBenchmark::FourArgFunction>(
108
+ "fourArgFunction"),
109
+ }), napi_enumerable),
110
+ });
111
+ }
112
+ private:
113
+ void NoArgFunction(const Napi::CallbackInfo& info) {
114
+ (void) info;
115
+ }
116
+
117
+ void OneArgFunction(const Napi::CallbackInfo& info) {
118
+ Napi::Value argv0 = info[0]; (void) argv0;
119
+ }
120
+
121
+ void TwoArgFunction(const Napi::CallbackInfo& info) {
122
+ Napi::Value argv0 = info[0]; (void) argv0;
123
+ Napi::Value argv1 = info[1]; (void) argv1;
124
+ }
125
+
126
+ void ThreeArgFunction(const Napi::CallbackInfo& info) {
127
+ Napi::Value argv0 = info[0]; (void) argv0;
128
+ Napi::Value argv1 = info[1]; (void) argv1;
129
+ Napi::Value argv2 = info[2]; (void) argv2;
130
+ }
131
+
132
+ void FourArgFunction(const Napi::CallbackInfo& info) {
133
+ Napi::Value argv0 = info[0]; (void) argv0;
134
+ Napi::Value argv1 = info[1]; (void) argv1;
135
+ Napi::Value argv2 = info[2]; (void) argv2;
136
+ Napi::Value argv3 = info[3]; (void) argv3;
137
+ }
138
+ };
139
+ #endif // NAPI_VERSION > 5
140
+
81
141
  static Napi::Object Init(Napi::Env env, Napi::Object exports) {
82
142
  napi_value no_arg_function, one_arg_function, two_arg_function,
83
143
  three_arg_function, four_arg_function;
@@ -147,6 +207,10 @@ static Napi::Object Init(Napi::Env env, Napi::Object exports) {
147
207
  templated["fourArgFunction"] = Napi::Function::New<FourArgFunction>(env);
148
208
  exports["templated"] = templated;
149
209
 
210
+ #if NAPI_VERSION > 5
211
+ FunctionArgsBenchmark::Init(env, exports);
212
+ #endif // NAPI_VERSION > 5
213
+
150
214
  return exports;
151
215
  }
152
216
 
@@ -4,16 +4,22 @@ const addonName = path.basename(__filename, '.js');
4
4
 
5
5
  [ addonName, addonName + '_noexcept' ]
6
6
  .forEach((addonName) => {
7
- const rootAddon = require(`./build/Release/${addonName}`);
7
+ const rootAddon = require('bindings')({
8
+ bindings: addonName,
9
+ module_root: __dirname
10
+ });
11
+ delete rootAddon.path;
8
12
  const implems = Object.keys(rootAddon);
13
+ const maxNameLength =
14
+ implems.reduce((soFar, value) => Math.max(soFar, value.length), 0);
9
15
  const anObject = {};
10
16
 
11
- console.log(`${addonName}: `);
17
+ console.log(`\n${addonName}: `);
12
18
 
13
19
  console.log('no arguments:');
14
20
  implems.reduce((suite, implem) => {
15
21
  const fn = rootAddon[implem].noArgFunction;
16
- return suite.add(implem, () => fn());
22
+ return suite.add(implem.padStart(maxNameLength, ' '), () => fn());
17
23
  }, new Benchmark.Suite)
18
24
  .on('cycle', (event) => console.log(String(event.target)))
19
25
  .run();
@@ -21,7 +27,7 @@ const addonName = path.basename(__filename, '.js');
21
27
  console.log('one argument:');
22
28
  implems.reduce((suite, implem) => {
23
29
  const fn = rootAddon[implem].oneArgFunction;
24
- return suite.add(implem, () => fn('x'));
30
+ return suite.add(implem.padStart(maxNameLength, ' '), () => fn('x'));
25
31
  }, new Benchmark.Suite)
26
32
  .on('cycle', (event) => console.log(String(event.target)))
27
33
  .run();
@@ -29,7 +35,7 @@ const addonName = path.basename(__filename, '.js');
29
35
  console.log('two arguments:');
30
36
  implems.reduce((suite, implem) => {
31
37
  const fn = rootAddon[implem].twoArgFunction;
32
- return suite.add(implem, () => fn('x', 12));
38
+ return suite.add(implem.padStart(maxNameLength, ' '), () => fn('x', 12));
33
39
  }, new Benchmark.Suite)
34
40
  .on('cycle', (event) => console.log(String(event.target)))
35
41
  .run();
@@ -37,7 +43,8 @@ const addonName = path.basename(__filename, '.js');
37
43
  console.log('three arguments:');
38
44
  implems.reduce((suite, implem) => {
39
45
  const fn = rootAddon[implem].threeArgFunction;
40
- return suite.add(implem, () => fn('x', 12, true));
46
+ return suite.add(implem.padStart(maxNameLength, ' '),
47
+ () => fn('x', 12, true));
41
48
  }, new Benchmark.Suite)
42
49
  .on('cycle', (event) => console.log(String(event.target)))
43
50
  .run();
@@ -45,7 +52,8 @@ const addonName = path.basename(__filename, '.js');
45
52
  console.log('four arguments:');
46
53
  implems.reduce((suite, implem) => {
47
54
  const fn = rootAddon[implem].fourArgFunction;
48
- return suite.add(implem, () => fn('x', 12, true, anObject));
55
+ return suite.add(implem.padStart(maxNameLength, ' '),
56
+ () => fn('x', 12, true, anObject));
49
57
  }, new Benchmark.Suite)
50
58
  .on('cycle', (event) => console.log(String(event.target)))
51
59
  .run();
@@ -26,6 +26,33 @@ static void Setter(const Napi::CallbackInfo& info) {
26
26
  (void) info[0];
27
27
  }
28
28
 
29
+ #if NAPI_VERSION > 5
30
+ class PropDescBenchmark : public Napi::Addon<PropDescBenchmark> {
31
+ public:
32
+ PropDescBenchmark(Napi::Env, Napi::Object exports) {
33
+ DefineAddon(exports, {
34
+ InstanceAccessor("addon",
35
+ &PropDescBenchmark::Getter,
36
+ &PropDescBenchmark::Setter,
37
+ napi_enumerable),
38
+ InstanceAccessor<&PropDescBenchmark::Getter,
39
+ &PropDescBenchmark::Setter>("addon_templated",
40
+ napi_enumerable),
41
+ });
42
+ }
43
+
44
+ private:
45
+ Napi::Value Getter(const Napi::CallbackInfo& info) {
46
+ return Napi::Number::New(info.Env(), 42);
47
+ }
48
+
49
+ void Setter(const Napi::CallbackInfo& info, const Napi::Value& val) {
50
+ (void) info[0];
51
+ (void) val;
52
+ }
53
+ };
54
+ #endif // NAPI_VERSION > 5
55
+
29
56
  static Napi::Object Init(Napi::Env env, Napi::Object exports) {
30
57
  napi_status status;
31
58
  napi_property_descriptor core_prop = {
@@ -54,6 +81,10 @@ static Napi::Object Init(Napi::Env env, Napi::Object exports) {
54
81
  Napi::PropertyDescriptor::Accessor<Getter, Setter>("templated",
55
82
  napi_enumerable));
56
83
 
84
+ #if NAPI_VERSION > 5
85
+ PropDescBenchmark::Init(env, exports);
86
+ #endif // NAPI_VERSION > 5
87
+
57
88
  return exports;
58
89
  }
59
90
 
@@ -4,17 +4,23 @@ const addonName = path.basename(__filename, '.js');
4
4
 
5
5
  [ addonName, addonName + '_noexcept' ]
6
6
  .forEach((addonName) => {
7
- const rootAddon = require(`./build/Release/${addonName}`);
7
+ const rootAddon = require('bindings')({
8
+ bindings: addonName,
9
+ module_root: __dirname
10
+ });
11
+ delete rootAddon.path;
8
12
  const getters = new Benchmark.Suite;
9
13
  const setters = new Benchmark.Suite;
14
+ const maxNameLength = Object.keys(rootAddon)
15
+ .reduce((soFar, value) => Math.max(soFar, value.length), 0);
10
16
 
11
- console.log(`${addonName}: `);
17
+ console.log(`\n${addonName}: `);
12
18
 
13
19
  Object.keys(rootAddon).forEach((key) => {
14
- getters.add(`${key} getter`, () => {
20
+ getters.add(`${key} getter`.padStart(maxNameLength + 7), () => {
15
21
  const x = rootAddon[key];
16
22
  });
17
- setters.add(`${key} setter`, () => {
23
+ setters.add(`${key} setter`.padStart(maxNameLength + 7), () => {
18
24
  rootAddon[key] = 5;
19
25
  })
20
26
  });
@@ -23,6 +29,8 @@ const addonName = path.basename(__filename, '.js');
23
29
  .on('cycle', (event) => console.log(String(event.target)))
24
30
  .run();
25
31
 
32
+ console.log('');
33
+
26
34
  setters
27
35
  .on('cycle', (event) => console.log(String(event.target)))
28
36
  .run();
package/common.gypi CHANGED
@@ -15,7 +15,7 @@
15
15
  }
16
16
  }]
17
17
  ],
18
- 'include_dirs': ["<!@(node -p \"require('../').include\")"],
18
+ 'include_dirs': ["<!(node -p \"require('../').include_dir\")"],
19
19
  'cflags': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ],
20
20
  'cflags_cc': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ]
21
21
  }
package/doc/addon.md ADDED
@@ -0,0 +1,157 @@
1
+ # Add-on Structure
2
+
3
+ Class `Napi::Addon<T>` inherits from class [`Napi::InstanceWrap<T>`][].
4
+
5
+ Creating add-ons that work correctly when loaded multiple times from the same
6
+ source package into multiple Node.js threads and/or multiple times into the same
7
+ Node.js thread requires that all global data they hold be associated with the
8
+ environment in which they run. It is not safe to store global data in static
9
+ variables because doing so does not take into account the fact that an add-on
10
+ may be loaded into multiple threads nor that an add-on may be loaded multiple
11
+ times into a single thread.
12
+
13
+ The `Napi::Addon<T>` class can be used to define an entire add-on. Instances of
14
+ `Napi::Addon<T>` subclasses become instances of the add-on, stored safely by
15
+ Node.js on its various threads and into its various contexts. Thus, any data
16
+ stored in the instance variables of a `Napi::Addon<T>` subclass instance are
17
+ stored safely by Node.js. Functions exposed to JavaScript using
18
+ `Napi::Addon<T>::InstanceMethod` and/or `Napi::Addon<T>::DefineAddon` are
19
+ instance methods of the `Napi::Addon` subclass and thus have access to data
20
+ stored inside the instance.
21
+
22
+ `Napi::Addon<T>::DefineProperties` may be used to attach `Napi::Addon<T>`
23
+ subclass instance methods to objects other than the one that will be returned to
24
+ Node.js as the add-on instance.
25
+
26
+ The `Napi::Addon<T>` class can be used together with the `NODE_API_ADDON()` and
27
+ `NODE_API_NAMED_ADDON()` macros to define add-ons.
28
+
29
+ ## Example
30
+
31
+ ```cpp
32
+ #include <napi.h>
33
+
34
+ class ExampleAddon : public Napi::Addon<ExampleAddon> {
35
+ public:
36
+ ExampleAddon(Napi::Env env, Napi::Object exports) {
37
+ // In the constructor we declare the functions the add-on makes avaialable
38
+ // to JavaScript.
39
+ DefineAddon(exports, {
40
+ InstanceMethod("increment", &ExampleAddon::Increment),
41
+
42
+ // We can also attach plain objects to `exports`, and instance methods as
43
+ // properties of those sub-objects.
44
+ InstanceValue("subObject", DefineProperties(Napi::Object::New(), {
45
+ InstanceMethod("decrement", &ExampleAddon::Decrement
46
+ })), napi_enumerable)
47
+ });
48
+ }
49
+ private:
50
+
51
+ // This method has access to the data stored in the environment because it is
52
+ // an instance method of `ExampleAddon` and because it was listed among the
53
+ // property descriptors passed to `DefineAddon()` in the constructor.
54
+ Napi::Value Increment(const Napi::CallbackInfo& info) {
55
+ return Napi::Number::New(info.Env(), ++value);
56
+ }
57
+
58
+ // This method has access to the data stored in the environment because it is
59
+ // an instance method of `ExampleAddon` and because it was exposed to
60
+ // JavaScript by calling `DefineProperties()` with the object onto which it is
61
+ // attached.
62
+ Napi::Value Decrement(const Napi::CallbackInfo& info) {
63
+ return Napi::Number::New(info.Env(), --value);
64
+ }
65
+
66
+ // Data stored in these variables is unique to each instance of the add-on.
67
+ uint32_t value = 42;
68
+ };
69
+
70
+ // The macro announces that instances of the class `ExampleAddon` will be
71
+ // created for each instance of the add-on that must be loaded into Node.js.
72
+ NODE_API_ADDON(ExampleAddon)
73
+ ```
74
+
75
+ The above code can be used from JavaScript as follows:
76
+
77
+ ```js
78
+ 'use strict'
79
+
80
+ const exampleAddon = require('bindings')('example_addon');
81
+ console.log(exampleAddon.increment()); // prints 43
82
+ console.log(exampleAddon.increment()); // prints 44
83
+ consnole.log(exampleAddon.subObject.decrement()); // prints 43
84
+ ```
85
+
86
+ When Node.js loads an instance of the add-on, a new instance of the class is
87
+ created. Its constructor receives the environment `Napi::Env env` and the
88
+ exports object `Napi::Object exports`. It can then use the method `DefineAddon`
89
+ to either attach methods, accessors, and/or values to the `exports` object or to
90
+ create its own `exports` object and attach methods, accessors, and/or values to
91
+ it.
92
+
93
+ Functions created with `Napi::Function::New()`, accessors created with
94
+ `PropertyDescriptor::Accessor()`, and values can also be attached. If their
95
+ implementation requires the `ExampleAddon` instance, it can be retrieved from
96
+ the `Napi::Env env` with `GetInstanceData()`:
97
+
98
+ ```cpp
99
+ void ExampleBinding(const Napi::CallbackInfo& info) {
100
+ ExampleAddon* addon = info.Env().GetInstanceData<ExampleAddon>();
101
+ }
102
+ ```
103
+
104
+ ## Methods
105
+
106
+ ### Constructor
107
+
108
+ Creates a new instance of the add-on.
109
+
110
+ ```cpp
111
+ Napi::Addon(Napi::Env env, Napi::Object exports);
112
+ ```
113
+
114
+ - `[in] env`: The environment into which the add-on is being loaded.
115
+ - `[in] exports`: The exports object received from JavaScript.
116
+
117
+ Typically, the constructor calls `DefineAddon()` to attach methods, accessors,
118
+ and/or values to `exports`. The constructor may also create a new object and
119
+ pass it to `DefineAddon()` as its first parameter if it wishes to replace the
120
+ `exports` object as provided by Node.js.
121
+
122
+ ### DefineAddon
123
+
124
+ Defines an add-on instance with functions, accessors, and/or values.
125
+
126
+ ```cpp
127
+ template <typename T>
128
+ void Napi::Addon<T>::DefineAddon(Napi::Object exports,
129
+ const std::initializer_list<PropertyDescriptor>& properties);
130
+ ```
131
+
132
+ * `[in] exports`: The object to return to Node.js as an instance of the add-on.
133
+ * `[in] properties`: Initializer list of add-on property descriptors of the
134
+ methods, property accessors, and values that define the add-on. They will be
135
+ set on `exports`.
136
+ See: [`Class property and descriptor`](class_property_descriptor.md).
137
+
138
+ ### DefineProperties
139
+
140
+ Defines function, accessor, and/or value properties on an object using add-on
141
+ instance methods.
142
+
143
+ ```cpp
144
+ template <typename T>
145
+ Napi::Object
146
+ Napi::Addon<T>::DefineProperties(Napi::Object object,
147
+ const std::initializer_list<PropertyDescriptor>& properties);
148
+ ```
149
+
150
+ * `[in] object`: The object that will receive the new properties.
151
+ * `[in] properties`: Initializer list of property descriptors of the methods,
152
+ property accessors, and values to attach to `object`.
153
+ See: [`Class property and descriptor`](class_property_descriptor.md).
154
+
155
+ Returns `object`.
156
+
157
+ [`Napi::InstanceWrap<T>`]: ./instance_wrap.md
package/doc/array.md ADDED
@@ -0,0 +1,81 @@
1
+ # Array
2
+
3
+ Class [`Napi::Array`][] inherits from class [`Napi::Object`][].
4
+
5
+ Arrays are native representations of JavaScript Arrays. `Napi::Array` is a wrapper
6
+ around `napi_value` representing a JavaScript Array.
7
+
8
+ [`Napi::TypedArray`][] and [`Napi::ArrayBuffer`][] correspond to JavaScript data
9
+ types such as [`Napi::Int32Array`][] and [`Napi::ArrayBuffer`][], respectively,
10
+ that can be used for transferring large amounts of data from JavaScript to the
11
+ native side. An example illustrating the use of a JavaScript-provided
12
+ `ArrayBuffer` in native code is available [here](https://github.com/nodejs/node-addon-examples/tree/master/array_buffer_to_native/node-addon-api).
13
+
14
+ ## Constructor
15
+ ```cpp
16
+ Napi::Array::Array();
17
+ ```
18
+
19
+ Returns an empty array.
20
+
21
+ If an error occurs, a `Napi::Error` will be thrown. If C++ exceptions are not
22
+ being used, callers should check the result of `Env::IsExceptionPending` before
23
+ attempting to use the returned value.
24
+
25
+ ```cpp
26
+ Napi::Array::Array(napi_env env, napi_value value);
27
+ ```
28
+ - `[in] env` - The environment in which to create the array.
29
+ - `[in] value` - The primitive to wrap.
30
+
31
+ Returns a `Napi::Array` wrapping a `napi_value`.
32
+
33
+ If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not
34
+ being used, callers should check the result of `Env::IsExceptionPending` before
35
+ attempting to use the returned value.
36
+
37
+ ## Methods
38
+
39
+ ### New
40
+ ```cpp
41
+ static Napi::Array Napi::Array::New(napi_env env);
42
+ ```
43
+ - `[in] env` - The environment in which to create the array.
44
+
45
+ Returns a new `Napi::Array`.
46
+
47
+ If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not
48
+ being used, callers should check the result of `Env::IsExceptionPending` before
49
+ attempting to use the returned value.
50
+
51
+ ### New
52
+
53
+ ```cpp
54
+ static Napi::Array Napi::Array::New(napi_env env, size_t length);
55
+ ```
56
+ - `[in] env` - The environment in which to create the array.
57
+ - `[in] length` - The length of the array.
58
+
59
+ Returns a new `Napi::Array` with the given length.
60
+
61
+ If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not
62
+ being used, callers should check the result of `Env::IsExceptionPending` before
63
+ attempting to use the returned value.
64
+
65
+ ### Length
66
+ ```cpp
67
+ uint32_t Napi::Array::Length() const;
68
+ ```
69
+
70
+ Returns the length of the array.
71
+
72
+ Note:
73
+ This can execute JavaScript code implicitly according to JavaScript semantics.
74
+ If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not
75
+ being used, callers should check the result of `Env::IsExceptionPending` before
76
+ attempting to use the returned value.
77
+
78
+ [`Napi::ArrayBuffer`]: ./array_buffer.md
79
+ [`Napi::Int32Array`]: ./typed_array_of.md
80
+ [`Napi::Object`]: ./object.md
81
+ [`Napi::TypedArray`]: ./typed_array.md
@@ -1,5 +1,7 @@
1
1
  # ArrayBuffer
2
2
 
3
+ Class `Napi::ArrayBuffer` inherits from class [`Napi::Object`][].
4
+
3
5
  The `Napi::ArrayBuffer` class corresponds to the
4
6
  [JavaScript `ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
5
7
  class.
@@ -127,3 +129,5 @@ void* Napi::ArrayBuffer::Data() const;
127
129
  ```
128
130
 
129
131
  Returns a pointer the wrapped data.
132
+
133
+ [`Napi::Object`]: ./object.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
@@ -91,3 +93,5 @@ void Napi::BigInt::ToWords(int* sign_bit, size_t* word_count, uint64_t* words);
91
93
 
92
94
  Returns a single `BigInt` value into a sign bit, 64-bit little-endian array,
93
95
  and the number of elements in the array.
96
+
97
+ [`Napi::Value`]: ./value.md