node-addon-api 2.0.2 → 3.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.
Files changed (82) hide show
  1. package/.clang-format +111 -0
  2. package/.github/workflows/ci.yml +55 -0
  3. package/.github/workflows/linter.yml +24 -0
  4. package/.github/workflows/stale.yml +18 -0
  5. package/.travis.yml +1 -5
  6. package/CHANGELOG.md +237 -23
  7. package/README.md +101 -31
  8. package/appveyor.yml +3 -14
  9. package/benchmark/README.md +47 -0
  10. package/benchmark/binding.gyp +25 -0
  11. package/benchmark/function_args.cc +217 -0
  12. package/benchmark/function_args.js +60 -0
  13. package/benchmark/index.js +34 -0
  14. package/benchmark/property_descriptor.cc +91 -0
  15. package/benchmark/property_descriptor.js +37 -0
  16. package/common.gypi +21 -0
  17. package/doc/addon.md +157 -0
  18. package/doc/array.md +81 -0
  19. package/doc/array_buffer.md +20 -0
  20. package/doc/async_context.md +1 -1
  21. package/doc/async_worker.md +34 -5
  22. package/doc/{async_progress_worker.md → async_worker_variants.md} +236 -23
  23. package/doc/bigint.md +7 -2
  24. package/doc/boolean.md +5 -1
  25. package/doc/buffer.md +4 -0
  26. package/doc/checker-tool.md +1 -1
  27. package/doc/class_property_descriptor.md +3 -3
  28. package/doc/creating_a_release.md +6 -6
  29. package/doc/dataview.md +4 -0
  30. package/doc/date.md +2 -2
  31. package/doc/env.md +69 -0
  32. package/doc/error.md +5 -0
  33. package/doc/escapable_handle_scope.md +1 -1
  34. package/doc/external.md +4 -0
  35. package/doc/function.md +111 -3
  36. package/doc/function_reference.md +1 -1
  37. package/doc/handle_scope.md +1 -1
  38. package/doc/hierarchy.md +91 -0
  39. package/doc/instance_wrap.md +408 -0
  40. package/doc/name.md +29 -0
  41. package/doc/number.md +1 -1
  42. package/doc/object.md +44 -1
  43. package/doc/object_lifetime_management.md +2 -2
  44. package/doc/object_reference.md +1 -1
  45. package/doc/object_wrap.md +220 -216
  46. package/doc/prebuild_tools.md +2 -2
  47. package/doc/promises.md +5 -0
  48. package/doc/property_descriptor.md +67 -12
  49. package/doc/setup.md +1 -2
  50. package/doc/string.md +5 -1
  51. package/doc/symbol.md +5 -1
  52. package/doc/threadsafe.md +121 -0
  53. package/doc/threadsafe_function.md +16 -46
  54. package/doc/typed_array.md +4 -0
  55. package/doc/typed_array_of.md +4 -0
  56. package/doc/typed_threadsafe_function.md +307 -0
  57. package/doc/value.md +166 -104
  58. package/doc/version_management.md +2 -2
  59. package/except.gypi +16 -0
  60. package/index.js +7 -41
  61. package/napi-inl.h +1685 -464
  62. package/napi.h +606 -141
  63. package/node_api.gyp +9 -0
  64. package/noexcept.gypi +16 -0
  65. package/{src/nothing.c → nothing.c} +0 -0
  66. package/package-support.json +21 -0
  67. package/package.json +106 -2
  68. package/tools/README.md +12 -6
  69. package/tools/clang-format.js +47 -0
  70. package/tools/conversion.js +4 -8
  71. package/doc/Doxyfile +0 -2450
  72. package/doc/basic_types.md +0 -423
  73. package/doc/working_with_javascript_values.md +0 -14
  74. package/external-napi/node_api.h +0 -7
  75. package/src/node_api.cc +0 -3655
  76. package/src/node_api.gyp +0 -21
  77. package/src/node_api.h +0 -588
  78. package/src/node_api_types.h +0 -115
  79. package/src/node_internals.cc +0 -142
  80. package/src/node_internals.h +0 -157
  81. package/src/util-inl.h +0 -38
  82. package/src/util.h +0 -7
@@ -1,18 +1,20 @@
1
1
  # Object Wrap
2
2
 
