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.
Files changed (77) hide show
  1. package/CHANGELOG.md +84 -0
  2. package/README.md +67 -47
  3. package/napi-inl.deprecated.h +8 -8
  4. package/napi-inl.h +118 -80
  5. package/napi.h +513 -424
  6. package/package.json +25 -5
  7. package/tools/clang-format.js +30 -10
  8. package/.clang-format +0 -111
  9. package/.editorconfig +0 -8
  10. package/.github/workflows/ci.yml +0 -55
  11. package/.github/workflows/linter.yml +0 -24
  12. package/.github/workflows/stale.yml +0 -18
  13. package/.travis.yml +0 -58
  14. package/CODE_OF_CONDUCT.md +0 -4
  15. package/CONTRIBUTING.md +0 -66
  16. package/appveyor.yml +0 -37
  17. package/benchmark/README.md +0 -47
  18. package/benchmark/binding.gyp +0 -25
  19. package/benchmark/function_args.cc +0 -217
  20. package/benchmark/function_args.js +0 -60
  21. package/benchmark/index.js +0 -34
  22. package/benchmark/property_descriptor.cc +0 -91
  23. package/benchmark/property_descriptor.js +0 -37
  24. package/doc/addon.md +0 -157
  25. package/doc/array.md +0 -81
  26. package/doc/array_buffer.md +0 -149
  27. package/doc/async_context.md +0 -86
  28. package/doc/async_operations.md +0 -31
  29. package/doc/async_worker.md +0 -427
  30. package/doc/async_worker_variants.md +0 -557
  31. package/doc/bigint.md +0 -97
  32. package/doc/boolean.md +0 -68
  33. package/doc/buffer.md +0 -144
  34. package/doc/callback_scope.md +0 -54
  35. package/doc/callbackinfo.md +0 -97
  36. package/doc/checker-tool.md +0 -32
  37. package/doc/class_property_descriptor.md +0 -117
  38. package/doc/cmake-js.md +0 -68
  39. package/doc/conversion-tool.md +0 -28
  40. package/doc/creating_a_release.md +0 -62
  41. package/doc/dataview.md +0 -248
  42. package/doc/date.md +0 -68
  43. package/doc/env.md +0 -132
  44. package/doc/error.md +0 -120
  45. package/doc/error_handling.md +0 -186
  46. package/doc/escapable_handle_scope.md +0 -82
  47. package/doc/external.md +0 -63
  48. package/doc/function.md +0 -402
  49. package/doc/function_reference.md +0 -238
  50. package/doc/generator.md +0 -13
  51. package/doc/handle_scope.md +0 -65
  52. package/doc/hierarchy.md +0 -91
  53. package/doc/instance_wrap.md +0 -408
  54. package/doc/memory_management.md +0 -27
  55. package/doc/name.md +0 -29
  56. package/doc/node-gyp.md +0 -82
  57. package/doc/number.md +0 -163
  58. package/doc/object.md +0 -279
  59. package/doc/object_lifetime_management.md +0 -83
  60. package/doc/object_reference.md +0 -117
  61. package/doc/object_wrap.md +0 -561
  62. package/doc/prebuild_tools.md +0 -16
  63. package/doc/promises.md +0 -79
  64. package/doc/property_descriptor.md +0 -286
  65. package/doc/range_error.md +0 -59
  66. package/doc/reference.md +0 -111
  67. package/doc/setup.md +0 -81
  68. package/doc/string.md +0 -93
  69. package/doc/symbol.md +0 -48
  70. package/doc/threadsafe.md +0 -121
  71. package/doc/threadsafe_function.md +0 -290
  72. package/doc/type_error.md +0 -59
  73. package/doc/typed_array.md +0 -78
  74. package/doc/typed_array_of.md +0 -137
  75. package/doc/typed_threadsafe_function.md +0 -307
  76. package/doc/value.md +0 -340
  77. 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
- /// N-API C++ Wrapper Classes
109
+ /// Node-API C++ Wrapper Classes
110
110
  ///
