node-addon-api 3.0.1 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/CHANGELOG.md +217 -0
  2. package/README.md +104 -60
  3. package/common.gypi +1 -1
  4. package/index.js +4 -3
  5. package/napi-inl.deprecated.h +8 -8
  6. package/napi-inl.h +1206 -540
  7. package/napi.h +821 -522
  8. package/package-support.json +21 -0
  9. package/package.json +80 -4
  10. package/tools/README.md +8 -2
  11. package/tools/clang-format.js +67 -0
  12. package/tools/conversion.js +4 -4
  13. package/.editorconfig +0 -8
  14. package/.travis.yml +0 -59
  15. package/CODE_OF_CONDUCT.md +0 -4
  16. package/CONTRIBUTING.md +0 -66
  17. package/appveyor.yml +0 -48
  18. package/benchmark/README.md +0 -47
  19. package/benchmark/binding.gyp +0 -25
  20. package/benchmark/function_args.cc +0 -153
  21. package/benchmark/function_args.js +0 -52
  22. package/benchmark/index.js +0 -34
  23. package/benchmark/property_descriptor.cc +0 -60
  24. package/benchmark/property_descriptor.js +0 -29
  25. package/doc/Doxyfile +0 -2450
  26. package/doc/array_buffer.md +0 -129
  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 -456
  31. package/doc/basic_types.md +0 -423
  32. package/doc/bigint.md +0 -93
  33. package/doc/boolean.md +0 -64
  34. package/doc/buffer.md +0 -140
  35. package/doc/callback_scope.md +0 -54
  36. package/doc/callbackinfo.md +0 -97
  37. package/doc/checker-tool.md +0 -32
  38. package/doc/class_property_descriptor.md +0 -117
  39. package/doc/cmake-js.md +0 -68
  40. package/doc/conversion-tool.md +0 -28
  41. package/doc/creating_a_release.md +0 -62
  42. package/doc/dataview.md +0 -244
  43. package/doc/date.md +0 -68
  44. package/doc/env.md +0 -132
  45. package/doc/error.md +0 -115
  46. package/doc/error_handling.md +0 -186
  47. package/doc/escapable_handle_scope.md +0 -82
  48. package/doc/external.md +0 -59
  49. package/doc/function.md +0 -401
  50. package/doc/function_reference.md +0 -238
  51. package/doc/generator.md +0 -13
  52. package/doc/handle_scope.md +0 -65
  53. package/doc/memory_management.md +0 -27
  54. package/doc/node-gyp.md +0 -82
  55. package/doc/number.md +0 -163
  56. package/doc/object.md +0 -275
  57. package/doc/object_lifetime_management.md +0 -83
  58. package/doc/object_reference.md +0 -117
  59. package/doc/object_wrap.md +0 -851
  60. package/doc/prebuild_tools.md +0 -16
  61. package/doc/promises.md +0 -74
  62. package/doc/property_descriptor.md +0 -286
  63. package/doc/range_error.md +0 -59
  64. package/doc/reference.md +0 -111
  65. package/doc/setup.md +0 -81
  66. package/doc/string.md +0 -89
  67. package/doc/symbol.md +0 -44
  68. package/doc/threadsafe_function.md +0 -320
  69. package/doc/type_error.md +0 -59
  70. package/doc/typed_array.md +0 -74
  71. package/doc/typed_array_of.md +0 -133
  72. package/doc/value.md +0 -278
  73. package/doc/version_management.md +0 -43
  74. package/doc/working_with_javascript_values.md +0 -14
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,66 @@ 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.
874
+
875
+ #if NAPI_VERSION >= 7
876
+ bool IsDetached() const;
877
+ void Detach();
878
+ #endif // NAPI_VERSION >= 7
825
879
  };
826
880
 
827
881
  /// A JavaScript typed-array value with unknown array type.
