@@ -1637,25 +1637,36 @@ If it is called more than once an error will be returned.
16371637
16381638This API can be called even if there is a pending JavaScript exception.
16391639
1640- ### References to objects with a lifespan longer than that of the native method
1640+ ### References to values with a lifespan longer than that of the native method
16411641
1642- In some cases an addon will need to be able to create and reference objects
1642+ In some cases, an addon will need to be able to create and reference values
16431643with a lifespan longer than that of a single native method invocation. For
16441644example, to create a constructor and later use that constructor
1645- in a request to creates instances, it must be possible to reference
1645+ in a request to create instances, it must be possible to reference
16461646the constructor object across many different instance creation requests. This
16471647would not be possible with a normal handle returned as a `napi_value` as
16481648described in the earlier section. The lifespan of a normal handle is
16491649managed by scopes and all scopes must be closed before the end of a native
16501650method.
16511651
1652- Node-API provides methods to create persistent references to an object.
1653- Each persistent reference has an associated count with a value of 0
1654- or higher. The count determines if the reference will keep
1655- the corresponding object live. References with a count of 0 do not
1656- prevent the object from being collected and are often called 'weak'
1657- references. Any count greater than 0 will prevent the object
1658- from being collected.
1652+ Node-API provides methods for creating persistent references to values.
1653+ Each reference has an associated count with a value of 0 or higher,
1654+ which determines whether the reference will keep the corresponding value alive.
1655+ References with a count of 0 do not prevent values from being collected.
1656+ Values of object (object, function, external) and symbol types are becoming
1657+ 'weak' references and can still be accessed while they are not collected.
1658+ Values of other types are released when the count becomes 0
1659+ and cannot be accessed from the reference any more.
1660+ Any count greater than 0 will prevent the values from being collected.
1661+
1662+ Symbol values have different flavors. The true weak reference behavior is
1663+ only supported by local symbols created with the `Symbol()` constructor call.
1664+ Globally registered symbols created with the `Symbol.for()` call remain
1665+ always strong references because the garbage collector does not collect them.
1666+ The same is true for well-known symbols such as `Symbol.iterator`. They are
1667+ also never collected by the garbage collector. JavaScript's `WeakRef` and
1668+ `WeakMap` types return an error when registered symbols are used,
1669+ but they succeed for local and well-known symbols.
16591670
16601671References can be created with an initial reference count. The count can
16611672then be modified through [`napi_reference_ref`][] and
@@ -1666,6 +1677,11 @@ will return `NULL` for the returned `napi_value`. An attempt to call
16661677[`napi_reference_ref`][] for a reference whose object has been collected
16671678results in an error.
16681679
1680+ Node-API versions 8 and earlier only allow references to be created for a
1681+ limited set of value types, including object, external, function, and symbol.
1682+ However, in newer Node-API versions, references can be created for any
1683+ value type.
1684+
16691685References must be deleted once they are no longer required by the addon. When
16701686a reference is deleted, it will no longer prevent the corresponding object from
16711687being collected. Failure to delete a persistent reference results in
@@ -1698,15 +1714,18 @@ NAPI_EXTERN napi_status napi_create_reference(napi_env env,
16981714```
16991715
17001716* `[in] env`: The environment that the API is invoked under.
1701- * `[in] value`: `napi_value` representing the `Object` to which we want a
1702- reference.
1717+ * `[in] value`: The `napi_value` for which a reference is being created.
17031718* `[in] initial_refcount`: Initial reference count for the new reference.
17041719* `[out] result`: `napi_ref` pointing to the new reference.
17051720
17061721Returns `napi_ok` if the API succeeded.
17071722
17081723This API creates a new reference with the specified reference count
1709- to the `Object` passed in.
1724+ to the value passed in.
1725+
1726+ In Node-API version 8 and earlier, a reference could only be created for
1727+ object, function, external, and symbol value types. However, in newer Node-API
1728+ versions, a reference can be created for any value type.
17101729
17111730#### `napi_delete_reference`
17121731
@@ -1785,18 +1804,15 @@ NAPI_EXTERN napi_status napi_get_reference_value(napi_env env,
17851804 napi_value* result);
17861805```
17871806
1788- the `napi_value passed` in or out of these methods is a handle to the
1789- object to which the reference is related.
1790-
17911807* `[in] env`: The environment that the API is invoked under.
1792- * `[in] ref`: `napi_ref` for which we requesting the corresponding `Object`.
1793- * `[out] result`: The `napi_value` for the `Object` referenced by the
1794- `napi_ref`.
1808+ * `[in] ref`: The `napi_ref` for which the corresponding value is
1809+ being requested.
1810+ * `[out] result`: The `napi_value` referenced by the `napi_ref`.
17951811
17961812Returns `napi_ok` if the API succeeded.
17971813
17981814If still valid, this API returns the `napi_value` representing the
1799- JavaScript `Object` associated with the `napi_ref`. Otherwise, result
1815+ JavaScript value associated with the `napi_ref`. Otherwise, result
18001816will be `NULL`.
18011817
18021818### Cleanup on exit of the current Node.js environment
@@ -5065,9 +5081,8 @@ napi_status napi_define_class(napi_env env,
50655081```
50665082
50675083* `[in] env`: The environment that the API is invoked under.
5068- * `[in] utf8name`: Name of the JavaScript constructor function; When wrapping a
5069- C++ class, we recommend for clarity that this name be the same as that of
5070- the C++ class.
5084+ * `[in] utf8name`: Name of the JavaScript constructor function. For clarity,
5085+ it is recommended to use the C++ class name when wrapping a C++ class.
50715086* `[in] length`: The length of the `utf8name` in bytes, or `NAPI_AUTO_LENGTH`
50725087 if it is null-terminated.
50735088* `[in] constructor`: Callback function that handles constructing instances
0 commit comments