• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v18.18.2">
7  <title>Node-API | Node.js v18.18.2 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/n-api.html">
12  <script async defer src="assets/api.js" type="text/javascript"></script>
13
14</head>
15<body class="alt apidoc" id="api-section-n-api">
16  <div id="content" class="clearfix">
17    <div id="column2" class="interior">
18      <div id="intro" class="interior">
19        <a href="/" title="Go back to the home page">
20          Node.js
21        </a>
22      </div>
23      <ul>
24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
26</ul>
27<hr class="line">
28<ul>
29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
33<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
34<li><a href="n-api.html" class="nav-n-api active">C/C++ addons with Node-API</a></li>
35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
38<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
39<li><a href="console.html" class="nav-console">Console</a></li>
40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
45<li><a href="dns.html" class="nav-dns">DNS</a></li>
46<li><a href="domain.html" class="nav-domain">Domain</a></li>
47<li><a href="errors.html" class="nav-errors">Errors</a></li>
48<li><a href="events.html" class="nav-events">Events</a></li>
49<li><a href="fs.html" class="nav-fs">File system</a></li>
50<li><a href="globals.html" class="nav-globals">Globals</a></li>
51<li><a href="http.html" class="nav-http">HTTP</a></li>
52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
53<li><a href="https.html" class="nav-https">HTTPS</a></li>
54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
55<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
60<li><a href="net.html" class="nav-net">Net</a></li>
61<li><a href="os.html" class="nav-os">OS</a></li>
62<li><a href="path.html" class="nav-path">Path</a></li>
63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
65<li><a href="process.html" class="nav-process">Process</a></li>
66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
68<li><a href="readline.html" class="nav-readline">Readline</a></li>
69<li><a href="repl.html" class="nav-repl">REPL</a></li>
70<li><a href="report.html" class="nav-report">Report</a></li>
71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
72<li><a href="stream.html" class="nav-stream">Stream</a></li>
73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
74<li><a href="test.html" class="nav-test">Test runner</a></li>
75<li><a href="timers.html" class="nav-timers">Timers</a></li>
76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
78<li><a href="tty.html" class="nav-tty">TTY</a></li>
79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
80<li><a href="url.html" class="nav-url">URL</a></li>
81<li><a href="util.html" class="nav-util">Utilities</a></li>
82<li><a href="v8.html" class="nav-v8">V8</a></li>
83<li><a href="vm.html" class="nav-vm">VM</a></li>
84<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
89</ul>
90<hr class="line">
91<ul>
92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
93</ul>
94    </div>
95
96    <div id="column1" data-id="n-api" class="interior">
97      <header class="header">
98        <div class="header-container">
99          <h1>Node.js v18.18.2 documentation</h1>
100          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
101            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
102              <path fill="none" d="M0 0h24v24H0z" />
103              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
104              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
105            </svg>
106            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
107              <path d="M0 0h24v24H0z" fill="none" />
108              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
109            </svg>
110          </button>
111        </div>
112        <div id="gtoc">
113          <ul>
114            <li class="pinned-header">Node.js v18.18.2</li>
115
116    <li class="picker-header">
117      <a href="#">
118        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
119        Table of contents
120      </a>
121
122      <div class="picker"><div class="toc"><ul>
123<li><span class="stability_2"><a href="#node-api">Node-API</a></span>
124<ul>
125<li><a href="#implications-of-abi-stability">Implications of ABI stability</a></li>
126<li><a href="#building">Building</a>
127<ul>
128<li><a href="#build-tools">Build tools</a>
129<ul>
130<li><a href="#node-gyp">node-gyp</a></li>
131<li><a href="#cmakejs">CMake.js</a></li>
132</ul>
133</li>
134<li><a href="#uploading-precompiled-binaries">Uploading precompiled binaries</a>
135<ul>
136<li><a href="#node-pre-gyp">node-pre-gyp</a></li>
137<li><a href="#prebuild">prebuild</a></li>
138<li><a href="#prebuildify">prebuildify</a></li>
139</ul>
140</li>
141</ul>
142</li>
143<li><a href="#usage">Usage</a></li>
144<li><a href="#node-api-version-matrix">Node-API version matrix</a></li>
145<li><a href="#environment-life-cycle-apis">Environment life cycle APIs</a>
146<ul>
147<li><a href="#napi_set_instance_data"><code>napi_set_instance_data</code></a></li>
148<li><a href="#napi_get_instance_data"><code>napi_get_instance_data</code></a></li>
149</ul>
150</li>
151<li><a href="#basic-node-api-data-types">Basic Node-API data types</a>
152<ul>
153<li><a href="#napi_status"><code>napi_status</code></a></li>
154<li><a href="#napi_extended_error_info"><code>napi_extended_error_info</code></a></li>
155<li><a href="#napi_env"><code>napi_env</code></a></li>
156<li><a href="#napi_value"><code>napi_value</code></a></li>
157<li><a href="#napi_threadsafe_function"><code>napi_threadsafe_function</code></a></li>
158<li><a href="#napi_threadsafe_function_release_mode"><code>napi_threadsafe_function_release_mode</code></a></li>
159<li><a href="#napi_threadsafe_function_call_mode"><code>napi_threadsafe_function_call_mode</code></a></li>
160<li><a href="#node-api-memory-management-types">Node-API memory management types</a>
161<ul>
162<li><a href="#napi_handle_scope"><code>napi_handle_scope</code></a></li>
163<li><a href="#napi_escapable_handle_scope"><code>napi_escapable_handle_scope</code></a></li>
164<li><a href="#napi_ref"><code>napi_ref</code></a></li>
165<li><a href="#napi_type_tag"><code>napi_type_tag</code></a></li>
166<li><a href="#napi_async_cleanup_hook_handle"><code>napi_async_cleanup_hook_handle</code></a></li>
167</ul>
168</li>
169<li><a href="#node-api-callback-types">Node-API callback types</a>
170<ul>
171<li><a href="#napi_callback_info"><code>napi_callback_info</code></a></li>
172<li><a href="#napi_callback"><code>napi_callback</code></a></li>
173<li><a href="#napi_finalize"><code>napi_finalize</code></a></li>
174<li><a href="#napi_async_execute_callback"><code>napi_async_execute_callback</code></a></li>
175<li><a href="#napi_async_complete_callback"><code>napi_async_complete_callback</code></a></li>
176<li><a href="#napi_threadsafe_function_call_js"><code>napi_threadsafe_function_call_js</code></a></li>
177<li><a href="#napi_cleanup_hook"><code>napi_cleanup_hook</code></a></li>
178<li><a href="#napi_async_cleanup_hook"><code>napi_async_cleanup_hook</code></a></li>
179</ul>
180</li>
181</ul>
182</li>
183<li><a href="#error-handling">Error handling</a>
184<ul>
185<li><a href="#return-values">Return values</a>
186<ul>
187<li><a href="#napi_get_last_error_info"><code>napi_get_last_error_info</code></a></li>
188</ul>
189</li>
190<li><a href="#exceptions">Exceptions</a>
191<ul>
192<li><a href="#napi_throw"><code>napi_throw</code></a></li>
193<li><a href="#napi_throw_error"><code>napi_throw_error</code></a></li>
194<li><a href="#napi_throw_type_error"><code>napi_throw_type_error</code></a></li>
195<li><a href="#napi_throw_range_error"><code>napi_throw_range_error</code></a></li>
196<li><a href="#node_api_throw_syntax_error"><code>node_api_throw_syntax_error</code></a></li>
197<li><a href="#napi_is_error"><code>napi_is_error</code></a></li>
198<li><a href="#napi_create_error"><code>napi_create_error</code></a></li>
199<li><a href="#napi_create_type_error"><code>napi_create_type_error</code></a></li>
200<li><a href="#napi_create_range_error"><code>napi_create_range_error</code></a></li>
201<li><a href="#node_api_create_syntax_error"><code>node_api_create_syntax_error</code></a></li>
202<li><a href="#napi_get_and_clear_last_exception"><code>napi_get_and_clear_last_exception</code></a></li>
203<li><a href="#napi_is_exception_pending"><code>napi_is_exception_pending</code></a></li>
204<li><a href="#napi_fatal_exception"><code>napi_fatal_exception</code></a></li>
205</ul>
206</li>
207<li><a href="#fatal-errors">Fatal errors</a>
208<ul>
209<li><a href="#napi_fatal_error"><code>napi_fatal_error</code></a></li>
210</ul>
211</li>
212</ul>
213</li>
214<li><a href="#object-lifetime-management">Object lifetime management</a>
215<ul>
216<li><a href="#making-handle-lifespan-shorter-than-that-of-the-native-method">Making handle lifespan shorter than that of the native method</a>
217<ul>
218<li><a href="#napi_open_handle_scope"><code>napi_open_handle_scope</code></a></li>
219<li><a href="#napi_close_handle_scope"><code>napi_close_handle_scope</code></a></li>
220<li><a href="#napi_open_escapable_handle_scope"><code>napi_open_escapable_handle_scope</code></a></li>
221<li><a href="#napi_close_escapable_handle_scope"><code>napi_close_escapable_handle_scope</code></a></li>
222<li><a href="#napi_escape_handle"><code>napi_escape_handle</code></a></li>
223</ul>
224</li>
225<li><a href="#references-to-values-with-a-lifespan-longer-than-that-of-the-native-method">References to values with a lifespan longer than that of the native method</a>
226<ul>
227<li><a href="#napi_create_reference"><code>napi_create_reference</code></a></li>
228<li><a href="#napi_delete_reference"><code>napi_delete_reference</code></a></li>
229<li><a href="#napi_reference_ref"><code>napi_reference_ref</code></a></li>
230<li><a href="#napi_reference_unref"><code>napi_reference_unref</code></a></li>
231<li><a href="#napi_get_reference_value"><code>napi_get_reference_value</code></a></li>
232</ul>
233</li>
234<li><a href="#cleanup-on-exit-of-the-current-nodejs-environment">Cleanup on exit of the current Node.js environment</a>
235<ul>
236<li><a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a></li>
237<li><a href="#napi_remove_env_cleanup_hook"><code>napi_remove_env_cleanup_hook</code></a></li>
238<li><a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a></li>
239<li><a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a></li>
240</ul>
241</li>
242<li><a href="#finalization-on-the-exit-of-the-nodejs-environment">Finalization on the exit of the Node.js environment</a></li>
243</ul>
244</li>
245<li><a href="#module-registration">Module registration</a></li>
246<li><a href="#working-with-javascript-values">Working with JavaScript values</a>
247<ul>
248<li><a href="#enum-types">Enum types</a>
249<ul>
250<li><a href="#napi_key_collection_mode"><code>napi_key_collection_mode</code></a></li>
251<li><a href="#napi_key_filter"><code>napi_key_filter</code></a></li>
252<li><a href="#napi_key_conversion"><code>napi_key_conversion</code></a></li>
253<li><a href="#napi_valuetype"><code>napi_valuetype</code></a></li>
254<li><a href="#napi_typedarray_type"><code>napi_typedarray_type</code></a></li>
255</ul>
256</li>
257<li><a href="#object-creation-functions">Object creation functions</a>
258<ul>
259<li><a href="#napi_create_array"><code>napi_create_array</code></a></li>
260<li><a href="#napi_create_array_with_length"><code>napi_create_array_with_length</code></a></li>
261<li><a href="#napi_create_arraybuffer"><code>napi_create_arraybuffer</code></a></li>
262<li><a href="#napi_create_buffer"><code>napi_create_buffer</code></a></li>
263<li><a href="#napi_create_buffer_copy"><code>napi_create_buffer_copy</code></a></li>
264<li><a href="#napi_create_date"><code>napi_create_date</code></a></li>
265<li><a href="#napi_create_external"><code>napi_create_external</code></a></li>
266<li><a href="#napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code></a></li>
267<li><a href="#napi_create_external_buffer"><code>napi_create_external_buffer</code></a></li>
268<li><a href="#napi_create_object"><code>napi_create_object</code></a></li>
269<li><a href="#napi_create_symbol"><code>napi_create_symbol</code></a></li>
270<li><a href="#node_api_symbol_for"><code>node_api_symbol_for</code></a></li>
271<li><a href="#napi_create_typedarray"><code>napi_create_typedarray</code></a></li>
272<li><a href="#napi_create_dataview"><code>napi_create_dataview</code></a></li>
273</ul>
274</li>
275<li><a href="#functions-to-convert-from-c-types-to-node-api">Functions to convert from C types to Node-API</a>
276<ul>
277<li><a href="#napi_create_int32"><code>napi_create_int32</code></a></li>
278<li><a href="#napi_create_uint32"><code>napi_create_uint32</code></a></li>
279<li><a href="#napi_create_int64"><code>napi_create_int64</code></a></li>
280<li><a href="#napi_create_double"><code>napi_create_double</code></a></li>
281<li><a href="#napi_create_bigint_int64"><code>napi_create_bigint_int64</code></a></li>
282<li><a href="#napi_create_bigint_uint64"><code>napi_create_bigint_uint64</code></a></li>
283<li><a href="#napi_create_bigint_words"><code>napi_create_bigint_words</code></a></li>
284<li><a href="#napi_create_string_latin1"><code>napi_create_string_latin1</code></a></li>
285<li><span class="stability_1"><a href="#node_api_create_external_string_latin1"><code>node_api_create_external_string_latin1</code></a></span></li>
286<li><a href="#napi_create_string_utf16"><code>napi_create_string_utf16</code></a></li>
287<li><span class="stability_1"><a href="#node_api_create_external_string_utf16"><code>node_api_create_external_string_utf16</code></a></span></li>
288<li><a href="#napi_create_string_utf8"><code>napi_create_string_utf8</code></a></li>
289</ul>
290</li>
291<li><a href="#functions-to-convert-from-node-api-to-c-types">Functions to convert from Node-API to C types</a>
292<ul>
293<li><a href="#napi_get_array_length"><code>napi_get_array_length</code></a></li>
294<li><a href="#napi_get_arraybuffer_info"><code>napi_get_arraybuffer_info</code></a></li>
295<li><a href="#napi_get_buffer_info"><code>napi_get_buffer_info</code></a></li>
296<li><a href="#napi_get_prototype"><code>napi_get_prototype</code></a></li>
297<li><a href="#napi_get_typedarray_info"><code>napi_get_typedarray_info</code></a></li>
298<li><a href="#napi_get_dataview_info"><code>napi_get_dataview_info</code></a></li>
299<li><a href="#napi_get_date_value"><code>napi_get_date_value</code></a></li>
300<li><a href="#napi_get_value_bool"><code>napi_get_value_bool</code></a></li>
301<li><a href="#napi_get_value_double"><code>napi_get_value_double</code></a></li>
302<li><a href="#napi_get_value_bigint_int64"><code>napi_get_value_bigint_int64</code></a></li>
303<li><a href="#napi_get_value_bigint_uint64"><code>napi_get_value_bigint_uint64</code></a></li>
304<li><a href="#napi_get_value_bigint_words"><code>napi_get_value_bigint_words</code></a></li>
305<li><a href="#napi_get_value_external"><code>napi_get_value_external</code></a></li>
306<li><a href="#napi_get_value_int32"><code>napi_get_value_int32</code></a></li>
307<li><a href="#napi_get_value_int64"><code>napi_get_value_int64</code></a></li>
308<li><a href="#napi_get_value_string_latin1"><code>napi_get_value_string_latin1</code></a></li>
309<li><a href="#napi_get_value_string_utf8"><code>napi_get_value_string_utf8</code></a></li>
310<li><a href="#napi_get_value_string_utf16"><code>napi_get_value_string_utf16</code></a></li>
311<li><a href="#napi_get_value_uint32"><code>napi_get_value_uint32</code></a></li>
312</ul>
313</li>
314<li><a href="#functions-to-get-global-instances">Functions to get global instances</a>
315<ul>
316<li><a href="#napi_get_boolean"><code>napi_get_boolean</code></a></li>
317<li><a href="#napi_get_global"><code>napi_get_global</code></a></li>
318<li><a href="#napi_get_null"><code>napi_get_null</code></a></li>
319<li><a href="#napi_get_undefined"><code>napi_get_undefined</code></a></li>
320</ul>
321</li>
322</ul>
323</li>
324<li><a href="#working-with-javascript-values-and-abstract-operations">Working with JavaScript values and abstract operations</a>
325<ul>
326<li><a href="#napi_coerce_to_bool"><code>napi_coerce_to_bool</code></a></li>
327<li><a href="#napi_coerce_to_number"><code>napi_coerce_to_number</code></a></li>
328<li><a href="#napi_coerce_to_object"><code>napi_coerce_to_object</code></a></li>
329<li><a href="#napi_coerce_to_string"><code>napi_coerce_to_string</code></a></li>
330<li><a href="#napi_typeof"><code>napi_typeof</code></a></li>
331<li><a href="#napi_instanceof"><code>napi_instanceof</code></a></li>
332<li><a href="#napi_is_array"><code>napi_is_array</code></a></li>
333<li><a href="#napi_is_arraybuffer"><code>napi_is_arraybuffer</code></a></li>
334<li><a href="#napi_is_buffer"><code>napi_is_buffer</code></a></li>
335<li><a href="#napi_is_date"><code>napi_is_date</code></a></li>
336<li><a href="#napi_is_error_1"><code>napi_is_error</code></a></li>
337<li><a href="#napi_is_typedarray"><code>napi_is_typedarray</code></a></li>
338<li><a href="#napi_is_dataview"><code>napi_is_dataview</code></a></li>
339<li><a href="#napi_strict_equals"><code>napi_strict_equals</code></a></li>
340<li><a href="#napi_detach_arraybuffer"><code>napi_detach_arraybuffer</code></a></li>
341<li><a href="#napi_is_detached_arraybuffer"><code>napi_is_detached_arraybuffer</code></a></li>
342</ul>
343</li>
344<li><a href="#working-with-javascript-properties">Working with JavaScript properties</a>
345<ul>
346<li><a href="#structures">Structures</a>
347<ul>
348<li><a href="#napi_property_attributes"><code>napi_property_attributes</code></a></li>
349<li><a href="#napi_property_descriptor"><code>napi_property_descriptor</code></a></li>
350</ul>
351</li>
352<li><a href="#functions">Functions</a>
353<ul>
354<li><a href="#napi_get_property_names"><code>napi_get_property_names</code></a></li>
355<li><a href="#napi_get_all_property_names"><code>napi_get_all_property_names</code></a></li>
356<li><a href="#napi_set_property"><code>napi_set_property</code></a></li>
357<li><a href="#napi_get_property"><code>napi_get_property</code></a></li>
358<li><a href="#napi_has_property"><code>napi_has_property</code></a></li>
359<li><a href="#napi_delete_property"><code>napi_delete_property</code></a></li>
360<li><a href="#napi_has_own_property"><code>napi_has_own_property</code></a></li>
361<li><a href="#napi_set_named_property"><code>napi_set_named_property</code></a></li>
362<li><a href="#napi_get_named_property"><code>napi_get_named_property</code></a></li>
363<li><a href="#napi_has_named_property"><code>napi_has_named_property</code></a></li>
364<li><a href="#napi_set_element"><code>napi_set_element</code></a></li>
365<li><a href="#napi_get_element"><code>napi_get_element</code></a></li>
366<li><a href="#napi_has_element"><code>napi_has_element</code></a></li>
367<li><a href="#napi_delete_element"><code>napi_delete_element</code></a></li>
368<li><a href="#napi_define_properties"><code>napi_define_properties</code></a></li>
369<li><a href="#napi_object_freeze"><code>napi_object_freeze</code></a></li>
370<li><a href="#napi_object_seal"><code>napi_object_seal</code></a></li>
371</ul>
372</li>
373</ul>
374</li>
375<li><a href="#working-with-javascript-functions">Working with JavaScript functions</a>
376<ul>
377<li><a href="#napi_call_function"><code>napi_call_function</code></a></li>
378<li><a href="#napi_create_function"><code>napi_create_function</code></a></li>
379<li><a href="#napi_get_cb_info"><code>napi_get_cb_info</code></a></li>
380<li><a href="#napi_get_new_target"><code>napi_get_new_target</code></a></li>
381<li><a href="#napi_new_instance"><code>napi_new_instance</code></a></li>
382</ul>
383</li>
384<li><a href="#object-wrap">Object wrap</a>
385<ul>
386<li><a href="#napi_define_class"><code>napi_define_class</code></a></li>
387<li><a href="#napi_wrap"><code>napi_wrap</code></a></li>
388<li><a href="#napi_unwrap"><code>napi_unwrap</code></a></li>
389<li><a href="#napi_remove_wrap"><code>napi_remove_wrap</code></a></li>
390<li><a href="#napi_type_tag_object"><code>napi_type_tag_object</code></a></li>
391<li><a href="#napi_check_object_type_tag"><code>napi_check_object_type_tag</code></a></li>
392<li><a href="#napi_add_finalizer"><code>napi_add_finalizer</code></a></li>
393</ul>
394</li>
395<li><a href="#simple-asynchronous-operations">Simple asynchronous operations</a>
396<ul>
397<li><a href="#napi_create_async_work"><code>napi_create_async_work</code></a></li>
398<li><a href="#napi_delete_async_work"><code>napi_delete_async_work</code></a></li>
399<li><a href="#napi_queue_async_work"><code>napi_queue_async_work</code></a></li>
400<li><a href="#napi_cancel_async_work"><code>napi_cancel_async_work</code></a></li>
401</ul>
402</li>
403<li><a href="#custom-asynchronous-operations">Custom asynchronous operations</a>
404<ul>
405<li><a href="#napi_async_init"><code>napi_async_init</code></a></li>
406<li><a href="#napi_async_destroy"><code>napi_async_destroy</code></a></li>
407<li><a href="#napi_make_callback"><code>napi_make_callback</code></a></li>
408<li><a href="#napi_open_callback_scope"><code>napi_open_callback_scope</code></a></li>
409<li><a href="#napi_close_callback_scope"><code>napi_close_callback_scope</code></a></li>
410</ul>
411</li>
412<li><a href="#version-management">Version management</a>
413<ul>
414<li><a href="#napi_get_node_version"><code>napi_get_node_version</code></a></li>
415<li><a href="#napi_get_version"><code>napi_get_version</code></a></li>
416</ul>
417</li>
418<li><a href="#memory-management">Memory management</a>
419<ul>
420<li><a href="#napi_adjust_external_memory"><code>napi_adjust_external_memory</code></a></li>
421</ul>
422</li>
423<li><a href="#promises">Promises</a>
424<ul>
425<li><a href="#napi_create_promise"><code>napi_create_promise</code></a></li>
426<li><a href="#napi_resolve_deferred"><code>napi_resolve_deferred</code></a></li>
427<li><a href="#napi_reject_deferred"><code>napi_reject_deferred</code></a></li>
428<li><a href="#napi_is_promise"><code>napi_is_promise</code></a></li>
429</ul>
430</li>
431<li><a href="#script-execution">Script execution</a>
432<ul>
433<li><a href="#napi_run_script"><code>napi_run_script</code></a></li>
434</ul>
435</li>
436<li><a href="#libuv-event-loop">libuv event loop</a>
437<ul>
438<li><a href="#napi_get_uv_event_loop"><code>napi_get_uv_event_loop</code></a></li>
439</ul>
440</li>
441<li><a href="#asynchronous-thread-safe-function-calls">Asynchronous thread-safe function calls</a>
442<ul>
443<li><a href="#calling-a-thread-safe-function">Calling a thread-safe function</a></li>
444<li><a href="#reference-counting-of-thread-safe-functions">Reference counting of thread-safe functions</a></li>
445<li><a href="#deciding-whether-to-keep-the-process-running">Deciding whether to keep the process running</a></li>
446<li><a href="#napi_create_threadsafe_function"><code>napi_create_threadsafe_function</code></a></li>
447<li><a href="#napi_get_threadsafe_function_context"><code>napi_get_threadsafe_function_context</code></a></li>
448<li><a href="#napi_call_threadsafe_function"><code>napi_call_threadsafe_function</code></a></li>
449<li><a href="#napi_acquire_threadsafe_function"><code>napi_acquire_threadsafe_function</code></a></li>
450<li><a href="#napi_release_threadsafe_function"><code>napi_release_threadsafe_function</code></a></li>
451<li><a href="#napi_ref_threadsafe_function"><code>napi_ref_threadsafe_function</code></a></li>
452<li><a href="#napi_unref_threadsafe_function"><code>napi_unref_threadsafe_function</code></a></li>
453</ul>
454</li>
455<li><a href="#miscellaneous-utilities">Miscellaneous utilities</a>
456<ul>
457<li><a href="#node_api_get_module_file_name"><code>node_api_get_module_file_name</code></a></li>
458</ul>
459</li>
460</ul>
461</li>
462</ul></div></div>
463    </li>
464
465
466    <li class="picker-header">
467      <a href="#">
468        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
469        Index
470      </a>
471
472      <div class="picker"><ul>
473<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
474<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
475
476      <li>
477        <a href="index.html">Index</a>
478      </li>
479    </ul>
480
481<hr class="line">
482<ul>
483<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
484<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
485<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
486<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
487<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
488<li><a href="n-api.html" class="nav-n-api active">C/C++ addons with Node-API</a></li>
489<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
490<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
491<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
492<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
493<li><a href="console.html" class="nav-console">Console</a></li>
494<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
495<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
496<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
497<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
498<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
499<li><a href="dns.html" class="nav-dns">DNS</a></li>
500<li><a href="domain.html" class="nav-domain">Domain</a></li>
501<li><a href="errors.html" class="nav-errors">Errors</a></li>
502<li><a href="events.html" class="nav-events">Events</a></li>
503<li><a href="fs.html" class="nav-fs">File system</a></li>
504<li><a href="globals.html" class="nav-globals">Globals</a></li>
505<li><a href="http.html" class="nav-http">HTTP</a></li>
506<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
507<li><a href="https.html" class="nav-https">HTTPS</a></li>
508<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
509<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
510<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
511<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
512<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
513<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
514<li><a href="net.html" class="nav-net">Net</a></li>
515<li><a href="os.html" class="nav-os">OS</a></li>
516<li><a href="path.html" class="nav-path">Path</a></li>
517<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
518<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
519<li><a href="process.html" class="nav-process">Process</a></li>
520<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
521<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
522<li><a href="readline.html" class="nav-readline">Readline</a></li>
523<li><a href="repl.html" class="nav-repl">REPL</a></li>
524<li><a href="report.html" class="nav-report">Report</a></li>
525<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
526<li><a href="stream.html" class="nav-stream">Stream</a></li>
527<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
528<li><a href="test.html" class="nav-test">Test runner</a></li>
529<li><a href="timers.html" class="nav-timers">Timers</a></li>
530<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
531<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
532<li><a href="tty.html" class="nav-tty">TTY</a></li>
533<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
534<li><a href="url.html" class="nav-url">URL</a></li>
535<li><a href="util.html" class="nav-util">Utilities</a></li>
536<li><a href="v8.html" class="nav-v8">V8</a></li>
537<li><a href="vm.html" class="nav-vm">VM</a></li>
538<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
539<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
540<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
541<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
542<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
543</ul>
544<hr class="line">
545<ul>
546<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
547</ul></div>
548    </li>
549
550
551    <li class="picker-header">
552      <a href="#">
553        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
554        Other versions
555      </a>
556      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v20.x/api/n-api.html">20.x</a></li>
557<li><a href="https://nodejs.org/docs/latest-v19.x/api/n-api.html">19.x</a></li>
558<li><a href="https://nodejs.org/docs/latest-v18.x/api/n-api.html">18.x <b>LTS</b></a></li>
559<li><a href="https://nodejs.org/docs/latest-v17.x/api/n-api.html">17.x</a></li>
560<li><a href="https://nodejs.org/docs/latest-v16.x/api/n-api.html">16.x</a></li>
561<li><a href="https://nodejs.org/docs/latest-v15.x/api/n-api.html">15.x</a></li>
562<li><a href="https://nodejs.org/docs/latest-v14.x/api/n-api.html">14.x</a></li>
563<li><a href="https://nodejs.org/docs/latest-v13.x/api/n-api.html">13.x</a></li>
564<li><a href="https://nodejs.org/docs/latest-v12.x/api/n-api.html">12.x</a></li>
565<li><a href="https://nodejs.org/docs/latest-v11.x/api/n-api.html">11.x</a></li>
566<li><a href="https://nodejs.org/docs/latest-v10.x/api/n-api.html">10.x</a></li>
567<li><a href="https://nodejs.org/docs/latest-v9.x/api/n-api.html">9.x</a></li>
568<li><a href="https://nodejs.org/docs/latest-v8.x/api/n-api.html">8.x</a></li></ol></div>
569    </li>
570
571            <li class="picker-header">
572              <a href="#">
573                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
574                Options
575              </a>
576
577              <div class="picker">
578                <ul>
579                  <li>
580                    <a href="all.html">View on single page</a>
581                  </li>
582                  <li>
583                    <a href="n-api.json">View as JSON</a>
584                  </li>
585                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/n-api.md">Edit on GitHub</a></li>
586                </ul>
587              </div>
588            </li>
589          </ul>
590        </div>
591        <hr>
592      </header>
593
594      <details id="toc" open><summary>Table of contents</summary><ul>
595<li><span class="stability_2"><a href="#node-api">Node-API</a></span>
596<ul>
597<li><a href="#implications-of-abi-stability">Implications of ABI stability</a></li>
598<li><a href="#building">Building</a>
599<ul>
600<li><a href="#build-tools">Build tools</a>
601<ul>
602<li><a href="#node-gyp">node-gyp</a></li>
603<li><a href="#cmakejs">CMake.js</a></li>
604</ul>
605</li>
606<li><a href="#uploading-precompiled-binaries">Uploading precompiled binaries</a>
607<ul>
608<li><a href="#node-pre-gyp">node-pre-gyp</a></li>
609<li><a href="#prebuild">prebuild</a></li>
610<li><a href="#prebuildify">prebuildify</a></li>
611</ul>
612</li>
613</ul>
614</li>
615<li><a href="#usage">Usage</a></li>
616<li><a href="#node-api-version-matrix">Node-API version matrix</a></li>
617<li><a href="#environment-life-cycle-apis">Environment life cycle APIs</a>
618<ul>
619<li><a href="#napi_set_instance_data"><code>napi_set_instance_data</code></a></li>
620<li><a href="#napi_get_instance_data"><code>napi_get_instance_data</code></a></li>
621</ul>
622</li>
623<li><a href="#basic-node-api-data-types">Basic Node-API data types</a>
624<ul>
625<li><a href="#napi_status"><code>napi_status</code></a></li>
626<li><a href="#napi_extended_error_info"><code>napi_extended_error_info</code></a></li>
627<li><a href="#napi_env"><code>napi_env</code></a></li>
628<li><a href="#napi_value"><code>napi_value</code></a></li>
629<li><a href="#napi_threadsafe_function"><code>napi_threadsafe_function</code></a></li>
630<li><a href="#napi_threadsafe_function_release_mode"><code>napi_threadsafe_function_release_mode</code></a></li>
631<li><a href="#napi_threadsafe_function_call_mode"><code>napi_threadsafe_function_call_mode</code></a></li>
632<li><a href="#node-api-memory-management-types">Node-API memory management types</a>
633<ul>
634<li><a href="#napi_handle_scope"><code>napi_handle_scope</code></a></li>
635<li><a href="#napi_escapable_handle_scope"><code>napi_escapable_handle_scope</code></a></li>
636<li><a href="#napi_ref"><code>napi_ref</code></a></li>
637<li><a href="#napi_type_tag"><code>napi_type_tag</code></a></li>
638<li><a href="#napi_async_cleanup_hook_handle"><code>napi_async_cleanup_hook_handle</code></a></li>
639</ul>
640</li>
641<li><a href="#node-api-callback-types">Node-API callback types</a>
642<ul>
643<li><a href="#napi_callback_info"><code>napi_callback_info</code></a></li>
644<li><a href="#napi_callback"><code>napi_callback</code></a></li>
645<li><a href="#napi_finalize"><code>napi_finalize</code></a></li>
646<li><a href="#napi_async_execute_callback"><code>napi_async_execute_callback</code></a></li>
647<li><a href="#napi_async_complete_callback"><code>napi_async_complete_callback</code></a></li>
648<li><a href="#napi_threadsafe_function_call_js"><code>napi_threadsafe_function_call_js</code></a></li>
649<li><a href="#napi_cleanup_hook"><code>napi_cleanup_hook</code></a></li>
650<li><a href="#napi_async_cleanup_hook"><code>napi_async_cleanup_hook</code></a></li>
651</ul>
652</li>
653</ul>
654</li>
655<li><a href="#error-handling">Error handling</a>
656<ul>
657<li><a href="#return-values">Return values</a>
658<ul>
659<li><a href="#napi_get_last_error_info"><code>napi_get_last_error_info</code></a></li>
660</ul>
661</li>
662<li><a href="#exceptions">Exceptions</a>
663<ul>
664<li><a href="#napi_throw"><code>napi_throw</code></a></li>
665<li><a href="#napi_throw_error"><code>napi_throw_error</code></a></li>
666<li><a href="#napi_throw_type_error"><code>napi_throw_type_error</code></a></li>
667<li><a href="#napi_throw_range_error"><code>napi_throw_range_error</code></a></li>
668<li><a href="#node_api_throw_syntax_error"><code>node_api_throw_syntax_error</code></a></li>
669<li><a href="#napi_is_error"><code>napi_is_error</code></a></li>
670<li><a href="#napi_create_error"><code>napi_create_error</code></a></li>
671<li><a href="#napi_create_type_error"><code>napi_create_type_error</code></a></li>
672<li><a href="#napi_create_range_error"><code>napi_create_range_error</code></a></li>
673<li><a href="#node_api_create_syntax_error"><code>node_api_create_syntax_error</code></a></li>
674<li><a href="#napi_get_and_clear_last_exception"><code>napi_get_and_clear_last_exception</code></a></li>
675<li><a href="#napi_is_exception_pending"><code>napi_is_exception_pending</code></a></li>
676<li><a href="#napi_fatal_exception"><code>napi_fatal_exception</code></a></li>
677</ul>
678</li>
679<li><a href="#fatal-errors">Fatal errors</a>
680<ul>
681<li><a href="#napi_fatal_error"><code>napi_fatal_error</code></a></li>
682</ul>
683</li>
684</ul>
685</li>
686<li><a href="#object-lifetime-management">Object lifetime management</a>
687<ul>
688<li><a href="#making-handle-lifespan-shorter-than-that-of-the-native-method">Making handle lifespan shorter than that of the native method</a>
689<ul>
690<li><a href="#napi_open_handle_scope"><code>napi_open_handle_scope</code></a></li>
691<li><a href="#napi_close_handle_scope"><code>napi_close_handle_scope</code></a></li>
692<li><a href="#napi_open_escapable_handle_scope"><code>napi_open_escapable_handle_scope</code></a></li>
693<li><a href="#napi_close_escapable_handle_scope"><code>napi_close_escapable_handle_scope</code></a></li>
694<li><a href="#napi_escape_handle"><code>napi_escape_handle</code></a></li>
695</ul>
696</li>
697<li><a href="#references-to-values-with-a-lifespan-longer-than-that-of-the-native-method">References to values with a lifespan longer than that of the native method</a>
698<ul>
699<li><a href="#napi_create_reference"><code>napi_create_reference</code></a></li>
700<li><a href="#napi_delete_reference"><code>napi_delete_reference</code></a></li>
701<li><a href="#napi_reference_ref"><code>napi_reference_ref</code></a></li>
702<li><a href="#napi_reference_unref"><code>napi_reference_unref</code></a></li>
703<li><a href="#napi_get_reference_value"><code>napi_get_reference_value</code></a></li>
704</ul>
705</li>
706<li><a href="#cleanup-on-exit-of-the-current-nodejs-environment">Cleanup on exit of the current Node.js environment</a>
707<ul>
708<li><a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a></li>
709<li><a href="#napi_remove_env_cleanup_hook"><code>napi_remove_env_cleanup_hook</code></a></li>
710<li><a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a></li>
711<li><a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a></li>
712</ul>
713</li>
714<li><a href="#finalization-on-the-exit-of-the-nodejs-environment">Finalization on the exit of the Node.js environment</a></li>
715</ul>
716</li>
717<li><a href="#module-registration">Module registration</a></li>
718<li><a href="#working-with-javascript-values">Working with JavaScript values</a>
719<ul>
720<li><a href="#enum-types">Enum types</a>
721<ul>
722<li><a href="#napi_key_collection_mode"><code>napi_key_collection_mode</code></a></li>
723<li><a href="#napi_key_filter"><code>napi_key_filter</code></a></li>
724<li><a href="#napi_key_conversion"><code>napi_key_conversion</code></a></li>
725<li><a href="#napi_valuetype"><code>napi_valuetype</code></a></li>
726<li><a href="#napi_typedarray_type"><code>napi_typedarray_type</code></a></li>
727</ul>
728</li>
729<li><a href="#object-creation-functions">Object creation functions</a>
730<ul>
731<li><a href="#napi_create_array"><code>napi_create_array</code></a></li>
732<li><a href="#napi_create_array_with_length"><code>napi_create_array_with_length</code></a></li>
733<li><a href="#napi_create_arraybuffer"><code>napi_create_arraybuffer</code></a></li>
734<li><a href="#napi_create_buffer"><code>napi_create_buffer</code></a></li>
735<li><a href="#napi_create_buffer_copy"><code>napi_create_buffer_copy</code></a></li>
736<li><a href="#napi_create_date"><code>napi_create_date</code></a></li>
737<li><a href="#napi_create_external"><code>napi_create_external</code></a></li>
738<li><a href="#napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code></a></li>
739<li><a href="#napi_create_external_buffer"><code>napi_create_external_buffer</code></a></li>
740<li><a href="#napi_create_object"><code>napi_create_object</code></a></li>
741<li><a href="#napi_create_symbol"><code>napi_create_symbol</code></a></li>
742<li><a href="#node_api_symbol_for"><code>node_api_symbol_for</code></a></li>
743<li><a href="#napi_create_typedarray"><code>napi_create_typedarray</code></a></li>
744<li><a href="#napi_create_dataview"><code>napi_create_dataview</code></a></li>
745</ul>
746</li>
747<li><a href="#functions-to-convert-from-c-types-to-node-api">Functions to convert from C types to Node-API</a>
748<ul>
749<li><a href="#napi_create_int32"><code>napi_create_int32</code></a></li>
750<li><a href="#napi_create_uint32"><code>napi_create_uint32</code></a></li>
751<li><a href="#napi_create_int64"><code>napi_create_int64</code></a></li>
752<li><a href="#napi_create_double"><code>napi_create_double</code></a></li>
753<li><a href="#napi_create_bigint_int64"><code>napi_create_bigint_int64</code></a></li>
754<li><a href="#napi_create_bigint_uint64"><code>napi_create_bigint_uint64</code></a></li>
755<li><a href="#napi_create_bigint_words"><code>napi_create_bigint_words</code></a></li>
756<li><a href="#napi_create_string_latin1"><code>napi_create_string_latin1</code></a></li>
757<li><span class="stability_1"><a href="#node_api_create_external_string_latin1"><code>node_api_create_external_string_latin1</code></a></span></li>
758<li><a href="#napi_create_string_utf16"><code>napi_create_string_utf16</code></a></li>
759<li><span class="stability_1"><a href="#node_api_create_external_string_utf16"><code>node_api_create_external_string_utf16</code></a></span></li>
760<li><a href="#napi_create_string_utf8"><code>napi_create_string_utf8</code></a></li>
761</ul>
762</li>
763<li><a href="#functions-to-convert-from-node-api-to-c-types">Functions to convert from Node-API to C types</a>
764<ul>
765<li><a href="#napi_get_array_length"><code>napi_get_array_length</code></a></li>
766<li><a href="#napi_get_arraybuffer_info"><code>napi_get_arraybuffer_info</code></a></li>
767<li><a href="#napi_get_buffer_info"><code>napi_get_buffer_info</code></a></li>
768<li><a href="#napi_get_prototype"><code>napi_get_prototype</code></a></li>
769<li><a href="#napi_get_typedarray_info"><code>napi_get_typedarray_info</code></a></li>
770<li><a href="#napi_get_dataview_info"><code>napi_get_dataview_info</code></a></li>
771<li><a href="#napi_get_date_value"><code>napi_get_date_value</code></a></li>
772<li><a href="#napi_get_value_bool"><code>napi_get_value_bool</code></a></li>
773<li><a href="#napi_get_value_double"><code>napi_get_value_double</code></a></li>
774<li><a href="#napi_get_value_bigint_int64"><code>napi_get_value_bigint_int64</code></a></li>
775<li><a href="#napi_get_value_bigint_uint64"><code>napi_get_value_bigint_uint64</code></a></li>
776<li><a href="#napi_get_value_bigint_words"><code>napi_get_value_bigint_words</code></a></li>
777<li><a href="#napi_get_value_external"><code>napi_get_value_external</code></a></li>
778<li><a href="#napi_get_value_int32"><code>napi_get_value_int32</code></a></li>
779<li><a href="#napi_get_value_int64"><code>napi_get_value_int64</code></a></li>
780<li><a href="#napi_get_value_string_latin1"><code>napi_get_value_string_latin1</code></a></li>
781<li><a href="#napi_get_value_string_utf8"><code>napi_get_value_string_utf8</code></a></li>
782<li><a href="#napi_get_value_string_utf16"><code>napi_get_value_string_utf16</code></a></li>
783<li><a href="#napi_get_value_uint32"><code>napi_get_value_uint32</code></a></li>
784</ul>
785</li>
786<li><a href="#functions-to-get-global-instances">Functions to get global instances</a>
787<ul>
788<li><a href="#napi_get_boolean"><code>napi_get_boolean</code></a></li>
789<li><a href="#napi_get_global"><code>napi_get_global</code></a></li>
790<li><a href="#napi_get_null"><code>napi_get_null</code></a></li>
791<li><a href="#napi_get_undefined"><code>napi_get_undefined</code></a></li>
792</ul>
793</li>
794</ul>
795</li>
796<li><a href="#working-with-javascript-values-and-abstract-operations">Working with JavaScript values and abstract operations</a>
797<ul>
798<li><a href="#napi_coerce_to_bool"><code>napi_coerce_to_bool</code></a></li>
799<li><a href="#napi_coerce_to_number"><code>napi_coerce_to_number</code></a></li>
800<li><a href="#napi_coerce_to_object"><code>napi_coerce_to_object</code></a></li>
801<li><a href="#napi_coerce_to_string"><code>napi_coerce_to_string</code></a></li>
802<li><a href="#napi_typeof"><code>napi_typeof</code></a></li>
803<li><a href="#napi_instanceof"><code>napi_instanceof</code></a></li>
804<li><a href="#napi_is_array"><code>napi_is_array</code></a></li>
805<li><a href="#napi_is_arraybuffer"><code>napi_is_arraybuffer</code></a></li>
806<li><a href="#napi_is_buffer"><code>napi_is_buffer</code></a></li>
807<li><a href="#napi_is_date"><code>napi_is_date</code></a></li>
808<li><a href="#napi_is_error_1"><code>napi_is_error</code></a></li>
809<li><a href="#napi_is_typedarray"><code>napi_is_typedarray</code></a></li>
810<li><a href="#napi_is_dataview"><code>napi_is_dataview</code></a></li>
811<li><a href="#napi_strict_equals"><code>napi_strict_equals</code></a></li>
812<li><a href="#napi_detach_arraybuffer"><code>napi_detach_arraybuffer</code></a></li>
813<li><a href="#napi_is_detached_arraybuffer"><code>napi_is_detached_arraybuffer</code></a></li>
814</ul>
815</li>
816<li><a href="#working-with-javascript-properties">Working with JavaScript properties</a>
817<ul>
818<li><a href="#structures">Structures</a>
819<ul>
820<li><a href="#napi_property_attributes"><code>napi_property_attributes</code></a></li>
821<li><a href="#napi_property_descriptor"><code>napi_property_descriptor</code></a></li>
822</ul>
823</li>
824<li><a href="#functions">Functions</a>
825<ul>
826<li><a href="#napi_get_property_names"><code>napi_get_property_names</code></a></li>
827<li><a href="#napi_get_all_property_names"><code>napi_get_all_property_names</code></a></li>
828<li><a href="#napi_set_property"><code>napi_set_property</code></a></li>
829<li><a href="#napi_get_property"><code>napi_get_property</code></a></li>
830<li><a href="#napi_has_property"><code>napi_has_property</code></a></li>
831<li><a href="#napi_delete_property"><code>napi_delete_property</code></a></li>
832<li><a href="#napi_has_own_property"><code>napi_has_own_property</code></a></li>
833<li><a href="#napi_set_named_property"><code>napi_set_named_property</code></a></li>
834<li><a href="#napi_get_named_property"><code>napi_get_named_property</code></a></li>
835<li><a href="#napi_has_named_property"><code>napi_has_named_property</code></a></li>
836<li><a href="#napi_set_element"><code>napi_set_element</code></a></li>
837<li><a href="#napi_get_element"><code>napi_get_element</code></a></li>
838<li><a href="#napi_has_element"><code>napi_has_element</code></a></li>
839<li><a href="#napi_delete_element"><code>napi_delete_element</code></a></li>
840<li><a href="#napi_define_properties"><code>napi_define_properties</code></a></li>
841<li><a href="#napi_object_freeze"><code>napi_object_freeze</code></a></li>
842<li><a href="#napi_object_seal"><code>napi_object_seal</code></a></li>
843</ul>
844</li>
845</ul>
846</li>
847<li><a href="#working-with-javascript-functions">Working with JavaScript functions</a>
848<ul>
849<li><a href="#napi_call_function"><code>napi_call_function</code></a></li>
850<li><a href="#napi_create_function"><code>napi_create_function</code></a></li>
851<li><a href="#napi_get_cb_info"><code>napi_get_cb_info</code></a></li>
852<li><a href="#napi_get_new_target"><code>napi_get_new_target</code></a></li>
853<li><a href="#napi_new_instance"><code>napi_new_instance</code></a></li>
854</ul>
855</li>
856<li><a href="#object-wrap">Object wrap</a>
857<ul>
858<li><a href="#napi_define_class"><code>napi_define_class</code></a></li>
859<li><a href="#napi_wrap"><code>napi_wrap</code></a></li>
860<li><a href="#napi_unwrap"><code>napi_unwrap</code></a></li>
861<li><a href="#napi_remove_wrap"><code>napi_remove_wrap</code></a></li>
862<li><a href="#napi_type_tag_object"><code>napi_type_tag_object</code></a></li>
863<li><a href="#napi_check_object_type_tag"><code>napi_check_object_type_tag</code></a></li>
864<li><a href="#napi_add_finalizer"><code>napi_add_finalizer</code></a></li>
865</ul>
866</li>
867<li><a href="#simple-asynchronous-operations">Simple asynchronous operations</a>
868<ul>
869<li><a href="#napi_create_async_work"><code>napi_create_async_work</code></a></li>
870<li><a href="#napi_delete_async_work"><code>napi_delete_async_work</code></a></li>
871<li><a href="#napi_queue_async_work"><code>napi_queue_async_work</code></a></li>
872<li><a href="#napi_cancel_async_work"><code>napi_cancel_async_work</code></a></li>
873</ul>
874</li>
875<li><a href="#custom-asynchronous-operations">Custom asynchronous operations</a>
876<ul>
877<li><a href="#napi_async_init"><code>napi_async_init</code></a></li>
878<li><a href="#napi_async_destroy"><code>napi_async_destroy</code></a></li>
879<li><a href="#napi_make_callback"><code>napi_make_callback</code></a></li>
880<li><a href="#napi_open_callback_scope"><code>napi_open_callback_scope</code></a></li>
881<li><a href="#napi_close_callback_scope"><code>napi_close_callback_scope</code></a></li>
882</ul>
883</li>
884<li><a href="#version-management">Version management</a>
885<ul>
886<li><a href="#napi_get_node_version"><code>napi_get_node_version</code></a></li>
887<li><a href="#napi_get_version"><code>napi_get_version</code></a></li>
888</ul>
889</li>
890<li><a href="#memory-management">Memory management</a>
891<ul>
892<li><a href="#napi_adjust_external_memory"><code>napi_adjust_external_memory</code></a></li>
893</ul>
894</li>
895<li><a href="#promises">Promises</a>
896<ul>
897<li><a href="#napi_create_promise"><code>napi_create_promise</code></a></li>
898<li><a href="#napi_resolve_deferred"><code>napi_resolve_deferred</code></a></li>
899<li><a href="#napi_reject_deferred"><code>napi_reject_deferred</code></a></li>
900<li><a href="#napi_is_promise"><code>napi_is_promise</code></a></li>
901</ul>
902</li>
903<li><a href="#script-execution">Script execution</a>
904<ul>
905<li><a href="#napi_run_script"><code>napi_run_script</code></a></li>
906</ul>
907</li>
908<li><a href="#libuv-event-loop">libuv event loop</a>
909<ul>
910<li><a href="#napi_get_uv_event_loop"><code>napi_get_uv_event_loop</code></a></li>
911</ul>
912</li>
913<li><a href="#asynchronous-thread-safe-function-calls">Asynchronous thread-safe function calls</a>
914<ul>
915<li><a href="#calling-a-thread-safe-function">Calling a thread-safe function</a></li>
916<li><a href="#reference-counting-of-thread-safe-functions">Reference counting of thread-safe functions</a></li>
917<li><a href="#deciding-whether-to-keep-the-process-running">Deciding whether to keep the process running</a></li>
918<li><a href="#napi_create_threadsafe_function"><code>napi_create_threadsafe_function</code></a></li>
919<li><a href="#napi_get_threadsafe_function_context"><code>napi_get_threadsafe_function_context</code></a></li>
920<li><a href="#napi_call_threadsafe_function"><code>napi_call_threadsafe_function</code></a></li>
921<li><a href="#napi_acquire_threadsafe_function"><code>napi_acquire_threadsafe_function</code></a></li>
922<li><a href="#napi_release_threadsafe_function"><code>napi_release_threadsafe_function</code></a></li>
923<li><a href="#napi_ref_threadsafe_function"><code>napi_ref_threadsafe_function</code></a></li>
924<li><a href="#napi_unref_threadsafe_function"><code>napi_unref_threadsafe_function</code></a></li>
925</ul>
926</li>
927<li><a href="#miscellaneous-utilities">Miscellaneous utilities</a>
928<ul>
929<li><a href="#node_api_get_module_file_name"><code>node_api_get_module_file_name</code></a></li>
930</ul>
931</li>
932</ul>
933</li>
934</ul></details>
935
936      <div id="apicontent">
937        <h2>Node-API<span><a class="mark" href="#node-api" id="node-api">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api"></a></h2>
938
939
940<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
941<p>Node-API (formerly N-API) is an API for building native Addons. It is
942independent from the underlying JavaScript runtime (for example, V8) and is
943maintained as part of Node.js itself. This API will be Application Binary
944Interface (ABI) stable across versions of Node.js. It is intended to insulate
945addons from changes in the underlying JavaScript engine and allow modules
946compiled for one major version to run on later major versions of Node.js without
947recompilation. The <a href="https://nodejs.org/en/docs/guides/abi-stability/">ABI Stability</a> guide provides a more in-depth explanation.</p>
948<p>Addons are built/packaged with the same approach/tools outlined in the section
949titled <a href="addons.html">C++ Addons</a>. The only difference is the set of APIs that are used by
950the native code. Instead of using the V8 or <a href="https://github.com/nodejs/nan">Native Abstractions for Node.js</a>
951APIs, the functions available in Node-API are used.</p>
952<p>APIs exposed by Node-API are generally used to create and manipulate
953JavaScript values. Concepts and operations generally map to ideas specified
954in the ECMA-262 Language Specification. The APIs have the following
955properties:</p>
956<ul>
957<li>All Node-API calls return a status code of type <code>napi_status</code>. This
958status indicates whether the API call succeeded or failed.</li>
959<li>The API's return value is passed via an out parameter.</li>
960<li>All JavaScript values are abstracted behind an opaque type named
961<code>napi_value</code>.</li>
962<li>In case of an error status code, additional information can be obtained
963using <code>napi_get_last_error_info</code>. More information can be found in the error
964handling section <a href="#error-handling">Error handling</a>.</li>
965</ul>
966<p>Node-API is a C API that ensures ABI stability across Node.js versions
967and different compiler levels. A C++ API can be easier to use.
968To support using C++, the project maintains a
969C++ wrapper module called <a href="https://github.com/nodejs/node-addon-api"><code>node-addon-api</code></a>.
970This wrapper provides an inlinable C++ API. Binaries built
971with <code>node-addon-api</code> will depend on the symbols for the Node-API C-based
972functions exported by Node.js. <code>node-addon-api</code> is a more
973efficient way to write code that calls Node-API. Take, for example, the
974following <code>node-addon-api</code> code. The first section shows the
975<code>node-addon-api</code> code and the second section shows what actually gets
976used in the addon.</p>
977<pre><code class="language-cpp">Object obj = Object::<span class="hljs-built_in">New</span>(env);
978obj[<span class="hljs-string">"foo"</span>] = String::<span class="hljs-built_in">New</span>(env, <span class="hljs-string">"bar"</span>);</code> <button class="copy-button">copy</button></pre>
979<pre><code class="language-cpp">napi_status status;
980napi_value object, string;
981status = <span class="hljs-built_in">napi_create_object</span>(env, &#x26;object);
982<span class="hljs-keyword">if</span> (status != napi_ok) {
983  <span class="hljs-built_in">napi_throw_error</span>(env, ...);
984  <span class="hljs-keyword">return</span>;
985}
986
987status = <span class="hljs-built_in">napi_create_string_utf8</span>(env, <span class="hljs-string">"bar"</span>, NAPI_AUTO_LENGTH, &#x26;string);
988<span class="hljs-keyword">if</span> (status != napi_ok) {
989  <span class="hljs-built_in">napi_throw_error</span>(env, ...);
990  <span class="hljs-keyword">return</span>;
991}
992
993status = <span class="hljs-built_in">napi_set_named_property</span>(env, object, <span class="hljs-string">"foo"</span>, string);
994<span class="hljs-keyword">if</span> (status != napi_ok) {
995  <span class="hljs-built_in">napi_throw_error</span>(env, ...);
996  <span class="hljs-keyword">return</span>;
997}</code> <button class="copy-button">copy</button></pre>
998<p>The end result is that the addon only uses the exported C APIs. As a result,
999it still gets the benefits of the ABI stability provided by the C API.</p>
1000<p>When using <code>node-addon-api</code> instead of the C APIs, start with the API <a href="https://github.com/nodejs/node-addon-api#api-documentation">docs</a>
1001for <code>node-addon-api</code>.</p>
1002<p>The <a href="https://nodejs.github.io/node-addon-examples/">Node-API Resource</a> offers
1003an excellent orientation and tips for developers just getting started with
1004Node-API and <code>node-addon-api</code>. Additional media resources can be found on the
1005<a href="https://github.com/nodejs/abi-stable-node/blob/HEAD/node-api-media.md">Node-API Media</a> page.</p>
1006<section><h3>Implications of ABI stability<span><a class="mark" href="#implications-of-abi-stability" id="implications-of-abi-stability">#</a></span><a aria-hidden="true" class="legacy" id="n_api_implications_of_abi_stability"></a></h3>
1007<p>Although Node-API provides an ABI stability guarantee, other parts of Node.js do
1008not, and any external libraries used from the addon may not. In particular,
1009none of the following APIs provide an ABI stability guarantee across major
1010versions:</p>
1011<ul>
1012<li>
1013<p>the Node.js C++ APIs available via any of</p>
1014<pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node.h></span></span>
1015<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_buffer.h></span></span>
1016<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_version.h></span></span>
1017<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_object_wrap.h></span></span></code> <button class="copy-button">copy</button></pre>
1018</li>
1019<li>
1020<p>the libuv APIs which are also included with Node.js and available via</p>
1021<pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;uv.h></span></span></code> <button class="copy-button">copy</button></pre>
1022</li>
1023<li>
1024<p>the V8 API available via</p>
1025<pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;v8.h></span></span></code> <button class="copy-button">copy</button></pre>
1026</li>
1027</ul>
1028<p>Thus, for an addon to remain ABI-compatible across Node.js major versions, it
1029must use Node-API exclusively by restricting itself to using</p>
1030<pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_api.h></span></span></code> <button class="copy-button">copy</button></pre>
1031<p>and by checking, for all external libraries that it uses, that the external
1032library makes ABI stability guarantees similar to Node-API.</p>
1033</section><section><h3>Building<span><a class="mark" href="#building" id="building">#</a></span><a aria-hidden="true" class="legacy" id="n_api_building"></a></h3>
1034<p>Unlike modules written in JavaScript, developing and deploying Node.js
1035native addons using Node-API requires an additional set of tools. Besides the
1036basic tools required to develop for Node.js, the native addon developer
1037requires a toolchain that can compile C and C++ code into a binary. In
1038addition, depending upon how the native addon is deployed, the <em>user</em> of
1039the native addon will also need to have a C/C++ toolchain installed.</p>
1040<p>For Linux developers, the necessary C/C++ toolchain packages are readily
1041available. <a href="https://gcc.gnu.org">GCC</a> is widely used in the Node.js community to build and
1042test across a variety of platforms. For many developers, the <a href="https://llvm.org">LLVM</a>
1043compiler infrastructure is also a good choice.</p>
1044<p>For Mac developers, <a href="https://developer.apple.com/xcode/">Xcode</a> offers all the required compiler tools.
1045However, it is not necessary to install the entire Xcode IDE. The following
1046command installs the necessary toolchain:</p>
1047<pre><code class="language-bash">xcode-select --install</code> <button class="copy-button">copy</button></pre>
1048<p>For Windows developers, <a href="https://visualstudio.microsoft.com">Visual Studio</a> offers all the required compiler
1049tools. However, it is not necessary to install the entire Visual Studio
1050IDE. The following command installs the necessary toolchain:</p>
1051<pre><code class="language-bash">npm install --global windows-build-tools</code> <button class="copy-button">copy</button></pre>
1052<p>The sections below describe the additional tools available for developing
1053and deploying Node.js native addons.</p>
1054<h4>Build tools<span><a class="mark" href="#build-tools" id="build-tools">#</a></span><a aria-hidden="true" class="legacy" id="n_api_build_tools"></a></h4>
1055<p>Both the tools listed here require that <em>users</em> of the native
1056addon have a C/C++ toolchain installed in order to successfully install
1057the native addon.</p>
1058<h5>node-gyp<span><a class="mark" href="#node-gyp" id="node-gyp">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_gyp"></a></h5>
1059<p><a href="https://github.com/nodejs/node-gyp">node-gyp</a> is a build system based on the <a href="https://github.com/nodejs/gyp-next">gyp-next</a> fork of
1060Google's <a href="https://gyp.gsrc.io">GYP</a> tool and comes bundled with npm. GYP, and therefore node-gyp,
1061requires that Python be installed.</p>
1062<p>Historically, node-gyp has been the tool of choice for building native
1063addons. It has widespread adoption and documentation. However, some
1064developers have run into limitations in node-gyp.</p>
1065<h5>CMake.js<span><a class="mark" href="#cmakejs" id="cmakejs">#</a></span><a aria-hidden="true" class="legacy" id="n_api_cmake_js"></a></h5>
1066<p><a href="https://github.com/cmake-js/cmake-js">CMake.js</a> is an alternative build system based on <a href="https://cmake.org">CMake</a>.</p>
1067<p>CMake.js is a good choice for projects that already use CMake or for
1068developers affected by limitations in node-gyp.</p>
1069<h4>Uploading precompiled binaries<span><a class="mark" href="#uploading-precompiled-binaries" id="uploading-precompiled-binaries">#</a></span><a aria-hidden="true" class="legacy" id="n_api_uploading_precompiled_binaries"></a></h4>
1070<p>The three tools listed here permit native addon developers and maintainers
1071to create and upload binaries to public or private servers. These tools are
1072typically integrated with CI/CD build systems like <a href="https://travis-ci.org">Travis CI</a> and
1073<a href="https://www.appveyor.com">AppVeyor</a> to build and upload binaries for a variety of platforms and
1074architectures. These binaries are then available for download by users who
1075do not need to have a C/C++ toolchain installed.</p>
1076<h5>node-pre-gyp<span><a class="mark" href="#node-pre-gyp" id="node-pre-gyp">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_pre_gyp"></a></h5>
1077<p><a href="https://github.com/mapbox/node-pre-gyp">node-pre-gyp</a> is a tool based on node-gyp that adds the ability to
1078upload binaries to a server of the developer's choice. node-pre-gyp has
1079particularly good support for uploading binaries to Amazon S3.</p>
1080<h5>prebuild<span><a class="mark" href="#prebuild" id="prebuild">#</a></span><a aria-hidden="true" class="legacy" id="n_api_prebuild"></a></h5>
1081<p><a href="https://github.com/prebuild/prebuild">prebuild</a> is a tool that supports builds using either node-gyp or
1082CMake.js. Unlike node-pre-gyp which supports a variety of servers, prebuild
1083uploads binaries only to <a href="https://help.github.com/en/github/administering-a-repository/about-releases">GitHub releases</a>. prebuild is a good choice for
1084GitHub projects using CMake.js.</p>
1085<h5>prebuildify<span><a class="mark" href="#prebuildify" id="prebuildify">#</a></span><a aria-hidden="true" class="legacy" id="n_api_prebuildify"></a></h5>
1086<p><a href="https://github.com/prebuild/prebuildify">prebuildify</a> is a tool based on node-gyp. The advantage of prebuildify is
1087that the built binaries are bundled with the native addon when it's
1088uploaded to npm. The binaries are downloaded from npm and are immediately
1089available to the module user when the native addon is installed.</p>
1090</section><section><h3>Usage<span><a class="mark" href="#usage" id="usage">#</a></span><a aria-hidden="true" class="legacy" id="n_api_usage"></a></h3>
1091<p>In order to use the Node-API functions, include the file <a href="https://github.com/nodejs/node/blob/HEAD/src/node_api.h"><code>node_api.h</code></a> which
1092is located in the src directory in the node development tree:</p>
1093<pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_api.h></span></span></code> <button class="copy-button">copy</button></pre>
1094<p>This will opt into the default <code>NAPI_VERSION</code> for the given release of Node.js.
1095In order to ensure compatibility with specific versions of Node-API, the version
1096can be specified explicitly when including the header:</p>
1097<pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_VERSION 3</span>
1098<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_api.h></span></span></code> <button class="copy-button">copy</button></pre>
1099<p>This restricts the Node-API surface to just the functionality that was available
1100in the specified (and earlier) versions.</p>
1101<p>Some of the Node-API surface is experimental and requires explicit opt-in:</p>
1102<pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_EXPERIMENTAL</span>
1103<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_api.h></span></span></code> <button class="copy-button">copy</button></pre>
1104<p>In this case the entire API surface, including any experimental APIs, will be
1105available to the module code.</p>
1106</section><section><h3>Node-API version matrix<span><a class="mark" href="#node-api-version-matrix" id="node-api-version-matrix">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_version_matrix"></a></h3>
1107<p>Node-API versions are additive and versioned independently from Node.js.
1108Version 4 is an extension to version 3 in that it has all of the APIs
1109from version 3 with some additions. This means that it is not necessary
1110to recompile for new versions of Node.js which are
1111listed as supporting a later version.</p>
1112<!-- For accessibility purposes, this table needs row headers. That means we
1113     can't do it in markdown. Hence, the raw HTML. -->
1114<table>
1115  <tbody><tr>
1116    <td></td>
1117    <th scope="col">1</th>
1118    <th scope="col">2</th>
1119    <th scope="col">3</th>
1120  </tr>
1121  <tr>
1122    <th scope="row">v6.x</th>
1123    <td></td>
1124    <td></td>
1125    <td>v6.14.2*</td>
1126  </tr>
1127  <tr>
1128    <th scope="row">v8.x</th>
1129    <td>v8.6.0**</td>
1130    <td>v8.10.0*</td>
1131    <td>v8.11.2</td>
1132  </tr>
1133  <tr>
1134    <th scope="row">v9.x</th>
1135    <td>v9.0.0*</td>
1136    <td>v9.3.0*</td>
1137    <td>v9.11.0*</td>
1138  </tr>
1139  <tr>
1140    <th scope="row">≥ v10.x</th>
1141    <td>all releases</td>
1142    <td>all releases</td>
1143    <td>all releases</td>
1144  </tr>
1145</tbody></table>
1146<table>
1147  <tbody><tr>
1148    <td></td>
1149    <th scope="col">4</th>
1150    <th scope="col">5</th>
1151    <th scope="col">6</th>
1152    <th scope="col">7</th>
1153    <th scope="col">8</th>
1154  </tr>
1155  <tr>
1156    <th scope="row">v10.x</th>
1157    <td>v10.16.0</td>
1158    <td>v10.17.0</td>
1159    <td>v10.20.0</td>
1160    <td>v10.23.0</td>
1161    <td></td>
1162  </tr>
1163  <tr>
1164    <th scope="row">v11.x</th>
1165    <td>v11.8.0</td>
1166    <td></td>
1167    <td></td>
1168    <td></td>
1169    <td></td>
1170  </tr>
1171  <tr>
1172    <th scope="row">v12.x</th>
1173    <td>v12.0.0</td>
1174    <td>v12.11.0</td>
1175    <td>v12.17.0</td>
1176    <td>v12.19.0</td>
1177    <td>v12.22.0</td>
1178  </tr>
1179  <tr>
1180    <th scope="row">v13.x</th>
1181    <td>v13.0.0</td>
1182    <td>v13.0.0</td>
1183    <td></td>
1184    <td></td>
1185    <td></td>
1186  </tr>
1187  <tr>
1188    <th scope="row">v14.x</th>
1189    <td>v14.0.0</td>
1190    <td>v14.0.0</td>
1191    <td>v14.0.0</td>
1192    <td>v14.12.0</td>
1193    <td>v14.17.0</td>
1194  </tr>
1195  <tr>
1196    <th scope="row">v15.x</th>
1197    <td>v15.0.0</td>
1198    <td>v15.0.0</td>
1199    <td>v15.0.0</td>
1200    <td>v15.0.0</td>
1201    <td>v15.12.0</td>
1202  </tr>
1203  <tr>
1204    <th scope="row">v16.x</th>
1205    <td>v16.0.0</td>
1206    <td>v16.0.0</td>
1207    <td>v16.0.0</td>
1208    <td>v16.0.0</td>
1209    <td>v16.0.0</td>
1210  </tr>
1211</tbody></table>
1212<p>* Node-API was experimental.</p>
1213<p>** Node.js 8.0.0 included Node-API as experimental. It was released as
1214Node-API version 1 but continued to evolve until Node.js 8.6.0. The API is
1215different in versions prior to Node.js 8.6.0. We recommend Node-API version 3 or
1216later.</p>
1217<p>Each API documented for Node-API will have a header named <code>added in:</code>, and APIs
1218which are stable will have the additional header <code>Node-API version:</code>.
1219APIs are directly usable when using a Node.js version which supports
1220the Node-API version shown in <code>Node-API version:</code> or higher.
1221When using a Node.js version that does not support the
1222<code>Node-API version:</code> listed or if there is no <code>Node-API version:</code> listed,
1223then the API will only be available if
1224<code>#define NAPI_EXPERIMENTAL</code> precedes the inclusion of <code>node_api.h</code>
1225or <code>js_native_api.h</code>. If an API appears not to be available on
1226a version of Node.js which is later than the one shown in <code>added in:</code> then
1227this is most likely the reason for the apparent absence.</p>
1228<p>The Node-APIs associated strictly with accessing ECMAScript features from native
1229code can be found separately in <code>js_native_api.h</code> and <code>js_native_api_types.h</code>.
1230The APIs defined in these headers are included in <code>node_api.h</code> and
1231<code>node_api_types.h</code>. The headers are structured in this way in order to allow
1232implementations of Node-API outside of Node.js. For those implementations the
1233Node.js specific APIs may not be applicable.</p>
1234<p>The Node.js-specific parts of an addon can be separated from the code that
1235exposes the actual functionality to the JavaScript environment so that the
1236latter may be used with multiple implementations of Node-API. In the example
1237below, <code>addon.c</code> and <code>addon.h</code> refer only to <code>js_native_api.h</code>. This ensures
1238that <code>addon.c</code> can be reused to compile against either the Node.js
1239implementation of Node-API or any implementation of Node-API outside of Node.js.</p>
1240<p><code>addon_node.c</code> is a separate file that contains the Node.js specific entry point
1241to the addon and which instantiates the addon by calling into <code>addon.c</code> when the
1242addon is loaded into a Node.js environment.</p>
1243<pre><code class="language-c"><span class="hljs-comment">// addon.h</span>
1244<span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> _ADDON_H_</span>
1245<span class="hljs-meta">#<span class="hljs-keyword">define</span> _ADDON_H_</span>
1246<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;js_native_api.h></span></span>
1247napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_env env)</span>;
1248<span class="hljs-meta">#<span class="hljs-keyword">endif</span>  <span class="hljs-comment">// _ADDON_H_</span></span></code> <button class="copy-button">copy</button></pre>
1249<pre><code class="language-c"><span class="hljs-comment">// addon.c</span>
1250<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"addon.h"</span></span>
1251
1252<span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_CALL(env, call)                                      \
1253  do {                                                            \
1254    napi_status status = (call);                                  \
1255    <span class="hljs-keyword">if</span> (status != napi_ok) {                                      \
1256      const napi_extended_error_info* error_info = NULL;          \
1257      napi_get_last_error_info((env), &#x26;error_info);               \
1258      const char* err_message = error_info->error_message;        \
1259      bool is_pending;                                            \
1260      napi_is_exception_pending((env), &#x26;is_pending);              \
1261      <span class="hljs-keyword">if</span> (!is_pending) {                                          \
1262        const char* message = (err_message == NULL)               \
1263            ? <span class="hljs-string">"empty error message"</span>                               \
1264            : err_message;                                        \
1265        napi_throw_error((env), NULL, message);                   \
1266        return NULL;                                              \
1267      }                                                           \
1268    }                                                             \
1269  } while(0)</span>
1270
1271<span class="hljs-type">static</span> napi_value
1272<span class="hljs-title function_">DoSomethingUseful</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> {
1273  <span class="hljs-comment">// Do something useful.</span>
1274  <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
1275}
1276
1277napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_env env)</span> {
1278  napi_value result;
1279  NAPI_CALL(env, napi_create_object(env, &#x26;result));
1280
1281  napi_value exported_function;
1282  NAPI_CALL(env, napi_create_function(env,
1283                                      <span class="hljs-string">"doSomethingUseful"</span>,
1284                                      NAPI_AUTO_LENGTH,
1285                                      DoSomethingUseful,
1286                                      <span class="hljs-literal">NULL</span>,
1287                                      &#x26;exported_function));
1288
1289  NAPI_CALL(env, napi_set_named_property(env,
1290                                         result,
1291                                         <span class="hljs-string">"doSomethingUseful"</span>,
1292                                         exported_function));
1293
1294  <span class="hljs-keyword">return</span> result;
1295}</code> <button class="copy-button">copy</button></pre>
1296<pre><code class="language-c"><span class="hljs-comment">// addon_node.c</span>
1297<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_api.h></span></span>
1298<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"addon.h"</span></span>
1299
1300NAPI_MODULE_INIT() {
1301  <span class="hljs-comment">// This function body is expected to return a `napi_value`.</span>
1302  <span class="hljs-comment">// The variables `napi_env env` and `napi_value exports` may be used within</span>
1303  <span class="hljs-comment">// the body, as they are provided by the definition of `NAPI_MODULE_INIT()`.</span>
1304  <span class="hljs-keyword">return</span> create_addon(env);
1305}</code> <button class="copy-button">copy</button></pre>
1306</section><section><h3>Environment life cycle APIs<span><a class="mark" href="#environment-life-cycle-apis" id="environment-life-cycle-apis">#</a></span><a aria-hidden="true" class="legacy" id="n_api_environment_life_cycle_apis"></a></h3>
1307<p><a href="https://tc39.es/ecma262/#sec-agents">Section 8.7</a> of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a> defines the concept
1308of an "Agent" as a self-contained environment in which JavaScript code runs.
1309Multiple such Agents may be started and terminated either concurrently or in
1310sequence by the process.</p>
1311<p>A Node.js environment corresponds to an ECMAScript Agent. In the main process,
1312an environment is created at startup, and additional environments can be created
1313on separate threads to serve as <a href="https://nodejs.org/api/worker_threads.html">worker threads</a>. When Node.js is embedded in
1314another application, the main thread of the application may also construct and
1315destroy a Node.js environment multiple times during the life cycle of the
1316application process such that each Node.js environment created by the
1317application may, in turn, during its life cycle create and destroy additional
1318environments as worker threads.</p>
1319<p>From the perspective of a native addon this means that the bindings it provides
1320may be called multiple times, from multiple contexts, and even concurrently from
1321multiple threads.</p>
1322<p>Native addons may need to allocate global state which they use during
1323their life cycle of an Node.js environment such that the state can be
1324unique to each instance of the addon.</p>
1325<p>To this end, Node-API provides a way to associate data such that its life cycle
1326is tied to the life cycle of a Node.js environment.</p>
1327<h4><code>napi_set_instance_data</code><span><a class="mark" href="#napi_set_instance_data" id="napi_set_instance_data">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_set_instance_data"></a></h4>
1328<div class="api_metadata">
1329<span>Added in: v12.8.0, v10.20.0</span>
1330<span>N-API version: 6</span>
1331</div>
1332<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_instance_data</span><span class="hljs-params">(napi_env env,
1333                                   <span class="hljs-type">void</span>* data,
1334                                   napi_finalize finalize_cb,
1335                                   <span class="hljs-type">void</span>* finalize_hint)</span>;</code> <button class="copy-button">copy</button></pre>
1336<ul>
1337<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
1338<li><code>[in] data</code>: The data item to make available to bindings of this instance.</li>
1339<li><code>[in] finalize_cb</code>: The function to call when the environment is being torn
1340down. The function receives <code>data</code> so that it might free it.
1341<a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
1342<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
1343collection.</li>
1344</ul>
1345<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1346<p>This API associates <code>data</code> with the currently running Node.js environment. <code>data</code>
1347can later be retrieved using <code>napi_get_instance_data()</code>. Any existing data
1348associated with the currently running Node.js environment which was set by means
1349of a previous call to <code>napi_set_instance_data()</code> will be overwritten. If a
1350<code>finalize_cb</code> was provided by the previous call, it will not be called.</p>
1351<h4><code>napi_get_instance_data</code><span><a class="mark" href="#napi_get_instance_data" id="napi_get_instance_data">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_instance_data"></a></h4>
1352<div class="api_metadata">
1353<span>Added in: v12.8.0, v10.20.0</span>
1354<span>N-API version: 6</span>
1355</div>
1356<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_instance_data</span><span class="hljs-params">(napi_env env,
1357                                   <span class="hljs-type">void</span>** data)</span>;</code> <button class="copy-button">copy</button></pre>
1358<ul>
1359<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
1360<li><code>[out] data</code>: The data item that was previously associated with the currently
1361running Node.js environment by a call to <code>napi_set_instance_data()</code>.</li>
1362</ul>
1363<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1364<p>This API retrieves data that was previously associated with the currently
1365running Node.js environment via <code>napi_set_instance_data()</code>. If no data is set,
1366the call will succeed and <code>data</code> will be set to <code>NULL</code>.</p>
1367</section><section><h3>Basic Node-API data types<span><a class="mark" href="#basic-node-api-data-types" id="basic-node-api-data-types">#</a></span><a aria-hidden="true" class="legacy" id="n_api_basic_node_api_data_types"></a></h3>
1368<p>Node-API exposes the following fundamental data types as abstractions that are
1369consumed by the various APIs. These APIs should be treated as opaque,
1370introspectable only with other Node-API calls.</p>
1371<h4><code>napi_status</code><span><a class="mark" href="#napi_status" id="napi_status">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_status"></a></h4>
1372<div class="api_metadata">
1373<span>Added in: v8.0.0</span>
1374<span>N-API version: 1</span>
1375</div>
1376<p>Integral status code indicating the success or failure of a Node-API call.
1377Currently, the following status codes are supported.</p>
1378<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
1379  napi_ok,
1380  napi_invalid_arg,
1381  napi_object_expected,
1382  napi_string_expected,
1383  napi_name_expected,
1384  napi_function_expected,
1385  napi_number_expected,
1386  napi_boolean_expected,
1387  napi_array_expected,
1388  napi_generic_failure,
1389  napi_pending_exception,
1390  napi_cancelled,
1391  napi_escape_called_twice,
1392  napi_handle_scope_mismatch,
1393  napi_callback_scope_mismatch,
1394  napi_queue_full,
1395  napi_closing,
1396  napi_bigint_expected,
1397  napi_date_expected,
1398  napi_arraybuffer_expected,
1399  napi_detachable_arraybuffer_expected,
1400  napi_would_deadlock,  <span class="hljs-comment">/* unused */</span>
1401  napi_no_external_buffers_allowed,
1402  napi_cannot_run_js
1403} napi_status;</code> <button class="copy-button">copy</button></pre>
1404<p>If additional information is required upon an API returning a failed status,
1405it can be obtained by calling <code>napi_get_last_error_info</code>.</p>
1406<h4><code>napi_extended_error_info</code><span><a class="mark" href="#napi_extended_error_info" id="napi_extended_error_info">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_extended_error_info"></a></h4>
1407<div class="api_metadata">
1408<span>Added in: v8.0.0</span>
1409<span>N-API version: 1</span>
1410</div>
1411<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span>
1412  <span class="hljs-type">const</span> <span class="hljs-type">char</span>* error_message;
1413  <span class="hljs-type">void</span>* engine_reserved;
1414  <span class="hljs-type">uint32_t</span> engine_error_code;
1415  napi_status error_code;
1416} napi_extended_error_info;</code> <button class="copy-button">copy</button></pre>
1417<ul>
1418<li><code>error_message</code>: UTF8-encoded string containing a VM-neutral description of
1419the error.</li>
1420<li><code>engine_reserved</code>: Reserved for VM-specific error details. This is currently
1421not implemented for any VM.</li>
1422<li><code>engine_error_code</code>: VM-specific error code. This is currently
1423not implemented for any VM.</li>
1424<li><code>error_code</code>: The Node-API status code that originated with the last error.</li>
1425</ul>
1426<p>See the <a href="#error-handling">Error handling</a> section for additional information.</p>
1427<h4><code>napi_env</code><span><a class="mark" href="#napi_env" id="napi_env">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_env"></a></h4>
1428<p><code>napi_env</code> is used to represent a context that the underlying Node-API
1429implementation can use to persist VM-specific state. This structure is passed
1430to native functions when they're invoked, and it must be passed back when
1431making Node-API calls. Specifically, the same <code>napi_env</code> that was passed in when
1432the initial native function was called must be passed to any subsequent
1433nested Node-API calls. Caching the <code>napi_env</code> for the purpose of general reuse,
1434and passing the <code>napi_env</code> between instances of the same addon running on
1435different <a href="worker_threads.html#class-worker"><code>Worker</code></a> threads is not allowed. The <code>napi_env</code> becomes invalid
1436when an instance of a native addon is unloaded. Notification of this event is
1437delivered through the callbacks given to <a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a> and
1438<a href="#napi_set_instance_data"><code>napi_set_instance_data</code></a>.</p>
1439<h4><code>napi_value</code><span><a class="mark" href="#napi_value" id="napi_value">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_value"></a></h4>
1440<p>This is an opaque pointer that is used to represent a JavaScript value.</p>
1441<h4><code>napi_threadsafe_function</code><span><a class="mark" href="#napi_threadsafe_function" id="napi_threadsafe_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_threadsafe_function"></a></h4>
1442<div class="api_metadata">
1443<span>Added in: v10.6.0</span>
1444<span>N-API version: 4</span>
1445</div>
1446<p>This is an opaque pointer that represents a JavaScript function which can be
1447called asynchronously from multiple threads via
1448<code>napi_call_threadsafe_function()</code>.</p>
1449<h4><code>napi_threadsafe_function_release_mode</code><span><a class="mark" href="#napi_threadsafe_function_release_mode" id="napi_threadsafe_function_release_mode">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_threadsafe_function_release_mode"></a></h4>
1450<div class="api_metadata">
1451<span>Added in: v10.6.0</span>
1452<span>N-API version: 4</span>
1453</div>
1454<p>A value to be given to <code>napi_release_threadsafe_function()</code> to indicate whether
1455the thread-safe function is to be closed immediately (<code>napi_tsfn_abort</code>) or
1456merely released (<code>napi_tsfn_release</code>) and thus available for subsequent use via
1457<code>napi_acquire_threadsafe_function()</code> and <code>napi_call_threadsafe_function()</code>.</p>
1458<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
1459  napi_tsfn_release,
1460  napi_tsfn_abort
1461} napi_threadsafe_function_release_mode;</code> <button class="copy-button">copy</button></pre>
1462<h4><code>napi_threadsafe_function_call_mode</code><span><a class="mark" href="#napi_threadsafe_function_call_mode" id="napi_threadsafe_function_call_mode">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_threadsafe_function_call_mode"></a></h4>
1463<div class="api_metadata">
1464<span>Added in: v10.6.0</span>
1465<span>N-API version: 4</span>
1466</div>
1467<p>A value to be given to <code>napi_call_threadsafe_function()</code> to indicate whether
1468the call should block whenever the queue associated with the thread-safe
1469function is full.</p>
1470<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
1471  napi_tsfn_nonblocking,
1472  napi_tsfn_blocking
1473} napi_threadsafe_function_call_mode;</code> <button class="copy-button">copy</button></pre>
1474<h4>Node-API memory management types<span><a class="mark" href="#node-api-memory-management-types" id="node-api-memory-management-types">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_memory_management_types"></a></h4>
1475<h5><code>napi_handle_scope</code><span><a class="mark" href="#napi_handle_scope" id="napi_handle_scope">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_handle_scope"></a></h5>
1476<p>This is an abstraction used to control and modify the lifetime of objects
1477created within a particular scope. In general, Node-API values are created
1478within the context of a handle scope. When a native method is called from
1479JavaScript, a default handle scope will exist. If the user does not explicitly
1480create a new handle scope, Node-API values will be created in the default handle
1481scope. For any invocations of code outside the execution of a native method
1482(for instance, during a libuv callback invocation), the module is required to
1483create a scope before invoking any functions that can result in the creation
1484of JavaScript values.</p>
1485<p>Handle scopes are created using <a href="#napi_open_handle_scope"><code>napi_open_handle_scope</code></a> and are destroyed
1486using <a href="#napi_close_handle_scope"><code>napi_close_handle_scope</code></a>. Closing the scope can indicate to the GC
1487that all <code>napi_value</code>s created during the lifetime of the handle scope are no
1488longer referenced from the current stack frame.</p>
1489<p>For more details, review the <a href="#object-lifetime-management">Object lifetime management</a>.</p>
1490<h5><code>napi_escapable_handle_scope</code><span><a class="mark" href="#napi_escapable_handle_scope" id="napi_escapable_handle_scope">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_escapable_handle_scope"></a></h5>
1491<div class="api_metadata">
1492<span>Added in: v8.0.0</span>
1493<span>N-API version: 1</span>
1494</div>
1495<p>Escapable handle scopes are a special type of handle scope to return values
1496created within a particular handle scope to a parent scope.</p>
1497<h5><code>napi_ref</code><span><a class="mark" href="#napi_ref" id="napi_ref">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_ref"></a></h5>
1498<div class="api_metadata">
1499<span>Added in: v8.0.0</span>
1500<span>N-API version: 1</span>
1501</div>
1502<p>This is the abstraction to use to reference a <code>napi_value</code>. This allows for
1503users to manage the lifetimes of JavaScript values, including defining their
1504minimum lifetimes explicitly.</p>
1505<p>For more details, review the <a href="#object-lifetime-management">Object lifetime management</a>.</p>
1506<h5><code>napi_type_tag</code><span><a class="mark" href="#napi_type_tag" id="napi_type_tag">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_type_tag"></a></h5>
1507<div class="api_metadata">
1508<span>Added in: v14.8.0, v12.19.0</span>
1509<span>N-API version: 8</span>
1510</div>
1511<p>A 128-bit value stored as two unsigned 64-bit integers. It serves as a UUID
1512with which JavaScript objects or <a href="#napi_create_external">externals</a> can be "tagged" in order to
1513ensure that they are of a certain type. This is a stronger check than
1514<a href="#napi_instanceof"><code>napi_instanceof</code></a>, because the latter can report a false positive if the
1515object's prototype has been manipulated. Type-tagging is most useful in
1516conjunction with <a href="#napi_wrap"><code>napi_wrap</code></a> because it ensures that the pointer retrieved
1517from a wrapped object can be safely cast to the native type corresponding to the
1518type tag that had been previously applied to the JavaScript object.</p>
1519<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span>
1520  <span class="hljs-type">uint64_t</span> lower;
1521  <span class="hljs-type">uint64_t</span> upper;
1522} napi_type_tag;</code> <button class="copy-button">copy</button></pre>
1523<h5><code>napi_async_cleanup_hook_handle</code><span><a class="mark" href="#napi_async_cleanup_hook_handle" id="napi_async_cleanup_hook_handle">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_async_cleanup_hook_handle"></a></h5>
1524<div class="api_metadata">
1525<span>Added in: v14.10.0, v12.19.0</span>
1526</div>
1527<p>An opaque value returned by <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>. It must be passed
1528to <a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a> when the chain of asynchronous cleanup
1529events completes.</p>
1530<h4>Node-API callback types<span><a class="mark" href="#node-api-callback-types" id="node-api-callback-types">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_callback_types"></a></h4>
1531<h5><code>napi_callback_info</code><span><a class="mark" href="#napi_callback_info" id="napi_callback_info">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_callback_info"></a></h5>
1532<div class="api_metadata">
1533<span>Added in: v8.0.0</span>
1534<span>N-API version: 1</span>
1535</div>
1536<p>Opaque datatype that is passed to a callback function. It can be used for
1537getting additional information about the context in which the callback was
1538invoked.</p>
1539<h5><code>napi_callback</code><span><a class="mark" href="#napi_callback" id="napi_callback">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_callback"></a></h5>
1540<div class="api_metadata">
1541<span>Added in: v8.0.0</span>
1542<span>N-API version: 1</span>
1543</div>
1544<p>Function pointer type for user-provided native functions which are to be
1545exposed to JavaScript via Node-API. Callback functions should satisfy the
1546following signature:</p>
1547<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">napi_value</span> <span class="hljs-params">(*napi_callback)</span><span class="hljs-params">(napi_env, napi_callback_info)</span>;</code> <button class="copy-button">copy</button></pre>
1548<p>Unless for reasons discussed in <a href="#object-lifetime-management">Object Lifetime Management</a>, creating a
1549handle and/or callback scope inside a <code>napi_callback</code> is not necessary.</p>
1550<h5><code>napi_finalize</code><span><a class="mark" href="#napi_finalize" id="napi_finalize">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_finalize"></a></h5>
1551<div class="api_metadata">
1552<span>Added in: v8.0.0</span>
1553<span>N-API version: 1</span>
1554</div>
1555<p>Function pointer type for add-on provided functions that allow the user to be
1556notified when externally-owned data is ready to be cleaned up because the
1557object with which it was associated with has been garbage-collected. The user
1558must provide a function satisfying the following signature which would get
1559called upon the object's collection. Currently, <code>napi_finalize</code> can be used for
1560finding out when objects that have external data are collected.</p>
1561<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_finalize)</span><span class="hljs-params">(napi_env env,
1562                              <span class="hljs-type">void</span>* finalize_data,
1563                              <span class="hljs-type">void</span>* finalize_hint)</span>;</code> <button class="copy-button">copy</button></pre>
1564<p>Unless for reasons discussed in <a href="#object-lifetime-management">Object Lifetime Management</a>, creating a
1565handle and/or callback scope inside the function body is not necessary.</p>
1566<p>Since these functions may be called while the JavaScript engine is in a state
1567where it cannot execute JavaScript code, some Node-API calls may return
1568<code>napi_pending_exception</code> even when there is no exception pending.</p>
1569<p>In the case of <a href="#node_api_create_external_string_latin1"><code>node_api_create_external_string_latin1</code></a> and
1570<a href="#node_api_create_external_string_utf16"><code>node_api_create_external_string_utf16</code></a> the <code>env</code> parameter may be null,
1571because external strings can be collected during the latter part of environment
1572shutdown.</p>
1573<p>Change History:</p>
1574<ul>
1575<li>
1576<p>experimental (<code>NAPI_EXPERIMENTAL</code> is defined):</p>
1577<p>Node-API calls made from a finalizer will return <code>napi_cannot_run_js</code> when
1578the JavaScript engine is unable to execute JavaScript, and will return
1579<code>napi_exception_pending</code> if there is a pending exception.</p>
1580</li>
1581</ul>
1582<h5><code>napi_async_execute_callback</code><span><a class="mark" href="#napi_async_execute_callback" id="napi_async_execute_callback">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_async_execute_callback"></a></h5>
1583<div class="api_metadata">
1584<span>Added in: v8.0.0</span>
1585<span>N-API version: 1</span>
1586</div>
1587<p>Function pointer used with functions that support asynchronous
1588operations. Callback functions must satisfy the following signature:</p>
1589<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_execute_callback)</span><span class="hljs-params">(napi_env env, <span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button></pre>
1590<p>Implementations of this function must avoid making Node-API calls that execute
1591JavaScript or interact with JavaScript objects. Node-API calls should be in the
1592<code>napi_async_complete_callback</code> instead. Do not use the <code>napi_env</code> parameter as
1593it will likely result in execution of JavaScript.</p>
1594<h5><code>napi_async_complete_callback</code><span><a class="mark" href="#napi_async_complete_callback" id="napi_async_complete_callback">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_async_complete_callback"></a></h5>
1595<div class="api_metadata">
1596<span>Added in: v8.0.0</span>
1597<span>N-API version: 1</span>
1598</div>
1599<p>Function pointer used with functions that support asynchronous
1600operations. Callback functions must satisfy the following signature:</p>
1601<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_complete_callback)</span><span class="hljs-params">(napi_env env,
1602                                             napi_status status,
1603                                             <span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button></pre>
1604<p>Unless for reasons discussed in <a href="#object-lifetime-management">Object Lifetime Management</a>, creating a
1605handle and/or callback scope inside the function body is not necessary.</p>
1606<h5><code>napi_threadsafe_function_call_js</code><span><a class="mark" href="#napi_threadsafe_function_call_js" id="napi_threadsafe_function_call_js">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_threadsafe_function_call_js"></a></h5>
1607<div class="api_metadata">
1608<span>Added in: v10.6.0</span>
1609<span>N-API version: 4</span>
1610</div>
1611<p>Function pointer used with asynchronous thread-safe function calls. The callback
1612will be called on the main thread. Its purpose is to use a data item arriving
1613via the queue from one of the secondary threads to construct the parameters
1614necessary for a call into JavaScript, usually via <code>napi_call_function</code>, and then
1615make the call into JavaScript.</p>
1616<p>The data arriving from the secondary thread via the queue is given in the <code>data</code>
1617parameter and the JavaScript function to call is given in the <code>js_callback</code>
1618parameter.</p>
1619<p>Node-API sets up the environment prior to calling this callback, so it is
1620sufficient to call the JavaScript function via <code>napi_call_function</code> rather than
1621via <code>napi_make_callback</code>.</p>
1622<p>Callback functions must satisfy the following signature:</p>
1623<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_threadsafe_function_call_js)</span><span class="hljs-params">(napi_env env,
1624                                                 napi_value js_callback,
1625                                                 <span class="hljs-type">void</span>* context,
1626                                                 <span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button></pre>
1627<ul>
1628<li><code>[in] env</code>: The environment to use for API calls, or <code>NULL</code> if the thread-safe
1629function is being torn down and <code>data</code> may need to be freed.</li>
1630<li><code>[in] js_callback</code>: The JavaScript function to call, or <code>NULL</code> if the
1631thread-safe function is being torn down and <code>data</code> may need to be freed. It
1632may also be <code>NULL</code> if the thread-safe function was created without
1633<code>js_callback</code>.</li>
1634<li><code>[in] context</code>: The optional data with which the thread-safe function was
1635created.</li>
1636<li><code>[in] data</code>: Data created by the secondary thread. It is the responsibility of
1637the callback to convert this native data to JavaScript values (with Node-API
1638functions) that can be passed as parameters when <code>js_callback</code> is invoked.
1639This pointer is managed entirely by the threads and this callback. Thus this
1640callback should free the data.</li>
1641</ul>
1642<p>Unless for reasons discussed in <a href="#object-lifetime-management">Object Lifetime Management</a>, creating a
1643handle and/or callback scope inside the function body is not necessary.</p>
1644<h5><code>napi_cleanup_hook</code><span><a class="mark" href="#napi_cleanup_hook" id="napi_cleanup_hook">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_cleanup_hook"></a></h5>
1645<div class="api_metadata">
1646<span>Added in: v18.13.0</span>
1647<span>N-API version: 3</span>
1648</div>
1649<p>Function pointer used with <a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a>. It will be called
1650when the environment is being torn down.</p>
1651<p>Callback functions must satisfy the following signature:</p>
1652<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_cleanup_hook)</span><span class="hljs-params">(<span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button></pre>
1653<ul>
1654<li><code>[in] data</code>: The data that was passed to <a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a>.</li>
1655</ul>
1656<h5><code>napi_async_cleanup_hook</code><span><a class="mark" href="#napi_async_cleanup_hook" id="napi_async_cleanup_hook">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_async_cleanup_hook"></a></h5>
1657<div class="api_metadata">
1658<span>Added in: v14.10.0, v12.19.0</span>
1659</div>
1660<p>Function pointer used with <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>. It will be called
1661when the environment is being torn down.</p>
1662<p>Callback functions must satisfy the following signature:</p>
1663<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_cleanup_hook)</span><span class="hljs-params">(napi_async_cleanup_hook_handle handle,
1664                                        <span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button></pre>
1665<ul>
1666<li><code>[in] handle</code>: The handle that must be passed to
1667<a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a> after completion of the asynchronous
1668cleanup.</li>
1669<li><code>[in] data</code>: The data that was passed to <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>.</li>
1670</ul>
1671<p>The body of the function should initiate the asynchronous cleanup actions at the
1672end of which <code>handle</code> must be passed in a call to
1673<a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a>.</p>
1674</section><section><h3>Error handling<span><a class="mark" href="#error-handling" id="error-handling">#</a></span><a aria-hidden="true" class="legacy" id="n_api_error_handling"></a></h3>
1675<p>Node-API uses both return values and JavaScript exceptions for error handling.
1676The following sections explain the approach for each case.</p>
1677<h4>Return values<span><a class="mark" href="#return-values" id="return-values">#</a></span><a aria-hidden="true" class="legacy" id="n_api_return_values"></a></h4>
1678<p>All of the Node-API functions share the same error handling pattern. The
1679return type of all API functions is <code>napi_status</code>.</p>
1680<p>The return value will be <code>napi_ok</code> if the request was successful and
1681no uncaught JavaScript exception was thrown. If an error occurred AND
1682an exception was thrown, the <code>napi_status</code> value for the error
1683will be returned. If an exception was thrown, and no error occurred,
1684<code>napi_pending_exception</code> will be returned.</p>
1685<p>In cases where a return value other than <code>napi_ok</code> or
1686<code>napi_pending_exception</code> is returned, <a href="#napi_is_exception_pending"><code>napi_is_exception_pending</code></a>
1687must be called to check if an exception is pending.
1688See the section on exceptions for more details.</p>
1689<p>The full set of possible <code>napi_status</code> values is defined
1690in <code>napi_api_types.h</code>.</p>
1691<p>The <code>napi_status</code> return value provides a VM-independent representation of
1692the error which occurred. In some cases it is useful to be able to get
1693more detailed information, including a string representing the error as well as
1694VM (engine)-specific information.</p>
1695<p>In order to retrieve this information <a href="#napi_get_last_error_info"><code>napi_get_last_error_info</code></a>
1696is provided which returns a <code>napi_extended_error_info</code> structure.
1697The format of the <code>napi_extended_error_info</code> structure is as follows:</p>
1698<div class="api_metadata">
1699<span>Added in: v8.0.0</span>
1700<span>N-API version: 1</span>
1701</div>
1702<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">napi_extended_error_info</span> {</span>
1703  <span class="hljs-type">const</span> <span class="hljs-type">char</span>* error_message;
1704  <span class="hljs-type">void</span>* engine_reserved;
1705  <span class="hljs-type">uint32_t</span> engine_error_code;
1706  napi_status error_code;
1707};</code> <button class="copy-button">copy</button></pre>
1708<ul>
1709<li><code>error_message</code>: Textual representation of the error that occurred.</li>
1710<li><code>engine_reserved</code>: Opaque handle reserved for engine use only.</li>
1711<li><code>engine_error_code</code>: VM specific error code.</li>
1712<li><code>error_code</code>: Node-API status code for the last error.</li>
1713</ul>
1714<p><a href="#napi_get_last_error_info"><code>napi_get_last_error_info</code></a> returns the information for the last
1715Node-API call that was made.</p>
1716<p>Do not rely on the content or format of any of the extended information as it
1717is not subject to SemVer and may change at any time. It is intended only for
1718logging purposes.</p>
1719<h5><code>napi_get_last_error_info</code><span><a class="mark" href="#napi_get_last_error_info" id="napi_get_last_error_info">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_last_error_info"></a></h5>
1720<div class="api_metadata">
1721<span>Added in: v8.0.0</span>
1722<span>N-API version: 1</span>
1723</div>
1724<pre><code class="language-c">napi_status
1725<span class="hljs-title function_">napi_get_last_error_info</span><span class="hljs-params">(napi_env env,
1726                         <span class="hljs-type">const</span> napi_extended_error_info** result)</span>;</code> <button class="copy-button">copy</button></pre>
1727<ul>
1728<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1729<li><code>[out] result</code>: The <code>napi_extended_error_info</code> structure with more
1730information about the error.</li>
1731</ul>
1732<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1733<p>This API retrieves a <code>napi_extended_error_info</code> structure with information
1734about the last error that occurred.</p>
1735<p>The content of the <code>napi_extended_error_info</code> returned is only valid up until
1736a Node-API function is called on the same <code>env</code>. This includes a call to
1737<code>napi_is_exception_pending</code> so it may often be necessary to make a copy
1738of the information so that it can be used later. The pointer returned
1739in <code>error_message</code> points to a statically-defined string so it is safe to use
1740that pointer if you have copied it out of the <code>error_message</code> field (which will
1741be overwritten) before another Node-API function was called.</p>
1742<p>Do not rely on the content or format of any of the extended information as it
1743is not subject to SemVer and may change at any time. It is intended only for
1744logging purposes.</p>
1745<p>This API can be called even if there is a pending JavaScript exception.</p>
1746<h4>Exceptions<span><a class="mark" href="#exceptions" id="exceptions">#</a></span><a aria-hidden="true" class="legacy" id="n_api_exceptions"></a></h4>
1747<p>Any Node-API function call may result in a pending JavaScript exception. This is
1748the case for any of the API functions, even those that may not cause the
1749execution of JavaScript.</p>
1750<p>If the <code>napi_status</code> returned by a function is <code>napi_ok</code> then no
1751exception is pending and no additional action is required. If the
1752<code>napi_status</code> returned is anything other than <code>napi_ok</code> or
1753<code>napi_pending_exception</code>, in order to try to recover and continue
1754instead of simply returning immediately, <a href="#napi_is_exception_pending"><code>napi_is_exception_pending</code></a>
1755must be called in order to determine if an exception is pending or not.</p>
1756<p>In many cases when a Node-API function is called and an exception is
1757already pending, the function will return immediately with a
1758<code>napi_status</code> of <code>napi_pending_exception</code>. However, this is not the case
1759for all functions. Node-API allows a subset of the functions to be
1760called to allow for some minimal cleanup before returning to JavaScript.
1761In that case, <code>napi_status</code> will reflect the status for the function. It
1762will not reflect previous pending exceptions. To avoid confusion, check
1763the error status after every function call.</p>
1764<p>When an exception is pending one of two approaches can be employed.</p>
1765<p>The first approach is to do any appropriate cleanup and then return so that
1766execution will return to JavaScript. As part of the transition back to
1767JavaScript, the exception will be thrown at the point in the JavaScript
1768code where the native method was invoked. The behavior of most Node-API calls
1769is unspecified while an exception is pending, and many will simply return
1770<code>napi_pending_exception</code>, so do as little as possible and then return to
1771JavaScript where the exception can be handled.</p>
1772<p>The second approach is to try to handle the exception. There will be cases
1773where the native code can catch the exception, take the appropriate action,
1774and then continue. This is only recommended in specific cases
1775where it is known that the exception can be safely handled. In these
1776cases <a href="#napi_get_and_clear_last_exception"><code>napi_get_and_clear_last_exception</code></a> can be used to get and
1777clear the exception. On success, result will contain the handle to
1778the last JavaScript <code>Object</code> thrown. If it is determined, after
1779retrieving the exception, the exception cannot be handled after all
1780it can be re-thrown it with <a href="#napi_throw"><code>napi_throw</code></a> where error is the
1781JavaScript value to be thrown.</p>
1782<p>The following utility functions are also available in case native code
1783needs to throw an exception or determine if a <code>napi_value</code> is an instance
1784of a JavaScript <code>Error</code> object: <a href="#napi_throw_error"><code>napi_throw_error</code></a>,
1785<a href="#napi_throw_type_error"><code>napi_throw_type_error</code></a>, <a href="#napi_throw_range_error"><code>napi_throw_range_error</code></a>, <a href="#node_api_throw_syntax_error"><code>node_api_throw_syntax_error</code></a> and <a href="#napi_is_error"><code>napi_is_error</code></a>.</p>
1786<p>The following utility functions are also available in case native
1787code needs to create an <code>Error</code> object: <a href="#napi_create_error"><code>napi_create_error</code></a>,
1788<a href="#napi_create_type_error"><code>napi_create_type_error</code></a>, <a href="#napi_create_range_error"><code>napi_create_range_error</code></a> and <a href="#node_api_create_syntax_error"><code>node_api_create_syntax_error</code></a>,
1789where result is the <code>napi_value</code> that refers to the newly created
1790JavaScript <code>Error</code> object.</p>
1791<p>The Node.js project is adding error codes to all of the errors
1792generated internally. The goal is for applications to use these
1793error codes for all error checking. The associated error messages
1794will remain, but will only be meant to be used for logging and
1795display with the expectation that the message can change without
1796SemVer applying. In order to support this model with Node-API, both
1797in internal functionality and for module specific functionality
1798(as its good practice), the <code>throw_</code> and <code>create_</code> functions
1799take an optional code parameter which is the string for the code
1800to be added to the error object. If the optional parameter is <code>NULL</code>
1801then no code will be associated with the error. If a code is provided,
1802the name associated with the error is also updated to be:</p>
1803<pre><code class="language-text">originalName [code]</code> <button class="copy-button">copy</button></pre>
1804<p>where <code>originalName</code> is the original name associated with the error
1805and <code>code</code> is the code that was provided. For example, if the code
1806is <code>'ERR_ERROR_1'</code> and a <code>TypeError</code> is being created the name will be:</p>
1807<pre><code class="language-text">TypeError [ERR_ERROR_1]</code> <button class="copy-button">copy</button></pre>
1808<h5><code>napi_throw</code><span><a class="mark" href="#napi_throw" id="napi_throw">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_throw"></a></h5>
1809<div class="api_metadata">
1810<span>Added in: v8.0.0</span>
1811<span>N-API version: 1</span>
1812</div>
1813<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw</span><span class="hljs-params">(napi_env env, napi_value error)</span>;</code> <button class="copy-button">copy</button></pre>
1814<ul>
1815<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1816<li><code>[in] error</code>: The JavaScript value to be thrown.</li>
1817</ul>
1818<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1819<p>This API throws the JavaScript value provided.</p>
1820<h5><code>napi_throw_error</code><span><a class="mark" href="#napi_throw_error" id="napi_throw_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_throw_error"></a></h5>
1821<div class="api_metadata">
1822<span>Added in: v8.0.0</span>
1823<span>N-API version: 1</span>
1824</div>
1825<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_error</span><span class="hljs-params">(napi_env env,
1826                                         <span class="hljs-type">const</span> <span class="hljs-type">char</span>* code,
1827                                         <span class="hljs-type">const</span> <span class="hljs-type">char</span>* msg)</span>;</code> <button class="copy-button">copy</button></pre>
1828<ul>
1829<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1830<li><code>[in] code</code>: Optional error code to be set on the error.</li>
1831<li><code>[in] msg</code>: C string representing the text to be associated with the error.</li>
1832</ul>
1833<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1834<p>This API throws a JavaScript <code>Error</code> with the text provided.</p>
1835<h5><code>napi_throw_type_error</code><span><a class="mark" href="#napi_throw_type_error" id="napi_throw_type_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_throw_type_error"></a></h5>
1836<div class="api_metadata">
1837<span>Added in: v8.0.0</span>
1838<span>N-API version: 1</span>
1839</div>
1840<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_type_error</span><span class="hljs-params">(napi_env env,
1841                                              <span class="hljs-type">const</span> <span class="hljs-type">char</span>* code,
1842                                              <span class="hljs-type">const</span> <span class="hljs-type">char</span>* msg)</span>;</code> <button class="copy-button">copy</button></pre>
1843<ul>
1844<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1845<li><code>[in] code</code>: Optional error code to be set on the error.</li>
1846<li><code>[in] msg</code>: C string representing the text to be associated with the error.</li>
1847</ul>
1848<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1849<p>This API throws a JavaScript <code>TypeError</code> with the text provided.</p>
1850<h5><code>napi_throw_range_error</code><span><a class="mark" href="#napi_throw_range_error" id="napi_throw_range_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_throw_range_error"></a></h5>
1851<div class="api_metadata">
1852<span>Added in: v8.0.0</span>
1853<span>N-API version: 1</span>
1854</div>
1855<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_range_error</span><span class="hljs-params">(napi_env env,
1856                                               <span class="hljs-type">const</span> <span class="hljs-type">char</span>* code,
1857                                               <span class="hljs-type">const</span> <span class="hljs-type">char</span>* msg)</span>;</code> <button class="copy-button">copy</button></pre>
1858<ul>
1859<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1860<li><code>[in] code</code>: Optional error code to be set on the error.</li>
1861<li><code>[in] msg</code>: C string representing the text to be associated with the error.</li>
1862</ul>
1863<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1864<p>This API throws a JavaScript <code>RangeError</code> with the text provided.</p>
1865<h5><code>node_api_throw_syntax_error</code><span><a class="mark" href="#node_api_throw_syntax_error" id="node_api_throw_syntax_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_throw_syntax_error"></a></h5>
1866<div class="api_metadata">
1867<span>Added in: v17.2.0, v16.14.0</span>
1868<span>N-API version: 9</span>
1869</div>
1870<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">node_api_throw_syntax_error</span><span class="hljs-params">(napi_env env,
1871                                                    <span class="hljs-type">const</span> <span class="hljs-type">char</span>* code,
1872                                                    <span class="hljs-type">const</span> <span class="hljs-type">char</span>* msg)</span>;</code> <button class="copy-button">copy</button></pre>
1873<ul>
1874<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1875<li><code>[in] code</code>: Optional error code to be set on the error.</li>
1876<li><code>[in] msg</code>: C string representing the text to be associated with the error.</li>
1877</ul>
1878<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1879<p>This API throws a JavaScript <code>SyntaxError</code> with the text provided.</p>
1880<h5><code>napi_is_error</code><span><a class="mark" href="#napi_is_error" id="napi_is_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_error"></a></h5>
1881<div class="api_metadata">
1882<span>Added in: v8.0.0</span>
1883<span>N-API version: 1</span>
1884</div>
1885<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_is_error</span><span class="hljs-params">(napi_env env,
1886                                      napi_value value,
1887                                      <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
1888<ul>
1889<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1890<li><code>[in] value</code>: The <code>napi_value</code> to be checked.</li>
1891<li><code>[out] result</code>: Boolean value that is set to true if <code>napi_value</code> represents
1892an error, false otherwise.</li>
1893</ul>
1894<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1895<p>This API queries a <code>napi_value</code> to check if it represents an error object.</p>
1896<h5><code>napi_create_error</code><span><a class="mark" href="#napi_create_error" id="napi_create_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_error"></a></h5>
1897<div class="api_metadata">
1898<span>Added in: v8.0.0</span>
1899<span>N-API version: 1</span>
1900</div>
1901<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_error</span><span class="hljs-params">(napi_env env,
1902                                          napi_value code,
1903                                          napi_value msg,
1904                                          napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
1905<ul>
1906<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1907<li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be
1908associated with the error.</li>
1909<li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as
1910the message for the <code>Error</code>.</li>
1911<li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li>
1912</ul>
1913<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1914<p>This API returns a JavaScript <code>Error</code> with the text provided.</p>
1915<h5><code>napi_create_type_error</code><span><a class="mark" href="#napi_create_type_error" id="napi_create_type_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_type_error"></a></h5>
1916<div class="api_metadata">
1917<span>Added in: v8.0.0</span>
1918<span>N-API version: 1</span>
1919</div>
1920<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_type_error</span><span class="hljs-params">(napi_env env,
1921                                               napi_value code,
1922                                               napi_value msg,
1923                                               napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
1924<ul>
1925<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1926<li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be
1927associated with the error.</li>
1928<li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as
1929the message for the <code>Error</code>.</li>
1930<li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li>
1931</ul>
1932<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1933<p>This API returns a JavaScript <code>TypeError</code> with the text provided.</p>
1934<h5><code>napi_create_range_error</code><span><a class="mark" href="#napi_create_range_error" id="napi_create_range_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_range_error"></a></h5>
1935<div class="api_metadata">
1936<span>Added in: v8.0.0</span>
1937<span>N-API version: 1</span>
1938</div>
1939<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_range_error</span><span class="hljs-params">(napi_env env,
1940                                                napi_value code,
1941                                                napi_value msg,
1942                                                napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
1943<ul>
1944<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1945<li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be
1946associated with the error.</li>
1947<li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as
1948the message for the <code>Error</code>.</li>
1949<li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li>
1950</ul>
1951<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1952<p>This API returns a JavaScript <code>RangeError</code> with the text provided.</p>
1953<h5><code>node_api_create_syntax_error</code><span><a class="mark" href="#node_api_create_syntax_error" id="node_api_create_syntax_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_create_syntax_error"></a></h5>
1954<div class="api_metadata">
1955<span>Added in: v17.2.0, v16.14.0</span>
1956<span>N-API version: 9</span>
1957</div>
1958<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">node_api_create_syntax_error</span><span class="hljs-params">(napi_env env,
1959                                                     napi_value code,
1960                                                     napi_value msg,
1961                                                     napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
1962<ul>
1963<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1964<li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be
1965associated with the error.</li>
1966<li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as
1967the message for the <code>Error</code>.</li>
1968<li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li>
1969</ul>
1970<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1971<p>This API returns a JavaScript <code>SyntaxError</code> with the text provided.</p>
1972<h5><code>napi_get_and_clear_last_exception</code><span><a class="mark" href="#napi_get_and_clear_last_exception" id="napi_get_and_clear_last_exception">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_and_clear_last_exception"></a></h5>
1973<div class="api_metadata">
1974<span>Added in: v8.0.0</span>
1975<span>N-API version: 1</span>
1976</div>
1977<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_and_clear_last_exception</span><span class="hljs-params">(napi_env env,
1978                                              napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
1979<ul>
1980<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1981<li><code>[out] result</code>: The exception if one is pending, <code>NULL</code> otherwise.</li>
1982</ul>
1983<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1984<p>This API can be called even if there is a pending JavaScript exception.</p>
1985<h5><code>napi_is_exception_pending</code><span><a class="mark" href="#napi_is_exception_pending" id="napi_is_exception_pending">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_exception_pending"></a></h5>
1986<div class="api_metadata">
1987<span>Added in: v8.0.0</span>
1988<span>N-API version: 1</span>
1989</div>
1990<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_exception_pending</span><span class="hljs-params">(napi_env env, <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
1991<ul>
1992<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1993<li><code>[out] result</code>: Boolean value that is set to true if an exception is pending.</li>
1994</ul>
1995<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1996<p>This API can be called even if there is a pending JavaScript exception.</p>
1997<h5><code>napi_fatal_exception</code><span><a class="mark" href="#napi_fatal_exception" id="napi_fatal_exception">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_fatal_exception"></a></h5>
1998<div class="api_metadata">
1999<span>Added in: v9.10.0</span>
2000<span>N-API version: 3</span>
2001</div>
2002<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_fatal_exception</span><span class="hljs-params">(napi_env env, napi_value err)</span>;</code> <button class="copy-button">copy</button></pre>
2003<ul>
2004<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2005<li><code>[in] err</code>: The error that is passed to <code>'uncaughtException'</code>.</li>
2006</ul>
2007<p>Trigger an <code>'uncaughtException'</code> in JavaScript. Useful if an async
2008callback throws an exception with no way to recover.</p>
2009<h4>Fatal errors<span><a class="mark" href="#fatal-errors" id="fatal-errors">#</a></span><a aria-hidden="true" class="legacy" id="n_api_fatal_errors"></a></h4>
2010<p>In the event of an unrecoverable error in a native addon, a fatal error can be
2011thrown to immediately terminate the process.</p>
2012<h5><code>napi_fatal_error</code><span><a class="mark" href="#napi_fatal_error" id="napi_fatal_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_fatal_error"></a></h5>
2013<div class="api_metadata">
2014<span>Added in: v8.2.0</span>
2015<span>N-API version: 1</span>
2016</div>
2017<pre><code class="language-c">NAPI_NO_RETURN <span class="hljs-type">void</span> <span class="hljs-title function_">napi_fatal_error</span><span class="hljs-params">(<span class="hljs-type">const</span> <span class="hljs-type">char</span>* location,
2018                                     <span class="hljs-type">size_t</span> location_len,
2019                                     <span class="hljs-type">const</span> <span class="hljs-type">char</span>* message,
2020                                     <span class="hljs-type">size_t</span> message_len)</span>;</code> <button class="copy-button">copy</button></pre>
2021<ul>
2022<li><code>[in] location</code>: Optional location at which the error occurred.</li>
2023<li><code>[in] location_len</code>: The length of the location in bytes, or
2024<code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li>
2025<li><code>[in] message</code>: The message associated with the error.</li>
2026<li><code>[in] message_len</code>: The length of the message in bytes, or <code>NAPI_AUTO_LENGTH</code>
2027if it is null-terminated.</li>
2028</ul>
2029<p>The function call does not return, the process will be terminated.</p>
2030<p>This API can be called even if there is a pending JavaScript exception.</p>
2031</section><section><h3>Object lifetime management<span><a class="mark" href="#object-lifetime-management" id="object-lifetime-management">#</a></span><a aria-hidden="true" class="legacy" id="n_api_object_lifetime_management"></a></h3>
2032<p>As Node-API calls are made, handles to objects in the heap for the underlying
2033VM may be returned as <code>napi_values</code>. These handles must hold the
2034objects 'live' until they are no longer required by the native code,
2035otherwise the objects could be collected before the native code was
2036finished using them.</p>
2037<p>As object handles are returned they are associated with a
2038'scope'. The lifespan for the default scope is tied to the lifespan
2039of the native method call. The result is that, by default, handles
2040remain valid and the objects associated with these handles will be
2041held live for the lifespan of the native method call.</p>
2042<p>In many cases, however, it is necessary that the handles remain valid for
2043either a shorter or longer lifespan than that of the native method.
2044The sections which follow describe the Node-API functions that can be used
2045to change the handle lifespan from the default.</p>
2046<h4>Making handle lifespan shorter than that of the native method<span><a class="mark" href="#making-handle-lifespan-shorter-than-that-of-the-native-method" id="making-handle-lifespan-shorter-than-that-of-the-native-method">#</a></span><a aria-hidden="true" class="legacy" id="n_api_making_handle_lifespan_shorter_than_that_of_the_native_method"></a></h4>
2047<p>It is often necessary to make the lifespan of handles shorter than
2048the lifespan of a native method. For example, consider a native method
2049that has a loop which iterates through the elements in a large array:</p>
2050<pre><code class="language-c"><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &#x3C; <span class="hljs-number">1000000</span>; i++) {
2051  napi_value result;
2052  napi_status status = napi_get_element(env, object, i, &#x26;result);
2053  <span class="hljs-keyword">if</span> (status != napi_ok) {
2054    <span class="hljs-keyword">break</span>;
2055  }
2056  <span class="hljs-comment">// do something with element</span>
2057}</code> <button class="copy-button">copy</button></pre>
2058<p>This would result in a large number of handles being created, consuming
2059substantial resources. In addition, even though the native code could only
2060use the most recent handle, all of the associated objects would also be
2061kept alive since they all share the same scope.</p>
2062<p>To handle this case, Node-API provides the ability to establish a new 'scope' to
2063which newly created handles will be associated. Once those handles
2064are no longer required, the scope can be 'closed' and any handles associated
2065with the scope are invalidated. The methods available to open/close scopes are
2066<a href="#napi_open_handle_scope"><code>napi_open_handle_scope</code></a> and <a href="#napi_close_handle_scope"><code>napi_close_handle_scope</code></a>.</p>
2067<p>Node-API only supports a single nested hierarchy of scopes. There is only one
2068active scope at any time, and all new handles will be associated with that
2069scope while it is active. Scopes must be closed in the reverse order from
2070which they are opened. In addition, all scopes created within a native method
2071must be closed before returning from that method.</p>
2072<p>Taking the earlier example, adding calls to <a href="#napi_open_handle_scope"><code>napi_open_handle_scope</code></a> and
2073<a href="#napi_close_handle_scope"><code>napi_close_handle_scope</code></a> would ensure that at most a single handle
2074is valid throughout the execution of the loop:</p>
2075<pre><code class="language-c"><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &#x3C; <span class="hljs-number">1000000</span>; i++) {
2076  napi_handle_scope scope;
2077  napi_status status = napi_open_handle_scope(env, &#x26;scope);
2078  <span class="hljs-keyword">if</span> (status != napi_ok) {
2079    <span class="hljs-keyword">break</span>;
2080  }
2081  napi_value result;
2082  status = napi_get_element(env, object, i, &#x26;result);
2083  <span class="hljs-keyword">if</span> (status != napi_ok) {
2084    <span class="hljs-keyword">break</span>;
2085  }
2086  <span class="hljs-comment">// do something with element</span>
2087  status = napi_close_handle_scope(env, scope);
2088  <span class="hljs-keyword">if</span> (status != napi_ok) {
2089    <span class="hljs-keyword">break</span>;
2090  }
2091}</code> <button class="copy-button">copy</button></pre>
2092<p>When nesting scopes, there are cases where a handle from an
2093inner scope needs to live beyond the lifespan of that scope. Node-API supports
2094an 'escapable scope' in order to support this case. An escapable scope
2095allows one handle to be 'promoted' so that it 'escapes' the
2096current scope and the lifespan of the handle changes from the current
2097scope to that of the outer scope.</p>
2098<p>The methods available to open/close escapable scopes are
2099<a href="#napi_open_escapable_handle_scope"><code>napi_open_escapable_handle_scope</code></a> and
2100<a href="#napi_close_escapable_handle_scope"><code>napi_close_escapable_handle_scope</code></a>.</p>
2101<p>The request to promote a handle is made through <a href="#napi_escape_handle"><code>napi_escape_handle</code></a> which
2102can only be called once.</p>
2103<h5><code>napi_open_handle_scope</code><span><a class="mark" href="#napi_open_handle_scope" id="napi_open_handle_scope">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_open_handle_scope"></a></h5>
2104<div class="api_metadata">
2105<span>Added in: v8.0.0</span>
2106<span>N-API version: 1</span>
2107</div>
2108<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_open_handle_scope</span><span class="hljs-params">(napi_env env,
2109                                               napi_handle_scope* result)</span>;</code> <button class="copy-button">copy</button></pre>
2110<ul>
2111<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2112<li><code>[out] result</code>: <code>napi_value</code> representing the new scope.</li>
2113</ul>
2114<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2115<p>This API opens a new scope.</p>
2116<h5><code>napi_close_handle_scope</code><span><a class="mark" href="#napi_close_handle_scope" id="napi_close_handle_scope">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_close_handle_scope"></a></h5>
2117<div class="api_metadata">
2118<span>Added in: v8.0.0</span>
2119<span>N-API version: 1</span>
2120</div>
2121<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_close_handle_scope</span><span class="hljs-params">(napi_env env,
2122                                                napi_handle_scope scope)</span>;</code> <button class="copy-button">copy</button></pre>
2123<ul>
2124<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2125<li><code>[in] scope</code>: <code>napi_value</code> representing the scope to be closed.</li>
2126</ul>
2127<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2128<p>This API closes the scope passed in. Scopes must be closed in the
2129reverse order from which they were created.</p>
2130<p>This API can be called even if there is a pending JavaScript exception.</p>
2131<h5><code>napi_open_escapable_handle_scope</code><span><a class="mark" href="#napi_open_escapable_handle_scope" id="napi_open_escapable_handle_scope">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_open_escapable_handle_scope"></a></h5>
2132<div class="api_metadata">
2133<span>Added in: v8.0.0</span>
2134<span>N-API version: 1</span>
2135</div>
2136<pre><code class="language-c">NAPI_EXTERN napi_status
2137    <span class="hljs-title function_">napi_open_escapable_handle_scope</span><span class="hljs-params">(napi_env env,
2138                                     napi_handle_scope* result)</span>;</code> <button class="copy-button">copy</button></pre>
2139<ul>
2140<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2141<li><code>[out] result</code>: <code>napi_value</code> representing the new scope.</li>
2142</ul>
2143<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2144<p>This API opens a new scope from which one object can be promoted
2145to the outer scope.</p>
2146<h5><code>napi_close_escapable_handle_scope</code><span><a class="mark" href="#napi_close_escapable_handle_scope" id="napi_close_escapable_handle_scope">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_close_escapable_handle_scope"></a></h5>
2147<div class="api_metadata">
2148<span>Added in: v8.0.0</span>
2149<span>N-API version: 1</span>
2150</div>
2151<pre><code class="language-c">NAPI_EXTERN napi_status
2152    <span class="hljs-title function_">napi_close_escapable_handle_scope</span><span class="hljs-params">(napi_env env,
2153                                      napi_handle_scope scope)</span>;</code> <button class="copy-button">copy</button></pre>
2154<ul>
2155<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2156<li><code>[in] scope</code>: <code>napi_value</code> representing the scope to be closed.</li>
2157</ul>
2158<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2159<p>This API closes the scope passed in. Scopes must be closed in the
2160reverse order from which they were created.</p>
2161<p>This API can be called even if there is a pending JavaScript exception.</p>
2162<h5><code>napi_escape_handle</code><span><a class="mark" href="#napi_escape_handle" id="napi_escape_handle">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_escape_handle"></a></h5>
2163<div class="api_metadata">
2164<span>Added in: v8.0.0</span>
2165<span>N-API version: 1</span>
2166</div>
2167<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_escape_handle</span><span class="hljs-params">(napi_env env,
2168                               napi_escapable_handle_scope scope,
2169                               napi_value escapee,
2170                               napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
2171<ul>
2172<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2173<li><code>[in] scope</code>: <code>napi_value</code> representing the current scope.</li>
2174<li><code>[in] escapee</code>: <code>napi_value</code> representing the JavaScript <code>Object</code> to be
2175escaped.</li>
2176<li><code>[out] result</code>: <code>napi_value</code> representing the handle to the escaped <code>Object</code>
2177in the outer scope.</li>
2178</ul>
2179<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2180<p>This API promotes the handle to the JavaScript object so that it is valid
2181for the lifetime of the outer scope. It can only be called once per scope.
2182If it is called more than once an error will be returned.</p>
2183<p>This API can be called even if there is a pending JavaScript exception.</p>
2184<h4>References to values with a lifespan longer than that of the native method<span><a class="mark" href="#references-to-values-with-a-lifespan-longer-than-that-of-the-native-method" id="references-to-values-with-a-lifespan-longer-than-that-of-the-native-method">#</a></span><a aria-hidden="true" class="legacy" id="n_api_references_to_values_with_a_lifespan_longer_than_that_of_the_native_method"></a></h4>
2185<p>In some cases, an addon will need to be able to create and reference values
2186with a lifespan longer than that of a single native method invocation. For
2187example, to create a constructor and later use that constructor
2188in a request to create instances, it must be possible to reference
2189the constructor object across many different instance creation requests. This
2190would not be possible with a normal handle returned as a <code>napi_value</code> as
2191described in the earlier section. The lifespan of a normal handle is
2192managed by scopes and all scopes must be closed before the end of a native
2193method.</p>
2194<p>Node-API provides methods for creating persistent references to values.
2195Currently Node-API only allows references to be created for a
2196limited set of value types, including object, external, function, and symbol.</p>
2197<p>Each reference has an associated count with a value of 0 or higher,
2198which determines whether the reference will keep the corresponding value alive.
2199References with a count of 0 do not prevent values from being collected.
2200Values of object (object, function, external) and symbol types are becoming
2201'weak' references and can still be accessed while they are not collected.
2202Any count greater than 0 will prevent the values from being collected.</p>
2203<p>Symbol values have different flavors. The true weak reference behavior is
2204only supported by local symbols created with the <code>napi_create_symbol</code> function
2205or the JavaScript <code>Symbol()</code> constructor calls. Globally registered symbols
2206created with the <code>node_api_symbol_for</code> function or JavaScript <code>Symbol.for()</code>
2207function calls remain always strong references because the garbage collector
2208does not collect them. The same is true for well-known symbols such as
2209<code>Symbol.iterator</code>. They are also never collected by the garbage collector.</p>
2210<p>References can be created with an initial reference count. The count can
2211then be modified through <a href="#napi_reference_ref"><code>napi_reference_ref</code></a> and
2212<a href="#napi_reference_unref"><code>napi_reference_unref</code></a>. If an object is collected while the count
2213for a reference is 0, all subsequent calls to
2214get the object associated with the reference <a href="#napi_get_reference_value"><code>napi_get_reference_value</code></a>
2215will return <code>NULL</code> for the returned <code>napi_value</code>. An attempt to call
2216<a href="#napi_reference_ref"><code>napi_reference_ref</code></a> for a reference whose object has been collected
2217results in an error.</p>
2218<p>References must be deleted once they are no longer required by the addon. When
2219a reference is deleted, it will no longer prevent the corresponding object from
2220being collected. Failure to delete a persistent reference results in
2221a 'memory leak' with both the native memory for the persistent reference and
2222the corresponding object on the heap being retained forever.</p>
2223<p>There can be multiple persistent references created which refer to the same
2224object, each of which will either keep the object live or not based on its
2225individual count. Multiple persistent references to the same object
2226can result in unexpectedly keeping alive native memory. The native structures
2227for a persistent reference must be kept alive until finalizers for the
2228referenced object are executed. If a new persistent reference is created
2229for the same object, the finalizers for that object will not be
2230run and the native memory pointed by the earlier persistent reference
2231will not be freed. This can be avoided by calling
2232<code>napi_delete_reference</code> in addition to <code>napi_reference_unref</code> when possible.</p>
2233<p><strong>Change History:</strong></p>
2234<ul>
2235<li>
2236<p>Experimental (<code>NAPI_EXPERIMENTAL</code> is defined):</p>
2237<p>References can be created for all value types. The new supported value
2238types do not support weak reference semantic and the values of these types
2239are released when the reference count becomes 0 and cannot be accessed from
2240the reference anymore.</p>
2241</li>
2242</ul>
2243<h5><code>napi_create_reference</code><span><a class="mark" href="#napi_create_reference" id="napi_create_reference">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_reference"></a></h5>
2244<div class="api_metadata">
2245<span>Added in: v8.0.0</span>
2246<span>N-API version: 1</span>
2247</div>
2248<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_reference</span><span class="hljs-params">(napi_env env,
2249                                              napi_value value,
2250                                              <span class="hljs-type">uint32_t</span> initial_refcount,
2251                                              napi_ref* result)</span>;</code> <button class="copy-button">copy</button></pre>
2252<ul>
2253<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2254<li><code>[in] value</code>: The <code>napi_value</code> for which a reference is being created.</li>
2255<li><code>[in] initial_refcount</code>: Initial reference count for the new reference.</li>
2256<li><code>[out] result</code>: <code>napi_ref</code> pointing to the new reference.</li>
2257</ul>
2258<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2259<p>This API creates a new reference with the specified reference count
2260to the value passed in.</p>
2261<h5><code>napi_delete_reference</code><span><a class="mark" href="#napi_delete_reference" id="napi_delete_reference">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_delete_reference"></a></h5>
2262<div class="api_metadata">
2263<span>Added in: v8.0.0</span>
2264<span>N-API version: 1</span>
2265</div>
2266<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_delete_reference</span><span class="hljs-params">(napi_env env, napi_ref ref)</span>;</code> <button class="copy-button">copy</button></pre>
2267<ul>
2268<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2269<li><code>[in] ref</code>: <code>napi_ref</code> to be deleted.</li>
2270</ul>
2271<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2272<p>This API deletes the reference passed in.</p>
2273<p>This API can be called even if there is a pending JavaScript exception.</p>
2274<h5><code>napi_reference_ref</code><span><a class="mark" href="#napi_reference_ref" id="napi_reference_ref">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_reference_ref"></a></h5>
2275<div class="api_metadata">
2276<span>Added in: v8.0.0</span>
2277<span>N-API version: 1</span>
2278</div>
2279<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_reference_ref</span><span class="hljs-params">(napi_env env,
2280                                           napi_ref ref,
2281                                           <span class="hljs-type">uint32_t</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
2282<ul>
2283<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2284<li><code>[in] ref</code>: <code>napi_ref</code> for which the reference count will be incremented.</li>
2285<li><code>[out] result</code>: The new reference count.</li>
2286</ul>
2287<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2288<p>This API increments the reference count for the reference
2289passed in and returns the resulting reference count.</p>
2290<h5><code>napi_reference_unref</code><span><a class="mark" href="#napi_reference_unref" id="napi_reference_unref">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_reference_unref"></a></h5>
2291<div class="api_metadata">
2292<span>Added in: v8.0.0</span>
2293<span>N-API version: 1</span>
2294</div>
2295<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_reference_unref</span><span class="hljs-params">(napi_env env,
2296                                             napi_ref ref,
2297                                             <span class="hljs-type">uint32_t</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
2298<ul>
2299<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2300<li><code>[in] ref</code>: <code>napi_ref</code> for which the reference count will be decremented.</li>
2301<li><code>[out] result</code>: The new reference count.</li>
2302</ul>
2303<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2304<p>This API decrements the reference count for the reference
2305passed in and returns the resulting reference count.</p>
2306<h5><code>napi_get_reference_value</code><span><a class="mark" href="#napi_get_reference_value" id="napi_get_reference_value">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_reference_value"></a></h5>
2307<div class="api_metadata">
2308<span>Added in: v8.0.0</span>
2309<span>N-API version: 1</span>
2310</div>
2311<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_reference_value</span><span class="hljs-params">(napi_env env,
2312                                                 napi_ref ref,
2313                                                 napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
2314<ul>
2315<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2316<li><code>[in] ref</code>: The <code>napi_ref</code> for which the corresponding value is
2317being requested.</li>
2318<li><code>[out] result</code>: The <code>napi_value</code> referenced by the <code>napi_ref</code>.</li>
2319</ul>
2320<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2321<p>If still valid, this API returns the <code>napi_value</code> representing the
2322JavaScript value associated with the <code>napi_ref</code>. Otherwise, result
2323will be <code>NULL</code>.</p>
2324<h4>Cleanup on exit of the current Node.js environment<span><a class="mark" href="#cleanup-on-exit-of-the-current-nodejs-environment" id="cleanup-on-exit-of-the-current-nodejs-environment">#</a></span><a aria-hidden="true" class="legacy" id="n_api_cleanup_on_exit_of_the_current_node_js_environment"></a></h4>
2325<p>While a Node.js process typically releases all its resources when exiting,
2326embedders of Node.js, or future Worker support, may require addons to register
2327clean-up hooks that will be run once the current Node.js environment exits.</p>
2328<p>Node-API provides functions for registering and un-registering such callbacks.
2329When those callbacks are run, all resources that are being held by the addon
2330should be freed up.</p>
2331<h5><code>napi_add_env_cleanup_hook</code><span><a class="mark" href="#napi_add_env_cleanup_hook" id="napi_add_env_cleanup_hook">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_add_env_cleanup_hook"></a></h5>
2332<div class="api_metadata">
2333<span>Added in: v10.2.0</span>
2334<span>N-API version: 3</span>
2335</div>
2336<pre><code class="language-c">NODE_EXTERN napi_status <span class="hljs-title function_">napi_add_env_cleanup_hook</span><span class="hljs-params">(napi_env env,
2337                                                  napi_cleanup_hook fun,
2338                                                  <span class="hljs-type">void</span>* arg)</span>;</code> <button class="copy-button">copy</button></pre>
2339<p>Registers <code>fun</code> as a function to be run with the <code>arg</code> parameter once the
2340current Node.js environment exits.</p>
2341<p>A function can safely be specified multiple times with different
2342<code>arg</code> values. In that case, it will be called multiple times as well.
2343Providing the same <code>fun</code> and <code>arg</code> values multiple times is not allowed
2344and will lead the process to abort.</p>
2345<p>The hooks will be called in reverse order, i.e. the most recently added one
2346will be called first.</p>
2347<p>Removing this hook can be done by using <a href="#napi_remove_env_cleanup_hook"><code>napi_remove_env_cleanup_hook</code></a>.
2348Typically, that happens when the resource for which this hook was added
2349is being torn down anyway.</p>
2350<p>For asynchronous cleanup, <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a> is available.</p>
2351<h5><code>napi_remove_env_cleanup_hook</code><span><a class="mark" href="#napi_remove_env_cleanup_hook" id="napi_remove_env_cleanup_hook">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_remove_env_cleanup_hook"></a></h5>
2352<div class="api_metadata">
2353<span>Added in: v10.2.0</span>
2354<span>N-API version: 3</span>
2355</div>
2356<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remove_env_cleanup_hook</span><span class="hljs-params">(napi_env env,
2357                                                     <span class="hljs-type">void</span> (*fun)(<span class="hljs-type">void</span>* arg),
2358                                                     <span class="hljs-type">void</span>* arg)</span>;</code> <button class="copy-button">copy</button></pre>
2359<p>Unregisters <code>fun</code> as a function to be run with the <code>arg</code> parameter once the
2360current Node.js environment exits. Both the argument and the function value
2361need to be exact matches.</p>
2362<p>The function must have originally been registered
2363with <code>napi_add_env_cleanup_hook</code>, otherwise the process will abort.</p>
2364<h5><code>napi_add_async_cleanup_hook</code><span><a class="mark" href="#napi_add_async_cleanup_hook" id="napi_add_async_cleanup_hook">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_add_async_cleanup_hook"></a></h5>
2365<div class="api_metadata">
2366<details class="changelog"><summary>History</summary>
2367<table>
2368<tbody><tr><th>Version</th><th>Changes</th></tr>
2369<tr><td>v14.10.0, v12.19.0</td>
2370<td><p>Changed signature of the <code>hook</code> callback.</p></td></tr>
2371<tr><td>v14.8.0, v12.19.0</td>
2372<td><p><span>Added in: v14.8.0, v12.19.0</span></p></td></tr>
2373</tbody></table>
2374</details>
2375<span>N-API version: 8</span>
2376</div>
2377<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_add_async_cleanup_hook</span><span class="hljs-params">(
2378    napi_env env,
2379    napi_async_cleanup_hook hook,
2380    <span class="hljs-type">void</span>* arg,
2381    napi_async_cleanup_hook_handle* remove_handle)</span>;</code> <button class="copy-button">copy</button></pre>
2382<ul>
2383<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2384<li><code>[in] hook</code>: The function pointer to call at environment teardown.</li>
2385<li><code>[in] arg</code>: The pointer to pass to <code>hook</code> when it gets called.</li>
2386<li><code>[out] remove_handle</code>: Optional handle that refers to the asynchronous cleanup
2387hook.</li>
2388</ul>
2389<p>Registers <code>hook</code>, which is a function of type <a href="#napi_async_cleanup_hook"><code>napi_async_cleanup_hook</code></a>, as
2390a function to be run with the <code>remove_handle</code> and <code>arg</code> parameters once the
2391current Node.js environment exits.</p>
2392<p>Unlike <a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a>, the hook is allowed to be asynchronous.</p>
2393<p>Otherwise, behavior generally matches that of <a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a>.</p>
2394<p>If <code>remove_handle</code> is not <code>NULL</code>, an opaque value will be stored in it
2395that must later be passed to <a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a>,
2396regardless of whether the hook has already been invoked.
2397Typically, that happens when the resource for which this hook was added
2398is being torn down anyway.</p>
2399<h5><code>napi_remove_async_cleanup_hook</code><span><a class="mark" href="#napi_remove_async_cleanup_hook" id="napi_remove_async_cleanup_hook">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_remove_async_cleanup_hook"></a></h5>
2400<div class="api_metadata">
2401<details class="changelog"><summary>History</summary>
2402<table>
2403<tbody><tr><th>Version</th><th>Changes</th></tr>
2404<tr><td>v14.10.0, v12.19.0</td>
2405<td><p>Removed <code>env</code> parameter.</p></td></tr>
2406<tr><td>v14.8.0, v12.19.0</td>
2407<td><p><span>Added in: v14.8.0, v12.19.0</span></p></td></tr>
2408</tbody></table>
2409</details>
2410</div>
2411<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remove_async_cleanup_hook</span><span class="hljs-params">(
2412    napi_async_cleanup_hook_handle remove_handle)</span>;</code> <button class="copy-button">copy</button></pre>
2413<ul>
2414<li><code>[in] remove_handle</code>: The handle to an asynchronous cleanup hook that was
2415created with <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>.</li>
2416</ul>
2417<p>Unregisters the cleanup hook corresponding to <code>remove_handle</code>. This will prevent
2418the hook from being executed, unless it has already started executing.
2419This must be called on any <code>napi_async_cleanup_hook_handle</code> value obtained
2420from <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>.</p>
2421<h4>Finalization on the exit of the Node.js environment<span><a class="mark" href="#finalization-on-the-exit-of-the-nodejs-environment" id="finalization-on-the-exit-of-the-nodejs-environment">#</a></span><a aria-hidden="true" class="legacy" id="n_api_finalization_on_the_exit_of_the_node_js_environment"></a></h4>
2422<p>The Node.js environment may be torn down at an arbitrary time as soon as
2423possible with JavaScript execution disallowed, like on the request of
2424<a href="worker_threads.html#workerterminate"><code>worker.terminate()</code></a>. When the environment is being torn down, the
2425registered <code>napi_finalize</code> callbacks of JavaScript objects, thread-safe
2426functions and environment instance data are invoked immediately and
2427independently.</p>
2428<p>The invocation of <code>napi_finalize</code> callbacks is scheduled after the manually
2429registered cleanup hooks. In order to ensure a proper order of addon
2430finalization during environment shutdown to avoid use-after-free in the
2431<code>napi_finalize</code> callback, addons should register a cleanup hook with
2432<code>napi_add_env_cleanup_hook</code> and <code>napi_add_async_cleanup_hook</code> to manually
2433release the allocated resource in a proper order.</p>
2434</section><section><h3>Module registration<span><a class="mark" href="#module-registration" id="module-registration">#</a></span><a aria-hidden="true" class="legacy" id="n_api_module_registration"></a></h3>
2435<p>Node-API modules are registered in a manner similar to other modules
2436except that instead of using the <code>NODE_MODULE</code> macro the following
2437is used:</p>
2438<pre><code class="language-c">NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)</code> <button class="copy-button">copy</button></pre>
2439<p>The next difference is the signature for the <code>Init</code> method. For a Node-API
2440module it is as follows:</p>
2441<pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span>;</code> <button class="copy-button">copy</button></pre>
2442<p>The return value from <code>Init</code> is treated as the <code>exports</code> object for the module.
2443The <code>Init</code> method is passed an empty object via the <code>exports</code> parameter as a
2444convenience. If <code>Init</code> returns <code>NULL</code>, the parameter passed as <code>exports</code> is
2445exported by the module. Node-API modules cannot modify the <code>module</code> object but
2446can specify anything as the <code>exports</code> property of the module.</p>
2447<p>To add the method <code>hello</code> as a function so that it can be called as a method
2448provided by the addon:</p>
2449<pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> {
2450  napi_status status;
2451  napi_property_descriptor desc = {
2452    <span class="hljs-string">"hello"</span>,
2453    <span class="hljs-literal">NULL</span>,
2454    Method,
2455    <span class="hljs-literal">NULL</span>,
2456    <span class="hljs-literal">NULL</span>,
2457    <span class="hljs-literal">NULL</span>,
2458    napi_writable | napi_enumerable | napi_configurable,
2459    <span class="hljs-literal">NULL</span>
2460  };
2461  status = napi_define_properties(env, exports, <span class="hljs-number">1</span>, &#x26;desc);
2462  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
2463  <span class="hljs-keyword">return</span> exports;
2464}</code> <button class="copy-button">copy</button></pre>
2465<p>To set a function to be returned by the <code>require()</code> for the addon:</p>
2466<pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> {
2467  napi_value method;
2468  napi_status status;
2469  status = napi_create_function(env, <span class="hljs-string">"exports"</span>, NAPI_AUTO_LENGTH, Method, <span class="hljs-literal">NULL</span>, &#x26;method);
2470  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
2471  <span class="hljs-keyword">return</span> method;
2472}</code> <button class="copy-button">copy</button></pre>
2473<p>To define a class so that new instances can be created (often used with
2474<a href="#object-wrap">Object wrap</a>):</p>
2475<pre><code class="language-c"><span class="hljs-comment">// <span class="hljs-doctag">NOTE:</span> partial example, not all referenced code is included</span>
2476napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> {
2477  napi_status status;
2478  napi_property_descriptor properties[] = {
2479    { <span class="hljs-string">"value"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, GetValue, SetValue, <span class="hljs-literal">NULL</span>, napi_writable | napi_configurable, <span class="hljs-literal">NULL</span> },
2480    DECLARE_NAPI_METHOD(<span class="hljs-string">"plusOne"</span>, PlusOne),
2481    DECLARE_NAPI_METHOD(<span class="hljs-string">"multiply"</span>, Multiply),
2482  };
2483
2484  napi_value cons;
2485  status =
2486      napi_define_class(env, <span class="hljs-string">"MyObject"</span>, New, <span class="hljs-literal">NULL</span>, <span class="hljs-number">3</span>, properties, &#x26;cons);
2487  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
2488
2489  status = napi_create_reference(env, cons, <span class="hljs-number">1</span>, &#x26;constructor);
2490  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
2491
2492  status = napi_set_named_property(env, exports, <span class="hljs-string">"MyObject"</span>, cons);
2493  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
2494
2495  <span class="hljs-keyword">return</span> exports;
2496}</code> <button class="copy-button">copy</button></pre>
2497<p>You can also use the <code>NAPI_MODULE_INIT</code> macro, which acts as a shorthand
2498for <code>NAPI_MODULE</code> and defining an <code>Init</code> function:</p>
2499<pre><code class="language-c">NAPI_MODULE_INIT() {
2500  napi_value answer;
2501  napi_status result;
2502
2503  status = napi_create_int64(env, <span class="hljs-number">42</span>, &#x26;answer);
2504  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
2505
2506  status = napi_set_named_property(env, exports, <span class="hljs-string">"answer"</span>, answer);
2507  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
2508
2509  <span class="hljs-keyword">return</span> exports;
2510}</code> <button class="copy-button">copy</button></pre>
2511<p>All Node-API addons are context-aware, meaning they may be loaded multiple
2512times. There are a few design considerations when declaring such a module.
2513The documentation on <a href="addons.html#context-aware-addons">context-aware addons</a> provides more details.</p>
2514<p>The variables <code>env</code> and <code>exports</code> will be available inside the function body
2515following the macro invocation.</p>
2516<p>For more details on setting properties on objects, see the section on
2517<a href="#working-with-javascript-properties">Working with JavaScript properties</a>.</p>
2518<p>For more details on building addon modules in general, refer to the existing
2519API.</p>
2520</section><section><h3>Working with JavaScript values<span><a class="mark" href="#working-with-javascript-values" id="working-with-javascript-values">#</a></span><a aria-hidden="true" class="legacy" id="n_api_working_with_javascript_values"></a></h3>
2521<p>Node-API exposes a set of APIs to create all types of JavaScript values.
2522Some of these types are documented under <a href="https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values">Section 6</a>
2523of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p>
2524<p>Fundamentally, these APIs are used to do one of the following:</p>
2525<ol>
2526<li>Create a new JavaScript object</li>
2527<li>Convert from a primitive C type to a Node-API value</li>
2528<li>Convert from Node-API value to a primitive C type</li>
2529<li>Get global instances including <code>undefined</code> and <code>null</code></li>
2530</ol>
2531<p>Node-API values are represented by the type <code>napi_value</code>.
2532Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>.
2533In some cases, the API does check the type of the <code>napi_value</code> up-front.
2534However, for better performance, it's better for the caller to make sure that
2535the <code>napi_value</code> in question is of the JavaScript type expected by the API.</p>
2536<h4>Enum types<span><a class="mark" href="#enum-types" id="enum-types">#</a></span><a aria-hidden="true" class="legacy" id="n_api_enum_types"></a></h4>
2537<h5><code>napi_key_collection_mode</code><span><a class="mark" href="#napi_key_collection_mode" id="napi_key_collection_mode">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_key_collection_mode"></a></h5>
2538<div class="api_metadata">
2539<span>Added in: v13.7.0, v12.17.0, v10.20.0</span>
2540<span>N-API version: 6</span>
2541</div>
2542<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
2543  napi_key_include_prototypes,
2544  napi_key_own_only
2545} napi_key_collection_mode;</code> <button class="copy-button">copy</button></pre>
2546<p>Describes the <code>Keys/Properties</code> filter enums:</p>
2547<p><code>napi_key_collection_mode</code> limits the range of collected properties.</p>
2548<p><code>napi_key_own_only</code> limits the collected properties to the given
2549object only. <code>napi_key_include_prototypes</code> will include all keys
2550of the objects's prototype chain as well.</p>
2551<h5><code>napi_key_filter</code><span><a class="mark" href="#napi_key_filter" id="napi_key_filter">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_key_filter"></a></h5>
2552<div class="api_metadata">
2553<span>Added in: v13.7.0, v12.17.0, v10.20.0</span>
2554<span>N-API version: 6</span>
2555</div>
2556<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
2557  napi_key_all_properties = <span class="hljs-number">0</span>,
2558  napi_key_writable = <span class="hljs-number">1</span>,
2559  napi_key_enumerable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">1</span>,
2560  napi_key_configurable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">2</span>,
2561  napi_key_skip_strings = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">3</span>,
2562  napi_key_skip_symbols = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">4</span>
2563} napi_key_filter;</code> <button class="copy-button">copy</button></pre>
2564<p>Property filter bits. They can be or'ed to build a composite filter.</p>
2565<h5><code>napi_key_conversion</code><span><a class="mark" href="#napi_key_conversion" id="napi_key_conversion">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_key_conversion"></a></h5>
2566<div class="api_metadata">
2567<span>Added in: v13.7.0, v12.17.0, v10.20.0</span>
2568<span>N-API version: 6</span>
2569</div>
2570<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
2571  napi_key_keep_numbers,
2572  napi_key_numbers_to_strings
2573} napi_key_conversion;</code> <button class="copy-button">copy</button></pre>
2574<p><code>napi_key_numbers_to_strings</code> will convert integer indices to
2575strings. <code>napi_key_keep_numbers</code> will return numbers for integer
2576indices.</p>
2577<h5><code>napi_valuetype</code><span><a class="mark" href="#napi_valuetype" id="napi_valuetype">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_valuetype"></a></h5>
2578<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
2579  <span class="hljs-comment">// ES6 types (corresponds to typeof)</span>
2580  napi_undefined,
2581  napi_null,
2582  napi_boolean,
2583  napi_number,
2584  napi_string,
2585  napi_symbol,
2586  napi_object,
2587  napi_function,
2588  napi_external,
2589  napi_bigint,
2590} napi_valuetype;</code> <button class="copy-button">copy</button></pre>
2591<p>Describes the type of a <code>napi_value</code>. This generally corresponds to the types
2592described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types">Section 6.1</a> of the ECMAScript Language Specification.
2593In addition to types in that section, <code>napi_valuetype</code> can also represent
2594<code>Function</code>s and <code>Object</code>s with external data.</p>
2595<p>A JavaScript value of type <code>napi_external</code> appears in JavaScript as a plain
2596object such that no properties can be set on it, and no prototype.</p>
2597<h5><code>napi_typedarray_type</code><span><a class="mark" href="#napi_typedarray_type" id="napi_typedarray_type">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_typedarray_type"></a></h5>
2598<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
2599  napi_int8_array,
2600  napi_uint8_array,
2601  napi_uint8_clamped_array,
2602  napi_int16_array,
2603  napi_uint16_array,
2604  napi_int32_array,
2605  napi_uint32_array,
2606  napi_float32_array,
2607  napi_float64_array,
2608  napi_bigint64_array,
2609  napi_biguint64_array,
2610} napi_typedarray_type;</code> <button class="copy-button">copy</button></pre>
2611<p>This represents the underlying binary scalar datatype of the <code>TypedArray</code>.
2612Elements of this enum correspond to
2613<a href="https://tc39.github.io/ecma262/#sec-typedarray-objects">Section 22.2</a> of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p>
2614<h4>Object creation functions<span><a class="mark" href="#object-creation-functions" id="object-creation-functions">#</a></span><a aria-hidden="true" class="legacy" id="n_api_object_creation_functions"></a></h4>
2615<h5><code>napi_create_array</code><span><a class="mark" href="#napi_create_array" id="napi_create_array">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_array"></a></h5>
2616<div class="api_metadata">
2617<span>Added in: v8.0.0</span>
2618<span>N-API version: 1</span>
2619</div>
2620<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_array</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2621<ul>
2622<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
2623<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Array</code>.</li>
2624</ul>
2625<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2626<p>This API returns a Node-API value corresponding to a JavaScript <code>Array</code> type.
2627JavaScript arrays are described in
2628<a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Language Specification.</p>
2629<h5><code>napi_create_array_with_length</code><span><a class="mark" href="#napi_create_array_with_length" id="napi_create_array_with_length">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_array_with_length"></a></h5>
2630<div class="api_metadata">
2631<span>Added in: v8.0.0</span>
2632<span>N-API version: 1</span>
2633</div>
2634<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_array_with_length</span><span class="hljs-params">(napi_env env,
2635                                          <span class="hljs-type">size_t</span> length,
2636                                          napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2637<ul>
2638<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2639<li><code>[in] length</code>: The initial length of the <code>Array</code>.</li>
2640<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Array</code>.</li>
2641</ul>
2642<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2643<p>This API returns a Node-API value corresponding to a JavaScript <code>Array</code> type.
2644The <code>Array</code>'s length property is set to the passed-in length parameter.
2645However, the underlying buffer is not guaranteed to be pre-allocated by the VM
2646when the array is created. That behavior is left to the underlying VM
2647implementation. If the buffer must be a contiguous block of memory that can be
2648directly read and/or written via C, consider using
2649<a href="#napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code></a>.</p>
2650<p>JavaScript arrays are described in
2651<a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Language Specification.</p>
2652<h5><code>napi_create_arraybuffer</code><span><a class="mark" href="#napi_create_arraybuffer" id="napi_create_arraybuffer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_arraybuffer"></a></h5>
2653<div class="api_metadata">
2654<span>Added in: v8.0.0</span>
2655<span>N-API version: 1</span>
2656</div>
2657<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_arraybuffer</span><span class="hljs-params">(napi_env env,
2658                                    <span class="hljs-type">size_t</span> byte_length,
2659                                    <span class="hljs-type">void</span>** data,
2660                                    napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2661<ul>
2662<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2663<li><code>[in] length</code>: The length in bytes of the array buffer to create.</li>
2664<li><code>[out] data</code>: Pointer to the underlying byte buffer of the <code>ArrayBuffer</code>.
2665<code>data</code> can optionally be ignored by passing <code>NULL</code>.</li>
2666<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>ArrayBuffer</code>.</li>
2667</ul>
2668<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2669<p>This API returns a Node-API value corresponding to a JavaScript <code>ArrayBuffer</code>.
2670<code>ArrayBuffer</code>s are used to represent fixed-length binary data buffers. They are
2671normally used as a backing-buffer for <code>TypedArray</code> objects.
2672The <code>ArrayBuffer</code> allocated will have an underlying byte buffer whose size is
2673determined by the <code>length</code> parameter that's passed in.
2674The underlying buffer is optionally returned back to the caller in case the
2675caller wants to directly manipulate the buffer. This buffer can only be
2676written to directly from native code. To write to this buffer from JavaScript,
2677a typed array or <code>DataView</code> object would need to be created.</p>
2678<p>JavaScript <code>ArrayBuffer</code> objects are described in
2679<a href="https://tc39.github.io/ecma262/#sec-arraybuffer-objects">Section 24.1</a> of the ECMAScript Language Specification.</p>
2680<h5><code>napi_create_buffer</code><span><a class="mark" href="#napi_create_buffer" id="napi_create_buffer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_buffer"></a></h5>
2681<div class="api_metadata">
2682<span>Added in: v8.0.0</span>
2683<span>N-API version: 1</span>
2684</div>
2685<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer</span><span class="hljs-params">(napi_env env,
2686                               <span class="hljs-type">size_t</span> size,
2687                               <span class="hljs-type">void</span>** data,
2688                               napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2689<ul>
2690<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2691<li><code>[in] size</code>: Size in bytes of the underlying buffer.</li>
2692<li><code>[out] data</code>: Raw pointer to the underlying buffer.
2693<code>data</code> can optionally be ignored by passing <code>NULL</code>.</li>
2694<li><code>[out] result</code>: A <code>napi_value</code> representing a <code>node::Buffer</code>.</li>
2695</ul>
2696<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2697<p>This API allocates a <code>node::Buffer</code> object. While this is still a
2698fully-supported data structure, in most cases using a <code>TypedArray</code> will suffice.</p>
2699<h5><code>napi_create_buffer_copy</code><span><a class="mark" href="#napi_create_buffer_copy" id="napi_create_buffer_copy">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_buffer_copy"></a></h5>
2700<div class="api_metadata">
2701<span>Added in: v8.0.0</span>
2702<span>N-API version: 1</span>
2703</div>
2704<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer_copy</span><span class="hljs-params">(napi_env env,
2705                                    <span class="hljs-type">size_t</span> length,
2706                                    <span class="hljs-type">const</span> <span class="hljs-type">void</span>* data,
2707                                    <span class="hljs-type">void</span>** result_data,
2708                                    napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2709<ul>
2710<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2711<li><code>[in] size</code>: Size in bytes of the input buffer (should be the same as the size
2712of the new buffer).</li>
2713<li><code>[in] data</code>: Raw pointer to the underlying buffer to copy from.</li>
2714<li><code>[out] result_data</code>: Pointer to the new <code>Buffer</code>'s underlying data buffer.
2715<code>result_data</code> can optionally be ignored by passing <code>NULL</code>.</li>
2716<li><code>[out] result</code>: A <code>napi_value</code> representing a <code>node::Buffer</code>.</li>
2717</ul>
2718<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2719<p>This API allocates a <code>node::Buffer</code> object and initializes it with data copied
2720from the passed-in buffer. While this is still a fully-supported data
2721structure, in most cases using a <code>TypedArray</code> will suffice.</p>
2722<h5><code>napi_create_date</code><span><a class="mark" href="#napi_create_date" id="napi_create_date">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_date"></a></h5>
2723<div class="api_metadata">
2724<span>Added in: v11.11.0, v10.17.0</span>
2725<span>N-API version: 5</span>
2726</div>
2727<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_date</span><span class="hljs-params">(napi_env env,
2728                             <span class="hljs-type">double</span> time,
2729                             napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
2730<ul>
2731<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2732<li><code>[in] time</code>: ECMAScript time value in milliseconds since 01 January, 1970 UTC.</li>
2733<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Date</code>.</li>
2734</ul>
2735<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2736<p>This API does not observe leap seconds; they are ignored, as
2737ECMAScript aligns with POSIX time specification.</p>
2738<p>This API allocates a JavaScript <code>Date</code> object.</p>
2739<p>JavaScript <code>Date</code> objects are described in
2740<a href="https://tc39.github.io/ecma262/#sec-date-objects">Section 20.3</a> of the ECMAScript Language Specification.</p>
2741<h5><code>napi_create_external</code><span><a class="mark" href="#napi_create_external" id="napi_create_external">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_external"></a></h5>
2742<div class="api_metadata">
2743<span>Added in: v8.0.0</span>
2744<span>N-API version: 1</span>
2745</div>
2746<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external</span><span class="hljs-params">(napi_env env,
2747                                 <span class="hljs-type">void</span>* data,
2748                                 napi_finalize finalize_cb,
2749                                 <span class="hljs-type">void</span>* finalize_hint,
2750                                 napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2751<ul>
2752<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2753<li><code>[in] data</code>: Raw pointer to the external data.</li>
2754<li><code>[in] finalize_cb</code>: Optional callback to call when the external value is being
2755collected. <a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
2756<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
2757collection.</li>
2758<li><code>[out] result</code>: A <code>napi_value</code> representing an external value.</li>
2759</ul>
2760<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2761<p>This API allocates a JavaScript value with external data attached to it. This
2762is used to pass external data through JavaScript code, so it can be retrieved
2763later by native code using <a href="#napi_get_value_external"><code>napi_get_value_external</code></a>.</p>
2764<p>The API adds a <code>napi_finalize</code> callback which will be called when the JavaScript
2765object just created has been garbage collected.</p>
2766<p>The created value is not an object, and therefore does not support additional
2767properties. It is considered a distinct value type: calling <code>napi_typeof()</code> with
2768an external value yields <code>napi_external</code>.</p>
2769<h5><code>napi_create_external_arraybuffer</code><span><a class="mark" href="#napi_create_external_arraybuffer" id="napi_create_external_arraybuffer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_external_arraybuffer"></a></h5>
2770<div class="api_metadata">
2771<span>Added in: v8.0.0</span>
2772<span>N-API version: 1</span>
2773</div>
2774<pre><code class="language-c">napi_status
2775<span class="hljs-title function_">napi_create_external_arraybuffer</span><span class="hljs-params">(napi_env env,
2776                                 <span class="hljs-type">void</span>* external_data,
2777                                 <span class="hljs-type">size_t</span> byte_length,
2778                                 napi_finalize finalize_cb,
2779                                 <span class="hljs-type">void</span>* finalize_hint,
2780                                 napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2781<ul>
2782<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2783<li><code>[in] external_data</code>: Pointer to the underlying byte buffer of the
2784<code>ArrayBuffer</code>.</li>
2785<li><code>[in] byte_length</code>: The length in bytes of the underlying buffer.</li>
2786<li><code>[in] finalize_cb</code>: Optional callback to call when the <code>ArrayBuffer</code> is being
2787collected. <a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
2788<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
2789collection.</li>
2790<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>ArrayBuffer</code>.</li>
2791</ul>
2792<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2793<p><strong>Some runtimes other than Node.js have dropped support for external buffers</strong>.
2794On runtimes other than Node.js this method may return
2795<code>napi_no_external_buffers_allowed</code> to indicate that external
2796buffers are not supported. One such runtime is Electron as
2797described in this issue
2798<a href="https://github.com/electron/electron/issues/35801">electron/issues/35801</a>.</p>
2799<p>In order to maintain broadest compatibility with all runtimes
2800you may define <code>NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED</code> in your addon before
2801includes for the node-api headers. Doing so will hide the 2 functions
2802that create external buffers. This will ensure a compilation error
2803occurs if you accidentally use one of these methods.</p>
2804<p>This API returns a Node-API value corresponding to a JavaScript <code>ArrayBuffer</code>.
2805The underlying byte buffer of the <code>ArrayBuffer</code> is externally allocated and
2806managed. The caller must ensure that the byte buffer remains valid until the
2807finalize callback is called.</p>
2808<p>The API adds a <code>napi_finalize</code> callback which will be called when the JavaScript
2809object just created has been garbage collected.</p>
2810<p>JavaScript <code>ArrayBuffer</code>s are described in
2811<a href="https://tc39.github.io/ecma262/#sec-arraybuffer-objects">Section 24.1</a> of the ECMAScript Language Specification.</p>
2812<h5><code>napi_create_external_buffer</code><span><a class="mark" href="#napi_create_external_buffer" id="napi_create_external_buffer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_external_buffer"></a></h5>
2813<div class="api_metadata">
2814<span>Added in: v8.0.0</span>
2815<span>N-API version: 1</span>
2816</div>
2817<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external_buffer</span><span class="hljs-params">(napi_env env,
2818                                        <span class="hljs-type">size_t</span> length,
2819                                        <span class="hljs-type">void</span>* data,
2820                                        napi_finalize finalize_cb,
2821                                        <span class="hljs-type">void</span>* finalize_hint,
2822                                        napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2823<ul>
2824<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2825<li><code>[in] length</code>: Size in bytes of the input buffer (should be the same as the
2826size of the new buffer).</li>
2827<li><code>[in] data</code>: Raw pointer to the underlying buffer to expose to JavaScript.</li>
2828<li><code>[in] finalize_cb</code>: Optional callback to call when the <code>ArrayBuffer</code> is being
2829collected. <a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
2830<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
2831collection.</li>
2832<li><code>[out] result</code>: A <code>napi_value</code> representing a <code>node::Buffer</code>.</li>
2833</ul>
2834<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2835<p><strong>Some runtimes other than Node.js have dropped support for external buffers</strong>.
2836On runtimes other than Node.js this method may return
2837<code>napi_no_external_buffers_allowed</code> to indicate that external
2838buffers are not supported. One such runtime is Electron as
2839described in this issue
2840<a href="https://github.com/electron/electron/issues/35801">electron/issues/35801</a>.</p>
2841<p>In order to maintain broadest compatibility with all runtimes
2842you may define <code>NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED</code> in your addon before
2843includes for the node-api headers. Doing so will hide the 2 functions
2844that create external buffers. This will ensure a compilation error
2845occurs if you accidentally use one of these methods.</p>
2846<p>This API allocates a <code>node::Buffer</code> object and initializes it with data
2847backed by the passed in buffer. While this is still a fully-supported data
2848structure, in most cases using a <code>TypedArray</code> will suffice.</p>
2849<p>The API adds a <code>napi_finalize</code> callback which will be called when the JavaScript
2850object just created has been garbage collected.</p>
2851<p>For Node.js >=4 <code>Buffers</code> are <code>Uint8Array</code>s.</p>
2852<h5><code>napi_create_object</code><span><a class="mark" href="#napi_create_object" id="napi_create_object">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_object"></a></h5>
2853<div class="api_metadata">
2854<span>Added in: v8.0.0</span>
2855<span>N-API version: 1</span>
2856</div>
2857<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_object</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2858<ul>
2859<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2860<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Object</code>.</li>
2861</ul>
2862<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2863<p>This API allocates a default JavaScript <code>Object</code>.
2864It is the equivalent of doing <code>new Object()</code> in JavaScript.</p>
2865<p>The JavaScript <code>Object</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-object-type">Section 6.1.7</a> of the
2866ECMAScript Language Specification.</p>
2867<h5><code>napi_create_symbol</code><span><a class="mark" href="#napi_create_symbol" id="napi_create_symbol">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_symbol"></a></h5>
2868<div class="api_metadata">
2869<span>Added in: v8.0.0</span>
2870<span>N-API version: 1</span>
2871</div>
2872<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_symbol</span><span class="hljs-params">(napi_env env,
2873                               napi_value description,
2874                               napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2875<ul>
2876<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2877<li><code>[in] description</code>: Optional <code>napi_value</code> which refers to a JavaScript
2878<code>string</code> to be set as the description for the symbol.</li>
2879<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>symbol</code>.</li>
2880</ul>
2881<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2882<p>This API creates a JavaScript <code>symbol</code> value from a UTF8-encoded C string.</p>
2883<p>The JavaScript <code>symbol</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-symbol-objects">Section 19.4</a>
2884of the ECMAScript Language Specification.</p>
2885<h5><code>node_api_symbol_for</code><span><a class="mark" href="#node_api_symbol_for" id="node_api_symbol_for">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_symbol_for"></a></h5>
2886<div class="api_metadata">
2887<span>Added in: v17.5.0</span>
2888<span>N-API version: 9</span>
2889</div>
2890<pre><code class="language-c">napi_status <span class="hljs-title function_">node_api_symbol_for</span><span class="hljs-params">(napi_env env,
2891                                <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8description,
2892                                <span class="hljs-type">size_t</span> length,
2893                                napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2894<ul>
2895<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2896<li><code>[in] utf8description</code>: UTF-8 C string representing the text to be used as the
2897description for the symbol.</li>
2898<li><code>[in] length</code>: The length of the description string in bytes, or
2899<code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li>
2900<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>symbol</code>.</li>
2901</ul>
2902<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2903<p>This API searches in the global registry for an existing symbol with the given
2904description. If the symbol already exists it will be returned, otherwise a new
2905symbol will be created in the registry.</p>
2906<p>The JavaScript <code>symbol</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-symbol-objects">Section 19.4</a> of the ECMAScript
2907Language Specification.</p>
2908<h5><code>napi_create_typedarray</code><span><a class="mark" href="#napi_create_typedarray" id="napi_create_typedarray">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_typedarray"></a></h5>
2909<div class="api_metadata">
2910<span>Added in: v8.0.0</span>
2911<span>N-API version: 1</span>
2912</div>
2913<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_typedarray</span><span class="hljs-params">(napi_env env,
2914                                   napi_typedarray_type type,
2915                                   <span class="hljs-type">size_t</span> length,
2916                                   napi_value arraybuffer,
2917                                   <span class="hljs-type">size_t</span> byte_offset,
2918                                   napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2919<ul>
2920<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2921<li><code>[in] type</code>: Scalar datatype of the elements within the <code>TypedArray</code>.</li>
2922<li><code>[in] length</code>: Number of elements in the <code>TypedArray</code>.</li>
2923<li><code>[in] arraybuffer</code>: <code>ArrayBuffer</code> underlying the typed array.</li>
2924<li><code>[in] byte_offset</code>: The byte offset within the <code>ArrayBuffer</code> from which to
2925start projecting the <code>TypedArray</code>.</li>
2926<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>TypedArray</code>.</li>
2927</ul>
2928<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2929<p>This API creates a JavaScript <code>TypedArray</code> object over an existing
2930<code>ArrayBuffer</code>. <code>TypedArray</code> objects provide an array-like view over an
2931underlying data buffer where each element has the same underlying binary scalar
2932datatype.</p>
2933<p>It's required that <code>(length * size_of_element) + byte_offset</code> should
2934be &#x3C;= the size in bytes of the array passed in. If not, a <code>RangeError</code> exception
2935is raised.</p>
2936<p>JavaScript <code>TypedArray</code> objects are described in
2937<a href="https://tc39.github.io/ecma262/#sec-typedarray-objects">Section 22.2</a> of the ECMAScript Language Specification.</p>
2938<h5><code>napi_create_dataview</code><span><a class="mark" href="#napi_create_dataview" id="napi_create_dataview">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_dataview"></a></h5>
2939<div class="api_metadata">
2940<span>Added in: v8.3.0</span>
2941<span>N-API version: 1</span>
2942</div>
2943<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_dataview</span><span class="hljs-params">(napi_env env,
2944                                 <span class="hljs-type">size_t</span> byte_length,
2945                                 napi_value arraybuffer,
2946                                 <span class="hljs-type">size_t</span> byte_offset,
2947                                 napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2948<ul>
2949<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2950<li><code>[in] length</code>: Number of elements in the <code>DataView</code>.</li>
2951<li><code>[in] arraybuffer</code>: <code>ArrayBuffer</code> underlying the <code>DataView</code>.</li>
2952<li><code>[in] byte_offset</code>: The byte offset within the <code>ArrayBuffer</code> from which to
2953start projecting the <code>DataView</code>.</li>
2954<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>DataView</code>.</li>
2955</ul>
2956<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2957<p>This API creates a JavaScript <code>DataView</code> object over an existing <code>ArrayBuffer</code>.
2958<code>DataView</code> objects provide an array-like view over an underlying data buffer,
2959but one which allows items of different size and type in the <code>ArrayBuffer</code>.</p>
2960<p>It is required that <code>byte_length + byte_offset</code> is less than or equal to the
2961size in bytes of the array passed in. If not, a <code>RangeError</code> exception is
2962raised.</p>
2963<p>JavaScript <code>DataView</code> objects are described in
2964<a href="https://tc39.github.io/ecma262/#sec-dataview-objects">Section 24.3</a> of the ECMAScript Language Specification.</p>
2965<h4>Functions to convert from C types to Node-API<span><a class="mark" href="#functions-to-convert-from-c-types-to-node-api" id="functions-to-convert-from-c-types-to-node-api">#</a></span><a aria-hidden="true" class="legacy" id="n_api_functions_to_convert_from_c_types_to_node_api"></a></h4>
2966<h5><code>napi_create_int32</code><span><a class="mark" href="#napi_create_int32" id="napi_create_int32">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_int32"></a></h5>
2967<div class="api_metadata">
2968<span>Added in: v8.4.0</span>
2969<span>N-API version: 1</span>
2970</div>
2971<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_int32</span><span class="hljs-params">(napi_env env, <span class="hljs-type">int32_t</span> value, napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2972<ul>
2973<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2974<li><code>[in] value</code>: Integer value to be represented in JavaScript.</li>
2975<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li>
2976</ul>
2977<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2978<p>This API is used to convert from the C <code>int32_t</code> type to the JavaScript
2979<code>number</code> type.</p>
2980<p>The JavaScript <code>number</code> type is described in
2981<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a> of the ECMAScript Language Specification.</p>
2982<h5><code>napi_create_uint32</code><span><a class="mark" href="#napi_create_uint32" id="napi_create_uint32">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_uint32"></a></h5>
2983<div class="api_metadata">
2984<span>Added in: v8.4.0</span>
2985<span>N-API version: 1</span>
2986</div>
2987<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_uint32</span><span class="hljs-params">(napi_env env, <span class="hljs-type">uint32_t</span> value, napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2988<ul>
2989<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2990<li><code>[in] value</code>: Unsigned integer value to be represented in JavaScript.</li>
2991<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li>
2992</ul>
2993<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2994<p>This API is used to convert from the C <code>uint32_t</code> type to the JavaScript
2995<code>number</code> type.</p>
2996<p>The JavaScript <code>number</code> type is described in
2997<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a> of the ECMAScript Language Specification.</p>
2998<h5><code>napi_create_int64</code><span><a class="mark" href="#napi_create_int64" id="napi_create_int64">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_int64"></a></h5>
2999<div class="api_metadata">
3000<span>Added in: v8.4.0</span>
3001<span>N-API version: 1</span>
3002</div>
3003<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_int64</span><span class="hljs-params">(napi_env env, <span class="hljs-type">int64_t</span> value, napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3004<ul>
3005<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3006<li><code>[in] value</code>: Integer value to be represented in JavaScript.</li>
3007<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li>
3008</ul>
3009<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3010<p>This API is used to convert from the C <code>int64_t</code> type to the JavaScript
3011<code>number</code> type.</p>
3012<p>The JavaScript <code>number</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a>
3013of the ECMAScript Language Specification. Note the complete range of <code>int64_t</code>
3014cannot be represented with full precision in JavaScript. Integer values
3015outside the range of <a href="https://tc39.github.io/ecma262/#sec-number.min_safe_integer"><code>Number.MIN_SAFE_INTEGER</code></a> <code>-(2**53 - 1)</code> -
3016<a href="https://tc39.github.io/ecma262/#sec-number.max_safe_integer"><code>Number.MAX_SAFE_INTEGER</code></a> <code>(2**53 - 1)</code> will lose precision.</p>
3017<h5><code>napi_create_double</code><span><a class="mark" href="#napi_create_double" id="napi_create_double">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_double"></a></h5>
3018<div class="api_metadata">
3019<span>Added in: v8.4.0</span>
3020<span>N-API version: 1</span>
3021</div>
3022<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_double</span><span class="hljs-params">(napi_env env, <span class="hljs-type">double</span> value, napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3023<ul>
3024<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3025<li><code>[in] value</code>: Double-precision value to be represented in JavaScript.</li>
3026<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li>
3027</ul>
3028<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3029<p>This API is used to convert from the C <code>double</code> type to the JavaScript
3030<code>number</code> type.</p>
3031<p>The JavaScript <code>number</code> type is described in
3032<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a> of the ECMAScript Language Specification.</p>
3033<h5><code>napi_create_bigint_int64</code><span><a class="mark" href="#napi_create_bigint_int64" id="napi_create_bigint_int64">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_bigint_int64"></a></h5>
3034<div class="api_metadata">
3035<span>Added in: v10.7.0</span>
3036<span>N-API version: 6</span>
3037</div>
3038<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_int64</span><span class="hljs-params">(napi_env env,
3039                                     <span class="hljs-type">int64_t</span> value,
3040                                     napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
3041<ul>
3042<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3043<li><code>[in] value</code>: Integer value to be represented in JavaScript.</li>
3044<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>BigInt</code>.</li>
3045</ul>
3046<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3047<p>This API converts the C <code>int64_t</code> type to the JavaScript <code>BigInt</code> type.</p>
3048<h5><code>napi_create_bigint_uint64</code><span><a class="mark" href="#napi_create_bigint_uint64" id="napi_create_bigint_uint64">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_bigint_uint64"></a></h5>
3049<div class="api_metadata">
3050<span>Added in: v10.7.0</span>
3051<span>N-API version: 6</span>
3052</div>
3053<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_uint64</span><span class="hljs-params">(napi_env env,
3054                                      <span class="hljs-type">uint64_t</span> value,
3055                                      napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
3056<ul>
3057<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3058<li><code>[in] value</code>: Unsigned integer value to be represented in JavaScript.</li>
3059<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>BigInt</code>.</li>
3060</ul>
3061<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3062<p>This API converts the C <code>uint64_t</code> type to the JavaScript <code>BigInt</code> type.</p>
3063<h5><code>napi_create_bigint_words</code><span><a class="mark" href="#napi_create_bigint_words" id="napi_create_bigint_words">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_bigint_words"></a></h5>
3064<div class="api_metadata">
3065<span>Added in: v10.7.0</span>
3066<span>N-API version: 6</span>
3067</div>
3068<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_words</span><span class="hljs-params">(napi_env env,
3069                                     <span class="hljs-type">int</span> sign_bit,
3070                                     <span class="hljs-type">size_t</span> word_count,
3071                                     <span class="hljs-type">const</span> <span class="hljs-type">uint64_t</span>* words,
3072                                     napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
3073<ul>
3074<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3075<li><code>[in] sign_bit</code>: Determines if the resulting <code>BigInt</code> will be positive or
3076negative.</li>
3077<li><code>[in] word_count</code>: The length of the <code>words</code> array.</li>
3078<li><code>[in] words</code>: An array of <code>uint64_t</code> little-endian 64-bit words.</li>
3079<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>BigInt</code>.</li>
3080</ul>
3081<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3082<p>This API converts an array of unsigned 64-bit words into a single <code>BigInt</code>
3083value.</p>
3084<p>The resulting <code>BigInt</code> is calculated as: (–1)<sup><code>sign_bit</code></sup> (<code>words[0]</code>
3085× (2<sup>64</sup>)<sup>0</sup> + <code>words[1]</code> × (2<sup>64</sup>)<sup>1</sup> + …)</p>
3086<h5><code>napi_create_string_latin1</code><span><a class="mark" href="#napi_create_string_latin1" id="napi_create_string_latin1">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_string_latin1"></a></h5>
3087<div class="api_metadata">
3088<span>Added in: v8.0.0</span>
3089<span>N-API version: 1</span>
3090</div>
3091<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_latin1</span><span class="hljs-params">(napi_env env,
3092                                      <span class="hljs-type">const</span> <span class="hljs-type">char</span>* str,
3093                                      <span class="hljs-type">size_t</span> length,
3094                                      napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
3095<ul>
3096<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3097<li><code>[in] str</code>: Character buffer representing an ISO-8859-1-encoded string.</li>
3098<li><code>[in] length</code>: The length of the string in bytes, or <code>NAPI_AUTO_LENGTH</code> if it
3099is null-terminated.</li>
3100<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li>
3101</ul>
3102<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3103<p>This API creates a JavaScript <code>string</code> value from an ISO-8859-1-encoded C
3104string. The native string is copied.</p>
3105<p>The JavaScript <code>string</code> type is described in
3106<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p>
3107<h5><code>node_api_create_external_string_latin1</code><span><a class="mark" href="#node_api_create_external_string_latin1" id="node_api_create_external_string_latin1">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_create_external_string_latin1"></a></h5>
3108<div class="api_metadata">
3109<span>Added in: v18.18.0</span>
3110</div>
3111<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
3112<pre><code class="language-c">napi_status
3113<span class="hljs-title function_">node_api_create_external_string_latin1</span><span class="hljs-params">(napi_env env,
3114                                       <span class="hljs-type">char</span>* str,
3115                                       <span class="hljs-type">size_t</span> length,
3116                                       napi_finalize finalize_callback,
3117                                       <span class="hljs-type">void</span>* finalize_hint,
3118                                       napi_value* result,
3119                                       <span class="hljs-type">bool</span>* copied)</span>;</code> <button class="copy-button">copy</button></pre>
3120<ul>
3121<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3122<li><code>[in] str</code>: Character buffer representing an ISO-8859-1-encoded string.</li>
3123<li><code>[in] length</code>: The length of the string in bytes, or <code>NAPI_AUTO_LENGTH</code> if it
3124is null-terminated.</li>
3125<li><code>[in] finalize_callback</code>: The function to call when the string is being
3126collected. The function will be called with the following parameters:
3127<ul>
3128<li><code>[in] env</code>: The environment in which the add-on is running. This value
3129may be null if the string is being collected as part of the termination
3130of the worker or the main Node.js instance.</li>
3131<li><code>[in] data</code>: This is the value <code>str</code> as a <code>void*</code> pointer.</li>
3132<li><code>[in] finalize_hint</code>: This is the value <code>finalize_hint</code> that was given
3133to the API.
3134<a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.
3135This parameter is optional. Passing a null value means that the add-on
3136doesn't need to be notified when the corresponding JavaScript string is
3137collected.</li>
3138</ul>
3139</li>
3140<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
3141collection.</li>
3142<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li>
3143<li><code>[out] copied</code>: Whether the string was copied. If it was, the finalizer will
3144already have been invoked to destroy <code>str</code>.</li>
3145</ul>
3146<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3147<p>This API creates a JavaScript <code>string</code> value from an ISO-8859-1-encoded C
3148string. The native string may not be copied and must thus exist for the entire
3149life cycle of the JavaScript value.</p>
3150<p>The JavaScript <code>string</code> type is described in
3151<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p>
3152<h5><code>napi_create_string_utf16</code><span><a class="mark" href="#napi_create_string_utf16" id="napi_create_string_utf16">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_string_utf16"></a></h5>
3153<div class="api_metadata">
3154<span>Added in: v8.0.0</span>
3155<span>N-API version: 1</span>
3156</div>
3157<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf16</span><span class="hljs-params">(napi_env env,
3158                                     <span class="hljs-type">const</span> <span class="hljs-type">char16_t</span>* str,
3159                                     <span class="hljs-type">size_t</span> length,
3160                                     napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3161<ul>
3162<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3163<li><code>[in] str</code>: Character buffer representing a UTF16-LE-encoded string.</li>
3164<li><code>[in] length</code>: The length of the string in two-byte code units, or
3165<code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li>
3166<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li>
3167</ul>
3168<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3169<p>This API creates a JavaScript <code>string</code> value from a UTF16-LE-encoded C string.
3170The native string is copied.</p>
3171<p>The JavaScript <code>string</code> type is described in
3172<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p>
3173<h5><code>node_api_create_external_string_utf16</code><span><a class="mark" href="#node_api_create_external_string_utf16" id="node_api_create_external_string_utf16">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_create_external_string_utf16"></a></h5>
3174<div class="api_metadata">
3175<span>Added in: v18.18.0</span>
3176</div>
3177<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
3178<pre><code class="language-c">napi_status
3179<span class="hljs-title function_">node_api_create_external_string_utf16</span><span class="hljs-params">(napi_env env,
3180                                      <span class="hljs-type">char16_t</span>* str,
3181                                      <span class="hljs-type">size_t</span> length,
3182                                      napi_finalize finalize_callback,
3183                                      <span class="hljs-type">void</span>* finalize_hint,
3184                                      napi_value* result,
3185                                      <span class="hljs-type">bool</span>* copied)</span>;</code> <button class="copy-button">copy</button></pre>
3186<ul>
3187<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3188<li><code>[in] str</code>: Character buffer representing a UTF16-LE-encoded string.</li>
3189<li><code>[in] length</code>: The length of the string in two-byte code units, or
3190<code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li>
3191<li><code>[in] finalize_callback</code>: The function to call when the string is being
3192collected. The function will be called with the following parameters:
3193<ul>
3194<li><code>[in] env</code>: The environment in which the add-on is running. This value
3195may be null if the string is being collected as part of the termination
3196of the worker or the main Node.js instance.</li>
3197<li><code>[in] data</code>: This is the value <code>str</code> as a <code>void*</code> pointer.</li>
3198<li><code>[in] finalize_hint</code>: This is the value <code>finalize_hint</code> that was given
3199to the API.
3200<a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.
3201This parameter is optional. Passing a null value means that the add-on
3202doesn't need to be notified when the corresponding JavaScript string is
3203collected.</li>
3204</ul>
3205</li>
3206<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
3207collection.</li>
3208<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li>
3209<li><code>[out] copied</code>: Whether the string was copied. If it was, the finalizer will
3210already have been invoked to destroy <code>str</code>.</li>
3211</ul>
3212<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3213<p>This API creates a JavaScript <code>string</code> value from a UTF16-LE-encoded C string.
3214The native string may not be copied and must thus exist for the entire life
3215cycle of the JavaScript value.</p>
3216<p>The JavaScript <code>string</code> type is described in
3217<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p>
3218<h5><code>napi_create_string_utf8</code><span><a class="mark" href="#napi_create_string_utf8" id="napi_create_string_utf8">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_string_utf8"></a></h5>
3219<div class="api_metadata">
3220<span>Added in: v8.0.0</span>
3221<span>N-API version: 1</span>
3222</div>
3223<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf8</span><span class="hljs-params">(napi_env env,
3224                                    <span class="hljs-type">const</span> <span class="hljs-type">char</span>* str,
3225                                    <span class="hljs-type">size_t</span> length,
3226                                    napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3227<ul>
3228<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3229<li><code>[in] str</code>: Character buffer representing a UTF8-encoded string.</li>
3230<li><code>[in] length</code>: The length of the string in bytes, or <code>NAPI_AUTO_LENGTH</code> if it
3231is null-terminated.</li>
3232<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li>
3233</ul>
3234<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3235<p>This API creates a JavaScript <code>string</code> value from a UTF8-encoded C string.
3236The native string is copied.</p>
3237<p>The JavaScript <code>string</code> type is described in
3238<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p>
3239<h4>Functions to convert from Node-API to C types<span><a class="mark" href="#functions-to-convert-from-node-api-to-c-types" id="functions-to-convert-from-node-api-to-c-types">#</a></span><a aria-hidden="true" class="legacy" id="n_api_functions_to_convert_from_node_api_to_c_types"></a></h4>
3240<h5><code>napi_get_array_length</code><span><a class="mark" href="#napi_get_array_length" id="napi_get_array_length">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_array_length"></a></h5>
3241<div class="api_metadata">
3242<span>Added in: v8.0.0</span>
3243<span>N-API version: 1</span>
3244</div>
3245<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_array_length</span><span class="hljs-params">(napi_env env,
3246                                  napi_value value,
3247                                  <span class="hljs-type">uint32_t</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3248<ul>
3249<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3250<li><code>[in] value</code>: <code>napi_value</code> representing the JavaScript <code>Array</code> whose length is
3251being queried.</li>
3252<li><code>[out] result</code>: <code>uint32</code> representing length of the array.</li>
3253</ul>
3254<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3255<p>This API returns the length of an array.</p>
3256<p><code>Array</code> length is described in <a href="https://tc39.github.io/ecma262/#sec-properties-of-array-instances-length">Section 22.1.4.1</a> of the ECMAScript Language
3257Specification.</p>
3258<h5><code>napi_get_arraybuffer_info</code><span><a class="mark" href="#napi_get_arraybuffer_info" id="napi_get_arraybuffer_info">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_arraybuffer_info"></a></h5>
3259<div class="api_metadata">
3260<span>Added in: v8.0.0</span>
3261<span>N-API version: 1</span>
3262</div>
3263<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_arraybuffer_info</span><span class="hljs-params">(napi_env env,
3264                                      napi_value arraybuffer,
3265                                      <span class="hljs-type">void</span>** data,
3266                                      <span class="hljs-type">size_t</span>* byte_length)</span></code> <button class="copy-button">copy</button></pre>
3267<ul>
3268<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3269<li><code>[in] arraybuffer</code>: <code>napi_value</code> representing the <code>ArrayBuffer</code> being queried.</li>
3270<li><code>[out] data</code>: The underlying data buffer of the <code>ArrayBuffer</code>. If byte_length
3271is <code>0</code>, this may be <code>NULL</code> or any other pointer value.</li>
3272<li><code>[out] byte_length</code>: Length in bytes of the underlying data buffer.</li>
3273</ul>
3274<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3275<p>This API is used to retrieve the underlying data buffer of an <code>ArrayBuffer</code> and
3276its length.</p>
3277<p><em>WARNING</em>: Use caution while using this API. The lifetime of the underlying data
3278buffer is managed by the <code>ArrayBuffer</code> even after it's returned. A
3279possible safe way to use this API is in conjunction with
3280<a href="#napi_create_reference"><code>napi_create_reference</code></a>, which can be used to guarantee control over the
3281lifetime of the <code>ArrayBuffer</code>. It's also safe to use the returned data buffer
3282within the same callback as long as there are no calls to other APIs that might
3283trigger a GC.</p>
3284<h5><code>napi_get_buffer_info</code><span><a class="mark" href="#napi_get_buffer_info" id="napi_get_buffer_info">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_buffer_info"></a></h5>
3285<div class="api_metadata">
3286<span>Added in: v8.0.0</span>
3287<span>N-API version: 1</span>
3288</div>
3289<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_buffer_info</span><span class="hljs-params">(napi_env env,
3290                                 napi_value value,
3291                                 <span class="hljs-type">void</span>** data,
3292                                 <span class="hljs-type">size_t</span>* length)</span></code> <button class="copy-button">copy</button></pre>
3293<ul>
3294<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3295<li><code>[in] value</code>: <code>napi_value</code> representing the <code>node::Buffer</code> being queried.</li>
3296<li><code>[out] data</code>: The underlying data buffer of the <code>node::Buffer</code>.
3297If length is <code>0</code>, this may be <code>NULL</code> or any other pointer value.</li>
3298<li><code>[out] length</code>: Length in bytes of the underlying data buffer.</li>
3299</ul>
3300<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3301<p>This API is used to retrieve the underlying data buffer of a <code>node::Buffer</code>
3302and its length.</p>
3303<p><em>Warning</em>: Use caution while using this API since the underlying data buffer's
3304lifetime is not guaranteed if it's managed by the VM.</p>
3305<h5><code>napi_get_prototype</code><span><a class="mark" href="#napi_get_prototype" id="napi_get_prototype">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_prototype"></a></h5>
3306<div class="api_metadata">
3307<span>Added in: v8.0.0</span>
3308<span>N-API version: 1</span>
3309</div>
3310<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_prototype</span><span class="hljs-params">(napi_env env,
3311                               napi_value object,
3312                               napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3313<ul>
3314<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3315<li><code>[in] object</code>: <code>napi_value</code> representing JavaScript <code>Object</code> whose prototype
3316to return. This returns the equivalent of <code>Object.getPrototypeOf</code> (which is
3317not the same as the function's <code>prototype</code> property).</li>
3318<li><code>[out] result</code>: <code>napi_value</code> representing prototype of the given object.</li>
3319</ul>
3320<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3321<h5><code>napi_get_typedarray_info</code><span><a class="mark" href="#napi_get_typedarray_info" id="napi_get_typedarray_info">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_typedarray_info"></a></h5>
3322<div class="api_metadata">
3323<span>Added in: v8.0.0</span>
3324<span>N-API version: 1</span>
3325</div>
3326<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_typedarray_info</span><span class="hljs-params">(napi_env env,
3327                                     napi_value typedarray,
3328                                     napi_typedarray_type* type,
3329                                     <span class="hljs-type">size_t</span>* length,
3330                                     <span class="hljs-type">void</span>** data,
3331                                     napi_value* arraybuffer,
3332                                     <span class="hljs-type">size_t</span>* byte_offset)</span></code> <button class="copy-button">copy</button></pre>
3333<ul>
3334<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3335<li><code>[in] typedarray</code>: <code>napi_value</code> representing the <code>TypedArray</code> whose
3336properties to query.</li>
3337<li><code>[out] type</code>: Scalar datatype of the elements within the <code>TypedArray</code>.</li>
3338<li><code>[out] length</code>: The number of elements in the <code>TypedArray</code>.</li>
3339<li><code>[out] data</code>: The data buffer underlying the <code>TypedArray</code> adjusted by
3340the <code>byte_offset</code> value so that it points to the first element in the
3341<code>TypedArray</code>. If the length of the array is <code>0</code>, this may be <code>NULL</code> or
3342any other pointer value.</li>
3343<li><code>[out] arraybuffer</code>: The <code>ArrayBuffer</code> underlying the <code>TypedArray</code>.</li>
3344<li><code>[out] byte_offset</code>: The byte offset within the underlying native array
3345at which the first element of the arrays is located. The value for the data
3346parameter has already been adjusted so that data points to the first element
3347in the array. Therefore, the first byte of the native array would be at
3348<code>data - byte_offset</code>.</li>
3349</ul>
3350<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3351<p>This API returns various properties of a typed array.</p>
3352<p>Any of the out parameters may be <code>NULL</code> if that property is unneeded.</p>
3353<p><em>Warning</em>: Use caution while using this API since the underlying data buffer
3354is managed by the VM.</p>
3355<h5><code>napi_get_dataview_info</code><span><a class="mark" href="#napi_get_dataview_info" id="napi_get_dataview_info">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_dataview_info"></a></h5>
3356<div class="api_metadata">
3357<span>Added in: v8.3.0</span>
3358<span>N-API version: 1</span>
3359</div>
3360<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_dataview_info</span><span class="hljs-params">(napi_env env,
3361                                   napi_value dataview,
3362                                   <span class="hljs-type">size_t</span>* byte_length,
3363                                   <span class="hljs-type">void</span>** data,
3364                                   napi_value* arraybuffer,
3365                                   <span class="hljs-type">size_t</span>* byte_offset)</span></code> <button class="copy-button">copy</button></pre>
3366<ul>
3367<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3368<li><code>[in] dataview</code>: <code>napi_value</code> representing the <code>DataView</code> whose
3369properties to query.</li>
3370<li><code>[out] byte_length</code>: Number of bytes in the <code>DataView</code>.</li>
3371<li><code>[out] data</code>: The data buffer underlying the <code>DataView</code>.
3372If byte_length is <code>0</code>, this may be <code>NULL</code> or any other pointer value.</li>
3373<li><code>[out] arraybuffer</code>: <code>ArrayBuffer</code> underlying the <code>DataView</code>.</li>
3374<li><code>[out] byte_offset</code>: The byte offset within the data buffer from which
3375to start projecting the <code>DataView</code>.</li>
3376</ul>
3377<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3378<p>Any of the out parameters may be <code>NULL</code> if that property is unneeded.</p>
3379<p>This API returns various properties of a <code>DataView</code>.</p>
3380<h5><code>napi_get_date_value</code><span><a class="mark" href="#napi_get_date_value" id="napi_get_date_value">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_date_value"></a></h5>
3381<div class="api_metadata">
3382<span>Added in: v11.11.0, v10.17.0</span>
3383<span>N-API version: 5</span>
3384</div>
3385<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_date_value</span><span class="hljs-params">(napi_env env,
3386                                napi_value value,
3387                                <span class="hljs-type">double</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3388<ul>
3389<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3390<li><code>[in] value</code>: <code>napi_value</code> representing a JavaScript <code>Date</code>.</li>
3391<li><code>[out] result</code>: Time value as a <code>double</code> represented as milliseconds since
3392midnight at the beginning of 01 January, 1970 UTC.</li>
3393</ul>
3394<p>This API does not observe leap seconds; they are ignored, as
3395ECMAScript aligns with POSIX time specification.</p>
3396<p>Returns <code>napi_ok</code> if the API succeeded. If a non-date <code>napi_value</code> is passed
3397in it returns <code>napi_date_expected</code>.</p>
3398<p>This API returns the C double primitive of time value for the given JavaScript
3399<code>Date</code>.</p>
3400<h5><code>napi_get_value_bool</code><span><a class="mark" href="#napi_get_value_bool" id="napi_get_value_bool">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_bool"></a></h5>
3401<div class="api_metadata">
3402<span>Added in: v8.0.0</span>
3403<span>N-API version: 1</span>
3404</div>
3405<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bool</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3406<ul>
3407<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3408<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>Boolean</code>.</li>
3409<li><code>[out] result</code>: C boolean primitive equivalent of the given JavaScript
3410<code>Boolean</code>.</li>
3411</ul>
3412<p>Returns <code>napi_ok</code> if the API succeeded. If a non-boolean <code>napi_value</code> is
3413passed in it returns <code>napi_boolean_expected</code>.</p>
3414<p>This API returns the C boolean primitive equivalent of the given JavaScript
3415<code>Boolean</code>.</p>
3416<h5><code>napi_get_value_double</code><span><a class="mark" href="#napi_get_value_double" id="napi_get_value_double">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_double"></a></h5>
3417<div class="api_metadata">
3418<span>Added in: v8.0.0</span>
3419<span>N-API version: 1</span>
3420</div>
3421<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_double</span><span class="hljs-params">(napi_env env,
3422                                  napi_value value,
3423                                  <span class="hljs-type">double</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3424<ul>
3425<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3426<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li>
3427<li><code>[out] result</code>: C double primitive equivalent of the given JavaScript
3428<code>number</code>.</li>
3429</ul>
3430<p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code> is passed
3431in it returns <code>napi_number_expected</code>.</p>
3432<p>This API returns the C double primitive equivalent of the given JavaScript
3433<code>number</code>.</p>
3434<h5><code>napi_get_value_bigint_int64</code><span><a class="mark" href="#napi_get_value_bigint_int64" id="napi_get_value_bigint_int64">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_bigint_int64"></a></h5>
3435<div class="api_metadata">
3436<span>Added in: v10.7.0</span>
3437<span>N-API version: 6</span>
3438</div>
3439<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_int64</span><span class="hljs-params">(napi_env env,
3440                                        napi_value value,
3441                                        <span class="hljs-type">int64_t</span>* result,
3442                                        <span class="hljs-type">bool</span>* lossless)</span>;</code> <button class="copy-button">copy</button></pre>
3443<ul>
3444<li><code>[in] env</code>: The environment that the API is invoked under</li>
3445<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>BigInt</code>.</li>
3446<li><code>[out] result</code>: C <code>int64_t</code> primitive equivalent of the given JavaScript
3447<code>BigInt</code>.</li>
3448<li><code>[out] lossless</code>: Indicates whether the <code>BigInt</code> value was converted
3449losslessly.</li>
3450</ul>
3451<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it
3452returns <code>napi_bigint_expected</code>.</p>
3453<p>This API returns the C <code>int64_t</code> primitive equivalent of the given JavaScript
3454<code>BigInt</code>. If needed it will truncate the value, setting <code>lossless</code> to <code>false</code>.</p>
3455<h5><code>napi_get_value_bigint_uint64</code><span><a class="mark" href="#napi_get_value_bigint_uint64" id="napi_get_value_bigint_uint64">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_bigint_uint64"></a></h5>
3456<div class="api_metadata">
3457<span>Added in: v10.7.0</span>
3458<span>N-API version: 6</span>
3459</div>
3460<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_uint64</span><span class="hljs-params">(napi_env env,
3461                                        napi_value value,
3462                                        <span class="hljs-type">uint64_t</span>* result,
3463                                        <span class="hljs-type">bool</span>* lossless)</span>;</code> <button class="copy-button">copy</button></pre>
3464<ul>
3465<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3466<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>BigInt</code>.</li>
3467<li><code>[out] result</code>: C <code>uint64_t</code> primitive equivalent of the given JavaScript
3468<code>BigInt</code>.</li>
3469<li><code>[out] lossless</code>: Indicates whether the <code>BigInt</code> value was converted
3470losslessly.</li>
3471</ul>
3472<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it
3473returns <code>napi_bigint_expected</code>.</p>
3474<p>This API returns the C <code>uint64_t</code> primitive equivalent of the given JavaScript
3475<code>BigInt</code>. If needed it will truncate the value, setting <code>lossless</code> to <code>false</code>.</p>
3476<h5><code>napi_get_value_bigint_words</code><span><a class="mark" href="#napi_get_value_bigint_words" id="napi_get_value_bigint_words">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_bigint_words"></a></h5>
3477<div class="api_metadata">
3478<span>Added in: v10.7.0</span>
3479<span>N-API version: 6</span>
3480</div>
3481<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_words</span><span class="hljs-params">(napi_env env,
3482                                        napi_value value,
3483                                        <span class="hljs-type">int</span>* sign_bit,
3484                                        <span class="hljs-type">size_t</span>* word_count,
3485                                        <span class="hljs-type">uint64_t</span>* words)</span>;</code> <button class="copy-button">copy</button></pre>
3486<ul>
3487<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3488<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>BigInt</code>.</li>
3489<li><code>[out] sign_bit</code>: Integer representing if the JavaScript <code>BigInt</code> is positive
3490or negative.</li>
3491<li><code>[in/out] word_count</code>: Must be initialized to the length of the <code>words</code>
3492array. Upon return, it will be set to the actual number of words that
3493would be needed to store this <code>BigInt</code>.</li>
3494<li><code>[out] words</code>: Pointer to a pre-allocated 64-bit word array.</li>
3495</ul>
3496<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3497<p>This API converts a single <code>BigInt</code> value into a sign bit, 64-bit little-endian
3498array, and the number of elements in the array. <code>sign_bit</code> and <code>words</code> may be
3499both set to <code>NULL</code>, in order to get only <code>word_count</code>.</p>
3500<h5><code>napi_get_value_external</code><span><a class="mark" href="#napi_get_value_external" id="napi_get_value_external">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_external"></a></h5>
3501<div class="api_metadata">
3502<span>Added in: v8.0.0</span>
3503<span>N-API version: 1</span>
3504</div>
3505<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_external</span><span class="hljs-params">(napi_env env,
3506                                    napi_value value,
3507                                    <span class="hljs-type">void</span>** result)</span></code> <button class="copy-button">copy</button></pre>
3508<ul>
3509<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3510<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript external value.</li>
3511<li><code>[out] result</code>: Pointer to the data wrapped by the JavaScript external value.</li>
3512</ul>
3513<p>Returns <code>napi_ok</code> if the API succeeded. If a non-external <code>napi_value</code> is
3514passed in it returns <code>napi_invalid_arg</code>.</p>
3515<p>This API retrieves the external data pointer that was previously passed to
3516<code>napi_create_external()</code>.</p>
3517<h5><code>napi_get_value_int32</code><span><a class="mark" href="#napi_get_value_int32" id="napi_get_value_int32">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_int32"></a></h5>
3518<div class="api_metadata">
3519<span>Added in: v8.0.0</span>
3520<span>N-API version: 1</span>
3521</div>
3522<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int32</span><span class="hljs-params">(napi_env env,
3523                                 napi_value value,
3524                                 <span class="hljs-type">int32_t</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3525<ul>
3526<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3527<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li>
3528<li><code>[out] result</code>: C <code>int32</code> primitive equivalent of the given JavaScript
3529<code>number</code>.</li>
3530</ul>
3531<p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
3532is passed in <code>napi_number_expected</code>.</p>
3533<p>This API returns the C <code>int32</code> primitive equivalent
3534of the given JavaScript <code>number</code>.</p>
3535<p>If the number exceeds the range of the 32 bit integer, then the result is
3536truncated to the equivalent of the bottom 32 bits. This can result in a large
3537positive number becoming a negative number if the value is > 2<sup>31</sup> - 1.</p>
3538<p>Non-finite number values (<code>NaN</code>, <code>+Infinity</code>, or <code>-Infinity</code>) set the
3539result to zero.</p>
3540<h5><code>napi_get_value_int64</code><span><a class="mark" href="#napi_get_value_int64" id="napi_get_value_int64">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_int64"></a></h5>
3541<div class="api_metadata">
3542<span>Added in: v8.0.0</span>
3543<span>N-API version: 1</span>
3544</div>
3545<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int64</span><span class="hljs-params">(napi_env env,
3546                                 napi_value value,
3547                                 <span class="hljs-type">int64_t</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3548<ul>
3549<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3550<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li>
3551<li><code>[out] result</code>: C <code>int64</code> primitive equivalent of the given JavaScript
3552<code>number</code>.</li>
3553</ul>
3554<p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
3555is passed in it returns <code>napi_number_expected</code>.</p>
3556<p>This API returns the C <code>int64</code> primitive equivalent of the given JavaScript
3557<code>number</code>.</p>
3558<p><code>number</code> values outside the range of <a href="https://tc39.github.io/ecma262/#sec-number.min_safe_integer"><code>Number.MIN_SAFE_INTEGER</code></a>
3559<code>-(2**53 - 1)</code> - <a href="https://tc39.github.io/ecma262/#sec-number.max_safe_integer"><code>Number.MAX_SAFE_INTEGER</code></a> <code>(2**53 - 1)</code> will lose
3560precision.</p>
3561<p>Non-finite number values (<code>NaN</code>, <code>+Infinity</code>, or <code>-Infinity</code>) set the
3562result to zero.</p>
3563<h5><code>napi_get_value_string_latin1</code><span><a class="mark" href="#napi_get_value_string_latin1" id="napi_get_value_string_latin1">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_string_latin1"></a></h5>
3564<div class="api_metadata">
3565<span>Added in: v8.0.0</span>
3566<span>N-API version: 1</span>
3567</div>
3568<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_latin1</span><span class="hljs-params">(napi_env env,
3569                                         napi_value value,
3570                                         <span class="hljs-type">char</span>* buf,
3571                                         <span class="hljs-type">size_t</span> bufsize,
3572                                         <span class="hljs-type">size_t</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3573<ul>
3574<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3575<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript string.</li>
3576<li><code>[in] buf</code>: Buffer to write the ISO-8859-1-encoded string into. If <code>NULL</code> is
3577passed in, the length of the string in bytes and excluding the null terminator
3578is returned in <code>result</code>.</li>
3579<li><code>[in] bufsize</code>: Size of the destination buffer. When this value is
3580insufficient, the returned string is truncated and null-terminated.</li>
3581<li><code>[out] result</code>: Number of bytes copied into the buffer, excluding the null
3582terminator.</li>
3583</ul>
3584<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value</code>
3585is passed in it returns <code>napi_string_expected</code>.</p>
3586<p>This API returns the ISO-8859-1-encoded string corresponding the value passed
3587in.</p>
3588<h5><code>napi_get_value_string_utf8</code><span><a class="mark" href="#napi_get_value_string_utf8" id="napi_get_value_string_utf8">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_string_utf8"></a></h5>
3589<div class="api_metadata">
3590<span>Added in: v8.0.0</span>
3591<span>N-API version: 1</span>
3592</div>
3593<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_utf8</span><span class="hljs-params">(napi_env env,
3594                                       napi_value value,
3595                                       <span class="hljs-type">char</span>* buf,
3596                                       <span class="hljs-type">size_t</span> bufsize,
3597                                       <span class="hljs-type">size_t</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3598<ul>
3599<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3600<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript string.</li>
3601<li><code>[in] buf</code>: Buffer to write the UTF8-encoded string into. If <code>NULL</code> is passed
3602in, the length of the string in bytes and excluding the null terminator is
3603returned in <code>result</code>.</li>
3604<li><code>[in] bufsize</code>: Size of the destination buffer. When this value is
3605insufficient, the returned string is truncated and null-terminated.</li>
3606<li><code>[out] result</code>: Number of bytes copied into the buffer, excluding the null
3607terminator.</li>
3608</ul>
3609<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value</code>
3610is passed in it returns <code>napi_string_expected</code>.</p>
3611<p>This API returns the UTF8-encoded string corresponding the value passed in.</p>
3612<h5><code>napi_get_value_string_utf16</code><span><a class="mark" href="#napi_get_value_string_utf16" id="napi_get_value_string_utf16">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_string_utf16"></a></h5>
3613<div class="api_metadata">
3614<span>Added in: v8.0.0</span>
3615<span>N-API version: 1</span>
3616</div>
3617<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_utf16</span><span class="hljs-params">(napi_env env,
3618                                        napi_value value,
3619                                        <span class="hljs-type">char16_t</span>* buf,
3620                                        <span class="hljs-type">size_t</span> bufsize,
3621                                        <span class="hljs-type">size_t</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3622<ul>
3623<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3624<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript string.</li>
3625<li><code>[in] buf</code>: Buffer to write the UTF16-LE-encoded string into. If <code>NULL</code> is
3626passed in, the length of the string in 2-byte code units and excluding the
3627null terminator is returned.</li>
3628<li><code>[in] bufsize</code>: Size of the destination buffer. When this value is
3629insufficient, the returned string is truncated and null-terminated.</li>
3630<li><code>[out] result</code>: Number of 2-byte code units copied into the buffer, excluding
3631the null terminator.</li>
3632</ul>
3633<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value</code>
3634is passed in it returns <code>napi_string_expected</code>.</p>
3635<p>This API returns the UTF16-encoded string corresponding the value passed in.</p>
3636<h5><code>napi_get_value_uint32</code><span><a class="mark" href="#napi_get_value_uint32" id="napi_get_value_uint32">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_uint32"></a></h5>
3637<div class="api_metadata">
3638<span>Added in: v8.0.0</span>
3639<span>N-API version: 1</span>
3640</div>
3641<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_uint32</span><span class="hljs-params">(napi_env env,
3642                                  napi_value value,
3643                                  <span class="hljs-type">uint32_t</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3644<ul>
3645<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3646<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li>
3647<li><code>[out] result</code>: C primitive equivalent of the given <code>napi_value</code> as a
3648<code>uint32_t</code>.</li>
3649</ul>
3650<p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
3651is passed in it returns <code>napi_number_expected</code>.</p>
3652<p>This API returns the C primitive equivalent of the given <code>napi_value</code> as a
3653<code>uint32_t</code>.</p>
3654<h4>Functions to get global instances<span><a class="mark" href="#functions-to-get-global-instances" id="functions-to-get-global-instances">#</a></span><a aria-hidden="true" class="legacy" id="n_api_functions_to_get_global_instances"></a></h4>
3655<h5><code>napi_get_boolean</code><span><a class="mark" href="#napi_get_boolean" id="napi_get_boolean">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_boolean"></a></h5>
3656<div class="api_metadata">
3657<span>Added in: v8.0.0</span>
3658<span>N-API version: 1</span>
3659</div>
3660<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_boolean</span><span class="hljs-params">(napi_env env, <span class="hljs-type">bool</span> value, napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3661<ul>
3662<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3663<li><code>[in] value</code>: The value of the boolean to retrieve.</li>
3664<li><code>[out] result</code>: <code>napi_value</code> representing JavaScript <code>Boolean</code> singleton to
3665retrieve.</li>
3666</ul>
3667<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3668<p>This API is used to return the JavaScript singleton object that is used to
3669represent the given boolean value.</p>
3670<h5><code>napi_get_global</code><span><a class="mark" href="#napi_get_global" id="napi_get_global">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_global"></a></h5>
3671<div class="api_metadata">
3672<span>Added in: v8.0.0</span>
3673<span>N-API version: 1</span>
3674</div>
3675<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_global</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3676<ul>
3677<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3678<li><code>[out] result</code>: <code>napi_value</code> representing JavaScript <code>global</code> object.</li>
3679</ul>
3680<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3681<p>This API returns the <code>global</code> object.</p>
3682<h5><code>napi_get_null</code><span><a class="mark" href="#napi_get_null" id="napi_get_null">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_null"></a></h5>
3683<div class="api_metadata">
3684<span>Added in: v8.0.0</span>
3685<span>N-API version: 1</span>
3686</div>
3687<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_null</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3688<ul>
3689<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3690<li><code>[out] result</code>: <code>napi_value</code> representing JavaScript <code>null</code> object.</li>
3691</ul>
3692<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3693<p>This API returns the <code>null</code> object.</p>
3694<h5><code>napi_get_undefined</code><span><a class="mark" href="#napi_get_undefined" id="napi_get_undefined">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_undefined"></a></h5>
3695<div class="api_metadata">
3696<span>Added in: v8.0.0</span>
3697<span>N-API version: 1</span>
3698</div>
3699<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_undefined</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3700<ul>
3701<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3702<li><code>[out] result</code>: <code>napi_value</code> representing JavaScript Undefined value.</li>
3703</ul>
3704<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3705<p>This API returns the Undefined object.</p>
3706</section><section><h3>Working with JavaScript values and abstract operations<span><a class="mark" href="#working-with-javascript-values-and-abstract-operations" id="working-with-javascript-values-and-abstract-operations">#</a></span><a aria-hidden="true" class="legacy" id="n_api_working_with_javascript_values_and_abstract_operations"></a></h3>
3707<p>Node-API exposes a set of APIs to perform some abstract operations on JavaScript
3708values. Some of these operations are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Section 7</a>
3709of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p>
3710<p>These APIs support doing one of the following:</p>
3711<ol>
3712<li>Coerce JavaScript values to specific JavaScript types (such as <code>number</code> or
3713<code>string</code>).</li>
3714<li>Check the type of a JavaScript value.</li>
3715<li>Check for equality between two JavaScript values.</li>
3716</ol>
3717<h4><code>napi_coerce_to_bool</code><span><a class="mark" href="#napi_coerce_to_bool" id="napi_coerce_to_bool">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_coerce_to_bool"></a></h4>
3718<div class="api_metadata">
3719<span>Added in: v8.0.0</span>
3720<span>N-API version: 1</span>
3721</div>
3722<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_bool</span><span class="hljs-params">(napi_env env,
3723                                napi_value value,
3724                                napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3725<ul>
3726<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3727<li><code>[in] value</code>: The JavaScript value to coerce.</li>
3728<li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>Boolean</code>.</li>
3729</ul>
3730<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3731<p>This API implements the abstract operation <code>ToBoolean()</code> as defined in
3732<a href="https://tc39.github.io/ecma262/#sec-toboolean">Section 7.1.2</a> of the ECMAScript Language Specification.</p>
3733<h4><code>napi_coerce_to_number</code><span><a class="mark" href="#napi_coerce_to_number" id="napi_coerce_to_number">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_coerce_to_number"></a></h4>
3734<div class="api_metadata">
3735<span>Added in: v8.0.0</span>
3736<span>N-API version: 1</span>
3737</div>
3738<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_number</span><span class="hljs-params">(napi_env env,
3739                                  napi_value value,
3740                                  napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3741<ul>
3742<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3743<li><code>[in] value</code>: The JavaScript value to coerce.</li>
3744<li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>number</code>.</li>
3745</ul>
3746<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3747<p>This API implements the abstract operation <code>ToNumber()</code> as defined in
3748<a href="https://tc39.github.io/ecma262/#sec-tonumber">Section 7.1.3</a> of the ECMAScript Language Specification.
3749This function potentially runs JS code if the passed-in value is an
3750object.</p>
3751<h4><code>napi_coerce_to_object</code><span><a class="mark" href="#napi_coerce_to_object" id="napi_coerce_to_object">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_coerce_to_object"></a></h4>
3752<div class="api_metadata">
3753<span>Added in: v8.0.0</span>
3754<span>N-API version: 1</span>
3755</div>
3756<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_object</span><span class="hljs-params">(napi_env env,
3757                                  napi_value value,
3758                                  napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3759<ul>
3760<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3761<li><code>[in] value</code>: The JavaScript value to coerce.</li>
3762<li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>Object</code>.</li>
3763</ul>
3764<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3765<p>This API implements the abstract operation <code>ToObject()</code> as defined in
3766<a href="https://tc39.github.io/ecma262/#sec-toobject">Section 7.1.13</a> of the ECMAScript Language Specification.</p>
3767<h4><code>napi_coerce_to_string</code><span><a class="mark" href="#napi_coerce_to_string" id="napi_coerce_to_string">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_coerce_to_string"></a></h4>
3768<div class="api_metadata">
3769<span>Added in: v8.0.0</span>
3770<span>N-API version: 1</span>
3771</div>
3772<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_string</span><span class="hljs-params">(napi_env env,
3773                                  napi_value value,
3774                                  napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3775<ul>
3776<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3777<li><code>[in] value</code>: The JavaScript value to coerce.</li>
3778<li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>string</code>.</li>
3779</ul>
3780<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3781<p>This API implements the abstract operation <code>ToString()</code> as defined in
3782<a href="https://tc39.github.io/ecma262/#sec-toobject">Section 7.1.13</a> of the ECMAScript Language Specification.
3783This function potentially runs JS code if the passed-in value is an
3784object.</p>
3785<h4><code>napi_typeof</code><span><a class="mark" href="#napi_typeof" id="napi_typeof">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_typeof"></a></h4>
3786<div class="api_metadata">
3787<span>Added in: v8.0.0</span>
3788<span>N-API version: 1</span>
3789</div>
3790<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_typeof</span><span class="hljs-params">(napi_env env, napi_value value, napi_valuetype* result)</span></code> <button class="copy-button">copy</button></pre>
3791<ul>
3792<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3793<li><code>[in] value</code>: The JavaScript value whose type to query.</li>
3794<li><code>[out] result</code>: The type of the JavaScript value.</li>
3795</ul>
3796<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3797<ul>
3798<li><code>napi_invalid_arg</code> if the type of <code>value</code> is not a known ECMAScript type and
3799<code>value</code> is not an External value.</li>
3800</ul>
3801<p>This API represents behavior similar to invoking the <code>typeof</code> Operator on
3802the object as defined in <a href="https://tc39.github.io/ecma262/#sec-typeof-operator">Section 12.5.5</a> of the ECMAScript Language
3803Specification. However, there are some differences:</p>
3804<ol>
3805<li>It has support for detecting an External value.</li>
3806<li>It detects <code>null</code> as a separate type, while ECMAScript <code>typeof</code> would detect
3807<code>object</code>.</li>
3808</ol>
3809<p>If <code>value</code> has a type that is invalid, an error is returned.</p>
3810<h4><code>napi_instanceof</code><span><a class="mark" href="#napi_instanceof" id="napi_instanceof">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_instanceof"></a></h4>
3811<div class="api_metadata">
3812<span>Added in: v8.0.0</span>
3813<span>N-API version: 1</span>
3814</div>
3815<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_instanceof</span><span class="hljs-params">(napi_env env,
3816                            napi_value object,
3817                            napi_value constructor,
3818                            <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3819<ul>
3820<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3821<li><code>[in] object</code>: The JavaScript value to check.</li>
3822<li><code>[in] constructor</code>: The JavaScript function object of the constructor function
3823to check against.</li>
3824<li><code>[out] result</code>: Boolean that is set to true if <code>object instanceof constructor</code>
3825is true.</li>
3826</ul>
3827<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3828<p>This API represents invoking the <code>instanceof</code> Operator on the object as
3829defined in <a href="https://tc39.github.io/ecma262/#sec-instanceofoperator">Section 12.10.4</a> of the ECMAScript Language Specification.</p>
3830<h4><code>napi_is_array</code><span><a class="mark" href="#napi_is_array" id="napi_is_array">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_array"></a></h4>
3831<div class="api_metadata">
3832<span>Added in: v8.0.0</span>
3833<span>N-API version: 1</span>
3834</div>
3835<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_array</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3836<ul>
3837<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3838<li><code>[in] value</code>: The JavaScript value to check.</li>
3839<li><code>[out] result</code>: Whether the given object is an array.</li>
3840</ul>
3841<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3842<p>This API represents invoking the <code>IsArray</code> operation on the object
3843as defined in <a href="https://tc39.github.io/ecma262/#sec-isarray">Section 7.2.2</a> of the ECMAScript Language Specification.</p>
3844<h4><code>napi_is_arraybuffer</code><span><a class="mark" href="#napi_is_arraybuffer" id="napi_is_arraybuffer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_arraybuffer"></a></h4>
3845<div class="api_metadata">
3846<span>Added in: v8.0.0</span>
3847<span>N-API version: 1</span>
3848</div>
3849<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_arraybuffer</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3850<ul>
3851<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3852<li><code>[in] value</code>: The JavaScript value to check.</li>
3853<li><code>[out] result</code>: Whether the given object is an <code>ArrayBuffer</code>.</li>
3854</ul>
3855<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3856<p>This API checks if the <code>Object</code> passed in is an array buffer.</p>
3857<h4><code>napi_is_buffer</code><span><a class="mark" href="#napi_is_buffer" id="napi_is_buffer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_buffer"></a></h4>
3858<div class="api_metadata">
3859<span>Added in: v8.0.0</span>
3860<span>N-API version: 1</span>
3861</div>
3862<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_buffer</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3863<ul>
3864<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3865<li><code>[in] value</code>: The JavaScript value to check.</li>
3866<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a <code>node::Buffer</code>
3867object.</li>
3868</ul>
3869<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3870<p>This API checks if the <code>Object</code> passed in is a buffer.</p>
3871<h4><code>napi_is_date</code><span><a class="mark" href="#napi_is_date" id="napi_is_date">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_date"></a></h4>
3872<div class="api_metadata">
3873<span>Added in: v11.11.0, v10.17.0</span>
3874<span>N-API version: 5</span>
3875</div>
3876<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_date</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3877<ul>
3878<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3879<li><code>[in] value</code>: The JavaScript value to check.</li>
3880<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a JavaScript <code>Date</code>
3881object.</li>
3882</ul>
3883<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3884<p>This API checks if the <code>Object</code> passed in is a date.</p>
3885<h4><code>napi_is_error</code><span><a class="mark" href="#napi_is_error_1" id="napi_is_error_1">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_error_1"></a></h4>
3886<div class="api_metadata">
3887<span>Added in: v8.0.0</span>
3888<span>N-API version: 1</span>
3889</div>
3890<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_error</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3891<ul>
3892<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3893<li><code>[in] value</code>: The JavaScript value to check.</li>
3894<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents an <code>Error</code> object.</li>
3895</ul>
3896<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3897<p>This API checks if the <code>Object</code> passed in is an <code>Error</code>.</p>
3898<h4><code>napi_is_typedarray</code><span><a class="mark" href="#napi_is_typedarray" id="napi_is_typedarray">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_typedarray"></a></h4>
3899<div class="api_metadata">
3900<span>Added in: v8.0.0</span>
3901<span>N-API version: 1</span>
3902</div>
3903<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_typedarray</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3904<ul>
3905<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3906<li><code>[in] value</code>: The JavaScript value to check.</li>
3907<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a <code>TypedArray</code>.</li>
3908</ul>
3909<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3910<p>This API checks if the <code>Object</code> passed in is a typed array.</p>
3911<h4><code>napi_is_dataview</code><span><a class="mark" href="#napi_is_dataview" id="napi_is_dataview">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_dataview"></a></h4>
3912<div class="api_metadata">
3913<span>Added in: v8.3.0</span>
3914<span>N-API version: 1</span>
3915</div>
3916<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_dataview</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3917<ul>
3918<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3919<li><code>[in] value</code>: The JavaScript value to check.</li>
3920<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a <code>DataView</code>.</li>
3921</ul>
3922<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3923<p>This API checks if the <code>Object</code> passed in is a <code>DataView</code>.</p>
3924<h4><code>napi_strict_equals</code><span><a class="mark" href="#napi_strict_equals" id="napi_strict_equals">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_strict_equals"></a></h4>
3925<div class="api_metadata">
3926<span>Added in: v8.0.0</span>
3927<span>N-API version: 1</span>
3928</div>
3929<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_strict_equals</span><span class="hljs-params">(napi_env env,
3930                               napi_value lhs,
3931                               napi_value rhs,
3932                               <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3933<ul>
3934<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3935<li><code>[in] lhs</code>: The JavaScript value to check.</li>
3936<li><code>[in] rhs</code>: The JavaScript value to check against.</li>
3937<li><code>[out] result</code>: Whether the two <code>napi_value</code> objects are equal.</li>
3938</ul>
3939<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3940<p>This API represents the invocation of the Strict Equality algorithm as
3941defined in <a href="https://tc39.github.io/ecma262/#sec-strict-equality-comparison">Section 7.2.14</a> of the ECMAScript Language Specification.</p>
3942<h4><code>napi_detach_arraybuffer</code><span><a class="mark" href="#napi_detach_arraybuffer" id="napi_detach_arraybuffer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_detach_arraybuffer"></a></h4>
3943<div class="api_metadata">
3944<span>Added in: v13.0.0, v12.16.0, v10.22.0</span>
3945<span>N-API version: 7</span>
3946</div>
3947<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_detach_arraybuffer</span><span class="hljs-params">(napi_env env,
3948                                    napi_value arraybuffer)</span></code> <button class="copy-button">copy</button></pre>
3949<ul>
3950<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3951<li><code>[in] arraybuffer</code>: The JavaScript <code>ArrayBuffer</code> to be detached.</li>
3952</ul>
3953<p>Returns <code>napi_ok</code> if the API succeeded. If a non-detachable <code>ArrayBuffer</code> is
3954passed in it returns <code>napi_detachable_arraybuffer_expected</code>.</p>
3955<p>Generally, an <code>ArrayBuffer</code> is non-detachable if it has been detached before.
3956The engine may impose additional conditions on whether an <code>ArrayBuffer</code> is
3957detachable. For example, V8 requires that the <code>ArrayBuffer</code> be external,
3958that is, created with <a href="#napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code></a>.</p>
3959<p>This API represents the invocation of the <code>ArrayBuffer</code> detach operation as
3960defined in <a href="https://tc39.es/ecma262/#sec-detacharraybuffer">Section 24.1.1.3</a> of the ECMAScript Language Specification.</p>
3961<h4><code>napi_is_detached_arraybuffer</code><span><a class="mark" href="#napi_is_detached_arraybuffer" id="napi_is_detached_arraybuffer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_detached_arraybuffer"></a></h4>
3962<div class="api_metadata">
3963<span>Added in: v13.3.0, v12.16.0, v10.22.0</span>
3964<span>N-API version: 7</span>
3965</div>
3966<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_detached_arraybuffer</span><span class="hljs-params">(napi_env env,
3967                                         napi_value arraybuffer,
3968                                         <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3969<ul>
3970<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3971<li><code>[in] arraybuffer</code>: The JavaScript <code>ArrayBuffer</code> to be checked.</li>
3972<li><code>[out] result</code>: Whether the <code>arraybuffer</code> is detached.</li>
3973</ul>
3974<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3975<p>The <code>ArrayBuffer</code> is considered detached if its internal data is <code>null</code>.</p>
3976<p>This API represents the invocation of the <code>ArrayBuffer</code> <code>IsDetachedBuffer</code>
3977operation as defined in <a href="https://tc39.es/ecma262/#sec-isdetachedbuffer">Section 24.1.1.2</a> of the ECMAScript Language
3978Specification.</p>
3979</section><section><h3>Working with JavaScript properties<span><a class="mark" href="#working-with-javascript-properties" id="working-with-javascript-properties">#</a></span><a aria-hidden="true" class="legacy" id="n_api_working_with_javascript_properties"></a></h3>
3980<p>Node-API exposes a set of APIs to get and set properties on JavaScript
3981objects. Some of these types are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Section 7</a> of the
3982<a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p>
3983<p>Properties in JavaScript are represented as a tuple of a key and a value.
3984Fundamentally, all property keys in Node-API can be represented in one of the
3985following forms:</p>
3986<ul>
3987<li>Named: a simple UTF8-encoded string</li>
3988<li>Integer-Indexed: an index value represented by <code>uint32_t</code></li>
3989<li>JavaScript value: these are represented in Node-API by <code>napi_value</code>. This can
3990be a <code>napi_value</code> representing a <code>string</code>, <code>number</code>, or <code>symbol</code>.</li>
3991</ul>
3992<p>Node-API values are represented by the type <code>napi_value</code>.
3993Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>.
3994However, it's the caller's responsibility to make sure that the
3995<code>napi_value</code> in question is of the JavaScript type expected by the API.</p>
3996<p>The APIs documented in this section provide a simple interface to
3997get and set properties on arbitrary JavaScript objects represented by
3998<code>napi_value</code>.</p>
3999<p>For instance, consider the following JavaScript code snippet:</p>
4000<pre><code class="language-js"><span class="hljs-keyword">const</span> obj = {};
4001obj.<span class="hljs-property">myProp</span> = <span class="hljs-number">123</span>;</code> <button class="copy-button">copy</button></pre>
4002<p>The equivalent can be done using Node-API values with the following snippet:</p>
4003<pre><code class="language-c">napi_status status = napi_generic_failure;
4004
4005<span class="hljs-comment">// const obj = {}</span>
4006napi_value obj, value;
4007status = napi_create_object(env, &#x26;obj);
4008<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
4009
4010<span class="hljs-comment">// Create a napi_value for 123</span>
4011status = napi_create_int32(env, <span class="hljs-number">123</span>, &#x26;value);
4012<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
4013
4014<span class="hljs-comment">// obj.myProp = 123</span>
4015status = napi_set_named_property(env, obj, <span class="hljs-string">"myProp"</span>, value);
4016<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code> <button class="copy-button">copy</button></pre>
4017<p>Indexed properties can be set in a similar manner. Consider the following
4018JavaScript snippet:</p>
4019<pre><code class="language-js"><span class="hljs-keyword">const</span> arr = [];
4020arr[<span class="hljs-number">123</span>] = <span class="hljs-string">'hello'</span>;</code> <button class="copy-button">copy</button></pre>
4021<p>The equivalent can be done using Node-API values with the following snippet:</p>
4022<pre><code class="language-c">napi_status status = napi_generic_failure;
4023
4024<span class="hljs-comment">// const arr = [];</span>
4025napi_value arr, value;
4026status = napi_create_array(env, &#x26;arr);
4027<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
4028
4029<span class="hljs-comment">// Create a napi_value for 'hello'</span>
4030status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &#x26;value);
4031<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
4032
4033<span class="hljs-comment">// arr[123] = 'hello';</span>
4034status = napi_set_element(env, arr, <span class="hljs-number">123</span>, value);
4035<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code> <button class="copy-button">copy</button></pre>
4036<p>Properties can be retrieved using the APIs described in this section.
4037Consider the following JavaScript snippet:</p>
4038<pre><code class="language-js"><span class="hljs-keyword">const</span> arr = [];
4039<span class="hljs-keyword">const</span> value = arr[<span class="hljs-number">123</span>];</code> <button class="copy-button">copy</button></pre>
4040<p>The following is the approximate equivalent of the Node-API counterpart:</p>
4041<pre><code class="language-c">napi_status status = napi_generic_failure;
4042
4043<span class="hljs-comment">// const arr = []</span>
4044napi_value arr, value;
4045status = napi_create_array(env, &#x26;arr);
4046<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
4047
4048<span class="hljs-comment">// const value = arr[123]</span>
4049status = napi_get_element(env, arr, <span class="hljs-number">123</span>, &#x26;value);
4050<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code> <button class="copy-button">copy</button></pre>
4051<p>Finally, multiple properties can also be defined on an object for performance
4052reasons. Consider the following JavaScript:</p>
4053<pre><code class="language-js"><span class="hljs-keyword">const</span> obj = {};
4054<span class="hljs-title class_">Object</span>.<span class="hljs-title function_">defineProperties</span>(obj, {
4055  <span class="hljs-string">'foo'</span>: { <span class="hljs-attr">value</span>: <span class="hljs-number">123</span>, <span class="hljs-attr">writable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">configurable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">enumerable</span>: <span class="hljs-literal">true</span> },
4056  <span class="hljs-string">'bar'</span>: { <span class="hljs-attr">value</span>: <span class="hljs-number">456</span>, <span class="hljs-attr">writable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">configurable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">enumerable</span>: <span class="hljs-literal">true</span> },
4057});</code> <button class="copy-button">copy</button></pre>
4058<p>The following is the approximate equivalent of the Node-API counterpart:</p>
4059<pre><code class="language-c">napi_status status = napi_status_generic_failure;
4060
4061<span class="hljs-comment">// const obj = {};</span>
4062napi_value obj;
4063status = napi_create_object(env, &#x26;obj);
4064<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
4065
4066<span class="hljs-comment">// Create napi_values for 123 and 456</span>
4067napi_value fooValue, barValue;
4068status = napi_create_int32(env, <span class="hljs-number">123</span>, &#x26;fooValue);
4069<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
4070status = napi_create_int32(env, <span class="hljs-number">456</span>, &#x26;barValue);
4071<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
4072
4073<span class="hljs-comment">// Set the properties</span>
4074napi_property_descriptor descriptors[] = {
4075  { <span class="hljs-string">"foo"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, fooValue, napi_writable | napi_configurable, <span class="hljs-literal">NULL</span> },
4076  { <span class="hljs-string">"bar"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, barValue, napi_writable | napi_configurable, <span class="hljs-literal">NULL</span> }
4077}
4078status = napi_define_properties(env,
4079                                obj,
4080                                <span class="hljs-keyword">sizeof</span>(descriptors) / <span class="hljs-keyword">sizeof</span>(descriptors[<span class="hljs-number">0</span>]),
4081                                descriptors);
4082<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code> <button class="copy-button">copy</button></pre>
4083<h4>Structures<span><a class="mark" href="#structures" id="structures">#</a></span><a aria-hidden="true" class="legacy" id="n_api_structures"></a></h4>
4084<h5><code>napi_property_attributes</code><span><a class="mark" href="#napi_property_attributes" id="napi_property_attributes">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_property_attributes"></a></h5>
4085<div class="api_metadata">
4086<details class="changelog"><summary>History</summary>
4087<table>
4088<tbody><tr><th>Version</th><th>Changes</th></tr>
4089<tr><td>v14.12.0</td>
4090<td><p>added <code>napi_default_method</code> and <code>napi_default_property</code>.</p></td></tr>
4091</tbody></table>
4092</details>
4093</div>
4094<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
4095  napi_default = <span class="hljs-number">0</span>,
4096  napi_writable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">0</span>,
4097  napi_enumerable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">1</span>,
4098  napi_configurable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">2</span>,
4099
4100  <span class="hljs-comment">// Used with napi_define_class to distinguish static properties</span>
4101  <span class="hljs-comment">// from instance properties. Ignored by napi_define_properties.</span>
4102  napi_static = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">10</span>,
4103
4104  <span class="hljs-comment">// Default for class methods.</span>
4105  napi_default_method = napi_writable | napi_configurable,
4106
4107  <span class="hljs-comment">// Default for object properties, like in JS obj[prop].</span>
4108  napi_default_jsproperty = napi_writable |
4109                          napi_enumerable |
4110                          napi_configurable,
4111} napi_property_attributes;</code> <button class="copy-button">copy</button></pre>
4112<p><code>napi_property_attributes</code> are flags used to control the behavior of properties
4113set on a JavaScript object. Other than <code>napi_static</code> they correspond to the
4114attributes listed in <a href="https://tc39.github.io/ecma262/#table-2">Section 6.1.7.1</a>
4115of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.
4116They can be one or more of the following bitflags:</p>
4117<ul>
4118<li><code>napi_default</code>: No explicit attributes are set on the property. By default, a
4119property is read only, not enumerable and not configurable.</li>
4120<li><code>napi_writable</code>: The property is writable.</li>
4121<li><code>napi_enumerable</code>: The property is enumerable.</li>
4122<li><code>napi_configurable</code>: The property is configurable as defined in
4123<a href="https://tc39.github.io/ecma262/#table-2">Section 6.1.7.1</a> of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</li>
4124<li><code>napi_static</code>: The property will be defined as a static property on a class as
4125opposed to an instance property, which is the default. This is used only by
4126<a href="#napi_define_class"><code>napi_define_class</code></a>. It is ignored by <code>napi_define_properties</code>.</li>
4127<li><code>napi_default_method</code>: Like a method in a JS class, the property is
4128configurable and writeable, but not enumerable.</li>
4129<li><code>napi_default_jsproperty</code>: Like a property set via assignment in JavaScript,
4130the property is writable, enumerable, and configurable.</li>
4131</ul>
4132<h5><code>napi_property_descriptor</code><span><a class="mark" href="#napi_property_descriptor" id="napi_property_descriptor">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_property_descriptor"></a></h5>
4133<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span>
4134  <span class="hljs-comment">// One of utf8name or name should be NULL.</span>
4135  <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8name;
4136  napi_value name;
4137
4138  napi_callback method;
4139  napi_callback getter;
4140  napi_callback setter;
4141  napi_value value;
4142
4143  napi_property_attributes attributes;
4144  <span class="hljs-type">void</span>* data;
4145} napi_property_descriptor;</code> <button class="copy-button">copy</button></pre>
4146<ul>
4147<li><code>utf8name</code>: Optional string describing the key for the property,
4148encoded as UTF8. One of <code>utf8name</code> or <code>name</code> must be provided for the
4149property.</li>
4150<li><code>name</code>: Optional <code>napi_value</code> that points to a JavaScript string or symbol
4151to be used as the key for the property. One of <code>utf8name</code> or <code>name</code> must
4152be provided for the property.</li>
4153<li><code>value</code>: The value that's retrieved by a get access of the property if the
4154property is a data property. If this is passed in, set <code>getter</code>, <code>setter</code>,
4155<code>method</code> and <code>data</code> to <code>NULL</code> (since these members won't be used).</li>
4156<li><code>getter</code>: A function to call when a get access of the property is performed.
4157If this is passed in, set <code>value</code> and <code>method</code> to <code>NULL</code> (since these members
4158won't be used). The given function is called implicitly by the runtime when
4159the property is accessed from JavaScript code (or if a get on the property is
4160performed using a Node-API call). <a href="#napi_callback"><code>napi_callback</code></a> provides more details.</li>
4161<li><code>setter</code>: A function to call when a set access of the property is performed.
4162If this is passed in, set <code>value</code> and <code>method</code> to <code>NULL</code> (since these members
4163won't be used). The given function is called implicitly by the runtime when
4164the property is set from JavaScript code (or if a set on the property is
4165performed using a Node-API call). <a href="#napi_callback"><code>napi_callback</code></a> provides more details.</li>
4166<li><code>method</code>: Set this to make the property descriptor object's <code>value</code>
4167property to be a JavaScript function represented by <code>method</code>. If this is
4168passed in, set <code>value</code>, <code>getter</code> and <code>setter</code> to <code>NULL</code> (since these members
4169won't be used). <a href="#napi_callback"><code>napi_callback</code></a> provides more details.</li>
4170<li><code>attributes</code>: The attributes associated with the particular property. See
4171<a href="#napi_property_attributes"><code>napi_property_attributes</code></a>.</li>
4172<li><code>data</code>: The callback data passed into <code>method</code>, <code>getter</code> and <code>setter</code> if this
4173function is invoked.</li>
4174</ul>
4175<h4>Functions<span><a class="mark" href="#functions" id="functions">#</a></span><a aria-hidden="true" class="legacy" id="n_api_functions"></a></h4>
4176<h5><code>napi_get_property_names</code><span><a class="mark" href="#napi_get_property_names" id="napi_get_property_names">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_property_names"></a></h5>
4177<div class="api_metadata">
4178<span>Added in: v8.0.0</span>
4179<span>N-API version: 1</span>
4180</div>
4181<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_property_names</span><span class="hljs-params">(napi_env env,
4182                                    napi_value object,
4183                                    napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
4184<ul>
4185<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4186<li><code>[in] object</code>: The object from which to retrieve the properties.</li>
4187<li><code>[out] result</code>: A <code>napi_value</code> representing an array of JavaScript values
4188that represent the property names of the object. The API can be used to
4189iterate over <code>result</code> using <a href="#napi_get_array_length"><code>napi_get_array_length</code></a>
4190and <a href="#napi_get_element"><code>napi_get_element</code></a>.</li>
4191</ul>
4192<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4193<p>This API returns the names of the enumerable properties of <code>object</code> as an array
4194of strings. The properties of <code>object</code> whose key is a symbol will not be
4195included.</p>
4196<h5><code>napi_get_all_property_names</code><span><a class="mark" href="#napi_get_all_property_names" id="napi_get_all_property_names">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_all_property_names"></a></h5>
4197<div class="api_metadata">
4198<span>Added in: v13.7.0, v12.17.0, v10.20.0</span>
4199<span>N-API version: 6</span>
4200</div>
4201<pre><code class="language-c">napi_get_all_property_names(napi_env env,
4202                            napi_value object,
4203                            napi_key_collection_mode key_mode,
4204                            napi_key_filter key_filter,
4205                            napi_key_conversion key_conversion,
4206                            napi_value* result);</code> <button class="copy-button">copy</button></pre>
4207<ul>
4208<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4209<li><code>[in] object</code>: The object from which to retrieve the properties.</li>
4210<li><code>[in] key_mode</code>: Whether to retrieve prototype properties as well.</li>
4211<li><code>[in] key_filter</code>: Which properties to retrieve
4212(enumerable/readable/writable).</li>
4213<li><code>[in] key_conversion</code>: Whether to convert numbered property keys to strings.</li>
4214<li><code>[out] result</code>: A <code>napi_value</code> representing an array of JavaScript values
4215that represent the property names of the object. <a href="#napi_get_array_length"><code>napi_get_array_length</code></a>
4216and <a href="#napi_get_element"><code>napi_get_element</code></a> can be used to iterate over <code>result</code>.</li>
4217</ul>
4218<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4219<p>This API returns an array containing the names of the available properties
4220of this object.</p>
4221<h5><code>napi_set_property</code><span><a class="mark" href="#napi_set_property" id="napi_set_property">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_set_property"></a></h5>
4222<div class="api_metadata">
4223<span>Added in: v8.0.0</span>
4224<span>N-API version: 1</span>
4225</div>
4226<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_property</span><span class="hljs-params">(napi_env env,
4227                              napi_value object,
4228                              napi_value key,
4229                              napi_value value)</span>;</code> <button class="copy-button">copy</button></pre>
4230<ul>
4231<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4232<li><code>[in] object</code>: The object on which to set the property.</li>
4233<li><code>[in] key</code>: The name of the property to set.</li>
4234<li><code>[in] value</code>: The property value.</li>
4235</ul>
4236<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4237<p>This API set a property on the <code>Object</code> passed in.</p>
4238<h5><code>napi_get_property</code><span><a class="mark" href="#napi_get_property" id="napi_get_property">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_property"></a></h5>
4239<div class="api_metadata">
4240<span>Added in: v8.0.0</span>
4241<span>N-API version: 1</span>
4242</div>
4243<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_property</span><span class="hljs-params">(napi_env env,
4244                              napi_value object,
4245                              napi_value key,
4246                              napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
4247<ul>
4248<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4249<li><code>[in] object</code>: The object from which to retrieve the property.</li>
4250<li><code>[in] key</code>: The name of the property to retrieve.</li>
4251<li><code>[out] result</code>: The value of the property.</li>
4252</ul>
4253<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4254<p>This API gets the requested property from the <code>Object</code> passed in.</p>
4255<h5><code>napi_has_property</code><span><a class="mark" href="#napi_has_property" id="napi_has_property">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_has_property"></a></h5>
4256<div class="api_metadata">
4257<span>Added in: v8.0.0</span>
4258<span>N-API version: 1</span>
4259</div>
4260<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_property</span><span class="hljs-params">(napi_env env,
4261                              napi_value object,
4262                              napi_value key,
4263                              <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
4264<ul>
4265<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4266<li><code>[in] object</code>: The object to query.</li>
4267<li><code>[in] key</code>: The name of the property whose existence to check.</li>
4268<li><code>[out] result</code>: Whether the property exists on the object or not.</li>
4269</ul>
4270<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4271<p>This API checks if the <code>Object</code> passed in has the named property.</p>
4272<h5><code>napi_delete_property</code><span><a class="mark" href="#napi_delete_property" id="napi_delete_property">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_delete_property"></a></h5>
4273<div class="api_metadata">
4274<span>Added in: v8.2.0</span>
4275<span>N-API version: 1</span>
4276</div>
4277<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_property</span><span class="hljs-params">(napi_env env,
4278                                 napi_value object,
4279                                 napi_value key,
4280                                 <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
4281<ul>
4282<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4283<li><code>[in] object</code>: The object to query.</li>
4284<li><code>[in] key</code>: The name of the property to delete.</li>
4285<li><code>[out] result</code>: Whether the property deletion succeeded or not. <code>result</code> can
4286optionally be ignored by passing <code>NULL</code>.</li>
4287</ul>
4288<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4289<p>This API attempts to delete the <code>key</code> own property from <code>object</code>.</p>
4290<h5><code>napi_has_own_property</code><span><a class="mark" href="#napi_has_own_property" id="napi_has_own_property">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_has_own_property"></a></h5>
4291<div class="api_metadata">
4292<span>Added in: v8.2.0</span>
4293<span>N-API version: 1</span>
4294</div>
4295<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_own_property</span><span class="hljs-params">(napi_env env,
4296                                  napi_value object,
4297                                  napi_value key,
4298                                  <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
4299<ul>
4300<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4301<li><code>[in] object</code>: The object to query.</li>
4302<li><code>[in] key</code>: The name of the own property whose existence to check.</li>
4303<li><code>[out] result</code>: Whether the own property exists on the object or not.</li>
4304</ul>
4305<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4306<p>This API checks if the <code>Object</code> passed in has the named own property. <code>key</code> must
4307be a <code>string</code> or a <code>symbol</code>, or an error will be thrown. Node-API will not
4308perform any conversion between data types.</p>
4309<h5><code>napi_set_named_property</code><span><a class="mark" href="#napi_set_named_property" id="napi_set_named_property">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_set_named_property"></a></h5>
4310<div class="api_metadata">
4311<span>Added in: v8.0.0</span>
4312<span>N-API version: 1</span>
4313</div>
4314<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_named_property</span><span class="hljs-params">(napi_env env,
4315                                    napi_value object,
4316                                    <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8Name,
4317                                    napi_value value)</span>;</code> <button class="copy-button">copy</button></pre>
4318<ul>
4319<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4320<li><code>[in] object</code>: The object on which to set the property.</li>
4321<li><code>[in] utf8Name</code>: The name of the property to set.</li>
4322<li><code>[in] value</code>: The property value.</li>
4323</ul>
4324<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4325<p>This method is equivalent to calling <a href="#napi_set_property"><code>napi_set_property</code></a> with a <code>napi_value</code>
4326created from the string passed in as <code>utf8Name</code>.</p>
4327<h5><code>napi_get_named_property</code><span><a class="mark" href="#napi_get_named_property" id="napi_get_named_property">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_named_property"></a></h5>
4328<div class="api_metadata">
4329<span>Added in: v8.0.0</span>
4330<span>N-API version: 1</span>
4331</div>
4332<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_named_property</span><span class="hljs-params">(napi_env env,
4333                                    napi_value object,
4334                                    <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8Name,
4335                                    napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
4336<ul>
4337<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4338<li><code>[in] object</code>: The object from which to retrieve the property.</li>
4339<li><code>[in] utf8Name</code>: The name of the property to get.</li>
4340<li><code>[out] result</code>: The value of the property.</li>
4341</ul>
4342<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4343<p>This method is equivalent to calling <a href="#napi_get_property"><code>napi_get_property</code></a> with a <code>napi_value</code>
4344created from the string passed in as <code>utf8Name</code>.</p>
4345<h5><code>napi_has_named_property</code><span><a class="mark" href="#napi_has_named_property" id="napi_has_named_property">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_has_named_property"></a></h5>
4346<div class="api_metadata">
4347<span>Added in: v8.0.0</span>
4348<span>N-API version: 1</span>
4349</div>
4350<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_named_property</span><span class="hljs-params">(napi_env env,
4351                                    napi_value object,
4352                                    <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8Name,
4353                                    <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
4354<ul>
4355<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4356<li><code>[in] object</code>: The object to query.</li>
4357<li><code>[in] utf8Name</code>: The name of the property whose existence to check.</li>
4358<li><code>[out] result</code>: Whether the property exists on the object or not.</li>
4359</ul>
4360<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4361<p>This method is equivalent to calling <a href="#napi_has_property"><code>napi_has_property</code></a> with a <code>napi_value</code>
4362created from the string passed in as <code>utf8Name</code>.</p>
4363<h5><code>napi_set_element</code><span><a class="mark" href="#napi_set_element" id="napi_set_element">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_set_element"></a></h5>
4364<div class="api_metadata">
4365<span>Added in: v8.0.0</span>
4366<span>N-API version: 1</span>
4367</div>
4368<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_element</span><span class="hljs-params">(napi_env env,
4369                             napi_value object,
4370                             <span class="hljs-type">uint32_t</span> index,
4371                             napi_value value)</span>;</code> <button class="copy-button">copy</button></pre>
4372<ul>
4373<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4374<li><code>[in] object</code>: The object from which to set the properties.</li>
4375<li><code>[in] index</code>: The index of the property to set.</li>
4376<li><code>[in] value</code>: The property value.</li>
4377</ul>
4378<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4379<p>This API sets an element on the <code>Object</code> passed in.</p>
4380<h5><code>napi_get_element</code><span><a class="mark" href="#napi_get_element" id="napi_get_element">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_element"></a></h5>
4381<div class="api_metadata">
4382<span>Added in: v8.0.0</span>
4383<span>N-API version: 1</span>
4384</div>
4385<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_element</span><span class="hljs-params">(napi_env env,
4386                             napi_value object,
4387                             <span class="hljs-type">uint32_t</span> index,
4388                             napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
4389<ul>
4390<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4391<li><code>[in] object</code>: The object from which to retrieve the property.</li>
4392<li><code>[in] index</code>: The index of the property to get.</li>
4393<li><code>[out] result</code>: The value of the property.</li>
4394</ul>
4395<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4396<p>This API gets the element at the requested index.</p>
4397<h5><code>napi_has_element</code><span><a class="mark" href="#napi_has_element" id="napi_has_element">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_has_element"></a></h5>
4398<div class="api_metadata">
4399<span>Added in: v8.0.0</span>
4400<span>N-API version: 1</span>
4401</div>
4402<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_element</span><span class="hljs-params">(napi_env env,
4403                             napi_value object,
4404                             <span class="hljs-type">uint32_t</span> index,
4405                             <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
4406<ul>
4407<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4408<li><code>[in] object</code>: The object to query.</li>
4409<li><code>[in] index</code>: The index of the property whose existence to check.</li>
4410<li><code>[out] result</code>: Whether the property exists on the object or not.</li>
4411</ul>
4412<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4413<p>This API returns if the <code>Object</code> passed in has an element at the
4414requested index.</p>
4415<h5><code>napi_delete_element</code><span><a class="mark" href="#napi_delete_element" id="napi_delete_element">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_delete_element"></a></h5>
4416<div class="api_metadata">
4417<span>Added in: v8.2.0</span>
4418<span>N-API version: 1</span>
4419</div>
4420<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_element</span><span class="hljs-params">(napi_env env,
4421                                napi_value object,
4422                                <span class="hljs-type">uint32_t</span> index,
4423                                <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
4424<ul>
4425<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4426<li><code>[in] object</code>: The object to query.</li>
4427<li><code>[in] index</code>: The index of the property to delete.</li>
4428<li><code>[out] result</code>: Whether the element deletion succeeded or not. <code>result</code> can
4429optionally be ignored by passing <code>NULL</code>.</li>
4430</ul>
4431<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4432<p>This API attempts to delete the specified <code>index</code> from <code>object</code>.</p>
4433<h5><code>napi_define_properties</code><span><a class="mark" href="#napi_define_properties" id="napi_define_properties">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_define_properties"></a></h5>
4434<div class="api_metadata">
4435<span>Added in: v8.0.0</span>
4436<span>N-API version: 1</span>
4437</div>
4438<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_properties</span><span class="hljs-params">(napi_env env,
4439                                   napi_value object,
4440                                   <span class="hljs-type">size_t</span> property_count,
4441                                   <span class="hljs-type">const</span> napi_property_descriptor* properties)</span>;</code> <button class="copy-button">copy</button></pre>
4442<ul>
4443<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4444<li><code>[in] object</code>: The object from which to retrieve the properties.</li>
4445<li><code>[in] property_count</code>: The number of elements in the <code>properties</code> array.</li>
4446<li><code>[in] properties</code>: The array of property descriptors.</li>
4447</ul>
4448<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4449<p>This method allows the efficient definition of multiple properties on a given
4450object. The properties are defined using property descriptors (see
4451<a href="#napi_property_descriptor"><code>napi_property_descriptor</code></a>). Given an array of such property descriptors,
4452this API will set the properties on the object one at a time, as defined by
4453<code>DefineOwnProperty()</code> (described in <a href="https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc">Section 9.1.6</a> of the ECMA-262
4454specification).</p>
4455<h5><code>napi_object_freeze</code><span><a class="mark" href="#napi_object_freeze" id="napi_object_freeze">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_object_freeze"></a></h5>
4456<div class="api_metadata">
4457<span>Added in: v14.14.0, v12.20.0</span>
4458<span>N-API version: 8</span>
4459</div>
4460<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_freeze</span><span class="hljs-params">(napi_env env,
4461                               napi_value object)</span>;</code> <button class="copy-button">copy</button></pre>
4462<ul>
4463<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4464<li><code>[in] object</code>: The object to freeze.</li>
4465</ul>
4466<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4467<p>This method freezes a given object. This prevents new properties from
4468being added to it, existing properties from being removed, prevents
4469changing the enumerability, configurability, or writability of existing
4470properties, and prevents the values of existing properties from being changed.
4471It also prevents the object's prototype from being changed. This is described
4472in <a href="https://tc39.es/ecma262/#sec-object.freeze">Section 19.1.2.6</a> of the
4473ECMA-262 specification.</p>
4474<h5><code>napi_object_seal</code><span><a class="mark" href="#napi_object_seal" id="napi_object_seal">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_object_seal"></a></h5>
4475<div class="api_metadata">
4476<span>Added in: v14.14.0, v12.20.0</span>
4477<span>N-API version: 8</span>
4478</div>
4479<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_seal</span><span class="hljs-params">(napi_env env,
4480                             napi_value object)</span>;</code> <button class="copy-button">copy</button></pre>
4481<ul>
4482<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4483<li><code>[in] object</code>: The object to seal.</li>
4484</ul>
4485<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4486<p>This method seals a given object. This prevents new properties from being
4487added to it, as well as marking all existing properties as non-configurable.
4488This is described in <a href="https://tc39.es/ecma262/#sec-object.seal">Section 19.1.2.20</a>
4489of the ECMA-262 specification.</p>
4490</section><section><h3>Working with JavaScript functions<span><a class="mark" href="#working-with-javascript-functions" id="working-with-javascript-functions">#</a></span><a aria-hidden="true" class="legacy" id="n_api_working_with_javascript_functions"></a></h3>
4491<p>Node-API provides a set of APIs that allow JavaScript code to
4492call back into native code. Node-APIs that support calling back
4493into native code take in a callback functions represented by
4494the <code>napi_callback</code> type. When the JavaScript VM calls back to
4495native code, the <code>napi_callback</code> function provided is invoked. The APIs
4496documented in this section allow the callback function to do the
4497following:</p>
4498<ul>
4499<li>Get information about the context in which the callback was invoked.</li>
4500<li>Get the arguments passed into the callback.</li>
4501<li>Return a <code>napi_value</code> back from the callback.</li>
4502</ul>
4503<p>Additionally, Node-API provides a set of functions which allow calling
4504JavaScript functions from native code. One can either call a function
4505like a regular JavaScript function call, or as a constructor
4506function.</p>
4507<p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> field of the
4508<code>napi_property_descriptor</code> items can be associated with <code>object</code> and freed
4509whenever <code>object</code> is garbage-collected by passing both <code>object</code> and the data to
4510<a href="#napi_add_finalizer"><code>napi_add_finalizer</code></a>.</p>
4511<h4><code>napi_call_function</code><span><a class="mark" href="#napi_call_function" id="napi_call_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_call_function"></a></h4>
4512<div class="api_metadata">
4513<span>Added in: v8.0.0</span>
4514<span>N-API version: 1</span>
4515</div>
4516<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_call_function</span><span class="hljs-params">(napi_env env,
4517                                           napi_value recv,
4518                                           napi_value func,
4519                                           <span class="hljs-type">size_t</span> argc,
4520                                           <span class="hljs-type">const</span> napi_value* argv,
4521                                           napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
4522<ul>
4523<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4524<li><code>[in] recv</code>: The <code>this</code> value passed to the called function.</li>
4525<li><code>[in] func</code>: <code>napi_value</code> representing the JavaScript function to be invoked.</li>
4526<li><code>[in] argc</code>: The count of elements in the <code>argv</code> array.</li>
4527<li><code>[in] argv</code>: Array of <code>napi_values</code> representing JavaScript values passed in
4528as arguments to the function.</li>
4529<li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned.</li>
4530</ul>
4531<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4532<p>This method allows a JavaScript function object to be called from a native
4533add-on. This is the primary mechanism of calling back <em>from</em> the add-on's
4534native code <em>into</em> JavaScript. For the special case of calling into JavaScript
4535after an async operation, see <a href="#napi_make_callback"><code>napi_make_callback</code></a>.</p>
4536<p>A sample use case might look as follows. Consider the following JavaScript
4537snippet:</p>
4538<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">AddTwo</span>(<span class="hljs-params">num</span>) {
4539  <span class="hljs-keyword">return</span> num + <span class="hljs-number">2</span>;
4540}
4541<span class="hljs-variable language_">global</span>.<span class="hljs-property">AddTwo</span> = <span class="hljs-title class_">AddTwo</span>;</code> <button class="copy-button">copy</button></pre>
4542<p>Then, the above function can be invoked from a native add-on using the
4543following code:</p>
4544<pre><code class="language-c"><span class="hljs-comment">// Get the function named "AddTwo" on the global object</span>
4545napi_value global, add_two, arg;
4546napi_status status = napi_get_global(env, &#x26;global);
4547<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4548
4549status = napi_get_named_property(env, global, <span class="hljs-string">"AddTwo"</span>, &#x26;add_two);
4550<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4551
4552<span class="hljs-comment">// const arg = 1337</span>
4553status = napi_create_int32(env, <span class="hljs-number">1337</span>, &#x26;arg);
4554<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4555
4556napi_value* argv = &#x26;arg;
4557<span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>;
4558
4559<span class="hljs-comment">// AddTwo(arg);</span>
4560napi_value return_val;
4561status = napi_call_function(env, global, add_two, argc, argv, &#x26;return_val);
4562<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4563
4564<span class="hljs-comment">// Convert the result back to a native type</span>
4565<span class="hljs-type">int32_t</span> result;
4566status = napi_get_value_int32(env, return_val, &#x26;result);
4567<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;</code> <button class="copy-button">copy</button></pre>
4568<h4><code>napi_create_function</code><span><a class="mark" href="#napi_create_function" id="napi_create_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_function"></a></h4>
4569<div class="api_metadata">
4570<span>Added in: v8.0.0</span>
4571<span>N-API version: 1</span>
4572</div>
4573<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_function</span><span class="hljs-params">(napi_env env,
4574                                 <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8name,
4575                                 <span class="hljs-type">size_t</span> length,
4576                                 napi_callback cb,
4577                                 <span class="hljs-type">void</span>* data,
4578                                 napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
4579<ul>
4580<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4581<li><code>[in] utf8Name</code>: Optional name of the function encoded as UTF8. This is
4582visible within JavaScript as the new function object's <code>name</code> property.</li>
4583<li><code>[in] length</code>: The length of the <code>utf8name</code> in bytes, or <code>NAPI_AUTO_LENGTH</code> if
4584it is null-terminated.</li>
4585<li><code>[in] cb</code>: The native function which should be called when this function
4586object is invoked. <a href="#napi_callback"><code>napi_callback</code></a> provides more details.</li>
4587<li><code>[in] data</code>: User-provided data context. This will be passed back into the
4588function when invoked later.</li>
4589<li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript function object for
4590the newly created function.</li>
4591</ul>
4592<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4593<p>This API allows an add-on author to create a function object in native code.
4594This is the primary mechanism to allow calling <em>into</em> the add-on's native code
4595<em>from</em> JavaScript.</p>
4596<p>The newly created function is not automatically visible from script after this
4597call. Instead, a property must be explicitly set on any object that is visible
4598to JavaScript, in order for the function to be accessible from script.</p>
4599<p>In order to expose a function as part of the
4600add-on's module exports, set the newly created function on the exports
4601object. A sample module might look as follows:</p>
4602<pre><code class="language-c">napi_value <span class="hljs-title function_">SayHello</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> {
4603  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello\n"</span>);
4604  <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4605}
4606
4607napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> {
4608  napi_status status;
4609
4610  napi_value fn;
4611  status = napi_create_function(env, <span class="hljs-literal">NULL</span>, <span class="hljs-number">0</span>, SayHello, <span class="hljs-literal">NULL</span>, &#x26;fn);
4612  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4613
4614  status = napi_set_named_property(env, exports, <span class="hljs-string">"sayHello"</span>, fn);
4615  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4616
4617  <span class="hljs-keyword">return</span> exports;
4618}
4619
4620NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)</code> <button class="copy-button">copy</button></pre>
4621<p>Given the above code, the add-on can be used from JavaScript as follows:</p>
4622<pre><code class="language-js"><span class="hljs-keyword">const</span> myaddon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./addon'</span>);
4623myaddon.<span class="hljs-title function_">sayHello</span>();</code> <button class="copy-button">copy</button></pre>
4624<p>The string passed to <code>require()</code> is the name of the target in <code>binding.gyp</code>
4625responsible for creating the <code>.node</code> file.</p>
4626<p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> parameter can
4627be associated with the resulting JavaScript function (which is returned in the
4628<code>result</code> parameter) and freed whenever the function is garbage-collected by
4629passing both the JavaScript function and the data to <a href="#napi_add_finalizer"><code>napi_add_finalizer</code></a>.</p>
4630<p>JavaScript <code>Function</code>s are described in <a href="https://tc39.github.io/ecma262/#sec-function-objects">Section 19.2</a> of the ECMAScript
4631Language Specification.</p>
4632<h4><code>napi_get_cb_info</code><span><a class="mark" href="#napi_get_cb_info" id="napi_get_cb_info">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_cb_info"></a></h4>
4633<div class="api_metadata">
4634<span>Added in: v8.0.0</span>
4635<span>N-API version: 1</span>
4636</div>
4637<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_cb_info</span><span class="hljs-params">(napi_env env,
4638                             napi_callback_info cbinfo,
4639                             <span class="hljs-type">size_t</span>* argc,
4640                             napi_value* argv,
4641                             napi_value* thisArg,
4642                             <span class="hljs-type">void</span>** data)</span></code> <button class="copy-button">copy</button></pre>
4643<ul>
4644<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4645<li><code>[in] cbinfo</code>: The callback info passed into the callback function.</li>
4646<li><code>[in-out] argc</code>: Specifies the length of the provided <code>argv</code> array and
4647receives the actual count of arguments. <code>argc</code> can
4648optionally be ignored by passing <code>NULL</code>.</li>
4649<li><code>[out] argv</code>: C array of <code>napi_value</code>s to which the arguments will be
4650copied. If there are more arguments than the provided count, only the
4651requested number of arguments are copied. If there are fewer arguments
4652provided than claimed, the rest of <code>argv</code> is filled with <code>napi_value</code> values
4653that represent <code>undefined</code>. <code>argv</code> can optionally be ignored by
4654passing <code>NULL</code>.</li>
4655<li><code>[out] thisArg</code>: Receives the JavaScript <code>this</code> argument for the call.
4656<code>thisArg</code> can optionally be ignored by passing <code>NULL</code>.</li>
4657<li><code>[out] data</code>: Receives the data pointer for the callback. <code>data</code> can
4658optionally be ignored by passing <code>NULL</code>.</li>
4659</ul>
4660<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4661<p>This method is used within a callback function to retrieve details about the
4662call like the arguments and the <code>this</code> pointer from a given callback info.</p>
4663<h4><code>napi_get_new_target</code><span><a class="mark" href="#napi_get_new_target" id="napi_get_new_target">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_new_target"></a></h4>
4664<div class="api_metadata">
4665<span>Added in: v8.6.0</span>
4666<span>N-API version: 1</span>
4667</div>
4668<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_new_target</span><span class="hljs-params">(napi_env env,
4669                                napi_callback_info cbinfo,
4670                                napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
4671<ul>
4672<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4673<li><code>[in] cbinfo</code>: The callback info passed into the callback function.</li>
4674<li><code>[out] result</code>: The <code>new.target</code> of the constructor call.</li>
4675</ul>
4676<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4677<p>This API returns the <code>new.target</code> of the constructor call. If the current
4678callback is not a constructor call, the result is <code>NULL</code>.</p>
4679<h4><code>napi_new_instance</code><span><a class="mark" href="#napi_new_instance" id="napi_new_instance">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_new_instance"></a></h4>
4680<div class="api_metadata">
4681<span>Added in: v8.0.0</span>
4682<span>N-API version: 1</span>
4683</div>
4684<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_new_instance</span><span class="hljs-params">(napi_env env,
4685                              napi_value cons,
4686                              <span class="hljs-type">size_t</span> argc,
4687                              napi_value* argv,
4688                              napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
4689<ul>
4690<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4691<li><code>[in] cons</code>: <code>napi_value</code> representing the JavaScript function to be invoked
4692as a constructor.</li>
4693<li><code>[in] argc</code>: The count of elements in the <code>argv</code> array.</li>
4694<li><code>[in] argv</code>: Array of JavaScript values as <code>napi_value</code> representing the
4695arguments to the constructor. If <code>argc</code> is zero this parameter may be
4696omitted by passing in <code>NULL</code>.</li>
4697<li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned,
4698which in this case is the constructed object.</li>
4699</ul>
4700<p>This method is used to instantiate a new JavaScript value using a given
4701<code>napi_value</code> that represents the constructor for the object. For example,
4702consider the following snippet:</p>
4703<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">MyObject</span>(<span class="hljs-params">param</span>) {
4704  <span class="hljs-variable language_">this</span>.<span class="hljs-property">param</span> = param;
4705}
4706
4707<span class="hljs-keyword">const</span> arg = <span class="hljs-string">'hello'</span>;
4708<span class="hljs-keyword">const</span> value = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyObject</span>(arg);</code> <button class="copy-button">copy</button></pre>
4709<p>The following can be approximated in Node-API using the following snippet:</p>
4710<pre><code class="language-c"><span class="hljs-comment">// Get the constructor function MyObject</span>
4711napi_value global, constructor, arg, value;
4712napi_status status = napi_get_global(env, &#x26;global);
4713<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4714
4715status = napi_get_named_property(env, global, <span class="hljs-string">"MyObject"</span>, &#x26;constructor);
4716<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4717
4718<span class="hljs-comment">// const arg = "hello"</span>
4719status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &#x26;arg);
4720<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4721
4722napi_value* argv = &#x26;arg;
4723<span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>;
4724
4725<span class="hljs-comment">// const value = new MyObject(arg)</span>
4726status = napi_new_instance(env, constructor, argc, argv, &#x26;value);</code> <button class="copy-button">copy</button></pre>
4727<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4728</section><section><h3>Object wrap<span><a class="mark" href="#object-wrap" id="object-wrap">#</a></span><a aria-hidden="true" class="legacy" id="n_api_object_wrap"></a></h3>
4729<p>Node-API offers a way to "wrap" C++ classes and instances so that the class
4730constructor and methods can be called from JavaScript.</p>
4731<ol>
4732<li>The <a href="#napi_define_class"><code>napi_define_class</code></a> API defines a JavaScript class with constructor,
4733static properties and methods, and instance properties and methods that
4734correspond to the C++ class.</li>
4735<li>When JavaScript code invokes the constructor, the constructor callback
4736uses <a href="#napi_wrap"><code>napi_wrap</code></a> to wrap a new C++ instance in a JavaScript object,
4737then returns the wrapper object.</li>
4738<li>When JavaScript code invokes a method or property accessor on the class,
4739the corresponding <code>napi_callback</code> C++ function is invoked. For an instance
4740callback, <a href="#napi_unwrap"><code>napi_unwrap</code></a> obtains the C++ instance that is the target of
4741the call.</li>
4742</ol>
4743<p>For wrapped objects it may be difficult to distinguish between a function
4744called on a class prototype and a function called on an instance of a class.
4745A common pattern used to address this problem is to save a persistent
4746reference to the class constructor for later <code>instanceof</code> checks.</p>
4747<pre><code class="language-c">napi_value MyClass_constructor = <span class="hljs-literal">NULL</span>;
4748status = napi_get_reference_value(env, MyClass::es_constructor, &#x26;MyClass_constructor);
4749assert(napi_ok == status);
4750<span class="hljs-type">bool</span> is_instance = <span class="hljs-literal">false</span>;
4751status = napi_instanceof(env, es_this, MyClass_constructor, &#x26;is_instance);
4752assert(napi_ok == status);
4753<span class="hljs-keyword">if</span> (is_instance) {
4754  <span class="hljs-comment">// napi_unwrap() ...</span>
4755} <span class="hljs-keyword">else</span> {
4756  <span class="hljs-comment">// otherwise...</span>
4757}</code> <button class="copy-button">copy</button></pre>
4758<p>The reference must be freed once it is no longer needed.</p>
4759<p>There are occasions where <code>napi_instanceof()</code> is insufficient for ensuring that
4760a JavaScript object is a wrapper for a certain native type. This is the case
4761especially when wrapped JavaScript objects are passed back into the addon via
4762static methods rather than as the <code>this</code> value of prototype methods. In such
4763cases there is a chance that they may be unwrapped incorrectly.</p>
4764<pre><code class="language-js"><span class="hljs-keyword">const</span> myAddon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/my_addon.node'</span>);
4765
4766<span class="hljs-comment">// `openDatabase()` returns a JavaScript object that wraps a native database</span>
4767<span class="hljs-comment">// handle.</span>
4768<span class="hljs-keyword">const</span> dbHandle = myAddon.<span class="hljs-title function_">openDatabase</span>();
4769
4770<span class="hljs-comment">// `query()` returns a JavaScript object that wraps a native query handle.</span>
4771<span class="hljs-keyword">const</span> queryHandle = myAddon.<span class="hljs-title function_">query</span>(dbHandle, <span class="hljs-string">'Gimme ALL the things!'</span>);
4772
4773<span class="hljs-comment">// There is an accidental error in the line below. The first parameter to</span>
4774<span class="hljs-comment">// `myAddon.queryHasRecords()` should be the database handle (`dbHandle`), not</span>
4775<span class="hljs-comment">// the query handle (`query`), so the correct condition for the while-loop</span>
4776<span class="hljs-comment">// should be</span>
4777<span class="hljs-comment">//</span>
4778<span class="hljs-comment">// myAddon.queryHasRecords(dbHandle, queryHandle)</span>
4779<span class="hljs-comment">//</span>
4780<span class="hljs-keyword">while</span> (myAddon.<span class="hljs-title function_">queryHasRecords</span>(queryHandle, dbHandle)) {
4781  <span class="hljs-comment">// retrieve records</span>
4782}</code> <button class="copy-button">copy</button></pre>
4783<p>In the above example <code>myAddon.queryHasRecords()</code> is a method that accepts two
4784arguments. The first is a database handle and the second is a query handle.
4785Internally, it unwraps the first argument and casts the resulting pointer to a
4786native database handle. It then unwraps the second argument and casts the
4787resulting pointer to a query handle. If the arguments are passed in the wrong
4788order, the casts will work, however, there is a good chance that the underlying
4789database operation will fail, or will even cause an invalid memory access.</p>
4790<p>To ensure that the pointer retrieved from the first argument is indeed a pointer
4791to a database handle and, similarly, that the pointer retrieved from the second
4792argument is indeed a pointer to a query handle, the implementation of
4793<code>queryHasRecords()</code> has to perform a type validation. Retaining the JavaScript
4794class constructor from which the database handle was instantiated and the
4795constructor from which the query handle was instantiated in <code>napi_ref</code>s can
4796help, because <code>napi_instanceof()</code> can then be used to ensure that the instances
4797passed into <code>queryHashRecords()</code> are indeed of the correct type.</p>
4798<p>Unfortunately, <code>napi_instanceof()</code> does not protect against prototype
4799manipulation. For example, the prototype of the database handle instance can be
4800set to the prototype of the constructor for query handle instances. In this
4801case, the database handle instance can appear as a query handle instance, and it
4802will pass the <code>napi_instanceof()</code> test for a query handle instance, while still
4803containing a pointer to a database handle.</p>
4804<p>To this end, Node-API provides type-tagging capabilities.</p>
4805<p>A type tag is a 128-bit integer unique to the addon. Node-API provides the
4806<code>napi_type_tag</code> structure for storing a type tag. When such a value is passed
4807along with a JavaScript object or <a href="#napi_create_external">external</a> stored in a <code>napi_value</code> to
4808<code>napi_type_tag_object()</code>, the JavaScript object will be "marked" with the
4809type tag. The "mark" is invisible on the JavaScript side. When a JavaScript
4810object arrives into a native binding, <code>napi_check_object_type_tag()</code> can be used
4811along with the original type tag to determine whether the JavaScript object was
4812previously "marked" with the type tag. This creates a type-checking capability
4813of a higher fidelity than <code>napi_instanceof()</code> can provide, because such type-
4814tagging survives prototype manipulation and addon unloading/reloading.</p>
4815<p>Continuing the above example, the following skeleton addon implementation
4816illustrates the use of <code>napi_type_tag_object()</code> and
4817<code>napi_check_object_type_tag()</code>.</p>
4818<pre><code class="language-c"><span class="hljs-comment">// This value is the type tag for a database handle. The command</span>
4819<span class="hljs-comment">//</span>
4820<span class="hljs-comment">//   uuidgen | sed -r -e 's/-//g' -e 's/(.{16})(.*)/0x\1, 0x\2/'</span>
4821<span class="hljs-comment">//</span>
4822<span class="hljs-comment">// can be used to obtain the two values with which to initialize the structure.</span>
4823<span class="hljs-type">static</span> <span class="hljs-type">const</span> napi_type_tag DatabaseHandleTypeTag = {
4824  <span class="hljs-number">0x1edf75a38336451d</span>, <span class="hljs-number">0xa5ed9ce2e4c00c38</span>
4825};
4826
4827<span class="hljs-comment">// This value is the type tag for a query handle.</span>
4828<span class="hljs-type">static</span> <span class="hljs-type">const</span> napi_type_tag QueryHandleTypeTag = {
4829  <span class="hljs-number">0x9c73317f9fad44a3</span>, <span class="hljs-number">0x93c3920bf3b0ad6a</span>
4830};
4831
4832<span class="hljs-type">static</span> napi_value
4833<span class="hljs-title function_">openDatabase</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> {
4834  napi_status status;
4835  napi_value result;
4836
4837  <span class="hljs-comment">// Perform the underlying action which results in a database handle.</span>
4838  DatabaseHandle* dbHandle = open_database();
4839
4840  <span class="hljs-comment">// Create a new, empty JS object.</span>
4841  status = napi_create_object(env, &#x26;result);
4842  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4843
4844  <span class="hljs-comment">// Tag the object to indicate that it holds a pointer to a `DatabaseHandle`.</span>
4845  status = napi_type_tag_object(env, result, &#x26;DatabaseHandleTypeTag);
4846  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4847
4848  <span class="hljs-comment">// Store the pointer to the `DatabaseHandle` structure inside the JS object.</span>
4849  status = napi_wrap(env, result, dbHandle, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>);
4850  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4851
4852  <span class="hljs-keyword">return</span> result;
4853}
4854
4855<span class="hljs-comment">// Later when we receive a JavaScript object purporting to be a database handle</span>
4856<span class="hljs-comment">// we can use `napi_check_object_type_tag()` to ensure that it is indeed such a</span>
4857<span class="hljs-comment">// handle.</span>
4858
4859<span class="hljs-type">static</span> napi_value
4860<span class="hljs-title function_">query</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> {
4861  napi_status status;
4862  <span class="hljs-type">size_t</span> argc = <span class="hljs-number">2</span>;
4863  napi_value argv[<span class="hljs-number">2</span>];
4864  <span class="hljs-type">bool</span> is_db_handle;
4865
4866  status = napi_get_cb_info(env, info, &#x26;argc, argv, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>);
4867  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4868
4869  <span class="hljs-comment">// Check that the object passed as the first parameter has the previously</span>
4870  <span class="hljs-comment">// applied tag.</span>
4871  status = napi_check_object_type_tag(env,
4872                                      argv[<span class="hljs-number">0</span>],
4873                                      &#x26;DatabaseHandleTypeTag,
4874                                      &#x26;is_db_handle);
4875  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4876
4877  <span class="hljs-comment">// Throw a `TypeError` if it doesn't.</span>
4878  <span class="hljs-keyword">if</span> (!is_db_handle) {
4879    <span class="hljs-comment">// Throw a TypeError.</span>
4880    <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4881  }
4882}</code> <button class="copy-button">copy</button></pre>
4883<h4><code>napi_define_class</code><span><a class="mark" href="#napi_define_class" id="napi_define_class">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_define_class"></a></h4>
4884<div class="api_metadata">
4885<span>Added in: v8.0.0</span>
4886<span>N-API version: 1</span>
4887</div>
4888<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_class</span><span class="hljs-params">(napi_env env,
4889                              <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8name,
4890                              <span class="hljs-type">size_t</span> length,
4891                              napi_callback constructor,
4892                              <span class="hljs-type">void</span>* data,
4893                              <span class="hljs-type">size_t</span> property_count,
4894                              <span class="hljs-type">const</span> napi_property_descriptor* properties,
4895                              napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
4896<ul>
4897<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4898<li><code>[in] utf8name</code>: Name of the JavaScript constructor function. For clarity,
4899it is recommended to use the C++ class name when wrapping a C++ class.</li>
4900<li><code>[in] length</code>: The length of the <code>utf8name</code> in bytes, or <code>NAPI_AUTO_LENGTH</code>
4901if it is null-terminated.</li>
4902<li><code>[in] constructor</code>: Callback function that handles constructing instances
4903of the class. When wrapping a C++ class, this method must be a static member
4904with the <a href="#napi_callback"><code>napi_callback</code></a> signature. A C++ class constructor cannot be
4905used. <a href="#napi_callback"><code>napi_callback</code></a> provides more details.</li>
4906<li><code>[in] data</code>: Optional data to be passed to the constructor callback as
4907the <code>data</code> property of the callback info.</li>
4908<li><code>[in] property_count</code>: Number of items in the <code>properties</code> array argument.</li>
4909<li><code>[in] properties</code>: Array of property descriptors describing static and
4910instance data properties, accessors, and methods on the class
4911See <code>napi_property_descriptor</code>.</li>
4912<li><code>[out] result</code>: A <code>napi_value</code> representing the constructor function for
4913the class.</li>
4914</ul>
4915<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4916<p>Defines a JavaScript class, including:</p>
4917<ul>
4918<li>A JavaScript constructor function that has the class name. When wrapping a
4919corresponding C++ class, the callback passed via <code>constructor</code> can be used to
4920instantiate a new C++ class instance, which can then be placed inside the
4921JavaScript object instance being constructed using <a href="#napi_wrap"><code>napi_wrap</code></a>.</li>
4922<li>Properties on the constructor function whose implementation can call
4923corresponding <em>static</em> data properties, accessors, and methods of the C++
4924class (defined by property descriptors with the <code>napi_static</code> attribute).</li>
4925<li>Properties on the constructor function's <code>prototype</code> object. When wrapping a
4926C++ class, <em>non-static</em> data properties, accessors, and methods of the C++
4927class can be called from the static functions given in the property
4928descriptors without the <code>napi_static</code> attribute after retrieving the C++ class
4929instance placed inside the JavaScript object instance by using
4930<a href="#napi_unwrap"><code>napi_unwrap</code></a>.</li>
4931</ul>
4932<p>When wrapping a C++ class, the C++ constructor callback passed via <code>constructor</code>
4933should be a static method on the class that calls the actual class constructor,
4934then wraps the new C++ instance in a JavaScript object, and returns the wrapper
4935object. See <a href="#napi_wrap"><code>napi_wrap</code></a> for details.</p>
4936<p>The JavaScript constructor function returned from <a href="#napi_define_class"><code>napi_define_class</code></a> is
4937often saved and used later to construct new instances of the class from native
4938code, and/or to check whether provided values are instances of the class. In
4939that case, to prevent the function value from being garbage-collected, a
4940strong persistent reference to it can be created using
4941<a href="#napi_create_reference"><code>napi_create_reference</code></a>, ensuring that the reference count is kept >= 1.</p>
4942<p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> parameter or via
4943the <code>data</code> field of the <code>napi_property_descriptor</code> array items can be associated
4944with the resulting JavaScript constructor (which is returned in the <code>result</code>
4945parameter) and freed whenever the class is garbage-collected by passing both
4946the JavaScript function and the data to <a href="#napi_add_finalizer"><code>napi_add_finalizer</code></a>.</p>
4947<h4><code>napi_wrap</code><span><a class="mark" href="#napi_wrap" id="napi_wrap">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_wrap"></a></h4>
4948<div class="api_metadata">
4949<span>Added in: v8.0.0</span>
4950<span>N-API version: 1</span>
4951</div>
4952<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_wrap</span><span class="hljs-params">(napi_env env,
4953                      napi_value js_object,
4954                      <span class="hljs-type">void</span>* native_object,
4955                      napi_finalize finalize_cb,
4956                      <span class="hljs-type">void</span>* finalize_hint,
4957                      napi_ref* result)</span>;</code> <button class="copy-button">copy</button></pre>
4958<ul>
4959<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4960<li><code>[in] js_object</code>: The JavaScript object that will be the wrapper for the
4961native object.</li>
4962<li><code>[in] native_object</code>: The native instance that will be wrapped in the
4963JavaScript object.</li>
4964<li><code>[in] finalize_cb</code>: Optional native callback that can be used to free the
4965native instance when the JavaScript object has been garbage-collected.
4966<a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
4967<li><code>[in] finalize_hint</code>: Optional contextual hint that is passed to the
4968finalize callback.</li>
4969<li><code>[out] result</code>: Optional reference to the wrapped object.</li>
4970</ul>
4971<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4972<p>Wraps a native instance in a JavaScript object. The native instance can be
4973retrieved later using <code>napi_unwrap()</code>.</p>
4974<p>When JavaScript code invokes a constructor for a class that was defined using
4975<code>napi_define_class()</code>, the <code>napi_callback</code> for the constructor is invoked.
4976After constructing an instance of the native class, the callback must then call
4977<code>napi_wrap()</code> to wrap the newly constructed instance in the already-created
4978JavaScript object that is the <code>this</code> argument to the constructor callback.
4979(That <code>this</code> object was created from the constructor function's <code>prototype</code>,
4980so it already has definitions of all the instance properties and methods.)</p>
4981<p>Typically when wrapping a class instance, a finalize callback should be
4982provided that simply deletes the native instance that is received as the <code>data</code>
4983argument to the finalize callback.</p>
4984<p>The optional returned reference is initially a weak reference, meaning it
4985has a reference count of 0. Typically this reference count would be incremented
4986temporarily during async operations that require the instance to remain valid.</p>
4987<p><em>Caution</em>: The optional returned reference (if obtained) should be deleted via
4988<a href="#napi_delete_reference"><code>napi_delete_reference</code></a> ONLY in response to the finalize callback
4989invocation. If it is deleted before then, then the finalize callback may never
4990be invoked. Therefore, when obtaining a reference a finalize callback is also
4991required in order to enable correct disposal of the reference.</p>
4992<p>Finalizer callbacks may be deferred, leaving a window where the object has
4993been garbage collected (and the weak reference is invalid) but the finalizer
4994hasn't been called yet. When using <code>napi_get_reference_value()</code> on weak
4995references returned by <code>napi_wrap()</code>, you should still handle an empty result.</p>
4996<p>Calling <code>napi_wrap()</code> a second time on an object will return an error. To
4997associate another native instance with the object, use <code>napi_remove_wrap()</code>
4998first.</p>
4999<h4><code>napi_unwrap</code><span><a class="mark" href="#napi_unwrap" id="napi_unwrap">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_unwrap"></a></h4>
5000<div class="api_metadata">
5001<span>Added in: v8.0.0</span>
5002<span>N-API version: 1</span>
5003</div>
5004<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_unwrap</span><span class="hljs-params">(napi_env env,
5005                        napi_value js_object,
5006                        <span class="hljs-type">void</span>** result)</span>;</code> <button class="copy-button">copy</button></pre>
5007<ul>
5008<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5009<li><code>[in] js_object</code>: The object associated with the native instance.</li>
5010<li><code>[out] result</code>: Pointer to the wrapped native instance.</li>
5011</ul>
5012<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5013<p>Retrieves a native instance that was previously wrapped in a JavaScript
5014object using <code>napi_wrap()</code>.</p>
5015<p>When JavaScript code invokes a method or property accessor on the class, the
5016corresponding <code>napi_callback</code> is invoked. If the callback is for an instance
5017method or accessor, then the <code>this</code> argument to the callback is the wrapper
5018object; the wrapped C++ instance that is the target of the call can be obtained
5019then by calling <code>napi_unwrap()</code> on the wrapper object.</p>
5020<h4><code>napi_remove_wrap</code><span><a class="mark" href="#napi_remove_wrap" id="napi_remove_wrap">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_remove_wrap"></a></h4>
5021<div class="api_metadata">
5022<span>Added in: v8.5.0</span>
5023<span>N-API version: 1</span>
5024</div>
5025<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_remove_wrap</span><span class="hljs-params">(napi_env env,
5026                             napi_value js_object,
5027                             <span class="hljs-type">void</span>** result)</span>;</code> <button class="copy-button">copy</button></pre>
5028<ul>
5029<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5030<li><code>[in] js_object</code>: The object associated with the native instance.</li>
5031<li><code>[out] result</code>: Pointer to the wrapped native instance.</li>
5032</ul>
5033<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5034<p>Retrieves a native instance that was previously wrapped in the JavaScript
5035object <code>js_object</code> using <code>napi_wrap()</code> and removes the wrapping. If a finalize
5036callback was associated with the wrapping, it will no longer be called when the
5037JavaScript object becomes garbage-collected.</p>
5038<h4><code>napi_type_tag_object</code><span><a class="mark" href="#napi_type_tag_object" id="napi_type_tag_object">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_type_tag_object"></a></h4>
5039<div class="api_metadata">
5040<span>Added in: v14.8.0, v12.19.0</span>
5041<span>N-API version: 8</span>
5042</div>
5043<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_type_tag_object</span><span class="hljs-params">(napi_env env,
5044                                 napi_value js_object,
5045                                 <span class="hljs-type">const</span> napi_type_tag* type_tag)</span>;</code> <button class="copy-button">copy</button></pre>
5046<ul>
5047<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5048<li><code>[in] js_object</code>: The JavaScript object or <a href="#napi_create_external">external</a> to be marked.</li>
5049<li><code>[in] type_tag</code>: The tag with which the object is to be marked.</li>
5050</ul>
5051<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5052<p>Associates the value of the <code>type_tag</code> pointer with the JavaScript object or
5053<a href="#napi_create_external">external</a>. <code>napi_check_object_type_tag()</code> can then be used to compare the tag
5054that was attached to the object with one owned by the addon to ensure that the
5055object has the right type.</p>
5056<p>If the object already has an associated type tag, this API will return
5057<code>napi_invalid_arg</code>.</p>
5058<h4><code>napi_check_object_type_tag</code><span><a class="mark" href="#napi_check_object_type_tag" id="napi_check_object_type_tag">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_check_object_type_tag"></a></h4>
5059<div class="api_metadata">
5060<span>Added in: v14.8.0, v12.19.0</span>
5061<span>N-API version: 8</span>
5062</div>
5063<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_check_object_type_tag</span><span class="hljs-params">(napi_env env,
5064                                       napi_value js_object,
5065                                       <span class="hljs-type">const</span> napi_type_tag* type_tag,
5066                                       <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
5067<ul>
5068<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5069<li><code>[in] js_object</code>: The JavaScript object or <a href="#napi_create_external">external</a> whose type tag to
5070examine.</li>
5071<li><code>[in] type_tag</code>: The tag with which to compare any tag found on the object.</li>
5072<li><code>[out] result</code>: Whether the type tag given matched the type tag on the
5073object. <code>false</code> is also returned if no type tag was found on the object.</li>
5074</ul>
5075<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5076<p>Compares the pointer given as <code>type_tag</code> with any that can be found on
5077<code>js_object</code>. If no tag is found on <code>js_object</code> or, if a tag is found but it does
5078not match <code>type_tag</code>, then <code>result</code> is set to <code>false</code>. If a tag is found and it
5079matches <code>type_tag</code>, then <code>result</code> is set to <code>true</code>.</p>
5080<h4><code>napi_add_finalizer</code><span><a class="mark" href="#napi_add_finalizer" id="napi_add_finalizer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_add_finalizer"></a></h4>
5081<div class="api_metadata">
5082<span>Added in: v8.0.0</span>
5083<span>N-API version: 5</span>
5084</div>
5085<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_add_finalizer</span><span class="hljs-params">(napi_env env,
5086                               napi_value js_object,
5087                               <span class="hljs-type">void</span>* finalize_data,
5088                               napi_finalize finalize_cb,
5089                               <span class="hljs-type">void</span>* finalize_hint,
5090                               napi_ref* result)</span>;</code> <button class="copy-button">copy</button></pre>
5091<ul>
5092<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5093<li><code>[in] js_object</code>: The JavaScript object to which the native data will be
5094attached.</li>
5095<li><code>[in] finalize_data</code>: Optional data to be passed to <code>finalize_cb</code>.</li>
5096<li><code>[in] finalize_cb</code>: Native callback that will be used to free the
5097native data when the JavaScript object has been garbage-collected.
5098<a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
5099<li><code>[in] finalize_hint</code>: Optional contextual hint that is passed to the
5100finalize callback.</li>
5101<li><code>[out] result</code>: Optional reference to the JavaScript object.</li>
5102</ul>
5103<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5104<p>Adds a <code>napi_finalize</code> callback which will be called when the JavaScript object
5105in <code>js_object</code> has been garbage-collected.</p>
5106<p>This API can be called multiple times on a single JavaScript object.</p>
5107<p><em>Caution</em>: The optional returned reference (if obtained) should be deleted via
5108<a href="#napi_delete_reference"><code>napi_delete_reference</code></a> ONLY in response to the finalize callback
5109invocation. If it is deleted before then, then the finalize callback may never
5110be invoked. Therefore, when obtaining a reference a finalize callback is also
5111required in order to enable correct disposal of the reference.</p>
5112</section><section><h3>Simple asynchronous operations<span><a class="mark" href="#simple-asynchronous-operations" id="simple-asynchronous-operations">#</a></span><a aria-hidden="true" class="legacy" id="n_api_simple_asynchronous_operations"></a></h3>
5113<p>Addon modules often need to leverage async helpers from libuv as part of their
5114implementation. This allows them to schedule work to be executed asynchronously
5115so that their methods can return in advance of the work being completed. This
5116allows them to avoid blocking overall execution of the Node.js application.</p>
5117<p>Node-API provides an ABI-stable interface for these
5118supporting functions which covers the most common asynchronous use cases.</p>
5119<p>Node-API defines the <code>napi_async_work</code> structure which is used to manage
5120asynchronous workers. Instances are created/deleted with
5121<a href="#napi_create_async_work"><code>napi_create_async_work</code></a> and <a href="#napi_delete_async_work"><code>napi_delete_async_work</code></a>.</p>
5122<p>The <code>execute</code> and <code>complete</code> callbacks are functions that will be
5123invoked when the executor is ready to execute and when it completes its
5124task respectively.</p>
5125<p>The <code>execute</code> function should avoid making any Node-API calls
5126that could result in the execution of JavaScript or interaction with
5127JavaScript objects. Most often, any code that needs to make Node-API
5128calls should be made in <code>complete</code> callback instead.
5129Avoid using the <code>napi_env</code> parameter in the execute callback as
5130it will likely execute JavaScript.</p>
5131<p>These functions implement the following interfaces:</p>
5132<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_execute_callback)</span><span class="hljs-params">(napi_env env,
5133                                            <span class="hljs-type">void</span>* data)</span>;
5134<span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_complete_callback)</span><span class="hljs-params">(napi_env env,
5135                                             napi_status status,
5136                                             <span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button></pre>
5137<p>When these methods are invoked, the <code>data</code> parameter passed will be the
5138addon-provided <code>void*</code> data that was passed into the
5139<code>napi_create_async_work</code> call.</p>
5140<p>Once created the async worker can be queued
5141for execution using the <a href="#napi_queue_async_work"><code>napi_queue_async_work</code></a> function:</p>
5142<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work</span><span class="hljs-params">(napi_env env,
5143                                  napi_async_work work)</span>;</code> <button class="copy-button">copy</button></pre>
5144<p><a href="#napi_cancel_async_work"><code>napi_cancel_async_work</code></a> can be used if the work needs
5145to be cancelled before the work has started execution.</p>
5146<p>After calling <a href="#napi_cancel_async_work"><code>napi_cancel_async_work</code></a>, the <code>complete</code> callback
5147will be invoked with a status value of <code>napi_cancelled</code>.
5148The work should not be deleted before the <code>complete</code>
5149callback invocation, even when it was cancelled.</p>
5150<h4><code>napi_create_async_work</code><span><a class="mark" href="#napi_create_async_work" id="napi_create_async_work">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_async_work"></a></h4>
5151<div class="api_metadata">
5152<details class="changelog"><summary>History</summary>
5153<table>
5154<tbody><tr><th>Version</th><th>Changes</th></tr>
5155<tr><td>v8.6.0</td>
5156<td><p>Added <code>async_resource</code> and <code>async_resource_name</code> parameters.</p></td></tr>
5157<tr><td>v8.0.0</td>
5158<td><p><span>Added in: v8.0.0</span></p></td></tr>
5159</tbody></table>
5160</details>
5161<span>N-API version: 1</span>
5162</div>
5163<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_async_work</span><span class="hljs-params">(napi_env env,
5164                                   napi_value async_resource,
5165                                   napi_value async_resource_name,
5166                                   napi_async_execute_callback execute,
5167                                   napi_async_complete_callback complete,
5168                                   <span class="hljs-type">void</span>* data,
5169                                   napi_async_work* result)</span>;</code> <button class="copy-button">copy</button></pre>
5170<ul>
5171<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5172<li><code>[in] async_resource</code>: An optional object associated with the async work
5173that will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#initasyncid-type-triggerasyncid-resource"><code>init</code> hooks</a>.</li>
5174<li><code>[in] async_resource_name</code>: Identifier for the kind of resource that is being
5175provided for diagnostic information exposed by the <code>async_hooks</code> API.</li>
5176<li><code>[in] execute</code>: The native function which should be called to execute the
5177logic asynchronously. The given function is called from a worker pool thread
5178and can execute in parallel with the main event loop thread.</li>
5179<li><code>[in] complete</code>: The native function which will be called when the
5180asynchronous logic is completed or is cancelled. The given function is called
5181from the main event loop thread. <a href="#napi_async_complete_callback"><code>napi_async_complete_callback</code></a> provides
5182more details.</li>
5183<li><code>[in] data</code>: User-provided data context. This will be passed back into the
5184execute and complete functions.</li>
5185<li><code>[out] result</code>: <code>napi_async_work*</code> which is the handle to the newly created
5186async work.</li>
5187</ul>
5188<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5189<p>This API allocates a work object that is used to execute logic asynchronously.
5190It should be freed using <a href="#napi_delete_async_work"><code>napi_delete_async_work</code></a> once the work is no longer
5191required.</p>
5192<p><code>async_resource_name</code> should be a null-terminated, UTF-8-encoded string.</p>
5193<p>The <code>async_resource_name</code> identifier is provided by the user and should be
5194representative of the type of async work being performed. It is also recommended
5195to apply namespacing to the identifier, e.g. by including the module name. See
5196the <a href="async_hooks.html#type"><code>async_hooks</code> documentation</a> for more information.</p>
5197<h4><code>napi_delete_async_work</code><span><a class="mark" href="#napi_delete_async_work" id="napi_delete_async_work">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_delete_async_work"></a></h4>
5198<div class="api_metadata">
5199<span>Added in: v8.0.0</span>
5200<span>N-API version: 1</span>
5201</div>
5202<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_async_work</span><span class="hljs-params">(napi_env env,
5203                                   napi_async_work work)</span>;</code> <button class="copy-button">copy</button></pre>
5204<ul>
5205<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5206<li><code>[in] work</code>: The handle returned by the call to <code>napi_create_async_work</code>.</li>
5207</ul>
5208<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5209<p>This API frees a previously allocated work object.</p>
5210<p>This API can be called even if there is a pending JavaScript exception.</p>
5211<h4><code>napi_queue_async_work</code><span><a class="mark" href="#napi_queue_async_work" id="napi_queue_async_work">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_queue_async_work"></a></h4>
5212<div class="api_metadata">
5213<span>Added in: v8.0.0</span>
5214<span>N-API version: 1</span>
5215</div>
5216<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work</span><span class="hljs-params">(napi_env env,
5217                                  napi_async_work work)</span>;</code> <button class="copy-button">copy</button></pre>
5218<ul>
5219<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5220<li><code>[in] work</code>: The handle returned by the call to <code>napi_create_async_work</code>.</li>
5221</ul>
5222<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5223<p>This API requests that the previously allocated work be scheduled
5224for execution. Once it returns successfully, this API must not be called again
5225with the same <code>napi_async_work</code> item or the result will be undefined.</p>
5226<h4><code>napi_cancel_async_work</code><span><a class="mark" href="#napi_cancel_async_work" id="napi_cancel_async_work">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_cancel_async_work"></a></h4>
5227<div class="api_metadata">
5228<span>Added in: v8.0.0</span>
5229<span>N-API version: 1</span>
5230</div>
5231<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_cancel_async_work</span><span class="hljs-params">(napi_env env,
5232                                   napi_async_work work)</span>;</code> <button class="copy-button">copy</button></pre>
5233<ul>
5234<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5235<li><code>[in] work</code>: The handle returned by the call to <code>napi_create_async_work</code>.</li>
5236</ul>
5237<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5238<p>This API cancels queued work if it has not yet
5239been started. If it has already started executing, it cannot be
5240cancelled and <code>napi_generic_failure</code> will be returned. If successful,
5241the <code>complete</code> callback will be invoked with a status value of
5242<code>napi_cancelled</code>. The work should not be deleted before the <code>complete</code>
5243callback invocation, even if it has been successfully cancelled.</p>
5244<p>This API can be called even if there is a pending JavaScript exception.</p>
5245</section><section><h3>Custom asynchronous operations<span><a class="mark" href="#custom-asynchronous-operations" id="custom-asynchronous-operations">#</a></span><a aria-hidden="true" class="legacy" id="n_api_custom_asynchronous_operations"></a></h3>
5246<p>The simple asynchronous work APIs above may not be appropriate for every
5247scenario. When using any other asynchronous mechanism, the following APIs
5248are necessary to ensure an asynchronous operation is properly tracked by
5249the runtime.</p>
5250<h4><code>napi_async_init</code><span><a class="mark" href="#napi_async_init" id="napi_async_init">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_async_init"></a></h4>
5251<div class="api_metadata">
5252<span>Added in: v8.6.0</span>
5253<span>N-API version: 1</span>
5254</div>
5255<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_init</span><span class="hljs-params">(napi_env env,
5256                            napi_value async_resource,
5257                            napi_value async_resource_name,
5258                            napi_async_context* result)</span></code> <button class="copy-button">copy</button></pre>
5259<ul>
5260<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5261<li><code>[in] async_resource</code>: Object associated with the async work
5262that will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#initasyncid-type-triggerasyncid-resource"><code>init</code> hooks</a> and can be
5263accessed by <a href="async_hooks.html#async_hooksexecutionasyncresource"><code>async_hooks.executionAsyncResource()</code></a>.</li>
5264<li><code>[in] async_resource_name</code>: Identifier for the kind of resource that is being
5265provided for diagnostic information exposed by the <code>async_hooks</code> API.</li>
5266<li><code>[out] result</code>: The initialized async context.</li>
5267</ul>
5268<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5269<p>The <code>async_resource</code> object needs to be kept alive until
5270<a href="#napi_async_destroy"><code>napi_async_destroy</code></a> to keep <code>async_hooks</code> related API acts correctly. In
5271order to retain ABI compatibility with previous versions, <code>napi_async_context</code>s
5272are not maintaining the strong reference to the <code>async_resource</code> objects to
5273avoid introducing causing memory leaks. However, if the <code>async_resource</code> is
5274garbage collected by JavaScript engine before the <code>napi_async_context</code> was
5275destroyed by <code>napi_async_destroy</code>, calling <code>napi_async_context</code> related APIs
5276like <a href="#napi_open_callback_scope"><code>napi_open_callback_scope</code></a> and <a href="#napi_make_callback"><code>napi_make_callback</code></a> can cause
5277problems like loss of async context when using the <code>AsyncLocalStorage</code> API.</p>
5278<p>In order to retain ABI compatibility with previous versions, passing <code>NULL</code>
5279for <code>async_resource</code> does not result in an error. However, this is not
5280recommended as this will result poor results with  <code>async_hooks</code>
5281<a href="async_hooks.html#initasyncid-type-triggerasyncid-resource"><code>init</code> hooks</a> and <code>async_hooks.executionAsyncResource()</code> as the resource is
5282now required by the underlying <code>async_hooks</code> implementation in order to provide
5283the linkage between async callbacks.</p>
5284<h4><code>napi_async_destroy</code><span><a class="mark" href="#napi_async_destroy" id="napi_async_destroy">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_async_destroy"></a></h4>
5285<div class="api_metadata">
5286<span>Added in: v8.6.0</span>
5287<span>N-API version: 1</span>
5288</div>
5289<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_destroy</span><span class="hljs-params">(napi_env env,
5290                               napi_async_context async_context)</span>;</code> <button class="copy-button">copy</button></pre>
5291<ul>
5292<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5293<li><code>[in] async_context</code>: The async context to be destroyed.</li>
5294</ul>
5295<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5296<p>This API can be called even if there is a pending JavaScript exception.</p>
5297<h4><code>napi_make_callback</code><span><a class="mark" href="#napi_make_callback" id="napi_make_callback">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_make_callback"></a></h4>
5298<div class="api_metadata">
5299<details class="changelog"><summary>History</summary>
5300<table>
5301<tbody><tr><th>Version</th><th>Changes</th></tr>
5302<tr><td>v8.6.0</td>
5303<td><p>Added <code>async_context</code> parameter.</p></td></tr>
5304<tr><td>v8.0.0</td>
5305<td><p><span>Added in: v8.0.0</span></p></td></tr>
5306</tbody></table>
5307</details>
5308<span>N-API version: 1</span>
5309</div>
5310<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_make_callback</span><span class="hljs-params">(napi_env env,
5311                                           napi_async_context async_context,
5312                                           napi_value recv,
5313                                           napi_value func,
5314                                           <span class="hljs-type">size_t</span> argc,
5315                                           <span class="hljs-type">const</span> napi_value* argv,
5316                                           napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
5317<ul>
5318<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5319<li><code>[in] async_context</code>: Context for the async operation that is
5320invoking the callback. This should normally be a value previously
5321obtained from <a href="#napi_async_init"><code>napi_async_init</code></a>.
5322In order to retain ABI compatibility with previous versions, passing <code>NULL</code>
5323for <code>async_context</code> does not result in an error. However, this results
5324in incorrect operation of async hooks. Potential issues include loss of
5325async context when using the <code>AsyncLocalStorage</code> API.</li>
5326<li><code>[in] recv</code>: The <code>this</code> value passed to the called function.</li>
5327<li><code>[in] func</code>: <code>napi_value</code> representing the JavaScript function to be invoked.</li>
5328<li><code>[in] argc</code>: The count of elements in the <code>argv</code> array.</li>
5329<li><code>[in] argv</code>: Array of JavaScript values as <code>napi_value</code> representing the
5330arguments to the function. If <code>argc</code> is zero this parameter may be
5331omitted by passing in <code>NULL</code>.</li>
5332<li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned.</li>
5333</ul>
5334<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5335<p>This method allows a JavaScript function object to be called from a native
5336add-on. This API is similar to <code>napi_call_function</code>. However, it is used to call
5337<em>from</em> native code back <em>into</em> JavaScript <em>after</em> returning from an async
5338operation (when there is no other script on the stack). It is a fairly simple
5339wrapper around <code>node::MakeCallback</code>.</p>
5340<p>Note it is <em>not</em> necessary to use <code>napi_make_callback</code> from within a
5341<code>napi_async_complete_callback</code>; in that situation the callback's async
5342context has already been set up, so a direct call to <code>napi_call_function</code>
5343is sufficient and appropriate. Use of the <code>napi_make_callback</code> function
5344may be required when implementing custom async behavior that does not use
5345<code>napi_create_async_work</code>.</p>
5346<p>Any <code>process.nextTick</code>s or Promises scheduled on the microtask queue by
5347JavaScript during the callback are ran before returning back to C/C++.</p>
5348<h4><code>napi_open_callback_scope</code><span><a class="mark" href="#napi_open_callback_scope" id="napi_open_callback_scope">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_open_callback_scope"></a></h4>
5349<div class="api_metadata">
5350<span>Added in: v9.6.0</span>
5351<span>N-API version: 3</span>
5352</div>
5353<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_open_callback_scope</span><span class="hljs-params">(napi_env env,
5354                                                 napi_value resource_object,
5355                                                 napi_async_context context,
5356                                                 napi_callback_scope* result)</span></code> <button class="copy-button">copy</button></pre>
5357<ul>
5358<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5359<li><code>[in] resource_object</code>: An object associated with the async work
5360that will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#initasyncid-type-triggerasyncid-resource"><code>init</code> hooks</a>. This
5361parameter has been deprecated and is ignored at runtime. Use the
5362<code>async_resource</code> parameter in <a href="#napi_async_init"><code>napi_async_init</code></a> instead.</li>
5363<li><code>[in] context</code>: Context for the async operation that is invoking the callback.
5364This should be a value previously obtained from <a href="#napi_async_init"><code>napi_async_init</code></a>.</li>
5365<li><code>[out] result</code>: The newly created scope.</li>
5366</ul>
5367<p>There are cases (for example, resolving promises) where it is
5368necessary to have the equivalent of the scope associated with a callback
5369in place when making certain Node-API calls. If there is no other script on
5370the stack the <a href="#napi_open_callback_scope"><code>napi_open_callback_scope</code></a> and
5371<a href="#napi_close_callback_scope"><code>napi_close_callback_scope</code></a> functions can be used to open/close
5372the required scope.</p>
5373<h4><code>napi_close_callback_scope</code><span><a class="mark" href="#napi_close_callback_scope" id="napi_close_callback_scope">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_close_callback_scope"></a></h4>
5374<div class="api_metadata">
5375<span>Added in: v9.6.0</span>
5376<span>N-API version: 3</span>
5377</div>
5378<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_close_callback_scope</span><span class="hljs-params">(napi_env env,
5379                                                  napi_callback_scope scope)</span></code> <button class="copy-button">copy</button></pre>
5380<ul>
5381<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5382<li><code>[in] scope</code>: The scope to be closed.</li>
5383</ul>
5384<p>This API can be called even if there is a pending JavaScript exception.</p>
5385</section><section><h3>Version management<span><a class="mark" href="#version-management" id="version-management">#</a></span><a aria-hidden="true" class="legacy" id="n_api_version_management"></a></h3>
5386<h4><code>napi_get_node_version</code><span><a class="mark" href="#napi_get_node_version" id="napi_get_node_version">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_node_version"></a></h4>
5387<div class="api_metadata">
5388<span>Added in: v8.4.0</span>
5389<span>N-API version: 1</span>
5390</div>
5391<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span>
5392  <span class="hljs-type">uint32_t</span> major;
5393  <span class="hljs-type">uint32_t</span> minor;
5394  <span class="hljs-type">uint32_t</span> patch;
5395  <span class="hljs-type">const</span> <span class="hljs-type">char</span>* release;
5396} napi_node_version;
5397
5398napi_status <span class="hljs-title function_">napi_get_node_version</span><span class="hljs-params">(napi_env env,
5399                                  <span class="hljs-type">const</span> napi_node_version** version)</span>;</code> <button class="copy-button">copy</button></pre>
5400<ul>
5401<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5402<li><code>[out] version</code>: A pointer to version information for Node.js itself.</li>
5403</ul>
5404<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5405<p>This function fills the <code>version</code> struct with the major, minor, and patch
5406version of Node.js that is currently running, and the <code>release</code> field with the
5407value of <a href="process.html#processrelease"><code>process.release.name</code></a>.</p>
5408<p>The returned buffer is statically allocated and does not need to be freed.</p>
5409<h4><code>napi_get_version</code><span><a class="mark" href="#napi_get_version" id="napi_get_version">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_version"></a></h4>
5410<div class="api_metadata">
5411<span>Added in: v8.0.0</span>
5412<span>N-API version: 1</span>
5413</div>
5414<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_version</span><span class="hljs-params">(napi_env env,
5415                             <span class="hljs-type">uint32_t</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
5416<ul>
5417<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5418<li><code>[out] result</code>: The highest version of Node-API supported.</li>
5419</ul>
5420<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5421<p>This API returns the highest Node-API version supported by the
5422Node.js runtime. Node-API is planned to be additive such that
5423newer releases of Node.js may support additional API functions.
5424In order to allow an addon to use a newer function when running with
5425versions of Node.js that support it, while providing
5426fallback behavior when running with Node.js versions that don't
5427support it:</p>
5428<ul>
5429<li>Call <code>napi_get_version()</code> to determine if the API is available.</li>
5430<li>If available, dynamically load a pointer to the function using <code>uv_dlsym()</code>.</li>
5431<li>Use the dynamically loaded pointer to invoke the function.</li>
5432<li>If the function is not available, provide an alternate implementation
5433that does not use the function.</li>
5434</ul>
5435</section><section><h3>Memory management<span><a class="mark" href="#memory-management" id="memory-management">#</a></span><a aria-hidden="true" class="legacy" id="n_api_memory_management"></a></h3>
5436<h4><code>napi_adjust_external_memory</code><span><a class="mark" href="#napi_adjust_external_memory" id="napi_adjust_external_memory">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_adjust_external_memory"></a></h4>
5437<div class="api_metadata">
5438<span>Added in: v8.5.0</span>
5439<span>N-API version: 1</span>
5440</div>
5441<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_adjust_external_memory</span><span class="hljs-params">(napi_env env,
5442                                                    <span class="hljs-type">int64_t</span> change_in_bytes,
5443                                                    <span class="hljs-type">int64_t</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
5444<ul>
5445<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5446<li><code>[in] change_in_bytes</code>: The change in externally allocated memory that is kept
5447alive by JavaScript objects.</li>
5448<li><code>[out] result</code>: The adjusted value</li>
5449</ul>
5450<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5451<p>This function gives V8 an indication of the amount of externally allocated
5452memory that is kept alive by JavaScript objects (i.e. a JavaScript object
5453that points to its own memory allocated by a native addon). Registering
5454externally allocated memory will trigger global garbage collections more
5455often than it would otherwise.</p>
5456</section><section><h3>Promises<span><a class="mark" href="#promises" id="promises">#</a></span><a aria-hidden="true" class="legacy" id="n_api_promises"></a></h3>
5457<p>Node-API provides facilities for creating <code>Promise</code> objects as described in
5458<a href="https://tc39.github.io/ecma262/#sec-promise-objects">Section 25.4</a> of the ECMA specification. It implements promises as a pair of
5459objects. When a promise is created by <code>napi_create_promise()</code>, a "deferred"
5460object is created and returned alongside the <code>Promise</code>. The deferred object is
5461bound to the created <code>Promise</code> and is the only means to resolve or reject the
5462<code>Promise</code> using <code>napi_resolve_deferred()</code> or <code>napi_reject_deferred()</code>. The
5463deferred object that is created by <code>napi_create_promise()</code> is freed by
5464<code>napi_resolve_deferred()</code> or <code>napi_reject_deferred()</code>. The <code>Promise</code> object may
5465be returned to JavaScript where it can be used in the usual fashion.</p>
5466<p>For example, to create a promise and pass it to an asynchronous worker:</p>
5467<pre><code class="language-c">napi_deferred deferred;
5468napi_value promise;
5469napi_status status;
5470
5471<span class="hljs-comment">// Create the promise.</span>
5472status = napi_create_promise(env, &#x26;deferred, &#x26;promise);
5473<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
5474
5475<span class="hljs-comment">// Pass the deferred to a function that performs an asynchronous action.</span>
5476do_something_asynchronous(deferred);
5477
5478<span class="hljs-comment">// Return the promise to JS</span>
5479<span class="hljs-keyword">return</span> promise;</code> <button class="copy-button">copy</button></pre>
5480<p>The above function <code>do_something_asynchronous()</code> would perform its asynchronous
5481action and then it would resolve or reject the deferred, thereby concluding the
5482promise and freeing the deferred:</p>
5483<pre><code class="language-c">napi_deferred deferred;
5484napi_value undefined;
5485napi_status status;
5486
5487<span class="hljs-comment">// Create a value with which to conclude the deferred.</span>
5488status = napi_get_undefined(env, &#x26;undefined);
5489<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
5490
5491<span class="hljs-comment">// Resolve or reject the promise associated with the deferred depending on</span>
5492<span class="hljs-comment">// whether the asynchronous action succeeded.</span>
5493<span class="hljs-keyword">if</span> (asynchronous_action_succeeded) {
5494  status = napi_resolve_deferred(env, deferred, undefined);
5495} <span class="hljs-keyword">else</span> {
5496  status = napi_reject_deferred(env, deferred, undefined);
5497}
5498<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
5499
5500<span class="hljs-comment">// At this point the deferred has been freed, so we should assign NULL to it.</span>
5501deferred = <span class="hljs-literal">NULL</span>;</code> <button class="copy-button">copy</button></pre>
5502<h4><code>napi_create_promise</code><span><a class="mark" href="#napi_create_promise" id="napi_create_promise">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_promise"></a></h4>
5503<div class="api_metadata">
5504<span>Added in: v8.5.0</span>
5505<span>N-API version: 1</span>
5506</div>
5507<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_promise</span><span class="hljs-params">(napi_env env,
5508                                napi_deferred* deferred,
5509                                napi_value* promise)</span>;</code> <button class="copy-button">copy</button></pre>
5510<ul>
5511<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5512<li><code>[out] deferred</code>: A newly created deferred object which can later be passed to
5513<code>napi_resolve_deferred()</code> or <code>napi_reject_deferred()</code> to resolve resp. reject
5514the associated promise.</li>
5515<li><code>[out] promise</code>: The JavaScript promise associated with the deferred object.</li>
5516</ul>
5517<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5518<p>This API creates a deferred object and a JavaScript promise.</p>
5519<h4><code>napi_resolve_deferred</code><span><a class="mark" href="#napi_resolve_deferred" id="napi_resolve_deferred">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_resolve_deferred"></a></h4>
5520<div class="api_metadata">
5521<span>Added in: v8.5.0</span>
5522<span>N-API version: 1</span>
5523</div>
5524<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_resolve_deferred</span><span class="hljs-params">(napi_env env,
5525                                  napi_deferred deferred,
5526                                  napi_value resolution)</span>;</code> <button class="copy-button">copy</button></pre>
5527<ul>
5528<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5529<li><code>[in] deferred</code>: The deferred object whose associated promise to resolve.</li>
5530<li><code>[in] resolution</code>: The value with which to resolve the promise.</li>
5531</ul>
5532<p>This API resolves a JavaScript promise by way of the deferred object
5533with which it is associated. Thus, it can only be used to resolve JavaScript
5534promises for which the corresponding deferred object is available. This
5535effectively means that the promise must have been created using
5536<code>napi_create_promise()</code> and the deferred object returned from that call must
5537have been retained in order to be passed to this API.</p>
5538<p>The deferred object is freed upon successful completion.</p>
5539<h4><code>napi_reject_deferred</code><span><a class="mark" href="#napi_reject_deferred" id="napi_reject_deferred">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_reject_deferred"></a></h4>
5540<div class="api_metadata">
5541<span>Added in: v8.5.0</span>
5542<span>N-API version: 1</span>
5543</div>
5544<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_reject_deferred</span><span class="hljs-params">(napi_env env,
5545                                 napi_deferred deferred,
5546                                 napi_value rejection)</span>;</code> <button class="copy-button">copy</button></pre>
5547<ul>
5548<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5549<li><code>[in] deferred</code>: The deferred object whose associated promise to resolve.</li>
5550<li><code>[in] rejection</code>: The value with which to reject the promise.</li>
5551</ul>
5552<p>This API rejects a JavaScript promise by way of the deferred object
5553with which it is associated. Thus, it can only be used to reject JavaScript
5554promises for which the corresponding deferred object is available. This
5555effectively means that the promise must have been created using
5556<code>napi_create_promise()</code> and the deferred object returned from that call must
5557have been retained in order to be passed to this API.</p>
5558<p>The deferred object is freed upon successful completion.</p>
5559<h4><code>napi_is_promise</code><span><a class="mark" href="#napi_is_promise" id="napi_is_promise">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_promise"></a></h4>
5560<div class="api_metadata">
5561<span>Added in: v8.5.0</span>
5562<span>N-API version: 1</span>
5563</div>
5564<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_promise</span><span class="hljs-params">(napi_env env,
5565                            napi_value value,
5566                            <span class="hljs-type">bool</span>* is_promise)</span>;</code> <button class="copy-button">copy</button></pre>
5567<ul>
5568<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5569<li><code>[in] value</code>: The value to examine</li>
5570<li><code>[out] is_promise</code>: Flag indicating whether <code>promise</code> is a native promise
5571object (that is, a promise object created by the underlying engine).</li>
5572</ul>
5573</section><section><h3>Script execution<span><a class="mark" href="#script-execution" id="script-execution">#</a></span><a aria-hidden="true" class="legacy" id="n_api_script_execution"></a></h3>
5574<p>Node-API provides an API for executing a string containing JavaScript using the
5575underlying JavaScript engine.</p>
5576<h4><code>napi_run_script</code><span><a class="mark" href="#napi_run_script" id="napi_run_script">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_run_script"></a></h4>
5577<div class="api_metadata">
5578<span>Added in: v8.5.0</span>
5579<span>N-API version: 1</span>
5580</div>
5581<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_run_script</span><span class="hljs-params">(napi_env env,
5582                                        napi_value script,
5583                                        napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
5584<ul>
5585<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5586<li><code>[in] script</code>: A JavaScript string containing the script to execute.</li>
5587<li><code>[out] result</code>: The value resulting from having executed the script.</li>
5588</ul>
5589<p>This function executes a string of JavaScript code and returns its result with
5590the following caveats:</p>
5591<ul>
5592<li>Unlike <code>eval</code>, this function does not allow the script to access the current
5593lexical scope, and therefore also does not allow to access the
5594<a href="modules.html#the-module-scope">module scope</a>, meaning that pseudo-globals such as <code>require</code> will not be
5595available.</li>
5596<li>The script can access the <a href="globals.html">global scope</a>. Function and <code>var</code> declarations
5597in the script will be added to the <a href="globals.html#global"><code>global</code></a> object. Variable declarations
5598made using <code>let</code> and <code>const</code> will be visible globally, but will not be added
5599to the <a href="globals.html#global"><code>global</code></a> object.</li>
5600<li>The value of <code>this</code> is <a href="globals.html#global"><code>global</code></a> within the script.</li>
5601</ul>
5602</section><section><h3>libuv event loop<span><a class="mark" href="#libuv-event-loop" id="libuv-event-loop">#</a></span><a aria-hidden="true" class="legacy" id="n_api_libuv_event_loop"></a></h3>
5603<p>Node-API provides a function for getting the current event loop associated with
5604a specific <code>napi_env</code>.</p>
5605<h4><code>napi_get_uv_event_loop</code><span><a class="mark" href="#napi_get_uv_event_loop" id="napi_get_uv_event_loop">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_uv_event_loop"></a></h4>
5606<div class="api_metadata">
5607<span>Added in: v9.3.0, v8.10.0</span>
5608<span>N-API version: 2</span>
5609</div>
5610<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_uv_event_loop</span><span class="hljs-params">(napi_env env,
5611                                               <span class="hljs-keyword">struct</span> uv_loop_s** loop)</span>;</code> <button class="copy-button">copy</button></pre>
5612<ul>
5613<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5614<li><code>[out] loop</code>: The current libuv loop instance.</li>
5615</ul>
5616</section><section><h3>Asynchronous thread-safe function calls<span><a class="mark" href="#asynchronous-thread-safe-function-calls" id="asynchronous-thread-safe-function-calls">#</a></span><a aria-hidden="true" class="legacy" id="n_api_asynchronous_thread_safe_function_calls"></a></h3>
5617<p>JavaScript functions can normally only be called from a native addon's main
5618thread. If an addon creates additional threads, then Node-API functions that
5619require a <code>napi_env</code>, <code>napi_value</code>, or <code>napi_ref</code> must not be called from those
5620threads.</p>
5621<p>When an addon has additional threads and JavaScript functions need to be invoked
5622based on the processing completed by those threads, those threads must
5623communicate with the addon's main thread so that the main thread can invoke the
5624JavaScript function on their behalf. The thread-safe function APIs provide an
5625easy way to do this.</p>
5626<p>These APIs provide the type <code>napi_threadsafe_function</code> as well as APIs to
5627create, destroy, and call objects of this type.
5628<code>napi_create_threadsafe_function()</code> creates a persistent reference to a
5629<code>napi_value</code> that holds a JavaScript function which can be called from multiple
5630threads. The calls happen asynchronously. This means that values with which the
5631JavaScript callback is to be called will be placed in a queue, and, for each
5632value in the queue, a call will eventually be made to the JavaScript function.</p>
5633<p>Upon creation of a <code>napi_threadsafe_function</code> a <code>napi_finalize</code> callback can be
5634provided. This callback will be invoked on the main thread when the thread-safe
5635function is about to be destroyed. It receives the context and the finalize data
5636given during construction, and provides an opportunity for cleaning up after the
5637threads e.g. by calling <code>uv_thread_join()</code>. <strong>Aside from the main loop thread,
5638no threads should be using the thread-safe function after the finalize callback
5639completes.</strong></p>
5640<p>The <code>context</code> given during the call to <code>napi_create_threadsafe_function()</code> can
5641be retrieved from any thread with a call to
5642<code>napi_get_threadsafe_function_context()</code>.</p>
5643<h4>Calling a thread-safe function<span><a class="mark" href="#calling-a-thread-safe-function" id="calling-a-thread-safe-function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_calling_a_thread_safe_function"></a></h4>
5644<p><code>napi_call_threadsafe_function()</code> can be used for initiating a call into
5645JavaScript. <code>napi_call_threadsafe_function()</code> accepts a parameter which controls
5646whether the API behaves blockingly. If set to <code>napi_tsfn_nonblocking</code>, the API
5647behaves non-blockingly, returning <code>napi_queue_full</code> if the queue was full,
5648preventing data from being successfully added to the queue. If set to
5649<code>napi_tsfn_blocking</code>, the API blocks until space becomes available in the queue.
5650<code>napi_call_threadsafe_function()</code> never blocks if the thread-safe function was
5651created with a maximum queue size of 0.</p>
5652<p><code>napi_call_threadsafe_function()</code> should not be called with <code>napi_tsfn_blocking</code>
5653from a JavaScript thread, because, if the queue is full, it may cause the
5654JavaScript thread to deadlock.</p>
5655<p>The actual call into JavaScript is controlled by the callback given via the
5656<code>call_js_cb</code> parameter. <code>call_js_cb</code> is invoked on the main thread once for each
5657value that was placed into the queue by a successful call to
5658<code>napi_call_threadsafe_function()</code>. If such a callback is not given, a default
5659callback will be used, and the resulting JavaScript call will have no arguments.
5660The <code>call_js_cb</code> callback receives the JavaScript function to call as a
5661<code>napi_value</code> in its parameters, as well as the <code>void*</code> context pointer used when
5662creating the <code>napi_threadsafe_function</code>, and the next data pointer that was
5663created by one of the secondary threads. The callback can then use an API such
5664as <code>napi_call_function()</code> to call into JavaScript.</p>
5665<p>The callback may also be invoked with <code>env</code> and <code>call_js_cb</code> both set to <code>NULL</code>
5666to indicate that calls into JavaScript are no longer possible, while items
5667remain in the queue that may need to be freed. This normally occurs when the
5668Node.js process exits while there is a thread-safe function still active.</p>
5669<p>It is not necessary to call into JavaScript via <code>napi_make_callback()</code> because
5670Node-API runs <code>call_js_cb</code> in a context appropriate for callbacks.</p>
5671<p>Zero or more queued items may be invoked in each tick of the event loop.
5672Applications should not depend on a specific behavior other than progress in
5673invoking callbacks will be made and events will be invoked
5674as time moves forward.</p>
5675<h4>Reference counting of thread-safe functions<span><a class="mark" href="#reference-counting-of-thread-safe-functions" id="reference-counting-of-thread-safe-functions">#</a></span><a aria-hidden="true" class="legacy" id="n_api_reference_counting_of_thread_safe_functions"></a></h4>
5676<p>Threads can be added to and removed from a <code>napi_threadsafe_function</code> object
5677during its existence. Thus, in addition to specifying an initial number of
5678threads upon creation, <code>napi_acquire_threadsafe_function</code> can be called to
5679indicate that a new thread will start making use of the thread-safe function.
5680Similarly, <code>napi_release_threadsafe_function</code> can be called to indicate that an
5681existing thread will stop making use of the thread-safe function.</p>
5682<p><code>napi_threadsafe_function</code> objects are destroyed when every thread which uses
5683the object has called <code>napi_release_threadsafe_function()</code> or has received a
5684return status of <code>napi_closing</code> in response to a call to
5685<code>napi_call_threadsafe_function</code>. The queue is emptied before the
5686<code>napi_threadsafe_function</code> is destroyed. <code>napi_release_threadsafe_function()</code>
5687should be the last API call made in conjunction with a given
5688<code>napi_threadsafe_function</code>, because after the call completes, there is no
5689guarantee that the <code>napi_threadsafe_function</code> is still allocated. For the same
5690reason, do not use a thread-safe function
5691after receiving a return value of <code>napi_closing</code> in response to a call to
5692<code>napi_call_threadsafe_function</code>. Data associated with the
5693<code>napi_threadsafe_function</code> can be freed in its <code>napi_finalize</code> callback which
5694was passed to <code>napi_create_threadsafe_function()</code>. The parameter
5695<code>initial_thread_count</code> of <code>napi_create_threadsafe_function</code> marks the initial
5696number of acquisitions of the thread-safe functions, instead of calling
5697<code>napi_acquire_threadsafe_function</code> multiple times at creation.</p>
5698<p>Once the number of threads making use of a <code>napi_threadsafe_function</code> reaches
5699zero, no further threads can start making use of it by calling
5700<code>napi_acquire_threadsafe_function()</code>. In fact, all subsequent API calls
5701associated with it, except <code>napi_release_threadsafe_function()</code>, will return an
5702error value of <code>napi_closing</code>.</p>
5703<p>The thread-safe function can be "aborted" by giving a value of <code>napi_tsfn_abort</code>
5704to <code>napi_release_threadsafe_function()</code>. This will cause all subsequent APIs
5705associated with the thread-safe function except
5706<code>napi_release_threadsafe_function()</code> to return <code>napi_closing</code> even before its
5707reference count reaches zero. In particular, <code>napi_call_threadsafe_function()</code>
5708will return <code>napi_closing</code>, thus informing the threads that it is no longer
5709possible to make asynchronous calls to the thread-safe function. This can be
5710used as a criterion for terminating the thread. <strong>Upon receiving a return value
5711of <code>napi_closing</code> from <code>napi_call_threadsafe_function()</code> a thread must not use
5712the thread-safe function anymore because it is no longer guaranteed to
5713be allocated.</strong></p>
5714<h4>Deciding whether to keep the process running<span><a class="mark" href="#deciding-whether-to-keep-the-process-running" id="deciding-whether-to-keep-the-process-running">#</a></span><a aria-hidden="true" class="legacy" id="n_api_deciding_whether_to_keep_the_process_running"></a></h4>
5715<p>Similarly to libuv handles, thread-safe functions can be "referenced" and
5716"unreferenced". A "referenced" thread-safe function will cause the event loop on
5717the thread on which it is created to remain alive until the thread-safe function
5718is destroyed. In contrast, an "unreferenced" thread-safe function will not
5719prevent the event loop from exiting. The APIs <code>napi_ref_threadsafe_function</code> and
5720<code>napi_unref_threadsafe_function</code> exist for this purpose.</p>
5721<p>Neither does <code>napi_unref_threadsafe_function</code> mark the thread-safe functions as
5722able to be destroyed nor does <code>napi_ref_threadsafe_function</code> prevent it from
5723being destroyed.</p>
5724<h4><code>napi_create_threadsafe_function</code><span><a class="mark" href="#napi_create_threadsafe_function" id="napi_create_threadsafe_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_threadsafe_function"></a></h4>
5725<div class="api_metadata">
5726<details class="changelog"><summary>History</summary>
5727<table>
5728<tbody><tr><th>Version</th><th>Changes</th></tr>
5729<tr><td>v12.6.0, v10.17.0</td>
5730<td><p>Made <code>func</code> parameter optional with custom <code>call_js_cb</code>.</p></td></tr>
5731<tr><td>v10.6.0</td>
5732<td><p><span>Added in: v10.6.0</span></p></td></tr>
5733</tbody></table>
5734</details>
5735<span>N-API version: 4</span>
5736</div>
5737<pre><code class="language-c">NAPI_EXTERN napi_status
5738<span class="hljs-title function_">napi_create_threadsafe_function</span><span class="hljs-params">(napi_env env,
5739                                napi_value func,
5740                                napi_value async_resource,
5741                                napi_value async_resource_name,
5742                                <span class="hljs-type">size_t</span> max_queue_size,
5743                                <span class="hljs-type">size_t</span> initial_thread_count,
5744                                <span class="hljs-type">void</span>* thread_finalize_data,
5745                                napi_finalize thread_finalize_cb,
5746                                <span class="hljs-type">void</span>* context,
5747                                napi_threadsafe_function_call_js call_js_cb,
5748                                napi_threadsafe_function* result)</span>;</code> <button class="copy-button">copy</button></pre>
5749<ul>
5750<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5751<li><code>[in] func</code>: An optional JavaScript function to call from another thread. It
5752must be provided if <code>NULL</code> is passed to <code>call_js_cb</code>.</li>
5753<li><code>[in] async_resource</code>: An optional object associated with the async work that
5754will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#initasyncid-type-triggerasyncid-resource"><code>init</code> hooks</a>.</li>
5755<li><code>[in] async_resource_name</code>: A JavaScript string to provide an identifier for
5756the kind of resource that is being provided for diagnostic information exposed
5757by the <code>async_hooks</code> API.</li>
5758<li><code>[in] max_queue_size</code>: Maximum size of the queue. <code>0</code> for no limit.</li>
5759<li><code>[in] initial_thread_count</code>: The initial number of acquisitions, i.e. the
5760initial number of threads, including the main thread, which will be making use
5761of this function.</li>
5762<li><code>[in] thread_finalize_data</code>: Optional data to be passed to <code>thread_finalize_cb</code>.</li>
5763<li><code>[in] thread_finalize_cb</code>: Optional function to call when the
5764<code>napi_threadsafe_function</code> is being destroyed.</li>
5765<li><code>[in] context</code>: Optional data to attach to the resulting
5766<code>napi_threadsafe_function</code>.</li>
5767<li><code>[in] call_js_cb</code>: Optional callback which calls the JavaScript function in
5768response to a call on a different thread. This callback will be called on the
5769main thread. If not given, the JavaScript function will be called with no
5770parameters and with <code>undefined</code> as its <code>this</code> value.
5771<a href="#napi_threadsafe_function_call_js"><code>napi_threadsafe_function_call_js</code></a> provides more details.</li>
5772<li><code>[out] result</code>: The asynchronous thread-safe JavaScript function.</li>
5773</ul>
5774<h4><code>napi_get_threadsafe_function_context</code><span><a class="mark" href="#napi_get_threadsafe_function_context" id="napi_get_threadsafe_function_context">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_threadsafe_function_context"></a></h4>
5775<div class="api_metadata">
5776<span>Added in: v10.6.0</span>
5777<span>N-API version: 4</span>
5778</div>
5779<pre><code class="language-c">NAPI_EXTERN napi_status
5780<span class="hljs-title function_">napi_get_threadsafe_function_context</span><span class="hljs-params">(napi_threadsafe_function func,
5781                                     <span class="hljs-type">void</span>** result)</span>;</code> <button class="copy-button">copy</button></pre>
5782<ul>
5783<li><code>[in] func</code>: The thread-safe function for which to retrieve the context.</li>
5784<li><code>[out] result</code>: The location where to store the context.</li>
5785</ul>
5786<p>This API may be called from any thread which makes use of <code>func</code>.</p>
5787<h4><code>napi_call_threadsafe_function</code><span><a class="mark" href="#napi_call_threadsafe_function" id="napi_call_threadsafe_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_call_threadsafe_function"></a></h4>
5788<div class="api_metadata">
5789<details class="changelog"><summary>History</summary>
5790<table>
5791<tbody><tr><th>Version</th><th>Changes</th></tr>
5792<tr><td>v14.5.0</td>
5793<td><p>Support for <code>napi_would_deadlock</code> has been reverted.</p></td></tr>
5794<tr><td>v14.1.0</td>
5795<td><p>Return <code>napi_would_deadlock</code> when called with <code>napi_tsfn_blocking</code> from the main thread or a worker thread and the queue is full.</p></td></tr>
5796<tr><td>v10.6.0</td>
5797<td><p><span>Added in: v10.6.0</span></p></td></tr>
5798</tbody></table>
5799</details>
5800<span>N-API version: 4</span>
5801</div>
5802<pre><code class="language-c">NAPI_EXTERN napi_status
5803<span class="hljs-title function_">napi_call_threadsafe_function</span><span class="hljs-params">(napi_threadsafe_function func,
5804                              <span class="hljs-type">void</span>* data,
5805                              napi_threadsafe_function_call_mode is_blocking)</span>;</code> <button class="copy-button">copy</button></pre>
5806<ul>
5807<li><code>[in] func</code>: The asynchronous thread-safe JavaScript function to invoke.</li>
5808<li><code>[in] data</code>: Data to send into JavaScript via the callback <code>call_js_cb</code>
5809provided during the creation of the thread-safe JavaScript function.</li>
5810<li><code>[in] is_blocking</code>: Flag whose value can be either <code>napi_tsfn_blocking</code> to
5811indicate that the call should block if the queue is full or
5812<code>napi_tsfn_nonblocking</code> to indicate that the call should return immediately
5813with a status of <code>napi_queue_full</code> whenever the queue is full.</li>
5814</ul>
5815<p>This API should not be called with <code>napi_tsfn_blocking</code> from a JavaScript
5816thread, because, if the queue is full, it may cause the JavaScript thread to
5817deadlock.</p>
5818<p>This API will return <code>napi_closing</code> if <code>napi_release_threadsafe_function()</code> was
5819called with <code>abort</code> set to <code>napi_tsfn_abort</code> from any thread. The value is only
5820added to the queue if the API returns <code>napi_ok</code>.</p>
5821<p>This API may be called from any thread which makes use of <code>func</code>.</p>
5822<h4><code>napi_acquire_threadsafe_function</code><span><a class="mark" href="#napi_acquire_threadsafe_function" id="napi_acquire_threadsafe_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_acquire_threadsafe_function"></a></h4>
5823<div class="api_metadata">
5824<span>Added in: v10.6.0</span>
5825<span>N-API version: 4</span>
5826</div>
5827<pre><code class="language-c">NAPI_EXTERN napi_status
5828<span class="hljs-title function_">napi_acquire_threadsafe_function</span><span class="hljs-params">(napi_threadsafe_function func)</span>;</code> <button class="copy-button">copy</button></pre>
5829<ul>
5830<li><code>[in] func</code>: The asynchronous thread-safe JavaScript function to start making
5831use of.</li>
5832</ul>
5833<p>A thread should call this API before passing <code>func</code> to any other thread-safe
5834function APIs to indicate that it will be making use of <code>func</code>. This prevents
5835<code>func</code> from being destroyed when all other threads have stopped making use of
5836it.</p>
5837<p>This API may be called from any thread which will start making use of <code>func</code>.</p>
5838<h4><code>napi_release_threadsafe_function</code><span><a class="mark" href="#napi_release_threadsafe_function" id="napi_release_threadsafe_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_release_threadsafe_function"></a></h4>
5839<div class="api_metadata">
5840<span>Added in: v10.6.0</span>
5841<span>N-API version: 4</span>
5842</div>
5843<pre><code class="language-c">NAPI_EXTERN napi_status
5844<span class="hljs-title function_">napi_release_threadsafe_function</span><span class="hljs-params">(napi_threadsafe_function func,
5845                                 napi_threadsafe_function_release_mode mode)</span>;</code> <button class="copy-button">copy</button></pre>
5846<ul>
5847<li><code>[in] func</code>: The asynchronous thread-safe JavaScript function whose reference
5848count to decrement.</li>
5849<li><code>[in] mode</code>: Flag whose value can be either <code>napi_tsfn_release</code> to indicate
5850that the current thread will make no further calls to the thread-safe
5851function, or <code>napi_tsfn_abort</code> to indicate that in addition to the current
5852thread, no other thread should make any further calls to the thread-safe
5853function. If set to <code>napi_tsfn_abort</code>, further calls to
5854<code>napi_call_threadsafe_function()</code> will return <code>napi_closing</code>, and no further
5855values will be placed in the queue.</li>
5856</ul>
5857<p>A thread should call this API when it stops making use of <code>func</code>. Passing <code>func</code>
5858to any thread-safe APIs after having called this API has undefined results, as
5859<code>func</code> may have been destroyed.</p>
5860<p>This API may be called from any thread which will stop making use of <code>func</code>.</p>
5861<h4><code>napi_ref_threadsafe_function</code><span><a class="mark" href="#napi_ref_threadsafe_function" id="napi_ref_threadsafe_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_ref_threadsafe_function"></a></h4>
5862<div class="api_metadata">
5863<span>Added in: v10.6.0</span>
5864<span>N-API version: 4</span>
5865</div>
5866<pre><code class="language-c">NAPI_EXTERN napi_status
5867<span class="hljs-title function_">napi_ref_threadsafe_function</span><span class="hljs-params">(napi_env env, napi_threadsafe_function func)</span>;</code> <button class="copy-button">copy</button></pre>
5868<ul>
5869<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5870<li><code>[in] func</code>: The thread-safe function to reference.</li>
5871</ul>
5872<p>This API is used to indicate that the event loop running on the main thread
5873should not exit until <code>func</code> has been destroyed. Similar to <a href="https://docs.libuv.org/en/v1.x/handle.html#c.uv_ref"><code>uv_ref</code></a> it is
5874also idempotent.</p>
5875<p>Neither does <code>napi_unref_threadsafe_function</code> mark the thread-safe functions as
5876able to be destroyed nor does <code>napi_ref_threadsafe_function</code> prevent it from
5877being destroyed. <code>napi_acquire_threadsafe_function</code> and
5878<code>napi_release_threadsafe_function</code> are available for that purpose.</p>
5879<p>This API may only be called from the main thread.</p>
5880<h4><code>napi_unref_threadsafe_function</code><span><a class="mark" href="#napi_unref_threadsafe_function" id="napi_unref_threadsafe_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_unref_threadsafe_function"></a></h4>
5881<div class="api_metadata">
5882<span>Added in: v10.6.0</span>
5883<span>N-API version: 4</span>
5884</div>
5885<pre><code class="language-c">NAPI_EXTERN napi_status
5886<span class="hljs-title function_">napi_unref_threadsafe_function</span><span class="hljs-params">(napi_env env, napi_threadsafe_function func)</span>;</code> <button class="copy-button">copy</button></pre>
5887<ul>
5888<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5889<li><code>[in] func</code>: The thread-safe function to unreference.</li>
5890</ul>
5891<p>This API is used to indicate that the event loop running on the main thread
5892may exit before <code>func</code> is destroyed. Similar to <a href="https://docs.libuv.org/en/v1.x/handle.html#c.uv_unref"><code>uv_unref</code></a> it is also
5893idempotent.</p>
5894<p>This API may only be called from the main thread.</p>
5895</section><section><h3>Miscellaneous utilities<span><a class="mark" href="#miscellaneous-utilities" id="miscellaneous-utilities">#</a></span><a aria-hidden="true" class="legacy" id="n_api_miscellaneous_utilities"></a></h3>
5896<h4><code>node_api_get_module_file_name</code><span><a class="mark" href="#node_api_get_module_file_name" id="node_api_get_module_file_name">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_get_module_file_name"></a></h4>
5897<div class="api_metadata">
5898<span>Added in: v15.9.0, v14.18.0, v12.22.0</span>
5899<span>N-API version: 9</span>
5900</div>
5901<pre><code class="language-c">NAPI_EXTERN napi_status
5902<span class="hljs-title function_">node_api_get_module_file_name</span><span class="hljs-params">(napi_env env, <span class="hljs-type">const</span> <span class="hljs-type">char</span>** result)</span>;
5903</code> <button class="copy-button">copy</button></pre>
5904<ul>
5905<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5906<li><code>[out] result</code>: A URL containing the absolute path of the
5907location from which the add-on was loaded. For a file on the local
5908file system it will start with <code>file://</code>. The string is null-terminated and
5909owned by <code>env</code> and must thus not be modified or freed.</li>
5910</ul>
5911<p><code>result</code> may be an empty string if the add-on loading process fails to establish
5912the add-on's file name during loading.</p></section>
5913        <!-- API END -->
5914      </div>
5915    </div>
5916  </div>
5917</body>
5918</html>
5919