Skip to content

Commit 6598a08

Browse files
HarshithaKPcodebytere
authored andcommitted
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. PR-URL: #31173 Fixes: #20604 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent d00a1b9 commit 6598a08

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
@@ -1398,6 +1398,35 @@ properties. Such objects are created either by Node.js internals or native
13981398
addons. In JavaScript, they are [frozen][`Object.freeze()`] objects with a
13991399
`null` prototype.
14001400

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

0 commit comments

Comments
 (0)