node-addon-api 3.0.1 → 3.2.1
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/CHANGELOG.md +217 -0
- package/README.md +104 -60
- package/common.gypi +1 -1
- package/index.js +4 -3
- package/napi-inl.deprecated.h +8 -8
- package/napi-inl.h +1206 -540
- package/napi.h +821 -522
- package/package-support.json +21 -0
- package/package.json +80 -4
- package/tools/README.md +8 -2
- package/tools/clang-format.js +67 -0
- package/tools/conversion.js +4 -4
- package/.editorconfig +0 -8
- package/.travis.yml +0 -59
- package/CODE_OF_CONDUCT.md +0 -4
- package/CONTRIBUTING.md +0 -66
- package/appveyor.yml +0 -48
- package/benchmark/README.md +0 -47
- package/benchmark/binding.gyp +0 -25
- package/benchmark/function_args.cc +0 -153
- package/benchmark/function_args.js +0 -52
- package/benchmark/index.js +0 -34
- package/benchmark/property_descriptor.cc +0 -60
- package/benchmark/property_descriptor.js +0 -29
- package/doc/Doxyfile +0 -2450
- package/doc/array_buffer.md +0 -129
- package/doc/async_context.md +0 -86
- package/doc/async_operations.md +0 -31
- package/doc/async_worker.md +0 -427
- package/doc/async_worker_variants.md +0 -456
- package/doc/basic_types.md +0 -423
- package/doc/bigint.md +0 -93
- package/doc/boolean.md +0 -64
- package/doc/buffer.md +0 -140
- package/doc/callback_scope.md +0 -54
- package/doc/callbackinfo.md +0 -97
- package/doc/checker-tool.md +0 -32
- package/doc/class_property_descriptor.md +0 -117
- package/doc/cmake-js.md +0 -68
- package/doc/conversion-tool.md +0 -28
- package/doc/creating_a_release.md +0 -62
- package/doc/dataview.md +0 -244
- package/doc/date.md +0 -68
- package/doc/env.md +0 -132
- package/doc/error.md +0 -115
- package/doc/error_handling.md +0 -186
- package/doc/escapable_handle_scope.md +0 -82
- package/doc/external.md +0 -59
- package/doc/function.md +0 -401
- package/doc/function_reference.md +0 -238
- package/doc/generator.md +0 -13
- package/doc/handle_scope.md +0 -65
- package/doc/memory_management.md +0 -27
- package/doc/node-gyp.md +0 -82
- package/doc/number.md +0 -163
- package/doc/object.md +0 -275
- package/doc/object_lifetime_management.md +0 -83
- package/doc/object_reference.md +0 -117
- package/doc/object_wrap.md +0 -851
- package/doc/prebuild_tools.md +0 -16
- package/doc/promises.md +0 -74
- package/doc/property_descriptor.md +0 -286
- package/doc/range_error.md +0 -59
- package/doc/reference.md +0 -111
- package/doc/setup.md +0 -81
- package/doc/string.md +0 -89
- package/doc/symbol.md +0 -44
- package/doc/threadsafe_function.md +0 -320
- package/doc/type_error.md +0 -59
- package/doc/typed_array.md +0 -74
- package/doc/typed_array_of.md +0 -133
- package/doc/value.md +0 -278
- package/doc/version_management.md +0 -43
- package/doc/working_with_javascript_values.md +0 -14
package/doc/handle_scope.md
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
# HandleScope
|
|
2
|
-
|
|
3
|
-
The HandleScope class is used to manage the lifetime of object handles
|
|
4
|
-
which are created through the use of node-addon-api. These handles
|
|
5
|
-
keep an object alive in the heap in order to ensure that the objects
|
|
6
|
-
are not collected while native code is using them.
|
|
7
|
-
A handle may be created when any new node-addon-api Value or one
|
|
8
|
-
of its subclasses is created or returned. For more details refer to
|
|
9
|
-
the section titled [Object lifetime management](object_lifetime_management.md).
|
|
10
|
-
|
|
11
|
-
## Methods
|
|
12
|
-
|
|
13
|
-
### Constructor
|
|
14
|
-
|
|
15
|
-
Creates a new handle scope on the stack.
|
|
16
|
-
|
|
17
|
-
```cpp
|
|
18
|
-
Napi::HandleScope::HandleScope(Napi::Env env);
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
- `[in] env`: The environment in which to construct the `Napi::HandleScope` object.
|
|
22
|
-
|
|
23
|
-
Returns a new `Napi::HandleScope`
|
|
24
|
-
|
|
25
|
-
### Constructor
|
|
26
|
-
|
|
27
|
-
Creates a new handle scope on the stack.
|
|
28
|
-
|
|
29
|
-
```cpp
|
|
30
|
-
Napi::HandleScope::HandleScope(Napi::Env env, Napi::HandleScope scope);
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
- `[in] env`: `Napi::Env` in which the scope passed in was created.
|
|
34
|
-
- `[in] scope`: pre-existing `Napi::HandleScope`.
|
|
35
|
-
|
|
36
|
-
Returns a new `Napi::HandleScope` instance which wraps the napi_handle_scope
|
|
37
|
-
handle passed in. This can be used to mix usage of the C N-API
|
|
38
|
-
and node-addon-api.
|
|
39
|
-
|
|
40
|
-
operator HandleScope::napi_handle_scope
|
|
41
|
-
|
|
42
|
-
```cpp
|
|
43
|
-
operator Napi::HandleScope::napi_handle_scope() const
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
Returns the N-API napi_handle_scope wrapped by the `Napi::EscapableHandleScope` object.
|
|
47
|
-
This can be used to mix usage of the C N-API and node-addon-api by allowing
|
|
48
|
-
the class to be used be converted to a napi_handle_scope.
|
|
49
|
-
|
|
50
|
-
### Destructor
|
|
51
|
-
```cpp
|
|
52
|
-
Napi::HandleScope::~HandleScope();
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
Deletes the `Napi::HandleScope` instance and allows any objects/handles created
|
|
56
|
-
in the scope to be collected by the garbage collector. There is no
|
|
57
|
-
guarantee as to when the gargbage collector will do this.
|
|
58
|
-
|
|
59
|
-
### Env
|
|
60
|
-
|
|
61
|
-
```cpp
|
|
62
|
-
Napi::Env Napi::HandleScope::Env() const;
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
Returns the `Napi::Env` associated with the `Napi::HandleScope`.
|
package/doc/memory_management.md
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
# MemoryManagement
|
|
2
|
-
|
|
3
|
-
The `Napi::MemoryManagement` class contains functions that give the JavaScript engine
|
|
4
|
-
an indication of the amount of externally allocated memory that is kept alive by
|
|
5
|
-
JavaScript objects.
|
|
6
|
-
|
|
7
|
-
## Methods
|
|
8
|
-
|
|
9
|
-
### AdjustExternalMemory
|
|
10
|
-
|
|
11
|
-
The function `AdjustExternalMemory` adjusts the amount of registered external
|
|
12
|
-
memory used to give the JavaScript engine an indication of the amount of externally
|
|
13
|
-
allocated memory that is kept alive by JavaScript objects.
|
|
14
|
-
The JavaScript engine uses this to decide when to perform global garbage collections.
|
|
15
|
-
Registering externally allocated memory will trigger global garbage collections
|
|
16
|
-
more often than it would otherwise in an attempt to garbage collect the JavaScript
|
|
17
|
-
objects that keep the externally allocated memory alive.
|
|
18
|
-
|
|
19
|
-
```cpp
|
|
20
|
-
static int64_t Napi::MemoryManagement::AdjustExternalMemory(Napi::Env env, int64_t change_in_bytes);
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
- `[in] env`: The environment in which the API is invoked under.
|
|
24
|
-
- `[in] change_in_bytes`: The change in externally allocated memory that is kept
|
|
25
|
-
alive by JavaScript objects expressed in bytes.
|
|
26
|
-
|
|
27
|
-
Returns the adjusted memory value.
|
package/doc/node-gyp.md
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
# node-gyp
|
|
2
|
-
|
|
3
|
-
C++ code needs to be compiled into executable form whether it be as an object
|
|
4
|
-
file to linked with others, a shared library, or a standalone executable.
|
|
5
|
-
|
|
6
|
-
The main reason for this is that we need to link to the Node.js dependencies and
|
|
7
|
-
headers correctly, another reason is that we need a cross platform way to build
|
|
8
|
-
C++ source into binary for the target platform.
|
|
9
|
-
|
|
10
|
-
Until now **node-gyp** is the **de-facto** standard build tool for writing
|
|
11
|
-
Node.js addons. It's based on Google's **gyp** build tool, which abstract away
|
|
12
|
-
many of the tedious issues related to cross platform building.
|
|
13
|
-
|
|
14
|
-
**node-gyp** uses a file called ```binding.gyp``` that is located on the root of
|
|
15
|
-
your addon project.
|
|
16
|
-
|
|
17
|
-
```binding.gyp``` file, contains all building configurations organized with a
|
|
18
|
-
JSON like syntax. The most important parameter is the **target** that must be
|
|
19
|
-
set to the same value used on the initialization code of the addon as in the
|
|
20
|
-
examples reported below:
|
|
21
|
-
|
|
22
|
-
### **binding.gyp**
|
|
23
|
-
|
|
24
|
-
```gyp
|
|
25
|
-
{
|
|
26
|
-
"targets": [
|
|
27
|
-
{
|
|
28
|
-
# myModule is the name of your native addon
|
|
29
|
-
"target_name": "myModule",
|
|
30
|
-
"sources": ["src/my_module.cc", ...],
|
|
31
|
-
...
|
|
32
|
-
]
|
|
33
|
-
}
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
### **my_module.cc**
|
|
37
|
-
|
|
38
|
-
```cpp
|
|
39
|
-
#include <napi.h>
|
|
40
|
-
|
|
41
|
-
// ...
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* This code is our entry-point. We receive two arguments here, the first is the
|
|
45
|
-
* environment that represent an independent instance of the JavaScript runtime,
|
|
46
|
-
* the second is exports, the same as module.exports in a .js file.
|
|
47
|
-
* You can either add properties to the exports object passed in or create your
|
|
48
|
-
* own exports object. In either case you must return the object to be used as
|
|
49
|
-
* the exports for the module when you return from the Init function.
|
|
50
|
-
*/
|
|
51
|
-
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
52
|
-
|
|
53
|
-
// ...
|
|
54
|
-
|
|
55
|
-
return exports;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* This code defines the entry-point for the Node addon, it tells Node where to go
|
|
60
|
-
* once the library has been loaded into active memory. The first argument must
|
|
61
|
-
* match the "target" in our *binding.gyp*. Using NODE_GYP_MODULE_NAME ensures
|
|
62
|
-
* that the argument will be correct, as long as the module is built with
|
|
63
|
-
* node-gyp (which is the usual way of building modules). The second argument
|
|
64
|
-
* points to the function to invoke. The function must not be namespaced.
|
|
65
|
-
*/
|
|
66
|
-
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
## **node-gyp** reference
|
|
70
|
-
|
|
71
|
-
- [Installation](https://www.npmjs.com/package/node-gyp#installation)
|
|
72
|
-
- [How to use](https://www.npmjs.com/package/node-gyp#how-to-use)
|
|
73
|
-
- [The binding.gyp file](https://www.npmjs.com/package/node-gyp#the-bindinggyp-file)
|
|
74
|
-
- [Commands](https://www.npmjs.com/package/node-gyp#commands)
|
|
75
|
-
- [Command options](https://www.npmjs.com/package/node-gyp#command-options)
|
|
76
|
-
- [Configuration](https://www.npmjs.com/package/node-gyp#configuration)
|
|
77
|
-
|
|
78
|
-
Sometimes finding the right settings for ```binding.gyp``` is not easy so to
|
|
79
|
-
accomplish at most complicated task please refer to:
|
|
80
|
-
|
|
81
|
-
- [GYP documentation](https://gyp.gsrc.io/index.md)
|
|
82
|
-
- [node-gyp wiki](https://github.com/nodejs/node-gyp/wiki)
|
package/doc/number.md
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
# Number
|
|
2
|
-
|
|
3
|
-
`Napi::Number` class is a representation of the JavaScript `Number` object. The
|
|
4
|
-
`Napi::Number` class inherits its behavior from `Napi::Value` class
|
|
5
|
-
(for more info see [`Napi::Value`](value.md))
|
|
6
|
-
|
|
7
|
-
## Methods
|
|
8
|
-
|
|
9
|
-
### Constructor
|
|
10
|
-
|
|
11
|
-
Creates a new _empty_ instance of a `Napi::Number` object.
|
|
12
|
-
|
|
13
|
-
```cpp
|
|
14
|
-
Napi::Number();
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
Returns a new _empty_ `Napi::Number` object.
|
|
18
|
-
|
|
19
|
-
### Contructor
|
|
20
|
-
|
|
21
|
-
Creates a new instance of a `Napi::Number` object.
|
|
22
|
-
|
|
23
|
-
```cpp
|
|
24
|
-
Napi::Number(napi_env env, napi_value value);
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Number` object.
|
|
28
|
-
- `[in] value`: The JavaScript value holding a number.
|
|
29
|
-
|
|
30
|
-
Returns a non-empty `Napi::Number` object.
|
|
31
|
-
|
|
32
|
-
### New
|
|
33
|
-
|
|
34
|
-
Creates a new instance of a `Napi::Number` object.
|
|
35
|
-
|
|
36
|
-
```cpp
|
|
37
|
-
Napi::Number Napi::Number::New(napi_env env, double value);
|
|
38
|
-
```
|
|
39
|
-
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Number` object.
|
|
40
|
-
- `[in] value`: The C++ primitive from which to instantiate the `Napi::Number`.
|
|
41
|
-
|
|
42
|
-
Creates a new instance of a `Napi::Number` object.
|
|
43
|
-
|
|
44
|
-
### Int32Value
|
|
45
|
-
|
|
46
|
-
Converts a `Napi::Number` value to a `int32_t` primitive type.
|
|
47
|
-
|
|
48
|
-
```cpp
|
|
49
|
-
Napi::Number::Int32Value() const;
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
Returns the `int32_t` primitive type of the corresponding `Napi::Number` object.
|
|
53
|
-
|
|
54
|
-
### Uint32Value
|
|
55
|
-
|
|
56
|
-
Converts a `Napi::Number` value to a `uint32_t` primitive type.
|
|
57
|
-
|
|
58
|
-
```cpp
|
|
59
|
-
Napi::Number::Uint32Value() const;
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
Returns the `uint32_t` primitive type of the corresponding `Napi::Number` object.
|
|
63
|
-
|
|
64
|
-
### Int64Value
|
|
65
|
-
|
|
66
|
-
Converts a `Napi::Number` value to a `int64_t` primitive type.
|
|
67
|
-
|
|
68
|
-
```cpp
|
|
69
|
-
Napi::Number::Int64Value() const;
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
Returns the `int64_t` primitive type of the corresponding `Napi::Number` object.
|
|
73
|
-
|
|
74
|
-
### FloatValue
|
|
75
|
-
|
|
76
|
-
Converts a `Napi::Number` value to a `float` primitive type.
|
|
77
|
-
|
|
78
|
-
```cpp
|
|
79
|
-
Napi::Number::FloatValue() const;
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
Returns the `float` primitive type of the corresponding `Napi::Number` object.
|
|
83
|
-
|
|
84
|
-
### DoubleValue
|
|
85
|
-
|
|
86
|
-
Converts a `Napi::Number` value to a `double` primitive type.
|
|
87
|
-
|
|
88
|
-
```cpp
|
|
89
|
-
Napi::Number::DoubleValue() const;
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
Returns the `double` primitive type of the corresponding `Napi::Number` object.
|
|
93
|
-
|
|
94
|
-
## Operators
|
|
95
|
-
|
|
96
|
-
The `Napi::Number` class contains a set of operators to easily cast JavaScript
|
|
97
|
-
`Number` object to one of the following primitive types:
|
|
98
|
-
|
|
99
|
-
- `int32_t`
|
|
100
|
-
- `uint32_t`
|
|
101
|
-
- `int64_t`
|
|
102
|
-
- `float`
|
|
103
|
-
- `double`
|
|
104
|
-
|
|
105
|
-
### operator int32_t
|
|
106
|
-
|
|
107
|
-
Converts a `Napi::Number` value to a `int32_t` primitive.
|
|
108
|
-
|
|
109
|
-
```cpp
|
|
110
|
-
Napi::Number::operator int32_t() const;
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
Returns the `int32_t` primitive type of the corresponding `Napi::Number` object.
|
|
114
|
-
|
|
115
|
-
### operator uint32_t
|
|
116
|
-
|
|
117
|
-
Converts a `Napi::Number` value to a `uint32_t` primitive type.
|
|
118
|
-
|
|
119
|
-
```cpp
|
|
120
|
-
Napi::Number::operator uint32_t() const;
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
Returns the `uint32_t` primitive type of the corresponding `Napi::Number` object.
|
|
124
|
-
|
|
125
|
-
### operator int64_t
|
|
126
|
-
|
|
127
|
-
Converts a `Napi::Number` value to a `int64_t` primitive type.
|
|
128
|
-
|
|
129
|
-
```cpp
|
|
130
|
-
Napi::Number::operator int64_t() const;
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
Returns the `int64_t` primitive type of the corresponding `Napi::Number` object.
|
|
134
|
-
|
|
135
|
-
### operator float
|
|
136
|
-
|
|
137
|
-
Converts a `Napi::Number` value to a `float` primitive type.
|
|
138
|
-
|
|
139
|
-
```cpp
|
|
140
|
-
Napi::Number::operator float() const;
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
Returns the `float` primitive type of the corresponding `Napi::Number` object.
|
|
144
|
-
|
|
145
|
-
### operator double
|
|
146
|
-
|
|
147
|
-
Converts a `Napi::Number` value to a `double` primitive type.
|
|
148
|
-
|
|
149
|
-
```cpp
|
|
150
|
-
Napi::Number::operator double() const;
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
Returns the `double` primitive type of the corresponding `Napi::Number` object.
|
|
154
|
-
|
|
155
|
-
### Example
|
|
156
|
-
|
|
157
|
-
The following shows an example of casting a number to an `uint32_t` value.
|
|
158
|
-
|
|
159
|
-
```cpp
|
|
160
|
-
uint32_t operatorVal = Napi::Number::New(Env(), 10.0); // Number to unsigned 32 bit integer
|
|
161
|
-
// or
|
|
162
|
-
auto instanceVal = info[0].As<Napi::Number>().Uint32Value();
|
|
163
|
-
```
|
package/doc/object.md
DELETED
|
@@ -1,275 +0,0 @@
|
|
|
1
|
-
# Object
|
|
2
|
-
|
|
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
|
-
|
|
5
|
-
- [`Napi::Value`](value.md) which is extended by [`Napi::Array`](basic_types.md#array)
|
|
6
|
-
- [`Napi::ArrayBuffer`](array_buffer.md)
|
|
7
|
-
- [`Napi::Buffer<T>`](buffer.md)
|
|
8
|
-
- [`Napi::Function`](function.md)
|
|
9
|
-
- [`Napi::TypedArray`](typed_array.md).
|
|
10
|
-
|
|
11
|
-
This class provides a number of convenience methods, most of which are used to set or get properties on a JavaScript object. For example, Set() and Get().
|
|
12
|
-
|
|
13
|
-
## Example
|
|
14
|
-
```cpp
|
|
15
|
-
#include <napi.h>
|
|
16
|
-
|
|
17
|
-
using namespace Napi;
|
|
18
|
-
|
|
19
|
-
Void Init(Env env) {
|
|
20
|
-
|
|
21
|
-
// Create a new instance
|
|
22
|
-
Object obj = Object::New(env);
|
|
23
|
-
|
|
24
|
-
// Assign values to properties
|
|
25
|
-
obj.Set("hello", "world");
|
|
26
|
-
obj.Set(uint32_t(42), "The Answer to Life, the Universe, and Everything");
|
|
27
|
-
obj.Set("Douglas Adams", true);
|
|
28
|
-
|
|
29
|
-
// Get properties
|
|
30
|
-
Value val1 = obj.Get("hello");
|
|
31
|
-
Value val2 = obj.Get(uint32_t(42));
|
|
32
|
-
Value val3 = obj.Get("Douglas Adams");
|
|
33
|
-
|
|
34
|
-
// Test if objects have properties.
|
|
35
|
-
bool obj1 = obj.Has("hello"); // true
|
|
36
|
-
bool obj2 = obj.Has("world"); // false
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
## Methods
|
|
42
|
-
|
|
43
|
-
### Empty Constructor
|
|
44
|
-
|
|
45
|
-
```cpp
|
|
46
|
-
Napi::Object::Object();
|
|
47
|
-
```
|
|
48
|
-
Creates a new empty Object instance.
|
|
49
|
-
|
|
50
|
-
### Constructor
|
|
51
|
-
|
|
52
|
-
```cpp
|
|
53
|
-
Napi::Object::Object(napi_env env, napi_value value);
|
|
54
|
-
```
|
|
55
|
-
- `[in] env`: The `napi_env` environment in which to construct the Value object.
|
|
56
|
-
|
|
57
|
-
- `[in] value`: The C++ primitive from which to instantiate the Value. `value` may be any of:
|
|
58
|
-
- bool
|
|
59
|
-
- Any integer type
|
|
60
|
-
- Any floating point type
|
|
61
|
-
- const char* (encoded using UTF-8, null-terminated)
|
|
62
|
-
- const char16_t* (encoded using UTF-16-LE, null-terminated)
|
|
63
|
-
- std::string (encoded using UTF-8)
|
|
64
|
-
- std::u16string
|
|
65
|
-
- Napi::Value
|
|
66
|
-
- napi_value
|
|
67
|
-
|
|
68
|
-
Creates a non-empty `Napi::Object` instance.
|
|
69
|
-
|
|
70
|
-
### New()
|
|
71
|
-
|
|
72
|
-
```cpp
|
|
73
|
-
Napi::Object Napi::Object::New(napi_env env);
|
|
74
|
-
```
|
|
75
|
-
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Value` object.
|
|
76
|
-
|
|
77
|
-
Creates a new `Napi::Object` value.
|
|
78
|
-
|
|
79
|
-
### Set()
|
|
80
|
-
|
|
81
|
-
```cpp
|
|
82
|
-
void Napi::Object::Set (____ key, ____ value);
|
|
83
|
-
```
|
|
84
|
-
- `[in] key`: The name for the property being assigned.
|
|
85
|
-
- `[in] value`: The value being assigned to the property.
|
|
86
|
-
|
|
87
|
-
Add a property with the specified key with the specified value to the object.
|
|
88
|
-
|
|
89
|
-
The key can be any of the following types:
|
|
90
|
-
- `napi_value`
|
|
91
|
-
- [`Napi::Value`](value.md)
|
|
92
|
-
- `const char*`
|
|
93
|
-
- `const std::string&`
|
|
94
|
-
- `uint32_t`
|
|
95
|
-
|
|
96
|
-
While the value must be any of the following types:
|
|
97
|
-
- `napi_value`
|
|
98
|
-
- [`Napi::Value`](value.md)
|
|
99
|
-
- `const char*`
|
|
100
|
-
- `std::string&`
|
|
101
|
-
- `bool`
|
|
102
|
-
- `double`
|
|
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
|
-
|
|
120
|
-
### Get()
|
|
121
|
-
|
|
122
|
-
```cpp
|
|
123
|
-
Napi::Value Napi::Object::Get(____ key);
|
|
124
|
-
```
|
|
125
|
-
- `[in] key`: The name of the property to return the value for.
|
|
126
|
-
|
|
127
|
-
Returns the [`Napi::Value`](value.md) associated with the key property. Returns the value *undefined* if the key does not exist.
|
|
128
|
-
|
|
129
|
-
The `key` can be any of the following types:
|
|
130
|
-
- `napi_value`
|
|
131
|
-
- [`Napi::Value`](value.md)
|
|
132
|
-
- `const char *`
|
|
133
|
-
- `const std::string &`
|
|
134
|
-
- `uint32_t`
|
|
135
|
-
|
|
136
|
-
### Has()
|
|
137
|
-
|
|
138
|
-
```cpp
|
|
139
|
-
bool Napi::Object::Has (____ key) const;
|
|
140
|
-
```
|
|
141
|
-
- `[in] key`: The name of the property to check.
|
|
142
|
-
|
|
143
|
-
Returns a `bool` that is *true* if the object has a property named `key` and *false* otherwise.
|
|
144
|
-
|
|
145
|
-
### InstanceOf()
|
|
146
|
-
|
|
147
|
-
```cpp
|
|
148
|
-
bool Napi::Object::InstanceOf (const Function& constructor) const
|
|
149
|
-
```
|
|
150
|
-
- `[in] constructor`: The constructor [`Napi::Function`](function.md) of the value that is being compared with the object.
|
|
151
|
-
|
|
152
|
-
Returns a `bool` that is true if the `Napi::Object` is an instance created by the `constructor` and false otherwise.
|
|
153
|
-
|
|
154
|
-
Note: This is equivalent to the JavaScript instanceof operator.
|
|
155
|
-
|
|
156
|
-
### AddFinalizer()
|
|
157
|
-
```cpp
|
|
158
|
-
template <typename Finalizer, typename T>
|
|
159
|
-
inline void AddFinalizer(Finalizer finalizeCallback, T* data);
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
- `[in] finalizeCallback`: The function to call when the object is garbage-collected.
|
|
163
|
-
- `[in] data`: The data to associate with the object.
|
|
164
|
-
|
|
165
|
-
Associates `data` with the object, calling `finalizeCallback` when the object is garbage-collected. `finalizeCallback`
|
|
166
|
-
has the signature
|
|
167
|
-
```cpp
|
|
168
|
-
void finalizeCallback(Napi::Env env, T* data);
|
|
169
|
-
```
|
|
170
|
-
where `data` is the pointer that was passed into the call to `AddFinalizer()`.
|
|
171
|
-
|
|
172
|
-
### AddFinalizer()
|
|
173
|
-
```cpp
|
|
174
|
-
template <typename Finalizer, typename T, typename Hint>
|
|
175
|
-
inline void AddFinalizer(Finalizer finalizeCallback,
|
|
176
|
-
T* data,
|
|
177
|
-
Hint* finalizeHint);
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
- `[in] data`: The data to associate with the object.
|
|
181
|
-
- `[in] finalizeCallback`: The function to call when the object is garbage-collected.
|
|
182
|
-
|
|
183
|
-
Associates `data` with the object, calling `finalizeCallback` when the object is garbage-collected. An additional hint
|
|
184
|
-
may be given. It will also be passed to `finalizeCallback`, which has the signature
|
|
185
|
-
```cpp
|
|
186
|
-
void finalizeCallback(Napi::Env env, T* data, Hint* hint);
|
|
187
|
-
```
|
|
188
|
-
where `data` and `hint` are the pointers that were passed into the call to `AddFinalizer()`.
|
|
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
|
-
|
|
213
|
-
### DefineProperty()
|
|
214
|
-
|
|
215
|
-
```cpp
|
|
216
|
-
void Napi::Object::DefineProperty (const Napi::PropertyDescriptor& property);
|
|
217
|
-
```
|
|
218
|
-
- `[in] property`: A [`Napi::PropertyDescriptor`](property_descriptor.md).
|
|
219
|
-
|
|
220
|
-
Define a property on the object.
|
|
221
|
-
|
|
222
|
-
### DefineProperties()
|
|
223
|
-
|
|
224
|
-
```cpp
|
|
225
|
-
void Napi::Object::DefineProperties (____ properties)
|
|
226
|
-
```
|
|
227
|
-
- `[in] properties`: A list of [`Napi::PropertyDescriptor`](property_descriptor.md). Can be one of the following types:
|
|
228
|
-
- const std::initializer_list<Napi::PropertyDescriptor>&
|
|
229
|
-
- const std::vector<Napi::PropertyDescriptor>&
|
|
230
|
-
|
|
231
|
-
Defines properties on the object.
|
|
232
|
-
|
|
233
|
-
### Operator[]()
|
|
234
|
-
|
|
235
|
-
```cpp
|
|
236
|
-
Napi::PropertyLValue<std::string> Napi::Object::operator[] (const char* utf8name);
|
|
237
|
-
```
|
|
238
|
-
- `[in] utf8name`: UTF-8 encoded null-terminated property name.
|
|
239
|
-
|
|
240
|
-
Returns a [`Napi::PropertyLValue`](propertylvalue.md) as the named property or sets the named property.
|
|
241
|
-
|
|
242
|
-
```cpp
|
|
243
|
-
Napi::PropertyLValue<std::string> Napi::Object::operator[] (const std::string& utf8name);
|
|
244
|
-
```
|
|
245
|
-
- `[in] utf8name`: UTF-8 encoded property name.
|
|
246
|
-
|
|
247
|
-
Returns a [`Napi::PropertyLValue`](propertylvalue.md) as the named property or sets the named property.
|
|
248
|
-
|
|
249
|
-
```cpp
|
|
250
|
-
Napi::PropertyLValue<uint32_t> Napi::Object::operator[] (uint32_t index);
|
|
251
|
-
```
|
|
252
|
-
- `[in] index`: Element index.
|
|
253
|
-
|
|
254
|
-
Returns a [`Napi::PropertyLValue`](propertylvalue.md) or sets an indexed property or array element.
|
|
255
|
-
|
|
256
|
-
```cpp
|
|
257
|
-
Napi::Value Napi::Object::operator[] (const char* utf8name) const;
|
|
258
|
-
```
|
|
259
|
-
- `[in] utf8name`: UTF-8 encoded null-terminated property name.
|
|
260
|
-
|
|
261
|
-
Returns the named property as a [`Napi::Value`](value.md).
|
|
262
|
-
|
|
263
|
-
```cpp
|
|
264
|
-
Napi::Value Napi::Object::operator[] (const std::string& utf8name) const;
|
|
265
|
-
```
|
|
266
|
-
- `[in] utf8name`: UTF-8 encoded property name.
|
|
267
|
-
|
|
268
|
-
Returns the named property as a [`Napi::Value`](value.md).
|
|
269
|
-
|
|
270
|
-
```cpp
|
|
271
|
-
Napi::Value Napi::Object::operator[] (uint32_t index) const;
|
|
272
|
-
```
|
|
273
|
-
- `[in] index`: Element index.
|
|
274
|
-
|
|
275
|
-
Returns an indexed property or array element as a [`Napi::Value`](value.md).
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
# Object lifetime management
|
|
2
|
-
|
|
3
|
-
A handle may be created when any new node-addon-api Value and
|
|
4
|
-
its subclasses is created or returned.
|
|
5
|
-
|
|
6
|
-
As the methods and classes within the node-addon-api are used,
|
|
7
|
-
handles to objects in the heap for the underlying
|
|
8
|
-
VM may be created. A handle may be created when any new
|
|
9
|
-
node-addon-api Value or one of its subclasses is created or returned.
|
|
10
|
-
These handles must hold the objects 'live' until they are no
|
|
11
|
-
longer required by the native code, otherwise the objects could be
|
|
12
|
-
collected by the garbage collector before the native code was
|
|
13
|
-
finished using them.
|
|
14
|
-
|
|
15
|
-
As handles are created they are associated with a
|
|
16
|
-
'scope'. The lifespan for the default scope is tied to the lifespan
|
|
17
|
-
of the native method call. The result is that, by default, handles
|
|
18
|
-
remain valid and the objects associated with these handles will be
|
|
19
|
-
held live for the lifespan of the native method call.
|
|
20
|
-
|
|
21
|
-
In many cases, however, it is necessary that the handles remain valid for
|
|
22
|
-
either a shorter or longer lifespan than that of the native method.
|
|
23
|
-
The sections which follow describe the node-addon-api classes and
|
|
24
|
-
methods that than can be used to change the handle lifespan from
|
|
25
|
-
the default.
|
|
26
|
-
|
|
27
|
-
## Making handle lifespan shorter than that of the native method
|
|
28
|
-
|
|
29
|
-
It is often necessary to make the lifespan of handles shorter than
|
|
30
|
-
the lifespan of a native method. For example, consider a native method
|
|
31
|
-
that has a loop which creates a number of values and does something
|
|
32
|
-
with each of the values, one at a time:
|
|
33
|
-
|
|
34
|
-
```C++
|
|
35
|
-
for (int i = 0; i < LOOP_MAX; i++) {
|
|
36
|
-
std::string name = std::string("inner-scope") + std::to_string(i);
|
|
37
|
-
Napi::Value newValue = Napi::String::New(info.Env(), name.c_str());
|
|
38
|
-
// do something with newValue
|
|
39
|
-
};
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
This would result in a large number of handles being created, consuming
|
|
43
|
-
substantial resources. In addition, even though the native code could only
|
|
44
|
-
use the most recently created value, all of the previously created
|
|
45
|
-
values would also be kept alive since they all share the same scope.
|
|
46
|
-
|
|
47
|
-
To handle this case, node-addon-api provides the ability to establish
|
|
48
|
-
a new 'scope' to which newly created handles will be associated. Once those
|
|
49
|
-
handles are no longer required, the scope can be deleted and any handles
|
|
50
|
-
associated with the scope are invalidated. The `Napi::HandleScope`
|
|
51
|
-
and `Napi::EscapableHandleScope` classes are provided by node-addon-api for
|
|
52
|
-
creating additional scopes.
|
|
53
|
-
|
|
54
|
-
node-addon-api only supports a single nested hierarchy of scopes. There is
|
|
55
|
-
only one active scope at any time, and all new handles will be associated
|
|
56
|
-
with that scope while it is active. Scopes must be deleted in the reverse
|
|
57
|
-
order from which they are opened. In addition, all scopes created within
|
|
58
|
-
a native method must be deleted before returning from that method. Since
|
|
59
|
-
`Napi::HandleScopes` are typically stack allocated the compiler will take care of
|
|
60
|
-
deletion, however, care must be taken to create the scope in the right
|
|
61
|
-
place such that you achieve the desired lifetime.
|
|
62
|
-
|
|
63
|
-
Taking the earlier example, creating a `Napi::HandleScope` in the innner loop
|
|
64
|
-
would ensure that at most a single new value is held alive throughout the
|
|
65
|
-
execution of the loop:
|
|
66
|
-
|
|
67
|
-
```C
|
|
68
|
-
for (int i = 0; i < LOOP_MAX; i++) {
|
|
69
|
-
Napi::HandleScope scope(info.Env());
|
|
70
|
-
std::string name = std::string("inner-scope") + std::to_string(i);
|
|
71
|
-
Napi::Value newValue = Napi::String::New(info.Env(), name.c_str());
|
|
72
|
-
// do something with newValue
|
|
73
|
-
};
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
When nesting scopes, there are cases where a handle from an
|
|
77
|
-
inner scope needs to live beyond the lifespan of that scope. node-addon-api
|
|
78
|
-
provides the `Napi::EscapableHandleScope` with the `Escape` method
|
|
79
|
-
in order to support this case. An escapable scope
|
|
80
|
-
allows one object to be 'promoted' so that it 'escapes' the
|
|
81
|
-
current scope and the lifespan of the handle changes from the current
|
|
82
|
-
scope to that of the outer scope. The `Escape` method can only be called
|
|
83
|
-
once for a given `Napi::EscapableHandleScope`.
|