3
- The `Napi::ObjectWrap` class is used to bind the lifetime of C++ code to a
3
+ Class `Napi::ObjectWrap<T>` inherits from class [`Napi::InstanceWrap<T>`][].
4
+
5
+ The `Napi::ObjectWrap<T>` class is used to bind the lifetime of C++ code to a
4
6
  JavaScript object. Once bound, each time an instance of the JavaScript object
5
7
  is created, an instance of the C++ class will also be created. When a method
6
8
  is called on the JavaScript object which is defined as an InstanceMethod, the
7
9
  corresponding C++ method on the wrapped C++ class will be invoked.
8
10
 
9
11
  In order to create a wrapper it's necessary to extend the
10
- `Napi::ObjectWrap`class which contains all the plumbing to connect JavaScript code
11
- with a C++ object. Classes extending `Napi::ObjectWrap` can be instantiated from
12
- JavaScript using the **new** operator, and their methods can be directly invoked
13
- from JavaScript. The **wrap** word refers to a way of grouping methods and state
14
- of the class because it will be necessary write custom code to bridge each of
15
- your C++ class methods.
12
+ `Napi::ObjectWrap<T>` class which contains all the plumbing to connect
13
+ JavaScript code with a C++ object. Classes extending `Napi::ObjectWrap` can be
14
+ instantiated from JavaScript using the **new** operator, and their methods can
15
+ be directly invoked from JavaScript. The **wrap** word refers to a way of
16
+ grouping methods and state of the class because it will be necessary write
17
+ custom code to bridge each of your C++ class methods.
16
18
 
17
19
  ## Example
18
20
 
@@ -22,49 +24,58 @@ your C++ class methods.
22
24
  class Example : public Napi::ObjectWrap<Example> {
23
25
  public:
24
26
  static Napi::Object Init(Napi::Env env, Napi::Object exports);
25
- Example(const Napi::CallbackInfo &info);
27
+ Example(const Napi::CallbackInfo& info);
28
+ static Napi::Value CreateNewItem(const Napi::CallbackInfo& info);
26
29
 
27
30
  private:
28
- static Napi::FunctionReference constructor;
29
31
  double _value;
30
- Napi::Value GetValue(const Napi::CallbackInfo &info);
31
- Napi::Value SetValue(const Napi::CallbackInfo &info);
32
+ Napi::Value GetValue(const Napi::CallbackInfo& info);
33
+ Napi::Value SetValue(const Napi::CallbackInfo& info);
32
34
  };
33
35
 
34
36
  Napi::Object Example::Init(Napi::Env env, Napi::Object exports) {
35
37
  // This method is used to hook the accessor and method callbacks
36
38
  Napi::Function func = DefineClass(env, "Example", {
37
- InstanceMethod("GetValue", &Example::GetValue),
38
- InstanceMethod("SetValue", &Example::SetValue)
39
+ InstanceMethod<&Example::GetValue>("GetValue"),
40
+ InstanceMethod<&Example::SetValue>("SetValue"),
41
+ StaticMethod<&Example::CreateNewItem>("CreateNewItem"),
39
42
  });
40
43
 
41
- // Create a peristent reference to the class constructor. This will allow
44
+ Napi::FunctionReference* constructor = new Napi::FunctionReference();
45
+
46
+ // Create a persistent reference to the class constructor. This will allow
42
47
  // a function called on a class prototype and a function
43
48
  // called on instance of a class to be distinguished from each other.
44
- constructor = Napi::Persistent(func);
45
- // Call the SuppressDestruct() method on the static data prevent the calling
46
- // to this destructor to reset the reference when the environment is no longer
47
- // available.
48
- constructor.SuppressDestruct();
49
+ *constructor = Napi::Persistent(func);
49
50
  exports.Set("Example", func);
51
+
52
+ // Store the constructor as the add-on instance data. This will allow this
53
+ // add-on to support multiple instances of itself running on multiple worker
54
+ // threads, as well as multiple instances of itself running in different
55
+ // contexts on the same thread.
56
+ //
57
+ // By default, the value set on the environment here will be destroyed when
58
+ // the add-on is unloaded using the `delete` operator, but it is also
59
+ // possible to supply a custom deleter.
60
+ env.SetInstanceData<Napi::FunctionReference>(constructor);
61
+
50
62
  return exports;
51
63
  }
52
64
 
