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.
- package/.clang-format +111 -0
- package/.github/workflows/ci.yml +55 -0
- package/.github/workflows/linter.yml +24 -0
- package/.github/workflows/stale.yml +18 -0
- package/.travis.yml +1 -5
- package/CHANGELOG.md +237 -23
- package/README.md +101 -31
- 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 +20 -0
- package/doc/async_context.md +1 -1
- package/doc/async_worker.md +34 -5
- package/doc/{async_progress_worker.md → async_worker_variants.md} +236 -23
- package/doc/bigint.md +7 -2
- package/doc/boolean.md +5 -1
- package/doc/buffer.md +4 -0
- package/doc/checker-tool.md +1 -1
- package/doc/class_property_descriptor.md +3 -3
- package/doc/creating_a_release.md +6 -6
- 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/escapable_handle_scope.md +1 -1
- package/doc/external.md +4 -0
- package/doc/function.md +111 -3
- package/doc/function_reference.md +1 -1
- package/doc/handle_scope.md +1 -1
- package/doc/hierarchy.md +91 -0
- package/doc/instance_wrap.md +408 -0
- package/doc/name.md +29 -0
- package/doc/number.md +1 -1
- package/doc/object.md +44 -1
- package/doc/object_lifetime_management.md +2 -2
- package/doc/object_reference.md +1 -1
- package/doc/object_wrap.md +220 -216
- package/doc/prebuild_tools.md +2 -2
- package/doc/promises.md +5 -0
- package/doc/property_descriptor.md +67 -12
- package/doc/setup.md +1 -2
- package/doc/string.md +5 -1
- package/doc/symbol.md +5 -1
- package/doc/threadsafe.md +121 -0
- package/doc/threadsafe_function.md +16 -46
- package/doc/typed_array.md +4 -0
- package/doc/typed_array_of.md +4 -0
- package/doc/typed_threadsafe_function.md +307 -0
- package/doc/value.md +166 -104
- package/doc/version_management.md +2 -2
- package/except.gypi +16 -0
- package/index.js +7 -41
- package/napi-inl.h +1685 -464
- package/napi.h +606 -141
- package/node_api.gyp +9 -0
- package/noexcept.gypi +16 -0
- package/{src/nothing.c → nothing.c} +0 -0
- package/package-support.json +21 -0
- package/package.json +106 -2
- package/tools/README.md +12 -6
- package/tools/clang-format.js +47 -0
- package/tools/conversion.js +4 -8
- package/doc/Doxyfile +0 -2450
- 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
|
@@ -0,0 +1,91 @@
|
|
|
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
|
+
#if NAPI_VERSION > 5
|
|
30
|
+
class PropDescBenchmark : public Napi::Addon<PropDescBenchmark> {
|
|
31
|
+
public:
|
|
32
|
+
PropDescBenchmark(Napi::Env, Napi::Object exports) {
|
|
33
|
+
DefineAddon(exports, {
|
|
34
|
+
InstanceAccessor("addon",
|
|
35
|
+
&PropDescBenchmark::Getter,
|
|
36
|
+
&PropDescBenchmark::Setter,
|
|
37
|
+
napi_enumerable),
|
|
38
|
+
InstanceAccessor<&PropDescBenchmark::Getter,
|
|
39
|
+
&PropDescBenchmark::Setter>("addon_templated",
|
|
40
|
+
napi_enumerable),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private:
|
|
45
|
+
Napi::Value Getter(const Napi::CallbackInfo& info) {
|
|
46
|
+
return Napi::Number::New(info.Env(), 42);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
void Setter(const Napi::CallbackInfo& info, const Napi::Value& val) {
|
|
50
|
+
(void) info[0];
|
|
51
|
+
(void) val;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
#endif // NAPI_VERSION > 5
|
|
55
|
+
|
|
56
|
+
static Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
57
|
+
napi_status status;
|
|
58
|
+
napi_property_descriptor core_prop = {
|
|
59
|
+
"core",
|
|
60
|
+
nullptr,
|
|
61
|
+
nullptr,
|
|
62
|
+
Getter_Core,
|
|
63
|
+
Setter_Core,
|
|
64
|
+
nullptr,
|
|
65
|
+
napi_enumerable,
|
|
66
|
+
nullptr
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
status = napi_define_properties(env, exports, 1, &core_prop);
|
|
70
|
+
NAPI_THROW_IF_FAILED(env, status, Napi::Object());
|
|
71
|
+
|
|
72
|
+
exports.DefineProperty(
|
|
73
|
+
Napi::PropertyDescriptor::Accessor(env,
|
|
74
|
+
exports,
|
|
75
|
+
"cplusplus",
|
|
76
|
+
Getter,
|
|
77
|
+
Setter,
|
|
78
|
+
napi_enumerable));
|
|
79
|
+
|
|
80
|
+
exports.DefineProperty(
|
|
81
|
+
Napi::PropertyDescriptor::Accessor<Getter, Setter>("templated",
|
|
82
|
+
napi_enumerable));
|
|
83
|
+
|
|
84
|
+
#if NAPI_VERSION > 5
|
|
85
|
+
PropDescBenchmark::Init(env, exports);
|
|
86
|
+
#endif // NAPI_VERSION > 5
|
|
87
|
+
|
|
88
|
+
return exports;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
|
|
@@ -0,0 +1,37 @@
|
|
|
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('bindings')({
|
|
8
|
+
bindings: addonName,
|
|
9
|
+
module_root: __dirname
|
|
10
|
+
});
|
|
11
|
+
delete rootAddon.path;
|
|
12
|
+
const getters = new Benchmark.Suite;
|
|
13
|
+
const setters = new Benchmark.Suite;
|
|
14
|
+
const maxNameLength = Object.keys(rootAddon)
|
|
15
|
+
.reduce((soFar, value) => Math.max(soFar, value.length), 0);
|
|
16
|
+
|
|
17
|
+
console.log(`\n${addonName}: `);
|
|
18
|
+
|
|
19
|
+
Object.keys(rootAddon).forEach((key) => {
|
|
20
|
+
getters.add(`${key} getter`.padStart(maxNameLength + 7), () => {
|
|
21
|
+
const x = rootAddon[key];
|
|
22
|
+
});
|
|
23
|
+
setters.add(`${key} setter`.padStart(maxNameLength + 7), () => {
|
|
24
|
+
rootAddon[key] = 5;
|
|
25
|
+
})
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
getters
|
|
29
|
+
.on('cycle', (event) => console.log(String(event.target)))
|
|
30
|
+
.run();
|
|
31
|
+
|
|
32
|
+
console.log('');
|
|
33
|
+
|
|
34
|
+
setters
|
|
35
|
+
.on('cycle', (event) => console.log(String(event.target)))
|
|
36
|
+
.run();
|
|
37
|
+
});
|
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_dir\")"],
|
|
19
|
+
'cflags': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ],
|
|
20
|
+
'cflags_cc': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ]
|
|
21
|
+
}
|
package/doc/addon.md
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# Add-on Structure
|
|
2
|
+
|
|
3
|
+
Class `Napi::Addon<T>` inherits from class [`Napi::InstanceWrap<T>`][].
|
|
4
|
+
|
|
5
|
+
Creating add-ons that work correctly when loaded multiple times from the same
|
|
6
|
+
source package into multiple Node.js threads and/or multiple times into the same
|
|
7
|
+
Node.js thread requires that all global data they hold be associated with the
|
|
8
|
+
environment in which they run. It is not safe to store global data in static
|
|
9
|
+
variables because doing so does not take into account the fact that an add-on
|
|
10
|
+
may be loaded into multiple threads nor that an add-on may be loaded multiple
|
|
11
|
+
times into a single thread.
|
|
12
|
+
|
|
13
|
+
The `Napi::Addon<T>` class can be used to define an entire add-on. Instances of
|
|
14
|
+
`Napi::Addon<T>` subclasses become instances of the add-on, stored safely by
|
|
15
|
+
Node.js on its various threads and into its various contexts. Thus, any data
|
|
16
|
+
stored in the instance variables of a `Napi::Addon<T>` subclass instance are
|
|
17
|
+
stored safely by Node.js. Functions exposed to JavaScript using
|
|
18
|
+
`Napi::Addon<T>::InstanceMethod` and/or `Napi::Addon<T>::DefineAddon` are
|
|
19
|
+
instance methods of the `Napi::Addon` subclass and thus have access to data
|
|
20
|
+
stored inside the instance.
|
|
21
|
+
|
|
22
|
+
`Napi::Addon<T>::DefineProperties` may be used to attach `Napi::Addon<T>`
|
|
23
|
+
subclass instance methods to objects other than the one that will be returned to
|
|
24
|
+
Node.js as the add-on instance.
|
|
25
|
+
|
|
26
|
+
The `Napi::Addon<T>` class can be used together with the `NODE_API_ADDON()` and
|
|
27
|
+
`NODE_API_NAMED_ADDON()` macros to define add-ons.
|
|
28
|
+
|
|
29
|
+
## Example
|
|
30
|
+
|
|
31
|
+
```cpp
|
|
32
|
+
#include <napi.h>
|
|
33
|
+
|
|
34
|
+
class ExampleAddon : public Napi::Addon<ExampleAddon> {
|
|
35
|
+
public:
|
|
36
|
+
ExampleAddon(Napi::Env env, Napi::Object exports) {
|
|
37
|
+
// In the constructor we declare the functions the add-on makes available
|
|
38
|
+
// to JavaScript.
|
|
39
|
+
DefineAddon(exports, {
|
|
40
|
+
InstanceMethod("increment", &ExampleAddon::Increment),
|
|
41
|
+
|
|
42
|
+
// We can also attach plain objects to `exports`, and instance methods as
|
|
43
|
+
// properties of those sub-objects.
|
|
44
|
+
InstanceValue("subObject", DefineProperties(Napi::Object::New(env), {
|
|
45
|
+
InstanceMethod("decrement", &ExampleAddon::Decrement)
|
|
46
|
+
}), napi_enumerable)
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
private:
|
|
50
|
+
|
|
51
|
+
// This method has access to the data stored in the environment because it is
|
|
52
|
+
// an instance method of `ExampleAddon` and because it was listed among the
|
|
53
|
+
// property descriptors passed to `DefineAddon()` in the constructor.
|
|
54
|
+
Napi::Value Increment(const Napi::CallbackInfo& info) {
|
|
55
|
+
return Napi::Number::New(info.Env(), ++value);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// This method has access to the data stored in the environment because it is
|
|
59
|
+
// an instance method of `ExampleAddon` and because it was exposed to
|
|
60
|
+
// JavaScript by calling `DefineProperties()` with the object onto which it is
|
|
61
|
+
// attached.
|
|
62
|
+
Napi::Value Decrement(const Napi::CallbackInfo& info) {
|
|
63
|
+
return Napi::Number::New(info.Env(), --value);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Data stored in these variables is unique to each instance of the add-on.
|
|
67
|
+
uint32_t value = 42;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// The macro announces that instances of the class `ExampleAddon` will be
|
|
71
|
+
// created for each instance of the add-on that must be loaded into Node.js.
|
|
72
|
+
NODE_API_ADDON(ExampleAddon)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The above code can be used from JavaScript as follows:
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
'use strict'
|
|
79
|
+
|
|
80
|
+
const exampleAddon = require('bindings')('example_addon');
|
|
81
|
+
console.log(exampleAddon.increment()); // prints 43
|
|
82
|
+
console.log(exampleAddon.increment()); // prints 44
|
|
83
|
+
console.log(exampleAddon.subObject.decrement()); // prints 43
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
When Node.js loads an instance of the add-on, a new instance of the class is
|
|
87
|
+
created. Its constructor receives the environment `Napi::Env env` and the
|
|
88
|
+
exports object `Napi::Object exports`. It can then use the method `DefineAddon`
|
|
89
|
+
to either attach methods, accessors, and/or values to the `exports` object or to
|
|
90
|
+
create its own `exports` object and attach methods, accessors, and/or values to
|
|
91
|
+
it.
|
|
92
|
+
|
|
93
|
+
Functions created with `Napi::Function::New()`, accessors created with
|
|
94
|
+
`PropertyDescriptor::Accessor()`, and values can also be attached. If their
|
|
95
|
+
implementation requires the `ExampleAddon` instance, it can be retrieved from
|
|
96
|
+
the `Napi::Env env` with `GetInstanceData()`:
|
|
97
|
+
|
|
98
|
+
```cpp
|
|
99
|
+
void ExampleBinding(const Napi::CallbackInfo& info) {
|
|
100
|
+
ExampleAddon* addon = info.Env().GetInstanceData<ExampleAddon>();
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Methods
|
|
105
|
+
|
|
106
|
+
### Constructor
|
|
107
|
+
|
|
108
|
+
Creates a new instance of the add-on.
|
|
109
|
+
|
|
110
|
+
```cpp
|
|
111
|
+
Napi::Addon(Napi::Env env, Napi::Object exports);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
- `[in] env`: The environment into which the add-on is being loaded.
|
|
115
|
+
- `[in] exports`: The exports object received from JavaScript.
|
|
116
|
+
|
|
117
|
+
Typically, the constructor calls `DefineAddon()` to attach methods, accessors,
|
|
118
|
+
and/or values to `exports`. The constructor may also create a new object and
|
|
119
|
+
pass it to `DefineAddon()` as its first parameter if it wishes to replace the
|
|
120
|
+
`exports` object as provided by Node.js.
|
|
121
|
+
|
|
122
|
+
### DefineAddon
|
|
123
|
+
|
|
124
|
+
Defines an add-on instance with functions, accessors, and/or values.
|
|
125
|
+
|
|
126
|
+
```cpp
|
|
127
|
+
template <typename T>
|
|
128
|
+
void Napi::Addon<T>::DefineAddon(Napi::Object exports,
|
|
129
|
+
const std::initializer_list<PropertyDescriptor>& properties);
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
* `[in] exports`: The object to return to Node.js as an instance of the add-on.
|
|
133
|
+
* `[in] properties`: Initializer list of add-on property descriptors of the
|
|
134
|
+
methods, property accessors, and values that define the add-on. They will be
|
|
135
|
+
set on `exports`.
|
|
136
|
+
See: [`Class property and descriptor`](class_property_descriptor.md).
|
|
137
|
+
|
|
138
|
+
### DefineProperties
|
|
139
|
+
|
|
140
|
+
Defines function, accessor, and/or value properties on an object using add-on
|
|
141
|
+
instance methods.
|
|
142
|
+
|
|
143
|
+
```cpp
|
|
144
|
+
template <typename T>
|
|
145
|
+
Napi::Object
|
|
146
|
+
Napi::Addon<T>::DefineProperties(Napi::Object object,
|
|
147
|
+
const std::initializer_list<PropertyDescriptor>& properties);
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
* `[in] object`: The object that will receive the new properties.
|
|
151
|
+
* `[in] properties`: Initializer list of property descriptors of the methods,
|
|
152
|
+
property accessors, and values to attach to `object`.
|
|
153
|
+
See: [`Class property and descriptor`](class_property_descriptor.md).
|
|
154
|
+
|
|
155
|
+
Returns `object`.
|
|
156
|
+
|
|
157
|
+
[`Napi::InstanceWrap<T>`]: ./instance_wrap.md
|
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,21 @@ void* Napi::ArrayBuffer::Data() const;
|
|
|
127
129
|
```
|
|
128
130
|
|
|
129
131
|
Returns a pointer the wrapped data.
|
|
132
|
+
|
|
133
|
+
### Detach
|
|
134
|
+
|
|
135
|
+
```cpp
|
|
136
|
+
void Napi::ArrayBuffer::Detach();
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Invokes the `ArrayBuffer` detach operation on a detachable `ArrayBuffer`.
|
|
140
|
+
|
|
141
|
+
### IsDetached
|
|
142
|
+
|
|
143
|
+
```cpp
|
|
144
|
+
bool Napi::ArrayBuffer::IsDetached() const;
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Returns `true` if this `ArrayBuffer` has been detached.
|
|
148
|
+
|
|
149
|
+
[`Napi::Object`]: ./object.md
|
package/doc/async_context.md
CHANGED
|
@@ -73,7 +73,7 @@ void MakeCallbackWithAsyncContext(const Napi::CallbackInfo& info) {
|
|
|
73
73
|
Napi::Function callback = info[0].As<Napi::Function>();
|
|
74
74
|
Napi::Object resource = info[1].As<Napi::Object>();
|
|
75
75
|
|
|
76
|
-
//
|
|
76
|
+
// Create a new async context instance.
|
|
77
77
|
Napi::AsyncContext context(info.Env(), "async_context_test", resource);
|
|
78
78
|
|
|
79
79
|
// Invoke the callback with the async context instance.
|
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
|
}
|
|
@@ -366,7 +395,7 @@ class EchoWorker : public AsyncWorker {
|
|
|
366
395
|
};
|
|
367
396
|
```
|
|
368
397
|
|
|
369
|
-
The `EchoWorker`'s
|
|
398
|
+
The `EchoWorker`'s constructor calls the base class' constructor to pass in the
|
|
370
399
|
callback that the `Napi::AsyncWorker` base class will store persistently. When
|
|
371
400
|
the work on the `Napi::AsyncWorker::Execute` method is done the
|
|
372
401
|
`Napi::AsyncWorker::OnOk` method is called and the results return back to
|
|
@@ -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.
|