node-addon-api 2.0.2 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.travis.yml +1 -4
- package/CHANGELOG.md +88 -30
- package/README.md +53 -7
- package/benchmark/README.md +47 -0
- package/benchmark/binding.gyp +25 -0
- package/benchmark/function_args.cc +153 -0
- package/benchmark/function_args.js +52 -0
- package/benchmark/index.js +34 -0
- package/benchmark/property_descriptor.cc +60 -0
- package/benchmark/property_descriptor.js +29 -0
- package/common.gypi +21 -0
- package/doc/async_worker.md +33 -4
- package/doc/{async_progress_worker.md → async_worker_variants.md} +115 -3
- package/doc/bigint.md +2 -1
- package/doc/class_property_descriptor.md +3 -3
- package/doc/creating_a_release.md +5 -5
- package/doc/env.md +14 -0
- package/doc/function.md +108 -1
- package/doc/object.md +40 -1
- package/doc/object_lifetime_management.md +1 -1
- package/doc/object_wrap.md +278 -2
- package/doc/property_descriptor.md +64 -9
- package/doc/setup.md +0 -1
- package/doc/string.md +1 -1
- package/doc/symbol.md +1 -1
- package/doc/value.md +1 -1
- package/except.gypi +16 -0
- package/index.js +5 -42
- package/napi-inl.h +727 -141
- package/napi.h +338 -83
- package/node_api.gyp +9 -0
- package/noexcept.gypi +16 -0
- package/{src/nothing.c → nothing.c} +0 -0
- package/package.json +33 -1
- package/tools/README.md +4 -4
- package/tools/conversion.js +0 -4
- 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
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#include "napi.h"
|
|
2
|
+
|
|
3
|
+
static napi_value Getter_Core(napi_env env, napi_callback_info info) {
|
|
4
|
+
(void) info;
|
|
5
|
+
napi_value result;
|
|
6
|
+
napi_status status = napi_create_uint32(env, 42, &result);
|
|
7
|
+
NAPI_THROW_IF_FAILED(env, status, nullptr);
|
|
8
|
+
return result;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
static napi_value Setter_Core(napi_env env, napi_callback_info info) {
|
|
12
|
+
size_t argc = 1;
|
|
13
|
+
napi_value argv;
|
|
14
|
+
napi_status status =
|
|
15
|
+
napi_get_cb_info(env, info, &argc, &argv, nullptr, nullptr);
|
|
16
|
+
NAPI_THROW_IF_FAILED(env, status, nullptr);
|
|
17
|
+
(void) argv;
|
|
18
|
+
return nullptr;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static Napi::Value Getter(const Napi::CallbackInfo& info) {
|
|
22
|
+
return Napi::Number::New(info.Env(), 42);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static void Setter(const Napi::CallbackInfo& info) {
|
|
26
|
+
(void) info[0];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
30
|
+
napi_status status;
|
|
31
|
+
napi_property_descriptor core_prop = {
|
|
32
|
+
"core",
|
|
33
|
+
nullptr,
|
|
34
|
+
nullptr,
|
|
35
|
+
Getter_Core,
|
|
36
|
+
Setter_Core,
|
|
37
|
+
nullptr,
|
|
38
|
+
napi_enumerable,
|
|
39
|
+
nullptr
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
status = napi_define_properties(env, exports, 1, &core_prop);
|
|
43
|
+
NAPI_THROW_IF_FAILED(env, status, Napi::Object());
|
|
44
|
+
|
|
45
|
+
exports.DefineProperty(
|
|
46
|
+
Napi::PropertyDescriptor::Accessor(env,
|
|
47
|
+
exports,
|
|
48
|
+
"cplusplus",
|
|
49
|
+
Getter,
|
|
50
|
+
Setter,
|
|
51
|
+
napi_enumerable));
|
|
52
|
+
|
|
53
|
+
exports.DefineProperty(
|
|
54
|
+
Napi::PropertyDescriptor::Accessor<Getter, Setter>("templated",
|
|
55
|
+
napi_enumerable));
|
|
56
|
+
|
|
57
|
+
return exports;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const Benchmark = require('benchmark');
|
|
3
|
+
const addonName = path.basename(__filename, '.js');
|
|
4
|
+
|
|
5
|
+
[ addonName, addonName + '_noexcept' ]
|
|
6
|
+
.forEach((addonName) => {
|
|
7
|
+
const rootAddon = require(`./build/Release/${addonName}`);
|
|
8
|
+
const getters = new Benchmark.Suite;
|
|
9
|
+
const setters = new Benchmark.Suite;
|
|
10
|
+
|
|
11
|
+
console.log(`${addonName}: `);
|
|
12
|
+
|
|
13
|
+
Object.keys(rootAddon).forEach((key) => {
|
|
14
|
+
getters.add(`${key} getter`, () => {
|
|
15
|
+
const x = rootAddon[key];
|
|
16
|
+
});
|
|
17
|
+
setters.add(`${key} setter`, () => {
|
|
18
|
+
rootAddon[key] = 5;
|
|
19
|
+
})
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
getters
|
|
23
|
+
.on('cycle', (event) => console.log(String(event.target)))
|
|
24
|
+
.run();
|
|
25
|
+
|
|
26
|
+
setters
|
|
27
|
+
.on('cycle', (event) => console.log(String(event.target)))
|
|
28
|
+
.run();
|
|
29
|
+
});
|
package/common.gypi
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
'variables': {
|
|
3
|
+
'NAPI_VERSION%': "<!(node -p \"process.versions.napi\")",
|
|
4
|
+
'disable_deprecated': "<!(node -p \"process.env['npm_config_disable_deprecated']\")"
|
|
5
|
+
},
|
|
6
|
+
'conditions': [
|
|
7
|
+
['NAPI_VERSION!=""', { 'defines': ['NAPI_VERSION=<@(NAPI_VERSION)'] } ],
|
|
8
|
+
['disable_deprecated=="true"', {
|
|
9
|
+
'defines': ['NODE_ADDON_API_DISABLE_DEPRECATED']
|
|
10
|
+
}],
|
|
11
|
+
['OS=="mac"', {
|
|
12
|
+
'cflags+': ['-fvisibility=hidden'],
|
|
13
|
+
'xcode_settings': {
|
|
14
|
+
'OTHER_CFLAGS': ['-fvisibility=hidden']
|
|
15
|
+
}
|
|
16
|
+
}]
|
|
17
|
+
],
|
|
18
|
+
'include_dirs': ["<!@(node -p \"require('../').include\")"],
|
|
19
|
+
'cflags': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ],
|
|
20
|
+
'cflags_cc': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ]
|
|
21
|
+
}
|
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
|
@@ -8,6 +8,7 @@ A JavaScript BigInt value.
|
|
|
8
8
|
|
|
9
9
|
```cpp
|
|
10
10
|
static Napi::BigInt Napi::BigInt::New(Napi::Env env, int64_t value);
|
|
11
|
+
static Napi::BigInt Napi::BigInt::New(Napi::Env env, uint64_t value);
|
|
11
12
|
```
|
|
12
13
|
|
|
13
14
|
- `[in] env`: The environment in which to construct the `Napi::BigInt` object.
|
|
@@ -47,7 +48,7 @@ Returns a new empty JavaScript `Napi::BigInt`.
|
|
|
47
48
|
### Int64Value
|
|
48
49
|
|
|
49
50
|
```cpp
|
|
50
|
-
int64_t Napi::
|
|
51
|
+
int64_t Napi::BigInt::Int64Value(bool* lossless) const;
|
|
51
52
|
```
|
|
52
53
|
|
|
53
54
|
- `[out] lossless`: Indicates whether the `BigInt` value was converted losslessly.
|
|
@@ -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/env.md
CHANGED
|
@@ -61,3 +61,17 @@ 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 &`
|
package/doc/function.md
CHANGED
|
@@ -11,6 +11,10 @@ functions that were created in JavaScript and passed to the native add-on.
|
|
|
11
11
|
The `Napi::Function` class inherits its behavior from the `Napi::Object` class (for more info
|
|
12
12
|
see: [`Napi::Object`](object.md)).
|
|
13
13
|
|
|
14
|
+
> For callbacks that will be called with asynchronous events from a
|
|
15
|
+
> non-JavaScript thread, please refer to [`Napi::ThreadSafeFunction`][] for more
|
|
16
|
+
> examples.
|
|
17
|
+
|
|
14
18
|
## Example
|
|
15
19
|
|
|
16
20
|
```cpp
|
|
@@ -25,7 +29,7 @@ Value Fn(const CallbackInfo& info) {
|
|
|
25
29
|
}
|
|
26
30
|
|
|
27
31
|
Object Init(Env env, Object exports) {
|
|
28
|
-
exports.Set(String::New(env, "fn"), Function::New(env
|
|
32
|
+
exports.Set(String::New(env, "fn"), Function::New<Fn>(env));
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
|
|
@@ -47,6 +51,27 @@ and in general in situations which don't have an existing JavaScript function on
|
|
|
47
51
|
the stack. The `Call` method is used when there is already a JavaScript function
|
|
48
52
|
on the stack (for example when running a native method called from JavaScript).
|
|
49
53
|
|
|
54
|
+
## Type definitions
|
|
55
|
+
|
|
56
|
+
### Napi::Function::VoidCallback
|
|
57
|
+
|
|
58
|
+
This is the type describing a callback returning `void` that will be invoked
|
|
59
|
+
from JavaScript.
|
|
60
|
+
|
|
61
|
+
```cpp
|
|
62
|
+
typedef void (*VoidCallback)(const Napi::CallbackInfo& info);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Napi::Function::Callback
|
|
66
|
+
|
|
67
|
+
This is the type describing a callback returning a value that will be invoked
|
|
68
|
+
from JavaScript.
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
```cpp
|
|
72
|
+
typedef Value (*Callback)(const Napi::CallbackInfo& info);
|
|
73
|
+
```
|
|
74
|
+
|
|
50
75
|
## Methods
|
|
51
76
|
|
|
52
77
|
### Constructor
|
|
@@ -74,6 +99,86 @@ Returns a non-empty `Napi::Function` instance.
|
|
|
74
99
|
|
|
75
100
|
Creates an instance of a `Napi::Function` object.
|
|
76
101
|
|
|
102
|
+
```cpp
|
|
103
|
+
template <Napi::VoidCallback cb>
|
|
104
|
+
static Napi::Function New(napi_env env,
|
|
105
|
+
const char* utf8name = nullptr,
|
|
106
|
+
void* data = nullptr);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
- `[template] cb`: The native function to invoke when the JavaScript function is
|
|
110
|
+
invoked.
|
|
111
|
+
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Function` object.
|
|
112
|
+
- `[in] utf8name`: Null-terminated string to be used as the name of the function.
|
|
113
|
+
- `[in] data`: User-provided data context. This will be passed back into the
|
|
114
|
+
function when invoked later.
|
|
115
|
+
|
|
116
|
+
Returns an instance of a `Napi::Function` object.
|
|
117
|
+
|
|
118
|
+
### New
|
|
119
|
+
|
|
120
|
+
Creates an instance of a `Napi::Function` object.
|
|
121
|
+
|
|
122
|
+
```cpp
|
|
123
|
+
template <Napi::Callback cb>
|
|
124
|
+
static Napi::Function New(napi_env env,
|
|
125
|
+
const char* utf8name = nullptr,
|
|
126
|
+
void* data = nullptr);
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
- `[template] cb`: The native function to invoke when the JavaScript function is
|
|
130
|
+
invoked.
|
|
131
|
+
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Function` object.
|
|
132
|
+
- `[in] utf8name`: Null-terminated string to be used as the name of the function.
|
|
133
|
+
- `[in] data`: User-provided data context. This will be passed back into the
|
|
134
|
+
function when invoked later.
|
|
135
|
+
|
|
136
|
+
Returns an instance of a `Napi::Function` object.
|
|
137
|
+
|
|
138
|
+
### New
|
|
139
|
+
|
|
140
|
+
Creates an instance of a `Napi::Function` object.
|
|
141
|
+
|
|
142
|
+
```cpp
|
|
143
|
+
template <Napi::VoidCallback cb>
|
|
144
|
+
static Napi::Function New(napi_env env,
|
|
145
|
+
const std::string& utf8name,
|
|
146
|
+
void* data = nullptr);
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
- `[template] cb`: The native function to invoke when the JavaScript function is
|
|
150
|
+
invoked.
|
|
151
|
+
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Function` object.
|
|
152
|
+
- `[in] utf8name`: String to be used as the name of the function.
|
|
153
|
+
- `[in] data`: User-provided data context. This will be passed back into the
|
|
154
|
+
function when invoked later.
|
|
155
|
+
|
|
156
|
+
Returns an instance of a `Napi::Function` object.
|
|
157
|
+
|
|
158
|
+
### New
|
|
159
|
+
|
|
160
|
+
Creates an instance of a `Napi::Function` object.
|
|
161
|
+
|
|
162
|
+
```cpp
|
|
163
|
+
template <Napi::Callback cb>
|
|
164
|
+
static Napi::Function New(napi_env env,
|
|
165
|
+
const std::string& utf8name,
|
|
166
|
+
void* data = nullptr);
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
- `[template] cb`: The native function to invoke when the JavaScript function is
|
|
170
|
+
invoked.
|
|
171
|
+
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Function` object.
|
|
172
|
+
- `[in] utf8name`: String to be used as the name of the function.
|
|
173
|
+
- `[in] data`: User-provided data context. This will be passed back into the
|
|
174
|
+
function when invoked later.
|
|
175
|
+
|
|
176
|
+
Returns an instance of a `Napi::Function` object.
|
|
177
|
+
|
|
178
|
+
### New
|
|
179
|
+
|
|
180
|
+
Creates an instance of a `Napi::Function` object.
|
|
181
|
+
|
|
77
182
|
```cpp
|
|
78
183
|
template <typename Callable>
|
|
79
184
|
static Napi::Function Napi::Function::New(napi_env env, Callable cb, const char* utf8name = nullptr, void* data = nullptr);
|
|
@@ -292,3 +397,5 @@ Napi::Value Napi::Function::operator ()(const std::initializer_list<napi_value>&
|
|
|
292
397
|
- `[in] args`: Initializer list of JavaScript values as `napi_value`.
|
|
293
398
|
|
|
294
399
|
Returns a `Napi::Value` representing the JavaScript value returned by the function.
|
|
400
|
+
|
|
401
|
+
[`Napi::ThreadSafeFunction`]: ./threadsafe_function.md
|
package/doc/object.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
The `Napi::Object` class corresponds to a JavaScript object. It is extended by the following node-addon-api classes that you may use when working with more specific types:
|
|
4
4
|
|
|
5
|
-
- [`Napi::Value`](value.md)
|
|
5
|
+
- [`Napi::Value`](value.md) which is extended by [`Napi::Array`](basic_types.md#array)
|
|
6
6
|
- [`Napi::ArrayBuffer`](array_buffer.md)
|
|
7
7
|
- [`Napi::Buffer<T>`](buffer.md)
|
|
8
8
|
- [`Napi::Function`](function.md)
|
|
@@ -101,6 +101,22 @@ While the value must be any of the following types:
|
|
|
101
101
|
- `bool`
|
|
102
102
|
- `double`
|
|
103
103
|
|
|
104
|
+
### Delete()
|
|
105
|
+
|
|
106
|
+
```cpp
|
|
107
|
+
bool Napi::Object::Delete(____ key);
|
|
108
|
+
```
|
|
109
|
+
- `[in] key`: The name of the property to delete.
|
|
110
|
+
|
|
111
|
+
Deletes the property associated with the given key. Returns `true` if the property was deleted.
|
|
112
|
+
|
|
113
|
+
The `key` can be any of the following types:
|
|
114
|
+
- `napi_value`
|
|
115
|
+
- [`Napi::Value`](value.md)
|
|
116
|
+
- `const char *`
|
|
117
|
+
- `const std::string &`
|
|
118
|
+
- `uint32_t`
|
|
119
|
+
|
|
104
120
|
### Get()
|
|
105
121
|
|
|
106
122
|
```cpp
|
|
@@ -171,6 +187,29 @@ void finalizeCallback(Napi::Env env, T* data, Hint* hint);
|
|
|
171
187
|
```
|
|
172
188
|
where `data` and `hint` are the pointers that were passed into the call to `AddFinalizer()`.
|
|
173
189
|
|
|
190
|
+
### GetPropertyNames()
|
|
191
|
+
```cpp
|
|
192
|
+
Napi::Array Napi::Object::GetPropertyNames() const;
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Returns the names of the enumerable properties of the object as a [`Napi::Array`](basic_types.md#array) of strings.
|
|
196
|
+
The properties whose key is a `Symbol` will not be included.
|
|
197
|
+
|
|
198
|
+
### HasOwnProperty()
|
|
199
|
+
```cpp
|
|
200
|
+
bool Napi::Object::HasOwnProperty(____ key); const
|
|
201
|
+
```
|
|
202
|
+
- `[in] key` The name of the property to check.
|
|
203
|
+
|
|
204
|
+
Returns a `bool` that is *true* if the object has an own property named `key` and *false* otherwise.
|
|
205
|
+
|
|
206
|
+
The key can be any of the following types:
|
|
207
|
+
- `napi_value`
|
|
208
|
+
- [`Napi::Value`](value.md)
|
|
209
|
+
- `const char*`
|
|
210
|
+
- `const std::string&`
|
|
211
|
+
- `uint32_t`
|
|
212
|
+
|
|
174
213
|
### DefineProperty()
|
|
175
214
|
|
|
176
215
|
```cpp
|
|
@@ -69,7 +69,7 @@ for (int i = 0; i < LOOP_MAX; i++) {
|
|
|
69
69
|
Napi::HandleScope scope(info.Env());
|
|
70
70
|
std::string name = std::string("inner-scope") + std::to_string(i);
|
|
71
71
|
Napi::Value newValue = Napi::String::New(info.Env(), name.c_str());
|
|
72
|
-
// do something with
|
|
72
|
+
// do something with newValue
|
|
73
73
|
};
|
|
74
74
|
```
|
|
75
75
|
|