koffi 2.7.1 → 2.7.3

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 (73) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +2 -2
  3. package/build/koffi/darwin_arm64/koffi.node +0 -0
  4. package/build/koffi/darwin_x64/koffi.node +0 -0
  5. package/build/koffi/freebsd_arm64/koffi.node +0 -0
  6. package/build/koffi/freebsd_ia32/koffi.node +0 -0
  7. package/build/koffi/freebsd_x64/koffi.node +0 -0
  8. package/build/koffi/linux_arm32hf/koffi.node +0 -0
  9. package/build/koffi/linux_arm64/koffi.node +0 -0
  10. package/build/koffi/linux_ia32/koffi.node +0 -0
  11. package/build/koffi/linux_riscv64hf64/koffi.node +0 -0
  12. package/build/koffi/linux_x64/koffi.node +0 -0
  13. package/build/koffi/openbsd_ia32/koffi.node +0 -0
  14. package/build/koffi/openbsd_x64/koffi.node +0 -0
  15. package/build/koffi/win32_arm64/koffi.node +0 -0
  16. package/build/koffi/win32_ia32/koffi.node +0 -0
  17. package/build/koffi/win32_x64/koffi.node +0 -0
  18. package/doc/misc.md +1 -1
  19. package/doc/unions.md +2 -2
  20. package/index.d.ts +2 -1
  21. package/index.js +23 -23
  22. package/indirect.js +23 -23
  23. package/package.json +2 -2
  24. package/src/core/libcc/brotli.cc +16 -24
  25. package/src/core/libcc/libcc.cc +63 -69
  26. package/src/core/libcc/libcc.hh +103 -110
  27. package/src/core/libcc/lz4.cc +18 -25
  28. package/src/core/libcc/miniz.cc +10 -18
  29. package/src/koffi/src/call.cc +1 -1
  30. package/src/koffi/src/util.cc +7 -13
  31. package/src/koffi/src/util.hh +9 -1
  32. package/vendor/node-addon-api/CHANGELOG.md +76 -0
  33. package/vendor/node-addon-api/LICENSE.md +2 -6
  34. package/vendor/node-addon-api/README.md +10 -9
  35. package/vendor/node-addon-api/benchmark/binding.gyp +4 -4
  36. package/vendor/node-addon-api/common.gypi +1 -2
  37. package/vendor/node-addon-api/doc/array.md +1 -1
  38. package/vendor/node-addon-api/doc/async_worker_variants.md +16 -16
  39. package/vendor/node-addon-api/doc/cmake-js.md +1 -1
  40. package/vendor/node-addon-api/doc/env.md +11 -0
  41. package/vendor/node-addon-api/doc/hierarchy.md +2 -0
  42. package/vendor/node-addon-api/doc/setup.md +53 -71
  43. package/vendor/node-addon-api/doc/syntax_error.md +66 -0
  44. package/vendor/node-addon-api/doc/value.md +2 -0
  45. package/vendor/node-addon-api/index.js +2 -1
  46. package/vendor/node-addon-api/napi-inl.h +68 -65
  47. package/vendor/node-addon-api/napi.h +15 -5
  48. package/vendor/node-addon-api/node_addon_api.gyp +32 -0
  49. package/vendor/node-addon-api/package.json +14 -3
  50. package/vendor/node-addon-api/test/addon.cc +7 -1
  51. package/vendor/node-addon-api/test/addon.js +5 -9
  52. package/vendor/node-addon-api/test/addon_data.cc +3 -3
  53. package/vendor/node-addon-api/test/addon_data.js +16 -38
  54. package/vendor/node-addon-api/test/bigint.cc +0 -1
  55. package/vendor/node-addon-api/test/binding.cc +12 -1
  56. package/vendor/node-addon-api/test/binding.gyp +10 -8
  57. package/vendor/node-addon-api/test/child_processes/addon.js +11 -0
  58. package/vendor/node-addon-api/test/child_processes/addon_data.js +24 -0
  59. package/vendor/node-addon-api/test/child_processes/objectwrap_function.js +22 -0
  60. package/vendor/node-addon-api/test/child_processes/threadsafe_function_exception.js +33 -0
  61. package/vendor/node-addon-api/test/child_processes/typed_threadsafe_function_exception.js +19 -0
  62. package/vendor/node-addon-api/test/common/index.js +57 -3
  63. package/vendor/node-addon-api/test/env_misc.cc +25 -0
  64. package/vendor/node-addon-api/test/env_misc.js +12 -0
  65. package/vendor/node-addon-api/test/error.cc +47 -0
  66. package/vendor/node-addon-api/test/error.js +16 -0
  67. package/vendor/node-addon-api/test/index.js +5 -0
  68. package/vendor/node-addon-api/test/objectwrap_function.cc +10 -12
  69. package/vendor/node-addon-api/test/objectwrap_function.js +4 -20
  70. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_exception.cc +50 -0
  71. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_exception.js +20 -0
  72. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_exception.cc +39 -0
  73. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_exception.js +13 -0
