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
@@ -1,186 +0,0 @@
1
- # Error handling
2
-
3
- Error handling represents one of the most important considerations when
4
- implementing a Node.js native add-on. When an error occurs in your C++ code you
5
- have to handle and dispatch it correctly. **node-addon-api** uses return values and
6
- JavaScript exceptions for error handling. You can choose return values or
7
- exception handling based on the mechanism that works best for your add-on.
8
-
9
- The `Napi::Error` is a persistent reference (for more info see: [`Napi::ObjectReference`](object_reference.md))
10
- to a JavaScript error object. Use of this class depends on whether C++
11
- exceptions are enabled at compile time.
12
-
13
- If C++ exceptions are enabled (for more info see: [Setup](setup.md)), then the
14
- `Napi::Error` class extends `std::exception` and enables integrated
15
- error-handling for C++ exceptions and JavaScript exceptions.
16
-
17
- The following sections explain the approach for each case:
18
-
19
- - [Handling Errors With C++ Exceptions](#exceptions)
20
- - [Handling Errors Without C++ Exceptions](#noexceptions)
21
-
22
- <a name="exceptions"></a>
23
-
24
- In most cases when an error occurs, the addon should do whatever clean is possible
25
- and then return to JavaScript so that they error can be propagated. In less frequent
26
- cases the addon may be able to recover from the error, clear the error and then
27
- continue.
28
-
29
- ## Handling Errors With C++ Exceptions
30
-
31
- When C++ exceptions are enabled try/catch can be used to catch exceptions thrown
32
- from calls to JavaScript and then they can either be handled or rethrown before
33
- returning from a native method.
34
-
35
- If a node-addon-api call fails without executing any JavaScript code (for example due to
36
- an invalid argument), then node-addon-api automatically converts and throws
37
- the error as a C++ exception of type `Napi::Error`.
38
-
39
- If a JavaScript function called by C++ code via node-addon-api throws a JavaScript
40
- exception, then node-addon-api automatically converts and throws it as a C++
41
- exception of type `Napi:Error` on return from the JavaScript code to the native
42
- method.
43
-
44
- If a C++ exception of type `Napi::Error` escapes from a N-API C++ callback, then
45
- the N-API wrapper automatically converts and throws it as a JavaScript exception.
46
-
47
- On return from a native method, node-addon-api will automatically convert a pending C++
48
- exception to a JavaScript exception.
49
-
50
- When C++ exceptions are enabled try/catch can be used to catch exceptions thrown
51
- from calls to JavaScript and then they can either be handled or rethrown before
52
- returning from a native method.
53
-
54
- ## Examples with C++ exceptions enabled
55
-
56
- ### Throwing a C++ exception
57
-
58
- ```cpp
59
- Env env = ...
60
- throw Napi::Error::New(env, "Example exception");
61
- // other C++ statements
62
- // ...
63
- ```
64
-
65
- The statements following the throw statement will not be executed. The exception
66
- will bubble up as a C++ exception of type `Napi::Error`, until it is either caught
67
- while still in C++, or else automatically propagated as a JavaScript exception
68
- when returning to JavaScript.
69
-
70
- ### Propagating a N-API C++ exception
71
-
72
- ```cpp
73
- Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
74
- Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
75
- // other C++ statements
76
- // ...
77
- ```
78
-
79
- The C++ statements following the call to the JavaScript function will not be
80
- executed. The exception will bubble up as a C++ exception of type `Napi::Error`,
81
- until it is either caught while still in C++, or else automatically propagated as
82
- a JavaScript exception when returning to JavaScript.
83
-
84
- ### Handling a N-API C++ exception
85
-
86
- ```cpp
87
- Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
88
- Napi::Value result;
89
- try {
90
- result = jsFunctionThatThrows({ arg1, arg2 });
91
- } catch (const Error& e) {
92
- cerr << "Caught JavaScript exception: " + e.what();
93
- }
94
- ```
95
-
96
- Since the exception was caught here, it will not be propagated as a JavaScript
97
- exception.
98
-
99
- <a name="noexceptions"></a>
100
-
101
- ## Handling Errors Without C++ Exceptions
102
-
103
- If C++ exceptions are disabled (for more info see: [Setup](setup.md)), then the
104
- `Napi::Error` class does not extend `std::exception`. This means that any calls to
105
- node-addon-api function do not throw a C++ exceptions. Instead, it raises
106
- _pending_ JavaScript exceptions and returns an _empty_ `Napi::Value`.
107
- The calling code should check `env.IsExceptionPending()` before attempting to use a
108
- returned value, and may use methods on the `Napi::Env` class
109
- to check for, get, and clear a pending JavaScript exception (for more info see: [Env](env.md)).
110
- If the pending exception is not cleared, it will be thrown when the native code
111
- returns to JavaScript.
112
-
113
- ## Examples with C++ exceptions disabled
114
-
115
- ### Throwing a JS exception
116
-
117
- ```cpp
118
- Napi::Env env = ...
119
- Napi::Error::New(env, "Example exception").ThrowAsJavaScriptException();
120
- return;
121
- ```
122
-
123
- After throwing a JavaScript exception, the code should generally return
124
- immediately from the native callback, after performing any necessary cleanup.
125
-
126
- ### Propagating a N-API JS exception
127
-
128
- ```cpp
129
- Napi::Env env = ...
130
- Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
131
- Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
132
- if (env.IsExceptionPending()) {
133
- Error e = env.GetAndClearPendingException();
134
- return e.Value();
135
- }
136
- ```
137
-
138
- If env.IsExceptionPending() returns true a JavaScript exception is pending. To
139
- let the exception propagate, the code should generally return immediately from
140
- the native callback, after performing any necessary cleanup.
141
-
142
- ### Handling a N-API JS exception
143
-
144
- ```cpp
145
- Napi::Env env = ...
146
- Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
147
- Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
148
- if (env.IsExceptionPending()) {
149
- Napi::Error e = env.GetAndClearPendingException();
150
- cerr << "Caught JavaScript exception: " + e.Message();
151
- }
152
- ```
153
-
154
- Since the exception was cleared here, it will not be propagated as a JavaScript
155
- exception after the native callback returns.
156
-
157
- ## Calling N-API directly from a **node-addon-api** addon
158
-
159
- **node-addon-api** provides macros for throwing errors in response to non-OK
160
- `napi_status` results when calling [N-API](https://nodejs.org/docs/latest/api/n-api.html)
161
- functions from within a native addon. These macros are defined differently
162
- depending on whether C++ exceptions are enabled or not, but are available for
163
- use in either case.
164
-
165
- ### `NAPI_THROW(e, ...)`
166
-
167
- This macro accepts a `Napi::Error`, throws it, and returns the value given as
168
- the last parameter. If C++ exceptions are enabled (by defining
169
- `NAPI_CPP_EXCEPTIONS` during the build), the return value will be ignored.
170
-
171
- ### `NAPI_THROW_IF_FAILED(env, status, ...)`
172
-
173
- This macro accepts a `Napi::Env` and a `napi_status`. It constructs an error
174
- from the `napi_status`, throws it, and returns the value given as the last
175
- parameter. If C++ exceptions are enabled (by defining `NAPI_CPP_EXCEPTIONS`
176
- during the build), the return value will be ignored.
177
-
178
- ### `NAPI_THROW_IF_FAILED_VOID(env, status)`
179
-
180
- This macro accepts a `Napi::Env` and a `napi_status`. It constructs an error
181
- from the `napi_status`, throws it, and returns.
182
-
183
- ### `NAPI_FATAL_IF_FAILED(status, location, message)`
184
-
185
- This macro accepts a `napi_status`, a C string indicating the location where the
186
- error occurred, and a second C string for the message to display.
@@ -1,82 +0,0 @@
1
- # EscapableHandleScope
2
-
3
- The `Napi::EscapableHandleScope` 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 by the garbage collector 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.
9
-
10
- The `Napi::EscapableHandleScope` is a special type of `Napi::HandleScope`
11
- which allows a single handle to be "promoted" to an outer scope.
12
-
13
- For more details refer to the section titled
14
- [Object lifetime management](object_lifetime_management.md).
15
-
16
- ## Methods
17
-
18
- ### Constructor
19
-
20
- Creates a new escapable handle scope.
21
-
22
- ```cpp
23
- Napi::EscapableHandleScope Napi::EscapableHandleScope::New(Napi:Env env);
24
- ```
25
-
26
- - `[in] Env`: The environment in which to construct the `Napi::EscapableHandleScope` object.
27
-
28
- Returns a new `Napi::EscapableHandleScope`
29
-
30
- ### Constructor
31
-
32
- Creates a new escapable handle scope.
33
-
34
- ```cpp
35
- Napi::EscapableHandleScope Napi::EscapableHandleScope::New(napi_env env, napi_handle_scope scope);
36
- ```
37
-
38
- - `[in] env`: napi_env in which the scope passed in was created.
39
- - `[in] scope`: pre-existing napi_handle_scope.
40
-
41
- Returns a new `Napi::EscapableHandleScope` instance which wraps the
42
- napi_escapable_handle_scope handle passed in. This can be used
43
- to mix usage of the C N-API and node-addon-api.
44
-
45
- operator EscapableHandleScope::napi_escapable_handle_scope
46
-
47
- ```cpp
48
- operator Napi::EscapableHandleScope::napi_escapable_handle_scope() const
49
- ```
50
-
51
- Returns the N-API napi_escapable_handle_scope wrapped by the `Napi::EscapableHandleScope` object.
52
- This can be used to mix usage of the C N-API and node-addon-api by allowing
53
- the class to be used be converted to a napi_escapable_handle_scope.
54
-
55
- ### Destructor
56
- ```cpp
57
- Napi::EscapableHandleScope::~EscapableHandleScope();
58
- ```
59
-
60
- Deletes the `Napi::EscapableHandleScope` instance and allows any objects/handles created
61
- in the scope to be collected by the garbage collector. There is no
62
- guarantee as to when the garbage collector will do this.
63
-
64
- ### Escape
65
-
66
- ```cpp
67
- napi::Value Napi::EscapableHandleScope::Escape(napi_value escapee);
68
- ```
69
-
70
- - `[in] escapee`: Napi::Value or napi_env to promote to the outer scope
71
-
72
- Returns `Napi::Value` which can be used in the outer scope. This method can
73
- be called at most once on a given `Napi::EscapableHandleScope`. If it is called
74
- more than once an exception will be thrown.
75
-
76
- ### Env
77
-
78
- ```cpp
79
- Napi::Env Napi::EscapableHandleScope::Env() const;
80
- ```
81
-
82
- Returns the `Napi::Env` associated with the `Napi::EscapableHandleScope`.
package/doc/external.md DELETED
@@ -1,63 +0,0 @@
1
- # External (template)
2
-
3
- Class `Napi::External<T>` inherits from class [`Napi::Value`][].
4
-
5
- The `Napi::External` template class implements the ability to create a `Napi::Value` object with arbitrary C++ data. It is the user's responsibility to manage the memory for the arbitrary C++ data.
6
-
7
- `Napi::External` objects can be created with an optional Finalizer function and optional Hint value. The Finalizer function, if specified, is called when your `Napi::External` object is released by Node's garbage collector. It gives your code the opportunity to free any dynamically created data. If you specify a Hint value, it is passed to your Finalizer function.
8
-
9
- ## Methods
10
-
11
- ### New
12
-
13
- ```cpp
14
- template <typename T>
15
- static Napi::External Napi::External::New(napi_env env, T* data);
16
- ```
17
-
18
- - `[in] env`: The `napi_env` environment in which to construct the `Napi::External` object.
19
- - `[in] data`: The arbitrary C++ data to be held by the `Napi::External` object.
20
-
21
- Returns the created `Napi::External<T>` object.
22
-
23
- ### New
24
-
25
- ```cpp
26
- template <typename T>
27
- static Napi::External Napi::External::New(napi_env env,
28
- T* data,
29
- Finalizer finalizeCallback);
30
- ```
31
-
32
- - `[in] env`: The `napi_env` environment in which to construct the `Napi::External` object.
33
- - `[in] data`: The arbitrary C++ data to be held by the `Napi::External` object.
34
- - `[in] finalizeCallback`: A function called when the `Napi::External` object is released by the garbage collector accepting a T* and returning void.
35
-
36
- Returns the created `Napi::External<T>` object.
37
-
38
- ### New
39
-
40
- ```cpp
41
- template <typename T>
42
- static Napi::External Napi::External::New(napi_env env,
43
- T* data,
44
- Finalizer finalizeCallback,
45
- Hint* finalizeHint);
46
- ```
47
-
48
- - `[in] env`: The `napi_env` environment in which to construct the `Napi::External` object.
49
- - `[in] data`: The arbitrary C++ data to be held by the `Napi::External` object.
50
- - `[in] finalizeCallback`: A function called when the `Napi::External` object is released by the garbage collector accepting T* and Hint* parameters and returning void.
51
- - `[in] finalizeHint`: A hint value passed to the `finalizeCallback` function.
52
-
53
- Returns the created `Napi::External<T>` object.
54
-
55
- ### Data
56
-
57
- ```cpp
58
- T* Napi::External::Data() const;
59
- ```
60
-
61
- Returns a pointer to the arbitrary C++ data held by the `Napi::External` object.
62
-
63
- [`Napi::Value`]: ./value.md