node-addon-api 3.1.0 → 3.2.0
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 +84 -0
- package/README.md +67 -47
- package/napi-inl.deprecated.h +8 -8
- package/napi-inl.h +118 -80
- package/napi.h +513 -424
- package/package.json +25 -5
- package/tools/clang-format.js +30 -10
- package/.clang-format +0 -111
- package/.editorconfig +0 -8
- package/.github/workflows/ci.yml +0 -55
- package/.github/workflows/linter.yml +0 -24
- package/.github/workflows/stale.yml +0 -18
- package/.travis.yml +0 -58
- package/CODE_OF_CONDUCT.md +0 -4
- package/CONTRIBUTING.md +0 -66
- package/appveyor.yml +0 -37
- package/benchmark/README.md +0 -47
- package/benchmark/binding.gyp +0 -25
- package/benchmark/function_args.cc +0 -217
- package/benchmark/function_args.js +0 -60
- package/benchmark/index.js +0 -34
- package/benchmark/property_descriptor.cc +0 -91
- package/benchmark/property_descriptor.js +0 -37
- package/doc/addon.md +0 -157
- package/doc/array.md +0 -81
- package/doc/array_buffer.md +0 -149
- package/doc/async_context.md +0 -86
- package/doc/async_operations.md +0 -31
- package/doc/async_worker.md +0 -427
- package/doc/async_worker_variants.md +0 -557
- package/doc/bigint.md +0 -97
- package/doc/boolean.md +0 -68
- package/doc/buffer.md +0 -144
- package/doc/callback_scope.md +0 -54
- package/doc/callbackinfo.md +0 -97
- package/doc/checker-tool.md +0 -32
- package/doc/class_property_descriptor.md +0 -117
- package/doc/cmake-js.md +0 -68
- package/doc/conversion-tool.md +0 -28
- package/doc/creating_a_release.md +0 -62
- package/doc/dataview.md +0 -248
- package/doc/date.md +0 -68
- package/doc/env.md +0 -132
- package/doc/error.md +0 -120
- package/doc/error_handling.md +0 -186
- package/doc/escapable_handle_scope.md +0 -82
- package/doc/external.md +0 -63
- package/doc/function.md +0 -402
- package/doc/function_reference.md +0 -238
- package/doc/generator.md +0 -13
- package/doc/handle_scope.md +0 -65
- package/doc/hierarchy.md +0 -91
- package/doc/instance_wrap.md +0 -408
- package/doc/memory_management.md +0 -27
- package/doc/name.md +0 -29
- package/doc/node-gyp.md +0 -82
- package/doc/number.md +0 -163
- package/doc/object.md +0 -279
- package/doc/object_lifetime_management.md +0 -83
- package/doc/object_reference.md +0 -117
- package/doc/object_wrap.md +0 -561
- package/doc/prebuild_tools.md +0 -16
- package/doc/promises.md +0 -79
- package/doc/property_descriptor.md +0 -286
- package/doc/range_error.md +0 -59
- package/doc/reference.md +0 -111
- package/doc/setup.md +0 -81
- package/doc/string.md +0 -93
- package/doc/symbol.md +0 -48
- package/doc/threadsafe.md +0 -121
- package/doc/threadsafe_function.md +0 -290
- package/doc/type_error.md +0 -59
- package/doc/typed_array.md +0 -78
- package/doc/typed_array_of.md +0 -137
- package/doc/typed_threadsafe_function.md +0 -307
- package/doc/value.md +0 -340
- package/doc/version_management.md +0 -43
package/napi.h
CHANGED
|
@@ -106,9 +106,9 @@ static_assert(sizeof(char16_t) == sizeof(wchar_t), "Size mismatch between char16
|
|
|
106
106
|
} while (0)
|
|
107
107
|
|
|
108
108
|
////////////////////////////////////////////////////////////////////////////////
|
|
109
|
-
///
|
|
109
|
+
/// Node-API C++ Wrapper Classes
|
|
110
110
|
///
|
|
111
|
-
/// These classes wrap the "
|
|
111
|
+
/// These classes wrap the "Node-API" ABI-stable C APIs for Node.js, providing a
|
|
112
112
|
/// C++ object model and C++ exception-handling semantics with low overhead.
|
|
113
113
|
/// The wrappers are all header-only so that they do not affect the ABI.
|
|
114
114
|
////////////////////////////////////////////////////////////////////////////////
|
|
@@ -136,35 +136,48 @@ namespace Napi {
|
|
|
136
136
|
class TypedArray;
|
|
137
137
|
template <typename T> class TypedArrayOf;
|
|
138
138
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
139
|
+
using Int8Array =
|
|
140
|
+
TypedArrayOf<int8_t>; ///< Typed-array of signed 8-bit integers
|
|
141
|
+
using Uint8Array =
|
|
142
|
+
TypedArrayOf<uint8_t>; ///< Typed-array of unsigned 8-bit integers
|
|
143
|
+
using Int16Array =
|
|
144
|
+
TypedArrayOf<int16_t>; ///< Typed-array of signed 16-bit integers
|
|
145
|
+
using Uint16Array =
|
|
146
|
+
TypedArrayOf<uint16_t>; ///< Typed-array of unsigned 16-bit integers
|
|
147
|
+
using Int32Array =
|
|
148
|
+
TypedArrayOf<int32_t>; ///< Typed-array of signed 32-bit integers
|
|
149
|
+
using Uint32Array =
|
|
150
|
+
TypedArrayOf<uint32_t>; ///< Typed-array of unsigned 32-bit integers
|
|
151
|
+
using Float32Array =
|
|
152
|
+
TypedArrayOf<float>; ///< Typed-array of 32-bit floating-point values
|
|
153
|
+
using Float64Array =
|
|
154
|
+
TypedArrayOf<double>; ///< Typed-array of 64-bit floating-point values
|
|
147
155
|
#if NAPI_VERSION > 5
|
|
148
|
-
|
|
149
|
-
|
|
156
|
+
using BigInt64Array =
|
|
157
|
+
TypedArrayOf<int64_t>; ///< Typed array of signed 64-bit integers
|
|
158
|
+
using BigUint64Array =
|
|
159
|
+
TypedArrayOf<uint64_t>; ///< Typed array of unsigned 64-bit integers
|
|
150
160
|
#endif // NAPI_VERSION > 5
|
|
151
161
|
|
|
152
|
-
/// Defines the signature of a
|
|
153
|
-
|
|
162
|
+
/// Defines the signature of a Node-API C++ module's registration callback
|
|
163
|
+
/// (init) function.
|
|
164
|
+
using ModuleRegisterCallback = Object (*)(Env env, Object exports);
|
|
154
165
|
|
|
155
166
|
class MemoryManagement;
|
|
156
167
|
|
|
157
|
-
/// Environment for
|
|
168
|
+
/// Environment for Node-API values and operations.
|
|
158
169
|
///
|
|
159
|
-
/// All
|
|
160
|
-
/// instance is always provided to callback functions; that
|
|
161
|
-
/// creation of
|
|
162
|
-
///
|
|
170
|
+
/// All Node-API values and operations must be associated with an environment.
|
|
171
|
+
/// An environment instance is always provided to callback functions; that
|
|
172
|
+
/// environment must then be used for any creation of Node-API values or other
|
|
173
|
+
/// Node-API operations within the callback. (Many methods infer the
|
|
174
|
+
/// environment from the `this` instance that the method is called on.)
|
|
163
175
|
///
|
|
164
|
-
/// In the future, multiple environments per process may be supported,
|
|
165
|
-
/// implementations only support one environment per process.
|
|
176
|
+
/// In the future, multiple environments per process may be supported,
|
|
177
|
+
/// although current implementations only support one environment per process.
|
|
166
178
|
///
|
|
167
|
-
/// In the V8 JavaScript engine, a
|
|
179
|
+
/// In the V8 JavaScript engine, a Node-API environment approximately
|
|
180
|
+
/// corresponds to an Isolate.
|
|
168
181
|
class Env {
|
|
169
182
|
#if NAPI_VERSION > 5
|
|
170
183
|
private:
|
|
@@ -222,7 +235,8 @@ namespace Napi {
|
|
|
222
235
|
class Value {
|
|
223
236
|
public:
|
|
224
237
|
Value(); ///< Creates a new _empty_ Value instance.
|
|
225
|
-
Value(napi_env env,
|
|
238
|
+
Value(napi_env env,
|
|
239
|
+
napi_value value); ///< Wraps a Node-API value primitive.
|
|
226
240
|
|
|
227
241
|
/// Creates a JS value from a C++ primitive.
|
|
228
242
|
///
|
|
@@ -239,7 +253,7 @@ namespace Napi {
|
|
|
239
253
|
template <typename T>
|
|
240
254
|
static Value From(napi_env env, const T& value);
|
|
241
255
|
|
|
242
|
-
/// Converts to a
|
|
256
|
+
/// Converts to a Node-API value primitive.
|
|
243
257
|
///
|
|
244
258
|
/// If the instance is _empty_, this returns `nullptr`.
|
|
245
259
|
operator napi_value() const;
|
|
@@ -312,80 +326,92 @@ namespace Napi {
|
|
|
312
326
|
/// A JavaScript boolean value.
|
|
313
327
|
class Boolean : public Value {
|
|
314
328
|
public:
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
);
|
|
329
|
+
static Boolean New(napi_env env, ///< Node-API environment
|
|
330
|
+
bool value ///< Boolean value
|
|
331
|
+
);
|
|
319
332
|
|
|
320
|
-
|
|
321
|
-
|
|
333
|
+
Boolean(); ///< Creates a new _empty_ Boolean instance.
|
|
334
|
+
Boolean(napi_env env,
|
|
335
|
+
napi_value value); ///< Wraps a Node-API value primitive.
|
|
322
336
|
|
|
323
|
-
|
|
324
|
-
|
|
337
|
+
operator bool() const; ///< Converts a Boolean value to a boolean primitive.
|
|
338
|
+
bool Value() const; ///< Converts a Boolean value to a boolean primitive.
|
|
325
339
|
};
|
|
326
340
|
|
|
327
341
|
/// A JavaScript number value.
|
|
328
342
|
class Number : public Value {
|
|
329
343
|
public:
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
344
|
+
static Number New(napi_env env, ///< Node-API environment
|
|
345
|
+
double value ///< Number value
|
|
346
|
+
);
|
|
347
|
+
|
|
348
|
+
Number(); ///< Creates a new _empty_ Number instance.
|
|
349
|
+
Number(napi_env env,
|
|
350
|
+
napi_value value); ///< Wraps a Node-API value primitive.
|
|
351
|
+
|
|
352
|
+
operator int32_t()
|
|
353
|
+
const; ///< Converts a Number value to a 32-bit signed integer value.
|
|
354
|
+
operator uint32_t()
|
|
355
|
+
const; ///< Converts a Number value to a 32-bit unsigned integer value.
|
|
356
|
+
operator int64_t()
|
|
357
|
+
const; ///< Converts a Number value to a 64-bit signed integer value.
|
|
358
|
+
operator float()
|
|
359
|
+
const; ///< Converts a Number value to a 32-bit floating-point value.
|
|
360
|
+
operator double()
|
|
361
|
+
const; ///< Converts a Number value to a 64-bit floating-point value.
|
|
362
|
+
|
|
363
|
+
int32_t Int32Value()
|
|
364
|
+
const; ///< Converts a Number value to a 32-bit signed integer value.
|
|
365
|
+
uint32_t Uint32Value()
|
|
366
|
+
const; ///< Converts a Number value to a 32-bit unsigned integer value.
|
|
367
|
+
int64_t Int64Value()
|
|
368
|
+
const; ///< Converts a Number value to a 64-bit signed integer value.
|
|
369
|
+
float FloatValue()
|
|
370
|
+
const; ///< Converts a Number value to a 32-bit floating-point value.
|
|
371
|
+
double DoubleValue()
|
|
372
|
+
const; ///< Converts a Number value to a 64-bit floating-point value.
|
|
349
373
|
};
|
|
350
374
|
|
|
351
375
|
#if NAPI_VERSION > 5
|
|
352
376
|
/// A JavaScript bigint value.
|
|
353
377
|
class BigInt : public Value {
|
|
354
378
|
public:
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
379
|
+
static BigInt New(napi_env env, ///< Node-API environment
|
|
380
|
+
int64_t value ///< Number value
|
|
381
|
+
);
|
|
382
|
+
static BigInt New(napi_env env, ///< Node-API environment
|
|
383
|
+
uint64_t value ///< Number value
|
|
384
|
+
);
|
|
385
|
+
|
|
386
|
+
/// Creates a new BigInt object using a specified sign bit and a
|
|
387
|
+
/// specified list of digits/words.
|
|
388
|
+
/// The resulting number is calculated as:
|
|
389
|
+
/// (-1)^sign_bit * (words[0] * (2^64)^0 + words[1] * (2^64)^1 + ...)
|
|
390
|
+
static BigInt New(napi_env env, ///< Node-API environment
|
|
391
|
+
int sign_bit, ///< Sign bit. 1 if negative.
|
|
392
|
+
size_t word_count, ///< Number of words in array
|
|
393
|
+
const uint64_t* words ///< Array of words
|
|
394
|
+
);
|
|
395
|
+
|
|
396
|
+
BigInt(); ///< Creates a new _empty_ BigInt instance.
|
|
397
|
+
BigInt(napi_env env,
|
|
398
|
+
napi_value value); ///< Wraps a Node-API value primitive.
|
|
399
|
+
|
|
400
|
+
int64_t Int64Value(bool* lossless)
|
|
401
|
+
const; ///< Converts a BigInt value to a 64-bit signed integer value.
|
|
402
|
+
uint64_t Uint64Value(bool* lossless)
|
|
403
|
+
const; ///< Converts a BigInt value to a 64-bit unsigned integer value.
|
|
404
|
+
|
|
405
|
+
size_t WordCount() const; ///< The number of 64-bit words needed to store
|
|
406
|
+
///< the result of ToWords().
|
|
407
|
+
|
|
408
|
+
/// Writes the contents of this BigInt to a specified memory location.
|
|
409
|
+
/// `sign_bit` must be provided and will be set to 1 if this BigInt is
|
|
410
|
+
/// negative.
|
|
411
|
+
/// `*word_count` has to be initialized to the length of the `words` array.
|
|
412
|
+
/// Upon return, it will be set to the actual number of words that would
|
|
413
|
+
/// be needed to store this BigInt (i.e. the return value of `WordCount()`).
|
|
414
|
+
void ToWords(int* sign_bit, size_t* word_count, uint64_t* words);
|
|
389
415
|
};
|
|
390
416
|
#endif // NAPI_VERSION > 5
|
|
391
417
|
|
|
@@ -394,16 +420,15 @@ namespace Napi {
|
|
|
394
420
|
class Date : public Value {
|
|
395
421
|
public:
|
|
396
422
|
/// Creates a new Date value from a double primitive.
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
);
|
|
423
|
+
static Date New(napi_env env, ///< Node-API environment
|
|
424
|
+
double value ///< Number value
|
|
425
|
+
);
|
|
401
426
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
427
|
+
Date(); ///< Creates a new _empty_ Date instance.
|
|
428
|
+
Date(napi_env env, napi_value value); ///< Wraps a Node-API value primitive.
|
|
429
|
+
operator double() const; ///< Converts a Date value to double primitive
|
|
405
430
|
|
|
406
|
-
|
|
431
|
+
double ValueOf() const; ///< Converts a Date value to a double primitive.
|
|
407
432
|
};
|
|
408
433
|
#endif
|
|
409
434
|
|
|
@@ -411,102 +436,111 @@ namespace Napi {
|
|
|
411
436
|
class Name : public Value {
|
|
412
437
|
public:
|
|
413
438
|
Name(); ///< Creates a new _empty_ Name instance.
|
|
414
|
-
Name(napi_env env,
|
|
439
|
+
Name(napi_env env,
|
|
440
|
+
napi_value value); ///< Wraps a Node-API value primitive.
|
|
415
441
|
};
|
|
416
442
|
|
|
417
443
|
/// A JavaScript string value.
|
|
418
444
|
class String : public Name {
|
|
419
445
|
public:
|
|
420
446
|
/// Creates a new String value from a UTF-8 encoded C++ string.
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
447
|
+
static String New(napi_env env, ///< Node-API environment
|
|
448
|
+
const std::string& value ///< UTF-8 encoded C++ string
|
|
449
|
+
);
|
|
450
|
+
|
|
451
|
+
/// Creates a new String value from a UTF-16 encoded C++ string.
|
|
452
|
+
static String New(napi_env env, ///< Node-API environment
|
|
453
|
+
const std::u16string& value ///< UTF-16 encoded C++ string
|
|
454
|
+
);
|
|
455
|
+
|
|
456
|
+
/// Creates a new String value from a UTF-8 encoded C string.
|
|
457
|
+
static String New(
|
|
458
|
+
napi_env env, ///< Node-API environment
|
|
459
|
+
const char* value ///< UTF-8 encoded null-terminated C string
|
|
460
|
+
);
|
|
461
|
+
|
|
462
|
+
/// Creates a new String value from a UTF-16 encoded C string.
|
|
463
|
+
static String New(
|
|
464
|
+
napi_env env, ///< Node-API environment
|
|
465
|
+
const char16_t* value ///< UTF-16 encoded null-terminated C string
|
|
466
|
+
);
|
|
467
|
+
|
|
468
|
+
/// Creates a new String value from a UTF-8 encoded C string with specified
|
|
469
|
+
/// length.
|
|
470
|
+
static String New(napi_env env, ///< Node-API environment
|
|
471
|
+
const char* value, ///< UTF-8 encoded C string (not
|
|
472
|
+
///< necessarily null-terminated)
|
|
473
|
+
size_t length ///< length of the string in bytes
|
|
474
|
+
);
|
|
475
|
+
|
|
476
|
+
/// Creates a new String value from a UTF-16 encoded C string with specified
|
|
477
|
+
/// length.
|
|
478
|
+
static String New(
|
|
479
|
+
napi_env env, ///< Node-API environment
|
|
480
|
+
const char16_t* value, ///< UTF-16 encoded C string (not necessarily
|
|
481
|
+
///< null-terminated)
|
|
482
|
+
size_t length ///< Length of the string in 2-byte code units
|
|
483
|
+
);
|
|
484
|
+
|
|
485
|
+
/// Creates a new String based on the original object's type.
|
|
486
|
+
///
|
|
487
|
+
/// `value` may be any of:
|
|
488
|
+
/// - const char* (encoded using UTF-8, null-terminated)
|
|
489
|
+
/// - const char16_t* (encoded using UTF-16-LE, null-terminated)
|
|
490
|
+
/// - std::string (encoded using UTF-8)
|
|
491
|
+
/// - std::u16string
|
|
492
|
+
template <typename T>
|
|
493
|
+
static String From(napi_env env, const T& value);
|
|
494
|
+
|
|
495
|
+
String(); ///< Creates a new _empty_ String instance.
|
|
496
|
+
String(napi_env env,
|
|
497
|
+
napi_value value); ///< Wraps a Node-API value primitive.
|
|
498
|
+
|
|
499
|
+
operator std::string()
|
|
500
|
+
const; ///< Converts a String value to a UTF-8 encoded C++ string.
|
|
501
|
+
operator std::u16string()
|
|
502
|
+
const; ///< Converts a String value to a UTF-16 encoded C++ string.
|
|
503
|
+
std::string Utf8Value()
|
|
504
|
+
const; ///< Converts a String value to a UTF-8 encoded C++ string.
|
|
505
|
+
std::u16string Utf16Value()
|
|
506
|
+
const; ///< Converts a String value to a UTF-16 encoded C++ string.
|
|
475
507
|
};
|
|
476
508
|
|
|
477
509
|
/// A JavaScript symbol value.
|
|
478
510
|
class Symbol : public Name {
|
|
479
511
|
public:
|
|
480
512
|
/// Creates a new Symbol value with an optional description.
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
513
|
+
static Symbol New(
|
|
514
|
+
napi_env env, ///< Node-API environment
|
|
515
|
+
const char* description =
|
|
516
|
+
nullptr ///< Optional UTF-8 encoded null-terminated C string
|
|
517
|
+
/// describing the symbol
|
|
518
|
+
);
|
|
519
|
+
|
|
520
|
+
/// Creates a new Symbol value with a description.
|
|
521
|
+
static Symbol New(
|
|
522
|
+
napi_env env, ///< Node-API environment
|
|
523
|
+
const std::string&
|
|
524
|
+
description ///< UTF-8 encoded C++ string describing the symbol
|
|
525
|
+
);
|
|
526
|
+
|
|
527
|
+
/// Creates a new Symbol value with a description.
|
|
528
|
+
static Symbol New(napi_env env, ///< Node-API environment
|
|
529
|
+
String description ///< String value describing the symbol
|
|
530
|
+
);
|
|
531
|
+
|
|
532
|
+
/// Creates a new Symbol value with a description.
|
|
533
|
+
static Symbol New(
|
|
534
|
+
napi_env env, ///< Node-API environment
|
|
535
|
+
napi_value description ///< String value describing the symbol
|
|
536
|
+
);
|
|
537
|
+
|
|
538
|
+
/// Get a public Symbol (e.g. Symbol.iterator).
|
|
539
|
+
static Symbol WellKnown(napi_env, const std::string& name);
|
|
540
|
+
|
|
541
|
+
Symbol(); ///< Creates a new _empty_ Symbol instance.
|
|
542
|
+
Symbol(napi_env env,
|
|
543
|
+
napi_value value); ///< Wraps a Node-API value primitive.
|
|
510
544
|
};
|
|
511
545
|
|
|
512
546
|
/// A JavaScript object value.
|
|
@@ -542,12 +576,12 @@ namespace Napi {
|
|
|
542
576
|
};
|
|
543
577
|
|
|
544
578
|
/// Creates a new Object value.
|
|
545
|
-
static Object New(
|
|
546
|
-
napi_env env ///< N-API environment
|
|
579
|
+
static Object New(napi_env env ///< Node-API environment
|
|
547
580
|
);
|
|
548
581
|
|
|
549
582
|
Object(); ///< Creates a new _empty_ Object instance.
|
|
550
|
-
Object(napi_env env,
|
|
583
|
+
Object(napi_env env,
|
|
584
|
+
napi_value value); ///< Wraps a Node-API value primitive.
|
|
551
585
|
|
|
552
586
|
/// Gets or sets a named property.
|
|
553
587
|
PropertyLValue<std::string> operator [](
|
|
@@ -641,30 +675,26 @@ namespace Napi {
|
|
|
641
675
|
|
|
642
676
|
/// Sets a property.
|
|
643
677
|
template <typename ValueType>
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
const ValueType& value ///< Property value primitive
|
|
678
|
+
bool Set(napi_value key, ///< Property key primitive
|
|
679
|
+
const ValueType& value ///< Property value primitive
|
|
647
680
|
);
|
|
648
681
|
|
|
649
682
|
/// Sets a property.
|
|
650
683
|
template <typename ValueType>
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
const ValueType& value ///< Property value
|
|
684
|
+
bool Set(Value key, ///< Property key
|
|
685
|
+
const ValueType& value ///< Property value
|
|
654
686
|
);
|
|
655
687
|
|
|
656
688
|
/// Sets a named property.
|
|
657
689
|
template <typename ValueType>
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
);
|
|
690
|
+
bool Set(
|
|
691
|
+
const char* utf8name, ///< UTF-8 encoded null-terminated property name
|
|
692
|
+
const ValueType& value);
|
|
662
693
|
|
|
663
694
|
/// Sets a named property.
|
|
664
695
|
template <typename ValueType>
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
const ValueType& value ///< Property value primitive
|
|
696
|
+
bool Set(const std::string& utf8name, ///< UTF-8 encoded property name
|
|
697
|
+
const ValueType& value ///< Property value primitive
|
|
668
698
|
);
|
|
669
699
|
|
|
670
700
|
/// Delete property.
|
|
@@ -699,9 +729,8 @@ namespace Napi {
|
|
|
699
729
|
|
|
700
730
|
/// Sets an indexed property or array element.
|
|
701
731
|
template <typename ValueType>
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
const ValueType& value ///< Property value primitive
|
|
732
|
+
bool Set(uint32_t index, ///< Property / element index
|
|
733
|
+
const ValueType& value ///< Property value primitive
|
|
705
734
|
);
|
|
706
735
|
|
|
707
736
|
/// Deletes an indexed property or array element.
|
|
@@ -712,19 +741,20 @@ namespace Napi {
|
|
|
712
741
|
Array GetPropertyNames() const; ///< Get all property names
|
|
713
742
|
|
|
714
743
|
/// Defines a property on the object.
|
|
715
|
-
|
|
716
|
-
|
|
744
|
+
bool DefineProperty(
|
|
745
|
+
const PropertyDescriptor&
|
|
746
|
+
property ///< Descriptor for the property to be defined
|
|
717
747
|
);
|
|
718
748
|
|
|
719
749
|
/// Defines properties on the object.
|
|
720
|
-
|
|
721
|
-
|
|
750
|
+
bool DefineProperties(
|
|
751
|
+
const std::initializer_list<PropertyDescriptor>& properties
|
|
722
752
|
///< List of descriptors for the properties to be defined
|
|
723
753
|
);
|
|
724
754
|
|
|
725
755
|
/// Defines properties on the object.
|
|
726
|
-
|
|
727
|
-
|
|
756
|
+
bool DefineProperties(
|
|
757
|
+
const std::vector<PropertyDescriptor>& properties
|
|
728
758
|
///< Vector of descriptors for the properties to be defined
|
|
729
759
|
);
|
|
730
760
|
|
|
@@ -742,6 +772,10 @@ namespace Napi {
|
|
|
742
772
|
inline void AddFinalizer(Finalizer finalizeCallback,
|
|
743
773
|
T* data,
|
|
744
774
|
Hint* finalizeHint);
|
|
775
|
+
#if NAPI_VERSION >= 8
|
|
776
|
+
bool Freeze();
|
|
777
|
+
bool Seal();
|
|
778
|
+
#endif // NAPI_VERSION >= 8
|
|
745
779
|
};
|
|
746
780
|
|
|
747
781
|
template <typename T>
|
|
@@ -782,46 +816,61 @@ namespace Napi {
|
|
|
782
816
|
class ArrayBuffer : public Object {
|
|
783
817
|
public:
|
|
784
818
|
/// Creates a new ArrayBuffer instance over a new automatically-allocated buffer.
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
819
|
+
static ArrayBuffer New(
|
|
820
|
+
napi_env env, ///< Node-API environment
|
|
821
|
+
size_t byteLength ///< Length of the buffer to be allocated, in bytes
|
|
822
|
+
);
|
|
823
|
+
|
|
824
|
+
/// Creates a new ArrayBuffer instance, using an external buffer with
|
|
825
|
+
/// specified byte length.
|
|
826
|
+
static ArrayBuffer New(
|
|
827
|
+
napi_env env, ///< Node-API environment
|
|
828
|
+
void* externalData, ///< Pointer to the external buffer to be used by
|
|
829
|
+
///< the array
|
|
830
|
+
size_t byteLength ///< Length of the external buffer to be used by the
|
|
831
|
+
///< array, in bytes
|
|
832
|
+
);
|
|
833
|
+
|
|
834
|
+
/// Creates a new ArrayBuffer instance, using an external buffer with
|
|
835
|
+
/// specified byte length.
|
|
836
|
+
template <typename Finalizer>
|
|
837
|
+
static ArrayBuffer New(
|
|
838
|
+
napi_env env, ///< Node-API environment
|
|
839
|
+
void* externalData, ///< Pointer to the external buffer to be used by
|
|
840
|
+
///< the array
|
|
841
|
+
size_t byteLength, ///< Length of the external buffer to be used by the
|
|
842
|
+
///< array,
|
|
843
|
+
/// in bytes
|
|
844
|
+
Finalizer finalizeCallback ///< Function to be called when the array
|
|
845
|
+
///< buffer is destroyed;
|
|
846
|
+
/// must implement `void operator()(Env env,
|
|
847
|
+
/// void* externalData)`
|
|
848
|
+
);
|
|
849
|
+
|
|
850
|
+
/// Creates a new ArrayBuffer instance, using an external buffer with
|
|
851
|
+
/// specified byte length.
|
|
852
|
+
template <typename Finalizer, typename Hint>
|
|
853
|
+
static ArrayBuffer New(
|
|
854
|
+
napi_env env, ///< Node-API environment
|
|
855
|
+
void* externalData, ///< Pointer to the external buffer to be used by
|
|
856
|
+
///< the array
|
|
857
|
+
size_t byteLength, ///< Length of the external buffer to be used by the
|
|
858
|
+
///< array,
|
|
859
|
+
/// in bytes
|
|
860
|
+
Finalizer finalizeCallback, ///< Function to be called when the array
|
|
861
|
+
///< buffer is destroyed;
|
|
862
|
+
/// must implement `void operator()(Env
|
|
863
|
+
/// env, void* externalData, Hint* hint)`
|
|
864
|
+
Hint* finalizeHint ///< Hint (second parameter) to be passed to the
|
|
865
|
+
///< finalize callback
|
|
866
|
+
);
|
|
867
|
+
|
|
868
|
+
ArrayBuffer(); ///< Creates a new _empty_ ArrayBuffer instance.
|
|
869
|
+
ArrayBuffer(napi_env env,
|
|
870
|
+
napi_value value); ///< Wraps a Node-API value primitive.
|
|
871
|
+
|
|
872
|
+
void* Data(); ///< Gets a pointer to the data buffer.
|
|
873
|
+
size_t ByteLength(); ///< Gets the length of the array buffer in bytes.
|
|
825
874
|
|
|
826
875
|
#if NAPI_VERSION >= 7
|
|
827
876
|
bool IsDetached() const;
|
|
@@ -841,7 +890,8 @@ namespace Napi {
|
|
|
841
890
|
class TypedArray : public Object {
|
|
842
891
|
public:
|
|
843
892
|
TypedArray(); ///< Creates a new _empty_ TypedArray instance.
|
|
844
|
-
TypedArray(napi_env env,
|
|
893
|
+
TypedArray(napi_env env,
|
|
894
|
+
napi_value value); ///< Wraps a Node-API value primitive.
|
|
845
895
|
|
|
846
896
|
napi_typedarray_type TypedArrayType() const; ///< Gets the type of this typed-array.
|
|
847
897
|
Napi::ArrayBuffer ArrayBuffer() const; ///< Gets the backing array buffer.
|
|
@@ -896,16 +946,19 @@ namespace Napi {
|
|
|
896
946
|
/// parameter T), except when creating a "clamped" array:
|
|
897
947
|
///
|
|
898
948
|
/// Uint8Array::New(env, length, napi_uint8_clamped_array)
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
949
|
+
static TypedArrayOf New(
|
|
950
|
+
napi_env env, ///< Node-API environment
|
|
951
|
+
size_t elementLength, ///< Length of the created array, as a number of
|
|
952
|
+
///< elements
|
|
902
953
|
#if defined(NAPI_HAS_CONSTEXPR)
|
|
903
|
-
|
|
954
|
+
napi_typedarray_type type =
|
|
955
|
+
TypedArray::TypedArrayTypeForPrimitiveType<T>()
|
|
904
956
|
#else
|
|
905
|
-
|
|
957
|
+
napi_typedarray_type type
|
|
906
958
|
#endif
|
|
907
|
-
|
|
908
|
-
|
|
959
|
+
///< Type of array, if different from the default array type for the
|
|
960
|
+
///< template parameter T.
|
|
961
|
+
);
|
|
909
962
|
|
|
910
963
|
/// Creates a new TypedArray instance over a provided array buffer.
|
|
911
964
|
///
|
|
@@ -913,21 +966,26 @@ namespace Napi {
|
|
|
913
966
|
/// parameter T), except when creating a "clamped" array:
|
|
914
967
|
///
|
|
915
968
|
/// Uint8Array::New(env, length, buffer, 0, napi_uint8_clamped_array)
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
969
|
+
static TypedArrayOf New(
|
|
970
|
+
napi_env env, ///< Node-API environment
|
|
971
|
+
size_t elementLength, ///< Length of the created array, as a number of
|
|
972
|
+
///< elements
|
|
973
|
+
Napi::ArrayBuffer arrayBuffer, ///< Backing array buffer instance to use
|
|
974
|
+
size_t bufferOffset, ///< Offset into the array buffer where the
|
|
975
|
+
///< typed-array starts
|
|
921
976
|
#if defined(NAPI_HAS_CONSTEXPR)
|
|
922
|
-
|
|
977
|
+
napi_typedarray_type type =
|
|
978
|
+
TypedArray::TypedArrayTypeForPrimitiveType<T>()
|
|
923
979
|
#else
|
|
924
|
-
|
|
980
|
+
napi_typedarray_type type
|
|
925
981
|
#endif
|
|
926
|
-
|
|
927
|
-
|
|
982
|
+
///< Type of array, if different from the default array type for the
|
|
983
|
+
///< template parameter T.
|
|
984
|
+
);
|
|
928
985
|
|
|
929
986
|
TypedArrayOf(); ///< Creates a new _empty_ TypedArrayOf instance.
|
|
930
|
-
TypedArrayOf(napi_env env,
|
|
987
|
+
TypedArrayOf(napi_env env,
|
|
988
|
+
napi_value value); ///< Wraps a Node-API value primitive.
|
|
931
989
|
|
|
932
990
|
T& operator [](size_t index); ///< Gets or sets an element in the array.
|
|
933
991
|
const T& operator [](size_t index) const; ///< Gets an element in the array.
|
|
@@ -969,7 +1027,8 @@ namespace Napi {
|
|
|
969
1027
|
size_t byteLength);
|
|
970
1028
|
|
|
971
1029
|
DataView(); ///< Creates a new _empty_ DataView instance.
|
|
972
|
-
DataView(napi_env env,
|
|
1030
|
+
DataView(napi_env env,
|
|
1031
|
+
napi_value value); ///< Wraps a Node-API value primitive.
|
|
973
1032
|
|
|
974
1033
|
Napi::ArrayBuffer ArrayBuffer() const; ///< Gets the backing array buffer.
|
|
975
1034
|
size_t ByteOffset() const; ///< Gets the offset into the buffer where the array starts.
|
|
@@ -1008,70 +1067,71 @@ namespace Napi {
|
|
|
1008
1067
|
|
|
1009
1068
|
class Function : public Object {
|
|
1010
1069
|
public:
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1070
|
+
using VoidCallback = void (*)(const CallbackInfo& info);
|
|
1071
|
+
using Callback = Value (*)(const CallbackInfo& info);
|
|
1072
|
+
|
|
1073
|
+
template <VoidCallback cb>
|
|
1074
|
+
static Function New(napi_env env,
|
|
1075
|
+
const char* utf8name = nullptr,
|
|
1076
|
+
void* data = nullptr);
|
|
1077
|
+
|
|
1078
|
+
template <Callback cb>
|
|
1079
|
+
static Function New(napi_env env,
|
|
1080
|
+
const char* utf8name = nullptr,
|
|
1081
|
+
void* data = nullptr);
|
|
1082
|
+
|
|
1083
|
+
template <VoidCallback cb>
|
|
1084
|
+
static Function New(napi_env env,
|
|
1085
|
+
const std::string& utf8name,
|
|
1086
|
+
void* data = nullptr);
|
|
1087
|
+
|
|
1088
|
+
template <Callback cb>
|
|
1089
|
+
static Function New(napi_env env,
|
|
1090
|
+
const std::string& utf8name,
|
|
1091
|
+
void* data = nullptr);
|
|
1092
|
+
|
|
1093
|
+
/// Callable must implement operator() accepting a const CallbackInfo&
|
|
1094
|
+
/// and return either void or Value.
|
|
1095
|
+
template <typename Callable>
|
|
1096
|
+
static Function New(napi_env env,
|
|
1097
|
+
Callable cb,
|
|
1098
|
+
const char* utf8name = nullptr,
|
|
1099
|
+
void* data = nullptr);
|
|
1100
|
+
/// Callable must implement operator() accepting a const CallbackInfo&
|
|
1101
|
+
/// and return either void or Value.
|
|
1102
|
+
template <typename Callable>
|
|
1103
|
+
static Function New(napi_env env,
|
|
1104
|
+
Callable cb,
|
|
1105
|
+
const std::string& utf8name,
|
|
1106
|
+
void* data = nullptr);
|
|
1107
|
+
|
|
1108
|
+
Function();
|
|
1109
|
+
Function(napi_env env, napi_value value);
|
|
1110
|
+
|
|
1111
|
+
Value operator()(const std::initializer_list<napi_value>& args) const;
|
|
1112
|
+
|
|
1113
|
+
Value Call(const std::initializer_list<napi_value>& args) const;
|
|
1114
|
+
Value Call(const std::vector<napi_value>& args) const;
|
|
1115
|
+
Value Call(size_t argc, const napi_value* args) const;
|
|
1116
|
+
Value Call(napi_value recv,
|
|
1117
|
+
const std::initializer_list<napi_value>& args) const;
|
|
1118
|
+
Value Call(napi_value recv, const std::vector<napi_value>& args) const;
|
|
1119
|
+
Value Call(napi_value recv, size_t argc, const napi_value* args) const;
|
|
1120
|
+
|
|
1121
|
+
Value MakeCallback(napi_value recv,
|
|
1122
|
+
const std::initializer_list<napi_value>& args,
|
|
1123
|
+
napi_async_context context = nullptr) const;
|
|
1124
|
+
Value MakeCallback(napi_value recv,
|
|
1125
|
+
const std::vector<napi_value>& args,
|
|
1126
|
+
napi_async_context context = nullptr) const;
|
|
1127
|
+
Value MakeCallback(napi_value recv,
|
|
1128
|
+
size_t argc,
|
|
1129
|
+
const napi_value* args,
|
|
1130
|
+
napi_async_context context = nullptr) const;
|
|
1131
|
+
|
|
1132
|
+
Object New(const std::initializer_list<napi_value>& args) const;
|
|
1133
|
+
Object New(const std::vector<napi_value>& args) const;
|
|
1134
|
+
Object New(size_t argc, const napi_value* args) const;
|
|
1075
1135
|
};
|
|
1076
1136
|
|
|
1077
1137
|
class Promise : public Object {
|
|
@@ -1164,9 +1224,11 @@ namespace Napi {
|
|
|
1164
1224
|
void Reset();
|
|
1165
1225
|
void Reset(const T& value, uint32_t refcount = 0);
|
|
1166
1226
|
|
|
1167
|
-
// Call this on a reference that is declared as static data, to prevent its
|
|
1168
|
-
// from running at program shutdown time, which would attempt to
|
|
1169
|
-
// the environment is no longer valid.
|
|
1227
|
+
// Call this on a reference that is declared as static data, to prevent its
|
|
1228
|
+
// destructor from running at program shutdown time, which would attempt to
|
|
1229
|
+
// reset the reference when the environment is no longer valid. Avoid using
|
|
1230
|
+
// this if at all possible. If you do need to use static data, MAKE SURE to
|
|
1231
|
+
// warn your users that your addon is NOT threadsafe.
|
|
1170
1232
|
void SuppressDestruct();
|
|
1171
1233
|
|
|
1172
1234
|
protected:
|
|
@@ -1195,26 +1257,26 @@ namespace Napi {
|
|
|
1195
1257
|
|
|
1196
1258
|
Napi::Value Get(const char* utf8name) const;
|
|
1197
1259
|
Napi::Value Get(const std::string& utf8name) const;
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1260
|
+
bool Set(const char* utf8name, napi_value value);
|
|
1261
|
+
bool Set(const char* utf8name, Napi::Value value);
|
|
1262
|
+
bool Set(const char* utf8name, const char* utf8value);
|
|
1263
|
+
bool Set(const char* utf8name, bool boolValue);
|
|
1264
|
+
bool Set(const char* utf8name, double numberValue);
|
|
1265
|
+
bool Set(const std::string& utf8name, napi_value value);
|
|
1266
|
+
bool Set(const std::string& utf8name, Napi::Value value);
|
|
1267
|
+
bool Set(const std::string& utf8name, std::string& utf8value);
|
|
1268
|
+
bool Set(const std::string& utf8name, bool boolValue);
|
|
1269
|
+
bool Set(const std::string& utf8name, double numberValue);
|
|
1208
1270
|
|
|
1209
1271
|
Napi::Value Get(uint32_t index) const;
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1272
|
+
bool Set(uint32_t index, const napi_value value);
|
|
1273
|
+
bool Set(uint32_t index, const Napi::Value value);
|
|
1274
|
+
bool Set(uint32_t index, const char* utf8value);
|
|
1275
|
+
bool Set(uint32_t index, const std::string& utf8value);
|
|
1276
|
+
bool Set(uint32_t index, bool boolValue);
|
|
1277
|
+
bool Set(uint32_t index, double numberValue);
|
|
1216
1278
|
|
|
1217
|
-
|
|
1279
|
+
protected:
|
|
1218
1280
|
ObjectReference(const ObjectReference&);
|
|
1219
1281
|
};
|
|
1220
1282
|
|
|
@@ -1263,43 +1325,48 @@ namespace Napi {
|
|
|
1263
1325
|
ObjectReference Persistent(Object value);
|
|
1264
1326
|
FunctionReference Persistent(Function value);
|
|
1265
1327
|
|
|
1266
|
-
/// A persistent reference to a JavaScript error object. Use of this class
|
|
1267
|
-
/// on whether C++ exceptions are enabled at compile time.
|
|
1328
|
+
/// A persistent reference to a JavaScript error object. Use of this class
|
|
1329
|
+
/// depends somewhat on whether C++ exceptions are enabled at compile time.
|
|
1268
1330
|
///
|
|
1269
1331
|
/// ### Handling Errors With C++ Exceptions
|
|
1270
1332
|
///
|
|
1271
|
-
/// If C++ exceptions are enabled, then the `Error` class extends
|
|
1272
|
-
/// integrated error-handling for C++ exceptions
|
|
1333
|
+
/// If C++ exceptions are enabled, then the `Error` class extends
|
|
1334
|
+
/// `std::exception` and enables integrated error-handling for C++ exceptions
|
|
1335
|
+
/// and JavaScript exceptions.
|
|
1273
1336
|
///
|
|
1274
|
-
/// If a
|
|
1275
|
-
/// argument), then the
|
|
1276
|
-
///
|
|
1277
|
-
///
|
|
1278
|
-
/// a
|
|
1337
|
+
/// If a Node-API call fails without executing any JavaScript code (for
|
|
1338
|
+
/// example due to an invalid argument), then the Node-API wrapper
|
|
1339
|
+
/// automatically converts and throws the error as a C++ exception of type
|
|
1340
|
+
/// `Napi::Error`. Or if a JavaScript function called by C++ code via Node-API
|
|
1341
|
+
/// throws a JavaScript exception, then the Node-API wrapper automatically
|
|
1342
|
+
/// converts and throws it as a C++ exception of type `Napi::Error`.
|
|
1279
1343
|
///
|
|
1280
|
-
/// If a C++ exception of type `Napi::Error` escapes from a
|
|
1281
|
-
/// wrapper automatically converts and throws it
|
|
1282
|
-
/// a
|
|
1344
|
+
/// If a C++ exception of type `Napi::Error` escapes from a Node-API C++
|
|
1345
|
+
/// callback, then the Node-API wrapper automatically converts and throws it
|
|
1346
|
+
/// as a JavaScript exception. Therefore, catching a C++ exception of type
|
|
1347
|
+
/// `Napi::Error` prevents a JavaScript exception from being thrown.
|
|
1283
1348
|
///
|
|
1284
1349
|
/// #### Example 1A - Throwing a C++ exception:
|
|
1285
1350
|
///
|
|
1286
1351
|
/// Napi::Env env = ...
|
|
1287
1352
|
/// throw Napi::Error::New(env, "Example exception");
|
|
1288
1353
|
///
|
|
1289
|
-
/// Following C++ statements will not be executed. The exception will bubble
|
|
1290
|
-
/// exception of type `Napi::Error`, until it is either caught
|
|
1291
|
-
///
|
|
1354
|
+
/// Following C++ statements will not be executed. The exception will bubble
|
|
1355
|
+
/// up as a C++ exception of type `Napi::Error`, until it is either caught
|
|
1356
|
+
/// while still in C++, or else automatically propataged as a JavaScript
|
|
1357
|
+
/// exception when the callback returns to JavaScript.
|
|
1292
1358
|
///
|
|
1293
|
-
/// #### Example 2A - Propagating a
|
|
1359
|
+
/// #### Example 2A - Propagating a Node-API C++ exception:
|
|
1294
1360
|
///
|
|
1295
1361
|
/// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
|
|
1296
1362
|
/// Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
|
|
1297
1363
|
///
|
|
1298
|
-
/// Following C++ statements will not be executed. The exception will bubble
|
|
1299
|
-
/// exception of type `Napi::Error`, until it is either caught
|
|
1300
|
-
///
|
|
1364
|
+
/// Following C++ statements will not be executed. The exception will bubble
|
|
1365
|
+
/// up as a C++ exception of type `Napi::Error`, until it is either caught
|
|
1366
|
+
/// while still in C++, or else automatically propagated as a JavaScript
|
|
1367
|
+
/// exception when the callback returns to JavaScript.
|
|
1301
1368
|
///
|
|
1302
|
-
/// #### Example 3A - Handling a
|
|
1369
|
+
/// #### Example 3A - Handling a Node-API C++ exception:
|
|
1303
1370
|
///
|
|
1304
1371
|
/// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
|
|
1305
1372
|
/// Napi::Value result;
|
|
@@ -1309,38 +1376,42 @@ namespace Napi {
|
|
|
1309
1376
|
/// cerr << "Caught JavaScript exception: " + e.what();
|
|
1310
1377
|
/// }
|
|
1311
1378
|
///
|
|
1312
|
-
/// Since the exception was caught here, it will not be propagated as a
|
|
1379
|
+
/// Since the exception was caught here, it will not be propagated as a
|
|
1380
|
+
/// JavaScript exception.
|
|
1313
1381
|
///
|
|
1314
1382
|
/// ### Handling Errors Without C++ Exceptions
|
|
1315
1383
|
///
|
|
1316
|
-
/// If C++ exceptions are disabled (by defining `NAPI_DISABLE_CPP_EXCEPTIONS`)
|
|
1317
|
-
/// does not extend `std::exception`, and APIs in the `Napi`
|
|
1318
|
-
/// exceptions when they fail. Instead, they raise
|
|
1319
|
-
/// return _empty_ `Value`s. Calling code
|
|
1320
|
-
///
|
|
1321
|
-
///
|
|
1322
|
-
///
|
|
1384
|
+
/// If C++ exceptions are disabled (by defining `NAPI_DISABLE_CPP_EXCEPTIONS`)
|
|
1385
|
+
/// then this class does not extend `std::exception`, and APIs in the `Napi`
|
|
1386
|
+
/// namespace do not throw C++ exceptions when they fail. Instead, they raise
|
|
1387
|
+
/// _pending_ JavaScript exceptions and return _empty_ `Value`s. Calling code
|
|
1388
|
+
/// should check `Value::IsEmpty()` before attempting to use a returned value,
|
|
1389
|
+
/// and may use methods on the `Env` class to check for, get, and clear a
|
|
1390
|
+
/// pending JavaScript exception. If the pending exception is not cleared, it
|
|
1391
|
+
/// will be thrown when the native callback returns to JavaScript.
|
|
1323
1392
|
///
|
|
1324
1393
|
/// #### Example 1B - Throwing a JS exception
|
|
1325
1394
|
///
|
|
1326
1395
|
/// Napi::Env env = ...
|
|
1327
|
-
/// Napi::Error::New(env, "Example
|
|
1328
|
-
/// return;
|
|
1396
|
+
/// Napi::Error::New(env, "Example
|
|
1397
|
+
/// exception").ThrowAsJavaScriptException(); return;
|
|
1329
1398
|
///
|
|
1330
|
-
/// After throwing a JS exception, the code should generally return
|
|
1331
|
-
/// callback, after performing any necessary
|
|
1399
|
+
/// After throwing a JS exception, the code should generally return
|
|
1400
|
+
/// immediately from the native callback, after performing any necessary
|
|
1401
|
+
/// cleanup.
|
|
1332
1402
|
///
|
|
1333
|
-
/// #### Example 2B - Propagating a
|
|
1403
|
+
/// #### Example 2B - Propagating a Node-API JS exception:
|
|
1334
1404
|
///
|
|
1335
1405
|
/// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
|
|
1336
1406
|
/// Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
|
|
1337
1407
|
/// if (result.IsEmpty()) return;
|
|
1338
1408
|
///
|
|
1339
|
-
/// An empty value result from a
|
|
1340
|
-
/// exception is pending. To let the exception propagate, the
|
|
1341
|
-
/// immediately from the native callback, after
|
|
1409
|
+
/// An empty value result from a Node-API call indicates an error occurred,
|
|
1410
|
+
/// and a JavaScript exception is pending. To let the exception propagate, the
|
|
1411
|
+
/// code should generally return immediately from the native callback, after
|
|
1412
|
+
/// performing any necessary cleanup.
|
|
1342
1413
|
///
|
|
1343
|
-
/// #### Example 3B - Handling a
|
|
1414
|
+
/// #### Example 3B - Handling a Node-API JS exception:
|
|
1344
1415
|
///
|
|
1345
1416
|
/// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
|
|
1346
1417
|
/// Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
|
|
@@ -1349,8 +1420,8 @@ namespace Napi {
|
|
|
1349
1420
|
/// cerr << "Caught JavaScript exception: " + e.Message();
|
|
1350
1421
|
/// }
|
|
1351
1422
|
///
|
|
1352
|
-
/// Since the exception was cleared here, it will not be propagated as a
|
|
1353
|
-
/// after the native callback returns.
|
|
1423
|
+
/// Since the exception was cleared here, it will not be propagated as a
|
|
1424
|
+
/// JavaScript exception after the native callback returns.
|
|
1354
1425
|
class Error : public ObjectReference
|
|
1355
1426
|
#ifdef NAPI_CPP_EXCEPTIONS
|
|
1356
1427
|
, public std::exception
|
|
@@ -1381,14 +1452,17 @@ namespace Napi {
|
|
|
1381
1452
|
|
|
1382
1453
|
protected:
|
|
1383
1454
|
/// !cond INTERNAL
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1455
|
+
using create_error_fn = napi_status (*)(napi_env envb,
|
|
1456
|
+
napi_value code,
|
|
1457
|
+
napi_value msg,
|
|
1458
|
+
napi_value* result);
|
|
1459
|
+
|
|
1460
|
+
template <typename TError>
|
|
1461
|
+
static TError New(napi_env env,
|
|
1462
|
+
const char* message,
|
|
1463
|
+
size_t length,
|
|
1464
|
+
create_error_fn create_error);
|
|
1465
|
+
/// !endcond
|
|
1392
1466
|
|
|
1393
1467
|
private:
|
|
1394
1468
|
mutable std::string _message;
|
|
@@ -1443,8 +1517,8 @@ namespace Napi {
|
|
|
1443
1517
|
|
|
1444
1518
|
class PropertyDescriptor {
|
|
1445
1519
|
public:
|
|
1446
|
-
|
|
1447
|
-
|
|
1520
|
+
using GetterCallback = Napi::Value (*)(const Napi::CallbackInfo& info);
|
|
1521
|
+
using SetterCallback = void (*)(const Napi::CallbackInfo& info);
|
|
1448
1522
|
|
|
1449
1523
|
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
|
|
1450
1524
|
template <typename Getter>
|
|
@@ -1664,13 +1738,13 @@ namespace Napi {
|
|
|
1664
1738
|
template <typename T>
|
|
1665
1739
|
class InstanceWrap {
|
|
1666
1740
|
public:
|
|
1741
|
+
using InstanceVoidMethodCallback = void (T::*)(const CallbackInfo& info);
|
|
1742
|
+
using InstanceMethodCallback = Napi::Value (T::*)(const CallbackInfo& info);
|
|
1743
|
+
using InstanceGetterCallback = Napi::Value (T::*)(const CallbackInfo& info);
|
|
1744
|
+
using InstanceSetterCallback = void (T::*)(const CallbackInfo& info,
|
|
1745
|
+
const Napi::Value& value);
|
|
1667
1746
|
|
|
1668
|
-
|
|
1669
|
-
typedef Napi::Value (T::*InstanceMethodCallback)(const CallbackInfo& info);
|
|
1670
|
-
typedef Napi::Value (T::*InstanceGetterCallback)(const CallbackInfo& info);
|
|
1671
|
-
typedef void (T::*InstanceSetterCallback)(const CallbackInfo& info, const Napi::Value& value);
|
|
1672
|
-
|
|
1673
|
-
typedef ClassPropertyDescriptor<T> PropertyDescriptor;
|
|
1747
|
+
using PropertyDescriptor = ClassPropertyDescriptor<T>;
|
|
1674
1748
|
|
|
1675
1749
|
static PropertyDescriptor InstanceMethod(const char* utf8name,
|
|
1676
1750
|
InstanceVoidMethodCallback method,
|
|
@@ -1735,11 +1809,12 @@ namespace Napi {
|
|
|
1735
1809
|
private:
|
|
1736
1810
|
using This = InstanceWrap<T>;
|
|
1737
1811
|
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1812
|
+
using InstanceVoidMethodCallbackData =
|
|
1813
|
+
MethodCallbackData<T, InstanceVoidMethodCallback>;
|
|
1814
|
+
using InstanceMethodCallbackData =
|
|
1815
|
+
MethodCallbackData<T, InstanceMethodCallback>;
|
|
1816
|
+
using InstanceAccessorCallbackData =
|
|
1817
|
+
AccessorCallbackData<T, InstanceGetterCallback, InstanceSetterCallback>;
|
|
1743
1818
|
|
|
1744
1819
|
static napi_value InstanceVoidMethodCallbackWrapper(napi_env env, napi_callback_info info);
|
|
1745
1820
|
static napi_value InstanceMethodCallbackWrapper(napi_env env, napi_callback_info info);
|
|
@@ -1747,13 +1822,18 @@ namespace Napi {
|
|
|
1747
1822
|
static napi_value InstanceSetterCallbackWrapper(napi_env env, napi_callback_info info);
|
|
1748
1823
|
|
|
1749
1824
|
template <InstanceSetterCallback method>
|
|
1750
|
-
static napi_value WrappedMethod(napi_env env,
|
|
1825
|
+
static napi_value WrappedMethod(napi_env env,
|
|
1826
|
+
napi_callback_info info) NAPI_NOEXCEPT;
|
|
1751
1827
|
|
|
1752
1828
|
template <InstanceSetterCallback setter> struct SetterTag {};
|
|
1753
1829
|
|
|
1754
1830
|
template <InstanceSetterCallback setter>
|
|
1755
|
-
static napi_callback WrapSetter(SetterTag<setter>)
|
|
1756
|
-
|
|
1831
|
+
static napi_callback WrapSetter(SetterTag<setter>) NAPI_NOEXCEPT {
|
|
1832
|
+
return &This::WrappedMethod<setter>;
|
|
1833
|
+
}
|
|
1834
|
+
static napi_callback WrapSetter(SetterTag<nullptr>) NAPI_NOEXCEPT {
|
|
1835
|
+
return nullptr;
|
|
1836
|
+
}
|
|
1757
1837
|
};
|
|
1758
1838
|
|
|
1759
1839
|
/// Base class to be extended by C++ classes exposed to JavaScript; each C++ class instance gets
|
|
@@ -1790,12 +1870,13 @@ namespace Napi {
|
|
|
1790
1870
|
static T* Unwrap(Object wrapper);
|
|
1791
1871
|
|
|
1792
1872
|
// Methods exposed to JavaScript must conform to one of these callback signatures.
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1873
|
+
using StaticVoidMethodCallback = void (*)(const CallbackInfo& info);
|
|
1874
|
+
using StaticMethodCallback = Napi::Value (*)(const CallbackInfo& info);
|
|
1875
|
+
using StaticGetterCallback = Napi::Value (*)(const CallbackInfo& info);
|
|
1876
|
+
using StaticSetterCallback = void (*)(const CallbackInfo& info,
|
|
1877
|
+
const Napi::Value& value);
|
|
1797
1878
|
|
|
1798
|
-
|
|
1879
|
+
using PropertyDescriptor = ClassPropertyDescriptor<T>;
|
|
1799
1880
|
|
|
1800
1881
|
static Function DefineClass(Napi::Env env,
|
|
1801
1882
|
const char* utf8name,
|
|
@@ -1878,21 +1959,29 @@ namespace Napi {
|
|
|
1878
1959
|
const napi_property_descriptor* props,
|
|
1879
1960
|
void* data = nullptr);
|
|
1880
1961
|
|
|
1881
|
-
|
|
1882
|
-
|
|
1962
|
+
using StaticVoidMethodCallbackData =
|
|
1963
|
+
MethodCallbackData<T, StaticVoidMethodCallback>;
|
|
1964
|
+
using StaticMethodCallbackData =
|
|
1965
|
+
MethodCallbackData<T, StaticMethodCallback>;
|
|
1883
1966
|
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
StaticSetterCallback> StaticAccessorCallbackData;
|
|
1967
|
+
using StaticAccessorCallbackData =
|
|
1968
|
+
AccessorCallbackData<T, StaticGetterCallback, StaticSetterCallback>;
|
|
1887
1969
|
|
|
1888
1970
|
template <StaticSetterCallback method>
|
|
1889
|
-
static napi_value WrappedMethod(napi_env env,
|
|
1971
|
+
static napi_value WrappedMethod(napi_env env,
|
|
1972
|
+
napi_callback_info info) NAPI_NOEXCEPT;
|
|
1890
1973
|
|
|
1891
1974
|
template <StaticSetterCallback setter> struct StaticSetterTag {};
|
|
1892
1975
|
|
|
1893
1976
|
template <StaticSetterCallback setter>
|
|
1894
|
-
static napi_callback WrapStaticSetter(StaticSetterTag<setter>)
|
|
1895
|
-
|
|
1977
|
+
static napi_callback WrapStaticSetter(StaticSetterTag<setter>)
|
|
1978
|
+
NAPI_NOEXCEPT {
|
|
1979
|
+
return &This::WrappedMethod<setter>;
|
|
1980
|
+
}
|
|
1981
|
+
static napi_callback WrapStaticSetter(StaticSetterTag<nullptr>)
|
|
1982
|
+
NAPI_NOEXCEPT {
|
|
1983
|
+
return nullptr;
|
|
1984
|
+
}
|
|
1896
1985
|
|
|
1897
1986
|
bool _construction_failed = true;
|
|
1898
1987
|
};
|
|
@@ -2258,10 +2347,10 @@ namespace Napi {
|
|
|
2258
2347
|
class TypedThreadSafeFunction {
|
|
2259
2348
|
public:
|
|
2260
2349
|
// This API may only be called from the main thread.
|
|
2261
|
-
// Helper function that returns nullptr if running
|
|
2350
|
+
// Helper function that returns nullptr if running Node-API 5+, otherwise a
|
|
2262
2351
|
// non-empty, no-op Function. This provides the ability to specify at
|
|
2263
2352
|
// compile-time a callback parameter to `New` that safely does no action
|
|
2264
|
-
// when targeting _any_
|
|
2353
|
+
// when targeting _any_ Node-API version.
|
|
2265
2354
|
#if NAPI_VERSION > 4
|
|
2266
2355
|
static std::nullptr_t EmptyFunctionFactory(Napi::Env env);
|
|
2267
2356
|
#else
|
|
@@ -2619,7 +2708,7 @@ namespace Napi {
|
|
|
2619
2708
|
static T* Unwrap(Object wrapper);
|
|
2620
2709
|
|
|
2621
2710
|
protected:
|
|
2622
|
-
|
|
2711
|
+
using AddonProp = ClassPropertyDescriptor<T>;
|
|
2623
2712
|
void DefineAddon(Object exports,
|
|
2624
2713
|
const std::initializer_list<AddonProp>& props);
|
|
2625
2714
|
Napi::Object DefineProperties(Object object,
|