@@ -31,22 +31,22 @@ namespace details {
31
31
  // Node.js releases. Only necessary when they are used in napi.h and napi-inl.h.
32
32
  constexpr int napi_no_external_buffers_allowed = 22;
33
33
 
34
+ template <typename FreeType>
35
+ inline void default_finalizer(napi_env /*env*/, void* data, void* /*hint*/) {
36
+ delete static_cast<FreeType*>(data);
37
+ }
38
+
34
39
  // Attach a data item to an object and delete it when the object gets
35
40
  // garbage-collected.
36
41
  // TODO: Replace this code with `napi_add_finalizer()` whenever it becomes
37
42
  // available on all supported versions of Node.js.
38
- template <typename FreeType>
43
+ template <typename FreeType,
44
+ napi_finalize finalizer = default_finalizer<FreeType>>
39
45
  inline napi_status AttachData(napi_env env,
40
46
  napi_value obj,
41
47
  FreeType* data,
42
- napi_finalize finalizer = nullptr,
43
48
  void* hint = nullptr) {
44
49
  napi_status status;
45
- if (finalizer == nullptr) {
46
- finalizer = [](napi_env /*env*/, void* data, void* /*hint*/) {
47
- delete static_cast<FreeType*>(data);
48
- };
49
- }
50
50
  #if (NAPI_VERSION < 5)
51
51
  napi_value symbol, external;
52
52
  status = napi_create_symbol(env, nullptr, &symbol);
@@ -263,10 +263,12 @@ struct ThreadSafeFinalize {
263
263
  template <typename ContextType, typename DataType, typename CallJs, CallJs call>
264
264
  inline typename std::enable_if<call != static_cast<CallJs>(nullptr)>::type
265
265
  CallJsWrapper(napi_env env, napi_value jsCallback, void* context, void* data) {
266
- call(env,
267
- Function(env, jsCallback),
268
- static_cast<ContextType*>(context),
269
- static_cast<DataType*>(data));
266
+ details::WrapVoidCallback([&]() {
267
+ call(env,
268
+ Function(env, jsCallback),
269
+ static_cast<ContextType*>(context),
270
+ static_cast<DataType*>(data));
271
+ });
270
272
  }
271
273
 
272
274
  template <typename ContextType, typename DataType, typename CallJs, CallJs call>
@@ -275,9 +277,11 @@ CallJsWrapper(napi_env env,
275
277
  napi_value jsCallback,
276
278
  void* /*context*/,
277
279
  void* /*data*/) {
278
- if (jsCallback != nullptr) {
279
- Function(env, jsCallback).Call(0, nullptr);
280
- }
280
+ details::WrapVoidCallback([&]() {
281
+ if (jsCallback != nullptr) {
282
+ Function(env, jsCallback).Call(0, nullptr);
283
+ }
284
+ });
281
285
  }
282
286
 
283
287
  #if NAPI_VERSION > 4
@@ -572,6 +576,14 @@ void Env::DefaultFiniWithHint(Env, DataType* data, HintType*) {
572
576
  }
573
577
  #endif // NAPI_VERSION > 5
574
578
 
579
+ #if NAPI_VERSION > 8
580
+ inline const char* Env::GetModuleFileName() const {
581
+ const char* result;
582
+ napi_status status = node_api_get_module_file_name(_env, &result);
583
+ NAPI_THROW_IF_FAILED(*this, status, nullptr);
584
+ return result;
585
+ }
586
+ #endif // NAPI_VERSION > 8
575
587
  ////////////////////////////////////////////////////////////////////////////////
576
588
  // Value class
577
589
  ////////////////////////////////////////////////////////////////////////////////
@@ -1624,11 +1636,8 @@ inline void Object::AddFinalizer(Finalizer finalizeCallback, T* data) const {
1624
1636
  new details::FinalizeData<T, Finalizer>(
1625
1637
  {std::move(finalizeCallback), nullptr});
1626
1638
  napi_status status =
1627
- details::AttachData(_env,
1628
- *this,
1629
- data,
1630
- details::FinalizeData<T, Finalizer>::Wrapper,
1631
- finalizeData);
1639
+ details::AttachData<T, details::FinalizeData<T, Finalizer>::Wrapper>(
1640
+ _env, *this, data, finalizeData);
1632
1641
  if (status != napi_ok) {
1633
1642
  delete finalizeData;
1634
1643
  NAPI_THROW_IF_FAILED_VOID(_env, status);
@@ -1642,12 +1651,9 @@ inline void Object::AddFinalizer(Finalizer finalizeCallback,
1642
1651
  details::FinalizeData<T, Finalizer, Hint>* finalizeData =
1643
1652
  new details::FinalizeData<T, Finalizer, Hint>(
1644
1653
  {std::move(finalizeCallback), finalizeHint});
1645
- napi_status status = details::AttachData(
1646
- _env,
1647
- *this,
1648
- data,
1649
- details::FinalizeData<T, Finalizer, Hint>::WrapperWithHint,
1650
- finalizeData);
1654
+ napi_status status = details::
1655
+ AttachData<T, details::FinalizeData<T, Finalizer, Hint>::WrapperWithHint>(
1656
+ _env, *this, data, finalizeData);
1651
1657
  if (status != napi_ok) {
1652
1658
  delete finalizeData;
1653
1659
  NAPI_THROW_IF_FAILED_VOID(_env, status);
@@ -2623,7 +2629,7 @@ inline Buffer<T> Buffer<T>::New(napi_env env, size_t length) {
2623
2629
  napi_status status =
2624
2630
  napi_create_buffer(env, length * sizeof(T), &data, &value);
2625
2631
  NAPI_THROW_IF_FAILED(env, status, Buffer<T>());
2626
- return Buffer(env, value, length, static_cast<T*>(data));
2632
+ return Buffer(env, value);
2627
2633
  }
2628
2634
 
2629
2635
  #ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
@@ -2633,7 +2639,7 @@ inline Buffer<T> Buffer<T>::New(napi_env env, T* data, size_t length) {
2633
2639
  napi_status status = napi_create_external_buffer(
2634
2640
  env, length * sizeof(T), data, nullptr, nullptr, &value);
2635
2641
  NAPI_THROW_IF_FAILED(env, status, Buffer<T>());
2636
- return Buffer(env, value, length, data);
2642
+ return Buffer(env, value);
2637
2643
  }
2638
2644
 
2639
2645
  template <typename T>
@@ -2657,7 +2663,7 @@ inline Buffer<T> Buffer<T>::New(napi_env env,
2657
2663
  delete finalizeData;
2658
2664
  NAPI_THROW_IF_FAILED(env, status, Buffer());
2659
2665
  }
2660
- return Buffer(env, value, length, data);
2666
+ return Buffer(env, value);
2661
2667
  }
2662
2668
 
2663
2669
  template <typename T>
@@ -2682,7 +2688,7 @@ inline Buffer<T> Buffer<T>::New(napi_env env,
2682
2688
  delete finalizeData;
2683
2689
  NAPI_THROW_IF_FAILED(env, status, Buffer());
2684
2690
  }
2685
- return Buffer(env, value, length, data);
2691
+ return Buffer(env, value);
2686
2692
  }
2687
2693
  #endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
2688
2694
 
@@ -2699,7 +2705,7 @@ inline Buffer<T> Buffer<T>::NewOrCopy(napi_env env, T* data, size_t length) {
2699
2705
  #ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
2700
2706
  }
2701
2707
  NAPI_THROW_IF_FAILED(env, status, Buffer<T>());
2702
- return Buffer(env, value, length, data);
2708
+ return Buffer(env, value);
2703
2709
  #endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
2704
2710
  }
2705
2711
 
@@ -2733,7 +2739,7 @@ inline Buffer<T> Buffer<T>::NewOrCopy(napi_env env,
2733
2739
  delete finalizeData;
2734
2740
  NAPI_THROW_IF_FAILED(env, status, Buffer());
2735
2741
  }
2736
- return Buffer(env, value, length, data);
2742
+ return Buffer(env, value);
2737
2743
  #endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
2738
2744
  }
2739
2745
 
@@ -2769,7 +2775,7 @@ inline Buffer<T> Buffer<T>::NewOrCopy(napi_env env,
2769
2775
  delete finalizeData;
2770
2776
  NAPI_THROW_IF_FAILED(env, status, Buffer());
2771
2777
  }
2772
- return Buffer(env, value, length, data);
2778
+ return Buffer(env, value);
2773
2779
  #endif
2774
2780
  }
2775
2781
 
@@ -2793,42 +2799,20 @@ inline void Buffer<T>::CheckCast(napi_env env, napi_value value) {
2793
2799
  }
2794
2800
 
2795
2801
  template <typename T>
2796
- inline Buffer<T>::Buffer() : Uint8Array(), _length(0), _data(nullptr) {}
2802
+ inline Buffer<T>::Buffer() : Uint8Array() {}
2797
2803
 
2798
2804
  template <typename T>
2799
2805
  inline Buffer<T>::Buffer(napi_env env, napi_value value)
2800
- : Uint8Array(env, value), _length(0), _data(nullptr) {}
2801
-
2802
- template <typename T>
2803
- inline Buffer<T>::Buffer(napi_env env, napi_value value, size_t length, T* data)
2804
- : Uint8Array(env, value), _length(length), _data(data) {}
2806
+ : Uint8Array(env, value) {}
2805
2807
 
2806
2808
  template <typename T>
2807
2809
  inline size_t Buffer<T>::Length() const {
2808
- EnsureInfo();
2809
- return _length;
2810
+ return ByteLength() / sizeof(T);
2810
2811
  }
2811
2812
 
2812
2813
  template <typename T>
2813
2814
  inline T* Buffer<T>::Data() const {
2814
- EnsureInfo();
2815
- return _data;
2816
- }
2817
-
2818
- template <typename T>
2819
- inline void Buffer<T>::EnsureInfo() const {
2820
- // The Buffer instance may have been constructed from a napi_value whose
2821
- // length/data are not yet known. Fetch and cache these values just once,
2822
- // since they can never change during the lifetime of the Buffer.
2823
- if (_data == nullptr) {
2824
- size_t byteLength;
2825
- void* voidData;
2826
- napi_status status =
2827
- napi_get_buffer_info(_env, _value, &voidData, &byteLength);
2828
- NAPI_THROW_IF_FAILED_VOID(_env, status);
2829
- _length = byteLength / sizeof(T);
2830
- _data = static_cast<T*>(voidData);
2831
- }
2815
+ return reinterpret_cast<T*>(const_cast<uint8_t*>(Uint8Array::Data()));
2832
2816
  }
2833
2817
 
2834
2818
  ////////////////////////////////////////////////////////////////////////////////
@@ -3157,6 +3141,23 @@ inline RangeError::RangeError() : Error() {}
3157
3141
  inline RangeError::RangeError(napi_env env, napi_value value)
3158
3142
  : Error(env, value) {}
3159
3143
 
3144
+ #if NAPI_VERSION > 8
3145
+ inline SyntaxError SyntaxError::New(napi_env env, const char* message) {
3146
+ return Error::New<SyntaxError>(
3147
+ env, message, std::strlen(message), node_api_create_syntax_error);
3148
+ }
3149
+
3150
+ inline SyntaxError SyntaxError::New(napi_env env, const std::string& message) {
3151
+ return Error::New<SyntaxError>(
3152
+ env, message.c_str(), message.size(), node_api_create_syntax_error);
3153
+ }
3154
+
3155
+ inline SyntaxError::SyntaxError() : Error() {}
3156
+
3157
+ inline SyntaxError::SyntaxError(napi_env env, napi_value value)
3158
+ : Error(env, value) {}
3159
+ #endif // NAPI_VERSION > 8
3160
+
3160
3161
  ////////////////////////////////////////////////////////////////////////////////
3161
3162
  // Reference<T> class
3162
3163
  ////////////////////////////////////////////////////////////////////////////////
@@ -6110,13 +6111,15 @@ inline void ThreadSafeFunction::CallJS(napi_env env,
6110
6111
  return;
6111
6112
  }
6112
6113
 
6113
- if (data != nullptr) {
6114
- auto* callbackWrapper = static_cast<CallbackWrapper*>(data);
6115
- (*callbackWrapper)(env, Function(env, jsCallback));
6116
- delete callbackWrapper;
6117
- } else if (jsCallback != nullptr) {
6118
- Function(env, jsCallback).Call({});
6119
- }
6114
+ details::WrapVoidCallback([&]() {
6115
+ if (data != nullptr) {
6116
+ auto* callbackWrapper = static_cast<CallbackWrapper*>(data);
6117
+ (*callbackWrapper)(env, Function(env, jsCallback));
6118
+ delete callbackWrapper;
6119
+ } else if (jsCallback != nullptr) {
6120
+ Function(env, jsCallback).Call({});
6121
+ }
6122
+ });
6120
6123
  }
6121
6124
 
6122
6125
  ////////////////////////////////////////////////////////////////////////////////
@@ -367,6 +367,10 @@ class Env {
367
367
  } * data;
368
368
  };
