node-addon-api 8.6.0 → 8.7.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/README.md CHANGED
@@ -19,7 +19,7 @@ and exception handling semantics with low overhead.
19
19
  API references are available in the [doc](doc/README.md) directory.
20
20
 
21
21
  <!-- x-release-please-start-version -->
22
- ## Current version: 8.6.0
22
+ ## Current version: 8.7.0
23
23
  <!-- x-release-please-end -->
24
24
 
25
25
  (See [CHANGELOG.md](CHANGELOG.md) for complete Changelog)
package/napi-inl.h CHANGED
@@ -1199,6 +1199,13 @@ inline Date Date::New(napi_env env, double val) {
1199
1199
  return Date(env, value);
1200
1200
  }
1201
1201
 
1202
+ inline Date Date::New(napi_env env, std::chrono::system_clock::time_point tp) {
1203
+ using namespace std::chrono;
1204
+ auto ms = static_cast<double>(
1205
+ duration_cast<milliseconds>(tp.time_since_epoch()).count());
1206
+ return Date::New(env, ms);
1207
+ }
1208
+
1202
1209
  inline void Date::CheckCast(napi_env env, napi_value value) {
1203
1210
  NAPI_CHECK(value != nullptr, "Date::CheckCast", "empty value");
1204
1211
 
@@ -1966,6 +1973,19 @@ inline MaybeOrValue<bool> Object::Seal() const {
1966
1973
  }
1967
1974
  #endif // NAPI_VERSION >= 8
1968
1975
 
1976
+ inline MaybeOrValue<Object> Object::GetPrototype() const {
1977
+ napi_value result;
1978
+ napi_status status = napi_get_prototype(_env, _value, &result);
1979
+ NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, Object(_env, result), Object);
1980
+ }
1981
+
1982
+ #ifdef NODE_API_EXPERIMENTAL_HAS_SET_PROTOTYPE
1983
+ inline MaybeOrValue<bool> Object::SetPrototype(const Object& value) const {
1984
+ napi_status status = node_api_set_prototype(_env, _value, value);
1985
+ NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool);
1986
+ }
1987
+ #endif
1988
+
1969
1989
  ////////////////////////////////////////////////////////////////////////////////
1970
1990
  // External class
1971
1991
  ////////////////////////////////////////////////////////////////////////////////
@@ -2289,6 +2309,39 @@ inline DataView DataView::New(napi_env env,
2289
2309
  return DataView(env, value);
2290
2310
  }
2291
2311
 
