@@ -1639,25 +1639,26 @@ If it is called more than once an error will be returned.
1639
1639
1640
1640
This API can be called even if there is a pending JavaScript exception.
1641
1641
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
1643
1643
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
1645
1645
with a lifespan longer than that of a single native method invocation. For
1646
1646
example, 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
1648
1648
the constructor object across many different instance creation requests. This
1649
1649
would not be possible with a normal handle returned as a `napi_value` as
1650
1650
described in the earlier section. The lifespan of a normal handle is
1651
1651
managed by scopes and all scopes must be closed before the end of a native
1652
1652
method.
1653
1653
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.
1661
1662
1662
1663
References can be created with an initial reference count. The count can
1663
1664
then be modified through [`napi_reference_ref`][] and
@@ -1668,21 +1669,24 @@ will return `NULL` for the returned `napi_value`. An attempt to call
1668
1669
[`napi_reference_ref`][] for a reference whose object has been collected
1669
1670
results in an error.
1670
1671
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
+
1671
1676
References 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
1686
1690
`napi_delete_reference` in addition to `napi_reference_unref` when possible.
1687
1691
1688
1692
#### `napi_create_reference`
@@ -1700,15 +1704,18 @@ NAPI_EXTERN napi_status napi_create_reference(napi_env env,
1700
1704
```
1701
1705
1702
1706
* `[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.
1705
1708
* `[in] initial_refcount`: Initial reference count for the new reference.
1706
1709
* `[out] result`: `napi_ref` pointing to the new reference.
1707
1710
1708
1711
Returns `napi_ok` if the API succeeded.
1709
1712
1710
1713
This 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.
1712
1719
1713
1720
#### `napi_delete_reference`
1714
1721
@@ -1787,18 +1794,14 @@ NAPI_EXTERN napi_status napi_get_reference_value(napi_env env,
1787
1794
napi_value* result);
1788
1795
```
1789
1796
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
-
1793
1797
* `[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`.
1797
1800
1798
1801
Returns `napi_ok` if the API succeeded.
1799
1802
1800
1803
If 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
1802
1805
will be `NULL`.
1803
1806
1804
1807
### Cleanup on exit of the current Node.js environment
0 commit comments