Gabriel Schulhof
a2f4206415
node-api: stop ref gc during environment teardown
...
A gc may happen during environment teardown. Thus, during finalization
initiated by environment teardown we must remove the V8 finalizer
before calling the Node-API finalizer.
Fixes: https://github.com/nodejs/node/issues/37236
PR-URL: https://github.com/nodejs/node/pull/37616
Backport-PR-URL: https://github.com/nodejs/node/pull/42512
Reviewed-By: Chengzhong Wu <legendecas@gmail.com >
Reviewed-By: Michael Dawson <midawson@redhat.com >
2022-03-30 10:48:01 -04:00
Gabriel Schulhof
93dd799a86
node-api: define version 8
...
Mark as stable the APIs that define Node-API version 8.
PR-URL: https://github.com/nodejs/node/pull/37652
Backport-PR-URL: https://github.com/nodejs/node/pull/37796
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de >
Reviewed-By: Michael Dawson <midawson@redhat.com >
Reviewed-By: Chengzhong Wu <legendecas@gmail.com >
2021-03-19 12:08:09 +00:00
Gabriel Schulhof and Michael Dawson
f5692093d3
node-api: allow retrieval of add-on file name
...
Unlike JS-only modules, native add-ons are always associated with a
dynamic shared object from which they are loaded. Being able to
retrieve its absolute path is important to native-only add-ons, i.e.
add-ons that are not themselves being loaded from a JS-only module
located in the same package as the native add-on itself.
Currently, the file name is obtained at environment construction time
from the JS `module.filename`. Nevertheless, the presence of `module`
is not required, because the file name could also be passed in via a
private property added onto `exports` from the `process.dlopen`
binding.
As an attempt at future-proofing, the file name is provided as a URL,
i.e. prefixed with the `file://` protocol.
Fixes: https://github.com/nodejs/node-addon-api/issues/449
PR-URL: https://github.com/nodejs/node/pull/37195
Backport-PR-URL: https://github.com/nodejs/node/pull/37328
Co-authored-by: Michael Dawson <mdawson@devrus.com >
Reviewed-By: Michael Dawson <midawson@redhat.com >
2021-03-17 20:59:08 +00:00
Michael Dawson
a93ca2d494
n-api: revert change to finalization
...
Fixes: https://github.com/nodejs/node/issues/35620
This reverts commit a6b655614f which
changed finalization behavior related to N-API. We will investigate
the original issue with the test separately.
PR-URL: https://github.com/nodejs/node/pull/35777
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com >
Reviewed-By: Rich Trott <rtrott@gmail.com >
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com >
Reviewed-By: Chengzhong Wu <legendecas@gmail.com >
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de >
2020-11-16 11:58:02 -05:00
Gabriel Schulhof and Anna Henningsen
a5aa3ddacf
n-api: re-implement async env cleanup hooks
...
* Avoid passing core `void*` and function pointers into add-on.
* Document `napi_async_cleanup_hook_handle` type.
* Render receipt of the handle mandatory from the point where the
hook gets called. Removal of the handle remains mandatory.
Fixes: https://github.com/nodejs/node/issues/34715
Signed-off-by: Gabriel Schulhof <gabriel.schulhof@intel.com >
Co-authored-by: Anna Henningsen <github@addaleax.net >
PR-URL: https://github.com/nodejs/node/pull/34819
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com >
Reviewed-By: Zeyu Yang <himself65@outlook.com >
2020-09-26 15:30:27 +02:00
Anna Henningsen
2cf231678b
n-api,src: provide asynchronous cleanup hooks
...
Sometimes addons need to perform cleanup actions, for example
closing libuv handles or waiting for requests to finish, that
cannot be performed synchronously.
Add C++ API and N-API functions that allow providing such
asynchronous cleanup hooks.
Fixes: https://github.com/nodejs/node/issues/34567
PR-URL: https://github.com/nodejs/node/pull/34572
Reviewed-By: James M Snell <jasnell@gmail.com >
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com >
2020-09-22 23:48:03 +02:00
Gabriel Schulhof
a6b655614f
n-api: handle weak no-finalizer refs correctly
...
When deleting a weak reference that has no finalizer we must not defer
deletion until the non-existent finalizer gets called.
Fixes: https://github.com/nodejs/node/issues/34731
Signed-off-by: Gabriel Schulhof <gabriel.schulhof@intel.com >
PR-URL: https://github.com/nodejs/node/pull/34839
Reviewed-By: Anna Henningsen <anna@addaleax.net >
Reviewed-By: Gus Caplan <me@gus.host >
Reviewed-By: James M Snell <jasnell@gmail.com >
Reviewed-By: Rich Trott <rtrott@gmail.com >
2020-09-22 19:55:59 +02:00
Anna Henningsen
335b61ac74
test: skip node-api/test_worker_terminate_finalization
...
The test fails under ASAN/valgrind. Since it has not been working
properly until today anyway, skip it.
Refs: https://github.com/nodejs/node/issues/34731
PR-URL: https://github.com/nodejs/node/pull/34732
Reviewed-By: James M Snell <jasnell@gmail.com >
Reviewed-By: Matheus Marchini <mat@mmarchini.me >
2020-09-22 19:55:25 +02:00
Anna Henningsen
e23f7ee1b9
test: fix test_worker_terminate_finalization
...
The test was missing an initialization of the global `ref` variable
because there was also an unused local one, leading to failures
like the one seen in https://github.com/nodejs/node/pull/34625 .
PR-URL: https://github.com/nodejs/node/pull/34726
Reviewed-By: Richard Lau <riclau@uk.ibm.com >
Reviewed-By: James M Snell <jasnell@gmail.com >
Reviewed-By: Ricky Zhou <0x19951125@gmail.com >
2020-09-22 19:55:21 +02:00
Michael Dawson
9e5d9d606a
n-api: ensure scope present for finalization
...
Refs: https://github.com/nodejs/node-addon-api/issues/722
Ensure a scope is on stack during finalization
as finalization functions can create JS Objects
Signed-off-by: Michael Dawson <michael_dawson@ca.ibm.com >
PR-URL: https://github.com/nodejs/node/pull/33508
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com >
Reviewed-By: James M Snell <jasnell@gmail.com >
2020-07-14 12:13:17 -07:00
Anna Henningsen
13cae34484
test: fix out-of-bound reads from invalid sizeof usage
...
`sizeof(data)` does not return the correct result here, as it measures
the size of the `data` variable, not what it points to.
PR-URL: https://github.com/nodejs/node/pull/33115
Reviewed-By: Colin Ihrig <cjihrig@gmail.com >
Reviewed-By: James M Snell <jasnell@gmail.com >
2020-05-13 17:53:00 +02:00
Anna Henningsen
08e01a12d0
test: add missing calls to napi_async_destroy
...
PR-URL: https://github.com/nodejs/node/pull/33114
Reviewed-By: Chengzhong Wu <legendecas@gmail.com >
Reviewed-By: Colin Ihrig <cjihrig@gmail.com >
Reviewed-By: James M Snell <jasnell@gmail.com >
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de >
2020-05-13 17:53:00 +02:00
legendecas
d08be9c8ca
n-api: fix false assumption on napi_async_context structures
...
napi_async_context should be an opaque type and not be used as same as
node::async_context.
PR-URL: https://github.com/nodejs/node/pull/32928
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com >
Reviewed-By: Anna Henningsen <anna@addaleax.net >
2020-05-13 17:52:53 +02:00
Gabriel Schulhof and legendecas
e22d853c5d
n-api: define release 6
...
Mark all N-APIs that have been added since version 5 as stable.
Backport-PR-URL: https://github.com/nodejs/node/pull/32482
PR-URL: https://github.com/nodejs/node/pull/32058
Fixes: https://github.com/nodejs/abi-stable-node/issues/393
Co-Authored-By: legendecas <legendecas@gmail.com >
Signed-off-by: Gabriel Schulhof <gabriel.schulhof@intel.com >
Reviewed-By: Chengzhong Wu <legendecas@gmail.com >
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl >
Reviewed-By: Colin Ihrig <cjihrig@gmail.com >
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de >
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com >
2020-04-28 16:00:37 +02:00
Gabriel Schulhof
08e09eca34
n-api: free instance data as reference
...
Instance data associated with a `napi_env` is no longer stored on the
env itself but is instead rendered as a reference. Since
`v8impl::Reference` is tied to a JS object, this modification factors
out the `v8impl::Reference` refcounting and the deletion process into
a base class for `v8impl::Reference`, called `v8impl::RefBase`. The
instance data is then stored as a `v8impl::RefBase`, along with other
references, preventing a segfault that arises from the fact that, up
until now, upon `napi_env` destruction, the instance data was freed
after all references had already been forcefully freed. If the addon
freed a reference during the `napi_set_instance_data` finalizer
callback, such a reference had already been freed during environment
teardown, causing a double free.
Re: https://github.com/nodejs/node-addon-api/pull/663
PR-URL: https://github.com/nodejs/node/pull/31638
Reviewed-By: Anna Henningsen <anna@addaleax.net >
Reviewed-By: David Carlier <devnexen@gmail.com >
2020-03-30 10:59:31 -07:00
Anna Henningsen
af5c489f39
n-api: keep napi_env alive while it has finalizers
...
Manage the napi_env refcount from Finalizer instances, as the
finalizer may refer to the napi_env until it is deleted.
Fixes: https://github.com/nodejs/node/issues/31134
Fixes: https://github.com/node-ffi-napi/node-ffi-napi/issues/48
PR-URL: https://github.com/nodejs/node/pull/31140
Reviewed-By: Jiawen Geng <technicalcute@gmail.com >
Reviewed-By: Rich Trott <rtrott@gmail.com >
2020-02-06 02:49:20 +00:00
Anna Henningsen
646b81c209
test: port worker + buffer test to N-API
...
This ports `test/addons/worker-buffer-callback` to N-API,
with the small exception of using external `ArrayBuffer`s rather
than external Node.js `Buffer`s.
PR-URL: https://github.com/nodejs/node/pull/30551
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com >
Reviewed-By: Colin Ihrig <cjihrig@gmail.com >
Reviewed-By: Denys Otrishko <shishugi@gmail.com >
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com >
2019-12-17 17:07:00 -05:00
Gabriel Schulhof
50d7c39d91
n-api: mark version 5 N-APIs as stable
...
PR-URL: https://github.com/nodejs/node/pull/29401
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl >
Reviewed-By: James M Snell <jasnell@gmail.com >
Reviewed-By: Colin Ihrig <cjihrig@gmail.com >
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com >
2019-09-20 09:36:17 +02:00
Gabriel Schulhof
860c0d89b6
n-api: add APIs for per-instance state management
...
Adds `napi_set_instance_data()` and `napi_get_instance_data()`, which
allow native addons to store their data on and retrieve their data from
`napi_env`. `napi_set_instance_data()` accepts a finalizer which is
called when the `node::Environment()` is destroyed.
This entails rendering the `napi_env` local to each add-on.
Fixes: https://github.com/nodejs/abi-stable-node/issues/378
PR-URL: https://github.com/nodejs/node/pull/28682
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl >
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com >
2019-08-02 22:19:01 +02:00
Anna Henningsen
79b1bf5a09
test: use assert() in N-API async test
...
The `Execute()` callback is not allowed to call into JS, so
we should use `assert()` instead of potentially throwing JS errors.
PR-URL: https://github.com/nodejs/node/pull/28423
Fixes: https://github.com/nodejs/help/issues/1998
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com >
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com >
Reviewed-By: Colin Ihrig <cjihrig@gmail.com >
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com >
Reviewed-By: Rich Trott <rtrott@gmail.com >
Reviewed-By: Luigi Pinca <luigipinca@gmail.com >
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com >
2019-07-02 11:46:55 +02:00
legendecas
53297e66cb
n-api: make func argument of napi_create_threadsafe_function optional
...
PR-URL: https://github.com/nodejs/node/pull/27791
Refs: https://github.com/nodejs/node/issues/27592
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com >
2019-07-02 09:07:45 +02:00
Anna Henningsen
a40cfb32d2
n-api: defer Buffer finalizer with SetImmediate
...
We have a test that verifies that JS execution from the Buffer
finalizer is accepted, and that errors thrown are passed
down synchronously.
However, since the finalizer executes during GC, this is behaviour is
fundamentally invalid and, for good reasons, disallowed by the
JS engine. This leaves us with the options of either finding a way
to allow JS execution from the callback, or explicitly forbidding it on
the N-API side as well.
This commit implements the former option, since it is the more
backwards-compatible one, in the sense that the current situation
sometimes appears to work as well and we should not break that
behaviour if we don’t have to, but rather try to actually make it
work reliably.
Since GC timing is largely unobservable anyway, this commit moves
the callback into a `SetImmediate()`, as we do elsewhere in the code,
and a second pass callback is not an easily implemented option,
as the API is supposed to wrap around Node’s `Buffer` API.
In this case, exceptions are handled like other uncaught exceptions.
Two tests have to be adjusted to account for the timing difference.
This is unfortunate, but unavoidable if we want to conform to the
JS engine API contract and keep all tests.
Fixes: https://github.com/nodejs/node/issues/26754
PR-URL: https://github.com/nodejs/node/pull/28082
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com >
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com >
Reviewed-By: James M Snell <jasnell@gmail.com >
2019-06-18 16:03:13 +02:00
Evgenii Shchepotev
0d74198123
test: cover import of a *.node file with a policy manifest
...
Cover import of a *.node file with a policy manifest. Add invalid
integrity test case.
PR-URL: https://github.com/nodejs/node/pull/27903
Reviewed-By: Anna Henningsen <anna@addaleax.net >
Reviewed-By: Rich Trott <rtrott@gmail.com >
2019-05-31 08:46:57 +02:00
Anna Henningsen
8b5d73867f
n-api: do not require JS Context for napi_async_destroy()
...
Allow the function to be called during GC, which is a common use case.
Fixes: https://github.com/nodejs/node/issues/27218
PR-URL: https://github.com/nodejs/node/pull/27255
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl >
Reviewed-By: James M Snell <jasnell@gmail.com >
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com >
2019-04-27 11:36:03 +02:00
Refael Ackermann
f2064dfc1f
src: de-lint header usage
...
PR-URL: https://github.com/nodejs/node/pull/26306
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com >
2019-03-12 09:57:19 -04:00
Rich Trott
33880d79dd
test: remove flaky designation for test_threadsafe_function
...
The test_threadsafe_function doesn't seem to be flaky anymore on
Windows. Optimistically removing the flaky designation in the relevant
status file.
Refs: https://github.com/nodejs/node/issues/23621#issuecomment-468938980
PR-URL: https://github.com/nodejs/node/pull/26403
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com >
Reviewed-By: Richard Lau <riclau@uk.ibm.com >
Reviewed-By: Anna Henningsen <anna@addaleax.net >
Reviewed-By: Colin Ihrig <cjihrig@gmail.com >
Reviewed-By: James M Snell <jasnell@gmail.com >
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de >
2019-03-04 11:09:47 -08:00
Anna Henningsen
8ebd339031
worker: improve integration with native addons
...
Allow loading add-ons from multiple Node.js instances if they are
declared context-aware; in particular, this applies to N-API addons.
Also, plug a memory leak that occurred when registering N-API addons.
Refs: https://github.com/nodejs/node/pull/23319
PR-URL: https://github.com/nodejs/node/pull/26175
Fixes: https://github.com/nodejs/node/issues/21481
Fixes: https://github.com/nodejs/node/issues/21783
Fixes: https://github.com/nodejs/node/issues/25662
Fixes: https://github.com/nodejs/node/issues/20239
Reviewed-By: James M Snell <jasnell@gmail.com >
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com >
Reviewed-By: Richard Lau <riclau@uk.ibm.com >
2019-02-22 21:42:09 +01:00
Anna Henningsen
441ef4d7f0
n-api: do not call into JS when that is not allowed
...
Check whether calling into JS is allowed before doing so.
PR-URL: https://github.com/nodejs/node/pull/26127
Reviewed-By: Gus Caplan <me@gus.host >
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com >
2019-02-17 18:09:58 +01:00
Gabriel Schulhof
11387e1454
n-api: mark thread-safe function as stable
...
Fixes: https://github.com/nodejs/node/issues/24249
PR-URL: https://github.com/nodejs/node/pull/25556
Reviewed-By: James M Snell <jasnell@gmail.com >
Reviewed-By: Colin Ihrig <cjihrig@gmail.com >
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com >
2019-01-21 22:30:11 -08:00
Rich Trott
06e5afa948
test: remove unused addons-napi directory
...
Refs: https://github.com/nodejs/node/pull/24557#issuecomment-444284512
PR-URL: https://github.com/nodejs/node/pull/24839
Reviewed-By: Colin Ihrig <cjihrig@gmail.com >
Reviewed-By: Richard Lau <riclau@uk.ibm.com >
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com >
2018-12-04 20:48:39 -08:00
Rich Trott
61e332b3cb
test: add .gitignore file for node-api
...
Refs: https://github.com/nodejs/node/pull/24557#issuecomment-444284114
PR-URL: https://github.com/nodejs/node/pull/24839
Reviewed-By: Colin Ihrig <cjihrig@gmail.com >
Reviewed-By: Richard Lau <riclau@uk.ibm.com >
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com >
2018-12-04 20:48:33 -08:00
Gabriel Schulhof
938e11882b
test: partition N-API tests
...
Partition test/addons-napi into test/js-native-api and test/node-api to
isolate the Node.js-agnostic portion of the N-API tests from the
Node.js-specific portion.
PR-URL: https://github.com/nodejs/node/pull/24557
Reviewed-By: Refael Ackermann <refack@gmail.com >
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com >
2018-12-04 13:58:17 -08:00