node-addon-api 1.6.1 → 1.7.1

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,114 @@
1
1
  # node-addon-api Changelog
2
2
 
3
- ## 2018-11-14 Version 1.6.1 (Current), @NickNaso
3
+ ## 2019-07-23 Version 1.7.1, @NickNaso
4
+
5
+ ### Notable changes:
6
+
7
+ #### API
8
+
9
+ - Fixed compilation problems that happen on Node.js with N-API version less than 4.
10
+
11
+ ### Commmits
12
+
13
+ * [[`c20bcbd069`](https://github.com/nodejs/node-addon-api/commit/c20bcbd069)] - Merge pull request #518 from NickNaso/master (Nicola Del Gobbo)
14
+ * [[`6720d57253`](https://github.com/nodejs/node-addon-api/commit/6720d57253)] - Create the native threadsafe\_function for test only for N-API greater than 3. (NickNaso)
15
+ * [[`37b6c185ad`](https://github.com/nodejs/node-addon-api/commit/37b6c185ad)] - Fix compilation breakage on 1.7.0 (NickNaso)
16
+
17
+ ## 2019-07-23 Version 1.7.0, @NickNaso
18
+
19
+ ### Notable changes:
20
+
21
+ #### API
22
+
23
+ - Added `Napi::ThreadSafeFunction` api.
24
+ - Added `Napi::AsyncWorker::GetResult()` method to `Napi::AsyncWorker`.
25
+ - Added `Napi::AsyncWorker::Destroy()()` method to `Napi::AsyncWorker`.
26
+ - Use full namespace on macros that create the errors.
27
+
28
+ #### Documentation
29
+
30
+ - Added documentation about contribution philosophy.
31
+ - Added documentation for `Napi::ThreadSafeFunction`.
32
+ - Some minor corrections all over the documentation.
33
+
34
+ #### TEST
35
+
36
+ - Added test case for bool operator.
37
+ - Fixed test case for `Napi::ObjectWrap`.
38
+
39
+ ### Commmits
40
+
41
+ * [[`717c9ab163`](https://github.com/nodejs/node-addon-api/commit/717c9ab163)] - **AsyncWorker**: add GetResult() method (Kevin Eady) [#512](https://github.com/nodejs/node-addon-api/pull/512)
42
+ * [[`d9d991bbc9`](https://github.com/nodejs/node-addon-api/commit/d9d991bbc9)] - **doc**: add ThreadSafeFunction to main README (#513) (Kevin Eady) [#513](https://github.com/nodejs/node-addon-api/pull/513)
43
+ * [[`ac6000d0fd`](https://github.com/nodejs/node-addon-api/commit/ac6000d0fd)] - **doc**: fix minor typo (Yohei Kishimoto) [#510](https://github.com/nodejs/node-addon-api/pull/510)
44
+ * [[`e9fa1eaa86`](https://github.com/nodejs/node-addon-api/commit/e9fa1eaa86)] - **doc**: document ThreadSafeFunction (#494) (Kevin Eady) [#494](https://github.com/nodejs/node-addon-api/pull/494)
45
+ * [[`cab3b1e2a2`](https://github.com/nodejs/node-addon-api/commit/cab3b1e2a2)] - **doc**: ClassPropertyDescriptor example (Ross Weir) [#507](https://github.com/nodejs/node-addon-api/pull/507)
46
+ * [[`c32d7dbdcf`](https://github.com/nodejs/node-addon-api/commit/c32d7dbdcf)] - **macros**: create errors fully namespaced (Gabriel Schulhof) [#506](https://github.com/nodejs/node-addon-api/pull/506)
47
+ * [[`0a90df2fcb`](https://github.com/nodejs/node-addon-api/commit/0a90df2fcb)] - Implement ThreadSafeFunction class (Jinho Bang)
48
+ * [[`1fb540eeb5`](https://github.com/nodejs/node-addon-api/commit/1fb540eeb5)] - Use curly brackets to include node\_api.h (NickNaso) [#493](https://github.com/nodejs/node-addon-api/pull/493)
49
+ * [[`b2b08122ea`](https://github.com/nodejs/node-addon-api/commit/b2b08122ea)] - **AsyncWorker**: make callback optional (Kevin Eady) [#489](https://github.com/nodejs/node-addon-api/pull/489)
50
+ * [[`a0cac77c82`](https://github.com/nodejs/node-addon-api/commit/a0cac77c82)] - Added test for bool operator (NickNaso) [#490](https://github.com/nodejs/node-addon-api/pull/490)
51
+ * [[`ab7d8fcc48`](https://github.com/nodejs/node-addon-api/commit/ab7d8fcc48)] - **src**: fix objectwrap test case (Michael Dawson) [#495](https://github.com/nodejs/node-addon-api/pull/495)
52
+ * [[`3b6b9eb88a`](https://github.com/nodejs/node-addon-api/commit/3b6b9eb88a)] - **AsyncWorker**: introduce Destroy() method (Gabriel Schulhof) [#488](https://github.com/nodejs/node-addon-api/pull/488)
53
+ * [[`f633fbd95d`](https://github.com/nodejs/node-addon-api/commit/f633fbd95d)] - string.md: Document existing New(env, value, length) APIs (Tux3) [#486](https://github.com/nodejs/node-addon-api/pull/486)
54
+ * [[`aaea55eda9`](https://github.com/nodejs/node-addon-api/commit/aaea55eda9)] - Little fix on code example (Nicola Del Gobbo) [#470](https://github.com/nodejs/node-addon-api/pull/470)
55
+ * [[`e1cf9a35a1`](https://github.com/nodejs/node-addon-api/commit/e1cf9a35a1)] - Use `Value::IsEmpty` to check for empty value (NickNaso) [#478](https://github.com/nodejs/node-addon-api/pull/478)
56
+ * [[`3ad5dfc7d9`](https://github.com/nodejs/node-addon-api/commit/3ad5dfc7d9)] - Fix link (Alba Mendez) [#481](https://github.com/nodejs/node-addon-api/pull/481)
57
+ * [[`a3b4d99c45`](https://github.com/nodejs/node-addon-api/commit/a3b4d99c45)] - **doc**: Add contribution philosophy doc (Hitesh Kanwathirtha)
58
+ * [[`36863f087b`](https://github.com/nodejs/node-addon-api/commit/36863f087b)] - **doc**: refer to TypedArray and ArrayBuffer from Array (Gabriel "_|Nix|_" Schulhof) [#465](https://github.com/nodejs/node-addon-api/pull/465)
59
+
60
+ ## 2019-04-03 Version 1.6.3, @NickNaso
61
+
62
+ ### Notable changes:
63
+
64
+ #### API
65
+
66
+ - Added `SuppressDestruct` method to `Napi::AsyncWorker`.
67
+ - Added new build targets for debug.
68
+ - Exposed macros that throw errors.
69
+ - Fixed memory leaks caused by callback data when a napi error occurs.
70
+ - Fixed missing `void *data` usage in `Napi::PropertyDescriptors`.
71
+
72
+ #### Documentation
73
+
74
+ - Some minor corrections all over the documentation.
75
+
76
+ ### Commmits
77
+
78
+ * [[`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)
79
+ * [[`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)
80
+ * [[`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)
81
+ * [[`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)
82
+ * [[`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)
83
+ * [[`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)
84
+ * [[`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)
85
+ * [[`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)
86
+ * [[`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)
87
+ * [[`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)
88
+ * [[`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)
89
+ * [[`28df833a49`](https://github.com/nodejs/node-addon-api/commit/28df833a49)] - Merge pull request #441 from jschlight/master (Jim Schlight)
90
+ * [[`4921e74d83`](https://github.com/nodejs/node-addon-api/commit/4921e74d83)] - Rearranges names to be alphabetical (Jim Schlight)
91
+ * [[`48220335b0`](https://github.com/nodejs/node-addon-api/commit/48220335b0)] - Membership review update (Jim Schlight)
92
+ * [[`44f0695533`](https://github.com/nodejs/node-addon-api/commit/44f0695533)] - Merge pull request #394 from NickNaso/create\_release (Nicola DelGobbo)
93
+ * [[`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)
94
+ * [[`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)
95
+ * [[`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)
96
+ * [[`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)
97
+ * [[`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)
98
+
99
+ ## 2018-11-29 Version 1.6.2, @NickNaso
100
+
101
+ ### Notable changes:
102
+
103
+ #### API
104
+
105
+ - Fixed selection logic for version 6.x.
106
+
107
+ ### Commmits
108
+
109
+ * [[`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)
110
+
111
+ ## 2018-11-14 Version 1.6.1, @NickNaso
4
112
 
5
113
  ### Notable changes:
6
114
 
@@ -12,7 +120,7 @@
12
120
  #### API
13
121
 
14
122
  - Removed unused member on `Napi::CallbackScope`.
15
- - Enabled or disabled `Napi::CallbackScope` only with N-API v3.
123
+ - Enabled `Napi::CallbackScope` only with N-API v3.
16
124
 
17
125
  ### Commmits
18
126
 
@@ -21,7 +129,7 @@
21
129
  * [[`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)
22
130
  * [[`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)
23
131
 
24
- ## 2018-11-02 Version 1.6.0 (Current), @NickNaso
132
+ ## 2018-11-02 Version 1.6.0, @NickNaso
25
133
 
26
134
  ### Notable changes:
27
135
 
@@ -51,7 +159,7 @@ associated with a callback in place when making certain N-API calls
51
159
  * [[`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)
52
160
  * [[`ffebf9ba9a`](https://github.com/nodejs/node-addon-api/commit/ffebf9ba9a)] - Updates for release 1.5.0 (NickNaso)
53
161
 
54
- ## 2018-10-03 Version 1.5.0 (Current), @NickNaso
162
+ ## 2018-10-03 Version 1.5.0, @NickNaso
55
163
 
56
164
  ### Notable changes:
57
165
 
@@ -0,0 +1,66 @@
1
+ # **node-addon-api** Contribution Philosophy
2
+
3
+ The **node-addon-api** team loves contributions. There are many ways in which you can
4
+ contribute to **node-addon-api**:
5
+ - Source code fixes
6
+ - Additional tests
7
+ - Documentation improvements
8
+ - Joining the N-API working group and participating in meetings
9
+
10
+ ## Source changes
11
+
12
+ **node-addon-api** is meant to be a thin convenience wrapper around N-API. With this
13
+ in mind, contributions of any new APIs that wrap around a core N-API API will
14
+ be considered for merge. However, changes that wrap existing **node-addon-api**
15
+ APIs are encouraged to instead be provided as an ecosystem module. The
16
+ **node-addon-api** team is happy to link to a curated set of modules that build on
17
+ top of **node-addon-api** if they have broad usefulness to the community and promote
18
+ a recommended idiom or pattern.
19
+
20
+ ### Rationale
21
+
22
+ The N-API team considered a couple different approaches with regards to changes
23
+ extending **node-addon-api**
24
+ - Larger core module - Incorporate these helpers and patterns into **node-addon-api**
25
+ - Extras package - Create a new package (strawman name '**node-addon-api**-extras')
26
+ that contain utility classes and methods that help promote good patterns and
27
+ idioms while writing native addons with **node-addon-api**.
28
+ - Ecosystem - Encourage creation of a module ecosystem around **node-addon-api**
29
+ where folks can build on top of it.
30
+
31
+ #### Larger Core
32
+ This is probably our simplest option in terms of immediate action needed. It
33
+ would involve landing any open PRs against **node-addon-api**, and continuing to
34
+ encourage folks to make PRs for utility helpers against the same repository.
35
+
36
+ The downside of the approach is the following:
37
+ - Less coherency for our API set
38
+ - More maintenance burden on the N-API WG core team.
39
+
40
+ #### Extras Package
41
+ This involves us spinning up a new package which contains the utility classes
42
+ and methods. This has the benefit of having a separate module where helpers
43
+ which make it easier to implement certain patterns and idioms for native addons
44
+ easier.
45
+
46
+ The downside of this approach is the following:
47
+ - Potential for confusion - we'll need to provide clear documentation to help the
48
+ community understand where a particular contribution should be directed to (what
49
+ belongs in **node-addon-api** vs **node-addon-api-extras**)
50
+ - Need to define the level of support/API guarantees
51
+ - Unclear if the maintenance burden on the N-API WG is reduced or not
52
+
53
+ #### Ecosystem
54
+ This doesn't require a ton of up-front work from the N-API WG. Instead of
55
+ accepting utility PRs into **node-addon-api** or creating and maintaining a new
56
+ module, the WG will encourage the creation of an ecosystem of modules that
57
+ build on top of **node-addon-api**, and provide some level of advertising for these
58
+ modules (listing them out on the repository/wiki, using them in workshops/tutorials
59
+ etc).
60
+
61
+ The downside of this approach is the following:
62
+ - Potential for lack of visibility - evangelism and education is hard, and module
63
+ authors might not find right patterns and instead implement things themselves
64
+ - There might be greater friction for the N-API WG in evolving APIs since the
65
+ ecosystem would have taken dependencies on the API shape of **node-addon-api**
66
+
package/README.md CHANGED
@@ -46,7 +46,7 @@ to ideas specified in the **ECMA262 Language Specification**.
46
46
  - **[Contributors](#contributors)**
47
47
  - **[License](#license)**
48
48
 
49
- ## **Current version: 1.6.1**
49
+ ## **Current version: 1.7.1**
50
50
 
51
51
  (See [CHANGELOG.md](CHANGELOG.md) for complete Changelog)
52
52
 
@@ -106,6 +106,7 @@ The following is the documentation for node-addon-api.
106
106
  - [Async Operations](doc/async_operations.md)
107
107
  - [AsyncWorker](doc/async_worker.md)
108
108
  - [AsyncContext](doc/async_context.md)
109
+ - [Thread-safe Functions](doc/threadsafe_function.md)
109
110
  - [Promises](doc/promises.md)
110
111
  - [Version management](doc/version_management.md)
111
112
 
@@ -141,10 +142,29 @@ npm install
141
142
  npm test --disable-deprecated
142
143
  ```
143
144
 
145
+ ### **Debug**
146
+
147
+ To run the **node-addon-api** tests with `--debug` option:
148
+
149
+ ```
150
+ npm run-script dev
151
+ ```
152
+
153
+ If you want faster build, you might use the following option:
154
+
155
+ ```
156
+ npm run-script dev:incremental
157
+ ```
158
+
144
159
  Take a look and get inspired by our **[test suite](https://github.com/nodejs/node-addon-api/tree/master/test)**
145
160
 
146
161
  <a name="resources"></a>
147
162
 
163
+ ## **Contributing**
164
+
165
+ We love contributions from the community to **node-addon-api**.
166
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for more details on our philosophy around extending this module.
167
+
148
168
  ### **More resource and info about native Addons**
149
169
  - **[C++ Addons](https://nodejs.org/dist/latest/docs/api/addons.html)**
150
170
  - **[N-API](https://nodejs.org/dist/latest/docs/api/n-api.html)**
@@ -152,20 +172,27 @@ Take a look and get inspired by our **[test suite](https://github.com/nodejs/nod
152
172
 
153
173
  <a name="contributors"></a>
154
174
 
155
- ### WG Members / Collaborators
156
- | Name | GitHub link |
175
+ ## WG Members / Collaborators
176
+
177
+ ### Active
178
+ | Name | GitHub Link |
157
179
  | ------------------- | ----------------------------------------------------- |
158
180
  | Anna Henningsen | [addaleax](https://github.com/addaleax) |
159
181
  | Arunesh Chandra | [aruneshchandra](https://github.com/aruneshchandra) |
160
- | Benjamin Byholm | [kkoopa](https://github.com/kkoopa) |
161
182
  | Gabriel Schulhof | [gabrielschulhof](https://github.com/gabrielschulhof) |
162
183
  | Hitesh Kanwathirtha | [digitalinfinity](https://github.com/digitalinfinity) |
163
- | Jason Ginchereau | [jasongin](https://github.com/jasongin) |
184
+ | Jim Schlight | [jschlight](https://github.com/jschlight) |
164
185
  | Michael Dawson | [mhdawson](https://github.com/mhdawson) |
165
186
  | Nicola Del Gobbo | [NickNaso](https://github.com/NickNaso) |
166
- | Sampson Gao | [sampsongao](https://github.com/sampsongao) |
167
187
  | Taylor Woll | [boingoing](https://github.com/boingoing) |
168
188
 
189
+ ### Emeritus
190
+ | Name | GitHub Link |
191
+ | ------------------- | ----------------------------------------------------- |
192
+ | Benjamin Byholm | [kkoopa](https://github.com/kkoopa) |
193
+ | Jason Ginchereau | [jasongin](https://github.com/jasongin) |
194
+ | Sampson Gao | [sampsongao](https://github.com/sampsongao) |
195
+
169
196
  <a name="license"></a>
170
197
 
171
198
  Licensed under [MIT](./LICENSE.md)
@@ -66,11 +66,20 @@ 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
72
81
  an error message will cause the `Napi::AsyncWorker::OnError` method to be
73
- invoked instead of `Napi::AsyncWorker::OnOKOnOK` once the
82
+ invoked instead of `Napi::AsyncWorker::OnOK` once the
74
83
  `Napi::AsyncWorker::Execute` method completes.
75
84
 
76
85
  ```cpp
@@ -96,17 +105,27 @@ virtual void Napi::AsyncWorker::Execute() = 0;
96
105
 
97
106
  ### OnOK
98
107
 
99
- This method is invoked when the computation in the `Excecute` method ends.
100
- The default implementation runs the Callback provided when the AsyncWorker class
101
- was created.
108
+ This method is invoked when the computation in the `Execute` method ends.
109
+ The default implementation runs the Callback optionally provided when the AsyncWorker class
110
+ was created. The callback will by default receive no arguments. To provide arguments,
111
+ override the `GetResult()` method.
102
112
 
103
113
  ```cpp
104
114
  virtual void Napi::AsyncWorker::OnOK();
105
115
  ```
116
+ ### GetResult
117
+
118
+ This method returns the arguments passed to the Callback invoked by the default
119
+ `OnOK()` implementation. The default implementation returns an empty vector,
120
+ providing no arguments to the Callback.
121
+
122
+ ```cpp
123
+ virtual std::vector<napi_value> Napi::AsyncWorker::GetResult(Napi::Env env);
124
+ ```
106
125
 
107
126
  ### OnError
108
127
 
109
- This method is invoked afer `Napi::AsyncWorker::Execute` completes if an error
128
+ This method is invoked after `Napi::AsyncWorker::Execute` completes if an error
110
129
  occurs while `Napi::AsyncWorker::Execute` is running and C++ exceptions are
111
130
  enabled or if an error was set through a call to `Napi::AsyncWorker::SetError`.
112
131
  The default implementation calls the callback provided when the `Napi::AsyncWorker`
@@ -116,6 +135,19 @@ class was created, passing in the error as the first parameter.
116
135
  virtual void Napi::AsyncWorker::OnError(const Napi::Error& e);
117
136
  ```
118
137
 
138
+ ### Destroy
139
+
140
+ This method is invoked when the instance must be deallocated. If
141
+ `SuppressDestruct()` was not called then this method will be called after either
142
+ `OnError()` or `OnOK()` complete. The default implementation of this method
143
+ causes the instance to delete itself using the `delete` operator. The method is
144
+ provided so as to ensure that instances allocated by means other than the `new`
145
+ operator can be deallocated upon work completion.
146
+
147
+ ```cpp
148
+ virtual void Napi::AsyncWorker::Destroy();
149
+ ```
150
+
119
151
  ### Constructor
120
152
 
121
153
  Creates a new `Napi::AsyncWorker`.
@@ -127,7 +159,7 @@ explicit Napi::AsyncWorker(const Napi::Function& callback);
127
159
  - `[in] callback`: The function which will be called when an asynchronous
128
160
  operations ends. The given function is called from the main event loop thread.
129
161
 
130
- Returns a`Napi::AsyncWork` instance which can later be queued for execution by calling
162
+ Returns a `Napi::AsyncWorker` instance which can later be queued for execution by calling
131
163
  `Queue`.
132
164
 
133
165
  ### Constructor
@@ -144,7 +176,7 @@ operations ends. The given function is called from the main event loop thread.
144
176
  identifier for the kind of resource that is being provided for diagnostic
145
177
  information exposed by the async_hooks API.
146
178
 
147
- Returns a `Napi::AsyncWork` instance which can later be queued for execution by
179
+ Returns a `Napi::AsyncWorker` instance which can later be queued for execution by
148
180
  calling `Napi::AsyncWork::Queue`.
149
181
 
150
182
  ### Constructor
@@ -163,7 +195,7 @@ information exposed by the async_hooks API.
163
195
  - `[in] resource`: Object associated with the asynchronous operation that
164
196
  will be passed to possible async_hooks.
165
197
 
166
- Returns a `Napi::AsyncWork` instance which can later be queued for execution by
198
+ Returns a `Napi::AsyncWorker` instance which can later be queued for execution by
167
199
  calling `Napi::AsyncWork::Queue`.
168
200
 
169
201
  ### Constructor
@@ -178,7 +210,7 @@ explicit Napi::AsyncWorker(const Napi::Object& receiver, const Napi::Function& c
178
210
  - `[in] callback`: The function which will be called when an asynchronous
179
211
  operations ends. The given function is called from the main event loop thread.
180
212
 
181
- Returns a `Napi::AsyncWork` instance which can later be queued for execution by
213
+ Returns a `Napi::AsyncWorker` instance which can later be queued for execution by
182
214
  calling `Napi::AsyncWork::Queue`.
183
215
 
184
216
  ### Constructor
@@ -186,7 +218,7 @@ calling `Napi::AsyncWork::Queue`.
186
218
  Creates a new `Napi::AsyncWorker`.
187
219
 
188
220
  ```cpp
189
- explicit Napi::AsyncWorker(const Napi::Object& receiver, const Napi::Function& callback,const char* resource_name);
221
+ explicit Napi::AsyncWorker(const Napi::Object& receiver, const Napi::Function& callback, const char* resource_name);
190
222
  ```
191
223
 
192
224
  - `[in] receiver`: The `this` object passed to the called function.
@@ -219,6 +251,54 @@ will be passed to possible async_hooks.
219
251
  Returns a `Napi::AsyncWork` instance which can later be queued for execution by
220
252
  calling `Napi::AsyncWork::Queue`.
221
253
 
254
+
255
+ ### Constructor
256
+
257
+ Creates a new `Napi::AsyncWorker`.
258
+
259
+ ```cpp
260
+ explicit Napi::AsyncWorker(Napi::Env env);
261
+ ```
262
+
263
+ - `[in] env`: The environment in which to create the `Napi::AsyncWorker`.
264
+
265
+ Returns an `Napi::AsyncWorker` instance which can later be queued for execution by calling
266
+ `Napi::AsyncWorker::Queue`.
267
+
268
+ ### Constructor
269
+
270
+ Creates a new `Napi::AsyncWorker`.
271
+
272
+ ```cpp
273
+ explicit Napi::AsyncWorker(Napi::Env env, const char* resource_name);
274
+ ```
275
+
276
+ - `[in] env`: The environment in which to create the `Napi::AsyncWorker`.
277
+ - `[in] resource_name`: Null-terminated strings that represents the
278
+ identifier for the kind of resource that is being provided for diagnostic
279
+ information exposed by the async_hooks API.
280
+
281
+ Returns a `Napi::AsyncWorker` instance which can later be queued for execution by
282
+ calling `Napi::AsyncWorker::Queue`.
283
+
284
+ ### Constructor
285
+
286
+ Creates a new `Napi::AsyncWorker`.
287
+
288
+ ```cpp
289
+ explicit Napi::AsyncWorker(Napi::Env env, const char* resource_name, const Napi::Object& resource);
290
+ ```
291
+
292
+ - `[in] env`: The environment in which to create the `Napi::AsyncWorker`.
293
+ - `[in] resource_name`: Null-terminated strings that represents the
294
+ identifier for the kind of resource that is being provided for diagnostic
295
+ information exposed by the async_hooks API.
296
+ - `[in] resource`: Object associated with the asynchronous operation that
297
+ will be passed to possible async_hooks.
298
+
299
+ Returns a `Napi::AsyncWorker` instance which can later be queued for execution by
300
+ calling `Napi::AsyncWorker::Queue`.
301
+
222
302
  ### Destructor
223
303
 
224
304
  Deletes the created work object that is used to execute logic asynchronously.
@@ -291,7 +371,7 @@ the work on the `Napi::AsyncWorker::Execute` method is done the
291
371
  `Napi::AsyncWorker::OnOk` method is called and the results return back to
292
372
  JavaScript invoking the stored callback with its associated environment.
293
373
 
294
- The following code shows an example on how to create and and use an `Napi::AsyncWorker`
374
+ The following code shows an example on how to create and use an `Napi::AsyncWorker`
295
375
 
296
376
  ```cpp
297
377
  #include<napi.h>
@@ -339,6 +339,12 @@ The value is not coerced to a string.
339
339
  Arrays are native representations of JavaScript Arrays. `Napi::Array` is a wrapper
340
340
  around `napi_value` representing a JavaScript Array.
341
341
 
342
+ [`Napi::TypedArray`][] and [`Napi::ArrayBuffer`][] correspond to JavaScript data
343
+ types such as [`Int32Array`][] and [`ArrayBuffer`][], respectively, that can be
344
+ used for transferring large amounts of data from JavaScript to the native side.
345
+ An example illustrating the use of a JavaScript-provided `ArrayBuffer` in native
346
+ code is available [here](https://github.com/nodejs/node-addon-examples/tree/master/array_buffer_to_native/node-addon-api).
347
+
342
348
  ### Constructor
343
349
  ```cpp
344
350
  Napi::Array::Array();
@@ -402,3 +408,8 @@ This can execute JavaScript code implicitly according to JavaScript semantics.
402
408
  If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not
403
409
  being used, callers should check the result of `Env::IsExceptionPending` before
404
410
  attempting to use the returned value.
411
+
412
+ [`Napi::TypedArray`]: ./typed_array.md
413
+ [`Napi::ArrayBuffer`]: ./array_buffer.md
414
+ [`Int32Array`]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Int32Array
415
+ [`ArrayBuffer`]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
@@ -1,4 +1,4 @@
1
- # Class propertry and descriptor
1
+ # Class property and descriptor
2
2
 
3
3
  Property descriptor for use with `Napi::ObjectWrap::DefineClass()`.
4
4
  This is different from the standalone `Napi::PropertyDescriptor` because it is
@@ -6,9 +6,91 @@ specific to each `Napi::ObjectWrap<T>` subclass.
6
6
  This prevents using descriptors from a different class when defining a new class
7
7
  (preventing the callbacks from having incorrect `this` pointers).
8
8
 
9
+ ## Example
10
+
11
+ ```cpp
12
+ #include <napi.h>
13
+
14
+ class Example : public Napi::ObjectWrap<Example> {
15
+ public:
16
+ static Napi::Object Init(Napi::Env env, Napi::Object exports);
17
+ Example(const Napi::CallbackInfo &info);
18
+
19
+ private:
20
+ static Napi::FunctionReference constructor;
21
+ double _value;
22
+ Napi::Value GetValue(const Napi::CallbackInfo &info);
23
+ Napi::Value SetValue(const Napi::CallbackInfo &info);
24
+ };
25
+
26
+ Napi::Object Example::Init(Napi::Env env, Napi::Object exports) {
27
+ Napi::Function func = DefineClass(env, "Example", {
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)
32
+ });
33
+
34
+ constructor = Napi::Persistent(func);
35
+ constructor.SuppressDestruct();
36
+ exports.Set("Example", func);
37
+
38
+ return exports;
39
+ }
40
+
41
+ Example::Example(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Example>(info) {
42
+ Napi::Env env = info.Env();
43
+ // ...
44
+ Napi::Number value = info[0].As<Napi::Number>();
45
+ this->_value = value.DoubleValue();
46
+ }
47
+
48
+ Napi::FunctionReference Example::constructor;
49
+
50
+ Napi::Value Example::GetValue(const Napi::CallbackInfo &info) {
51
+ Napi::Env env = info.Env();
52
+ return Napi::Number::New(env, this->_value);
53
+ }
54
+
55
+ Napi::Value Example::SetValue(const Napi::CallbackInfo &info, const Napi::Value &value) {
56
+ Napi::Env env = info.Env();
57
+ // ...
58
+ Napi::Number arg = value.As<Napi::Number>();
59
+ this->_value = arg.DoubleValue();
60
+ return this->GetValue(info);
61
+ }
62
+
63
+ // Initialize native add-on
64
+ Napi::Object Init (Napi::Env env, Napi::Object exports) {
65
+ Example::Init(env, exports);
66
+ return exports;
67
+ }
68
+
69
+ // Register and initialize native add-on
70
+ NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
71
+ ```
72
+
73
+ The above code can be used from JavaScript as follows:
74
+
75
+ ```js
76
+ 'use strict';
77
+
78
+ const { Example } = require('bindings')('addon');
79
+
80
+ const example = new Example(11);
81
+ console.log(example.value);
82
+ // It prints 11
83
+ example.value = 19;
84
+ console.log(example.value);
85
+ // It prints 19
86
+ example.readOnlyProp = 500;
87
+ console.log(example.readOnlyProp);
88
+ // Unchanged. It prints 19
89
+ ```
90
+
9
91
  ## Methods
10
92
 
11
- ### Contructor
93
+ ### Constructor
12
94
 
13
95
  Creates new instance of `Napi::ClassPropertyDescriptor` descriptor object.
14
96
 
@@ -33,4 +115,4 @@ Returns the original N-API `napi_property_descriptor` wrapped inside the `Napi::
33
115
  operator const napi_property_descriptor&() const { return _desc; }
34
116
  ```
35
117
 
36
- Returns the original N-API `napi_property_descriptor` wrapped inside the `Napi::ClassPropertyDescriptor`
118
+ Returns the original N-API `napi_property_descriptor` wrapped inside the `Napi::ClassPropertyDescriptor`