• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Node-API
2
3<!--introduced_in=v8.0.0-->
4<!-- type=misc -->
5
6> Stability: 2 - Stable
7
8Node-API (formerly N-API) is an API for building native Addons. It is
9independent from the underlying JavaScript runtime (for example, V8) and is
10maintained as part of Node.js itself. This API will be Application Binary
11Interface (ABI) stable across versions of Node.js. It is intended to insulate
12addons from changes in the underlying JavaScript engine and allow modules
13compiled for one major version to run on later major versions of Node.js without
14recompilation. The [ABI Stability][] guide provides a more in-depth explanation.
15
16Addons are built/packaged with the same approach/tools outlined in the section
17titled [C++ Addons][]. The only difference is the set of APIs that are used by
18the native code. Instead of using the V8 or [Native Abstractions for Node.js][]
19APIs, the functions available in Node-API are used.
20
21APIs exposed by Node-API are generally used to create and manipulate
22JavaScript values. Concepts and operations generally map to ideas specified
23in the ECMA-262 Language Specification. The APIs have the following
24properties:
25
26* All Node-API calls return a status code of type `napi_status`. This
27  status indicates whether the API call succeeded or failed.
28* The API's return value is passed via an out parameter.
29* All JavaScript values are abstracted behind an opaque type named
30  `napi_value`.
31* In case of an error status code, additional information can be obtained
32  using `napi_get_last_error_info`. More information can be found in the error
33  handling section [Error handling][].
34
35Node-API is a C API that ensures ABI stability across Node.js versions
36and different compiler levels. A C++ API can be easier to use.
37To support using C++, the project maintains a
38C++ wrapper module called [`node-addon-api`][].
39This wrapper provides an inlineable C++ API. Binaries built
40with `node-addon-api` will depend on the symbols for the Node-API C-based
41functions exported by Node.js. `node-addon-api` is a more
42efficient way to write code that calls Node-API. Take, for example, the
43following `node-addon-api` code. The first section shows the
44`node-addon-api` code and the second section shows what actually gets
45used in the addon.
46
47```cpp
48Object obj = Object::New(env);
49obj["foo"] = String::New(env, "bar");
50```
51
52```cpp
53napi_status status;
54napi_value object, string;
55status = napi_create_object(env, &object);
56if (status != napi_ok) {
57  napi_throw_error(env, ...);
58  return;
59}
60
61status = napi_create_string_utf8(env, "bar", NAPI_AUTO_LENGTH, &string);
62if (status != napi_ok) {
63  napi_throw_error(env, ...);
64  return;
65}
66
67status = napi_set_named_property(env, object, "foo", string);
68if (status != napi_ok) {
69  napi_throw_error(env, ...);
70  return;
71}
72```
73
74The end result is that the addon only uses the exported C APIs. As a result,
75it still gets the benefits of the ABI stability provided by the C API.
76
77When using `node-addon-api` instead of the C APIs, start with the API [docs][]
78for `node-addon-api`.
79
80The [Node-API Resource](https://nodejs.github.io/node-addon-examples/) offers
81an excellent orientation and tips for developers just getting started with
82Node-API and `node-addon-api`.
83
84## Implications of ABI stability
85
86Although Node-API provides an ABI stability guarantee, other parts of Node.js do
87not, and any external libraries used from the addon may not. In particular,
88none of the following APIs provide an ABI stability guarantee across major
89versions:
90
91* the Node.js C++ APIs available via any of
92
93    ```cpp
94    #include <node.h>
95    #include <node_buffer.h>
96    #include <node_version.h>
97    #include <node_object_wrap.h>
98    ```
99
100* the libuv APIs which are also included with Node.js and available via
101
102    ```cpp
103    #include <uv.h>
104    ```
105
106* the V8 API available via
107
108    ```cpp
109    #include <v8.h>
110    ```
111
112Thus, for an addon to remain ABI-compatible across Node.js major versions, it
113must use Node-API exclusively by restricting itself to using
114
115```c
116#include <node_api.h>
117```
118
119and by checking, for all external libraries that it uses, that the external
120library makes ABI stability guarantees similar to Node-API.
121
122## Building
123
124Unlike modules written in JavaScript, developing and deploying Node.js
125native addons using Node-API requires an additional set of tools. Besides the
126basic tools required to develop for Node.js, the native addon developer
127requires a toolchain that can compile C and C++ code into a binary. In
128addition, depending upon how the native addon is deployed, the *user* of
129the native addon will also need to have a C/C++ toolchain installed.
130
131For Linux developers, the necessary C/C++ toolchain packages are readily
132available. [GCC][] is widely used in the Node.js community to build and
133test across a variety of platforms. For many developers, the [LLVM][]
134compiler infrastructure is also a good choice.
135
136For Mac developers, [Xcode][] offers all the required compiler tools.
137However, it is not necessary to install the entire Xcode IDE. The following
138command installs the necessary toolchain:
139
140```bash
141xcode-select --install
142```
143
144For Windows developers, [Visual Studio][] offers all the required compiler
145tools. However, it is not necessary to install the entire Visual Studio
146IDE. The following command installs the necessary toolchain:
147
148```bash
149npm install --global windows-build-tools
150```
151
152The sections below describe the additional tools available for developing
153and deploying Node.js native addons.
154
155### Build tools
156
157Both the tools listed here require that *users* of the native
158addon have a C/C++ toolchain installed in order to successfully install
159the native addon.
160
161#### node-gyp
162
163[node-gyp][] is a build system based on the [gyp-next][] fork of
164Google's [GYP][] tool and comes bundled with npm. GYP, and therefore node-gyp,
165requires that Python be installed.
166
167Historically, node-gyp has been the tool of choice for building native
168addons. It has widespread adoption and documentation. However, some
169developers have run into limitations in node-gyp.
170
171#### CMake.js
172
173[CMake.js][] is an alternative build system based on [CMake][].
174
175CMake.js is a good choice for projects that already use CMake or for
176developers affected by limitations in node-gyp.
177
178### Uploading precompiled binaries
179
180The three tools listed here permit native addon developers and maintainers
181to create and upload binaries to public or private servers. These tools are
182typically integrated with CI/CD build systems like [Travis CI][] and
183[AppVeyor][] to build and upload binaries for a variety of platforms and
184architectures. These binaries are then available for download by users who
185do not need to have a C/C++ toolchain installed.
186
187#### node-pre-gyp
188
189[node-pre-gyp][] is a tool based on node-gyp that adds the ability to
190upload binaries to a server of the developer's choice. node-pre-gyp has
191particularly good support for uploading binaries to Amazon S3.
192
193#### prebuild
194
195[prebuild][] is a tool that supports builds using either node-gyp or
196CMake.js. Unlike node-pre-gyp which supports a variety of servers, prebuild
197uploads binaries only to [GitHub releases][]. prebuild is a good choice for
198GitHub projects using CMake.js.
199
200#### prebuildify
201
202[prebuildify][] is a tool based on node-gyp. The advantage of prebuildify is
203that the built binaries are bundled with the native module when it's
204uploaded to npm. The binaries are downloaded from npm and are immediately
205available to the module user when the native module is installed.
206
207## Usage
208
209In order to use the Node-API functions, include the file [`node_api.h`][] which
210is located in the src directory in the node development tree:
211
212```c
213#include <node_api.h>
214```
215
216This will opt into the default `NAPI_VERSION` for the given release of Node.js.
217In order to ensure compatibility with specific versions of Node-API, the version
218can be specified explicitly when including the header:
219
220```c
221#define NAPI_VERSION 3
222#include <node_api.h>
223```
224
225This restricts the Node-API surface to just the functionality that was available
226in the specified (and earlier) versions.
227
228Some of the Node-API surface is experimental and requires explicit opt-in:
229
230```c
231#define NAPI_EXPERIMENTAL
232#include <node_api.h>
233```
234
235In this case the entire API surface, including any experimental APIs, will be
236available to the module code.
237
238## Node-API version matrix
239
240Node-API versions are additive and versioned independently from Node.js.
241Version 4 is an extension to version 3 in that it has all of the APIs
242from version 3 with some additions. This means that it is not necessary
243to recompile for new versions of Node.js which are
244listed as supporting a later version.
245
246<!-- For accessibility purposes, this table needs row headers. That means we
247     can't do it in markdown. Hence, the raw HTML. -->
248
249<table>
250  <tr>
251    <td></td>
252    <th scope="col">1</th>
253    <th scope="col">2</th>
254    <th scope="col">3</th>
255  </tr>
256  <tr>
257    <th scope="row">v6.x</th>
258    <td></td>
259    <td></td>
260    <td>v6.14.2*</td>
261  </tr>
262  <tr>
263    <th scope="row">v8.x</th>
264    <td>v8.6.0**</td>
265    <td>v8.10.0*</td>
266    <td>v8.11.2</td>
267  </tr>
268  <tr>
269    <th scope="row">v9.x</th>
270    <td>v9.0.0*</td>
271    <td>v9.3.0*</td>
272    <td>v9.11.0*</td>
273  </tr>
274  <tr>
275    <th scope="row">≥ v10.x</th>
276    <td>all releases</td>
277    <td>all releases</td>
278    <td>all releases</td>
279  </tr>
280</table>
281
282<table>
283  <tr>
284    <td></td>
285    <th scope="col">4</th>
286    <th scope="col">5</th>
287    <th scope="col">6</th>
288    <th scope="col">7</th>
289    <th scope="col">8</th>
290  </tr>
291  <tr>
292    <th scope="row">v10.x</th>
293    <td>v10.16.0</td>
294    <td>v10.17.0</td>
295    <td>v10.20.0</td>
296    <td>v10.23.0</td>
297    <td></td>
298  </tr>
299  <tr>
300    <th scope="row">v11.x</th>
301    <td>v11.8.0</td>
302    <td></td>
303    <td></td>
304    <td></td>
305    <td></td>
306  </tr>
307  <tr>
308    <th scope="row">v12.x</th>
309    <td>v12.0.0</td>
310    <td>v12.11.0</td>
311    <td>v12.17.0</td>
312    <td>v12.19.0</td>
313    <td>v12.22.0</td>
314  </tr>
315  <tr>
316    <th scope="row">v13.x</th>
317    <td>v13.0.0</td>
318    <td>v13.0.0</td>
319    <td></td>
320    <td></td>
321    <td></td>
322  </tr>
323  <tr>
324    <th scope="row">v14.x</th>
325    <td>v14.0.0</td>
326    <td>v14.0.0</td>
327    <td>v14.0.0</td>
328    <td>v14.12.0</td>
329    <td>v14.17.0</td>
330  </tr>
331  <tr>
332    <th scope="row">v15.x</th>
333    <td>v15.0.0</td>
334    <td>v15.0.0</td>
335    <td>v15.0.0</td>
336    <td>v15.0.0</td>
337    <td>v15.12.0</td>
338  </tr>
339  <tr>
340    <th scope="row">v16.x</th>
341    <td>v16.0.0</td>
342    <td>v16.0.0</td>
343    <td>v16.0.0</td>
344    <td>v16.0.0</td>
345    <td>v16.0.0</td>
346  </tr>
347</table>
348
349\* Node-API was experimental.
350
351\*\* Node.js 8.0.0 included Node-API as experimental. It was released as
352Node-API version 1 but continued to evolve until Node.js 8.6.0. The API is
353different in versions prior to Node.js 8.6.0. We recommend Node-API version 3 or
354later.
355
356Each API documented for Node-API will have a header named `added in:`, and APIs
357which are stable will have the additional header `Node-API version:`.
358APIs are directly usable when using a Node.js version which supports
359the Node-API version shown in `Node-API version:` or higher.
360When using a Node.js version that does not support the
361`Node-API version:` listed or if there is no `Node-API version:` listed,
362then the API will only be available if
363`#define NAPI_EXPERIMENTAL` precedes the inclusion of `node_api.h`
364or `js_native_api.h`. If an API appears not to be available on
365a version of Node.js which is later than the one shown in `added in:` then
366this is most likely the reason for the apparent absence.
367
368The Node-APIs associated strictly with accessing ECMAScript features from native
369code can be found separately in `js_native_api.h` and `js_native_api_types.h`.
370The APIs defined in these headers are included in `node_api.h` and
371`node_api_types.h`. The headers are structured in this way in order to allow
372implementations of Node-API outside of Node.js. For those implementations the
373Node.js specific APIs may not be applicable.
374
375The Node.js-specific parts of an addon can be separated from the code that
376exposes the actual functionality to the JavaScript environment so that the
377latter may be used with multiple implementations of Node-API. In the example
378below, `addon.c` and `addon.h` refer only to `js_native_api.h`. This ensures
379that `addon.c` can be reused to compile against either the Node.js
380implementation of Node-API or any implementation of Node-API outside of Node.js.
381
382`addon_node.c` is a separate file that contains the Node.js specific entry point
383to the addon and which instantiates the addon by calling into `addon.c` when the
384addon is loaded into a Node.js environment.
385
386```c
387// addon.h
388#ifndef _ADDON_H_
389#define _ADDON_H_
390#include <js_native_api.h>
391napi_value create_addon(napi_env env);
392#endif  // _ADDON_H_
393```
394
395```c
396// addon.c
397#include "addon.h"
398
399#define NAPI_CALL(env, call)                                      \
400  do {                                                            \
401    napi_status status = (call);                                  \
402    if (status != napi_ok) {                                      \
403      const napi_extended_error_info* error_info = NULL;          \
404      napi_get_last_error_info((env), &error_info);               \
405      bool is_pending;                                            \
406      napi_is_exception_pending((env), &is_pending);              \
407      if (!is_pending) {                                          \
408        const char* message = (error_info->error_message == NULL) \
409            ? "empty error message"                               \
410            : error_info->error_message;                          \
411        napi_throw_error((env), NULL, message);                   \
412        return NULL;                                              \
413      }                                                           \
414    }                                                             \
415  } while(0)
416
417static napi_value
418DoSomethingUseful(napi_env env, napi_callback_info info) {
419  // Do something useful.
420  return NULL;
421}
422
423napi_value create_addon(napi_env env) {
424  napi_value result;
425  NAPI_CALL(env, napi_create_object(env, &result));
426
427  napi_value exported_function;
428  NAPI_CALL(env, napi_create_function(env,
429                                      "doSomethingUseful",
430                                      NAPI_AUTO_LENGTH,
431                                      DoSomethingUseful,
432                                      NULL,
433                                      &exported_function));
434
435  NAPI_CALL(env, napi_set_named_property(env,
436                                         result,
437                                         "doSomethingUseful",
438                                         exported_function));
439
440  return result;
441}
442```
443
444```c
445// addon_node.c
446#include <node_api.h>
447#include "addon.h"
448
449NAPI_MODULE_INIT() {
450  // This function body is expected to return a `napi_value`.
451  // The variables `napi_env env` and `napi_value exports` may be used within
452  // the body, as they are provided by the definition of `NAPI_MODULE_INIT()`.
453  return create_addon(env);
454}
455```
456
457## Environment life cycle APIs
458
459[Section 8.7][] of the [ECMAScript Language Specification][] defines the concept
460of an "Agent" as a self-contained environment in which JavaScript code runs.
461Multiple such Agents may be started and terminated either concurrently or in
462sequence by the process.
463
464A Node.js environment corresponds to an ECMAScript Agent. In the main process,
465an environment is created at startup, and additional environments can be created
466on separate threads to serve as [worker threads][]. When Node.js is embedded in
467another application, the main thread of the application may also construct and
468destroy a Node.js environment multiple times during the life cycle of the
469application process such that each Node.js environment created by the
470application may, in turn, during its life cycle create and destroy additional
471environments as worker threads.
472
473From the perspective of a native addon this means that the bindings it provides
474may be called multiple times, from multiple contexts, and even concurrently from
475multiple threads.
476
477Native addons may need to allocate global state which they use during
478their entire life cycle such that the state must be unique to each instance of
479the addon.
480
481To this end, Node-API provides a way to allocate data such that its life cycle
482is tied to the life cycle of the Agent.
483
484### napi_set_instance_data
485<!-- YAML
486added:
487 - v12.8.0
488 - v10.20.0
489napiVersion: 6
490-->
491
492```c
493napi_status napi_set_instance_data(napi_env env,
494                                   void* data,
495                                   napi_finalize finalize_cb,
496                                   void* finalize_hint);
497```
498
499* `[in] env`: The environment that the Node-API call is invoked under.
500* `[in] data`: The data item to make available to bindings of this instance.
501* `[in] finalize_cb`: The function to call when the environment is being torn
502  down. The function receives `data` so that it might free it.
503  [`napi_finalize`][] provides more details.
504* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
505  collection.
506
507Returns `napi_ok` if the API succeeded.
508
509This API associates `data` with the currently running Agent. `data` can later
510be retrieved using `napi_get_instance_data()`. Any existing data associated with
511the currently running Agent which was set by means of a previous call to
512`napi_set_instance_data()` will be overwritten. If a `finalize_cb` was provided
513by the previous call, it will not be called.
514
515### napi_get_instance_data
516<!-- YAML
517added:
518 - v12.8.0
519 - v10.20.0
520napiVersion: 6
521-->
522
523```c
524napi_status napi_get_instance_data(napi_env env,
525                                   void** data);
526```
527
528* `[in] env`: The environment that the Node-API call is invoked under.
529* `[out] data`: The data item that was previously associated with the currently
530  running Agent by a call to `napi_set_instance_data()`.
531
532Returns `napi_ok` if the API succeeded.
533
534This API retrieves data that was previously associated with the currently
535running Agent via `napi_set_instance_data()`. If no data is set, the call will
536succeed and `data` will be set to `NULL`.
537
538## Basic Node-API data types
539
540Node-API exposes the following fundamental datatypes as abstractions that are
541consumed by the various APIs. These APIs should be treated as opaque,
542introspectable only with other Node-API calls.
543
544### napi_status
545<!-- YAML
546added: v8.0.0
547napiVersion: 1
548-->
549Integral status code indicating the success or failure of a Node-API call.
550Currently, the following status codes are supported.
551
552```c
553typedef enum {
554  napi_ok,
555  napi_invalid_arg,
556  napi_object_expected,
557  napi_string_expected,
558  napi_name_expected,
559  napi_function_expected,
560  napi_number_expected,
561  napi_boolean_expected,
562  napi_array_expected,
563  napi_generic_failure,
564  napi_pending_exception,
565  napi_cancelled,
566  napi_escape_called_twice,
567  napi_handle_scope_mismatch,
568  napi_callback_scope_mismatch,
569  napi_queue_full,
570  napi_closing,
571  napi_bigint_expected,
572  napi_date_expected,
573  napi_arraybuffer_expected,
574  napi_detachable_arraybuffer_expected,
575  napi_would_deadlock,  /* unused */
576} napi_status;
577```
578
579If additional information is required upon an API returning a failed status,
580it can be obtained by calling `napi_get_last_error_info`.
581
582### napi_extended_error_info
583<!-- YAML
584added: v8.0.0
585napiVersion: 1
586-->
587
588```c
589typedef struct {
590  const char* error_message;
591  void* engine_reserved;
592  uint32_t engine_error_code;
593  napi_status error_code;
594} napi_extended_error_info;
595```
596
597* `error_message`: UTF8-encoded string containing a VM-neutral description of
598  the error.
599* `engine_reserved`: Reserved for VM-specific error details. This is currently
600  not implemented for any VM.
601* `engine_error_code`: VM-specific error code. This is currently
602  not implemented for any VM.
603* `error_code`: The Node-API status code that originated with the last error.
604
605See the [Error handling][] section for additional information.
606
607### napi_env
608
609`napi_env` is used to represent a context that the underlying Node-API
610implementation can use to persist VM-specific state. This structure is passed
611to native functions when they're invoked, and it must be passed back when
612making Node-API calls. Specifically, the same `napi_env` that was passed in when
613the initial native function was called must be passed to any subsequent
614nested Node-API calls. Caching the `napi_env` for the purpose of general reuse,
615and passing the `napi_env` between instances of the same addon running on
616different [`Worker`][] threads is not allowed. The `napi_env` becomes invalid
617when an instance of a native addon is unloaded. Notification of this event is
618delivered through the callbacks given to [`napi_add_env_cleanup_hook`][] and
619[`napi_set_instance_data`][].
620
621### napi_value
622
623This is an opaque pointer that is used to represent a JavaScript value.
624
625### napi_threadsafe_function
626<!-- YAML
627added: v10.6.0
628napiVersion: 4
629-->
630
631This is an opaque pointer that represents a JavaScript function which can be
632called asynchronously from multiple threads via
633`napi_call_threadsafe_function()`.
634
635### napi_threadsafe_function_release_mode
636<!-- YAML
637added: v10.6.0
638napiVersion: 4
639-->
640
641A value to be given to `napi_release_threadsafe_function()` to indicate whether
642the thread-safe function is to be closed immediately (`napi_tsfn_abort`) or
643merely released (`napi_tsfn_release`) and thus available for subsequent use via
644`napi_acquire_threadsafe_function()` and `napi_call_threadsafe_function()`.
645
646```c
647typedef enum {
648  napi_tsfn_release,
649  napi_tsfn_abort
650} napi_threadsafe_function_release_mode;
651```
652
653### napi_threadsafe_function_call_mode
654<!-- YAML
655added: v10.6.0
656napiVersion: 4
657-->
658
659A value to be given to `napi_call_threadsafe_function()` to indicate whether
660the call should block whenever the queue associated with the thread-safe
661function is full.
662
663```c
664typedef enum {
665  napi_tsfn_nonblocking,
666  napi_tsfn_blocking
667} napi_threadsafe_function_call_mode;
668```
669
670### Node-API memory management types
671#### napi_handle_scope
672
673This is an abstraction used to control and modify the lifetime of objects
674created within a particular scope. In general, Node-API values are created
675within the context of a handle scope. When a native method is called from
676JavaScript, a default handle scope will exist. If the user does not explicitly
677create a new handle scope, Node-API values will be created in the default handle
678scope. For any invocations of code outside the execution of a native method
679(for instance, during a libuv callback invocation), the module is required to
680create a scope before invoking any functions that can result in the creation
681of JavaScript values.
682
683Handle scopes are created using [`napi_open_handle_scope`][] and are destroyed
684using [`napi_close_handle_scope`][]. Closing the scope can indicate to the GC
685that all `napi_value`s created during the lifetime of the handle scope are no
686longer referenced from the current stack frame.
687
688For more details, review the [Object lifetime management][].
689
690#### napi_escapable_handle_scope
691<!-- YAML
692added: v8.0.0
693napiVersion: 1
694-->
695Escapable handle scopes are a special type of handle scope to return values
696created within a particular handle scope to a parent scope.
697
698#### napi_ref
699<!-- YAML
700added: v8.0.0
701napiVersion: 1
702-->
703This is the abstraction to use to reference a `napi_value`. This allows for
704users to manage the lifetimes of JavaScript values, including defining their
705minimum lifetimes explicitly.
706
707For more details, review the [Object lifetime management][].
708
709#### napi_type_tag
710<!-- YAML
711added:
712  - v14.8.0
713  - v12.19.0
714napiVersion: 8
715-->
716
717A 128-bit value stored as two unsigned 64-bit integers. It serves as a UUID
718with which JavaScript objects can be "tagged" in order to ensure that they are
719of a certain type. This is a stronger check than [`napi_instanceof`][], because
720the latter can report a false positive if the object's prototype has been
721manipulated. Type-tagging is most useful in conjunction with [`napi_wrap`][]
722because it ensures that the pointer retrieved from a wrapped object can be
723safely cast to the native type corresponding to the type tag that had been
724previously applied to the JavaScript object.
725
726```c
727typedef struct {
728  uint64_t lower;
729  uint64_t upper;
730} napi_type_tag;
731```
732
733#### napi_async_cleanup_hook_handle
734<!-- YAML
735added: v14.10.0
736-->
737
738An opaque value returned by [`napi_add_async_cleanup_hook`][]. It must be passed
739to [`napi_remove_async_cleanup_hook`][] when the chain of asynchronous cleanup
740events completes.
741
742### Node-API callback types
743
744#### napi_callback_info
745<!-- YAML
746added: v8.0.0
747napiVersion: 1
748-->
749Opaque datatype that is passed to a callback function. It can be used for
750getting additional information about the context in which the callback was
751invoked.
752
753#### napi_callback
754<!-- YAML
755added: v8.0.0
756napiVersion: 1
757-->
758Function pointer type for user-provided native functions which are to be
759exposed to JavaScript via Node-API. Callback functions should satisfy the
760following signature:
761
762```c
763typedef napi_value (*napi_callback)(napi_env, napi_callback_info);
764```
765
766Unless for reasons discussed in [Object Lifetime Management][], creating a
767handle and/or callback scope inside a `napi_callback` is not necessary.
768
769#### napi_finalize
770<!-- YAML
771added: v8.0.0
772napiVersion: 1
773-->
774Function pointer type for add-on provided functions that allow the user to be
775notified when externally-owned data is ready to be cleaned up because the
776object with which it was associated with, has been garbage-collected. The user
777must provide a function satisfying the following signature which would get
778called upon the object's collection. Currently, `napi_finalize` can be used for
779finding out when objects that have external data are collected.
780
781```c
782typedef void (*napi_finalize)(napi_env env,
783                              void* finalize_data,
784                              void* finalize_hint);
785```
786
787Unless for reasons discussed in [Object Lifetime Management][], creating a
788handle and/or callback scope inside the function body is not necessary.
789
790#### napi_async_execute_callback
791<!-- YAML
792added: v8.0.0
793napiVersion: 1
794-->
795Function pointer used with functions that support asynchronous
796operations. Callback functions must satisfy the following signature:
797
798```c
799typedef void (*napi_async_execute_callback)(napi_env env, void* data);
800```
801
802Implementations of this function must avoid making Node-API calls that execute
803JavaScript or interact with JavaScript objects. Node-API calls should be in the
804`napi_async_complete_callback` instead. Do not use the `napi_env` parameter as
805it will likely result in execution of JavaScript.
806
807#### napi_async_complete_callback
808<!-- YAML
809added: v8.0.0
810napiVersion: 1
811-->
812Function pointer used with functions that support asynchronous
813operations. Callback functions must satisfy the following signature:
814
815```c
816typedef void (*napi_async_complete_callback)(napi_env env,
817                                             napi_status status,
818                                             void* data);
819```
820
821Unless for reasons discussed in [Object Lifetime Management][], creating a
822handle and/or callback scope inside the function body is not necessary.
823
824#### napi_threadsafe_function_call_js
825<!-- YAML
826added: v10.6.0
827napiVersion: 4
828-->
829
830Function pointer used with asynchronous thread-safe function calls. The callback
831will be called on the main thread. Its purpose is to use a data item arriving
832via the queue from one of the secondary threads to construct the parameters
833necessary for a call into JavaScript, usually via `napi_call_function`, and then
834make the call into JavaScript.
835
836The data arriving from the secondary thread via the queue is given in the `data`
837parameter and the JavaScript function to call is given in the `js_callback`
838parameter.
839
840Node-API sets up the environment prior to calling this callback, so it is
841sufficient to call the JavaScript function via `napi_call_function` rather than
842via `napi_make_callback`.
843
844Callback functions must satisfy the following signature:
845
846```c
847typedef void (*napi_threadsafe_function_call_js)(napi_env env,
848                                                 napi_value js_callback,
849                                                 void* context,
850                                                 void* data);
851```
852
853* `[in] env`: The environment to use for API calls, or `NULL` if the thread-safe
854  function is being torn down and `data` may need to be freed.
855* `[in] js_callback`: The JavaScript function to call, or `NULL` if the
856  thread-safe function is being torn down and `data` may need to be freed. It
857  may also be `NULL` if the thread-safe function was created without
858  `js_callback`.
859* `[in] context`: The optional data with which the thread-safe function was
860  created.
861* `[in] data`: Data created by the secondary thread. It is the responsibility of
862  the callback to convert this native data to JavaScript values (with Node-API
863  functions) that can be passed as parameters when `js_callback` is invoked.
864  This pointer is managed entirely by the threads and this callback. Thus this
865  callback should free the data.
866
867Unless for reasons discussed in [Object Lifetime Management][], creating a
868handle and/or callback scope inside the function body is not necessary.
869
870#### napi_async_cleanup_hook
871<!-- YAML
872added: v14.10.0
873-->
874
875Function pointer used with [`napi_add_async_cleanup_hook`][]. It will be called
876when the environment is being torn down.
877
878Callback functions must satisfy the following signature:
879
880```c
881typedef void (*napi_async_cleanup_hook)(napi_async_cleanup_hook_handle handle,
882                                        void* data);
883```
884
885* `[in] handle`: The handle that must be passed to
886  [`napi_remove_async_cleanup_hook`][] after completion of the asynchronous
887  cleanup.
888* `[in] data`: The data that was passed to [`napi_add_async_cleanup_hook`][].
889
890The body of the function should initiate the asynchronous cleanup actions at the
891end of which `handle` must be passed in a call to
892[`napi_remove_async_cleanup_hook`][].
893
894## Error handling
895
896Node-API uses both return values and JavaScript exceptions for error handling.
897The following sections explain the approach for each case.
898
899### Return values
900
901All of the Node-API functions share the same error handling pattern. The
902return type of all API functions is `napi_status`.
903
904The return value will be `napi_ok` if the request was successful and
905no uncaught JavaScript exception was thrown. If an error occurred AND
906an exception was thrown, the `napi_status` value for the error
907will be returned. If an exception was thrown, and no error occurred,
908`napi_pending_exception` will be returned.
909
910In cases where a return value other than `napi_ok` or
911`napi_pending_exception` is returned, [`napi_is_exception_pending`][]
912must be called to check if an exception is pending.
913See the section on exceptions for more details.
914
915The full set of possible `napi_status` values is defined
916in `napi_api_types.h`.
917
918The `napi_status` return value provides a VM-independent representation of
919the error which occurred. In some cases it is useful to be able to get
920more detailed information, including a string representing the error as well as
921VM (engine)-specific information.
922
923In order to retrieve this information [`napi_get_last_error_info`][]
924is provided which returns a `napi_extended_error_info` structure.
925The format of the `napi_extended_error_info` structure is as follows:
926
927<!-- YAML
928added: v8.0.0
929napiVersion: 1
930-->
931
932```c
933typedef struct napi_extended_error_info {
934  const char* error_message;
935  void* engine_reserved;
936  uint32_t engine_error_code;
937  napi_status error_code;
938};
939```
940
941* `error_message`: Textual representation of the error that occurred.
942* `engine_reserved`: Opaque handle reserved for engine use only.
943* `engine_error_code`: VM specific error code.
944* `error_code`: Node-API status code for the last error.
945
946[`napi_get_last_error_info`][] returns the information for the last
947Node-API call that was made.
948
949Do not rely on the content or format of any of the extended information as it
950is not subject to SemVer and may change at any time. It is intended only for
951logging purposes.
952
953#### napi_get_last_error_info
954<!-- YAML
955added: v8.0.0
956napiVersion: 1
957-->
958
959```c
960napi_status
961napi_get_last_error_info(napi_env env,
962                         const napi_extended_error_info** result);
963```
964
965* `[in] env`: The environment that the API is invoked under.
966* `[out] result`: The `napi_extended_error_info` structure with more
967  information about the error.
968
969Returns `napi_ok` if the API succeeded.
970
971This API retrieves a `napi_extended_error_info` structure with information
972about the last error that occurred.
973
974The content of the `napi_extended_error_info` returned is only valid up until
975a Node-API function is called on the same `env`.
976
977Do not rely on the content or format of any of the extended information as it
978is not subject to SemVer and may change at any time. It is intended only for
979logging purposes.
980
981This API can be called even if there is a pending JavaScript exception.
982
983### Exceptions
984
985Any Node-API function call may result in a pending JavaScript exception. This is
986the case for any of the API functions, even those that may not cause the
987execution of JavaScript.
988
989If the `napi_status` returned by a function is `napi_ok` then no
990exception is pending and no additional action is required. If the
991`napi_status` returned is anything other than `napi_ok` or
992`napi_pending_exception`, in order to try to recover and continue
993instead of simply returning immediately, [`napi_is_exception_pending`][]
994must be called in order to determine if an exception is pending or not.
995
996In many cases when a Node-API function is called and an exception is
997already pending, the function will return immediately with a
998`napi_status` of `napi_pending_exception`. However, this is not the case
999for all functions. Node-API allows a subset of the functions to be
1000called to allow for some minimal cleanup before returning to JavaScript.
1001In that case, `napi_status` will reflect the status for the function. It
1002will not reflect previous pending exceptions. To avoid confusion, check
1003the error status after every function call.
1004
1005When an exception is pending one of two approaches can be employed.
1006
1007The first approach is to do any appropriate cleanup and then return so that
1008execution will return to JavaScript. As part of the transition back to
1009JavaScript, the exception will be thrown at the point in the JavaScript
1010code where the native method was invoked. The behavior of most Node-API calls
1011is unspecified while an exception is pending, and many will simply return
1012`napi_pending_exception`, so do as little as possible and then return to
1013JavaScript where the exception can be handled.
1014
1015The second approach is to try to handle the exception. There will be cases
1016where the native code can catch the exception, take the appropriate action,
1017and then continue. This is only recommended in specific cases
1018where it is known that the exception can be safely handled. In these
1019cases [`napi_get_and_clear_last_exception`][] can be used to get and
1020clear the exception. On success, result will contain the handle to
1021the last JavaScript `Object` thrown. If it is determined, after
1022retrieving the exception, the exception cannot be handled after all
1023it can be re-thrown it with [`napi_throw`][] where error is the
1024JavaScript value to be thrown.
1025
1026The following utility functions are also available in case native code
1027needs to throw an exception or determine if a `napi_value` is an instance
1028of a JavaScript `Error` object: [`napi_throw_error`][],
1029[`napi_throw_type_error`][], [`napi_throw_range_error`][] and
1030[`napi_is_error`][].
1031
1032The following utility functions are also available in case native
1033code needs to create an `Error` object: [`napi_create_error`][],
1034[`napi_create_type_error`][], and [`napi_create_range_error`][],
1035where result is the `napi_value` that refers to the newly created
1036JavaScript `Error` object.
1037
1038The Node.js project is adding error codes to all of the errors
1039generated internally. The goal is for applications to use these
1040error codes for all error checking. The associated error messages
1041will remain, but will only be meant to be used for logging and
1042display with the expectation that the message can change without
1043SemVer applying. In order to support this model with Node-API, both
1044in internal functionality and for module specific functionality
1045(as its good practice), the `throw_` and `create_` functions
1046take an optional code parameter which is the string for the code
1047to be added to the error object. If the optional parameter is `NULL`
1048then no code will be associated with the error. If a code is provided,
1049the name associated with the error is also updated to be:
1050
1051```text
1052originalName [code]
1053```
1054
1055where `originalName` is the original name associated with the error
1056and `code` is the code that was provided. For example, if the code
1057is `'ERR_ERROR_1'` and a `TypeError` is being created the name will be:
1058
1059```text
1060TypeError [ERR_ERROR_1]
1061```
1062
1063#### napi_throw
1064<!-- YAML
1065added: v8.0.0
1066napiVersion: 1
1067-->
1068
1069```c
1070NAPI_EXTERN napi_status napi_throw(napi_env env, napi_value error);
1071```
1072
1073* `[in] env`: The environment that the API is invoked under.
1074* `[in] error`: The JavaScript value to be thrown.
1075
1076Returns `napi_ok` if the API succeeded.
1077
1078This API throws the JavaScript value provided.
1079
1080#### napi_throw_error
1081<!-- YAML
1082added: v8.0.0
1083napiVersion: 1
1084-->
1085
1086```c
1087NAPI_EXTERN napi_status napi_throw_error(napi_env env,
1088                                         const char* code,
1089                                         const char* msg);
1090```
1091
1092* `[in] env`: The environment that the API is invoked under.
1093* `[in] code`: Optional error code to be set on the error.
1094* `[in] msg`: C string representing the text to be associated with the error.
1095
1096Returns `napi_ok` if the API succeeded.
1097
1098This API throws a JavaScript `Error` with the text provided.
1099
1100#### napi_throw_type_error
1101<!-- YAML
1102added: v8.0.0
1103napiVersion: 1
1104-->
1105
1106```c
1107NAPI_EXTERN napi_status napi_throw_type_error(napi_env env,
1108                                              const char* code,
1109                                              const char* msg);
1110```
1111
1112* `[in] env`: The environment that the API is invoked under.
1113* `[in] code`: Optional error code to be set on the error.
1114* `[in] msg`: C string representing the text to be associated with the error.
1115
1116Returns `napi_ok` if the API succeeded.
1117
1118This API throws a JavaScript `TypeError` with the text provided.
1119
1120#### napi_throw_range_error
1121<!-- YAML
1122added: v8.0.0
1123napiVersion: 1
1124-->
1125
1126```c
1127NAPI_EXTERN napi_status napi_throw_range_error(napi_env env,
1128                                               const char* code,
1129                                               const char* msg);
1130```
1131
1132* `[in] env`: The environment that the API is invoked under.
1133* `[in] code`: Optional error code to be set on the error.
1134* `[in] msg`: C string representing the text to be associated with the error.
1135
1136Returns `napi_ok` if the API succeeded.
1137
1138This API throws a JavaScript `RangeError` with the text provided.
1139
1140#### napi_is_error
1141<!-- YAML
1142added: v8.0.0
1143napiVersion: 1
1144-->
1145
1146```c
1147NAPI_EXTERN napi_status napi_is_error(napi_env env,
1148                                      napi_value value,
1149                                      bool* result);
1150```
1151
1152* `[in] env`: The environment that the API is invoked under.
1153* `[in] value`: The `napi_value` to be checked.
1154* `[out] result`: Boolean value that is set to true if `napi_value` represents
1155  an error, false otherwise.
1156
1157Returns `napi_ok` if the API succeeded.
1158
1159This API queries a `napi_value` to check if it represents an error object.
1160
1161#### napi_create_error
1162<!-- YAML
1163added: v8.0.0
1164napiVersion: 1
1165-->
1166
1167```c
1168NAPI_EXTERN napi_status napi_create_error(napi_env env,
1169                                          napi_value code,
1170                                          napi_value msg,
1171                                          napi_value* result);
1172```
1173
1174* `[in] env`: The environment that the API is invoked under.
1175* `[in] code`: Optional `napi_value` with the string for the error code to be
1176  associated with the error.
1177* `[in] msg`: `napi_value` that references a JavaScript `string` to be used as
1178  the message for the `Error`.
1179* `[out] result`: `napi_value` representing the error created.
1180
1181Returns `napi_ok` if the API succeeded.
1182
1183This API returns a JavaScript `Error` with the text provided.
1184
1185#### napi_create_type_error
1186<!-- YAML
1187added: v8.0.0
1188napiVersion: 1
1189-->
1190
1191```c
1192NAPI_EXTERN napi_status napi_create_type_error(napi_env env,
1193                                               napi_value code,
1194                                               napi_value msg,
1195                                               napi_value* result);
1196```
1197
1198* `[in] env`: The environment that the API is invoked under.
1199* `[in] code`: Optional `napi_value` with the string for the error code to be
1200  associated with the error.
1201* `[in] msg`: `napi_value` that references a JavaScript `string` to be used as
1202  the message for the `Error`.
1203* `[out] result`: `napi_value` representing the error created.
1204
1205Returns `napi_ok` if the API succeeded.
1206
1207This API returns a JavaScript `TypeError` with the text provided.
1208
1209#### napi_create_range_error
1210<!-- YAML
1211added: v8.0.0
1212napiVersion: 1
1213-->
1214
1215```c
1216NAPI_EXTERN napi_status napi_create_range_error(napi_env env,
1217                                                napi_value code,
1218                                                napi_value msg,
1219                                                napi_value* result);
1220```
1221
1222* `[in] env`: The environment that the API is invoked under.
1223* `[in] code`: Optional `napi_value` with the string for the error code to be
1224  associated with the error.
1225* `[in] msg`: `napi_value` that references a JavaScript `string` to be used as
1226  the message for the `Error`.
1227* `[out] result`: `napi_value` representing the error created.
1228
1229Returns `napi_ok` if the API succeeded.
1230
1231This API returns a JavaScript `RangeError` with the text provided.
1232
1233#### napi_get_and_clear_last_exception
1234<!-- YAML
1235added: v8.0.0
1236napiVersion: 1
1237-->
1238
1239```c
1240napi_status napi_get_and_clear_last_exception(napi_env env,
1241                                              napi_value* result);
1242```
1243
1244* `[in] env`: The environment that the API is invoked under.
1245* `[out] result`: The exception if one is pending, `NULL` otherwise.
1246
1247Returns `napi_ok` if the API succeeded.
1248
1249This API can be called even if there is a pending JavaScript exception.
1250
1251#### napi_is_exception_pending
1252<!-- YAML
1253added: v8.0.0
1254napiVersion: 1
1255-->
1256
1257```c
1258napi_status napi_is_exception_pending(napi_env env, bool* result);
1259```
1260
1261* `[in] env`: The environment that the API is invoked under.
1262* `[out] result`: Boolean value that is set to true if an exception is pending.
1263
1264Returns `napi_ok` if the API succeeded.
1265
1266This API can be called even if there is a pending JavaScript exception.
1267
1268#### napi_fatal_exception
1269<!-- YAML
1270added: v9.10.0
1271napiVersion: 3
1272-->
1273
1274```c
1275napi_status napi_fatal_exception(napi_env env, napi_value err);
1276```
1277
1278* `[in] env`: The environment that the API is invoked under.
1279* `[in] err`: The error that is passed to `'uncaughtException'`.
1280
1281Trigger an `'uncaughtException'` in JavaScript. Useful if an async
1282callback throws an exception with no way to recover.
1283
1284### Fatal errors
1285
1286In the event of an unrecoverable error in a native module, a fatal error can be
1287thrown to immediately terminate the process.
1288
1289#### napi_fatal_error
1290<!-- YAML
1291added: v8.2.0
1292napiVersion: 1
1293-->
1294
1295```c
1296NAPI_NO_RETURN void napi_fatal_error(const char* location,
1297                                     size_t location_len,
1298                                     const char* message,
1299                                     size_t message_len);
1300```
1301
1302* `[in] location`: Optional location at which the error occurred.
1303* `[in] location_len`: The length of the location in bytes, or
1304  `NAPI_AUTO_LENGTH` if it is null-terminated.
1305* `[in] message`: The message associated with the error.
1306* `[in] message_len`: The length of the message in bytes, or `NAPI_AUTO_LENGTH`
1307  if it is null-terminated.
1308
1309The function call does not return, the process will be terminated.
1310
1311This API can be called even if there is a pending JavaScript exception.
1312
1313## Object lifetime management
1314
1315As Node-API calls are made, handles to objects in the heap for the underlying
1316VM may be returned as `napi_values`. These handles must hold the
1317objects 'live' until they are no longer required by the native code,
1318otherwise the objects could be collected before the native code was
1319finished using them.
1320
1321As object handles are returned they are associated with a
1322'scope'. The lifespan for the default scope is tied to the lifespan
1323of the native method call. The result is that, by default, handles
1324remain valid and the objects associated with these handles will be
1325held live for the lifespan of the native method call.
1326
1327In many cases, however, it is necessary that the handles remain valid for
1328either a shorter or longer lifespan than that of the native method.
1329The sections which follow describe the Node-API functions that can be used
1330to change the handle lifespan from the default.
1331
1332### Making handle lifespan shorter than that of the native method
1333It is often necessary to make the lifespan of handles shorter than
1334the lifespan of a native method. For example, consider a native method
1335that has a loop which iterates through the elements in a large array:
1336
1337```c
1338for (int i = 0; i < 1000000; i++) {
1339  napi_value result;
1340  napi_status status = napi_get_element(env, object, i, &result);
1341  if (status != napi_ok) {
1342    break;
1343  }
1344  // do something with element
1345}
1346```
1347
1348This would result in a large number of handles being created, consuming
1349substantial resources. In addition, even though the native code could only
1350use the most recent handle, all of the associated objects would also be
1351kept alive since they all share the same scope.
1352
1353To handle this case, Node-API provides the ability to establish a new 'scope' to
1354which newly created handles will be associated. Once those handles
1355are no longer required, the scope can be 'closed' and any handles associated
1356with the scope are invalidated. The methods available to open/close scopes are
1357[`napi_open_handle_scope`][] and [`napi_close_handle_scope`][].
1358
1359Node-API only supports a single nested hierarchy of scopes. There is only one
1360active scope at any time, and all new handles will be associated with that
1361scope while it is active. Scopes must be closed in the reverse order from
1362which they are opened. In addition, all scopes created within a native method
1363must be closed before returning from that method.
1364
1365Taking the earlier example, adding calls to [`napi_open_handle_scope`][] and
1366[`napi_close_handle_scope`][] would ensure that at most a single handle
1367is valid throughout the execution of the loop:
1368
1369```c
1370for (int i = 0; i < 1000000; i++) {
1371  napi_handle_scope scope;
1372  napi_status status = napi_open_handle_scope(env, &scope);
1373  if (status != napi_ok) {
1374    break;
1375  }
1376  napi_value result;
1377  status = napi_get_element(env, object, i, &result);
1378  if (status != napi_ok) {
1379    break;
1380  }
1381  // do something with element
1382  status = napi_close_handle_scope(env, scope);
1383  if (status != napi_ok) {
1384    break;
1385  }
1386}
1387```
1388
1389When nesting scopes, there are cases where a handle from an
1390inner scope needs to live beyond the lifespan of that scope. Node-API supports
1391an 'escapable scope' in order to support this case. An escapable scope
1392allows one handle to be 'promoted' so that it 'escapes' the
1393current scope and the lifespan of the handle changes from the current
1394scope to that of the outer scope.
1395
1396The methods available to open/close escapable scopes are
1397[`napi_open_escapable_handle_scope`][] and
1398[`napi_close_escapable_handle_scope`][].
1399
1400The request to promote a handle is made through [`napi_escape_handle`][] which
1401can only be called once.
1402
1403#### napi_open_handle_scope
1404<!-- YAML
1405added: v8.0.0
1406napiVersion: 1
1407-->
1408
1409```c
1410NAPI_EXTERN napi_status napi_open_handle_scope(napi_env env,
1411                                               napi_handle_scope* result);
1412```
1413
1414* `[in] env`: The environment that the API is invoked under.
1415* `[out] result`: `napi_value` representing the new scope.
1416
1417Returns `napi_ok` if the API succeeded.
1418
1419This API opens a new scope.
1420
1421#### napi_close_handle_scope
1422<!-- YAML
1423added: v8.0.0
1424napiVersion: 1
1425-->
1426
1427```c
1428NAPI_EXTERN napi_status napi_close_handle_scope(napi_env env,
1429                                                napi_handle_scope scope);
1430```
1431
1432* `[in] env`: The environment that the API is invoked under.
1433* `[in] scope`: `napi_value` representing the scope to be closed.
1434
1435Returns `napi_ok` if the API succeeded.
1436
1437This API closes the scope passed in. Scopes must be closed in the
1438reverse order from which they were created.
1439
1440This API can be called even if there is a pending JavaScript exception.
1441
1442#### napi_open_escapable_handle_scope
1443<!-- YAML
1444added: v8.0.0
1445napiVersion: 1
1446-->
1447
1448```c
1449NAPI_EXTERN napi_status
1450    napi_open_escapable_handle_scope(napi_env env,
1451                                     napi_handle_scope* result);
1452```
1453
1454* `[in] env`: The environment that the API is invoked under.
1455* `[out] result`: `napi_value` representing the new scope.
1456
1457Returns `napi_ok` if the API succeeded.
1458
1459This API opens a new scope from which one object can be promoted
1460to the outer scope.
1461
1462#### napi_close_escapable_handle_scope
1463<!-- YAML
1464added: v8.0.0
1465napiVersion: 1
1466-->
1467
1468```c
1469NAPI_EXTERN napi_status
1470    napi_close_escapable_handle_scope(napi_env env,
1471                                      napi_handle_scope scope);
1472```
1473
1474* `[in] env`: The environment that the API is invoked under.
1475* `[in] scope`: `napi_value` representing the scope to be closed.
1476
1477Returns `napi_ok` if the API succeeded.
1478
1479This API closes the scope passed in. Scopes must be closed in the
1480reverse order from which they were created.
1481
1482This API can be called even if there is a pending JavaScript exception.
1483
1484#### napi_escape_handle
1485<!-- YAML
1486added: v8.0.0
1487napiVersion: 1
1488-->
1489
1490```c
1491napi_status napi_escape_handle(napi_env env,
1492                               napi_escapable_handle_scope scope,
1493                               napi_value escapee,
1494                               napi_value* result);
1495```
1496
1497* `[in] env`: The environment that the API is invoked under.
1498* `[in] scope`: `napi_value` representing the current scope.
1499* `[in] escapee`: `napi_value` representing the JavaScript `Object` to be
1500  escaped.
1501* `[out] result`: `napi_value` representing the handle to the escaped `Object`
1502  in the outer scope.
1503
1504Returns `napi_ok` if the API succeeded.
1505
1506This API promotes the handle to the JavaScript object so that it is valid
1507for the lifetime of the outer scope. It can only be called once per scope.
1508If it is called more than once an error will be returned.
1509
1510This API can be called even if there is a pending JavaScript exception.
1511
1512### References to objects with a lifespan longer than that of the native method
1513
1514In some cases an addon will need to be able to create and reference objects
1515with a lifespan longer than that of a single native method invocation. For
1516example, to create a constructor and later use that constructor
1517in a request to creates instances, it must be possible to reference
1518the constructor object across many different instance creation requests. This
1519would not be possible with a normal handle returned as a `napi_value` as
1520described in the earlier section. The lifespan of a normal handle is
1521managed by scopes and all scopes must be closed before the end of a native
1522method.
1523
1524Node-API provides methods to create persistent references to an object.
1525Each persistent reference has an associated count with a value of 0
1526or higher. The count determines if the reference will keep
1527the corresponding object live. References with a count of 0 do not
1528prevent the object from being collected and are often called 'weak'
1529references. Any count greater than 0 will prevent the object
1530from being collected.
1531
1532References can be created with an initial reference count. The count can
1533then be modified through [`napi_reference_ref`][] and
1534[`napi_reference_unref`][]. If an object is collected while the count
1535for a reference is 0, all subsequent calls to
1536get the object associated with the reference [`napi_get_reference_value`][]
1537will return `NULL` for the returned `napi_value`. An attempt to call
1538[`napi_reference_ref`][] for a reference whose object has been collected
1539results in an error.
1540
1541References must be deleted once they are no longer required by the addon. When
1542a reference is deleted, it will no longer prevent the corresponding object from
1543being collected. Failure to delete a persistent reference results in
1544a 'memory leak' with both the native memory for the persistent reference and
1545the corresponding object on the heap being retained forever.
1546
1547There can be multiple persistent references created which refer to the same
1548object, each of which will either keep the object live or not based on its
1549individual count.
1550
1551#### napi_create_reference
1552<!-- YAML
1553added: v8.0.0
1554napiVersion: 1
1555-->
1556
1557```c
1558NAPI_EXTERN napi_status napi_create_reference(napi_env env,
1559                                              napi_value value,
1560                                              uint32_t initial_refcount,
1561                                              napi_ref* result);
1562```
1563
1564* `[in] env`: The environment that the API is invoked under.
1565* `[in] value`: `napi_value` representing the `Object` to which we want a
1566  reference.
1567* `[in] initial_refcount`: Initial reference count for the new reference.
1568* `[out] result`: `napi_ref` pointing to the new reference.
1569
1570Returns `napi_ok` if the API succeeded.
1571
1572This API create a new reference with the specified reference count
1573to the `Object` passed in.
1574
1575#### napi_delete_reference
1576<!-- YAML
1577added: v8.0.0
1578napiVersion: 1
1579-->
1580
1581```c
1582NAPI_EXTERN napi_status napi_delete_reference(napi_env env, napi_ref ref);
1583```
1584
1585* `[in] env`: The environment that the API is invoked under.
1586* `[in] ref`: `napi_ref` to be deleted.
1587
1588Returns `napi_ok` if the API succeeded.
1589
1590This API deletes the reference passed in.
1591
1592This API can be called even if there is a pending JavaScript exception.
1593
1594#### napi_reference_ref
1595<!-- YAML
1596added: v8.0.0
1597napiVersion: 1
1598-->
1599
1600```c
1601NAPI_EXTERN napi_status napi_reference_ref(napi_env env,
1602                                           napi_ref ref,
1603                                           uint32_t* result);
1604```
1605
1606* `[in] env`: The environment that the API is invoked under.
1607* `[in] ref`: `napi_ref` for which the reference count will be incremented.
1608* `[out] result`: The new reference count.
1609
1610Returns `napi_ok` if the API succeeded.
1611
1612This API increments the reference count for the reference
1613passed in and returns the resulting reference count.
1614
1615#### napi_reference_unref
1616<!-- YAML
1617added: v8.0.0
1618napiVersion: 1
1619-->
1620
1621```c
1622NAPI_EXTERN napi_status napi_reference_unref(napi_env env,
1623                                             napi_ref ref,
1624                                             uint32_t* result);
1625```
1626
1627* `[in] env`: The environment that the API is invoked under.
1628* `[in] ref`: `napi_ref` for which the reference count will be decremented.
1629* `[out] result`: The new reference count.
1630
1631Returns `napi_ok` if the API succeeded.
1632
1633This API decrements the reference count for the reference
1634passed in and returns the resulting reference count.
1635
1636#### napi_get_reference_value
1637<!-- YAML
1638added: v8.0.0
1639napiVersion: 1
1640-->
1641
1642```c
1643NAPI_EXTERN napi_status napi_get_reference_value(napi_env env,
1644                                                 napi_ref ref,
1645                                                 napi_value* result);
1646```
1647
1648the `napi_value passed` in or out of these methods is a handle to the
1649object to which the reference is related.
1650
1651* `[in] env`: The environment that the API is invoked under.
1652* `[in] ref`: `napi_ref` for which we requesting the corresponding `Object`.
1653* `[out] result`: The `napi_value` for the `Object` referenced by the
1654  `napi_ref`.
1655
1656Returns `napi_ok` if the API succeeded.
1657
1658If still valid, this API returns the `napi_value` representing the
1659JavaScript `Object` associated with the `napi_ref`. Otherwise, result
1660will be `NULL`.
1661
1662### Cleanup on exit of the current Node.js instance
1663
1664While a Node.js process typically releases all its resources when exiting,
1665embedders of Node.js, or future Worker support, may require addons to register
1666clean-up hooks that will be run once the current Node.js instance exits.
1667
1668Node-API provides functions for registering and un-registering such callbacks.
1669When those callbacks are run, all resources that are being held by the addon
1670should be freed up.
1671
1672#### napi_add_env_cleanup_hook
1673<!-- YAML
1674added: v10.2.0
1675napiVersion: 3
1676-->
1677
1678```c
1679NODE_EXTERN napi_status napi_add_env_cleanup_hook(napi_env env,
1680                                                  void (*fun)(void* arg),
1681                                                  void* arg);
1682```
1683
1684Registers `fun` as a function to be run with the `arg` parameter once the
1685current Node.js environment exits.
1686
1687A function can safely be specified multiple times with different
1688`arg` values. In that case, it will be called multiple times as well.
1689Providing the same `fun` and `arg` values multiple times is not allowed
1690and will lead the process to abort.
1691
1692The hooks will be called in reverse order, i.e. the most recently added one
1693will be called first.
1694
1695Removing this hook can be done by using [`napi_remove_env_cleanup_hook`][].
1696Typically, that happens when the resource for which this hook was added
1697is being torn down anyway.
1698
1699For asynchronous cleanup, [`napi_add_async_cleanup_hook`][] is available.
1700
1701#### napi_remove_env_cleanup_hook
1702<!-- YAML
1703added: v10.2.0
1704napiVersion: 3
1705-->
1706
1707```c
1708NAPI_EXTERN napi_status napi_remove_env_cleanup_hook(napi_env env,
1709                                                     void (*fun)(void* arg),
1710                                                     void* arg);
1711```
1712
1713Unregisters `fun` as a function to be run with the `arg` parameter once the
1714current Node.js environment exits. Both the argument and the function value
1715need to be exact matches.
1716
1717The function must have originally been registered
1718with `napi_add_env_cleanup_hook`, otherwise the process will abort.
1719
1720#### napi_add_async_cleanup_hook
1721<!-- YAML
1722added:
1723  - v14.8.0
1724  - v12.19.0
1725napiVersion: 8
1726changes:
1727  - version: v14.10.0
1728    pr-url: https://github.com/nodejs/node/pull/34819
1729    description: Changed signature of the `hook` callback.
1730-->
1731
1732```c
1733NAPI_EXTERN napi_status napi_add_async_cleanup_hook(
1734    napi_env env,
1735    napi_async_cleanup_hook hook,
1736    void* arg,
1737    napi_async_cleanup_hook_handle* remove_handle);
1738```
1739
1740* `[in] env`: The environment that the API is invoked under.
1741* `[in] hook`: The function pointer to call at environment teardown.
1742* `[in] arg`: The pointer to pass to `hook` when it gets called.
1743* `[out] remove_handle`: Optional handle that refers to the asynchronous cleanup
1744  hook.
1745
1746Registers `hook`, which is a function of type [`napi_async_cleanup_hook`][], as
1747a function to be run with the `remove_handle` and `arg` parameters once the
1748current Node.js environment exits.
1749
1750Unlike [`napi_add_env_cleanup_hook`][], the hook is allowed to be asynchronous.
1751
1752Otherwise, behavior generally matches that of [`napi_add_env_cleanup_hook`][].
1753
1754If `remove_handle` is not `NULL`, an opaque value will be stored in it
1755that must later be passed to [`napi_remove_async_cleanup_hook`][],
1756regardless of whether the hook has already been invoked.
1757Typically, that happens when the resource for which this hook was added
1758is being torn down anyway.
1759
1760#### napi_remove_async_cleanup_hook
1761<!-- YAML
1762added: v14.8.0
1763changes:
1764  - version: v14.10.0
1765    pr-url: https://github.com/nodejs/node/pull/34819
1766    description: Removed `env` parameter.
1767-->
1768
1769```c
1770NAPI_EXTERN napi_status napi_remove_async_cleanup_hook(
1771    napi_async_cleanup_hook_handle remove_handle);
1772```
1773
1774* `[in] remove_handle`: The handle to an asynchronous cleanup hook that was
1775  created with [`napi_add_async_cleanup_hook`][].
1776
1777Unregisters the cleanup hook corresponding to `remove_handle`. This will prevent
1778the hook from being executed, unless it has already started executing.
1779This must be called on any `napi_async_cleanup_hook_handle` value obtained
1780from [`napi_add_async_cleanup_hook`][].
1781
1782## Module registration
1783Node-API modules are registered in a manner similar to other modules
1784except that instead of using the `NODE_MODULE` macro the following
1785is used:
1786
1787```c
1788NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
1789```
1790
1791The next difference is the signature for the `Init` method. For a Node-API
1792module it is as follows:
1793
1794```c
1795napi_value Init(napi_env env, napi_value exports);
1796```
1797
1798The return value from `Init` is treated as the `exports` object for the module.
1799The `Init` method is passed an empty object via the `exports` parameter as a
1800convenience. If `Init` returns `NULL`, the parameter passed as `exports` is
1801exported by the module. Node-API modules cannot modify the `module` object but
1802can specify anything as the `exports` property of the module.
1803
1804To add the method `hello` as a function so that it can be called as a method
1805provided by the addon:
1806
1807```c
1808napi_value Init(napi_env env, napi_value exports) {
1809  napi_status status;
1810  napi_property_descriptor desc = {
1811    "hello",
1812    NULL,
1813    Method,
1814    NULL,
1815    NULL,
1816    NULL,
1817    napi_writable | napi_enumerable | napi_configurable,
1818    NULL
1819  };
1820  status = napi_define_properties(env, exports, 1, &desc);
1821  if (status != napi_ok) return NULL;
1822  return exports;
1823}
1824```
1825
1826To set a function to be returned by the `require()` for the addon:
1827
1828```c
1829napi_value Init(napi_env env, napi_value exports) {
1830  napi_value method;
1831  napi_status status;
1832  status = napi_create_function(env, "exports", NAPI_AUTO_LENGTH, Method, NULL, &method);
1833  if (status != napi_ok) return NULL;
1834  return method;
1835}
1836```
1837
1838To define a class so that new instances can be created (often used with
1839[Object wrap][]):
1840
1841```c
1842// NOTE: partial example, not all referenced code is included
1843napi_value Init(napi_env env, napi_value exports) {
1844  napi_status status;
1845  napi_property_descriptor properties[] = {
1846    { "value", NULL, NULL, GetValue, SetValue, NULL, napi_writable | napi_configurable, NULL },
1847    DECLARE_NAPI_METHOD("plusOne", PlusOne),
1848    DECLARE_NAPI_METHOD("multiply", Multiply),
1849  };
1850
1851  napi_value cons;
1852  status =
1853      napi_define_class(env, "MyObject", New, NULL, 3, properties, &cons);
1854  if (status != napi_ok) return NULL;
1855
1856  status = napi_create_reference(env, cons, 1, &constructor);
1857  if (status != napi_ok) return NULL;
1858
1859  status = napi_set_named_property(env, exports, "MyObject", cons);
1860  if (status != napi_ok) return NULL;
1861
1862  return exports;
1863}
1864```
1865
1866You can also use the `NAPI_MODULE_INIT` macro, which acts as a shorthand
1867for `NAPI_MODULE` and defining an `Init` function:
1868
1869```c
1870NAPI_MODULE_INIT() {
1871  napi_value answer;
1872  napi_status result;
1873
1874  status = napi_create_int64(env, 42, &answer);
1875  if (status != napi_ok) return NULL;
1876
1877  status = napi_set_named_property(env, exports, "answer", answer);
1878  if (status != napi_ok) return NULL;
1879
1880  return exports;
1881}
1882```
1883
1884All Node-API addons are context-aware, meaning they may be loaded multiple
1885times. There are a few design considerations when declaring such a module.
1886The documentation on [context-aware addons][] provides more details.
1887
1888The variables `env` and `exports` will be available inside the function body
1889following the macro invocation.
1890
1891For more details on setting properties on objects, see the section on
1892[Working with JavaScript properties][].
1893
1894For more details on building addon modules in general, refer to the existing
1895API.
1896
1897## Working with JavaScript values
1898Node-API exposes a set of APIs to create all types of JavaScript values.
1899Some of these types are documented under [Section 6][]
1900of the [ECMAScript Language Specification][].
1901
1902Fundamentally, these APIs are used to do one of the following:
1903
19041. Create a new JavaScript object
19052. Convert from a primitive C type to a Node-API value
19063. Convert from Node-API value to a primitive C type
19074. Get global instances including `undefined` and `null`
1908
1909Node-API values are represented by the type `napi_value`.
1910Any Node-API call that requires a JavaScript value takes in a `napi_value`.
1911In some cases, the API does check the type of the `napi_value` up-front.
1912However, for better performance, it's better for the caller to make sure that
1913the `napi_value` in question is of the JavaScript type expected by the API.
1914
1915### Enum types
1916#### napi_key_collection_mode
1917<!-- YAML
1918added:
1919 - v13.7.0
1920 - v10.20.0
1921napiVersion: 6
1922-->
1923
1924```c
1925typedef enum {
1926  napi_key_include_prototypes,
1927  napi_key_own_only
1928} napi_key_collection_mode;
1929```
1930
1931Describes the `Keys/Properties` filter enums:
1932
1933`napi_key_collection_mode` limits the range of collected properties.
1934
1935`napi_key_own_only` limits the collected properties to the given
1936object only. `napi_key_include_prototypes` will include all keys
1937of the objects's prototype chain as well.
1938
1939#### napi_key_filter
1940<!-- YAML
1941added:
1942 - v13.7.0
1943 - v10.20.0
1944napiVersion: 6
1945-->
1946
1947```c
1948typedef enum {
1949  napi_key_all_properties = 0,
1950  napi_key_writable = 1,
1951  napi_key_enumerable = 1 << 1,
1952  napi_key_configurable = 1 << 2,
1953  napi_key_skip_strings = 1 << 3,
1954  napi_key_skip_symbols = 1 << 4
1955} napi_key_filter;
1956```
1957
1958Property filter bits. They can be or'ed to build a composite filter.
1959
1960#### napi_key_conversion
1961<!-- YAML
1962added:
1963 - v13.7.0
1964 - v10.20.0
1965napiVersion: 6
1966-->
1967
1968```c
1969typedef enum {
1970  napi_key_keep_numbers,
1971  napi_key_numbers_to_strings
1972} napi_key_conversion;
1973```
1974
1975`napi_key_numbers_to_strings` will convert integer indices to
1976strings. `napi_key_keep_numbers` will return numbers for integer
1977indices.
1978
1979#### napi_valuetype
1980
1981```c
1982typedef enum {
1983  // ES6 types (corresponds to typeof)
1984  napi_undefined,
1985  napi_null,
1986  napi_boolean,
1987  napi_number,
1988  napi_string,
1989  napi_symbol,
1990  napi_object,
1991  napi_function,
1992  napi_external,
1993  napi_bigint,
1994} napi_valuetype;
1995```
1996
1997Describes the type of a `napi_value`. This generally corresponds to the types
1998described in [Section 6.1][] of the ECMAScript Language Specification.
1999In addition to types in that section, `napi_valuetype` can also represent
2000`Function`s and `Object`s with external data.
2001
2002A JavaScript value of type `napi_external` appears in JavaScript as a plain
2003object such that no properties can be set on it, and no prototype.
2004
2005#### napi_typedarray_type
2006
2007```c
2008typedef enum {
2009  napi_int8_array,
2010  napi_uint8_array,
2011  napi_uint8_clamped_array,
2012  napi_int16_array,
2013  napi_uint16_array,
2014  napi_int32_array,
2015  napi_uint32_array,
2016  napi_float32_array,
2017  napi_float64_array,
2018  napi_bigint64_array,
2019  napi_biguint64_array,
2020} napi_typedarray_type;
2021```
2022
2023This represents the underlying binary scalar datatype of the `TypedArray`.
2024Elements of this enum correspond to
2025[Section 22.2][] of the [ECMAScript Language Specification][].
2026
2027### Object creation functions
2028#### napi_create_array
2029<!-- YAML
2030added: v8.0.0
2031napiVersion: 1
2032-->
2033
2034```c
2035napi_status napi_create_array(napi_env env, napi_value* result)
2036```
2037
2038* `[in] env`: The environment that the Node-API call is invoked under.
2039* `[out] result`: A `napi_value` representing a JavaScript `Array`.
2040
2041Returns `napi_ok` if the API succeeded.
2042
2043This API returns a Node-API value corresponding to a JavaScript `Array` type.
2044JavaScript arrays are described in
2045[Section 22.1][] of the ECMAScript Language Specification.
2046
2047#### napi_create_array_with_length
2048<!-- YAML
2049added: v8.0.0
2050napiVersion: 1
2051-->
2052
2053```c
2054napi_status napi_create_array_with_length(napi_env env,
2055                                          size_t length,
2056                                          napi_value* result)
2057```
2058
2059* `[in] env`: The environment that the API is invoked under.
2060* `[in] length`: The initial length of the `Array`.
2061* `[out] result`: A `napi_value` representing a JavaScript `Array`.
2062
2063Returns `napi_ok` if the API succeeded.
2064
2065This API returns a Node-API value corresponding to a JavaScript `Array` type.
2066The `Array`'s length property is set to the passed-in length parameter.
2067However, the underlying buffer is not guaranteed to be pre-allocated by the VM
2068when the array is created. That behavior is left to the underlying VM
2069implementation. If the buffer must be a contiguous block of memory that can be
2070directly read and/or written via C, consider using
2071[`napi_create_external_arraybuffer`][].
2072
2073JavaScript arrays are described in
2074[Section 22.1][] of the ECMAScript Language Specification.
2075
2076#### napi_create_arraybuffer
2077<!-- YAML
2078added: v8.0.0
2079napiVersion: 1
2080-->
2081
2082```c
2083napi_status napi_create_arraybuffer(napi_env env,
2084                                    size_t byte_length,
2085                                    void** data,
2086                                    napi_value* result)
2087```
2088
2089* `[in] env`: The environment that the API is invoked under.
2090* `[in] length`: The length in bytes of the array buffer to create.
2091* `[out] data`: Pointer to the underlying byte buffer of the `ArrayBuffer`.
2092* `[out] result`: A `napi_value` representing a JavaScript `ArrayBuffer`.
2093
2094Returns `napi_ok` if the API succeeded.
2095
2096This API returns a Node-API value corresponding to a JavaScript `ArrayBuffer`.
2097`ArrayBuffer`s are used to represent fixed-length binary data buffers. They are
2098normally used as a backing-buffer for `TypedArray` objects.
2099The `ArrayBuffer` allocated will have an underlying byte buffer whose size is
2100determined by the `length` parameter that's passed in.
2101The underlying buffer is optionally returned back to the caller in case the
2102caller wants to directly manipulate the buffer. This buffer can only be
2103written to directly from native code. To write to this buffer from JavaScript,
2104a typed array or `DataView` object would need to be created.
2105
2106JavaScript `ArrayBuffer` objects are described in
2107[Section 24.1][] of the ECMAScript Language Specification.
2108
2109#### napi_create_buffer
2110<!-- YAML
2111added: v8.0.0
2112napiVersion: 1
2113-->
2114
2115```c
2116napi_status napi_create_buffer(napi_env env,
2117                               size_t size,
2118                               void** data,
2119                               napi_value* result)
2120```
2121
2122* `[in] env`: The environment that the API is invoked under.
2123* `[in] size`: Size in bytes of the underlying buffer.
2124* `[out] data`: Raw pointer to the underlying buffer.
2125* `[out] result`: A `napi_value` representing a `node::Buffer`.
2126
2127Returns `napi_ok` if the API succeeded.
2128
2129This API allocates a `node::Buffer` object. While this is still a
2130fully-supported data structure, in most cases using a `TypedArray` will suffice.
2131
2132#### napi_create_buffer_copy
2133<!-- YAML
2134added: v8.0.0
2135napiVersion: 1
2136-->
2137
2138```c
2139napi_status napi_create_buffer_copy(napi_env env,
2140                                    size_t length,
2141                                    const void* data,
2142                                    void** result_data,
2143                                    napi_value* result)
2144```
2145
2146* `[in] env`: The environment that the API is invoked under.
2147* `[in] size`: Size in bytes of the input buffer (should be the same as the size
2148  of the new buffer).
2149* `[in] data`: Raw pointer to the underlying buffer to copy from.
2150* `[out] result_data`: Pointer to the new `Buffer`'s underlying data buffer.
2151* `[out] result`: A `napi_value` representing a `node::Buffer`.
2152
2153Returns `napi_ok` if the API succeeded.
2154
2155This API allocates a `node::Buffer` object and initializes it with data copied
2156from the passed-in buffer. While this is still a fully-supported data
2157structure, in most cases using a `TypedArray` will suffice.
2158
2159#### napi_create_date
2160<!-- YAML
2161added:
2162 - v11.11.0
2163 - v10.17.0
2164napiVersion: 5
2165-->
2166
2167```c
2168napi_status napi_create_date(napi_env env,
2169                             double time,
2170                             napi_value* result);
2171```
2172
2173* `[in] env`: The environment that the API is invoked under.
2174* `[in] time`: ECMAScript time value in milliseconds since 01 January, 1970 UTC.
2175* `[out] result`: A `napi_value` representing a JavaScript `Date`.
2176
2177Returns `napi_ok` if the API succeeded.
2178
2179This API does not observe leap seconds; they are ignored, as
2180ECMAScript aligns with POSIX time specification.
2181
2182This API allocates a JavaScript `Date` object.
2183
2184JavaScript `Date` objects are described in
2185[Section 20.3][] of the ECMAScript Language Specification.
2186
2187#### napi_create_external
2188<!-- YAML
2189added: v8.0.0
2190napiVersion: 1
2191-->
2192
2193```c
2194napi_status napi_create_external(napi_env env,
2195                                 void* data,
2196                                 napi_finalize finalize_cb,
2197                                 void* finalize_hint,
2198                                 napi_value* result)
2199```
2200
2201* `[in] env`: The environment that the API is invoked under.
2202* `[in] data`: Raw pointer to the external data.
2203* `[in] finalize_cb`: Optional callback to call when the external value is being
2204  collected. [`napi_finalize`][] provides more details.
2205* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
2206  collection.
2207* `[out] result`: A `napi_value` representing an external value.
2208
2209Returns `napi_ok` if the API succeeded.
2210
2211This API allocates a JavaScript value with external data attached to it. This
2212is used to pass external data through JavaScript code, so it can be retrieved
2213later by native code using [`napi_get_value_external`][].
2214
2215The API adds a `napi_finalize` callback which will be called when the JavaScript
2216object just created is ready for garbage collection. It is similar to
2217`napi_wrap()` except that:
2218
2219* the native data cannot be retrieved later using `napi_unwrap()`,
2220* nor can it be removed later using `napi_remove_wrap()`, and
2221* the object created by the API can be used with `napi_wrap()`.
2222
2223The created value is not an object, and therefore does not support additional
2224properties. It is considered a distinct value type: calling `napi_typeof()` with
2225an external value yields `napi_external`.
2226
2227#### napi_create_external_arraybuffer
2228<!-- YAML
2229added: v8.0.0
2230napiVersion: 1
2231-->
2232
2233```c
2234napi_status
2235napi_create_external_arraybuffer(napi_env env,
2236                                 void* external_data,
2237                                 size_t byte_length,
2238                                 napi_finalize finalize_cb,
2239                                 void* finalize_hint,
2240                                 napi_value* result)
2241```
2242
2243* `[in] env`: The environment that the API is invoked under.
2244* `[in] external_data`: Pointer to the underlying byte buffer of the
2245  `ArrayBuffer`.
2246* `[in] byte_length`: The length in bytes of the underlying buffer.
2247* `[in] finalize_cb`: Optional callback to call when the `ArrayBuffer` is being
2248  collected. [`napi_finalize`][] provides more details.
2249* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
2250  collection.
2251* `[out] result`: A `napi_value` representing a JavaScript `ArrayBuffer`.
2252
2253Returns `napi_ok` if the API succeeded.
2254
2255This API returns a Node-API value corresponding to a JavaScript `ArrayBuffer`.
2256The underlying byte buffer of the `ArrayBuffer` is externally allocated and
2257managed. The caller must ensure that the byte buffer remains valid until the
2258finalize callback is called.
2259
2260The API adds a `napi_finalize` callback which will be called when the JavaScript
2261object just created is ready for garbage collection. It is similar to
2262`napi_wrap()` except that:
2263
2264* the native data cannot be retrieved later using `napi_unwrap()`,
2265* nor can it be removed later using `napi_remove_wrap()`, and
2266* the object created by the API can be used with `napi_wrap()`.
2267
2268JavaScript `ArrayBuffer`s are described in
2269[Section 24.1][] of the ECMAScript Language Specification.
2270
2271#### napi_create_external_buffer
2272<!-- YAML
2273added: v8.0.0
2274napiVersion: 1
2275-->
2276
2277```c
2278napi_status napi_create_external_buffer(napi_env env,
2279                                        size_t length,
2280                                        void* data,
2281                                        napi_finalize finalize_cb,
2282                                        void* finalize_hint,
2283                                        napi_value* result)
2284```
2285
2286* `[in] env`: The environment that the API is invoked under.
2287* `[in] length`: Size in bytes of the input buffer (should be the same as the
2288  size of the new buffer).
2289* `[in] data`: Raw pointer to the underlying buffer to expose to JavaScript.
2290* `[in] finalize_cb`: Optional callback to call when the `ArrayBuffer` is being
2291  collected. [`napi_finalize`][] provides more details.
2292* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
2293  collection.
2294* `[out] result`: A `napi_value` representing a `node::Buffer`.
2295
2296Returns `napi_ok` if the API succeeded.
2297
2298This API allocates a `node::Buffer` object and initializes it with data
2299backed by the passed in buffer. While this is still a fully-supported data
2300structure, in most cases using a `TypedArray` will suffice.
2301
2302The API adds a `napi_finalize` callback which will be called when the JavaScript
2303object just created is ready for garbage collection. It is similar to
2304`napi_wrap()` except that:
2305
2306* the native data cannot be retrieved later using `napi_unwrap()`,
2307* nor can it be removed later using `napi_remove_wrap()`, and
2308* the object created by the API can be used with `napi_wrap()`.
2309
2310For Node.js >=4 `Buffers` are `Uint8Array`s.
2311
2312#### napi_create_object
2313<!-- YAML
2314added: v8.0.0
2315napiVersion: 1
2316-->
2317
2318```c
2319napi_status napi_create_object(napi_env env, napi_value* result)
2320```
2321
2322* `[in] env`: The environment that the API is invoked under.
2323* `[out] result`: A `napi_value` representing a JavaScript `Object`.
2324
2325Returns `napi_ok` if the API succeeded.
2326
2327This API allocates a default JavaScript `Object`.
2328It is the equivalent of doing `new Object()` in JavaScript.
2329
2330The JavaScript `Object` type is described in [Section 6.1.7][] of the
2331ECMAScript Language Specification.
2332
2333#### napi_create_symbol
2334<!-- YAML
2335added: v8.0.0
2336napiVersion: 1
2337-->
2338
2339```c
2340napi_status napi_create_symbol(napi_env env,
2341                               napi_value description,
2342                               napi_value* result)
2343```
2344
2345* `[in] env`: The environment that the API is invoked under.
2346* `[in] description`: Optional `napi_value` which refers to a JavaScript
2347  `string` to be set as the description for the symbol.
2348* `[out] result`: A `napi_value` representing a JavaScript `symbol`.
2349
2350Returns `napi_ok` if the API succeeded.
2351
2352This API creates a JavaScript `symbol` value from a UTF8-encoded C string.
2353
2354The JavaScript `symbol` type is described in [Section 19.4][]
2355of the ECMAScript Language Specification.
2356
2357#### napi_create_typedarray
2358<!-- YAML
2359added: v8.0.0
2360napiVersion: 1
2361-->
2362
2363```c
2364napi_status napi_create_typedarray(napi_env env,
2365                                   napi_typedarray_type type,
2366                                   size_t length,
2367                                   napi_value arraybuffer,
2368                                   size_t byte_offset,
2369                                   napi_value* result)
2370```
2371
2372* `[in] env`: The environment that the API is invoked under.
2373* `[in] type`: Scalar datatype of the elements within the `TypedArray`.
2374* `[in] length`: Number of elements in the `TypedArray`.
2375* `[in] arraybuffer`: `ArrayBuffer` underlying the typed array.
2376* `[in] byte_offset`: The byte offset within the `ArrayBuffer` from which to
2377  start projecting the `TypedArray`.
2378* `[out] result`: A `napi_value` representing a JavaScript `TypedArray`.
2379
2380Returns `napi_ok` if the API succeeded.
2381
2382This API creates a JavaScript `TypedArray` object over an existing
2383`ArrayBuffer`. `TypedArray` objects provide an array-like view over an
2384underlying data buffer where each element has the same underlying binary scalar
2385datatype.
2386
2387It's required that `(length * size_of_element) + byte_offset` should
2388be <= the size in bytes of the array passed in. If not, a `RangeError` exception
2389is raised.
2390
2391JavaScript `TypedArray` objects are described in
2392[Section 22.2][] of the ECMAScript Language Specification.
2393
2394#### napi_create_dataview
2395<!-- YAML
2396added: v8.3.0
2397napiVersion: 1
2398-->
2399
2400```c
2401napi_status napi_create_dataview(napi_env env,
2402                                 size_t byte_length,
2403                                 napi_value arraybuffer,
2404                                 size_t byte_offset,
2405                                 napi_value* result)
2406```
2407
2408* `[in] env`: The environment that the API is invoked under.
2409* `[in] length`: Number of elements in the `DataView`.
2410* `[in] arraybuffer`: `ArrayBuffer` underlying the `DataView`.
2411* `[in] byte_offset`: The byte offset within the `ArrayBuffer` from which to
2412  start projecting the `DataView`.
2413* `[out] result`: A `napi_value` representing a JavaScript `DataView`.
2414
2415Returns `napi_ok` if the API succeeded.
2416
2417This API creates a JavaScript `DataView` object over an existing `ArrayBuffer`.
2418`DataView` objects provide an array-like view over an underlying data buffer,
2419but one which allows items of different size and type in the `ArrayBuffer`.
2420
2421It is required that `byte_length + byte_offset` is less than or equal to the
2422size in bytes of the array passed in. If not, a `RangeError` exception is
2423raised.
2424
2425JavaScript `DataView` objects are described in
2426[Section 24.3][] of the ECMAScript Language Specification.
2427
2428### Functions to convert from C types to Node-API
2429#### napi_create_int32
2430<!-- YAML
2431added: v8.4.0
2432napiVersion: 1
2433-->
2434
2435```c
2436napi_status napi_create_int32(napi_env env, int32_t value, napi_value* result)
2437```
2438
2439* `[in] env`: The environment that the API is invoked under.
2440* `[in] value`: Integer value to be represented in JavaScript.
2441* `[out] result`: A `napi_value` representing a JavaScript `number`.
2442
2443Returns `napi_ok` if the API succeeded.
2444
2445This API is used to convert from the C `int32_t` type to the JavaScript
2446`number` type.
2447
2448The JavaScript `number` type is described in
2449[Section 6.1.6][] of the ECMAScript Language Specification.
2450
2451#### napi_create_uint32
2452<!-- YAML
2453added: v8.4.0
2454napiVersion: 1
2455-->
2456
2457```c
2458napi_status napi_create_uint32(napi_env env, uint32_t value, napi_value* result)
2459```
2460
2461* `[in] env`: The environment that the API is invoked under.
2462* `[in] value`: Unsigned integer value to be represented in JavaScript.
2463* `[out] result`: A `napi_value` representing a JavaScript `number`.
2464
2465Returns `napi_ok` if the API succeeded.
2466
2467This API is used to convert from the C `uint32_t` type to the JavaScript
2468`number` type.
2469
2470The JavaScript `number` type is described in
2471[Section 6.1.6][] of the ECMAScript Language Specification.
2472
2473#### napi_create_int64
2474<!-- YAML
2475added: v8.4.0
2476napiVersion: 1
2477-->
2478
2479```c
2480napi_status napi_create_int64(napi_env env, int64_t value, napi_value* result)
2481```
2482
2483* `[in] env`: The environment that the API is invoked under.
2484* `[in] value`: Integer value to be represented in JavaScript.
2485* `[out] result`: A `napi_value` representing a JavaScript `number`.
2486
2487Returns `napi_ok` if the API succeeded.
2488
2489This API is used to convert from the C `int64_t` type to the JavaScript
2490`number` type.
2491
2492The JavaScript `number` type is described in [Section 6.1.6][]
2493of the ECMAScript Language Specification. Note the complete range of `int64_t`
2494cannot be represented with full precision in JavaScript. Integer values
2495outside the range of [`Number.MIN_SAFE_INTEGER`][] `-(2**53 - 1)` -
2496[`Number.MAX_SAFE_INTEGER`][] `(2**53 - 1)` will lose precision.
2497
2498#### napi_create_double
2499<!-- YAML
2500added: v8.4.0
2501napiVersion: 1
2502-->
2503
2504```c
2505napi_status napi_create_double(napi_env env, double value, napi_value* result)
2506```
2507
2508* `[in] env`: The environment that the API is invoked under.
2509* `[in] value`: Double-precision value to be represented in JavaScript.
2510* `[out] result`: A `napi_value` representing a JavaScript `number`.
2511
2512Returns `napi_ok` if the API succeeded.
2513
2514This API is used to convert from the C `double` type to the JavaScript
2515`number` type.
2516
2517The JavaScript `number` type is described in
2518[Section 6.1.6][] of the ECMAScript Language Specification.
2519
2520#### napi_create_bigint_int64
2521<!-- YAML
2522added: v10.7.0
2523napiVersion: 6
2524-->
2525
2526```c
2527napi_status napi_create_bigint_int64(napi_env env,
2528                                     int64_t value,
2529                                     napi_value* result);
2530```
2531
2532* `[in] env`: The environment that the API is invoked under.
2533* `[in] value`: Integer value to be represented in JavaScript.
2534* `[out] result`: A `napi_value` representing a JavaScript `BigInt`.
2535
2536Returns `napi_ok` if the API succeeded.
2537
2538This API converts the C `int64_t` type to the JavaScript `BigInt` type.
2539
2540#### napi_create_bigint_uint64
2541<!-- YAML
2542added: v10.7.0
2543napiVersion: 6
2544-->
2545
2546```c
2547napi_status napi_create_bigint_uint64(napi_env env,
2548                                      uint64_t value,
2549                                      napi_value* result);
2550```
2551
2552* `[in] env`: The environment that the API is invoked under.
2553* `[in] value`: Unsigned integer value to be represented in JavaScript.
2554* `[out] result`: A `napi_value` representing a JavaScript `BigInt`.
2555
2556Returns `napi_ok` if the API succeeded.
2557
2558This API converts the C `uint64_t` type to the JavaScript `BigInt` type.
2559
2560#### napi_create_bigint_words
2561<!-- YAML
2562added: v10.7.0
2563napiVersion: 6
2564-->
2565
2566```c
2567napi_status napi_create_bigint_words(napi_env env,
2568                                     int sign_bit,
2569                                     size_t word_count,
2570                                     const uint64_t* words,
2571                                     napi_value* result);
2572```
2573
2574* `[in] env`: The environment that the API is invoked under.
2575* `[in] sign_bit`: Determines if the resulting `BigInt` will be positive or
2576  negative.
2577* `[in] word_count`: The length of the `words` array.
2578* `[in] words`: An array of `uint64_t` little-endian 64-bit words.
2579* `[out] result`: A `napi_value` representing a JavaScript `BigInt`.
2580
2581Returns `napi_ok` if the API succeeded.
2582
2583This API converts an array of unsigned 64-bit words into a single `BigInt`
2584value.
2585
2586The resulting `BigInt` is calculated as: (–1)<sup>`sign_bit`</sup> (`words[0]`
2587× (2<sup>64</sup>)<sup>0</sup> + `words[1]` × (2<sup>64</sup>)<sup>1</sup> + …)
2588
2589#### napi_create_string_latin1
2590<!-- YAML
2591added: v8.0.0
2592napiVersion: 1
2593-->
2594
2595```c
2596napi_status napi_create_string_latin1(napi_env env,
2597                                      const char* str,
2598                                      size_t length,
2599                                      napi_value* result);
2600```
2601
2602* `[in] env`: The environment that the API is invoked under.
2603* `[in] str`: Character buffer representing an ISO-8859-1-encoded string.
2604* `[in] length`: The length of the string in bytes, or `NAPI_AUTO_LENGTH` if it
2605  is null-terminated.
2606* `[out] result`: A `napi_value` representing a JavaScript `string`.
2607
2608Returns `napi_ok` if the API succeeded.
2609
2610This API creates a JavaScript `string` value from an ISO-8859-1-encoded C
2611string. The native string is copied.
2612
2613The JavaScript `string` type is described in
2614[Section 6.1.4][] of the ECMAScript Language Specification.
2615
2616#### napi_create_string_utf16
2617<!-- YAML
2618added: v8.0.0
2619napiVersion: 1
2620-->
2621
2622```c
2623napi_status napi_create_string_utf16(napi_env env,
2624                                     const char16_t* str,
2625                                     size_t length,
2626                                     napi_value* result)
2627```
2628
2629* `[in] env`: The environment that the API is invoked under.
2630* `[in] str`: Character buffer representing a UTF16-LE-encoded string.
2631* `[in] length`: The length of the string in two-byte code units, or
2632  `NAPI_AUTO_LENGTH` if it is null-terminated.
2633* `[out] result`: A `napi_value` representing a JavaScript `string`.
2634
2635Returns `napi_ok` if the API succeeded.
2636
2637This API creates a JavaScript `string` value from a UTF16-LE-encoded C string.
2638The native string is copied.
2639
2640The JavaScript `string` type is described in
2641[Section 6.1.4][] of the ECMAScript Language Specification.
2642
2643#### napi_create_string_utf8
2644<!-- YAML
2645added: v8.0.0
2646napiVersion: 1
2647-->
2648
2649```c
2650napi_status napi_create_string_utf8(napi_env env,
2651                                    const char* str,
2652                                    size_t length,
2653                                    napi_value* result)
2654```
2655
2656* `[in] env`: The environment that the API is invoked under.
2657* `[in] str`: Character buffer representing a UTF8-encoded string.
2658* `[in] length`: The length of the string in bytes, or `NAPI_AUTO_LENGTH` if it
2659  is null-terminated.
2660* `[out] result`: A `napi_value` representing a JavaScript `string`.
2661
2662Returns `napi_ok` if the API succeeded.
2663
2664This API creates a JavaScript `string` value from a UTF8-encoded C string.
2665The native string is copied.
2666
2667The JavaScript `string` type is described in
2668[Section 6.1.4][] of the ECMAScript Language Specification.
2669
2670### Functions to convert from Node-API to C types
2671#### napi_get_array_length
2672<!-- YAML
2673added: v8.0.0
2674napiVersion: 1
2675-->
2676
2677```c
2678napi_status napi_get_array_length(napi_env env,
2679                                  napi_value value,
2680                                  uint32_t* result)
2681```
2682
2683* `[in] env`: The environment that the API is invoked under.
2684* `[in] value`: `napi_value` representing the JavaScript `Array` whose length is
2685  being queried.
2686* `[out] result`: `uint32` representing length of the array.
2687
2688Returns `napi_ok` if the API succeeded.
2689
2690This API returns the length of an array.
2691
2692`Array` length is described in [Section 22.1.4.1][] of the ECMAScript Language
2693Specification.
2694
2695#### napi_get_arraybuffer_info
2696<!-- YAML
2697added: v8.0.0
2698napiVersion: 1
2699-->
2700
2701```c
2702napi_status napi_get_arraybuffer_info(napi_env env,
2703                                      napi_value arraybuffer,
2704                                      void** data,
2705                                      size_t* byte_length)
2706```
2707
2708* `[in] env`: The environment that the API is invoked under.
2709* `[in] arraybuffer`: `napi_value` representing the `ArrayBuffer` being queried.
2710* `[out] data`: The underlying data buffer of the `ArrayBuffer`. If byte_length
2711  is `0`, this may be `NULL` or any other pointer value.
2712* `[out] byte_length`: Length in bytes of the underlying data buffer.
2713
2714Returns `napi_ok` if the API succeeded.
2715
2716This API is used to retrieve the underlying data buffer of an `ArrayBuffer` and
2717its length.
2718
2719*WARNING*: Use caution while using this API. The lifetime of the underlying data
2720buffer is managed by the `ArrayBuffer` even after it's returned. A
2721possible safe way to use this API is in conjunction with
2722[`napi_create_reference`][], which can be used to guarantee control over the
2723lifetime of the `ArrayBuffer`. It's also safe to use the returned data buffer
2724within the same callback as long as there are no calls to other APIs that might
2725trigger a GC.
2726
2727#### napi_get_buffer_info
2728<!-- YAML
2729added: v8.0.0
2730napiVersion: 1
2731-->
2732
2733```c
2734napi_status napi_get_buffer_info(napi_env env,
2735                                 napi_value value,
2736                                 void** data,
2737                                 size_t* length)
2738```
2739
2740* `[in] env`: The environment that the API is invoked under.
2741* `[in] value`: `napi_value` representing the `node::Buffer` being queried.
2742* `[out] data`: The underlying data buffer of the `node::Buffer`.
2743  If length is `0`, this may be `NULL` or any other pointer value.
2744* `[out] length`: Length in bytes of the underlying data buffer.
2745
2746Returns `napi_ok` if the API succeeded.
2747
2748This API is used to retrieve the underlying data buffer of a `node::Buffer`
2749and it's length.
2750
2751*Warning*: Use caution while using this API since the underlying data buffer's
2752lifetime is not guaranteed if it's managed by the VM.
2753
2754#### napi_get_prototype
2755<!-- YAML
2756added: v8.0.0
2757napiVersion: 1
2758-->
2759
2760```c
2761napi_status napi_get_prototype(napi_env env,
2762                               napi_value object,
2763                               napi_value* result)
2764```
2765
2766* `[in] env`: The environment that the API is invoked under.
2767* `[in] object`: `napi_value` representing JavaScript `Object` whose prototype
2768  to return. This returns the equivalent of `Object.getPrototypeOf` (which is
2769  not the same as the function's `prototype` property).
2770* `[out] result`: `napi_value` representing prototype of the given object.
2771
2772Returns `napi_ok` if the API succeeded.
2773
2774#### napi_get_typedarray_info
2775<!-- YAML
2776added: v8.0.0
2777napiVersion: 1
2778-->
2779
2780```c
2781napi_status napi_get_typedarray_info(napi_env env,
2782                                     napi_value typedarray,
2783                                     napi_typedarray_type* type,
2784                                     size_t* length,
2785                                     void** data,
2786                                     napi_value* arraybuffer,
2787                                     size_t* byte_offset)
2788```
2789
2790* `[in] env`: The environment that the API is invoked under.
2791* `[in] typedarray`: `napi_value` representing the `TypedArray` whose
2792  properties to query.
2793* `[out] type`: Scalar datatype of the elements within the `TypedArray`.
2794* `[out] length`: The number of elements in the `TypedArray`.
2795* `[out] data`: The data buffer underlying the `TypedArray` adjusted by
2796  the `byte_offset` value so that it points to the first element in the
2797  `TypedArray`. If the length of the array is `0`, this may be `NULL` or
2798  any other pointer value.
2799* `[out] arraybuffer`: The `ArrayBuffer` underlying the `TypedArray`.
2800* `[out] byte_offset`: The byte offset within the underlying native array
2801  at which the first element of the arrays is located. The value for the data
2802  parameter has already been adjusted so that data points to the first element
2803  in the array. Therefore, the first byte of the native array would be at
2804  `data - byte_offset`.
2805
2806Returns `napi_ok` if the API succeeded.
2807
2808This API returns various properties of a typed array.
2809
2810*Warning*: Use caution while using this API since the underlying data buffer
2811is managed by the VM.
2812
2813#### napi_get_dataview_info
2814<!-- YAML
2815added: v8.3.0
2816napiVersion: 1
2817-->
2818
2819```c
2820napi_status napi_get_dataview_info(napi_env env,
2821                                   napi_value dataview,
2822                                   size_t* byte_length,
2823                                   void** data,
2824                                   napi_value* arraybuffer,
2825                                   size_t* byte_offset)
2826```
2827
2828* `[in] env`: The environment that the API is invoked under.
2829* `[in] dataview`: `napi_value` representing the `DataView` whose
2830  properties to query.
2831* `[out] byte_length`: Number of bytes in the `DataView`.
2832* `[out] data`: The data buffer underlying the `DataView`.
2833  If byte_length is `0`, this may be `NULL` or any other pointer value.
2834* `[out] arraybuffer`: `ArrayBuffer` underlying the `DataView`.
2835* `[out] byte_offset`: The byte offset within the data buffer from which
2836  to start projecting the `DataView`.
2837
2838Returns `napi_ok` if the API succeeded.
2839
2840This API returns various properties of a `DataView`.
2841
2842#### napi_get_date_value
2843<!-- YAML
2844added:
2845 - v11.11.0
2846 - v10.17.0
2847napiVersion: 5
2848-->
2849
2850```c
2851napi_status napi_get_date_value(napi_env env,
2852                                napi_value value,
2853                                double* result)
2854```
2855
2856* `[in] env`: The environment that the API is invoked under.
2857* `[in] value`: `napi_value` representing a JavaScript `Date`.
2858* `[out] result`: Time value as a `double` represented as milliseconds since
2859  midnight at the beginning of 01 January, 1970 UTC.
2860
2861This API does not observe leap seconds; they are ignored, as
2862ECMAScript aligns with POSIX time specification.
2863
2864Returns `napi_ok` if the API succeeded. If a non-date `napi_value` is passed
2865in it returns `napi_date_expected`.
2866
2867This API returns the C double primitive of time value for the given JavaScript
2868`Date`.
2869
2870#### napi_get_value_bool
2871<!-- YAML
2872added: v8.0.0
2873napiVersion: 1
2874-->
2875
2876```c
2877napi_status napi_get_value_bool(napi_env env, napi_value value, bool* result)
2878```
2879
2880* `[in] env`: The environment that the API is invoked under.
2881* `[in] value`: `napi_value` representing JavaScript `Boolean`.
2882* `[out] result`: C boolean primitive equivalent of the given JavaScript
2883  `Boolean`.
2884
2885Returns `napi_ok` if the API succeeded. If a non-boolean `napi_value` is
2886passed in it returns `napi_boolean_expected`.
2887
2888This API returns the C boolean primitive equivalent of the given JavaScript
2889`Boolean`.
2890
2891#### napi_get_value_double
2892<!-- YAML
2893added: v8.0.0
2894napiVersion: 1
2895-->
2896
2897```c
2898napi_status napi_get_value_double(napi_env env,
2899                                  napi_value value,
2900                                  double* result)
2901```
2902
2903* `[in] env`: The environment that the API is invoked under.
2904* `[in] value`: `napi_value` representing JavaScript `number`.
2905* `[out] result`: C double primitive equivalent of the given JavaScript
2906  `number`.
2907
2908Returns `napi_ok` if the API succeeded. If a non-number `napi_value` is passed
2909in it returns `napi_number_expected`.
2910
2911This API returns the C double primitive equivalent of the given JavaScript
2912`number`.
2913
2914#### napi_get_value_bigint_int64
2915<!-- YAML
2916added: v10.7.0
2917napiVersion: 6
2918-->
2919
2920```c
2921napi_status napi_get_value_bigint_int64(napi_env env,
2922                                        napi_value value,
2923                                        int64_t* result,
2924                                        bool* lossless);
2925```
2926
2927* `[in] env`: The environment that the API is invoked under
2928* `[in] value`: `napi_value` representing JavaScript `BigInt`.
2929* `[out] result`: C `int64_t` primitive equivalent of the given JavaScript
2930  `BigInt`.
2931* `[out] lossless`: Indicates whether the `BigInt` value was converted
2932  losslessly.
2933
2934Returns `napi_ok` if the API succeeded. If a non-`BigInt` is passed in it
2935returns `napi_bigint_expected`.
2936
2937This API returns the C `int64_t` primitive equivalent of the given JavaScript
2938`BigInt`. If needed it will truncate the value, setting `lossless` to `false`.
2939
2940#### napi_get_value_bigint_uint64
2941<!-- YAML
2942added: v10.7.0
2943napiVersion: 6
2944-->
2945
2946```c
2947napi_status napi_get_value_bigint_uint64(napi_env env,
2948                                        napi_value value,
2949                                        uint64_t* result,
2950                                        bool* lossless);
2951```
2952
2953* `[in] env`: The environment that the API is invoked under.
2954* `[in] value`: `napi_value` representing JavaScript `BigInt`.
2955* `[out] result`: C `uint64_t` primitive equivalent of the given JavaScript
2956  `BigInt`.
2957* `[out] lossless`: Indicates whether the `BigInt` value was converted
2958  losslessly.
2959
2960Returns `napi_ok` if the API succeeded. If a non-`BigInt` is passed in it
2961returns `napi_bigint_expected`.
2962
2963This API returns the C `uint64_t` primitive equivalent of the given JavaScript
2964`BigInt`. If needed it will truncate the value, setting `lossless` to `false`.
2965
2966#### napi_get_value_bigint_words
2967<!-- YAML
2968added: v10.7.0
2969napiVersion: 6
2970-->
2971
2972```c
2973napi_status napi_get_value_bigint_words(napi_env env,
2974                                        napi_value value,
2975                                        int* sign_bit,
2976                                        size_t* word_count,
2977                                        uint64_t* words);
2978```
2979
2980* `[in] env`: The environment that the API is invoked under.
2981* `[in] value`: `napi_value` representing JavaScript `BigInt`.
2982* `[out] sign_bit`: Integer representing if the JavaScript `BigInt` is positive
2983   or negative.
2984* `[in/out] word_count`: Must be initialized to the length of the `words`
2985   array. Upon return, it will be set to the actual number of words that
2986   would be needed to store this `BigInt`.
2987* `[out] words`: Pointer to a pre-allocated 64-bit word array.
2988
2989Returns `napi_ok` if the API succeeded.
2990
2991This API converts a single `BigInt` value into a sign bit, 64-bit little-endian
2992array, and the number of elements in the array. `sign_bit` and `words` may be
2993both set to `NULL`, in order to get only `word_count`.
2994
2995#### napi_get_value_external
2996<!-- YAML
2997added: v8.0.0
2998napiVersion: 1
2999-->
3000
3001```c
3002napi_status napi_get_value_external(napi_env env,
3003                                    napi_value value,
3004                                    void** result)
3005```
3006
3007* `[in] env`: The environment that the API is invoked under.
3008* `[in] value`: `napi_value` representing JavaScript external value.
3009* `[out] result`: Pointer to the data wrapped by the JavaScript external value.
3010
3011Returns `napi_ok` if the API succeeded. If a non-external `napi_value` is
3012passed in it returns `napi_invalid_arg`.
3013
3014This API retrieves the external data pointer that was previously passed to
3015`napi_create_external()`.
3016
3017#### napi_get_value_int32
3018<!-- YAML
3019added: v8.0.0
3020napiVersion: 1
3021-->
3022
3023```c
3024napi_status napi_get_value_int32(napi_env env,
3025                                 napi_value value,
3026                                 int32_t* result)
3027```
3028
3029* `[in] env`: The environment that the API is invoked under.
3030* `[in] value`: `napi_value` representing JavaScript `number`.
3031* `[out] result`: C `int32` primitive equivalent of the given JavaScript
3032  `number`.
3033
3034Returns `napi_ok` if the API succeeded. If a non-number `napi_value`
3035is passed in `napi_number_expected`.
3036
3037This API returns the C `int32` primitive equivalent
3038of the given JavaScript `number`.
3039
3040If the number exceeds the range of the 32 bit integer, then the result is
3041truncated to the equivalent of the bottom 32 bits. This can result in a large
3042positive number becoming a negative number if the value is > 2<sup>31</sup> - 1.
3043
3044Non-finite number values (`NaN`, `+Infinity`, or `-Infinity`) set the
3045result to zero.
3046
3047#### napi_get_value_int64
3048<!-- YAML
3049added: v8.0.0
3050napiVersion: 1
3051-->
3052
3053```c
3054napi_status napi_get_value_int64(napi_env env,
3055                                 napi_value value,
3056                                 int64_t* result)
3057```
3058
3059* `[in] env`: The environment that the API is invoked under.
3060* `[in] value`: `napi_value` representing JavaScript `number`.
3061* `[out] result`: C `int64` primitive equivalent of the given JavaScript
3062  `number`.
3063
3064Returns `napi_ok` if the API succeeded. If a non-number `napi_value`
3065is passed in it returns `napi_number_expected`.
3066
3067This API returns the C `int64` primitive equivalent of the given JavaScript
3068`number`.
3069
3070`number` values outside the range of [`Number.MIN_SAFE_INTEGER`][]
3071`-(2**53 - 1)` - [`Number.MAX_SAFE_INTEGER`][] `(2**53 - 1)` will lose
3072precision.
3073
3074Non-finite number values (`NaN`, `+Infinity`, or `-Infinity`) set the
3075result to zero.
3076
3077#### napi_get_value_string_latin1
3078<!-- YAML
3079added: v8.0.0
3080napiVersion: 1
3081-->
3082
3083```c
3084napi_status napi_get_value_string_latin1(napi_env env,
3085                                         napi_value value,
3086                                         char* buf,
3087                                         size_t bufsize,
3088                                         size_t* result)
3089```
3090
3091* `[in] env`: The environment that the API is invoked under.
3092* `[in] value`: `napi_value` representing JavaScript string.
3093* `[in] buf`: Buffer to write the ISO-8859-1-encoded string into. If `NULL` is
3094  passed in, the length of the string in bytes and excluding the null terminator
3095  is returned in `result`.
3096* `[in] bufsize`: Size of the destination buffer. When this value is
3097  insufficient, the returned string is truncated and null-terminated.
3098* `[out] result`: Number of bytes copied into the buffer, excluding the null
3099  terminator.
3100
3101Returns `napi_ok` if the API succeeded. If a non-`string` `napi_value`
3102is passed in it returns `napi_string_expected`.
3103
3104This API returns the ISO-8859-1-encoded string corresponding the value passed
3105in.
3106
3107#### napi_get_value_string_utf8
3108<!-- YAML
3109added: v8.0.0
3110napiVersion: 1
3111-->
3112
3113```c
3114napi_status napi_get_value_string_utf8(napi_env env,
3115                                       napi_value value,
3116                                       char* buf,
3117                                       size_t bufsize,
3118                                       size_t* result)
3119```
3120
3121* `[in] env`: The environment that the API is invoked under.
3122* `[in] value`: `napi_value` representing JavaScript string.
3123* `[in] buf`: Buffer to write the UTF8-encoded string into. If `NULL` is passed
3124  in, the length of the string in bytes and excluding the null terminator is
3125  returned in `result`.
3126* `[in] bufsize`: Size of the destination buffer. When this value is
3127  insufficient, the returned string is truncated and null-terminated.
3128* `[out] result`: Number of bytes copied into the buffer, excluding the null
3129  terminator.
3130
3131Returns `napi_ok` if the API succeeded. If a non-`string` `napi_value`
3132is passed in it returns `napi_string_expected`.
3133
3134This API returns the UTF8-encoded string corresponding the value passed in.
3135
3136#### napi_get_value_string_utf16
3137<!-- YAML
3138added: v8.0.0
3139napiVersion: 1
3140-->
3141
3142```c
3143napi_status napi_get_value_string_utf16(napi_env env,
3144                                        napi_value value,
3145                                        char16_t* buf,
3146                                        size_t bufsize,
3147                                        size_t* result)
3148```
3149
3150* `[in] env`: The environment that the API is invoked under.
3151* `[in] value`: `napi_value` representing JavaScript string.
3152* `[in] buf`: Buffer to write the UTF16-LE-encoded string into. If `NULL` is
3153  passed in, the length of the string in 2-byte code units and excluding the
3154  null terminator is returned.
3155* `[in] bufsize`: Size of the destination buffer. When this value is
3156  insufficient, the returned string is truncated and null-terminated.
3157* `[out] result`: Number of 2-byte code units copied into the buffer, excluding
3158  the null terminator.
3159
3160Returns `napi_ok` if the API succeeded. If a non-`string` `napi_value`
3161is passed in it returns `napi_string_expected`.
3162
3163This API returns the UTF16-encoded string corresponding the value passed in.
3164
3165#### napi_get_value_uint32
3166<!-- YAML
3167added: v8.0.0
3168napiVersion: 1
3169-->
3170
3171```c
3172napi_status napi_get_value_uint32(napi_env env,
3173                                  napi_value value,
3174                                  uint32_t* result)
3175```
3176
3177* `[in] env`: The environment that the API is invoked under.
3178* `[in] value`: `napi_value` representing JavaScript `number`.
3179* `[out] result`: C primitive equivalent of the given `napi_value` as a
3180  `uint32_t`.
3181
3182Returns `napi_ok` if the API succeeded. If a non-number `napi_value`
3183is passed in it returns `napi_number_expected`.
3184
3185This API returns the C primitive equivalent of the given `napi_value` as a
3186`uint32_t`.
3187
3188### Functions to get global instances
3189#### napi_get_boolean
3190<!-- YAML
3191added: v8.0.0
3192napiVersion: 1
3193-->
3194
3195```c
3196napi_status napi_get_boolean(napi_env env, bool value, napi_value* result)
3197```
3198
3199* `[in] env`: The environment that the API is invoked under.
3200* `[in] value`: The value of the boolean to retrieve.
3201* `[out] result`: `napi_value` representing JavaScript `Boolean` singleton to
3202  retrieve.
3203
3204Returns `napi_ok` if the API succeeded.
3205
3206This API is used to return the JavaScript singleton object that is used to
3207represent the given boolean value.
3208
3209#### napi_get_global
3210<!-- YAML
3211added: v8.0.0
3212napiVersion: 1
3213-->
3214
3215```c
3216napi_status napi_get_global(napi_env env, napi_value* result)
3217```
3218
3219* `[in] env`: The environment that the API is invoked under.
3220* `[out] result`: `napi_value` representing JavaScript `global` object.
3221
3222Returns `napi_ok` if the API succeeded.
3223
3224This API returns the `global` object.
3225
3226#### napi_get_null
3227<!-- YAML
3228added: v8.0.0
3229napiVersion: 1
3230-->
3231
3232```c
3233napi_status napi_get_null(napi_env env, napi_value* result)
3234```
3235
3236* `[in] env`: The environment that the API is invoked under.
3237* `[out] result`: `napi_value` representing JavaScript `null` object.
3238
3239Returns `napi_ok` if the API succeeded.
3240
3241This API returns the `null` object.
3242
3243#### napi_get_undefined
3244<!-- YAML
3245added: v8.0.0
3246napiVersion: 1
3247-->
3248
3249```c
3250napi_status napi_get_undefined(napi_env env, napi_value* result)
3251```
3252
3253* `[in] env`: The environment that the API is invoked under.
3254* `[out] result`: `napi_value` representing JavaScript Undefined value.
3255
3256Returns `napi_ok` if the API succeeded.
3257
3258This API returns the Undefined object.
3259
3260## Working with JavaScript values and abstract operations
3261
3262Node-API exposes a set of APIs to perform some abstract operations on JavaScript
3263values. Some of these operations are documented under [Section 7][]
3264of the [ECMAScript Language Specification][].
3265
3266These APIs support doing one of the following:
3267
32681. Coerce JavaScript values to specific JavaScript types (such as `number` or
3269   `string`).
32702. Check the type of a JavaScript value.
32713. Check for equality between two JavaScript values.
3272
3273### napi_coerce_to_bool
3274<!-- YAML
3275added: v8.0.0
3276napiVersion: 1
3277-->
3278
3279```c
3280napi_status napi_coerce_to_bool(napi_env env,
3281                                napi_value value,
3282                                napi_value* result)
3283```
3284
3285* `[in] env`: The environment that the API is invoked under.
3286* `[in] value`: The JavaScript value to coerce.
3287* `[out] result`: `napi_value` representing the coerced JavaScript `Boolean`.
3288
3289Returns `napi_ok` if the API succeeded.
3290
3291This API implements the abstract operation `ToBoolean()` as defined in
3292[Section 7.1.2][] of the ECMAScript Language Specification.
3293This API can be re-entrant if getters are defined on the passed-in `Object`.
3294
3295### napi_coerce_to_number
3296<!-- YAML
3297added: v8.0.0
3298napiVersion: 1
3299-->
3300
3301```c
3302napi_status napi_coerce_to_number(napi_env env,
3303                                  napi_value value,
3304                                  napi_value* result)
3305```
3306
3307* `[in] env`: The environment that the API is invoked under.
3308* `[in] value`: The JavaScript value to coerce.
3309* `[out] result`: `napi_value` representing the coerced JavaScript `number`.
3310
3311Returns `napi_ok` if the API succeeded.
3312
3313This API implements the abstract operation `ToNumber()` as defined in
3314[Section 7.1.3][] of the ECMAScript Language Specification.
3315This API can be re-entrant if getters are defined on the passed-in `Object`.
3316
3317### napi_coerce_to_object
3318<!-- YAML
3319added: v8.0.0
3320napiVersion: 1
3321-->
3322
3323```c
3324napi_status napi_coerce_to_object(napi_env env,
3325                                  napi_value value,
3326                                  napi_value* result)
3327```
3328
3329* `[in] env`: The environment that the API is invoked under.
3330* `[in] value`: The JavaScript value to coerce.
3331* `[out] result`: `napi_value` representing the coerced JavaScript `Object`.
3332
3333Returns `napi_ok` if the API succeeded.
3334
3335This API implements the abstract operation `ToObject()` as defined in
3336[Section 7.1.13][] of the ECMAScript Language Specification.
3337This API can be re-entrant if getters are defined on the passed-in `Object`.
3338
3339### napi_coerce_to_string
3340<!-- YAML
3341added: v8.0.0
3342napiVersion: 1
3343-->
3344
3345```c
3346napi_status napi_coerce_to_string(napi_env env,
3347                                  napi_value value,
3348                                  napi_value* result)
3349```
3350
3351* `[in] env`: The environment that the API is invoked under.
3352* `[in] value`: The JavaScript value to coerce.
3353* `[out] result`: `napi_value` representing the coerced JavaScript `string`.
3354
3355Returns `napi_ok` if the API succeeded.
3356
3357This API implements the abstract operation `ToString()` as defined in
3358[Section 7.1.13][] of the ECMAScript Language Specification.
3359This API can be re-entrant if getters are defined on the passed-in `Object`.
3360
3361### napi_typeof
3362<!-- YAML
3363added: v8.0.0
3364napiVersion: 1
3365-->
3366
3367```c
3368napi_status napi_typeof(napi_env env, napi_value value, napi_valuetype* result)
3369```
3370
3371* `[in] env`: The environment that the API is invoked under.
3372* `[in] value`: The JavaScript value whose type to query.
3373* `[out] result`: The type of the JavaScript value.
3374
3375Returns `napi_ok` if the API succeeded.
3376
3377* `napi_invalid_arg` if the type of `value` is not a known ECMAScript type and
3378  `value` is not an External value.
3379
3380This API represents behavior similar to invoking the `typeof` Operator on
3381the object as defined in [Section 12.5.5][] of the ECMAScript Language
3382Specification. However, there are some differences:
3383
33841. It has support for detecting an External value.
33852. It detects `null` as a separate type, while ECMAScript `typeof` would detect
3386   `object`.
3387
3388If `value` has a type that is invalid, an error is returned.
3389
3390### napi_instanceof
3391<!-- YAML
3392added: v8.0.0
3393napiVersion: 1
3394-->
3395
3396```c
3397napi_status napi_instanceof(napi_env env,
3398                            napi_value object,
3399                            napi_value constructor,
3400                            bool* result)
3401```
3402
3403* `[in] env`: The environment that the API is invoked under.
3404* `[in] object`: The JavaScript value to check.
3405* `[in] constructor`: The JavaScript function object of the constructor function
3406  to check against.
3407* `[out] result`: Boolean that is set to true if `object instanceof constructor`
3408  is true.
3409
3410Returns `napi_ok` if the API succeeded.
3411
3412This API represents invoking the `instanceof` Operator on the object as
3413defined in [Section 12.10.4][] of the ECMAScript Language Specification.
3414
3415### napi_is_array
3416<!-- YAML
3417added: v8.0.0
3418napiVersion: 1
3419-->
3420
3421```c
3422napi_status napi_is_array(napi_env env, napi_value value, bool* result)
3423```
3424
3425* `[in] env`: The environment that the API is invoked under.
3426* `[in] value`: The JavaScript value to check.
3427* `[out] result`: Whether the given object is an array.
3428
3429Returns `napi_ok` if the API succeeded.
3430
3431This API represents invoking the `IsArray` operation on the object
3432as defined in [Section 7.2.2][] of the ECMAScript Language Specification.
3433
3434### napi_is_arraybuffer
3435<!-- YAML
3436added: v8.0.0
3437napiVersion: 1
3438-->
3439
3440```c
3441napi_status napi_is_arraybuffer(napi_env env, napi_value value, bool* result)
3442```
3443
3444* `[in] env`: The environment that the API is invoked under.
3445* `[in] value`: The JavaScript value to check.
3446* `[out] result`: Whether the given object is an `ArrayBuffer`.
3447
3448Returns `napi_ok` if the API succeeded.
3449
3450This API checks if the `Object` passed in is an array buffer.
3451
3452### napi_is_buffer
3453<!-- YAML
3454added: v8.0.0
3455napiVersion: 1
3456-->
3457
3458```c
3459napi_status napi_is_buffer(napi_env env, napi_value value, bool* result)
3460```
3461
3462* `[in] env`: The environment that the API is invoked under.
3463* `[in] value`: The JavaScript value to check.
3464* `[out] result`: Whether the given `napi_value` represents a `node::Buffer`
3465  object.
3466
3467Returns `napi_ok` if the API succeeded.
3468
3469This API checks if the `Object` passed in is a buffer.
3470
3471### napi_is_date
3472<!-- YAML
3473added:
3474 - v11.11.0
3475 - v10.17.0
3476napiVersion: 5
3477-->
3478
3479```c
3480napi_status napi_is_date(napi_env env, napi_value value, bool* result)
3481```
3482
3483* `[in] env`: The environment that the API is invoked under.
3484* `[in] value`: The JavaScript value to check.
3485* `[out] result`: Whether the given `napi_value` represents a JavaScript `Date`
3486  object.
3487
3488Returns `napi_ok` if the API succeeded.
3489
3490This API checks if the `Object` passed in is a date.
3491
3492### napi_is_error
3493<!-- YAML
3494added: v8.0.0
3495napiVersion: 1
3496-->
3497
3498```c
3499napi_status napi_is_error(napi_env env, napi_value value, bool* result)
3500```
3501
3502* `[in] env`: The environment that the API is invoked under.
3503* `[in] value`: The JavaScript value to check.
3504* `[out] result`: Whether the given `napi_value` represents an `Error` object.
3505
3506Returns `napi_ok` if the API succeeded.
3507
3508This API checks if the `Object` passed in is an `Error`.
3509
3510### napi_is_typedarray
3511<!-- YAML
3512added: v8.0.0
3513napiVersion: 1
3514-->
3515
3516```c
3517napi_status napi_is_typedarray(napi_env env, napi_value value, bool* result)
3518```
3519
3520* `[in] env`: The environment that the API is invoked under.
3521* `[in] value`: The JavaScript value to check.
3522* `[out] result`: Whether the given `napi_value` represents a `TypedArray`.
3523
3524Returns `napi_ok` if the API succeeded.
3525
3526This API checks if the `Object` passed in is a typed array.
3527
3528### napi_is_dataview
3529<!-- YAML
3530added: v8.3.0
3531napiVersion: 1
3532-->
3533
3534```c
3535napi_status napi_is_dataview(napi_env env, napi_value value, bool* result)
3536```
3537
3538* `[in] env`: The environment that the API is invoked under.
3539* `[in] value`: The JavaScript value to check.
3540* `[out] result`: Whether the given `napi_value` represents a `DataView`.
3541
3542Returns `napi_ok` if the API succeeded.
3543
3544This API checks if the `Object` passed in is a `DataView`.
3545
3546### napi_strict_equals
3547<!-- YAML
3548added: v8.0.0
3549napiVersion: 1
3550-->
3551
3552```c
3553napi_status napi_strict_equals(napi_env env,
3554                               napi_value lhs,
3555                               napi_value rhs,
3556                               bool* result)
3557```
3558
3559* `[in] env`: The environment that the API is invoked under.
3560* `[in] lhs`: The JavaScript value to check.
3561* `[in] rhs`: The JavaScript value to check against.
3562* `[out] result`: Whether the two `napi_value` objects are equal.
3563
3564Returns `napi_ok` if the API succeeded.
3565
3566This API represents the invocation of the Strict Equality algorithm as
3567defined in [Section 7.2.14][] of the ECMAScript Language Specification.
3568
3569### napi_detach_arraybuffer
3570<!-- YAML
3571added:
3572 - v13.0.0
3573 - v12.16.0
3574 - v10.22.0
3575napiVersion: 7
3576-->
3577
3578```c
3579napi_status napi_detach_arraybuffer(napi_env env,
3580                                    napi_value arraybuffer)
3581```
3582
3583* `[in] env`: The environment that the API is invoked under.
3584* `[in] arraybuffer`: The JavaScript `ArrayBuffer` to be detached.
3585
3586Returns `napi_ok` if the API succeeded. If a non-detachable `ArrayBuffer` is
3587passed in it returns `napi_detachable_arraybuffer_expected`.
3588
3589Generally, an `ArrayBuffer` is non-detachable if it has been detached before.
3590The engine may impose additional conditions on whether an `ArrayBuffer` is
3591detachable. For example, V8 requires that the `ArrayBuffer` be external,
3592that is, created with [`napi_create_external_arraybuffer`][].
3593
3594This API represents the invocation of the `ArrayBuffer` detach operation as
3595defined in [Section 24.1.1.3][] of the ECMAScript Language Specification.
3596
3597### napi_is_detached_arraybuffer
3598<!-- YAML
3599added:
3600 - v13.3.0
3601 - v12.16.0
3602 - v10.22.0
3603napiVersion: 7
3604-->
3605
3606```c
3607napi_status napi_is_detached_arraybuffer(napi_env env,
3608                                         napi_value arraybuffer,
3609                                         bool* result)
3610```
3611
3612* `[in] env`: The environment that the API is invoked under.
3613* `[in] arraybuffer`: The JavaScript `ArrayBuffer` to be checked.
3614* `[out] result`: Whether the `arraybuffer` is detached.
3615
3616Returns `napi_ok` if the API succeeded.
3617
3618The `ArrayBuffer` is considered detached if its internal data is `null`.
3619
3620This API represents the invocation of the `ArrayBuffer` `IsDetachedBuffer`
3621operation as defined in [Section 24.1.1.2][] of the ECMAScript Language
3622Specification.
3623
3624## Working with JavaScript properties
3625
3626Node-API exposes a set of APIs to get and set properties on JavaScript
3627objects. Some of these types are documented under [Section 7][] of the
3628[ECMAScript Language Specification][].
3629
3630Properties in JavaScript are represented as a tuple of a key and a value.
3631Fundamentally, all property keys in Node-API can be represented in one of the
3632following forms:
3633
3634* Named: a simple UTF8-encoded string
3635* Integer-Indexed: an index value represented by `uint32_t`
3636* JavaScript value: these are represented in Node-API by `napi_value`. This can
3637  be a `napi_value` representing a `string`, `number`, or `symbol`.
3638
3639Node-API values are represented by the type `napi_value`.
3640Any Node-API call that requires a JavaScript value takes in a `napi_value`.
3641However, it's the caller's responsibility to make sure that the
3642`napi_value` in question is of the JavaScript type expected by the API.
3643
3644The APIs documented in this section provide a simple interface to
3645get and set properties on arbitrary JavaScript objects represented by
3646`napi_value`.
3647
3648For instance, consider the following JavaScript code snippet:
3649
3650```js
3651const obj = {};
3652obj.myProp = 123;
3653```
3654
3655The equivalent can be done using Node-API values with the following snippet:
3656
3657```c
3658napi_status status = napi_generic_failure;
3659
3660// const obj = {}
3661napi_value obj, value;
3662status = napi_create_object(env, &obj);
3663if (status != napi_ok) return status;
3664
3665// Create a napi_value for 123
3666status = napi_create_int32(env, 123, &value);
3667if (status != napi_ok) return status;
3668
3669// obj.myProp = 123
3670status = napi_set_named_property(env, obj, "myProp", value);
3671if (status != napi_ok) return status;
3672```
3673
3674Indexed properties can be set in a similar manner. Consider the following
3675JavaScript snippet:
3676
3677```js
3678const arr = [];
3679arr[123] = 'hello';
3680```
3681
3682The equivalent can be done using Node-API values with the following snippet:
3683
3684```c
3685napi_status status = napi_generic_failure;
3686
3687// const arr = [];
3688napi_value arr, value;
3689status = napi_create_array(env, &arr);
3690if (status != napi_ok) return status;
3691
3692// Create a napi_value for 'hello'
3693status = napi_create_string_utf8(env, "hello", NAPI_AUTO_LENGTH, &value);
3694if (status != napi_ok) return status;
3695
3696// arr[123] = 'hello';
3697status = napi_set_element(env, arr, 123, value);
3698if (status != napi_ok) return status;
3699```
3700
3701Properties can be retrieved using the APIs described in this section.
3702Consider the following JavaScript snippet:
3703
3704```js
3705const arr = [];
3706const value = arr[123];
3707```
3708
3709The following is the approximate equivalent of the Node-API counterpart:
3710
3711```c
3712napi_status status = napi_generic_failure;
3713
3714// const arr = []
3715napi_value arr, value;
3716status = napi_create_array(env, &arr);
3717if (status != napi_ok) return status;
3718
3719// const value = arr[123]
3720status = napi_get_element(env, arr, 123, &value);
3721if (status != napi_ok) return status;
3722```
3723
3724Finally, multiple properties can also be defined on an object for performance
3725reasons. Consider the following JavaScript:
3726
3727```js
3728const obj = {};
3729Object.defineProperties(obj, {
3730  'foo': { value: 123, writable: true, configurable: true, enumerable: true },
3731  'bar': { value: 456, writable: true, configurable: true, enumerable: true }
3732});
3733```
3734
3735The following is the approximate equivalent of the Node-API counterpart:
3736
3737```c
3738napi_status status = napi_status_generic_failure;
3739
3740// const obj = {};
3741napi_value obj;
3742status = napi_create_object(env, &obj);
3743if (status != napi_ok) return status;
3744
3745// Create napi_values for 123 and 456
3746napi_value fooValue, barValue;
3747status = napi_create_int32(env, 123, &fooValue);
3748if (status != napi_ok) return status;
3749status = napi_create_int32(env, 456, &barValue);
3750if (status != napi_ok) return status;
3751
3752// Set the properties
3753napi_property_descriptor descriptors[] = {
3754  { "foo", NULL, NULL, NULL, NULL, fooValue, napi_writable | napi_configurable, NULL },
3755  { "bar", NULL, NULL, NULL, NULL, barValue, napi_writable | napi_configurable, NULL }
3756}
3757status = napi_define_properties(env,
3758                                obj,
3759                                sizeof(descriptors) / sizeof(descriptors[0]),
3760                                descriptors);
3761if (status != napi_ok) return status;
3762```
3763
3764### Structures
3765#### napi_property_attributes
3766<!-- YAML
3767changes:
3768 - version: v14.12.0
3769   pr-url: https://github.com/nodejs/node/pull/35214
3770   description: added `napi_default_method` and `napi_default_property`.
3771-->
3772
3773```c
3774typedef enum {
3775  napi_default = 0,
3776  napi_writable = 1 << 0,
3777  napi_enumerable = 1 << 1,
3778  napi_configurable = 1 << 2,
3779
3780  // Used with napi_define_class to distinguish static properties
3781  // from instance properties. Ignored by napi_define_properties.
3782  napi_static = 1 << 10,
3783
3784  // Default for class methods.
3785  napi_default_method = napi_writable | napi_configurable,
3786
3787  // Default for object properties, like in JS obj[prop].
3788  napi_default_jsproperty = napi_writable |
3789                          napi_enumerable |
3790                          napi_configurable,
3791} napi_property_attributes;
3792```
3793
3794`napi_property_attributes` are flags used to control the behavior of properties
3795set on a JavaScript object. Other than `napi_static` they correspond to the
3796attributes listed in [Section 6.1.7.1][]
3797of the [ECMAScript Language Specification][].
3798They can be one or more of the following bitflags:
3799
3800* `napi_default`: No explicit attributes are set on the property. By default, a
3801  property is read only, not enumerable and not configurable.
3802* `napi_writable`: The property is writable.
3803* `napi_enumerable`: The property is enumerable.
3804* `napi_configurable`: The property is configurable as defined in
3805  [Section 6.1.7.1][] of the [ECMAScript Language Specification][].
3806* `napi_static`: The property will be defined as a static property on a class as
3807  opposed to an instance property, which is the default. This is used only by
3808  [`napi_define_class`][]. It is ignored by `napi_define_properties`.
3809* `napi_default_method`: Like a method in a JS class, the property is
3810  configurable and writeable, but not enumerable.
3811* `napi_default_jsproperty`: Like a property set via assignment in JavaScript,
3812  the property is writable, enumerable, and configurable.
3813
3814#### napi_property_descriptor
3815
3816```c
3817typedef struct {
3818  // One of utf8name or name should be NULL.
3819  const char* utf8name;
3820  napi_value name;
3821
3822  napi_callback method;
3823  napi_callback getter;
3824  napi_callback setter;
3825  napi_value value;
3826
3827  napi_property_attributes attributes;
3828  void* data;
3829} napi_property_descriptor;
3830```
3831
3832* `utf8name`: Optional string describing the key for the property,
3833  encoded as UTF8. One of `utf8name` or `name` must be provided for the
3834  property.
3835* `name`: Optional `napi_value` that points to a JavaScript string or symbol
3836  to be used as the key for the property. One of `utf8name` or `name` must
3837  be provided for the property.
3838* `value`: The value that's retrieved by a get access of the property if the
3839  property is a data property. If this is passed in, set `getter`, `setter`,
3840  `method` and `data` to `NULL` (since these members won't be used).
3841* `getter`: A function to call when a get access of the property is performed.
3842  If this is passed in, set `value` and `method` to `NULL` (since these members
3843  won't be used). The given function is called implicitly by the runtime when
3844  the property is accessed from JavaScript code (or if a get on the property is
3845  performed using a Node-API call). [`napi_callback`][] provides more details.
3846* `setter`: A function to call when a set access of the property is performed.
3847  If this is passed in, set `value` and `method` to `NULL` (since these members
3848  won't be used). The given function is called implicitly by the runtime when
3849  the property is set from JavaScript code (or if a set on the property is
3850  performed using a Node-API call). [`napi_callback`][] provides more details.
3851* `method`: Set this to make the property descriptor object's `value`
3852  property to be a JavaScript function represented by `method`. If this is
3853  passed in, set `value`, `getter` and `setter` to `NULL` (since these members
3854  won't be used). [`napi_callback`][] provides more details.
3855* `attributes`: The attributes associated with the particular property. See
3856  [`napi_property_attributes`][].
3857* `data`: The callback data passed into `method`, `getter` and `setter` if this
3858  function is invoked.
3859
3860### Functions
3861#### napi_get_property_names
3862<!-- YAML
3863added: v8.0.0
3864napiVersion: 1
3865-->
3866
3867```c
3868napi_status napi_get_property_names(napi_env env,
3869                                    napi_value object,
3870                                    napi_value* result);
3871```
3872
3873* `[in] env`: The environment that the Node-API call is invoked under.
3874* `[in] object`: The object from which to retrieve the properties.
3875* `[out] result`: A `napi_value` representing an array of JavaScript values
3876  that represent the property names of the object. The API can be used to
3877  iterate over `result` using [`napi_get_array_length`][]
3878  and [`napi_get_element`][].
3879
3880Returns `napi_ok` if the API succeeded.
3881
3882This API returns the names of the enumerable properties of `object` as an array
3883of strings. The properties of `object` whose key is a symbol will not be
3884included.
3885
3886#### napi_get_all_property_names
3887<!-- YAML
3888added:
3889 - v13.7.0
3890 - v10.20.0
3891napiVersion: 6
3892-->
3893
3894```c
3895napi_get_all_property_names(napi_env env,
3896                            napi_value object,
3897                            napi_key_collection_mode key_mode,
3898                            napi_key_filter key_filter,
3899                            napi_key_conversion key_conversion,
3900                            napi_value* result);
3901```
3902
3903* `[in] env`: The environment that the Node-API call is invoked under.
3904* `[in] object`: The object from which to retrieve the properties.
3905* `[in] key_mode`: Whether to retrieve prototype properties as well.
3906* `[in] key_filter`: Which properties to retrieve
3907  (enumerable/readable/writable).
3908* `[in] key_conversion`: Whether to convert numbered property keys to strings.
3909* `[out] result`: A `napi_value` representing an array of JavaScript values
3910  that represent the property names of the object. [`napi_get_array_length`][]
3911  and [`napi_get_element`][] can be used to iterate over `result`.
3912
3913Returns `napi_ok` if the API succeeded.
3914
3915This API returns an array containing the names of the available properties
3916of this object.
3917
3918#### napi_set_property
3919<!-- YAML
3920added: v8.0.0
3921napiVersion: 1
3922-->
3923
3924```c
3925napi_status napi_set_property(napi_env env,
3926                              napi_value object,
3927                              napi_value key,
3928                              napi_value value);
3929```
3930
3931* `[in] env`: The environment that the Node-API call is invoked under.
3932* `[in] object`: The object on which to set the property.
3933* `[in] key`: The name of the property to set.
3934* `[in] value`: The property value.
3935
3936Returns `napi_ok` if the API succeeded.
3937
3938This API set a property on the `Object` passed in.
3939
3940#### napi_get_property
3941<!-- YAML
3942added: v8.0.0
3943napiVersion: 1
3944-->
3945
3946```c
3947napi_status napi_get_property(napi_env env,
3948                              napi_value object,
3949                              napi_value key,
3950                              napi_value* result);
3951```
3952
3953* `[in] env`: The environment that the Node-API call is invoked under.
3954* `[in] object`: The object from which to retrieve the property.
3955* `[in] key`: The name of the property to retrieve.
3956* `[out] result`: The value of the property.
3957
3958Returns `napi_ok` if the API succeeded.
3959
3960This API gets the requested property from the `Object` passed in.
3961
3962#### napi_has_property
3963<!-- YAML
3964added: v8.0.0
3965napiVersion: 1
3966-->
3967
3968```c
3969napi_status napi_has_property(napi_env env,
3970                              napi_value object,
3971                              napi_value key,
3972                              bool* result);
3973```
3974
3975* `[in] env`: The environment that the Node-API call is invoked under.
3976* `[in] object`: The object to query.
3977* `[in] key`: The name of the property whose existence to check.
3978* `[out] result`: Whether the property exists on the object or not.
3979
3980Returns `napi_ok` if the API succeeded.
3981
3982This API checks if the `Object` passed in has the named property.
3983
3984#### napi_delete_property
3985<!-- YAML
3986added: v8.2.0
3987napiVersion: 1
3988-->
3989
3990```c
3991napi_status napi_delete_property(napi_env env,
3992                                 napi_value object,
3993                                 napi_value key,
3994                                 bool* result);
3995```
3996
3997* `[in] env`: The environment that the Node-API call is invoked under.
3998* `[in] object`: The object to query.
3999* `[in] key`: The name of the property to delete.
4000* `[out] result`: Whether the property deletion succeeded or not. `result` can
4001  optionally be ignored by passing `NULL`.
4002
4003Returns `napi_ok` if the API succeeded.
4004
4005This API attempts to delete the `key` own property from `object`.
4006
4007#### napi_has_own_property
4008<!-- YAML
4009added: v8.2.0
4010napiVersion: 1
4011-->
4012
4013```c
4014napi_status napi_has_own_property(napi_env env,
4015                                  napi_value object,
4016                                  napi_value key,
4017                                  bool* result);
4018```
4019
4020* `[in] env`: The environment that the Node-API call is invoked under.
4021* `[in] object`: The object to query.
4022* `[in] key`: The name of the own property whose existence to check.
4023* `[out] result`: Whether the own property exists on the object or not.
4024
4025Returns `napi_ok` if the API succeeded.
4026
4027This API checks if the `Object` passed in has the named own property. `key` must
4028be a `string` or a `symbol`, or an error will be thrown. Node-API will not
4029perform any conversion between data types.
4030
4031#### napi_set_named_property
4032<!-- YAML
4033added: v8.0.0
4034napiVersion: 1
4035-->
4036
4037```c
4038napi_status napi_set_named_property(napi_env env,
4039                                    napi_value object,
4040                                    const char* utf8Name,
4041                                    napi_value value);
4042```
4043
4044* `[in] env`: The environment that the Node-API call is invoked under.
4045* `[in] object`: The object on which to set the property.
4046* `[in] utf8Name`: The name of the property to set.
4047* `[in] value`: The property value.
4048
4049Returns `napi_ok` if the API succeeded.
4050
4051This method is equivalent to calling [`napi_set_property`][] with a `napi_value`
4052created from the string passed in as `utf8Name`.
4053
4054#### napi_get_named_property
4055<!-- YAML
4056added: v8.0.0
4057napiVersion: 1
4058-->
4059
4060```c
4061napi_status napi_get_named_property(napi_env env,
4062                                    napi_value object,
4063                                    const char* utf8Name,
4064                                    napi_value* result);
4065```
4066
4067* `[in] env`: The environment that the Node-API call is invoked under.
4068* `[in] object`: The object from which to retrieve the property.
4069* `[in] utf8Name`: The name of the property to get.
4070* `[out] result`: The value of the property.
4071
4072Returns `napi_ok` if the API succeeded.
4073
4074This method is equivalent to calling [`napi_get_property`][] with a `napi_value`
4075created from the string passed in as `utf8Name`.
4076
4077#### napi_has_named_property
4078<!-- YAML
4079added: v8.0.0
4080napiVersion: 1
4081-->
4082
4083```c
4084napi_status napi_has_named_property(napi_env env,
4085                                    napi_value object,
4086                                    const char* utf8Name,
4087                                    bool* result);
4088```
4089
4090* `[in] env`: The environment that the Node-API call is invoked under.
4091* `[in] object`: The object to query.
4092* `[in] utf8Name`: The name of the property whose existence to check.
4093* `[out] result`: Whether the property exists on the object or not.
4094
4095Returns `napi_ok` if the API succeeded.
4096
4097This method is equivalent to calling [`napi_has_property`][] with a `napi_value`
4098created from the string passed in as `utf8Name`.
4099
4100#### napi_set_element
4101<!-- YAML
4102added: v8.0.0
4103napiVersion: 1
4104-->
4105
4106```c
4107napi_status napi_set_element(napi_env env,
4108                             napi_value object,
4109                             uint32_t index,
4110                             napi_value value);
4111```
4112
4113* `[in] env`: The environment that the Node-API call is invoked under.
4114* `[in] object`: The object from which to set the properties.
4115* `[in] index`: The index of the property to set.
4116* `[in] value`: The property value.
4117
4118Returns `napi_ok` if the API succeeded.
4119
4120This API sets and element on the `Object` passed in.
4121
4122#### napi_get_element
4123<!-- YAML
4124added: v8.0.0
4125napiVersion: 1
4126-->
4127
4128```c
4129napi_status napi_get_element(napi_env env,
4130                             napi_value object,
4131                             uint32_t index,
4132                             napi_value* result);
4133```
4134
4135* `[in] env`: The environment that the Node-API call is invoked under.
4136* `[in] object`: The object from which to retrieve the property.
4137* `[in] index`: The index of the property to get.
4138* `[out] result`: The value of the property.
4139
4140Returns `napi_ok` if the API succeeded.
4141
4142This API gets the element at the requested index.
4143
4144#### napi_has_element
4145<!-- YAML
4146added: v8.0.0
4147napiVersion: 1
4148-->
4149
4150```c
4151napi_status napi_has_element(napi_env env,
4152                             napi_value object,
4153                             uint32_t index,
4154                             bool* result);
4155```
4156
4157* `[in] env`: The environment that the Node-API call is invoked under.
4158* `[in] object`: The object to query.
4159* `[in] index`: The index of the property whose existence to check.
4160* `[out] result`: Whether the property exists on the object or not.
4161
4162Returns `napi_ok` if the API succeeded.
4163
4164This API returns if the `Object` passed in has an element at the
4165requested index.
4166
4167#### napi_delete_element
4168<!-- YAML
4169added: v8.2.0
4170napiVersion: 1
4171-->
4172
4173```c
4174napi_status napi_delete_element(napi_env env,
4175                                napi_value object,
4176                                uint32_t index,
4177                                bool* result);
4178```
4179
4180* `[in] env`: The environment that the Node-API call is invoked under.
4181* `[in] object`: The object to query.
4182* `[in] index`: The index of the property to delete.
4183* `[out] result`: Whether the element deletion succeeded or not. `result` can
4184  optionally be ignored by passing `NULL`.
4185
4186Returns `napi_ok` if the API succeeded.
4187
4188This API attempts to delete the specified `index` from `object`.
4189
4190#### napi_define_properties
4191<!-- YAML
4192added: v8.0.0
4193napiVersion: 1
4194-->
4195
4196```c
4197napi_status napi_define_properties(napi_env env,
4198                                   napi_value object,
4199                                   size_t property_count,
4200                                   const napi_property_descriptor* properties);
4201```
4202
4203* `[in] env`: The environment that the Node-API call is invoked under.
4204* `[in] object`: The object from which to retrieve the properties.
4205* `[in] property_count`: The number of elements in the `properties` array.
4206* `[in] properties`: The array of property descriptors.
4207
4208Returns `napi_ok` if the API succeeded.
4209
4210This method allows the efficient definition of multiple properties on a given
4211object. The properties are defined using property descriptors (see
4212[`napi_property_descriptor`][]). Given an array of such property descriptors,
4213this API will set the properties on the object one at a time, as defined by
4214`DefineOwnProperty()` (described in [Section 9.1.6][] of the ECMA-262
4215specification).
4216
4217#### napi_object_freeze
4218<!-- YAML
4219added:
4220  - v14.14.0
4221  - v12.20.0
4222napiVersion: 8
4223-->
4224
4225```c
4226napi_status napi_object_freeze(napi_env env,
4227                               napi_value object);
4228```
4229
4230* `[in] env`: The environment that the Node-API call is invoked under.
4231* `[in] object`: The object to freeze.
4232
4233Returns `napi_ok` if the API succeeded.
4234
4235This method freezes a given object. This prevents new properties from
4236being added to it, existing properties from being removed, prevents
4237changing the enumerability, configurability, or writability of existing
4238properties, and prevents the values of existing properties from being changed.
4239It also prevents the object's prototype from being changed. This is described
4240in [Section 19.1.2.6](https://tc39.es/ecma262/#sec-object.freeze) of the
4241ECMA-262 specification.
4242
4243#### napi_object_seal
4244<!-- YAML
4245added:
4246  - v14.14.0
4247  - v12.20.0
4248napiVersion: 8
4249-->
4250
4251```c
4252napi_status napi_object_seal(napi_env env,
4253                             napi_value object);
4254```
4255
4256* `[in] env`: The environment that the Node-API call is invoked under.
4257* `[in] object`: The object to seal.
4258
4259Returns `napi_ok` if the API succeeded.
4260
4261This method seals a given object. This prevents new properties from being
4262added to it, as well as marking all existing properties as non-configurable.
4263This is described in [Section 19.1.2.20](https://tc39.es/ecma262/#sec-object.seal)
4264of the ECMA-262 specification.
4265
4266## Working with JavaScript functions
4267
4268Node-API provides a set of APIs that allow JavaScript code to
4269call back into native code. Node-APIs that support calling back
4270into native code take in a callback functions represented by
4271the `napi_callback` type. When the JavaScript VM calls back to
4272native code, the `napi_callback` function provided is invoked. The APIs
4273documented in this section allow the callback function to do the
4274following:
4275
4276* Get information about the context in which the callback was invoked.
4277* Get the arguments passed into the callback.
4278* Return a `napi_value` back from the callback.
4279
4280Additionally, Node-API provides a set of functions which allow calling
4281JavaScript functions from native code. One can either call a function
4282like a regular JavaScript function call, or as a constructor
4283function.
4284
4285Any non-`NULL` data which is passed to this API via the `data` field of the
4286`napi_property_descriptor` items can be associated with `object` and freed
4287whenever `object` is garbage-collected by passing both `object` and the data to
4288[`napi_add_finalizer`][].
4289
4290### napi_call_function
4291<!-- YAML
4292added: v8.0.0
4293napiVersion: 1
4294-->
4295
4296```c
4297NAPI_EXTERN napi_status napi_call_function(napi_env env,
4298                                           napi_value recv,
4299                                           napi_value func,
4300                                           size_t argc,
4301                                           const napi_value* argv,
4302                                           napi_value* result);
4303```
4304
4305* `[in] env`: The environment that the API is invoked under.
4306* `[in] recv`: The `this` value passed to the called function.
4307* `[in] func`: `napi_value` representing the JavaScript function to be invoked.
4308* `[in] argc`: The count of elements in the `argv` array.
4309* `[in] argv`: Array of `napi_values` representing JavaScript values passed in
4310  as arguments to the function.
4311* `[out] result`: `napi_value` representing the JavaScript object returned.
4312
4313Returns `napi_ok` if the API succeeded.
4314
4315This method allows a JavaScript function object to be called from a native
4316add-on. This is the primary mechanism of calling back *from* the add-on's
4317native code *into* JavaScript. For the special case of calling into JavaScript
4318after an async operation, see [`napi_make_callback`][].
4319
4320A sample use case might look as follows. Consider the following JavaScript
4321snippet:
4322
4323```js
4324function AddTwo(num) {
4325  return num + 2;
4326}
4327```
4328
4329Then, the above function can be invoked from a native add-on using the
4330following code:
4331
4332```c
4333// Get the function named "AddTwo" on the global object
4334napi_value global, add_two, arg;
4335napi_status status = napi_get_global(env, &global);
4336if (status != napi_ok) return;
4337
4338status = napi_get_named_property(env, global, "AddTwo", &add_two);
4339if (status != napi_ok) return;
4340
4341// const arg = 1337
4342status = napi_create_int32(env, 1337, &arg);
4343if (status != napi_ok) return;
4344
4345napi_value* argv = &arg;
4346size_t argc = 1;
4347
4348// AddTwo(arg);
4349napi_value return_val;
4350status = napi_call_function(env, global, add_two, argc, argv, &return_val);
4351if (status != napi_ok) return;
4352
4353// Convert the result back to a native type
4354int32_t result;
4355status = napi_get_value_int32(env, return_val, &result);
4356if (status != napi_ok) return;
4357```
4358
4359### napi_create_function
4360<!-- YAML
4361added: v8.0.0
4362napiVersion: 1
4363-->
4364
4365```c
4366napi_status napi_create_function(napi_env env,
4367                                 const char* utf8name,
4368                                 size_t length,
4369                                 napi_callback cb,
4370                                 void* data,
4371                                 napi_value* result);
4372```
4373
4374* `[in] env`: The environment that the API is invoked under.
4375* `[in] utf8Name`: The name of the function encoded as UTF8. This is visible
4376  within JavaScript as the new function object's `name` property.
4377* `[in] length`: The length of the `utf8name` in bytes, or `NAPI_AUTO_LENGTH` if
4378  it is null-terminated.
4379* `[in] cb`: The native function which should be called when this function
4380  object is invoked. [`napi_callback`][] provides more details.
4381* `[in] data`: User-provided data context. This will be passed back into the
4382  function when invoked later.
4383* `[out] result`: `napi_value` representing the JavaScript function object for
4384  the newly created function.
4385
4386Returns `napi_ok` if the API succeeded.
4387
4388This API allows an add-on author to create a function object in native code.
4389This is the primary mechanism to allow calling *into* the add-on's native code
4390*from* JavaScript.
4391
4392The newly created function is not automatically visible from script after this
4393call. Instead, a property must be explicitly set on any object that is visible
4394to JavaScript, in order for the function to be accessible from script.
4395
4396In order to expose a function as part of the
4397add-on's module exports, set the newly created function on the exports
4398object. A sample module might look as follows:
4399
4400```c
4401napi_value SayHello(napi_env env, napi_callback_info info) {
4402  printf("Hello\n");
4403  return NULL;
4404}
4405
4406napi_value Init(napi_env env, napi_value exports) {
4407  napi_status status;
4408
4409  napi_value fn;
4410  status = napi_create_function(env, NULL, 0, SayHello, NULL, &fn);
4411  if (status != napi_ok) return NULL;
4412
4413  status = napi_set_named_property(env, exports, "sayHello", fn);
4414  if (status != napi_ok) return NULL;
4415
4416  return exports;
4417}
4418
4419NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
4420```
4421
4422Given the above code, the add-on can be used from JavaScript as follows:
4423
4424```js
4425const myaddon = require('./addon');
4426myaddon.sayHello();
4427```
4428
4429The string passed to `require()` is the name of the target in `binding.gyp`
4430responsible for creating the `.node` file.
4431
4432Any non-`NULL` data which is passed to this API via the `data` parameter can
4433be associated with the resulting JavaScript function (which is returned in the
4434`result` parameter) and freed whenever the function is garbage-collected by
4435passing both the JavaScript function and the data to [`napi_add_finalizer`][].
4436
4437JavaScript `Function`s are described in [Section 19.2][] of the ECMAScript
4438Language Specification.
4439
4440### napi_get_cb_info
4441<!-- YAML
4442added: v8.0.0
4443napiVersion: 1
4444-->
4445
4446```c
4447napi_status napi_get_cb_info(napi_env env,
4448                             napi_callback_info cbinfo,
4449                             size_t* argc,
4450                             napi_value* argv,
4451                             napi_value* thisArg,
4452                             void** data)
4453```
4454
4455* `[in] env`: The environment that the API is invoked under.
4456* `[in] cbinfo`: The callback info passed into the callback function.
4457* `[in-out] argc`: Specifies the length of the provided `argv` array and
4458  receives the actual count of arguments.
4459* `[out] argv`: Buffer to which the `napi_value` representing the arguments are
4460  copied. If there are more arguments than the provided count, only the
4461  requested number of arguments are copied. If there are fewer arguments
4462  provided than claimed, the rest of `argv` is filled with `napi_value` values
4463  that represent `undefined`.
4464* `[out] this`: Receives the JavaScript `this` argument for the call.
4465* `[out] data`: Receives the data pointer for the callback.
4466
4467Returns `napi_ok` if the API succeeded.
4468
4469This method is used within a callback function to retrieve details about the
4470call like the arguments and the `this` pointer from a given callback info.
4471
4472### napi_get_new_target
4473<!-- YAML
4474added: v8.6.0
4475napiVersion: 1
4476-->
4477
4478```c
4479napi_status napi_get_new_target(napi_env env,
4480                                napi_callback_info cbinfo,
4481                                napi_value* result)
4482```
4483
4484* `[in] env`: The environment that the API is invoked under.
4485* `[in] cbinfo`: The callback info passed into the callback function.
4486* `[out] result`: The `new.target` of the constructor call.
4487
4488Returns `napi_ok` if the API succeeded.
4489
4490This API returns the `new.target` of the constructor call. If the current
4491callback is not a constructor call, the result is `NULL`.
4492
4493### napi_new_instance
4494<!-- YAML
4495added: v8.0.0
4496napiVersion: 1
4497-->
4498
4499```c
4500napi_status napi_new_instance(napi_env env,
4501                              napi_value cons,
4502                              size_t argc,
4503                              napi_value* argv,
4504                              napi_value* result)
4505```
4506
4507* `[in] env`: The environment that the API is invoked under.
4508* `[in] cons`: `napi_value` representing the JavaScript function to be invoked
4509  as a constructor.
4510* `[in] argc`: The count of elements in the `argv` array.
4511* `[in] argv`: Array of JavaScript values as `napi_value` representing the
4512  arguments to the constructor.
4513* `[out] result`: `napi_value` representing the JavaScript object returned,
4514  which in this case is the constructed object.
4515
4516This method is used to instantiate a new JavaScript value using a given
4517`napi_value` that represents the constructor for the object. For example,
4518consider the following snippet:
4519
4520```js
4521function MyObject(param) {
4522  this.param = param;
4523}
4524
4525const arg = 'hello';
4526const value = new MyObject(arg);
4527```
4528
4529The following can be approximated in Node-API using the following snippet:
4530
4531```c
4532// Get the constructor function MyObject
4533napi_value global, constructor, arg, value;
4534napi_status status = napi_get_global(env, &global);
4535if (status != napi_ok) return;
4536
4537status = napi_get_named_property(env, global, "MyObject", &constructor);
4538if (status != napi_ok) return;
4539
4540// const arg = "hello"
4541status = napi_create_string_utf8(env, "hello", NAPI_AUTO_LENGTH, &arg);
4542if (status != napi_ok) return;
4543
4544napi_value* argv = &arg;
4545size_t argc = 1;
4546
4547// const value = new MyObject(arg)
4548status = napi_new_instance(env, constructor, argc, argv, &value);
4549```
4550
4551Returns `napi_ok` if the API succeeded.
4552
4553## Object wrap
4554
4555Node-API offers a way to "wrap" C++ classes and instances so that the class
4556constructor and methods can be called from JavaScript.
4557
45581. The [`napi_define_class`][] API defines a JavaScript class with constructor,
4559    static properties and methods, and instance properties and methods that
4560    correspond to the C++ class.
45612. When JavaScript code invokes the constructor, the constructor callback
4562    uses [`napi_wrap`][] to wrap a new C++ instance in a JavaScript object,
4563    then returns the wrapper object.
45643. When JavaScript code invokes a method or property accessor on the class,
4565    the corresponding `napi_callback` C++ function is invoked. For an instance
4566    callback, [`napi_unwrap`][] obtains the C++ instance that is the target of
4567    the call.
4568
4569For wrapped objects it may be difficult to distinguish between a function
4570called on a class prototype and a function called on an instance of a class.
4571A common pattern used to address this problem is to save a persistent
4572reference to the class constructor for later `instanceof` checks.
4573
4574```c
4575napi_value MyClass_constructor = NULL;
4576status = napi_get_reference_value(env, MyClass::es_constructor, &MyClass_constructor);
4577assert(napi_ok == status);
4578bool is_instance = false;
4579status = napi_instanceof(env, es_this, MyClass_constructor, &is_instance);
4580assert(napi_ok == status);
4581if (is_instance) {
4582  // napi_unwrap() ...
4583} else {
4584  // otherwise...
4585}
4586```
4587
4588The reference must be freed once it is no longer needed.
4589
4590There are occasions where `napi_instanceof()` is insufficient for ensuring that
4591a JavaScript object is a wrapper for a certain native type. This is the case
4592especially when wrapped JavaScript objects are passed back into the addon via
4593static methods rather than as the `this` value of prototype methods. In such
4594cases there is a chance that they may be unwrapped incorrectly.
4595
4596```js
4597const myAddon = require('./build/Release/my_addon.node');
4598
4599// `openDatabase()` returns a JavaScript object that wraps a native database
4600// handle.
4601const dbHandle = myAddon.openDatabase();
4602
4603// `query()` returns a JavaScript object that wraps a native query handle.
4604const queryHandle = myAddon.query(dbHandle, 'Gimme ALL the things!');
4605
4606// There is an accidental error in the line below. The first parameter to
4607// `myAddon.queryHasRecords()` should be the database handle (`dbHandle`), not
4608// the query handle (`query`), so the correct condition for the while-loop
4609// should be
4610//
4611// myAddon.queryHasRecords(dbHandle, queryHandle)
4612//
4613while (myAddon.queryHasRecords(queryHandle, dbHandle)) {
4614  // retrieve records
4615}
4616```
4617
4618In the above example `myAddon.queryHasRecords()` is a method that accepts two
4619arguments. The first is a database handle and the second is a query handle.
4620Internally, it unwraps the first argument and casts the resulting pointer to a
4621native database handle. It then unwraps the second argument and casts the
4622resulting pointer to a query handle. If the arguments are passed in the wrong
4623order, the casts will work, however, there is a good chance that the underlying
4624database operation will fail, or will even cause an invalid memory access.
4625
4626To ensure that the pointer retrieved from the first argument is indeed a pointer
4627to a database handle and, similarly, that the pointer retrieved from the second
4628argument is indeed a pointer to a query handle, the implementation of
4629`queryHasRecords()` has to perform a type validation. Retaining the JavaScript
4630class constructor from which the database handle was instantiated and the
4631constructor from which the query handle was instantiated in `napi_ref`s can
4632help, because `napi_instanceof()` can then be used to ensure that the instances
4633passed into `queryHashRecords()` are indeed of the correct type.
4634
4635Unfortunately, `napi_instanceof()` does not protect against prototype
4636manipulation. For example, the prototype of the database handle instance can be
4637set to the prototype of the constructor for query handle instances. In this
4638case, the database handle instance can appear as a query handle instance, and it
4639will pass the `napi_instanceof()` test for a query handle instance, while still
4640containing a pointer to a database handle.
4641
4642To this end, Node-API provides type-tagging capabilities.
4643
4644A type tag is a 128-bit integer unique to the addon. Node-API provides the
4645`napi_type_tag` structure for storing a type tag. When such a value is passed
4646along with a JavaScript object stored in a `napi_value` to
4647`napi_type_tag_object()`, the JavaScript object will be "marked" with the
4648type tag. The "mark" is invisible on the JavaScript side. When a JavaScript
4649object arrives into a native binding, `napi_check_object_type_tag()` can be used
4650along with the original type tag to determine whether the JavaScript object was
4651previously "marked" with the type tag. This creates a type-checking capability
4652of a higher fidelity than `napi_instanceof()` can provide, because such type-
4653tagging survives prototype manipulation and addon unloading/reloading.
4654
4655Continuing the above example, the following skeleton addon implementation
4656illustrates the use of `napi_type_tag_object()` and
4657`napi_check_object_type_tag()`.
4658
4659```c
4660// This value is the type tag for a database handle. The command
4661//
4662//   uuidgen | sed -r -e 's/-//g' -e 's/(.{16})(.*)/0x\1, 0x\2/'
4663//
4664// can be used to obtain the two values with which to initialize the structure.
4665static const napi_type_tag DatabaseHandleTypeTag = {
4666  0x1edf75a38336451d, 0xa5ed9ce2e4c00c38
4667};
4668
4669// This value is the type tag for a query handle.
4670static const napi_type_tag QueryHandleTypeTag = {
4671  0x9c73317f9fad44a3, 0x93c3920bf3b0ad6a
4672};
4673
4674static napi_value
4675openDatabase(napi_env env, napi_callback_info info) {
4676  napi_status status;
4677  napi_value result;
4678
4679  // Perform the underlying action which results in a database handle.
4680  DatabaseHandle* dbHandle = open_database();
4681
4682  // Create a new, empty JS object.
4683  status = napi_create_object(env, &result);
4684  if (status != napi_ok) return NULL;
4685
4686  // Tag the object to indicate that it holds a pointer to a `DatabaseHandle`.
4687  status = napi_type_tag_object(env, result, &DatabaseHandleTypeTag);
4688  if (status != napi_ok) return NULL;
4689
4690  // Store the pointer to the `DatabaseHandle` structure inside the JS object.
4691  status = napi_wrap(env, result, dbHandle, NULL, NULL, NULL);
4692  if (status != napi_ok) return NULL;
4693
4694  return result;
4695}
4696
4697// Later when we receive a JavaScript object purporting to be a database handle
4698// we can use `napi_check_object_type_tag()` to ensure that it is indeed such a
4699// handle.
4700
4701static napi_value
4702query(napi_env env, napi_callback_info info) {
4703  napi_status status;
4704  size_t argc = 2;
4705  napi_value argv[2];
4706  bool is_db_handle;
4707
4708  status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
4709  if (status != napi_ok) return NULL;
4710
4711  // Check that the object passed as the first parameter has the previously
4712  // applied tag.
4713  status = napi_check_object_type_tag(env,
4714                                      argv[0],
4715                                      &DatabaseHandleTypeTag,
4716                                      &is_db_handle);
4717  if (status != napi_ok) return NULL;
4718
4719  // Throw a `TypeError` if it doesn't.
4720  if (!is_db_handle) {
4721    // Throw a TypeError.
4722    return NULL;
4723  }
4724}
4725```
4726
4727### napi_define_class
4728<!-- YAML
4729added: v8.0.0
4730napiVersion: 1
4731-->
4732
4733```c
4734napi_status napi_define_class(napi_env env,
4735                              const char* utf8name,
4736                              size_t length,
4737                              napi_callback constructor,
4738                              void* data,
4739                              size_t property_count,
4740                              const napi_property_descriptor* properties,
4741                              napi_value* result);
4742```
4743
4744* `[in] env`: The environment that the API is invoked under.
4745* `[in] utf8name`: Name of the JavaScript constructor function; When wrapping a
4746  C++ class, we recommend for clarity that this name be the same as that of
4747  the C++ class.
4748* `[in] length`: The length of the `utf8name` in bytes, or `NAPI_AUTO_LENGTH`
4749  if it is null-terminated.
4750* `[in] constructor`: Callback function that handles constructing instances
4751  of the class. When wrapping a C++ class, this method must be a static member
4752  with the [`napi_callback`][] signature. A C++ class constructor cannot be
4753  used. [`napi_callback`][] provides more details.
4754* `[in] data`: Optional data to be passed to the constructor callback as
4755  the `data` property of the callback info.
4756* `[in] property_count`: Number of items in the `properties` array argument.
4757* `[in] properties`: Array of property descriptors describing static and
4758  instance data properties, accessors, and methods on the class
4759  See `napi_property_descriptor`.
4760* `[out] result`: A `napi_value` representing the constructor function for
4761  the class.
4762
4763Returns `napi_ok` if the API succeeded.
4764
4765Defines a JavaScript class, including:
4766
4767* A JavaScript constructor function that has the class name. When wrapping a
4768  corresponding C++ class, the callback passed via `constructor` can be used to
4769  instantiate a new C++ class instance, which can then be placed inside the
4770  JavaScript object instance being constructed using [`napi_wrap`][].
4771* Properties on the constructor function whose implementation can call
4772  corresponding _static_ data properties, accessors, and methods of the C++
4773  class (defined by property descriptors with the `napi_static` attribute).
4774* Properties on the constructor function's `prototype` object. When wrapping a
4775  C++ class, _non-static_ data properties, accessors, and methods of the C++
4776  class can be called from the static functions given in the property
4777  descriptors without the `napi_static` attribute after retrieving the C++ class
4778  instance placed inside the JavaScript object instance by using
4779  [`napi_unwrap`][].
4780
4781When wrapping a C++ class, the C++ constructor callback passed via `constructor`
4782should be a static method on the class that calls the actual class constructor,
4783then wraps the new C++ instance in a JavaScript object, and returns the wrapper
4784object. See [`napi_wrap`][] for details.
4785
4786The JavaScript constructor function returned from [`napi_define_class`][] is
4787often saved and used later to construct new instances of the class from native
4788code, and/or to check whether provided values are instances of the class. In
4789that case, to prevent the function value from being garbage-collected, a
4790strong persistent reference to it can be created using
4791[`napi_create_reference`][], ensuring that the reference count is kept >= 1.
4792
4793Any non-`NULL` data which is passed to this API via the `data` parameter or via
4794the `data` field of the `napi_property_descriptor` array items can be associated
4795with the resulting JavaScript constructor (which is returned in the `result`
4796parameter) and freed whenever the class is garbage-collected by passing both
4797the JavaScript function and the data to [`napi_add_finalizer`][].
4798
4799### napi_wrap
4800<!-- YAML
4801added: v8.0.0
4802napiVersion: 1
4803-->
4804
4805```c
4806napi_status napi_wrap(napi_env env,
4807                      napi_value js_object,
4808                      void* native_object,
4809                      napi_finalize finalize_cb,
4810                      void* finalize_hint,
4811                      napi_ref* result);
4812```
4813
4814* `[in] env`: The environment that the API is invoked under.
4815* `[in] js_object`: The JavaScript object that will be the wrapper for the
4816  native object.
4817* `[in] native_object`: The native instance that will be wrapped in the
4818  JavaScript object.
4819* `[in] finalize_cb`: Optional native callback that can be used to free the
4820  native instance when the JavaScript object is ready for garbage-collection.
4821  [`napi_finalize`][] provides more details.
4822* `[in] finalize_hint`: Optional contextual hint that is passed to the
4823  finalize callback.
4824* `[out] result`: Optional reference to the wrapped object.
4825
4826Returns `napi_ok` if the API succeeded.
4827
4828Wraps a native instance in a JavaScript object. The native instance can be
4829retrieved later using `napi_unwrap()`.
4830
4831When JavaScript code invokes a constructor for a class that was defined using
4832`napi_define_class()`, the `napi_callback` for the constructor is invoked.
4833After constructing an instance of the native class, the callback must then call
4834`napi_wrap()` to wrap the newly constructed instance in the already-created
4835JavaScript object that is the `this` argument to the constructor callback.
4836(That `this` object was created from the constructor function's `prototype`,
4837so it already has definitions of all the instance properties and methods.)
4838
4839Typically when wrapping a class instance, a finalize callback should be
4840provided that simply deletes the native instance that is received as the `data`
4841argument to the finalize callback.
4842
4843The optional returned reference is initially a weak reference, meaning it
4844has a reference count of 0. Typically this reference count would be incremented
4845temporarily during async operations that require the instance to remain valid.
4846
4847*Caution*: The optional returned reference (if obtained) should be deleted via
4848[`napi_delete_reference`][] ONLY in response to the finalize callback
4849invocation. If it is deleted before then, then the finalize callback may never
4850be invoked. Therefore, when obtaining a reference a finalize callback is also
4851required in order to enable correct disposal of the reference.
4852
4853Calling `napi_wrap()` a second time on an object will return an error. To
4854associate another native instance with the object, use `napi_remove_wrap()`
4855first.
4856
4857### napi_unwrap
4858<!-- YAML
4859added: v8.0.0
4860napiVersion: 1
4861-->
4862
4863```c
4864napi_status napi_unwrap(napi_env env,
4865                        napi_value js_object,
4866                        void** result);
4867```
4868
4869* `[in] env`: The environment that the API is invoked under.
4870* `[in] js_object`: The object associated with the native instance.
4871* `[out] result`: Pointer to the wrapped native instance.
4872
4873Returns `napi_ok` if the API succeeded.
4874
4875Retrieves a native instance that was previously wrapped in a JavaScript
4876object using `napi_wrap()`.
4877
4878When JavaScript code invokes a method or property accessor on the class, the
4879corresponding `napi_callback` is invoked. If the callback is for an instance
4880method or accessor, then the `this` argument to the callback is the wrapper
4881object; the wrapped C++ instance that is the target of the call can be obtained
4882then by calling `napi_unwrap()` on the wrapper object.
4883
4884### napi_remove_wrap
4885<!-- YAML
4886added: v8.5.0
4887napiVersion: 1
4888-->
4889
4890```c
4891napi_status napi_remove_wrap(napi_env env,
4892                             napi_value js_object,
4893                             void** result);
4894```
4895
4896* `[in] env`: The environment that the API is invoked under.
4897* `[in] js_object`: The object associated with the native instance.
4898* `[out] result`: Pointer to the wrapped native instance.
4899
4900Returns `napi_ok` if the API succeeded.
4901
4902Retrieves a native instance that was previously wrapped in the JavaScript
4903object `js_object` using `napi_wrap()` and removes the wrapping. If a finalize
4904callback was associated with the wrapping, it will no longer be called when the
4905JavaScript object becomes garbage-collected.
4906
4907### napi_type_tag_object
4908<!-- YAML
4909added:
4910  - v14.8.0
4911  - v12.19.0
4912napiVersion: 8
4913-->
4914
4915```c
4916napi_status napi_type_tag_object(napi_env env,
4917                                 napi_value js_object,
4918                                 const napi_type_tag* type_tag);
4919```
4920
4921* `[in] env`: The environment that the API is invoked under.
4922* `[in] js_object`: The JavaScript object to be marked.
4923* `[in] type_tag`: The tag with which the object is to be marked.
4924
4925Returns `napi_ok` if the API succeeded.
4926
4927Associates the value of the `type_tag` pointer with the JavaScript object.
4928`napi_check_object_type_tag()` can then be used to compare the tag that was
4929attached to the object with one owned by the addon to ensure that the object
4930has the right type.
4931
4932If the object already has an associated type tag, this API will return
4933`napi_invalid_arg`.
4934
4935### napi_check_object_type_tag
4936<!-- YAML
4937added:
4938  - v14.8.0
4939  - v12.19.0
4940napiVersion: 8
4941-->
4942
4943```c
4944napi_status napi_check_object_type_tag(napi_env env,
4945                                       napi_value js_object,
4946                                       const napi_type_tag* type_tag,
4947                                       bool* result);
4948```
4949
4950* `[in] env`: The environment that the API is invoked under.
4951* `[in] js_object`: The JavaScript object whose type tag to examine.
4952* `[in] type_tag`: The tag with which to compare any tag found on the object.
4953* `[out] result`: Whether the type tag given matched the type tag on the
4954  object. `false` is also returned if no type tag was found on the object.
4955
4956Returns `napi_ok` if the API succeeded.
4957
4958Compares the pointer given as `type_tag` with any that can be found on
4959`js_object`. If no tag is found on `js_object` or, if a tag is found but it does
4960not match `type_tag`, then `result` is set to `false`. If a tag is found and it
4961matches `type_tag`, then `result` is set to `true`.
4962
4963### napi_add_finalizer
4964
4965<!-- YAML
4966added: v8.0.0
4967napiVersion: 5
4968-->
4969
4970```c
4971napi_status napi_add_finalizer(napi_env env,
4972                               napi_value js_object,
4973                               void* native_object,
4974                               napi_finalize finalize_cb,
4975                               void* finalize_hint,
4976                               napi_ref* result);
4977```
4978
4979* `[in] env`: The environment that the API is invoked under.
4980* `[in] js_object`: The JavaScript object to which the native data will be
4981  attached.
4982* `[in] native_object`: The native data that will be attached to the JavaScript
4983  object.
4984* `[in] finalize_cb`: Native callback that will be used to free the
4985  native data when the JavaScript object is ready for garbage-collection.
4986  [`napi_finalize`][] provides more details.
4987* `[in] finalize_hint`: Optional contextual hint that is passed to the
4988  finalize callback.
4989* `[out] result`: Optional reference to the JavaScript object.
4990
4991Returns `napi_ok` if the API succeeded.
4992
4993Adds a `napi_finalize` callback which will be called when the JavaScript object
4994in `js_object` is ready for garbage collection. This API is similar to
4995`napi_wrap()` except that:
4996
4997* the native data cannot be retrieved later using `napi_unwrap()`,
4998* nor can it be removed later using `napi_remove_wrap()`, and
4999* the API can be called multiple times with different data items in order to
5000  attach each of them to the JavaScript object, and
5001* the object manipulated by the API can be used with `napi_wrap()`.
5002
5003*Caution*: The optional returned reference (if obtained) should be deleted via
5004[`napi_delete_reference`][] ONLY in response to the finalize callback
5005invocation. If it is deleted before then, then the finalize callback may never
5006be invoked. Therefore, when obtaining a reference a finalize callback is also
5007required in order to enable correct disposal of the reference.
5008
5009## Simple asynchronous operations
5010
5011Addon modules often need to leverage async helpers from libuv as part of their
5012implementation. This allows them to schedule work to be executed asynchronously
5013so that their methods can return in advance of the work being completed. This
5014allows them to avoid blocking overall execution of the Node.js application.
5015
5016Node-API provides an ABI-stable interface for these
5017supporting functions which covers the most common asynchronous use cases.
5018
5019Node-API defines the `napi_async_work` structure which is used to manage
5020asynchronous workers. Instances are created/deleted with
5021[`napi_create_async_work`][] and [`napi_delete_async_work`][].
5022
5023The `execute` and `complete` callbacks are functions that will be
5024invoked when the executor is ready to execute and when it completes its
5025task respectively.
5026
5027The `execute` function should avoid making any Node-API calls
5028that could result in the execution of JavaScript or interaction with
5029JavaScript objects. Most often, any code that needs to make Node-API
5030calls should be made in `complete` callback instead.
5031Avoid using the `napi_env` parameter in the execute callback as
5032it will likely execute JavaScript.
5033
5034These functions implement the following interfaces:
5035
5036```c
5037typedef void (*napi_async_execute_callback)(napi_env env,
5038                                            void* data);
5039typedef void (*napi_async_complete_callback)(napi_env env,
5040                                             napi_status status,
5041                                             void* data);
5042```
5043
5044When these methods are invoked, the `data` parameter passed will be the
5045addon-provided `void*` data that was passed into the
5046`napi_create_async_work` call.
5047
5048Once created the async worker can be queued
5049for execution using the [`napi_queue_async_work`][] function:
5050
5051```c
5052napi_status napi_queue_async_work(napi_env env,
5053                                  napi_async_work work);
5054```
5055
5056[`napi_cancel_async_work`][] can be used if the work needs
5057to be cancelled before the work has started execution.
5058
5059After calling [`napi_cancel_async_work`][], the `complete` callback
5060will be invoked with a status value of `napi_cancelled`.
5061The work should not be deleted before the `complete`
5062callback invocation, even when it was cancelled.
5063
5064### napi_create_async_work
5065<!-- YAML
5066added: v8.0.0
5067napiVersion: 1
5068changes:
5069  - version: v8.6.0
5070    pr-url: https://github.com/nodejs/node/pull/14697
5071    description: Added `async_resource` and `async_resource_name` parameters.
5072-->
5073
5074```c
5075napi_status napi_create_async_work(napi_env env,
5076                                   napi_value async_resource,
5077                                   napi_value async_resource_name,
5078                                   napi_async_execute_callback execute,
5079                                   napi_async_complete_callback complete,
5080                                   void* data,
5081                                   napi_async_work* result);
5082```
5083
5084* `[in] env`: The environment that the API is invoked under.
5085* `[in] async_resource`: An optional object associated with the async work
5086  that will be passed to possible `async_hooks` [`init` hooks][].
5087* `[in] async_resource_name`: Identifier for the kind of resource that is being
5088  provided for diagnostic information exposed by the `async_hooks` API.
5089* `[in] execute`: The native function which should be called to execute the
5090  logic asynchronously. The given function is called from a worker pool thread
5091  and can execute in parallel with the main event loop thread.
5092* `[in] complete`: The native function which will be called when the
5093  asynchronous logic is completed or is cancelled. The given function is called
5094  from the main event loop thread. [`napi_async_complete_callback`][] provides
5095  more details.
5096* `[in] data`: User-provided data context. This will be passed back into the
5097  execute and complete functions.
5098* `[out] result`: `napi_async_work*` which is the handle to the newly created
5099  async work.
5100
5101Returns `napi_ok` if the API succeeded.
5102
5103This API allocates a work object that is used to execute logic asynchronously.
5104It should be freed using [`napi_delete_async_work`][] once the work is no longer
5105required.
5106
5107`async_resource_name` should be a null-terminated, UTF-8-encoded string.
5108
5109The `async_resource_name` identifier is provided by the user and should be
5110representative of the type of async work being performed. It is also recommended
5111to apply namespacing to the identifier, e.g. by including the module name. See
5112the [`async_hooks` documentation][async_hooks `type`] for more information.
5113
5114### napi_delete_async_work
5115<!-- YAML
5116added: v8.0.0
5117napiVersion: 1
5118-->
5119
5120```c
5121napi_status napi_delete_async_work(napi_env env,
5122                                   napi_async_work work);
5123```
5124
5125* `[in] env`: The environment that the API is invoked under.
5126* `[in] work`: The handle returned by the call to `napi_create_async_work`.
5127
5128Returns `napi_ok` if the API succeeded.
5129
5130This API frees a previously allocated work object.
5131
5132This API can be called even if there is a pending JavaScript exception.
5133
5134### napi_queue_async_work
5135<!-- YAML
5136added: v8.0.0
5137napiVersion: 1
5138-->
5139
5140```c
5141napi_status napi_queue_async_work(napi_env env,
5142                                  napi_async_work work);
5143```
5144
5145* `[in] env`: The environment that the API is invoked under.
5146* `[in] work`: The handle returned by the call to `napi_create_async_work`.
5147
5148Returns `napi_ok` if the API succeeded.
5149
5150This API requests that the previously allocated work be scheduled
5151for execution. Once it returns successfully, this API must not be called again
5152with the same `napi_async_work` item or the result will be undefined.
5153
5154### napi_cancel_async_work
5155<!-- YAML
5156added: v8.0.0
5157napiVersion: 1
5158-->
5159
5160```c
5161napi_status napi_cancel_async_work(napi_env env,
5162                                   napi_async_work work);
5163```
5164
5165* `[in] env`: The environment that the API is invoked under.
5166* `[in] work`: The handle returned by the call to `napi_create_async_work`.
5167
5168Returns `napi_ok` if the API succeeded.
5169
5170This API cancels queued work if it has not yet
5171been started. If it has already started executing, it cannot be
5172cancelled and `napi_generic_failure` will be returned. If successful,
5173the `complete` callback will be invoked with a status value of
5174`napi_cancelled`. The work should not be deleted before the `complete`
5175callback invocation, even if it has been successfully cancelled.
5176
5177This API can be called even if there is a pending JavaScript exception.
5178
5179## Custom asynchronous operations
5180
5181The simple asynchronous work APIs above may not be appropriate for every
5182scenario. When using any other asynchronous mechanism, the following APIs
5183are necessary to ensure an asynchronous operation is properly tracked by
5184the runtime.
5185
5186### napi_async_init
5187<!-- YAML
5188added: v8.6.0
5189napiVersion: 1
5190-->
5191
5192```c
5193napi_status napi_async_init(napi_env env,
5194                            napi_value async_resource,
5195                            napi_value async_resource_name,
5196                            napi_async_context* result)
5197```
5198
5199* `[in] env`: The environment that the API is invoked under.
5200* `[in] async_resource`: Object associated with the async work
5201  that will be passed to possible `async_hooks` [`init` hooks][] and can be
5202  accessed by [`async_hooks.executionAsyncResource()`][].
5203* `[in] async_resource_name`: Identifier for the kind of resource that is being
5204  provided for diagnostic information exposed by the `async_hooks` API.
5205* `[out] result`: The initialized async context.
5206
5207Returns `napi_ok` if the API succeeded.
5208
5209The `async_resource` object needs to be kept alive until
5210[`napi_async_destroy`][] to keep `async_hooks` related API acts correctly. In
5211order to retain ABI compatibility with previous versions, `napi_async_context`s
5212are not maintaining the strong reference to the `async_resource` objects to
5213avoid introducing causing memory leaks. However, if the `async_resource` is
5214garbage collected by JavaScript engine before the `napi_async_context` was
5215destroyed by `napi_async_destroy`, calling `napi_async_context` related APIs
5216like [`napi_open_callback_scope`][] and [`napi_make_callback`][] can cause
5217problems like loss of async context when using the `AsyncLocalStoage` API.
5218
5219In order to retain ABI compatibility with previous versions, passing `NULL`
5220for `async_resource` does not result in an error. However, this is not
5221recommended as this will result poor results with  `async_hooks`
5222[`init` hooks][] and `async_hooks.executionAsyncResource()` as the resource is
5223now required by the underlying `async_hooks` implementation in order to provide
5224the linkage between async callbacks.
5225
5226### napi_async_destroy
5227<!-- YAML
5228added: v8.6.0
5229napiVersion: 1
5230-->
5231
5232```c
5233napi_status napi_async_destroy(napi_env env,
5234                               napi_async_context async_context);
5235```
5236
5237* `[in] env`: The environment that the API is invoked under.
5238* `[in] async_context`: The async context to be destroyed.
5239
5240Returns `napi_ok` if the API succeeded.
5241
5242This API can be called even if there is a pending JavaScript exception.
5243
5244### napi_make_callback
5245<!-- YAML
5246added: v8.0.0
5247napiVersion: 1
5248changes:
5249  - version: v8.6.0
5250    pr-url: https://github.com/nodejs/node/pull/15189
5251    description: Added `async_context` parameter.
5252-->
5253
5254```c
5255NAPI_EXTERN napi_status napi_make_callback(napi_env env,
5256                                           napi_async_context async_context,
5257                                           napi_value recv,
5258                                           napi_value func,
5259                                           size_t argc,
5260                                           const napi_value* argv,
5261                                           napi_value* result);
5262```
5263
5264* `[in] env`: The environment that the API is invoked under.
5265* `[in] async_context`: Context for the async operation that is
5266   invoking the callback. This should normally be a value previously
5267   obtained from [`napi_async_init`][].
5268   In order to retain ABI compatibility with previous versions, passing `NULL`
5269   for `async_context` does not result in an error. However, this results
5270   in incorrect operation of async hooks. Potential issues include loss of
5271   async context when using the `AsyncLocalStorage` API.
5272* `[in] recv`: The `this` value passed to the called function.
5273* `[in] func`: `napi_value` representing the JavaScript function to be invoked.
5274* `[in] argc`: The count of elements in the `argv` array.
5275* `[in] argv`: Array of JavaScript values as `napi_value` representing the
5276  arguments to the function.
5277* `[out] result`: `napi_value` representing the JavaScript object returned.
5278
5279Returns `napi_ok` if the API succeeded.
5280
5281This method allows a JavaScript function object to be called from a native
5282add-on. This API is similar to `napi_call_function`. However, it is used to call
5283*from* native code back *into* JavaScript *after* returning from an async
5284operation (when there is no other script on the stack). It is a fairly simple
5285wrapper around `node::MakeCallback`.
5286
5287Note it is *not* necessary to use `napi_make_callback` from within a
5288`napi_async_complete_callback`; in that situation the callback's async
5289context has already been set up, so a direct call to `napi_call_function`
5290is sufficient and appropriate. Use of the `napi_make_callback` function
5291may be required when implementing custom async behavior that does not use
5292`napi_create_async_work`.
5293
5294Any `process.nextTick`s or Promises scheduled on the microtask queue by
5295JavaScript during the callback are ran before returning back to C/C++.
5296
5297### napi_open_callback_scope
5298<!-- YAML
5299added: v9.6.0
5300napiVersion: 3
5301-->
5302
5303```c
5304NAPI_EXTERN napi_status napi_open_callback_scope(napi_env env,
5305                                                 napi_value resource_object,
5306                                                 napi_async_context context,
5307                                                 napi_callback_scope* result)
5308```
5309
5310* `[in] env`: The environment that the API is invoked under.
5311* `[in] resource_object`: An object associated with the async work
5312  that will be passed to possible `async_hooks` [`init` hooks][]. This
5313  parameter has been deprecated and is ignored at runtime. Use the
5314  `async_resource` parameter in [`napi_async_init`][] instead.
5315* `[in] context`: Context for the async operation that is invoking the callback.
5316  This should be a value previously obtained from [`napi_async_init`][].
5317* `[out] result`: The newly created scope.
5318
5319There are cases (for example, resolving promises) where it is
5320necessary to have the equivalent of the scope associated with a callback
5321in place when making certain Node-API calls. If there is no other script on
5322the stack the [`napi_open_callback_scope`][] and
5323[`napi_close_callback_scope`][] functions can be used to open/close
5324the required scope.
5325
5326### napi_close_callback_scope
5327<!-- YAML
5328added: v9.6.0
5329napiVersion: 3
5330-->
5331
5332```c
5333NAPI_EXTERN napi_status napi_close_callback_scope(napi_env env,
5334                                                  napi_callback_scope scope)
5335```
5336
5337* `[in] env`: The environment that the API is invoked under.
5338* `[in] scope`: The scope to be closed.
5339
5340This API can be called even if there is a pending JavaScript exception.
5341
5342## Version management
5343
5344### napi_get_node_version
5345<!-- YAML
5346added: v8.4.0
5347napiVersion: 1
5348-->
5349
5350```c
5351typedef struct {
5352  uint32_t major;
5353  uint32_t minor;
5354  uint32_t patch;
5355  const char* release;
5356} napi_node_version;
5357
5358napi_status napi_get_node_version(napi_env env,
5359                                  const napi_node_version** version);
5360```
5361
5362* `[in] env`: The environment that the API is invoked under.
5363* `[out] version`: A pointer to version information for Node.js itself.
5364
5365Returns `napi_ok` if the API succeeded.
5366
5367This function fills the `version` struct with the major, minor, and patch
5368version of Node.js that is currently running, and the `release` field with the
5369value of [`process.release.name`][`process.release`].
5370
5371The returned buffer is statically allocated and does not need to be freed.
5372
5373### napi_get_version
5374<!-- YAML
5375added: v8.0.0
5376napiVersion: 1
5377-->
5378
5379```c
5380napi_status napi_get_version(napi_env env,
5381                             uint32_t* result);
5382```
5383
5384* `[in] env`: The environment that the API is invoked under.
5385* `[out] result`: The highest version of Node-API supported.
5386
5387Returns `napi_ok` if the API succeeded.
5388
5389This API returns the highest Node-API version supported by the
5390Node.js runtime. Node-API is planned to be additive such that
5391newer releases of Node.js may support additional API functions.
5392In order to allow an addon to use a newer function when running with
5393versions of Node.js that support it, while providing
5394fallback behavior when running with Node.js versions that don't
5395support it:
5396
5397* Call `napi_get_version()` to determine if the API is available.
5398* If available, dynamically load a pointer to the function using `uv_dlsym()`.
5399* Use the dynamically loaded pointer to invoke the function.
5400* If the function is not available, provide an alternate implementation
5401  that does not use the function.
5402
5403## Memory management
5404
5405### napi_adjust_external_memory
5406<!-- YAML
5407added: v8.5.0
5408napiVersion: 1
5409-->
5410
5411```c
5412NAPI_EXTERN napi_status napi_adjust_external_memory(napi_env env,
5413                                                    int64_t change_in_bytes,
5414                                                    int64_t* result);
5415```
5416
5417* `[in] env`: The environment that the API is invoked under.
5418* `[in] change_in_bytes`: The change in externally allocated memory that is kept
5419  alive by JavaScript objects.
5420* `[out] result`: The adjusted value
5421
5422Returns `napi_ok` if the API succeeded.
5423
5424This function gives V8 an indication of the amount of externally allocated
5425memory that is kept alive by JavaScript objects (i.e. a JavaScript object
5426that points to its own memory allocated by a native module). Registering
5427externally allocated memory will trigger global garbage collections more
5428often than it would otherwise.
5429
5430## Promises
5431
5432Node-API provides facilities for creating `Promise` objects as described in
5433[Section 25.4][] of the ECMA specification. It implements promises as a pair of
5434objects. When a promise is created by `napi_create_promise()`, a "deferred"
5435object is created and returned alongside the `Promise`. The deferred object is
5436bound to the created `Promise` and is the only means to resolve or reject the
5437`Promise` using `napi_resolve_deferred()` or `napi_reject_deferred()`. The
5438deferred object that is created by `napi_create_promise()` is freed by
5439`napi_resolve_deferred()` or `napi_reject_deferred()`. The `Promise` object may
5440be returned to JavaScript where it can be used in the usual fashion.
5441
5442For example, to create a promise and pass it to an asynchronous worker:
5443
5444```c
5445napi_deferred deferred;
5446napi_value promise;
5447napi_status status;
5448
5449// Create the promise.
5450status = napi_create_promise(env, &deferred, &promise);
5451if (status != napi_ok) return NULL;
5452
5453// Pass the deferred to a function that performs an asynchronous action.
5454do_something_asynchronous(deferred);
5455
5456// Return the promise to JS
5457return promise;
5458```
5459
5460The above function `do_something_asynchronous()` would perform its asynchronous
5461action and then it would resolve or reject the deferred, thereby concluding the
5462promise and freeing the deferred:
5463
5464```c
5465napi_deferred deferred;
5466napi_value undefined;
5467napi_status status;
5468
5469// Create a value with which to conclude the deferred.
5470status = napi_get_undefined(env, &undefined);
5471if (status != napi_ok) return NULL;
5472
5473// Resolve or reject the promise associated with the deferred depending on
5474// whether the asynchronous action succeeded.
5475if (asynchronous_action_succeeded) {
5476  status = napi_resolve_deferred(env, deferred, undefined);
5477} else {
5478  status = napi_reject_deferred(env, deferred, undefined);
5479}
5480if (status != napi_ok) return NULL;
5481
5482// At this point the deferred has been freed, so we should assign NULL to it.
5483deferred = NULL;
5484```
5485
5486### napi_create_promise
5487<!-- YAML
5488added: v8.5.0
5489napiVersion: 1
5490-->
5491
5492```c
5493napi_status napi_create_promise(napi_env env,
5494                                napi_deferred* deferred,
5495                                napi_value* promise);
5496```
5497
5498* `[in] env`: The environment that the API is invoked under.
5499* `[out] deferred`: A newly created deferred object which can later be passed to
5500  `napi_resolve_deferred()` or `napi_reject_deferred()` to resolve resp. reject
5501  the associated promise.
5502* `[out] promise`: The JavaScript promise associated with the deferred object.
5503
5504Returns `napi_ok` if the API succeeded.
5505
5506This API creates a deferred object and a JavaScript promise.
5507
5508### napi_resolve_deferred
5509<!-- YAML
5510added: v8.5.0
5511napiVersion: 1
5512-->
5513
5514```c
5515napi_status napi_resolve_deferred(napi_env env,
5516                                  napi_deferred deferred,
5517                                  napi_value resolution);
5518```
5519
5520* `[in] env`: The environment that the API is invoked under.
5521* `[in] deferred`: The deferred object whose associated promise to resolve.
5522* `[in] resolution`: The value with which to resolve the promise.
5523
5524This API resolves a JavaScript promise by way of the deferred object
5525with which it is associated. Thus, it can only be used to resolve JavaScript
5526promises for which the corresponding deferred object is available. This
5527effectively means that the promise must have been created using
5528`napi_create_promise()` and the deferred object returned from that call must
5529have been retained in order to be passed to this API.
5530
5531The deferred object is freed upon successful completion.
5532
5533### napi_reject_deferred
5534<!-- YAML
5535added: v8.5.0
5536napiVersion: 1
5537-->
5538
5539```c
5540napi_status napi_reject_deferred(napi_env env,
5541                                 napi_deferred deferred,
5542                                 napi_value rejection);
5543```
5544
5545* `[in] env`: The environment that the API is invoked under.
5546* `[in] deferred`: The deferred object whose associated promise to resolve.
5547* `[in] rejection`: The value with which to reject the promise.
5548
5549This API rejects a JavaScript promise by way of the deferred object
5550with which it is associated. Thus, it can only be used to reject JavaScript
5551promises for which the corresponding deferred object is available. This
5552effectively means that the promise must have been created using
5553`napi_create_promise()` and the deferred object returned from that call must
5554have been retained in order to be passed to this API.
5555
5556The deferred object is freed upon successful completion.
5557
5558### napi_is_promise
5559<!-- YAML
5560added: v8.5.0
5561napiVersion: 1
5562-->
5563
5564```c
5565napi_status napi_is_promise(napi_env env,
5566                            napi_value value,
5567                            bool* is_promise);
5568```
5569
5570* `[in] env`: The environment that the API is invoked under.
5571* `[in] value`: The value to examine
5572* `[out] is_promise`: Flag indicating whether `promise` is a native promise
5573  object (that is, a promise object created by the underlying engine).
5574
5575## Script execution
5576
5577Node-API provides an API for executing a string containing JavaScript using the
5578underlying JavaScript engine.
5579
5580### napi_run_script
5581<!-- YAML
5582added: v8.5.0
5583napiVersion: 1
5584-->
5585
5586```c
5587NAPI_EXTERN napi_status napi_run_script(napi_env env,
5588                                        napi_value script,
5589                                        napi_value* result);
5590```
5591
5592* `[in] env`: The environment that the API is invoked under.
5593* `[in] script`: A JavaScript string containing the script to execute.
5594* `[out] result`: The value resulting from having executed the script.
5595
5596This function executes a string of JavaScript code and returns its result with
5597the following caveats:
5598
5599* Unlike `eval`, this function does not allow the script to access the current
5600  lexical scope, and therefore also does not allow to access the
5601  [module scope][], meaning that pseudo-globals such as `require` will not be
5602  available.
5603* The script can access the [global scope][]. Function and `var` declarations
5604  in the script will be added to the [`global`][] object. Variable declarations
5605  made using `let` and `const` will be visible globally, but will not be added
5606  to the [`global`][] object.
5607* The value of `this` is [`global`][] within the script.
5608
5609## libuv event loop
5610
5611Node-API provides a function for getting the current event loop associated with
5612a specific `napi_env`.
5613
5614### napi_get_uv_event_loop
5615<!-- YAML
5616added:
5617  - v9.3.0
5618  - v8.10.0
5619napiVersion: 2
5620-->
5621
5622```c
5623NAPI_EXTERN napi_status napi_get_uv_event_loop(napi_env env,
5624                                               struct uv_loop_s** loop);
5625```
5626
5627* `[in] env`: The environment that the API is invoked under.
5628* `[out] loop`: The current libuv loop instance.
5629
5630## Asynchronous thread-safe function calls
5631
5632JavaScript functions can normally only be called from a native addon's main
5633thread. If an addon creates additional threads, then Node-API functions that
5634require a `napi_env`, `napi_value`, or `napi_ref` must not be called from those
5635threads.
5636
5637When an addon has additional threads and JavaScript functions need to be invoked
5638based on the processing completed by those threads, those threads must
5639communicate with the addon's main thread so that the main thread can invoke the
5640JavaScript function on their behalf. The thread-safe function APIs provide an
5641easy way to do this.
5642
5643These APIs provide the type `napi_threadsafe_function` as well as APIs to
5644create, destroy, and call objects of this type.
5645`napi_create_threadsafe_function()` creates a persistent reference to a
5646`napi_value` that holds a JavaScript function which can be called from multiple
5647threads. The calls happen asynchronously. This means that values with which the
5648JavaScript callback is to be called will be placed in a queue, and, for each
5649value in the queue, a call will eventually be made to the JavaScript function.
5650
5651Upon creation of a `napi_threadsafe_function` a `napi_finalize` callback can be
5652provided. This callback will be invoked on the main thread when the thread-safe
5653function is about to be destroyed. It receives the context and the finalize data
5654given during construction, and provides an opportunity for cleaning up after the
5655threads e.g. by calling `uv_thread_join()`. **Aside from the main loop thread,
5656no threads should be using the thread-safe function after the finalize callback
5657completes.**
5658
5659The `context` given during the call to `napi_create_threadsafe_function()` can
5660be retrieved from any thread with a call to
5661`napi_get_threadsafe_function_context()`.
5662
5663### Calling a thread-safe function
5664
5665`napi_call_threadsafe_function()` can be used for initiating a call into
5666JavaScript. `napi_call_threadsafe_function()` accepts a parameter which controls
5667whether the API behaves blockingly. If set to `napi_tsfn_nonblocking`, the API
5668behaves non-blockingly, returning `napi_queue_full` if the queue was full,
5669preventing data from being successfully added to the queue. If set to
5670`napi_tsfn_blocking`, the API blocks until space becomes available in the queue.
5671`napi_call_threadsafe_function()` never blocks if the thread-safe function was
5672created with a maximum queue size of 0.
5673
5674`napi_call_threadsafe_function()` should not be called with `napi_tsfn_blocking`
5675from a JavaScript thread, because, if the queue is full, it may cause the
5676JavaScript thread to deadlock.
5677
5678The actual call into JavaScript is controlled by the callback given via the
5679`call_js_cb` parameter. `call_js_cb` is invoked on the main thread once for each
5680value that was placed into the queue by a successful call to
5681`napi_call_threadsafe_function()`. If such a callback is not given, a default
5682callback will be used, and the resulting JavaScript call will have no arguments.
5683The `call_js_cb` callback receives the JavaScript function to call as a
5684`napi_value` in its parameters, as well as the `void*` context pointer used when
5685creating the `napi_threadsafe_function`, and the next data pointer that was
5686created by one of the secondary threads. The callback can then use an API such
5687as `napi_call_function()` to call into JavaScript.
5688
5689The callback may also be invoked with `env` and `call_js_cb` both set to `NULL`
5690to indicate that calls into JavaScript are no longer possible, while items
5691remain in the queue that may need to be freed. This normally occurs when the
5692Node.js process exits while there is a thread-safe function still active.
5693
5694It is not necessary to call into JavaScript via `napi_make_callback()` because
5695Node-API runs `call_js_cb` in a context appropriate for callbacks.
5696
5697### Reference counting of thread-safe functions
5698
5699Threads can be added to and removed from a `napi_threadsafe_function` object
5700during its existence. Thus, in addition to specifying an initial number of
5701threads upon creation, `napi_acquire_threadsafe_function` can be called to
5702indicate that a new thread will start making use of the thread-safe function.
5703Similarly, `napi_release_threadsafe_function` can be called to indicate that an
5704existing thread will stop making use of the thread-safe function.
5705
5706`napi_threadsafe_function` objects are destroyed when every thread which uses
5707the object has called `napi_release_threadsafe_function()` or has received a
5708return status of `napi_closing` in response to a call to
5709`napi_call_threadsafe_function`. The queue is emptied before the
5710`napi_threadsafe_function` is destroyed. `napi_release_threadsafe_function()`
5711should be the last API call made in conjunction with a given
5712`napi_threadsafe_function`, because after the call completes, there is no
5713guarantee that the `napi_threadsafe_function` is still allocated. For the same
5714reason, do not use a thread-safe function
5715after receiving a return value of `napi_closing` in response to a call to
5716`napi_call_threadsafe_function`. Data associated with the
5717`napi_threadsafe_function` can be freed in its `napi_finalize` callback which
5718was passed to `napi_create_threadsafe_function()`. The parameter
5719`initial_thread_count` of `napi_create_threadsafe_function` marks the initial
5720number of aquisitions of the thread-safe functions, instead of calling
5721`napi_acquire_threadsafe_function` multiple times at creation.
5722
5723Once the number of threads making use of a `napi_threadsafe_function` reaches
5724zero, no further threads can start making use of it by calling
5725`napi_acquire_threadsafe_function()`. In fact, all subsequent API calls
5726associated with it, except `napi_release_threadsafe_function()`, will return an
5727error value of `napi_closing`.
5728
5729The thread-safe function can be "aborted" by giving a value of `napi_tsfn_abort`
5730to `napi_release_threadsafe_function()`. This will cause all subsequent APIs
5731associated with the thread-safe function except
5732`napi_release_threadsafe_function()` to return `napi_closing` even before its
5733reference count reaches zero. In particular, `napi_call_threadsafe_function()`
5734will return `napi_closing`, thus informing the threads that it is no longer
5735possible to make asynchronous calls to the thread-safe function. This can be
5736used as a criterion for terminating the thread. **Upon receiving a return value
5737of `napi_closing` from `napi_call_threadsafe_function()` a thread must not use
5738the thread-safe function anymore because it is no longer guaranteed to
5739be allocated.**
5740
5741### Deciding whether to keep the process running
5742
5743Similarly to libuv handles, thread-safe functions can be "referenced" and
5744"unreferenced". A "referenced" thread-safe function will cause the event loop on
5745the thread on which it is created to remain alive until the thread-safe function
5746is destroyed. In contrast, an "unreferenced" thread-safe function will not
5747prevent the event loop from exiting. The APIs `napi_ref_threadsafe_function` and
5748`napi_unref_threadsafe_function` exist for this purpose.
5749
5750Neither does `napi_unref_threadsafe_function` mark the thread-safe functions as
5751able to be destroyed nor does `napi_ref_threadsafe_function` prevent it from
5752being destroyed.
5753
5754### napi_create_threadsafe_function
5755
5756<!-- YAML
5757added: v10.6.0
5758napiVersion: 4
5759changes:
5760  - version:
5761     - v12.6.0
5762     - v10.17.0
5763    pr-url: https://github.com/nodejs/node/pull/27791
5764    description: Made `func` parameter optional with custom `call_js_cb`.
5765-->
5766
5767```c
5768NAPI_EXTERN napi_status
5769napi_create_threadsafe_function(napi_env env,
5770                                napi_value func,
5771                                napi_value async_resource,
5772                                napi_value async_resource_name,
5773                                size_t max_queue_size,
5774                                size_t initial_thread_count,
5775                                void* thread_finalize_data,
5776                                napi_finalize thread_finalize_cb,
5777                                void* context,
5778                                napi_threadsafe_function_call_js call_js_cb,
5779                                napi_threadsafe_function* result);
5780```
5781
5782* `[in] env`: The environment that the API is invoked under.
5783* `[in] func`: An optional JavaScript function to call from another thread. It
5784  must be provided if `NULL` is passed to `call_js_cb`.
5785* `[in] async_resource`: An optional object associated with the async work that
5786  will be passed to possible `async_hooks` [`init` hooks][].
5787* `[in] async_resource_name`: A JavaScript string to provide an identifier for
5788  the kind of resource that is being provided for diagnostic information exposed
5789  by the `async_hooks` API.
5790* `[in] max_queue_size`: Maximum size of the queue. `0` for no limit.
5791* `[in] initial_thread_count`: The initial number of acquisitions, i.e. the
5792  initial number of threads, including the main thread, which will be making use
5793  of this function.
5794* `[in] thread_finalize_data`: Optional data to be passed to `thread_finalize_cb`.
5795* `[in] thread_finalize_cb`: Optional function to call when the
5796  `napi_threadsafe_function` is being destroyed.
5797* `[in] context`: Optional data to attach to the resulting
5798  `napi_threadsafe_function`.
5799* `[in] call_js_cb`: Optional callback which calls the JavaScript function in
5800  response to a call on a different thread. This callback will be called on the
5801  main thread. If not given, the JavaScript function will be called with no
5802  parameters and with `undefined` as its `this` value.
5803  [`napi_threadsafe_function_call_js`][] provides more details.
5804* `[out] result`: The asynchronous thread-safe JavaScript function.
5805
5806### napi_get_threadsafe_function_context
5807
5808<!-- YAML
5809added: v10.6.0
5810napiVersion: 4
5811-->
5812
5813```c
5814NAPI_EXTERN napi_status
5815napi_get_threadsafe_function_context(napi_threadsafe_function func,
5816                                     void** result);
5817```
5818
5819* `[in] func`: The thread-safe function for which to retrieve the context.
5820* `[out] result`: The location where to store the context.
5821
5822This API may be called from any thread which makes use of `func`.
5823
5824### napi_call_threadsafe_function
5825
5826<!-- YAML
5827added: v10.6.0
5828napiVersion: 4
5829changes:
5830  - version: v14.5.0
5831    pr-url: https://github.com/nodejs/node/pull/33453
5832    description: Support for `napi_would_deadlock` has been reverted.
5833  - version: v14.1.0
5834    pr-url: https://github.com/nodejs/node/pull/32689
5835    description: Return `napi_would_deadlock` when called with
5836                 `napi_tsfn_blocking` from the main thread or a worker thread
5837                 and the queue is full.
5838-->
5839
5840```c
5841NAPI_EXTERN napi_status
5842napi_call_threadsafe_function(napi_threadsafe_function func,
5843                              void* data,
5844                              napi_threadsafe_function_call_mode is_blocking);
5845```
5846
5847* `[in] func`: The asynchronous thread-safe JavaScript function to invoke.
5848* `[in] data`: Data to send into JavaScript via the callback `call_js_cb`
5849  provided during the creation of the thread-safe JavaScript function.
5850* `[in] is_blocking`: Flag whose value can be either `napi_tsfn_blocking` to
5851  indicate that the call should block if the queue is full or
5852  `napi_tsfn_nonblocking` to indicate that the call should return immediately
5853  with a status of `napi_queue_full` whenever the queue is full.
5854
5855This API should not be called with `napi_tsfn_blocking` from a JavaScript
5856thread, because, if the queue is full, it may cause the JavaScript thread to
5857deadlock.
5858
5859This API will return `napi_closing` if `napi_release_threadsafe_function()` was
5860called with `abort` set to `napi_tsfn_abort` from any thread. The value is only
5861added to the queue if the API returns `napi_ok`.
5862
5863This API may be called from any thread which makes use of `func`.
5864
5865### napi_acquire_threadsafe_function
5866
5867<!-- YAML
5868added: v10.6.0
5869napiVersion: 4
5870-->
5871
5872```c
5873NAPI_EXTERN napi_status
5874napi_acquire_threadsafe_function(napi_threadsafe_function func);
5875```
5876
5877* `[in] func`: The asynchronous thread-safe JavaScript function to start making
5878  use of.
5879
5880A thread should call this API before passing `func` to any other thread-safe
5881function APIs to indicate that it will be making use of `func`. This prevents
5882`func` from being destroyed when all other threads have stopped making use of
5883it.
5884
5885This API may be called from any thread which will start making use of `func`.
5886
5887### napi_release_threadsafe_function
5888
5889<!-- YAML
5890added: v10.6.0
5891napiVersion: 4
5892-->
5893
5894```c
5895NAPI_EXTERN napi_status
5896napi_release_threadsafe_function(napi_threadsafe_function func,
5897                                 napi_threadsafe_function_release_mode mode);
5898```
5899
5900* `[in] func`: The asynchronous thread-safe JavaScript function whose reference
5901  count to decrement.
5902* `[in] mode`: Flag whose value can be either `napi_tsfn_release` to indicate
5903  that the current thread will make no further calls to the thread-safe
5904  function, or `napi_tsfn_abort` to indicate that in addition to the current
5905  thread, no other thread should make any further calls to the thread-safe
5906  function. If set to `napi_tsfn_abort`, further calls to
5907  `napi_call_threadsafe_function()` will return `napi_closing`, and no further
5908  values will be placed in the queue.
5909
5910A thread should call this API when it stops making use of `func`. Passing `func`
5911to any thread-safe APIs after having called this API has undefined results, as
5912`func` may have been destroyed.
5913
5914This API may be called from any thread which will stop making use of `func`.
5915
5916### napi_ref_threadsafe_function
5917
5918<!-- YAML
5919added: v10.6.0
5920napiVersion: 4
5921-->
5922
5923```c
5924NAPI_EXTERN napi_status
5925napi_ref_threadsafe_function(napi_env env, napi_threadsafe_function func);
5926```
5927
5928* `[in] env`: The environment that the API is invoked under.
5929* `[in] func`: The thread-safe function to reference.
5930
5931This API is used to indicate that the event loop running on the main thread
5932should not exit until `func` has been destroyed. Similar to [`uv_ref`][] it is
5933also idempotent.
5934
5935Neither does `napi_unref_threadsafe_function` mark the thread-safe functions as
5936able to be destroyed nor does `napi_ref_threadsafe_function` prevent it from
5937being destroyed. `napi_acquire_threadsafe_function` and
5938`napi_release_threadsafe_function` are available for that purpose.
5939
5940This API may only be called from the main thread.
5941
5942### napi_unref_threadsafe_function
5943
5944<!-- YAML
5945added: v10.6.0
5946napiVersion: 4
5947-->
5948
5949```c
5950NAPI_EXTERN napi_status
5951napi_unref_threadsafe_function(napi_env env, napi_threadsafe_function func);
5952```
5953
5954* `[in] env`: The environment that the API is invoked under.
5955* `[in] func`: The thread-safe function to unreference.
5956
5957This API is used to indicate that the event loop running on the main thread
5958may exit before `func` is destroyed. Similar to [`uv_unref`][] it is also
5959idempotent.
5960
5961This API may only be called from the main thread.
5962
5963## Miscellaneous utilities
5964
5965## node_api_get_module_file_name
5966
5967<!-- YAML
5968added: v14.18.0
5969-->
5970
5971> Stability: 1 - Experimental
5972
5973```c
5974NAPI_EXTERN napi_status
5975node_api_get_module_file_name(napi_env env, const char** result);
5976
5977```
5978
5979* `[in] env`: The environment that the API is invoked under.
5980* `[out] result`: A URL containing the absolute path of the
5981  location from which the add-on was loaded. For a file on the local
5982  file system it will start with `file://`. The string is null-terminated and
5983  owned by `env` and must thus not be modified or freed.
5984
5985`result` may be an empty string if the add-on loading process fails to establish
5986the add-on's file name during loading.
5987
5988[ABI Stability]: https://nodejs.org/en/docs/guides/abi-stability/
5989[AppVeyor]: https://www.appveyor.com
5990[C++ Addons]: addons.md
5991[CMake]: https://cmake.org
5992[CMake.js]: https://github.com/cmake-js/cmake-js
5993[ECMAScript Language Specification]: https://tc39.github.io/ecma262/
5994[Error handling]: #n_api_error_handling
5995[GCC]: https://gcc.gnu.org
5996[GYP]: https://gyp.gsrc.io
5997[GitHub releases]: https://help.github.com/en/github/administering-a-repository/about-releases
5998[LLVM]: https://llvm.org
5999[Native Abstractions for Node.js]: https://github.com/nodejs/nan
6000[Object lifetime management]: #n_api_object_lifetime_management
6001[Object wrap]: #n_api_object_wrap
6002[Section 12.10.4]: https://tc39.github.io/ecma262/#sec-instanceofoperator
6003[Section 12.5.5]: https://tc39.github.io/ecma262/#sec-typeof-operator
6004[Section 19.2]: https://tc39.github.io/ecma262/#sec-function-objects
6005[Section 19.4]: https://tc39.github.io/ecma262/#sec-symbol-objects
6006[Section 20.3]: https://tc39.github.io/ecma262/#sec-date-objects
6007[Section 22.1]: https://tc39.github.io/ecma262/#sec-array-objects
6008[Section 22.1.4.1]: https://tc39.github.io/ecma262/#sec-properties-of-array-instances-length
6009[Section 22.2]: https://tc39.github.io/ecma262/#sec-typedarray-objects
6010[Section 24.1]: https://tc39.github.io/ecma262/#sec-arraybuffer-objects
6011[Section 24.1.1.2]: https://tc39.es/ecma262/#sec-isdetachedbuffer
6012[Section 24.1.1.3]: https://tc39.es/ecma262/#sec-detacharraybuffer
6013[Section 24.3]: https://tc39.github.io/ecma262/#sec-dataview-objects
6014[Section 25.4]: https://tc39.github.io/ecma262/#sec-promise-objects
6015[Section 6]: https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values
6016[Section 6.1]: https://tc39.github.io/ecma262/#sec-ecmascript-language-types
6017[Section 6.1.4]: https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type
6018[Section 6.1.6]: https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type
6019[Section 6.1.7]: https://tc39.github.io/ecma262/#sec-object-type
6020[Section 6.1.7.1]: https://tc39.github.io/ecma262/#table-2
6021[Section 7]: https://tc39.github.io/ecma262/#sec-abstract-operations
6022[Section 7.1.13]: https://tc39.github.io/ecma262/#sec-toobject
6023[Section 7.1.2]: https://tc39.github.io/ecma262/#sec-toboolean
6024[Section 7.1.3]: https://tc39.github.io/ecma262/#sec-tonumber
6025[Section 7.2.14]: https://tc39.github.io/ecma262/#sec-strict-equality-comparison
6026[Section 7.2.2]: https://tc39.github.io/ecma262/#sec-isarray
6027[Section 8.7]: https://tc39.es/ecma262/#sec-agents
6028[Section 9.1.6]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc
6029[Travis CI]: https://travis-ci.org
6030[Visual Studio]: https://visualstudio.microsoft.com
6031[Working with JavaScript properties]: #n_api_working_with_javascript_properties
6032[Xcode]: https://developer.apple.com/xcode/
6033[`Number.MAX_SAFE_INTEGER`]: https://tc39.github.io/ecma262/#sec-number.max_safe_integer
6034[`Number.MIN_SAFE_INTEGER`]: https://tc39.github.io/ecma262/#sec-number.min_safe_integer
6035[`Worker`]: worker_threads.md#worker_threads_class_worker
6036[`async_hooks.executionAsyncResource()`]: async_hooks.md#async_hooks_async_hooks_executionasyncresource
6037[`global`]: globals.md#globals_global
6038[`init` hooks]: async_hooks.md#async_hooks_init_asyncid_type_triggerasyncid_resource
6039[`napi_add_async_cleanup_hook`]: #n_api_napi_add_async_cleanup_hook
6040[`napi_add_env_cleanup_hook`]: #n_api_napi_add_env_cleanup_hook
6041[`napi_add_finalizer`]: #n_api_napi_add_finalizer
6042[`napi_async_cleanup_hook`]: #n_api_napi_async_cleanup_hook
6043[`napi_async_complete_callback`]: #n_api_napi_async_complete_callback
6044[`napi_async_destroy`]: #n_api_napi_async_destroy
6045[`napi_async_init`]: #n_api_napi_async_init
6046[`napi_callback`]: #n_api_napi_callback
6047[`napi_cancel_async_work`]: #n_api_napi_cancel_async_work
6048[`napi_close_callback_scope`]: #n_api_napi_close_callback_scope
6049[`napi_close_escapable_handle_scope`]: #n_api_napi_close_escapable_handle_scope
6050[`napi_close_handle_scope`]: #n_api_napi_close_handle_scope
6051[`napi_create_async_work`]: #n_api_napi_create_async_work
6052[`napi_create_error`]: #n_api_napi_create_error
6053[`napi_create_external_arraybuffer`]: #n_api_napi_create_external_arraybuffer
6054[`napi_create_range_error`]: #n_api_napi_create_range_error
6055[`napi_create_reference`]: #n_api_napi_create_reference
6056[`napi_create_type_error`]: #n_api_napi_create_type_error
6057[`napi_define_class`]: #n_api_napi_define_class
6058[`napi_delete_async_work`]: #n_api_napi_delete_async_work
6059[`napi_delete_reference`]: #n_api_napi_delete_reference
6060[`napi_escape_handle`]: #n_api_napi_escape_handle
6061[`napi_finalize`]: #n_api_napi_finalize
6062[`napi_get_and_clear_last_exception`]: #n_api_napi_get_and_clear_last_exception
6063[`napi_get_array_length`]: #n_api_napi_get_array_length
6064[`napi_get_element`]: #n_api_napi_get_element
6065[`napi_get_last_error_info`]: #n_api_napi_get_last_error_info
6066[`napi_get_property`]: #n_api_napi_get_property
6067[`napi_get_reference_value`]: #n_api_napi_get_reference_value
6068[`napi_get_value_external`]: #n_api_napi_get_value_external
6069[`napi_has_property`]: #n_api_napi_has_property
6070[`napi_instanceof`]: #n_api_napi_instanceof
6071[`napi_is_error`]: #n_api_napi_is_error
6072[`napi_is_exception_pending`]: #n_api_napi_is_exception_pending
6073[`napi_make_callback`]: #n_api_napi_make_callback
6074[`napi_open_callback_scope`]: #n_api_napi_open_callback_scope
6075[`napi_open_escapable_handle_scope`]: #n_api_napi_open_escapable_handle_scope
6076[`napi_open_handle_scope`]: #n_api_napi_open_handle_scope
6077[`napi_property_attributes`]: #n_api_napi_property_attributes
6078[`napi_property_descriptor`]: #n_api_napi_property_descriptor
6079[`napi_queue_async_work`]: #n_api_napi_queue_async_work
6080[`napi_reference_ref`]: #n_api_napi_reference_ref
6081[`napi_reference_unref`]: #n_api_napi_reference_unref
6082[`napi_remove_async_cleanup_hook`]: #n_api_napi_remove_async_cleanup_hook
6083[`napi_remove_env_cleanup_hook`]: #n_api_napi_remove_env_cleanup_hook
6084[`napi_set_instance_data`]: #n_api_napi_set_instance_data
6085[`napi_set_property`]: #n_api_napi_set_property
6086[`napi_threadsafe_function_call_js`]: #n_api_napi_threadsafe_function_call_js
6087[`napi_throw_error`]: #n_api_napi_throw_error
6088[`napi_throw_range_error`]: #n_api_napi_throw_range_error
6089[`napi_throw_type_error`]: #n_api_napi_throw_type_error
6090[`napi_throw`]: #n_api_napi_throw
6091[`napi_unwrap`]: #n_api_napi_unwrap
6092[`napi_wrap`]: #n_api_napi_wrap
6093[`node-addon-api`]: https://github.com/nodejs/node-addon-api
6094[`node_api.h`]: https://github.com/nodejs/node/blob/HEAD/src/node_api.h
6095[`process.release`]: process.md#process_process_release
6096[`uv_ref`]: https://docs.libuv.org/en/v1.x/handle.html#c.uv_ref
6097[`uv_unref`]: https://docs.libuv.org/en/v1.x/handle.html#c.uv_unref
6098[async_hooks `type`]: async_hooks.md#async_hooks_type
6099[context-aware addons]: addons.md#addons_context_aware_addons
6100[docs]: https://github.com/nodejs/node-addon-api#api-documentation
6101[global scope]: globals.md
6102[gyp-next]: https://github.com/nodejs/gyp-next
6103[module scope]: modules.md#modules_the_module_scope
6104[node-gyp]: https://github.com/nodejs/node-gyp
6105[node-pre-gyp]: https://github.com/mapbox/node-pre-gyp
6106[prebuild]: https://github.com/prebuild/prebuild
6107[prebuildify]: https://github.com/prebuild/prebuildify
6108[worker threads]: https://nodejs.org/api/worker_threads.html
6109