369
369
  #endif // NAPI_VERSION > 2
370
+
371
+ #if NAPI_VERSION > 8
372
+ const char* GetModuleFileName() const;
373
+ #endif // NAPI_VERSION > 8
370
374
  };
371
375
 
372
376
  /// A JavaScript value of unknown type.
@@ -1531,11 +1535,6 @@ class Buffer : public Uint8Array {
1531
1535
  T* Data() const;
1532
1536
 
1533
1537
  private:
1534
- mutable size_t _length;
1535
- mutable T* _data;
1536
-
1537
- Buffer(napi_env env, napi_value value, size_t length, T* data);
1538
- void EnsureInfo() const;
1539
1538
  };
1540
1539
 
1541
1540
  /// Holds a counted reference to a value; initially a weak reference unless
@@ -1853,6 +1852,17 @@ class RangeError : public Error {
1853
1852
  RangeError(napi_env env, napi_value value);
1854
1853
  };
1855
1854
 
1855
+ #if NAPI_VERSION > 8
1856
+ class SyntaxError : public Error {
1857
+ public:
1858
+ static SyntaxError New(napi_env env, const char* message);
1859
+ static SyntaxError New(napi_env env, const std::string& message);
1860
+
1861
+ SyntaxError();
1862
+ SyntaxError(napi_env env, napi_value value);
1863
+ };
1864
+ #endif // NAPI_VERSION > 8
1865
+
1856
1866
  class CallbackInfo {
1857
1867
  public:
1858
1868
  CallbackInfo(napi_env env, napi_callback_info info);
@@ -0,0 +1,32 @@
1
+ {
2
+ 'targets': [
3
+ {
4
+ 'target_name': 'node_addon_api',
5
+ 'type': 'none',
6
+ 'sources': [ 'napi.h', 'napi-inl.h' ],
7
+ 'direct_dependent_settings': {
8
+ 'include_dirs': [ '.' ],
9
+ 'includes': ['noexcept.gypi'],
10
+ }
11
+ },
12
+ {
13
+ 'target_name': 'node_addon_api_except',
14
+ 'type': 'none',
15
+ 'sources': [ 'napi.h', 'napi-inl.h' ],
16
+ 'direct_dependent_settings': {
17
+ 'include_dirs': [ '.' ],
18
+ 'includes': ['except.gypi'],
19
+ }
20
+ },
21
+ {
22
+ 'target_name': 'node_addon_api_maybe',
23
+ 'type': 'none',
24
+ 'sources': [ 'napi.h', 'napi-inl.h' ],
25
+ 'direct_dependent_settings': {
26
+ 'include_dirs': [ '.' ],
27
+ 'includes': ['noexcept.gypi'],
28
+ 'defines': ['NODE_ADDON_API_ENABLE_MAYBE']
29
+ }
30
+ },
31
+ ]
32
+ }
@@ -407,6 +407,14 @@
407
407
  {
408
408
  "name": "Caleb Hearon",
409
409
  "url": "https://github.com/chearon"
410
+ },
411
+ {
412
+ "name": "Marx",
413
+ "url": "https://github.com/MarxJiao"
414
+ },
415
+ {
416
+ "name": "Ömer AKGÜL",
417
+ "url": "https://github.com/tuhalf"
410
418
  }
411
419
  ],