2312
+ #ifdef NODE_API_EXPERIMENTAL_HAS_SHAREDARRAYBUFFER
2313
+ inline DataView DataView::New(napi_env env,
2314
+ Napi::SharedArrayBuffer arrayBuffer) {
2315
+ return New(env, arrayBuffer, 0, arrayBuffer.ByteLength());
2316
+ }
2317
+
2318
+ inline DataView DataView::New(napi_env env,
2319
+ Napi::SharedArrayBuffer arrayBuffer,
2320
+ size_t byteOffset) {
2321
+ if (byteOffset > arrayBuffer.ByteLength()) {
2322
+ NAPI_THROW(RangeError::New(
2323
+ env, "Start offset is outside the bounds of the buffer"),
2324
+ DataView());
2325
+ }
2326
+ return New(
2327
+ env, arrayBuffer, byteOffset, arrayBuffer.ByteLength() - byteOffset);
2328
+ }
2329
+
2330
+ inline DataView DataView::New(napi_env env,
2331
+ Napi::SharedArrayBuffer arrayBuffer,
2332
+ size_t byteOffset,
2333
+ size_t byteLength) {
2334
+ if (byteOffset + byteLength > arrayBuffer.ByteLength()) {
2335
+ NAPI_THROW(RangeError::New(env, "Invalid DataView length"), DataView());
2336
+ }
2337
+ napi_value value;
2338
+ napi_status status =
2339
+ napi_create_dataview(env, byteLength, arrayBuffer, byteOffset, &value);
2340
+ NAPI_THROW_IF_FAILED(env, status, DataView());
2341
+ return DataView(env, value);
2342
+ }
2343
+ #endif // NODE_API_EXPERIMENTAL_HAS_SHAREDARRAYBUFFER
2344
+
2292
2345
  inline void DataView::CheckCast(napi_env env, napi_value value) {
2293
2346
  NAPI_CHECK(value != nullptr, "DataView::CheckCast", "empty value");
2294
2347
 
@@ -2312,6 +2365,10 @@ inline DataView::DataView(napi_env env, napi_value value) : Object(env, value) {
2312
2365
  }
2313
2366
 
2314
2367
  inline Napi::ArrayBuffer DataView::ArrayBuffer() const {
2368
+ return Buffer().As<Napi::ArrayBuffer>();
2369
+ }
2370
+
2371
+ inline Napi::Value DataView::Buffer() const {
2315
2372
  napi_value arrayBuffer;
2316
2373
  napi_status status = napi_get_dataview_info(_env,
2317
2374
  _value /* dataView */,
@@ -2319,8 +2376,8 @@ inline Napi::ArrayBuffer DataView::ArrayBuffer() const {
2319
2376
  nullptr /* data */,
2320
2377
  &arrayBuffer /* arrayBuffer */,
2321
2378
  nullptr /* byteOffset */);
2322
- NAPI_THROW_IF_FAILED(_env, status, Napi::ArrayBuffer());
2323
- return Napi::ArrayBuffer(_env, arrayBuffer);
2379
+ NAPI_THROW_IF_FAILED(_env, status, Napi::Value());
2380
+ return Napi::Value(_env, arrayBuffer);
2324
2381
  }
2325
2382
 
2326
2383
  inline size_t DataView::ByteOffset() const {
@@ -3809,8 +3866,8 @@ inline MaybeOrValue<bool> ObjectReference::Set(const std::string& utf8name,
3809
3866
  return Value().Set(utf8name, value);
3810
3867
  }
3811
3868
 
3812
- inline MaybeOrValue<bool> ObjectReference::Set(const std::string& utf8name,
3813
- std::string& utf8value) const {
3869
+ inline MaybeOrValue<bool> ObjectReference::Set(
3870
+ const std::string& utf8name, const std::string& utf8value) const {
3814
3871
  HandleScope scope(_env);
3815
3872
  return Value().Set(utf8name, utf8value);
3816
3873
  }
package/napi.h CHANGED
@@ -17,6 +17,7 @@
17
17
  #if NAPI_HAS_THREADS
18
18
  #include <mutex>
19
19
  #endif // NAPI_HAS_THREADS
20
+ #include <chrono>
20
21
  #include <string>
21
22
  #include <vector>
22
23
 
@@ -359,10 +360,10 @@ class BasicEnv {
359
360
  // ... occurs when comparing foo.Env() == bar.Env() or foo.Env() == nullptr
360
361
  bool operator==(const BasicEnv& other) const {
361
362
  return _env == other._env;
362
- };
363
+ }
363
364
  bool operator==(std::nullptr_t /*other*/) const {
364
365
  return _env == nullptr;
365
- };
366
+ }
366
367
 
367
368
  #if NAPI_VERSION > 2
368
369
  template <typename Hook, typename Arg = void>
@@ -685,6 +686,12 @@ class Date : public Value {
685
686
  double value ///< Number value
686
687
  );
687
688
 
689
+ /// Creates a new Date value from a std::chrono::system_clock::time_point.
690
+ static Date New(
691
+ napi_env env, ///< Node-API environment
692
+ std::chrono::system_clock::time_point time_point ///< Time point value
693
+ );
694
+
688
695
  static void CheckCast(napi_env env, napi_value value);
689
696
 
690
697
  Date(); ///< Creates a new _empty_ Date instance.
@@ -1118,6 +1125,12 @@ class Object : public TypeTaggable {
1118
1125
  /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof
1119
1126
  MaybeOrValue<bool> Seal() const;
1120
1127
  #endif // NAPI_VERSION >= 8
1128
+
1129
+ MaybeOrValue<Object> GetPrototype() const;
1130
+
1131
+ #ifdef NODE_API_EXPERIMENTAL_HAS_SET_PROTOTYPE
1132
+ MaybeOrValue<bool> SetPrototype(const Object& value) const;
1133
+ #endif
1121
1134
  };
1122
1135
 
1123
1136
  template <typename T>
@@ -1450,13 +1463,37 @@ class DataView : public Object {
1450
1463
  size_t byteOffset,
1451
1464
  size_t byteLength);
1452
1465
 
1466
+ #ifdef NODE_API_EXPERIMENTAL_HAS_SHAREDARRAYBUFFER
1467
+ static DataView New(napi_env env, Napi::SharedArrayBuffer arrayBuffer);
1468
+ static DataView New(napi_env env,
1469
+ Napi::SharedArrayBuffer arrayBuffer,
1470
+ size_t byteOffset);
1471
+ static DataView New(napi_env env,
1472
+ Napi::SharedArrayBuffer arrayBuffer,
1473
+ size_t byteOffset,
1474
+ size_t byteLength);
1475
+ #endif
1476
+
1453
1477
  static void CheckCast(napi_env env, napi_value value);
1454
1478
 
1455
1479
  DataView(); ///< Creates a new _empty_ DataView instance.
1456
1480
  DataView(napi_env env,
1457
1481
  napi_value value); ///< Wraps a Node-API value primitive.
1458
1482
 
1459
- Napi::ArrayBuffer ArrayBuffer() const; ///< Gets the backing array buffer.
1483
+ // Gets the backing `ArrayBuffer`.
1484
+ //
1485
+ // If this `DataView` is not backed by an `ArrayBuffer`, this method will
1486
+ // terminate the process with a fatal error when using
1487
+ // `NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS` or exhibit undefined behavior
1488
+ // otherwise. Use `Buffer()` instead to get the backing buffer without
1489
+ // assuming its type.
1490
+ Napi::ArrayBuffer ArrayBuffer() const;
1491
+
1492
+ // Gets the backing buffer (an `ArrayBuffer` or `SharedArrayBuffer`).
1493
+ //
1494
+ // Use `IsArrayBuffer()` or `IsSharedArrayBuffer()` to check the type of the
1495
+ // backing buffer prior to casting with `As<T>()`.
1496
+ Napi::Value Buffer() const;
1460
1497
  size_t ByteOffset()
1461
1498
  const; ///< Gets the offset into the buffer where the array starts.
1462
1499
  size_t ByteLength() const; ///< Gets the length of the array in bytes.
@@ -1733,7 +1770,7 @@ class ObjectReference : public Reference<Object> {
1733
1770
  MaybeOrValue<bool> Set(const std::string& utf8name, napi_value value) const;
1734
1771
  MaybeOrValue<bool> Set(const std::string& utf8name, Napi::Value value) const;
1735
1772
  MaybeOrValue<bool> Set(const std::string& utf8name,
1736
- std::string& utf8value) const;
1773
+ const std::string& utf8value) const;
1737
1774
  MaybeOrValue<bool> Set(const std::string& utf8name, bool boolValue) const;
1738
1775
  MaybeOrValue<bool> Set(const std::string& utf8name, double numberValue) const;
1739
1776
 
@@ -3126,8 +3163,8 @@ class AsyncProgressWorkerBase : public AsyncWorker {
3126
3163
 
3127
3164
  AsyncProgressWorkerBase* asyncprogressworker() {
3128
3165
  return _asyncprogressworker;
3129
- };
3130
- DataType* data() { return _data; };
3166
+ }
3167
+ DataType* data() { return _data; }
3131
3168
 
3132
3169
  private:
3133
3170
  AsyncProgressWorkerBase* _asyncprogressworker;
package/package.json CHANGED
@@ -472,7 +472,7 @@
472
472
  "lint:fix": "eslint --fix && node tools/clang-format --fix"
473
473
  },
474
474
  "pre-commit": "lint",
475
- "version": "8.6.0",
475
+ "version": "8.7.0",
476
476
  "support": true,
477
477
  "engines": {
478
478
  "node": "^18 || ^20 || >= 21"