111
- /// These classes wrap the "N-API" ABI-stable C APIs for Node.js, providing a
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
- typedef TypedArrayOf<int8_t> Int8Array; ///< Typed-array of signed 8-bit integers
140
- typedef TypedArrayOf<uint8_t> Uint8Array; ///< Typed-array of unsigned 8-bit integers
141
- typedef TypedArrayOf<int16_t> Int16Array; ///< Typed-array of signed 16-bit integers
142
- typedef TypedArrayOf<uint16_t> Uint16Array; ///< Typed-array of unsigned 16-bit integers
143
- typedef TypedArrayOf<int32_t> Int32Array; ///< Typed-array of signed 32-bit integers
144
- typedef TypedArrayOf<uint32_t> Uint32Array; ///< Typed-array of unsigned 32-bit integers
145
- typedef TypedArrayOf<float> Float32Array; ///< Typed-array of 32-bit floating-point values
146
- typedef TypedArrayOf<double> Float64Array; ///< Typed-array of 64-bit floating-point values
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
- typedef TypedArrayOf<int64_t> BigInt64Array; ///< Typed array of signed 64-bit integers
149
- typedef TypedArrayOf<uint64_t> BigUint64Array; ///< Typed array of unsigned 64-bit integers
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 N-API C++ module's registration callback (init) function.
153
- typedef Object (*ModuleRegisterCallback)(Env env, Object exports);
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 N-API values and operations.
168
+ /// Environment for Node-API values and operations.
158
169
  ///
159
- /// All N-API values and operations must be associated with an environment. An environment
160
- /// instance is always provided to callback functions; that environment must then be used for any
161
- /// creation of N-API values or other N-API operations within the callback. (Many methods infer
162
- /// the environment from the `this` instance that the method is called on.)
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, although current
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 N-API environment approximately corresponds to an Isolate.
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, napi_value value); ///< Wraps a N-API value primitive.
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 N-API value primitive.
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
- static Boolean New(
316
- napi_env env, ///< N-API environment
317
- bool value ///< Boolean value
318
- );
329
+ static Boolean New(napi_env env, ///< Node-API environment
330
+ bool value ///< Boolean value
331
+ );
319
332
 
