@@ -1639,25 +1639,26 @@ 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 to create persistent references to values.
1655+ Each reference has an associated count with a value of 0 or higher.
1656+ The count determines if the reference will keep the corresponding value alive.
1657+ References with a count of 0 do not prevent values of object types (object,
1658+ function, external) from being collected and are often called 'weak'
1659+ references. Values of non-object types cannot be collected if a reference
1660+ has count 0 because they do not support the 'weak' object semantic.
1661+ Any count greater than 0 will prevent the values from being collected.
16611662
16621663References can be created with an initial reference count. The count can
16631664then be modified through [`napi_reference_ref`][] and
@@ -1668,21 +1669,24 @@ will return `NULL` for the returned `napi_value`. An attempt to call
16681669[`napi_reference_ref`][] for a reference whose object has been collected
16691670results in an error.
16701671
1672+ In Node-API version 8 and before we can only create references for a limited
1673+ set of value types: object, external, function, and symbol. In newer Node-API
1674+ versions the references can be created for any value type.
1675+
16711676References must be deleted once they are no longer required by the addon. When
1672- a reference is deleted, it will no longer prevent the corresponding object from
1673- being collected. Failure to delete a persistent reference results in
1674- a 'memory leak' with both the native memory for the persistent reference and
1675- the corresponding object on the heap being retained forever.
1676-
1677- There can be multiple persistent references created which refer to the same
1678- object, each of which will either keep the object live or not based on its
1679- individual count. Multiple persistent references to the same object
1680- can result in unexpectedly keeping alive native memory. The native structures
1681- for a persistent reference must be kept alive until finalizers for the
1682- referenced object are executed. If a new persistent reference is created
1683- for the same object, the finalizers for that object will not be
1684- run and the native memory pointed by the earlier persistent reference
1685- will not be freed. This can be avoided by calling
1677+ a reference is deleted, it will no longer prevent the corresponding value from
1678+ being collected. If you don't delete a persistent reference, it can cause a
1679+ 'memory leak'. This means that both the native memory for the persistent
1680+ reference and the corresponding value on the heap will be retained forever.
1681+
1682+ You can create multiple persistent references that refer to the same value.
1683+ Each reference will keep the value alive or not based on its individual count.
1684+ If there are multiple persistent references to the same value, it can
1685+ unexpectedly keep alive native memory. The native structures for a persistent
1686+ reference must be kept alive until finalizers for the referenced value are
1687+ executed. If you create a new persistent reference for the same value, the
1688+ finalizers for that value will not be run and the native memory pointed by
1689+ the earlier persistent reference will not be freed. To avoid this, call
16861690`napi_delete_reference` in addition to `napi_reference_unref` when possible.
16871691
16881692#### `napi_create_reference`
@@ -1700,15 +1704,18 @@ NAPI_EXTERN napi_status napi_create_reference(napi_env env,
17001704```
17011705
17021706* `[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.
1707+ * `[in] value`: `napi_value` to which we want to create a reference.
17051708* `[in] initial_refcount`: Initial reference count for the new reference.
17061709* `[out] result`: `napi_ref` pointing to the new reference.
17071710
17081711Returns `napi_ok` if the API succeeded.
17091712
17101713This API creates a new reference with the specified reference count
1711- to the `Object` passed in.
1714+ to the value passed in.
1715+
1716+ In Node-API version 8 and earlier, you could only create a reference for
1717+ object, function, external, and symbol value types. In newer Node-API
1718+ versions, you can create a reference for any value type.
17121719
17131720#### `napi_delete_reference`
17141721
@@ -1787,18 +1794,14 @@ NAPI_EXTERN napi_status napi_get_reference_value(napi_env env,
17871794 napi_value* result);
17881795```
17891796
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-
17931797* `[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`.
1798+ * `[in] ref`: `napi_ref` for which we requesting the corresponding value.
1799+ * `[out] result`: The `napi_value` referenced by the `napi_ref`.
17971800
17981801Returns `napi_ok` if the API succeeded.
17991802
18001803If still valid, this API returns the `napi_value` representing the
1801- JavaScript `Object` associated with the `napi_ref`. Otherwise, result
1804+ JavaScript value associated with the `napi_ref`. Otherwise, result
18021805will be `NULL`.
18031806
18041807### Cleanup on exit of the current Node.js environment
0 commit comments