Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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
  • Loading branch information
HarshithaKP committed Jan 10, 2020
commit aa6096d3c90b23e4430ef4607b34d748144623b7
30 changes: 30 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,35 @@ properties. Such objects are created either by Node.js internals or native
addons. In JavaScript, they are [frozen][`Object.freeze()`] objects with a
`null` prototype.

```c++
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
#include <js_native_api.h>
#include <string.h>
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
napi_value result;
static napi_value MyNapi(napi_env env, napi_callback_info info) {
int* raw = (int*) malloc(1024);
napi_status status = napi_create_external(env, (void*) raw, NULL, NULL, &result);
if (status != napi_ok) {
napi_throw_error(env, NULL, "napi_create_external failed");
return NULL;
}
return result;
}
...
DECLARE_NAPI_PROPERTY("myNapi", MyNapi)
...
```

```js
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
const native = require('napi_addon.node');
const data = native.myNapi();
util.types.isExternal(data); // returns true
util.types.isExternal(0); // returns false
util.types.isExternal(new String('foo')); // returns false
```

For further information on `napi_create_external`, refer to
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
[`napi_create_external()`][].

### `util.types.isFloat32Array(value)`
<!-- YAML
added: v10.0.0
Expand Down Expand Up @@ -2358,5 +2387,6 @@ util.log('Timestamped message.');
[default sort]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
[global symbol registry]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for
[list of deprecated APIS]: deprecations.html#deprecations_list_of_deprecated_apis
[`napi_create_external()`]: n-api.html#n_api_napi_create_external
[semantically incompatible]: https://github.com/nodejs/node/issues/4179
[util.inspect.custom]: #util_util_inspect_custom