From 146f554634a117a48fb9c0815ffdc418955a9063 Mon Sep 17 00:00:00 2001 From: Jason Ginchereau Date: Mon, 20 Mar 2017 14:55:26 -0700 Subject: [PATCH 001/183] n-api: add support for abi stable module API Add support for abi stable module API (N-API) as "Experimental feature". The goal of this API is to provide a stable Node API for native module developers. N-API aims to provide ABI compatibility guarantees across different Node versions and also across different Node VMs - allowing N-API enabled native modules to just work across different versions and flavors of Node.js without recompilation. A more detailed introduction is provided in: https://github.com/nodejs/node-eps/blob/master/005-ABI-Stable-Module-API.md and https://github.com/nodejs/abi-stable-node/blob/doc/VM%20Summit.pdf. The feature, during its experimental state, will be guarded by a runtime flag "--napi-modules". Only when this flag is added to the command line will N-API modules along with regular non N-API modules be supported. The API is defined by the methods in "src/node_api.h" and "src/node_api_types.h". This is the best starting point to review the API surface. More documentation will follow. In addition to the implementation of the API using V8, which is included in this PR, the API has also been validated against chakracore and that port is available in https://github.com/nodejs/abi-stable-node/tree/api-prototype-chakracore-8.x. The current plan is to provide N-API support in versions 8.X and 6.X directly. For older versions, such as 4.X or pre N-API versions of 6.X, we plan to create an external npm module to provide a migration path that will allow modules targeting older Node.js versions to use the API, albeit without getting the advantage of not having to recompile. In addition, we also plan an external npm package with C++ sugar to simplify the use of the API. The sugar will be in-line only and will only use the exported N-API methods but is not part of the N-API itself. The current version is in: https://github.com/nodejs/node-api. This PR is a result of work in the abi-stable-node repo: https://github.com/nodejs/abi-stable-node/tree/doc, with this PR being the cumulative work on the api-prototype-8.x branch with the following contributors in alphabetical order: Author: Arunesh Chandra Author: Gabriel Schulhof Author: Hitesh Kanwathirtha Author: Ian Halliday Author: Jason Ginchereau Author: Michael Dawson Author: Sampson Gao Author: Taylor Woll PR-URL: https://github.com/nodejs/node/pull/11975 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- Makefile | 60 +- doc/api/cli.md | 8 + doc/node.1 | 5 + node.gyp | 3 + src/node.cc | 29 +- src/node_api.cc | 2529 +++++++++++++++++ src/node_api.h | 479 ++++ src/node_api_backport.h | 10 + src/node_api_types.h | 95 + test/addons-napi/.gitignore | 7 + test/addons-napi/1_hello_world/binding.c | 22 + test/addons-napi/1_hello_world/binding.gyp | 8 + test/addons-napi/1_hello_world/test.js | 6 + .../2_function_arguments/binding.c | 58 + .../2_function_arguments/binding.gyp | 8 + test/addons-napi/2_function_arguments/test.js | 6 + test/addons-napi/3_callbacks/binding.c | 51 + test/addons-napi/3_callbacks/binding.gyp | 8 + test/addons-napi/3_callbacks/test.js | 22 + test/addons-napi/4_object_factory/binding.c | 31 + test/addons-napi/4_object_factory/binding.gyp | 8 + test/addons-napi/4_object_factory/test.js | 8 + test/addons-napi/5_function_factory/binding.c | 36 + .../5_function_factory/binding.gyp | 8 + test/addons-napi/5_function_factory/test.js | 7 + test/addons-napi/6_object_wrap/binding.cc | 7 + test/addons-napi/6_object_wrap/binding.gyp | 8 + test/addons-napi/6_object_wrap/myobject.cc | 201 ++ test/addons-napi/6_object_wrap/myobject.h | 26 + test/addons-napi/6_object_wrap/test.js | 19 + test/addons-napi/7_factory_wrap/binding.cc | 32 + test/addons-napi/7_factory_wrap/binding.gyp | 8 + test/addons-napi/7_factory_wrap/myobject.cc | 110 + test/addons-napi/7_factory_wrap/myobject.h | 26 + test/addons-napi/7_factory_wrap/test.js | 14 + test/addons-napi/8_passing_wrapped/binding.cc | 57 + .../addons-napi/8_passing_wrapped/binding.gyp | 8 + .../addons-napi/8_passing_wrapped/myobject.cc | 81 + test/addons-napi/8_passing_wrapped/myobject.h | 26 + test/addons-napi/8_passing_wrapped/test.js | 9 + test/addons-napi/test_array/binding.gyp | 8 + test/addons-napi/test_array/test.js | 37 + test/addons-napi/test_array/test_array.c | 137 + test/addons-napi/test_buffer/binding.gyp | 8 + test/addons-napi/test_buffer/test.js | 25 + test/addons-napi/test_buffer/test_buffer.c | 160 ++ test/addons-napi/test_constructor/binding.gyp | 8 + test/addons-napi/test_constructor/test.js | 28 + .../test_constructor/test_constructor.c | 104 + test/addons-napi/test_conversions/binding.gyp | 8 + test/addons-napi/test_conversions/test.js | 140 + .../test_conversions/test_conversions.c | 237 ++ test/addons-napi/test_error/binding.gyp | 8 + test/addons-napi/test_error/test.js | 57 + test/addons-napi/test_error/test_error.cc | 37 + test/addons-napi/test_exception/binding.gyp | 8 + test/addons-napi/test_exception/test.js | 53 + .../test_exception/test_exception.c | 75 + test/addons-napi/test_function/binding.gyp | 8 + test/addons-napi/test_function/test.js | 28 + .../addons-napi/test_function/test_function.c | 55 + test/addons-napi/test_instanceof/binding.gyp | 8 + test/addons-napi/test_instanceof/test.js | 87 + .../test_instanceof/test_instanceof.c | 39 + test/addons-napi/test_number/binding.gyp | 8 + test/addons-napi/test_number/test.js | 39 + test/addons-napi/test_number/test_number.c | 55 + test/addons-napi/test_object/binding.gyp | 8 + test/addons-napi/test_object/test.js | 65 + test/addons-napi/test_object/test_object.c | 246 ++ test/addons-napi/test_properties/binding.gyp | 8 + test/addons-napi/test_properties/test.js | 27 + .../test_properties/test_properties.c | 85 + test/addons-napi/test_string/binding.gyp | 8 + test/addons-napi/test_string/test.js | 26 + test/addons-napi/test_string/test_string.c | 134 + test/addons-napi/test_symbol/binding.gyp | 8 + test/addons-napi/test_symbol/test1.js | 20 + test/addons-napi/test_symbol/test2.js | 15 + test/addons-napi/test_symbol/test3.js | 19 + test/addons-napi/test_symbol/test_symbol.c | 94 + test/addons-napi/test_typedarray/binding.gyp | 8 + test/addons-napi/test_typedarray/test.js | 39 + .../test_typedarray/test_typedarray.c | 144 + test/addons-napi/testcfg.py | 6 + test/testpy/__init__.py | 2 +- tools/install.py | 2 + tools/test.py | 1 + vcbuild.bat | 30 +- 89 files changed, 6586 insertions(+), 18 deletions(-) create mode 100644 src/node_api.cc create mode 100644 src/node_api.h create mode 100644 src/node_api_backport.h create mode 100644 src/node_api_types.h create mode 100644 test/addons-napi/.gitignore create mode 100644 test/addons-napi/1_hello_world/binding.c create mode 100644 test/addons-napi/1_hello_world/binding.gyp create mode 100644 test/addons-napi/1_hello_world/test.js create mode 100644 test/addons-napi/2_function_arguments/binding.c create mode 100644 test/addons-napi/2_function_arguments/binding.gyp create mode 100644 test/addons-napi/2_function_arguments/test.js create mode 100644 test/addons-napi/3_callbacks/binding.c create mode 100644 test/addons-napi/3_callbacks/binding.gyp create mode 100644 test/addons-napi/3_callbacks/test.js create mode 100644 test/addons-napi/4_object_factory/binding.c create mode 100644 test/addons-napi/4_object_factory/binding.gyp create mode 100644 test/addons-napi/4_object_factory/test.js create mode 100644 test/addons-napi/5_function_factory/binding.c create mode 100644 test/addons-napi/5_function_factory/binding.gyp create mode 100644 test/addons-napi/5_function_factory/test.js create mode 100644 test/addons-napi/6_object_wrap/binding.cc create mode 100644 test/addons-napi/6_object_wrap/binding.gyp create mode 100644 test/addons-napi/6_object_wrap/myobject.cc create mode 100644 test/addons-napi/6_object_wrap/myobject.h create mode 100644 test/addons-napi/6_object_wrap/test.js create mode 100644 test/addons-napi/7_factory_wrap/binding.cc create mode 100644 test/addons-napi/7_factory_wrap/binding.gyp create mode 100644 test/addons-napi/7_factory_wrap/myobject.cc create mode 100644 test/addons-napi/7_factory_wrap/myobject.h create mode 100644 test/addons-napi/7_factory_wrap/test.js create mode 100644 test/addons-napi/8_passing_wrapped/binding.cc create mode 100644 test/addons-napi/8_passing_wrapped/binding.gyp create mode 100644 test/addons-napi/8_passing_wrapped/myobject.cc create mode 100644 test/addons-napi/8_passing_wrapped/myobject.h create mode 100644 test/addons-napi/8_passing_wrapped/test.js create mode 100644 test/addons-napi/test_array/binding.gyp create mode 100644 test/addons-napi/test_array/test.js create mode 100644 test/addons-napi/test_array/test_array.c create mode 100644 test/addons-napi/test_buffer/binding.gyp create mode 100644 test/addons-napi/test_buffer/test.js create mode 100644 test/addons-napi/test_buffer/test_buffer.c create mode 100644 test/addons-napi/test_constructor/binding.gyp create mode 100644 test/addons-napi/test_constructor/test.js create mode 100644 test/addons-napi/test_constructor/test_constructor.c create mode 100644 test/addons-napi/test_conversions/binding.gyp create mode 100644 test/addons-napi/test_conversions/test.js create mode 100644 test/addons-napi/test_conversions/test_conversions.c create mode 100644 test/addons-napi/test_error/binding.gyp create mode 100644 test/addons-napi/test_error/test.js create mode 100644 test/addons-napi/test_error/test_error.cc create mode 100644 test/addons-napi/test_exception/binding.gyp create mode 100644 test/addons-napi/test_exception/test.js create mode 100644 test/addons-napi/test_exception/test_exception.c create mode 100644 test/addons-napi/test_function/binding.gyp create mode 100644 test/addons-napi/test_function/test.js create mode 100644 test/addons-napi/test_function/test_function.c create mode 100644 test/addons-napi/test_instanceof/binding.gyp create mode 100644 test/addons-napi/test_instanceof/test.js create mode 100644 test/addons-napi/test_instanceof/test_instanceof.c create mode 100644 test/addons-napi/test_number/binding.gyp create mode 100644 test/addons-napi/test_number/test.js create mode 100644 test/addons-napi/test_number/test_number.c create mode 100644 test/addons-napi/test_object/binding.gyp create mode 100644 test/addons-napi/test_object/test.js create mode 100644 test/addons-napi/test_object/test_object.c create mode 100644 test/addons-napi/test_properties/binding.gyp create mode 100644 test/addons-napi/test_properties/test.js create mode 100644 test/addons-napi/test_properties/test_properties.c create mode 100644 test/addons-napi/test_string/binding.gyp create mode 100644 test/addons-napi/test_string/test.js create mode 100644 test/addons-napi/test_string/test_string.c create mode 100644 test/addons-napi/test_symbol/binding.gyp create mode 100644 test/addons-napi/test_symbol/test1.js create mode 100644 test/addons-napi/test_symbol/test2.js create mode 100644 test/addons-napi/test_symbol/test3.js create mode 100644 test/addons-napi/test_symbol/test_symbol.c create mode 100644 test/addons-napi/test_typedarray/binding.gyp create mode 100644 test/addons-napi/test_typedarray/test.js create mode 100644 test/addons-napi/test_typedarray/test_typedarray.c create mode 100644 test/addons-napi/testcfg.py diff --git a/Makefile b/Makefile index c947a2a4367d05..523a3d8c541ed8 100644 --- a/Makefile +++ b/Makefile @@ -120,9 +120,10 @@ v8: test: all $(MAKE) build-addons + $(MAKE) build-addons-napi $(MAKE) cctest $(PYTHON) tools/test.py --mode=release -J \ - doctool inspector known_issues message pseudo-tty parallel sequential addons + doctool inspector known_issues message pseudo-tty parallel sequential addons addons-napi $(MAKE) lint test-parallel: all @@ -189,6 +190,41 @@ test/addons/.buildstamp: config.gypi \ # TODO(bnoordhuis) Force rebuild after gyp update. build-addons: $(NODE_EXE) test/addons/.buildstamp +ADDONS_NAPI_BINDING_GYPS := \ + $(filter-out test/addons-napi/??_*/binding.gyp, \ + $(wildcard test/addons-napi/*/binding.gyp)) + +ADDONS_NAPI_BINDING_SOURCES := \ + $(filter-out test/addons-napi/??_*/*.cc, $(wildcard test/addons-napi/*/*.cc)) \ + $(filter-out test/addons-napi/??_*/*.h, $(wildcard test/addons-napi/*/*.h)) + +# Implicitly depends on $(NODE_EXE), see the build-addons-napi rule for rationale. +test/addons-napi/.buildstamp: config.gypi \ + deps/npm/node_modules/node-gyp/package.json \ + $(ADDONS_NAPI_BINDING_GYPS) $(ADDONS_NAPI_BINDING_SOURCES) \ + deps/uv/include/*.h deps/v8/include/*.h \ + src/node.h src/node_buffer.h src/node_object_wrap.h src/node_version.h \ + src/node_api.h src/node_api_types.h +# Cannot use $(wildcard test/addons-napi/*/) here, it's evaluated before +# embedded addons have been generated from the documentation. + @for dirname in test/addons-napi/*/; do \ + printf "\nBuilding addon $$PWD/$$dirname\n" ; \ + env MAKEFLAGS="-j1" $(NODE) deps/npm/node_modules/node-gyp/bin/node-gyp \ + --loglevel=$(LOGLEVEL) rebuild \ + --python="$(PYTHON)" \ + --directory="$$PWD/$$dirname" \ + --nodedir="$$PWD" || exit 1 ; \ + done + touch $@ + +# .buildstamp and .docbuildstamp need $(NODE_EXE) but cannot depend on it +# directly because it calls make recursively. The parent make cannot know +# if the subprocess touched anything so it pessimistically assumes that +# .buildstamp and .docbuildstamp are out of date and need a rebuild. +# Just goes to show that recursive make really is harmful... +# TODO(bnoordhuis) Force rebuild after gyp or node-gyp update. +build-addons-napi: $(NODE_EXE) test/addons-napi/.buildstamp + clear-stalled: # Clean up any leftover processes but don't error if found. ps awwx | grep Release/node | grep -v grep | cat @@ -200,7 +236,9 @@ clear-stalled: test-gc: all test/gc/node_modules/weak/build/Release/weakref.node $(PYTHON) tools/test.py --mode=release gc -test-build: | all build-addons +test-build: | all build-addons build-addons-napi + +test-build-addons-napi: all build-addons-napi test-all: test-build test/gc/node_modules/weak/build/Release/weakref.node $(PYTHON) tools/test.py --mode=debug,release @@ -208,12 +246,12 @@ test-all: test-build test/gc/node_modules/weak/build/Release/weakref.node test-all-valgrind: test-build $(PYTHON) tools/test.py --mode=debug,release --valgrind -CI_NATIVE_SUITES := addons +CI_NATIVE_SUITES := addons addons-napi CI_JS_SUITES := doctool inspector known_issues message parallel pseudo-tty sequential # Build and test addons without building anything else test-ci-native: LOGLEVEL := info -test-ci-native: | test/addons/.buildstamp +test-ci-native: | test/addons/.buildstamp test/addons-napi/.buildstamp $(PYTHON) tools/test.py $(PARALLEL_ARGS) -p tap --logfile test.tap \ --mode=release --flaky-tests=$(FLAKY_TESTS) \ $(TEST_CI_ARGS) $(CI_NATIVE_SUITES) @@ -231,11 +269,11 @@ test-ci-js: | clear-stalled fi test-ci: LOGLEVEL := info -test-ci: | clear-stalled build-addons +test-ci: | clear-stalled build-addons build-addons-napi out/Release/cctest --gtest_output=tap:cctest.tap $(PYTHON) tools/test.py $(PARALLEL_ARGS) -p tap --logfile test.tap \ --mode=release --flaky-tests=$(FLAKY_TESTS) \ - $(TEST_CI_ARGS) $(CI_JS_SUITES) $(CI_NATIVE_SUITES) + $(TEST_CI_ARGS) $(CI_JS_SUITES) addons-napi $(CI_NATIVE_SUITES) # Clean up any leftover processes, error if found. ps awwx | grep Release/node | grep -v grep | cat @PS_OUT=`ps awwx | grep Release/node | grep -v grep | awk '{print $$1}'`; \ @@ -282,7 +320,10 @@ test-npm: $(NODE_EXE) test-npm-publish: $(NODE_EXE) npm_package_config_publishtest=true $(NODE) deps/npm/test/run.js -test-addons: test-build +test-addons-napi: test-build-addons-napi + $(PYTHON) tools/test.py --mode=release addons-napi + +test-addons: test-build test-addons-napi $(PYTHON) tools/test.py --mode=release addons test-addons-clean: @@ -813,6 +854,8 @@ LINT_CPP_FILES = $(filter-out $(LINT_CPP_EXCLUDE), $(wildcard \ test/addons/*/*.h \ test/cctest/*.cc \ test/cctest/*.h \ + test/addons-napi/*/*.cc \ + test/addons-napi/*/*.h \ tools/icu/*.cc \ tools/icu/*.h \ )) @@ -857,5 +900,6 @@ endif bench-buffer bench-net bench-http bench-fs bench-tls cctest run-ci \ test-v8 test-v8-intl test-v8-benchmarks test-v8-all v8 lint-ci \ bench-ci lint-js-ci doc-only $(TARBALL)-headers test-ci test-ci-native \ - test-ci-js build-ci test-hash-seed clear-stalled + test-ci-js build-ci test-hash-seed clear-stalled test-addons-napi \ + build-addons-napi diff --git a/doc/api/cli.md b/doc/api/cli.md index f1b33e883ae883..5bcb6f3b4d0107 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -119,6 +119,14 @@ added: v6.0.0 Silence all process warnings (including deprecations). +### `--napi-modules` + + +Enable loading native modules compiled with the ABI-stable Node.js API (N-API) +(experimental). + ### `--trace-warnings` +```C +NAPI_EXTERN napi_status +napi_get_last_error_info(napi_env env, + const napi_extended_error_info** result); +``` +- `[in] env`: The environment that the API is invoked under. +- `[out] result`: The `napi_extended_error_info` structure with more +information about the error. + +Returns `napi_ok` if the API succeeded. + +This API retrieves a `napi_extended_error_info` structure with information +about the last error that occured. + +**Note:** Do not rely on the content or format of any of the extended +information as it is not subject to SemVer and may change at any time. +It is intended only for logging purposes. + + +### Exceptions +Any N-API function call may result in a pending JavaScript exception. This is +obviously the case for any function that may cause the execution of +JavaScript, but N-API specifies that an exception may be pending +on return from any of the API functions. + +If the `napi_status` returned by a function is `napi_ok` then no +exception is pending and no additional action is required. If the +`napi_status` returned is anything other than `napi_ok` or +`napi_pending_exception`, in order to try to recover and continue +instead of simply returning immediately, [`napi_is_exception_pending`][] +must be called in order to determine if an exception is pending or not. + +When an exception is pending one of two approaches can be employed. + +The first appoach is to do any appropriate cleanup and then return so that +execution will return to JavaScript. As part of the transition back to +JavaScript the exception will be thrown at the point in the JavaScript +code where the native method was invoked. The behavior of most N-API calls +is unspecified while an exception is pending, and many will simply return +`napi_pending_exception`, so it is important to do as little as possible +and then return to JavaScript where the exception can be handled. + +The second approach is to try to handle the exception. There will be cases +where the native code can catch the exception, take the appropriate action, +and then continue. This is only recommended in specific cases +where it is known that the exception can be safely handled. In these +cases [`napi_get_and_clear_last_exception`][] can be used to get and +clear the exception. On success, result will contain the handle to +the last JavaScript Object thrown. If it is determined, after +retrieving the exception, the exception cannot be handled after all +it can be re-thrown it with [`napi_throw`][] where error is the +JavaScript Error object to be thrown. + +The following utility functions are also available in case native code +needs to throw an exception or determine if a `napi_value` is an instance +of a JavaScript `Error` object: [`napi_throw_error`][], +[`napi_throw_type_error`][], [`napi_throw_range_error`][] and +[`napi_is_error`][]. + +The following utility functions are also available in case native +code needs to create an Error object: [`napi_create_error`][], +[`napi_create_type_error`][], and [`napi_create_range_error`][]. +where result is the napi_value that refers to the newly created +JavaScript Error object. + +#### napi_throw + +```C +NODE_EXTERN napi_status napi_throw(napi_env env, napi_value error); +``` +- `[in] env`: The environment that the API is invoked under. +- `[in] error`: The `napi_value` for the Error to be thrown. + +Returns `napi_ok` if the API succeeded. + +This API throws the JavaScript Error provided. + + +#### napi_throw_error + +```C +NODE_EXTERN napi_status napi_throw_error(napi_env env, const char* msg); +``` +- `[in] env`: The environment that the API is invoked under. +- `[in] msg`: C string representing the text to be associated with +the error. + +Returns `napi_ok` if the API succeeded. + +This API throws a JavaScript Error with the text provided. + +#### napi_throw_type_error + +```C +NODE_EXTERN napi_status napi_throw_type_error(napi_env env, const char* msg); +``` +- `[in] env`: The environment that the API is invoked under. +- `[in] msg`: C string representing the text to be associated with +the error. + +Returns `napi_ok` if the API succeeded. + +This API throws a JavaScript TypeError with the text provided. + +#### napi_throw_range_error + +```C +NODE_EXTERN napi_status napi_throw_range_error(napi_env env, const char* msg); +``` +- `[in] env`: The environment that the API is invoked under. +- `[in] msg`: C string representing the text to be associated with +the error. + +Returns `napi_ok` if the API succeeded. + +This API throws a JavaScript RangeError with the text provided. + + +#### napi_is_error + +```C +NODE_EXTERN napi_status napi_is_error(napi_env env, + napi_value value, + bool* result); +``` +- `[in] env`: The environment that the API is invoked under. +- `[in] msg`: The `napi_value` to be checked. +- `[out] result`: Boolean value that is set to true if `napi_value` represents +an error, false otherwise. + +Returns `napi_ok` if the API succeeded. + +This API queries a `napi_value` to check if it represents an error object. + + +#### napi_create_error + +```C +NODE_EXTERN napi_status napi_create_error(napi_env env, const char* msg); +``` +- `[in] env`: The environment that the API is invoked under. +- `[in] msg`: C string representing the text to be associated with. +- `[out] result`: `napi_value` representing the error created. + +Returns `napi_ok` if the API succeeded. + +This API returns a JavaScript Error with the text provided. + +#### napi_create_type_error + +```C +NODE_EXTERN napi_status napi_create_type_error(napi_env env, const char* msg); +``` +- `[in] env`: The environment that the API is invoked under. +- `[in] msg`: C string representing the text to be associated with. +- `[out] result`: `napi_value` representing the error created. + +Returns `napi_ok` if the API succeeded. + +This API returns a JavaScript TypeError with the text provided. + + +#### napi_create_range_error + +```C +NODE_EXTERN napi_status napi_create_range_error(napi_env env, const char* msg); +``` +- `[in] env`: The environment that the API is invoked under. +- `[in] msg`: C string representing the text to be associated with. +- `[out] result`: `napi_value` representing the error created. + +Returns `napi_ok` if the API succeeded. + +This API returns a JavaScript RangeError with the text provided. + + +#### napi_get_and_clear_last_exception + +```C +NAPI_EXTERN napi_status napi_get_and_clear_last_exception(napi_env env, + napi_value* result); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[out] result`: The exception if one is pending, NULL otherwise. + +Returns `napi_ok` if the API succeeded. + +This API returns true if an exception is pending. + +#### napi_is_exception_pending + +```C +NAPI_EXTERN napi_status napi_is_exception_pending(napi_env env, bool* result); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[out] result`: Boolean value that is set to true if an exception is pending. + +Returns `napi_ok` if the API succeeded. + +This API returns true if an exception is pending. + + +## Object Lifetime management + +As N-API calls are made, handles to objects in the heap for the underlying +VM may be returned as `napi_values`. These handles must hold the +objects 'live' until they are no longer required by the native code, +otherwise the objects could be collected before the native code was +finished using them. + +As object handles are returned they are associated with a +'scope'. The lifespan for the default scope is tied to the lifespan +of the native method call. The result is that, by default, handles +remain valid and the objects associated with these handles will be +held live for the lifespan of the native method call. + +In many cases, however, it is necessary that the handles remain valid for +either a shorter or longer lifespan than that of the native method. +The sections which follow describe the N-API functions than can be used +to change the handle lifespan from the default. + +### Making handle lifespan shorter than that of the native method +It is often necessary to make the lifespan of handles shorter than +the lifespan of a native method. For example, consider a native method +that has a loop which iterates through the elements in a large array: + +```C +for (int i = 0; i < 1000000; i++) { + napi_value result; + napi_status status = napi_get_element(e object, i, &result); + if (status != napi_ok) { + break; + } + // do something with element +} +``` + +This would result in a large number of handles being created, consuming +substantial resources. In addition, even though the native code could only +use the most recent handle, all of the associated objects would also be +kept alive since they all share the same scope. + +To handle this case, N-API provides the ability to establish a new 'scope' to +which newly created handles will be associated. Once those handles +are no longer required, the scope can be 'closed' and any handles associated +with the scope are invalidated. The methods available to open/close scopes are +[`napi_open_handle_scope`][] and [`napi_close_handle_scope`][]. + +N-API only supports a single nested hiearchy of scopes. There is only one +active scope at any time, and all new handles will be associated with that +scope while it is active. Scopes must be closed in the reverse order from +which they are opened. In addition, all scopes created within a native method +must be closed before returning from that method. + +Taking the earlier example, adding calls to [`napi_open_handle_scope`][] and +[`napi_close_handle_scope`][] would ensure that at most a single handle +is valid throughout the execution of the loop: + +```C +for (int i = 0; i < 1000000; i++) {napi_ + napi_handle_scope scope; + napi_status status = napi_open_handle_scope(env, &scope); + if (status != napi_ok) { + break; + } + napi_value result; + status = napi_get_element(e object, i, &result); + if (status != napi_ok) { + break; + } + // do something with element + status = napi_close_handle_scope(env, scope); + if (status != napi_ok) { + break; + } +} +``` + +When nesting scopes, there are cases where a handle from an +inner scope needs to live beyond the lifespan of that scope. N-API supports an +'escapable scope' in order to support this case. An escapable scope +allows one or more handles to be 'promoted' so that they 'escape' the +current scope and the lifespan of the handle(s) changes from the current +scope to that of the outer scope. + +The methods available to open/close escapable scopes are +[`napi_open_escapable_handle_scope`][] and [`napi_close_escapable_handle_scope`][]. + +The request to promote a handle is made through the [`napi_escape_handle`][]. + +#### napi_open_handle_scope + +```C +NODE_EXTERN napi_status napi_open_handle_scope(napi_env env, + napi_handle_scope* result); +``` +- `[in] env`: The environment that the API is invoked under. +- `[out] result`: `napi_value` representing the new scope. + +Returns `napi_ok` if the API succeeded. + +This API open a new scope. + +#### napi_close_handle_scope + +```C +NODE_EXTERN napi_status napi_close_handle_scope(napi_env env, + napi_handle_scope scope); +``` +- `[in] env`: The environment that the API is invoked under. +- `[in] scope`: `napi_value` representing the scope to be closed. + +Returns `napi_ok` if the API succeeded. + +This API closes the scope passed in. Scopes must be closed in the +reverse order from which they were created. + +#### napi_open_escapable_handle_scope + +```C +NODE_EXTERN napi_status + napi_open_escapable_handle_scope(napi_env env, + napi_handle_scope* result); +``` +- `[in] env`: The environment that the API is invoked under. +- `[out] result`: `napi_value` representing the new scope. + +Returns `napi_ok` if the API succeeded. + +This API open a new scope from which objects can be promoted +to the outer scope. + +#### napi_close_escapable_handle_scope + +```C +NODE_EXTERN napi_status + napi_close_escapable_handle_scope(napi_env env, + napi_handle_scope scope); +``` +- `[in] env`: The environment that the API is invoked under. +- `[in] scope`: `napi_value` representing the scope to be closed. + +Returns `napi_ok` if the API succeeded. + +This API closes the scope passed in. Scopes must be closed in the +reverse order from which they were created. + +#### napi_escape_handle + +```C +NAPI_EXTERN napi_status napi_escape_handle(napi_env env, + napi_escapable_handle_scope scope, + napi_value escapee, + napi_value* result); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] scope`: `napi_value` representing the current scope. +- `[in] escapee`: `napi_value` representing the JavaScript Object to be escaped. +- `[out] result`: `napi_value` representing the handle to the escaped +Object in the outer scope. + +Returns `napi_ok` if the API succeeded. + +This API promotes the handle to the JavaScript object so that it valid +for the lifetime of the outer scope. + + +### References to objects with a lifespan longer than that of the native method +In some cases an addon will need to be able to create and reference objects +with a lifespan longer than that of a single native method invocation. For +example, to create a constructor and later use that constructor +in a request to creates instances, it must be possible to reference +the constructor object across many different instance creation requests. This +would not be possible with a normal handle returned as a `napi_value` as +described in the earlier section. The lifespan of a normal handle is +managed by scopes and all scopes must be closed before the end of a native +method. + +N-API provides methods to create persistent references to an object. +Each persistent reference has an associated count with a value of 0 +or higher. The count determines if the reference will keep +the corresponding object live. References with a count of 0 do not +prevent the object from being collected and are often called 'weak' +references. Any count greater than 0 will prevent the object +from being collected. + +References can be created with an initial reference count. The count can +then be modified through [`napi_reference_ref`][] and +[`napi_reference_unref`][]. If an object is collected while the count +for a reference is 0, all subsequent calls to +get the object associated with the reference [`napi_get_reference_value`][] +will return NULL for the returned `napi_value`. An attempt to call +[`napi_reference_ref`][] for a reference whose object has been collected +will result in an error. + +References must be deleted once they are no longer required by the addon. When +a reference is deleted it will no longer prevent the corresponding object from +being collected. Failure to delete a persistent reference will result in +a 'memory leak' with both the native memory for the persistent reference and +the corresponding object on the heap being retained forever. + +There can be multiple persistent references created which refer to the same +object, each of which will either keep the object live or not based on its +individual count. + +#### napi_create_reference + +```C +NODE_EXTERN napi_status napi_create_reference(napi_env env, + napi_value value, + int initial_refcount, + ndapi_ref* result); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: `napi_value` representing the Object to which we want +a reference to. +- `[in] initial_refcount`: Initial reference count for the new reference. +- `[out] result`: `napi_ref` pointing to the new reference. + +Returns `napi_ok` if the API succeeded. + +This API create a new reference with the specified reference count +to the Object passed in. + +#### napi_delete_reference + +```C +NODE_EXTERN napi_status napi_delete_reference(napi_env env, napi_ref ref); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] ref`: `napi_ref` to be deleted. + +Returns `napi_ok` if the API succeeded. + +This API deletes the reference passed in. + +#### napi_reference_ref + +```C +NODE_EXTERN napi_status napi_reference_ref(napi_env env, + napi_ref ref, + int* result); +``` +- `[in] env`: The environment that the API is invoked under. +- `[in] ref`: `napi_ref` for which the reference count will be incremented. +- `[out] result`: The new reference count. + +Returns `napi_ok` if the API succeeded. + +This API increments the reference count for the reference +passed in and returns the resulting reference count. + + +#### napi_reference_unref + +```C +NODE_EXTERN napi_status napi_reference_unref(napi_env env, + napi_ref ref, + int* result); +``` +- `[in] env`: The environment that the API is invoked under. +- `[in] ref`: `napi_ref` for which the reference count will be decremented. +- `[out] result`: The new reference count. + +Returns `napi_ok` if the API succeeded. + +This API decrements the reference count for the reference +passed in and returns the resulting reference count. + + +#### napi_get_reference_value + +```C +NODE_EXTERN napi_status napi_get_reference_value(napi_env env, + napi_ref ref, + napi_value* result); +``` + +the `napi_value passed` in or out of these methods is a handle to the +object to which the reference is related. +- `[in] env`: The environment that the API is invoked under. +- `[in] ref`: `napi_ref` for which we requesting the corresponding Object. +- `[out] result`: The `napi_value` for the Object referenced by the +`napi_ref`. + +Returns `napi_ok` if the API succeeded. + +If still valid, this API returns the `napi_value` representing the +JavaScript Object associated with the `napi_ref`. Otherise, result +will be NULL. + +## Module registration +N-API modules are registered in the same manner as other modules +except that instead of using the `NODE_MODULE` macro the following +is used: + +```C +NAPI_MODULE(addon, Init) +``` + +The next difference is the signature for the `Init` method. For a N-API +module it is as follows: + +```C +void Init(napi_env env, napi_value exports, napi_value module, void* priv); +``` + +As with any other module, functions are exported by either adding them to +the `exports` or `module` objects passed to the `Init` method. + +For example, to add the method `hello` as a function so that it can be called +as a method provided by the addon: + +```C +void Init(napi_env env, napi_value exports, napi_value module, void* priv) { + napi_status status; + napi_property_descriptor desc = + {"hello", Method, 0, 0, 0, napi_default, 0}; + status = napi_define_properties(env, exports, 1, &desc); +} +``` + +For example, to set a function to be returned by the `require()` for the addon: + +```C +void Init(napi_env env, napi_value exports, napi_value module, void* priv) { + napi_status status; + napi_property_descriptor desc = + {"exports", Method, 0, 0, 0, napi_default, 0}; + status = napi_define_properties(env, module, 1, &desc); +} +``` + +For example, to define a class so that new instances can be created +(often used with [Object Wrap][]): + +```C +// NOTE: partial example, not all referenced code is included + +napi_status status; +napi_property_descriptor properties[] = { + { "value", nullptr, GetValue, SetValue, 0, napi_default, 0 }, + DECLARE_NAPI_METHOD("plusOne", PlusOne), + DECLARE_NAPI_METHOD("multiply", Multiply), +}; + +napi_value cons; +status = + napi_define_class(env, "MyObject", New, nullptr, 3, properties, &cons); +if (status != napi_ok) return; + +status = napi_create_reference(env, cons, 1, &constructor); +if (status != napi_ok) return; + +status = napi_set_named_property(env, exports, "MyObject", cons); +if (status != napi_ok) return; +``` + +For more details on setting properties on either the `exports` or `module` +objects, see the section on +[Working with JavaScript Properties][]. + +For more details on building addon modules in general, refer to the existing API + +## Working with JavaScript Values +N-API exposes a set of APIs to create all types of JavaScript values. +Some of these types are documented under +[Section 6](https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values) +of the [ECMAScript Language Specification][]. + +Fundamentally, these APIs are used to do one of the following: +1. Create a new JavaScript object +2. Convert from a primitive C type to an N-API value +3. Convert from N-API value to a primitive C type +4. Get global instances including `undefined` and `null` + +N-API values are represented by the type `napi_value`. +Any N-API call that requires a JavaScript value takes in a `napi_value`. +In some cases, the API does check the type of the `napi_value` up-front. +However, for better performance, it's better for the caller to make sure that +the `napi_value` in question is of the JavaScript type expected by the API. + +### Enum types +#### *napi_valuetype* +```C +typedef enum { + // ES6 types (corresponds to typeof) + napi_undefined, + napi_null, + napi_boolean, + napi_number, + napi_string, + napi_symbol, + napi_object, + napi_function, + napi_external, +} napi_valuetype; +``` + +Describes the type of a `napi_value`. This generally corresponds to the types +described in +[Section 6.1](https://tc39.github.io/ecma262/#sec-ecmascript-language-types) of +the ECMAScript Language Specification. +In addition to types in that section, `napi_valuetype` can also represent +Functions and Objects with external data. + +#### *napi_typedarray_type* +```C +typedef enum { + napi_int8_array, + napi_uint8_array, + napi_uint8_clamped_array, + napi_int16_array, + napi_uint16_array, + napi_int32_array, + napi_uint32_array, + napi_float32_array, + napi_float64_array, +} napi_typedarray_type; +``` + +This represents the underlying binary scalar datatype of the TypedArray. +Elements of this enum correspond to +[Section 22.2](https://tc39.github.io/ecma262/#sec-typedarray-objects) +of the [ECMAScript Language Specification][]. + +### Object Creation Functions +#### *napi_create_array* + +```C +napi_status napi_create_array(napi_env env, napi_value* result) +``` + +- `[in] env`: The environment that the N-API call is invoked under. +- `[out] result`: A `napi_value` representing a JavaScript Array. + +Returns `napi_ok` if the API succeeded. + +This API returns an N-API value corresponding to a JavaScript Array type. +JavaScript arrays are described in +[Section 22.1](https://tc39.github.io/ecma262/#sec-array-objects) of the +ECMAScript Language Specification. + +#### *napi_create_array_with_length* + +```C +napi_status napi_create_array_with_length(napi_env env, + size_t length, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] length`: The initial length of the Array. +- `[out] result`: A `napi_value` representing a JavaScript Array. + +Returns `napi_ok` if the API succeeded. + +This API returns an N-API value corresponding to a JavaScript Array type. +The Array's length property is set to the passed-in length parameter. +However, the underlying buffer is not guaranteed to be pre-allocated by the VM +when the array is created - that behavior is left to the underlying VM +implementation. +if the buffer must be a contiguous block of memory that can be +directly read and/or written via C, consider using +[`napi_create_external_arraybuffer`][]. + +JavaScript arrays are described in +[Section 22.1](https://tc39.github.io/ecma262/#sec-array-objects) of the +ECMAScript Language Specification. + +#### *napi_create_arraybuffer* + +```C +napi_status napi_create_arraybuffer(napi_env env, + size_t byte_length, + void** data, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] length`: The length in bytes of the array buffer to create. +- `[out] data`: Pointer to the underlying byte buffer of the ArrayBuffer. +- `[out] result`: A `napi_value` representing a JavaScript ArrayBuffer. + +Returns `napi_ok` if the API succeeded. + +This API returns an N-API value corresponding to a JavaScript ArrayBuffer. +ArrayBuffers are used to represent fixed-length binary data buffers. They are +normally used as a backing-buffer for TypedArray objects. +The ArrayBuffer allocated will have an underlying byte buffer whose size is +determined by the `length` parameter that's passed in. +The underlying buffer is optionally returned back to the caller in case the +caller wants to directly manipulate the buffer. This buffer can only be +written to directly from native code. To write to this buffer from JavaScript, +a typed array or DataView object would need to be created. + +JavaScript ArrayBuffer objects are described in +[Section 24.1](https://tc39.github.io/ecma262/#sec-arraybuffer-objects) +of the ECMAScript Language Specification. + +#### *napi_create_buffer* + +```C +napi_status napi_create_buffer(napi_env env, + size_t size, + void** data, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] size`: Size in bytes of the underlying buffer. +- `[out] data`: Raw pointer to the underlying buffer. +- `[out] result`: A `napi_value` representing a `node::Buffer`. + +Returns `napi_ok` if the API succeeded. + +This API allocates a `node::Buffer` object. While this is still a +fully-supported data structure, in most cases using a TypedArray will suffice. + +#### *napi_create_buffer_copy* + +```C +napi_status napi_create_buffer_copy(napi_env env, + size_t length, + const void* data, + void** result_data, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] size`: Size in bytes of the input buffer (should be the same as the + size of the new buffer). +- `[in] data`: Raw pointer to the underlying buffer to copy from. +- `[out] result_data`: Pointer to the new Buffer's underlying data buffer. +- `[out] result`: A `napi_value` representing a `node::Buffer`. + +Returns `napi_ok` if the API succeeded. + +This API allocates a `node::Buffer` object and initializes it with data copied +from the passed-in buffer. While this is still a fully-supported data +structure, in most cases using a TypedArray will suffice. + +#### *napi_create_external* + +```C +napi_status napi_create_external(napi_env env, + void* data, + napi_finalize finalize_cb, + void* finalize_hint, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] data`: Raw pointer to the external data being wrapped. +- `[in] finalize_cb`: Optional callback to call when the wrapped object +is being collected. +- `[in] finalize_hint`: Optional hint to pass to the finalize callback +during collection. +- `[out] result`: A `napi_value` representing an external object. + +Returns `napi_ok` if the API succeeded. + +This API allocates a JavaScript object with external data attached to it. +This is used to wrap native objects and project them into JavaScript. +The API allows the caller to pass in a finalize callback, in case the +underlying native resource needs to be cleaned up when the wrapper +JavaScript object gets collected. + +#### napi_create_external_arraybuffer + +```C +napi_status +napi_create_external_arraybuffer(napi_env env, + void* external_data, + size_t byte_length, + napi_finalize finalize_cb, + void* finalize_hint, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] external_data`: Pointer to the underlying byte buffer of the +ArrayBuffer. +- `[in] byte_length`: The length in bytes of the underlying buffer. +- `[in] finalize_cb`: Optional callback to call when the ArrayBuffer is +being collected. +- `[in] finalize_hint`: Optional hint to pass to the finalize callback +during collection. +- `[out] result`: A `napi_value` representing a JavaScript ArrayBuffer. + +Returns `napi_ok` if the API succeeded. + +This API returns an N-API value corresponding to a JavaScript ArrayBuffer. +The underlying byte buffer of the ArrayBuffer is externally allocated and +managed. The caller must ensure that the byte buffer remains valid until the +finalize callback is called. + +JavaScript ArrayBuffers are described in +[Section 24.1](https://tc39.github.io/ecma262/#sec-arraybuffer-objects) +of the ECMAScript Language Specification. + +#### *napi_create_external_buffer* + +```C +napi_status napi_create_external_buffer(napi_env env, + size_t length, + void* data, + napi_finalize finalize_cb, + void* finalize_hint, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] length`: Size in bytes of the input buffer (should be the same as +the size of the new buffer). +- `[in] data`: Raw pointer to the underlying buffer to copy from. +- `[in] finalize_cb`: Optional callback to call when the ArrayBuffer is +being collected. +- `[in] finalize_hint`: Optional hint to pass to the finalize callback +during collection. +- `[out] result`: A `napi_value` representing a `node::Buffer`. + +Returns `napi_ok` if the API succeeded. + +This API allocates a `node::Buffer` object and initializes it with data +backed by the passed in buffer. While this is still a fully-supported data +structure, in most cases using a TypedArray will suffice. + +**Note:** For Node.js >=4 `Buffers` are Uint8Arrays. + +#### *napi_create_function* + +```C +napi_status napi_create_function(napi_env env, + const char* utf8name, + napi_callback cb, + void* data, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] utf8name`: A string representing the name of the function encoded as +UTF8. +- `[in] cb`: A function pointer to the native function to be invoked when the +created function is invoked from JavaScript. +- `[in] data`: Optional arbitrary context data to be passed into the native +function when it is invoked. +- `[out] result`: A `napi_value` representing a JavaScript function. + +Returns `napi_ok` if the API succeeded. + +This API returns an N-API value corresponding to a JavaScript Function object. +It's used to wrap native functions so that they can be invoked from JavaScript. + +JavaScript Functions are described in +[Section 19.2](https://tc39.github.io/ecma262/#sec-function-objects) +of the ECMAScript Language Specification. + +#### *napi_create_object* + +```C +napi_status napi_create_object(napi_env env, napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[out] result`: A `napi_value` representing a JavaScript Object. + +Returns `napi_ok` if the API succeeded. + +This API allocates a default JavaScript Object. +It is the equivalent of doing `new Object()` in JavaScript. + +The JavaScript Object type is described in +[Section 6.1.7](https://tc39.github.io/ecma262/#sec-object-type) of the +ECMAScript Language Specification. + +#### *napi_create_symbol* + +```C +napi_status napi_create_symbol(napi_env env, + const char* description, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] description`: Null-terminated character buffer representing a +UTF8-encoded string to describe the symbol. +- `[out] result`: A `napi_value` representing a JavaScript Symbol. + +Returns `napi_ok` if the API succeeded. + +This API creates a JavaScript Symbol object from a UTF8-encoded C string + +The JavaScript Symbol type is described in +[Section 19.4](https://tc39.github.io/ecma262/#sec-symbol-objects) +of the ECMAScript Language Specification. + +#### *napi_create_typedarray* + +```C +napi_status napi_create_typedarray(napi_env env, + napi_typedarray_type type, + size_t length, + napi_value arraybuffer, + size_t byte_offset, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] type`: Scalar datatype of the elements within the TypedArray. +- `[in] length`: Number of elements in the TypedArray. +- `[in] arraybuffer`: ArrayBuffer underlying the typed array. +- `[in] byte_offset`: The byte offset within the ArrayBuffer from which to +start projecting the TypedArray. +- `[out] result`: A `napi_value` representing a JavaScript TypedArray. + +Returns `napi_ok` if the API succeeded. + +This API creates a JavaScript TypedArray object over an existing ArrayBuffer. +TypedArray objects provide an array-like view over an underlying data buffer +where each element has the same underlying binary scalar datatype. + +It's required that (length * size_of_element) + byte_offset should +be <= the size in bytes of the array passed in. If not, a RangeError exception is +raised. + +JavaScript TypedArray Objects are described in +[Section 22.2](https://tc39.github.io/ecma262/#sec-typedarray-objects) +of the ECMAScript Language Specification. + +### Functions to convert from C types to N-API +#### *napi_create_number* + +```C +napi_status napi_create_number(napi_env env, double value, napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: Double-precision value to be represented in JavaScript. +- `[out] result`: A `napi_value` representing a JavaScript Number. + +Returns `napi_ok` if the API succeeded. + +This API is used to convert from the C double type to the JavaScript +Number type. + +The JavaScript Number type is described in +[Section 6.1.6](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type) +of the ECMAScript Language Specification. + +#### *napi_create_string_utf16* + +```C +napi_status napi_create_string_utf16(napi_env env, + const char16_t* str, + size_t length, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] str`: Character buffer representing a UTF16-LE-encoded string. +- `[in] length`: The length of the string in characters, or -1 if it is +null-terminated. +- `[out] result`: A `napi_value` representing a JavaScript String. + +Returns `napi_ok` if the API succeeded. + +This API creates a JavaScript String object from a UTF16-LE-encoded C string + +The JavaScript String type is described in +[Section 6.1.4](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type) +of the ECMAScript Language Specification. + +#### *napi_create_string_utf8* + +```C +napi_status napi_create_string_utf8(napi_env env, + const char* str, + size_t length, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] s`: Character buffer representing a UTF8-encoded string. +- `[in] length`: The length of the string in characters, or -1 if it is +null-terminated. +- `[out] result`: A `napi_value` representing a JavaScript String. + +Returns `napi_ok` if the API succeeded. + +This API creates a JavaScript String object from a UTF8-encoded C string + +The JavaScript String type is described in +[Section 6.1.4](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type) +of the ECMAScript Language Specification. + +### Functions to convert from N-API to C types +#### *napi_get_array_length* + +```C +napi_status napi_get_array_length(napi_env env, + napi_value value, + uint32_t* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: `napi_value` representing the JavaScript Array whose length is +being queried. +- `[out] result`: `uint32` representing length of the array. + +Returns `napi_ok` if the API succeeded. + +This API returns the length of an array. + +Array length is described in +[Section 22.1.4.1](https://tc39.github.io/ecma262/#sec-properties-of-array-instances-length) +of the ECMAScript Language Specification. + +#### *napi_get_arraybuffer_info* + +```C +napi_status napi_get_arraybuffer_info(napi_env env, + napi_value arraybuffer, + void** data, + size_t* byte_length) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] arraybuffer`: `napi_value` representing the ArrayBuffer being queried. +- `[out] data`: The underlying data buffer of the ArrayBuffer. +- `[out] byte_length`: Length in bytes of the underlying data buffer. + +Returns `napi_ok` if the API succeeded. + +This API is used to retrieve the underlying data buffer of an ArrayBuffer and +its length. +WARNING: Use caution while using this API. The lifetime of the underlying data +buffer is managed by the ArrayBuffer even after it's returned. A +possible safe way to use this API is in conjunction with [`napi_create_reference`][], +which can be used to guarantee control over the lifetime of the +ArrayBuffer. It's also safe to use the returned data buffer within the same +callback as long as there are no calls to other APIs that might trigger a GC. + +#### *napi_get_buffer_info* + +```C +napi_status napi_get_buffer_info(napi_env env, + napi_value value, + void** data, + size_t* length) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: `napi_value` representing the `node::Buffer` being queried. +- `[out] data`: The underlying data buffer of the `node::Buffer`. +- `[out] length`: Length in bytes of the underlying data buffer. + +Returns `napi_ok` if the API succeeded. + +This API is used to retrieve the underlying data buffer of a `node::Buffer` +and it's length. +Warning: Use caution while using this API since the underlying data buffer's +lifetime is not guaranteed if it's managed by the VM. + +#### *napi_get_prototype* + +```C +napi_status napi_get_prototype(napi_env env, + napi_value object, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] object`: `napi_value` representing JavaScript Object whose prototype +to return. This returns the equivalent of `Object.getPrototypeOf` (which is +not the same as the function's `prototype` property). +- `[out] result`: `napi_value` representing prototype of the given object. + +Returns `napi_ok` if the API succeeded. + +#### *napi_get_typedarray_info* + +```C +napi_status napi_get_typedarray_info(napi_env env, + napi_value typedarray, + napi_typedarray_type* type, + size_t* length, + void** data, + napi_value* arraybuffer, + size_t* byte_offset) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] typedarray`: `napi_value` representing the TypedArray whose +properties to query. +- `[out] type`: Scalar datatype of the elements within the TypedArray. +- `[out] length`: Number of elements in the TypedArray. +- `[out] data`: The data buffer underlying the typed array. +- `[out] byte_offset`: The byte offset within the data buffer from which +to start projecting the TypedArray. + +Returns `napi_ok` if the API succeeded. + +This API returns various properties of a typed array. +Warning: Use caution while using this API since the underlying data buffer +is managed by the VM + +#### *napi_get_value_bool* + +```C +napi_status napi_get_value_bool(napi_env env, napi_value value, bool* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: `napi_value` representing JavaScript Boolean. +- `[out] result`: C boolean primitive equivalent of the given JavaScript +Boolean. + +Returns `napi_ok` if the API succeeded. If a non-boolean `napi_value` is +passed in it returns `napi_boolean_expected`. + +This API returns C boolean primitive equivalent of the given JavaScript +Boolea + +#### *napi_get_value_double* + +```C +napi_status napi_get_value_double(napi_env env, + napi_value value, + double* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: `napi_value` representing JavaScript Number. +- `[out] result`: C double primitive equivalent of the given JavaScript +Number. + +Returns `napi_ok` if the API succeeded. If a non-number `napi_value` is passed +in it returns `napi_number_expected`. + +This API returns the C double primitive equivalent of the given JavaScript +Number. + + +#### *napi_get_value_external* + +```C +napi_status napi_get_value_external(napi_env env, + napi_value value, + void** result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: `napi_value` representing JavaScript External value. +- `[out] result`: Pointer to the data wrapped by the JavaScript External value. + +Returns `napi_ok` if the API succeeded. If a non-external `napi_value` is +passed in it returns `napi_invalid_arg`. + +This API returns the pointer to the data wrapped by the JavaScript +External value + +#### *napi_get_value_int32* + +```C +napi_status napi_get_value_int32(napi_env env, + napi_value value, + int32_t* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: `napi_value` representing JavaScript Number. +- `[out] result`: C int32 primitive equivalent of the given JavaScript Number. + +Returns `napi_ok` if the API succeeded. If a non-number `napi_value` +is passed in `napi_number_expected . + +This API returns the C int32 primitive equivalent +of the given JavaScript Number. If the number exceeds the range of the +32 bit integer, then the result is truncated to the equivalent of the +bottom 32 bits. This can result in a large positive number becoming +a negative number if the the value is > 2^31 -1. + +#### *napi_get_value_int64* + +```C +napi_status napi_get_value_int64(napi_env env, + napi_value value, + int64_t* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: `napi_value` representing JavaScript Number. +- `[out] result`: C int64 primitive equivalent of the given JavaScript Number. + +Returns `napi_ok` if the API succeeded. If a non-number `napi_value` +is passed in it returns `napi_number_expected`. + +This API returns the C int64 primitive equivalent of the given +JavaScript Number + +#### *napi_get_value_string_length* + +```C +napi_status napi_get_value_string_length(napi_env env, + napi_value value, + int* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: `napi_value` representing JavaScript string. +- `[out] result`: Number of characters in the given JavaScript string. + +Returns `napi_ok` if the API succeeded. If a non-String `napi_value` +is passed in it returns `napi_string_expected`. + +This API returns the number of characters in the given JavaScript string. + +#### *napi_get_value_string_utf8* + +```C +napi_status napi_get_value_string_utf8(napi_env env, + napi_value value, + char* buf, + size_t bufsize, + size_t* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: `napi_value` representing JavaScript string. +- `[in] buf`: Buffer to write the UTF8-encoded string into. If NULL is passed + in, the length of the string (in bytes) is returned. +- `[in] bufsize`: Size of the destination buffer. +- `[out] result`: Number of bytes copied into the buffer including the null. +terminator. If the buffer size is insufficient, the string will be truncated +including a null terminator. + +Returns `napi_ok` if the API succeeded. Ifa non-String `napi_value` +x is passed in it returns `napi_string_expected`. + +This API returns the UTF8-encoded string corresponding the value passed in. + +#### *napi_get_value_string_utf16_length* + +```C +napi_status napi_get_value_string_utf16(napi_env env, + napi_value value, + char16_t* buf, + size_t bufsize, + size_t* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: `napi_value` representing JavaScript string. +- `[in] buf`: Buffer to write the UTF16-LE-encoded string into. If NULL is +passed in, the length of the string (in 2-byte code units) is returned. +- `[in] bufsize`: Size of the destination buffer. +- `[out] result`: Number of 2-byte code units copied into the buffer including +the null terminateor. If the buffer size is insufficient, the string will be +truncated including a null terminator. + +Returns `napi_ok` if the API succeeded. If a non-String `napi_value` +is passed in it returns `napi_string_expected`. + +This API returns the UTF16-encoded string corresponding the value passed in. + +#### *napi_get_value_uint32* + +```C +napi_status napi_get_value_uint32(napi_env env, + napi_value value, + uint32_t* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: `napi_value` representing JavaScript Number. +- `[out] result`: C primitive equivalent of the given `napi_value` as a +`uint32_t`. + +Returns `napi_ok` if the API succeeded. If a non-number `napi_value` +is passed in it returns `napi_number_expected`. + +This API returns the C primitive equivalent of the given `napi_value` as a +`uint32_t`. + +### Functions to get global instances +#### *napi_get_boolean* + +```C +napi_status napi_get_boolean(napi_env env, bool value, napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: The value of the boolean to retrieve. +- `[out] result`: `napi_value` representing JavaScript Boolean singleton to +retrieve. + +Returns `napi_ok` if the API succeeded. + +This API is used to return the JavaScript singleton object that is used to +represent the given boolean value + +#### *napi_get_global* + +```C +napi_status napi_get_global(napi_env env, napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[out] result`: `napi_value` representing JavaScript Global Object. + +Returns `napi_ok` if the API succeeded. + +This API returns the global Object. + +#### *napi_get_null* + +```C +napi_status napi_get_null(napi_env env, napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[out] result`: `napi_value` representing JavaScript Null Object. + +Returns `napi_ok` if the API succeeded. + +This API returns the null Object. + +#### *napi_get_undefined* + +```C +napi_status napi_get_undefined(napi_env env, napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[out] result`: `napi_value` representing JavaScript Undefined value. + +Returns `napi_ok` if the API succeeded. + +This API returns the Undefined object. + +## Working with JavaScript Values - Abstract Operations + +N-API exposes a set of APIs to perform some abstract operations on JavaScript +values. Some of these operations are documented under +[Section 7](https://tc39.github.io/ecma262/#sec-abstract-operations) +of the [ECMAScript Language Specification](https://tc39.github.io/ecma262/). + +These APIs support doing one of the following: +1. Coerce JavaScript values to specific JavaScript types (such as Number or + String) +2. Check the type of a JavaScript value +3. Check for equality between two JavaScript values + +### *napi_coerce_to_bool* + +```C +napi_status napi_coerce_to_bool(napi_env env, + napi_value value, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: The JavaScript value to coerce. +- `[out] result`: `napi_value` representing the coerced JavaScript Boolean. + +Returns `napi_ok` if the API succeeded. + +This API implements the abstract operation ToBoolean as defined in +[Section 7.1.2](https://tc39.github.io/ecma262/#sec-toboolean) +of the ECMAScript Language Specification. +This API can be re-entrant if getters are defined on the passed-in Object. + +### *napi_coerce_to_number* + +```C +napi_status napi_coerce_to_number(napi_env env, + napi_value value, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: The JavaScript value to coerce. +- `[out] result`: `napi_value` representing the coerced JavaScript Number. + +Returns `napi_ok` if the API succeeded. + +This API implements the abstract operation ToNumber as defined in +[Section 7.1.3](https://tc39.github.io/ecma262/#sec-tonumber) +of the ECMAScript Language Specification. +This API can be re-entrant if getters are defined on the passed-in Object. + +### *napi_coerce_to_object* + +```C +napi_status napi_coerce_to_object(napi_env env, + napi_value value, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: The JavaScript value to coerce. +- `[out] result`: `napi_value` representing the coerced JavaScript Object. + +Returns `napi_ok` if the API succeeded. + +This API implements the abstract operation ToObject as defined in +[Section 7.1.13](https://tc39.github.io/ecma262/#sec-toobject) +of the ECMAScript Language Specification. +This API can be re-entrant if getters are defined on the passed-in Object. + +### *napi_coerce_to_string* + +```C +napi_status napi_coerce_to_string(napi_env env, + napi_value value, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: The JavaScript value to coerce. +- `[out] result`: `napi_value` representing the coerced JavaScript String. + +Returns `napi_ok` if the API succeeded. + +This API implements the abstract operation ToString as defined in +[Section 7.1.13](https://tc39.github.io/ecma262/#sec-tostring) +of the ECMAScript Language Specification. +This API can be re-entrant if getters are defined on the passed-in Object. + +### *napi_typeof* + +```C +napi_status napi_typeof(napi_env env, napi_value value, napi_valuetype* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: The JavaScript value whose type to query. +- `[out] result`: The type of the JavaScript value. + +Returns `napi_ok` if the API succeeded. +- `napi_invalid_arg` if the type of `value` is not a known ECMAScript type and + `value` is not an External value. + +This API represents behavior similar to invoking the `typeof` Operator on +the object as defined in [Section 12.5.5][] of the ECMAScript Language +Specification. However, it has support for detecting an External value. +If `value` has a type that is invalid, an error is returned. + +### *napi_instanceof* + +```C +napi_status napi_instanceof(napi_env env, + napi_value object, + napi_value constructor, + bool* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] object`: The JavaScript value to check. +- `[in] constructor`: The JavaScript function object of the constructor +function to check against. +- `[out] result`: Boolean that is set to true if `object instanceof constructor` +is true. + +Returns `napi_ok` if the API succeeded. + +This API represents invoking the `instanceof` Operator on the object as +defined in +[Section 12.10.4](https://tc39.github.io/ecma262/#sec-instanceofoperator) +of the ECMAScript Language Specification. + +### *napi_is_array* + +```C +napi_status napi_is_array(napi_env env, napi_value value, bool* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: The JavaScript value to check. +- `[out] result`: Whether the given object is an array. + +Returns `napi_ok` if the API succeeded. + +This API represents invoking the `IsArray` operation on the object +as defined in [Section 7.2.2](https://tc39.github.io/ecma262/#sec-isarray) +of the ECMAScript Language Specification. + +### *napi_is_arraybuffer* + +```C +napi_status napi_is_arraybuffer(napi_env env, napi_value value, bool* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: The JavaScript value to check. +- `[out] result`: Whether the given object is an ArrayBuffer. + +Returns `napi_ok` if the API succeeded. + +This API checks if the Object passsed in is an array buffer. + +### *napi_is_buffer* + +```C +napi_status napi_is_buffer(napi_env env, napi_value value, bool* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: The JavaScript value to check. +- `[out] result`: Whether the given `napi_value` represents a `node::Buffer` +object. + +Returns `napi_ok` if the API succeeded. + +This API checks if the Object passsed in is a buffer. + +### *napi_is_error* + +```C +napi_status napi_is_error(napi_env env, napi_value value, bool* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: The JavaScript value to check. +- `[out] result`: Whether the given `napi_value` represents an Error object. + +Returns `napi_ok` if the API succeeded. + +This API checks if the Object passsed in is an Error. + +### *napi_is_typedarray* + +```C +napi_status napi_is_typedarray(napi_env env, napi_value value, bool* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: The JavaScript value to check. +- `[out] result`: Whether the given `napi_value` represents a TypedArray. + +Returns `napi_ok` if the API succeeded. + +This API checks if the Object passsed in is a typed array. + +### *napi_strict_equals* + +```C +napi_status napi_strict_equals(napi_env env, + napi_value lhs, + napi_value rhs, + bool* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] lhs`: The JavaScript value to check. +- `[in] rhs`: The JavaScript value to check against. +- `[out] result`: Whether the two `napi_value` objects are equal. + +Returns `napi_ok` if the API succeeded. + +This API represents the invocation of the Strict Equality algorithm as +defined in +[Section 7.2.14](https://tc39.github.io/ecma262/#sec-strict-equality-comparison) +of the ECMAScript Language Specification. + +## Working with JavaScript Properties + +N-API exposes a set of APIs to get and set properties on JavaScript +objects. Some of these types are documented under +[Section 7](https://tc39.github.io/ecma262/#sec-operations-on-objects) of the +[ECMAScript Language Specification](https://tc39.github.io/ecma262/). + +Properties in JavaScript are represented as a tuple of a key and a value. +Fundamentally, all property keys in N-API can be represented in one of the +following forms: +- Named: a simple UTF8-encoded string +- Integer-Indexed: an index value represented by `uint32_t` +- JavaScript value: these are represented in N-API by `napi_value`. This can +be a `napi_value` representing a String, Number or Symbol. + +N-API values are represented by the type `napi_value`. +Any N-API call that requires a JavaScript value takes in a `napi_value`. +However, it's the caller's responsibility to make sure that the +`napi_value` in question is of the JavaScript type expected by the API. + +The APIs documented in this section provide a simple interface to +get and set properties on arbitrary JavaScript objects represented by +`napi_value`. + +For instance, consider the following JavaScript code snippet: +```js +const obj = {}; +obj.myProp = 123; +``` +The equivalent can be done using N-API values with the following snippet: +```C +napi_status status = napi_generic_failure; + +// const obj = {} +napi_value obj, value; +status = napi_create_object(env, &obj); +if (status != napi_ok) return status; + +// Create a napi_value for 123 +status = napi_create_number(env, 123, &value); +if (status != napi_ok) return status; + +// obj.myProp = 123 +status = napi_set_named_property(env, obj, "myProp", value); +if (status != napi_ok) return status; +``` + +Indexed properties can be set in a similar manner. Consider the following +JavaScript snippet: +```js +const arr = []; +arr[123] = 'hello'; +``` +The equivalent can be done using N-API values with the following snippet: +```C +napi_status status = napi_generic_failure; + +// const arr = []; +napi_value arr, value; +status = napi_create_array(env, &arr); +if (status != napi_ok) return status; + +// Create a napi_value for 'hello' +status = napi_create_string_utf8(env, "hello", -1, &value); +if (status != napi_ok) return status; + +// arr[123] = 'hello'; +status = napi_set_element(env, arr, 123, value); +if (status != napi_ok) return status; +``` + +Properties can be retrieved using the APIs described in this section. +Consider the following JavaScript snippet: +```js +const arr = []; +const value = arr[123]; +``` + +The following is the approximate equivalent of the N-API counterpart: +```C +napi_status status = napi_generic_failure; + +// const arr = [] +napi_value arr, value; +status = napi_create_array(env, &arr); +if (status != napi_ok) return status; + +// const value = arr[123] +status = napi_get_element(env, arr, 123, &value); +if (status != napi_ok) return status; +``` + +Finally, multiple properties can also be defined on an object for performance +reasons. Consider the following JavaScript: +```js +const obj = {}; +Object.defineProperties(obj, { + 'foo': { value: 123, writable: true, configurable: true, enumerable: true }, + 'bar': { value: 456, writable: true, configurable: true, enumerable: true } +}); +``` + +The following is the approximate equivalent of the N-API counterpart: +```C +napi_status status = napi_status_generic_failure; + +// const obj = {}; +napi_value obj; +status = napi_create_obj(env, &obj); +if (status != napi_ok) return status; + +// Create napi_values for 123 and 456 +napi_value fooValue, barValue; +status = napi_create_number(env, 123, &fooValue); +if (status != napi_ok) return status; +status = napi_create_number(env, 456, &barValue); +if (status != napi_ok) return status; + +// Set the properties +napi_property_descriptors descriptors[] = { + { "foo", fooValue, 0, 0, 0, napi_default, 0 }, + { "bar", barValue, 0, 0, 0, napi_default, 0 } +} +status = napi_define_properties(env, + obj, + sizeof(descriptors) / sizeof(descriptors[0]), + descriptors); +if (status != napi_ok) return status; +``` + +### Structures +#### *napi_property_attributes* +```C +typedef enum { + napi_default = 0, + napi_read_only = 1 << 0, + napi_dont_enum = 1 << 1, + napi_dont_delete = 1 << 2, + napi_static_property = 1 << 10, +} napi_property_attributes; +``` + +`napi_property_attributes` are flags used to control the behavior of properties +set on a JavaScript object. They roughly correspond to the attributes listed in +[Section 6.1.7.1](https://tc39.github.io/ecma262/#table-2) of the +[ECMAScript Language Specification](https://tc39.github.io/ecma262/). They can +be one or more of the following bitflags: + +- `napi_default` - Used to indicate that no explicit attributes are set on the +given property. By default, a property is Writable, Enumerable, and + Configurable. This is a deviation from the ECMAScript specification, + where generally the values for a property descriptor attribute default to + false if they're not provided. +- `napi_read_only` - Used to indicate that a given property is not Writable. +- `napi_dont_enum` - Used to indicate that a given property is not Enumerable. +- `napi_dont_delete` - Used to indicate that a given property is not. +Configurable, as defined in +[Section 6.1.7.1](https://tc39.github.io/ecma262/#table-2) of the +[ECMAScript Language Specification](https://tc39.github.io/ecma262/). +- `napi_static_property` - Used to indicate that the property will be defined as +a static property on a class as opposed to an instance property, which is the +default. This is used only by [`napi_define_class`][]. It is ignored by +`napi_define_properties`. + +#### *napi_property_descriptor* +```C +typedef struct { + const char* utf8name; + + napi_callback method; + napi_callback getter; + napi_callback setter; + napi_value value; + + napi_property_attributes attributes; + void* data; +} napi_property_descriptor; +``` + +- `utf8name`: String describing the key for the property, encoded as UTF8. +- `value`: The value that's retrieved by a get access of the property if the + property is a data property. If this is passed in, set `getter`, `setter`, + `method` and `data` to `NULL` (since these members won't be used). +- `getter`: A function to call when a get access of the property is performed. +If this is passed in, set `value` and `method` to `NULL` (since these members +won't be used). The given function is called implicitly by the runtime when the +property is accessed from JavaScript code (or if a get on the property is +performed using a N-API call). +- `setter`: A function to call when a set access of the property is performed. +If this is passed in, set `value` and `method` to `NULL` (since these members +won't be used). The given function is called implicitly by the runtime when the +property is set from JavaScript code (or if a set on the property is +performed using a N-API call). +- `method`: Set this to make the property descriptor object's `value` +property to be a JavaScript function represented by `method`. If this is +passed in, set `value`, `getter` and `setter` to `NULL` (since these members +won't be used). +- `data`: The callback data passed into `method`, `getter` and `setter` if +this function is invoked. +- `attributes`: The attributes associated with the particular property. +See [`napi_property_attributes`](#napi_property_attributes). + +### Functions +#### *napi_get_property_names* + +```C +napi_status napi_get_property_names(napi_env env, + napi_value object, + napi_value* result); +``` + +- `[in] env`: The environment that the N-API call is invoked under. +- `[in] object`: The object from which to retrieve the properties. +- `[out] result`: A `napi_value` representing an array of JavaScript values +that represent the property names of the object. The API can be used to +iterate over `result` using [`napi_get_array_length`][] +and [`napi_get_element`][]. + +Returns `napi_ok` if the API succeeded. + +This API returns the array of propertys for the Object passed in + +#### *napi_set_property* + +```C +napi_status napi_set_property(napi_env env, + napi_value object, + napi_value key, + napi_value value); +``` + +- `[in] env`: The environment that the N-API call is invoked under. +- `[in] object`: The object on which to set the property. +- `[in] key`: The name of the property to set. +- `[in] value`: The property value. + +Returns `napi_ok` if the API succeeded. + +This API set a property on the Object passed in. + +#### *napi_get_property* + +```C +napi_status napi_get_property(napi_env env, + napi_value object, + napi_value key, + napi_value* result); +``` + +- `[in] env`: The environment that the N-API call is invoked under. +- `[in] object`: The object from which to retrieve the property. +- `[in] key`: The name of the property to retrieve. +- `[out] result`: The value of the property. + +Returns `napi_ok` if the API succeeded. + +This API gets the requested property from the Object passed in. + + +#### *napi_has_property* + +```C +napi_status napi_has_property(napi_env env, + napi_value object, + napi_value key, + bool* result); +``` + +- `[in] env`: The environment that the N-API call is invoked under. +- `[in] object`: The object to query. +- `[in] key`: The name of the property whose existence to check. +- `[out] result`: Whether the property exists on the object or not. + +Returns `napi_ok` if the API succeeded. + +This API checks if the Object passed in has the named property. + + +#### *napi_set_named_property* + +```C +napi_status napi_set_named_property(napi_env env, + napi_value object, + const char* utf8Name, + napi_value value); +``` + +- `[in] env`: The environment that the N-API call is invoked under. +- `[in] object`: The object on which to set the property. +- `[in] utf8Name`: The name of the property to set. +- `[in] value`: The property value. + +Returns `napi_ok` if the API succeeded. + +This method is equivalent to calling [`napi_set_property`][] with a `napi_value` +created from the string passed in as `utf8Name` + +#### *napi_get_named_property* + +```C +napi_status napi_get_named_property(napi_env env, + napi_value object, + const char* utf8Name, + napi_value* result); +``` + +- `[in] env`: The environment that the N-API call is invoked under. +- `[in] object`: The object from which to retrieve the property. +- `[in] utf8Name`: The name of the property to get. +- `[out] result`: The value of the property. + +Returns `napi_ok` if the API succeeded. + +This method is equivalent to calling [`napi_get_property`][] with a `napi_value` +created from the string passed in as `utf8Name` + +#### *napi_has_named_property* + +```C +napi_status napi_has_named_property(napi_env env, + napi_value object, + const char* utf8Name, + bool* result); +``` + +- `[in] env`: The environment that the N-API call is invoked under. +- `[in] object`: The object to query. +- `[in] utf8Name`: The name of the property whose existence to check. +- `[out] result`: Whether the property exists on the object or not. + +Returns `napi_ok` if the API succeeded. + +This method is equivalent to calling [`napi_has_property`][] with a `napi_value` +created from the string passed in as `utf8Name` + +#### *napi_set_element* + +```C +napi_status napi_set_element(napi_env env, + napi_value object, + uint32_t index, + napi_value value); +``` + +- `[in] env`: The environment that the N-API call is invoked under. +- `[in] object`: The object from which to set the properties. +- `[in] index`: The index of the property to set. +- `[in] value`: The property value. + +Returns `napi_ok` if the API succeeded. + +This API sets and element on the Object passed in. + +#### *napi_get_element* + +```C +napi_status napi_get_element(napi_env env, + napi_value object, + uint32_t index, + napi_value* result); +``` + +- `[in] env`: The environment that the N-API call is invoked under. +- `[in] object`: The object from which to retrieve the property. +- `[in] index`: The index of the property to get. +- `[out] result`: The value of the property. + +Returns `napi_ok` if the API succeeded. + +This API gets the element at the requested index. + +#### *napi_has_element* + +```C +napi_status napi_has_element(napi_env env, + napi_value object, + uint32_t index, + bool* result); +``` + +- `[in] env`: The environment that the N-API call is invoked under. +- `[in] object`: The object to query. +- `[in] index`: The index of the property whose existence to check. +- `[out] result`: Whether the property exists on the object or not. + +Returns `napi_ok` if the API succeeded. + +This API returns if the Object passed in has an element at the +requested index. + +#### *napi_define_properties* + +```C +napi_status napi_define_properties(napi_env env, + napi_value object, + size_t property_count, + const napi_property_descriptor* properties); +``` + +- `[in] env`: The environment that the N-API call is invoked under. +- `[in] object`: The object from which to retrieve the properties. +- `[in] property_count`: The number of elements in the `properties` array. +- `[in] properties`: The array of property descriptors. + +Returns `napi_ok` if the API succeeded. + +This method allows the efficient definition of multiple properties on a given +object. The properties are defined using property descriptors (See +[`napi_property_descriptor`][]). Given an array of such property descriptors, this +API will set the properties on the object one at a time, as defined by +DefineOwnProperty (described in [Section 9.1.6][] of the ECMA262 specification). + +## Working with JavaScript Functions + +N-API provides a set of APIs that allow JavaScript code to +call back into native code. N-API APIs that support calling back +into native code take in a callback functions represented by +the `napi_callback` type. When the JavaScript VM calls back to +native code, the `napi_callback` function provided is invoked. The APIs +documented in this section allow the callback function to do the +following: +- Get information about the context in which the callback was invoked. +- Get the arguments passed into the callback. +- Return a `napi_value` back from the callback. + +Additionally, N-API provides a set of functions which allow calling +JavaScript functions from native code. One can either call a function +like a regular JavaScript function call, or as a constructor +function. + + +### *napi_call_function* + +```C +napi_status napi_call_function(napi_env env, + napi_value recv, + napi_value func, + int argc, + const napi_value* argv, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] recv`: The `this` object passed to the called function. +- `[in] func`: `napi_value` representing the JavaScript function +to be invoked. +- `[in] argc`: The count of elements in the `argv` array. +- `[in] argv`: Array of `napi_values` representing JavaScript values passed +in as arguments to the function. +- `[out] result`: `napi_value` representing the JavaScript object returned. + +Returns `napi_ok` if the API succeeded. + +This method allows a JavaScript function object to be called from a native +add-on. This is an primary mechanism of calling back *from* the add-on's +native code *into* JavaScript. For special cases like calling into JavaScript +after an async operation, see [`napi_make_callback`][]. + +A sample use case might look as follows. Consider the following JavaScript +snippet: +```js +function AddTwo(num) { + return num + 2; +} +``` + +Then, the above function can be invoked from a native add-on using the +following code: +```C +// Get the function named "AddTwo" on the global object +napi_value global, add_two, arg; +napi_status status = napi_get_global(env, &global); +if (status != napi_ok) return; + +status = napi_get_named_property(env, global, "AddTwo", &add_two); +if (status != napi_ok) return; + +// const arg = 1337 +status = napi_create_number(env, 1337, &arg); +if (status != napi_ok) return; + +napi_value* argv = &arg; +size_t argc = 1; + +// AddTwo(arg); +napi_value return_val; +status = napi_call_function(env, global, add_two, argc, argv, &return_val); +if (status != napi_ok) return; + +// Convert the result back to a native type +int32_t result; +status = napi_get_value_int32(env, return_val, &result); +if (status != napi_ok) return; +``` + +### *napi_create_function* + +```C +napi_status napi_create_function(napi_env env, + const char* utf8name, + napi_callback cb, + void* data, + napi_value* result); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] utf8Name`: The name of the function encoded as UTF8. This is visible +within JavaScript as the new function object's `name` property. +- `[in] cb`: The native function which should be called when this function +object is invoked. +- `[in] data`: User-provided data context. This will be passed back into the +function when invoked later. +- `[out] result`: `napi_value` representing the JavaScript function object for +the newly created function. + +Returns `napi_ok` if the API succeeded. + +This API allows an add-on author to create a function object in native code. +This is the primary mechanism to allow calling *into* the add-on's native code +*from* Javascript. + +**Note:** The newly created function is not automatically visible from +script after this call. Instead, a property must be explicitly set on any +object that is visible to JavaScript, in order for the function to be accessible +from script. + +In order to expose a function as part of the +add-on's module exports, set the newly created function on the exports +object. A sample module might look as follows: +```C +void SayHello(napi_env env, napi_callback_info info) { + printf("Hello\n"); +} + +void Init(napi_env env, napi_value exports, napi_value module, void* priv) { + napi_status status; + + napi_value fn; + status = napi_create_function(env, NULL, SayHello, NULL, &fn); + if (status != napi_ok) return; + + status = napi_set_named_property(env, exports, "sayHello", fn); + if (status != napi_ok) return; +} + +NAPI_MODULE(addon, Init) +``` + +Given the above code, the add-on can be used from JavaScript as follows: +```js +const myaddon = require('./addon'); +myaddon.sayHello(); +``` + +**Note:** The string passed to require is not necessarily the name passed into +`NAPI_MODULE` in the earlier snippet but the name of the target in `binding.gyp` +responsible for creating the `.node` file. + +### *napi_get_cb_info* + +```C +napi_status napi_get_cb_info(napi_env env, + napi_callback_info cbinfo, + size_t* argc, + napi_value* argv, + napi_value* thisArg, + void** data) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] cbinfo`: The callback info passed into the callback function. +- `[in-out] argc`: Specifies the size of the provided `argv` array +and receives the actual count of arguments. +- `[out] argv`: Buffer to which the `napi_value` representing the +arguments are copied. If there are more arguments than the provided +count, only the requested number of arguments are copied. If there are fewer +arguments provided than claimed, the rest of `argv` is filled with `napi_value` +values that represent `undefined`. +- `[out] this`: Receives the JavaScript `this` argument for the call. +- `[out] data`: Receives the data pointer for the callback. + +Returns `napi_ok` if the API succeeded. + +This method is used within a callback function to retrieve details about the +call like the arguments and the `this` pointer from a given callback info. + +### *napi_is_construct_call* + +```C +napi_status napi_is_construct_call(napi_env env, + napi_callback_info cbinfo, + bool* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] cbinfo`: The callback info passed into the callback function. +- `[out] result`: Whether the native function is being invoked as +a constructor call. + +Returns `napi_ok` if the API succeeded. + +This API checks if the the current callback was due to a +consructor call. + +### *napi_new_instance* + +```C +napi_status napi_new_instance(napi_env env, + napi_value cons, + size_t argc, + napi_value* argv, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] cons`: `napi_value` representing the JavaScript function +to be invoked as a constructor. +- `[in] argc`: The count of elements in the `argv` array. +- `[in] argv`: Array of JavaScript values as `napi_value` +representing the arguments to the constructor. +- `[out] result`: `napi_value` representing the JavaScript object returned, +which in this case is the constructed object. + +This method is used to instantiate a new JavaScript value using a given +`napi_value` that represents the constructor for the object. For example, +consider the following snippet: +```js +function MyObject(param) { + this.param = param; +} + +const arg = 'hello'; +const value = new MyObject(arg); +``` + +The following can be approximated in N-API using the following snippet: +```C +// Get the constructor function MyObject +napi_value global, constructor, arg, value; +napi_status status = napi_get_global(env, &global); +if (status != napi_ok) return; + +status = napi_get_named_property(env, global, "MyObject", &constructor); +if (status != napi_ok) return; + +// const arg = "hello" +status = napi_create_string_utf8(env, "hello", -1, &arg); +if (status != napi_ok) return; + +napi_value* argv = &arg; +size_t argc = 1; + +// const value = new MyObject(arg) +status = napi_new_instance(env, constructor, argc, argv, &value); +``` + +Returns `napi_ok` if the API succeeded. + +### *napi_make_callback* + +```C +napi_status napi_make_callback(napi_env env, + napi_value recv, + napi_value func, + int argc, + const napi_value* argv, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] recv`: The `this` object passed to the called function. +- `[in] func`: `napi_value` representing the JavaScript function +to be invoked. +- `[in] argc`: The count of elements in the `argv` array. +- `[in] argv`: Array of JavaScript values as `napi_value` +representing the arguments to the function. +- `[out] result`: `napi_value` representing the JavaScript object returned. + +Returns `napi_ok` if the API succeeded. + +This method allows a JavaScript function object to be called from a native +add-on. This API is similar to `napi_call_function`. However, it is used to call +*from* native code back *into* JavaScript *after* returning from an async +operation (when there is no other script on the stack). It is a fairly simple +wrapper around `node::MakeCallback`. + +For an example on how to use `napi_make_callback`, see the section on +[Asynchronous Operations][]. + +## Object Wrap + +N-API offers a way to "wrap" C++ classes and instances so that the class +constructor and methods can be called from JavaScript. + + 1. The [`napi_define_class`][] API defines a JavaScript class with constructor, + static properties and methods, and instance properties and methods that + correspond to the The C++ class. + 2. When JavaScript code invokes the constructor, the constructor callback + uses [`napi_wrap`][] to wrap a new C++ instance in a JavaScript object, + then returns the wrapper object. + 3. When JavaScript code invokes a method or property accessor on the class, + the corresponding `napi_callback` C++ function is invoked. For an instance + callback, [`napi_unwrap`][] obtains the C++ instance that is the target of + the call. + +### *napi_define_class* + +```C +napi_status napi_define_class(napi_env env, + const char* utf8name, + napi_callback constructor, + void* data, + size_t property_count, + const napi_property_descriptor* properties, + napi_value* result); +``` + + - `[in] env`: The environment that the API is invoked under. + - `[in] utf8name`: Name of the JavaScript constructor function; this is + not required to be the same as the C++ class name, though it is recommended + for clarity. + - `[in] constructor`: Callback function that handles constructing instances + of the class. (This should be a static method on the class, not an actual + C++ constructor function.) + - `[in] data`: Optional data to be passed to the constructor callback as + the `data` property of the callback info. + - `[in] property_count`: Number of items in the `properties` array argument. + - `[in] properties`: Array of property descriptors describing static and + instance data properties, accessors, and methods on the class + See `napi_property_descriptor`. + - `[out] result`: A `napi_value` representing the constructor function for + the class. + +Returns `napi_ok` if the API succeeded. + +Defines a JavaScript class that corresponds to a C++ class, including: + - A JavaScript constructor function that has the class name and invokes the + provided C++ constructor callback. + - Properties on the constructor function corresponding to _static_ data + properties, accessors, and methods of the C++ class (defined by + property descriptors with the `napi_static` attribute). + - Properties on the constructor function's `prototype` object corresponding to + _non-static_ data properties, accessors, and methods of the C++ class + (defined by property descriptors without the `napi_static` attribute). + +The C++ constructor callback should be a static method on the class that calls +the actual class constructor, then wraps the new C++ instance in a JavaScript +object, and returns the wrapper object. See `napi_wrap()` for details. + +The JavaScript constructor function returned from [`napi_define_class`][] is +often saved and used later, to construct new instances of the class from native +code, and/or check whether provided values are instances of the class. In that +case, to prevent the function value from being garbage-collected, create a +persistent reference to it using [`napi_create_reference`][] and ensure the +reference count is kept >= 1. + +### *napi_wrap* + +```C +napi_status napi_wrap(napi_env env, + napi_value js_object, + void* native_object, + napi_finalize finalize_cb, + void* finalize_hint, + napi_ref* result); +``` + + - `[in] env`: The environment that the API is invoked under. + - `[in] js_object`: The JavaScript object that will be the wrapper for the + native object. This object _must_ have been created from the `prototype` of + a constructor that was created using `napi_define_class()`. + - `[in] native_object`: The native instance that will be wrapped in the + JavaScript object. + - `[in] finalize_cb`: Optional native callback that can be used to free the + native instance when the JavaScript object is ready for garbage-collection. + - `[in] finalize_hint`: Optional contextual hint that is passed to the + finalize callback. + - `[out] result`: Optional reference to the wrapped object. + +Returns `napi_ok` if the API succeeded. + +Wraps a native instance in JavaScript object of the corresponding type. + +When JavaScript code invokes a constructor for a class that was defined using +`napi_define_class()`, the `napi_callback` for the constructor is invoked. +After constructing an instance of the native class, the callback must then call +`napi_wrap()` to wrap the newly constructed instance in the already-created +JavaScript object that is the `this` argument to the constructor callback. +(That `this` object was created from the constructor function's `prototype`, +so it already has definitions of all the instance properties and methods.) + +Typically when wrapping a class instance, a finalize callback should be +provided that simply deletes the native instance that is received as the `data` +argument to the finalize callback. + +The optional returned reference is initially a weak reference, meaning it +has a reference count of 0. Typically this reference count would be incremented +temporarily during async operations that require the instance to remain valid. + +Caution: The optional returned reference (if obtained) should be deleted via +[`napi_delete_reference`][] ONLY in response to the finalize callback invocation. +(If it is deleted before then, then the finalize callback may never be +invoked.) Therefore when obtaining a reference a finalize callback is also +required in order to enable correct proper of the reference. + +### *napi_unwrap* + +```C +napi_status napi_unwrap(napi_env env, + napi_value js_object, + void** result); +``` + + - `[in] env`: The environment that the API is invoked under. + - `[in] js_object`: The object associated with the C++ class instance. + - `[out] result`: Pointer to the wrapped C++ class instance. + +Returns `napi_ok` if the API succeeded. + +When JavaScript code invokes a method or property accessor on the class, the +corresponding `napi_callback` is invoked. If the callback is for an instance +method or accessor, then the `this` argument to the callback is the wrapper +object; the wrapped C++ instance that is the target of the call can be obtained +then by calling `napi_unwrap()` on the wrapper object. + +## Asynchronous Operations + +Addon modules often need to leverage async helpers from libuv as part of their +implementation. This allows them to schedule work to be executed asynchronously +so that their methods can return in advance of the work being completed. This +is important in order to allow them to avoid blocking overall execution +of the Node.js application. + +N-API provides an ABI-stable interface for these +supporting functions which covers the most common asynchronous use cases. + +N-API defines the `napi_work` structure which is used to manage +asynchronous workers. Instances are created/deleted with +[`napi_create_async_work`][] and [`napi_delete_async_work`][]. + +The `execute` and `complete` callbacks are functions that will be +invoked when the executor is ready to execute and when it completes its +task respectively. These functions implement the following interfaces: + +```C +typedef void (*napi_async_execute_callback)(napi_env env, + void* data); +typedef void (*napi_async_complete_callback)(napi_env env, + napi_status status, + void* data); +``` + + +When these methods are invoked, the `data` parameter passed will be the +addon-provided void* data that was passed into the +`napi_create_async_work` call. + +Once created the async worker can be queued +for execution using the [`napi_queue_async_work`][] function: + +```C +NAPI_EXTERN napi_status napi_queue_async_work(napi_env env, + napi_async_work work); +``` + +[`napi_cancel_async_work`][] can be used if the work needs +to be cancelled before the work has started execution. + +After calling [`napi_cancel_async_work`][], the `complete` callback +will be invoked with a status value of `napi_cancelled`. +The work should not be deleted before the `complete` +callback invocation, even when it was cancelled. + +**Note:** As mentioned in the section on memory management, if +the code to be run in the callbacks will create N-API values, then +N-API handle scope functions must be used to create/destroy a +`napi_handle_scope` such that the scope is active when +objects can be created. + + +### napi_create_async_work + +```C +NAPI_EXTERN +napi_status napi_create_async_work(napi_env env, + napi_async_execute_callback execute, + napi_async_complete_callback complete, + void* data, + napi_async_work* result); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] execute`: The native function which should be called to excute +the logic asynchronously. +- `[in] complete`: The native function which will be called when the +asynchronous logic is comple or is cancelled. +- `[in] data`: User-provided data context. This will be passed back into the +execute and complete functions. +- `[out] result`: `napi_async_work*` which is the handle to the newly created +async work. + +Returns `napi_ok` if the API succeeded. + +This API allocates a work object that is used to execute logic asynchronously. +It should be freed using [`napi_delete_async_work`][] once the work is no longer +required. + +### napi_delete_async_work + +```C +NAPI_EXTERN napi_status napi_delete_async_work(napi_env env, + napi_async_work work); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] work`: The handle returned by the call to `napi_create_async_work`. + +Returns `napi_ok` if the API succeeded. + +This API frees a previously allocated work object. + +### napi_queue_async_work + +```C +NAPI_EXTERN napi_status napi_queue_async_work(napi_env env, + napi_async_work work); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] work`: The handle returned by the call to `napi_create_async_work`. + +Returns `napi_ok` if the API succeeded. + +This API requests that the previously allocated work be scheduled +for execution. + +### napi_cancel_async_work + +```C +NAPI_EXTERN napi_status napi_cancel_async_work(napi_env env, + napi_async_work work); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] work`: The handle returned by the call to `napi_create_async_work`. + +Returns `napi_ok` if the API succeeded. + +This API cancels a previously allocated work, provided +it has not yet been queued for execution. After this function is called +the `complete` callback will be invoked with a status value of +`napi_cancelled`. The work should not be deleted before the `complete` +callback invocation, even when it was cancelled. + + +[Aynchronous Operations]: #n_api_asynchronous_operations +[Basic N-API Data Types]: #n_api_basic_n_api_data_types +[ECMAScript Language Specification]: https://tc39.github.io/ecma262/ +[Error Handling]: #n_api_error_handling +[Module Registration]: #n_api_module_registration +[Native Abstractions for Node.js]: https://github.com/nodejs/nan +[Object Lifetime Management]: #n_api_object_lifetime_management +[Object Wrap]: #n_api_object_wrap +[Section 9.1.6]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc +[Section 12.5.5]: https://tc39.github.io/ecma262/#sec-typeof-operator +[Working with JavaScript Functions]: #n_api_working_with_javascript_functions +[Working with JavaScript Properties]: #n_api_working_with_javascript_properties +[Working with JavaScript Values]: #n_api_working_with_javascript_values +[Working with JavaScript Values - Abstract Operations]: #n_api_working_with_javascript_values_abstract_operations + +[`napi_cancel_async_work`]: #n_api_napi_cancel_async_work +[`napi_close_escapable_handle_scope`]: #n_api_napi_close_escapable_handle_scope +[`napi_close_handle_scope`]: #n_api_napi_close_handle_scope +[`napi_create_async_work`]: #n_api_napi_create_async_work +[`napi_create_error`]: #n_api_napi_create_error +[`napi_create_external_arraybuffer`][]: #n_api_napi_create_external_arraybuffer +[`napi_create_range_error`]: #n_api_napi_create_range_error +[`napi_create_reference`]: #n_api_napi_create_reference +[`napi_create_type_error`]: #n_api_napi_create_type_error +[`napi_define_class`]: #n_api_napi_define_class +[`napi_delete_async_work`]: #n_api_napi_delete_async_work +[`napi_define_class`][]: #n_api_napi_define_class +[`napi_delete_reference`]: #n_api_napi_delete_reference +[`napi_escape_handle`]: #n_api_napi_escape_handle +[`napi_get_array_length`]: #n_api_napi_get_array_length +[`napi_get_element`]: #n_api_napi_get_element +[`napi_get_property`]: #n_api_napi_get_property +[`napi_has_property`]: #n_api_napi_has_property +[`napi_set_property`]: #n_api_napi_set_property +[`napi_get_reference_value`]: #n_api_napi_get_reference_value +[`napi_is_error`]: #n_api_napi_is_error +[`napi_is_exception_pending`]: #n_api_napi_is_exception_pending +[`napi_get_last_error_info`]: #n_api_napi_get_last_error_info +[`napi_get_and_clear_last_exception`]: #n_api_napi_get_and_clear_last_exception +[`napi_make_callback`]: #n_api_napi_make_callback +[`napi_open_escapable_handle_scope`]: #n_api_napi_open_escapable_handle_scope +[`napi_open_handle_scope`]: #n_api_napi_open_handle_scope +[`napi_property_descriptor`]: #n_api_napi_property_descriptor +[`napi_queue_async_work`]: #n_api_napi_queue_async_work +[`napi_reference_ref`]: #n_api_napi_reference_ref +[`napi_reference_unref`]: #n_api_napi_reference_unref +[`napi_throw_error`]: #n_api_napi_throw_error +[`napi_throw_range_error`]: #n_api_napi_throw_range_error +[`napi_throw_type_error`]: #n_api_napi_throw_type_error +[`napi_unwrap`]: #n_api_napi_unwrap +[`napi_wrap`]: #n_api_napi_wrap From 3461bc13da995e90031c18b8e9d7237d7b5c0106 Mon Sep 17 00:00:00 2001 From: gwer Date: Sun, 23 Apr 2017 02:01:54 +0300 Subject: [PATCH 026/183] test: replace indexOf with includes Start the transition to Array.prototype.includes() and String.prototype.includes(). This commit refactors most of the comparisons of Array.prototype.indexOf() and String.prototype.indexOf() return values with -1 to the former methods in tests. PR-URL: https://github.com/nodejs/node/pull/12604 Refs: https://github.com/nodejs/node/issues/12586 Reviewed-By: Alexey Orlenko Reviewed-By: James M Snell Reviewed-By: Gibson Fahnestock --- test/addons-napi/test_constructor/test.js | 16 ++++++++-------- test/addons-napi/test_properties/test.js | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/test/addons-napi/test_constructor/test.js b/test/addons-napi/test_constructor/test.js index 92440bf49e74c1..26083db7a28a21 100644 --- a/test/addons-napi/test_constructor/test.js +++ b/test/addons-napi/test_constructor/test.js @@ -22,14 +22,14 @@ const propertyNames = []; for (const name in test_object) { propertyNames.push(name); } -assert.ok(propertyNames.indexOf('echo') >= 0); -assert.ok(propertyNames.indexOf('readwriteValue') >= 0); -assert.ok(propertyNames.indexOf('readonlyValue') >= 0); -assert.ok(propertyNames.indexOf('hiddenValue') < 0); -assert.ok(propertyNames.indexOf('readwriteAccessor1') < 0); -assert.ok(propertyNames.indexOf('readwriteAccessor2') < 0); -assert.ok(propertyNames.indexOf('readonlyAccessor1') < 0); -assert.ok(propertyNames.indexOf('readonlyAccessor2') < 0); +assert.ok(propertyNames.includes('echo')); +assert.ok(propertyNames.includes('readwriteValue')); +assert.ok(propertyNames.includes('readonlyValue')); +assert.ok(!propertyNames.includes('hiddenValue')); +assert.ok(!propertyNames.includes('readwriteAccessor1')); +assert.ok(!propertyNames.includes('readwriteAccessor2')); +assert.ok(!propertyNames.includes('readonlyAccessor1')); +assert.ok(!propertyNames.includes('readonlyAccessor2')); // The napi_writable attribute should be ignored for accessors. test_object.readwriteAccessor1 = 1; diff --git a/test/addons-napi/test_properties/test.js b/test/addons-napi/test_properties/test.js index 868c603879f1a2..a8127a27860eb3 100644 --- a/test/addons-napi/test_properties/test.js +++ b/test/addons-napi/test_properties/test.js @@ -21,14 +21,14 @@ const propertyNames = []; for (const name in test_object) { propertyNames.push(name); } -assert.ok(propertyNames.indexOf('echo') >= 0); -assert.ok(propertyNames.indexOf('readwriteValue') >= 0); -assert.ok(propertyNames.indexOf('readonlyValue') >= 0); -assert.ok(propertyNames.indexOf('hiddenValue') < 0); -assert.ok(propertyNames.indexOf('readwriteAccessor1') < 0); -assert.ok(propertyNames.indexOf('readwriteAccessor2') < 0); -assert.ok(propertyNames.indexOf('readonlyAccessor1') < 0); -assert.ok(propertyNames.indexOf('readonlyAccessor2') < 0); +assert.ok(propertyNames.includes('echo')); +assert.ok(propertyNames.includes('readwriteValue')); +assert.ok(propertyNames.includes('readonlyValue')); +assert.ok(!propertyNames.includes('hiddenValue')); +assert.ok(!propertyNames.includes('readwriteAccessor1')); +assert.ok(!propertyNames.includes('readwriteAccessor2')); +assert.ok(!propertyNames.includes('readonlyAccessor1')); +assert.ok(!propertyNames.includes('readonlyAccessor2')); // The napi_writable attribute should be ignored for accessors. test_object.readwriteAccessor1 = 1; From b91ac1014e014ddf006f4ca7727d04645285a8ce Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Thu, 27 Apr 2017 17:27:05 -0700 Subject: [PATCH 027/183] test,lib,doc: use function declarations Replace function expressions with function declarations in preparation for a lint rule requiring function declarations. PR-URL: https://github.com/nodejs/node/pull/12711 Reviewed-By: Vse Mozhet Byt Reviewed-By: Gibson Fahnestock --- test/addons-napi/test_async/test.js | 5 ++--- test/addons-napi/test_exception/test.js | 6 +++--- test/addons-napi/test_instanceof/test.js | 4 ++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/test/addons-napi/test_async/test.js b/test/addons-napi/test_async/test.js index 7c140d79fc054f..2b4577624a371e 100644 --- a/test/addons-napi/test_async/test.js +++ b/test/addons-napi/test_async/test.js @@ -6,8 +6,7 @@ const test_async = require(`./build/${common.buildType}/test_async`); test_async.Test(5, common.mustCall(function(err, val) { assert.strictEqual(err, null); assert.strictEqual(val, 10); - process.nextTick(common.mustCall(function() {})); + process.nextTick(common.mustCall()); })); -const cancelSuceeded = function() {}; -test_async.TestCancel(common.mustCall(cancelSuceeded)); +test_async.TestCancel(common.mustCall()); diff --git a/test/addons-napi/test_exception/test.js b/test/addons-napi/test_exception/test.js index 83d2b5000ec54a..94f9566a4b341f 100644 --- a/test/addons-napi/test_exception/test.js +++ b/test/addons-napi/test_exception/test.js @@ -4,12 +4,12 @@ const common = require('../../common'); const test_exception = require(`./build/${common.buildType}/test_exception`); const assert = require('assert'); const theError = new Error('Some error'); -const throwTheError = function() { +function throwTheError() { throw theError; -}; +} let caughtError; -const throwNoError = function() {}; +const throwNoError = common.noop; // Test that the native side successfully captures the exception let returnedError = test_exception.returnException(throwTheError); diff --git a/test/addons-napi/test_instanceof/test.js b/test/addons-napi/test_instanceof/test.js index 38d17031e9a6c9..418149d1909e6f 100644 --- a/test/addons-napi/test_instanceof/test.js +++ b/test/addons-napi/test_instanceof/test.js @@ -57,14 +57,14 @@ if (typeof Symbol !== 'undefined' && 'hasInstance' in Symbol && (theObject instanceof theConstructor)); } - const MyClass = function MyClass() {}; + function MyClass() {} Object.defineProperty(MyClass, Symbol.hasInstance, { value: function(candidate) { return 'mark' in candidate; } }); - const MySubClass = function MySubClass() {}; + function MySubClass() {} MySubClass.prototype = new MyClass(); let x = new MySubClass(); From e89a91172b2cc7e5097d2b3791b0d9f1d2a8064b Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Thu, 27 Apr 2017 22:15:36 +0300 Subject: [PATCH 028/183] n-api: remove unnecessary try-catch bracket from certain APIs These APIs do not need a try-catch around their body, because no exceptions are thrown in their implementation: - `napi_is_array()` - `napi_get_value_string_latin1()` - `napi_get_value_string_utf8()` - `napi_get_value_string_utf16()` - `napi_get_value_external()` - `napi_is_buffer()` - `napi_is_arraybuffer()` - `napi_get_arraybuffer_info()` - `napi_is_typedarray()` - `napi_get_typedarray_info()` Fixes: https://github.com/nodejs/abi-stable-node/issues/238 PR-URL: https://github.com/nodejs/node/pull/12705 Reviewed-By: Michael Dawson Reviewed-By: Colin Ihrig Reviewed-By: Jason Ginchereau Reviewed-By: Anna Henningsen --- src/node_api.cc | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index d1ca7bec27c269..4113124b093419 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -1209,14 +1209,14 @@ napi_status napi_define_properties(napi_env env, } napi_status napi_is_array(napi_env env, napi_value value, bool* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, value); CHECK_ARG(env, result); v8::Local val = v8impl::V8LocalValueFromJsValue(value); *result = val->IsArray(); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_get_array_length(napi_env env, @@ -1777,7 +1777,7 @@ napi_status napi_get_value_string_latin1(napi_env env, char* buf, size_t bufsize, size_t* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, value); v8::Local val = v8impl::V8LocalValueFromJsValue(value); @@ -1797,7 +1797,7 @@ napi_status napi_get_value_string_latin1(napi_env env, } } - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } // Copies a JavaScript string into a UTF-8 string buffer. The result is the @@ -1813,7 +1813,7 @@ napi_status napi_get_value_string_utf8(napi_env env, char* buf, size_t bufsize, size_t* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, value); v8::Local val = v8impl::V8LocalValueFromJsValue(value); @@ -1833,7 +1833,7 @@ napi_status napi_get_value_string_utf8(napi_env env, } } - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } // Copies a JavaScript string into a UTF-16 string buffer. The result is the @@ -1849,7 +1849,7 @@ napi_status napi_get_value_string_utf16(napi_env env, char16_t* buf, size_t bufsize, size_t* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, value); v8::Local val = v8impl::V8LocalValueFromJsValue(value); @@ -1870,7 +1870,7 @@ napi_status napi_get_value_string_utf16(napi_env env, } } - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_coerce_to_object(napi_env env, @@ -2024,13 +2024,13 @@ napi_status napi_create_external(napi_env env, *result = v8impl::JsValueFromV8LocalValue(external_value); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_get_value_external(napi_env env, napi_value value, void** result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, value); CHECK_ARG(env, result); @@ -2040,7 +2040,7 @@ napi_status napi_get_value_external(napi_env env, v8::Local external_value = val.As(); *result = external_value->Value(); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } // Set initial_refcount to 0 for a weak reference, >0 for a strong reference. @@ -2481,12 +2481,12 @@ napi_status napi_create_buffer_copy(napi_env env, } napi_status napi_is_buffer(napi_env env, napi_value value, bool* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, value); CHECK_ARG(env, result); *result = node::Buffer::HasInstance(v8impl::V8LocalValueFromJsValue(value)); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_get_buffer_info(napi_env env, @@ -2510,14 +2510,14 @@ napi_status napi_get_buffer_info(napi_env env, } napi_status napi_is_arraybuffer(napi_env env, napi_value value, bool* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, value); CHECK_ARG(env, result); v8::Local val = v8impl::V8LocalValueFromJsValue(value); *result = val->IsArrayBuffer(); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_create_arraybuffer(napi_env env, @@ -2574,7 +2574,7 @@ napi_status napi_get_arraybuffer_info(napi_env env, napi_value arraybuffer, void** data, size_t* byte_length) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, arraybuffer); v8::Local value = v8impl::V8LocalValueFromJsValue(arraybuffer); @@ -2591,18 +2591,18 @@ napi_status napi_get_arraybuffer_info(napi_env env, *byte_length = contents.ByteLength(); } - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_is_typedarray(napi_env env, napi_value value, bool* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, value); CHECK_ARG(env, result); v8::Local val = v8impl::V8LocalValueFromJsValue(value); *result = val->IsTypedArray(); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_create_typedarray(napi_env env, @@ -2664,7 +2664,7 @@ napi_status napi_get_typedarray_info(napi_env env, void** data, napi_value* arraybuffer, size_t* byte_offset) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, typedarray); v8::Local value = v8impl::V8LocalValueFromJsValue(typedarray); @@ -2712,7 +2712,7 @@ napi_status napi_get_typedarray_info(napi_env env, *byte_offset = array->ByteOffset(); } - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } namespace uvimpl { From f14a0bb840437a39c5bbaa1bc24e259414284577 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Fri, 28 Apr 2017 15:56:21 -0400 Subject: [PATCH 029/183] test: fix warning in n-api reference test Add cast to avoid warning during build of addon. PR-URL: https://github.com/nodejs/node/pull/12730 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Anna Henningsen --- test/addons-napi/test_reference/test_reference.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/addons-napi/test_reference/test_reference.c b/test/addons-napi/test_reference/test_reference.c index 1a238a560aac53..9be9fd135fb0e9 100644 --- a/test/addons-napi/test_reference/test_reference.c +++ b/test/addons-napi/test_reference/test_reference.c @@ -60,10 +60,10 @@ napi_value CheckExternal(napi_env env, napi_callback_info info) { NAPI_ASSERT(env, argtype == napi_external, "Expected an external value.") - int* data; + void* data; NAPI_CALL(env, napi_get_value_external(env, arg, &data)); - NAPI_ASSERT(env, data != NULL && *data == test_value, + NAPI_ASSERT(env, data != NULL && *(int*)data == test_value, "An external data value of 1 was expected.") return NULL; From 269e921a8b675718a0fbd7f95e0007996cbcc7b8 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Fri, 28 Apr 2017 15:28:34 -0400 Subject: [PATCH 030/183] test: add coverage for error apis Add coverage for N-API functions related to throwing and creating errors. A number of these are currently showing as not having any coverage in the nightly code coverage reports. PR-URL: https://github.com/nodejs/node/pull/12729 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- test/addons-napi/test_error/test.js | 34 ++++++++++++++ test/addons-napi/test_error/test_error.cc | 55 +++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/test/addons-napi/test_error/test.js b/test/addons-napi/test_error/test.js index 521c29250d5ffa..f7479f2c9a64d8 100644 --- a/test/addons-napi/test_error/test.js +++ b/test/addons-napi/test_error/test.js @@ -55,3 +55,37 @@ assert.strictEqual(test_error.checkError({}), false, // Test that non-error primitive is correctly classed assert.strictEqual(test_error.checkError('non-object'), false, 'Non-error primitive correctly classed by napi_is_error'); + +assert.throws(() => { + test_error.throwExistingError(); +}, /^Error: existing error$/); + +assert.throws(() => { + test_error.throwError(); +}, /^Error: error$/); + +assert.throws(() => { + test_error.throwRangeError(); +}, /^RangeError: range error$/); + +assert.throws(() => { + test_error.throwTypeError(); +}, /^TypeError: type error$/); + +let error = test_error.createError(); +assert.ok(error instanceof Error, 'expected error to be an instance of Error'); +assert.strictEqual(error.message, 'error', 'expected message to be "error"'); + +error = test_error.createRangeError(); +assert.ok(error instanceof RangeError, + 'expected error to be an instance of RangeError'); +assert.strictEqual(error.message, + 'range error', + 'expected message to be "range error"'); + +error = test_error.createTypeError(); +assert.ok(error instanceof TypeError, + 'expected error to be an instance of TypeError'); +assert.strictEqual(error.message, + 'type error', + 'expected message to be "type error"'); diff --git a/test/addons-napi/test_error/test_error.cc b/test/addons-napi/test_error/test_error.cc index eb616cac371b0c..ddba2059f23be6 100644 --- a/test/addons-napi/test_error/test_error.cc +++ b/test/addons-napi/test_error/test_error.cc @@ -15,9 +15,64 @@ napi_value checkError(napi_env env, napi_callback_info info) { return result; } +napi_value throwExistingError(napi_env env, napi_callback_info info) { + napi_value message; + napi_value error; + NAPI_CALL(env, napi_create_string_utf8(env, "existing error", -1, &message)); + NAPI_CALL(env, napi_create_error(env, message, &error)); + NAPI_CALL(env, napi_throw(env, error)); + return nullptr; +} + +napi_value throwError(napi_env env, napi_callback_info info) { + NAPI_CALL(env, napi_throw_error(env, "error")); + return nullptr; +} + +napi_value throwRangeError(napi_env env, napi_callback_info info) { + NAPI_CALL(env, napi_throw_range_error(env, "range error")); + return nullptr; +} + +napi_value throwTypeError(napi_env env, napi_callback_info info) { + NAPI_CALL(env, napi_throw_type_error(env, "type error")); + return nullptr; +} + +napi_value createError(napi_env env, napi_callback_info info) { + napi_value result; + napi_value message; + NAPI_CALL(env, napi_create_string_utf8(env, "error", -1, &message)); + NAPI_CALL(env, napi_create_error(env, message, &result)); + return result; +} + +napi_value createRangeError(napi_env env, napi_callback_info info) { + napi_value result; + napi_value message; + NAPI_CALL(env, napi_create_string_utf8(env, "range error", -1, &message)); + NAPI_CALL(env, napi_create_range_error(env, message, &result)); + return result; +} + +napi_value createTypeError(napi_env env, napi_callback_info info) { + napi_value result; + napi_value message; + NAPI_CALL(env, napi_create_string_utf8(env, "type error", -1, &message)); + NAPI_CALL(env, napi_create_type_error(env, message, &result)); + return result; +} + void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("checkError", checkError), + DECLARE_NAPI_PROPERTY("throwExistingError", throwExistingError), + DECLARE_NAPI_PROPERTY("throwError", throwError), + DECLARE_NAPI_PROPERTY("throwRangeError", throwRangeError), + DECLARE_NAPI_PROPERTY("throwTypeError", throwTypeError), + DECLARE_NAPI_PROPERTY("createError", createError), + DECLARE_NAPI_PROPERTY("createRangeError", createRangeError), + DECLARE_NAPI_PROPERTY("createTypeError", createTypeError), }; NAPI_CALL_RETURN_VOID(env, napi_define_properties( From ed2ef69c55fd2a5ea40a2086820dde1278b294e7 Mon Sep 17 00:00:00 2001 From: Hitesh Kanwathirtha Date: Thu, 13 Apr 2017 20:21:52 -0700 Subject: [PATCH 031/183] test: port test for make_callback to n-api Improved test coverage for napi_make_callback by porting the existing addons/make_callback test to n-api PR-URL: https://github.com/nodejs/node/pull/12409 Reviewed-By: Anna Henningsen Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell Reviewed-By: Refael Ackermann --- .../addons-napi/test_make_callback/binding.cc | 48 ++++++ .../test_make_callback/binding.gyp | 9 ++ test/addons-napi/test_make_callback/test.js | 83 ++++++++++ .../test_make_callback_recurse/binding.cc | 32 ++++ .../test_make_callback_recurse/binding.gyp | 9 ++ .../test_make_callback_recurse/test.js | 151 ++++++++++++++++++ 6 files changed, 332 insertions(+) create mode 100644 test/addons-napi/test_make_callback/binding.cc create mode 100644 test/addons-napi/test_make_callback/binding.gyp create mode 100644 test/addons-napi/test_make_callback/test.js create mode 100644 test/addons-napi/test_make_callback_recurse/binding.cc create mode 100644 test/addons-napi/test_make_callback_recurse/binding.gyp create mode 100644 test/addons-napi/test_make_callback_recurse/test.js diff --git a/test/addons-napi/test_make_callback/binding.cc b/test/addons-napi/test_make_callback/binding.cc new file mode 100644 index 00000000000000..987b024098598c --- /dev/null +++ b/test/addons-napi/test_make_callback/binding.cc @@ -0,0 +1,48 @@ +#include +#include "../common.h" +#include + +namespace { + +napi_value MakeCallback(napi_env env, napi_callback_info info) { + const int kMaxArgs = 10; + size_t argc = kMaxArgs; + napi_value args[kMaxArgs]; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); + + NAPI_ASSERT(env, argc > 0, "Wrong number of arguments"); + + napi_value recv = args[0]; + napi_value func = args[1]; + + std::vector argv; + for (size_t n = 2; n < argc; n += 1) { + argv.push_back(args[n]); + } + + napi_valuetype func_type; + + NAPI_CALL(env, napi_typeof(env, func, &func_type)); + + napi_value result; + if (func_type == napi_function) { + NAPI_CALL(env, + napi_make_callback(env, recv, func, argv.size(), argv.data(), &result)); + } else { + NAPI_ASSERT(env, false, "Unexpected argument type"); + } + + return result; +} + +void Init(napi_env env, napi_value exports, napi_value module, void* priv) { + napi_value fn; + NAPI_CALL_RETURN_VOID(env, + napi_create_function(env, NULL, MakeCallback, NULL, &fn)); + NAPI_CALL_RETURN_VOID(env, + napi_set_named_property(env, exports, "makeCallback", fn)); +} + +} // namespace + +NAPI_MODULE(binding, Init) diff --git a/test/addons-napi/test_make_callback/binding.gyp b/test/addons-napi/test_make_callback/binding.gyp new file mode 100644 index 00000000000000..7ede63d94a0d77 --- /dev/null +++ b/test/addons-napi/test_make_callback/binding.gyp @@ -0,0 +1,9 @@ +{ + 'targets': [ + { + 'target_name': 'binding', + 'defines': [ 'V8_DEPRECATION_WARNINGS=1' ], + 'sources': [ 'binding.cc' ] + } + ] +} diff --git a/test/addons-napi/test_make_callback/test.js b/test/addons-napi/test_make_callback/test.js new file mode 100644 index 00000000000000..c4f24872bdb78e --- /dev/null +++ b/test/addons-napi/test_make_callback/test.js @@ -0,0 +1,83 @@ +'use strict'; + +const common = require('../../common'); +const assert = require('assert'); +const vm = require('vm'); +const binding = require(`./build/${common.buildType}/binding`); +const makeCallback = binding.makeCallback; + +function myMultiArgFunc(arg1, arg2, arg3) { + console.log(`MyFunc was called with ${arguments.length} arguments`); + assert.strictEqual(arg1, 1); + assert.strictEqual(arg2, 2); + assert.strictEqual(arg3, 3); + return 42; +} + +assert.strictEqual(42, makeCallback(process, common.mustCall(function() { + assert.strictEqual(0, arguments.length); + assert.strictEqual(this, process); + return 42; +}))); + +assert.strictEqual(42, makeCallback(process, common.mustCall(function(x) { + assert.strictEqual(1, arguments.length); + assert.strictEqual(this, process); + assert.strictEqual(x, 1337); + return 42; +}), 1337)); + +assert.strictEqual(42, + makeCallback(this, + common.mustCall(myMultiArgFunc), 1, 2, 3)); + +// TODO(node-api): napi_make_callback needs to support +// strings passed for the func argument +/* +const recv = { + one: common.mustCall(function() { + assert.strictEqual(0, arguments.length); + assert.strictEqual(this, recv); + return 42; + }), + two: common.mustCall(function(x) { + assert.strictEqual(1, arguments.length); + assert.strictEqual(this, recv); + assert.strictEqual(x, 1337); + return 42; + }), +}; + +assert.strictEqual(42, makeCallback(recv, 'one')); +assert.strictEqual(42, makeCallback(recv, 'two', 1337)); + +// Check that callbacks on a receiver from a different context works. +const foreignObject = vm.runInNewContext('({ fortytwo() { return 42; } })'); +assert.strictEqual(42, makeCallback(foreignObject, 'fortytwo')); +*/ + +// Check that the callback is made in the context of the receiver. +const target = vm.runInNewContext(` + (function($Object) { + if (Object === $Object) + throw new Error('bad'); + return Object; + }) +`); +assert.notStrictEqual(Object, makeCallback(process, target, Object)); + +// Runs in inner context. +const forward = vm.runInNewContext(` + (function(forward) { + return forward(Object); + }) +`); + +// Runs in outer context. +function endpoint($Object) { + if (Object === $Object) + throw new Error('bad'); + return Object; +} + +assert.strictEqual(Object, makeCallback(process, forward, endpoint)); diff --git a/test/addons-napi/test_make_callback_recurse/binding.cc b/test/addons-napi/test_make_callback_recurse/binding.cc new file mode 100644 index 00000000000000..3f5a4c28b43524 --- /dev/null +++ b/test/addons-napi/test_make_callback_recurse/binding.cc @@ -0,0 +1,32 @@ +#include +#include "../common.h" +#include + +namespace { + +napi_value MakeCallback(napi_env env, napi_callback_info info) { + size_t argc = 2; + napi_value args[2]; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); + + napi_value recv = args[0]; + napi_value func = args[1]; + + napi_make_callback(env, + recv, func, 0 /* argc */, nullptr /* argv */, nullptr /* result */); + + return recv; +} + +void Init(napi_env env, napi_value exports, napi_value module, void* priv) { + napi_value fn; + NAPI_CALL_RETURN_VOID(env, + napi_create_function(env, NULL, MakeCallback, NULL, &fn)); + NAPI_CALL_RETURN_VOID(env, + napi_set_named_property(env, exports, "makeCallback", fn)); +} + + +} // namespace + +NAPI_MODULE(binding, Init) diff --git a/test/addons-napi/test_make_callback_recurse/binding.gyp b/test/addons-napi/test_make_callback_recurse/binding.gyp new file mode 100644 index 00000000000000..7ede63d94a0d77 --- /dev/null +++ b/test/addons-napi/test_make_callback_recurse/binding.gyp @@ -0,0 +1,9 @@ +{ + 'targets': [ + { + 'target_name': 'binding', + 'defines': [ 'V8_DEPRECATION_WARNINGS=1' ], + 'sources': [ 'binding.cc' ] + } + ] +} diff --git a/test/addons-napi/test_make_callback_recurse/test.js b/test/addons-napi/test_make_callback_recurse/test.js new file mode 100644 index 00000000000000..895769bc33029a --- /dev/null +++ b/test/addons-napi/test_make_callback_recurse/test.js @@ -0,0 +1,151 @@ +'use strict'; + +const common = require('../../common'); +const assert = require('assert'); +const domain = require('domain'); +const binding = require(`./build/${common.buildType}/binding`); +const makeCallback = binding.makeCallback; + +// Make sure this is run in the future. +const mustCallCheckDomains = common.mustCall(checkDomains); + + +// Make sure that using MakeCallback allows the error to propagate. +assert.throws(function() { + makeCallback({}, function() { + throw new Error('hi from domain error'); + }); +}, /^Error: hi from domain error$/); + + +// Check the execution order of the nextTickQueue and MicrotaskQueue in +// relation to running multiple MakeCallback's from bootstrap, +// node::MakeCallback() and node::AsyncWrap::MakeCallback(). +// TODO(trevnorris): Is there a way to verify this is being run during +// bootstrap? +(function verifyExecutionOrder(arg) { + const results = []; + + // Processing of the MicrotaskQueue is manually handled by node. They are not + // processed until after the nextTickQueue has been processed. + Promise.resolve(1).then(common.mustCall(function() { + results.push(7); + })); + + // The nextTick should run after all immediately invoked calls. + process.nextTick(common.mustCall(function() { + results.push(3); + + // Run same test again but while processing the nextTickQueue to make sure + // the following MakeCallback call breaks in the middle of processing the + // queue and allows the script to run normally. + process.nextTick(common.mustCall(function() { + results.push(6); + })); + + makeCallback({}, common.mustCall(function() { + results.push(4); + })); + + results.push(5); + })); + + results.push(0); + + // MakeCallback is calling the function immediately, but should then detect + // that a script is already in the middle of execution and return before + // either the nextTickQueue or MicrotaskQueue are processed. + makeCallback({}, common.mustCall(function() { + results.push(1); + })); + + // This should run before either the nextTickQueue or MicrotaskQueue are + // processed. Previously MakeCallback would not detect this circumstance + // and process them immediately. + results.push(2); + + setImmediate(common.mustCall(function() { + for (let i = 0; i < results.length; i++) { + assert.strictEqual(results[i], i, + `verifyExecutionOrder(${arg}) results: ${results}`); + } + if (arg === 1) { + // The tests are first run on bootstrap during LoadEnvironment() in + // src/node.cc. Now run the tests through node::MakeCallback(). + setImmediate(function() { + makeCallback({}, common.mustCall(function() { + verifyExecutionOrder(2); + })); + }); + } else if (arg === 2) { + // setTimeout runs via the TimerWrap, which runs through + // AsyncWrap::MakeCallback(). Make sure there are no conflicts using + // node::MakeCallback() within it. + setTimeout(common.mustCall(function() { + verifyExecutionOrder(3); + }), 10); + } else if (arg === 3) { + mustCallCheckDomains(); + } else { + throw new Error('UNREACHABLE'); + } + })); +}(1)); + + +function checkDomains() { + // Check that domains are properly entered/exited when called in multiple + // levels from both node::MakeCallback() and AsyncWrap::MakeCallback + setImmediate(common.mustCall(function() { + const d1 = domain.create(); + const d2 = domain.create(); + const d3 = domain.create(); + + makeCallback({domain: d1}, common.mustCall(function() { + assert.strictEqual(d1, process.domain); + makeCallback({domain: d2}, common.mustCall(function() { + assert.strictEqual(d2, process.domain); + makeCallback({domain: d3}, common.mustCall(function() { + assert.strictEqual(d3, process.domain); + })); + assert.strictEqual(d2, process.domain); + })); + assert.strictEqual(d1, process.domain); + })); + })); + + setTimeout(common.mustCall(function() { + const d1 = domain.create(); + const d2 = domain.create(); + const d3 = domain.create(); + + makeCallback({domain: d1}, common.mustCall(function() { + assert.strictEqual(d1, process.domain); + makeCallback({domain: d2}, common.mustCall(function() { + assert.strictEqual(d2, process.domain); + makeCallback({domain: d3}, common.mustCall(function() { + assert.strictEqual(d3, process.domain); + })); + assert.strictEqual(d2, process.domain); + })); + assert.strictEqual(d1, process.domain); + })); + }), 1); + + function testTimer(id) { + // Make sure nextTick, setImmediate and setTimeout can all recover properly + // after a thrown makeCallback call. + const d = domain.create(); + d.on('error', common.mustCall(function(e) { + assert.strictEqual(e.message, `throw from domain ${id}`); + })); + makeCallback({domain: d}, function() { + throw new Error(`throw from domain ${id}`); + }); + throw new Error('UNREACHABLE'); + } + + process.nextTick(common.mustCall(testTimer), 3); + setImmediate(common.mustCall(testTimer), 2); + setTimeout(common.mustCall(testTimer), 1, 1); +} From 7491cee8611b63eb95eb7bbde8335f37d229b7ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Fri, 5 May 2017 14:43:54 +0200 Subject: [PATCH 032/183] test: fix napi test_reference for recent V8 PR-URL: https://github.com/nodejs/node/pull/12864 Ref: https://github.com/nodejs/node/pull/12551#issuecomment-297949361 Reviewed-By: James M Snell Reviewed-By: Michael Dawson --- test/addons-napi/test_reference/test.js | 29 ++++++++++++++----------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/test/addons-napi/test_reference/test.js b/test/addons-napi/test_reference/test.js index ddfec58f1f9d7d..30effe7eec0922 100644 --- a/test/addons-napi/test_reference/test.js +++ b/test/addons-napi/test_reference/test.js @@ -33,19 +33,6 @@ assert.strictEqual(test_reference.finalizeCount, 0); assert.strictEqual(test_reference.finalizeCount, 1); } -{ - // Weak reference - let value = test_reference.createExternalWithFinalize(); - assert.strictEqual(test_reference.finalizeCount, 0); - test_reference.createReference(value, 0); - assert.strictEqual(test_reference.referenceValue, value); - value = null; - global.gc(); // Value should be GC'd because there is only a weak ref - assert.strictEqual(test_reference.referenceValue, undefined); - assert.strictEqual(test_reference.finalizeCount, 1); - test_reference.deleteReference(); -} - { // Strong reference let value = test_reference.createExternalWithFinalize(); @@ -85,3 +72,19 @@ assert.strictEqual(test_reference.finalizeCount, 0); global.gc(); // Value was already GC'd assert.strictEqual(test_reference.finalizeCount, 1); } + +{ + // Weak reference + let value = test_reference.createExternalWithFinalize(); + assert.strictEqual(test_reference.finalizeCount, 0); + test_reference.createReference(value, 0); + assert.strictEqual(test_reference.referenceValue, value); + value = null; + setImmediate(common.mustCall(() => { + // This test only works if gc() is called from an immediate callback. + global.gc(); // Value should be GC'd because there is only a weak ref + assert.strictEqual(test_reference.referenceValue, undefined); + assert.strictEqual(test_reference.finalizeCount, 1); + test_reference.deleteReference(); + })); +} From 61992ff08528cc78f3916fd1767beec48dfb3fc7 Mon Sep 17 00:00:00 2001 From: Jason Ginchereau Date: Fri, 5 May 2017 11:38:21 -0700 Subject: [PATCH 033/183] n-api: napi_get_cb_info should fill array When the number of args requested is greater than the actual number of args supplied to the function call, the remainder of the args array should be filled in with `undefined` values. Because of this bug, the remainder of the array was left uninitialized, which could cause a crash. Refer to the documentation for the `argv` parameter at https://github.com/nodejs/node/blob/master/doc/api/n-api.md#napi_get_cb_info PR-URL: https://github.com/nodejs/node/pull/12863 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Michael Dawson --- src/node_api.cc | 2 +- test/addons-napi/3_callbacks/binding.c | 17 +++++++++++++++-- test/addons-napi/test_function/test_function.c | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index 4113124b093419..2f8409fc81627c 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -1526,7 +1526,7 @@ napi_status napi_get_cb_info( if (argv != nullptr) { CHECK_ARG(env, argc); - info->Args(argv, std::min(*argc, info->ArgsLength())); + info->Args(argv, *argc); } if (argc != nullptr) { *argc = info->ArgsLength(); diff --git a/test/addons-napi/3_callbacks/binding.c b/test/addons-napi/3_callbacks/binding.c index 47360bd979ffc4..8640a936107d47 100644 --- a/test/addons-napi/3_callbacks/binding.c +++ b/test/addons-napi/3_callbacks/binding.c @@ -3,10 +3,23 @@ #include napi_value RunCallback(napi_env env, napi_callback_info info) { - size_t argc = 1; - napi_value args[1]; + size_t argc = 2; + napi_value args[2]; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); + NAPI_ASSERT(env, argc == 1, + "Wrong number of arguments. Expects a single argument."); + + napi_valuetype valuetype0; + NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0)); + NAPI_ASSERT(env, valuetype0 == napi_function, + "Wrong type of arguments. Expects a function as first argument."); + + napi_valuetype valuetype1; + NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1)); + NAPI_ASSERT(env, valuetype1 == napi_undefined, + "Additional arguments should be undefined."); + napi_value argv[1]; const char* str = "hello world"; size_t str_len = strlen(str); diff --git a/test/addons-napi/test_function/test_function.c b/test/addons-napi/test_function/test_function.c index 928f99c184cb57..4ce0203e7232dd 100644 --- a/test/addons-napi/test_function/test_function.c +++ b/test/addons-napi/test_function/test_function.c @@ -12,7 +12,7 @@ napi_value Test(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0)); NAPI_ASSERT(env, valuetype0 == napi_function, - "Wrong type of arguments. Expects a number as first argument."); + "Wrong type of arguments. Expects a function as first argument."); napi_value* argv = args + 1; argc = argc - 1; From 184ffb5dfe3a91ee4a939fb2709393a885c987d8 Mon Sep 17 00:00:00 2001 From: Jason Ginchereau Date: Thu, 4 May 2017 15:24:44 -0700 Subject: [PATCH 034/183] n-api: Handle fatal exception in async callback - Create a handle scope before invoking the async completion callback, because it is basically always needed, easy for user code to forget, and this makes it more consistent with ordinary N-API function callbacks. - Check for an unhandled JS exception after invoking an async completion callback, and report it via `node::FatalException()`. - Add a corresponding test case for an exception in async callback. Previously, any unhandled JS exception thrown from a `napi_async_complete_callback` would be silently ignored. Among other things this meant assertions in some test cases could be undetected. PR-URL: https://github.com/nodejs/node/pull/12838 Reviewed-By: Anna Henningsen Reviewed-By: Benjamin Gruenbaum Reviewed-By: Michael Dawson Reviewed-By: James M Snell --- doc/api/n-api.md | 7 ------- src/node_api.cc | 21 ++++++++++++++++++++- test/addons-napi/test_async/test.js | 18 ++++++++++++++++++ test/addons-napi/test_async/test_async.cc | 19 ------------------- 4 files changed, 38 insertions(+), 27 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index f9e47a7b8d32a0..bc0499e4d09447 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -2855,13 +2855,6 @@ will be invoked with a status value of `napi_cancelled`. The work should not be deleted before the `complete` callback invocation, even when it was cancelled. -**Note:** As mentioned in the section on memory management, if -the code to be run in the callbacks will create N-API values, then -N-API handle scope functions must be used to create/destroy a -`napi_handle_scope` such that the scope is active when -objects can be created. - - ### napi_create_async_work -Node.js Addons are dynamically-linked shared objects, written in C or C++, that +Node.js Addons are dynamically-linked shared objects, written in C++, that can be loaded into Node.js using the [`require()`][require] function, and used just as if they were an ordinary Node.js module. They are used primarily to provide an interface between JavaScript running in Node.js and C/C++ libraries. @@ -28,7 +28,7 @@ involving knowledge of several components and APIs : off-loading work via libuv to non-blocking system operations, worker threads or a custom use of libuv's threads. - - Internal Node.js libraries. Node.js itself exports a number of C/C++ APIs + - Internal Node.js libraries. Node.js itself exports a number of C++ APIs that Addons can use — the most important of which is the `node::ObjectWrap` class. diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 7530c0b1b4f4d2..348f2ad04466bd 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -12,7 +12,7 @@ compiled for one version to run on later versions of Node.js without recompilation. Addons are built/packaged with the same approach/tools -outlined in the section titled [C/C++ Addons](addons.html). +outlined in the section titled [C++ Addons](addons.html). The only difference is the set of APIs that are used by the native code. Instead of using the V8 or [Native Abstractions for Node.js][] APIs, the functions available in the N-API are used. From bdd1375155504accbd0e9780b959d79dc241fe33 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Wed, 10 May 2017 19:37:25 -0700 Subject: [PATCH 038/183] test: add common.mustCall() to NAPI exception test Use `common.mustCall()` to confirm that function is invoked. PR-URL: https://github.com/nodejs/node/pull/12959 Reviewed-By: Colin Ihrig Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Gibson Fahnestock Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- test/addons-napi/test_exception/test.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/addons-napi/test_exception/test.js b/test/addons-napi/test_exception/test.js index 94f9566a4b341f..ddd1195fa7234a 100644 --- a/test/addons-napi/test_exception/test.js +++ b/test/addons-napi/test_exception/test.js @@ -9,8 +9,6 @@ function throwTheError() { } let caughtError; -const throwNoError = common.noop; - // Test that the native side successfully captures the exception let returnedError = test_exception.returnException(throwTheError); assert.strictEqual(theError, returnedError, @@ -34,13 +32,13 @@ assert.strictEqual(test_exception.wasPending(), true, ' when it was allowed through'); // Test that the native side does not capture a non-existing exception -returnedError = test_exception.returnException(throwNoError); +returnedError = test_exception.returnException(common.mustCall()); assert.strictEqual(undefined, returnedError, 'Returned error is undefined when no exception is thrown'); // Test that no exception appears that was not thrown by us try { - test_exception.allowException(throwNoError); + test_exception.allowException(common.mustCall()); } catch (anError) { caughtError = anError; } From ba2594e1cee76e228dab69431e0023ec178ad66e Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Thu, 11 May 2017 09:53:35 -0400 Subject: [PATCH 039/183] doc: clarify operation of napi_cancel_async_work PR-URL: https://github.com/nodejs/node/pull/12974 Reviewed-By: Gibson Fahnestock Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Jason Ginchereau --- doc/api/n-api.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 348f2ad04466bd..423e71859dea94 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -2931,11 +2931,12 @@ NAPI_EXTERN napi_status napi_cancel_async_work(napi_env env, Returns `napi_ok` if the API succeeded. -This API cancels a previously allocated work, provided -it has not yet been queued for execution. After this function is called +This API cancels queued work if it has not yet +been started. If it has already started executing, it cannot be +cancelled and `napi_generic_failure` will be returned. If successful, the `complete` callback will be invoked with a status value of `napi_cancelled`. The work should not be deleted before the `complete` -callback invocation, even when it was cancelled. +callback invocation, even if it has been successfully cancelled. [Aynchronous Operations]: #n_api_asynchronous_operations From 67e0372e2a475d07dde58409d35a1b7326941903 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Fri, 12 May 2017 17:26:38 -0400 Subject: [PATCH 040/183] test: improve N-API test coverage Add tests to cover functions that return globals PR-URL: https://github.com/nodejs/node/pull/13006 Reviewed-By: Colin Ihrig Reviewed-By: Anna Henningsen Reviewed-By: Jason Ginchereau --- test/addons-napi/test_globals/binding.gyp | 8 ++++++ test/addons-napi/test_globals/test.js | 8 ++++++ test/addons-napi/test_globals/test_globals.c | 26 ++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 test/addons-napi/test_globals/binding.gyp create mode 100644 test/addons-napi/test_globals/test.js create mode 100644 test/addons-napi/test_globals/test_globals.c diff --git a/test/addons-napi/test_globals/binding.gyp b/test/addons-napi/test_globals/binding.gyp new file mode 100644 index 00000000000000..0160dc72e18017 --- /dev/null +++ b/test/addons-napi/test_globals/binding.gyp @@ -0,0 +1,8 @@ +{ + "targets": [ + { + "target_name": "test_globals", + "sources": [ "test_globals.c" ] + } + ] +} diff --git a/test/addons-napi/test_globals/test.js b/test/addons-napi/test_globals/test.js new file mode 100644 index 00000000000000..a6e5f722cb9379 --- /dev/null +++ b/test/addons-napi/test_globals/test.js @@ -0,0 +1,8 @@ +'use strict'; +const common = require('../../common'); +const assert = require('assert'); + +const test_globals = require(`./build/${common.buildType}/test_globals`); + +assert.strictEqual(test_globals.getUndefined(), undefined); +assert.strictEqual(test_globals.getNull(), null); diff --git a/test/addons-napi/test_globals/test_globals.c b/test/addons-napi/test_globals/test_globals.c new file mode 100644 index 00000000000000..709e42a2e0e6c8 --- /dev/null +++ b/test/addons-napi/test_globals/test_globals.c @@ -0,0 +1,26 @@ +#include +#include "../common.h" + +napi_value getNull(napi_env env, napi_callback_info info) { + napi_value result; + NAPI_CALL(env, napi_get_null(env, &result)); + return result; +} + +napi_value getUndefined(napi_env env, napi_callback_info info) { + napi_value result; + NAPI_CALL(env, napi_get_undefined(env, &result)); + return result; +} + +void Init(napi_env env, napi_value exports, napi_value module, void* priv) { + napi_property_descriptor descriptors[] = { + DECLARE_NAPI_PROPERTY("getUndefined", getUndefined), + DECLARE_NAPI_PROPERTY("getNull", getNull), + }; + + NAPI_CALL_RETURN_VOID(env, napi_define_properties( + env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); +} + +NAPI_MODULE(addon, Init) From d572c9c262d5269ae785db20ff4f1502e90383ac Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 13 May 2017 17:36:23 +0200 Subject: [PATCH 041/183] n-api: remove compiler warning `TryCatch` without an `Isolate*` argument is deprecated, so add one. PR-URL: https://github.com/nodejs/node/pull/13014 Reviewed-By: Timothy Gu Reviewed-By: Gibson Fahnestock Reviewed-By: Colin Ihrig Reviewed-By: Michael Dawson Reviewed-By: Jason Ginchereau --- src/node_api.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_api.cc b/src/node_api.cc index 8600783523c7a5..b0900d1acd12bb 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -2783,7 +2783,7 @@ class Work { // report it as a fatal exception. (There is no JavaScript on the // callstack that can possibly handle it.) if (!env->last_exception.IsEmpty()) { - v8::TryCatch try_catch; + v8::TryCatch try_catch(env->isolate); env->isolate->ThrowException( v8::Local::New(env->isolate, env->last_exception)); node::FatalException(env->isolate, try_catch); From b10fe3e8bc28cedff58189552296c8be340ec348 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Mon, 15 May 2017 20:18:50 -0400 Subject: [PATCH 042/183] test: Improve N-API test coverage - add coverage for napi_get_prototype - add coverage for napi_strict_equals PR-URL: https://github.com/nodejs/node/pull/13044 Reviewed-By: Colin Ihrig Reviewed-By: Jason Ginchereau --- test/addons-napi/test_general/binding.gyp | 8 +++++ test/addons-napi/test_general/test.js | 32 +++++++++++++++++ test/addons-napi/test_general/test_general.c | 38 ++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 test/addons-napi/test_general/binding.gyp create mode 100644 test/addons-napi/test_general/test.js create mode 100644 test/addons-napi/test_general/test_general.c diff --git a/test/addons-napi/test_general/binding.gyp b/test/addons-napi/test_general/binding.gyp new file mode 100644 index 00000000000000..f8ef9f59613355 --- /dev/null +++ b/test/addons-napi/test_general/binding.gyp @@ -0,0 +1,8 @@ +{ + "targets": [ + { + "target_name": "test_general", + "sources": [ "test_general.c" ] + } + ] +} diff --git a/test/addons-napi/test_general/test.js b/test/addons-napi/test_general/test.js new file mode 100644 index 00000000000000..a2e57126ce8c22 --- /dev/null +++ b/test/addons-napi/test_general/test.js @@ -0,0 +1,32 @@ +'use strict'; + +const common = require('../../common'); +const test_general = require(`./build/${common.buildType}/test_general`); +const assert = require('assert'); + +const val1 = '1'; +const val2 = 1; +const val3 = 1; + +class BaseClass { +} + +class ExtendedClass extends BaseClass { +} + +const baseObject = new BaseClass(); +const extendedObject = new ExtendedClass(); + +// test napi_strict_equals +assert.ok(test_general.testStrictEquals(val1, val1)); +assert.strictEqual(test_general.testStrictEquals(val1, val2), false); +assert.ok(test_general.testStrictEquals(val2, val3)); + +// test napi_get_prototype +assert.strictEqual(test_general.testGetPrototype(baseObject), + Object.getPrototypeOf(baseObject)); +assert.strictEqual(test_general.testGetPrototype(extendedObject), + Object.getPrototypeOf(extendedObject)); +assert.ok(test_general.testGetPrototype(baseObject) !== + test_general.testGetPrototype(extendedObject), + 'Prototypes for base and extended should be different'); diff --git a/test/addons-napi/test_general/test_general.c b/test/addons-napi/test_general/test_general.c new file mode 100644 index 00000000000000..17911b36253df1 --- /dev/null +++ b/test/addons-napi/test_general/test_general.c @@ -0,0 +1,38 @@ +#include +#include "../common.h" + +napi_value testStrictEquals(napi_env env, napi_callback_info info) { + size_t argc = 2; + napi_value args[2]; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); + + bool bool_result; + napi_value result; + NAPI_CALL(env, napi_strict_equals(env, args[0], args[1], &bool_result)); + NAPI_CALL(env, napi_get_boolean(env, bool_result, &result)); + + return result; +} + +napi_value testGetPrototype(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value args[1]; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); + + napi_value result; + NAPI_CALL(env, napi_get_prototype(env, args[0], &result)); + + return result; +} + +void Init(napi_env env, napi_value exports, napi_value module, void* priv) { + napi_property_descriptor descriptors[] = { + DECLARE_NAPI_PROPERTY("testStrictEquals", testStrictEquals), + DECLARE_NAPI_PROPERTY("testGetPrototype", testGetPrototype), + }; + + NAPI_CALL_RETURN_VOID(env, napi_define_properties( + env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); +} + +NAPI_MODULE(addon, Init) From 726d5849777a155dcc788fbbc4a993da7c43fe90 Mon Sep 17 00:00:00 2001 From: Jason Ginchereau Date: Wed, 17 May 2017 16:56:37 -0700 Subject: [PATCH 043/183] n-api: Retain last code when getting error info Unlike most N-API functions, `napi_get_last_error_info()` should not clear the last error code when successful, because a pointer to (not a copy of) the error info structure is returned via an out parameter. PR-URL: https://github.com/nodejs/node/pull/13087 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Michael Dawson --- src/node_api.cc | 2 +- test/addons-napi/test_napi_status/test_napi_status.cc | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/node_api.cc b/src/node_api.cc index b0900d1acd12bb..976bf15f3ca423 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -757,7 +757,7 @@ napi_status napi_get_last_error_info(napi_env env, error_messages[env->last_error.error_code]; *result = &(env->last_error); - return napi_clear_last_error(env); + return napi_ok; } napi_status napi_create_function(napi_env env, diff --git a/test/addons-napi/test_napi_status/test_napi_status.cc b/test/addons-napi/test_napi_status/test_napi_status.cc index 9046feffd4a15d..9e340aa46e106c 100644 --- a/test/addons-napi/test_napi_status/test_napi_status.cc +++ b/test/addons-napi/test_napi_status/test_napi_status.cc @@ -10,6 +10,14 @@ napi_value createNapiError(napi_env env, napi_callback_info info) { NAPI_ASSERT(env, status != napi_ok, "Failed to produce error condition"); + const napi_extended_error_info *error_info = 0; + NAPI_CALL(env, napi_get_last_error_info(env, &error_info)); + + NAPI_ASSERT(env, error_info->error_code == status, + "Last error info code should match last status"); + NAPI_ASSERT(env, error_info->error_message, + "Last error info message should not be null"); + return nullptr; } From f7232ad330cffe13f80f130883f1f5ad9a2cba2c Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Wed, 17 May 2017 17:16:37 -0400 Subject: [PATCH 044/183] doc: add reference to node_api.h in docs Realized that we don't actually point people to the file to include in order to access N-API functions. Add that. PR-URL: https://github.com/nodejs/node/pull/13084 Reviewed-By: Colin Ihrig Reviewed-By: Gibson Fahnestock Reviewed-By: Luigi Pinca Reviewed-By: Anna Henningsen --- doc/api/n-api.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 423e71859dea94..de841331aba0ad 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -52,6 +52,14 @@ for the N-API C based functions exported by Node.js. These wrappers are not part of N-API, nor will they be maintained as part of Node.js. One such example is: [node-api](https://github.com/nodejs/node-api). +In order to use the N-API functions, include the file +[node_api.h](https://github.com/nodejs/node/blob/master/src/node_api.h) +which is located in the src directory in the node development tree. +For example: +```C +#include +``` + ## Basic N-API Data Types N-API exposes the following fundamental datatypes as abstractions that are From 2e48f585cb47de0add787f623a353c156eb25b2f Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Fri, 19 May 2017 17:34:11 -0400 Subject: [PATCH 045/183] doc: fix title/function name mismatch Fix mismatch in title for napi_get_value_string_utf16 Fixes: https://github.com/nodejs/abi-stable-node/issues/243 PR-URL: https://github.com/nodejs/node/pull/13123 Reviewed-By: Anna Henningsen Reviewed-By: Refael Ackermann Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Gibson Fahnestock Reviewed-By: Colin Ihrig --- doc/api/n-api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index de841331aba0ad..d3efd09ab15ec0 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -1590,7 +1590,7 @@ x is passed in it returns `napi_string_expected`. This API returns the UTF8-encoded string corresponding the value passed in. -#### *napi_get_value_string_utf16_length* +#### *napi_get_value_string_utf16* From 4d730bed50152a5b4be7dfd2374f1d6ec5feebab Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Fri, 19 May 2017 18:18:54 -0400 Subject: [PATCH 046/183] test: increase n-api constructor coverage Add tests to validate that properties marked as static are available through the class as opposed to instances PR-URL: https://github.com/nodejs/node/pull/13124 Reviewed-By: Jason Ginchereau Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Hitesh Kanwathirtha --- test/addons-napi/test_constructor/test.js | 5 +++++ .../test_constructor/test_constructor.c | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/test/addons-napi/test_constructor/test.js b/test/addons-napi/test_constructor/test.js index 26083db7a28a21..75c7b3678306e4 100644 --- a/test/addons-napi/test_constructor/test.js +++ b/test/addons-napi/test_constructor/test.js @@ -40,3 +40,8 @@ test_object.readwriteAccessor2 = 2; assert.strictEqual(test_object.readwriteAccessor2, 2); assert.strictEqual(test_object.readonlyAccessor2, 2); assert.throws(() => { test_object.readonlyAccessor2 = 3; }, TypeError); + +// validate that static properties are on the class as opposed +// to the instance +assert.strictEqual(TestConstructor.staticReadonlyAccessor1, 10); +assert.strictEqual(test_object.staticReadonlyAccessor1, undefined); diff --git a/test/addons-napi/test_constructor/test_constructor.c b/test/addons-napi/test_constructor/test_constructor.c index 0a73010d72f115..220d564753ca10 100644 --- a/test/addons-napi/test_constructor/test_constructor.c +++ b/test/addons-napi/test_constructor/test_constructor.c @@ -2,6 +2,7 @@ #include "../common.h" static double value_ = 1; +static double static_value_ = 10; napi_ref constructor_; napi_value GetValue(napi_env env, napi_callback_info info) { @@ -45,6 +46,19 @@ napi_value New(napi_env env, napi_callback_info info) { return _this; } +napi_value GetStaticValue(napi_env env, napi_callback_info info) { + size_t argc = 0; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, NULL, NULL, NULL)); + + NAPI_ASSERT(env, argc == 0, "Wrong number of arguments"); + + napi_value number; + NAPI_CALL(env, napi_create_number(env, static_value_, &number)); + + return number; +} + + void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_value number; NAPI_CALL_RETURN_VOID(env, napi_create_number(env, value_, &number)); @@ -58,6 +72,8 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) { { "readwriteAccessor2", 0, 0, GetValue, SetValue, 0, napi_writable, 0}, { "readonlyAccessor1", 0, 0, GetValue, NULL, 0, napi_default, 0}, { "readonlyAccessor2", 0, 0, GetValue, NULL, 0, napi_writable, 0}, + { "staticReadonlyAccessor1", 0, 0, GetStaticValue, NULL, 0, + napi_default | napi_static, 0}, }; napi_value cons; From 278ba33f70beef76d5b995bfa5a95ab3e357f6de Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Wed, 24 May 2017 12:49:07 +0200 Subject: [PATCH 047/183] src: correct endif comment SRC_NODE_API_H__ Really minor but I could not find an open PR for anything n-api where this could be changed, so creating this so that it is not forgotten. PR-URL: https://github.com/nodejs/node/pull/13190 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Michael Dawson Reviewed-By: James M Snell --- src/node_api.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_api.h b/src/node_api.h index 29c5c513f72f59..5a95c030a35272 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -480,4 +480,4 @@ NAPI_EXTERN napi_status napi_cancel_async_work(napi_env env, EXTERN_C_END -#endif // SRC_NODE_API_H__ +#endif // SRC_NODE_API_H_ From 54bd4b531f79ab8186c186e954d1f4e8f4347dff Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Tue, 23 May 2017 17:26:37 -0400 Subject: [PATCH 048/183] test: add coverage for napi_has_named_property Add test to cover napi_has_named_property PR-URL: https://github.com/nodejs/node/pull/13178 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Jason Ginchereau --- test/addons-napi/test_properties/test.js | 6 +++++ .../test_properties/test_properties.c | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/test/addons-napi/test_properties/test.js b/test/addons-napi/test_properties/test.js index a8127a27860eb3..0f37af81b7ffad 100644 --- a/test/addons-napi/test_properties/test.js +++ b/test/addons-napi/test_properties/test.js @@ -39,3 +39,9 @@ test_object.readwriteAccessor2 = 2; assert.strictEqual(test_object.readwriteAccessor2, 2); assert.strictEqual(test_object.readonlyAccessor2, 2); assert.throws(() => { test_object.readonlyAccessor2 = 3; }, TypeError); + +assert.strictEqual(test_object.hasNamedProperty(test_object, 'echo'), true); +assert.strictEqual(test_object.hasNamedProperty(test_object, 'hiddenValue'), + true); +assert.strictEqual(test_object.hasNamedProperty(test_object, 'doesnotexist'), + false); diff --git a/test/addons-napi/test_properties/test_properties.c b/test/addons-napi/test_properties/test_properties.c index 67e6b1a0d5ed0a..575d750a7d936b 100644 --- a/test/addons-napi/test_properties/test_properties.c +++ b/test/addons-napi/test_properties/test_properties.c @@ -37,6 +37,28 @@ napi_value Echo(napi_env env, napi_callback_info info) { return args[0]; } +napi_value HasNamedProperty(napi_env env, napi_callback_info info) { + size_t argc = 2; + napi_value args[2]; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); + + NAPI_ASSERT(env, argc == 2, "Wrong number of arguments"); + + // Extract the name of the property to check + char buffer[128]; + size_t copied; + NAPI_CALL(env, + napi_get_value_string_utf8(env, args[1], buffer, sizeof(buffer), &copied)); + + // do the check and create the boolean return value + bool value; + napi_value result; + NAPI_CALL(env, napi_has_named_property(env, args[0], buffer, &value)); + NAPI_CALL(env, napi_get_boolean(env, value, &result)); + + return result; +} + void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_value number; NAPI_CALL_RETURN_VOID(env, napi_create_number(env, value_, &number)); @@ -50,6 +72,7 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) { { "readwriteAccessor2", 0, 0, GetValue, SetValue, 0, napi_writable, 0}, { "readonlyAccessor1", 0, 0, GetValue, NULL, 0, napi_default, 0}, { "readonlyAccessor2", 0, 0, GetValue, NULL, 0, napi_writable, 0}, + { "hasNamedProperty", 0, HasNamedProperty, 0, 0, 0, napi_default, 0 }, }; NAPI_CALL_RETURN_VOID(env, napi_define_properties( From 5c0c021a4b7a27657aff8ef2c560574a37eeff81 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Wed, 24 May 2017 16:58:03 -0400 Subject: [PATCH 049/183] n-api: add napi_get_version Add napi_get_version function so that addons can query the level of N-API supported. PR-URL: https://github.com/nodejs/node/pull/13207 Fixes: https://github.com/nodejs/abi-stable-node/issues/231 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Jason Ginchereau --- doc/api/n-api.md | 29 ++++++++++++++++++++ src/node_api.cc | 9 ++++++ src/node_api.h | 4 +++ test/addons-napi/test_general/test.js | 4 +++ test/addons-napi/test_general/test_general.c | 9 ++++++ 5 files changed, 55 insertions(+) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index d3efd09ab15ec0..e151a7357fbb58 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -2946,6 +2946,35 @@ the `complete` callback will be invoked with a status value of `napi_cancelled`. The work should not be deleted before the `complete` callback invocation, even if it has been successfully cancelled. +## Version Management + +### napi_get_version + +```C +NAPI_EXTERN napi_status napi_get_version(napi_env env, + uint32_t* result); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[out] result`: The highest version of N-API supported. + +Returns `napi_ok` if the API succeeded. + +This API returns the highest N-API version supported by the +Node.js runtime. N-API is planned to be additive such that +newer releases of Node.js may support additional API functions. +In order to allow an addon to use a newer function when running with +versions of Node.js that support it, while providing +fallback behavior when running with Node.js versions that don't +support it: + +* Call `napi_get_version()` to determine if the API is available. +* If available, dynamically load a pointer to the function using `uv_dlsym()`. +* Use the dynamically loaded pointer to invoke the function. +* If the function is not available, provide an alternate implementation + that does not use the function. [Aynchronous Operations]: #n_api_asynchronous_operations [Basic N-API Data Types]: #n_api_basic_n_api_data_types diff --git a/src/node_api.cc b/src/node_api.cc index 976bf15f3ca423..7a485283bbb13d 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -20,6 +20,8 @@ #include "env-inl.h" #include "node_api_backport.h" +#define NAPI_VERSION 1 + static napi_status napi_set_last_error(napi_env env, napi_status error_code, uint32_t engine_error_code = 0, @@ -2715,6 +2717,13 @@ napi_status napi_get_typedarray_info(napi_env env, return napi_clear_last_error(env); } +napi_status napi_get_version(napi_env env, uint32_t* result) { + CHECK_ENV(env); + CHECK_ARG(env, result); + *result = NAPI_VERSION; + return napi_clear_last_error(env); +} + namespace uvimpl { static napi_status ConvertUVErrorCode(int code) { diff --git a/src/node_api.h b/src/node_api.h index 5a95c030a35272..a1840943602ce3 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -478,6 +478,10 @@ NAPI_EXTERN napi_status napi_queue_async_work(napi_env env, NAPI_EXTERN napi_status napi_cancel_async_work(napi_env env, napi_async_work work); + +// version management +NAPI_EXTERN napi_status napi_get_version(napi_env env, uint32_t* result); + EXTERN_C_END #endif // SRC_NODE_API_H_ diff --git a/test/addons-napi/test_general/test.js b/test/addons-napi/test_general/test.js index a2e57126ce8c22..614cf0f4f0e7c4 100644 --- a/test/addons-napi/test_general/test.js +++ b/test/addons-napi/test_general/test.js @@ -30,3 +30,7 @@ assert.strictEqual(test_general.testGetPrototype(extendedObject), assert.ok(test_general.testGetPrototype(baseObject) !== test_general.testGetPrototype(extendedObject), 'Prototypes for base and extended should be different'); + +// test version management funcitons +// expected version is currently 1 +assert.strictEqual(test_general.testGetVersion(), 1); diff --git a/test/addons-napi/test_general/test_general.c b/test/addons-napi/test_general/test_general.c index 17911b36253df1..585a3bf2b3ff0d 100644 --- a/test/addons-napi/test_general/test_general.c +++ b/test/addons-napi/test_general/test_general.c @@ -25,10 +25,19 @@ napi_value testGetPrototype(napi_env env, napi_callback_info info) { return result; } +napi_value testGetVersion(napi_env env, napi_callback_info info) { + uint32_t version; + napi_value result; + NAPI_CALL(env, napi_get_version(env, &version)); + NAPI_CALL(env ,napi_create_number(env, version, &result)); + return result; +} + void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("testStrictEquals", testStrictEquals), DECLARE_NAPI_PROPERTY("testGetPrototype", testGetPrototype), + DECLARE_NAPI_PROPERTY("testGetVersion", testGetVersion), }; NAPI_CALL_RETURN_VOID(env, napi_define_properties( From 5c19e28bcfd4721c119b40b8178c12a489d52cfc Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Fri, 26 May 2017 13:24:23 -0400 Subject: [PATCH 050/183] test: improve n-api coverage for typed arrays Add testing for all types of typed arrays. Add testing for napi_is_arraybuffer. PR-URL: https://github.com/nodejs/node/pull/13244 Reviewed-By: Colin Ihrig Reviewed-By: Jason Ginchereau Reviewed-By: James M Snell Reviewed-By: Kunal Pathak Reviewed-By: Hitesh Kanwathirtha --- test/addons-napi/test_typedarray/test.js | 20 ++++++++ .../test_typedarray/test_typedarray.c | 48 +++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/test/addons-napi/test_typedarray/test.js b/test/addons-napi/test_typedarray/test.js index cc1fcbe3566da3..dcb7d76442f608 100644 --- a/test/addons-napi/test_typedarray/test.js +++ b/test/addons-napi/test_typedarray/test.js @@ -37,3 +37,23 @@ assert.strictEqual(externalResult.length, 3); assert.strictEqual(externalResult[0], 0); assert.strictEqual(externalResult[1], 1); assert.strictEqual(externalResult[2], 2); + +// validate creation of all kinds of TypedArrays +const buffer = new ArrayBuffer(128); +const arrayTypes = [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, + Uint16Array, Int32Array, Uint32Array, Float32Array, + Float64Array ]; + +arrayTypes.forEach((currentType, key) => { + const template = Reflect.construct(currentType, buffer); + const theArray = test_typedarray.CreateTypedArray(template, buffer); + + assert.ok(theArray instanceof currentType, + 'Type of new array should match that of the template'); + assert.notStrictEqual(theArray, + template, + 'the new array should not be a copy of the template'); + assert.strictEqual(theArray.buffer, + buffer, + 'Buffer for array should match the one passed in'); +}); diff --git a/test/addons-napi/test_typedarray/test_typedarray.c b/test/addons-napi/test_typedarray/test_typedarray.c index d77945486518a2..4194492cae8b3a 100644 --- a/test/addons-napi/test_typedarray/test_typedarray.c +++ b/test/addons-napi/test_typedarray/test_typedarray.c @@ -95,10 +95,58 @@ napi_value External(napi_env env, napi_callback_info info) { return output_array; } +napi_value CreateTypedArray(napi_env env, napi_callback_info info) { + size_t argc = 2; + napi_value args[2]; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); + + NAPI_ASSERT(env, argc == 2, "Wrong number of arguments"); + + napi_value input_array = args[0]; + napi_valuetype valuetype0; + NAPI_CALL(env, napi_typeof(env, input_array, &valuetype0)); + + NAPI_ASSERT(env, valuetype0 == napi_object, + "Wrong type of argments. Expects a typed array as first argument."); + + bool is_typedarray; + NAPI_CALL(env, napi_is_typedarray(env, input_array, &is_typedarray)); + + NAPI_ASSERT(env, is_typedarray, + "Wrong type of argments. Expects a typed array as first argument."); + + napi_valuetype valuetype1; + napi_value input_buffer = args[1]; + NAPI_CALL(env, napi_typeof(env, input_buffer, &valuetype1)); + + NAPI_ASSERT(env, valuetype1 == napi_object, + "Wrong type of argments. Expects an array buffer as second argument."); + + bool is_arraybuffer; + NAPI_CALL(env, napi_is_arraybuffer(env, input_buffer, &is_arraybuffer)); + + NAPI_ASSERT(env, is_arraybuffer, + "Wrong type of argments. Expects an array buffer as second argument."); + + napi_typedarray_type type; + napi_value in_array_buffer; + size_t byte_offset; + size_t length; + NAPI_CALL(env, napi_get_typedarray_info( + env, input_array, &type, &length, NULL, &in_array_buffer, &byte_offset)); + + napi_value output_array; + NAPI_CALL(env, napi_create_typedarray( + env, type, length, input_buffer, byte_offset, &output_array)); + + return output_array; +} + void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("Multiply", Multiply), DECLARE_NAPI_PROPERTY("External", External), + DECLARE_NAPI_PROPERTY("CreateTypedArray", CreateTypedArray), }; NAPI_CALL_RETURN_VOID(env, napi_define_properties( From 8653a1bad4bea81504bf49f8c36d25831e6587e4 Mon Sep 17 00:00:00 2001 From: Jason Ginchereau Date: Thu, 18 May 2017 15:29:49 -0700 Subject: [PATCH 051/183] test: Make N-API weak-ref GC tests asynchronous One of the N-API weak-reference test cases already had to be made asynchronous to handle different behavior in a newer V8 version: https://github.com/nodejs/node/pull/12864 When porting N-API to Node-ChakraCore, we found more of the test cases needed similar treatment: https://github.com/nodejs/node-chakracore/issues/246 So to make thes tests more robust (and avoid having differences in the test code for Node-ChakraCore), I am refactoring the tests in this file to insert a `setImmedate()` callback before every call to `gc()` and assertions about the effects of the GC. PR-URL: https://github.com/nodejs/node/pull/13121 Reviewed-By: Michael Dawson --- test/addons-napi/test_reference/test.js | 168 ++++++++++++++---------- 1 file changed, 99 insertions(+), 69 deletions(-) diff --git a/test/addons-napi/test_reference/test.js b/test/addons-napi/test_reference/test.js index 30effe7eec0922..aaf5531f39e1f3 100644 --- a/test/addons-napi/test_reference/test.js +++ b/test/addons-napi/test_reference/test.js @@ -11,80 +11,110 @@ const test_reference = require(`./build/${common.buildType}/test_reference`); // of a finalizer callback increments the finalizeCount property. assert.strictEqual(test_reference.finalizeCount, 0); -{ - // External value without a finalizer - let value = test_reference.createExternal(); - assert.strictEqual(test_reference.finalizeCount, 0); - assert.strictEqual(typeof value, 'object'); - test_reference.checkExternal(value); - value = null; - global.gc(); - assert.strictEqual(test_reference.finalizeCount, 0); +// Run each test function in sequence, +// with an async delay and GC call between each. +function runTests(i, title, tests) { + if (tests[i]) { + if (typeof tests[i] === 'string') { + title = tests[i]; + runTests(i + 1, title, tests); + } else { + try { + tests[i](); + } catch (e) { + console.error('Test failed: ' + title); + throw e; + } + setImmediate(() => { + global.gc(); + runTests(i + 1, title, tests); + }); + } + } } +runTests(0, undefined, [ -{ - // External value with a finalizer - let value = test_reference.createExternalWithFinalize(); - assert.strictEqual(test_reference.finalizeCount, 0); - assert.strictEqual(typeof value, 'object'); - test_reference.checkExternal(value); - value = null; - global.gc(); - assert.strictEqual(test_reference.finalizeCount, 1); -} - -{ - // Strong reference - let value = test_reference.createExternalWithFinalize(); - assert.strictEqual(test_reference.finalizeCount, 0); - test_reference.createReference(value, 1); - assert.strictEqual(test_reference.referenceValue, value); - value = null; - global.gc(); // Value should NOT be GC'd because there is a strong ref - assert.strictEqual(test_reference.finalizeCount, 0); - test_reference.deleteReference(); - global.gc(); // Value should be GC'd because the strong ref was deleted - assert.strictEqual(test_reference.finalizeCount, 1); -} - -{ - // Strong reference, increment then decrement to weak reference - let value = test_reference.createExternalWithFinalize(); - assert.strictEqual(test_reference.finalizeCount, 0); - test_reference.createReference(value, 1); - value = null; - global.gc(); // Value should NOT be GC'd because there is a strong ref - assert.strictEqual(test_reference.finalizeCount, 0); + 'External value without a finalizer', + () => { + const value = test_reference.createExternal(); + assert.strictEqual(test_reference.finalizeCount, 0); + assert.strictEqual(typeof value, 'object'); + test_reference.checkExternal(value); + }, + () => { + assert.strictEqual(test_reference.finalizeCount, 0); + }, - assert.strictEqual(test_reference.incrementRefcount(), 2); - global.gc(); // Value should NOT be GC'd because there is a strong ref - assert.strictEqual(test_reference.finalizeCount, 0); - - assert.strictEqual(test_reference.decrementRefcount(), 1); - global.gc(); // Value should NOT be GC'd because there is a strong ref - assert.strictEqual(test_reference.finalizeCount, 0); + 'External value with a finalizer', + () => { + const value = test_reference.createExternalWithFinalize(); + assert.strictEqual(test_reference.finalizeCount, 0); + assert.strictEqual(typeof value, 'object'); + test_reference.checkExternal(value); + }, + () => { + assert.strictEqual(test_reference.finalizeCount, 1); + }, - assert.strictEqual(test_reference.decrementRefcount(), 0); - global.gc(); // Value should be GC'd because the ref is now weak! - assert.strictEqual(test_reference.finalizeCount, 1); + 'Weak reference', + () => { + const value = test_reference.createExternalWithFinalize(); + assert.strictEqual(test_reference.finalizeCount, 0); + test_reference.createReference(value, 0); + assert.strictEqual(test_reference.referenceValue, value); + }, + () => { + // Value should be GC'd because there is only a weak ref + assert.strictEqual(test_reference.referenceValue, undefined); + assert.strictEqual(test_reference.finalizeCount, 1); + test_reference.deleteReference(); + }, - test_reference.deleteReference(); - global.gc(); // Value was already GC'd - assert.strictEqual(test_reference.finalizeCount, 1); -} + 'Strong reference', + () => { + const value = test_reference.createExternalWithFinalize(); + assert.strictEqual(test_reference.finalizeCount, 0); + test_reference.createReference(value, 1); + assert.strictEqual(test_reference.referenceValue, value); + }, + () => { + // Value should NOT be GC'd because there is a strong ref + assert.strictEqual(test_reference.finalizeCount, 0); + test_reference.deleteReference(); + }, + () => { + // Value should be GC'd because the strong ref was deleted + assert.strictEqual(test_reference.finalizeCount, 1); + }, -{ - // Weak reference - let value = test_reference.createExternalWithFinalize(); - assert.strictEqual(test_reference.finalizeCount, 0); - test_reference.createReference(value, 0); - assert.strictEqual(test_reference.referenceValue, value); - value = null; - setImmediate(common.mustCall(() => { - // This test only works if gc() is called from an immediate callback. - global.gc(); // Value should be GC'd because there is only a weak ref - assert.strictEqual(test_reference.referenceValue, undefined); + 'Strong reference, increment then decrement to weak reference', + () => { + const value = test_reference.createExternalWithFinalize(); + assert.strictEqual(test_reference.finalizeCount, 0); + test_reference.createReference(value, 1); + }, + () => { + // Value should NOT be GC'd because there is a strong ref + assert.strictEqual(test_reference.finalizeCount, 0); + assert.strictEqual(test_reference.incrementRefcount(), 2); + }, + () => { + // Value should NOT be GC'd because there is a strong ref + assert.strictEqual(test_reference.finalizeCount, 0); + assert.strictEqual(test_reference.decrementRefcount(), 1); + }, + () => { + // Value should NOT be GC'd because there is a strong ref + assert.strictEqual(test_reference.finalizeCount, 0); + assert.strictEqual(test_reference.decrementRefcount(), 0); + }, + () => { + // Value should be GC'd because the ref is now weak! assert.strictEqual(test_reference.finalizeCount, 1); test_reference.deleteReference(); - })); -} + }, + () => { + // Value was already GC'd + assert.strictEqual(test_reference.finalizeCount, 1); + }, +]); From 897bac2d0db143c351050ae97662049c56bbf4ee Mon Sep 17 00:00:00 2001 From: JongChan Choi Date: Wed, 31 May 2017 14:04:14 +0900 Subject: [PATCH 052/183] doc: fix typo in n-api.md PR-URL: https://github.com/nodejs/node/pull/13323 Reviewed-By: Luigi Pinca --- doc/api/n-api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index e151a7357fbb58..7da9e5171664a9 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -530,7 +530,7 @@ Taking the earlier example, adding calls to [`napi_open_handle_scope`][] and is valid throughout the execution of the loop: ```C -for (int i = 0; i < 1000000; i++) {napi_ +for (int i = 0; i < 1000000; i++) { napi_handle_scope scope; napi_status status = napi_open_handle_scope(env, &scope); if (status != napi_ok) { From 224958164f747349321895ac83aa043a06abec9b Mon Sep 17 00:00:00 2001 From: Jason Ginchereau Date: Thu, 25 May 2017 17:05:21 -0700 Subject: [PATCH 053/183] n-api: enable napi_wrap() to work with any object Previously, napi_wrap() would only work with objects created from a constructor returned by napi_define_class(). While the N-API team was aware of this limitation, it was not clearly documented and is likely to cause confusion anyway. It's much simpler if addons are allowed to use any JS object. Also, the specific behavior of the limitation is difficult to reimplement on other VMs that work differently from V8. V8 requires object internal fields to be declared on the object prototype (which napi_define_class() used to do). Since it's too late to modify the object prototype by the time napi_wrap() is called, napi_wrap() now inserts a new object (with the internal field) into the supplied object's prototype chain. Then it can be retrieved from there later by napi_unwrap(). This change also includes improvements to the documentation for napi_create_external(), partly to explain how it is different from napi_wrap(). PR-URL: https://github.com/nodejs/node/pull/13250 Reviewed-By: Michael Dawson --- doc/api/n-api.md | 57 ++++++++----- src/node_api.cc | 42 ++++++---- test/addons-napi/test_object/test.js | 98 +++++++++++++++------- test/addons-napi/test_object/test_object.c | 27 ++++++ 4 files changed, 159 insertions(+), 65 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 7da9e5171664a9..ab90cf05f4750d 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -1059,20 +1059,24 @@ napi_status napi_create_external(napi_env env, ``` - `[in] env`: The environment that the API is invoked under. -- `[in] data`: Raw pointer to the external data being wrapped. -- `[in] finalize_cb`: Optional callback to call when the wrapped object +- `[in] data`: Raw pointer to the external data. +- `[in] finalize_cb`: Optional callback to call when the external value is being collected. - `[in] finalize_hint`: Optional hint to pass to the finalize callback during collection. -- `[out] result`: A `napi_value` representing an external object. +- `[out] result`: A `napi_value` representing an external value. Returns `napi_ok` if the API succeeded. -This API allocates a JavaScript object with external data attached to it. -This is used to wrap native objects and project them into JavaScript. -The API allows the caller to pass in a finalize callback, in case the -underlying native resource needs to be cleaned up when the wrapper -JavaScript object gets collected. +This API allocates a JavaScript value with external data attached to it. This +is used to pass external data through JavaScript code, so it can be retrieved +later by native code. The API allows the caller to pass in a finalize callback, +in case the underlying native resource needs to be cleaned up when the external +JavaScript value gets collected. + +*Note*: The created value is not an object, and therefore does not support +additional properties. It is considered a distinct value type: calling +`napi_typeof()` with an external value yields `napi_external`. #### napi_create_external_arraybuffer -```C -napi_status napi_get_value_string_length(napi_env env, - napi_value value, - int* result) -``` - -- `[in] env`: The environment that the API is invoked under. -- `[in] value`: `napi_value` representing JavaScript string. -- `[out] result`: Number of characters in the given JavaScript string. - -Returns `napi_ok` if the API succeeded. If a non-String `napi_value` -is passed in it returns `napi_string_expected`. - -This API returns the number of characters in the given JavaScript string. - #### *napi_get_value_string_utf8* ```C -NODE_EXTERN napi_status napi_create_error(napi_env env, const char* msg); +NODE_EXTERN napi_status napi_create_error(napi_env env, + const char* msg, + napi_value* result); ``` - `[in] env`: The environment that the API is invoked under. - `[in] msg`: C string representing the text to be associated with. @@ -422,7 +424,9 @@ This API returns a JavaScript Error with the text provided. added: v8.0.0 --> ```C -NODE_EXTERN napi_status napi_create_type_error(napi_env env, const char* msg); +NODE_EXTERN napi_status napi_create_type_error(napi_env env, + const char* msg, + napi_value* result); ``` - `[in] env`: The environment that the API is invoked under. - `[in] msg`: C string representing the text to be associated with. @@ -438,7 +442,9 @@ This API returns a JavaScript TypeError with the text provided. added: v8.0.0 --> ```C -NODE_EXTERN napi_status napi_create_range_error(napi_env env, const char* msg); +NODE_EXTERN napi_status napi_create_range_error(napi_env env, + const char* msg, + napi_value* result); ``` - `[in] env`: The environment that the API is invoked under. - `[in] msg`: C string representing the text to be associated with. From 2b94a31893ab321fdac81fb0f4db165007cee78c Mon Sep 17 00:00:00 2001 From: XadillaX Date: Fri, 9 Jun 2017 18:14:22 +0800 Subject: [PATCH 062/183] doc: fix out of date napi_callback doc The earlier version `napi_callback` returns `void` but now is `napi_value`. The document of this section hasn't been modified. PR-URL: https://github.com/nodejs/node/pull/13570 Fixes: https://github.com/nodejs/node/issues/12248 Fixes: https://github.com/nodejs/node/issues/13562 Reviewed-By: Gibson Fahnestock Reviewed-By: Colin Ihrig Reviewed-By: Jason Ginchereau Reviewed-By: James M Snell --- doc/api/n-api.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 72b35f3810bbbd..da93f7c9d67c65 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -161,18 +161,16 @@ For more details, review the [Object Lifetime Management][]. ### N-API Callback types #### *napi_callback_info* -Opaque datatype that is passed to a callback function. It can be used for two -purposes: -- Get additional information about the context in which the callback was - invoked. -- Set the return value of the callback. +Opaque datatype that is passed to a callback function. It can be used for +getting additional information about the context in which the callback was +invoked. #### *napi_callback* Function pointer type for user-provided native functions which are to be exposed to JavaScript via N-API. Callback functions should satisfy the following signature: ```C -typedef void (*napi_callback)(napi_env, napi_callback_info); +typedef napi_value (*napi_callback)(napi_env, napi_callback_info); ``` #### *napi_finalize* @@ -2516,8 +2514,9 @@ In order to expose a function as part of the add-on's module exports, set the newly created function on the exports object. A sample module might look as follows: ```C -void SayHello(napi_env env, napi_callback_info info) { +napi_value SayHello(napi_env env, napi_callback_info info) { printf("Hello\n"); + return nullptr; } void Init(napi_env env, napi_value exports, napi_value module, void* priv) { From 00ee0277ad1b4cabdf9dd4d4064e578646275613 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Tue, 13 Jun 2017 01:02:52 -0400 Subject: [PATCH 063/183] doc: fix a few n-api doc issues - Add doc for napi_create_string_latin1(). - Fix signatures where c string was specified instead of napi_value. - Fix return type of napi_callback. - Update to specify that napi_escape_handle() can only be called once for a given scope. PR-URL: https://github.com/nodejs/node/pull/13650 Fixes: https://github.com/nodejs/node/issues/13555 Fixes: https://github.com/nodejs/node/issues/13556 Fixes: https://github.com/nodejs/node/issues/13562 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Jason Ginchereau Reviewed-By: Ingvar Stepanyan --- doc/api/n-api.md | 69 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 20 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index da93f7c9d67c65..f8154d2cb490ec 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -406,11 +406,12 @@ added: v8.0.0 --> ```C NODE_EXTERN napi_status napi_create_error(napi_env env, - const char* msg, + napi_value msg, napi_value* result); ``` - `[in] env`: The environment that the API is invoked under. -- `[in] msg`: C string representing the text to be associated with. +- `[in] msg`: napi_value that references a JavaScript String to be +used as the message for the Error. - `[out] result`: `napi_value` representing the error created. Returns `napi_ok` if the API succeeded. @@ -423,11 +424,12 @@ added: v8.0.0 --> ```C NODE_EXTERN napi_status napi_create_type_error(napi_env env, - const char* msg, + napi_value msg, napi_value* result); ``` - `[in] env`: The environment that the API is invoked under. -- `[in] msg`: C string representing the text to be associated with. +- `[in] msg`: napi_value that references a JavaScript String to be +used as the message for the Error. - `[out] result`: `napi_value` representing the error created. Returns `napi_ok` if the API succeeded. @@ -445,7 +447,8 @@ NODE_EXTERN napi_status napi_create_range_error(napi_env env, napi_value* result); ``` - `[in] env`: The environment that the API is invoked under. -- `[in] msg`: C string representing the text to be associated with. +- `[in] msg`: napi_value that references a JavaScript String to be +used as the message for the Error. - `[out] result`: `napi_value` representing the error created. Returns `napi_ok` if the API succeeded. @@ -562,16 +565,17 @@ for (int i = 0; i < 1000000; i++) { ``` When nesting scopes, there are cases where a handle from an -inner scope needs to live beyond the lifespan of that scope. N-API supports an +inner scope needs to live beyond the lifespan of that scope. N-API supports an 'escapable scope' in order to support this case. An escapable scope -allows one or more handles to be 'promoted' so that they 'escape' the -current scope and the lifespan of the handle(s) changes from the current +allows one handle to be 'promoted' so that it 'escapes' the +current scope and the lifespan of the handle changes from the current scope to that of the outer scope. The methods available to open/close escapable scopes are [`napi_open_escapable_handle_scope`][] and [`napi_close_escapable_handle_scope`][]. -The request to promote a handle is made through the [`napi_escape_handle`][]. +The request to promote a handle is made through [`napi_escape_handle`][] which +can only be called once. #### napi_open_handle_scope ```C napi_status napi_create_symbol(napi_env env, - const char* description, + napi_value description, napi_value* result) ``` - `[in] env`: The environment that the API is invoked under. -- `[in] description`: Null-terminated character buffer representing a -UTF8-encoded string to describe the symbol. +- `[in] description`: Optional napi_value which refers to a JavaScript +String to be set as the description for the symbol. - `[out] result`: A `napi_value` representing a JavaScript Symbol. Returns `napi_ok` if the API succeeded. @@ -1299,8 +1303,8 @@ napi_status napi_create_string_utf16(napi_env env, - `[in] env`: The environment that the API is invoked under. - `[in] str`: Character buffer representing a UTF16-LE-encoded string. -- `[in] length`: The length of the string in characters, or -1 if it is -null-terminated. +- `[in] length`: The length of the string in two-byte code units, or -1 if +it is null-terminated. - `[out] result`: A `napi_value` representing a JavaScript String. Returns `napi_ok` if the API succeeded. @@ -1311,6 +1315,31 @@ The JavaScript String type is described in [Section 6.1.4](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type) of the ECMAScript Language Specification. +#### *napi_create_string_latin1* + +```C +NAPI_EXTERN napi_status napi_create_string_latin1(napi_env env, + const char* str, + size_t length, + napi_value* result); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] str`: Character buffer representing a latin1-encoded string. +- `[in] length`: The length of the string in bytes, or -1 if it is +null-terminated. +- `[out] result`: A `napi_value` representing a JavaScript String. + +Returns `napi_ok` if the API succeeded. + +This API creates a JavaScript String object from a latin1-encoded C string. + +The JavaScript String type is described in +[Section 6.1.4](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type) +of the ECMAScript Language Specification. + #### *napi_create_string_utf8* +```C +napi_status napi_delete_element(napi_env env, + napi_value object, + uint32_t index, + bool* result); +``` + +- `[in] env`: The environment that the N-API call is invoked under. +- `[in] object`: The object to query. +- `[in] index`: The index of the property to delete. +- `[out] result`: Whether the element deletion succeeded or not. `result` can +optionally be ignored by passing `NULL`. + +Returns `napi_ok` if the API succeeded. + +This API attempts to delete the specified `index` from `object`. + #### *napi_define_properties* +```C +napi_status napi_delete_property(napi_env env, + napi_value object, + napi_value key, + bool* result); +``` + +- `[in] env`: The environment that the N-API call is invoked under. +- `[in] object`: The object to query. +- `[in] key`: The name of the property to delete. +- `[out] result`: Whether the property deletion succeeded or not. `result` can +optionally be ignored by passing `NULL`. + +Returns `napi_ok` if the API succeeded. + +This API attempts to delete the `key` own property from `object`. + + #### *napi_set_named_property* +```C +napi_status napi_has_own_property(napi_env env, + napi_value object, + napi_value key, + bool* result); +``` + +- `[in] env`: The environment that the N-API call is invoked under. +- `[in] object`: The object to query. +- `[in] key`: The name of the own property whose existence to check. +- `[out] result`: Whether the own property exists on the object or not. + +Returns `napi_ok` if the API succeeded. + +This API checks if the Object passed in has the named own property. `key` must +be a string or a Symbol, or an error will be thrown. N-API will not perform any +conversion between data types. + + #### *napi_set_named_property* ```C -NODE_EXTERN napi_status napi_throw_error(napi_env env, const char* msg); +NODE_EXTERN napi_status napi_throw_error(napi_env env, + const char* code, + const char* msg); ``` - `[in] env`: The environment that the API is invoked under. +- `[in] code`: Optional error code to be set on the error. - `[in] msg`: C string representing the text to be associated with the error. @@ -358,9 +386,12 @@ This API throws a JavaScript Error with the text provided. added: v8.0.0 --> ```C -NODE_EXTERN napi_status napi_throw_type_error(napi_env env, const char* msg); +NODE_EXTERN napi_status napi_throw_type_error(napi_env env, + const char* code, + const char* msg); ``` - `[in] env`: The environment that the API is invoked under. +- `[in] code`: Optional error code to be set on the error. - `[in] msg`: C string representing the text to be associated with the error. @@ -373,9 +404,12 @@ This API throws a JavaScript TypeError with the text provided. added: v8.0.0 --> ```C -NODE_EXTERN napi_status napi_throw_range_error(napi_env env, const char* msg); +NODE_EXTERN napi_status napi_throw_range_error(napi_env env, + const char* code, + const char* msg); ``` - `[in] env`: The environment that the API is invoked under. +- `[in] code`: Optional error code to be set on the error. - `[in] msg`: C string representing the text to be associated with the error. @@ -409,10 +443,13 @@ added: v8.0.0 --> ```C NODE_EXTERN napi_status napi_create_error(napi_env env, + napi_value code, napi_value msg, napi_value* result); ``` - `[in] env`: The environment that the API is invoked under. +- `[in] code`: Optional `napi_value` with the string for the error code to + be associated with the error. - `[in] msg`: napi_value that references a JavaScript String to be used as the message for the Error. - `[out] result`: `napi_value` representing the error created. @@ -427,10 +464,13 @@ added: v8.0.0 --> ```C NODE_EXTERN napi_status napi_create_type_error(napi_env env, + napi_value code, napi_value msg, napi_value* result); ``` - `[in] env`: The environment that the API is invoked under. +- `[in] code`: Optional `napi_value` with the string for the error code to + be associated with the error. - `[in] msg`: napi_value that references a JavaScript String to be used as the message for the Error. - `[out] result`: `napi_value` representing the error created. @@ -446,10 +486,13 @@ added: v8.0.0 --> ```C NODE_EXTERN napi_status napi_create_range_error(napi_env env, + napi_value code, const char* msg, napi_value* result); ``` - `[in] env`: The environment that the API is invoked under. +- `[in] code`: Optional `napi_value` with the string for the error code to + be associated with the error. - `[in] msg`: napi_value that references a JavaScript String to be used as the message for the Error. - `[out] result`: `napi_value` representing the error created. diff --git a/src/node_api.cc b/src/node_api.cc index fee00dabe9a14d..25e8c6385572e7 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -21,6 +21,7 @@ #include "node_internals.h" #include "env-inl.h" #include "node_api_backport.h" +#include "util.h" #define NAPI_VERSION 1 @@ -1525,7 +1526,61 @@ napi_status napi_create_symbol(napi_env env, return GET_RETURN_STATUS(env); } +static napi_status set_error_code(napi_env env, + v8::Local error, + napi_value code, + const char* code_cstring) { + if ((code != nullptr) || (code_cstring != nullptr)) { + v8::Isolate* isolate = env->isolate; + v8::Local context = isolate->GetCurrentContext(); + v8::Local err_object = error.As(); + + v8::Local code_value = v8impl::V8LocalValueFromJsValue(code); + if (code != nullptr) { + code_value = v8impl::V8LocalValueFromJsValue(code); + RETURN_STATUS_IF_FALSE(env, code_value->IsString(), napi_string_expected); + } else { + CHECK_NEW_FROM_UTF8(env, code_value, code_cstring); + } + + v8::Local code_key; + CHECK_NEW_FROM_UTF8(env, code_key, "code"); + + v8::Maybe set_maybe = err_object->Set(context, code_key, code_value); + RETURN_STATUS_IF_FALSE(env, + set_maybe.FromMaybe(false), + napi_generic_failure); + + // now update the name to be "name [code]" where name is the + // original name and code is the code associated with the Error + v8::Local name_string; + CHECK_NEW_FROM_UTF8(env, name_string, ""); + v8::Local name_key; + CHECK_NEW_FROM_UTF8(env, name_key, "name"); + + auto maybe_name = err_object->Get(context, name_key); + if (!maybe_name.IsEmpty()) { + v8::Local name = maybe_name.ToLocalChecked(); + if (name->IsString()) { + name_string = v8::String::Concat(name_string, name.As()); + } + } + name_string = v8::String::Concat(name_string, + FIXED_ONE_BYTE_STRING(isolate, " [")); + name_string = v8::String::Concat(name_string, code_value.As()); + name_string = v8::String::Concat(name_string, + FIXED_ONE_BYTE_STRING(isolate, "]")); + + set_maybe = err_object->Set(context, name_key, name_string); + RETURN_STATUS_IF_FALSE(env, + set_maybe.FromMaybe(false), + napi_generic_failure); + } + return napi_ok; +} + napi_status napi_create_error(napi_env env, + napi_value code, napi_value msg, napi_value* result) { NAPI_PREAMBLE(env); @@ -1535,13 +1590,18 @@ napi_status napi_create_error(napi_env env, v8::Local message_value = v8impl::V8LocalValueFromJsValue(msg); RETURN_STATUS_IF_FALSE(env, message_value->IsString(), napi_string_expected); - *result = v8impl::JsValueFromV8LocalValue(v8::Exception::Error( - message_value.As())); + v8::Local error_obj = + v8::Exception::Error(message_value.As()); + napi_status status = set_error_code(env, error_obj, code, nullptr); + if (status != napi_ok) return status; + + *result = v8impl::JsValueFromV8LocalValue(error_obj); return GET_RETURN_STATUS(env); } napi_status napi_create_type_error(napi_env env, + napi_value code, napi_value msg, napi_value* result) { NAPI_PREAMBLE(env); @@ -1551,13 +1611,18 @@ napi_status napi_create_type_error(napi_env env, v8::Local message_value = v8impl::V8LocalValueFromJsValue(msg); RETURN_STATUS_IF_FALSE(env, message_value->IsString(), napi_string_expected); - *result = v8impl::JsValueFromV8LocalValue(v8::Exception::TypeError( - message_value.As())); + v8::Local error_obj = + v8::Exception::TypeError(message_value.As()); + napi_status status = set_error_code(env, error_obj, code, nullptr); + if (status != napi_ok) return status; + + *result = v8impl::JsValueFromV8LocalValue(error_obj); return GET_RETURN_STATUS(env); } napi_status napi_create_range_error(napi_env env, + napi_value code, napi_value msg, napi_value* result) { NAPI_PREAMBLE(env); @@ -1567,8 +1632,12 @@ napi_status napi_create_range_error(napi_env env, v8::Local message_value = v8impl::V8LocalValueFromJsValue(msg); RETURN_STATUS_IF_FALSE(env, message_value->IsString(), napi_string_expected); - *result = v8impl::JsValueFromV8LocalValue(v8::Exception::RangeError( - message_value.As())); + v8::Local error_obj = + v8::Exception::RangeError(message_value.As()); + napi_status status = set_error_code(env, error_obj, code, nullptr); + if (status != napi_ok) return status; + + *result = v8impl::JsValueFromV8LocalValue(error_obj); return GET_RETURN_STATUS(env); } @@ -1741,40 +1810,58 @@ napi_status napi_throw(napi_env env, napi_value error) { return napi_clear_last_error(env); } -napi_status napi_throw_error(napi_env env, const char* msg) { +napi_status napi_throw_error(napi_env env, + const char* code, + const char* msg) { NAPI_PREAMBLE(env); v8::Isolate* isolate = env->isolate; v8::Local str; CHECK_NEW_FROM_UTF8(env, str, msg); - isolate->ThrowException(v8::Exception::Error(str)); + v8::Local error_obj = v8::Exception::Error(str); + napi_status status = set_error_code(env, error_obj, nullptr, code); + if (status != napi_ok) return status; + + isolate->ThrowException(error_obj); // any VM calls after this point and before returning // to the javascript invoker will fail return napi_clear_last_error(env); } -napi_status napi_throw_type_error(napi_env env, const char* msg) { +napi_status napi_throw_type_error(napi_env env, + const char* code, + const char* msg) { NAPI_PREAMBLE(env); v8::Isolate* isolate = env->isolate; v8::Local str; CHECK_NEW_FROM_UTF8(env, str, msg); - isolate->ThrowException(v8::Exception::TypeError(str)); + v8::Local error_obj = v8::Exception::TypeError(str); + napi_status status = set_error_code(env, error_obj, nullptr, code); + if (status != napi_ok) return status; + + isolate->ThrowException(error_obj); // any VM calls after this point and before returning // to the javascript invoker will fail return napi_clear_last_error(env); } -napi_status napi_throw_range_error(napi_env env, const char* msg) { +napi_status napi_throw_range_error(napi_env env, + const char* code, + const char* msg) { NAPI_PREAMBLE(env); v8::Isolate* isolate = env->isolate; v8::Local str; CHECK_NEW_FROM_UTF8(env, str, msg); - isolate->ThrowException(v8::Exception::RangeError(str)); + v8::Local error_obj = v8::Exception::RangeError(str); + napi_status status = set_error_code(env, error_obj, nullptr, code); + if (status != napi_ok) return status; + + isolate->ThrowException(error_obj); // any VM calls after this point and before returning // to the javascript invoker will fail return napi_clear_last_error(env); @@ -2394,7 +2481,9 @@ napi_status napi_instanceof(napi_env env, CHECK_TO_OBJECT(env, context, ctor, constructor); if (!ctor->IsFunction()) { - napi_throw_type_error(env, "constructor must be a function"); + napi_throw_type_error(env, + "ERR_NAPI_CONS_FUNCTION", + "Constructor must be a function"); return napi_set_last_error(env, napi_function_expected); } @@ -2462,7 +2551,10 @@ napi_status napi_instanceof(napi_env env, v8::Local prototype_property = maybe_prototype.ToLocalChecked(); if (!prototype_property->IsObject()) { - napi_throw_type_error(env, "constructor.prototype must be an object"); + napi_throw_type_error( + env, + "ERR_NAPI_CONS_PROTOTYPE_OBJECT", + "Constructor.prototype must be an object"); return napi_set_last_error(env, napi_object_expected); } diff --git a/src/node_api.h b/src/node_api.h index e346b762dd2702..af98789d418210 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -142,12 +142,15 @@ NAPI_EXTERN napi_status napi_create_function(napi_env env, void* data, napi_value* result); NAPI_EXTERN napi_status napi_create_error(napi_env env, + napi_value code, napi_value msg, napi_value* result); NAPI_EXTERN napi_status napi_create_type_error(napi_env env, + napi_value code, napi_value msg, napi_value* result); NAPI_EXTERN napi_status napi_create_range_error(napi_env env, + napi_value code, napi_value msg, napi_value* result); @@ -404,9 +407,15 @@ NAPI_EXTERN napi_status napi_escape_handle(napi_env env, // Methods to support error handling NAPI_EXTERN napi_status napi_throw(napi_env env, napi_value error); -NAPI_EXTERN napi_status napi_throw_error(napi_env env, const char* msg); -NAPI_EXTERN napi_status napi_throw_type_error(napi_env env, const char* msg); -NAPI_EXTERN napi_status napi_throw_range_error(napi_env env, const char* msg); +NAPI_EXTERN napi_status napi_throw_error(napi_env env, + const char* code, + const char* msg); +NAPI_EXTERN napi_status napi_throw_type_error(napi_env env, + const char* code, + const char* msg); +NAPI_EXTERN napi_status napi_throw_range_error(napi_env env, + const char* code, + const char* msg); NAPI_EXTERN napi_status napi_is_error(napi_env env, napi_value value, bool* result); diff --git a/test/addons-napi/common.h b/test/addons-napi/common.h index e9640935d4154b..422418ced49a39 100644 --- a/test/addons-napi/common.h +++ b/test/addons-napi/common.h @@ -12,7 +12,7 @@ const char* error_message = error_info->error_message != NULL ? \ error_info->error_message : \ "empty error message"; \ - napi_throw_error((env), error_message); \ + napi_throw_error((env), NULL, error_message); \ } \ } while (0) @@ -21,6 +21,7 @@ if (!(assertion)) { \ napi_throw_error( \ (env), \ + NULL, \ "assertion (" #assertion ") failed: " message); \ return ret_val; \ } \ diff --git a/test/addons-napi/test_async/test_async.cc b/test/addons-napi/test_async/test_async.cc index cc7bc334304323..f257b268b93159 100644 --- a/test/addons-napi/test_async/test_async.cc +++ b/test/addons-napi/test_async/test_async.cc @@ -29,7 +29,7 @@ void Execute(napi_env env, void* data) { carrier* c = static_cast(data); if (c != &the_carrier) { - napi_throw_type_error(env, "Wrong data parameter to Execute."); + napi_throw_type_error(env, nullptr, "Wrong data parameter to Execute."); return; } @@ -40,12 +40,12 @@ void Complete(napi_env env, napi_status status, void* data) { carrier* c = static_cast(data); if (c != &the_carrier) { - napi_throw_type_error(env, "Wrong data parameter to Complete."); + napi_throw_type_error(env, nullptr, "Wrong data parameter to Complete."); return; } if (status != napi_ok) { - napi_throw_type_error(env, "Execute callback failed."); + napi_throw_type_error(env, nullptr, "Execute callback failed."); return; } diff --git a/test/addons-napi/test_error/test.js b/test/addons-napi/test_error/test.js index f7479f2c9a64d8..80f99f48ba79f3 100644 --- a/test/addons-napi/test_error/test.js +++ b/test/addons-napi/test_error/test.js @@ -72,6 +72,30 @@ assert.throws(() => { test_error.throwTypeError(); }, /^TypeError: type error$/); +assert.throws( + () => test_error.throwErrorCode(), + common.expectsError({ + code: 'ERR_TEST_CODE', + message: 'Error [error]' + }) +); + +assert.throws( + () => test_error.throwRangeErrorCode(), + common.expectsError({ + code: 'ERR_TEST_CODE', + message: 'RangeError [range error]' + }) +); + +assert.throws( + () => test_error.throwTypeErrorCode(), + common.expectsError({ + code: 'ERR_TEST_CODE', + message: 'TypeError [type error]' + }) +); + let error = test_error.createError(); assert.ok(error instanceof Error, 'expected error to be an instance of Error'); assert.strictEqual(error.message, 'error', 'expected message to be "error"'); @@ -89,3 +113,41 @@ assert.ok(error instanceof TypeError, assert.strictEqual(error.message, 'type error', 'expected message to be "type error"'); + +error = test_error.createErrorCode(); +assert.ok(error instanceof Error, 'expected error to be an instance of Error'); +assert.strictEqual(error.code, + 'ERR_TEST_CODE', + 'expected code to be "ERR_TEST_CODE"'); +assert.strictEqual(error.message, + 'Error [error]', + 'expected message to be "Error [error]"'); +assert.strictEqual(error.name, + 'Error [ERR_TEST_CODE]', + 'expected name to be "Error [ERR_TEST_CODE]"'); + +error = test_error.createRangeErrorCode(); +assert.ok(error instanceof RangeError, + 'expected error to be an instance of RangeError'); +assert.strictEqual(error.message, + 'RangeError [range error]', + 'expected message to be "RangeError [range error]"'); +assert.strictEqual(error.code, + 'ERR_TEST_CODE', + 'expected code to be "ERR_TEST_CODE"'); +assert.strictEqual(error.name, + 'RangeError [ERR_TEST_CODE]', + 'expected name to be "RangeError[ERR_TEST_CODE]"'); + +error = test_error.createTypeErrorCode(); +assert.ok(error instanceof TypeError, + 'expected error to be an instance of TypeError'); +assert.strictEqual(error.message, + 'TypeError [type error]', + 'expected message to be "TypeError [type error]"'); +assert.strictEqual(error.code, + 'ERR_TEST_CODE', + 'expected code to be "ERR_TEST_CODE"'); +assert.strictEqual(error.name, + 'TypeError [ERR_TEST_CODE]', + 'expected name to be "TypeError[ERR_TEST_CODE]"'); diff --git a/test/addons-napi/test_error/test_error.cc b/test/addons-napi/test_error/test_error.cc index ddba2059f23be6..29aba1f1ef62f6 100644 --- a/test/addons-napi/test_error/test_error.cc +++ b/test/addons-napi/test_error/test_error.cc @@ -19,31 +19,51 @@ napi_value throwExistingError(napi_env env, napi_callback_info info) { napi_value message; napi_value error; NAPI_CALL(env, napi_create_string_utf8(env, "existing error", -1, &message)); - NAPI_CALL(env, napi_create_error(env, message, &error)); + NAPI_CALL(env, napi_create_error(env, nullptr, message, &error)); NAPI_CALL(env, napi_throw(env, error)); return nullptr; } napi_value throwError(napi_env env, napi_callback_info info) { - NAPI_CALL(env, napi_throw_error(env, "error")); + NAPI_CALL(env, napi_throw_error(env, nullptr, "error")); return nullptr; } napi_value throwRangeError(napi_env env, napi_callback_info info) { - NAPI_CALL(env, napi_throw_range_error(env, "range error")); + NAPI_CALL(env, napi_throw_range_error(env, nullptr, "range error")); return nullptr; } napi_value throwTypeError(napi_env env, napi_callback_info info) { - NAPI_CALL(env, napi_throw_type_error(env, "type error")); + NAPI_CALL(env, napi_throw_type_error(env, nullptr, "type error")); return nullptr; } +napi_value throwErrorCode(napi_env env, napi_callback_info info) { + NAPI_CALL(env, napi_throw_error(env, "ERR_TEST_CODE", "Error [error]")); + return nullptr; +} + +napi_value throwRangeErrorCode(napi_env env, napi_callback_info info) { + NAPI_CALL(env, napi_throw_range_error(env, + "ERR_TEST_CODE", + "RangeError [range error]")); + return nullptr; +} + +napi_value throwTypeErrorCode(napi_env env, napi_callback_info info) { + NAPI_CALL(env, napi_throw_type_error(env, + "ERR_TEST_CODE", + "TypeError [type error]")); + return nullptr; +} + + napi_value createError(napi_env env, napi_callback_info info) { napi_value result; napi_value message; NAPI_CALL(env, napi_create_string_utf8(env, "error", -1, &message)); - NAPI_CALL(env, napi_create_error(env, message, &result)); + NAPI_CALL(env, napi_create_error(env, nullptr, message, &result)); return result; } @@ -51,7 +71,7 @@ napi_value createRangeError(napi_env env, napi_callback_info info) { napi_value result; napi_value message; NAPI_CALL(env, napi_create_string_utf8(env, "range error", -1, &message)); - NAPI_CALL(env, napi_create_range_error(env, message, &result)); + NAPI_CALL(env, napi_create_range_error(env, nullptr, message, &result)); return result; } @@ -59,7 +79,43 @@ napi_value createTypeError(napi_env env, napi_callback_info info) { napi_value result; napi_value message; NAPI_CALL(env, napi_create_string_utf8(env, "type error", -1, &message)); - NAPI_CALL(env, napi_create_type_error(env, message, &result)); + NAPI_CALL(env, napi_create_type_error(env, nullptr, message, &result)); + return result; +} + +napi_value createErrorCode(napi_env env, napi_callback_info info) { + napi_value result; + napi_value message; + napi_value code; + NAPI_CALL(env, napi_create_string_utf8(env, "Error [error]", -1, &message)); + NAPI_CALL(env, napi_create_string_utf8(env, "ERR_TEST_CODE", -1, &code)); + NAPI_CALL(env, napi_create_error(env, code, message, &result)); + return result; +} + +napi_value createRangeErrorCode(napi_env env, napi_callback_info info) { + napi_value result; + napi_value message; + napi_value code; + NAPI_CALL(env, napi_create_string_utf8(env, + "RangeError [range error]", + -1, + &message)); + NAPI_CALL(env, napi_create_string_utf8(env, "ERR_TEST_CODE", -1, &code)); + NAPI_CALL(env, napi_create_range_error(env, code, message, &result)); + return result; +} + +napi_value createTypeErrorCode(napi_env env, napi_callback_info info) { + napi_value result; + napi_value message; + napi_value code; + NAPI_CALL(env, napi_create_string_utf8(env, + "TypeError [type error]", + -1, + &message)); + NAPI_CALL(env, napi_create_string_utf8(env, "ERR_TEST_CODE", -1, &code)); + NAPI_CALL(env, napi_create_type_error(env, code, message, &result)); return result; } @@ -70,9 +126,15 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) { DECLARE_NAPI_PROPERTY("throwError", throwError), DECLARE_NAPI_PROPERTY("throwRangeError", throwRangeError), DECLARE_NAPI_PROPERTY("throwTypeError", throwTypeError), + DECLARE_NAPI_PROPERTY("throwErrorCode", throwErrorCode), + DECLARE_NAPI_PROPERTY("throwRangeErrorCode", throwRangeErrorCode), + DECLARE_NAPI_PROPERTY("throwTypeErrorCode", throwTypeErrorCode), DECLARE_NAPI_PROPERTY("createError", createError), DECLARE_NAPI_PROPERTY("createRangeError", createRangeError), DECLARE_NAPI_PROPERTY("createTypeError", createTypeError), + DECLARE_NAPI_PROPERTY("createErrorCode", createErrorCode), + DECLARE_NAPI_PROPERTY("createRangeErrorCode", createRangeErrorCode), + DECLARE_NAPI_PROPERTY("createTypeErrorCode", createTypeErrorCode), }; NAPI_CALL_RETURN_VOID(env, napi_define_properties( diff --git a/test/addons-napi/test_typedarray/test_typedarray.c b/test/addons-napi/test_typedarray/test_typedarray.c index 4194492cae8b3a..ffc118681bad9e 100644 --- a/test/addons-napi/test_typedarray/test_typedarray.c +++ b/test/addons-napi/test_typedarray/test_typedarray.c @@ -65,7 +65,7 @@ napi_value Multiply(napi_env env, napi_callback_info info) { output_doubles[i] = input_doubles[i] * multiplier; } } else { - napi_throw_error(env, "Typed array was of a type not expected by test."); + napi_throw_error(env, NULL, "Typed array was of a type not expected by test."); return NULL; } diff --git a/test/common/index.js b/test/common/index.js index d762f4e0aa8498..a5328e29ec998f 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -625,6 +625,59 @@ Object.defineProperty(exports, 'hasIntl', { } }); +// Useful for testing expected internal/error objects +exports.expectsError = function expectsError(fn, settings, exact) { + if (typeof fn !== 'function') { + exact = settings; + settings = fn; + fn = undefined; + } + function innerFn(error) { + assert.strictEqual(error.code, settings.code); + if ('type' in settings) { + const type = settings.type; + if (type !== Error && !Error.isPrototypeOf(type)) { + throw new TypeError('`settings.type` must inherit from `Error`'); + } + assert(error instanceof type, + `${error.name} is not instance of ${type.name}`); + let typeName = error.constructor.name; + if (typeName === 'NodeError' && type.name !== 'NodeError') { + typeName = Object.getPrototypeOf(error.constructor).name; + } + assert.strictEqual(typeName, type.name); + } + if ('message' in settings) { + const message = settings.message; + if (typeof message === 'string') { + assert.strictEqual(error.message, message); + } else { + assert(message.test(error.message), + `${error.message} does not match ${message}`); + } + } + if ('name' in settings) { + assert.strictEqual(error.name, settings.name); + } + if (error.constructor.name === 'AssertionError') { + ['generatedMessage', 'actual', 'expected', 'operator'].forEach((key) => { + if (key in settings) { + const actual = error[key]; + const expected = settings[key]; + assert.strictEqual(actual, expected, + `${key}: expected ${expected}, not ${actual}`); + } + }); + } + return true; + } + if (fn) { + assert.throws(fn, innerFn); + return; + } + return exports.mustCall(innerFn, exact); +}; + // Crash the process on unhandled rejections. exports.crashOnUnhandledRejection = function() { process.on('unhandledRejection', From 25e46c52ac1a769a572c33882ead6eddb29da76f Mon Sep 17 00:00:00 2001 From: Kyle Farnung Date: Tue, 27 Jun 2017 17:54:44 -0700 Subject: [PATCH 080/183] n-api: add napi_fatal_error API PR-URL: https://github.com/nodejs/node/pull/13971 Reviewed-By: James M Snell Reviewed-By: Jason Ginchereau Reviewed-By: Ben Noordhuis Reviewed-By: Colin Ihrig Reviewed-By: Michael Dawson --- doc/api/n-api.md | 17 +++++++++++++++++ src/node_api.cc | 5 +++++ src/node_api.h | 9 +++++++++ test/addons-napi/test_fatal/binding.gyp | 8 ++++++++ test/addons-napi/test_fatal/test.js | 18 ++++++++++++++++++ test/addons-napi/test_fatal/test_fatal.c | 18 ++++++++++++++++++ 6 files changed, 75 insertions(+) create mode 100644 test/addons-napi/test_fatal/binding.gyp create mode 100644 test/addons-napi/test_fatal/test.js create mode 100644 test/addons-napi/test_fatal/test_fatal.c diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 875ced3579ecf0..8aac594f8323cd 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -533,6 +533,23 @@ Returns `napi_ok` if the API succeeded. This API returns true if an exception is pending. +### Fatal Errors + +In the event of an unrecoverable error in a native module, a fatal error can be +thrown to immediately terminate the process. + +#### napi_fatal_error + +```C +NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location, const char* message); +``` + +- `[in] location`: Optional location at which the error occurred. +- `[in] message`: The message associated with the error. + +The function call does not return, the process will be terminated. ## Object Lifetime management diff --git a/src/node_api.cc b/src/node_api.cc index 25e8c6385572e7..58aeaf08979501 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -826,6 +826,11 @@ napi_status napi_get_last_error_info(napi_env env, return napi_ok; } +NAPI_NO_RETURN void napi_fatal_error(const char* location, + const char* message) { + node::FatalError(location, message); +} + napi_status napi_create_function(napi_env env, const char* utf8name, napi_callback cb, diff --git a/src/node_api.h b/src/node_api.h index af98789d418210..f4d097ad104cf9 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -37,6 +37,12 @@ # define NAPI_MODULE_EXPORT __attribute__((visibility("default"))) #endif +#ifdef __GNUC__ +#define NAPI_NO_RETURN __attribute__((noreturn)) +#else +#define NAPI_NO_RETURN +#endif + typedef void (*napi_addon_register_func)(napi_env env, napi_value exports, @@ -104,6 +110,9 @@ NAPI_EXTERN napi_status napi_get_last_error_info(napi_env env, const napi_extended_error_info** result); +NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location, + const char* message); + // Getters for defined singletons NAPI_EXTERN napi_status napi_get_undefined(napi_env env, napi_value* result); NAPI_EXTERN napi_status napi_get_null(napi_env env, napi_value* result); diff --git a/test/addons-napi/test_fatal/binding.gyp b/test/addons-napi/test_fatal/binding.gyp new file mode 100644 index 00000000000000..ad661825f1fa9b --- /dev/null +++ b/test/addons-napi/test_fatal/binding.gyp @@ -0,0 +1,8 @@ +{ + "targets": [ + { + "target_name": "test_fatal", + "sources": [ "test_fatal.c" ] + } + ] +} diff --git a/test/addons-napi/test_fatal/test.js b/test/addons-napi/test_fatal/test.js new file mode 100644 index 00000000000000..aa37dc6803f0d5 --- /dev/null +++ b/test/addons-napi/test_fatal/test.js @@ -0,0 +1,18 @@ +'use strict'; +const common = require('../../common'); +const assert = require('assert'); +const child_process = require('child_process'); +const test_fatal = require(`./build/${common.buildType}/test_fatal`); + +// Test in a child process because the test code will trigger a fatal error +// that crashes the process. +if (process.argv[2] === 'child') { + test_fatal.Test(); + return; +} + +const p = child_process.spawnSync( + process.execPath, [ '--napi-modules', __filename, 'child' ]); +assert.ifError(p.error); +assert.ok(p.stderr.toString().includes( + 'FATAL ERROR: test_fatal::Test fatal message')); diff --git a/test/addons-napi/test_fatal/test_fatal.c b/test/addons-napi/test_fatal/test_fatal.c new file mode 100644 index 00000000000000..1e8a1cf7955d18 --- /dev/null +++ b/test/addons-napi/test_fatal/test_fatal.c @@ -0,0 +1,18 @@ +#include +#include "../common.h" + +napi_value Test(napi_env env, napi_callback_info info) { + napi_fatal_error("test_fatal::Test", "fatal message"); + return NULL; +} + +void Init(napi_env env, napi_value exports, napi_value module, void* priv) { + napi_property_descriptor properties[] = { + DECLARE_NAPI_PROPERTY("Test", Test), + }; + + NAPI_CALL_RETURN_VOID(env, napi_define_properties( + env, exports, sizeof(properties) / sizeof(*properties), properties)); +} + +NAPI_MODULE(addon, Init) From bc83e71e1651637f59d367cc11e7938bfa62cf72 Mon Sep 17 00:00:00 2001 From: "Song, Bintao Garfield" Date: Sun, 16 Jul 2017 15:08:33 +0800 Subject: [PATCH 081/183] test: replace string concat with template literal Replace the string concat at test/addons-napi/test_reference/test.js. PR-URL: https://github.com/nodejs/node/pull/14269 Reviewed-By: Joyee Cheung Reviewed-By: Rich Trott Reviewed-By: Colin Ihrig --- test/addons-napi/test_reference/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/addons-napi/test_reference/test.js b/test/addons-napi/test_reference/test.js index aaf5531f39e1f3..14932a74ca70b0 100644 --- a/test/addons-napi/test_reference/test.js +++ b/test/addons-napi/test_reference/test.js @@ -22,7 +22,7 @@ function runTests(i, title, tests) { try { tests[i](); } catch (e) { - console.error('Test failed: ' + title); + console.error(`Test failed: ${title}`); throw e; } setImmediate(() => { From e88566170015c68a043bd3bf5bfa112d59097a26 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Thu, 20 Jul 2017 21:01:51 +0300 Subject: [PATCH 082/183] doc: fix some links PR-URL: https://github.com/nodejs/node/pull/14400 Reviewed-By: Colin Ihrig Reviewed-By: Luigi Pinca Reviewed-By: Yuta Hiroto Reviewed-By: Khaidi Chu Reviewed-By: James M Snell --- doc/api/dns.md | 4 ++-- doc/api/n-api.md | 1 + doc/guides/writing-tests.md | 11 +++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/doc/api/dns.md b/doc/api/dns.md index 7fce91970aa10e..1052a46c200f3f 100644 --- a/doc/api/dns.md +++ b/doc/api/dns.md @@ -175,7 +175,7 @@ added: v0.1.27 - `rrtype` {string} Resource record type. Default: `'A'`. - `callback` {Function} - `err` {Error} - - `records` {string[] | Object[] | string[][] | Object} + - `records` {string[] | Object[] | Object} Uses the DNS protocol to resolve a hostname (e.g. `'nodejs.org'`) into an array of the resource records. The `callback` function has arguments @@ -362,7 +362,7 @@ added: v0.1.27 - `hostname` {string} - `callback` {Function} - `err` {Error} - - `records` {string[][]} + - `records` {string[]} Uses the DNS protocol to resolve text queries (`TXT` records) for the `hostname`. The `records` argument passed to the `callback` function is a diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 8aac594f8323cd..91780846ff2e4a 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -3204,6 +3204,7 @@ support it: [`napi_queue_async_work`]: #n_api_napi_queue_async_work [`napi_reference_ref`]: #n_api_napi_reference_ref [`napi_reference_unref`]: #n_api_napi_reference_unref +[`napi_throw`]: #n_api_napi_throw [`napi_throw_error`]: #n_api_napi_throw_error [`napi_throw_range_error`]: #n_api_napi_throw_range_error [`napi_throw_type_error`]: #n_api_napi_throw_type_error diff --git a/doc/guides/writing-tests.md b/doc/guides/writing-tests.md index aac098640aec9e..cc87886656c06d 100644 --- a/doc/guides/writing-tests.md +++ b/doc/guides/writing-tests.md @@ -92,7 +92,7 @@ The test checks functionality in the `http` module. Most tests use the `assert` module to confirm expectations of the test. The require statements are sorted in -[ASCII](http://man7.org/linux/man-pages/man7/ascii.7.html) order (digits, upper +[ASCII][] order (digits, upper case, `_`, lower case). ### **Lines 10-21** @@ -252,9 +252,9 @@ assert.throws( For performance considerations, we only use a selected subset of ES.Next features in JavaScript code in the `lib` directory. However, when writing tests, for the ease of backporting, it is encouraged to use those ES.Next -features that can be used directly without a flag in [all maintained branches] -(https://github.com/nodejs/lts). [node.green](http://node.green/) lists -available features in each release. +features that can be used directly without a flag in +[all maintained branches][]. [node.green][] lists available features +in each release. For example: @@ -279,8 +279,7 @@ functions worked correctly with the `beforeExit` event, then it might be named ### Web Platform Tests Some of the tests for the WHATWG URL implementation (named -`test-whatwg-url-*.js`) are imported from the -[Web Platform Tests Project](https://github.com/w3c/web-platform-tests/tree/master/url). +`test-whatwg-url-*.js`) are imported from the [Web Platform Tests Project][]. These imported tests will be wrapped like this: ```js From 55e3ca88bc1c40d582bf76eaba7059ba01386d41 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 20 Jul 2017 17:03:11 +0200 Subject: [PATCH 083/183] n-api: add fast paths for integer getters Ref: https://github.com/nodejs/node/issues/14379 PR-URL: https://github.com/nodejs/node/pull/14393 Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell Reviewed-By: Timothy Gu Reviewed-By: Colin Ihrig Reviewed-By: Jason Ginchereau --- src/node_api.cc | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/node_api.cc b/src/node_api.cc index 58aeaf08979501..986a9facccac9d 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -1912,6 +1912,12 @@ napi_status napi_get_value_int32(napi_env env, CHECK_ARG(env, result); v8::Local val = v8impl::V8LocalValueFromJsValue(value); + + if (val->IsInt32()) { + *result = val.As()->Value(); + return napi_clear_last_error(env); + } + RETURN_STATUS_IF_FALSE(env, val->IsNumber(), napi_number_expected); v8::Isolate* isolate = env->isolate; @@ -1931,6 +1937,12 @@ napi_status napi_get_value_uint32(napi_env env, CHECK_ARG(env, result); v8::Local val = v8impl::V8LocalValueFromJsValue(value); + + if (val->IsUint32()) { + *result = val.As()->Value(); + return napi_clear_last_error(env); + } + RETURN_STATUS_IF_FALSE(env, val->IsNumber(), napi_number_expected); v8::Isolate* isolate = env->isolate; @@ -1950,6 +1962,13 @@ napi_status napi_get_value_int64(napi_env env, CHECK_ARG(env, result); v8::Local val = v8impl::V8LocalValueFromJsValue(value); + + // This is still a fast path very likely to be taken. + if (val->IsInt32()) { + *result = val.As()->Value(); + return napi_clear_last_error(env); + } + RETURN_STATUS_IF_FALSE(env, val->IsNumber(), napi_number_expected); // v8::Value::IntegerValue() converts NaN to INT64_MIN, inconsistent with From 39ce757a48ec2cafb60c99b7fed6a03bd9ff0d37 Mon Sep 17 00:00:00 2001 From: Kyle Farnung Date: Wed, 12 Jul 2017 17:14:00 -0700 Subject: [PATCH 084/183] n-api: directly create Local from Persistent The `v8::PersistentBase.Get` method didn't exist in node 4 and it's just a helper which creates a new `v8::Local` from the given object. PR-URL: https://github.com/nodejs/node/pull/14211 Reviewed-By: Timothy Gu Reviewed-By: Colin Ihrig Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Michael Dawson Reviewed-By: Jason Ginchereau --- src/node_api.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/node_api.cc b/src/node_api.cc index 986a9facccac9d..06f18562e7d3c0 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -58,7 +58,8 @@ struct napi_env__ { (destination)->SetInternalFieldCount((field_count)); \ (env)->prefix ## _template.Reset(isolate, (destination)); \ } else { \ - (destination) = env->prefix ## _template.Get(isolate); \ + (destination) = v8::Local::New( \ + isolate, env->prefix ## _template); \ } \ } while (0) From 8fab9bc7482af0a7381c243186844e44febbd860 Mon Sep 17 00:00:00 2001 From: Pratik Jain Date: Mon, 24 Jul 2017 11:37:31 +0000 Subject: [PATCH 085/183] test: changed error message validator Replaced TypeError with RegEx to match the exact error message in file test/addons-napi/test_properties/test.js. The RegEx will check the validity of the error being thrown. PR-URL: https://github.com/nodejs/node/pull/14443 Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Yuta Hiroto Reviewed-By: Vse Mozhet Byt Reviewed-By: Rich Trott Reviewed-By: Anna Henningsen --- test/addons-napi/test_properties/test.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/addons-napi/test_properties/test.js b/test/addons-napi/test_properties/test.js index 8d11a669d46daf..0a3bbee853610f 100644 --- a/test/addons-napi/test_properties/test.js +++ b/test/addons-napi/test_properties/test.js @@ -1,6 +1,7 @@ 'use strict'; const common = require('../../common'); const assert = require('assert'); +const readonlyErrorRE = /^TypeError: Cannot assign to read only property '.*' of object '#'$/; // Testing api calls for defining properties const test_object = require(`./build/${common.buildType}/test_properties`); @@ -12,7 +13,7 @@ assert.strictEqual(test_object.readwriteValue, 1); test_object.readwriteValue = 2; assert.strictEqual(test_object.readwriteValue, 2); -assert.throws(() => { test_object.readonlyValue = 3; }, TypeError); +assert.throws(() => { test_object.readonlyValue = 3; }, readonlyErrorRE); assert.ok(test_object.hiddenValue); @@ -42,11 +43,11 @@ assert.strictEqual(symbolDescription, 'NameKeySymbol'); test_object.readwriteAccessor1 = 1; assert.strictEqual(test_object.readwriteAccessor1, 1); assert.strictEqual(test_object.readonlyAccessor1, 1); -assert.throws(() => { test_object.readonlyAccessor1 = 3; }, TypeError); +assert.throws(() => { test_object.readonlyAccessor1 = 3; }, readonlyErrorRE); test_object.readwriteAccessor2 = 2; assert.strictEqual(test_object.readwriteAccessor2, 2); assert.strictEqual(test_object.readonlyAccessor2, 2); -assert.throws(() => { test_object.readonlyAccessor2 = 3; }, TypeError); +assert.throws(() => { test_object.readonlyAccessor2 = 3; }, readonlyErrorRE); assert.strictEqual(test_object.hasNamedProperty(test_object, 'echo'), true); assert.strictEqual(test_object.hasNamedProperty(test_object, 'hiddenValue'), From b5ef731373b45ccf6a26411740112bb274ba5333 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Thu, 13 Jul 2017 11:35:55 +0300 Subject: [PATCH 086/183] n-api: re-use napi_env between modules Store the `napi_env` on the global object at a private key. This gives us one `napi_env` per context. Refs: https://github.com/nodejs/node/issues/14367 PR-URL: https://github.com/nodejs/node/pull/14217 Reviewed-By: Ben Noordhuis Reviewed-By: Timothy Gu --- src/node_api.cc | 45 +++++++++++++++++-- test/addons-napi/test_env_sharing/binding.gyp | 12 +++++ .../test_env_sharing/compare_env.c | 22 +++++++++ test/addons-napi/test_env_sharing/store_env.c | 12 +++++ test/addons-napi/test_env_sharing/test.js | 10 +++++ 5 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 test/addons-napi/test_env_sharing/binding.gyp create mode 100644 test/addons-napi/test_env_sharing/compare_env.c create mode 100644 test/addons-napi/test_env_sharing/store_env.c create mode 100644 test/addons-napi/test_env_sharing/test.js diff --git a/src/node_api.cc b/src/node_api.cc index 06f18562e7d3c0..3cdd4b8a970f28 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -712,6 +712,45 @@ bool FindWrapper(v8::Local obj, return true; } +static void DeleteEnv(napi_env env, void* data, void* hint) { + delete env; +} + +napi_env GetEnv(v8::Local context) { + napi_env result; + + auto isolate = context->GetIsolate(); + auto global = context->Global(); + + // In the case of the string for which we grab the private and the value of + // the private on the global object we can call .ToLocalChecked() directly + // because we need to stop hard if either of them is empty. + // + // Re https://github.com/nodejs/node/pull/14217#discussion_r128775149 + auto key = v8::Private::ForApi(isolate, + v8::String::NewFromOneByte(isolate, + reinterpret_cast("N-API Environment"), + v8::NewStringType::kInternalized).ToLocalChecked()); + auto value = global->GetPrivate(context, key).ToLocalChecked(); + + if (value->IsExternal()) { + result = static_cast(value.As()->Value()); + } else { + result = new napi_env__(isolate); + auto external = v8::External::New(isolate, result); + + // We must also stop hard if the result of assigning the env to the global + // is either nothing or false. + CHECK(global->SetPrivate(context, key, external).FromJust()); + + // Create a self-destructing reference to external that will get rid of the + // napi_env when external goes out of scope. + Reference::New(result, external, 0, true, DeleteEnv, nullptr, nullptr); + } + + return result; +} + } // end of namespace v8impl // Intercepts the Node-V8 module registration callback. Converts parameters @@ -723,9 +762,9 @@ void napi_module_register_cb(v8::Local exports, void* priv) { napi_module* mod = static_cast(priv); - // Create a new napi_env for this module. Once module unloading is supported - // we shall have to call delete on this object from there. - napi_env env = new napi_env__(context->GetIsolate()); + // Create a new napi_env for this module or reference one if a pre-existing + // one is found. + napi_env env = v8impl::GetEnv(context); mod->nm_register_func( env, diff --git a/test/addons-napi/test_env_sharing/binding.gyp b/test/addons-napi/test_env_sharing/binding.gyp new file mode 100644 index 00000000000000..5699a8391dd347 --- /dev/null +++ b/test/addons-napi/test_env_sharing/binding.gyp @@ -0,0 +1,12 @@ +{ + "targets": [ + { + "target_name": "store_env", + "sources": [ "store_env.c" ] + }, + { + "target_name": "compare_env", + "sources": [ "compare_env.c" ] + } + ] +} diff --git a/test/addons-napi/test_env_sharing/compare_env.c b/test/addons-napi/test_env_sharing/compare_env.c new file mode 100644 index 00000000000000..b209c8f6d8e968 --- /dev/null +++ b/test/addons-napi/test_env_sharing/compare_env.c @@ -0,0 +1,22 @@ +#include +#include "../common.h" + +napi_value compare(napi_env env, napi_callback_info info) { + napi_value external; + size_t argc = 1; + void* data; + napi_value return_value; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &external, NULL, NULL)); + NAPI_CALL(env, napi_get_value_external(env, external, &data)); + NAPI_CALL(env, napi_get_boolean(env, ((napi_env)data) == env, &return_value)); + + return return_value; +} + +void Init(napi_env env, napi_value exports, napi_value module, void* context) { + napi_property_descriptor prop = DECLARE_NAPI_PROPERTY("exports", compare); + NAPI_CALL_RETURN_VOID(env, napi_define_properties(env, module, 1, &prop)); +} + +NAPI_MODULE(compare_env, Init) diff --git a/test/addons-napi/test_env_sharing/store_env.c b/test/addons-napi/test_env_sharing/store_env.c new file mode 100644 index 00000000000000..391f6faa27648e --- /dev/null +++ b/test/addons-napi/test_env_sharing/store_env.c @@ -0,0 +1,12 @@ +#include +#include "../common.h" + +void Init(napi_env env, napi_value exports, napi_value module, void* context) { + napi_value external; + NAPI_CALL_RETURN_VOID(env, + napi_create_external(env, env, NULL, NULL, &external)); + NAPI_CALL_RETURN_VOID(env, + napi_set_named_property(env, module, "exports", external)); +} + +NAPI_MODULE(store_env, Init) diff --git a/test/addons-napi/test_env_sharing/test.js b/test/addons-napi/test_env_sharing/test.js new file mode 100644 index 00000000000000..6e21bf4c638b80 --- /dev/null +++ b/test/addons-napi/test_env_sharing/test.js @@ -0,0 +1,10 @@ +'use strict'; + +const common = require('../../common'); +const storeEnv = require(`./build/${common.buildType}/store_env`); +const compareEnv = require(`./build/${common.buildType}/compare_env`); +const assert = require('assert'); + +assert.strictEqual(compareEnv(storeEnv), true, + 'N-API environment pointers in two different modules have ' + + 'the same value'); From 1fe20045822e7b4cda1af10c9b2e1a03b9e1a0ca Mon Sep 17 00:00:00 2001 From: Shivanth MP Date: Thu, 20 Jul 2017 01:50:43 +0530 Subject: [PATCH 087/183] n-api: add support for DataView Basic support for Dataview is added in this commit. This is achieved by using three functions, napi_create_dataview(), napi_is_dataview() and napi_get_dataview_info(). PR-URL: https://github.com/nodejs/node/pull/14382 Fixes: https://github.com/nodejs/node/issues/13926 Reviewed-By: Anna Henningsen Reviewed-By: Timothy Gu Reviewed-By: Colin Ihrig --- doc/api/n-api.md | 84 +++++++++++++++++++ src/node_api.cc | 66 +++++++++++++++ src/node_api.h | 15 ++++ test/addons-napi/test_dataview/binding.gyp | 8 ++ test/addons-napi/test_dataview/test.js | 14 ++++ .../addons-napi/test_dataview/test_dataview.c | 49 +++++++++++ 6 files changed, 236 insertions(+) create mode 100644 test/addons-napi/test_dataview/binding.gyp create mode 100644 test/addons-napi/test_dataview/test.js create mode 100644 test/addons-napi/test_dataview/test_dataview.c diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 91780846ff2e4a..8f8aa76f83ac7b 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -1331,6 +1331,40 @@ JavaScript TypedArray Objects are described in [Section 22.2](https://tc39.github.io/ecma262/#sec-typedarray-objects) of the ECMAScript Language Specification. + +#### *napi_create_dataview* + + +```C +napi_status napi_create_dataview(napi_env env, + size_t byte_length, + napi_value arraybuffer, + size_t byte_offset, + napi_value* result) + +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] length`: Number of elements in the DataView. +- `[in] arraybuffer`: ArrayBuffer underlying the DataView. +- `[in] byte_offset`: The byte offset within the ArrayBuffer from which to + start projecting the DataView. +- `[out] result`: A `napi_value` representing a JavaScript DataView. + +Returns `napi_ok` if the API succeeded. + +This API creates a JavaScript DataView object over an existing ArrayBuffer. +DataView objects provide an array-like view over an underlying data buffer, +but one which allows items of different size and type in the ArrayBuffer. + +It is required that `byte_length + byte_offset` is less than or equal to the +size in bytes of the array passed in. If not, a RangeError exception is raised. + +JavaScript DataView Objects are described in +[Section 24.3][] of the ECMAScript Language Specification. + ### Functions to convert from C types to N-API #### *napi_create_number* + +```C +napi_status napi_get_dataview_info(napi_env env, + napi_value dataview, + size_t* byte_length, + void** data, + napi_value* arraybuffer, + size_t* byte_offset) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] dataview`: `napi_value` representing the DataView whose + properties to query. +- `[out] byte_length`: Number of bytes in the DataView. +- `[out] data`: The data buffer underlying the DataView. +- `[out] arraybuffer`: ArrayBuffer underlying the DataView. +- `[out] byte_offset`: The byte offset within the data buffer from which + to start projecting the DataView. + +Returns `napi_ok` if the API succeeded. + +This API returns various properties of a DataView. + + #### *napi_get_value_bool* + +```C +napi_status napi_is_dataview(napi_env env, napi_value value, bool* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: The JavaScript value to check. +- `[out] result`: Whether the given `napi_value` represents a DataView. + +Returns `napi_ok` if the API succeeded. + +This API checks if the Object passed in is a DataView. + ### *napi_strict_equals* +```C +napi_status napi_create_int32(napi_env env, int32_t value, napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: Integer value to be represented in JavaScript. +- `[out] result`: A `napi_value` representing a JavaScript Number. + +Returns `napi_ok` if the API succeeded. + +This API is used to convert from the C `int32_t` type to the JavaScript +Number type. + +The JavaScript Number type is described in +[Section 6.1.6](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type) +of the ECMAScript Language Specification. + +#### *napi_create_uint32* + +```C +napi_status napi_create_uint32(napi_env env, uint32_t value, napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: Unsigned integer value to be represented in JavaScript. +- `[out] result`: A `napi_value` representing a JavaScript Number. + +Returns `napi_ok` if the API succeeded. + +This API is used to convert from the C `uint32_t` type to the JavaScript +Number type. + +The JavaScript Number type is described in +[Section 6.1.6](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type) +of the ECMAScript Language Specification. + +#### *napi_create_int64* + +```C +napi_status napi_create_int64(napi_env env, int64_t value, napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: Integer value to be represented in JavaScript. +- `[out] result`: A `napi_value` representing a JavaScript Number. + +Returns `napi_ok` if the API succeeded. + +This API is used to convert from the C `int64_t` type to the JavaScript +Number type. + +The JavaScript Number type is described in +[Section 6.1.6](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type) +of the ECMAScript Language Specification. Note the complete range of `int64_t` +cannot be represented with full precision in JavaScript. Integer values +outside the range of +[`Number.MIN_SAFE_INTEGER`](https://tc39.github.io/ecma262/#sec-number.min_safe_integer) +-(2^53 - 1) - +[`Number.MAX_SAFE_INTEGER`](https://tc39.github.io/ecma262/#sec-number.max_safe_integer) +(2^53 - 1) will lose precision. + +#### *napi_create_double* + ```C -napi_status napi_create_number(napi_env env, double value, napi_value* result) +napi_status napi_create_double(napi_env env, double value, napi_value* result) ``` - `[in] env`: The environment that the API is invoked under. @@ -1386,7 +1455,7 @@ napi_status napi_create_number(napi_env env, double value, napi_value* result) Returns `napi_ok` if the API succeeded. -This API is used to convert from the C double type to the JavaScript +This API is used to convert from the C `double` type to the JavaScript Number type. The JavaScript Number type is described in @@ -2170,7 +2239,7 @@ status = napi_create_object(env, &obj); if (status != napi_ok) return status; // Create a napi_value for 123 -status = napi_create_number(env, 123, &value); +status = napi_create_int32(env, 123, &value); if (status != napi_ok) return status; // obj.myProp = 123 @@ -2244,9 +2313,9 @@ if (status != napi_ok) return status; // Create napi_values for 123 and 456 napi_value fooValue, barValue; -status = napi_create_number(env, 123, &fooValue); +status = napi_create_int32(env, 123, &fooValue); if (status != napi_ok) return status; -status = napi_create_number(env, 456, &barValue); +status = napi_create_int32(env, 456, &barValue); if (status != napi_ok) return status; // Set the properties @@ -2707,7 +2776,7 @@ status = napi_get_named_property(env, global, "AddTwo", &add_two); if (status != napi_ok) return; // const arg = 1337 -status = napi_create_number(env, 1337, &arg); +status = napi_create_int32(env, 1337, &arg); if (status != napi_ok) return; napi_value* argv = &arg; diff --git a/src/node_api.cc b/src/node_api.cc index 932ec858963558..948f6afef55291 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -821,9 +821,7 @@ const char* error_messages[] = {nullptr, "The async work item was cancelled", "napi_escape_handle already called on scope"}; -static napi_status napi_clear_last_error(napi_env env) { - CHECK_ENV(env); - +static inline napi_status napi_clear_last_error(napi_env env) { env->last_error.error_code = napi_ok; // TODO(boingoing): Should this be a callback? @@ -832,13 +830,13 @@ static napi_status napi_clear_last_error(napi_env env) { return napi_ok; } -static napi_status napi_set_last_error(napi_env env, napi_status error_code, - uint32_t engine_error_code, - void* engine_reserved) { +static inline +napi_status napi_set_last_error(napi_env env, napi_status error_code, + uint32_t engine_error_code, + void* engine_reserved) { env->last_error.error_code = error_code; env->last_error.engine_error_code = engine_error_code; env->last_error.engine_reserved = engine_reserved; - return error_code; } @@ -1439,42 +1437,42 @@ napi_status napi_get_prototype(napi_env env, } napi_status napi_create_object(napi_env env, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, result); *result = v8impl::JsValueFromV8LocalValue( v8::Object::New(env->isolate)); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_create_array(napi_env env, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, result); *result = v8impl::JsValueFromV8LocalValue( v8::Array::New(env->isolate)); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_create_array_with_length(napi_env env, size_t length, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, result); *result = v8impl::JsValueFromV8LocalValue( v8::Array::New(env->isolate, length)); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_create_string_latin1(napi_env env, const char* str, size_t length, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, result); auto isolate = env->isolate; @@ -1486,28 +1484,28 @@ napi_status napi_create_string_latin1(napi_env env, CHECK_MAYBE_EMPTY(env, str_maybe, napi_generic_failure); *result = v8impl::JsValueFromV8LocalValue(str_maybe.ToLocalChecked()); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_create_string_utf8(napi_env env, const char* str, size_t length, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, result); v8::Local s; CHECK_NEW_FROM_UTF8_LEN(env, s, str, length); *result = v8impl::JsValueFromV8LocalValue(s); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_create_string_utf16(napi_env env, const char16_t* str, size_t length, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, result); auto isolate = env->isolate; @@ -1519,19 +1517,55 @@ napi_status napi_create_string_utf16(napi_env env, CHECK_MAYBE_EMPTY(env, str_maybe, napi_generic_failure); *result = v8impl::JsValueFromV8LocalValue(str_maybe.ToLocalChecked()); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } -napi_status napi_create_number(napi_env env, +napi_status napi_create_double(napi_env env, double value, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, result); *result = v8impl::JsValueFromV8LocalValue( v8::Number::New(env->isolate, value)); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); +} + +napi_status napi_create_int32(napi_env env, + int32_t value, + napi_value* result) { + CHECK_ENV(env); + CHECK_ARG(env, result); + + *result = v8impl::JsValueFromV8LocalValue( + v8::Integer::New(env->isolate, value)); + + return napi_clear_last_error(env); +} + +napi_status napi_create_uint32(napi_env env, + uint32_t value, + napi_value* result) { + CHECK_ENV(env); + CHECK_ARG(env, result); + + *result = v8impl::JsValueFromV8LocalValue( + v8::Integer::NewFromUnsigned(env->isolate, value)); + + return napi_clear_last_error(env); +} + +napi_status napi_create_int64(napi_env env, + int64_t value, + napi_value* result) { + CHECK_ENV(env); + CHECK_ARG(env, result); + + *result = v8impl::JsValueFromV8LocalValue( + v8::Number::New(env->isolate, static_cast(value))); + + return napi_clear_last_error(env); } napi_status napi_get_boolean(napi_env env, bool value, napi_value* result) { @@ -1552,7 +1586,7 @@ napi_status napi_get_boolean(napi_env env, bool value, napi_value* result) { napi_status napi_create_symbol(napi_env env, napi_value description, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, result); v8::Isolate* isolate = env->isolate; @@ -1567,7 +1601,7 @@ napi_status napi_create_symbol(napi_env env, v8::Symbol::New(isolate, desc.As())); } - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } static napi_status set_error_code(napi_env env, @@ -1627,7 +1661,7 @@ napi_status napi_create_error(napi_env env, napi_value code, napi_value msg, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, msg); CHECK_ARG(env, result); @@ -1641,14 +1675,14 @@ napi_status napi_create_error(napi_env env, *result = v8impl::JsValueFromV8LocalValue(error_obj); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_create_type_error(napi_env env, napi_value code, napi_value msg, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, msg); CHECK_ARG(env, result); @@ -1662,14 +1696,14 @@ napi_status napi_create_type_error(napi_env env, *result = v8impl::JsValueFromV8LocalValue(error_obj); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_create_range_error(napi_env env, napi_value code, napi_value msg, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, msg); CHECK_ARG(env, result); @@ -1683,7 +1717,7 @@ napi_status napi_create_range_error(napi_env env, *result = v8impl::JsValueFromV8LocalValue(error_obj); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_typeof(napi_env env, @@ -1954,14 +1988,13 @@ napi_status napi_get_value_int32(napi_env env, if (val->IsInt32()) { *result = val.As()->Value(); - return napi_clear_last_error(env); - } - - RETURN_STATUS_IF_FALSE(env, val->IsNumber(), napi_number_expected); + } else { + RETURN_STATUS_IF_FALSE(env, val->IsNumber(), napi_number_expected); - v8::Isolate* isolate = env->isolate; - v8::Local context = isolate->GetCurrentContext(); - *result = val->Int32Value(context).FromJust(); + // Empty context: https://github.com/nodejs/node/issues/14379 + v8::Local context; + *result = val->Int32Value(context).FromJust(); + } return napi_clear_last_error(env); } @@ -1979,14 +2012,13 @@ napi_status napi_get_value_uint32(napi_env env, if (val->IsUint32()) { *result = val.As()->Value(); - return napi_clear_last_error(env); - } - - RETURN_STATUS_IF_FALSE(env, val->IsNumber(), napi_number_expected); + } else { + RETURN_STATUS_IF_FALSE(env, val->IsNumber(), napi_number_expected); - v8::Isolate* isolate = env->isolate; - v8::Local context = isolate->GetCurrentContext(); - *result = val->Uint32Value(context).FromJust(); + // Empty context: https://github.com/nodejs/node/issues/14379 + v8::Local context; + *result = val->Uint32Value(context).FromJust(); + } return napi_clear_last_error(env); } @@ -2016,8 +2048,8 @@ napi_status napi_get_value_int64(napi_env env, if (std::isnan(doubleValue)) { *result = 0; } else { - v8::Isolate* isolate = env->isolate; - v8::Local context = isolate->GetCurrentContext(); + // Empty context: https://github.com/nodejs/node/issues/14379 + v8::Local context; *result = val->IntegerValue(context).FromJust(); } @@ -2796,11 +2828,10 @@ napi_status napi_get_buffer_info(napi_env env, napi_value value, void** data, size_t* length) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, value); - v8::Local buffer = - v8impl::V8LocalValueFromJsValue(value).As(); + v8::Local buffer = v8impl::V8LocalValueFromJsValue(value); if (data != nullptr) { *data = node::Buffer::Data(buffer); @@ -2809,7 +2840,7 @@ napi_status napi_get_buffer_info(napi_env env, *length = node::Buffer::Length(buffer); } - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_is_arraybuffer(napi_env env, napi_value value, bool* result) { diff --git a/src/node_api.h b/src/node_api.h index 5a2ba946ec8114..cf4128be210fe7 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -127,9 +127,18 @@ NAPI_EXTERN napi_status napi_create_array(napi_env env, napi_value* result); NAPI_EXTERN napi_status napi_create_array_with_length(napi_env env, size_t length, napi_value* result); -NAPI_EXTERN napi_status napi_create_number(napi_env env, +NAPI_EXTERN napi_status napi_create_double(napi_env env, double value, napi_value* result); +NAPI_EXTERN napi_status napi_create_int32(napi_env env, + int32_t value, + napi_value* result); +NAPI_EXTERN napi_status napi_create_uint32(napi_env env, + uint32_t value, + napi_value* result); +NAPI_EXTERN napi_status napi_create_int64(napi_env env, + int64_t value, + napi_value* result); NAPI_EXTERN napi_status napi_create_string_latin1(napi_env env, const char* str, size_t length, diff --git a/test/addons-napi/2_function_arguments/binding.c b/test/addons-napi/2_function_arguments/binding.c index a5ccac7b61c458..92f89fd2ffa199 100644 --- a/test/addons-napi/2_function_arguments/binding.c +++ b/test/addons-napi/2_function_arguments/binding.c @@ -24,7 +24,7 @@ napi_value Add(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_double(env, args[1], &value1)); napi_value sum; - NAPI_CALL(env, napi_create_number(env, value0 + value1, &sum)); + NAPI_CALL(env, napi_create_double(env, value0 + value1, &sum)); return sum; } diff --git a/test/addons-napi/6_object_wrap/myobject.cc b/test/addons-napi/6_object_wrap/myobject.cc index c2557facc06382..56b00ddae49a32 100644 --- a/test/addons-napi/6_object_wrap/myobject.cc +++ b/test/addons-napi/6_object_wrap/myobject.cc @@ -86,7 +86,7 @@ napi_value MyObject::GetValue(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_unwrap(env, _this, reinterpret_cast(&obj))); napi_value num; - NAPI_CALL(env, napi_create_number(env, obj->value_, &num)); + NAPI_CALL(env, napi_create_double(env, obj->value_, &num)); return num; } @@ -116,7 +116,7 @@ napi_value MyObject::PlusOne(napi_env env, napi_callback_info info) { obj->value_ += 1; napi_value num; - NAPI_CALL(env, napi_create_number(env, obj->value_, &num)); + NAPI_CALL(env, napi_create_double(env, obj->value_, &num)); return num; } @@ -140,7 +140,7 @@ napi_value MyObject::Multiply(napi_env env, napi_callback_info info) { const int kArgCount = 1; napi_value argv[kArgCount]; - NAPI_CALL(env, napi_create_number(env, obj->value_ * multiple, argv)); + NAPI_CALL(env, napi_create_double(env, obj->value_ * multiple, argv)); napi_value instance; NAPI_CALL(env, napi_new_instance(env, cons, kArgCount, argv, &instance)); diff --git a/test/addons-napi/7_factory_wrap/myobject.cc b/test/addons-napi/7_factory_wrap/myobject.cc index d6b374d7bb7ff8..4a2b284439ddb9 100644 --- a/test/addons-napi/7_factory_wrap/myobject.cc +++ b/test/addons-napi/7_factory_wrap/myobject.cc @@ -45,7 +45,7 @@ napi_value MyObject::New(napi_env env, napi_callback_info info) { if (valuetype == napi_undefined) { obj->counter_ = 0; } else { - NAPI_CALL(env, napi_get_value_double(env, args[0], &obj->counter_)); + NAPI_CALL(env, napi_get_value_uint32(env, args[0], &obj->counter_)); } obj->env_ = env; @@ -88,7 +88,7 @@ napi_value MyObject::PlusOne(napi_env env, napi_callback_info info) { obj->counter_ += 1; napi_value num; - NAPI_CALL(env, napi_create_number(env, obj->counter_, &num)); + NAPI_CALL(env, napi_create_uint32(env, obj->counter_, &num)); return num; } diff --git a/test/addons-napi/7_factory_wrap/myobject.h b/test/addons-napi/7_factory_wrap/myobject.h index c0b8522c609c4c..28ca94d16e30dd 100644 --- a/test/addons-napi/7_factory_wrap/myobject.h +++ b/test/addons-napi/7_factory_wrap/myobject.h @@ -18,7 +18,7 @@ class MyObject { static napi_ref constructor; static napi_value New(napi_env env, napi_callback_info info); static napi_value PlusOne(napi_env env, napi_callback_info info); - double counter_; + uint32_t counter_; napi_env env_; napi_ref wrapper_; }; diff --git a/test/addons-napi/8_passing_wrapped/binding.cc b/test/addons-napi/8_passing_wrapped/binding.cc index 581638bdfa2b07..c22ac6442f6c4e 100644 --- a/test/addons-napi/8_passing_wrapped/binding.cc +++ b/test/addons-napi/8_passing_wrapped/binding.cc @@ -24,7 +24,7 @@ napi_value Add(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_unwrap(env, args[1], reinterpret_cast(&obj2))); napi_value sum; - NAPI_CALL(env, napi_create_number(env, obj1->Val() + obj2->Val(), &sum)); + NAPI_CALL(env, napi_create_double(env, obj1->Val() + obj2->Val(), &sum)); return sum; } diff --git a/test/addons-napi/test_async/test_async.cc b/test/addons-napi/test_async/test_async.cc index f257b268b93159..ca76fa2d33b132 100644 --- a/test/addons-napi/test_async/test_async.cc +++ b/test/addons-napi/test_async/test_async.cc @@ -52,7 +52,7 @@ void Complete(napi_env env, napi_status status, void* data) { napi_value argv[2]; NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &argv[0])); - NAPI_CALL_RETURN_VOID(env, napi_create_number(env, c->_output, &argv[1])); + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, c->_output, &argv[1])); napi_value callback; NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, c->_callback, &callback)); diff --git a/test/addons-napi/test_buffer/test_buffer.c b/test/addons-napi/test_buffer/test_buffer.c index 880149e1ff8561..0e12bedfb5602f 100644 --- a/test/addons-napi/test_buffer/test_buffer.c +++ b/test/addons-napi/test_buffer/test_buffer.c @@ -55,7 +55,7 @@ napi_value newExternalBuffer(napi_env env, napi_callback_info info) { napi_value getDeleterCallCount(napi_env env, napi_callback_info info) { napi_value callCount; - NAPI_CALL(env, napi_create_number(env, deleterCallCount, &callCount)); + NAPI_CALL(env, napi_create_int32(env, deleterCallCount, &callCount)); return callCount; } diff --git a/test/addons-napi/test_constructor/test_constructor.c b/test/addons-napi/test_constructor/test_constructor.c index 220d564753ca10..a991dab8533bf1 100644 --- a/test/addons-napi/test_constructor/test_constructor.c +++ b/test/addons-napi/test_constructor/test_constructor.c @@ -12,7 +12,7 @@ napi_value GetValue(napi_env env, napi_callback_info info) { NAPI_ASSERT(env, argc == 0, "Wrong number of arguments"); napi_value number; - NAPI_CALL(env, napi_create_number(env, value_, &number)); + NAPI_CALL(env, napi_create_double(env, value_, &number)); return number; } @@ -53,7 +53,7 @@ napi_value GetStaticValue(napi_env env, napi_callback_info info) { NAPI_ASSERT(env, argc == 0, "Wrong number of arguments"); napi_value number; - NAPI_CALL(env, napi_create_number(env, static_value_, &number)); + NAPI_CALL(env, napi_create_double(env, static_value_, &number)); return number; } @@ -61,7 +61,7 @@ napi_value GetStaticValue(napi_env env, napi_callback_info info) { void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_value number; - NAPI_CALL_RETURN_VOID(env, napi_create_number(env, value_, &number)); + NAPI_CALL_RETURN_VOID(env, napi_create_double(env, value_, &number)); napi_property_descriptor properties[] = { { "echo", 0, Echo, 0, 0, 0, napi_enumerable, 0 }, diff --git a/test/addons-napi/test_conversions/test_conversions.c b/test/addons-napi/test_conversions/test_conversions.c index 637cff43b82c4d..a8d526c763c9a2 100644 --- a/test/addons-napi/test_conversions/test_conversions.c +++ b/test/addons-napi/test_conversions/test_conversions.c @@ -24,7 +24,7 @@ napi_value AsInt32(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_int32(env, args[0], &value)); napi_value output; - NAPI_CALL(env, napi_create_number(env, value, &output)); + NAPI_CALL(env, napi_create_int32(env, value, &output)); return output; } @@ -38,7 +38,7 @@ napi_value AsUInt32(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_uint32(env, args[0], &value)); napi_value output; - NAPI_CALL(env, napi_create_number(env, value, &output)); + NAPI_CALL(env, napi_create_uint32(env, value, &output)); return output; } @@ -52,7 +52,7 @@ napi_value AsInt64(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_int64(env, args[0], &value)); napi_value output; - NAPI_CALL(env, napi_create_number(env, (double)value, &output)); + NAPI_CALL(env, napi_create_int64(env, (double)value, &output)); return output; } @@ -66,7 +66,7 @@ napi_value AsDouble(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_double(env, args[0], &value)); napi_value output; - NAPI_CALL(env, napi_create_number(env, value, &output)); + NAPI_CALL(env, napi_create_double(env, value, &output)); return output; } diff --git a/test/addons-napi/test_general/test_general.c b/test/addons-napi/test_general/test_general.c index 2d2d13a3624e6e..858611939474b2 100644 --- a/test/addons-napi/test_general/test_general.c +++ b/test/addons-napi/test_general/test_general.c @@ -29,7 +29,7 @@ napi_value testGetVersion(napi_env env, napi_callback_info info) { uint32_t version; napi_value result; NAPI_CALL(env, napi_get_version(env, &version)); - NAPI_CALL(env, napi_create_number(env, version, &result)); + NAPI_CALL(env, napi_create_uint32(env, version, &result)); return result; } diff --git a/test/addons-napi/test_number/test_number.c b/test/addons-napi/test_number/test_number.c index 1054741d2de369..6b28afe18ff9e8 100644 --- a/test/addons-napi/test_number/test_number.c +++ b/test/addons-napi/test_number/test_number.c @@ -18,7 +18,7 @@ napi_value Test(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_double(env, args[0], &input)); napi_value output; - NAPI_CALL(env, napi_create_number(env, input, &output)); + NAPI_CALL(env, napi_create_double(env, input, &output)); return output; } @@ -40,7 +40,7 @@ napi_value TestInt32Truncation(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_int32(env, args[0], &input)); napi_value output; - NAPI_CALL(env, napi_create_number(env, input, &output)); + NAPI_CALL(env, napi_create_int32(env, input, &output)); return output; } diff --git a/test/addons-napi/test_object/test_object.c b/test/addons-napi/test_object/test_object.c index 663e561a35b197..88ac79c170f5ea 100644 --- a/test/addons-napi/test_object/test_object.c +++ b/test/addons-napi/test_object/test_object.c @@ -144,7 +144,7 @@ napi_value New(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_create_object(env, &ret)); napi_value num; - NAPI_CALL(env, napi_create_number(env, 987654321, &num)); + NAPI_CALL(env, napi_create_int32(env, 987654321, &num)); NAPI_CALL(env, napi_set_named_property(env, ret, "test_number", num)); @@ -187,7 +187,7 @@ napi_value Inflate(napi_env env, napi_callback_info info) { double double_val; NAPI_CALL(env, napi_get_value_double(env, value, &double_val)); - NAPI_CALL(env, napi_create_number(env, double_val + 1, &value)); + NAPI_CALL(env, napi_create_double(env, double_val + 1, &value)); NAPI_CALL(env, napi_set_property(env, obj, property_str, value)); } diff --git a/test/addons-napi/test_properties/test_properties.c b/test/addons-napi/test_properties/test_properties.c index 3053fda4864e8f..3f4f0a6bcbba96 100644 --- a/test/addons-napi/test_properties/test_properties.c +++ b/test/addons-napi/test_properties/test_properties.c @@ -10,7 +10,7 @@ napi_value GetValue(napi_env env, napi_callback_info info) { NAPI_ASSERT(env, argc == 0, "Wrong number of arguments"); napi_value number; - NAPI_CALL(env, napi_create_number(env, value_, &number)); + NAPI_CALL(env, napi_create_double(env, value_, &number)); return number; } @@ -61,7 +61,7 @@ napi_value HasNamedProperty(napi_env env, napi_callback_info info) { void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_value number; - NAPI_CALL_RETURN_VOID(env, napi_create_number(env, value_, &number)); + NAPI_CALL_RETURN_VOID(env, napi_create_double(env, value_, &number)); napi_value name_value; NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, diff --git a/test/addons-napi/test_reference/test_reference.c b/test/addons-napi/test_reference/test_reference.c index 0d3925ee5d04bc..b16e10d70361b5 100644 --- a/test/addons-napi/test_reference/test_reference.c +++ b/test/addons-napi/test_reference/test_reference.c @@ -8,7 +8,7 @@ static napi_ref test_reference = NULL; napi_value GetFinalizeCount(napi_env env, napi_callback_info info) { napi_value result; - NAPI_CALL(env, napi_create_number(env, finalize_count, &result)); + NAPI_CALL(env, napi_create_int32(env, finalize_count, &result)); return result; } @@ -107,7 +107,7 @@ napi_value IncrementRefcount(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_reference_ref(env, test_reference, &refcount)); napi_value result; - NAPI_CALL(env, napi_create_number(env, refcount, &result)); + NAPI_CALL(env, napi_create_uint32(env, refcount, &result)); return result; } @@ -119,7 +119,7 @@ napi_value DecrementRefcount(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_reference_unref(env, test_reference, &refcount)); napi_value result; - NAPI_CALL(env, napi_create_number(env, refcount, &result)); + NAPI_CALL(env, napi_create_uint32(env, refcount, &result)); return result; } diff --git a/test/addons-napi/test_string/test_string.c b/test/addons-napi/test_string/test_string.c index 5cd6d413a679fb..ec80e2c7b57fa8 100644 --- a/test/addons-napi/test_string/test_string.c +++ b/test/addons-napi/test_string/test_string.c @@ -174,7 +174,7 @@ napi_value Utf16Length(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_string_utf16(env, args[0], NULL, 0, &length)); napi_value output; - NAPI_CALL(env, napi_create_number(env, (double)length, &output)); + NAPI_CALL(env, napi_create_uint32(env, (uint32_t)length, &output)); return output; } @@ -196,7 +196,7 @@ napi_value Utf8Length(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_string_utf8(env, args[0], NULL, 0, &length)); napi_value output; - NAPI_CALL(env, napi_create_number(env, (double)length, &output)); + NAPI_CALL(env, napi_create_uint32(env, (uint32_t)length, &output)); return output; } From 11427caf9fdbd9573cd461724372c5250eaa8b2b Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 8 Aug 2017 19:09:28 +0200 Subject: [PATCH 091/183] n-api: add napi_get_node_version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add `napi_get_node_version`, to help with feature-detecting Node.js as an environment. PR-URL: https://github.com/nodejs/node/pull/14696 Reviewed-By: Kyle Farnung Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Tobias Nießen --- doc/api/n-api.md | 31 ++++++++++++++++++++ src/node.cc | 3 +- src/node_api.cc | 14 +++++++++ src/node_api.h | 4 +++ src/node_api_types.h | 7 +++++ src/node_version.h | 4 +++ test/addons-napi/test_general/test.js | 5 ++++ test/addons-napi/test_general/test_general.c | 20 +++++++++++++ 8 files changed, 87 insertions(+), 1 deletion(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 5b7c97d15af6a5..0009a279309daf 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -3285,6 +3285,35 @@ callback invocation, even if it has been successfully cancelled. ## Version Management +### napi_get_node_version + + +```C +typedef struct { + uint32_t major; + uint32_t minor; + uint32_t patch; + const char* release; +} napi_node_version; + +NAPI_EXTERN +napi_status napi_get_node_version(napi_env env, + const napi_node_version** version); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[out] version`: A pointer to version information for Node itself. + +Returns `napi_ok` if the API succeeded. + +This function fills the `version` struct with the major, minor and patch version +of Node that is currently running, and the `release` field with the +value of [`process.release.name`][`process.release`]. + +The returned buffer is statically allocated and does not need to be freed. + ### napi_get_version + [Asynchronous Operations]: #n_api_asynchronous_operations [Basic N-API Data Types]: #n_api_basic_n_api_data_types [ECMAScript Language Specification]: https://tc39.github.io/ecma262/ From b0abcbf37487573b604460574281efd0f863d1bf Mon Sep 17 00:00:00 2001 From: Kyle Farnung Date: Mon, 7 Aug 2017 14:07:19 -0700 Subject: [PATCH 094/183] doc: added napi_get_value_string_latin1 * Reordered string functions alphabetically * Fixed a typo in napi_get_value_string_utf8 PR-URL: https://github.com/nodejs/node/pull/14678 Fixes: https://github.com/nodejs/node/issues/14397 Refs: https://github.com/nodejs/node/issues/14256 Reviewed-By: Refael Ackermann Reviewed-By: Colin Ihrig Reviewed-By: Anna Henningsen Reviewed-By: Michael Dawson Reviewed-By: James M Snell Reviewed-By: Timothy Gu --- doc/api/n-api.md | 73 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 43b5bddb5f634b..c26a1222e19be2 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -1462,51 +1462,51 @@ The JavaScript Number type is described in [Section 6.1.6](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type) of the ECMAScript Language Specification. -#### *napi_create_string_utf16* +#### *napi_create_string_latin1* ```C -napi_status napi_create_string_utf16(napi_env env, - const char16_t* str, - size_t length, - napi_value* result) +NAPI_EXTERN napi_status napi_create_string_latin1(napi_env env, + const char* str, + size_t length, + napi_value* result); ``` - `[in] env`: The environment that the API is invoked under. -- `[in] str`: Character buffer representing a UTF16-LE-encoded string. -- `[in] length`: The length of the string in two-byte code units, or -1 if -it is null-terminated. +- `[in] str`: Character buffer representing a ISO-8859-1-encoded string. +- `[in] length`: The length of the string in bytes, or -1 if it is +null-terminated. - `[out] result`: A `napi_value` representing a JavaScript String. Returns `napi_ok` if the API succeeded. -This API creates a JavaScript String object from a UTF16-LE-encoded C string +This API creates a JavaScript String object from a ISO-8859-1-encoded C string. The JavaScript String type is described in [Section 6.1.4](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type) of the ECMAScript Language Specification. -#### *napi_create_string_latin1* +#### *napi_create_string_utf16* ```C -NAPI_EXTERN napi_status napi_create_string_latin1(napi_env env, - const char* str, - size_t length, - napi_value* result); +napi_status napi_create_string_utf16(napi_env env, + const char16_t* str, + size_t length, + napi_value* result) ``` - `[in] env`: The environment that the API is invoked under. -- `[in] str`: Character buffer representing a latin1-encoded string. -- `[in] length`: The length of the string in bytes, or -1 if it is -null-terminated. +- `[in] str`: Character buffer representing a UTF16-LE-encoded string. +- `[in] length`: The length of the string in two-byte code units, or -1 if +it is null-terminated. - `[out] result`: A `napi_value` representing a JavaScript String. Returns `napi_ok` if the API succeeded. -This API creates a JavaScript String object from a latin1-encoded C string. +This API creates a JavaScript String object from a UTF16-LE-encoded C string The JavaScript String type is described in [Section 6.1.4](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type) @@ -1795,6 +1795,33 @@ is passed in it returns `napi_number_expected`. This API returns the C int64 primitive equivalent of the given JavaScript Number +#### *napi_get_value_string_latin1* + +```C +NAPI_EXTERN napi_status napi_get_value_string_latin1(napi_env env, + napi_value value, + char* buf, + size_t bufsize, + size_t* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: `napi_value` representing JavaScript string. +- `[in] buf`: Buffer to write the ISO-8859-1-encoded string into. If NULL is +passed in, the length of the string (in bytes) is returned. +- `[in] bufsize`: Size of the destination buffer. +- `[out] result`: Number of bytes copied into the buffer including the null +terminator. If the buffer size is insufficient, the string will be truncated +including a null terminator. + +Returns `napi_ok` if the API succeeded. If a non-String `napi_value` +is passed in it returns `napi_string_expected`. + +This API returns the ISO-8859-1-encoded string corresponding the value passed +in. + #### *napi_get_value_string_utf8* +```C +napi_status napi_remove_wrap(napi_env env, + napi_value js_object, + void** result); +``` + + - `[in] env`: The environment that the API is invoked under. + - `[in] js_object`: The object associated with the native instance. + - `[out] result`: Pointer to the wrapped native instance. + +Returns `napi_ok` if the API succeeded. + +Retrieves a native instance that was previously wrapped in the JavaScript +object `js_object` using `napi_wrap()` and removes the wrapping, thereby +restoring the JavaScript object's prototype chain. If a finalize callback was +associated with the wrapping, it will no longer be called when the JavaScript +object becomes garbage-collected. + ## Asynchronous Operations Addon modules often need to leverage async helpers from libuv as part of their diff --git a/src/node_api.cc b/src/node_api.cc index 2caa85aee5fc2c..db66ea0bab4550 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -679,6 +679,8 @@ v8::Local CreateAccessorCallbackData(napi_env env, return cbdata; } +int kWrapperFields = 3; + // Pointer used to identify items wrapped by N-API. Used by FindWrapper and // napi_wrap(). const char napi_wrap_name[] = "N-API Wrapper"; @@ -687,7 +689,8 @@ const char napi_wrap_name[] = "N-API Wrapper"; // wrapper would be the first in the chain, but it is OK for other objects to // be inserted in the prototype chain. bool FindWrapper(v8::Local obj, - v8::Local* result = nullptr) { + v8::Local* result = nullptr, + v8::Local* parent = nullptr) { v8::Local wrapper = obj; do { @@ -695,8 +698,11 @@ bool FindWrapper(v8::Local obj, if (proto.IsEmpty() || !proto->IsObject()) { return false; } + if (parent != nullptr) { + *parent = wrapper; + } wrapper = proto.As(); - if (wrapper->InternalFieldCount() == 2) { + if (wrapper->InternalFieldCount() == kWrapperFields) { v8::Local external = wrapper->GetInternalField(1); if (external->IsExternal() && external.As()->Value() == v8impl::napi_wrap_name) { @@ -750,6 +756,29 @@ napi_env GetEnv(v8::Local context) { return result; } +napi_status Unwrap(napi_env env, + napi_value js_object, + void** result, + v8::Local* wrapper, + v8::Local* parent = nullptr) { + CHECK_ARG(env, js_object); + CHECK_ARG(env, result); + + v8::Local value = v8impl::V8LocalValueFromJsValue(js_object); + RETURN_STATUS_IF_FALSE(env, value->IsObject(), napi_invalid_arg); + v8::Local obj = value.As(); + + RETURN_STATUS_IF_FALSE( + env, v8impl::FindWrapper(obj, wrapper, parent), napi_invalid_arg); + + v8::Local unwrappedValue = (*wrapper)->GetInternalField(0); + RETURN_STATUS_IF_FALSE(env, unwrappedValue->IsExternal(), napi_invalid_arg); + + *result = unwrappedValue.As()->Value(); + + return napi_ok; +} + } // end of namespace v8impl // Intercepts the Node-V8 module registration callback. Converts parameters @@ -2269,62 +2298,78 @@ napi_status napi_wrap(napi_env env, // Create a wrapper object with an internal field to hold the wrapped pointer // and a second internal field to identify the owner as N-API. v8::Local wrapper_template; - ENV_OBJECT_TEMPLATE(env, wrap, wrapper_template, 2); + ENV_OBJECT_TEMPLATE(env, wrap, wrapper_template, v8impl::kWrapperFields); auto maybe_object = wrapper_template->NewInstance(context); CHECK_MAYBE_EMPTY(env, maybe_object, napi_generic_failure); - v8::Local wrapper = maybe_object.ToLocalChecked(); - wrapper->SetInternalField(1, v8::External::New(isolate, - reinterpret_cast(const_cast(v8impl::napi_wrap_name)))); // Store the pointer as an external in the wrapper. wrapper->SetInternalField(0, v8::External::New(isolate, native_object)); + wrapper->SetInternalField(1, v8::External::New(isolate, + reinterpret_cast(const_cast(v8impl::napi_wrap_name)))); // Insert the wrapper into the object's prototype chain. v8::Local proto = obj->GetPrototype(); CHECK(wrapper->SetPrototype(context, proto).FromJust()); CHECK(obj->SetPrototype(context, wrapper).FromJust()); + v8impl::Reference* reference = nullptr; if (result != nullptr) { // The returned reference should be deleted via napi_delete_reference() // ONLY in response to the finalize callback invocation. (If it is deleted // before then, then the finalize callback will never be invoked.) // Therefore a finalize callback is required when returning a reference. CHECK_ARG(env, finalize_cb); - v8impl::Reference* reference = v8impl::Reference::New( + reference = v8impl::Reference::New( env, obj, 0, false, finalize_cb, native_object, finalize_hint); *result = reinterpret_cast(reference); } else if (finalize_cb != nullptr) { // Create a self-deleting reference just for the finalize callback. - v8impl::Reference::New( + reference = v8impl::Reference::New( env, obj, 0, true, finalize_cb, native_object, finalize_hint); } + if (reference != nullptr) { + wrapper->SetInternalField(2, v8::External::New(isolate, reference)); + } + return GET_RETURN_STATUS(env); } -napi_status napi_unwrap(napi_env env, napi_value js_object, void** result) { +napi_status napi_unwrap(napi_env env, napi_value obj, void** result) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. CHECK_ENV(env); - CHECK_ARG(env, js_object); - CHECK_ARG(env, result); - - v8::Local value = v8impl::V8LocalValueFromJsValue(js_object); - RETURN_STATUS_IF_FALSE(env, value->IsObject(), napi_invalid_arg); - v8::Local obj = value.As(); + v8::Local wrapper; + return napi_set_last_error(env, v8impl::Unwrap(env, obj, result, &wrapper)); +} +napi_status napi_remove_wrap(napi_env env, napi_value obj, void** result) { + NAPI_PREAMBLE(env); v8::Local wrapper; - RETURN_STATUS_IF_FALSE( - env, v8impl::FindWrapper(obj, &wrapper), napi_invalid_arg); + v8::Local parent; + napi_status status = v8impl::Unwrap(env, obj, result, &wrapper, &parent); + if (status != napi_ok) { + return napi_set_last_error(env, status); + } - v8::Local unwrappedValue = wrapper->GetInternalField(0); - RETURN_STATUS_IF_FALSE(env, unwrappedValue->IsExternal(), napi_invalid_arg); + v8::Local external = wrapper->GetInternalField(2); + if (external->IsExternal()) { + v8impl::Reference::Delete( + static_cast(external.As()->Value())); + } - *result = unwrappedValue.As()->Value(); + if (!parent.IsEmpty()) { + v8::Maybe maybe = parent->SetPrototype( + env->isolate->GetCurrentContext(), wrapper->GetPrototype()); + CHECK_MAYBE_NOTHING(env, maybe, napi_generic_failure); + if (!maybe.FromMaybe(false)) { + return napi_set_last_error(env, napi_generic_failure); + } + } - return napi_clear_last_error(env); + return GET_RETURN_STATUS(env); } napi_status napi_create_external(napi_env env, diff --git a/src/node_api.h b/src/node_api.h index 0cf0ba046997d3..e52e2016d733b9 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -362,6 +362,9 @@ NAPI_EXTERN napi_status napi_wrap(napi_env env, NAPI_EXTERN napi_status napi_unwrap(napi_env env, napi_value js_object, void** result); +NAPI_EXTERN napi_status napi_remove_wrap(napi_env env, + napi_value js_object, + void** result); NAPI_EXTERN napi_status napi_create_external(napi_env env, void* data, napi_finalize finalize_cb, diff --git a/test/addons-napi/test_general/test.js b/test/addons-napi/test_general/test.js index 5dfdf6e8539c5a..10cf8b6e40132b 100644 --- a/test/addons-napi/test_general/test.js +++ b/test/addons-napi/test_general/test.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --expose-gc const common = require('../../common'); const test_general = require(`./build/${common.buildType}/test_general`); @@ -56,10 +57,37 @@ assert.strictEqual(release, process.release.name); // for null assert.strictEqual(test_general.testNapiTypeof(null), 'null'); -const x = {}; +// Ensure that garbage collecting an object with a wrapped native item results +// in the finalize callback being called. +let w = {}; +test_general.wrap(w, []); +w = null; +global.gc(); +assert.strictEqual(test_general.derefItemWasCalled(), true, + 'deref_item() was called upon garbage collecting a ' + + 'wrapped object'); // Assert that wrapping twice fails. +const x = {}; test_general.wrap(x, 25); assert.throws(function() { test_general.wrap(x, 'Blah'); }, Error); + +// Ensure that wrapping, removing the wrap, and then wrapping again works. +const y = {}; +test_general.wrap(y, -12); +test_general.removeWrap(y); +assert.doesNotThrow(function() { + test_general.wrap(y, 're-wrap!'); +}, Error, 'Wrapping twice succeeds if a remove_wrap() separates the instances'); + +// Ensure that removing a wrap and garbage collecting does not fire the +// finalize callback. +let z = {}; +test_general.testFinalizeWrap(z); +test_general.removeWrap(z); +z = null; +global.gc(); +assert.strictEqual(test_general.finalizeWasCalled(), false, + 'finalize callback was not called upon garbage collection'); diff --git a/test/addons-napi/test_general/test_general.c b/test/addons-napi/test_general/test_general.c index da347bd68bcc9a..ecec3e014ba0b1 100644 --- a/test/addons-napi/test_general/test_general.c +++ b/test/addons-napi/test_general/test_general.c @@ -138,17 +138,29 @@ napi_value testNapiTypeof(napi_env env, napi_callback_info info) { return result; } +static bool deref_item_called = false; static void deref_item(napi_env env, void* data, void* hint) { (void) hint; + deref_item_called = true; NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, (napi_ref)data)); } +napi_value deref_item_was_called(napi_env env, napi_callback_info info) { + napi_value it_was_called; + + NAPI_CALL(env, napi_get_boolean(env, deref_item_called, &it_was_called)); + + return it_was_called; +} + napi_value wrap(napi_env env, napi_callback_info info) { size_t argc = 2; napi_value argv[2]; napi_ref payload; + deref_item_called = false; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); NAPI_CALL(env, napi_create_reference(env, argv[1], 1, &payload)); NAPI_CALL(env, napi_wrap(env, argv[0], payload, deref_item, NULL, NULL)); @@ -156,6 +168,43 @@ napi_value wrap(napi_env env, napi_callback_info info) { return NULL; } +napi_value remove_wrap(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value wrapped; + void* data; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &wrapped, NULL, NULL)); + NAPI_CALL(env, napi_remove_wrap(env, wrapped, &data)); + if (data != NULL) { + NAPI_CALL(env, napi_delete_reference(env, (napi_ref)data)); + } + + return NULL; +} + +static bool finalize_called = false; +static void test_finalize(napi_env env, void* data, void* hint) { + finalize_called = true; +} + +napi_value test_finalize_wrap(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value js_object; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &js_object, NULL, NULL)); + NAPI_CALL(env, napi_wrap(env, js_object, NULL, test_finalize, NULL, NULL)); + + return NULL; +} + +napi_value finalize_was_called(napi_env env, napi_callback_info info) { + napi_value it_was_called; + + NAPI_CALL(env, napi_get_boolean(env, finalize_called, &it_was_called)); + + return it_was_called; +} + void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("testStrictEquals", testStrictEquals), @@ -169,6 +218,10 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) { DECLARE_NAPI_PROPERTY("testNapiErrorCleanup", testNapiErrorCleanup), DECLARE_NAPI_PROPERTY("testNapiTypeof", testNapiTypeof), DECLARE_NAPI_PROPERTY("wrap", wrap), + DECLARE_NAPI_PROPERTY("removeWrap", remove_wrap), + DECLARE_NAPI_PROPERTY("testFinalizeWrap", test_finalize_wrap), + DECLARE_NAPI_PROPERTY("finalizeWasCalled", finalize_was_called), + DECLARE_NAPI_PROPERTY("derefItemWasCalled", deref_item_was_called), }; NAPI_CALL_RETURN_VOID(env, napi_define_properties( From 41b7f4c6194fd624c352e16b21cd5290d7c89141 Mon Sep 17 00:00:00 2001 From: Daniel Taveras Date: Thu, 27 Jul 2017 21:05:01 -0400 Subject: [PATCH 096/183] doc: fix doc for napi_get_value_string_utf8 The API for napi_get_value_string_utf8() appears to have been previously changed. This improves the doc reflect the current design. PR-URL: https://github.com/nodejs/node/pull/14529 Fixes: https://github.com/nodejs/node/issues/14398 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Michael Dawson --- doc/api/n-api.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index be06a5ab3bed7a..89aa08a15198ce 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -1811,10 +1811,10 @@ NAPI_EXTERN napi_status napi_get_value_string_latin1(napi_env env, - `[in] value`: `napi_value` representing JavaScript string. - `[in] buf`: Buffer to write the ISO-8859-1-encoded string into. If NULL is passed in, the length of the string (in bytes) is returned. -- `[in] bufsize`: Size of the destination buffer. -- `[out] result`: Number of bytes copied into the buffer including the null -terminator. If the buffer size is insufficient, the string will be truncated -including a null terminator. +- `[in] bufsize`: Size of the destination buffer. When this value is +insufficient, the returned string will be truncated. +- `[out] result`: Number of bytes copied into the buffer, excluding the null +terminator. Returns `napi_ok` if the API succeeded. If a non-String `napi_value` is passed in it returns `napi_string_expected`. @@ -1837,11 +1837,11 @@ napi_status napi_get_value_string_utf8(napi_env env, - `[in] env`: The environment that the API is invoked under. - `[in] value`: `napi_value` representing JavaScript string. - `[in] buf`: Buffer to write the UTF8-encoded string into. If NULL is passed -in, the length of the string (in bytes) is returned. -- `[in] bufsize`: Size of the destination buffer. -- `[out] result`: Number of bytes copied into the buffer including the null -terminator. If the buffer size is insufficient, the string will be truncated -including a null terminator. + in, the length of the string (in bytes) is returned. +- `[in] bufsize`: Size of the destination buffer. When this value is +insufficient, the returned string will be truncated. +- `[out] result`: Number of bytes copied into the buffer, excluding the null +terminator. Returns `napi_ok` if the API succeeded. If a non-String `napi_value` is passed in it returns `napi_string_expected`. @@ -1864,10 +1864,10 @@ napi_status napi_get_value_string_utf16(napi_env env, - `[in] value`: `napi_value` representing JavaScript string. - `[in] buf`: Buffer to write the UTF16-LE-encoded string into. If NULL is passed in, the length of the string (in 2-byte code units) is returned. -- `[in] bufsize`: Size of the destination buffer. -- `[out] result`: Number of 2-byte code units copied into the buffer including -the null terminator. If the buffer size is insufficient, the string will be -truncated including a null terminator. +- `[in] bufsize`: Size of the destination buffer. When this value is +insufficient, the returned string will be truncated. +- `[out] result`: Number of 2-byte code units copied into the buffer, excluding the null +terminator. Returns `napi_ok` if the API succeeded. If a non-String `napi_value` is passed in it returns `napi_string_expected`. From 6ec485ad0ce86c0a37b5af471e7f02e0e8f92370 Mon Sep 17 00:00:00 2001 From: Daniil Shakir Date: Tue, 22 Aug 2017 00:36:32 +0300 Subject: [PATCH 097/183] test: remove unused parameters PR-URL: https://github.com/nodejs/node/pull/14968 Reviewed-By: Rich Trott Reviewed-By: Kunal Pathak Reviewed-By: Colin Ihrig Reviewed-By: David Cai Reviewed-By: Yuta Hiroto Reviewed-By: Luigi Pinca Reviewed-By: Daniel Bevenius Reviewed-By: James M Snell --- test/addons-napi/test_async/test.js | 2 +- test/addons-napi/test_typedarray/test.js | 2 +- test/addons/repl-domain-abort/test.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/addons-napi/test_async/test.js b/test/addons-napi/test_async/test.js index eda6963743ff6f..cfd39a5b11ae2d 100644 --- a/test/addons-napi/test_async/test.js +++ b/test/addons-napi/test_async/test.js @@ -9,7 +9,7 @@ const testException = 'test_async_cb_exception'; // Exception thrown from async completion callback. // (Tested in a spawned process because the exception is fatal.) if (process.argv[2] === 'child') { - test_async.Test(1, common.mustCall(function(err, val) { + test_async.Test(1, common.mustCall(function() { throw new Error(testException); })); return; diff --git a/test/addons-napi/test_typedarray/test.js b/test/addons-napi/test_typedarray/test.js index dcb7d76442f608..b81295902069b7 100644 --- a/test/addons-napi/test_typedarray/test.js +++ b/test/addons-napi/test_typedarray/test.js @@ -44,7 +44,7 @@ const arrayTypes = [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array ]; -arrayTypes.forEach((currentType, key) => { +arrayTypes.forEach((currentType) => { const template = Reflect.construct(currentType, buffer); const theArray = test_typedarray.CreateTypedArray(template, buffer); diff --git a/test/addons/repl-domain-abort/test.js b/test/addons/repl-domain-abort/test.js index 8e5d8785164fb9..9b9a63abfb685c 100644 --- a/test/addons/repl-domain-abort/test.js +++ b/test/addons/repl-domain-abort/test.js @@ -25,7 +25,7 @@ const lines = [ const dInput = new stream.Readable(); const dOutput = new stream.Writable(); -dInput._read = function _read(size) { +dInput._read = function _read() { while (lines.length > 0 && this.push(lines.shift())); if (lines.length === 0) this.push(null); From 2bab64e3de306619ad3dd3111149e6b5abf2a8bc Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Thu, 24 Aug 2017 13:33:26 +0300 Subject: [PATCH 098/183] n-api: implement promise Promise is implemented as a pair of objects. `napi_create_promise()` returns both a JavaScript promise and a newly allocated "deferred" in its out-params. The deferred is linked to the promise such that the deferred can be passed to `napi_resolve_deferred()` or `napi_reject_deferred()` to reject/resolve the promise. `napi_is_promise()` can be used to check if a `napi_value` is a native promise - that is, a promise created by the underlying engine, rather than a pure JS implementation of a promise. PR-URL: https://github.com/nodejs/node/pull/14365 Fixes: https://github.com/nodejs/abi-stable-node/issues/242 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Benjamin Gruenbaum Reviewed-By: Timothy Gu --- doc/api/n-api.md | 138 +++++++++++++++++++ src/node_api.cc | 78 +++++++++++ src/node_api.h | 14 ++ src/node_api_types.h | 1 + test/addons-napi/test_promise/binding.gyp | 8 ++ test/addons-napi/test_promise/test.js | 60 ++++++++ test/addons-napi/test_promise/test_promise.c | 60 ++++++++ 7 files changed, 359 insertions(+) create mode 100644 test/addons-napi/test_promise/binding.gyp create mode 100644 test/addons-napi/test_promise/test.js create mode 100644 test/addons-napi/test_promise/test_promise.c diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 89aa08a15198ce..12aad7583f6d93 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -42,6 +42,7 @@ The documentation for N-API is structured as follows: * [Working with JavaScript Functions][] * [Object Wrap][] * [Asynchronous Operations][] +* [Promises][] The N-API is a C API that ensures ABI stability across Node.js versions and different compiler levels. However, we also understand that a C++ @@ -3395,6 +3396,142 @@ support it: +## Promises + +N-API provides facilities for creating `Promise` objects as described in +[Section 25.4][] of the ECMA specification. It implements promises as a pair of +objects. When a promise is created by `napi_create_promise()`, a "deferred" +object is created and returned alongside the `Promise`. The deferred object is +bound to the created `Promise` and is the only means to resolve or reject the +`Promise` using `napi_resolve_deferred()` or `napi_reject_deferred()`. The +deferred object that is created by `napi_create_promise()` is freed by +`napi_resolve_deferred()` or `napi_reject_deferred()`. The `Promise` object may +be returned to JavaScript where it can be used in the usual fashion. + +For example, to create a promise and pass it to an asynchronous worker: +```c +napi_deferred deferred; +napi_value promise; +napi_status status; + +// Create the promise. +status = napi_create_promise(env, &deferred, &promise); +if (status != napi_ok) return NULL; + +// Pass the deferred to a function that performs an asynchronous action. +do_something_asynchronous(deferred); + +// Return the promise to JS +return promise; +``` + +The above function `do_something_asynchronous()` would perform its asynchronous +action and then it would resolve or reject the deferred, thereby concluding the +promise and freeing the deferred: +```c +napi_deferred deferred; +napi_value undefined; +napi_status status; + +// Create a value with which to conclude the deferred. +status = napi_get_undefined(env, &undefined); +if (status != napi_ok) return NULL; + +// Resolve or reject the promise associated with the deferred depending on +// whether the asynchronous action succeeded. +if (asynchronous_action_succeeded) { + status = napi_resolve_deferred(env, deferred, undefined); +} else { + status = napi_reject_deferred(env, deferred, undefined); +} +if (status != napi_ok) return NULL; + +// At this point the deferred has been freed, so we should assign NULL to it. +deferred = NULL; +``` + +### napi_create_promise + +```C +NAPI_EXTERN napi_status napi_create_promise(napi_env env, + napi_deferred* deferred, + napi_value* promise); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[out] deferred`: A newly created deferred object which can later be passed to +`napi_resolve_deferred()` or `napi_reject_deferred()` to resolve resp. reject +the associated promise. +- `[out] promise`: The JavaScript promise associated with the deferred object. + +Returns `napi_ok` if the API succeeded. + +This API creates a deferred object and a JavaScript promise. + +### napi_resolve_deferred + +```C +NAPI_EXTERN napi_status napi_resolve_deferred(napi_env env, + napi_deferred deferred, + napi_value resolution); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] deferred`: The deferred object whose associated promise to resolve. +- `[in] resolution`: The value with which to resolve the promise. + +This API resolves a JavaScript promise by way of the deferred object +with which it is associated. Thus, it can only be used to resolve JavaScript +promises for which the corresponding deferred object is available. This +effectively means that the promise must have been created using +`napi_create_promise()` and the deferred object returned from that call must +have been retained in order to be passed to this API. + +The deferred object is freed upon successful completion. + +### napi_reject_deferred + +```C +NAPI_EXTERN napi_status napi_reject_deferred(napi_env env, + napi_deferred deferred, + napi_value rejection); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] deferred`: The deferred object whose associated promise to resolve. +- `[in] rejection`: The value with which to reject the promise. + +This API rejects a JavaScript promise by way of the deferred object +with which it is associated. Thus, it can only be used to reject JavaScript +promises for which the corresponding deferred object is available. This +effectively means that the promise must have been created using +`napi_create_promise()` and the deferred object returned from that call must +have been retained in order to be passed to this API. + +The deferred object is freed upon successful completion. + +### napi_is_promise + +```C +NAPI_EXTERN napi_status napi_is_promise(napi_env env, + napi_value promise, + bool* is_promise); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] promise`: The promise to examine +- `[out] is_promise`: Flag indicating whether `promise` is a native promise +object - that is, a promise object created by the underlying engine. + +[Promises]: #n_api_promises [Asynchronous Operations]: #n_api_asynchronous_operations [Basic N-API Data Types]: #n_api_basic_n_api_data_types [ECMAScript Language Specification]: https://tc39.github.io/ecma262/ @@ -3406,6 +3543,7 @@ support it: [Section 9.1.6]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc [Section 12.5.5]: https://tc39.github.io/ecma262/#sec-typeof-operator [Section 24.3]: https://tc39.github.io/ecma262/#sec-dataview-objects +[Section 25.4]: https://tc39.github.io/ecma262/#sec-promise-objects [Working with JavaScript Functions]: #n_api_working_with_javascript_functions [Working with JavaScript Properties]: #n_api_working_with_javascript_properties [Working with JavaScript Values]: #n_api_working_with_javascript_values diff --git a/src/node_api.cc b/src/node_api.cc index db66ea0bab4550..fd31817e4a8f71 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -223,6 +223,14 @@ V8EscapableHandleScopeFromJsEscapableHandleScope( static_assert(sizeof(v8::Local) == sizeof(napi_value), "Cannot convert between v8::Local and napi_value"); +napi_deferred JsDeferredFromV8Persistent(v8::Persistent* local) { + return reinterpret_cast(local); +} + +v8::Persistent* V8PersistentFromJsDeferred(napi_deferred local) { + return reinterpret_cast*>(local); +} + napi_value JsValueFromV8LocalValue(v8::Local local) { return reinterpret_cast(*local); } @@ -779,6 +787,33 @@ napi_status Unwrap(napi_env env, return napi_ok; } +napi_status ConcludeDeferred(napi_env env, + napi_deferred deferred, + napi_value result, + bool is_resolved) { + NAPI_PREAMBLE(env); + CHECK_ARG(env, result); + + v8::Local context = env->isolate->GetCurrentContext(); + v8::Persistent* deferred_ref = + V8PersistentFromJsDeferred(deferred); + v8::Local v8_deferred = + v8::Local::New(env->isolate, *deferred_ref); + + auto v8_resolver = v8::Local::Cast(v8_deferred); + + v8::Maybe success = is_resolved ? + v8_resolver->Resolve(context, v8impl::V8LocalValueFromJsValue(result)) : + v8_resolver->Reject(context, v8impl::V8LocalValueFromJsValue(result)); + + deferred_ref->Reset(); + delete deferred_ref; + + RETURN_STATUS_IF_FALSE(env, success.FromMaybe(false), napi_generic_failure); + + return GET_RETURN_STATUS(env); +} + } // end of namespace v8impl // Intercepts the Node-V8 module registration callback. Converts parameters @@ -3335,3 +3370,46 @@ napi_status napi_cancel_async_work(napi_env env, napi_async_work work) { return napi_clear_last_error(env); } + +NAPI_EXTERN napi_status napi_create_promise(napi_env env, + napi_deferred* deferred, + napi_value* promise) { + NAPI_PREAMBLE(env); + CHECK_ARG(env, deferred); + CHECK_ARG(env, promise); + + auto maybe = v8::Promise::Resolver::New(env->isolate->GetCurrentContext()); + CHECK_MAYBE_EMPTY(env, maybe, napi_generic_failure); + + auto v8_resolver = maybe.ToLocalChecked(); + auto v8_deferred = new v8::Persistent(); + v8_deferred->Reset(env->isolate, v8_resolver); + + *deferred = v8impl::JsDeferredFromV8Persistent(v8_deferred); + *promise = v8impl::JsValueFromV8LocalValue(v8_resolver->GetPromise()); + return GET_RETURN_STATUS(env); +} + +NAPI_EXTERN napi_status napi_resolve_deferred(napi_env env, + napi_deferred deferred, + napi_value resolution) { + return v8impl::ConcludeDeferred(env, deferred, resolution, true); +} + +NAPI_EXTERN napi_status napi_reject_deferred(napi_env env, + napi_deferred deferred, + napi_value resolution) { + return v8impl::ConcludeDeferred(env, deferred, resolution, false); +} + +NAPI_EXTERN napi_status napi_is_promise(napi_env env, + napi_value promise, + bool* is_promise) { + CHECK_ENV(env); + CHECK_ARG(env, promise); + CHECK_ARG(env, is_promise); + + *is_promise = v8impl::V8LocalValueFromJsValue(promise)->IsPromise(); + + return napi_clear_last_error(env); +} diff --git a/src/node_api.h b/src/node_api.h index e52e2016d733b9..6a4b2941879ff0 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -543,6 +543,20 @@ NAPI_EXTERN napi_status napi_get_node_version(napi_env env, const napi_node_version** version); +// Promises +NAPI_EXTERN napi_status napi_create_promise(napi_env env, + napi_deferred* deferred, + napi_value* promise); +NAPI_EXTERN napi_status napi_resolve_deferred(napi_env env, + napi_deferred deferred, + napi_value resolution); +NAPI_EXTERN napi_status napi_reject_deferred(napi_env env, + napi_deferred deferred, + napi_value rejection); +NAPI_EXTERN napi_status napi_is_promise(napi_env env, + napi_value promise, + bool* is_promise); + EXTERN_C_END #endif // SRC_NODE_API_H_ diff --git a/src/node_api_types.h b/src/node_api_types.h index 0bdc377c8fc6a0..ac8482bf9dcf9d 100644 --- a/src/node_api_types.h +++ b/src/node_api_types.h @@ -17,6 +17,7 @@ typedef struct napi_handle_scope__ *napi_handle_scope; typedef struct napi_escapable_handle_scope__ *napi_escapable_handle_scope; typedef struct napi_callback_info__ *napi_callback_info; typedef struct napi_async_work__ *napi_async_work; +typedef struct napi_deferred__ *napi_deferred; typedef enum { napi_default = 0, diff --git a/test/addons-napi/test_promise/binding.gyp b/test/addons-napi/test_promise/binding.gyp new file mode 100644 index 00000000000000..bf266f93db74be --- /dev/null +++ b/test/addons-napi/test_promise/binding.gyp @@ -0,0 +1,8 @@ +{ + "targets": [ + { + "target_name": "test_promise", + "sources": [ "test_promise.c" ] + } + ] +} diff --git a/test/addons-napi/test_promise/test.js b/test/addons-napi/test_promise/test.js new file mode 100644 index 00000000000000..4c2a2e5e76c9fb --- /dev/null +++ b/test/addons-napi/test_promise/test.js @@ -0,0 +1,60 @@ +'use strict'; + +const common = require('../../common'); +const test_promise = require(`./build/${common.buildType}/test_promise`); +const assert = require('assert'); + +let expected_result, promise; + +// A resolution +expected_result = 42; +promise = test_promise.createPromise(); +promise.then( + common.mustCall(function(result) { + assert.strictEqual(result, expected_result, + 'promise resolved as expected'); + }), + common.mustNotCall()); +test_promise.concludeCurrentPromise(expected_result, true); + +// A rejection +expected_result = 'It\'s not you, it\'s me.'; +promise = test_promise.createPromise(); +promise.then( + common.mustNotCall(), + common.mustCall(function(result) { + assert.strictEqual(result, expected_result, + 'promise rejected as expected'); + })); +test_promise.concludeCurrentPromise(expected_result, false); + +// Chaining +promise = test_promise.createPromise(); +promise.then( + common.mustCall(function(result) { + assert.strictEqual(result, 'chained answer', + 'resolving with a promise chains properly'); + }), + common.mustNotCall()); +test_promise.concludeCurrentPromise(Promise.resolve('chained answer'), true); + +assert.strictEqual(test_promise.isPromise(promise), true, + 'natively created promise is recognized as a promise'); + +assert.strictEqual(test_promise.isPromise(Promise.reject(-1)), true, + 'Promise created with JS is recognized as a promise'); + +assert.strictEqual(test_promise.isPromise(2.4), false, + 'Number is recognized as not a promise'); + +assert.strictEqual(test_promise.isPromise('I promise!'), false, + 'String is recognized as not a promise'); + +assert.strictEqual(test_promise.isPromise(undefined), false, + 'undefined is recognized as not a promise'); + +assert.strictEqual(test_promise.isPromise(null), false, + 'null is recognized as not a promise'); + +assert.strictEqual(test_promise.isPromise({}), false, + 'an object is recognized as not a promise'); diff --git a/test/addons-napi/test_promise/test_promise.c b/test/addons-napi/test_promise/test_promise.c new file mode 100644 index 00000000000000..dc617f4592e520 --- /dev/null +++ b/test/addons-napi/test_promise/test_promise.c @@ -0,0 +1,60 @@ +#include +#include "../common.h" + +napi_deferred deferred = NULL; + +napi_value createPromise(napi_env env, napi_callback_info info) { + napi_value promise; + + // We do not overwrite an existing deferred. + if (deferred != NULL) { + return NULL; + } + + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + + return promise; +} + +napi_value concludeCurrentPromise(napi_env env, napi_callback_info info) { + napi_value argv[2]; + size_t argc = 2; + bool resolution; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); + NAPI_CALL(env, napi_get_value_bool(env, argv[1], &resolution)); + if (resolution) { + NAPI_CALL(env, napi_resolve_deferred(env, deferred, argv[0])); + } else { + NAPI_CALL(env, napi_reject_deferred(env, deferred, argv[0])); + } + + deferred = NULL; + + return NULL; +} + +napi_value isPromise(napi_env env, napi_callback_info info) { + napi_value promise, result; + size_t argc = 1; + bool is_promise; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &promise, NULL, NULL)); + NAPI_CALL(env, napi_is_promise(env, promise, &is_promise)); + NAPI_CALL(env, napi_get_boolean(env, is_promise, &result)); + + return result; +} + +void Init(napi_env env, napi_value exports, napi_value module, void* priv) { + napi_property_descriptor descriptors[] = { + DECLARE_NAPI_PROPERTY("createPromise", createPromise), + DECLARE_NAPI_PROPERTY("concludeCurrentPromise", concludeCurrentPromise), + DECLARE_NAPI_PROPERTY("isPromise", isPromise), + }; + + NAPI_CALL_RETURN_VOID(env, napi_define_properties( + env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); +} + +NAPI_MODULE(addon, Init) From 28786e2c6b4d30ffcfe89511d635bad67a6adee5 Mon Sep 17 00:00:00 2001 From: Chris Young Date: Tue, 27 Jun 2017 20:56:15 -0700 Subject: [PATCH 099/183] n-api: adds function to adjust external memory Added a wrapper around v8::Isolate::AdjustAmountOfExternalAllocatedMemory PR-URL: https://github.com/nodejs/node/pull/14310 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Timothy Gu Reviewed-By: Colin Ihrig Reviewed-By: Michael Dawson Reviewed-By: Benjamin Gruenbaum Fixes: https://github.com/nodejs/node/issues/13928 --- doc/api/n-api.md | 25 ++++++++++++++++++++ src/node_api.cc | 13 ++++++++++ src/node_api.h | 5 ++++ test/addons-napi/test_general/test.js | 5 ++++ test/addons-napi/test_general/test_general.c | 11 +++++++++ 5 files changed, 59 insertions(+) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 12aad7583f6d93..9c56e50e83f299 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -3394,6 +3394,31 @@ support it: * If the function is not available, provide an alternate implementation that does not use the function. +## Memory Management + +### napi_adjust_external_memory + +```C +NAPI_EXTERN napi_status napi_adjust_external_memory(napi_env env, + int64_t change_in_bytes, + int64_t* result); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] change_in_bytes`: The change in externally allocated memory that is +kept alive by JavaScript objects. +- `[out] result`: The adjusted value + +Returns `napi_ok` if the API succeeded. + +This function gives V8 an indication of the amount of externally allocated +memory that is kept alive by JavaScript objects (i.e. a JavaScript object +that points to its own memory allocated by a native module). Registering +externally allocated memory will trigger global garbage collections more +often than it would otherwise. + ## Promises diff --git a/src/node_api.cc b/src/node_api.cc index fd31817e4a8f71..a2322295fccbaa 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -3216,6 +3216,19 @@ napi_status napi_get_node_version(napi_env env, return napi_clear_last_error(env); } +napi_status napi_adjust_external_memory(napi_env env, + int64_t change_in_bytes, + int64_t* adjusted_value) { + CHECK_ENV(env); + CHECK_ARG(env, &change_in_bytes); + CHECK_ARG(env, adjusted_value); + + *adjusted_value = env->isolate->AdjustAmountOfExternalAllocatedMemory( + change_in_bytes); + + return napi_clear_last_error(env); +} + namespace uvimpl { static napi_status ConvertUVErrorCode(int code) { diff --git a/src/node_api.h b/src/node_api.h index 6a4b2941879ff0..702ddf2d9e6a0e 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -557,6 +557,11 @@ NAPI_EXTERN napi_status napi_is_promise(napi_env env, napi_value promise, bool* is_promise); +// Memory management +NAPI_EXTERN napi_status napi_adjust_external_memory(napi_env env, + int64_t change_in_bytes, + int64_t* adjusted_value); + EXTERN_C_END #endif // SRC_NODE_API_H_ diff --git a/test/addons-napi/test_general/test.js b/test/addons-napi/test_general/test.js index 10cf8b6e40132b..ec59922639e949 100644 --- a/test/addons-napi/test_general/test.js +++ b/test/addons-napi/test_general/test.js @@ -91,3 +91,8 @@ z = null; global.gc(); assert.strictEqual(test_general.finalizeWasCalled(), false, 'finalize callback was not called upon garbage collection'); + +// test napi_adjust_external_memory +const adjustedValue = test_general.testAdjustExternalMemory(); +assert.strictEqual(typeof adjustedValue, 'number'); +assert(adjustedValue > 0); diff --git a/test/addons-napi/test_general/test_general.c b/test/addons-napi/test_general/test_general.c index ecec3e014ba0b1..ea1f2ece0a5d30 100644 --- a/test/addons-napi/test_general/test_general.c +++ b/test/addons-napi/test_general/test_general.c @@ -205,6 +205,16 @@ napi_value finalize_was_called(napi_env env, napi_callback_info info) { return it_was_called; } +napi_value testAdjustExternalMemory(napi_env env, napi_callback_info info) { + napi_value result; + int64_t adjustedValue; + + NAPI_CALL(env, napi_adjust_external_memory(env, 1, &adjustedValue)); + NAPI_CALL(env, napi_create_double(env, adjustedValue, &result)); + + return result; +} + void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("testStrictEquals", testStrictEquals), @@ -222,6 +232,7 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) { DECLARE_NAPI_PROPERTY("testFinalizeWrap", test_finalize_wrap), DECLARE_NAPI_PROPERTY("finalizeWasCalled", finalize_was_called), DECLARE_NAPI_PROPERTY("derefItemWasCalled", deref_item_was_called), + DECLARE_NAPI_PROPERTY("testAdjustExternalMemory", testAdjustExternalMemory) }; NAPI_CALL_RETURN_VOID(env, napi_define_properties( From 32504e1ab24939359776d827a520900065399161 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Wed, 6 Sep 2017 13:00:43 +0300 Subject: [PATCH 100/183] n-api: implement napi_run_script Fixes: https://github.com/nodejs/abi-stable-node/issues/51 PR-URL: https://github.com/nodejs/node/pull/15216 Reviewed-By: Anna Henningsen Reviewed-By: Benjamin Gruenbaum Reviewed-By: Colin Ihrig Reviewed-By: Michael Dawson Reviewed-By: Timothy Gu --- doc/api/n-api.md | 21 +++++++++++++++ src/node_api.cc | 27 ++++++++++++++++++++ src/node_api.h | 5 ++++ test/addons-napi/test_general/testNapiRun.js | 12 +++++++++ test/addons-napi/test_general/test_general.c | 13 ++++++++++ 5 files changed, 78 insertions(+) create mode 100644 test/addons-napi/test_general/testNapiRun.js diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 9c56e50e83f299..80d83612509ca1 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -43,6 +43,7 @@ The documentation for N-API is structured as follows: * [Object Wrap][] * [Asynchronous Operations][] * [Promises][] +* [Script Execution][] The N-API is a C API that ensures ABI stability across Node.js versions and different compiler levels. However, we also understand that a C++ @@ -3556,6 +3557,25 @@ NAPI_EXTERN napi_status napi_is_promise(napi_env env, - `[out] is_promise`: Flag indicating whether `promise` is a native promise object - that is, a promise object created by the underlying engine. +## Script execution + +N-API provides an API for executing a string containing JavaScript using the +underlying JavaScript engine. + +### napi_run_script + +```C +NAPI_EXTERN napi_status napi_run_script(napi_env env, + napi_value script, + napi_value* result); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] script`: A JavaScript string containing the script to execute. +- `[out] result`: The value resulting from having executed the script. + [Promises]: #n_api_promises [Asynchronous Operations]: #n_api_asynchronous_operations [Basic N-API Data Types]: #n_api_basic_n_api_data_types @@ -3565,6 +3585,7 @@ object - that is, a promise object created by the underlying engine. [Native Abstractions for Node.js]: https://github.com/nodejs/nan [Object Lifetime Management]: #n_api_object_lifetime_management [Object Wrap]: #n_api_object_wrap +[Script Execution]: #n_api_script_execution [Section 9.1.6]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc [Section 12.5.5]: https://tc39.github.io/ecma262/#sec-typeof-operator [Section 24.3]: https://tc39.github.io/ecma262/#sec-dataview-objects diff --git a/src/node_api.cc b/src/node_api.cc index a2322295fccbaa..9c8e0edec94d40 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -3426,3 +3426,30 @@ NAPI_EXTERN napi_status napi_is_promise(napi_env env, return napi_clear_last_error(env); } + +NAPI_EXTERN napi_status napi_run_script(napi_env env, + napi_value script, + napi_value* result) { + NAPI_PREAMBLE(env); + CHECK_ARG(env, script); + CHECK_ARG(env, result); + + v8::Local v8_script = v8impl::V8LocalValueFromJsValue(script); + + if (!v8_script->IsString()) { + return napi_set_last_error(env, napi_string_expected); + } + + v8::Local context = env->isolate->GetCurrentContext(); + + auto maybe_script = v8::Script::Compile(context, + v8::Local::Cast(v8_script)); + CHECK_MAYBE_EMPTY(env, maybe_script, napi_generic_failure); + + auto script_result = + maybe_script.ToLocalChecked()->Run(context); + CHECK_MAYBE_EMPTY(env, script_result, napi_generic_failure); + + *result = v8impl::JsValueFromV8LocalValue(script_result.ToLocalChecked()); + return GET_RETURN_STATUS(env); +} diff --git a/src/node_api.h b/src/node_api.h index 702ddf2d9e6a0e..807595777dd947 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -562,6 +562,11 @@ NAPI_EXTERN napi_status napi_adjust_external_memory(napi_env env, int64_t change_in_bytes, int64_t* adjusted_value); +// Runnig a script +NAPI_EXTERN napi_status napi_run_script(napi_env env, + napi_value script, + napi_value* result); + EXTERN_C_END #endif // SRC_NODE_API_H_ diff --git a/test/addons-napi/test_general/testNapiRun.js b/test/addons-napi/test_general/testNapiRun.js new file mode 100644 index 00000000000000..d7534ecf9c3912 --- /dev/null +++ b/test/addons-napi/test_general/testNapiRun.js @@ -0,0 +1,12 @@ +'use strict'; + +const common = require('../../common'); +const assert = require('assert'); + +// addon is referenced through the eval expression in testFile +// eslint-disable-next-line no-unused-vars +const addon = require(`./build/${common.buildType}/test_general`); + +assert.strictEqual(addon.testNapiRun('(41.92 + 0.08);'), 42, + 'napi_run_script() works correctly'); +assert.throws(() => addon.testNapiRun({ abc: 'def' }), /string was expected/); diff --git a/test/addons-napi/test_general/test_general.c b/test/addons-napi/test_general/test_general.c index ea1f2ece0a5d30..287c1d7cb8cb34 100644 --- a/test/addons-napi/test_general/test_general.c +++ b/test/addons-napi/test_general/test_general.c @@ -1,4 +1,5 @@ #include +#include #include "../common.h" napi_value testStrictEquals(napi_env env, napi_callback_info info) { @@ -215,12 +216,24 @@ napi_value testAdjustExternalMemory(napi_env env, napi_callback_info info) { return result; } +napi_value testNapiRun(napi_env env, napi_callback_info info) { + napi_value script, result; + size_t argc = 1; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &script, NULL, NULL)); + + NAPI_CALL(env, napi_run_script(env, script, &result)); + + return result; +} + void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("testStrictEquals", testStrictEquals), DECLARE_NAPI_PROPERTY("testGetPrototype", testGetPrototype), DECLARE_NAPI_PROPERTY("testGetVersion", testGetVersion), DECLARE_NAPI_PROPERTY("testGetNodeVersion", testGetNodeVersion), + DECLARE_NAPI_PROPERTY("testNapiRun", testNapiRun), DECLARE_NAPI_PROPERTY("doInstanceOf", doInstanceOf), DECLARE_NAPI_PROPERTY("getUndefined", getUndefined), DECLARE_NAPI_PROPERTY("getNull", getNull), From 63e86053beef1ca5755f50ff1ff356924f853ac8 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Sat, 9 Sep 2017 12:15:14 +0300 Subject: [PATCH 101/183] n-api: stop creating references to primitives The binding testing napi_wrap() creates references to primitives passed into the binding in its second parameter. This is unnecessary and not at all the point of the test. Additionally, creating persistent references to primitive values may not be supported by all VMs, since primitives are best persisted in their native form. Instead, the point of the test is to make sure that the finalize callback gets called when it should get called, that it gets called with the correct pointer, and that it does not get called when it should not get called. Creating persistent references is not necessary for verifying this. PR-URL: https://github.com/nodejs/node/pull/15289 Reviewed-By: Jason Ginchereau Reviewed-By: Colin Ihrig Re: https://github.com/nodejs/node-chakracore/issues/380 --- doc/api/n-api.md | 2 +- src/node_api.cc | 10 ++++++++-- test/addons-napi/test_general/test.js | 10 +++++----- test/addons-napi/test_general/test_general.c | 17 +++++++---------- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 80d83612509ca1..0efa21056f0e74 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -787,7 +787,7 @@ NODE_EXTERN napi_status napi_create_reference(napi_env env, - `[in] env`: The environment that the API is invoked under. - `[in] value`: `napi_value` representing the Object to which we want -a reference to. +a reference. - `[in] initial_refcount`: Initial reference count for the new reference. - `[out] result`: `napi_ref` pointing to the new reference. diff --git a/src/node_api.cc b/src/node_api.cc index 9c8e0edec94d40..5b70b9678dd2e7 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -2461,8 +2461,14 @@ napi_status napi_create_reference(napi_env env, CHECK_ARG(env, value); CHECK_ARG(env, result); - v8impl::Reference* reference = v8impl::Reference::New( - env, v8impl::V8LocalValueFromJsValue(value), initial_refcount, false); + v8::Local v8_value = v8impl::V8LocalValueFromJsValue(value); + + if (!(v8_value->IsObject() || v8_value->IsFunction())) { + return napi_set_last_error(env, napi_object_expected); + } + + v8impl::Reference* reference = + v8impl::Reference::New(env, v8_value, initial_refcount, false); *result = reinterpret_cast(reference); return napi_clear_last_error(env); diff --git a/test/addons-napi/test_general/test.js b/test/addons-napi/test_general/test.js index ec59922639e949..51ce9563a7196e 100644 --- a/test/addons-napi/test_general/test.js +++ b/test/addons-napi/test_general/test.js @@ -60,7 +60,7 @@ assert.strictEqual(test_general.testNapiTypeof(null), 'null'); // Ensure that garbage collecting an object with a wrapped native item results // in the finalize callback being called. let w = {}; -test_general.wrap(w, []); +test_general.wrap(w); w = null; global.gc(); assert.strictEqual(test_general.derefItemWasCalled(), true, @@ -69,17 +69,17 @@ assert.strictEqual(test_general.derefItemWasCalled(), true, // Assert that wrapping twice fails. const x = {}; -test_general.wrap(x, 25); +test_general.wrap(x); assert.throws(function() { - test_general.wrap(x, 'Blah'); + test_general.wrap(x); }, Error); // Ensure that wrapping, removing the wrap, and then wrapping again works. const y = {}; -test_general.wrap(y, -12); +test_general.wrap(y); test_general.removeWrap(y); assert.doesNotThrow(function() { - test_general.wrap(y, 're-wrap!'); + test_general.wrap(y); }, Error, 'Wrapping twice succeeds if a remove_wrap() separates the instances'); // Ensure that removing a wrap and garbage collecting does not fire the diff --git a/test/addons-napi/test_general/test_general.c b/test/addons-napi/test_general/test_general.c index 287c1d7cb8cb34..048066d25c789b 100644 --- a/test/addons-napi/test_general/test_general.c +++ b/test/addons-napi/test_general/test_general.c @@ -143,8 +143,10 @@ static bool deref_item_called = false; static void deref_item(napi_env env, void* data, void* hint) { (void) hint; + NAPI_ASSERT_RETURN_VOID(env, data == &deref_item_called, + "Finalize callback was called with the correct pointer"); + deref_item_called = true; - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, (napi_ref)data)); } napi_value deref_item_was_called(napi_env env, napi_callback_info info) { @@ -156,15 +158,13 @@ napi_value deref_item_was_called(napi_env env, napi_callback_info info) { } napi_value wrap(napi_env env, napi_callback_info info) { - size_t argc = 2; - napi_value argv[2]; - napi_ref payload; + size_t argc = 1; + napi_value to_wrap; deref_item_called = false; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); - NAPI_CALL(env, napi_create_reference(env, argv[1], 1, &payload)); - NAPI_CALL(env, napi_wrap(env, argv[0], payload, deref_item, NULL, NULL)); + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &to_wrap, NULL, NULL)); + NAPI_CALL(env, napi_wrap(env, to_wrap, &deref_item_called, deref_item, NULL, NULL)); return NULL; } @@ -176,9 +176,6 @@ napi_value remove_wrap(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &wrapped, NULL, NULL)); NAPI_CALL(env, napi_remove_wrap(env, wrapped, &data)); - if (data != NULL) { - NAPI_CALL(env, napi_delete_reference(env, (napi_ref)data)); - } return NULL; } From 9c68371202181b11a8a262e99cc9d5d34f1d1a75 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 8 Aug 2017 20:18:16 +0200 Subject: [PATCH 102/183] n-api: use AsyncResource for Work tracking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Enable combining N-API async work with async-hooks. PR-URL: https://github.com/nodejs/node/pull/14697 Reviewed-By: Tobias Nießen Reviewed-By: Refael Ackermann Reviewed-By: James M Snell Reviewed-By: Jason Ginchereau Reviewed-By: Michael Dawson --- doc/api/n-api.md | 20 ++++++++++++++ src/node_api.cc | 32 +++++++++++++++++++---- src/node_api.h | 2 ++ test/addons-napi/test_async/test.js | 4 +-- test/addons-napi/test_async/test_async.cc | 31 +++++++++++++--------- 5 files changed, 69 insertions(+), 20 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 0efa21056f0e74..0f04c21bd7fa37 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -3256,10 +3256,16 @@ callback invocation, even when it was cancelled. ### napi_create_async_work ```C NAPI_EXTERN napi_status napi_create_async_work(napi_env env, + napi_value async_resource, + const char* async_resource_name, napi_async_execute_callback execute, napi_async_complete_callback complete, void* data, @@ -3267,6 +3273,10 @@ napi_status napi_create_async_work(napi_env env, ``` - `[in] env`: The environment that the API is invoked under. +- `[in] async_resource`: An optional object associated with the async work + that will be passed to possible async_hooks [`init` hooks][]. +- `[in] async_resource_name`: An identifier for the kind of resource that is +being provided for diagnostic information exposed by the `async_hooks` API. - `[in] execute`: The native function which should be called to excute the logic asynchronously. - `[in] complete`: The native function which will be called when the @@ -3282,6 +3292,14 @@ This API allocates a work object that is used to execute logic asynchronously. It should be freed using [`napi_delete_async_work`][] once the work is no longer required. +`async_resource_name` should be a null-terminated, UTF-8-encoded string. + +*Note*: The `async_resource_name` identifier is provided by the user and should +be representative of the type of async work being performed. It is also +recommended to apply namespacing to the identifier, e.g. by including the +module name. See the [`async_hooks` documentation][async_hooks `type`] +for more information. + ### napi_delete_async_work ```C -NAPI_EXTERN napi_status +napi_status napi_get_last_error_info(napi_env env, const napi_extended_error_info** result); ``` @@ -515,8 +516,8 @@ This API returns a JavaScript RangeError with the text provided. added: v8.0.0 --> ```C -NAPI_EXTERN napi_status napi_get_and_clear_last_exception(napi_env env, - napi_value* result); +napi_status napi_get_and_clear_last_exception(napi_env env, + napi_value* result); ``` - `[in] env`: The environment that the API is invoked under. @@ -531,7 +532,7 @@ This API returns true if an exception is pending. added: v8.0.0 --> ```C -NAPI_EXTERN napi_status napi_is_exception_pending(napi_env env, bool* result); +napi_status napi_is_exception_pending(napi_env env, bool* result); ``` - `[in] env`: The environment that the API is invoked under. @@ -551,7 +552,7 @@ thrown to immediately terminate the process. added: REPLACEME --> ```C -NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location, const char* message); +NAPI_NO_RETURN void napi_fatal_error(const char* location, const char* message); ``` - `[in] location`: Optional location at which the error occurred. @@ -718,10 +719,10 @@ reverse order from which they were created. added: v8.0.0 --> ```C -NAPI_EXTERN napi_status napi_escape_handle(napi_env env, - napi_escapable_handle_scope scope, - napi_value escapee, - napi_value* result); +napi_status napi_escape_handle(napi_env env, + napi_escapable_handle_scope scope, + napi_value escapee, + napi_value* result); ``` - `[in] env`: The environment that the API is invoked under. @@ -1478,10 +1479,10 @@ of the ECMAScript Language Specification. added: v8.0.0 --> ```C -NAPI_EXTERN napi_status napi_create_string_latin1(napi_env env, - const char* str, - size_t length, - napi_value* result); +napi_status napi_create_string_latin1(napi_env env, + const char* str, + size_t length, + napi_value* result); ``` - `[in] env`: The environment that the API is invoked under. @@ -1811,11 +1812,11 @@ JavaScript Number added: v8.0.0 --> ```C -NAPI_EXTERN napi_status napi_get_value_string_latin1(napi_env env, - napi_value value, - char* buf, - size_t bufsize, - size_t* result) +napi_status napi_get_value_string_latin1(napi_env env, + napi_value value, + char* buf, + size_t bufsize, + size_t* result) ``` - `[in] env`: The environment that the API is invoked under. @@ -2790,8 +2791,8 @@ in as arguments to the function. Returns `napi_ok` if the API succeeded. This method allows a JavaScript function object to be called from a native -add-on. This is an primary mechanism of calling back *from* the add-on's -native code *into* JavaScript. For special cases like calling into JavaScript +add-on. This is the primary mechanism of calling back *from* the add-on's +native code *into* JavaScript. For the special case of calling into JavaScript after an async operation, see [`napi_make_callback`][]. A sample use case might look as follows. Consider the following JavaScript @@ -3003,39 +3004,6 @@ status = napi_new_instance(env, constructor, argc, argv, &value); Returns `napi_ok` if the API succeeded. -### *napi_make_callback* - -```C -napi_status napi_make_callback(napi_env env, - napi_value recv, - napi_value func, - int argc, - const napi_value* argv, - napi_value* result) -``` - -- `[in] env`: The environment that the API is invoked under. -- `[in] recv`: The `this` object passed to the called function. -- `[in] func`: `napi_value` representing the JavaScript function -to be invoked. -- `[in] argc`: The count of elements in the `argv` array. -- `[in] argv`: Array of JavaScript values as `napi_value` -representing the arguments to the function. -- `[out] result`: `napi_value` representing the JavaScript object returned. - -Returns `napi_ok` if the API succeeded. - -This method allows a JavaScript function object to be called from a native -add-on. This API is similar to `napi_call_function`. However, it is used to call -*from* native code back *into* JavaScript *after* returning from an async -operation (when there is no other script on the stack). It is a fairly simple -wrapper around `node::MakeCallback`. - -For an example on how to use `napi_make_callback`, see the section on -[Asynchronous Operations][]. - ## Object Wrap N-API offers a way to "wrap" C++ classes and instances so that the class @@ -3214,7 +3182,7 @@ restoring the JavaScript object's prototype chain. If a finalize callback was associated with the wrapping, it will no longer be called when the JavaScript object becomes garbage-collected. -## Asynchronous Operations +## Simple Asynchronous Operations Addon modules often need to leverage async helpers from libuv as part of their implementation. This allows them to schedule work to be executed asynchronously @@ -3250,8 +3218,8 @@ Once created the async worker can be queued for execution using the [`napi_queue_async_work`][] function: ```C -NAPI_EXTERN napi_status napi_queue_async_work(napi_env env, - napi_async_work work); +napi_status napi_queue_async_work(napi_env env, + napi_async_work work); ``` [`napi_cancel_async_work`][] can be used if the work needs @@ -3271,7 +3239,6 @@ changes: description: Added `async_resource` and `async_resource_name` parameters. --> ```C -NAPI_EXTERN napi_status napi_create_async_work(napi_env env, napi_value async_resource, napi_value async_resource_name, @@ -3314,8 +3281,8 @@ for more information. added: v8.0.0 --> ```C -NAPI_EXTERN napi_status napi_delete_async_work(napi_env env, - napi_async_work work); +napi_status napi_delete_async_work(napi_env env, + napi_async_work work); ``` - `[in] env`: The environment that the API is invoked under. @@ -3330,8 +3297,8 @@ This API frees a previously allocated work object. added: v8.0.0 --> ```C -NAPI_EXTERN napi_status napi_queue_async_work(napi_env env, - napi_async_work work); +napi_status napi_queue_async_work(napi_env env, + napi_async_work work); ``` - `[in] env`: The environment that the API is invoked under. @@ -3347,8 +3314,8 @@ for execution. added: v8.0.0 --> ```C -NAPI_EXTERN napi_status napi_cancel_async_work(napi_env env, - napi_async_work work); +napi_status napi_cancel_async_work(napi_env env, + napi_async_work work); ``` - `[in] env`: The environment that the API is invoked under. @@ -3363,6 +3330,93 @@ the `complete` callback will be invoked with a status value of `napi_cancelled`. The work should not be deleted before the `complete` callback invocation, even if it has been successfully cancelled. +## Custom Asynchronous Operations +The simple asynchronous work APIs above may not be appropriate for every +scenario, because with those the async execution still happens on the main +event loop. When using any other async mechanism, the following APIs are +necessary to ensure an async operation is properly tracked by the runtime. + +### *napi_async_init** + +```C +napi_status napi_async_init(napi_env env, + napi_value async_resource, + napi_value async_resource_name, + napi_async_context* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] async_resource`: An optional object associated with the async work + that will be passed to possible `async_hooks` [`init` hooks][]. +- `[in] async_resource_name`: Required identifier for the kind of resource + that is being provided for diagnostic information exposed by the + `async_hooks` API. +- `[out] result`: The initialized async context. + +Returns `napi_ok` if the API succeeded. + +### *napi_async_destroy** + +```C +napi_status napi_async_destroy(napi_env env, + napi_async_context async_context); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] async_context`: The async context to be destroyed. + +Returns `napi_ok` if the API succeeded. + +### *napi_make_callback* + +```C +napi_status napi_make_callback(napi_env env, + napi_async_context async_context, + napi_value recv, + napi_value func, + int argc, + const napi_value* argv, + napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] async_context`: Context for the async operation that is + invoking the callback. This should normally be a value previously + obtained from [`napi_async_init`][]. However `NULL` is also allowed, + which indicates the current async context (if any) is to be used + for the callback. +- `[in] recv`: The `this` object passed to the called function. +- `[in] func`: `napi_value` representing the JavaScript function +to be invoked. +- `[in] argc`: The count of elements in the `argv` array. +- `[in] argv`: Array of JavaScript values as `napi_value` +representing the arguments to the function. +- `[out] result`: `napi_value` representing the JavaScript object returned. + +Returns `napi_ok` if the API succeeded. + +This method allows a JavaScript function object to be called from a native +add-on. This API is similar to `napi_call_function`. However, it is used to call +*from* native code back *into* JavaScript *after* returning from an async +operation (when there is no other script on the stack). It is a fairly simple +wrapper around `node::MakeCallback`. + +Note it is *not* necessary to use `napi_make_callback` from within a +`napi_async_complete_callback`; in that situation the callback's async +context has already been set up, so a direct call to `napi_call_function` +is sufficient and appropriate. Use of the `napi_make_callback` function +may be required when implementing custom async behavior that does not use +`napi_create_async_work`. + ## Version Management ### napi_get_node_version @@ -3378,7 +3432,6 @@ typedef struct { const char* release; } napi_node_version; -NAPI_EXTERN napi_status napi_get_node_version(napi_env env, const napi_node_version** version); ``` @@ -3399,8 +3452,8 @@ The returned buffer is statically allocated and does not need to be freed. added: REPLACEME --> ```C -NAPI_EXTERN napi_status napi_get_version(napi_env env, - uint32_t* result); +napi_status napi_get_version(napi_env env, + uint32_t* result); ``` - `[in] env`: The environment that the API is invoked under. @@ -3508,9 +3561,9 @@ deferred = NULL; added: REPLACEME --> ```C -NAPI_EXTERN napi_status napi_create_promise(napi_env env, - napi_deferred* deferred, - napi_value* promise); +napi_status napi_create_promise(napi_env env, + napi_deferred* deferred, + napi_value* promise); ``` - `[in] env`: The environment that the API is invoked under. @@ -3528,9 +3581,9 @@ This API creates a deferred object and a JavaScript promise. added: REPLACEME --> ```C -NAPI_EXTERN napi_status napi_resolve_deferred(napi_env env, - napi_deferred deferred, - napi_value resolution); +napi_status napi_resolve_deferred(napi_env env, + napi_deferred deferred, + napi_value resolution); ``` - `[in] env`: The environment that the API is invoked under. @@ -3551,9 +3604,9 @@ The deferred object is freed upon successful completion. added: REPLACEME --> ```C -NAPI_EXTERN napi_status napi_reject_deferred(napi_env env, - napi_deferred deferred, - napi_value rejection); +napi_status napi_reject_deferred(napi_env env, + napi_deferred deferred, + napi_value rejection); ``` - `[in] env`: The environment that the API is invoked under. @@ -3574,9 +3627,9 @@ The deferred object is freed upon successful completion. added: REPLACEME --> ```C -NAPI_EXTERN napi_status napi_is_promise(napi_env env, - napi_value promise, - bool* is_promise); +napi_status napi_is_promise(napi_env env, + napi_value promise, + bool* is_promise); ``` - `[in] env`: The environment that the API is invoked under. @@ -3604,7 +3657,8 @@ NAPI_EXTERN napi_status napi_run_script(napi_env env, - `[out] result`: The value resulting from having executed the script. [Promises]: #n_api_promises -[Asynchronous Operations]: #n_api_asynchronous_operations +[Simple Asynchronous Operations]: #n_api_asynchronous_operations +[Custom Asynchronous Operations]: #n_api_custom_asynchronous_operations [Basic N-API Data Types]: #n_api_basic_n_api_data_types [ECMAScript Language Specification]: https://tc39.github.io/ecma262/ [Error Handling]: #n_api_error_handling diff --git a/src/node_api.cc b/src/node_api.cc index 5efb6b4f549e6a..246b3fd8085a41 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -2770,7 +2770,52 @@ napi_status napi_instanceof(napi_env env, return GET_RETURN_STATUS(env); } +napi_status napi_async_init(napi_env env, + napi_value async_resource, + napi_value async_resource_name, + napi_async_context* result) { + CHECK_ENV(env); + CHECK_ARG(env, async_resource_name); + CHECK_ARG(env, result); + + v8::Isolate* isolate = env->isolate; + v8::Local context = isolate->GetCurrentContext(); + + v8::Local v8_resource; + if (async_resource != nullptr) { + CHECK_TO_OBJECT(env, context, v8_resource, async_resource); + } else { + v8_resource = v8::Object::New(isolate); + } + + v8::Local v8_resource_name; + CHECK_TO_STRING(env, context, v8_resource_name, async_resource_name); + + // TODO(jasongin): Consider avoiding allocation here by using + // a tagged pointer with 2×31 bit fields instead. + node::async_context* async_context = new node::async_context(); + + *async_context = node::EmitAsyncInit(isolate, v8_resource, v8_resource_name); + *result = reinterpret_cast(async_context); + + return napi_clear_last_error(env); +} + +napi_status napi_async_destroy(napi_env env, + napi_async_context async_context) { + CHECK_ENV(env); + CHECK_ARG(env, async_context); + + v8::Isolate* isolate = env->isolate; + node::async_context* node_async_context = + reinterpret_cast(async_context); + node::EmitAsyncDestroy(isolate, *node_async_context); + + return napi_clear_last_error(env); +} + napi_status napi_make_callback(napi_env env, + napi_async_context async_context, napi_value recv, napi_value func, size_t argc, @@ -2791,12 +2836,22 @@ napi_status napi_make_callback(napi_env env, v8::Local v8func; CHECK_TO_FUNCTION(env, v8func, func); - v8::Local callback_result = node::MakeCallback( + node::async_context* node_async_context = + reinterpret_cast(async_context); + if (node_async_context == nullptr) { + static node::async_context empty_context = { 0, 0 }; + node_async_context = &empty_context; + } + + v8::MaybeLocal callback_result = node::MakeCallback( isolate, v8recv, v8func, argc, - reinterpret_cast*>(const_cast(argv))); + reinterpret_cast*>(const_cast(argv)), + *node_async_context); + CHECK_MAYBE_EMPTY(env, callback_result, napi_generic_failure); if (result != nullptr) { - *result = v8impl::JsValueFromV8LocalValue(callback_result); + *result = v8impl::JsValueFromV8LocalValue( + callback_result.ToLocalChecked()); } return GET_RETURN_STATUS(env); diff --git a/src/node_api.h b/src/node_api.h index cdc3a6bdb7ebe3..c7d5e746f1b98d 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -318,14 +318,6 @@ NAPI_EXTERN napi_status napi_instanceof(napi_env env, napi_value constructor, bool* result); -// Napi version of node::MakeCallback(...) -NAPI_EXTERN napi_status napi_make_callback(napi_env env, - napi_value recv, - napi_value func, - size_t argc, - const napi_value* argv, - napi_value* result); - // Methods to work with napi_callbacks // Gets all callback info in a single call. (Ugly, but faster.) @@ -535,6 +527,22 @@ NAPI_EXTERN napi_status napi_queue_async_work(napi_env env, NAPI_EXTERN napi_status napi_cancel_async_work(napi_env env, napi_async_work work); +// Methods for custom handling of async operations +NAPI_EXTERN napi_status napi_async_init(napi_env env, + napi_value async_resource, + napi_value async_resource_name, + napi_async_context* result); + +NAPI_EXTERN napi_status napi_async_destroy(napi_env env, + napi_async_context async_context); + +NAPI_EXTERN napi_status napi_make_callback(napi_env env, + napi_async_context async_context, + napi_value recv, + napi_value func, + size_t argc, + const napi_value* argv, + napi_value* result); // version management NAPI_EXTERN napi_status napi_get_version(napi_env env, uint32_t* result); diff --git a/src/node_api_backport.cc b/src/node_api_backport.cc index 249e3e8100efef..bd2c33242a4af7 100644 --- a/src/node_api_backport.cc +++ b/src/node_api_backport.cc @@ -81,6 +81,26 @@ AsyncResource::~AsyncResource() { object.Reset(); } +async_context EmitAsyncInit(v8::Isolate* isolate, + v8::Local resource, + v8::Local name, + async_id trigger_async_id) { + return async_context(); +} + +void EmitAsyncDestroy(v8::Isolate* isolate, + async_context asyncContext) { +} + +v8::MaybeLocal MakeCallback(v8::Isolate* isolate, + v8::Local recv, + v8::Local callback, + int argc, + v8::Local* argv, + async_context asyncContext) { + return node::MakeCallback(isolate, recv, callback, argc, argv); +} + } // end of namespace node CallbackScope::CallbackScope(node::AsyncResource* work) : diff --git a/src/node_api_backport.h b/src/node_api_backport.h index 549b3ed42033de..9ef12cebd8f677 100644 --- a/src/node_api_backport.h +++ b/src/node_api_backport.h @@ -31,6 +31,21 @@ class CallbackScope { Environment::AsyncCallbackScope callback_scope; }; +NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate, + v8::Local resource, + v8::Local name, + async_id trigger_async_id = -1); + +NODE_EXTERN void EmitAsyncDestroy(v8::Isolate* isolate, + async_context asyncContext); + +v8::MaybeLocal MakeCallback(v8::Isolate* isolate, + v8::Local recv, + v8::Local callback, + int argc, + v8::Local* argv, + async_context asyncContext); + class AsyncResource { public: AsyncResource(v8::Isolate* _isolate, diff --git a/src/node_api_types.h b/src/node_api_types.h index ac8482bf9dcf9d..574cb6ff984f16 100644 --- a/src/node_api_types.h +++ b/src/node_api_types.h @@ -16,6 +16,7 @@ typedef struct napi_ref__ *napi_ref; typedef struct napi_handle_scope__ *napi_handle_scope; typedef struct napi_escapable_handle_scope__ *napi_escapable_handle_scope; typedef struct napi_callback_info__ *napi_callback_info; +typedef struct napi_async_context__ *napi_async_context; typedef struct napi_async_work__ *napi_async_work; typedef struct napi_deferred__ *napi_deferred; diff --git a/test/addons-napi/test_make_callback/binding.cc b/test/addons-napi/test_make_callback/binding.cc index 1ba28c0ef40b72..9032e8b2f3f301 100644 --- a/test/addons-napi/test_make_callback/binding.cc +++ b/test/addons-napi/test_make_callback/binding.cc @@ -24,14 +24,22 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_typeof(env, func, &func_type)); + napi_value resource_name; + NAPI_CALL(env, napi_create_string_utf8(env, "test", -1, &resource_name)); + + napi_async_context context; + NAPI_CALL(env, napi_async_init(env, func, resource_name, &context)); + napi_value result; if (func_type == napi_function) { - NAPI_CALL(env, - napi_make_callback(env, recv, func, argv.size(), argv.data(), &result)); + NAPI_CALL(env, napi_make_callback( + env, context, recv, func, argv.size(), argv.data(), &result)); } else { NAPI_ASSERT(env, false, "Unexpected argument type"); } + NAPI_CALL(env, napi_async_destroy(env, context)); + return result; } diff --git a/test/addons-napi/test_make_callback/test.js b/test/addons-napi/test_make_callback/test.js index c4f24872bdb78e..56e2b3f4e2b6c6 100644 --- a/test/addons-napi/test_make_callback/test.js +++ b/test/addons-napi/test_make_callback/test.js @@ -7,7 +7,6 @@ const binding = require(`./build/${common.buildType}/binding`); const makeCallback = binding.makeCallback; function myMultiArgFunc(arg1, arg2, arg3) { - console.log(`MyFunc was called with ${arguments.length} arguments`); assert.strictEqual(arg1, 1); assert.strictEqual(arg2, 2); assert.strictEqual(arg3, 3); diff --git a/test/addons-napi/test_make_callback_recurse/binding.cc b/test/addons-napi/test_make_callback_recurse/binding.cc index f3c80ab103406e..d0ee1ddb4998e6 100644 --- a/test/addons-napi/test_make_callback_recurse/binding.cc +++ b/test/addons-napi/test_make_callback_recurse/binding.cc @@ -12,8 +12,8 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) { napi_value recv = args[0]; napi_value func = args[1]; - napi_make_callback(env, - recv, func, 0 /* argc */, nullptr /* argv */, nullptr /* result */); + napi_make_callback(env, nullptr /* async_context */, + recv, func, 0 /* argc */, nullptr /* argv */, nullptr /* result */); return recv; } From 574f09e48668032f451386c288f8c100895613c4 Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Tue, 8 Aug 2017 16:21:56 -0400 Subject: [PATCH 106/183] n-api: napi_is_construct_call->napi_get_new_target Remove napi_is_construct_call and introduce napi_get_new_target. PR-URL: https://github.com/nodejs/node/pull/14698 Reviewed-By: Jason Ginchereau Reviewed-By: Colin Ihrig Reviewed-By: Michael Dawson Reviewed-By: James M Snell Reviewed-By: Kyle Farnung --- doc/api/n-api.md | 17 ++++++++-------- src/node_api.cc | 23 ++++++++++++---------- src/node_api.h | 6 +++--- test/addons-napi/6_object_wrap/myobject.cc | 5 +++-- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 1d9b405c78a07e..4797e62fd97d46 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -2928,25 +2928,24 @@ Returns `napi_ok` if the API succeeded. This method is used within a callback function to retrieve details about the call like the arguments and the `this` pointer from a given callback info. -### *napi_is_construct_call* +### *napi_get_new_target* ```C -napi_status napi_is_construct_call(napi_env env, - napi_callback_info cbinfo, - bool* result) +napi_status napi_get_new_target(napi_env env, + napi_callback_info cbinfo, + napi_value* result) ``` - `[in] env`: The environment that the API is invoked under. - `[in] cbinfo`: The callback info passed into the callback function. -- `[out] result`: Whether the native function is being invoked as -a constructor call. +- `[out] result`: The `new.target` of the constructor call. Returns `napi_ok` if the API succeeded. -This API checks if the the current callback was due to a -consructor call. +This API returns the `new.target` of the constructor call. If the current +callback is not a constructor call, the result is `nullptr`. ### *napi_new_instance* ```C -NAPI_NO_RETURN void napi_fatal_error(const char* location, const char* message); +NAPI_NO_RETURN void napi_fatal_error(const char* location, + size_t location_len, + const char* message, + size_t message_len); ``` - `[in] location`: Optional location at which the error occurred. +- `[in] location_len`: The length of the location in bytes, or -1 if it is +null-terminated. - `[in] message`: The message associated with the error. +- `[in] message_len`: The length of the message in bytes, or -1 if it is +null-terminated. The function call does not return, the process will be terminated. @@ -1248,6 +1255,7 @@ added: v8.0.0 ```C napi_status napi_create_function(napi_env env, const char* utf8name, + size_t length, napi_callback cb, void* data, napi_value* result) @@ -1256,6 +1264,8 @@ napi_status napi_create_function(napi_env env, - `[in] env`: The environment that the API is invoked under. - `[in] utf8name`: A string representing the name of the function encoded as UTF8. +- `[in] length`: The length of the utf8name in bytes, or -1 if it is +null-terminated. - `[in] cb`: A function pointer to the native function to be invoked when the created function is invoked from JavaScript. - `[in] data`: Optional arbitrary context data to be passed into the native @@ -3026,6 +3036,7 @@ added: v8.0.0 ```C napi_status napi_define_class(napi_env env, const char* utf8name, + size_t length, napi_callback constructor, void* data, size_t property_count, @@ -3037,6 +3048,8 @@ napi_status napi_define_class(napi_env env, - `[in] utf8name`: Name of the JavaScript constructor function; this is not required to be the same as the C++ class name, though it is recommended for clarity. + - `[in] length`: The length of the utf8name in bytes, or -1 if it is +null-terminated. - `[in] constructor`: Callback function that handles constructing instances of the class. (This should be a static method on the class, not an actual C++ constructor function.) diff --git a/src/node_api.cc b/src/node_api.cc index 7c68abfc6797de..44751a8f945da9 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -938,12 +938,29 @@ napi_status napi_get_last_error_info(napi_env env, } NAPI_NO_RETURN void napi_fatal_error(const char* location, - const char* message) { - node::FatalError(location, message); + size_t location_len, + const char* message, + size_t message_len) { + char* location_string = const_cast(location); + char* message_string = const_cast(message); + if (location_len != -1) { + location_string = reinterpret_cast( + malloc(location_len * sizeof(char) + 1)); + strncpy(location_string, location, location_len); + location_string[location_len] = '\0'; + } + if (message_len != -1) { + message_string = reinterpret_cast( + malloc(message_len * sizeof(char) + 1)); + strncpy(message_string, message, message_len); + message_string[message_len] = '\0'; + } + node::FatalError(location_string, message_string); } napi_status napi_create_function(napi_env env, const char* utf8name, + size_t length, napi_callback cb, void* callback_data, napi_value* result) { @@ -970,7 +987,7 @@ napi_status napi_create_function(napi_env env, if (utf8name != nullptr) { v8::Local name_string; - CHECK_NEW_FROM_UTF8(env, name_string, utf8name); + CHECK_NEW_FROM_UTF8_LEN(env, name_string, utf8name, length); return_value->SetName(name_string); } @@ -981,6 +998,7 @@ napi_status napi_create_function(napi_env env, napi_status napi_define_class(napi_env env, const char* utf8name, + size_t length, napi_callback constructor, void* callback_data, size_t property_count, @@ -1002,7 +1020,7 @@ napi_status napi_define_class(napi_env env, isolate, v8impl::FunctionCallbackWrapper::Invoke, cbdata); v8::Local name_string; - CHECK_NEW_FROM_UTF8(env, name_string, utf8name); + CHECK_NEW_FROM_UTF8_LEN(env, name_string, utf8name, length); tpl->SetClassName(name_string); size_t static_property_count = 0; diff --git a/src/node_api.h b/src/node_api.h index e1a40983a9907d..82f2e1900e2fe4 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -109,7 +109,9 @@ napi_get_last_error_info(napi_env env, const napi_extended_error_info** result); NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location, - const char* message); + size_t location_len, + const char* message, + size_t message_len); // Getters for defined singletons NAPI_EXTERN napi_status napi_get_undefined(napi_env env, napi_value* result); @@ -154,6 +156,7 @@ NAPI_EXTERN napi_status napi_create_symbol(napi_env env, napi_value* result); NAPI_EXTERN napi_status napi_create_function(napi_env env, const char* utf8name, + size_t length, napi_callback cb, void* data, napi_value* result); @@ -336,6 +339,7 @@ NAPI_EXTERN napi_status napi_get_new_target(napi_env env, NAPI_EXTERN napi_status napi_define_class(napi_env env, const char* utf8name, + size_t length, napi_callback constructor, void* data, size_t property_count, diff --git a/test/addons-napi/4_object_factory/binding.c b/test/addons-napi/4_object_factory/binding.c index fe509c02ad13b6..6bdca80ec365be 100644 --- a/test/addons-napi/4_object_factory/binding.c +++ b/test/addons-napi/4_object_factory/binding.c @@ -16,7 +16,7 @@ napi_value CreateObject(napi_env env, napi_callback_info info) { napi_value Init(napi_env env, napi_value exports) { NAPI_CALL(env, - napi_create_function(env, "exports", CreateObject, NULL, &exports)); + napi_create_function(env, "exports", -1, CreateObject, NULL, &exports)); return exports; } diff --git a/test/addons-napi/5_function_factory/binding.c b/test/addons-napi/5_function_factory/binding.c index a2e03dd9bdd6b5..6b2b6f8f0ebe81 100644 --- a/test/addons-napi/5_function_factory/binding.c +++ b/test/addons-napi/5_function_factory/binding.c @@ -4,21 +4,19 @@ napi_value MyFunction(napi_env env, napi_callback_info info) { napi_value str; NAPI_CALL(env, napi_create_string_utf8(env, "hello world", -1, &str)); - return str; } napi_value CreateFunction(napi_env env, napi_callback_info info) { napi_value fn; NAPI_CALL(env, - napi_create_function(env, "theFunction", MyFunction, NULL, &fn)); - + napi_create_function(env, "theFunction", -1, MyFunction, NULL, &fn)); return fn; } napi_value Init(napi_env env, napi_value exports) { NAPI_CALL(env, - napi_create_function(env, "exports", CreateFunction, NULL, &exports)); + napi_create_function(env, "exports", -1, CreateFunction, NULL, &exports)); return exports; } diff --git a/test/addons-napi/6_object_wrap/myobject.cc b/test/addons-napi/6_object_wrap/myobject.cc index 90815253add651..aca91877d3a2ae 100644 --- a/test/addons-napi/6_object_wrap/myobject.cc +++ b/test/addons-napi/6_object_wrap/myobject.cc @@ -22,8 +22,8 @@ void MyObject::Init(napi_env env, napi_value exports) { }; napi_value cons; - NAPI_CALL_RETURN_VOID(env, - napi_define_class(env, "MyObject", New, nullptr, 3, properties, &cons)); + NAPI_CALL_RETURN_VOID(env, napi_define_class( + env, "MyObject", -1, New, nullptr, 3, properties, &cons)); NAPI_CALL_RETURN_VOID(env, napi_create_reference(env, cons, 1, &constructor)); diff --git a/test/addons-napi/7_factory_wrap/binding.cc b/test/addons-napi/7_factory_wrap/binding.cc index 4d02247f85b924..9d8f23c561d77a 100644 --- a/test/addons-napi/7_factory_wrap/binding.cc +++ b/test/addons-napi/7_factory_wrap/binding.cc @@ -16,7 +16,7 @@ napi_value Init(napi_env env, napi_value exports) { NAPI_CALL(env, MyObject::Init(env)); NAPI_CALL(env, - napi_create_function(env, "exports", CreateObject, NULL, &exports)); + napi_create_function(env, "exports", -1, CreateObject, NULL, &exports)); return exports; } diff --git a/test/addons-napi/7_factory_wrap/myobject.cc b/test/addons-napi/7_factory_wrap/myobject.cc index c6d538a7cefbc6..4e1d79c1febc17 100644 --- a/test/addons-napi/7_factory_wrap/myobject.cc +++ b/test/addons-napi/7_factory_wrap/myobject.cc @@ -21,8 +21,8 @@ napi_status MyObject::Init(napi_env env) { }; napi_value cons; - status = - napi_define_class(env, "MyObject", New, nullptr, 1, properties, &cons); + status = napi_define_class( + env, "MyObject", -1, New, nullptr, 1, properties, &cons); if (status != napi_ok) return status; status = napi_create_reference(env, cons, 1, &constructor); diff --git a/test/addons-napi/8_passing_wrapped/myobject.cc b/test/addons-napi/8_passing_wrapped/myobject.cc index 0c24d7696e66ec..19cc7dd2a29493 100644 --- a/test/addons-napi/8_passing_wrapped/myobject.cc +++ b/test/addons-napi/8_passing_wrapped/myobject.cc @@ -17,7 +17,8 @@ napi_status MyObject::Init(napi_env env) { napi_status status; napi_value cons; - status = napi_define_class(env, "MyObject", New, nullptr, 0, nullptr, &cons); + status = napi_define_class( + env, "MyObject", -1, New, nullptr, 0, nullptr, &cons); if (status != napi_ok) return status; status = napi_create_reference(env, cons, 1, &constructor); diff --git a/test/addons-napi/test_constructor/binding.gyp b/test/addons-napi/test_constructor/binding.gyp index 55140e7c379f02..1945a9fd5a711e 100644 --- a/test/addons-napi/test_constructor/binding.gyp +++ b/test/addons-napi/test_constructor/binding.gyp @@ -3,6 +3,10 @@ { "target_name": "test_constructor", "sources": [ "test_constructor.c" ] + }, + { + "target_name": "test_constructor_name", + "sources": [ "test_constructor_name.c" ] } ] } diff --git a/test/addons-napi/test_constructor/test2.js b/test/addons-napi/test_constructor/test2.js new file mode 100644 index 00000000000000..64c03cbc684ac3 --- /dev/null +++ b/test/addons-napi/test_constructor/test2.js @@ -0,0 +1,8 @@ +'use strict'; +const common = require('../../common'); +const assert = require('assert'); + +// Testing api calls for a constructor that defines properties +const TestConstructor = + require(`./build/${common.buildType}/test_constructor_name`); +assert.strictEqual(TestConstructor.name, 'MyObject'); diff --git a/test/addons-napi/test_constructor/test_constructor.c b/test/addons-napi/test_constructor/test_constructor.c index 437c32e12517fc..8b5b4e53ea7bc0 100644 --- a/test/addons-napi/test_constructor/test_constructor.c +++ b/test/addons-napi/test_constructor/test_constructor.c @@ -77,7 +77,7 @@ napi_value Init(napi_env env, napi_value exports) { }; napi_value cons; - NAPI_CALL(env, napi_define_class(env, "MyObject", New, + NAPI_CALL(env, napi_define_class(env, "MyObject", -1, New, NULL, sizeof(properties)/sizeof(*properties), properties, &cons)); NAPI_CALL(env, napi_create_reference(env, cons, 1, &constructor_)); diff --git a/test/addons-napi/test_constructor/test_constructor_name.c b/test/addons-napi/test_constructor/test_constructor_name.c new file mode 100644 index 00000000000000..b178e80f49f489 --- /dev/null +++ b/test/addons-napi/test_constructor/test_constructor_name.c @@ -0,0 +1,23 @@ +#include +#include "../common.h" + +napi_ref constructor_; + +napi_value New(napi_env env, napi_callback_info info) { + napi_value _this; + NAPI_CALL(env, napi_get_cb_info(env, info, NULL, NULL, &_this, NULL)); + + return _this; +} + +napi_value Init(napi_env env, napi_value exports) { + napi_value cons; + NAPI_CALL(env, napi_define_class( + env, "MyObject_Extra", 8, New, NULL, 0, NULL, &cons)); + + NAPI_CALL(env, + napi_create_reference(env, cons, 1, &constructor_)); + return cons; +} + +NAPI_MODULE(addon, Init) diff --git a/test/addons-napi/test_env_sharing/compare_env.c b/test/addons-napi/test_env_sharing/compare_env.c index df200f2a96f986..39adfb9b713ab3 100644 --- a/test/addons-napi/test_env_sharing/compare_env.c +++ b/test/addons-napi/test_env_sharing/compare_env.c @@ -15,7 +15,7 @@ napi_value compare(napi_env env, napi_callback_info info) { } napi_value Init(napi_env env, napi_value exports) { - NAPI_CALL(env, napi_create_function(env, "exports", compare, NULL, &exports)); + NAPI_CALL(env, napi_create_function(env, "exports", -1, compare, NULL, &exports)); return exports; } diff --git a/test/addons-napi/test_fatal/test2.js b/test/addons-napi/test_fatal/test2.js new file mode 100644 index 00000000000000..b9bde8f13016cc --- /dev/null +++ b/test/addons-napi/test_fatal/test2.js @@ -0,0 +1,18 @@ +'use strict'; +const common = require('../../common'); +const assert = require('assert'); +const child_process = require('child_process'); +const test_fatal = require(`./build/${common.buildType}/test_fatal`); + +// Test in a child process because the test code will trigger a fatal error +// that crashes the process. +if (process.argv[2] === 'child') { + test_fatal.TestStringLength(); + return; +} + +const p = child_process.spawnSync( + process.execPath, [ '--napi-modules', __filename, 'child' ]); +assert.ifError(p.error); +assert.ok(p.stderr.toString().includes( + 'FATAL ERROR: test_fatal::Test fatal message')); diff --git a/test/addons-napi/test_fatal/test_fatal.c b/test/addons-napi/test_fatal/test_fatal.c index f6cb2184a6e8e2..4159fa4b47f567 100644 --- a/test/addons-napi/test_fatal/test_fatal.c +++ b/test/addons-napi/test_fatal/test_fatal.c @@ -2,13 +2,19 @@ #include "../common.h" napi_value Test(napi_env env, napi_callback_info info) { - napi_fatal_error("test_fatal::Test", "fatal message"); + napi_fatal_error("test_fatal::Test", -1, "fatal message", -1); + return NULL; +} + +napi_value TestStringLength(napi_env env, napi_callback_info info) { + napi_fatal_error("test_fatal::TestStringLength", 16, "fatal message", 13); return NULL; } napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor properties[] = { DECLARE_NAPI_PROPERTY("Test", Test), + DECLARE_NAPI_PROPERTY("TestStringLength", TestStringLength), }; NAPI_CALL(env, napi_define_properties( diff --git a/test/addons-napi/test_function/test.js b/test/addons-napi/test_function/test.js index bdb9133adf9346..752e9965b23039 100644 --- a/test/addons-napi/test_function/test.js +++ b/test/addons-napi/test_function/test.js @@ -9,20 +9,23 @@ const test_function = require(`./build/${common.buildType}/test_function`); function func1() { return 1; } -assert.strictEqual(test_function.Test(func1), 1); +assert.strictEqual(test_function.TestCall(func1), 1); function func2() { console.log('hello world!'); return null; } -assert.strictEqual(test_function.Test(func2), null); +assert.strictEqual(test_function.TestCall(func2), null); function func3(input) { return input + 1; } -assert.strictEqual(test_function.Test(func3, 1), 2); +assert.strictEqual(test_function.TestCall(func3, 1), 2); function func4(input) { return func3(input); } -assert.strictEqual(test_function.Test(func4, 1), 2); +assert.strictEqual(test_function.TestCall(func4, 1), 2); + +assert.strictEqual(test_function.TestName.name, 'Name'); +assert.strictEqual(test_function.TestNameShort.name, 'Name_'); diff --git a/test/addons-napi/test_function/test_function.c b/test/addons-napi/test_function/test_function.c index 2cb25db5beaf03..77495d9b8a3a46 100644 --- a/test/addons-napi/test_function/test_function.c +++ b/test/addons-napi/test_function/test_function.c @@ -1,7 +1,7 @@ #include #include "../common.h" -napi_value Test(napi_env env, napi_callback_info info) { +napi_value TestCallFunction(napi_env env, napi_callback_info info) { size_t argc = 10; napi_value args[10]; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); @@ -26,10 +26,25 @@ napi_value Test(napi_env env, napi_callback_info info) { return result; } +void TestFunctionName(napi_env env, napi_callback_info info) {} + napi_value Init(napi_env env, napi_value exports) { - napi_value fn; - NAPI_CALL(env, napi_create_function(env, NULL, Test, NULL, &fn)); - NAPI_CALL(env, napi_set_named_property(env, exports, "Test", fn)); + napi_value fn1; + NAPI_CALL(env, napi_create_function( + env, NULL, -1, TestCallFunction, NULL, &fn1)); + + napi_value fn2; + NAPI_CALL(env, napi_create_function( + env, "Name", -1, TestFunctionName, NULL, &fn2)); + + napi_value fn3; + NAPI_CALL(env, napi_create_function( + env, "Name_extra", 5, TestFunctionName, NULL, &fn3)); + + NAPI_CALL(env, napi_set_named_property(env, exports, "TestCall", fn1)); + NAPI_CALL(env, napi_set_named_property(env, exports, "TestName", fn2)); + NAPI_CALL(env, napi_set_named_property(env, exports, "TestNameShort", fn3)); + return exports; } diff --git a/test/addons-napi/test_make_callback/binding.cc b/test/addons-napi/test_make_callback/binding.cc index 9032e8b2f3f301..18bf46764be40c 100644 --- a/test/addons-napi/test_make_callback/binding.cc +++ b/test/addons-napi/test_make_callback/binding.cc @@ -45,7 +45,7 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) { napi_value Init(napi_env env, napi_value exports) { napi_value fn; - NAPI_CALL(env, napi_create_function(env, NULL, MakeCallback, NULL, &fn)); + NAPI_CALL(env, napi_create_function(env, NULL, -1, MakeCallback, NULL, &fn)); NAPI_CALL(env, napi_set_named_property(env, exports, "makeCallback", fn)); return exports; } diff --git a/test/addons-napi/test_make_callback_recurse/binding.cc b/test/addons-napi/test_make_callback_recurse/binding.cc index d0ee1ddb4998e6..ea823294352490 100644 --- a/test/addons-napi/test_make_callback_recurse/binding.cc +++ b/test/addons-napi/test_make_callback_recurse/binding.cc @@ -20,7 +20,7 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) { napi_value Init(napi_env env, napi_value exports) { napi_value fn; - NAPI_CALL(env, napi_create_function(env, NULL, MakeCallback, NULL, &fn)); + NAPI_CALL(env, napi_create_function(env, NULL, -1, MakeCallback, NULL, &fn)); NAPI_CALL(env, napi_set_named_property(env, exports, "makeCallback", fn)); return exports; } From a61dd4b6300ff2a3c2d964d2d3388731789f0fb4 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Fri, 18 Aug 2017 13:30:05 +0300 Subject: [PATCH 108/183] n-api: remove n-api module loading flag Remove the command line flag that was needed for N-API module loading. Re: https://github.com/nodejs/vm/issues/9 PR-URL: https://github.com/nodejs/node/pull/14902 Reviewed-By: Refael Ackermann Reviewed-By: Colin Ihrig Reviewed-By: Anna Henningsen Reviewed-By: Matteo Collina Reviewed-By: Michael Dawson Reviewed-By: James M Snell Reviewed-By: Hitesh Kanwathirtha --- doc/api/cli.md | 9 ----- doc/api/n-api.md | 8 ---- src/env-inl.h | 1 + src/env.cc | 6 +++ src/env.h | 3 ++ src/node.cc | 38 +++++++------------ src/node_api.cc | 20 +--------- test/addons-napi/test_async/test.js | 2 +- test/addons-napi/test_fatal/test.js | 4 +- .../addons-napi/test_function/test_function.c | 4 +- test/addons-napi/test_warning/binding.gyp | 12 ++++++ test/addons-napi/test_warning/test.js | 18 +++++++++ test/addons-napi/test_warning/test_warning.c | 11 ++++++ test/addons-napi/test_warning/test_warning2.c | 11 ++++++ test/addons-napi/testcfg.py | 2 +- 15 files changed, 83 insertions(+), 66 deletions(-) create mode 100644 test/addons-napi/test_warning/binding.gyp create mode 100644 test/addons-napi/test_warning/test.js create mode 100644 test/addons-napi/test_warning/test_warning.c create mode 100644 test/addons-napi/test_warning/test_warning2.c diff --git a/doc/api/cli.md b/doc/api/cli.md index 5bcb6f3b4d0107..84876ea181d0a8 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -119,14 +119,6 @@ added: v6.0.0 Silence all process warnings (including deprecations). -### `--napi-modules` - - -Enable loading native modules compiled with the ABI-stable Node.js API (N-API) -(experimental). - ### `--trace-warnings` +```C +NAPI_EXTERN napi_status napi_get_uv_event_loop(napi_env env, + uv_loop_t** loop); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[out] loop`: The current libuv loop instance. + [Promises]: #n_api_promises [Simple Asynchronous Operations]: #n_api_simple_asynchronous_operations [Custom Asynchronous Operations]: #n_api_custom_asynchronous_operations diff --git a/src/node_api.cc b/src/node_api.cc index f1c13214b3ed96..10c8093f5a280b 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -22,7 +22,7 @@ #include "node_api_backport.h" #include "util.h" -#define NAPI_VERSION 1 +#define NAPI_VERSION 2 static napi_status napi_set_last_error(napi_env env, napi_status error_code, @@ -31,8 +31,11 @@ napi_status napi_set_last_error(napi_env env, napi_status error_code, static napi_status napi_clear_last_error(napi_env env); struct napi_env__ { - explicit napi_env__(v8::Isolate* _isolate): isolate(_isolate), - has_instance_available(true), last_error() {} + explicit napi_env__(v8::Isolate* _isolate, uv_loop_t *_loop): + isolate(_isolate), + has_instance_available(true), + last_error(), + loop(_loop) {} ~napi_env__() { last_exception.Reset(); has_instance.Reset(); @@ -49,6 +52,7 @@ struct napi_env__ { bool has_instance_available; napi_extended_error_info last_error; int open_handle_scopes = 0; + uv_loop_t* loop = nullptr; }; #define ENV_OBJECT_TEMPLATE(env, prefix, destination, field_count) \ @@ -780,7 +784,7 @@ napi_env GetEnv(v8::Local context) { if (value->IsExternal()) { result = static_cast(value.As()->Value()); } else { - result = new napi_env__(isolate); + result = new napi_env__(isolate, node::GetCurrentEventLoop(isolate)); auto external = v8::External::New(isolate, result); // We must also stop hard if the result of assigning the env to the global @@ -3494,15 +3498,22 @@ napi_status napi_delete_async_work(napi_env env, napi_async_work work) { return napi_clear_last_error(env); } +napi_status napi_get_uv_event_loop(napi_env env, uv_loop_t** loop) { + CHECK_ENV(env); + CHECK_ARG(env, loop); + *loop = env->loop; + return napi_clear_last_error(env); +} + napi_status napi_queue_async_work(napi_env env, napi_async_work work) { CHECK_ENV(env); CHECK_ARG(env, work); - // Consider: Encapsulate the uv_loop_t into an opaque pointer parameter. - // Currently the environment event loop is the same as the UV default loop. - // Someday (if node ever supports multiple isolates), it may be better to get - // the loop from node::Environment::GetCurrent(env->isolate)->event_loop(); - uv_loop_t* event_loop = uv_default_loop(); + napi_status status; + uv_loop_t* event_loop = nullptr; + status = napi_get_uv_event_loop(env, &event_loop); + if (status != napi_ok) + return napi_set_last_error(env, status); uvimpl::Work* w = reinterpret_cast(work); diff --git a/src/node_api.h b/src/node_api.h index a3a07a64673366..8e5eef8a47728f 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -14,6 +14,8 @@ #include #include "node_api_types.h" +struct uv_loop_s; // Forward declaration. + #ifdef _WIN32 #ifdef BUILDING_NODE_EXTENSION #ifdef EXTERNAL_NAPI @@ -581,6 +583,10 @@ NAPI_EXTERN napi_status napi_run_script(napi_env env, napi_value script, napi_value* result); +// Return the current libuv event loop for a given environment +NAPI_EXTERN napi_status napi_get_uv_event_loop(napi_env env, + struct uv_loop_s** loop); + EXTERN_C_END #endif // SRC_NODE_API_H_ diff --git a/src/node_api_backport.cc b/src/node_api_backport.cc index 655b61da5cf094..52a0653bfcc591 100644 --- a/src/node_api_backport.cc +++ b/src/node_api_backport.cc @@ -71,6 +71,14 @@ CallbackScope::~CallbackScope() { env->tick_callback_function()->Call(env->process_object(), 0, nullptr); } +uv_loop_t *GetCurrentEventLoop(v8::Isolate *isolate) { + HandleScope handle_scope(isolate); + auto context = isolate->GetCurrentContext(); + if (context.IsEmpty()) + return nullptr; + return Environment::GetCurrent(context)->event_loop(); +} + AsyncResource::AsyncResource(v8::Isolate* _isolate, v8::Local _object, char* name) : isolate(_isolate) { diff --git a/src/node_api_backport.h b/src/node_api_backport.h index 89ae33d0861529..968169ef3bd05f 100644 --- a/src/node_api_backport.h +++ b/src/node_api_backport.h @@ -31,6 +31,8 @@ class CallbackScope { Environment::AsyncCallbackScope callback_scope; }; +uv_loop_t *GetCurrentEventLoop(v8::Isolate *isolate); + NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate, v8::Local resource, v8::Local name, diff --git a/test/addons-napi/test_general/test.js b/test/addons-napi/test_general/test.js index 51ce9563a7196e..10b85ef727bb20 100644 --- a/test/addons-napi/test_general/test.js +++ b/test/addons-napi/test_general/test.js @@ -34,7 +34,7 @@ assert.ok(test_general.testGetPrototype(baseObject) !== // test version management funcitons // expected version is currently 1 -assert.strictEqual(test_general.testGetVersion(), 1); +assert.strictEqual(test_general.testGetVersion(), 2); const [ major, minor, patch, release ] = test_general.testGetNodeVersion(); assert.strictEqual(process.version.split('-')[0], From c04169deac5769c01aeeb75265b52421ca5bdee6 Mon Sep 17 00:00:00 2001 From: neta Date: Mon, 20 Nov 2017 21:41:56 +0200 Subject: [PATCH 135/183] src: add napi_handle_scope_mismatch to msg list PR-URL: https://github.com/nodejs/node/pull/17161 Reviewed-By: Timothy Gu Reviewed-By: Benjamin Gruenbaum Reviewed-By: Refael Ackermann Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Michael Dawson Reviewed-By: Alexey Orlenko Reviewed-By: Franziska Hinkelmann --- src/node_api.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index 10c8093f5a280b..9f6d407cfd3ec1 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -909,7 +909,8 @@ const char* error_messages[] = {nullptr, "Unknown failure", "An exception is pending", "The async work item was cancelled", - "napi_escape_handle already called on scope"}; + "napi_escape_handle already called on scope", + "Invalid handle scope usage"}; static inline napi_status napi_clear_last_error(napi_env env) { env->last_error.error_code = napi_ok; @@ -940,9 +941,9 @@ napi_status napi_get_last_error_info(napi_env env, // We don't have a napi_status_last as this would result in an ABI // change each time a message was added. static_assert( - node::arraysize(error_messages) == napi_escape_called_twice + 1, + node::arraysize(error_messages) == napi_handle_scope_mismatch + 1, "Count of error messages must match count of error values"); - CHECK_LE(env->last_error.error_code, napi_escape_called_twice); + CHECK_LE(env->last_error.error_code, napi_handle_scope_mismatch); // Wait until someone requests the last error information to fetch the error // message string From 32dd8f15fba677b626e68cad22dd0b8f0a7dddc5 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 18 Nov 2017 20:01:57 -0800 Subject: [PATCH 136/183] doc: use "JavaScript" instead of "Javascript" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/17163 Reviewed-By: Michaël Zasso Reviewed-By: Vse Mozhet Byt Reviewed-By: Benjamin Gruenbaum Reviewed-By: Alexey Orlenko Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Gireesh Punathil --- README.md | 4 ++-- doc/api/n-api.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ad3d838dd58076..cb1a2b7a98bb32 100644 --- a/README.md +++ b/README.md @@ -194,8 +194,8 @@ us a report nonetheless. - [#14519](https://github.com/nodejs/node/issues/14519): _Internal domain function can be used to cause segfaults_. Causing program termination using - either the public Javascript APIs or the private bindings layer APIs requires - the ability to execute arbitrary Javascript code, which is already the highest + either the public JavaScript APIs or the private bindings layer APIs requires + the ability to execute arbitrary JavaScript code, which is already the highest level of privilege possible. - [#12141](https://github.com/nodejs/node/pull/12141): _buffer: zero fill diff --git a/doc/api/n-api.md b/doc/api/n-api.md index b31ea3a2688d43..3c2ce8d14eb080 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -202,7 +202,7 @@ typedef void (*napi_async_complete_callback)(napi_env env, ``` ## Error Handling -N-API uses both return values and Javascript exceptions for error handling. +N-API uses both return values and JavaScript exceptions for error handling. The following sections explain the approach for each case. ### Return values @@ -2861,7 +2861,7 @@ Returns `napi_ok` if the API succeeded. This API allows an add-on author to create a function object in native code. This is the primary mechanism to allow calling *into* the add-on's native code -*from* Javascript. +*from* JavaScript. **Note:** The newly created function is not automatically visible from script after this call. Instead, a property must be explicitly set on any From 45a3e6b4d0f27b2e8b4a0470cbd6e3f7261eb49c Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Thu, 23 Nov 2017 15:25:14 +0100 Subject: [PATCH 137/183] n-api: use nullptr instead of NULL in node_api.cc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit changes two checks which use NULL to use nullptr. I'm not very familiar with N-API but wanted to bring this up in case it was something that was overlooked. PR-URL: https://github.com/nodejs/node/pull/17276 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Benjamin Gruenbaum Reviewed-By: Timothy Gu Reviewed-By: Ali Ijaz Sheikh Reviewed-By: Michael Dawson Reviewed-By: Lance Ball Reviewed-By: Alexey Orlenko Reviewed-By: James M Snell Reviewed-By: MichaëZasso --- src/node_api.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index 9f6d407cfd3ec1..adc1c9c1b48e7d 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -1228,7 +1228,7 @@ napi_status napi_delete_property(napi_env env, v8::Maybe delete_maybe = obj->Delete(context, k); CHECK_MAYBE_NOTHING(env, delete_maybe, napi_generic_failure); - if (result != NULL) + if (result != nullptr) *result = delete_maybe.FromMaybe(false); return GET_RETURN_STATUS(env); @@ -1406,7 +1406,7 @@ napi_status napi_delete_element(napi_env env, v8::Maybe delete_maybe = obj->Delete(context, index); CHECK_MAYBE_NOTHING(env, delete_maybe, napi_generic_failure); - if (result != NULL) + if (result != nullptr) *result = delete_maybe.FromMaybe(false); return GET_RETURN_STATUS(env); From d9dc130a5b4fba425e386289a8d0196799c53e79 Mon Sep 17 00:00:00 2001 From: Franziska Hinkelmann Date: Mon, 4 Dec 2017 18:45:22 +0100 Subject: [PATCH 138/183] doc: update example in module registration Update return type of `Init` function in documentation to match `napi_addon_register_func` signature. Return type used to be `void`, now it is `napi_value`. PR-URL: https://github.com/nodejs/node/pull/17424 Reviewed-By: Michael Dawson Reviewed-By: James M Snell --- doc/api/n-api.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 3c2ce8d14eb080..f58f96742eecb3 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -2877,15 +2877,17 @@ napi_value SayHello(napi_env env, napi_callback_info info) { return nullptr; } -void Init(napi_env env, napi_value exports, napi_value module, void* priv) { +napi_value Init(napi_env env, napi_value exports) { napi_status status; napi_value fn; - status = napi_create_function(env, NULL, SayHello, NULL, &fn); - if (status != napi_ok) return; + status = napi_create_function(env, nullptr, 0, SayHello, nullptr, &fn); + if (status != napi_ok) return nullptr; status = napi_set_named_property(env, exports, "sayHello", fn); - if (status != napi_ok) return; + if (status != napi_ok) return nullptr; + + return exports; } NAPI_MODULE(addon, Init) From 3f4ac092d6428470d5f6f4cc41898ffee5ad3292 Mon Sep 17 00:00:00 2001 From: Leko Date: Mon, 4 Dec 2017 16:07:45 +0900 Subject: [PATCH 139/183] test: replace assert.throws with common.expectsError PR-URL: https://github.com/nodejs/node/pull/17445 Reviewed-By: Luigi Pinca Reviewed-By: Colin Ihrig Reviewed-By: Jon Moss Reviewed-By: James M Snell --- test/addons-napi/test_error/test.js | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/test/addons-napi/test_error/test.js b/test/addons-napi/test_error/test.js index 245e734e7503f0..bc2b7a8db75d3e 100644 --- a/test/addons-napi/test_error/test.js +++ b/test/addons-napi/test_error/test.js @@ -60,29 +60,26 @@ assert.throws(() => { test_error.throwTypeError(); }, /^TypeError: type error$/); -assert.throws( +common.expectsError( () => test_error.throwErrorCode(), - common.expectsError({ + { code: 'ERR_TEST_CODE', message: 'Error [error]' - }) -); + }); -assert.throws( +common.expectsError( () => test_error.throwRangeErrorCode(), - common.expectsError({ + { code: 'ERR_TEST_CODE', message: 'RangeError [range error]' - }) -); + }); -assert.throws( +common.expectsError( () => test_error.throwTypeErrorCode(), - common.expectsError({ + { code: 'ERR_TEST_CODE', message: 'TypeError [type error]' - }) -); + }); let error = test_error.createError(); assert.ok(error instanceof Error, 'expected error to be an instance of Error'); From a005c304c4c50314bff74f7b1d70e2dac6790169 Mon Sep 17 00:00:00 2001 From: babygoat Date: Thu, 23 Nov 2017 19:44:20 +0800 Subject: [PATCH 140/183] test: add unhandled rejection guard Add an unhandled rejection function in addons-napi/test_promise/test.js. Also, add a rejection handler to catch the unhandled rejection after introducing the guard and test the reason code. PR-URL: https://github.com/nodejs/node/pull/17275 Reviewed-By: Anna Henningsen Reviewed-By: Ruben Bridgewater --- test/addons-napi/test_promise/test.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/addons-napi/test_promise/test.js b/test/addons-napi/test_promise/test.js index b43ecd87363e44..6dc51b3fa558a2 100644 --- a/test/addons-napi/test_promise/test.js +++ b/test/addons-napi/test_promise/test.js @@ -7,6 +7,8 @@ const common = require('../../common'); const assert = require('assert'); const test_promise = require(`./build/${common.buildType}/test_promise`); +common.crashOnUnhandledRejection(); + // A resolution { const expected_result = 42; @@ -44,7 +46,14 @@ const test_promise = require(`./build/${common.buildType}/test_promise`); } assert.strictEqual(test_promise.isPromise(test_promise.createPromise()), true); -assert.strictEqual(test_promise.isPromise(Promise.reject(-1)), true); + +const rejectPromise = Promise.reject(-1); +const expected_reason = -1; +assert.strictEqual(test_promise.isPromise(rejectPromise), true); +rejectPromise.catch((reason) => { + assert.strictEqual(reason, expected_reason); +}); + assert.strictEqual(test_promise.isPromise(2.4), false); assert.strictEqual(test_promise.isPromise('I promise!'), false); assert.strictEqual(test_promise.isPromise(undefined), false); From 2515f516d7fc0e63143662b9418a6e0436b84f67 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Tue, 12 Dec 2017 21:20:47 -0800 Subject: [PATCH 141/183] test: remove literals that obscure assert messages Remove string literals as messages to `assert.strictEqual()`. They can be misleading here (where perhaps the reason an assertino failed isn't that the deleter wasn't called but rather was called too many times. PR-URL: https://github.com/nodejs/node/pull/17642 Reviewed-By: Anna Henningsen Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig Reviewed-By: Luigi Pinca Reviewed-By: Jon Moss Reviewed-By: James M Snell Reviewed-By: Anatoli Papirovski Reviewed-By: Michael Dawson --- test/addons-napi/test_buffer/test.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/addons-napi/test_buffer/test.js b/test/addons-napi/test_buffer/test.js index 713966775df18b..740b0474a79c60 100644 --- a/test/addons-napi/test_buffer/test.js +++ b/test/addons-napi/test_buffer/test.js @@ -9,14 +9,13 @@ assert.strictEqual(binding.newBuffer().toString(), binding.theText); assert.strictEqual(binding.newExternalBuffer().toString(), binding.theText); console.log('gc1'); global.gc(); -assert.strictEqual(binding.getDeleterCallCount(), 1, 'deleter was not called'); +assert.strictEqual(binding.getDeleterCallCount(), 1); assert.strictEqual(binding.copyBuffer().toString(), binding.theText); let buffer = binding.staticBuffer(); -assert.strictEqual(binding.bufferHasInstance(buffer), true, - 'buffer type checking fails'); +assert.strictEqual(binding.bufferHasInstance(buffer), true); assert.strictEqual(binding.bufferInfo(buffer), true); buffer = null; global.gc(); console.log('gc2'); -assert.strictEqual(binding.getDeleterCallCount(), 2, 'deleter was not called'); +assert.strictEqual(binding.getDeleterCallCount(), 2); From 825468fa7cbc382472a25dca533c5140cb4f2a36 Mon Sep 17 00:00:00 2001 From: alnyan Date: Sun, 17 Dec 2017 12:22:46 +0200 Subject: [PATCH 142/183] n-api: fix memory leak in napi_async_destroy() PR-URL: https://github.com/nodejs/node/pull/17714 Reviewed-By: Alexey Orlenko Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Timothy Gu Reviewed-By: James M Snell --- src/node_api.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/node_api.cc b/src/node_api.cc index adc1c9c1b48e7d..b3334c486edc14 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -2864,6 +2864,8 @@ napi_status napi_async_destroy(napi_env env, reinterpret_cast(async_context); node::EmitAsyncDestroy(isolate, *node_async_context); + delete node_async_context; + return napi_clear_last_error(env); } From 6d5b622ecda6888ae8620462831be586228d3750 Mon Sep 17 00:00:00 2001 From: Nicholas Drane Date: Thu, 21 Dec 2017 16:48:41 -0600 Subject: [PATCH 143/183] test: remove ambiguous error messages from test_error assert.strictEqual accepts 3 arguments, the last of which allows for user-specified error message to be thrown when the assertion fails. Unfortunately, this error message is less helpful than the default when it is vague. This commit removes vague, user-specified error messages, instead relying on clearer, default error messages. PR-URL: https://github.com/nodejs/node/pull/17812 Reviewed-By: Colin Ihrig Reviewed-By: Jon Moss Reviewed-By: Rich Trott Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Luigi Pinca --- test/addons-napi/test_error/test.js | 46 ++++++++--------------------- 1 file changed, 12 insertions(+), 34 deletions(-) diff --git a/test/addons-napi/test_error/test.js b/test/addons-napi/test_error/test.js index bc2b7a8db75d3e..d5c92cb4c3cd2e 100644 --- a/test/addons-napi/test_error/test.js +++ b/test/addons-napi/test_error/test.js @@ -83,56 +83,34 @@ common.expectsError( let error = test_error.createError(); assert.ok(error instanceof Error, 'expected error to be an instance of Error'); -assert.strictEqual(error.message, 'error', 'expected message to be "error"'); +assert.strictEqual(error.message, 'error'); error = test_error.createRangeError(); assert.ok(error instanceof RangeError, 'expected error to be an instance of RangeError'); -assert.strictEqual(error.message, - 'range error', - 'expected message to be "range error"'); +assert.strictEqual(error.message, 'range error'); error = test_error.createTypeError(); assert.ok(error instanceof TypeError, 'expected error to be an instance of TypeError'); -assert.strictEqual(error.message, - 'type error', - 'expected message to be "type error"'); +assert.strictEqual(error.message, 'type error'); error = test_error.createErrorCode(); assert.ok(error instanceof Error, 'expected error to be an instance of Error'); -assert.strictEqual(error.code, - 'ERR_TEST_CODE', - 'expected code to be "ERR_TEST_CODE"'); -assert.strictEqual(error.message, - 'Error [error]', - 'expected message to be "Error [error]"'); -assert.strictEqual(error.name, - 'Error [ERR_TEST_CODE]', - 'expected name to be "Error [ERR_TEST_CODE]"'); +assert.strictEqual(error.code, 'ERR_TEST_CODE'); +assert.strictEqual(error.message, 'Error [error]'); +assert.strictEqual(error.name, 'Error [ERR_TEST_CODE]'); error = test_error.createRangeErrorCode(); assert.ok(error instanceof RangeError, 'expected error to be an instance of RangeError'); -assert.strictEqual(error.message, - 'RangeError [range error]', - 'expected message to be "RangeError [range error]"'); -assert.strictEqual(error.code, - 'ERR_TEST_CODE', - 'expected code to be "ERR_TEST_CODE"'); -assert.strictEqual(error.name, - 'RangeError [ERR_TEST_CODE]', - 'expected name to be "RangeError[ERR_TEST_CODE]"'); +assert.strictEqual(error.message, 'RangeError [range error]'); +assert.strictEqual(error.code, 'ERR_TEST_CODE'); +assert.strictEqual(error.name, 'RangeError [ERR_TEST_CODE]'); error = test_error.createTypeErrorCode(); assert.ok(error instanceof TypeError, 'expected error to be an instance of TypeError'); -assert.strictEqual(error.message, - 'TypeError [type error]', - 'expected message to be "TypeError [type error]"'); -assert.strictEqual(error.code, - 'ERR_TEST_CODE', - 'expected code to be "ERR_TEST_CODE"'); -assert.strictEqual(error.name, - 'TypeError [ERR_TEST_CODE]', - 'expected name to be "TypeError[ERR_TEST_CODE]"'); +assert.strictEqual(error.message, 'TypeError [type error]'); +assert.strictEqual(error.code, 'ERR_TEST_CODE'); +assert.strictEqual(error.name, 'TypeError [ERR_TEST_CODE]'); From 968d7575ad244e1b87159969de0de93c0a7bbb77 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Fri, 5 Jan 2018 12:19:20 -0500 Subject: [PATCH 144/183] doc: updates examples to use NULL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Examples in the N-API doc used a mix of nullptr and NULL. We should be consistent and because N-API is a 'C' API I believe using NULL is better. This will avoid any potential confusion as to whether N-API can be used with plain C. PR-URL: https://github.com/nodejs/node/pull/18008 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig Reviewed-By: Luigi Pinca Reviewed-By: Tobias Nießen Reviewed-By: Franziska Hinkelmann Reviewed-By: Gireesh Punathil --- doc/api/n-api.md | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index f58f96742eecb3..86bdecbdff0273 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -903,9 +903,9 @@ napi_value Init(napi_env env, napi_value exports) { napi_status status; napi_property_descriptor desc = {"hello", Method, 0, 0, 0, napi_default, 0}; - if (status != napi_ok) return nullptr; + if (status != napi_ok) return NULL; status = napi_define_properties(env, exports, 1, &desc); - if (status != napi_ok) return nullptr; + if (status != napi_ok) return NULL; return exports; } ``` @@ -917,7 +917,7 @@ napi_value Init(napi_env env, napi_value exports) { napi_value method; napi_status status; status = napi_create_function(env, "exports", Method, NULL, &method)); - if (status != napi_ok) return nullptr; + if (status != napi_ok) return NULL; return method; } ``` @@ -930,21 +930,21 @@ For example, to define a class so that new instances can be created napi_value Init(napi_env env, napi_value exports) { napi_status status; napi_property_descriptor properties[] = { - { "value", nullptr, GetValue, SetValue, 0, napi_default, 0 }, + { "value", NULL, GetValue, SetValue, 0, napi_default, 0 }, DECLARE_NAPI_METHOD("plusOne", PlusOne), DECLARE_NAPI_METHOD("multiply", Multiply), }; napi_value cons; status = - napi_define_class(env, "MyObject", New, nullptr, 3, properties, &cons); - if (status != napi_ok) return nullptr; + napi_define_class(env, "MyObject", New, NULL, 3, properties, &cons); + if (status != napi_ok) return NULL; status = napi_create_reference(env, cons, 1, &constructor); - if (status != napi_ok) return nullptr; + if (status != napi_ok) return NULL; status = napi_set_named_property(env, exports, "MyObject", cons); - if (status != napi_ok) return nullptr; + if (status != napi_ok) return NULL; return exports; } @@ -2362,8 +2362,8 @@ if (status != napi_ok) return status; // Set the properties napi_property_descriptor descriptors[] = { - { "foo", nullptr, 0, 0, 0, fooValue, napi_default, 0 }, - { "bar", nullptr, 0, 0, 0, barValue, napi_default, 0 } + { "foo", NULL, 0, 0, 0, fooValue, napi_default, 0 }, + { "bar", NULL, 0, 0, 0, barValue, napi_default, 0 } } status = napi_define_properties(env, obj, @@ -2874,18 +2874,18 @@ object. A sample module might look as follows: ```C napi_value SayHello(napi_env env, napi_callback_info info) { printf("Hello\n"); - return nullptr; + return NULL; } napi_value Init(napi_env env, napi_value exports) { napi_status status; napi_value fn; - status = napi_create_function(env, nullptr, 0, SayHello, nullptr, &fn); - if (status != napi_ok) return nullptr; + status = napi_create_function(env, NULL, 0, SayHello, NULL, &fn); + if (status != napi_ok) return NULL; status = napi_set_named_property(env, exports, "sayHello", fn); - if (status != napi_ok) return nullptr; + if (status != napi_ok) return NULL; return exports; } @@ -2950,7 +2950,7 @@ napi_status napi_get_new_target(napi_env env, Returns `napi_ok` if the API succeeded. This API returns the `new.target` of the constructor call. If the current -callback is not a constructor call, the result is `nullptr`. +callback is not a constructor call, the result is `NULL`. ### *napi_new_instance* @@ -3387,7 +3387,7 @@ napi_status napi_async_init(napi_env env, Returns `napi_ok` if the API succeeded. -### *napi_async_destroy** +### napi_async_destroy @@ -3401,7 +3401,7 @@ napi_status napi_async_destroy(napi_env env, Returns `napi_ok` if the API succeeded. -### *napi_make_callback* +### napi_make_callback @@ -1036,7 +1036,7 @@ JavaScript arrays are described in [Section 22.1](https://tc39.github.io/ecma262/#sec-array-objects) of the ECMAScript Language Specification. -#### *napi_create_array_with_length* +#### napi_create_array_with_length @@ -1065,7 +1065,7 @@ JavaScript arrays are described in [Section 22.1](https://tc39.github.io/ecma262/#sec-array-objects) of the ECMAScript Language Specification. -#### *napi_create_arraybuffer* +#### napi_create_arraybuffer @@ -1097,7 +1097,7 @@ JavaScript ArrayBuffer objects are described in [Section 24.1](https://tc39.github.io/ecma262/#sec-arraybuffer-objects) of the ECMAScript Language Specification. -#### *napi_create_buffer* +#### napi_create_buffer @@ -1118,7 +1118,7 @@ Returns `napi_ok` if the API succeeded. This API allocates a `node::Buffer` object. While this is still a fully-supported data structure, in most cases using a TypedArray will suffice. -#### *napi_create_buffer_copy* +#### napi_create_buffer_copy @@ -1143,7 +1143,7 @@ This API allocates a `node::Buffer` object and initializes it with data copied from the passed-in buffer. While this is still a fully-supported data structure, in most cases using a TypedArray will suffice. -#### *napi_create_external* +#### napi_create_external @@ -1210,7 +1210,7 @@ JavaScript ArrayBuffers are described in [Section 24.1](https://tc39.github.io/ecma262/#sec-arraybuffer-objects) of the ECMAScript Language Specification. -#### *napi_create_external_buffer* +#### napi_create_external_buffer @@ -1241,7 +1241,7 @@ structure, in most cases using a TypedArray will suffice. **Note:** For Node.js >=4 `Buffers` are Uint8Arrays. -#### *napi_create_function* +#### napi_create_function @@ -1274,7 +1274,7 @@ JavaScript Functions are described in [Section 19.2](https://tc39.github.io/ecma262/#sec-function-objects) of the ECMAScript Language Specification. -#### *napi_create_object* +#### napi_create_object @@ -1294,7 +1294,7 @@ The JavaScript Object type is described in [Section 6.1.7](https://tc39.github.io/ecma262/#sec-object-type) of the ECMAScript Language Specification. -#### *napi_create_symbol* +#### napi_create_symbol @@ -1317,7 +1317,7 @@ The JavaScript Symbol type is described in [Section 19.4](https://tc39.github.io/ecma262/#sec-symbol-objects) of the ECMAScript Language Specification. -#### *napi_create_typedarray* +#### napi_create_typedarray @@ -1353,7 +1353,7 @@ JavaScript TypedArray Objects are described in of the ECMAScript Language Specification. -#### *napi_create_dataview* +#### napi_create_dataview @@ -1387,7 +1387,7 @@ JavaScript DataView Objects are described in [Section 24.3][] of the ECMAScript Language Specification. ### Functions to convert from C types to N-API -#### *napi_create_int32* +#### napi_create_int32 @@ -1408,7 +1408,7 @@ The JavaScript Number type is described in [Section 6.1.6](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type) of the ECMAScript Language Specification. -#### *napi_create_uint32* +#### napi_create_uint32 @@ -1429,7 +1429,7 @@ The JavaScript Number type is described in [Section 6.1.6](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type) of the ECMAScript Language Specification. -#### *napi_create_int64* +#### napi_create_int64 @@ -1456,7 +1456,7 @@ outside the range of [`Number.MAX_SAFE_INTEGER`](https://tc39.github.io/ecma262/#sec-number.max_safe_integer) (2^53 - 1) will lose precision. -#### *napi_create_double* +#### napi_create_double @@ -1477,7 +1477,7 @@ The JavaScript Number type is described in [Section 6.1.6](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type) of the ECMAScript Language Specification. -#### *napi_create_string_latin1* +#### napi_create_string_latin1 @@ -1502,7 +1502,7 @@ The JavaScript String type is described in [Section 6.1.4](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type) of the ECMAScript Language Specification. -#### *napi_create_string_utf16* +#### napi_create_string_utf16 @@ -1527,7 +1527,7 @@ The JavaScript String type is described in [Section 6.1.4](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type) of the ECMAScript Language Specification. -#### *napi_create_string_utf8* +#### napi_create_string_utf8 @@ -1553,7 +1553,7 @@ The JavaScript String type is described in of the ECMAScript Language Specification. ### Functions to convert from N-API to C types -#### *napi_get_array_length* +#### napi_get_array_length @@ -1576,7 +1576,7 @@ Array length is described in [Section 22.1.4.1](https://tc39.github.io/ecma262/#sec-properties-of-array-instances-length) of the ECMAScript Language Specification. -#### *napi_get_arraybuffer_info* +#### napi_get_arraybuffer_info @@ -1604,7 +1604,7 @@ which can be used to guarantee control over the lifetime of the ArrayBuffer. It's also safe to use the returned data buffer within the same callback as long as there are no calls to other APIs that might trigger a GC. -#### *napi_get_buffer_info* +#### napi_get_buffer_info @@ -1628,7 +1628,7 @@ and it's length. *Warning*: Use caution while using this API since the underlying data buffer's lifetime is not guaranteed if it's managed by the VM. -#### *napi_get_prototype* +#### napi_get_prototype @@ -1646,7 +1646,7 @@ not the same as the function's `prototype` property). Returns `napi_ok` if the API succeeded. -#### *napi_get_typedarray_info* +#### napi_get_typedarray_info @@ -1678,7 +1678,7 @@ is managed by the VM -#### *napi_get_dataview_info* +#### napi_get_dataview_info @@ -1706,7 +1706,7 @@ Returns `napi_ok` if the API succeeded. This API returns various properties of a DataView. -#### *napi_get_value_bool* +#### napi_get_value_bool @@ -1725,7 +1725,7 @@ passed in it returns `napi_boolean_expected`. This API returns the C boolean primitive equivalent of the given JavaScript Boolean. -#### *napi_get_value_double* +#### napi_get_value_double @@ -1747,7 +1747,7 @@ This API returns the C double primitive equivalent of the given JavaScript Number. -#### *napi_get_value_external* +#### napi_get_value_external @@ -1767,7 +1767,7 @@ passed in it returns `napi_invalid_arg`. This API retrieves the external data pointer that was previously passed to `napi_create_external()`. -#### *napi_get_value_int32* +#### napi_get_value_int32 @@ -1790,7 +1790,7 @@ of the given JavaScript Number. If the number exceeds the range of the bottom 32 bits. This can result in a large positive number becoming a negative number if the value is > 2^31 -1. -#### *napi_get_value_int64* +#### napi_get_value_int64 @@ -1810,7 +1810,7 @@ is passed in it returns `napi_number_expected`. This API returns the C int64 primitive equivalent of the given JavaScript Number -#### *napi_get_value_string_latin1* +#### napi_get_value_string_latin1 @@ -1837,7 +1837,7 @@ is passed in it returns `napi_string_expected`. This API returns the ISO-8859-1-encoded string corresponding the value passed in. -#### *napi_get_value_string_utf8* +#### napi_get_value_string_utf8 @@ -1863,7 +1863,7 @@ is passed in it returns `napi_string_expected`. This API returns the UTF8-encoded string corresponding the value passed in. -#### *napi_get_value_string_utf16* +#### napi_get_value_string_utf16 @@ -1889,7 +1889,7 @@ is passed in it returns `napi_string_expected`. This API returns the UTF16-encoded string corresponding the value passed in. -#### *napi_get_value_uint32* +#### napi_get_value_uint32 @@ -1911,7 +1911,7 @@ This API returns the C primitive equivalent of the given `napi_value` as a `uint32_t`. ### Functions to get global instances -#### *napi_get_boolean* +#### napi_get_boolean @@ -1929,7 +1929,7 @@ Returns `napi_ok` if the API succeeded. This API is used to return the JavaScript singleton object that is used to represent the given boolean value -#### *napi_get_global* +#### napi_get_global @@ -1944,7 +1944,7 @@ Returns `napi_ok` if the API succeeded. This API returns the global Object. -#### *napi_get_null* +#### napi_get_null @@ -1959,7 +1959,7 @@ Returns `napi_ok` if the API succeeded. This API returns the null Object. -#### *napi_get_undefined* +#### napi_get_undefined @@ -1987,7 +1987,7 @@ These APIs support doing one of the following: 2. Check the type of a JavaScript value 3. Check for equality between two JavaScript values -### *napi_coerce_to_bool* +### napi_coerce_to_bool @@ -2008,7 +2008,7 @@ This API implements the abstract operation ToBoolean as defined in of the ECMAScript Language Specification. This API can be re-entrant if getters are defined on the passed-in Object. -### *napi_coerce_to_number* +### napi_coerce_to_number @@ -2029,7 +2029,7 @@ This API implements the abstract operation ToNumber as defined in of the ECMAScript Language Specification. This API can be re-entrant if getters are defined on the passed-in Object. -### *napi_coerce_to_object* +### napi_coerce_to_object @@ -2050,7 +2050,7 @@ This API implements the abstract operation ToObject as defined in of the ECMAScript Language Specification. This API can be re-entrant if getters are defined on the passed-in Object. -### *napi_coerce_to_string* +### napi_coerce_to_string @@ -2071,7 +2071,7 @@ This API implements the abstract operation ToString as defined in of the ECMAScript Language Specification. This API can be re-entrant if getters are defined on the passed-in Object. -### *napi_typeof* +### napi_typeof @@ -2092,7 +2092,7 @@ the object as defined in [Section 12.5.5][] of the ECMAScript Language Specification. However, it has support for detecting an External value. If `value` has a type that is invalid, an error is returned. -### *napi_instanceof* +### napi_instanceof @@ -2117,7 +2117,7 @@ defined in [Section 12.10.4](https://tc39.github.io/ecma262/#sec-instanceofoperator) of the ECMAScript Language Specification. -### *napi_is_array* +### napi_is_array @@ -2135,7 +2135,7 @@ This API represents invoking the `IsArray` operation on the object as defined in [Section 7.2.2](https://tc39.github.io/ecma262/#sec-isarray) of the ECMAScript Language Specification. -### *napi_is_arraybuffer* +### napi_is_arraybuffer @@ -2151,7 +2151,7 @@ Returns `napi_ok` if the API succeeded. This API checks if the Object passsed in is an array buffer. -### *napi_is_buffer* +### napi_is_buffer @@ -2168,7 +2168,7 @@ Returns `napi_ok` if the API succeeded. This API checks if the Object passsed in is a buffer. -### *napi_is_error* +### napi_is_error @@ -2184,7 +2184,7 @@ Returns `napi_ok` if the API succeeded. This API checks if the Object passsed in is an Error. -### *napi_is_typedarray* +### napi_is_typedarray @@ -2202,7 +2202,7 @@ This API checks if the Object passsed in is a typed array. -### *napi_is_dataview* +### napi_is_dataview @@ -2219,7 +2219,7 @@ Returns `napi_ok` if the API succeeded. This API checks if the Object passed in is a DataView. -### *napi_strict_equals* +### napi_strict_equals @@ -2373,7 +2373,7 @@ if (status != napi_ok) return status; ``` ### Structures -#### *napi_property_attributes* +#### napi_property_attributes ```C typedef enum { napi_default = 0, @@ -2407,7 +2407,7 @@ a static property on a class as opposed to an instance property, which is the default. This is used only by [`napi_define_class`][]. It is ignored by `napi_define_properties`. -#### *napi_property_descriptor* +#### napi_property_descriptor ```C typedef struct { // One of utf8name or name should be NULL. @@ -2453,7 +2453,7 @@ this function is invoked. See [`napi_property_attributes`](#napi_property_attributes). ### Functions -#### *napi_get_property_names* +#### napi_get_property_names @@ -2474,7 +2474,7 @@ Returns `napi_ok` if the API succeeded. This API returns the array of propertys for the Object passed in -#### *napi_set_property* +#### napi_set_property @@ -2494,7 +2494,7 @@ Returns `napi_ok` if the API succeeded. This API set a property on the Object passed in. -#### *napi_get_property* +#### napi_get_property @@ -2515,7 +2515,7 @@ Returns `napi_ok` if the API succeeded. This API gets the requested property from the Object passed in. -#### *napi_has_property* +#### napi_has_property @@ -2536,7 +2536,7 @@ Returns `napi_ok` if the API succeeded. This API checks if the Object passed in has the named property. -#### *napi_delete_property* +#### napi_delete_property @@ -2558,7 +2558,7 @@ Returns `napi_ok` if the API succeeded. This API attempts to delete the `key` own property from `object`. -#### *napi_has_own_property* +#### napi_has_own_property @@ -2581,7 +2581,7 @@ be a string or a Symbol, or an error will be thrown. N-API will not perform any conversion between data types. -#### *napi_set_named_property* +#### napi_set_named_property @@ -2602,7 +2602,7 @@ Returns `napi_ok` if the API succeeded. This method is equivalent to calling [`napi_set_property`][] with a `napi_value` created from the string passed in as `utf8Name` -#### *napi_get_named_property* +#### napi_get_named_property @@ -2623,7 +2623,7 @@ Returns `napi_ok` if the API succeeded. This method is equivalent to calling [`napi_get_property`][] with a `napi_value` created from the string passed in as `utf8Name` -#### *napi_has_named_property* +#### napi_has_named_property @@ -2644,7 +2644,7 @@ Returns `napi_ok` if the API succeeded. This method is equivalent to calling [`napi_has_property`][] with a `napi_value` created from the string passed in as `utf8Name` -#### *napi_set_element* +#### napi_set_element @@ -2664,7 +2664,7 @@ Returns `napi_ok` if the API succeeded. This API sets and element on the Object passed in. -#### *napi_get_element* +#### napi_get_element @@ -2684,7 +2684,7 @@ Returns `napi_ok` if the API succeeded. This API gets the element at the requested index. -#### *napi_has_element* +#### napi_has_element @@ -2705,7 +2705,7 @@ Returns `napi_ok` if the API succeeded. This API returns if the Object passed in has an element at the requested index. -#### *napi_delete_element* +#### napi_delete_element @@ -2726,7 +2726,7 @@ Returns `napi_ok` if the API succeeded. This API attempts to delete the specified `index` from `object`. -#### *napi_define_properties* +#### napi_define_properties @@ -2769,7 +2769,7 @@ like a regular JavaScript function call, or as a constructor function. -### *napi_call_function* +### napi_call_function @@ -2835,7 +2835,7 @@ status = napi_get_value_int32(env, return_val, &result); if (status != napi_ok) return; ``` -### *napi_create_function* +### napi_create_function @@ -2903,7 +2903,7 @@ myaddon.sayHello(); `NAPI_MODULE` in the earlier snippet but the name of the target in `binding.gyp` responsible for creating the `.node` file. -### *napi_get_cb_info* +### napi_get_cb_info @@ -2933,7 +2933,7 @@ Returns `napi_ok` if the API succeeded. This method is used within a callback function to retrieve details about the call like the arguments and the `this` pointer from a given callback info. -### *napi_get_new_target* +### napi_get_new_target @@ -2952,7 +2952,7 @@ Returns `napi_ok` if the API succeeded. This API returns the `new.target` of the constructor call. If the current callback is not a constructor call, the result is `NULL`. -### *napi_new_instance* +### napi_new_instance @@ -3047,7 +3047,7 @@ if (is_instance) { The reference must be freed once it is no longer needed. -### *napi_define_class* +### napi_define_class @@ -3103,7 +3103,7 @@ case, to prevent the function value from being garbage-collected, create a persistent reference to it using [`napi_create_reference`][] and ensure the reference count is kept >= 1. -### *napi_wrap* +### napi_wrap @@ -3165,7 +3165,7 @@ native instance associated with it by virtue of a previous call to another native instance with the given object, call `napi_remove_wrap()` on it first. -### *napi_unwrap* +### napi_unwrap @@ -3190,7 +3190,7 @@ method or accessor, then the `this` argument to the callback is the wrapper object; the wrapped C++ instance that is the target of the call can be obtained then by calling `napi_unwrap()` on the wrapper object. -### *napi_remove_wrap* +### napi_remove_wrap From 5d06fab7168f18351a90bea1027496b26208d812 Mon Sep 17 00:00:00 2001 From: Jinho Bang Date: Mon, 8 Jan 2018 23:37:27 +0900 Subject: [PATCH 149/183] n-api: throw RangeError napi_create_typedarray() According to the ECMA spec, we should throw a RangeError in the following cases: - `(length * elementSize) + offset` > the size of the array passed in - `offset % elementSize` != `0` In the current implementation, this check was omitted. So, the following code will cause a crash. ``` napi_create_typedarray(env, napi_uint16_array, 2 /* length */, buffer, 1 /* byte_offset */, &output_array); ``` This change fixes the problem and write some related tests. Refs: https://tc39.github.io/ecma262/#sec-typedarray-buffer-byteoffset-length PR-URL: https://github.com/nodejs/node/pull/18037 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Michael Dawson --- src/node_api.cc | 51 +++++++++++++++---- test/addons-napi/test_typedarray/test.js | 18 +++++++ .../test_typedarray/test_typedarray.c | 28 ++++++++-- 3 files changed, 85 insertions(+), 12 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index 19b993300d1b00..93cce1909f0860 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -149,6 +149,30 @@ struct napi_env__ { (!try_catch.HasCaught() ? napi_ok \ : napi_set_last_error((env), napi_pending_exception)) +#define THROW_RANGE_ERROR_IF_FALSE(env, condition, error, message) \ + do { \ + if (!(condition)) { \ + napi_throw_range_error((env), (error), (message)); \ + return napi_set_last_error((env), napi_generic_failure); \ + } \ + } while (0) + +#define CREATE_TYPED_ARRAY( \ + env, type, size_of_element, buffer, byte_offset, length, out) \ + do { \ + if ((size_of_element) > 1) { \ + THROW_RANGE_ERROR_IF_FALSE( \ + (env), (byte_offset) % (size_of_element) == 0, \ + "ERR_NAPI_INVALID_TYPEDARRAY_ALIGNMENT", \ + "start offset of "#type" should be a multiple of "#size_of_element); \ + } \ + THROW_RANGE_ERROR_IF_FALSE((env), (length) * (size_of_element) + \ + (byte_offset) <= buffer->ByteLength(), \ + "ERR_NAPI_INVALID_TYPEDARRAY_LENGTH", \ + "Invalid typed array length"); \ + (out) = v8::type::New((buffer), (byte_offset), (length)); \ + } while (0) + namespace v8impl { // convert from n-api property attributes to v8::PropertyAttribute @@ -3157,31 +3181,40 @@ napi_status napi_create_typedarray(napi_env env, switch (type) { case napi_int8_array: - typedArray = v8::Int8Array::New(buffer, byte_offset, length); + CREATE_TYPED_ARRAY( + env, Int8Array, 1, buffer, byte_offset, length, typedArray); break; case napi_uint8_array: - typedArray = v8::Uint8Array::New(buffer, byte_offset, length); + CREATE_TYPED_ARRAY( + env, Uint8Array, 1, buffer, byte_offset, length, typedArray); break; case napi_uint8_clamped_array: - typedArray = v8::Uint8ClampedArray::New(buffer, byte_offset, length); + CREATE_TYPED_ARRAY( + env, Uint8ClampedArray, 1, buffer, byte_offset, length, typedArray); break; case napi_int16_array: - typedArray = v8::Int16Array::New(buffer, byte_offset, length); + CREATE_TYPED_ARRAY( + env, Int16Array, 2, buffer, byte_offset, length, typedArray); break; case napi_uint16_array: - typedArray = v8::Uint16Array::New(buffer, byte_offset, length); + CREATE_TYPED_ARRAY( + env, Uint16Array, 2, buffer, byte_offset, length, typedArray); break; case napi_int32_array: - typedArray = v8::Int32Array::New(buffer, byte_offset, length); + CREATE_TYPED_ARRAY( + env, Int32Array, 4, buffer, byte_offset, length, typedArray); break; case napi_uint32_array: - typedArray = v8::Uint32Array::New(buffer, byte_offset, length); + CREATE_TYPED_ARRAY( + env, Uint32Array, 4, buffer, byte_offset, length, typedArray); break; case napi_float32_array: - typedArray = v8::Float32Array::New(buffer, byte_offset, length); + CREATE_TYPED_ARRAY( + env, Float32Array, 4, buffer, byte_offset, length, typedArray); break; case napi_float64_array: - typedArray = v8::Float64Array::New(buffer, byte_offset, length); + CREATE_TYPED_ARRAY( + env, Float64Array, 8, buffer, byte_offset, length, typedArray); break; default: return napi_set_last_error(env, napi_invalid_arg); diff --git a/test/addons-napi/test_typedarray/test.js b/test/addons-napi/test_typedarray/test.js index 27ef054fe4635e..4a4e79ebe7bcdb 100644 --- a/test/addons-napi/test_typedarray/test.js +++ b/test/addons-napi/test_typedarray/test.js @@ -55,3 +55,21 @@ arrayTypes.forEach((currentType) => { assert.notStrictEqual(theArray, template); assert.strictEqual(theArray.buffer, buffer); }); + +arrayTypes.forEach((currentType) => { + const template = Reflect.construct(currentType, buffer); + assert.throws(() => { + test_typedarray.CreateTypedArray(template, buffer, 0, 136); + }, /Invalid typed array length/); +}); + +const nonByteArrayTypes = [ Int16Array, Uint16Array, Int32Array, Uint32Array, + Float32Array, Float64Array ]; +nonByteArrayTypes.forEach((currentType) => { + const template = Reflect.construct(currentType, buffer); + assert.throws(() => { + test_typedarray.CreateTypedArray(template, buffer, + currentType.BYTES_PER_ELEMENT + 1, 1); + console.log(`start of offset ${currentType}`); + }, /start offset of/); +}); diff --git a/test/addons-napi/test_typedarray/test_typedarray.c b/test/addons-napi/test_typedarray/test_typedarray.c index 93995d450c440b..febb45e715cb11 100644 --- a/test/addons-napi/test_typedarray/test_typedarray.c +++ b/test/addons-napi/test_typedarray/test_typedarray.c @@ -97,11 +97,11 @@ napi_value External(napi_env env, napi_callback_info info) { } napi_value CreateTypedArray(napi_env env, napi_callback_info info) { - size_t argc = 2; - napi_value args[2]; + size_t argc = 4; + napi_value args[4]; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); - NAPI_ASSERT(env, argc == 2, "Wrong number of arguments"); + NAPI_ASSERT(env, argc == 2 || argc == 4, "Wrong number of arguments"); napi_value input_array = args[0]; napi_valuetype valuetype0; @@ -136,6 +136,28 @@ napi_value CreateTypedArray(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_typedarray_info( env, input_array, &type, &length, NULL, &in_array_buffer, &byte_offset)); + if (argc == 4) { + napi_valuetype valuetype2; + NAPI_CALL(env, napi_typeof(env, args[2], &valuetype2)); + + NAPI_ASSERT(env, valuetype2 == napi_number, + "Wrong type of arguments. Expects a number as third argument."); + + uint32_t uint32_length; + NAPI_CALL(env, napi_get_value_uint32(env, args[2], &uint32_length)); + length = uint32_length; + + napi_valuetype valuetype3; + NAPI_CALL(env, napi_typeof(env, args[3], &valuetype3)); + + NAPI_ASSERT(env, valuetype3 == napi_number, + "Wrong type of arguments. Expects a number as third argument."); + + uint32_t uint32_byte_offset; + NAPI_CALL(env, napi_get_value_uint32(env, args[3], &uint32_byte_offset)); + byte_offset = uint32_byte_offset; + } + napi_value output_array; NAPI_CALL(env, napi_create_typedarray( env, type, length, input_buffer, byte_offset, &output_array)); From e32cca2f2c9e61328f208e47b443ed3f3e6c101f Mon Sep 17 00:00:00 2001 From: furstenheim Date: Sun, 14 Jan 2018 19:01:51 +0100 Subject: [PATCH 150/183] test: fixed typos in napi test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/18148 Reviewed-By: James M Snell Reviewed-By: Jon Moss Reviewed-By: Colin Ihrig Reviewed-By: Michael Dawson Reviewed-By: Anna Henningsen Reviewed-By: Tobias Nießen Reviewed-By: Luigi Pinca --- test/addons-napi/test_symbol/test_symbol.c | 2 +- test/addons-napi/test_typedarray/test_typedarray.c | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/addons-napi/test_symbol/test_symbol.c b/test/addons-napi/test_symbol/test_symbol.c index acb1c7f715409a..87f6cdde671600 100644 --- a/test/addons-napi/test_symbol/test_symbol.c +++ b/test/addons-napi/test_symbol/test_symbol.c @@ -12,7 +12,7 @@ napi_value Test(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_typeof(env, args[0], &valuetype)); NAPI_ASSERT(env, valuetype == napi_symbol, - "Wrong type of argments. Expects a symbol."); + "Wrong type of arguments. Expects a symbol."); char buffer[128]; size_t buffer_size = 128; diff --git a/test/addons-napi/test_typedarray/test_typedarray.c b/test/addons-napi/test_typedarray/test_typedarray.c index febb45e715cb11..278812f98d5971 100644 --- a/test/addons-napi/test_typedarray/test_typedarray.c +++ b/test/addons-napi/test_typedarray/test_typedarray.c @@ -13,20 +13,20 @@ napi_value Multiply(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0)); NAPI_ASSERT(env, valuetype0 == napi_object, - "Wrong type of argments. Expects a typed array as first argument."); + "Wrong type of arguments. Expects a typed array as first argument."); napi_value input_array = args[0]; bool is_typedarray; NAPI_CALL(env, napi_is_typedarray(env, input_array, &is_typedarray)); NAPI_ASSERT(env, is_typedarray, - "Wrong type of argments. Expects a typed array as first argument."); + "Wrong type of arguments. Expects a typed array as first argument."); napi_valuetype valuetype1; NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1)); NAPI_ASSERT(env, valuetype1 == napi_number, - "Wrong type of argments. Expects a number as second argument."); + "Wrong type of arguments. Expects a number as second argument."); double multiplier; NAPI_CALL(env, napi_get_value_double(env, args[1], &multiplier)); @@ -108,26 +108,26 @@ napi_value CreateTypedArray(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_typeof(env, input_array, &valuetype0)); NAPI_ASSERT(env, valuetype0 == napi_object, - "Wrong type of argments. Expects a typed array as first argument."); + "Wrong type of arguments. Expects a typed array as first argument."); bool is_typedarray; NAPI_CALL(env, napi_is_typedarray(env, input_array, &is_typedarray)); NAPI_ASSERT(env, is_typedarray, - "Wrong type of argments. Expects a typed array as first argument."); + "Wrong type of arguments. Expects a typed array as first argument."); napi_valuetype valuetype1; napi_value input_buffer = args[1]; NAPI_CALL(env, napi_typeof(env, input_buffer, &valuetype1)); NAPI_ASSERT(env, valuetype1 == napi_object, - "Wrong type of argments. Expects an array buffer as second argument."); + "Wrong type of arguments. Expects an array buffer as second argument."); bool is_arraybuffer; NAPI_CALL(env, napi_is_arraybuffer(env, input_buffer, &is_arraybuffer)); NAPI_ASSERT(env, is_arraybuffer, - "Wrong type of argments. Expects an array buffer as second argument."); + "Wrong type of arguments. Expects an array buffer as second argument."); napi_typedarray_type type; napi_value in_array_buffer; From 91c9e193dc4074a2cbfdd0203424eb23e01cee0f Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Tue, 16 Jan 2018 14:05:26 -0500 Subject: [PATCH 151/183] doc: remove uannecessary Require This was the only instance were we said a parameter was required. It is assumed parameters are required unless the doc says they are option. Remove `Required` to make consistent with the rest of the doc PR-URL: https://github.com/nodejs/node/pull/18184 Reviewed-By: Colin Ihrig Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Rich Trott Reviewed-By: Gireesh Punathil Reviewed-By: Daniel Bevenius --- doc/api/n-api.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 9fc99759101489..c82ace15ed48b7 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -3281,7 +3281,7 @@ napi_status napi_create_async_work(napi_env env, - `[in] env`: The environment that the API is invoked under. - `[in] async_resource`: An optional object associated with the async work that will be passed to possible async_hooks [`init` hooks][]. -- `[in] async_resource_name`: An identifier for the kind of resource that is +- `[in] async_resource_name`: Identifier for the kind of resource that is being provided for diagnostic information exposed by the `async_hooks` API. - `[in] execute`: The native function which should be called to excute the logic asynchronously. @@ -3380,7 +3380,7 @@ napi_status napi_async_init(napi_env env, - `[in] env`: The environment that the API is invoked under. - `[in] async_resource`: An optional object associated with the async work that will be passed to possible `async_hooks` [`init` hooks][]. -- `[in] async_resource_name`: Required identifier for the kind of resource +- `[in] async_resource_name`: Identifier for the kind of resource that is being provided for diagnostic information exposed by the `async_hooks` API. - `[out] result`: The initialized async context. From f103e2123dd418c07ed62f8412aa9b4908d0c5c8 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Tue, 23 Jan 2018 22:18:38 -0800 Subject: [PATCH 152/183] test: refactor addons-napi/test_exception/test.js * provide block scoping to prevent unintended side effects * remove confusing and unnecessary assertion message * use consisitent `actual, expected` argument order for assertions PR-URL: https://github.com/nodejs/node/pull/18340 Reviewed-By: Colin Ihrig Reviewed-By: Jon Moss Reviewed-By: James M Snell --- test/addons-napi/test_exception/test.js | 86 ++++++++++++------------- 1 file changed, 42 insertions(+), 44 deletions(-) diff --git a/test/addons-napi/test_exception/test.js b/test/addons-napi/test_exception/test.js index 8bd2f50b12b15f..83961411df7574 100644 --- a/test/addons-napi/test_exception/test.js +++ b/test/addons-napi/test_exception/test.js @@ -4,49 +4,47 @@ const common = require('../../common'); const test_exception = require(`./build/${common.buildType}/test_exception`); const assert = require('assert'); const theError = new Error('Some error'); -function throwTheError() { - throw theError; + +{ + const throwTheError = () => { throw theError; }; + + // Test that the native side successfully captures the exception + let returnedError = test_exception.returnException(throwTheError); + assert.strictEqual(theError, returnedError); + + // Test that the native side passes the exception through + assert.throws( + () => { test_exception.allowException(throwTheError); }, + (err) => err === theError + ); + + // Test that the exception thrown above was marked as pending + // before it was handled on the JS side + assert.strictEqual(test_exception.wasPending(), true, + 'VM was marked as having an exception pending' + + ' when it was allowed through'); + + // Test that the native side does not capture a non-existing exception + returnedError = test_exception.returnException(common.mustCall()); + assert.strictEqual(returnedError, undefined, + 'Returned error should be undefined when no exception is' + + ` thrown, but ${returnedError} was passed`); } -let caughtError; - -// Test that the native side successfully captures the exception -let returnedError = test_exception.returnException(throwTheError); -assert.strictEqual(theError, returnedError); - -// Test that the native side passes the exception through -assert.throws( - () => { - test_exception.allowException(throwTheError); - }, - function(err) { - return err === theError; - }, - 'Thrown exception was allowed to pass through unhindered' -); - -// Test that the exception thrown above was marked as pending -// before it was handled on the JS side -assert.strictEqual(test_exception.wasPending(), true, - 'VM was marked as having an exception pending' + - ' when it was allowed through'); - -// Test that the native side does not capture a non-existing exception -returnedError = test_exception.returnException(common.mustCall()); -assert.strictEqual(undefined, returnedError, - 'Returned error should be undefined when no exception is' + - ` thrown, but ${returnedError} was passed`); - -// Test that no exception appears that was not thrown by us -try { - test_exception.allowException(common.mustCall()); -} catch (anError) { - caughtError = anError; + +{ + // Test that no exception appears that was not thrown by us + let caughtError; + try { + test_exception.allowException(common.mustCall()); + } catch (anError) { + caughtError = anError; + } + assert.strictEqual(caughtError, undefined, + 'No exception originated on the native side, but' + + ` ${caughtError} was passed`); + + // Test that the exception state remains clear when no exception is thrown + assert.strictEqual(test_exception.wasPending(), false, + 'VM was not marked as having an exception pending' + + ' when none was allowed through'); } -assert.strictEqual(undefined, caughtError, - 'No exception originated on the native side, but' + - ` ${caughtError} was passed`); - -// Test that the exception state remains clear when no exception is thrown -assert.strictEqual(test_exception.wasPending(), false, - 'VM was not marked as having an exception pending' + - ' when none was allowed through'); From 0fd8ceb251ab4964c9750db93f156c7d0aa16c93 Mon Sep 17 00:00:00 2001 From: Aaron Kau Date: Sat, 27 Jan 2018 14:20:13 -0500 Subject: [PATCH 153/183] n-api: change assert ok check to notStrictEqual. PR-URL: https://github.com/nodejs/node/pull/18414 Reviewed-By: Gireesh Punathil Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- test/addons-napi/test_general/test.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/test/addons-napi/test_general/test.js b/test/addons-napi/test_general/test.js index 10b85ef727bb20..15461cef2f6a36 100644 --- a/test/addons-napi/test_general/test.js +++ b/test/addons-napi/test_general/test.js @@ -28,9 +28,9 @@ assert.strictEqual(test_general.testGetPrototype(baseObject), Object.getPrototypeOf(baseObject)); assert.strictEqual(test_general.testGetPrototype(extendedObject), Object.getPrototypeOf(extendedObject)); -assert.ok(test_general.testGetPrototype(baseObject) !== - test_general.testGetPrototype(extendedObject), - 'Prototypes for base and extended should be different'); +// Prototypes for base and extended should be different. +assert.notStrictEqual(test_general.testGetPrototype(baseObject), + test_general.testGetPrototype(extendedObject)); // test version management funcitons // expected version is currently 1 @@ -70,17 +70,15 @@ assert.strictEqual(test_general.derefItemWasCalled(), true, // Assert that wrapping twice fails. const x = {}; test_general.wrap(x); -assert.throws(function() { - test_general.wrap(x); -}, Error); +assert.throws(() => test_general.wrap(x), Error); // Ensure that wrapping, removing the wrap, and then wrapping again works. const y = {}; test_general.wrap(y); test_general.removeWrap(y); -assert.doesNotThrow(function() { - test_general.wrap(y); -}, Error, 'Wrapping twice succeeds if a remove_wrap() separates the instances'); +assert.doesNotThrow(() => test_general.wrap(y), Error, + 'Wrapping twice succeeds if a remove_wrap()' + + ' separates the instances'); // Ensure that removing a wrap and garbage collecting does not fire the // finalize callback. From e57f34c310e937b8f3ffd11f9a0f145e9934f72d Mon Sep 17 00:00:00 2001 From: Ben Wilcox Date: Sat, 27 Jan 2018 14:39:55 -0500 Subject: [PATCH 154/183] test: show pending exception error in napi tests Shows the result of the wasPending in the error message if the assertion fails. PR-URL: https://github.com/nodejs/node/pull/18413 Reviewed-By: James M Snell Reviewed-By: Rich Trott Reviewed-By: Ruben Bridgewater --- test/addons-napi/test_exception/test.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/test/addons-napi/test_exception/test.js b/test/addons-napi/test_exception/test.js index 83961411df7574..787b7d78b1c72b 100644 --- a/test/addons-napi/test_exception/test.js +++ b/test/addons-napi/test_exception/test.js @@ -20,9 +20,10 @@ const theError = new Error('Some error'); // Test that the exception thrown above was marked as pending // before it was handled on the JS side - assert.strictEqual(test_exception.wasPending(), true, - 'VM was marked as having an exception pending' + - ' when it was allowed through'); + const exception_pending = test_exception.wasPending(); + assert.strictEqual(exception_pending, true, + 'Exception not pending as expected,' + + ` .wasPending() returned ${exception_pending}`); // Test that the native side does not capture a non-existing exception returnedError = test_exception.returnException(common.mustCall()); @@ -44,7 +45,8 @@ const theError = new Error('Some error'); ` ${caughtError} was passed`); // Test that the exception state remains clear when no exception is thrown - assert.strictEqual(test_exception.wasPending(), false, - 'VM was not marked as having an exception pending' + - ' when none was allowed through'); + const exception_pending = test_exception.wasPending(); + assert.strictEqual(exception_pending, false, + 'Exception state did not remain clear as expected,' + + ` .wasPending() returned ${exception_pending}`); } From 5c183d76dd32ab8062cd792ebe5e0f7cd35779f9 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Mon, 22 Jan 2018 23:13:02 -0500 Subject: [PATCH 155/183] n-api: implement wrapping using private properties PR-URL: https://github.com/nodejs/node/pull/18311 Reviewed-By: Anna Henningsen Reviewed-By: Michael Dawson Fixes: https://github.com/nodejs/node/issues/14367 --- src/env.h | 2 + src/node_api.cc | 160 ++++++++++++---------------------------- src/node_api_backport.h | 1 + 3 files changed, 52 insertions(+), 111 deletions(-) diff --git a/src/env.h b/src/env.h index 36ca7004ad748c..0045a4fa79c95e 100644 --- a/src/env.h +++ b/src/env.h @@ -62,6 +62,8 @@ namespace node { V(npn_buffer_private_symbol, "node:npnBuffer") \ V(processed_private_symbol, "node:processed") \ V(selected_npn_buffer_private_symbol, "node:selectedNpnBuffer") \ + V(napi_env, "node:napi:env") \ + V(napi_wrapper, "node:napi:wrapper") \ // Strings are per-isolate primitives but Environment proxies them // for the sake of convenience. Strings should be ASCII-only. diff --git a/src/node_api.cc b/src/node_api.cc index 93cce1909f0860..dc32986e3adfc3 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -20,7 +20,6 @@ #include "node_internals.h" #include "env-inl.h" #include "node_api_backport.h" -#include "util.h" static napi_status napi_set_last_error(napi_env env, napi_status error_code, @@ -53,6 +52,9 @@ struct napi_env__ { uv_loop_t* loop = nullptr; }; +#define NAPI_PRIVATE_KEY(context, suffix) \ + (node::Environment::GetCurrent((context))->napi_ ## suffix()) + #define ENV_OBJECT_TEMPLATE(env, prefix, destination, field_count) \ do { \ if ((env)->prefix ## _template.IsEmpty()) { \ @@ -383,6 +385,10 @@ class Reference : private Finalizer { } public: + void* Data() { + return _finalize_data; + } + static Reference* New(napi_env env, v8::Local value, uint32_t initial_refcount, @@ -742,45 +748,6 @@ v8::Local CreateAccessorCallbackData(napi_env env, return cbdata; } -int kWrapperFields = 3; - -// Pointer used to identify items wrapped by N-API. Used by FindWrapper and -// napi_wrap(). -const char napi_wrap_name[] = "N-API Wrapper"; - -// Search the object's prototype chain for the wrapper object. Usually the -// wrapper would be the first in the chain, but it is OK for other objects to -// be inserted in the prototype chain. -static -bool FindWrapper(v8::Local obj, - v8::Local* result = nullptr, - v8::Local* parent = nullptr) { - v8::Local wrapper = obj; - - do { - v8::Local proto = wrapper->GetPrototype(); - if (proto.IsEmpty() || !proto->IsObject()) { - return false; - } - if (parent != nullptr) { - *parent = wrapper; - } - wrapper = proto.As(); - if (wrapper->InternalFieldCount() == kWrapperFields) { - v8::Local external = wrapper->GetInternalField(1); - if (external->IsExternal() && - external.As()->Value() == v8impl::napi_wrap_name) { - break; - } - } - } while (true); - - if (result != nullptr) { - *result = wrapper; - } - return true; -} - static void DeleteEnv(napi_env env, void* data, void* hint) { delete env; } @@ -797,11 +764,8 @@ napi_env GetEnv(v8::Local context) { // because we need to stop hard if either of them is empty. // // Re https://github.com/nodejs/node/pull/14217#discussion_r128775149 - auto key = v8::Private::ForApi(isolate, - v8::String::NewFromOneByte(isolate, - reinterpret_cast("N-API Environment"), - v8::NewStringType::kInternalized).ToLocalChecked()); - auto value = global->GetPrivate(context, key).ToLocalChecked(); + auto value = global->GetPrivate(context, NAPI_PRIVATE_KEY(context, env)) + .ToLocalChecked(); if (value->IsExternal()) { result = static_cast(value.As()->Value()); @@ -811,7 +775,8 @@ napi_env GetEnv(v8::Local context) { // We must also stop hard if the result of assigning the env to the global // is either nothing or false. - CHECK(global->SetPrivate(context, key, external).FromJust()); + CHECK(global->SetPrivate(context, NAPI_PRIVATE_KEY(context, env), external) + .FromJust()); // Create a self-destructing reference to external that will get rid of the // napi_env when external goes out of scope. @@ -821,28 +786,46 @@ napi_env GetEnv(v8::Local context) { return result; } +enum UnwrapAction { + KeepWrap, + RemoveWrap +}; + static napi_status Unwrap(napi_env env, napi_value js_object, void** result, - v8::Local* wrapper, - v8::Local* parent = nullptr) { + UnwrapAction action) { + NAPI_PREAMBLE(env); CHECK_ARG(env, js_object); - CHECK_ARG(env, result); + if (action == KeepWrap) { + CHECK_ARG(env, result); + } + + v8::Isolate* isolate = env->isolate; + v8::Local context = isolate->GetCurrentContext(); v8::Local value = v8impl::V8LocalValueFromJsValue(js_object); RETURN_STATUS_IF_FALSE(env, value->IsObject(), napi_invalid_arg); v8::Local obj = value.As(); - RETURN_STATUS_IF_FALSE( - env, v8impl::FindWrapper(obj, wrapper, parent), napi_invalid_arg); + auto val = obj->GetPrivate(context, NAPI_PRIVATE_KEY(context, wrapper)) + .ToLocalChecked(); + RETURN_STATUS_IF_FALSE(env, val->IsExternal(), napi_invalid_arg); + Reference* reference = + static_cast(val.As()->Value()); - v8::Local unwrappedValue = (*wrapper)->GetInternalField(0); - RETURN_STATUS_IF_FALSE(env, unwrappedValue->IsExternal(), napi_invalid_arg); + if (result) { + *result = reference->Data(); + } - *result = unwrappedValue.As()->Value(); + if (action == RemoveWrap) { + CHECK(obj->DeletePrivate(context, NAPI_PRIVATE_KEY(context, wrapper)) + .FromJust()); + Reference::Delete(reference); + } - return napi_ok; + return GET_RETURN_STATUS(env); } static @@ -2399,26 +2382,9 @@ napi_status napi_wrap(napi_env env, v8::Local obj = value.As(); // If we've already wrapped this object, we error out. - RETURN_STATUS_IF_FALSE(env, !v8impl::FindWrapper(obj), napi_invalid_arg); - - // Create a wrapper object with an internal field to hold the wrapped pointer - // and a second internal field to identify the owner as N-API. - v8::Local wrapper_template; - ENV_OBJECT_TEMPLATE(env, wrap, wrapper_template, v8impl::kWrapperFields); - - auto maybe_object = wrapper_template->NewInstance(context); - CHECK_MAYBE_EMPTY(env, maybe_object, napi_generic_failure); - v8::Local wrapper = maybe_object.ToLocalChecked(); - - // Store the pointer as an external in the wrapper. - wrapper->SetInternalField(0, v8::External::New(isolate, native_object)); - wrapper->SetInternalField(1, v8::External::New(isolate, - reinterpret_cast(const_cast(v8impl::napi_wrap_name)))); - - // Insert the wrapper into the object's prototype chain. - v8::Local proto = obj->GetPrototype(); - CHECK(wrapper->SetPrototype(context, proto).FromJust()); - CHECK(obj->SetPrototype(context, wrapper).FromJust()); + RETURN_STATUS_IF_FALSE(env, + !obj->HasPrivate(context, NAPI_PRIVATE_KEY(context, wrapper)).FromJust(), + napi_invalid_arg); v8impl::Reference* reference = nullptr; if (result != nullptr) { @@ -2430,52 +2396,24 @@ napi_status napi_wrap(napi_env env, reference = v8impl::Reference::New( env, obj, 0, false, finalize_cb, native_object, finalize_hint); *result = reinterpret_cast(reference); - } else if (finalize_cb != nullptr) { - // Create a self-deleting reference just for the finalize callback. - reference = v8impl::Reference::New( - env, obj, 0, true, finalize_cb, native_object, finalize_hint); + } else { + // Create a self-deleting reference. + reference = v8impl::Reference::New(env, obj, 0, true, finalize_cb, + native_object, finalize_cb == nullptr ? nullptr : finalize_hint); } - if (reference != nullptr) { - wrapper->SetInternalField(2, v8::External::New(isolate, reference)); - } + CHECK(obj->SetPrivate(context, NAPI_PRIVATE_KEY(context, wrapper), + v8::External::New(isolate, reference)).FromJust()); return GET_RETURN_STATUS(env); } napi_status napi_unwrap(napi_env env, napi_value obj, void** result) { - // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw - // JS exceptions. - CHECK_ENV(env); - v8::Local wrapper; - return napi_set_last_error(env, v8impl::Unwrap(env, obj, result, &wrapper)); + return v8impl::Unwrap(env, obj, result, v8impl::KeepWrap); } napi_status napi_remove_wrap(napi_env env, napi_value obj, void** result) { - NAPI_PREAMBLE(env); - v8::Local wrapper; - v8::Local parent; - napi_status status = v8impl::Unwrap(env, obj, result, &wrapper, &parent); - if (status != napi_ok) { - return napi_set_last_error(env, status); - } - - v8::Local external = wrapper->GetInternalField(2); - if (external->IsExternal()) { - v8impl::Reference::Delete( - static_cast(external.As()->Value())); - } - - if (!parent.IsEmpty()) { - v8::Maybe maybe = parent->SetPrototype( - env->isolate->GetCurrentContext(), wrapper->GetPrototype()); - CHECK_MAYBE_NOTHING(env, maybe, napi_generic_failure); - if (!maybe.FromMaybe(false)) { - return napi_set_last_error(env, napi_generic_failure); - } - } - - return GET_RETURN_STATUS(env); + return v8impl::Unwrap(env, obj, result, v8impl::RemoveWrap); } napi_status napi_create_external(napi_env env, diff --git a/src/node_api_backport.h b/src/node_api_backport.h index 968169ef3bd05f..1333b33eddfe14 100644 --- a/src/node_api_backport.h +++ b/src/node_api_backport.h @@ -6,6 +6,7 @@ // N-API. #include "node_internals.h" +#include "env-inl.h" namespace node { From 76dfefa56cbfa15fc371ac0accae0068249c1620 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 2 Feb 2018 17:27:01 +0100 Subject: [PATCH 156/183] n-api: wrap control flow macro in do/while Make CHECK_ENV() safe to use in the following context: if (condition) CHECK_ENV(env); else something_else(); PR-URL: https://github.com/nodejs/node/pull/18532 Reviewed-By: Michael Dawson Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Ali Ijaz Sheikh Reviewed-By: Ruben Bridgewater Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Daniel Bevenius --- src/node_api.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index dc32986e3adfc3..58803b30bcc96b 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -75,10 +75,12 @@ struct napi_env__ { } \ } while (0) -#define CHECK_ENV(env) \ - if ((env) == nullptr) { \ - return napi_invalid_arg; \ - } +#define CHECK_ENV(env) \ + do { \ + if ((env) == nullptr) { \ + return napi_invalid_arg; \ + } \ + } while (0) #define CHECK_ARG(env, arg) \ RETURN_STATUS_IF_FALSE((env), ((arg) != nullptr), napi_invalid_arg) From b7093fad5790ff875eb227fb524e46d0e289a306 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Fri, 2 Feb 2018 10:33:01 -0500 Subject: [PATCH 157/183] doc: remove usage of you in n-api doc We avoid using 'you' in the documentation based on our guidelines. Remove usage in the n-api doc. PR-URL: https://github.com/nodejs/node/pull/18528 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Ruben Bridgewater Reviewed-By: Gireesh Punathil Reviewed-By: Daniel Bevenius --- doc/api/n-api.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index c82ace15ed48b7..6084fab87a6709 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -3159,11 +3159,8 @@ required in order to enable correct proper of the reference. Afterward, additional manipulation of the wrapper's prototype chain may cause `napi_unwrap()` to fail. -*Note*: Calling `napi_wrap()` a second time on an object that already has a -native instance associated with it by virtue of a previous call to -`napi_wrap()` will cause an error to be returned. If you wish to associate -another native instance with the given object, call `napi_remove_wrap()` on it -first. +Calling napi_wrap() a second time on an object will return an error. To associate +another native instance with the object, use napi_remove_wrap() first. ### napi_unwrap +```C +NAPI_EXTERN napi_status napi_open_callback_scope(napi_env env, + napi_value resource_object, + napi_async_context context, + napi_callback_scope* result) +``` +- `[in] env`: The environment that the API is invoked under. +- `[in] resource_object`: An optional object associated with the async work + that will be passed to possible async_hooks [`init` hooks][]. +- `[in] context`: Context for the async operation that is +invoking the callback. This should be a value previously obtained +from [`napi_async_init`][]. +- `[out] result`: The newly created scope. + +There are cases(for example resolving promises) where it is +necessary to have the equivalent of the scope associated with a callback +in place when making certain N-API calls. If there is no other script on +the stack the [`napi_open_callback_scope`][] and +[`napi_close_callback_scope`][] functions can be used to open/close +the required scope. + +### *napi_close_callback_scope* + +```C +NAPI_EXTERN napi_status napi_close_callback_scope(napi_env env, + napi_callback_scope scope) +``` +- `[in] env`: The environment that the API is invoked under. +- `[in] scope`: The scope to be closed. + ## Version Management ### napi_get_node_version @@ -3723,6 +3759,7 @@ NAPI_EXTERN napi_status napi_get_uv_event_loop(napi_env env, [`napi_async_init`]: #n_api_napi_async_init [`napi_cancel_async_work`]: #n_api_napi_cancel_async_work [`napi_close_escapable_handle_scope`]: #n_api_napi_close_escapable_handle_scope +[`napi_close_callback_scope`]: #n_api_napi_close_callback_scope [`napi_close_handle_scope`]: #n_api_napi_close_handle_scope [`napi_create_async_work`]: #n_api_napi_create_async_work [`napi_create_error`]: #n_api_napi_create_error @@ -3749,6 +3786,7 @@ NAPI_EXTERN napi_status napi_get_uv_event_loop(napi_env env, [`napi_get_last_error_info`]: #n_api_napi_get_last_error_info [`napi_get_and_clear_last_exception`]: #n_api_napi_get_and_clear_last_exception [`napi_make_callback`]: #n_api_napi_make_callback +[`napi_open_callback_scope`]: #n_api_napi_open_callback_scope [`napi_open_escapable_handle_scope`]: #n_api_napi_open_escapable_handle_scope [`napi_open_handle_scope`]: #n_api_napi_open_handle_scope [`napi_property_descriptor`]: #n_api_napi_property_descriptor diff --git a/src/env-inl.h b/src/env-inl.h index 45a327de16931d..9b4000d75b180d 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -111,7 +111,7 @@ inline Environment::AsyncCallbackScope::~AsyncCallbackScope() { env_->makecallback_cntr_--; } -inline bool Environment::AsyncCallbackScope::in_makecallback() { +inline bool Environment::AsyncCallbackScope::in_makecallback() const { return env_->makecallback_cntr_ > 1; } diff --git a/src/env.h b/src/env.h index 0045a4fa79c95e..593f38964c7839 100644 --- a/src/env.h +++ b/src/env.h @@ -297,7 +297,7 @@ class Environment { explicit AsyncCallbackScope(Environment* env); ~AsyncCallbackScope(); - inline bool in_makecallback(); + inline bool in_makecallback() const; private: Environment* env_; diff --git a/src/node_api.cc b/src/node_api.cc index 58803b30bcc96b..a5902db0454eba 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -49,6 +49,7 @@ struct napi_env__ { bool has_instance_available; napi_extended_error_info last_error; int open_handle_scopes = 0; + int open_callback_scopes = 0; uv_loop_t* loop = nullptr; }; @@ -261,6 +262,18 @@ V8EscapableHandleScopeFromJsEscapableHandleScope( return reinterpret_cast(s); } +static +napi_callback_scope JsCallbackScopeFromV8CallbackScope( + node::CallbackScope* s) { + return reinterpret_cast(s); +} + +static +node::CallbackScope* V8CallbackScopeFromJsCallbackScope( + napi_callback_scope s) { + return reinterpret_cast(s); +} + //=== Conversion between V8 Handles and napi_value ======================== // This asserts v8::Local<> will always be implemented with a single @@ -552,6 +565,7 @@ class CallbackWrapperBase : public CallbackWrapper { napi_clear_last_error(env); int open_handle_scopes = env->open_handle_scopes; + int open_callback_scopes = env->open_callback_scopes; napi_value result = cb(env, cbinfo_wrapper); @@ -560,6 +574,7 @@ class CallbackWrapperBase : public CallbackWrapper { } CHECK_EQ(env->open_handle_scopes, open_handle_scopes); + CHECK_EQ(env->open_callback_scopes, open_callback_scopes); if (!env->last_exception.IsEmpty()) { isolate->ThrowException( @@ -917,7 +932,8 @@ const char* error_messages[] = {nullptr, "An exception is pending", "The async work item was cancelled", "napi_escape_handle already called on scope", - "Invalid handle scope usage"}; + "Invalid handle scope usage", + "Invalid callback scope usage"}; static inline napi_status napi_clear_last_error(napi_env env) { env->last_error.error_code = napi_ok; @@ -948,9 +964,9 @@ napi_status napi_get_last_error_info(napi_env env, // We don't have a napi_status_last as this would result in an ABI // change each time a message was added. static_assert( - node::arraysize(error_messages) == napi_handle_scope_mismatch + 1, + node::arraysize(error_messages) == napi_callback_scope_mismatch + 1, "Count of error messages must match count of error values"); - CHECK_LE(env->last_error.error_code, napi_handle_scope_mismatch); + CHECK_LE(env->last_error.error_code, napi_callback_scope_mismatch); // Wait until someone requests the last error information to fetch the error // message string @@ -2639,6 +2655,46 @@ napi_status napi_escape_handle(napi_env env, return napi_set_last_error(env, napi_escape_called_twice); } +napi_status napi_open_callback_scope(napi_env env, + napi_value resource_object, + napi_async_context async_context_handle, + napi_callback_scope* result) { + // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw + // JS exceptions. + CHECK_ENV(env); + CHECK_ARG(env, result); + + v8::Local context = env->isolate->GetCurrentContext(); + + node::async_context* node_async_context = + reinterpret_cast(async_context_handle); + + v8::Local resource; + CHECK_TO_OBJECT(env, context, resource, resource_object); + + *result = v8impl::JsCallbackScopeFromV8CallbackScope( + new node::CallbackScope(env->isolate, + resource, + *node_async_context)); + + env->open_callback_scopes++; + return napi_clear_last_error(env); +} + +napi_status napi_close_callback_scope(napi_env env, napi_callback_scope scope) { + // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw + // JS exceptions. + CHECK_ENV(env); + CHECK_ARG(env, scope); + if (env->open_callback_scopes == 0) { + return napi_callback_scope_mismatch; + } + + env->open_callback_scopes--; + delete v8impl::V8CallbackScopeFromJsCallbackScope(scope); + return napi_clear_last_error(env); +} + napi_status napi_new_instance(napi_env env, napi_value constructor, size_t argc, diff --git a/src/node_api.h b/src/node_api.h index 8e5eef8a47728f..3b741c27b976ae 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -424,6 +424,14 @@ NAPI_EXTERN napi_status napi_escape_handle(napi_env env, napi_value escapee, napi_value* result); +NAPI_EXTERN napi_status napi_open_callback_scope(napi_env env, + napi_value resource_object, + napi_async_context context, + napi_callback_scope* result); + +NAPI_EXTERN napi_status napi_close_callback_scope(napi_env env, + napi_callback_scope scope); + // Methods to support error handling NAPI_EXTERN napi_status napi_throw(napi_env env, napi_value error); NAPI_EXTERN napi_status napi_throw_error(napi_env env, diff --git a/src/node_api_types.h b/src/node_api_types.h index 230c1f4ae3446f..76f38802e83e2e 100644 --- a/src/node_api_types.h +++ b/src/node_api_types.h @@ -15,6 +15,7 @@ typedef struct napi_value__ *napi_value; typedef struct napi_ref__ *napi_ref; typedef struct napi_handle_scope__ *napi_handle_scope; typedef struct napi_escapable_handle_scope__ *napi_escapable_handle_scope; +typedef struct napi_callback_scope__ *napi_callback_scope; typedef struct napi_callback_info__ *napi_callback_info; typedef struct napi_async_context__ *napi_async_context; typedef struct napi_async_work__ *napi_async_work; @@ -70,7 +71,8 @@ typedef enum { napi_pending_exception, napi_cancelled, napi_escape_called_twice, - napi_handle_scope_mismatch + napi_handle_scope_mismatch, + napi_callback_scope_mismatch } napi_status; typedef napi_value (*napi_callback)(napi_env env, diff --git a/test/addons-napi/test_callback_scope/binding.cc b/test/addons-napi/test_callback_scope/binding.cc new file mode 100644 index 00000000000000..e6631b6ac7bb52 --- /dev/null +++ b/test/addons-napi/test_callback_scope/binding.cc @@ -0,0 +1,138 @@ +#include "node_api.h" +#include "uv.h" +#include "../common.h" + +namespace { + +// the test needs to fake out the async structure, so we need to use +// the raw structure here and then cast as done behind the scenes +// in napi calls. +struct async_context { + double async_id; + double trigger_async_id; +}; + + +napi_value RunInCallbackScope(napi_env env, napi_callback_info info) { + size_t argc; + napi_value args[4]; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr)); + NAPI_ASSERT(env, argc == 4 , "Wrong number of arguments"); + + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); + + napi_valuetype valuetype; + NAPI_CALL(env, napi_typeof(env, args[0], &valuetype)); + NAPI_ASSERT(env, valuetype == napi_object, + "Wrong type of arguments. Expects an object as first argument."); + + NAPI_CALL(env, napi_typeof(env, args[1], &valuetype)); + NAPI_ASSERT(env, valuetype == napi_number, + "Wrong type of arguments. Expects a number as second argument."); + + NAPI_CALL(env, napi_typeof(env, args[2], &valuetype)); + NAPI_ASSERT(env, valuetype == napi_number, + "Wrong type of arguments. Expects a number as third argument."); + + NAPI_CALL(env, napi_typeof(env, args[3], &valuetype)); + NAPI_ASSERT(env, valuetype == napi_function, + "Wrong type of arguments. Expects a function as third argument."); + + struct async_context context; + NAPI_CALL(env, napi_get_value_double(env, args[1], &context.async_id)); + NAPI_CALL(env, + napi_get_value_double(env, args[2], &context.trigger_async_id)); + + napi_callback_scope scope = nullptr; + NAPI_CALL( + env, + napi_open_callback_scope(env, + args[0], + reinterpret_cast(&context), + &scope)); + + // if the function has an exception pending after the call that is ok + // so we don't use NAPI_CALL as we must close the callback scope regardless + napi_value result = nullptr; + napi_status function_call_result = + napi_call_function(env, args[0], args[3], 0, nullptr, &result); + if (function_call_result != napi_ok) { + GET_AND_THROW_LAST_ERROR((env)); + } + + NAPI_CALL(env, napi_close_callback_scope(env, scope)); + + return result; +} + +static napi_env shared_env = nullptr; +static napi_deferred deferred = nullptr; + +static void Callback(uv_work_t* req, int ignored) { + napi_env env = shared_env; + + napi_handle_scope handle_scope = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_open_handle_scope(env, &handle_scope)); + + napi_value resource_name; + NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8( + env, "test", NAPI_AUTO_LENGTH, &resource_name)); + napi_async_context context; + NAPI_CALL_RETURN_VOID(env, + napi_async_init(env, nullptr, resource_name, &context)); + + napi_value resource_object; + NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &resource_object)); + + napi_value undefined_value; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined_value)); + + napi_callback_scope scope = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_open_callback_scope(env, + resource_object, + context, + &scope)); + + NAPI_CALL_RETURN_VOID(env, + napi_resolve_deferred(env, deferred, undefined_value)); + + NAPI_CALL_RETURN_VOID(env, napi_close_callback_scope(env, scope)); + + NAPI_CALL_RETURN_VOID(env, napi_close_handle_scope(env, handle_scope)); + delete req; +} + +napi_value TestResolveAsync(napi_env env, napi_callback_info info) { + napi_value promise = nullptr; + if (deferred == nullptr) { + shared_env = env; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + + uv_loop_t* loop = nullptr; + NAPI_CALL(env, napi_get_uv_event_loop(env, &loop)); + + uv_work_t* req = new uv_work_t(); + uv_queue_work(loop, + req, + [](uv_work_t*) {}, + Callback); + } + return promise; +} + +napi_value Init(napi_env env, napi_value exports) { + napi_property_descriptor descriptors[] = { + DECLARE_NAPI_PROPERTY("runInCallbackScope", RunInCallbackScope), + DECLARE_NAPI_PROPERTY("testResolveAsync", TestResolveAsync) + }; + + NAPI_CALL(env, napi_define_properties( + env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); + + return exports; +} + +} // anonymous namespace + +NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) diff --git a/test/addons-napi/test_callback_scope/binding.gyp b/test/addons-napi/test_callback_scope/binding.gyp new file mode 100644 index 00000000000000..7ede63d94a0d77 --- /dev/null +++ b/test/addons-napi/test_callback_scope/binding.gyp @@ -0,0 +1,9 @@ +{ + 'targets': [ + { + 'target_name': 'binding', + 'defines': [ 'V8_DEPRECATION_WARNINGS=1' ], + 'sources': [ 'binding.cc' ] + } + ] +} diff --git a/test/addons-napi/test_callback_scope/test-resolve-async.js b/test/addons-napi/test_callback_scope/test-resolve-async.js new file mode 100644 index 00000000000000..e9f4b9044c0154 --- /dev/null +++ b/test/addons-napi/test_callback_scope/test-resolve-async.js @@ -0,0 +1,13 @@ +'use strict'; + +const common = require('../../common'); +const assert = require('assert'); +const { testResolveAsync } = require(`./build/${common.buildType}/binding`); + +let called = false; +testResolveAsync().then(common.mustCall(() => { + called = true; +})); + +setTimeout(common.mustCall(() => { assert(called); }), + common.platformTimeout(20)); diff --git a/test/addons-napi/test_callback_scope/test.js b/test/addons-napi/test_callback_scope/test.js new file mode 100644 index 00000000000000..2f2efe5f47b98a --- /dev/null +++ b/test/addons-napi/test_callback_scope/test.js @@ -0,0 +1,17 @@ +'use strict'; + +const common = require('../../common'); +const assert = require('assert'); +const { runInCallbackScope } = require(`./build/${common.buildType}/binding`); + +assert.strictEqual(runInCallbackScope({}, 0, 0, () => 42), 42); + +{ + process.once('uncaughtException', common.mustCall((err) => { + assert.strictEqual(err.message, 'foo'); + })); + + runInCallbackScope({}, 0, 0, () => { + throw new Error('foo'); + }); +} From d75fbebafd82c7e22d73283f8d9722026a2c3233 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Fri, 2 Feb 2018 20:34:19 -0500 Subject: [PATCH 160/183] n-api: remove extra reference from test PR-URL: https://github.com/nodejs/node/pull/18542 Reviewed-By: Michael Dawson Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Colin Ihrig --- test/addons-napi/test_constructor/test_constructor.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/addons-napi/test_constructor/test_constructor.c b/test/addons-napi/test_constructor/test_constructor.c index 43623d8b60a2ef..bed61cc55cd5e9 100644 --- a/test/addons-napi/test_constructor/test_constructor.c +++ b/test/addons-napi/test_constructor/test_constructor.c @@ -3,7 +3,6 @@ static double value_ = 1; static double static_value_ = 10; -napi_ref constructor_; napi_value GetValue(napi_env env, napi_callback_info info) { size_t argc = 0; @@ -80,8 +79,6 @@ napi_value Init(napi_env env, napi_value exports) { NAPI_CALL(env, napi_define_class(env, "MyObject", NAPI_AUTO_LENGTH, New, NULL, sizeof(properties)/sizeof(*properties), properties, &cons)); - NAPI_CALL(env, napi_create_reference(env, cons, 1, &constructor_)); - return cons; } From 475336904e47f4e62296a1479693dc2c046812db Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Tue, 6 Feb 2018 02:00:12 +0200 Subject: [PATCH 161/183] doc: fix typo in n-api.md PR-URL: https://github.com/nodejs/node/pull/18590 Reviewed-By: Jon Moss Reviewed-By: Weijia Wang Reviewed-By: Colin Ihrig Reviewed-By: Rich Trott Reviewed-By: Daniel Bevenius Reviewed-By: Yuta Hiroto --- doc/api/n-api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 388fa0f95d0086..d965f3a4693884 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -3462,7 +3462,7 @@ invoking the callback. This should be a value previously obtained from [`napi_async_init`][]. - `[out] result`: The newly created scope. -There are cases(for example resolving promises) where it is +There are cases (for example resolving promises) where it is necessary to have the equivalent of the scope associated with a callback in place when making certain N-API calls. If there is no other script on the stack the [`napi_open_callback_scope`][] and From c7f4a4f9a7a8f16b7728df1155015d323c8b06da Mon Sep 17 00:00:00 2001 From: Bhavani Shankar Date: Thu, 1 Feb 2018 15:32:41 +0530 Subject: [PATCH 162/183] test: improve error message output PR-URL: https://github.com/nodejs/node/pull/18498 Reviewed-By: Ruben Bridgewater Reviewed-By: Anatoli Papirovski Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Reviewed-By: Gireesh Punathil Reviewed-By: Michael Dawson --- test/addons-napi/test_general/test.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/test/addons-napi/test_general/test.js b/test/addons-napi/test_general/test.js index 15461cef2f6a36..d43a1c69ba0c43 100644 --- a/test/addons-napi/test_general/test.js +++ b/test/addons-napi/test_general/test.js @@ -63,22 +63,25 @@ let w = {}; test_general.wrap(w); w = null; global.gc(); -assert.strictEqual(test_general.derefItemWasCalled(), true, +const derefItemWasCalled = test_general.derefItemWasCalled(); +assert.strictEqual(derefItemWasCalled, true, 'deref_item() was called upon garbage collecting a ' + - 'wrapped object'); + 'wrapped object. test_general.derefItemWasCalled() ' + + `returned ${derefItemWasCalled}`); + // Assert that wrapping twice fails. const x = {}; test_general.wrap(x); -assert.throws(() => test_general.wrap(x), Error); +common.expectsError(() => test_general.wrap(x), + { type: Error, message: 'Invalid argument' }); // Ensure that wrapping, removing the wrap, and then wrapping again works. const y = {}; test_general.wrap(y); test_general.removeWrap(y); -assert.doesNotThrow(() => test_general.wrap(y), Error, - 'Wrapping twice succeeds if a remove_wrap()' + - ' separates the instances'); +// Wrapping twice succeeds if a remove_wrap() separates the instances +assert.doesNotThrow(() => test_general.wrap(y)); // Ensure that removing a wrap and garbage collecting does not fire the // finalize callback. @@ -87,8 +90,11 @@ test_general.testFinalizeWrap(z); test_general.removeWrap(z); z = null; global.gc(); -assert.strictEqual(test_general.finalizeWasCalled(), false, - 'finalize callback was not called upon garbage collection'); +const finalizeWasCalled = test_general.finalizeWasCalled(); +assert.strictEqual(finalizeWasCalled, false, + 'finalize callback was not called upon garbage collection.' + + ' test_general.finalizeWasCalled() ' + + `returned ${finalizeWasCalled}`); // test napi_adjust_external_memory const adjustedValue = test_general.testAdjustExternalMemory(); From 85728d176fffdcfb3aad3867a7b5e89dba4936f1 Mon Sep 17 00:00:00 2001 From: Jack Horton Date: Mon, 5 Feb 2018 11:00:33 -0800 Subject: [PATCH 163/183] test: convert new tests to use error types PR-URL: https://github.com/nodejs/node/pull/18581 Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig Reviewed-By: Luigi Pinca --- test/addons-napi/test_typedarray/test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/addons-napi/test_typedarray/test.js b/test/addons-napi/test_typedarray/test.js index 4a4e79ebe7bcdb..4c139d92200fe3 100644 --- a/test/addons-napi/test_typedarray/test.js +++ b/test/addons-napi/test_typedarray/test.js @@ -60,7 +60,7 @@ arrayTypes.forEach((currentType) => { const template = Reflect.construct(currentType, buffer); assert.throws(() => { test_typedarray.CreateTypedArray(template, buffer, 0, 136); - }, /Invalid typed array length/); + }, RangeError); }); const nonByteArrayTypes = [ Int16Array, Uint16Array, Int32Array, Uint32Array, @@ -71,5 +71,5 @@ nonByteArrayTypes.forEach((currentType) => { test_typedarray.CreateTypedArray(template, buffer, currentType.BYTES_PER_ELEMENT + 1, 1); console.log(`start of offset ${currentType}`); - }, /start offset of/); + }, RangeError); }); From 495077256e0da9251d79568383332df8397259f4 Mon Sep 17 00:00:00 2001 From: Aonghus O Nia Date: Thu, 8 Feb 2018 17:01:23 -0500 Subject: [PATCH 164/183] doc: fix exporting a function example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Missing the length argument in napi_create_function. PR-URL: https://github.com/nodejs/node/pull/18661 Reviewed-By: Michael Dawson Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Tobias Nießen --- doc/api/n-api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index d965f3a4693884..650c4f96baf9eb 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -916,7 +916,7 @@ For example, to set a function to be returned by the `require()` for the addon: napi_value Init(napi_env env, napi_value exports) { napi_value method; napi_status status; - status = napi_create_function(env, "exports", Method, NULL, &method); + status = napi_create_function(env, "exports", NAPI_AUTO_LENGTH, Method, NULL, &method); if (status != napi_ok) return NULL; return method; } From 799646388585b62a3b3b0c50b6382a9440b25ac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Sat, 10 Feb 2018 16:32:01 +0100 Subject: [PATCH 165/183] doc: mark NAPI_AUTO_LENGTH as code PR-URL: https://github.com/nodejs/node/pull/18697 Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig Reviewed-By: Jeremiah Senkpiel Reviewed-By: Luigi Pinca --- doc/api/n-api.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 650c4f96baf9eb..cae1dab65ded70 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -552,10 +552,10 @@ NAPI_NO_RETURN void napi_fatal_error(const char* location, - `[in] location`: Optional location at which the error occurred. - `[in] location_len`: The length of the location in bytes, or -NAPI_AUTO_LENGTH if it is null-terminated. +`NAPI_AUTO_LENGTH` if it is null-terminated. - `[in] message`: The message associated with the error. - `[in] message_len`: The length of the message in bytes, or -NAPI_AUTO_LENGTH if it is +`NAPI_AUTO_LENGTH` if it is null-terminated. The function call does not return, the process will be terminated. @@ -1258,7 +1258,7 @@ napi_status napi_create_function(napi_env env, - `[in] utf8name`: A string representing the name of the function encoded as UTF8. - `[in] length`: The length of the utf8name in bytes, or -NAPI_AUTO_LENGTH if it is null-terminated. +`NAPI_AUTO_LENGTH` if it is null-terminated. - `[in] cb`: A function pointer to the native function to be invoked when the created function is invoked from JavaScript. - `[in] data`: Optional arbitrary context data to be passed into the native @@ -1491,7 +1491,7 @@ napi_status napi_create_string_latin1(napi_env env, - `[in] env`: The environment that the API is invoked under. - `[in] str`: Character buffer representing a ISO-8859-1-encoded string. - `[in] length`: The length of the string in bytes, or -NAPI_AUTO_LENGTH if it is null-terminated. +`NAPI_AUTO_LENGTH` if it is null-terminated. - `[out] result`: A `napi_value` representing a JavaScript String. Returns `napi_ok` if the API succeeded. @@ -1516,7 +1516,7 @@ napi_status napi_create_string_utf16(napi_env env, - `[in] env`: The environment that the API is invoked under. - `[in] str`: Character buffer representing a UTF16-LE-encoded string. - `[in] length`: The length of the string in two-byte code units, or -NAPI_AUTO_LENGTH if it is null-terminated. +`NAPI_AUTO_LENGTH` if it is null-terminated. - `[out] result`: A `napi_value` representing a JavaScript String. Returns `napi_ok` if the API succeeded. @@ -1540,7 +1540,7 @@ napi_status napi_create_string_utf8(napi_env env, - `[in] env`: The environment that the API is invoked under. - `[in] str`: Character buffer representing a UTF8-encoded string. -- `[in] length`: The length of the string in bytes, or NAPI_AUTO_LENGTH +- `[in] length`: The length of the string in bytes, or `NAPI_AUTO_LENGTH` if it is null-terminated. - `[out] result`: A `napi_value` representing a JavaScript String. @@ -3066,7 +3066,7 @@ napi_status napi_define_class(napi_env env, - `[in] utf8name`: Name of the JavaScript constructor function; this is not required to be the same as the C++ class name, though it is recommended for clarity. - - `[in] length`: The length of the utf8name in bytes, or NAPI_AUTO_LENGTH + - `[in] length`: The length of the utf8name in bytes, or `NAPI_AUTO_LENGTH` if it is null-terminated. - `[in] constructor`: Callback function that handles constructing instances of the class. (This should be a static method on the class, not an actual From a12f442c0f1e3469cbb73f2101f4ae18d01f6e22 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sun, 11 Feb 2018 17:07:39 -0500 Subject: [PATCH 166/183] test: remove unnecessary timer The timer in NAPI's test_callback_scope/test-resolve-async.js can be removed. If the test fails, it will timeout on its own. The extra timer increases the chances of the test being flaky. PR-URL: https://github.com/nodejs/node/pull/18719 Fixes: https://github.com/nodejs/node/issues/18702 Reviewed-By: Ruben Bridgewater Reviewed-By: Santiago Gimeno Reviewed-By: Luigi Pinca Reviewed-By: James M Snell --- .../test_callback_scope/test-resolve-async.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/test/addons-napi/test_callback_scope/test-resolve-async.js b/test/addons-napi/test_callback_scope/test-resolve-async.js index e9f4b9044c0154..77f25c9dde533f 100644 --- a/test/addons-napi/test_callback_scope/test-resolve-async.js +++ b/test/addons-napi/test_callback_scope/test-resolve-async.js @@ -1,13 +1,6 @@ 'use strict'; const common = require('../../common'); -const assert = require('assert'); const { testResolveAsync } = require(`./build/${common.buildType}/binding`); -let called = false; -testResolveAsync().then(common.mustCall(() => { - called = true; -})); - -setTimeout(common.mustCall(() => { assert(called); }), - common.platformTimeout(20)); +testResolveAsync().then(common.mustCall()); From 87863351b50e22d537c7b44fd238d883d4d9f0c9 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Tue, 27 Feb 2018 13:02:24 -0500 Subject: [PATCH 167/183] n-api: fix object test Passing a pointer to a static integer is sufficient for the test. PR-URL: https://github.com/nodejs/node/pull/19039 Reviewed-By: Colin Ihrig Reviewed-By: Michael Dawson Reviewed-By: Hitesh Kanwathirtha --- test/addons-napi/test_object/test_object.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/addons-napi/test_object/test_object.c b/test/addons-napi/test_object/test_object.c index 75e0ee4c30f85c..a84da27365ff2e 100644 --- a/test/addons-napi/test_object/test_object.c +++ b/test/addons-napi/test_object/test_object.c @@ -1,7 +1,6 @@ #include #include "../common.h" #include -#include static int test_value = 3; @@ -199,9 +198,7 @@ napi_value Wrap(napi_env env, napi_callback_info info) { napi_value arg; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &arg, NULL, NULL)); - int32_t* data = malloc(sizeof(int32_t)); - *data = test_value; - NAPI_CALL(env, napi_wrap(env, arg, data, NULL, NULL, NULL)); + NAPI_CALL(env, napi_wrap(env, arg, &test_value, NULL, NULL, NULL)); return NULL; } From 0efd8267fcc6e0f7228092f87f9701f9c00fff32 Mon Sep 17 00:00:00 2001 From: Eric Bickle Date: Thu, 1 Mar 2018 09:27:16 -0800 Subject: [PATCH 168/183] doc: fix n-api asynchronous threading docs Documentation for N-API Custom Asynchronous Operations incorrectly stated that async execution happens on the main event loop. Added details to napi_create_async_work about which threads are used to invoke the execute and complete callbacks. Changed 'async' to 'asynchronous' in the documentation for Custom Asynchronous Operations. Changed "executes in parallel" to "can execute in parallel" for the documentation of napi_create_async_work execute parameter. PR-URL: https://github.com/nodejs/node/pull/19073 Fixes: https://github.com/nodejs/node/issues/19071 Reviewed-By: Michael Dawson Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- doc/api/n-api.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index cae1dab65ded70..4c555554405331 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -3281,9 +3281,11 @@ napi_status napi_create_async_work(napi_env env, - `[in] async_resource_name`: Identifier for the kind of resource that is being provided for diagnostic information exposed by the `async_hooks` API. - `[in] execute`: The native function which should be called to excute -the logic asynchronously. +the logic asynchronously. The given function is called from a worker pool +thread and can execute in parallel with the main event loop thread. - `[in] complete`: The native function which will be called when the -asynchronous logic is comple or is cancelled. +asynchronous logic is completed or is cancelled. The given function is called +from the main event loop thread. - `[in] data`: User-provided data context. This will be passed back into the execute and complete functions. - `[out] result`: `napi_async_work*` which is the handle to the newly created @@ -3359,9 +3361,9 @@ callback invocation, even if it has been successfully cancelled. ## Custom Asynchronous Operations The simple asynchronous work APIs above may not be appropriate for every -scenario, because with those the async execution still happens on the main -event loop. When using any other async mechanism, the following APIs are -necessary to ensure an async operation is properly tracked by the runtime. +scenario. When using any other asynchronous mechanism, the following APIs +are necessary to ensure an asynchronous operation is properly tracked by +the runtime. ### napi_async_init +```C +napi_status napi_fatal_exception(napi_env env, napi_value err); +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] err`: The error you want to pass to `uncaughtException`. + +Trigger an `uncaughtException` in JavaScript. Useful if an async +callback throws an exception with no way to recover. + ### Fatal Errors In the event of an unrecoverable error in a native module, a fatal error can be diff --git a/src/node_api.cc b/src/node_api.cc index b153aa5f3c3bf6..a52ecef27c4c27 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -293,6 +293,13 @@ v8::Local V8LocalValueFromJsValue(napi_value v) { return local; } +static inline void trigger_fatal_exception( + napi_env env, v8::Local local_err) { + v8::Local local_msg = + v8::Exception::CreateMessage(env->isolate, local_err); + node::FatalException(env->isolate, local_err, local_msg); +} + static inline napi_status V8NameFromPropertyDescriptor(napi_env env, const napi_property_descriptor* p, v8::Local* result) { @@ -967,6 +974,16 @@ napi_status napi_get_last_error_info(napi_env env, return napi_ok; } +napi_status napi_fatal_exception(napi_env env, napi_value err) { + NAPI_PREAMBLE(env); + CHECK_ARG(env, err); + + v8::Local local_err = v8impl::V8LocalValueFromJsValue(err); + v8impl::trigger_fatal_exception(env, local_err); + + return napi_clear_last_error(env); +} + NAPI_NO_RETURN void napi_fatal_error(const char* location, size_t location_len, const char* message, @@ -3457,10 +3474,9 @@ class Work : public node::AsyncResource { // report it as a fatal exception. (There is no JavaScript on the // callstack that can possibly handle it.) if (!env->last_exception.IsEmpty()) { - v8::TryCatch try_catch(env->isolate); - env->isolate->ThrowException( - v8::Local::New(env->isolate, env->last_exception)); - node::FatalException(env->isolate, try_catch); + v8::Local local_err = v8::Local::New( + env->isolate, env->last_exception); + v8impl::trigger_fatal_exception(env, local_err); } } } diff --git a/src/node_api.h b/src/node_api.h index c5b8a2a30cfe15..a2a73d5bcfd8f5 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -103,6 +103,8 @@ NAPI_EXTERN napi_status napi_get_last_error_info(napi_env env, const napi_extended_error_info** result); +NAPI_EXTERN napi_status napi_fatal_exception(napi_env env, napi_value err); + NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location, size_t location_len, const char* message, diff --git a/src/node_internals.h b/src/node_internals.h index de1b8adc87f4d3..bfb9bf296d7285 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -118,6 +118,11 @@ void GetSockOrPeerName(const v8::FunctionCallbackInfo& args) { args.GetReturnValue().Set(err); } +void FatalException(v8::Isolate* isolate, + v8::Local error, + v8::Local message); + + void SignalExit(int signo); #ifdef __POSIX__ void RegisterSignalHandler(int signal, diff --git a/test/addons-napi/test_async/test-uncaught.js b/test/addons-napi/test_async/test-uncaught.js new file mode 100644 index 00000000000000..fdcb3203f54410 --- /dev/null +++ b/test/addons-napi/test_async/test-uncaught.js @@ -0,0 +1,18 @@ +'use strict'; +const common = require('../../common'); +const assert = require('assert'); +const test_async = require(`./build/${common.buildType}/test_async`); + +process.on('uncaughtException', common.mustCall(function(err) { + try { + throw new Error('should not fail'); + } catch (err) { + assert.strictEqual(err.message, 'should not fail'); + } + assert.strictEqual(err.message, 'uncaught'); +})); + +// Successful async execution and completion callback. +test_async.Test(5, {}, common.mustCall(function() { + throw new Error('uncaught'); +})); diff --git a/test/addons-napi/test_fatal_exception/binding.gyp b/test/addons-napi/test_fatal_exception/binding.gyp new file mode 100644 index 00000000000000..f4dc0a71ea2817 --- /dev/null +++ b/test/addons-napi/test_fatal_exception/binding.gyp @@ -0,0 +1,8 @@ +{ + "targets": [ + { + "target_name": "test_fatal_exception", + "sources": [ "test_fatal_exception.c" ] + } + ] +} diff --git a/test/addons-napi/test_fatal_exception/test.js b/test/addons-napi/test_fatal_exception/test.js new file mode 100644 index 00000000000000..f02b9bce1e8169 --- /dev/null +++ b/test/addons-napi/test_fatal_exception/test.js @@ -0,0 +1,11 @@ +'use strict'; +const common = require('../../common'); +const assert = require('assert'); +const test_fatal = require(`./build/${common.buildType}/test_fatal_exception`); + +process.on('uncaughtException', common.mustCall(function(err) { + assert.strictEqual(err.message, 'fatal error'); +})); + +const err = new Error('fatal error'); +test_fatal.Test(err); diff --git a/test/addons-napi/test_fatal_exception/test_fatal_exception.c b/test/addons-napi/test_fatal_exception/test_fatal_exception.c new file mode 100644 index 00000000000000..fd81c56d856db8 --- /dev/null +++ b/test/addons-napi/test_fatal_exception/test_fatal_exception.c @@ -0,0 +1,26 @@ +#include +#include "../common.h" + +napi_value Test(napi_env env, napi_callback_info info) { + napi_value err; + size_t argc = 1; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &err, NULL, NULL)); + + NAPI_CALL(env, napi_fatal_exception(env, err)); + + return NULL; +} + +napi_value Init(napi_env env, napi_value exports) { + napi_property_descriptor properties[] = { + DECLARE_NAPI_PROPERTY("Test", Test), + }; + + NAPI_CALL(env, napi_define_properties( + env, exports, sizeof(properties) / sizeof(*properties), properties)); + + return exports; +} + +NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) From 6b1201644c5d5cc624bbfad0abdad3f9a266f34b Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Mon, 19 Mar 2018 11:59:41 -0400 Subject: [PATCH 178/183] n-api: re-write test_make_callback This re-writes the test in C by dropping std::vector in favour of a C99 variable length array, and by dropping the anonymous namespace in favour of static function declarations. PR-URL: https://github.com/nodejs/node/pull/19448 Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Michael Dawson --- .../{binding.cc => binding.c} | 21 +++++++++---------- .../test_make_callback/binding.gyp | 2 +- 2 files changed, 11 insertions(+), 12 deletions(-) rename test/addons-napi/test_make_callback/{binding.cc => binding.c} (79%) diff --git a/test/addons-napi/test_make_callback/binding.cc b/test/addons-napi/test_make_callback/binding.c similarity index 79% rename from test/addons-napi/test_make_callback/binding.cc rename to test/addons-napi/test_make_callback/binding.c index 18e558aecf8d33..f893319987cb89 100644 --- a/test/addons-napi/test_make_callback/binding.cc +++ b/test/addons-napi/test_make_callback/binding.c @@ -1,13 +1,13 @@ #include #include "../common.h" -#include -namespace { +#define MAX_ARGUMENTS 10 +static napi_value MakeCallback(napi_env env, napi_callback_info info) { - const int kMaxArgs = 10; - size_t argc = kMaxArgs; - napi_value args[kMaxArgs]; + size_t argc = MAX_ARGUMENTS; + size_t n; + napi_value args[MAX_ARGUMENTS]; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); NAPI_ASSERT(env, argc > 0, "Wrong number of arguments"); @@ -15,9 +15,9 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) { napi_value recv = args[0]; napi_value func = args[1]; - std::vector argv; - for (size_t n = 2; n < argc; n += 1) { - argv.push_back(args[n]); + napi_value argv[MAX_ARGUMENTS - 2]; + for (n = 2; n < argc; n += 1) { + argv[n - 2] = args[n]; } napi_valuetype func_type; @@ -34,7 +34,7 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) { napi_value result; if (func_type == napi_function) { NAPI_CALL(env, napi_make_callback( - env, context, recv, func, argv.size(), argv.data(), &result)); + env, context, recv, func, argc - 2, argv, &result)); } else { NAPI_ASSERT(env, false, "Unexpected argument type"); } @@ -44,6 +44,7 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) { return result; } +static napi_value Init(napi_env env, napi_value exports) { napi_value fn; NAPI_CALL(env, napi_create_function( @@ -52,6 +53,4 @@ napi_value Init(napi_env env, napi_value exports) { return exports; } -} // namespace - NAPI_MODULE(binding, Init) diff --git a/test/addons-napi/test_make_callback/binding.gyp b/test/addons-napi/test_make_callback/binding.gyp index 7ede63d94a0d77..23daf507916ff6 100644 --- a/test/addons-napi/test_make_callback/binding.gyp +++ b/test/addons-napi/test_make_callback/binding.gyp @@ -3,7 +3,7 @@ { 'target_name': 'binding', 'defines': [ 'V8_DEPRECATION_WARNINGS=1' ], - 'sources': [ 'binding.cc' ] + 'sources': [ 'binding.c' ] } ] } From 041c9a0a2bc68bf9bd23d8c9fb5eb0d00223b203 Mon Sep 17 00:00:00 2001 From: jiangq Date: Fri, 23 Mar 2018 22:28:39 +0800 Subject: [PATCH 179/183] doc: Add a missing comma PR-URL: https://github.com/nodejs/node/pull/19555 Reviewed-By: Colin Ihrig Reviewed-By: Trivikram Kamat --- doc/api/n-api.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 4245c28fb046c0..e176cbdf1ef9cd 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -608,7 +608,7 @@ that has a loop which iterates through the elements in a large array: ```C for (int i = 0; i < 1000000; i++) { napi_value result; - napi_status status = napi_get_element(e object, i, &result); + napi_status status = napi_get_element(e, object, i, &result); if (status != napi_ok) { break; } @@ -645,7 +645,7 @@ for (int i = 0; i < 1000000; i++) { break; } napi_value result; - status = napi_get_element(e object, i, &result); + status = napi_get_element(e, object, i, &result); if (status != napi_ok) { break; } From 96fe46b91c82d27601e158f3d9acf63eebf2a5b6 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Tue, 20 Mar 2018 17:14:24 -0400 Subject: [PATCH 180/183] n-api: bump version of n-api supported Bump the version due to additions to the api. PR-URL: https://github.com/nodejs/node/pull/19497 Reviewed-By: Colin Ihrig Reviewed-By: Trivikram Kamat Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- src/node_version.h | 2 +- test/addons-napi/test_general/test.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/node_version.h b/src/node_version.h index 843e5b524d533f..e71ed35478e3b7 100644 --- a/src/node_version.h +++ b/src/node_version.h @@ -62,6 +62,6 @@ #define NODE_MODULE_VERSION 48 /* Node.js v6.0.0 */ // the NAPI_VERSION provided by this version of the runtime -#define NAPI_VERSION 2 +#define NAPI_VERSION 3 #endif // SRC_NODE_VERSION_H_ diff --git a/test/addons-napi/test_general/test.js b/test/addons-napi/test_general/test.js index d43a1c69ba0c43..4faf508d5db145 100644 --- a/test/addons-napi/test_general/test.js +++ b/test/addons-napi/test_general/test.js @@ -32,9 +32,9 @@ assert.strictEqual(test_general.testGetPrototype(extendedObject), assert.notStrictEqual(test_general.testGetPrototype(baseObject), test_general.testGetPrototype(extendedObject)); -// test version management funcitons -// expected version is currently 1 -assert.strictEqual(test_general.testGetVersion(), 2); +// test version management functions +// expected version is currently 3 +assert.strictEqual(test_general.testGetVersion(), 3); const [ major, minor, patch, release ] = test_general.testGetNodeVersion(); assert.strictEqual(process.version.split('-')[0], From f4b0f7a49eae9ef18b33039471656d17068e4454 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Thu, 22 Mar 2018 17:01:37 -0400 Subject: [PATCH 181/183] n-api: ensure in-module exceptions are propagated Whenever we call into an addon, whether it is for a callback, for module init, or for async work-related reasons, we should make sure that * the last error is cleared, * the scopes before the call are the same as after, and * if an exception was thrown and captured inside the module, then it is re-thrown after the call. Therefore we should call into the module in a unified fashion. This change introduces the macro NAPI_CALL_INTO_MODULE() which should be used whenever invoking a callback provided by the module. Fixes: https://github.com/nodejs/node/issues/19437 PR-URL: https://github.com/nodejs/node/pull/19537 Reviewed-By: James M Snell Reviewed-By: Michael Dawson --- src/node_api.cc | 79 ++++++++++--------- test/addons-napi/test_exception/test.js | 30 ++++++- .../test_exception/test_exception.c | 42 ++++++++-- 3 files changed, 108 insertions(+), 43 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index a52ecef27c4c27..e4638e4559af97 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -168,6 +168,24 @@ struct napi_env__ { (out) = v8::type::New((buffer), (byte_offset), (length)); \ } while (0) +#define NAPI_CALL_INTO_MODULE(env, call, handle_exception) \ + do { \ + int open_handle_scopes = (env)->open_handle_scopes; \ + int open_callback_scopes = (env)->open_callback_scopes; \ + napi_clear_last_error((env)); \ + call; \ + CHECK_EQ((env)->open_handle_scopes, open_handle_scopes); \ + CHECK_EQ((env)->open_callback_scopes, open_callback_scopes); \ + if (!(env)->last_exception.IsEmpty()) { \ + handle_exception( \ + v8::Local::New((env)->isolate, (env)->last_exception)); \ + (env)->last_exception.Reset(); \ + } \ + } while (0) + +#define NAPI_CALL_INTO_MODULE_THROW(env, call) \ + NAPI_CALL_INTO_MODULE((env), call, (env)->isolate->ThrowException) + namespace v8impl { // convert from n-api property attributes to v8::PropertyAttribute @@ -349,10 +367,11 @@ class Finalizer { static void FinalizeBufferCallback(char* data, void* hint) { Finalizer* finalizer = static_cast(hint); if (finalizer->_finalize_callback != nullptr) { - finalizer->_finalize_callback( - finalizer->_env, - data, - finalizer->_finalize_hint); + NAPI_CALL_INTO_MODULE_THROW(finalizer->_env, + finalizer->_finalize_callback( + finalizer->_env, + data, + finalizer->_finalize_hint)); } Delete(finalizer); @@ -464,10 +483,11 @@ class Reference : private Finalizer { bool delete_self = reference->_delete_self; if (reference->_finalize_callback != nullptr) { - reference->_finalize_callback( - reference->_env, - reference->_finalize_data, - reference->_finalize_hint); + NAPI_CALL_INTO_MODULE_THROW(reference->_env, + reference->_finalize_callback( + reference->_env, + reference->_finalize_data, + reference->_finalize_hint)); } if (delete_self) { @@ -552,32 +572,17 @@ class CallbackWrapperBase : public CallbackWrapper { napi_callback cb = reinterpret_cast( v8::Local::Cast( _cbdata->GetInternalField(kInternalFieldIndex))->Value()); - v8::Isolate* isolate = _cbinfo.GetIsolate(); napi_env env = static_cast( v8::Local::Cast( _cbdata->GetInternalField(kEnvIndex))->Value()); - // Make sure any errors encountered last time we were in N-API are gone. - napi_clear_last_error(env); - - int open_handle_scopes = env->open_handle_scopes; - int open_callback_scopes = env->open_callback_scopes; - - napi_value result = cb(env, cbinfo_wrapper); + napi_value result; + NAPI_CALL_INTO_MODULE_THROW(env, result = cb(env, cbinfo_wrapper)); if (result != nullptr) { this->SetReturnValue(result); } - - CHECK_EQ(env->open_handle_scopes, open_handle_scopes); - CHECK_EQ(env->open_callback_scopes, open_callback_scopes); - - if (!env->last_exception.IsEmpty()) { - isolate->ThrowException( - v8::Local::New(isolate, env->last_exception)); - env->last_exception.Reset(); - } } const Info& _cbinfo; @@ -885,8 +890,10 @@ void napi_module_register_cb(v8::Local exports, // one is found. napi_env env = v8impl::GetEnv(context); - napi_value _exports = - mod->nm_register_func(env, v8impl::JsValueFromV8LocalValue(exports)); + napi_value _exports; + NAPI_CALL_INTO_MODULE_THROW(env, + _exports = mod->nm_register_func(env, + v8impl::JsValueFromV8LocalValue(exports))); // If register function returned a non-null exports object different from // the exports object we passed it, set that as the "exports" property of @@ -3465,19 +3472,17 @@ class Work : public node::AsyncResource { v8::HandleScope scope(env->isolate); CallbackScope callback_scope(work); - work->_complete(env, ConvertUVErrorCode(status), work->_data); + NAPI_CALL_INTO_MODULE(env, + work->_complete(env, ConvertUVErrorCode(status), work->_data), + [env] (v8::Local local_err) { + // If there was an unhandled exception in the complete callback, + // report it as a fatal exception. (There is no JavaScript on the + // callstack that can possibly handle it.) + v8impl::trigger_fatal_exception(env, local_err); + }); // Note: Don't access `work` after this point because it was // likely deleted by the complete callback. - - // If there was an unhandled exception in the complete callback, - // report it as a fatal exception. (There is no JavaScript on the - // callstack that can possibly handle it.) - if (!env->last_exception.IsEmpty()) { - v8::Local local_err = v8::Local::New( - env->isolate, env->last_exception); - v8impl::trigger_fatal_exception(env, local_err); - } } } diff --git a/test/addons-napi/test_exception/test.js b/test/addons-napi/test_exception/test.js index 787b7d78b1c72b..b9311add6c92d7 100644 --- a/test/addons-napi/test_exception/test.js +++ b/test/addons-napi/test_exception/test.js @@ -1,10 +1,26 @@ 'use strict'; +// Flags: --expose-gc const common = require('../../common'); -const test_exception = require(`./build/${common.buildType}/test_exception`); const assert = require('assert'); const theError = new Error('Some error'); +// The test module throws an error during Init, but in order for its exports to +// not be lost, it attaches them to the error's "bindings" property. This way, +// we can make sure that exceptions thrown during the module initialization +// phase are propagated through require() into JavaScript. +// https://github.com/nodejs/node/issues/19437 +const test_exception = (function() { + let resultingException; + try { + require(`./build/${common.buildType}/test_exception`); + } catch (anException) { + resultingException = anException; + } + assert.strictEqual(resultingException.message, 'Error during Init'); + return resultingException.binding; +})(); + { const throwTheError = () => { throw theError; }; @@ -50,3 +66,15 @@ const theError = new Error('Some error'); 'Exception state did not remain clear as expected,' + ` .wasPending() returned ${exception_pending}`); } + +// Make sure that exceptions that occur during finalization are propagated. +function testFinalize(binding) { + let x = test_exception[binding](); + x = null; + assert.throws(() => { global.gc(); }, /Error during Finalize/); + + // To assuage the linter's concerns. + (function() {})(x); +} +testFinalize('createExternal'); +testFinalize('createExternalBuffer'); diff --git a/test/addons-napi/test_exception/test_exception.c b/test/addons-napi/test_exception/test_exception.c index 2ab01b653becfb..c83fd5ebcade19 100644 --- a/test/addons-napi/test_exception/test_exception.c +++ b/test/addons-napi/test_exception/test_exception.c @@ -3,7 +3,7 @@ static bool exceptionWasPending = false; -napi_value returnException(napi_env env, napi_callback_info info) { +static napi_value returnException(napi_env env, napi_callback_info info) { size_t argc = 1; napi_value args[1]; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); @@ -22,7 +22,7 @@ napi_value returnException(napi_env env, napi_callback_info info) { return NULL; } -napi_value allowException(napi_env env, napi_callback_info info) { +static napi_value allowException(napi_env env, napi_callback_info info) { size_t argc = 1; napi_value args[1]; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); @@ -38,23 +38,55 @@ napi_value allowException(napi_env env, napi_callback_info info) { return NULL; } -napi_value wasPending(napi_env env, napi_callback_info info) { +static napi_value wasPending(napi_env env, napi_callback_info info) { napi_value result; NAPI_CALL(env, napi_get_boolean(env, exceptionWasPending, &result)); return result; } -napi_value Init(napi_env env, napi_value exports) { +static void finalizer(napi_env env, void *data, void *hint) { + NAPI_CALL_RETURN_VOID(env, + napi_throw_error(env, NULL, "Error during Finalize")); +} + +static napi_value createExternal(napi_env env, napi_callback_info info) { + napi_value external; + + NAPI_CALL(env, + napi_create_external(env, NULL, finalizer, NULL, &external)); + + return external; +} + +static char buffer_data[12]; + +static napi_value createExternalBuffer(napi_env env, napi_callback_info info) { + napi_value buffer; + NAPI_CALL(env, napi_create_external_buffer(env, sizeof(buffer_data), + buffer_data, finalizer, NULL, &buffer)); + return buffer; +} + +static napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("returnException", returnException), DECLARE_NAPI_PROPERTY("allowException", allowException), DECLARE_NAPI_PROPERTY("wasPending", wasPending), + DECLARE_NAPI_PROPERTY("createExternal", createExternal), + DECLARE_NAPI_PROPERTY("createExternalBuffer", createExternalBuffer), }; - NAPI_CALL(env, napi_define_properties( env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); + napi_value error, code, message; + NAPI_CALL(env, napi_create_string_utf8(env, "Error during Init", + NAPI_AUTO_LENGTH, &message)); + NAPI_CALL(env, napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &code)); + NAPI_CALL(env, napi_create_error(env, code, message, &error)); + NAPI_CALL(env, napi_set_named_property(env, error, "binding", exports)); + NAPI_CALL(env, napi_throw(env, error)); + return exports; } From dc4c8257b84b9558262af78b50219812bd0e601f Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Sat, 31 Mar 2018 21:37:17 -0400 Subject: [PATCH 182/183] n-api: back up env before finalize Heed the comment to not use fields of a Reference after calling its finalize callback, because such a call may destroy the Reference. Fixes: https://github.com/nodejs/node/issues/19673 PR-URL: https://github.com/nodejs/node/pull/19718 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- src/node_api.cc | 3 ++- test/addons-napi/8_passing_wrapped/binding.cc | 15 ++++++++++++--- test/addons-napi/8_passing_wrapped/myobject.cc | 12 +++++++++++- test/addons-napi/8_passing_wrapped/test.js | 9 ++++++++- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index e4638e4559af97..a13398006b652d 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -481,9 +481,10 @@ class Reference : private Finalizer { // Check before calling the finalize callback, because the callback might // delete it. bool delete_self = reference->_delete_self; + napi_env env = reference->_env; if (reference->_finalize_callback != nullptr) { - NAPI_CALL_INTO_MODULE_THROW(reference->_env, + NAPI_CALL_INTO_MODULE_THROW(env, reference->_finalize_callback( reference->_env, reference->_finalize_data, diff --git a/test/addons-napi/8_passing_wrapped/binding.cc b/test/addons-napi/8_passing_wrapped/binding.cc index 86a6e4d739f03d..f38861d54d8dc2 100644 --- a/test/addons-napi/8_passing_wrapped/binding.cc +++ b/test/addons-napi/8_passing_wrapped/binding.cc @@ -1,7 +1,9 @@ #include "myobject.h" #include "../common.h" -napi_value CreateObject(napi_env env, napi_callback_info info) { +extern size_t finalize_count; + +static napi_value CreateObject(napi_env env, napi_callback_info info) { size_t argc = 1; napi_value args[1]; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); @@ -12,7 +14,7 @@ napi_value CreateObject(napi_env env, napi_callback_info info) { return instance; } -napi_value Add(napi_env env, napi_callback_info info) { +static napi_value Add(napi_env env, napi_callback_info info) { size_t argc = 2; napi_value args[2]; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); @@ -29,12 +31,19 @@ napi_value Add(napi_env env, napi_callback_info info) { return sum; } -napi_value Init(napi_env env, napi_value exports) { +static napi_value FinalizeCount(napi_env env, napi_callback_info info) { + napi_value return_value; + NAPI_CALL(env, napi_create_uint32(env, finalize_count, &return_value)); + return return_value; +} + +static napi_value Init(napi_env env, napi_value exports) { MyObject::Init(env); napi_property_descriptor desc[] = { DECLARE_NAPI_PROPERTY("createObject", CreateObject), DECLARE_NAPI_PROPERTY("add", Add), + DECLARE_NAPI_PROPERTY("finalizeCount", FinalizeCount), }; NAPI_CALL(env, diff --git a/test/addons-napi/8_passing_wrapped/myobject.cc b/test/addons-napi/8_passing_wrapped/myobject.cc index 19cc7dd2a29493..0c9ca90f52f8f3 100644 --- a/test/addons-napi/8_passing_wrapped/myobject.cc +++ b/test/addons-napi/8_passing_wrapped/myobject.cc @@ -1,9 +1,14 @@ #include "myobject.h" #include "../common.h" +size_t finalize_count = 0; + MyObject::MyObject() : env_(nullptr), wrapper_(nullptr) {} -MyObject::~MyObject() { napi_delete_reference(env_, wrapper_); } +MyObject::~MyObject() { + finalize_count++; + napi_delete_reference(env_, wrapper_); +} void MyObject::Destructor( napi_env env, void* nativeObject, void* /*finalize_hint*/) { @@ -45,6 +50,11 @@ napi_value MyObject::New(napi_env env, napi_callback_info info) { } obj->env_ = env; + + // It is important that the below call to napi_wrap() be such that we request + // a reference to the wrapped object via the out-parameter, because this + // ensures that we test the code path that deals with a reference that is + // destroyed from its own finalizer. NAPI_CALL(env, napi_wrap(env, _this, obj, diff --git a/test/addons-napi/8_passing_wrapped/test.js b/test/addons-napi/8_passing_wrapped/test.js index 3d24fa5d9fdaa7..7793133f7750ba 100644 --- a/test/addons-napi/8_passing_wrapped/test.js +++ b/test/addons-napi/8_passing_wrapped/test.js @@ -1,9 +1,16 @@ 'use strict'; +// Flags: --expose-gc + const common = require('../../common'); const assert = require('assert'); const addon = require(`./build/${common.buildType}/binding`); -const obj1 = addon.createObject(10); +let obj1 = addon.createObject(10); const obj2 = addon.createObject(20); const result = addon.add(obj1, obj2); assert.strictEqual(result, 30); + +// Make sure the native destructor gets called. +obj1 = null; +global.gc(); +assert.strictEqual(addon.finalizeCount(), 1); From f498a625d0d0698242857a10d3616c2e75644283 Mon Sep 17 00:00:00 2001 From: Kyle Farnung Date: Thu, 15 Mar 2018 17:22:30 -0700 Subject: [PATCH 183/183] n-api: add more `int64_t` tests * Updated tests for `Number` and `int32_t` * Added new tests for `int64_t` * Updated N-API `int64_t` behavior to return zero for all non-finite numbers * Clarified the documentation for these calls. PR-URL: https://github.com/nodejs/node/pull/19402 Refs: https://github.com/nodejs/node-chakracore/pull/500 Reviewed-By: Michael Dawson Reviewed-By: James M Snell --- doc/api/n-api.md | 27 ++++-- src/node_api.cc | 23 +++-- test/addons-napi/test_number/test.js | 135 ++++++++++++++++++++------- 3 files changed, 135 insertions(+), 50 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index e176cbdf1ef9cd..38d7b22f558ec2 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -1806,13 +1806,17 @@ napi_status napi_get_value_int32(napi_env env, - `[out] result`: C int32 primitive equivalent of the given JavaScript Number. Returns `napi_ok` if the API succeeded. If a non-number `napi_value` -is passed in `napi_number_expected . +is passed in `napi_number_expected`. This API returns the C int32 primitive equivalent -of the given JavaScript Number. If the number exceeds the range of the -32 bit integer, then the result is truncated to the equivalent of the -bottom 32 bits. This can result in a large positive number becoming -a negative number if the value is > 2^31 -1. +of the given JavaScript Number. + +If the number exceeds the range of the 32 bit integer, then the result is +truncated to the equivalent of the bottom 32 bits. This can result in a large +positive number becoming a negative number if the value is > 2^31 -1. + +Non-finite number values (NaN, positive infinity, or negative infinity) set the +result to zero. #### napi_get_value_int64