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/doc/promises.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Promise
2
2
 
3
+ Class `Napi::Promise` inherits from class [`Napi::Object`][].
4
+
3
5
  The `Napi::Promise` class, along with its `Napi::Promise::Deferred` class, implement the ability to create, resolve, and reject Promise objects.
4
6
 
5
7
  The basic approach is to create a `Napi::Promise::Deferred` object and return to your caller the value returned by the `Napi::Promise::Deferred::Promise` method. For example:
@@ -72,3 +74,6 @@ void Napi::Promise::Deferred::Reject(napi_value value) const;
72
74
  Rejects the Promise object held by the `Napi::Promise::Deferred` object.
73
75
 
74
76
  * `[in] value`: The N-API primitive value with which to reject the `Napi::Promise`.
77
+
78
+
79
+ [`Napi::Object`]: ./object.md
package/doc/setup.md CHANGED
@@ -26,7 +26,7 @@ To use **N-API** in a native module:
26
26
  2. Reference this package's include directory and gyp file in `binding.gyp`:
27
27
 
28
28
  ```gyp
29
- 'include_dirs': ["<!@(node -p \"require('node-addon-api').include\")"],
29
+ 'include_dirs': ["<!(node -p \"require('node-addon-api').include_dir\")"],
30
30
  ```
31
31
 
32
32
  3. Decide whether the package will enable C++ exceptions in the N-API wrapper.
package/doc/string.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # String
2
2
 
3
+ Class `Napi::String` inherits from class [`Napi::Name`][].
4
+
3
5
  ## Constructor
4
6
 
5
7
  ```cpp
@@ -87,3 +89,5 @@ std::u16string Napi::String::Utf16Value() const;
87
89
  ```
88
90
 
89
91
  Returns a UTF-16 encoded C++ string.
92
+
93
+ [`Napi::Name`]: ./name.md
package/doc/symbol.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Symbol
2
2
 
3
+ Class `Napi::Symbol` inherits from class [`Napi::Name`][].
4
+
3
5
  ## Methods
4
6
 
5
7
  ### Constructor
