node-addon-api 1.5.0 → 1.6.3

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,6 +1,108 @@
1
1
  # node-addon-api Changelog
2
2
 
3
- ## 2018-10-03 Version 1.5.0 (Current), @NickNasso
3
+ ## 2019-04-03 Version 1.6.3, @NickNaso
4
+
5
+ ### Notable changes:
6
+
7
+ #### API
8
+
9
+ - Added `SuppressDestruct` method to `Napi::AsyncWorker`.
10
+ - Added new build targets for debug.
11
+ - Exposed macros that throw errors.
12
+ - Fixed memory leaks caused by callback data when a napi error occurs.
13
+ - Fixed missing `void *data` usage in `Napi::PropertyDescriptors`.
14
+
15
+ #### Documentation
16
+
17
+ - Some minor corrections all over the documentation
18
+
19
+ ### Commmits
20
+
21
+ * [[`83b41c2fe4`](https://github.com/nodejs/node-addon-api/commit/83b41c2fe4)] - Document adding -fvisibility=hidden flag for macOS users (Nicola Del Gobbo) [#460](https://github.com/nodejs/node-addon-api/pull/460)
22
+ * [[`1ed7ad8769`](https://github.com/nodejs/node-addon-api/commit/1ed7ad8769)] - **doc**: correct return type of Int32Value to int32\_t (Bill Gallafent) [#459](https://github.com/nodejs/node-addon-api/pull/459)
23
+ * [[`b0f6b601aa`](https://github.com/nodejs/node-addon-api/commit/b0f6b601aa)] - **src**: add AsyncWorker destruction suppression (Gabriel Schulhof) [#407](https://github.com/nodejs/node-addon-api/pull/407)
24
+ * [[`72b1975cff`](https://github.com/nodejs/node-addon-api/commit/72b1975cff)] - **doc**: fix links to the Property Descriptor docs (Ryuichi Okumura) [#458](https://github.com/nodejs/node-addon-api/pull/458)
25
+ * [[`fcfc612728`](https://github.com/nodejs/node-addon-api/commit/fcfc612728)] - **build**: new build targets for debug purposes (Jinho Bang) [#186](https://github.com/nodejs/node-addon-api/pull/186)
26
+ * [[`c629553cd7`](https://github.com/nodejs/node-addon-api/commit/c629553cd7)] - **doc**: minor doc corrections and clarifications (Bruce A. MacNaughton) [#426](https://github.com/nodejs/node-addon-api/pull/426)
27
+ * [[`7b87e0b999`](https://github.com/nodejs/node-addon-api/commit/7b87e0b999)] - **doc**: update number.md (Bernardo Heynemann) [#436](https://github.com/nodejs/node-addon-api/pull/436)
28
+ * [[`fcf173d2a1`](https://github.com/nodejs/node-addon-api/commit/fcf173d2a1)] - **src**: expose macros that throw errors (Gabriel Schulhof) [#448](https://github.com/nodejs/node-addon-api/pull/448)
29
+ * [[`b409a2f987`](https://github.com/nodejs/node-addon-api/commit/b409a2f987)] - **package**: add npm search keywords (Sam Roberts) [#452](https://github.com/nodejs/node-addon-api/pull/452)
30
+ * [[`0bc7987806`](https://github.com/nodejs/node-addon-api/commit/0bc7987806)] - **doc**: fix references to Weak and Persistent (Jake Barnes) [#428](https://github.com/nodejs/node-addon-api/pull/428)
31
+ * [[`ad6f569f85`](https://github.com/nodejs/node-addon-api/commit/ad6f569f85)] - **doc**: dix typo (Abhishek Kumar Singh) [#435](https://github.com/nodejs/node-addon-api/pull/435)
32
+ * [[`28df833a49`](https://github.com/nodejs/node-addon-api/commit/28df833a49)] - Merge pull request #441 from jschlight/master (Jim Schlight)
33
+ * [[`4921e74d83`](https://github.com/nodejs/node-addon-api/commit/4921e74d83)] - Rearranges names to be alphabetical (Jim Schlight)
34
+ * [[`48220335b0`](https://github.com/nodejs/node-addon-api/commit/48220335b0)] - Membership review update (Jim Schlight)
35
+ * [[`44f0695533`](https://github.com/nodejs/node-addon-api/commit/44f0695533)] - Merge pull request #394 from NickNaso/create\_release (Nicola DelGobbo)
36
+ * [[`fa49d68416`](https://github.com/nodejs/node-addon-api/commit/fa49d68416)] - **doc**: fix some `Finalizer` signatures (Philipp Renoth) [#414](https://github.com/nodejs/node-addon-api/pull/414)
37
+ * [[`020ac4a628`](https://github.com/nodejs/node-addon-api/commit/020ac4a628)] - **src**: make `Object::GetPropertyNames()` const (Philipp Renoth)[#415](https://github.com/nodejs/node-addon-api/pull/415)
38
+ * [[`91eaa6f4cb`](https://github.com/nodejs/node-addon-api/commit/91eaa6f4cb)] - **src**: fix callbackData leaks on error napi status (Philipp Renoth) [#417](https://github.com/nodejs/node-addon-api/pull/417)
39
+ * [[`0b40275752`](https://github.com/nodejs/node-addon-api/commit/0b40275752)] - **src**: fix noexcept control flow issues (Philipp Renoth) [#420](https://github.com/nodejs/node-addon-api/pull/420)
40
+ * [[`c1ff2936f9`](https://github.com/nodejs/node-addon-api/commit/c1ff2936f9)] - **src**: fix missing void\*data usage in PropertyDescriptors (Luciano Martorella) [#374](https://github.com/nodejs/node-addon-api/pull/374)
41
+
42
+ ## 2018-11-29 Version 1.6.2, @NickNaso
43
+
44
+ ### Notable changes:
45
+
46
+ #### API
47
+
48
+ - Fixed selection logic for version 6.x.
49
+
50
+ ### Commmits
51
+
52
+ * [[`07a0fc4e95`](https://github.com/nodejs/node-addon-api/commit/07a0fc4e95)] - **src**: fix selection logic for 6.x (Michael Dawson) [#402](https://github.com/nodejs/node-addon-api/pull/402)
53
+
54
+ ## 2018-11-14 Version 1.6.1, @NickNaso
55
+
56
+ ### Notable changes:
57
+
58
+ #### Documentation
59
+
60
+ - Updated links for examples to point to node-addon-examples repo.
61
+ - Fixed typos on some parts of documentation.
62
+
63
+ #### API
64
+
65
+ - Removed unused member on `Napi::CallbackScope`.
66
+ - Enabled `Napi::CallbackScope` only with N-API v3.
67
+
68
+ ### Commmits
69
+
70
+ * [[`e7cd292a74`](https://github.com/nodejs/node-addon-api/commit/e7cd292a74)] - **src**: remove unused CallbackScope member (Gabriel Schulhof) [#391](https://github.com/nodejs/node-addon-api/pull/391)
71
+ * [[`d47399fe25`](https://github.com/nodejs/node-addon-api/commit/d47399fe25)] - **src**: guard CallbackScope with N-API v3 (Michael Dawson) [#395](https://github.com/nodejs/node-addon-api/pull/395)
72
+ * [[`29a0262ab9`](https://github.com/nodejs/node-addon-api/commit/29a0262ab9)] - **doc**: fix typo (Dongjin Na) [#385](https://github.com/nodejs/node-addon-api/pull/385)
73
+ * [[`b6dc15b88d`](https://github.com/nodejs/node-addon-api/commit/b6dc15b88d)] - **doc**: make links point to node-addon-examples repo (Nicola Del Gobbo) [#389](https://github.com/nodejs/node-addon-api/pull/389)
74
+
75
+ ## 2018-11-02 Version 1.6.0, @NickNaso
76
+
77
+ ### Notable changes:
78
+
79
+ #### Documentation
80
+
81
+ - Improved documentation about ABI stability.
82
+
83
+ #### API
84
+
85
+ - Add `Napi::CallbackScope` class that help to have the equivalent of the scope
86
+ associated with a callback in place when making certain N-API calls
87
+
88
+ #### TEST
89
+
90
+ - Added tests for `Napi::Array` class.
91
+ - Added tests for `Napi::ArrayBuffer` class.
92
+
93
+ ### Commmits
94
+
95
+ * [[`8ce605c657`](https://github.com/nodejs/node-addon-api/commit/8ce605c657)] - **build**: avoid using package-lock.json (Jaeseok Yoon) [#359](https://github.com/nodejs/node-addon-api/pull/359)
96
+ * [[`fa3a6150b3`](https://github.com/nodejs/node-addon-api/commit/fa3a6150b3)] - **src**: use MakeCallback() -\> Call() in AsyncWorker (Jinho Bang) [#361](https://github.com/nodejs/node-addon-api/pull/361)
97
+ * [[`2342415463`](https://github.com/nodejs/node-addon-api/commit/2342415463)] - **test**: create test objects in the stack instead of the heap (Dongjin Na) [#371](https://github.com/nodejs/node-addon-api/pull/371)
98
+ * [[`67b7db0a6f`](https://github.com/nodejs/node-addon-api/commit/67b7db0a6f)] - **test**: write tests for Array class (Jaeseok Yoon) [#363](https://github.com/nodejs/node-addon-api/pull/363)
99
+ * [[`729f6dc4ee`](https://github.com/nodejs/node-addon-api/commit/729f6dc4ee)] - **test**: add arraybuffer tests (Dongjin Na) [#369](https://github.com/nodejs/node-addon-api/pull/369)
100
+ * [[`405f3e5b5b`](https://github.com/nodejs/node-addon-api/commit/405f3e5b5b)] - **src**: implement CallbackScope class (Jinho Bang) [#362](https://github.com/nodejs/node-addon-api/pull/362)
101
+ * [[`015d95312f`](https://github.com/nodejs/node-addon-api/commit/015d95312f)] - **doc**: fix Napi::Reference link (Gentilhomme) [#365](https://github.com/nodejs/node-addon-api/pull/365)
102
+ * [[`fd65078e3c`](https://github.com/nodejs/node-addon-api/commit/fd65078e3c)] - README.md: link to new ABI stability guide (Gabriel Schulhof) [#367](https://github.com/nodejs/node-addon-api/pull/367)
103
+ * [[`ffebf9ba9a`](https://github.com/nodejs/node-addon-api/commit/ffebf9ba9a)] - Updates for release 1.5.0 (NickNaso)
104
+
105
+ ## 2018-10-03 Version 1.5.0, @NickNaso
4
106
 
5
107
  ### Notable changes:
6
108
 
@@ -12,7 +114,7 @@
12
114
  #### API
13
115
 
14
116
  - Add `Napi::AsyncContext` class to handle asynchronous operation.
15
- - Add B`Napi::igInt` class to work with BigInt type.
117
+ - Add `Napi::BigInt` class to work with BigInt type.
16
118
  - Add `Napi::VersionManagement` class to retrieve the versions of Node.js and N-API.
17
119
  - Fix potential memory leaks.
18
120
  - DataView feature is enabled by default
@@ -65,7 +167,7 @@ yet backported in the previous Node.js version.
65
167
  * [[`622ffaea76`](https://github.com/nodejs/node-addon-api/commit/622ffaea76)] - **test**: Tighten up compiler warnings (Mikhail Cheshkov) [#315](https://github.com/nodejs/node-addon-api/pull/315)
66
168
  * [[`fd3c37b0f2`](https://github.com/nodejs/node-addon-api/commit/fd3c37b0f2)] - **tools**: add tool to check for N-API modules (Gabriel Schulhof) [#346](https://github.com/nodejs/node-addon-api/pull/346)
67
169
 
68
- ## 2018-07-19 Version 1.4.0, @NickNasso
170
+ ## 2018-07-19 Version 1.4.0, @NickNaso
69
171
 
70
172
  ### Notable changes:
71
173
 
package/README.md CHANGED
@@ -19,17 +19,19 @@ of Node.js.
19
19
 
20
20
  It is important to remember that *other* Node.js interfaces such as
21
21
  `libuv` (included in a project via `#include <uv.h>`) are not ABI-stable across
22
- Node.js major versions. Thus, and addon must use N-API and/or `node-addon-api`
22
+ Node.js major versions. Thus, an addon must use N-API and/or `node-addon-api`
23
23
  exclusively and build against a version of Node.js that includes an
24
24
  implementation of N-API (meaning a version of Node.js newer than 6.14.2) in
25
- order to benefit from ABI stability across Node.js major versions.
25
+ order to benefit from ABI stability across Node.js major versions. Node.js
26
+ provides an [ABI stability guide][] containing a detailed explanation of ABI
27
+ stability in general, and the N-API ABI stability guarantee in particular.
26
28
 
27
29
  As new APIs are added to N-API, node-addon-api must be updated to provide
28
30
  wrappers for those new APIs. For this reason node-addon-api provides
29
31
  methods that allow callers to obtain the underlying N-API handles so
30
32
  direct calls to N-API and the use of the objects/methods provided by
31
33
  node-addon-api can be used together. For example, in order to be able
32
- to use an API for which the node-add-api does not yet provide a wrapper.
34
+ to use an API for which the node-addon-api does not yet provide a wrapper.
33
35
 
34
36
  APIs exposed by node-addon-api are generally used to create and
35
37
  manipulate JavaScript values. Concepts and operations generally map
@@ -44,7 +46,7 @@ to ideas specified in the **ECMA262 Language Specification**.
44
46
  - **[Contributors](#contributors)**
45
47
  - **[License](#license)**
46
48
 
47
- ## **Current version: 1.5**
49
+ ## **Current version: 1.6.3**
48
50
 
49
51
  (See [CHANGELOG.md](CHANGELOG.md) for complete Changelog)
50
52
 
@@ -111,16 +113,16 @@ The following is the documentation for node-addon-api.
111
113
 
112
114
  ### **Examples**
113
115
 
114
- Are you new to **node-addon-api**? Take a look at our **[examples](https://github.com/nodejs/abi-stable-node-addon-examples)**
116
+ Are you new to **node-addon-api**? Take a look at our **[examples](https://github.com/nodejs/node-addon-examples)**
115
117
 
116
- - **[Hello World](https://github.com/nodejs/abi-stable-node-addon-examples/tree/master/1_hello_world/node-addon-api)**
117
- - **[Pass arguments to a function](https://github.com/nodejs/abi-stable-node-addon-examples/tree/master/2_function_arguments/node-addon-api)**
118
- - **[Callbacks](https://github.com/nodejs/abi-stable-node-addon-examples/tree/master/3_callbacks/node-addon-api)**
119
- - **[Object factory](https://github.com/nodejs/abi-stable-node-addon-examples/tree/master/4_object_factory/node-addon-api)**
120
- - **[Function factory](https://github.com/nodejs/abi-stable-node-addon-examples/tree/master/5_function_factory/node-addon-api)**
121
- - **[Wrapping C++ Object](https://github.com/nodejs/abi-stable-node-addon-examples/tree/master/6_object_wrap/node-addon-api)**
122
- - **[Factory of wrapped object](https://github.com/nodejs/abi-stable-node-addon-examples/tree/master/7_factory_wrap/node-addon-api)**
123
- - **[Passing wrapped object around](https://github.com/nodejs/abi-stable-node-addon-examples/tree/master/8_passing_wrapped/node-addon-api)**
118
+ - **[Hello World](https://github.com/nodejs/node-addon-examples/tree/master/1_hello_world/node-addon-api)**
119
+ - **[Pass arguments to a function](https://github.com/nodejs/node-addon-examples/tree/master/2_function_arguments/node-addon-api)**
120
+ - **[Callbacks](https://github.com/nodejs/node-addon-examples/tree/master/3_callbacks/node-addon-api)**
121
+ - **[Object factory](https://github.com/nodejs/node-addon-examples/tree/master/4_object_factory/node-addon-api)**
122
+ - **[Function factory](https://github.com/nodejs/node-addon-examples/tree/master/5_function_factory/node-addon-api)**
123
+ - **[Wrapping C++ Object](https://github.com/nodejs/node-addon-examples/tree/master/6_object_wrap/node-addon-api)**
124
+ - **[Factory of wrapped object](https://github.com/nodejs/node-addon-examples/tree/master/7_factory_wrap/node-addon-api)**
125
+ - **[Passing wrapped object around](https://github.com/nodejs/node-addon-examples/tree/master/8_passing_wrapped/node-addon-api)**
124
126
 
125
127
  <a name="tests"></a>
126
128
 
@@ -139,6 +141,20 @@ npm install
139
141
  npm test --disable-deprecated
140
142
  ```
141
143
 
144
+ ### **Debug**
145
+
146
+ To run the **node-addon-api** tests with `--debug` option:
147
+
148
+ ```
149
+ npm run-script dev
150
+ ```
151
+
152
+ If you want faster build, you might use the following option:
153
+
154
+ ```
155
+ npm run-script dev:incremental
156
+ ```
157
+
142
158
  Take a look and get inspired by our **[test suite](https://github.com/nodejs/node-addon-api/tree/master/test)**
143
159
 
144
160
  <a name="resources"></a>
@@ -150,20 +166,29 @@ Take a look and get inspired by our **[test suite](https://github.com/nodejs/nod
150
166
 
151
167
  <a name="contributors"></a>
152
168
 
153
- ### WG Members / Collaborators
154
- | Name | GitHub link |
169
+ ## WG Members / Collaborators
170
+
171
+ ### Active
172
+ | Name | GitHub Link |
155
173
  | ------------------- | ----------------------------------------------------- |
156
174
  | Anna Henningsen | [addaleax](https://github.com/addaleax) |
157
175
  | Arunesh Chandra | [aruneshchandra](https://github.com/aruneshchandra) |
158
- | Benjamin Byholm | [kkoopa](https://github.com/kkoopa) |
159
176
  | Gabriel Schulhof | [gabrielschulhof](https://github.com/gabrielschulhof) |
160
177
  | Hitesh Kanwathirtha | [digitalinfinity](https://github.com/digitalinfinity) |
161
- | Jason Ginchereau | [jasongin](https://github.com/jasongin) |
178
+ | Jim Schlight | [jschlight](https://github.com/jschlight) |
162
179
  | Michael Dawson | [mhdawson](https://github.com/mhdawson) |
163
180
  | Nicola Del Gobbo | [NickNaso](https://github.com/NickNaso) |
164
- | Sampson Gao | [sampsongao](https://github.com/sampsongao) |
165
181
  | Taylor Woll | [boingoing](https://github.com/boingoing) |
166
182
 
183
+ ### Emeritus
184
+ | Name | GitHub Link |
185
+ | ------------------- | ----------------------------------------------------- |
186
+ | Benjamin Byholm | [kkoopa](https://github.com/kkoopa) |
187
+ | Jason Ginchereau | [jasongin](https://github.com/jasongin) |
188
+ | Sampson Gao | [sampsongao](https://github.com/sampsongao) |
189
+
167
190
  <a name="license"></a>
168
191
 
169
192
  Licensed under [MIT](./LICENSE.md)
193
+
194
+ [ABI stability guide]: https://nodejs.org/en/docs/guides/abi-stability/
@@ -27,3 +27,5 @@ other asynchronous mechanism, the following API is necessary to ensure an
27
27
  asynchronous operation is properly tracked by the runtime:
28
28
 
29
29
  - **[AsyncContext](async_context.md)**
30
+
31
+ - **[CallbackScope](callback_scope.md)**
@@ -66,6 +66,15 @@ the computation that happened in the `Napi::AsyncWorker::Execute` method, unless
66
66
  the default implementation of `Napi::AsyncWorker::OnOK` or
67
67
  `Napi::AsyncWorker::OnError` is overridden.
68
68
 
69
+ ### SuppressDestruct
70
+
71
+ ```cpp
72
+ void Napi::AsyncWorker::SuppressDestruct();
73
+ ```
74
+
75
+ Prevents the destruction of the `Napi::AsyncWorker` instance upon completion of
76
+ the `Napi::AsyncWorker::OnOK` callback.
77
+
69
78
  ### SetError
70
79
 
71
80
  Sets the error message for the error that happened during the execution. Setting
@@ -0,0 +1,54 @@
1
+ # CallbackScope
2
+
3
+ There are cases (for example, resolving promises) where it is necessary to have
4
+ the equivalent of the scope associated with a callback in place when making
5
+ certain N-API calls.
6
+
7
+ ## Methods
8
+
9
+ ### Constructor
10
+
11
+ Creates a new callback scope on the stack.
12
+
13
+ ```cpp
14
+ Napi::CallbackScope::CallbackScope(napi_env env, napi_callback_scope scope);
15
+ ```
16
+
17
+ - `[in] env`: The environment in which to create the `Napi::CallbackScope`.
18
+ - `[in] scope`: The pre-existing `napi_callback_scope` or `Napi::CallbackScope`.
19
+
20
+ ### Constructor
21
+
22
+ Creates a new callback scope on the stack.
23
+
24
+ ```cpp
25
+ Napi::CallbackScope::CallbackScope(napi_env env, napi_async_context context);
26
+ ```
27
+
28
+ - `[in] env`: The environment in which to create the `Napi::CallbackScope`.
29
+ - `[in] async_context`: The pre-existing `napi_async_context` or `Napi::AsyncContext`.
30
+
31
+ ### Destructor
32
+
33
+ Deletes the instance of `Napi::CallbackScope` object.
34
+
35
+ ```cpp
36
+ virtual Napi::CallbackScope::~CallbackScope();
37
+ ```
38
+
39
+ ### Env
40
+
41
+ ```cpp
42
+ Napi::Env Napi::CallbackScope::Env() const;
43
+ ```
44
+
45
+ Returns the `Napi::Env` associated with the `Napi::CallbackScope`.
46
+
47
+ ## Operator
48
+
49
+ ```cpp
50
+ Napi::CallbackScope::operator napi_callback_scope() const;
51
+ ```
52
+
53
+ Returns the N-API `napi_callback_scope` wrapped by the `Napi::CallbackScope`
54
+ object. This can be used to mix usage of the C N-API and node-addon-api.
@@ -8,7 +8,7 @@ This prevents using descriptors from a different class when defining a new class
8
8
 
9
9
  ## Methods
10
10
 
11
- ### Contructor
11
+ ### Constructor
12
12
 
13
13
  Creates new instance of `Napi::ClassPropertyDescriptor` descriptor object.
14
14
 
@@ -33,4 +33,4 @@ Returns the original N-API `napi_property_descriptor` wrapped inside the `Napi::
33
33
  operator const napi_property_descriptor&() const { return _desc; }
34
34
  ```
35
35
 
36
- Returns the original N-API `napi_property_descriptor` wrapped inside the `Napi::ClassPropertyDescriptor`
36
+ Returns the original N-API `napi_property_descriptor` wrapped inside the `Napi::ClassPropertyDescriptor`
@@ -1,41 +1,62 @@
1
1
  # Creating a release
2
2
 
3
- Only collaborators in npm for node-addon-api can create releases.
3
+ Only collaborators in npm for **node-addon-api** can create releases.
4
4
  If you want to be able to do releases ask one of the existing
5
- collaborators to add you. If necessary you can ask the build
5
+ collaborators to add you. If necessary you can ask the build
6
6
  Working Group who manages the Node.js npm user to add you if
7
7
  there are no other active collaborators.
8
8
 
9
+ ## Prerequisites
10
+
11
+ Before to start creating a new release check if you have installed the following
12
+ tools:
13
+
14
+ * [Changelog maker](https://www.npmjs.com/package/changelog-maker)
15
+
16
+ If not please follow the instruction reported in the tool's documentation to
17
+ install it.
18
+
19
+ ## Publish new release
20
+
9
21
  These are the steps to follow to create a new release:
10
22
 
11
- * Open an issue in the node-addon-api repo documenting
12
- the intent to create a new release. Give people some
13
- time to comment or suggest PRs that should land first.
23
+ * Open an issue in the **node-addon-api** repo documenting the intent to create a
24
+ new release. Give people some time to comment or suggest PRs that should land first.
14
25
 
15
26
  * Validate all tests pass by running npm test on master.
16
27
 
17
- * Use https://ci.nodejs.org/view/x%20-%20Abi%20stable%20module%20API/job/node-test-node-addon-api/
18
- to validate tests pass for latest 9, 8, 6, 4 releases
19
- (note there are still some issues on SmartOS and
20
- Windows in the testing).
28
+ * Update the version in **package.json** appropriately.
21
29
 
22
- * Update the version in package.json appropriately.
30
+ * Update the [README.md](https://github.com/nodejs/node-addon-api/blob/master/README.md)
31
+ to show the new version as the latest.
23
32
 
24
- * Update the README.md to show the new version as the latest.
33
+ * Generate the changelog for the new version using **changelog maker** tool. From
34
+ the route folder of the repo launch the following command:
35
+
36
+ ```bash
37
+ > changelog-maker
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)
40
+ following the style used in publishing the previous release.
41
+
42
+ * Add any new contributors to the "contributors" section in the package.json
43
+
44
+ * Validate all tests pass by running npm test on master.
25
45
 
26
- * Add any new contributors to the "contributors" section in
27
- the package.json
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
48
+ Windows in the testing).
28
49
 
29
- * Do a clean checkout of node-add-api.
50
+ * Do a clean checkout of node-addon-api.
30
51
 
31
52
  * Login and then run `npm publish`.
32
53
 
33
54
  * Create a release in Github (look at existing releases for an example).
34
55
 
35
56
  * Validate that you can run `npm install node-addon-api` successfully
36
- and that the correct version is installed.
57
+ and that the correct version is installed.
37
58
 
38
- * Comment on the issue opened in the first step that the
39
- release has been created and close the issue.
59
+ * Comment on the issue opened in the first step that the release has been created
60
+ and close the issue.
40
61
 
41
62
  * Tweet that the release has been created.
@@ -153,3 +153,34 @@ if (env.IsExceptionPending()) {
153
153
 
154
154
  Since the exception was cleared here, it will not be propagated as a JavaScript
155
155
  exception after the native callback returns.
156
+
157
+ ## Calling N-API directly from a **node-addon-api** addon
158
+
159
+ **node-addon-api** provides macros for throwing errors in response to non-OK
160
+ `napi_status` results when calling [N-API](https://nodejs.org/docs/latest/api/n-api.html)
161
+ functions from within a native addon. These macros are defined differently
162
+ depending on whether C++ exceptions are enabled or not, but are available for
163
+ use in either case.
164
+
165
+ ### `NAPI_THROW(e, ...)`
166
+
167
+ This macro accepts a `Napi::Error`, throws it, and returns the value given as
168
+ the last parameter. If C++ exceptions are enabled (by defining
169
+ `NAPI_CPP_EXCEPTIONS` during the build), the return value will be ignored.
170
+
171
+ ### `NAPI_THROW_IF_FAILED(env, status, ...)`
172
+
173
+ This macro accepts a `Napi::Env` and a `napi_status`. It constructs an error
174
+ from the `napi_status`, throws it, and returns the value given as the last
175
+ parameter. If C++ exceptions are enabled (by defining `NAPI_CPP_EXCEPTIONS`
176
+ during the build), the return value will be ignored.
177
+
178
+ ### `NAPI_THROW_IF_FAILED_VOID(env, status)`
179
+
180
+ This macro accepts a `Napi::Env` and a `napi_status`. It constructs an error
181
+ from the `napi_status`, throws it, and returns.
182
+
183
+ ### `NAPI_FATAL_IF_FAILED(status, location, message)`
184
+
185
+ This macro accepts a `napi_status`, a C string indicating the location where the
186
+ error occurred, and a second C string for the message to display.
@@ -23,7 +23,7 @@ Creates a "weak" reference to the value, in that the initial reference count is
23
23
  set to 0.
24
24
 
25
25
  ```cpp
26
- static Napi::FunctionReference Napi::FunctionReference::Weak(const Napi::Function& value);
26
+ static Napi::FunctionReference Napi::Weak(const Napi::Function& value);
27
27
  ```
28
28
 
29
29
  - `[in] value`: The value which is to be referenced.
@@ -36,7 +36,7 @@ Creates a "persistent" reference to the value, in that the initial reference
36
36
  count is set to 1.
37
37
 
38
38
  ```cpp
39
- static Napi::FunctionReference Napi::FunctionReference::Persistent(const Napi::Function& value);
39
+ static Napi::FunctionReference Napi::Persistent(const Napi::Function& value);
40
40
  ```
41
41
 
42
42
  - `[in] value`: The value which is to be referenced.
package/doc/number.md CHANGED
@@ -24,8 +24,8 @@ Creates a new instance of a `Napi::Number` object.
24
24
  Napi::Number(napi_env env, napi_value value);
25
25
  ```
26
26
 
27
- - `[in] env`: The `napi_env` environment in which to construct the `Napi::Nuber` object.
28
- - `[in] value`: The `napi_value` which is a handle for a JavaScript `Number`.
27
+ - `[in] env`: The `napi_env` environment in which to construct the `Napi::Number` object.
28
+ - `[in] value`: The JavaScript value holding a number.
29
29
 
30
30
  Returns a non-empty `Napi::Number` object.
31
31
 
@@ -36,14 +36,14 @@ Napi::Number(napi_env env, napi_value value);
36
36
  ```cpp
37
37
  Napi::Number Napi::Number::New(napi_env env, double value);
38
38
  ```
39
- - `[in] env`: The `napi_env` environment in which to construct the `Napi::Nuber` object.
40
- - `[in] value`: The `napi_value` which is a handle for a JavaScript `Number`.
39
+ - `[in] env`: The `napi_env` environment in which to construct the `Napi::Number` object.
40
+ - `[in] value`: The C++ primitive from which to instantiate the `Napi::Number`.
41
41
 
42
42
  Creates a new instance of a `Napi::Number` object.
43
43
 
44
44
  ### Int32Value
45
45
 
46
- Converts a `Napi::Number` value to a `uint32_t` primitive type.
46
+ Converts a `Napi::Number` value to a `int32_t` primitive type.
47
47
 
48
48
  ```cpp
49
49
  Napi::Number::Int32Value() const;
package/doc/object.md CHANGED
@@ -108,7 +108,7 @@ Napi::Value Napi::Object::Get(____ key);
108
108
  ```
109
109
  - `[in] key`: The name of the property to return the value for.
110
110
 
111
- Returns the [`Napi::Value`](value.md) associated with the key property. Returns NULL if no such key exists.
111
+ Returns the [`Napi::Value`](value.md) associated with the key property. Returns the value *undefined* if the key does not exist.
112
112
 
113
113
  The `key` can be any of the following types:
114
114
  - `napi_value`
@@ -142,7 +142,7 @@ Note: This is equivalent to the JavaScript instanceof operator.
142
142
  ```cpp
143
143
  void Napi::Object::DefineProperty (const Napi::PropertyDescriptor& property);
144
144
  ```
145
- - `[in] property`: A [`Napi::PropertyDescriptor`](propertydescriptor.md).
145
+ - `[in] property`: A [`Napi::PropertyDescriptor`](property_descriptor.md).
146
146
 
147
147
  Define a property on the object.
148
148
 
@@ -151,7 +151,7 @@ Define a property on the object.
151
151
  ```cpp
152
152
  void Napi::Object::DefineProperties (____ properties)
153
153
  ```
154
- - `[in] properties`: A list of [`Napi::PropertyDescriptor`](propertydescriptor.md). Can be one of the following types:
154
+ - `[in] properties`: A list of [`Napi::PropertyDescriptor`](property_descriptor.md). Can be one of the following types:
155
155
  - const std::initializer_list<Napi::PropertyDescriptor>&
156
156
  - const std::vector<Napi::PropertyDescriptor>&
157
157
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  `Napi::ObjectReference` is a subclass of [`Napi::Reference`](reference.md), and is equivalent to an instance of `Napi::Reference<Object>`. This means that a `Napi::ObjectReference` holds a [`Napi::Object`](object.md), and a count of the number of references to that Object. When the count is greater than 0, an ObjectReference is not eligible for garbage collection. This ensures that the Object being held as a value of the ObjectReference will remain accessible, even if the original Object no longer is. However, ObjectReference is unique from a Reference since properties can be set and get to the Object itself that can be accessed through the ObjectReference.
4
4
 
5
- For more general information on references, please consult [`Napi::Reference`](referenc.md).
5
+ For more general information on references, please consult [`Napi::Reference`](reference.md).
6
6
 
7
7
  ## Example
8
8
  ```cpp
@@ -40,7 +40,7 @@ static Napi::ObjectReference Napi::ObjectReference::New(const Napi::Object& valu
40
40
  Returns the newly created reference.
41
41
 
42
42
  ```cpp
43
- static Napi::ObjectReference Napi::ObjectReference::Weak(const Napi::Object& value);
43
+ static Napi::ObjectReference Napi::Weak(const Napi::Object& value);
44
44
  ```
45
45
 
46
46
  Creates a "weak" reference to the value, in that the initial count of number of references is set to 0.
@@ -50,7 +50,7 @@ Creates a "weak" reference to the value, in that the initial count of number of
50
50
  Returns the newly created reference.
51
51
 
52
52
  ```cpp
53
- static Napi::ObjectReference Napi::ObjectReference::Persistent(const Napi::Object& value);
53
+ static Napi::ObjectReference Napi::Persistent(const Napi::Object& value);
54
54
  ```
55
55
 
56
56
  Creates a "persistent" reference to the value, in that the initial count of number of references is set to 1.
@@ -116,7 +116,7 @@ against the class constructor.
116
116
 
117
117
  ## Methods
118
118
 
119
- ### Contructor
119
+ ### Constructor
120
120
 
121
121
  Creates a new instance of a JavaScript object that wraps native instance.
122
122
 
@@ -192,7 +192,7 @@ static Napi::PropertyDescriptor Napi::PropertyDescriptor::Function (
192
192
  void *data = nullptr);
193
193
  ```
194
194
 
195
- * `[in] env`: The environemnt in which to create this accessor.
195
+ * `[in] env`: The environment in which to create this accessor.
196
196
  * `[in] name`: The name of the Callable function.
197
197
  * `[in] cb`: The function
198
198
  * `[in] attributes`: Potential attributes for the getter function.
package/doc/setup.md CHANGED
@@ -55,8 +55,19 @@ To use **N-API** in a native module:
55
55
  ```gyp
56
56
  'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
57
57
  ```
58
-
59
- 4. Include `napi.h` in the native module code.
58
+ 4. If you would like your native addon to support OSX, please also add the
59
+ following settings in the `binding.gyp` file:
60
+
61
+ ```gyp
62
+ ['OS=="mac"', {
63
+ 'cflags+': ['-fvisibility=hidden'],
64
+ 'xcode_settings': {
65
+ 'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden
66
+ }
67
+ }]
68
+ ```
69
+
70
+ 5. Include `napi.h` in the native module code.
60
71
  To ensure only ABI-stable APIs are used, DO NOT include
61
72
  `node.h`, `nan.h`, or `v8.h`.
62
73
 
package/index.js CHANGED
@@ -17,6 +17,7 @@ var versionArray = process.version
17
17
  var isNodeApiBuiltin = (
18
18
  versionArray[0] > 8 ||
19
19
  (versionArray[0] == 8 && versionArray[1] >= 6) ||
20
+ (versionArray[0] == 6 && versionArray[1] >= 15) ||
20
21
  (versionArray[0] == 6 && versionArray[1] >= 14 && versionArray[2] >= 2));
21
22
 
22
23
  // The flag is not needed when the Node version is not 8, nor if the API is
@@ -73,7 +73,7 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(const char* utf8name,
73
73
  void* /*data*/) {
74
74
  typedef details::AccessorCallbackData<Getter, Setter> CbData;
75
75
  // TODO: Delete when the function is destroyed
76
- auto callbackData = new CbData({ getter, setter });
76
+ auto callbackData = new CbData({ getter, setter, nullptr });
77
77
 
78
78
  return PropertyDescriptor({
79
79
  utf8name,
@@ -104,7 +104,7 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(napi_value name,
104
104
  void* /*data*/) {
105
105
  typedef details::AccessorCallbackData<Getter, Setter> CbData;
106
106
  // TODO: Delete when the function is destroyed
107
- auto callbackData = new CbData({ getter, setter });
107
+ auto callbackData = new CbData({ getter, setter, nullptr });
108
108
 
109
109
  return PropertyDescriptor({
110
110
  nullptr,
package/napi-inl.h CHANGED
@@ -17,49 +17,6 @@ namespace Napi {
17
17
  // Helpers to handle functions exposed from C++.
18
18
  namespace details {
19
19
 
20
- #ifdef NAPI_CPP_EXCEPTIONS
21
-
22
- #define NAPI_THROW(e) throw e
23
-
24
- // When C++ exceptions are enabled, Errors are thrown directly. There is no need
25
- // to return anything after the throw statement. The variadic parameter is an
26
- // optional return value that is ignored.
27
- #define NAPI_THROW_IF_FAILED(env, status, ...) \
28
- if ((status) != napi_ok) throw Error::New(env);
29
-
30
- #define NAPI_THROW_IF_FAILED_VOID(env, status) \
31
- if ((status) != napi_ok) throw Error::New(env);
32
-
33
- #else // NAPI_CPP_EXCEPTIONS
34
-
35
- #define NAPI_THROW(e) (e).ThrowAsJavaScriptException();
36
-
37
- // When C++ exceptions are disabled, Errors are thrown as JavaScript exceptions,
38
- // which are pending until the callback returns to JS. The variadic parameter
39
- // is an optional return value; usually it is an empty result.
40
- #define NAPI_THROW_IF_FAILED(env, status, ...) \
41
- if ((status) != napi_ok) { \
42
- Error::New(env).ThrowAsJavaScriptException(); \
43
- return __VA_ARGS__; \
44
- }
45
-
46
- // We need a _VOID version of this macro to avoid warnings resulting from
47
- // leaving the NAPI_THROW_IF_FAILED `...` argument empty.
48
- #define NAPI_THROW_IF_FAILED_VOID(env, status) \
49
- if ((status) != napi_ok) { \
50
- Error::New(env).ThrowAsJavaScriptException(); \
51
- return; \
52
- }
53
-
54
- #endif // NAPI_CPP_EXCEPTIONS
55
-
56
- #define NAPI_FATAL_IF_FAILED(status, location, message) \
57
- do { \
58
- if ((status) != napi_ok) { \
59
- Error::Fatal((location), (message)); \
60
- } \
61
- } while (0)
62
-
63
20
  // Attach a data item to an object and delete it when the object gets
64
21
  // garbage-collected.
65
22
  // TODO: Replace this code with `napi_add_finalizer()` whenever it becomes
@@ -176,6 +133,7 @@ struct AccessorCallbackData {
176
133
  CallbackInfo callbackInfo(env, info);
177
134
  AccessorCallbackData* callbackData =
178
135
  static_cast<AccessorCallbackData*>(callbackInfo.Data());
136
+ callbackInfo.SetData(callbackData->data);
179
137
  return callbackData->getterCallback(callbackInfo);
180
138
  });
181
139
  }
@@ -186,6 +144,7 @@ struct AccessorCallbackData {
186
144
  CallbackInfo callbackInfo(env, info);
187
145
  AccessorCallbackData* callbackData =
188
146
  static_cast<AccessorCallbackData*>(callbackInfo.Data());
147
+ callbackInfo.SetData(callbackData->data);
189
148
  callbackData->setterCallback(callbackInfo);
190
149
  return nullptr;
191
150
  });
@@ -193,6 +152,7 @@ struct AccessorCallbackData {
193
152
 
194
153
  Getter getterCallback;
195
154
  Setter setterCallback;
155
+ void* data;
196
156
  };
197
157
 
198
158
  } // namespace details
@@ -1060,7 +1020,7 @@ inline bool Object::Delete(uint32_t index) {
1060
1020
  return result;
1061
1021
  }
1062
1022
 
1063
- inline Array Object::GetPropertyNames() {
1023
+ inline Array Object::GetPropertyNames() const {
1064
1024
  napi_value result;
1065
1025
  napi_status status = napi_get_property_names(_env, _value, &result);
1066
1026
  NAPI_THROW_IF_FAILED(_env, status, Array());
@@ -1309,8 +1269,8 @@ inline DataView DataView::New(napi_env env,
1309
1269
  size_t byteOffset) {
1310
1270
  if (byteOffset > arrayBuffer.ByteLength()) {
1311
1271
  NAPI_THROW(RangeError::New(env,
1312
- "Start offset is outside the bounds of the buffer"));
1313
- return DataView();
1272
+ "Start offset is outside the bounds of the buffer"),
1273
+ DataView());
1314
1274
  }
1315
1275
  return New(env, arrayBuffer, byteOffset,
1316
1276
  arrayBuffer.ByteLength() - byteOffset);
@@ -1321,8 +1281,8 @@ inline DataView DataView::New(napi_env env,
1321
1281
  size_t byteOffset,
1322
1282
  size_t byteLength) {
1323
1283
  if (byteOffset + byteLength > arrayBuffer.ByteLength()) {
1324
- NAPI_THROW(RangeError::New(env, "Invalid DataView length"));
1325
- return DataView();
1284
+ NAPI_THROW(RangeError::New(env, "Invalid DataView length"),
1285
+ DataView());
1326
1286
  }
1327
1287
  napi_value value;
1328
1288
  napi_status status = napi_create_dataview(
@@ -1448,8 +1408,7 @@ inline T DataView::ReadData(size_t byteOffset) const {
1448
1408
  if (byteOffset + sizeof(T) > _length ||
1449
1409
  byteOffset + sizeof(T) < byteOffset) { // overflow
1450
1410
  NAPI_THROW(RangeError::New(_env,
1451
- "Offset is outside the bounds of the DataView"));
1452
- return 0;
1411
+ "Offset is outside the bounds of the DataView"), 0);
1453
1412
  }
1454
1413
 
1455
1414
  return *reinterpret_cast<T*>(static_cast<uint8_t*>(_data) + byteOffset);
@@ -1459,9 +1418,8 @@ template <typename T>
1459
1418
  inline void DataView::WriteData(size_t byteOffset, T value) const {
1460
1419
  if (byteOffset + sizeof(T) > _length ||
1461
1420
  byteOffset + sizeof(T) < byteOffset) { // overflow
1462
- NAPI_THROW(RangeError::New(_env,
1421
+ NAPI_THROW_VOID(RangeError::New(_env,
1463
1422
  "Offset is outside the bounds of the DataView"));
1464
- return;
1465
1423
  }
1466
1424
 
1467
1425
  *reinterpret_cast<T*>(static_cast<uint8_t*>(_data) + byteOffset) = value;
@@ -1597,7 +1555,7 @@ inline TypedArrayOf<T>::TypedArrayOf(napi_env env,
1597
1555
  : TypedArray(env, value, type, length), _data(data) {
1598
1556
  if (!(type == TypedArrayTypeForPrimitiveType<T>() ||
1599
1557
  (type == napi_uint8_clamped_array && std::is_same<T, uint8_t>::value))) {
1600
- NAPI_THROW(TypeError::New(env, "Array type must match the template parameter. "
1558
+ NAPI_THROW_VOID(TypeError::New(env, "Array type must match the template parameter. "
1601
1559
  "(Uint8 arrays may optionally have the \"clamped\" array type.)"));
1602
1560
  }
1603
1561
  }
@@ -1657,7 +1615,11 @@ inline Function Function::New(napi_env env,
1657
1615
  CbData::Wrapper,
1658
1616
  callbackData,
1659
1617
  &value);
1660
- NAPI_THROW_IF_FAILED(env, status, Function());
1618
+ if (status != napi_ok) {
1619
+ delete callbackData;
1620
+ NAPI_THROW_IF_FAILED(env, status, Function());
1621
+ }
1622
+
1661
1623
  return Function(env, value);
1662
1624
  }
1663
1625
 
@@ -2031,8 +1993,20 @@ inline const std::string& Error::Message() const NAPI_NOEXCEPT {
2031
1993
  inline void Error::ThrowAsJavaScriptException() const {
2032
1994
  HandleScope scope(_env);
2033
1995
  if (!IsEmpty()) {
1996
+
1997
+ // We intentionally don't use `NAPI_THROW_*` macros here to ensure
1998
+ // that there is no possible recursion as `ThrowAsJavaScriptException`
1999
+ // is part of `NAPI_THROW_*` macro definition for noexcept.
2000
+
2034
2001
  napi_status status = napi_throw(_env, Value());
2035
- NAPI_THROW_IF_FAILED_VOID(_env, status);
2002
+
2003
+ #ifdef NAPI_CPP_EXCEPTIONS
2004
+ if (status != napi_ok) {
2005
+ throw Error::New(_env);
2006
+ }
2007
+ #else // NAPI_CPP_EXCEPTIONS
2008
+ NAPI_FATAL_IF_FAILED(status, "Error::ThrowAsJavaScriptException", "napi_throw");
2009
+ #endif // NAPI_CPP_EXCEPTIONS
2036
2010
  }
2037
2011
  }
2038
2012
 
@@ -2603,12 +2577,15 @@ PropertyDescriptor::Accessor(Napi::Env env,
2603
2577
  const char* utf8name,
2604
2578
  Getter getter,
2605
2579
  napi_property_attributes attributes,
2606
- void* /*data*/) {
2580
+ void* data) {
2607
2581
  typedef details::CallbackData<Getter, Napi::Value> CbData;
2608
- auto callbackData = new CbData({ getter, nullptr });
2582
+ auto callbackData = new CbData({ getter, data });
2609
2583
 
2610
2584
  napi_status status = AttachData(env, object, callbackData);
2611
- NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor());
2585
+ if (status != napi_ok) {
2586
+ delete callbackData;
2587
+ NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor());
2588
+ }
2612
2589
 
2613
2590
  return PropertyDescriptor({
2614
2591
  utf8name,
@@ -2638,12 +2615,15 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env,
2638
2615
  Name name,
2639
2616
  Getter getter,
2640
2617
  napi_property_attributes attributes,
2641
- void* /*data*/) {
2618
+ void* data) {
2642
2619
  typedef details::CallbackData<Getter, Napi::Value> CbData;
2643
- auto callbackData = new CbData({ getter, nullptr });
2620
+ auto callbackData = new CbData({ getter, data });
2644
2621
 
2645
2622
  napi_status status = AttachData(env, object, callbackData);
2646
- NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor());
2623
+ if (status != napi_ok) {
2624
+ delete callbackData;
2625
+ NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor());
2626
+ }
2647
2627
 
2648
2628
  return PropertyDescriptor({
2649
2629
  nullptr,
@@ -2664,12 +2644,15 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env,
2664
2644
  Getter getter,
2665
2645
  Setter setter,
2666
2646
  napi_property_attributes attributes,
2667
- void* /*data*/) {
2647
+ void* data) {
2668
2648
  typedef details::AccessorCallbackData<Getter, Setter> CbData;
2669
- auto callbackData = new CbData({ getter, setter });
2649
+ auto callbackData = new CbData({ getter, setter, data });
2670
2650
 
2671
2651
  napi_status status = AttachData(env, object, callbackData);
2672
- NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor());
2652
+ if (status != napi_ok) {
2653
+ delete callbackData;
2654
+ NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor());
2655
+ }
2673
2656
 
2674
2657
  return PropertyDescriptor({
2675
2658
  utf8name,
@@ -2701,12 +2684,15 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env,
2701
2684
  Getter getter,
2702
2685
  Setter setter,
2703
2686
  napi_property_attributes attributes,
2704
- void* /*data*/) {
2687
+ void* data) {
2705
2688
  typedef details::AccessorCallbackData<Getter, Setter> CbData;
2706
- auto callbackData = new CbData({ getter, setter });
2689
+ auto callbackData = new CbData({ getter, setter, data });
2707
2690
 
2708
2691
  napi_status status = AttachData(env, object, callbackData);
2709
- NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor());
2692
+ if (status != napi_ok) {
2693
+ delete callbackData;
2694
+ NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor());
2695
+ }
2710
2696
 
2711
2697
  return PropertyDescriptor({
2712
2698
  nullptr,
@@ -3404,6 +3390,36 @@ inline Value EscapableHandleScope::Escape(napi_value escapee) {
3404
3390
  return Value(_env, result);
3405
3391
  }
3406
3392
 
3393
+
3394
+ #if (NAPI_VERSION > 2)
3395
+ ////////////////////////////////////////////////////////////////////////////////
3396
+ // CallbackScope class
3397
+ ////////////////////////////////////////////////////////////////////////////////
3398
+
3399
+ inline CallbackScope::CallbackScope(
3400
+ napi_env env, napi_callback_scope scope) : _env(env), _scope(scope) {
3401
+ }
3402
+
3403
+ inline CallbackScope::CallbackScope(napi_env env, napi_async_context context)
3404
+ : _env(env) {
3405
+ napi_status status = napi_open_callback_scope(
3406
+ _env, Object::New(env), context, &_scope);
3407
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
3408
+ }
3409
+
3410
+ inline CallbackScope::~CallbackScope() {
3411
+ napi_close_callback_scope(_env, _scope);
3412
+ }
3413
+
3414
+ inline CallbackScope::operator napi_callback_scope() const {
3415
+ return _scope;
3416
+ }
3417
+
3418
+ inline Napi::Env CallbackScope::Env() const {
3419
+ return Napi::Env(_env);
3420
+ }
3421
+ #endif
3422
+
3407
3423
  ////////////////////////////////////////////////////////////////////////////////
3408
3424
  // AsyncContext class
3409
3425
  ////////////////////////////////////////////////////////////////////////////////
@@ -3494,7 +3510,8 @@ inline AsyncWorker::AsyncWorker(const Object& receiver,
3494
3510
  const Object& resource)
3495
3511
  : _env(callback.Env()),
3496
3512
  _receiver(Napi::Persistent(receiver)),
3497
- _callback(Napi::Persistent(callback)) {
3513
+ _callback(Napi::Persistent(callback)),
3514
+ _suppress_destruct(false) {
3498
3515
  napi_value resource_id;
3499
3516
  napi_status status = napi_create_string_latin1(
3500
3517
  _env, resource_name, NAPI_AUTO_LENGTH, &resource_id);
@@ -3520,6 +3537,7 @@ inline AsyncWorker::AsyncWorker(AsyncWorker&& other) {
3520
3537
  _receiver = std::move(other._receiver);
3521
3538
  _callback = std::move(other._callback);
3522
3539
  _error = std::move(other._error);
3540
+ _suppress_destruct = other._suppress_destruct;
3523
3541
  }
3524
3542
 
3525
3543
  inline AsyncWorker& AsyncWorker::operator =(AsyncWorker&& other) {
@@ -3530,6 +3548,7 @@ inline AsyncWorker& AsyncWorker::operator =(AsyncWorker&& other) {
3530
3548
  _receiver = std::move(other._receiver);
3531
3549
  _callback = std::move(other._callback);
3532
3550
  _error = std::move(other._error);
3551
+ _suppress_destruct = other._suppress_destruct;
3533
3552
  return *this;
3534
3553
  }
3535
3554
 
@@ -3559,12 +3578,16 @@ inline FunctionReference& AsyncWorker::Callback() {
3559
3578
  return _callback;
3560
3579
  }
3561
3580
 
3581
+ inline void AsyncWorker::SuppressDestruct() {
3582
+ _suppress_destruct = true;
3583
+ }
3584
+
3562
3585
  inline void AsyncWorker::OnOK() {
3563
- _callback.MakeCallback(_receiver.Value(), std::initializer_list<napi_value>{});
3586
+ _callback.Call(_receiver.Value(), std::initializer_list<napi_value>{});
3564
3587
  }
3565
3588
 
3566
3589
  inline void AsyncWorker::OnError(const Error& e) {
3567
- _callback.MakeCallback(_receiver.Value(), std::initializer_list<napi_value>{ e.Value() });
3590
+ _callback.Call(_receiver.Value(), std::initializer_list<napi_value>{ e.Value() });
3568
3591
  }
3569
3592
 
3570
3593
  inline void AsyncWorker::SetError(const std::string& error) {
@@ -3599,7 +3622,9 @@ inline void AsyncWorker::OnWorkComplete(
3599
3622
  return nullptr;
3600
3623
  });
3601
3624
  }
3602
- delete self;
3625
+ if (!self->_suppress_destruct) {
3626
+ delete self;
3627
+ }
3603
3628
  }
3604
3629
 
3605
3630
  ////////////////////////////////////////////////////////////////////////////////
@@ -3631,10 +3656,6 @@ inline const napi_node_version* VersionManagement::GetNodeVersion(Env env) {
3631
3656
  return result;
3632
3657
  }
3633
3658
 
3634
- // These macros shouldn't be useful in user code.
3635
- #undef NAPI_THROW
3636
- #undef NAPI_THROW_IF_FAILED
3637
-
3638
3659
  } // namespace Napi
3639
3660
 
3640
3661
  #endif // SRC_NAPI_INL_H_
package/napi.h CHANGED
@@ -38,6 +38,64 @@ static_assert(sizeof(char16_t) == sizeof(wchar_t), "Size mismatch between char16
38
38
  #define NAPI_NOEXCEPT noexcept
39
39
  #endif
40
40
 
41
+ #ifdef NAPI_CPP_EXCEPTIONS
42
+
43
+ // When C++ exceptions are enabled, Errors are thrown directly. There is no need
44
+ // to return anything after the throw statements. The variadic parameter is an
45
+ // optional return value that is ignored.
46
+ // We need _VOID versions of the macros to avoid warnings resulting from
47
+ // leaving the NAPI_THROW_* `...` argument empty.
48
+
49
+ #define NAPI_THROW(e, ...) throw e
50
+ #define NAPI_THROW_VOID(e) throw e
51
+
52
+ #define NAPI_THROW_IF_FAILED(env, status, ...) \
53
+ if ((status) != napi_ok) throw Error::New(env);
54
+
55
+ #define NAPI_THROW_IF_FAILED_VOID(env, status) \
56
+ if ((status) != napi_ok) throw Error::New(env);
57
+
58
+ #else // NAPI_CPP_EXCEPTIONS
59
+
60
+ // When C++ exceptions are disabled, Errors are thrown as JavaScript exceptions,
61
+ // which are pending until the callback returns to JS. The variadic parameter
62
+ // is an optional return value; usually it is an empty result.
63
+ // We need _VOID versions of the macros to avoid warnings resulting from
64
+ // leaving the NAPI_THROW_* `...` argument empty.
65
+
66
+ #define NAPI_THROW(e, ...) \
67
+ do { \
68
+ (e).ThrowAsJavaScriptException(); \
69
+ return __VA_ARGS__; \
70
+ } while (0)
71
+
72
+ #define NAPI_THROW_VOID(e) \
73
+ do { \
74
+ (e).ThrowAsJavaScriptException(); \
75
+ return; \
76
+ } while (0)
77
+
78
+ #define NAPI_THROW_IF_FAILED(env, status, ...) \
79
+ if ((status) != napi_ok) { \
80
+ Error::New(env).ThrowAsJavaScriptException(); \
81
+ return __VA_ARGS__; \
82
+ }
83
+
84
+ #define NAPI_THROW_IF_FAILED_VOID(env, status) \
85
+ if ((status) != napi_ok) { \
86
+ Error::New(env).ThrowAsJavaScriptException(); \
87
+ return; \
88
+ }
89
+
90
+ #endif // NAPI_CPP_EXCEPTIONS
91
+
92
+ #define NAPI_FATAL_IF_FAILED(status, location, message) \
93
+ do { \
94
+ if ((status) != napi_ok) { \
95
+ Error::Fatal((location), (message)); \
96
+ } \
97
+ } while (0)
98
+
41
99
  ////////////////////////////////////////////////////////////////////////////////
42
100
  /// N-API C++ Wrapper Classes
43
101
  ///
@@ -601,7 +659,7 @@ namespace Napi {
601
659
  uint32_t index ///< Property / element index
602
660
  );
603
661
 
604
- Array GetPropertyNames(); ///< Get all property names
662
+ Array GetPropertyNames() const; ///< Get all property names
605
663
 
606
664
  /// Defines a property on the object.
607
665
  void DefineProperty(
@@ -633,12 +691,12 @@ namespace Napi {
633
691
  public:
634
692
  static External New(napi_env env, T* data);
635
693
 
636
- // Finalizer must implement operator() accepting a T* and returning void.
694
+ // Finalizer must implement `void operator()(Env env, T* data)`.
637
695
  template <typename Finalizer>
638
696
  static External New(napi_env env,
639
697
  T* data,
640
698
  Finalizer finalizeCallback);
641
- // Finalizer must implement operator() accepting a T* and Hint* and returning void.
699
+ // Finalizer must implement `void operator()(Env env, T* data, Hint* hint)`.
642
700
  template <typename Finalizer, typename Hint>
643
701
  static External New(napi_env env,
644
702
  T* data,
@@ -686,8 +744,7 @@ namespace Napi {
686
744
  size_t byteLength, ///< Length of the external buffer to be used by the array,
687
745
  /// in bytes
688
746
  Finalizer finalizeCallback ///< Function to be called when the array buffer is destroyed;
689
- /// must implement `operator()`, accept a `void*` (which is the
690
- /// data buffer pointer), and return `void`
747
+ /// must implement `void operator()(Env env, void* externalData)`
691
748
  );
692
749
 
693
750
  /// Creates a new ArrayBuffer instance, using an external buffer with specified byte length.
@@ -698,8 +755,7 @@ namespace Napi {
698
755
  size_t byteLength, ///< Length of the external buffer to be used by the array,
699
756
  /// in bytes
700
757
  Finalizer finalizeCallback, ///< Function to be called when the array buffer is destroyed;
701
- /// must implement `operator()`, accept a `void*` (which is the
702
- /// data buffer pointer) and `Hint*`, and return `void`
758
+ /// must implement `void operator()(Env env, void* externalData, Hint* hint)`
703
759
  Hint* finalizeHint ///< Hint (second parameter) to be passed to the finalize callback
704
760
  );
705
761
 
@@ -969,12 +1025,12 @@ namespace Napi {
969
1025
  static Buffer<T> New(napi_env env, size_t length);
970
1026
  static Buffer<T> New(napi_env env, T* data, size_t length);
971
1027
 
972
- // Finalizer must implement operator() accepting a T* and returning void.
1028
+ // Finalizer must implement `void operator()(Env env, T* data)`.
973
1029
  template <typename Finalizer>
974
1030
  static Buffer<T> New(napi_env env, T* data,
975
1031
  size_t length,
976
1032
  Finalizer finalizeCallback);
977
- // Finalizer must implement operator() accepting a T* and Hint* and returning void.
1033
+ // Finalizer must implement `void operator()(Env env, T* data, Hint* hint)`.
978
1034
  template <typename Finalizer, typename Hint>
979
1035
  static Buffer<T> New(napi_env env, T* data,
980
1036
  size_t length,
@@ -1671,6 +1727,23 @@ namespace Napi {
1671
1727
  napi_escapable_handle_scope _scope;
1672
1728
  };
1673
1729
 
1730
+ #if (NAPI_VERSION > 2)
1731
+ class CallbackScope {
1732
+ public:
1733
+ CallbackScope(napi_env env, napi_callback_scope scope);
1734
+ CallbackScope(napi_env env, napi_async_context context);
1735
+ virtual ~CallbackScope();
1736
+
1737
+ operator napi_callback_scope() const;
1738
+
1739
+ Napi::Env Env() const;
1740
+
1741
+ private:
1742
+ napi_env _env;
1743
+ napi_callback_scope _scope;
1744
+ };
1745
+ #endif
1746
+
1674
1747
  class AsyncContext {
1675
1748
  public:
1676
1749
  explicit AsyncContext(napi_env env, const char* resource_name);
@@ -1705,6 +1778,7 @@ namespace Napi {
1705
1778
 
1706
1779
  void Queue();
1707
1780
  void Cancel();
1781
+ void SuppressDestruct();
1708
1782
 
1709
1783
  ObjectReference& Receiver();
1710
1784
  FunctionReference& Callback();
@@ -1743,6 +1817,7 @@ namespace Napi {
1743
1817
  ObjectReference _receiver;
1744
1818
  FunctionReference _callback;
1745
1819
  std::string _error;
1820
+ bool _suppress_destruct;
1746
1821
  };
1747
1822
 
1748
1823
  // Memory management.
package/package.json CHANGED
@@ -3,6 +3,7 @@
3
3
  "url": "https://github.com/nodejs/node-addon-api/issues"
4
4
  },
5
5
  "contributors": [
6
+ "Abhishek Kumar Singh (https://github.com/abhi11210646)",
6
7
  "Andrew Petersen (https://github.com/kirbysayshi)",
7
8
  "Anisha Rohra (https://github.com/anisha-rohra)",
8
9
  "Anna Henningsen (https://github.com/addaleax)",
@@ -10,6 +11,8 @@
10
11
  "Arunesh Chandra (https://github.com/aruneshchandra)",
11
12
  "Ben Berman (https://github.com/rivertam)",
12
13
  "Benjamin Byholm (https://github.com/kkoopa)",
14
+ "Bill Gallafent (https://github.com/gallafent)",
15
+ "Bruce A. MacNaughton (https://github.com/bmacnaughton)",
13
16
  "Cory Mickelson (https://github.com/corymickelson)",
14
17
  "David Halls (https://github.com/davedoesdev)",
15
18
  "Dongjin Na (https://github.com/nadongguri)",
@@ -17,6 +20,7 @@
17
20
  "Gabriel Schulhof (https://github.com/gabrielschulhof)",
18
21
  "Gus Caplan (https://github.com/devsnek)",
19
22
  "Hitesh Kanwathirtha (https://github.com/digitalinfinity)",
23
+ "Jake Barnes (https://github.com/DuBistKomisch)",
20
24
  "Jake Yoon (https://github.com/yjaeseok)",
21
25
  "Jason Ginchereau (https://github.com/jasongin)",
22
26
  "Jim Schlight (https://github.com/jschlight)",
@@ -24,6 +28,7 @@
24
28
  "joshgarde (https://github.com/joshgarde)",
25
29
  "Konstantin Tarkus (https://github.com/koistya)",
26
30
  "Kyle Farnung (https://github.com/kfarnung)",
31
+ "Luciano Martorella (https://github.com/lmartorella)",
27
32
  "Matteo Collina (https://github.com/mcollina)",
28
33
  "Michael Dawson (https://github.com/mhdawson)",
29
34
  "Michele Campus (https://github.com/kYroL01)",
@@ -32,8 +37,11 @@
32
37
  "Nick Soggin (https://github.com/iSkore)",
33
38
  "Philipp Renoth (https://github.com/DaAitch)",
34
39
  "Rolf Timmermans (https://github.com/rolftimmermans)",
40
+ "Ryuichi Okumura (https://github.com/okuryu)",
35
41
  "Sampson Gao (https://github.com/sampsongao)",
36
- "Taylor Woll (https://github.com/boingoing)"
42
+ "Sam Roberts (https://github.com/sam-github)",
43
+ "Taylor Woll (https://github.com/boingoing)",
44
+ "Thomas Gentilhomme (https://github.com/fraxken)"
37
45
  ],
38
46
  "dependencies": {},
39
47
  "description": "Node.js API (N-API)",
@@ -42,6 +50,7 @@
42
50
  },
43
51
  "directories": {},
44
52
  "homepage": "https://github.com/nodejs/node-addon-api",
53
+ "keywords": ["n-api", "napi", "addon", "native", "bindings", "c", "c++", "nan", "node-addon-api"],
45
54
  "license": "MIT",
46
55
  "main": "index.js",
47
56
  "name": "node-addon-api",
@@ -54,7 +63,11 @@
54
63
  "scripts": {
55
64
  "pretest": "node-gyp rebuild -C test",
56
65
  "test": "node test",
66
+ "predev": "node-gyp rebuild -C test --debug",
67
+ "dev": "node test",
68
+ "predev:incremental": "node-gyp configure build -C test --debug",
69
+ "dev:incremental": "node test",
57
70
  "doc": "doxygen doc/Doxyfile"
58
71
  },
59
- "version": "1.5.0"
72
+ "version": "1.6.3"
60
73
  }
package/tools/README.md CHANGED
@@ -25,7 +25,7 @@ Here is the list of things that can be fixed easily.
25
25
 
26
26
 
27
27
  ### Major Reconstructions
28
- The implementation of `Napi::ObjectWrap` is significantly different from NAN's. `Napi::ObjectWrap` takes a pointer to the wrapped object and creates a reference to the wrapped object inside ObjectWrap constructor. `Napi::ObjectWrap` also associated wrapped object's instance methods to Javascript module instead of static methods like NAN.
28
+ The implementation of `Napi::ObjectWrap` is significantly different from NAN's. `Napi::ObjectWrap` takes a pointer to the wrapped object and creates a reference to the wrapped object inside ObjectWrap constructor. `Napi::ObjectWrap` also associates wrapped object's instance methods to Javascript module instead of static methods like NAN.
29
29
 
30
30
  So if you use Nan::ObjectWrap in your module, you will need to execute the following steps.
31
31
 
@@ -39,7 +39,7 @@ and define it as
39
39
  ...
40
40
  }
41
41
  ```
42
- This way, the `Napi::ObjectWrap` constructor will be invoked after the object has been instanciated and `Napi::ObjectWrap` can use the `this` pointer to create reference to the wrapped object.
42
+ This way, the `Napi::ObjectWrap` constructor will be invoked after the object has been instantiated and `Napi::ObjectWrap` can use the `this` pointer to create a reference to the wrapped object.
43
43
 
44
44
  2. Move your original constructor code into the new constructor. Delete your original constructor.
45
45
  3. In your class initialization function, associate native methods in the following way. The `&` character before methods is required because they are not static methods but instance methods.