Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bb9622b

Browse filesBrowse files
HarshithaKPMylesBorins
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 108046d commit bb9622b
Copy full SHA for bb9622b

File tree

Expand file treeCollapse file tree

1 file changed

+30
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+30
-0
lines changed
Open diff view settings
Collapse file

‎doc/api/util.md‎

Copy file name to clipboardExpand all lines: doc/api/util.md
+30Lines changed: 30 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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 <stdlib.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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.