@@ -42,3 +44,5 @@ static Napi::Symbol Napi::Symbol::WellKnown(napi_env env, const std::string& nam
42
44
 
43
45
  Returns a `Napi::Symbol` representing a well-known `Symbol` from the
44
46
  `Symbol` registry.
47
+
48
+ [`Napi::Name`]: ./name.md
@@ -1,5 +1,7 @@
1
1
  # TypedArray
2
2
 
3
+ Class `Napi::TypedArray` inherits from class [`Napi::Object`][].
4
+
3
5
  The `Napi::TypedArray` class corresponds to the
4
6
  [JavaScript `TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)
5
7
  class.
@@ -72,3 +74,5 @@ size_t Napi::TypedArray::ByteLength() const;
72
74
  ```
73
75
 
74
76
  Returns the length of the array, in bytes.
77
+
78
+ [`Napi::Object`]: ./object.md
@@ -1,5 +1,7 @@
1
1
  # TypedArrayOf
2
2
 
3
+ Class `Napi::TypedArrayOf<T>` inherits from class [`Napi::TypedArray`][].
4
+
3
5
  The `Napi::TypedArrayOf` class corresponds to the various
4
6
  [JavaScript `TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)
5
7
  classes.
@@ -131,3 +133,5 @@ const T* Napi::TypedArrayOf::Data() const
131
133
 
132
134
  Returns a pointer into the backing `Napi::ArrayBuffer` which is offset to point to the
133
135
  start of the array.
136
+
137
+ [`Napi::TypedArray`]: ./typed_array.md
package/doc/value.md CHANGED
@@ -1,27 +1,13 @@
1
1
  # Value
2
2
 
3
- `Napi::Value` is the C++ manifestation of a JavaScript value.
4
-
5
- Value is a the base class upon which other JavaScript values such as Number, Boolean, String, and Object are based.
6
-
7
- The following classes inherit, either directly or indirectly, from `Napi::Value`:
8
-
9
- - [`Napi::Array`](basic_types.md#array)
10
- - [`Napi::ArrayBuffer`](array_buffer.md)
11
- - [`Napi::Boolean`](boolean.md)
12
- - [`Napi::Buffer`](buffer.md)
13
- - [`Napi::Date`](date.md)
14
- - [`Napi::External`](external.md)
15
- - [`Napi::Function`](function.md)
16
- - [`Napi::Name`](name.md)
17
- - [`Napi::Number`](number.md)
18
- - [`Napi::Object`](object.md)
19
- - [`Napi::String`](string.md)
20
- - [`Napi::Symbol`](symbol.md)
21
- - [`Napi::TypedArray`](typed_array.md)
22
- - [`Napi::TypedArrayOf`](typed_array_of.md)
3
+ `Napi::Value` is the C++ manifestation of a JavaScript value. It is the base
4
+ class upon which other JavaScript values such as `Napi::Number`,
5
+ `Napi::Boolean`, `Napi::String`, and `Napi::Object` are based. It represents a
6
+ JavaScript value of an unknown type. It is a thin wrapper around the N-API
7
+ datatype `napi_value`. Methods on this class can be used to check the JavaScript
8
+ type of the underlying N-API `napi_value` and also to convert to C++ types.
23
9
 
24
- ## Methods
10
+ ## Constructors
25
11
 
26
12
  ### Empty Constructor
27
13
 
@@ -37,9 +23,10 @@ Creates a new *empty* `Napi::Value` instance.
37
23
  Napi::Value::Value(napi_env env, napi_value value);
38
24
  ```
39
25
 
40
- - `[in] env`: The `napi_env` environment in which to construct the `Napi::Value` object.
41
-
42
- - `[in] value`: The C++ primitive from which to instantiate the `Napi::Value`. `value` may be any of:
26
+ - `[in] env`: The `napi_env` environment in which to construct the `Napi::Value`
27
+ object.
28
+ - `[in] value`: The C++ primitive from which to instantiate the `Napi::Value`.
29
+ value` may be any of:
43
30
  - `bool`
44
31
  - Any integer type
45
32
  - Any floating point type
@@ -50,38 +37,25 @@ Napi::Value::Value(napi_env env, napi_value value);
50
37
  - `Napi::Value`
51
38
  - `napi_value`
52
39
 
53
- ### From
54
-
55
- ```cpp
56
- template <typename T> static Napi::Value Napi::Value::From(napi_env env, const T& value);
57
- ```
58
-
59
- - `[in] env`: The `napi_env` environment in which to create the `Napi::Value` object.
60
-
61
- - `[in] value`: The N-API primitive value from which to create the `Napi::Value` object.
62
-
63
- Returns a `Napi::Value` object from an N-API primitive value.
40
+ ## Operators
64
41
 
65
42
  ### operator napi_value
66
43
 
67
44
  ```cpp
68
- operator napi_value() const;
45
+ Napi::Value::operator napi_value() const;
69
46
  ```
70
47
 
71
- Returns this Value's N-API value primitive.
72
-
73
- Returns `nullptr` if this `Napi::Value` is *empty*.
48
+ Returns the underlying N-API `napi_value`. If the instance is _empty_, this
49
+ returns `nullptr`.
74
50
 
75
51
  ### operator ==
76
52
 
77
53
  ```cpp
78
-
79
54
  bool Napi::Value::operator ==(const Napi::Value& other) const;
80
55
  ```
81
56
 
82
- - `[in] other`: The `Napi::Value` object to be compared.
83
-
84
- Returns a `bool` indicating if this `Napi::Value` strictly equals another `Napi::Value`.
57
+ Returns `true` if this value strictly equals another value, or `false`
58
+ otherwise.
85
59
 
86
60
  ### operator !=
87
61
 
@@ -89,18 +63,22 @@ Returns a `bool` indicating if this `Napi::Value` strictly equals another `Napi:
89
63
  bool Napi::Value::operator !=(const Napi::Value& other) const;
90
64
  ```
91
65
 
92
- - `[in] other`: The `Napi::Value` object to be compared.
66
+ Returns `false` if this value strictly equals another value, or `true`
67
+ otherwise.
93
68
 
94
- Returns a `bool` indicating if this `Napi::Value` does not strictly equal another `Napi::Value`.
69
+ ## Methods
95
70
 
96
- ### StrictEquals
71
+ ### As
97
72
 
98
73
  ```cpp
99
- bool Napi::Value::StrictEquals(const Napi::Value& other) const;
74
+ template <typename T> T Napi::Value::As() const;
100
75
  ```
101
- - `[in] other`: The `Napi::Value` object to be compared.
102
76
 
103
- Returns a `bool` indicating if this `Napi::Value` strictly equals another `Napi::Value`.
77
+ Casts to another type of `Napi::Value`, when the actual type is known or
78
+ assumed.
79
+
80
+ This conversion does not coerce the type. Calling any methods inappropriate for
81
+ the actual value type will throw `Napi::Error`.
104
82
 
105
83
  ### Env
106
84
 
@@ -108,100 +86,141 @@ Returns a `bool` indicating if this `Napi::Value` strictly equals another `Napi:
108
86
  Napi::Env Napi::Value::Env() const;
109
87
  ```
110
88
 
111
- Returns the `Napi::Env` environment this value is associated with.
89
+ Returns the `Napi::Env` environment this value is associated with. See
90
+ [`Napi::Env`](env.md) for more details about environments.
112
91
 
113
- ### IsEmpty
92
+ ### From
114
93
 
115
94
  ```cpp
116
- bool Napi::Value::IsEmpty() const;
95
+ template <typename T>
96
+ static Napi::Value Napi::Value::From(napi_env env, const T& value);
117
97
  ```
118
98
 
119
- Returns a `bool` indicating if this `Napi::Value` is *empty* (uninitialized).
99
+ - `[in] env`: The `napi_env` environment in which to create the `Napi::Value`
100
+ object.
101
+ - `[in] value`: The N-API primitive value from which to create the `Napi::Value`
102
+ object.
120
103
 
121
- An empty `Napi::Value` is invalid, and most attempts to perform an operation on an empty Value will result in an exception.
122
- Note an empty `Napi::Value` is distinct from JavaScript `null` or `undefined`, which are valid values.
104
+ Returns a `Napi::Value` object from an N-API primitive value.
123
105
 
124
- When C++ exceptions are disabled at compile time, a method with a `Napi::Value` return type may return an empty Value to indicate a pending exception. So when not using C++ exceptions, callers should check whether this `Napi::Value` is empty before attempting to use it.
106
+ This method is used to convert from a C++ type to a JavaScript value.
107
+ Here, `value` may be any of:
108
+ - `bool` - returns a `Napi::Boolean`.
109
+ - Any integer type - returns a `Napi::Number`.
110
+ - Any floating point type - returns a `Napi::Number`.
111
+ - `const char*` (encoded using UTF-8, null-terminated) - returns a
112
+ `Napi::String`.
113
+ - `const char16_t*` (encoded using UTF-16-LE, null-terminated) - returns a
114
+ `Napi::String`.
115
+ - `std::string` (encoded using UTF-8) - returns a `Napi::String`.
116
+ - `std::u16string` - returns a `Napi::String`.
117
+ - `Napi::Value` - returns a `Napi::Value`.
118
+ - `Napi_value` - returns a `Napi::Value`.
125
119
 
126
- ### Type
120
+ ### IsArray
127
121
 
128
122
  ```cpp
129
- napi_valuetype Napi::Value::Type() const;
123
+ bool Napi::Value::IsArray() const;
130
124
  ```
131
125
 
132
- Returns the `napi_valuetype` type of the `Napi::Value`.
126
+ Returns `true` if the underlying value is a JavaScript `Napi::Array` or `false`
127
+ otherwise.
133
128
 
134
- ### IsUndefined
129
+ ### IsArrayBuffer
135
130
 
136
131
  ```cpp
137
- bool Napi::Value::IsUndefined() const;
132
+ bool Napi::Value::IsArrayBuffer() const;
138
133
  ```
139
134
 
140
- Returns a `bool` indicating if this `Napi::Value` is an undefined JavaScript value.
135
+ Returns `true` if the underlying value is a JavaScript `Napi::ArrayBuffer` or
136
+ `false` otherwise.
141
137
 
142
- ### IsNull
138
+ ### IsBoolean
143
139
 
144
140
  ```cpp
145
- bool Napi::Value::IsNull() const;
141
+ bool Napi::Value::IsBoolean() const;
146
142
  ```
147
143
 
148
- Returns a `bool` indicating if this `Napi::Value` is a null JavaScript value.
144
+ Returns `true` if the underlying value is a JavaScript `true` or JavaScript
145
+ `false`, or `false` if the value is not a `Napi::Boolean` value in JavaScript.
149
146
 
150
- ### IsBoolean
147
+ ### IsBuffer
151
148
 
152
149
  ```cpp
153
- bool Napi::Value::IsBoolean() const;
150
+ bool Napi::Value::IsBuffer() const;
154
151
  ```
155
152
 
156
- Returns a `bool` indicating if this `Napi::Value` is a JavaScript boolean.
153
+ Returns `true` if the underlying value is a Node.js `Napi::Buffer` or `false`
154
+ otherwise.
157
155
 
158
- ### IsNumber
156
+ ### IsDataView
157
+ ```cpp
158
+ bool Napi::Value::IsDataView() const;
159
+ ```
160
+
161
+ Returns `true` if the underlying value is a JavaScript `Napi::DataView` or
162
+ `false` otherwise.
163
+
164
+ ### IsDate
159
165
 
160
166
  ```cpp
161
- bool Napi::Value::IsNumber() const;
167
+ bool Napi::Value::IsDate() const;
162
168
  ```
163
169
 
164
- Returns a `bool` indicating if this `Napi::Value` is a JavaScript number.
170
+ Returns `true` if the underlying value is a JavaScript `Date` or `false`
171
+ otherwise.
165
172
 
166
- ### IsString
173
+ ### IsEmpty
167
174
 
168
175
  ```cpp
169
- bool Napi::Value::IsString() const;
176
+ bool Napi::Value::IsEmpty() const;
170
177
  ```
171
178
 
172
- Returns a `bool` indicating if this `Napi::Value` is a JavaScript string.
179
+ Returns `true` if the value is uninitialized.
173
180
 
174
- ### IsSymbol
181
+ An empty `Napi::Value` is invalid, and most attempts to perform an operation on
182
+ an empty `Napi::Value` will result in an exception. An empty `Napi::Value` is
183
+ distinct from JavaScript `null` or `undefined`, which are valid values.
184
+
185
+ When C++ exceptions are disabled at compile time, a method with a `Napi::Value`
186
+ return type may return an empty `Napi::Value` to indicate a pending exception.
187
+ Thus, when C++ exceptions are not being used, callers should check the result of
188
+ `Env::IsExceptionPending` before attempting to use the value.
175
189
 
190
+ ### IsExternal
176
191
  ```cpp
177
- bool Napi::Value::IsSymbol() const;
192
+ bool Napi::Value::IsExternal() const;
178
193
  ```
179
194
 
180
- Returns a `bool` indicating if this `Napi::Value` is a JavaScript symbol.
195
+ Returns `true` if the underlying value is a N-API external object or `false`
196
+ otherwise.
181
197
 
182
- ### IsArray
198
+ ### IsFunction
183
199
 
184
200
  ```cpp
185
- bool Napi::Value::IsArray() const;
201
+ bool Napi::Value::IsFunction() const;
186
202
  ```
187
203
 
188
- Returns a `bool` indicating if this `Napi::Value` is a JavaScript array.
204
+ Returns `true` if the underlying value is a JavaScript `Napi::Function` or
205
+ `false` otherwise.
189
206
 
190
- ### IsArrayBuffer
207
+ ### IsNull
191
208
 
192
209
  ```cpp
193
- bool Napi::Value::IsArrayBuffer() const;
210
+ bool Napi::Value::IsNull() const;
194
211
  ```
195
212
 
196
- Returns a `bool` indicating if this `Napi::Value` is a JavaScript array buffer.
213
+ Returns `true` if the underlying value is a JavaScript `null` or `false`
214
+ otherwise.
197
215
 
198
- ### IsTypedArray
216
+ ### IsNumber
199
217
 
200
218
  ```cpp
201
- bool Napi::Value::IsTypedArray() const;
219
+ bool Napi::Value::IsNumber() const;
202
220
  ```
203
221
 
204
- Returns a `bool` indicating if this `Napi::Value` is a JavaScript typed array.
222
+ Returns `true` if the underlying value is a JavaScript `Napi::Number` or `false`
223
+ otherwise.
205
224
 
206
225
  ### IsObject
207
226
 
@@ -209,41 +228,63 @@ Returns a `bool` indicating if this `Napi::Value` is a JavaScript typed array.
209
228
  bool Napi::Value::IsObject() const;
210
229
  ```
211
230
 
212
- Returns a `bool` indicating if this `Napi::Value` is JavaScript object.
231
+ Returns `true` if the underlying value is a JavaScript `Napi::Object` or `false`
232
+ otherwise.
213
233
 
214
- ### IsFunction
234
+ ### IsPromise
215
235
 
216
236
  ```cpp
217
- bool Napi::Value::IsFunction() const;
237
+ bool Napi::Value::IsPromise() const;
218
238
  ```
219
239
 
220
- Returns a `bool` indicating if this `Napi::Value` is a JavaScript function.
240
+ Returns `true` if the underlying value is a JavaScript `Napi::Promise` or
241
+ `false` otherwise.
221
242
 
222
- ### IsBuffer
243
+ ### IsString
223
244
 
224
245
  ```cpp
225
- bool Napi::Value::IsBuffer() const;
246
+ bool Napi::Value::IsString() const;
226
247
  ```
227
248
 
228
- Returns a `bool` indicating if this `Napi::Value` is a Node buffer.
249
+ Returns `true` if the underlying value is a JavaScript `Napi::String` or `false`
250
+ otherwise.
229
251
 
230
- ### IsDate
252
+ ### IsSymbol
231
253
 
232
254
  ```cpp
233
- bool Napi::Value::IsDate() const;
255
+ bool Napi::Value::IsSymbol() const;
234
256
  ```
235
257
 
236
- Returns a `bool` indicating if this `Napi::Value` is a JavaScript date.
258
+ Returns `true` if the underlying value is a JavaScript `Napi::Symbol` or `false`
259
+ otherwise.
237
260
 
238
- ### As
261
+ ### IsTypedArray
239
262
 
240
263
  ```cpp
241
- template <typename T> T Napi::Value::As() const;
264
+ bool Napi::Value::IsTypedArray() const;
242
265
  ```
243
266
 
244
- Casts to another type of `Napi::Value`, when the actual type is known or assumed.
267
+ Returns `true` if the underlying value is a JavaScript `Napi::TypedArray` or
268
+ `false` otherwise.
245
269
 
246
- This conversion does not coerce the type. Calling any methods inappropriate for the actual value type will throw `Napi::Error`.
270
+ ### IsUndefined
271
+
272
+ ```cpp
273
+ bool Napi::Value::IsUndefined() const;
274
+ ```
275
+
276
+ Returns `true` if the underlying value is a JavaScript `undefined` or `false`
277
+ otherwise.
278
+
279
+ ### StrictEquals
280
+
281
+ ```cpp
282
+ bool Napi::Value::StrictEquals(const Napi::Value& other) const;
283
+ ```
284
+ - `[in] other`: The `Napi::Value` object to be compared.
285
+
286
+ Returns a `bool` indicating if this `Napi::Value` strictly equals another
287
+ `Napi::Value`.
247
288
 
248
289
  ### ToBoolean
249
290
 
@@ -251,7 +292,12 @@ This conversion does not coerce the type. Calling any methods inappropriate for
251
292
  Napi::Boolean Napi::Value::ToBoolean() const;
252
293
  ```
253
294
 
254
- Returns the `Napi::Value` coerced to a JavaScript boolean.
295
+ Returns a `Napi::Boolean` representing the `Napi::Value`.
296
+
297
+ This is a wrapper around `napi_coerce_to_boolean`. This will throw a JavaScript
298
+ exception if the coercion fails. If C++ exceptions are not being used, callers
299
+ should check the result of `Env::IsExceptionPending` before attempting to use
300
+ the returned value.
255
301
 
256
302
  ### ToNumber
257
303
 
@@ -261,6 +307,14 @@ Napi::Number Napi::Value::ToNumber() const;
261
307
 
262
308
  Returns the `Napi::Value` coerced to a JavaScript number.
263
309
 
310
+ ### ToObject
311
+
312
+ ```cpp
313
+ Napi::Object Napi::Value::ToObject() const;
314
+ ```
315
+
316
+ Returns the `Napi::Value` coerced to a JavaScript object.
317
+
264
318
  ### ToString
265
319
 
266
320
  ```cpp
@@ -269,10 +323,18 @@ Napi::String Napi::Value::ToString() const;
269
323
 
270
324
  Returns the `Napi::Value` coerced to a JavaScript string.
271
325
 
272
- ### ToObject
326
+ ### Type
273
327
 
274
328
  ```cpp
275
- Napi::Object Napi::Value::ToObject() const;
329
+ napi_valuetype Napi::Value::Type() const;
276
330
  ```
277
331
 
278
- Returns the `Napi::Value` coerced to a JavaScript object.
332
+ Returns the `napi_valuetype` type of the `Napi::Value`.
333
+
334
+ [`Napi::Boolean`]: ./boolean.md
335
+ [`Napi::BigInt`]: ./bigint.md
336
+ [`Napi::Date`]: ./date.md
337
+ [`Napi::External`]: ./external.md
338
+ [`Napi::Name`]: ./name.md
339
+ [`Napi::Number`]: ./number.md
340
+ [`Napi::Object`]: ./object.md
package/index.js CHANGED
@@ -1,10 +1,11 @@
1
1
  const path = require('path');
2
2
 
3
- const include = path.relative('.', __dirname);
3
+ const include_dir = path.relative('.', __dirname);
4
4
 
5
5
  module.exports = {
6
- include: include,
7
- gyp: path.join(include, 'node_api.gyp:nothing'),
6
+ include: `"${__dirname}"`, // deprecated, can be removed as part of 4.0.0
7
+ include_dir,
8
+ gyp: path.join(include_dir, 'node_api.gyp:nothing'),
8
9
  isNodeApiBuiltin: true,
9
10
  needsFlag: false
10
11
  };