• 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.20.1">
7  <title>Node-API | Node.js v18.20.1 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.20.1 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.20.1</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><span class="stability_1"><a href="#node_api_nogc_env"><code>node_api_nogc_env</code></a></span></li>
157<li><a href="#napi_value"><code>napi_value</code></a></li>
158<li><a href="#napi_threadsafe_function"><code>napi_threadsafe_function</code></a></li>
159<li><a href="#napi_threadsafe_function_release_mode"><code>napi_threadsafe_function_release_mode</code></a></li>
160<li><a href="#napi_threadsafe_function_call_mode"><code>napi_threadsafe_function_call_mode</code></a></li>
161<li><a href="#node-api-memory-management-types">Node-API memory management types</a>
162<ul>
163<li><a href="#napi_handle_scope"><code>napi_handle_scope</code></a></li>
164<li><a href="#napi_escapable_handle_scope"><code>napi_escapable_handle_scope</code></a></li>
165<li><a href="#napi_ref"><code>napi_ref</code></a></li>
166<li><a href="#napi_type_tag"><code>napi_type_tag</code></a></li>
167<li><a href="#napi_async_cleanup_hook_handle"><code>napi_async_cleanup_hook_handle</code></a></li>
168</ul>
169</li>
170<li><a href="#node-api-callback-types">Node-API callback types</a>
171<ul>
172<li><a href="#napi_callback_info"><code>napi_callback_info</code></a></li>
173<li><a href="#napi_callback"><code>napi_callback</code></a></li>
174<li><span class="stability_1"><a href="#node_api_nogc_finalize"><code>node_api_nogc_finalize</code></a></span></li>
175<li><a href="#napi_finalize"><code>napi_finalize</code></a></li>
176<li><a href="#napi_async_execute_callback"><code>napi_async_execute_callback</code></a></li>
177<li><a href="#napi_async_complete_callback"><code>napi_async_complete_callback</code></a></li>
178<li><a href="#napi_threadsafe_function_call_js"><code>napi_threadsafe_function_call_js</code></a></li>
179<li><a href="#napi_cleanup_hook"><code>napi_cleanup_hook</code></a></li>
180<li><a href="#napi_async_cleanup_hook"><code>napi_async_cleanup_hook</code></a></li>
181</ul>
182</li>
183</ul>
184</li>
185<li><a href="#error-handling">Error handling</a>
186<ul>
187<li><a href="#return-values">Return values</a>
188<ul>
189<li><a href="#napi_get_last_error_info"><code>napi_get_last_error_info</code></a></li>
190</ul>
191</li>
192<li><a href="#exceptions">Exceptions</a>
193<ul>
194<li><a href="#napi_throw"><code>napi_throw</code></a></li>
195<li><a href="#napi_throw_error"><code>napi_throw_error</code></a></li>
196<li><a href="#napi_throw_type_error"><code>napi_throw_type_error</code></a></li>
197<li><a href="#napi_throw_range_error"><code>napi_throw_range_error</code></a></li>
198<li><a href="#node_api_throw_syntax_error"><code>node_api_throw_syntax_error</code></a></li>
199<li><a href="#napi_is_error"><code>napi_is_error</code></a></li>
200<li><a href="#napi_create_error"><code>napi_create_error</code></a></li>
201<li><a href="#napi_create_type_error"><code>napi_create_type_error</code></a></li>
202<li><a href="#napi_create_range_error"><code>napi_create_range_error</code></a></li>
203<li><a href="#node_api_create_syntax_error"><code>node_api_create_syntax_error</code></a></li>
204<li><a href="#napi_get_and_clear_last_exception"><code>napi_get_and_clear_last_exception</code></a></li>
205<li><a href="#napi_is_exception_pending"><code>napi_is_exception_pending</code></a></li>
206<li><a href="#napi_fatal_exception"><code>napi_fatal_exception</code></a></li>
207</ul>
208</li>
209<li><a href="#fatal-errors">Fatal errors</a>
210<ul>
211<li><a href="#napi_fatal_error"><code>napi_fatal_error</code></a></li>
212</ul>
213</li>
214</ul>
215</li>
216<li><a href="#object-lifetime-management">Object lifetime management</a>
217<ul>
218<li><a href="#making-handle-lifespan-shorter-than-that-of-the-native-method">Making handle lifespan shorter than that of the native method</a>
219<ul>
220<li><a href="#napi_open_handle_scope"><code>napi_open_handle_scope</code></a></li>
221<li><a href="#napi_close_handle_scope"><code>napi_close_handle_scope</code></a></li>
222<li><a href="#napi_open_escapable_handle_scope"><code>napi_open_escapable_handle_scope</code></a></li>
223<li><a href="#napi_close_escapable_handle_scope"><code>napi_close_escapable_handle_scope</code></a></li>
224<li><a href="#napi_escape_handle"><code>napi_escape_handle</code></a></li>
225</ul>
226</li>
227<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>
228<ul>
229<li><a href="#napi_create_reference"><code>napi_create_reference</code></a></li>
230<li><a href="#napi_delete_reference"><code>napi_delete_reference</code></a></li>
231<li><a href="#napi_reference_ref"><code>napi_reference_ref</code></a></li>
232<li><a href="#napi_reference_unref"><code>napi_reference_unref</code></a></li>
233<li><a href="#napi_get_reference_value"><code>napi_get_reference_value</code></a></li>
234</ul>
235</li>
236<li><a href="#cleanup-on-exit-of-the-current-nodejs-environment">Cleanup on exit of the current Node.js environment</a>
237<ul>
238<li><a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a></li>
239<li><a href="#napi_remove_env_cleanup_hook"><code>napi_remove_env_cleanup_hook</code></a></li>
240<li><a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a></li>
241<li><a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a></li>
242</ul>
243</li>
244<li><a href="#finalization-on-the-exit-of-the-nodejs-environment">Finalization on the exit of the Node.js environment</a></li>
245</ul>
246</li>
247<li><a href="#module-registration">Module registration</a></li>
248<li><a href="#working-with-javascript-values">Working with JavaScript values</a>
249<ul>
250<li><a href="#enum-types">Enum types</a>
251<ul>
252<li><a href="#napi_key_collection_mode"><code>napi_key_collection_mode</code></a></li>
253<li><a href="#napi_key_filter"><code>napi_key_filter</code></a></li>
254<li><a href="#napi_key_conversion"><code>napi_key_conversion</code></a></li>
255<li><a href="#napi_valuetype"><code>napi_valuetype</code></a></li>
256<li><a href="#napi_typedarray_type"><code>napi_typedarray_type</code></a></li>
257</ul>
258</li>
259<li><a href="#object-creation-functions">Object creation functions</a>
260<ul>
261<li><a href="#napi_create_array"><code>napi_create_array</code></a></li>
262<li><a href="#napi_create_array_with_length"><code>napi_create_array_with_length</code></a></li>
263<li><a href="#napi_create_arraybuffer"><code>napi_create_arraybuffer</code></a></li>
264<li><a href="#napi_create_buffer"><code>napi_create_buffer</code></a></li>
265<li><a href="#napi_create_buffer_copy"><code>napi_create_buffer_copy</code></a></li>
266<li><a href="#napi_create_date"><code>napi_create_date</code></a></li>
267<li><a href="#napi_create_external"><code>napi_create_external</code></a></li>
268<li><a href="#napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code></a></li>
269<li><a href="#napi_create_external_buffer"><code>napi_create_external_buffer</code></a></li>
270<li><a href="#napi_create_object"><code>napi_create_object</code></a></li>
271<li><a href="#napi_create_symbol"><code>napi_create_symbol</code></a></li>
272<li><a href="#node_api_symbol_for"><code>node_api_symbol_for</code></a></li>
273<li><a href="#napi_create_typedarray"><code>napi_create_typedarray</code></a></li>
274<li><a href="#napi_create_dataview"><code>napi_create_dataview</code></a></li>
275</ul>
276</li>
277<li><a href="#functions-to-convert-from-c-types-to-node-api">Functions to convert from C types to Node-API</a>
278<ul>
279<li><a href="#napi_create_int32"><code>napi_create_int32</code></a></li>
280<li><a href="#napi_create_uint32"><code>napi_create_uint32</code></a></li>
281<li><a href="#napi_create_int64"><code>napi_create_int64</code></a></li>
282<li><a href="#napi_create_double"><code>napi_create_double</code></a></li>
283<li><a href="#napi_create_bigint_int64"><code>napi_create_bigint_int64</code></a></li>
284<li><a href="#napi_create_bigint_uint64"><code>napi_create_bigint_uint64</code></a></li>
285<li><a href="#napi_create_bigint_words"><code>napi_create_bigint_words</code></a></li>
286<li><a href="#napi_create_string_latin1"><code>napi_create_string_latin1</code></a></li>
287<li><span class="stability_1"><a href="#node_api_create_external_string_latin1"><code>node_api_create_external_string_latin1</code></a></span></li>
288<li><a href="#napi_create_string_utf16"><code>napi_create_string_utf16</code></a></li>
289<li><span class="stability_1"><a href="#node_api_create_external_string_utf16"><code>node_api_create_external_string_utf16</code></a></span></li>
290<li><a href="#napi_create_string_utf8"><code>napi_create_string_utf8</code></a></li>
291</ul>
292</li>
293<li><a href="#functions-to-convert-from-node-api-to-c-types">Functions to convert from Node-API to C types</a>
294<ul>
295<li><a href="#napi_get_array_length"><code>napi_get_array_length</code></a></li>
296<li><a href="#napi_get_arraybuffer_info"><code>napi_get_arraybuffer_info</code></a></li>
297<li><a href="#napi_get_buffer_info"><code>napi_get_buffer_info</code></a></li>
298<li><a href="#napi_get_prototype"><code>napi_get_prototype</code></a></li>
299<li><a href="#napi_get_typedarray_info"><code>napi_get_typedarray_info</code></a></li>
300<li><a href="#napi_get_dataview_info"><code>napi_get_dataview_info</code></a></li>
301<li><a href="#napi_get_date_value"><code>napi_get_date_value</code></a></li>
302<li><a href="#napi_get_value_bool"><code>napi_get_value_bool</code></a></li>
303<li><a href="#napi_get_value_double"><code>napi_get_value_double</code></a></li>
304<li><a href="#napi_get_value_bigint_int64"><code>napi_get_value_bigint_int64</code></a></li>
305<li><a href="#napi_get_value_bigint_uint64"><code>napi_get_value_bigint_uint64</code></a></li>
306<li><a href="#napi_get_value_bigint_words"><code>napi_get_value_bigint_words</code></a></li>
307<li><a href="#napi_get_value_external"><code>napi_get_value_external</code></a></li>
308<li><a href="#napi_get_value_int32"><code>napi_get_value_int32</code></a></li>
309<li><a href="#napi_get_value_int64"><code>napi_get_value_int64</code></a></li>
310<li><a href="#napi_get_value_string_latin1"><code>napi_get_value_string_latin1</code></a></li>
311<li><a href="#napi_get_value_string_utf8"><code>napi_get_value_string_utf8</code></a></li>
312<li><a href="#napi_get_value_string_utf16"><code>napi_get_value_string_utf16</code></a></li>
313<li><a href="#napi_get_value_uint32"><code>napi_get_value_uint32</code></a></li>
314</ul>
315</li>
316<li><a href="#functions-to-get-global-instances">Functions to get global instances</a>
317<ul>
318<li><a href="#napi_get_boolean"><code>napi_get_boolean</code></a></li>
319<li><a href="#napi_get_global"><code>napi_get_global</code></a></li>
320<li><a href="#napi_get_null"><code>napi_get_null</code></a></li>
321<li><a href="#napi_get_undefined"><code>napi_get_undefined</code></a></li>
322</ul>
323</li>
324</ul>
325</li>
326<li><a href="#working-with-javascript-values-and-abstract-operations">Working with JavaScript values and abstract operations</a>
327<ul>
328<li><a href="#napi_coerce_to_bool"><code>napi_coerce_to_bool</code></a></li>
329<li><a href="#napi_coerce_to_number"><code>napi_coerce_to_number</code></a></li>
330<li><a href="#napi_coerce_to_object"><code>napi_coerce_to_object</code></a></li>
331<li><a href="#napi_coerce_to_string"><code>napi_coerce_to_string</code></a></li>
332<li><a href="#napi_typeof"><code>napi_typeof</code></a></li>
333<li><a href="#napi_instanceof"><code>napi_instanceof</code></a></li>
334<li><a href="#napi_is_array"><code>napi_is_array</code></a></li>
335<li><a href="#napi_is_arraybuffer"><code>napi_is_arraybuffer</code></a></li>
336<li><a href="#napi_is_buffer"><code>napi_is_buffer</code></a></li>
337<li><a href="#napi_is_date"><code>napi_is_date</code></a></li>
338<li><a href="#napi_is_error_1"><code>napi_is_error</code></a></li>
339<li><a href="#napi_is_typedarray"><code>napi_is_typedarray</code></a></li>
340<li><a href="#napi_is_dataview"><code>napi_is_dataview</code></a></li>
341<li><a href="#napi_strict_equals"><code>napi_strict_equals</code></a></li>
342<li><a href="#napi_detach_arraybuffer"><code>napi_detach_arraybuffer</code></a></li>
343<li><a href="#napi_is_detached_arraybuffer"><code>napi_is_detached_arraybuffer</code></a></li>
344</ul>
345</li>
346<li><a href="#working-with-javascript-properties">Working with JavaScript properties</a>
347<ul>
348<li><a href="#structures">Structures</a>
349<ul>
350<li><a href="#napi_property_attributes"><code>napi_property_attributes</code></a></li>
351<li><a href="#napi_property_descriptor"><code>napi_property_descriptor</code></a></li>
352</ul>
353</li>
354<li><a href="#functions">Functions</a>
355<ul>
356<li><a href="#napi_get_property_names"><code>napi_get_property_names</code></a></li>
357<li><a href="#napi_get_all_property_names"><code>napi_get_all_property_names</code></a></li>
358<li><a href="#napi_set_property"><code>napi_set_property</code></a></li>
359<li><a href="#napi_get_property"><code>napi_get_property</code></a></li>
360<li><a href="#napi_has_property"><code>napi_has_property</code></a></li>
361<li><a href="#napi_delete_property"><code>napi_delete_property</code></a></li>
362<li><a href="#napi_has_own_property"><code>napi_has_own_property</code></a></li>
363<li><a href="#napi_set_named_property"><code>napi_set_named_property</code></a></li>
364<li><a href="#napi_get_named_property"><code>napi_get_named_property</code></a></li>
365<li><a href="#napi_has_named_property"><code>napi_has_named_property</code></a></li>
366<li><a href="#napi_set_element"><code>napi_set_element</code></a></li>
367<li><a href="#napi_get_element"><code>napi_get_element</code></a></li>
368<li><a href="#napi_has_element"><code>napi_has_element</code></a></li>
369<li><a href="#napi_delete_element"><code>napi_delete_element</code></a></li>
370<li><a href="#napi_define_properties"><code>napi_define_properties</code></a></li>
371<li><a href="#napi_object_freeze"><code>napi_object_freeze</code></a></li>
372<li><a href="#napi_object_seal"><code>napi_object_seal</code></a></li>
373</ul>
374</li>
375</ul>
376</li>
377<li><a href="#working-with-javascript-functions">Working with JavaScript functions</a>
378<ul>
379<li><a href="#napi_call_function"><code>napi_call_function</code></a></li>
380<li><a href="#napi_create_function"><code>napi_create_function</code></a></li>
381<li><a href="#napi_get_cb_info"><code>napi_get_cb_info</code></a></li>
382<li><a href="#napi_get_new_target"><code>napi_get_new_target</code></a></li>
383<li><a href="#napi_new_instance"><code>napi_new_instance</code></a></li>
384</ul>
385</li>
386<li><a href="#object-wrap">Object wrap</a>
387<ul>
388<li><a href="#napi_define_class"><code>napi_define_class</code></a></li>
389<li><a href="#napi_wrap"><code>napi_wrap</code></a></li>
390<li><a href="#napi_unwrap"><code>napi_unwrap</code></a></li>
391<li><a href="#napi_remove_wrap"><code>napi_remove_wrap</code></a></li>
392<li><a href="#napi_type_tag_object"><code>napi_type_tag_object</code></a></li>
393<li><a href="#napi_check_object_type_tag"><code>napi_check_object_type_tag</code></a></li>
394<li><a href="#napi_add_finalizer"><code>napi_add_finalizer</code></a>
395<ul>
396<li><span class="stability_1"><a href="#node_api_post_finalizer"><code>node_api_post_finalizer</code></a></span></li>
397</ul>
398</li>
399</ul>
400</li>
401<li><a href="#simple-asynchronous-operations">Simple asynchronous operations</a>
402<ul>
403<li><a href="#napi_create_async_work"><code>napi_create_async_work</code></a></li>
404<li><a href="#napi_delete_async_work"><code>napi_delete_async_work</code></a></li>
405<li><a href="#napi_queue_async_work"><code>napi_queue_async_work</code></a></li>
406<li><a href="#napi_cancel_async_work"><code>napi_cancel_async_work</code></a></li>
407</ul>
408</li>
409<li><a href="#custom-asynchronous-operations">Custom asynchronous operations</a>
410<ul>
411<li><a href="#napi_async_init"><code>napi_async_init</code></a></li>
412<li><a href="#napi_async_destroy"><code>napi_async_destroy</code></a></li>
413<li><a href="#napi_make_callback"><code>napi_make_callback</code></a></li>
414<li><a href="#napi_open_callback_scope"><code>napi_open_callback_scope</code></a></li>
415<li><a href="#napi_close_callback_scope"><code>napi_close_callback_scope</code></a></li>
416</ul>
417</li>
418<li><a href="#version-management">Version management</a>
419<ul>
420<li><a href="#napi_get_node_version"><code>napi_get_node_version</code></a></li>
421<li><a href="#napi_get_version"><code>napi_get_version</code></a></li>
422</ul>
423</li>
424<li><a href="#memory-management">Memory management</a>
425<ul>
426<li><a href="#napi_adjust_external_memory"><code>napi_adjust_external_memory</code></a></li>
427</ul>
428</li>
429<li><a href="#promises">Promises</a>
430<ul>
431<li><a href="#napi_create_promise"><code>napi_create_promise</code></a></li>
432<li><a href="#napi_resolve_deferred"><code>napi_resolve_deferred</code></a></li>
433<li><a href="#napi_reject_deferred"><code>napi_reject_deferred</code></a></li>
434<li><a href="#napi_is_promise"><code>napi_is_promise</code></a></li>
435</ul>
436</li>
437<li><a href="#script-execution">Script execution</a>
438<ul>
439<li><a href="#napi_run_script"><code>napi_run_script</code></a></li>
440</ul>
441</li>
442<li><a href="#libuv-event-loop">libuv event loop</a>
443<ul>
444<li><a href="#napi_get_uv_event_loop"><code>napi_get_uv_event_loop</code></a></li>
445</ul>
446</li>
447<li><a href="#asynchronous-thread-safe-function-calls">Asynchronous thread-safe function calls</a>
448<ul>
449<li><a href="#calling-a-thread-safe-function">Calling a thread-safe function</a></li>
450<li><a href="#reference-counting-of-thread-safe-functions">Reference counting of thread-safe functions</a></li>
451<li><a href="#deciding-whether-to-keep-the-process-running">Deciding whether to keep the process running</a></li>
452<li><a href="#napi_create_threadsafe_function"><code>napi_create_threadsafe_function</code></a></li>
453<li><a href="#napi_get_threadsafe_function_context"><code>napi_get_threadsafe_function_context</code></a></li>
454<li><a href="#napi_call_threadsafe_function"><code>napi_call_threadsafe_function</code></a></li>
455<li><a href="#napi_acquire_threadsafe_function"><code>napi_acquire_threadsafe_function</code></a></li>
456<li><a href="#napi_release_threadsafe_function"><code>napi_release_threadsafe_function</code></a></li>
457<li><a href="#napi_ref_threadsafe_function"><code>napi_ref_threadsafe_function</code></a></li>
458<li><a href="#napi_unref_threadsafe_function"><code>napi_unref_threadsafe_function</code></a></li>
459</ul>
460</li>
461<li><a href="#miscellaneous-utilities">Miscellaneous utilities</a>
462<ul>
463<li><a href="#node_api_get_module_file_name"><code>node_api_get_module_file_name</code></a></li>
464</ul>
465</li>
466</ul>
467</li>
468</ul></div></div>
469    </li>
470
471
472    <li class="picker-header">
473      <a href="#">
474        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
475        Index
476      </a>
477
478      <div class="picker"><ul>
479<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
480<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
481
482      <li>
483        <a href="index.html">Index</a>
484      </li>
485    </ul>
486
487<hr class="line">
488<ul>
489<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
490<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
491<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
492<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
493<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
494<li><a href="n-api.html" class="nav-n-api active">C/C++ addons with Node-API</a></li>
495<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
496<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
497<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
498<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
499<li><a href="console.html" class="nav-console">Console</a></li>
500<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
501<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
502<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
503<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
504<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
505<li><a href="dns.html" class="nav-dns">DNS</a></li>
506<li><a href="domain.html" class="nav-domain">Domain</a></li>
507<li><a href="errors.html" class="nav-errors">Errors</a></li>
508<li><a href="events.html" class="nav-events">Events</a></li>
509<li><a href="fs.html" class="nav-fs">File system</a></li>
510<li><a href="globals.html" class="nav-globals">Globals</a></li>
511<li><a href="http.html" class="nav-http">HTTP</a></li>
512<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
513<li><a href="https.html" class="nav-https">HTTPS</a></li>
514<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
515<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
516<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
517<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
518<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
519<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
520<li><a href="net.html" class="nav-net">Net</a></li>
521<li><a href="os.html" class="nav-os">OS</a></li>
522<li><a href="path.html" class="nav-path">Path</a></li>
523<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
524<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
525<li><a href="process.html" class="nav-process">Process</a></li>
526<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
527<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
528<li><a href="readline.html" class="nav-readline">Readline</a></li>
529<li><a href="repl.html" class="nav-repl">REPL</a></li>
530<li><a href="report.html" class="nav-report">Report</a></li>
531<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
532<li><a href="stream.html" class="nav-stream">Stream</a></li>
533<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
534<li><a href="test.html" class="nav-test">Test runner</a></li>
535<li><a href="timers.html" class="nav-timers">Timers</a></li>
536<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
537<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
538<li><a href="tty.html" class="nav-tty">TTY</a></li>
539<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
540<li><a href="url.html" class="nav-url">URL</a></li>
541<li><a href="util.html" class="nav-util">Utilities</a></li>
542<li><a href="v8.html" class="nav-v8">V8</a></li>
543<li><a href="vm.html" class="nav-vm">VM</a></li>
544<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
545<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
546<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
547<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
548<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
549</ul>
550<hr class="line">
551<ul>
552<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
553</ul></div>
554    </li>
555
556
557    <li class="picker-header">
558      <a href="#">
559        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
560        Other versions
561      </a>
562      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/n-api.html">21.x</a></li>
563<li><a href="https://nodejs.org/docs/latest-v20.x/api/n-api.html">20.x <b>LTS</b></a></li>
564<li><a href="https://nodejs.org/docs/latest-v19.x/api/n-api.html">19.x</a></li>
565<li><a href="https://nodejs.org/docs/latest-v18.x/api/n-api.html">18.x <b>LTS</b></a></li>
566<li><a href="https://nodejs.org/docs/latest-v17.x/api/n-api.html">17.x</a></li>
567<li><a href="https://nodejs.org/docs/latest-v16.x/api/n-api.html">16.x</a></li>
568<li><a href="https://nodejs.org/docs/latest-v15.x/api/n-api.html">15.x</a></li>
569<li><a href="https://nodejs.org/docs/latest-v14.x/api/n-api.html">14.x</a></li>
570<li><a href="https://nodejs.org/docs/latest-v13.x/api/n-api.html">13.x</a></li>
571<li><a href="https://nodejs.org/docs/latest-v12.x/api/n-api.html">12.x</a></li>
572<li><a href="https://nodejs.org/docs/latest-v11.x/api/n-api.html">11.x</a></li>
573<li><a href="https://nodejs.org/docs/latest-v10.x/api/n-api.html">10.x</a></li>
574<li><a href="https://nodejs.org/docs/latest-v9.x/api/n-api.html">9.x</a></li>
575<li><a href="https://nodejs.org/docs/latest-v8.x/api/n-api.html">8.x</a></li></ol></div>
576    </li>
577
578            <li class="picker-header">
579              <a href="#">
580                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
581                Options
582              </a>
583
584              <div class="picker">
585                <ul>
586                  <li>
587                    <a href="all.html">View on single page</a>
588                  </li>
589                  <li>
590                    <a href="n-api.json">View as JSON</a>
591                  </li>
592                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/n-api.md">Edit on GitHub</a></li>
593                </ul>
594              </div>
595            </li>
596          </ul>
597        </div>
598        <hr>
599      </header>
600
601      <details id="toc" open><summary>Table of contents</summary><ul>
602<li><span class="stability_2"><a href="#node-api">Node-API</a></span>
603<ul>
604<li><a href="#implications-of-abi-stability">Implications of ABI stability</a></li>
605<li><a href="#building">Building</a>
606<ul>
607<li><a href="#build-tools">Build tools</a>
608<ul>
609<li><a href="#node-gyp">node-gyp</a></li>
610<li><a href="#cmakejs">CMake.js</a></li>
611</ul>
612</li>
613<li><a href="#uploading-precompiled-binaries">Uploading precompiled binaries</a>
614<ul>
615<li><a href="#node-pre-gyp">node-pre-gyp</a></li>
616<li><a href="#prebuild">prebuild</a></li>
617<li><a href="#prebuildify">prebuildify</a></li>
618</ul>
619</li>
620</ul>
621</li>
622<li><a href="#usage">Usage</a></li>
623<li><a href="#node-api-version-matrix">Node-API version matrix</a></li>
624<li><a href="#environment-life-cycle-apis">Environment life cycle APIs</a>
625<ul>
626<li><a href="#napi_set_instance_data"><code>napi_set_instance_data</code></a></li>
627<li><a href="#napi_get_instance_data"><code>napi_get_instance_data</code></a></li>
628</ul>
629</li>
630<li><a href="#basic-node-api-data-types">Basic Node-API data types</a>
631<ul>
632<li><a href="#napi_status"><code>napi_status</code></a></li>
633<li><a href="#napi_extended_error_info"><code>napi_extended_error_info</code></a></li>
634<li><a href="#napi_env"><code>napi_env</code></a></li>
635<li><span class="stability_1"><a href="#node_api_nogc_env"><code>node_api_nogc_env</code></a></span></li>
636<li><a href="#napi_value"><code>napi_value</code></a></li>
637<li><a href="#napi_threadsafe_function"><code>napi_threadsafe_function</code></a></li>
638<li><a href="#napi_threadsafe_function_release_mode"><code>napi_threadsafe_function_release_mode</code></a></li>
639<li><a href="#napi_threadsafe_function_call_mode"><code>napi_threadsafe_function_call_mode</code></a></li>
640<li><a href="#node-api-memory-management-types">Node-API memory management types</a>
641<ul>
642<li><a href="#napi_handle_scope"><code>napi_handle_scope</code></a></li>
643<li><a href="#napi_escapable_handle_scope"><code>napi_escapable_handle_scope</code></a></li>
644<li><a href="#napi_ref"><code>napi_ref</code></a></li>
645<li><a href="#napi_type_tag"><code>napi_type_tag</code></a></li>
646<li><a href="#napi_async_cleanup_hook_handle"><code>napi_async_cleanup_hook_handle</code></a></li>
647</ul>
648</li>
649<li><a href="#node-api-callback-types">Node-API callback types</a>
650<ul>
651<li><a href="#napi_callback_info"><code>napi_callback_info</code></a></li>
652<li><a href="#napi_callback"><code>napi_callback</code></a></li>
653<li><span class="stability_1"><a href="#node_api_nogc_finalize"><code>node_api_nogc_finalize</code></a></span></li>
654<li><a href="#napi_finalize"><code>napi_finalize</code></a></li>
655<li><a href="#napi_async_execute_callback"><code>napi_async_execute_callback</code></a></li>
656<li><a href="#napi_async_complete_callback"><code>napi_async_complete_callback</code></a></li>
657<li><a href="#napi_threadsafe_function_call_js"><code>napi_threadsafe_function_call_js</code></a></li>
658<li><a href="#napi_cleanup_hook"><code>napi_cleanup_hook</code></a></li>
659<li><a href="#napi_async_cleanup_hook"><code>napi_async_cleanup_hook</code></a></li>
660</ul>
661</li>
662</ul>
663</li>
664<li><a href="#error-handling">Error handling</a>
665<ul>
666<li><a href="#return-values">Return values</a>
667<ul>
668<li><a href="#napi_get_last_error_info"><code>napi_get_last_error_info</code></a></li>
669</ul>
670</li>
671<li><a href="#exceptions">Exceptions</a>
672<ul>
673<li><a href="#napi_throw"><code>napi_throw</code></a></li>
674<li><a href="#napi_throw_error"><code>napi_throw_error</code></a></li>
675<li><a href="#napi_throw_type_error"><code>napi_throw_type_error</code></a></li>
676<li><a href="#napi_throw_range_error"><code>napi_throw_range_error</code></a></li>
677<li><a href="#node_api_throw_syntax_error"><code>node_api_throw_syntax_error</code></a></li>
678<li><a href="#napi_is_error"><code>napi_is_error</code></a></li>
679<li><a href="#napi_create_error"><code>napi_create_error</code></a></li>
680<li><a href="#napi_create_type_error"><code>napi_create_type_error</code></a></li>
681<li><a href="#napi_create_range_error"><code>napi_create_range_error</code></a></li>
682<li><a href="#node_api_create_syntax_error"><code>node_api_create_syntax_error</code></a></li>
683<li><a href="#napi_get_and_clear_last_exception"><code>napi_get_and_clear_last_exception</code></a></li>
684<li><a href="#napi_is_exception_pending"><code>napi_is_exception_pending</code></a></li>
685<li><a href="#napi_fatal_exception"><code>napi_fatal_exception</code></a></li>
686</ul>
687</li>
688<li><a href="#fatal-errors">Fatal errors</a>
689<ul>
690<li><a href="#napi_fatal_error"><code>napi_fatal_error</code></a></li>
691</ul>
692</li>
693</ul>
694</li>
695<li><a href="#object-lifetime-management">Object lifetime management</a>
696<ul>
697<li><a href="#making-handle-lifespan-shorter-than-that-of-the-native-method">Making handle lifespan shorter than that of the native method</a>
698<ul>
699<li><a href="#napi_open_handle_scope"><code>napi_open_handle_scope</code></a></li>
700<li><a href="#napi_close_handle_scope"><code>napi_close_handle_scope</code></a></li>
701<li><a href="#napi_open_escapable_handle_scope"><code>napi_open_escapable_handle_scope</code></a></li>
702<li><a href="#napi_close_escapable_handle_scope"><code>napi_close_escapable_handle_scope</code></a></li>
703<li><a href="#napi_escape_handle"><code>napi_escape_handle</code></a></li>
704</ul>
705</li>
706<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>
707<ul>
708<li><a href="#napi_create_reference"><code>napi_create_reference</code></a></li>
709<li><a href="#napi_delete_reference"><code>napi_delete_reference</code></a></li>
710<li><a href="#napi_reference_ref"><code>napi_reference_ref</code></a></li>
711<li><a href="#napi_reference_unref"><code>napi_reference_unref</code></a></li>
712<li><a href="#napi_get_reference_value"><code>napi_get_reference_value</code></a></li>
713</ul>
714</li>
715<li><a href="#cleanup-on-exit-of-the-current-nodejs-environment">Cleanup on exit of the current Node.js environment</a>
716<ul>
717<li><a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a></li>
718<li><a href="#napi_remove_env_cleanup_hook"><code>napi_remove_env_cleanup_hook</code></a></li>
719<li><a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a></li>
720<li><a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a></li>
721</ul>
722</li>
723<li><a href="#finalization-on-the-exit-of-the-nodejs-environment">Finalization on the exit of the Node.js environment</a></li>
724</ul>
725</li>
726<li><a href="#module-registration">Module registration</a></li>
727<li><a href="#working-with-javascript-values">Working with JavaScript values</a>
728<ul>
729<li><a href="#enum-types">Enum types</a>
730<ul>
731<li><a href="#napi_key_collection_mode"><code>napi_key_collection_mode</code></a></li>
732<li><a href="#napi_key_filter"><code>napi_key_filter</code></a></li>
733<li><a href="#napi_key_conversion"><code>napi_key_conversion</code></a></li>
734<li><a href="#napi_valuetype"><code>napi_valuetype</code></a></li>
735<li><a href="#napi_typedarray_type"><code>napi_typedarray_type</code></a></li>
736</ul>
737</li>
738<li><a href="#object-creation-functions">Object creation functions</a>
739<ul>
740<li><a href="#napi_create_array"><code>napi_create_array</code></a></li>
741<li><a href="#napi_create_array_with_length"><code>napi_create_array_with_length</code></a></li>
742<li><a href="#napi_create_arraybuffer"><code>napi_create_arraybuffer</code></a></li>
743<li><a href="#napi_create_buffer"><code>napi_create_buffer</code></a></li>
744<li><a href="#napi_create_buffer_copy"><code>napi_create_buffer_copy</code></a></li>
745<li><a href="#napi_create_date"><code>napi_create_date</code></a></li>
746<li><a href="#napi_create_external"><code>napi_create_external</code></a></li>
747<li><a href="#napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code></a></li>
748<li><a href="#napi_create_external_buffer"><code>napi_create_external_buffer</code></a></li>
749<li><a href="#napi_create_object"><code>napi_create_object</code></a></li>
750<li><a href="#napi_create_symbol"><code>napi_create_symbol</code></a></li>
751<li><a href="#node_api_symbol_for"><code>node_api_symbol_for</code></a></li>
752<li><a href="#napi_create_typedarray"><code>napi_create_typedarray</code></a></li>
753<li><a href="#napi_create_dataview"><code>napi_create_dataview</code></a></li>
754</ul>
755</li>
756<li><a href="#functions-to-convert-from-c-types-to-node-api">Functions to convert from C types to Node-API</a>
757<ul>
758<li><a href="#napi_create_int32"><code>napi_create_int32</code></a></li>
759<li><a href="#napi_create_uint32"><code>napi_create_uint32</code></a></li>
760<li><a href="#napi_create_int64"><code>napi_create_int64</code></a></li>
761<li><a href="#napi_create_double"><code>napi_create_double</code></a></li>
762<li><a href="#napi_create_bigint_int64"><code>napi_create_bigint_int64</code></a></li>
763<li><a href="#napi_create_bigint_uint64"><code>napi_create_bigint_uint64</code></a></li>
764<li><a href="#napi_create_bigint_words"><code>napi_create_bigint_words</code></a></li>
765<li><a href="#napi_create_string_latin1"><code>napi_create_string_latin1</code></a></li>
766<li><span class="stability_1"><a href="#node_api_create_external_string_latin1"><code>node_api_create_external_string_latin1</code></a></span></li>
767<li><a href="#napi_create_string_utf16"><code>napi_create_string_utf16</code></a></li>
768<li><span class="stability_1"><a href="#node_api_create_external_string_utf16"><code>node_api_create_external_string_utf16</code></a></span></li>
769<li><a href="#napi_create_string_utf8"><code>napi_create_string_utf8</code></a></li>
770</ul>
771</li>
772<li><a href="#functions-to-convert-from-node-api-to-c-types">Functions to convert from Node-API to C types</a>
773<ul>
774<li><a href="#napi_get_array_length"><code>napi_get_array_length</code></a></li>
775<li><a href="#napi_get_arraybuffer_info"><code>napi_get_arraybuffer_info</code></a></li>
776<li><a href="#napi_get_buffer_info"><code>napi_get_buffer_info</code></a></li>
777<li><a href="#napi_get_prototype"><code>napi_get_prototype</code></a></li>
778<li><a href="#napi_get_typedarray_info"><code>napi_get_typedarray_info</code></a></li>
779<li><a href="#napi_get_dataview_info"><code>napi_get_dataview_info</code></a></li>
780<li><a href="#napi_get_date_value"><code>napi_get_date_value</code></a></li>
781<li><a href="#napi_get_value_bool"><code>napi_get_value_bool</code></a></li>
782<li><a href="#napi_get_value_double"><code>napi_get_value_double</code></a></li>
783<li><a href="#napi_get_value_bigint_int64"><code>napi_get_value_bigint_int64</code></a></li>
784<li><a href="#napi_get_value_bigint_uint64"><code>napi_get_value_bigint_uint64</code></a></li>
785<li><a href="#napi_get_value_bigint_words"><code>napi_get_value_bigint_words</code></a></li>
786<li><a href="#napi_get_value_external"><code>napi_get_value_external</code></a></li>
787<li><a href="#napi_get_value_int32"><code>napi_get_value_int32</code></a></li>
788<li><a href="#napi_get_value_int64"><code>napi_get_value_int64</code></a></li>
789<li><a href="#napi_get_value_string_latin1"><code>napi_get_value_string_latin1</code></a></li>
790<li><a href="#napi_get_value_string_utf8"><code>napi_get_value_string_utf8</code></a></li>
791<li><a href="#napi_get_value_string_utf16"><code>napi_get_value_string_utf16</code></a></li>
792<li><a href="#napi_get_value_uint32"><code>napi_get_value_uint32</code></a></li>
793</ul>
794</li>
795<li><a href="#functions-to-get-global-instances">Functions to get global instances</a>
796<ul>
797<li><a href="#napi_get_boolean"><code>napi_get_boolean</code></a></li>
798<li><a href="#napi_get_global"><code>napi_get_global</code></a></li>
799<li><a href="#napi_get_null"><code>napi_get_null</code></a></li>
800<li><a href="#napi_get_undefined"><code>napi_get_undefined</code></a></li>
801</ul>
802</li>
803</ul>
804</li>
805<li><a href="#working-with-javascript-values-and-abstract-operations">Working with JavaScript values and abstract operations</a>
806<ul>
807<li><a href="#napi_coerce_to_bool"><code>napi_coerce_to_bool</code></a></li>
808<li><a href="#napi_coerce_to_number"><code>napi_coerce_to_number</code></a></li>
809<li><a href="#napi_coerce_to_object"><code>napi_coerce_to_object</code></a></li>
810<li><a href="#napi_coerce_to_string"><code>napi_coerce_to_string</code></a></li>
811<li><a href="#napi_typeof"><code>napi_typeof</code></a></li>
812<li><a href="#napi_instanceof"><code>napi_instanceof</code></a></li>
813<li><a href="#napi_is_array"><code>napi_is_array</code></a></li>
814<li><a href="#napi_is_arraybuffer"><code>napi_is_arraybuffer</code></a></li>
815<li><a href="#napi_is_buffer"><code>napi_is_buffer</code></a></li>
816<li><a href="#napi_is_date"><code>napi_is_date</code></a></li>
817<li><a href="#napi_is_error_1"><code>napi_is_error</code></a></li>
818<li><a href="#napi_is_typedarray"><code>napi_is_typedarray</code></a></li>
819<li><a href="#napi_is_dataview"><code>napi_is_dataview</code></a></li>
820<li><a href="#napi_strict_equals"><code>napi_strict_equals</code></a></li>
821<li><a href="#napi_detach_arraybuffer"><code>napi_detach_arraybuffer</code></a></li>
822<li><a href="#napi_is_detached_arraybuffer"><code>napi_is_detached_arraybuffer</code></a></li>
823</ul>
824</li>
825<li><a href="#working-with-javascript-properties">Working with JavaScript properties</a>
826<ul>
827<li><a href="#structures">Structures</a>
828<ul>
829<li><a href="#napi_property_attributes"><code>napi_property_attributes</code></a></li>
830<li><a href="#napi_property_descriptor"><code>napi_property_descriptor</code></a></li>
831</ul>
832</li>
833<li><a href="#functions">Functions</a>
834<ul>
835<li><a href="#napi_get_property_names"><code>napi_get_property_names</code></a></li>
836<li><a href="#napi_get_all_property_names"><code>napi_get_all_property_names</code></a></li>
837<li><a href="#napi_set_property"><code>napi_set_property</code></a></li>
838<li><a href="#napi_get_property"><code>napi_get_property</code></a></li>
839<li><a href="#napi_has_property"><code>napi_has_property</code></a></li>
840<li><a href="#napi_delete_property"><code>napi_delete_property</code></a></li>
841<li><a href="#napi_has_own_property"><code>napi_has_own_property</code></a></li>
842<li><a href="#napi_set_named_property"><code>napi_set_named_property</code></a></li>
843<li><a href="#napi_get_named_property"><code>napi_get_named_property</code></a></li>
844<li><a href="#napi_has_named_property"><code>napi_has_named_property</code></a></li>
845<li><a href="#napi_set_element"><code>napi_set_element</code></a></li>
846<li><a href="#napi_get_element"><code>napi_get_element</code></a></li>
847<li><a href="#napi_has_element"><code>napi_has_element</code></a></li>
848<li><a href="#napi_delete_element"><code>napi_delete_element</code></a></li>
849<li><a href="#napi_define_properties"><code>napi_define_properties</code></a></li>
850<li><a href="#napi_object_freeze"><code>napi_object_freeze</code></a></li>
851<li><a href="#napi_object_seal"><code>napi_object_seal</code></a></li>
852</ul>
853</li>
854</ul>
855</li>
856<li><a href="#working-with-javascript-functions">Working with JavaScript functions</a>
857<ul>
858<li><a href="#napi_call_function"><code>napi_call_function</code></a></li>
859<li><a href="#napi_create_function"><code>napi_create_function</code></a></li>
860<li><a href="#napi_get_cb_info"><code>napi_get_cb_info</code></a></li>
861<li><a href="#napi_get_new_target"><code>napi_get_new_target</code></a></li>
862<li><a href="#napi_new_instance"><code>napi_new_instance</code></a></li>
863</ul>
864</li>
865<li><a href="#object-wrap">Object wrap</a>
866<ul>
867<li><a href="#napi_define_class"><code>napi_define_class</code></a></li>
868<li><a href="#napi_wrap"><code>napi_wrap</code></a></li>
869<li><a href="#napi_unwrap"><code>napi_unwrap</code></a></li>
870<li><a href="#napi_remove_wrap"><code>napi_remove_wrap</code></a></li>
871<li><a href="#napi_type_tag_object"><code>napi_type_tag_object</code></a></li>
872<li><a href="#napi_check_object_type_tag"><code>napi_check_object_type_tag</code></a></li>
873<li><a href="#napi_add_finalizer"><code>napi_add_finalizer</code></a>
874<ul>
875<li><span class="stability_1"><a href="#node_api_post_finalizer"><code>node_api_post_finalizer</code></a></span></li>
876</ul>
877</li>
878</ul>
879</li>
880<li><a href="#simple-asynchronous-operations">Simple asynchronous operations</a>
881<ul>
882<li><a href="#napi_create_async_work"><code>napi_create_async_work</code></a></li>
883<li><a href="#napi_delete_async_work"><code>napi_delete_async_work</code></a></li>
884<li><a href="#napi_queue_async_work"><code>napi_queue_async_work</code></a></li>
885<li><a href="#napi_cancel_async_work"><code>napi_cancel_async_work</code></a></li>
886</ul>
887</li>
888<li><a href="#custom-asynchronous-operations">Custom asynchronous operations</a>
889<ul>
890<li><a href="#napi_async_init"><code>napi_async_init</code></a></li>
891<li><a href="#napi_async_destroy"><code>napi_async_destroy</code></a></li>
892<li><a href="#napi_make_callback"><code>napi_make_callback</code></a></li>
893<li><a href="#napi_open_callback_scope"><code>napi_open_callback_scope</code></a></li>
894<li><a href="#napi_close_callback_scope"><code>napi_close_callback_scope</code></a></li>
895</ul>
896</li>
897<li><a href="#version-management">Version management</a>
898<ul>
899<li><a href="#napi_get_node_version"><code>napi_get_node_version</code></a></li>
900<li><a href="#napi_get_version"><code>napi_get_version</code></a></li>
901</ul>
902</li>
903<li><a href="#memory-management">Memory management</a>
904<ul>
905<li><a href="#napi_adjust_external_memory"><code>napi_adjust_external_memory</code></a></li>
906</ul>
907</li>
908<li><a href="#promises">Promises</a>
909<ul>
910<li><a href="#napi_create_promise"><code>napi_create_promise</code></a></li>
911<li><a href="#napi_resolve_deferred"><code>napi_resolve_deferred</code></a></li>
912<li><a href="#napi_reject_deferred"><code>napi_reject_deferred</code></a></li>
913<li><a href="#napi_is_promise"><code>napi_is_promise</code></a></li>
914</ul>
915</li>
916<li><a href="#script-execution">Script execution</a>
917<ul>
918<li><a href="#napi_run_script"><code>napi_run_script</code></a></li>
919</ul>
920</li>
921<li><a href="#libuv-event-loop">libuv event loop</a>
922<ul>
923<li><a href="#napi_get_uv_event_loop"><code>napi_get_uv_event_loop</code></a></li>
924</ul>
925</li>
926<li><a href="#asynchronous-thread-safe-function-calls">Asynchronous thread-safe function calls</a>
927<ul>
928<li><a href="#calling-a-thread-safe-function">Calling a thread-safe function</a></li>
929<li><a href="#reference-counting-of-thread-safe-functions">Reference counting of thread-safe functions</a></li>
930<li><a href="#deciding-whether-to-keep-the-process-running">Deciding whether to keep the process running</a></li>
931<li><a href="#napi_create_threadsafe_function"><code>napi_create_threadsafe_function</code></a></li>
932<li><a href="#napi_get_threadsafe_function_context"><code>napi_get_threadsafe_function_context</code></a></li>
933<li><a href="#napi_call_threadsafe_function"><code>napi_call_threadsafe_function</code></a></li>
934<li><a href="#napi_acquire_threadsafe_function"><code>napi_acquire_threadsafe_function</code></a></li>
935<li><a href="#napi_release_threadsafe_function"><code>napi_release_threadsafe_function</code></a></li>
936<li><a href="#napi_ref_threadsafe_function"><code>napi_ref_threadsafe_function</code></a></li>
937<li><a href="#napi_unref_threadsafe_function"><code>napi_unref_threadsafe_function</code></a></li>
938</ul>
939</li>
940<li><a href="#miscellaneous-utilities">Miscellaneous utilities</a>
941<ul>
942<li><a href="#node_api_get_module_file_name"><code>node_api_get_module_file_name</code></a></li>
943</ul>
944</li>
945</ul>
946</li>
947</ul></details>
948
949      <div id="apicontent">
950        <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>
951
952
953<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
954<p>Node-API (formerly N-API) is an API for building native Addons. It is
955independent from the underlying JavaScript runtime (for example, V8) and is
956maintained as part of Node.js itself. This API will be Application Binary
957Interface (ABI) stable across versions of Node.js. It is intended to insulate
958addons from changes in the underlying JavaScript engine and allow modules
959compiled for one major version to run on later major versions of Node.js without
960recompilation. The <a href="https://nodejs.org/en/docs/guides/abi-stability/">ABI Stability</a> guide provides a more in-depth explanation.</p>
961<p>Addons are built/packaged with the same approach/tools outlined in the section
962titled <a href="addons.html">C++ Addons</a>. The only difference is the set of APIs that are used by
963the native code. Instead of using the V8 or <a href="https://github.com/nodejs/nan">Native Abstractions for Node.js</a>
964APIs, the functions available in Node-API are used.</p>
965<p>APIs exposed by Node-API are generally used to create and manipulate
966JavaScript values. Concepts and operations generally map to ideas specified
967in the ECMA-262 Language Specification. The APIs have the following
968properties:</p>
969<ul>
970<li>All Node-API calls return a status code of type <code>napi_status</code>. This
971status indicates whether the API call succeeded or failed.</li>
972<li>The API's return value is passed via an out parameter.</li>
973<li>All JavaScript values are abstracted behind an opaque type named
974<code>napi_value</code>.</li>
975<li>In case of an error status code, additional information can be obtained
976using <code>napi_get_last_error_info</code>. More information can be found in the error
977handling section <a href="#error-handling">Error handling</a>.</li>
978</ul>
979<p>Node-API is a C API that ensures ABI stability across Node.js versions
980and different compiler levels. A C++ API can be easier to use.
981To support using C++, the project maintains a
982C++ wrapper module called <a href="https://github.com/nodejs/node-addon-api"><code>node-addon-api</code></a>.
983This wrapper provides an inlinable C++ API. Binaries built
984with <code>node-addon-api</code> will depend on the symbols for the Node-API C-based
985functions exported by Node.js. <code>node-addon-api</code> is a more
986efficient way to write code that calls Node-API. Take, for example, the
987following <code>node-addon-api</code> code. The first section shows the
988<code>node-addon-api</code> code and the second section shows what actually gets
989used in the addon.</p>
990<pre><code class="language-cpp">Object obj = Object::<span class="hljs-built_in">New</span>(env);
991obj[<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>
992<pre><code class="language-cpp">napi_status status;
993napi_value object, string;
994status = <span class="hljs-built_in">napi_create_object</span>(env, &#x26;object);
995<span class="hljs-keyword">if</span> (status != napi_ok) {
996  <span class="hljs-built_in">napi_throw_error</span>(env, ...);
997  <span class="hljs-keyword">return</span>;
998}
999
1000status = <span class="hljs-built_in">napi_create_string_utf8</span>(env, <span class="hljs-string">"bar"</span>, NAPI_AUTO_LENGTH, &#x26;string);
1001<span class="hljs-keyword">if</span> (status != napi_ok) {
1002  <span class="hljs-built_in">napi_throw_error</span>(env, ...);
1003  <span class="hljs-keyword">return</span>;
1004}
1005
1006status = <span class="hljs-built_in">napi_set_named_property</span>(env, object, <span class="hljs-string">"foo"</span>, string);
1007<span class="hljs-keyword">if</span> (status != napi_ok) {
1008  <span class="hljs-built_in">napi_throw_error</span>(env, ...);
1009  <span class="hljs-keyword">return</span>;
1010}</code> <button class="copy-button">copy</button></pre>
1011<p>The end result is that the addon only uses the exported C APIs. As a result,
1012it still gets the benefits of the ABI stability provided by the C API.</p>
1013<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>
1014for <code>node-addon-api</code>.</p>
1015<p>The <a href="https://nodejs.github.io/node-addon-examples/">Node-API Resource</a> offers
1016an excellent orientation and tips for developers just getting started with
1017Node-API and <code>node-addon-api</code>. Additional media resources can be found on the
1018<a href="https://github.com/nodejs/abi-stable-node/blob/HEAD/node-api-media.md">Node-API Media</a> page.</p>
1019<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>
1020<p>Although Node-API provides an ABI stability guarantee, other parts of Node.js do
1021not, and any external libraries used from the addon may not. In particular,
1022none of the following APIs provide an ABI stability guarantee across major
1023versions:</p>
1024<ul>
1025<li>
1026<p>the Node.js C++ APIs available via any of</p>
1027<pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node.h></span></span>
1028<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_buffer.h></span></span>
1029<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_version.h></span></span>
1030<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>
1031</li>
1032<li>
1033<p>the libuv APIs which are also included with Node.js and available via</p>
1034<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>
1035</li>
1036<li>
1037<p>the V8 API available via</p>
1038<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>
1039</li>
1040</ul>
1041<p>Thus, for an addon to remain ABI-compatible across Node.js major versions, it
1042must use Node-API exclusively by restricting itself to using</p>
1043<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>
1044<p>and by checking, for all external libraries that it uses, that the external
1045library makes ABI stability guarantees similar to Node-API.</p>
1046</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>
1047<p>Unlike modules written in JavaScript, developing and deploying Node.js
1048native addons using Node-API requires an additional set of tools. Besides the
1049basic tools required to develop for Node.js, the native addon developer
1050requires a toolchain that can compile C and C++ code into a binary. In
1051addition, depending upon how the native addon is deployed, the <em>user</em> of
1052the native addon will also need to have a C/C++ toolchain installed.</p>
1053<p>For Linux developers, the necessary C/C++ toolchain packages are readily
1054available. <a href="https://gcc.gnu.org">GCC</a> is widely used in the Node.js community to build and
1055test across a variety of platforms. For many developers, the <a href="https://llvm.org">LLVM</a>
1056compiler infrastructure is also a good choice.</p>
1057<p>For Mac developers, <a href="https://developer.apple.com/xcode/">Xcode</a> offers all the required compiler tools.
1058However, it is not necessary to install the entire Xcode IDE. The following
1059command installs the necessary toolchain:</p>
1060<pre><code class="language-bash">xcode-select --install</code> <button class="copy-button">copy</button></pre>
1061<p>For Windows developers, <a href="https://visualstudio.microsoft.com">Visual Studio</a> offers all the required compiler
1062tools. However, it is not necessary to install the entire Visual Studio
1063IDE. The following command installs the necessary toolchain:</p>
1064<pre><code class="language-bash">npm install --global windows-build-tools</code> <button class="copy-button">copy</button></pre>
1065<p>The sections below describe the additional tools available for developing
1066and deploying Node.js native addons.</p>
1067<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>
1068<p>Both the tools listed here require that <em>users</em> of the native
1069addon have a C/C++ toolchain installed in order to successfully install
1070the native addon.</p>
1071<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>
1072<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
1073Google's <a href="https://gyp.gsrc.io">GYP</a> tool and comes bundled with npm. GYP, and therefore node-gyp,
1074requires that Python be installed.</p>
1075<p>Historically, node-gyp has been the tool of choice for building native
1076addons. It has widespread adoption and documentation. However, some
1077developers have run into limitations in node-gyp.</p>
1078<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>
1079<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>
1080<p>CMake.js is a good choice for projects that already use CMake or for
1081developers affected by limitations in node-gyp. <a href="https://github.com/nodejs/node-addon-examples/tree/main/build_with_cmake"><code>build_with_cmake</code></a> is an
1082example of a CMake-based native addon project.</p>
1083<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>
1084<p>The three tools listed here permit native addon developers and maintainers
1085to create and upload binaries to public or private servers. These tools are
1086typically integrated with CI/CD build systems like <a href="https://travis-ci.org">Travis CI</a> and
1087<a href="https://www.appveyor.com">AppVeyor</a> to build and upload binaries for a variety of platforms and
1088architectures. These binaries are then available for download by users who
1089do not need to have a C/C++ toolchain installed.</p>
1090<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>
1091<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
1092upload binaries to a server of the developer's choice. node-pre-gyp has
1093particularly good support for uploading binaries to Amazon S3.</p>
1094<h5>prebuild<span><a class="mark" href="#prebuild" id="prebuild">#</a></span><a aria-hidden="true" class="legacy" id="n_api_prebuild"></a></h5>
1095<p><a href="https://github.com/prebuild/prebuild">prebuild</a> is a tool that supports builds using either node-gyp or
1096CMake.js. Unlike node-pre-gyp which supports a variety of servers, prebuild
1097uploads 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
1098GitHub projects using CMake.js.</p>
1099<h5>prebuildify<span><a class="mark" href="#prebuildify" id="prebuildify">#</a></span><a aria-hidden="true" class="legacy" id="n_api_prebuildify"></a></h5>
1100<p><a href="https://github.com/prebuild/prebuildify">prebuildify</a> is a tool based on node-gyp. The advantage of prebuildify is
1101that the built binaries are bundled with the native addon when it's
1102uploaded to npm. The binaries are downloaded from npm and are immediately
1103available to the module user when the native addon is installed.</p>
1104</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>
1105<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
1106is located in the src directory in the node development tree:</p>
1107<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>
1108<p>This will opt into the default <code>NAPI_VERSION</code> for the given release of Node.js.
1109In order to ensure compatibility with specific versions of Node-API, the version
1110can be specified explicitly when including the header:</p>
1111<pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_VERSION 3</span>
1112<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>
1113<p>This restricts the Node-API surface to just the functionality that was available
1114in the specified (and earlier) versions.</p>
1115<p>Some of the Node-API surface is experimental and requires explicit opt-in:</p>
1116<pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_EXPERIMENTAL</span>
1117<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>
1118<p>In this case the entire API surface, including any experimental APIs, will be
1119available to the module code.</p>
1120<p>Occasionally, experimental features are introduced that affect already-released
1121and stable APIs. These features can be disabled by an opt-out:</p>
1122<pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_EXPERIMENTAL</span>
1123<span class="hljs-meta">#<span class="hljs-keyword">define</span> NODE_API_EXPERIMENTAL_<span class="hljs-string">&#x3C;FEATURE_NAME></span>_OPT_OUT</span>
1124<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>
1125<p>where <code>&#x3C;FEATURE_NAME></code> is the name of an experimental feature that affects both
1126experimental and stable APIs.</p>
1127</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>
1128<p>Node-API versions are additive and versioned independently from Node.js.
1129Version 4 is an extension to version 3 in that it has all of the APIs
1130from version 3 with some additions. This means that it is not necessary
1131to recompile for new versions of Node.js which are
1132listed as supporting a later version.</p>
1133<p>This table may not be up to date in older streams, the most up to date
1134information is in the latest API documentation in:
1135<a href="https://nodejs.org/docs/latest/api/n-api.html#node-api-version-matrix">Node-API version matrix</a></p>
1136<!-- For accessibility purposes, this table needs row headers. That means we
1137     can't do it in markdown. Hence, the raw HTML. -->
1138<table>
1139  <tbody><tr>
1140    <th>Node-API version</th>
1141    <th scope="col">Supported In</th>
1142  </tr>
1143  <tr>
1144    <th scope="row">9</th>
1145    <td>v18.17.0+, 20.3.0+, 21.0.0 and all later versions</td>
1146  </tr>
1147  <tr>
1148    <th scope="row">8</th>
1149    <td>v12.22.0+, v14.17.0+, v15.12.0+, 16.0.0 and all later versions</td>
1150  </tr>
1151  <tr>
1152    <th scope="row">7</th>
1153    <td>v10.23.0+, v12.19.0+, v14.12.0+, 15.0.0 and all later versions</td>
1154  </tr>
1155  <tr>
1156    <th scope="row">6</th>
1157    <td>v10.20.0+, v12.17.0+, 14.0.0 and all later versions</td>
1158  </tr>
1159  <tr>
1160    <th scope="row">5</th>
1161    <td>v10.17.0+, v12.11.0+, 13.0.0 and all later versions</td>
1162  </tr>
1163  <tr>
1164    <th scope="row">4</th>
1165    <td>v10.16.0+, v11.8.0+, 12.0.0 and all later versions</td>
1166  </tr>
1167
1168    <tr>
1169    <th scope="row">3</th>
1170    <td>v6.14.2*, 8.11.2+, v9.11.0+*, 10.0.0 and all later versions</td>
1171  </tr>
1172  <tr>
1173    <th scope="row">2</th>
1174    <td>v8.10.0+*, v9.3.0+*, 10.0.0 and all later versions</td>
1175  </tr>
1176  <tr>
1177    <th scope="row">1</th>
1178    <td>v8.6.0+**, v9.0.0+*, 10.0.0 and all later versions</td>
1179  </tr>
1180</tbody></table>
1181<p>* Node-API was experimental.</p>
1182<p>** Node.js 8.0.0 included Node-API as experimental. It was released as
1183Node-API version 1 but continued to evolve until Node.js 8.6.0. The API is
1184different in versions prior to Node.js 8.6.0. We recommend Node-API version 3 or
1185later.</p>
1186<p>Each API documented for Node-API will have a header named <code>added in:</code>, and APIs
1187which are stable will have the additional header <code>Node-API version:</code>.
1188APIs are directly usable when using a Node.js version which supports
1189the Node-API version shown in <code>Node-API version:</code> or higher.
1190When using a Node.js version that does not support the
1191<code>Node-API version:</code> listed or if there is no <code>Node-API version:</code> listed,
1192then the API will only be available if
1193<code>#define NAPI_EXPERIMENTAL</code> precedes the inclusion of <code>node_api.h</code>
1194or <code>js_native_api.h</code>. If an API appears not to be available on
1195a version of Node.js which is later than the one shown in <code>added in:</code> then
1196this is most likely the reason for the apparent absence.</p>
1197<p>The Node-APIs associated strictly with accessing ECMAScript features from native
1198code can be found separately in <code>js_native_api.h</code> and <code>js_native_api_types.h</code>.
1199The APIs defined in these headers are included in <code>node_api.h</code> and
1200<code>node_api_types.h</code>. The headers are structured in this way in order to allow
1201implementations of Node-API outside of Node.js. For those implementations the
1202Node.js specific APIs may not be applicable.</p>
1203<p>The Node.js-specific parts of an addon can be separated from the code that
1204exposes the actual functionality to the JavaScript environment so that the
1205latter may be used with multiple implementations of Node-API. In the example
1206below, <code>addon.c</code> and <code>addon.h</code> refer only to <code>js_native_api.h</code>. This ensures
1207that <code>addon.c</code> can be reused to compile against either the Node.js
1208implementation of Node-API or any implementation of Node-API outside of Node.js.</p>
1209<p><code>addon_node.c</code> is a separate file that contains the Node.js specific entry point
1210to the addon and which instantiates the addon by calling into <code>addon.c</code> when the
1211addon is loaded into a Node.js environment.</p>
1212<pre><code class="language-c"><span class="hljs-comment">// addon.h</span>
1213<span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> _ADDON_H_</span>
1214<span class="hljs-meta">#<span class="hljs-keyword">define</span> _ADDON_H_</span>
1215<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;js_native_api.h></span></span>
1216napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_env env)</span>;
1217<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>
1218<pre><code class="language-c"><span class="hljs-comment">// addon.c</span>
1219<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"addon.h"</span></span>
1220
1221<span class="hljs-meta">#<span class="hljs-keyword">define</span> NODE_API_CALL(env, call)                                  \
1222  do {                                                            \
1223    napi_status status = (call);                                  \
1224    <span class="hljs-keyword">if</span> (status != napi_ok) {                                      \
1225      const napi_extended_error_info* error_info = NULL;          \
1226      napi_get_last_error_info((env), &#x26;error_info);               \
1227      const char* err_message = error_info->error_message;        \
1228      bool is_pending;                                            \
1229      napi_is_exception_pending((env), &#x26;is_pending);              \
1230      <span class="hljs-comment">/* If an exception is already pending, don't rethrow it */</span>  \
1231      <span class="hljs-keyword">if</span> (!is_pending) {                                          \
1232        const char* message = (err_message == NULL)               \
1233            ? <span class="hljs-string">"empty error message"</span>                               \
1234            : err_message;                                        \
1235        napi_throw_error((env), NULL, message);                   \
1236      }                                                           \
1237      return NULL;                                                \
1238    }                                                             \
1239  } while(0)</span>
1240
1241<span class="hljs-type">static</span> napi_value
1242<span class="hljs-title function_">DoSomethingUseful</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> {
1243  <span class="hljs-comment">// Do something useful.</span>
1244  <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
1245}
1246
1247napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_env env)</span> {
1248  napi_value result;
1249  NODE_API_CALL(env, napi_create_object(env, &#x26;result));
1250
1251  napi_value exported_function;
1252  NODE_API_CALL(env, napi_create_function(env,
1253                                          <span class="hljs-string">"doSomethingUseful"</span>,
1254                                          NAPI_AUTO_LENGTH,
1255                                          DoSomethingUseful,
1256                                          <span class="hljs-literal">NULL</span>,
1257                                          &#x26;exported_function));
1258
1259  NODE_API_CALL(env, napi_set_named_property(env,
1260                                             result,
1261                                             <span class="hljs-string">"doSomethingUseful"</span>,
1262                                             exported_function));
1263
1264  <span class="hljs-keyword">return</span> result;
1265}</code> <button class="copy-button">copy</button></pre>
1266<pre><code class="language-c"><span class="hljs-comment">// addon_node.c</span>
1267<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_api.h></span></span>
1268<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"addon.h"</span></span>
1269
1270NAPI_MODULE_INIT(<span class="hljs-comment">/* napi_env env, napi_value exports */</span>) {
1271  <span class="hljs-comment">// This function body is expected to return a `napi_value`.</span>
1272  <span class="hljs-comment">// The variables `napi_env env` and `napi_value exports` may be used within</span>
1273  <span class="hljs-comment">// the body, as they are provided by the definition of `NAPI_MODULE_INIT()`.</span>
1274  <span class="hljs-keyword">return</span> create_addon(env);
1275}</code> <button class="copy-button">copy</button></pre>
1276</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>
1277<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
1278of an "Agent" as a self-contained environment in which JavaScript code runs.
1279Multiple such Agents may be started and terminated either concurrently or in
1280sequence by the process.</p>
1281<p>A Node.js environment corresponds to an ECMAScript Agent. In the main process,
1282an environment is created at startup, and additional environments can be created
1283on separate threads to serve as <a href="https://nodejs.org/api/worker_threads.html">worker threads</a>. When Node.js is embedded in
1284another application, the main thread of the application may also construct and
1285destroy a Node.js environment multiple times during the life cycle of the
1286application process such that each Node.js environment created by the
1287application may, in turn, during its life cycle create and destroy additional
1288environments as worker threads.</p>
1289<p>From the perspective of a native addon this means that the bindings it provides
1290may be called multiple times, from multiple contexts, and even concurrently from
1291multiple threads.</p>
1292<p>Native addons may need to allocate global state which they use during
1293their life cycle of an Node.js environment such that the state can be
1294unique to each instance of the addon.</p>
1295<p>To this end, Node-API provides a way to associate data such that its life cycle
1296is tied to the life cycle of a Node.js environment.</p>
1297<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>
1298<div class="api_metadata">
1299<span>Added in: v12.8.0, v10.20.0</span>
1300<span>N-API version: 6</span>
1301</div>
1302<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_instance_data</span><span class="hljs-params">(node_api_nogc_env env,
1303                                   <span class="hljs-type">void</span>* data,
1304                                   napi_finalize finalize_cb,
1305                                   <span class="hljs-type">void</span>* finalize_hint)</span>;</code> <button class="copy-button">copy</button></pre>
1306<ul>
1307<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
1308<li><code>[in] data</code>: The data item to make available to bindings of this instance.</li>
1309<li><code>[in] finalize_cb</code>: The function to call when the environment is being torn
1310down. The function receives <code>data</code> so that it might free it.
1311<a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
1312<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
1313collection.</li>
1314</ul>
1315<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1316<p>This API associates <code>data</code> with the currently running Node.js environment. <code>data</code>
1317can later be retrieved using <code>napi_get_instance_data()</code>. Any existing data
1318associated with the currently running Node.js environment which was set by means
1319of a previous call to <code>napi_set_instance_data()</code> will be overwritten. If a
1320<code>finalize_cb</code> was provided by the previous call, it will not be called.</p>
1321<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>
1322<div class="api_metadata">
1323<span>Added in: v12.8.0, v10.20.0</span>
1324<span>N-API version: 6</span>
1325</div>
1326<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_instance_data</span><span class="hljs-params">(node_api_nogc_env env,
1327                                   <span class="hljs-type">void</span>** data)</span>;</code> <button class="copy-button">copy</button></pre>
1328<ul>
1329<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
1330<li><code>[out] data</code>: The data item that was previously associated with the currently
1331running Node.js environment by a call to <code>napi_set_instance_data()</code>.</li>
1332</ul>
1333<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1334<p>This API retrieves data that was previously associated with the currently
1335running Node.js environment via <code>napi_set_instance_data()</code>. If no data is set,
1336the call will succeed and <code>data</code> will be set to <code>NULL</code>.</p>
1337</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>
1338<p>Node-API exposes the following fundamental data types as abstractions that are
1339consumed by the various APIs. These APIs should be treated as opaque,
1340introspectable only with other Node-API calls.</p>
1341<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>
1342<div class="api_metadata">
1343<span>Added in: v8.0.0</span>
1344<span>N-API version: 1</span>
1345</div>
1346<p>Integral status code indicating the success or failure of a Node-API call.
1347Currently, the following status codes are supported.</p>
1348<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
1349  napi_ok,
1350  napi_invalid_arg,
1351  napi_object_expected,
1352  napi_string_expected,
1353  napi_name_expected,
1354  napi_function_expected,
1355  napi_number_expected,
1356  napi_boolean_expected,
1357  napi_array_expected,
1358  napi_generic_failure,
1359  napi_pending_exception,
1360  napi_cancelled,
1361  napi_escape_called_twice,
1362  napi_handle_scope_mismatch,
1363  napi_callback_scope_mismatch,
1364  napi_queue_full,
1365  napi_closing,
1366  napi_bigint_expected,
1367  napi_date_expected,
1368  napi_arraybuffer_expected,
1369  napi_detachable_arraybuffer_expected,
1370  napi_would_deadlock,  <span class="hljs-comment">/* unused */</span>
1371  napi_no_external_buffers_allowed,
1372  napi_cannot_run_js
1373} napi_status;</code> <button class="copy-button">copy</button></pre>
1374<p>If additional information is required upon an API returning a failed status,
1375it can be obtained by calling <code>napi_get_last_error_info</code>.</p>
1376<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>
1377<div class="api_metadata">
1378<span>Added in: v8.0.0</span>
1379<span>N-API version: 1</span>
1380</div>
1381<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span>
1382  <span class="hljs-type">const</span> <span class="hljs-type">char</span>* error_message;
1383  <span class="hljs-type">void</span>* engine_reserved;
1384  <span class="hljs-type">uint32_t</span> engine_error_code;
1385  napi_status error_code;
1386} napi_extended_error_info;</code> <button class="copy-button">copy</button></pre>
1387<ul>
1388<li><code>error_message</code>: UTF8-encoded string containing a VM-neutral description of
1389the error.</li>
1390<li><code>engine_reserved</code>: Reserved for VM-specific error details. This is currently
1391not implemented for any VM.</li>
1392<li><code>engine_error_code</code>: VM-specific error code. This is currently
1393not implemented for any VM.</li>
1394<li><code>error_code</code>: The Node-API status code that originated with the last error.</li>
1395</ul>
1396<p>See the <a href="#error-handling">Error handling</a> section for additional information.</p>
1397<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>
1398<p><code>napi_env</code> is used to represent a context that the underlying Node-API
1399implementation can use to persist VM-specific state. This structure is passed
1400to native functions when they're invoked, and it must be passed back when
1401making Node-API calls. Specifically, the same <code>napi_env</code> that was passed in when
1402the initial native function was called must be passed to any subsequent
1403nested Node-API calls. Caching the <code>napi_env</code> for the purpose of general reuse,
1404and passing the <code>napi_env</code> between instances of the same addon running on
1405different <a href="worker_threads.html#class-worker"><code>Worker</code></a> threads is not allowed. The <code>napi_env</code> becomes invalid
1406when an instance of a native addon is unloaded. Notification of this event is
1407delivered through the callbacks given to <a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a> and
1408<a href="#napi_set_instance_data"><code>napi_set_instance_data</code></a>.</p>
1409<h4><code>node_api_nogc_env</code><span><a class="mark" href="#node_api_nogc_env" id="node_api_nogc_env">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_nogc_env"></a></h4>
1410<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
1411<p>This variant of <code>napi_env</code> is passed to synchronous finalizers
1412(<a href="#node_api_nogc_finalize"><code>node_api_nogc_finalize</code></a>). There is a subset of Node-APIs which accept
1413a parameter of type <code>node_api_nogc_env</code> as their first argument. These APIs do
1414not access the state of the JavaScript engine and are thus safe to call from
1415synchronous finalizers. Passing a parameter of type <code>napi_env</code> to these APIs is
1416allowed, however, passing a parameter of type <code>node_api_nogc_env</code> to APIs that
1417access the JavaScript engine state is not allowed. Attempting to do so without
1418a cast will produce a compiler warning or an error when add-ons are compiled
1419with flags which cause them to emit warnings and/or errors when incorrect
1420pointer types are passed into a function. Calling such APIs from a synchronous
1421finalizer will ultimately result in the termination of the application.</p>
1422<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>
1423<p>This is an opaque pointer that is used to represent a JavaScript value.</p>
1424<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>
1425<div class="api_metadata">
1426<span>Added in: v10.6.0</span>
1427<span>N-API version: 4</span>
1428</div>
1429<p>This is an opaque pointer that represents a JavaScript function which can be
1430called asynchronously from multiple threads via
1431<code>napi_call_threadsafe_function()</code>.</p>
1432<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>
1433<div class="api_metadata">
1434<span>Added in: v10.6.0</span>
1435<span>N-API version: 4</span>
1436</div>
1437<p>A value to be given to <code>napi_release_threadsafe_function()</code> to indicate whether
1438the thread-safe function is to be closed immediately (<code>napi_tsfn_abort</code>) or
1439merely released (<code>napi_tsfn_release</code>) and thus available for subsequent use via
1440<code>napi_acquire_threadsafe_function()</code> and <code>napi_call_threadsafe_function()</code>.</p>
1441<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
1442  napi_tsfn_release,
1443  napi_tsfn_abort
1444} napi_threadsafe_function_release_mode;</code> <button class="copy-button">copy</button></pre>
1445<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>
1446<div class="api_metadata">
1447<span>Added in: v10.6.0</span>
1448<span>N-API version: 4</span>
1449</div>
1450<p>A value to be given to <code>napi_call_threadsafe_function()</code> to indicate whether
1451the call should block whenever the queue associated with the thread-safe
1452function is full.</p>
1453<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
1454  napi_tsfn_nonblocking,
1455  napi_tsfn_blocking
1456} napi_threadsafe_function_call_mode;</code> <button class="copy-button">copy</button></pre>
1457<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>
1458<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>
1459<p>This is an abstraction used to control and modify the lifetime of objects
1460created within a particular scope. In general, Node-API values are created
1461within the context of a handle scope. When a native method is called from
1462JavaScript, a default handle scope will exist. If the user does not explicitly
1463create a new handle scope, Node-API values will be created in the default handle
1464scope. For any invocations of code outside the execution of a native method
1465(for instance, during a libuv callback invocation), the module is required to
1466create a scope before invoking any functions that can result in the creation
1467of JavaScript values.</p>
1468<p>Handle scopes are created using <a href="#napi_open_handle_scope"><code>napi_open_handle_scope</code></a> and are destroyed
1469using <a href="#napi_close_handle_scope"><code>napi_close_handle_scope</code></a>. Closing the scope can indicate to the GC
1470that all <code>napi_value</code>s created during the lifetime of the handle scope are no
1471longer referenced from the current stack frame.</p>
1472<p>For more details, review the <a href="#object-lifetime-management">Object lifetime management</a>.</p>
1473<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>
1474<div class="api_metadata">
1475<span>Added in: v8.0.0</span>
1476<span>N-API version: 1</span>
1477</div>
1478<p>Escapable handle scopes are a special type of handle scope to return values
1479created within a particular handle scope to a parent scope.</p>
1480<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>
1481<div class="api_metadata">
1482<span>Added in: v8.0.0</span>
1483<span>N-API version: 1</span>
1484</div>
1485<p>This is the abstraction to use to reference a <code>napi_value</code>. This allows for
1486users to manage the lifetimes of JavaScript values, including defining their
1487minimum lifetimes explicitly.</p>
1488<p>For more details, review the <a href="#object-lifetime-management">Object lifetime management</a>.</p>
1489<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>
1490<div class="api_metadata">
1491<span>Added in: v14.8.0, v12.19.0</span>
1492<span>N-API version: 8</span>
1493</div>
1494<p>A 128-bit value stored as two unsigned 64-bit integers. It serves as a UUID
1495with which JavaScript objects or <a href="#napi_create_external">externals</a> can be "tagged" in order to
1496ensure that they are of a certain type. This is a stronger check than
1497<a href="#napi_instanceof"><code>napi_instanceof</code></a>, because the latter can report a false positive if the
1498object's prototype has been manipulated. Type-tagging is most useful in
1499conjunction with <a href="#napi_wrap"><code>napi_wrap</code></a> because it ensures that the pointer retrieved
1500from a wrapped object can be safely cast to the native type corresponding to the
1501type tag that had been previously applied to the JavaScript object.</p>
1502<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span>
1503  <span class="hljs-type">uint64_t</span> lower;
1504  <span class="hljs-type">uint64_t</span> upper;
1505} napi_type_tag;</code> <button class="copy-button">copy</button></pre>
1506<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>
1507<div class="api_metadata">
1508<span>Added in: v14.10.0, v12.19.0</span>
1509</div>
1510<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
1511to <a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a> when the chain of asynchronous cleanup
1512events completes.</p>
1513<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>
1514<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>
1515<div class="api_metadata">
1516<span>Added in: v8.0.0</span>
1517<span>N-API version: 1</span>
1518</div>
1519<p>Opaque datatype that is passed to a callback function. It can be used for
1520getting additional information about the context in which the callback was
1521invoked.</p>
1522<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>
1523<div class="api_metadata">
1524<span>Added in: v8.0.0</span>
1525<span>N-API version: 1</span>
1526</div>
1527<p>Function pointer type for user-provided native functions which are to be
1528exposed to JavaScript via Node-API. Callback functions should satisfy the
1529following signature:</p>
1530<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>
1531<p>Unless for reasons discussed in <a href="#object-lifetime-management">Object Lifetime Management</a>, creating a
1532handle and/or callback scope inside a <code>napi_callback</code> is not necessary.</p>
1533<h5><code>node_api_nogc_finalize</code><span><a class="mark" href="#node_api_nogc_finalize" id="node_api_nogc_finalize">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_nogc_finalize"></a></h5>
1534<div class="api_metadata">
1535<span>Added in: v18.20.0</span>
1536</div>
1537<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
1538<p>Function pointer type for add-on provided functions that allow the user to be
1539notified when externally-owned data is ready to be cleaned up because the
1540object it was associated with has been garbage-collected. The user must provide
1541a function satisfying the following signature which would get called upon the
1542object's collection. Currently, <code>node_api_nogc_finalize</code> can be used for
1543finding out when objects that have external data are collected.</p>
1544<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*node_api_nogc_finalize)</span><span class="hljs-params">(node_api_nogc_env env,
1545                                      <span class="hljs-type">void</span>* finalize_data,
1546                                      <span class="hljs-type">void</span>* finalize_hint)</span>;</code> <button class="copy-button">copy</button></pre>
1547<p>Unless for reasons discussed in <a href="#object-lifetime-management">Object Lifetime Management</a>, creating a
1548handle and/or callback scope inside the function body is not necessary.</p>
1549<p>Since these functions may be called while the JavaScript engine is in a state
1550where it cannot execute JavaScript code, only Node-APIs which accept a
1551<code>node_api_nogc_env</code> as their first parameter may be called.
1552<a href="#node_api_post_finalizer"><code>node_api_post_finalizer</code></a> can be used to schedule Node-API calls that
1553require access to the JavaScript engine's state to run after the current
1554garbage collection cycle has completed.</p>
1555<p>In the case of <a href="#node_api_create_external_string_latin1"><code>node_api_create_external_string_latin1</code></a> and
1556<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,
1557because external strings can be collected during the latter part of environment
1558shutdown.</p>
1559<p>Change History:</p>
1560<ul>
1561<li>
1562<p>experimental (<code>NAPI_EXPERIMENTAL</code>):</p>
1563<p>Only Node-API calls that accept a <code>node_api_nogc_env</code> as their first
1564parameter may be called, otherwise the application will be terminated with an
1565appropriate error message. This feature can be turned off by defining
1566<code>NODE_API_EXPERIMENTAL_NOGC_ENV_OPT_OUT</code>.</p>
1567</li>
1568</ul>
1569<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>
1570<div class="api_metadata">
1571<span>Added in: v8.0.0</span>
1572<span>N-API version: 1</span>
1573</div>
1574<p>Function pointer type for add-on provided function that allow the user to
1575schedule a group of calls to Node-APIs in response to a garbage collection
1576event, after the garbage collection cycle has completed. These function
1577pointers can be used with <a href="#node_api_post_finalizer"><code>node_api_post_finalizer</code></a>.</p>
1578<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,
1579                              <span class="hljs-type">void</span>* finalize_data,
1580                              <span class="hljs-type">void</span>* finalize_hint)</span>;</code> <button class="copy-button">copy</button></pre>
1581<p>Change History:</p>
1582<ul>
1583<li>
1584<p>experimental (<code>NAPI_EXPERIMENTAL</code> is defined):</p>
1585<p>A function of this type may no longer be used as a finalizer, except with
1586<a href="#node_api_post_finalizer"><code>node_api_post_finalizer</code></a>. <a href="#node_api_nogc_finalize"><code>node_api_nogc_finalize</code></a> must be used
1587instead. This feature can be turned off by defining
1588<code>NODE_API_EXPERIMENTAL_NOGC_ENV_OPT_OUT</code>.</p>
1589</li>
1590</ul>
1591<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>
1592<div class="api_metadata">
1593<span>Added in: v8.0.0</span>
1594<span>N-API version: 1</span>
1595</div>
1596<p>Function pointer used with functions that support asynchronous
1597operations. Callback functions must satisfy the following signature:</p>
1598<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>
1599<p>Implementations of this function must avoid making Node-API calls that execute
1600JavaScript or interact with JavaScript objects. Node-API calls should be in the
1601<code>napi_async_complete_callback</code> instead. Do not use the <code>napi_env</code> parameter as
1602it will likely result in execution of JavaScript.</p>
1603<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>
1604<div class="api_metadata">
1605<span>Added in: v8.0.0</span>
1606<span>N-API version: 1</span>
1607</div>
1608<p>Function pointer used with functions that support asynchronous
1609operations. Callback functions must satisfy the following signature:</p>
1610<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,
1611                                             napi_status status,
1612                                             <span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button></pre>
1613<p>Unless for reasons discussed in <a href="#object-lifetime-management">Object Lifetime Management</a>, creating a
1614handle and/or callback scope inside the function body is not necessary.</p>
1615<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>
1616<div class="api_metadata">
1617<span>Added in: v10.6.0</span>
1618<span>N-API version: 4</span>
1619</div>
1620<p>Function pointer used with asynchronous thread-safe function calls. The callback
1621will be called on the main thread. Its purpose is to use a data item arriving
1622via the queue from one of the secondary threads to construct the parameters
1623necessary for a call into JavaScript, usually via <code>napi_call_function</code>, and then
1624make the call into JavaScript.</p>
1625<p>The data arriving from the secondary thread via the queue is given in the <code>data</code>
1626parameter and the JavaScript function to call is given in the <code>js_callback</code>
1627parameter.</p>
1628<p>Node-API sets up the environment prior to calling this callback, so it is
1629sufficient to call the JavaScript function via <code>napi_call_function</code> rather than
1630via <code>napi_make_callback</code>.</p>
1631<p>Callback functions must satisfy the following signature:</p>
1632<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,
1633                                                 napi_value js_callback,
1634                                                 <span class="hljs-type">void</span>* context,
1635                                                 <span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button></pre>
1636<ul>
1637<li><code>[in] env</code>: The environment to use for API calls, or <code>NULL</code> if the thread-safe
1638function is being torn down and <code>data</code> may need to be freed.</li>
1639<li><code>[in] js_callback</code>: The JavaScript function to call, or <code>NULL</code> if the
1640thread-safe function is being torn down and <code>data</code> may need to be freed. It
1641may also be <code>NULL</code> if the thread-safe function was created without
1642<code>js_callback</code>.</li>
1643<li><code>[in] context</code>: The optional data with which the thread-safe function was
1644created.</li>
1645<li><code>[in] data</code>: Data created by the secondary thread. It is the responsibility of
1646the callback to convert this native data to JavaScript values (with Node-API
1647functions) that can be passed as parameters when <code>js_callback</code> is invoked.
1648This pointer is managed entirely by the threads and this callback. Thus this
1649callback should free the data.</li>
1650</ul>
1651<p>Unless for reasons discussed in <a href="#object-lifetime-management">Object Lifetime Management</a>, creating a
1652handle and/or callback scope inside the function body is not necessary.</p>
1653<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>
1654<div class="api_metadata">
1655<span>Added in: v18.13.0</span>
1656<span>N-API version: 3</span>
1657</div>
1658<p>Function pointer used with <a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a>. It will be called
1659when the environment is being torn down.</p>
1660<p>Callback functions must satisfy the following signature:</p>
1661<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>
1662<ul>
1663<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>
1664</ul>
1665<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>
1666<div class="api_metadata">
1667<span>Added in: v14.10.0, v12.19.0</span>
1668</div>
1669<p>Function pointer used with <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>. It will be called
1670when the environment is being torn down.</p>
1671<p>Callback functions must satisfy the following signature:</p>
1672<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,
1673                                        <span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button></pre>
1674<ul>
1675<li><code>[in] handle</code>: The handle that must be passed to
1676<a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a> after completion of the asynchronous
1677cleanup.</li>
1678<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>
1679</ul>
1680<p>The body of the function should initiate the asynchronous cleanup actions at the
1681end of which <code>handle</code> must be passed in a call to
1682<a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a>.</p>
1683</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>
1684<p>Node-API uses both return values and JavaScript exceptions for error handling.
1685The following sections explain the approach for each case.</p>
1686<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>
1687<p>All of the Node-API functions share the same error handling pattern. The
1688return type of all API functions is <code>napi_status</code>.</p>
1689<p>The return value will be <code>napi_ok</code> if the request was successful and
1690no uncaught JavaScript exception was thrown. If an error occurred AND
1691an exception was thrown, the <code>napi_status</code> value for the error
1692will be returned. If an exception was thrown, and no error occurred,
1693<code>napi_pending_exception</code> will be returned.</p>
1694<p>In cases where a return value other than <code>napi_ok</code> or
1695<code>napi_pending_exception</code> is returned, <a href="#napi_is_exception_pending"><code>napi_is_exception_pending</code></a>
1696must be called to check if an exception is pending.
1697See the section on exceptions for more details.</p>
1698<p>The full set of possible <code>napi_status</code> values is defined
1699in <code>napi_api_types.h</code>.</p>
1700<p>The <code>napi_status</code> return value provides a VM-independent representation of
1701the error which occurred. In some cases it is useful to be able to get
1702more detailed information, including a string representing the error as well as
1703VM (engine)-specific information.</p>
1704<p>In order to retrieve this information <a href="#napi_get_last_error_info"><code>napi_get_last_error_info</code></a>
1705is provided which returns a <code>napi_extended_error_info</code> structure.
1706The format of the <code>napi_extended_error_info</code> structure is as follows:</p>
1707<div class="api_metadata">
1708<span>Added in: v8.0.0</span>
1709<span>N-API version: 1</span>
1710</div>
1711<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>
1712  <span class="hljs-type">const</span> <span class="hljs-type">char</span>* error_message;
1713  <span class="hljs-type">void</span>* engine_reserved;
1714  <span class="hljs-type">uint32_t</span> engine_error_code;
1715  napi_status error_code;
1716};</code> <button class="copy-button">copy</button></pre>
1717<ul>
1718<li><code>error_message</code>: Textual representation of the error that occurred.</li>
1719<li><code>engine_reserved</code>: Opaque handle reserved for engine use only.</li>
1720<li><code>engine_error_code</code>: VM specific error code.</li>
1721<li><code>error_code</code>: Node-API status code for the last error.</li>
1722</ul>
1723<p><a href="#napi_get_last_error_info"><code>napi_get_last_error_info</code></a> returns the information for the last
1724Node-API call that was made.</p>
1725<p>Do not rely on the content or format of any of the extended information as it
1726is not subject to SemVer and may change at any time. It is intended only for
1727logging purposes.</p>
1728<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>
1729<div class="api_metadata">
1730<span>Added in: v8.0.0</span>
1731<span>N-API version: 1</span>
1732</div>
1733<pre><code class="language-c">napi_status
1734<span class="hljs-title function_">napi_get_last_error_info</span><span class="hljs-params">(node_api_nogc_env env,
1735                         <span class="hljs-type">const</span> napi_extended_error_info** result)</span>;</code> <button class="copy-button">copy</button></pre>
1736<ul>
1737<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1738<li><code>[out] result</code>: The <code>napi_extended_error_info</code> structure with more
1739information about the error.</li>
1740</ul>
1741<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1742<p>This API retrieves a <code>napi_extended_error_info</code> structure with information
1743about the last error that occurred.</p>
1744<p>The content of the <code>napi_extended_error_info</code> returned is only valid up until
1745a Node-API function is called on the same <code>env</code>. This includes a call to
1746<code>napi_is_exception_pending</code> so it may often be necessary to make a copy
1747of the information so that it can be used later. The pointer returned
1748in <code>error_message</code> points to a statically-defined string so it is safe to use
1749that pointer if you have copied it out of the <code>error_message</code> field (which will
1750be overwritten) before another Node-API function was called.</p>
1751<p>Do not rely on the content or format of any of the extended information as it
1752is not subject to SemVer and may change at any time. It is intended only for
1753logging purposes.</p>
1754<p>This API can be called even if there is a pending JavaScript exception.</p>
1755<h4>Exceptions<span><a class="mark" href="#exceptions" id="exceptions">#</a></span><a aria-hidden="true" class="legacy" id="n_api_exceptions"></a></h4>
1756<p>Any Node-API function call may result in a pending JavaScript exception. This is
1757the case for any of the API functions, even those that may not cause the
1758execution of JavaScript.</p>
1759<p>If the <code>napi_status</code> returned by a function is <code>napi_ok</code> then no
1760exception is pending and no additional action is required. If the
1761<code>napi_status</code> returned is anything other than <code>napi_ok</code> or
1762<code>napi_pending_exception</code>, in order to try to recover and continue
1763instead of simply returning immediately, <a href="#napi_is_exception_pending"><code>napi_is_exception_pending</code></a>
1764must be called in order to determine if an exception is pending or not.</p>
1765<p>In many cases when a Node-API function is called and an exception is
1766already pending, the function will return immediately with a
1767<code>napi_status</code> of <code>napi_pending_exception</code>. However, this is not the case
1768for all functions. Node-API allows a subset of the functions to be
1769called to allow for some minimal cleanup before returning to JavaScript.
1770In that case, <code>napi_status</code> will reflect the status for the function. It
1771will not reflect previous pending exceptions. To avoid confusion, check
1772the error status after every function call.</p>
1773<p>When an exception is pending one of two approaches can be employed.</p>
1774<p>The first approach is to do any appropriate cleanup and then return so that
1775execution will return to JavaScript. As part of the transition back to
1776JavaScript, the exception will be thrown at the point in the JavaScript
1777code where the native method was invoked. The behavior of most Node-API calls
1778is unspecified while an exception is pending, and many will simply return
1779<code>napi_pending_exception</code>, so do as little as possible and then return to
1780JavaScript where the exception can be handled.</p>
1781<p>The second approach is to try to handle the exception. There will be cases
1782where the native code can catch the exception, take the appropriate action,
1783and then continue. This is only recommended in specific cases
1784where it is known that the exception can be safely handled. In these
1785cases <a href="#napi_get_and_clear_last_exception"><code>napi_get_and_clear_last_exception</code></a> can be used to get and
1786clear the exception. On success, result will contain the handle to
1787the last JavaScript <code>Object</code> thrown. If it is determined, after
1788retrieving the exception, the exception cannot be handled after all
1789it can be re-thrown it with <a href="#napi_throw"><code>napi_throw</code></a> where error is the
1790JavaScript value to be thrown.</p>
1791<p>The following utility functions are also available in case native code
1792needs to throw an exception or determine if a <code>napi_value</code> is an instance
1793of a JavaScript <code>Error</code> object: <a href="#napi_throw_error"><code>napi_throw_error</code></a>,
1794<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>
1795<p>The following utility functions are also available in case native
1796code needs to create an <code>Error</code> object: <a href="#napi_create_error"><code>napi_create_error</code></a>,
1797<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>,
1798where result is the <code>napi_value</code> that refers to the newly created
1799JavaScript <code>Error</code> object.</p>
1800<p>The Node.js project is adding error codes to all of the errors
1801generated internally. The goal is for applications to use these
1802error codes for all error checking. The associated error messages
1803will remain, but will only be meant to be used for logging and
1804display with the expectation that the message can change without
1805SemVer applying. In order to support this model with Node-API, both
1806in internal functionality and for module specific functionality
1807(as its good practice), the <code>throw_</code> and <code>create_</code> functions
1808take an optional code parameter which is the string for the code
1809to be added to the error object. If the optional parameter is <code>NULL</code>
1810then no code will be associated with the error. If a code is provided,
1811the name associated with the error is also updated to be:</p>
1812<pre><code class="language-text">originalName [code]</code> <button class="copy-button">copy</button></pre>
1813<p>where <code>originalName</code> is the original name associated with the error
1814and <code>code</code> is the code that was provided. For example, if the code
1815is <code>'ERR_ERROR_1'</code> and a <code>TypeError</code> is being created the name will be:</p>
1816<pre><code class="language-text">TypeError [ERR_ERROR_1]</code> <button class="copy-button">copy</button></pre>
1817<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>
1818<div class="api_metadata">
1819<span>Added in: v8.0.0</span>
1820<span>N-API version: 1</span>
1821</div>
1822<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>
1823<ul>
1824<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1825<li><code>[in] error</code>: The JavaScript value to be thrown.</li>
1826</ul>
1827<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1828<p>This API throws the JavaScript value provided.</p>
1829<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>
1830<div class="api_metadata">
1831<span>Added in: v8.0.0</span>
1832<span>N-API version: 1</span>
1833</div>
1834<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,
1835                                         <span class="hljs-type">const</span> <span class="hljs-type">char</span>* code,
1836                                         <span class="hljs-type">const</span> <span class="hljs-type">char</span>* msg)</span>;</code> <button class="copy-button">copy</button></pre>
1837<ul>
1838<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1839<li><code>[in] code</code>: Optional error code to be set on the error.</li>
1840<li><code>[in] msg</code>: C string representing the text to be associated with the error.</li>
1841</ul>
1842<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1843<p>This API throws a JavaScript <code>Error</code> with the text provided.</p>
1844<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>
1845<div class="api_metadata">
1846<span>Added in: v8.0.0</span>
1847<span>N-API version: 1</span>
1848</div>
1849<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,
1850                                              <span class="hljs-type">const</span> <span class="hljs-type">char</span>* code,
1851                                              <span class="hljs-type">const</span> <span class="hljs-type">char</span>* msg)</span>;</code> <button class="copy-button">copy</button></pre>
1852<ul>
1853<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1854<li><code>[in] code</code>: Optional error code to be set on the error.</li>
1855<li><code>[in] msg</code>: C string representing the text to be associated with the error.</li>
1856</ul>
1857<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1858<p>This API throws a JavaScript <code>TypeError</code> with the text provided.</p>
1859<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>
1860<div class="api_metadata">
1861<span>Added in: v8.0.0</span>
1862<span>N-API version: 1</span>
1863</div>
1864<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,
1865                                               <span class="hljs-type">const</span> <span class="hljs-type">char</span>* code,
1866                                               <span class="hljs-type">const</span> <span class="hljs-type">char</span>* msg)</span>;</code> <button class="copy-button">copy</button></pre>
1867<ul>
1868<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1869<li><code>[in] code</code>: Optional error code to be set on the error.</li>
1870<li><code>[in] msg</code>: C string representing the text to be associated with the error.</li>
1871</ul>
1872<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1873<p>This API throws a JavaScript <code>RangeError</code> with the text provided.</p>
1874<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>
1875<div class="api_metadata">
1876<span>Added in: v17.2.0, v16.14.0</span>
1877<span>N-API version: 9</span>
1878</div>
1879<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,
1880                                                    <span class="hljs-type">const</span> <span class="hljs-type">char</span>* code,
1881                                                    <span class="hljs-type">const</span> <span class="hljs-type">char</span>* msg)</span>;</code> <button class="copy-button">copy</button></pre>
1882<ul>
1883<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1884<li><code>[in] code</code>: Optional error code to be set on the error.</li>
1885<li><code>[in] msg</code>: C string representing the text to be associated with the error.</li>
1886</ul>
1887<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1888<p>This API throws a JavaScript <code>SyntaxError</code> with the text provided.</p>
1889<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>
1890<div class="api_metadata">
1891<span>Added in: v8.0.0</span>
1892<span>N-API version: 1</span>
1893</div>
1894<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,
1895                                      napi_value value,
1896                                      <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
1897<ul>
1898<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1899<li><code>[in] value</code>: The <code>napi_value</code> to be checked.</li>
1900<li><code>[out] result</code>: Boolean value that is set to true if <code>napi_value</code> represents
1901an error, false otherwise.</li>
1902</ul>
1903<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1904<p>This API queries a <code>napi_value</code> to check if it represents an error object.</p>
1905<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>
1906<div class="api_metadata">
1907<span>Added in: v8.0.0</span>
1908<span>N-API version: 1</span>
1909</div>
1910<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,
1911                                          napi_value code,
1912                                          napi_value msg,
1913                                          napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
1914<ul>
1915<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1916<li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be
1917associated with the error.</li>
1918<li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as
1919the message for the <code>Error</code>.</li>
1920<li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li>
1921</ul>
1922<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1923<p>This API returns a JavaScript <code>Error</code> with the text provided.</p>
1924<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>
1925<div class="api_metadata">
1926<span>Added in: v8.0.0</span>
1927<span>N-API version: 1</span>
1928</div>
1929<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,
1930                                               napi_value code,
1931                                               napi_value msg,
1932                                               napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
1933<ul>
1934<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1935<li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be
1936associated with the error.</li>
1937<li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as
1938the message for the <code>Error</code>.</li>
1939<li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li>
1940</ul>
1941<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1942<p>This API returns a JavaScript <code>TypeError</code> with the text provided.</p>
1943<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>
1944<div class="api_metadata">
1945<span>Added in: v8.0.0</span>
1946<span>N-API version: 1</span>
1947</div>
1948<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,
1949                                                napi_value code,
1950                                                napi_value msg,
1951                                                napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
1952<ul>
1953<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1954<li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be
1955associated with the error.</li>
1956<li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as
1957the message for the <code>Error</code>.</li>
1958<li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li>
1959</ul>
1960<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1961<p>This API returns a JavaScript <code>RangeError</code> with the text provided.</p>
1962<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>
1963<div class="api_metadata">
1964<span>Added in: v17.2.0, v16.14.0</span>
1965<span>N-API version: 9</span>
1966</div>
1967<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,
1968                                                     napi_value code,
1969                                                     napi_value msg,
1970                                                     napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
1971<ul>
1972<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1973<li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be
1974associated with the error.</li>
1975<li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as
1976the message for the <code>Error</code>.</li>
1977<li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li>
1978</ul>
1979<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1980<p>This API returns a JavaScript <code>SyntaxError</code> with the text provided.</p>
1981<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>
1982<div class="api_metadata">
1983<span>Added in: v8.0.0</span>
1984<span>N-API version: 1</span>
1985</div>
1986<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,
1987                                              napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
1988<ul>
1989<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1990<li><code>[out] result</code>: The exception if one is pending, <code>NULL</code> otherwise.</li>
1991</ul>
1992<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1993<p>This API can be called even if there is a pending JavaScript exception.</p>
1994<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>
1995<div class="api_metadata">
1996<span>Added in: v8.0.0</span>
1997<span>N-API version: 1</span>
1998</div>
1999<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>
2000<ul>
2001<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2002<li><code>[out] result</code>: Boolean value that is set to true if an exception is pending.</li>
2003</ul>
2004<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2005<p>This API can be called even if there is a pending JavaScript exception.</p>
2006<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>
2007<div class="api_metadata">
2008<span>Added in: v9.10.0</span>
2009<span>N-API version: 3</span>
2010</div>
2011<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>
2012<ul>
2013<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2014<li><code>[in] err</code>: The error that is passed to <code>'uncaughtException'</code>.</li>
2015</ul>
2016<p>Trigger an <code>'uncaughtException'</code> in JavaScript. Useful if an async
2017callback throws an exception with no way to recover.</p>
2018<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>
2019<p>In the event of an unrecoverable error in a native addon, a fatal error can be
2020thrown to immediately terminate the process.</p>
2021<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>
2022<div class="api_metadata">
2023<span>Added in: v8.2.0</span>
2024<span>N-API version: 1</span>
2025</div>
2026<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,
2027                                     <span class="hljs-type">size_t</span> location_len,
2028                                     <span class="hljs-type">const</span> <span class="hljs-type">char</span>* message,
2029                                     <span class="hljs-type">size_t</span> message_len)</span>;</code> <button class="copy-button">copy</button></pre>
2030<ul>
2031<li><code>[in] location</code>: Optional location at which the error occurred.</li>
2032<li><code>[in] location_len</code>: The length of the location in bytes, or
2033<code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li>
2034<li><code>[in] message</code>: The message associated with the error.</li>
2035<li><code>[in] message_len</code>: The length of the message in bytes, or <code>NAPI_AUTO_LENGTH</code>
2036if it is null-terminated.</li>
2037</ul>
2038<p>The function call does not return, the process will be terminated.</p>
2039<p>This API can be called even if there is a pending JavaScript exception.</p>
2040</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>
2041<p>As Node-API calls are made, handles to objects in the heap for the underlying
2042VM may be returned as <code>napi_values</code>. These handles must hold the
2043objects 'live' until they are no longer required by the native code,
2044otherwise the objects could be collected before the native code was
2045finished using them.</p>
2046<p>As object handles are returned they are associated with a
2047'scope'. The lifespan for the default scope is tied to the lifespan
2048of the native method call. The result is that, by default, handles
2049remain valid and the objects associated with these handles will be
2050held live for the lifespan of the native method call.</p>
2051<p>In many cases, however, it is necessary that the handles remain valid for
2052either a shorter or longer lifespan than that of the native method.
2053The sections which follow describe the Node-API functions that can be used
2054to change the handle lifespan from the default.</p>
2055<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>
2056<p>It is often necessary to make the lifespan of handles shorter than
2057the lifespan of a native method. For example, consider a native method
2058that has a loop which iterates through the elements in a large array:</p>
2059<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++) {
2060  napi_value result;
2061  napi_status status = napi_get_element(env, object, i, &#x26;result);
2062  <span class="hljs-keyword">if</span> (status != napi_ok) {
2063    <span class="hljs-keyword">break</span>;
2064  }
2065  <span class="hljs-comment">// do something with element</span>
2066}</code> <button class="copy-button">copy</button></pre>
2067<p>This would result in a large number of handles being created, consuming
2068substantial resources. In addition, even though the native code could only
2069use the most recent handle, all of the associated objects would also be
2070kept alive since they all share the same scope.</p>
2071<p>To handle this case, Node-API provides the ability to establish a new 'scope' to
2072which newly created handles will be associated. Once those handles
2073are no longer required, the scope can be 'closed' and any handles associated
2074with the scope are invalidated. The methods available to open/close scopes are
2075<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>
2076<p>Node-API only supports a single nested hierarchy of scopes. There is only one
2077active scope at any time, and all new handles will be associated with that
2078scope while it is active. Scopes must be closed in the reverse order from
2079which they are opened. In addition, all scopes created within a native method
2080must be closed before returning from that method.</p>
2081<p>Taking the earlier example, adding calls to <a href="#napi_open_handle_scope"><code>napi_open_handle_scope</code></a> and
2082<a href="#napi_close_handle_scope"><code>napi_close_handle_scope</code></a> would ensure that at most a single handle
2083is valid throughout the execution of the loop:</p>
2084<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++) {
2085  napi_handle_scope scope;
2086  napi_status status = napi_open_handle_scope(env, &#x26;scope);
2087  <span class="hljs-keyword">if</span> (status != napi_ok) {
2088    <span class="hljs-keyword">break</span>;
2089  }
2090  napi_value result;
2091  status = napi_get_element(env, object, i, &#x26;result);
2092  <span class="hljs-keyword">if</span> (status != napi_ok) {
2093    <span class="hljs-keyword">break</span>;
2094  }
2095  <span class="hljs-comment">// do something with element</span>
2096  status = napi_close_handle_scope(env, scope);
2097  <span class="hljs-keyword">if</span> (status != napi_ok) {
2098    <span class="hljs-keyword">break</span>;
2099  }
2100}</code> <button class="copy-button">copy</button></pre>
2101<p>When nesting scopes, there are cases where a handle from an
2102inner scope needs to live beyond the lifespan of that scope. Node-API supports
2103an 'escapable scope' in order to support this case. An escapable scope
2104allows one handle to be 'promoted' so that it 'escapes' the
2105current scope and the lifespan of the handle changes from the current
2106scope to that of the outer scope.</p>
2107<p>The methods available to open/close escapable scopes are
2108<a href="#napi_open_escapable_handle_scope"><code>napi_open_escapable_handle_scope</code></a> and
2109<a href="#napi_close_escapable_handle_scope"><code>napi_close_escapable_handle_scope</code></a>.</p>
2110<p>The request to promote a handle is made through <a href="#napi_escape_handle"><code>napi_escape_handle</code></a> which
2111can only be called once.</p>
2112<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>
2113<div class="api_metadata">
2114<span>Added in: v8.0.0</span>
2115<span>N-API version: 1</span>
2116</div>
2117<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,
2118                                               napi_handle_scope* result)</span>;</code> <button class="copy-button">copy</button></pre>
2119<ul>
2120<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2121<li><code>[out] result</code>: <code>napi_value</code> representing the new scope.</li>
2122</ul>
2123<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2124<p>This API opens a new scope.</p>
2125<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>
2126<div class="api_metadata">
2127<span>Added in: v8.0.0</span>
2128<span>N-API version: 1</span>
2129</div>
2130<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,
2131                                                napi_handle_scope scope)</span>;</code> <button class="copy-button">copy</button></pre>
2132<ul>
2133<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2134<li><code>[in] scope</code>: <code>napi_value</code> representing the scope to be closed.</li>
2135</ul>
2136<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2137<p>This API closes the scope passed in. Scopes must be closed in the
2138reverse order from which they were created.</p>
2139<p>This API can be called even if there is a pending JavaScript exception.</p>
2140<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>
2141<div class="api_metadata">
2142<span>Added in: v8.0.0</span>
2143<span>N-API version: 1</span>
2144</div>
2145<pre><code class="language-c">NAPI_EXTERN napi_status
2146    <span class="hljs-title function_">napi_open_escapable_handle_scope</span><span class="hljs-params">(napi_env env,
2147                                     napi_handle_scope* result)</span>;</code> <button class="copy-button">copy</button></pre>
2148<ul>
2149<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2150<li><code>[out] result</code>: <code>napi_value</code> representing the new scope.</li>
2151</ul>
2152<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2153<p>This API opens a new scope from which one object can be promoted
2154to the outer scope.</p>
2155<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>
2156<div class="api_metadata">
2157<span>Added in: v8.0.0</span>
2158<span>N-API version: 1</span>
2159</div>
2160<pre><code class="language-c">NAPI_EXTERN napi_status
2161    <span class="hljs-title function_">napi_close_escapable_handle_scope</span><span class="hljs-params">(napi_env env,
2162                                      napi_handle_scope scope)</span>;</code> <button class="copy-button">copy</button></pre>
2163<ul>
2164<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2165<li><code>[in] scope</code>: <code>napi_value</code> representing the scope to be closed.</li>
2166</ul>
2167<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2168<p>This API closes the scope passed in. Scopes must be closed in the
2169reverse order from which they were created.</p>
2170<p>This API can be called even if there is a pending JavaScript exception.</p>
2171<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>
2172<div class="api_metadata">
2173<span>Added in: v8.0.0</span>
2174<span>N-API version: 1</span>
2175</div>
2176<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_escape_handle</span><span class="hljs-params">(napi_env env,
2177                               napi_escapable_handle_scope scope,
2178                               napi_value escapee,
2179                               napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
2180<ul>
2181<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2182<li><code>[in] scope</code>: <code>napi_value</code> representing the current scope.</li>
2183<li><code>[in] escapee</code>: <code>napi_value</code> representing the JavaScript <code>Object</code> to be
2184escaped.</li>
2185<li><code>[out] result</code>: <code>napi_value</code> representing the handle to the escaped <code>Object</code>
2186in the outer scope.</li>
2187</ul>
2188<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2189<p>This API promotes the handle to the JavaScript object so that it is valid
2190for the lifetime of the outer scope. It can only be called once per scope.
2191If it is called more than once an error will be returned.</p>
2192<p>This API can be called even if there is a pending JavaScript exception.</p>
2193<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>
2194<p>In some cases, an addon will need to be able to create and reference values
2195with a lifespan longer than that of a single native method invocation. For
2196example, to create a constructor and later use that constructor
2197in a request to create instances, it must be possible to reference
2198the constructor object across many different instance creation requests. This
2199would not be possible with a normal handle returned as a <code>napi_value</code> as
2200described in the earlier section. The lifespan of a normal handle is
2201managed by scopes and all scopes must be closed before the end of a native
2202method.</p>
2203<p>Node-API provides methods for creating persistent references to values.
2204Currently Node-API only allows references to be created for a
2205limited set of value types, including object, external, function, and symbol.</p>
2206<p>Each reference has an associated count with a value of 0 or higher,
2207which determines whether the reference will keep the corresponding value alive.
2208References with a count of 0 do not prevent values from being collected.
2209Values of object (object, function, external) and symbol types are becoming
2210'weak' references and can still be accessed while they are not collected.
2211Any count greater than 0 will prevent the values from being collected.</p>
2212<p>Symbol values have different flavors. The true weak reference behavior is
2213only supported by local symbols created with the <code>napi_create_symbol</code> function
2214or the JavaScript <code>Symbol()</code> constructor calls. Globally registered symbols
2215created with the <code>node_api_symbol_for</code> function or JavaScript <code>Symbol.for()</code>
2216function calls remain always strong references because the garbage collector
2217does not collect them. The same is true for well-known symbols such as
2218<code>Symbol.iterator</code>. They are also never collected by the garbage collector.</p>
2219<p>References can be created with an initial reference count. The count can
2220then be modified through <a href="#napi_reference_ref"><code>napi_reference_ref</code></a> and
2221<a href="#napi_reference_unref"><code>napi_reference_unref</code></a>. If an object is collected while the count
2222for a reference is 0, all subsequent calls to
2223get the object associated with the reference <a href="#napi_get_reference_value"><code>napi_get_reference_value</code></a>
2224will return <code>NULL</code> for the returned <code>napi_value</code>. An attempt to call
2225<a href="#napi_reference_ref"><code>napi_reference_ref</code></a> for a reference whose object has been collected
2226results in an error.</p>
2227<p>References must be deleted once they are no longer required by the addon. When
2228a reference is deleted, it will no longer prevent the corresponding object from
2229being collected. Failure to delete a persistent reference results in
2230a 'memory leak' with both the native memory for the persistent reference and
2231the corresponding object on the heap being retained forever.</p>
2232<p>There can be multiple persistent references created which refer to the same
2233object, each of which will either keep the object live or not based on its
2234individual count. Multiple persistent references to the same object
2235can result in unexpectedly keeping alive native memory. The native structures
2236for a persistent reference must be kept alive until finalizers for the
2237referenced object are executed. If a new persistent reference is created
2238for the same object, the finalizers for that object will not be
2239run and the native memory pointed by the earlier persistent reference
2240will not be freed. This can be avoided by calling
2241<code>napi_delete_reference</code> in addition to <code>napi_reference_unref</code> when possible.</p>
2242<p><strong>Change History:</strong></p>
2243<ul>
2244<li>
2245<p>Experimental (<code>NAPI_EXPERIMENTAL</code> is defined):</p>
2246<p>References can be created for all value types. The new supported value
2247types do not support weak reference semantic and the values of these types
2248are released when the reference count becomes 0 and cannot be accessed from
2249the reference anymore.</p>
2250</li>
2251</ul>
2252<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>
2253<div class="api_metadata">
2254<span>Added in: v8.0.0</span>
2255<span>N-API version: 1</span>
2256</div>
2257<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,
2258                                              napi_value value,
2259                                              <span class="hljs-type">uint32_t</span> initial_refcount,
2260                                              napi_ref* result)</span>;</code> <button class="copy-button">copy</button></pre>
2261<ul>
2262<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2263<li><code>[in] value</code>: The <code>napi_value</code> for which a reference is being created.</li>
2264<li><code>[in] initial_refcount</code>: Initial reference count for the new reference.</li>
2265<li><code>[out] result</code>: <code>napi_ref</code> pointing to the new reference.</li>
2266</ul>
2267<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2268<p>This API creates a new reference with the specified reference count
2269to the value passed in.</p>
2270<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>
2271<div class="api_metadata">
2272<span>Added in: v8.0.0</span>
2273<span>N-API version: 1</span>
2274</div>
2275<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>
2276<ul>
2277<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2278<li><code>[in] ref</code>: <code>napi_ref</code> to be deleted.</li>
2279</ul>
2280<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2281<p>This API deletes the reference passed in.</p>
2282<p>This API can be called even if there is a pending JavaScript exception.</p>
2283<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>
2284<div class="api_metadata">
2285<span>Added in: v8.0.0</span>
2286<span>N-API version: 1</span>
2287</div>
2288<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,
2289                                           napi_ref ref,
2290                                           <span class="hljs-type">uint32_t</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
2291<ul>
2292<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2293<li><code>[in] ref</code>: <code>napi_ref</code> for which the reference count will be incremented.</li>
2294<li><code>[out] result</code>: The new reference count.</li>
2295</ul>
2296<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2297<p>This API increments the reference count for the reference
2298passed in and returns the resulting reference count.</p>
2299<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>
2300<div class="api_metadata">
2301<span>Added in: v8.0.0</span>
2302<span>N-API version: 1</span>
2303</div>
2304<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,
2305                                             napi_ref ref,
2306                                             <span class="hljs-type">uint32_t</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
2307<ul>
2308<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2309<li><code>[in] ref</code>: <code>napi_ref</code> for which the reference count will be decremented.</li>
2310<li><code>[out] result</code>: The new reference count.</li>
2311</ul>
2312<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2313<p>This API decrements the reference count for the reference
2314passed in and returns the resulting reference count.</p>
2315<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>
2316<div class="api_metadata">
2317<span>Added in: v8.0.0</span>
2318<span>N-API version: 1</span>
2319</div>
2320<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,
2321                                                 napi_ref ref,
2322                                                 napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
2323<ul>
2324<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2325<li><code>[in] ref</code>: The <code>napi_ref</code> for which the corresponding value is
2326being requested.</li>
2327<li><code>[out] result</code>: The <code>napi_value</code> referenced by the <code>napi_ref</code>.</li>
2328</ul>
2329<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2330<p>If still valid, this API returns the <code>napi_value</code> representing the
2331JavaScript value associated with the <code>napi_ref</code>. Otherwise, result
2332will be <code>NULL</code>.</p>
2333<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>
2334<p>While a Node.js process typically releases all its resources when exiting,
2335embedders of Node.js, or future Worker support, may require addons to register
2336clean-up hooks that will be run once the current Node.js environment exits.</p>
2337<p>Node-API provides functions for registering and un-registering such callbacks.
2338When those callbacks are run, all resources that are being held by the addon
2339should be freed up.</p>
2340<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>
2341<div class="api_metadata">
2342<span>Added in: v10.2.0</span>
2343<span>N-API version: 3</span>
2344</div>
2345<pre><code class="language-c">NODE_EXTERN napi_status <span class="hljs-title function_">napi_add_env_cleanup_hook</span><span class="hljs-params">(node_api_nogc_env env,
2346                                                  napi_cleanup_hook fun,
2347                                                  <span class="hljs-type">void</span>* arg)</span>;</code> <button class="copy-button">copy</button></pre>
2348<p>Registers <code>fun</code> as a function to be run with the <code>arg</code> parameter once the
2349current Node.js environment exits.</p>
2350<p>A function can safely be specified multiple times with different
2351<code>arg</code> values. In that case, it will be called multiple times as well.
2352Providing the same <code>fun</code> and <code>arg</code> values multiple times is not allowed
2353and will lead the process to abort.</p>
2354<p>The hooks will be called in reverse order, i.e. the most recently added one
2355will be called first.</p>
2356<p>Removing this hook can be done by using <a href="#napi_remove_env_cleanup_hook"><code>napi_remove_env_cleanup_hook</code></a>.
2357Typically, that happens when the resource for which this hook was added
2358is being torn down anyway.</p>
2359<p>For asynchronous cleanup, <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a> is available.</p>
2360<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>
2361<div class="api_metadata">
2362<span>Added in: v10.2.0</span>
2363<span>N-API version: 3</span>
2364</div>
2365<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remove_env_cleanup_hook</span><span class="hljs-params">(node_api_nogc_env env,
2366                                                     <span class="hljs-type">void</span> (*fun)(<span class="hljs-type">void</span>* arg),
2367                                                     <span class="hljs-type">void</span>* arg)</span>;</code> <button class="copy-button">copy</button></pre>
2368<p>Unregisters <code>fun</code> as a function to be run with the <code>arg</code> parameter once the
2369current Node.js environment exits. Both the argument and the function value
2370need to be exact matches.</p>
2371<p>The function must have originally been registered
2372with <code>napi_add_env_cleanup_hook</code>, otherwise the process will abort.</p>
2373<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>
2374<div class="api_metadata">
2375<details class="changelog"><summary>History</summary>
2376<table>
2377<tbody><tr><th>Version</th><th>Changes</th></tr>
2378<tr><td>v14.10.0, v12.19.0</td>
2379<td><p>Changed signature of the <code>hook</code> callback.</p></td></tr>
2380<tr><td>v14.8.0, v12.19.0</td>
2381<td><p><span>Added in: v14.8.0, v12.19.0</span></p></td></tr>
2382</tbody></table>
2383</details>
2384<span>N-API version: 8</span>
2385</div>
2386<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_add_async_cleanup_hook</span><span class="hljs-params">(
2387    node_api_nogc_env env,
2388    napi_async_cleanup_hook hook,
2389    <span class="hljs-type">void</span>* arg,
2390    napi_async_cleanup_hook_handle* remove_handle)</span>;</code> <button class="copy-button">copy</button></pre>
2391<ul>
2392<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2393<li><code>[in] hook</code>: The function pointer to call at environment teardown.</li>
2394<li><code>[in] arg</code>: The pointer to pass to <code>hook</code> when it gets called.</li>
2395<li><code>[out] remove_handle</code>: Optional handle that refers to the asynchronous cleanup
2396hook.</li>
2397</ul>
2398<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
2399a function to be run with the <code>remove_handle</code> and <code>arg</code> parameters once the
2400current Node.js environment exits.</p>
2401<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>
2402<p>Otherwise, behavior generally matches that of <a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a>.</p>
2403<p>If <code>remove_handle</code> is not <code>NULL</code>, an opaque value will be stored in it
2404that must later be passed to <a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a>,
2405regardless of whether the hook has already been invoked.
2406Typically, that happens when the resource for which this hook was added
2407is being torn down anyway.</p>
2408<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>
2409<div class="api_metadata">
2410<details class="changelog"><summary>History</summary>
2411<table>
2412<tbody><tr><th>Version</th><th>Changes</th></tr>
2413<tr><td>v14.10.0, v12.19.0</td>
2414<td><p>Removed <code>env</code> parameter.</p></td></tr>
2415<tr><td>v14.8.0, v12.19.0</td>
2416<td><p><span>Added in: v14.8.0, v12.19.0</span></p></td></tr>
2417</tbody></table>
2418</details>
2419</div>
2420<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remove_async_cleanup_hook</span><span class="hljs-params">(
2421    napi_async_cleanup_hook_handle remove_handle)</span>;</code> <button class="copy-button">copy</button></pre>
2422<ul>
2423<li><code>[in] remove_handle</code>: The handle to an asynchronous cleanup hook that was
2424created with <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>.</li>
2425</ul>
2426<p>Unregisters the cleanup hook corresponding to <code>remove_handle</code>. This will prevent
2427the hook from being executed, unless it has already started executing.
2428This must be called on any <code>napi_async_cleanup_hook_handle</code> value obtained
2429from <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>.</p>
2430<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>
2431<p>The Node.js environment may be torn down at an arbitrary time as soon as
2432possible with JavaScript execution disallowed, like on the request of
2433<a href="worker_threads.html#workerterminate"><code>worker.terminate()</code></a>. When the environment is being torn down, the
2434registered <code>napi_finalize</code> callbacks of JavaScript objects, thread-safe
2435functions and environment instance data are invoked immediately and
2436independently.</p>
2437<p>The invocation of <code>napi_finalize</code> callbacks is scheduled after the manually
2438registered cleanup hooks. In order to ensure a proper order of addon
2439finalization during environment shutdown to avoid use-after-free in the
2440<code>napi_finalize</code> callback, addons should register a cleanup hook with
2441<code>napi_add_env_cleanup_hook</code> and <code>napi_add_async_cleanup_hook</code> to manually
2442release the allocated resource in a proper order.</p>
2443</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>
2444<p>Node-API modules are registered in a manner similar to other modules
2445except that instead of using the <code>NODE_MODULE</code> macro the following
2446is used:</p>
2447<pre><code class="language-c">NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)</code> <button class="copy-button">copy</button></pre>
2448<p>The next difference is the signature for the <code>Init</code> method. For a Node-API
2449module it is as follows:</p>
2450<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>
2451<p>The return value from <code>Init</code> is treated as the <code>exports</code> object for the module.
2452The <code>Init</code> method is passed an empty object via the <code>exports</code> parameter as a
2453convenience. If <code>Init</code> returns <code>NULL</code>, the parameter passed as <code>exports</code> is
2454exported by the module. Node-API modules cannot modify the <code>module</code> object but
2455can specify anything as the <code>exports</code> property of the module.</p>
2456<p>To add the method <code>hello</code> as a function so that it can be called as a method
2457provided by the addon:</p>
2458<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> {
2459  napi_status status;
2460  napi_property_descriptor desc = {
2461    <span class="hljs-string">"hello"</span>,
2462    <span class="hljs-literal">NULL</span>,
2463    Method,
2464    <span class="hljs-literal">NULL</span>,
2465    <span class="hljs-literal">NULL</span>,
2466    <span class="hljs-literal">NULL</span>,
2467    napi_writable | napi_enumerable | napi_configurable,
2468    <span class="hljs-literal">NULL</span>
2469  };
2470  status = napi_define_properties(env, exports, <span class="hljs-number">1</span>, &#x26;desc);
2471  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
2472  <span class="hljs-keyword">return</span> exports;
2473}</code> <button class="copy-button">copy</button></pre>
2474<p>To set a function to be returned by the <code>require()</code> for the addon:</p>
2475<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> {
2476  napi_value method;
2477  napi_status status;
2478  status = napi_create_function(env, <span class="hljs-string">"exports"</span>, NAPI_AUTO_LENGTH, Method, <span class="hljs-literal">NULL</span>, &#x26;method);
2479  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
2480  <span class="hljs-keyword">return</span> method;
2481}</code> <button class="copy-button">copy</button></pre>
2482<p>To define a class so that new instances can be created (often used with
2483<a href="#object-wrap">Object wrap</a>):</p>
2484<pre><code class="language-c"><span class="hljs-comment">// <span class="hljs-doctag">NOTE:</span> partial example, not all referenced code is included</span>
2485napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> {
2486  napi_status status;
2487  napi_property_descriptor properties[] = {
2488    { <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> },
2489    DECLARE_NAPI_METHOD(<span class="hljs-string">"plusOne"</span>, PlusOne),
2490    DECLARE_NAPI_METHOD(<span class="hljs-string">"multiply"</span>, Multiply),
2491  };
2492
2493  napi_value cons;
2494  status =
2495      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);
2496  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
2497
2498  status = napi_create_reference(env, cons, <span class="hljs-number">1</span>, &#x26;constructor);
2499  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
2500
2501  status = napi_set_named_property(env, exports, <span class="hljs-string">"MyObject"</span>, cons);
2502  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
2503
2504  <span class="hljs-keyword">return</span> exports;
2505}</code> <button class="copy-button">copy</button></pre>
2506<p>You can also use the <code>NAPI_MODULE_INIT</code> macro, which acts as a shorthand
2507for <code>NAPI_MODULE</code> and defining an <code>Init</code> function:</p>
2508<pre><code class="language-c">NAPI_MODULE_INIT(<span class="hljs-comment">/* napi_env env, napi_value exports */</span>) {
2509  napi_value answer;
2510  napi_status result;
2511
2512  status = napi_create_int64(env, <span class="hljs-number">42</span>, &#x26;answer);
2513  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
2514
2515  status = napi_set_named_property(env, exports, <span class="hljs-string">"answer"</span>, answer);
2516  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
2517
2518  <span class="hljs-keyword">return</span> exports;
2519}</code> <button class="copy-button">copy</button></pre>
2520<p>The parameters <code>env</code> and <code>exports</code> are provided to the body of the
2521<code>NAPI_MODULE_INIT</code> macro.</p>
2522<p>All Node-API addons are context-aware, meaning they may be loaded multiple
2523times. There are a few design considerations when declaring such a module.
2524The documentation on <a href="addons.html#context-aware-addons">context-aware addons</a> provides more details.</p>
2525<p>The variables <code>env</code> and <code>exports</code> will be available inside the function body
2526following the macro invocation.</p>
2527<p>For more details on setting properties on objects, see the section on
2528<a href="#working-with-javascript-properties">Working with JavaScript properties</a>.</p>
2529<p>For more details on building addon modules in general, refer to the existing
2530API.</p>
2531</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>
2532<p>Node-API exposes a set of APIs to create all types of JavaScript values.
2533Some of these types are documented under <a href="https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values">Section 6</a>
2534of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p>
2535<p>Fundamentally, these APIs are used to do one of the following:</p>
2536<ol>
2537<li>Create a new JavaScript object</li>
2538<li>Convert from a primitive C type to a Node-API value</li>
2539<li>Convert from Node-API value to a primitive C type</li>
2540<li>Get global instances including <code>undefined</code> and <code>null</code></li>
2541</ol>
2542<p>Node-API values are represented by the type <code>napi_value</code>.
2543Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>.
2544In some cases, the API does check the type of the <code>napi_value</code> up-front.
2545However, for better performance, it's better for the caller to make sure that
2546the <code>napi_value</code> in question is of the JavaScript type expected by the API.</p>
2547<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>
2548<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>
2549<div class="api_metadata">
2550<span>Added in: v13.7.0, v12.17.0, v10.20.0</span>
2551<span>N-API version: 6</span>
2552</div>
2553<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
2554  napi_key_include_prototypes,
2555  napi_key_own_only
2556} napi_key_collection_mode;</code> <button class="copy-button">copy</button></pre>
2557<p>Describes the <code>Keys/Properties</code> filter enums:</p>
2558<p><code>napi_key_collection_mode</code> limits the range of collected properties.</p>
2559<p><code>napi_key_own_only</code> limits the collected properties to the given
2560object only. <code>napi_key_include_prototypes</code> will include all keys
2561of the objects's prototype chain as well.</p>
2562<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>
2563<div class="api_metadata">
2564<span>Added in: v13.7.0, v12.17.0, v10.20.0</span>
2565<span>N-API version: 6</span>
2566</div>
2567<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
2568  napi_key_all_properties = <span class="hljs-number">0</span>,
2569  napi_key_writable = <span class="hljs-number">1</span>,
2570  napi_key_enumerable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">1</span>,
2571  napi_key_configurable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">2</span>,
2572  napi_key_skip_strings = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">3</span>,
2573  napi_key_skip_symbols = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">4</span>
2574} napi_key_filter;</code> <button class="copy-button">copy</button></pre>
2575<p>Property filter bits. They can be or'ed to build a composite filter.</p>
2576<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>
2577<div class="api_metadata">
2578<span>Added in: v13.7.0, v12.17.0, v10.20.0</span>
2579<span>N-API version: 6</span>
2580</div>
2581<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
2582  napi_key_keep_numbers,
2583  napi_key_numbers_to_strings
2584} napi_key_conversion;</code> <button class="copy-button">copy</button></pre>
2585<p><code>napi_key_numbers_to_strings</code> will convert integer indices to
2586strings. <code>napi_key_keep_numbers</code> will return numbers for integer
2587indices.</p>
2588<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>
2589<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
2590  <span class="hljs-comment">// ES6 types (corresponds to typeof)</span>
2591  napi_undefined,
2592  napi_null,
2593  napi_boolean,
2594  napi_number,
2595  napi_string,
2596  napi_symbol,
2597  napi_object,
2598  napi_function,
2599  napi_external,
2600  napi_bigint,
2601} napi_valuetype;</code> <button class="copy-button">copy</button></pre>
2602<p>Describes the type of a <code>napi_value</code>. This generally corresponds to the types
2603described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types">Section 6.1</a> of the ECMAScript Language Specification.
2604In addition to types in that section, <code>napi_valuetype</code> can also represent
2605<code>Function</code>s and <code>Object</code>s with external data.</p>
2606<p>A JavaScript value of type <code>napi_external</code> appears in JavaScript as a plain
2607object such that no properties can be set on it, and no prototype.</p>
2608<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>
2609<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
2610  napi_int8_array,
2611  napi_uint8_array,
2612  napi_uint8_clamped_array,
2613  napi_int16_array,
2614  napi_uint16_array,
2615  napi_int32_array,
2616  napi_uint32_array,
2617  napi_float32_array,
2618  napi_float64_array,
2619  napi_bigint64_array,
2620  napi_biguint64_array,
2621} napi_typedarray_type;</code> <button class="copy-button">copy</button></pre>
2622<p>This represents the underlying binary scalar datatype of the <code>TypedArray</code>.
2623Elements of this enum correspond to
2624<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>
2625<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>
2626<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>
2627<div class="api_metadata">
2628<span>Added in: v8.0.0</span>
2629<span>N-API version: 1</span>
2630</div>
2631<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>
2632<ul>
2633<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
2634<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Array</code>.</li>
2635</ul>
2636<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2637<p>This API returns a Node-API value corresponding to a JavaScript <code>Array</code> type.
2638JavaScript arrays are described in
2639<a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Language Specification.</p>
2640<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>
2641<div class="api_metadata">
2642<span>Added in: v8.0.0</span>
2643<span>N-API version: 1</span>
2644</div>
2645<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,
2646                                          <span class="hljs-type">size_t</span> length,
2647                                          napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2648<ul>
2649<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2650<li><code>[in] length</code>: The initial length of the <code>Array</code>.</li>
2651<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Array</code>.</li>
2652</ul>
2653<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2654<p>This API returns a Node-API value corresponding to a JavaScript <code>Array</code> type.
2655The <code>Array</code>'s length property is set to the passed-in length parameter.
2656However, the underlying buffer is not guaranteed to be pre-allocated by the VM
2657when the array is created. That behavior is left to the underlying VM
2658implementation. If the buffer must be a contiguous block of memory that can be
2659directly read and/or written via C, consider using
2660<a href="#napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code></a>.</p>
2661<p>JavaScript arrays are described in
2662<a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Language Specification.</p>
2663<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>
2664<div class="api_metadata">
2665<span>Added in: v8.0.0</span>
2666<span>N-API version: 1</span>
2667</div>
2668<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_arraybuffer</span><span class="hljs-params">(napi_env env,
2669                                    <span class="hljs-type">size_t</span> byte_length,
2670                                    <span class="hljs-type">void</span>** data,
2671                                    napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2672<ul>
2673<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2674<li><code>[in] length</code>: The length in bytes of the array buffer to create.</li>
2675<li><code>[out] data</code>: Pointer to the underlying byte buffer of the <code>ArrayBuffer</code>.
2676<code>data</code> can optionally be ignored by passing <code>NULL</code>.</li>
2677<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>ArrayBuffer</code>.</li>
2678</ul>
2679<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2680<p>This API returns a Node-API value corresponding to a JavaScript <code>ArrayBuffer</code>.
2681<code>ArrayBuffer</code>s are used to represent fixed-length binary data buffers. They are
2682normally used as a backing-buffer for <code>TypedArray</code> objects.
2683The <code>ArrayBuffer</code> allocated will have an underlying byte buffer whose size is
2684determined by the <code>length</code> parameter that's passed in.
2685The underlying buffer is optionally returned back to the caller in case the
2686caller wants to directly manipulate the buffer. This buffer can only be
2687written to directly from native code. To write to this buffer from JavaScript,
2688a typed array or <code>DataView</code> object would need to be created.</p>
2689<p>JavaScript <code>ArrayBuffer</code> objects are described in
2690<a href="https://tc39.github.io/ecma262/#sec-arraybuffer-objects">Section 24.1</a> of the ECMAScript Language Specification.</p>
2691<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>
2692<div class="api_metadata">
2693<span>Added in: v8.0.0</span>
2694<span>N-API version: 1</span>
2695</div>
2696<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer</span><span class="hljs-params">(napi_env env,
2697                               <span class="hljs-type">size_t</span> size,
2698                               <span class="hljs-type">void</span>** data,
2699                               napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2700<ul>
2701<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2702<li><code>[in] size</code>: Size in bytes of the underlying buffer.</li>
2703<li><code>[out] data</code>: Raw pointer to the underlying buffer.
2704<code>data</code> can optionally be ignored by passing <code>NULL</code>.</li>
2705<li><code>[out] result</code>: A <code>napi_value</code> representing a <code>node::Buffer</code>.</li>
2706</ul>
2707<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2708<p>This API allocates a <code>node::Buffer</code> object. While this is still a
2709fully-supported data structure, in most cases using a <code>TypedArray</code> will suffice.</p>
2710<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>
2711<div class="api_metadata">
2712<span>Added in: v8.0.0</span>
2713<span>N-API version: 1</span>
2714</div>
2715<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer_copy</span><span class="hljs-params">(napi_env env,
2716                                    <span class="hljs-type">size_t</span> length,
2717                                    <span class="hljs-type">const</span> <span class="hljs-type">void</span>* data,
2718                                    <span class="hljs-type">void</span>** result_data,
2719                                    napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2720<ul>
2721<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2722<li><code>[in] size</code>: Size in bytes of the input buffer (should be the same as the size
2723of the new buffer).</li>
2724<li><code>[in] data</code>: Raw pointer to the underlying buffer to copy from.</li>
2725<li><code>[out] result_data</code>: Pointer to the new <code>Buffer</code>'s underlying data buffer.
2726<code>result_data</code> can optionally be ignored by passing <code>NULL</code>.</li>
2727<li><code>[out] result</code>: A <code>napi_value</code> representing a <code>node::Buffer</code>.</li>
2728</ul>
2729<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2730<p>This API allocates a <code>node::Buffer</code> object and initializes it with data copied
2731from the passed-in buffer. While this is still a fully-supported data
2732structure, in most cases using a <code>TypedArray</code> will suffice.</p>
2733<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>
2734<div class="api_metadata">
2735<span>Added in: v11.11.0, v10.17.0</span>
2736<span>N-API version: 5</span>
2737</div>
2738<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_date</span><span class="hljs-params">(napi_env env,
2739                             <span class="hljs-type">double</span> time,
2740                             napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
2741<ul>
2742<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2743<li><code>[in] time</code>: ECMAScript time value in milliseconds since 01 January, 1970 UTC.</li>
2744<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Date</code>.</li>
2745</ul>
2746<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2747<p>This API does not observe leap seconds; they are ignored, as
2748ECMAScript aligns with POSIX time specification.</p>
2749<p>This API allocates a JavaScript <code>Date</code> object.</p>
2750<p>JavaScript <code>Date</code> objects are described in
2751<a href="https://tc39.github.io/ecma262/#sec-date-objects">Section 20.3</a> of the ECMAScript Language Specification.</p>
2752<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>
2753<div class="api_metadata">
2754<span>Added in: v8.0.0</span>
2755<span>N-API version: 1</span>
2756</div>
2757<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external</span><span class="hljs-params">(napi_env env,
2758                                 <span class="hljs-type">void</span>* data,
2759                                 napi_finalize finalize_cb,
2760                                 <span class="hljs-type">void</span>* finalize_hint,
2761                                 napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2762<ul>
2763<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2764<li><code>[in] data</code>: Raw pointer to the external data.</li>
2765<li><code>[in] finalize_cb</code>: Optional callback to call when the external value is being
2766collected. <a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
2767<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
2768collection.</li>
2769<li><code>[out] result</code>: A <code>napi_value</code> representing an external value.</li>
2770</ul>
2771<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2772<p>This API allocates a JavaScript value with external data attached to it. This
2773is used to pass external data through JavaScript code, so it can be retrieved
2774later by native code using <a href="#napi_get_value_external"><code>napi_get_value_external</code></a>.</p>
2775<p>The API adds a <code>napi_finalize</code> callback which will be called when the JavaScript
2776object just created has been garbage collected.</p>
2777<p>The created value is not an object, and therefore does not support additional
2778properties. It is considered a distinct value type: calling <code>napi_typeof()</code> with
2779an external value yields <code>napi_external</code>.</p>
2780<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>
2781<div class="api_metadata">
2782<span>Added in: v8.0.0</span>
2783<span>N-API version: 1</span>
2784</div>
2785<pre><code class="language-c">napi_status
2786<span class="hljs-title function_">napi_create_external_arraybuffer</span><span class="hljs-params">(napi_env env,
2787                                 <span class="hljs-type">void</span>* external_data,
2788                                 <span class="hljs-type">size_t</span> byte_length,
2789                                 napi_finalize finalize_cb,
2790                                 <span class="hljs-type">void</span>* finalize_hint,
2791                                 napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2792<ul>
2793<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2794<li><code>[in] external_data</code>: Pointer to the underlying byte buffer of the
2795<code>ArrayBuffer</code>.</li>
2796<li><code>[in] byte_length</code>: The length in bytes of the underlying buffer.</li>
2797<li><code>[in] finalize_cb</code>: Optional callback to call when the <code>ArrayBuffer</code> is being
2798collected. <a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
2799<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
2800collection.</li>
2801<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>ArrayBuffer</code>.</li>
2802</ul>
2803<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2804<p><strong>Some runtimes other than Node.js have dropped support for external buffers</strong>.
2805On runtimes other than Node.js this method may return
2806<code>napi_no_external_buffers_allowed</code> to indicate that external
2807buffers are not supported. One such runtime is Electron as
2808described in this issue
2809<a href="https://github.com/electron/electron/issues/35801">electron/issues/35801</a>.</p>
2810<p>In order to maintain broadest compatibility with all runtimes
2811you may define <code>NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED</code> in your addon before
2812includes for the node-api headers. Doing so will hide the 2 functions
2813that create external buffers. This will ensure a compilation error
2814occurs if you accidentally use one of these methods.</p>
2815<p>This API returns a Node-API value corresponding to a JavaScript <code>ArrayBuffer</code>.
2816The underlying byte buffer of the <code>ArrayBuffer</code> is externally allocated and
2817managed. The caller must ensure that the byte buffer remains valid until the
2818finalize callback is called.</p>
2819<p>The API adds a <code>napi_finalize</code> callback which will be called when the JavaScript
2820object just created has been garbage collected.</p>
2821<p>JavaScript <code>ArrayBuffer</code>s are described in
2822<a href="https://tc39.github.io/ecma262/#sec-arraybuffer-objects">Section 24.1</a> of the ECMAScript Language Specification.</p>
2823<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>
2824<div class="api_metadata">
2825<span>Added in: v8.0.0</span>
2826<span>N-API version: 1</span>
2827</div>
2828<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external_buffer</span><span class="hljs-params">(napi_env env,
2829                                        <span class="hljs-type">size_t</span> length,
2830                                        <span class="hljs-type">void</span>* data,
2831                                        napi_finalize finalize_cb,
2832                                        <span class="hljs-type">void</span>* finalize_hint,
2833                                        napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2834<ul>
2835<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2836<li><code>[in] length</code>: Size in bytes of the input buffer (should be the same as the
2837size of the new buffer).</li>
2838<li><code>[in] data</code>: Raw pointer to the underlying buffer to expose to JavaScript.</li>
2839<li><code>[in] finalize_cb</code>: Optional callback to call when the <code>ArrayBuffer</code> is being
2840collected. <a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
2841<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
2842collection.</li>
2843<li><code>[out] result</code>: A <code>napi_value</code> representing a <code>node::Buffer</code>.</li>
2844</ul>
2845<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2846<p><strong>Some runtimes other than Node.js have dropped support for external buffers</strong>.
2847On runtimes other than Node.js this method may return
2848<code>napi_no_external_buffers_allowed</code> to indicate that external
2849buffers are not supported. One such runtime is Electron as
2850described in this issue
2851<a href="https://github.com/electron/electron/issues/35801">electron/issues/35801</a>.</p>
2852<p>In order to maintain broadest compatibility with all runtimes
2853you may define <code>NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED</code> in your addon before
2854includes for the node-api headers. Doing so will hide the 2 functions
2855that create external buffers. This will ensure a compilation error
2856occurs if you accidentally use one of these methods.</p>
2857<p>This API allocates a <code>node::Buffer</code> object and initializes it with data
2858backed by the passed in buffer. While this is still a fully-supported data
2859structure, in most cases using a <code>TypedArray</code> will suffice.</p>
2860<p>The API adds a <code>napi_finalize</code> callback which will be called when the JavaScript
2861object just created has been garbage collected.</p>
2862<p>For Node.js >=4 <code>Buffers</code> are <code>Uint8Array</code>s.</p>
2863<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>
2864<div class="api_metadata">
2865<span>Added in: v8.0.0</span>
2866<span>N-API version: 1</span>
2867</div>
2868<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>
2869<ul>
2870<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2871<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Object</code>.</li>
2872</ul>
2873<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2874<p>This API allocates a default JavaScript <code>Object</code>.
2875It is the equivalent of doing <code>new Object()</code> in JavaScript.</p>
2876<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
2877ECMAScript Language Specification.</p>
2878<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>
2879<div class="api_metadata">
2880<span>Added in: v8.0.0</span>
2881<span>N-API version: 1</span>
2882</div>
2883<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_symbol</span><span class="hljs-params">(napi_env env,
2884                               napi_value description,
2885                               napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2886<ul>
2887<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2888<li><code>[in] description</code>: Optional <code>napi_value</code> which refers to a JavaScript
2889<code>string</code> to be set as the description for the symbol.</li>
2890<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>symbol</code>.</li>
2891</ul>
2892<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2893<p>This API creates a JavaScript <code>symbol</code> value from a UTF8-encoded C string.</p>
2894<p>The JavaScript <code>symbol</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-symbol-objects">Section 19.4</a>
2895of the ECMAScript Language Specification.</p>
2896<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>
2897<div class="api_metadata">
2898<span>Added in: v17.5.0</span>
2899<span>N-API version: 9</span>
2900</div>
2901<pre><code class="language-c">napi_status <span class="hljs-title function_">node_api_symbol_for</span><span class="hljs-params">(napi_env env,
2902                                <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8description,
2903                                <span class="hljs-type">size_t</span> length,
2904                                napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2905<ul>
2906<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2907<li><code>[in] utf8description</code>: UTF-8 C string representing the text to be used as the
2908description for the symbol.</li>
2909<li><code>[in] length</code>: The length of the description string in bytes, or
2910<code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li>
2911<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>symbol</code>.</li>
2912</ul>
2913<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2914<p>This API searches in the global registry for an existing symbol with the given
2915description. If the symbol already exists it will be returned, otherwise a new
2916symbol will be created in the registry.</p>
2917<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
2918Language Specification.</p>
2919<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>
2920<div class="api_metadata">
2921<span>Added in: v8.0.0</span>
2922<span>N-API version: 1</span>
2923</div>
2924<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_typedarray</span><span class="hljs-params">(napi_env env,
2925                                   napi_typedarray_type type,
2926                                   <span class="hljs-type">size_t</span> length,
2927                                   napi_value arraybuffer,
2928                                   <span class="hljs-type">size_t</span> byte_offset,
2929                                   napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2930<ul>
2931<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2932<li><code>[in] type</code>: Scalar datatype of the elements within the <code>TypedArray</code>.</li>
2933<li><code>[in] length</code>: Number of elements in the <code>TypedArray</code>.</li>
2934<li><code>[in] arraybuffer</code>: <code>ArrayBuffer</code> underlying the typed array.</li>
2935<li><code>[in] byte_offset</code>: The byte offset within the <code>ArrayBuffer</code> from which to
2936start projecting the <code>TypedArray</code>.</li>
2937<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>TypedArray</code>.</li>
2938</ul>
2939<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2940<p>This API creates a JavaScript <code>TypedArray</code> object over an existing
2941<code>ArrayBuffer</code>. <code>TypedArray</code> objects provide an array-like view over an
2942underlying data buffer where each element has the same underlying binary scalar
2943datatype.</p>
2944<p>It's required that <code>(length * size_of_element) + byte_offset</code> should
2945be &#x3C;= the size in bytes of the array passed in. If not, a <code>RangeError</code> exception
2946is raised.</p>
2947<p>JavaScript <code>TypedArray</code> objects are described in
2948<a href="https://tc39.github.io/ecma262/#sec-typedarray-objects">Section 22.2</a> of the ECMAScript Language Specification.</p>
2949<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>
2950<div class="api_metadata">
2951<span>Added in: v8.3.0</span>
2952<span>N-API version: 1</span>
2953</div>
2954<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_dataview</span><span class="hljs-params">(napi_env env,
2955                                 <span class="hljs-type">size_t</span> byte_length,
2956                                 napi_value arraybuffer,
2957                                 <span class="hljs-type">size_t</span> byte_offset,
2958                                 napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
2959<ul>
2960<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2961<li><code>[in] length</code>: Number of elements in the <code>DataView</code>.</li>
2962<li><code>[in] arraybuffer</code>: <code>ArrayBuffer</code> underlying the <code>DataView</code>.</li>
2963<li><code>[in] byte_offset</code>: The byte offset within the <code>ArrayBuffer</code> from which to
2964start projecting the <code>DataView</code>.</li>
2965<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>DataView</code>.</li>
2966</ul>
2967<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2968<p>This API creates a JavaScript <code>DataView</code> object over an existing <code>ArrayBuffer</code>.
2969<code>DataView</code> objects provide an array-like view over an underlying data buffer,
2970but one which allows items of different size and type in the <code>ArrayBuffer</code>.</p>
2971<p>It is required that <code>byte_length + byte_offset</code> is less than or equal to the
2972size in bytes of the array passed in. If not, a <code>RangeError</code> exception is
2973raised.</p>
2974<p>JavaScript <code>DataView</code> objects are described in
2975<a href="https://tc39.github.io/ecma262/#sec-dataview-objects">Section 24.3</a> of the ECMAScript Language Specification.</p>
2976<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>
2977<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>
2978<div class="api_metadata">
2979<span>Added in: v8.4.0</span>
2980<span>N-API version: 1</span>
2981</div>
2982<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>
2983<ul>
2984<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2985<li><code>[in] value</code>: Integer value to be represented in JavaScript.</li>
2986<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li>
2987</ul>
2988<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2989<p>This API is used to convert from the C <code>int32_t</code> type to the JavaScript
2990<code>number</code> type.</p>
2991<p>The JavaScript <code>number</code> type is described in
2992<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a> of the ECMAScript Language Specification.</p>
2993<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>
2994<div class="api_metadata">
2995<span>Added in: v8.4.0</span>
2996<span>N-API version: 1</span>
2997</div>
2998<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>
2999<ul>
3000<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3001<li><code>[in] value</code>: Unsigned integer value to be represented in JavaScript.</li>
3002<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li>
3003</ul>
3004<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3005<p>This API is used to convert from the C <code>uint32_t</code> type to the JavaScript
3006<code>number</code> type.</p>
3007<p>The JavaScript <code>number</code> type is described in
3008<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a> of the ECMAScript Language Specification.</p>
3009<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>
3010<div class="api_metadata">
3011<span>Added in: v8.4.0</span>
3012<span>N-API version: 1</span>
3013</div>
3014<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>
3015<ul>
3016<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3017<li><code>[in] value</code>: Integer value to be represented in JavaScript.</li>
3018<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li>
3019</ul>
3020<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3021<p>This API is used to convert from the C <code>int64_t</code> type to the JavaScript
3022<code>number</code> type.</p>
3023<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>
3024of the ECMAScript Language Specification. Note the complete range of <code>int64_t</code>
3025cannot be represented with full precision in JavaScript. Integer values
3026outside 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> -
3027<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>
3028<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>
3029<div class="api_metadata">
3030<span>Added in: v8.4.0</span>
3031<span>N-API version: 1</span>
3032</div>
3033<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>
3034<ul>
3035<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3036<li><code>[in] value</code>: Double-precision value to be represented in JavaScript.</li>
3037<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li>
3038</ul>
3039<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3040<p>This API is used to convert from the C <code>double</code> type to the JavaScript
3041<code>number</code> type.</p>
3042<p>The JavaScript <code>number</code> type is described in
3043<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a> of the ECMAScript Language Specification.</p>
3044<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>
3045<div class="api_metadata">
3046<span>Added in: v10.7.0</span>
3047<span>N-API version: 6</span>
3048</div>
3049<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_int64</span><span class="hljs-params">(napi_env env,
3050                                     <span class="hljs-type">int64_t</span> value,
3051                                     napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
3052<ul>
3053<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3054<li><code>[in] value</code>: Integer value to be represented in JavaScript.</li>
3055<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>BigInt</code>.</li>
3056</ul>
3057<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3058<p>This API converts the C <code>int64_t</code> type to the JavaScript <code>BigInt</code> type.</p>
3059<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>
3060<div class="api_metadata">
3061<span>Added in: v10.7.0</span>
3062<span>N-API version: 6</span>
3063</div>
3064<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_uint64</span><span class="hljs-params">(napi_env env,
3065                                      <span class="hljs-type">uint64_t</span> value,
3066                                      napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
3067<ul>
3068<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3069<li><code>[in] value</code>: Unsigned integer value to be represented in JavaScript.</li>
3070<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>BigInt</code>.</li>
3071</ul>
3072<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3073<p>This API converts the C <code>uint64_t</code> type to the JavaScript <code>BigInt</code> type.</p>
3074<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>
3075<div class="api_metadata">
3076<span>Added in: v10.7.0</span>
3077<span>N-API version: 6</span>
3078</div>
3079<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_words</span><span class="hljs-params">(napi_env env,
3080                                     <span class="hljs-type">int</span> sign_bit,
3081                                     <span class="hljs-type">size_t</span> word_count,
3082                                     <span class="hljs-type">const</span> <span class="hljs-type">uint64_t</span>* words,
3083                                     napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
3084<ul>
3085<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3086<li><code>[in] sign_bit</code>: Determines if the resulting <code>BigInt</code> will be positive or
3087negative.</li>
3088<li><code>[in] word_count</code>: The length of the <code>words</code> array.</li>
3089<li><code>[in] words</code>: An array of <code>uint64_t</code> little-endian 64-bit words.</li>
3090<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>BigInt</code>.</li>
3091</ul>
3092<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3093<p>This API converts an array of unsigned 64-bit words into a single <code>BigInt</code>
3094value.</p>
3095<p>The resulting <code>BigInt</code> is calculated as: (–1)<sup><code>sign_bit</code></sup> (<code>words[0]</code>
3096× (2<sup>64</sup>)<sup>0</sup> + <code>words[1]</code> × (2<sup>64</sup>)<sup>1</sup> + …)</p>
3097<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>
3098<div class="api_metadata">
3099<span>Added in: v8.0.0</span>
3100<span>N-API version: 1</span>
3101</div>
3102<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_latin1</span><span class="hljs-params">(napi_env env,
3103                                      <span class="hljs-type">const</span> <span class="hljs-type">char</span>* str,
3104                                      <span class="hljs-type">size_t</span> length,
3105                                      napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
3106<ul>
3107<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3108<li><code>[in] str</code>: Character buffer representing an ISO-8859-1-encoded string.</li>
3109<li><code>[in] length</code>: The length of the string in bytes, or <code>NAPI_AUTO_LENGTH</code> if it
3110is null-terminated.</li>
3111<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li>
3112</ul>
3113<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3114<p>This API creates a JavaScript <code>string</code> value from an ISO-8859-1-encoded C
3115string. The native string is copied.</p>
3116<p>The JavaScript <code>string</code> type is described in
3117<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p>
3118<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>
3119<div class="api_metadata">
3120<span>Added in: v18.18.0</span>
3121</div>
3122<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
3123<pre><code class="language-c">napi_status
3124<span class="hljs-title function_">node_api_create_external_string_latin1</span><span class="hljs-params">(napi_env env,
3125                                       <span class="hljs-type">char</span>* str,
3126                                       <span class="hljs-type">size_t</span> length,
3127                                       napi_finalize finalize_callback,
3128                                       <span class="hljs-type">void</span>* finalize_hint,
3129                                       napi_value* result,
3130                                       <span class="hljs-type">bool</span>* copied)</span>;</code> <button class="copy-button">copy</button></pre>
3131<ul>
3132<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3133<li><code>[in] str</code>: Character buffer representing an ISO-8859-1-encoded string.</li>
3134<li><code>[in] length</code>: The length of the string in bytes, or <code>NAPI_AUTO_LENGTH</code> if it
3135is null-terminated.</li>
3136<li><code>[in] finalize_callback</code>: The function to call when the string is being
3137collected. The function will be called with the following parameters:
3138<ul>
3139<li><code>[in] env</code>: The environment in which the add-on is running. This value
3140may be null if the string is being collected as part of the termination
3141of the worker or the main Node.js instance.</li>
3142<li><code>[in] data</code>: This is the value <code>str</code> as a <code>void*</code> pointer.</li>
3143<li><code>[in] finalize_hint</code>: This is the value <code>finalize_hint</code> that was given
3144to the API.
3145<a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.
3146This parameter is optional. Passing a null value means that the add-on
3147doesn't need to be notified when the corresponding JavaScript string is
3148collected.</li>
3149</ul>
3150</li>
3151<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
3152collection.</li>
3153<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li>
3154<li><code>[out] copied</code>: Whether the string was copied. If it was, the finalizer will
3155already have been invoked to destroy <code>str</code>.</li>
3156</ul>
3157<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3158<p>This API creates a JavaScript <code>string</code> value from an ISO-8859-1-encoded C
3159string. The native string may not be copied and must thus exist for the entire
3160life cycle of the JavaScript value.</p>
3161<p>The JavaScript <code>string</code> type is described in
3162<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p>
3163<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>
3164<div class="api_metadata">
3165<span>Added in: v8.0.0</span>
3166<span>N-API version: 1</span>
3167</div>
3168<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf16</span><span class="hljs-params">(napi_env env,
3169                                     <span class="hljs-type">const</span> <span class="hljs-type">char16_t</span>* str,
3170                                     <span class="hljs-type">size_t</span> length,
3171                                     napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3172<ul>
3173<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3174<li><code>[in] str</code>: Character buffer representing a UTF16-LE-encoded string.</li>
3175<li><code>[in] length</code>: The length of the string in two-byte code units, or
3176<code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li>
3177<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li>
3178</ul>
3179<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3180<p>This API creates a JavaScript <code>string</code> value from a UTF16-LE-encoded C string.
3181The native string is copied.</p>
3182<p>The JavaScript <code>string</code> type is described in
3183<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p>
3184<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>
3185<div class="api_metadata">
3186<span>Added in: v18.18.0</span>
3187</div>
3188<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
3189<pre><code class="language-c">napi_status
3190<span class="hljs-title function_">node_api_create_external_string_utf16</span><span class="hljs-params">(napi_env env,
3191                                      <span class="hljs-type">char16_t</span>* str,
3192                                      <span class="hljs-type">size_t</span> length,
3193                                      napi_finalize finalize_callback,
3194                                      <span class="hljs-type">void</span>* finalize_hint,
3195                                      napi_value* result,
3196                                      <span class="hljs-type">bool</span>* copied)</span>;</code> <button class="copy-button">copy</button></pre>
3197<ul>
3198<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3199<li><code>[in] str</code>: Character buffer representing a UTF16-LE-encoded string.</li>
3200<li><code>[in] length</code>: The length of the string in two-byte code units, or
3201<code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li>
3202<li><code>[in] finalize_callback</code>: The function to call when the string is being
3203collected. The function will be called with the following parameters:
3204<ul>
3205<li><code>[in] env</code>: The environment in which the add-on is running. This value
3206may be null if the string is being collected as part of the termination
3207of the worker or the main Node.js instance.</li>
3208<li><code>[in] data</code>: This is the value <code>str</code> as a <code>void*</code> pointer.</li>
3209<li><code>[in] finalize_hint</code>: This is the value <code>finalize_hint</code> that was given
3210to the API.
3211<a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.
3212This parameter is optional. Passing a null value means that the add-on
3213doesn't need to be notified when the corresponding JavaScript string is
3214collected.</li>
3215</ul>
3216</li>
3217<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
3218collection.</li>
3219<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li>
3220<li><code>[out] copied</code>: Whether the string was copied. If it was, the finalizer will
3221already have been invoked to destroy <code>str</code>.</li>
3222</ul>
3223<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3224<p>This API creates a JavaScript <code>string</code> value from a UTF16-LE-encoded C string.
3225The native string may not be copied and must thus exist for the entire life
3226cycle of the JavaScript value.</p>
3227<p>The JavaScript <code>string</code> type is described in
3228<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p>
3229<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>
3230<div class="api_metadata">
3231<span>Added in: v8.0.0</span>
3232<span>N-API version: 1</span>
3233</div>
3234<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf8</span><span class="hljs-params">(napi_env env,
3235                                    <span class="hljs-type">const</span> <span class="hljs-type">char</span>* str,
3236                                    <span class="hljs-type">size_t</span> length,
3237                                    napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3238<ul>
3239<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3240<li><code>[in] str</code>: Character buffer representing a UTF8-encoded string.</li>
3241<li><code>[in] length</code>: The length of the string in bytes, or <code>NAPI_AUTO_LENGTH</code> if it
3242is null-terminated.</li>
3243<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li>
3244</ul>
3245<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3246<p>This API creates a JavaScript <code>string</code> value from a UTF8-encoded C string.
3247The native string is copied.</p>
3248<p>The JavaScript <code>string</code> type is described in
3249<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p>
3250<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>
3251<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>
3252<div class="api_metadata">
3253<span>Added in: v8.0.0</span>
3254<span>N-API version: 1</span>
3255</div>
3256<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_array_length</span><span class="hljs-params">(napi_env env,
3257                                  napi_value value,
3258                                  <span class="hljs-type">uint32_t</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3259<ul>
3260<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3261<li><code>[in] value</code>: <code>napi_value</code> representing the JavaScript <code>Array</code> whose length is
3262being queried.</li>
3263<li><code>[out] result</code>: <code>uint32</code> representing length of the array.</li>
3264</ul>
3265<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3266<p>This API returns the length of an array.</p>
3267<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
3268Specification.</p>
3269<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>
3270<div class="api_metadata">
3271<span>Added in: v8.0.0</span>
3272<span>N-API version: 1</span>
3273</div>
3274<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_arraybuffer_info</span><span class="hljs-params">(napi_env env,
3275                                      napi_value arraybuffer,
3276                                      <span class="hljs-type">void</span>** data,
3277                                      <span class="hljs-type">size_t</span>* byte_length)</span></code> <button class="copy-button">copy</button></pre>
3278<ul>
3279<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3280<li><code>[in] arraybuffer</code>: <code>napi_value</code> representing the <code>ArrayBuffer</code> being queried.</li>
3281<li><code>[out] data</code>: The underlying data buffer of the <code>ArrayBuffer</code>. If byte_length
3282is <code>0</code>, this may be <code>NULL</code> or any other pointer value.</li>
3283<li><code>[out] byte_length</code>: Length in bytes of the underlying data buffer.</li>
3284</ul>
3285<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3286<p>This API is used to retrieve the underlying data buffer of an <code>ArrayBuffer</code> and
3287its length.</p>
3288<p><em>WARNING</em>: Use caution while using this API. The lifetime of the underlying data
3289buffer is managed by the <code>ArrayBuffer</code> even after it's returned. A
3290possible safe way to use this API is in conjunction with
3291<a href="#napi_create_reference"><code>napi_create_reference</code></a>, which can be used to guarantee control over the
3292lifetime of the <code>ArrayBuffer</code>. It's also safe to use the returned data buffer
3293within the same callback as long as there are no calls to other APIs that might
3294trigger a GC.</p>
3295<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>
3296<div class="api_metadata">
3297<span>Added in: v8.0.0</span>
3298<span>N-API version: 1</span>
3299</div>
3300<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_buffer_info</span><span class="hljs-params">(napi_env env,
3301                                 napi_value value,
3302                                 <span class="hljs-type">void</span>** data,
3303                                 <span class="hljs-type">size_t</span>* length)</span></code> <button class="copy-button">copy</button></pre>
3304<ul>
3305<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3306<li><code>[in] value</code>: <code>napi_value</code> representing the <code>node::Buffer</code> or <code>Uint8Array</code>
3307being queried.</li>
3308<li><code>[out] data</code>: The underlying data buffer of the <code>node::Buffer</code> or
3309<code>Uint8Array</code>. If length is <code>0</code>, this may be <code>NULL</code> or any other pointer value.</li>
3310<li><code>[out] length</code>: Length in bytes of the underlying data buffer.</li>
3311</ul>
3312<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3313<p>This method returns the identical <code>data</code> and <code>byte_length</code> as
3314<a href="#napi_get_typedarray_info"><code>napi_get_typedarray_info</code></a>. And <code>napi_get_typedarray_info</code> accepts a
3315<code>node::Buffer</code> (a Uint8Array) as the value too.</p>
3316<p>This API is used to retrieve the underlying data buffer of a <code>node::Buffer</code>
3317and its length.</p>
3318<p><em>Warning</em>: Use caution while using this API since the underlying data buffer's
3319lifetime is not guaranteed if it's managed by the VM.</p>
3320<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>
3321<div class="api_metadata">
3322<span>Added in: v8.0.0</span>
3323<span>N-API version: 1</span>
3324</div>
3325<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_prototype</span><span class="hljs-params">(napi_env env,
3326                               napi_value object,
3327                               napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3328<ul>
3329<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3330<li><code>[in] object</code>: <code>napi_value</code> representing JavaScript <code>Object</code> whose prototype
3331to return. This returns the equivalent of <code>Object.getPrototypeOf</code> (which is
3332not the same as the function's <code>prototype</code> property).</li>
3333<li><code>[out] result</code>: <code>napi_value</code> representing prototype of the given object.</li>
3334</ul>
3335<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3336<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>
3337<div class="api_metadata">
3338<span>Added in: v8.0.0</span>
3339<span>N-API version: 1</span>
3340</div>
3341<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_typedarray_info</span><span class="hljs-params">(napi_env env,
3342                                     napi_value typedarray,
3343                                     napi_typedarray_type* type,
3344                                     <span class="hljs-type">size_t</span>* length,
3345                                     <span class="hljs-type">void</span>** data,
3346                                     napi_value* arraybuffer,
3347                                     <span class="hljs-type">size_t</span>* byte_offset)</span></code> <button class="copy-button">copy</button></pre>
3348<ul>
3349<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3350<li><code>[in] typedarray</code>: <code>napi_value</code> representing the <code>TypedArray</code> whose
3351properties to query.</li>
3352<li><code>[out] type</code>: Scalar datatype of the elements within the <code>TypedArray</code>.</li>
3353<li><code>[out] length</code>: The number of elements in the <code>TypedArray</code>.</li>
3354<li><code>[out] data</code>: The data buffer underlying the <code>TypedArray</code> adjusted by
3355the <code>byte_offset</code> value so that it points to the first element in the
3356<code>TypedArray</code>. If the length of the array is <code>0</code>, this may be <code>NULL</code> or
3357any other pointer value.</li>
3358<li><code>[out] arraybuffer</code>: The <code>ArrayBuffer</code> underlying the <code>TypedArray</code>.</li>
3359<li><code>[out] byte_offset</code>: The byte offset within the underlying native array
3360at which the first element of the arrays is located. The value for the data
3361parameter has already been adjusted so that data points to the first element
3362in the array. Therefore, the first byte of the native array would be at
3363<code>data - byte_offset</code>.</li>
3364</ul>
3365<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3366<p>This API returns various properties of a typed array.</p>
3367<p>Any of the out parameters may be <code>NULL</code> if that property is unneeded.</p>
3368<p><em>Warning</em>: Use caution while using this API since the underlying data buffer
3369is managed by the VM.</p>
3370<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>
3371<div class="api_metadata">
3372<span>Added in: v8.3.0</span>
3373<span>N-API version: 1</span>
3374</div>
3375<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_dataview_info</span><span class="hljs-params">(napi_env env,
3376                                   napi_value dataview,
3377                                   <span class="hljs-type">size_t</span>* byte_length,
3378                                   <span class="hljs-type">void</span>** data,
3379                                   napi_value* arraybuffer,
3380                                   <span class="hljs-type">size_t</span>* byte_offset)</span></code> <button class="copy-button">copy</button></pre>
3381<ul>
3382<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3383<li><code>[in] dataview</code>: <code>napi_value</code> representing the <code>DataView</code> whose
3384properties to query.</li>
3385<li><code>[out] byte_length</code>: Number of bytes in the <code>DataView</code>.</li>
3386<li><code>[out] data</code>: The data buffer underlying the <code>DataView</code>.
3387If byte_length is <code>0</code>, this may be <code>NULL</code> or any other pointer value.</li>
3388<li><code>[out] arraybuffer</code>: <code>ArrayBuffer</code> underlying the <code>DataView</code>.</li>
3389<li><code>[out] byte_offset</code>: The byte offset within the data buffer from which
3390to start projecting the <code>DataView</code>.</li>
3391</ul>
3392<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3393<p>Any of the out parameters may be <code>NULL</code> if that property is unneeded.</p>
3394<p>This API returns various properties of a <code>DataView</code>.</p>
3395<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>
3396<div class="api_metadata">
3397<span>Added in: v11.11.0, v10.17.0</span>
3398<span>N-API version: 5</span>
3399</div>
3400<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_date_value</span><span class="hljs-params">(napi_env env,
3401                                napi_value value,
3402                                <span class="hljs-type">double</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3403<ul>
3404<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3405<li><code>[in] value</code>: <code>napi_value</code> representing a JavaScript <code>Date</code>.</li>
3406<li><code>[out] result</code>: Time value as a <code>double</code> represented as milliseconds since
3407midnight at the beginning of 01 January, 1970 UTC.</li>
3408</ul>
3409<p>This API does not observe leap seconds; they are ignored, as
3410ECMAScript aligns with POSIX time specification.</p>
3411<p>Returns <code>napi_ok</code> if the API succeeded. If a non-date <code>napi_value</code> is passed
3412in it returns <code>napi_date_expected</code>.</p>
3413<p>This API returns the C double primitive of time value for the given JavaScript
3414<code>Date</code>.</p>
3415<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>
3416<div class="api_metadata">
3417<span>Added in: v8.0.0</span>
3418<span>N-API version: 1</span>
3419</div>
3420<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>
3421<ul>
3422<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3423<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>Boolean</code>.</li>
3424<li><code>[out] result</code>: C boolean primitive equivalent of the given JavaScript
3425<code>Boolean</code>.</li>
3426</ul>
3427<p>Returns <code>napi_ok</code> if the API succeeded. If a non-boolean <code>napi_value</code> is
3428passed in it returns <code>napi_boolean_expected</code>.</p>
3429<p>This API returns the C boolean primitive equivalent of the given JavaScript
3430<code>Boolean</code>.</p>
3431<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>
3432<div class="api_metadata">
3433<span>Added in: v8.0.0</span>
3434<span>N-API version: 1</span>
3435</div>
3436<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_double</span><span class="hljs-params">(napi_env env,
3437                                  napi_value value,
3438                                  <span class="hljs-type">double</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3439<ul>
3440<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3441<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li>
3442<li><code>[out] result</code>: C double primitive equivalent of the given JavaScript
3443<code>number</code>.</li>
3444</ul>
3445<p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code> is passed
3446in it returns <code>napi_number_expected</code>.</p>
3447<p>This API returns the C double primitive equivalent of the given JavaScript
3448<code>number</code>.</p>
3449<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>
3450<div class="api_metadata">
3451<span>Added in: v10.7.0</span>
3452<span>N-API version: 6</span>
3453</div>
3454<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,
3455                                        napi_value value,
3456                                        <span class="hljs-type">int64_t</span>* result,
3457                                        <span class="hljs-type">bool</span>* lossless)</span>;</code> <button class="copy-button">copy</button></pre>
3458<ul>
3459<li><code>[in] env</code>: The environment that the API is invoked under</li>
3460<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>BigInt</code>.</li>
3461<li><code>[out] result</code>: C <code>int64_t</code> primitive equivalent of the given JavaScript
3462<code>BigInt</code>.</li>
3463<li><code>[out] lossless</code>: Indicates whether the <code>BigInt</code> value was converted
3464losslessly.</li>
3465</ul>
3466<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it
3467returns <code>napi_bigint_expected</code>.</p>
3468<p>This API returns the C <code>int64_t</code> primitive equivalent of the given JavaScript
3469<code>BigInt</code>. If needed it will truncate the value, setting <code>lossless</code> to <code>false</code>.</p>
3470<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>
3471<div class="api_metadata">
3472<span>Added in: v10.7.0</span>
3473<span>N-API version: 6</span>
3474</div>
3475<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,
3476                                        napi_value value,
3477                                        <span class="hljs-type">uint64_t</span>* result,
3478                                        <span class="hljs-type">bool</span>* lossless)</span>;</code> <button class="copy-button">copy</button></pre>
3479<ul>
3480<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3481<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>BigInt</code>.</li>
3482<li><code>[out] result</code>: C <code>uint64_t</code> primitive equivalent of the given JavaScript
3483<code>BigInt</code>.</li>
3484<li><code>[out] lossless</code>: Indicates whether the <code>BigInt</code> value was converted
3485losslessly.</li>
3486</ul>
3487<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it
3488returns <code>napi_bigint_expected</code>.</p>
3489<p>This API returns the C <code>uint64_t</code> primitive equivalent of the given JavaScript
3490<code>BigInt</code>. If needed it will truncate the value, setting <code>lossless</code> to <code>false</code>.</p>
3491<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>
3492<div class="api_metadata">
3493<span>Added in: v10.7.0</span>
3494<span>N-API version: 6</span>
3495</div>
3496<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,
3497                                        napi_value value,
3498                                        <span class="hljs-type">int</span>* sign_bit,
3499                                        <span class="hljs-type">size_t</span>* word_count,
3500                                        <span class="hljs-type">uint64_t</span>* words)</span>;</code> <button class="copy-button">copy</button></pre>
3501<ul>
3502<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3503<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>BigInt</code>.</li>
3504<li><code>[out] sign_bit</code>: Integer representing if the JavaScript <code>BigInt</code> is positive
3505or negative.</li>
3506<li><code>[in/out] word_count</code>: Must be initialized to the length of the <code>words</code>
3507array. Upon return, it will be set to the actual number of words that
3508would be needed to store this <code>BigInt</code>.</li>
3509<li><code>[out] words</code>: Pointer to a pre-allocated 64-bit word array.</li>
3510</ul>
3511<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3512<p>This API converts a single <code>BigInt</code> value into a sign bit, 64-bit little-endian
3513array, and the number of elements in the array. <code>sign_bit</code> and <code>words</code> may be
3514both set to <code>NULL</code>, in order to get only <code>word_count</code>.</p>
3515<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>
3516<div class="api_metadata">
3517<span>Added in: v8.0.0</span>
3518<span>N-API version: 1</span>
3519</div>
3520<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_external</span><span class="hljs-params">(napi_env env,
3521                                    napi_value value,
3522                                    <span class="hljs-type">void</span>** result)</span></code> <button class="copy-button">copy</button></pre>
3523<ul>
3524<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3525<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript external value.</li>
3526<li><code>[out] result</code>: Pointer to the data wrapped by the JavaScript external value.</li>
3527</ul>
3528<p>Returns <code>napi_ok</code> if the API succeeded. If a non-external <code>napi_value</code> is
3529passed in it returns <code>napi_invalid_arg</code>.</p>
3530<p>This API retrieves the external data pointer that was previously passed to
3531<code>napi_create_external()</code>.</p>
3532<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>
3533<div class="api_metadata">
3534<span>Added in: v8.0.0</span>
3535<span>N-API version: 1</span>
3536</div>
3537<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int32</span><span class="hljs-params">(napi_env env,
3538                                 napi_value value,
3539                                 <span class="hljs-type">int32_t</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3540<ul>
3541<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3542<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li>
3543<li><code>[out] result</code>: C <code>int32</code> primitive equivalent of the given JavaScript
3544<code>number</code>.</li>
3545</ul>
3546<p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
3547is passed in <code>napi_number_expected</code>.</p>
3548<p>This API returns the C <code>int32</code> primitive equivalent
3549of the given JavaScript <code>number</code>.</p>
3550<p>If the number exceeds the range of the 32 bit integer, then the result is
3551truncated to the equivalent of the bottom 32 bits. This can result in a large
3552positive number becoming a negative number if the value is > 2<sup>31</sup> - 1.</p>
3553<p>Non-finite number values (<code>NaN</code>, <code>+Infinity</code>, or <code>-Infinity</code>) set the
3554result to zero.</p>
3555<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>
3556<div class="api_metadata">
3557<span>Added in: v8.0.0</span>
3558<span>N-API version: 1</span>
3559</div>
3560<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int64</span><span class="hljs-params">(napi_env env,
3561                                 napi_value value,
3562                                 <span class="hljs-type">int64_t</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3563<ul>
3564<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3565<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li>
3566<li><code>[out] result</code>: C <code>int64</code> primitive equivalent of the given JavaScript
3567<code>number</code>.</li>
3568</ul>
3569<p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
3570is passed in it returns <code>napi_number_expected</code>.</p>
3571<p>This API returns the C <code>int64</code> primitive equivalent of the given JavaScript
3572<code>number</code>.</p>
3573<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>
3574<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
3575precision.</p>
3576<p>Non-finite number values (<code>NaN</code>, <code>+Infinity</code>, or <code>-Infinity</code>) set the
3577result to zero.</p>
3578<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>
3579<div class="api_metadata">
3580<span>Added in: v8.0.0</span>
3581<span>N-API version: 1</span>
3582</div>
3583<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,
3584                                         napi_value value,
3585                                         <span class="hljs-type">char</span>* buf,
3586                                         <span class="hljs-type">size_t</span> bufsize,
3587                                         <span class="hljs-type">size_t</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3588<ul>
3589<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3590<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript string.</li>
3591<li><code>[in] buf</code>: Buffer to write the ISO-8859-1-encoded string into. If <code>NULL</code> is
3592passed in, the length of the string in bytes and excluding the null terminator
3593is returned in <code>result</code>.</li>
3594<li><code>[in] bufsize</code>: Size of the destination buffer. When this value is
3595insufficient, the returned string is truncated and null-terminated.</li>
3596<li><code>[out] result</code>: Number of bytes copied into the buffer, excluding the null
3597terminator.</li>
3598</ul>
3599<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value</code>
3600is passed in it returns <code>napi_string_expected</code>.</p>
3601<p>This API returns the ISO-8859-1-encoded string corresponding the value passed
3602in.</p>
3603<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>
3604<div class="api_metadata">
3605<span>Added in: v8.0.0</span>
3606<span>N-API version: 1</span>
3607</div>
3608<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,
3609                                       napi_value value,
3610                                       <span class="hljs-type">char</span>* buf,
3611                                       <span class="hljs-type">size_t</span> bufsize,
3612                                       <span class="hljs-type">size_t</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3613<ul>
3614<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3615<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript string.</li>
3616<li><code>[in] buf</code>: Buffer to write the UTF8-encoded string into. If <code>NULL</code> is passed
3617in, the length of the string in bytes and excluding the null terminator is
3618returned in <code>result</code>.</li>
3619<li><code>[in] bufsize</code>: Size of the destination buffer. When this value is
3620insufficient, the returned string is truncated and null-terminated.</li>
3621<li><code>[out] result</code>: Number of bytes copied into the buffer, excluding the null
3622terminator.</li>
3623</ul>
3624<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value</code>
3625is passed in it returns <code>napi_string_expected</code>.</p>
3626<p>This API returns the UTF8-encoded string corresponding the value passed in.</p>
3627<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>
3628<div class="api_metadata">
3629<span>Added in: v8.0.0</span>
3630<span>N-API version: 1</span>
3631</div>
3632<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,
3633                                        napi_value value,
3634                                        <span class="hljs-type">char16_t</span>* buf,
3635                                        <span class="hljs-type">size_t</span> bufsize,
3636                                        <span class="hljs-type">size_t</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3637<ul>
3638<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3639<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript string.</li>
3640<li><code>[in] buf</code>: Buffer to write the UTF16-LE-encoded string into. If <code>NULL</code> is
3641passed in, the length of the string in 2-byte code units and excluding the
3642null terminator is returned.</li>
3643<li><code>[in] bufsize</code>: Size of the destination buffer. When this value is
3644insufficient, the returned string is truncated and null-terminated.</li>
3645<li><code>[out] result</code>: Number of 2-byte code units copied into the buffer, excluding
3646the null terminator.</li>
3647</ul>
3648<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value</code>
3649is passed in it returns <code>napi_string_expected</code>.</p>
3650<p>This API returns the UTF16-encoded string corresponding the value passed in.</p>
3651<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>
3652<div class="api_metadata">
3653<span>Added in: v8.0.0</span>
3654<span>N-API version: 1</span>
3655</div>
3656<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_uint32</span><span class="hljs-params">(napi_env env,
3657                                  napi_value value,
3658                                  <span class="hljs-type">uint32_t</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3659<ul>
3660<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3661<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li>
3662<li><code>[out] result</code>: C primitive equivalent of the given <code>napi_value</code> as a
3663<code>uint32_t</code>.</li>
3664</ul>
3665<p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
3666is passed in it returns <code>napi_number_expected</code>.</p>
3667<p>This API returns the C primitive equivalent of the given <code>napi_value</code> as a
3668<code>uint32_t</code>.</p>
3669<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>
3670<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>
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_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>
3676<ul>
3677<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3678<li><code>[in] value</code>: The value of the boolean to retrieve.</li>
3679<li><code>[out] result</code>: <code>napi_value</code> representing JavaScript <code>Boolean</code> singleton to
3680retrieve.</li>
3681</ul>
3682<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3683<p>This API is used to return the JavaScript singleton object that is used to
3684represent the given boolean value.</p>
3685<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>
3686<div class="api_metadata">
3687<span>Added in: v8.0.0</span>
3688<span>N-API version: 1</span>
3689</div>
3690<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>
3691<ul>
3692<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3693<li><code>[out] result</code>: <code>napi_value</code> representing JavaScript <code>global</code> object.</li>
3694</ul>
3695<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3696<p>This API returns the <code>global</code> object.</p>
3697<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>
3698<div class="api_metadata">
3699<span>Added in: v8.0.0</span>
3700<span>N-API version: 1</span>
3701</div>
3702<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>
3703<ul>
3704<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3705<li><code>[out] result</code>: <code>napi_value</code> representing JavaScript <code>null</code> object.</li>
3706</ul>
3707<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3708<p>This API returns the <code>null</code> object.</p>
3709<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>
3710<div class="api_metadata">
3711<span>Added in: v8.0.0</span>
3712<span>N-API version: 1</span>
3713</div>
3714<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>
3715<ul>
3716<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3717<li><code>[out] result</code>: <code>napi_value</code> representing JavaScript Undefined value.</li>
3718</ul>
3719<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3720<p>This API returns the Undefined object.</p>
3721</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>
3722<p>Node-API exposes a set of APIs to perform some abstract operations on JavaScript
3723values. Some of these operations are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Section 7</a>
3724of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p>
3725<p>These APIs support doing one of the following:</p>
3726<ol>
3727<li>Coerce JavaScript values to specific JavaScript types (such as <code>number</code> or
3728<code>string</code>).</li>
3729<li>Check the type of a JavaScript value.</li>
3730<li>Check for equality between two JavaScript values.</li>
3731</ol>
3732<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>
3733<div class="api_metadata">
3734<span>Added in: v8.0.0</span>
3735<span>N-API version: 1</span>
3736</div>
3737<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_bool</span><span class="hljs-params">(napi_env env,
3738                                napi_value value,
3739                                napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3740<ul>
3741<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3742<li><code>[in] value</code>: The JavaScript value to coerce.</li>
3743<li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>Boolean</code>.</li>
3744</ul>
3745<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3746<p>This API implements the abstract operation <code>ToBoolean()</code> as defined in
3747<a href="https://tc39.github.io/ecma262/#sec-toboolean">Section 7.1.2</a> of the ECMAScript Language Specification.</p>
3748<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>
3749<div class="api_metadata">
3750<span>Added in: v8.0.0</span>
3751<span>N-API version: 1</span>
3752</div>
3753<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_number</span><span class="hljs-params">(napi_env env,
3754                                  napi_value value,
3755                                  napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3756<ul>
3757<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3758<li><code>[in] value</code>: The JavaScript value to coerce.</li>
3759<li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>number</code>.</li>
3760</ul>
3761<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3762<p>This API implements the abstract operation <code>ToNumber()</code> as defined in
3763<a href="https://tc39.github.io/ecma262/#sec-tonumber">Section 7.1.3</a> of the ECMAScript Language Specification.
3764This function potentially runs JS code if the passed-in value is an
3765object.</p>
3766<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>
3767<div class="api_metadata">
3768<span>Added in: v8.0.0</span>
3769<span>N-API version: 1</span>
3770</div>
3771<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_object</span><span class="hljs-params">(napi_env env,
3772                                  napi_value value,
3773                                  napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3774<ul>
3775<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3776<li><code>[in] value</code>: The JavaScript value to coerce.</li>
3777<li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>Object</code>.</li>
3778</ul>
3779<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3780<p>This API implements the abstract operation <code>ToObject()</code> as defined in
3781<a href="https://tc39.github.io/ecma262/#sec-toobject">Section 7.1.13</a> of the ECMAScript Language Specification.</p>
3782<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>
3783<div class="api_metadata">
3784<span>Added in: v8.0.0</span>
3785<span>N-API version: 1</span>
3786</div>
3787<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_string</span><span class="hljs-params">(napi_env env,
3788                                  napi_value value,
3789                                  napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
3790<ul>
3791<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3792<li><code>[in] value</code>: The JavaScript value to coerce.</li>
3793<li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>string</code>.</li>
3794</ul>
3795<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3796<p>This API implements the abstract operation <code>ToString()</code> as defined in
3797<a href="https://tc39.github.io/ecma262/#sec-toobject">Section 7.1.13</a> of the ECMAScript Language Specification.
3798This function potentially runs JS code if the passed-in value is an
3799object.</p>
3800<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>
3801<div class="api_metadata">
3802<span>Added in: v8.0.0</span>
3803<span>N-API version: 1</span>
3804</div>
3805<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>
3806<ul>
3807<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3808<li><code>[in] value</code>: The JavaScript value whose type to query.</li>
3809<li><code>[out] result</code>: The type of the JavaScript value.</li>
3810</ul>
3811<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3812<ul>
3813<li><code>napi_invalid_arg</code> if the type of <code>value</code> is not a known ECMAScript type and
3814<code>value</code> is not an External value.</li>
3815</ul>
3816<p>This API represents behavior similar to invoking the <code>typeof</code> Operator on
3817the object as defined in <a href="https://tc39.github.io/ecma262/#sec-typeof-operator">Section 12.5.5</a> of the ECMAScript Language
3818Specification. However, there are some differences:</p>
3819<ol>
3820<li>It has support for detecting an External value.</li>
3821<li>It detects <code>null</code> as a separate type, while ECMAScript <code>typeof</code> would detect
3822<code>object</code>.</li>
3823</ol>
3824<p>If <code>value</code> has a type that is invalid, an error is returned.</p>
3825<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>
3826<div class="api_metadata">
3827<span>Added in: v8.0.0</span>
3828<span>N-API version: 1</span>
3829</div>
3830<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_instanceof</span><span class="hljs-params">(napi_env env,
3831                            napi_value object,
3832                            napi_value constructor,
3833                            <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3834<ul>
3835<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3836<li><code>[in] object</code>: The JavaScript value to check.</li>
3837<li><code>[in] constructor</code>: The JavaScript function object of the constructor function
3838to check against.</li>
3839<li><code>[out] result</code>: Boolean that is set to true if <code>object instanceof constructor</code>
3840is true.</li>
3841</ul>
3842<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3843<p>This API represents invoking the <code>instanceof</code> Operator on the object as
3844defined in <a href="https://tc39.github.io/ecma262/#sec-instanceofoperator">Section 12.10.4</a> of the ECMAScript Language Specification.</p>
3845<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>
3846<div class="api_metadata">
3847<span>Added in: v8.0.0</span>
3848<span>N-API version: 1</span>
3849</div>
3850<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>
3851<ul>
3852<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3853<li><code>[in] value</code>: The JavaScript value to check.</li>
3854<li><code>[out] result</code>: Whether the given object is an array.</li>
3855</ul>
3856<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3857<p>This API represents invoking the <code>IsArray</code> operation on the object
3858as defined in <a href="https://tc39.github.io/ecma262/#sec-isarray">Section 7.2.2</a> of the ECMAScript Language Specification.</p>
3859<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>
3860<div class="api_metadata">
3861<span>Added in: v8.0.0</span>
3862<span>N-API version: 1</span>
3863</div>
3864<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>
3865<ul>
3866<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3867<li><code>[in] value</code>: The JavaScript value to check.</li>
3868<li><code>[out] result</code>: Whether the given object is an <code>ArrayBuffer</code>.</li>
3869</ul>
3870<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3871<p>This API checks if the <code>Object</code> passed in is an array buffer.</p>
3872<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>
3873<div class="api_metadata">
3874<span>Added in: v8.0.0</span>
3875<span>N-API version: 1</span>
3876</div>
3877<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>
3878<ul>
3879<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3880<li><code>[in] value</code>: The JavaScript value to check.</li>
3881<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a <code>node::Buffer</code> or
3882<code>Uint8Array</code> object.</li>
3883</ul>
3884<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3885<p>This API checks if the <code>Object</code> passed in is a buffer or Uint8Array.
3886<a href="#napi_is_typedarray"><code>napi_is_typedarray</code></a> should be preferred if the caller needs to check if the
3887value is a Uint8Array.</p>
3888<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>
3889<div class="api_metadata">
3890<span>Added in: v11.11.0, v10.17.0</span>
3891<span>N-API version: 5</span>
3892</div>
3893<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>
3894<ul>
3895<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3896<li><code>[in] value</code>: The JavaScript value to check.</li>
3897<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a JavaScript <code>Date</code>
3898object.</li>
3899</ul>
3900<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3901<p>This API checks if the <code>Object</code> passed in is a date.</p>
3902<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>
3903<div class="api_metadata">
3904<span>Added in: v8.0.0</span>
3905<span>N-API version: 1</span>
3906</div>
3907<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>
3908<ul>
3909<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3910<li><code>[in] value</code>: The JavaScript value to check.</li>
3911<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents an <code>Error</code> object.</li>
3912</ul>
3913<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3914<p>This API checks if the <code>Object</code> passed in is an <code>Error</code>.</p>
3915<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>
3916<div class="api_metadata">
3917<span>Added in: v8.0.0</span>
3918<span>N-API version: 1</span>
3919</div>
3920<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>
3921<ul>
3922<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3923<li><code>[in] value</code>: The JavaScript value to check.</li>
3924<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a <code>TypedArray</code>.</li>
3925</ul>
3926<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3927<p>This API checks if the <code>Object</code> passed in is a typed array.</p>
3928<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>
3929<div class="api_metadata">
3930<span>Added in: v8.3.0</span>
3931<span>N-API version: 1</span>
3932</div>
3933<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>
3934<ul>
3935<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3936<li><code>[in] value</code>: The JavaScript value to check.</li>
3937<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a <code>DataView</code>.</li>
3938</ul>
3939<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3940<p>This API checks if the <code>Object</code> passed in is a <code>DataView</code>.</p>
3941<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>
3942<div class="api_metadata">
3943<span>Added in: v8.0.0</span>
3944<span>N-API version: 1</span>
3945</div>
3946<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_strict_equals</span><span class="hljs-params">(napi_env env,
3947                               napi_value lhs,
3948                               napi_value rhs,
3949                               <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3950<ul>
3951<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3952<li><code>[in] lhs</code>: The JavaScript value to check.</li>
3953<li><code>[in] rhs</code>: The JavaScript value to check against.</li>
3954<li><code>[out] result</code>: Whether the two <code>napi_value</code> objects are equal.</li>
3955</ul>
3956<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3957<p>This API represents the invocation of the Strict Equality algorithm as
3958defined in <a href="https://tc39.github.io/ecma262/#sec-strict-equality-comparison">Section 7.2.14</a> of the ECMAScript Language Specification.</p>
3959<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>
3960<div class="api_metadata">
3961<span>Added in: v13.0.0, v12.16.0, v10.22.0</span>
3962<span>N-API version: 7</span>
3963</div>
3964<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_detach_arraybuffer</span><span class="hljs-params">(napi_env env,
3965                                    napi_value arraybuffer)</span></code> <button class="copy-button">copy</button></pre>
3966<ul>
3967<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3968<li><code>[in] arraybuffer</code>: The JavaScript <code>ArrayBuffer</code> to be detached.</li>
3969</ul>
3970<p>Returns <code>napi_ok</code> if the API succeeded. If a non-detachable <code>ArrayBuffer</code> is
3971passed in it returns <code>napi_detachable_arraybuffer_expected</code>.</p>
3972<p>Generally, an <code>ArrayBuffer</code> is non-detachable if it has been detached before.
3973The engine may impose additional conditions on whether an <code>ArrayBuffer</code> is
3974detachable. For example, V8 requires that the <code>ArrayBuffer</code> be external,
3975that is, created with <a href="#napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code></a>.</p>
3976<p>This API represents the invocation of the <code>ArrayBuffer</code> detach operation as
3977defined in <a href="https://tc39.es/ecma262/#sec-detacharraybuffer">Section 24.1.1.3</a> of the ECMAScript Language Specification.</p>
3978<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>
3979<div class="api_metadata">
3980<span>Added in: v13.3.0, v12.16.0, v10.22.0</span>
3981<span>N-API version: 7</span>
3982</div>
3983<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_detached_arraybuffer</span><span class="hljs-params">(napi_env env,
3984                                         napi_value arraybuffer,
3985                                         <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre>
3986<ul>
3987<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3988<li><code>[in] arraybuffer</code>: The JavaScript <code>ArrayBuffer</code> to be checked.</li>
3989<li><code>[out] result</code>: Whether the <code>arraybuffer</code> is detached.</li>
3990</ul>
3991<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3992<p>The <code>ArrayBuffer</code> is considered detached if its internal data is <code>null</code>.</p>
3993<p>This API represents the invocation of the <code>ArrayBuffer</code> <code>IsDetachedBuffer</code>
3994operation as defined in <a href="https://tc39.es/ecma262/#sec-isdetachedbuffer">Section 24.1.1.2</a> of the ECMAScript Language
3995Specification.</p>
3996</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>
3997<p>Node-API exposes a set of APIs to get and set properties on JavaScript
3998objects. Some of these types are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Section 7</a> of the
3999<a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p>
4000<p>Properties in JavaScript are represented as a tuple of a key and a value.
4001Fundamentally, all property keys in Node-API can be represented in one of the
4002following forms:</p>
4003<ul>
4004<li>Named: a simple UTF8-encoded string</li>
4005<li>Integer-Indexed: an index value represented by <code>uint32_t</code></li>
4006<li>JavaScript value: these are represented in Node-API by <code>napi_value</code>. This can
4007be a <code>napi_value</code> representing a <code>string</code>, <code>number</code>, or <code>symbol</code>.</li>
4008</ul>
4009<p>Node-API values are represented by the type <code>napi_value</code>.
4010Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>.
4011However, it's the caller's responsibility to make sure that the
4012<code>napi_value</code> in question is of the JavaScript type expected by the API.</p>
4013<p>The APIs documented in this section provide a simple interface to
4014get and set properties on arbitrary JavaScript objects represented by
4015<code>napi_value</code>.</p>
4016<p>For instance, consider the following JavaScript code snippet:</p>
4017<pre><code class="language-js"><span class="hljs-keyword">const</span> obj = {};
4018obj.<span class="hljs-property">myProp</span> = <span class="hljs-number">123</span>;</code> <button class="copy-button">copy</button></pre>
4019<p>The equivalent can be done using Node-API values with the following snippet:</p>
4020<pre><code class="language-c">napi_status status = napi_generic_failure;
4021
4022<span class="hljs-comment">// const obj = {}</span>
4023napi_value obj, value;
4024status = napi_create_object(env, &#x26;obj);
4025<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
4026
4027<span class="hljs-comment">// Create a napi_value for 123</span>
4028status = napi_create_int32(env, <span class="hljs-number">123</span>, &#x26;value);
4029<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
4030
4031<span class="hljs-comment">// obj.myProp = 123</span>
4032status = napi_set_named_property(env, obj, <span class="hljs-string">"myProp"</span>, value);
4033<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code> <button class="copy-button">copy</button></pre>
4034<p>Indexed properties can be set in a similar manner. Consider the following
4035JavaScript snippet:</p>
4036<pre><code class="language-js"><span class="hljs-keyword">const</span> arr = [];
4037arr[<span class="hljs-number">123</span>] = <span class="hljs-string">'hello'</span>;</code> <button class="copy-button">copy</button></pre>
4038<p>The equivalent can be done using Node-API values with the following snippet:</p>
4039<pre><code class="language-c">napi_status status = napi_generic_failure;
4040
4041<span class="hljs-comment">// const arr = [];</span>
4042napi_value arr, value;
4043status = napi_create_array(env, &#x26;arr);
4044<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
4045
4046<span class="hljs-comment">// Create a napi_value for 'hello'</span>
4047status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &#x26;value);
4048<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
4049
4050<span class="hljs-comment">// arr[123] = 'hello';</span>
4051status = napi_set_element(env, arr, <span class="hljs-number">123</span>, value);
4052<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code> <button class="copy-button">copy</button></pre>
4053<p>Properties can be retrieved using the APIs described in this section.
4054Consider the following JavaScript snippet:</p>
4055<pre><code class="language-js"><span class="hljs-keyword">const</span> arr = [];
4056<span class="hljs-keyword">const</span> value = arr[<span class="hljs-number">123</span>];</code> <button class="copy-button">copy</button></pre>
4057<p>The following is the approximate equivalent of the Node-API counterpart:</p>
4058<pre><code class="language-c">napi_status status = napi_generic_failure;
4059
4060<span class="hljs-comment">// const arr = []</span>
4061napi_value arr, value;
4062status = napi_create_array(env, &#x26;arr);
4063<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
4064
4065<span class="hljs-comment">// const value = arr[123]</span>
4066status = napi_get_element(env, arr, <span class="hljs-number">123</span>, &#x26;value);
4067<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code> <button class="copy-button">copy</button></pre>
4068<p>Finally, multiple properties can also be defined on an object for performance
4069reasons. Consider the following JavaScript:</p>
4070<pre><code class="language-js"><span class="hljs-keyword">const</span> obj = {};
4071<span class="hljs-title class_">Object</span>.<span class="hljs-title function_">defineProperties</span>(obj, {
4072  <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> },
4073  <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> },
4074});</code> <button class="copy-button">copy</button></pre>
4075<p>The following is the approximate equivalent of the Node-API counterpart:</p>
4076<pre><code class="language-c">napi_status status = napi_status_generic_failure;
4077
4078<span class="hljs-comment">// const obj = {};</span>
4079napi_value obj;
4080status = napi_create_object(env, &#x26;obj);
4081<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
4082
4083<span class="hljs-comment">// Create napi_values for 123 and 456</span>
4084napi_value fooValue, barValue;
4085status = napi_create_int32(env, <span class="hljs-number">123</span>, &#x26;fooValue);
4086<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
4087status = napi_create_int32(env, <span class="hljs-number">456</span>, &#x26;barValue);
4088<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
4089
4090<span class="hljs-comment">// Set the properties</span>
4091napi_property_descriptor descriptors[] = {
4092  { <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> },
4093  { <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> }
4094}
4095status = napi_define_properties(env,
4096                                obj,
4097                                <span class="hljs-keyword">sizeof</span>(descriptors) / <span class="hljs-keyword">sizeof</span>(descriptors[<span class="hljs-number">0</span>]),
4098                                descriptors);
4099<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code> <button class="copy-button">copy</button></pre>
4100<h4>Structures<span><a class="mark" href="#structures" id="structures">#</a></span><a aria-hidden="true" class="legacy" id="n_api_structures"></a></h4>
4101<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>
4102<div class="api_metadata">
4103<details class="changelog"><summary>History</summary>
4104<table>
4105<tbody><tr><th>Version</th><th>Changes</th></tr>
4106<tr><td>v14.12.0</td>
4107<td><p>added <code>napi_default_method</code> and <code>napi_default_property</code>.</p></td></tr>
4108</tbody></table>
4109</details>
4110</div>
4111<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
4112  napi_default = <span class="hljs-number">0</span>,
4113  napi_writable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">0</span>,
4114  napi_enumerable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">1</span>,
4115  napi_configurable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">2</span>,
4116
4117  <span class="hljs-comment">// Used with napi_define_class to distinguish static properties</span>
4118  <span class="hljs-comment">// from instance properties. Ignored by napi_define_properties.</span>
4119  napi_static = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">10</span>,
4120
4121  <span class="hljs-comment">// Default for class methods.</span>
4122  napi_default_method = napi_writable | napi_configurable,
4123
4124  <span class="hljs-comment">// Default for object properties, like in JS obj[prop].</span>
4125  napi_default_jsproperty = napi_writable |
4126                          napi_enumerable |
4127                          napi_configurable,
4128} napi_property_attributes;</code> <button class="copy-button">copy</button></pre>
4129<p><code>napi_property_attributes</code> are flags used to control the behavior of properties
4130set on a JavaScript object. Other than <code>napi_static</code> they correspond to the
4131attributes listed in <a href="https://tc39.github.io/ecma262/#table-2">Section 6.1.7.1</a>
4132of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.
4133They can be one or more of the following bitflags:</p>
4134<ul>
4135<li><code>napi_default</code>: No explicit attributes are set on the property. By default, a
4136property is read only, not enumerable and not configurable.</li>
4137<li><code>napi_writable</code>: The property is writable.</li>
4138<li><code>napi_enumerable</code>: The property is enumerable.</li>
4139<li><code>napi_configurable</code>: The property is configurable as defined in
4140<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>
4141<li><code>napi_static</code>: The property will be defined as a static property on a class as
4142opposed to an instance property, which is the default. This is used only by
4143<a href="#napi_define_class"><code>napi_define_class</code></a>. It is ignored by <code>napi_define_properties</code>.</li>
4144<li><code>napi_default_method</code>: Like a method in a JS class, the property is
4145configurable and writeable, but not enumerable.</li>
4146<li><code>napi_default_jsproperty</code>: Like a property set via assignment in JavaScript,
4147the property is writable, enumerable, and configurable.</li>
4148</ul>
4149<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>
4150<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span>
4151  <span class="hljs-comment">// One of utf8name or name should be NULL.</span>
4152  <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8name;
4153  napi_value name;
4154
4155  napi_callback method;
4156  napi_callback getter;
4157  napi_callback setter;
4158  napi_value value;
4159
4160  napi_property_attributes attributes;
4161  <span class="hljs-type">void</span>* data;
4162} napi_property_descriptor;</code> <button class="copy-button">copy</button></pre>
4163<ul>
4164<li><code>utf8name</code>: Optional string describing the key for the property,
4165encoded as UTF8. One of <code>utf8name</code> or <code>name</code> must be provided for the
4166property.</li>
4167<li><code>name</code>: Optional <code>napi_value</code> that points to a JavaScript string or symbol
4168to be used as the key for the property. One of <code>utf8name</code> or <code>name</code> must
4169be provided for the property.</li>
4170<li><code>value</code>: The value that's retrieved by a get access of the property if the
4171property is a data property. If this is passed in, set <code>getter</code>, <code>setter</code>,
4172<code>method</code> and <code>data</code> to <code>NULL</code> (since these members won't be used).</li>
4173<li><code>getter</code>: A function to call when a get access of the property is performed.
4174If this is passed in, set <code>value</code> and <code>method</code> to <code>NULL</code> (since these members
4175won't be used). The given function is called implicitly by the runtime when
4176the property is accessed from JavaScript code (or if a get on the property is
4177performed using a Node-API call). <a href="#napi_callback"><code>napi_callback</code></a> provides more details.</li>
4178<li><code>setter</code>: A function to call when a set access of the property is performed.
4179If this is passed in, set <code>value</code> and <code>method</code> to <code>NULL</code> (since these members
4180won't be used). The given function is called implicitly by the runtime when
4181the property is set from JavaScript code (or if a set on the property is
4182performed using a Node-API call). <a href="#napi_callback"><code>napi_callback</code></a> provides more details.</li>
4183<li><code>method</code>: Set this to make the property descriptor object's <code>value</code>
4184property to be a JavaScript function represented by <code>method</code>. If this is
4185passed in, set <code>value</code>, <code>getter</code> and <code>setter</code> to <code>NULL</code> (since these members
4186won't be used). <a href="#napi_callback"><code>napi_callback</code></a> provides more details.</li>
4187<li><code>attributes</code>: The attributes associated with the particular property. See
4188<a href="#napi_property_attributes"><code>napi_property_attributes</code></a>.</li>
4189<li><code>data</code>: The callback data passed into <code>method</code>, <code>getter</code> and <code>setter</code> if this
4190function is invoked.</li>
4191</ul>
4192<h4>Functions<span><a class="mark" href="#functions" id="functions">#</a></span><a aria-hidden="true" class="legacy" id="n_api_functions"></a></h4>
4193<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>
4194<div class="api_metadata">
4195<span>Added in: v8.0.0</span>
4196<span>N-API version: 1</span>
4197</div>
4198<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_property_names</span><span class="hljs-params">(napi_env env,
4199                                    napi_value object,
4200                                    napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
4201<ul>
4202<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4203<li><code>[in] object</code>: The object from which to retrieve the properties.</li>
4204<li><code>[out] result</code>: A <code>napi_value</code> representing an array of JavaScript values
4205that represent the property names of the object. The API can be used to
4206iterate over <code>result</code> using <a href="#napi_get_array_length"><code>napi_get_array_length</code></a>
4207and <a href="#napi_get_element"><code>napi_get_element</code></a>.</li>
4208</ul>
4209<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4210<p>This API returns the names of the enumerable properties of <code>object</code> as an array
4211of strings. The properties of <code>object</code> whose key is a symbol will not be
4212included.</p>
4213<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>
4214<div class="api_metadata">
4215<span>Added in: v13.7.0, v12.17.0, v10.20.0</span>
4216<span>N-API version: 6</span>
4217</div>
4218<pre><code class="language-c">napi_get_all_property_names(napi_env env,
4219                            napi_value object,
4220                            napi_key_collection_mode key_mode,
4221                            napi_key_filter key_filter,
4222                            napi_key_conversion key_conversion,
4223                            napi_value* result);</code> <button class="copy-button">copy</button></pre>
4224<ul>
4225<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4226<li><code>[in] object</code>: The object from which to retrieve the properties.</li>
4227<li><code>[in] key_mode</code>: Whether to retrieve prototype properties as well.</li>
4228<li><code>[in] key_filter</code>: Which properties to retrieve
4229(enumerable/readable/writable).</li>
4230<li><code>[in] key_conversion</code>: Whether to convert numbered property keys to strings.</li>
4231<li><code>[out] result</code>: A <code>napi_value</code> representing an array of JavaScript values
4232that represent the property names of the object. <a href="#napi_get_array_length"><code>napi_get_array_length</code></a>
4233and <a href="#napi_get_element"><code>napi_get_element</code></a> can be used to iterate over <code>result</code>.</li>
4234</ul>
4235<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4236<p>This API returns an array containing the names of the available properties
4237of this object.</p>
4238<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>
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_set_property</span><span class="hljs-params">(napi_env env,
4244                              napi_value object,
4245                              napi_value key,
4246                              napi_value value)</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 on which to set the property.</li>
4250<li><code>[in] key</code>: The name of the property to set.</li>
4251<li><code>[in] value</code>: The property value.</li>
4252</ul>
4253<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4254<p>This API set a property on the <code>Object</code> passed in.</p>
4255<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>
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_get_property</span><span class="hljs-params">(napi_env env,
4261                              napi_value object,
4262                              napi_value key,
4263                              napi_value* 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 from which to retrieve the property.</li>
4267<li><code>[in] key</code>: The name of the property to retrieve.</li>
4268<li><code>[out] result</code>: The value of the property.</li>
4269</ul>
4270<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4271<p>This API gets the requested property from the <code>Object</code> passed in.</p>
4272<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>
4273<div class="api_metadata">
4274<span>Added in: v8.0.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_has_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 whose existence to check.</li>
4285<li><code>[out] result</code>: Whether the property exists on the object or not.</li>
4286</ul>
4287<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4288<p>This API checks if the <code>Object</code> passed in has the named property.</p>
4289<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>
4290<div class="api_metadata">
4291<span>Added in: v8.2.0</span>
4292<span>N-API version: 1</span>
4293</div>
4294<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_property</span><span class="hljs-params">(napi_env env,
4295                                 napi_value object,
4296                                 napi_value key,
4297                                 <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
4298<ul>
4299<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4300<li><code>[in] object</code>: The object to query.</li>
4301<li><code>[in] key</code>: The name of the property to delete.</li>
4302<li><code>[out] result</code>: Whether the property deletion succeeded or not. <code>result</code> can
4303optionally be ignored by passing <code>NULL</code>.</li>
4304</ul>
4305<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4306<p>This API attempts to delete the <code>key</code> own property from <code>object</code>.</p>
4307<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>
4308<div class="api_metadata">
4309<span>Added in: v8.2.0</span>
4310<span>N-API version: 1</span>
4311</div>
4312<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_own_property</span><span class="hljs-params">(napi_env env,
4313                                  napi_value object,
4314                                  napi_value key,
4315                                  <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
4316<ul>
4317<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4318<li><code>[in] object</code>: The object to query.</li>
4319<li><code>[in] key</code>: The name of the own property whose existence to check.</li>
4320<li><code>[out] result</code>: Whether the own property exists on the object or not.</li>
4321</ul>
4322<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4323<p>This API checks if the <code>Object</code> passed in has the named own property. <code>key</code> must
4324be a <code>string</code> or a <code>symbol</code>, or an error will be thrown. Node-API will not
4325perform any conversion between data types.</p>
4326<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>
4327<div class="api_metadata">
4328<span>Added in: v8.0.0</span>
4329<span>N-API version: 1</span>
4330</div>
4331<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_named_property</span><span class="hljs-params">(napi_env env,
4332                                    napi_value object,
4333                                    <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8Name,
4334                                    napi_value value)</span>;</code> <button class="copy-button">copy</button></pre>
4335<ul>
4336<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4337<li><code>[in] object</code>: The object on which to set the property.</li>
4338<li><code>[in] utf8Name</code>: The name of the property to set.</li>
4339<li><code>[in] value</code>: The property value.</li>
4340</ul>
4341<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4342<p>This method is equivalent to calling <a href="#napi_set_property"><code>napi_set_property</code></a> with a <code>napi_value</code>
4343created from the string passed in as <code>utf8Name</code>.</p>
4344<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>
4345<div class="api_metadata">
4346<span>Added in: v8.0.0</span>
4347<span>N-API version: 1</span>
4348</div>
4349<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_named_property</span><span class="hljs-params">(napi_env env,
4350                                    napi_value object,
4351                                    <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8Name,
4352                                    napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
4353<ul>
4354<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4355<li><code>[in] object</code>: The object from which to retrieve the property.</li>
4356<li><code>[in] utf8Name</code>: The name of the property to get.</li>
4357<li><code>[out] result</code>: The value of the property.</li>
4358</ul>
4359<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4360<p>This method is equivalent to calling <a href="#napi_get_property"><code>napi_get_property</code></a> with a <code>napi_value</code>
4361created from the string passed in as <code>utf8Name</code>.</p>
4362<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>
4363<div class="api_metadata">
4364<span>Added in: v8.0.0</span>
4365<span>N-API version: 1</span>
4366</div>
4367<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_named_property</span><span class="hljs-params">(napi_env env,
4368                                    napi_value object,
4369                                    <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8Name,
4370                                    <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
4371<ul>
4372<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4373<li><code>[in] object</code>: The object to query.</li>
4374<li><code>[in] utf8Name</code>: The name of the property whose existence to check.</li>
4375<li><code>[out] result</code>: Whether the property exists on the object or not.</li>
4376</ul>
4377<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4378<p>This method is equivalent to calling <a href="#napi_has_property"><code>napi_has_property</code></a> with a <code>napi_value</code>
4379created from the string passed in as <code>utf8Name</code>.</p>
4380<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>
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_set_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 value)</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 set the properties.</li>
4392<li><code>[in] index</code>: The index of the property to set.</li>
4393<li><code>[in] value</code>: The property value.</li>
4394</ul>
4395<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4396<p>This API sets an element on the <code>Object</code> passed in.</p>
4397<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>
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_get_element</span><span class="hljs-params">(napi_env env,
4403                             napi_value object,
4404                             <span class="hljs-type">uint32_t</span> index,
4405                             napi_value* 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 from which to retrieve the property.</li>
4409<li><code>[in] index</code>: The index of the property to get.</li>
4410<li><code>[out] result</code>: The value of the property.</li>
4411</ul>
4412<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4413<p>This API gets the element at the requested index.</p>
4414<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>
4415<div class="api_metadata">
4416<span>Added in: v8.0.0</span>
4417<span>N-API version: 1</span>
4418</div>
4419<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_element</span><span class="hljs-params">(napi_env env,
4420                             napi_value object,
4421                             <span class="hljs-type">uint32_t</span> index,
4422                             <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
4423<ul>
4424<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4425<li><code>[in] object</code>: The object to query.</li>
4426<li><code>[in] index</code>: The index of the property whose existence to check.</li>
4427<li><code>[out] result</code>: Whether the property exists on the object or not.</li>
4428</ul>
4429<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4430<p>This API returns if the <code>Object</code> passed in has an element at the
4431requested index.</p>
4432<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>
4433<div class="api_metadata">
4434<span>Added in: v8.2.0</span>
4435<span>N-API version: 1</span>
4436</div>
4437<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_element</span><span class="hljs-params">(napi_env env,
4438                                napi_value object,
4439                                <span class="hljs-type">uint32_t</span> index,
4440                                <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
4441<ul>
4442<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4443<li><code>[in] object</code>: The object to query.</li>
4444<li><code>[in] index</code>: The index of the property to delete.</li>
4445<li><code>[out] result</code>: Whether the element deletion succeeded or not. <code>result</code> can
4446optionally be ignored by passing <code>NULL</code>.</li>
4447</ul>
4448<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4449<p>This API attempts to delete the specified <code>index</code> from <code>object</code>.</p>
4450<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>
4451<div class="api_metadata">
4452<span>Added in: v8.0.0</span>
4453<span>N-API version: 1</span>
4454</div>
4455<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_properties</span><span class="hljs-params">(napi_env env,
4456                                   napi_value object,
4457                                   <span class="hljs-type">size_t</span> property_count,
4458                                   <span class="hljs-type">const</span> napi_property_descriptor* properties)</span>;</code> <button class="copy-button">copy</button></pre>
4459<ul>
4460<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4461<li><code>[in] object</code>: The object from which to retrieve the properties.</li>
4462<li><code>[in] property_count</code>: The number of elements in the <code>properties</code> array.</li>
4463<li><code>[in] properties</code>: The array of property descriptors.</li>
4464</ul>
4465<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4466<p>This method allows the efficient definition of multiple properties on a given
4467object. The properties are defined using property descriptors (see
4468<a href="#napi_property_descriptor"><code>napi_property_descriptor</code></a>). Given an array of such property descriptors,
4469this API will set the properties on the object one at a time, as defined by
4470<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
4471specification).</p>
4472<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>
4473<div class="api_metadata">
4474<span>Added in: v14.14.0, v12.20.0</span>
4475<span>N-API version: 8</span>
4476</div>
4477<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_freeze</span><span class="hljs-params">(napi_env env,
4478                               napi_value object)</span>;</code> <button class="copy-button">copy</button></pre>
4479<ul>
4480<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4481<li><code>[in] object</code>: The object to freeze.</li>
4482</ul>
4483<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4484<p>This method freezes a given object. This prevents new properties from
4485being added to it, existing properties from being removed, prevents
4486changing the enumerability, configurability, or writability of existing
4487properties, and prevents the values of existing properties from being changed.
4488It also prevents the object's prototype from being changed. This is described
4489in <a href="https://tc39.es/ecma262/#sec-object.freeze">Section 19.1.2.6</a> of the
4490ECMA-262 specification.</p>
4491<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>
4492<div class="api_metadata">
4493<span>Added in: v14.14.0, v12.20.0</span>
4494<span>N-API version: 8</span>
4495</div>
4496<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_seal</span><span class="hljs-params">(napi_env env,
4497                             napi_value object)</span>;</code> <button class="copy-button">copy</button></pre>
4498<ul>
4499<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
4500<li><code>[in] object</code>: The object to seal.</li>
4501</ul>
4502<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4503<p>This method seals a given object. This prevents new properties from being
4504added to it, as well as marking all existing properties as non-configurable.
4505This is described in <a href="https://tc39.es/ecma262/#sec-object.seal">Section 19.1.2.20</a>
4506of the ECMA-262 specification.</p>
4507</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>
4508<p>Node-API provides a set of APIs that allow JavaScript code to
4509call back into native code. Node-APIs that support calling back
4510into native code take in a callback functions represented by
4511the <code>napi_callback</code> type. When the JavaScript VM calls back to
4512native code, the <code>napi_callback</code> function provided is invoked. The APIs
4513documented in this section allow the callback function to do the
4514following:</p>
4515<ul>
4516<li>Get information about the context in which the callback was invoked.</li>
4517<li>Get the arguments passed into the callback.</li>
4518<li>Return a <code>napi_value</code> back from the callback.</li>
4519</ul>
4520<p>Additionally, Node-API provides a set of functions which allow calling
4521JavaScript functions from native code. One can either call a function
4522like a regular JavaScript function call, or as a constructor
4523function.</p>
4524<p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> field of the
4525<code>napi_property_descriptor</code> items can be associated with <code>object</code> and freed
4526whenever <code>object</code> is garbage-collected by passing both <code>object</code> and the data to
4527<a href="#napi_add_finalizer"><code>napi_add_finalizer</code></a>.</p>
4528<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>
4529<div class="api_metadata">
4530<span>Added in: v8.0.0</span>
4531<span>N-API version: 1</span>
4532</div>
4533<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,
4534                                           napi_value recv,
4535                                           napi_value func,
4536                                           <span class="hljs-type">size_t</span> argc,
4537                                           <span class="hljs-type">const</span> napi_value* argv,
4538                                           napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
4539<ul>
4540<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4541<li><code>[in] recv</code>: The <code>this</code> value passed to the called function.</li>
4542<li><code>[in] func</code>: <code>napi_value</code> representing the JavaScript function to be invoked.</li>
4543<li><code>[in] argc</code>: The count of elements in the <code>argv</code> array.</li>
4544<li><code>[in] argv</code>: Array of <code>napi_values</code> representing JavaScript values passed in
4545as arguments to the function.</li>
4546<li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned.</li>
4547</ul>
4548<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4549<p>This method allows a JavaScript function object to be called from a native
4550add-on. This is the primary mechanism of calling back <em>from</em> the add-on's
4551native code <em>into</em> JavaScript. For the special case of calling into JavaScript
4552after an async operation, see <a href="#napi_make_callback"><code>napi_make_callback</code></a>.</p>
4553<p>A sample use case might look as follows. Consider the following JavaScript
4554snippet:</p>
4555<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">AddTwo</span>(<span class="hljs-params">num</span>) {
4556  <span class="hljs-keyword">return</span> num + <span class="hljs-number">2</span>;
4557}
4558<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>
4559<p>Then, the above function can be invoked from a native add-on using the
4560following code:</p>
4561<pre><code class="language-c"><span class="hljs-comment">// Get the function named "AddTwo" on the global object</span>
4562napi_value global, add_two, arg;
4563napi_status status = napi_get_global(env, &#x26;global);
4564<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4565
4566status = napi_get_named_property(env, global, <span class="hljs-string">"AddTwo"</span>, &#x26;add_two);
4567<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4568
4569<span class="hljs-comment">// const arg = 1337</span>
4570status = napi_create_int32(env, <span class="hljs-number">1337</span>, &#x26;arg);
4571<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4572
4573napi_value* argv = &#x26;arg;
4574<span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>;
4575
4576<span class="hljs-comment">// AddTwo(arg);</span>
4577napi_value return_val;
4578status = napi_call_function(env, global, add_two, argc, argv, &#x26;return_val);
4579<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4580
4581<span class="hljs-comment">// Convert the result back to a native type</span>
4582<span class="hljs-type">int32_t</span> result;
4583status = napi_get_value_int32(env, return_val, &#x26;result);
4584<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;</code> <button class="copy-button">copy</button></pre>
4585<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>
4586<div class="api_metadata">
4587<span>Added in: v8.0.0</span>
4588<span>N-API version: 1</span>
4589</div>
4590<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_function</span><span class="hljs-params">(napi_env env,
4591                                 <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8name,
4592                                 <span class="hljs-type">size_t</span> length,
4593                                 napi_callback cb,
4594                                 <span class="hljs-type">void</span>* data,
4595                                 napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
4596<ul>
4597<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4598<li><code>[in] utf8Name</code>: Optional name of the function encoded as UTF8. This is
4599visible within JavaScript as the new function object's <code>name</code> property.</li>
4600<li><code>[in] length</code>: The length of the <code>utf8name</code> in bytes, or <code>NAPI_AUTO_LENGTH</code> if
4601it is null-terminated.</li>
4602<li><code>[in] cb</code>: The native function which should be called when this function
4603object is invoked. <a href="#napi_callback"><code>napi_callback</code></a> provides more details.</li>
4604<li><code>[in] data</code>: User-provided data context. This will be passed back into the
4605function when invoked later.</li>
4606<li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript function object for
4607the newly created function.</li>
4608</ul>
4609<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4610<p>This API allows an add-on author to create a function object in native code.
4611This is the primary mechanism to allow calling <em>into</em> the add-on's native code
4612<em>from</em> JavaScript.</p>
4613<p>The newly created function is not automatically visible from script after this
4614call. Instead, a property must be explicitly set on any object that is visible
4615to JavaScript, in order for the function to be accessible from script.</p>
4616<p>In order to expose a function as part of the
4617add-on's module exports, set the newly created function on the exports
4618object. A sample module might look as follows:</p>
4619<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> {
4620  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello\n"</span>);
4621  <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4622}
4623
4624napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> {
4625  napi_status status;
4626
4627  napi_value fn;
4628  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);
4629  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4630
4631  status = napi_set_named_property(env, exports, <span class="hljs-string">"sayHello"</span>, fn);
4632  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4633
4634  <span class="hljs-keyword">return</span> exports;
4635}
4636
4637NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)</code> <button class="copy-button">copy</button></pre>
4638<p>Given the above code, the add-on can be used from JavaScript as follows:</p>
4639<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>);
4640myaddon.<span class="hljs-title function_">sayHello</span>();</code> <button class="copy-button">copy</button></pre>
4641<p>The string passed to <code>require()</code> is the name of the target in <code>binding.gyp</code>
4642responsible for creating the <code>.node</code> file.</p>
4643<p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> parameter can
4644be associated with the resulting JavaScript function (which is returned in the
4645<code>result</code> parameter) and freed whenever the function is garbage-collected by
4646passing both the JavaScript function and the data to <a href="#napi_add_finalizer"><code>napi_add_finalizer</code></a>.</p>
4647<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
4648Language Specification.</p>
4649<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>
4650<div class="api_metadata">
4651<span>Added in: v8.0.0</span>
4652<span>N-API version: 1</span>
4653</div>
4654<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_cb_info</span><span class="hljs-params">(napi_env env,
4655                             napi_callback_info cbinfo,
4656                             <span class="hljs-type">size_t</span>* argc,
4657                             napi_value* argv,
4658                             napi_value* thisArg,
4659                             <span class="hljs-type">void</span>** data)</span></code> <button class="copy-button">copy</button></pre>
4660<ul>
4661<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4662<li><code>[in] cbinfo</code>: The callback info passed into the callback function.</li>
4663<li><code>[in-out] argc</code>: Specifies the length of the provided <code>argv</code> array and
4664receives the actual count of arguments. <code>argc</code> can
4665optionally be ignored by passing <code>NULL</code>.</li>
4666<li><code>[out] argv</code>: C array of <code>napi_value</code>s to which the arguments will be
4667copied. If there are more arguments than the provided count, only the
4668requested number of arguments are copied. If there are fewer arguments
4669provided than claimed, the rest of <code>argv</code> is filled with <code>napi_value</code> values
4670that represent <code>undefined</code>. <code>argv</code> can optionally be ignored by
4671passing <code>NULL</code>.</li>
4672<li><code>[out] thisArg</code>: Receives the JavaScript <code>this</code> argument for the call.
4673<code>thisArg</code> can optionally be ignored by passing <code>NULL</code>.</li>
4674<li><code>[out] data</code>: Receives the data pointer for the callback. <code>data</code> can
4675optionally be ignored by passing <code>NULL</code>.</li>
4676</ul>
4677<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4678<p>This method is used within a callback function to retrieve details about the
4679call like the arguments and the <code>this</code> pointer from a given callback info.</p>
4680<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>
4681<div class="api_metadata">
4682<span>Added in: v8.6.0</span>
4683<span>N-API version: 1</span>
4684</div>
4685<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_new_target</span><span class="hljs-params">(napi_env env,
4686                                napi_callback_info cbinfo,
4687                                napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
4688<ul>
4689<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4690<li><code>[in] cbinfo</code>: The callback info passed into the callback function.</li>
4691<li><code>[out] result</code>: The <code>new.target</code> of the constructor call.</li>
4692</ul>
4693<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4694<p>This API returns the <code>new.target</code> of the constructor call. If the current
4695callback is not a constructor call, the result is <code>NULL</code>.</p>
4696<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>
4697<div class="api_metadata">
4698<span>Added in: v8.0.0</span>
4699<span>N-API version: 1</span>
4700</div>
4701<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_new_instance</span><span class="hljs-params">(napi_env env,
4702                              napi_value cons,
4703                              <span class="hljs-type">size_t</span> argc,
4704                              napi_value* argv,
4705                              napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
4706<ul>
4707<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4708<li><code>[in] cons</code>: <code>napi_value</code> representing the JavaScript function to be invoked
4709as a constructor.</li>
4710<li><code>[in] argc</code>: The count of elements in the <code>argv</code> array.</li>
4711<li><code>[in] argv</code>: Array of JavaScript values as <code>napi_value</code> representing the
4712arguments to the constructor. If <code>argc</code> is zero this parameter may be
4713omitted by passing in <code>NULL</code>.</li>
4714<li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned,
4715which in this case is the constructed object.</li>
4716</ul>
4717<p>This method is used to instantiate a new JavaScript value using a given
4718<code>napi_value</code> that represents the constructor for the object. For example,
4719consider the following snippet:</p>
4720<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">MyObject</span>(<span class="hljs-params">param</span>) {
4721  <span class="hljs-variable language_">this</span>.<span class="hljs-property">param</span> = param;
4722}
4723
4724<span class="hljs-keyword">const</span> arg = <span class="hljs-string">'hello'</span>;
4725<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>
4726<p>The following can be approximated in Node-API using the following snippet:</p>
4727<pre><code class="language-c"><span class="hljs-comment">// Get the constructor function MyObject</span>
4728napi_value global, constructor, arg, value;
4729napi_status status = napi_get_global(env, &#x26;global);
4730<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4731
4732status = napi_get_named_property(env, global, <span class="hljs-string">"MyObject"</span>, &#x26;constructor);
4733<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4734
4735<span class="hljs-comment">// const arg = "hello"</span>
4736status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &#x26;arg);
4737<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4738
4739napi_value* argv = &#x26;arg;
4740<span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>;
4741
4742<span class="hljs-comment">// const value = new MyObject(arg)</span>
4743status = napi_new_instance(env, constructor, argc, argv, &#x26;value);</code> <button class="copy-button">copy</button></pre>
4744<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4745</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>
4746<p>Node-API offers a way to "wrap" C++ classes and instances so that the class
4747constructor and methods can be called from JavaScript.</p>
4748<ol>
4749<li>The <a href="#napi_define_class"><code>napi_define_class</code></a> API defines a JavaScript class with constructor,
4750static properties and methods, and instance properties and methods that
4751correspond to the C++ class.</li>
4752<li>When JavaScript code invokes the constructor, the constructor callback
4753uses <a href="#napi_wrap"><code>napi_wrap</code></a> to wrap a new C++ instance in a JavaScript object,
4754then returns the wrapper object.</li>
4755<li>When JavaScript code invokes a method or property accessor on the class,
4756the corresponding <code>napi_callback</code> C++ function is invoked. For an instance
4757callback, <a href="#napi_unwrap"><code>napi_unwrap</code></a> obtains the C++ instance that is the target of
4758the call.</li>
4759</ol>
4760<p>For wrapped objects it may be difficult to distinguish between a function
4761called on a class prototype and a function called on an instance of a class.
4762A common pattern used to address this problem is to save a persistent
4763reference to the class constructor for later <code>instanceof</code> checks.</p>
4764<pre><code class="language-c">napi_value MyClass_constructor = <span class="hljs-literal">NULL</span>;
4765status = napi_get_reference_value(env, MyClass::es_constructor, &#x26;MyClass_constructor);
4766assert(napi_ok == status);
4767<span class="hljs-type">bool</span> is_instance = <span class="hljs-literal">false</span>;
4768status = napi_instanceof(env, es_this, MyClass_constructor, &#x26;is_instance);
4769assert(napi_ok == status);
4770<span class="hljs-keyword">if</span> (is_instance) {
4771  <span class="hljs-comment">// napi_unwrap() ...</span>
4772} <span class="hljs-keyword">else</span> {
4773  <span class="hljs-comment">// otherwise...</span>
4774}</code> <button class="copy-button">copy</button></pre>
4775<p>The reference must be freed once it is no longer needed.</p>
4776<p>There are occasions where <code>napi_instanceof()</code> is insufficient for ensuring that
4777a JavaScript object is a wrapper for a certain native type. This is the case
4778especially when wrapped JavaScript objects are passed back into the addon via
4779static methods rather than as the <code>this</code> value of prototype methods. In such
4780cases there is a chance that they may be unwrapped incorrectly.</p>
4781<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>);
4782
4783<span class="hljs-comment">// `openDatabase()` returns a JavaScript object that wraps a native database</span>
4784<span class="hljs-comment">// handle.</span>
4785<span class="hljs-keyword">const</span> dbHandle = myAddon.<span class="hljs-title function_">openDatabase</span>();
4786
4787<span class="hljs-comment">// `query()` returns a JavaScript object that wraps a native query handle.</span>
4788<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>);
4789
4790<span class="hljs-comment">// There is an accidental error in the line below. The first parameter to</span>
4791<span class="hljs-comment">// `myAddon.queryHasRecords()` should be the database handle (`dbHandle`), not</span>
4792<span class="hljs-comment">// the query handle (`query`), so the correct condition for the while-loop</span>
4793<span class="hljs-comment">// should be</span>
4794<span class="hljs-comment">//</span>
4795<span class="hljs-comment">// myAddon.queryHasRecords(dbHandle, queryHandle)</span>
4796<span class="hljs-comment">//</span>
4797<span class="hljs-keyword">while</span> (myAddon.<span class="hljs-title function_">queryHasRecords</span>(queryHandle, dbHandle)) {
4798  <span class="hljs-comment">// retrieve records</span>
4799}</code> <button class="copy-button">copy</button></pre>
4800<p>In the above example <code>myAddon.queryHasRecords()</code> is a method that accepts two
4801arguments. The first is a database handle and the second is a query handle.
4802Internally, it unwraps the first argument and casts the resulting pointer to a
4803native database handle. It then unwraps the second argument and casts the
4804resulting pointer to a query handle. If the arguments are passed in the wrong
4805order, the casts will work, however, there is a good chance that the underlying
4806database operation will fail, or will even cause an invalid memory access.</p>
4807<p>To ensure that the pointer retrieved from the first argument is indeed a pointer
4808to a database handle and, similarly, that the pointer retrieved from the second
4809argument is indeed a pointer to a query handle, the implementation of
4810<code>queryHasRecords()</code> has to perform a type validation. Retaining the JavaScript
4811class constructor from which the database handle was instantiated and the
4812constructor from which the query handle was instantiated in <code>napi_ref</code>s can
4813help, because <code>napi_instanceof()</code> can then be used to ensure that the instances
4814passed into <code>queryHashRecords()</code> are indeed of the correct type.</p>
4815<p>Unfortunately, <code>napi_instanceof()</code> does not protect against prototype
4816manipulation. For example, the prototype of the database handle instance can be
4817set to the prototype of the constructor for query handle instances. In this
4818case, the database handle instance can appear as a query handle instance, and it
4819will pass the <code>napi_instanceof()</code> test for a query handle instance, while still
4820containing a pointer to a database handle.</p>
4821<p>To this end, Node-API provides type-tagging capabilities.</p>
4822<p>A type tag is a 128-bit integer unique to the addon. Node-API provides the
4823<code>napi_type_tag</code> structure for storing a type tag. When such a value is passed
4824along with a JavaScript object or <a href="#napi_create_external">external</a> stored in a <code>napi_value</code> to
4825<code>napi_type_tag_object()</code>, the JavaScript object will be "marked" with the
4826type tag. The "mark" is invisible on the JavaScript side. When a JavaScript
4827object arrives into a native binding, <code>napi_check_object_type_tag()</code> can be used
4828along with the original type tag to determine whether the JavaScript object was
4829previously "marked" with the type tag. This creates a type-checking capability
4830of a higher fidelity than <code>napi_instanceof()</code> can provide, because such type-
4831tagging survives prototype manipulation and addon unloading/reloading.</p>
4832<p>Continuing the above example, the following skeleton addon implementation
4833illustrates the use of <code>napi_type_tag_object()</code> and
4834<code>napi_check_object_type_tag()</code>.</p>
4835<pre><code class="language-c"><span class="hljs-comment">// This value is the type tag for a database handle. The command</span>
4836<span class="hljs-comment">//</span>
4837<span class="hljs-comment">//   uuidgen | sed -r -e 's/-//g' -e 's/(.{16})(.*)/0x\1, 0x\2/'</span>
4838<span class="hljs-comment">//</span>
4839<span class="hljs-comment">// can be used to obtain the two values with which to initialize the structure.</span>
4840<span class="hljs-type">static</span> <span class="hljs-type">const</span> napi_type_tag DatabaseHandleTypeTag = {
4841  <span class="hljs-number">0x1edf75a38336451d</span>, <span class="hljs-number">0xa5ed9ce2e4c00c38</span>
4842};
4843
4844<span class="hljs-comment">// This value is the type tag for a query handle.</span>
4845<span class="hljs-type">static</span> <span class="hljs-type">const</span> napi_type_tag QueryHandleTypeTag = {
4846  <span class="hljs-number">0x9c73317f9fad44a3</span>, <span class="hljs-number">0x93c3920bf3b0ad6a</span>
4847};
4848
4849<span class="hljs-type">static</span> napi_value
4850<span class="hljs-title function_">openDatabase</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> {
4851  napi_status status;
4852  napi_value result;
4853
4854  <span class="hljs-comment">// Perform the underlying action which results in a database handle.</span>
4855  DatabaseHandle* dbHandle = open_database();
4856
4857  <span class="hljs-comment">// Create a new, empty JS object.</span>
4858  status = napi_create_object(env, &#x26;result);
4859  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4860
4861  <span class="hljs-comment">// Tag the object to indicate that it holds a pointer to a `DatabaseHandle`.</span>
4862  status = napi_type_tag_object(env, result, &#x26;DatabaseHandleTypeTag);
4863  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4864
4865  <span class="hljs-comment">// Store the pointer to the `DatabaseHandle` structure inside the JS object.</span>
4866  status = napi_wrap(env, result, dbHandle, <span class="hljs-literal">NULL</span>, <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-keyword">return</span> result;
4870}
4871
4872<span class="hljs-comment">// Later when we receive a JavaScript object purporting to be a database handle</span>
4873<span class="hljs-comment">// we can use `napi_check_object_type_tag()` to ensure that it is indeed such a</span>
4874<span class="hljs-comment">// handle.</span>
4875
4876<span class="hljs-type">static</span> napi_value
4877<span class="hljs-title function_">query</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> {
4878  napi_status status;
4879  <span class="hljs-type">size_t</span> argc = <span class="hljs-number">2</span>;
4880  napi_value argv[<span class="hljs-number">2</span>];
4881  <span class="hljs-type">bool</span> is_db_handle;
4882
4883  status = napi_get_cb_info(env, info, &#x26;argc, argv, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>);
4884  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4885
4886  <span class="hljs-comment">// Check that the object passed as the first parameter has the previously</span>
4887  <span class="hljs-comment">// applied tag.</span>
4888  status = napi_check_object_type_tag(env,
4889                                      argv[<span class="hljs-number">0</span>],
4890                                      &#x26;DatabaseHandleTypeTag,
4891                                      &#x26;is_db_handle);
4892  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4893
4894  <span class="hljs-comment">// Throw a `TypeError` if it doesn't.</span>
4895  <span class="hljs-keyword">if</span> (!is_db_handle) {
4896    <span class="hljs-comment">// Throw a TypeError.</span>
4897    <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4898  }
4899}</code> <button class="copy-button">copy</button></pre>
4900<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>
4901<div class="api_metadata">
4902<span>Added in: v8.0.0</span>
4903<span>N-API version: 1</span>
4904</div>
4905<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_class</span><span class="hljs-params">(napi_env env,
4906                              <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8name,
4907                              <span class="hljs-type">size_t</span> length,
4908                              napi_callback constructor,
4909                              <span class="hljs-type">void</span>* data,
4910                              <span class="hljs-type">size_t</span> property_count,
4911                              <span class="hljs-type">const</span> napi_property_descriptor* properties,
4912                              napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
4913<ul>
4914<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4915<li><code>[in] utf8name</code>: Name of the JavaScript constructor function. For clarity,
4916it is recommended to use the C++ class name when wrapping a C++ class.</li>
4917<li><code>[in] length</code>: The length of the <code>utf8name</code> in bytes, or <code>NAPI_AUTO_LENGTH</code>
4918if it is null-terminated.</li>
4919<li><code>[in] constructor</code>: Callback function that handles constructing instances
4920of the class. When wrapping a C++ class, this method must be a static member
4921with the <a href="#napi_callback"><code>napi_callback</code></a> signature. A C++ class constructor cannot be
4922used. <a href="#napi_callback"><code>napi_callback</code></a> provides more details.</li>
4923<li><code>[in] data</code>: Optional data to be passed to the constructor callback as
4924the <code>data</code> property of the callback info.</li>
4925<li><code>[in] property_count</code>: Number of items in the <code>properties</code> array argument.</li>
4926<li><code>[in] properties</code>: Array of property descriptors describing static and
4927instance data properties, accessors, and methods on the class
4928See <code>napi_property_descriptor</code>.</li>
4929<li><code>[out] result</code>: A <code>napi_value</code> representing the constructor function for
4930the class.</li>
4931</ul>
4932<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4933<p>Defines a JavaScript class, including:</p>
4934<ul>
4935<li>A JavaScript constructor function that has the class name. When wrapping a
4936corresponding C++ class, the callback passed via <code>constructor</code> can be used to
4937instantiate a new C++ class instance, which can then be placed inside the
4938JavaScript object instance being constructed using <a href="#napi_wrap"><code>napi_wrap</code></a>.</li>
4939<li>Properties on the constructor function whose implementation can call
4940corresponding <em>static</em> data properties, accessors, and methods of the C++
4941class (defined by property descriptors with the <code>napi_static</code> attribute).</li>
4942<li>Properties on the constructor function's <code>prototype</code> object. When wrapping a
4943C++ class, <em>non-static</em> data properties, accessors, and methods of the C++
4944class can be called from the static functions given in the property
4945descriptors without the <code>napi_static</code> attribute after retrieving the C++ class
4946instance placed inside the JavaScript object instance by using
4947<a href="#napi_unwrap"><code>napi_unwrap</code></a>.</li>
4948</ul>
4949<p>When wrapping a C++ class, the C++ constructor callback passed via <code>constructor</code>
4950should be a static method on the class that calls the actual class constructor,
4951then wraps the new C++ instance in a JavaScript object, and returns the wrapper
4952object. See <a href="#napi_wrap"><code>napi_wrap</code></a> for details.</p>
4953<p>The JavaScript constructor function returned from <a href="#napi_define_class"><code>napi_define_class</code></a> is
4954often saved and used later to construct new instances of the class from native
4955code, and/or to check whether provided values are instances of the class. In
4956that case, to prevent the function value from being garbage-collected, a
4957strong persistent reference to it can be created using
4958<a href="#napi_create_reference"><code>napi_create_reference</code></a>, ensuring that the reference count is kept >= 1.</p>
4959<p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> parameter or via
4960the <code>data</code> field of the <code>napi_property_descriptor</code> array items can be associated
4961with the resulting JavaScript constructor (which is returned in the <code>result</code>
4962parameter) and freed whenever the class is garbage-collected by passing both
4963the JavaScript function and the data to <a href="#napi_add_finalizer"><code>napi_add_finalizer</code></a>.</p>
4964<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>
4965<div class="api_metadata">
4966<span>Added in: v8.0.0</span>
4967<span>N-API version: 1</span>
4968</div>
4969<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_wrap</span><span class="hljs-params">(napi_env env,
4970                      napi_value js_object,
4971                      <span class="hljs-type">void</span>* native_object,
4972                      napi_finalize finalize_cb,
4973                      <span class="hljs-type">void</span>* finalize_hint,
4974                      napi_ref* result)</span>;</code> <button class="copy-button">copy</button></pre>
4975<ul>
4976<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4977<li><code>[in] js_object</code>: The JavaScript object that will be the wrapper for the
4978native object.</li>
4979<li><code>[in] native_object</code>: The native instance that will be wrapped in the
4980JavaScript object.</li>
4981<li><code>[in] finalize_cb</code>: Optional native callback that can be used to free the
4982native instance when the JavaScript object has been garbage-collected.
4983<a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
4984<li><code>[in] finalize_hint</code>: Optional contextual hint that is passed to the
4985finalize callback.</li>
4986<li><code>[out] result</code>: Optional reference to the wrapped object.</li>
4987</ul>
4988<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4989<p>Wraps a native instance in a JavaScript object. The native instance can be
4990retrieved later using <code>napi_unwrap()</code>.</p>
4991<p>When JavaScript code invokes a constructor for a class that was defined using
4992<code>napi_define_class()</code>, the <code>napi_callback</code> for the constructor is invoked.
4993After constructing an instance of the native class, the callback must then call
4994<code>napi_wrap()</code> to wrap the newly constructed instance in the already-created
4995JavaScript object that is the <code>this</code> argument to the constructor callback.
4996(That <code>this</code> object was created from the constructor function's <code>prototype</code>,
4997so it already has definitions of all the instance properties and methods.)</p>
4998<p>Typically when wrapping a class instance, a finalize callback should be
4999provided that simply deletes the native instance that is received as the <code>data</code>
5000argument to the finalize callback.</p>
5001<p>The optional returned reference is initially a weak reference, meaning it
5002has a reference count of 0. Typically this reference count would be incremented
5003temporarily during async operations that require the instance to remain valid.</p>
5004<p><em>Caution</em>: The optional returned reference (if obtained) should be deleted via
5005<a href="#napi_delete_reference"><code>napi_delete_reference</code></a> ONLY in response to the finalize callback
5006invocation. If it is deleted before then, then the finalize callback may never
5007be invoked. Therefore, when obtaining a reference a finalize callback is also
5008required in order to enable correct disposal of the reference.</p>
5009<p>Finalizer callbacks may be deferred, leaving a window where the object has
5010been garbage collected (and the weak reference is invalid) but the finalizer
5011hasn't been called yet. When using <code>napi_get_reference_value()</code> on weak
5012references returned by <code>napi_wrap()</code>, you should still handle an empty result.</p>
5013<p>Calling <code>napi_wrap()</code> a second time on an object will return an error. To
5014associate another native instance with the object, use <code>napi_remove_wrap()</code>
5015first.</p>
5016<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>
5017<div class="api_metadata">
5018<span>Added in: v8.0.0</span>
5019<span>N-API version: 1</span>
5020</div>
5021<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_unwrap</span><span class="hljs-params">(napi_env env,
5022                        napi_value js_object,
5023                        <span class="hljs-type">void</span>** result)</span>;</code> <button class="copy-button">copy</button></pre>
5024<ul>
5025<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5026<li><code>[in] js_object</code>: The object associated with the native instance.</li>
5027<li><code>[out] result</code>: Pointer to the wrapped native instance.</li>
5028</ul>
5029<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5030<p>Retrieves a native instance that was previously wrapped in a JavaScript
5031object using <code>napi_wrap()</code>.</p>
5032<p>When JavaScript code invokes a method or property accessor on the class, the
5033corresponding <code>napi_callback</code> is invoked. If the callback is for an instance
5034method or accessor, then the <code>this</code> argument to the callback is the wrapper
5035object; the wrapped C++ instance that is the target of the call can be obtained
5036then by calling <code>napi_unwrap()</code> on the wrapper object.</p>
5037<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>
5038<div class="api_metadata">
5039<span>Added in: v8.5.0</span>
5040<span>N-API version: 1</span>
5041</div>
5042<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_remove_wrap</span><span class="hljs-params">(napi_env env,
5043                             napi_value js_object,
5044                             <span class="hljs-type">void</span>** result)</span>;</code> <button class="copy-button">copy</button></pre>
5045<ul>
5046<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5047<li><code>[in] js_object</code>: The object associated with the native instance.</li>
5048<li><code>[out] result</code>: Pointer to the wrapped native instance.</li>
5049</ul>
5050<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5051<p>Retrieves a native instance that was previously wrapped in the JavaScript
5052object <code>js_object</code> using <code>napi_wrap()</code> and removes the wrapping. If a finalize
5053callback was associated with the wrapping, it will no longer be called when the
5054JavaScript object becomes garbage-collected.</p>
5055<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>
5056<div class="api_metadata">
5057<span>Added in: v14.8.0, v12.19.0</span>
5058<span>N-API version: 8</span>
5059</div>
5060<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_type_tag_object</span><span class="hljs-params">(napi_env env,
5061                                 napi_value js_object,
5062                                 <span class="hljs-type">const</span> napi_type_tag* type_tag)</span>;</code> <button class="copy-button">copy</button></pre>
5063<ul>
5064<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5065<li><code>[in] js_object</code>: The JavaScript object or <a href="#napi_create_external">external</a> to be marked.</li>
5066<li><code>[in] type_tag</code>: The tag with which the object is to be marked.</li>
5067</ul>
5068<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5069<p>Associates the value of the <code>type_tag</code> pointer with the JavaScript object or
5070<a href="#napi_create_external">external</a>. <code>napi_check_object_type_tag()</code> can then be used to compare the tag
5071that was attached to the object with one owned by the addon to ensure that the
5072object has the right type.</p>
5073<p>If the object already has an associated type tag, this API will return
5074<code>napi_invalid_arg</code>.</p>
5075<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>
5076<div class="api_metadata">
5077<span>Added in: v14.8.0, v12.19.0</span>
5078<span>N-API version: 8</span>
5079</div>
5080<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,
5081                                       napi_value js_object,
5082                                       <span class="hljs-type">const</span> napi_type_tag* type_tag,
5083                                       <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
5084<ul>
5085<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5086<li><code>[in] js_object</code>: The JavaScript object or <a href="#napi_create_external">external</a> whose type tag to
5087examine.</li>
5088<li><code>[in] type_tag</code>: The tag with which to compare any tag found on the object.</li>
5089<li><code>[out] result</code>: Whether the type tag given matched the type tag on the
5090object. <code>false</code> is also returned if no type tag was found on the object.</li>
5091</ul>
5092<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5093<p>Compares the pointer given as <code>type_tag</code> with any that can be found on
5094<code>js_object</code>. If no tag is found on <code>js_object</code> or, if a tag is found but it does
5095not match <code>type_tag</code>, then <code>result</code> is set to <code>false</code>. If a tag is found and it
5096matches <code>type_tag</code>, then <code>result</code> is set to <code>true</code>.</p>
5097<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>
5098<div class="api_metadata">
5099<span>Added in: v8.0.0</span>
5100<span>N-API version: 5</span>
5101</div>
5102<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_add_finalizer</span><span class="hljs-params">(napi_env env,
5103                               napi_value js_object,
5104                               <span class="hljs-type">void</span>* finalize_data,
5105                               node_api_nogc_finalize finalize_cb,
5106                               <span class="hljs-type">void</span>* finalize_hint,
5107                               napi_ref* result)</span>;</code> <button class="copy-button">copy</button></pre>
5108<ul>
5109<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5110<li><code>[in] js_object</code>: The JavaScript object to which the native data will be
5111attached.</li>
5112<li><code>[in] finalize_data</code>: Optional data to be passed to <code>finalize_cb</code>.</li>
5113<li><code>[in] finalize_cb</code>: Native callback that will be used to free the
5114native data when the JavaScript object has been garbage-collected.
5115<a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
5116<li><code>[in] finalize_hint</code>: Optional contextual hint that is passed to the
5117finalize callback.</li>
5118<li><code>[out] result</code>: Optional reference to the JavaScript object.</li>
5119</ul>
5120<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5121<p>Adds a <code>napi_finalize</code> callback which will be called when the JavaScript object
5122in <code>js_object</code> has been garbage-collected.</p>
5123<p>This API can be called multiple times on a single JavaScript object.</p>
5124<p><em>Caution</em>: The optional returned reference (if obtained) should be deleted via
5125<a href="#napi_delete_reference"><code>napi_delete_reference</code></a> ONLY in response to the finalize callback
5126invocation. If it is deleted before then, then the finalize callback may never
5127be invoked. Therefore, when obtaining a reference a finalize callback is also
5128required in order to enable correct disposal of the reference.</p>
5129<h5><code>node_api_post_finalizer</code><span><a class="mark" href="#node_api_post_finalizer" id="node_api_post_finalizer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_post_finalizer"></a></h5>
5130<div class="api_metadata">
5131<span>Added in: v18.19.0</span>
5132</div>
5133<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
5134<pre><code class="language-c">napi_status <span class="hljs-title function_">node_api_post_finalizer</span><span class="hljs-params">(node_api_nogc_env env,
5135                                    napi_finalize finalize_cb,
5136                                    <span class="hljs-type">void</span>* finalize_data,
5137                                    <span class="hljs-type">void</span>* finalize_hint)</span>;</code> <button class="copy-button">copy</button></pre>
5138<ul>
5139<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5140<li><code>[in] finalize_cb</code>: Native callback that will be used to free the
5141native data when the JavaScript object has been garbage-collected.
5142<a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
5143<li><code>[in] finalize_data</code>: Optional data to be passed to <code>finalize_cb</code>.</li>
5144<li><code>[in] finalize_hint</code>: Optional contextual hint that is passed to the
5145finalize callback.</li>
5146</ul>
5147<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5148<p>Schedules a <code>napi_finalize</code> callback to be called asynchronously in the
5149event loop.</p>
5150<p>Normally, finalizers are called while the GC (garbage collector) collects
5151objects. At that point calling any Node-API that may cause changes in the GC
5152state will be disabled and will crash Node.js.</p>
5153<p><code>node_api_post_finalizer</code> helps to work around this limitation by allowing the
5154add-on to defer calls to such Node-APIs to a point in time outside of the GC
5155finalization.</p>
5156</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>
5157<p>Addon modules often need to leverage async helpers from libuv as part of their
5158implementation. This allows them to schedule work to be executed asynchronously
5159so that their methods can return in advance of the work being completed. This
5160allows them to avoid blocking overall execution of the Node.js application.</p>
5161<p>Node-API provides an ABI-stable interface for these
5162supporting functions which covers the most common asynchronous use cases.</p>
5163<p>Node-API defines the <code>napi_async_work</code> structure which is used to manage
5164asynchronous workers. Instances are created/deleted with
5165<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>
5166<p>The <code>execute</code> and <code>complete</code> callbacks are functions that will be
5167invoked when the executor is ready to execute and when it completes its
5168task respectively.</p>
5169<p>The <code>execute</code> function should avoid making any Node-API calls
5170that could result in the execution of JavaScript or interaction with
5171JavaScript objects. Most often, any code that needs to make Node-API
5172calls should be made in <code>complete</code> callback instead.
5173Avoid using the <code>napi_env</code> parameter in the execute callback as
5174it will likely execute JavaScript.</p>
5175<p>These functions implement the following interfaces:</p>
5176<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,
5177                                            <span class="hljs-type">void</span>* data)</span>;
5178<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,
5179                                             napi_status status,
5180                                             <span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button></pre>
5181<p>When these methods are invoked, the <code>data</code> parameter passed will be the
5182addon-provided <code>void*</code> data that was passed into the
5183<code>napi_create_async_work</code> call.</p>
5184<p>Once created the async worker can be queued
5185for execution using the <a href="#napi_queue_async_work"><code>napi_queue_async_work</code></a> function:</p>
5186<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work</span><span class="hljs-params">(node_api_nogc_env env,
5187                                  napi_async_work work)</span>;</code> <button class="copy-button">copy</button></pre>
5188<p><a href="#napi_cancel_async_work"><code>napi_cancel_async_work</code></a> can be used if the work needs
5189to be cancelled before the work has started execution.</p>
5190<p>After calling <a href="#napi_cancel_async_work"><code>napi_cancel_async_work</code></a>, the <code>complete</code> callback
5191will be invoked with a status value of <code>napi_cancelled</code>.
5192The work should not be deleted before the <code>complete</code>
5193callback invocation, even when it was cancelled.</p>
5194<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>
5195<div class="api_metadata">
5196<details class="changelog"><summary>History</summary>
5197<table>
5198<tbody><tr><th>Version</th><th>Changes</th></tr>
5199<tr><td>v8.6.0</td>
5200<td><p>Added <code>async_resource</code> and <code>async_resource_name</code> parameters.</p></td></tr>
5201<tr><td>v8.0.0</td>
5202<td><p><span>Added in: v8.0.0</span></p></td></tr>
5203</tbody></table>
5204</details>
5205<span>N-API version: 1</span>
5206</div>
5207<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_async_work</span><span class="hljs-params">(napi_env env,
5208                                   napi_value async_resource,
5209                                   napi_value async_resource_name,
5210                                   napi_async_execute_callback execute,
5211                                   napi_async_complete_callback complete,
5212                                   <span class="hljs-type">void</span>* data,
5213                                   napi_async_work* result)</span>;</code> <button class="copy-button">copy</button></pre>
5214<ul>
5215<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5216<li><code>[in] async_resource</code>: An optional object associated with the async work
5217that will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#initasyncid-type-triggerasyncid-resource"><code>init</code> hooks</a>.</li>
5218<li><code>[in] async_resource_name</code>: Identifier for the kind of resource that is being
5219provided for diagnostic information exposed by the <code>async_hooks</code> API.</li>
5220<li><code>[in] execute</code>: The native function which should be called to execute the
5221logic asynchronously. The given function is called from a worker pool thread
5222and can execute in parallel with the main event loop thread.</li>
5223<li><code>[in] complete</code>: The native function which will be called when the
5224asynchronous logic is completed or is cancelled. The given function is called
5225from the main event loop thread. <a href="#napi_async_complete_callback"><code>napi_async_complete_callback</code></a> provides
5226more details.</li>
5227<li><code>[in] data</code>: User-provided data context. This will be passed back into the
5228execute and complete functions.</li>
5229<li><code>[out] result</code>: <code>napi_async_work*</code> which is the handle to the newly created
5230async work.</li>
5231</ul>
5232<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5233<p>This API allocates a work object that is used to execute logic asynchronously.
5234It should be freed using <a href="#napi_delete_async_work"><code>napi_delete_async_work</code></a> once the work is no longer
5235required.</p>
5236<p><code>async_resource_name</code> should be a null-terminated, UTF-8-encoded string.</p>
5237<p>The <code>async_resource_name</code> identifier is provided by the user and should be
5238representative of the type of async work being performed. It is also recommended
5239to apply namespacing to the identifier, e.g. by including the module name. See
5240the <a href="async_hooks.html#type"><code>async_hooks</code> documentation</a> for more information.</p>
5241<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>
5242<div class="api_metadata">
5243<span>Added in: v8.0.0</span>
5244<span>N-API version: 1</span>
5245</div>
5246<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_async_work</span><span class="hljs-params">(napi_env env,
5247                                   napi_async_work work)</span>;</code> <button class="copy-button">copy</button></pre>
5248<ul>
5249<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5250<li><code>[in] work</code>: The handle returned by the call to <code>napi_create_async_work</code>.</li>
5251</ul>
5252<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5253<p>This API frees a previously allocated work object.</p>
5254<p>This API can be called even if there is a pending JavaScript exception.</p>
5255<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>
5256<div class="api_metadata">
5257<span>Added in: v8.0.0</span>
5258<span>N-API version: 1</span>
5259</div>
5260<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work</span><span class="hljs-params">(node_api_nogc_env env,
5261                                  napi_async_work work)</span>;</code> <button class="copy-button">copy</button></pre>
5262<ul>
5263<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5264<li><code>[in] work</code>: The handle returned by the call to <code>napi_create_async_work</code>.</li>
5265</ul>
5266<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5267<p>This API requests that the previously allocated work be scheduled
5268for execution. Once it returns successfully, this API must not be called again
5269with the same <code>napi_async_work</code> item or the result will be undefined.</p>
5270<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>
5271<div class="api_metadata">
5272<span>Added in: v8.0.0</span>
5273<span>N-API version: 1</span>
5274</div>
5275<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_cancel_async_work</span><span class="hljs-params">(node_api_nogc_env env,
5276                                   napi_async_work work)</span>;</code> <button class="copy-button">copy</button></pre>
5277<ul>
5278<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5279<li><code>[in] work</code>: The handle returned by the call to <code>napi_create_async_work</code>.</li>
5280</ul>
5281<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5282<p>This API cancels queued work if it has not yet
5283been started. If it has already started executing, it cannot be
5284cancelled and <code>napi_generic_failure</code> will be returned. If successful,
5285the <code>complete</code> callback will be invoked with a status value of
5286<code>napi_cancelled</code>. The work should not be deleted before the <code>complete</code>
5287callback invocation, even if it has been successfully cancelled.</p>
5288<p>This API can be called even if there is a pending JavaScript exception.</p>
5289</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>
5290<p>The simple asynchronous work APIs above may not be appropriate for every
5291scenario. When using any other asynchronous mechanism, the following APIs
5292are necessary to ensure an asynchronous operation is properly tracked by
5293the runtime.</p>
5294<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>
5295<div class="api_metadata">
5296<span>Added in: v8.6.0</span>
5297<span>N-API version: 1</span>
5298</div>
5299<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_init</span><span class="hljs-params">(napi_env env,
5300                            napi_value async_resource,
5301                            napi_value async_resource_name,
5302                            napi_async_context* result)</span></code> <button class="copy-button">copy</button></pre>
5303<ul>
5304<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5305<li><code>[in] async_resource</code>: Object associated with the async work
5306that 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
5307accessed by <a href="async_hooks.html#async_hooksexecutionasyncresource"><code>async_hooks.executionAsyncResource()</code></a>.</li>
5308<li><code>[in] async_resource_name</code>: Identifier for the kind of resource that is being
5309provided for diagnostic information exposed by the <code>async_hooks</code> API.</li>
5310<li><code>[out] result</code>: The initialized async context.</li>
5311</ul>
5312<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5313<p>The <code>async_resource</code> object needs to be kept alive until
5314<a href="#napi_async_destroy"><code>napi_async_destroy</code></a> to keep <code>async_hooks</code> related API acts correctly. In
5315order to retain ABI compatibility with previous versions, <code>napi_async_context</code>s
5316are not maintaining the strong reference to the <code>async_resource</code> objects to
5317avoid introducing causing memory leaks. However, if the <code>async_resource</code> is
5318garbage collected by JavaScript engine before the <code>napi_async_context</code> was
5319destroyed by <code>napi_async_destroy</code>, calling <code>napi_async_context</code> related APIs
5320like <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
5321problems like loss of async context when using the <code>AsyncLocalStorage</code> API.</p>
5322<p>In order to retain ABI compatibility with previous versions, passing <code>NULL</code>
5323for <code>async_resource</code> does not result in an error. However, this is not
5324recommended as this will result in undesirable behavior with  <code>async_hooks</code>
5325<a href="async_hooks.html#initasyncid-type-triggerasyncid-resource"><code>init</code> hooks</a> and <code>async_hooks.executionAsyncResource()</code> as the resource is
5326now required by the underlying <code>async_hooks</code> implementation in order to provide
5327the linkage between async callbacks.</p>
5328<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>
5329<div class="api_metadata">
5330<span>Added in: v8.6.0</span>
5331<span>N-API version: 1</span>
5332</div>
5333<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_destroy</span><span class="hljs-params">(napi_env env,
5334                               napi_async_context async_context)</span>;</code> <button class="copy-button">copy</button></pre>
5335<ul>
5336<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5337<li><code>[in] async_context</code>: The async context to be destroyed.</li>
5338</ul>
5339<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5340<p>This API can be called even if there is a pending JavaScript exception.</p>
5341<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>
5342<div class="api_metadata">
5343<details class="changelog"><summary>History</summary>
5344<table>
5345<tbody><tr><th>Version</th><th>Changes</th></tr>
5346<tr><td>v8.6.0</td>
5347<td><p>Added <code>async_context</code> parameter.</p></td></tr>
5348<tr><td>v8.0.0</td>
5349<td><p><span>Added in: v8.0.0</span></p></td></tr>
5350</tbody></table>
5351</details>
5352<span>N-API version: 1</span>
5353</div>
5354<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,
5355                                           napi_async_context async_context,
5356                                           napi_value recv,
5357                                           napi_value func,
5358                                           <span class="hljs-type">size_t</span> argc,
5359                                           <span class="hljs-type">const</span> napi_value* argv,
5360                                           napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
5361<ul>
5362<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5363<li><code>[in] async_context</code>: Context for the async operation that is
5364invoking the callback. This should normally be a value previously
5365obtained from <a href="#napi_async_init"><code>napi_async_init</code></a>.
5366In order to retain ABI compatibility with previous versions, passing <code>NULL</code>
5367for <code>async_context</code> does not result in an error. However, this results
5368in incorrect operation of async hooks. Potential issues include loss of
5369async context when using the <code>AsyncLocalStorage</code> API.</li>
5370<li><code>[in] recv</code>: The <code>this</code> value passed to the called function.</li>
5371<li><code>[in] func</code>: <code>napi_value</code> representing the JavaScript function to be invoked.</li>
5372<li><code>[in] argc</code>: The count of elements in the <code>argv</code> array.</li>
5373<li><code>[in] argv</code>: Array of JavaScript values as <code>napi_value</code> representing the
5374arguments to the function. If <code>argc</code> is zero this parameter may be
5375omitted by passing in <code>NULL</code>.</li>
5376<li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned.</li>
5377</ul>
5378<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5379<p>This method allows a JavaScript function object to be called from a native
5380add-on. This API is similar to <code>napi_call_function</code>. However, it is used to call
5381<em>from</em> native code back <em>into</em> JavaScript <em>after</em> returning from an async
5382operation (when there is no other script on the stack). It is a fairly simple
5383wrapper around <code>node::MakeCallback</code>.</p>
5384<p>Note it is <em>not</em> necessary to use <code>napi_make_callback</code> from within a
5385<code>napi_async_complete_callback</code>; in that situation the callback's async
5386context has already been set up, so a direct call to <code>napi_call_function</code>
5387is sufficient and appropriate. Use of the <code>napi_make_callback</code> function
5388may be required when implementing custom async behavior that does not use
5389<code>napi_create_async_work</code>.</p>
5390<p>Any <code>process.nextTick</code>s or Promises scheduled on the microtask queue by
5391JavaScript during the callback are ran before returning back to C/C++.</p>
5392<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>
5393<div class="api_metadata">
5394<span>Added in: v9.6.0</span>
5395<span>N-API version: 3</span>
5396</div>
5397<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,
5398                                                 napi_value resource_object,
5399                                                 napi_async_context context,
5400                                                 napi_callback_scope* result)</span></code> <button class="copy-button">copy</button></pre>
5401<ul>
5402<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5403<li><code>[in] resource_object</code>: An object associated with the async work
5404that will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#initasyncid-type-triggerasyncid-resource"><code>init</code> hooks</a>. This
5405parameter has been deprecated and is ignored at runtime. Use the
5406<code>async_resource</code> parameter in <a href="#napi_async_init"><code>napi_async_init</code></a> instead.</li>
5407<li><code>[in] context</code>: Context for the async operation that is invoking the callback.
5408This should be a value previously obtained from <a href="#napi_async_init"><code>napi_async_init</code></a>.</li>
5409<li><code>[out] result</code>: The newly created scope.</li>
5410</ul>
5411<p>There are cases (for example, resolving promises) where it is
5412necessary to have the equivalent of the scope associated with a callback
5413in place when making certain Node-API calls. If there is no other script on
5414the stack the <a href="#napi_open_callback_scope"><code>napi_open_callback_scope</code></a> and
5415<a href="#napi_close_callback_scope"><code>napi_close_callback_scope</code></a> functions can be used to open/close
5416the required scope.</p>
5417<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>
5418<div class="api_metadata">
5419<span>Added in: v9.6.0</span>
5420<span>N-API version: 3</span>
5421</div>
5422<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,
5423                                                  napi_callback_scope scope)</span></code> <button class="copy-button">copy</button></pre>
5424<ul>
5425<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5426<li><code>[in] scope</code>: The scope to be closed.</li>
5427</ul>
5428<p>This API can be called even if there is a pending JavaScript exception.</p>
5429</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>
5430<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>
5431<div class="api_metadata">
5432<span>Added in: v8.4.0</span>
5433<span>N-API version: 1</span>
5434</div>
5435<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span>
5436  <span class="hljs-type">uint32_t</span> major;
5437  <span class="hljs-type">uint32_t</span> minor;
5438  <span class="hljs-type">uint32_t</span> patch;
5439  <span class="hljs-type">const</span> <span class="hljs-type">char</span>* release;
5440} napi_node_version;
5441
5442napi_status <span class="hljs-title function_">napi_get_node_version</span><span class="hljs-params">(node_api_nogc_env env,
5443                                  <span class="hljs-type">const</span> napi_node_version** version)</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>[out] version</code>: A pointer to version information for Node.js itself.</li>
5447</ul>
5448<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5449<p>This function fills the <code>version</code> struct with the major, minor, and patch
5450version of Node.js that is currently running, and the <code>release</code> field with the
5451value of <a href="process.html#processrelease"><code>process.release.name</code></a>.</p>
5452<p>The returned buffer is statically allocated and does not need to be freed.</p>
5453<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>
5454<div class="api_metadata">
5455<span>Added in: v8.0.0</span>
5456<span>N-API version: 1</span>
5457</div>
5458<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_version</span><span class="hljs-params">(node_api_nogc_env env,
5459                             <span class="hljs-type">uint32_t</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
5460<ul>
5461<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5462<li><code>[out] result</code>: The highest version of Node-API supported.</li>
5463</ul>
5464<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5465<p>This API returns the highest Node-API version supported by the
5466Node.js runtime. Node-API is planned to be additive such that
5467newer releases of Node.js may support additional API functions.
5468In order to allow an addon to use a newer function when running with
5469versions of Node.js that support it, while providing
5470fallback behavior when running with Node.js versions that don't
5471support it:</p>
5472<ul>
5473<li>Call <code>napi_get_version()</code> to determine if the API is available.</li>
5474<li>If available, dynamically load a pointer to the function using <code>uv_dlsym()</code>.</li>
5475<li>Use the dynamically loaded pointer to invoke the function.</li>
5476<li>If the function is not available, provide an alternate implementation
5477that does not use the function.</li>
5478</ul>
5479</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>
5480<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>
5481<div class="api_metadata">
5482<span>Added in: v8.5.0</span>
5483<span>N-API version: 1</span>
5484</div>
5485<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_adjust_external_memory</span><span class="hljs-params">(node_api_nogc_env env,
5486                                                    <span class="hljs-type">int64_t</span> change_in_bytes,
5487                                                    <span class="hljs-type">int64_t</span>* result)</span>;</code> <button class="copy-button">copy</button></pre>
5488<ul>
5489<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5490<li><code>[in] change_in_bytes</code>: The change in externally allocated memory that is kept
5491alive by JavaScript objects.</li>
5492<li><code>[out] result</code>: The adjusted value</li>
5493</ul>
5494<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5495<p>This function gives V8 an indication of the amount of externally allocated
5496memory that is kept alive by JavaScript objects (i.e. a JavaScript object
5497that points to its own memory allocated by a native addon). Registering
5498externally allocated memory will trigger global garbage collections more
5499often than it would otherwise.</p>
5500</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>
5501<p>Node-API provides facilities for creating <code>Promise</code> objects as described in
5502<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
5503objects. When a promise is created by <code>napi_create_promise()</code>, a "deferred"
5504object is created and returned alongside the <code>Promise</code>. The deferred object is
5505bound to the created <code>Promise</code> and is the only means to resolve or reject the
5506<code>Promise</code> using <code>napi_resolve_deferred()</code> or <code>napi_reject_deferred()</code>. The
5507deferred object that is created by <code>napi_create_promise()</code> is freed by
5508<code>napi_resolve_deferred()</code> or <code>napi_reject_deferred()</code>. The <code>Promise</code> object may
5509be returned to JavaScript where it can be used in the usual fashion.</p>
5510<p>For example, to create a promise and pass it to an asynchronous worker:</p>
5511<pre><code class="language-c">napi_deferred deferred;
5512napi_value promise;
5513napi_status status;
5514
5515<span class="hljs-comment">// Create the promise.</span>
5516status = napi_create_promise(env, &#x26;deferred, &#x26;promise);
5517<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
5518
5519<span class="hljs-comment">// Pass the deferred to a function that performs an asynchronous action.</span>
5520do_something_asynchronous(deferred);
5521
5522<span class="hljs-comment">// Return the promise to JS</span>
5523<span class="hljs-keyword">return</span> promise;</code> <button class="copy-button">copy</button></pre>
5524<p>The above function <code>do_something_asynchronous()</code> would perform its asynchronous
5525action and then it would resolve or reject the deferred, thereby concluding the
5526promise and freeing the deferred:</p>
5527<pre><code class="language-c">napi_deferred deferred;
5528napi_value undefined;
5529napi_status status;
5530
5531<span class="hljs-comment">// Create a value with which to conclude the deferred.</span>
5532status = napi_get_undefined(env, &#x26;undefined);
5533<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
5534
5535<span class="hljs-comment">// Resolve or reject the promise associated with the deferred depending on</span>
5536<span class="hljs-comment">// whether the asynchronous action succeeded.</span>
5537<span class="hljs-keyword">if</span> (asynchronous_action_succeeded) {
5538  status = napi_resolve_deferred(env, deferred, undefined);
5539} <span class="hljs-keyword">else</span> {
5540  status = napi_reject_deferred(env, deferred, undefined);
5541}
5542<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
5543
5544<span class="hljs-comment">// At this point the deferred has been freed, so we should assign NULL to it.</span>
5545deferred = <span class="hljs-literal">NULL</span>;</code> <button class="copy-button">copy</button></pre>
5546<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>
5547<div class="api_metadata">
5548<span>Added in: v8.5.0</span>
5549<span>N-API version: 1</span>
5550</div>
5551<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_promise</span><span class="hljs-params">(napi_env env,
5552                                napi_deferred* deferred,
5553                                napi_value* promise)</span>;</code> <button class="copy-button">copy</button></pre>
5554<ul>
5555<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5556<li><code>[out] deferred</code>: A newly created deferred object which can later be passed to
5557<code>napi_resolve_deferred()</code> or <code>napi_reject_deferred()</code> to resolve resp. reject
5558the associated promise.</li>
5559<li><code>[out] promise</code>: The JavaScript promise associated with the deferred object.</li>
5560</ul>
5561<p>Returns <code>napi_ok</code> if the API succeeded.</p>
5562<p>This API creates a deferred object and a JavaScript promise.</p>
5563<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>
5564<div class="api_metadata">
5565<span>Added in: v8.5.0</span>
5566<span>N-API version: 1</span>
5567</div>
5568<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_resolve_deferred</span><span class="hljs-params">(napi_env env,
5569                                  napi_deferred deferred,
5570                                  napi_value resolution)</span>;</code> <button class="copy-button">copy</button></pre>
5571<ul>
5572<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5573<li><code>[in] deferred</code>: The deferred object whose associated promise to resolve.</li>
5574<li><code>[in] resolution</code>: The value with which to resolve the promise.</li>
5575</ul>
5576<p>This API resolves a JavaScript promise by way of the deferred object
5577with which it is associated. Thus, it can only be used to resolve JavaScript
5578promises for which the corresponding deferred object is available. This
5579effectively means that the promise must have been created using
5580<code>napi_create_promise()</code> and the deferred object returned from that call must
5581have been retained in order to be passed to this API.</p>
5582<p>The deferred object is freed upon successful completion.</p>
5583<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>
5584<div class="api_metadata">
5585<span>Added in: v8.5.0</span>
5586<span>N-API version: 1</span>
5587</div>
5588<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_reject_deferred</span><span class="hljs-params">(napi_env env,
5589                                 napi_deferred deferred,
5590                                 napi_value rejection)</span>;</code> <button class="copy-button">copy</button></pre>
5591<ul>
5592<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5593<li><code>[in] deferred</code>: The deferred object whose associated promise to resolve.</li>
5594<li><code>[in] rejection</code>: The value with which to reject the promise.</li>
5595</ul>
5596<p>This API rejects a JavaScript promise by way of the deferred object
5597with which it is associated. Thus, it can only be used to reject JavaScript
5598promises for which the corresponding deferred object is available. This
5599effectively means that the promise must have been created using
5600<code>napi_create_promise()</code> and the deferred object returned from that call must
5601have been retained in order to be passed to this API.</p>
5602<p>The deferred object is freed upon successful completion.</p>
5603<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>
5604<div class="api_metadata">
5605<span>Added in: v8.5.0</span>
5606<span>N-API version: 1</span>
5607</div>
5608<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_promise</span><span class="hljs-params">(napi_env env,
5609                            napi_value value,
5610                            <span class="hljs-type">bool</span>* is_promise)</span>;</code> <button class="copy-button">copy</button></pre>
5611<ul>
5612<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5613<li><code>[in] value</code>: The value to examine</li>
5614<li><code>[out] is_promise</code>: Flag indicating whether <code>promise</code> is a native promise
5615object (that is, a promise object created by the underlying engine).</li>
5616</ul>
5617</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>
5618<p>Node-API provides an API for executing a string containing JavaScript using the
5619underlying JavaScript engine.</p>
5620<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>
5621<div class="api_metadata">
5622<span>Added in: v8.5.0</span>
5623<span>N-API version: 1</span>
5624</div>
5625<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,
5626                                        napi_value script,
5627                                        napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
5628<ul>
5629<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5630<li><code>[in] script</code>: A JavaScript string containing the script to execute.</li>
5631<li><code>[out] result</code>: The value resulting from having executed the script.</li>
5632</ul>
5633<p>This function executes a string of JavaScript code and returns its result with
5634the following caveats:</p>
5635<ul>
5636<li>Unlike <code>eval</code>, this function does not allow the script to access the current
5637lexical scope, and therefore also does not allow to access the
5638<a href="modules.html#the-module-scope">module scope</a>, meaning that pseudo-globals such as <code>require</code> will not be
5639available.</li>
5640<li>The script can access the <a href="globals.html">global scope</a>. Function and <code>var</code> declarations
5641in the script will be added to the <a href="globals.html#global"><code>global</code></a> object. Variable declarations
5642made using <code>let</code> and <code>const</code> will be visible globally, but will not be added
5643to the <a href="globals.html#global"><code>global</code></a> object.</li>
5644<li>The value of <code>this</code> is <a href="globals.html#global"><code>global</code></a> within the script.</li>
5645</ul>
5646</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>
5647<p>Node-API provides a function for getting the current event loop associated with
5648a specific <code>napi_env</code>.</p>
5649<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>
5650<div class="api_metadata">
5651<span>Added in: v9.3.0, v8.10.0</span>
5652<span>N-API version: 2</span>
5653</div>
5654<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_uv_event_loop</span><span class="hljs-params">(node_api_nogc_env env,
5655                                               <span class="hljs-keyword">struct</span> uv_loop_s** loop)</span>;</code> <button class="copy-button">copy</button></pre>
5656<ul>
5657<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5658<li><code>[out] loop</code>: The current libuv loop instance.</li>
5659</ul>
5660</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>
5661<p>JavaScript functions can normally only be called from a native addon's main
5662thread. If an addon creates additional threads, then Node-API functions that
5663require a <code>napi_env</code>, <code>napi_value</code>, or <code>napi_ref</code> must not be called from those
5664threads.</p>
5665<p>When an addon has additional threads and JavaScript functions need to be invoked
5666based on the processing completed by those threads, those threads must
5667communicate with the addon's main thread so that the main thread can invoke the
5668JavaScript function on their behalf. The thread-safe function APIs provide an
5669easy way to do this.</p>
5670<p>These APIs provide the type <code>napi_threadsafe_function</code> as well as APIs to
5671create, destroy, and call objects of this type.
5672<code>napi_create_threadsafe_function()</code> creates a persistent reference to a
5673<code>napi_value</code> that holds a JavaScript function which can be called from multiple
5674threads. The calls happen asynchronously. This means that values with which the
5675JavaScript callback is to be called will be placed in a queue, and, for each
5676value in the queue, a call will eventually be made to the JavaScript function.</p>
5677<p>Upon creation of a <code>napi_threadsafe_function</code> a <code>napi_finalize</code> callback can be
5678provided. This callback will be invoked on the main thread when the thread-safe
5679function is about to be destroyed. It receives the context and the finalize data
5680given during construction, and provides an opportunity for cleaning up after the
5681threads e.g. by calling <code>uv_thread_join()</code>. <strong>Aside from the main loop thread,
5682no threads should be using the thread-safe function after the finalize callback
5683completes.</strong></p>
5684<p>The <code>context</code> given during the call to <code>napi_create_threadsafe_function()</code> can
5685be retrieved from any thread with a call to
5686<code>napi_get_threadsafe_function_context()</code>.</p>
5687<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>
5688<p><code>napi_call_threadsafe_function()</code> can be used for initiating a call into
5689JavaScript. <code>napi_call_threadsafe_function()</code> accepts a parameter which controls
5690whether the API behaves blockingly. If set to <code>napi_tsfn_nonblocking</code>, the API
5691behaves non-blockingly, returning <code>napi_queue_full</code> if the queue was full,
5692preventing data from being successfully added to the queue. If set to
5693<code>napi_tsfn_blocking</code>, the API blocks until space becomes available in the queue.
5694<code>napi_call_threadsafe_function()</code> never blocks if the thread-safe function was
5695created with a maximum queue size of 0.</p>
5696<p><code>napi_call_threadsafe_function()</code> should not be called with <code>napi_tsfn_blocking</code>
5697from a JavaScript thread, because, if the queue is full, it may cause the
5698JavaScript thread to deadlock.</p>
5699<p>The actual call into JavaScript is controlled by the callback given via the
5700<code>call_js_cb</code> parameter. <code>call_js_cb</code> is invoked on the main thread once for each
5701value that was placed into the queue by a successful call to
5702<code>napi_call_threadsafe_function()</code>. If such a callback is not given, a default
5703callback will be used, and the resulting JavaScript call will have no arguments.
5704The <code>call_js_cb</code> callback receives the JavaScript function to call as a
5705<code>napi_value</code> in its parameters, as well as the <code>void*</code> context pointer used when
5706creating the <code>napi_threadsafe_function</code>, and the next data pointer that was
5707created by one of the secondary threads. The callback can then use an API such
5708as <code>napi_call_function()</code> to call into JavaScript.</p>
5709<p>The callback may also be invoked with <code>env</code> and <code>call_js_cb</code> both set to <code>NULL</code>
5710to indicate that calls into JavaScript are no longer possible, while items
5711remain in the queue that may need to be freed. This normally occurs when the
5712Node.js process exits while there is a thread-safe function still active.</p>
5713<p>It is not necessary to call into JavaScript via <code>napi_make_callback()</code> because
5714Node-API runs <code>call_js_cb</code> in a context appropriate for callbacks.</p>
5715<p>Zero or more queued items may be invoked in each tick of the event loop.
5716Applications should not depend on a specific behavior other than progress in
5717invoking callbacks will be made and events will be invoked
5718as time moves forward.</p>
5719<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>
5720<p>Threads can be added to and removed from a <code>napi_threadsafe_function</code> object
5721during its existence. Thus, in addition to specifying an initial number of
5722threads upon creation, <code>napi_acquire_threadsafe_function</code> can be called to
5723indicate that a new thread will start making use of the thread-safe function.
5724Similarly, <code>napi_release_threadsafe_function</code> can be called to indicate that an
5725existing thread will stop making use of the thread-safe function.</p>
5726<p><code>napi_threadsafe_function</code> objects are destroyed when every thread which uses
5727the object has called <code>napi_release_threadsafe_function()</code> or has received a
5728return status of <code>napi_closing</code> in response to a call to
5729<code>napi_call_threadsafe_function</code>. The queue is emptied before the
5730<code>napi_threadsafe_function</code> is destroyed. <code>napi_release_threadsafe_function()</code>
5731should be the last API call made in conjunction with a given
5732<code>napi_threadsafe_function</code>, because after the call completes, there is no
5733guarantee that the <code>napi_threadsafe_function</code> is still allocated. For the same
5734reason, do not use a thread-safe function
5735after receiving a return value of <code>napi_closing</code> in response to a call to
5736<code>napi_call_threadsafe_function</code>. Data associated with the
5737<code>napi_threadsafe_function</code> can be freed in its <code>napi_finalize</code> callback which
5738was passed to <code>napi_create_threadsafe_function()</code>. The parameter
5739<code>initial_thread_count</code> of <code>napi_create_threadsafe_function</code> marks the initial
5740number of acquisitions of the thread-safe functions, instead of calling
5741<code>napi_acquire_threadsafe_function</code> multiple times at creation.</p>
5742<p>Once the number of threads making use of a <code>napi_threadsafe_function</code> reaches
5743zero, no further threads can start making use of it by calling
5744<code>napi_acquire_threadsafe_function()</code>. In fact, all subsequent API calls
5745associated with it, except <code>napi_release_threadsafe_function()</code>, will return an
5746error value of <code>napi_closing</code>.</p>
5747<p>The thread-safe function can be "aborted" by giving a value of <code>napi_tsfn_abort</code>
5748to <code>napi_release_threadsafe_function()</code>. This will cause all subsequent APIs
5749associated with the thread-safe function except
5750<code>napi_release_threadsafe_function()</code> to return <code>napi_closing</code> even before its
5751reference count reaches zero. In particular, <code>napi_call_threadsafe_function()</code>
5752will return <code>napi_closing</code>, thus informing the threads that it is no longer
5753possible to make asynchronous calls to the thread-safe function. This can be
5754used as a criterion for terminating the thread. <strong>Upon receiving a return value
5755of <code>napi_closing</code> from <code>napi_call_threadsafe_function()</code> a thread must not use
5756the thread-safe function anymore because it is no longer guaranteed to
5757be allocated.</strong></p>
5758<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>
5759<p>Similarly to libuv handles, thread-safe functions can be "referenced" and
5760"unreferenced". A "referenced" thread-safe function will cause the event loop on
5761the thread on which it is created to remain alive until the thread-safe function
5762is destroyed. In contrast, an "unreferenced" thread-safe function will not
5763prevent the event loop from exiting. The APIs <code>napi_ref_threadsafe_function</code> and
5764<code>napi_unref_threadsafe_function</code> exist for this purpose.</p>
5765<p>Neither does <code>napi_unref_threadsafe_function</code> mark the thread-safe functions as
5766able to be destroyed nor does <code>napi_ref_threadsafe_function</code> prevent it from
5767being destroyed.</p>
5768<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>
5769<div class="api_metadata">
5770<details class="changelog"><summary>History</summary>
5771<table>
5772<tbody><tr><th>Version</th><th>Changes</th></tr>
5773<tr><td>v12.6.0, v10.17.0</td>
5774<td><p>Made <code>func</code> parameter optional with custom <code>call_js_cb</code>.</p></td></tr>
5775<tr><td>v10.6.0</td>
5776<td><p><span>Added in: v10.6.0</span></p></td></tr>
5777</tbody></table>
5778</details>
5779<span>N-API version: 4</span>
5780</div>
5781<pre><code class="language-c">NAPI_EXTERN napi_status
5782<span class="hljs-title function_">napi_create_threadsafe_function</span><span class="hljs-params">(napi_env env,
5783                                napi_value func,
5784                                napi_value async_resource,
5785                                napi_value async_resource_name,
5786                                <span class="hljs-type">size_t</span> max_queue_size,
5787                                <span class="hljs-type">size_t</span> initial_thread_count,
5788                                <span class="hljs-type">void</span>* thread_finalize_data,
5789                                napi_finalize thread_finalize_cb,
5790                                <span class="hljs-type">void</span>* context,
5791                                napi_threadsafe_function_call_js call_js_cb,
5792                                napi_threadsafe_function* result)</span>;</code> <button class="copy-button">copy</button></pre>
5793<ul>
5794<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5795<li><code>[in] func</code>: An optional JavaScript function to call from another thread. It
5796must be provided if <code>NULL</code> is passed to <code>call_js_cb</code>.</li>
5797<li><code>[in] async_resource</code>: An optional object associated with the async work that
5798will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#initasyncid-type-triggerasyncid-resource"><code>init</code> hooks</a>.</li>
5799<li><code>[in] async_resource_name</code>: A JavaScript string to provide an identifier for
5800the kind of resource that is being provided for diagnostic information exposed
5801by the <code>async_hooks</code> API.</li>
5802<li><code>[in] max_queue_size</code>: Maximum size of the queue. <code>0</code> for no limit.</li>
5803<li><code>[in] initial_thread_count</code>: The initial number of acquisitions, i.e. the
5804initial number of threads, including the main thread, which will be making use
5805of this function.</li>
5806<li><code>[in] thread_finalize_data</code>: Optional data to be passed to <code>thread_finalize_cb</code>.</li>
5807<li><code>[in] thread_finalize_cb</code>: Optional function to call when the
5808<code>napi_threadsafe_function</code> is being destroyed.</li>
5809<li><code>[in] context</code>: Optional data to attach to the resulting
5810<code>napi_threadsafe_function</code>.</li>
5811<li><code>[in] call_js_cb</code>: Optional callback which calls the JavaScript function in
5812response to a call on a different thread. This callback will be called on the
5813main thread. If not given, the JavaScript function will be called with no
5814parameters and with <code>undefined</code> as its <code>this</code> value.
5815<a href="#napi_threadsafe_function_call_js"><code>napi_threadsafe_function_call_js</code></a> provides more details.</li>
5816<li><code>[out] result</code>: The asynchronous thread-safe JavaScript function.</li>
5817</ul>
5818<p><strong>Change History:</strong></p>
5819<ul>
5820<li>
5821<p>Experimental (<code>NAPI_EXPERIMENTAL</code> is defined):</p>
5822<p>Uncaught exceptions thrown in <code>call_js_cb</code> are handled with the
5823<a href="process.html#event-uncaughtexception"><code>'uncaughtException'</code></a> event, instead of being ignored.</p>
5824</li>
5825</ul>
5826<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>
5827<div class="api_metadata">
5828<span>Added in: v10.6.0</span>
5829<span>N-API version: 4</span>
5830</div>
5831<pre><code class="language-c">NAPI_EXTERN napi_status
5832<span class="hljs-title function_">napi_get_threadsafe_function_context</span><span class="hljs-params">(napi_threadsafe_function func,
5833                                     <span class="hljs-type">void</span>** result)</span>;</code> <button class="copy-button">copy</button></pre>
5834<ul>
5835<li><code>[in] func</code>: The thread-safe function for which to retrieve the context.</li>
5836<li><code>[out] result</code>: The location where to store the context.</li>
5837</ul>
5838<p>This API may be called from any thread which makes use of <code>func</code>.</p>
5839<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>
5840<div class="api_metadata">
5841<details class="changelog"><summary>History</summary>
5842<table>
5843<tbody><tr><th>Version</th><th>Changes</th></tr>
5844<tr><td>v14.5.0</td>
5845<td><p>Support for <code>napi_would_deadlock</code> has been reverted.</p></td></tr>
5846<tr><td>v14.1.0</td>
5847<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>
5848<tr><td>v10.6.0</td>
5849<td><p><span>Added in: v10.6.0</span></p></td></tr>
5850</tbody></table>
5851</details>
5852<span>N-API version: 4</span>
5853</div>
5854<pre><code class="language-c">NAPI_EXTERN napi_status
5855<span class="hljs-title function_">napi_call_threadsafe_function</span><span class="hljs-params">(napi_threadsafe_function func,
5856                              <span class="hljs-type">void</span>* data,
5857                              napi_threadsafe_function_call_mode is_blocking)</span>;</code> <button class="copy-button">copy</button></pre>
5858<ul>
5859<li><code>[in] func</code>: The asynchronous thread-safe JavaScript function to invoke.</li>
5860<li><code>[in] data</code>: Data to send into JavaScript via the callback <code>call_js_cb</code>
5861provided during the creation of the thread-safe JavaScript function.</li>
5862<li><code>[in] is_blocking</code>: Flag whose value can be either <code>napi_tsfn_blocking</code> to
5863indicate that the call should block if the queue is full or
5864<code>napi_tsfn_nonblocking</code> to indicate that the call should return immediately
5865with a status of <code>napi_queue_full</code> whenever the queue is full.</li>
5866</ul>
5867<p>This API should not be called with <code>napi_tsfn_blocking</code> from a JavaScript
5868thread, because, if the queue is full, it may cause the JavaScript thread to
5869deadlock.</p>
5870<p>This API will return <code>napi_closing</code> if <code>napi_release_threadsafe_function()</code> was
5871called with <code>abort</code> set to <code>napi_tsfn_abort</code> from any thread. The value is only
5872added to the queue if the API returns <code>napi_ok</code>.</p>
5873<p>This API may be called from any thread which makes use of <code>func</code>.</p>
5874<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>
5875<div class="api_metadata">
5876<span>Added in: v10.6.0</span>
5877<span>N-API version: 4</span>
5878</div>
5879<pre><code class="language-c">NAPI_EXTERN napi_status
5880<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>
5881<ul>
5882<li><code>[in] func</code>: The asynchronous thread-safe JavaScript function to start making
5883use of.</li>
5884</ul>
5885<p>A thread should call this API before passing <code>func</code> to any other thread-safe
5886function APIs to indicate that it will be making use of <code>func</code>. This prevents
5887<code>func</code> from being destroyed when all other threads have stopped making use of
5888it.</p>
5889<p>This API may be called from any thread which will start making use of <code>func</code>.</p>
5890<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>
5891<div class="api_metadata">
5892<span>Added in: v10.6.0</span>
5893<span>N-API version: 4</span>
5894</div>
5895<pre><code class="language-c">NAPI_EXTERN napi_status
5896<span class="hljs-title function_">napi_release_threadsafe_function</span><span class="hljs-params">(napi_threadsafe_function func,
5897                                 napi_threadsafe_function_release_mode mode)</span>;</code> <button class="copy-button">copy</button></pre>
5898<ul>
5899<li><code>[in] func</code>: The asynchronous thread-safe JavaScript function whose reference
5900count to decrement.</li>
5901<li><code>[in] mode</code>: Flag whose value can be either <code>napi_tsfn_release</code> to indicate
5902that the current thread will make no further calls to the thread-safe
5903function, or <code>napi_tsfn_abort</code> to indicate that in addition to the current
5904thread, no other thread should make any further calls to the thread-safe
5905function. If set to <code>napi_tsfn_abort</code>, further calls to
5906<code>napi_call_threadsafe_function()</code> will return <code>napi_closing</code>, and no further
5907values will be placed in the queue.</li>
5908</ul>
5909<p>A thread should call this API when it stops making use of <code>func</code>. Passing <code>func</code>
5910to any thread-safe APIs after having called this API has undefined results, as
5911<code>func</code> may have been destroyed.</p>
5912<p>This API may be called from any thread which will stop making use of <code>func</code>.</p>
5913<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>
5914<div class="api_metadata">
5915<span>Added in: v10.6.0</span>
5916<span>N-API version: 4</span>
5917</div>
5918<pre><code class="language-c">NAPI_EXTERN napi_status
5919<span class="hljs-title function_">napi_ref_threadsafe_function</span><span class="hljs-params">(node_api_nogc_env env, napi_threadsafe_function func)</span>;</code> <button class="copy-button">copy</button></pre>
5920<ul>
5921<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5922<li><code>[in] func</code>: The thread-safe function to reference.</li>
5923</ul>
5924<p>This API is used to indicate that the event loop running on the main thread
5925should 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
5926also idempotent.</p>
5927<p>Neither does <code>napi_unref_threadsafe_function</code> mark the thread-safe functions as
5928able to be destroyed nor does <code>napi_ref_threadsafe_function</code> prevent it from
5929being destroyed. <code>napi_acquire_threadsafe_function</code> and
5930<code>napi_release_threadsafe_function</code> are available for that purpose.</p>
5931<p>This API may only be called from the main thread.</p>
5932<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>
5933<div class="api_metadata">
5934<span>Added in: v10.6.0</span>
5935<span>N-API version: 4</span>
5936</div>
5937<pre><code class="language-c">NAPI_EXTERN napi_status
5938<span class="hljs-title function_">napi_unref_threadsafe_function</span><span class="hljs-params">(node_api_nogc_env env, napi_threadsafe_function func)</span>;</code> <button class="copy-button">copy</button></pre>
5939<ul>
5940<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5941<li><code>[in] func</code>: The thread-safe function to unreference.</li>
5942</ul>
5943<p>This API is used to indicate that the event loop running on the main thread
5944may 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
5945idempotent.</p>
5946<p>This API may only be called from the main thread.</p>
5947</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>
5948<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>
5949<div class="api_metadata">
5950<span>Added in: v15.9.0, v14.18.0, v12.22.0</span>
5951<span>N-API version: 9</span>
5952</div>
5953<pre><code class="language-c">NAPI_EXTERN napi_status
5954<span class="hljs-title function_">node_api_get_module_file_name</span><span class="hljs-params">(node_api_nogc_env env, <span class="hljs-type">const</span> <span class="hljs-type">char</span>** result)</span>;
5955</code> <button class="copy-button">copy</button></pre>
5956<ul>
5957<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5958<li><code>[out] result</code>: A URL containing the absolute path of the
5959location from which the add-on was loaded. For a file on the local
5960file system it will start with <code>file://</code>. The string is null-terminated and
5961owned by <code>env</code> and must thus not be modified or freed.</li>
5962</ul>
5963<p><code>result</code> may be an empty string if the add-on loading process fails to establish
5964the add-on's file name during loading.</p></section>
5965        <!-- API END -->
5966      </div>
5967    </div>
5968  </div>
5969</body>
5970</html>
5971