koffi 2.3.17 → 2.3.19

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 (68) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/build/2.3.19/koffi_darwin_arm64/koffi.node +0 -0
  3. package/build/{2.3.17 → 2.3.19}/koffi_darwin_x64/koffi.node +0 -0
  4. package/build/2.3.19/koffi_freebsd_arm64/koffi.node +0 -0
  5. package/build/{2.3.17 → 2.3.19}/koffi_freebsd_ia32/koffi.node +0 -0
  6. package/build/{2.3.17 → 2.3.19}/koffi_freebsd_x64/koffi.node +0 -0
  7. package/build/{2.3.17 → 2.3.19}/koffi_linux_arm32hf/koffi.node +0 -0
  8. package/build/{2.3.17 → 2.3.19}/koffi_linux_arm64/koffi.node +0 -0
  9. package/build/{2.3.17 → 2.3.19}/koffi_linux_ia32/koffi.node +0 -0
  10. package/build/{2.3.17 → 2.3.19}/koffi_linux_riscv64hf64/koffi.node +0 -0
  11. package/build/{2.3.17 → 2.3.19}/koffi_linux_x64/koffi.node +0 -0
  12. package/build/{2.3.17 → 2.3.19}/koffi_openbsd_ia32/koffi.node +0 -0
  13. package/build/{2.3.17 → 2.3.19}/koffi_openbsd_x64/koffi.node +0 -0
  14. package/build/2.3.19/koffi_win32_arm64/koffi.node +0 -0
  15. package/build/{2.3.17 → 2.3.19}/koffi_win32_ia32/koffi.node +0 -0
  16. package/build/2.3.19/koffi_win32_x64/koffi.node +0 -0
  17. package/package.json +2 -2
  18. package/src/koffi/src/call.cc +16 -8
  19. package/src/koffi/src/ffi.cc +3 -4
  20. package/src/koffi/src/ffi.hh +2 -0
  21. package/vendor/node-addon-api/CHANGELOG.md +41 -0
  22. package/vendor/node-addon-api/README.md +1 -1
  23. package/vendor/node-addon-api/doc/array_buffer.md +10 -0
  24. package/vendor/node-addon-api/doc/buffer.md +97 -0
  25. package/vendor/node-addon-api/doc/env.md +2 -2
  26. package/vendor/node-addon-api/doc/external.md +2 -2
  27. package/vendor/node-addon-api/doc/external_buffer.md +18 -0
  28. package/vendor/node-addon-api/doc/hierarchy.md +4 -2
  29. package/vendor/node-addon-api/doc/object.md +2 -29
  30. package/vendor/node-addon-api/doc/type_taggable.md +40 -0
  31. package/vendor/node-addon-api/doc/value.md +7 -1
  32. package/vendor/node-addon-api/napi-inl.h +317 -22
  33. package/vendor/node-addon-api/napi.h +84 -7
  34. package/vendor/node-addon-api/package.json +9 -1
  35. package/vendor/node-addon-api/test/async_progress_worker.cc +15 -3
  36. package/vendor/node-addon-api/test/binding.cc +4 -2
  37. package/vendor/node-addon-api/test/binding.gyp +11 -1
  38. package/vendor/node-addon-api/test/buffer.cc +13 -19
  39. package/vendor/node-addon-api/test/buffer.h +26 -0
  40. package/vendor/node-addon-api/test/buffer.js +82 -0
  41. package/vendor/node-addon-api/test/buffer_new_or_copy-inl.h +68 -0
  42. package/vendor/node-addon-api/test/buffer_no_external.cc +24 -0
  43. package/vendor/node-addon-api/test/error.cc +101 -0
  44. package/vendor/node-addon-api/test/error.js +15 -1
  45. package/vendor/node-addon-api/test/index.js +1 -1
  46. package/vendor/node-addon-api/test/object_reference.cc +220 -22
  47. package/vendor/node-addon-api/test/object_reference.js +83 -80
  48. package/vendor/node-addon-api/test/objectwrap.cc +23 -3
  49. package/vendor/node-addon-api/test/objectwrap.js +14 -2
  50. package/vendor/node-addon-api/test/reference.cc +55 -1
  51. package/vendor/node-addon-api/test/reference.js +7 -1
  52. package/vendor/node-addon-api/test/type_taggable.cc +66 -0
  53. package/vendor/node-addon-api/test/type_taggable.js +60 -0
  54. package/vendor/node-addon-api/test/value_type_cast.cc +60 -0
  55. package/vendor/node-addon-api/test/value_type_cast.js +106 -0
  56. package/vendor/node-addon-api/tools/eslint-format.js +2 -2
  57. package/build/2.3.17/koffi_darwin_arm64/koffi.node +0 -0
  58. package/build/2.3.17/koffi_freebsd_arm64/koffi.node +0 -0
  59. package/build/2.3.17/koffi_win32_arm64/koffi.node +0 -0
  60. package/build/2.3.17/koffi_win32_x64/koffi.node +0 -0
  61. package/vendor/node-addon-api/test/object/object_type_tag.cc +0 -39
  62. package/vendor/node-addon-api/test/object/object_type_tag.js +0 -55
  63. /package/build/{2.3.17 → 2.3.19}/koffi_win32_arm64/koffi.exp +0 -0
  64. /package/build/{2.3.17 → 2.3.19}/koffi_win32_arm64/koffi.lib +0 -0
  65. /package/build/{2.3.17 → 2.3.19}/koffi_win32_ia32/koffi.exp +0 -0
  66. /package/build/{2.3.17 → 2.3.19}/koffi_win32_ia32/koffi.lib +0 -0
  67. /package/build/{2.3.17 → 2.3.19}/koffi_win32_x64/koffi.exp +0 -0
  68. /package/build/{2.3.17 → 2.3.19}/koffi_win32_x64/koffi.lib +0 -0
