node-addon-api 3.1.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/CHANGELOG.md +84 -0
  2. package/README.md +67 -47
  3. package/napi-inl.deprecated.h +8 -8
  4. package/napi-inl.h +118 -80
  5. package/napi.h +513 -424
  6. package/package.json +25 -5
  7. package/tools/clang-format.js +30 -10
  8. package/.clang-format +0 -111
  9. package/.editorconfig +0 -8
  10. package/.github/workflows/ci.yml +0 -55
  11. package/.github/workflows/linter.yml +0 -24
  12. package/.github/workflows/stale.yml +0 -18
  13. package/.travis.yml +0 -58
  14. package/CODE_OF_CONDUCT.md +0 -4
  15. package/CONTRIBUTING.md +0 -66
  16. package/appveyor.yml +0 -37
  17. package/benchmark/README.md +0 -47
  18. package/benchmark/binding.gyp +0 -25
  19. package/benchmark/function_args.cc +0 -217
  20. package/benchmark/function_args.js +0 -60
  21. package/benchmark/index.js +0 -34
  22. package/benchmark/property_descriptor.cc +0 -91
  23. package/benchmark/property_descriptor.js +0 -37
  24. package/doc/addon.md +0 -157
  25. package/doc/array.md +0 -81
  26. package/doc/array_buffer.md +0 -149
  27. package/doc/async_context.md +0 -86
  28. package/doc/async_operations.md +0 -31
  29. package/doc/async_worker.md +0 -427
  30. package/doc/async_worker_variants.md +0 -557
  31. package/doc/bigint.md +0 -97
  32. package/doc/boolean.md +0 -68
  33. package/doc/buffer.md +0 -144
  34. package/doc/callback_scope.md +0 -54
  35. package/doc/callbackinfo.md +0 -97
  36. package/doc/checker-tool.md +0 -32
  37. package/doc/class_property_descriptor.md +0 -117
  38. package/doc/cmake-js.md +0 -68
  39. package/doc/conversion-tool.md +0 -28
  40. package/doc/creating_a_release.md +0 -62
  41. package/doc/dataview.md +0 -248
  42. package/doc/date.md +0 -68
  43. package/doc/env.md +0 -132
  44. package/doc/error.md +0 -120
  45. package/doc/error_handling.md +0 -186
  46. package/doc/escapable_handle_scope.md +0 -82
  47. package/doc/external.md +0 -63
  48. package/doc/function.md +0 -402
  49. package/doc/function_reference.md +0 -238
  50. package/doc/generator.md +0 -13
  51. package/doc/handle_scope.md +0 -65
  52. package/doc/hierarchy.md +0 -91
  53. package/doc/instance_wrap.md +0 -408
  54. package/doc/memory_management.md +0 -27
  55. package/doc/name.md +0 -29
  56. package/doc/node-gyp.md +0 -82
  57. package/doc/number.md +0 -163
  58. package/doc/object.md +0 -279
  59. package/doc/object_lifetime_management.md +0 -83
  60. package/doc/object_reference.md +0 -117
  61. package/doc/object_wrap.md +0 -561
  62. package/doc/prebuild_tools.md +0 -16
  63. package/doc/promises.md +0 -79
  64. package/doc/property_descriptor.md +0 -286
  65. package/doc/range_error.md +0 -59
  66. package/doc/reference.md +0 -111
  67. package/doc/setup.md +0 -81
  68. package/doc/string.md +0 -93
  69. package/doc/symbol.md +0 -48
  70. package/doc/threadsafe.md +0 -121
  71. package/doc/threadsafe_function.md +0 -290
  72. package/doc/type_error.md +0 -59
  73. package/doc/typed_array.md +0 -78
  74. package/doc/typed_array_of.md +0 -137
  75. package/doc/typed_threadsafe_function.md +0 -307
  76. package/doc/value.md +0 -340
  77. package/doc/version_management.md +0 -43
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
- ### Constructor
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,279 +0,0 @@
1
- # Object
2
-
3
- Class `Napi::Object` inherits from class [`Napi::Value`][].
4
-
5
- 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:
6
-
7
- - [`Napi::Array`](array.md)
8
- - [`Napi::ArrayBuffer`](array_buffer.md)
9
- - [`Napi::Buffer<T>`](buffer.md)
10
- - [`Napi::Function`](function.md)
11
- - [`Napi::TypedArray`](typed_array.md).
12
-
13
- 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().
14
-
15
- ## Example
16
- ```cpp
17
- #include <napi.h>
18
-
19
- using namespace Napi;
20
-
21
- Void Init(Env env) {
22
-
23
- // Create a new instance
24
- Object obj = Object::New(env);
25
-
26
- // Assign values to properties
27
- obj.Set("hello", "world");
28
- obj.Set(uint32_t(42), "The Answer to Life, the Universe, and Everything");
29
- obj.Set("Douglas Adams", true);
30
-
31
- // Get properties
32
- Value val1 = obj.Get("hello");
33
- Value val2 = obj.Get(uint32_t(42));
34
- Value val3 = obj.Get("Douglas Adams");
35
-
36
- // Test if objects have properties.
37
- bool obj1 = obj.Has("hello"); // true
38
- bool obj2 = obj.Has("world"); // false
39
-
40
- }
41
- ```
42
-
43
- ## Methods
44
-
45
- ### Empty Constructor
46
-
47
- ```cpp
48
- Napi::Object::Object();
49
- ```
50
- Creates a new empty Object instance.
51
-
52
- ### Constructor
53
-
54
- ```cpp
55
- Napi::Object::Object(napi_env env, napi_value value);
56
- ```
57
- - `[in] env`: The `napi_env` environment in which to construct the Value object.
58
-
59
- - `[in] value`: The C++ primitive from which to instantiate the Value. `value` may be any of:
60
- - bool
61
- - Any integer type
62
- - Any floating point type
63
- - const char* (encoded using UTF-8, null-terminated)
64
- - const char16_t* (encoded using UTF-16-LE, null-terminated)
65
- - std::string (encoded using UTF-8)
66
- - std::u16string
67
- - Napi::Value
68
- - napi_value
69
-
70
- Creates a non-empty `Napi::Object` instance.
71
-
72
- ### New()
73
-
74
- ```cpp
75
- Napi::Object Napi::Object::New(napi_env env);
76
- ```
77
- - `[in] env`: The `napi_env` environment in which to construct the `Napi::Value` object.
78
-
79
- Creates a new `Napi::Object` value.
80
-
81
- ### Set()
82
-
83
- ```cpp
84
- void Napi::Object::Set (____ key, ____ value);
85
- ```
86
- - `[in] key`: The name for the property being assigned.
87
- - `[in] value`: The value being assigned to the property.
88
-
89
- Add a property with the specified key with the specified value to the object.
90
-
91
- The key can be any of the following types:
92
- - `napi_value`
93
- - [`Napi::Value`](value.md)
94
- - `const char*`
95
- - `const std::string&`
96
- - `uint32_t`
97
-
98
- While the value must be any of the following types:
99
- - `napi_value`
100
- - [`Napi::Value`](value.md)
101
- - `const char*`
102
- - `std::string&`
103
- - `bool`
104
- - `double`
105
-
106
- ### Delete()
107
-
108
- ```cpp
109
- bool Napi::Object::Delete(____ key);
110
- ```
111
- - `[in] key`: The name of the property to delete.
112
-
113
- Deletes the property associated with the given key. Returns `true` if the property was deleted.
114
-
115
- The `key` can be any of the following types:
116
- - `napi_value`
117
- - [`Napi::Value`](value.md)
118
- - `const char *`
119
- - `const std::string &`
120
- - `uint32_t`
121
-
122
- ### Get()
123
-
124
- ```cpp
125
- Napi::Value Napi::Object::Get(____ key);
126
- ```
127
- - `[in] key`: The name of the property to return the value for.
128
-
129
- Returns the [`Napi::Value`](value.md) associated with the key property. Returns the value *undefined* if the key does not exist.
130
-
131
- The `key` can be any of the following types:
132
- - `napi_value`
133
- - [`Napi::Value`](value.md)
134
- - `const char *`
135
- - `const std::string &`
136
- - `uint32_t`
137
-
138
- ### Has()
139
-
140
- ```cpp
141
- bool Napi::Object::Has (____ key) const;
142
- ```
143
- - `[in] key`: The name of the property to check.
144
-
145
- Returns a `bool` that is *true* if the object has a property named `key` and *false* otherwise.
146
-
147
- ### InstanceOf()
148
-
149
- ```cpp
150
- bool Napi::Object::InstanceOf (const Function& constructor) const
151
- ```
152
- - `[in] constructor`: The constructor [`Napi::Function`](function.md) of the value that is being compared with the object.
153
-
154
- Returns a `bool` that is true if the `Napi::Object` is an instance created by the `constructor` and false otherwise.
155
-
156
- Note: This is equivalent to the JavaScript instanceof operator.
157
-
158
- ### AddFinalizer()
159
- ```cpp
160
- template <typename Finalizer, typename T>
161
- inline void AddFinalizer(Finalizer finalizeCallback, T* data);
162
- ```
163
-
164
- - `[in] finalizeCallback`: The function to call when the object is garbage-collected.
165
- - `[in] data`: The data to associate with the object.
166
-
167
- Associates `data` with the object, calling `finalizeCallback` when the object is garbage-collected. `finalizeCallback`
168
- has the signature
169
- ```cpp
170
- void finalizeCallback(Napi::Env env, T* data);
171
- ```
172
- where `data` is the pointer that was passed into the call to `AddFinalizer()`.
173
-
174
- ### AddFinalizer()
175
- ```cpp
176
- template <typename Finalizer, typename T, typename Hint>
177
- inline void AddFinalizer(Finalizer finalizeCallback,
178
- T* data,
179
- Hint* finalizeHint);
180
- ```
181
-
182
- - `[in] data`: The data to associate with the object.
183
- - `[in] finalizeCallback`: The function to call when the object is garbage-collected.
184
-
185
- Associates `data` with the object, calling `finalizeCallback` when the object is garbage-collected. An additional hint
186
- may be given. It will also be passed to `finalizeCallback`, which has the signature
187
- ```cpp
188
- void finalizeCallback(Napi::Env env, T* data, Hint* hint);
189
- ```
190
- where `data` and `hint` are the pointers that were passed into the call to `AddFinalizer()`.
191
-
192
- ### GetPropertyNames()
193
- ```cpp
194
- Napi::Array Napi::Object::GetPropertyNames() const;
195
- ```
196
-
197
- Returns the names of the enumerable properties of the object as a [`Napi::Array`](array.md) of strings.
198
- The properties whose key is a `Symbol` will not be included.
199
-
200
- ### HasOwnProperty()
201
- ```cpp
202
- bool Napi::Object::HasOwnProperty(____ key); const
203
- ```
204
- - `[in] key` The name of the property to check.
205
-
206
- Returns a `bool` that is *true* if the object has an own property named `key` and *false* otherwise.
207
-
208
- The key can be any of the following types:
209
- - `napi_value`
210
- - [`Napi::Value`](value.md)
211
- - `const char*`
212
- - `const std::string&`
213
- - `uint32_t`
214
-
215
- ### DefineProperty()
216
-
217
- ```cpp
218
- void Napi::Object::DefineProperty (const Napi::PropertyDescriptor& property);
219
- ```
220
- - `[in] property`: A [`Napi::PropertyDescriptor`](property_descriptor.md).
221
-
222
- Define a property on the object.
223
-
224
- ### DefineProperties()
225
-
226
- ```cpp
227
- void Napi::Object::DefineProperties (____ properties)
228
- ```
229
- - `[in] properties`: A list of [`Napi::PropertyDescriptor`](property_descriptor.md). Can be one of the following types:
230
- - const std::initializer_list<Napi::PropertyDescriptor>&
231
- - const std::vector<Napi::PropertyDescriptor>&
232
-
233
- Defines properties on the object.
234
-
235
- ### Operator[]()
236
-
237
- ```cpp
238
- Napi::PropertyLValue<std::string> Napi::Object::operator[] (const char* utf8name);
239
- ```
240
- - `[in] utf8name`: UTF-8 encoded null-terminated property name.
241
-
242
- Returns a [`Napi::PropertyLValue`](propertylvalue.md) as the named property or sets the named property.
243
-
244
- ```cpp
245
- Napi::PropertyLValue<std::string> Napi::Object::operator[] (const std::string& utf8name);
246
- ```
247
- - `[in] utf8name`: UTF-8 encoded property name.
248
-
249
- Returns a [`Napi::PropertyLValue`](propertylvalue.md) as the named property or sets the named property.
250
-
251
- ```cpp
252
- Napi::PropertyLValue<uint32_t> Napi::Object::operator[] (uint32_t index);
253
- ```
254
- - `[in] index`: Element index.
255
-
256
- Returns a [`Napi::PropertyLValue`](propertylvalue.md) or sets an indexed property or array element.
257
-
258
- ```cpp
259
- Napi::Value Napi::Object::operator[] (const char* utf8name) const;
260
- ```
261
- - `[in] utf8name`: UTF-8 encoded null-terminated property name.
262
-
263
- Returns the named property as a [`Napi::Value`](value.md).
264
-
265
- ```cpp
266
- Napi::Value Napi::Object::operator[] (const std::string& utf8name) const;
267
- ```
268
- - `[in] utf8name`: UTF-8 encoded property name.
269
-
270
- Returns the named property as a [`Napi::Value`](value.md).
271
-
272
- ```cpp
273
- Napi::Value Napi::Object::operator[] (uint32_t index) const;
274
- ```
275
- - `[in] index`: Element index.
276
-
277
- Returns an indexed property or array element as a [`Napi::Value`](value.md).
278
-
279
- [`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 inner 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`.