412
420
  "description": "Node.js API (Node-API)",
@@ -420,7 +428,7 @@
420
428
  "eslint-plugin-import": "^2.24.2",
421
429
  "eslint-plugin-node": "^11.1.0",
422
430
  "eslint-plugin-promise": "^5.1.0",
423
- "fs-extra": "^9.0.1",
431
+ "fs-extra": "^11.1.1",
424
432
  "path": "^0.12.7",
425
433
  "pre-commit": "^1.2.2",
426
434
  "safe-buffer": "^5.1.1"
@@ -467,6 +475,9 @@
467
475
  "lint:fix": "node tools/clang-format --fix && node tools/eslint-format --fix"
468
476
  },
469
477
  "pre-commit": "lint",
470
- "version": "7.0.0",
471
- "support": true
478
+ "version": "7.1.0",
479
+ "support": true,
480
+ "engines": {
481
+ "node": "^16 || ^18 || >= 20"
482
+ }
472
483
  }
@@ -17,6 +17,8 @@ class TestAddon : public Napi::Addon<TestAddon> {
17
17
  {InstanceMethod("decrement", &TestAddon::Decrement)}))});
18
18
  }
19
19
 
20
+ ~TestAddon() { fprintf(stderr, "TestAddon::~TestAddon\n"); }
21
+
20
22
  private:
