summaryrefslogtreecommitdiff
path: root/test/node-api
Commit message (Collapse)AuthorAgeFilesLines
* node-api: get Node API version used by addonVladimir Morozov2023-05-053-0/+318
| | | | | | | PR-URL: https://github.com/nodejs/node/pull/45715 Reviewed-By: Gabriel Schulhof <gabrielschulhof@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
* src: throw DataCloneError on transfering untransferable objectsChengzhong Wu2023-05-051-1/+4
| | | | | | | | | | | | The HTML StructuredSerializeWithTransfer algorithm defines that when an untransferable object is in the transfer list, a DataCloneError is thrown. An array buffer that is already transferred is also considered as untransferable. PR-URL: https://github.com/nodejs/node/pull/47604 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
* node-api: deprecate napi_module_registerVladimir Morozov2023-03-241-1/+51
| | | | | | PR-URL: https://github.com/nodejs/node/pull/46319 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
* node-api: verify cleanup hooks orderChengzhong Wu2023-03-153-2/+63
| | | | | | | | | | Cleanup hooks are called before the environment shutdown finalizer invocations. PR-URL: https://github.com/nodejs/node/pull/46692 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
* test: add trailing commas in `test/node-api`Antoine du Hamel2023-01-299-26/+26
| | | | | | PR-URL: https://github.com/nodejs/node/pull/46384 Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: James M Snell <jasnell@gmail.com>
* process,worker: ensure code after exit() effectlessywave6202022-12-252-4/+2
| | | | | | | | | | | | Cope with the delay(to the next function call) of v8::Isolate::TerminateExecution() PR-URL: https://github.com/nodejs/node/pull/45620 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
* node-api: handle no support for external buffersMichael Dawson2022-11-091-0/+5
| | | | | | | | | | | | | | | | | | | Refs: https://github.com/electron/electron/issues/35801 Refs: https://github.com/nodejs/abi-stable-node/issues/441 Electron recently dropped support for external buffers. Provide a way for addon authors to: - hide the methods to create external buffers so they can avoid using them if they want the broadest compatibility. - call the methods that create external buffers at runtime to check if external buffers are supported and either use them or not based on the return code. Signed-off-by: Michael Dawson <mdawson@devrus.com> PR-URL: https://github.com/nodejs/node/pull/45181 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
* test: use uv_sleep() where possibleSantiago Gimeno2022-10-271-16/+3
| | | | | | | | | | | | | | PR-URL: https://github.com/nodejs/node/pull/45124 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: theanarkh <theratliter@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
* src: disambiguate terms used to refer to builtins and addonsJoyee Cheung2022-08-091-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The term "native module" dates back to some of the oldest code in the code base. Within the context of Node.js core it usually refers to modules that are native to Node.js (e.g. fs, http), but it can cause confusion for people who don't work on this part of the code base, as "native module" can also refer to native addons - which is even the case in some of the API docs and error messages. This patch tries to make the usage of these terms more consistent. Now within the context of Node.js core: - JavaScript scripts that are built-in to Node.js are now referred to as "built-in(s)". If they are available as modules, they can also be referred to as "built-in module(s)". - Dynamically-linked shared objects that are loaded into the Node.js processes are referred to as "addons". We will try to avoid using the term "native modules" because it could be ambiguous. Changes in this patch: File names: - node_native_module.h -> node_builtins.h, - node_native_module.cc -> node_builtins.cc C++ binding names: - `native_module` -> `builtins` `node::Environment`: - `native_modules_without_cache` -> `builtins_without_cache` - `native_modules_with_cache` -> `builtins_with_cache` - `native_modules_in_snapshot` -> `builtins_in_cache` - `native_module_require` -> `builtin_module_require` `node::EnvSerializeInfo`: - `native_modules` -> `builtins `node::native_module::NativeModuleLoader`: - `native_module` namespace -> `builtins` namespace - `NativeModuleLoader` -> `BuiltinLoader` - `NativeModuleRecordMap` -> `BuiltinSourceMap` - `NativeModuleCacheMap` -> `BuiltinCodeCacheMap` - `ModuleIds` -> `BuiltinIds` - `ModuleCategories` -> `BuiltinCategories` - `LoadBuiltinModuleSource` -> `LoadBuiltinSource` `loader.js`: - `NativeModule` -> `BuiltinModule` (the `NativeModule` name used in `process.moduleLoadList` is kept for compatibility) And other clarifications in the documentation and comments. PR-URL: https://github.com/nodejs/node/pull/44135 Fixes: https://github.com/nodejs/node/issues/44036 Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Jan Krems <jan.krems@gmail.com>
* test: mark flaky tests on smartosFeng Yu2022-06-281-0/+5
| | | | | | | | | PR-URL: https://github.com/nodejs/node/pull/43596 Refs: https://github.com/nodejs/node/issues/43457 Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
* test: replace gc(true) with gc({ type: 'minor' })Tobias Nießen2022-06-211-1/+1
| | | | | | | | | | V8 considers gc(true) legacy, and the new signature is much more expressive. PR-URL: https://github.com/nodejs/node/pull/43493 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
* Revert "test: mark test_buffer/test_finalizer flaky"Matteo Collina2022-06-171-1/+0
| | | | | | | | | | | | This reverts commit 73d8db896ee9d016f6832295e242e79cfe464be8. PR-URL: https://github.com/nodejs/node/pull/43418 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
* test: make node-api/test_buffer/test_finalizer not flakyMatteo Collina2022-06-171-1/+5
| | | | | | | | | | PR-URL: https://github.com/nodejs/node/pull/43418 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
* test: mark test_buffer/test_finalizer flakyMichael Dawson2022-06-141-0/+1
| | | | | | | | | | | | | | | | Refs: https://github.com/nodejs/node/issues/43389 Mark new test as flaky as it seems to have had intermittent failures since it was added. Signed-off-by: Michael Dawson <mdawson@devrus.com> PR-URL: https://github.com/nodejs/node/pull/43414 Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
* node-api: emit uncaught-exception on unhandled tsfn callbackslegendecas2022-05-275-0/+136
| | | | | | | PR-URL: https://github.com/nodejs/node/pull/36510 Fixes: https://github.com/nodejs/node/issues/36402 Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: James M Snell <jasnell@gmail.com>
* node-api,src: fix module registration in MSVC C++Vladimir Morozov2022-04-073-0/+70
| | | | | | | PR-URL: https://github.com/nodejs/node/pull/42459 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
* src: return proper URLs from node_api_get_module_file_nameAnna Henningsen2022-03-071-7/+38
| | | | | | | | | | Using `file://${path}` does not properly escape special URL characters. PR-URL: https://github.com/nodejs/node/pull/41758 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
* test: enable no-empty ESLint ruleRich Trott2022-02-052-3/+4
| | | | | | | | PR-URL: https://github.com/nodejs/node/pull/41831 Refs: https://eslint.org/docs/rules/no-empty Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
* Revert "src: skip test_fatal/test_threads for Debug builds"Anna Henningsen2021-09-081-4/+0
| | | | | | | | | | | This reverts commit 1fc4d43a3255273709f5fcb51bb0c5b0407de4ac. PR-URL: https://github.com/nodejs/node/pull/39954 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
* report: generates report on threads with no isolateslegendecas2021-06-141-0/+36
| | | | | | | PR-URL: https://github.com/nodejs/node/pull/38994 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
* src: skip test_fatal/test_threads for Debug buildsDaniel Bevenius2021-06-081-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently test/node-api/test_fatal/test_threads.js fails for a Debug build with the following error: 1: 0x101e3f8 node::DumpBacktrace(_IO_FILE*) [/node/out/Debug/node] 2: 0x11c31ed [/node/out/Debug/node] 3: 0x11c320d [/node/out/Debug/node] 4: 0x2ba4448 V8_Fatal(char const*, int, char const*, ...) [/node/out/Debug/node] 5: 0x2ba4473 [/node/out/Debug/node] 6: 0x139e049 v8::internal::Isolate::Current() [/node/out/Debug/node] 7: 0x11025ee node::OnFatalError(char const*, char const*) [/node/out/Debug/node] 8: 0x1102564 node::FatalError(char const*, char const*) [/node/out/Debug/node] 9: 0x10add1d napi_open_callback_scope [/node/out/Debug/node] 10: 0x7f05664211dc [/node/test/node-api/test_fatal/build/Debug/test_fatal.node] 11: 0x7f056608e4e2 [/usr/lib64/libpthread.so.0] 12: 0x7f0565fbd6c3 clone [/usr/lib64/libc.so.6] node:assert:412 throw err; ^ AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value: assert.ok(p.status === 134 || p.signal === 'SIGABRT') at Object.<anonymous> (/node/test/node-api/test_fatal/test_threads.js:21:8) at Module._compile (node:internal/modules/cjs/loader:1109:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10) at Module.load (node:internal/modules/cjs/loader:989:32) at Function.Module._load (node:internal/modules/cjs/loader:829:14) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12) at node:internal/main/run_main_module:17:47 { generatedMessage: true, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==' } This is caused by a call to Isolate::GetCurrent() when the calling thread has not initialized V8. We are working suggestion to add a method to V8 which allows a check/get without any checks but in the mean time this change should allow debug builds to pass the test suit. PR-URL: https://github.com/nodejs/node/pull/38805 Refs: https://chromium-review.googlesource.com/c/v8/v8/+/2910630 Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: James M Snell <jasnell@gmail.com>
* src: fix fatal errors when a current isolate not existlegendecas2021-05-184-0/+43
| | | | | | | | napi_fatal_error and node watchdog trigger fatal error but rather running on a thread that hold no current isolate. PR-URL: https://github.com/nodejs/node/pull/38624 Reviewed-By: Michael Dawson <midawson@redhat.com>
* node-api: faster threadsafe_functionFedor Indutny2021-05-042-2/+11
| | | | | | | | | | | | | | Invoke threadsafe_function during the same tick and avoid marshalling costs between threads and/or churning event loop if either: 1. There's a queued call already 2. `Push()` is called while the main thread was running threadsafe_function PR-URL: https://github.com/nodejs/node/pull/38506 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
* test: fix typo in comment in binding.cTobias Nießen2021-04-131-1/+1
| | | | | | | | | | | PR-URL: https://github.com/nodejs/node/pull/38220 Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Beth Griggs <bgriggs@redhat.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
* test: prepare for consistent comma-dangle lint ruleRich Trott2021-04-012-2/+2
| | | | | | | | | Make changes so that tests will pass when the comma-dangle settings applied to the rest of the code base are also applied to tests. PR-URL: https://github.com/nodejs/node/pull/37930 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Derek Lewis <DerekNonGeneric@inf.is>
* node-api: stop ref gc during environment teardownGabriel Schulhof2021-03-183-0/+59
| | | | | | | | | | | 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 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
* node-api: define version 8Gabriel Schulhof2021-03-101-1/+0
| | | | | | | | | Mark as stable the APIs that define Node-API version 8. PR-URL: https://github.com/nodejs/node/pull/37652 Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
* node-api: allow retrieval of add-on file nameGabriel Schulhof2021-02-092-1/+19
| | | | | | | | | | | | | | | | | | | | | | 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 Co-authored-by: Michael Dawson <mdawson@devrus.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
* test: rename n-api to node-apiGabriel Schulhof2021-02-0618-392/+352
| | | | | | | | | | This renames the macros used in the tests from `NAPI_*` to `NODE_API_*`. PR-URL: https://github.com/nodejs/node/pull/37217 Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Beth Griggs <bgriggs@redhat.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
* test: remove outdated V8 flagMichaël Zasso2021-02-021-1/+1
| | | | | | | | | | | | | | | | The flag is going to be removed upstream in V8 9.0. Refs: https://github.com/v8/v8/commit/0a480c30d553c1b8dddb0dddcbdec5fb6f3101ff PR-URL: https://github.com/nodejs/node/pull/37151 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
* test,doc,lib: adjust object literal newlines for lint ruleRich Trott2021-01-261-1/+2
| | | | | | | | | | | Before enabling object-curly-newline for our ESLint rules, adjust files to comply with it. Refs: https://eslint.org/docs/rules/object-curly-newline PR-URL: https://github.com/nodejs/node/pull/37040 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
* n-api: fix test_async_context warningsGabriel Schulhof2020-11-201-2/+3
| | | | | | | | | | Signed-off-by: Gabriel Schulhof <gabriel.schulhof@intel.com> PR-URL: https://github.com/nodejs/node/pull/36171 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
* n-api: unlink reference during its destructorGabriel Schulhof2020-11-041-2/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently, a reference is being unlinked from the list of references tracked by the environment when `v8impl::Reference::Delete` is called. This causes a leak when deletion must be deferred because the finalizer hasn't yet run, but the finalizer does not run because environment teardown is in progress, and so no more gc runs will happen, and the `FinalizeAll` run that happens during environment teardown does not catch the reference because it's no longer in the list. The test below will fail when running with ASAN: ``` ./node ./test/node-api/test_worker_terminate_finalization/test.js ``` OTOH if, to address the above leak, we make a special case to not unlink a reference during environment teardown, we run into a situation where the reference gets deleted by `v8impl::Reference::Delete` but does not get unlinked because it's environment teardown time. This leaves a stale pointer in the linked list which will result in a use-after-free in `FinalizeAll` during environment teardown. The test below will fail if we make the above change: ``` ./node -e "require('./test/node-api/test_instance_data/build/Release/test_ref_then_set.node');" ``` Thus, we unlink a reference precisely when we destroy it – in its destructor. Refs: https://github.com/nodejs/node/issues/34731 Refs: https://github.com/nodejs/node/pull/34839 Refs: https://github.com/nodejs/node/issues/35620 Refs: https://github.com/nodejs/node/issues/35777 Fixes: https://github.com/nodejs/node/issues/35778 Signed-off-by: Gabriel Schulhof <gabriel.schulhof@intel.com> PR-URL: https://github.com/nodejs/node/pull/35933 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Zeyu Yang <himself65@outlook.com>
* n-api: napi_make_callback emit async init with resource of async_contextlegendecas2020-10-318-44/+296
| | | | | | | | | | instead of emit async init with receiver of the callback. PR-URL: https://github.com/nodejs/node/pull/32930 Fixes: https://github.com/nodejs/node/issues/32898 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
* n-api: revert change to finalizationMichael Dawson2020-10-271-0/+6
| | | | | | | | | | | | | | | Fixes: https://github.com/nodejs/node/issues/35620 This reverts commit a6b655614f03e073b9c60f3d71ed884c5af32ffc 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>
* test: adjust comments for upcoming lint ruleRich Trott2020-10-071-21/+21
| | | | | | | | | Enforce `//` for multiline comments. Some tests mixed and matched, and at least one did so in a (to me) surprising way. PR-URL: https://github.com/nodejs/node/pull/35485 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
* n-api: re-implement async env cleanup hooksGabriel Schulhof2020-08-271-21/+10
| | | | | | | | | | | | | | * 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>
* n-api: handle weak no-finalizer refs correctlyGabriel Schulhof2020-08-211-4/+0
| | | | | | | | | | | | | | 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>
* test: skip node-api/test_worker_terminate_finalizationAnna Henningsen2020-08-111-0/+5
| | | | | | | | | | | 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>
* test: fix test_worker_terminate_finalizationAnna Henningsen2020-08-111-1/+0
| | | | | | | | | | | 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>
* n-api,src: provide asynchronous cleanup hooksAnna Henningsen2020-08-073-0/+99
| | | | | | | | | | | | | | | 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>
* test: convert most N-API tests from C++ to CGabriel Schulhof2020-08-058-77/+67
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Prefix functions with `static` to make them local * Remove anonymous namespaces * `nullptr` -> `NULL` * .cc -> .c and update binding.gyp * `static_cast<x>()` -> `(x)()` * Replace `new`/`delete` with `malloc()`/`free()` (only in test_callback_scope) * Move lambda out and convert to local function (only in test_callback_scope) * Remove superfluous `#include <vector>` (only in test_callback_scope_recurse) Some tests are best left as C++. ```bash ls -l test/{node-api,js-native-api}/*/*.cc ``` for those remaining as C++ tests. Signed-off-by: Gabriel Schulhof <gabriel.schulhof@intel.com> PR-URL: https://github.com/nodejs/node/pull/34615 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
* n-api: ensure scope present for finalizationMichael Dawson2020-06-093-0/+74
| | | | | | | | | | | | | 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>
* module: deprecate module.parentAntoine du HAMEL2020-05-241-1/+1
| | | | | | | | | | | | | | | | This feature does not work when a module is imported using ECMAScript modules specification, therefore it is deprecated. Fixes: https://github.com/nodejs/modules/issues/469 PR-URL: https://github.com/nodejs/node/pull/32217 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
* Revert "n-api: detect deadlocks in thread-safe function"Anna Henningsen2020-05-233-67/+4
| | | | | | | | | | | | | | | | | | This reverts commit d26ca06c16f497ffa5ac4845a27922d5058a9318 because it breaks running the tests in debug mode, as `v8::Isolate::GetCurrent()` is not allowed if no `Isolate` is active on the current thread. Refs: https://github.com/nodejs/node/pull/33276 Refs: https://github.com/nodejs/node/pull/32860 PR-URL: https://github.com/nodejs/node/pull/33453 Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
* buffer,n-api: release external buffers from BackingStore callbackAnna Henningsen2020-05-161-4/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Release `Buffer` and `ArrayBuffer` instances that were created through our addon APIs and have finalizers attached to them only after V8 has called the deleter callback passed to the `BackingStore`, instead of relying on our own GC callback(s). This fixes the following race condition: 1. Addon code allocates pointer P via `malloc`. 2. P is passed into `napi_create_external_buffer` with a finalization callback which calls `free(P)`. P is inserted into V8’s global array buffer table for tracking. 3. The finalization callback is executed on GC. P is freed and returned to the allocator. P is not yet removed from V8’s global array buffer table. (!) 4. Addon code attempts to allocate memory once again. The allocator returns P, as it is now available. 5. P is passed into `napi_create_external_buffer`. P still has not been removed from the v8 global array buffer table. 6. The world ends with `Check failed: result.second`. Since our API contract is to call the finalizer on the JS thread on which the `ArrayBuffer` was created, but V8 may call the `BackingStore` deleter callback on another thread, fixing this requires posting a task back to the JS thread. Refs: https://github.com/nodejs/node/issues/32463#issuecomment-625877175 Fixes: https://github.com/nodejs/node/issues/32463 PR-URL: https://github.com/nodejs/node/pull/33321 Reviewed-By: James M Snell <jasnell@gmail.com>
* test: fix out-of-bound reads from invalid sizeof usageAnna Henningsen2020-04-301-1/+1
| | | | | | | | | `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>
* test: add missing calls to napi_async_destroyAnna Henningsen2020-04-301-0/+2
| | | | | | | | 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>
* n-api: fix false assumption on napi_async_context structureslegendecas2020-04-283-28/+23
| | | | | | | | | 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>
* n-api: detect deadlocks in thread-safe functionGabriel Schulhof2020-04-193-4/+67
| | | | | | | | | | | | | We introduce status `napi_would_deadlock` to be used as a return status by `napi_call_threadsafe_function` if the call is made with `napi_tsfn_blocking` on the main thread and the queue is full. Fixes: https://github.com/nodejs/node/issues/32615 Signed-off-by: Gabriel Schulhof <gabriel.schulhof@intel.com> PR-URL: https://github.com/nodejs/node/pull/32860 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Zeyu Yang <himself65@outlook.com>