koffi 0.9.30 → 0.9.33

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 (60) hide show
  1. package/package.json +1 -1
  2. package/src/call.hh +2 -1
  3. package/src/call_arm32.cc +27 -15
  4. package/src/call_arm64.cc +4 -9
  5. package/src/call_x64_sysv.cc +4 -9
  6. package/src/call_x64_sysv_fwd.S +13 -3
  7. package/src/call_x64_win.cc +4 -9
  8. package/src/call_x86.cc +18 -16
  9. package/src/ffi.cc +93 -46
  10. package/src/ffi.hh +11 -6
  11. package/src/util.cc +35 -1
  12. package/src/util.hh +2 -0
  13. package/test/tests/misc.c +62 -1
  14. package/test/tests/misc.js +49 -1
  15. package/vendor/node-addon-api/CHANGELOG.md +57 -0
  16. package/vendor/node-addon-api/README.md +3 -3
  17. package/vendor/node-addon-api/doc/array_buffer.md +2 -2
  18. package/vendor/node-addon-api/doc/buffer.md +2 -2
  19. package/vendor/node-addon-api/doc/class_property_descriptor.md +13 -5
  20. package/vendor/node-addon-api/doc/env.md +5 -5
  21. package/vendor/node-addon-api/doc/object.md +12 -33
  22. package/vendor/node-addon-api/doc/object_reference.md +1 -1
  23. package/vendor/node-addon-api/doc/object_wrap.md +27 -0
  24. package/vendor/node-addon-api/doc/reference.md +2 -2
  25. package/vendor/node-addon-api/doc/threadsafe_function.md +3 -3
  26. package/vendor/node-addon-api/doc/typed_threadsafe_function.md +2 -2
  27. package/vendor/node-addon-api/napi-inl.h +111 -61
  28. package/vendor/node-addon-api/napi.h +79 -59
  29. package/vendor/node-addon-api/package.json +22 -6
  30. package/vendor/node-addon-api/test/async_context.cc +15 -0
  31. package/vendor/node-addon-api/test/async_context.js +69 -33
  32. package/vendor/node-addon-api/test/binding.cc +2 -0
  33. package/vendor/node-addon-api/test/binding.gyp +7 -0
  34. package/vendor/node-addon-api/test/common/index.js +23 -22
  35. package/vendor/node-addon-api/test/common/test_helper.h +10 -0
  36. package/vendor/node-addon-api/test/error_terminating_environment.js +1 -0
  37. package/vendor/node-addon-api/test/function.cc +29 -0
  38. package/vendor/node-addon-api/test/function.js +35 -23
  39. package/vendor/node-addon-api/test/index.js +40 -17
  40. package/vendor/node-addon-api/test/object/object.cc +2 -0
  41. package/vendor/node-addon-api/test/object/set_property.cc +8 -0
  42. package/vendor/node-addon-api/test/object/set_property.js +6 -5
  43. package/vendor/node-addon-api/test/object/subscript_operator.cc +19 -3
  44. package/vendor/node-addon-api/test/objectwrap_function.cc +45 -0
  45. package/vendor/node-addon-api/test/objectwrap_function.js +22 -0
  46. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_sum.cc +1 -1
  47. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_ctx.js +2 -2
  48. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_sum.cc +1 -1
  49. package/vendor/node-addon-api/tools/clang-format.js +4 -1
  50. package/vendor/node-addon-api/unit-test/README.md +28 -0
  51. package/vendor/node-addon-api/unit-test/binding-file-template.js +39 -0
  52. package/vendor/node-addon-api/unit-test/binding.gyp +72 -0
  53. package/vendor/node-addon-api/unit-test/exceptions.js +32 -0
  54. package/vendor/node-addon-api/unit-test/generate-binding-cc.js +61 -0
  55. package/vendor/node-addon-api/unit-test/injectTestParams.js +101 -0
  56. package/vendor/node-addon-api/unit-test/listOfTestModules.js +88 -0
  57. package/vendor/node-addon-api/unit-test/matchModules.js +65 -0
  58. package/vendor/node-addon-api/unit-test/setup.js +13 -0
  59. package/vendor/node-addon-api/unit-test/spawnTask.js +26 -0
  60. package/vendor/node-addon-api/unit-test/test.js +30 -0
package/src/util.cc CHANGED
@@ -20,6 +20,40 @@
20
20
 