@@ -1,11 +1,22 @@
1
1
  #ifndef SRC_NAPI_H_
2
2
  #define SRC_NAPI_H_
3
3
 
4
+ #ifndef NAPI_HAS_THREADS
5
+ #if !defined(__wasm__) || (defined(__EMSCRIPTEN_PTHREADS__) || \
6
+ (defined(__wasi__) && defined(_REENTRANT)))
7
+ #define NAPI_HAS_THREADS 1
8
+ #else
9
+ #define NAPI_HAS_THREADS 0
10
+ #endif
11
+ #endif
12
+
4
13
  #include <node_api.h>
5
14
  #include <functional>
6
15
  #include <initializer_list>
7
16
  #include <memory>
17
+ #if NAPI_HAS_THREADS
8
18
  #include <mutex>
19
+ #endif // NAPI_HAS_THREADS
9
20
  #include <string>
10
21
  #include <vector>
11
22
 
@@ -452,6 +463,9 @@ class Value {
452
463
  ///
453
464
  /// This conversion does NOT coerce the type. Calling any methods
454
465
  /// inappropriate for the actual value type will throw `Napi::Error`.
466
+ ///
467
+ /// If `NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS` is defined, this method
468
+ /// asserts that the actual type is the expected type.
455
469
  template <typename T>
456
470
  T As() const;
457
471
 
@@ -478,6 +492,8 @@ class Boolean : public Value {
478
492
  bool value ///< Boolean value
479
493
  );
480
494
 
495
+ static void CheckCast(napi_env env, napi_value value);
496
+
481
497
  Boolean(); ///< Creates a new _empty_ Boolean instance.
482
498
  Boolean(napi_env env,
483
499
  napi_value value); ///< Wraps a Node-API value primitive.
@@ -493,6 +509,8 @@ class Number : public Value {
493
509
  double value ///< Number value
494
510
  );
495
511
 
512
+ static void CheckCast(napi_env env, napi_value value);
513
+
496
514
  Number(); ///< Creates a new _empty_ Number instance.
497
515
  Number(napi_env env,
498
516
  napi_value value); ///< Wraps a Node-API value primitive.
@@ -541,6 +559,8 @@ class BigInt : public Value {
541
559
  const uint64_t* words ///< Array of words
542
560
  );
543
561
 
562
+ static void CheckCast(napi_env env, napi_value value);
563
+
544
564
  BigInt(); ///< Creates a new _empty_ BigInt instance.
545
565
  BigInt(napi_env env,
546
566
  napi_value value); ///< Wraps a Node-API value primitive.
@@ -572,6 +592,8 @@ class Date : public Value {
572
592
  double value ///< Number value
573
593
  );
574
594
 
595
+ static void CheckCast(napi_env env, napi_value value);
596
+
575
597
  Date(); ///< Creates a new _empty_ Date instance.
576
598
  Date(napi_env env, napi_value value); ///< Wraps a Node-API value primitive.
577
599
  operator double() const; ///< Converts a Date value to double primitive
@@ -583,6 +605,8 @@ class Date : public Value {
583
605
  /// A JavaScript string or symbol value (that can be used as a property name).
584
606
  class Name : public Value {
585
607
  public:
608
+ static void CheckCast(napi_env env, napi_value value);
609
+
586
610
  Name(); ///< Creates a new _empty_ Name instance.
587
611
  Name(napi_env env,
588
612
  napi_value value); ///< Wraps a Node-API value primitive.
@@ -640,6 +664,8 @@ class String : public Name {
640
664
  template <typename T>
641
665
  static String From(napi_env env, const T& value);
642
666
 
667
+ static void CheckCast(napi_env env, napi_value value);
668
+
643
669
  String(); ///< Creates a new _empty_ String instance.
644
670
  String(napi_env env,
645
671
  napi_value value); ///< Wraps a Node-API value primitive.
@@ -698,13 +724,26 @@ class Symbol : public Name {
698
724
  // Create a symbol in the global registry, napi_value describing the symbol
699
725
  static MaybeOrValue<Symbol> For(napi_env env, napi_value description);
700
726
 
727
+ static void CheckCast(napi_env env, napi_value value);
728
+
701
729
  Symbol(); ///< Creates a new _empty_ Symbol instance.
702
730
  Symbol(napi_env env,
703
731
  napi_value value); ///< Wraps a Node-API value primitive.
704
732
  };
705
733
 
734
+ class TypeTaggable : public Value {
735
+ public:
736
+ #if NAPI_VERSION >= 8
737
+ void TypeTag(const napi_type_tag* type_tag) const;
738
+ bool CheckTypeTag(const napi_type_tag* type_tag) const;
739
+ #endif // NAPI_VERSION >= 8
740
+ protected:
741
+ TypeTaggable();
742
+ TypeTaggable(napi_env env, napi_value value);
743
+ };
744
+
706
745
  /// A JavaScript object value.
707
- class Object : public Value {
746
+ class Object : public TypeTaggable {
708
747
  public:
709
748
  /// Enables property and element assignments using indexing syntax.
710
749
  ///
@@ -745,6 +784,8 @@ class Object : public Value {
745
784
  static Object New(napi_env env ///< Node-API environment
746
785
  );
747
786
 
787
+ static void CheckCast(napi_env env, napi_value value);
788
+
748
789
  Object(); ///< Creates a new _empty_ Object instance.
749
790
  Object(napi_env env,
750
791
  napi_value value); ///< Wraps a Node-API value primitive.
@@ -980,14 +1021,11 @@ class Object : public Value {
980
1021
  /// See
981
1022
  /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof
982
1023
  MaybeOrValue<bool> Seal() const;
983
-
984
- void TypeTag(const napi_type_tag* type_tag) const;
985
- bool CheckTypeTag(const napi_type_tag* type_tag) const;
986
1024
  #endif // NAPI_VERSION >= 8
987
1025
  };
988
1026
 
989
1027
  template <typename T>
990
- class External : public Value {
1028
+ class External : public TypeTaggable {
991
1029
  public:
992
1030
  static External New(napi_env env, T* data);
993
1031
 
@@ -1001,6 +1039,8 @@ class External : public Value {
1001
1039
  Finalizer finalizeCallback,
1002
1040
  Hint* finalizeHint);
1003
1041
 
1042
+ static void CheckCast(napi_env env, napi_value value);
1043
+
1004
1044
  External();
1005
1045
  External(napi_env env, napi_value value);
1006
1046
 
@@ -1012,6 +1052,8 @@ class Array : public Object {
1012
1052
  static Array New(napi_env env);
1013
1053
  static Array New(napi_env env, size_t length);
1014
1054
 
1055
+ static void CheckCast(napi_env env, napi_value value);
1056
+
1015
1057
  Array();
1016
1058
  Array(napi_env env, napi_value value);
1017
1059
 
@@ -1077,6 +1119,7 @@ class ArrayBuffer : public Object {
1077
1119
  size_t byteLength ///< Length of the buffer to be allocated, in bytes
1078
1120
  );
1079
1121
 
1122
+ #ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
1080
1123
  /// Creates a new ArrayBuffer instance, using an external buffer with
1081
1124
  /// specified byte length.
1082
1125
  static ArrayBuffer New(
@@ -1120,6 +1163,9 @@ class ArrayBuffer : public Object {
1120
1163
  Hint* finalizeHint ///< Hint (second parameter) to be passed to the
1121
1164
  ///< finalize callback
1122
1165
  );
1166
+ #endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
1167
+
1168
+ static void CheckCast(napi_env env, napi_value value);
1123
1169
 
1124
1170
  ArrayBuffer(); ///< Creates a new _empty_ ArrayBuffer instance.
1125
1171
  ArrayBuffer(napi_env env,
@@ -1145,6 +1191,8 @@ class ArrayBuffer : public Object {
1145
1191
  /// }
1146
1192
  class TypedArray : public Object {
1147
1193
  public:
1194
+ static void CheckCast(napi_env env, napi_value value);
1195
+
1148
1196
  TypedArray(); ///< Creates a new _empty_ TypedArray instance.
1149
1197
  TypedArray(napi_env env,
1150
1198
  napi_value value); ///< Wraps a Node-API value primitive.
@@ -1245,6 +1293,8 @@ class TypedArrayOf : public TypedArray {
1245
1293
  ///< template parameter T.
1246
1294
  );
1247
1295
 
1296
+ static void CheckCast(napi_env env, napi_value value);
1297
+
1248
1298
  TypedArrayOf(); ///< Creates a new _empty_ TypedArrayOf instance.
1249
1299
  TypedArrayOf(napi_env env,
1250
1300
  napi_value value); ///< Wraps a Node-API value primitive.
@@ -1289,6 +1339,8 @@ class DataView : public Object {
1289
1339
  size_t byteOffset,
1290
1340
  size_t byteLength);
1291
1341
 
1342
+ static void CheckCast(napi_env env, napi_value value);
1343
+
1292
1344
  DataView(); ///< Creates a new _empty_ DataView instance.
1293
1345
  DataView(napi_env env,
1294
1346
  napi_value value); ///< Wraps a Node-API value primitive.
@@ -1369,6 +1421,8 @@ class Function : public Object {
1369
1421
  const std::string& utf8name,
1370
1422
  void* data = nullptr);
1371
1423
 
1424
+ static void CheckCast(napi_env env, napi_value value);
1425
+
1372
1426
  Function();
1373
1427
  Function(napi_env env, napi_value value);
1374
1428
 
@@ -1425,6 +1479,8 @@ class Promise : public Object {
1425
1479
  napi_value _promise;
1426
1480
  };
1427
1481
 
1482
+ static void CheckCast(napi_env env, napi_value value);
1483
+
1428
1484
  Promise(napi_env env, napi_value value);
1429
1485
  };
1430
1486
 
@@ -1432,6 +1488,7 @@ template <typename T>
1432
1488
  class Buffer : public Uint8Array {
1433
1489
  public:
1434
1490
  static Buffer<T> New(napi_env env, size_t length);
1491
+ #ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
1435
1492
  static Buffer<T> New(napi_env env, T* data, size_t length);
1436
1493
 
1437
1494
  // Finalizer must implement `void operator()(Env env, T* data)`.
@@ -1447,9 +1504,27 @@ class Buffer : public Uint8Array {
1447
1504
  size_t length,
1448
1505
  Finalizer finalizeCallback,
1449
1506
  Hint* finalizeHint);
1507
+ #endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
1508
+
1509
+ static Buffer<T> NewOrCopy(napi_env env, T* data, size_t length);
1510
+ // Finalizer must implement `void operator()(Env env, T* data)`.
1511
+ template <typename Finalizer>
1512
+ static Buffer<T> NewOrCopy(napi_env env,
1513
+ T* data,
1514
+ size_t length,
1515
+ Finalizer finalizeCallback);
1516
+ // Finalizer must implement `void operator()(Env env, T* data, Hint* hint)`.
1517
+ template <typename Finalizer, typename Hint>
1518
+ static Buffer<T> NewOrCopy(napi_env env,
1519
+ T* data,
1520
+ size_t length,
1521
+ Finalizer finalizeCallback,
1522
+ Hint* finalizeHint);
1450
1523
 
1451
1524
  static Buffer<T> Copy(napi_env env, const T* data, size_t length);
1452
1525
 
1526
+ static void CheckCast(napi_env env, napi_value value);
1527
+
1453
1528
  Buffer();
1454
1529
  Buffer(napi_env env, napi_value value);
1455
1530
  size_t Length() const;
@@ -2435,6 +2510,7 @@ class AsyncContext {
2435
2510
  napi_async_context _context;
2436
2511
  };
2437
2512
 
2513
+ #if NAPI_HAS_THREADS
2438
2514
  class AsyncWorker {
2439
2515
  public:
2440
2516
  virtual ~AsyncWorker();
@@ -2497,8 +2573,9 @@ class AsyncWorker {
2497
2573
  std::string _error;
2498
2574
  bool _suppress_destruct;
2499
2575
  };
2576
+ #endif // NAPI_HAS_THREADS
2500
2577
 
2501
- #if (NAPI_VERSION > 3 && !defined(__wasm32__))
2578
+ #if (NAPI_VERSION > 3 && NAPI_HAS_THREADS)
2502
2579
  class ThreadSafeFunction {
2503
2580
  public:
2504
2581
  // This API may only be called from the main thread.
@@ -3068,7 +3145,7 @@ class AsyncProgressQueueWorker
3068
3145
  void Signal() const;
3069
3146
  void SendProgress_(const T* data, size_t count);
3070
3147
  };
3071
- #endif // NAPI_VERSION > 3 && !defined(__wasm32__)
3148
+ #endif // NAPI_VERSION > 3 && NAPI_HAS_THREADS
3072
3149
 
3073
3150
  // Memory management.
3074
3151
  class MemoryManagement {
@@ -247,6 +247,10 @@
247
247
  "name": "Mathias Küsel",
248
248
  "url": "https://github.com/mathiask88"
249
249
  },
250
+ {
251
+ "name": "Mathias Stearn",
252
+ "url": "https://github.com/RedBeard0531"
253
+ },
250
254
  {
251
255
  "name": "Matteo Collina",
252
256
  "url": "https://github.com/mcollina"
@@ -355,6 +359,10 @@
355
359
  "name": "todoroff",
356
360
  "url": "https://github.com/todoroff"
357
361
  },
362
+ {
363
+ "name": "Toyo Li",
364
+ "url": "https://github.com/toyobayashi"
365
+ },
358
366
  {
359
367
  "name": "Tux3",
360
368
  "url": "https://github.com/tux3"
@@ -451,6 +459,6 @@
451
459
  "lint:fix": "node tools/clang-format --fix && node tools/eslint-format --fix"
452
460
  },
453
461
  "pre-commit": "lint",
454
- "version": "6.0.0",
462
+ "version": "6.1.0",
455
463
  "support": true
456
464
  }
@@ -34,11 +34,16 @@ class TestWorker : public AsyncProgressWorker<ProgressData> {
34
34
  SetError("test error");
35
35
  }
36
36
  ProgressData data{0};
37
- std::unique_lock<std::mutex> lock(_cvm);
37
+
38
38
  for (int32_t idx = 0; idx < _times; idx++) {
39
39
  data.progress = idx;
40
40
  progress.Send(&data, 1);
41
- _cv.wait(lock);
41
+
42
+ {
43
+ std::unique_lock<std::mutex> lk(_cvm);
44
+ _cv.wait(lk, [this] { return dataSent; });
45
+ dataSent = false;
46
+ }
42
47
  }
43
48
  }
44
49
 
@@ -48,7 +53,12 @@ class TestWorker : public AsyncProgressWorker<ProgressData> {
48
53
  Number progress = Number::New(env, data->progress);
49
54
  _progress.MakeCallback(Receiver().Value(), {progress});
50
55
  }
51
- _cv.notify_one();
56
+
57
+ {
58
+ std::lock_guard<std::mutex> lk(_cvm);
59
+ dataSent = true;
60
+ _cv.notify_one();
61
+ }
52
62
  }
53
63
 
54
64
  private:
@@ -59,6 +69,8 @@ class TestWorker : public AsyncProgressWorker<ProgressData> {
59
69
  : AsyncProgressWorker(cb, resource_name, resource) {
60
70
  _progress.Reset(progress, 1);
61
71
  }
72
+
73
+ bool dataSent = false;
62
74
  std::condition_variable _cv;
63
75
  std::mutex _cvm;
64
76
  int32_t _times;
@@ -22,6 +22,7 @@ Object InitBasicTypesValue(Env env);
22
22
  Object InitBigInt(Env env);
23
23
  #endif
24
24
  Object InitBuffer(Env env);
25
+ Object InitBufferNoExternal(Env env);
25
26
  #if (NAPI_VERSION > 2)
26
27
  Object InitCallbackScope(Env env);
27
28
  #endif
@@ -75,7 +76,7 @@ Object InitVersionManagement(Env env);
75
76
  Object InitThunkingManual(Env env);
76
77
  #if (NAPI_VERSION > 7)
77
78
  Object InitObjectFreezeSeal(Env env);
78
- Object InitObjectTypeTag(Env env);
79
+ Object InitTypeTaggable(Env env);
79
80
  #endif
80
81
 
81
82
  #if defined(NODE_ADDON_API_ENABLE_MAYBE)
@@ -107,6 +108,7 @@ Object Init(Env env, Object exports) {
107
108
  exports.Set("date", InitDate(env));
108
109
  #endif
109
110
  exports.Set("buffer", InitBuffer(env));
111
+ exports.Set("bufferNoExternal", InitBufferNoExternal(env));
110
112
  #if (NAPI_VERSION > 2)
111
113
  exports.Set("callbackscope", InitCallbackScope(env));
112
114
  #endif
@@ -167,7 +169,7 @@ Object Init(Env env, Object exports) {
167
169
  exports.Set("thunking_manual", InitThunkingManual(env));
168
170
  #if (NAPI_VERSION > 7)
169
171
  exports.Set("object_freeze_seal", InitObjectFreezeSeal(env));
170
- exports.Set("object_type_tag", InitObjectTypeTag(env));
172
+ exports.Set("type_taggable", InitTypeTaggable(env));
171
173
  #endif
172
174
 
173
175
  #if defined(NODE_ADDON_API_ENABLE_MAYBE)
@@ -20,6 +20,7 @@
20
20
  'callbackInfo.cc',
21
21
  'date.cc',
22
22
  'binding.cc',
23
+ 'buffer_no_external.cc',
23
24
  'buffer.cc',
24
25
  'callbackscope.cc',
25
26
  'dataview/dataview.cc',
@@ -47,7 +48,6 @@
47
48
  'object/has_property.cc',
48
49
  'object/object.cc',
49
50
  'object/object_freeze_seal.cc',
50
- 'object/object_type_tag.cc',
51
51
  'object/set_property.cc',
52
52
  'object/subscript_operator.cc',
53
53
  'promise.cc',
@@ -59,6 +59,7 @@
59
59
  'threadsafe_function/threadsafe_function_sum.cc',
60
60
  'threadsafe_function/threadsafe_function_unref.cc',
61
61
  'threadsafe_function/threadsafe_function.cc',
62
+ 'type_taggable.cc',
62
63
  'typed_threadsafe_function/typed_threadsafe_function_ctx.cc',
63
64
  'typed_threadsafe_function/typed_threadsafe_function_existing_tsfn.cc',
64
65
  'typed_threadsafe_function/typed_threadsafe_function_ptr.cc',
@@ -80,6 +81,9 @@
80
81
  'binding-swallowexcept.cc',
81
82
  'error.cc',
82
83
  ],
84
+ 'build_sources_type_check': [
85
+ 'value_type_cast.cc'
86
+ ],
83
87
  'conditions': [
84
88
  ['disable_deprecated!="true"', {
85
89
  'build_sources': ['object/object_deprecated.cc']
@@ -116,6 +120,12 @@
116
120
  'sources': ['>@(build_sources_swallowexcept)'],
117
121
  'defines': ['NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS']
118
122
  },
123
+ {
124
+ 'target_name': 'binding_type_check',
125
+ 'includes': ['../noexcept.gypi'],
126
+ 'sources': ['>@(build_sources_type_check)'],
127
+ 'defines': ['NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS']
128
+ },
119
129
  {
120
130
  'target_name': 'binding_custom_namespace',
121
131
  'includes': ['../noexcept.gypi'],
@@ -1,30 +1,16 @@
1
+ #include "buffer.h"
1
2
  #include "napi.h"
2
3
 
3
4
  using namespace Napi;
4
5
 
5
- namespace {
6
-
7
- const size_t testLength = 4;
6
+ namespace test_buffer {
8
7
  uint16_t testData[testLength];
9
8
  int finalizeCount = 0;
9
+ } // namespace test_buffer
10
10
 
11
- template <typename T>
12
- void InitData(T* data, size_t length) {
13
- for (size_t i = 0; i < length; i++) {
14
- data[i] = static_cast<T>(i);
15
- }
16
- }
17
-
18
- template <typename T>
19
- bool VerifyData(T* data, size_t length) {
20
- for (size_t i = 0; i < length; i++) {
21
- if (data[i] != static_cast<T>(i)) {
22
- return false;
23
- }
24
- }
25
- return true;
26
- }
11
+ using namespace test_buffer;
27
12
 
13
+ namespace {
28
14
  Value CreateBuffer(const CallbackInfo& info) {
29
15
  Buffer<uint16_t> buffer = Buffer<uint16_t>::New(info.Env(), testLength);
30
16
 
@@ -146,6 +132,8 @@ Value CreateBufferCopy(const CallbackInfo& info) {
146
132
  return buffer;
147
133
  }
148
134
 
135
+ #include "buffer_new_or_copy-inl.h"
136
+
149
137
  void CheckBuffer(const CallbackInfo& info) {
150
138
  if (!info[0].IsBuffer()) {
151
139
  Error::New(info.Env(), "A buffer was expected.")
@@ -183,6 +171,12 @@ Object InitBuffer(Env env) {
183
171
  Function::New(env, CreateExternalBufferWithFinalize);
184
172
  exports["createExternalBufferWithFinalizeHint"] =
185
173
  Function::New(env, CreateExternalBufferWithFinalizeHint);
174
+ exports["createOrCopyExternalBuffer"] =
175
+ Function::New(env, CreateOrCopyExternalBuffer);
176
+ exports["createOrCopyExternalBufferWithFinalize"] =
177
+ Function::New(env, CreateOrCopyExternalBufferWithFinalize);
178
+ exports["createOrCopyExternalBufferWithFinalizeHint"] =
179
+ Function::New(env, CreateOrCopyExternalBufferWithFinalizeHint);
186
180
  exports["createBufferCopy"] = Function::New(env, CreateBufferCopy);
187
181
  exports["checkBuffer"] = Function::New(env, CheckBuffer);
188
182
  exports["getFinalizeCount"] = Function::New(env, GetFinalizeCount);
@@ -0,0 +1,26 @@
1
+ #include <cstdint>
2
+ #include <cstdlib>
3
+
4
+ namespace test_buffer {
5
+
6
+ const size_t testLength = 4;
7
+ extern uint16_t testData[testLength];
8
+ extern int finalizeCount;
9
+
10
+ template <typename T>
11
+ void InitData(T* data, size_t length) {
12
+ for (size_t i = 0; i < length; i++) {
13
+ data[i] = static_cast<T>(i);
14
+ }
15
+ }
16
+
17
+ template <typename T>
18
+ bool VerifyData(T* data, size_t length) {
19
+ for (size_t i = 0; i < length; i++) {
20
+ if (data[i] != static_cast<T>(i)) {
21
+ return false;
22
+ }
23
+ }
24
+ return true;
25
+ }
26
+ } // namespace test_buffer
@@ -62,6 +62,88 @@ function test (binding) {
62
62
  () => {
63
63
  global.gc();
64
64
  },
65
+ () => {
66
+ assert.strictEqual(1, binding.buffer.getFinalizeCount());
67
+ },
68
+
69
+ 'Create or Copy External Buffer',
70
+ () => {
71
+ const test = binding.buffer.createOrCopyExternalBuffer();
72
+ binding.buffer.checkBuffer(test);
73
+ assert.ok(test instanceof Buffer);
74
+ assert.strictEqual(0, binding.buffer.getFinalizeCount());
75
+ },
76
+ () => {
77
+ global.gc();
78
+ assert.strictEqual(0, binding.buffer.getFinalizeCount());
79
+ },
80
+
81
+ 'Create or Copy External Buffer with finalizer',
82
+ () => {
83
+ const test = binding.buffer.createOrCopyExternalBufferWithFinalize();
84
+ binding.buffer.checkBuffer(test);
85
+ assert.ok(test instanceof Buffer);
86
+ assert.strictEqual(0, binding.buffer.getFinalizeCount());
87
+ },
88
+ () => {
89
+ global.gc();
90
+ },
91
+ () => {
92
+ assert.strictEqual(1, binding.buffer.getFinalizeCount());
93
+ },
94
+
95
+ 'Create or Copy External Buffer with finalizer hint',
96
+ () => {
97
+ const test = binding.buffer.createOrCopyExternalBufferWithFinalizeHint();
98
+ binding.buffer.checkBuffer(test);
99
+ assert.ok(test instanceof Buffer);
100
+ assert.strictEqual(0, binding.buffer.getFinalizeCount());
101
+ },
102
+ () => {
103
+ global.gc();
104
+ },
105
+ () => {
106
+ assert.strictEqual(1, binding.buffer.getFinalizeCount());
107
+ },
108
+
109
+ 'Create or Copy External Buffer when NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED defined',
110
+ () => {
111
+ const test = binding.bufferNoExternal.createOrCopyExternalBuffer();
112
+ binding.buffer.checkBuffer(test);
113
+ assert.ok(test instanceof Buffer);
114
+ assert.strictEqual(0, binding.buffer.getFinalizeCount());
115
+ },
116
+ () => {
117
+ global.gc();
118
+ assert.strictEqual(0, binding.buffer.getFinalizeCount());
119
+ },
120
+
121
+ 'Create or Copy External Buffer with finalizer when NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED defined',
122
+ () => {
123
+ const test = binding.bufferNoExternal.createOrCopyExternalBufferWithFinalize();
124
+ binding.buffer.checkBuffer(test);
125
+ assert.ok(test instanceof Buffer);
126
+ // finalizer should have been called when the buffer was created.
127
+ assert.strictEqual(1, binding.buffer.getFinalizeCount());
128
+ },
129
+ () => {
130
+ global.gc();
131
+ },
132
+ () => {
133
+ assert.strictEqual(1, binding.buffer.getFinalizeCount());
134
+ },
135
+
136
+ 'Create or Copy External Buffer with finalizer hint when NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED defined',
137
+ () => {
138
+ const test = binding.bufferNoExternal.createOrCopyExternalBufferWithFinalizeHint();
139
+ binding.buffer.checkBuffer(test);
140
+ assert.ok(test instanceof Buffer);
141
+ // finalizer should have been called when the buffer was created.
142
+ assert.strictEqual(1, binding.buffer.getFinalizeCount());
143
+ },
144
+ () => {
145
+ global.gc();
146
+ },
65
147
  () => {
66
148
  assert.strictEqual(1, binding.buffer.getFinalizeCount());
67
149
  }
@@ -0,0 +1,68 @@
1
+ // Same tests on when NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED is defined or not
2
+ // defined.
3
+
4
+ Value CreateOrCopyExternalBuffer(const CallbackInfo& info) {
5
+ finalizeCount = 0;
6
+
7
+ InitData(testData, testLength);
8
+ Buffer<uint16_t> buffer =
9
+ Buffer<uint16_t>::NewOrCopy(info.Env(), testData, testLength);
10
+
11
+ if (buffer.Length() != testLength) {
12
+ Error::New(info.Env(), "Incorrect buffer length.")
13
+ .ThrowAsJavaScriptException();
14
+ return Value();
15
+ }
16
+
17
+ VerifyData(buffer.Data(), testLength);
18
+ return buffer;
19
+ }
20
+
21
+ Value CreateOrCopyExternalBufferWithFinalize(const CallbackInfo& info) {
22
+ finalizeCount = 0;
23
+
24
+ uint16_t* data = new uint16_t[testLength];
25
+ InitData(data, testLength);
26
+
27
+ Buffer<uint16_t> buffer = Buffer<uint16_t>::NewOrCopy(
28
+ info.Env(), data, testLength, [](Env /*env*/, uint16_t* finalizeData) {
29
+ delete[] finalizeData;
30
+ finalizeCount++;
31
+ });
32
+
33
+ if (buffer.Length() != testLength) {
34
+ Error::New(info.Env(), "Incorrect buffer length.")
35
+ .ThrowAsJavaScriptException();
36
+ return Value();
37
+ }
38
+
39
+ VerifyData(buffer.Data(), testLength);
40
+ return buffer;
41
+ }
42
+
43
+ Value CreateOrCopyExternalBufferWithFinalizeHint(const CallbackInfo& info) {
44
+ finalizeCount = 0;
45
+
46
+ uint16_t* data = new uint16_t[testLength];
47
+ InitData(data, testLength);
48
+
49
+ char* hint = nullptr;
50
+ Buffer<uint16_t> buffer = Buffer<uint16_t>::NewOrCopy(
51
+ info.Env(),
52
+ data,
53
+ testLength,
54
+ [](Env /*env*/, uint16_t* finalizeData, char* /*finalizeHint*/) {
55
+ delete[] finalizeData;
56
+ finalizeCount++;
57
+ },
58
+ hint);
59
+
60
+ if (buffer.Length() != testLength) {
61
+ Error::New(info.Env(), "Incorrect buffer length.")
62
+ .ThrowAsJavaScriptException();
63
+ return Value();
64
+ }
65
+
66
+ VerifyData(buffer.Data(), testLength);
67
+ return buffer;
68
+ }
@@ -0,0 +1,24 @@
1
+ #define NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
2
+ // Should compile without errors
3
+ #include "buffer.h"
4
+ #include "napi.h"
5
+
6
+ using namespace Napi;
7
+ using namespace test_buffer;
8
+
9
+ namespace {
10
+ #include "buffer_new_or_copy-inl.h"
11
+ }
12
+
13
+ Object InitBufferNoExternal(Env env) {
14
+ Object exports = Object::New(env);
15
+
16
+ exports["createOrCopyExternalBuffer"] =
17
+ Function::New(env, CreateOrCopyExternalBuffer);
18
+ exports["createOrCopyExternalBufferWithFinalize"] =
19
+ Function::New(env, CreateOrCopyExternalBufferWithFinalize);
20
+ exports["createOrCopyExternalBufferWithFinalizeHint"] =
21
+ Function::New(env, CreateOrCopyExternalBufferWithFinalizeHint);
22
+
23
+ return exports;
24
+ }