320
- Boolean(); ///< Creates a new _empty_ Boolean instance.
321
- Boolean(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
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
- operator bool() const; ///< Converts a Boolean value to a boolean primitive.
324
- bool Value() const; ///< Converts a Boolean value to a boolean primitive.
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
- static Number New(
331
- napi_env env, ///< N-API environment
332
- double value ///< Number value
333
- );
334
-
335
- Number(); ///< Creates a new _empty_ Number instance.
336
- Number(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
337
-
338
- operator int32_t() const; ///< Converts a Number value to a 32-bit signed integer value.
339
- operator uint32_t() const; ///< Converts a Number value to a 32-bit unsigned integer value.
340
- operator int64_t() const; ///< Converts a Number value to a 64-bit signed integer value.
341
- operator float() const; ///< Converts a Number value to a 32-bit floating-point value.
342
- operator double() const; ///< Converts a Number value to a 64-bit floating-point value.
343
-
344
- int32_t Int32Value() const; ///< Converts a Number value to a 32-bit signed integer value.
345
- uint32_t Uint32Value() const; ///< Converts a Number value to a 32-bit unsigned integer value.
346
- int64_t Int64Value() const; ///< Converts a Number value to a 64-bit signed integer value.
347
- float FloatValue() const; ///< Converts a Number value to a 32-bit floating-point value.
348
- double DoubleValue() const; ///< Converts a Number value to a 64-bit floating-point value.
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
- static BigInt New(
356
- napi_env env, ///< N-API environment
357
- int64_t value ///< Number value
358
- );
359
- static BigInt New(
360
- napi_env env, ///< N-API environment
361
- uint64_t value ///< Number value
362
- );
363
-
364
- /// Creates a new BigInt object using a specified sign bit and a
365
- /// specified list of digits/words.
366
- /// The resulting number is calculated as:
367
- /// (-1)^sign_bit * (words[0] * (2^64)^0 + words[1] * (2^64)^1 + ...)
368
- static BigInt New(
369
- napi_env env, ///< N-API environment
370
- int sign_bit, ///< Sign bit. 1 if negative.
371
- size_t word_count, ///< Number of words in array
372
- const uint64_t* words ///< Array of words
373
- );
374
-
375
- BigInt(); ///< Creates a new _empty_ BigInt instance.
376
- BigInt(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
377
-
378
- int64_t Int64Value(bool* lossless) const; ///< Converts a BigInt value to a 64-bit signed integer value.
379
- uint64_t Uint64Value(bool* lossless) const; ///< Converts a BigInt value to a 64-bit unsigned integer value.
380
-
381
- size_t WordCount() const; ///< The number of 64-bit words needed to store the result of ToWords().
382
-
383
- /// Writes the contents of this BigInt to a specified memory location.
384
- /// `sign_bit` must be provided and will be set to 1 if this BigInt is negative.
385
- /// `*word_count` has to be initialized to the length of the `words` array.
386
- /// Upon return, it will be set to the actual number of words that would
387
- /// be needed to store this BigInt (i.e. the return value of `WordCount()`).
388
- void ToWords(int* sign_bit, size_t* word_count, uint64_t* words);
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
- static Date New(
398
- napi_env env, ///< N-API environment
399
- double value ///< Number value
400
- );
423
+ static Date New(napi_env env, ///< Node-API environment
424
+ double value ///< Number value
425
+ );
401
426
 
402
- Date(); ///< Creates a new _empty_ Date instance.
403
- Date(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
404
- operator double() const; ///< Converts a Date value to double primitive
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
- double ValueOf() const; ///< Converts a Date value to a double primitive.
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, napi_value value); ///< Wraps a N-API value primitive.
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
- static String New(
422
- napi_env env, ///< N-API environment
423
- const std::string& value ///< UTF-8 encoded C++ string
424
- );
425
-
426
- /// Creates a new String value from a UTF-16 encoded C++ string.
427
- static String New(
428
- napi_env env, ///< N-API environment
429
- const std::u16string& value ///< UTF-16 encoded C++ string
430
- );
431
-
432
- /// Creates a new String value from a UTF-8 encoded C string.
433
- static String New(
434
- napi_env env, ///< N-API environment
435
- const char* value ///< UTF-8 encoded null-terminated C string
436
- );
437
-
438
- /// Creates a new String value from a UTF-16 encoded C string.
439
- static String New(
440
- napi_env env, ///< N-API environment
441
- const char16_t* value ///< UTF-16 encoded null-terminated C string
442
- );
443
-
444
- /// Creates a new String value from a UTF-8 encoded C string with specified length.
445
- static String New(
446
- napi_env env, ///< N-API environment
447
- const char* value, ///< UTF-8 encoded C string (not necessarily null-terminated)
448
- size_t length ///< length of the string in bytes
449
- );
450
-
451
- /// Creates a new String value from a UTF-16 encoded C string with specified length.
452
- static String New(
453
- napi_env env, ///< N-API environment
454
- const char16_t* value, ///< UTF-16 encoded C string (not necessarily null-terminated)
455
- size_t length ///< Length of the string in 2-byte code units
456
- );
457
-
458
- /// Creates a new String based on the original object's type.
459
- ///
460
- /// `value` may be any of:
461
- /// - const char* (encoded using UTF-8, null-terminated)
462
- /// - const char16_t* (encoded using UTF-16-LE, null-terminated)
463
- /// - std::string (encoded using UTF-8)
464
- /// - std::u16string
465
- template <typename T>
466
- static String From(napi_env env, const T& value);
467
-
468
- String(); ///< Creates a new _empty_ String instance.
469
- String(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
470
-
471
- operator std::string() const; ///< Converts a String value to a UTF-8 encoded C++ string.
472
- operator std::u16string() const; ///< Converts a String value to a UTF-16 encoded C++ string.
473
- std::string Utf8Value() const; ///< Converts a String value to a UTF-8 encoded C++ string.
474
- std::u16string Utf16Value() const; ///< Converts a String value to a UTF-16 encoded C++ string.
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
- static Symbol New(
482
- napi_env env, ///< N-API environment
483
- const char* description = nullptr ///< Optional UTF-8 encoded null-terminated C string
484
- /// describing the symbol
485
- );
486
-
487
- /// Creates a new Symbol value with a description.
488
- static Symbol New(
489
- napi_env env, ///< N-API environment
490
- const std::string& description ///< UTF-8 encoded C++ string describing the symbol
491
- );
492
-
493
- /// Creates a new Symbol value with a description.
494
- static Symbol New(
495
- napi_env env, ///< N-API environment
496
- String description ///< String value describing the symbol
497
- );
498
-
499
- /// Creates a new Symbol value with a description.
500
- static Symbol New(
501
- napi_env env, ///< N-API environment
502
- napi_value description ///< String value describing the symbol
503
- );
504
-
505
- /// Get a public Symbol (e.g. Symbol.iterator).
506
- static Symbol WellKnown(napi_env, const std::string& name);
507
-
508
- Symbol(); ///< Creates a new _empty_ Symbol instance.
509
- Symbol(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
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, napi_value value); ///< Wraps a N-API value primitive.
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
- void Set(
645
- napi_value key, ///< Property key primitive
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
- void Set(
652
- Value key, ///< Property key
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
- void Set(
659
- const char* utf8name, ///< UTF-8 encoded null-terminated property name
660
- const ValueType& value
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
- void Set(
666
- const std::string& utf8name, ///< UTF-8 encoded property name
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
- void Set(
703
- uint32_t index, ///< Property / element index
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
- void DefineProperty(
716
- const PropertyDescriptor& property ///< Descriptor for the property to be defined
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
- void DefineProperties(
721
- const std::initializer_list<PropertyDescriptor>& properties
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
- void DefineProperties(
727
- const std::vector<PropertyDescriptor>& properties
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
- static ArrayBuffer New(
786
- napi_env env, ///< N-API environment
787
- size_t byteLength ///< Length of the buffer to be allocated, in bytes
788
- );
789
-
790
- /// Creates a new ArrayBuffer instance, using an external buffer with specified byte length.
791
- static ArrayBuffer New(
792
- napi_env env, ///< N-API environment
793
- void* externalData, ///< Pointer to the external buffer to be used by the array
794
- size_t byteLength ///< Length of the external buffer to be used by the array, in bytes
795
- );
796
-
797
- /// Creates a new ArrayBuffer instance, using an external buffer with specified byte length.
798
- template <typename Finalizer>
799
- static ArrayBuffer New(
800
- napi_env env, ///< N-API environment
801
- void* externalData, ///< Pointer to the external buffer to be used by the array
802
- size_t byteLength, ///< Length of the external buffer to be used by the array,
803
- /// in bytes
804
- Finalizer finalizeCallback ///< Function to be called when the array buffer is destroyed;
805
- /// must implement `void operator()(Env env, void* externalData)`
806
- );
807
-
808
- /// Creates a new ArrayBuffer instance, using an external buffer with specified byte length.
809
- template <typename Finalizer, typename Hint>
810
- static ArrayBuffer New(
811
- napi_env env, ///< N-API environment
812
- void* externalData, ///< Pointer to the external buffer to be used by the array
813
- size_t byteLength, ///< Length of the external buffer to be used by the array,
814
- /// in bytes
815
- Finalizer finalizeCallback, ///< Function to be called when the array buffer is destroyed;
816
- /// must implement `void operator()(Env env, void* externalData, Hint* hint)`
817
- Hint* finalizeHint ///< Hint (second parameter) to be passed to the finalize callback
818
- );
819
-
820
- ArrayBuffer(); ///< Creates a new _empty_ ArrayBuffer instance.
821
- ArrayBuffer(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
822
-
823
- void* Data(); ///< Gets a pointer to the data buffer.
824
- size_t ByteLength(); ///< Gets the length of the array buffer in bytes.
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, napi_value value); ///< Wraps a N-API value primitive.
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
- static TypedArrayOf New(
900
- napi_env env, ///< N-API environment
901
- size_t elementLength, ///< Length of the created array, as a number of elements
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
- napi_typedarray_type type = TypedArray::TypedArrayTypeForPrimitiveType<T>()
954
+ napi_typedarray_type type =
955
+ TypedArray::TypedArrayTypeForPrimitiveType<T>()
904
956
  #else
905
- napi_typedarray_type type
957
+ napi_typedarray_type type
906
958
  #endif
907
- ///< Type of array, if different from the default array type for the template parameter T.
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
- static TypedArrayOf New(
917
- napi_env env, ///< N-API environment
918
- size_t elementLength, ///< Length of the created array, as a number of elements
919
- Napi::ArrayBuffer arrayBuffer, ///< Backing array buffer instance to use
920
- size_t bufferOffset, ///< Offset into the array buffer where the typed-array starts
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
- napi_typedarray_type type = TypedArray::TypedArrayTypeForPrimitiveType<T>()
977
+ napi_typedarray_type type =
978
+ TypedArray::TypedArrayTypeForPrimitiveType<T>()
923
979
  #else
924
- napi_typedarray_type type
980
+ napi_typedarray_type type
925
981
  #endif
926
- ///< Type of array, if different from the default array type for the template parameter T.
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, napi_value value); ///< Wraps a N-API value primitive.
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, napi_value value); ///< Wraps a N-API value primitive.
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
- typedef void (*VoidCallback)(const CallbackInfo& info);
1012
- typedef Value (*Callback)(const CallbackInfo& info);
1013
-
1014
- template <VoidCallback cb>
1015
- static Function New(napi_env env,
1016
- const char* utf8name = nullptr,
1017
- void* data = nullptr);
1018
-
1019
- template <Callback cb>
1020
- static Function New(napi_env env,
1021
- const char* utf8name = nullptr,
1022
- void* data = nullptr);
1023
-
1024
- template <VoidCallback cb>
1025
- static Function New(napi_env env,
1026
- const std::string& utf8name,
1027
- void* data = nullptr);
1028
-
1029
- template <Callback cb>
1030
- static Function New(napi_env env,
1031
- const std::string& utf8name,
1032
- void* data = nullptr);
1033
-
1034
- /// Callable must implement operator() accepting a const CallbackInfo&
1035
- /// and return either void or Value.
1036
- template <typename Callable>
1037
- static Function New(napi_env env,
1038
- Callable cb,
1039
- const char* utf8name = nullptr,
1040
- void* data = nullptr);
1041
- /// Callable must implement operator() accepting a const CallbackInfo&
1042
- /// and return either void or Value.
1043
- template <typename Callable>
1044
- static Function New(napi_env env,
1045
- Callable cb,
1046
- const std::string& utf8name,
1047
- void* data = nullptr);
1048
-
1049
- Function();
1050
- Function(napi_env env, napi_value value);
1051
-
1052
- Value operator ()(const std::initializer_list<napi_value>& args) const;
1053
-
1054
- Value Call(const std::initializer_list<napi_value>& args) const;
1055
- Value Call(const std::vector<napi_value>& args) const;
1056
- Value Call(size_t argc, const napi_value* args) const;
1057
- Value Call(napi_value recv, const std::initializer_list<napi_value>& args) const;
1058
- Value Call(napi_value recv, const std::vector<napi_value>& args) const;
1059
- Value Call(napi_value recv, size_t argc, const napi_value* args) const;
1060
-
1061
- Value MakeCallback(napi_value recv,
1062
- const std::initializer_list<napi_value>& args,
1063
- napi_async_context context = nullptr) const;
1064
- Value MakeCallback(napi_value recv,
1065
- const std::vector<napi_value>& args,
1066
- napi_async_context context = nullptr) const;
1067
- Value MakeCallback(napi_value recv,
1068
- size_t argc,
1069
- const napi_value* args,
1070
- napi_async_context context = nullptr) const;
1071
-
1072
- Object New(const std::initializer_list<napi_value>& args) const;
1073
- Object New(const std::vector<napi_value>& args) const;
1074
- Object New(size_t argc, const napi_value* args) const;
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 destructor
1168
- // from running at program shutdown time, which would attempt to reset the reference when
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
- void Set(const char* utf8name, napi_value value);
1199
- void Set(const char* utf8name, Napi::Value value);
1200
- void Set(const char* utf8name, const char* utf8value);
1201
- void Set(const char* utf8name, bool boolValue);
1202
- void Set(const char* utf8name, double numberValue);
1203
- void Set(const std::string& utf8name, napi_value value);
1204
- void Set(const std::string& utf8name, Napi::Value value);
1205
- void Set(const std::string& utf8name, std::string& utf8value);
1206
- void Set(const std::string& utf8name, bool boolValue);
1207
- void Set(const std::string& utf8name, double numberValue);
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
- void Set(uint32_t index, const napi_value value);
1211
- void Set(uint32_t index, const Napi::Value value);
1212
- void Set(uint32_t index, const char* utf8value);
1213
- void Set(uint32_t index, const std::string& utf8value);
1214
- void Set(uint32_t index, bool boolValue);
1215
- void Set(uint32_t index, double numberValue);
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
- protected:
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 depends somewhat
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 `std::exception` and enables
1272
- /// integrated error-handling for C++ exceptions and JavaScript 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 N-API call fails without executing any JavaScript code (for example due to an invalid
1275
- /// argument), then the N-API wrapper automatically converts and throws the error as a C++
1276
- /// exception of type `Napi::Error`. Or if a JavaScript function called by C++ code via N-API
1277
- /// throws a JavaScript exception, then the N-API wrapper automatically converts and throws it as
1278
- /// a C++ exception of type `Napi::Error`.
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 N-API C++ callback, then the N-API
1281
- /// wrapper automatically converts and throws it as a JavaScript exception. Therefore, catching
1282
- /// a C++ exception of type `Napi::Error` prevents a JavaScript exception from being thrown.
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 up as a C++
1290
- /// exception of type `Napi::Error`, until it is either caught while still in C++, or else
1291
- /// automatically propataged as a JavaScript exception when the callback returns to JavaScript.
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 N-API C++ exception:
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 up as a C++
1299
- /// exception of type `Napi::Error`, until it is either caught while still in C++, or else
1300
- /// automatically propagated as a JavaScript exception when the callback returns to JavaScript.
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 N-API C++ exception:
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 JavaScript exception.
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`) then this class
1317
- /// does not extend `std::exception`, and APIs in the `Napi` namespace do not throw C++
1318
- /// exceptions when they fail. Instead, they raise _pending_ JavaScript exceptions and
1319
- /// return _empty_ `Value`s. Calling code should check `Value::IsEmpty()` before attempting
1320
- /// to use a returned value, and may use methods on the `Env` class to check for, get, and
1321
- /// clear a pending JavaScript exception. If the pending exception is not cleared, it will
1322
- /// be thrown when the native callback returns to JavaScript.
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 exception").ThrowAsJavaScriptException();
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 immediately from the native
1331
- /// callback, after performing any necessary cleanup.
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 N-API JS exception:
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 N-API call indicates an error occurred, and a JavaScript
1340
- /// exception is pending. To let the exception propagate, the code should generally return
1341
- /// immediately from the native callback, after performing any necessary cleanup.
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 N-API JS exception:
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 JavaScript exception
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
- typedef napi_status (*create_error_fn)(napi_env envb, napi_value code, napi_value msg, napi_value* result);
1385
-
1386
- template <typename TError>
1387
- static TError New(napi_env env,
1388
- const char* message,
1389
- size_t length,
1390
- create_error_fn create_error);
1391
- /// !endcond
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
- typedef Napi::Value (*GetterCallback)(const Napi::CallbackInfo& info);
1447
- typedef void (*SetterCallback)(const Napi::CallbackInfo& info);
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
- typedef void (T::*InstanceVoidMethodCallback)(const CallbackInfo& info);
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
- typedef MethodCallbackData<T, InstanceVoidMethodCallback> InstanceVoidMethodCallbackData;
1739
- typedef MethodCallbackData<T, InstanceMethodCallback> InstanceMethodCallbackData;
1740
- typedef AccessorCallbackData<T,
1741
- InstanceGetterCallback,
1742
- InstanceSetterCallback> InstanceAccessorCallbackData;
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, napi_callback_info info) noexcept;
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>) noexcept { return &This::WrappedMethod<setter>; }
1756
- static napi_callback WrapSetter(SetterTag<nullptr>) noexcept { return nullptr; }
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
- typedef void (*StaticVoidMethodCallback)(const CallbackInfo& info);
1794
- typedef Napi::Value (*StaticMethodCallback)(const CallbackInfo& info);
1795
- typedef Napi::Value (*StaticGetterCallback)(const CallbackInfo& info);
1796
- typedef void (*StaticSetterCallback)(const CallbackInfo& info, const Napi::Value& value);
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
- typedef ClassPropertyDescriptor<T> PropertyDescriptor;
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
- typedef MethodCallbackData<T, StaticVoidMethodCallback> StaticVoidMethodCallbackData;
1882
- typedef MethodCallbackData<T, StaticMethodCallback> StaticMethodCallbackData;
1962
+ using StaticVoidMethodCallbackData =
1963
+ MethodCallbackData<T, StaticVoidMethodCallback>;
1964
+ using StaticMethodCallbackData =
1965
+ MethodCallbackData<T, StaticMethodCallback>;
1883
1966
 
1884
- typedef AccessorCallbackData<T,
1885
- StaticGetterCallback,
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, napi_callback_info info) noexcept;
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>) noexcept { return &This::WrappedMethod<setter>; }
1895
- static napi_callback WrapStaticSetter(StaticSetterTag<nullptr>) noexcept { return nullptr; }
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 N-API 5+, otherwise a
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_ N-API version.
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
- typedef ClassPropertyDescriptor<T> AddonProp;
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,