Skip to content

Commit aa6096d

Browse files
committed
doc: add an example for util.types.isExternal
added usage example for util.types.isExternal which was missing owing to the complexity. Used a combination of n-api and js to demonstrate usage of the api. Fixes: #20604
1 parent 9530362 commit aa6096d

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

doc/api/util.md

+30
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,35 @@ properties. Such objects are created either by Node.js internals or native
13991399
addons. In JavaScript, they are [frozen][`Object.freeze()`] objects with a
14001400
`null` prototype.
14011401

1402+
```c++
1403+
#include <js_native_api.h>
1404+
#include <string.h>
1405+
napi_value result;
1406+
static napi_value MyNapi(napi_env env, napi_callback_info info) {
1407+
int* raw = (int*) malloc(1024);
1408+
napi_status status = napi_create_external(env, (void*) raw, NULL, NULL, &result);
1409+
if (status != napi_ok) {
1410+
napi_throw_error(env, NULL, "napi_create_external failed");
1411+
return NULL;
1412+
}
1413+
return result;
1414+
}
1415+
...
1416+
DECLARE_NAPI_PROPERTY("myNapi", MyNapi)
1417+
...
1418+
```
1419+
1420+
```js
1421+
const native = require('napi_addon.node');
1422+
const data = native.myNapi();
1423+
util.types.isExternal(data); // returns true
1424+
util.types.isExternal(0); // returns false
1425+
util.types.isExternal(new String('foo')); // returns false
1426+
```
1427+
1428+
For further information on `napi_create_external`, refer to
1429+
[`napi_create_external()`][].
1430+
14021431
### `util.types.isFloat32Array(value)`
14031432
<!-- YAML
14041433
added: v10.0.0
@@ -2358,5 +2387,6 @@ util.log('Timestamped message.');
23582387
[default sort]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
23592388
[global symbol registry]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for
23602389
[list of deprecated APIS]: deprecations.html#deprecations_list_of_deprecated_apis
2390+
[`napi_create_external()`]: n-api.html#n_api_napi_create_external
23612391
[semantically incompatible]: https://github.com/nodejs/node/issues/4179
23622392
[util.inspect.custom]: #util_util_inspect_custom

0 commit comments

Comments
 (0)