From efd23fd50f82467a3b2394b93dbcf94267a2ae2d Mon Sep 17 00:00:00 2001 From: Vladimir Morozov Date: Thu, 31 Mar 2022 21:05:44 -0700 Subject: [PATCH] node-api: add new napi_ref type for any napi_value --- doc/api/n-api.md | 123 +++++++++++--- src/js_native_api.h | 39 +++-- src/js_native_api_types.h | 7 + src/js_native_api_v8.cc | 117 +++++++++++--- src/js_native_api_v8.h | 13 +- .../js-native-api/test_strong_ref/binding.gyp | 11 ++ test/js-native-api/test_strong_ref/test.js | 39 +++++ .../test_strong_ref/test_strong_ref.cc | 153 ++++++++++++++++++ 8 files changed, 447 insertions(+), 55 deletions(-) create mode 100644 test/js-native-api/test_strong_ref/binding.gyp create mode 100644 test/js-native-api/test_strong_ref/test.js create mode 100644 test/js-native-api/test_strong_ref/test_strong_ref.cc diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 76f45091fae711..1d03822dbd83e5 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -721,6 +721,33 @@ minimum lifetimes explicitly. For more details, review the [Object lifetime management][]. +#### `node_api_reftype` + + + +Type of the `napi_ref` reference. +There are two types of reference: + +* `node_api_reftype_strong_or_weak` - a reference to an object (`napi_object`) + that can be a strong or a weak reference. References with ref count greater + than 0 are strong references and references with ref count equal to 0 are + weak references. The values referenced by weak references can be collected + at any time by GC if there are no other strong references to the + `napi_value`. To delete the reference we must use `napi_delete_reference` + function. +* `node_api_reftype_strong` - a strong reference to any type of `napi_value`. + When the ref count goes down to 0, the reference is deleted. + The `napi_delete_reference` function must not be used with the strong references. + +```c +typedef enum { + node_api_reftype_strong_or_weak, + node_api_reftype_strong, +} node_api_reftype; +``` + #### `napi_type_tag` + +```c +NAPI_EXTERN napi_status node_api_create_reference(napi_env env, + napi_value value, + node_api_reftype reftype, + uint32_t initial_refcount, + napi_ref* result); +``` + +* `[in] env`: The environment that the API is invoked under. +* `[in] value`: `napi_value` to which we want to create a reference. +* `[in] reftype`: Type of the reference. +* `[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. + +For the `node_api_reftype_strong_or_weak` `reftype` set `initial_refcount` to 0 +for a weak reference, and value greater than 0 for a strong reference. It +accepts only `napi_object` values. +The `node_api_reftype_strong` ignores the `initial_refcount` and always uses 1. +It accept `napi_value` of any type. #### `napi_create_reference` @@ -1684,6 +1752,25 @@ Returns `napi_ok` if the API succeeded. This API creates a new reference with the specified reference count to the `Object` passed in. +#### `node_api_get_reference_type` + + + +```c +NAPI_EXTERN napi_status node_api_get_reference_type(napi_env env, + napi_ref ref, + node_api_reftype* result); +``` + +* `[in] env`: The environment that the API is invoked under. +* `[in] ref`: `napi_ref` which type we want to get. +* `[out] result`: `node_api_reftype` type of the `ref`. + +Returns `napi_ok` if the API succeeded and the `result` contains type of the +reference. + #### `napi_delete_reference`