koffi 2.12.0 → 2.12.2
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 +17 -0
- package/LICENSE.txt +2 -2
- package/README.md +8 -8
- package/build/koffi/darwin_arm64/koffi.node +0 -0
- package/build/koffi/darwin_x64/koffi.node +0 -0
- package/build/koffi/freebsd_arm64/koffi.node +0 -0
- package/build/koffi/freebsd_ia32/koffi.node +0 -0
- package/build/koffi/freebsd_x64/koffi.node +0 -0
- package/build/koffi/linux_arm64/koffi.node +0 -0
- package/build/koffi/linux_armhf/koffi.node +0 -0
- package/build/koffi/linux_ia32/koffi.node +0 -0
- package/build/koffi/linux_loong64/koffi.node +0 -0
- package/build/koffi/linux_riscv64d/koffi.node +0 -0
- package/build/koffi/linux_x64/koffi.node +0 -0
- package/build/koffi/musl_arm64/koffi.node +0 -0
- package/build/koffi/musl_x64/koffi.node +0 -0
- package/build/koffi/openbsd_ia32/koffi.node +0 -0
- package/build/koffi/openbsd_x64/koffi.node +0 -0
- package/build/koffi/win32_arm64/koffi.exp +0 -0
- package/build/koffi/win32_arm64/koffi.node +0 -0
- package/build/koffi/win32_ia32/koffi.exp +0 -0
- package/build/koffi/win32_ia32/koffi.node +0 -0
- package/build/koffi/win32_x64/koffi.exp +0 -0
- package/build/koffi/win32_x64/koffi.node +0 -0
- package/doc/assets.ini +3 -3
- package/doc/develop.sh +4 -3
- package/doc/pages/platforms.md +1 -1
- package/doc/static/highlight.js +1 -1
- package/doc/static/koffi.css +3 -2
- package/doc/static/print.css +1 -1
- package/doc/templates/code.html +5 -5
- package/doc/templates/page.html +4 -4
- package/index.d.ts +2 -2
- package/index.js +25 -25
- package/indirect.js +25 -5
- package/package.json +2 -2
- package/src/cnoke/LICENSE.txt +2 -2
- package/src/cnoke/README.md +2 -0
- package/src/cnoke/assets/FindCNoke.cmake +12 -2
- package/src/cnoke/assets/win_delay_hook.c +4 -4
- package/src/cnoke/cnoke.js +4 -2
- package/src/cnoke/package.json +1 -1
- package/src/cnoke/src/builder.js +11 -14
- package/src/cnoke/src/index.js +2 -2
- package/src/cnoke/src/tools.js +34 -2
- package/src/core/{libcc/libcc.cc → base/base.cc} +3092 -1674
- package/src/core/{libcc/libcc.hh → base/base.hh} +3059 -2432
- package/src/core/{libcc → base}/mimetypes.inc +42 -4
- package/src/core/{libcc → base}/mimetypes_gen.py +3 -3
- package/src/core/unicode/generate.py +124 -0
- package/src/core/unicode/xid.cc +52 -0
- package/src/core/unicode/xid.hh +29 -0
- package/src/core/unicode/xid.inc +465 -0
- package/src/koffi/CMakeLists.txt +8 -5
- package/src/koffi/cmake/raylib.cmake +6 -2
- package/src/koffi/cmake/{sqlite.cmake → sqlite3.cmake} +3 -3
- package/src/koffi/examples/electron-forge/forge.config.js +23 -3
- package/src/koffi/examples/electron-forge/package.json +18 -16
- package/src/koffi/src/abi_arm32.cc +8 -21
- package/src/koffi/src/abi_arm32_asm.S +2 -2
- package/src/koffi/src/abi_arm64.cc +46 -59
- package/src/koffi/src/abi_arm64_asm.S +4 -4
- package/src/koffi/src/abi_arm64_asm.asm +2 -2
- package/src/koffi/src/abi_loong64_asm.S +2 -2
- package/src/koffi/src/abi_riscv64.cc +6 -19
- package/src/koffi/src/abi_riscv64_asm.S +2 -2
- package/src/koffi/src/abi_x64_sysv.cc +4 -17
- package/src/koffi/src/abi_x64_sysv_asm.S +5 -5
- package/src/koffi/src/abi_x64_win.cc +4 -17
- package/src/koffi/src/abi_x64_win_asm.asm +2 -2
- package/src/koffi/src/abi_x86.cc +9 -22
- package/src/koffi/src/abi_x86_asm.S +2 -2
- package/src/koffi/src/abi_x86_asm.asm +2 -2
- package/src/koffi/src/call.cc +49 -42
- package/src/koffi/src/call.hh +9 -8
- package/src/koffi/src/errno.inc +152 -152
- package/src/koffi/src/ffi.cc +45 -39
- package/src/koffi/src/ffi.hh +6 -6
- package/src/koffi/src/init.js +1 -1
- package/src/koffi/src/parser.cc +6 -5
- package/src/koffi/src/parser.hh +3 -3
- package/src/koffi/src/trampolines/armasm.inc +2 -2
- package/src/koffi/src/trampolines/gnu.inc +2 -2
- package/src/koffi/src/trampolines/masm32.inc +2 -2
- package/src/koffi/src/trampolines/masm64.inc +2 -2
- package/src/koffi/src/trampolines/prototypes.inc +2 -2
- package/src/koffi/src/util.cc +9 -9
- package/src/koffi/src/util.hh +3 -3
- package/src/koffi/src/win32.cc +5 -5
- package/src/koffi/src/win32.hh +9 -9
- package/vendor/node-addon-api/CHANGELOG.md +97 -2
- package/vendor/node-addon-api/CONTRIBUTING.md +117 -7
- package/vendor/node-addon-api/README.md +25 -249
- package/vendor/node-addon-api/common.gypi +1 -0
- package/vendor/node-addon-api/doc/README.md +145 -0
- package/vendor/node-addon-api/doc/array_buffer.md +15 -15
- package/vendor/node-addon-api/doc/basic_env.md +200 -0
- package/vendor/node-addon-api/doc/buffer.md +24 -26
- package/vendor/node-addon-api/doc/cmake-js.md +19 -0
- package/vendor/node-addon-api/doc/{creating_a_release.md → contributing/creating_a_release.md} +16 -4
- package/vendor/node-addon-api/doc/env.md +11 -131
- package/vendor/node-addon-api/doc/error_handling.md +12 -0
- package/vendor/node-addon-api/doc/external.md +13 -4
- package/vendor/node-addon-api/doc/finalization.md +153 -0
- package/vendor/node-addon-api/doc/memory_management.md +1 -1
- package/vendor/node-addon-api/doc/object_wrap.md +19 -3
- package/vendor/node-addon-api/doc/promises.md +51 -0
- package/vendor/node-addon-api/doc/setup.md +29 -6
- package/vendor/node-addon-api/doc/value.md +13 -0
- package/vendor/node-addon-api/doc/version_management.md +2 -2
- package/vendor/node-addon-api/eslint.config.js +5 -0
- package/vendor/node-addon-api/index.js +2 -0
- package/vendor/node-addon-api/napi-inl.h +592 -166
- package/vendor/node-addon-api/napi.h +167 -59
- package/vendor/node-addon-api/node_addon_api.gyp +10 -0
- package/vendor/node-addon-api/noexcept.gypi +1 -1
- package/vendor/node-addon-api/package.json +10 -13
- package/vendor/node-addon-api/release-please-config.json +15 -0
- package/vendor/node-addon-api/test/addon_build/tpl/binding.gyp +2 -1
- package/vendor/node-addon-api/test/array_buffer.js +1 -1
- package/vendor/node-addon-api/test/async_context.js +2 -2
- package/vendor/node-addon-api/test/async_progress_queue_worker.js +3 -3
- package/vendor/node-addon-api/test/async_progress_worker.js +3 -3
- package/vendor/node-addon-api/test/async_worker.cc +15 -13
- package/vendor/node-addon-api/test/async_worker.js +5 -5
- package/vendor/node-addon-api/test/basic_types/value.cc +6 -0
- package/vendor/node-addon-api/test/basic_types/value.js +17 -0
- package/vendor/node-addon-api/test/binding.cc +8 -0
- package/vendor/node-addon-api/test/binding.gyp +23 -1
- package/vendor/node-addon-api/test/buffer.js +1 -2
- package/vendor/node-addon-api/test/common/index.js +1 -1
- package/vendor/node-addon-api/test/except_all.cc +22 -0
- package/vendor/node-addon-api/test/except_all.js +14 -0
- package/vendor/node-addon-api/test/exports.js +19 -0
- package/vendor/node-addon-api/test/finalizer_order.cc +152 -0
- package/vendor/node-addon-api/test/finalizer_order.js +98 -0
- package/vendor/node-addon-api/test/function.js +2 -2
- package/vendor/node-addon-api/test/function_reference.js +2 -2
- package/vendor/node-addon-api/test/globalObject/global_object_delete_property.cc +4 -4
- package/vendor/node-addon-api/test/globalObject/global_object_get_property.cc +4 -4
- package/vendor/node-addon-api/test/globalObject/global_object_has_own_property.cc +3 -3
- package/vendor/node-addon-api/test/globalObject/global_object_set_property.cc +5 -5
- package/vendor/node-addon-api/test/name.cc +10 -8
- package/vendor/node-addon-api/test/object/delete_property.cc +5 -5
- package/vendor/node-addon-api/test/object/get_property.cc +5 -5
- package/vendor/node-addon-api/test/object/has_own_property.cc +4 -4
- package/vendor/node-addon-api/test/object/has_property.cc +5 -5
- package/vendor/node-addon-api/test/object/object.cc +1 -1
- package/vendor/node-addon-api/test/object/set_property.cc +5 -5
- package/vendor/node-addon-api/test/object/subscript_operator.cc +3 -3
- package/vendor/node-addon-api/test/object_reference.cc +18 -18
- package/vendor/node-addon-api/test/promise.cc +75 -0
- package/vendor/node-addon-api/test/promise.js +23 -0
- package/vendor/node-addon-api/test/require_basic_finalizers/index.js +38 -0
- package/vendor/node-addon-api/test/require_basic_finalizers/tpl/addon.cc +12 -0
- package/vendor/node-addon-api/test/require_basic_finalizers/tpl/binding.gyp +48 -0
- package/vendor/node-addon-api/test/require_basic_finalizers/tpl/index.js +3 -0
- package/vendor/node-addon-api/test/require_basic_finalizers/tpl/package.json +11 -0
- package/vendor/node-addon-api/test/run_script.cc +1 -1
- package/vendor/node-addon-api/test/type_taggable.cc +1 -1
- package/vendor/node-addon-api/test/type_taggable.js +3 -4
- package/vendor/node-addon-api/test/typedarray.cc +28 -24
- package/vendor/node-addon-api/tools/conversion.js +1 -1
- package/vendor/node-api-headers/CHANGELOG.md +29 -0
- package/vendor/node-api-headers/CREATING_A_RELEASE.md +12 -2
- package/vendor/node-api-headers/def/js_native_api.def +6 -1
- package/vendor/node-api-headers/def/node_api.def +7 -1
- package/vendor/node-api-headers/include/js_native_api.h +56 -24
- package/vendor/node-api-headers/include/js_native_api_types.h +27 -0
- package/vendor/node-api-headers/include/node_api.h +38 -29
- package/vendor/node-api-headers/lib/parse-utils.js +92 -0
- package/vendor/node-api-headers/package.json +7 -7
- package/vendor/node-api-headers/release-please-config.json +12 -0
- package/vendor/node-api-headers/scripts/update-headers.js +63 -12
- package/vendor/node-api-headers/scripts/write-symbols.js +1 -1
- package/vendor/node-api-headers/symbols.js +17 -1
- package/vendor/node-api-headers/test/parse-utils.js +21 -0
- package/doc/flat/flat.css +0 -27
- package/doc/flat/normal.css +0 -429
- package/doc/flat/print.css +0 -29
- package/doc/flat/reset.css +0 -41
- package/doc/flat/small.css +0 -104
- package/doc/flat/static.js +0 -161
- package/src/core/libcc/brotli.cc +0 -186
- package/src/core/libcc/lz4.cc +0 -197
- package/src/core/libcc/miniz.cc +0 -353
- package/vendor/node-addon-api/tools/eslint-format.js +0 -79
- /package/vendor/node-api-headers/{scripts → lib}/clang-utils.js +0 -0
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
# Env
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Class `Napi::Env` inherits from class [`Napi::BasicEnv`][].
|
|
4
4
|
|
|
5
|
-
The
|
|
5
|
+
The data structure containing the environment in which the request is being run.
|
|
6
|
+
|
|
7
|
+
The `Napi::Env` object is usually created and passed by the Node.js runtime or
|
|
8
|
+
node-addon-api infrastructure.
|
|
9
|
+
|
|
10
|
+
The `Napi::Env` object represents an environment that has a superset of APIs
|
|
11
|
+
when compared to `Napi::BasicEnv` and therefore _cannot_ be used in basic
|
|
12
|
+
finalizers. See [Finalization][] for more details.
|
|
6
13
|
|
|
7
14
|
## Methods
|
|
8
15
|
|
|
@@ -76,132 +83,5 @@ The `script` can be any of the following types:
|
|
|
76
83
|
- `const char *`
|
|
77
84
|
- `const std::string &`
|
|
78
85
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
template <typename T> T* GetInstanceData() const;
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
Returns the instance data that was previously associated with the environment,
|
|
85
|
-
or `nullptr` if none was associated.
|
|
86
|
-
|
|
87
|
-
### SetInstanceData
|
|
88
|
-
|
|
89
|
-
```cpp
|
|
90
|
-
template <typename T> using Finalizer = void (*)(Env, T*);
|
|
91
|
-
template <typename T, Finalizer<T> fini = Env::DefaultFini<T>>
|
|
92
|
-
void SetInstanceData(T* data) const;
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
- `[template] fini`: A function to call when the instance data is to be deleted.
|
|
96
|
-
Accepts a function of the form `void CleanupData(Napi::Env env, T* data)`. If
|
|
97
|
-
not given, the default finalizer will be used, which simply uses the `delete`
|
|
98
|
-
operator to destroy `T*` when the addon instance is unloaded.
|
|
99
|
-
- `[in] data`: A pointer to data that will be associated with the instance of
|
|
100
|
-
the addon for the duration of its lifecycle.
|
|
101
|
-
|
|
102
|
-
Associates a data item stored at `T* data` with the current instance of the
|
|
103
|
-
addon. The item will be passed to the function `fini` which gets called when an
|
|
104
|
-
instance of the addon is unloaded.
|
|
105
|
-
|
|
106
|
-
### SetInstanceData
|
|
107
|
-
|
|
108
|
-
```cpp
|
|
109
|
-
template <typename DataType, typename HintType>
|
|
110
|
-
using FinalizerWithHint = void (*)(Env, DataType*, HintType*);
|
|
111
|
-
template <typename DataType,
|
|
112
|
-
typename HintType,
|
|
113
|
-
FinalizerWithHint<DataType, HintType> fini =
|
|
114
|
-
Env::DefaultFiniWithHint<DataType, HintType>>
|
|
115
|
-
void SetInstanceData(DataType* data, HintType* hint) const;
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
- `[template] fini`: A function to call when the instance data is to be deleted.
|
|
119
|
-
Accepts a function of the form
|
|
120
|
-
`void CleanupData(Napi::Env env, DataType* data, HintType* hint)`. If not given,
|
|
121
|
-
the default finalizer will be used, which simply uses the `delete` operator to
|
|
122
|
-
destroy `T*` when the addon instance is unloaded.
|
|
123
|
-
- `[in] data`: A pointer to data that will be associated with the instance of
|
|
124
|
-
the addon for the duration of its lifecycle.
|
|
125
|
-
- `[in] hint`: A pointer to data that will be associated with the instance of
|
|
126
|
-
the addon for the duration of its lifecycle and will be passed as a hint to
|
|
127
|
-
`fini` when the addon instance is unloaded.
|
|
128
|
-
|
|
129
|
-
Associates a data item stored at `T* data` with the current instance of the
|
|
130
|
-
addon. The item will be passed to the function `fini` which gets called when an
|
|
131
|
-
instance of the addon is unloaded. This overload accepts an additional hint to
|
|
132
|
-
be passed to `fini`.
|
|
133
|
-
|
|
134
|
-
### GetModuleFileName
|
|
135
|
-
|
|
136
|
-
```cpp
|
|
137
|
-
const char* Napi::Env::GetModuleFileName() const;
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
Returns a A URL containing the absolute path of the location from which the
|
|
141
|
-
add-on was loaded. For a file on the local file system it will start with
|
|
142
|
-
`file://`. The string is null-terminated and owned by env and must thus not be
|
|
143
|
-
modified or freed. It is only valid while the add-on is loaded.
|
|
144
|
-
|
|
145
|
-
### AddCleanupHook
|
|
146
|
-
|
|
147
|
-
```cpp
|
|
148
|
-
template <typename Hook>
|
|
149
|
-
CleanupHook<Hook> AddCleanupHook(Hook hook);
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
- `[in] hook`: A function to call when the environment exits. Accepts a
|
|
153
|
-
function of the form `void ()`.
|
|
154
|
-
|
|
155
|
-
Registers `hook` as a function to be run once the current Node.js environment
|
|
156
|
-
exits. Unlike the underlying C-based Node-API, providing the same `hook`
|
|
157
|
-
multiple times **is** allowed. The hooks will be called in reverse order, i.e.
|
|
158
|
-
the most recently added one will be called first.
|
|
159
|
-
|
|
160
|
-
Returns an `Env::CleanupHook` object, which can be used to remove the hook via
|
|
161
|
-
its `Remove()` method.
|
|
162
|
-
|
|
163
|
-
### AddCleanupHook
|
|
164
|
-
|
|
165
|
-
```cpp
|
|
166
|
-
template <typename Hook, typename Arg>
|
|
167
|
-
CleanupHook<Hook, Arg> AddCleanupHook(Hook hook, Arg* arg);
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
- `[in] hook`: A function to call when the environment exits. Accepts a
|
|
171
|
-
function of the form `void (Arg* arg)`.
|
|
172
|
-
- `[in] arg`: A pointer to data that will be passed as the argument to `hook`.
|
|
173
|
-
|
|
174
|
-
Registers `hook` as a function to be run with the `arg` parameter once the
|
|
175
|
-
current Node.js environment exits. Unlike the underlying C-based Node-API,
|
|
176
|
-
providing the same `hook` and `arg` pair multiple times **is** allowed. The
|
|
177
|
-
hooks will be called in reverse order, i.e. the most recently added one will be
|
|
178
|
-
called first.
|
|
179
|
-
|
|
180
|
-
Returns an `Env::CleanupHook` object, which can be used to remove the hook via
|
|
181
|
-
its `Remove()` method.
|
|
182
|
-
|
|
183
|
-
# Env::CleanupHook
|
|
184
|
-
|
|
185
|
-
The `Env::CleanupHook` object allows removal of the hook added via
|
|
186
|
-
`Env::AddCleanupHook()`
|
|
187
|
-
|
|
188
|
-
## Methods
|
|
189
|
-
|
|
190
|
-
### IsEmpty
|
|
191
|
-
|
|
192
|
-
```cpp
|
|
193
|
-
bool IsEmpty();
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
Returns `true` if the cleanup hook was **not** successfully registered.
|
|
197
|
-
|
|
198
|
-
### Remove
|
|
199
|
-
|
|
200
|
-
```cpp
|
|
201
|
-
bool Remove(Env env);
|
|
202
|
-
```
|
|
203
|
-
|
|
204
|
-
Unregisters the hook from running once the current Node.js environment exits.
|
|
205
|
-
|
|
206
|
-
Returns `true` if the hook was successfully removed from the Node.js
|
|
207
|
-
environment.
|
|
86
|
+
[`Napi::BasicEnv`]: ./basic_env.md
|
|
87
|
+
[Finalization]: ./finalization.md
|
|
@@ -48,6 +48,18 @@ method.
|
|
|
48
48
|
If a C++ exception of type `Napi::Error` escapes from a Node-API C++ callback, then
|
|
49
49
|
the Node-API wrapper automatically converts and throws it as a JavaScript exception.
|
|
50
50
|
|
|
51
|
+
If other types of C++ exceptions are thrown, node-addon-api will either abort
|
|
52
|
+
the process or wrap the exception in an `Napi::Error` in order to throw it as a
|
|
53
|
+
JavaScript exception. This behavior is determined by which node-gyp dependency
|
|
54
|
+
used:
|
|
55
|
+
|
|
56
|
+
- When using the `node_addon_api_except` dependency, only `Napi::Error` objects
|
|
57
|
+
will be handled.
|
|
58
|
+
- When using the `node_addon_api_except_all` dependency, all exceptions will be
|
|
59
|
+
handled. For exceptions derived from `std::exception`, an `Napi::Error` will be
|
|
60
|
+
created with the message of the exception's `what()` member function. For all
|
|
61
|
+
other exceptions, an `Napi::Error` will be created with a generic error message.
|
|
62
|
+
|
|
51
63
|
On return from a native method, node-addon-api will automatically convert a pending
|
|
52
64
|
`Napi::Error` C++ exception to a JavaScript exception.
|
|
53
65
|
|
|
@@ -4,7 +4,11 @@ Class `Napi::External<T>` inherits from class [`Napi::TypeTaggable`][].
|
|
|
4
4
|
|
|
5
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
6
|
|
|
7
|
-
`Napi::External` objects can be created with an optional Finalizer function and
|
|
7
|
+
`Napi::External` objects can be created with an optional Finalizer function and
|
|
8
|
+
optional Hint value. The `Finalizer` function, if specified, is called when your
|
|
9
|
+
`Napi::External` object is released by Node's garbage collector. It gives your
|
|
10
|
+
code the opportunity to free any dynamically created data. If you specify a Hint
|
|
11
|
+
value, it is passed to your `Finalizer` function. See [Finalization][] for more details.
|
|
8
12
|
|
|
9
13
|
Note that `Napi::Value::IsExternal()` will return `true` for any external value.
|
|
10
14
|
It does not differentiate between the templated parameter `T` in
|
|
@@ -38,7 +42,9 @@ static Napi::External Napi::External::New(napi_env env,
|
|
|
38
42
|
|
|
39
43
|
- `[in] env`: The `napi_env` environment in which to construct the `Napi::External` object.
|
|
40
44
|
- `[in] data`: The arbitrary C++ data to be held by the `Napi::External` object.
|
|
41
|
-
- `[in] finalizeCallback`:
|
|
45
|
+
- `[in] finalizeCallback`: The function called when the engine destroys the
|
|
46
|
+
`Napi::External` object, implementing `operator()(Napi::BasicEnv, T*)`. See
|
|
47
|
+
[Finalization][] for more details.
|
|
42
48
|
|
|
43
49
|
Returns the created `Napi::External<T>` object.
|
|
44
50
|
|
|
@@ -54,8 +60,10 @@ static Napi::External Napi::External::New(napi_env env,
|
|
|
54
60
|
|
|
55
61
|
- `[in] env`: The `napi_env` environment in which to construct the `Napi::External` object.
|
|
56
62
|
- `[in] data`: The arbitrary C++ data to be held by the `Napi::External` object.
|
|
57
|
-
- `[in] finalizeCallback`:
|
|
58
|
-
|
|
63
|
+
- `[in] finalizeCallback`: The function called when the engine destroys the
|
|
64
|
+
`Napi::External` object, implementing `operator()(Napi::BasicEnv, T*, Hint*)`.
|
|
65
|
+
See [Finalization][] for more details.
|
|
66
|
+
- `[in] finalizeHint`: The hint value passed to the `finalizeCallback` function.
|
|
59
67
|
|
|
60
68
|
Returns the created `Napi::External<T>` object.
|
|
61
69
|
|
|
@@ -67,4 +75,5 @@ T* Napi::External::Data() const;
|
|
|
67
75
|
|
|
68
76
|
Returns a pointer to the arbitrary C++ data held by the `Napi::External` object.
|
|
69
77
|
|
|
78
|
+
[Finalization]: ./finalization.md
|
|
70
79
|
[`Napi::TypeTaggable`]: ./type_taggable.md
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# Finalization
|
|
2
|
+
|
|
3
|
+
Various node-addon-api methods accept a templated `Finalizer finalizeCallback`
|
|
4
|
+
parameter. This parameter represents a native callback function that runs in
|
|
5
|
+
response to a garbage collection event. A finalizer is considered a _basic_
|
|
6
|
+
finalizer if the callback only utilizes a certain subset of APIs, which may
|
|
7
|
+
provide more efficient memory management, optimizations, improved execution, or
|
|
8
|
+
other benefits.
|
|
9
|
+
|
|
10
|
+
In general, it is best to use basic finalizers whenever possible (eg. when
|
|
11
|
+
access to JavaScript is _not_ needed). The
|
|
12
|
+
`NODE_ADDON_API_REQUIRE_BASIC_FINALIZERS` preprocessor directive can be defined
|
|
13
|
+
to ensure that all finalizers are basic.
|
|
14
|
+
|
|
15
|
+
## Finalizers
|
|
16
|
+
|
|
17
|
+
The callback takes `Napi::Env` as its first argument:
|
|
18
|
+
|
|
19
|
+
### Example
|
|
20
|
+
|
|
21
|
+
```cpp
|
|
22
|
+
Napi::External<int>::New(Env(), new int(1), [](Napi::Env env, int* data) {
|
|
23
|
+
env.RunScript("console.log('Finalizer called')");
|
|
24
|
+
delete data;
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Basic Finalizers
|
|
29
|
+
|
|
30
|
+
Use of basic finalizers may allow the engine to perform optimizations when
|
|
31
|
+
scheduling or executing the callback. For example, V8 does not allow access to
|
|
32
|
+
the engine heap during garbage collection. Restricting finalizers from accessing
|
|
33
|
+
the engine heap allows the callback to execute during garbage collection,
|
|
34
|
+
providing a chance to free native memory eagerly.
|
|
35
|
+
|
|
36
|
+
In general, APIs that access engine heap are not allowed in basic finalizers.
|
|
37
|
+
|
|
38
|
+
The callback takes `Napi::BasicEnv` as its first argument:
|
|
39
|
+
|
|
40
|
+
### Example
|
|
41
|
+
|
|
42
|
+
```cpp
|
|
43
|
+
Napi::ArrayBuffer::New(
|
|
44
|
+
Env(), data, length, [](Napi::BasicEnv /*env*/, void* finalizeData) {
|
|
45
|
+
delete[] static_cast<uint8_t*>(finalizeData);
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Scheduling Finalizers
|
|
50
|
+
|
|
51
|
+
In addition to passing finalizers to `Napi::External`s and other Node-API
|
|
52
|
+
constructs, `Napi::BasicEnv::PostFinalize(Napi::BasicEnv, Finalizer)` can be
|
|
53
|
+
used to schedule a callback to run outside of the garbage collector
|
|
54
|
+
finalization. Since the associated native memory may already be freed by the
|
|
55
|
+
basic finalizer, any additional data may be passed eg. via the finalizer's
|
|
56
|
+
parameters (`T data*`, `Hint hint*`) or via lambda capture. This allows for
|
|
57
|
+
freeing native data in a basic finalizer, while executing any JavaScript code in
|
|
58
|
+
an additional finalizer.
|
|
59
|
+
|
|
60
|
+
### Example
|
|
61
|
+
|
|
62
|
+
```cpp
|
|
63
|
+
// Native Add-on
|
|
64
|
+
|
|
65
|
+
#include <iostream>
|
|
66
|
+
#include <memory>
|
|
67
|
+
#include "napi.h"
|
|
68
|
+
|
|
69
|
+
using namespace Napi;
|
|
70
|
+
|
|
71
|
+
// A structure representing some data that uses a "large" amount of memory.
|
|
72
|
+
class LargeData {
|
|
73
|
+
public:
|
|
74
|
+
LargeData() : id(instances++) {}
|
|
75
|
+
size_t id;
|
|
76
|
+
|
|
77
|
+
static size_t instances;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
size_t LargeData::instances = 0;
|
|
81
|
+
|
|
82
|
+
// Basic finalizer to free `LargeData`. Takes ownership of the pointer and
|
|
83
|
+
// frees its memory after use.
|
|
84
|
+
void MyBasicFinalizer(Napi::BasicEnv env, LargeData* data) {
|
|
85
|
+
std::unique_ptr<LargeData> instance(data);
|
|
86
|
+
std::cout << "Basic finalizer for instance " << instance->id
|
|
87
|
+
<< " called\n";
|
|
88
|
+
|
|
89
|
+
// Register a finalizer. Since the instance will be deleted by
|
|
90
|
+
// the time this callback executes, pass the instance's `id` via lambda copy
|
|
91
|
+
// capture and _not_ a reference capture that accesses `this`.
|
|
92
|
+
env.PostFinalizer([instanceId = instance->id](Napi::Env env) {
|
|
93
|
+
env.RunScript("console.log('Finalizer for instance " +
|
|
94
|
+
std::to_string(instanceId) + " called');");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Free the `LargeData` held in `data` once `instance` goes out of scope.
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
Value CreateExternal(const CallbackInfo& info) {
|
|
101
|
+
// Create a new instance of LargeData.
|
|
102
|
+
auto instance = std::make_unique<LargeData>();
|
|
103
|
+
|
|
104
|
+
// Wrap the instance in an External object, registering a basic
|
|
105
|
+
// finalizer that will delete the instance to free the "large" amount of
|
|
106
|
+
// memory.
|
|
107
|
+
return External<LargeData>::New(info.Env(), instance.release(), MyBasicFinalizer);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
Object Init(Napi::Env env, Object exports) {
|
|
111
|
+
exports["createExternal"] = Function::New(env, CreateExternal);
|
|
112
|
+
return exports;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
NODE_API_MODULE(addon, Init)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
// JavaScript
|
|
120
|
+
|
|
121
|
+
const { createExternal } = require('./addon.node');
|
|
122
|
+
|
|
123
|
+
for (let i = 0; i < 5; i++) {
|
|
124
|
+
const ext = createExternal();
|
|
125
|
+
// ... do something with `ext` ..
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
console.log('Loop complete');
|
|
129
|
+
await new Promise(resolve => setImmediate(resolve));
|
|
130
|
+
console.log('Next event loop cycle');
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Possible output:
|
|
134
|
+
|
|
135
|
+
```
|
|
136
|
+
Basic finalizer for instance 0 called
|
|
137
|
+
Basic finalizer for instance 1 called
|
|
138
|
+
Basic finalizer for instance 2 called
|
|
139
|
+
Basic finalizer for instance 3 called
|
|
140
|
+
Basic finalizer for instance 4 called
|
|
141
|
+
Loop complete
|
|
142
|
+
Finalizer for instance 3 called
|
|
143
|
+
Finalizer for instance 4 called
|
|
144
|
+
Finalizer for instance 1 called
|
|
145
|
+
Finalizer for instance 2 called
|
|
146
|
+
Finalizer for instance 0 called
|
|
147
|
+
Next event loop cycle
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
If the garbage collector runs during the loop, the basic finalizers execute and
|
|
151
|
+
display their logging message synchronously during the loop execution. The
|
|
152
|
+
additional finalizers execute at some later point after the garbage collection
|
|
153
|
+
cycle.
|
|
@@ -17,7 +17,7 @@ more often than it would otherwise in an attempt to garbage collect the JavaScri
|
|
|
17
17
|
objects that keep the externally allocated memory alive.
|
|
18
18
|
|
|
19
19
|
```cpp
|
|
20
|
-
static int64_t Napi::MemoryManagement::AdjustExternalMemory(Napi::
|
|
20
|
+
static int64_t Napi::MemoryManagement::AdjustExternalMemory(Napi::BasicEnv env, int64_t change_in_bytes);
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
- `[in] env`: The environment in which the API is invoked under.
|
|
@@ -241,9 +241,24 @@ request being made.
|
|
|
241
241
|
|
|
242
242
|
### Finalize
|
|
243
243
|
|
|
244
|
-
Provides an opportunity to run cleanup code that
|
|
245
|
-
|
|
246
|
-
|
|
244
|
+
Provides an opportunity to run cleanup code that only utilizes basic Node APIs, if any.
|
|
245
|
+
Override to implement. See [Finalization][] for more details.
|
|
246
|
+
|
|
247
|
+
```cpp
|
|
248
|
+
virtual void Finalize(Napi::BasicEnv env);
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
- `[in] env`: `Napi::Env`.
|
|
252
|
+
|
|
253
|
+
### Finalize
|
|
254
|
+
|
|
255
|
+
Provides an opportunity to run cleanup code that utilizes non-basic Node APIs.
|
|
256
|
+
Override to implement.
|
|
257
|
+
|
|
258
|
+
*NOTE*: Defining this method causes the deletion of the underlying `T* data` to
|
|
259
|
+
be postponed until _after_ the garbage collection cycle. Since an `Napi::Env`
|
|
260
|
+
has access to non-basic Node APIs, it cannot run in the same current tick as the
|
|
261
|
+
garbage collector.
|
|
247
262
|
|
|
248
263
|
```cpp
|
|
249
264
|
virtual void Finalize(Napi::Env env);
|
|
@@ -586,3 +601,4 @@ Returns `Napi::PropertyDescriptor` object that represents an static value
|
|
|
586
601
|
property of a JavaScript class
|
|
587
602
|
|
|
588
603
|
[`Napi::InstanceWrap<T>`]: ./instance_wrap.md
|
|
604
|
+
[Finalization]: ./finalization.md
|
|
@@ -75,5 +75,56 @@ Rejects the Promise object held by the `Napi::Promise::Deferred` object.
|
|
|
75
75
|
|
|
76
76
|
* `[in] value`: The Node-API primitive value with which to reject the `Napi::Promise`.
|
|
77
77
|
|
|
78
|
+
## Promise Methods
|
|
79
|
+
|
|
80
|
+
### Then
|
|
81
|
+
|
|
82
|
+
```cpp
|
|
83
|
+
Napi::Promise Napi::Promise::Then(napi_value onFulfilled) const;
|
|
84
|
+
Napi::Promise Napi::Promise::Then(const Function& onFulfilled) const;
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Attaches a fulfillment handler to the promise and returns a new promise.
|
|
88
|
+
|
|
89
|
+
**Parameters:**
|
|
90
|
+
* `[in] onFulfilled`: The fulfillment handler for the promise. May be any of:
|
|
91
|
+
- `napi_value` – a JavaScript function to be called when the promise is fulfilled.
|
|
92
|
+
- `const Function&` – the [`Napi::Function`](function.md) to be called when the promise is fulfilled.
|
|
93
|
+
|
|
94
|
+
**Returns:** A new `Napi::Promise` that resolves or rejects based on the handler's result.
|
|
95
|
+
|
|
96
|
+
### Then
|
|
97
|
+
|
|
98
|
+
```cpp
|
|
99
|
+
Napi::Promise Napi::Promise::Then(napi_value onFulfilled, napi_value onRejected) const;
|
|
100
|
+
Napi::Promise Napi::Promise::Then(const Function& onFulfilled,
|
|
101
|
+
const Function& onRejected) const;
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Attaches a fulfillment and rejection handlers to the promise and returns a new promise.
|
|
105
|
+
|
|
106
|
+
**Parameters:**
|
|
107
|
+
* `[in] onFulfilled`: The fulfillment handler for the promise. May be any of:
|
|
108
|
+
- `napi_value` – a JavaScript function to be called when the promise is fulfilled.
|
|
109
|
+
- `const Function&` – the [`Napi::Function`](function.md) to be called when the promise is fulfilled.
|
|
110
|
+
* `[in] onRejected` (optional): The rejection handler for the promise. May be any of:
|
|
111
|
+
- `napi_value` – a JavaScript function to be called when the promise is rejected.
|
|
112
|
+
- `const Function&` – the [`Napi::Function`](function.md) to be called when the promise is rejected.
|
|
113
|
+
|
|
114
|
+
### Catch
|
|
115
|
+
```cpp
|
|
116
|
+
Napi::Promise Napi::Promise::Catch(napi_value onRejected) const;
|
|
117
|
+
Napi::Promise Napi::Promise::Catch(const Function& onRejected) const;
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Attaches a rejection handler to the promise and returns a new promise.
|
|
121
|
+
|
|
122
|
+
**Parameters:**
|
|
123
|
+
* `[in] onRejected`: The rejection handler for the promise. May be any of:
|
|
124
|
+
- `napi_value` – a JavaScript function to be called when the promise is rejected.
|
|
125
|
+
- `const Function&` – the [`Napi::Function`](function.md) to be called when the promise is rejected.
|
|
126
|
+
|
|
127
|
+
**Returns:** A new `Napi::Promise` that handles rejection cases.
|
|
78
128
|
|
|
79
129
|
[`Napi::Object`]: ./object.md
|
|
130
|
+
[`Napi::Function`]: ./function.md
|
|
@@ -38,7 +38,10 @@ To use **Node-API** in a native module:
|
|
|
38
38
|
],
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
-
To enable that capability, add an alternative dependency in `binding.gyp
|
|
41
|
+
To enable that capability, add an alternative dependency in `binding.gyp`
|
|
42
|
+
depending on if you want to integrate C++ exception handling for exceptions
|
|
43
|
+
derived from `Napi::Error` or all C++ exceptions. To catch only
|
|
44
|
+
`Napi::Error` exceptions, use:
|
|
42
45
|
|
|
43
46
|
```gyp
|
|
44
47
|
'dependencies': [
|
|
@@ -46,6 +49,15 @@ To use **Node-API** in a native module:
|
|
|
46
49
|
],
|
|
47
50
|
```
|
|
48
51
|
|
|
52
|
+
Or, to allow catching all native C++ exceptions, use the
|
|
53
|
+
`node_addon_api_except_all` dependency:
|
|
54
|
+
|
|
55
|
+
```gyp
|
|
56
|
+
'dependencies': [
|
|
57
|
+
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except_all",
|
|
58
|
+
],
|
|
59
|
+
```
|
|
60
|
+
|
|
49
61
|
If you decide to use node-addon-api without C++ exceptions enabled, please
|
|
50
62
|
consider enabling node-addon-api safe API type guards to ensure the proper
|
|
51
63
|
exception handling pattern:
|
|
@@ -81,12 +93,23 @@ To use **Node-API** in a native module:
|
|
|
81
93
|
At build time, the Node-API back-compat library code will be used only when the
|
|
82
94
|
targeted node version *does not* have Node-API built-in.
|
|
83
95
|
|
|
84
|
-
The preprocessor directive
|
|
85
|
-
compile time before including `napi.h` to skip the definition of deprecated
|
|
96
|
+
The `NODE_ADDON_API_DISABLE_DEPRECATED` preprocessor directive can be defined at
|
|
97
|
+
compile time before including `napi.h` to skip the definition of deprecated
|
|
98
|
+
APIs.
|
|
86
99
|
|
|
87
100
|
By default, throwing an exception on a terminating environment (eg. worker
|
|
88
101
|
threads) will cause a fatal exception, terminating the Node process. This is to
|
|
89
102
|
provide feedback to the user of the runtime error, as it is impossible to pass
|
|
90
|
-
the error to JavaScript when the environment is terminating.
|
|
91
|
-
|
|
92
|
-
|
|
103
|
+
the error to JavaScript when the environment is terminating. The
|
|
104
|
+
`NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS` preprocessor directive can be defined
|
|
105
|
+
to bypass this behavior, such that the Node process will not terminate.
|
|
106
|
+
|
|
107
|
+
Various Node-API constructs provide a mechanism to run a callback in response to
|
|
108
|
+
a garbage collection event of that object. These callbacks are called
|
|
109
|
+
[_finalizers_]. Some finalizers have restrictions on the type of Node-APIs
|
|
110
|
+
available within the callback. node-addon-api provides convenience helpers that
|
|
111
|
+
bypass this limitation, but may cause the add-on to run less efficiently. The
|
|
112
|
+
`NODE_ADDON_API_REQUIRE_BASIC_FINALIZERS` preprocessor directive can be defined
|
|
113
|
+
to disable the convenience helpers.
|
|
114
|
+
|
|
115
|
+
[_finalizers_]: ./finalization.md
|
|
@@ -86,6 +86,19 @@ In order to enforce expected type, use `Napi::Value::Is*()` methods to check
|
|
|
86
86
|
the type before calling `Napi::Value::As()`, or compile with definition
|
|
87
87
|
`NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS` to enforce type checks.
|
|
88
88
|
|
|
89
|
+
### UnsafeAs
|
|
90
|
+
|
|
91
|
+
```cpp
|
|
92
|
+
template <typename T> T Napi::Value::UnsafeAs() const;
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Casts to another type of `Napi::Value`, when the actual type is known or
|
|
96
|
+
assumed.
|
|
97
|
+
|
|
98
|
+
This conversion does not coerce the type. This does not check the type even if
|
|
99
|
+
`NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS` is defined. This indicates intentional
|
|
100
|
+
unsafe type cast. Use `Napi::Value::As()` if possible.
|
|
101
|
+
|
|
89
102
|
### Env
|
|
90
103
|
|
|
91
104
|
```cpp
|
|
@@ -11,7 +11,7 @@ important to make decisions based on different versions of the system.
|
|
|
11
11
|
Retrieves the highest Node-API version supported by Node.js runtime.
|
|
12
12
|
|
|
13
13
|
```cpp
|
|
14
|
-
static uint32_t Napi::VersionManagement::GetNapiVersion(
|
|
14
|
+
static uint32_t Napi::VersionManagement::GetNapiVersion(Napi::BasicEnv env);
|
|
15
15
|
```
|
|
16
16
|
|
|
17
17
|
- `[in] env`: The environment in which the API is invoked under.
|
|
@@ -34,7 +34,7 @@ typedef struct {
|
|
|
34
34
|
````
|
|
35
35
|
|
|
36
36
|
```cpp
|
|
37
|
-
static const napi_node_version* Napi::VersionManagement::GetNodeVersion(
|
|
37
|
+
static const napi_node_version* Napi::VersionManagement::GetNodeVersion(Napi::BasicEnv env);
|
|
38
38
|
```
|
|
39
39
|
|
|
40
40
|
- `[in] env`: The environment in which the API is invoked under.
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
|
+
const { version } = require('./package.json');
|
|
2
3
|
|
|
3
4
|
const includeDir = path.relative('.', __dirname);
|
|
4
5
|
|
|
@@ -7,6 +8,7 @@ module.exports = {
|
|
|
7
8
|
include_dir: includeDir,
|
|
8
9
|
gyp: path.join(includeDir, 'node_api.gyp:nothing'), // deprecated.
|
|
9
10
|
targets: path.join(includeDir, 'node_addon_api.gyp'),
|
|
11
|
+
version,
|
|
10
12
|
isNodeApiBuiltin: true,
|
|
11
13
|
needsFlag: false
|
|
12
14
|
};
|