21
21
  namespace RG {
22
22
 
23
+ const TypeInfo *ResolveType(const InstanceData *instance, Napi::Value value, int *out_directions)
24
+ {
25
+ if (value.IsString()) {
26
+ std::string str = value.As<Napi::String>();
27
+
28
+ const TypeInfo *type = instance->types_map.FindValue(str.c_str(), nullptr);
29
+
30
+ if (!type) {
31
+ ThrowError<Napi::TypeError>(value.Env(), "Unknown type string '%1'", str.c_str());
32
+ return nullptr;
33
+ }
34
+
35
+ if (out_directions) {
36
+ *out_directions = 1;
37
+ }
38
+ return type;
39
+ } else if (CheckValueTag(instance, value, &TypeInfoMarker)) {
40
+ Napi::External<TypeInfo> external = value.As<Napi::External<TypeInfo>>();
41
+
42
+ const TypeInfo *raw = external.Data();
43
+ const TypeInfo *type = AlignDown(raw, 4);
44
+ RG_ASSERT(type);
45
+
46
+ if (out_directions) {
47
+ Size delta = (uint8_t *)raw - (uint8_t *)type;
48
+ *out_directions = 1 + (int)delta;
49
+ }
50
+ return type;
51
+ } else {
52
+ ThrowError<Napi::TypeError>(value.Env(), "Unexpected %1 value as type specifier, expected string or type", GetValueType(instance, value));
53
+ return nullptr;
54
+ }
55
+ }
56
+
23
57
  const char *GetValueType(const InstanceData *instance, Napi::Value value)
24
58
  {
25
59
  for (const TypeInfo &type: instance->types) {
@@ -327,7 +361,7 @@ static void DumpMemory(const char *type, Span<const uint8_t> bytes)
327
361
 
328
362
  void CallData::DumpDebug() const
329
363
  {
330
- PrintLn(stderr, "%!..+---- %1 ----%!0", func->name);
364
+ PrintLn(stderr, "%!..+---- %1 (%2) ----%!0", func->name, CallConventionNames[(int)func->convention]);
331
365
 
332
366
  if (func->parameters.len) {
333
367
  PrintLn(stderr, "Parameters:");
package/src/util.hh CHANGED
@@ -51,6 +51,8 @@ static inline T *AlignDown(T *ptr, Size align)
51
51
  return (T *)aligned;
52
52
  }
53
53
 
54
+ const TypeInfo *ResolveType(const InstanceData *instance, Napi::Value value, int *out_directions = nullptr);
55
+
54
56
  // Can be slow, only use for error messages
55
57
  const char *GetValueType(const InstanceData *instance, Napi::Value value);
56
58
 
package/test/tests/misc.c CHANGED
@@ -15,6 +15,7 @@
15
15
  #include <stdio.h>
16
16
  #include <inttypes.h>
17
17
  #include <string.h>
18
+ #include <stdarg.h>
18
19
 
19
20
  #ifdef _WIN32
20
21
  #define EXPORT __declspec(dllexport)
@@ -34,6 +35,13 @@
34
35
  #define STDCALL
35
36
  #endif
36
37
 
38
+ typedef struct Pack1 {
39
+ int a;
40
+ } Pack1;
41
+ typedef struct Pack2 {
42
+ int a;
43
+ int b;
44
+ } Pack2;
37
45
  typedef struct Pack3 {
38
46
  int a;
39
47
  int b;
@@ -69,6 +77,47 @@ typedef struct PackedBFG {
69
77
  } PackedBFG;
70
78
  #pragma pack(pop)
71
79
 
80
+ EXPORT void FillPack1(int a, Pack1 *p)
81
+ {
82
+ p->a = a;
83
+ }
84
+
85
+ EXPORT Pack1 RetPack1(int a)
86
+ {
87
+ Pack1 p;
88
+
89
+ p.a = a;
90
+
91
+ return p;
92
+ }
93
+
94
+ EXPORT void FASTCALL AddPack1(int a, Pack1 *p)
95
+ {
96
+ p->a += a;
97
+ }
98
+
99
+ EXPORT void FillPack2(int a, int b, Pack2 *p)
100
+ {
101
+ p->a = a;
102
+ p->b = b;
103
+ }
104
+
105
+ EXPORT Pack2 RetPack2(int a, int b)
106
+ {
107
+ Pack2 p;
108
+
109
+ p.a = a;
110
+ p.b = b;
111
+
112
+ return p;
113
+ }
114
+
115
+ EXPORT void FASTCALL AddPack2(int a, int b, Pack2 *p)
116
+ {
117
+ p->a += a;
118
+ p->b += b;
119
+ }
120
+
72
121
  EXPORT void FillPack3(int a, int b, int c, Pack3 *p)
73
122
  {
74
123
  p->a = a;
@@ -79,7 +128,7 @@ EXPORT void FillPack3(int a, int b, int c, Pack3 *p)
79
128
  EXPORT Pack3 RetPack3(int a, int b, int c)
80
129
  {
81
130
  Pack3 p;
82
-
131
+
83
132
  p.a = a;
84
133
  p.b = b;
85
134
  p.c = c;
@@ -197,3 +246,15 @@ EXPORT const char *ReturnBigString(const char *str)
197
246
 
198
247
  return buf;
199
248
  }
249
+
250
+ EXPORT const char *PrintFmt(const char *fmt, ...)
251
+ {
252
+ static char buf[256];
253
+
254
+ va_list ap;
255
+ va_start(ap, fmt);
256
+ vsnprintf(buf, sizeof(buf), fmt, ap);
257
+ va_end(ap);
258
+
259
+ return buf;
260
+ }
@@ -17,6 +17,13 @@ const koffi = require('../build/koffi.node');
17
17
  const assert = require('assert');
18
18
  const path = require('path');
19
19
 
20
+ const Pack1 = koffi.struct('Pack1', {
21
+ a: 'int'
22
+ });
23
+ const Pack2 = koffi.struct('Pack2', {
24
+ a: 'int',
25
+ b: 'int'
26
+ });
20
27
  const Pack3 = koffi.struct('Pack3', {
21
28
  a: 'int',
22
29
  b: 'int',
@@ -62,6 +69,12 @@ async function test() {
62
69
  let lib_filename = path.dirname(__filename) + '/../build/misc' + koffi.extension;
63
70
  let lib = koffi.load(lib_filename);
64
71
 
72
+ const FillPack1 = lib.cdecl('FillPack1', 'void', ['int', koffi.out(koffi.pointer(Pack1))]);
73
+ const RetPack1 = lib.cdecl('RetPack1', Pack1, ['int']);
74
+ const AddPack1 = lib.fastcall('AddPack1', 'void', ['int', koffi.inout(koffi.pointer(Pack1))]);
75
+ const FillPack2 = lib.cdecl('FillPack2', 'void', ['int', 'int', koffi.out(koffi.pointer(Pack2))]);
76
+ const RetPack2 = lib.cdecl('RetPack2', Pack2, ['int', 'int']);
77
+ const AddPack2 = lib.fastcall('AddPack2', 'void', ['int', 'int', koffi.inout(koffi.pointer(Pack2))]);
65
78
  const FillPack3 = lib.cdecl('FillPack3', 'void', ['int', 'int', 'int', koffi.out(koffi.pointer(Pack3))]);
66
79
  const RetPack3 = lib.cdecl('RetPack3', Pack3, ['int', 'int', 'int']);
67
80
  const AddPack3 = lib.fastcall('AddPack3', 'void', ['int', 'int', 'int', koffi.inout(koffi.pointer(Pack3))]);
@@ -74,8 +87,37 @@ async function test() {
74
87
  const MakeBFG = lib.stdcall('MakeBFG', BFG, [koffi.out(koffi.pointer(BFG)), 'int', 'double', 'string']);
75
88
  const MakePackedBFG = lib.fastcall('MakePackedBFG', PackedBFG, ['int', 'double', koffi.out(koffi.pointer(PackedBFG)), 'string']);
76
89
  const ReturnBigString = lib.stdcall('ReturnBigString', 'string', ['string']);
90
+ const PrintFmt = lib.cdecl('PrintFmt', 'string', ['string', '...']);
91
+
92
+ // Simple tests with Pack1
93
+ {
94
+ let p = {};
95
+
96
+ FillPack1(777, p);
97
+ assert.deepEqual(p, { a: 777 });
98
+
99
+ let q = RetPack1(6);
100
+ assert.deepEqual(q, { a: 6 });
101
+
102
+ AddPack1(6, p);
103
+ assert.deepEqual(p, { a: 783 });
104
+ }
77
105
 
78
- // Simple tests
106
+ // Simple tests with Pack2
107
+ {
108
+ let p = {};
109
+
110
+ FillPack2(123, 456, p);
111
+ assert.deepEqual(p, { a: 123, b: 456 });
112
+
113
+ let q = RetPack2(6, 9);
114
+ assert.deepEqual(q, { a: 6, b: 9 });
115
+
116
+ AddPack2(6, 9, p);
117
+ assert.deepEqual(p, { a: 129, b: 465 });
118
+ }
119
+
120
+ // Simple tests with Pack3
79
121
  {
80
122
  let p = {};
81
123
 
@@ -120,4 +162,10 @@ async function test() {
120
162
  let str = 'fooBAR!'.repeat(1024 * 1024);
121
163
  assert.equal(ReturnBigString(str), str);
122
164
  }
165
+
166
+ // Variadic
167
+ {
168
+ let str = PrintFmt('foo %d %g %s', 'int', 200, 'double', 1.5, 'string', 'BAR');
169
+ assert.equal(str, 'foo 200 1.5 BAR');
170
+ }
123
171
  }
@@ -1,5 +1,62 @@
1
1
  # node-addon-api Changelog
2
2
 
3
+ ## 2022-05-02 Version 5.0.0, @NickNaso
4
+
5
+ ### Notable changes:
6
+
7
+ #### API
8
+ - Marked methods of wrapper classes `const`.
9
+ - Enabled wrapping `Napi` namespace with custom namespace.
10
+ - Added an override to `Napi::Function::Call` to call it with a c-style array
11
+ of `Napi::Value`'s.
12
+ - Some other minor fixes.
13
+
14
+ #### TEST
15
+
16
+ - Improved the test framework. Added the possibility to run subsets of tests
17
+ more easily.
18
+ - Added test for `Napi::AsyncContext` class.
19
+ - Fixed ramdom failure on test for `Napi::ThreadSafeFunction` e
20
+ `Napi::TypedThreadSafeFunction` class.
21
+ - Fixed compilation problem on debian 8 system.
22
+ - Added test for `Napi::Object::Set()` method.
23
+
24
+ ### Documentation
25
+ - Added some clarifications for `Napi::ClassPropertyDescriptor`.
26
+ - Added clarification about weak reference for `Napi::ObjectWrap`.
27
+ - Some minor fixes all over the documentation.
28
+
29
+ ### TOOL
30
+
31
+ - Fixed `eslint` configuration.
32
+ - Fixed CI configuration for Windows.
33
+ - Enabled pre-commit `ClangFormat` on Windows.
34
+
35
+ ### Commits
36
+
37
+ * \[[`f32db917f3`](https://github.com/nodejs/node-addon-api/commit/f32db917f3)] - Add test coverage for async contexts (#1164) (Jack)
38
+ * \[[`24455f88af`](https://github.com/nodejs/node-addon-api/commit/24455f88af)] - **src**: check for tsfn in conditional\_variable wait (Kevin Eady) [#1168](https://github.com/nodejs/node-addon-api/pull/1168)
39
+ * \[[`40ed7ce409`](https://github.com/nodejs/node-addon-api/commit/40ed7ce409)] - **src**: fix regression introduced by #874 (Michael Dawson)
40
+ * \[[`9bea434326`](https://github.com/nodejs/node-addon-api/commit/9bea434326)] - **doc**: added some comments to ClassPropertyDescriptor. (#1149) (Nicola Del Gobbo)
41
+ * \[[`57c212e15f`](https://github.com/nodejs/node-addon-api/commit/57c212e15f)] - **buld**: Enable running pre-commit ClangFormat on Win (Vladimir Morozov)
42
+ * \[[`8c46a9501a`](https://github.com/nodejs/node-addon-api/commit/8c46a9501a)] - **doc**: clarify ObjectWrap weak ref behavior (#1155) (Alba Mendez)
43
+ * \[[`01274966d5`](https://github.com/nodejs/node-addon-api/commit/01274966d5)] - **build**: run Windows CI only on nondeprecated build configurations (#1152) (Darshan Sen)
44
+ * \[[`b8449e17e0`](https://github.com/nodejs/node-addon-api/commit/b8449e17e0)] - **src**: mark methods of wrapper classes const (Nikolai Vavilov) [#874](https://github.com/nodejs/node-addon-api/pull/874)
45
+ * \[[`5e2c1f24f8`](https://github.com/nodejs/node-addon-api/commit/5e2c1f24f8)] - **lint**: set sourceType to 'script' (#1141) (Anna Henningsen)
46
+ * \[[`da8af20152`](https://github.com/nodejs/node-addon-api/commit/da8af20152)] - **doc**: mention Napi::Env arg for Finalization callback (#1139) (extremeheat)
47
+ * \[[`5b51864a39`](https://github.com/nodejs/node-addon-api/commit/5b51864a39)] - **src**: enable wrapping Napi namespace with custom namespace (#1135) (Anna Henningsen)
48
+ * \[[`c54aeef5fd`](https://github.com/nodejs/node-addon-api/commit/c54aeef5fd)] - Add Function::Call Napi::Value override (#1026) (rgerd)
49
+ * \[[`e906b5a7ce`](https://github.com/nodejs/node-addon-api/commit/e906b5a7ce)] - **test**: fix compilation problem on debian 8 (NickNaso) [#1138](https://github.com/nodejs/node-addon-api/pull/1138)
50
+ * \[[`5790c55784`](https://github.com/nodejs/node-addon-api/commit/5790c55784)] - **src**: do not use non-static class member for constant value (#1134) (Anna Henningsen)
51
+ * \[[`b7659db945`](https://github.com/nodejs/node-addon-api/commit/b7659db945)] - Merge pull request #1130 from meixg/main (Jack)
52
+ * \[[`a840d51d21`](https://github.com/nodejs/node-addon-api/commit/a840d51d21)] - Add test case for Object Set using uint32 as key (meixg)
53
+ * \[[`2c88a7ec4c`](https://github.com/nodejs/node-addon-api/commit/2c88a7ec4c)] - Merge pull request #1132 from JckXia/test-wfl-run (Jack)
54
+ * \[[`d3a5ed3869`](https://github.com/nodejs/node-addon-api/commit/d3a5ed3869)] - _**Revert**_ "window CI to running on 2019" (JckXia)
55
+ * \[[`cee899ade5`](https://github.com/nodejs/node-addon-api/commit/cee899ade5)] - **src**: allow customization of ObjectWrap behavior (Aaron Meriwether) [#1125](https://github.com/nodejs/node-addon-api/pull/1125)
56
+ * \[[`91879b4082`](https://github.com/nodejs/node-addon-api/commit/91879b4082)] - remove window-latest to debug (JckXia)
57
+ * \[[`1593ef46ee`](https://github.com/nodejs/node-addon-api/commit/1593ef46ee)] - Testing CI run (JckXia)
58
+ * \[[`744c8d2410`](https://github.com/nodejs/node-addon-api/commit/744c8d2410)] - **test**: enhance the test framework (Deepak Rajamohan)
59
+
3
60
  ## 2022-01-21 Version 4.3.0, @NickNaso
4
61
 
5
62
  ### Notable changes:
@@ -10,7 +10,7 @@ git branch -u origin/main main
10
10
  ```
11
11
 
12
12
  # **node-addon-api module**
13
- This module contains **header-only C++ wrapper classes** which simplify
13
+ This module contains **header-only C++ wrapper classes** which simplify
14
14
  the use of the C based [Node-API](https://nodejs.org/dist/latest/docs/api/n-api.html)
15
15
  provided by Node.js when using C++. It provides a C++ object model
16
16
  and exception handling semantics with low overhead.
@@ -70,7 +70,7 @@ and node-addon-api.
70
70
  - **[Contributors](#contributors)**
71
71
  - **[License](#license)**
72
72
 
73
- ## **Current version: 4.3.0**
73
+ ## **Current version: 5.0.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 12.x.
86
+ The oldest Node.js version supported by the current version of node-addon-api is Node.js 14.x.
87
87
 
88
88
  ## Setup
89
89
  - [Installation and usage](doc/setup.md)
@@ -67,7 +67,7 @@ static Napi::ArrayBuffer Napi::ArrayBuffer::New(napi_env env,
67
67
  - `[in] externalData`: The pointer to the external data to wrap.
68
68
  - `[in] byteLength`: The length of the `externalData`, in bytes.
69
69
  - `[in] finalizeCallback`: A function to be called when the `Napi::ArrayBuffer` is
70
- destroyed. It must implement `operator()`, accept a `void*` (which is the
70
+ destroyed. It must implement `operator()`, accept an Napi::Env, a `void*` (which is the
71
71
  `externalData` pointer), and return `void`.
72
72
 
73
73
  Returns a new `Napi::ArrayBuffer` instance.
@@ -94,7 +94,7 @@ static Napi::ArrayBuffer Napi::ArrayBuffer::New(napi_env env,
94
94
  - `[in] externalData`: The pointer to the external data to wrap.
95
95
  - `[in] byteLength`: The length of the `externalData`, in bytes.
96
96
  - `[in] finalizeCallback`: The function to be called when the `Napi::ArrayBuffer` is
97
- destroyed. It must implement `operator()`, accept a `void*` (which is the
97
+ destroyed. It must implement `operator()`, accept an Napi::Env, a `void*` (which is the
98
98
  `externalData` pointer) and `Hint*`, and return `void`.
99
99
  - `[in] finalizeHint`: The hint to be passed as the second parameter of the
100
100
  finalize callback.
@@ -65,7 +65,7 @@ static Napi::Buffer<T> Napi::Buffer::New(napi_env env,
65
65
  - `[in] data`: The pointer to the external data to expose.
66
66
  - `[in] length`: The number of `T` elements in the external data.
67
67
  - `[in] finalizeCallback`: The function to be called when the `Napi::Buffer` is
68
- destroyed. It must implement `operator()`, accept a `T*` (which is the
68
+ destroyed. It must implement `operator()`, accept an Napi::Env, a `T*` (which is the
69
69
  external data pointer), and return `void`.
70
70
 
71
71
  Returns a new `Napi::Buffer` object.
@@ -91,7 +91,7 @@ static Napi::Buffer<T> Napi::Buffer::New(napi_env env,
91
91
  - `[in] data`: The pointer to the external data to expose.
92
92
  - `[in] length`: The number of `T` elements in the external data.
93
93
  - `[in] finalizeCallback`: The function to be called when the `Napi::Buffer` is
94
- destroyed. It must implement `operator()`, accept a `T*` (which is the
94
+ destroyed. It must implement `operator()`, accept an Napi::Env, a `T*` (which is the
95
95
  external data pointer) and `Hint*`, and return `void`.
96
96
  - `[in] finalizeHint`: The hint to be passed as the second parameter of the
97
97
  finalize callback.
@@ -1,10 +1,18 @@
1
1
  # Class property and descriptor
2
2
 
3
- Property descriptor for use with `Napi::ObjectWrap::DefineClass()`.
4
- This is different from the standalone `Napi::PropertyDescriptor` because it is
5
- specific to each `Napi::ObjectWrap<T>` subclass.
6
- This prevents using descriptors from a different class when defining a new class
7
- (preventing the callbacks from having incorrect `this` pointers).
3
+ Property descriptor for use with `Napi::ObjectWrap<T>` and
4
+ `Napi::InstanceWrap<T>`. This is different from the standalone
5
+ `Napi::PropertyDescriptor` because it is specific to each
6
+ `Napi::ObjectWrap<T>` and `Napi::InstanceWrap<T>` subclasses.
7
+ This prevents using descriptors from a different class when defining a new
8
+ class (preventing the callbacks from having incorrect `this` pointers).
9
+
10
+ `Napi::ClassPropertyDescriptor` is a helper class created with
11
+ `Napi::ObjectWrap<T>` and `Napi::InstanceWrap<T>`. For more reference about it
12
+ see:
13
+
14
+ - [InstanceWrap](./instance_wrap.md)
15
+ - [ObjectWrap](./object_wrap.md)
8
16
 
9
17
  ## Example
10
18
 
@@ -57,7 +57,7 @@ Returns a `bool` indicating if an exception is pending in the environment.
57
57
  ### GetAndClearPendingException
58
58
 
59
59
  ```cpp
60
- Napi::Error Napi::Env::GetAndClearPendingException();
60
+ Napi::Error Napi::Env::GetAndClearPendingException() const;
61
61
  ```
62
62
 
63
63
  Returns an `Napi::Error` object representing the environment's pending exception, if any.
@@ -65,7 +65,7 @@ Returns an `Napi::Error` object representing the environment's pending exception
65
65
  ### RunScript
66
66
 
67
67
  ```cpp
68
- Napi::Value Napi::Env::RunScript(____ script);
68
+ Napi::Value Napi::Env::RunScript(____ script) const;
69
69
  ```
70
70
  - `[in] script`: A string containing JavaScript code to execute.
71
71
 
@@ -78,7 +78,7 @@ The `script` can be any of the following types:
78
78
 
79
79
  ### GetInstanceData
80
80
  ```cpp
81
- template <typename T> T* GetInstanceData();
81
+ template <typename T> T* GetInstanceData() const;
82
82
  ```
83
83
 
84
84
  Returns the instance data that was previously associated with the environment,
@@ -89,7 +89,7 @@ or `nullptr` if none was associated.
89
89
  ```cpp
90
90
  template <typename T> using Finalizer = void (*)(Env, T*);
91
91
  template <typename T, Finalizer<T> fini = Env::DefaultFini<T>>
92
- void SetInstanceData(T* data);
92
+ void SetInstanceData(T* data) const;
93
93
  ```
94
94
 
95
95
  - `[template] fini`: A function to call when the instance data is to be deleted.
@@ -112,7 +112,7 @@ template <typename DataType,
112
112
  typename HintType,
113
113
  FinalizerWithHint<DataType, HintType> fini =
114
114
  Env::DefaultFiniWithHint<DataType, HintType>>
115
- void SetInstanceData(DataType* data, HintType* hint);
115
+ void SetInstanceData(DataType* data, HintType* hint) const;
116
116
  ```
117
117
 
118
118
  - `[template] fini`: A function to call when the instance data is to be deleted.
@@ -72,7 +72,7 @@ Creates a new `Napi::Object` value.
72
72
  ### Set()
73
73
 
74
74
  ```cpp
75
- bool Napi::Object::Set (____ key, ____ value);
75
+ bool Napi::Object::Set (____ key, ____ value) const;
76
76
  ```
77
77
  - `[in] key`: The name for the property being assigned.
78
78
  - `[in] value`: The value being assigned to the property.
@@ -91,7 +91,7 @@ The `value` can be of any type that is accepted by [`Napi::Value::From`][].
91
91
  ### Delete()
92
92
 
93
93
  ```cpp
94
- bool Napi::Object::Delete(____ key);
94
+ bool Napi::Object::Delete(____ key) const;
95
95
  ```
96
96
  - `[in] key`: The name of the property to delete.
97
97
 
@@ -143,7 +143,7 @@ Note: This is equivalent to the JavaScript instanceof operator.
143
143
  ### AddFinalizer()
144
144
  ```cpp
145
145
  template <typename Finalizer, typename T>
146
- inline void AddFinalizer(Finalizer finalizeCallback, T* data);
146
+ inline void AddFinalizer(Finalizer finalizeCallback, T* data) const;
147
147
  ```
148
148
 
149
149
  - `[in] finalizeCallback`: The function to call when the object is garbage-collected.
@@ -161,7 +161,7 @@ where `data` is the pointer that was passed into the call to `AddFinalizer()`.
161
161
  template <typename Finalizer, typename T, typename Hint>
162
162
  inline void AddFinalizer(Finalizer finalizeCallback,
163
163
  T* data,
164
- Hint* finalizeHint);
164
+ Hint* finalizeHint) const;
165
165
  ```
166
166
 
167
167
  - `[in] data`: The data to associate with the object.
@@ -184,7 +184,7 @@ The properties whose key is a `Symbol` will not be included.
184
184
 
185
185
  ### HasOwnProperty()
186
186
  ```cpp
187
- bool Napi::Object::HasOwnProperty(____ key); const
187
+ bool Napi::Object::HasOwnProperty(____ key) const;
188
188
  ```
189
189
  - `[in] key` The name of the property to check.
190
190
 
@@ -200,7 +200,7 @@ The key can be any of the following types:
200
200
  ### DefineProperty()
201
201
 
202
202
  ```cpp
203
- bool Napi::Object::DefineProperty (const Napi::PropertyDescriptor& property);
203
+ bool Napi::Object::DefineProperty (const Napi::PropertyDescriptor& property) const;
204
204
  ```
205
205
  - `[in] property`: A [`Napi::PropertyDescriptor`](property_descriptor.md).
206
206
 
@@ -209,7 +209,7 @@ Define a property on the object.
209
209
  ### DefineProperties()
210
210
 
211
211
  ```cpp
212
- bool Napi::Object::DefineProperties (____ properties)
212
+ bool Napi::Object::DefineProperties (____ properties) const;
213
213
  ```
214
214
  - `[in] properties`: A list of [`Napi::PropertyDescriptor`](property_descriptor.md). Can be one of the following types:
215
215
  - const std::initializer_list<Napi::PropertyDescriptor>&
@@ -220,7 +220,7 @@ Defines properties on the object.
220
220
  ### Freeze()
221
221
 
222
222
  ```cpp
223
- void Napi::Object::Freeze()
223
+ void Napi::Object::Freeze() const;
224
224
  ```
225
225
 
226
226
  The `Napi::Object::Freeze()` method freezes an object. A frozen object can no
@@ -233,7 +233,7 @@ freezing an object also prevents its prototype from being changed.
233
233
  ### Seal()
234
234
 
235
235
  ```cpp
236
- void Napi::Object::Seal()
236
+ void Napi::Object::Seal() const;
237
237
  ```
238
238
 
239
239
  The `Napi::Object::Seal()` method seals an object, preventing new properties
@@ -244,7 +244,7 @@ writable.
244
244
  ### operator\[\]()
245
245
 
246
246
  ```cpp
247
- Napi::PropertyLValue<std::string> Napi::Object::operator[] (const char* utf8name);
247
+ Napi::PropertyLValue<std::string> Napi::Object::operator[] (const char* utf8name) const;
248
248
  ```
249
249
  - `[in] utf8name`: UTF-8 encoded null-terminated property name.
250
250
 
@@ -252,7 +252,7 @@ Returns a [`Napi::Object::PropertyLValue`](propertylvalue.md) as the named
252
252
  property or sets the named property.
253
253
 
254
254
  ```cpp
255
- Napi::PropertyLValue<std::string> Napi::Object::operator[] (const std::string& utf8name);
255
+ Napi::PropertyLValue<std::string> Napi::Object::operator[] (const std::string& utf8name) const;
256
256
  ```
257
257
  - `[in] utf8name`: UTF-8 encoded property name.
258
258
 
@@ -260,34 +260,13 @@ Returns a [`Napi::Object::PropertyLValue`](propertylvalue.md) as the named
260
260
  property or sets the named property.
261
261
 
262
262
  ```cpp
263
- Napi::PropertyLValue<uint32_t> Napi::Object::operator[] (uint32_t index);
263
+ Napi::PropertyLValue<uint32_t> Napi::Object::operator[] (uint32_t index) const;
264
264
  ```
265
265
  - `[in] index`: Element index.
266
266
 
267
267
  Returns a [`Napi::Object::PropertyLValue`](propertylvalue.md) or sets an
268
268
  indexed property or array element.
269
269
 
270
- ```cpp
271
- Napi::Value Napi::Object::operator[] (const char* utf8name) const;
272
- ```
273
- - `[in] utf8name`: UTF-8 encoded null-terminated property name.
274
-
275
- Returns the named property as a [`Napi::Value`](value.md).
276
-
277
- ```cpp
278
- Napi::Value Napi::Object::operator[] (const std::string& utf8name) const;
279
- ```
280
- - `[in] utf8name`: UTF-8 encoded property name.
281
-
282
- Returns the named property as a [`Napi::Value`](value.md).
283
-
284
- ```cpp
285
- Napi::Value Napi::Object::operator[] (uint32_t index) const;
286
- ```
287
- - `[in] index`: Element index.
288
-
289
- Returns an indexed property or array element as a [`Napi::Value`](value.md).
290
-
291
270
  ### begin()
292
271
 
293
272
  ```cpp
@@ -103,7 +103,7 @@ The `value` can be any of the following types:
103
103
  ### Get
104
104
 
105
105
  ```cpp
106
- Napi::Value Napi::ObjectReference::Get(___ key);
106
+ Napi::Value Napi::ObjectReference::Get(___ key) const;
107
107
  ```
108
108
 
109
109
  * `[in] key`: The name of the property to return the value for.
@@ -16,6 +16,10 @@ be directly invoked from JavaScript. The **wrap** word refers to a way of
16
16
  grouping methods and state of the class because it will be necessary write
17
17
  custom code to bridge each of your C++ class methods.
18
18
 
19
+ **Caution:** When the JavaScript object is garbage collected, the call to the
20
+ C++ destructor may be deferred until a later time. Within that period,
21
+ `Value()` will return an empty value.
22
+
19
23
  ## Example
20
24
 
21
25
  ```cpp
@@ -212,6 +216,29 @@ property of the `Napi::CallbackInfo`.
212
216
 
213
217
  Returns a `Napi::Function` representing the constructor function for the class.
214
218
 
219
+ ### OnCalledAsFunction
220
+
221
+ Provides an opportunity to customize the behavior when a `Napi::ObjectWrap<T>`
222
+ class is called from JavaScript as a function (without the **new** operator).
223
+
224
+ The default behavior in this scenario is to throw a `Napi::TypeError` with the
225
+ message `Class constructors cannot be invoked without 'new'`. Define this
226
+ public method on your derived class to override that behavior.
227
+
228
+ For example, you could internally re-call the JavaScript contstructor _with_
229
+ the **new** operator (via
230
+ `Napi::Function::New(const std::vector<napi_value> &args)`), and return the
231
+ resulting object. Or you might do something else entirely, such as the way
232
+ [`Date()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#constructor)
233
+ produces a string when called as a function.
234
+
235
+ ```cpp
236
+ static Napi::Value OnCalledAsFunction(const Napi::CallbackInfo& callbackInfo);
237
+ ```
238
+
239
+ - `[in] callbackInfo`: The object representing the components of the JavaScript
240
+ request being made.
241
+
215
242
  ### Finalize
216
243
 
217
244
  Provides an opportunity to run cleanup code that requires access to the
@@ -69,7 +69,7 @@ Returns the value held by the `Napi::Reference`.
69
69
  ### Ref
70
70
 
71
71
  ```cpp
72
- uint32_t Napi::Reference::Ref();
72
+ uint32_t Napi::Reference::Ref() const;
73
73
  ```
74
74
 
75
75
  Increments the reference count for the `Napi::Reference` and returns the resulting reference count. Throws an error if the increment fails.
@@ -77,7 +77,7 @@ Increments the reference count for the `Napi::Reference` and returns the resulti
77
77
  ### Unref
78
78
 
79
79
  ```cpp
80
- uint32_t Napi::Reference::Unref();
80
+ uint32_t Napi::Reference::Unref() const;
81
81
  ```
82
82
 
83
83
  Decrements the reference count for the `Napi::Reference` and returns the resulting reference count. Throws an error if the decrement fails.
@@ -83,7 +83,7 @@ Add a thread to this thread-safe function object, indicating that a new thread
83
83
  will start making use of the thread-safe function.
84
84
 
85
85
  ```cpp
86
- napi_status Napi::ThreadSafeFunction::Acquire()
86
+ napi_status Napi::ThreadSafeFunction::Acquire() const
87
87
  ```
88
88
 
89
89
  Returns one of:
@@ -100,7 +100,7 @@ thread-safe function. Using any thread-safe APIs after having called this API
100
100
  has undefined results in the current thread, as it may have been destroyed.
101
101
 
102
102
  ```cpp
103
- napi_status Napi::ThreadSafeFunction::Release()
103
+ napi_status Napi::ThreadSafeFunction::Release() const
104
104
  ```
105
105
 
106
106
  Returns one of:
@@ -122,7 +122,7 @@ make no further use of the thread-safe function because it is no longer
122
122
  guaranteed to be allocated.
123
123
 
124
124
  ```cpp
125
- napi_status Napi::ThreadSafeFunction::Abort()
125
+ napi_status Napi::ThreadSafeFunction::Abort() const
126
126
  ```
127
127
 
128
128
  Returns one of:
@@ -124,7 +124,7 @@ has undefined results in the current thread, as the thread-safe function may
124
124
  have been destroyed.
125
125
 
126
126
  ```cpp
127
- napi_status Napi::TypedThreadSafeFunction<ContextType, DataType, Callback>::Release()
127
+ napi_status Napi::TypedThreadSafeFunction<ContextType, DataType, Callback>::Release() const
128
128
  ```
129
129
 
130
130
  Returns one of:
@@ -146,7 +146,7 @@ function call a thread must make no further use of the thread-safe function
146
146
  because it is no longer guaranteed to be allocated.
147
147
 
148
148
  ```cpp
149
- napi_status Napi::TypedThreadSafeFunction<ContextType, DataType, Callback>::Abort()
149
+ napi_status Napi::TypedThreadSafeFunction<ContextType, DataType, Callback>::Abort() const
150
150
  ```
151
151
 
152
152
  Returns one of: