node-addon-api 2.0.1 → 3.0.2
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/.travis.yml +1 -4
- package/CHANGELOG.md +172 -10
- package/README.md +81 -30
- package/appveyor.yml +3 -14
- package/benchmark/README.md +47 -0
- package/benchmark/binding.gyp +25 -0
- package/benchmark/function_args.cc +217 -0
- package/benchmark/function_args.js +60 -0
- package/benchmark/index.js +34 -0
- package/benchmark/property_descriptor.cc +91 -0
- package/benchmark/property_descriptor.js +37 -0
- package/common.gypi +21 -0
- package/doc/addon.md +157 -0
- package/doc/array.md +81 -0
- package/doc/array_buffer.md +4 -0
- package/doc/async_worker.md +33 -4
- package/doc/{async_progress_worker.md → async_worker_variants.md} +115 -3
- package/doc/bigint.md +7 -2
- package/doc/boolean.md +4 -0
- package/doc/buffer.md +4 -0
- package/doc/class_property_descriptor.md +3 -3
- package/doc/creating_a_release.md +5 -5
- package/doc/dataview.md +4 -0
- package/doc/date.md +2 -2
- package/doc/env.md +69 -0
- package/doc/error.md +5 -0
- package/doc/external.md +4 -0
- package/doc/function.md +109 -1
- package/doc/hierarchy.md +91 -0
- package/doc/instance_wrap.md +408 -0
- package/doc/name.md +29 -0
- package/doc/object.md +44 -1
- package/doc/object_lifetime_management.md +1 -1
- package/doc/object_wrap.md +219 -215
- package/doc/promises.md +5 -0
- package/doc/property_descriptor.md +64 -9
- package/doc/setup.md +1 -2
- package/doc/string.md +5 -1
- package/doc/symbol.md +5 -1
- package/doc/typed_array.md +4 -0
- package/doc/typed_array_of.md +4 -0
- package/doc/value.md +166 -104
- package/except.gypi +16 -0
- package/index.js +7 -41
- package/napi-inl.h +1116 -400
- package/napi.h +414 -142
- package/node_api.gyp +9 -0
- package/noexcept.gypi +16 -0
- package/{src/nothing.c → nothing.c} +0 -0
- package/package.json +63 -1
- package/tools/README.md +4 -4
- package/tools/conversion.js +4 -8
- package/doc/basic_types.md +0 -423
- package/doc/working_with_javascript_values.md +0 -14
- package/external-napi/node_api.h +0 -7
- package/src/node_api.cc +0 -3655
- package/src/node_api.gyp +0 -21
- package/src/node_api.h +0 -588
- package/src/node_api_types.h +0 -115
- package/src/node_internals.cc +0 -142
- package/src/node_internals.h +0 -157
- package/src/util-inl.h +0 -38
- package/src/util.h +0 -7
package/doc/array.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Array
|
|
2
|
+
|
|
3
|
+
Class [`Napi::Array`][] inherits from class [`Napi::Object`][].
|
|
4
|
+
|
|
5
|
+
Arrays are native representations of JavaScript Arrays. `Napi::Array` is a wrapper
|
|
6
|
+
around `napi_value` representing a JavaScript Array.
|
|
7
|
+
|
|
8
|
+
[`Napi::TypedArray`][] and [`Napi::ArrayBuffer`][] correspond to JavaScript data
|
|
9
|
+
types such as [`Napi::Int32Array`][] and [`Napi::ArrayBuffer`][], respectively,
|
|
10
|
+
that can be used for transferring large amounts of data from JavaScript to the
|
|
11
|
+
native side. An example illustrating the use of a JavaScript-provided
|
|
12
|
+
`ArrayBuffer` in native code is available [here](https://github.com/nodejs/node-addon-examples/tree/master/array_buffer_to_native/node-addon-api).
|
|
13
|
+
|
|
14
|
+
## Constructor
|
|
15
|
+
```cpp
|
|
16
|
+
Napi::Array::Array();
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Returns an empty array.
|
|
20
|
+
|
|
21
|
+
If an error occurs, a `Napi::Error` will be thrown. If C++ exceptions are not
|
|
22
|
+
being used, callers should check the result of `Env::IsExceptionPending` before
|
|
23
|
+
attempting to use the returned value.
|
|
24
|
+
|
|
25
|
+
```cpp
|
|
26
|
+
Napi::Array::Array(napi_env env, napi_value value);
|
|
27
|
+
```
|
|
28
|
+
- `[in] env` - The environment in which to create the array.
|
|
29
|
+
- `[in] value` - The primitive to wrap.
|
|
30
|
+
|
|
31
|
+
Returns a `Napi::Array` wrapping a `napi_value`.
|
|
32
|
+
|
|
33
|
+
If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not
|
|
34
|
+
being used, callers should check the result of `Env::IsExceptionPending` before
|
|
35
|
+
attempting to use the returned value.
|
|
36
|
+
|
|
37
|
+
## Methods
|
|
38
|
+
|
|
39
|
+
### New
|
|
40
|
+
```cpp
|
|
41
|
+
static Napi::Array Napi::Array::New(napi_env env);
|
|
42
|
+
```
|
|
43
|
+
- `[in] env` - The environment in which to create the array.
|
|
44
|
+
|
|
45
|
+
Returns a new `Napi::Array`.
|
|
46
|
+
|
|
47
|
+
If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not
|
|
48
|
+
being used, callers should check the result of `Env::IsExceptionPending` before
|
|
49
|
+
attempting to use the returned value.
|
|
50
|
+
|
|
51
|
+
### New
|
|
52
|
+
|
|
53
|
+
```cpp
|
|
54
|
+
static Napi::Array Napi::Array::New(napi_env env, size_t length);
|
|
55
|
+
```
|
|
56
|
+
- `[in] env` - The environment in which to create the array.
|
|
57
|
+
- `[in] length` - The length of the array.
|
|
58
|
+
|
|
59
|
+
Returns a new `Napi::Array` with the given length.
|
|
60
|
+
|
|
61
|
+
If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not
|
|
62
|
+
being used, callers should check the result of `Env::IsExceptionPending` before
|
|
63
|
+
attempting to use the returned value.
|
|
64
|
+
|
|
65
|
+
### Length
|
|
66
|
+
```cpp
|
|
67
|
+
uint32_t Napi::Array::Length() const;
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Returns the length of the array.
|
|
71
|
+
|
|
72
|
+
Note:
|
|
73
|
+
This can execute JavaScript code implicitly according to JavaScript semantics.
|
|
74
|
+
If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not
|
|
75
|
+
being used, callers should check the result of `Env::IsExceptionPending` before
|
|
76
|
+
attempting to use the returned value.
|
|
77
|
+
|
|
78
|
+
[`Napi::ArrayBuffer`]: ./array_buffer.md
|
|
79
|
+
[`Napi::Int32Array`]: ./typed_array_of.md
|
|
80
|
+
[`Napi::Object`]: ./object.md
|
|
81
|
+
[`Napi::TypedArray`]: ./typed_array.md
|
package/doc/array_buffer.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# ArrayBuffer
|
|
2
2
|
|
|
3
|
+
Class `Napi::ArrayBuffer` inherits from class [`Napi::Object`][].
|
|
4
|
+
|
|
3
5
|
The `Napi::ArrayBuffer` class corresponds to the
|
|
4
6
|
[JavaScript `ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
|
|
5
7
|
class.
|
|
@@ -127,3 +129,5 @@ void* Napi::ArrayBuffer::Data() const;
|
|
|
127
129
|
```
|
|
128
130
|
|
|
129
131
|
Returns a pointer the wrapped data.
|
|
132
|
+
|
|
133
|
+
[`Napi::Object`]: ./object.md
|
package/doc/async_worker.md
CHANGED
|
@@ -136,6 +136,35 @@ class was created, passing in the error as the first parameter.
|
|
|
136
136
|
virtual void Napi::AsyncWorker::OnError(const Napi::Error& e);
|
|
137
137
|
```
|
|
138
138
|
|
|
139
|
+
### OnWorkComplete
|
|
140
|
+
|
|
141
|
+
This method is invoked after the work has completed on JavaScript thread.
|
|
142
|
+
The default implementation of this method checks the status of the work and
|
|
143
|
+
tries to dispatch the result to `Napi::AsyncWorker::OnOk` or `Napi::AsyncWorker::Error`
|
|
144
|
+
if the work has committed an error. If the work was cancelled, neither
|
|
145
|
+
`Napi::AsyncWorker::OnOk` nor `Napi::AsyncWorker::Error` will be invoked.
|
|
146
|
+
After the result is dispatched, the default implementation will call into
|
|
147
|
+
`Napi::AsyncWorker::Destroy` if `SuppressDestruct()` was not called.
|
|
148
|
+
|
|
149
|
+
```cpp
|
|
150
|
+
virtual void OnWorkComplete(Napi::Env env, napi_status status);
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### OnExecute
|
|
154
|
+
|
|
155
|
+
This method is invoked immediately on the work thread when scheduled.
|
|
156
|
+
The default implementation of this method just calls the `Napi::AsyncWorker::Execute`
|
|
157
|
+
and handles exceptions if cpp exceptions were enabled.
|
|
158
|
+
|
|
159
|
+
The `OnExecute` method receives an `napi_env` argument. However, the `napi_env`
|
|
160
|
+
must NOT be used within this method, as it does not run on the JavaScript
|
|
161
|
+
thread and must not run any method that would cause JavaScript to run. In
|
|
162
|
+
practice, this means that almost any use of `napi_env` will be incorrect.
|
|
163
|
+
|
|
164
|
+
```cpp
|
|
165
|
+
virtual void OnExecute(Napi::Env env);
|
|
166
|
+
```
|
|
167
|
+
|
|
139
168
|
### Destroy
|
|
140
169
|
|
|
141
170
|
This method is invoked when the instance must be deallocated. If
|
|
@@ -342,7 +371,7 @@ The code below shows a basic example of `Napi::AsyncWorker` the implementation:
|
|
|
342
371
|
#include <chrono>
|
|
343
372
|
#include <thread>
|
|
344
373
|
|
|
345
|
-
|
|
374
|
+
using namespace Napi;
|
|
346
375
|
|
|
347
376
|
class EchoWorker : public AsyncWorker {
|
|
348
377
|
public:
|
|
@@ -351,12 +380,12 @@ class EchoWorker : public AsyncWorker {
|
|
|
351
380
|
|
|
352
381
|
~EchoWorker() {}
|
|
353
382
|
// This code will be executed on the worker thread
|
|
354
|
-
void Execute() {
|
|
383
|
+
void Execute() override {
|
|
355
384
|
// Need to simulate cpu heavy task
|
|
356
385
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
357
386
|
}
|
|
358
387
|
|
|
359
|
-
void OnOK() {
|
|
388
|
+
void OnOK() override {
|
|
360
389
|
HandleScope scope(Env());
|
|
361
390
|
Callback().Call({Env().Null(), String::New(Env(), echo)});
|
|
362
391
|
}
|
|
@@ -380,7 +409,7 @@ The following code shows an example of how to create and use an `Napi::AsyncWork
|
|
|
380
409
|
// Include EchoWorker class
|
|
381
410
|
// ..
|
|
382
411
|
|
|
383
|
-
|
|
412
|
+
using namespace Napi;
|
|
384
413
|
|
|
385
414
|
Value Echo(const CallbackInfo& info) {
|
|
386
415
|
// You need to validate the arguments here.
|
|
@@ -272,12 +272,12 @@ called and are executed as part of the event loop.
|
|
|
272
272
|
The code below shows a basic example of the `Napi::AsyncProgressWorker` implementation:
|
|
273
273
|
|
|
274
274
|
```cpp
|
|
275
|
-
#include<napi.h>
|
|
275
|
+
#include <napi.h>
|
|
276
276
|
|
|
277
277
|
#include <chrono>
|
|
278
278
|
#include <thread>
|
|
279
279
|
|
|
280
|
-
|
|
280
|
+
using namespace Napi;
|
|
281
281
|
|
|
282
282
|
class EchoWorker : public AsyncProgressWorker<uint32_t> {
|
|
283
283
|
public:
|
|
@@ -323,7 +323,7 @@ The following code shows an example of how to create and use an `Napi::AsyncProg
|
|
|
323
323
|
// Include EchoWorker class
|
|
324
324
|
// ..
|
|
325
325
|
|
|
326
|
-
|
|
326
|
+
using namespace Napi;
|
|
327
327
|
|
|
328
328
|
Value Echo(const CallbackInfo& info) {
|
|
329
329
|
// We need to validate the arguments here
|
|
@@ -341,4 +341,116 @@ asynchronous task ends and other data needed for the computation. Once created,
|
|
|
341
341
|
the only other action needed is to call the `Napi::AsyncProgressWorker::Queue`
|
|
342
342
|
method that will queue the created worker for execution.
|
|
343
343
|
|
|
344
|
+
# AsyncProgressQueueWorker
|
|
345
|
+
|
|
346
|
+
`Napi::AsyncProgressQueueWorker` acts exactly like `Napi::AsyncProgressWorker`
|
|
347
|
+
except that each progress committed by `Napi::AsyncProgressQueueWorker::ExecutionProgress::Send`
|
|
348
|
+
during `Napi::AsyncProgressQueueWorker::Execute` is guaranteed to be
|
|
349
|
+
processed by `Napi::AsyncProgressQueueWorker::OnProgress` on the JavaScript
|
|
350
|
+
thread in the order it was committed.
|
|
351
|
+
|
|
352
|
+
For the most basic use, only the `Napi::AsyncProgressQueueWorker::Execute` and
|
|
353
|
+
`Napi::AsyncProgressQueueWorker::OnProgress` method must be implemented in a subclass.
|
|
354
|
+
|
|
355
|
+
# AsyncProgressQueueWorker::ExecutionProcess
|
|
356
|
+
|
|
357
|
+
A bridge class created before the worker thread execution of `Napi::AsyncProgressQueueWorker::Execute`.
|
|
358
|
+
|
|
359
|
+
## Methods
|
|
360
|
+
|
|
361
|
+
### Send
|
|
362
|
+
|
|
363
|
+
`Napi::AsyncProgressQueueWorker::ExecutionProcess::Send` takes two arguments, a pointer
|
|
364
|
+
to a generic type of data, and a `size_t` to indicate how many items the pointer is
|
|
365
|
+
pointing to.
|
|
366
|
+
|
|
367
|
+
The data pointed to will be copied to internal slots of `Napi::AsyncProgressQueueWorker` so
|
|
368
|
+
after the call to `Napi::AsyncProgressQueueWorker::ExecutionProcess::Send` the data can
|
|
369
|
+
be safely released.
|
|
370
|
+
|
|
371
|
+
`Napi::AsyncProgressQueueWorker::ExecutionProcess::Send` guarantees invocation
|
|
372
|
+
of `Napi::AsyncProgressQueueWorker::OnProgress`, which means multiple `Send`
|
|
373
|
+
call will result in the in-order invocation of `Napi::AsyncProgressQueueWorker::OnProgress`
|
|
374
|
+
with each data item.
|
|
375
|
+
|
|
376
|
+
```cpp
|
|
377
|
+
void Napi::AsyncProgressQueueWorker::ExecutionProcess::Send(const T* data, size_t count) const;
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
## Example
|
|
381
|
+
|
|
382
|
+
The code below shows a basic example of the `Napi::AsyncProgressQueueWorker` implementation:
|
|
383
|
+
|
|
384
|
+
```cpp
|
|
385
|
+
#include <napi.h>
|
|
386
|
+
|
|
387
|
+
#include <chrono>
|
|
388
|
+
#include <thread>
|
|
389
|
+
|
|
390
|
+
using namespace Napi;
|
|
391
|
+
|
|
392
|
+
class EchoWorker : public AsyncProgressQueueWorker<uint32_t> {
|
|
393
|
+
public:
|
|
394
|
+
EchoWorker(Function& callback, std::string& echo)
|
|
395
|
+
: AsyncProgressQueueWorker(callback), echo(echo) {}
|
|
396
|
+
|
|
397
|
+
~EchoWorker() {}
|
|
398
|
+
// This code will be executed on the worker thread
|
|
399
|
+
void Execute(const ExecutionProgress& progress) {
|
|
400
|
+
// Need to simulate cpu heavy task
|
|
401
|
+
for (uint32_t i = 0; i < 100; ++i) {
|
|
402
|
+
progress.Send(&i, 1);
|
|
403
|
+
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
void OnOK() {
|
|
408
|
+
HandleScope scope(Env());
|
|
409
|
+
Callback().Call({Env().Null(), String::New(Env(), echo)});
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
void OnProgress(const uint32_t* data, size_t /* count */) {
|
|
413
|
+
HandleScope scope(Env());
|
|
414
|
+
Callback().Call({Env().Null(), Env().Null(), Number::New(Env(), *data)});
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
private:
|
|
418
|
+
std::string echo;
|
|
419
|
+
};
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
The `EchoWorker`'s constructor calls the base class' constructor to pass in the
|
|
423
|
+
callback that the `Napi::AsyncProgressQueueWorker` base class will store
|
|
424
|
+
persistently. When the work on the `Napi::AsyncProgressQueueWorker::Execute`
|
|
425
|
+
method is done the `Napi::AsyncProgressQueueWorker::OnOk` method is called and
|
|
426
|
+
the results are returned back to JavaScript when the stored callback is invoked
|
|
427
|
+
with its associated environment.
|
|
428
|
+
|
|
429
|
+
The following code shows an example of how to create and use an
|
|
430
|
+
`Napi::AsyncProgressQueueWorker`.
|
|
431
|
+
|
|
432
|
+
```cpp
|
|
433
|
+
#include <napi.h>
|
|
434
|
+
|
|
435
|
+
// Include EchoWorker class
|
|
436
|
+
// ..
|
|
437
|
+
|
|
438
|
+
using namespace Napi;
|
|
439
|
+
|
|
440
|
+
Value Echo(const CallbackInfo& info) {
|
|
441
|
+
// We need to validate the arguments here.
|
|
442
|
+
Function cb = info[1].As<Function>();
|
|
443
|
+
std::string in = info[0].As<String>();
|
|
444
|
+
EchoWorker* wk = new EchoWorker(cb, in);
|
|
445
|
+
wk->Queue();
|
|
446
|
+
return info.Env().Undefined();
|
|
447
|
+
}
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
The implementation of a `Napi::AsyncProgressQueueWorker` can be used by creating a
|
|
451
|
+
new instance and passing to its constructor the callback to execute when the
|
|
452
|
+
asynchronous task ends and other data needed for the computation. Once created,
|
|
453
|
+
the only other action needed is to call the `Napi::AsyncProgressQueueWorker::Queue`
|
|
454
|
+
method that will queue the created worker for execution.
|
|
455
|
+
|
|
344
456
|
[`Napi::AsyncWorker`]: ./async_worker.md
|
package/doc/bigint.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# BigInt
|
|
2
2
|
|
|
3
|
+
Class `Napi::Bigint` inherits from class [`Napi::Value`][].
|
|
4
|
+
|
|
3
5
|
A JavaScript BigInt value.
|
|
4
6
|
|
|
5
7
|
## Methods
|
|
@@ -8,6 +10,7 @@ A JavaScript BigInt value.
|
|
|
8
10
|
|
|
9
11
|
```cpp
|
|
10
12
|
static Napi::BigInt Napi::BigInt::New(Napi::Env env, int64_t value);
|
|
13
|
+
static Napi::BigInt Napi::BigInt::New(Napi::Env env, uint64_t value);
|
|
11
14
|
```
|
|
12
15
|
|
|
13
16
|
- `[in] env`: The environment in which to construct the `Napi::BigInt` object.
|
|
@@ -47,7 +50,7 @@ Returns a new empty JavaScript `Napi::BigInt`.
|
|
|
47
50
|
### Int64Value
|
|
48
51
|
|
|
49
52
|
```cpp
|
|
50
|
-
int64_t Napi::
|
|
53
|
+
int64_t Napi::BigInt::Int64Value(bool* lossless) const;
|
|
51
54
|
```
|
|
52
55
|
|
|
53
56
|
- `[out] lossless`: Indicates whether the `BigInt` value was converted losslessly.
|
|
@@ -78,7 +81,7 @@ Returns the number of words needed to store this `BigInt` value.
|
|
|
78
81
|
### ToWords
|
|
79
82
|
|
|
80
83
|
```cpp
|
|
81
|
-
void Napi::BigInt::ToWords(
|
|
84
|
+
void Napi::BigInt::ToWords(int* sign_bit, size_t* word_count, uint64_t* words);
|
|
82
85
|
```
|
|
83
86
|
|
|
84
87
|
- `[out] sign_bit`: Integer representing if the JavaScript `BigInt` is positive
|
|
@@ -90,3 +93,5 @@ void Napi::BigInt::ToWords(size_t* word_count, int* sign_bit, uint64_t* words);
|
|
|
90
93
|
|
|
91
94
|
Returns a single `BigInt` value into a sign bit, 64-bit little-endian array,
|
|
92
95
|
and the number of elements in the array.
|
|
96
|
+
|
|
97
|
+
[`Napi::Value`]: ./value.md
|
package/doc/boolean.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Boolean
|
|
2
2
|
|
|
3
|
+
Class `Napi::Boolean` inherits from class [`Napi::Value`][].
|
|
4
|
+
|
|
3
5
|
`Napi::Boolean` class is a representation of the JavaScript `Boolean` object. The
|
|
4
6
|
`Napi::Boolean` class inherits its behavior from the `Napi::Value` class
|
|
5
7
|
(for more info see: [`Napi::Value`](value.md)).
|
|
@@ -62,3 +64,5 @@ Napi::Boolean::operator bool() const;
|
|
|
62
64
|
```
|
|
63
65
|
|
|
64
66
|
Returns the boolean primitive type of the corresponding `Napi::Boolean` object.
|
|
67
|
+
|
|
68
|
+
[`Napi::Value`]: ./value.md
|
package/doc/buffer.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Buffer
|
|
2
2
|
|
|
3
|
+
Class `Napi::Buffer` inherits from class [`Napi::Uint8Array`][].
|
|
4
|
+
|
|
3
5
|
The `Napi::Buffer` class creates a projection of raw data that can be consumed by
|
|
4
6
|
script.
|
|
5
7
|
|
|
@@ -138,3 +140,5 @@ size_t Napi::Buffer::Length() const;
|
|
|
138
140
|
```
|
|
139
141
|
|
|
140
142
|
Returns the number of `T` elements in the external data.
|
|
143
|
+
|
|
144
|
+
[`Napi::Uint8Array`]: ./typed_array_of.md
|
|
@@ -26,9 +26,9 @@ class Example : public Napi::ObjectWrap<Example> {
|
|
|
26
26
|
Napi::Object Example::Init(Napi::Env env, Napi::Object exports) {
|
|
27
27
|
Napi::Function func = DefineClass(env, "Example", {
|
|
28
28
|
// Register a class instance accessor with getter and setter functions.
|
|
29
|
-
InstanceAccessor
|
|
30
|
-
// We can also register a readonly accessor by
|
|
31
|
-
InstanceAccessor("readOnlyProp"
|
|
29
|
+
InstanceAccessor<&Example::GetValue, &Example::SetValue>("value"),
|
|
30
|
+
// We can also register a readonly accessor by omitting the setter.
|
|
31
|
+
InstanceAccessor<&Example::GetValue>("readOnlyProp")
|
|
32
32
|
});
|
|
33
33
|
|
|
34
34
|
constructor = Napi::Persistent(func);
|
|
@@ -13,7 +13,7 @@ tools:
|
|
|
13
13
|
|
|
14
14
|
* [Changelog maker](https://www.npmjs.com/package/changelog-maker)
|
|
15
15
|
|
|
16
|
-
If not please follow the instruction reported in the tool's documentation to
|
|
16
|
+
If not please follow the instruction reported in the tool's documentation to
|
|
17
17
|
install it.
|
|
18
18
|
|
|
19
19
|
## Publish new release
|
|
@@ -27,13 +27,13 @@ new release. Give people some time to comment or suggest PRs that should land fi
|
|
|
27
27
|
|
|
28
28
|
* Update the version in **package.json** appropriately.
|
|
29
29
|
|
|
30
|
-
* Update the [README.md](https://github.com/nodejs/node-addon-api/blob/master/README.md)
|
|
30
|
+
* Update the [README.md](https://github.com/nodejs/node-addon-api/blob/master/README.md)
|
|
31
31
|
to show the new version as the latest.
|
|
32
32
|
|
|
33
33
|
* Generate the changelog for the new version using **changelog maker** tool. From
|
|
34
34
|
the route folder of the repo launch the following command:
|
|
35
35
|
|
|
36
|
-
```bash
|
|
36
|
+
```bash
|
|
37
37
|
> changelog-maker
|
|
38
38
|
```
|
|
39
39
|
* Use the output generated by **changelog maker** to pdate the [CHANGELOG.md](https://github.com/nodejs/node-addon-api/blob/master/CHANGELOG.md)
|
|
@@ -43,8 +43,8 @@ following the style used in publishing the previous release.
|
|
|
43
43
|
|
|
44
44
|
* Validate all tests pass by running npm test on master.
|
|
45
45
|
|
|
46
|
-
* Use **[CI](https://ci.nodejs.org/view/x%20-%20Abi%20stable%20module%20API/job/node-test-node-addon-api/)**
|
|
47
|
-
to validate tests pass
|
|
46
|
+
* Use **[CI](https://ci.nodejs.org/view/x%20-%20Abi%20stable%20module%20API/job/node-test-node-addon-api-new/)**
|
|
47
|
+
to validate tests pass (note there are still some issues on SmartOS and
|
|
48
48
|
Windows in the testing).
|
|
49
49
|
|
|
50
50
|
* Do a clean checkout of node-addon-api.
|
package/doc/dataview.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# DataView
|
|
2
2
|
|
|
3
|
+
Class `Napi::DataView` inherits from class [`Napi::Object`][].
|
|
4
|
+
|
|
3
5
|
The `Napi::DataView` class corresponds to the
|
|
4
6
|
[JavaScript `DataView`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView)
|
|
5
7
|
class.
|
|
@@ -242,3 +244,5 @@ void Napi::DataView::SetUint32(size_t byteOffset, uint32_t value) const;
|
|
|
242
244
|
|
|
243
245
|
- `[in] byteOffset`: The offset, in byte, from the start of the view where to read the data.
|
|
244
246
|
- `[in] value`: The value to set.
|
|
247
|
+
|
|
248
|
+
[`Napi::Object`]: ./object.md
|
package/doc/date.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# Date
|
|
2
2
|
|
|
3
3
|
`Napi::Date` class is a representation of the JavaScript `Date` object. The
|
|
4
|
-
`Napi::Date` class inherits its behavior from `Napi::Value` class
|
|
5
|
-
(for more info see [`Napi::Value`](value.md))
|
|
4
|
+
`Napi::Date` class inherits its behavior from the `Napi::Value` class
|
|
5
|
+
(for more info see [`Napi::Value`](value.md)).
|
|
6
6
|
|
|
7
7
|
## Methods
|
|
8
8
|
|
package/doc/env.md
CHANGED
|
@@ -61,3 +61,72 @@ Napi::Error Napi::Env::GetAndClearPendingException();
|
|
|
61
61
|
```
|
|
62
62
|
|
|
63
63
|
Returns an `Napi::Error` object representing the environment's pending exception, if any.
|
|
64
|
+
|
|
65
|
+
### RunScript
|
|
66
|
+
|
|
67
|
+
```cpp
|
|
68
|
+
Napi::Value Napi::Env::RunScript(____ script);
|
|
69
|
+
```
|
|
70
|
+
- `[in] script`: A string containing JavaScript code to execute.
|
|
71
|
+
|
|
72
|
+
Runs JavaScript code contained in a string and returns its result.
|
|
73
|
+
|
|
74
|
+
The `script` can be any of the following types:
|
|
75
|
+
- [`Napi::String`](string.md)
|
|
76
|
+
- `const char *`
|
|
77
|
+
- `const std::string &`
|
|
78
|
+
|
|
79
|
+
### GetInstanceData
|
|
80
|
+
```cpp
|
|
81
|
+
template <typename T> T* GetInstanceData();
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Returns the instance data that was previously associated with the environment,
|
|
85
|
+
or `nullptr` if none was associated.
|
|
86
|
+
|
|
87
|
+
### SetInstanceData
|
|
88
|
+
|
|
89
|
+
```cpp
|
|
90
|
+
template <typename T> using Finalizer = void (*)(Env, T*);
|
|
91
|
+
template <typename T, Finalizer<T> fini = Env::DefaultFini<T>>
|
|
92
|
+
void SetInstanceData(T* data);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
- `[template] fini`: A function to call when the instance data is to be deleted.
|
|
96
|
+
Accepts a function of the form `void CleanupData(Napi::Env env, T* data)`. If
|
|
97
|
+
not given, the default finalizer will be used, which simply uses the `delete`
|
|
98
|
+
operator to destroy `T*` when the addon instance is unloaded.
|
|
99
|
+
- `[in] data`: A pointer to data that will be associated with the instance of
|
|
100
|
+
the addon for the duration of its lifecycle.
|
|
101
|
+
|
|
102
|
+
Associates a data item stored at `T* data` with the current instance of the
|
|
103
|
+
addon. The item will be passed to the function `fini` which gets called when an
|
|
104
|
+
instance of the addon is unloaded.
|
|
105
|
+
|
|
106
|
+
### SetInstanceData
|
|
107
|
+
|
|
108
|
+
```cpp
|
|
109
|
+
template <typename DataType, typename HintType>
|
|
110
|
+
using FinalizerWithHint = void (*)(Env, DataType*, HintType*);
|
|
111
|
+
template <typename DataType,
|
|
112
|
+
typename HintType,
|
|
113
|
+
FinalizerWithHint<DataType, HintType> fini =
|
|
114
|
+
Env::DefaultFiniWithHint<DataType, HintType>>
|
|
115
|
+
void SetInstanceData(DataType* data, HintType* hint);
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
- `[template] fini`: A function to call when the instance data is to be deleted.
|
|
119
|
+
Accepts a function of the form
|
|
120
|
+
`void CleanupData(Napi::Env env, DataType* data, HintType* hint)`. If not given,
|
|
121
|
+
the default finalizer will be used, which simply uses the `delete` operator to
|
|
122
|
+
destroy `T*` when the addon instance is unloaded.
|
|
123
|
+
- `[in] data`: A pointer to data that will be associated with the instance of
|
|
124
|
+
the addon for the duration of its lifecycle.
|
|
125
|
+
- `[in] hint`: A pointer to data that will be associated with the instance of
|
|
126
|
+
the addon for the duration of its lifecycle and will be passed as a hint to
|
|
127
|
+
`fini` when the addon instance is unloaded.
|
|
128
|
+
|
|
129
|
+
Associates a data item stored at `T* data` with the current instance of the
|
|
130
|
+
addon. The item will be passed to the function `fini` which gets called when an
|
|
131
|
+
instance of the addon is unloaded. This overload accepts an additional hint to
|
|
132
|
+
be passed to `fini`.
|
package/doc/error.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Error
|
|
2
2
|
|
|
3
|
+
Class `Napi::Error` inherits from class [`Napi::ObjectReference`][] and class [`std::exception`][].
|
|
4
|
+
|
|
3
5
|
The `Napi::Error` class is a representation of the JavaScript `Error` object that is thrown
|
|
4
6
|
when runtime errors occur. The Error object can also be used as a base object for
|
|
5
7
|
user-defined exceptions.
|
|
@@ -113,3 +115,6 @@ const char* Napi::Error::what() const NAPI_NOEXCEPT override;
|
|
|
113
115
|
|
|
114
116
|
Returns a pointer to a null-terminated string that is used to identify the
|
|
115
117
|
exception. This method can be used only if the exception mechanism is enabled.
|
|
118
|
+
|
|
119
|
+
[`Napi::ObjectReference`]: ./object_reference.md
|
|
120
|
+
[`std::exception`]: http://cplusplus.com/reference/exception/exception/
|
package/doc/external.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# External (template)
|
|
2
2
|
|
|
3
|
+
Class `Napi::External<T>` inherits from class [`Napi::Value`][].
|
|
4
|
+
|
|
3
5
|
The `Napi::External` template class implements the ability to create a `Napi::Value` object with arbitrary C++ data. It is the user's responsibility to manage the memory for the arbitrary C++ data.
|
|
4
6
|
|
|
5
7
|
`Napi::External` objects can be created with an optional Finalizer function and optional Hint value. The Finalizer function, if specified, is called when your `Napi::External` object is released by Node's garbage collector. It gives your code the opportunity to free any dynamically created data. If you specify a Hint value, it is passed to your Finalizer function.
|
|
@@ -57,3 +59,5 @@ T* Napi::External::Data() const;
|
|
|
57
59
|
```
|
|
58
60
|
|
|
59
61
|
Returns a pointer to the arbitrary C++ data held by the `Napi::External` object.
|
|
62
|
+
|
|
63
|
+
[`Napi::Value`]: ./value.md
|