node-addon-api 3.0.1 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +45 -0
- package/README.md +28 -23
- package/appveyor.yml +3 -14
- package/benchmark/function_args.cc +64 -0
- package/benchmark/function_args.js +15 -7
- package/benchmark/property_descriptor.cc +31 -0
- package/benchmark/property_descriptor.js +12 -4
- package/common.gypi +1 -1
- package/doc/addon.md +157 -0
- package/doc/array.md +81 -0
- package/doc/array_buffer.md +4 -0
- package/doc/bigint.md +4 -0
- package/doc/boolean.md +4 -0
- package/doc/buffer.md +4 -0
- package/doc/dataview.md +4 -0
- package/doc/date.md +2 -2
- package/doc/error.md +5 -0
- package/doc/external.md +4 -0
- package/doc/function.md +1 -0
- package/doc/hierarchy.md +91 -0
- package/doc/instance_wrap.md +408 -0
- package/doc/name.md +29 -0
- package/doc/object.md +6 -2
- package/doc/object_wrap.md +120 -410
- package/doc/promises.md +5 -0
- package/doc/setup.md +1 -1
- package/doc/string.md +4 -0
- package/doc/symbol.md +4 -0
- package/doc/typed_array.md +4 -0
- package/doc/typed_array_of.md +4 -0
- package/doc/value.md +166 -104
- package/index.js +4 -3
- package/napi-inl.h +539 -461
- package/napi.h +134 -119
- package/package.json +15 -1
- package/tools/conversion.js +4 -4
- package/doc/basic_types.md +0 -423
- package/doc/working_with_javascript_values.md +0 -14
package/doc/basic_types.md
DELETED
|
@@ -1,423 +0,0 @@
|
|
|
1
|
-
# Basic Types
|
|
2
|
-
|
|
3
|
-
Node Addon API consists of a few fundamental data types. These allow a user of
|
|
4
|
-
the API to create, convert and introspect fundamental JavaScript types, and
|
|
5
|
-
interoperate with their C++ counterparts.
|
|
6
|
-
|
|
7
|
-
## Value
|
|
8
|
-
|
|
9
|
-
`Napi::Value` is the base class of Node Addon API's fundamental object type hierarchy.
|
|
10
|
-
It represents a JavaScript value of an unknown type. It is a thin wrapper around
|
|
11
|
-
the N-API datatype `napi_value`. Methods on this class can be used to check
|
|
12
|
-
the JavaScript type of the underlying N-API `napi_value` and also to convert to
|
|
13
|
-
C++ types.
|
|
14
|
-
|
|
15
|
-
### Constructor
|
|
16
|
-
|
|
17
|
-
```cpp
|
|
18
|
-
Napi::Value::Value();
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
Used to create a Node Addon API `Napi::Value` that represents an **empty** value.
|
|
22
|
-
|
|
23
|
-
```cpp
|
|
24
|
-
Napi::Value::Value(napi_env env, napi_value value);
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
- `[in] env` - The `napi_env` environment in which to construct the `Napi::Value`
|
|
28
|
-
object.
|
|
29
|
-
- `[in] value` - The underlying JavaScript value that the `Napi::Value` instance
|
|
30
|
-
represents.
|
|
31
|
-
|
|
32
|
-
Returns a Node.js Addon API `Napi::Value` that represents the `napi_value` passed
|
|
33
|
-
in.
|
|
34
|
-
|
|
35
|
-
### Operators
|
|
36
|
-
|
|
37
|
-
#### operator napi_value
|
|
38
|
-
|
|
39
|
-
```cpp
|
|
40
|
-
Napi::Value::operator napi_value() const;
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
Returns the underlying N-API `napi_value`. If the instance is _empty_, this
|
|
44
|
-
returns `nullptr`.
|
|
45
|
-
|
|
46
|
-
#### operator ==
|
|
47
|
-
|
|
48
|
-
```cpp
|
|
49
|
-
bool Napi::Value::operator ==(const Value& other) const;
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
Returns `true` if this value strictly equals another value, or `false` otherwise.
|
|
53
|
-
|
|
54
|
-
#### operator !=
|
|
55
|
-
|
|
56
|
-
```cpp
|
|
57
|
-
bool Napi::Value::operator !=(const Value& other) const;
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
Returns `false` if this value strictly equals another value, or `true` otherwise.
|
|
61
|
-
|
|
62
|
-
### Methods
|
|
63
|
-
|
|
64
|
-
#### From
|
|
65
|
-
```cpp
|
|
66
|
-
template <typename T>
|
|
67
|
-
static Napi::Value Napi::Value::From(napi_env env, const T& value);
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
- `[in] env` - The `napi_env` environment in which to construct the `Napi::Value` object.
|
|
71
|
-
- `[in] value` - The C++ type to represent in JavaScript.
|
|
72
|
-
|
|
73
|
-
Returns a `Napi::Value` representing the input C++ type in JavaScript.
|
|
74
|
-
|
|
75
|
-
This method is used to convert from a C++ type to a JavaScript value.
|
|
76
|
-
Here, `value` may be any of:
|
|
77
|
-
- `bool` - returns a `Napi::Boolean`.
|
|
78
|
-
- Any integer type - returns a `Napi::Number`.
|
|
79
|
-
- Any floating point type - returns a `Napi::Number`.
|
|
80
|
-
- `const char*` (encoded using UTF-8, null-terminated) - returns a `Napi::String`.
|
|
81
|
-
- `const char16_t*` (encoded using UTF-16-LE, null-terminated) - returns a `Napi::String`.
|
|
82
|
-
- `std::string` (encoded using UTF-8) - returns a `Napi::String`.
|
|
83
|
-
- `std::u16string` - returns a `Napi::String`.
|
|
84
|
-
- `napi::Value` - returns a `Napi::Value`.
|
|
85
|
-
- `napi_value` - returns a `Napi::Value`.
|
|
86
|
-
|
|
87
|
-
#### As
|
|
88
|
-
```cpp
|
|
89
|
-
template <typename T> T Napi::Value::As() const;
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
Returns the `Napi::Value` cast to a desired C++ type.
|
|
93
|
-
|
|
94
|
-
Use this when the actual type is known or assumed.
|
|
95
|
-
|
|
96
|
-
Note:
|
|
97
|
-
This conversion does NOT coerce the type. Calling any methods inappropriate for
|
|
98
|
-
the actual value type will throw `Napi::Error`.
|
|
99
|
-
|
|
100
|
-
#### StrictEquals
|
|
101
|
-
```cpp
|
|
102
|
-
bool Napi::Value::StrictEquals(const Value& other) const;
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
- `[in] other` - The value to compare against.
|
|
106
|
-
|
|
107
|
-
Returns true if the other `Napi::Value` is strictly equal to this one.
|
|
108
|
-
|
|
109
|
-
#### Env
|
|
110
|
-
```cpp
|
|
111
|
-
Napi::Env Napi::Value::Env() const;
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
Returns the environment that the value is associated with. See
|
|
115
|
-
[`Napi::Env`](env.md) for more details about environments.
|
|
116
|
-
|
|
117
|
-
#### IsEmpty
|
|
118
|
-
```cpp
|
|
119
|
-
bool Napi::Value::IsEmpty() const;
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
Returns `true` if the value is uninitialized.
|
|
123
|
-
|
|
124
|
-
An empty value is invalid, and most attempts to perform an operation on an
|
|
125
|
-
empty value will result in an exception. An empty value is distinct from
|
|
126
|
-
JavaScript `null` or `undefined`, which are valid values.
|
|
127
|
-
|
|
128
|
-
When C++ exceptions are disabled at compile time, a method with a `Napi::Value`
|
|
129
|
-
return type may return an empty value to indicate a pending exception. If C++
|
|
130
|
-
exceptions are not being used, callers should check the result of
|
|
131
|
-
`Env::IsExceptionPending` before attempting to use the value.
|
|
132
|
-
|
|
133
|
-
#### Type
|
|
134
|
-
```cpp
|
|
135
|
-
napi_valuetype Napi::Value::Type() const;
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
Returns the underlying N-API `napi_valuetype` of the value.
|
|
139
|
-
|
|
140
|
-
#### IsUndefined
|
|
141
|
-
```cpp
|
|
142
|
-
bool Napi::Value::IsUndefined() const;
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
Returns `true` if the underlying value is a JavaScript `undefined` or `false`
|
|
146
|
-
otherwise.
|
|
147
|
-
|
|
148
|
-
#### IsNull
|
|
149
|
-
```cpp
|
|
150
|
-
bool Napi::Value::IsNull() const;
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
Returns `true` if the underlying value is a JavaScript `null` or `false`
|
|
154
|
-
otherwise.
|
|
155
|
-
|
|
156
|
-
#### IsBoolean
|
|
157
|
-
```cpp
|
|
158
|
-
bool Napi::Value::IsBoolean() const;
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
Returns `true` if the underlying value is a JavaScript `true` or JavaScript
|
|
162
|
-
`false`, or `false` if the value is not a `Napi::Boolean` value in JavaScript.
|
|
163
|
-
|
|
164
|
-
#### IsNumber
|
|
165
|
-
```cpp
|
|
166
|
-
bool Napi::Value::IsNumber() const;
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
Returns `true` if the underlying value is a JavaScript `Napi::Number` or `false`
|
|
170
|
-
otherwise.
|
|
171
|
-
|
|
172
|
-
#### IsString
|
|
173
|
-
```cpp
|
|
174
|
-
bool Napi::Value::IsString() const;
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
Returns `true` if the underlying value is a JavaScript `Napi::String` or `false`
|
|
178
|
-
otherwise.
|
|
179
|
-
|
|
180
|
-
#### IsSymbol
|
|
181
|
-
```cpp
|
|
182
|
-
bool Napi::Value::IsSymbol() const;
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
Returns `true` if the underlying value is a JavaScript `Napi::Symbol` or `false`
|
|
186
|
-
otherwise.
|
|
187
|
-
|
|
188
|
-
#### IsArray
|
|
189
|
-
```cpp
|
|
190
|
-
bool Napi::Value::IsArray() const;
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
Returns `true` if the underlying value is a JavaScript `Napi::Array` or `false`
|
|
194
|
-
otherwise.
|
|
195
|
-
|
|
196
|
-
#### IsArrayBuffer
|
|
197
|
-
```cpp
|
|
198
|
-
bool Napi::Value::IsArrayBuffer() const;
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
Returns `true` if the underlying value is a JavaScript `Napi::ArrayBuffer` or `false`
|
|
202
|
-
otherwise.
|
|
203
|
-
|
|
204
|
-
#### IsTypedArray
|
|
205
|
-
```cpp
|
|
206
|
-
bool Napi::Value::IsTypedArray() const;
|
|
207
|
-
```
|
|
208
|
-
|
|
209
|
-
Returns `true` if the underlying value is a JavaScript `Napi::TypedArray` or `false`
|
|
210
|
-
otherwise.
|
|
211
|
-
|
|
212
|
-
#### IsObject
|
|
213
|
-
```cpp
|
|
214
|
-
bool Napi::Value::IsObject() const;
|
|
215
|
-
```
|
|
216
|
-
|
|
217
|
-
Returns `true` if the underlying value is a JavaScript `Napi::Object` or `false`
|
|
218
|
-
otherwise.
|
|
219
|
-
|
|
220
|
-
#### IsFunction
|
|
221
|
-
```cpp
|
|
222
|
-
bool Napi::Value::IsFunction() const;
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
Returns `true` if the underlying value is a JavaScript `Napi::Function` or `false`
|
|
226
|
-
otherwise.
|
|
227
|
-
|
|
228
|
-
#### IsPromise
|
|
229
|
-
```cpp
|
|
230
|
-
bool Napi::Value::IsPromise() const;
|
|
231
|
-
```
|
|
232
|
-
|
|
233
|
-
Returns `true` if the underlying value is a JavaScript `Napi::Promise` or `false`
|
|
234
|
-
otherwise.
|
|
235
|
-
|
|
236
|
-
#### IsDataView
|
|
237
|
-
```cpp
|
|
238
|
-
bool Napi::Value::IsDataView() const;
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
Returns `true` if the underlying value is a JavaScript `Napi::DataView` or `false`
|
|
242
|
-
otherwise.
|
|
243
|
-
|
|
244
|
-
#### IsBuffer
|
|
245
|
-
```cpp
|
|
246
|
-
bool Napi::Value::IsBuffer() const;
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
Returns `true` if the underlying value is a Node.js `Napi::Buffer` or `false`
|
|
250
|
-
otherwise.
|
|
251
|
-
|
|
252
|
-
#### IsExternal
|
|
253
|
-
```cpp
|
|
254
|
-
bool Napi::Value::IsExternal() const;
|
|
255
|
-
```
|
|
256
|
-
|
|
257
|
-
Returns `true` if the underlying value is a N-API external object or `false`
|
|
258
|
-
otherwise.
|
|
259
|
-
|
|
260
|
-
#### IsDate
|
|
261
|
-
```cpp
|
|
262
|
-
bool Napi::Value::IsDate() const;
|
|
263
|
-
```
|
|
264
|
-
|
|
265
|
-
Returns `true` if the underlying value is a JavaScript `Date` or `false`
|
|
266
|
-
otherwise.
|
|
267
|
-
|
|
268
|
-
#### ToBoolean
|
|
269
|
-
```cpp
|
|
270
|
-
Napi::Boolean Napi::Value::ToBoolean() const;
|
|
271
|
-
```
|
|
272
|
-
|
|
273
|
-
Returns a `Napi::Boolean` representing the `Napi::Value`.
|
|
274
|
-
|
|
275
|
-
This is a wrapper around `napi_coerce_to_boolean`. This will throw a JavaScript
|
|
276
|
-
exception if the coercion fails. If C++ exceptions are not being used, callers
|
|
277
|
-
should check the result of `Env::IsExceptionPending` before attempting to use
|
|
278
|
-
the returned value.
|
|
279
|
-
|
|
280
|
-
#### ToNumber
|
|
281
|
-
```cpp
|
|
282
|
-
Napi::Number Napi::Value::ToNumber() const;
|
|
283
|
-
```
|
|
284
|
-
|
|
285
|
-
Returns a `Napi::Number` representing the `Napi::Value`.
|
|
286
|
-
|
|
287
|
-
Note:
|
|
288
|
-
This can cause script code to be executed according to JavaScript semantics.
|
|
289
|
-
This is a wrapper around `napi_coerce_to_number`. This will throw a JavaScript
|
|
290
|
-
exception if the coercion fails. If C++ exceptions are not being used, callers
|
|
291
|
-
should check the result of `Env::IsExceptionPending` before attempting to use
|
|
292
|
-
the returned value.
|
|
293
|
-
|
|
294
|
-
#### ToString
|
|
295
|
-
```cpp
|
|
296
|
-
Napi::String Napi::Value::ToString() const;
|
|
297
|
-
```
|
|
298
|
-
|
|
299
|
-
Returns a `Napi::String` representing the `Napi::Value`.
|
|
300
|
-
|
|
301
|
-
Note that this can cause script code to be executed according to JavaScript
|
|
302
|
-
semantics. This is a wrapper around `napi_coerce_to_string`. This will throw a
|
|
303
|
-
JavaScript exception if the coercion fails. If C++ exceptions are not being
|
|
304
|
-
used, callers should check the result of `Env::IsExceptionPending` before
|
|
305
|
-
attempting to use the returned value.
|
|
306
|
-
|
|
307
|
-
#### ToObject
|
|
308
|
-
```cpp
|
|
309
|
-
Napi::Object Napi::Value::ToObject() const;
|
|
310
|
-
```
|
|
311
|
-
|
|
312
|
-
Returns a `Napi::Object` representing the `Napi::Value`.
|
|
313
|
-
|
|
314
|
-
This is a wrapper around `napi_coerce_to_object`. This will throw a JavaScript
|
|
315
|
-
exception if the coercion fails. If C++ exceptions are not being used, callers
|
|
316
|
-
should check the result of `Env::IsExceptionPending` before attempting to use
|
|
317
|
-
the returned value.
|
|
318
|
-
|
|
319
|
-
## Name
|
|
320
|
-
|
|
321
|
-
Names are JavaScript values that can be used as a property name. There are two
|
|
322
|
-
specialized types of names supported in Node.js Addon API [`Napi::String`](string.md)
|
|
323
|
-
and [`Napi::Symbol`](symbol.md).
|
|
324
|
-
|
|
325
|
-
### Methods
|
|
326
|
-
|
|
327
|
-
#### Constructor
|
|
328
|
-
```cpp
|
|
329
|
-
Napi::Name::Name();
|
|
330
|
-
```
|
|
331
|
-
|
|
332
|
-
Returns an empty `Napi::Name`.
|
|
333
|
-
|
|
334
|
-
```cpp
|
|
335
|
-
Napi::Name::Name(napi_env env, napi_value value);
|
|
336
|
-
```
|
|
337
|
-
- `[in] env` - The environment in which to create the array.
|
|
338
|
-
- `[in] value` - The primitive to wrap.
|
|
339
|
-
|
|
340
|
-
Returns a `Napi::Name` created from the JavaScript primitive.
|
|
341
|
-
|
|
342
|
-
Note:
|
|
343
|
-
The value is not coerced to a string.
|
|
344
|
-
|
|
345
|
-
## Array
|
|
346
|
-
|
|
347
|
-
Arrays are native representations of JavaScript Arrays. `Napi::Array` is a wrapper
|
|
348
|
-
around `napi_value` representing a JavaScript Array.
|
|
349
|
-
|
|
350
|
-
[`Napi::TypedArray`][] and [`Napi::ArrayBuffer`][] correspond to JavaScript data
|
|
351
|
-
types such as [`Int32Array`][] and [`ArrayBuffer`][], respectively, that can be
|
|
352
|
-
used for transferring large amounts of data from JavaScript to the native side.
|
|
353
|
-
An example illustrating the use of a JavaScript-provided `ArrayBuffer` in native
|
|
354
|
-
code is available [here](https://github.com/nodejs/node-addon-examples/tree/master/array_buffer_to_native/node-addon-api).
|
|
355
|
-
|
|
356
|
-
### Constructor
|
|
357
|
-
```cpp
|
|
358
|
-
Napi::Array::Array();
|
|
359
|
-
```
|
|
360
|
-
|
|
361
|
-
Returns an empty array.
|
|
362
|
-
|
|
363
|
-
If an error occurs, a `Napi::Error` will be thrown. If C++ exceptions are not
|
|
364
|
-
being used, callers should check the result of `Env::IsExceptionPending` before
|
|
365
|
-
attempting to use the returned value.
|
|
366
|
-
|
|
367
|
-
```cpp
|
|
368
|
-
Napi::Array::Array(napi_env env, napi_value value);
|
|
369
|
-
```
|
|
370
|
-
- `[in] env` - The environment in which to create the array.
|
|
371
|
-
- `[in] value` - The primitive to wrap.
|
|
372
|
-
|
|
373
|
-
Returns a `Napi::Array` wrapping a `napi_value`.
|
|
374
|
-
|
|
375
|
-
If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not
|
|
376
|
-
being used, callers should check the result of `Env::IsExceptionPending` before
|
|
377
|
-
attempting to use the returned value.
|
|
378
|
-
|
|
379
|
-
### Methods
|
|
380
|
-
|
|
381
|
-
#### New
|
|
382
|
-
```cpp
|
|
383
|
-
static Napi::Array Napi::Array::New(napi_env env);
|
|
384
|
-
```
|
|
385
|
-
- `[in] env` - The environment in which to create the array.
|
|
386
|
-
|
|
387
|
-
Returns a new `Napi::Array`.
|
|
388
|
-
|
|
389
|
-
If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not
|
|
390
|
-
being used, callers should check the result of `Env::IsExceptionPending` before
|
|
391
|
-
attempting to use the returned value.
|
|
392
|
-
|
|
393
|
-
#### New
|
|
394
|
-
|
|
395
|
-
```cpp
|
|
396
|
-
static Napi::Array Napi::Array::New(napi_env env, size_t length);
|
|
397
|
-
```
|
|
398
|
-
- `[in] env` - The environment in which to create the array.
|
|
399
|
-
- `[in] length` - The length of the array.
|
|
400
|
-
|
|
401
|
-
Returns a new `Napi::Array` with the given length.
|
|
402
|
-
|
|
403
|
-
If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not
|
|
404
|
-
being used, callers should check the result of `Env::IsExceptionPending` before
|
|
405
|
-
attempting to use the returned value.
|
|
406
|
-
|
|
407
|
-
#### Length
|
|
408
|
-
```cpp
|
|
409
|
-
uint32_t Napi::Array::Length() const;
|
|
410
|
-
```
|
|
411
|
-
|
|
412
|
-
Returns the length of the array.
|
|
413
|
-
|
|
414
|
-
Note:
|
|
415
|
-
This can execute JavaScript code implicitly according to JavaScript semantics.
|
|
416
|
-
If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not
|
|
417
|
-
being used, callers should check the result of `Env::IsExceptionPending` before
|
|
418
|
-
attempting to use the returned value.
|
|
419
|
-
|
|
420
|
-
[`Napi::TypedArray`]: ./typed_array.md
|
|
421
|
-
[`Napi::ArrayBuffer`]: ./array_buffer.md
|
|
422
|
-
[`Int32Array`]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Int32Array
|
|
423
|
-
[`ArrayBuffer`]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
# Working with JavaScript Values
|
|
2
|
-
|
|
3
|
-
`node-addon-api` provides a set of classes that allow to create and manage
|
|
4
|
-
JavaScript object:
|
|
5
|
-
|
|
6
|
-
- [Function](function.md)
|
|
7
|
-
- [FunctionReference](function_reference.md)
|
|
8
|
-
- [ObjectWrap](object_wrap.md)
|
|
9
|
-
- [ClassPropertyDescriptor](class_property_descriptor.md)
|
|
10
|
-
- [Buffer](buffer.md)
|
|
11
|
-
- [ArrayBuffer](array_buffer.md)
|
|
12
|
-
- [TypedArray](typed_array.md)
|
|
13
|
-
- [TypedArrayOf](typed_array_of.md)
|
|
14
|
-
- [DataView](dataview.md)
|