@@ -1639,25 +1639,36 @@ If it is called more than once an error will be returned.
16391639
16401640This API can be called even if there is a pending JavaScript exception.
16411641
1642- ### References to objects with a lifespan longer than that of the native method
1642+ ### References to values with a lifespan longer than that of the native method
16431643
1644- In some cases an addon will need to be able to create and reference objects
1644+ In some cases, an addon will need to be able to create and reference values
16451645with a lifespan longer than that of a single native method invocation. For
16461646example, to create a constructor and later use that constructor
1647- in a request to creates instances, it must be possible to reference
1647+ in a request to create instances, it must be possible to reference
16481648the constructor object across many different instance creation requests. This
16491649would not be possible with a normal handle returned as a `napi_value` as
16501650described in the earlier section. The lifespan of a normal handle is
16511651managed by scopes and all scopes must be closed before the end of a native
16521652method.
16531653
1654- Node-API provides methods to create persistent references to an object.
1655- Each persistent reference has an associated count with a value of 0
1656- or higher. The count determines if the reference will keep
1657- the corresponding object live. References with a count of 0 do not
1658- prevent the object from being collected and are often called 'weak'
1659- references. Any count greater than 0 will prevent the object
1660- from being collected.
1654+ Node-API provides methods for creating persistent references to values.
1655+ Each reference has an associated count with a value of 0 or higher,
1656+ which determines whether the reference will keep the corresponding value alive.
1657+ References with a count of 0 do not prevent values from being collected.
1658+ Values of object (object, function, external) and symbol types are becoming
1659+ 'weak' references and can still be accessed while they are not collected.
1660+ Values of other types are released when the count becomes 0
1661+ and cannot be accessed from the reference any more.
1662+ Any count greater than 0 will prevent the values from being collected.
1663+
1664+ Symbol values have different flavors. The true weak reference behavior is
1665+ only supported by local symbols created with the `Symbol()` constructor call.
1666+ Globally registered symbols created with the `Symbol.for()` call remain
1667+ always strong references because the garbage collector does not collect them.
1668+ The same is true for well-known symbols such as `Symbol.iterator`. They are
1669+ also never collected by the garbage collector. JavaScript's `WeakRef` and
1670+ `WeakMap` types return an error when registered symbols are used,
1671+ but they succeed for local and well-known symbols.
16611672
16621673References can be created with an initial reference count. The count can
16631674then be modified through [`napi_reference_ref`][] and
@@ -1668,6 +1679,11 @@ will return `NULL` for the returned `napi_value`. An attempt to call
16681679[`napi_reference_ref`][] for a reference whose object has been collected
16691680results in an error.
16701681
1682+ Node-API versions 8 and earlier only allow references to be created for a
1683+ limited set of value types, including object, external, function, and symbol.
1684+ However, in newer Node-API versions, references can be created for any
1685+ value type.
1686+
16711687References must be deleted once they are no longer required by the addon. When
16721688a reference is deleted, it will no longer prevent the corresponding object from
16731689being collected. Failure to delete a persistent reference results in
@@ -1700,15 +1716,18 @@ NAPI_EXTERN napi_status napi_create_reference(napi_env env,
17001716```
17011717
17021718* `[in] env`: The environment that the API is invoked under.
1703- * `[in] value`: `napi_value` representing the `Object` to which we want a
1704- reference.
1719+ * `[in] value`: The `napi_value` for which a reference is being created.
17051720* `[in] initial_refcount`: Initial reference count for the new reference.
17061721* `[out] result`: `napi_ref` pointing to the new reference.
17071722
17081723Returns `napi_ok` if the API succeeded.
17091724
17101725This API creates a new reference with the specified reference count
1711- to the `Object` passed in.
1726+ to the value passed in.
1727+
1728+ In Node-API version 8 and earlier, a reference could only be created for
1729+ object, function, external, and symbol value types. However, in newer Node-API
1730+ versions, a reference can be created for any value type.
17121731
17131732#### `napi_delete_reference`
17141733
@@ -1787,18 +1806,15 @@ NAPI_EXTERN napi_status napi_get_reference_value(napi_env env,
17871806 napi_value* result);
17881807```
17891808
1790- the `napi_value passed` in or out of these methods is a handle to the
1791- object to which the reference is related.
1792-
17931809* `[in] env`: The environment that the API is invoked under.
1794- * `[in] ref`: `napi_ref` for which we requesting the corresponding `Object`.
1795- * `[out] result`: The `napi_value` for the `Object` referenced by the
1796- `napi_ref`.
1810+ * `[in] ref`: The `napi_ref` for which the corresponding value is
1811+ being requested.
1812+ * `[out] result`: The `napi_value` referenced by the `napi_ref`.
17971813
17981814Returns `napi_ok` if the API succeeded.
17991815
18001816If still valid, this API returns the `napi_value` representing the
1801- JavaScript `Object` associated with the `napi_ref`. Otherwise, result
1817+ JavaScript value associated with the `napi_ref`. Otherwise, result
18021818will be `NULL`.
18031819
18041820### Cleanup on exit of the current Node.js environment
@@ -5069,9 +5085,8 @@ napi_status napi_define_class(napi_env env,
50695085```
50705086
50715087* `[in] env`: The environment that the API is invoked under.
5072- * `[in] utf8name`: Name of the JavaScript constructor function; When wrapping a
5073- C++ class, we recommend for clarity that this name be the same as that of
5074- the C++ class.
5088+ * `[in] utf8name`: Name of the JavaScript constructor function. For clarity,
5089+ it is recommended to use the C++ class name when wrapping a C++ class.
50755090* `[in] length`: The length of the `utf8name` in bytes, or `NAPI_AUTO_LENGTH`
50765091 if it is null-terminated.
50775092* `[in] constructor`: Callback function that handles constructing instances
0 commit comments