21
23
  Napi::Value Increment(const Napi::CallbackInfo& info) {
22
24
  return Napi::Number::New(info.Env(), ++value);
@@ -29,10 +31,14 @@ class TestAddon : public Napi::Addon<TestAddon> {
29
31
  uint32_t value = 42;
30
32
  };
31
33
 
34
+ Napi::Value CreateAddon(const Napi::CallbackInfo& info) {
35
+ return TestAddon::Init(info.Env(), Napi::Object::New(info.Env()));
36
+ }
37
+
32
38
  } // end of anonymous namespace
33
39
 
34
40
  Napi::Object InitAddon(Napi::Env env) {
35
- return TestAddon::Init(env, Napi::Object::New(env));
41
+ return Napi::Function::New<CreateAddon>(env, "CreateAddon");
36
42
  }
37
43
 
38
44
  #endif // (NAPI_VERSION > 5)
@@ -1,11 +1,7 @@
1
1
  'use strict';
2
2
 
3
- const assert = require('assert');
4
-
5
- module.exports = require('./common').runTest(test);
6
-
7
- function test (binding) {
8
- assert.strictEqual(binding.addon.increment(), 43);
9
- assert.strictEqual(binding.addon.increment(), 44);
10
- assert.strictEqual(binding.addon.subObject.decrement(), 43);
11
- }
3
+ module.exports = require('./common').runTestInChildProcess({
4
+ suite: 'addon',
5
+ testName: 'workingCode',
6
+ expectedStderr: ['TestAddon::~TestAddon']
7
+ });
@@ -4,10 +4,10 @@
4
4
  #include "test_helper.h"
