• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v14.21.2">
7  <title>Node-API | Node.js v14.21.2 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/n-api.html">
12</head>
13<body class="alt apidoc" id="api-section-n-api">
14  <div id="content" class="clearfix">
15    <div id="column2" class="interior">
16      <div id="intro" class="interior">
17        <a href="/" title="Go back to the home page">
18          Node.js
19        </a>
20      </div>
21      <ul>
22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
24</ul>
25<hr class="line">
26<ul>
27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
29<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
30<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
31<li><a href="n-api.html" class="nav-n-api active">C/C++ addons with Node-API</a></li>
32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
35<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
36<li><a href="console.html" class="nav-console">Console</a></li>
37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
42<li><a href="dns.html" class="nav-dns">DNS</a></li>
43<li><a href="domain.html" class="nav-domain">Domain</a></li>
44<li><a href="errors.html" class="nav-errors">Errors</a></li>
45<li><a href="events.html" class="nav-events">Events</a></li>
46<li><a href="fs.html" class="nav-fs">File system</a></li>
47<li><a href="globals.html" class="nav-globals">Globals</a></li>
48<li><a href="http.html" class="nav-http">HTTP</a></li>
49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
50<li><a href="https.html" class="nav-https">HTTPS</a></li>
51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
52<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
57<li><a href="net.html" class="nav-net">Net</a></li>
58<li><a href="os.html" class="nav-os">OS</a></li>
59<li><a href="path.html" class="nav-path">Path</a></li>
60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
61<li><a href="policy.html" class="nav-policy">Policies</a></li>
62<li><a href="process.html" class="nav-process">Process</a></li>
63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
65<li><a href="readline.html" class="nav-readline">Readline</a></li>
66<li><a href="repl.html" class="nav-repl">REPL</a></li>
67<li><a href="report.html" class="nav-report">Report</a></li>
68<li><a href="stream.html" class="nav-stream">Stream</a></li>
69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
70<li><a href="timers.html" class="nav-timers">Timers</a></li>
71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
73<li><a href="tty.html" class="nav-tty">TTY</a></li>
74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
75<li><a href="url.html" class="nav-url">URL</a></li>
76<li><a href="util.html" class="nav-util">Utilities</a></li>
77<li><a href="v8.html" class="nav-v8">V8</a></li>
78<li><a href="vm.html" class="nav-vm">VM</a></li>
79<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
82</ul>
83<hr class="line">
84<ul>
85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
86</ul>
87    </div>
88
89    <div id="column1" data-id="n-api" class="interior">
90      <header>
91        <div class="header-container">
92          <h1>Node.js v14.21.2 documentation</h1>
93          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
94            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
95              <path fill="none" d="M0 0h24v24H0z" />
96              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
97              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
98            </svg>
99            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
100              <path d="M0 0h24v24H0z" fill="none" />
101              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
102            </svg>
103          </button>
104        </div>
105        <div id="gtoc">
106          <ul>
107            <li>
108              <a href="index.html">Index</a>
109            </li>
110            <li>
111              <a href="all.html">View on single page</a>
112            </li>
113            <li>
114              <a href="n-api.json">View as JSON</a>
115            </li>
116
117    <li class="version-picker">
118      <a href="#">View another version <span>&#x25bc;</span></a>
119      <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v19.x/api/n-api.html">19.x</a></li>
120<li><a href="https://nodejs.org/docs/latest-v18.x/api/n-api.html">18.x <b>LTS</b></a></li>
121<li><a href="https://nodejs.org/docs/latest-v17.x/api/n-api.html">17.x</a></li>
122<li><a href="https://nodejs.org/docs/latest-v16.x/api/n-api.html">16.x <b>LTS</b></a></li>
123<li><a href="https://nodejs.org/docs/latest-v15.x/api/n-api.html">15.x</a></li>
124<li><a href="https://nodejs.org/docs/latest-v14.x/api/n-api.html">14.x <b>LTS</b></a></li>
125<li><a href="https://nodejs.org/docs/latest-v13.x/api/n-api.html">13.x</a></li>
126<li><a href="https://nodejs.org/docs/latest-v12.x/api/n-api.html">12.x</a></li>
127<li><a href="https://nodejs.org/docs/latest-v11.x/api/n-api.html">11.x</a></li>
128<li><a href="https://nodejs.org/docs/latest-v10.x/api/n-api.html">10.x</a></li>
129<li><a href="https://nodejs.org/docs/latest-v9.x/api/n-api.html">9.x</a></li>
130<li><a href="https://nodejs.org/docs/latest-v8.x/api/n-api.html">8.x</a></li></ol>
131    </li>
132
133            <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/n-api.md">Edit on GitHub</a></li>
134          </ul>
135        </div>
136        <hr>
137      </header>
138
139      <details id="toc" open><summary>Table of contents</summary><ul>
140<li><span class="stability_2"><a href="#n_api_node_api">Node-API</a></span>
141<ul>
142<li><a href="#n_api_implications_of_abi_stability">Implications of ABI stability</a></li>
143<li><a href="#n_api_building">Building</a>
144<ul>
145<li><a href="#n_api_build_tools">Build tools</a>
146<ul>
147<li><a href="#n_api_node_gyp">node-gyp</a></li>
148<li><a href="#n_api_cmake_js">CMake.js</a></li>
149</ul>
150</li>
151<li><a href="#n_api_uploading_precompiled_binaries">Uploading precompiled binaries</a>
152<ul>
153<li><a href="#n_api_node_pre_gyp">node-pre-gyp</a></li>
154<li><a href="#n_api_prebuild">prebuild</a></li>
155<li><a href="#n_api_prebuildify">prebuildify</a></li>
156</ul>
157</li>
158</ul>
159</li>
160<li><a href="#n_api_usage">Usage</a></li>
161<li><a href="#n_api_node_api_version_matrix">Node-API version matrix</a></li>
162<li><a href="#n_api_environment_life_cycle_apis">Environment life cycle APIs</a>
163<ul>
164<li><a href="#n_api_napi_set_instance_data">napi_set_instance_data</a></li>
165<li><a href="#n_api_napi_get_instance_data">napi_get_instance_data</a></li>
166</ul>
167</li>
168<li><a href="#n_api_basic_node_api_data_types">Basic Node-API data types</a>
169<ul>
170<li><a href="#n_api_napi_status">napi_status</a></li>
171<li><a href="#n_api_napi_extended_error_info">napi_extended_error_info</a></li>
172<li><a href="#n_api_napi_env">napi_env</a></li>
173<li><a href="#n_api_napi_value">napi_value</a></li>
174<li><a href="#n_api_napi_threadsafe_function">napi_threadsafe_function</a></li>
175<li><a href="#n_api_napi_threadsafe_function_release_mode">napi_threadsafe_function_release_mode</a></li>
176<li><a href="#n_api_napi_threadsafe_function_call_mode">napi_threadsafe_function_call_mode</a></li>
177<li><a href="#n_api_node_api_memory_management_types">Node-API memory management types</a>
178<ul>
179<li><a href="#n_api_napi_handle_scope">napi_handle_scope</a></li>
180<li><a href="#n_api_napi_escapable_handle_scope">napi_escapable_handle_scope</a></li>
181<li><a href="#n_api_napi_ref">napi_ref</a></li>
182<li><a href="#n_api_napi_type_tag">napi_type_tag</a></li>
183<li><a href="#n_api_napi_async_cleanup_hook_handle">napi_async_cleanup_hook_handle</a></li>
184</ul>
185</li>
186<li><a href="#n_api_node_api_callback_types">Node-API callback types</a>
187<ul>
188<li><a href="#n_api_napi_callback_info">napi_callback_info</a></li>
189<li><a href="#n_api_napi_callback">napi_callback</a></li>
190<li><a href="#n_api_napi_finalize">napi_finalize</a></li>
191<li><a href="#n_api_napi_async_execute_callback">napi_async_execute_callback</a></li>
192<li><a href="#n_api_napi_async_complete_callback">napi_async_complete_callback</a></li>
193<li><a href="#n_api_napi_threadsafe_function_call_js">napi_threadsafe_function_call_js</a></li>
194<li><a href="#n_api_napi_async_cleanup_hook">napi_async_cleanup_hook</a></li>
195</ul>
196</li>
197</ul>
198</li>
199<li><a href="#n_api_error_handling">Error handling</a>
200<ul>
201<li><a href="#n_api_return_values">Return values</a>
202<ul>
203<li><a href="#n_api_napi_get_last_error_info">napi_get_last_error_info</a></li>
204</ul>
205</li>
206<li><a href="#n_api_exceptions">Exceptions</a>
207<ul>
208<li><a href="#n_api_napi_throw">napi_throw</a></li>
209<li><a href="#n_api_napi_throw_error">napi_throw_error</a></li>
210<li><a href="#n_api_napi_throw_type_error">napi_throw_type_error</a></li>
211<li><a href="#n_api_napi_throw_range_error">napi_throw_range_error</a></li>
212<li><a href="#n_api_napi_is_error">napi_is_error</a></li>
213<li><a href="#n_api_napi_create_error">napi_create_error</a></li>
214<li><a href="#n_api_napi_create_type_error">napi_create_type_error</a></li>
215<li><a href="#n_api_napi_create_range_error">napi_create_range_error</a></li>
216<li><a href="#n_api_napi_get_and_clear_last_exception">napi_get_and_clear_last_exception</a></li>
217<li><a href="#n_api_napi_is_exception_pending">napi_is_exception_pending</a></li>
218<li><a href="#n_api_napi_fatal_exception">napi_fatal_exception</a></li>
219</ul>
220</li>
221<li><a href="#n_api_fatal_errors">Fatal errors</a>
222<ul>
223<li><a href="#n_api_napi_fatal_error">napi_fatal_error</a></li>
224</ul>
225</li>
226</ul>
227</li>
228<li><a href="#n_api_object_lifetime_management">Object lifetime management</a>
229<ul>
230<li><a href="#n_api_making_handle_lifespan_shorter_than_that_of_the_native_method">Making handle lifespan shorter than that of the native method</a>
231<ul>
232<li><a href="#n_api_napi_open_handle_scope">napi_open_handle_scope</a></li>
233<li><a href="#n_api_napi_close_handle_scope">napi_close_handle_scope</a></li>
234<li><a href="#n_api_napi_open_escapable_handle_scope">napi_open_escapable_handle_scope</a></li>
235<li><a href="#n_api_napi_close_escapable_handle_scope">napi_close_escapable_handle_scope</a></li>
236<li><a href="#n_api_napi_escape_handle">napi_escape_handle</a></li>
237</ul>
238</li>
239<li><a href="#n_api_references_to_objects_with_a_lifespan_longer_than_that_of_the_native_method">References to objects with a lifespan longer than that of the native method</a>
240<ul>
241<li><a href="#n_api_napi_create_reference">napi_create_reference</a></li>
242<li><a href="#n_api_napi_delete_reference">napi_delete_reference</a></li>
243<li><a href="#n_api_napi_reference_ref">napi_reference_ref</a></li>
244<li><a href="#n_api_napi_reference_unref">napi_reference_unref</a></li>
245<li><a href="#n_api_napi_get_reference_value">napi_get_reference_value</a></li>
246</ul>
247</li>
248<li><a href="#n_api_cleanup_on_exit_of_the_current_node_js_instance">Cleanup on exit of the current Node.js instance</a>
249<ul>
250<li><a href="#n_api_napi_add_env_cleanup_hook">napi_add_env_cleanup_hook</a></li>
251<li><a href="#n_api_napi_remove_env_cleanup_hook">napi_remove_env_cleanup_hook</a></li>
252<li><a href="#n_api_napi_add_async_cleanup_hook">napi_add_async_cleanup_hook</a></li>
253<li><a href="#n_api_napi_remove_async_cleanup_hook">napi_remove_async_cleanup_hook</a></li>
254</ul>
255</li>
256</ul>
257</li>
258<li><a href="#n_api_module_registration">Module registration</a></li>
259<li><a href="#n_api_working_with_javascript_values">Working with JavaScript values</a>
260<ul>
261<li><a href="#n_api_enum_types">Enum types</a>
262<ul>
263<li><a href="#n_api_napi_key_collection_mode">napi_key_collection_mode</a></li>
264<li><a href="#n_api_napi_key_filter">napi_key_filter</a></li>
265<li><a href="#n_api_napi_key_conversion">napi_key_conversion</a></li>
266<li><a href="#n_api_napi_valuetype">napi_valuetype</a></li>
267<li><a href="#n_api_napi_typedarray_type">napi_typedarray_type</a></li>
268</ul>
269</li>
270<li><a href="#n_api_object_creation_functions">Object creation functions</a>
271<ul>
272<li><a href="#n_api_napi_create_array">napi_create_array</a></li>
273<li><a href="#n_api_napi_create_array_with_length">napi_create_array_with_length</a></li>
274<li><a href="#n_api_napi_create_arraybuffer">napi_create_arraybuffer</a></li>
275<li><a href="#n_api_napi_create_buffer">napi_create_buffer</a></li>
276<li><a href="#n_api_napi_create_buffer_copy">napi_create_buffer_copy</a></li>
277<li><a href="#n_api_napi_create_date">napi_create_date</a></li>
278<li><a href="#n_api_napi_create_external">napi_create_external</a></li>
279<li><a href="#n_api_napi_create_external_arraybuffer">napi_create_external_arraybuffer</a></li>
280<li><a href="#n_api_napi_create_external_buffer">napi_create_external_buffer</a></li>
281<li><a href="#n_api_napi_create_object">napi_create_object</a></li>
282<li><a href="#n_api_napi_create_symbol">napi_create_symbol</a></li>
283<li><a href="#n_api_napi_create_typedarray">napi_create_typedarray</a></li>
284<li><a href="#n_api_napi_create_dataview">napi_create_dataview</a></li>
285</ul>
286</li>
287<li><a href="#n_api_functions_to_convert_from_c_types_to_node_api">Functions to convert from C types to Node-API</a>
288<ul>
289<li><a href="#n_api_napi_create_int32">napi_create_int32</a></li>
290<li><a href="#n_api_napi_create_uint32">napi_create_uint32</a></li>
291<li><a href="#n_api_napi_create_int64">napi_create_int64</a></li>
292<li><a href="#n_api_napi_create_double">napi_create_double</a></li>
293<li><a href="#n_api_napi_create_bigint_int64">napi_create_bigint_int64</a></li>
294<li><a href="#n_api_napi_create_bigint_uint64">napi_create_bigint_uint64</a></li>
295<li><a href="#n_api_napi_create_bigint_words">napi_create_bigint_words</a></li>
296<li><a href="#n_api_napi_create_string_latin1">napi_create_string_latin1</a></li>
297<li><a href="#n_api_napi_create_string_utf16">napi_create_string_utf16</a></li>
298<li><a href="#n_api_napi_create_string_utf8">napi_create_string_utf8</a></li>
299</ul>
300</li>
301<li><a href="#n_api_functions_to_convert_from_node_api_to_c_types">Functions to convert from Node-API to C types</a>
302<ul>
303<li><a href="#n_api_napi_get_array_length">napi_get_array_length</a></li>
304<li><a href="#n_api_napi_get_arraybuffer_info">napi_get_arraybuffer_info</a></li>
305<li><a href="#n_api_napi_get_buffer_info">napi_get_buffer_info</a></li>
306<li><a href="#n_api_napi_get_prototype">napi_get_prototype</a></li>
307<li><a href="#n_api_napi_get_typedarray_info">napi_get_typedarray_info</a></li>
308<li><a href="#n_api_napi_get_dataview_info">napi_get_dataview_info</a></li>
309<li><a href="#n_api_napi_get_date_value">napi_get_date_value</a></li>
310<li><a href="#n_api_napi_get_value_bool">napi_get_value_bool</a></li>
311<li><a href="#n_api_napi_get_value_double">napi_get_value_double</a></li>
312<li><a href="#n_api_napi_get_value_bigint_int64">napi_get_value_bigint_int64</a></li>
313<li><a href="#n_api_napi_get_value_bigint_uint64">napi_get_value_bigint_uint64</a></li>
314<li><a href="#n_api_napi_get_value_bigint_words">napi_get_value_bigint_words</a></li>
315<li><a href="#n_api_napi_get_value_external">napi_get_value_external</a></li>
316<li><a href="#n_api_napi_get_value_int32">napi_get_value_int32</a></li>
317<li><a href="#n_api_napi_get_value_int64">napi_get_value_int64</a></li>
318<li><a href="#n_api_napi_get_value_string_latin1">napi_get_value_string_latin1</a></li>
319<li><a href="#n_api_napi_get_value_string_utf8">napi_get_value_string_utf8</a></li>
320<li><a href="#n_api_napi_get_value_string_utf16">napi_get_value_string_utf16</a></li>
321<li><a href="#n_api_napi_get_value_uint32">napi_get_value_uint32</a></li>
322</ul>
323</li>
324<li><a href="#n_api_functions_to_get_global_instances">Functions to get global instances</a>
325<ul>
326<li><a href="#n_api_napi_get_boolean">napi_get_boolean</a></li>
327<li><a href="#n_api_napi_get_global">napi_get_global</a></li>
328<li><a href="#n_api_napi_get_null">napi_get_null</a></li>
329<li><a href="#n_api_napi_get_undefined">napi_get_undefined</a></li>
330</ul>
331</li>
332</ul>
333</li>
334<li><a href="#n_api_working_with_javascript_values_and_abstract_operations">Working with JavaScript values and abstract operations</a>
335<ul>
336<li><a href="#n_api_napi_coerce_to_bool">napi_coerce_to_bool</a></li>
337<li><a href="#n_api_napi_coerce_to_number">napi_coerce_to_number</a></li>
338<li><a href="#n_api_napi_coerce_to_object">napi_coerce_to_object</a></li>
339<li><a href="#n_api_napi_coerce_to_string">napi_coerce_to_string</a></li>
340<li><a href="#n_api_napi_typeof">napi_typeof</a></li>
341<li><a href="#n_api_napi_instanceof">napi_instanceof</a></li>
342<li><a href="#n_api_napi_is_array">napi_is_array</a></li>
343<li><a href="#n_api_napi_is_arraybuffer">napi_is_arraybuffer</a></li>
344<li><a href="#n_api_napi_is_buffer">napi_is_buffer</a></li>
345<li><a href="#n_api_napi_is_date">napi_is_date</a></li>
346<li><a href="#n_api_napi_is_error_1">napi_is_error</a></li>
347<li><a href="#n_api_napi_is_typedarray">napi_is_typedarray</a></li>
348<li><a href="#n_api_napi_is_dataview">napi_is_dataview</a></li>
349<li><a href="#n_api_napi_strict_equals">napi_strict_equals</a></li>
350<li><a href="#n_api_napi_detach_arraybuffer">napi_detach_arraybuffer</a></li>
351<li><a href="#n_api_napi_is_detached_arraybuffer">napi_is_detached_arraybuffer</a></li>
352</ul>
353</li>
354<li><a href="#n_api_working_with_javascript_properties">Working with JavaScript properties</a>
355<ul>
356<li><a href="#n_api_structures">Structures</a>
357<ul>
358<li><a href="#n_api_napi_property_attributes">napi_property_attributes</a></li>
359<li><a href="#n_api_napi_property_descriptor">napi_property_descriptor</a></li>
360</ul>
361</li>
362<li><a href="#n_api_functions">Functions</a>
363<ul>
364<li><a href="#n_api_napi_get_property_names">napi_get_property_names</a></li>
365<li><a href="#n_api_napi_get_all_property_names">napi_get_all_property_names</a></li>
366<li><a href="#n_api_napi_set_property">napi_set_property</a></li>
367<li><a href="#n_api_napi_get_property">napi_get_property</a></li>
368<li><a href="#n_api_napi_has_property">napi_has_property</a></li>
369<li><a href="#n_api_napi_delete_property">napi_delete_property</a></li>
370<li><a href="#n_api_napi_has_own_property">napi_has_own_property</a></li>
371<li><a href="#n_api_napi_set_named_property">napi_set_named_property</a></li>
372<li><a href="#n_api_napi_get_named_property">napi_get_named_property</a></li>
373<li><a href="#n_api_napi_has_named_property">napi_has_named_property</a></li>
374<li><a href="#n_api_napi_set_element">napi_set_element</a></li>
375<li><a href="#n_api_napi_get_element">napi_get_element</a></li>
376<li><a href="#n_api_napi_has_element">napi_has_element</a></li>
377<li><a href="#n_api_napi_delete_element">napi_delete_element</a></li>
378<li><a href="#n_api_napi_define_properties">napi_define_properties</a></li>
379<li><a href="#n_api_napi_object_freeze">napi_object_freeze</a></li>
380<li><a href="#n_api_napi_object_seal">napi_object_seal</a></li>
381</ul>
382</li>
383</ul>
384</li>
385<li><a href="#n_api_working_with_javascript_functions">Working with JavaScript functions</a>
386<ul>
387<li><a href="#n_api_napi_call_function">napi_call_function</a></li>
388<li><a href="#n_api_napi_create_function">napi_create_function</a></li>
389<li><a href="#n_api_napi_get_cb_info">napi_get_cb_info</a></li>
390<li><a href="#n_api_napi_get_new_target">napi_get_new_target</a></li>
391<li><a href="#n_api_napi_new_instance">napi_new_instance</a></li>
392</ul>
393</li>
394<li><a href="#n_api_object_wrap">Object wrap</a>
395<ul>
396<li><a href="#n_api_napi_define_class">napi_define_class</a></li>
397<li><a href="#n_api_napi_wrap">napi_wrap</a></li>
398<li><a href="#n_api_napi_unwrap">napi_unwrap</a></li>
399<li><a href="#n_api_napi_remove_wrap">napi_remove_wrap</a></li>
400<li><a href="#n_api_napi_type_tag_object">napi_type_tag_object</a></li>
401<li><a href="#n_api_napi_check_object_type_tag">napi_check_object_type_tag</a></li>
402<li><a href="#n_api_napi_add_finalizer">napi_add_finalizer</a></li>
403</ul>
404</li>
405<li><a href="#n_api_simple_asynchronous_operations">Simple asynchronous operations</a>
406<ul>
407<li><a href="#n_api_napi_create_async_work">napi_create_async_work</a></li>
408<li><a href="#n_api_napi_delete_async_work">napi_delete_async_work</a></li>
409<li><a href="#n_api_napi_queue_async_work">napi_queue_async_work</a></li>
410<li><a href="#n_api_napi_cancel_async_work">napi_cancel_async_work</a></li>
411</ul>
412</li>
413<li><a href="#n_api_custom_asynchronous_operations">Custom asynchronous operations</a>
414<ul>
415<li><a href="#n_api_napi_async_init">napi_async_init</a></li>
416<li><a href="#n_api_napi_async_destroy">napi_async_destroy</a></li>
417<li><a href="#n_api_napi_make_callback">napi_make_callback</a></li>
418<li><a href="#n_api_napi_open_callback_scope">napi_open_callback_scope</a></li>
419<li><a href="#n_api_napi_close_callback_scope">napi_close_callback_scope</a></li>
420</ul>
421</li>
422<li><a href="#n_api_version_management">Version management</a>
423<ul>
424<li><a href="#n_api_napi_get_node_version">napi_get_node_version</a></li>
425<li><a href="#n_api_napi_get_version">napi_get_version</a></li>
426</ul>
427</li>
428<li><a href="#n_api_memory_management">Memory management</a>
429<ul>
430<li><a href="#n_api_napi_adjust_external_memory">napi_adjust_external_memory</a></li>
431</ul>
432</li>
433<li><a href="#n_api_promises">Promises</a>
434<ul>
435<li><a href="#n_api_napi_create_promise">napi_create_promise</a></li>
436<li><a href="#n_api_napi_resolve_deferred">napi_resolve_deferred</a></li>
437<li><a href="#n_api_napi_reject_deferred">napi_reject_deferred</a></li>
438<li><a href="#n_api_napi_is_promise">napi_is_promise</a></li>
439</ul>
440</li>
441<li><a href="#n_api_script_execution">Script execution</a>
442<ul>
443<li><a href="#n_api_napi_run_script">napi_run_script</a></li>
444</ul>
445</li>
446<li><a href="#n_api_libuv_event_loop">libuv event loop</a>
447<ul>
448<li><a href="#n_api_napi_get_uv_event_loop">napi_get_uv_event_loop</a></li>
449</ul>
450</li>
451<li><a href="#n_api_asynchronous_thread_safe_function_calls">Asynchronous thread-safe function calls</a>
452<ul>
453<li><a href="#n_api_calling_a_thread_safe_function">Calling a thread-safe function</a></li>
454<li><a href="#n_api_reference_counting_of_thread_safe_functions">Reference counting of thread-safe functions</a></li>
455<li><a href="#n_api_deciding_whether_to_keep_the_process_running">Deciding whether to keep the process running</a></li>
456<li><a href="#n_api_napi_create_threadsafe_function">napi_create_threadsafe_function</a></li>
457<li><a href="#n_api_napi_get_threadsafe_function_context">napi_get_threadsafe_function_context</a></li>
458<li><a href="#n_api_napi_call_threadsafe_function">napi_call_threadsafe_function</a></li>
459<li><a href="#n_api_napi_acquire_threadsafe_function">napi_acquire_threadsafe_function</a></li>
460<li><a href="#n_api_napi_release_threadsafe_function">napi_release_threadsafe_function</a></li>
461<li><a href="#n_api_napi_ref_threadsafe_function">napi_ref_threadsafe_function</a></li>
462<li><a href="#n_api_napi_unref_threadsafe_function">napi_unref_threadsafe_function</a></li>
463</ul>
464</li>
465<li><a href="#n_api_miscellaneous_utilities">Miscellaneous utilities</a></li>
466<li><span class="stability_1"><a href="#n_api_node_api_get_module_file_name">node_api_get_module_file_name</a></span></li>
467</ul>
468</li>
469</ul></details>
470
471      <div id="apicontent">
472        <h2>Node-API<span><a class="mark" href="#n_api_node_api" id="n_api_node_api">#</a></span></h2>
473
474
475<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
476<p>Node-API (formerly N-API) is an API for building native Addons. It is
477independent from the underlying JavaScript runtime (for example, V8) and is
478maintained as part of Node.js itself. This API will be Application Binary
479Interface (ABI) stable across versions of Node.js. It is intended to insulate
480addons from changes in the underlying JavaScript engine and allow modules
481compiled for one major version to run on later major versions of Node.js without
482recompilation. The <a href="https://nodejs.org/en/docs/guides/abi-stability/">ABI Stability</a> guide provides a more in-depth explanation.</p>
483<p>Addons are built/packaged with the same approach/tools outlined in the section
484titled <a href="addons.html">C++ Addons</a>. The only difference is the set of APIs that are used by
485the native code. Instead of using the V8 or <a href="https://github.com/nodejs/nan">Native Abstractions for Node.js</a>
486APIs, the functions available in Node-API are used.</p>
487<p>APIs exposed by Node-API are generally used to create and manipulate
488JavaScript values. Concepts and operations generally map to ideas specified
489in the ECMA-262 Language Specification. The APIs have the following
490properties:</p>
491<ul>
492<li>All Node-API calls return a status code of type <code>napi_status</code>. This
493status indicates whether the API call succeeded or failed.</li>
494<li>The API's return value is passed via an out parameter.</li>
495<li>All JavaScript values are abstracted behind an opaque type named
496<code>napi_value</code>.</li>
497<li>In case of an error status code, additional information can be obtained
498using <code>napi_get_last_error_info</code>. More information can be found in the error
499handling section <a href="#n_api_error_handling">Error handling</a>.</li>
500</ul>
501<p>Node-API is a C API that ensures ABI stability across Node.js versions
502and different compiler levels. A C++ API can be easier to use.
503To support using C++, the project maintains a
504C++ wrapper module called <a href="https://github.com/nodejs/node-addon-api"><code>node-addon-api</code></a>.
505This wrapper provides an inlineable C++ API. Binaries built
506with <code>node-addon-api</code> will depend on the symbols for the Node-API C-based
507functions exported by Node.js. <code>node-addon-api</code> is a more
508efficient way to write code that calls Node-API. Take, for example, the
509following <code>node-addon-api</code> code. The first section shows the
510<code>node-addon-api</code> code and the second section shows what actually gets
511used in the addon.</p>
512<pre><code class="language-cpp">Object obj = Object::<span class="hljs-built_in">New</span>(env);
513obj[<span class="hljs-string">"foo"</span>] = String::<span class="hljs-built_in">New</span>(env, <span class="hljs-string">"bar"</span>);</code></pre>
514<pre><code class="language-cpp">napi_status status;
515napi_value object, string;
516status = <span class="hljs-built_in">napi_create_object</span>(env, &#x26;object);
517<span class="hljs-keyword">if</span> (status != napi_ok) {
518  <span class="hljs-built_in">napi_throw_error</span>(env, ...);
519  <span class="hljs-keyword">return</span>;
520}
521
522status = <span class="hljs-built_in">napi_create_string_utf8</span>(env, <span class="hljs-string">"bar"</span>, NAPI_AUTO_LENGTH, &#x26;string);
523<span class="hljs-keyword">if</span> (status != napi_ok) {
524  <span class="hljs-built_in">napi_throw_error</span>(env, ...);
525  <span class="hljs-keyword">return</span>;
526}
527
528status = <span class="hljs-built_in">napi_set_named_property</span>(env, object, <span class="hljs-string">"foo"</span>, string);
529<span class="hljs-keyword">if</span> (status != napi_ok) {
530  <span class="hljs-built_in">napi_throw_error</span>(env, ...);
531  <span class="hljs-keyword">return</span>;
532}</code></pre>
533<p>The end result is that the addon only uses the exported C APIs. As a result,
534it still gets the benefits of the ABI stability provided by the C API.</p>
535<p>When using <code>node-addon-api</code> instead of the C APIs, start with the API <a href="https://github.com/nodejs/node-addon-api#api-documentation">docs</a>
536for <code>node-addon-api</code>.</p>
537<p>The <a href="https://nodejs.github.io/node-addon-examples/">Node-API Resource</a> offers
538an excellent orientation and tips for developers just getting started with
539Node-API and <code>node-addon-api</code>.</p>
540<section><h3>Implications of ABI stability<span><a class="mark" href="#n_api_implications_of_abi_stability" id="n_api_implications_of_abi_stability">#</a></span></h3>
541<p>Although Node-API provides an ABI stability guarantee, other parts of Node.js do
542not, and any external libraries used from the addon may not. In particular,
543none of the following APIs provide an ABI stability guarantee across major
544versions:</p>
545<ul>
546<li>
547<p>the Node.js C++ APIs available via any of</p>
548<pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node.h></span></span>
549<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_buffer.h></span></span>
550<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_version.h></span></span>
551<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_object_wrap.h></span></span></code></pre>
552</li>
553<li>
554<p>the libuv APIs which are also included with Node.js and available via</p>
555<pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;uv.h></span></span></code></pre>
556</li>
557<li>
558<p>the V8 API available via</p>
559<pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;v8.h></span></span></code></pre>
560</li>
561</ul>
562<p>Thus, for an addon to remain ABI-compatible across Node.js major versions, it
563must use Node-API exclusively by restricting itself to using</p>
564<pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_api.h></span></span></code></pre>
565<p>and by checking, for all external libraries that it uses, that the external
566library makes ABI stability guarantees similar to Node-API.</p>
567</section><section><h3>Building<span><a class="mark" href="#n_api_building" id="n_api_building">#</a></span></h3>
568<p>Unlike modules written in JavaScript, developing and deploying Node.js
569native addons using Node-API requires an additional set of tools. Besides the
570basic tools required to develop for Node.js, the native addon developer
571requires a toolchain that can compile C and C++ code into a binary. In
572addition, depending upon how the native addon is deployed, the <em>user</em> of
573the native addon will also need to have a C/C++ toolchain installed.</p>
574<p>For Linux developers, the necessary C/C++ toolchain packages are readily
575available. <a href="https://gcc.gnu.org">GCC</a> is widely used in the Node.js community to build and
576test across a variety of platforms. For many developers, the <a href="https://llvm.org">LLVM</a>
577compiler infrastructure is also a good choice.</p>
578<p>For Mac developers, <a href="https://developer.apple.com/xcode/">Xcode</a> offers all the required compiler tools.
579However, it is not necessary to install the entire Xcode IDE. The following
580command installs the necessary toolchain:</p>
581<pre><code class="language-bash">xcode-select --install</code></pre>
582<p>For Windows developers, <a href="https://visualstudio.microsoft.com">Visual Studio</a> offers all the required compiler
583tools. However, it is not necessary to install the entire Visual Studio
584IDE. The following command installs the necessary toolchain:</p>
585<pre><code class="language-bash">npm install --global windows-build-tools</code></pre>
586<p>The sections below describe the additional tools available for developing
587and deploying Node.js native addons.</p>
588<h4>Build tools<span><a class="mark" href="#n_api_build_tools" id="n_api_build_tools">#</a></span></h4>
589<p>Both the tools listed here require that <em>users</em> of the native
590addon have a C/C++ toolchain installed in order to successfully install
591the native addon.</p>
592<h5>node-gyp<span><a class="mark" href="#n_api_node_gyp" id="n_api_node_gyp">#</a></span></h5>
593<p><a href="https://github.com/nodejs/node-gyp">node-gyp</a> is a build system based on the <a href="https://github.com/nodejs/gyp-next">gyp-next</a> fork of
594Google's <a href="https://gyp.gsrc.io">GYP</a> tool and comes bundled with npm. GYP, and therefore node-gyp,
595requires that Python be installed.</p>
596<p>Historically, node-gyp has been the tool of choice for building native
597addons. It has widespread adoption and documentation. However, some
598developers have run into limitations in node-gyp.</p>
599<h5>CMake.js<span><a class="mark" href="#n_api_cmake_js" id="n_api_cmake_js">#</a></span></h5>
600<p><a href="https://github.com/cmake-js/cmake-js">CMake.js</a> is an alternative build system based on <a href="https://cmake.org">CMake</a>.</p>
601<p>CMake.js is a good choice for projects that already use CMake or for
602developers affected by limitations in node-gyp.</p>
603<h4>Uploading precompiled binaries<span><a class="mark" href="#n_api_uploading_precompiled_binaries" id="n_api_uploading_precompiled_binaries">#</a></span></h4>
604<p>The three tools listed here permit native addon developers and maintainers
605to create and upload binaries to public or private servers. These tools are
606typically integrated with CI/CD build systems like <a href="https://travis-ci.org">Travis CI</a> and
607<a href="https://www.appveyor.com">AppVeyor</a> to build and upload binaries for a variety of platforms and
608architectures. These binaries are then available for download by users who
609do not need to have a C/C++ toolchain installed.</p>
610<h5>node-pre-gyp<span><a class="mark" href="#n_api_node_pre_gyp" id="n_api_node_pre_gyp">#</a></span></h5>
611<p><a href="https://github.com/mapbox/node-pre-gyp">node-pre-gyp</a> is a tool based on node-gyp that adds the ability to
612upload binaries to a server of the developer's choice. node-pre-gyp has
613particularly good support for uploading binaries to Amazon S3.</p>
614<h5>prebuild<span><a class="mark" href="#n_api_prebuild" id="n_api_prebuild">#</a></span></h5>
615<p><a href="https://github.com/prebuild/prebuild">prebuild</a> is a tool that supports builds using either node-gyp or
616CMake.js. Unlike node-pre-gyp which supports a variety of servers, prebuild
617uploads binaries only to <a href="https://help.github.com/en/github/administering-a-repository/about-releases">GitHub releases</a>. prebuild is a good choice for
618GitHub projects using CMake.js.</p>
619<h5>prebuildify<span><a class="mark" href="#n_api_prebuildify" id="n_api_prebuildify">#</a></span></h5>
620<p><a href="https://github.com/prebuild/prebuildify">prebuildify</a> is a tool based on node-gyp. The advantage of prebuildify is
621that the built binaries are bundled with the native module when it's
622uploaded to npm. The binaries are downloaded from npm and are immediately
623available to the module user when the native module is installed.</p>
624</section><section><h3>Usage<span><a class="mark" href="#n_api_usage" id="n_api_usage">#</a></span></h3>
625<p>In order to use the Node-API functions, include the file <a href="https://github.com/nodejs/node/blob/HEAD/src/node_api.h"><code>node_api.h</code></a> which
626is located in the src directory in the node development tree:</p>
627<pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_api.h></span></span></code></pre>
628<p>This will opt into the default <code>NAPI_VERSION</code> for the given release of Node.js.
629In order to ensure compatibility with specific versions of Node-API, the version
630can be specified explicitly when including the header:</p>
631<pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_VERSION 3</span>
632<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_api.h></span></span></code></pre>
633<p>This restricts the Node-API surface to just the functionality that was available
634in the specified (and earlier) versions.</p>
635<p>Some of the Node-API surface is experimental and requires explicit opt-in:</p>
636<pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_EXPERIMENTAL</span>
637<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_api.h></span></span></code></pre>
638<p>In this case the entire API surface, including any experimental APIs, will be
639available to the module code.</p>
640</section><section><h3>Node-API version matrix<span><a class="mark" href="#n_api_node_api_version_matrix" id="n_api_node_api_version_matrix">#</a></span></h3>
641<p>Node-API versions are additive and versioned independently from Node.js.
642Version 4 is an extension to version 3 in that it has all of the APIs
643from version 3 with some additions. This means that it is not necessary
644to recompile for new versions of Node.js which are
645listed as supporting a later version.</p>
646<!-- For accessibility purposes, this table needs row headers. That means we
647     can't do it in markdown. Hence, the raw HTML. -->
648<table>
649  <tbody><tr>
650    <td></td>
651    <th scope="col">1</th>
652    <th scope="col">2</th>
653    <th scope="col">3</th>
654  </tr>
655  <tr>
656    <th scope="row">v6.x</th>
657    <td></td>
658    <td></td>
659    <td>v6.14.2*</td>
660  </tr>
661  <tr>
662    <th scope="row">v8.x</th>
663    <td>v8.6.0**</td>
664    <td>v8.10.0*</td>
665    <td>v8.11.2</td>
666  </tr>
667  <tr>
668    <th scope="row">v9.x</th>
669    <td>v9.0.0*</td>
670    <td>v9.3.0*</td>
671    <td>v9.11.0*</td>
672  </tr>
673  <tr>
674    <th scope="row">≥ v10.x</th>
675    <td>all releases</td>
676    <td>all releases</td>
677    <td>all releases</td>
678  </tr>
679</tbody></table>
680<table>
681  <tbody><tr>
682    <td></td>
683    <th scope="col">4</th>
684    <th scope="col">5</th>
685    <th scope="col">6</th>
686    <th scope="col">7</th>
687    <th scope="col">8</th>
688  </tr>
689  <tr>
690    <th scope="row">v10.x</th>
691    <td>v10.16.0</td>
692    <td>v10.17.0</td>
693    <td>v10.20.0</td>
694    <td>v10.23.0</td>
695    <td></td>
696  </tr>
697  <tr>
698    <th scope="row">v11.x</th>
699    <td>v11.8.0</td>
700    <td></td>
701    <td></td>
702    <td></td>
703    <td></td>
704  </tr>
705  <tr>
706    <th scope="row">v12.x</th>
707    <td>v12.0.0</td>
708    <td>v12.11.0</td>
709    <td>v12.17.0</td>
710    <td>v12.19.0</td>
711    <td>v12.22.0</td>
712  </tr>
713  <tr>
714    <th scope="row">v13.x</th>
715    <td>v13.0.0</td>
716    <td>v13.0.0</td>
717    <td></td>
718    <td></td>
719    <td></td>
720  </tr>
721  <tr>
722    <th scope="row">v14.x</th>
723    <td>v14.0.0</td>
724    <td>v14.0.0</td>
725    <td>v14.0.0</td>
726    <td>v14.12.0</td>
727    <td>v14.17.0</td>
728  </tr>
729  <tr>
730    <th scope="row">v15.x</th>
731    <td>v15.0.0</td>
732    <td>v15.0.0</td>
733    <td>v15.0.0</td>
734    <td>v15.0.0</td>
735    <td>v15.12.0</td>
736  </tr>
737  <tr>
738    <th scope="row">v16.x</th>
739    <td>v16.0.0</td>
740    <td>v16.0.0</td>
741    <td>v16.0.0</td>
742    <td>v16.0.0</td>
743    <td>v16.0.0</td>
744  </tr>
745</tbody></table>
746<p>* Node-API was experimental.</p>
747<p>** Node.js 8.0.0 included Node-API as experimental. It was released as
748Node-API version 1 but continued to evolve until Node.js 8.6.0. The API is
749different in versions prior to Node.js 8.6.0. We recommend Node-API version 3 or
750later.</p>
751<p>Each API documented for Node-API will have a header named <code>added in:</code>, and APIs
752which are stable will have the additional header <code>Node-API version:</code>.
753APIs are directly usable when using a Node.js version which supports
754the Node-API version shown in <code>Node-API version:</code> or higher.
755When using a Node.js version that does not support the
756<code>Node-API version:</code> listed or if there is no <code>Node-API version:</code> listed,
757then the API will only be available if
758<code>#define NAPI_EXPERIMENTAL</code> precedes the inclusion of <code>node_api.h</code>
759or <code>js_native_api.h</code>. If an API appears not to be available on
760a version of Node.js which is later than the one shown in <code>added in:</code> then
761this is most likely the reason for the apparent absence.</p>
762<p>The Node-APIs associated strictly with accessing ECMAScript features from native
763code can be found separately in <code>js_native_api.h</code> and <code>js_native_api_types.h</code>.
764The APIs defined in these headers are included in <code>node_api.h</code> and
765<code>node_api_types.h</code>. The headers are structured in this way in order to allow
766implementations of Node-API outside of Node.js. For those implementations the
767Node.js specific APIs may not be applicable.</p>
768<p>The Node.js-specific parts of an addon can be separated from the code that
769exposes the actual functionality to the JavaScript environment so that the
770latter may be used with multiple implementations of Node-API. In the example
771below, <code>addon.c</code> and <code>addon.h</code> refer only to <code>js_native_api.h</code>. This ensures
772that <code>addon.c</code> can be reused to compile against either the Node.js
773implementation of Node-API or any implementation of Node-API outside of Node.js.</p>
774<p><code>addon_node.c</code> is a separate file that contains the Node.js specific entry point
775to the addon and which instantiates the addon by calling into <code>addon.c</code> when the
776addon is loaded into a Node.js environment.</p>
777<pre><code class="language-c"><span class="hljs-comment">// addon.h</span>
778<span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> _ADDON_H_</span>
779<span class="hljs-meta">#<span class="hljs-keyword">define</span> _ADDON_H_</span>
780<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;js_native_api.h></span></span>
781napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_env env)</span>;
782<span class="hljs-meta">#<span class="hljs-keyword">endif</span>  <span class="hljs-comment">// _ADDON_H_</span></span></code></pre>
783<pre><code class="language-c"><span class="hljs-comment">// addon.c</span>
784<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"addon.h"</span></span>
785
786<span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_CALL(env, call)                                      \
787  do {                                                            \
788    napi_status status = (call);                                  \
789    <span class="hljs-keyword">if</span> (status != napi_ok) {                                      \
790      const napi_extended_error_info* error_info = NULL;          \
791      napi_get_last_error_info((env), &#x26;error_info);               \
792      bool is_pending;                                            \
793      napi_is_exception_pending((env), &#x26;is_pending);              \
794      <span class="hljs-keyword">if</span> (!is_pending) {                                          \
795        const char* message = (error_info->error_message == NULL) \
796            ? <span class="hljs-string">"empty error message"</span>                               \
797            : error_info->error_message;                          \
798        napi_throw_error((env), NULL, message);                   \
799        return NULL;                                              \
800      }                                                           \
801    }                                                             \
802  } while(0)</span>
803
804<span class="hljs-keyword">static</span> napi_value
805<span class="hljs-title function_">DoSomethingUseful</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> {
806  <span class="hljs-comment">// Do something useful.</span>
807  <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
808}
809
810napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_env env)</span> {
811  napi_value result;
812  NAPI_CALL(env, napi_create_object(env, &#x26;result));
813
814  napi_value exported_function;
815  NAPI_CALL(env, napi_create_function(env,
816                                      <span class="hljs-string">"doSomethingUseful"</span>,
817                                      NAPI_AUTO_LENGTH,
818                                      DoSomethingUseful,
819                                      <span class="hljs-literal">NULL</span>,
820                                      &#x26;exported_function));
821
822  NAPI_CALL(env, napi_set_named_property(env,
823                                         result,
824                                         <span class="hljs-string">"doSomethingUseful"</span>,
825                                         exported_function));
826
827  <span class="hljs-keyword">return</span> result;
828}</code></pre>
829<pre><code class="language-c"><span class="hljs-comment">// addon_node.c</span>
830<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;node_api.h></span></span>
831<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"addon.h"</span></span>
832
833NAPI_MODULE_INIT() {
834  <span class="hljs-comment">// This function body is expected to return a `napi_value`.</span>
835  <span class="hljs-comment">// The variables `napi_env env` and `napi_value exports` may be used within</span>
836  <span class="hljs-comment">// the body, as they are provided by the definition of `NAPI_MODULE_INIT()`.</span>
837  <span class="hljs-keyword">return</span> create_addon(env);
838}</code></pre>
839</section><section><h3>Environment life cycle APIs<span><a class="mark" href="#n_api_environment_life_cycle_apis" id="n_api_environment_life_cycle_apis">#</a></span></h3>
840<p><a href="https://tc39.es/ecma262/#sec-agents">Section 8.7</a> of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a> defines the concept
841of an "Agent" as a self-contained environment in which JavaScript code runs.
842Multiple such Agents may be started and terminated either concurrently or in
843sequence by the process.</p>
844<p>A Node.js environment corresponds to an ECMAScript Agent. In the main process,
845an environment is created at startup, and additional environments can be created
846on separate threads to serve as <a href="https://nodejs.org/api/worker_threads.html">worker threads</a>. When Node.js is embedded in
847another application, the main thread of the application may also construct and
848destroy a Node.js environment multiple times during the life cycle of the
849application process such that each Node.js environment created by the
850application may, in turn, during its life cycle create and destroy additional
851environments as worker threads.</p>
852<p>From the perspective of a native addon this means that the bindings it provides
853may be called multiple times, from multiple contexts, and even concurrently from
854multiple threads.</p>
855<p>Native addons may need to allocate global state which they use during
856their entire life cycle such that the state must be unique to each instance of
857the addon.</p>
858<p>To this end, Node-API provides a way to allocate data such that its life cycle
859is tied to the life cycle of the Agent.</p>
860<h4>napi_set_instance_data<span><a class="mark" href="#n_api_napi_set_instance_data" id="n_api_napi_set_instance_data">#</a></span></h4>
861<div class="api_metadata">
862<span>Added in: v12.8.0, v10.20.0</span>
863<span>N-API version: 6</span>
864</div>
865<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_instance_data</span><span class="hljs-params">(napi_env env,
866                                   <span class="hljs-type">void</span>* data,
867                                   napi_finalize finalize_cb,
868                                   <span class="hljs-type">void</span>* finalize_hint)</span>;</code></pre>
869<ul>
870<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
871<li><code>[in] data</code>: The data item to make available to bindings of this instance.</li>
872<li><code>[in] finalize_cb</code>: The function to call when the environment is being torn
873down. The function receives <code>data</code> so that it might free it.
874<a href="#n_api_napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
875<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
876collection.</li>
877</ul>
878<p>Returns <code>napi_ok</code> if the API succeeded.</p>
879<p>This API associates <code>data</code> with the currently running Agent. <code>data</code> can later
880be retrieved using <code>napi_get_instance_data()</code>. Any existing data associated with
881the currently running Agent which was set by means of a previous call to
882<code>napi_set_instance_data()</code> will be overwritten. If a <code>finalize_cb</code> was provided
883by the previous call, it will not be called.</p>
884<h4>napi_get_instance_data<span><a class="mark" href="#n_api_napi_get_instance_data" id="n_api_napi_get_instance_data">#</a></span></h4>
885<div class="api_metadata">
886<span>Added in: v12.8.0, v10.20.0</span>
887<span>N-API version: 6</span>
888</div>
889<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_instance_data</span><span class="hljs-params">(napi_env env,
890                                   <span class="hljs-type">void</span>** data)</span>;</code></pre>
891<ul>
892<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
893<li><code>[out] data</code>: The data item that was previously associated with the currently
894running Agent by a call to <code>napi_set_instance_data()</code>.</li>
895</ul>
896<p>Returns <code>napi_ok</code> if the API succeeded.</p>
897<p>This API retrieves data that was previously associated with the currently
898running Agent via <code>napi_set_instance_data()</code>. If no data is set, the call will
899succeed and <code>data</code> will be set to <code>NULL</code>.</p>
900</section><section><h3>Basic Node-API data types<span><a class="mark" href="#n_api_basic_node_api_data_types" id="n_api_basic_node_api_data_types">#</a></span></h3>
901<p>Node-API exposes the following fundamental datatypes as abstractions that are
902consumed by the various APIs. These APIs should be treated as opaque,
903introspectable only with other Node-API calls.</p>
904<h4>napi_status<span><a class="mark" href="#n_api_napi_status" id="n_api_napi_status">#</a></span></h4>
905<div class="api_metadata">
906<span>Added in: v8.0.0</span>
907<span>N-API version: 1</span>
908</div>
909<p>Integral status code indicating the success or failure of a Node-API call.
910Currently, the following status codes are supported.</p>
911<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
912  napi_ok,
913  napi_invalid_arg,
914  napi_object_expected,
915  napi_string_expected,
916  napi_name_expected,
917  napi_function_expected,
918  napi_number_expected,
919  napi_boolean_expected,
920  napi_array_expected,
921  napi_generic_failure,
922  napi_pending_exception,
923  napi_cancelled,
924  napi_escape_called_twice,
925  napi_handle_scope_mismatch,
926  napi_callback_scope_mismatch,
927  napi_queue_full,
928  napi_closing,
929  napi_bigint_expected,
930  napi_date_expected,
931  napi_arraybuffer_expected,
932  napi_detachable_arraybuffer_expected,
933  napi_would_deadlock,  <span class="hljs-comment">/* unused */</span>
934  napi_no_external_buffers_allowed
935} napi_status;</code></pre>
936<p>If additional information is required upon an API returning a failed status,
937it can be obtained by calling <code>napi_get_last_error_info</code>.</p>
938<h4>napi_extended_error_info<span><a class="mark" href="#n_api_napi_extended_error_info" id="n_api_napi_extended_error_info">#</a></span></h4>
939<div class="api_metadata">
940<span>Added in: v8.0.0</span>
941<span>N-API version: 1</span>
942</div>
943<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span>
944  <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* error_message;
945  <span class="hljs-type">void</span>* engine_reserved;
946  <span class="hljs-type">uint32_t</span> engine_error_code;
947  napi_status error_code;
948} napi_extended_error_info;</code></pre>
949<ul>
950<li><code>error_message</code>: UTF8-encoded string containing a VM-neutral description of
951the error.</li>
952<li><code>engine_reserved</code>: Reserved for VM-specific error details. This is currently
953not implemented for any VM.</li>
954<li><code>engine_error_code</code>: VM-specific error code. This is currently
955not implemented for any VM.</li>
956<li><code>error_code</code>: The Node-API status code that originated with the last error.</li>
957</ul>
958<p>See the <a href="#n_api_error_handling">Error handling</a> section for additional information.</p>
959<h4>napi_env<span><a class="mark" href="#n_api_napi_env" id="n_api_napi_env">#</a></span></h4>
960<p><code>napi_env</code> is used to represent a context that the underlying Node-API
961implementation can use to persist VM-specific state. This structure is passed
962to native functions when they're invoked, and it must be passed back when
963making Node-API calls. Specifically, the same <code>napi_env</code> that was passed in when
964the initial native function was called must be passed to any subsequent
965nested Node-API calls. Caching the <code>napi_env</code> for the purpose of general reuse,
966and passing the <code>napi_env</code> between instances of the same addon running on
967different <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads is not allowed. The <code>napi_env</code> becomes invalid
968when an instance of a native addon is unloaded. Notification of this event is
969delivered through the callbacks given to <a href="#n_api_napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a> and
970<a href="#n_api_napi_set_instance_data"><code>napi_set_instance_data</code></a>.</p>
971<h4>napi_value<span><a class="mark" href="#n_api_napi_value" id="n_api_napi_value">#</a></span></h4>
972<p>This is an opaque pointer that is used to represent a JavaScript value.</p>
973<h4>napi_threadsafe_function<span><a class="mark" href="#n_api_napi_threadsafe_function" id="n_api_napi_threadsafe_function">#</a></span></h4>
974<div class="api_metadata">
975<span>Added in: v10.6.0</span>
976<span>N-API version: 4</span>
977</div>
978<p>This is an opaque pointer that represents a JavaScript function which can be
979called asynchronously from multiple threads via
980<code>napi_call_threadsafe_function()</code>.</p>
981<h4>napi_threadsafe_function_release_mode<span><a class="mark" href="#n_api_napi_threadsafe_function_release_mode" id="n_api_napi_threadsafe_function_release_mode">#</a></span></h4>
982<div class="api_metadata">
983<span>Added in: v10.6.0</span>
984<span>N-API version: 4</span>
985</div>
986<p>A value to be given to <code>napi_release_threadsafe_function()</code> to indicate whether
987the thread-safe function is to be closed immediately (<code>napi_tsfn_abort</code>) or
988merely released (<code>napi_tsfn_release</code>) and thus available for subsequent use via
989<code>napi_acquire_threadsafe_function()</code> and <code>napi_call_threadsafe_function()</code>.</p>
990<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
991  napi_tsfn_release,
992  napi_tsfn_abort
993} napi_threadsafe_function_release_mode;</code></pre>
994<h4>napi_threadsafe_function_call_mode<span><a class="mark" href="#n_api_napi_threadsafe_function_call_mode" id="n_api_napi_threadsafe_function_call_mode">#</a></span></h4>
995<div class="api_metadata">
996<span>Added in: v10.6.0</span>
997<span>N-API version: 4</span>
998</div>
999<p>A value to be given to <code>napi_call_threadsafe_function()</code> to indicate whether
1000the call should block whenever the queue associated with the thread-safe
1001function is full.</p>
1002<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
1003  napi_tsfn_nonblocking,
1004  napi_tsfn_blocking
1005} napi_threadsafe_function_call_mode;</code></pre>
1006<h4>Node-API memory management types<span><a class="mark" href="#n_api_node_api_memory_management_types" id="n_api_node_api_memory_management_types">#</a></span></h4>
1007<h5>napi_handle_scope<span><a class="mark" href="#n_api_napi_handle_scope" id="n_api_napi_handle_scope">#</a></span></h5>
1008<p>This is an abstraction used to control and modify the lifetime of objects
1009created within a particular scope. In general, Node-API values are created
1010within the context of a handle scope. When a native method is called from
1011JavaScript, a default handle scope will exist. If the user does not explicitly
1012create a new handle scope, Node-API values will be created in the default handle
1013scope. For any invocations of code outside the execution of a native method
1014(for instance, during a libuv callback invocation), the module is required to
1015create a scope before invoking any functions that can result in the creation
1016of JavaScript values.</p>
1017<p>Handle scopes are created using <a href="#n_api_napi_open_handle_scope"><code>napi_open_handle_scope</code></a> and are destroyed
1018using <a href="#n_api_napi_close_handle_scope"><code>napi_close_handle_scope</code></a>. Closing the scope can indicate to the GC
1019that all <code>napi_value</code>s created during the lifetime of the handle scope are no
1020longer referenced from the current stack frame.</p>
1021<p>For more details, review the <a href="#n_api_object_lifetime_management">Object lifetime management</a>.</p>
1022<h5>napi_escapable_handle_scope<span><a class="mark" href="#n_api_napi_escapable_handle_scope" id="n_api_napi_escapable_handle_scope">#</a></span></h5>
1023<div class="api_metadata">
1024<span>Added in: v8.0.0</span>
1025<span>N-API version: 1</span>
1026</div>
1027<p>Escapable handle scopes are a special type of handle scope to return values
1028created within a particular handle scope to a parent scope.</p>
1029<h5>napi_ref<span><a class="mark" href="#n_api_napi_ref" id="n_api_napi_ref">#</a></span></h5>
1030<div class="api_metadata">
1031<span>Added in: v8.0.0</span>
1032<span>N-API version: 1</span>
1033</div>
1034<p>This is the abstraction to use to reference a <code>napi_value</code>. This allows for
1035users to manage the lifetimes of JavaScript values, including defining their
1036minimum lifetimes explicitly.</p>
1037<p>For more details, review the <a href="#n_api_object_lifetime_management">Object lifetime management</a>.</p>
1038<h5>napi_type_tag<span><a class="mark" href="#n_api_napi_type_tag" id="n_api_napi_type_tag">#</a></span></h5>
1039<div class="api_metadata">
1040<span>Added in: v14.8.0, v12.19.0</span>
1041<span>N-API version: 8</span>
1042</div>
1043<p>A 128-bit value stored as two unsigned 64-bit integers. It serves as a UUID
1044with which JavaScript objects can be "tagged" in order to ensure that they are
1045of a certain type. This is a stronger check than <a href="#n_api_napi_instanceof"><code>napi_instanceof</code></a>, because
1046the latter can report a false positive if the object's prototype has been
1047manipulated. Type-tagging is most useful in conjunction with <a href="#n_api_napi_wrap"><code>napi_wrap</code></a>
1048because it ensures that the pointer retrieved from a wrapped object can be
1049safely cast to the native type corresponding to the type tag that had been
1050previously applied to the JavaScript object.</p>
1051<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span>
1052  <span class="hljs-type">uint64_t</span> lower;
1053  <span class="hljs-type">uint64_t</span> upper;
1054} napi_type_tag;</code></pre>
1055<h5>napi_async_cleanup_hook_handle<span><a class="mark" href="#n_api_napi_async_cleanup_hook_handle" id="n_api_napi_async_cleanup_hook_handle">#</a></span></h5>
1056<div class="api_metadata">
1057<span>Added in: v14.10.0</span>
1058</div>
1059<p>An opaque value returned by <a href="#n_api_napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>. It must be passed
1060to <a href="#n_api_napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a> when the chain of asynchronous cleanup
1061events completes.</p>
1062<h4>Node-API callback types<span><a class="mark" href="#n_api_node_api_callback_types" id="n_api_node_api_callback_types">#</a></span></h4>
1063<h5>napi_callback_info<span><a class="mark" href="#n_api_napi_callback_info" id="n_api_napi_callback_info">#</a></span></h5>
1064<div class="api_metadata">
1065<span>Added in: v8.0.0</span>
1066<span>N-API version: 1</span>
1067</div>
1068<p>Opaque datatype that is passed to a callback function. It can be used for
1069getting additional information about the context in which the callback was
1070invoked.</p>
1071<h5>napi_callback<span><a class="mark" href="#n_api_napi_callback" id="n_api_napi_callback">#</a></span></h5>
1072<div class="api_metadata">
1073<span>Added in: v8.0.0</span>
1074<span>N-API version: 1</span>
1075</div>
1076<p>Function pointer type for user-provided native functions which are to be
1077exposed to JavaScript via Node-API. Callback functions should satisfy the
1078following signature:</p>
1079<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">napi_value</span> <span class="hljs-params">(*napi_callback)</span><span class="hljs-params">(napi_env, napi_callback_info)</span>;</code></pre>
1080<p>Unless for reasons discussed in <a href="#n_api_object_lifetime_management">Object Lifetime Management</a>, creating a
1081handle and/or callback scope inside a <code>napi_callback</code> is not necessary.</p>
1082<h5>napi_finalize<span><a class="mark" href="#n_api_napi_finalize" id="n_api_napi_finalize">#</a></span></h5>
1083<div class="api_metadata">
1084<span>Added in: v8.0.0</span>
1085<span>N-API version: 1</span>
1086</div>
1087<p>Function pointer type for add-on provided functions that allow the user to be
1088notified when externally-owned data is ready to be cleaned up because the
1089object with which it was associated with, has been garbage-collected. The user
1090must provide a function satisfying the following signature which would get
1091called upon the object's collection. Currently, <code>napi_finalize</code> can be used for
1092finding out when objects that have external data are collected.</p>
1093<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_finalize)</span><span class="hljs-params">(napi_env env,
1094                              <span class="hljs-type">void</span>* finalize_data,
1095                              <span class="hljs-type">void</span>* finalize_hint)</span>;</code></pre>
1096<p>Unless for reasons discussed in <a href="#n_api_object_lifetime_management">Object Lifetime Management</a>, creating a
1097handle and/or callback scope inside the function body is not necessary.</p>
1098<h5>napi_async_execute_callback<span><a class="mark" href="#n_api_napi_async_execute_callback" id="n_api_napi_async_execute_callback">#</a></span></h5>
1099<div class="api_metadata">
1100<span>Added in: v8.0.0</span>
1101<span>N-API version: 1</span>
1102</div>
1103<p>Function pointer used with functions that support asynchronous
1104operations. Callback functions must satisfy the following signature:</p>
1105<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_execute_callback)</span><span class="hljs-params">(napi_env env, <span class="hljs-type">void</span>* data)</span>;</code></pre>
1106<p>Implementations of this function must avoid making Node-API calls that execute
1107JavaScript or interact with JavaScript objects. Node-API calls should be in the
1108<code>napi_async_complete_callback</code> instead. Do not use the <code>napi_env</code> parameter as
1109it will likely result in execution of JavaScript.</p>
1110<h5>napi_async_complete_callback<span><a class="mark" href="#n_api_napi_async_complete_callback" id="n_api_napi_async_complete_callback">#</a></span></h5>
1111<div class="api_metadata">
1112<span>Added in: v8.0.0</span>
1113<span>N-API version: 1</span>
1114</div>
1115<p>Function pointer used with functions that support asynchronous
1116operations. Callback functions must satisfy the following signature:</p>
1117<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_complete_callback)</span><span class="hljs-params">(napi_env env,
1118                                             napi_status status,
1119                                             <span class="hljs-type">void</span>* data)</span>;</code></pre>
1120<p>Unless for reasons discussed in <a href="#n_api_object_lifetime_management">Object Lifetime Management</a>, creating a
1121handle and/or callback scope inside the function body is not necessary.</p>
1122<h5>napi_threadsafe_function_call_js<span><a class="mark" href="#n_api_napi_threadsafe_function_call_js" id="n_api_napi_threadsafe_function_call_js">#</a></span></h5>
1123<div class="api_metadata">
1124<span>Added in: v10.6.0</span>
1125<span>N-API version: 4</span>
1126</div>
1127<p>Function pointer used with asynchronous thread-safe function calls. The callback
1128will be called on the main thread. Its purpose is to use a data item arriving
1129via the queue from one of the secondary threads to construct the parameters
1130necessary for a call into JavaScript, usually via <code>napi_call_function</code>, and then
1131make the call into JavaScript.</p>
1132<p>The data arriving from the secondary thread via the queue is given in the <code>data</code>
1133parameter and the JavaScript function to call is given in the <code>js_callback</code>
1134parameter.</p>
1135<p>Node-API sets up the environment prior to calling this callback, so it is
1136sufficient to call the JavaScript function via <code>napi_call_function</code> rather than
1137via <code>napi_make_callback</code>.</p>
1138<p>Callback functions must satisfy the following signature:</p>
1139<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_threadsafe_function_call_js)</span><span class="hljs-params">(napi_env env,
1140                                                 napi_value js_callback,
1141                                                 <span class="hljs-type">void</span>* context,
1142                                                 <span class="hljs-type">void</span>* data)</span>;</code></pre>
1143<ul>
1144<li><code>[in] env</code>: The environment to use for API calls, or <code>NULL</code> if the thread-safe
1145function is being torn down and <code>data</code> may need to be freed.</li>
1146<li><code>[in] js_callback</code>: The JavaScript function to call, or <code>NULL</code> if the
1147thread-safe function is being torn down and <code>data</code> may need to be freed. It
1148may also be <code>NULL</code> if the thread-safe function was created without
1149<code>js_callback</code>.</li>
1150<li><code>[in] context</code>: The optional data with which the thread-safe function was
1151created.</li>
1152<li><code>[in] data</code>: Data created by the secondary thread. It is the responsibility of
1153the callback to convert this native data to JavaScript values (with Node-API
1154functions) that can be passed as parameters when <code>js_callback</code> is invoked.
1155This pointer is managed entirely by the threads and this callback. Thus this
1156callback should free the data.</li>
1157</ul>
1158<p>Unless for reasons discussed in <a href="#n_api_object_lifetime_management">Object Lifetime Management</a>, creating a
1159handle and/or callback scope inside the function body is not necessary.</p>
1160<h5>napi_async_cleanup_hook<span><a class="mark" href="#n_api_napi_async_cleanup_hook" id="n_api_napi_async_cleanup_hook">#</a></span></h5>
1161<div class="api_metadata">
1162<span>Added in: v14.10.0</span>
1163</div>
1164<p>Function pointer used with <a href="#n_api_napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>. It will be called
1165when the environment is being torn down.</p>
1166<p>Callback functions must satisfy the following signature:</p>
1167<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_cleanup_hook)</span><span class="hljs-params">(napi_async_cleanup_hook_handle handle,
1168                                        <span class="hljs-type">void</span>* data)</span>;</code></pre>
1169<ul>
1170<li><code>[in] handle</code>: The handle that must be passed to
1171<a href="#n_api_napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a> after completion of the asynchronous
1172cleanup.</li>
1173<li><code>[in] data</code>: The data that was passed to <a href="#n_api_napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>.</li>
1174</ul>
1175<p>The body of the function should initiate the asynchronous cleanup actions at the
1176end of which <code>handle</code> must be passed in a call to
1177<a href="#n_api_napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a>.</p>
1178</section><section><h3>Error handling<span><a class="mark" href="#n_api_error_handling" id="n_api_error_handling">#</a></span></h3>
1179<p>Node-API uses both return values and JavaScript exceptions for error handling.
1180The following sections explain the approach for each case.</p>
1181<h4>Return values<span><a class="mark" href="#n_api_return_values" id="n_api_return_values">#</a></span></h4>
1182<p>All of the Node-API functions share the same error handling pattern. The
1183return type of all API functions is <code>napi_status</code>.</p>
1184<p>The return value will be <code>napi_ok</code> if the request was successful and
1185no uncaught JavaScript exception was thrown. If an error occurred AND
1186an exception was thrown, the <code>napi_status</code> value for the error
1187will be returned. If an exception was thrown, and no error occurred,
1188<code>napi_pending_exception</code> will be returned.</p>
1189<p>In cases where a return value other than <code>napi_ok</code> or
1190<code>napi_pending_exception</code> is returned, <a href="#n_api_napi_is_exception_pending"><code>napi_is_exception_pending</code></a>
1191must be called to check if an exception is pending.
1192See the section on exceptions for more details.</p>
1193<p>The full set of possible <code>napi_status</code> values is defined
1194in <code>napi_api_types.h</code>.</p>
1195<p>The <code>napi_status</code> return value provides a VM-independent representation of
1196the error which occurred. In some cases it is useful to be able to get
1197more detailed information, including a string representing the error as well as
1198VM (engine)-specific information.</p>
1199<p>In order to retrieve this information <a href="#n_api_napi_get_last_error_info"><code>napi_get_last_error_info</code></a>
1200is provided which returns a <code>napi_extended_error_info</code> structure.
1201The format of the <code>napi_extended_error_info</code> structure is as follows:</p>
1202<div class="api_metadata">
1203<span>Added in: v8.0.0</span>
1204<span>N-API version: 1</span>
1205</div>
1206<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">napi_extended_error_info</span> {</span>
1207  <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* error_message;
1208  <span class="hljs-type">void</span>* engine_reserved;
1209  <span class="hljs-type">uint32_t</span> engine_error_code;
1210  napi_status error_code;
1211};</code></pre>
1212<ul>
1213<li><code>error_message</code>: Textual representation of the error that occurred.</li>
1214<li><code>engine_reserved</code>: Opaque handle reserved for engine use only.</li>
1215<li><code>engine_error_code</code>: VM specific error code.</li>
1216<li><code>error_code</code>: Node-API status code for the last error.</li>
1217</ul>
1218<p><a href="#n_api_napi_get_last_error_info"><code>napi_get_last_error_info</code></a> returns the information for the last
1219Node-API call that was made.</p>
1220<p>Do not rely on the content or format of any of the extended information as it
1221is not subject to SemVer and may change at any time. It is intended only for
1222logging purposes.</p>
1223<h5>napi_get_last_error_info<span><a class="mark" href="#n_api_napi_get_last_error_info" id="n_api_napi_get_last_error_info">#</a></span></h5>
1224<div class="api_metadata">
1225<span>Added in: v8.0.0</span>
1226<span>N-API version: 1</span>
1227</div>
1228<pre><code class="language-c">napi_status
1229<span class="hljs-title function_">napi_get_last_error_info</span><span class="hljs-params">(napi_env env,
1230                         <span class="hljs-keyword">const</span> napi_extended_error_info** result)</span>;</code></pre>
1231<ul>
1232<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1233<li><code>[out] result</code>: The <code>napi_extended_error_info</code> structure with more
1234information about the error.</li>
1235</ul>
1236<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1237<p>This API retrieves a <code>napi_extended_error_info</code> structure with information
1238about the last error that occurred.</p>
1239<p>The content of the <code>napi_extended_error_info</code> returned is only valid up until
1240a Node-API function is called on the same <code>env</code>.</p>
1241<p>Do not rely on the content or format of any of the extended information as it
1242is not subject to SemVer and may change at any time. It is intended only for
1243logging purposes.</p>
1244<p>This API can be called even if there is a pending JavaScript exception.</p>
1245<h4>Exceptions<span><a class="mark" href="#n_api_exceptions" id="n_api_exceptions">#</a></span></h4>
1246<p>Any Node-API function call may result in a pending JavaScript exception. This is
1247the case for any of the API functions, even those that may not cause the
1248execution of JavaScript.</p>
1249<p>If the <code>napi_status</code> returned by a function is <code>napi_ok</code> then no
1250exception is pending and no additional action is required. If the
1251<code>napi_status</code> returned is anything other than <code>napi_ok</code> or
1252<code>napi_pending_exception</code>, in order to try to recover and continue
1253instead of simply returning immediately, <a href="#n_api_napi_is_exception_pending"><code>napi_is_exception_pending</code></a>
1254must be called in order to determine if an exception is pending or not.</p>
1255<p>In many cases when a Node-API function is called and an exception is
1256already pending, the function will return immediately with a
1257<code>napi_status</code> of <code>napi_pending_exception</code>. However, this is not the case
1258for all functions. Node-API allows a subset of the functions to be
1259called to allow for some minimal cleanup before returning to JavaScript.
1260In that case, <code>napi_status</code> will reflect the status for the function. It
1261will not reflect previous pending exceptions. To avoid confusion, check
1262the error status after every function call.</p>
1263<p>When an exception is pending one of two approaches can be employed.</p>
1264<p>The first approach is to do any appropriate cleanup and then return so that
1265execution will return to JavaScript. As part of the transition back to
1266JavaScript, the exception will be thrown at the point in the JavaScript
1267code where the native method was invoked. The behavior of most Node-API calls
1268is unspecified while an exception is pending, and many will simply return
1269<code>napi_pending_exception</code>, so do as little as possible and then return to
1270JavaScript where the exception can be handled.</p>
1271<p>The second approach is to try to handle the exception. There will be cases
1272where the native code can catch the exception, take the appropriate action,
1273and then continue. This is only recommended in specific cases
1274where it is known that the exception can be safely handled. In these
1275cases <a href="#n_api_napi_get_and_clear_last_exception"><code>napi_get_and_clear_last_exception</code></a> can be used to get and
1276clear the exception. On success, result will contain the handle to
1277the last JavaScript <code>Object</code> thrown. If it is determined, after
1278retrieving the exception, the exception cannot be handled after all
1279it can be re-thrown it with <a href="#n_api_napi_throw"><code>napi_throw</code></a> where error is the
1280JavaScript value to be thrown.</p>
1281<p>The following utility functions are also available in case native code
1282needs to throw an exception or determine if a <code>napi_value</code> is an instance
1283of a JavaScript <code>Error</code> object: <a href="#n_api_napi_throw_error"><code>napi_throw_error</code></a>,
1284<a href="#n_api_napi_throw_type_error"><code>napi_throw_type_error</code></a>, <a href="#n_api_napi_throw_range_error"><code>napi_throw_range_error</code></a> and
1285<a href="#n_api_napi_is_error"><code>napi_is_error</code></a>.</p>
1286<p>The following utility functions are also available in case native
1287code needs to create an <code>Error</code> object: <a href="#n_api_napi_create_error"><code>napi_create_error</code></a>,
1288<a href="#n_api_napi_create_type_error"><code>napi_create_type_error</code></a>, and <a href="#n_api_napi_create_range_error"><code>napi_create_range_error</code></a>,
1289where result is the <code>napi_value</code> that refers to the newly created
1290JavaScript <code>Error</code> object.</p>
1291<p>The Node.js project is adding error codes to all of the errors
1292generated internally. The goal is for applications to use these
1293error codes for all error checking. The associated error messages
1294will remain, but will only be meant to be used for logging and
1295display with the expectation that the message can change without
1296SemVer applying. In order to support this model with Node-API, both
1297in internal functionality and for module specific functionality
1298(as its good practice), the <code>throw_</code> and <code>create_</code> functions
1299take an optional code parameter which is the string for the code
1300to be added to the error object. If the optional parameter is <code>NULL</code>
1301then no code will be associated with the error. If a code is provided,
1302the name associated with the error is also updated to be:</p>
1303<pre><code class="language-text">originalName [code]</code></pre>
1304<p>where <code>originalName</code> is the original name associated with the error
1305and <code>code</code> is the code that was provided. For example, if the code
1306is <code>'ERR_ERROR_1'</code> and a <code>TypeError</code> is being created the name will be:</p>
1307<pre><code class="language-text">TypeError [ERR_ERROR_1]</code></pre>
1308<h5>napi_throw<span><a class="mark" href="#n_api_napi_throw" id="n_api_napi_throw">#</a></span></h5>
1309<div class="api_metadata">
1310<span>Added in: v8.0.0</span>
1311<span>N-API version: 1</span>
1312</div>
1313<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw</span><span class="hljs-params">(napi_env env, napi_value error)</span>;</code></pre>
1314<ul>
1315<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1316<li><code>[in] error</code>: The JavaScript value to be thrown.</li>
1317</ul>
1318<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1319<p>This API throws the JavaScript value provided.</p>
1320<h5>napi_throw_error<span><a class="mark" href="#n_api_napi_throw_error" id="n_api_napi_throw_error">#</a></span></h5>
1321<div class="api_metadata">
1322<span>Added in: v8.0.0</span>
1323<span>N-API version: 1</span>
1324</div>
1325<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_error</span><span class="hljs-params">(napi_env env,
1326                                         <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* code,
1327                                         <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* msg)</span>;</code></pre>
1328<ul>
1329<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1330<li><code>[in] code</code>: Optional error code to be set on the error.</li>
1331<li><code>[in] msg</code>: C string representing the text to be associated with the error.</li>
1332</ul>
1333<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1334<p>This API throws a JavaScript <code>Error</code> with the text provided.</p>
1335<h5>napi_throw_type_error<span><a class="mark" href="#n_api_napi_throw_type_error" id="n_api_napi_throw_type_error">#</a></span></h5>
1336<div class="api_metadata">
1337<span>Added in: v8.0.0</span>
1338<span>N-API version: 1</span>
1339</div>
1340<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_type_error</span><span class="hljs-params">(napi_env env,
1341                                              <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* code,
1342                                              <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* msg)</span>;</code></pre>
1343<ul>
1344<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1345<li><code>[in] code</code>: Optional error code to be set on the error.</li>
1346<li><code>[in] msg</code>: C string representing the text to be associated with the error.</li>
1347</ul>
1348<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1349<p>This API throws a JavaScript <code>TypeError</code> with the text provided.</p>
1350<h5>napi_throw_range_error<span><a class="mark" href="#n_api_napi_throw_range_error" id="n_api_napi_throw_range_error">#</a></span></h5>
1351<div class="api_metadata">
1352<span>Added in: v8.0.0</span>
1353<span>N-API version: 1</span>
1354</div>
1355<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_range_error</span><span class="hljs-params">(napi_env env,
1356                                               <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* code,
1357                                               <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* msg)</span>;</code></pre>
1358<ul>
1359<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1360<li><code>[in] code</code>: Optional error code to be set on the error.</li>
1361<li><code>[in] msg</code>: C string representing the text to be associated with the error.</li>
1362</ul>
1363<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1364<p>This API throws a JavaScript <code>RangeError</code> with the text provided.</p>
1365<h5>napi_is_error<span><a class="mark" href="#n_api_napi_is_error" id="n_api_napi_is_error">#</a></span></h5>
1366<div class="api_metadata">
1367<span>Added in: v8.0.0</span>
1368<span>N-API version: 1</span>
1369</div>
1370<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_is_error</span><span class="hljs-params">(napi_env env,
1371                                      napi_value value,
1372                                      <span class="hljs-type">bool</span>* result)</span>;</code></pre>
1373<ul>
1374<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1375<li><code>[in] value</code>: The <code>napi_value</code> to be checked.</li>
1376<li><code>[out] result</code>: Boolean value that is set to true if <code>napi_value</code> represents
1377an error, false otherwise.</li>
1378</ul>
1379<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1380<p>This API queries a <code>napi_value</code> to check if it represents an error object.</p>
1381<h5>napi_create_error<span><a class="mark" href="#n_api_napi_create_error" id="n_api_napi_create_error">#</a></span></h5>
1382<div class="api_metadata">
1383<span>Added in: v8.0.0</span>
1384<span>N-API version: 1</span>
1385</div>
1386<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_error</span><span class="hljs-params">(napi_env env,
1387                                          napi_value code,
1388                                          napi_value msg,
1389                                          napi_value* result)</span>;</code></pre>
1390<ul>
1391<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1392<li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be
1393associated with the error.</li>
1394<li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as
1395the message for the <code>Error</code>.</li>
1396<li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li>
1397</ul>
1398<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1399<p>This API returns a JavaScript <code>Error</code> with the text provided.</p>
1400<h5>napi_create_type_error<span><a class="mark" href="#n_api_napi_create_type_error" id="n_api_napi_create_type_error">#</a></span></h5>
1401<div class="api_metadata">
1402<span>Added in: v8.0.0</span>
1403<span>N-API version: 1</span>
1404</div>
1405<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_type_error</span><span class="hljs-params">(napi_env env,
1406                                               napi_value code,
1407                                               napi_value msg,
1408                                               napi_value* result)</span>;</code></pre>
1409<ul>
1410<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1411<li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be
1412associated with the error.</li>
1413<li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as
1414the message for the <code>Error</code>.</li>
1415<li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li>
1416</ul>
1417<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1418<p>This API returns a JavaScript <code>TypeError</code> with the text provided.</p>
1419<h5>napi_create_range_error<span><a class="mark" href="#n_api_napi_create_range_error" id="n_api_napi_create_range_error">#</a></span></h5>
1420<div class="api_metadata">
1421<span>Added in: v8.0.0</span>
1422<span>N-API version: 1</span>
1423</div>
1424<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_range_error</span><span class="hljs-params">(napi_env env,
1425                                                napi_value code,
1426                                                napi_value msg,
1427                                                napi_value* result)</span>;</code></pre>
1428<ul>
1429<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1430<li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be
1431associated with the error.</li>
1432<li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as
1433the message for the <code>Error</code>.</li>
1434<li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li>
1435</ul>
1436<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1437<p>This API returns a JavaScript <code>RangeError</code> with the text provided.</p>
1438<h5>napi_get_and_clear_last_exception<span><a class="mark" href="#n_api_napi_get_and_clear_last_exception" id="n_api_napi_get_and_clear_last_exception">#</a></span></h5>
1439<div class="api_metadata">
1440<span>Added in: v8.0.0</span>
1441<span>N-API version: 1</span>
1442</div>
1443<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_and_clear_last_exception</span><span class="hljs-params">(napi_env env,
1444                                              napi_value* result)</span>;</code></pre>
1445<ul>
1446<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1447<li><code>[out] result</code>: The exception if one is pending, <code>NULL</code> otherwise.</li>
1448</ul>
1449<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1450<p>This API can be called even if there is a pending JavaScript exception.</p>
1451<h5>napi_is_exception_pending<span><a class="mark" href="#n_api_napi_is_exception_pending" id="n_api_napi_is_exception_pending">#</a></span></h5>
1452<div class="api_metadata">
1453<span>Added in: v8.0.0</span>
1454<span>N-API version: 1</span>
1455</div>
1456<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_exception_pending</span><span class="hljs-params">(napi_env env, <span class="hljs-type">bool</span>* result)</span>;</code></pre>
1457<ul>
1458<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1459<li><code>[out] result</code>: Boolean value that is set to true if an exception is pending.</li>
1460</ul>
1461<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1462<p>This API can be called even if there is a pending JavaScript exception.</p>
1463<h5>napi_fatal_exception<span><a class="mark" href="#n_api_napi_fatal_exception" id="n_api_napi_fatal_exception">#</a></span></h5>
1464<div class="api_metadata">
1465<span>Added in: v9.10.0</span>
1466<span>N-API version: 3</span>
1467</div>
1468<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_fatal_exception</span><span class="hljs-params">(napi_env env, napi_value err)</span>;</code></pre>
1469<ul>
1470<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1471<li><code>[in] err</code>: The error that is passed to <code>'uncaughtException'</code>.</li>
1472</ul>
1473<p>Trigger an <code>'uncaughtException'</code> in JavaScript. Useful if an async
1474callback throws an exception with no way to recover.</p>
1475<h4>Fatal errors<span><a class="mark" href="#n_api_fatal_errors" id="n_api_fatal_errors">#</a></span></h4>
1476<p>In the event of an unrecoverable error in a native module, a fatal error can be
1477thrown to immediately terminate the process.</p>
1478<h5>napi_fatal_error<span><a class="mark" href="#n_api_napi_fatal_error" id="n_api_napi_fatal_error">#</a></span></h5>
1479<div class="api_metadata">
1480<span>Added in: v8.2.0</span>
1481<span>N-API version: 1</span>
1482</div>
1483<pre><code class="language-c">NAPI_NO_RETURN <span class="hljs-type">void</span> <span class="hljs-title function_">napi_fatal_error</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* location,
1484                                     <span class="hljs-type">size_t</span> location_len,
1485                                     <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* message,
1486                                     <span class="hljs-type">size_t</span> message_len)</span>;</code></pre>
1487<ul>
1488<li><code>[in] location</code>: Optional location at which the error occurred.</li>
1489<li><code>[in] location_len</code>: The length of the location in bytes, or
1490<code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li>
1491<li><code>[in] message</code>: The message associated with the error.</li>
1492<li><code>[in] message_len</code>: The length of the message in bytes, or <code>NAPI_AUTO_LENGTH</code>
1493if it is null-terminated.</li>
1494</ul>
1495<p>The function call does not return, the process will be terminated.</p>
1496<p>This API can be called even if there is a pending JavaScript exception.</p>
1497</section><section><h3>Object lifetime management<span><a class="mark" href="#n_api_object_lifetime_management" id="n_api_object_lifetime_management">#</a></span></h3>
1498<p>As Node-API calls are made, handles to objects in the heap for the underlying
1499VM may be returned as <code>napi_values</code>. These handles must hold the
1500objects 'live' until they are no longer required by the native code,
1501otherwise the objects could be collected before the native code was
1502finished using them.</p>
1503<p>As object handles are returned they are associated with a
1504'scope'. The lifespan for the default scope is tied to the lifespan
1505of the native method call. The result is that, by default, handles
1506remain valid and the objects associated with these handles will be
1507held live for the lifespan of the native method call.</p>
1508<p>In many cases, however, it is necessary that the handles remain valid for
1509either a shorter or longer lifespan than that of the native method.
1510The sections which follow describe the Node-API functions that can be used
1511to change the handle lifespan from the default.</p>
1512<h4>Making handle lifespan shorter than that of the native method<span><a class="mark" href="#n_api_making_handle_lifespan_shorter_than_that_of_the_native_method" id="n_api_making_handle_lifespan_shorter_than_that_of_the_native_method">#</a></span></h4>
1513<p>It is often necessary to make the lifespan of handles shorter than
1514the lifespan of a native method. For example, consider a native method
1515that has a loop which iterates through the elements in a large array:</p>
1516<pre><code class="language-c"><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &#x3C; <span class="hljs-number">1000000</span>; i++) {
1517  napi_value result;
1518  napi_status status = napi_get_element(env, object, i, &#x26;result);
1519  <span class="hljs-keyword">if</span> (status != napi_ok) {
1520    <span class="hljs-keyword">break</span>;
1521  }
1522  <span class="hljs-comment">// do something with element</span>
1523}</code></pre>
1524<p>This would result in a large number of handles being created, consuming
1525substantial resources. In addition, even though the native code could only
1526use the most recent handle, all of the associated objects would also be
1527kept alive since they all share the same scope.</p>
1528<p>To handle this case, Node-API provides the ability to establish a new 'scope' to
1529which newly created handles will be associated. Once those handles
1530are no longer required, the scope can be 'closed' and any handles associated
1531with the scope are invalidated. The methods available to open/close scopes are
1532<a href="#n_api_napi_open_handle_scope"><code>napi_open_handle_scope</code></a> and <a href="#n_api_napi_close_handle_scope"><code>napi_close_handle_scope</code></a>.</p>
1533<p>Node-API only supports a single nested hierarchy of scopes. There is only one
1534active scope at any time, and all new handles will be associated with that
1535scope while it is active. Scopes must be closed in the reverse order from
1536which they are opened. In addition, all scopes created within a native method
1537must be closed before returning from that method.</p>
1538<p>Taking the earlier example, adding calls to <a href="#n_api_napi_open_handle_scope"><code>napi_open_handle_scope</code></a> and
1539<a href="#n_api_napi_close_handle_scope"><code>napi_close_handle_scope</code></a> would ensure that at most a single handle
1540is valid throughout the execution of the loop:</p>
1541<pre><code class="language-c"><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &#x3C; <span class="hljs-number">1000000</span>; i++) {
1542  napi_handle_scope scope;
1543  napi_status status = napi_open_handle_scope(env, &#x26;scope);
1544  <span class="hljs-keyword">if</span> (status != napi_ok) {
1545    <span class="hljs-keyword">break</span>;
1546  }
1547  napi_value result;
1548  status = napi_get_element(env, object, i, &#x26;result);
1549  <span class="hljs-keyword">if</span> (status != napi_ok) {
1550    <span class="hljs-keyword">break</span>;
1551  }
1552  <span class="hljs-comment">// do something with element</span>
1553  status = napi_close_handle_scope(env, scope);
1554  <span class="hljs-keyword">if</span> (status != napi_ok) {
1555    <span class="hljs-keyword">break</span>;
1556  }
1557}</code></pre>
1558<p>When nesting scopes, there are cases where a handle from an
1559inner scope needs to live beyond the lifespan of that scope. Node-API supports
1560an 'escapable scope' in order to support this case. An escapable scope
1561allows one handle to be 'promoted' so that it 'escapes' the
1562current scope and the lifespan of the handle changes from the current
1563scope to that of the outer scope.</p>
1564<p>The methods available to open/close escapable scopes are
1565<a href="#n_api_napi_open_escapable_handle_scope"><code>napi_open_escapable_handle_scope</code></a> and
1566<a href="#n_api_napi_close_escapable_handle_scope"><code>napi_close_escapable_handle_scope</code></a>.</p>
1567<p>The request to promote a handle is made through <a href="#n_api_napi_escape_handle"><code>napi_escape_handle</code></a> which
1568can only be called once.</p>
1569<h5>napi_open_handle_scope<span><a class="mark" href="#n_api_napi_open_handle_scope" id="n_api_napi_open_handle_scope">#</a></span></h5>
1570<div class="api_metadata">
1571<span>Added in: v8.0.0</span>
1572<span>N-API version: 1</span>
1573</div>
1574<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_open_handle_scope</span><span class="hljs-params">(napi_env env,
1575                                               napi_handle_scope* result)</span>;</code></pre>
1576<ul>
1577<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1578<li><code>[out] result</code>: <code>napi_value</code> representing the new scope.</li>
1579</ul>
1580<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1581<p>This API opens a new scope.</p>
1582<h5>napi_close_handle_scope<span><a class="mark" href="#n_api_napi_close_handle_scope" id="n_api_napi_close_handle_scope">#</a></span></h5>
1583<div class="api_metadata">
1584<span>Added in: v8.0.0</span>
1585<span>N-API version: 1</span>
1586</div>
1587<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_close_handle_scope</span><span class="hljs-params">(napi_env env,
1588                                                napi_handle_scope scope)</span>;</code></pre>
1589<ul>
1590<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1591<li><code>[in] scope</code>: <code>napi_value</code> representing the scope to be closed.</li>
1592</ul>
1593<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1594<p>This API closes the scope passed in. Scopes must be closed in the
1595reverse order from which they were created.</p>
1596<p>This API can be called even if there is a pending JavaScript exception.</p>
1597<h5>napi_open_escapable_handle_scope<span><a class="mark" href="#n_api_napi_open_escapable_handle_scope" id="n_api_napi_open_escapable_handle_scope">#</a></span></h5>
1598<div class="api_metadata">
1599<span>Added in: v8.0.0</span>
1600<span>N-API version: 1</span>
1601</div>
1602<pre><code class="language-c">NAPI_EXTERN napi_status
1603    <span class="hljs-title function_">napi_open_escapable_handle_scope</span><span class="hljs-params">(napi_env env,
1604                                     napi_handle_scope* result)</span>;</code></pre>
1605<ul>
1606<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1607<li><code>[out] result</code>: <code>napi_value</code> representing the new scope.</li>
1608</ul>
1609<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1610<p>This API opens a new scope from which one object can be promoted
1611to the outer scope.</p>
1612<h5>napi_close_escapable_handle_scope<span><a class="mark" href="#n_api_napi_close_escapable_handle_scope" id="n_api_napi_close_escapable_handle_scope">#</a></span></h5>
1613<div class="api_metadata">
1614<span>Added in: v8.0.0</span>
1615<span>N-API version: 1</span>
1616</div>
1617<pre><code class="language-c">NAPI_EXTERN napi_status
1618    <span class="hljs-title function_">napi_close_escapable_handle_scope</span><span class="hljs-params">(napi_env env,
1619                                      napi_handle_scope scope)</span>;</code></pre>
1620<ul>
1621<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1622<li><code>[in] scope</code>: <code>napi_value</code> representing the scope to be closed.</li>
1623</ul>
1624<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1625<p>This API closes the scope passed in. Scopes must be closed in the
1626reverse order from which they were created.</p>
1627<p>This API can be called even if there is a pending JavaScript exception.</p>
1628<h5>napi_escape_handle<span><a class="mark" href="#n_api_napi_escape_handle" id="n_api_napi_escape_handle">#</a></span></h5>
1629<div class="api_metadata">
1630<span>Added in: v8.0.0</span>
1631<span>N-API version: 1</span>
1632</div>
1633<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_escape_handle</span><span class="hljs-params">(napi_env env,
1634                               napi_escapable_handle_scope scope,
1635                               napi_value escapee,
1636                               napi_value* result)</span>;</code></pre>
1637<ul>
1638<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1639<li><code>[in] scope</code>: <code>napi_value</code> representing the current scope.</li>
1640<li><code>[in] escapee</code>: <code>napi_value</code> representing the JavaScript <code>Object</code> to be
1641escaped.</li>
1642<li><code>[out] result</code>: <code>napi_value</code> representing the handle to the escaped <code>Object</code>
1643in the outer scope.</li>
1644</ul>
1645<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1646<p>This API promotes the handle to the JavaScript object so that it is valid
1647for the lifetime of the outer scope. It can only be called once per scope.
1648If it is called more than once an error will be returned.</p>
1649<p>This API can be called even if there is a pending JavaScript exception.</p>
1650<h4>References to objects with a lifespan longer than that of the native method<span><a class="mark" href="#n_api_references_to_objects_with_a_lifespan_longer_than_that_of_the_native_method" id="n_api_references_to_objects_with_a_lifespan_longer_than_that_of_the_native_method">#</a></span></h4>
1651<p>In some cases an addon will need to be able to create and reference objects
1652with a lifespan longer than that of a single native method invocation. For
1653example, to create a constructor and later use that constructor
1654in a request to creates instances, it must be possible to reference
1655the constructor object across many different instance creation requests. This
1656would not be possible with a normal handle returned as a <code>napi_value</code> as
1657described in the earlier section. The lifespan of a normal handle is
1658managed by scopes and all scopes must be closed before the end of a native
1659method.</p>
1660<p>Node-API provides methods to create persistent references to an object.
1661Each persistent reference has an associated count with a value of 0
1662or higher. The count determines if the reference will keep
1663the corresponding object live. References with a count of 0 do not
1664prevent the object from being collected and are often called 'weak'
1665references. Any count greater than 0 will prevent the object
1666from being collected.</p>
1667<p>References can be created with an initial reference count. The count can
1668then be modified through <a href="#n_api_napi_reference_ref"><code>napi_reference_ref</code></a> and
1669<a href="#n_api_napi_reference_unref"><code>napi_reference_unref</code></a>. If an object is collected while the count
1670for a reference is 0, all subsequent calls to
1671get the object associated with the reference <a href="#n_api_napi_get_reference_value"><code>napi_get_reference_value</code></a>
1672will return <code>NULL</code> for the returned <code>napi_value</code>. An attempt to call
1673<a href="#n_api_napi_reference_ref"><code>napi_reference_ref</code></a> for a reference whose object has been collected
1674results in an error.</p>
1675<p>References must be deleted once they are no longer required by the addon. When
1676a reference is deleted, it will no longer prevent the corresponding object from
1677being collected. Failure to delete a persistent reference results in
1678a 'memory leak' with both the native memory for the persistent reference and
1679the corresponding object on the heap being retained forever.</p>
1680<p>There can be multiple persistent references created which refer to the same
1681object, each of which will either keep the object live or not based on its
1682individual count.</p>
1683<h5>napi_create_reference<span><a class="mark" href="#n_api_napi_create_reference" id="n_api_napi_create_reference">#</a></span></h5>
1684<div class="api_metadata">
1685<span>Added in: v8.0.0</span>
1686<span>N-API version: 1</span>
1687</div>
1688<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_reference</span><span class="hljs-params">(napi_env env,
1689                                              napi_value value,
1690                                              <span class="hljs-type">uint32_t</span> initial_refcount,
1691                                              napi_ref* result)</span>;</code></pre>
1692<ul>
1693<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1694<li><code>[in] value</code>: <code>napi_value</code> representing the <code>Object</code> to which we want a
1695reference.</li>
1696<li><code>[in] initial_refcount</code>: Initial reference count for the new reference.</li>
1697<li><code>[out] result</code>: <code>napi_ref</code> pointing to the new reference.</li>
1698</ul>
1699<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1700<p>This API create a new reference with the specified reference count
1701to the <code>Object</code> passed in.</p>
1702<h5>napi_delete_reference<span><a class="mark" href="#n_api_napi_delete_reference" id="n_api_napi_delete_reference">#</a></span></h5>
1703<div class="api_metadata">
1704<span>Added in: v8.0.0</span>
1705<span>N-API version: 1</span>
1706</div>
1707<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_delete_reference</span><span class="hljs-params">(napi_env env, napi_ref ref)</span>;</code></pre>
1708<ul>
1709<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1710<li><code>[in] ref</code>: <code>napi_ref</code> to be deleted.</li>
1711</ul>
1712<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1713<p>This API deletes the reference passed in.</p>
1714<p>This API can be called even if there is a pending JavaScript exception.</p>
1715<h5>napi_reference_ref<span><a class="mark" href="#n_api_napi_reference_ref" id="n_api_napi_reference_ref">#</a></span></h5>
1716<div class="api_metadata">
1717<span>Added in: v8.0.0</span>
1718<span>N-API version: 1</span>
1719</div>
1720<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_reference_ref</span><span class="hljs-params">(napi_env env,
1721                                           napi_ref ref,
1722                                           <span class="hljs-type">uint32_t</span>* result)</span>;</code></pre>
1723<ul>
1724<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1725<li><code>[in] ref</code>: <code>napi_ref</code> for which the reference count will be incremented.</li>
1726<li><code>[out] result</code>: The new reference count.</li>
1727</ul>
1728<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1729<p>This API increments the reference count for the reference
1730passed in and returns the resulting reference count.</p>
1731<h5>napi_reference_unref<span><a class="mark" href="#n_api_napi_reference_unref" id="n_api_napi_reference_unref">#</a></span></h5>
1732<div class="api_metadata">
1733<span>Added in: v8.0.0</span>
1734<span>N-API version: 1</span>
1735</div>
1736<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_reference_unref</span><span class="hljs-params">(napi_env env,
1737                                             napi_ref ref,
1738                                             <span class="hljs-type">uint32_t</span>* result)</span>;</code></pre>
1739<ul>
1740<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1741<li><code>[in] ref</code>: <code>napi_ref</code> for which the reference count will be decremented.</li>
1742<li><code>[out] result</code>: The new reference count.</li>
1743</ul>
1744<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1745<p>This API decrements the reference count for the reference
1746passed in and returns the resulting reference count.</p>
1747<h5>napi_get_reference_value<span><a class="mark" href="#n_api_napi_get_reference_value" id="n_api_napi_get_reference_value">#</a></span></h5>
1748<div class="api_metadata">
1749<span>Added in: v8.0.0</span>
1750<span>N-API version: 1</span>
1751</div>
1752<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_reference_value</span><span class="hljs-params">(napi_env env,
1753                                                 napi_ref ref,
1754                                                 napi_value* result)</span>;</code></pre>
1755<p>the <code>napi_value passed</code> in or out of these methods is a handle to the
1756object to which the reference is related.</p>
1757<ul>
1758<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1759<li><code>[in] ref</code>: <code>napi_ref</code> for which we requesting the corresponding <code>Object</code>.</li>
1760<li><code>[out] result</code>: The <code>napi_value</code> for the <code>Object</code> referenced by the
1761<code>napi_ref</code>.</li>
1762</ul>
1763<p>Returns <code>napi_ok</code> if the API succeeded.</p>
1764<p>If still valid, this API returns the <code>napi_value</code> representing the
1765JavaScript <code>Object</code> associated with the <code>napi_ref</code>. Otherwise, result
1766will be <code>NULL</code>.</p>
1767<h4>Cleanup on exit of the current Node.js instance<span><a class="mark" href="#n_api_cleanup_on_exit_of_the_current_node_js_instance" id="n_api_cleanup_on_exit_of_the_current_node_js_instance">#</a></span></h4>
1768<p>While a Node.js process typically releases all its resources when exiting,
1769embedders of Node.js, or future Worker support, may require addons to register
1770clean-up hooks that will be run once the current Node.js instance exits.</p>
1771<p>Node-API provides functions for registering and un-registering such callbacks.
1772When those callbacks are run, all resources that are being held by the addon
1773should be freed up.</p>
1774<h5>napi_add_env_cleanup_hook<span><a class="mark" href="#n_api_napi_add_env_cleanup_hook" id="n_api_napi_add_env_cleanup_hook">#</a></span></h5>
1775<div class="api_metadata">
1776<span>Added in: v10.2.0</span>
1777<span>N-API version: 3</span>
1778</div>
1779<pre><code class="language-c">NODE_EXTERN napi_status <span class="hljs-title function_">napi_add_env_cleanup_hook</span><span class="hljs-params">(napi_env env,
1780                                                  <span class="hljs-type">void</span> (*fun)(<span class="hljs-type">void</span>* arg),
1781                                                  <span class="hljs-type">void</span>* arg)</span>;</code></pre>
1782<p>Registers <code>fun</code> as a function to be run with the <code>arg</code> parameter once the
1783current Node.js environment exits.</p>
1784<p>A function can safely be specified multiple times with different
1785<code>arg</code> values. In that case, it will be called multiple times as well.
1786Providing the same <code>fun</code> and <code>arg</code> values multiple times is not allowed
1787and will lead the process to abort.</p>
1788<p>The hooks will be called in reverse order, i.e. the most recently added one
1789will be called first.</p>
1790<p>Removing this hook can be done by using <a href="#n_api_napi_remove_env_cleanup_hook"><code>napi_remove_env_cleanup_hook</code></a>.
1791Typically, that happens when the resource for which this hook was added
1792is being torn down anyway.</p>
1793<p>For asynchronous cleanup, <a href="#n_api_napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a> is available.</p>
1794<h5>napi_remove_env_cleanup_hook<span><a class="mark" href="#n_api_napi_remove_env_cleanup_hook" id="n_api_napi_remove_env_cleanup_hook">#</a></span></h5>
1795<div class="api_metadata">
1796<span>Added in: v10.2.0</span>
1797<span>N-API version: 3</span>
1798</div>
1799<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remove_env_cleanup_hook</span><span class="hljs-params">(napi_env env,
1800                                                     <span class="hljs-type">void</span> (*fun)(<span class="hljs-type">void</span>* arg),
1801                                                     <span class="hljs-type">void</span>* arg)</span>;</code></pre>
1802<p>Unregisters <code>fun</code> as a function to be run with the <code>arg</code> parameter once the
1803current Node.js environment exits. Both the argument and the function value
1804need to be exact matches.</p>
1805<p>The function must have originally been registered
1806with <code>napi_add_env_cleanup_hook</code>, otherwise the process will abort.</p>
1807<h5>napi_add_async_cleanup_hook<span><a class="mark" href="#n_api_napi_add_async_cleanup_hook" id="n_api_napi_add_async_cleanup_hook">#</a></span></h5>
1808<div class="api_metadata">
1809<details class="changelog"><summary>History</summary>
1810<table>
1811<tbody><tr><th>Version</th><th>Changes</th></tr>
1812<tr><td>v14.10.0</td>
1813<td><p>Changed signature of the <code>hook</code> callback.</p></td></tr>
1814<tr><td>v14.8.0, v12.19.0</td>
1815<td><p><span>Added in: v14.8.0, v12.19.0</span></p></td></tr>
1816</tbody></table>
1817</details>
1818<span>N-API version: 8</span>
1819</div>
1820<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_add_async_cleanup_hook</span><span class="hljs-params">(
1821    napi_env env,
1822    napi_async_cleanup_hook hook,
1823    <span class="hljs-type">void</span>* arg,
1824    napi_async_cleanup_hook_handle* remove_handle)</span>;</code></pre>
1825<ul>
1826<li><code>[in] env</code>: The environment that the API is invoked under.</li>
1827<li><code>[in] hook</code>: The function pointer to call at environment teardown.</li>
1828<li><code>[in] arg</code>: The pointer to pass to <code>hook</code> when it gets called.</li>
1829<li><code>[out] remove_handle</code>: Optional handle that refers to the asynchronous cleanup
1830hook.</li>
1831</ul>
1832<p>Registers <code>hook</code>, which is a function of type <a href="#n_api_napi_async_cleanup_hook"><code>napi_async_cleanup_hook</code></a>, as
1833a function to be run with the <code>remove_handle</code> and <code>arg</code> parameters once the
1834current Node.js environment exits.</p>
1835<p>Unlike <a href="#n_api_napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a>, the hook is allowed to be asynchronous.</p>
1836<p>Otherwise, behavior generally matches that of <a href="#n_api_napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a>.</p>
1837<p>If <code>remove_handle</code> is not <code>NULL</code>, an opaque value will be stored in it
1838that must later be passed to <a href="#n_api_napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a>,
1839regardless of whether the hook has already been invoked.
1840Typically, that happens when the resource for which this hook was added
1841is being torn down anyway.</p>
1842<h5>napi_remove_async_cleanup_hook<span><a class="mark" href="#n_api_napi_remove_async_cleanup_hook" id="n_api_napi_remove_async_cleanup_hook">#</a></span></h5>
1843<div class="api_metadata">
1844<details class="changelog"><summary>History</summary>
1845<table>
1846<tbody><tr><th>Version</th><th>Changes</th></tr>
1847<tr><td>v14.10.0</td>
1848<td><p>Removed <code>env</code> parameter.</p></td></tr>
1849<tr><td>v14.8.0</td>
1850<td><p><span>Added in: v14.8.0</span></p></td></tr>
1851</tbody></table>
1852</details>
1853</div>
1854<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remove_async_cleanup_hook</span><span class="hljs-params">(
1855    napi_async_cleanup_hook_handle remove_handle)</span>;</code></pre>
1856<ul>
1857<li><code>[in] remove_handle</code>: The handle to an asynchronous cleanup hook that was
1858created with <a href="#n_api_napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>.</li>
1859</ul>
1860<p>Unregisters the cleanup hook corresponding to <code>remove_handle</code>. This will prevent
1861the hook from being executed, unless it has already started executing.
1862This must be called on any <code>napi_async_cleanup_hook_handle</code> value obtained
1863from <a href="#n_api_napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>.</p>
1864</section><section><h3>Module registration<span><a class="mark" href="#n_api_module_registration" id="n_api_module_registration">#</a></span></h3>
1865<p>Node-API modules are registered in a manner similar to other modules
1866except that instead of using the <code>NODE_MODULE</code> macro the following
1867is used:</p>
1868<pre><code class="language-c">NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)</code></pre>
1869<p>The next difference is the signature for the <code>Init</code> method. For a Node-API
1870module it is as follows:</p>
1871<pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span>;</code></pre>
1872<p>The return value from <code>Init</code> is treated as the <code>exports</code> object for the module.
1873The <code>Init</code> method is passed an empty object via the <code>exports</code> parameter as a
1874convenience. If <code>Init</code> returns <code>NULL</code>, the parameter passed as <code>exports</code> is
1875exported by the module. Node-API modules cannot modify the <code>module</code> object but
1876can specify anything as the <code>exports</code> property of the module.</p>
1877<p>To add the method <code>hello</code> as a function so that it can be called as a method
1878provided by the addon:</p>
1879<pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> {
1880  napi_status status;
1881  napi_property_descriptor desc = {
1882    <span class="hljs-string">"hello"</span>,
1883    <span class="hljs-literal">NULL</span>,
1884    Method,
1885    <span class="hljs-literal">NULL</span>,
1886    <span class="hljs-literal">NULL</span>,
1887    <span class="hljs-literal">NULL</span>,
1888    napi_writable | napi_enumerable | napi_configurable,
1889    <span class="hljs-literal">NULL</span>
1890  };
1891  status = napi_define_properties(env, exports, <span class="hljs-number">1</span>, &#x26;desc);
1892  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
1893  <span class="hljs-keyword">return</span> exports;
1894}</code></pre>
1895<p>To set a function to be returned by the <code>require()</code> for the addon:</p>
1896<pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> {
1897  napi_value method;
1898  napi_status status;
1899  status = napi_create_function(env, <span class="hljs-string">"exports"</span>, NAPI_AUTO_LENGTH, Method, <span class="hljs-literal">NULL</span>, &#x26;method);
1900  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
1901  <span class="hljs-keyword">return</span> method;
1902}</code></pre>
1903<p>To define a class so that new instances can be created (often used with
1904<a href="#n_api_object_wrap">Object wrap</a>):</p>
1905<pre><code class="language-c"><span class="hljs-comment">// <span class="hljs-doctag">NOTE:</span> partial example, not all referenced code is included</span>
1906napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> {
1907  napi_status status;
1908  napi_property_descriptor properties[] = {
1909    { <span class="hljs-string">"value"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, GetValue, SetValue, <span class="hljs-literal">NULL</span>, napi_writable | napi_configurable, <span class="hljs-literal">NULL</span> },
1910    DECLARE_NAPI_METHOD(<span class="hljs-string">"plusOne"</span>, PlusOne),
1911    DECLARE_NAPI_METHOD(<span class="hljs-string">"multiply"</span>, Multiply),
1912  };
1913
1914  napi_value cons;
1915  status =
1916      napi_define_class(env, <span class="hljs-string">"MyObject"</span>, New, <span class="hljs-literal">NULL</span>, <span class="hljs-number">3</span>, properties, &#x26;cons);
1917  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
1918
1919  status = napi_create_reference(env, cons, <span class="hljs-number">1</span>, &#x26;constructor);
1920  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
1921
1922  status = napi_set_named_property(env, exports, <span class="hljs-string">"MyObject"</span>, cons);
1923  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
1924
1925  <span class="hljs-keyword">return</span> exports;
1926}</code></pre>
1927<p>You can also use the <code>NAPI_MODULE_INIT</code> macro, which acts as a shorthand
1928for <code>NAPI_MODULE</code> and defining an <code>Init</code> function:</p>
1929<pre><code class="language-c">NAPI_MODULE_INIT() {
1930  napi_value answer;
1931  napi_status result;
1932
1933  status = napi_create_int64(env, <span class="hljs-number">42</span>, &#x26;answer);
1934  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
1935
1936  status = napi_set_named_property(env, exports, <span class="hljs-string">"answer"</span>, answer);
1937  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
1938
1939  <span class="hljs-keyword">return</span> exports;
1940}</code></pre>
1941<p>All Node-API addons are context-aware, meaning they may be loaded multiple
1942times. There are a few design considerations when declaring such a module.
1943The documentation on <a href="addons.html#addons_context_aware_addons">context-aware addons</a> provides more details.</p>
1944<p>The variables <code>env</code> and <code>exports</code> will be available inside the function body
1945following the macro invocation.</p>
1946<p>For more details on setting properties on objects, see the section on
1947<a href="#n_api_working_with_javascript_properties">Working with JavaScript properties</a>.</p>
1948<p>For more details on building addon modules in general, refer to the existing
1949API.</p>
1950</section><section><h3>Working with JavaScript values<span><a class="mark" href="#n_api_working_with_javascript_values" id="n_api_working_with_javascript_values">#</a></span></h3>
1951<p>Node-API exposes a set of APIs to create all types of JavaScript values.
1952Some of these types are documented under <a href="https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values">Section 6</a>
1953of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p>
1954<p>Fundamentally, these APIs are used to do one of the following:</p>
1955<ol>
1956<li>Create a new JavaScript object</li>
1957<li>Convert from a primitive C type to a Node-API value</li>
1958<li>Convert from Node-API value to a primitive C type</li>
1959<li>Get global instances including <code>undefined</code> and <code>null</code></li>
1960</ol>
1961<p>Node-API values are represented by the type <code>napi_value</code>.
1962Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>.
1963In some cases, the API does check the type of the <code>napi_value</code> up-front.
1964However, for better performance, it's better for the caller to make sure that
1965the <code>napi_value</code> in question is of the JavaScript type expected by the API.</p>
1966<h4>Enum types<span><a class="mark" href="#n_api_enum_types" id="n_api_enum_types">#</a></span></h4>
1967<h5>napi_key_collection_mode<span><a class="mark" href="#n_api_napi_key_collection_mode" id="n_api_napi_key_collection_mode">#</a></span></h5>
1968<div class="api_metadata">
1969<span>Added in: v13.7.0, v10.20.0</span>
1970<span>N-API version: 6</span>
1971</div>
1972<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
1973  napi_key_include_prototypes,
1974  napi_key_own_only
1975} napi_key_collection_mode;</code></pre>
1976<p>Describes the <code>Keys/Properties</code> filter enums:</p>
1977<p><code>napi_key_collection_mode</code> limits the range of collected properties.</p>
1978<p><code>napi_key_own_only</code> limits the collected properties to the given
1979object only. <code>napi_key_include_prototypes</code> will include all keys
1980of the objects's prototype chain as well.</p>
1981<h5>napi_key_filter<span><a class="mark" href="#n_api_napi_key_filter" id="n_api_napi_key_filter">#</a></span></h5>
1982<div class="api_metadata">
1983<span>Added in: v13.7.0, v10.20.0</span>
1984<span>N-API version: 6</span>
1985</div>
1986<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
1987  napi_key_all_properties = <span class="hljs-number">0</span>,
1988  napi_key_writable = <span class="hljs-number">1</span>,
1989  napi_key_enumerable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">1</span>,
1990  napi_key_configurable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">2</span>,
1991  napi_key_skip_strings = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">3</span>,
1992  napi_key_skip_symbols = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">4</span>
1993} napi_key_filter;</code></pre>
1994<p>Property filter bits. They can be or'ed to build a composite filter.</p>
1995<h5>napi_key_conversion<span><a class="mark" href="#n_api_napi_key_conversion" id="n_api_napi_key_conversion">#</a></span></h5>
1996<div class="api_metadata">
1997<span>Added in: v13.7.0, v10.20.0</span>
1998<span>N-API version: 6</span>
1999</div>
2000<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
2001  napi_key_keep_numbers,
2002  napi_key_numbers_to_strings
2003} napi_key_conversion;</code></pre>
2004<p><code>napi_key_numbers_to_strings</code> will convert integer indices to
2005strings. <code>napi_key_keep_numbers</code> will return numbers for integer
2006indices.</p>
2007<h5>napi_valuetype<span><a class="mark" href="#n_api_napi_valuetype" id="n_api_napi_valuetype">#</a></span></h5>
2008<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
2009  <span class="hljs-comment">// ES6 types (corresponds to typeof)</span>
2010  napi_undefined,
2011  napi_null,
2012  napi_boolean,
2013  napi_number,
2014  napi_string,
2015  napi_symbol,
2016  napi_object,
2017  napi_function,
2018  napi_external,
2019  napi_bigint,
2020} napi_valuetype;</code></pre>
2021<p>Describes the type of a <code>napi_value</code>. This generally corresponds to the types
2022described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types">Section 6.1</a> of the ECMAScript Language Specification.
2023In addition to types in that section, <code>napi_valuetype</code> can also represent
2024<code>Function</code>s and <code>Object</code>s with external data.</p>
2025<p>A JavaScript value of type <code>napi_external</code> appears in JavaScript as a plain
2026object such that no properties can be set on it, and no prototype.</p>
2027<h5>napi_typedarray_type<span><a class="mark" href="#n_api_napi_typedarray_type" id="n_api_napi_typedarray_type">#</a></span></h5>
2028<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
2029  napi_int8_array,
2030  napi_uint8_array,
2031  napi_uint8_clamped_array,
2032  napi_int16_array,
2033  napi_uint16_array,
2034  napi_int32_array,
2035  napi_uint32_array,
2036  napi_float32_array,
2037  napi_float64_array,
2038  napi_bigint64_array,
2039  napi_biguint64_array,
2040} napi_typedarray_type;</code></pre>
2041<p>This represents the underlying binary scalar datatype of the <code>TypedArray</code>.
2042Elements of this enum correspond to
2043<a href="https://tc39.github.io/ecma262/#sec-typedarray-objects">Section 22.2</a> of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p>
2044<h4>Object creation functions<span><a class="mark" href="#n_api_object_creation_functions" id="n_api_object_creation_functions">#</a></span></h4>
2045<h5>napi_create_array<span><a class="mark" href="#n_api_napi_create_array" id="n_api_napi_create_array">#</a></span></h5>
2046<div class="api_metadata">
2047<span>Added in: v8.0.0</span>
2048<span>N-API version: 1</span>
2049</div>
2050<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_array</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code></pre>
2051<ul>
2052<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
2053<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Array</code>.</li>
2054</ul>
2055<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2056<p>This API returns a Node-API value corresponding to a JavaScript <code>Array</code> type.
2057JavaScript arrays are described in
2058<a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Language Specification.</p>
2059<h5>napi_create_array_with_length<span><a class="mark" href="#n_api_napi_create_array_with_length" id="n_api_napi_create_array_with_length">#</a></span></h5>
2060<div class="api_metadata">
2061<span>Added in: v8.0.0</span>
2062<span>N-API version: 1</span>
2063</div>
2064<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_array_with_length</span><span class="hljs-params">(napi_env env,
2065                                          <span class="hljs-type">size_t</span> length,
2066                                          napi_value* result)</span></code></pre>
2067<ul>
2068<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2069<li><code>[in] length</code>: The initial length of the <code>Array</code>.</li>
2070<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Array</code>.</li>
2071</ul>
2072<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2073<p>This API returns a Node-API value corresponding to a JavaScript <code>Array</code> type.
2074The <code>Array</code>'s length property is set to the passed-in length parameter.
2075However, the underlying buffer is not guaranteed to be pre-allocated by the VM
2076when the array is created. That behavior is left to the underlying VM
2077implementation. If the buffer must be a contiguous block of memory that can be
2078directly read and/or written via C, consider using
2079<a href="#n_api_napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code></a>.</p>
2080<p>JavaScript arrays are described in
2081<a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Language Specification.</p>
2082<h5>napi_create_arraybuffer<span><a class="mark" href="#n_api_napi_create_arraybuffer" id="n_api_napi_create_arraybuffer">#</a></span></h5>
2083<div class="api_metadata">
2084<span>Added in: v8.0.0</span>
2085<span>N-API version: 1</span>
2086</div>
2087<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_arraybuffer</span><span class="hljs-params">(napi_env env,
2088                                    <span class="hljs-type">size_t</span> byte_length,
2089                                    <span class="hljs-type">void</span>** data,
2090                                    napi_value* result)</span></code></pre>
2091<ul>
2092<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2093<li><code>[in] length</code>: The length in bytes of the array buffer to create.</li>
2094<li><code>[out] data</code>: Pointer to the underlying byte buffer of the <code>ArrayBuffer</code>.</li>
2095<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>ArrayBuffer</code>.</li>
2096</ul>
2097<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2098<p>This API returns a Node-API value corresponding to a JavaScript <code>ArrayBuffer</code>.
2099<code>ArrayBuffer</code>s are used to represent fixed-length binary data buffers. They are
2100normally used as a backing-buffer for <code>TypedArray</code> objects.
2101The <code>ArrayBuffer</code> allocated will have an underlying byte buffer whose size is
2102determined by the <code>length</code> parameter that's passed in.
2103The underlying buffer is optionally returned back to the caller in case the
2104caller wants to directly manipulate the buffer. This buffer can only be
2105written to directly from native code. To write to this buffer from JavaScript,
2106a typed array or <code>DataView</code> object would need to be created.</p>
2107<p>JavaScript <code>ArrayBuffer</code> objects are described in
2108<a href="https://tc39.github.io/ecma262/#sec-arraybuffer-objects">Section 24.1</a> of the ECMAScript Language Specification.</p>
2109<h5>napi_create_buffer<span><a class="mark" href="#n_api_napi_create_buffer" id="n_api_napi_create_buffer">#</a></span></h5>
2110<div class="api_metadata">
2111<span>Added in: v8.0.0</span>
2112<span>N-API version: 1</span>
2113</div>
2114<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer</span><span class="hljs-params">(napi_env env,
2115                               <span class="hljs-type">size_t</span> size,
2116                               <span class="hljs-type">void</span>** data,
2117                               napi_value* result)</span></code></pre>
2118<ul>
2119<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2120<li><code>[in] size</code>: Size in bytes of the underlying buffer.</li>
2121<li><code>[out] data</code>: Raw pointer to the underlying buffer.</li>
2122<li><code>[out] result</code>: A <code>napi_value</code> representing a <code>node::Buffer</code>.</li>
2123</ul>
2124<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2125<p>This API allocates a <code>node::Buffer</code> object. While this is still a
2126fully-supported data structure, in most cases using a <code>TypedArray</code> will suffice.</p>
2127<h5>napi_create_buffer_copy<span><a class="mark" href="#n_api_napi_create_buffer_copy" id="n_api_napi_create_buffer_copy">#</a></span></h5>
2128<div class="api_metadata">
2129<span>Added in: v8.0.0</span>
2130<span>N-API version: 1</span>
2131</div>
2132<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer_copy</span><span class="hljs-params">(napi_env env,
2133                                    <span class="hljs-type">size_t</span> length,
2134                                    <span class="hljs-keyword">const</span> <span class="hljs-type">void</span>* data,
2135                                    <span class="hljs-type">void</span>** result_data,
2136                                    napi_value* result)</span></code></pre>
2137<ul>
2138<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2139<li><code>[in] size</code>: Size in bytes of the input buffer (should be the same as the size
2140of the new buffer).</li>
2141<li><code>[in] data</code>: Raw pointer to the underlying buffer to copy from.</li>
2142<li><code>[out] result_data</code>: Pointer to the new <code>Buffer</code>'s underlying data buffer.</li>
2143<li><code>[out] result</code>: A <code>napi_value</code> representing a <code>node::Buffer</code>.</li>
2144</ul>
2145<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2146<p>This API allocates a <code>node::Buffer</code> object and initializes it with data copied
2147from the passed-in buffer. While this is still a fully-supported data
2148structure, in most cases using a <code>TypedArray</code> will suffice.</p>
2149<h5>napi_create_date<span><a class="mark" href="#n_api_napi_create_date" id="n_api_napi_create_date">#</a></span></h5>
2150<div class="api_metadata">
2151<span>Added in: v11.11.0, v10.17.0</span>
2152<span>N-API version: 5</span>
2153</div>
2154<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_date</span><span class="hljs-params">(napi_env env,
2155                             <span class="hljs-type">double</span> time,
2156                             napi_value* result)</span>;</code></pre>
2157<ul>
2158<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2159<li><code>[in] time</code>: ECMAScript time value in milliseconds since 01 January, 1970 UTC.</li>
2160<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Date</code>.</li>
2161</ul>
2162<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2163<p>This API does not observe leap seconds; they are ignored, as
2164ECMAScript aligns with POSIX time specification.</p>
2165<p>This API allocates a JavaScript <code>Date</code> object.</p>
2166<p>JavaScript <code>Date</code> objects are described in
2167<a href="https://tc39.github.io/ecma262/#sec-date-objects">Section 20.3</a> of the ECMAScript Language Specification.</p>
2168<h5>napi_create_external<span><a class="mark" href="#n_api_napi_create_external" id="n_api_napi_create_external">#</a></span></h5>
2169<div class="api_metadata">
2170<span>Added in: v8.0.0</span>
2171<span>N-API version: 1</span>
2172</div>
2173<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external</span><span class="hljs-params">(napi_env env,
2174                                 <span class="hljs-type">void</span>* data,
2175                                 napi_finalize finalize_cb,
2176                                 <span class="hljs-type">void</span>* finalize_hint,
2177                                 napi_value* result)</span></code></pre>
2178<ul>
2179<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2180<li><code>[in] data</code>: Raw pointer to the external data.</li>
2181<li><code>[in] finalize_cb</code>: Optional callback to call when the external value is being
2182collected. <a href="#n_api_napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
2183<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
2184collection.</li>
2185<li><code>[out] result</code>: A <code>napi_value</code> representing an external value.</li>
2186</ul>
2187<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2188<p>This API allocates a JavaScript value with external data attached to it. This
2189is used to pass external data through JavaScript code, so it can be retrieved
2190later by native code using <a href="#n_api_napi_get_value_external"><code>napi_get_value_external</code></a>.</p>
2191<p>The API adds a <code>napi_finalize</code> callback which will be called when the JavaScript
2192object just created is ready for garbage collection. It is similar to
2193<code>napi_wrap()</code> except that:</p>
2194<ul>
2195<li>the native data cannot be retrieved later using <code>napi_unwrap()</code>,</li>
2196<li>nor can it be removed later using <code>napi_remove_wrap()</code>, and</li>
2197<li>the object created by the API can be used with <code>napi_wrap()</code>.</li>
2198</ul>
2199<p>The created value is not an object, and therefore does not support additional
2200properties. It is considered a distinct value type: calling <code>napi_typeof()</code> with
2201an external value yields <code>napi_external</code>.</p>
2202<h5>napi_create_external_arraybuffer<span><a class="mark" href="#n_api_napi_create_external_arraybuffer" id="n_api_napi_create_external_arraybuffer">#</a></span></h5>
2203<div class="api_metadata">
2204<span>Added in: v8.0.0</span>
2205<span>N-API version: 1</span>
2206</div>
2207<pre><code class="language-c">napi_status
2208<span class="hljs-title function_">napi_create_external_arraybuffer</span><span class="hljs-params">(napi_env env,
2209                                 <span class="hljs-type">void</span>* external_data,
2210                                 <span class="hljs-type">size_t</span> byte_length,
2211                                 napi_finalize finalize_cb,
2212                                 <span class="hljs-type">void</span>* finalize_hint,
2213                                 napi_value* result)</span></code></pre>
2214<ul>
2215<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2216<li><code>[in] external_data</code>: Pointer to the underlying byte buffer of the
2217<code>ArrayBuffer</code>.</li>
2218<li><code>[in] byte_length</code>: The length in bytes of the underlying buffer.</li>
2219<li><code>[in] finalize_cb</code>: Optional callback to call when the <code>ArrayBuffer</code> is being
2220collected. <a href="#n_api_napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
2221<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
2222collection.</li>
2223<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>ArrayBuffer</code>.</li>
2224</ul>
2225<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2226<p><strong>Some runtimes other than Node.js have dropped support for external buffers</strong>.
2227On runtimes other than Node.js this method may return
2228<code>napi_no_external_buffers_allowed</code> to indicate that external
2229buffers are not supported. One such runtime is Electron as
2230described in this issue
2231<a href="https://github.com/electron/electron/issues/35801">electron/issues/35801</a>.</p>
2232<p>In order to maintain broadest compatibility with all runtimes
2233you may define <code>NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED</code> in your addon before
2234includes for the node-api headers. Doing so will hide the 2 functions
2235that create external buffers. This will ensure a compilation error
2236occurs if you accidentally use one of these methods.</p>
2237<p>This API returns a Node-API value corresponding to a JavaScript <code>ArrayBuffer</code>.
2238The underlying byte buffer of the <code>ArrayBuffer</code> is externally allocated and
2239managed. The caller must ensure that the byte buffer remains valid until the
2240finalize callback is called.</p>
2241<p>The API adds a <code>napi_finalize</code> callback which will be called when the JavaScript
2242object just created is ready for garbage collection. It is similar to
2243<code>napi_wrap()</code> except that:</p>
2244<ul>
2245<li>the native data cannot be retrieved later using <code>napi_unwrap()</code>,</li>
2246<li>nor can it be removed later using <code>napi_remove_wrap()</code>, and</li>
2247<li>the object created by the API can be used with <code>napi_wrap()</code>.</li>
2248</ul>
2249<p>JavaScript <code>ArrayBuffer</code>s are described in
2250<a href="https://tc39.github.io/ecma262/#sec-arraybuffer-objects">Section 24.1</a> of the ECMAScript Language Specification.</p>
2251<h5>napi_create_external_buffer<span><a class="mark" href="#n_api_napi_create_external_buffer" id="n_api_napi_create_external_buffer">#</a></span></h5>
2252<div class="api_metadata">
2253<span>Added in: v8.0.0</span>
2254<span>N-API version: 1</span>
2255</div>
2256<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external_buffer</span><span class="hljs-params">(napi_env env,
2257                                        <span class="hljs-type">size_t</span> length,
2258                                        <span class="hljs-type">void</span>* data,
2259                                        napi_finalize finalize_cb,
2260                                        <span class="hljs-type">void</span>* finalize_hint,
2261                                        napi_value* result)</span></code></pre>
2262<ul>
2263<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2264<li><code>[in] length</code>: Size in bytes of the input buffer (should be the same as the
2265size of the new buffer).</li>
2266<li><code>[in] data</code>: Raw pointer to the underlying buffer to expose to JavaScript.</li>
2267<li><code>[in] finalize_cb</code>: Optional callback to call when the <code>ArrayBuffer</code> is being
2268collected. <a href="#n_api_napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
2269<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during
2270collection.</li>
2271<li><code>[out] result</code>: A <code>napi_value</code> representing a <code>node::Buffer</code>.</li>
2272</ul>
2273<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2274<p><strong>Some runtimes other than Node.js have dropped support for external buffers</strong>.
2275On runtimes other than Node.js this method may return
2276<code>napi_no_external_buffers_allowed</code> to indicate that external
2277buffers are not supported. One such runtime is Electron as
2278described in this issue
2279<a href="https://github.com/electron/electron/issues/35801">electron/issues/35801</a>.</p>
2280<p>In order to maintain broadest compatibility with all runtimes
2281you may define <code>NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED</code> in your addon before
2282includes for the node-api headers. Doing so will hide the 2 functions
2283that create external buffers. This will ensure a compilation error
2284occurs if you accidentally use one of these methods.</p>
2285<p>This API allocates a <code>node::Buffer</code> object and initializes it with data
2286backed by the passed in buffer. While this is still a fully-supported data
2287structure, in most cases using a <code>TypedArray</code> will suffice.</p>
2288<p>The API adds a <code>napi_finalize</code> callback which will be called when the JavaScript
2289object just created is ready for garbage collection. It is similar to
2290<code>napi_wrap()</code> except that:</p>
2291<ul>
2292<li>the native data cannot be retrieved later using <code>napi_unwrap()</code>,</li>
2293<li>nor can it be removed later using <code>napi_remove_wrap()</code>, and</li>
2294<li>the object created by the API can be used with <code>napi_wrap()</code>.</li>
2295</ul>
2296<p>For Node.js >=4 <code>Buffers</code> are <code>Uint8Array</code>s.</p>
2297<h5>napi_create_object<span><a class="mark" href="#n_api_napi_create_object" id="n_api_napi_create_object">#</a></span></h5>
2298<div class="api_metadata">
2299<span>Added in: v8.0.0</span>
2300<span>N-API version: 1</span>
2301</div>
2302<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_object</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code></pre>
2303<ul>
2304<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2305<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Object</code>.</li>
2306</ul>
2307<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2308<p>This API allocates a default JavaScript <code>Object</code>.
2309It is the equivalent of doing <code>new Object()</code> in JavaScript.</p>
2310<p>The JavaScript <code>Object</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-object-type">Section 6.1.7</a> of the
2311ECMAScript Language Specification.</p>
2312<h5>napi_create_symbol<span><a class="mark" href="#n_api_napi_create_symbol" id="n_api_napi_create_symbol">#</a></span></h5>
2313<div class="api_metadata">
2314<span>Added in: v8.0.0</span>
2315<span>N-API version: 1</span>
2316</div>
2317<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_symbol</span><span class="hljs-params">(napi_env env,
2318                               napi_value description,
2319                               napi_value* result)</span></code></pre>
2320<ul>
2321<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2322<li><code>[in] description</code>: Optional <code>napi_value</code> which refers to a JavaScript
2323<code>string</code> to be set as the description for the symbol.</li>
2324<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>symbol</code>.</li>
2325</ul>
2326<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2327<p>This API creates a JavaScript <code>symbol</code> value from a UTF8-encoded C string.</p>
2328<p>The JavaScript <code>symbol</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-symbol-objects">Section 19.4</a>
2329of the ECMAScript Language Specification.</p>
2330<h5>napi_create_typedarray<span><a class="mark" href="#n_api_napi_create_typedarray" id="n_api_napi_create_typedarray">#</a></span></h5>
2331<div class="api_metadata">
2332<span>Added in: v8.0.0</span>
2333<span>N-API version: 1</span>
2334</div>
2335<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_typedarray</span><span class="hljs-params">(napi_env env,
2336                                   napi_typedarray_type type,
2337                                   <span class="hljs-type">size_t</span> length,
2338                                   napi_value arraybuffer,
2339                                   <span class="hljs-type">size_t</span> byte_offset,
2340                                   napi_value* result)</span></code></pre>
2341<ul>
2342<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2343<li><code>[in] type</code>: Scalar datatype of the elements within the <code>TypedArray</code>.</li>
2344<li><code>[in] length</code>: Number of elements in the <code>TypedArray</code>.</li>
2345<li><code>[in] arraybuffer</code>: <code>ArrayBuffer</code> underlying the typed array.</li>
2346<li><code>[in] byte_offset</code>: The byte offset within the <code>ArrayBuffer</code> from which to
2347start projecting the <code>TypedArray</code>.</li>
2348<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>TypedArray</code>.</li>
2349</ul>
2350<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2351<p>This API creates a JavaScript <code>TypedArray</code> object over an existing
2352<code>ArrayBuffer</code>. <code>TypedArray</code> objects provide an array-like view over an
2353underlying data buffer where each element has the same underlying binary scalar
2354datatype.</p>
2355<p>It's required that <code>(length * size_of_element) + byte_offset</code> should
2356be &#x3C;= the size in bytes of the array passed in. If not, a <code>RangeError</code> exception
2357is raised.</p>
2358<p>JavaScript <code>TypedArray</code> objects are described in
2359<a href="https://tc39.github.io/ecma262/#sec-typedarray-objects">Section 22.2</a> of the ECMAScript Language Specification.</p>
2360<h5>napi_create_dataview<span><a class="mark" href="#n_api_napi_create_dataview" id="n_api_napi_create_dataview">#</a></span></h5>
2361<div class="api_metadata">
2362<span>Added in: v8.3.0</span>
2363<span>N-API version: 1</span>
2364</div>
2365<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_dataview</span><span class="hljs-params">(napi_env env,
2366                                 <span class="hljs-type">size_t</span> byte_length,
2367                                 napi_value arraybuffer,
2368                                 <span class="hljs-type">size_t</span> byte_offset,
2369                                 napi_value* result)</span></code></pre>
2370<ul>
2371<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2372<li><code>[in] length</code>: Number of elements in the <code>DataView</code>.</li>
2373<li><code>[in] arraybuffer</code>: <code>ArrayBuffer</code> underlying the <code>DataView</code>.</li>
2374<li><code>[in] byte_offset</code>: The byte offset within the <code>ArrayBuffer</code> from which to
2375start projecting the <code>DataView</code>.</li>
2376<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>DataView</code>.</li>
2377</ul>
2378<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2379<p>This API creates a JavaScript <code>DataView</code> object over an existing <code>ArrayBuffer</code>.
2380<code>DataView</code> objects provide an array-like view over an underlying data buffer,
2381but one which allows items of different size and type in the <code>ArrayBuffer</code>.</p>
2382<p>It is required that <code>byte_length + byte_offset</code> is less than or equal to the
2383size in bytes of the array passed in. If not, a <code>RangeError</code> exception is
2384raised.</p>
2385<p>JavaScript <code>DataView</code> objects are described in
2386<a href="https://tc39.github.io/ecma262/#sec-dataview-objects">Section 24.3</a> of the ECMAScript Language Specification.</p>
2387<h4>Functions to convert from C types to Node-API<span><a class="mark" href="#n_api_functions_to_convert_from_c_types_to_node_api" id="n_api_functions_to_convert_from_c_types_to_node_api">#</a></span></h4>
2388<h5>napi_create_int32<span><a class="mark" href="#n_api_napi_create_int32" id="n_api_napi_create_int32">#</a></span></h5>
2389<div class="api_metadata">
2390<span>Added in: v8.4.0</span>
2391<span>N-API version: 1</span>
2392</div>
2393<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_int32</span><span class="hljs-params">(napi_env env, <span class="hljs-type">int32_t</span> value, napi_value* result)</span></code></pre>
2394<ul>
2395<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2396<li><code>[in] value</code>: Integer value to be represented in JavaScript.</li>
2397<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li>
2398</ul>
2399<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2400<p>This API is used to convert from the C <code>int32_t</code> type to the JavaScript
2401<code>number</code> type.</p>
2402<p>The JavaScript <code>number</code> type is described in
2403<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a> of the ECMAScript Language Specification.</p>
2404<h5>napi_create_uint32<span><a class="mark" href="#n_api_napi_create_uint32" id="n_api_napi_create_uint32">#</a></span></h5>
2405<div class="api_metadata">
2406<span>Added in: v8.4.0</span>
2407<span>N-API version: 1</span>
2408</div>
2409<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_uint32</span><span class="hljs-params">(napi_env env, <span class="hljs-type">uint32_t</span> value, napi_value* result)</span></code></pre>
2410<ul>
2411<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2412<li><code>[in] value</code>: Unsigned integer value to be represented in JavaScript.</li>
2413<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li>
2414</ul>
2415<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2416<p>This API is used to convert from the C <code>uint32_t</code> type to the JavaScript
2417<code>number</code> type.</p>
2418<p>The JavaScript <code>number</code> type is described in
2419<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a> of the ECMAScript Language Specification.</p>
2420<h5>napi_create_int64<span><a class="mark" href="#n_api_napi_create_int64" id="n_api_napi_create_int64">#</a></span></h5>
2421<div class="api_metadata">
2422<span>Added in: v8.4.0</span>
2423<span>N-API version: 1</span>
2424</div>
2425<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_int64</span><span class="hljs-params">(napi_env env, <span class="hljs-type">int64_t</span> value, napi_value* result)</span></code></pre>
2426<ul>
2427<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2428<li><code>[in] value</code>: Integer value to be represented in JavaScript.</li>
2429<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li>
2430</ul>
2431<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2432<p>This API is used to convert from the C <code>int64_t</code> type to the JavaScript
2433<code>number</code> type.</p>
2434<p>The JavaScript <code>number</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a>
2435of the ECMAScript Language Specification. Note the complete range of <code>int64_t</code>
2436cannot be represented with full precision in JavaScript. Integer values
2437outside the range of <a href="https://tc39.github.io/ecma262/#sec-number.min_safe_integer"><code>Number.MIN_SAFE_INTEGER</code></a> <code>-(2**53 - 1)</code> -
2438<a href="https://tc39.github.io/ecma262/#sec-number.max_safe_integer"><code>Number.MAX_SAFE_INTEGER</code></a> <code>(2**53 - 1)</code> will lose precision.</p>
2439<h5>napi_create_double<span><a class="mark" href="#n_api_napi_create_double" id="n_api_napi_create_double">#</a></span></h5>
2440<div class="api_metadata">
2441<span>Added in: v8.4.0</span>
2442<span>N-API version: 1</span>
2443</div>
2444<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_double</span><span class="hljs-params">(napi_env env, <span class="hljs-type">double</span> value, napi_value* result)</span></code></pre>
2445<ul>
2446<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2447<li><code>[in] value</code>: Double-precision value to be represented in JavaScript.</li>
2448<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li>
2449</ul>
2450<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2451<p>This API is used to convert from the C <code>double</code> type to the JavaScript
2452<code>number</code> type.</p>
2453<p>The JavaScript <code>number</code> type is described in
2454<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a> of the ECMAScript Language Specification.</p>
2455<h5>napi_create_bigint_int64<span><a class="mark" href="#n_api_napi_create_bigint_int64" id="n_api_napi_create_bigint_int64">#</a></span></h5>
2456<div class="api_metadata">
2457<span>Added in: v10.7.0</span>
2458<span>N-API version: 6</span>
2459</div>
2460<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_int64</span><span class="hljs-params">(napi_env env,
2461                                     <span class="hljs-type">int64_t</span> value,
2462                                     napi_value* result)</span>;</code></pre>
2463<ul>
2464<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2465<li><code>[in] value</code>: Integer value to be represented in JavaScript.</li>
2466<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>BigInt</code>.</li>
2467</ul>
2468<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2469<p>This API converts the C <code>int64_t</code> type to the JavaScript <code>BigInt</code> type.</p>
2470<h5>napi_create_bigint_uint64<span><a class="mark" href="#n_api_napi_create_bigint_uint64" id="n_api_napi_create_bigint_uint64">#</a></span></h5>
2471<div class="api_metadata">
2472<span>Added in: v10.7.0</span>
2473<span>N-API version: 6</span>
2474</div>
2475<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_uint64</span><span class="hljs-params">(napi_env env,
2476                                      <span class="hljs-type">uint64_t</span> value,
2477                                      napi_value* result)</span>;</code></pre>
2478<ul>
2479<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2480<li><code>[in] value</code>: Unsigned integer value to be represented in JavaScript.</li>
2481<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>BigInt</code>.</li>
2482</ul>
2483<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2484<p>This API converts the C <code>uint64_t</code> type to the JavaScript <code>BigInt</code> type.</p>
2485<h5>napi_create_bigint_words<span><a class="mark" href="#n_api_napi_create_bigint_words" id="n_api_napi_create_bigint_words">#</a></span></h5>
2486<div class="api_metadata">
2487<span>Added in: v10.7.0</span>
2488<span>N-API version: 6</span>
2489</div>
2490<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_words</span><span class="hljs-params">(napi_env env,
2491                                     <span class="hljs-type">int</span> sign_bit,
2492                                     <span class="hljs-type">size_t</span> word_count,
2493                                     <span class="hljs-keyword">const</span> <span class="hljs-type">uint64_t</span>* words,
2494                                     napi_value* result)</span>;</code></pre>
2495<ul>
2496<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2497<li><code>[in] sign_bit</code>: Determines if the resulting <code>BigInt</code> will be positive or
2498negative.</li>
2499<li><code>[in] word_count</code>: The length of the <code>words</code> array.</li>
2500<li><code>[in] words</code>: An array of <code>uint64_t</code> little-endian 64-bit words.</li>
2501<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>BigInt</code>.</li>
2502</ul>
2503<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2504<p>This API converts an array of unsigned 64-bit words into a single <code>BigInt</code>
2505value.</p>
2506<p>The resulting <code>BigInt</code> is calculated as: (–1)<sup><code>sign_bit</code></sup> (<code>words[0]</code>
2507× (2<sup>64</sup>)<sup>0</sup> + <code>words[1]</code> × (2<sup>64</sup>)<sup>1</sup> + …)</p>
2508<h5>napi_create_string_latin1<span><a class="mark" href="#n_api_napi_create_string_latin1" id="n_api_napi_create_string_latin1">#</a></span></h5>
2509<div class="api_metadata">
2510<span>Added in: v8.0.0</span>
2511<span>N-API version: 1</span>
2512</div>
2513<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_latin1</span><span class="hljs-params">(napi_env env,
2514                                      <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* str,
2515                                      <span class="hljs-type">size_t</span> length,
2516                                      napi_value* result)</span>;</code></pre>
2517<ul>
2518<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2519<li><code>[in] str</code>: Character buffer representing an ISO-8859-1-encoded string.</li>
2520<li><code>[in] length</code>: The length of the string in bytes, or <code>NAPI_AUTO_LENGTH</code> if it
2521is null-terminated.</li>
2522<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li>
2523</ul>
2524<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2525<p>This API creates a JavaScript <code>string</code> value from an ISO-8859-1-encoded C
2526string. The native string is copied.</p>
2527<p>The JavaScript <code>string</code> type is described in
2528<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p>
2529<h5>napi_create_string_utf16<span><a class="mark" href="#n_api_napi_create_string_utf16" id="n_api_napi_create_string_utf16">#</a></span></h5>
2530<div class="api_metadata">
2531<span>Added in: v8.0.0</span>
2532<span>N-API version: 1</span>
2533</div>
2534<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf16</span><span class="hljs-params">(napi_env env,
2535                                     <span class="hljs-keyword">const</span> <span class="hljs-type">char16_t</span>* str,
2536                                     <span class="hljs-type">size_t</span> length,
2537                                     napi_value* result)</span></code></pre>
2538<ul>
2539<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2540<li><code>[in] str</code>: Character buffer representing a UTF16-LE-encoded string.</li>
2541<li><code>[in] length</code>: The length of the string in two-byte code units, or
2542<code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li>
2543<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li>
2544</ul>
2545<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2546<p>This API creates a JavaScript <code>string</code> value from a UTF16-LE-encoded C string.
2547The native string is copied.</p>
2548<p>The JavaScript <code>string</code> type is described in
2549<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p>
2550<h5>napi_create_string_utf8<span><a class="mark" href="#n_api_napi_create_string_utf8" id="n_api_napi_create_string_utf8">#</a></span></h5>
2551<div class="api_metadata">
2552<span>Added in: v8.0.0</span>
2553<span>N-API version: 1</span>
2554</div>
2555<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf8</span><span class="hljs-params">(napi_env env,
2556                                    <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* str,
2557                                    <span class="hljs-type">size_t</span> length,
2558                                    napi_value* result)</span></code></pre>
2559<ul>
2560<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2561<li><code>[in] str</code>: Character buffer representing a UTF8-encoded string.</li>
2562<li><code>[in] length</code>: The length of the string in bytes, or <code>NAPI_AUTO_LENGTH</code> if it
2563is null-terminated.</li>
2564<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li>
2565</ul>
2566<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2567<p>This API creates a JavaScript <code>string</code> value from a UTF8-encoded C string.
2568The native string is copied.</p>
2569<p>The JavaScript <code>string</code> type is described in
2570<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p>
2571<h4>Functions to convert from Node-API to C types<span><a class="mark" href="#n_api_functions_to_convert_from_node_api_to_c_types" id="n_api_functions_to_convert_from_node_api_to_c_types">#</a></span></h4>
2572<h5>napi_get_array_length<span><a class="mark" href="#n_api_napi_get_array_length" id="n_api_napi_get_array_length">#</a></span></h5>
2573<div class="api_metadata">
2574<span>Added in: v8.0.0</span>
2575<span>N-API version: 1</span>
2576</div>
2577<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_array_length</span><span class="hljs-params">(napi_env env,
2578                                  napi_value value,
2579                                  <span class="hljs-type">uint32_t</span>* result)</span></code></pre>
2580<ul>
2581<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2582<li><code>[in] value</code>: <code>napi_value</code> representing the JavaScript <code>Array</code> whose length is
2583being queried.</li>
2584<li><code>[out] result</code>: <code>uint32</code> representing length of the array.</li>
2585</ul>
2586<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2587<p>This API returns the length of an array.</p>
2588<p><code>Array</code> length is described in <a href="https://tc39.github.io/ecma262/#sec-properties-of-array-instances-length">Section 22.1.4.1</a> of the ECMAScript Language
2589Specification.</p>
2590<h5>napi_get_arraybuffer_info<span><a class="mark" href="#n_api_napi_get_arraybuffer_info" id="n_api_napi_get_arraybuffer_info">#</a></span></h5>
2591<div class="api_metadata">
2592<span>Added in: v8.0.0</span>
2593<span>N-API version: 1</span>
2594</div>
2595<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_arraybuffer_info</span><span class="hljs-params">(napi_env env,
2596                                      napi_value arraybuffer,
2597                                      <span class="hljs-type">void</span>** data,
2598                                      <span class="hljs-type">size_t</span>* byte_length)</span></code></pre>
2599<ul>
2600<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2601<li><code>[in] arraybuffer</code>: <code>napi_value</code> representing the <code>ArrayBuffer</code> being queried.</li>
2602<li><code>[out] data</code>: The underlying data buffer of the <code>ArrayBuffer</code>. If byte_length
2603is <code>0</code>, this may be <code>NULL</code> or any other pointer value.</li>
2604<li><code>[out] byte_length</code>: Length in bytes of the underlying data buffer.</li>
2605</ul>
2606<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2607<p>This API is used to retrieve the underlying data buffer of an <code>ArrayBuffer</code> and
2608its length.</p>
2609<p><em>WARNING</em>: Use caution while using this API. The lifetime of the underlying data
2610buffer is managed by the <code>ArrayBuffer</code> even after it's returned. A
2611possible safe way to use this API is in conjunction with
2612<a href="#n_api_napi_create_reference"><code>napi_create_reference</code></a>, which can be used to guarantee control over the
2613lifetime of the <code>ArrayBuffer</code>. It's also safe to use the returned data buffer
2614within the same callback as long as there are no calls to other APIs that might
2615trigger a GC.</p>
2616<h5>napi_get_buffer_info<span><a class="mark" href="#n_api_napi_get_buffer_info" id="n_api_napi_get_buffer_info">#</a></span></h5>
2617<div class="api_metadata">
2618<span>Added in: v8.0.0</span>
2619<span>N-API version: 1</span>
2620</div>
2621<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_buffer_info</span><span class="hljs-params">(napi_env env,
2622                                 napi_value value,
2623                                 <span class="hljs-type">void</span>** data,
2624                                 <span class="hljs-type">size_t</span>* length)</span></code></pre>
2625<ul>
2626<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2627<li><code>[in] value</code>: <code>napi_value</code> representing the <code>node::Buffer</code> being queried.</li>
2628<li><code>[out] data</code>: The underlying data buffer of the <code>node::Buffer</code>.
2629If length is <code>0</code>, this may be <code>NULL</code> or any other pointer value.</li>
2630<li><code>[out] length</code>: Length in bytes of the underlying data buffer.</li>
2631</ul>
2632<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2633<p>This API is used to retrieve the underlying data buffer of a <code>node::Buffer</code>
2634and it's length.</p>
2635<p><em>Warning</em>: Use caution while using this API since the underlying data buffer's
2636lifetime is not guaranteed if it's managed by the VM.</p>
2637<h5>napi_get_prototype<span><a class="mark" href="#n_api_napi_get_prototype" id="n_api_napi_get_prototype">#</a></span></h5>
2638<div class="api_metadata">
2639<span>Added in: v8.0.0</span>
2640<span>N-API version: 1</span>
2641</div>
2642<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_prototype</span><span class="hljs-params">(napi_env env,
2643                               napi_value object,
2644                               napi_value* result)</span></code></pre>
2645<ul>
2646<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2647<li><code>[in] object</code>: <code>napi_value</code> representing JavaScript <code>Object</code> whose prototype
2648to return. This returns the equivalent of <code>Object.getPrototypeOf</code> (which is
2649not the same as the function's <code>prototype</code> property).</li>
2650<li><code>[out] result</code>: <code>napi_value</code> representing prototype of the given object.</li>
2651</ul>
2652<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2653<h5>napi_get_typedarray_info<span><a class="mark" href="#n_api_napi_get_typedarray_info" id="n_api_napi_get_typedarray_info">#</a></span></h5>
2654<div class="api_metadata">
2655<span>Added in: v8.0.0</span>
2656<span>N-API version: 1</span>
2657</div>
2658<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_typedarray_info</span><span class="hljs-params">(napi_env env,
2659                                     napi_value typedarray,
2660                                     napi_typedarray_type* type,
2661                                     <span class="hljs-type">size_t</span>* length,
2662                                     <span class="hljs-type">void</span>** data,
2663                                     napi_value* arraybuffer,
2664                                     <span class="hljs-type">size_t</span>* byte_offset)</span></code></pre>
2665<ul>
2666<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2667<li><code>[in] typedarray</code>: <code>napi_value</code> representing the <code>TypedArray</code> whose
2668properties to query.</li>
2669<li><code>[out] type</code>: Scalar datatype of the elements within the <code>TypedArray</code>.</li>
2670<li><code>[out] length</code>: The number of elements in the <code>TypedArray</code>.</li>
2671<li><code>[out] data</code>: The data buffer underlying the <code>TypedArray</code> adjusted by
2672the <code>byte_offset</code> value so that it points to the first element in the
2673<code>TypedArray</code>. If the length of the array is <code>0</code>, this may be <code>NULL</code> or
2674any other pointer value.</li>
2675<li><code>[out] arraybuffer</code>: The <code>ArrayBuffer</code> underlying the <code>TypedArray</code>.</li>
2676<li><code>[out] byte_offset</code>: The byte offset within the underlying native array
2677at which the first element of the arrays is located. The value for the data
2678parameter has already been adjusted so that data points to the first element
2679in the array. Therefore, the first byte of the native array would be at
2680<code>data - byte_offset</code>.</li>
2681</ul>
2682<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2683<p>This API returns various properties of a typed array.</p>
2684<p><em>Warning</em>: Use caution while using this API since the underlying data buffer
2685is managed by the VM.</p>
2686<h5>napi_get_dataview_info<span><a class="mark" href="#n_api_napi_get_dataview_info" id="n_api_napi_get_dataview_info">#</a></span></h5>
2687<div class="api_metadata">
2688<span>Added in: v8.3.0</span>
2689<span>N-API version: 1</span>
2690</div>
2691<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_dataview_info</span><span class="hljs-params">(napi_env env,
2692                                   napi_value dataview,
2693                                   <span class="hljs-type">size_t</span>* byte_length,
2694                                   <span class="hljs-type">void</span>** data,
2695                                   napi_value* arraybuffer,
2696                                   <span class="hljs-type">size_t</span>* byte_offset)</span></code></pre>
2697<ul>
2698<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2699<li><code>[in] dataview</code>: <code>napi_value</code> representing the <code>DataView</code> whose
2700properties to query.</li>
2701<li><code>[out] byte_length</code>: Number of bytes in the <code>DataView</code>.</li>
2702<li><code>[out] data</code>: The data buffer underlying the <code>DataView</code>.
2703If byte_length is <code>0</code>, this may be <code>NULL</code> or any other pointer value.</li>
2704<li><code>[out] arraybuffer</code>: <code>ArrayBuffer</code> underlying the <code>DataView</code>.</li>
2705<li><code>[out] byte_offset</code>: The byte offset within the data buffer from which
2706to start projecting the <code>DataView</code>.</li>
2707</ul>
2708<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2709<p>This API returns various properties of a <code>DataView</code>.</p>
2710<h5>napi_get_date_value<span><a class="mark" href="#n_api_napi_get_date_value" id="n_api_napi_get_date_value">#</a></span></h5>
2711<div class="api_metadata">
2712<span>Added in: v11.11.0, v10.17.0</span>
2713<span>N-API version: 5</span>
2714</div>
2715<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_date_value</span><span class="hljs-params">(napi_env env,
2716                                napi_value value,
2717                                <span class="hljs-type">double</span>* result)</span></code></pre>
2718<ul>
2719<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2720<li><code>[in] value</code>: <code>napi_value</code> representing a JavaScript <code>Date</code>.</li>
2721<li><code>[out] result</code>: Time value as a <code>double</code> represented as milliseconds since
2722midnight at the beginning of 01 January, 1970 UTC.</li>
2723</ul>
2724<p>This API does not observe leap seconds; they are ignored, as
2725ECMAScript aligns with POSIX time specification.</p>
2726<p>Returns <code>napi_ok</code> if the API succeeded. If a non-date <code>napi_value</code> is passed
2727in it returns <code>napi_date_expected</code>.</p>
2728<p>This API returns the C double primitive of time value for the given JavaScript
2729<code>Date</code>.</p>
2730<h5>napi_get_value_bool<span><a class="mark" href="#n_api_napi_get_value_bool" id="n_api_napi_get_value_bool">#</a></span></h5>
2731<div class="api_metadata">
2732<span>Added in: v8.0.0</span>
2733<span>N-API version: 1</span>
2734</div>
2735<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bool</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code></pre>
2736<ul>
2737<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2738<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>Boolean</code>.</li>
2739<li><code>[out] result</code>: C boolean primitive equivalent of the given JavaScript
2740<code>Boolean</code>.</li>
2741</ul>
2742<p>Returns <code>napi_ok</code> if the API succeeded. If a non-boolean <code>napi_value</code> is
2743passed in it returns <code>napi_boolean_expected</code>.</p>
2744<p>This API returns the C boolean primitive equivalent of the given JavaScript
2745<code>Boolean</code>.</p>
2746<h5>napi_get_value_double<span><a class="mark" href="#n_api_napi_get_value_double" id="n_api_napi_get_value_double">#</a></span></h5>
2747<div class="api_metadata">
2748<span>Added in: v8.0.0</span>
2749<span>N-API version: 1</span>
2750</div>
2751<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_double</span><span class="hljs-params">(napi_env env,
2752                                  napi_value value,
2753                                  <span class="hljs-type">double</span>* result)</span></code></pre>
2754<ul>
2755<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2756<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li>
2757<li><code>[out] result</code>: C double primitive equivalent of the given JavaScript
2758<code>number</code>.</li>
2759</ul>
2760<p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code> is passed
2761in it returns <code>napi_number_expected</code>.</p>
2762<p>This API returns the C double primitive equivalent of the given JavaScript
2763<code>number</code>.</p>
2764<h5>napi_get_value_bigint_int64<span><a class="mark" href="#n_api_napi_get_value_bigint_int64" id="n_api_napi_get_value_bigint_int64">#</a></span></h5>
2765<div class="api_metadata">
2766<span>Added in: v10.7.0</span>
2767<span>N-API version: 6</span>
2768</div>
2769<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_int64</span><span class="hljs-params">(napi_env env,
2770                                        napi_value value,
2771                                        <span class="hljs-type">int64_t</span>* result,
2772                                        <span class="hljs-type">bool</span>* lossless)</span>;</code></pre>
2773<ul>
2774<li><code>[in] env</code>: The environment that the API is invoked under</li>
2775<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>BigInt</code>.</li>
2776<li><code>[out] result</code>: C <code>int64_t</code> primitive equivalent of the given JavaScript
2777<code>BigInt</code>.</li>
2778<li><code>[out] lossless</code>: Indicates whether the <code>BigInt</code> value was converted
2779losslessly.</li>
2780</ul>
2781<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it
2782returns <code>napi_bigint_expected</code>.</p>
2783<p>This API returns the C <code>int64_t</code> primitive equivalent of the given JavaScript
2784<code>BigInt</code>. If needed it will truncate the value, setting <code>lossless</code> to <code>false</code>.</p>
2785<h5>napi_get_value_bigint_uint64<span><a class="mark" href="#n_api_napi_get_value_bigint_uint64" id="n_api_napi_get_value_bigint_uint64">#</a></span></h5>
2786<div class="api_metadata">
2787<span>Added in: v10.7.0</span>
2788<span>N-API version: 6</span>
2789</div>
2790<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_uint64</span><span class="hljs-params">(napi_env env,
2791                                        napi_value value,
2792                                        <span class="hljs-type">uint64_t</span>* result,
2793                                        <span class="hljs-type">bool</span>* lossless)</span>;</code></pre>
2794<ul>
2795<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2796<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>BigInt</code>.</li>
2797<li><code>[out] result</code>: C <code>uint64_t</code> primitive equivalent of the given JavaScript
2798<code>BigInt</code>.</li>
2799<li><code>[out] lossless</code>: Indicates whether the <code>BigInt</code> value was converted
2800losslessly.</li>
2801</ul>
2802<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it
2803returns <code>napi_bigint_expected</code>.</p>
2804<p>This API returns the C <code>uint64_t</code> primitive equivalent of the given JavaScript
2805<code>BigInt</code>. If needed it will truncate the value, setting <code>lossless</code> to <code>false</code>.</p>
2806<h5>napi_get_value_bigint_words<span><a class="mark" href="#n_api_napi_get_value_bigint_words" id="n_api_napi_get_value_bigint_words">#</a></span></h5>
2807<div class="api_metadata">
2808<span>Added in: v10.7.0</span>
2809<span>N-API version: 6</span>
2810</div>
2811<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_words</span><span class="hljs-params">(napi_env env,
2812                                        napi_value value,
2813                                        <span class="hljs-type">int</span>* sign_bit,
2814                                        <span class="hljs-type">size_t</span>* word_count,
2815                                        <span class="hljs-type">uint64_t</span>* words)</span>;</code></pre>
2816<ul>
2817<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2818<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>BigInt</code>.</li>
2819<li><code>[out] sign_bit</code>: Integer representing if the JavaScript <code>BigInt</code> is positive
2820or negative.</li>
2821<li><code>[in/out] word_count</code>: Must be initialized to the length of the <code>words</code>
2822array. Upon return, it will be set to the actual number of words that
2823would be needed to store this <code>BigInt</code>.</li>
2824<li><code>[out] words</code>: Pointer to a pre-allocated 64-bit word array.</li>
2825</ul>
2826<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2827<p>This API converts a single <code>BigInt</code> value into a sign bit, 64-bit little-endian
2828array, and the number of elements in the array. <code>sign_bit</code> and <code>words</code> may be
2829both set to <code>NULL</code>, in order to get only <code>word_count</code>.</p>
2830<h5>napi_get_value_external<span><a class="mark" href="#n_api_napi_get_value_external" id="n_api_napi_get_value_external">#</a></span></h5>
2831<div class="api_metadata">
2832<span>Added in: v8.0.0</span>
2833<span>N-API version: 1</span>
2834</div>
2835<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_external</span><span class="hljs-params">(napi_env env,
2836                                    napi_value value,
2837                                    <span class="hljs-type">void</span>** result)</span></code></pre>
2838<ul>
2839<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2840<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript external value.</li>
2841<li><code>[out] result</code>: Pointer to the data wrapped by the JavaScript external value.</li>
2842</ul>
2843<p>Returns <code>napi_ok</code> if the API succeeded. If a non-external <code>napi_value</code> is
2844passed in it returns <code>napi_invalid_arg</code>.</p>
2845<p>This API retrieves the external data pointer that was previously passed to
2846<code>napi_create_external()</code>.</p>
2847<h5>napi_get_value_int32<span><a class="mark" href="#n_api_napi_get_value_int32" id="n_api_napi_get_value_int32">#</a></span></h5>
2848<div class="api_metadata">
2849<span>Added in: v8.0.0</span>
2850<span>N-API version: 1</span>
2851</div>
2852<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int32</span><span class="hljs-params">(napi_env env,
2853                                 napi_value value,
2854                                 <span class="hljs-type">int32_t</span>* result)</span></code></pre>
2855<ul>
2856<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2857<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li>
2858<li><code>[out] result</code>: C <code>int32</code> primitive equivalent of the given JavaScript
2859<code>number</code>.</li>
2860</ul>
2861<p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
2862is passed in <code>napi_number_expected</code>.</p>
2863<p>This API returns the C <code>int32</code> primitive equivalent
2864of the given JavaScript <code>number</code>.</p>
2865<p>If the number exceeds the range of the 32 bit integer, then the result is
2866truncated to the equivalent of the bottom 32 bits. This can result in a large
2867positive number becoming a negative number if the value is > 2<sup>31</sup> - 1.</p>
2868<p>Non-finite number values (<code>NaN</code>, <code>+Infinity</code>, or <code>-Infinity</code>) set the
2869result to zero.</p>
2870<h5>napi_get_value_int64<span><a class="mark" href="#n_api_napi_get_value_int64" id="n_api_napi_get_value_int64">#</a></span></h5>
2871<div class="api_metadata">
2872<span>Added in: v8.0.0</span>
2873<span>N-API version: 1</span>
2874</div>
2875<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int64</span><span class="hljs-params">(napi_env env,
2876                                 napi_value value,
2877                                 <span class="hljs-type">int64_t</span>* result)</span></code></pre>
2878<ul>
2879<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2880<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li>
2881<li><code>[out] result</code>: C <code>int64</code> primitive equivalent of the given JavaScript
2882<code>number</code>.</li>
2883</ul>
2884<p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
2885is passed in it returns <code>napi_number_expected</code>.</p>
2886<p>This API returns the C <code>int64</code> primitive equivalent of the given JavaScript
2887<code>number</code>.</p>
2888<p><code>number</code> values outside the range of <a href="https://tc39.github.io/ecma262/#sec-number.min_safe_integer"><code>Number.MIN_SAFE_INTEGER</code></a>
2889<code>-(2**53 - 1)</code> - <a href="https://tc39.github.io/ecma262/#sec-number.max_safe_integer"><code>Number.MAX_SAFE_INTEGER</code></a> <code>(2**53 - 1)</code> will lose
2890precision.</p>
2891<p>Non-finite number values (<code>NaN</code>, <code>+Infinity</code>, or <code>-Infinity</code>) set the
2892result to zero.</p>
2893<h5>napi_get_value_string_latin1<span><a class="mark" href="#n_api_napi_get_value_string_latin1" id="n_api_napi_get_value_string_latin1">#</a></span></h5>
2894<div class="api_metadata">
2895<span>Added in: v8.0.0</span>
2896<span>N-API version: 1</span>
2897</div>
2898<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_latin1</span><span class="hljs-params">(napi_env env,
2899                                         napi_value value,
2900                                         <span class="hljs-type">char</span>* buf,
2901                                         <span class="hljs-type">size_t</span> bufsize,
2902                                         <span class="hljs-type">size_t</span>* result)</span></code></pre>
2903<ul>
2904<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2905<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript string.</li>
2906<li><code>[in] buf</code>: Buffer to write the ISO-8859-1-encoded string into. If <code>NULL</code> is
2907passed in, the length of the string in bytes and excluding the null terminator
2908is returned in <code>result</code>.</li>
2909<li><code>[in] bufsize</code>: Size of the destination buffer. When this value is
2910insufficient, the returned string is truncated and null-terminated.</li>
2911<li><code>[out] result</code>: Number of bytes copied into the buffer, excluding the null
2912terminator.</li>
2913</ul>
2914<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value</code>
2915is passed in it returns <code>napi_string_expected</code>.</p>
2916<p>This API returns the ISO-8859-1-encoded string corresponding the value passed
2917in.</p>
2918<h5>napi_get_value_string_utf8<span><a class="mark" href="#n_api_napi_get_value_string_utf8" id="n_api_napi_get_value_string_utf8">#</a></span></h5>
2919<div class="api_metadata">
2920<span>Added in: v8.0.0</span>
2921<span>N-API version: 1</span>
2922</div>
2923<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_utf8</span><span class="hljs-params">(napi_env env,
2924                                       napi_value value,
2925                                       <span class="hljs-type">char</span>* buf,
2926                                       <span class="hljs-type">size_t</span> bufsize,
2927                                       <span class="hljs-type">size_t</span>* result)</span></code></pre>
2928<ul>
2929<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2930<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript string.</li>
2931<li><code>[in] buf</code>: Buffer to write the UTF8-encoded string into. If <code>NULL</code> is passed
2932in, the length of the string in bytes and excluding the null terminator is
2933returned in <code>result</code>.</li>
2934<li><code>[in] bufsize</code>: Size of the destination buffer. When this value is
2935insufficient, the returned string is truncated and null-terminated.</li>
2936<li><code>[out] result</code>: Number of bytes copied into the buffer, excluding the null
2937terminator.</li>
2938</ul>
2939<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value</code>
2940is passed in it returns <code>napi_string_expected</code>.</p>
2941<p>This API returns the UTF8-encoded string corresponding the value passed in.</p>
2942<h5>napi_get_value_string_utf16<span><a class="mark" href="#n_api_napi_get_value_string_utf16" id="n_api_napi_get_value_string_utf16">#</a></span></h5>
2943<div class="api_metadata">
2944<span>Added in: v8.0.0</span>
2945<span>N-API version: 1</span>
2946</div>
2947<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_utf16</span><span class="hljs-params">(napi_env env,
2948                                        napi_value value,
2949                                        <span class="hljs-type">char16_t</span>* buf,
2950                                        <span class="hljs-type">size_t</span> bufsize,
2951                                        <span class="hljs-type">size_t</span>* result)</span></code></pre>
2952<ul>
2953<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2954<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript string.</li>
2955<li><code>[in] buf</code>: Buffer to write the UTF16-LE-encoded string into. If <code>NULL</code> is
2956passed in, the length of the string in 2-byte code units and excluding the
2957null terminator is returned.</li>
2958<li><code>[in] bufsize</code>: Size of the destination buffer. When this value is
2959insufficient, the returned string is truncated and null-terminated.</li>
2960<li><code>[out] result</code>: Number of 2-byte code units copied into the buffer, excluding
2961the null terminator.</li>
2962</ul>
2963<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value</code>
2964is passed in it returns <code>napi_string_expected</code>.</p>
2965<p>This API returns the UTF16-encoded string corresponding the value passed in.</p>
2966<h5>napi_get_value_uint32<span><a class="mark" href="#n_api_napi_get_value_uint32" id="n_api_napi_get_value_uint32">#</a></span></h5>
2967<div class="api_metadata">
2968<span>Added in: v8.0.0</span>
2969<span>N-API version: 1</span>
2970</div>
2971<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_uint32</span><span class="hljs-params">(napi_env env,
2972                                  napi_value value,
2973                                  <span class="hljs-type">uint32_t</span>* result)</span></code></pre>
2974<ul>
2975<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2976<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li>
2977<li><code>[out] result</code>: C primitive equivalent of the given <code>napi_value</code> as a
2978<code>uint32_t</code>.</li>
2979</ul>
2980<p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
2981is passed in it returns <code>napi_number_expected</code>.</p>
2982<p>This API returns the C primitive equivalent of the given <code>napi_value</code> as a
2983<code>uint32_t</code>.</p>
2984<h4>Functions to get global instances<span><a class="mark" href="#n_api_functions_to_get_global_instances" id="n_api_functions_to_get_global_instances">#</a></span></h4>
2985<h5>napi_get_boolean<span><a class="mark" href="#n_api_napi_get_boolean" id="n_api_napi_get_boolean">#</a></span></h5>
2986<div class="api_metadata">
2987<span>Added in: v8.0.0</span>
2988<span>N-API version: 1</span>
2989</div>
2990<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_boolean</span><span class="hljs-params">(napi_env env, <span class="hljs-type">bool</span> value, napi_value* result)</span></code></pre>
2991<ul>
2992<li><code>[in] env</code>: The environment that the API is invoked under.</li>
2993<li><code>[in] value</code>: The value of the boolean to retrieve.</li>
2994<li><code>[out] result</code>: <code>napi_value</code> representing JavaScript <code>Boolean</code> singleton to
2995retrieve.</li>
2996</ul>
2997<p>Returns <code>napi_ok</code> if the API succeeded.</p>
2998<p>This API is used to return the JavaScript singleton object that is used to
2999represent the given boolean value.</p>
3000<h5>napi_get_global<span><a class="mark" href="#n_api_napi_get_global" id="n_api_napi_get_global">#</a></span></h5>
3001<div class="api_metadata">
3002<span>Added in: v8.0.0</span>
3003<span>N-API version: 1</span>
3004</div>
3005<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_global</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code></pre>
3006<ul>
3007<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3008<li><code>[out] result</code>: <code>napi_value</code> representing JavaScript <code>global</code> object.</li>
3009</ul>
3010<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3011<p>This API returns the <code>global</code> object.</p>
3012<h5>napi_get_null<span><a class="mark" href="#n_api_napi_get_null" id="n_api_napi_get_null">#</a></span></h5>
3013<div class="api_metadata">
3014<span>Added in: v8.0.0</span>
3015<span>N-API version: 1</span>
3016</div>
3017<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_null</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code></pre>
3018<ul>
3019<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3020<li><code>[out] result</code>: <code>napi_value</code> representing JavaScript <code>null</code> object.</li>
3021</ul>
3022<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3023<p>This API returns the <code>null</code> object.</p>
3024<h5>napi_get_undefined<span><a class="mark" href="#n_api_napi_get_undefined" id="n_api_napi_get_undefined">#</a></span></h5>
3025<div class="api_metadata">
3026<span>Added in: v8.0.0</span>
3027<span>N-API version: 1</span>
3028</div>
3029<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_undefined</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code></pre>
3030<ul>
3031<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3032<li><code>[out] result</code>: <code>napi_value</code> representing JavaScript Undefined value.</li>
3033</ul>
3034<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3035<p>This API returns the Undefined object.</p>
3036</section><section><h3>Working with JavaScript values and abstract operations<span><a class="mark" href="#n_api_working_with_javascript_values_and_abstract_operations" id="n_api_working_with_javascript_values_and_abstract_operations">#</a></span></h3>
3037<p>Node-API exposes a set of APIs to perform some abstract operations on JavaScript
3038values. Some of these operations are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Section 7</a>
3039of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p>
3040<p>These APIs support doing one of the following:</p>
3041<ol>
3042<li>Coerce JavaScript values to specific JavaScript types (such as <code>number</code> or
3043<code>string</code>).</li>
3044<li>Check the type of a JavaScript value.</li>
3045<li>Check for equality between two JavaScript values.</li>
3046</ol>
3047<h4>napi_coerce_to_bool<span><a class="mark" href="#n_api_napi_coerce_to_bool" id="n_api_napi_coerce_to_bool">#</a></span></h4>
3048<div class="api_metadata">
3049<span>Added in: v8.0.0</span>
3050<span>N-API version: 1</span>
3051</div>
3052<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_bool</span><span class="hljs-params">(napi_env env,
3053                                napi_value value,
3054                                napi_value* result)</span></code></pre>
3055<ul>
3056<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3057<li><code>[in] value</code>: The JavaScript value to coerce.</li>
3058<li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>Boolean</code>.</li>
3059</ul>
3060<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3061<p>This API implements the abstract operation <code>ToBoolean()</code> as defined in
3062<a href="https://tc39.github.io/ecma262/#sec-toboolean">Section 7.1.2</a> of the ECMAScript Language Specification.
3063This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p>
3064<h4>napi_coerce_to_number<span><a class="mark" href="#n_api_napi_coerce_to_number" id="n_api_napi_coerce_to_number">#</a></span></h4>
3065<div class="api_metadata">
3066<span>Added in: v8.0.0</span>
3067<span>N-API version: 1</span>
3068</div>
3069<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_number</span><span class="hljs-params">(napi_env env,
3070                                  napi_value value,
3071                                  napi_value* result)</span></code></pre>
3072<ul>
3073<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3074<li><code>[in] value</code>: The JavaScript value to coerce.</li>
3075<li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>number</code>.</li>
3076</ul>
3077<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3078<p>This API implements the abstract operation <code>ToNumber()</code> as defined in
3079<a href="https://tc39.github.io/ecma262/#sec-tonumber">Section 7.1.3</a> of the ECMAScript Language Specification.
3080This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p>
3081<h4>napi_coerce_to_object<span><a class="mark" href="#n_api_napi_coerce_to_object" id="n_api_napi_coerce_to_object">#</a></span></h4>
3082<div class="api_metadata">
3083<span>Added in: v8.0.0</span>
3084<span>N-API version: 1</span>
3085</div>
3086<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_object</span><span class="hljs-params">(napi_env env,
3087                                  napi_value value,
3088                                  napi_value* result)</span></code></pre>
3089<ul>
3090<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3091<li><code>[in] value</code>: The JavaScript value to coerce.</li>
3092<li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>Object</code>.</li>
3093</ul>
3094<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3095<p>This API implements the abstract operation <code>ToObject()</code> as defined in
3096<a href="https://tc39.github.io/ecma262/#sec-toobject">Section 7.1.13</a> of the ECMAScript Language Specification.
3097This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p>
3098<h4>napi_coerce_to_string<span><a class="mark" href="#n_api_napi_coerce_to_string" id="n_api_napi_coerce_to_string">#</a></span></h4>
3099<div class="api_metadata">
3100<span>Added in: v8.0.0</span>
3101<span>N-API version: 1</span>
3102</div>
3103<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_string</span><span class="hljs-params">(napi_env env,
3104                                  napi_value value,
3105                                  napi_value* result)</span></code></pre>
3106<ul>
3107<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3108<li><code>[in] value</code>: The JavaScript value to coerce.</li>
3109<li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>string</code>.</li>
3110</ul>
3111<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3112<p>This API implements the abstract operation <code>ToString()</code> as defined in
3113<a href="https://tc39.github.io/ecma262/#sec-toobject">Section 7.1.13</a> of the ECMAScript Language Specification.
3114This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p>
3115<h4>napi_typeof<span><a class="mark" href="#n_api_napi_typeof" id="n_api_napi_typeof">#</a></span></h4>
3116<div class="api_metadata">
3117<span>Added in: v8.0.0</span>
3118<span>N-API version: 1</span>
3119</div>
3120<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_typeof</span><span class="hljs-params">(napi_env env, napi_value value, napi_valuetype* result)</span></code></pre>
3121<ul>
3122<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3123<li><code>[in] value</code>: The JavaScript value whose type to query.</li>
3124<li><code>[out] result</code>: The type of the JavaScript value.</li>
3125</ul>
3126<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3127<ul>
3128<li><code>napi_invalid_arg</code> if the type of <code>value</code> is not a known ECMAScript type and
3129<code>value</code> is not an External value.</li>
3130</ul>
3131<p>This API represents behavior similar to invoking the <code>typeof</code> Operator on
3132the object as defined in <a href="https://tc39.github.io/ecma262/#sec-typeof-operator">Section 12.5.5</a> of the ECMAScript Language
3133Specification. However, there are some differences:</p>
3134<ol>
3135<li>It has support for detecting an External value.</li>
3136<li>It detects <code>null</code> as a separate type, while ECMAScript <code>typeof</code> would detect
3137<code>object</code>.</li>
3138</ol>
3139<p>If <code>value</code> has a type that is invalid, an error is returned.</p>
3140<h4>napi_instanceof<span><a class="mark" href="#n_api_napi_instanceof" id="n_api_napi_instanceof">#</a></span></h4>
3141<div class="api_metadata">
3142<span>Added in: v8.0.0</span>
3143<span>N-API version: 1</span>
3144</div>
3145<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_instanceof</span><span class="hljs-params">(napi_env env,
3146                            napi_value object,
3147                            napi_value constructor,
3148                            <span class="hljs-type">bool</span>* result)</span></code></pre>
3149<ul>
3150<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3151<li><code>[in] object</code>: The JavaScript value to check.</li>
3152<li><code>[in] constructor</code>: The JavaScript function object of the constructor function
3153to check against.</li>
3154<li><code>[out] result</code>: Boolean that is set to true if <code>object instanceof constructor</code>
3155is true.</li>
3156</ul>
3157<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3158<p>This API represents invoking the <code>instanceof</code> Operator on the object as
3159defined in <a href="https://tc39.github.io/ecma262/#sec-instanceofoperator">Section 12.10.4</a> of the ECMAScript Language Specification.</p>
3160<h4>napi_is_array<span><a class="mark" href="#n_api_napi_is_array" id="n_api_napi_is_array">#</a></span></h4>
3161<div class="api_metadata">
3162<span>Added in: v8.0.0</span>
3163<span>N-API version: 1</span>
3164</div>
3165<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_array</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code></pre>
3166<ul>
3167<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3168<li><code>[in] value</code>: The JavaScript value to check.</li>
3169<li><code>[out] result</code>: Whether the given object is an array.</li>
3170</ul>
3171<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3172<p>This API represents invoking the <code>IsArray</code> operation on the object
3173as defined in <a href="https://tc39.github.io/ecma262/#sec-isarray">Section 7.2.2</a> of the ECMAScript Language Specification.</p>
3174<h4>napi_is_arraybuffer<span><a class="mark" href="#n_api_napi_is_arraybuffer" id="n_api_napi_is_arraybuffer">#</a></span></h4>
3175<div class="api_metadata">
3176<span>Added in: v8.0.0</span>
3177<span>N-API version: 1</span>
3178</div>
3179<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_arraybuffer</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code></pre>
3180<ul>
3181<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3182<li><code>[in] value</code>: The JavaScript value to check.</li>
3183<li><code>[out] result</code>: Whether the given object is an <code>ArrayBuffer</code>.</li>
3184</ul>
3185<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3186<p>This API checks if the <code>Object</code> passed in is an array buffer.</p>
3187<h4>napi_is_buffer<span><a class="mark" href="#n_api_napi_is_buffer" id="n_api_napi_is_buffer">#</a></span></h4>
3188<div class="api_metadata">
3189<span>Added in: v8.0.0</span>
3190<span>N-API version: 1</span>
3191</div>
3192<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_buffer</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code></pre>
3193<ul>
3194<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3195<li><code>[in] value</code>: The JavaScript value to check.</li>
3196<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a <code>node::Buffer</code>
3197object.</li>
3198</ul>
3199<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3200<p>This API checks if the <code>Object</code> passed in is a buffer.</p>
3201<h4>napi_is_date<span><a class="mark" href="#n_api_napi_is_date" id="n_api_napi_is_date">#</a></span></h4>
3202<div class="api_metadata">
3203<span>Added in: v11.11.0, v10.17.0</span>
3204<span>N-API version: 5</span>
3205</div>
3206<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_date</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code></pre>
3207<ul>
3208<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3209<li><code>[in] value</code>: The JavaScript value to check.</li>
3210<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a JavaScript <code>Date</code>
3211object.</li>
3212</ul>
3213<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3214<p>This API checks if the <code>Object</code> passed in is a date.</p>
3215<h4>napi_is_error<span><a class="mark" href="#n_api_napi_is_error_1" id="n_api_napi_is_error_1">#</a></span></h4>
3216<div class="api_metadata">
3217<span>Added in: v8.0.0</span>
3218<span>N-API version: 1</span>
3219</div>
3220<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_error</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code></pre>
3221<ul>
3222<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3223<li><code>[in] value</code>: The JavaScript value to check.</li>
3224<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents an <code>Error</code> object.</li>
3225</ul>
3226<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3227<p>This API checks if the <code>Object</code> passed in is an <code>Error</code>.</p>
3228<h4>napi_is_typedarray<span><a class="mark" href="#n_api_napi_is_typedarray" id="n_api_napi_is_typedarray">#</a></span></h4>
3229<div class="api_metadata">
3230<span>Added in: v8.0.0</span>
3231<span>N-API version: 1</span>
3232</div>
3233<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_typedarray</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code></pre>
3234<ul>
3235<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3236<li><code>[in] value</code>: The JavaScript value to check.</li>
3237<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a <code>TypedArray</code>.</li>
3238</ul>
3239<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3240<p>This API checks if the <code>Object</code> passed in is a typed array.</p>
3241<h4>napi_is_dataview<span><a class="mark" href="#n_api_napi_is_dataview" id="n_api_napi_is_dataview">#</a></span></h4>
3242<div class="api_metadata">
3243<span>Added in: v8.3.0</span>
3244<span>N-API version: 1</span>
3245</div>
3246<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_dataview</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code></pre>
3247<ul>
3248<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3249<li><code>[in] value</code>: The JavaScript value to check.</li>
3250<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a <code>DataView</code>.</li>
3251</ul>
3252<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3253<p>This API checks if the <code>Object</code> passed in is a <code>DataView</code>.</p>
3254<h4>napi_strict_equals<span><a class="mark" href="#n_api_napi_strict_equals" id="n_api_napi_strict_equals">#</a></span></h4>
3255<div class="api_metadata">
3256<span>Added in: v8.0.0</span>
3257<span>N-API version: 1</span>
3258</div>
3259<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_strict_equals</span><span class="hljs-params">(napi_env env,
3260                               napi_value lhs,
3261                               napi_value rhs,
3262                               <span class="hljs-type">bool</span>* result)</span></code></pre>
3263<ul>
3264<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3265<li><code>[in] lhs</code>: The JavaScript value to check.</li>
3266<li><code>[in] rhs</code>: The JavaScript value to check against.</li>
3267<li><code>[out] result</code>: Whether the two <code>napi_value</code> objects are equal.</li>
3268</ul>
3269<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3270<p>This API represents the invocation of the Strict Equality algorithm as
3271defined in <a href="https://tc39.github.io/ecma262/#sec-strict-equality-comparison">Section 7.2.14</a> of the ECMAScript Language Specification.</p>
3272<h4>napi_detach_arraybuffer<span><a class="mark" href="#n_api_napi_detach_arraybuffer" id="n_api_napi_detach_arraybuffer">#</a></span></h4>
3273<div class="api_metadata">
3274<span>Added in: v13.0.0, v12.16.0, v10.22.0</span>
3275<span>N-API version: 7</span>
3276</div>
3277<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_detach_arraybuffer</span><span class="hljs-params">(napi_env env,
3278                                    napi_value arraybuffer)</span></code></pre>
3279<ul>
3280<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3281<li><code>[in] arraybuffer</code>: The JavaScript <code>ArrayBuffer</code> to be detached.</li>
3282</ul>
3283<p>Returns <code>napi_ok</code> if the API succeeded. If a non-detachable <code>ArrayBuffer</code> is
3284passed in it returns <code>napi_detachable_arraybuffer_expected</code>.</p>
3285<p>Generally, an <code>ArrayBuffer</code> is non-detachable if it has been detached before.
3286The engine may impose additional conditions on whether an <code>ArrayBuffer</code> is
3287detachable. For example, V8 requires that the <code>ArrayBuffer</code> be external,
3288that is, created with <a href="#n_api_napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code></a>.</p>
3289<p>This API represents the invocation of the <code>ArrayBuffer</code> detach operation as
3290defined in <a href="https://tc39.es/ecma262/#sec-detacharraybuffer">Section 24.1.1.3</a> of the ECMAScript Language Specification.</p>
3291<h4>napi_is_detached_arraybuffer<span><a class="mark" href="#n_api_napi_is_detached_arraybuffer" id="n_api_napi_is_detached_arraybuffer">#</a></span></h4>
3292<div class="api_metadata">
3293<span>Added in: v13.3.0, v12.16.0, v10.22.0</span>
3294<span>N-API version: 7</span>
3295</div>
3296<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_detached_arraybuffer</span><span class="hljs-params">(napi_env env,
3297                                         napi_value arraybuffer,
3298                                         <span class="hljs-type">bool</span>* result)</span></code></pre>
3299<ul>
3300<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3301<li><code>[in] arraybuffer</code>: The JavaScript <code>ArrayBuffer</code> to be checked.</li>
3302<li><code>[out] result</code>: Whether the <code>arraybuffer</code> is detached.</li>
3303</ul>
3304<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3305<p>The <code>ArrayBuffer</code> is considered detached if its internal data is <code>null</code>.</p>
3306<p>This API represents the invocation of the <code>ArrayBuffer</code> <code>IsDetachedBuffer</code>
3307operation as defined in <a href="https://tc39.es/ecma262/#sec-isdetachedbuffer">Section 24.1.1.2</a> of the ECMAScript Language
3308Specification.</p>
3309</section><section><h3>Working with JavaScript properties<span><a class="mark" href="#n_api_working_with_javascript_properties" id="n_api_working_with_javascript_properties">#</a></span></h3>
3310<p>Node-API exposes a set of APIs to get and set properties on JavaScript
3311objects. Some of these types are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Section 7</a> of the
3312<a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p>
3313<p>Properties in JavaScript are represented as a tuple of a key and a value.
3314Fundamentally, all property keys in Node-API can be represented in one of the
3315following forms:</p>
3316<ul>
3317<li>Named: a simple UTF8-encoded string</li>
3318<li>Integer-Indexed: an index value represented by <code>uint32_t</code></li>
3319<li>JavaScript value: these are represented in Node-API by <code>napi_value</code>. This can
3320be a <code>napi_value</code> representing a <code>string</code>, <code>number</code>, or <code>symbol</code>.</li>
3321</ul>
3322<p>Node-API values are represented by the type <code>napi_value</code>.
3323Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>.
3324However, it's the caller's responsibility to make sure that the
3325<code>napi_value</code> in question is of the JavaScript type expected by the API.</p>
3326<p>The APIs documented in this section provide a simple interface to
3327get and set properties on arbitrary JavaScript objects represented by
3328<code>napi_value</code>.</p>
3329<p>For instance, consider the following JavaScript code snippet:</p>
3330<pre><code class="language-js"><span class="hljs-keyword">const</span> obj = {};
3331obj.<span class="hljs-property">myProp</span> = <span class="hljs-number">123</span>;</code></pre>
3332<p>The equivalent can be done using Node-API values with the following snippet:</p>
3333<pre><code class="language-c">napi_status status = napi_generic_failure;
3334
3335<span class="hljs-comment">// const obj = {}</span>
3336napi_value obj, value;
3337status = napi_create_object(env, &#x26;obj);
3338<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
3339
3340<span class="hljs-comment">// Create a napi_value for 123</span>
3341status = napi_create_int32(env, <span class="hljs-number">123</span>, &#x26;value);
3342<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
3343
3344<span class="hljs-comment">// obj.myProp = 123</span>
3345status = napi_set_named_property(env, obj, <span class="hljs-string">"myProp"</span>, value);
3346<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code></pre>
3347<p>Indexed properties can be set in a similar manner. Consider the following
3348JavaScript snippet:</p>
3349<pre><code class="language-js"><span class="hljs-keyword">const</span> arr = [];
3350arr[<span class="hljs-number">123</span>] = <span class="hljs-string">'hello'</span>;</code></pre>
3351<p>The equivalent can be done using Node-API values with the following snippet:</p>
3352<pre><code class="language-c">napi_status status = napi_generic_failure;
3353
3354<span class="hljs-comment">// const arr = [];</span>
3355napi_value arr, value;
3356status = napi_create_array(env, &#x26;arr);
3357<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
3358
3359<span class="hljs-comment">// Create a napi_value for 'hello'</span>
3360status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &#x26;value);
3361<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
3362
3363<span class="hljs-comment">// arr[123] = 'hello';</span>
3364status = napi_set_element(env, arr, <span class="hljs-number">123</span>, value);
3365<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code></pre>
3366<p>Properties can be retrieved using the APIs described in this section.
3367Consider the following JavaScript snippet:</p>
3368<pre><code class="language-js"><span class="hljs-keyword">const</span> arr = [];
3369<span class="hljs-keyword">const</span> value = arr[<span class="hljs-number">123</span>];</code></pre>
3370<p>The following is the approximate equivalent of the Node-API counterpart:</p>
3371<pre><code class="language-c">napi_status status = napi_generic_failure;
3372
3373<span class="hljs-comment">// const arr = []</span>
3374napi_value arr, value;
3375status = napi_create_array(env, &#x26;arr);
3376<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
3377
3378<span class="hljs-comment">// const value = arr[123]</span>
3379status = napi_get_element(env, arr, <span class="hljs-number">123</span>, &#x26;value);
3380<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code></pre>
3381<p>Finally, multiple properties can also be defined on an object for performance
3382reasons. Consider the following JavaScript:</p>
3383<pre><code class="language-js"><span class="hljs-keyword">const</span> obj = {};
3384<span class="hljs-title class_">Object</span>.<span class="hljs-title function_">defineProperties</span>(obj, {
3385  <span class="hljs-string">'foo'</span>: { <span class="hljs-attr">value</span>: <span class="hljs-number">123</span>, <span class="hljs-attr">writable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">configurable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">enumerable</span>: <span class="hljs-literal">true</span> },
3386  <span class="hljs-string">'bar'</span>: { <span class="hljs-attr">value</span>: <span class="hljs-number">456</span>, <span class="hljs-attr">writable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">configurable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">enumerable</span>: <span class="hljs-literal">true</span> }
3387});</code></pre>
3388<p>The following is the approximate equivalent of the Node-API counterpart:</p>
3389<pre><code class="language-c">napi_status status = napi_status_generic_failure;
3390
3391<span class="hljs-comment">// const obj = {};</span>
3392napi_value obj;
3393status = napi_create_object(env, &#x26;obj);
3394<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
3395
3396<span class="hljs-comment">// Create napi_values for 123 and 456</span>
3397napi_value fooValue, barValue;
3398status = napi_create_int32(env, <span class="hljs-number">123</span>, &#x26;fooValue);
3399<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
3400status = napi_create_int32(env, <span class="hljs-number">456</span>, &#x26;barValue);
3401<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;
3402
3403<span class="hljs-comment">// Set the properties</span>
3404napi_property_descriptor descriptors[] = {
3405  { <span class="hljs-string">"foo"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, fooValue, napi_writable | napi_configurable, <span class="hljs-literal">NULL</span> },
3406  { <span class="hljs-string">"bar"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, barValue, napi_writable | napi_configurable, <span class="hljs-literal">NULL</span> }
3407}
3408status = napi_define_properties(env,
3409                                obj,
3410                                <span class="hljs-keyword">sizeof</span>(descriptors) / <span class="hljs-keyword">sizeof</span>(descriptors[<span class="hljs-number">0</span>]),
3411                                descriptors);
3412<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code></pre>
3413<h4>Structures<span><a class="mark" href="#n_api_structures" id="n_api_structures">#</a></span></h4>
3414<h5>napi_property_attributes<span><a class="mark" href="#n_api_napi_property_attributes" id="n_api_napi_property_attributes">#</a></span></h5>
3415<div class="api_metadata">
3416<details class="changelog"><summary>History</summary>
3417<table>
3418<tbody><tr><th>Version</th><th>Changes</th></tr>
3419<tr><td>v14.12.0</td>
3420<td><p>added <code>napi_default_method</code> and <code>napi_default_property</code>.</p></td></tr>
3421</tbody></table>
3422</details>
3423</div>
3424<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span>
3425  napi_default = <span class="hljs-number">0</span>,
3426  napi_writable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">0</span>,
3427  napi_enumerable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">1</span>,
3428  napi_configurable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">2</span>,
3429
3430  <span class="hljs-comment">// Used with napi_define_class to distinguish static properties</span>
3431  <span class="hljs-comment">// from instance properties. Ignored by napi_define_properties.</span>
3432  napi_static = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">10</span>,
3433
3434  <span class="hljs-comment">// Default for class methods.</span>
3435  napi_default_method = napi_writable | napi_configurable,
3436
3437  <span class="hljs-comment">// Default for object properties, like in JS obj[prop].</span>
3438  napi_default_jsproperty = napi_writable |
3439                          napi_enumerable |
3440                          napi_configurable,
3441} napi_property_attributes;</code></pre>
3442<p><code>napi_property_attributes</code> are flags used to control the behavior of properties
3443set on a JavaScript object. Other than <code>napi_static</code> they correspond to the
3444attributes listed in <a href="https://tc39.github.io/ecma262/#table-2">Section 6.1.7.1</a>
3445of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.
3446They can be one or more of the following bitflags:</p>
3447<ul>
3448<li><code>napi_default</code>: No explicit attributes are set on the property. By default, a
3449property is read only, not enumerable and not configurable.</li>
3450<li><code>napi_writable</code>: The property is writable.</li>
3451<li><code>napi_enumerable</code>: The property is enumerable.</li>
3452<li><code>napi_configurable</code>: The property is configurable as defined in
3453<a href="https://tc39.github.io/ecma262/#table-2">Section 6.1.7.1</a> of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</li>
3454<li><code>napi_static</code>: The property will be defined as a static property on a class as
3455opposed to an instance property, which is the default. This is used only by
3456<a href="#n_api_napi_define_class"><code>napi_define_class</code></a>. It is ignored by <code>napi_define_properties</code>.</li>
3457<li><code>napi_default_method</code>: Like a method in a JS class, the property is
3458configurable and writeable, but not enumerable.</li>
3459<li><code>napi_default_jsproperty</code>: Like a property set via assignment in JavaScript,
3460the property is writable, enumerable, and configurable.</li>
3461</ul>
3462<h5>napi_property_descriptor<span><a class="mark" href="#n_api_napi_property_descriptor" id="n_api_napi_property_descriptor">#</a></span></h5>
3463<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span>
3464  <span class="hljs-comment">// One of utf8name or name should be NULL.</span>
3465  <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8name;
3466  napi_value name;
3467
3468  napi_callback method;
3469  napi_callback getter;
3470  napi_callback setter;
3471  napi_value value;
3472
3473  napi_property_attributes attributes;
3474  <span class="hljs-type">void</span>* data;
3475} napi_property_descriptor;</code></pre>
3476<ul>
3477<li><code>utf8name</code>: Optional string describing the key for the property,
3478encoded as UTF8. One of <code>utf8name</code> or <code>name</code> must be provided for the
3479property.</li>
3480<li><code>name</code>: Optional <code>napi_value</code> that points to a JavaScript string or symbol
3481to be used as the key for the property. One of <code>utf8name</code> or <code>name</code> must
3482be provided for the property.</li>
3483<li><code>value</code>: The value that's retrieved by a get access of the property if the
3484property is a data property. If this is passed in, set <code>getter</code>, <code>setter</code>,
3485<code>method</code> and <code>data</code> to <code>NULL</code> (since these members won't be used).</li>
3486<li><code>getter</code>: A function to call when a get access of the property is performed.
3487If this is passed in, set <code>value</code> and <code>method</code> to <code>NULL</code> (since these members
3488won't be used). The given function is called implicitly by the runtime when
3489the property is accessed from JavaScript code (or if a get on the property is
3490performed using a Node-API call). <a href="#n_api_napi_callback"><code>napi_callback</code></a> provides more details.</li>
3491<li><code>setter</code>: A function to call when a set access of the property is performed.
3492If this is passed in, set <code>value</code> and <code>method</code> to <code>NULL</code> (since these members
3493won't be used). The given function is called implicitly by the runtime when
3494the property is set from JavaScript code (or if a set on the property is
3495performed using a Node-API call). <a href="#n_api_napi_callback"><code>napi_callback</code></a> provides more details.</li>
3496<li><code>method</code>: Set this to make the property descriptor object's <code>value</code>
3497property to be a JavaScript function represented by <code>method</code>. If this is
3498passed in, set <code>value</code>, <code>getter</code> and <code>setter</code> to <code>NULL</code> (since these members
3499won't be used). <a href="#n_api_napi_callback"><code>napi_callback</code></a> provides more details.</li>
3500<li><code>attributes</code>: The attributes associated with the particular property. See
3501<a href="#n_api_napi_property_attributes"><code>napi_property_attributes</code></a>.</li>
3502<li><code>data</code>: The callback data passed into <code>method</code>, <code>getter</code> and <code>setter</code> if this
3503function is invoked.</li>
3504</ul>
3505<h4>Functions<span><a class="mark" href="#n_api_functions" id="n_api_functions">#</a></span></h4>
3506<h5>napi_get_property_names<span><a class="mark" href="#n_api_napi_get_property_names" id="n_api_napi_get_property_names">#</a></span></h5>
3507<div class="api_metadata">
3508<span>Added in: v8.0.0</span>
3509<span>N-API version: 1</span>
3510</div>
3511<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_property_names</span><span class="hljs-params">(napi_env env,
3512                                    napi_value object,
3513                                    napi_value* result)</span>;</code></pre>
3514<ul>
3515<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
3516<li><code>[in] object</code>: The object from which to retrieve the properties.</li>
3517<li><code>[out] result</code>: A <code>napi_value</code> representing an array of JavaScript values
3518that represent the property names of the object. The API can be used to
3519iterate over <code>result</code> using <a href="#n_api_napi_get_array_length"><code>napi_get_array_length</code></a>
3520and <a href="#n_api_napi_get_element"><code>napi_get_element</code></a>.</li>
3521</ul>
3522<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3523<p>This API returns the names of the enumerable properties of <code>object</code> as an array
3524of strings. The properties of <code>object</code> whose key is a symbol will not be
3525included.</p>
3526<h5>napi_get_all_property_names<span><a class="mark" href="#n_api_napi_get_all_property_names" id="n_api_napi_get_all_property_names">#</a></span></h5>
3527<div class="api_metadata">
3528<span>Added in: v13.7.0, v10.20.0</span>
3529<span>N-API version: 6</span>
3530</div>
3531<pre><code class="language-c">napi_get_all_property_names(napi_env env,
3532                            napi_value object,
3533                            napi_key_collection_mode key_mode,
3534                            napi_key_filter key_filter,
3535                            napi_key_conversion key_conversion,
3536                            napi_value* result);</code></pre>
3537<ul>
3538<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
3539<li><code>[in] object</code>: The object from which to retrieve the properties.</li>
3540<li><code>[in] key_mode</code>: Whether to retrieve prototype properties as well.</li>
3541<li><code>[in] key_filter</code>: Which properties to retrieve
3542(enumerable/readable/writable).</li>
3543<li><code>[in] key_conversion</code>: Whether to convert numbered property keys to strings.</li>
3544<li><code>[out] result</code>: A <code>napi_value</code> representing an array of JavaScript values
3545that represent the property names of the object. <a href="#n_api_napi_get_array_length"><code>napi_get_array_length</code></a>
3546and <a href="#n_api_napi_get_element"><code>napi_get_element</code></a> can be used to iterate over <code>result</code>.</li>
3547</ul>
3548<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3549<p>This API returns an array containing the names of the available properties
3550of this object.</p>
3551<h5>napi_set_property<span><a class="mark" href="#n_api_napi_set_property" id="n_api_napi_set_property">#</a></span></h5>
3552<div class="api_metadata">
3553<span>Added in: v8.0.0</span>
3554<span>N-API version: 1</span>
3555</div>
3556<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_property</span><span class="hljs-params">(napi_env env,
3557                              napi_value object,
3558                              napi_value key,
3559                              napi_value value)</span>;</code></pre>
3560<ul>
3561<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
3562<li><code>[in] object</code>: The object on which to set the property.</li>
3563<li><code>[in] key</code>: The name of the property to set.</li>
3564<li><code>[in] value</code>: The property value.</li>
3565</ul>
3566<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3567<p>This API set a property on the <code>Object</code> passed in.</p>
3568<h5>napi_get_property<span><a class="mark" href="#n_api_napi_get_property" id="n_api_napi_get_property">#</a></span></h5>
3569<div class="api_metadata">
3570<span>Added in: v8.0.0</span>
3571<span>N-API version: 1</span>
3572</div>
3573<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_property</span><span class="hljs-params">(napi_env env,
3574                              napi_value object,
3575                              napi_value key,
3576                              napi_value* result)</span>;</code></pre>
3577<ul>
3578<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
3579<li><code>[in] object</code>: The object from which to retrieve the property.</li>
3580<li><code>[in] key</code>: The name of the property to retrieve.</li>
3581<li><code>[out] result</code>: The value of the property.</li>
3582</ul>
3583<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3584<p>This API gets the requested property from the <code>Object</code> passed in.</p>
3585<h5>napi_has_property<span><a class="mark" href="#n_api_napi_has_property" id="n_api_napi_has_property">#</a></span></h5>
3586<div class="api_metadata">
3587<span>Added in: v8.0.0</span>
3588<span>N-API version: 1</span>
3589</div>
3590<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_property</span><span class="hljs-params">(napi_env env,
3591                              napi_value object,
3592                              napi_value key,
3593                              <span class="hljs-type">bool</span>* result)</span>;</code></pre>
3594<ul>
3595<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
3596<li><code>[in] object</code>: The object to query.</li>
3597<li><code>[in] key</code>: The name of the property whose existence to check.</li>
3598<li><code>[out] result</code>: Whether the property exists on the object or not.</li>
3599</ul>
3600<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3601<p>This API checks if the <code>Object</code> passed in has the named property.</p>
3602<h5>napi_delete_property<span><a class="mark" href="#n_api_napi_delete_property" id="n_api_napi_delete_property">#</a></span></h5>
3603<div class="api_metadata">
3604<span>Added in: v8.2.0</span>
3605<span>N-API version: 1</span>
3606</div>
3607<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_property</span><span class="hljs-params">(napi_env env,
3608                                 napi_value object,
3609                                 napi_value key,
3610                                 <span class="hljs-type">bool</span>* result)</span>;</code></pre>
3611<ul>
3612<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
3613<li><code>[in] object</code>: The object to query.</li>
3614<li><code>[in] key</code>: The name of the property to delete.</li>
3615<li><code>[out] result</code>: Whether the property deletion succeeded or not. <code>result</code> can
3616optionally be ignored by passing <code>NULL</code>.</li>
3617</ul>
3618<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3619<p>This API attempts to delete the <code>key</code> own property from <code>object</code>.</p>
3620<h5>napi_has_own_property<span><a class="mark" href="#n_api_napi_has_own_property" id="n_api_napi_has_own_property">#</a></span></h5>
3621<div class="api_metadata">
3622<span>Added in: v8.2.0</span>
3623<span>N-API version: 1</span>
3624</div>
3625<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_own_property</span><span class="hljs-params">(napi_env env,
3626                                  napi_value object,
3627                                  napi_value key,
3628                                  <span class="hljs-type">bool</span>* result)</span>;</code></pre>
3629<ul>
3630<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
3631<li><code>[in] object</code>: The object to query.</li>
3632<li><code>[in] key</code>: The name of the own property whose existence to check.</li>
3633<li><code>[out] result</code>: Whether the own property exists on the object or not.</li>
3634</ul>
3635<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3636<p>This API checks if the <code>Object</code> passed in has the named own property. <code>key</code> must
3637be a <code>string</code> or a <code>symbol</code>, or an error will be thrown. Node-API will not
3638perform any conversion between data types.</p>
3639<h5>napi_set_named_property<span><a class="mark" href="#n_api_napi_set_named_property" id="n_api_napi_set_named_property">#</a></span></h5>
3640<div class="api_metadata">
3641<span>Added in: v8.0.0</span>
3642<span>N-API version: 1</span>
3643</div>
3644<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_named_property</span><span class="hljs-params">(napi_env env,
3645                                    napi_value object,
3646                                    <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8Name,
3647                                    napi_value value)</span>;</code></pre>
3648<ul>
3649<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
3650<li><code>[in] object</code>: The object on which to set the property.</li>
3651<li><code>[in] utf8Name</code>: The name of the property to set.</li>
3652<li><code>[in] value</code>: The property value.</li>
3653</ul>
3654<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3655<p>This method is equivalent to calling <a href="#n_api_napi_set_property"><code>napi_set_property</code></a> with a <code>napi_value</code>
3656created from the string passed in as <code>utf8Name</code>.</p>
3657<h5>napi_get_named_property<span><a class="mark" href="#n_api_napi_get_named_property" id="n_api_napi_get_named_property">#</a></span></h5>
3658<div class="api_metadata">
3659<span>Added in: v8.0.0</span>
3660<span>N-API version: 1</span>
3661</div>
3662<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_named_property</span><span class="hljs-params">(napi_env env,
3663                                    napi_value object,
3664                                    <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8Name,
3665                                    napi_value* result)</span>;</code></pre>
3666<ul>
3667<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
3668<li><code>[in] object</code>: The object from which to retrieve the property.</li>
3669<li><code>[in] utf8Name</code>: The name of the property to get.</li>
3670<li><code>[out] result</code>: The value of the property.</li>
3671</ul>
3672<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3673<p>This method is equivalent to calling <a href="#n_api_napi_get_property"><code>napi_get_property</code></a> with a <code>napi_value</code>
3674created from the string passed in as <code>utf8Name</code>.</p>
3675<h5>napi_has_named_property<span><a class="mark" href="#n_api_napi_has_named_property" id="n_api_napi_has_named_property">#</a></span></h5>
3676<div class="api_metadata">
3677<span>Added in: v8.0.0</span>
3678<span>N-API version: 1</span>
3679</div>
3680<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_named_property</span><span class="hljs-params">(napi_env env,
3681                                    napi_value object,
3682                                    <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8Name,
3683                                    <span class="hljs-type">bool</span>* result)</span>;</code></pre>
3684<ul>
3685<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
3686<li><code>[in] object</code>: The object to query.</li>
3687<li><code>[in] utf8Name</code>: The name of the property whose existence to check.</li>
3688<li><code>[out] result</code>: Whether the property exists on the object or not.</li>
3689</ul>
3690<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3691<p>This method is equivalent to calling <a href="#n_api_napi_has_property"><code>napi_has_property</code></a> with a <code>napi_value</code>
3692created from the string passed in as <code>utf8Name</code>.</p>
3693<h5>napi_set_element<span><a class="mark" href="#n_api_napi_set_element" id="n_api_napi_set_element">#</a></span></h5>
3694<div class="api_metadata">
3695<span>Added in: v8.0.0</span>
3696<span>N-API version: 1</span>
3697</div>
3698<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_element</span><span class="hljs-params">(napi_env env,
3699                             napi_value object,
3700                             <span class="hljs-type">uint32_t</span> index,
3701                             napi_value value)</span>;</code></pre>
3702<ul>
3703<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
3704<li><code>[in] object</code>: The object from which to set the properties.</li>
3705<li><code>[in] index</code>: The index of the property to set.</li>
3706<li><code>[in] value</code>: The property value.</li>
3707</ul>
3708<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3709<p>This API sets and element on the <code>Object</code> passed in.</p>
3710<h5>napi_get_element<span><a class="mark" href="#n_api_napi_get_element" id="n_api_napi_get_element">#</a></span></h5>
3711<div class="api_metadata">
3712<span>Added in: v8.0.0</span>
3713<span>N-API version: 1</span>
3714</div>
3715<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_element</span><span class="hljs-params">(napi_env env,
3716                             napi_value object,
3717                             <span class="hljs-type">uint32_t</span> index,
3718                             napi_value* result)</span>;</code></pre>
3719<ul>
3720<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
3721<li><code>[in] object</code>: The object from which to retrieve the property.</li>
3722<li><code>[in] index</code>: The index of the property to get.</li>
3723<li><code>[out] result</code>: The value of the property.</li>
3724</ul>
3725<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3726<p>This API gets the element at the requested index.</p>
3727<h5>napi_has_element<span><a class="mark" href="#n_api_napi_has_element" id="n_api_napi_has_element">#</a></span></h5>
3728<div class="api_metadata">
3729<span>Added in: v8.0.0</span>
3730<span>N-API version: 1</span>
3731</div>
3732<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_element</span><span class="hljs-params">(napi_env env,
3733                             napi_value object,
3734                             <span class="hljs-type">uint32_t</span> index,
3735                             <span class="hljs-type">bool</span>* result)</span>;</code></pre>
3736<ul>
3737<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
3738<li><code>[in] object</code>: The object to query.</li>
3739<li><code>[in] index</code>: The index of the property whose existence to check.</li>
3740<li><code>[out] result</code>: Whether the property exists on the object or not.</li>
3741</ul>
3742<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3743<p>This API returns if the <code>Object</code> passed in has an element at the
3744requested index.</p>
3745<h5>napi_delete_element<span><a class="mark" href="#n_api_napi_delete_element" id="n_api_napi_delete_element">#</a></span></h5>
3746<div class="api_metadata">
3747<span>Added in: v8.2.0</span>
3748<span>N-API version: 1</span>
3749</div>
3750<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_element</span><span class="hljs-params">(napi_env env,
3751                                napi_value object,
3752                                <span class="hljs-type">uint32_t</span> index,
3753                                <span class="hljs-type">bool</span>* result)</span>;</code></pre>
3754<ul>
3755<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
3756<li><code>[in] object</code>: The object to query.</li>
3757<li><code>[in] index</code>: The index of the property to delete.</li>
3758<li><code>[out] result</code>: Whether the element deletion succeeded or not. <code>result</code> can
3759optionally be ignored by passing <code>NULL</code>.</li>
3760</ul>
3761<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3762<p>This API attempts to delete the specified <code>index</code> from <code>object</code>.</p>
3763<h5>napi_define_properties<span><a class="mark" href="#n_api_napi_define_properties" id="n_api_napi_define_properties">#</a></span></h5>
3764<div class="api_metadata">
3765<span>Added in: v8.0.0</span>
3766<span>N-API version: 1</span>
3767</div>
3768<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_properties</span><span class="hljs-params">(napi_env env,
3769                                   napi_value object,
3770                                   <span class="hljs-type">size_t</span> property_count,
3771                                   <span class="hljs-keyword">const</span> napi_property_descriptor* properties)</span>;</code></pre>
3772<ul>
3773<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
3774<li><code>[in] object</code>: The object from which to retrieve the properties.</li>
3775<li><code>[in] property_count</code>: The number of elements in the <code>properties</code> array.</li>
3776<li><code>[in] properties</code>: The array of property descriptors.</li>
3777</ul>
3778<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3779<p>This method allows the efficient definition of multiple properties on a given
3780object. The properties are defined using property descriptors (see
3781<a href="#n_api_napi_property_descriptor"><code>napi_property_descriptor</code></a>). Given an array of such property descriptors,
3782this API will set the properties on the object one at a time, as defined by
3783<code>DefineOwnProperty()</code> (described in <a href="https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc">Section 9.1.6</a> of the ECMA-262
3784specification).</p>
3785<h5>napi_object_freeze<span><a class="mark" href="#n_api_napi_object_freeze" id="n_api_napi_object_freeze">#</a></span></h5>
3786<div class="api_metadata">
3787<span>Added in: v14.14.0, v12.20.0</span>
3788<span>N-API version: 8</span>
3789</div>
3790<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_freeze</span><span class="hljs-params">(napi_env env,
3791                               napi_value object)</span>;</code></pre>
3792<ul>
3793<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
3794<li><code>[in] object</code>: The object to freeze.</li>
3795</ul>
3796<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3797<p>This method freezes a given object. This prevents new properties from
3798being added to it, existing properties from being removed, prevents
3799changing the enumerability, configurability, or writability of existing
3800properties, and prevents the values of existing properties from being changed.
3801It also prevents the object's prototype from being changed. This is described
3802in <a href="https://tc39.es/ecma262/#sec-object.freeze">Section 19.1.2.6</a> of the
3803ECMA-262 specification.</p>
3804<h5>napi_object_seal<span><a class="mark" href="#n_api_napi_object_seal" id="n_api_napi_object_seal">#</a></span></h5>
3805<div class="api_metadata">
3806<span>Added in: v14.14.0, v12.20.0</span>
3807<span>N-API version: 8</span>
3808</div>
3809<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_seal</span><span class="hljs-params">(napi_env env,
3810                             napi_value object)</span>;</code></pre>
3811<ul>
3812<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
3813<li><code>[in] object</code>: The object to seal.</li>
3814</ul>
3815<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3816<p>This method seals a given object. This prevents new properties from being
3817added to it, as well as marking all existing properties as non-configurable.
3818This is described in <a href="https://tc39.es/ecma262/#sec-object.seal">Section 19.1.2.20</a>
3819of the ECMA-262 specification.</p>
3820</section><section><h3>Working with JavaScript functions<span><a class="mark" href="#n_api_working_with_javascript_functions" id="n_api_working_with_javascript_functions">#</a></span></h3>
3821<p>Node-API provides a set of APIs that allow JavaScript code to
3822call back into native code. Node-APIs that support calling back
3823into native code take in a callback functions represented by
3824the <code>napi_callback</code> type. When the JavaScript VM calls back to
3825native code, the <code>napi_callback</code> function provided is invoked. The APIs
3826documented in this section allow the callback function to do the
3827following:</p>
3828<ul>
3829<li>Get information about the context in which the callback was invoked.</li>
3830<li>Get the arguments passed into the callback.</li>
3831<li>Return a <code>napi_value</code> back from the callback.</li>
3832</ul>
3833<p>Additionally, Node-API provides a set of functions which allow calling
3834JavaScript functions from native code. One can either call a function
3835like a regular JavaScript function call, or as a constructor
3836function.</p>
3837<p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> field of the
3838<code>napi_property_descriptor</code> items can be associated with <code>object</code> and freed
3839whenever <code>object</code> is garbage-collected by passing both <code>object</code> and the data to
3840<a href="#n_api_napi_add_finalizer"><code>napi_add_finalizer</code></a>.</p>
3841<h4>napi_call_function<span><a class="mark" href="#n_api_napi_call_function" id="n_api_napi_call_function">#</a></span></h4>
3842<div class="api_metadata">
3843<span>Added in: v8.0.0</span>
3844<span>N-API version: 1</span>
3845</div>
3846<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_call_function</span><span class="hljs-params">(napi_env env,
3847                                           napi_value recv,
3848                                           napi_value func,
3849                                           <span class="hljs-type">size_t</span> argc,
3850                                           <span class="hljs-keyword">const</span> napi_value* argv,
3851                                           napi_value* result)</span>;</code></pre>
3852<ul>
3853<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3854<li><code>[in] recv</code>: The <code>this</code> value passed to the called function.</li>
3855<li><code>[in] func</code>: <code>napi_value</code> representing the JavaScript function to be invoked.</li>
3856<li><code>[in] argc</code>: The count of elements in the <code>argv</code> array.</li>
3857<li><code>[in] argv</code>: Array of <code>napi_values</code> representing JavaScript values passed in
3858as arguments to the function.</li>
3859<li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned.</li>
3860</ul>
3861<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3862<p>This method allows a JavaScript function object to be called from a native
3863add-on. This is the primary mechanism of calling back <em>from</em> the add-on's
3864native code <em>into</em> JavaScript. For the special case of calling into JavaScript
3865after an async operation, see <a href="#n_api_napi_make_callback"><code>napi_make_callback</code></a>.</p>
3866<p>A sample use case might look as follows. Consider the following JavaScript
3867snippet:</p>
3868<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">AddTwo</span>(<span class="hljs-params">num</span>) {
3869  <span class="hljs-keyword">return</span> num + <span class="hljs-number">2</span>;
3870}</code></pre>
3871<p>Then, the above function can be invoked from a native add-on using the
3872following code:</p>
3873<pre><code class="language-c"><span class="hljs-comment">// Get the function named "AddTwo" on the global object</span>
3874napi_value global, add_two, arg;
3875napi_status status = napi_get_global(env, &#x26;global);
3876<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
3877
3878status = napi_get_named_property(env, global, <span class="hljs-string">"AddTwo"</span>, &#x26;add_two);
3879<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
3880
3881<span class="hljs-comment">// const arg = 1337</span>
3882status = napi_create_int32(env, <span class="hljs-number">1337</span>, &#x26;arg);
3883<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
3884
3885napi_value* argv = &#x26;arg;
3886<span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>;
3887
3888<span class="hljs-comment">// AddTwo(arg);</span>
3889napi_value return_val;
3890status = napi_call_function(env, global, add_two, argc, argv, &#x26;return_val);
3891<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
3892
3893<span class="hljs-comment">// Convert the result back to a native type</span>
3894<span class="hljs-type">int32_t</span> result;
3895status = napi_get_value_int32(env, return_val, &#x26;result);
3896<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;</code></pre>
3897<h4>napi_create_function<span><a class="mark" href="#n_api_napi_create_function" id="n_api_napi_create_function">#</a></span></h4>
3898<div class="api_metadata">
3899<span>Added in: v8.0.0</span>
3900<span>N-API version: 1</span>
3901</div>
3902<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_function</span><span class="hljs-params">(napi_env env,
3903                                 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8name,
3904                                 <span class="hljs-type">size_t</span> length,
3905                                 napi_callback cb,
3906                                 <span class="hljs-type">void</span>* data,
3907                                 napi_value* result)</span>;</code></pre>
3908<ul>
3909<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3910<li><code>[in] utf8Name</code>: The name of the function encoded as UTF8. This is visible
3911within JavaScript as the new function object's <code>name</code> property.</li>
3912<li><code>[in] length</code>: The length of the <code>utf8name</code> in bytes, or <code>NAPI_AUTO_LENGTH</code> if
3913it is null-terminated.</li>
3914<li><code>[in] cb</code>: The native function which should be called when this function
3915object is invoked. <a href="#n_api_napi_callback"><code>napi_callback</code></a> provides more details.</li>
3916<li><code>[in] data</code>: User-provided data context. This will be passed back into the
3917function when invoked later.</li>
3918<li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript function object for
3919the newly created function.</li>
3920</ul>
3921<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3922<p>This API allows an add-on author to create a function object in native code.
3923This is the primary mechanism to allow calling <em>into</em> the add-on's native code
3924<em>from</em> JavaScript.</p>
3925<p>The newly created function is not automatically visible from script after this
3926call. Instead, a property must be explicitly set on any object that is visible
3927to JavaScript, in order for the function to be accessible from script.</p>
3928<p>In order to expose a function as part of the
3929add-on's module exports, set the newly created function on the exports
3930object. A sample module might look as follows:</p>
3931<pre><code class="language-c">napi_value <span class="hljs-title function_">SayHello</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> {
3932  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello\n"</span>);
3933  <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
3934}
3935
3936napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> {
3937  napi_status status;
3938
3939  napi_value fn;
3940  status = napi_create_function(env, <span class="hljs-literal">NULL</span>, <span class="hljs-number">0</span>, SayHello, <span class="hljs-literal">NULL</span>, &#x26;fn);
3941  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
3942
3943  status = napi_set_named_property(env, exports, <span class="hljs-string">"sayHello"</span>, fn);
3944  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
3945
3946  <span class="hljs-keyword">return</span> exports;
3947}
3948
3949NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)</code></pre>
3950<p>Given the above code, the add-on can be used from JavaScript as follows:</p>
3951<pre><code class="language-js"><span class="hljs-keyword">const</span> myaddon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./addon'</span>);
3952myaddon.<span class="hljs-title function_">sayHello</span>();</code></pre>
3953<p>The string passed to <code>require()</code> is the name of the target in <code>binding.gyp</code>
3954responsible for creating the <code>.node</code> file.</p>
3955<p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> parameter can
3956be associated with the resulting JavaScript function (which is returned in the
3957<code>result</code> parameter) and freed whenever the function is garbage-collected by
3958passing both the JavaScript function and the data to <a href="#n_api_napi_add_finalizer"><code>napi_add_finalizer</code></a>.</p>
3959<p>JavaScript <code>Function</code>s are described in <a href="https://tc39.github.io/ecma262/#sec-function-objects">Section 19.2</a> of the ECMAScript
3960Language Specification.</p>
3961<h4>napi_get_cb_info<span><a class="mark" href="#n_api_napi_get_cb_info" id="n_api_napi_get_cb_info">#</a></span></h4>
3962<div class="api_metadata">
3963<span>Added in: v8.0.0</span>
3964<span>N-API version: 1</span>
3965</div>
3966<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_cb_info</span><span class="hljs-params">(napi_env env,
3967                             napi_callback_info cbinfo,
3968                             <span class="hljs-type">size_t</span>* argc,
3969                             napi_value* argv,
3970                             napi_value* thisArg,
3971                             <span class="hljs-type">void</span>** data)</span></code></pre>
3972<ul>
3973<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3974<li><code>[in] cbinfo</code>: The callback info passed into the callback function.</li>
3975<li><code>[in-out] argc</code>: Specifies the length of the provided <code>argv</code> array and
3976receives the actual count of arguments.</li>
3977<li><code>[out] argv</code>: Buffer to which the <code>napi_value</code> representing the arguments are
3978copied. If there are more arguments than the provided count, only the
3979requested number of arguments are copied. If there are fewer arguments
3980provided than claimed, the rest of <code>argv</code> is filled with <code>napi_value</code> values
3981that represent <code>undefined</code>.</li>
3982<li><code>[out] this</code>: Receives the JavaScript <code>this</code> argument for the call.</li>
3983<li><code>[out] data</code>: Receives the data pointer for the callback.</li>
3984</ul>
3985<p>Returns <code>napi_ok</code> if the API succeeded.</p>
3986<p>This method is used within a callback function to retrieve details about the
3987call like the arguments and the <code>this</code> pointer from a given callback info.</p>
3988<h4>napi_get_new_target<span><a class="mark" href="#n_api_napi_get_new_target" id="n_api_napi_get_new_target">#</a></span></h4>
3989<div class="api_metadata">
3990<span>Added in: v8.6.0</span>
3991<span>N-API version: 1</span>
3992</div>
3993<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_new_target</span><span class="hljs-params">(napi_env env,
3994                                napi_callback_info cbinfo,
3995                                napi_value* result)</span></code></pre>
3996<ul>
3997<li><code>[in] env</code>: The environment that the API is invoked under.</li>
3998<li><code>[in] cbinfo</code>: The callback info passed into the callback function.</li>
3999<li><code>[out] result</code>: The <code>new.target</code> of the constructor call.</li>
4000</ul>
4001<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4002<p>This API returns the <code>new.target</code> of the constructor call. If the current
4003callback is not a constructor call, the result is <code>NULL</code>.</p>
4004<h4>napi_new_instance<span><a class="mark" href="#n_api_napi_new_instance" id="n_api_napi_new_instance">#</a></span></h4>
4005<div class="api_metadata">
4006<span>Added in: v8.0.0</span>
4007<span>N-API version: 1</span>
4008</div>
4009<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_new_instance</span><span class="hljs-params">(napi_env env,
4010                              napi_value cons,
4011                              <span class="hljs-type">size_t</span> argc,
4012                              napi_value* argv,
4013                              napi_value* result)</span></code></pre>
4014<ul>
4015<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4016<li><code>[in] cons</code>: <code>napi_value</code> representing the JavaScript function to be invoked
4017as a constructor.</li>
4018<li><code>[in] argc</code>: The count of elements in the <code>argv</code> array.</li>
4019<li><code>[in] argv</code>: Array of JavaScript values as <code>napi_value</code> representing the
4020arguments to the constructor.</li>
4021<li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned,
4022which in this case is the constructed object.</li>
4023</ul>
4024<p>This method is used to instantiate a new JavaScript value using a given
4025<code>napi_value</code> that represents the constructor for the object. For example,
4026consider the following snippet:</p>
4027<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">MyObject</span>(<span class="hljs-params">param</span>) {
4028  <span class="hljs-variable language_">this</span>.<span class="hljs-property">param</span> = param;
4029}
4030
4031<span class="hljs-keyword">const</span> arg = <span class="hljs-string">'hello'</span>;
4032<span class="hljs-keyword">const</span> value = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyObject</span>(arg);</code></pre>
4033<p>The following can be approximated in Node-API using the following snippet:</p>
4034<pre><code class="language-c"><span class="hljs-comment">// Get the constructor function MyObject</span>
4035napi_value global, constructor, arg, value;
4036napi_status status = napi_get_global(env, &#x26;global);
4037<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4038
4039status = napi_get_named_property(env, global, <span class="hljs-string">"MyObject"</span>, &#x26;constructor);
4040<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4041
4042<span class="hljs-comment">// const arg = "hello"</span>
4043status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &#x26;arg);
4044<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
4045
4046napi_value* argv = &#x26;arg;
4047<span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>;
4048
4049<span class="hljs-comment">// const value = new MyObject(arg)</span>
4050status = napi_new_instance(env, constructor, argc, argv, &#x26;value);</code></pre>
4051<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4052</section><section><h3>Object wrap<span><a class="mark" href="#n_api_object_wrap" id="n_api_object_wrap">#</a></span></h3>
4053<p>Node-API offers a way to "wrap" C++ classes and instances so that the class
4054constructor and methods can be called from JavaScript.</p>
4055<ol>
4056<li>The <a href="#n_api_napi_define_class"><code>napi_define_class</code></a> API defines a JavaScript class with constructor,
4057static properties and methods, and instance properties and methods that
4058correspond to the C++ class.</li>
4059<li>When JavaScript code invokes the constructor, the constructor callback
4060uses <a href="#n_api_napi_wrap"><code>napi_wrap</code></a> to wrap a new C++ instance in a JavaScript object,
4061then returns the wrapper object.</li>
4062<li>When JavaScript code invokes a method or property accessor on the class,
4063the corresponding <code>napi_callback</code> C++ function is invoked. For an instance
4064callback, <a href="#n_api_napi_unwrap"><code>napi_unwrap</code></a> obtains the C++ instance that is the target of
4065the call.</li>
4066</ol>
4067<p>For wrapped objects it may be difficult to distinguish between a function
4068called on a class prototype and a function called on an instance of a class.
4069A common pattern used to address this problem is to save a persistent
4070reference to the class constructor for later <code>instanceof</code> checks.</p>
4071<pre><code class="language-c">napi_value MyClass_constructor = <span class="hljs-literal">NULL</span>;
4072status = napi_get_reference_value(env, MyClass::es_constructor, &#x26;MyClass_constructor);
4073assert(napi_ok == status);
4074<span class="hljs-type">bool</span> is_instance = <span class="hljs-literal">false</span>;
4075status = napi_instanceof(env, es_this, MyClass_constructor, &#x26;is_instance);
4076assert(napi_ok == status);
4077<span class="hljs-keyword">if</span> (is_instance) {
4078  <span class="hljs-comment">// napi_unwrap() ...</span>
4079} <span class="hljs-keyword">else</span> {
4080  <span class="hljs-comment">// otherwise...</span>
4081}</code></pre>
4082<p>The reference must be freed once it is no longer needed.</p>
4083<p>There are occasions where <code>napi_instanceof()</code> is insufficient for ensuring that
4084a JavaScript object is a wrapper for a certain native type. This is the case
4085especially when wrapped JavaScript objects are passed back into the addon via
4086static methods rather than as the <code>this</code> value of prototype methods. In such
4087cases there is a chance that they may be unwrapped incorrectly.</p>
4088<pre><code class="language-js"><span class="hljs-keyword">const</span> myAddon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/my_addon.node'</span>);
4089
4090<span class="hljs-comment">// `openDatabase()` returns a JavaScript object that wraps a native database</span>
4091<span class="hljs-comment">// handle.</span>
4092<span class="hljs-keyword">const</span> dbHandle = myAddon.<span class="hljs-title function_">openDatabase</span>();
4093
4094<span class="hljs-comment">// `query()` returns a JavaScript object that wraps a native query handle.</span>
4095<span class="hljs-keyword">const</span> queryHandle = myAddon.<span class="hljs-title function_">query</span>(dbHandle, <span class="hljs-string">'Gimme ALL the things!'</span>);
4096
4097<span class="hljs-comment">// There is an accidental error in the line below. The first parameter to</span>
4098<span class="hljs-comment">// `myAddon.queryHasRecords()` should be the database handle (`dbHandle`), not</span>
4099<span class="hljs-comment">// the query handle (`query`), so the correct condition for the while-loop</span>
4100<span class="hljs-comment">// should be</span>
4101<span class="hljs-comment">//</span>
4102<span class="hljs-comment">// myAddon.queryHasRecords(dbHandle, queryHandle)</span>
4103<span class="hljs-comment">//</span>
4104<span class="hljs-keyword">while</span> (myAddon.<span class="hljs-title function_">queryHasRecords</span>(queryHandle, dbHandle)) {
4105  <span class="hljs-comment">// retrieve records</span>
4106}</code></pre>
4107<p>In the above example <code>myAddon.queryHasRecords()</code> is a method that accepts two
4108arguments. The first is a database handle and the second is a query handle.
4109Internally, it unwraps the first argument and casts the resulting pointer to a
4110native database handle. It then unwraps the second argument and casts the
4111resulting pointer to a query handle. If the arguments are passed in the wrong
4112order, the casts will work, however, there is a good chance that the underlying
4113database operation will fail, or will even cause an invalid memory access.</p>
4114<p>To ensure that the pointer retrieved from the first argument is indeed a pointer
4115to a database handle and, similarly, that the pointer retrieved from the second
4116argument is indeed a pointer to a query handle, the implementation of
4117<code>queryHasRecords()</code> has to perform a type validation. Retaining the JavaScript
4118class constructor from which the database handle was instantiated and the
4119constructor from which the query handle was instantiated in <code>napi_ref</code>s can
4120help, because <code>napi_instanceof()</code> can then be used to ensure that the instances
4121passed into <code>queryHashRecords()</code> are indeed of the correct type.</p>
4122<p>Unfortunately, <code>napi_instanceof()</code> does not protect against prototype
4123manipulation. For example, the prototype of the database handle instance can be
4124set to the prototype of the constructor for query handle instances. In this
4125case, the database handle instance can appear as a query handle instance, and it
4126will pass the <code>napi_instanceof()</code> test for a query handle instance, while still
4127containing a pointer to a database handle.</p>
4128<p>To this end, Node-API provides type-tagging capabilities.</p>
4129<p>A type tag is a 128-bit integer unique to the addon. Node-API provides the
4130<code>napi_type_tag</code> structure for storing a type tag. When such a value is passed
4131along with a JavaScript object stored in a <code>napi_value</code> to
4132<code>napi_type_tag_object()</code>, the JavaScript object will be "marked" with the
4133type tag. The "mark" is invisible on the JavaScript side. When a JavaScript
4134object arrives into a native binding, <code>napi_check_object_type_tag()</code> can be used
4135along with the original type tag to determine whether the JavaScript object was
4136previously "marked" with the type tag. This creates a type-checking capability
4137of a higher fidelity than <code>napi_instanceof()</code> can provide, because such type-
4138tagging survives prototype manipulation and addon unloading/reloading.</p>
4139<p>Continuing the above example, the following skeleton addon implementation
4140illustrates the use of <code>napi_type_tag_object()</code> and
4141<code>napi_check_object_type_tag()</code>.</p>
4142<pre><code class="language-c"><span class="hljs-comment">// This value is the type tag for a database handle. The command</span>
4143<span class="hljs-comment">//</span>
4144<span class="hljs-comment">//   uuidgen | sed -r -e 's/-//g' -e 's/(.{16})(.*)/0x\1, 0x\2/'</span>
4145<span class="hljs-comment">//</span>
4146<span class="hljs-comment">// can be used to obtain the two values with which to initialize the structure.</span>
4147<span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> napi_type_tag DatabaseHandleTypeTag = {
4148  <span class="hljs-number">0x1edf75a38336451d</span>, <span class="hljs-number">0xa5ed9ce2e4c00c38</span>
4149};
4150
4151<span class="hljs-comment">// This value is the type tag for a query handle.</span>
4152<span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> napi_type_tag QueryHandleTypeTag = {
4153  <span class="hljs-number">0x9c73317f9fad44a3</span>, <span class="hljs-number">0x93c3920bf3b0ad6a</span>
4154};
4155
4156<span class="hljs-keyword">static</span> napi_value
4157<span class="hljs-title function_">openDatabase</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> {
4158  napi_status status;
4159  napi_value result;
4160
4161  <span class="hljs-comment">// Perform the underlying action which results in a database handle.</span>
4162  DatabaseHandle* dbHandle = open_database();
4163
4164  <span class="hljs-comment">// Create a new, empty JS object.</span>
4165  status = napi_create_object(env, &#x26;result);
4166  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4167
4168  <span class="hljs-comment">// Tag the object to indicate that it holds a pointer to a `DatabaseHandle`.</span>
4169  status = napi_type_tag_object(env, result, &#x26;DatabaseHandleTypeTag);
4170  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4171
4172  <span class="hljs-comment">// Store the pointer to the `DatabaseHandle` structure inside the JS object.</span>
4173  status = napi_wrap(env, result, dbHandle, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>);
4174  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4175
4176  <span class="hljs-keyword">return</span> result;
4177}
4178
4179<span class="hljs-comment">// Later when we receive a JavaScript object purporting to be a database handle</span>
4180<span class="hljs-comment">// we can use `napi_check_object_type_tag()` to ensure that it is indeed such a</span>
4181<span class="hljs-comment">// handle.</span>
4182
4183<span class="hljs-keyword">static</span> napi_value
4184<span class="hljs-title function_">query</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> {
4185  napi_status status;
4186  <span class="hljs-type">size_t</span> argc = <span class="hljs-number">2</span>;
4187  napi_value argv[<span class="hljs-number">2</span>];
4188  <span class="hljs-type">bool</span> is_db_handle;
4189
4190  status = napi_get_cb_info(env, info, &#x26;argc, argv, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>);
4191  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4192
4193  <span class="hljs-comment">// Check that the object passed as the first parameter has the previously</span>
4194  <span class="hljs-comment">// applied tag.</span>
4195  status = napi_check_object_type_tag(env,
4196                                      argv[<span class="hljs-number">0</span>],
4197                                      &#x26;DatabaseHandleTypeTag,
4198                                      &#x26;is_db_handle);
4199  <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4200
4201  <span class="hljs-comment">// Throw a `TypeError` if it doesn't.</span>
4202  <span class="hljs-keyword">if</span> (!is_db_handle) {
4203    <span class="hljs-comment">// Throw a TypeError.</span>
4204    <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4205  }
4206}</code></pre>
4207<h4>napi_define_class<span><a class="mark" href="#n_api_napi_define_class" id="n_api_napi_define_class">#</a></span></h4>
4208<div class="api_metadata">
4209<span>Added in: v8.0.0</span>
4210<span>N-API version: 1</span>
4211</div>
4212<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_class</span><span class="hljs-params">(napi_env env,
4213                              <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8name,
4214                              <span class="hljs-type">size_t</span> length,
4215                              napi_callback constructor,
4216                              <span class="hljs-type">void</span>* data,
4217                              <span class="hljs-type">size_t</span> property_count,
4218                              <span class="hljs-keyword">const</span> napi_property_descriptor* properties,
4219                              napi_value* result)</span>;</code></pre>
4220<ul>
4221<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4222<li><code>[in] utf8name</code>: Name of the JavaScript constructor function; When wrapping a
4223C++ class, we recommend for clarity that this name be the same as that of
4224the C++ class.</li>
4225<li><code>[in] length</code>: The length of the <code>utf8name</code> in bytes, or <code>NAPI_AUTO_LENGTH</code>
4226if it is null-terminated.</li>
4227<li><code>[in] constructor</code>: Callback function that handles constructing instances
4228of the class. When wrapping a C++ class, this method must be a static member
4229with the <a href="#n_api_napi_callback"><code>napi_callback</code></a> signature. A C++ class constructor cannot be
4230used. <a href="#n_api_napi_callback"><code>napi_callback</code></a> provides more details.</li>
4231<li><code>[in] data</code>: Optional data to be passed to the constructor callback as
4232the <code>data</code> property of the callback info.</li>
4233<li><code>[in] property_count</code>: Number of items in the <code>properties</code> array argument.</li>
4234<li><code>[in] properties</code>: Array of property descriptors describing static and
4235instance data properties, accessors, and methods on the class
4236See <code>napi_property_descriptor</code>.</li>
4237<li><code>[out] result</code>: A <code>napi_value</code> representing the constructor function for
4238the class.</li>
4239</ul>
4240<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4241<p>Defines a JavaScript class, including:</p>
4242<ul>
4243<li>A JavaScript constructor function that has the class name. When wrapping a
4244corresponding C++ class, the callback passed via <code>constructor</code> can be used to
4245instantiate a new C++ class instance, which can then be placed inside the
4246JavaScript object instance being constructed using <a href="#n_api_napi_wrap"><code>napi_wrap</code></a>.</li>
4247<li>Properties on the constructor function whose implementation can call
4248corresponding <em>static</em> data properties, accessors, and methods of the C++
4249class (defined by property descriptors with the <code>napi_static</code> attribute).</li>
4250<li>Properties on the constructor function's <code>prototype</code> object. When wrapping a
4251C++ class, <em>non-static</em> data properties, accessors, and methods of the C++
4252class can be called from the static functions given in the property
4253descriptors without the <code>napi_static</code> attribute after retrieving the C++ class
4254instance placed inside the JavaScript object instance by using
4255<a href="#n_api_napi_unwrap"><code>napi_unwrap</code></a>.</li>
4256</ul>
4257<p>When wrapping a C++ class, the C++ constructor callback passed via <code>constructor</code>
4258should be a static method on the class that calls the actual class constructor,
4259then wraps the new C++ instance in a JavaScript object, and returns the wrapper
4260object. See <a href="#n_api_napi_wrap"><code>napi_wrap</code></a> for details.</p>
4261<p>The JavaScript constructor function returned from <a href="#n_api_napi_define_class"><code>napi_define_class</code></a> is
4262often saved and used later to construct new instances of the class from native
4263code, and/or to check whether provided values are instances of the class. In
4264that case, to prevent the function value from being garbage-collected, a
4265strong persistent reference to it can be created using
4266<a href="#n_api_napi_create_reference"><code>napi_create_reference</code></a>, ensuring that the reference count is kept >= 1.</p>
4267<p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> parameter or via
4268the <code>data</code> field of the <code>napi_property_descriptor</code> array items can be associated
4269with the resulting JavaScript constructor (which is returned in the <code>result</code>
4270parameter) and freed whenever the class is garbage-collected by passing both
4271the JavaScript function and the data to <a href="#n_api_napi_add_finalizer"><code>napi_add_finalizer</code></a>.</p>
4272<h4>napi_wrap<span><a class="mark" href="#n_api_napi_wrap" id="n_api_napi_wrap">#</a></span></h4>
4273<div class="api_metadata">
4274<span>Added in: v8.0.0</span>
4275<span>N-API version: 1</span>
4276</div>
4277<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_wrap</span><span class="hljs-params">(napi_env env,
4278                      napi_value js_object,
4279                      <span class="hljs-type">void</span>* native_object,
4280                      napi_finalize finalize_cb,
4281                      <span class="hljs-type">void</span>* finalize_hint,
4282                      napi_ref* result)</span>;</code></pre>
4283<ul>
4284<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4285<li><code>[in] js_object</code>: The JavaScript object that will be the wrapper for the
4286native object.</li>
4287<li><code>[in] native_object</code>: The native instance that will be wrapped in the
4288JavaScript object.</li>
4289<li><code>[in] finalize_cb</code>: Optional native callback that can be used to free the
4290native instance when the JavaScript object is ready for garbage-collection.
4291<a href="#n_api_napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
4292<li><code>[in] finalize_hint</code>: Optional contextual hint that is passed to the
4293finalize callback.</li>
4294<li><code>[out] result</code>: Optional reference to the wrapped object.</li>
4295</ul>
4296<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4297<p>Wraps a native instance in a JavaScript object. The native instance can be
4298retrieved later using <code>napi_unwrap()</code>.</p>
4299<p>When JavaScript code invokes a constructor for a class that was defined using
4300<code>napi_define_class()</code>, the <code>napi_callback</code> for the constructor is invoked.
4301After constructing an instance of the native class, the callback must then call
4302<code>napi_wrap()</code> to wrap the newly constructed instance in the already-created
4303JavaScript object that is the <code>this</code> argument to the constructor callback.
4304(That <code>this</code> object was created from the constructor function's <code>prototype</code>,
4305so it already has definitions of all the instance properties and methods.)</p>
4306<p>Typically when wrapping a class instance, a finalize callback should be
4307provided that simply deletes the native instance that is received as the <code>data</code>
4308argument to the finalize callback.</p>
4309<p>The optional returned reference is initially a weak reference, meaning it
4310has a reference count of 0. Typically this reference count would be incremented
4311temporarily during async operations that require the instance to remain valid.</p>
4312<p><em>Caution</em>: The optional returned reference (if obtained) should be deleted via
4313<a href="#n_api_napi_delete_reference"><code>napi_delete_reference</code></a> ONLY in response to the finalize callback
4314invocation. If it is deleted before then, then the finalize callback may never
4315be invoked. Therefore, when obtaining a reference a finalize callback is also
4316required in order to enable correct disposal of the reference.</p>
4317<p>Calling <code>napi_wrap()</code> a second time on an object will return an error. To
4318associate another native instance with the object, use <code>napi_remove_wrap()</code>
4319first.</p>
4320<h4>napi_unwrap<span><a class="mark" href="#n_api_napi_unwrap" id="n_api_napi_unwrap">#</a></span></h4>
4321<div class="api_metadata">
4322<span>Added in: v8.0.0</span>
4323<span>N-API version: 1</span>
4324</div>
4325<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_unwrap</span><span class="hljs-params">(napi_env env,
4326                        napi_value js_object,
4327                        <span class="hljs-type">void</span>** result)</span>;</code></pre>
4328<ul>
4329<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4330<li><code>[in] js_object</code>: The object associated with the native instance.</li>
4331<li><code>[out] result</code>: Pointer to the wrapped native instance.</li>
4332</ul>
4333<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4334<p>Retrieves a native instance that was previously wrapped in a JavaScript
4335object using <code>napi_wrap()</code>.</p>
4336<p>When JavaScript code invokes a method or property accessor on the class, the
4337corresponding <code>napi_callback</code> is invoked. If the callback is for an instance
4338method or accessor, then the <code>this</code> argument to the callback is the wrapper
4339object; the wrapped C++ instance that is the target of the call can be obtained
4340then by calling <code>napi_unwrap()</code> on the wrapper object.</p>
4341<h4>napi_remove_wrap<span><a class="mark" href="#n_api_napi_remove_wrap" id="n_api_napi_remove_wrap">#</a></span></h4>
4342<div class="api_metadata">
4343<span>Added in: v8.5.0</span>
4344<span>N-API version: 1</span>
4345</div>
4346<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_remove_wrap</span><span class="hljs-params">(napi_env env,
4347                             napi_value js_object,
4348                             <span class="hljs-type">void</span>** result)</span>;</code></pre>
4349<ul>
4350<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4351<li><code>[in] js_object</code>: The object associated with the native instance.</li>
4352<li><code>[out] result</code>: Pointer to the wrapped native instance.</li>
4353</ul>
4354<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4355<p>Retrieves a native instance that was previously wrapped in the JavaScript
4356object <code>js_object</code> using <code>napi_wrap()</code> and removes the wrapping. If a finalize
4357callback was associated with the wrapping, it will no longer be called when the
4358JavaScript object becomes garbage-collected.</p>
4359<h4>napi_type_tag_object<span><a class="mark" href="#n_api_napi_type_tag_object" id="n_api_napi_type_tag_object">#</a></span></h4>
4360<div class="api_metadata">
4361<span>Added in: v14.8.0, v12.19.0</span>
4362<span>N-API version: 8</span>
4363</div>
4364<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_type_tag_object</span><span class="hljs-params">(napi_env env,
4365                                 napi_value js_object,
4366                                 <span class="hljs-keyword">const</span> napi_type_tag* type_tag)</span>;</code></pre>
4367<ul>
4368<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4369<li><code>[in] js_object</code>: The JavaScript object to be marked.</li>
4370<li><code>[in] type_tag</code>: The tag with which the object is to be marked.</li>
4371</ul>
4372<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4373<p>Associates the value of the <code>type_tag</code> pointer with the JavaScript object.
4374<code>napi_check_object_type_tag()</code> can then be used to compare the tag that was
4375attached to the object with one owned by the addon to ensure that the object
4376has the right type.</p>
4377<p>If the object already has an associated type tag, this API will return
4378<code>napi_invalid_arg</code>.</p>
4379<h4>napi_check_object_type_tag<span><a class="mark" href="#n_api_napi_check_object_type_tag" id="n_api_napi_check_object_type_tag">#</a></span></h4>
4380<div class="api_metadata">
4381<span>Added in: v14.8.0, v12.19.0</span>
4382<span>N-API version: 8</span>
4383</div>
4384<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_check_object_type_tag</span><span class="hljs-params">(napi_env env,
4385                                       napi_value js_object,
4386                                       <span class="hljs-keyword">const</span> napi_type_tag* type_tag,
4387                                       <span class="hljs-type">bool</span>* result)</span>;</code></pre>
4388<ul>
4389<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4390<li><code>[in] js_object</code>: The JavaScript object whose type tag to examine.</li>
4391<li><code>[in] type_tag</code>: The tag with which to compare any tag found on the object.</li>
4392<li><code>[out] result</code>: Whether the type tag given matched the type tag on the
4393object. <code>false</code> is also returned if no type tag was found on the object.</li>
4394</ul>
4395<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4396<p>Compares the pointer given as <code>type_tag</code> with any that can be found on
4397<code>js_object</code>. If no tag is found on <code>js_object</code> or, if a tag is found but it does
4398not match <code>type_tag</code>, then <code>result</code> is set to <code>false</code>. If a tag is found and it
4399matches <code>type_tag</code>, then <code>result</code> is set to <code>true</code>.</p>
4400<h4>napi_add_finalizer<span><a class="mark" href="#n_api_napi_add_finalizer" id="n_api_napi_add_finalizer">#</a></span></h4>
4401<div class="api_metadata">
4402<span>Added in: v8.0.0</span>
4403<span>N-API version: 5</span>
4404</div>
4405<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_add_finalizer</span><span class="hljs-params">(napi_env env,
4406                               napi_value js_object,
4407                               <span class="hljs-type">void</span>* native_object,
4408                               napi_finalize finalize_cb,
4409                               <span class="hljs-type">void</span>* finalize_hint,
4410                               napi_ref* result)</span>;</code></pre>
4411<ul>
4412<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4413<li><code>[in] js_object</code>: The JavaScript object to which the native data will be
4414attached.</li>
4415<li><code>[in] native_object</code>: The native data that will be attached to the JavaScript
4416object.</li>
4417<li><code>[in] finalize_cb</code>: Native callback that will be used to free the
4418native data when the JavaScript object is ready for garbage-collection.
4419<a href="#n_api_napi_finalize"><code>napi_finalize</code></a> provides more details.</li>
4420<li><code>[in] finalize_hint</code>: Optional contextual hint that is passed to the
4421finalize callback.</li>
4422<li><code>[out] result</code>: Optional reference to the JavaScript object.</li>
4423</ul>
4424<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4425<p>Adds a <code>napi_finalize</code> callback which will be called when the JavaScript object
4426in <code>js_object</code> is ready for garbage collection. This API is similar to
4427<code>napi_wrap()</code> except that:</p>
4428<ul>
4429<li>the native data cannot be retrieved later using <code>napi_unwrap()</code>,</li>
4430<li>nor can it be removed later using <code>napi_remove_wrap()</code>, and</li>
4431<li>the API can be called multiple times with different data items in order to
4432attach each of them to the JavaScript object, and</li>
4433<li>the object manipulated by the API can be used with <code>napi_wrap()</code>.</li>
4434</ul>
4435<p><em>Caution</em>: The optional returned reference (if obtained) should be deleted via
4436<a href="#n_api_napi_delete_reference"><code>napi_delete_reference</code></a> ONLY in response to the finalize callback
4437invocation. If it is deleted before then, then the finalize callback may never
4438be invoked. Therefore, when obtaining a reference a finalize callback is also
4439required in order to enable correct disposal of the reference.</p>
4440</section><section><h3>Simple asynchronous operations<span><a class="mark" href="#n_api_simple_asynchronous_operations" id="n_api_simple_asynchronous_operations">#</a></span></h3>
4441<p>Addon modules often need to leverage async helpers from libuv as part of their
4442implementation. This allows them to schedule work to be executed asynchronously
4443so that their methods can return in advance of the work being completed. This
4444allows them to avoid blocking overall execution of the Node.js application.</p>
4445<p>Node-API provides an ABI-stable interface for these
4446supporting functions which covers the most common asynchronous use cases.</p>
4447<p>Node-API defines the <code>napi_async_work</code> structure which is used to manage
4448asynchronous workers. Instances are created/deleted with
4449<a href="#n_api_napi_create_async_work"><code>napi_create_async_work</code></a> and <a href="#n_api_napi_delete_async_work"><code>napi_delete_async_work</code></a>.</p>
4450<p>The <code>execute</code> and <code>complete</code> callbacks are functions that will be
4451invoked when the executor is ready to execute and when it completes its
4452task respectively.</p>
4453<p>The <code>execute</code> function should avoid making any Node-API calls
4454that could result in the execution of JavaScript or interaction with
4455JavaScript objects. Most often, any code that needs to make Node-API
4456calls should be made in <code>complete</code> callback instead.
4457Avoid using the <code>napi_env</code> parameter in the execute callback as
4458it will likely execute JavaScript.</p>
4459<p>These functions implement the following interfaces:</p>
4460<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_execute_callback)</span><span class="hljs-params">(napi_env env,
4461                                            <span class="hljs-type">void</span>* data)</span>;
4462<span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_complete_callback)</span><span class="hljs-params">(napi_env env,
4463                                             napi_status status,
4464                                             <span class="hljs-type">void</span>* data)</span>;</code></pre>
4465<p>When these methods are invoked, the <code>data</code> parameter passed will be the
4466addon-provided <code>void*</code> data that was passed into the
4467<code>napi_create_async_work</code> call.</p>
4468<p>Once created the async worker can be queued
4469for execution using the <a href="#n_api_napi_queue_async_work"><code>napi_queue_async_work</code></a> function:</p>
4470<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work</span><span class="hljs-params">(napi_env env,
4471                                  napi_async_work work)</span>;</code></pre>
4472<p><a href="#n_api_napi_cancel_async_work"><code>napi_cancel_async_work</code></a> can be used if the work needs
4473to be cancelled before the work has started execution.</p>
4474<p>After calling <a href="#n_api_napi_cancel_async_work"><code>napi_cancel_async_work</code></a>, the <code>complete</code> callback
4475will be invoked with a status value of <code>napi_cancelled</code>.
4476The work should not be deleted before the <code>complete</code>
4477callback invocation, even when it was cancelled.</p>
4478<h4>napi_create_async_work<span><a class="mark" href="#n_api_napi_create_async_work" id="n_api_napi_create_async_work">#</a></span></h4>
4479<div class="api_metadata">
4480<details class="changelog"><summary>History</summary>
4481<table>
4482<tbody><tr><th>Version</th><th>Changes</th></tr>
4483<tr><td>v8.6.0</td>
4484<td><p>Added <code>async_resource</code> and <code>async_resource_name</code> parameters.</p></td></tr>
4485<tr><td>v8.0.0</td>
4486<td><p><span>Added in: v8.0.0</span></p></td></tr>
4487</tbody></table>
4488</details>
4489<span>N-API version: 1</span>
4490</div>
4491<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_async_work</span><span class="hljs-params">(napi_env env,
4492                                   napi_value async_resource,
4493                                   napi_value async_resource_name,
4494                                   napi_async_execute_callback execute,
4495                                   napi_async_complete_callback complete,
4496                                   <span class="hljs-type">void</span>* data,
4497                                   napi_async_work* result)</span>;</code></pre>
4498<ul>
4499<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4500<li><code>[in] async_resource</code>: An optional object associated with the async work
4501that will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource"><code>init</code> hooks</a>.</li>
4502<li><code>[in] async_resource_name</code>: Identifier for the kind of resource that is being
4503provided for diagnostic information exposed by the <code>async_hooks</code> API.</li>
4504<li><code>[in] execute</code>: The native function which should be called to execute the
4505logic asynchronously. The given function is called from a worker pool thread
4506and can execute in parallel with the main event loop thread.</li>
4507<li><code>[in] complete</code>: The native function which will be called when the
4508asynchronous logic is completed or is cancelled. The given function is called
4509from the main event loop thread. <a href="#n_api_napi_async_complete_callback"><code>napi_async_complete_callback</code></a> provides
4510more details.</li>
4511<li><code>[in] data</code>: User-provided data context. This will be passed back into the
4512execute and complete functions.</li>
4513<li><code>[out] result</code>: <code>napi_async_work*</code> which is the handle to the newly created
4514async work.</li>
4515</ul>
4516<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4517<p>This API allocates a work object that is used to execute logic asynchronously.
4518It should be freed using <a href="#n_api_napi_delete_async_work"><code>napi_delete_async_work</code></a> once the work is no longer
4519required.</p>
4520<p><code>async_resource_name</code> should be a null-terminated, UTF-8-encoded string.</p>
4521<p>The <code>async_resource_name</code> identifier is provided by the user and should be
4522representative of the type of async work being performed. It is also recommended
4523to apply namespacing to the identifier, e.g. by including the module name. See
4524the <a href="async_hooks.html#async_hooks_type"><code>async_hooks</code> documentation</a> for more information.</p>
4525<h4>napi_delete_async_work<span><a class="mark" href="#n_api_napi_delete_async_work" id="n_api_napi_delete_async_work">#</a></span></h4>
4526<div class="api_metadata">
4527<span>Added in: v8.0.0</span>
4528<span>N-API version: 1</span>
4529</div>
4530<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_async_work</span><span class="hljs-params">(napi_env env,
4531                                   napi_async_work work)</span>;</code></pre>
4532<ul>
4533<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4534<li><code>[in] work</code>: The handle returned by the call to <code>napi_create_async_work</code>.</li>
4535</ul>
4536<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4537<p>This API frees a previously allocated work object.</p>
4538<p>This API can be called even if there is a pending JavaScript exception.</p>
4539<h4>napi_queue_async_work<span><a class="mark" href="#n_api_napi_queue_async_work" id="n_api_napi_queue_async_work">#</a></span></h4>
4540<div class="api_metadata">
4541<span>Added in: v8.0.0</span>
4542<span>N-API version: 1</span>
4543</div>
4544<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work</span><span class="hljs-params">(napi_env env,
4545                                  napi_async_work work)</span>;</code></pre>
4546<ul>
4547<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4548<li><code>[in] work</code>: The handle returned by the call to <code>napi_create_async_work</code>.</li>
4549</ul>
4550<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4551<p>This API requests that the previously allocated work be scheduled
4552for execution. Once it returns successfully, this API must not be called again
4553with the same <code>napi_async_work</code> item or the result will be undefined.</p>
4554<h4>napi_cancel_async_work<span><a class="mark" href="#n_api_napi_cancel_async_work" id="n_api_napi_cancel_async_work">#</a></span></h4>
4555<div class="api_metadata">
4556<span>Added in: v8.0.0</span>
4557<span>N-API version: 1</span>
4558</div>
4559<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_cancel_async_work</span><span class="hljs-params">(napi_env env,
4560                                   napi_async_work work)</span>;</code></pre>
4561<ul>
4562<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4563<li><code>[in] work</code>: The handle returned by the call to <code>napi_create_async_work</code>.</li>
4564</ul>
4565<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4566<p>This API cancels queued work if it has not yet
4567been started. If it has already started executing, it cannot be
4568cancelled and <code>napi_generic_failure</code> will be returned. If successful,
4569the <code>complete</code> callback will be invoked with a status value of
4570<code>napi_cancelled</code>. The work should not be deleted before the <code>complete</code>
4571callback invocation, even if it has been successfully cancelled.</p>
4572<p>This API can be called even if there is a pending JavaScript exception.</p>
4573</section><section><h3>Custom asynchronous operations<span><a class="mark" href="#n_api_custom_asynchronous_operations" id="n_api_custom_asynchronous_operations">#</a></span></h3>
4574<p>The simple asynchronous work APIs above may not be appropriate for every
4575scenario. When using any other asynchronous mechanism, the following APIs
4576are necessary to ensure an asynchronous operation is properly tracked by
4577the runtime.</p>
4578<h4>napi_async_init<span><a class="mark" href="#n_api_napi_async_init" id="n_api_napi_async_init">#</a></span></h4>
4579<div class="api_metadata">
4580<span>Added in: v8.6.0</span>
4581<span>N-API version: 1</span>
4582</div>
4583<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_init</span><span class="hljs-params">(napi_env env,
4584                            napi_value async_resource,
4585                            napi_value async_resource_name,
4586                            napi_async_context* result)</span></code></pre>
4587<ul>
4588<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4589<li><code>[in] async_resource</code>: Object associated with the async work
4590that will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource"><code>init</code> hooks</a> and can be
4591accessed by <a href="async_hooks.html#async_hooks_async_hooks_executionasyncresource"><code>async_hooks.executionAsyncResource()</code></a>.</li>
4592<li><code>[in] async_resource_name</code>: Identifier for the kind of resource that is being
4593provided for diagnostic information exposed by the <code>async_hooks</code> API.</li>
4594<li><code>[out] result</code>: The initialized async context.</li>
4595</ul>
4596<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4597<p>The <code>async_resource</code> object needs to be kept alive until
4598<a href="#n_api_napi_async_destroy"><code>napi_async_destroy</code></a> to keep <code>async_hooks</code> related API acts correctly. In
4599order to retain ABI compatibility with previous versions, <code>napi_async_context</code>s
4600are not maintaining the strong reference to the <code>async_resource</code> objects to
4601avoid introducing causing memory leaks. However, if the <code>async_resource</code> is
4602garbage collected by JavaScript engine before the <code>napi_async_context</code> was
4603destroyed by <code>napi_async_destroy</code>, calling <code>napi_async_context</code> related APIs
4604like <a href="#n_api_napi_open_callback_scope"><code>napi_open_callback_scope</code></a> and <a href="#n_api_napi_make_callback"><code>napi_make_callback</code></a> can cause
4605problems like loss of async context when using the <code>AsyncLocalStoage</code> API.</p>
4606<p>In order to retain ABI compatibility with previous versions, passing <code>NULL</code>
4607for <code>async_resource</code> does not result in an error. However, this is not
4608recommended as this will result poor results with  <code>async_hooks</code>
4609<a href="async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource"><code>init</code> hooks</a> and <code>async_hooks.executionAsyncResource()</code> as the resource is
4610now required by the underlying <code>async_hooks</code> implementation in order to provide
4611the linkage between async callbacks.</p>
4612<h4>napi_async_destroy<span><a class="mark" href="#n_api_napi_async_destroy" id="n_api_napi_async_destroy">#</a></span></h4>
4613<div class="api_metadata">
4614<span>Added in: v8.6.0</span>
4615<span>N-API version: 1</span>
4616</div>
4617<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_destroy</span><span class="hljs-params">(napi_env env,
4618                               napi_async_context async_context)</span>;</code></pre>
4619<ul>
4620<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4621<li><code>[in] async_context</code>: The async context to be destroyed.</li>
4622</ul>
4623<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4624<p>This API can be called even if there is a pending JavaScript exception.</p>
4625<h4>napi_make_callback<span><a class="mark" href="#n_api_napi_make_callback" id="n_api_napi_make_callback">#</a></span></h4>
4626<div class="api_metadata">
4627<details class="changelog"><summary>History</summary>
4628<table>
4629<tbody><tr><th>Version</th><th>Changes</th></tr>
4630<tr><td>v8.6.0</td>
4631<td><p>Added <code>async_context</code> parameter.</p></td></tr>
4632<tr><td>v8.0.0</td>
4633<td><p><span>Added in: v8.0.0</span></p></td></tr>
4634</tbody></table>
4635</details>
4636<span>N-API version: 1</span>
4637</div>
4638<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_make_callback</span><span class="hljs-params">(napi_env env,
4639                                           napi_async_context async_context,
4640                                           napi_value recv,
4641                                           napi_value func,
4642                                           <span class="hljs-type">size_t</span> argc,
4643                                           <span class="hljs-keyword">const</span> napi_value* argv,
4644                                           napi_value* result)</span>;</code></pre>
4645<ul>
4646<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4647<li><code>[in] async_context</code>: Context for the async operation that is
4648invoking the callback. This should normally be a value previously
4649obtained from <a href="#n_api_napi_async_init"><code>napi_async_init</code></a>.
4650In order to retain ABI compatibility with previous versions, passing <code>NULL</code>
4651for <code>async_context</code> does not result in an error. However, this results
4652in incorrect operation of async hooks. Potential issues include loss of
4653async context when using the <code>AsyncLocalStorage</code> API.</li>
4654<li><code>[in] recv</code>: The <code>this</code> value passed to the called function.</li>
4655<li><code>[in] func</code>: <code>napi_value</code> representing the JavaScript function to be invoked.</li>
4656<li><code>[in] argc</code>: The count of elements in the <code>argv</code> array.</li>
4657<li><code>[in] argv</code>: Array of JavaScript values as <code>napi_value</code> representing the
4658arguments to the function.</li>
4659<li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned.</li>
4660</ul>
4661<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4662<p>This method allows a JavaScript function object to be called from a native
4663add-on. This API is similar to <code>napi_call_function</code>. However, it is used to call
4664<em>from</em> native code back <em>into</em> JavaScript <em>after</em> returning from an async
4665operation (when there is no other script on the stack). It is a fairly simple
4666wrapper around <code>node::MakeCallback</code>.</p>
4667<p>Note it is <em>not</em> necessary to use <code>napi_make_callback</code> from within a
4668<code>napi_async_complete_callback</code>; in that situation the callback's async
4669context has already been set up, so a direct call to <code>napi_call_function</code>
4670is sufficient and appropriate. Use of the <code>napi_make_callback</code> function
4671may be required when implementing custom async behavior that does not use
4672<code>napi_create_async_work</code>.</p>
4673<p>Any <code>process.nextTick</code>s or Promises scheduled on the microtask queue by
4674JavaScript during the callback are ran before returning back to C/C++.</p>
4675<h4>napi_open_callback_scope<span><a class="mark" href="#n_api_napi_open_callback_scope" id="n_api_napi_open_callback_scope">#</a></span></h4>
4676<div class="api_metadata">
4677<span>Added in: v9.6.0</span>
4678<span>N-API version: 3</span>
4679</div>
4680<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_open_callback_scope</span><span class="hljs-params">(napi_env env,
4681                                                 napi_value resource_object,
4682                                                 napi_async_context context,
4683                                                 napi_callback_scope* result)</span></code></pre>
4684<ul>
4685<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4686<li><code>[in] resource_object</code>: An object associated with the async work
4687that will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource"><code>init</code> hooks</a>. This
4688parameter has been deprecated and is ignored at runtime. Use the
4689<code>async_resource</code> parameter in <a href="#n_api_napi_async_init"><code>napi_async_init</code></a> instead.</li>
4690<li><code>[in] context</code>: Context for the async operation that is invoking the callback.
4691This should be a value previously obtained from <a href="#n_api_napi_async_init"><code>napi_async_init</code></a>.</li>
4692<li><code>[out] result</code>: The newly created scope.</li>
4693</ul>
4694<p>There are cases (for example, resolving promises) where it is
4695necessary to have the equivalent of the scope associated with a callback
4696in place when making certain Node-API calls. If there is no other script on
4697the stack the <a href="#n_api_napi_open_callback_scope"><code>napi_open_callback_scope</code></a> and
4698<a href="#n_api_napi_close_callback_scope"><code>napi_close_callback_scope</code></a> functions can be used to open/close
4699the required scope.</p>
4700<h4>napi_close_callback_scope<span><a class="mark" href="#n_api_napi_close_callback_scope" id="n_api_napi_close_callback_scope">#</a></span></h4>
4701<div class="api_metadata">
4702<span>Added in: v9.6.0</span>
4703<span>N-API version: 3</span>
4704</div>
4705<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_close_callback_scope</span><span class="hljs-params">(napi_env env,
4706                                                  napi_callback_scope scope)</span></code></pre>
4707<ul>
4708<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4709<li><code>[in] scope</code>: The scope to be closed.</li>
4710</ul>
4711<p>This API can be called even if there is a pending JavaScript exception.</p>
4712</section><section><h3>Version management<span><a class="mark" href="#n_api_version_management" id="n_api_version_management">#</a></span></h3>
4713<h4>napi_get_node_version<span><a class="mark" href="#n_api_napi_get_node_version" id="n_api_napi_get_node_version">#</a></span></h4>
4714<div class="api_metadata">
4715<span>Added in: v8.4.0</span>
4716<span>N-API version: 1</span>
4717</div>
4718<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span>
4719  <span class="hljs-type">uint32_t</span> major;
4720  <span class="hljs-type">uint32_t</span> minor;
4721  <span class="hljs-type">uint32_t</span> patch;
4722  <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* release;
4723} napi_node_version;
4724
4725napi_status <span class="hljs-title function_">napi_get_node_version</span><span class="hljs-params">(napi_env env,
4726                                  <span class="hljs-keyword">const</span> napi_node_version** version)</span>;</code></pre>
4727<ul>
4728<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4729<li><code>[out] version</code>: A pointer to version information for Node.js itself.</li>
4730</ul>
4731<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4732<p>This function fills the <code>version</code> struct with the major, minor, and patch
4733version of Node.js that is currently running, and the <code>release</code> field with the
4734value of <a href="process.html#process_process_release"><code>process.release.name</code></a>.</p>
4735<p>The returned buffer is statically allocated and does not need to be freed.</p>
4736<h4>napi_get_version<span><a class="mark" href="#n_api_napi_get_version" id="n_api_napi_get_version">#</a></span></h4>
4737<div class="api_metadata">
4738<span>Added in: v8.0.0</span>
4739<span>N-API version: 1</span>
4740</div>
4741<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_version</span><span class="hljs-params">(napi_env env,
4742                             <span class="hljs-type">uint32_t</span>* result)</span>;</code></pre>
4743<ul>
4744<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4745<li><code>[out] result</code>: The highest version of Node-API supported.</li>
4746</ul>
4747<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4748<p>This API returns the highest Node-API version supported by the
4749Node.js runtime. Node-API is planned to be additive such that
4750newer releases of Node.js may support additional API functions.
4751In order to allow an addon to use a newer function when running with
4752versions of Node.js that support it, while providing
4753fallback behavior when running with Node.js versions that don't
4754support it:</p>
4755<ul>
4756<li>Call <code>napi_get_version()</code> to determine if the API is available.</li>
4757<li>If available, dynamically load a pointer to the function using <code>uv_dlsym()</code>.</li>
4758<li>Use the dynamically loaded pointer to invoke the function.</li>
4759<li>If the function is not available, provide an alternate implementation
4760that does not use the function.</li>
4761</ul>
4762</section><section><h3>Memory management<span><a class="mark" href="#n_api_memory_management" id="n_api_memory_management">#</a></span></h3>
4763<h4>napi_adjust_external_memory<span><a class="mark" href="#n_api_napi_adjust_external_memory" id="n_api_napi_adjust_external_memory">#</a></span></h4>
4764<div class="api_metadata">
4765<span>Added in: v8.5.0</span>
4766<span>N-API version: 1</span>
4767</div>
4768<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_adjust_external_memory</span><span class="hljs-params">(napi_env env,
4769                                                    <span class="hljs-type">int64_t</span> change_in_bytes,
4770                                                    <span class="hljs-type">int64_t</span>* result)</span>;</code></pre>
4771<ul>
4772<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4773<li><code>[in] change_in_bytes</code>: The change in externally allocated memory that is kept
4774alive by JavaScript objects.</li>
4775<li><code>[out] result</code>: The adjusted value</li>
4776</ul>
4777<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4778<p>This function gives V8 an indication of the amount of externally allocated
4779memory that is kept alive by JavaScript objects (i.e. a JavaScript object
4780that points to its own memory allocated by a native module). Registering
4781externally allocated memory will trigger global garbage collections more
4782often than it would otherwise.</p>
4783</section><section><h3>Promises<span><a class="mark" href="#n_api_promises" id="n_api_promises">#</a></span></h3>
4784<p>Node-API provides facilities for creating <code>Promise</code> objects as described in
4785<a href="https://tc39.github.io/ecma262/#sec-promise-objects">Section 25.4</a> of the ECMA specification. It implements promises as a pair of
4786objects. When a promise is created by <code>napi_create_promise()</code>, a "deferred"
4787object is created and returned alongside the <code>Promise</code>. The deferred object is
4788bound to the created <code>Promise</code> and is the only means to resolve or reject the
4789<code>Promise</code> using <code>napi_resolve_deferred()</code> or <code>napi_reject_deferred()</code>. The
4790deferred object that is created by <code>napi_create_promise()</code> is freed by
4791<code>napi_resolve_deferred()</code> or <code>napi_reject_deferred()</code>. The <code>Promise</code> object may
4792be returned to JavaScript where it can be used in the usual fashion.</p>
4793<p>For example, to create a promise and pass it to an asynchronous worker:</p>
4794<pre><code class="language-c">napi_deferred deferred;
4795napi_value promise;
4796napi_status status;
4797
4798<span class="hljs-comment">// Create the promise.</span>
4799status = napi_create_promise(env, &#x26;deferred, &#x26;promise);
4800<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4801
4802<span class="hljs-comment">// Pass the deferred to a function that performs an asynchronous action.</span>
4803do_something_asynchronous(deferred);
4804
4805<span class="hljs-comment">// Return the promise to JS</span>
4806<span class="hljs-keyword">return</span> promise;</code></pre>
4807<p>The above function <code>do_something_asynchronous()</code> would perform its asynchronous
4808action and then it would resolve or reject the deferred, thereby concluding the
4809promise and freeing the deferred:</p>
4810<pre><code class="language-c">napi_deferred deferred;
4811napi_value undefined;
4812napi_status status;
4813
4814<span class="hljs-comment">// Create a value with which to conclude the deferred.</span>
4815status = napi_get_undefined(env, &#x26;undefined);
4816<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4817
4818<span class="hljs-comment">// Resolve or reject the promise associated with the deferred depending on</span>
4819<span class="hljs-comment">// whether the asynchronous action succeeded.</span>
4820<span class="hljs-keyword">if</span> (asynchronous_action_succeeded) {
4821  status = napi_resolve_deferred(env, deferred, undefined);
4822} <span class="hljs-keyword">else</span> {
4823  status = napi_reject_deferred(env, deferred, undefined);
4824}
4825<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
4826
4827<span class="hljs-comment">// At this point the deferred has been freed, so we should assign NULL to it.</span>
4828deferred = <span class="hljs-literal">NULL</span>;</code></pre>
4829<h4>napi_create_promise<span><a class="mark" href="#n_api_napi_create_promise" id="n_api_napi_create_promise">#</a></span></h4>
4830<div class="api_metadata">
4831<span>Added in: v8.5.0</span>
4832<span>N-API version: 1</span>
4833</div>
4834<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_promise</span><span class="hljs-params">(napi_env env,
4835                                napi_deferred* deferred,
4836                                napi_value* promise)</span>;</code></pre>
4837<ul>
4838<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4839<li><code>[out] deferred</code>: A newly created deferred object which can later be passed to
4840<code>napi_resolve_deferred()</code> or <code>napi_reject_deferred()</code> to resolve resp. reject
4841the associated promise.</li>
4842<li><code>[out] promise</code>: The JavaScript promise associated with the deferred object.</li>
4843</ul>
4844<p>Returns <code>napi_ok</code> if the API succeeded.</p>
4845<p>This API creates a deferred object and a JavaScript promise.</p>
4846<h4>napi_resolve_deferred<span><a class="mark" href="#n_api_napi_resolve_deferred" id="n_api_napi_resolve_deferred">#</a></span></h4>
4847<div class="api_metadata">
4848<span>Added in: v8.5.0</span>
4849<span>N-API version: 1</span>
4850</div>
4851<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_resolve_deferred</span><span class="hljs-params">(napi_env env,
4852                                  napi_deferred deferred,
4853                                  napi_value resolution)</span>;</code></pre>
4854<ul>
4855<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4856<li><code>[in] deferred</code>: The deferred object whose associated promise to resolve.</li>
4857<li><code>[in] resolution</code>: The value with which to resolve the promise.</li>
4858</ul>
4859<p>This API resolves a JavaScript promise by way of the deferred object
4860with which it is associated. Thus, it can only be used to resolve JavaScript
4861promises for which the corresponding deferred object is available. This
4862effectively means that the promise must have been created using
4863<code>napi_create_promise()</code> and the deferred object returned from that call must
4864have been retained in order to be passed to this API.</p>
4865<p>The deferred object is freed upon successful completion.</p>
4866<h4>napi_reject_deferred<span><a class="mark" href="#n_api_napi_reject_deferred" id="n_api_napi_reject_deferred">#</a></span></h4>
4867<div class="api_metadata">
4868<span>Added in: v8.5.0</span>
4869<span>N-API version: 1</span>
4870</div>
4871<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_reject_deferred</span><span class="hljs-params">(napi_env env,
4872                                 napi_deferred deferred,
4873                                 napi_value rejection)</span>;</code></pre>
4874<ul>
4875<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4876<li><code>[in] deferred</code>: The deferred object whose associated promise to resolve.</li>
4877<li><code>[in] rejection</code>: The value with which to reject the promise.</li>
4878</ul>
4879<p>This API rejects a JavaScript promise by way of the deferred object
4880with which it is associated. Thus, it can only be used to reject JavaScript
4881promises for which the corresponding deferred object is available. This
4882effectively means that the promise must have been created using
4883<code>napi_create_promise()</code> and the deferred object returned from that call must
4884have been retained in order to be passed to this API.</p>
4885<p>The deferred object is freed upon successful completion.</p>
4886<h4>napi_is_promise<span><a class="mark" href="#n_api_napi_is_promise" id="n_api_napi_is_promise">#</a></span></h4>
4887<div class="api_metadata">
4888<span>Added in: v8.5.0</span>
4889<span>N-API version: 1</span>
4890</div>
4891<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_promise</span><span class="hljs-params">(napi_env env,
4892                            napi_value value,
4893                            <span class="hljs-type">bool</span>* is_promise)</span>;</code></pre>
4894<ul>
4895<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4896<li><code>[in] value</code>: The value to examine</li>
4897<li><code>[out] is_promise</code>: Flag indicating whether <code>promise</code> is a native promise
4898object (that is, a promise object created by the underlying engine).</li>
4899</ul>
4900</section><section><h3>Script execution<span><a class="mark" href="#n_api_script_execution" id="n_api_script_execution">#</a></span></h3>
4901<p>Node-API provides an API for executing a string containing JavaScript using the
4902underlying JavaScript engine.</p>
4903<h4>napi_run_script<span><a class="mark" href="#n_api_napi_run_script" id="n_api_napi_run_script">#</a></span></h4>
4904<div class="api_metadata">
4905<span>Added in: v8.5.0</span>
4906<span>N-API version: 1</span>
4907</div>
4908<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_run_script</span><span class="hljs-params">(napi_env env,
4909                                        napi_value script,
4910                                        napi_value* result)</span>;</code></pre>
4911<ul>
4912<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4913<li><code>[in] script</code>: A JavaScript string containing the script to execute.</li>
4914<li><code>[out] result</code>: The value resulting from having executed the script.</li>
4915</ul>
4916<p>This function executes a string of JavaScript code and returns its result with
4917the following caveats:</p>
4918<ul>
4919<li>Unlike <code>eval</code>, this function does not allow the script to access the current
4920lexical scope, and therefore also does not allow to access the
4921<a href="modules.html#modules_the_module_scope">module scope</a>, meaning that pseudo-globals such as <code>require</code> will not be
4922available.</li>
4923<li>The script can access the <a href="globals.html">global scope</a>. Function and <code>var</code> declarations
4924in the script will be added to the <a href="globals.html#globals_global"><code>global</code></a> object. Variable declarations
4925made using <code>let</code> and <code>const</code> will be visible globally, but will not be added
4926to the <a href="globals.html#globals_global"><code>global</code></a> object.</li>
4927<li>The value of <code>this</code> is <a href="globals.html#globals_global"><code>global</code></a> within the script.</li>
4928</ul>
4929</section><section><h3>libuv event loop<span><a class="mark" href="#n_api_libuv_event_loop" id="n_api_libuv_event_loop">#</a></span></h3>
4930<p>Node-API provides a function for getting the current event loop associated with
4931a specific <code>napi_env</code>.</p>
4932<h4>napi_get_uv_event_loop<span><a class="mark" href="#n_api_napi_get_uv_event_loop" id="n_api_napi_get_uv_event_loop">#</a></span></h4>
4933<div class="api_metadata">
4934<span>Added in: v9.3.0, v8.10.0</span>
4935<span>N-API version: 2</span>
4936</div>
4937<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_uv_event_loop</span><span class="hljs-params">(napi_env env,
4938                                               <span class="hljs-keyword">struct</span> uv_loop_s** loop)</span>;</code></pre>
4939<ul>
4940<li><code>[in] env</code>: The environment that the API is invoked under.</li>
4941<li><code>[out] loop</code>: The current libuv loop instance.</li>
4942</ul>
4943</section><section><h3>Asynchronous thread-safe function calls<span><a class="mark" href="#n_api_asynchronous_thread_safe_function_calls" id="n_api_asynchronous_thread_safe_function_calls">#</a></span></h3>
4944<p>JavaScript functions can normally only be called from a native addon's main
4945thread. If an addon creates additional threads, then Node-API functions that
4946require a <code>napi_env</code>, <code>napi_value</code>, or <code>napi_ref</code> must not be called from those
4947threads.</p>
4948<p>When an addon has additional threads and JavaScript functions need to be invoked
4949based on the processing completed by those threads, those threads must
4950communicate with the addon's main thread so that the main thread can invoke the
4951JavaScript function on their behalf. The thread-safe function APIs provide an
4952easy way to do this.</p>
4953<p>These APIs provide the type <code>napi_threadsafe_function</code> as well as APIs to
4954create, destroy, and call objects of this type.
4955<code>napi_create_threadsafe_function()</code> creates a persistent reference to a
4956<code>napi_value</code> that holds a JavaScript function which can be called from multiple
4957threads. The calls happen asynchronously. This means that values with which the
4958JavaScript callback is to be called will be placed in a queue, and, for each
4959value in the queue, a call will eventually be made to the JavaScript function.</p>
4960<p>Upon creation of a <code>napi_threadsafe_function</code> a <code>napi_finalize</code> callback can be
4961provided. This callback will be invoked on the main thread when the thread-safe
4962function is about to be destroyed. It receives the context and the finalize data
4963given during construction, and provides an opportunity for cleaning up after the
4964threads e.g. by calling <code>uv_thread_join()</code>. <strong>Aside from the main loop thread,
4965no threads should be using the thread-safe function after the finalize callback
4966completes.</strong></p>
4967<p>The <code>context</code> given during the call to <code>napi_create_threadsafe_function()</code> can
4968be retrieved from any thread with a call to
4969<code>napi_get_threadsafe_function_context()</code>.</p>
4970<h4>Calling a thread-safe function<span><a class="mark" href="#n_api_calling_a_thread_safe_function" id="n_api_calling_a_thread_safe_function">#</a></span></h4>
4971<p><code>napi_call_threadsafe_function()</code> can be used for initiating a call into
4972JavaScript. <code>napi_call_threadsafe_function()</code> accepts a parameter which controls
4973whether the API behaves blockingly. If set to <code>napi_tsfn_nonblocking</code>, the API
4974behaves non-blockingly, returning <code>napi_queue_full</code> if the queue was full,
4975preventing data from being successfully added to the queue. If set to
4976<code>napi_tsfn_blocking</code>, the API blocks until space becomes available in the queue.
4977<code>napi_call_threadsafe_function()</code> never blocks if the thread-safe function was
4978created with a maximum queue size of 0.</p>
4979<p><code>napi_call_threadsafe_function()</code> should not be called with <code>napi_tsfn_blocking</code>
4980from a JavaScript thread, because, if the queue is full, it may cause the
4981JavaScript thread to deadlock.</p>
4982<p>The actual call into JavaScript is controlled by the callback given via the
4983<code>call_js_cb</code> parameter. <code>call_js_cb</code> is invoked on the main thread once for each
4984value that was placed into the queue by a successful call to
4985<code>napi_call_threadsafe_function()</code>. If such a callback is not given, a default
4986callback will be used, and the resulting JavaScript call will have no arguments.
4987The <code>call_js_cb</code> callback receives the JavaScript function to call as a
4988<code>napi_value</code> in its parameters, as well as the <code>void*</code> context pointer used when
4989creating the <code>napi_threadsafe_function</code>, and the next data pointer that was
4990created by one of the secondary threads. The callback can then use an API such
4991as <code>napi_call_function()</code> to call into JavaScript.</p>
4992<p>The callback may also be invoked with <code>env</code> and <code>call_js_cb</code> both set to <code>NULL</code>
4993to indicate that calls into JavaScript are no longer possible, while items
4994remain in the queue that may need to be freed. This normally occurs when the
4995Node.js process exits while there is a thread-safe function still active.</p>
4996<p>It is not necessary to call into JavaScript via <code>napi_make_callback()</code> because
4997Node-API runs <code>call_js_cb</code> in a context appropriate for callbacks.</p>
4998<h4>Reference counting of thread-safe functions<span><a class="mark" href="#n_api_reference_counting_of_thread_safe_functions" id="n_api_reference_counting_of_thread_safe_functions">#</a></span></h4>
4999<p>Threads can be added to and removed from a <code>napi_threadsafe_function</code> object
5000during its existence. Thus, in addition to specifying an initial number of
5001threads upon creation, <code>napi_acquire_threadsafe_function</code> can be called to
5002indicate that a new thread will start making use of the thread-safe function.
5003Similarly, <code>napi_release_threadsafe_function</code> can be called to indicate that an
5004existing thread will stop making use of the thread-safe function.</p>
5005<p><code>napi_threadsafe_function</code> objects are destroyed when every thread which uses
5006the object has called <code>napi_release_threadsafe_function()</code> or has received a
5007return status of <code>napi_closing</code> in response to a call to
5008<code>napi_call_threadsafe_function</code>. The queue is emptied before the
5009<code>napi_threadsafe_function</code> is destroyed. <code>napi_release_threadsafe_function()</code>
5010should be the last API call made in conjunction with a given
5011<code>napi_threadsafe_function</code>, because after the call completes, there is no
5012guarantee that the <code>napi_threadsafe_function</code> is still allocated. For the same
5013reason, do not use a thread-safe function
5014after receiving a return value of <code>napi_closing</code> in response to a call to
5015<code>napi_call_threadsafe_function</code>. Data associated with the
5016<code>napi_threadsafe_function</code> can be freed in its <code>napi_finalize</code> callback which
5017was passed to <code>napi_create_threadsafe_function()</code>. The parameter
5018<code>initial_thread_count</code> of <code>napi_create_threadsafe_function</code> marks the initial
5019number of aquisitions of the thread-safe functions, instead of calling
5020<code>napi_acquire_threadsafe_function</code> multiple times at creation.</p>
5021<p>Once the number of threads making use of a <code>napi_threadsafe_function</code> reaches
5022zero, no further threads can start making use of it by calling
5023<code>napi_acquire_threadsafe_function()</code>. In fact, all subsequent API calls
5024associated with it, except <code>napi_release_threadsafe_function()</code>, will return an
5025error value of <code>napi_closing</code>.</p>
5026<p>The thread-safe function can be "aborted" by giving a value of <code>napi_tsfn_abort</code>
5027to <code>napi_release_threadsafe_function()</code>. This will cause all subsequent APIs
5028associated with the thread-safe function except
5029<code>napi_release_threadsafe_function()</code> to return <code>napi_closing</code> even before its
5030reference count reaches zero. In particular, <code>napi_call_threadsafe_function()</code>
5031will return <code>napi_closing</code>, thus informing the threads that it is no longer
5032possible to make asynchronous calls to the thread-safe function. This can be
5033used as a criterion for terminating the thread. <strong>Upon receiving a return value
5034of <code>napi_closing</code> from <code>napi_call_threadsafe_function()</code> a thread must not use
5035the thread-safe function anymore because it is no longer guaranteed to
5036be allocated.</strong></p>
5037<h4>Deciding whether to keep the process running<span><a class="mark" href="#n_api_deciding_whether_to_keep_the_process_running" id="n_api_deciding_whether_to_keep_the_process_running">#</a></span></h4>
5038<p>Similarly to libuv handles, thread-safe functions can be "referenced" and
5039"unreferenced". A "referenced" thread-safe function will cause the event loop on
5040the thread on which it is created to remain alive until the thread-safe function
5041is destroyed. In contrast, an "unreferenced" thread-safe function will not
5042prevent the event loop from exiting. The APIs <code>napi_ref_threadsafe_function</code> and
5043<code>napi_unref_threadsafe_function</code> exist for this purpose.</p>
5044<p>Neither does <code>napi_unref_threadsafe_function</code> mark the thread-safe functions as
5045able to be destroyed nor does <code>napi_ref_threadsafe_function</code> prevent it from
5046being destroyed.</p>
5047<h4>napi_create_threadsafe_function<span><a class="mark" href="#n_api_napi_create_threadsafe_function" id="n_api_napi_create_threadsafe_function">#</a></span></h4>
5048<div class="api_metadata">
5049<details class="changelog"><summary>History</summary>
5050<table>
5051<tbody><tr><th>Version</th><th>Changes</th></tr>
5052<tr><td>v12.6.0, v10.17.0</td>
5053<td><p>Made <code>func</code> parameter optional with custom <code>call_js_cb</code>.</p></td></tr>
5054<tr><td>v10.6.0</td>
5055<td><p><span>Added in: v10.6.0</span></p></td></tr>
5056</tbody></table>
5057</details>
5058<span>N-API version: 4</span>
5059</div>
5060<pre><code class="language-c">NAPI_EXTERN napi_status
5061<span class="hljs-title function_">napi_create_threadsafe_function</span><span class="hljs-params">(napi_env env,
5062                                napi_value func,
5063                                napi_value async_resource,
5064                                napi_value async_resource_name,
5065                                <span class="hljs-type">size_t</span> max_queue_size,
5066                                <span class="hljs-type">size_t</span> initial_thread_count,
5067                                <span class="hljs-type">void</span>* thread_finalize_data,
5068                                napi_finalize thread_finalize_cb,
5069                                <span class="hljs-type">void</span>* context,
5070                                napi_threadsafe_function_call_js call_js_cb,
5071                                napi_threadsafe_function* result)</span>;</code></pre>
5072<ul>
5073<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5074<li><code>[in] func</code>: An optional JavaScript function to call from another thread. It
5075must be provided if <code>NULL</code> is passed to <code>call_js_cb</code>.</li>
5076<li><code>[in] async_resource</code>: An optional object associated with the async work that
5077will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource"><code>init</code> hooks</a>.</li>
5078<li><code>[in] async_resource_name</code>: A JavaScript string to provide an identifier for
5079the kind of resource that is being provided for diagnostic information exposed
5080by the <code>async_hooks</code> API.</li>
5081<li><code>[in] max_queue_size</code>: Maximum size of the queue. <code>0</code> for no limit.</li>
5082<li><code>[in] initial_thread_count</code>: The initial number of acquisitions, i.e. the
5083initial number of threads, including the main thread, which will be making use
5084of this function.</li>
5085<li><code>[in] thread_finalize_data</code>: Optional data to be passed to <code>thread_finalize_cb</code>.</li>
5086<li><code>[in] thread_finalize_cb</code>: Optional function to call when the
5087<code>napi_threadsafe_function</code> is being destroyed.</li>
5088<li><code>[in] context</code>: Optional data to attach to the resulting
5089<code>napi_threadsafe_function</code>.</li>
5090<li><code>[in] call_js_cb</code>: Optional callback which calls the JavaScript function in
5091response to a call on a different thread. This callback will be called on the
5092main thread. If not given, the JavaScript function will be called with no
5093parameters and with <code>undefined</code> as its <code>this</code> value.
5094<a href="#n_api_napi_threadsafe_function_call_js"><code>napi_threadsafe_function_call_js</code></a> provides more details.</li>
5095<li><code>[out] result</code>: The asynchronous thread-safe JavaScript function.</li>
5096</ul>
5097<h4>napi_get_threadsafe_function_context<span><a class="mark" href="#n_api_napi_get_threadsafe_function_context" id="n_api_napi_get_threadsafe_function_context">#</a></span></h4>
5098<div class="api_metadata">
5099<span>Added in: v10.6.0</span>
5100<span>N-API version: 4</span>
5101</div>
5102<pre><code class="language-c">NAPI_EXTERN napi_status
5103<span class="hljs-title function_">napi_get_threadsafe_function_context</span><span class="hljs-params">(napi_threadsafe_function func,
5104                                     <span class="hljs-type">void</span>** result)</span>;</code></pre>
5105<ul>
5106<li><code>[in] func</code>: The thread-safe function for which to retrieve the context.</li>
5107<li><code>[out] result</code>: The location where to store the context.</li>
5108</ul>
5109<p>This API may be called from any thread which makes use of <code>func</code>.</p>
5110<h4>napi_call_threadsafe_function<span><a class="mark" href="#n_api_napi_call_threadsafe_function" id="n_api_napi_call_threadsafe_function">#</a></span></h4>
5111<div class="api_metadata">
5112<details class="changelog"><summary>History</summary>
5113<table>
5114<tbody><tr><th>Version</th><th>Changes</th></tr>
5115<tr><td>v14.5.0</td>
5116<td><p>Support for <code>napi_would_deadlock</code> has been reverted.</p></td></tr>
5117<tr><td>v14.1.0</td>
5118<td><p>Return <code>napi_would_deadlock</code> when called with <code>napi_tsfn_blocking</code> from the main thread or a worker thread and the queue is full.</p></td></tr>
5119<tr><td>v10.6.0</td>
5120<td><p><span>Added in: v10.6.0</span></p></td></tr>
5121</tbody></table>
5122</details>
5123<span>N-API version: 4</span>
5124</div>
5125<pre><code class="language-c">NAPI_EXTERN napi_status
5126<span class="hljs-title function_">napi_call_threadsafe_function</span><span class="hljs-params">(napi_threadsafe_function func,
5127                              <span class="hljs-type">void</span>* data,
5128                              napi_threadsafe_function_call_mode is_blocking)</span>;</code></pre>
5129<ul>
5130<li><code>[in] func</code>: The asynchronous thread-safe JavaScript function to invoke.</li>
5131<li><code>[in] data</code>: Data to send into JavaScript via the callback <code>call_js_cb</code>
5132provided during the creation of the thread-safe JavaScript function.</li>
5133<li><code>[in] is_blocking</code>: Flag whose value can be either <code>napi_tsfn_blocking</code> to
5134indicate that the call should block if the queue is full or
5135<code>napi_tsfn_nonblocking</code> to indicate that the call should return immediately
5136with a status of <code>napi_queue_full</code> whenever the queue is full.</li>
5137</ul>
5138<p>This API should not be called with <code>napi_tsfn_blocking</code> from a JavaScript
5139thread, because, if the queue is full, it may cause the JavaScript thread to
5140deadlock.</p>
5141<p>This API will return <code>napi_closing</code> if <code>napi_release_threadsafe_function()</code> was
5142called with <code>abort</code> set to <code>napi_tsfn_abort</code> from any thread. The value is only
5143added to the queue if the API returns <code>napi_ok</code>.</p>
5144<p>This API may be called from any thread which makes use of <code>func</code>.</p>
5145<h4>napi_acquire_threadsafe_function<span><a class="mark" href="#n_api_napi_acquire_threadsafe_function" id="n_api_napi_acquire_threadsafe_function">#</a></span></h4>
5146<div class="api_metadata">
5147<span>Added in: v10.6.0</span>
5148<span>N-API version: 4</span>
5149</div>
5150<pre><code class="language-c">NAPI_EXTERN napi_status
5151<span class="hljs-title function_">napi_acquire_threadsafe_function</span><span class="hljs-params">(napi_threadsafe_function func)</span>;</code></pre>
5152<ul>
5153<li><code>[in] func</code>: The asynchronous thread-safe JavaScript function to start making
5154use of.</li>
5155</ul>
5156<p>A thread should call this API before passing <code>func</code> to any other thread-safe
5157function APIs to indicate that it will be making use of <code>func</code>. This prevents
5158<code>func</code> from being destroyed when all other threads have stopped making use of
5159it.</p>
5160<p>This API may be called from any thread which will start making use of <code>func</code>.</p>
5161<h4>napi_release_threadsafe_function<span><a class="mark" href="#n_api_napi_release_threadsafe_function" id="n_api_napi_release_threadsafe_function">#</a></span></h4>
5162<div class="api_metadata">
5163<span>Added in: v10.6.0</span>
5164<span>N-API version: 4</span>
5165</div>
5166<pre><code class="language-c">NAPI_EXTERN napi_status
5167<span class="hljs-title function_">napi_release_threadsafe_function</span><span class="hljs-params">(napi_threadsafe_function func,
5168                                 napi_threadsafe_function_release_mode mode)</span>;</code></pre>
5169<ul>
5170<li><code>[in] func</code>: The asynchronous thread-safe JavaScript function whose reference
5171count to decrement.</li>
5172<li><code>[in] mode</code>: Flag whose value can be either <code>napi_tsfn_release</code> to indicate
5173that the current thread will make no further calls to the thread-safe
5174function, or <code>napi_tsfn_abort</code> to indicate that in addition to the current
5175thread, no other thread should make any further calls to the thread-safe
5176function. If set to <code>napi_tsfn_abort</code>, further calls to
5177<code>napi_call_threadsafe_function()</code> will return <code>napi_closing</code>, and no further
5178values will be placed in the queue.</li>
5179</ul>
5180<p>A thread should call this API when it stops making use of <code>func</code>. Passing <code>func</code>
5181to any thread-safe APIs after having called this API has undefined results, as
5182<code>func</code> may have been destroyed.</p>
5183<p>This API may be called from any thread which will stop making use of <code>func</code>.</p>
5184<h4>napi_ref_threadsafe_function<span><a class="mark" href="#n_api_napi_ref_threadsafe_function" id="n_api_napi_ref_threadsafe_function">#</a></span></h4>
5185<div class="api_metadata">
5186<span>Added in: v10.6.0</span>
5187<span>N-API version: 4</span>
5188</div>
5189<pre><code class="language-c">NAPI_EXTERN napi_status
5190<span class="hljs-title function_">napi_ref_threadsafe_function</span><span class="hljs-params">(napi_env env, napi_threadsafe_function func)</span>;</code></pre>
5191<ul>
5192<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5193<li><code>[in] func</code>: The thread-safe function to reference.</li>
5194</ul>
5195<p>This API is used to indicate that the event loop running on the main thread
5196should not exit until <code>func</code> has been destroyed. Similar to <a href="https://docs.libuv.org/en/v1.x/handle.html#c.uv_ref"><code>uv_ref</code></a> it is
5197also idempotent.</p>
5198<p>Neither does <code>napi_unref_threadsafe_function</code> mark the thread-safe functions as
5199able to be destroyed nor does <code>napi_ref_threadsafe_function</code> prevent it from
5200being destroyed. <code>napi_acquire_threadsafe_function</code> and
5201<code>napi_release_threadsafe_function</code> are available for that purpose.</p>
5202<p>This API may only be called from the main thread.</p>
5203<h4>napi_unref_threadsafe_function<span><a class="mark" href="#n_api_napi_unref_threadsafe_function" id="n_api_napi_unref_threadsafe_function">#</a></span></h4>
5204<div class="api_metadata">
5205<span>Added in: v10.6.0</span>
5206<span>N-API version: 4</span>
5207</div>
5208<pre><code class="language-c">NAPI_EXTERN napi_status
5209<span class="hljs-title function_">napi_unref_threadsafe_function</span><span class="hljs-params">(napi_env env, napi_threadsafe_function func)</span>;</code></pre>
5210<ul>
5211<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5212<li><code>[in] func</code>: The thread-safe function to unreference.</li>
5213</ul>
5214<p>This API is used to indicate that the event loop running on the main thread
5215may exit before <code>func</code> is destroyed. Similar to <a href="https://docs.libuv.org/en/v1.x/handle.html#c.uv_unref"><code>uv_unref</code></a> it is also
5216idempotent.</p>
5217<p>This API may only be called from the main thread.</p>
5218</section><section><h3>Miscellaneous utilities<span><a class="mark" href="#n_api_miscellaneous_utilities" id="n_api_miscellaneous_utilities">#</a></span></h3>
5219</section><section><h3>node_api_get_module_file_name<span><a class="mark" href="#n_api_node_api_get_module_file_name" id="n_api_node_api_get_module_file_name">#</a></span></h3>
5220<div class="api_metadata">
5221<span>Added in: v14.18.0</span>
5222</div>
5223<p></p><div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experimental</div><p></p>
5224<pre><code class="language-c">NAPI_EXTERN napi_status
5225<span class="hljs-title function_">node_api_get_module_file_name</span><span class="hljs-params">(napi_env env, <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>** result)</span>;
5226</code></pre>
5227<ul>
5228<li><code>[in] env</code>: The environment that the API is invoked under.</li>
5229<li><code>[out] result</code>: A URL containing the absolute path of the
5230location from which the add-on was loaded. For a file on the local
5231file system it will start with <code>file://</code>. The string is null-terminated and
5232owned by <code>env</code> and must thus not be modified or freed.</li>
5233</ul>
5234<p><code>result</code> may be an empty string if the add-on loading process fails to establish
5235the add-on's file name during loading.</p></section>
5236        <!-- API END -->
5237      </div>
5238    </div>
5239  </div>
5240  <script>
5241    'use strict';
5242    {
5243      const kCustomPreference = 'customDarkTheme';
5244      const userSettings = sessionStorage.getItem(kCustomPreference);
5245      const themeToggleButton = document.getElementById('theme-toggle-btn');
5246      if (userSettings === null && window.matchMedia) {
5247        const mq = window.matchMedia('(prefers-color-scheme: dark)');
5248        if ('onchange' in mq) {
5249          function mqChangeListener(e) {
5250            document.body.classList.toggle('dark-mode', e.matches);
5251          }
5252          mq.addEventListener('change', mqChangeListener);
5253          if (themeToggleButton) {
5254            themeToggleButton.addEventListener('click', function() {
5255              mq.removeEventListener('change', mqChangeListener);
5256            }, { once: true });
5257          }
5258        }
5259        if (mq.matches) {
5260          document.body.classList.add('dark-mode');
5261        }
5262      } else if (userSettings === 'true') {
5263        document.body.classList.add('dark-mode');
5264      }
5265      if (themeToggleButton) {
5266        themeToggleButton.hidden = false;
5267        themeToggleButton.addEventListener('click', function() {
5268          sessionStorage.setItem(
5269            kCustomPreference,
5270            document.body.classList.toggle('dark-mode')
5271          );
5272        });
5273      }
5274    }
5275  </script>
5276</body>
5277</html>
5278