@@ -836,7 +890,8 @@ namespace Napi {
836
890
  class TypedArray : public Object {
837
891
  public:
838
892
  TypedArray(); ///< Creates a new _empty_ TypedArray instance.
839
- 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.
840
895
 
841
896
  napi_typedarray_type TypedArrayType() const; ///< Gets the type of this typed-array.
842
897
  Napi::ArrayBuffer ArrayBuffer() const; ///< Gets the backing array buffer.
@@ -891,16 +946,19 @@ namespace Napi {
891
946
  /// parameter T), except when creating a "clamped" array:
892
947
  ///
893
948
  /// Uint8Array::New(env, length, napi_uint8_clamped_array)
894
- static TypedArrayOf New(
895
- napi_env env, ///< N-API environment
896
- 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
897
953
  #if defined(NAPI_HAS_CONSTEXPR)
898
- napi_typedarray_type type = TypedArray::TypedArrayTypeForPrimitiveType<T>()
954
+ napi_typedarray_type type =
955
+ TypedArray::TypedArrayTypeForPrimitiveType<T>()
899
956
  #else
900
- napi_typedarray_type type
957
+ napi_typedarray_type type
901
958
  #endif
902
- ///< Type of array, if different from the default array type for the template parameter T.
903
- );
959
+ ///< Type of array, if different from the default array type for the
960
+ ///< template parameter T.
961
+ );
904
962
 
905
963
  /// Creates a new TypedArray instance over a provided array buffer.
906
964
  ///
@@ -908,21 +966,26 @@ namespace Napi {
908
966
  /// parameter T), except when creating a "clamped" array:
909
967
  ///
910
968
  /// Uint8Array::New(env, length, buffer, 0, napi_uint8_clamped_array)
911
- static TypedArrayOf New(
912
- napi_env env, ///< N-API environment
913
- size_t elementLength, ///< Length of the created array, as a number of elements
914
- Napi::ArrayBuffer arrayBuffer, ///< Backing array buffer instance to use
915
- 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
916
976
  #if defined(NAPI_HAS_CONSTEXPR)
917
- napi_typedarray_type type = TypedArray::TypedArrayTypeForPrimitiveType<T>()
977
+ napi_typedarray_type type =
978
+ TypedArray::TypedArrayTypeForPrimitiveType<T>()
918
979
  #else
919
- napi_typedarray_type type
980
+ napi_typedarray_type type
920
981
  #endif
921
- ///< Type of array, if different from the default array type for the template parameter T.
922
- );
982
+ ///< Type of array, if different from the default array type for the
983
+ ///< template parameter T.
984
+ );
923
985
 
924
986
  TypedArrayOf(); ///< Creates a new _empty_ TypedArrayOf instance.
925
- 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.
926
989
 
927
990
  T& operator [](size_t index); ///< Gets or sets an element in the array.
928
991
  const T& operator [](size_t index) const; ///< Gets an element in the array.
@@ -964,7 +1027,8 @@ namespace Napi {
964
1027
  size_t byteLength);
965
1028
 
966
1029
  DataView(); ///< Creates a new _empty_ DataView instance.
967
- 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.
968
1032
 
969
1033
  Napi::ArrayBuffer ArrayBuffer() const; ///< Gets the backing array buffer.
970
1034
  size_t ByteOffset() const; ///< Gets the offset into the buffer where the array starts.
@@ -1003,70 +1067,71 @@ namespace Napi {
1003
1067
 
1004
1068
  class Function : public Object {
1005
1069
  public:
1006
- typedef void (*VoidCallback)(const CallbackInfo& info);
1007
- typedef Value (*Callback)(const CallbackInfo& info);
1008
-
1009
- template <VoidCallback cb>
1010
- static Function New(napi_env env,
1011
- const char* utf8name = nullptr,
1012
- void* data = nullptr);
1013
-
1014
- template <Callback cb>
1015
- static Function New(napi_env env,
1016
- const char* utf8name = nullptr,
1017
- void* data = nullptr);
1018
-
1019
- template <VoidCallback cb>
1020
- static Function New(napi_env env,
1021
- const std::string& utf8name,
1022
- void* data = nullptr);
1023
-
1024
- template <Callback cb>
1025
- static Function New(napi_env env,
1026
- const std::string& utf8name,
1027
- void* data = nullptr);
1028
-
1029
- /// Callable must implement operator() accepting a const CallbackInfo&
1030
- /// and return either void or Value.
1031
- template <typename Callable>
1032
- static Function New(napi_env env,
1033
- Callable cb,
1034
- const char* utf8name = nullptr,
1035
- void* data = nullptr);
1036
- /// Callable must implement operator() accepting a const CallbackInfo&
1037
- /// and return either void or Value.
1038
- template <typename Callable>
1039
- static Function New(napi_env env,
1040
- Callable cb,
1041
- const std::string& utf8name,
1042
- void* data = nullptr);
1043
-
1044
- Function();
1045
- Function(napi_env env, napi_value value);
1046
-
1047
- Value operator ()(const std::initializer_list<napi_value>& args) const;
1048
-
1049
- Value Call(const std::initializer_list<napi_value>& args) const;
1050
- Value Call(const std::vector<napi_value>& args) const;
1051
- Value Call(size_t argc, const napi_value* args) const;
1052
- Value Call(napi_value recv, const std::initializer_list<napi_value>& args) const;
1053
- Value Call(napi_value recv, const std::vector<napi_value>& args) const;
1054
- Value Call(napi_value recv, size_t argc, const napi_value* args) const;
1055
-
1056
- Value MakeCallback(napi_value recv,
1057
- const std::initializer_list<napi_value>& args,
1058
- napi_async_context context = nullptr) const;
1059
- Value MakeCallback(napi_value recv,
1060
- const std::vector<napi_value>& args,
1061
- napi_async_context context = nullptr) const;
1062
- Value MakeCallback(napi_value recv,
1063
- size_t argc,
1064
- const napi_value* args,
1065
- napi_async_context context = nullptr) const;
1066
-
1067
- Object New(const std::initializer_list<napi_value>& args) const;
1068
- Object New(const std::vector<napi_value>& args) const;
1069
- 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;
1070
1135
  };
1071
1136
 
1072
1137
  class Promise : public Object {
@@ -1159,9 +1224,11 @@ namespace Napi {
1159
1224
  void Reset();
1160
1225
  void Reset(const T& value, uint32_t refcount = 0);
1161
1226
 
1162
- // Call this on a reference that is declared as static data, to prevent its destructor
1163
- // from running at program shutdown time, which would attempt to reset the reference when
1164
- // 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.
1165
1232
  void SuppressDestruct();
1166
1233
 
1167
1234
  protected:
@@ -1190,26 +1257,26 @@ namespace Napi {
1190
1257
 
1191
1258
  Napi::Value Get(const char* utf8name) const;
1192
1259
  Napi::Value Get(const std::string& utf8name) const;
1193
- void Set(const char* utf8name, napi_value value);
1194
- void Set(const char* utf8name, Napi::Value value);
1195
- void Set(const char* utf8name, const char* utf8value);
1196
- void Set(const char* utf8name, bool boolValue);
1197
- void Set(const char* utf8name, double numberValue);
1198
- void Set(const std::string& utf8name, napi_value value);
1199
- void Set(const std::string& utf8name, Napi::Value value);
1200
- void Set(const std::string& utf8name, std::string& utf8value);
1201
- void Set(const std::string& utf8name, bool boolValue);
1202
- 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);
1203
1270
 
1204
1271
  Napi::Value Get(uint32_t index) const;
1205
- void Set(uint32_t index, const napi_value value);
1206
- void Set(uint32_t index, const Napi::Value value);
1207
- void Set(uint32_t index, const char* utf8value);
1208
- void Set(uint32_t index, const std::string& utf8value);
1209
- void Set(uint32_t index, bool boolValue);
1210
- void Set(uint32_t index, double numberValue);
1211
-
1212
- protected:
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);
1278
+
1279
+ protected:
1213
1280
  ObjectReference(const ObjectReference&);
1214
1281
  };
1215
1282
 
@@ -1258,43 +1325,48 @@ namespace Napi {
1258
1325
  ObjectReference Persistent(Object value);
1259
1326
  FunctionReference Persistent(Function value);
1260
1327
 
1261
- /// A persistent reference to a JavaScript error object. Use of this class depends somewhat
1262
- /// 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.
1263
1330
  ///
1264
1331
  /// ### Handling Errors With C++ Exceptions
1265
1332
  ///
1266
- /// If C++ exceptions are enabled, then the `Error` class extends `std::exception` and enables
1267
- /// 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.
1268
1336
  ///
1269
- /// If a N-API call fails without executing any JavaScript code (for example due to an invalid
1270
- /// argument), then the N-API wrapper automatically converts and throws the error as a C++
1271
- /// exception of type `Napi::Error`. Or if a JavaScript function called by C++ code via N-API
1272
- /// throws a JavaScript exception, then the N-API wrapper automatically converts and throws it as
1273
- /// 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`.
1274
1343
  ///
1275
- /// If a C++ exception of type `Napi::Error` escapes from a N-API C++ callback, then the N-API
1276
- /// wrapper automatically converts and throws it as a JavaScript exception. Therefore, catching
1277
- /// 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.
1278
1348
  ///
1279
1349
  /// #### Example 1A - Throwing a C++ exception:
1280
1350
  ///
1281
1351
  /// Napi::Env env = ...
1282
1352
  /// throw Napi::Error::New(env, "Example exception");
1283
1353
  ///
1284
- /// Following C++ statements will not be executed. The exception will bubble up as a C++
1285
- /// exception of type `Napi::Error`, until it is either caught while still in C++, or else
1286
- /// 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.
1287
1358
  ///
1288
- /// #### Example 2A - Propagating a N-API C++ exception:
1359
+ /// #### Example 2A - Propagating a Node-API C++ exception:
1289
1360
  ///
1290
1361
  /// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
1291
1362
  /// Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
1292
1363
  ///
1293
- /// Following C++ statements will not be executed. The exception will bubble up as a C++
1294
- /// exception of type `Napi::Error`, until it is either caught while still in C++, or else
1295
- /// 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.
1296
1368
  ///
1297
- /// #### Example 3A - Handling a N-API C++ exception:
1369
+ /// #### Example 3A - Handling a Node-API C++ exception:
1298
1370
  ///
1299
1371
  /// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
1300
1372
  /// Napi::Value result;
@@ -1304,38 +1376,42 @@ namespace Napi {
1304
1376
  /// cerr << "Caught JavaScript exception: " + e.what();
1305
1377
  /// }
1306
1378
  ///
1307
- /// 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.
1308
1381
  ///
1309
1382
  /// ### Handling Errors Without C++ Exceptions
1310
1383
  ///
1311
- /// If C++ exceptions are disabled (by defining `NAPI_DISABLE_CPP_EXCEPTIONS`) then this class
1312
- /// does not extend `std::exception`, and APIs in the `Napi` namespace do not throw C++
1313
- /// exceptions when they fail. Instead, they raise _pending_ JavaScript exceptions and
1314
- /// return _empty_ `Value`s. Calling code should check `Value::IsEmpty()` before attempting
1315
- /// to use a returned value, and may use methods on the `Env` class to check for, get, and
1316
- /// clear a pending JavaScript exception. If the pending exception is not cleared, it will
1317
- /// 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.
1318
1392
  ///
1319
1393
  /// #### Example 1B - Throwing a JS exception
1320
1394
  ///
1321
1395
  /// Napi::Env env = ...
1322
- /// Napi::Error::New(env, "Example exception").ThrowAsJavaScriptException();
1323
- /// return;
1396
+ /// Napi::Error::New(env, "Example
1397
+ /// exception").ThrowAsJavaScriptException(); return;
1324
1398
  ///
1325
- /// After throwing a JS exception, the code should generally return immediately from the native
1326
- /// 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.
1327
1402
  ///
1328
- /// #### Example 2B - Propagating a N-API JS exception:
1403
+ /// #### Example 2B - Propagating a Node-API JS exception:
1329
1404
  ///
1330
1405
  /// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
1331
1406
  /// Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
1332
1407
  /// if (result.IsEmpty()) return;
1333
1408
  ///
1334
- /// An empty value result from a N-API call indicates an error occurred, and a JavaScript
1335
- /// exception is pending. To let the exception propagate, the code should generally return
1336
- /// 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.
1337
1413
  ///
1338
- /// #### Example 3B - Handling a N-API JS exception:
1414
+ /// #### Example 3B - Handling a Node-API JS exception:
1339
1415
  ///
1340
1416
  /// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
1341
1417
  /// Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
@@ -1344,8 +1420,8 @@ namespace Napi {
1344
1420
  /// cerr << "Caught JavaScript exception: " + e.Message();
1345
1421
  /// }
1346
1422
  ///
1347
- /// Since the exception was cleared here, it will not be propagated as a JavaScript exception
1348
- /// 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.
1349
1425
  class Error : public ObjectReference
1350
1426
  #ifdef NAPI_CPP_EXCEPTIONS
1351
1427
  , public std::exception
@@ -1376,14 +1452,17 @@ namespace Napi {
1376
1452
 
1377
1453
  protected:
1378
1454
  /// !cond INTERNAL
1379
- typedef napi_status (*create_error_fn)(napi_env envb, napi_value code, napi_value msg, napi_value* result);
1380
-
1381
- template <typename TError>
1382
- static TError New(napi_env env,
1383
- const char* message,
1384
- size_t length,
1385
- create_error_fn create_error);
1386
- /// !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
1387
1466
 
1388
1467
  private:
1389
1468
  mutable std::string _message;
@@ -1438,8 +1517,8 @@ namespace Napi {
1438
1517
 
1439
1518
  class PropertyDescriptor {
1440
1519
  public:
1441
- typedef Napi::Value (*GetterCallback)(const Napi::CallbackInfo& info);
1442
- typedef void (*SetterCallback)(const Napi::CallbackInfo& info);
1520
+ using GetterCallback = Napi::Value (*)(const Napi::CallbackInfo& info);
1521
+ using SetterCallback = void (*)(const Napi::CallbackInfo& info);
1443
1522
 
1444
1523
  #ifndef NODE_ADDON_API_DISABLE_DEPRECATED
1445
1524
  template <typename Getter>
@@ -1623,10 +1702,6 @@ namespace Napi {
1623
1702
  operator const napi_property_descriptor&() const;
1624
1703
 
1625
1704
  private:
1626
- template <GetterCallback Getter>
1627
- static napi_value GetterCallbackWrapper(napi_env env, napi_callback_info info);
1628
- template <SetterCallback Setter>
1629
- static napi_value SetterCallbackWrapper(napi_env env, napi_callback_info info);
1630
1705
  napi_property_descriptor _desc;
1631
1706
  };
1632
1707
 
@@ -1647,6 +1722,120 @@ namespace Napi {
1647
1722
  napi_property_descriptor _desc;
1648
1723
  };
1649
1724
 
1725
+ template <typename T, typename TCallback>
1726
+ struct MethodCallbackData {
1727
+ TCallback callback;
1728
+ void* data;
1729
+ };
1730
+
1731
+ template <typename T, typename TGetterCallback, typename TSetterCallback>
1732
+ struct AccessorCallbackData {
1733
+ TGetterCallback getterCallback;
1734
+ TSetterCallback setterCallback;
1735
+ void* data;
1736
+ };
1737
+
1738
+ template <typename T>
1739
+ class InstanceWrap {
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);
1746
+
1747
+ using PropertyDescriptor = ClassPropertyDescriptor<T>;
1748
+
1749
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
1750
+ InstanceVoidMethodCallback method,
1751
+ napi_property_attributes attributes = napi_default,
1752
+ void* data = nullptr);
1753
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
1754
+ InstanceMethodCallback method,
1755
+ napi_property_attributes attributes = napi_default,
1756
+ void* data = nullptr);
1757
+ static PropertyDescriptor InstanceMethod(Symbol name,
1758
+ InstanceVoidMethodCallback method,
1759
+ napi_property_attributes attributes = napi_default,
1760
+ void* data = nullptr);
1761
+ static PropertyDescriptor InstanceMethod(Symbol name,
1762
+ InstanceMethodCallback method,
1763
+ napi_property_attributes attributes = napi_default,
1764
+ void* data = nullptr);
1765
+ template <InstanceVoidMethodCallback method>
1766
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
1767
+ napi_property_attributes attributes = napi_default,
1768
+ void* data = nullptr);
1769
+ template <InstanceMethodCallback method>
1770
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
1771
+ napi_property_attributes attributes = napi_default,
1772
+ void* data = nullptr);
1773
+ template <InstanceVoidMethodCallback method>
1774
+ static PropertyDescriptor InstanceMethod(Symbol name,
1775
+ napi_property_attributes attributes = napi_default,
1776
+ void* data = nullptr);
1777
+ template <InstanceMethodCallback method>
1778
+ static PropertyDescriptor InstanceMethod(Symbol name,
1779
+ napi_property_attributes attributes = napi_default,
1780
+ void* data = nullptr);
1781
+ static PropertyDescriptor InstanceAccessor(const char* utf8name,
1782
+ InstanceGetterCallback getter,
1783
+ InstanceSetterCallback setter,
1784
+ napi_property_attributes attributes = napi_default,
1785
+ void* data = nullptr);
1786
+ static PropertyDescriptor InstanceAccessor(Symbol name,
1787
+ InstanceGetterCallback getter,
1788
+ InstanceSetterCallback setter,
1789
+ napi_property_attributes attributes = napi_default,
1790
+ void* data = nullptr);
1791
+ template <InstanceGetterCallback getter, InstanceSetterCallback setter=nullptr>
1792
+ static PropertyDescriptor InstanceAccessor(const char* utf8name,
1793
+ napi_property_attributes attributes = napi_default,
1794
+ void* data = nullptr);
1795
+ template <InstanceGetterCallback getter, InstanceSetterCallback setter=nullptr>
1796
+ static PropertyDescriptor InstanceAccessor(Symbol name,
1797
+ napi_property_attributes attributes = napi_default,
1798
+ void* data = nullptr);
1799
+ static PropertyDescriptor InstanceValue(const char* utf8name,
1800
+ Napi::Value value,
1801
+ napi_property_attributes attributes = napi_default);
1802
+ static PropertyDescriptor InstanceValue(Symbol name,
1803
+ Napi::Value value,
1804
+ napi_property_attributes attributes = napi_default);
1805
+
1806
+ protected:
1807
+ static void AttachPropData(napi_env env, napi_value value, const napi_property_descriptor* prop);
1808
+
1809
+ private:
1810
+ using This = InstanceWrap<T>;
1811
+
1812
+ using InstanceVoidMethodCallbackData =
1813
+ MethodCallbackData<T, InstanceVoidMethodCallback>;
1814
+ using InstanceMethodCallbackData =
1815
+ MethodCallbackData<T, InstanceMethodCallback>;
1816
+ using InstanceAccessorCallbackData =
1817
+ AccessorCallbackData<T, InstanceGetterCallback, InstanceSetterCallback>;
1818
+
1819
+ static napi_value InstanceVoidMethodCallbackWrapper(napi_env env, napi_callback_info info);
1820
+ static napi_value InstanceMethodCallbackWrapper(napi_env env, napi_callback_info info);
1821
+ static napi_value InstanceGetterCallbackWrapper(napi_env env, napi_callback_info info);
1822
+ static napi_value InstanceSetterCallbackWrapper(napi_env env, napi_callback_info info);
1823
+
1824
+ template <InstanceSetterCallback method>
1825
+ static napi_value WrappedMethod(napi_env env,
1826
+ napi_callback_info info) NAPI_NOEXCEPT;
1827
+
1828
+ template <InstanceSetterCallback setter> struct SetterTag {};
1829
+
1830
+ template <InstanceSetterCallback setter>
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
+ }
1837
+ };
1838
+
1650
1839
  /// Base class to be extended by C++ classes exposed to JavaScript; each C++ class instance gets
1651
1840
  /// "wrapped" by a JavaScript object that is managed by this class.
1652
1841
  ///
@@ -1673,7 +1862,7 @@ namespace Napi {
1673
1862
  /// Napi::Value DoSomething(const Napi::CallbackInfo& info);
1674
1863
  /// }
1675
1864
  template <typename T>
1676
- class ObjectWrap : public Reference<Object> {
1865
+ class ObjectWrap : public InstanceWrap<T>, public Reference<Object> {
1677
1866
  public:
1678
1867
  ObjectWrap(const CallbackInfo& callbackInfo);
1679
1868
  virtual ~ObjectWrap();
@@ -1681,16 +1870,13 @@ namespace Napi {
1681
1870
  static T* Unwrap(Object wrapper);
1682
1871
 
1683
1872
  // Methods exposed to JavaScript must conform to one of these callback signatures.
1684
- typedef void (*StaticVoidMethodCallback)(const CallbackInfo& info);
1685
- typedef Napi::Value (*StaticMethodCallback)(const CallbackInfo& info);
1686
- typedef Napi::Value (*StaticGetterCallback)(const CallbackInfo& info);
1687
- typedef void (*StaticSetterCallback)(const CallbackInfo& info, const Napi::Value& value);
1688
- typedef void (T::*InstanceVoidMethodCallback)(const CallbackInfo& info);
1689
- typedef Napi::Value (T::*InstanceMethodCallback)(const CallbackInfo& info);
1690
- typedef Napi::Value (T::*InstanceGetterCallback)(const CallbackInfo& info);
1691
- typedef void (T::*InstanceSetterCallback)(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);
1692
1878
 
1693
- typedef ClassPropertyDescriptor<T> PropertyDescriptor;
1879
+ using PropertyDescriptor = ClassPropertyDescriptor<T>;
1694
1880
 
1695
1881
  static Function DefineClass(Napi::Env env,
1696
1882
  const char* utf8name,
@@ -1750,68 +1936,12 @@ namespace Napi {
1750
1936
  static PropertyDescriptor StaticAccessor(Symbol name,
1751
1937
  napi_property_attributes attributes = napi_default,
1752
1938
  void* data = nullptr);
1753
- static PropertyDescriptor InstanceMethod(const char* utf8name,
1754
- InstanceVoidMethodCallback method,
1755
- napi_property_attributes attributes = napi_default,
1756
- void* data = nullptr);
1757
- static PropertyDescriptor InstanceMethod(const char* utf8name,
1758
- InstanceMethodCallback method,
1759
- napi_property_attributes attributes = napi_default,
1760
- void* data = nullptr);
1761
- static PropertyDescriptor InstanceMethod(Symbol name,
1762
- InstanceVoidMethodCallback method,
1763
- napi_property_attributes attributes = napi_default,
1764
- void* data = nullptr);
1765
- static PropertyDescriptor InstanceMethod(Symbol name,
1766
- InstanceMethodCallback method,
1767
- napi_property_attributes attributes = napi_default,
1768
- void* data = nullptr);
1769
- template <InstanceVoidMethodCallback method>
1770
- static PropertyDescriptor InstanceMethod(const char* utf8name,
1771
- napi_property_attributes attributes = napi_default,
1772
- void* data = nullptr);
1773
- template <InstanceMethodCallback method>
1774
- static PropertyDescriptor InstanceMethod(const char* utf8name,
1775
- napi_property_attributes attributes = napi_default,
1776
- void* data = nullptr);
1777
- template <InstanceVoidMethodCallback method>
1778
- static PropertyDescriptor InstanceMethod(Symbol name,
1779
- napi_property_attributes attributes = napi_default,
1780
- void* data = nullptr);
1781
- template <InstanceMethodCallback method>
1782
- static PropertyDescriptor InstanceMethod(Symbol name,
1783
- napi_property_attributes attributes = napi_default,
1784
- void* data = nullptr);
1785
- static PropertyDescriptor InstanceAccessor(const char* utf8name,
1786
- InstanceGetterCallback getter,
1787
- InstanceSetterCallback setter,
1788
- napi_property_attributes attributes = napi_default,
1789
- void* data = nullptr);
1790
- static PropertyDescriptor InstanceAccessor(Symbol name,
1791
- InstanceGetterCallback getter,
1792
- InstanceSetterCallback setter,
1793
- napi_property_attributes attributes = napi_default,
1794
- void* data = nullptr);
1795
- template <InstanceGetterCallback getter, InstanceSetterCallback setter=nullptr>
1796
- static PropertyDescriptor InstanceAccessor(const char* utf8name,
1797
- napi_property_attributes attributes = napi_default,
1798
- void* data = nullptr);
1799
- template <InstanceGetterCallback getter, InstanceSetterCallback setter=nullptr>
1800
- static PropertyDescriptor InstanceAccessor(Symbol name,
1801
- napi_property_attributes attributes = napi_default,
1802
- void* data = nullptr);
1803
1939
  static PropertyDescriptor StaticValue(const char* utf8name,
1804
1940
  Napi::Value value,
1805
1941
  napi_property_attributes attributes = napi_default);
1806
1942
  static PropertyDescriptor StaticValue(Symbol name,
1807
1943
  Napi::Value value,
1808
1944
  napi_property_attributes attributes = napi_default);
1809
- static PropertyDescriptor InstanceValue(const char* utf8name,
1810
- Napi::Value value,
1811
- napi_property_attributes attributes = napi_default);
1812
- static PropertyDescriptor InstanceValue(Symbol name,
1813
- Napi::Value value,
1814
- napi_property_attributes attributes = napi_default);
1815
1945
  virtual void Finalize(Napi::Env env);
1816
1946
 
1817
1947
  private:
@@ -1822,10 +1952,6 @@ namespace Napi {
1822
1952
  static napi_value StaticMethodCallbackWrapper(napi_env env, napi_callback_info info);
1823
1953
  static napi_value StaticGetterCallbackWrapper(napi_env env, napi_callback_info info);
1824
1954
  static napi_value StaticSetterCallbackWrapper(napi_env env, napi_callback_info info);
1825
- static napi_value InstanceVoidMethodCallbackWrapper(napi_env env, napi_callback_info info);
1826
- static napi_value InstanceMethodCallbackWrapper(napi_env env, napi_callback_info info);
1827
- static napi_value InstanceGetterCallbackWrapper(napi_env env, napi_callback_info info);
1828
- static napi_value InstanceSetterCallbackWrapper(napi_env env, napi_callback_info info);
1829
1955
  static void FinalizeCallback(napi_env env, void* data, void* hint);
1830
1956
  static Function DefineClass(Napi::Env env,
1831
1957
  const char* utf8name,
@@ -1833,65 +1959,29 @@ namespace Napi {
1833
1959
  const napi_property_descriptor* props,
1834
1960
  void* data = nullptr);
1835
1961
 
1836
- template <typename TCallback>
1837
- struct MethodCallbackData {
1838
- TCallback callback;
1839
- void* data;
1840
- };
1841
- typedef MethodCallbackData<StaticVoidMethodCallback> StaticVoidMethodCallbackData;
1842
- typedef MethodCallbackData<StaticMethodCallback> StaticMethodCallbackData;
1843
- typedef MethodCallbackData<InstanceVoidMethodCallback> InstanceVoidMethodCallbackData;
1844
- typedef MethodCallbackData<InstanceMethodCallback> InstanceMethodCallbackData;
1845
-
1846
- template <typename TGetterCallback, typename TSetterCallback>
1847
- struct AccessorCallbackData {
1848
- TGetterCallback getterCallback;
1849
- TSetterCallback setterCallback;
1850
- void* data;
1851
- };
1852
- typedef AccessorCallbackData<StaticGetterCallback, StaticSetterCallback>
1853
- StaticAccessorCallbackData;
1854
- typedef AccessorCallbackData<InstanceGetterCallback, InstanceSetterCallback>
1855
- InstanceAccessorCallbackData;
1962
+ using StaticVoidMethodCallbackData =
1963
+ MethodCallbackData<T, StaticVoidMethodCallback>;
1964
+ using StaticMethodCallbackData =
1965
+ MethodCallbackData<T, StaticMethodCallback>;
1856
1966
 
1857
- template <StaticVoidMethodCallback method>
1858
- static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1859
-
1860
- template <StaticMethodCallback method>
1861
- static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1862
-
1863
- template <InstanceVoidMethodCallback method>
1864
- static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1865
-
1866
- template <InstanceMethodCallback method>
1867
- static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1967
+ using StaticAccessorCallbackData =
1968
+ AccessorCallbackData<T, StaticGetterCallback, StaticSetterCallback>;
1868
1969
 
1869
1970
  template <StaticSetterCallback method>
1870
- static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1871
-
1872
- template <InstanceSetterCallback method>
1873
- static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1874
-
1875
- template <StaticGetterCallback getter> struct StaticGetterTag {};
1876
- template <StaticSetterCallback setter> struct StaticSetterTag {};
1877
- template <InstanceGetterCallback getter> struct GetterTag {};
1878
- template <InstanceSetterCallback setter> struct SetterTag {};
1971
+ static napi_value WrappedMethod(napi_env env,
1972
+ napi_callback_info info) NAPI_NOEXCEPT;
1879
1973
 
1880
- template <StaticGetterCallback getter>
1881
- static napi_callback WrapStaticGetter(StaticGetterTag<getter>) noexcept { return &This::WrappedMethod<getter>; }
1882
- static napi_callback WrapStaticGetter(StaticGetterTag<nullptr>) noexcept { return nullptr; }
1974
+ template <StaticSetterCallback setter> struct StaticSetterTag {};
1883
1975
 
1884
1976
  template <StaticSetterCallback setter>
1885
- static napi_callback WrapStaticSetter(StaticSetterTag<setter>) noexcept { return &This::WrappedMethod<setter>; }
1886
- static napi_callback WrapStaticSetter(StaticSetterTag<nullptr>) noexcept { return nullptr; }
1887
-
1888
- template <InstanceGetterCallback getter>
1889
- static napi_callback WrapGetter(GetterTag<getter>) noexcept { return &This::WrappedMethod<getter>; }
1890
- static napi_callback WrapGetter(GetterTag<nullptr>) noexcept { return nullptr; }
1891
-
1892
- template <InstanceSetterCallback setter>
1893
- static napi_callback WrapSetter(SetterTag<setter>) noexcept { return &This::WrappedMethod<setter>; }
1894
- static napi_callback WrapSetter(SetterTag<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
+ }
1895
1985
 
1896
1986
  bool _construction_failed = true;
1897
1987
  };
@@ -2248,6 +2338,196 @@ namespace Napi {
2248
2338
  napi_threadsafe_function _tsfn;
2249
2339
  };
2250
2340
 
2341
+ // A TypedThreadSafeFunction by default has no context (nullptr) and can
2342
+ // accept any type (void) to its CallJs.
2343
+ template <typename ContextType = std::nullptr_t,
2344
+ typename DataType = void,
2345
+ void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*) =
2346
+ nullptr>
2347
+ class TypedThreadSafeFunction {
2348
+ public:
2349
+ // This API may only be called from the main thread.
2350
+ // Helper function that returns nullptr if running Node-API 5+, otherwise a
2351
+ // non-empty, no-op Function. This provides the ability to specify at
2352
+ // compile-time a callback parameter to `New` that safely does no action
2353
+ // when targeting _any_ Node-API version.
2354
+ #if NAPI_VERSION > 4
2355
+ static std::nullptr_t EmptyFunctionFactory(Napi::Env env);
2356
+ #else
2357
+ static Napi::Function EmptyFunctionFactory(Napi::Env env);
2358
+ #endif
2359
+ static Napi::Function FunctionOrEmpty(Napi::Env env,
2360
+ Napi::Function& callback);
2361
+
2362
+ #if NAPI_VERSION > 4
2363
+ // This API may only be called from the main thread.
2364
+ // Creates a new threadsafe function with:
2365
+ // Callback [missing] Resource [missing] Finalizer [missing]
2366
+ template <typename ResourceString>
2367
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2368
+ napi_env env,
2369
+ ResourceString resourceName,
2370
+ size_t maxQueueSize,
2371
+ size_t initialThreadCount,
2372
+ ContextType* context = nullptr);
2373
+
2374
+ // This API may only be called from the main thread.
2375
+ // Creates a new threadsafe function with:
2376
+ // Callback [missing] Resource [passed] Finalizer [missing]
2377
+ template <typename ResourceString>
2378
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2379
+ napi_env env,
2380
+ const Object& resource,
2381
+ ResourceString resourceName,
2382
+ size_t maxQueueSize,
2383
+ size_t initialThreadCount,
2384
+ ContextType* context = nullptr);
2385
+
2386
+ // This API may only be called from the main thread.
2387
+ // Creates a new threadsafe function with:
2388
+ // Callback [missing] Resource [missing] Finalizer [passed]
2389
+ template <typename ResourceString,
2390
+ typename Finalizer,
2391
+ typename FinalizerDataType = void>
2392
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2393
+ napi_env env,
2394
+ ResourceString resourceName,
2395
+ size_t maxQueueSize,
2396
+ size_t initialThreadCount,
2397
+ ContextType* context,
2398
+ Finalizer finalizeCallback,
2399
+ FinalizerDataType* data = nullptr);
2400
+
2401
+ // This API may only be called from the main thread.
2402
+ // Creates a new threadsafe function with:
2403
+ // Callback [missing] Resource [passed] Finalizer [passed]
2404
+ template <typename ResourceString,
2405
+ typename Finalizer,
2406
+ typename FinalizerDataType = void>
2407
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2408
+ napi_env env,
2409
+ const Object& resource,
2410
+ ResourceString resourceName,
2411
+ size_t maxQueueSize,
2412
+ size_t initialThreadCount,
2413
+ ContextType* context,
2414
+ Finalizer finalizeCallback,
2415
+ FinalizerDataType* data = nullptr);
2416
+ #endif
2417
+
2418
+ // This API may only be called from the main thread.
2419
+ // Creates a new threadsafe function with:
2420
+ // Callback [passed] Resource [missing] Finalizer [missing]
2421
+ template <typename ResourceString>
2422
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2423
+ napi_env env,
2424
+ const Function& callback,
2425
+ ResourceString resourceName,
2426
+ size_t maxQueueSize,
2427
+ size_t initialThreadCount,
2428
+ ContextType* context = nullptr);
2429
+
2430
+ // This API may only be called from the main thread.
2431
+ // Creates a new threadsafe function with:
2432
+ // Callback [passed] Resource [passed] Finalizer [missing]
2433
+ template <typename ResourceString>
2434
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2435
+ napi_env env,
2436
+ const Function& callback,
2437
+ const Object& resource,
2438
+ ResourceString resourceName,
2439
+ size_t maxQueueSize,
2440
+ size_t initialThreadCount,
2441
+ ContextType* context = nullptr);
2442
+
2443
+ // This API may only be called from the main thread.
2444
+ // Creates a new threadsafe function with:
2445
+ // Callback [passed] Resource [missing] Finalizer [passed]
2446
+ template <typename ResourceString,
2447
+ typename Finalizer,
2448
+ typename FinalizerDataType = void>
2449
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2450
+ napi_env env,
2451
+ const Function& callback,
2452
+ ResourceString resourceName,
2453
+ size_t maxQueueSize,
2454
+ size_t initialThreadCount,
2455
+ ContextType* context,
2456
+ Finalizer finalizeCallback,
2457
+ FinalizerDataType* data = nullptr);
2458
+
2459
+ // This API may only be called from the main thread.
2460
+ // Creates a new threadsafe function with:
2461
+ // Callback [passed] Resource [passed] Finalizer [passed]
2462
+ template <typename CallbackType,
2463
+ typename ResourceString,
2464
+ typename Finalizer,
2465
+ typename FinalizerDataType>
2466
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2467
+ napi_env env,
2468
+ CallbackType callback,
2469
+ const Object& resource,
2470
+ ResourceString resourceName,
2471
+ size_t maxQueueSize,
2472
+ size_t initialThreadCount,
2473
+ ContextType* context,
2474
+ Finalizer finalizeCallback,
2475
+ FinalizerDataType* data = nullptr);
2476
+
2477
+ TypedThreadSafeFunction<ContextType, DataType, CallJs>();
2478
+ TypedThreadSafeFunction<ContextType, DataType, CallJs>(
2479
+ napi_threadsafe_function tsFunctionValue);
2480
+
2481
+ operator napi_threadsafe_function() const;
2482
+
2483
+ // This API may be called from any thread.
2484
+ napi_status BlockingCall(DataType* data = nullptr) const;
2485
+
2486
+ // This API may be called from any thread.
2487
+ napi_status NonBlockingCall(DataType* data = nullptr) const;
2488
+
2489
+ // This API may only be called from the main thread.
2490
+ void Ref(napi_env env) const;
2491
+
2492
+ // This API may only be called from the main thread.
2493
+ void Unref(napi_env env) const;
2494
+
2495
+ // This API may be called from any thread.
2496
+ napi_status Acquire() const;
2497
+
2498
+ // This API may be called from any thread.
2499
+ napi_status Release();
2500
+
2501
+ // This API may be called from any thread.
2502
+ napi_status Abort();
2503
+
2504
+ // This API may be called from any thread.
2505
+ ContextType* GetContext() const;
2506
+
2507
+ private:
2508
+ template <typename ResourceString,
2509
+ typename Finalizer,
2510
+ typename FinalizerDataType>
2511
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2512
+ napi_env env,
2513
+ const Function& callback,
2514
+ const Object& resource,
2515
+ ResourceString resourceName,
2516
+ size_t maxQueueSize,
2517
+ size_t initialThreadCount,
2518
+ ContextType* context,
2519
+ Finalizer finalizeCallback,
2520
+ FinalizerDataType* data,
2521
+ napi_finalize wrapper);
2522
+
2523
+ static void CallJsInternal(napi_env env,
2524
+ napi_value jsCallback,
2525
+ void* context,
2526
+ void* data);
2527
+
2528
+ protected:
2529
+ napi_threadsafe_function _tsfn;
2530
+ };
2251
2531
  template <typename DataType>
2252
2532
  class AsyncProgressWorkerBase : public AsyncWorker {
2253
2533
  public:
@@ -2420,6 +2700,25 @@ namespace Napi {
2420
2700
  static const napi_node_version* GetNodeVersion(Env env);
2421
2701
  };
2422
2702
 
2703
+ #if NAPI_VERSION > 5
2704
+ template <typename T>
2705
+ class Addon : public InstanceWrap<T> {
2706
+ public:
2707
+ static inline Object Init(Env env, Object exports);
2708
+ static T* Unwrap(Object wrapper);
2709
+
2710
+ protected:
2711
+ using AddonProp = ClassPropertyDescriptor<T>;
2712
+ void DefineAddon(Object exports,
2713
+ const std::initializer_list<AddonProp>& props);
2714
+ Napi::Object DefineProperties(Object object,
2715
+ const std::initializer_list<AddonProp>& props);
2716
+
2717
+ private:
2718
+ Object entry_point_;
2719
+ };
2720
+ #endif // NAPI_VERSION > 5
2721
+
2423
2722
  } // namespace Napi
2424
2723
 
2425
2724
  // Inline implementations of all the above class methods are included here.