5
5
 
6
6
  // An overly elaborate way to get/set a boolean stored in the instance data:
7
- // 0. A boolean named "verbose" is stored in the instance data. The constructor
8
- // for JS `VerboseIndicator` instances is also stored in the instance data.
7
+ // 0. The constructor for JS `VerboseIndicator` instances, which have a private
8
+ // member named "verbose", is stored in the instance data.
9
9
  // 1. Add a property named "verbose" onto exports served by a getter/setter.
10
- // 2. The getter returns a object of type VerboseIndicator, which itself has a
10
+ // 2. The getter returns an object of type VerboseIndicator, which itself has a
11
11
  // property named "verbose", also served by a getter/setter:
12
12
  // * The getter returns a boolean, indicating whether "verbose" is set.
13
13
  // * The setter sets "verbose" on the instance data.
@@ -1,46 +1,24 @@
1
1
  'use strict';
2
2
 
3
- const assert = require('assert');
4
- const { spawn } = require('child_process');
5
- const readline = require('readline');
3
+ const common = require('./common');
6
4
 
7
- module.exports = require('./common').runTestWithBindingPath(test);
5
+ module.exports = common.runTest(test);
8
6
 
9
- // Make sure the instance data finalizer is called at process exit. If the hint
10
- // is non-zero, it will be printed out by the child process.
11
- function testFinalizer (bindingName, hint, expected) {
12
- return new Promise((resolve) => {
13
- bindingName = bindingName.split('\\').join('\\\\');
14
- const child = spawn(process.execPath, [
15
- '-e',
16
- `require('${bindingName}').addon_data(${hint}).verbose = true;`
17
- ]);
18
- const actual = [];
19
- readline
20
- .createInterface({ input: child.stderr })
21
- .on('line', (line) => {
22
- if (expected.indexOf(line) >= 0) {
23
- actual.push(line);
24
- }
25
- })
26
- .on('close', () => {
27
- assert.deepStrictEqual(expected, actual);
28
- resolve();
29
- });
7
+ async function test () {
8
+ await common.runTestInChildProcess({
9
+ suite: 'addon_data',
10
+ testName: 'workingCode'
30
11
  });
31
- }
32
-
33
- async function test (bindingName) {
34
- const binding = require(bindingName).addon_data(0);
35
12
 
36
- // Make sure it is possible to get/set instance data.
37
- assert.strictEqual(binding.verbose.verbose, false);
38
- binding.verbose = true;
39
- assert.strictEqual(binding.verbose.verbose, true);
40
- binding.verbose = false;
41
- assert.strictEqual(binding.verbose.verbose, false);
13
+ await common.runTestInChildProcess({
14
+ suite: 'addon_data',
15
+ testName: 'cleanupWithoutHint',
16
+ expectedStderr: ['addon_data: Addon::~Addon']
17
+ });
42
18
 
43
- await testFinalizer(bindingName, 0, ['addon_data: Addon::~Addon']);
44
- await testFinalizer(bindingName, 42,
45
- ['addon_data: Addon::~Addon', 'hint: 42']);
19
+ await common.runTestInChildProcess({
20
+ suite: 'addon_data',
21
+ testName: 'cleanupWithHint',
22
+ expectedStderr: ['addon_data: Addon::~Addon', 'hint: 42']
23
+ });
46
24
  }
@@ -1,6 +1,5 @@
1
1
  #if (NAPI_VERSION > 5)
2
2
 
3
- #define NAPI_EXPERIMENTAL
4
3
  #include "napi.h"
5
4
 
6
5
  #include "test_helper.h"
@@ -50,12 +50,14 @@ Object InitPromise(Env env);
50
50
  Object InitRunScript(Env env);
51
51
  #if (NAPI_VERSION > 3)
52
52
  Object InitThreadSafeFunctionCtx(Env env);
53
+ Object InitThreadSafeFunctionException(Env env);
53
54
  Object InitThreadSafeFunctionExistingTsfn(Env env);
54
55
  Object InitThreadSafeFunctionPtr(Env env);
55
56
  Object InitThreadSafeFunctionSum(Env env);
56
57
  Object InitThreadSafeFunctionUnref(Env env);
57
58
  Object InitThreadSafeFunction(Env env);
58
59
  Object InitTypedThreadSafeFunctionCtx(Env env);
60
+ Object InitTypedThreadSafeFunctionException(Env env);
59
61
  Object InitTypedThreadSafeFunctionExistingTsfn(Env env);
60
62
  Object InitTypedThreadSafeFunctionPtr(Env env);
61
63
  Object InitTypedThreadSafeFunctionSum(Env env);
@@ -78,7 +80,9 @@ Object InitThunkingManual(Env env);
78
80
  Object InitObjectFreezeSeal(Env env);
79
81
  Object InitTypeTaggable(Env env);
80
82
  #endif
81
-
83
+ #if (NAPI_VERSION > 8)
84
+ Object InitEnvMiscellaneous(Env env);
85
+ #endif
82
86
  #if defined(NODE_ADDON_API_ENABLE_MAYBE)
83
87
  Object InitMaybeCheck(Env env);
84
88
  #endif
@@ -137,6 +141,8 @@ Object Init(Env env, Object exports) {
137
141
  exports.Set("symbol", InitSymbol(env));
138
142
  #if (NAPI_VERSION > 3)
139
143
  exports.Set("threadsafe_function_ctx", InitThreadSafeFunctionCtx(env));
144
+ exports.Set("threadsafe_function_exception",
145
+ InitThreadSafeFunctionException(env));
140
146
  exports.Set("threadsafe_function_existing_tsfn",
141
147
  InitThreadSafeFunctionExistingTsfn(env));
142
148
  exports.Set("threadsafe_function_ptr", InitThreadSafeFunctionPtr(env));
@@ -145,6 +151,8 @@ Object Init(Env env, Object exports) {
145
151
  exports.Set("threadsafe_function", InitThreadSafeFunction(env));
146
152
  exports.Set("typed_threadsafe_function_ctx",
147
153
  InitTypedThreadSafeFunctionCtx(env));
154
+ exports.Set("typed_threadsafe_function_exception",
155
+ InitTypedThreadSafeFunctionException(env));
148
156
  exports.Set("typed_threadsafe_function_existing_tsfn",
149
157
  InitTypedThreadSafeFunctionExistingTsfn(env));
150
158
  exports.Set("typed_threadsafe_function_ptr",
@@ -171,6 +179,9 @@ Object Init(Env env, Object exports) {
171
179
  exports.Set("object_freeze_seal", InitObjectFreezeSeal(env));
172
180
  exports.Set("type_taggable", InitTypeTaggable(env));
173
181
  #endif
182
+ #if (NAPI_VERSION > 8)
183
+ exports.Set("env_misc", InitEnvMiscellaneous(env));
184
+ #endif
174
185
 
175
186
  #if defined(NODE_ADDON_API_ENABLE_MAYBE)
176
187
  exports.Set("maybe_check", InitMaybeCheck(env));
@@ -26,6 +26,7 @@
26
26
  'dataview/dataview.cc',
27
27
  'dataview/dataview_read_write.cc',
28
28
  'env_cleanup.cc',
29
+ 'env_misc.cc',
29
30
  'error.cc',
30
31
  'error_handling_for_primitives.cc',
31
32
  'external.cc',
@@ -54,6 +55,7 @@
54
55
  'run_script.cc',
55
56
  'symbol.cc',
56
57
  'threadsafe_function/threadsafe_function_ctx.cc',
58
+ 'threadsafe_function/threadsafe_function_exception.cc',
57
59
  'threadsafe_function/threadsafe_function_existing_tsfn.cc',
58
60
  'threadsafe_function/threadsafe_function_ptr.cc',
59
61
  'threadsafe_function/threadsafe_function_sum.cc',
@@ -61,6 +63,7 @@
61
63
  'threadsafe_function/threadsafe_function.cc',
62
64
  'type_taggable.cc',
63
65
  'typed_threadsafe_function/typed_threadsafe_function_ctx.cc',
66
+ 'typed_threadsafe_function/typed_threadsafe_function_exception.cc',
64
67
  'typed_threadsafe_function/typed_threadsafe_function_existing_tsfn.cc',
65
68
  'typed_threadsafe_function/typed_threadsafe_function_ptr.cc',
66
69
  'typed_threadsafe_function/typed_threadsafe_function_sum.cc',
@@ -94,41 +97,40 @@
94
97
  'targets': [
95
98
  {
96
99
  'target_name': 'binding',
97
- 'includes': ['../except.gypi'],
100
+ 'dependencies': ['../node_addon_api.gyp:node_addon_api_except'],
98
101
  'sources': ['>@(build_sources)']
99
102
  },
100
103
  {
101
104
  'target_name': 'binding_noexcept',
102
- 'includes': ['../noexcept.gypi'],
105
+ 'dependencies': ['../node_addon_api.gyp:node_addon_api'],
103
106
  'sources': ['>@(build_sources)']
104
107
  },
105
108
  {
106
109
  'target_name': 'binding_noexcept_maybe',
107
- 'includes': ['../noexcept.gypi'],
110
+ 'dependencies': ['../node_addon_api.gyp:node_addon_api_maybe'],
108
111
  'sources': ['>@(build_sources)'],
109
- 'defines': ['NODE_ADDON_API_ENABLE_MAYBE']
110
112
  },
111
113
  {
112
114
  'target_name': 'binding_swallowexcept',
113
- 'includes': ['../except.gypi'],
115
+ 'dependencies': ['../node_addon_api.gyp:node_addon_api_except'],
114
116
  'sources': [ '>@(build_sources_swallowexcept)'],
115
117
  'defines': ['NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS']
116
118
  },
117
119
  {
118
120
  'target_name': 'binding_swallowexcept_noexcept',
119
- 'includes': ['../noexcept.gypi'],
121
+ 'dependencies': ['../node_addon_api.gyp:node_addon_api'],
120
122
  'sources': ['>@(build_sources_swallowexcept)'],
121
123
  'defines': ['NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS']
122
124
  },
123
125
  {
124
126
  'target_name': 'binding_type_check',
125
- 'includes': ['../noexcept.gypi'],
127
+ 'dependencies': ['../node_addon_api.gyp:node_addon_api'],
126
128
  'sources': ['>@(build_sources_type_check)'],
127
129
  'defines': ['NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS']
128
130
  },
129
131
  {
130
132
  'target_name': 'binding_custom_namespace',
131
- 'includes': ['../noexcept.gypi'],
133
+ 'dependencies': ['../node_addon_api.gyp:node_addon_api'],
132
134
  'sources': ['>@(build_sources)'],
133
135
  'defines': ['NAPI_CPP_CUSTOM_NAMESPACE=cstm']
134
136
  },
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+ const assert = require('assert');
3
+
4
+ module.exports = {
5
+ workingCode: binding => {
6
+ const addon = binding.addon();
7
+ assert.strictEqual(addon.increment(), 43);
8
+ assert.strictEqual(addon.increment(), 44);
9
+ assert.strictEqual(addon.subObject.decrement(), 43);
10
+ }
11
+ };
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ const assert = require('assert');
4
+
5
+ // Make sure the instance data finalizer is called at process exit. If the hint
6
+ // is non-zero, it will be printed out by the child process.
7
+ const cleanupTest = (binding, hint) => {
8
+ binding.addon_data(hint).verbose = true;
9
+ };
10
+
11
+ module.exports = {
12
+ workingCode: binding => {
13
+ const addonData = binding.addon_data(0);
14
+
15
+ // Make sure it is possible to get/set instance data.
16
+ assert.strictEqual(addonData.verbose.verbose, false);
17
+ addonData.verbose = true;
18
+ assert.strictEqual(addonData.verbose.verbose, true);
19
+ addonData.verbose = false;
20
+ assert.strictEqual(addonData.verbose.verbose, false);
21
+ },
22
+ cleanupWithHint: binding => cleanupTest(binding, 42),
23
+ cleanupWithoutHint: binding => cleanupTest(binding, 0)
24
+ };