node-addon-api 6.1.0 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md CHANGED
@@ -1,13 +1,9 @@
1
1
  The MIT License (MIT)
2
- =====================
3
2
 
4
- Copyright (c) 2017 Node.js API collaborators
5
- -----------------------------------
6
-
7
- *Node.js API collaborators listed at <https://github.com/nodejs/node-addon-api#collaborators>*
3
+ Copyright (c) 2017 [Node.js API collaborators](https://github.com/nodejs/node-addon-api#collaborators)
8
4
 
9
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
6
 
11
7
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12
8
 
13
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -70,7 +70,7 @@ and node-addon-api.
70
70
  - **[Contributors](#contributors)**
71
71
  - **[License](#license)**
72
72
 
73
- ## **Current version: 6.1.0**
73
+ ## **Current version: 7.1.0**
74
74
 
75
75
  (See [CHANGELOG.md](CHANGELOG.md) for complete Changelog)
76
76
 
@@ -83,7 +83,7 @@ This allows addons built with it to run with Node.js versions which support the
83
83
  **However** the node-addon-api support model is to support only the active LTS Node.js versions. This means that
84
84
  every year there will be a new major which drops support for the Node.js LTS version which has gone out of service.
85
85
 
86
- The oldest Node.js version supported by the current version of node-addon-api is Node.js 14.x.
86
+ The oldest Node.js version supported by the current version of node-addon-api is Node.js 16.x.
87
87
 
88
88
  ## Setup
89
89
  - [Installation and usage](doc/setup.md)
@@ -132,6 +132,7 @@ The following is the documentation for node-addon-api.
132
132
  - [Error](doc/error.md)
133
133
  - [TypeError](doc/type_error.md)
134
134
  - [RangeError](doc/range_error.md)
135
+ - [SyntaxError](doc/syntax_error.md)
135
136
  - [Object Lifetime Management](doc/object_lifetime_management.md)
136
137
  - [HandleScope](doc/handle_scope.md)
137
138
  - [EscapableHandleScope](doc/escapable_handle_scope.md)
@@ -152,14 +153,14 @@ The following is the documentation for node-addon-api.
152
153
 
153
154
  Are you new to **node-addon-api**? Take a look at our **[examples](https://github.com/nodejs/node-addon-examples)**
154
155
 
155
- - **[Hello World](https://github.com/nodejs/node-addon-examples/tree/HEAD/1_hello_world/node-addon-api)**
156
- - **[Pass arguments to a function](https://github.com/nodejs/node-addon-examples/tree/HEAD/2_function_arguments/node-addon-api)**
157
- - **[Callbacks](https://github.com/nodejs/node-addon-examples/tree/HEAD/3_callbacks/node-addon-api)**
158
- - **[Object factory](https://github.com/nodejs/node-addon-examples/tree/HEAD/4_object_factory/node-addon-api)**
159
- - **[Function factory](https://github.com/nodejs/node-addon-examples/tree/HEAD/5_function_factory/node-addon-api)**
160
- - **[Wrapping C++ Object](https://github.com/nodejs/node-addon-examples/tree/HEAD/6_object_wrap/node-addon-api)**
161
- - **[Factory of wrapped object](https://github.com/nodejs/node-addon-examples/tree/HEAD/7_factory_wrap/node-addon-api)**
162
- - **[Passing wrapped object around](https://github.com/nodejs/node-addon-examples/tree/HEAD/8_passing_wrapped/node-addon-api)**
156
+ - **[Hello World](https://github.com/nodejs/node-addon-examples/tree/main/src/1-getting-started/1_hello_world)**
157
+ - **[Pass arguments to a function](https://github.com/nodejs/node-addon-examples/tree/main/src/1-getting-started/2_function_arguments/node-addon-api)**
158
+ - **[Callbacks](https://github.com/nodejs/node-addon-examples/tree/main/src/1-getting-started/3_callbacks/node-addon-api)**
159
+ - **[Object factory](https://github.com/nodejs/node-addon-examples/tree/main/src/1-getting-started/4_object_factory/node-addon-api)**
160
+ - **[Function factory](https://github.com/nodejs/node-addon-examples/tree/main/src/1-getting-started/5_function_factory/node-addon-api)**
161
+ - **[Wrapping C++ Object](https://github.com/nodejs/node-addon-examples/tree/main/src/1-getting-started/6_object_wrap/node-addon-api)**
162
+ - **[Factory of wrapped object](https://github.com/nodejs/node-addon-examples/tree/main/src/1-getting-started/7_factory_wrap/node-addon-api)**
163
+ - **[Passing wrapped object around](https://github.com/nodejs/node-addon-examples/tree/main/src/2-js-to-native-conversion/8_passing_wrapped/node-addon-api)**
163
164
 
164
165
  <a name="tests"></a>
165
166
 
@@ -275,6 +276,7 @@ available:
275
276
  ![Node-API v6 Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20v6%20Badge.svg)
276
277
  ![Node-API v7 Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20v7%20Badge.svg)
277
278
  ![Node-API v8 Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20v8%20Badge.svg)
279
+ ![Node-API v9 Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20v9%20Badge.svg)
278
280
  ![Node-API Experimental Version Badge](https://github.com/nodejs/abi-stable-node/blob/doc/assets/Node-API%20Experimental%20Version%20Badge.svg)
279
281
 
280
282
  ## **Contributing**
package/common.gypi CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  'variables': {
3
- 'NAPI_VERSION%': "<!(node -p \"process.versions.napi\")",
3
+ 'NAPI_VERSION%': "<!(node -p \"process.env.NAPI_VERSION || process.versions.napi\")",
4
4
  'disable_deprecated': "<!(node -p \"process.env['npm_config_disable_deprecated']\")"
5
5
  },
6
6
  'conditions': [
@@ -15,7 +15,6 @@
15
15
  }
16
16
  }]
17
17
  ],
18
- 'include_dirs': ["<!(node -p \"require('../').include_dir\")"],
19
18
  'cflags': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ],
20
19
  'cflags_cc': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ]
21
20
  }
package/index.js CHANGED
@@ -5,7 +5,8 @@ const includeDir = path.relative('.', __dirname);
5
5
  module.exports = {
6
6
  include: `"${__dirname}"`, // deprecated, can be removed as part of 4.0.0
7
7
  include_dir: includeDir,
8
- gyp: path.join(includeDir, 'node_api.gyp:nothing'),
8
+ gyp: path.join(includeDir, 'node_api.gyp:nothing'), // deprecated.
9
+ targets: path.join(includeDir, 'node_addon_api.gyp'),
9
10
  isNodeApiBuiltin: true,
10
11
  needsFlag: false
11
12
  };
package/napi-inl.h CHANGED
@@ -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);
@@ -165,7 +165,7 @@ napi_value TemplatedInstanceCallback(napi_env env,
165
165
  return details::WrapCallback([&] {
166
166
  CallbackInfo cbInfo(env, info);
167
167
  T* instance = T::Unwrap(cbInfo.This().As<Object>());
168
- return (instance->*UnwrapCallback)(cbInfo);
168
+ return instance ? (instance->*UnwrapCallback)(cbInfo) : Napi::Value();
169
169
  });
170
170
  }
171
171
 
@@ -175,7 +175,7 @@ napi_value TemplatedInstanceVoidCallback(napi_env env, napi_callback_info info)
175
175
  return details::WrapCallback([&] {
176
176
  CallbackInfo cbInfo(env, info);
177
177
  T* instance = T::Unwrap(cbInfo.This().As<Object>());
178
- (instance->*UnwrapCallback)(cbInfo);
178
+ if (instance) (instance->*UnwrapCallback)(cbInfo);
179
179
  return nullptr;
180
180
  });
181
181
  }
@@ -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
  ////////////////////////////////////////////////////////////////////////////////
@@ -2937,6 +2921,22 @@ inline Error::Error(napi_env env, napi_value value)
2937
2921
  nullptr};
2938
2922
 
2939
2923
  status = napi_define_properties(env, wrappedErrorObj, 1, &wrapObjFlag);
2924
+ #ifdef NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS
2925
+ if (status == napi_pending_exception) {
2926
+ // Test if the pending exception was reported because the environment is
2927
+ // shutting down. We assume that a status of napi_pending_exception
2928
+ // coupled with the absence of an actual pending exception means that
2929
+ // the environment is shutting down. If so, we replace the
2930
+ // napi_pending_exception status with napi_ok.
2931
+ bool is_exception_pending = false;
2932
+ status = napi_is_exception_pending(env, &is_exception_pending);
2933
+ if (status == napi_ok && !is_exception_pending) {
2934
+ status = napi_ok;
2935
+ } else {
2936
+ status = napi_pending_exception;
2937
+ }
2938
+ }
2939
+ #endif // NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS
2940
2940
  NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_define_properties");
2941
2941
 
2942
2942
  // Create a reference on the newly wrapped object
@@ -3141,6 +3141,23 @@ inline RangeError::RangeError() : Error() {}
3141
3141
  inline RangeError::RangeError(napi_env env, napi_value value)
3142
3142
  : Error(env, value) {}
3143
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
+
3144
3161
  ////////////////////////////////////////////////////////////////////////////////
3145
3162
  // Reference<T> class
3146
3163
  ////////////////////////////////////////////////////////////////////////////////
@@ -4340,7 +4357,7 @@ inline napi_value InstanceWrap<T>::InstanceVoidMethodCallbackWrapper(
4340
4357
  callbackInfo.SetData(callbackData->data);
4341
4358
  T* instance = T::Unwrap(callbackInfo.This().As<Object>());
4342
4359
  auto cb = callbackData->callback;
4343
- (instance->*cb)(callbackInfo);
4360
+ if (instance) (instance->*cb)(callbackInfo);
4344
4361
  return nullptr;
4345
4362
  });
4346
4363
  }
@@ -4355,7 +4372,7 @@ inline napi_value InstanceWrap<T>::InstanceMethodCallbackWrapper(
4355
4372
  callbackInfo.SetData(callbackData->data);
4356
4373
  T* instance = T::Unwrap(callbackInfo.This().As<Object>());
4357
4374
  auto cb = callbackData->callback;
4358
- return (instance->*cb)(callbackInfo);
4375
+ return instance ? (instance->*cb)(callbackInfo) : Napi::Value();
4359
4376
  });
4360
4377
  }
4361
4378
 
@@ -4369,7 +4386,7 @@ inline napi_value InstanceWrap<T>::InstanceGetterCallbackWrapper(
4369
4386
  callbackInfo.SetData(callbackData->data);
4370
4387
  T* instance = T::Unwrap(callbackInfo.This().As<Object>());
4371
4388
  auto cb = callbackData->getterCallback;
4372
- return (instance->*cb)(callbackInfo);
4389
+ return instance ? (instance->*cb)(callbackInfo) : Napi::Value();
4373
4390
  });
4374
4391
  }
4375
4392
 
@@ -4383,7 +4400,7 @@ inline napi_value InstanceWrap<T>::InstanceSetterCallbackWrapper(
4383
4400
  callbackInfo.SetData(callbackData->data);
4384
4401
  T* instance = T::Unwrap(callbackInfo.This().As<Object>());
4385
4402
  auto cb = callbackData->setterCallback;
4386
- (instance->*cb)(callbackInfo, callbackInfo[0]);
4403
+ if (instance) (instance->*cb)(callbackInfo, callbackInfo[0]);
4387
4404
  return nullptr;
4388
4405
  });
4389
4406
  }
@@ -4395,7 +4412,7 @@ inline napi_value InstanceWrap<T>::WrappedMethod(
4395
4412
  return details::WrapCallback([&] {
4396
4413
  const CallbackInfo cbInfo(env, info);
4397
4414
  T* instance = T::Unwrap(cbInfo.This().As<Object>());
4398
- (instance->*method)(cbInfo, cbInfo[0]);
4415
+ if (instance) (instance->*method)(cbInfo, cbInfo[0]);
4399
4416
  return nullptr;
4400
4417
  });
4401
4418
  }
@@ -6094,13 +6111,15 @@ inline void ThreadSafeFunction::CallJS(napi_env env,
6094
6111
  return;
6095
6112
  }
6096
6113
 
6097
- if (data != nullptr) {
6098
- auto* callbackWrapper = static_cast<CallbackWrapper*>(data);
6099
- (*callbackWrapper)(env, Function(env, jsCallback));
6100
- delete callbackWrapper;
6101
- } else if (jsCallback != nullptr) {
6102
- Function(env, jsCallback).Call({});
6103
- }
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
+ });
6104
6123
  }
6105
6124
 
6106
6125
  ////////////////////////////////////////////////////////////////////////////////
package/napi.h CHANGED
@@ -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
+ }
package/package.json CHANGED
@@ -399,6 +399,22 @@
399
399
  {
400
400
  "name": "Feng Yu",
401
401
  "url": "https://github.com/F3n67u"
402
+ },
403
+ {
404
+ "name": "wanlu wang",
405
+ "url": "https://github.com/wanlu"
406
+ },
407
+ {
408
+ "name": "Caleb Hearon",
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"
402
418
  }
403
419
  ],
404
420
  "description": "Node.js API (Node-API)",
@@ -412,7 +428,7 @@
412
428
  "eslint-plugin-import": "^2.24.2",
413
429
  "eslint-plugin-node": "^11.1.0",
414
430
  "eslint-plugin-promise": "^5.1.0",
415
- "fs-extra": "^9.0.1",
431
+ "fs-extra": "^11.1.1",
416
432
  "path": "^0.12.7",
417
433
  "pre-commit": "^1.2.2",
418
434
  "safe-buffer": "^5.1.1"
@@ -459,6 +475,9 @@
459
475
  "lint:fix": "node tools/clang-format --fix && node tools/eslint-format --fix"
460
476
  },
461
477
  "pre-commit": "lint",
462
- "version": "6.1.0",
463
- "support": true
478
+ "version": "7.1.0",
479
+ "support": true,
480
+ "engines": {
481
+ "node": "^16 || ^18 || >= 20"
482
+ }
464
483
  }