Lines Matching +full:code +full:-
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>
11 <link rel="canonical" href="https://nodejs.org/api/n-api.html">
15 <body class="alt apidoc" id="api-section-n-api">
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>
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…
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>
92 <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code reposito…
96 <div id="column1" data-id="n-api" class="interior">
98 <div class="header-container">
100 …<button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-la…
101 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
103 …-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.6…
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-2…
106 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
108 …-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.4…
114 <li class="pinned-header">Node.js v18.20.1</li>
116 <li class="picker-header">
118 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span>
123 <li><span class="stability_2"><a href="#node-api">Node-API</a></span>
125 <li><a href="#implications-of-abi-stability">Implications of ABI stability</a></li>
128 <li><a href="#build-tools">Build tools</a>
130 <li><a href="#node-gyp">node-gyp</a></li>
134 <li><a href="#uploading-precompiled-binaries">Uploading precompiled binaries</a>
136 <li><a href="#node-pre-gyp">node-pre-gyp</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>
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>
151 <li><a href="#basic-node-api-data-types">Basic Node-API data types</a>
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…
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 …i><a href="#napi_threadsafe_function_release_mode"><code>napi_threadsafe_function_release_mode</co…
160 <li><a href="#napi_threadsafe_function_call_mode"><code>napi_threadsafe_function_call_mode</code></…
161 <li><a href="#node-api-memory-management-types">Node-API memory management types</a>
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>
170 <li><a href="#node-api-callback-types">Node-API callback types</a>
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>…
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></…
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>
185 <li><a href="#error-handling">Error handling</a>
187 <li><a href="#return-values">Return values</a>
189 <li><a href="#napi_get_last_error_info"><code>napi_get_last_error_info</code></a></li>
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>…
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>
209 <li><a href="#fatal-errors">Fatal errors</a>
211 <li><a href="#napi_fatal_error"><code>napi_fatal_error</code></a></li>
216 <li><a href="#object-lifetime-management">Object lifetime management</a>
218 <li><a href="#making-handle-lifespan-shorter-than-that-of-the-native-method">Making handle lifespan…
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></…
223 <li><a href="#napi_close_escapable_handle_scope"><code>napi_close_escapable_handle_scope</code></a>…
224 <li><a href="#napi_escape_handle"><code>napi_escape_handle</code></a></li>
227 <li><a href="#references-to-values-with-a-lifespan-longer-than-that-of-the-native-method">Reference…
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>
236 <li><a href="#cleanup-on-exit-of-the-current-nodejs-environment">Cleanup on exit of the current Nod…
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>
244 <li><a href="#finalization-on-the-exit-of-the-nodejs-environment">Finalization on the exit of the N…
247 <li><a href="#module-registration">Module registration</a></li>
248 <li><a href="#working-with-javascript-values">Working with JavaScript values</a>
250 <li><a href="#enum-types">Enum types</a>
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>
259 <li><a href="#object-creation-functions">Object creation functions</a>
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></…
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>
277 <li><a href="#functions-to-convert-from-c-types-to-node-api">Functions to convert from C types to N…
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 …><a href="#node_api_create_external_string_latin1"><code>node_api_create_external_string_latin1</c…
288 <li><a href="#napi_create_string_utf16"><code>napi_create_string_utf16</code></a></li>
289 …"><a href="#node_api_create_external_string_utf16"><code>node_api_create_external_string_utf16</co…
290 <li><a href="#napi_create_string_utf8"><code>napi_create_string_utf8</code></a></li>
293 <li><a href="#functions-to-convert-from-node-api-to-c-types">Functions to convert from Node-API to …
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>
316 <li><a href="#functions-to-get-global-instances">Functions to get global instances</a>
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>
326 <li><a href="#working-with-javascript-values-and-abstract-operations">Working with JavaScript value…
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>
346 <li><a href="#working-with-javascript-properties">Working with JavaScript properties</a>
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>
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>
377 <li><a href="#working-with-javascript-functions">Working with JavaScript functions</a>
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>
386 <li><a href="#object-wrap">Object wrap</a>
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>
396 …an class="stability_1"><a href="#node_api_post_finalizer"><code>node_api_post_finalizer</code></a>…
401 <li><a href="#simple-asynchronous-operations">Simple asynchronous operations</a>
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>
409 <li><a href="#custom-asynchronous-operations">Custom asynchronous operations</a>
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>
418 <li><a href="#version-management">Version management</a>
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>
424 <li><a href="#memory-management">Memory management</a>
426 <li><a href="#napi_adjust_external_memory"><code>napi_adjust_external_memory</code></a></li>
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>
437 <li><a href="#script-execution">Script execution</a>
439 <li><a href="#napi_run_script"><code>napi_run_script</code></a></li>
442 <li><a href="#libuv-event-loop">libuv event loop</a>
444 <li><a href="#napi_get_uv_event_loop"><code>napi_get_uv_event_loop</code></a></li>
447 <li><a href="#asynchronous-thread-safe-function-calls">Asynchronous thread-safe function calls</a>
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 functi…
451 <li><a href="#deciding-whether-to-keep-the-process-running">Deciding whether to keep the process ru…
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</cod…
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></…
456 <li><a href="#napi_release_threadsafe_function"><code>napi_release_threadsafe_function</code></a></…
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>
461 <li><a href="#miscellaneous-utilities">Miscellaneous utilities</a>
463 <li><a href="#node_api_get_module_file_name"><code>node_api_get_module_file_name</code></a></li>
472 <li class="picker-header">
474 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span>
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>
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…
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>
552 <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code reposito…
557 <li class="picker-header">
559 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span>
562 …<div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/n-api.…
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>
578 <li class="picker-header">
580 … <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span>
590 <a href="n-api.json">View as JSON</a>
592 …"edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/n-api.md">Edit on GitHu…
602 <li><span class="stability_2"><a href="#node-api">Node-API</a></span>
604 <li><a href="#implications-of-abi-stability">Implications of ABI stability</a></li>
607 <li><a href="#build-tools">Build tools</a>
609 <li><a href="#node-gyp">node-gyp</a></li>
613 <li><a href="#uploading-precompiled-binaries">Uploading precompiled binaries</a>
615 <li><a href="#node-pre-gyp">node-pre-gyp</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>
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>
630 <li><a href="#basic-node-api-data-types">Basic Node-API data types</a>
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…
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 …i><a href="#napi_threadsafe_function_release_mode"><code>napi_threadsafe_function_release_mode</co…
639 <li><a href="#napi_threadsafe_function_call_mode"><code>napi_threadsafe_function_call_mode</code></…
640 <li><a href="#node-api-memory-management-types">Node-API memory management types</a>
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>
649 <li><a href="#node-api-callback-types">Node-API callback types</a>
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>…
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></…
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>
664 <li><a href="#error-handling">Error handling</a>
666 <li><a href="#return-values">Return values</a>
668 <li><a href="#napi_get_last_error_info"><code>napi_get_last_error_info</code></a></li>
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>…
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>
688 <li><a href="#fatal-errors">Fatal errors</a>
690 <li><a href="#napi_fatal_error"><code>napi_fatal_error</code></a></li>
695 <li><a href="#object-lifetime-management">Object lifetime management</a>
697 <li><a href="#making-handle-lifespan-shorter-than-that-of-the-native-method">Making handle lifespan…
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></…
702 <li><a href="#napi_close_escapable_handle_scope"><code>napi_close_escapable_handle_scope</code></a>…
703 <li><a href="#napi_escape_handle"><code>napi_escape_handle</code></a></li>
706 <li><a href="#references-to-values-with-a-lifespan-longer-than-that-of-the-native-method">Reference…
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>
715 <li><a href="#cleanup-on-exit-of-the-current-nodejs-environment">Cleanup on exit of the current Nod…
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>
723 <li><a href="#finalization-on-the-exit-of-the-nodejs-environment">Finalization on the exit of the N…
726 <li><a href="#module-registration">Module registration</a></li>
727 <li><a href="#working-with-javascript-values">Working with JavaScript values</a>
729 <li><a href="#enum-types">Enum types</a>
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>
738 <li><a href="#object-creation-functions">Object creation functions</a>
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></…
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>
756 <li><a href="#functions-to-convert-from-c-types-to-node-api">Functions to convert from C types to N…
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 …><a href="#node_api_create_external_string_latin1"><code>node_api_create_external_string_latin1</c…
767 <li><a href="#napi_create_string_utf16"><code>napi_create_string_utf16</code></a></li>
768 …"><a href="#node_api_create_external_string_utf16"><code>node_api_create_external_string_utf16</co…
769 <li><a href="#napi_create_string_utf8"><code>napi_create_string_utf8</code></a></li>
772 <li><a href="#functions-to-convert-from-node-api-to-c-types">Functions to convert from Node-API to …
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>
795 <li><a href="#functions-to-get-global-instances">Functions to get global instances</a>
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>
805 <li><a href="#working-with-javascript-values-and-abstract-operations">Working with JavaScript value…
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>
825 <li><a href="#working-with-javascript-properties">Working with JavaScript properties</a>
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>
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>
856 <li><a href="#working-with-javascript-functions">Working with JavaScript functions</a>
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>
865 <li><a href="#object-wrap">Object wrap</a>
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>
875 …an class="stability_1"><a href="#node_api_post_finalizer"><code>node_api_post_finalizer</code></a>…
880 <li><a href="#simple-asynchronous-operations">Simple asynchronous operations</a>
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>
888 <li><a href="#custom-asynchronous-operations">Custom asynchronous operations</a>
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>
897 <li><a href="#version-management">Version management</a>
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>
903 <li><a href="#memory-management">Memory management</a>
905 <li><a href="#napi_adjust_external_memory"><code>napi_adjust_external_memory</code></a></li>
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>
916 <li><a href="#script-execution">Script execution</a>
918 <li><a href="#napi_run_script"><code>napi_run_script</code></a></li>
921 <li><a href="#libuv-event-loop">libuv event loop</a>
923 <li><a href="#napi_get_uv_event_loop"><code>napi_get_uv_event_loop</code></a></li>
926 <li><a href="#asynchronous-thread-safe-function-calls">Asynchronous thread-safe function calls</a>
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 functi…
930 <li><a href="#deciding-whether-to-keep-the-process-running">Deciding whether to keep the process ru…
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</cod…
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></…
935 <li><a href="#napi_release_threadsafe_function"><code>napi_release_threadsafe_function</code></a></…
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>
940 <li><a href="#miscellaneous-utilities">Miscellaneous utilities</a>
942 <li><a href="#node_api_get_module_file_name"><code>node_api_get_module_file_name</code></a></li>
950 …<h2>Node-API<span><a class="mark" href="#node-api" id="node-api">#</a></span><a aria-hidden="true"…
953 …tability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</…
954 <p>Node-API (formerly N-API) is an API for building native Addons. It is
960 …e <a href="https://nodejs.org/en/docs/guides/abi-stability/">ABI Stability</a> guide provides a mo…
963 the native code. Instead of using the V8 or <a href="https://github.com/nodejs/nan">Native Abstract…
964 APIs, the functions available in Node-API are used.</p>
965 <p>APIs exposed by Node-API are generally used to create and manipulate
967 in the ECMA-262 Language Specification. The APIs have the following
970 <li>All Node-API calls return a status code of type <code>napi_status</code>. This
974 <code>napi_value</code>.</li>
975 <li>In case of an error status code, additional information can be obtained
976 using <code>napi_get_last_error_info</code>. More information can be found in the error
977 handling section <a href="#error-handling">Error handling</a>.</li>
979 <p>Node-API is a C API that ensures ABI stability across Node.js versions
982 …per module called <a href="https://github.com/nodejs/node-addon-api"><code>node-addon-api</code></…
984 with <code>node-addon-api</code> will depend on the symbols for the Node-API C-based
985 functions exported by Node.js. <code>node-addon-api</code> is a more
986 efficient way to write code that calls Node-API. Take, for example, the
987 following <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
990 <pre><code class="language-cpp">Object obj = Object::<span class="hljs-built_in">New</span>(env);
991 …hljs-string">"foo"</span>] = String::<span class="hljs-built_in">New</span>(env, <span class="hljs…
992 <pre><code class="language-cpp">napi_status status;
994 status = <span class="hljs-built_in">napi_create_object</span>(env, &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>;
1000 status = <span class="hljs-built_in">napi_create_string_utf8</span>(env, <span class="hljs-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>;
1006 status = <span class="hljs-built_in">napi_set_named_property</span>(env, object, <span class="hljs-…
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>
1013 <p>When using <code>node-addon-api</code> instead of the C APIs, start with the API <a href="https:…
1014 for <code>node-addon-api</code>.</p>
1015 <p>The <a href="https://nodejs.github.io/node-addon-examples/">Node-API Resource</a> offers
1017 Node-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> …
1019 …pan><a class="mark" href="#implications-of-abi-stability" id="implications-of-abi-stability">#</a>…
1020 <p>Although Node-API provides an ABI stability guarantee, other parts of Node.js do
1027 <pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> …
1028 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><…
1029 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><…
1030 …"hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_object…
1034 …code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span…
1038 …code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span…
1041 <p>Thus, for an addon to remain ABI-compatible across Node.js major versions, it
1042 must use Node-API exclusively by restricting itself to using</p>
1043 …code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span c…
1045 library makes ABI stability guarantees similar to Node-API.</p>
1046 …ding<span><a class="mark" href="#building" id="building">#</a></span><a aria-hidden="true" class="…
1048 native addons using Node-API requires an additional set of tools. Besides the
1050 requires a toolchain that can compile C and C++ code into a binary. In
1060 <pre><code class="language-bash">xcode-select --install</code> <button class="copy-button">copy</bu…
1064 <pre><code class="language-bash">npm install --global windows-build-tools</code> <button class="cop…
1067 <h4>Build tools<span><a class="mark" href="#build-tools" id="build-tools">#</a></span><a aria-hidde…
1071 <h5>node-gyp<span><a class="mark" href="#node-gyp" id="node-gyp">#</a></span><a aria-hidden="true" …
1072 …ps://github.com/nodejs/node-gyp">node-gyp</a> is a build system based on the <a href="https://gith…
1073 …<a href="https://gyp.gsrc.io">GYP</a> tool and comes bundled with npm. GYP, and therefore node-gyp,
1075 <p>Historically, node-gyp has been the tool of choice for building native
1077 developers 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" cl…
1079 <p><a href="https://github.com/cmake-js/cmake-js">CMake.js</a> is an alternative build system based…
1081 …imitations in node-gyp. <a href="https://github.com/nodejs/node-addon-examples/tree/main/build_wit…
1082 example of a CMake-based native addon project.</p>
1083 …span><a class="mark" href="#uploading-precompiled-binaries" id="uploading-precompiled-binaries">#<…
1086 typically integrated with CI/CD build systems like <a href="https://travis-ci.org">Travis CI</a> and
1090 <h5>node-pre-gyp<span><a class="mark" href="#node-pre-gyp" id="node-pre-gyp">#</a></span><a aria-hi…
1091 <p><a href="https://github.com/mapbox/node-pre-gyp">node-pre-gyp</a> is a tool based on node-gyp th…
1092 upload binaries to a server of the developer's choice. node-pre-gyp has
1094 <h5>prebuild<span><a class="mark" href="#prebuild" id="prebuild">#</a></span><a aria-hidden="true" …
1095 …/github.com/prebuild/prebuild">prebuild</a> is a tool that supports builds using either node-gyp or
1096 CMake.js. Unlike node-pre-gyp which supports a variety of servers, prebuild
1097 …s only to <a href="https://help.github.com/en/github/administering-a-repository/about-releases">Gi…
1099 <h5>prebuildify<span><a class="mark" href="#prebuildify" id="prebuildify">#</a></span><a aria-hidde…
1100 <p><a href="https://github.com/prebuild/prebuildify">prebuildify</a> is a tool based on node-gyp. T…
1104 </section><section><h3>Usage<span><a class="mark" href="#usage" id="usage">#</a></span><a aria-hidd…
1105 … to use the Node-API functions, include the file <a href="https://github.com/nodejs/node/blob/HEAD…
1107 …code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span c…
1108 <p>This will opt into the default <code>NAPI_VERSION</code> for the given release of Node.js.
1109 In order to ensure compatibility with specific versions of Node-API, the version
1111 <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAP…
1112 …ass="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_ap…
1113 <p>This restricts the Node-API surface to just the functionality that was available
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> NAP…
1117 …ass="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_ap…
1119 available to the module code.</p>
1120 <p>Occasionally, experimental features are introduced that affect already-released
1121 and 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> NAP…
1123 <span class="hljs-meta">#<span class="hljs-keyword">define</span> NODE_API_EXPERIMENTAL_<span class…
1124 …ass="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_ap…
1125 <p>where <code><FEATURE_NAME></code> is the name of an experimental feature that affects both
1127 …ection><h3>Node-API version matrix<span><a class="mark" href="#node-api-version-matrix" id="node-a…
1128 <p>Node-API versions are additive and versioned independently from Node.js.
1135 <a href="https://nodejs.org/docs/latest/api/n-api.html#node-api-version-matrix">Node-API version ma…
1136 <!-- For accessibility purposes, this table needs row headers. That means we
1137 can't do it in markdown. Hence, the raw HTML. -->
1140 <th>Node-API version</th>
1181 <p>* Node-API was experimental.</p>
1182 <p>** Node.js 8.0.0 included Node-API as experimental. It was released as
1183 Node-API version 1 but continued to evolve until Node.js 8.6.0. The API is
1184 different in versions prior to Node.js 8.6.0. We recommend Node-API version 3 or
1186 <p>Each API documented for Node-API will have a header named <code>added in:</code>, and APIs
1187 which are stable will have the additional header <code>Node-API version:</code>.
1189 the Node-API version shown in <code>Node-API version:</code> or higher.
1191 <code>Node-API version:</code> listed or if there is no <code>Node-API version:</code> listed,
1193 <code>#define NAPI_EXPERIMENTAL</code> precedes the inclusion of <code>node_api.h</code>
1194 or <code>js_native_api.h</code>. If an API appears not to be available on
1195 a version of Node.js which is later than the one shown in <code>added in:</code> then
1197 <p>The Node-APIs associated strictly with accessing ECMAScript features from native
1198 code can be found separately in <code>js_native_api.h</code> and <code>js_native_api_types.h</code>.
1199 The 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
1201 implementations of Node-API outside of Node.js. For those implementations the
1203 <p>The Node.js-specific parts of an addon can be separated from the code that
1205 latter may be used with multiple implementations of Node-API. In the example
1206 below, <code>addon.c</code> and <code>addon.h</code> refer only to <code>js_native_api.h</code>. Th…
1207 that <code>addon.c</code> can be reused to compile against either the Node.js
1208 implementation 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
1210 to the addon and which instantiates the addon by calling into <code>addon.c</code> when the
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"><…
1216 napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_en…
1217 …class="hljs-meta">#<span class="hljs-keyword">endif</span> <span class="hljs-comment">// _ADDON_H…
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…
1221 <span class="hljs-meta">#<span class="hljs-keyword">define</span> NODE_API_CALL(env, call) …
1224 … <span class="hljs-keyword">if</span> (status != napi_ok) { \
1227 const char* err_message = error_info->error_message; \
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) { \
1233 ? <span class="hljs-string">"empty error message"</span> \
1241 <span class="hljs-type">static</span> napi_value
1242 <span class="hljs-title function_">DoSomethingUseful</span><span class="hljs-params">(napi_env env,…
1243 <span class="hljs-comment">// Do something useful.</span>
1244 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
1247 napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_en…
1253 <span class="hljs-string">"doSomethingUseful"</span>,
1256 <span class="hljs-literal">NULL</span>,
1261 <span class="hljs-string">"doSomethingUseful"</span>,
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"><…
1268 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"addon…
1270 NAPI_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 wi…
1273 …<span class="hljs-comment">// the body, as they are provided by the definition of `NAPI_MODULE_INI…
1274 <span class="hljs-keyword">return</span> create_addon(env);
1275 }</code> <button class="copy-button">copy</button></pre>
1276 …s<span><a class="mark" href="#environment-life-cycle-apis" id="environment-life-cycle-apis">#</a><…
1277 <p><a href="https://tc39.es/ecma262/#sec-agents">Section 8.7</a> of the <a href="https://tc39.githu…
1278 of an "Agent" as a self-contained environment in which JavaScript code runs.
1295 <p>To this end, Node-API provides a way to associate data such that its life cycle
1297 <h4><code>napi_set_instance_data</code><span><a class="mark" href="#napi_set_instance_data" id="nap…
1300 <span>N-API version: 6</span>
1302 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_instance_data…
1303 <span class="hljs-type">void</span>* data,
1305 …<span class="hljs-type">void</span>* finalize_hint)</span>;</code> <button class="copy-button">cop…
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
1310 down. 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
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…
1317 can later be retrieved using <code>napi_get_instance_data()</code>. Any existing data
1319 of 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="nap…
1324 <span>N-API version: 6</span>
1326 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_instance_data…
1327 …<span class="hljs-type">void</span>** data)</span>;</code> <button class="copy-button">copy</butto…
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
1331 running Node.js environment by a call to <code>napi_set_instance_data()</code>.</li>
1333 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
1335 running Node.js environment via <code>napi_set_instance_data()</code>. If no data is set,
1336 the call will succeed and <code>data</code> will be set to <code>NULL</code>.</p>
1337 …><h3>Basic Node-API data types<span><a class="mark" href="#basic-node-api-data-types" id="basic-no…
1338 <p>Node-API exposes the following fundamental data types as abstractions that are
1340 introspectable 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>…
1344 <span>N-API version: 1</span>
1346 <p>Integral status code indicating the success or failure of a Node-API call.
1348 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
1370 napi_would_deadlock, <span class="hljs-comment">/* unused */</span>
1373 } napi_status;</code> <button class="copy-button">copy</button></pre>
1375 it 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=…
1379 <span>N-API version: 1</span>
1381 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
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;
1386 } napi_extended_error_info;</code> <button class="copy-button">copy</button></pre>
1388 <li><code>error_message</code>: UTF8-encoded string containing a VM-neutral description of
1390 <li><code>engine_reserved</code>: Reserved for VM-specific error details. This is currently
1392 <li><code>engine_error_code</code>: VM-specific error code. This is currently
1394 <li><code>error_code</code>: The Node-API status code that originated with the last error.</li>
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-h…
1398 <p><code>napi_env</code> is used to represent a context that the underlying Node-API
1399 implementation can use to persist VM-specific state. This structure is passed
1401 making Node-API calls. Specifically, the same <code>napi_env</code> that was passed in when
1403 nested Node-API calls. Caching the <code>napi_env</code> for the purpose of general reuse,
1404 and passing the <code>napi_env</code> between instances of the same addon running on
1405 …t <a href="worker_threads.html#class-worker"><code>Worker</code></a> threads is not allowed. The <…
1407 … callbacks given to <a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a…
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…
1410 …tability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experime…
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 N…
1413 a parameter of type <code>node_api_nogc_env</code> as their first argument. These APIs do
1415 synchronous finalizers. Passing a parameter of type <code>napi_env</code> to these APIs is
1416 allowed, however, passing a parameter of type <code>node_api_nogc_env</code> to APIs that
1418 a cast will produce a compiler warning or an error when add-ons are compiled
1422 <h4><code>napi_value</code><span><a class="mark" href="#napi_value" id="napi_value">#</a></span><a …
1424 <h4><code>napi_threadsafe_function</code><span><a class="mark" href="#napi_threadsafe_function" id=…
1427 <span>N-API version: 4</span>
1431 <code>napi_call_threadsafe_function()</code>.</p>
1432 …code>napi_threadsafe_function_release_mode</code><span><a class="mark" href="#napi_threadsafe_func…
1435 <span>N-API version: 4</span>
1437 <p>A value to be given to <code>napi_release_threadsafe_function()</code> to indicate whether
1438 the thread-safe function is to be closed immediately (<code>napi_tsfn_abort</code>) or
1439 merely 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>.</…
1441 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
1444 } napi_threadsafe_function_release_mode;</code> <button class="copy-button">copy</button></pre>
1445 …code>napi_threadsafe_function_call_mode</code><span><a class="mark" href="#napi_threadsafe_functio…
1448 <span>N-API version: 4</span>
1450 <p>A value to be given to <code>napi_call_threadsafe_function()</code> to indicate whether
1451 the call should block whenever the queue associated with the thread-safe
1453 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
1456 } napi_threadsafe_function_call_mode;</code> <button class="copy-button">copy</button></pre>
1457 …de-API memory management types<span><a class="mark" href="#node-api-memory-management-types" id="n…
1458 <h5><code>napi_handle_scope</code><span><a class="mark" href="#napi_handle_scope" id="napi_handle_s…
1460 created within a particular scope. In general, Node-API values are created
1463 create a new handle scope, Node-API values will be created in the default handle
1464 scope. For any invocations of code outside the execution of a native method
1468 …scopes are created using <a href="#napi_open_handle_scope"><code>napi_open_handle_scope</code></a>…
1469 using <a href="#napi_close_handle_scope"><code>napi_close_handle_scope</code></a>. Closing the scop…
1470 that all <code>napi_value</code>s created during the lifetime of the handle scope are no
1472 <p>For more details, review the <a href="#object-lifetime-management">Object lifetime management</a…
1473 …<code>napi_escapable_handle_scope</code><span><a class="mark" href="#napi_escapable_handle_scope" …
1476 <span>N-API version: 1</span>
1480 <h5><code>napi_ref</code><span><a class="mark" href="#napi_ref" id="napi_ref">#</a></span><a aria-h…
1483 <span>N-API version: 1</span>
1485 <p>This is the abstraction to use to reference a <code>napi_value</code>. This allows for
1488 <p>For more details, review the <a href="#object-lifetime-management">Object lifetime management</a…
1489 <h5><code>napi_type_tag</code><span><a class="mark" href="#napi_type_tag" id="napi_type_tag">#</a><…
1492 <span>N-API version: 8</span>
1494 <p>A 128-bit value stored as two unsigned 64-bit integers. It serves as a UUID
1497 <a href="#napi_instanceof"><code>napi_instanceof</code></a>, because the latter can report a false …
1498 object's prototype has been manipulated. Type-tagging is most useful in
1499 conjunction with <a href="#napi_wrap"><code>napi_wrap</code></a> because it ensures that the pointe…
1502 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
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 …code>napi_async_cleanup_hook_handle</code><span><a class="mark" href="#napi_async_cleanup_hook_han…
1510 …alue returned by <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></…
1511 to <a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a> when t…
1513 <h4>Node-API callback types<span><a class="mark" href="#node-api-callback-types" id="node-api-callb…
1514 <h5><code>napi_callback_info</code><span><a class="mark" href="#napi_callback_info" id="napi_callba…
1517 <span>N-API version: 1</span>
1522 <h5><code>napi_callback</code><span><a class="mark" href="#napi_callback" id="napi_callback">#</a><…
1525 <span>N-API version: 1</span>
1527 <p>Function pointer type for user-provided native functions which are to be
1528 exposed to JavaScript via Node-API. Callback functions should satisfy the
1530 …code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function…
1531 <p>Unless for reasons discussed in <a href="#object-lifetime-management">Object Lifetime Management…
1532 handle 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="nod…
1537 …tability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experime…
1538 <p>Function pointer type for add-on provided functions that allow the user to be
1539 notified when externally-owned data is ready to be cleaned up because the
1540 object it was associated with has been garbage-collected. The user must provide
1542 object's collection. Currently, <code>node_api_nogc_finalize</code> can be used for
1544 …code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function…
1545 <span class="hljs-type">void</span>* finalize_data,
1546 …<span class="hljs-type">void</span>* finalize_hint)</span>;</code> <button class="copy-button">cop…
1547 <p>Unless for reasons discussed in <a href="#object-lifetime-management">Object Lifetime Management…
1550 where 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…
1555 … <a href="#node_api_create_external_string_latin1"><code>node_api_create_external_string_latin1</c…
1556 …node_api_create_external_string_utf16"><code>node_api_create_external_string_utf16</code></a> the …
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
1566 <code>NODE_API_EXPERIMENTAL_NOGC_ENV_OPT_OUT</code>.</p>
1569 <h5><code>napi_finalize</code><span><a class="mark" href="#napi_finalize" id="napi_finalize">#</a><…
1572 <span>N-API version: 1</span>
1574 <p>Function pointer type for add-on provided function that allow the user to
1575 schedule a group of calls to Node-APIs in response to a garbage collection
1577 pointers can be used with <a href="#node_api_post_finalizer"><code>node_api_post_finalizer</code></…
1578 …code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function…
1579 <span class="hljs-type">void</span>* finalize_data,
1580 …<span class="hljs-type">void</span>* finalize_hint)</span>;</code> <button class="copy-button">cop…
1584 <p>experimental (<code>NAPI_EXPERIMENTAL</code> is defined):</p>
1586 …_api_post_finalizer"><code>node_api_post_finalizer</code></a>. <a href="#node_api_nogc_finalize"><…
1588 <code>NODE_API_EXPERIMENTAL_NOGC_ENV_OPT_OUT</code>.</p>
1591 …<code>napi_async_execute_callback</code><span><a class="mark" href="#napi_async_execute_callback" …
1594 <span>N-API version: 1</span>
1598 …code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function…
1599 <p>Implementations of this function must avoid making Node-API calls that execute
1600 JavaScript 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
1603 …code>napi_async_complete_callback</code><span><a class="mark" href="#napi_async_complete_callback"…
1606 <span>N-API version: 1</span>
1610 …code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function…
1612 …<span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button…
1613 <p>Unless for reasons discussed in <a href="#object-lifetime-management">Object Lifetime Management…
1615 …code>napi_threadsafe_function_call_js</code><span><a class="mark" href="#napi_threadsafe_function_…
1618 <span>N-API version: 4</span>
1620 <p>Function pointer used with asynchronous thread-safe function calls. The callback
1623 necessary for a call into JavaScript, usually via <code>napi_call_function</code>, and then
1625 <p>The data arriving from the secondary thread via the queue is given in the <code>data</code>
1626 parameter and the JavaScript function to call is given in the <code>js_callback</code>
1628 <p>Node-API sets up the environment prior to calling this callback, so it is
1629 sufficient to call the JavaScript function via <code>napi_call_function</code> rather than
1630 via <code>napi_make_callback</code>.</p>
1632 …code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function…
1634 <span class="hljs-type">void</span>* context,
1635 …<span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button…
1637 <li><code>[in] env</code>: The environment to use for API calls, or <code>NULL</code> if the thread…
1638 function 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
1640 thread-safe function is being torn down and <code>data</code> may need to be freed. It
1641 may 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
1645 <li><code>[in] data</code>: Data created by the secondary thread. It is the responsibility of
1646 the callback to convert this native data to JavaScript values (with Node-API
1647 functions) that can be passed as parameters when <code>js_callback</code> is invoked.
1651 <p>Unless for reasons discussed in <a href="#object-lifetime-management">Object Lifetime Management…
1653 <h5><code>napi_cleanup_hook</code><span><a class="mark" href="#napi_cleanup_hook" id="napi_cleanup_…
1656 <span>N-API version: 3</span>
1658 …n pointer used with <a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a…
1661 …code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function…
1663 <li><code>[in] data</code>: The data that was passed to <a href="#napi_add_env_cleanup_hook"><code>…
1665 <h5><code>napi_async_cleanup_hook</code><span><a class="mark" href="#napi_async_cleanup_hook" id="n…
1669 …ointer used with <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></…
1672 …code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function…
1673 …<span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button…
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 com…
1678 <li><code>[in] data</code>: The data that was passed to <a href="#napi_add_async_cleanup_hook"><cod…
1681 end 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 …h3>Error handling<span><a class="mark" href="#error-handling" id="error-handling">#</a></span><a a…
1684 <p>Node-API uses both return values and JavaScript exceptions for error handling.
1686 <h4>Return values<span><a class="mark" href="#return-values" id="return-values">#</a></span><a aria…
1687 <p>All of the Node-API functions share the same error handling pattern. The
1688 return 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
1691 an exception was thrown, the <code>napi_status</code> value for the error
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…
1698 <p>The full set of possible <code>napi_status</code> values is defined
1699 in <code>napi_api_types.h</code>.</p>
1700 <p>The <code>napi_status</code> return value provides a VM-independent representation of
1703 VM (engine)-specific information.</p>
1704 …ieve this information <a href="#napi_get_last_error_info"><code>napi_get_last_error_info</code></a>
1705 is provided which returns a <code>napi_extended_error_info</code> structure.
1706 The format of the <code>napi_extended_error_info</code> structure is as follows:</p>
1709 <span>N-API version: 1</span>
1711 …<code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><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;
1716 };</code> <button class="copy-button">copy</button></pre>
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>
1723 <p><a href="#napi_get_last_error_info"><code>napi_get_last_error_info</code></a> returns the inform…
1724 Node-API call that was made.</p>
1728 <h5><code>napi_get_last_error_info</code><span><a class="mark" href="#napi_get_last_error_info" id=…
1731 <span>N-API version: 1</span>
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_a…
1735 …<span class="hljs-type">const</span> napi_extended_error_info** result)</span>;</code> <button cla…
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
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
1744 <p>The content of the <code>napi_extended_error_info</code> returned is only valid up until
1745 a 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
1748 in <code>error_message</code> points to a statically-defined string so it is safe to use
1749 that pointer if you have copied it out of the <code>error_message</code> field (which will
1750 be overwritten) before another Node-API function was called.</p>
1755 <h4>Exceptions<span><a class="mark" href="#exceptions" id="exceptions">#</a></span><a aria-hidden="…
1756 <p>Any Node-API function call may result in a pending JavaScript exception. This is
1759 <p>If the <code>napi_status</code> returned by a function is <code>napi_ok</code> then no
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
1763 …urning immediately, <a href="#napi_is_exception_pending"><code>napi_is_exception_pending</code></a>
1765 <p>In many cases when a Node-API function is called and an exception is
1767 <code>napi_status</code> of <code>napi_pending_exception</code>. However, this is not the case
1768 for all functions. Node-API allows a subset of the functions to be
1770 In that case, <code>napi_status</code> will reflect the status for the function. It
1777 code where the native method was invoked. The behavior of most Node-API calls
1779 <code>napi_pending_exception</code>, so do as little as possible and then return to
1782 where the native code can catch the exception, take the appropriate action,
1785 cases <a href="#napi_get_and_clear_last_exception"><code>napi_get_and_clear_last_exception</code></…
1787 the last JavaScript <code>Object</code> thrown. If it is determined, after
1789 it can be re-thrown it with <a href="#napi_throw"><code>napi_throw</code></a> where error is the
1791 <p>The following utility functions are also available in case native code
1792 needs to throw an exception or determine if a <code>napi_value</code> is an instance
1793 of a JavaScript <code>Error</code> object: <a href="#napi_throw_error"><code>napi_throw_error</code…
1794 …code>napi_throw_type_error</code></a>, <a href="#napi_throw_range_error"><code>napi_throw_range_er…
1796 code needs to create an <code>Error</code> object: <a href="#napi_create_error"><code>napi_create_e…
1797 …code>napi_create_type_error</code></a>, <a href="#napi_create_range_error"><code>napi_create_range…
1798 where result is the <code>napi_value</code> that refers to the newly created
1799 JavaScript <code>Error</code> object.</p>
1805 SemVer applying. In order to support this model with Node-API, both
1807 (as its good practice), the <code>throw_</code> and <code>create_</code> functions
1808 take an optional code parameter which is the string for the code
1809 to be added to the error object. If the optional parameter is <code>NULL</code>
1810 then no code will be associated with the error. If a code is provided,
1812 <pre><code class="language-text">originalName [code]</code> <button class="copy-button">copy</butto…
1813 <p>where <code>originalName</code> is the original name associated with the error
1814 and <code>code</code> is the code that was provided. For example, if the code
1815 is <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</b…
1817 <h5><code>napi_throw</code><span><a class="mark" href="#napi_throw" id="napi_throw">#</a></span><a …
1820 <span>N-API version: 1</span>
1822 …code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw</spa…
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>
1827 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
1829 <h5><code>napi_throw_error</code><span><a class="mark" href="#napi_throw_error" id="napi_throw_erro…
1832 <span>N-API version: 1</span>
1834 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw…
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> <but…
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>
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_…
1847 <span>N-API version: 1</span>
1849 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw…
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> <but…
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>
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="nap…
1862 <span>N-API version: 1</span>
1864 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw…
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> <but…
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>
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 …<code>node_api_throw_syntax_error</code><span><a class="mark" href="#node_api_throw_syntax_error" …
1877 <span>N-API version: 9</span>
1879 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">node_api_t…
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> <but…
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>
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><…
1892 <span>N-API version: 1</span>
1894 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_is_er…
1896 …<span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</butt…
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> represe…
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_e…
1908 <span>N-API version: 1</span>
1910 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_creat…
1911 napi_value code,
1913 … napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
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 …
1918 <li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code>…
1919 the message for the <code>Error</code>.</li>
1920 <li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li>
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="nap…
1927 <span>N-API version: 1</span>
1929 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_creat…
1930 napi_value code,
1932 … napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
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 …
1937 <li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code>…
1938 the message for the <code>Error</code>.</li>
1939 <li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li>
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="n…
1946 <span>N-API version: 1</span>
1948 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_creat…
1949 napi_value code,
1951 … napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
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 …
1956 <li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code>…
1957 the message for the <code>Error</code>.</li>
1958 <li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li>
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 …code>node_api_create_syntax_error</code><span><a class="mark" href="#node_api_create_syntax_error"…
1965 <span>N-API version: 9</span>
1967 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">node_api_c…
1968 napi_value code,
1970 … napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
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 …
1975 <li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code>…
1976 the message for the <code>Error</code>.</li>
1977 <li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li>
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 …code>napi_get_and_clear_last_exception</code><span><a class="mark" href="#napi_get_and_clear_last_…
1984 <span>N-API version: 1</span>
1986 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_and_clear_las…
1987 … napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
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>
1992 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
1994 …h5><code>napi_is_exception_pending</code><span><a class="mark" href="#napi_is_exception_pending" i…
1997 <span>N-API version: 1</span>
1999 …code class="language-c">napi_status <span class="hljs-title function_">napi_is_exception_pending</…
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>
2004 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
2006 <h5><code>napi_fatal_exception</code><span><a class="mark" href="#napi_fatal_exception" id="napi_fa…
2009 <span>N-API version: 3</span>
2011 …code class="language-c">napi_status <span class="hljs-title function_">napi_fatal_exception</span>…
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>
2016 <p>Trigger an <code>'uncaughtException'</code> in JavaScript. Useful if an async
2018 <h4>Fatal errors<span><a class="mark" href="#fatal-errors" id="fatal-errors">#</a></span><a aria-hi…
2021 <h5><code>napi_fatal_error</code><span><a class="mark" href="#napi_fatal_error" id="napi_fatal_erro…
2024 <span>N-API version: 1</span>
2026 …code class="language-c">NAPI_NO_RETURN <span class="hljs-type">void</span> <span class="hljs-title…
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…
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</c…
2036 if it is null-terminated.</li>
2040 …ment<span><a class="mark" href="#object-lifetime-management" id="object-lifetime-management">#</a>…
2041 <p>As Node-API calls are made, handles to objects in the heap for the underlying
2042 VM may be returned as <code>napi_values</code>. These handles must hold the
2043 objects 'live' until they are no longer required by the native code,
2044 otherwise the objects could be collected before the native code was
2053 The sections which follow describe the Node-API functions that can be used
2055 …king-handle-lifespan-shorter-than-that-of-the-native-method" id="making-handle-lifespan-shorter-th…
2059 …code class="language-c"><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> …
2062 <span class="hljs-keyword">if</span> (status != napi_ok) {
2063 <span class="hljs-keyword">break</span>;
2065 <span class="hljs-comment">// do something with element</span>
2066 }</code> <button class="copy-button">copy</button></pre>
2068 substantial resources. In addition, even though the native code could only
2071 <p>To handle this case, Node-API provides the ability to establish a new 'scope' to
2075 …open_handle_scope"><code>napi_open_handle_scope</code></a> and <a href="#napi_close_handle_scope">…
2076 <p>Node-API only supports a single nested hierarchy of scopes. There is only one
2081 …example, adding calls to <a href="#napi_open_handle_scope"><code>napi_open_handle_scope</code></a>…
2082 <a href="#napi_close_handle_scope"><code>napi_close_handle_scope</code></a> would ensure that at mo…
2084 …code class="language-c"><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> …
2087 <span class="hljs-keyword">if</span> (status != napi_ok) {
2088 <span class="hljs-keyword">break</span>;
2092 <span class="hljs-keyword">if</span> (status != napi_ok) {
2093 <span class="hljs-keyword">break</span>;
2095 <span class="hljs-comment">// do something with element</span>
2097 <span class="hljs-keyword">if</span> (status != napi_ok) {
2098 <span class="hljs-keyword">break</span>;
2100 }</code> <button class="copy-button">copy</button></pre>
2102 inner scope needs to live beyond the lifespan of that scope. Node-API supports
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 …omote a handle is made through <a href="#napi_escape_handle"><code>napi_escape_handle</code></a> w…
2112 <h5><code>napi_open_handle_scope</code><span><a class="mark" href="#napi_open_handle_scope" id="nap…
2115 <span>N-API version: 1</span>
2117 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_open_…
2118 … napi_handle_scope* result)</span>;</code> <button class="copy-button">copy</button></pre>
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>
2123 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
2125 <h5><code>napi_close_handle_scope</code><span><a class="mark" href="#napi_close_handle_scope" id="n…
2128 <span>N-API version: 1</span>
2130 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_close…
2131 … napi_handle_scope scope)</span>;</code> <button class="copy-button">copy</button></pre>
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>
2136 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
2140 …code>napi_open_escapable_handle_scope</code><span><a class="mark" href="#napi_open_escapable_handl…
2143 <span>N-API version: 1</span>
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…
2147 … napi_handle_scope* result)</span>;</code> <button class="copy-button">copy</button></pre>
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>
2152 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
2155 …code>napi_close_escapable_handle_scope</code><span><a class="mark" href="#napi_close_escapable_han…
2158 <span>N-API version: 1</span>
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-param…
2162 … napi_handle_scope scope)</span>;</code> <button class="copy-button">copy</button></pre>
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>
2167 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
2171 <h5><code>napi_escape_handle</code><span><a class="mark" href="#napi_escape_handle" id="napi_escape…
2174 <span>N-API version: 1</span>
2176 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_escape_handle</sp…
2179 … napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
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</co…
2185 <li><code>[out] result</code>: <code>napi_value</code> representing the handle to the escaped <code…
2188 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
2193 …-to-values-with-a-lifespan-longer-than-that-of-the-native-method" id="references-to-values-with-a-…
2199 would not be possible with a normal handle returned as a <code>napi_value</code> as
2203 <p>Node-API provides methods for creating persistent references to values.
2204 Currently Node-API only allows references to be created for a
2213 only supported by local symbols created with the <code>napi_create_symbol</code> function
2214 or the JavaScript <code>Symbol()</code> constructor calls. Globally registered symbols
2215 created with the <code>node_api_symbol_for</code> function or JavaScript <code>Symbol.for()</code>
2217 does 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>
2220 then 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 wh…
2223 …ed with the reference <a href="#napi_get_reference_value"><code>napi_get_reference_value</code></a>
2224 will 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 …
2241 <code>napi_delete_reference</code> in addition to <code>napi_reference_unref</code> when possible.<…
2245 <p>Experimental (<code>NAPI_EXPERIMENTAL</code> is defined):</p>
2252 <h5><code>napi_create_reference</code><span><a class="mark" href="#napi_create_reference" id="napi_…
2255 <span>N-API version: 1</span>
2257 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_creat…
2259 … <span class="hljs-type">uint32_t</span> initial_refcount,
2260 … napi_ref* result)</span>;</code> <button class="copy-button">copy</button></pre>
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.</l…
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>
2267 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
2270 <h5><code>napi_delete_reference</code><span><a class="mark" href="#napi_delete_reference" id="napi_…
2273 <span>N-API version: 1</span>
2275 …code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_delete_ref…
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>
2280 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
2283 <h5><code>napi_reference_ref</code><span><a class="mark" href="#napi_reference_ref" id="napi_refere…
2286 <span>N-API version: 1</span>
2288 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_refer…
2290 …<span class="hljs-type">uint32_t</span>* result)</span>;</code> <button class="copy-button">copy</…
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.…
2294 <li><code>[out] result</code>: The new reference count.</li>
2296 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
2299 <h5><code>napi_reference_unref</code><span><a class="mark" href="#napi_reference_unref" id="napi_re…
2302 <span>N-API version: 1</span>
2304 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_refer…
2306 …<span class="hljs-type">uint32_t</span>* result)</span>;</code> <button class="copy-button">copy</…
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.…
2310 <li><code>[out] result</code>: The new reference count.</li>
2312 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
2315 <h5><code>napi_get_reference_value</code><span><a class="mark" href="#napi_get_reference_value" id=…
2318 <span>N-API version: 1</span>
2320 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_r…
2322 … napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
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
2327 <li><code>[out] result</code>: The <code>napi_value</code> referenced by the <code>napi_ref</code>.…
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
2331 JavaScript value associated with the <code>napi_ref</code>. Otherwise, result
2332 will be <code>NULL</code>.</p>
2333 …k" href="#cleanup-on-exit-of-the-current-nodejs-environment" id="cleanup-on-exit-of-the-current-no…
2336 clean-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.
2340 …h5><code>napi_add_env_cleanup_hook</code><span><a class="mark" href="#napi_add_env_cleanup_hook" i…
2343 <span>N-API version: 3</span>
2345 <pre><code class="language-c">NODE_EXTERN napi_status <span class="hljs-title function_">napi_add_e…
2347 …<span class="hljs-type">void</span>* arg)</span>;</code> <button class="copy-button">copy</button>…
2348 <p>Registers <code>fun</code> as a function to be run with the <code>arg</code> parameter once the
2351 <code>arg</code> values. In that case, it will be called multiple times as well.
2352 Providing the same <code>fun</code> and <code>arg</code> values multiple times is not allowed
2356 …e done by using <a href="#napi_remove_env_cleanup_hook"><code>napi_remove_env_cleanup_hook</code><…
2359 …hronous cleanup, <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></…
2360 …code>napi_remove_env_cleanup_hook</code><span><a class="mark" href="#napi_remove_env_cleanup_hook"…
2363 <span>N-API version: 3</span>
2365 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remov…
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>…
2368 <p>Unregisters <code>fun</code> as a function to be run with the <code>arg</code> parameter once the
2372 with <code>napi_add_env_cleanup_hook</code>, otherwise the process will abort.</p>
2373 …<code>napi_add_async_cleanup_hook</code><span><a class="mark" href="#napi_add_async_cleanup_hook" …
2379 <td><p>Changed signature of the <code>hook</code> callback.</p></td></tr>
2384 <span>N-API version: 8</span>
2386 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_add_a…
2389 <span class="hljs-type">void</span>* arg,
2390 …napi_async_cleanup_hook_handle* remove_handle)</span>;</code> <button class="copy-button">copy</bu…
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
2398 <p>Registers <code>hook</code>, which is a function of type <a href="#napi_async_cleanup_hook"><cod…
2399 a function to be run with the <code>remove_handle</code> and <code>arg</code> parameters once the
2401 <p>Unlike <a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a>, the hook…
2402 …lly matches that of <a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a…
2403 <p>If <code>remove_handle</code> is not <code>NULL</code>, an opaque value will be stored in it
2404 …be passed to <a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code>…
2408 …code>napi_remove_async_cleanup_hook</code><span><a class="mark" href="#napi_remove_async_cleanup_h…
2414 <td><p>Removed <code>env</code> parameter.</p></td></tr>
2420 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remov…
2421 …napi_async_cleanup_hook_handle remove_handle)</span>;</code> <button class="copy-button">copy</but…
2423 <li><code>[in] remove_handle</code>: The handle to an asynchronous cleanup hook that was
2424 created with <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>.</…
2426 <p>Unregisters the cleanup hook corresponding to <code>remove_handle</code>. This will prevent
2428 This must be called on any <code>napi_async_cleanup_hook_handle</code> value obtained
2429 from <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>.</p>
2430 …ref="#finalization-on-the-exit-of-the-nodejs-environment" id="finalization-on-the-exit-of-the-node…
2433 <a href="worker_threads.html#workerterminate"><code>worker.terminate()</code></a>. When the environ…
2434 registered <code>napi_finalize</code> callbacks of JavaScript objects, thread-safe
2437 <p>The invocation of <code>napi_finalize</code> callbacks is scheduled after the manually
2439 finalization 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
2443 …egistration<span><a class="mark" href="#module-registration" id="module-registration">#</a></span>…
2444 <p>Node-API modules are registered in a manner similar to other modules
2445 except that instead of using the <code>NODE_MODULE</code> macro the following
2447 <pre><code class="language-c">NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)</code> <button class="copy-bu…
2448 <p>The next difference is the signature for the <code>Init</code> method. For a Node-API
2450 …code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class="hljs…
2451 <p>The return value from <code>Init</code> is treated as the <code>exports</code> object for the mo…
2452 The <code>Init</code> method is passed an empty object via the <code>exports</code> parameter as a
2453 convenience. If <code>Init</code> returns <code>NULL</code>, the parameter passed as <code>exports<…
2454 exported by the module. Node-API modules cannot modify the <code>module</code> object but
2455 can 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
2458 <pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class=…
2461 <span class="hljs-string">"hello"</span>,
2462 <span class="hljs-literal">NULL</span>,
2464 <span class="hljs-literal">NULL</span>,
2465 <span class="hljs-literal">NULL</span>,
2466 <span class="hljs-literal">NULL</span>,
2468 <span class="hljs-literal">NULL</span>
2470 status = napi_define_properties(env, exports, <span class="hljs-number">1</span>, &desc);
2471 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</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=…
2478 …pi_create_function(env, <span class="hljs-string">"exports"</span>, NAPI_AUTO_LENGTH, Method, <spa…
2479 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
2480 <span class="hljs-keyword">return</span> method;
2481 }</code> <button class="copy-button">copy</button></pre>
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> …
2485 napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, n…
2488 …-string">"value"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</…
2489 DECLARE_NAPI_METHOD(<span class="hljs-string">"plusOne"</span>, PlusOne),
2490 DECLARE_NAPI_METHOD(<span class="hljs-string">"multiply"</span>, Multiply),
2495 …ne_class(env, <span class="hljs-string">"MyObject"</span>, New, <span class="hljs-literal">NULL</s…
2496 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
2498 status = napi_create_reference(env, cons, <span class="hljs-number">1</span>, &constructor);
2499 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
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> …
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
2507 for <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_val…
2512 status = napi_create_int64(env, <span class="hljs-number">42</span>, &answer);
2513 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
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> …
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
2524 The documentation on <a href="addons.html#context-aware-addons">context-aware addons</a> provides m…
2525 <p>The variables <code>env</code> and <code>exports</code> will be available inside the function bo…
2528 <a href="#working-with-javascript-properties">Working with JavaScript properties</a>.</p>
2531 …<span><a class="mark" href="#working-with-javascript-values" id="working-with-javascript-values">#…
2532 <p>Node-API exposes a set of APIs to create all types of JavaScript values.
2533 …e documented under <a href="https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values">…
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>
2542 <p>Node-API values are represented by the type <code>napi_value</code>.
2543 Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>.
2544 In some cases, the API does check the type of the <code>napi_value</code> up-front.
2546 the <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="…
2548 <h5><code>napi_key_collection_mode</code><span><a class="mark" href="#napi_key_collection_mode" id=…
2551 <span>N-API version: 6</span>
2553 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
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
2560 object only. <code>napi_key_include_prototypes</code> will include all keys
2562 <h5><code>napi_key_filter</code><span><a class="mark" href="#napi_key_filter" id="napi_key_filter">…
2565 <span>N-API version: 6</span>
2567 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
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> << <span class="hljs-number">1<…
2571 …napi_key_configurable = <span class="hljs-number">1</span> << <span class="hljs-number">…
2572 …napi_key_skip_strings = <span class="hljs-number">1</span> << <span class="hljs-number">…
2573 …napi_key_skip_symbols = <span class="hljs-number">1</span> << <span class="hljs-number">…
2574 } napi_key_filter;</code> <button class="copy-button">copy</button></pre>
2576 <h5><code>napi_key_conversion</code><span><a class="mark" href="#napi_key_conversion" id="napi_key_…
2579 <span>N-API version: 6</span>
2581 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
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
2586 strings. <code>napi_key_keep_numbers</code> will return numbers for integer
2588 <h5><code>napi_valuetype</code><span><a class="mark" href="#napi_valuetype" id="napi_valuetype">#</…
2589 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
2590 <span class="hljs-comment">// ES6 types (corresponds to typeof)</span>
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
2603 described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types">Section 6.1</a…
2604 In 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
2608 <h5><code>napi_typedarray_type</code><span><a class="mark" href="#napi_typedarray_type" id="napi_ty…
2609 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
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>.
2624 <a href="https://tc39.github.io/ecma262/#sec-typedarray-objects">Section 22.2</a> of the <a href="h…
2625 …tions<span><a class="mark" href="#object-creation-functions" id="object-creation-functions">#</a><…
2626 <h5><code>napi_create_array</code><span><a class="mark" href="#napi_create_array" id="napi_create_a…
2629 <span>N-API version: 1</span>
2631 …code class="language-c">napi_status <span class="hljs-title function_">napi_create_array</span><sp…
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</cod…
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.
2639 <a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Lang…
2640 …code>napi_create_array_with_length</code><span><a class="mark" href="#napi_create_array_with_lengt…
2643 <span>N-API version: 1</span>
2645 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_array_with…
2646 <span class="hljs-type">size_t</span> length,
2647 … napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
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</cod…
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.
2655 The <code>Array</code>'s length property is set to the passed-in length parameter.
2656 However, the underlying buffer is not guaranteed to be pre-allocated by the VM
2660 <a href="#napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code></a>.</p>
2662 <a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Lang…
2663 <h5><code>napi_create_arraybuffer</code><span><a class="mark" href="#napi_create_arraybuffer" id="n…
2666 <span>N-API version: 1</span>
2668 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_arraybuffe…
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>
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>ArrayBuffe…
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
2682 normally used as a backing-buffer for <code>TypedArray</code> objects.
2683 The <code>ArrayBuffer</code> allocated will have an underlying byte buffer whose size is
2684 determined by the <code>length</code> parameter that's passed in.
2687 written to directly from native code. To write to this buffer from JavaScript,
2688 a 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 ECMAScrip…
2691 <h5><code>napi_create_buffer</code><span><a class="mark" href="#napi_create_buffer" id="napi_create…
2694 <span>N-API version: 1</span>
2696 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer</sp…
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>
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>.<…
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
2709 fully-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="n…
2713 <span>N-API version: 1</span>
2715 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer_cop…
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>
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
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>.<…
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
2731 from the passed-in buffer. While this is still a fully-supported data
2732 structure, 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_dat…
2736 <span>N-API version: 5</span>
2738 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_date</span…
2739 <span class="hljs-type">double</span> time,
2740 … napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
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…
2746 <p>Returns <code>napi_ok</code> if the API succeeded.</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 Langu…
2752 <h5><code>napi_create_external</code><span><a class="mark" href="#napi_create_external" id="napi_cr…
2755 <span>N-API version: 1</span>
2757 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external</…
2758 <span class="hljs-type">void</span>* data,
2760 <span class="hljs-type">void</span>* finalize_hint,
2761 … napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
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
2766 collected. <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
2769 <li><code>[out] result</code>: A <code>napi_value</code> representing an external value.</li>
2771 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
2773 is used to pass external data through JavaScript code, so it can be retrieved
2774 later by native code using <a href="#napi_get_value_external"><code>napi_get_value_external</code><…
2775 <p>The API adds a <code>napi_finalize</code> callback which will be called when the JavaScript
2778 properties. It is considered a distinct value type: calling <code>napi_typeof()</code> with
2779 an external value yields <code>napi_external</code>.</p>
2780 …code>napi_create_external_arraybuffer</code><span><a class="mark" href="#napi_create_external_arra…
2783 <span>N-API version: 1</span>
2785 <pre><code class="language-c">napi_status
2786 <span class="hljs-title function_">napi_create_external_arraybuffer</span><span class="hljs-params"…
2787 <span class="hljs-type">void</span>* external_data,
2788 <span class="hljs-type">size_t</span> byte_length,
2790 <span class="hljs-type">void</span>* finalize_hint,
2791 … napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
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 b…
2798 collected. <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
2801 <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>ArrayBuffe…
2803 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
2806 <code>napi_no_external_buffers_allowed</code> to indicate that external
2811 you may define <code>NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED</code> in your addon before
2812 includes for the node-api headers. Doing so will hide the 2 functions
2815 <p>This API returns a Node-API value corresponding to a JavaScript <code>ArrayBuffer</code>.
2816 The underlying byte buffer of the <code>ArrayBuffer</code> is externally allocated and
2819 <p>The API adds a <code>napi_finalize</code> callback which will be called when the JavaScript
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 ECMAScrip…
2823 …<code>napi_create_external_buffer</code><span><a class="mark" href="#napi_create_external_buffer" …
2826 <span>N-API version: 1</span>
2828 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external_b…
2829 <span class="hljs-type">size_t</span> length,
2830 <span class="hljs-type">void</span>* data,
2832 <span class="hljs-type">void</span>* finalize_hint,
2833 … napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
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
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 b…
2840 collected. <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
2843 <li><code>[out] result</code>: A <code>napi_value</code> representing a <code>node::Buffer</code>.<…
2845 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
2848 <code>napi_no_external_buffers_allowed</code> to indicate that external
2853 you may define <code>NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED</code> in your addon before
2854 includes for the node-api headers. Doing so will hide the 2 functions
2857 <p>This API allocates a <code>node::Buffer</code> object and initializes it with data
2858 backed by the passed in buffer. While this is still a fully-supported data
2859 structure, 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
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…
2866 <span>N-API version: 1</span>
2868 …code class="language-c">napi_status <span class="hljs-title function_">napi_create_object</span><s…
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</co…
2873 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
2874 <p>This API allocates a default JavaScript <code>Object</code>.
2875 It 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/…
2878 <h5><code>napi_create_symbol</code><span><a class="mark" href="#napi_create_symbol" id="napi_create…
2881 <span>N-API version: 1</span>
2883 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_symbol</sp…
2885 … napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
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</co…
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/…
2896 <h5><code>node_api_symbol_for</code><span><a class="mark" href="#node_api_symbol_for" id="node_api_…
2899 <span>N-API version: 9</span>
2901 <pre><code class="language-c">napi_status <span class="hljs-title function_">node_api_symbol_for</s…
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>
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
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</co…
2913 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
2917 <p>The JavaScript <code>symbol</code> type is described in <a href="https://tc39.github.io/ecma262/…
2919 <h5><code>napi_create_typedarray</code><span><a class="mark" href="#napi_create_typedarray" id="nap…
2922 <span>N-API version: 1</span>
2924 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_typedarray…
2926 <span class="hljs-type">size_t</span> length,
2928 <span class="hljs-type">size_t</span> byte_offset,
2929 … napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
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
2936 start projecting the <code>TypedArray</code>.</li>
2937 <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>TypedArray…
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
2944 <p>It's required that <code>(length * size_of_element) + byte_offset</code> should
2945 be <= the size in bytes of the array passed in. If not, a <code>RangeError</code> exception
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…
2949 <h5><code>napi_create_dataview</code><span><a class="mark" href="#napi_create_dataview" id="napi_cr…
2952 <span>N-API version: 1</span>
2954 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_dataview</…
2955 <span class="hljs-type">size_t</span> byte_length,
2957 <span class="hljs-type">size_t</span> byte_offset,
2958 … napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
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>.</…
2963 <li><code>[in] byte_offset</code>: The byte offset within the <code>ArrayBuffer</code> from which to
2964 start projecting the <code>DataView</code>.</li>
2965 <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>DataView</…
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</c…
2969 <code>DataView</code> objects provide an array-like view over an underlying data buffer,
2970 but 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
2972 size in bytes of the array passed in. If not, a <code>RangeError</code> exception is
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 L…
2976 …e-API<span><a class="mark" href="#functions-to-convert-from-c-types-to-node-api" id="functions-to-…
2977 <h5><code>napi_create_int32</code><span><a class="mark" href="#napi_create_int32" id="napi_create_i…
2980 <span>N-API version: 1</span>
2982 …code class="language-c">napi_status <span class="hljs-title function_">napi_create_int32</span><sp…
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</co…
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</…
2993 <h5><code>napi_create_uint32</code><span><a class="mark" href="#napi_create_uint32" id="napi_create…
2996 <span>N-API version: 1</span>
2998 …code class="language-c">napi_status <span class="hljs-title function_">napi_create_uint32</span><s…
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</co…
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</…
3009 <h5><code>napi_create_int64</code><span><a class="mark" href="#napi_create_int64" id="napi_create_i…
3012 <span>N-API version: 1</span>
3014 …code class="language-c">napi_status <span class="hljs-title function_">napi_create_int64</span><sp…
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</co…
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 …>The JavaScript <code>number</code> type is described in <a href="https://tc39.github.io/ecma262/#…
3024 of the ECMAScript Language Specification. Note the complete range of <code>int64_t</code>
3026 …//tc39.github.io/ecma262/#sec-number.min_safe_integer"><code>Number.MIN_SAFE_INTEGER</code></a> <c…
3027 …s://tc39.github.io/ecma262/#sec-number.max_safe_integer"><code>Number.MAX_SAFE_INTEGER</code></a> …
3028 <h5><code>napi_create_double</code><span><a class="mark" href="#napi_create_double" id="napi_create…
3031 <span>N-API version: 1</span>
3033 …code class="language-c">napi_status <span class="hljs-title function_">napi_create_double</span><s…
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</co…
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</…
3044 <h5><code>napi_create_bigint_int64</code><span><a class="mark" href="#napi_create_bigint_int64" id=…
3047 <span>N-API version: 6</span>
3049 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_int…
3050 <span class="hljs-type">int64_t</span> value,
3051 … napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
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</co…
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" i…
3062 <span>N-API version: 6</span>
3064 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_uin…
3065 <span class="hljs-type">uint64_t</span> value,
3066 … napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
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</co…
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.</…
3074 <h5><code>napi_create_bigint_words</code><span><a class="mark" href="#napi_create_bigint_words" id=…
3077 <span>N-API version: 6</span>
3079 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_wor…
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>
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
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</co…
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>
3095 <p>The resulting <code>BigInt</code> is calculated as: (–1)<sup><code>sign_bit</code></sup> (<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" i…
3100 <span>N-API version: 1</span>
3102 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_lat…
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>
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> i…
3110 is null-terminated.</li>
3111 <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</co…
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
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</…
3118 …code>node_api_create_external_string_latin1</code><span><a class="mark" href="#node_api_create_ext…
3122 …tability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experime…
3123 <pre><code class="language-c">napi_status
3124 <span class="hljs-title function_">node_api_create_external_string_latin1</span><span class="hljs-p…
3125 <span class="hljs-type">char</span>* str,
3126 <span class="hljs-type">size_t</span> length,
3128 <span class="hljs-type">void</span>* finalize_hint,
3130 …<span class="hljs-type">bool</span>* copied)</span>;</code> <button class="copy-button">copy</butt…
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> i…
3135 is null-terminated.</li>
3136 <li><code>[in] finalize_callback</code>: The function to call when the string is being
3139 <li><code>[in] env</code>: The environment in which the add-on is running. This value
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
3145 <a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.
3146 This parameter is optional. Passing a null value means that the add-on
3151 <li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
3153 <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</co…
3154 <li><code>[out] copied</code>: Whether the string was copied. If it was, the finalizer will
3155 already have been invoked to destroy <code>str</code>.</li>
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
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</…
3163 <h5><code>napi_create_string_utf16</code><span><a class="mark" href="#napi_create_string_utf16" id=…
3166 <span>N-API version: 1</span>
3168 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf…
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>
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</co…
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.
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</…
3184 …code>node_api_create_external_string_utf16</code><span><a class="mark" href="#node_api_create_exte…
3188 …tability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experime…
3189 <pre><code class="language-c">napi_status
3190 <span class="hljs-title function_">node_api_create_external_string_utf16</span><span class="hljs-pa…
3191 <span class="hljs-type">char16_t</span>* str,
3192 <span class="hljs-type">size_t</span> length,
3194 <span class="hljs-type">void</span>* finalize_hint,
3196 …<span class="hljs-type">bool</span>* copied)</span>;</code> <button class="copy-button">copy</butt…
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
3205 <li><code>[in] env</code>: The environment in which the add-on is running. This value
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
3211 <a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.
3212 This parameter is optional. Passing a null value means that the add-on
3217 <li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
3219 <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</co…
3220 <li><code>[out] copied</code>: Whether the string was copied. If it was, the finalizer will
3221 already have been invoked to destroy <code>str</code>.</li>
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.
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</…
3229 <h5><code>napi_create_string_utf8</code><span><a class="mark" href="#napi_create_string_utf8" id="n…
3232 <span>N-API version: 1</span>
3234 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf…
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>
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> i…
3242 is null-terminated.</li>
3243 <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</co…
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.
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</…
3250 …-API to C types<span><a class="mark" href="#functions-to-convert-from-node-api-to-c-types" id="fun…
3251 <h5><code>napi_get_array_length</code><span><a class="mark" href="#napi_get_array_length" id="napi_…
3254 <span>N-API version: 1</span>
3256 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_array_length<…
3258 …<span class="hljs-type">uint32_t</span>* result)</span></code> <button class="copy-button">copy</b…
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>…
3263 <li><code>[out] result</code>: <code>uint32</code> representing length of the array.</li>
3265 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
3267 <p><code>Array</code> length is described in <a href="https://tc39.github.io/ecma262/#sec-propertie…
3269 …h5><code>napi_get_arraybuffer_info</code><span><a class="mark" href="#napi_get_arraybuffer_info" i…
3272 <span>N-API version: 1</span>
3274 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_arraybuffer_i…
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…
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…
3281 <li><code>[out] data</code>: The underlying data buffer of the <code>ArrayBuffer</code>. If byte_le…
3282 is <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>
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
3289 buffer is managed by the <code>ArrayBuffer</code> even after it's returned. A
3291 <a href="#napi_create_reference"><code>napi_create_reference</code></a>, which can be used to guara…
3292 lifetime of the <code>ArrayBuffer</code>. It's also safe to use the returned data buffer
3295 <h5><code>napi_get_buffer_info</code><span><a class="mark" href="#napi_get_buffer_info" id="napi_ge…
3298 <span>N-API version: 1</span>
3300 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_buffer_info</…
3302 <span class="hljs-type">void</span>** data,
3303 …<span class="hljs-type">size_t</span>* length)</span></code> <button class="copy-button">copy</but…
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 …
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 po…
3310 <li><code>[out] length</code>: Length in bytes of the underlying data buffer.</li>
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 … href="#napi_get_typedarray_info"><code>napi_get_typedarray_info</code></a>. And <code>napi_get_ty…
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>
3320 <h5><code>napi_get_prototype</code><span><a class="mark" href="#napi_get_prototype" id="napi_get_pr…
3323 <span>N-API version: 1</span>
3325 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_prototype</sp…
3327 … napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
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> w…
3331 to return. This returns the equivalent of <code>Object.getPrototypeOf</code> (which is
3332 not 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.<…
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=…
3339 <span>N-API version: 1</span>
3341 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_typedarray_in…
3344 <span class="hljs-type">size_t</span>* length,
3345 <span class="hljs-type">void</span>** data,
3347 …<span class="hljs-type">size_t</span>* byte_offset)</span></code> <button class="copy-button">copy…
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> …
3352 <li><code>[out] type</code>: Scalar datatype of the elements within the <code>TypedArray</code>.</l…
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
3355 the <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…
3358 <li><code>[out] arraybuffer</code>: The <code>ArrayBuffer</code> underlying the <code>TypedArray</c…
3359 <li><code>[out] byte_offset</code>: The byte offset within the underlying native array
3363 <code>data - byte_offset</code>.</li>
3365 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
3367 <p>Any of the out parameters may be <code>NULL</code> if that property is unneeded.</p>
3370 <h5><code>napi_get_dataview_info</code><span><a class="mark" href="#napi_get_dataview_info" id="nap…
3373 <span>N-API version: 1</span>
3375 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_dataview_info…
3377 <span class="hljs-type">size_t</span>* byte_length,
3378 <span class="hljs-type">void</span>** data,
3380 …<span class="hljs-type">size_t</span>* byte_offset)</span></code> <button class="copy-button">copy…
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
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>.
3387 If 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>.<…
3389 <li><code>[out] byte_offset</code>: The byte offset within the data buffer from which
3390 to start projecting the <code>DataView</code>.</li>
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_…
3398 <span>N-API version: 5</span>
3400 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_date_value</s…
3402 …<span class="hljs-type">double</span>* result)</span></code> <button class="copy-button">copy</but…
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>.</…
3406 <li><code>[out] result</code>: Time value as a <code>double</code> represented as milliseconds since
3411 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-date <code>napi_value</code> is pass…
3412 in it returns <code>napi_date_expected</code>.</p>
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_…
3418 <span>N-API version: 1</span>
3420 …code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bool</span><…
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>.<…
3424 <li><code>[out] result</code>: C boolean primitive equivalent of the given JavaScript
3425 <code>Boolean</code>.</li>
3427 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-boolean <code>napi_value</code> is
3428 passed in it returns <code>napi_boolean_expected</code>.</p>
3430 <code>Boolean</code>.</p>
3431 <h5><code>napi_get_value_double</code><span><a class="mark" href="#napi_get_value_double" id="napi_…
3434 <span>N-API version: 1</span>
3436 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_double<…
3438 …<span class="hljs-type">double</span>* result)</span></code> <button class="copy-button">copy</but…
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>.</…
3442 <li><code>[out] result</code>: C double primitive equivalent of the given JavaScript
3443 <code>number</code>.</li>
3445 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code> is pa…
3446 in it returns <code>napi_number_expected</code>.</p>
3448 <code>number</code>.</p>
3449 …<code>napi_get_value_bigint_int64</code><span><a class="mark" href="#napi_get_value_bigint_int64" …
3452 <span>N-API version: 6</span>
3454 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_…
3456 <span class="hljs-type">int64_t</span>* result,
3457 …<span class="hljs-type">bool</span>* lossless)</span>;</code> <button class="copy-button">copy</bu…
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>.</…
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
3466 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it
3467 returns <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>f…
3470 …code>napi_get_value_bigint_uint64</code><span><a class="mark" href="#napi_get_value_bigint_uint64"…
3473 <span>N-API version: 6</span>
3475 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_…
3477 <span class="hljs-type">uint64_t</span>* result,
3478 …<span class="hljs-type">bool</span>* lossless)</span>;</code> <button class="copy-button">copy</bu…
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>.</…
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
3487 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it
3488 returns <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>f…
3491 …<code>napi_get_value_bigint_words</code><span><a class="mark" href="#napi_get_value_bigint_words" …
3494 <span>N-API version: 6</span>
3496 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_…
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</b…
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>.</…
3504 <li><code>[out] sign_bit</code>: Integer representing if the JavaScript <code>BigInt</code> is posi…
3506 <li><code>[in/out] word_count</code>: Must be initialized to the length of the <code>words</code>
3508 would 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>
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
3513 array, and the number of elements in the array. <code>sign_bit</code> and <code>words</code> may be
3514 both 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="n…
3518 <span>N-API version: 1</span>
3520 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_externa…
3522 …<span class="hljs-type">void</span>** result)</span></code> <button class="copy-button">copy</butt…
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>
3528 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-external <code>napi_value</code> is
3529 passed in it returns <code>napi_invalid_arg</code>.</p>
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_ge…
3535 <span>N-API version: 1</span>
3537 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int32</…
3539 …<span class="hljs-type">int32_t</span>* result)</span></code> <button class="copy-button">copy</bu…
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>.</…
3543 <li><code>[out] result</code>: C <code>int32</code> primitive equivalent of the given JavaScript
3544 <code>number</code>.</li>
3546 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
3547 is passed in <code>napi_number_expected</code>.</p>
3548 <p>This API returns the C <code>int32</code> primitive equivalent
3549 of the given JavaScript <code>number</code>.</p>
3552 positive 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>) s…
3555 <h5><code>napi_get_value_int64</code><span><a class="mark" href="#napi_get_value_int64" id="napi_ge…
3558 <span>N-API version: 1</span>
3560 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int64</…
3562 …<span class="hljs-type">int64_t</span>* result)</span></code> <button class="copy-button">copy</bu…
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>.</…
3566 <li><code>[out] result</code>: C <code>int64</code> primitive equivalent of the given JavaScript
3567 <code>number</code>.</li>
3569 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
3570 is 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 …code>number</code> values outside the range of <a href="https://tc39.github.io/ecma262/#sec-number…
3574 …code>-(2**53 - 1)</code> - <a href="https://tc39.github.io/ecma262/#sec-number.max_safe_integer"><…
3576 <p>Non-finite number values (<code>NaN</code>, <code>+Infinity</code>, or <code>-Infinity</code>) s…
3578 …code>napi_get_value_string_latin1</code><span><a class="mark" href="#napi_get_value_string_latin1"…
3581 <span>N-API version: 1</span>
3583 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_…
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</but…
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>…
3593 is returned in <code>result</code>.</li>
3594 <li><code>[in] bufsize</code>: Size of the destination buffer. When this value is
3595 insufficient, 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
3599 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value…
3600 is 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
3603 …5><code>napi_get_value_string_utf8</code><span><a class="mark" href="#napi_get_value_string_utf8" …
3606 <span>N-API version: 1</span>
3608 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_…
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</but…
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 pa…
3618 returned in <code>result</code>.</li>
3619 <li><code>[in] bufsize</code>: Size of the destination buffer. When this value is
3620 insufficient, 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
3624 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value…
3625 is 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 …<code>napi_get_value_string_utf16</code><span><a class="mark" href="#napi_get_value_string_utf16" …
3630 <span>N-API version: 1</span>
3632 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_…
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</but…
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
3641 passed in, the length of the string in 2-byte code units and excluding the
3643 <li><code>[in] bufsize</code>: Size of the destination buffer. When this value is
3644 insufficient, 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
3648 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value…
3649 is 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_…
3654 <span>N-API version: 1</span>
3656 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_uint32<…
3658 …<span class="hljs-type">uint32_t</span>* result)</span></code> <button class="copy-button">copy</b…
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>.</…
3662 <li><code>[out] result</code>: C primitive equivalent of the given <code>napi_value</code> as a
3663 <code>uint32_t</code>.</li>
3665 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
3666 is 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 …n><a class="mark" href="#functions-to-get-global-instances" id="functions-to-get-global-instances"…
3670 <h5><code>napi_get_boolean</code><span><a class="mark" href="#napi_get_boolean" id="napi_get_boolea…
3673 <span>N-API version: 1</span>
3675 …code class="language-c">napi_status <span class="hljs-title function_">napi_get_boolean</span><spa…
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>…
3682 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
3685 <h5><code>napi_get_global</code><span><a class="mark" href="#napi_get_global" id="napi_get_global">…
3688 <span>N-API version: 1</span>
3690 …code class="language-c">napi_status <span class="hljs-title function_">napi_get_global</span><span…
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> …
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><…
3700 <span>N-API version: 1</span>
3702 …code class="language-c">napi_status <span class="hljs-title function_">napi_get_null</span><span c…
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> ob…
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_un…
3712 <span>N-API version: 1</span>
3714 …code class="language-c">napi_status <span class="hljs-title function_">napi_get_undefined</span><s…
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>
3719 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
3721 …ef="#working-with-javascript-values-and-abstract-operations" id="working-with-javascript-values-an…
3722 <p>Node-API exposes a set of APIs to perform some abstract operations on JavaScript
3723 …tions are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Secti…
3727 <li>Coerce JavaScript values to specific JavaScript types (such as <code>number</code> or
3728 <code>string</code>).</li>
3732 <h4><code>napi_coerce_to_bool</code><span><a class="mark" href="#napi_coerce_to_bool" id="napi_coer…
3735 <span>N-API version: 1</span>
3737 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_bool</s…
3739 … napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
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>Bo…
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 Languag…
3748 <h4><code>napi_coerce_to_number</code><span><a class="mark" href="#napi_coerce_to_number" id="napi_…
3751 <span>N-API version: 1</span>
3753 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_number<…
3755 … napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
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>nu…
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…
3764 This function potentially runs JS code if the passed-in value is an
3766 <h4><code>napi_coerce_to_object</code><span><a class="mark" href="#napi_coerce_to_object" id="napi_…
3769 <span>N-API version: 1</span>
3771 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_object<…
3773 … napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
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>Ob…
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 Languag…
3782 <h4><code>napi_coerce_to_string</code><span><a class="mark" href="#napi_coerce_to_string" id="napi_…
3785 <span>N-API version: 1</span>
3787 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_string<…
3789 … napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
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>st…
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 Languag…
3798 This function potentially runs JS code if the passed-in value is an
3800 <h4><code>napi_typeof</code><span><a class="mark" href="#napi_typeof" id="napi_typeof">#</a></span>…
3803 <span>N-API version: 1</span>
3805 …code class="language-c">napi_status <span class="hljs-title function_">napi_typeof</span><span cla…
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>
3811 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
3813 <li><code>napi_invalid_arg</code> if the type of <code>value</code> is not a known ECMAScript type …
3814 <code>value</code> is not an External value.</li>
3816 <p>This API represents behavior similar to invoking the <code>typeof</code> Operator on
3817 the object as defined in <a href="https://tc39.github.io/ecma262/#sec-typeof-operator">Section 12.5…
3821 <li>It detects <code>null</code> as a separate type, while ECMAScript <code>typeof</code> would det…
3822 <code>object</code>.</li>
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">…
3828 <span>N-API version: 1</span>
3830 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_instanceof</span>…
3833 …<span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</butto…
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
3839 <li><code>[out] result</code>: Boolean that is set to true if <code>object instanceof constructor</…
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
3844 defined in <a href="https://tc39.github.io/ecma262/#sec-instanceofoperator">Section 12.10.4</a> of …
3845 <h4><code>napi_is_array</code><span><a class="mark" href="#napi_is_array" id="napi_is_array">#</a><…
3848 <span>N-API version: 1</span>
3850 …code class="language-c">napi_status <span class="hljs-title function_">napi_is_array</span><span c…
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>
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
3858 as defined in <a href="https://tc39.github.io/ecma262/#sec-isarray">Section 7.2.2</a> of the ECMASc…
3859 <h4><code>napi_is_arraybuffer</code><span><a class="mark" href="#napi_is_arraybuffer" id="napi_is_a…
3862 <span>N-API version: 1</span>
3864 …code class="language-c">napi_status <span class="hljs-title function_">napi_is_arraybuffer</span><…
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>
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">#</…
3875 <span>N-API version: 1</span>
3877 …code class="language-c">napi_status <span class="hljs-title function_">napi_is_buffer</span><span …
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::B…
3882 <code>Uint8Array</code> object.</li>
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…
3888 <h4><code>napi_is_date</code><span><a class="mark" href="#napi_is_date" id="napi_is_date">#</a></sp…
3891 <span>N-API version: 5</span>
3893 …code class="language-c">napi_status <span class="hljs-title function_">napi_is_date</span><span cl…
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 <c…
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">#<…
3905 <span>N-API version: 1</span>
3907 …code class="language-c">napi_status <span class="hljs-title function_">napi_is_error</span><span c…
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<…
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_typ…
3918 <span>N-API version: 1</span>
3920 …code class="language-c">napi_status <span class="hljs-title function_">napi_is_typedarray</span><s…
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>TypedAr…
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_datavie…
3931 <span>N-API version: 1</span>
3933 …code class="language-c">napi_status <span class="hljs-title function_">napi_is_dataview</span><spa…
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>DataVie…
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…
3944 <span>N-API version: 1</span>
3946 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_strict_equals</sp…
3949 …<span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</butto…
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>
3956 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
3958 defined in <a href="https://tc39.github.io/ecma262/#sec-strict-equality-comparison">Section 7.2.14<…
3959 <h4><code>napi_detach_arraybuffer</code><span><a class="mark" href="#napi_detach_arraybuffer" id="n…
3962 <span>N-API version: 7</span>
3964 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_detach_arraybuffe…
3965 … napi_value arraybuffer)</span></code> <button class="copy-button">copy</button></pre>
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>
3970 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-detachable <code>ArrayBuffer</code> …
3971 passed 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.
3973 The engine may impose additional conditions on whether an <code>ArrayBuffer</code> is
3974 detachable. For example, V8 requires that the <code>ArrayBuffer</code> be external,
3975 …ated with <a href="#napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code…
3976 <p>This API represents the invocation of the <code>ArrayBuffer</code> detach operation as
3977 defined in <a href="https://tc39.es/ecma262/#sec-detacharraybuffer">Section 24.1.1.3</a> of the ECM…
3978 …code>napi_is_detached_arraybuffer</code><span><a class="mark" href="#napi_is_detached_arraybuffer"…
3981 <span>N-API version: 7</span>
3983 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_detached_array…
3985 …<span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</butto…
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>
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>.</…
3993 <p>This API represents the invocation of the <code>ArrayBuffer</code> <code>IsDetachedBuffer</code>
3994 operation as defined in <a href="https://tc39.es/ecma262/#sec-isdetachedbuffer">Section 24.1.1.2</a…
3996 …n><a class="mark" href="#working-with-javascript-properties" id="working-with-javascript-propertie…
3997 <p>Node-API exposes a set of APIs to get and set properties on JavaScript
3998 …types are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Secti…
4001 Fundamentally, all property keys in Node-API can be represented in one of the
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
4007 be a <code>napi_value</code> representing a <code>string</code>, <code>number</code>, or <code>symb…
4009 <p>Node-API values are represented by the type <code>napi_value</code>.
4010 Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>.
4012 <code>napi_value</code> in question is of the JavaScript type expected by the API.</p>
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 = {};
4018 obj.<span class="hljs-property">myProp</span> = <span class="hljs-number">123</span>;</code> <butto…
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;
4022 <span class="hljs-comment">// const obj = {}</span>
4025 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
4027 <span class="hljs-comment">// Create a napi_value for 123</span>
4028 status = napi_create_int32(env, <span class="hljs-number">123</span>, &value);
4029 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
4031 <span class="hljs-comment">// obj.myProp = 123</span>
4032 status = 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> s…
4036 <pre><code class="language-js"><span class="hljs-keyword">const</span> arr = [];
4037 arr[<span class="hljs-number">123</span>] = <span class="hljs-string">'hello'</span>;</code> <butto…
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;
4041 <span class="hljs-comment">// const arr = [];</span>
4044 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
4046 <span class="hljs-comment">// Create a napi_value for 'hello'</span>
4047 status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &…
4048 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
4050 <span class="hljs-comment">// arr[123] = 'hello';</span>
4051 status = 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> s…
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> <…
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;
4060 <span class="hljs-comment">// const arr = []</span>
4063 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
4065 <span class="hljs-comment">// const value = arr[123]</span>
4066 status = napi_get_element(env, arr, <span class="hljs-number">123</span>, &value);
4067 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
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</…
4072 …-string">'foo'</span>: { <span class="hljs-attr">value</span>: <span class="hljs-number">123</span…
4073 …-string">'bar'</span>: { <span class="hljs-attr">value</span>: <span class="hljs-number">456</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;
4078 <span class="hljs-comment">// const obj = {};</span>
4081 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
4083 <span class="hljs-comment">// Create napi_values for 123 and 456</span>
4085 status = napi_create_int32(env, <span class="hljs-number">123</span>, &fooValue);
4086 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
4087 status = napi_create_int32(env, <span class="hljs-number">456</span>, &barValue);
4088 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
4090 <span class="hljs-comment">// Set the properties</span>
4092 …-string">"foo"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</sp…
4093 …-string">"bar"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</sp…
4097 …<span class="hljs-keyword">sizeof</span>(descriptors) / <span class="hljs-keyword">sizeof</span>(d…
4099 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
4100 <h4>Structures<span><a class="mark" href="#structures" id="structures">#</a></span><a aria-hidden="…
4101 <h5><code>napi_property_attributes</code><span><a class="mark" href="#napi_property_attributes" id=…
4107 <td><p>added <code>napi_default_method</code> and <code>napi_default_property</code>.</p></td></tr>
4111 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
4112 napi_default = <span class="hljs-number">0</span>,
4113 …napi_writable = <span class="hljs-number">1</span> << <span class="hljs-number">0</span>,
4114 …napi_enumerable = <span class="hljs-number">1</span> << <span class="hljs-number">1</spa…
4115 …napi_configurable = <span class="hljs-number">1</span> << <span class="hljs-number">2</s…
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> << <span class="hljs-number">10</span>,
4121 <span class="hljs-comment">// Default for class methods.</span>
4124 <span class="hljs-comment">// Default for object properties, like in JS obj[prop].</span>
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
4130 set on a JavaScript object. Other than <code>napi_static</code> they correspond to the
4131 attributes listed in <a href="https://tc39.github.io/ecma262/#table-2">Section 6.1.7.1</a>
4135 <li><code>napi_default</code>: No explicit attributes are set on the property. By default, a
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.…
4141 <li><code>napi_static</code>: The property will be defined as a static property on a class as
4143 <a href="#napi_define_class"><code>napi_define_class</code></a>. It is ignored by <code>napi_define…
4144 <li><code>napi_default_method</code>: Like a method in a JS class, the property is
4146 <li><code>napi_default_jsproperty</code>: Like a property set via assignment in JavaScript,
4149 <h5><code>napi_property_descriptor</code><span><a class="mark" href="#napi_property_descriptor" id=…
4150 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
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;
4161 <span class="hljs-type">void</span>* data;
4162 } napi_property_descriptor;</code> <button class="copy-button">copy</button></pre>
4164 <li><code>utf8name</code>: Optional string describing the key for the property,
4165 encoded as UTF8. One of <code>utf8name</code> or <code>name</code> must be provided for the
4167 <li><code>name</code>: Optional <code>napi_value</code> that points to a JavaScript string or symbol
4168 to be used as the key for the property. One of <code>utf8name</code> or <code>name</code> must
4170 <li><code>value</code>: The value that's retrieved by a get access of the property if the
4171 property 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).…
4173 <li><code>getter</code>: A function to call when a get access of the property is performed.
4174 If this is passed in, set <code>value</code> and <code>method</code> to <code>NULL</code> (since th…
4176 the property is accessed from JavaScript code (or if a get on the property is
4177 performed using a Node-API call). <a href="#napi_callback"><code>napi_callback</code></a> provides …
4178 <li><code>setter</code>: A function to call when a set access of the property is performed.
4179 If this is passed in, set <code>value</code> and <code>method</code> to <code>NULL</code> (since th…
4181 the property is set from JavaScript code (or if a set on the property is
4182 performed using a Node-API call). <a href="#napi_callback"><code>napi_callback</code></a> provides …
4183 <li><code>method</code>: Set this to make the property descriptor object's <code>value</code>
4184 property to be a JavaScript function represented by <code>method</code>. If this is
4185 passed in, set <code>value</code>, <code>getter</code> and <code>setter</code> to <code>NULL</code>…
4186 won'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 <…
4192 <h4>Functions<span><a class="mark" href="#functions" id="functions">#</a></span><a aria-hidden="tru…
4193 <h5><code>napi_get_property_names</code><span><a class="mark" href="#napi_get_property_names" id="n…
4196 <span>N-API version: 1</span>
4198 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_property_name…
4200 … napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
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
4206 iterate over <code>result</code> using <a href="#napi_get_array_length"><code>napi_get_array_length…
4207 and <a href="#napi_get_element"><code>napi_get_element</code></a>.</li>
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
4211 of strings. The properties of <code>object</code> whose key is a symbol will not be
4213 …<code>napi_get_all_property_names</code><span><a class="mark" href="#napi_get_all_property_names" …
4216 <span>N-API version: 6</span>
4218 <pre><code class="language-c">napi_get_all_property_names(napi_env env,
4223 … napi_value* result);</code> <button class="copy-button">copy</button></pre>
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
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
4232 …operty names of the object. <a href="#napi_get_array_length"><code>napi_get_array_length</code></a>
4233 and <a href="#napi_get_element"><code>napi_get_element</code></a> can be used to iterate over <code…
4235 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
4238 <h5><code>napi_set_property</code><span><a class="mark" href="#napi_set_property" id="napi_set_prop…
4241 <span>N-API version: 1</span>
4243 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_property</spa…
4246 … napi_value value)</span>;</code> <button class="copy-button">copy</button></pre>
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>
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_prop…
4258 <span>N-API version: 1</span>
4260 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_property</spa…
4263 … napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
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>
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_prop…
4275 <span>N-API version: 1</span>
4277 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_property</spa…
4280 …<span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</butt…
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>
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_de…
4292 <span>N-API version: 1</span>
4294 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_property</…
4297 …<span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</butt…
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> …
4303 optionally be ignored by passing <code>NULL</code>.</li>
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_…
4310 <span>N-API version: 1</span>
4312 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_own_property<…
4315 …<span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</butt…
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>
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…
4324 be a <code>string</code> or a <code>symbol</code>, or an error will be thrown. Node-API will not
4326 <h5><code>napi_set_named_property</code><span><a class="mark" href="#napi_set_named_property" id="n…
4329 <span>N-API version: 1</span>
4331 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_named_propert…
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>
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>
4341 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
4342 …ent to calling <a href="#napi_set_property"><code>napi_set_property</code></a> with a <code>napi_v…
4343 created 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="n…
4347 <span>N-API version: 1</span>
4349 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_named_propert…
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>
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>
4359 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
4360 …ent to calling <a href="#napi_get_property"><code>napi_get_property</code></a> with a <code>napi_v…
4361 created 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="n…
4365 <span>N-API version: 1</span>
4367 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_named_propert…
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</butt…
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>
4377 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
4378 …ent to calling <a href="#napi_has_property"><code>napi_has_property</code></a> with a <code>napi_v…
4379 created 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_elemen…
4383 <span>N-API version: 1</span>
4385 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_element</span…
4387 <span class="hljs-type">uint32_t</span> index,
4388 … napi_value value)</span>;</code> <button class="copy-button">copy</button></pre>
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>
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_elemen…
4400 <span>N-API version: 1</span>
4402 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_element</span…
4404 <span class="hljs-type">uint32_t</span> index,
4405 … napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
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>
4412 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
4414 <h5><code>napi_has_element</code><span><a class="mark" href="#napi_has_element" id="napi_has_elemen…
4417 <span>N-API version: 1</span>
4419 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_element</span…
4421 <span class="hljs-type">uint32_t</span> index,
4422 …<span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</butt…
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>
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
4432 <h5><code>napi_delete_element</code><span><a class="mark" href="#napi_delete_element" id="napi_dele…
4435 <span>N-API version: 1</span>
4437 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_element</s…
4439 <span class="hljs-type">uint32_t</span> index,
4440 …<span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</butt…
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> c…
4446 optionally be ignored by passing <code>NULL</code>.</li>
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="nap…
4453 <span>N-API version: 1</span>
4455 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_properties…
4457 <span class="hljs-type">size_t</span> property_count,
4458 …<span class="hljs-type">const</span> napi_property_descriptor* properties)</span>;</code> <button …
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.<…
4463 <li><code>[in] properties</code>: The array of property descriptors.</li>
4465 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
4468 <a href="#napi_property_descriptor"><code>napi_property_descriptor</code></a>). Given an array of s…
4470 …code>DefineOwnProperty()</code> (described in <a href="https://tc39.github.io/ecma262/#sec-ordinar…
4472 <h5><code>napi_object_freeze</code><span><a class="mark" href="#napi_object_freeze" id="napi_object…
4475 <span>N-API version: 8</span>
4477 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_freeze</sp…
4478 … napi_value object)</span>;</code> <button class="copy-button">copy</button></pre>
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>
4483 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
4489 in <a href="https://tc39.es/ecma262/#sec-object.freeze">Section 19.1.2.6</a> of the
4490 ECMA-262 specification.</p>
4491 <h5><code>napi_object_seal</code><span><a class="mark" href="#napi_object_seal" id="napi_object_sea…
4494 <span>N-API version: 8</span>
4496 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_seal</span…
4497 … napi_value object)</span>;</code> <button class="copy-button">copy</button></pre>
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>
4502 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
4504 added to it, as well as marking all existing properties as non-configurable.
4505 This is described in <a href="https://tc39.es/ecma262/#sec-object.seal">Section 19.1.2.20</a>
4506 of the ECMA-262 specification.</p>
4507 …an><a class="mark" href="#working-with-javascript-functions" id="working-with-javascript-functions…
4508 <p>Node-API provides a set of APIs that allow JavaScript code to
4509 call back into native code. Node-APIs that support calling back
4510 into native code take in a callback functions represented by
4511 the <code>napi_callback</code> type. When the JavaScript VM calls back to
4512 native code, the <code>napi_callback</code> function provided is invoked. The APIs
4518 <li>Return a <code>napi_value</code> back from the callback.</li>
4520 <p>Additionally, Node-API provides a set of functions which allow calling
4521 JavaScript functions from native code. One can either call a function
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
4526 whenever <code>object</code> is garbage-collected by passing both <code>object</code> and the data …
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_f…
4531 <span>N-API version: 1</span>
4533 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_call_…
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>
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 invo…
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…
4546 <li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned.…
4548 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
4550 add-on. This is the primary mechanism of calling back <em>from</em> the add-on's
4551 native code <em>into</em> JavaScript. For the special case of calling into JavaScript
4552 after an async operation, see <a href="#napi_make_callback"><code>napi_make_callback</code></a>.</p>
4555 <pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title f…
4556 <span class="hljs-keyword">return</span> num + <span class="hljs-number">2</span>;
4558 …-variable language_">global</span>.<span class="hljs-property">AddTwo</span> = <span class="hljs-t…
4559 <p>Then, the above function can be invoked from a native add-on using the
4560 following code:</p>
4561 <pre><code class="language-c"><span class="hljs-comment">// Get the function named "AddTwo" on the …
4564 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4566 status = napi_get_named_property(env, global, <span class="hljs-string">"AddTwo"</span>, &add_…
4567 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4569 <span class="hljs-comment">// const arg = 1337</span>
4570 status = napi_create_int32(env, <span class="hljs-number">1337</span>, &arg);
4571 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4574 <span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>;
4576 <span class="hljs-comment">// AddTwo(arg);</span>
4579 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4581 <span class="hljs-comment">// Convert the result back to a native type</span>
4582 <span class="hljs-type">int32_t</span> result;
4584 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;<…
4585 <h4><code>napi_create_function</code><span><a class="mark" href="#napi_create_function" id="napi_cr…
4588 <span>N-API version: 1</span>
4590 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_function</…
4591 … <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8name,
4592 <span class="hljs-type">size_t</span> length,
4594 <span class="hljs-type">void</span>* data,
4595 … napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
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
4599 visible 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_…
4601 it is null-terminated.</li>
4602 <li><code>[in] cb</code>: The native function which should be called when this function
4603 object is invoked. <a href="#napi_callback"><code>napi_callback</code></a> provides more details.</…
4604 <li><code>[in] data</code>: User-provided data context. This will be passed back into the
4606 <li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript function object …
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.
4611 This is the primary mechanism to allow calling <em>into</em> the add-on's native code
4617 add-on's module exports, set the newly created function on the exports
4619 <pre><code class="language-c">napi_value <span class="hljs-title function_">SayHello</span><span cl…
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>;
4624 napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, n…
4628 …e_function(env, <span class="hljs-literal">NULL</span>, <span class="hljs-number">0</span>, SayHel…
4629 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
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> …
4634 <span class="hljs-keyword">return</span> exports;
4637 NAPI_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-…
4640 myaddon.<span class="hljs-title function_">sayHello</span>();</code> <button class="copy-button">co…
4641 <p>The string passed to <code>require()</code> is the name of the target in <code>binding.gyp</code>
4642 responsible 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 c…
4645 <code>result</code> parameter) and freed whenever the function is garbage-collected by
4646 …cript function and the data to <a href="#napi_add_finalizer"><code>napi_add_finalizer</code></a>.<…
4647 <p>JavaScript <code>Function</code>s are described in <a href="https://tc39.github.io/ecma262/#sec-…
4649 <h4><code>napi_get_cb_info</code><span><a class="mark" href="#napi_get_cb_info" id="napi_get_cb_inf…
4652 <span>N-API version: 1</span>
4654 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_cb_info</span…
4656 <span class="hljs-type">size_t</span>* argc,
4659 …<span class="hljs-type">void</span>** data)</span></code> <button class="copy-button">copy</button…
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
4664 receives the actual count of arguments. <code>argc</code> can
4665 optionally 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
4669 provided than claimed, the rest of <code>argv</code> is filled with <code>napi_value</code> values
4670 that represent <code>undefined</code>. <code>argv</code> can optionally be ignored by
4671 passing <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
4675 optionally be ignored by passing <code>NULL</code>.</li>
4677 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
4679 call 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_…
4683 <span>N-API version: 1</span>
4685 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_new_target</s…
4687 … napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
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>
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
4695 callback 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_inst…
4699 <span>N-API version: 1</span>
4701 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_new_instance</spa…
4703 <span class="hljs-type">size_t</span> argc,
4705 … napi_value* result)</span></code> <button class="copy-button">copy</button></pre>
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 invo…
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
4712 arguments to the constructor. If <code>argc</code> is zero this parameter may be
4713 omitted by passing in <code>NULL</code>.</li>
4714 <li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned,
4718 <code>napi_value</code> that represents the constructor for the object. For example,
4720 <pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title f…
4721 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">param</span> = param;
4724 <span class="hljs-keyword">const</span> arg = <span class="hljs-string">'hello'</span>;
4725 …s-keyword">const</span> value = <span class="hljs-keyword">new</span> <span class="hljs-title clas…
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</…
4730 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4732 status = napi_get_named_property(env, global, <span class="hljs-string">"MyObject"</span>, &co…
4733 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4735 <span class="hljs-comment">// const arg = "hello"</span>
4736 status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &…
4737 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4740 <span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>;
4742 <span class="hljs-comment">// const value = new MyObject(arg)</span>
4743 status = napi_new_instance(env, constructor, argc, argv, &value);</code> <button class="copy-b…
4744 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
4745 …ion><h3>Object wrap<span><a class="mark" href="#object-wrap" id="object-wrap">#</a></span><a aria-…
4746 <p>Node-API offers a way to "wrap" C++ classes and instances so that the class
4749 <li>The <a href="#napi_define_class"><code>napi_define_class</code></a> API defines a JavaScript cl…
4752 <li>When JavaScript code invokes the constructor, the constructor callback
4753 uses <a href="#napi_wrap"><code>napi_wrap</code></a> to wrap a new C++ instance in a JavaScript obj…
4755 <li>When JavaScript code invokes a method or property accessor on the class,
4756 the corresponding <code>napi_callback</code> C++ function is invoked. For an instance
4757 callback, <a href="#napi_unwrap"><code>napi_unwrap</code></a> obtains the C++ instance that is the …
4763 reference 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</spa…
4767 <span class="hljs-type">bool</span> is_instance = <span class="hljs-literal">false</span>;
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>
4776 <p>There are occasions where <code>napi_instanceof()</code> is insufficient for ensuring that
4779 static methods rather than as the <code>this</code> value of prototype methods. In such
4781 <pre><code class="language-js"><span class="hljs-keyword">const</span> myAddon = <span class="hljs-…
4783 <span class="hljs-comment">// `openDatabase()` returns a JavaScript object that wraps a native data…
4784 <span class="hljs-comment">// handle.</span>
4785 <span class="hljs-keyword">const</span> dbHandle = myAddon.<span class="hljs-title function_">openD…
4787 <span class="hljs-comment">// `query()` returns a JavaScript object that wraps a native query handl…
4788 …span class="hljs-keyword">const</span> queryHandle = myAddon.<span class="hljs-title function_">qu…
4790 <span class="hljs-comment">// There is an accidental error in the line below. The first parameter t…
4791 <span class="hljs-comment">// `myAddon.queryHasRecords()` should be the database handle (`dbHandle`…
4792 <span class="hljs-comment">// the query handle (`query`), so the correct condition for the while-lo…
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…
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
4810 <code>queryHasRecords()</code> has to perform a type validation. Retaining the JavaScript
4812 constructor from which the query handle was instantiated in <code>napi_ref</code>s can
4813 help, because <code>napi_instanceof()</code> can then be used to ensure that the instances
4814 passed into <code>queryHashRecords()</code> are indeed of the correct type.</p>
4815 <p>Unfortunately, <code>napi_instanceof()</code> does not protect against prototype
4819 will pass the <code>napi_instanceof()</code> test for a query handle instance, while still
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
4824 …cript 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
4827 object arrives into a native binding, <code>napi_check_object_type_tag()</code> can be used
4829 previously "marked" with the type tag. This creates a type-checking capability
4830 of a higher fidelity than <code>napi_instanceof()</code> can provide, because such type-
4833 illustrates 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 databa…
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 str…
4840 <span class="hljs-type">static</span> <span class="hljs-type">const</span> napi_type_tag DatabaseHa…
4841 …<span class="hljs-number">0x1edf75a38336451d</span>, <span class="hljs-number">0xa5ed9ce2e4c00c38<…
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 QueryHandl…
4846 …<span class="hljs-number">0x9c73317f9fad44a3</span>, <span class="hljs-number">0x93c3920bf3b0ad6a<…
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…
4854 …<span class="hljs-comment">// Perform the underlying action which results in a database handle.</s…
4857 <span class="hljs-comment">// Create a new, empty JS object.</span>
4859 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
4861 …<span class="hljs-comment">// Tag the object to indicate that it holds a pointer to a `DatabaseHan…
4863 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
4865 …<span class="hljs-comment">// Store the pointer to the `DatabaseHandle` structure inside the JS ob…
4866 … result, dbHandle, <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> …
4869 <span class="hljs-keyword">return</span> result;
4872 <span class="hljs-comment">// Later when we receive a JavaScript object purporting to be a database…
4873 <span class="hljs-comment">// we can use `napi_check_object_type_tag()` to ensure that it is indeed…
4874 <span class="hljs-comment">// handle.</span>
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_callba…
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;
4883 …t_cb_info(env, info, &argc, argv, <span class="hljs-literal">NULL</span>, <span class="hljs-l…
4884 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
4886 …<span class="hljs-comment">// Check that the object passed as the first parameter has the previous…
4887 <span class="hljs-comment">// applied tag.</span>
4889 argv[<span class="hljs-number">0</span>],
4892 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
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>;
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_c…
4903 <span>N-API version: 1</span>
4905 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_class</spa…
4906 … <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8name,
4907 <span class="hljs-type">size_t</span> length,
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>
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,
4917 <li><code>[in] length</code>: The length of the <code>utf8name</code> in bytes, or <code>NAPI_AUTO_…
4918 if it is null-terminated.</li>
4919 <li><code>[in] constructor</code>: Callback function that handles constructing instances
4921 with the <a href="#napi_callback"><code>napi_callback</code></a> signature. A C++ class constructor…
4922 used. <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
4924 the <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…
4926 <li><code>[in] properties</code>: Array of property descriptors describing static and
4928 See <code>napi_property_descriptor</code>.</li>
4929 <li><code>[out] result</code>: A <code>napi_value</code> representing the constructor function for
4932 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
4936 corresponding C++ class, the callback passed via <code>constructor</code> can be used to
4938 JavaScript object instance being constructed using <a href="#napi_wrap"><code>napi_wrap</code></a>.…
4941 class (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
4943 C++ class, <em>non-static</em> data properties, accessors, and methods of the C++
4945 descriptors without the <code>napi_static</code> attribute after retrieving the C++ class
4947 <a href="#napi_unwrap"><code>napi_unwrap</code></a>.</li>
4949 <p>When wrapping a C++ class, the C++ constructor callback passed via <code>constructor</code>
4952 object. See <a href="#napi_wrap"><code>napi_wrap</code></a> for details.</p>
4953 …nstructor function returned from <a href="#napi_define_class"><code>napi_define_class</code></a> is
4955 code, and/or to check whether provided values are instances of the class. In
4956 that case, to prevent the function value from being garbage-collected, a
4958 <a href="#napi_create_reference"><code>napi_create_reference</code></a>, ensuring that the referenc…
4959 <p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> parameter o…
4960 the <code>data</code> field of the <code>napi_property_descriptor</code> array items can be associa…
4961 with the resulting JavaScript constructor (which is returned in the <code>result</code>
4962 parameter) and freed whenever the class is garbage-collected by passing both
4963 …cript function and the data to <a href="#napi_add_finalizer"><code>napi_add_finalizer</code></a>.<…
4964 <h4><code>napi_wrap</code><span><a class="mark" href="#napi_wrap" id="napi_wrap">#</a></span><a ari…
4967 <span>N-API version: 1</span>
4969 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_wrap</span><span …
4971 <span class="hljs-type">void</span>* native_object,
4973 <span class="hljs-type">void</span>* finalize_hint,
4974 … napi_ref* result)</span>;</code> <button class="copy-button">copy</button></pre>
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
4979 <li><code>[in] native_object</code>: The native instance that will be wrapped in the
4981 <li><code>[in] finalize_cb</code>: Optional native callback that can be used to free the
4982 native 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
4986 <li><code>[out] result</code>: Optional reference to the wrapped object.</li>
4988 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
4990 retrieved 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.
4994 <code>napi_wrap()</code> to wrap the newly constructed instance in the already-created
4995 JavaScript 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>,
4999 provided that simply deletes the native instance that is received as the <code>data</code>
5005 <a href="#napi_delete_reference"><code>napi_delete_reference</code></a> ONLY in response to the fin…
5011 hasn't been called yet. When using <code>napi_get_reference_value()</code> on weak
5012 references 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
5014 associate another native instance with the object, use <code>napi_remove_wrap()</code>
5016 <h4><code>napi_unwrap</code><span><a class="mark" href="#napi_unwrap" id="napi_unwrap">#</a></span>…
5019 <span>N-API version: 1</span>
5021 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_unwrap</span><spa…
5023 …<span class="hljs-type">void</span>** result)</span>;</code> <button class="copy-button">copy</but…
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>
5029 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
5031 object using <code>napi_wrap()</code>.</p>
5032 <p>When JavaScript code invokes a method or property accessor on the class, the
5033 corresponding <code>napi_callback</code> is invoked. If the callback is for an instance
5034 method or accessor, then the <code>this</code> argument to the callback is the wrapper
5036 then 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_wra…
5040 <span>N-API version: 1</span>
5042 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_remove_wrap</span…
5044 …<span class="hljs-type">void</span>** result)</span>;</code> <button class="copy-button">copy</but…
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>
5050 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
5052 object <code>js_object</code> using <code>napi_wrap()</code> and removes the wrapping. If a finalize
5054 JavaScript object becomes garbage-collected.</p>
5055 <h4><code>napi_type_tag_object</code><span><a class="mark" href="#napi_type_tag_object" id="napi_ty…
5058 <span>N-API version: 8</span>
5060 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_type_tag_object</…
5062 …<span class="hljs-type">const</span> napi_type_tag* type_tag)</span>;</code> <button class="copy-b…
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<…
5066 <li><code>[in] type_tag</code>: The tag with which the object is to be marked.</li>
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…
5074 <code>napi_invalid_arg</code>.</p>
5075 …4><code>napi_check_object_type_tag</code><span><a class="mark" href="#napi_check_object_type_tag" …
5078 <span>N-API version: 8</span>
5080 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_check_object_type…
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</butt…
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<…
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
5090 object. <code>false</code> is also returned if no type tag was found on the object.</li>
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 d…
5095 not match <code>type_tag</code>, then <code>result</code> is set to <code>false</code>. If a tag is…
5096 matches <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_fi…
5100 <span>N-API version: 5</span>
5102 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_add_finalizer</sp…
5104 <span class="hljs-type">void</span>* finalize_data,
5106 <span class="hljs-type">void</span>* finalize_hint,
5107 … napi_ref* result)</span>;</code> <button class="copy-button">copy</button></pre>
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
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
5114 native 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
5118 <li><code>[out] result</code>: Optional reference to the JavaScript object.</li>
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
5122 in <code>js_object</code> has been garbage-collected.</p>
5125 <a href="#napi_delete_reference"><code>napi_delete_reference</code></a> ONLY in response to the fin…
5129 <h5><code>node_api_post_finalizer</code><span><a class="mark" href="#node_api_post_finalizer" id="n…
5133 …tability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experime…
5134 <pre><code class="language-c">napi_status <span class="hljs-title function_">node_api_post_finalize…
5136 <span class="hljs-type">void</span>* finalize_data,
5137 …<span class="hljs-type">void</span>* finalize_hint)</span>;</code> <button class="copy-button">cop…
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
5141 native 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
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
5151 objects. At that point calling any Node-API that may cause changes in the GC
5153 <p><code>node_api_post_finalizer</code> helps to work around this limitation by allowing the
5154 add-on to defer calls to such Node-APIs to a point in time outside of the GC
5156 …<span><a class="mark" href="#simple-asynchronous-operations" id="simple-asynchronous-operations">#…
5161 <p>Node-API provides an ABI-stable interface for these
5163 <p>Node-API defines the <code>napi_async_work</code> structure which is used to manage
5165 …_create_async_work"><code>napi_create_async_work</code></a> and <a href="#napi_delete_async_work">…
5166 <p>The <code>execute</code> and <code>complete</code> callbacks are functions that will be
5169 <p>The <code>execute</code> function should avoid making any Node-API calls
5171 JavaScript objects. Most often, any code that needs to make Node-API
5172 calls should be made in <code>complete</code> callback instead.
5173 Avoid using the <code>napi_env</code> parameter in the execute callback as
5176 …code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function…
5177 <span class="hljs-type">void</span>* data)</span>;
5178 …-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">…
5180 …<span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button…
5181 <p>When these methods are invoked, the <code>data</code> parameter passed will be the
5182 addon-provided <code>void*</code> data that was passed into the
5183 <code>napi_create_async_work</code> call.</p>
5185 for execution using the <a href="#napi_queue_async_work"><code>napi_queue_async_work</code></a> fun…
5186 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work<…
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 wor…
5190 … calling <a href="#napi_cancel_async_work"><code>napi_cancel_async_work</code></a>, the <code>comp…
5191 will be invoked with a status value of <code>napi_cancelled</code>.
5192 The work should not be deleted before the <code>complete</code>
5194 <h4><code>napi_create_async_work</code><span><a class="mark" href="#napi_create_async_work" id="nap…
5200 <td><p>Added <code>async_resource</code> and <code>async_resource_name</code> parameters.</p></td><…
5205 <span>N-API version: 1</span>
5207 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_async_work…
5212 <span class="hljs-type">void</span>* data,
5213 … napi_async_work* result)</span>;</code> <button class="copy-button">copy</button></pre>
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
5217 …e passed to possible <code>async_hooks</code> <a href="async_hooks.html#initasyncid-type-triggeras…
5218 <li><code>[in] async_resource_name</code>: Identifier for the kind of resource that is being
5219 provided 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
5223 <li><code>[in] complete</code>: The native function which will be called when the
5225 …nt loop thread. <a href="#napi_async_complete_callback"><code>napi_async_complete_callback</code><…
5227 <li><code>[in] data</code>: User-provided data context. This will be passed back into the
5229 <li><code>[out] result</code>: <code>napi_async_work*</code> which is the handle to the newly creat…
5232 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
5234 It should be freed using <a href="#napi_delete_async_work"><code>napi_delete_async_work</code></a> …
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
5240 the <a href="async_hooks.html#type"><code>async_hooks</code> documentation</a> for more information…
5241 <h4><code>napi_delete_async_work</code><span><a class="mark" href="#napi_delete_async_work" id="nap…
5244 <span>N-API version: 1</span>
5246 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_async_work…
5247 … napi_async_work work)</span>;</code> <button class="copy-button">copy</button></pre>
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>.…
5252 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
5255 <h4><code>napi_queue_async_work</code><span><a class="mark" href="#napi_queue_async_work" id="napi_…
5258 <span>N-API version: 1</span>
5260 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work<…
5261 … napi_async_work work)</span>;</code> <button class="copy-button">copy</button></pre>
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>.…
5266 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
5269 with 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="nap…
5273 <span>N-API version: 1</span>
5275 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_cancel_async_work…
5276 … napi_async_work work)</span>;</code> <button class="copy-button">copy</button></pre>
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>.…
5281 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
5284 cancelled and <code>napi_generic_failure</code> will be returned. If successful,
5285 the <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>
5289 …<span><a class="mark" href="#custom-asynchronous-operations" id="custom-asynchronous-operations">#…
5294 <h4><code>napi_async_init</code><span><a class="mark" href="#napi_async_init" id="napi_async_init">…
5297 <span>N-API version: 1</span>
5299 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_init</span>…
5302 … napi_async_context* result)</span></code> <button class="copy-button">copy</button></pre>
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
5306 …e passed to possible <code>async_hooks</code> <a href="async_hooks.html#initasyncid-type-triggeras…
5307 …async_hooks.html#async_hooksexecutionasyncresource"><code>async_hooks.executionAsyncResource()</co…
5308 <li><code>[in] async_resource_name</code>: Identifier for the kind of resource that is being
5309 provided for diagnostic information exposed by the <code>async_hooks</code> API.</li>
5310 <li><code>[out] result</code>: The initialized async context.</li>
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> …
5315 order to retain ABI compatibility with previous versions, <code>napi_async_context</code>s
5316 are not maintaining the strong reference to the <code>async_resource</code> objects to
5317 avoid introducing causing memory leaks. However, if the <code>async_resource</code> is
5318 garbage collected by JavaScript engine before the <code>napi_async_context</code> was
5319 destroyed by <code>napi_async_destroy</code>, calling <code>napi_async_context</code> related APIs
5320 …i_open_callback_scope"><code>napi_open_callback_scope</code></a> and <a href="#napi_make_callback"…
5321 problems 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>
5323 for <code>async_resource</code> does not result in an error. However, this is not
5324 recommended as this will result in undesirable behavior with <code>async_hooks</code>
5325 …ooks.html#initasyncid-type-triggerasyncid-resource"><code>init</code> hooks</a> and <code>async_ho…
5326 now required by the underlying <code>async_hooks</code> implementation in order to provide
5328 <h4><code>napi_async_destroy</code><span><a class="mark" href="#napi_async_destroy" id="napi_async_…
5331 <span>N-API version: 1</span>
5333 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_destroy</sp…
5334 … napi_async_context async_context)</span>;</code> <button class="copy-button">copy</button></pre>
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>
5339 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
5341 <h4><code>napi_make_callback</code><span><a class="mark" href="#napi_make_callback" id="napi_make_c…
5347 <td><p>Added <code>async_context</code> parameter.</p></td></tr>
5352 <span>N-API version: 1</span>
5354 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_make_…
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>
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
5365 obtained from <a href="#napi_async_init"><code>napi_async_init</code></a>.
5366 In order to retain ABI compatibility with previous versions, passing <code>NULL</code>
5367 for <code>async_context</code> does not result in an error. However, this results
5369 async 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 invo…
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
5374 arguments to the function. If <code>argc</code> is zero this parameter may be
5375 omitted by passing in <code>NULL</code>.</li>
5376 <li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned.…
5378 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
5380 add-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
5383 wrapper 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
5386 context has already been set up, so a direct call to <code>napi_call_function</code>
5387 is sufficient and appropriate. Use of the <code>napi_make_callback</code> function
5389 <code>napi_create_async_work</code>.</p>
5390 <p>Any <code>process.nextTick</code>s or Promises scheduled on the microtask queue by
5392 <h4><code>napi_open_callback_scope</code><span><a class="mark" href="#napi_open_callback_scope" id=…
5395 <span>N-API version: 3</span>
5397 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_open_…
5400 … napi_callback_scope* result)</span></code> <button class="copy-button">copy</button></pre>
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
5404 …e passed to possible <code>async_hooks</code> <a href="async_hooks.html#initasyncid-type-triggeras…
5406 <code>async_resource</code> parameter in <a href="#napi_async_init"><code>napi_async_init</code></a…
5407 <li><code>[in] context</code>: Context for the async operation that is invoking the callback.
5408 …e a value previously obtained from <a href="#napi_async_init"><code>napi_async_init</code></a>.</l…
5409 <li><code>[out] result</code>: The newly created scope.</li>
5413 in place when making certain Node-API calls. If there is no other script on
5414 the 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 us…
5417 …h4><code>napi_close_callback_scope</code><span><a class="mark" href="#napi_close_callback_scope" i…
5420 <span>N-API version: 3</span>
5422 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_close…
5423 … napi_callback_scope scope)</span></code> <button class="copy-button">copy</button></pre>
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>
5429 …n management<span><a class="mark" href="#version-management" id="version-management">#</a></span><…
5430 <h4><code>napi_get_node_version</code><span><a class="mark" href="#napi_get_node_version" id="napi_…
5433 <span>N-API version: 1</span>
5435 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
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;
5442 napi_status <span class="hljs-title function_">napi_get_node_version</span><span class="hljs-params…
5443 …<span class="hljs-type">const</span> napi_node_version** version)</span>;</code> <button class="co…
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>
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
5450 version of Node.js that is currently running, and the <code>release</code> field with the
5451 value of <a href="process.html#processrelease"><code>process.release.name</code></a>.</p>
5453 <h4><code>napi_get_version</code><span><a class="mark" href="#napi_get_version" id="napi_get_versio…
5456 <span>N-API version: 1</span>
5458 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_version</span…
5459 …<span class="hljs-type">uint32_t</span>* result)</span>;</code> <button class="copy-button">copy</…
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>
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
5466 Node.js runtime. Node-API is planned to be additive such that
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>
5479 …ry management<span><a class="mark" href="#memory-management" id="memory-management">#</a></span><a…
5480 …<code>napi_adjust_external_memory</code><span><a class="mark" href="#napi_adjust_external_memory" …
5483 <span>N-API version: 1</span>
5485 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_adjus…
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</b…
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
5492 <li><code>[out] result</code>: The adjusted value</li>
5494 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
5500 …ises<span><a class="mark" href="#promises" id="promises">#</a></span><a aria-hidden="true" class="…
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 specific…
5503 objects. When a promise is created by <code>napi_create_promise()</code>, a "deferred"
5504 object is created and returned alongside the <code>Promise</code>. The deferred object is
5505 bound 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()</co…
5507 deferred 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</cod…
5511 <pre><code class="language-c">napi_deferred deferred;
5515 <span class="hljs-comment">// Create the promise.</span>
5517 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <…
5519 <span class="hljs-comment">// Pass the deferred to a function that performs an asynchronous action.…
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><…
5524 <p>The above function <code>do_something_asynchronous()</code> would perform its asynchronous
5527 <pre><code class="language-c">napi_deferred deferred;
5531 <span class="hljs-comment">// Create a value with which to conclude the deferred.</span>
5533 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <…
5535 <span class="hljs-comment">// Resolve or reject the promise associated with the deferred depending …
5536 <span class="hljs-comment">// whether the asynchronous action succeeded.</span>
5537 <span class="hljs-keyword">if</span> (asynchronous_action_succeeded) {
5539 } <span class="hljs-keyword">else</span> {
5542 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <…
5544 <span class="hljs-comment">// At this point the deferred has been freed, so we should assign NULL t…
5545 deferred = <span class="hljs-literal">NULL</span>;</code> <button class="copy-button">copy</button>…
5546 <h4><code>napi_create_promise</code><span><a class="mark" href="#napi_create_promise" id="napi_crea…
5549 <span>N-API version: 1</span>
5551 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_promise</s…
5553 … napi_value* promise)</span>;</code> <button class="copy-button">copy</button></pre>
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
5559 <li><code>[out] promise</code>: The JavaScript promise associated with the deferred object.</li>
5561 <p>Returns <code>napi_ok</code> if the API succeeded.</p>
5563 <h4><code>napi_resolve_deferred</code><span><a class="mark" href="#napi_resolve_deferred" id="napi_…
5566 <span>N-API version: 1</span>
5568 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_resolve_deferred<…
5570 … napi_value resolution)</span>;</code> <button class="copy-button">copy</button></pre>
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>
5580 <code>napi_create_promise()</code> and the deferred object returned from that call must
5583 <h4><code>napi_reject_deferred</code><span><a class="mark" href="#napi_reject_deferred" id="napi_re…
5586 <span>N-API version: 1</span>
5588 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_reject_deferred</…
5590 … napi_value rejection)</span>;</code> <button class="copy-button">copy</button></pre>
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>
5600 <code>napi_create_promise()</code> and the deferred object returned from that call must
5603 <h4><code>napi_is_promise</code><span><a class="mark" href="#napi_is_promise" id="napi_is_promise">…
5606 <span>N-API version: 1</span>
5608 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_promise</span>…
5610 …<span class="hljs-type">bool</span>* is_promise)</span>;</code> <button class="copy-button">copy</…
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
5617 …ript execution<span><a class="mark" href="#script-execution" id="script-execution">#</a></span><a …
5618 <p>Node-API provides an API for executing a string containing JavaScript using the
5620 <h4><code>napi_run_script</code><span><a class="mark" href="#napi_run_script" id="napi_run_script">…
5623 <span>N-API version: 1</span>
5625 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_run_s…
5627 … napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre>
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>
5633 <p>This function executes a string of JavaScript code and returns its result with
5636 <li>Unlike <code>eval</code>, this function does not allow the script to access the current
5638 <a href="modules.html#the-module-scope">module scope</a>, meaning that pseudo-globals such as <code…
5640 <li>The script can access the <a href="globals.html">global scope</a>. Function and <code>var</code…
5641 in the script will be added to the <a href="globals.html#global"><code>global</code></a> object. Va…
5642 made using <code>let</code> and <code>const</code> will be visible globally, but will not be added
5643 to 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 …
5646 …ibuv event loop<span><a class="mark" href="#libuv-event-loop" id="libuv-event-loop">#</a></span><a…
5647 <p>Node-API provides a function for getting the current event loop associated with
5648 a 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="nap…
5652 <span>N-API version: 2</span>
5654 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_u…
5655 …<span class="hljs-keyword">struct</span> uv_loop_s** loop)</span>;</code> <button class="copy-butt…
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>
5660 …-safe function calls<span><a class="mark" href="#asynchronous-thread-safe-function-calls" id="asyn…
5662 thread. If an addon creates additional threads, then Node-API functions that
5663 require a <code>napi_env</code>, <code>napi_value</code>, or <code>napi_ref</code> must not be call…
5668 JavaScript function on their behalf. The thread-safe function APIs provide an
5670 <p>These APIs provide the type <code>napi_threadsafe_function</code> as well as APIs to
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
5677 <p>Upon creation of a <code>napi_threadsafe_function</code> a <code>napi_finalize</code> callback c…
5678 provided. This callback will be invoked on the main thread when the thread-safe
5681 threads e.g. by calling <code>uv_thread_join()</code>. <strong>Aside from the main loop thread,
5682 no threads should be using the thread-safe function after the finalize callback
5684 <p>The <code>context</code> given during the call to <code>napi_create_threadsafe_function()</code>…
5686 <code>napi_get_threadsafe_function_context()</code>.</p>
5687 …ng a thread-safe function<span><a class="mark" href="#calling-a-thread-safe-function" id="calling-…
5688 <p><code>napi_call_threadsafe_function()</code> can be used for initiating a call into
5689 JavaScript. <code>napi_call_threadsafe_function()</code> accepts a parameter which controls
5690 whether the API behaves blockingly. If set to <code>napi_tsfn_nonblocking</code>, the API
5691 behaves non-blockingly, returning <code>napi_queue_full</code> if the queue was full,
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
5696 <p><code>napi_call_threadsafe_function()</code> should not be called with <code>napi_tsfn_blocking<…
5700 <code>call_js_cb</code> parameter. <code>call_js_cb</code> is invoked on the main thread once for e…
5702 <code>napi_call_threadsafe_function()</code>. If such a callback is not given, a default
5704 The <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 w…
5706 creating the <code>napi_threadsafe_function</code>, and the next data pointer that was
5708 as <code>napi_call_function()</code> to call into JavaScript.</p>
5709 …>The callback may also be invoked with <code>env</code> and <code>call_js_cb</code> both set to <c…
5712 Node.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
5714 Node-API runs <code>call_js_cb</code> in a context appropriate for callbacks.</p>
5719 …-safe functions<span><a class="mark" href="#reference-counting-of-thread-safe-functions" id="refer…
5720 <p>Threads can be added to and removed from a <code>napi_threadsafe_function</code> object
5722 threads upon creation, <code>napi_acquire_threadsafe_function</code> can be called to
5723 indicate that a new thread will start making use of the thread-safe function.
5724 Similarly, <code>napi_release_threadsafe_function</code> can be called to indicate that an
5725 existing 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
5727 the object has called <code>napi_release_threadsafe_function()</code> or has received a
5728 return 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>
5732 <code>napi_threadsafe_function</code>, because after the call completes, there is no
5733 guarantee that the <code>napi_threadsafe_function</code> is still allocated. For the same
5734 reason, do not use a thread-safe function
5735 after 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
5738 was 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
5740 number 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
5744 <code>napi_acquire_threadsafe_function()</code>. In fact, all subsequent API calls
5745 associated with it, except <code>napi_release_threadsafe_function()</code>, will return an
5746 error 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>
5748 to <code>napi_release_threadsafe_function()</code>. This will cause all subsequent APIs
5749 associated with the thread-safe function except
5750 <code>napi_release_threadsafe_function()</code> to return <code>napi_closing</code> even before its
5751 reference count reaches zero. In particular, <code>napi_call_threadsafe_function()</code>
5752 will return <code>napi_closing</code>, thus informing the threads that it is no longer
5753 possible to make asynchronous calls to the thread-safe function. This can be
5755 of <code>napi_closing</code> from <code>napi_call_threadsafe_function()</code> a thread must not use
5756 the thread-safe function anymore because it is no longer guaranteed to
5758 …"mark" href="#deciding-whether-to-keep-the-process-running" id="deciding-whether-to-keep-the-proce…
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
5761 the thread on which it is created to remain alive until the thread-safe function
5762 is destroyed. In contrast, an "unreferenced" thread-safe function will not
5763 prevent 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
5766 able to be destroyed nor does <code>napi_ref_threadsafe_function</code> prevent it from
5768 …code>napi_create_threadsafe_function</code><span><a class="mark" href="#napi_create_threadsafe_fun…
5774 <td><p>Made <code>func</code> parameter optional with custom <code>call_js_cb</code>.</p></td></tr>
5779 <span>N-API version: 4</span>
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">…
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,
5790 <span class="hljs-type">void</span>* context,
5792 … napi_threadsafe_function* result)</span>;</code> <button class="copy-button">copy</button></pre>
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
5796 must 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
5798 …e passed to possible <code>async_hooks</code> <a href="async_hooks.html#initasyncid-type-triggeras…
5799 <li><code>[in] async_resource_name</code>: A JavaScript string to provide an identifier for
5801 by 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
5806 <li><code>[in] thread_finalize_data</code>: Optional data to be passed to <code>thread_finalize_cb<…
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
5814 parameters 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> provi…
5816 <li><code>[out] result</code>: The asynchronous thread-safe JavaScript function.</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 …
5826 …code>napi_get_threadsafe_function_context</code><span><a class="mark" href="#napi_get_threadsafe_f…
5829 <span>N-API version: 4</span>
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-par…
5833 …<span class="hljs-type">void</span>** result)</span>;</code> <button class="copy-button">copy</but…
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>
5838 <p>This API may be called from any thread which makes use of <code>func</code>.</p>
5839 …code>napi_call_threadsafe_function</code><span><a class="mark" href="#napi_call_threadsafe_functio…
5845 <td><p>Support for <code>napi_would_deadlock</code> has been reverted.</p></td></tr>
5847 <td><p>Return <code>napi_would_deadlock</code> when called with <code>napi_tsfn_blocking</code> fro…
5852 <span>N-API version: 4</span>
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">(n…
5856 <span class="hljs-type">void</span>* data,
5857 …napi_threadsafe_function_call_mode is_blocking)</span>;</code> <button class="copy-button">copy</b…
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>
5861 provided 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
5864 <code>napi_tsfn_nonblocking</code> to indicate that the call should return immediately
5865 with a status of <code>napi_queue_full</code> whenever the queue is full.</li>
5867 <p>This API should not be called with <code>napi_tsfn_blocking</code> from a JavaScript
5870 <p>This API will return <code>napi_closing</code> if <code>napi_release_threadsafe_function()</code…
5871 called with <code>abort</code> set to <code>napi_tsfn_abort</code> from any thread. The value is on…
5872 added 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 …code>napi_acquire_threadsafe_function</code><span><a class="mark" href="#napi_acquire_threadsafe_f…
5877 <span>N-API version: 4</span>
5879 <pre><code class="language-c">NAPI_EXTERN napi_status
5880 …s-title function_">napi_acquire_threadsafe_function</span><span class="hljs-params">(napi_threadsa…
5882 <li><code>[in] func</code>: The asynchronous thread-safe JavaScript function to start making
5885 <p>A thread should call this API before passing <code>func</code> to any other thread-safe
5886 function 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
5889 <p>This API may be called from any thread which will start making use of <code>func</code>.</p>
5890 …code>napi_release_threadsafe_function</code><span><a class="mark" href="#napi_release_threadsafe_f…
5893 <span>N-API version: 4</span>
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"…
5897 …napi_threadsafe_function_release_mode mode)</span>;</code> <button class="copy-button">copy</butto…
5899 <li><code>[in] func</code>: The asynchronous thread-safe JavaScript function whose reference
5901 <li><code>[in] mode</code>: Flag whose value can be either <code>napi_tsfn_release</code> to indica…
5902 that the current thread will make no further calls to the thread-safe
5903 function, or <code>napi_tsfn_abort</code> to indicate that in addition to the current
5904 thread, no other thread should make any further calls to the thread-safe
5905 function. 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
5909 …hread should call this API when it stops making use of <code>func</code>. Passing <code>func</code>
5910 to 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 …code>napi_ref_threadsafe_function</code><span><a class="mark" href="#napi_ref_threadsafe_function"…
5916 <span>N-API version: 4</span>
5918 <pre><code class="language-c">NAPI_EXTERN napi_status
5919 …-title function_">napi_ref_threadsafe_function</span><span class="hljs-params">(node_api_nogc_env …
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>
5925 …exit until <code>func</code> has been destroyed. Similar to <a href="https://docs.libuv.org/en/v1.…
5927 <p>Neither does <code>napi_unref_threadsafe_function</code> mark the thread-safe functions as
5928 able to be destroyed nor does <code>napi_ref_threadsafe_function</code> prevent it from
5929 being destroyed. <code>napi_acquire_threadsafe_function</code> and
5930 <code>napi_release_threadsafe_function</code> are available for that purpose.</p>
5932 …code>napi_unref_threadsafe_function</code><span><a class="mark" href="#napi_unref_threadsafe_funct…
5935 <span>N-API version: 4</span>
5937 <pre><code class="language-c">NAPI_EXTERN napi_status
5938 …-title function_">napi_unref_threadsafe_function</span><span class="hljs-params">(node_api_nogc_en…
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>
5944 …exit before <code>func</code> is destroyed. Similar to <a href="https://docs.libuv.org/en/v1.x/han…
5947 …ties<span><a class="mark" href="#miscellaneous-utilities" id="miscellaneous-utilities">#</a></span…
5948 …code>node_api_get_module_file_name</code><span><a class="mark" href="#node_api_get_module_file_nam…
5951 <span>N-API version: 9</span>
5953 <pre><code class="language-c">NAPI_EXTERN napi_status
5954 …-title function_">node_api_get_module_file_name</span><span class="hljs-params">(node_api_nogc_env…
5955 </code> <button class="copy-button">copy</button></pre>
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
5959 location from which the add-on was loaded. For a file on the local
5960 file system it will start with <code>file://</code>. The string is null-terminated and
5961 owned by <code>env</code> and must thus not be modified or freed.</li>
5963 <p><code>result</code> may be an empty string if the add-on loading process fails to establish
5964 the add-on's file name during loading.</p></section>
5965 <!-- API END -->