53
- Example::Example(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Example>(info) {
54
- Napi::Env env = info.Env();
55
- // ...
56
- Napi::Number value = info[0].As<Napi::Number>();
57
- this->_value = value.DoubleValue();
65
+ Example::Example(const Napi::CallbackInfo& info) :
66
+ Napi::ObjectWrap<Example>(info) {
67
+ Napi::Env env = info.Env();
68
+ // ...
69
+ Napi::Number value = info[0].As<Napi::Number>();
70
+ this->_value = value.DoubleValue();
58
71
  }
59
72
 
60
- Napi::FunctionReference Example::constructor;
61
-
62
- Napi::Value Example::GetValue(const Napi::CallbackInfo &info){
73
+ Napi::Value Example::GetValue(const Napi::CallbackInfo& info){
63
74
  Napi::Env env = info.Env();
64
75
  return Napi::Number::New(env, this->_value);
65
76
  }
66
77
 
67
- Napi::Value Example::SetValue(const Napi::CallbackInfo &info){
78
+ Napi::Value Example::SetValue(const Napi::CallbackInfo& info){
68
79
  Napi::Env env = info.Env();
69
80
  // ...
70
81
  Napi::Number value = info[0].As<Napi::Number>();
@@ -78,6 +89,16 @@ Napi::Object Init (Napi::Env env, Napi::Object exports) {
78
89
  return exports;
79
90
  }
80
91
 
92
+ // Create a new item using the constructor stored during Init.
93
+ Napi::Value Example::CreateNewItem(const Napi::CallbackInfo& info) {
94
+ // Retrieve the instance data we stored during `Init()`. We only stored the
95
+ // constructor there, so we retrieve it here to create a new instance of the
96
+ // JS class the constructor represents.
97
+ Napi::FunctionReference* constructor =
98
+ info.Env().GetInstanceData<Napi::FunctionReference>();
99
+ return constructor->New({ Napi::Number::New(info.Env(), 42) });
100
+ }
101
+
81
102
  // Register and initialize native add-on
82
103
  NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
83
104
  ```
@@ -97,22 +118,23 @@ console.log(example.GetValue());
97
118
  // It prints 19
98
119
  ```
99
120
 
100
- At initialization time, the `Napi::ObjectWrap::DefineClass()` method must be used
101
- to hook up the accessor and method callbacks. It takes a list of property
121
+ At initialization time, the `Napi::ObjectWrap::DefineClass()` method must be
122
+ used to hook up the accessor and method callbacks. It takes a list of property
102
123
  descriptors, which can be constructed via the various static methods on the base
103
124
  class.
104
125
 
105
- When JavaScript code invokes the constructor, the constructor callback will create
106
- a new C++ instance and "wrap" it into the newly created JavaScript object.
126
+ When JavaScript code invokes the constructor, the constructor callback will
127
+ create a new C++ instance and "wrap" it into the newly created JavaScript
128
+ object.
107
129
 
108
130
  When JavaScript code invokes a method or a property accessor on the class the
109
131
  corresponding C++ callback function will be executed.
110
132
 
111
- For a wrapped object it could be difficult to distinguish between a function called
112
- on a class prototype and a function called on instance of a class. Therefore it is
113
- good practice to save a persistent reference to the class constructor. This allows
114
- the two cases to be distinguished from each other by checking the this object
115
- against the class constructor.
133
+ For a wrapped object it could be difficult to distinguish between a function
134
+ called on a class prototype and a function called on instance of a class.
135
+ Therefore it is good practice to save a persistent reference to the class
136
+ constructor. This allows the two cases to be distinguished from each other by
137
+ checking the this object against the class constructor.
116
138
 
117
139
  ## Methods
118
140
 
@@ -137,7 +159,7 @@ static T* Napi::ObjectWrap::Unwrap(Napi::Object wrapper);
137
159
 
138
160
  * `[in] wrapper`: The JavaScript object that wraps the native instance.
139
161
 
140
- Returns a native instace wrapped in a JavaScript object. Given the
162
+ Returns a native instance wrapped in a JavaScript object. Given the
141
163
  Napi:Object, this allows a method to get a pointer to the wrapped
142
164
  C++ object and then reference fields, call methods, etc. within that class.
143
165
  In many cases calling Unwrap is not required, as methods can
@@ -151,9 +173,9 @@ methods.
151
173
 
152
174
  ```cpp
153
175
  static Napi::Function Napi::ObjectWrap::DefineClass(Napi::Env env,
154
- const char* utf8name,
155
- const std::initializer_list<PropertyDescriptor>& properties,
156
- void* data = nullptr);
176
+ const char* utf8name,
177
+ const std::initializer_list<PropertyDescriptor>& properties,
178
+ void* data = nullptr);
157
179
  ```
158
180
 
159
181
  * `[in] env`: The environment in which to construct a JavaScript class.
@@ -192,8 +214,9 @@ Returns a `Napi::Function` representing the constructor function for the class.
192
214
 
193
215
  ### Finalize
194
216
 
195
- Provides an opportunity to run cleanup code that requires access to the `Napi::Env`
196
- before the wrapped native object instance is freed. Override to implement.
217
+ Provides an opportunity to run cleanup code that requires access to the
218
+ `Napi::Env` before the wrapped native object instance is freed. Override to
219
+ implement.
197
220
 
198
221
  ```cpp
199
222
  virtual void Finalize(Napi::Env env);
@@ -203,13 +226,15 @@ virtual void Finalize(Napi::Env env);
203
226
 
204
227
  ### StaticMethod
205
228
 
206
- Creates property descriptor that represents a static method of a JavaScript class.
229
+ Creates property descriptor that represents a static method of a JavaScript
230
+ class.
207
231
 
208
232
  ```cpp
209
- static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(const char* utf8name,
210
- StaticVoidMethodCallback method,
211
- napi_property_attributes attributes = napi_default,
212
- void* data = nullptr);
233
+ static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(
234
+ const char* utf8name,
235
+ StaticVoidMethodCallback method,
236
+ napi_property_attributes attributes = napi_default,
237
+ void* data = nullptr);
213
238
  ```
214
239
 
215
240
  - `[in] utf8name`: Null-terminated string that represents the name of a static
@@ -225,13 +250,15 @@ JavaScript class.
225
250
 
226
251
  ### StaticMethod
227
252
 
228
- Creates property descriptor that represents a static method of a JavaScript class.
253
+ Creates property descriptor that represents a static method of a JavaScript
254
+ class.
229
255
 
230
256
  ```cpp
231
- static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(const char* utf8name,
232
- StaticMethodCallback method,
233
- napi_property_attributes attributes = napi_default,
234
- void* data = nullptr);
257
+ static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(
258
+ const char* utf8name,
259
+ StaticMethodCallback method,
260
+ napi_property_attributes attributes = napi_default,
261
+ void* data = nullptr);
235
262
  ```
236
263
 
237
264
  - `[in] utf8name`: Null-terminated string that represents the name of a static
@@ -247,13 +274,14 @@ JavaScript class.
247
274
 
248
275
  ### StaticMethod
249
276
 
250
- Creates property descriptor that represents a static method of a JavaScript class.
277
+ Creates property descriptor that represents a static method of a JavaScript
278
+ class.
251
279
 
252
280
  ```cpp
253
281
  static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(Symbol name,
254
- StaticVoidMethodCallback method,
255
- napi_property_attributes attributes = napi_default,
256
- void* data = nullptr);
282
+ StaticVoidMethodCallback method,
283
+ napi_property_attributes attributes = napi_default,
284
+ void* data = nullptr);
257
285
  ```
258
286
 
259
287
  - `[in] name`: Napi:Symbol that represents the name of a static
@@ -269,13 +297,14 @@ JavaScript class.
269
297
 
270
298
  ### StaticMethod
271
299
 
272
- Creates property descriptor that represents a static method of a JavaScript class.
300
+ Creates property descriptor that represents a static method of a JavaScript
301
+ class.
273
302
 
274
303
  ```cpp
275
304
  static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(Symbol name,
276
- StaticMethodCallback method,
277
- napi_property_attributes attributes = napi_default,
278
- void* data = nullptr);
305
+ StaticMethodCallback method,
306
+ napi_property_attributes attributes = napi_default,
307
+ void* data = nullptr);
279
308
  ```
280
309
 
281
310
  method for the class.
@@ -289,197 +318,203 @@ One or more of `napi_property_attributes`.
289
318
  Returns `Napi::PropertyDescriptor` object that represents a static method of a
290
319
  JavaScript class.
291
320
 
292
- ### StaticAccessor
321
+ ### StaticMethod
293
322
 
294
- Creates property descriptor that represents a static accessor property of a
295
- JavaScript class.
323
+ Creates property descriptor that represents a static method of a JavaScript
324
+ class.
296
325
 
297
326
  ```cpp
298
- static Napi::PropertyDescriptor Napi::ObjectWrap::StaticAccessor(const char* utf8name,
299
- StaticGetterCallback getter,
300
- StaticSetterCallback setter,
301
- napi_property_attributes attributes = napi_default,
302
- void* data = nullptr);
327
+ template <StaticVoidMethodCallback method>
328
+ static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(
329
+ const char* utf8name,
330
+ napi_property_attributes attributes = napi_default,
331
+ void* data = nullptr);
303
332
  ```
304
333
 
334
+ - `[in] method`: The native function that represents a static method of a
335
+ JavaScript class. This function returns nothing.
305
336
  - `[in] utf8name`: Null-terminated string that represents the name of a static
306
- accessor property for the class.
307
- - `[in] getter`: The native function to call when a get access to the property of
308
- a JavaScript class is performed.
309
- - `[in] setter`: The native function to call when a set access to the property of
310
- a JavaScript class is performed.
337
+ method for the class.
311
338
  - `[in] attributes`: The attributes associated with a particular property.
312
339
  One or more of `napi_property_attributes`.
313
- - `[in] data`: User-provided data passed into getter or setter when
314
- is invoked.
340
+ - `[in] data`: User-provided data passed into method when it is invoked.
315
341
 
316
- Returns `Napi::PropertyDescriptor` object that represents a static accessor
317
- property of a JavaScript class.
342
+ Returns `Napi::PropertyDescriptor` object that represents the static method of a
343
+ JavaScript class.
318
344
 
319
- ### StaticAccessor
345
+ ### StaticMethod
320
346
 
321
- Creates property descriptor that represents a static accessor property of a
322
- JavaScript class.
347
+ Creates property descriptor that represents a static method of a JavaScript
348
+ class.
323
349
 
324
350
  ```cpp
325
- static Napi::PropertyDescriptor Napi::ObjectWrap::StaticAccessor(Symbol name,
326
- StaticGetterCallback getter,
327
- StaticSetterCallback setter,
328
- napi_property_attributes attributes = napi_default,
329
- void* data = nullptr);
351
+ template <StaticMethodCallback method>
352
+ static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(
353
+ const char* utf8name,
354
+ napi_property_attributes attributes = napi_default,
355
+ void* data = nullptr);
330
356
  ```
331
357
 
332
- - `[in] name`: Napi:Symbol that represents the name of a static accessor.
333
- - `[in] getter`: The native function to call when a get access to the property of
334
- a JavaScript class is performed.
335
- - `[in] setter`: The native function to call when a set access to the property of
336
- a JavaScript class is performed.
358
+ - `[in] method`: The native function that represents a static method of a
359
+ JavaScript class.
360
+ - `[in] utf8name`: Null-terminated string that represents the name of a static
361
+ method for the class.
337
362
  - `[in] attributes`: The attributes associated with a particular property.
338
363
  One or more of `napi_property_attributes`.
339
- - `[in] data`: User-provided data passed into getter or setter when
340
- is invoked.
364
+ - `[in] data`: User-provided data passed into method when it is invoked.
341
365
 
342
- Returns `Napi::PropertyDescriptor` object that represents a static accessor
343
- property of a JavaScript class.
366
+ Returns `Napi::PropertyDescriptor` object that represents a static method of a
367
+ JavaScript class.
344
368
 
345
- ### InstanceMethod
369
+ ### StaticMethod
346
370
 
347
- Creates property descriptor that represents an instance method of a JavaScript class.
371
+ Creates property descriptor that represents a static method of a JavaScript
372
+ class.
348
373
 
349
374
  ```cpp
350
- static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceMethod(const char* utf8name,
351
- InstanceVoidMethodCallback method,
352
- napi_property_attributes attributes = napi_default,
353
- void* data = nullptr);
375
+ template <StaticVoidMethodCallback method>
376
+ static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(Symbol name,
377
+ napi_property_attributes attributes = napi_default,
378
+ void* data = nullptr);
354
379
  ```
355
380
 
356
- - `[in] utf8name`: Null-terminated string that represents the name of an instance
357
- method for the class.
358
- - `[in] method`: The native function that represents an instance method of a
381
+ - `[in] method`: The native function that represents a static method of a
359
382
  JavaScript class.
383
+ - `[in] name`: Napi:Symbol that represents the name of a static
384
+ method for the class.
360
385
  - `[in] attributes`: The attributes associated with a particular property.
361
386
  One or more of `napi_property_attributes`.
362
387
  - `[in] data`: User-provided data passed into method when it is invoked.
363
388
 
364
- Returns `Napi::PropertyDescriptor` object that represents an instance method of a
389
+ Returns `Napi::PropertyDescriptor` object that represents the static method of a
365
390
  JavaScript class.
366
391
 
367
- ### InstanceMethod
392
+ ### StaticMethod
368
393
 
369
- Creates property descriptor that represents an instance method of a JavaScript class.
394
+ Creates property descriptor that represents a static method of a JavaScript
395
+ class.
370
396
 
371
397
  ```cpp
372
- static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceMethod(const char* utf8name,
373
- InstanceMethodCallback method,
374
- napi_property_attributes attributes = napi_default,
375
- void* data = nullptr);
398
+ template <StaticMethodCallback method>
399
+ static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(Symbol name,
400
+ napi_property_attributes attributes = napi_default,
401
+ void* data = nullptr);
376
402
  ```
377
403
 
378
- - `[in] utf8name`: Null-terminated string that represents the name of an instance
379
- method for the class.
380
- - `[in] method`: The native function that represents an instance method of a
404
+ - `[in] method`: The native function that represents a static method of a
381
405
  JavaScript class.
406
+ - `[in] name`: Napi:Symbol that represents the name of a static.
382
407
  - `[in] attributes`: The attributes associated with a particular property.
383
408
  One or more of `napi_property_attributes`.
384
409
  - `[in] data`: User-provided data passed into method when it is invoked.
385
410
 
386
- Returns `Napi::PropertyDescriptor` object that represents an instance method of a
411
+ Returns `Napi::PropertyDescriptor` object that represents a static method of a
387
412
  JavaScript class.
388
413
 
389
- ### InstanceMethod
414
+ ### StaticAccessor
390
415
 
391
- Creates property descriptor that represents an instance method of a JavaScript class.
416
+ Creates property descriptor that represents a static accessor property of a
417
+ JavaScript class.
392
418
 
393
419
  ```cpp
394
- static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceMethod(Napi::Symbol name,
395
- InstanceVoidMethodCallback method,
396
- napi_property_attributes attributes = napi_default,
397
- void* data = nullptr);
420
+ static Napi::PropertyDescriptor Napi::ObjectWrap::StaticAccessor(
421
+ const char* utf8name,
422
+ StaticGetterCallback getter,
423
+ StaticSetterCallback setter,
424
+ napi_property_attributes attributes = napi_default,
425
+ void* data = nullptr);
398
426
  ```
399
427
 
400
- - `[in] name`: The `Napi::Symbol` object whose value is used to identify the
401
- instance method for the class.
402
- - `[in] method`: The native function that represents an instance method of a
403
- JavaScript class.
428
+ - `[in] utf8name`: Null-terminated string that represents the name of a static
429
+ accessor property for the class.
430
+ - `[in] getter`: The native function to call when a get access to the property
431
+ of a JavaScript class is performed.
432
+ - `[in] setter`: The native function to call when a set access to the property
433
+ of a JavaScript class is performed.
404
434
  - `[in] attributes`: The attributes associated with a particular property.
405
435
  One or more of `napi_property_attributes`.
406
- - `[in] data`: User-provided data passed into method when it is invoked.
436
+ - `[in] data`: User-provided data passed into getter or setter when
437
+ is invoked.
407
438
 
408
- Returns `Napi::PropertyDescriptor` object that represents an instance method of a
409
- JavaScript class.
439
+ Returns `Napi::PropertyDescriptor` object that represents a static accessor
440
+ property of a JavaScript class.
410
441
 
411
- ### InstanceMethod
442
+ ### StaticAccessor
412
443
 
413
- Creates property descriptor that represents an instance method of a JavaScript class.
444
+ Creates property descriptor that represents a static accessor property of a
445
+ JavaScript class.
414
446
 
415
447
  ```cpp
416
- static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceMethod(Napi::Symbol name,
417
- InstanceMethodCallback method,
418
- napi_property_attributes attributes = napi_default,
419
- void* data = nullptr);
448
+ static Napi::PropertyDescriptor Napi::ObjectWrap::StaticAccessor(Symbol name,
449
+ StaticGetterCallback getter,
450
+ StaticSetterCallback setter,
451
+ napi_property_attributes attributes = napi_default,
452
+ void* data = nullptr);
420
453
  ```
421
454
 
422
- - `[in] name`: The `Napi::Symbol` object whose value is used to identify the
423
- instance method for the class.
424
- - `[in] method`: The native function that represents an instance method of a
425
- JavaScript class.
455
+ - `[in] name`: Napi:Symbol that represents the name of a static accessor.
456
+ - `[in] getter`: The native function to call when a get access to the property
457
+ of a JavaScript class is performed.
458
+ - `[in] setter`: The native function to call when a set access to the property
459
+ of a JavaScript class is performed.
426
460
  - `[in] attributes`: The attributes associated with a particular property.
427
461
  One or more of `napi_property_attributes`.
428
- - `[in] data`: User-provided data passed into method when it is invoked.
462
+ - `[in] data`: User-provided data passed into getter or setter when
463
+ is invoked.
429
464
 
430
- Returns `Napi::PropertyDescriptor` object that represents an instance method of a
431
- JavaScript class.
465
+ Returns `Napi::PropertyDescriptor` object that represents a static accessor
466
+ property of a JavaScript class.
432
467
 
433
- ### InstanceAccessor
468
+ ### StaticAccessor
434
469
 
435
- Creates property descriptor that represents an instance accessor property of a
470
+ Creates property descriptor that represents a static accessor property of a
436
471
  JavaScript class.
437
472
 
438
473
  ```cpp
439
- static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceAccessor(const char* utf8name,
440
- InstanceGetterCallback getter,
441
- InstanceSetterCallback setter,
442
- napi_property_attributes attributes = napi_default,
443
- void* data = nullptr);
474
+ template <StaticGetterCallback getter, StaticSetterCallback setter=nullptr>
475
+ static Napi::PropertyDescriptor Napi::ObjectWrap::StaticAccessor(
476
+ const char* utf8name,
477
+ napi_property_attributes attributes = napi_default,
478
+ void* data = nullptr);
444
479
  ```
445
480
 
446
- - `[in] utf8name`: Null-terminated string that represents the name of an instance
481
+ - `[in] getter`: The native function to call when a get access to the property
482
+ of a JavaScript class is performed.
483
+ - `[in] setter`: The native function to call when a set access to the property
484
+ of a JavaScript class is performed.
485
+ - `[in] utf8name`: Null-terminated string that represents the name of a static
447
486
  accessor property for the class.
448
- - `[in] getter`: The native function to call when a get access to the property of
449
- a JavaScript class is performed.
450
- - `[in] setter`: The native function to call when a set access to the property of
451
- a JavaScript class is performed.
452
- - `[in] attributes`: The attributes associated with the particular property.
487
+ - `[in] attributes`: The attributes associated with a particular property.
453
488
  One or more of `napi_property_attributes`.
454
- - `[in] data`: User-provided data passed into getter or setter when this is invoked.
489
+ - `[in] data`: User-provided data passed into getter or setter when
490
+ is invoked.
455
491
 
456
- Returns `Napi::PropertyDescriptor` object that represents an instance accessor
492
+ Returns `Napi::PropertyDescriptor` object that represents a static accessor
457
493
  property of a JavaScript class.
458
494
 
459
- ### InstanceAccessor
495
+ ### StaticAccessor
460
496
 
461
- Creates property descriptor that represents an instance accessor property of a
497
+ Creates property descriptor that represents a static accessor property of a
462
498
  JavaScript class.
463
499
 
464
500
  ```cpp
465
- static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceAccessor(Symbol name,
466
- InstanceGetterCallback getter,
467
- InstanceSetterCallback setter,
468
- napi_property_attributes attributes = napi_default,
469
- void* data = nullptr);
501
+ template <StaticGetterCallback getter, StaticSetterCallback setter=nullptr>
502
+ static Napi::PropertyDescriptor Napi::ObjectWrap::StaticAccessor(Symbol name,
503
+ napi_property_attributes attributes = napi_default,
504
+ void* data = nullptr);
470
505
  ```
471
506
 
472
- - `[in] name`: The `Napi::Symbol` object whose value is used to identify the
473
- instance accessor.
474
- - `[in] getter`: The native function to call when a get access to the property of
475
- a JavaScript class is performed.
476
- - `[in] setter`: The native function to call when a set access to the property of
477
- a JavaScript class is performed.
478
- - `[in] attributes`: The attributes associated with the particular property.
507
+ - `[in] getter`: The native function to call when a get access to the property
508
+ of a JavaScript class is performed.
509
+ - `[in] setter`: The native function to call when a set access to the property
510
+ of a JavaScript class is performed.
511
+ - `[in] name`: Napi:Symbol that represents the name of a static accessor.
512
+ - `[in] attributes`: The attributes associated with a particular property.
479
513
  One or more of `napi_property_attributes`.
480
- - `[in] data`: User-provided data passed into getter or setter when this is invoked.
514
+ - `[in] data`: User-provided data passed into getter or setter when
515
+ is invoked.
481
516
 
482
- Returns `Napi::PropertyDescriptor` object that represents an instance accessor
517
+ Returns `Napi::PropertyDescriptor` object that represents a static accessor
483
518
  property of a JavaScript class.
484
519
 
485
520
  ### StaticValue
@@ -487,16 +522,18 @@ property of a JavaScript class.
487
522
  Creates property descriptor that represents an static value property of a
488
523
  JavaScript class.
489
524
  ```cpp
490
- static Napi::PropertyDescriptor Napi::ObjectWrap::StaticValue(const char* utf8name,
491
- Napi::Value value,
492
- napi_property_attributes attributes = napi_default);
525
+ static Napi::PropertyDescriptor Napi::ObjectWrap::StaticValue(
526
+ const char* utf8name,
527
+ Napi::Value value,
528
+ napi_property_attributes attributes = napi_default);
493
529
  ```
494
530
 
495
531
  - `[in] utf8name`: Null-terminated string that represents the name of the static
496
532
  property.
497
533
  - `[in] value`: The value that's retrieved by a get access of the property.
498
- - `[in] attributes`: The attributes to be associated with the property in addition
499
- to the napi_static attribute. One or more of `napi_property_attributes`.
534
+ - `[in] attributes`: The attributes to be associated with the property in
535
+ addition to the napi_static attribute. One or more of
536
+ `napi_property_attributes`.
500
537
 
501
538
  Returns `Napi::PropertyDescriptor` object that represents an static value
502
539
  property of a JavaScript class
@@ -507,51 +544,18 @@ Creates property descriptor that represents an static value property of a
507
544
  JavaScript class.
508
545
  ```cpp
509
546
  static Napi::PropertyDescriptor Napi::ObjectWrap::StaticValue(Symbol name,
510
- Napi::Value value,
511
- napi_property_attributes attributes = napi_default);
547
+ Napi::Value value,
548
+ napi_property_attributes attributes = napi_default);
512
549
  ```
513
550
 
514
551
  - `[in] name`: The `Napi::Symbol` object whose value is used to identify the
515
552
  name of the static property.
516
553
  - `[in] value`: The value that's retrieved by a get access of the property.
517
- - `[in] attributes`: The attributes to be associated with the property in addition
518
- to the napi_static attribute. One or more of `napi_property_attributes`.
554
+ - `[in] attributes`: The attributes to be associated with the property in
555
+ addition to the napi_static attribute. One or more of
556
+ `napi_property_attributes`.
519
557
 
520
558
  Returns `Napi::PropertyDescriptor` object that represents an static value
521
559
  property of a JavaScript class
522
560
 
523
- ### InstanceValue
524
-
525
- Creates property descriptor that represents an instance value property of a
526
- JavaScript class.
527
- ```cpp
528
- static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceValue(const char* utf8name,
529
- Napi::Value value,
530
- napi_property_attributes attributes = napi_default);
531
- ```
532
-
533
- - `[in] utf8name`: Null-terminated string that represents the name of the property.
534
- - `[in] value`: The value that's retrieved by a get access of the property.
535
- - `[in] attributes`: The attributes to be associated with the property.
536
- One or more of `napi_property_attributes`.
537
-
538
- Returns `Napi::PropertyDescriptor` object that represents an instance value
539
- property of a JavaScript class.
540
-
541
- ### InstanceValue
542
-
543
- Creates property descriptor that represents an instance value property of a
544
- JavaScript class.
545
- ```cpp
546
- static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceValue(Symbol name,
547
- Napi::Value value,
548
- napi_property_attributes attributes = napi_default);
549
- ```
550
-
551
- - `[in] name`: The `Napi::Symbol` object whose value is used to identify the
552
- name of the property.
553
- - `[in] value`: The value that's retrieved by a get access of the property.
554
- - `[in] attributes`: The attributes to be associated with the property.
555
- One or more of `napi_property_attributes`.
556
-
557
- Returns `Napi::PropertyDescriptor` object that represents an instance value
561
+ [`Napi::InstanceWrap<T>`]: ./instance_wrap.md