From bb9622ba5a3073d3f483ec47177ce13bffe4b051 Mon Sep 17 00:00:00 2001 From: Harshitha KP Date: Fri, 3 Jan 2020 02:28:52 -0500 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/31173 Fixes: https://github.com/nodejs/node/issues/20604 Reviewed-By: Ruben Bridgewater Reviewed-By: Anto Aravinth Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott --- doc/api/util.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/doc/api/util.md b/doc/api/util.md index f2fb69c4ba947e..9f48a5b8fd72e0 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -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 +#include +#include +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 +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 +[`napi_create_external()`][]. + ### `util.types.isFloat32Array(value)`