xref: /third_party/node/doc/api/n-api.md (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci# Node-API
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!--introduced_in=v8.0.0-->
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci<!-- type=misc -->
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci> Stability: 2 - Stable
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciNode-API (formerly N-API) is an API for building native Addons. It is
101cb0ef41Sopenharmony_ciindependent from the underlying JavaScript runtime (for example, V8) and is
111cb0ef41Sopenharmony_cimaintained as part of Node.js itself. This API will be Application Binary
121cb0ef41Sopenharmony_ciInterface (ABI) stable across versions of Node.js. It is intended to insulate
131cb0ef41Sopenharmony_ciaddons from changes in the underlying JavaScript engine and allow modules
141cb0ef41Sopenharmony_cicompiled for one major version to run on later major versions of Node.js without
151cb0ef41Sopenharmony_cirecompilation. The [ABI Stability][] guide provides a more in-depth explanation.
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciAddons are built/packaged with the same approach/tools outlined in the section
181cb0ef41Sopenharmony_cititled [C++ Addons][]. The only difference is the set of APIs that are used by
191cb0ef41Sopenharmony_cithe native code. Instead of using the V8 or [Native Abstractions for Node.js][]
201cb0ef41Sopenharmony_ciAPIs, the functions available in Node-API are used.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciAPIs exposed by Node-API are generally used to create and manipulate
231cb0ef41Sopenharmony_ciJavaScript values. Concepts and operations generally map to ideas specified
241cb0ef41Sopenharmony_ciin the ECMA-262 Language Specification. The APIs have the following
251cb0ef41Sopenharmony_ciproperties:
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci* All Node-API calls return a status code of type `napi_status`. This
281cb0ef41Sopenharmony_ci  status indicates whether the API call succeeded or failed.
291cb0ef41Sopenharmony_ci* The API's return value is passed via an out parameter.
301cb0ef41Sopenharmony_ci* All JavaScript values are abstracted behind an opaque type named
311cb0ef41Sopenharmony_ci  `napi_value`.
321cb0ef41Sopenharmony_ci* In case of an error status code, additional information can be obtained
331cb0ef41Sopenharmony_ci  using `napi_get_last_error_info`. More information can be found in the error
341cb0ef41Sopenharmony_ci  handling section [Error handling][].
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciNode-API is a C API that ensures ABI stability across Node.js versions
371cb0ef41Sopenharmony_ciand different compiler levels. A C++ API can be easier to use.
381cb0ef41Sopenharmony_ciTo support using C++, the project maintains a
391cb0ef41Sopenharmony_ciC++ wrapper module called [`node-addon-api`][].
401cb0ef41Sopenharmony_ciThis wrapper provides an inlinable C++ API. Binaries built
411cb0ef41Sopenharmony_ciwith `node-addon-api` will depend on the symbols for the Node-API C-based
421cb0ef41Sopenharmony_cifunctions exported by Node.js. `node-addon-api` is a more
431cb0ef41Sopenharmony_ciefficient way to write code that calls Node-API. Take, for example, the
441cb0ef41Sopenharmony_cifollowing `node-addon-api` code. The first section shows the
451cb0ef41Sopenharmony_ci`node-addon-api` code and the second section shows what actually gets
461cb0ef41Sopenharmony_ciused in the addon.
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci```cpp
491cb0ef41Sopenharmony_ciObject obj = Object::New(env);
501cb0ef41Sopenharmony_ciobj["foo"] = String::New(env, "bar");
511cb0ef41Sopenharmony_ci```
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci```cpp
541cb0ef41Sopenharmony_cinapi_status status;
551cb0ef41Sopenharmony_cinapi_value object, string;
561cb0ef41Sopenharmony_cistatus = napi_create_object(env, &object);
571cb0ef41Sopenharmony_ciif (status != napi_ok) {
581cb0ef41Sopenharmony_ci  napi_throw_error(env, ...);
591cb0ef41Sopenharmony_ci  return;
601cb0ef41Sopenharmony_ci}
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_cistatus = napi_create_string_utf8(env, "bar", NAPI_AUTO_LENGTH, &string);
631cb0ef41Sopenharmony_ciif (status != napi_ok) {
641cb0ef41Sopenharmony_ci  napi_throw_error(env, ...);
651cb0ef41Sopenharmony_ci  return;
661cb0ef41Sopenharmony_ci}
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_cistatus = napi_set_named_property(env, object, "foo", string);
691cb0ef41Sopenharmony_ciif (status != napi_ok) {
701cb0ef41Sopenharmony_ci  napi_throw_error(env, ...);
711cb0ef41Sopenharmony_ci  return;
721cb0ef41Sopenharmony_ci}
731cb0ef41Sopenharmony_ci```
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ciThe end result is that the addon only uses the exported C APIs. As a result,
761cb0ef41Sopenharmony_ciit still gets the benefits of the ABI stability provided by the C API.
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ciWhen using `node-addon-api` instead of the C APIs, start with the API [docs][]
791cb0ef41Sopenharmony_cifor `node-addon-api`.
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ciThe [Node-API Resource](https://nodejs.github.io/node-addon-examples/) offers
821cb0ef41Sopenharmony_cian excellent orientation and tips for developers just getting started with
831cb0ef41Sopenharmony_ciNode-API and `node-addon-api`. Additional media resources can be found on the
841cb0ef41Sopenharmony_ci[Node-API Media][] page.
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci## Implications of ABI stability
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ciAlthough Node-API provides an ABI stability guarantee, other parts of Node.js do
891cb0ef41Sopenharmony_cinot, and any external libraries used from the addon may not. In particular,
901cb0ef41Sopenharmony_cinone of the following APIs provide an ABI stability guarantee across major
911cb0ef41Sopenharmony_civersions:
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci* the Node.js C++ APIs available via any of
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci  ```cpp
961cb0ef41Sopenharmony_ci  #include <node.h>
971cb0ef41Sopenharmony_ci  #include <node_buffer.h>
981cb0ef41Sopenharmony_ci  #include <node_version.h>
991cb0ef41Sopenharmony_ci  #include <node_object_wrap.h>
1001cb0ef41Sopenharmony_ci  ```
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci* the libuv APIs which are also included with Node.js and available via
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci  ```cpp
1051cb0ef41Sopenharmony_ci  #include <uv.h>
1061cb0ef41Sopenharmony_ci  ```
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci* the V8 API available via
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci  ```cpp
1111cb0ef41Sopenharmony_ci  #include <v8.h>
1121cb0ef41Sopenharmony_ci  ```
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ciThus, for an addon to remain ABI-compatible across Node.js major versions, it
1151cb0ef41Sopenharmony_cimust use Node-API exclusively by restricting itself to using
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci```c
1181cb0ef41Sopenharmony_ci#include <node_api.h>
1191cb0ef41Sopenharmony_ci```
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ciand by checking, for all external libraries that it uses, that the external
1221cb0ef41Sopenharmony_cilibrary makes ABI stability guarantees similar to Node-API.
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci## Building
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ciUnlike modules written in JavaScript, developing and deploying Node.js
1271cb0ef41Sopenharmony_cinative addons using Node-API requires an additional set of tools. Besides the
1281cb0ef41Sopenharmony_cibasic tools required to develop for Node.js, the native addon developer
1291cb0ef41Sopenharmony_cirequires a toolchain that can compile C and C++ code into a binary. In
1301cb0ef41Sopenharmony_ciaddition, depending upon how the native addon is deployed, the _user_ of
1311cb0ef41Sopenharmony_cithe native addon will also need to have a C/C++ toolchain installed.
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ciFor Linux developers, the necessary C/C++ toolchain packages are readily
1341cb0ef41Sopenharmony_ciavailable. [GCC][] is widely used in the Node.js community to build and
1351cb0ef41Sopenharmony_citest across a variety of platforms. For many developers, the [LLVM][]
1361cb0ef41Sopenharmony_cicompiler infrastructure is also a good choice.
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ciFor Mac developers, [Xcode][] offers all the required compiler tools.
1391cb0ef41Sopenharmony_ciHowever, it is not necessary to install the entire Xcode IDE. The following
1401cb0ef41Sopenharmony_cicommand installs the necessary toolchain:
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci```bash
1431cb0ef41Sopenharmony_cixcode-select --install
1441cb0ef41Sopenharmony_ci```
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ciFor Windows developers, [Visual Studio][] offers all the required compiler
1471cb0ef41Sopenharmony_citools. However, it is not necessary to install the entire Visual Studio
1481cb0ef41Sopenharmony_ciIDE. The following command installs the necessary toolchain:
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci```bash
1511cb0ef41Sopenharmony_cinpm install --global windows-build-tools
1521cb0ef41Sopenharmony_ci```
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ciThe sections below describe the additional tools available for developing
1551cb0ef41Sopenharmony_ciand deploying Node.js native addons.
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci### Build tools
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ciBoth the tools listed here require that _users_ of the native
1601cb0ef41Sopenharmony_ciaddon have a C/C++ toolchain installed in order to successfully install
1611cb0ef41Sopenharmony_cithe native addon.
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci#### node-gyp
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci[node-gyp][] is a build system based on the [gyp-next][] fork of
1661cb0ef41Sopenharmony_ciGoogle's [GYP][] tool and comes bundled with npm. GYP, and therefore node-gyp,
1671cb0ef41Sopenharmony_cirequires that Python be installed.
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ciHistorically, node-gyp has been the tool of choice for building native
1701cb0ef41Sopenharmony_ciaddons. It has widespread adoption and documentation. However, some
1711cb0ef41Sopenharmony_cidevelopers have run into limitations in node-gyp.
1721cb0ef41Sopenharmony_ci
1731cb0ef41Sopenharmony_ci#### CMake.js
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci[CMake.js][] is an alternative build system based on [CMake][].
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ciCMake.js is a good choice for projects that already use CMake or for
1781cb0ef41Sopenharmony_cidevelopers affected by limitations in node-gyp. [`build_with_cmake`][] is an
1791cb0ef41Sopenharmony_ciexample of a CMake-based native addon project.
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ci### Uploading precompiled binaries
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_ciThe three tools listed here permit native addon developers and maintainers
1841cb0ef41Sopenharmony_cito create and upload binaries to public or private servers. These tools are
1851cb0ef41Sopenharmony_citypically integrated with CI/CD build systems like [Travis CI][] and
1861cb0ef41Sopenharmony_ci[AppVeyor][] to build and upload binaries for a variety of platforms and
1871cb0ef41Sopenharmony_ciarchitectures. These binaries are then available for download by users who
1881cb0ef41Sopenharmony_cido not need to have a C/C++ toolchain installed.
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_ci#### node-pre-gyp
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci[node-pre-gyp][] is a tool based on node-gyp that adds the ability to
1931cb0ef41Sopenharmony_ciupload binaries to a server of the developer's choice. node-pre-gyp has
1941cb0ef41Sopenharmony_ciparticularly good support for uploading binaries to Amazon S3.
1951cb0ef41Sopenharmony_ci
1961cb0ef41Sopenharmony_ci#### prebuild
1971cb0ef41Sopenharmony_ci
1981cb0ef41Sopenharmony_ci[prebuild][] is a tool that supports builds using either node-gyp or
1991cb0ef41Sopenharmony_ciCMake.js. Unlike node-pre-gyp which supports a variety of servers, prebuild
2001cb0ef41Sopenharmony_ciuploads binaries only to [GitHub releases][]. prebuild is a good choice for
2011cb0ef41Sopenharmony_ciGitHub projects using CMake.js.
2021cb0ef41Sopenharmony_ci
2031cb0ef41Sopenharmony_ci#### prebuildify
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ci[prebuildify][] is a tool based on node-gyp. The advantage of prebuildify is
2061cb0ef41Sopenharmony_cithat the built binaries are bundled with the native addon when it's
2071cb0ef41Sopenharmony_ciuploaded to npm. The binaries are downloaded from npm and are immediately
2081cb0ef41Sopenharmony_ciavailable to the module user when the native addon is installed.
2091cb0ef41Sopenharmony_ci
2101cb0ef41Sopenharmony_ci## Usage
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ciIn order to use the Node-API functions, include the file [`node_api.h`][] which
2131cb0ef41Sopenharmony_ciis located in the src directory in the node development tree:
2141cb0ef41Sopenharmony_ci
2151cb0ef41Sopenharmony_ci```c
2161cb0ef41Sopenharmony_ci#include <node_api.h>
2171cb0ef41Sopenharmony_ci```
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_ciThis will opt into the default `NAPI_VERSION` for the given release of Node.js.
2201cb0ef41Sopenharmony_ciIn order to ensure compatibility with specific versions of Node-API, the version
2211cb0ef41Sopenharmony_cican be specified explicitly when including the header:
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ci```c
2241cb0ef41Sopenharmony_ci#define NAPI_VERSION 3
2251cb0ef41Sopenharmony_ci#include <node_api.h>
2261cb0ef41Sopenharmony_ci```
2271cb0ef41Sopenharmony_ci
2281cb0ef41Sopenharmony_ciThis restricts the Node-API surface to just the functionality that was available
2291cb0ef41Sopenharmony_ciin the specified (and earlier) versions.
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_ciSome of the Node-API surface is experimental and requires explicit opt-in:
2321cb0ef41Sopenharmony_ci
2331cb0ef41Sopenharmony_ci```c
2341cb0ef41Sopenharmony_ci#define NAPI_EXPERIMENTAL
2351cb0ef41Sopenharmony_ci#include <node_api.h>
2361cb0ef41Sopenharmony_ci```
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ciIn this case the entire API surface, including any experimental APIs, will be
2391cb0ef41Sopenharmony_ciavailable to the module code.
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ciOccasionally, experimental features are introduced that affect already-released
2421cb0ef41Sopenharmony_ciand stable APIs. These features can be disabled by an opt-out:
2431cb0ef41Sopenharmony_ci
2441cb0ef41Sopenharmony_ci```c
2451cb0ef41Sopenharmony_ci#define NAPI_EXPERIMENTAL
2461cb0ef41Sopenharmony_ci#define NODE_API_EXPERIMENTAL_<FEATURE_NAME>_OPT_OUT
2471cb0ef41Sopenharmony_ci#include <node_api.h>
2481cb0ef41Sopenharmony_ci```
2491cb0ef41Sopenharmony_ci
2501cb0ef41Sopenharmony_ciwhere `<FEATURE_NAME>` is the name of an experimental feature that affects both
2511cb0ef41Sopenharmony_ciexperimental and stable APIs.
2521cb0ef41Sopenharmony_ci
2531cb0ef41Sopenharmony_ci## Node-API version matrix
2541cb0ef41Sopenharmony_ci
2551cb0ef41Sopenharmony_ciNode-API versions are additive and versioned independently from Node.js.
2561cb0ef41Sopenharmony_ciVersion 4 is an extension to version 3 in that it has all of the APIs
2571cb0ef41Sopenharmony_cifrom version 3 with some additions. This means that it is not necessary
2581cb0ef41Sopenharmony_cito recompile for new versions of Node.js which are
2591cb0ef41Sopenharmony_cilisted as supporting a later version.
2601cb0ef41Sopenharmony_ci
2611cb0ef41Sopenharmony_ciThis table may not be up to date in older streams, the most up to date
2621cb0ef41Sopenharmony_ciinformation is in the latest API documentation in:
2631cb0ef41Sopenharmony_ci[Node-API version matrix](https://nodejs.org/docs/latest/api/n-api.html#node-api-version-matrix)
2641cb0ef41Sopenharmony_ci
2651cb0ef41Sopenharmony_ci<!-- For accessibility purposes, this table needs row headers. That means we
2661cb0ef41Sopenharmony_ci     can't do it in markdown. Hence, the raw HTML. -->
2671cb0ef41Sopenharmony_ci
2681cb0ef41Sopenharmony_ci<table>
2691cb0ef41Sopenharmony_ci  <tr>
2701cb0ef41Sopenharmony_ci    <th>Node-API version</th>
2711cb0ef41Sopenharmony_ci    <th scope="col">Supported In</th>
2721cb0ef41Sopenharmony_ci  </tr>
2731cb0ef41Sopenharmony_ci  <tr>
2741cb0ef41Sopenharmony_ci    <th scope="row">9</th>
2751cb0ef41Sopenharmony_ci    <td>v18.17.0+, 20.3.0+, 21.0.0 and all later versions</td>
2761cb0ef41Sopenharmony_ci  </tr>
2771cb0ef41Sopenharmony_ci  <tr>
2781cb0ef41Sopenharmony_ci    <th scope="row">8</th>
2791cb0ef41Sopenharmony_ci    <td>v12.22.0+, v14.17.0+, v15.12.0+, 16.0.0 and all later versions</td>
2801cb0ef41Sopenharmony_ci  </tr>
2811cb0ef41Sopenharmony_ci  <tr>
2821cb0ef41Sopenharmony_ci    <th scope="row">7</th>
2831cb0ef41Sopenharmony_ci    <td>v10.23.0+, v12.19.0+, v14.12.0+, 15.0.0 and all later versions</td>
2841cb0ef41Sopenharmony_ci  </tr>
2851cb0ef41Sopenharmony_ci  <tr>
2861cb0ef41Sopenharmony_ci    <th scope="row">6</th>
2871cb0ef41Sopenharmony_ci    <td>v10.20.0+, v12.17.0+, 14.0.0 and all later versions</td>
2881cb0ef41Sopenharmony_ci  </tr>
2891cb0ef41Sopenharmony_ci  <tr>
2901cb0ef41Sopenharmony_ci    <th scope="row">5</th>
2911cb0ef41Sopenharmony_ci    <td>v10.17.0+, v12.11.0+, 13.0.0 and all later versions</td>
2921cb0ef41Sopenharmony_ci  </tr>
2931cb0ef41Sopenharmony_ci  <tr>
2941cb0ef41Sopenharmony_ci    <th scope="row">4</th>
2951cb0ef41Sopenharmony_ci    <td>v10.16.0+, v11.8.0+, 12.0.0 and all later versions</td>
2961cb0ef41Sopenharmony_ci  </tr>
2971cb0ef41Sopenharmony_ci  </tr>
2981cb0ef41Sopenharmony_ci    <tr>
2991cb0ef41Sopenharmony_ci    <th scope="row">3</th>
3001cb0ef41Sopenharmony_ci    <td>v6.14.2*, 8.11.2+, v9.11.0+*, 10.0.0 and all later versions</td>
3011cb0ef41Sopenharmony_ci  </tr>
3021cb0ef41Sopenharmony_ci  <tr>
3031cb0ef41Sopenharmony_ci    <th scope="row">2</th>
3041cb0ef41Sopenharmony_ci    <td>v8.10.0+*, v9.3.0+*, 10.0.0 and all later versions</td>
3051cb0ef41Sopenharmony_ci  </tr>
3061cb0ef41Sopenharmony_ci  <tr>
3071cb0ef41Sopenharmony_ci    <th scope="row">1</th>
3081cb0ef41Sopenharmony_ci    <td>v8.6.0+**, v9.0.0+*, 10.0.0 and all later versions</td>
3091cb0ef41Sopenharmony_ci  </tr>
3101cb0ef41Sopenharmony_ci</table>
3111cb0ef41Sopenharmony_ci
3121cb0ef41Sopenharmony_ci\* Node-API was experimental.
3131cb0ef41Sopenharmony_ci
3141cb0ef41Sopenharmony_ci\*\* Node.js 8.0.0 included Node-API as experimental. It was released as
3151cb0ef41Sopenharmony_ciNode-API version 1 but continued to evolve until Node.js 8.6.0. The API is
3161cb0ef41Sopenharmony_cidifferent in versions prior to Node.js 8.6.0. We recommend Node-API version 3 or
3171cb0ef41Sopenharmony_cilater.
3181cb0ef41Sopenharmony_ci
3191cb0ef41Sopenharmony_ciEach API documented for Node-API will have a header named `added in:`, and APIs
3201cb0ef41Sopenharmony_ciwhich are stable will have the additional header `Node-API version:`.
3211cb0ef41Sopenharmony_ciAPIs are directly usable when using a Node.js version which supports
3221cb0ef41Sopenharmony_cithe Node-API version shown in `Node-API version:` or higher.
3231cb0ef41Sopenharmony_ciWhen using a Node.js version that does not support the
3241cb0ef41Sopenharmony_ci`Node-API version:` listed or if there is no `Node-API version:` listed,
3251cb0ef41Sopenharmony_cithen the API will only be available if
3261cb0ef41Sopenharmony_ci`#define NAPI_EXPERIMENTAL` precedes the inclusion of `node_api.h`
3271cb0ef41Sopenharmony_cior `js_native_api.h`. If an API appears not to be available on
3281cb0ef41Sopenharmony_cia version of Node.js which is later than the one shown in `added in:` then
3291cb0ef41Sopenharmony_cithis is most likely the reason for the apparent absence.
3301cb0ef41Sopenharmony_ci
3311cb0ef41Sopenharmony_ciThe Node-APIs associated strictly with accessing ECMAScript features from native
3321cb0ef41Sopenharmony_cicode can be found separately in `js_native_api.h` and `js_native_api_types.h`.
3331cb0ef41Sopenharmony_ciThe APIs defined in these headers are included in `node_api.h` and
3341cb0ef41Sopenharmony_ci`node_api_types.h`. The headers are structured in this way in order to allow
3351cb0ef41Sopenharmony_ciimplementations of Node-API outside of Node.js. For those implementations the
3361cb0ef41Sopenharmony_ciNode.js specific APIs may not be applicable.
3371cb0ef41Sopenharmony_ci
3381cb0ef41Sopenharmony_ciThe Node.js-specific parts of an addon can be separated from the code that
3391cb0ef41Sopenharmony_ciexposes the actual functionality to the JavaScript environment so that the
3401cb0ef41Sopenharmony_cilatter may be used with multiple implementations of Node-API. In the example
3411cb0ef41Sopenharmony_cibelow, `addon.c` and `addon.h` refer only to `js_native_api.h`. This ensures
3421cb0ef41Sopenharmony_cithat `addon.c` can be reused to compile against either the Node.js
3431cb0ef41Sopenharmony_ciimplementation of Node-API or any implementation of Node-API outside of Node.js.
3441cb0ef41Sopenharmony_ci
3451cb0ef41Sopenharmony_ci`addon_node.c` is a separate file that contains the Node.js specific entry point
3461cb0ef41Sopenharmony_cito the addon and which instantiates the addon by calling into `addon.c` when the
3471cb0ef41Sopenharmony_ciaddon is loaded into a Node.js environment.
3481cb0ef41Sopenharmony_ci
3491cb0ef41Sopenharmony_ci```c
3501cb0ef41Sopenharmony_ci// addon.h
3511cb0ef41Sopenharmony_ci#ifndef _ADDON_H_
3521cb0ef41Sopenharmony_ci#define _ADDON_H_
3531cb0ef41Sopenharmony_ci#include <js_native_api.h>
3541cb0ef41Sopenharmony_cinapi_value create_addon(napi_env env);
3551cb0ef41Sopenharmony_ci#endif  // _ADDON_H_
3561cb0ef41Sopenharmony_ci```
3571cb0ef41Sopenharmony_ci
3581cb0ef41Sopenharmony_ci```c
3591cb0ef41Sopenharmony_ci// addon.c
3601cb0ef41Sopenharmony_ci#include "addon.h"
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_ci#define NODE_API_CALL(env, call)                                  \
3631cb0ef41Sopenharmony_ci  do {                                                            \
3641cb0ef41Sopenharmony_ci    napi_status status = (call);                                  \
3651cb0ef41Sopenharmony_ci    if (status != napi_ok) {                                      \
3661cb0ef41Sopenharmony_ci      const napi_extended_error_info* error_info = NULL;          \
3671cb0ef41Sopenharmony_ci      napi_get_last_error_info((env), &error_info);               \
3681cb0ef41Sopenharmony_ci      const char* err_message = error_info->error_message;        \
3691cb0ef41Sopenharmony_ci      bool is_pending;                                            \
3701cb0ef41Sopenharmony_ci      napi_is_exception_pending((env), &is_pending);              \
3711cb0ef41Sopenharmony_ci      /* If an exception is already pending, don't rethrow it */  \
3721cb0ef41Sopenharmony_ci      if (!is_pending) {                                          \
3731cb0ef41Sopenharmony_ci        const char* message = (err_message == NULL)               \
3741cb0ef41Sopenharmony_ci            ? "empty error message"                               \
3751cb0ef41Sopenharmony_ci            : err_message;                                        \
3761cb0ef41Sopenharmony_ci        napi_throw_error((env), NULL, message);                   \
3771cb0ef41Sopenharmony_ci      }                                                           \
3781cb0ef41Sopenharmony_ci      return NULL;                                                \
3791cb0ef41Sopenharmony_ci    }                                                             \
3801cb0ef41Sopenharmony_ci  } while(0)
3811cb0ef41Sopenharmony_ci
3821cb0ef41Sopenharmony_cistatic napi_value
3831cb0ef41Sopenharmony_ciDoSomethingUseful(napi_env env, napi_callback_info info) {
3841cb0ef41Sopenharmony_ci  // Do something useful.
3851cb0ef41Sopenharmony_ci  return NULL;
3861cb0ef41Sopenharmony_ci}
3871cb0ef41Sopenharmony_ci
3881cb0ef41Sopenharmony_cinapi_value create_addon(napi_env env) {
3891cb0ef41Sopenharmony_ci  napi_value result;
3901cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_create_object(env, &result));
3911cb0ef41Sopenharmony_ci
3921cb0ef41Sopenharmony_ci  napi_value exported_function;
3931cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_create_function(env,
3941cb0ef41Sopenharmony_ci                                          "doSomethingUseful",
3951cb0ef41Sopenharmony_ci                                          NAPI_AUTO_LENGTH,
3961cb0ef41Sopenharmony_ci                                          DoSomethingUseful,
3971cb0ef41Sopenharmony_ci                                          NULL,
3981cb0ef41Sopenharmony_ci                                          &exported_function));
3991cb0ef41Sopenharmony_ci
4001cb0ef41Sopenharmony_ci  NODE_API_CALL(env, napi_set_named_property(env,
4011cb0ef41Sopenharmony_ci                                             result,
4021cb0ef41Sopenharmony_ci                                             "doSomethingUseful",
4031cb0ef41Sopenharmony_ci                                             exported_function));
4041cb0ef41Sopenharmony_ci
4051cb0ef41Sopenharmony_ci  return result;
4061cb0ef41Sopenharmony_ci}
4071cb0ef41Sopenharmony_ci```
4081cb0ef41Sopenharmony_ci
4091cb0ef41Sopenharmony_ci```c
4101cb0ef41Sopenharmony_ci// addon_node.c
4111cb0ef41Sopenharmony_ci#include <node_api.h>
4121cb0ef41Sopenharmony_ci#include "addon.h"
4131cb0ef41Sopenharmony_ci
4141cb0ef41Sopenharmony_ciNAPI_MODULE_INIT(/* napi_env env, napi_value exports */) {
4151cb0ef41Sopenharmony_ci  // This function body is expected to return a `napi_value`.
4161cb0ef41Sopenharmony_ci  // The variables `napi_env env` and `napi_value exports` may be used within
4171cb0ef41Sopenharmony_ci  // the body, as they are provided by the definition of `NAPI_MODULE_INIT()`.
4181cb0ef41Sopenharmony_ci  return create_addon(env);
4191cb0ef41Sopenharmony_ci}
4201cb0ef41Sopenharmony_ci```
4211cb0ef41Sopenharmony_ci
4221cb0ef41Sopenharmony_ci## Environment life cycle APIs
4231cb0ef41Sopenharmony_ci
4241cb0ef41Sopenharmony_ci[Section 8.7][] of the [ECMAScript Language Specification][] defines the concept
4251cb0ef41Sopenharmony_ciof an "Agent" as a self-contained environment in which JavaScript code runs.
4261cb0ef41Sopenharmony_ciMultiple such Agents may be started and terminated either concurrently or in
4271cb0ef41Sopenharmony_cisequence by the process.
4281cb0ef41Sopenharmony_ci
4291cb0ef41Sopenharmony_ciA Node.js environment corresponds to an ECMAScript Agent. In the main process,
4301cb0ef41Sopenharmony_cian environment is created at startup, and additional environments can be created
4311cb0ef41Sopenharmony_cion separate threads to serve as [worker threads][]. When Node.js is embedded in
4321cb0ef41Sopenharmony_cianother application, the main thread of the application may also construct and
4331cb0ef41Sopenharmony_cidestroy a Node.js environment multiple times during the life cycle of the
4341cb0ef41Sopenharmony_ciapplication process such that each Node.js environment created by the
4351cb0ef41Sopenharmony_ciapplication may, in turn, during its life cycle create and destroy additional
4361cb0ef41Sopenharmony_cienvironments as worker threads.
4371cb0ef41Sopenharmony_ci
4381cb0ef41Sopenharmony_ciFrom the perspective of a native addon this means that the bindings it provides
4391cb0ef41Sopenharmony_cimay be called multiple times, from multiple contexts, and even concurrently from
4401cb0ef41Sopenharmony_cimultiple threads.
4411cb0ef41Sopenharmony_ci
4421cb0ef41Sopenharmony_ciNative addons may need to allocate global state which they use during
4431cb0ef41Sopenharmony_citheir life cycle of an Node.js environment such that the state can be
4441cb0ef41Sopenharmony_ciunique to each instance of the addon.
4451cb0ef41Sopenharmony_ci
4461cb0ef41Sopenharmony_ciTo this end, Node-API provides a way to associate data such that its life cycle
4471cb0ef41Sopenharmony_ciis tied to the life cycle of a Node.js environment.
4481cb0ef41Sopenharmony_ci
4491cb0ef41Sopenharmony_ci### `napi_set_instance_data`
4501cb0ef41Sopenharmony_ci
4511cb0ef41Sopenharmony_ci<!-- YAML
4521cb0ef41Sopenharmony_ciadded:
4531cb0ef41Sopenharmony_ci - v12.8.0
4541cb0ef41Sopenharmony_ci - v10.20.0
4551cb0ef41Sopenharmony_cinapiVersion: 6
4561cb0ef41Sopenharmony_ci-->
4571cb0ef41Sopenharmony_ci
4581cb0ef41Sopenharmony_ci```c
4591cb0ef41Sopenharmony_cinapi_status napi_set_instance_data(node_api_nogc_env env,
4601cb0ef41Sopenharmony_ci                                   void* data,
4611cb0ef41Sopenharmony_ci                                   napi_finalize finalize_cb,
4621cb0ef41Sopenharmony_ci                                   void* finalize_hint);
4631cb0ef41Sopenharmony_ci```
4641cb0ef41Sopenharmony_ci
4651cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
4661cb0ef41Sopenharmony_ci* `[in] data`: The data item to make available to bindings of this instance.
4671cb0ef41Sopenharmony_ci* `[in] finalize_cb`: The function to call when the environment is being torn
4681cb0ef41Sopenharmony_ci  down. The function receives `data` so that it might free it.
4691cb0ef41Sopenharmony_ci  [`napi_finalize`][] provides more details.
4701cb0ef41Sopenharmony_ci* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
4711cb0ef41Sopenharmony_ci  collection.
4721cb0ef41Sopenharmony_ci
4731cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
4741cb0ef41Sopenharmony_ci
4751cb0ef41Sopenharmony_ciThis API associates `data` with the currently running Node.js environment. `data`
4761cb0ef41Sopenharmony_cican later be retrieved using `napi_get_instance_data()`. Any existing data
4771cb0ef41Sopenharmony_ciassociated with the currently running Node.js environment which was set by means
4781cb0ef41Sopenharmony_ciof a previous call to `napi_set_instance_data()` will be overwritten. If a
4791cb0ef41Sopenharmony_ci`finalize_cb` was provided by the previous call, it will not be called.
4801cb0ef41Sopenharmony_ci
4811cb0ef41Sopenharmony_ci### `napi_get_instance_data`
4821cb0ef41Sopenharmony_ci
4831cb0ef41Sopenharmony_ci<!-- YAML
4841cb0ef41Sopenharmony_ciadded:
4851cb0ef41Sopenharmony_ci - v12.8.0
4861cb0ef41Sopenharmony_ci - v10.20.0
4871cb0ef41Sopenharmony_cinapiVersion: 6
4881cb0ef41Sopenharmony_ci-->
4891cb0ef41Sopenharmony_ci
4901cb0ef41Sopenharmony_ci```c
4911cb0ef41Sopenharmony_cinapi_status napi_get_instance_data(node_api_nogc_env env,
4921cb0ef41Sopenharmony_ci                                   void** data);
4931cb0ef41Sopenharmony_ci```
4941cb0ef41Sopenharmony_ci
4951cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
4961cb0ef41Sopenharmony_ci* `[out] data`: The data item that was previously associated with the currently
4971cb0ef41Sopenharmony_ci  running Node.js environment by a call to `napi_set_instance_data()`.
4981cb0ef41Sopenharmony_ci
4991cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
5001cb0ef41Sopenharmony_ci
5011cb0ef41Sopenharmony_ciThis API retrieves data that was previously associated with the currently
5021cb0ef41Sopenharmony_cirunning Node.js environment via `napi_set_instance_data()`. If no data is set,
5031cb0ef41Sopenharmony_cithe call will succeed and `data` will be set to `NULL`.
5041cb0ef41Sopenharmony_ci
5051cb0ef41Sopenharmony_ci## Basic Node-API data types
5061cb0ef41Sopenharmony_ci
5071cb0ef41Sopenharmony_ciNode-API exposes the following fundamental data types as abstractions that are
5081cb0ef41Sopenharmony_ciconsumed by the various APIs. These APIs should be treated as opaque,
5091cb0ef41Sopenharmony_ciintrospectable only with other Node-API calls.
5101cb0ef41Sopenharmony_ci
5111cb0ef41Sopenharmony_ci### `napi_status`
5121cb0ef41Sopenharmony_ci
5131cb0ef41Sopenharmony_ci<!-- YAML
5141cb0ef41Sopenharmony_ciadded: v8.0.0
5151cb0ef41Sopenharmony_cinapiVersion: 1
5161cb0ef41Sopenharmony_ci-->
5171cb0ef41Sopenharmony_ci
5181cb0ef41Sopenharmony_ciIntegral status code indicating the success or failure of a Node-API call.
5191cb0ef41Sopenharmony_ciCurrently, the following status codes are supported.
5201cb0ef41Sopenharmony_ci
5211cb0ef41Sopenharmony_ci```c
5221cb0ef41Sopenharmony_citypedef enum {
5231cb0ef41Sopenharmony_ci  napi_ok,
5241cb0ef41Sopenharmony_ci  napi_invalid_arg,
5251cb0ef41Sopenharmony_ci  napi_object_expected,
5261cb0ef41Sopenharmony_ci  napi_string_expected,
5271cb0ef41Sopenharmony_ci  napi_name_expected,
5281cb0ef41Sopenharmony_ci  napi_function_expected,
5291cb0ef41Sopenharmony_ci  napi_number_expected,
5301cb0ef41Sopenharmony_ci  napi_boolean_expected,
5311cb0ef41Sopenharmony_ci  napi_array_expected,
5321cb0ef41Sopenharmony_ci  napi_generic_failure,
5331cb0ef41Sopenharmony_ci  napi_pending_exception,
5341cb0ef41Sopenharmony_ci  napi_cancelled,
5351cb0ef41Sopenharmony_ci  napi_escape_called_twice,
5361cb0ef41Sopenharmony_ci  napi_handle_scope_mismatch,
5371cb0ef41Sopenharmony_ci  napi_callback_scope_mismatch,
5381cb0ef41Sopenharmony_ci  napi_queue_full,
5391cb0ef41Sopenharmony_ci  napi_closing,
5401cb0ef41Sopenharmony_ci  napi_bigint_expected,
5411cb0ef41Sopenharmony_ci  napi_date_expected,
5421cb0ef41Sopenharmony_ci  napi_arraybuffer_expected,
5431cb0ef41Sopenharmony_ci  napi_detachable_arraybuffer_expected,
5441cb0ef41Sopenharmony_ci  napi_would_deadlock,  /* unused */
5451cb0ef41Sopenharmony_ci  napi_no_external_buffers_allowed,
5461cb0ef41Sopenharmony_ci  napi_cannot_run_js
5471cb0ef41Sopenharmony_ci} napi_status;
5481cb0ef41Sopenharmony_ci```
5491cb0ef41Sopenharmony_ci
5501cb0ef41Sopenharmony_ciIf additional information is required upon an API returning a failed status,
5511cb0ef41Sopenharmony_ciit can be obtained by calling `napi_get_last_error_info`.
5521cb0ef41Sopenharmony_ci
5531cb0ef41Sopenharmony_ci### `napi_extended_error_info`
5541cb0ef41Sopenharmony_ci
5551cb0ef41Sopenharmony_ci<!-- YAML
5561cb0ef41Sopenharmony_ciadded: v8.0.0
5571cb0ef41Sopenharmony_cinapiVersion: 1
5581cb0ef41Sopenharmony_ci-->
5591cb0ef41Sopenharmony_ci
5601cb0ef41Sopenharmony_ci```c
5611cb0ef41Sopenharmony_citypedef struct {
5621cb0ef41Sopenharmony_ci  const char* error_message;
5631cb0ef41Sopenharmony_ci  void* engine_reserved;
5641cb0ef41Sopenharmony_ci  uint32_t engine_error_code;
5651cb0ef41Sopenharmony_ci  napi_status error_code;
5661cb0ef41Sopenharmony_ci} napi_extended_error_info;
5671cb0ef41Sopenharmony_ci```
5681cb0ef41Sopenharmony_ci
5691cb0ef41Sopenharmony_ci* `error_message`: UTF8-encoded string containing a VM-neutral description of
5701cb0ef41Sopenharmony_ci  the error.
5711cb0ef41Sopenharmony_ci* `engine_reserved`: Reserved for VM-specific error details. This is currently
5721cb0ef41Sopenharmony_ci  not implemented for any VM.
5731cb0ef41Sopenharmony_ci* `engine_error_code`: VM-specific error code. This is currently
5741cb0ef41Sopenharmony_ci  not implemented for any VM.
5751cb0ef41Sopenharmony_ci* `error_code`: The Node-API status code that originated with the last error.
5761cb0ef41Sopenharmony_ci
5771cb0ef41Sopenharmony_ciSee the [Error handling][] section for additional information.
5781cb0ef41Sopenharmony_ci
5791cb0ef41Sopenharmony_ci### `napi_env`
5801cb0ef41Sopenharmony_ci
5811cb0ef41Sopenharmony_ci`napi_env` is used to represent a context that the underlying Node-API
5821cb0ef41Sopenharmony_ciimplementation can use to persist VM-specific state. This structure is passed
5831cb0ef41Sopenharmony_cito native functions when they're invoked, and it must be passed back when
5841cb0ef41Sopenharmony_cimaking Node-API calls. Specifically, the same `napi_env` that was passed in when
5851cb0ef41Sopenharmony_cithe initial native function was called must be passed to any subsequent
5861cb0ef41Sopenharmony_cinested Node-API calls. Caching the `napi_env` for the purpose of general reuse,
5871cb0ef41Sopenharmony_ciand passing the `napi_env` between instances of the same addon running on
5881cb0ef41Sopenharmony_cidifferent [`Worker`][] threads is not allowed. The `napi_env` becomes invalid
5891cb0ef41Sopenharmony_ciwhen an instance of a native addon is unloaded. Notification of this event is
5901cb0ef41Sopenharmony_cidelivered through the callbacks given to [`napi_add_env_cleanup_hook`][] and
5911cb0ef41Sopenharmony_ci[`napi_set_instance_data`][].
5921cb0ef41Sopenharmony_ci
5931cb0ef41Sopenharmony_ci### `node_api_nogc_env`
5941cb0ef41Sopenharmony_ci
5951cb0ef41Sopenharmony_ci> Stability: 1 - Experimental
5961cb0ef41Sopenharmony_ci
5971cb0ef41Sopenharmony_ciThis variant of `napi_env` is passed to synchronous finalizers
5981cb0ef41Sopenharmony_ci([`node_api_nogc_finalize`][]). There is a subset of Node-APIs which accept
5991cb0ef41Sopenharmony_cia parameter of type `node_api_nogc_env` as their first argument. These APIs do
6001cb0ef41Sopenharmony_cinot access the state of the JavaScript engine and are thus safe to call from
6011cb0ef41Sopenharmony_cisynchronous finalizers. Passing a parameter of type `napi_env` to these APIs is
6021cb0ef41Sopenharmony_ciallowed, however, passing a parameter of type `node_api_nogc_env` to APIs that
6031cb0ef41Sopenharmony_ciaccess the JavaScript engine state is not allowed. Attempting to do so without
6041cb0ef41Sopenharmony_cia cast will produce a compiler warning or an error when add-ons are compiled
6051cb0ef41Sopenharmony_ciwith flags which cause them to emit warnings and/or errors when incorrect
6061cb0ef41Sopenharmony_cipointer types are passed into a function. Calling such APIs from a synchronous
6071cb0ef41Sopenharmony_cifinalizer will ultimately result in the termination of the application.
6081cb0ef41Sopenharmony_ci
6091cb0ef41Sopenharmony_ci### `napi_value`
6101cb0ef41Sopenharmony_ci
6111cb0ef41Sopenharmony_ciThis is an opaque pointer that is used to represent a JavaScript value.
6121cb0ef41Sopenharmony_ci
6131cb0ef41Sopenharmony_ci### `napi_threadsafe_function`
6141cb0ef41Sopenharmony_ci
6151cb0ef41Sopenharmony_ci<!-- YAML
6161cb0ef41Sopenharmony_ciadded: v10.6.0
6171cb0ef41Sopenharmony_cinapiVersion: 4
6181cb0ef41Sopenharmony_ci-->
6191cb0ef41Sopenharmony_ci
6201cb0ef41Sopenharmony_ciThis is an opaque pointer that represents a JavaScript function which can be
6211cb0ef41Sopenharmony_cicalled asynchronously from multiple threads via
6221cb0ef41Sopenharmony_ci`napi_call_threadsafe_function()`.
6231cb0ef41Sopenharmony_ci
6241cb0ef41Sopenharmony_ci### `napi_threadsafe_function_release_mode`
6251cb0ef41Sopenharmony_ci
6261cb0ef41Sopenharmony_ci<!-- YAML
6271cb0ef41Sopenharmony_ciadded: v10.6.0
6281cb0ef41Sopenharmony_cinapiVersion: 4
6291cb0ef41Sopenharmony_ci-->
6301cb0ef41Sopenharmony_ci
6311cb0ef41Sopenharmony_ciA value to be given to `napi_release_threadsafe_function()` to indicate whether
6321cb0ef41Sopenharmony_cithe thread-safe function is to be closed immediately (`napi_tsfn_abort`) or
6331cb0ef41Sopenharmony_cimerely released (`napi_tsfn_release`) and thus available for subsequent use via
6341cb0ef41Sopenharmony_ci`napi_acquire_threadsafe_function()` and `napi_call_threadsafe_function()`.
6351cb0ef41Sopenharmony_ci
6361cb0ef41Sopenharmony_ci```c
6371cb0ef41Sopenharmony_citypedef enum {
6381cb0ef41Sopenharmony_ci  napi_tsfn_release,
6391cb0ef41Sopenharmony_ci  napi_tsfn_abort
6401cb0ef41Sopenharmony_ci} napi_threadsafe_function_release_mode;
6411cb0ef41Sopenharmony_ci```
6421cb0ef41Sopenharmony_ci
6431cb0ef41Sopenharmony_ci### `napi_threadsafe_function_call_mode`
6441cb0ef41Sopenharmony_ci
6451cb0ef41Sopenharmony_ci<!-- YAML
6461cb0ef41Sopenharmony_ciadded: v10.6.0
6471cb0ef41Sopenharmony_cinapiVersion: 4
6481cb0ef41Sopenharmony_ci-->
6491cb0ef41Sopenharmony_ci
6501cb0ef41Sopenharmony_ciA value to be given to `napi_call_threadsafe_function()` to indicate whether
6511cb0ef41Sopenharmony_cithe call should block whenever the queue associated with the thread-safe
6521cb0ef41Sopenharmony_cifunction is full.
6531cb0ef41Sopenharmony_ci
6541cb0ef41Sopenharmony_ci```c
6551cb0ef41Sopenharmony_citypedef enum {
6561cb0ef41Sopenharmony_ci  napi_tsfn_nonblocking,
6571cb0ef41Sopenharmony_ci  napi_tsfn_blocking
6581cb0ef41Sopenharmony_ci} napi_threadsafe_function_call_mode;
6591cb0ef41Sopenharmony_ci```
6601cb0ef41Sopenharmony_ci
6611cb0ef41Sopenharmony_ci### Node-API memory management types
6621cb0ef41Sopenharmony_ci
6631cb0ef41Sopenharmony_ci#### `napi_handle_scope`
6641cb0ef41Sopenharmony_ci
6651cb0ef41Sopenharmony_ciThis is an abstraction used to control and modify the lifetime of objects
6661cb0ef41Sopenharmony_cicreated within a particular scope. In general, Node-API values are created
6671cb0ef41Sopenharmony_ciwithin the context of a handle scope. When a native method is called from
6681cb0ef41Sopenharmony_ciJavaScript, a default handle scope will exist. If the user does not explicitly
6691cb0ef41Sopenharmony_cicreate a new handle scope, Node-API values will be created in the default handle
6701cb0ef41Sopenharmony_ciscope. For any invocations of code outside the execution of a native method
6711cb0ef41Sopenharmony_ci(for instance, during a libuv callback invocation), the module is required to
6721cb0ef41Sopenharmony_cicreate a scope before invoking any functions that can result in the creation
6731cb0ef41Sopenharmony_ciof JavaScript values.
6741cb0ef41Sopenharmony_ci
6751cb0ef41Sopenharmony_ciHandle scopes are created using [`napi_open_handle_scope`][] and are destroyed
6761cb0ef41Sopenharmony_ciusing [`napi_close_handle_scope`][]. Closing the scope can indicate to the GC
6771cb0ef41Sopenharmony_cithat all `napi_value`s created during the lifetime of the handle scope are no
6781cb0ef41Sopenharmony_cilonger referenced from the current stack frame.
6791cb0ef41Sopenharmony_ci
6801cb0ef41Sopenharmony_ciFor more details, review the [Object lifetime management][].
6811cb0ef41Sopenharmony_ci
6821cb0ef41Sopenharmony_ci#### `napi_escapable_handle_scope`
6831cb0ef41Sopenharmony_ci
6841cb0ef41Sopenharmony_ci<!-- YAML
6851cb0ef41Sopenharmony_ciadded: v8.0.0
6861cb0ef41Sopenharmony_cinapiVersion: 1
6871cb0ef41Sopenharmony_ci-->
6881cb0ef41Sopenharmony_ci
6891cb0ef41Sopenharmony_ciEscapable handle scopes are a special type of handle scope to return values
6901cb0ef41Sopenharmony_cicreated within a particular handle scope to a parent scope.
6911cb0ef41Sopenharmony_ci
6921cb0ef41Sopenharmony_ci#### `napi_ref`
6931cb0ef41Sopenharmony_ci
6941cb0ef41Sopenharmony_ci<!-- YAML
6951cb0ef41Sopenharmony_ciadded: v8.0.0
6961cb0ef41Sopenharmony_cinapiVersion: 1
6971cb0ef41Sopenharmony_ci-->
6981cb0ef41Sopenharmony_ci
6991cb0ef41Sopenharmony_ciThis is the abstraction to use to reference a `napi_value`. This allows for
7001cb0ef41Sopenharmony_ciusers to manage the lifetimes of JavaScript values, including defining their
7011cb0ef41Sopenharmony_ciminimum lifetimes explicitly.
7021cb0ef41Sopenharmony_ci
7031cb0ef41Sopenharmony_ciFor more details, review the [Object lifetime management][].
7041cb0ef41Sopenharmony_ci
7051cb0ef41Sopenharmony_ci#### `napi_type_tag`
7061cb0ef41Sopenharmony_ci
7071cb0ef41Sopenharmony_ci<!-- YAML
7081cb0ef41Sopenharmony_ciadded:
7091cb0ef41Sopenharmony_ci  - v14.8.0
7101cb0ef41Sopenharmony_ci  - v12.19.0
7111cb0ef41Sopenharmony_cinapiVersion: 8
7121cb0ef41Sopenharmony_ci-->
7131cb0ef41Sopenharmony_ci
7141cb0ef41Sopenharmony_ciA 128-bit value stored as two unsigned 64-bit integers. It serves as a UUID
7151cb0ef41Sopenharmony_ciwith which JavaScript objects or [externals][] can be "tagged" in order to
7161cb0ef41Sopenharmony_ciensure that they are of a certain type. This is a stronger check than
7171cb0ef41Sopenharmony_ci[`napi_instanceof`][], because the latter can report a false positive if the
7181cb0ef41Sopenharmony_ciobject's prototype has been manipulated. Type-tagging is most useful in
7191cb0ef41Sopenharmony_ciconjunction with [`napi_wrap`][] because it ensures that the pointer retrieved
7201cb0ef41Sopenharmony_cifrom a wrapped object can be safely cast to the native type corresponding to the
7211cb0ef41Sopenharmony_citype tag that had been previously applied to the JavaScript object.
7221cb0ef41Sopenharmony_ci
7231cb0ef41Sopenharmony_ci```c
7241cb0ef41Sopenharmony_citypedef struct {
7251cb0ef41Sopenharmony_ci  uint64_t lower;
7261cb0ef41Sopenharmony_ci  uint64_t upper;
7271cb0ef41Sopenharmony_ci} napi_type_tag;
7281cb0ef41Sopenharmony_ci```
7291cb0ef41Sopenharmony_ci
7301cb0ef41Sopenharmony_ci#### `napi_async_cleanup_hook_handle`
7311cb0ef41Sopenharmony_ci
7321cb0ef41Sopenharmony_ci<!-- YAML
7331cb0ef41Sopenharmony_ciadded:
7341cb0ef41Sopenharmony_ci  - v14.10.0
7351cb0ef41Sopenharmony_ci  - v12.19.0
7361cb0ef41Sopenharmony_ci-->
7371cb0ef41Sopenharmony_ci
7381cb0ef41Sopenharmony_ciAn opaque value returned by [`napi_add_async_cleanup_hook`][]. It must be passed
7391cb0ef41Sopenharmony_cito [`napi_remove_async_cleanup_hook`][] when the chain of asynchronous cleanup
7401cb0ef41Sopenharmony_cievents completes.
7411cb0ef41Sopenharmony_ci
7421cb0ef41Sopenharmony_ci### Node-API callback types
7431cb0ef41Sopenharmony_ci
7441cb0ef41Sopenharmony_ci#### `napi_callback_info`
7451cb0ef41Sopenharmony_ci
7461cb0ef41Sopenharmony_ci<!-- YAML
7471cb0ef41Sopenharmony_ciadded: v8.0.0
7481cb0ef41Sopenharmony_cinapiVersion: 1
7491cb0ef41Sopenharmony_ci-->
7501cb0ef41Sopenharmony_ci
7511cb0ef41Sopenharmony_ciOpaque datatype that is passed to a callback function. It can be used for
7521cb0ef41Sopenharmony_cigetting additional information about the context in which the callback was
7531cb0ef41Sopenharmony_ciinvoked.
7541cb0ef41Sopenharmony_ci
7551cb0ef41Sopenharmony_ci#### `napi_callback`
7561cb0ef41Sopenharmony_ci
7571cb0ef41Sopenharmony_ci<!-- YAML
7581cb0ef41Sopenharmony_ciadded: v8.0.0
7591cb0ef41Sopenharmony_cinapiVersion: 1
7601cb0ef41Sopenharmony_ci-->
7611cb0ef41Sopenharmony_ci
7621cb0ef41Sopenharmony_ciFunction pointer type for user-provided native functions which are to be
7631cb0ef41Sopenharmony_ciexposed to JavaScript via Node-API. Callback functions should satisfy the
7641cb0ef41Sopenharmony_cifollowing signature:
7651cb0ef41Sopenharmony_ci
7661cb0ef41Sopenharmony_ci```c
7671cb0ef41Sopenharmony_citypedef napi_value (*napi_callback)(napi_env, napi_callback_info);
7681cb0ef41Sopenharmony_ci```
7691cb0ef41Sopenharmony_ci
7701cb0ef41Sopenharmony_ciUnless for reasons discussed in [Object Lifetime Management][], creating a
7711cb0ef41Sopenharmony_cihandle and/or callback scope inside a `napi_callback` is not necessary.
7721cb0ef41Sopenharmony_ci
7731cb0ef41Sopenharmony_ci#### `node_api_nogc_finalize`
7741cb0ef41Sopenharmony_ci
7751cb0ef41Sopenharmony_ci<!-- YAML
7761cb0ef41Sopenharmony_ciadded: v18.20.0
7771cb0ef41Sopenharmony_ci-->
7781cb0ef41Sopenharmony_ci
7791cb0ef41Sopenharmony_ci> Stability: 1 - Experimental
7801cb0ef41Sopenharmony_ci
7811cb0ef41Sopenharmony_ciFunction pointer type for add-on provided functions that allow the user to be
7821cb0ef41Sopenharmony_cinotified when externally-owned data is ready to be cleaned up because the
7831cb0ef41Sopenharmony_ciobject it was associated with has been garbage-collected. The user must provide
7841cb0ef41Sopenharmony_cia function satisfying the following signature which would get called upon the
7851cb0ef41Sopenharmony_ciobject's collection. Currently, `node_api_nogc_finalize` can be used for
7861cb0ef41Sopenharmony_cifinding out when objects that have external data are collected.
7871cb0ef41Sopenharmony_ci
7881cb0ef41Sopenharmony_ci```c
7891cb0ef41Sopenharmony_citypedef void (*node_api_nogc_finalize)(node_api_nogc_env env,
7901cb0ef41Sopenharmony_ci                                      void* finalize_data,
7911cb0ef41Sopenharmony_ci                                      void* finalize_hint);
7921cb0ef41Sopenharmony_ci```
7931cb0ef41Sopenharmony_ci
7941cb0ef41Sopenharmony_ciUnless for reasons discussed in [Object Lifetime Management][], creating a
7951cb0ef41Sopenharmony_cihandle and/or callback scope inside the function body is not necessary.
7961cb0ef41Sopenharmony_ci
7971cb0ef41Sopenharmony_ciSince these functions may be called while the JavaScript engine is in a state
7981cb0ef41Sopenharmony_ciwhere it cannot execute JavaScript code, only Node-APIs which accept a
7991cb0ef41Sopenharmony_ci`node_api_nogc_env` as their first parameter may be called.
8001cb0ef41Sopenharmony_ci[`node_api_post_finalizer`][] can be used to schedule Node-API calls that
8011cb0ef41Sopenharmony_cirequire access to the JavaScript engine's state to run after the current
8021cb0ef41Sopenharmony_cigarbage collection cycle has completed.
8031cb0ef41Sopenharmony_ci
8041cb0ef41Sopenharmony_ciIn the case of [`node_api_create_external_string_latin1`][] and
8051cb0ef41Sopenharmony_ci[`node_api_create_external_string_utf16`][] the `env` parameter may be null,
8061cb0ef41Sopenharmony_cibecause external strings can be collected during the latter part of environment
8071cb0ef41Sopenharmony_cishutdown.
8081cb0ef41Sopenharmony_ci
8091cb0ef41Sopenharmony_ciChange History:
8101cb0ef41Sopenharmony_ci
8111cb0ef41Sopenharmony_ci* experimental (`NAPI_EXPERIMENTAL`):
8121cb0ef41Sopenharmony_ci
8131cb0ef41Sopenharmony_ci  Only Node-API calls that accept a `node_api_nogc_env` as their first
8141cb0ef41Sopenharmony_ci  parameter may be called, otherwise the application will be terminated with an
8151cb0ef41Sopenharmony_ci  appropriate error message. This feature can be turned off by defining
8161cb0ef41Sopenharmony_ci  `NODE_API_EXPERIMENTAL_NOGC_ENV_OPT_OUT`.
8171cb0ef41Sopenharmony_ci
8181cb0ef41Sopenharmony_ci#### `napi_finalize`
8191cb0ef41Sopenharmony_ci
8201cb0ef41Sopenharmony_ci<!-- YAML
8211cb0ef41Sopenharmony_ciadded: v8.0.0
8221cb0ef41Sopenharmony_cinapiVersion: 1
8231cb0ef41Sopenharmony_ci-->
8241cb0ef41Sopenharmony_ci
8251cb0ef41Sopenharmony_ciFunction pointer type for add-on provided function that allow the user to
8261cb0ef41Sopenharmony_cischedule a group of calls to Node-APIs in response to a garbage collection
8271cb0ef41Sopenharmony_cievent, after the garbage collection cycle has completed. These function
8281cb0ef41Sopenharmony_cipointers can be used with [`node_api_post_finalizer`][].
8291cb0ef41Sopenharmony_ci
8301cb0ef41Sopenharmony_ci```c
8311cb0ef41Sopenharmony_citypedef void (*napi_finalize)(napi_env env,
8321cb0ef41Sopenharmony_ci                              void* finalize_data,
8331cb0ef41Sopenharmony_ci                              void* finalize_hint);
8341cb0ef41Sopenharmony_ci```
8351cb0ef41Sopenharmony_ci
8361cb0ef41Sopenharmony_ciChange History:
8371cb0ef41Sopenharmony_ci
8381cb0ef41Sopenharmony_ci* experimental (`NAPI_EXPERIMENTAL` is defined):
8391cb0ef41Sopenharmony_ci
8401cb0ef41Sopenharmony_ci  A function of this type may no longer be used as a finalizer, except with
8411cb0ef41Sopenharmony_ci  [`node_api_post_finalizer`][]. [`node_api_nogc_finalize`][] must be used
8421cb0ef41Sopenharmony_ci  instead. This feature can be turned off by defining
8431cb0ef41Sopenharmony_ci  `NODE_API_EXPERIMENTAL_NOGC_ENV_OPT_OUT`.
8441cb0ef41Sopenharmony_ci
8451cb0ef41Sopenharmony_ci#### `napi_async_execute_callback`
8461cb0ef41Sopenharmony_ci
8471cb0ef41Sopenharmony_ci<!-- YAML
8481cb0ef41Sopenharmony_ciadded: v8.0.0
8491cb0ef41Sopenharmony_cinapiVersion: 1
8501cb0ef41Sopenharmony_ci-->
8511cb0ef41Sopenharmony_ci
8521cb0ef41Sopenharmony_ciFunction pointer used with functions that support asynchronous
8531cb0ef41Sopenharmony_cioperations. Callback functions must satisfy the following signature:
8541cb0ef41Sopenharmony_ci
8551cb0ef41Sopenharmony_ci```c
8561cb0ef41Sopenharmony_citypedef void (*napi_async_execute_callback)(napi_env env, void* data);
8571cb0ef41Sopenharmony_ci```
8581cb0ef41Sopenharmony_ci
8591cb0ef41Sopenharmony_ciImplementations of this function must avoid making Node-API calls that execute
8601cb0ef41Sopenharmony_ciJavaScript or interact with JavaScript objects. Node-API calls should be in the
8611cb0ef41Sopenharmony_ci`napi_async_complete_callback` instead. Do not use the `napi_env` parameter as
8621cb0ef41Sopenharmony_ciit will likely result in execution of JavaScript.
8631cb0ef41Sopenharmony_ci
8641cb0ef41Sopenharmony_ci#### `napi_async_complete_callback`
8651cb0ef41Sopenharmony_ci
8661cb0ef41Sopenharmony_ci<!-- YAML
8671cb0ef41Sopenharmony_ciadded: v8.0.0
8681cb0ef41Sopenharmony_cinapiVersion: 1
8691cb0ef41Sopenharmony_ci-->
8701cb0ef41Sopenharmony_ci
8711cb0ef41Sopenharmony_ciFunction pointer used with functions that support asynchronous
8721cb0ef41Sopenharmony_cioperations. Callback functions must satisfy the following signature:
8731cb0ef41Sopenharmony_ci
8741cb0ef41Sopenharmony_ci```c
8751cb0ef41Sopenharmony_citypedef void (*napi_async_complete_callback)(napi_env env,
8761cb0ef41Sopenharmony_ci                                             napi_status status,
8771cb0ef41Sopenharmony_ci                                             void* data);
8781cb0ef41Sopenharmony_ci```
8791cb0ef41Sopenharmony_ci
8801cb0ef41Sopenharmony_ciUnless for reasons discussed in [Object Lifetime Management][], creating a
8811cb0ef41Sopenharmony_cihandle and/or callback scope inside the function body is not necessary.
8821cb0ef41Sopenharmony_ci
8831cb0ef41Sopenharmony_ci#### `napi_threadsafe_function_call_js`
8841cb0ef41Sopenharmony_ci
8851cb0ef41Sopenharmony_ci<!-- YAML
8861cb0ef41Sopenharmony_ciadded: v10.6.0
8871cb0ef41Sopenharmony_cinapiVersion: 4
8881cb0ef41Sopenharmony_ci-->
8891cb0ef41Sopenharmony_ci
8901cb0ef41Sopenharmony_ciFunction pointer used with asynchronous thread-safe function calls. The callback
8911cb0ef41Sopenharmony_ciwill be called on the main thread. Its purpose is to use a data item arriving
8921cb0ef41Sopenharmony_civia the queue from one of the secondary threads to construct the parameters
8931cb0ef41Sopenharmony_cinecessary for a call into JavaScript, usually via `napi_call_function`, and then
8941cb0ef41Sopenharmony_cimake the call into JavaScript.
8951cb0ef41Sopenharmony_ci
8961cb0ef41Sopenharmony_ciThe data arriving from the secondary thread via the queue is given in the `data`
8971cb0ef41Sopenharmony_ciparameter and the JavaScript function to call is given in the `js_callback`
8981cb0ef41Sopenharmony_ciparameter.
8991cb0ef41Sopenharmony_ci
9001cb0ef41Sopenharmony_ciNode-API sets up the environment prior to calling this callback, so it is
9011cb0ef41Sopenharmony_cisufficient to call the JavaScript function via `napi_call_function` rather than
9021cb0ef41Sopenharmony_civia `napi_make_callback`.
9031cb0ef41Sopenharmony_ci
9041cb0ef41Sopenharmony_ciCallback functions must satisfy the following signature:
9051cb0ef41Sopenharmony_ci
9061cb0ef41Sopenharmony_ci```c
9071cb0ef41Sopenharmony_citypedef void (*napi_threadsafe_function_call_js)(napi_env env,
9081cb0ef41Sopenharmony_ci                                                 napi_value js_callback,
9091cb0ef41Sopenharmony_ci                                                 void* context,
9101cb0ef41Sopenharmony_ci                                                 void* data);
9111cb0ef41Sopenharmony_ci```
9121cb0ef41Sopenharmony_ci
9131cb0ef41Sopenharmony_ci* `[in] env`: The environment to use for API calls, or `NULL` if the thread-safe
9141cb0ef41Sopenharmony_ci  function is being torn down and `data` may need to be freed.
9151cb0ef41Sopenharmony_ci* `[in] js_callback`: The JavaScript function to call, or `NULL` if the
9161cb0ef41Sopenharmony_ci  thread-safe function is being torn down and `data` may need to be freed. It
9171cb0ef41Sopenharmony_ci  may also be `NULL` if the thread-safe function was created without
9181cb0ef41Sopenharmony_ci  `js_callback`.
9191cb0ef41Sopenharmony_ci* `[in] context`: The optional data with which the thread-safe function was
9201cb0ef41Sopenharmony_ci  created.
9211cb0ef41Sopenharmony_ci* `[in] data`: Data created by the secondary thread. It is the responsibility of
9221cb0ef41Sopenharmony_ci  the callback to convert this native data to JavaScript values (with Node-API
9231cb0ef41Sopenharmony_ci  functions) that can be passed as parameters when `js_callback` is invoked.
9241cb0ef41Sopenharmony_ci  This pointer is managed entirely by the threads and this callback. Thus this
9251cb0ef41Sopenharmony_ci  callback should free the data.
9261cb0ef41Sopenharmony_ci
9271cb0ef41Sopenharmony_ciUnless for reasons discussed in [Object Lifetime Management][], creating a
9281cb0ef41Sopenharmony_cihandle and/or callback scope inside the function body is not necessary.
9291cb0ef41Sopenharmony_ci
9301cb0ef41Sopenharmony_ci#### `napi_cleanup_hook`
9311cb0ef41Sopenharmony_ci
9321cb0ef41Sopenharmony_ci<!-- YAML
9331cb0ef41Sopenharmony_ciadded: v18.13.0
9341cb0ef41Sopenharmony_cinapiVersion: 3
9351cb0ef41Sopenharmony_ci-->
9361cb0ef41Sopenharmony_ci
9371cb0ef41Sopenharmony_ciFunction pointer used with [`napi_add_env_cleanup_hook`][]. It will be called
9381cb0ef41Sopenharmony_ciwhen the environment is being torn down.
9391cb0ef41Sopenharmony_ci
9401cb0ef41Sopenharmony_ciCallback functions must satisfy the following signature:
9411cb0ef41Sopenharmony_ci
9421cb0ef41Sopenharmony_ci```c
9431cb0ef41Sopenharmony_citypedef void (*napi_cleanup_hook)(void* data);
9441cb0ef41Sopenharmony_ci```
9451cb0ef41Sopenharmony_ci
9461cb0ef41Sopenharmony_ci* `[in] data`: The data that was passed to [`napi_add_env_cleanup_hook`][].
9471cb0ef41Sopenharmony_ci
9481cb0ef41Sopenharmony_ci#### `napi_async_cleanup_hook`
9491cb0ef41Sopenharmony_ci
9501cb0ef41Sopenharmony_ci<!-- YAML
9511cb0ef41Sopenharmony_ciadded:
9521cb0ef41Sopenharmony_ci  - v14.10.0
9531cb0ef41Sopenharmony_ci  - v12.19.0
9541cb0ef41Sopenharmony_ci-->
9551cb0ef41Sopenharmony_ci
9561cb0ef41Sopenharmony_ciFunction pointer used with [`napi_add_async_cleanup_hook`][]. It will be called
9571cb0ef41Sopenharmony_ciwhen the environment is being torn down.
9581cb0ef41Sopenharmony_ci
9591cb0ef41Sopenharmony_ciCallback functions must satisfy the following signature:
9601cb0ef41Sopenharmony_ci
9611cb0ef41Sopenharmony_ci```c
9621cb0ef41Sopenharmony_citypedef void (*napi_async_cleanup_hook)(napi_async_cleanup_hook_handle handle,
9631cb0ef41Sopenharmony_ci                                        void* data);
9641cb0ef41Sopenharmony_ci```
9651cb0ef41Sopenharmony_ci
9661cb0ef41Sopenharmony_ci* `[in] handle`: The handle that must be passed to
9671cb0ef41Sopenharmony_ci  [`napi_remove_async_cleanup_hook`][] after completion of the asynchronous
9681cb0ef41Sopenharmony_ci  cleanup.
9691cb0ef41Sopenharmony_ci* `[in] data`: The data that was passed to [`napi_add_async_cleanup_hook`][].
9701cb0ef41Sopenharmony_ci
9711cb0ef41Sopenharmony_ciThe body of the function should initiate the asynchronous cleanup actions at the
9721cb0ef41Sopenharmony_ciend of which `handle` must be passed in a call to
9731cb0ef41Sopenharmony_ci[`napi_remove_async_cleanup_hook`][].
9741cb0ef41Sopenharmony_ci
9751cb0ef41Sopenharmony_ci## Error handling
9761cb0ef41Sopenharmony_ci
9771cb0ef41Sopenharmony_ciNode-API uses both return values and JavaScript exceptions for error handling.
9781cb0ef41Sopenharmony_ciThe following sections explain the approach for each case.
9791cb0ef41Sopenharmony_ci
9801cb0ef41Sopenharmony_ci### Return values
9811cb0ef41Sopenharmony_ci
9821cb0ef41Sopenharmony_ciAll of the Node-API functions share the same error handling pattern. The
9831cb0ef41Sopenharmony_cireturn type of all API functions is `napi_status`.
9841cb0ef41Sopenharmony_ci
9851cb0ef41Sopenharmony_ciThe return value will be `napi_ok` if the request was successful and
9861cb0ef41Sopenharmony_cino uncaught JavaScript exception was thrown. If an error occurred AND
9871cb0ef41Sopenharmony_cian exception was thrown, the `napi_status` value for the error
9881cb0ef41Sopenharmony_ciwill be returned. If an exception was thrown, and no error occurred,
9891cb0ef41Sopenharmony_ci`napi_pending_exception` will be returned.
9901cb0ef41Sopenharmony_ci
9911cb0ef41Sopenharmony_ciIn cases where a return value other than `napi_ok` or
9921cb0ef41Sopenharmony_ci`napi_pending_exception` is returned, [`napi_is_exception_pending`][]
9931cb0ef41Sopenharmony_cimust be called to check if an exception is pending.
9941cb0ef41Sopenharmony_ciSee the section on exceptions for more details.
9951cb0ef41Sopenharmony_ci
9961cb0ef41Sopenharmony_ciThe full set of possible `napi_status` values is defined
9971cb0ef41Sopenharmony_ciin `napi_api_types.h`.
9981cb0ef41Sopenharmony_ci
9991cb0ef41Sopenharmony_ciThe `napi_status` return value provides a VM-independent representation of
10001cb0ef41Sopenharmony_cithe error which occurred. In some cases it is useful to be able to get
10011cb0ef41Sopenharmony_cimore detailed information, including a string representing the error as well as
10021cb0ef41Sopenharmony_ciVM (engine)-specific information.
10031cb0ef41Sopenharmony_ci
10041cb0ef41Sopenharmony_ciIn order to retrieve this information [`napi_get_last_error_info`][]
10051cb0ef41Sopenharmony_ciis provided which returns a `napi_extended_error_info` structure.
10061cb0ef41Sopenharmony_ciThe format of the `napi_extended_error_info` structure is as follows:
10071cb0ef41Sopenharmony_ci
10081cb0ef41Sopenharmony_ci<!-- YAML
10091cb0ef41Sopenharmony_ciadded: v8.0.0
10101cb0ef41Sopenharmony_cinapiVersion: 1
10111cb0ef41Sopenharmony_ci-->
10121cb0ef41Sopenharmony_ci
10131cb0ef41Sopenharmony_ci```c
10141cb0ef41Sopenharmony_citypedef struct napi_extended_error_info {
10151cb0ef41Sopenharmony_ci  const char* error_message;
10161cb0ef41Sopenharmony_ci  void* engine_reserved;
10171cb0ef41Sopenharmony_ci  uint32_t engine_error_code;
10181cb0ef41Sopenharmony_ci  napi_status error_code;
10191cb0ef41Sopenharmony_ci};
10201cb0ef41Sopenharmony_ci```
10211cb0ef41Sopenharmony_ci
10221cb0ef41Sopenharmony_ci* `error_message`: Textual representation of the error that occurred.
10231cb0ef41Sopenharmony_ci* `engine_reserved`: Opaque handle reserved for engine use only.
10241cb0ef41Sopenharmony_ci* `engine_error_code`: VM specific error code.
10251cb0ef41Sopenharmony_ci* `error_code`: Node-API status code for the last error.
10261cb0ef41Sopenharmony_ci
10271cb0ef41Sopenharmony_ci[`napi_get_last_error_info`][] returns the information for the last
10281cb0ef41Sopenharmony_ciNode-API call that was made.
10291cb0ef41Sopenharmony_ci
10301cb0ef41Sopenharmony_ciDo not rely on the content or format of any of the extended information as it
10311cb0ef41Sopenharmony_ciis not subject to SemVer and may change at any time. It is intended only for
10321cb0ef41Sopenharmony_cilogging purposes.
10331cb0ef41Sopenharmony_ci
10341cb0ef41Sopenharmony_ci#### `napi_get_last_error_info`
10351cb0ef41Sopenharmony_ci
10361cb0ef41Sopenharmony_ci<!-- YAML
10371cb0ef41Sopenharmony_ciadded: v8.0.0
10381cb0ef41Sopenharmony_cinapiVersion: 1
10391cb0ef41Sopenharmony_ci-->
10401cb0ef41Sopenharmony_ci
10411cb0ef41Sopenharmony_ci```c
10421cb0ef41Sopenharmony_cinapi_status
10431cb0ef41Sopenharmony_cinapi_get_last_error_info(node_api_nogc_env env,
10441cb0ef41Sopenharmony_ci                         const napi_extended_error_info** result);
10451cb0ef41Sopenharmony_ci```
10461cb0ef41Sopenharmony_ci
10471cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
10481cb0ef41Sopenharmony_ci* `[out] result`: The `napi_extended_error_info` structure with more
10491cb0ef41Sopenharmony_ci  information about the error.
10501cb0ef41Sopenharmony_ci
10511cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
10521cb0ef41Sopenharmony_ci
10531cb0ef41Sopenharmony_ciThis API retrieves a `napi_extended_error_info` structure with information
10541cb0ef41Sopenharmony_ciabout the last error that occurred.
10551cb0ef41Sopenharmony_ci
10561cb0ef41Sopenharmony_ciThe content of the `napi_extended_error_info` returned is only valid up until
10571cb0ef41Sopenharmony_cia Node-API function is called on the same `env`. This includes a call to
10581cb0ef41Sopenharmony_ci`napi_is_exception_pending` so it may often be necessary to make a copy
10591cb0ef41Sopenharmony_ciof the information so that it can be used later. The pointer returned
10601cb0ef41Sopenharmony_ciin `error_message` points to a statically-defined string so it is safe to use
10611cb0ef41Sopenharmony_cithat pointer if you have copied it out of the `error_message` field (which will
10621cb0ef41Sopenharmony_cibe overwritten) before another Node-API function was called.
10631cb0ef41Sopenharmony_ci
10641cb0ef41Sopenharmony_ciDo not rely on the content or format of any of the extended information as it
10651cb0ef41Sopenharmony_ciis not subject to SemVer and may change at any time. It is intended only for
10661cb0ef41Sopenharmony_cilogging purposes.
10671cb0ef41Sopenharmony_ci
10681cb0ef41Sopenharmony_ciThis API can be called even if there is a pending JavaScript exception.
10691cb0ef41Sopenharmony_ci
10701cb0ef41Sopenharmony_ci### Exceptions
10711cb0ef41Sopenharmony_ci
10721cb0ef41Sopenharmony_ciAny Node-API function call may result in a pending JavaScript exception. This is
10731cb0ef41Sopenharmony_cithe case for any of the API functions, even those that may not cause the
10741cb0ef41Sopenharmony_ciexecution of JavaScript.
10751cb0ef41Sopenharmony_ci
10761cb0ef41Sopenharmony_ciIf the `napi_status` returned by a function is `napi_ok` then no
10771cb0ef41Sopenharmony_ciexception is pending and no additional action is required. If the
10781cb0ef41Sopenharmony_ci`napi_status` returned is anything other than `napi_ok` or
10791cb0ef41Sopenharmony_ci`napi_pending_exception`, in order to try to recover and continue
10801cb0ef41Sopenharmony_ciinstead of simply returning immediately, [`napi_is_exception_pending`][]
10811cb0ef41Sopenharmony_cimust be called in order to determine if an exception is pending or not.
10821cb0ef41Sopenharmony_ci
10831cb0ef41Sopenharmony_ciIn many cases when a Node-API function is called and an exception is
10841cb0ef41Sopenharmony_cialready pending, the function will return immediately with a
10851cb0ef41Sopenharmony_ci`napi_status` of `napi_pending_exception`. However, this is not the case
10861cb0ef41Sopenharmony_cifor all functions. Node-API allows a subset of the functions to be
10871cb0ef41Sopenharmony_cicalled to allow for some minimal cleanup before returning to JavaScript.
10881cb0ef41Sopenharmony_ciIn that case, `napi_status` will reflect the status for the function. It
10891cb0ef41Sopenharmony_ciwill not reflect previous pending exceptions. To avoid confusion, check
10901cb0ef41Sopenharmony_cithe error status after every function call.
10911cb0ef41Sopenharmony_ci
10921cb0ef41Sopenharmony_ciWhen an exception is pending one of two approaches can be employed.
10931cb0ef41Sopenharmony_ci
10941cb0ef41Sopenharmony_ciThe first approach is to do any appropriate cleanup and then return so that
10951cb0ef41Sopenharmony_ciexecution will return to JavaScript. As part of the transition back to
10961cb0ef41Sopenharmony_ciJavaScript, the exception will be thrown at the point in the JavaScript
10971cb0ef41Sopenharmony_cicode where the native method was invoked. The behavior of most Node-API calls
10981cb0ef41Sopenharmony_ciis unspecified while an exception is pending, and many will simply return
10991cb0ef41Sopenharmony_ci`napi_pending_exception`, so do as little as possible and then return to
11001cb0ef41Sopenharmony_ciJavaScript where the exception can be handled.
11011cb0ef41Sopenharmony_ci
11021cb0ef41Sopenharmony_ciThe second approach is to try to handle the exception. There will be cases
11031cb0ef41Sopenharmony_ciwhere the native code can catch the exception, take the appropriate action,
11041cb0ef41Sopenharmony_ciand then continue. This is only recommended in specific cases
11051cb0ef41Sopenharmony_ciwhere it is known that the exception can be safely handled. In these
11061cb0ef41Sopenharmony_cicases [`napi_get_and_clear_last_exception`][] can be used to get and
11071cb0ef41Sopenharmony_ciclear the exception. On success, result will contain the handle to
11081cb0ef41Sopenharmony_cithe last JavaScript `Object` thrown. If it is determined, after
11091cb0ef41Sopenharmony_ciretrieving the exception, the exception cannot be handled after all
11101cb0ef41Sopenharmony_ciit can be re-thrown it with [`napi_throw`][] where error is the
11111cb0ef41Sopenharmony_ciJavaScript value to be thrown.
11121cb0ef41Sopenharmony_ci
11131cb0ef41Sopenharmony_ciThe following utility functions are also available in case native code
11141cb0ef41Sopenharmony_cineeds to throw an exception or determine if a `napi_value` is an instance
11151cb0ef41Sopenharmony_ciof a JavaScript `Error` object: [`napi_throw_error`][],
11161cb0ef41Sopenharmony_ci[`napi_throw_type_error`][], [`napi_throw_range_error`][], [`node_api_throw_syntax_error`][] and [`napi_is_error`][].
11171cb0ef41Sopenharmony_ci
11181cb0ef41Sopenharmony_ciThe following utility functions are also available in case native
11191cb0ef41Sopenharmony_cicode needs to create an `Error` object: [`napi_create_error`][],
11201cb0ef41Sopenharmony_ci[`napi_create_type_error`][], [`napi_create_range_error`][] and [`node_api_create_syntax_error`][],
11211cb0ef41Sopenharmony_ciwhere result is the `napi_value` that refers to the newly created
11221cb0ef41Sopenharmony_ciJavaScript `Error` object.
11231cb0ef41Sopenharmony_ci
11241cb0ef41Sopenharmony_ciThe Node.js project is adding error codes to all of the errors
11251cb0ef41Sopenharmony_cigenerated internally. The goal is for applications to use these
11261cb0ef41Sopenharmony_cierror codes for all error checking. The associated error messages
11271cb0ef41Sopenharmony_ciwill remain, but will only be meant to be used for logging and
11281cb0ef41Sopenharmony_cidisplay with the expectation that the message can change without
11291cb0ef41Sopenharmony_ciSemVer applying. In order to support this model with Node-API, both
11301cb0ef41Sopenharmony_ciin internal functionality and for module specific functionality
11311cb0ef41Sopenharmony_ci(as its good practice), the `throw_` and `create_` functions
11321cb0ef41Sopenharmony_citake an optional code parameter which is the string for the code
11331cb0ef41Sopenharmony_cito be added to the error object. If the optional parameter is `NULL`
11341cb0ef41Sopenharmony_cithen no code will be associated with the error. If a code is provided,
11351cb0ef41Sopenharmony_cithe name associated with the error is also updated to be:
11361cb0ef41Sopenharmony_ci
11371cb0ef41Sopenharmony_ci```text
11381cb0ef41Sopenharmony_cioriginalName [code]
11391cb0ef41Sopenharmony_ci```
11401cb0ef41Sopenharmony_ci
11411cb0ef41Sopenharmony_ciwhere `originalName` is the original name associated with the error
11421cb0ef41Sopenharmony_ciand `code` is the code that was provided. For example, if the code
11431cb0ef41Sopenharmony_ciis `'ERR_ERROR_1'` and a `TypeError` is being created the name will be:
11441cb0ef41Sopenharmony_ci
11451cb0ef41Sopenharmony_ci```text
11461cb0ef41Sopenharmony_ciTypeError [ERR_ERROR_1]
11471cb0ef41Sopenharmony_ci```
11481cb0ef41Sopenharmony_ci
11491cb0ef41Sopenharmony_ci#### `napi_throw`
11501cb0ef41Sopenharmony_ci
11511cb0ef41Sopenharmony_ci<!-- YAML
11521cb0ef41Sopenharmony_ciadded: v8.0.0
11531cb0ef41Sopenharmony_cinapiVersion: 1
11541cb0ef41Sopenharmony_ci-->
11551cb0ef41Sopenharmony_ci
11561cb0ef41Sopenharmony_ci```c
11571cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_throw(napi_env env, napi_value error);
11581cb0ef41Sopenharmony_ci```
11591cb0ef41Sopenharmony_ci
11601cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
11611cb0ef41Sopenharmony_ci* `[in] error`: The JavaScript value to be thrown.
11621cb0ef41Sopenharmony_ci
11631cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
11641cb0ef41Sopenharmony_ci
11651cb0ef41Sopenharmony_ciThis API throws the JavaScript value provided.
11661cb0ef41Sopenharmony_ci
11671cb0ef41Sopenharmony_ci#### `napi_throw_error`
11681cb0ef41Sopenharmony_ci
11691cb0ef41Sopenharmony_ci<!-- YAML
11701cb0ef41Sopenharmony_ciadded: v8.0.0
11711cb0ef41Sopenharmony_cinapiVersion: 1
11721cb0ef41Sopenharmony_ci-->
11731cb0ef41Sopenharmony_ci
11741cb0ef41Sopenharmony_ci```c
11751cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_throw_error(napi_env env,
11761cb0ef41Sopenharmony_ci                                         const char* code,
11771cb0ef41Sopenharmony_ci                                         const char* msg);
11781cb0ef41Sopenharmony_ci```
11791cb0ef41Sopenharmony_ci
11801cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
11811cb0ef41Sopenharmony_ci* `[in] code`: Optional error code to be set on the error.
11821cb0ef41Sopenharmony_ci* `[in] msg`: C string representing the text to be associated with the error.
11831cb0ef41Sopenharmony_ci
11841cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
11851cb0ef41Sopenharmony_ci
11861cb0ef41Sopenharmony_ciThis API throws a JavaScript `Error` with the text provided.
11871cb0ef41Sopenharmony_ci
11881cb0ef41Sopenharmony_ci#### `napi_throw_type_error`
11891cb0ef41Sopenharmony_ci
11901cb0ef41Sopenharmony_ci<!-- YAML
11911cb0ef41Sopenharmony_ciadded: v8.0.0
11921cb0ef41Sopenharmony_cinapiVersion: 1
11931cb0ef41Sopenharmony_ci-->
11941cb0ef41Sopenharmony_ci
11951cb0ef41Sopenharmony_ci```c
11961cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_throw_type_error(napi_env env,
11971cb0ef41Sopenharmony_ci                                              const char* code,
11981cb0ef41Sopenharmony_ci                                              const char* msg);
11991cb0ef41Sopenharmony_ci```
12001cb0ef41Sopenharmony_ci
12011cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
12021cb0ef41Sopenharmony_ci* `[in] code`: Optional error code to be set on the error.
12031cb0ef41Sopenharmony_ci* `[in] msg`: C string representing the text to be associated with the error.
12041cb0ef41Sopenharmony_ci
12051cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
12061cb0ef41Sopenharmony_ci
12071cb0ef41Sopenharmony_ciThis API throws a JavaScript `TypeError` with the text provided.
12081cb0ef41Sopenharmony_ci
12091cb0ef41Sopenharmony_ci#### `napi_throw_range_error`
12101cb0ef41Sopenharmony_ci
12111cb0ef41Sopenharmony_ci<!-- YAML
12121cb0ef41Sopenharmony_ciadded: v8.0.0
12131cb0ef41Sopenharmony_cinapiVersion: 1
12141cb0ef41Sopenharmony_ci-->
12151cb0ef41Sopenharmony_ci
12161cb0ef41Sopenharmony_ci```c
12171cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_throw_range_error(napi_env env,
12181cb0ef41Sopenharmony_ci                                               const char* code,
12191cb0ef41Sopenharmony_ci                                               const char* msg);
12201cb0ef41Sopenharmony_ci```
12211cb0ef41Sopenharmony_ci
12221cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
12231cb0ef41Sopenharmony_ci* `[in] code`: Optional error code to be set on the error.
12241cb0ef41Sopenharmony_ci* `[in] msg`: C string representing the text to be associated with the error.
12251cb0ef41Sopenharmony_ci
12261cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
12271cb0ef41Sopenharmony_ci
12281cb0ef41Sopenharmony_ciThis API throws a JavaScript `RangeError` with the text provided.
12291cb0ef41Sopenharmony_ci
12301cb0ef41Sopenharmony_ci#### `node_api_throw_syntax_error`
12311cb0ef41Sopenharmony_ci
12321cb0ef41Sopenharmony_ci<!-- YAML
12331cb0ef41Sopenharmony_ciadded:
12341cb0ef41Sopenharmony_ci  - v17.2.0
12351cb0ef41Sopenharmony_ci  - v16.14.0
12361cb0ef41Sopenharmony_cinapiVersion: 9
12371cb0ef41Sopenharmony_ci-->
12381cb0ef41Sopenharmony_ci
12391cb0ef41Sopenharmony_ci```c
12401cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status node_api_throw_syntax_error(napi_env env,
12411cb0ef41Sopenharmony_ci                                                    const char* code,
12421cb0ef41Sopenharmony_ci                                                    const char* msg);
12431cb0ef41Sopenharmony_ci```
12441cb0ef41Sopenharmony_ci
12451cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
12461cb0ef41Sopenharmony_ci* `[in] code`: Optional error code to be set on the error.
12471cb0ef41Sopenharmony_ci* `[in] msg`: C string representing the text to be associated with the error.
12481cb0ef41Sopenharmony_ci
12491cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
12501cb0ef41Sopenharmony_ci
12511cb0ef41Sopenharmony_ciThis API throws a JavaScript `SyntaxError` with the text provided.
12521cb0ef41Sopenharmony_ci
12531cb0ef41Sopenharmony_ci#### `napi_is_error`
12541cb0ef41Sopenharmony_ci
12551cb0ef41Sopenharmony_ci<!-- YAML
12561cb0ef41Sopenharmony_ciadded: v8.0.0
12571cb0ef41Sopenharmony_cinapiVersion: 1
12581cb0ef41Sopenharmony_ci-->
12591cb0ef41Sopenharmony_ci
12601cb0ef41Sopenharmony_ci```c
12611cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_is_error(napi_env env,
12621cb0ef41Sopenharmony_ci                                      napi_value value,
12631cb0ef41Sopenharmony_ci                                      bool* result);
12641cb0ef41Sopenharmony_ci```
12651cb0ef41Sopenharmony_ci
12661cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
12671cb0ef41Sopenharmony_ci* `[in] value`: The `napi_value` to be checked.
12681cb0ef41Sopenharmony_ci* `[out] result`: Boolean value that is set to true if `napi_value` represents
12691cb0ef41Sopenharmony_ci  an error, false otherwise.
12701cb0ef41Sopenharmony_ci
12711cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
12721cb0ef41Sopenharmony_ci
12731cb0ef41Sopenharmony_ciThis API queries a `napi_value` to check if it represents an error object.
12741cb0ef41Sopenharmony_ci
12751cb0ef41Sopenharmony_ci#### `napi_create_error`
12761cb0ef41Sopenharmony_ci
12771cb0ef41Sopenharmony_ci<!-- YAML
12781cb0ef41Sopenharmony_ciadded: v8.0.0
12791cb0ef41Sopenharmony_cinapiVersion: 1
12801cb0ef41Sopenharmony_ci-->
12811cb0ef41Sopenharmony_ci
12821cb0ef41Sopenharmony_ci```c
12831cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_create_error(napi_env env,
12841cb0ef41Sopenharmony_ci                                          napi_value code,
12851cb0ef41Sopenharmony_ci                                          napi_value msg,
12861cb0ef41Sopenharmony_ci                                          napi_value* result);
12871cb0ef41Sopenharmony_ci```
12881cb0ef41Sopenharmony_ci
12891cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
12901cb0ef41Sopenharmony_ci* `[in] code`: Optional `napi_value` with the string for the error code to be
12911cb0ef41Sopenharmony_ci  associated with the error.
12921cb0ef41Sopenharmony_ci* `[in] msg`: `napi_value` that references a JavaScript `string` to be used as
12931cb0ef41Sopenharmony_ci  the message for the `Error`.
12941cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing the error created.
12951cb0ef41Sopenharmony_ci
12961cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
12971cb0ef41Sopenharmony_ci
12981cb0ef41Sopenharmony_ciThis API returns a JavaScript `Error` with the text provided.
12991cb0ef41Sopenharmony_ci
13001cb0ef41Sopenharmony_ci#### `napi_create_type_error`
13011cb0ef41Sopenharmony_ci
13021cb0ef41Sopenharmony_ci<!-- YAML
13031cb0ef41Sopenharmony_ciadded: v8.0.0
13041cb0ef41Sopenharmony_cinapiVersion: 1
13051cb0ef41Sopenharmony_ci-->
13061cb0ef41Sopenharmony_ci
13071cb0ef41Sopenharmony_ci```c
13081cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_create_type_error(napi_env env,
13091cb0ef41Sopenharmony_ci                                               napi_value code,
13101cb0ef41Sopenharmony_ci                                               napi_value msg,
13111cb0ef41Sopenharmony_ci                                               napi_value* result);
13121cb0ef41Sopenharmony_ci```
13131cb0ef41Sopenharmony_ci
13141cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
13151cb0ef41Sopenharmony_ci* `[in] code`: Optional `napi_value` with the string for the error code to be
13161cb0ef41Sopenharmony_ci  associated with the error.
13171cb0ef41Sopenharmony_ci* `[in] msg`: `napi_value` that references a JavaScript `string` to be used as
13181cb0ef41Sopenharmony_ci  the message for the `Error`.
13191cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing the error created.
13201cb0ef41Sopenharmony_ci
13211cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
13221cb0ef41Sopenharmony_ci
13231cb0ef41Sopenharmony_ciThis API returns a JavaScript `TypeError` with the text provided.
13241cb0ef41Sopenharmony_ci
13251cb0ef41Sopenharmony_ci#### `napi_create_range_error`
13261cb0ef41Sopenharmony_ci
13271cb0ef41Sopenharmony_ci<!-- YAML
13281cb0ef41Sopenharmony_ciadded: v8.0.0
13291cb0ef41Sopenharmony_cinapiVersion: 1
13301cb0ef41Sopenharmony_ci-->
13311cb0ef41Sopenharmony_ci
13321cb0ef41Sopenharmony_ci```c
13331cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_create_range_error(napi_env env,
13341cb0ef41Sopenharmony_ci                                                napi_value code,
13351cb0ef41Sopenharmony_ci                                                napi_value msg,
13361cb0ef41Sopenharmony_ci                                                napi_value* result);
13371cb0ef41Sopenharmony_ci```
13381cb0ef41Sopenharmony_ci
13391cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
13401cb0ef41Sopenharmony_ci* `[in] code`: Optional `napi_value` with the string for the error code to be
13411cb0ef41Sopenharmony_ci  associated with the error.
13421cb0ef41Sopenharmony_ci* `[in] msg`: `napi_value` that references a JavaScript `string` to be used as
13431cb0ef41Sopenharmony_ci  the message for the `Error`.
13441cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing the error created.
13451cb0ef41Sopenharmony_ci
13461cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
13471cb0ef41Sopenharmony_ci
13481cb0ef41Sopenharmony_ciThis API returns a JavaScript `RangeError` with the text provided.
13491cb0ef41Sopenharmony_ci
13501cb0ef41Sopenharmony_ci#### `node_api_create_syntax_error`
13511cb0ef41Sopenharmony_ci
13521cb0ef41Sopenharmony_ci<!-- YAML
13531cb0ef41Sopenharmony_ciadded:
13541cb0ef41Sopenharmony_ci  - v17.2.0
13551cb0ef41Sopenharmony_ci  - v16.14.0
13561cb0ef41Sopenharmony_cinapiVersion: 9
13571cb0ef41Sopenharmony_ci-->
13581cb0ef41Sopenharmony_ci
13591cb0ef41Sopenharmony_ci```c
13601cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status node_api_create_syntax_error(napi_env env,
13611cb0ef41Sopenharmony_ci                                                     napi_value code,
13621cb0ef41Sopenharmony_ci                                                     napi_value msg,
13631cb0ef41Sopenharmony_ci                                                     napi_value* result);
13641cb0ef41Sopenharmony_ci```
13651cb0ef41Sopenharmony_ci
13661cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
13671cb0ef41Sopenharmony_ci* `[in] code`: Optional `napi_value` with the string for the error code to be
13681cb0ef41Sopenharmony_ci  associated with the error.
13691cb0ef41Sopenharmony_ci* `[in] msg`: `napi_value` that references a JavaScript `string` to be used as
13701cb0ef41Sopenharmony_ci  the message for the `Error`.
13711cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing the error created.
13721cb0ef41Sopenharmony_ci
13731cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
13741cb0ef41Sopenharmony_ci
13751cb0ef41Sopenharmony_ciThis API returns a JavaScript `SyntaxError` with the text provided.
13761cb0ef41Sopenharmony_ci
13771cb0ef41Sopenharmony_ci#### `napi_get_and_clear_last_exception`
13781cb0ef41Sopenharmony_ci
13791cb0ef41Sopenharmony_ci<!-- YAML
13801cb0ef41Sopenharmony_ciadded: v8.0.0
13811cb0ef41Sopenharmony_cinapiVersion: 1
13821cb0ef41Sopenharmony_ci-->
13831cb0ef41Sopenharmony_ci
13841cb0ef41Sopenharmony_ci```c
13851cb0ef41Sopenharmony_cinapi_status napi_get_and_clear_last_exception(napi_env env,
13861cb0ef41Sopenharmony_ci                                              napi_value* result);
13871cb0ef41Sopenharmony_ci```
13881cb0ef41Sopenharmony_ci
13891cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
13901cb0ef41Sopenharmony_ci* `[out] result`: The exception if one is pending, `NULL` otherwise.
13911cb0ef41Sopenharmony_ci
13921cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
13931cb0ef41Sopenharmony_ci
13941cb0ef41Sopenharmony_ciThis API can be called even if there is a pending JavaScript exception.
13951cb0ef41Sopenharmony_ci
13961cb0ef41Sopenharmony_ci#### `napi_is_exception_pending`
13971cb0ef41Sopenharmony_ci
13981cb0ef41Sopenharmony_ci<!-- YAML
13991cb0ef41Sopenharmony_ciadded: v8.0.0
14001cb0ef41Sopenharmony_cinapiVersion: 1
14011cb0ef41Sopenharmony_ci-->
14021cb0ef41Sopenharmony_ci
14031cb0ef41Sopenharmony_ci```c
14041cb0ef41Sopenharmony_cinapi_status napi_is_exception_pending(napi_env env, bool* result);
14051cb0ef41Sopenharmony_ci```
14061cb0ef41Sopenharmony_ci
14071cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
14081cb0ef41Sopenharmony_ci* `[out] result`: Boolean value that is set to true if an exception is pending.
14091cb0ef41Sopenharmony_ci
14101cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
14111cb0ef41Sopenharmony_ci
14121cb0ef41Sopenharmony_ciThis API can be called even if there is a pending JavaScript exception.
14131cb0ef41Sopenharmony_ci
14141cb0ef41Sopenharmony_ci#### `napi_fatal_exception`
14151cb0ef41Sopenharmony_ci
14161cb0ef41Sopenharmony_ci<!-- YAML
14171cb0ef41Sopenharmony_ciadded: v9.10.0
14181cb0ef41Sopenharmony_cinapiVersion: 3
14191cb0ef41Sopenharmony_ci-->
14201cb0ef41Sopenharmony_ci
14211cb0ef41Sopenharmony_ci```c
14221cb0ef41Sopenharmony_cinapi_status napi_fatal_exception(napi_env env, napi_value err);
14231cb0ef41Sopenharmony_ci```
14241cb0ef41Sopenharmony_ci
14251cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
14261cb0ef41Sopenharmony_ci* `[in] err`: The error that is passed to `'uncaughtException'`.
14271cb0ef41Sopenharmony_ci
14281cb0ef41Sopenharmony_ciTrigger an `'uncaughtException'` in JavaScript. Useful if an async
14291cb0ef41Sopenharmony_cicallback throws an exception with no way to recover.
14301cb0ef41Sopenharmony_ci
14311cb0ef41Sopenharmony_ci### Fatal errors
14321cb0ef41Sopenharmony_ci
14331cb0ef41Sopenharmony_ciIn the event of an unrecoverable error in a native addon, a fatal error can be
14341cb0ef41Sopenharmony_cithrown to immediately terminate the process.
14351cb0ef41Sopenharmony_ci
14361cb0ef41Sopenharmony_ci#### `napi_fatal_error`
14371cb0ef41Sopenharmony_ci
14381cb0ef41Sopenharmony_ci<!-- YAML
14391cb0ef41Sopenharmony_ciadded: v8.2.0
14401cb0ef41Sopenharmony_cinapiVersion: 1
14411cb0ef41Sopenharmony_ci-->
14421cb0ef41Sopenharmony_ci
14431cb0ef41Sopenharmony_ci```c
14441cb0ef41Sopenharmony_ciNAPI_NO_RETURN void napi_fatal_error(const char* location,
14451cb0ef41Sopenharmony_ci                                     size_t location_len,
14461cb0ef41Sopenharmony_ci                                     const char* message,
14471cb0ef41Sopenharmony_ci                                     size_t message_len);
14481cb0ef41Sopenharmony_ci```
14491cb0ef41Sopenharmony_ci
14501cb0ef41Sopenharmony_ci* `[in] location`: Optional location at which the error occurred.
14511cb0ef41Sopenharmony_ci* `[in] location_len`: The length of the location in bytes, or
14521cb0ef41Sopenharmony_ci  `NAPI_AUTO_LENGTH` if it is null-terminated.
14531cb0ef41Sopenharmony_ci* `[in] message`: The message associated with the error.
14541cb0ef41Sopenharmony_ci* `[in] message_len`: The length of the message in bytes, or `NAPI_AUTO_LENGTH`
14551cb0ef41Sopenharmony_ci  if it is null-terminated.
14561cb0ef41Sopenharmony_ci
14571cb0ef41Sopenharmony_ciThe function call does not return, the process will be terminated.
14581cb0ef41Sopenharmony_ci
14591cb0ef41Sopenharmony_ciThis API can be called even if there is a pending JavaScript exception.
14601cb0ef41Sopenharmony_ci
14611cb0ef41Sopenharmony_ci## Object lifetime management
14621cb0ef41Sopenharmony_ci
14631cb0ef41Sopenharmony_ciAs Node-API calls are made, handles to objects in the heap for the underlying
14641cb0ef41Sopenharmony_ciVM may be returned as `napi_values`. These handles must hold the
14651cb0ef41Sopenharmony_ciobjects 'live' until they are no longer required by the native code,
14661cb0ef41Sopenharmony_ciotherwise the objects could be collected before the native code was
14671cb0ef41Sopenharmony_cifinished using them.
14681cb0ef41Sopenharmony_ci
14691cb0ef41Sopenharmony_ciAs object handles are returned they are associated with a
14701cb0ef41Sopenharmony_ci'scope'. The lifespan for the default scope is tied to the lifespan
14711cb0ef41Sopenharmony_ciof the native method call. The result is that, by default, handles
14721cb0ef41Sopenharmony_ciremain valid and the objects associated with these handles will be
14731cb0ef41Sopenharmony_ciheld live for the lifespan of the native method call.
14741cb0ef41Sopenharmony_ci
14751cb0ef41Sopenharmony_ciIn many cases, however, it is necessary that the handles remain valid for
14761cb0ef41Sopenharmony_cieither a shorter or longer lifespan than that of the native method.
14771cb0ef41Sopenharmony_ciThe sections which follow describe the Node-API functions that can be used
14781cb0ef41Sopenharmony_cito change the handle lifespan from the default.
14791cb0ef41Sopenharmony_ci
14801cb0ef41Sopenharmony_ci### Making handle lifespan shorter than that of the native method
14811cb0ef41Sopenharmony_ci
14821cb0ef41Sopenharmony_ciIt is often necessary to make the lifespan of handles shorter than
14831cb0ef41Sopenharmony_cithe lifespan of a native method. For example, consider a native method
14841cb0ef41Sopenharmony_cithat has a loop which iterates through the elements in a large array:
14851cb0ef41Sopenharmony_ci
14861cb0ef41Sopenharmony_ci```c
14871cb0ef41Sopenharmony_cifor (int i = 0; i < 1000000; i++) {
14881cb0ef41Sopenharmony_ci  napi_value result;
14891cb0ef41Sopenharmony_ci  napi_status status = napi_get_element(env, object, i, &result);
14901cb0ef41Sopenharmony_ci  if (status != napi_ok) {
14911cb0ef41Sopenharmony_ci    break;
14921cb0ef41Sopenharmony_ci  }
14931cb0ef41Sopenharmony_ci  // do something with element
14941cb0ef41Sopenharmony_ci}
14951cb0ef41Sopenharmony_ci```
14961cb0ef41Sopenharmony_ci
14971cb0ef41Sopenharmony_ciThis would result in a large number of handles being created, consuming
14981cb0ef41Sopenharmony_cisubstantial resources. In addition, even though the native code could only
14991cb0ef41Sopenharmony_ciuse the most recent handle, all of the associated objects would also be
15001cb0ef41Sopenharmony_cikept alive since they all share the same scope.
15011cb0ef41Sopenharmony_ci
15021cb0ef41Sopenharmony_ciTo handle this case, Node-API provides the ability to establish a new 'scope' to
15031cb0ef41Sopenharmony_ciwhich newly created handles will be associated. Once those handles
15041cb0ef41Sopenharmony_ciare no longer required, the scope can be 'closed' and any handles associated
15051cb0ef41Sopenharmony_ciwith the scope are invalidated. The methods available to open/close scopes are
15061cb0ef41Sopenharmony_ci[`napi_open_handle_scope`][] and [`napi_close_handle_scope`][].
15071cb0ef41Sopenharmony_ci
15081cb0ef41Sopenharmony_ciNode-API only supports a single nested hierarchy of scopes. There is only one
15091cb0ef41Sopenharmony_ciactive scope at any time, and all new handles will be associated with that
15101cb0ef41Sopenharmony_ciscope while it is active. Scopes must be closed in the reverse order from
15111cb0ef41Sopenharmony_ciwhich they are opened. In addition, all scopes created within a native method
15121cb0ef41Sopenharmony_cimust be closed before returning from that method.
15131cb0ef41Sopenharmony_ci
15141cb0ef41Sopenharmony_ciTaking the earlier example, adding calls to [`napi_open_handle_scope`][] and
15151cb0ef41Sopenharmony_ci[`napi_close_handle_scope`][] would ensure that at most a single handle
15161cb0ef41Sopenharmony_ciis valid throughout the execution of the loop:
15171cb0ef41Sopenharmony_ci
15181cb0ef41Sopenharmony_ci```c
15191cb0ef41Sopenharmony_cifor (int i = 0; i < 1000000; i++) {
15201cb0ef41Sopenharmony_ci  napi_handle_scope scope;
15211cb0ef41Sopenharmony_ci  napi_status status = napi_open_handle_scope(env, &scope);
15221cb0ef41Sopenharmony_ci  if (status != napi_ok) {
15231cb0ef41Sopenharmony_ci    break;
15241cb0ef41Sopenharmony_ci  }
15251cb0ef41Sopenharmony_ci  napi_value result;
15261cb0ef41Sopenharmony_ci  status = napi_get_element(env, object, i, &result);
15271cb0ef41Sopenharmony_ci  if (status != napi_ok) {
15281cb0ef41Sopenharmony_ci    break;
15291cb0ef41Sopenharmony_ci  }
15301cb0ef41Sopenharmony_ci  // do something with element
15311cb0ef41Sopenharmony_ci  status = napi_close_handle_scope(env, scope);
15321cb0ef41Sopenharmony_ci  if (status != napi_ok) {
15331cb0ef41Sopenharmony_ci    break;
15341cb0ef41Sopenharmony_ci  }
15351cb0ef41Sopenharmony_ci}
15361cb0ef41Sopenharmony_ci```
15371cb0ef41Sopenharmony_ci
15381cb0ef41Sopenharmony_ciWhen nesting scopes, there are cases where a handle from an
15391cb0ef41Sopenharmony_ciinner scope needs to live beyond the lifespan of that scope. Node-API supports
15401cb0ef41Sopenharmony_cian 'escapable scope' in order to support this case. An escapable scope
15411cb0ef41Sopenharmony_ciallows one handle to be 'promoted' so that it 'escapes' the
15421cb0ef41Sopenharmony_cicurrent scope and the lifespan of the handle changes from the current
15431cb0ef41Sopenharmony_ciscope to that of the outer scope.
15441cb0ef41Sopenharmony_ci
15451cb0ef41Sopenharmony_ciThe methods available to open/close escapable scopes are
15461cb0ef41Sopenharmony_ci[`napi_open_escapable_handle_scope`][] and
15471cb0ef41Sopenharmony_ci[`napi_close_escapable_handle_scope`][].
15481cb0ef41Sopenharmony_ci
15491cb0ef41Sopenharmony_ciThe request to promote a handle is made through [`napi_escape_handle`][] which
15501cb0ef41Sopenharmony_cican only be called once.
15511cb0ef41Sopenharmony_ci
15521cb0ef41Sopenharmony_ci#### `napi_open_handle_scope`
15531cb0ef41Sopenharmony_ci
15541cb0ef41Sopenharmony_ci<!-- YAML
15551cb0ef41Sopenharmony_ciadded: v8.0.0
15561cb0ef41Sopenharmony_cinapiVersion: 1
15571cb0ef41Sopenharmony_ci-->
15581cb0ef41Sopenharmony_ci
15591cb0ef41Sopenharmony_ci```c
15601cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_open_handle_scope(napi_env env,
15611cb0ef41Sopenharmony_ci                                               napi_handle_scope* result);
15621cb0ef41Sopenharmony_ci```
15631cb0ef41Sopenharmony_ci
15641cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
15651cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing the new scope.
15661cb0ef41Sopenharmony_ci
15671cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
15681cb0ef41Sopenharmony_ci
15691cb0ef41Sopenharmony_ciThis API opens a new scope.
15701cb0ef41Sopenharmony_ci
15711cb0ef41Sopenharmony_ci#### `napi_close_handle_scope`
15721cb0ef41Sopenharmony_ci
15731cb0ef41Sopenharmony_ci<!-- YAML
15741cb0ef41Sopenharmony_ciadded: v8.0.0
15751cb0ef41Sopenharmony_cinapiVersion: 1
15761cb0ef41Sopenharmony_ci-->
15771cb0ef41Sopenharmony_ci
15781cb0ef41Sopenharmony_ci```c
15791cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_close_handle_scope(napi_env env,
15801cb0ef41Sopenharmony_ci                                                napi_handle_scope scope);
15811cb0ef41Sopenharmony_ci```
15821cb0ef41Sopenharmony_ci
15831cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
15841cb0ef41Sopenharmony_ci* `[in] scope`: `napi_value` representing the scope to be closed.
15851cb0ef41Sopenharmony_ci
15861cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
15871cb0ef41Sopenharmony_ci
15881cb0ef41Sopenharmony_ciThis API closes the scope passed in. Scopes must be closed in the
15891cb0ef41Sopenharmony_cireverse order from which they were created.
15901cb0ef41Sopenharmony_ci
15911cb0ef41Sopenharmony_ciThis API can be called even if there is a pending JavaScript exception.
15921cb0ef41Sopenharmony_ci
15931cb0ef41Sopenharmony_ci#### `napi_open_escapable_handle_scope`
15941cb0ef41Sopenharmony_ci
15951cb0ef41Sopenharmony_ci<!-- YAML
15961cb0ef41Sopenharmony_ciadded: v8.0.0
15971cb0ef41Sopenharmony_cinapiVersion: 1
15981cb0ef41Sopenharmony_ci-->
15991cb0ef41Sopenharmony_ci
16001cb0ef41Sopenharmony_ci```c
16011cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status
16021cb0ef41Sopenharmony_ci    napi_open_escapable_handle_scope(napi_env env,
16031cb0ef41Sopenharmony_ci                                     napi_handle_scope* result);
16041cb0ef41Sopenharmony_ci```
16051cb0ef41Sopenharmony_ci
16061cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
16071cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing the new scope.
16081cb0ef41Sopenharmony_ci
16091cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
16101cb0ef41Sopenharmony_ci
16111cb0ef41Sopenharmony_ciThis API opens a new scope from which one object can be promoted
16121cb0ef41Sopenharmony_cito the outer scope.
16131cb0ef41Sopenharmony_ci
16141cb0ef41Sopenharmony_ci#### `napi_close_escapable_handle_scope`
16151cb0ef41Sopenharmony_ci
16161cb0ef41Sopenharmony_ci<!-- YAML
16171cb0ef41Sopenharmony_ciadded: v8.0.0
16181cb0ef41Sopenharmony_cinapiVersion: 1
16191cb0ef41Sopenharmony_ci-->
16201cb0ef41Sopenharmony_ci
16211cb0ef41Sopenharmony_ci```c
16221cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status
16231cb0ef41Sopenharmony_ci    napi_close_escapable_handle_scope(napi_env env,
16241cb0ef41Sopenharmony_ci                                      napi_handle_scope scope);
16251cb0ef41Sopenharmony_ci```
16261cb0ef41Sopenharmony_ci
16271cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
16281cb0ef41Sopenharmony_ci* `[in] scope`: `napi_value` representing the scope to be closed.
16291cb0ef41Sopenharmony_ci
16301cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
16311cb0ef41Sopenharmony_ci
16321cb0ef41Sopenharmony_ciThis API closes the scope passed in. Scopes must be closed in the
16331cb0ef41Sopenharmony_cireverse order from which they were created.
16341cb0ef41Sopenharmony_ci
16351cb0ef41Sopenharmony_ciThis API can be called even if there is a pending JavaScript exception.
16361cb0ef41Sopenharmony_ci
16371cb0ef41Sopenharmony_ci#### `napi_escape_handle`
16381cb0ef41Sopenharmony_ci
16391cb0ef41Sopenharmony_ci<!-- YAML
16401cb0ef41Sopenharmony_ciadded: v8.0.0
16411cb0ef41Sopenharmony_cinapiVersion: 1
16421cb0ef41Sopenharmony_ci-->
16431cb0ef41Sopenharmony_ci
16441cb0ef41Sopenharmony_ci```c
16451cb0ef41Sopenharmony_cinapi_status napi_escape_handle(napi_env env,
16461cb0ef41Sopenharmony_ci                               napi_escapable_handle_scope scope,
16471cb0ef41Sopenharmony_ci                               napi_value escapee,
16481cb0ef41Sopenharmony_ci                               napi_value* result);
16491cb0ef41Sopenharmony_ci```
16501cb0ef41Sopenharmony_ci
16511cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
16521cb0ef41Sopenharmony_ci* `[in] scope`: `napi_value` representing the current scope.
16531cb0ef41Sopenharmony_ci* `[in] escapee`: `napi_value` representing the JavaScript `Object` to be
16541cb0ef41Sopenharmony_ci  escaped.
16551cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing the handle to the escaped `Object`
16561cb0ef41Sopenharmony_ci  in the outer scope.
16571cb0ef41Sopenharmony_ci
16581cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
16591cb0ef41Sopenharmony_ci
16601cb0ef41Sopenharmony_ciThis API promotes the handle to the JavaScript object so that it is valid
16611cb0ef41Sopenharmony_cifor the lifetime of the outer scope. It can only be called once per scope.
16621cb0ef41Sopenharmony_ciIf it is called more than once an error will be returned.
16631cb0ef41Sopenharmony_ci
16641cb0ef41Sopenharmony_ciThis API can be called even if there is a pending JavaScript exception.
16651cb0ef41Sopenharmony_ci
16661cb0ef41Sopenharmony_ci### References to values with a lifespan longer than that of the native method
16671cb0ef41Sopenharmony_ci
16681cb0ef41Sopenharmony_ciIn some cases, an addon will need to be able to create and reference values
16691cb0ef41Sopenharmony_ciwith a lifespan longer than that of a single native method invocation. For
16701cb0ef41Sopenharmony_ciexample, to create a constructor and later use that constructor
16711cb0ef41Sopenharmony_ciin a request to create instances, it must be possible to reference
16721cb0ef41Sopenharmony_cithe constructor object across many different instance creation requests. This
16731cb0ef41Sopenharmony_ciwould not be possible with a normal handle returned as a `napi_value` as
16741cb0ef41Sopenharmony_cidescribed in the earlier section. The lifespan of a normal handle is
16751cb0ef41Sopenharmony_cimanaged by scopes and all scopes must be closed before the end of a native
16761cb0ef41Sopenharmony_cimethod.
16771cb0ef41Sopenharmony_ci
16781cb0ef41Sopenharmony_ciNode-API provides methods for creating persistent references to values.
16791cb0ef41Sopenharmony_ciCurrently Node-API only allows references to be created for a
16801cb0ef41Sopenharmony_cilimited set of value types, including object, external, function, and symbol.
16811cb0ef41Sopenharmony_ci
16821cb0ef41Sopenharmony_ciEach reference has an associated count with a value of 0 or higher,
16831cb0ef41Sopenharmony_ciwhich determines whether the reference will keep the corresponding value alive.
16841cb0ef41Sopenharmony_ciReferences with a count of 0 do not prevent values from being collected.
16851cb0ef41Sopenharmony_ciValues of object (object, function, external) and symbol types are becoming
16861cb0ef41Sopenharmony_ci'weak' references and can still be accessed while they are not collected.
16871cb0ef41Sopenharmony_ciAny count greater than 0 will prevent the values from being collected.
16881cb0ef41Sopenharmony_ci
16891cb0ef41Sopenharmony_ciSymbol values have different flavors. The true weak reference behavior is
16901cb0ef41Sopenharmony_cionly supported by local symbols created with the `napi_create_symbol` function
16911cb0ef41Sopenharmony_cior the JavaScript `Symbol()` constructor calls. Globally registered symbols
16921cb0ef41Sopenharmony_cicreated with the `node_api_symbol_for` function or JavaScript `Symbol.for()`
16931cb0ef41Sopenharmony_cifunction calls remain always strong references because the garbage collector
16941cb0ef41Sopenharmony_cidoes not collect them. The same is true for well-known symbols such as
16951cb0ef41Sopenharmony_ci`Symbol.iterator`. They are also never collected by the garbage collector.
16961cb0ef41Sopenharmony_ci
16971cb0ef41Sopenharmony_ciReferences can be created with an initial reference count. The count can
16981cb0ef41Sopenharmony_cithen be modified through [`napi_reference_ref`][] and
16991cb0ef41Sopenharmony_ci[`napi_reference_unref`][]. If an object is collected while the count
17001cb0ef41Sopenharmony_cifor a reference is 0, all subsequent calls to
17011cb0ef41Sopenharmony_ciget the object associated with the reference [`napi_get_reference_value`][]
17021cb0ef41Sopenharmony_ciwill return `NULL` for the returned `napi_value`. An attempt to call
17031cb0ef41Sopenharmony_ci[`napi_reference_ref`][] for a reference whose object has been collected
17041cb0ef41Sopenharmony_ciresults in an error.
17051cb0ef41Sopenharmony_ci
17061cb0ef41Sopenharmony_ciReferences must be deleted once they are no longer required by the addon. When
17071cb0ef41Sopenharmony_cia reference is deleted, it will no longer prevent the corresponding object from
17081cb0ef41Sopenharmony_cibeing collected. Failure to delete a persistent reference results in
17091cb0ef41Sopenharmony_cia 'memory leak' with both the native memory for the persistent reference and
17101cb0ef41Sopenharmony_cithe corresponding object on the heap being retained forever.
17111cb0ef41Sopenharmony_ci
17121cb0ef41Sopenharmony_ciThere can be multiple persistent references created which refer to the same
17131cb0ef41Sopenharmony_ciobject, each of which will either keep the object live or not based on its
17141cb0ef41Sopenharmony_ciindividual count. Multiple persistent references to the same object
17151cb0ef41Sopenharmony_cican result in unexpectedly keeping alive native memory. The native structures
17161cb0ef41Sopenharmony_cifor a persistent reference must be kept alive until finalizers for the
17171cb0ef41Sopenharmony_cireferenced object are executed. If a new persistent reference is created
17181cb0ef41Sopenharmony_cifor the same object, the finalizers for that object will not be
17191cb0ef41Sopenharmony_cirun and the native memory pointed by the earlier persistent reference
17201cb0ef41Sopenharmony_ciwill not be freed. This can be avoided by calling
17211cb0ef41Sopenharmony_ci`napi_delete_reference` in addition to `napi_reference_unref` when possible.
17221cb0ef41Sopenharmony_ci
17231cb0ef41Sopenharmony_ci**Change History:**
17241cb0ef41Sopenharmony_ci
17251cb0ef41Sopenharmony_ci* Experimental (`NAPI_EXPERIMENTAL` is defined):
17261cb0ef41Sopenharmony_ci
17271cb0ef41Sopenharmony_ci  References can be created for all value types. The new supported value
17281cb0ef41Sopenharmony_ci  types do not support weak reference semantic and the values of these types
17291cb0ef41Sopenharmony_ci  are released when the reference count becomes 0 and cannot be accessed from
17301cb0ef41Sopenharmony_ci  the reference anymore.
17311cb0ef41Sopenharmony_ci
17321cb0ef41Sopenharmony_ci#### `napi_create_reference`
17331cb0ef41Sopenharmony_ci
17341cb0ef41Sopenharmony_ci<!-- YAML
17351cb0ef41Sopenharmony_ciadded: v8.0.0
17361cb0ef41Sopenharmony_cinapiVersion: 1
17371cb0ef41Sopenharmony_ci-->
17381cb0ef41Sopenharmony_ci
17391cb0ef41Sopenharmony_ci```c
17401cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_create_reference(napi_env env,
17411cb0ef41Sopenharmony_ci                                              napi_value value,
17421cb0ef41Sopenharmony_ci                                              uint32_t initial_refcount,
17431cb0ef41Sopenharmony_ci                                              napi_ref* result);
17441cb0ef41Sopenharmony_ci```
17451cb0ef41Sopenharmony_ci
17461cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
17471cb0ef41Sopenharmony_ci* `[in] value`: The `napi_value` for which a reference is being created.
17481cb0ef41Sopenharmony_ci* `[in] initial_refcount`: Initial reference count for the new reference.
17491cb0ef41Sopenharmony_ci* `[out] result`: `napi_ref` pointing to the new reference.
17501cb0ef41Sopenharmony_ci
17511cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
17521cb0ef41Sopenharmony_ci
17531cb0ef41Sopenharmony_ciThis API creates a new reference with the specified reference count
17541cb0ef41Sopenharmony_cito the value passed in.
17551cb0ef41Sopenharmony_ci
17561cb0ef41Sopenharmony_ci#### `napi_delete_reference`
17571cb0ef41Sopenharmony_ci
17581cb0ef41Sopenharmony_ci<!-- YAML
17591cb0ef41Sopenharmony_ciadded: v8.0.0
17601cb0ef41Sopenharmony_cinapiVersion: 1
17611cb0ef41Sopenharmony_ci-->
17621cb0ef41Sopenharmony_ci
17631cb0ef41Sopenharmony_ci```c
17641cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_delete_reference(napi_env env, napi_ref ref);
17651cb0ef41Sopenharmony_ci```
17661cb0ef41Sopenharmony_ci
17671cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
17681cb0ef41Sopenharmony_ci* `[in] ref`: `napi_ref` to be deleted.
17691cb0ef41Sopenharmony_ci
17701cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
17711cb0ef41Sopenharmony_ci
17721cb0ef41Sopenharmony_ciThis API deletes the reference passed in.
17731cb0ef41Sopenharmony_ci
17741cb0ef41Sopenharmony_ciThis API can be called even if there is a pending JavaScript exception.
17751cb0ef41Sopenharmony_ci
17761cb0ef41Sopenharmony_ci#### `napi_reference_ref`
17771cb0ef41Sopenharmony_ci
17781cb0ef41Sopenharmony_ci<!-- YAML
17791cb0ef41Sopenharmony_ciadded: v8.0.0
17801cb0ef41Sopenharmony_cinapiVersion: 1
17811cb0ef41Sopenharmony_ci-->
17821cb0ef41Sopenharmony_ci
17831cb0ef41Sopenharmony_ci```c
17841cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_reference_ref(napi_env env,
17851cb0ef41Sopenharmony_ci                                           napi_ref ref,
17861cb0ef41Sopenharmony_ci                                           uint32_t* result);
17871cb0ef41Sopenharmony_ci```
17881cb0ef41Sopenharmony_ci
17891cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
17901cb0ef41Sopenharmony_ci* `[in] ref`: `napi_ref` for which the reference count will be incremented.
17911cb0ef41Sopenharmony_ci* `[out] result`: The new reference count.
17921cb0ef41Sopenharmony_ci
17931cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
17941cb0ef41Sopenharmony_ci
17951cb0ef41Sopenharmony_ciThis API increments the reference count for the reference
17961cb0ef41Sopenharmony_cipassed in and returns the resulting reference count.
17971cb0ef41Sopenharmony_ci
17981cb0ef41Sopenharmony_ci#### `napi_reference_unref`
17991cb0ef41Sopenharmony_ci
18001cb0ef41Sopenharmony_ci<!-- YAML
18011cb0ef41Sopenharmony_ciadded: v8.0.0
18021cb0ef41Sopenharmony_cinapiVersion: 1
18031cb0ef41Sopenharmony_ci-->
18041cb0ef41Sopenharmony_ci
18051cb0ef41Sopenharmony_ci```c
18061cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_reference_unref(napi_env env,
18071cb0ef41Sopenharmony_ci                                             napi_ref ref,
18081cb0ef41Sopenharmony_ci                                             uint32_t* result);
18091cb0ef41Sopenharmony_ci```
18101cb0ef41Sopenharmony_ci
18111cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
18121cb0ef41Sopenharmony_ci* `[in] ref`: `napi_ref` for which the reference count will be decremented.
18131cb0ef41Sopenharmony_ci* `[out] result`: The new reference count.
18141cb0ef41Sopenharmony_ci
18151cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
18161cb0ef41Sopenharmony_ci
18171cb0ef41Sopenharmony_ciThis API decrements the reference count for the reference
18181cb0ef41Sopenharmony_cipassed in and returns the resulting reference count.
18191cb0ef41Sopenharmony_ci
18201cb0ef41Sopenharmony_ci#### `napi_get_reference_value`
18211cb0ef41Sopenharmony_ci
18221cb0ef41Sopenharmony_ci<!-- YAML
18231cb0ef41Sopenharmony_ciadded: v8.0.0
18241cb0ef41Sopenharmony_cinapiVersion: 1
18251cb0ef41Sopenharmony_ci-->
18261cb0ef41Sopenharmony_ci
18271cb0ef41Sopenharmony_ci```c
18281cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_get_reference_value(napi_env env,
18291cb0ef41Sopenharmony_ci                                                 napi_ref ref,
18301cb0ef41Sopenharmony_ci                                                 napi_value* result);
18311cb0ef41Sopenharmony_ci```
18321cb0ef41Sopenharmony_ci
18331cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
18341cb0ef41Sopenharmony_ci* `[in] ref`: The `napi_ref` for which the corresponding value is
18351cb0ef41Sopenharmony_ci  being requested.
18361cb0ef41Sopenharmony_ci* `[out] result`: The `napi_value` referenced by the `napi_ref`.
18371cb0ef41Sopenharmony_ci
18381cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
18391cb0ef41Sopenharmony_ci
18401cb0ef41Sopenharmony_ciIf still valid, this API returns the `napi_value` representing the
18411cb0ef41Sopenharmony_ciJavaScript value associated with the `napi_ref`. Otherwise, result
18421cb0ef41Sopenharmony_ciwill be `NULL`.
18431cb0ef41Sopenharmony_ci
18441cb0ef41Sopenharmony_ci### Cleanup on exit of the current Node.js environment
18451cb0ef41Sopenharmony_ci
18461cb0ef41Sopenharmony_ciWhile a Node.js process typically releases all its resources when exiting,
18471cb0ef41Sopenharmony_ciembedders of Node.js, or future Worker support, may require addons to register
18481cb0ef41Sopenharmony_ciclean-up hooks that will be run once the current Node.js environment exits.
18491cb0ef41Sopenharmony_ci
18501cb0ef41Sopenharmony_ciNode-API provides functions for registering and un-registering such callbacks.
18511cb0ef41Sopenharmony_ciWhen those callbacks are run, all resources that are being held by the addon
18521cb0ef41Sopenharmony_cishould be freed up.
18531cb0ef41Sopenharmony_ci
18541cb0ef41Sopenharmony_ci#### `napi_add_env_cleanup_hook`
18551cb0ef41Sopenharmony_ci
18561cb0ef41Sopenharmony_ci<!-- YAML
18571cb0ef41Sopenharmony_ciadded: v10.2.0
18581cb0ef41Sopenharmony_cinapiVersion: 3
18591cb0ef41Sopenharmony_ci-->
18601cb0ef41Sopenharmony_ci
18611cb0ef41Sopenharmony_ci```c
18621cb0ef41Sopenharmony_ciNODE_EXTERN napi_status napi_add_env_cleanup_hook(node_api_nogc_env env,
18631cb0ef41Sopenharmony_ci                                                  napi_cleanup_hook fun,
18641cb0ef41Sopenharmony_ci                                                  void* arg);
18651cb0ef41Sopenharmony_ci```
18661cb0ef41Sopenharmony_ci
18671cb0ef41Sopenharmony_ciRegisters `fun` as a function to be run with the `arg` parameter once the
18681cb0ef41Sopenharmony_cicurrent Node.js environment exits.
18691cb0ef41Sopenharmony_ci
18701cb0ef41Sopenharmony_ciA function can safely be specified multiple times with different
18711cb0ef41Sopenharmony_ci`arg` values. In that case, it will be called multiple times as well.
18721cb0ef41Sopenharmony_ciProviding the same `fun` and `arg` values multiple times is not allowed
18731cb0ef41Sopenharmony_ciand will lead the process to abort.
18741cb0ef41Sopenharmony_ci
18751cb0ef41Sopenharmony_ciThe hooks will be called in reverse order, i.e. the most recently added one
18761cb0ef41Sopenharmony_ciwill be called first.
18771cb0ef41Sopenharmony_ci
18781cb0ef41Sopenharmony_ciRemoving this hook can be done by using [`napi_remove_env_cleanup_hook`][].
18791cb0ef41Sopenharmony_ciTypically, that happens when the resource for which this hook was added
18801cb0ef41Sopenharmony_ciis being torn down anyway.
18811cb0ef41Sopenharmony_ci
18821cb0ef41Sopenharmony_ciFor asynchronous cleanup, [`napi_add_async_cleanup_hook`][] is available.
18831cb0ef41Sopenharmony_ci
18841cb0ef41Sopenharmony_ci#### `napi_remove_env_cleanup_hook`
18851cb0ef41Sopenharmony_ci
18861cb0ef41Sopenharmony_ci<!-- YAML
18871cb0ef41Sopenharmony_ciadded: v10.2.0
18881cb0ef41Sopenharmony_cinapiVersion: 3
18891cb0ef41Sopenharmony_ci-->
18901cb0ef41Sopenharmony_ci
18911cb0ef41Sopenharmony_ci```c
18921cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_remove_env_cleanup_hook(node_api_nogc_env env,
18931cb0ef41Sopenharmony_ci                                                     void (*fun)(void* arg),
18941cb0ef41Sopenharmony_ci                                                     void* arg);
18951cb0ef41Sopenharmony_ci```
18961cb0ef41Sopenharmony_ci
18971cb0ef41Sopenharmony_ciUnregisters `fun` as a function to be run with the `arg` parameter once the
18981cb0ef41Sopenharmony_cicurrent Node.js environment exits. Both the argument and the function value
18991cb0ef41Sopenharmony_cineed to be exact matches.
19001cb0ef41Sopenharmony_ci
19011cb0ef41Sopenharmony_ciThe function must have originally been registered
19021cb0ef41Sopenharmony_ciwith `napi_add_env_cleanup_hook`, otherwise the process will abort.
19031cb0ef41Sopenharmony_ci
19041cb0ef41Sopenharmony_ci#### `napi_add_async_cleanup_hook`
19051cb0ef41Sopenharmony_ci
19061cb0ef41Sopenharmony_ci<!-- YAML
19071cb0ef41Sopenharmony_ciadded:
19081cb0ef41Sopenharmony_ci  - v14.8.0
19091cb0ef41Sopenharmony_ci  - v12.19.0
19101cb0ef41Sopenharmony_cinapiVersion: 8
19111cb0ef41Sopenharmony_cichanges:
19121cb0ef41Sopenharmony_ci  - version:
19131cb0ef41Sopenharmony_ci    - v14.10.0
19141cb0ef41Sopenharmony_ci    - v12.19.0
19151cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34819
19161cb0ef41Sopenharmony_ci    description: Changed signature of the `hook` callback.
19171cb0ef41Sopenharmony_ci-->
19181cb0ef41Sopenharmony_ci
19191cb0ef41Sopenharmony_ci```c
19201cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_add_async_cleanup_hook(
19211cb0ef41Sopenharmony_ci    node_api_nogc_env env,
19221cb0ef41Sopenharmony_ci    napi_async_cleanup_hook hook,
19231cb0ef41Sopenharmony_ci    void* arg,
19241cb0ef41Sopenharmony_ci    napi_async_cleanup_hook_handle* remove_handle);
19251cb0ef41Sopenharmony_ci```
19261cb0ef41Sopenharmony_ci
19271cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
19281cb0ef41Sopenharmony_ci* `[in] hook`: The function pointer to call at environment teardown.
19291cb0ef41Sopenharmony_ci* `[in] arg`: The pointer to pass to `hook` when it gets called.
19301cb0ef41Sopenharmony_ci* `[out] remove_handle`: Optional handle that refers to the asynchronous cleanup
19311cb0ef41Sopenharmony_ci  hook.
19321cb0ef41Sopenharmony_ci
19331cb0ef41Sopenharmony_ciRegisters `hook`, which is a function of type [`napi_async_cleanup_hook`][], as
19341cb0ef41Sopenharmony_cia function to be run with the `remove_handle` and `arg` parameters once the
19351cb0ef41Sopenharmony_cicurrent Node.js environment exits.
19361cb0ef41Sopenharmony_ci
19371cb0ef41Sopenharmony_ciUnlike [`napi_add_env_cleanup_hook`][], the hook is allowed to be asynchronous.
19381cb0ef41Sopenharmony_ci
19391cb0ef41Sopenharmony_ciOtherwise, behavior generally matches that of [`napi_add_env_cleanup_hook`][].
19401cb0ef41Sopenharmony_ci
19411cb0ef41Sopenharmony_ciIf `remove_handle` is not `NULL`, an opaque value will be stored in it
19421cb0ef41Sopenharmony_cithat must later be passed to [`napi_remove_async_cleanup_hook`][],
19431cb0ef41Sopenharmony_ciregardless of whether the hook has already been invoked.
19441cb0ef41Sopenharmony_ciTypically, that happens when the resource for which this hook was added
19451cb0ef41Sopenharmony_ciis being torn down anyway.
19461cb0ef41Sopenharmony_ci
19471cb0ef41Sopenharmony_ci#### `napi_remove_async_cleanup_hook`
19481cb0ef41Sopenharmony_ci
19491cb0ef41Sopenharmony_ci<!-- YAML
19501cb0ef41Sopenharmony_ciadded:
19511cb0ef41Sopenharmony_ci  - v14.8.0
19521cb0ef41Sopenharmony_ci  - v12.19.0
19531cb0ef41Sopenharmony_cichanges:
19541cb0ef41Sopenharmony_ci  - version:
19551cb0ef41Sopenharmony_ci    - v14.10.0
19561cb0ef41Sopenharmony_ci    - v12.19.0
19571cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34819
19581cb0ef41Sopenharmony_ci    description: Removed `env` parameter.
19591cb0ef41Sopenharmony_ci-->
19601cb0ef41Sopenharmony_ci
19611cb0ef41Sopenharmony_ci```c
19621cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_remove_async_cleanup_hook(
19631cb0ef41Sopenharmony_ci    napi_async_cleanup_hook_handle remove_handle);
19641cb0ef41Sopenharmony_ci```
19651cb0ef41Sopenharmony_ci
19661cb0ef41Sopenharmony_ci* `[in] remove_handle`: The handle to an asynchronous cleanup hook that was
19671cb0ef41Sopenharmony_ci  created with [`napi_add_async_cleanup_hook`][].
19681cb0ef41Sopenharmony_ci
19691cb0ef41Sopenharmony_ciUnregisters the cleanup hook corresponding to `remove_handle`. This will prevent
19701cb0ef41Sopenharmony_cithe hook from being executed, unless it has already started executing.
19711cb0ef41Sopenharmony_ciThis must be called on any `napi_async_cleanup_hook_handle` value obtained
19721cb0ef41Sopenharmony_cifrom [`napi_add_async_cleanup_hook`][].
19731cb0ef41Sopenharmony_ci
19741cb0ef41Sopenharmony_ci### Finalization on the exit of the Node.js environment
19751cb0ef41Sopenharmony_ci
19761cb0ef41Sopenharmony_ciThe Node.js environment may be torn down at an arbitrary time as soon as
19771cb0ef41Sopenharmony_cipossible with JavaScript execution disallowed, like on the request of
19781cb0ef41Sopenharmony_ci[`worker.terminate()`][]. When the environment is being torn down, the
19791cb0ef41Sopenharmony_ciregistered `napi_finalize` callbacks of JavaScript objects, thread-safe
19801cb0ef41Sopenharmony_cifunctions and environment instance data are invoked immediately and
19811cb0ef41Sopenharmony_ciindependently.
19821cb0ef41Sopenharmony_ci
19831cb0ef41Sopenharmony_ciThe invocation of `napi_finalize` callbacks is scheduled after the manually
19841cb0ef41Sopenharmony_ciregistered cleanup hooks. In order to ensure a proper order of addon
19851cb0ef41Sopenharmony_cifinalization during environment shutdown to avoid use-after-free in the
19861cb0ef41Sopenharmony_ci`napi_finalize` callback, addons should register a cleanup hook with
19871cb0ef41Sopenharmony_ci`napi_add_env_cleanup_hook` and `napi_add_async_cleanup_hook` to manually
19881cb0ef41Sopenharmony_cirelease the allocated resource in a proper order.
19891cb0ef41Sopenharmony_ci
19901cb0ef41Sopenharmony_ci## Module registration
19911cb0ef41Sopenharmony_ci
19921cb0ef41Sopenharmony_ciNode-API modules are registered in a manner similar to other modules
19931cb0ef41Sopenharmony_ciexcept that instead of using the `NODE_MODULE` macro the following
19941cb0ef41Sopenharmony_ciis used:
19951cb0ef41Sopenharmony_ci
19961cb0ef41Sopenharmony_ci```c
19971cb0ef41Sopenharmony_ciNAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
19981cb0ef41Sopenharmony_ci```
19991cb0ef41Sopenharmony_ci
20001cb0ef41Sopenharmony_ciThe next difference is the signature for the `Init` method. For a Node-API
20011cb0ef41Sopenharmony_cimodule it is as follows:
20021cb0ef41Sopenharmony_ci
20031cb0ef41Sopenharmony_ci```c
20041cb0ef41Sopenharmony_cinapi_value Init(napi_env env, napi_value exports);
20051cb0ef41Sopenharmony_ci```
20061cb0ef41Sopenharmony_ci
20071cb0ef41Sopenharmony_ciThe return value from `Init` is treated as the `exports` object for the module.
20081cb0ef41Sopenharmony_ciThe `Init` method is passed an empty object via the `exports` parameter as a
20091cb0ef41Sopenharmony_ciconvenience. If `Init` returns `NULL`, the parameter passed as `exports` is
20101cb0ef41Sopenharmony_ciexported by the module. Node-API modules cannot modify the `module` object but
20111cb0ef41Sopenharmony_cican specify anything as the `exports` property of the module.
20121cb0ef41Sopenharmony_ci
20131cb0ef41Sopenharmony_ciTo add the method `hello` as a function so that it can be called as a method
20141cb0ef41Sopenharmony_ciprovided by the addon:
20151cb0ef41Sopenharmony_ci
20161cb0ef41Sopenharmony_ci```c
20171cb0ef41Sopenharmony_cinapi_value Init(napi_env env, napi_value exports) {
20181cb0ef41Sopenharmony_ci  napi_status status;
20191cb0ef41Sopenharmony_ci  napi_property_descriptor desc = {
20201cb0ef41Sopenharmony_ci    "hello",
20211cb0ef41Sopenharmony_ci    NULL,
20221cb0ef41Sopenharmony_ci    Method,
20231cb0ef41Sopenharmony_ci    NULL,
20241cb0ef41Sopenharmony_ci    NULL,
20251cb0ef41Sopenharmony_ci    NULL,
20261cb0ef41Sopenharmony_ci    napi_writable | napi_enumerable | napi_configurable,
20271cb0ef41Sopenharmony_ci    NULL
20281cb0ef41Sopenharmony_ci  };
20291cb0ef41Sopenharmony_ci  status = napi_define_properties(env, exports, 1, &desc);
20301cb0ef41Sopenharmony_ci  if (status != napi_ok) return NULL;
20311cb0ef41Sopenharmony_ci  return exports;
20321cb0ef41Sopenharmony_ci}
20331cb0ef41Sopenharmony_ci```
20341cb0ef41Sopenharmony_ci
20351cb0ef41Sopenharmony_ciTo set a function to be returned by the `require()` for the addon:
20361cb0ef41Sopenharmony_ci
20371cb0ef41Sopenharmony_ci```c
20381cb0ef41Sopenharmony_cinapi_value Init(napi_env env, napi_value exports) {
20391cb0ef41Sopenharmony_ci  napi_value method;
20401cb0ef41Sopenharmony_ci  napi_status status;
20411cb0ef41Sopenharmony_ci  status = napi_create_function(env, "exports", NAPI_AUTO_LENGTH, Method, NULL, &method);
20421cb0ef41Sopenharmony_ci  if (status != napi_ok) return NULL;
20431cb0ef41Sopenharmony_ci  return method;
20441cb0ef41Sopenharmony_ci}
20451cb0ef41Sopenharmony_ci```
20461cb0ef41Sopenharmony_ci
20471cb0ef41Sopenharmony_ciTo define a class so that new instances can be created (often used with
20481cb0ef41Sopenharmony_ci[Object wrap][]):
20491cb0ef41Sopenharmony_ci
20501cb0ef41Sopenharmony_ci```c
20511cb0ef41Sopenharmony_ci// NOTE: partial example, not all referenced code is included
20521cb0ef41Sopenharmony_cinapi_value Init(napi_env env, napi_value exports) {
20531cb0ef41Sopenharmony_ci  napi_status status;
20541cb0ef41Sopenharmony_ci  napi_property_descriptor properties[] = {
20551cb0ef41Sopenharmony_ci    { "value", NULL, NULL, GetValue, SetValue, NULL, napi_writable | napi_configurable, NULL },
20561cb0ef41Sopenharmony_ci    DECLARE_NAPI_METHOD("plusOne", PlusOne),
20571cb0ef41Sopenharmony_ci    DECLARE_NAPI_METHOD("multiply", Multiply),
20581cb0ef41Sopenharmony_ci  };
20591cb0ef41Sopenharmony_ci
20601cb0ef41Sopenharmony_ci  napi_value cons;
20611cb0ef41Sopenharmony_ci  status =
20621cb0ef41Sopenharmony_ci      napi_define_class(env, "MyObject", New, NULL, 3, properties, &cons);
20631cb0ef41Sopenharmony_ci  if (status != napi_ok) return NULL;
20641cb0ef41Sopenharmony_ci
20651cb0ef41Sopenharmony_ci  status = napi_create_reference(env, cons, 1, &constructor);
20661cb0ef41Sopenharmony_ci  if (status != napi_ok) return NULL;
20671cb0ef41Sopenharmony_ci
20681cb0ef41Sopenharmony_ci  status = napi_set_named_property(env, exports, "MyObject", cons);
20691cb0ef41Sopenharmony_ci  if (status != napi_ok) return NULL;
20701cb0ef41Sopenharmony_ci
20711cb0ef41Sopenharmony_ci  return exports;
20721cb0ef41Sopenharmony_ci}
20731cb0ef41Sopenharmony_ci```
20741cb0ef41Sopenharmony_ci
20751cb0ef41Sopenharmony_ciYou can also use the `NAPI_MODULE_INIT` macro, which acts as a shorthand
20761cb0ef41Sopenharmony_cifor `NAPI_MODULE` and defining an `Init` function:
20771cb0ef41Sopenharmony_ci
20781cb0ef41Sopenharmony_ci```c
20791cb0ef41Sopenharmony_ciNAPI_MODULE_INIT(/* napi_env env, napi_value exports */) {
20801cb0ef41Sopenharmony_ci  napi_value answer;
20811cb0ef41Sopenharmony_ci  napi_status result;
20821cb0ef41Sopenharmony_ci
20831cb0ef41Sopenharmony_ci  status = napi_create_int64(env, 42, &answer);
20841cb0ef41Sopenharmony_ci  if (status != napi_ok) return NULL;
20851cb0ef41Sopenharmony_ci
20861cb0ef41Sopenharmony_ci  status = napi_set_named_property(env, exports, "answer", answer);
20871cb0ef41Sopenharmony_ci  if (status != napi_ok) return NULL;
20881cb0ef41Sopenharmony_ci
20891cb0ef41Sopenharmony_ci  return exports;
20901cb0ef41Sopenharmony_ci}
20911cb0ef41Sopenharmony_ci```
20921cb0ef41Sopenharmony_ci
20931cb0ef41Sopenharmony_ciThe parameters `env` and `exports` are provided to the body of the
20941cb0ef41Sopenharmony_ci`NAPI_MODULE_INIT` macro.
20951cb0ef41Sopenharmony_ci
20961cb0ef41Sopenharmony_ciAll Node-API addons are context-aware, meaning they may be loaded multiple
20971cb0ef41Sopenharmony_citimes. There are a few design considerations when declaring such a module.
20981cb0ef41Sopenharmony_ciThe documentation on [context-aware addons][] provides more details.
20991cb0ef41Sopenharmony_ci
21001cb0ef41Sopenharmony_ciThe variables `env` and `exports` will be available inside the function body
21011cb0ef41Sopenharmony_cifollowing the macro invocation.
21021cb0ef41Sopenharmony_ci
21031cb0ef41Sopenharmony_ciFor more details on setting properties on objects, see the section on
21041cb0ef41Sopenharmony_ci[Working with JavaScript properties][].
21051cb0ef41Sopenharmony_ci
21061cb0ef41Sopenharmony_ciFor more details on building addon modules in general, refer to the existing
21071cb0ef41Sopenharmony_ciAPI.
21081cb0ef41Sopenharmony_ci
21091cb0ef41Sopenharmony_ci## Working with JavaScript values
21101cb0ef41Sopenharmony_ci
21111cb0ef41Sopenharmony_ciNode-API exposes a set of APIs to create all types of JavaScript values.
21121cb0ef41Sopenharmony_ciSome of these types are documented under [Section 6][]
21131cb0ef41Sopenharmony_ciof the [ECMAScript Language Specification][].
21141cb0ef41Sopenharmony_ci
21151cb0ef41Sopenharmony_ciFundamentally, these APIs are used to do one of the following:
21161cb0ef41Sopenharmony_ci
21171cb0ef41Sopenharmony_ci1. Create a new JavaScript object
21181cb0ef41Sopenharmony_ci2. Convert from a primitive C type to a Node-API value
21191cb0ef41Sopenharmony_ci3. Convert from Node-API value to a primitive C type
21201cb0ef41Sopenharmony_ci4. Get global instances including `undefined` and `null`
21211cb0ef41Sopenharmony_ci
21221cb0ef41Sopenharmony_ciNode-API values are represented by the type `napi_value`.
21231cb0ef41Sopenharmony_ciAny Node-API call that requires a JavaScript value takes in a `napi_value`.
21241cb0ef41Sopenharmony_ciIn some cases, the API does check the type of the `napi_value` up-front.
21251cb0ef41Sopenharmony_ciHowever, for better performance, it's better for the caller to make sure that
21261cb0ef41Sopenharmony_cithe `napi_value` in question is of the JavaScript type expected by the API.
21271cb0ef41Sopenharmony_ci
21281cb0ef41Sopenharmony_ci### Enum types
21291cb0ef41Sopenharmony_ci
21301cb0ef41Sopenharmony_ci#### `napi_key_collection_mode`
21311cb0ef41Sopenharmony_ci
21321cb0ef41Sopenharmony_ci<!-- YAML
21331cb0ef41Sopenharmony_ciadded:
21341cb0ef41Sopenharmony_ci - v13.7.0
21351cb0ef41Sopenharmony_ci - v12.17.0
21361cb0ef41Sopenharmony_ci - v10.20.0
21371cb0ef41Sopenharmony_cinapiVersion: 6
21381cb0ef41Sopenharmony_ci-->
21391cb0ef41Sopenharmony_ci
21401cb0ef41Sopenharmony_ci```c
21411cb0ef41Sopenharmony_citypedef enum {
21421cb0ef41Sopenharmony_ci  napi_key_include_prototypes,
21431cb0ef41Sopenharmony_ci  napi_key_own_only
21441cb0ef41Sopenharmony_ci} napi_key_collection_mode;
21451cb0ef41Sopenharmony_ci```
21461cb0ef41Sopenharmony_ci
21471cb0ef41Sopenharmony_ciDescribes the `Keys/Properties` filter enums:
21481cb0ef41Sopenharmony_ci
21491cb0ef41Sopenharmony_ci`napi_key_collection_mode` limits the range of collected properties.
21501cb0ef41Sopenharmony_ci
21511cb0ef41Sopenharmony_ci`napi_key_own_only` limits the collected properties to the given
21521cb0ef41Sopenharmony_ciobject only. `napi_key_include_prototypes` will include all keys
21531cb0ef41Sopenharmony_ciof the objects's prototype chain as well.
21541cb0ef41Sopenharmony_ci
21551cb0ef41Sopenharmony_ci#### `napi_key_filter`
21561cb0ef41Sopenharmony_ci
21571cb0ef41Sopenharmony_ci<!-- YAML
21581cb0ef41Sopenharmony_ciadded:
21591cb0ef41Sopenharmony_ci - v13.7.0
21601cb0ef41Sopenharmony_ci - v12.17.0
21611cb0ef41Sopenharmony_ci - v10.20.0
21621cb0ef41Sopenharmony_cinapiVersion: 6
21631cb0ef41Sopenharmony_ci-->
21641cb0ef41Sopenharmony_ci
21651cb0ef41Sopenharmony_ci```c
21661cb0ef41Sopenharmony_citypedef enum {
21671cb0ef41Sopenharmony_ci  napi_key_all_properties = 0,
21681cb0ef41Sopenharmony_ci  napi_key_writable = 1,
21691cb0ef41Sopenharmony_ci  napi_key_enumerable = 1 << 1,
21701cb0ef41Sopenharmony_ci  napi_key_configurable = 1 << 2,
21711cb0ef41Sopenharmony_ci  napi_key_skip_strings = 1 << 3,
21721cb0ef41Sopenharmony_ci  napi_key_skip_symbols = 1 << 4
21731cb0ef41Sopenharmony_ci} napi_key_filter;
21741cb0ef41Sopenharmony_ci```
21751cb0ef41Sopenharmony_ci
21761cb0ef41Sopenharmony_ciProperty filter bits. They can be or'ed to build a composite filter.
21771cb0ef41Sopenharmony_ci
21781cb0ef41Sopenharmony_ci#### `napi_key_conversion`
21791cb0ef41Sopenharmony_ci
21801cb0ef41Sopenharmony_ci<!-- YAML
21811cb0ef41Sopenharmony_ciadded:
21821cb0ef41Sopenharmony_ci - v13.7.0
21831cb0ef41Sopenharmony_ci - v12.17.0
21841cb0ef41Sopenharmony_ci - v10.20.0
21851cb0ef41Sopenharmony_cinapiVersion: 6
21861cb0ef41Sopenharmony_ci-->
21871cb0ef41Sopenharmony_ci
21881cb0ef41Sopenharmony_ci```c
21891cb0ef41Sopenharmony_citypedef enum {
21901cb0ef41Sopenharmony_ci  napi_key_keep_numbers,
21911cb0ef41Sopenharmony_ci  napi_key_numbers_to_strings
21921cb0ef41Sopenharmony_ci} napi_key_conversion;
21931cb0ef41Sopenharmony_ci```
21941cb0ef41Sopenharmony_ci
21951cb0ef41Sopenharmony_ci`napi_key_numbers_to_strings` will convert integer indices to
21961cb0ef41Sopenharmony_cistrings. `napi_key_keep_numbers` will return numbers for integer
21971cb0ef41Sopenharmony_ciindices.
21981cb0ef41Sopenharmony_ci
21991cb0ef41Sopenharmony_ci#### `napi_valuetype`
22001cb0ef41Sopenharmony_ci
22011cb0ef41Sopenharmony_ci```c
22021cb0ef41Sopenharmony_citypedef enum {
22031cb0ef41Sopenharmony_ci  // ES6 types (corresponds to typeof)
22041cb0ef41Sopenharmony_ci  napi_undefined,
22051cb0ef41Sopenharmony_ci  napi_null,
22061cb0ef41Sopenharmony_ci  napi_boolean,
22071cb0ef41Sopenharmony_ci  napi_number,
22081cb0ef41Sopenharmony_ci  napi_string,
22091cb0ef41Sopenharmony_ci  napi_symbol,
22101cb0ef41Sopenharmony_ci  napi_object,
22111cb0ef41Sopenharmony_ci  napi_function,
22121cb0ef41Sopenharmony_ci  napi_external,
22131cb0ef41Sopenharmony_ci  napi_bigint,
22141cb0ef41Sopenharmony_ci} napi_valuetype;
22151cb0ef41Sopenharmony_ci```
22161cb0ef41Sopenharmony_ci
22171cb0ef41Sopenharmony_ciDescribes the type of a `napi_value`. This generally corresponds to the types
22181cb0ef41Sopenharmony_cidescribed in [Section 6.1][] of the ECMAScript Language Specification.
22191cb0ef41Sopenharmony_ciIn addition to types in that section, `napi_valuetype` can also represent
22201cb0ef41Sopenharmony_ci`Function`s and `Object`s with external data.
22211cb0ef41Sopenharmony_ci
22221cb0ef41Sopenharmony_ciA JavaScript value of type `napi_external` appears in JavaScript as a plain
22231cb0ef41Sopenharmony_ciobject such that no properties can be set on it, and no prototype.
22241cb0ef41Sopenharmony_ci
22251cb0ef41Sopenharmony_ci#### `napi_typedarray_type`
22261cb0ef41Sopenharmony_ci
22271cb0ef41Sopenharmony_ci```c
22281cb0ef41Sopenharmony_citypedef enum {
22291cb0ef41Sopenharmony_ci  napi_int8_array,
22301cb0ef41Sopenharmony_ci  napi_uint8_array,
22311cb0ef41Sopenharmony_ci  napi_uint8_clamped_array,
22321cb0ef41Sopenharmony_ci  napi_int16_array,
22331cb0ef41Sopenharmony_ci  napi_uint16_array,
22341cb0ef41Sopenharmony_ci  napi_int32_array,
22351cb0ef41Sopenharmony_ci  napi_uint32_array,
22361cb0ef41Sopenharmony_ci  napi_float32_array,
22371cb0ef41Sopenharmony_ci  napi_float64_array,
22381cb0ef41Sopenharmony_ci  napi_bigint64_array,
22391cb0ef41Sopenharmony_ci  napi_biguint64_array,
22401cb0ef41Sopenharmony_ci} napi_typedarray_type;
22411cb0ef41Sopenharmony_ci```
22421cb0ef41Sopenharmony_ci
22431cb0ef41Sopenharmony_ciThis represents the underlying binary scalar datatype of the `TypedArray`.
22441cb0ef41Sopenharmony_ciElements of this enum correspond to
22451cb0ef41Sopenharmony_ci[Section 22.2][] of the [ECMAScript Language Specification][].
22461cb0ef41Sopenharmony_ci
22471cb0ef41Sopenharmony_ci### Object creation functions
22481cb0ef41Sopenharmony_ci
22491cb0ef41Sopenharmony_ci#### `napi_create_array`
22501cb0ef41Sopenharmony_ci
22511cb0ef41Sopenharmony_ci<!-- YAML
22521cb0ef41Sopenharmony_ciadded: v8.0.0
22531cb0ef41Sopenharmony_cinapiVersion: 1
22541cb0ef41Sopenharmony_ci-->
22551cb0ef41Sopenharmony_ci
22561cb0ef41Sopenharmony_ci```c
22571cb0ef41Sopenharmony_cinapi_status napi_create_array(napi_env env, napi_value* result)
22581cb0ef41Sopenharmony_ci```
22591cb0ef41Sopenharmony_ci
22601cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
22611cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `Array`.
22621cb0ef41Sopenharmony_ci
22631cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
22641cb0ef41Sopenharmony_ci
22651cb0ef41Sopenharmony_ciThis API returns a Node-API value corresponding to a JavaScript `Array` type.
22661cb0ef41Sopenharmony_ciJavaScript arrays are described in
22671cb0ef41Sopenharmony_ci[Section 22.1][] of the ECMAScript Language Specification.
22681cb0ef41Sopenharmony_ci
22691cb0ef41Sopenharmony_ci#### `napi_create_array_with_length`
22701cb0ef41Sopenharmony_ci
22711cb0ef41Sopenharmony_ci<!-- YAML
22721cb0ef41Sopenharmony_ciadded: v8.0.0
22731cb0ef41Sopenharmony_cinapiVersion: 1
22741cb0ef41Sopenharmony_ci-->
22751cb0ef41Sopenharmony_ci
22761cb0ef41Sopenharmony_ci```c
22771cb0ef41Sopenharmony_cinapi_status napi_create_array_with_length(napi_env env,
22781cb0ef41Sopenharmony_ci                                          size_t length,
22791cb0ef41Sopenharmony_ci                                          napi_value* result)
22801cb0ef41Sopenharmony_ci```
22811cb0ef41Sopenharmony_ci
22821cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
22831cb0ef41Sopenharmony_ci* `[in] length`: The initial length of the `Array`.
22841cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `Array`.
22851cb0ef41Sopenharmony_ci
22861cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
22871cb0ef41Sopenharmony_ci
22881cb0ef41Sopenharmony_ciThis API returns a Node-API value corresponding to a JavaScript `Array` type.
22891cb0ef41Sopenharmony_ciThe `Array`'s length property is set to the passed-in length parameter.
22901cb0ef41Sopenharmony_ciHowever, the underlying buffer is not guaranteed to be pre-allocated by the VM
22911cb0ef41Sopenharmony_ciwhen the array is created. That behavior is left to the underlying VM
22921cb0ef41Sopenharmony_ciimplementation. If the buffer must be a contiguous block of memory that can be
22931cb0ef41Sopenharmony_cidirectly read and/or written via C, consider using
22941cb0ef41Sopenharmony_ci[`napi_create_external_arraybuffer`][].
22951cb0ef41Sopenharmony_ci
22961cb0ef41Sopenharmony_ciJavaScript arrays are described in
22971cb0ef41Sopenharmony_ci[Section 22.1][] of the ECMAScript Language Specification.
22981cb0ef41Sopenharmony_ci
22991cb0ef41Sopenharmony_ci#### `napi_create_arraybuffer`
23001cb0ef41Sopenharmony_ci
23011cb0ef41Sopenharmony_ci<!-- YAML
23021cb0ef41Sopenharmony_ciadded: v8.0.0
23031cb0ef41Sopenharmony_cinapiVersion: 1
23041cb0ef41Sopenharmony_ci-->
23051cb0ef41Sopenharmony_ci
23061cb0ef41Sopenharmony_ci```c
23071cb0ef41Sopenharmony_cinapi_status napi_create_arraybuffer(napi_env env,
23081cb0ef41Sopenharmony_ci                                    size_t byte_length,
23091cb0ef41Sopenharmony_ci                                    void** data,
23101cb0ef41Sopenharmony_ci                                    napi_value* result)
23111cb0ef41Sopenharmony_ci```
23121cb0ef41Sopenharmony_ci
23131cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
23141cb0ef41Sopenharmony_ci* `[in] length`: The length in bytes of the array buffer to create.
23151cb0ef41Sopenharmony_ci* `[out] data`: Pointer to the underlying byte buffer of the `ArrayBuffer`.
23161cb0ef41Sopenharmony_ci  `data` can optionally be ignored by passing `NULL`.
23171cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `ArrayBuffer`.
23181cb0ef41Sopenharmony_ci
23191cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
23201cb0ef41Sopenharmony_ci
23211cb0ef41Sopenharmony_ciThis API returns a Node-API value corresponding to a JavaScript `ArrayBuffer`.
23221cb0ef41Sopenharmony_ci`ArrayBuffer`s are used to represent fixed-length binary data buffers. They are
23231cb0ef41Sopenharmony_cinormally used as a backing-buffer for `TypedArray` objects.
23241cb0ef41Sopenharmony_ciThe `ArrayBuffer` allocated will have an underlying byte buffer whose size is
23251cb0ef41Sopenharmony_cidetermined by the `length` parameter that's passed in.
23261cb0ef41Sopenharmony_ciThe underlying buffer is optionally returned back to the caller in case the
23271cb0ef41Sopenharmony_cicaller wants to directly manipulate the buffer. This buffer can only be
23281cb0ef41Sopenharmony_ciwritten to directly from native code. To write to this buffer from JavaScript,
23291cb0ef41Sopenharmony_cia typed array or `DataView` object would need to be created.
23301cb0ef41Sopenharmony_ci
23311cb0ef41Sopenharmony_ciJavaScript `ArrayBuffer` objects are described in
23321cb0ef41Sopenharmony_ci[Section 24.1][] of the ECMAScript Language Specification.
23331cb0ef41Sopenharmony_ci
23341cb0ef41Sopenharmony_ci#### `napi_create_buffer`
23351cb0ef41Sopenharmony_ci
23361cb0ef41Sopenharmony_ci<!-- YAML
23371cb0ef41Sopenharmony_ciadded: v8.0.0
23381cb0ef41Sopenharmony_cinapiVersion: 1
23391cb0ef41Sopenharmony_ci-->
23401cb0ef41Sopenharmony_ci
23411cb0ef41Sopenharmony_ci```c
23421cb0ef41Sopenharmony_cinapi_status napi_create_buffer(napi_env env,
23431cb0ef41Sopenharmony_ci                               size_t size,
23441cb0ef41Sopenharmony_ci                               void** data,
23451cb0ef41Sopenharmony_ci                               napi_value* result)
23461cb0ef41Sopenharmony_ci```
23471cb0ef41Sopenharmony_ci
23481cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
23491cb0ef41Sopenharmony_ci* `[in] size`: Size in bytes of the underlying buffer.
23501cb0ef41Sopenharmony_ci* `[out] data`: Raw pointer to the underlying buffer.
23511cb0ef41Sopenharmony_ci  `data` can optionally be ignored by passing `NULL`.
23521cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a `node::Buffer`.
23531cb0ef41Sopenharmony_ci
23541cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
23551cb0ef41Sopenharmony_ci
23561cb0ef41Sopenharmony_ciThis API allocates a `node::Buffer` object. While this is still a
23571cb0ef41Sopenharmony_cifully-supported data structure, in most cases using a `TypedArray` will suffice.
23581cb0ef41Sopenharmony_ci
23591cb0ef41Sopenharmony_ci#### `napi_create_buffer_copy`
23601cb0ef41Sopenharmony_ci
23611cb0ef41Sopenharmony_ci<!-- YAML
23621cb0ef41Sopenharmony_ciadded: v8.0.0
23631cb0ef41Sopenharmony_cinapiVersion: 1
23641cb0ef41Sopenharmony_ci-->
23651cb0ef41Sopenharmony_ci
23661cb0ef41Sopenharmony_ci```c
23671cb0ef41Sopenharmony_cinapi_status napi_create_buffer_copy(napi_env env,
23681cb0ef41Sopenharmony_ci                                    size_t length,
23691cb0ef41Sopenharmony_ci                                    const void* data,
23701cb0ef41Sopenharmony_ci                                    void** result_data,
23711cb0ef41Sopenharmony_ci                                    napi_value* result)
23721cb0ef41Sopenharmony_ci```
23731cb0ef41Sopenharmony_ci
23741cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
23751cb0ef41Sopenharmony_ci* `[in] size`: Size in bytes of the input buffer (should be the same as the size
23761cb0ef41Sopenharmony_ci  of the new buffer).
23771cb0ef41Sopenharmony_ci* `[in] data`: Raw pointer to the underlying buffer to copy from.
23781cb0ef41Sopenharmony_ci* `[out] result_data`: Pointer to the new `Buffer`'s underlying data buffer.
23791cb0ef41Sopenharmony_ci  `result_data` can optionally be ignored by passing `NULL`.
23801cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a `node::Buffer`.
23811cb0ef41Sopenharmony_ci
23821cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
23831cb0ef41Sopenharmony_ci
23841cb0ef41Sopenharmony_ciThis API allocates a `node::Buffer` object and initializes it with data copied
23851cb0ef41Sopenharmony_cifrom the passed-in buffer. While this is still a fully-supported data
23861cb0ef41Sopenharmony_cistructure, in most cases using a `TypedArray` will suffice.
23871cb0ef41Sopenharmony_ci
23881cb0ef41Sopenharmony_ci#### `napi_create_date`
23891cb0ef41Sopenharmony_ci
23901cb0ef41Sopenharmony_ci<!-- YAML
23911cb0ef41Sopenharmony_ciadded:
23921cb0ef41Sopenharmony_ci - v11.11.0
23931cb0ef41Sopenharmony_ci - v10.17.0
23941cb0ef41Sopenharmony_cinapiVersion: 5
23951cb0ef41Sopenharmony_ci-->
23961cb0ef41Sopenharmony_ci
23971cb0ef41Sopenharmony_ci```c
23981cb0ef41Sopenharmony_cinapi_status napi_create_date(napi_env env,
23991cb0ef41Sopenharmony_ci                             double time,
24001cb0ef41Sopenharmony_ci                             napi_value* result);
24011cb0ef41Sopenharmony_ci```
24021cb0ef41Sopenharmony_ci
24031cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
24041cb0ef41Sopenharmony_ci* `[in] time`: ECMAScript time value in milliseconds since 01 January, 1970 UTC.
24051cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `Date`.
24061cb0ef41Sopenharmony_ci
24071cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
24081cb0ef41Sopenharmony_ci
24091cb0ef41Sopenharmony_ciThis API does not observe leap seconds; they are ignored, as
24101cb0ef41Sopenharmony_ciECMAScript aligns with POSIX time specification.
24111cb0ef41Sopenharmony_ci
24121cb0ef41Sopenharmony_ciThis API allocates a JavaScript `Date` object.
24131cb0ef41Sopenharmony_ci
24141cb0ef41Sopenharmony_ciJavaScript `Date` objects are described in
24151cb0ef41Sopenharmony_ci[Section 20.3][] of the ECMAScript Language Specification.
24161cb0ef41Sopenharmony_ci
24171cb0ef41Sopenharmony_ci#### `napi_create_external`
24181cb0ef41Sopenharmony_ci
24191cb0ef41Sopenharmony_ci<!-- YAML
24201cb0ef41Sopenharmony_ciadded: v8.0.0
24211cb0ef41Sopenharmony_cinapiVersion: 1
24221cb0ef41Sopenharmony_ci-->
24231cb0ef41Sopenharmony_ci
24241cb0ef41Sopenharmony_ci```c
24251cb0ef41Sopenharmony_cinapi_status napi_create_external(napi_env env,
24261cb0ef41Sopenharmony_ci                                 void* data,
24271cb0ef41Sopenharmony_ci                                 napi_finalize finalize_cb,
24281cb0ef41Sopenharmony_ci                                 void* finalize_hint,
24291cb0ef41Sopenharmony_ci                                 napi_value* result)
24301cb0ef41Sopenharmony_ci```
24311cb0ef41Sopenharmony_ci
24321cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
24331cb0ef41Sopenharmony_ci* `[in] data`: Raw pointer to the external data.
24341cb0ef41Sopenharmony_ci* `[in] finalize_cb`: Optional callback to call when the external value is being
24351cb0ef41Sopenharmony_ci  collected. [`napi_finalize`][] provides more details.
24361cb0ef41Sopenharmony_ci* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
24371cb0ef41Sopenharmony_ci  collection.
24381cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing an external value.
24391cb0ef41Sopenharmony_ci
24401cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
24411cb0ef41Sopenharmony_ci
24421cb0ef41Sopenharmony_ciThis API allocates a JavaScript value with external data attached to it. This
24431cb0ef41Sopenharmony_ciis used to pass external data through JavaScript code, so it can be retrieved
24441cb0ef41Sopenharmony_cilater by native code using [`napi_get_value_external`][].
24451cb0ef41Sopenharmony_ci
24461cb0ef41Sopenharmony_ciThe API adds a `napi_finalize` callback which will be called when the JavaScript
24471cb0ef41Sopenharmony_ciobject just created has been garbage collected.
24481cb0ef41Sopenharmony_ci
24491cb0ef41Sopenharmony_ciThe created value is not an object, and therefore does not support additional
24501cb0ef41Sopenharmony_ciproperties. It is considered a distinct value type: calling `napi_typeof()` with
24511cb0ef41Sopenharmony_cian external value yields `napi_external`.
24521cb0ef41Sopenharmony_ci
24531cb0ef41Sopenharmony_ci#### `napi_create_external_arraybuffer`
24541cb0ef41Sopenharmony_ci
24551cb0ef41Sopenharmony_ci<!-- YAML
24561cb0ef41Sopenharmony_ciadded: v8.0.0
24571cb0ef41Sopenharmony_cinapiVersion: 1
24581cb0ef41Sopenharmony_ci-->
24591cb0ef41Sopenharmony_ci
24601cb0ef41Sopenharmony_ci```c
24611cb0ef41Sopenharmony_cinapi_status
24621cb0ef41Sopenharmony_cinapi_create_external_arraybuffer(napi_env env,
24631cb0ef41Sopenharmony_ci                                 void* external_data,
24641cb0ef41Sopenharmony_ci                                 size_t byte_length,
24651cb0ef41Sopenharmony_ci                                 napi_finalize finalize_cb,
24661cb0ef41Sopenharmony_ci                                 void* finalize_hint,
24671cb0ef41Sopenharmony_ci                                 napi_value* result)
24681cb0ef41Sopenharmony_ci```
24691cb0ef41Sopenharmony_ci
24701cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
24711cb0ef41Sopenharmony_ci* `[in] external_data`: Pointer to the underlying byte buffer of the
24721cb0ef41Sopenharmony_ci  `ArrayBuffer`.
24731cb0ef41Sopenharmony_ci* `[in] byte_length`: The length in bytes of the underlying buffer.
24741cb0ef41Sopenharmony_ci* `[in] finalize_cb`: Optional callback to call when the `ArrayBuffer` is being
24751cb0ef41Sopenharmony_ci  collected. [`napi_finalize`][] provides more details.
24761cb0ef41Sopenharmony_ci* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
24771cb0ef41Sopenharmony_ci  collection.
24781cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `ArrayBuffer`.
24791cb0ef41Sopenharmony_ci
24801cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
24811cb0ef41Sopenharmony_ci
24821cb0ef41Sopenharmony_ci**Some runtimes other than Node.js have dropped support for external buffers**.
24831cb0ef41Sopenharmony_ciOn runtimes other than Node.js this method may return
24841cb0ef41Sopenharmony_ci`napi_no_external_buffers_allowed` to indicate that external
24851cb0ef41Sopenharmony_cibuffers are not supported. One such runtime is Electron as
24861cb0ef41Sopenharmony_cidescribed in this issue
24871cb0ef41Sopenharmony_ci[electron/issues/35801](https://github.com/electron/electron/issues/35801).
24881cb0ef41Sopenharmony_ci
24891cb0ef41Sopenharmony_ciIn order to maintain broadest compatibility with all runtimes
24901cb0ef41Sopenharmony_ciyou may define `NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED` in your addon before
24911cb0ef41Sopenharmony_ciincludes for the node-api headers. Doing so will hide the 2 functions
24921cb0ef41Sopenharmony_cithat create external buffers. This will ensure a compilation error
24931cb0ef41Sopenharmony_cioccurs if you accidentally use one of these methods.
24941cb0ef41Sopenharmony_ci
24951cb0ef41Sopenharmony_ciThis API returns a Node-API value corresponding to a JavaScript `ArrayBuffer`.
24961cb0ef41Sopenharmony_ciThe underlying byte buffer of the `ArrayBuffer` is externally allocated and
24971cb0ef41Sopenharmony_cimanaged. The caller must ensure that the byte buffer remains valid until the
24981cb0ef41Sopenharmony_cifinalize callback is called.
24991cb0ef41Sopenharmony_ci
25001cb0ef41Sopenharmony_ciThe API adds a `napi_finalize` callback which will be called when the JavaScript
25011cb0ef41Sopenharmony_ciobject just created has been garbage collected.
25021cb0ef41Sopenharmony_ci
25031cb0ef41Sopenharmony_ciJavaScript `ArrayBuffer`s are described in
25041cb0ef41Sopenharmony_ci[Section 24.1][] of the ECMAScript Language Specification.
25051cb0ef41Sopenharmony_ci
25061cb0ef41Sopenharmony_ci#### `napi_create_external_buffer`
25071cb0ef41Sopenharmony_ci
25081cb0ef41Sopenharmony_ci<!-- YAML
25091cb0ef41Sopenharmony_ciadded: v8.0.0
25101cb0ef41Sopenharmony_cinapiVersion: 1
25111cb0ef41Sopenharmony_ci-->
25121cb0ef41Sopenharmony_ci
25131cb0ef41Sopenharmony_ci```c
25141cb0ef41Sopenharmony_cinapi_status napi_create_external_buffer(napi_env env,
25151cb0ef41Sopenharmony_ci                                        size_t length,
25161cb0ef41Sopenharmony_ci                                        void* data,
25171cb0ef41Sopenharmony_ci                                        napi_finalize finalize_cb,
25181cb0ef41Sopenharmony_ci                                        void* finalize_hint,
25191cb0ef41Sopenharmony_ci                                        napi_value* result)
25201cb0ef41Sopenharmony_ci```
25211cb0ef41Sopenharmony_ci
25221cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
25231cb0ef41Sopenharmony_ci* `[in] length`: Size in bytes of the input buffer (should be the same as the
25241cb0ef41Sopenharmony_ci  size of the new buffer).
25251cb0ef41Sopenharmony_ci* `[in] data`: Raw pointer to the underlying buffer to expose to JavaScript.
25261cb0ef41Sopenharmony_ci* `[in] finalize_cb`: Optional callback to call when the `ArrayBuffer` is being
25271cb0ef41Sopenharmony_ci  collected. [`napi_finalize`][] provides more details.
25281cb0ef41Sopenharmony_ci* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
25291cb0ef41Sopenharmony_ci  collection.
25301cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a `node::Buffer`.
25311cb0ef41Sopenharmony_ci
25321cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
25331cb0ef41Sopenharmony_ci
25341cb0ef41Sopenharmony_ci**Some runtimes other than Node.js have dropped support for external buffers**.
25351cb0ef41Sopenharmony_ciOn runtimes other than Node.js this method may return
25361cb0ef41Sopenharmony_ci`napi_no_external_buffers_allowed` to indicate that external
25371cb0ef41Sopenharmony_cibuffers are not supported. One such runtime is Electron as
25381cb0ef41Sopenharmony_cidescribed in this issue
25391cb0ef41Sopenharmony_ci[electron/issues/35801](https://github.com/electron/electron/issues/35801).
25401cb0ef41Sopenharmony_ci
25411cb0ef41Sopenharmony_ciIn order to maintain broadest compatibility with all runtimes
25421cb0ef41Sopenharmony_ciyou may define `NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED` in your addon before
25431cb0ef41Sopenharmony_ciincludes for the node-api headers. Doing so will hide the 2 functions
25441cb0ef41Sopenharmony_cithat create external buffers. This will ensure a compilation error
25451cb0ef41Sopenharmony_cioccurs if you accidentally use one of these methods.
25461cb0ef41Sopenharmony_ci
25471cb0ef41Sopenharmony_ciThis API allocates a `node::Buffer` object and initializes it with data
25481cb0ef41Sopenharmony_cibacked by the passed in buffer. While this is still a fully-supported data
25491cb0ef41Sopenharmony_cistructure, in most cases using a `TypedArray` will suffice.
25501cb0ef41Sopenharmony_ci
25511cb0ef41Sopenharmony_ciThe API adds a `napi_finalize` callback which will be called when the JavaScript
25521cb0ef41Sopenharmony_ciobject just created has been garbage collected.
25531cb0ef41Sopenharmony_ci
25541cb0ef41Sopenharmony_ciFor Node.js >=4 `Buffers` are `Uint8Array`s.
25551cb0ef41Sopenharmony_ci
25561cb0ef41Sopenharmony_ci#### `napi_create_object`
25571cb0ef41Sopenharmony_ci
25581cb0ef41Sopenharmony_ci<!-- YAML
25591cb0ef41Sopenharmony_ciadded: v8.0.0
25601cb0ef41Sopenharmony_cinapiVersion: 1
25611cb0ef41Sopenharmony_ci-->
25621cb0ef41Sopenharmony_ci
25631cb0ef41Sopenharmony_ci```c
25641cb0ef41Sopenharmony_cinapi_status napi_create_object(napi_env env, napi_value* result)
25651cb0ef41Sopenharmony_ci```
25661cb0ef41Sopenharmony_ci
25671cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
25681cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `Object`.
25691cb0ef41Sopenharmony_ci
25701cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
25711cb0ef41Sopenharmony_ci
25721cb0ef41Sopenharmony_ciThis API allocates a default JavaScript `Object`.
25731cb0ef41Sopenharmony_ciIt is the equivalent of doing `new Object()` in JavaScript.
25741cb0ef41Sopenharmony_ci
25751cb0ef41Sopenharmony_ciThe JavaScript `Object` type is described in [Section 6.1.7][] of the
25761cb0ef41Sopenharmony_ciECMAScript Language Specification.
25771cb0ef41Sopenharmony_ci
25781cb0ef41Sopenharmony_ci#### `napi_create_symbol`
25791cb0ef41Sopenharmony_ci
25801cb0ef41Sopenharmony_ci<!-- YAML
25811cb0ef41Sopenharmony_ciadded: v8.0.0
25821cb0ef41Sopenharmony_cinapiVersion: 1
25831cb0ef41Sopenharmony_ci-->
25841cb0ef41Sopenharmony_ci
25851cb0ef41Sopenharmony_ci```c
25861cb0ef41Sopenharmony_cinapi_status napi_create_symbol(napi_env env,
25871cb0ef41Sopenharmony_ci                               napi_value description,
25881cb0ef41Sopenharmony_ci                               napi_value* result)
25891cb0ef41Sopenharmony_ci```
25901cb0ef41Sopenharmony_ci
25911cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
25921cb0ef41Sopenharmony_ci* `[in] description`: Optional `napi_value` which refers to a JavaScript
25931cb0ef41Sopenharmony_ci  `string` to be set as the description for the symbol.
25941cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `symbol`.
25951cb0ef41Sopenharmony_ci
25961cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
25971cb0ef41Sopenharmony_ci
25981cb0ef41Sopenharmony_ciThis API creates a JavaScript `symbol` value from a UTF8-encoded C string.
25991cb0ef41Sopenharmony_ci
26001cb0ef41Sopenharmony_ciThe JavaScript `symbol` type is described in [Section 19.4][]
26011cb0ef41Sopenharmony_ciof the ECMAScript Language Specification.
26021cb0ef41Sopenharmony_ci
26031cb0ef41Sopenharmony_ci#### `node_api_symbol_for`
26041cb0ef41Sopenharmony_ci
26051cb0ef41Sopenharmony_ci<!-- YAML
26061cb0ef41Sopenharmony_ciadded: v17.5.0
26071cb0ef41Sopenharmony_cinapiVersion: 9
26081cb0ef41Sopenharmony_ci-->
26091cb0ef41Sopenharmony_ci
26101cb0ef41Sopenharmony_ci```c
26111cb0ef41Sopenharmony_cinapi_status node_api_symbol_for(napi_env env,
26121cb0ef41Sopenharmony_ci                                const char* utf8description,
26131cb0ef41Sopenharmony_ci                                size_t length,
26141cb0ef41Sopenharmony_ci                                napi_value* result)
26151cb0ef41Sopenharmony_ci```
26161cb0ef41Sopenharmony_ci
26171cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
26181cb0ef41Sopenharmony_ci* `[in] utf8description`: UTF-8 C string representing the text to be used as the
26191cb0ef41Sopenharmony_ci  description for the symbol.
26201cb0ef41Sopenharmony_ci* `[in] length`: The length of the description string in bytes, or
26211cb0ef41Sopenharmony_ci  `NAPI_AUTO_LENGTH` if it is null-terminated.
26221cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `symbol`.
26231cb0ef41Sopenharmony_ci
26241cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
26251cb0ef41Sopenharmony_ci
26261cb0ef41Sopenharmony_ciThis API searches in the global registry for an existing symbol with the given
26271cb0ef41Sopenharmony_cidescription. If the symbol already exists it will be returned, otherwise a new
26281cb0ef41Sopenharmony_cisymbol will be created in the registry.
26291cb0ef41Sopenharmony_ci
26301cb0ef41Sopenharmony_ciThe JavaScript `symbol` type is described in [Section 19.4][] of the ECMAScript
26311cb0ef41Sopenharmony_ciLanguage Specification.
26321cb0ef41Sopenharmony_ci
26331cb0ef41Sopenharmony_ci#### `napi_create_typedarray`
26341cb0ef41Sopenharmony_ci
26351cb0ef41Sopenharmony_ci<!-- YAML
26361cb0ef41Sopenharmony_ciadded: v8.0.0
26371cb0ef41Sopenharmony_cinapiVersion: 1
26381cb0ef41Sopenharmony_ci-->
26391cb0ef41Sopenharmony_ci
26401cb0ef41Sopenharmony_ci```c
26411cb0ef41Sopenharmony_cinapi_status napi_create_typedarray(napi_env env,
26421cb0ef41Sopenharmony_ci                                   napi_typedarray_type type,
26431cb0ef41Sopenharmony_ci                                   size_t length,
26441cb0ef41Sopenharmony_ci                                   napi_value arraybuffer,
26451cb0ef41Sopenharmony_ci                                   size_t byte_offset,
26461cb0ef41Sopenharmony_ci                                   napi_value* result)
26471cb0ef41Sopenharmony_ci```
26481cb0ef41Sopenharmony_ci
26491cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
26501cb0ef41Sopenharmony_ci* `[in] type`: Scalar datatype of the elements within the `TypedArray`.
26511cb0ef41Sopenharmony_ci* `[in] length`: Number of elements in the `TypedArray`.
26521cb0ef41Sopenharmony_ci* `[in] arraybuffer`: `ArrayBuffer` underlying the typed array.
26531cb0ef41Sopenharmony_ci* `[in] byte_offset`: The byte offset within the `ArrayBuffer` from which to
26541cb0ef41Sopenharmony_ci  start projecting the `TypedArray`.
26551cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `TypedArray`.
26561cb0ef41Sopenharmony_ci
26571cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
26581cb0ef41Sopenharmony_ci
26591cb0ef41Sopenharmony_ciThis API creates a JavaScript `TypedArray` object over an existing
26601cb0ef41Sopenharmony_ci`ArrayBuffer`. `TypedArray` objects provide an array-like view over an
26611cb0ef41Sopenharmony_ciunderlying data buffer where each element has the same underlying binary scalar
26621cb0ef41Sopenharmony_cidatatype.
26631cb0ef41Sopenharmony_ci
26641cb0ef41Sopenharmony_ciIt's required that `(length * size_of_element) + byte_offset` should
26651cb0ef41Sopenharmony_cibe <= the size in bytes of the array passed in. If not, a `RangeError` exception
26661cb0ef41Sopenharmony_ciis raised.
26671cb0ef41Sopenharmony_ci
26681cb0ef41Sopenharmony_ciJavaScript `TypedArray` objects are described in
26691cb0ef41Sopenharmony_ci[Section 22.2][] of the ECMAScript Language Specification.
26701cb0ef41Sopenharmony_ci
26711cb0ef41Sopenharmony_ci#### `napi_create_dataview`
26721cb0ef41Sopenharmony_ci
26731cb0ef41Sopenharmony_ci<!-- YAML
26741cb0ef41Sopenharmony_ciadded: v8.3.0
26751cb0ef41Sopenharmony_cinapiVersion: 1
26761cb0ef41Sopenharmony_ci-->
26771cb0ef41Sopenharmony_ci
26781cb0ef41Sopenharmony_ci```c
26791cb0ef41Sopenharmony_cinapi_status napi_create_dataview(napi_env env,
26801cb0ef41Sopenharmony_ci                                 size_t byte_length,
26811cb0ef41Sopenharmony_ci                                 napi_value arraybuffer,
26821cb0ef41Sopenharmony_ci                                 size_t byte_offset,
26831cb0ef41Sopenharmony_ci                                 napi_value* result)
26841cb0ef41Sopenharmony_ci```
26851cb0ef41Sopenharmony_ci
26861cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
26871cb0ef41Sopenharmony_ci* `[in] length`: Number of elements in the `DataView`.
26881cb0ef41Sopenharmony_ci* `[in] arraybuffer`: `ArrayBuffer` underlying the `DataView`.
26891cb0ef41Sopenharmony_ci* `[in] byte_offset`: The byte offset within the `ArrayBuffer` from which to
26901cb0ef41Sopenharmony_ci  start projecting the `DataView`.
26911cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `DataView`.
26921cb0ef41Sopenharmony_ci
26931cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
26941cb0ef41Sopenharmony_ci
26951cb0ef41Sopenharmony_ciThis API creates a JavaScript `DataView` object over an existing `ArrayBuffer`.
26961cb0ef41Sopenharmony_ci`DataView` objects provide an array-like view over an underlying data buffer,
26971cb0ef41Sopenharmony_cibut one which allows items of different size and type in the `ArrayBuffer`.
26981cb0ef41Sopenharmony_ci
26991cb0ef41Sopenharmony_ciIt is required that `byte_length + byte_offset` is less than or equal to the
27001cb0ef41Sopenharmony_cisize in bytes of the array passed in. If not, a `RangeError` exception is
27011cb0ef41Sopenharmony_ciraised.
27021cb0ef41Sopenharmony_ci
27031cb0ef41Sopenharmony_ciJavaScript `DataView` objects are described in
27041cb0ef41Sopenharmony_ci[Section 24.3][] of the ECMAScript Language Specification.
27051cb0ef41Sopenharmony_ci
27061cb0ef41Sopenharmony_ci### Functions to convert from C types to Node-API
27071cb0ef41Sopenharmony_ci
27081cb0ef41Sopenharmony_ci#### `napi_create_int32`
27091cb0ef41Sopenharmony_ci
27101cb0ef41Sopenharmony_ci<!-- YAML
27111cb0ef41Sopenharmony_ciadded: v8.4.0
27121cb0ef41Sopenharmony_cinapiVersion: 1
27131cb0ef41Sopenharmony_ci-->
27141cb0ef41Sopenharmony_ci
27151cb0ef41Sopenharmony_ci```c
27161cb0ef41Sopenharmony_cinapi_status napi_create_int32(napi_env env, int32_t value, napi_value* result)
27171cb0ef41Sopenharmony_ci```
27181cb0ef41Sopenharmony_ci
27191cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
27201cb0ef41Sopenharmony_ci* `[in] value`: Integer value to be represented in JavaScript.
27211cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `number`.
27221cb0ef41Sopenharmony_ci
27231cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
27241cb0ef41Sopenharmony_ci
27251cb0ef41Sopenharmony_ciThis API is used to convert from the C `int32_t` type to the JavaScript
27261cb0ef41Sopenharmony_ci`number` type.
27271cb0ef41Sopenharmony_ci
27281cb0ef41Sopenharmony_ciThe JavaScript `number` type is described in
27291cb0ef41Sopenharmony_ci[Section 6.1.6][] of the ECMAScript Language Specification.
27301cb0ef41Sopenharmony_ci
27311cb0ef41Sopenharmony_ci#### `napi_create_uint32`
27321cb0ef41Sopenharmony_ci
27331cb0ef41Sopenharmony_ci<!-- YAML
27341cb0ef41Sopenharmony_ciadded: v8.4.0
27351cb0ef41Sopenharmony_cinapiVersion: 1
27361cb0ef41Sopenharmony_ci-->
27371cb0ef41Sopenharmony_ci
27381cb0ef41Sopenharmony_ci```c
27391cb0ef41Sopenharmony_cinapi_status napi_create_uint32(napi_env env, uint32_t value, napi_value* result)
27401cb0ef41Sopenharmony_ci```
27411cb0ef41Sopenharmony_ci
27421cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
27431cb0ef41Sopenharmony_ci* `[in] value`: Unsigned integer value to be represented in JavaScript.
27441cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `number`.
27451cb0ef41Sopenharmony_ci
27461cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
27471cb0ef41Sopenharmony_ci
27481cb0ef41Sopenharmony_ciThis API is used to convert from the C `uint32_t` type to the JavaScript
27491cb0ef41Sopenharmony_ci`number` type.
27501cb0ef41Sopenharmony_ci
27511cb0ef41Sopenharmony_ciThe JavaScript `number` type is described in
27521cb0ef41Sopenharmony_ci[Section 6.1.6][] of the ECMAScript Language Specification.
27531cb0ef41Sopenharmony_ci
27541cb0ef41Sopenharmony_ci#### `napi_create_int64`
27551cb0ef41Sopenharmony_ci
27561cb0ef41Sopenharmony_ci<!-- YAML
27571cb0ef41Sopenharmony_ciadded: v8.4.0
27581cb0ef41Sopenharmony_cinapiVersion: 1
27591cb0ef41Sopenharmony_ci-->
27601cb0ef41Sopenharmony_ci
27611cb0ef41Sopenharmony_ci```c
27621cb0ef41Sopenharmony_cinapi_status napi_create_int64(napi_env env, int64_t value, napi_value* result)
27631cb0ef41Sopenharmony_ci```
27641cb0ef41Sopenharmony_ci
27651cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
27661cb0ef41Sopenharmony_ci* `[in] value`: Integer value to be represented in JavaScript.
27671cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `number`.
27681cb0ef41Sopenharmony_ci
27691cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
27701cb0ef41Sopenharmony_ci
27711cb0ef41Sopenharmony_ciThis API is used to convert from the C `int64_t` type to the JavaScript
27721cb0ef41Sopenharmony_ci`number` type.
27731cb0ef41Sopenharmony_ci
27741cb0ef41Sopenharmony_ciThe JavaScript `number` type is described in [Section 6.1.6][]
27751cb0ef41Sopenharmony_ciof the ECMAScript Language Specification. Note the complete range of `int64_t`
27761cb0ef41Sopenharmony_cicannot be represented with full precision in JavaScript. Integer values
27771cb0ef41Sopenharmony_cioutside the range of [`Number.MIN_SAFE_INTEGER`][] `-(2**53 - 1)` -
27781cb0ef41Sopenharmony_ci[`Number.MAX_SAFE_INTEGER`][] `(2**53 - 1)` will lose precision.
27791cb0ef41Sopenharmony_ci
27801cb0ef41Sopenharmony_ci#### `napi_create_double`
27811cb0ef41Sopenharmony_ci
27821cb0ef41Sopenharmony_ci<!-- YAML
27831cb0ef41Sopenharmony_ciadded: v8.4.0
27841cb0ef41Sopenharmony_cinapiVersion: 1
27851cb0ef41Sopenharmony_ci-->
27861cb0ef41Sopenharmony_ci
27871cb0ef41Sopenharmony_ci```c
27881cb0ef41Sopenharmony_cinapi_status napi_create_double(napi_env env, double value, napi_value* result)
27891cb0ef41Sopenharmony_ci```
27901cb0ef41Sopenharmony_ci
27911cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
27921cb0ef41Sopenharmony_ci* `[in] value`: Double-precision value to be represented in JavaScript.
27931cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `number`.
27941cb0ef41Sopenharmony_ci
27951cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
27961cb0ef41Sopenharmony_ci
27971cb0ef41Sopenharmony_ciThis API is used to convert from the C `double` type to the JavaScript
27981cb0ef41Sopenharmony_ci`number` type.
27991cb0ef41Sopenharmony_ci
28001cb0ef41Sopenharmony_ciThe JavaScript `number` type is described in
28011cb0ef41Sopenharmony_ci[Section 6.1.6][] of the ECMAScript Language Specification.
28021cb0ef41Sopenharmony_ci
28031cb0ef41Sopenharmony_ci#### `napi_create_bigint_int64`
28041cb0ef41Sopenharmony_ci
28051cb0ef41Sopenharmony_ci<!-- YAML
28061cb0ef41Sopenharmony_ciadded: v10.7.0
28071cb0ef41Sopenharmony_cinapiVersion: 6
28081cb0ef41Sopenharmony_ci-->
28091cb0ef41Sopenharmony_ci
28101cb0ef41Sopenharmony_ci```c
28111cb0ef41Sopenharmony_cinapi_status napi_create_bigint_int64(napi_env env,
28121cb0ef41Sopenharmony_ci                                     int64_t value,
28131cb0ef41Sopenharmony_ci                                     napi_value* result);
28141cb0ef41Sopenharmony_ci```
28151cb0ef41Sopenharmony_ci
28161cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
28171cb0ef41Sopenharmony_ci* `[in] value`: Integer value to be represented in JavaScript.
28181cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `BigInt`.
28191cb0ef41Sopenharmony_ci
28201cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
28211cb0ef41Sopenharmony_ci
28221cb0ef41Sopenharmony_ciThis API converts the C `int64_t` type to the JavaScript `BigInt` type.
28231cb0ef41Sopenharmony_ci
28241cb0ef41Sopenharmony_ci#### `napi_create_bigint_uint64`
28251cb0ef41Sopenharmony_ci
28261cb0ef41Sopenharmony_ci<!-- YAML
28271cb0ef41Sopenharmony_ciadded: v10.7.0
28281cb0ef41Sopenharmony_cinapiVersion: 6
28291cb0ef41Sopenharmony_ci-->
28301cb0ef41Sopenharmony_ci
28311cb0ef41Sopenharmony_ci```c
28321cb0ef41Sopenharmony_cinapi_status napi_create_bigint_uint64(napi_env env,
28331cb0ef41Sopenharmony_ci                                      uint64_t value,
28341cb0ef41Sopenharmony_ci                                      napi_value* result);
28351cb0ef41Sopenharmony_ci```
28361cb0ef41Sopenharmony_ci
28371cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
28381cb0ef41Sopenharmony_ci* `[in] value`: Unsigned integer value to be represented in JavaScript.
28391cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `BigInt`.
28401cb0ef41Sopenharmony_ci
28411cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
28421cb0ef41Sopenharmony_ci
28431cb0ef41Sopenharmony_ciThis API converts the C `uint64_t` type to the JavaScript `BigInt` type.
28441cb0ef41Sopenharmony_ci
28451cb0ef41Sopenharmony_ci#### `napi_create_bigint_words`
28461cb0ef41Sopenharmony_ci
28471cb0ef41Sopenharmony_ci<!-- YAML
28481cb0ef41Sopenharmony_ciadded: v10.7.0
28491cb0ef41Sopenharmony_cinapiVersion: 6
28501cb0ef41Sopenharmony_ci-->
28511cb0ef41Sopenharmony_ci
28521cb0ef41Sopenharmony_ci```c
28531cb0ef41Sopenharmony_cinapi_status napi_create_bigint_words(napi_env env,
28541cb0ef41Sopenharmony_ci                                     int sign_bit,
28551cb0ef41Sopenharmony_ci                                     size_t word_count,
28561cb0ef41Sopenharmony_ci                                     const uint64_t* words,
28571cb0ef41Sopenharmony_ci                                     napi_value* result);
28581cb0ef41Sopenharmony_ci```
28591cb0ef41Sopenharmony_ci
28601cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
28611cb0ef41Sopenharmony_ci* `[in] sign_bit`: Determines if the resulting `BigInt` will be positive or
28621cb0ef41Sopenharmony_ci  negative.
28631cb0ef41Sopenharmony_ci* `[in] word_count`: The length of the `words` array.
28641cb0ef41Sopenharmony_ci* `[in] words`: An array of `uint64_t` little-endian 64-bit words.
28651cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `BigInt`.
28661cb0ef41Sopenharmony_ci
28671cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
28681cb0ef41Sopenharmony_ci
28691cb0ef41Sopenharmony_ciThis API converts an array of unsigned 64-bit words into a single `BigInt`
28701cb0ef41Sopenharmony_civalue.
28711cb0ef41Sopenharmony_ci
28721cb0ef41Sopenharmony_ciThe resulting `BigInt` is calculated as: (–1)<sup>`sign_bit`</sup> (`words[0]`
28731cb0ef41Sopenharmony_ci× (2<sup>64</sup>)<sup>0</sup> + `words[1]` × (2<sup>64</sup>)<sup>1</sup> + …)
28741cb0ef41Sopenharmony_ci
28751cb0ef41Sopenharmony_ci#### `napi_create_string_latin1`
28761cb0ef41Sopenharmony_ci
28771cb0ef41Sopenharmony_ci<!-- YAML
28781cb0ef41Sopenharmony_ciadded: v8.0.0
28791cb0ef41Sopenharmony_cinapiVersion: 1
28801cb0ef41Sopenharmony_ci-->
28811cb0ef41Sopenharmony_ci
28821cb0ef41Sopenharmony_ci```c
28831cb0ef41Sopenharmony_cinapi_status napi_create_string_latin1(napi_env env,
28841cb0ef41Sopenharmony_ci                                      const char* str,
28851cb0ef41Sopenharmony_ci                                      size_t length,
28861cb0ef41Sopenharmony_ci                                      napi_value* result);
28871cb0ef41Sopenharmony_ci```
28881cb0ef41Sopenharmony_ci
28891cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
28901cb0ef41Sopenharmony_ci* `[in] str`: Character buffer representing an ISO-8859-1-encoded string.
28911cb0ef41Sopenharmony_ci* `[in] length`: The length of the string in bytes, or `NAPI_AUTO_LENGTH` if it
28921cb0ef41Sopenharmony_ci  is null-terminated.
28931cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `string`.
28941cb0ef41Sopenharmony_ci
28951cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
28961cb0ef41Sopenharmony_ci
28971cb0ef41Sopenharmony_ciThis API creates a JavaScript `string` value from an ISO-8859-1-encoded C
28981cb0ef41Sopenharmony_cistring. The native string is copied.
28991cb0ef41Sopenharmony_ci
29001cb0ef41Sopenharmony_ciThe JavaScript `string` type is described in
29011cb0ef41Sopenharmony_ci[Section 6.1.4][] of the ECMAScript Language Specification.
29021cb0ef41Sopenharmony_ci
29031cb0ef41Sopenharmony_ci#### `node_api_create_external_string_latin1`
29041cb0ef41Sopenharmony_ci
29051cb0ef41Sopenharmony_ci<!-- YAML
29061cb0ef41Sopenharmony_ciadded: v18.18.0
29071cb0ef41Sopenharmony_ci-->
29081cb0ef41Sopenharmony_ci
29091cb0ef41Sopenharmony_ci> Stability: 1 - Experimental
29101cb0ef41Sopenharmony_ci
29111cb0ef41Sopenharmony_ci```c
29121cb0ef41Sopenharmony_cinapi_status
29131cb0ef41Sopenharmony_cinode_api_create_external_string_latin1(napi_env env,
29141cb0ef41Sopenharmony_ci                                       char* str,
29151cb0ef41Sopenharmony_ci                                       size_t length,
29161cb0ef41Sopenharmony_ci                                       napi_finalize finalize_callback,
29171cb0ef41Sopenharmony_ci                                       void* finalize_hint,
29181cb0ef41Sopenharmony_ci                                       napi_value* result,
29191cb0ef41Sopenharmony_ci                                       bool* copied);
29201cb0ef41Sopenharmony_ci```
29211cb0ef41Sopenharmony_ci
29221cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
29231cb0ef41Sopenharmony_ci* `[in] str`: Character buffer representing an ISO-8859-1-encoded string.
29241cb0ef41Sopenharmony_ci* `[in] length`: The length of the string in bytes, or `NAPI_AUTO_LENGTH` if it
29251cb0ef41Sopenharmony_ci  is null-terminated.
29261cb0ef41Sopenharmony_ci* `[in] finalize_callback`: The function to call when the string is being
29271cb0ef41Sopenharmony_ci  collected. The function will be called with the following parameters:
29281cb0ef41Sopenharmony_ci  * `[in] env`: The environment in which the add-on is running. This value
29291cb0ef41Sopenharmony_ci    may be null if the string is being collected as part of the termination
29301cb0ef41Sopenharmony_ci    of the worker or the main Node.js instance.
29311cb0ef41Sopenharmony_ci  * `[in] data`: This is the value `str` as a `void*` pointer.
29321cb0ef41Sopenharmony_ci  * `[in] finalize_hint`: This is the value `finalize_hint` that was given
29331cb0ef41Sopenharmony_ci    to the API.
29341cb0ef41Sopenharmony_ci    [`napi_finalize`][] provides more details.
29351cb0ef41Sopenharmony_ci    This parameter is optional. Passing a null value means that the add-on
29361cb0ef41Sopenharmony_ci    doesn't need to be notified when the corresponding JavaScript string is
29371cb0ef41Sopenharmony_ci    collected.
29381cb0ef41Sopenharmony_ci* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
29391cb0ef41Sopenharmony_ci  collection.
29401cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `string`.
29411cb0ef41Sopenharmony_ci* `[out] copied`: Whether the string was copied. If it was, the finalizer will
29421cb0ef41Sopenharmony_ci  already have been invoked to destroy `str`.
29431cb0ef41Sopenharmony_ci
29441cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
29451cb0ef41Sopenharmony_ci
29461cb0ef41Sopenharmony_ciThis API creates a JavaScript `string` value from an ISO-8859-1-encoded C
29471cb0ef41Sopenharmony_cistring. The native string may not be copied and must thus exist for the entire
29481cb0ef41Sopenharmony_cilife cycle of the JavaScript value.
29491cb0ef41Sopenharmony_ci
29501cb0ef41Sopenharmony_ciThe JavaScript `string` type is described in
29511cb0ef41Sopenharmony_ci[Section 6.1.4][] of the ECMAScript Language Specification.
29521cb0ef41Sopenharmony_ci
29531cb0ef41Sopenharmony_ci#### `napi_create_string_utf16`
29541cb0ef41Sopenharmony_ci
29551cb0ef41Sopenharmony_ci<!-- YAML
29561cb0ef41Sopenharmony_ciadded: v8.0.0
29571cb0ef41Sopenharmony_cinapiVersion: 1
29581cb0ef41Sopenharmony_ci-->
29591cb0ef41Sopenharmony_ci
29601cb0ef41Sopenharmony_ci```c
29611cb0ef41Sopenharmony_cinapi_status napi_create_string_utf16(napi_env env,
29621cb0ef41Sopenharmony_ci                                     const char16_t* str,
29631cb0ef41Sopenharmony_ci                                     size_t length,
29641cb0ef41Sopenharmony_ci                                     napi_value* result)
29651cb0ef41Sopenharmony_ci```
29661cb0ef41Sopenharmony_ci
29671cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
29681cb0ef41Sopenharmony_ci* `[in] str`: Character buffer representing a UTF16-LE-encoded string.
29691cb0ef41Sopenharmony_ci* `[in] length`: The length of the string in two-byte code units, or
29701cb0ef41Sopenharmony_ci  `NAPI_AUTO_LENGTH` if it is null-terminated.
29711cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `string`.
29721cb0ef41Sopenharmony_ci
29731cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
29741cb0ef41Sopenharmony_ci
29751cb0ef41Sopenharmony_ciThis API creates a JavaScript `string` value from a UTF16-LE-encoded C string.
29761cb0ef41Sopenharmony_ciThe native string is copied.
29771cb0ef41Sopenharmony_ci
29781cb0ef41Sopenharmony_ciThe JavaScript `string` type is described in
29791cb0ef41Sopenharmony_ci[Section 6.1.4][] of the ECMAScript Language Specification.
29801cb0ef41Sopenharmony_ci
29811cb0ef41Sopenharmony_ci#### `node_api_create_external_string_utf16`
29821cb0ef41Sopenharmony_ci
29831cb0ef41Sopenharmony_ci<!-- YAML
29841cb0ef41Sopenharmony_ciadded: v18.18.0
29851cb0ef41Sopenharmony_ci-->
29861cb0ef41Sopenharmony_ci
29871cb0ef41Sopenharmony_ci> Stability: 1 - Experimental
29881cb0ef41Sopenharmony_ci
29891cb0ef41Sopenharmony_ci```c
29901cb0ef41Sopenharmony_cinapi_status
29911cb0ef41Sopenharmony_cinode_api_create_external_string_utf16(napi_env env,
29921cb0ef41Sopenharmony_ci                                      char16_t* str,
29931cb0ef41Sopenharmony_ci                                      size_t length,
29941cb0ef41Sopenharmony_ci                                      napi_finalize finalize_callback,
29951cb0ef41Sopenharmony_ci                                      void* finalize_hint,
29961cb0ef41Sopenharmony_ci                                      napi_value* result,
29971cb0ef41Sopenharmony_ci                                      bool* copied);
29981cb0ef41Sopenharmony_ci```
29991cb0ef41Sopenharmony_ci
30001cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
30011cb0ef41Sopenharmony_ci* `[in] str`: Character buffer representing a UTF16-LE-encoded string.
30021cb0ef41Sopenharmony_ci* `[in] length`: The length of the string in two-byte code units, or
30031cb0ef41Sopenharmony_ci  `NAPI_AUTO_LENGTH` if it is null-terminated.
30041cb0ef41Sopenharmony_ci* `[in] finalize_callback`: The function to call when the string is being
30051cb0ef41Sopenharmony_ci  collected. The function will be called with the following parameters:
30061cb0ef41Sopenharmony_ci  * `[in] env`: The environment in which the add-on is running. This value
30071cb0ef41Sopenharmony_ci    may be null if the string is being collected as part of the termination
30081cb0ef41Sopenharmony_ci    of the worker or the main Node.js instance.
30091cb0ef41Sopenharmony_ci  * `[in] data`: This is the value `str` as a `void*` pointer.
30101cb0ef41Sopenharmony_ci  * `[in] finalize_hint`: This is the value `finalize_hint` that was given
30111cb0ef41Sopenharmony_ci    to the API.
30121cb0ef41Sopenharmony_ci    [`napi_finalize`][] provides more details.
30131cb0ef41Sopenharmony_ci    This parameter is optional. Passing a null value means that the add-on
30141cb0ef41Sopenharmony_ci    doesn't need to be notified when the corresponding JavaScript string is
30151cb0ef41Sopenharmony_ci    collected.
30161cb0ef41Sopenharmony_ci* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
30171cb0ef41Sopenharmony_ci  collection.
30181cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `string`.
30191cb0ef41Sopenharmony_ci* `[out] copied`: Whether the string was copied. If it was, the finalizer will
30201cb0ef41Sopenharmony_ci  already have been invoked to destroy `str`.
30211cb0ef41Sopenharmony_ci
30221cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
30231cb0ef41Sopenharmony_ci
30241cb0ef41Sopenharmony_ciThis API creates a JavaScript `string` value from a UTF16-LE-encoded C string.
30251cb0ef41Sopenharmony_ciThe native string may not be copied and must thus exist for the entire life
30261cb0ef41Sopenharmony_cicycle of the JavaScript value.
30271cb0ef41Sopenharmony_ci
30281cb0ef41Sopenharmony_ciThe JavaScript `string` type is described in
30291cb0ef41Sopenharmony_ci[Section 6.1.4][] of the ECMAScript Language Specification.
30301cb0ef41Sopenharmony_ci
30311cb0ef41Sopenharmony_ci#### `napi_create_string_utf8`
30321cb0ef41Sopenharmony_ci
30331cb0ef41Sopenharmony_ci<!-- YAML
30341cb0ef41Sopenharmony_ciadded: v8.0.0
30351cb0ef41Sopenharmony_cinapiVersion: 1
30361cb0ef41Sopenharmony_ci-->
30371cb0ef41Sopenharmony_ci
30381cb0ef41Sopenharmony_ci```c
30391cb0ef41Sopenharmony_cinapi_status napi_create_string_utf8(napi_env env,
30401cb0ef41Sopenharmony_ci                                    const char* str,
30411cb0ef41Sopenharmony_ci                                    size_t length,
30421cb0ef41Sopenharmony_ci                                    napi_value* result)
30431cb0ef41Sopenharmony_ci```
30441cb0ef41Sopenharmony_ci
30451cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
30461cb0ef41Sopenharmony_ci* `[in] str`: Character buffer representing a UTF8-encoded string.
30471cb0ef41Sopenharmony_ci* `[in] length`: The length of the string in bytes, or `NAPI_AUTO_LENGTH` if it
30481cb0ef41Sopenharmony_ci  is null-terminated.
30491cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing a JavaScript `string`.
30501cb0ef41Sopenharmony_ci
30511cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
30521cb0ef41Sopenharmony_ci
30531cb0ef41Sopenharmony_ciThis API creates a JavaScript `string` value from a UTF8-encoded C string.
30541cb0ef41Sopenharmony_ciThe native string is copied.
30551cb0ef41Sopenharmony_ci
30561cb0ef41Sopenharmony_ciThe JavaScript `string` type is described in
30571cb0ef41Sopenharmony_ci[Section 6.1.4][] of the ECMAScript Language Specification.
30581cb0ef41Sopenharmony_ci
30591cb0ef41Sopenharmony_ci### Functions to convert from Node-API to C types
30601cb0ef41Sopenharmony_ci
30611cb0ef41Sopenharmony_ci#### `napi_get_array_length`
30621cb0ef41Sopenharmony_ci
30631cb0ef41Sopenharmony_ci<!-- YAML
30641cb0ef41Sopenharmony_ciadded: v8.0.0
30651cb0ef41Sopenharmony_cinapiVersion: 1
30661cb0ef41Sopenharmony_ci-->
30671cb0ef41Sopenharmony_ci
30681cb0ef41Sopenharmony_ci```c
30691cb0ef41Sopenharmony_cinapi_status napi_get_array_length(napi_env env,
30701cb0ef41Sopenharmony_ci                                  napi_value value,
30711cb0ef41Sopenharmony_ci                                  uint32_t* result)
30721cb0ef41Sopenharmony_ci```
30731cb0ef41Sopenharmony_ci
30741cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
30751cb0ef41Sopenharmony_ci* `[in] value`: `napi_value` representing the JavaScript `Array` whose length is
30761cb0ef41Sopenharmony_ci  being queried.
30771cb0ef41Sopenharmony_ci* `[out] result`: `uint32` representing length of the array.
30781cb0ef41Sopenharmony_ci
30791cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
30801cb0ef41Sopenharmony_ci
30811cb0ef41Sopenharmony_ciThis API returns the length of an array.
30821cb0ef41Sopenharmony_ci
30831cb0ef41Sopenharmony_ci`Array` length is described in [Section 22.1.4.1][] of the ECMAScript Language
30841cb0ef41Sopenharmony_ciSpecification.
30851cb0ef41Sopenharmony_ci
30861cb0ef41Sopenharmony_ci#### `napi_get_arraybuffer_info`
30871cb0ef41Sopenharmony_ci
30881cb0ef41Sopenharmony_ci<!-- YAML
30891cb0ef41Sopenharmony_ciadded: v8.0.0
30901cb0ef41Sopenharmony_cinapiVersion: 1
30911cb0ef41Sopenharmony_ci-->
30921cb0ef41Sopenharmony_ci
30931cb0ef41Sopenharmony_ci```c
30941cb0ef41Sopenharmony_cinapi_status napi_get_arraybuffer_info(napi_env env,
30951cb0ef41Sopenharmony_ci                                      napi_value arraybuffer,
30961cb0ef41Sopenharmony_ci                                      void** data,
30971cb0ef41Sopenharmony_ci                                      size_t* byte_length)
30981cb0ef41Sopenharmony_ci```
30991cb0ef41Sopenharmony_ci
31001cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
31011cb0ef41Sopenharmony_ci* `[in] arraybuffer`: `napi_value` representing the `ArrayBuffer` being queried.
31021cb0ef41Sopenharmony_ci* `[out] data`: The underlying data buffer of the `ArrayBuffer`. If byte\_length
31031cb0ef41Sopenharmony_ci  is `0`, this may be `NULL` or any other pointer value.
31041cb0ef41Sopenharmony_ci* `[out] byte_length`: Length in bytes of the underlying data buffer.
31051cb0ef41Sopenharmony_ci
31061cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
31071cb0ef41Sopenharmony_ci
31081cb0ef41Sopenharmony_ciThis API is used to retrieve the underlying data buffer of an `ArrayBuffer` and
31091cb0ef41Sopenharmony_ciits length.
31101cb0ef41Sopenharmony_ci
31111cb0ef41Sopenharmony_ci_WARNING_: Use caution while using this API. The lifetime of the underlying data
31121cb0ef41Sopenharmony_cibuffer is managed by the `ArrayBuffer` even after it's returned. A
31131cb0ef41Sopenharmony_cipossible safe way to use this API is in conjunction with
31141cb0ef41Sopenharmony_ci[`napi_create_reference`][], which can be used to guarantee control over the
31151cb0ef41Sopenharmony_cilifetime of the `ArrayBuffer`. It's also safe to use the returned data buffer
31161cb0ef41Sopenharmony_ciwithin the same callback as long as there are no calls to other APIs that might
31171cb0ef41Sopenharmony_citrigger a GC.
31181cb0ef41Sopenharmony_ci
31191cb0ef41Sopenharmony_ci#### `napi_get_buffer_info`
31201cb0ef41Sopenharmony_ci
31211cb0ef41Sopenharmony_ci<!-- YAML
31221cb0ef41Sopenharmony_ciadded: v8.0.0
31231cb0ef41Sopenharmony_cinapiVersion: 1
31241cb0ef41Sopenharmony_ci-->
31251cb0ef41Sopenharmony_ci
31261cb0ef41Sopenharmony_ci```c
31271cb0ef41Sopenharmony_cinapi_status napi_get_buffer_info(napi_env env,
31281cb0ef41Sopenharmony_ci                                 napi_value value,
31291cb0ef41Sopenharmony_ci                                 void** data,
31301cb0ef41Sopenharmony_ci                                 size_t* length)
31311cb0ef41Sopenharmony_ci```
31321cb0ef41Sopenharmony_ci
31331cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
31341cb0ef41Sopenharmony_ci* `[in] value`: `napi_value` representing the `node::Buffer` or `Uint8Array`
31351cb0ef41Sopenharmony_ci  being queried.
31361cb0ef41Sopenharmony_ci* `[out] data`: The underlying data buffer of the `node::Buffer` or
31371cb0ef41Sopenharmony_ci  `Uint8Array`. If length is `0`, this may be `NULL` or any other pointer value.
31381cb0ef41Sopenharmony_ci* `[out] length`: Length in bytes of the underlying data buffer.
31391cb0ef41Sopenharmony_ci
31401cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
31411cb0ef41Sopenharmony_ci
31421cb0ef41Sopenharmony_ciThis method returns the identical `data` and `byte_length` as
31431cb0ef41Sopenharmony_ci[`napi_get_typedarray_info`][]. And `napi_get_typedarray_info` accepts a
31441cb0ef41Sopenharmony_ci`node::Buffer` (a Uint8Array) as the value too.
31451cb0ef41Sopenharmony_ci
31461cb0ef41Sopenharmony_ciThis API is used to retrieve the underlying data buffer of a `node::Buffer`
31471cb0ef41Sopenharmony_ciand its length.
31481cb0ef41Sopenharmony_ci
31491cb0ef41Sopenharmony_ci_Warning_: Use caution while using this API since the underlying data buffer's
31501cb0ef41Sopenharmony_cilifetime is not guaranteed if it's managed by the VM.
31511cb0ef41Sopenharmony_ci
31521cb0ef41Sopenharmony_ci#### `napi_get_prototype`
31531cb0ef41Sopenharmony_ci
31541cb0ef41Sopenharmony_ci<!-- YAML
31551cb0ef41Sopenharmony_ciadded: v8.0.0
31561cb0ef41Sopenharmony_cinapiVersion: 1
31571cb0ef41Sopenharmony_ci-->
31581cb0ef41Sopenharmony_ci
31591cb0ef41Sopenharmony_ci```c
31601cb0ef41Sopenharmony_cinapi_status napi_get_prototype(napi_env env,
31611cb0ef41Sopenharmony_ci                               napi_value object,
31621cb0ef41Sopenharmony_ci                               napi_value* result)
31631cb0ef41Sopenharmony_ci```
31641cb0ef41Sopenharmony_ci
31651cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
31661cb0ef41Sopenharmony_ci* `[in] object`: `napi_value` representing JavaScript `Object` whose prototype
31671cb0ef41Sopenharmony_ci  to return. This returns the equivalent of `Object.getPrototypeOf` (which is
31681cb0ef41Sopenharmony_ci  not the same as the function's `prototype` property).
31691cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing prototype of the given object.
31701cb0ef41Sopenharmony_ci
31711cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
31721cb0ef41Sopenharmony_ci
31731cb0ef41Sopenharmony_ci#### `napi_get_typedarray_info`
31741cb0ef41Sopenharmony_ci
31751cb0ef41Sopenharmony_ci<!-- YAML
31761cb0ef41Sopenharmony_ciadded: v8.0.0
31771cb0ef41Sopenharmony_cinapiVersion: 1
31781cb0ef41Sopenharmony_ci-->
31791cb0ef41Sopenharmony_ci
31801cb0ef41Sopenharmony_ci```c
31811cb0ef41Sopenharmony_cinapi_status napi_get_typedarray_info(napi_env env,
31821cb0ef41Sopenharmony_ci                                     napi_value typedarray,
31831cb0ef41Sopenharmony_ci                                     napi_typedarray_type* type,
31841cb0ef41Sopenharmony_ci                                     size_t* length,
31851cb0ef41Sopenharmony_ci                                     void** data,
31861cb0ef41Sopenharmony_ci                                     napi_value* arraybuffer,
31871cb0ef41Sopenharmony_ci                                     size_t* byte_offset)
31881cb0ef41Sopenharmony_ci```
31891cb0ef41Sopenharmony_ci
31901cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
31911cb0ef41Sopenharmony_ci* `[in] typedarray`: `napi_value` representing the `TypedArray` whose
31921cb0ef41Sopenharmony_ci  properties to query.
31931cb0ef41Sopenharmony_ci* `[out] type`: Scalar datatype of the elements within the `TypedArray`.
31941cb0ef41Sopenharmony_ci* `[out] length`: The number of elements in the `TypedArray`.
31951cb0ef41Sopenharmony_ci* `[out] data`: The data buffer underlying the `TypedArray` adjusted by
31961cb0ef41Sopenharmony_ci  the `byte_offset` value so that it points to the first element in the
31971cb0ef41Sopenharmony_ci  `TypedArray`. If the length of the array is `0`, this may be `NULL` or
31981cb0ef41Sopenharmony_ci  any other pointer value.
31991cb0ef41Sopenharmony_ci* `[out] arraybuffer`: The `ArrayBuffer` underlying the `TypedArray`.
32001cb0ef41Sopenharmony_ci* `[out] byte_offset`: The byte offset within the underlying native array
32011cb0ef41Sopenharmony_ci  at which the first element of the arrays is located. The value for the data
32021cb0ef41Sopenharmony_ci  parameter has already been adjusted so that data points to the first element
32031cb0ef41Sopenharmony_ci  in the array. Therefore, the first byte of the native array would be at
32041cb0ef41Sopenharmony_ci  `data - byte_offset`.
32051cb0ef41Sopenharmony_ci
32061cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
32071cb0ef41Sopenharmony_ci
32081cb0ef41Sopenharmony_ciThis API returns various properties of a typed array.
32091cb0ef41Sopenharmony_ci
32101cb0ef41Sopenharmony_ciAny of the out parameters may be `NULL` if that property is unneeded.
32111cb0ef41Sopenharmony_ci
32121cb0ef41Sopenharmony_ci_Warning_: Use caution while using this API since the underlying data buffer
32131cb0ef41Sopenharmony_ciis managed by the VM.
32141cb0ef41Sopenharmony_ci
32151cb0ef41Sopenharmony_ci#### `napi_get_dataview_info`
32161cb0ef41Sopenharmony_ci
32171cb0ef41Sopenharmony_ci<!-- YAML
32181cb0ef41Sopenharmony_ciadded: v8.3.0
32191cb0ef41Sopenharmony_cinapiVersion: 1
32201cb0ef41Sopenharmony_ci-->
32211cb0ef41Sopenharmony_ci
32221cb0ef41Sopenharmony_ci```c
32231cb0ef41Sopenharmony_cinapi_status napi_get_dataview_info(napi_env env,
32241cb0ef41Sopenharmony_ci                                   napi_value dataview,
32251cb0ef41Sopenharmony_ci                                   size_t* byte_length,
32261cb0ef41Sopenharmony_ci                                   void** data,
32271cb0ef41Sopenharmony_ci                                   napi_value* arraybuffer,
32281cb0ef41Sopenharmony_ci                                   size_t* byte_offset)
32291cb0ef41Sopenharmony_ci```
32301cb0ef41Sopenharmony_ci
32311cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
32321cb0ef41Sopenharmony_ci* `[in] dataview`: `napi_value` representing the `DataView` whose
32331cb0ef41Sopenharmony_ci  properties to query.
32341cb0ef41Sopenharmony_ci* `[out] byte_length`: Number of bytes in the `DataView`.
32351cb0ef41Sopenharmony_ci* `[out] data`: The data buffer underlying the `DataView`.
32361cb0ef41Sopenharmony_ci  If byte\_length is `0`, this may be `NULL` or any other pointer value.
32371cb0ef41Sopenharmony_ci* `[out] arraybuffer`: `ArrayBuffer` underlying the `DataView`.
32381cb0ef41Sopenharmony_ci* `[out] byte_offset`: The byte offset within the data buffer from which
32391cb0ef41Sopenharmony_ci  to start projecting the `DataView`.
32401cb0ef41Sopenharmony_ci
32411cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
32421cb0ef41Sopenharmony_ci
32431cb0ef41Sopenharmony_ciAny of the out parameters may be `NULL` if that property is unneeded.
32441cb0ef41Sopenharmony_ci
32451cb0ef41Sopenharmony_ciThis API returns various properties of a `DataView`.
32461cb0ef41Sopenharmony_ci
32471cb0ef41Sopenharmony_ci#### `napi_get_date_value`
32481cb0ef41Sopenharmony_ci
32491cb0ef41Sopenharmony_ci<!-- YAML
32501cb0ef41Sopenharmony_ciadded:
32511cb0ef41Sopenharmony_ci - v11.11.0
32521cb0ef41Sopenharmony_ci - v10.17.0
32531cb0ef41Sopenharmony_cinapiVersion: 5
32541cb0ef41Sopenharmony_ci-->
32551cb0ef41Sopenharmony_ci
32561cb0ef41Sopenharmony_ci```c
32571cb0ef41Sopenharmony_cinapi_status napi_get_date_value(napi_env env,
32581cb0ef41Sopenharmony_ci                                napi_value value,
32591cb0ef41Sopenharmony_ci                                double* result)
32601cb0ef41Sopenharmony_ci```
32611cb0ef41Sopenharmony_ci
32621cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
32631cb0ef41Sopenharmony_ci* `[in] value`: `napi_value` representing a JavaScript `Date`.
32641cb0ef41Sopenharmony_ci* `[out] result`: Time value as a `double` represented as milliseconds since
32651cb0ef41Sopenharmony_ci  midnight at the beginning of 01 January, 1970 UTC.
32661cb0ef41Sopenharmony_ci
32671cb0ef41Sopenharmony_ciThis API does not observe leap seconds; they are ignored, as
32681cb0ef41Sopenharmony_ciECMAScript aligns with POSIX time specification.
32691cb0ef41Sopenharmony_ci
32701cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded. If a non-date `napi_value` is passed
32711cb0ef41Sopenharmony_ciin it returns `napi_date_expected`.
32721cb0ef41Sopenharmony_ci
32731cb0ef41Sopenharmony_ciThis API returns the C double primitive of time value for the given JavaScript
32741cb0ef41Sopenharmony_ci`Date`.
32751cb0ef41Sopenharmony_ci
32761cb0ef41Sopenharmony_ci#### `napi_get_value_bool`
32771cb0ef41Sopenharmony_ci
32781cb0ef41Sopenharmony_ci<!-- YAML
32791cb0ef41Sopenharmony_ciadded: v8.0.0
32801cb0ef41Sopenharmony_cinapiVersion: 1
32811cb0ef41Sopenharmony_ci-->
32821cb0ef41Sopenharmony_ci
32831cb0ef41Sopenharmony_ci```c
32841cb0ef41Sopenharmony_cinapi_status napi_get_value_bool(napi_env env, napi_value value, bool* result)
32851cb0ef41Sopenharmony_ci```
32861cb0ef41Sopenharmony_ci
32871cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
32881cb0ef41Sopenharmony_ci* `[in] value`: `napi_value` representing JavaScript `Boolean`.
32891cb0ef41Sopenharmony_ci* `[out] result`: C boolean primitive equivalent of the given JavaScript
32901cb0ef41Sopenharmony_ci  `Boolean`.
32911cb0ef41Sopenharmony_ci
32921cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded. If a non-boolean `napi_value` is
32931cb0ef41Sopenharmony_cipassed in it returns `napi_boolean_expected`.
32941cb0ef41Sopenharmony_ci
32951cb0ef41Sopenharmony_ciThis API returns the C boolean primitive equivalent of the given JavaScript
32961cb0ef41Sopenharmony_ci`Boolean`.
32971cb0ef41Sopenharmony_ci
32981cb0ef41Sopenharmony_ci#### `napi_get_value_double`
32991cb0ef41Sopenharmony_ci
33001cb0ef41Sopenharmony_ci<!-- YAML
33011cb0ef41Sopenharmony_ciadded: v8.0.0
33021cb0ef41Sopenharmony_cinapiVersion: 1
33031cb0ef41Sopenharmony_ci-->
33041cb0ef41Sopenharmony_ci
33051cb0ef41Sopenharmony_ci```c
33061cb0ef41Sopenharmony_cinapi_status napi_get_value_double(napi_env env,
33071cb0ef41Sopenharmony_ci                                  napi_value value,
33081cb0ef41Sopenharmony_ci                                  double* result)
33091cb0ef41Sopenharmony_ci```
33101cb0ef41Sopenharmony_ci
33111cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
33121cb0ef41Sopenharmony_ci* `[in] value`: `napi_value` representing JavaScript `number`.
33131cb0ef41Sopenharmony_ci* `[out] result`: C double primitive equivalent of the given JavaScript
33141cb0ef41Sopenharmony_ci  `number`.
33151cb0ef41Sopenharmony_ci
33161cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded. If a non-number `napi_value` is passed
33171cb0ef41Sopenharmony_ciin it returns `napi_number_expected`.
33181cb0ef41Sopenharmony_ci
33191cb0ef41Sopenharmony_ciThis API returns the C double primitive equivalent of the given JavaScript
33201cb0ef41Sopenharmony_ci`number`.
33211cb0ef41Sopenharmony_ci
33221cb0ef41Sopenharmony_ci#### `napi_get_value_bigint_int64`
33231cb0ef41Sopenharmony_ci
33241cb0ef41Sopenharmony_ci<!-- YAML
33251cb0ef41Sopenharmony_ciadded: v10.7.0
33261cb0ef41Sopenharmony_cinapiVersion: 6
33271cb0ef41Sopenharmony_ci-->
33281cb0ef41Sopenharmony_ci
33291cb0ef41Sopenharmony_ci```c
33301cb0ef41Sopenharmony_cinapi_status napi_get_value_bigint_int64(napi_env env,
33311cb0ef41Sopenharmony_ci                                        napi_value value,
33321cb0ef41Sopenharmony_ci                                        int64_t* result,
33331cb0ef41Sopenharmony_ci                                        bool* lossless);
33341cb0ef41Sopenharmony_ci```
33351cb0ef41Sopenharmony_ci
33361cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under
33371cb0ef41Sopenharmony_ci* `[in] value`: `napi_value` representing JavaScript `BigInt`.
33381cb0ef41Sopenharmony_ci* `[out] result`: C `int64_t` primitive equivalent of the given JavaScript
33391cb0ef41Sopenharmony_ci  `BigInt`.
33401cb0ef41Sopenharmony_ci* `[out] lossless`: Indicates whether the `BigInt` value was converted
33411cb0ef41Sopenharmony_ci  losslessly.
33421cb0ef41Sopenharmony_ci
33431cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded. If a non-`BigInt` is passed in it
33441cb0ef41Sopenharmony_cireturns `napi_bigint_expected`.
33451cb0ef41Sopenharmony_ci
33461cb0ef41Sopenharmony_ciThis API returns the C `int64_t` primitive equivalent of the given JavaScript
33471cb0ef41Sopenharmony_ci`BigInt`. If needed it will truncate the value, setting `lossless` to `false`.
33481cb0ef41Sopenharmony_ci
33491cb0ef41Sopenharmony_ci#### `napi_get_value_bigint_uint64`
33501cb0ef41Sopenharmony_ci
33511cb0ef41Sopenharmony_ci<!-- YAML
33521cb0ef41Sopenharmony_ciadded: v10.7.0
33531cb0ef41Sopenharmony_cinapiVersion: 6
33541cb0ef41Sopenharmony_ci-->
33551cb0ef41Sopenharmony_ci
33561cb0ef41Sopenharmony_ci```c
33571cb0ef41Sopenharmony_cinapi_status napi_get_value_bigint_uint64(napi_env env,
33581cb0ef41Sopenharmony_ci                                        napi_value value,
33591cb0ef41Sopenharmony_ci                                        uint64_t* result,
33601cb0ef41Sopenharmony_ci                                        bool* lossless);
33611cb0ef41Sopenharmony_ci```
33621cb0ef41Sopenharmony_ci
33631cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
33641cb0ef41Sopenharmony_ci* `[in] value`: `napi_value` representing JavaScript `BigInt`.
33651cb0ef41Sopenharmony_ci* `[out] result`: C `uint64_t` primitive equivalent of the given JavaScript
33661cb0ef41Sopenharmony_ci  `BigInt`.
33671cb0ef41Sopenharmony_ci* `[out] lossless`: Indicates whether the `BigInt` value was converted
33681cb0ef41Sopenharmony_ci  losslessly.
33691cb0ef41Sopenharmony_ci
33701cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded. If a non-`BigInt` is passed in it
33711cb0ef41Sopenharmony_cireturns `napi_bigint_expected`.
33721cb0ef41Sopenharmony_ci
33731cb0ef41Sopenharmony_ciThis API returns the C `uint64_t` primitive equivalent of the given JavaScript
33741cb0ef41Sopenharmony_ci`BigInt`. If needed it will truncate the value, setting `lossless` to `false`.
33751cb0ef41Sopenharmony_ci
33761cb0ef41Sopenharmony_ci#### `napi_get_value_bigint_words`
33771cb0ef41Sopenharmony_ci
33781cb0ef41Sopenharmony_ci<!-- YAML
33791cb0ef41Sopenharmony_ciadded: v10.7.0
33801cb0ef41Sopenharmony_cinapiVersion: 6
33811cb0ef41Sopenharmony_ci-->
33821cb0ef41Sopenharmony_ci
33831cb0ef41Sopenharmony_ci```c
33841cb0ef41Sopenharmony_cinapi_status napi_get_value_bigint_words(napi_env env,
33851cb0ef41Sopenharmony_ci                                        napi_value value,
33861cb0ef41Sopenharmony_ci                                        int* sign_bit,
33871cb0ef41Sopenharmony_ci                                        size_t* word_count,
33881cb0ef41Sopenharmony_ci                                        uint64_t* words);
33891cb0ef41Sopenharmony_ci```
33901cb0ef41Sopenharmony_ci
33911cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
33921cb0ef41Sopenharmony_ci* `[in] value`: `napi_value` representing JavaScript `BigInt`.
33931cb0ef41Sopenharmony_ci* `[out] sign_bit`: Integer representing if the JavaScript `BigInt` is positive
33941cb0ef41Sopenharmony_ci  or negative.
33951cb0ef41Sopenharmony_ci* `[in/out] word_count`: Must be initialized to the length of the `words`
33961cb0ef41Sopenharmony_ci  array. Upon return, it will be set to the actual number of words that
33971cb0ef41Sopenharmony_ci  would be needed to store this `BigInt`.
33981cb0ef41Sopenharmony_ci* `[out] words`: Pointer to a pre-allocated 64-bit word array.
33991cb0ef41Sopenharmony_ci
34001cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
34011cb0ef41Sopenharmony_ci
34021cb0ef41Sopenharmony_ciThis API converts a single `BigInt` value into a sign bit, 64-bit little-endian
34031cb0ef41Sopenharmony_ciarray, and the number of elements in the array. `sign_bit` and `words` may be
34041cb0ef41Sopenharmony_ciboth set to `NULL`, in order to get only `word_count`.
34051cb0ef41Sopenharmony_ci
34061cb0ef41Sopenharmony_ci#### `napi_get_value_external`
34071cb0ef41Sopenharmony_ci
34081cb0ef41Sopenharmony_ci<!-- YAML
34091cb0ef41Sopenharmony_ciadded: v8.0.0
34101cb0ef41Sopenharmony_cinapiVersion: 1
34111cb0ef41Sopenharmony_ci-->
34121cb0ef41Sopenharmony_ci
34131cb0ef41Sopenharmony_ci```c
34141cb0ef41Sopenharmony_cinapi_status napi_get_value_external(napi_env env,
34151cb0ef41Sopenharmony_ci                                    napi_value value,
34161cb0ef41Sopenharmony_ci                                    void** result)
34171cb0ef41Sopenharmony_ci```
34181cb0ef41Sopenharmony_ci
34191cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
34201cb0ef41Sopenharmony_ci* `[in] value`: `napi_value` representing JavaScript external value.
34211cb0ef41Sopenharmony_ci* `[out] result`: Pointer to the data wrapped by the JavaScript external value.
34221cb0ef41Sopenharmony_ci
34231cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded. If a non-external `napi_value` is
34241cb0ef41Sopenharmony_cipassed in it returns `napi_invalid_arg`.
34251cb0ef41Sopenharmony_ci
34261cb0ef41Sopenharmony_ciThis API retrieves the external data pointer that was previously passed to
34271cb0ef41Sopenharmony_ci`napi_create_external()`.
34281cb0ef41Sopenharmony_ci
34291cb0ef41Sopenharmony_ci#### `napi_get_value_int32`
34301cb0ef41Sopenharmony_ci
34311cb0ef41Sopenharmony_ci<!-- YAML
34321cb0ef41Sopenharmony_ciadded: v8.0.0
34331cb0ef41Sopenharmony_cinapiVersion: 1
34341cb0ef41Sopenharmony_ci-->
34351cb0ef41Sopenharmony_ci
34361cb0ef41Sopenharmony_ci```c
34371cb0ef41Sopenharmony_cinapi_status napi_get_value_int32(napi_env env,
34381cb0ef41Sopenharmony_ci                                 napi_value value,
34391cb0ef41Sopenharmony_ci                                 int32_t* result)
34401cb0ef41Sopenharmony_ci```
34411cb0ef41Sopenharmony_ci
34421cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
34431cb0ef41Sopenharmony_ci* `[in] value`: `napi_value` representing JavaScript `number`.
34441cb0ef41Sopenharmony_ci* `[out] result`: C `int32` primitive equivalent of the given JavaScript
34451cb0ef41Sopenharmony_ci  `number`.
34461cb0ef41Sopenharmony_ci
34471cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded. If a non-number `napi_value`
34481cb0ef41Sopenharmony_ciis passed in `napi_number_expected`.
34491cb0ef41Sopenharmony_ci
34501cb0ef41Sopenharmony_ciThis API returns the C `int32` primitive equivalent
34511cb0ef41Sopenharmony_ciof the given JavaScript `number`.
34521cb0ef41Sopenharmony_ci
34531cb0ef41Sopenharmony_ciIf the number exceeds the range of the 32 bit integer, then the result is
34541cb0ef41Sopenharmony_citruncated to the equivalent of the bottom 32 bits. This can result in a large
34551cb0ef41Sopenharmony_cipositive number becoming a negative number if the value is > 2<sup>31</sup> - 1.
34561cb0ef41Sopenharmony_ci
34571cb0ef41Sopenharmony_ciNon-finite number values (`NaN`, `+Infinity`, or `-Infinity`) set the
34581cb0ef41Sopenharmony_ciresult to zero.
34591cb0ef41Sopenharmony_ci
34601cb0ef41Sopenharmony_ci#### `napi_get_value_int64`
34611cb0ef41Sopenharmony_ci
34621cb0ef41Sopenharmony_ci<!-- YAML
34631cb0ef41Sopenharmony_ciadded: v8.0.0
34641cb0ef41Sopenharmony_cinapiVersion: 1
34651cb0ef41Sopenharmony_ci-->
34661cb0ef41Sopenharmony_ci
34671cb0ef41Sopenharmony_ci```c
34681cb0ef41Sopenharmony_cinapi_status napi_get_value_int64(napi_env env,
34691cb0ef41Sopenharmony_ci                                 napi_value value,
34701cb0ef41Sopenharmony_ci                                 int64_t* result)
34711cb0ef41Sopenharmony_ci```
34721cb0ef41Sopenharmony_ci
34731cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
34741cb0ef41Sopenharmony_ci* `[in] value`: `napi_value` representing JavaScript `number`.
34751cb0ef41Sopenharmony_ci* `[out] result`: C `int64` primitive equivalent of the given JavaScript
34761cb0ef41Sopenharmony_ci  `number`.
34771cb0ef41Sopenharmony_ci
34781cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded. If a non-number `napi_value`
34791cb0ef41Sopenharmony_ciis passed in it returns `napi_number_expected`.
34801cb0ef41Sopenharmony_ci
34811cb0ef41Sopenharmony_ciThis API returns the C `int64` primitive equivalent of the given JavaScript
34821cb0ef41Sopenharmony_ci`number`.
34831cb0ef41Sopenharmony_ci
34841cb0ef41Sopenharmony_ci`number` values outside the range of [`Number.MIN_SAFE_INTEGER`][]
34851cb0ef41Sopenharmony_ci`-(2**53 - 1)` - [`Number.MAX_SAFE_INTEGER`][] `(2**53 - 1)` will lose
34861cb0ef41Sopenharmony_ciprecision.
34871cb0ef41Sopenharmony_ci
34881cb0ef41Sopenharmony_ciNon-finite number values (`NaN`, `+Infinity`, or `-Infinity`) set the
34891cb0ef41Sopenharmony_ciresult to zero.
34901cb0ef41Sopenharmony_ci
34911cb0ef41Sopenharmony_ci#### `napi_get_value_string_latin1`
34921cb0ef41Sopenharmony_ci
34931cb0ef41Sopenharmony_ci<!-- YAML
34941cb0ef41Sopenharmony_ciadded: v8.0.0
34951cb0ef41Sopenharmony_cinapiVersion: 1
34961cb0ef41Sopenharmony_ci-->
34971cb0ef41Sopenharmony_ci
34981cb0ef41Sopenharmony_ci```c
34991cb0ef41Sopenharmony_cinapi_status napi_get_value_string_latin1(napi_env env,
35001cb0ef41Sopenharmony_ci                                         napi_value value,
35011cb0ef41Sopenharmony_ci                                         char* buf,
35021cb0ef41Sopenharmony_ci                                         size_t bufsize,
35031cb0ef41Sopenharmony_ci                                         size_t* result)
35041cb0ef41Sopenharmony_ci```
35051cb0ef41Sopenharmony_ci
35061cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
35071cb0ef41Sopenharmony_ci* `[in] value`: `napi_value` representing JavaScript string.
35081cb0ef41Sopenharmony_ci* `[in] buf`: Buffer to write the ISO-8859-1-encoded string into. If `NULL` is
35091cb0ef41Sopenharmony_ci  passed in, the length of the string in bytes and excluding the null terminator
35101cb0ef41Sopenharmony_ci  is returned in `result`.
35111cb0ef41Sopenharmony_ci* `[in] bufsize`: Size of the destination buffer. When this value is
35121cb0ef41Sopenharmony_ci  insufficient, the returned string is truncated and null-terminated.
35131cb0ef41Sopenharmony_ci* `[out] result`: Number of bytes copied into the buffer, excluding the null
35141cb0ef41Sopenharmony_ci  terminator.
35151cb0ef41Sopenharmony_ci
35161cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded. If a non-`string` `napi_value`
35171cb0ef41Sopenharmony_ciis passed in it returns `napi_string_expected`.
35181cb0ef41Sopenharmony_ci
35191cb0ef41Sopenharmony_ciThis API returns the ISO-8859-1-encoded string corresponding the value passed
35201cb0ef41Sopenharmony_ciin.
35211cb0ef41Sopenharmony_ci
35221cb0ef41Sopenharmony_ci#### `napi_get_value_string_utf8`
35231cb0ef41Sopenharmony_ci
35241cb0ef41Sopenharmony_ci<!-- YAML
35251cb0ef41Sopenharmony_ciadded: v8.0.0
35261cb0ef41Sopenharmony_cinapiVersion: 1
35271cb0ef41Sopenharmony_ci-->
35281cb0ef41Sopenharmony_ci
35291cb0ef41Sopenharmony_ci```c
35301cb0ef41Sopenharmony_cinapi_status napi_get_value_string_utf8(napi_env env,
35311cb0ef41Sopenharmony_ci                                       napi_value value,
35321cb0ef41Sopenharmony_ci                                       char* buf,
35331cb0ef41Sopenharmony_ci                                       size_t bufsize,
35341cb0ef41Sopenharmony_ci                                       size_t* result)
35351cb0ef41Sopenharmony_ci```
35361cb0ef41Sopenharmony_ci
35371cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
35381cb0ef41Sopenharmony_ci* `[in] value`: `napi_value` representing JavaScript string.
35391cb0ef41Sopenharmony_ci* `[in] buf`: Buffer to write the UTF8-encoded string into. If `NULL` is passed
35401cb0ef41Sopenharmony_ci  in, the length of the string in bytes and excluding the null terminator is
35411cb0ef41Sopenharmony_ci  returned in `result`.
35421cb0ef41Sopenharmony_ci* `[in] bufsize`: Size of the destination buffer. When this value is
35431cb0ef41Sopenharmony_ci  insufficient, the returned string is truncated and null-terminated.
35441cb0ef41Sopenharmony_ci* `[out] result`: Number of bytes copied into the buffer, excluding the null
35451cb0ef41Sopenharmony_ci  terminator.
35461cb0ef41Sopenharmony_ci
35471cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded. If a non-`string` `napi_value`
35481cb0ef41Sopenharmony_ciis passed in it returns `napi_string_expected`.
35491cb0ef41Sopenharmony_ci
35501cb0ef41Sopenharmony_ciThis API returns the UTF8-encoded string corresponding the value passed in.
35511cb0ef41Sopenharmony_ci
35521cb0ef41Sopenharmony_ci#### `napi_get_value_string_utf16`
35531cb0ef41Sopenharmony_ci
35541cb0ef41Sopenharmony_ci<!-- YAML
35551cb0ef41Sopenharmony_ciadded: v8.0.0
35561cb0ef41Sopenharmony_cinapiVersion: 1
35571cb0ef41Sopenharmony_ci-->
35581cb0ef41Sopenharmony_ci
35591cb0ef41Sopenharmony_ci```c
35601cb0ef41Sopenharmony_cinapi_status napi_get_value_string_utf16(napi_env env,
35611cb0ef41Sopenharmony_ci                                        napi_value value,
35621cb0ef41Sopenharmony_ci                                        char16_t* buf,
35631cb0ef41Sopenharmony_ci                                        size_t bufsize,
35641cb0ef41Sopenharmony_ci                                        size_t* result)
35651cb0ef41Sopenharmony_ci```
35661cb0ef41Sopenharmony_ci
35671cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
35681cb0ef41Sopenharmony_ci* `[in] value`: `napi_value` representing JavaScript string.
35691cb0ef41Sopenharmony_ci* `[in] buf`: Buffer to write the UTF16-LE-encoded string into. If `NULL` is
35701cb0ef41Sopenharmony_ci  passed in, the length of the string in 2-byte code units and excluding the
35711cb0ef41Sopenharmony_ci  null terminator is returned.
35721cb0ef41Sopenharmony_ci* `[in] bufsize`: Size of the destination buffer. When this value is
35731cb0ef41Sopenharmony_ci  insufficient, the returned string is truncated and null-terminated.
35741cb0ef41Sopenharmony_ci* `[out] result`: Number of 2-byte code units copied into the buffer, excluding
35751cb0ef41Sopenharmony_ci  the null terminator.
35761cb0ef41Sopenharmony_ci
35771cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded. If a non-`string` `napi_value`
35781cb0ef41Sopenharmony_ciis passed in it returns `napi_string_expected`.
35791cb0ef41Sopenharmony_ci
35801cb0ef41Sopenharmony_ciThis API returns the UTF16-encoded string corresponding the value passed in.
35811cb0ef41Sopenharmony_ci
35821cb0ef41Sopenharmony_ci#### `napi_get_value_uint32`
35831cb0ef41Sopenharmony_ci
35841cb0ef41Sopenharmony_ci<!-- YAML
35851cb0ef41Sopenharmony_ciadded: v8.0.0
35861cb0ef41Sopenharmony_cinapiVersion: 1
35871cb0ef41Sopenharmony_ci-->
35881cb0ef41Sopenharmony_ci
35891cb0ef41Sopenharmony_ci```c
35901cb0ef41Sopenharmony_cinapi_status napi_get_value_uint32(napi_env env,
35911cb0ef41Sopenharmony_ci                                  napi_value value,
35921cb0ef41Sopenharmony_ci                                  uint32_t* result)
35931cb0ef41Sopenharmony_ci```
35941cb0ef41Sopenharmony_ci
35951cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
35961cb0ef41Sopenharmony_ci* `[in] value`: `napi_value` representing JavaScript `number`.
35971cb0ef41Sopenharmony_ci* `[out] result`: C primitive equivalent of the given `napi_value` as a
35981cb0ef41Sopenharmony_ci  `uint32_t`.
35991cb0ef41Sopenharmony_ci
36001cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded. If a non-number `napi_value`
36011cb0ef41Sopenharmony_ciis passed in it returns `napi_number_expected`.
36021cb0ef41Sopenharmony_ci
36031cb0ef41Sopenharmony_ciThis API returns the C primitive equivalent of the given `napi_value` as a
36041cb0ef41Sopenharmony_ci`uint32_t`.
36051cb0ef41Sopenharmony_ci
36061cb0ef41Sopenharmony_ci### Functions to get global instances
36071cb0ef41Sopenharmony_ci
36081cb0ef41Sopenharmony_ci#### `napi_get_boolean`
36091cb0ef41Sopenharmony_ci
36101cb0ef41Sopenharmony_ci<!-- YAML
36111cb0ef41Sopenharmony_ciadded: v8.0.0
36121cb0ef41Sopenharmony_cinapiVersion: 1
36131cb0ef41Sopenharmony_ci-->
36141cb0ef41Sopenharmony_ci
36151cb0ef41Sopenharmony_ci```c
36161cb0ef41Sopenharmony_cinapi_status napi_get_boolean(napi_env env, bool value, napi_value* result)
36171cb0ef41Sopenharmony_ci```
36181cb0ef41Sopenharmony_ci
36191cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
36201cb0ef41Sopenharmony_ci* `[in] value`: The value of the boolean to retrieve.
36211cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing JavaScript `Boolean` singleton to
36221cb0ef41Sopenharmony_ci  retrieve.
36231cb0ef41Sopenharmony_ci
36241cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
36251cb0ef41Sopenharmony_ci
36261cb0ef41Sopenharmony_ciThis API is used to return the JavaScript singleton object that is used to
36271cb0ef41Sopenharmony_cirepresent the given boolean value.
36281cb0ef41Sopenharmony_ci
36291cb0ef41Sopenharmony_ci#### `napi_get_global`
36301cb0ef41Sopenharmony_ci
36311cb0ef41Sopenharmony_ci<!-- YAML
36321cb0ef41Sopenharmony_ciadded: v8.0.0
36331cb0ef41Sopenharmony_cinapiVersion: 1
36341cb0ef41Sopenharmony_ci-->
36351cb0ef41Sopenharmony_ci
36361cb0ef41Sopenharmony_ci```c
36371cb0ef41Sopenharmony_cinapi_status napi_get_global(napi_env env, napi_value* result)
36381cb0ef41Sopenharmony_ci```
36391cb0ef41Sopenharmony_ci
36401cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
36411cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing JavaScript `global` object.
36421cb0ef41Sopenharmony_ci
36431cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
36441cb0ef41Sopenharmony_ci
36451cb0ef41Sopenharmony_ciThis API returns the `global` object.
36461cb0ef41Sopenharmony_ci
36471cb0ef41Sopenharmony_ci#### `napi_get_null`
36481cb0ef41Sopenharmony_ci
36491cb0ef41Sopenharmony_ci<!-- YAML
36501cb0ef41Sopenharmony_ciadded: v8.0.0
36511cb0ef41Sopenharmony_cinapiVersion: 1
36521cb0ef41Sopenharmony_ci-->
36531cb0ef41Sopenharmony_ci
36541cb0ef41Sopenharmony_ci```c
36551cb0ef41Sopenharmony_cinapi_status napi_get_null(napi_env env, napi_value* result)
36561cb0ef41Sopenharmony_ci```
36571cb0ef41Sopenharmony_ci
36581cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
36591cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing JavaScript `null` object.
36601cb0ef41Sopenharmony_ci
36611cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
36621cb0ef41Sopenharmony_ci
36631cb0ef41Sopenharmony_ciThis API returns the `null` object.
36641cb0ef41Sopenharmony_ci
36651cb0ef41Sopenharmony_ci#### `napi_get_undefined`
36661cb0ef41Sopenharmony_ci
36671cb0ef41Sopenharmony_ci<!-- YAML
36681cb0ef41Sopenharmony_ciadded: v8.0.0
36691cb0ef41Sopenharmony_cinapiVersion: 1
36701cb0ef41Sopenharmony_ci-->
36711cb0ef41Sopenharmony_ci
36721cb0ef41Sopenharmony_ci```c
36731cb0ef41Sopenharmony_cinapi_status napi_get_undefined(napi_env env, napi_value* result)
36741cb0ef41Sopenharmony_ci```
36751cb0ef41Sopenharmony_ci
36761cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
36771cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing JavaScript Undefined value.
36781cb0ef41Sopenharmony_ci
36791cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
36801cb0ef41Sopenharmony_ci
36811cb0ef41Sopenharmony_ciThis API returns the Undefined object.
36821cb0ef41Sopenharmony_ci
36831cb0ef41Sopenharmony_ci## Working with JavaScript values and abstract operations
36841cb0ef41Sopenharmony_ci
36851cb0ef41Sopenharmony_ciNode-API exposes a set of APIs to perform some abstract operations on JavaScript
36861cb0ef41Sopenharmony_civalues. Some of these operations are documented under [Section 7][]
36871cb0ef41Sopenharmony_ciof the [ECMAScript Language Specification][].
36881cb0ef41Sopenharmony_ci
36891cb0ef41Sopenharmony_ciThese APIs support doing one of the following:
36901cb0ef41Sopenharmony_ci
36911cb0ef41Sopenharmony_ci1. Coerce JavaScript values to specific JavaScript types (such as `number` or
36921cb0ef41Sopenharmony_ci   `string`).
36931cb0ef41Sopenharmony_ci2. Check the type of a JavaScript value.
36941cb0ef41Sopenharmony_ci3. Check for equality between two JavaScript values.
36951cb0ef41Sopenharmony_ci
36961cb0ef41Sopenharmony_ci### `napi_coerce_to_bool`
36971cb0ef41Sopenharmony_ci
36981cb0ef41Sopenharmony_ci<!-- YAML
36991cb0ef41Sopenharmony_ciadded: v8.0.0
37001cb0ef41Sopenharmony_cinapiVersion: 1
37011cb0ef41Sopenharmony_ci-->
37021cb0ef41Sopenharmony_ci
37031cb0ef41Sopenharmony_ci```c
37041cb0ef41Sopenharmony_cinapi_status napi_coerce_to_bool(napi_env env,
37051cb0ef41Sopenharmony_ci                                napi_value value,
37061cb0ef41Sopenharmony_ci                                napi_value* result)
37071cb0ef41Sopenharmony_ci```
37081cb0ef41Sopenharmony_ci
37091cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
37101cb0ef41Sopenharmony_ci* `[in] value`: The JavaScript value to coerce.
37111cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing the coerced JavaScript `Boolean`.
37121cb0ef41Sopenharmony_ci
37131cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
37141cb0ef41Sopenharmony_ci
37151cb0ef41Sopenharmony_ciThis API implements the abstract operation `ToBoolean()` as defined in
37161cb0ef41Sopenharmony_ci[Section 7.1.2][] of the ECMAScript Language Specification.
37171cb0ef41Sopenharmony_ci
37181cb0ef41Sopenharmony_ci### `napi_coerce_to_number`
37191cb0ef41Sopenharmony_ci
37201cb0ef41Sopenharmony_ci<!-- YAML
37211cb0ef41Sopenharmony_ciadded: v8.0.0
37221cb0ef41Sopenharmony_cinapiVersion: 1
37231cb0ef41Sopenharmony_ci-->
37241cb0ef41Sopenharmony_ci
37251cb0ef41Sopenharmony_ci```c
37261cb0ef41Sopenharmony_cinapi_status napi_coerce_to_number(napi_env env,
37271cb0ef41Sopenharmony_ci                                  napi_value value,
37281cb0ef41Sopenharmony_ci                                  napi_value* result)
37291cb0ef41Sopenharmony_ci```
37301cb0ef41Sopenharmony_ci
37311cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
37321cb0ef41Sopenharmony_ci* `[in] value`: The JavaScript value to coerce.
37331cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing the coerced JavaScript `number`.
37341cb0ef41Sopenharmony_ci
37351cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
37361cb0ef41Sopenharmony_ci
37371cb0ef41Sopenharmony_ciThis API implements the abstract operation `ToNumber()` as defined in
37381cb0ef41Sopenharmony_ci[Section 7.1.3][] of the ECMAScript Language Specification.
37391cb0ef41Sopenharmony_ciThis function potentially runs JS code if the passed-in value is an
37401cb0ef41Sopenharmony_ciobject.
37411cb0ef41Sopenharmony_ci
37421cb0ef41Sopenharmony_ci### `napi_coerce_to_object`
37431cb0ef41Sopenharmony_ci
37441cb0ef41Sopenharmony_ci<!-- YAML
37451cb0ef41Sopenharmony_ciadded: v8.0.0
37461cb0ef41Sopenharmony_cinapiVersion: 1
37471cb0ef41Sopenharmony_ci-->
37481cb0ef41Sopenharmony_ci
37491cb0ef41Sopenharmony_ci```c
37501cb0ef41Sopenharmony_cinapi_status napi_coerce_to_object(napi_env env,
37511cb0ef41Sopenharmony_ci                                  napi_value value,
37521cb0ef41Sopenharmony_ci                                  napi_value* result)
37531cb0ef41Sopenharmony_ci```
37541cb0ef41Sopenharmony_ci
37551cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
37561cb0ef41Sopenharmony_ci* `[in] value`: The JavaScript value to coerce.
37571cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing the coerced JavaScript `Object`.
37581cb0ef41Sopenharmony_ci
37591cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
37601cb0ef41Sopenharmony_ci
37611cb0ef41Sopenharmony_ciThis API implements the abstract operation `ToObject()` as defined in
37621cb0ef41Sopenharmony_ci[Section 7.1.13][] of the ECMAScript Language Specification.
37631cb0ef41Sopenharmony_ci
37641cb0ef41Sopenharmony_ci### `napi_coerce_to_string`
37651cb0ef41Sopenharmony_ci
37661cb0ef41Sopenharmony_ci<!-- YAML
37671cb0ef41Sopenharmony_ciadded: v8.0.0
37681cb0ef41Sopenharmony_cinapiVersion: 1
37691cb0ef41Sopenharmony_ci-->
37701cb0ef41Sopenharmony_ci
37711cb0ef41Sopenharmony_ci```c
37721cb0ef41Sopenharmony_cinapi_status napi_coerce_to_string(napi_env env,
37731cb0ef41Sopenharmony_ci                                  napi_value value,
37741cb0ef41Sopenharmony_ci                                  napi_value* result)
37751cb0ef41Sopenharmony_ci```
37761cb0ef41Sopenharmony_ci
37771cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
37781cb0ef41Sopenharmony_ci* `[in] value`: The JavaScript value to coerce.
37791cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing the coerced JavaScript `string`.
37801cb0ef41Sopenharmony_ci
37811cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
37821cb0ef41Sopenharmony_ci
37831cb0ef41Sopenharmony_ciThis API implements the abstract operation `ToString()` as defined in
37841cb0ef41Sopenharmony_ci[Section 7.1.13][] of the ECMAScript Language Specification.
37851cb0ef41Sopenharmony_ciThis function potentially runs JS code if the passed-in value is an
37861cb0ef41Sopenharmony_ciobject.
37871cb0ef41Sopenharmony_ci
37881cb0ef41Sopenharmony_ci### `napi_typeof`
37891cb0ef41Sopenharmony_ci
37901cb0ef41Sopenharmony_ci<!-- YAML
37911cb0ef41Sopenharmony_ciadded: v8.0.0
37921cb0ef41Sopenharmony_cinapiVersion: 1
37931cb0ef41Sopenharmony_ci-->
37941cb0ef41Sopenharmony_ci
37951cb0ef41Sopenharmony_ci```c
37961cb0ef41Sopenharmony_cinapi_status napi_typeof(napi_env env, napi_value value, napi_valuetype* result)
37971cb0ef41Sopenharmony_ci```
37981cb0ef41Sopenharmony_ci
37991cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
38001cb0ef41Sopenharmony_ci* `[in] value`: The JavaScript value whose type to query.
38011cb0ef41Sopenharmony_ci* `[out] result`: The type of the JavaScript value.
38021cb0ef41Sopenharmony_ci
38031cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
38041cb0ef41Sopenharmony_ci
38051cb0ef41Sopenharmony_ci* `napi_invalid_arg` if the type of `value` is not a known ECMAScript type and
38061cb0ef41Sopenharmony_ci  `value` is not an External value.
38071cb0ef41Sopenharmony_ci
38081cb0ef41Sopenharmony_ciThis API represents behavior similar to invoking the `typeof` Operator on
38091cb0ef41Sopenharmony_cithe object as defined in [Section 12.5.5][] of the ECMAScript Language
38101cb0ef41Sopenharmony_ciSpecification. However, there are some differences:
38111cb0ef41Sopenharmony_ci
38121cb0ef41Sopenharmony_ci1. It has support for detecting an External value.
38131cb0ef41Sopenharmony_ci2. It detects `null` as a separate type, while ECMAScript `typeof` would detect
38141cb0ef41Sopenharmony_ci   `object`.
38151cb0ef41Sopenharmony_ci
38161cb0ef41Sopenharmony_ciIf `value` has a type that is invalid, an error is returned.
38171cb0ef41Sopenharmony_ci
38181cb0ef41Sopenharmony_ci### `napi_instanceof`
38191cb0ef41Sopenharmony_ci
38201cb0ef41Sopenharmony_ci<!-- YAML
38211cb0ef41Sopenharmony_ciadded: v8.0.0
38221cb0ef41Sopenharmony_cinapiVersion: 1
38231cb0ef41Sopenharmony_ci-->
38241cb0ef41Sopenharmony_ci
38251cb0ef41Sopenharmony_ci```c
38261cb0ef41Sopenharmony_cinapi_status napi_instanceof(napi_env env,
38271cb0ef41Sopenharmony_ci                            napi_value object,
38281cb0ef41Sopenharmony_ci                            napi_value constructor,
38291cb0ef41Sopenharmony_ci                            bool* result)
38301cb0ef41Sopenharmony_ci```
38311cb0ef41Sopenharmony_ci
38321cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
38331cb0ef41Sopenharmony_ci* `[in] object`: The JavaScript value to check.
38341cb0ef41Sopenharmony_ci* `[in] constructor`: The JavaScript function object of the constructor function
38351cb0ef41Sopenharmony_ci  to check against.
38361cb0ef41Sopenharmony_ci* `[out] result`: Boolean that is set to true if `object instanceof constructor`
38371cb0ef41Sopenharmony_ci  is true.
38381cb0ef41Sopenharmony_ci
38391cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
38401cb0ef41Sopenharmony_ci
38411cb0ef41Sopenharmony_ciThis API represents invoking the `instanceof` Operator on the object as
38421cb0ef41Sopenharmony_cidefined in [Section 12.10.4][] of the ECMAScript Language Specification.
38431cb0ef41Sopenharmony_ci
38441cb0ef41Sopenharmony_ci### `napi_is_array`
38451cb0ef41Sopenharmony_ci
38461cb0ef41Sopenharmony_ci<!-- YAML
38471cb0ef41Sopenharmony_ciadded: v8.0.0
38481cb0ef41Sopenharmony_cinapiVersion: 1
38491cb0ef41Sopenharmony_ci-->
38501cb0ef41Sopenharmony_ci
38511cb0ef41Sopenharmony_ci```c
38521cb0ef41Sopenharmony_cinapi_status napi_is_array(napi_env env, napi_value value, bool* result)
38531cb0ef41Sopenharmony_ci```
38541cb0ef41Sopenharmony_ci
38551cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
38561cb0ef41Sopenharmony_ci* `[in] value`: The JavaScript value to check.
38571cb0ef41Sopenharmony_ci* `[out] result`: Whether the given object is an array.
38581cb0ef41Sopenharmony_ci
38591cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
38601cb0ef41Sopenharmony_ci
38611cb0ef41Sopenharmony_ciThis API represents invoking the `IsArray` operation on the object
38621cb0ef41Sopenharmony_cias defined in [Section 7.2.2][] of the ECMAScript Language Specification.
38631cb0ef41Sopenharmony_ci
38641cb0ef41Sopenharmony_ci### `napi_is_arraybuffer`
38651cb0ef41Sopenharmony_ci
38661cb0ef41Sopenharmony_ci<!-- YAML
38671cb0ef41Sopenharmony_ciadded: v8.0.0
38681cb0ef41Sopenharmony_cinapiVersion: 1
38691cb0ef41Sopenharmony_ci-->
38701cb0ef41Sopenharmony_ci
38711cb0ef41Sopenharmony_ci```c
38721cb0ef41Sopenharmony_cinapi_status napi_is_arraybuffer(napi_env env, napi_value value, bool* result)
38731cb0ef41Sopenharmony_ci```
38741cb0ef41Sopenharmony_ci
38751cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
38761cb0ef41Sopenharmony_ci* `[in] value`: The JavaScript value to check.
38771cb0ef41Sopenharmony_ci* `[out] result`: Whether the given object is an `ArrayBuffer`.
38781cb0ef41Sopenharmony_ci
38791cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
38801cb0ef41Sopenharmony_ci
38811cb0ef41Sopenharmony_ciThis API checks if the `Object` passed in is an array buffer.
38821cb0ef41Sopenharmony_ci
38831cb0ef41Sopenharmony_ci### `napi_is_buffer`
38841cb0ef41Sopenharmony_ci
38851cb0ef41Sopenharmony_ci<!-- YAML
38861cb0ef41Sopenharmony_ciadded: v8.0.0
38871cb0ef41Sopenharmony_cinapiVersion: 1
38881cb0ef41Sopenharmony_ci-->
38891cb0ef41Sopenharmony_ci
38901cb0ef41Sopenharmony_ci```c
38911cb0ef41Sopenharmony_cinapi_status napi_is_buffer(napi_env env, napi_value value, bool* result)
38921cb0ef41Sopenharmony_ci```
38931cb0ef41Sopenharmony_ci
38941cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
38951cb0ef41Sopenharmony_ci* `[in] value`: The JavaScript value to check.
38961cb0ef41Sopenharmony_ci* `[out] result`: Whether the given `napi_value` represents a `node::Buffer` or
38971cb0ef41Sopenharmony_ci  `Uint8Array` object.
38981cb0ef41Sopenharmony_ci
38991cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
39001cb0ef41Sopenharmony_ci
39011cb0ef41Sopenharmony_ciThis API checks if the `Object` passed in is a buffer or Uint8Array.
39021cb0ef41Sopenharmony_ci[`napi_is_typedarray`][] should be preferred if the caller needs to check if the
39031cb0ef41Sopenharmony_civalue is a Uint8Array.
39041cb0ef41Sopenharmony_ci
39051cb0ef41Sopenharmony_ci### `napi_is_date`
39061cb0ef41Sopenharmony_ci
39071cb0ef41Sopenharmony_ci<!-- YAML
39081cb0ef41Sopenharmony_ciadded:
39091cb0ef41Sopenharmony_ci - v11.11.0
39101cb0ef41Sopenharmony_ci - v10.17.0
39111cb0ef41Sopenharmony_cinapiVersion: 5
39121cb0ef41Sopenharmony_ci-->
39131cb0ef41Sopenharmony_ci
39141cb0ef41Sopenharmony_ci```c
39151cb0ef41Sopenharmony_cinapi_status napi_is_date(napi_env env, napi_value value, bool* result)
39161cb0ef41Sopenharmony_ci```
39171cb0ef41Sopenharmony_ci
39181cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
39191cb0ef41Sopenharmony_ci* `[in] value`: The JavaScript value to check.
39201cb0ef41Sopenharmony_ci* `[out] result`: Whether the given `napi_value` represents a JavaScript `Date`
39211cb0ef41Sopenharmony_ci  object.
39221cb0ef41Sopenharmony_ci
39231cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
39241cb0ef41Sopenharmony_ci
39251cb0ef41Sopenharmony_ciThis API checks if the `Object` passed in is a date.
39261cb0ef41Sopenharmony_ci
39271cb0ef41Sopenharmony_ci### `napi_is_error`
39281cb0ef41Sopenharmony_ci
39291cb0ef41Sopenharmony_ci<!-- YAML
39301cb0ef41Sopenharmony_ciadded: v8.0.0
39311cb0ef41Sopenharmony_cinapiVersion: 1
39321cb0ef41Sopenharmony_ci-->
39331cb0ef41Sopenharmony_ci
39341cb0ef41Sopenharmony_ci```c
39351cb0ef41Sopenharmony_cinapi_status napi_is_error(napi_env env, napi_value value, bool* result)
39361cb0ef41Sopenharmony_ci```
39371cb0ef41Sopenharmony_ci
39381cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
39391cb0ef41Sopenharmony_ci* `[in] value`: The JavaScript value to check.
39401cb0ef41Sopenharmony_ci* `[out] result`: Whether the given `napi_value` represents an `Error` object.
39411cb0ef41Sopenharmony_ci
39421cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
39431cb0ef41Sopenharmony_ci
39441cb0ef41Sopenharmony_ciThis API checks if the `Object` passed in is an `Error`.
39451cb0ef41Sopenharmony_ci
39461cb0ef41Sopenharmony_ci### `napi_is_typedarray`
39471cb0ef41Sopenharmony_ci
39481cb0ef41Sopenharmony_ci<!-- YAML
39491cb0ef41Sopenharmony_ciadded: v8.0.0
39501cb0ef41Sopenharmony_cinapiVersion: 1
39511cb0ef41Sopenharmony_ci-->
39521cb0ef41Sopenharmony_ci
39531cb0ef41Sopenharmony_ci```c
39541cb0ef41Sopenharmony_cinapi_status napi_is_typedarray(napi_env env, napi_value value, bool* result)
39551cb0ef41Sopenharmony_ci```
39561cb0ef41Sopenharmony_ci
39571cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
39581cb0ef41Sopenharmony_ci* `[in] value`: The JavaScript value to check.
39591cb0ef41Sopenharmony_ci* `[out] result`: Whether the given `napi_value` represents a `TypedArray`.
39601cb0ef41Sopenharmony_ci
39611cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
39621cb0ef41Sopenharmony_ci
39631cb0ef41Sopenharmony_ciThis API checks if the `Object` passed in is a typed array.
39641cb0ef41Sopenharmony_ci
39651cb0ef41Sopenharmony_ci### `napi_is_dataview`
39661cb0ef41Sopenharmony_ci
39671cb0ef41Sopenharmony_ci<!-- YAML
39681cb0ef41Sopenharmony_ciadded: v8.3.0
39691cb0ef41Sopenharmony_cinapiVersion: 1
39701cb0ef41Sopenharmony_ci-->
39711cb0ef41Sopenharmony_ci
39721cb0ef41Sopenharmony_ci```c
39731cb0ef41Sopenharmony_cinapi_status napi_is_dataview(napi_env env, napi_value value, bool* result)
39741cb0ef41Sopenharmony_ci```
39751cb0ef41Sopenharmony_ci
39761cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
39771cb0ef41Sopenharmony_ci* `[in] value`: The JavaScript value to check.
39781cb0ef41Sopenharmony_ci* `[out] result`: Whether the given `napi_value` represents a `DataView`.
39791cb0ef41Sopenharmony_ci
39801cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
39811cb0ef41Sopenharmony_ci
39821cb0ef41Sopenharmony_ciThis API checks if the `Object` passed in is a `DataView`.
39831cb0ef41Sopenharmony_ci
39841cb0ef41Sopenharmony_ci### `napi_strict_equals`
39851cb0ef41Sopenharmony_ci
39861cb0ef41Sopenharmony_ci<!-- YAML
39871cb0ef41Sopenharmony_ciadded: v8.0.0
39881cb0ef41Sopenharmony_cinapiVersion: 1
39891cb0ef41Sopenharmony_ci-->
39901cb0ef41Sopenharmony_ci
39911cb0ef41Sopenharmony_ci```c
39921cb0ef41Sopenharmony_cinapi_status napi_strict_equals(napi_env env,
39931cb0ef41Sopenharmony_ci                               napi_value lhs,
39941cb0ef41Sopenharmony_ci                               napi_value rhs,
39951cb0ef41Sopenharmony_ci                               bool* result)
39961cb0ef41Sopenharmony_ci```
39971cb0ef41Sopenharmony_ci
39981cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
39991cb0ef41Sopenharmony_ci* `[in] lhs`: The JavaScript value to check.
40001cb0ef41Sopenharmony_ci* `[in] rhs`: The JavaScript value to check against.
40011cb0ef41Sopenharmony_ci* `[out] result`: Whether the two `napi_value` objects are equal.
40021cb0ef41Sopenharmony_ci
40031cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
40041cb0ef41Sopenharmony_ci
40051cb0ef41Sopenharmony_ciThis API represents the invocation of the Strict Equality algorithm as
40061cb0ef41Sopenharmony_cidefined in [Section 7.2.14][] of the ECMAScript Language Specification.
40071cb0ef41Sopenharmony_ci
40081cb0ef41Sopenharmony_ci### `napi_detach_arraybuffer`
40091cb0ef41Sopenharmony_ci
40101cb0ef41Sopenharmony_ci<!-- YAML
40111cb0ef41Sopenharmony_ciadded:
40121cb0ef41Sopenharmony_ci - v13.0.0
40131cb0ef41Sopenharmony_ci - v12.16.0
40141cb0ef41Sopenharmony_ci - v10.22.0
40151cb0ef41Sopenharmony_cinapiVersion: 7
40161cb0ef41Sopenharmony_ci-->
40171cb0ef41Sopenharmony_ci
40181cb0ef41Sopenharmony_ci```c
40191cb0ef41Sopenharmony_cinapi_status napi_detach_arraybuffer(napi_env env,
40201cb0ef41Sopenharmony_ci                                    napi_value arraybuffer)
40211cb0ef41Sopenharmony_ci```
40221cb0ef41Sopenharmony_ci
40231cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
40241cb0ef41Sopenharmony_ci* `[in] arraybuffer`: The JavaScript `ArrayBuffer` to be detached.
40251cb0ef41Sopenharmony_ci
40261cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded. If a non-detachable `ArrayBuffer` is
40271cb0ef41Sopenharmony_cipassed in it returns `napi_detachable_arraybuffer_expected`.
40281cb0ef41Sopenharmony_ci
40291cb0ef41Sopenharmony_ciGenerally, an `ArrayBuffer` is non-detachable if it has been detached before.
40301cb0ef41Sopenharmony_ciThe engine may impose additional conditions on whether an `ArrayBuffer` is
40311cb0ef41Sopenharmony_cidetachable. For example, V8 requires that the `ArrayBuffer` be external,
40321cb0ef41Sopenharmony_cithat is, created with [`napi_create_external_arraybuffer`][].
40331cb0ef41Sopenharmony_ci
40341cb0ef41Sopenharmony_ciThis API represents the invocation of the `ArrayBuffer` detach operation as
40351cb0ef41Sopenharmony_cidefined in [Section 24.1.1.3][] of the ECMAScript Language Specification.
40361cb0ef41Sopenharmony_ci
40371cb0ef41Sopenharmony_ci### `napi_is_detached_arraybuffer`
40381cb0ef41Sopenharmony_ci
40391cb0ef41Sopenharmony_ci<!-- YAML
40401cb0ef41Sopenharmony_ciadded:
40411cb0ef41Sopenharmony_ci - v13.3.0
40421cb0ef41Sopenharmony_ci - v12.16.0
40431cb0ef41Sopenharmony_ci - v10.22.0
40441cb0ef41Sopenharmony_cinapiVersion: 7
40451cb0ef41Sopenharmony_ci-->
40461cb0ef41Sopenharmony_ci
40471cb0ef41Sopenharmony_ci```c
40481cb0ef41Sopenharmony_cinapi_status napi_is_detached_arraybuffer(napi_env env,
40491cb0ef41Sopenharmony_ci                                         napi_value arraybuffer,
40501cb0ef41Sopenharmony_ci                                         bool* result)
40511cb0ef41Sopenharmony_ci```
40521cb0ef41Sopenharmony_ci
40531cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
40541cb0ef41Sopenharmony_ci* `[in] arraybuffer`: The JavaScript `ArrayBuffer` to be checked.
40551cb0ef41Sopenharmony_ci* `[out] result`: Whether the `arraybuffer` is detached.
40561cb0ef41Sopenharmony_ci
40571cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
40581cb0ef41Sopenharmony_ci
40591cb0ef41Sopenharmony_ciThe `ArrayBuffer` is considered detached if its internal data is `null`.
40601cb0ef41Sopenharmony_ci
40611cb0ef41Sopenharmony_ciThis API represents the invocation of the `ArrayBuffer` `IsDetachedBuffer`
40621cb0ef41Sopenharmony_cioperation as defined in [Section 24.1.1.2][] of the ECMAScript Language
40631cb0ef41Sopenharmony_ciSpecification.
40641cb0ef41Sopenharmony_ci
40651cb0ef41Sopenharmony_ci## Working with JavaScript properties
40661cb0ef41Sopenharmony_ci
40671cb0ef41Sopenharmony_ciNode-API exposes a set of APIs to get and set properties on JavaScript
40681cb0ef41Sopenharmony_ciobjects. Some of these types are documented under [Section 7][] of the
40691cb0ef41Sopenharmony_ci[ECMAScript Language Specification][].
40701cb0ef41Sopenharmony_ci
40711cb0ef41Sopenharmony_ciProperties in JavaScript are represented as a tuple of a key and a value.
40721cb0ef41Sopenharmony_ciFundamentally, all property keys in Node-API can be represented in one of the
40731cb0ef41Sopenharmony_cifollowing forms:
40741cb0ef41Sopenharmony_ci
40751cb0ef41Sopenharmony_ci* Named: a simple UTF8-encoded string
40761cb0ef41Sopenharmony_ci* Integer-Indexed: an index value represented by `uint32_t`
40771cb0ef41Sopenharmony_ci* JavaScript value: these are represented in Node-API by `napi_value`. This can
40781cb0ef41Sopenharmony_ci  be a `napi_value` representing a `string`, `number`, or `symbol`.
40791cb0ef41Sopenharmony_ci
40801cb0ef41Sopenharmony_ciNode-API values are represented by the type `napi_value`.
40811cb0ef41Sopenharmony_ciAny Node-API call that requires a JavaScript value takes in a `napi_value`.
40821cb0ef41Sopenharmony_ciHowever, it's the caller's responsibility to make sure that the
40831cb0ef41Sopenharmony_ci`napi_value` in question is of the JavaScript type expected by the API.
40841cb0ef41Sopenharmony_ci
40851cb0ef41Sopenharmony_ciThe APIs documented in this section provide a simple interface to
40861cb0ef41Sopenharmony_ciget and set properties on arbitrary JavaScript objects represented by
40871cb0ef41Sopenharmony_ci`napi_value`.
40881cb0ef41Sopenharmony_ci
40891cb0ef41Sopenharmony_ciFor instance, consider the following JavaScript code snippet:
40901cb0ef41Sopenharmony_ci
40911cb0ef41Sopenharmony_ci```js
40921cb0ef41Sopenharmony_ciconst obj = {};
40931cb0ef41Sopenharmony_ciobj.myProp = 123;
40941cb0ef41Sopenharmony_ci```
40951cb0ef41Sopenharmony_ci
40961cb0ef41Sopenharmony_ciThe equivalent can be done using Node-API values with the following snippet:
40971cb0ef41Sopenharmony_ci
40981cb0ef41Sopenharmony_ci```c
40991cb0ef41Sopenharmony_cinapi_status status = napi_generic_failure;
41001cb0ef41Sopenharmony_ci
41011cb0ef41Sopenharmony_ci// const obj = {}
41021cb0ef41Sopenharmony_cinapi_value obj, value;
41031cb0ef41Sopenharmony_cistatus = napi_create_object(env, &obj);
41041cb0ef41Sopenharmony_ciif (status != napi_ok) return status;
41051cb0ef41Sopenharmony_ci
41061cb0ef41Sopenharmony_ci// Create a napi_value for 123
41071cb0ef41Sopenharmony_cistatus = napi_create_int32(env, 123, &value);
41081cb0ef41Sopenharmony_ciif (status != napi_ok) return status;
41091cb0ef41Sopenharmony_ci
41101cb0ef41Sopenharmony_ci// obj.myProp = 123
41111cb0ef41Sopenharmony_cistatus = napi_set_named_property(env, obj, "myProp", value);
41121cb0ef41Sopenharmony_ciif (status != napi_ok) return status;
41131cb0ef41Sopenharmony_ci```
41141cb0ef41Sopenharmony_ci
41151cb0ef41Sopenharmony_ciIndexed properties can be set in a similar manner. Consider the following
41161cb0ef41Sopenharmony_ciJavaScript snippet:
41171cb0ef41Sopenharmony_ci
41181cb0ef41Sopenharmony_ci```js
41191cb0ef41Sopenharmony_ciconst arr = [];
41201cb0ef41Sopenharmony_ciarr[123] = 'hello';
41211cb0ef41Sopenharmony_ci```
41221cb0ef41Sopenharmony_ci
41231cb0ef41Sopenharmony_ciThe equivalent can be done using Node-API values with the following snippet:
41241cb0ef41Sopenharmony_ci
41251cb0ef41Sopenharmony_ci```c
41261cb0ef41Sopenharmony_cinapi_status status = napi_generic_failure;
41271cb0ef41Sopenharmony_ci
41281cb0ef41Sopenharmony_ci// const arr = [];
41291cb0ef41Sopenharmony_cinapi_value arr, value;
41301cb0ef41Sopenharmony_cistatus = napi_create_array(env, &arr);
41311cb0ef41Sopenharmony_ciif (status != napi_ok) return status;
41321cb0ef41Sopenharmony_ci
41331cb0ef41Sopenharmony_ci// Create a napi_value for 'hello'
41341cb0ef41Sopenharmony_cistatus = napi_create_string_utf8(env, "hello", NAPI_AUTO_LENGTH, &value);
41351cb0ef41Sopenharmony_ciif (status != napi_ok) return status;
41361cb0ef41Sopenharmony_ci
41371cb0ef41Sopenharmony_ci// arr[123] = 'hello';
41381cb0ef41Sopenharmony_cistatus = napi_set_element(env, arr, 123, value);
41391cb0ef41Sopenharmony_ciif (status != napi_ok) return status;
41401cb0ef41Sopenharmony_ci```
41411cb0ef41Sopenharmony_ci
41421cb0ef41Sopenharmony_ciProperties can be retrieved using the APIs described in this section.
41431cb0ef41Sopenharmony_ciConsider the following JavaScript snippet:
41441cb0ef41Sopenharmony_ci
41451cb0ef41Sopenharmony_ci```js
41461cb0ef41Sopenharmony_ciconst arr = [];
41471cb0ef41Sopenharmony_ciconst value = arr[123];
41481cb0ef41Sopenharmony_ci```
41491cb0ef41Sopenharmony_ci
41501cb0ef41Sopenharmony_ciThe following is the approximate equivalent of the Node-API counterpart:
41511cb0ef41Sopenharmony_ci
41521cb0ef41Sopenharmony_ci```c
41531cb0ef41Sopenharmony_cinapi_status status = napi_generic_failure;
41541cb0ef41Sopenharmony_ci
41551cb0ef41Sopenharmony_ci// const arr = []
41561cb0ef41Sopenharmony_cinapi_value arr, value;
41571cb0ef41Sopenharmony_cistatus = napi_create_array(env, &arr);
41581cb0ef41Sopenharmony_ciif (status != napi_ok) return status;
41591cb0ef41Sopenharmony_ci
41601cb0ef41Sopenharmony_ci// const value = arr[123]
41611cb0ef41Sopenharmony_cistatus = napi_get_element(env, arr, 123, &value);
41621cb0ef41Sopenharmony_ciif (status != napi_ok) return status;
41631cb0ef41Sopenharmony_ci```
41641cb0ef41Sopenharmony_ci
41651cb0ef41Sopenharmony_ciFinally, multiple properties can also be defined on an object for performance
41661cb0ef41Sopenharmony_cireasons. Consider the following JavaScript:
41671cb0ef41Sopenharmony_ci
41681cb0ef41Sopenharmony_ci```js
41691cb0ef41Sopenharmony_ciconst obj = {};
41701cb0ef41Sopenharmony_ciObject.defineProperties(obj, {
41711cb0ef41Sopenharmony_ci  'foo': { value: 123, writable: true, configurable: true, enumerable: true },
41721cb0ef41Sopenharmony_ci  'bar': { value: 456, writable: true, configurable: true, enumerable: true },
41731cb0ef41Sopenharmony_ci});
41741cb0ef41Sopenharmony_ci```
41751cb0ef41Sopenharmony_ci
41761cb0ef41Sopenharmony_ciThe following is the approximate equivalent of the Node-API counterpart:
41771cb0ef41Sopenharmony_ci
41781cb0ef41Sopenharmony_ci```c
41791cb0ef41Sopenharmony_cinapi_status status = napi_status_generic_failure;
41801cb0ef41Sopenharmony_ci
41811cb0ef41Sopenharmony_ci// const obj = {};
41821cb0ef41Sopenharmony_cinapi_value obj;
41831cb0ef41Sopenharmony_cistatus = napi_create_object(env, &obj);
41841cb0ef41Sopenharmony_ciif (status != napi_ok) return status;
41851cb0ef41Sopenharmony_ci
41861cb0ef41Sopenharmony_ci// Create napi_values for 123 and 456
41871cb0ef41Sopenharmony_cinapi_value fooValue, barValue;
41881cb0ef41Sopenharmony_cistatus = napi_create_int32(env, 123, &fooValue);
41891cb0ef41Sopenharmony_ciif (status != napi_ok) return status;
41901cb0ef41Sopenharmony_cistatus = napi_create_int32(env, 456, &barValue);
41911cb0ef41Sopenharmony_ciif (status != napi_ok) return status;
41921cb0ef41Sopenharmony_ci
41931cb0ef41Sopenharmony_ci// Set the properties
41941cb0ef41Sopenharmony_cinapi_property_descriptor descriptors[] = {
41951cb0ef41Sopenharmony_ci  { "foo", NULL, NULL, NULL, NULL, fooValue, napi_writable | napi_configurable, NULL },
41961cb0ef41Sopenharmony_ci  { "bar", NULL, NULL, NULL, NULL, barValue, napi_writable | napi_configurable, NULL }
41971cb0ef41Sopenharmony_ci}
41981cb0ef41Sopenharmony_cistatus = napi_define_properties(env,
41991cb0ef41Sopenharmony_ci                                obj,
42001cb0ef41Sopenharmony_ci                                sizeof(descriptors) / sizeof(descriptors[0]),
42011cb0ef41Sopenharmony_ci                                descriptors);
42021cb0ef41Sopenharmony_ciif (status != napi_ok) return status;
42031cb0ef41Sopenharmony_ci```
42041cb0ef41Sopenharmony_ci
42051cb0ef41Sopenharmony_ci### Structures
42061cb0ef41Sopenharmony_ci
42071cb0ef41Sopenharmony_ci#### `napi_property_attributes`
42081cb0ef41Sopenharmony_ci
42091cb0ef41Sopenharmony_ci<!-- YAML
42101cb0ef41Sopenharmony_cichanges:
42111cb0ef41Sopenharmony_ci - version: v14.12.0
42121cb0ef41Sopenharmony_ci   pr-url: https://github.com/nodejs/node/pull/35214
42131cb0ef41Sopenharmony_ci   description: added `napi_default_method` and `napi_default_property`.
42141cb0ef41Sopenharmony_ci-->
42151cb0ef41Sopenharmony_ci
42161cb0ef41Sopenharmony_ci```c
42171cb0ef41Sopenharmony_citypedef enum {
42181cb0ef41Sopenharmony_ci  napi_default = 0,
42191cb0ef41Sopenharmony_ci  napi_writable = 1 << 0,
42201cb0ef41Sopenharmony_ci  napi_enumerable = 1 << 1,
42211cb0ef41Sopenharmony_ci  napi_configurable = 1 << 2,
42221cb0ef41Sopenharmony_ci
42231cb0ef41Sopenharmony_ci  // Used with napi_define_class to distinguish static properties
42241cb0ef41Sopenharmony_ci  // from instance properties. Ignored by napi_define_properties.
42251cb0ef41Sopenharmony_ci  napi_static = 1 << 10,
42261cb0ef41Sopenharmony_ci
42271cb0ef41Sopenharmony_ci  // Default for class methods.
42281cb0ef41Sopenharmony_ci  napi_default_method = napi_writable | napi_configurable,
42291cb0ef41Sopenharmony_ci
42301cb0ef41Sopenharmony_ci  // Default for object properties, like in JS obj[prop].
42311cb0ef41Sopenharmony_ci  napi_default_jsproperty = napi_writable |
42321cb0ef41Sopenharmony_ci                          napi_enumerable |
42331cb0ef41Sopenharmony_ci                          napi_configurable,
42341cb0ef41Sopenharmony_ci} napi_property_attributes;
42351cb0ef41Sopenharmony_ci```
42361cb0ef41Sopenharmony_ci
42371cb0ef41Sopenharmony_ci`napi_property_attributes` are flags used to control the behavior of properties
42381cb0ef41Sopenharmony_ciset on a JavaScript object. Other than `napi_static` they correspond to the
42391cb0ef41Sopenharmony_ciattributes listed in [Section 6.1.7.1][]
42401cb0ef41Sopenharmony_ciof the [ECMAScript Language Specification][].
42411cb0ef41Sopenharmony_ciThey can be one or more of the following bitflags:
42421cb0ef41Sopenharmony_ci
42431cb0ef41Sopenharmony_ci* `napi_default`: No explicit attributes are set on the property. By default, a
42441cb0ef41Sopenharmony_ci  property is read only, not enumerable and not configurable.
42451cb0ef41Sopenharmony_ci* `napi_writable`: The property is writable.
42461cb0ef41Sopenharmony_ci* `napi_enumerable`: The property is enumerable.
42471cb0ef41Sopenharmony_ci* `napi_configurable`: The property is configurable as defined in
42481cb0ef41Sopenharmony_ci  [Section 6.1.7.1][] of the [ECMAScript Language Specification][].
42491cb0ef41Sopenharmony_ci* `napi_static`: The property will be defined as a static property on a class as
42501cb0ef41Sopenharmony_ci  opposed to an instance property, which is the default. This is used only by
42511cb0ef41Sopenharmony_ci  [`napi_define_class`][]. It is ignored by `napi_define_properties`.
42521cb0ef41Sopenharmony_ci* `napi_default_method`: Like a method in a JS class, the property is
42531cb0ef41Sopenharmony_ci  configurable and writeable, but not enumerable.
42541cb0ef41Sopenharmony_ci* `napi_default_jsproperty`: Like a property set via assignment in JavaScript,
42551cb0ef41Sopenharmony_ci  the property is writable, enumerable, and configurable.
42561cb0ef41Sopenharmony_ci
42571cb0ef41Sopenharmony_ci#### `napi_property_descriptor`
42581cb0ef41Sopenharmony_ci
42591cb0ef41Sopenharmony_ci```c
42601cb0ef41Sopenharmony_citypedef struct {
42611cb0ef41Sopenharmony_ci  // One of utf8name or name should be NULL.
42621cb0ef41Sopenharmony_ci  const char* utf8name;
42631cb0ef41Sopenharmony_ci  napi_value name;
42641cb0ef41Sopenharmony_ci
42651cb0ef41Sopenharmony_ci  napi_callback method;
42661cb0ef41Sopenharmony_ci  napi_callback getter;
42671cb0ef41Sopenharmony_ci  napi_callback setter;
42681cb0ef41Sopenharmony_ci  napi_value value;
42691cb0ef41Sopenharmony_ci
42701cb0ef41Sopenharmony_ci  napi_property_attributes attributes;
42711cb0ef41Sopenharmony_ci  void* data;
42721cb0ef41Sopenharmony_ci} napi_property_descriptor;
42731cb0ef41Sopenharmony_ci```
42741cb0ef41Sopenharmony_ci
42751cb0ef41Sopenharmony_ci* `utf8name`: Optional string describing the key for the property,
42761cb0ef41Sopenharmony_ci  encoded as UTF8. One of `utf8name` or `name` must be provided for the
42771cb0ef41Sopenharmony_ci  property.
42781cb0ef41Sopenharmony_ci* `name`: Optional `napi_value` that points to a JavaScript string or symbol
42791cb0ef41Sopenharmony_ci  to be used as the key for the property. One of `utf8name` or `name` must
42801cb0ef41Sopenharmony_ci  be provided for the property.
42811cb0ef41Sopenharmony_ci* `value`: The value that's retrieved by a get access of the property if the
42821cb0ef41Sopenharmony_ci  property is a data property. If this is passed in, set `getter`, `setter`,
42831cb0ef41Sopenharmony_ci  `method` and `data` to `NULL` (since these members won't be used).
42841cb0ef41Sopenharmony_ci* `getter`: A function to call when a get access of the property is performed.
42851cb0ef41Sopenharmony_ci  If this is passed in, set `value` and `method` to `NULL` (since these members
42861cb0ef41Sopenharmony_ci  won't be used). The given function is called implicitly by the runtime when
42871cb0ef41Sopenharmony_ci  the property is accessed from JavaScript code (or if a get on the property is
42881cb0ef41Sopenharmony_ci  performed using a Node-API call). [`napi_callback`][] provides more details.
42891cb0ef41Sopenharmony_ci* `setter`: A function to call when a set access of the property is performed.
42901cb0ef41Sopenharmony_ci  If this is passed in, set `value` and `method` to `NULL` (since these members
42911cb0ef41Sopenharmony_ci  won't be used). The given function is called implicitly by the runtime when
42921cb0ef41Sopenharmony_ci  the property is set from JavaScript code (or if a set on the property is
42931cb0ef41Sopenharmony_ci  performed using a Node-API call). [`napi_callback`][] provides more details.
42941cb0ef41Sopenharmony_ci* `method`: Set this to make the property descriptor object's `value`
42951cb0ef41Sopenharmony_ci  property to be a JavaScript function represented by `method`. If this is
42961cb0ef41Sopenharmony_ci  passed in, set `value`, `getter` and `setter` to `NULL` (since these members
42971cb0ef41Sopenharmony_ci  won't be used). [`napi_callback`][] provides more details.
42981cb0ef41Sopenharmony_ci* `attributes`: The attributes associated with the particular property. See
42991cb0ef41Sopenharmony_ci  [`napi_property_attributes`][].
43001cb0ef41Sopenharmony_ci* `data`: The callback data passed into `method`, `getter` and `setter` if this
43011cb0ef41Sopenharmony_ci  function is invoked.
43021cb0ef41Sopenharmony_ci
43031cb0ef41Sopenharmony_ci### Functions
43041cb0ef41Sopenharmony_ci
43051cb0ef41Sopenharmony_ci#### `napi_get_property_names`
43061cb0ef41Sopenharmony_ci
43071cb0ef41Sopenharmony_ci<!-- YAML
43081cb0ef41Sopenharmony_ciadded: v8.0.0
43091cb0ef41Sopenharmony_cinapiVersion: 1
43101cb0ef41Sopenharmony_ci-->
43111cb0ef41Sopenharmony_ci
43121cb0ef41Sopenharmony_ci```c
43131cb0ef41Sopenharmony_cinapi_status napi_get_property_names(napi_env env,
43141cb0ef41Sopenharmony_ci                                    napi_value object,
43151cb0ef41Sopenharmony_ci                                    napi_value* result);
43161cb0ef41Sopenharmony_ci```
43171cb0ef41Sopenharmony_ci
43181cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
43191cb0ef41Sopenharmony_ci* `[in] object`: The object from which to retrieve the properties.
43201cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing an array of JavaScript values
43211cb0ef41Sopenharmony_ci  that represent the property names of the object. The API can be used to
43221cb0ef41Sopenharmony_ci  iterate over `result` using [`napi_get_array_length`][]
43231cb0ef41Sopenharmony_ci  and [`napi_get_element`][].
43241cb0ef41Sopenharmony_ci
43251cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
43261cb0ef41Sopenharmony_ci
43271cb0ef41Sopenharmony_ciThis API returns the names of the enumerable properties of `object` as an array
43281cb0ef41Sopenharmony_ciof strings. The properties of `object` whose key is a symbol will not be
43291cb0ef41Sopenharmony_ciincluded.
43301cb0ef41Sopenharmony_ci
43311cb0ef41Sopenharmony_ci#### `napi_get_all_property_names`
43321cb0ef41Sopenharmony_ci
43331cb0ef41Sopenharmony_ci<!-- YAML
43341cb0ef41Sopenharmony_ciadded:
43351cb0ef41Sopenharmony_ci - v13.7.0
43361cb0ef41Sopenharmony_ci - v12.17.0
43371cb0ef41Sopenharmony_ci - v10.20.0
43381cb0ef41Sopenharmony_cinapiVersion: 6
43391cb0ef41Sopenharmony_ci-->
43401cb0ef41Sopenharmony_ci
43411cb0ef41Sopenharmony_ci```c
43421cb0ef41Sopenharmony_cinapi_get_all_property_names(napi_env env,
43431cb0ef41Sopenharmony_ci                            napi_value object,
43441cb0ef41Sopenharmony_ci                            napi_key_collection_mode key_mode,
43451cb0ef41Sopenharmony_ci                            napi_key_filter key_filter,
43461cb0ef41Sopenharmony_ci                            napi_key_conversion key_conversion,
43471cb0ef41Sopenharmony_ci                            napi_value* result);
43481cb0ef41Sopenharmony_ci```
43491cb0ef41Sopenharmony_ci
43501cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
43511cb0ef41Sopenharmony_ci* `[in] object`: The object from which to retrieve the properties.
43521cb0ef41Sopenharmony_ci* `[in] key_mode`: Whether to retrieve prototype properties as well.
43531cb0ef41Sopenharmony_ci* `[in] key_filter`: Which properties to retrieve
43541cb0ef41Sopenharmony_ci  (enumerable/readable/writable).
43551cb0ef41Sopenharmony_ci* `[in] key_conversion`: Whether to convert numbered property keys to strings.
43561cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing an array of JavaScript values
43571cb0ef41Sopenharmony_ci  that represent the property names of the object. [`napi_get_array_length`][]
43581cb0ef41Sopenharmony_ci  and [`napi_get_element`][] can be used to iterate over `result`.
43591cb0ef41Sopenharmony_ci
43601cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
43611cb0ef41Sopenharmony_ci
43621cb0ef41Sopenharmony_ciThis API returns an array containing the names of the available properties
43631cb0ef41Sopenharmony_ciof this object.
43641cb0ef41Sopenharmony_ci
43651cb0ef41Sopenharmony_ci#### `napi_set_property`
43661cb0ef41Sopenharmony_ci
43671cb0ef41Sopenharmony_ci<!-- YAML
43681cb0ef41Sopenharmony_ciadded: v8.0.0
43691cb0ef41Sopenharmony_cinapiVersion: 1
43701cb0ef41Sopenharmony_ci-->
43711cb0ef41Sopenharmony_ci
43721cb0ef41Sopenharmony_ci```c
43731cb0ef41Sopenharmony_cinapi_status napi_set_property(napi_env env,
43741cb0ef41Sopenharmony_ci                              napi_value object,
43751cb0ef41Sopenharmony_ci                              napi_value key,
43761cb0ef41Sopenharmony_ci                              napi_value value);
43771cb0ef41Sopenharmony_ci```
43781cb0ef41Sopenharmony_ci
43791cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
43801cb0ef41Sopenharmony_ci* `[in] object`: The object on which to set the property.
43811cb0ef41Sopenharmony_ci* `[in] key`: The name of the property to set.
43821cb0ef41Sopenharmony_ci* `[in] value`: The property value.
43831cb0ef41Sopenharmony_ci
43841cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
43851cb0ef41Sopenharmony_ci
43861cb0ef41Sopenharmony_ciThis API set a property on the `Object` passed in.
43871cb0ef41Sopenharmony_ci
43881cb0ef41Sopenharmony_ci#### `napi_get_property`
43891cb0ef41Sopenharmony_ci
43901cb0ef41Sopenharmony_ci<!-- YAML
43911cb0ef41Sopenharmony_ciadded: v8.0.0
43921cb0ef41Sopenharmony_cinapiVersion: 1
43931cb0ef41Sopenharmony_ci-->
43941cb0ef41Sopenharmony_ci
43951cb0ef41Sopenharmony_ci```c
43961cb0ef41Sopenharmony_cinapi_status napi_get_property(napi_env env,
43971cb0ef41Sopenharmony_ci                              napi_value object,
43981cb0ef41Sopenharmony_ci                              napi_value key,
43991cb0ef41Sopenharmony_ci                              napi_value* result);
44001cb0ef41Sopenharmony_ci```
44011cb0ef41Sopenharmony_ci
44021cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
44031cb0ef41Sopenharmony_ci* `[in] object`: The object from which to retrieve the property.
44041cb0ef41Sopenharmony_ci* `[in] key`: The name of the property to retrieve.
44051cb0ef41Sopenharmony_ci* `[out] result`: The value of the property.
44061cb0ef41Sopenharmony_ci
44071cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
44081cb0ef41Sopenharmony_ci
44091cb0ef41Sopenharmony_ciThis API gets the requested property from the `Object` passed in.
44101cb0ef41Sopenharmony_ci
44111cb0ef41Sopenharmony_ci#### `napi_has_property`
44121cb0ef41Sopenharmony_ci
44131cb0ef41Sopenharmony_ci<!-- YAML
44141cb0ef41Sopenharmony_ciadded: v8.0.0
44151cb0ef41Sopenharmony_cinapiVersion: 1
44161cb0ef41Sopenharmony_ci-->
44171cb0ef41Sopenharmony_ci
44181cb0ef41Sopenharmony_ci```c
44191cb0ef41Sopenharmony_cinapi_status napi_has_property(napi_env env,
44201cb0ef41Sopenharmony_ci                              napi_value object,
44211cb0ef41Sopenharmony_ci                              napi_value key,
44221cb0ef41Sopenharmony_ci                              bool* result);
44231cb0ef41Sopenharmony_ci```
44241cb0ef41Sopenharmony_ci
44251cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
44261cb0ef41Sopenharmony_ci* `[in] object`: The object to query.
44271cb0ef41Sopenharmony_ci* `[in] key`: The name of the property whose existence to check.
44281cb0ef41Sopenharmony_ci* `[out] result`: Whether the property exists on the object or not.
44291cb0ef41Sopenharmony_ci
44301cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
44311cb0ef41Sopenharmony_ci
44321cb0ef41Sopenharmony_ciThis API checks if the `Object` passed in has the named property.
44331cb0ef41Sopenharmony_ci
44341cb0ef41Sopenharmony_ci#### `napi_delete_property`
44351cb0ef41Sopenharmony_ci
44361cb0ef41Sopenharmony_ci<!-- YAML
44371cb0ef41Sopenharmony_ciadded: v8.2.0
44381cb0ef41Sopenharmony_cinapiVersion: 1
44391cb0ef41Sopenharmony_ci-->
44401cb0ef41Sopenharmony_ci
44411cb0ef41Sopenharmony_ci```c
44421cb0ef41Sopenharmony_cinapi_status napi_delete_property(napi_env env,
44431cb0ef41Sopenharmony_ci                                 napi_value object,
44441cb0ef41Sopenharmony_ci                                 napi_value key,
44451cb0ef41Sopenharmony_ci                                 bool* result);
44461cb0ef41Sopenharmony_ci```
44471cb0ef41Sopenharmony_ci
44481cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
44491cb0ef41Sopenharmony_ci* `[in] object`: The object to query.
44501cb0ef41Sopenharmony_ci* `[in] key`: The name of the property to delete.
44511cb0ef41Sopenharmony_ci* `[out] result`: Whether the property deletion succeeded or not. `result` can
44521cb0ef41Sopenharmony_ci  optionally be ignored by passing `NULL`.
44531cb0ef41Sopenharmony_ci
44541cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
44551cb0ef41Sopenharmony_ci
44561cb0ef41Sopenharmony_ciThis API attempts to delete the `key` own property from `object`.
44571cb0ef41Sopenharmony_ci
44581cb0ef41Sopenharmony_ci#### `napi_has_own_property`
44591cb0ef41Sopenharmony_ci
44601cb0ef41Sopenharmony_ci<!-- YAML
44611cb0ef41Sopenharmony_ciadded: v8.2.0
44621cb0ef41Sopenharmony_cinapiVersion: 1
44631cb0ef41Sopenharmony_ci-->
44641cb0ef41Sopenharmony_ci
44651cb0ef41Sopenharmony_ci```c
44661cb0ef41Sopenharmony_cinapi_status napi_has_own_property(napi_env env,
44671cb0ef41Sopenharmony_ci                                  napi_value object,
44681cb0ef41Sopenharmony_ci                                  napi_value key,
44691cb0ef41Sopenharmony_ci                                  bool* result);
44701cb0ef41Sopenharmony_ci```
44711cb0ef41Sopenharmony_ci
44721cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
44731cb0ef41Sopenharmony_ci* `[in] object`: The object to query.
44741cb0ef41Sopenharmony_ci* `[in] key`: The name of the own property whose existence to check.
44751cb0ef41Sopenharmony_ci* `[out] result`: Whether the own property exists on the object or not.
44761cb0ef41Sopenharmony_ci
44771cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
44781cb0ef41Sopenharmony_ci
44791cb0ef41Sopenharmony_ciThis API checks if the `Object` passed in has the named own property. `key` must
44801cb0ef41Sopenharmony_cibe a `string` or a `symbol`, or an error will be thrown. Node-API will not
44811cb0ef41Sopenharmony_ciperform any conversion between data types.
44821cb0ef41Sopenharmony_ci
44831cb0ef41Sopenharmony_ci#### `napi_set_named_property`
44841cb0ef41Sopenharmony_ci
44851cb0ef41Sopenharmony_ci<!-- YAML
44861cb0ef41Sopenharmony_ciadded: v8.0.0
44871cb0ef41Sopenharmony_cinapiVersion: 1
44881cb0ef41Sopenharmony_ci-->
44891cb0ef41Sopenharmony_ci
44901cb0ef41Sopenharmony_ci```c
44911cb0ef41Sopenharmony_cinapi_status napi_set_named_property(napi_env env,
44921cb0ef41Sopenharmony_ci                                    napi_value object,
44931cb0ef41Sopenharmony_ci                                    const char* utf8Name,
44941cb0ef41Sopenharmony_ci                                    napi_value value);
44951cb0ef41Sopenharmony_ci```
44961cb0ef41Sopenharmony_ci
44971cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
44981cb0ef41Sopenharmony_ci* `[in] object`: The object on which to set the property.
44991cb0ef41Sopenharmony_ci* `[in] utf8Name`: The name of the property to set.
45001cb0ef41Sopenharmony_ci* `[in] value`: The property value.
45011cb0ef41Sopenharmony_ci
45021cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
45031cb0ef41Sopenharmony_ci
45041cb0ef41Sopenharmony_ciThis method is equivalent to calling [`napi_set_property`][] with a `napi_value`
45051cb0ef41Sopenharmony_cicreated from the string passed in as `utf8Name`.
45061cb0ef41Sopenharmony_ci
45071cb0ef41Sopenharmony_ci#### `napi_get_named_property`
45081cb0ef41Sopenharmony_ci
45091cb0ef41Sopenharmony_ci<!-- YAML
45101cb0ef41Sopenharmony_ciadded: v8.0.0
45111cb0ef41Sopenharmony_cinapiVersion: 1
45121cb0ef41Sopenharmony_ci-->
45131cb0ef41Sopenharmony_ci
45141cb0ef41Sopenharmony_ci```c
45151cb0ef41Sopenharmony_cinapi_status napi_get_named_property(napi_env env,
45161cb0ef41Sopenharmony_ci                                    napi_value object,
45171cb0ef41Sopenharmony_ci                                    const char* utf8Name,
45181cb0ef41Sopenharmony_ci                                    napi_value* result);
45191cb0ef41Sopenharmony_ci```
45201cb0ef41Sopenharmony_ci
45211cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
45221cb0ef41Sopenharmony_ci* `[in] object`: The object from which to retrieve the property.
45231cb0ef41Sopenharmony_ci* `[in] utf8Name`: The name of the property to get.
45241cb0ef41Sopenharmony_ci* `[out] result`: The value of the property.
45251cb0ef41Sopenharmony_ci
45261cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
45271cb0ef41Sopenharmony_ci
45281cb0ef41Sopenharmony_ciThis method is equivalent to calling [`napi_get_property`][] with a `napi_value`
45291cb0ef41Sopenharmony_cicreated from the string passed in as `utf8Name`.
45301cb0ef41Sopenharmony_ci
45311cb0ef41Sopenharmony_ci#### `napi_has_named_property`
45321cb0ef41Sopenharmony_ci
45331cb0ef41Sopenharmony_ci<!-- YAML
45341cb0ef41Sopenharmony_ciadded: v8.0.0
45351cb0ef41Sopenharmony_cinapiVersion: 1
45361cb0ef41Sopenharmony_ci-->
45371cb0ef41Sopenharmony_ci
45381cb0ef41Sopenharmony_ci```c
45391cb0ef41Sopenharmony_cinapi_status napi_has_named_property(napi_env env,
45401cb0ef41Sopenharmony_ci                                    napi_value object,
45411cb0ef41Sopenharmony_ci                                    const char* utf8Name,
45421cb0ef41Sopenharmony_ci                                    bool* result);
45431cb0ef41Sopenharmony_ci```
45441cb0ef41Sopenharmony_ci
45451cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
45461cb0ef41Sopenharmony_ci* `[in] object`: The object to query.
45471cb0ef41Sopenharmony_ci* `[in] utf8Name`: The name of the property whose existence to check.
45481cb0ef41Sopenharmony_ci* `[out] result`: Whether the property exists on the object or not.
45491cb0ef41Sopenharmony_ci
45501cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
45511cb0ef41Sopenharmony_ci
45521cb0ef41Sopenharmony_ciThis method is equivalent to calling [`napi_has_property`][] with a `napi_value`
45531cb0ef41Sopenharmony_cicreated from the string passed in as `utf8Name`.
45541cb0ef41Sopenharmony_ci
45551cb0ef41Sopenharmony_ci#### `napi_set_element`
45561cb0ef41Sopenharmony_ci
45571cb0ef41Sopenharmony_ci<!-- YAML
45581cb0ef41Sopenharmony_ciadded: v8.0.0
45591cb0ef41Sopenharmony_cinapiVersion: 1
45601cb0ef41Sopenharmony_ci-->
45611cb0ef41Sopenharmony_ci
45621cb0ef41Sopenharmony_ci```c
45631cb0ef41Sopenharmony_cinapi_status napi_set_element(napi_env env,
45641cb0ef41Sopenharmony_ci                             napi_value object,
45651cb0ef41Sopenharmony_ci                             uint32_t index,
45661cb0ef41Sopenharmony_ci                             napi_value value);
45671cb0ef41Sopenharmony_ci```
45681cb0ef41Sopenharmony_ci
45691cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
45701cb0ef41Sopenharmony_ci* `[in] object`: The object from which to set the properties.
45711cb0ef41Sopenharmony_ci* `[in] index`: The index of the property to set.
45721cb0ef41Sopenharmony_ci* `[in] value`: The property value.
45731cb0ef41Sopenharmony_ci
45741cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
45751cb0ef41Sopenharmony_ci
45761cb0ef41Sopenharmony_ciThis API sets an element on the `Object` passed in.
45771cb0ef41Sopenharmony_ci
45781cb0ef41Sopenharmony_ci#### `napi_get_element`
45791cb0ef41Sopenharmony_ci
45801cb0ef41Sopenharmony_ci<!-- YAML
45811cb0ef41Sopenharmony_ciadded: v8.0.0
45821cb0ef41Sopenharmony_cinapiVersion: 1
45831cb0ef41Sopenharmony_ci-->
45841cb0ef41Sopenharmony_ci
45851cb0ef41Sopenharmony_ci```c
45861cb0ef41Sopenharmony_cinapi_status napi_get_element(napi_env env,
45871cb0ef41Sopenharmony_ci                             napi_value object,
45881cb0ef41Sopenharmony_ci                             uint32_t index,
45891cb0ef41Sopenharmony_ci                             napi_value* result);
45901cb0ef41Sopenharmony_ci```
45911cb0ef41Sopenharmony_ci
45921cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
45931cb0ef41Sopenharmony_ci* `[in] object`: The object from which to retrieve the property.
45941cb0ef41Sopenharmony_ci* `[in] index`: The index of the property to get.
45951cb0ef41Sopenharmony_ci* `[out] result`: The value of the property.
45961cb0ef41Sopenharmony_ci
45971cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
45981cb0ef41Sopenharmony_ci
45991cb0ef41Sopenharmony_ciThis API gets the element at the requested index.
46001cb0ef41Sopenharmony_ci
46011cb0ef41Sopenharmony_ci#### `napi_has_element`
46021cb0ef41Sopenharmony_ci
46031cb0ef41Sopenharmony_ci<!-- YAML
46041cb0ef41Sopenharmony_ciadded: v8.0.0
46051cb0ef41Sopenharmony_cinapiVersion: 1
46061cb0ef41Sopenharmony_ci-->
46071cb0ef41Sopenharmony_ci
46081cb0ef41Sopenharmony_ci```c
46091cb0ef41Sopenharmony_cinapi_status napi_has_element(napi_env env,
46101cb0ef41Sopenharmony_ci                             napi_value object,
46111cb0ef41Sopenharmony_ci                             uint32_t index,
46121cb0ef41Sopenharmony_ci                             bool* result);
46131cb0ef41Sopenharmony_ci```
46141cb0ef41Sopenharmony_ci
46151cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
46161cb0ef41Sopenharmony_ci* `[in] object`: The object to query.
46171cb0ef41Sopenharmony_ci* `[in] index`: The index of the property whose existence to check.
46181cb0ef41Sopenharmony_ci* `[out] result`: Whether the property exists on the object or not.
46191cb0ef41Sopenharmony_ci
46201cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
46211cb0ef41Sopenharmony_ci
46221cb0ef41Sopenharmony_ciThis API returns if the `Object` passed in has an element at the
46231cb0ef41Sopenharmony_cirequested index.
46241cb0ef41Sopenharmony_ci
46251cb0ef41Sopenharmony_ci#### `napi_delete_element`
46261cb0ef41Sopenharmony_ci
46271cb0ef41Sopenharmony_ci<!-- YAML
46281cb0ef41Sopenharmony_ciadded: v8.2.0
46291cb0ef41Sopenharmony_cinapiVersion: 1
46301cb0ef41Sopenharmony_ci-->
46311cb0ef41Sopenharmony_ci
46321cb0ef41Sopenharmony_ci```c
46331cb0ef41Sopenharmony_cinapi_status napi_delete_element(napi_env env,
46341cb0ef41Sopenharmony_ci                                napi_value object,
46351cb0ef41Sopenharmony_ci                                uint32_t index,
46361cb0ef41Sopenharmony_ci                                bool* result);
46371cb0ef41Sopenharmony_ci```
46381cb0ef41Sopenharmony_ci
46391cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
46401cb0ef41Sopenharmony_ci* `[in] object`: The object to query.
46411cb0ef41Sopenharmony_ci* `[in] index`: The index of the property to delete.
46421cb0ef41Sopenharmony_ci* `[out] result`: Whether the element deletion succeeded or not. `result` can
46431cb0ef41Sopenharmony_ci  optionally be ignored by passing `NULL`.
46441cb0ef41Sopenharmony_ci
46451cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
46461cb0ef41Sopenharmony_ci
46471cb0ef41Sopenharmony_ciThis API attempts to delete the specified `index` from `object`.
46481cb0ef41Sopenharmony_ci
46491cb0ef41Sopenharmony_ci#### `napi_define_properties`
46501cb0ef41Sopenharmony_ci
46511cb0ef41Sopenharmony_ci<!-- YAML
46521cb0ef41Sopenharmony_ciadded: v8.0.0
46531cb0ef41Sopenharmony_cinapiVersion: 1
46541cb0ef41Sopenharmony_ci-->
46551cb0ef41Sopenharmony_ci
46561cb0ef41Sopenharmony_ci```c
46571cb0ef41Sopenharmony_cinapi_status napi_define_properties(napi_env env,
46581cb0ef41Sopenharmony_ci                                   napi_value object,
46591cb0ef41Sopenharmony_ci                                   size_t property_count,
46601cb0ef41Sopenharmony_ci                                   const napi_property_descriptor* properties);
46611cb0ef41Sopenharmony_ci```
46621cb0ef41Sopenharmony_ci
46631cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
46641cb0ef41Sopenharmony_ci* `[in] object`: The object from which to retrieve the properties.
46651cb0ef41Sopenharmony_ci* `[in] property_count`: The number of elements in the `properties` array.
46661cb0ef41Sopenharmony_ci* `[in] properties`: The array of property descriptors.
46671cb0ef41Sopenharmony_ci
46681cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
46691cb0ef41Sopenharmony_ci
46701cb0ef41Sopenharmony_ciThis method allows the efficient definition of multiple properties on a given
46711cb0ef41Sopenharmony_ciobject. The properties are defined using property descriptors (see
46721cb0ef41Sopenharmony_ci[`napi_property_descriptor`][]). Given an array of such property descriptors,
46731cb0ef41Sopenharmony_cithis API will set the properties on the object one at a time, as defined by
46741cb0ef41Sopenharmony_ci`DefineOwnProperty()` (described in [Section 9.1.6][] of the ECMA-262
46751cb0ef41Sopenharmony_cispecification).
46761cb0ef41Sopenharmony_ci
46771cb0ef41Sopenharmony_ci#### `napi_object_freeze`
46781cb0ef41Sopenharmony_ci
46791cb0ef41Sopenharmony_ci<!-- YAML
46801cb0ef41Sopenharmony_ciadded:
46811cb0ef41Sopenharmony_ci  - v14.14.0
46821cb0ef41Sopenharmony_ci  - v12.20.0
46831cb0ef41Sopenharmony_cinapiVersion: 8
46841cb0ef41Sopenharmony_ci-->
46851cb0ef41Sopenharmony_ci
46861cb0ef41Sopenharmony_ci```c
46871cb0ef41Sopenharmony_cinapi_status napi_object_freeze(napi_env env,
46881cb0ef41Sopenharmony_ci                               napi_value object);
46891cb0ef41Sopenharmony_ci```
46901cb0ef41Sopenharmony_ci
46911cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
46921cb0ef41Sopenharmony_ci* `[in] object`: The object to freeze.
46931cb0ef41Sopenharmony_ci
46941cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
46951cb0ef41Sopenharmony_ci
46961cb0ef41Sopenharmony_ciThis method freezes a given object. This prevents new properties from
46971cb0ef41Sopenharmony_cibeing added to it, existing properties from being removed, prevents
46981cb0ef41Sopenharmony_cichanging the enumerability, configurability, or writability of existing
46991cb0ef41Sopenharmony_ciproperties, and prevents the values of existing properties from being changed.
47001cb0ef41Sopenharmony_ciIt also prevents the object's prototype from being changed. This is described
47011cb0ef41Sopenharmony_ciin [Section 19.1.2.6](https://tc39.es/ecma262/#sec-object.freeze) of the
47021cb0ef41Sopenharmony_ciECMA-262 specification.
47031cb0ef41Sopenharmony_ci
47041cb0ef41Sopenharmony_ci#### `napi_object_seal`
47051cb0ef41Sopenharmony_ci
47061cb0ef41Sopenharmony_ci<!-- YAML
47071cb0ef41Sopenharmony_ciadded:
47081cb0ef41Sopenharmony_ci  - v14.14.0
47091cb0ef41Sopenharmony_ci  - v12.20.0
47101cb0ef41Sopenharmony_cinapiVersion: 8
47111cb0ef41Sopenharmony_ci-->
47121cb0ef41Sopenharmony_ci
47131cb0ef41Sopenharmony_ci```c
47141cb0ef41Sopenharmony_cinapi_status napi_object_seal(napi_env env,
47151cb0ef41Sopenharmony_ci                             napi_value object);
47161cb0ef41Sopenharmony_ci```
47171cb0ef41Sopenharmony_ci
47181cb0ef41Sopenharmony_ci* `[in] env`: The environment that the Node-API call is invoked under.
47191cb0ef41Sopenharmony_ci* `[in] object`: The object to seal.
47201cb0ef41Sopenharmony_ci
47211cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
47221cb0ef41Sopenharmony_ci
47231cb0ef41Sopenharmony_ciThis method seals a given object. This prevents new properties from being
47241cb0ef41Sopenharmony_ciadded to it, as well as marking all existing properties as non-configurable.
47251cb0ef41Sopenharmony_ciThis is described in [Section 19.1.2.20](https://tc39.es/ecma262/#sec-object.seal)
47261cb0ef41Sopenharmony_ciof the ECMA-262 specification.
47271cb0ef41Sopenharmony_ci
47281cb0ef41Sopenharmony_ci## Working with JavaScript functions
47291cb0ef41Sopenharmony_ci
47301cb0ef41Sopenharmony_ciNode-API provides a set of APIs that allow JavaScript code to
47311cb0ef41Sopenharmony_cicall back into native code. Node-APIs that support calling back
47321cb0ef41Sopenharmony_ciinto native code take in a callback functions represented by
47331cb0ef41Sopenharmony_cithe `napi_callback` type. When the JavaScript VM calls back to
47341cb0ef41Sopenharmony_cinative code, the `napi_callback` function provided is invoked. The APIs
47351cb0ef41Sopenharmony_cidocumented in this section allow the callback function to do the
47361cb0ef41Sopenharmony_cifollowing:
47371cb0ef41Sopenharmony_ci
47381cb0ef41Sopenharmony_ci* Get information about the context in which the callback was invoked.
47391cb0ef41Sopenharmony_ci* Get the arguments passed into the callback.
47401cb0ef41Sopenharmony_ci* Return a `napi_value` back from the callback.
47411cb0ef41Sopenharmony_ci
47421cb0ef41Sopenharmony_ciAdditionally, Node-API provides a set of functions which allow calling
47431cb0ef41Sopenharmony_ciJavaScript functions from native code. One can either call a function
47441cb0ef41Sopenharmony_cilike a regular JavaScript function call, or as a constructor
47451cb0ef41Sopenharmony_cifunction.
47461cb0ef41Sopenharmony_ci
47471cb0ef41Sopenharmony_ciAny non-`NULL` data which is passed to this API via the `data` field of the
47481cb0ef41Sopenharmony_ci`napi_property_descriptor` items can be associated with `object` and freed
47491cb0ef41Sopenharmony_ciwhenever `object` is garbage-collected by passing both `object` and the data to
47501cb0ef41Sopenharmony_ci[`napi_add_finalizer`][].
47511cb0ef41Sopenharmony_ci
47521cb0ef41Sopenharmony_ci### `napi_call_function`
47531cb0ef41Sopenharmony_ci
47541cb0ef41Sopenharmony_ci<!-- YAML
47551cb0ef41Sopenharmony_ciadded: v8.0.0
47561cb0ef41Sopenharmony_cinapiVersion: 1
47571cb0ef41Sopenharmony_ci-->
47581cb0ef41Sopenharmony_ci
47591cb0ef41Sopenharmony_ci```c
47601cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_call_function(napi_env env,
47611cb0ef41Sopenharmony_ci                                           napi_value recv,
47621cb0ef41Sopenharmony_ci                                           napi_value func,
47631cb0ef41Sopenharmony_ci                                           size_t argc,
47641cb0ef41Sopenharmony_ci                                           const napi_value* argv,
47651cb0ef41Sopenharmony_ci                                           napi_value* result);
47661cb0ef41Sopenharmony_ci```
47671cb0ef41Sopenharmony_ci
47681cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
47691cb0ef41Sopenharmony_ci* `[in] recv`: The `this` value passed to the called function.
47701cb0ef41Sopenharmony_ci* `[in] func`: `napi_value` representing the JavaScript function to be invoked.
47711cb0ef41Sopenharmony_ci* `[in] argc`: The count of elements in the `argv` array.
47721cb0ef41Sopenharmony_ci* `[in] argv`: Array of `napi_values` representing JavaScript values passed in
47731cb0ef41Sopenharmony_ci  as arguments to the function.
47741cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing the JavaScript object returned.
47751cb0ef41Sopenharmony_ci
47761cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
47771cb0ef41Sopenharmony_ci
47781cb0ef41Sopenharmony_ciThis method allows a JavaScript function object to be called from a native
47791cb0ef41Sopenharmony_ciadd-on. This is the primary mechanism of calling back _from_ the add-on's
47801cb0ef41Sopenharmony_cinative code _into_ JavaScript. For the special case of calling into JavaScript
47811cb0ef41Sopenharmony_ciafter an async operation, see [`napi_make_callback`][].
47821cb0ef41Sopenharmony_ci
47831cb0ef41Sopenharmony_ciA sample use case might look as follows. Consider the following JavaScript
47841cb0ef41Sopenharmony_cisnippet:
47851cb0ef41Sopenharmony_ci
47861cb0ef41Sopenharmony_ci```js
47871cb0ef41Sopenharmony_cifunction AddTwo(num) {
47881cb0ef41Sopenharmony_ci  return num + 2;
47891cb0ef41Sopenharmony_ci}
47901cb0ef41Sopenharmony_ciglobal.AddTwo = AddTwo;
47911cb0ef41Sopenharmony_ci```
47921cb0ef41Sopenharmony_ci
47931cb0ef41Sopenharmony_ciThen, the above function can be invoked from a native add-on using the
47941cb0ef41Sopenharmony_cifollowing code:
47951cb0ef41Sopenharmony_ci
47961cb0ef41Sopenharmony_ci```c
47971cb0ef41Sopenharmony_ci// Get the function named "AddTwo" on the global object
47981cb0ef41Sopenharmony_cinapi_value global, add_two, arg;
47991cb0ef41Sopenharmony_cinapi_status status = napi_get_global(env, &global);
48001cb0ef41Sopenharmony_ciif (status != napi_ok) return;
48011cb0ef41Sopenharmony_ci
48021cb0ef41Sopenharmony_cistatus = napi_get_named_property(env, global, "AddTwo", &add_two);
48031cb0ef41Sopenharmony_ciif (status != napi_ok) return;
48041cb0ef41Sopenharmony_ci
48051cb0ef41Sopenharmony_ci// const arg = 1337
48061cb0ef41Sopenharmony_cistatus = napi_create_int32(env, 1337, &arg);
48071cb0ef41Sopenharmony_ciif (status != napi_ok) return;
48081cb0ef41Sopenharmony_ci
48091cb0ef41Sopenharmony_cinapi_value* argv = &arg;
48101cb0ef41Sopenharmony_cisize_t argc = 1;
48111cb0ef41Sopenharmony_ci
48121cb0ef41Sopenharmony_ci// AddTwo(arg);
48131cb0ef41Sopenharmony_cinapi_value return_val;
48141cb0ef41Sopenharmony_cistatus = napi_call_function(env, global, add_two, argc, argv, &return_val);
48151cb0ef41Sopenharmony_ciif (status != napi_ok) return;
48161cb0ef41Sopenharmony_ci
48171cb0ef41Sopenharmony_ci// Convert the result back to a native type
48181cb0ef41Sopenharmony_ciint32_t result;
48191cb0ef41Sopenharmony_cistatus = napi_get_value_int32(env, return_val, &result);
48201cb0ef41Sopenharmony_ciif (status != napi_ok) return;
48211cb0ef41Sopenharmony_ci```
48221cb0ef41Sopenharmony_ci
48231cb0ef41Sopenharmony_ci### `napi_create_function`
48241cb0ef41Sopenharmony_ci
48251cb0ef41Sopenharmony_ci<!-- YAML
48261cb0ef41Sopenharmony_ciadded: v8.0.0
48271cb0ef41Sopenharmony_cinapiVersion: 1
48281cb0ef41Sopenharmony_ci-->
48291cb0ef41Sopenharmony_ci
48301cb0ef41Sopenharmony_ci```c
48311cb0ef41Sopenharmony_cinapi_status napi_create_function(napi_env env,
48321cb0ef41Sopenharmony_ci                                 const char* utf8name,
48331cb0ef41Sopenharmony_ci                                 size_t length,
48341cb0ef41Sopenharmony_ci                                 napi_callback cb,
48351cb0ef41Sopenharmony_ci                                 void* data,
48361cb0ef41Sopenharmony_ci                                 napi_value* result);
48371cb0ef41Sopenharmony_ci```
48381cb0ef41Sopenharmony_ci
48391cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
48401cb0ef41Sopenharmony_ci* `[in] utf8Name`: Optional name of the function encoded as UTF8. This is
48411cb0ef41Sopenharmony_ci  visible within JavaScript as the new function object's `name` property.
48421cb0ef41Sopenharmony_ci* `[in] length`: The length of the `utf8name` in bytes, or `NAPI_AUTO_LENGTH` if
48431cb0ef41Sopenharmony_ci  it is null-terminated.
48441cb0ef41Sopenharmony_ci* `[in] cb`: The native function which should be called when this function
48451cb0ef41Sopenharmony_ci  object is invoked. [`napi_callback`][] provides more details.
48461cb0ef41Sopenharmony_ci* `[in] data`: User-provided data context. This will be passed back into the
48471cb0ef41Sopenharmony_ci  function when invoked later.
48481cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing the JavaScript function object for
48491cb0ef41Sopenharmony_ci  the newly created function.
48501cb0ef41Sopenharmony_ci
48511cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
48521cb0ef41Sopenharmony_ci
48531cb0ef41Sopenharmony_ciThis API allows an add-on author to create a function object in native code.
48541cb0ef41Sopenharmony_ciThis is the primary mechanism to allow calling _into_ the add-on's native code
48551cb0ef41Sopenharmony_ci_from_ JavaScript.
48561cb0ef41Sopenharmony_ci
48571cb0ef41Sopenharmony_ciThe newly created function is not automatically visible from script after this
48581cb0ef41Sopenharmony_cicall. Instead, a property must be explicitly set on any object that is visible
48591cb0ef41Sopenharmony_cito JavaScript, in order for the function to be accessible from script.
48601cb0ef41Sopenharmony_ci
48611cb0ef41Sopenharmony_ciIn order to expose a function as part of the
48621cb0ef41Sopenharmony_ciadd-on's module exports, set the newly created function on the exports
48631cb0ef41Sopenharmony_ciobject. A sample module might look as follows:
48641cb0ef41Sopenharmony_ci
48651cb0ef41Sopenharmony_ci```c
48661cb0ef41Sopenharmony_cinapi_value SayHello(napi_env env, napi_callback_info info) {
48671cb0ef41Sopenharmony_ci  printf("Hello\n");
48681cb0ef41Sopenharmony_ci  return NULL;
48691cb0ef41Sopenharmony_ci}
48701cb0ef41Sopenharmony_ci
48711cb0ef41Sopenharmony_cinapi_value Init(napi_env env, napi_value exports) {
48721cb0ef41Sopenharmony_ci  napi_status status;
48731cb0ef41Sopenharmony_ci
48741cb0ef41Sopenharmony_ci  napi_value fn;
48751cb0ef41Sopenharmony_ci  status = napi_create_function(env, NULL, 0, SayHello, NULL, &fn);
48761cb0ef41Sopenharmony_ci  if (status != napi_ok) return NULL;
48771cb0ef41Sopenharmony_ci
48781cb0ef41Sopenharmony_ci  status = napi_set_named_property(env, exports, "sayHello", fn);
48791cb0ef41Sopenharmony_ci  if (status != napi_ok) return NULL;
48801cb0ef41Sopenharmony_ci
48811cb0ef41Sopenharmony_ci  return exports;
48821cb0ef41Sopenharmony_ci}
48831cb0ef41Sopenharmony_ci
48841cb0ef41Sopenharmony_ciNAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
48851cb0ef41Sopenharmony_ci```
48861cb0ef41Sopenharmony_ci
48871cb0ef41Sopenharmony_ciGiven the above code, the add-on can be used from JavaScript as follows:
48881cb0ef41Sopenharmony_ci
48891cb0ef41Sopenharmony_ci```js
48901cb0ef41Sopenharmony_ciconst myaddon = require('./addon');
48911cb0ef41Sopenharmony_cimyaddon.sayHello();
48921cb0ef41Sopenharmony_ci```
48931cb0ef41Sopenharmony_ci
48941cb0ef41Sopenharmony_ciThe string passed to `require()` is the name of the target in `binding.gyp`
48951cb0ef41Sopenharmony_ciresponsible for creating the `.node` file.
48961cb0ef41Sopenharmony_ci
48971cb0ef41Sopenharmony_ciAny non-`NULL` data which is passed to this API via the `data` parameter can
48981cb0ef41Sopenharmony_cibe associated with the resulting JavaScript function (which is returned in the
48991cb0ef41Sopenharmony_ci`result` parameter) and freed whenever the function is garbage-collected by
49001cb0ef41Sopenharmony_cipassing both the JavaScript function and the data to [`napi_add_finalizer`][].
49011cb0ef41Sopenharmony_ci
49021cb0ef41Sopenharmony_ciJavaScript `Function`s are described in [Section 19.2][] of the ECMAScript
49031cb0ef41Sopenharmony_ciLanguage Specification.
49041cb0ef41Sopenharmony_ci
49051cb0ef41Sopenharmony_ci### `napi_get_cb_info`
49061cb0ef41Sopenharmony_ci
49071cb0ef41Sopenharmony_ci<!-- YAML
49081cb0ef41Sopenharmony_ciadded: v8.0.0
49091cb0ef41Sopenharmony_cinapiVersion: 1
49101cb0ef41Sopenharmony_ci-->
49111cb0ef41Sopenharmony_ci
49121cb0ef41Sopenharmony_ci```c
49131cb0ef41Sopenharmony_cinapi_status napi_get_cb_info(napi_env env,
49141cb0ef41Sopenharmony_ci                             napi_callback_info cbinfo,
49151cb0ef41Sopenharmony_ci                             size_t* argc,
49161cb0ef41Sopenharmony_ci                             napi_value* argv,
49171cb0ef41Sopenharmony_ci                             napi_value* thisArg,
49181cb0ef41Sopenharmony_ci                             void** data)
49191cb0ef41Sopenharmony_ci```
49201cb0ef41Sopenharmony_ci
49211cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
49221cb0ef41Sopenharmony_ci* `[in] cbinfo`: The callback info passed into the callback function.
49231cb0ef41Sopenharmony_ci* `[in-out] argc`: Specifies the length of the provided `argv` array and
49241cb0ef41Sopenharmony_ci  receives the actual count of arguments. `argc` can
49251cb0ef41Sopenharmony_ci  optionally be ignored by passing `NULL`.
49261cb0ef41Sopenharmony_ci* `[out] argv`: C array of `napi_value`s to which the arguments will be
49271cb0ef41Sopenharmony_ci  copied. If there are more arguments than the provided count, only the
49281cb0ef41Sopenharmony_ci  requested number of arguments are copied. If there are fewer arguments
49291cb0ef41Sopenharmony_ci  provided than claimed, the rest of `argv` is filled with `napi_value` values
49301cb0ef41Sopenharmony_ci  that represent `undefined`. `argv` can optionally be ignored by
49311cb0ef41Sopenharmony_ci  passing `NULL`.
49321cb0ef41Sopenharmony_ci* `[out] thisArg`: Receives the JavaScript `this` argument for the call.
49331cb0ef41Sopenharmony_ci  `thisArg` can optionally be ignored by passing `NULL`.
49341cb0ef41Sopenharmony_ci* `[out] data`: Receives the data pointer for the callback. `data` can
49351cb0ef41Sopenharmony_ci  optionally be ignored by passing `NULL`.
49361cb0ef41Sopenharmony_ci
49371cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
49381cb0ef41Sopenharmony_ci
49391cb0ef41Sopenharmony_ciThis method is used within a callback function to retrieve details about the
49401cb0ef41Sopenharmony_cicall like the arguments and the `this` pointer from a given callback info.
49411cb0ef41Sopenharmony_ci
49421cb0ef41Sopenharmony_ci### `napi_get_new_target`
49431cb0ef41Sopenharmony_ci
49441cb0ef41Sopenharmony_ci<!-- YAML
49451cb0ef41Sopenharmony_ciadded: v8.6.0
49461cb0ef41Sopenharmony_cinapiVersion: 1
49471cb0ef41Sopenharmony_ci-->
49481cb0ef41Sopenharmony_ci
49491cb0ef41Sopenharmony_ci```c
49501cb0ef41Sopenharmony_cinapi_status napi_get_new_target(napi_env env,
49511cb0ef41Sopenharmony_ci                                napi_callback_info cbinfo,
49521cb0ef41Sopenharmony_ci                                napi_value* result)
49531cb0ef41Sopenharmony_ci```
49541cb0ef41Sopenharmony_ci
49551cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
49561cb0ef41Sopenharmony_ci* `[in] cbinfo`: The callback info passed into the callback function.
49571cb0ef41Sopenharmony_ci* `[out] result`: The `new.target` of the constructor call.
49581cb0ef41Sopenharmony_ci
49591cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
49601cb0ef41Sopenharmony_ci
49611cb0ef41Sopenharmony_ciThis API returns the `new.target` of the constructor call. If the current
49621cb0ef41Sopenharmony_cicallback is not a constructor call, the result is `NULL`.
49631cb0ef41Sopenharmony_ci
49641cb0ef41Sopenharmony_ci### `napi_new_instance`
49651cb0ef41Sopenharmony_ci
49661cb0ef41Sopenharmony_ci<!-- YAML
49671cb0ef41Sopenharmony_ciadded: v8.0.0
49681cb0ef41Sopenharmony_cinapiVersion: 1
49691cb0ef41Sopenharmony_ci-->
49701cb0ef41Sopenharmony_ci
49711cb0ef41Sopenharmony_ci```c
49721cb0ef41Sopenharmony_cinapi_status napi_new_instance(napi_env env,
49731cb0ef41Sopenharmony_ci                              napi_value cons,
49741cb0ef41Sopenharmony_ci                              size_t argc,
49751cb0ef41Sopenharmony_ci                              napi_value* argv,
49761cb0ef41Sopenharmony_ci                              napi_value* result)
49771cb0ef41Sopenharmony_ci```
49781cb0ef41Sopenharmony_ci
49791cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
49801cb0ef41Sopenharmony_ci* `[in] cons`: `napi_value` representing the JavaScript function to be invoked
49811cb0ef41Sopenharmony_ci  as a constructor.
49821cb0ef41Sopenharmony_ci* `[in] argc`: The count of elements in the `argv` array.
49831cb0ef41Sopenharmony_ci* `[in] argv`: Array of JavaScript values as `napi_value` representing the
49841cb0ef41Sopenharmony_ci  arguments to the constructor. If `argc` is zero this parameter may be
49851cb0ef41Sopenharmony_ci  omitted by passing in `NULL`.
49861cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing the JavaScript object returned,
49871cb0ef41Sopenharmony_ci  which in this case is the constructed object.
49881cb0ef41Sopenharmony_ci
49891cb0ef41Sopenharmony_ciThis method is used to instantiate a new JavaScript value using a given
49901cb0ef41Sopenharmony_ci`napi_value` that represents the constructor for the object. For example,
49911cb0ef41Sopenharmony_ciconsider the following snippet:
49921cb0ef41Sopenharmony_ci
49931cb0ef41Sopenharmony_ci```js
49941cb0ef41Sopenharmony_cifunction MyObject(param) {
49951cb0ef41Sopenharmony_ci  this.param = param;
49961cb0ef41Sopenharmony_ci}
49971cb0ef41Sopenharmony_ci
49981cb0ef41Sopenharmony_ciconst arg = 'hello';
49991cb0ef41Sopenharmony_ciconst value = new MyObject(arg);
50001cb0ef41Sopenharmony_ci```
50011cb0ef41Sopenharmony_ci
50021cb0ef41Sopenharmony_ciThe following can be approximated in Node-API using the following snippet:
50031cb0ef41Sopenharmony_ci
50041cb0ef41Sopenharmony_ci```c
50051cb0ef41Sopenharmony_ci// Get the constructor function MyObject
50061cb0ef41Sopenharmony_cinapi_value global, constructor, arg, value;
50071cb0ef41Sopenharmony_cinapi_status status = napi_get_global(env, &global);
50081cb0ef41Sopenharmony_ciif (status != napi_ok) return;
50091cb0ef41Sopenharmony_ci
50101cb0ef41Sopenharmony_cistatus = napi_get_named_property(env, global, "MyObject", &constructor);
50111cb0ef41Sopenharmony_ciif (status != napi_ok) return;
50121cb0ef41Sopenharmony_ci
50131cb0ef41Sopenharmony_ci// const arg = "hello"
50141cb0ef41Sopenharmony_cistatus = napi_create_string_utf8(env, "hello", NAPI_AUTO_LENGTH, &arg);
50151cb0ef41Sopenharmony_ciif (status != napi_ok) return;
50161cb0ef41Sopenharmony_ci
50171cb0ef41Sopenharmony_cinapi_value* argv = &arg;
50181cb0ef41Sopenharmony_cisize_t argc = 1;
50191cb0ef41Sopenharmony_ci
50201cb0ef41Sopenharmony_ci// const value = new MyObject(arg)
50211cb0ef41Sopenharmony_cistatus = napi_new_instance(env, constructor, argc, argv, &value);
50221cb0ef41Sopenharmony_ci```
50231cb0ef41Sopenharmony_ci
50241cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
50251cb0ef41Sopenharmony_ci
50261cb0ef41Sopenharmony_ci## Object wrap
50271cb0ef41Sopenharmony_ci
50281cb0ef41Sopenharmony_ciNode-API offers a way to "wrap" C++ classes and instances so that the class
50291cb0ef41Sopenharmony_ciconstructor and methods can be called from JavaScript.
50301cb0ef41Sopenharmony_ci
50311cb0ef41Sopenharmony_ci1. The [`napi_define_class`][] API defines a JavaScript class with constructor,
50321cb0ef41Sopenharmony_ci   static properties and methods, and instance properties and methods that
50331cb0ef41Sopenharmony_ci   correspond to the C++ class.
50341cb0ef41Sopenharmony_ci2. When JavaScript code invokes the constructor, the constructor callback
50351cb0ef41Sopenharmony_ci   uses [`napi_wrap`][] to wrap a new C++ instance in a JavaScript object,
50361cb0ef41Sopenharmony_ci   then returns the wrapper object.
50371cb0ef41Sopenharmony_ci3. When JavaScript code invokes a method or property accessor on the class,
50381cb0ef41Sopenharmony_ci   the corresponding `napi_callback` C++ function is invoked. For an instance
50391cb0ef41Sopenharmony_ci   callback, [`napi_unwrap`][] obtains the C++ instance that is the target of
50401cb0ef41Sopenharmony_ci   the call.
50411cb0ef41Sopenharmony_ci
50421cb0ef41Sopenharmony_ciFor wrapped objects it may be difficult to distinguish between a function
50431cb0ef41Sopenharmony_cicalled on a class prototype and a function called on an instance of a class.
50441cb0ef41Sopenharmony_ciA common pattern used to address this problem is to save a persistent
50451cb0ef41Sopenharmony_cireference to the class constructor for later `instanceof` checks.
50461cb0ef41Sopenharmony_ci
50471cb0ef41Sopenharmony_ci```c
50481cb0ef41Sopenharmony_cinapi_value MyClass_constructor = NULL;
50491cb0ef41Sopenharmony_cistatus = napi_get_reference_value(env, MyClass::es_constructor, &MyClass_constructor);
50501cb0ef41Sopenharmony_ciassert(napi_ok == status);
50511cb0ef41Sopenharmony_cibool is_instance = false;
50521cb0ef41Sopenharmony_cistatus = napi_instanceof(env, es_this, MyClass_constructor, &is_instance);
50531cb0ef41Sopenharmony_ciassert(napi_ok == status);
50541cb0ef41Sopenharmony_ciif (is_instance) {
50551cb0ef41Sopenharmony_ci  // napi_unwrap() ...
50561cb0ef41Sopenharmony_ci} else {
50571cb0ef41Sopenharmony_ci  // otherwise...
50581cb0ef41Sopenharmony_ci}
50591cb0ef41Sopenharmony_ci```
50601cb0ef41Sopenharmony_ci
50611cb0ef41Sopenharmony_ciThe reference must be freed once it is no longer needed.
50621cb0ef41Sopenharmony_ci
50631cb0ef41Sopenharmony_ciThere are occasions where `napi_instanceof()` is insufficient for ensuring that
50641cb0ef41Sopenharmony_cia JavaScript object is a wrapper for a certain native type. This is the case
50651cb0ef41Sopenharmony_ciespecially when wrapped JavaScript objects are passed back into the addon via
50661cb0ef41Sopenharmony_cistatic methods rather than as the `this` value of prototype methods. In such
50671cb0ef41Sopenharmony_cicases there is a chance that they may be unwrapped incorrectly.
50681cb0ef41Sopenharmony_ci
50691cb0ef41Sopenharmony_ci```js
50701cb0ef41Sopenharmony_ciconst myAddon = require('./build/Release/my_addon.node');
50711cb0ef41Sopenharmony_ci
50721cb0ef41Sopenharmony_ci// `openDatabase()` returns a JavaScript object that wraps a native database
50731cb0ef41Sopenharmony_ci// handle.
50741cb0ef41Sopenharmony_ciconst dbHandle = myAddon.openDatabase();
50751cb0ef41Sopenharmony_ci
50761cb0ef41Sopenharmony_ci// `query()` returns a JavaScript object that wraps a native query handle.
50771cb0ef41Sopenharmony_ciconst queryHandle = myAddon.query(dbHandle, 'Gimme ALL the things!');
50781cb0ef41Sopenharmony_ci
50791cb0ef41Sopenharmony_ci// There is an accidental error in the line below. The first parameter to
50801cb0ef41Sopenharmony_ci// `myAddon.queryHasRecords()` should be the database handle (`dbHandle`), not
50811cb0ef41Sopenharmony_ci// the query handle (`query`), so the correct condition for the while-loop
50821cb0ef41Sopenharmony_ci// should be
50831cb0ef41Sopenharmony_ci//
50841cb0ef41Sopenharmony_ci// myAddon.queryHasRecords(dbHandle, queryHandle)
50851cb0ef41Sopenharmony_ci//
50861cb0ef41Sopenharmony_ciwhile (myAddon.queryHasRecords(queryHandle, dbHandle)) {
50871cb0ef41Sopenharmony_ci  // retrieve records
50881cb0ef41Sopenharmony_ci}
50891cb0ef41Sopenharmony_ci```
50901cb0ef41Sopenharmony_ci
50911cb0ef41Sopenharmony_ciIn the above example `myAddon.queryHasRecords()` is a method that accepts two
50921cb0ef41Sopenharmony_ciarguments. The first is a database handle and the second is a query handle.
50931cb0ef41Sopenharmony_ciInternally, it unwraps the first argument and casts the resulting pointer to a
50941cb0ef41Sopenharmony_cinative database handle. It then unwraps the second argument and casts the
50951cb0ef41Sopenharmony_ciresulting pointer to a query handle. If the arguments are passed in the wrong
50961cb0ef41Sopenharmony_ciorder, the casts will work, however, there is a good chance that the underlying
50971cb0ef41Sopenharmony_cidatabase operation will fail, or will even cause an invalid memory access.
50981cb0ef41Sopenharmony_ci
50991cb0ef41Sopenharmony_ciTo ensure that the pointer retrieved from the first argument is indeed a pointer
51001cb0ef41Sopenharmony_cito a database handle and, similarly, that the pointer retrieved from the second
51011cb0ef41Sopenharmony_ciargument is indeed a pointer to a query handle, the implementation of
51021cb0ef41Sopenharmony_ci`queryHasRecords()` has to perform a type validation. Retaining the JavaScript
51031cb0ef41Sopenharmony_ciclass constructor from which the database handle was instantiated and the
51041cb0ef41Sopenharmony_ciconstructor from which the query handle was instantiated in `napi_ref`s can
51051cb0ef41Sopenharmony_cihelp, because `napi_instanceof()` can then be used to ensure that the instances
51061cb0ef41Sopenharmony_cipassed into `queryHashRecords()` are indeed of the correct type.
51071cb0ef41Sopenharmony_ci
51081cb0ef41Sopenharmony_ciUnfortunately, `napi_instanceof()` does not protect against prototype
51091cb0ef41Sopenharmony_cimanipulation. For example, the prototype of the database handle instance can be
51101cb0ef41Sopenharmony_ciset to the prototype of the constructor for query handle instances. In this
51111cb0ef41Sopenharmony_cicase, the database handle instance can appear as a query handle instance, and it
51121cb0ef41Sopenharmony_ciwill pass the `napi_instanceof()` test for a query handle instance, while still
51131cb0ef41Sopenharmony_cicontaining a pointer to a database handle.
51141cb0ef41Sopenharmony_ci
51151cb0ef41Sopenharmony_ciTo this end, Node-API provides type-tagging capabilities.
51161cb0ef41Sopenharmony_ci
51171cb0ef41Sopenharmony_ciA type tag is a 128-bit integer unique to the addon. Node-API provides the
51181cb0ef41Sopenharmony_ci`napi_type_tag` structure for storing a type tag. When such a value is passed
51191cb0ef41Sopenharmony_cialong with a JavaScript object or [external][] stored in a `napi_value` to
51201cb0ef41Sopenharmony_ci`napi_type_tag_object()`, the JavaScript object will be "marked" with the
51211cb0ef41Sopenharmony_citype tag. The "mark" is invisible on the JavaScript side. When a JavaScript
51221cb0ef41Sopenharmony_ciobject arrives into a native binding, `napi_check_object_type_tag()` can be used
51231cb0ef41Sopenharmony_cialong with the original type tag to determine whether the JavaScript object was
51241cb0ef41Sopenharmony_cipreviously "marked" with the type tag. This creates a type-checking capability
51251cb0ef41Sopenharmony_ciof a higher fidelity than `napi_instanceof()` can provide, because such type-
51261cb0ef41Sopenharmony_citagging survives prototype manipulation and addon unloading/reloading.
51271cb0ef41Sopenharmony_ci
51281cb0ef41Sopenharmony_ciContinuing the above example, the following skeleton addon implementation
51291cb0ef41Sopenharmony_ciillustrates the use of `napi_type_tag_object()` and
51301cb0ef41Sopenharmony_ci`napi_check_object_type_tag()`.
51311cb0ef41Sopenharmony_ci
51321cb0ef41Sopenharmony_ci```c
51331cb0ef41Sopenharmony_ci// This value is the type tag for a database handle. The command
51341cb0ef41Sopenharmony_ci//
51351cb0ef41Sopenharmony_ci//   uuidgen | sed -r -e 's/-//g' -e 's/(.{16})(.*)/0x\1, 0x\2/'
51361cb0ef41Sopenharmony_ci//
51371cb0ef41Sopenharmony_ci// can be used to obtain the two values with which to initialize the structure.
51381cb0ef41Sopenharmony_cistatic const napi_type_tag DatabaseHandleTypeTag = {
51391cb0ef41Sopenharmony_ci  0x1edf75a38336451d, 0xa5ed9ce2e4c00c38
51401cb0ef41Sopenharmony_ci};
51411cb0ef41Sopenharmony_ci
51421cb0ef41Sopenharmony_ci// This value is the type tag for a query handle.
51431cb0ef41Sopenharmony_cistatic const napi_type_tag QueryHandleTypeTag = {
51441cb0ef41Sopenharmony_ci  0x9c73317f9fad44a3, 0x93c3920bf3b0ad6a
51451cb0ef41Sopenharmony_ci};
51461cb0ef41Sopenharmony_ci
51471cb0ef41Sopenharmony_cistatic napi_value
51481cb0ef41Sopenharmony_ciopenDatabase(napi_env env, napi_callback_info info) {
51491cb0ef41Sopenharmony_ci  napi_status status;
51501cb0ef41Sopenharmony_ci  napi_value result;
51511cb0ef41Sopenharmony_ci
51521cb0ef41Sopenharmony_ci  // Perform the underlying action which results in a database handle.
51531cb0ef41Sopenharmony_ci  DatabaseHandle* dbHandle = open_database();
51541cb0ef41Sopenharmony_ci
51551cb0ef41Sopenharmony_ci  // Create a new, empty JS object.
51561cb0ef41Sopenharmony_ci  status = napi_create_object(env, &result);
51571cb0ef41Sopenharmony_ci  if (status != napi_ok) return NULL;
51581cb0ef41Sopenharmony_ci
51591cb0ef41Sopenharmony_ci  // Tag the object to indicate that it holds a pointer to a `DatabaseHandle`.
51601cb0ef41Sopenharmony_ci  status = napi_type_tag_object(env, result, &DatabaseHandleTypeTag);
51611cb0ef41Sopenharmony_ci  if (status != napi_ok) return NULL;
51621cb0ef41Sopenharmony_ci
51631cb0ef41Sopenharmony_ci  // Store the pointer to the `DatabaseHandle` structure inside the JS object.
51641cb0ef41Sopenharmony_ci  status = napi_wrap(env, result, dbHandle, NULL, NULL, NULL);
51651cb0ef41Sopenharmony_ci  if (status != napi_ok) return NULL;
51661cb0ef41Sopenharmony_ci
51671cb0ef41Sopenharmony_ci  return result;
51681cb0ef41Sopenharmony_ci}
51691cb0ef41Sopenharmony_ci
51701cb0ef41Sopenharmony_ci// Later when we receive a JavaScript object purporting to be a database handle
51711cb0ef41Sopenharmony_ci// we can use `napi_check_object_type_tag()` to ensure that it is indeed such a
51721cb0ef41Sopenharmony_ci// handle.
51731cb0ef41Sopenharmony_ci
51741cb0ef41Sopenharmony_cistatic napi_value
51751cb0ef41Sopenharmony_ciquery(napi_env env, napi_callback_info info) {
51761cb0ef41Sopenharmony_ci  napi_status status;
51771cb0ef41Sopenharmony_ci  size_t argc = 2;
51781cb0ef41Sopenharmony_ci  napi_value argv[2];
51791cb0ef41Sopenharmony_ci  bool is_db_handle;
51801cb0ef41Sopenharmony_ci
51811cb0ef41Sopenharmony_ci  status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
51821cb0ef41Sopenharmony_ci  if (status != napi_ok) return NULL;
51831cb0ef41Sopenharmony_ci
51841cb0ef41Sopenharmony_ci  // Check that the object passed as the first parameter has the previously
51851cb0ef41Sopenharmony_ci  // applied tag.
51861cb0ef41Sopenharmony_ci  status = napi_check_object_type_tag(env,
51871cb0ef41Sopenharmony_ci                                      argv[0],
51881cb0ef41Sopenharmony_ci                                      &DatabaseHandleTypeTag,
51891cb0ef41Sopenharmony_ci                                      &is_db_handle);
51901cb0ef41Sopenharmony_ci  if (status != napi_ok) return NULL;
51911cb0ef41Sopenharmony_ci
51921cb0ef41Sopenharmony_ci  // Throw a `TypeError` if it doesn't.
51931cb0ef41Sopenharmony_ci  if (!is_db_handle) {
51941cb0ef41Sopenharmony_ci    // Throw a TypeError.
51951cb0ef41Sopenharmony_ci    return NULL;
51961cb0ef41Sopenharmony_ci  }
51971cb0ef41Sopenharmony_ci}
51981cb0ef41Sopenharmony_ci```
51991cb0ef41Sopenharmony_ci
52001cb0ef41Sopenharmony_ci### `napi_define_class`
52011cb0ef41Sopenharmony_ci
52021cb0ef41Sopenharmony_ci<!-- YAML
52031cb0ef41Sopenharmony_ciadded: v8.0.0
52041cb0ef41Sopenharmony_cinapiVersion: 1
52051cb0ef41Sopenharmony_ci-->
52061cb0ef41Sopenharmony_ci
52071cb0ef41Sopenharmony_ci```c
52081cb0ef41Sopenharmony_cinapi_status napi_define_class(napi_env env,
52091cb0ef41Sopenharmony_ci                              const char* utf8name,
52101cb0ef41Sopenharmony_ci                              size_t length,
52111cb0ef41Sopenharmony_ci                              napi_callback constructor,
52121cb0ef41Sopenharmony_ci                              void* data,
52131cb0ef41Sopenharmony_ci                              size_t property_count,
52141cb0ef41Sopenharmony_ci                              const napi_property_descriptor* properties,
52151cb0ef41Sopenharmony_ci                              napi_value* result);
52161cb0ef41Sopenharmony_ci```
52171cb0ef41Sopenharmony_ci
52181cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
52191cb0ef41Sopenharmony_ci* `[in] utf8name`: Name of the JavaScript constructor function. For clarity,
52201cb0ef41Sopenharmony_ci  it is recommended to use the C++ class name when wrapping a C++ class.
52211cb0ef41Sopenharmony_ci* `[in] length`: The length of the `utf8name` in bytes, or `NAPI_AUTO_LENGTH`
52221cb0ef41Sopenharmony_ci  if it is null-terminated.
52231cb0ef41Sopenharmony_ci* `[in] constructor`: Callback function that handles constructing instances
52241cb0ef41Sopenharmony_ci  of the class. When wrapping a C++ class, this method must be a static member
52251cb0ef41Sopenharmony_ci  with the [`napi_callback`][] signature. A C++ class constructor cannot be
52261cb0ef41Sopenharmony_ci  used. [`napi_callback`][] provides more details.
52271cb0ef41Sopenharmony_ci* `[in] data`: Optional data to be passed to the constructor callback as
52281cb0ef41Sopenharmony_ci  the `data` property of the callback info.
52291cb0ef41Sopenharmony_ci* `[in] property_count`: Number of items in the `properties` array argument.
52301cb0ef41Sopenharmony_ci* `[in] properties`: Array of property descriptors describing static and
52311cb0ef41Sopenharmony_ci  instance data properties, accessors, and methods on the class
52321cb0ef41Sopenharmony_ci  See `napi_property_descriptor`.
52331cb0ef41Sopenharmony_ci* `[out] result`: A `napi_value` representing the constructor function for
52341cb0ef41Sopenharmony_ci  the class.
52351cb0ef41Sopenharmony_ci
52361cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
52371cb0ef41Sopenharmony_ci
52381cb0ef41Sopenharmony_ciDefines a JavaScript class, including:
52391cb0ef41Sopenharmony_ci
52401cb0ef41Sopenharmony_ci* A JavaScript constructor function that has the class name. When wrapping a
52411cb0ef41Sopenharmony_ci  corresponding C++ class, the callback passed via `constructor` can be used to
52421cb0ef41Sopenharmony_ci  instantiate a new C++ class instance, which can then be placed inside the
52431cb0ef41Sopenharmony_ci  JavaScript object instance being constructed using [`napi_wrap`][].
52441cb0ef41Sopenharmony_ci* Properties on the constructor function whose implementation can call
52451cb0ef41Sopenharmony_ci  corresponding _static_ data properties, accessors, and methods of the C++
52461cb0ef41Sopenharmony_ci  class (defined by property descriptors with the `napi_static` attribute).
52471cb0ef41Sopenharmony_ci* Properties on the constructor function's `prototype` object. When wrapping a
52481cb0ef41Sopenharmony_ci  C++ class, _non-static_ data properties, accessors, and methods of the C++
52491cb0ef41Sopenharmony_ci  class can be called from the static functions given in the property
52501cb0ef41Sopenharmony_ci  descriptors without the `napi_static` attribute after retrieving the C++ class
52511cb0ef41Sopenharmony_ci  instance placed inside the JavaScript object instance by using
52521cb0ef41Sopenharmony_ci  [`napi_unwrap`][].
52531cb0ef41Sopenharmony_ci
52541cb0ef41Sopenharmony_ciWhen wrapping a C++ class, the C++ constructor callback passed via `constructor`
52551cb0ef41Sopenharmony_cishould be a static method on the class that calls the actual class constructor,
52561cb0ef41Sopenharmony_cithen wraps the new C++ instance in a JavaScript object, and returns the wrapper
52571cb0ef41Sopenharmony_ciobject. See [`napi_wrap`][] for details.
52581cb0ef41Sopenharmony_ci
52591cb0ef41Sopenharmony_ciThe JavaScript constructor function returned from [`napi_define_class`][] is
52601cb0ef41Sopenharmony_cioften saved and used later to construct new instances of the class from native
52611cb0ef41Sopenharmony_cicode, and/or to check whether provided values are instances of the class. In
52621cb0ef41Sopenharmony_cithat case, to prevent the function value from being garbage-collected, a
52631cb0ef41Sopenharmony_cistrong persistent reference to it can be created using
52641cb0ef41Sopenharmony_ci[`napi_create_reference`][], ensuring that the reference count is kept >= 1.
52651cb0ef41Sopenharmony_ci
52661cb0ef41Sopenharmony_ciAny non-`NULL` data which is passed to this API via the `data` parameter or via
52671cb0ef41Sopenharmony_cithe `data` field of the `napi_property_descriptor` array items can be associated
52681cb0ef41Sopenharmony_ciwith the resulting JavaScript constructor (which is returned in the `result`
52691cb0ef41Sopenharmony_ciparameter) and freed whenever the class is garbage-collected by passing both
52701cb0ef41Sopenharmony_cithe JavaScript function and the data to [`napi_add_finalizer`][].
52711cb0ef41Sopenharmony_ci
52721cb0ef41Sopenharmony_ci### `napi_wrap`
52731cb0ef41Sopenharmony_ci
52741cb0ef41Sopenharmony_ci<!-- YAML
52751cb0ef41Sopenharmony_ciadded: v8.0.0
52761cb0ef41Sopenharmony_cinapiVersion: 1
52771cb0ef41Sopenharmony_ci-->
52781cb0ef41Sopenharmony_ci
52791cb0ef41Sopenharmony_ci```c
52801cb0ef41Sopenharmony_cinapi_status napi_wrap(napi_env env,
52811cb0ef41Sopenharmony_ci                      napi_value js_object,
52821cb0ef41Sopenharmony_ci                      void* native_object,
52831cb0ef41Sopenharmony_ci                      napi_finalize finalize_cb,
52841cb0ef41Sopenharmony_ci                      void* finalize_hint,
52851cb0ef41Sopenharmony_ci                      napi_ref* result);
52861cb0ef41Sopenharmony_ci```
52871cb0ef41Sopenharmony_ci
52881cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
52891cb0ef41Sopenharmony_ci* `[in] js_object`: The JavaScript object that will be the wrapper for the
52901cb0ef41Sopenharmony_ci  native object.
52911cb0ef41Sopenharmony_ci* `[in] native_object`: The native instance that will be wrapped in the
52921cb0ef41Sopenharmony_ci  JavaScript object.
52931cb0ef41Sopenharmony_ci* `[in] finalize_cb`: Optional native callback that can be used to free the
52941cb0ef41Sopenharmony_ci  native instance when the JavaScript object has been garbage-collected.
52951cb0ef41Sopenharmony_ci  [`napi_finalize`][] provides more details.
52961cb0ef41Sopenharmony_ci* `[in] finalize_hint`: Optional contextual hint that is passed to the
52971cb0ef41Sopenharmony_ci  finalize callback.
52981cb0ef41Sopenharmony_ci* `[out] result`: Optional reference to the wrapped object.
52991cb0ef41Sopenharmony_ci
53001cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
53011cb0ef41Sopenharmony_ci
53021cb0ef41Sopenharmony_ciWraps a native instance in a JavaScript object. The native instance can be
53031cb0ef41Sopenharmony_ciretrieved later using `napi_unwrap()`.
53041cb0ef41Sopenharmony_ci
53051cb0ef41Sopenharmony_ciWhen JavaScript code invokes a constructor for a class that was defined using
53061cb0ef41Sopenharmony_ci`napi_define_class()`, the `napi_callback` for the constructor is invoked.
53071cb0ef41Sopenharmony_ciAfter constructing an instance of the native class, the callback must then call
53081cb0ef41Sopenharmony_ci`napi_wrap()` to wrap the newly constructed instance in the already-created
53091cb0ef41Sopenharmony_ciJavaScript object that is the `this` argument to the constructor callback.
53101cb0ef41Sopenharmony_ci(That `this` object was created from the constructor function's `prototype`,
53111cb0ef41Sopenharmony_ciso it already has definitions of all the instance properties and methods.)
53121cb0ef41Sopenharmony_ci
53131cb0ef41Sopenharmony_ciTypically when wrapping a class instance, a finalize callback should be
53141cb0ef41Sopenharmony_ciprovided that simply deletes the native instance that is received as the `data`
53151cb0ef41Sopenharmony_ciargument to the finalize callback.
53161cb0ef41Sopenharmony_ci
53171cb0ef41Sopenharmony_ciThe optional returned reference is initially a weak reference, meaning it
53181cb0ef41Sopenharmony_cihas a reference count of 0. Typically this reference count would be incremented
53191cb0ef41Sopenharmony_citemporarily during async operations that require the instance to remain valid.
53201cb0ef41Sopenharmony_ci
53211cb0ef41Sopenharmony_ci_Caution_: The optional returned reference (if obtained) should be deleted via
53221cb0ef41Sopenharmony_ci[`napi_delete_reference`][] ONLY in response to the finalize callback
53231cb0ef41Sopenharmony_ciinvocation. If it is deleted before then, then the finalize callback may never
53241cb0ef41Sopenharmony_cibe invoked. Therefore, when obtaining a reference a finalize callback is also
53251cb0ef41Sopenharmony_cirequired in order to enable correct disposal of the reference.
53261cb0ef41Sopenharmony_ci
53271cb0ef41Sopenharmony_ciFinalizer callbacks may be deferred, leaving a window where the object has
53281cb0ef41Sopenharmony_cibeen garbage collected (and the weak reference is invalid) but the finalizer
53291cb0ef41Sopenharmony_cihasn't been called yet. When using `napi_get_reference_value()` on weak
53301cb0ef41Sopenharmony_cireferences returned by `napi_wrap()`, you should still handle an empty result.
53311cb0ef41Sopenharmony_ci
53321cb0ef41Sopenharmony_ciCalling `napi_wrap()` a second time on an object will return an error. To
53331cb0ef41Sopenharmony_ciassociate another native instance with the object, use `napi_remove_wrap()`
53341cb0ef41Sopenharmony_cifirst.
53351cb0ef41Sopenharmony_ci
53361cb0ef41Sopenharmony_ci### `napi_unwrap`
53371cb0ef41Sopenharmony_ci
53381cb0ef41Sopenharmony_ci<!-- YAML
53391cb0ef41Sopenharmony_ciadded: v8.0.0
53401cb0ef41Sopenharmony_cinapiVersion: 1
53411cb0ef41Sopenharmony_ci-->
53421cb0ef41Sopenharmony_ci
53431cb0ef41Sopenharmony_ci```c
53441cb0ef41Sopenharmony_cinapi_status napi_unwrap(napi_env env,
53451cb0ef41Sopenharmony_ci                        napi_value js_object,
53461cb0ef41Sopenharmony_ci                        void** result);
53471cb0ef41Sopenharmony_ci```
53481cb0ef41Sopenharmony_ci
53491cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
53501cb0ef41Sopenharmony_ci* `[in] js_object`: The object associated with the native instance.
53511cb0ef41Sopenharmony_ci* `[out] result`: Pointer to the wrapped native instance.
53521cb0ef41Sopenharmony_ci
53531cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
53541cb0ef41Sopenharmony_ci
53551cb0ef41Sopenharmony_ciRetrieves a native instance that was previously wrapped in a JavaScript
53561cb0ef41Sopenharmony_ciobject using `napi_wrap()`.
53571cb0ef41Sopenharmony_ci
53581cb0ef41Sopenharmony_ciWhen JavaScript code invokes a method or property accessor on the class, the
53591cb0ef41Sopenharmony_cicorresponding `napi_callback` is invoked. If the callback is for an instance
53601cb0ef41Sopenharmony_cimethod or accessor, then the `this` argument to the callback is the wrapper
53611cb0ef41Sopenharmony_ciobject; the wrapped C++ instance that is the target of the call can be obtained
53621cb0ef41Sopenharmony_cithen by calling `napi_unwrap()` on the wrapper object.
53631cb0ef41Sopenharmony_ci
53641cb0ef41Sopenharmony_ci### `napi_remove_wrap`
53651cb0ef41Sopenharmony_ci
53661cb0ef41Sopenharmony_ci<!-- YAML
53671cb0ef41Sopenharmony_ciadded: v8.5.0
53681cb0ef41Sopenharmony_cinapiVersion: 1
53691cb0ef41Sopenharmony_ci-->
53701cb0ef41Sopenharmony_ci
53711cb0ef41Sopenharmony_ci```c
53721cb0ef41Sopenharmony_cinapi_status napi_remove_wrap(napi_env env,
53731cb0ef41Sopenharmony_ci                             napi_value js_object,
53741cb0ef41Sopenharmony_ci                             void** result);
53751cb0ef41Sopenharmony_ci```
53761cb0ef41Sopenharmony_ci
53771cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
53781cb0ef41Sopenharmony_ci* `[in] js_object`: The object associated with the native instance.
53791cb0ef41Sopenharmony_ci* `[out] result`: Pointer to the wrapped native instance.
53801cb0ef41Sopenharmony_ci
53811cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
53821cb0ef41Sopenharmony_ci
53831cb0ef41Sopenharmony_ciRetrieves a native instance that was previously wrapped in the JavaScript
53841cb0ef41Sopenharmony_ciobject `js_object` using `napi_wrap()` and removes the wrapping. If a finalize
53851cb0ef41Sopenharmony_cicallback was associated with the wrapping, it will no longer be called when the
53861cb0ef41Sopenharmony_ciJavaScript object becomes garbage-collected.
53871cb0ef41Sopenharmony_ci
53881cb0ef41Sopenharmony_ci### `napi_type_tag_object`
53891cb0ef41Sopenharmony_ci
53901cb0ef41Sopenharmony_ci<!-- YAML
53911cb0ef41Sopenharmony_ciadded:
53921cb0ef41Sopenharmony_ci  - v14.8.0
53931cb0ef41Sopenharmony_ci  - v12.19.0
53941cb0ef41Sopenharmony_cinapiVersion: 8
53951cb0ef41Sopenharmony_ci-->
53961cb0ef41Sopenharmony_ci
53971cb0ef41Sopenharmony_ci```c
53981cb0ef41Sopenharmony_cinapi_status napi_type_tag_object(napi_env env,
53991cb0ef41Sopenharmony_ci                                 napi_value js_object,
54001cb0ef41Sopenharmony_ci                                 const napi_type_tag* type_tag);
54011cb0ef41Sopenharmony_ci```
54021cb0ef41Sopenharmony_ci
54031cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
54041cb0ef41Sopenharmony_ci* `[in] js_object`: The JavaScript object or [external][] to be marked.
54051cb0ef41Sopenharmony_ci* `[in] type_tag`: The tag with which the object is to be marked.
54061cb0ef41Sopenharmony_ci
54071cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
54081cb0ef41Sopenharmony_ci
54091cb0ef41Sopenharmony_ciAssociates the value of the `type_tag` pointer with the JavaScript object or
54101cb0ef41Sopenharmony_ci[external][]. `napi_check_object_type_tag()` can then be used to compare the tag
54111cb0ef41Sopenharmony_cithat was attached to the object with one owned by the addon to ensure that the
54121cb0ef41Sopenharmony_ciobject has the right type.
54131cb0ef41Sopenharmony_ci
54141cb0ef41Sopenharmony_ciIf the object already has an associated type tag, this API will return
54151cb0ef41Sopenharmony_ci`napi_invalid_arg`.
54161cb0ef41Sopenharmony_ci
54171cb0ef41Sopenharmony_ci### `napi_check_object_type_tag`
54181cb0ef41Sopenharmony_ci
54191cb0ef41Sopenharmony_ci<!-- YAML
54201cb0ef41Sopenharmony_ciadded:
54211cb0ef41Sopenharmony_ci  - v14.8.0
54221cb0ef41Sopenharmony_ci  - v12.19.0
54231cb0ef41Sopenharmony_cinapiVersion: 8
54241cb0ef41Sopenharmony_ci-->
54251cb0ef41Sopenharmony_ci
54261cb0ef41Sopenharmony_ci```c
54271cb0ef41Sopenharmony_cinapi_status napi_check_object_type_tag(napi_env env,
54281cb0ef41Sopenharmony_ci                                       napi_value js_object,
54291cb0ef41Sopenharmony_ci                                       const napi_type_tag* type_tag,
54301cb0ef41Sopenharmony_ci                                       bool* result);
54311cb0ef41Sopenharmony_ci```
54321cb0ef41Sopenharmony_ci
54331cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
54341cb0ef41Sopenharmony_ci* `[in] js_object`: The JavaScript object or [external][] whose type tag to
54351cb0ef41Sopenharmony_ci  examine.
54361cb0ef41Sopenharmony_ci* `[in] type_tag`: The tag with which to compare any tag found on the object.
54371cb0ef41Sopenharmony_ci* `[out] result`: Whether the type tag given matched the type tag on the
54381cb0ef41Sopenharmony_ci  object. `false` is also returned if no type tag was found on the object.
54391cb0ef41Sopenharmony_ci
54401cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
54411cb0ef41Sopenharmony_ci
54421cb0ef41Sopenharmony_ciCompares the pointer given as `type_tag` with any that can be found on
54431cb0ef41Sopenharmony_ci`js_object`. If no tag is found on `js_object` or, if a tag is found but it does
54441cb0ef41Sopenharmony_cinot match `type_tag`, then `result` is set to `false`. If a tag is found and it
54451cb0ef41Sopenharmony_cimatches `type_tag`, then `result` is set to `true`.
54461cb0ef41Sopenharmony_ci
54471cb0ef41Sopenharmony_ci### `napi_add_finalizer`
54481cb0ef41Sopenharmony_ci
54491cb0ef41Sopenharmony_ci<!-- YAML
54501cb0ef41Sopenharmony_ciadded: v8.0.0
54511cb0ef41Sopenharmony_cinapiVersion: 5
54521cb0ef41Sopenharmony_ci-->
54531cb0ef41Sopenharmony_ci
54541cb0ef41Sopenharmony_ci```c
54551cb0ef41Sopenharmony_cinapi_status napi_add_finalizer(napi_env env,
54561cb0ef41Sopenharmony_ci                               napi_value js_object,
54571cb0ef41Sopenharmony_ci                               void* finalize_data,
54581cb0ef41Sopenharmony_ci                               node_api_nogc_finalize finalize_cb,
54591cb0ef41Sopenharmony_ci                               void* finalize_hint,
54601cb0ef41Sopenharmony_ci                               napi_ref* result);
54611cb0ef41Sopenharmony_ci```
54621cb0ef41Sopenharmony_ci
54631cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
54641cb0ef41Sopenharmony_ci* `[in] js_object`: The JavaScript object to which the native data will be
54651cb0ef41Sopenharmony_ci  attached.
54661cb0ef41Sopenharmony_ci* `[in] finalize_data`: Optional data to be passed to `finalize_cb`.
54671cb0ef41Sopenharmony_ci* `[in] finalize_cb`: Native callback that will be used to free the
54681cb0ef41Sopenharmony_ci  native data when the JavaScript object has been garbage-collected.
54691cb0ef41Sopenharmony_ci  [`napi_finalize`][] provides more details.
54701cb0ef41Sopenharmony_ci* `[in] finalize_hint`: Optional contextual hint that is passed to the
54711cb0ef41Sopenharmony_ci  finalize callback.
54721cb0ef41Sopenharmony_ci* `[out] result`: Optional reference to the JavaScript object.
54731cb0ef41Sopenharmony_ci
54741cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
54751cb0ef41Sopenharmony_ci
54761cb0ef41Sopenharmony_ciAdds a `napi_finalize` callback which will be called when the JavaScript object
54771cb0ef41Sopenharmony_ciin `js_object` has been garbage-collected.
54781cb0ef41Sopenharmony_ci
54791cb0ef41Sopenharmony_ciThis API can be called multiple times on a single JavaScript object.
54801cb0ef41Sopenharmony_ci
54811cb0ef41Sopenharmony_ci_Caution_: The optional returned reference (if obtained) should be deleted via
54821cb0ef41Sopenharmony_ci[`napi_delete_reference`][] ONLY in response to the finalize callback
54831cb0ef41Sopenharmony_ciinvocation. If it is deleted before then, then the finalize callback may never
54841cb0ef41Sopenharmony_cibe invoked. Therefore, when obtaining a reference a finalize callback is also
54851cb0ef41Sopenharmony_cirequired in order to enable correct disposal of the reference.
54861cb0ef41Sopenharmony_ci
54871cb0ef41Sopenharmony_ci#### `node_api_post_finalizer`
54881cb0ef41Sopenharmony_ci
54891cb0ef41Sopenharmony_ci<!-- YAML
54901cb0ef41Sopenharmony_ciadded: v18.19.0
54911cb0ef41Sopenharmony_ci-->
54921cb0ef41Sopenharmony_ci
54931cb0ef41Sopenharmony_ci> Stability: 1 - Experimental
54941cb0ef41Sopenharmony_ci
54951cb0ef41Sopenharmony_ci```c
54961cb0ef41Sopenharmony_cinapi_status node_api_post_finalizer(node_api_nogc_env env,
54971cb0ef41Sopenharmony_ci                                    napi_finalize finalize_cb,
54981cb0ef41Sopenharmony_ci                                    void* finalize_data,
54991cb0ef41Sopenharmony_ci                                    void* finalize_hint);
55001cb0ef41Sopenharmony_ci```
55011cb0ef41Sopenharmony_ci
55021cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
55031cb0ef41Sopenharmony_ci* `[in] finalize_cb`: Native callback that will be used to free the
55041cb0ef41Sopenharmony_ci  native data when the JavaScript object has been garbage-collected.
55051cb0ef41Sopenharmony_ci  [`napi_finalize`][] provides more details.
55061cb0ef41Sopenharmony_ci* `[in] finalize_data`: Optional data to be passed to `finalize_cb`.
55071cb0ef41Sopenharmony_ci* `[in] finalize_hint`: Optional contextual hint that is passed to the
55081cb0ef41Sopenharmony_ci  finalize callback.
55091cb0ef41Sopenharmony_ci
55101cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
55111cb0ef41Sopenharmony_ci
55121cb0ef41Sopenharmony_ciSchedules a `napi_finalize` callback to be called asynchronously in the
55131cb0ef41Sopenharmony_cievent loop.
55141cb0ef41Sopenharmony_ci
55151cb0ef41Sopenharmony_ciNormally, finalizers are called while the GC (garbage collector) collects
55161cb0ef41Sopenharmony_ciobjects. At that point calling any Node-API that may cause changes in the GC
55171cb0ef41Sopenharmony_cistate will be disabled and will crash Node.js.
55181cb0ef41Sopenharmony_ci
55191cb0ef41Sopenharmony_ci`node_api_post_finalizer` helps to work around this limitation by allowing the
55201cb0ef41Sopenharmony_ciadd-on to defer calls to such Node-APIs to a point in time outside of the GC
55211cb0ef41Sopenharmony_cifinalization.
55221cb0ef41Sopenharmony_ci
55231cb0ef41Sopenharmony_ci## Simple asynchronous operations
55241cb0ef41Sopenharmony_ci
55251cb0ef41Sopenharmony_ciAddon modules often need to leverage async helpers from libuv as part of their
55261cb0ef41Sopenharmony_ciimplementation. This allows them to schedule work to be executed asynchronously
55271cb0ef41Sopenharmony_ciso that their methods can return in advance of the work being completed. This
55281cb0ef41Sopenharmony_ciallows them to avoid blocking overall execution of the Node.js application.
55291cb0ef41Sopenharmony_ci
55301cb0ef41Sopenharmony_ciNode-API provides an ABI-stable interface for these
55311cb0ef41Sopenharmony_cisupporting functions which covers the most common asynchronous use cases.
55321cb0ef41Sopenharmony_ci
55331cb0ef41Sopenharmony_ciNode-API defines the `napi_async_work` structure which is used to manage
55341cb0ef41Sopenharmony_ciasynchronous workers. Instances are created/deleted with
55351cb0ef41Sopenharmony_ci[`napi_create_async_work`][] and [`napi_delete_async_work`][].
55361cb0ef41Sopenharmony_ci
55371cb0ef41Sopenharmony_ciThe `execute` and `complete` callbacks are functions that will be
55381cb0ef41Sopenharmony_ciinvoked when the executor is ready to execute and when it completes its
55391cb0ef41Sopenharmony_citask respectively.
55401cb0ef41Sopenharmony_ci
55411cb0ef41Sopenharmony_ciThe `execute` function should avoid making any Node-API calls
55421cb0ef41Sopenharmony_cithat could result in the execution of JavaScript or interaction with
55431cb0ef41Sopenharmony_ciJavaScript objects. Most often, any code that needs to make Node-API
55441cb0ef41Sopenharmony_cicalls should be made in `complete` callback instead.
55451cb0ef41Sopenharmony_ciAvoid using the `napi_env` parameter in the execute callback as
55461cb0ef41Sopenharmony_ciit will likely execute JavaScript.
55471cb0ef41Sopenharmony_ci
55481cb0ef41Sopenharmony_ciThese functions implement the following interfaces:
55491cb0ef41Sopenharmony_ci
55501cb0ef41Sopenharmony_ci```c
55511cb0ef41Sopenharmony_citypedef void (*napi_async_execute_callback)(napi_env env,
55521cb0ef41Sopenharmony_ci                                            void* data);
55531cb0ef41Sopenharmony_citypedef void (*napi_async_complete_callback)(napi_env env,
55541cb0ef41Sopenharmony_ci                                             napi_status status,
55551cb0ef41Sopenharmony_ci                                             void* data);
55561cb0ef41Sopenharmony_ci```
55571cb0ef41Sopenharmony_ci
55581cb0ef41Sopenharmony_ciWhen these methods are invoked, the `data` parameter passed will be the
55591cb0ef41Sopenharmony_ciaddon-provided `void*` data that was passed into the
55601cb0ef41Sopenharmony_ci`napi_create_async_work` call.
55611cb0ef41Sopenharmony_ci
55621cb0ef41Sopenharmony_ciOnce created the async worker can be queued
55631cb0ef41Sopenharmony_cifor execution using the [`napi_queue_async_work`][] function:
55641cb0ef41Sopenharmony_ci
55651cb0ef41Sopenharmony_ci```c
55661cb0ef41Sopenharmony_cinapi_status napi_queue_async_work(node_api_nogc_env env,
55671cb0ef41Sopenharmony_ci                                  napi_async_work work);
55681cb0ef41Sopenharmony_ci```
55691cb0ef41Sopenharmony_ci
55701cb0ef41Sopenharmony_ci[`napi_cancel_async_work`][] can be used if the work needs
55711cb0ef41Sopenharmony_cito be cancelled before the work has started execution.
55721cb0ef41Sopenharmony_ci
55731cb0ef41Sopenharmony_ciAfter calling [`napi_cancel_async_work`][], the `complete` callback
55741cb0ef41Sopenharmony_ciwill be invoked with a status value of `napi_cancelled`.
55751cb0ef41Sopenharmony_ciThe work should not be deleted before the `complete`
55761cb0ef41Sopenharmony_cicallback invocation, even when it was cancelled.
55771cb0ef41Sopenharmony_ci
55781cb0ef41Sopenharmony_ci### `napi_create_async_work`
55791cb0ef41Sopenharmony_ci
55801cb0ef41Sopenharmony_ci<!-- YAML
55811cb0ef41Sopenharmony_ciadded: v8.0.0
55821cb0ef41Sopenharmony_cinapiVersion: 1
55831cb0ef41Sopenharmony_cichanges:
55841cb0ef41Sopenharmony_ci  - version: v8.6.0
55851cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/14697
55861cb0ef41Sopenharmony_ci    description: Added `async_resource` and `async_resource_name` parameters.
55871cb0ef41Sopenharmony_ci-->
55881cb0ef41Sopenharmony_ci
55891cb0ef41Sopenharmony_ci```c
55901cb0ef41Sopenharmony_cinapi_status napi_create_async_work(napi_env env,
55911cb0ef41Sopenharmony_ci                                   napi_value async_resource,
55921cb0ef41Sopenharmony_ci                                   napi_value async_resource_name,
55931cb0ef41Sopenharmony_ci                                   napi_async_execute_callback execute,
55941cb0ef41Sopenharmony_ci                                   napi_async_complete_callback complete,
55951cb0ef41Sopenharmony_ci                                   void* data,
55961cb0ef41Sopenharmony_ci                                   napi_async_work* result);
55971cb0ef41Sopenharmony_ci```
55981cb0ef41Sopenharmony_ci
55991cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
56001cb0ef41Sopenharmony_ci* `[in] async_resource`: An optional object associated with the async work
56011cb0ef41Sopenharmony_ci  that will be passed to possible `async_hooks` [`init` hooks][].
56021cb0ef41Sopenharmony_ci* `[in] async_resource_name`: Identifier for the kind of resource that is being
56031cb0ef41Sopenharmony_ci  provided for diagnostic information exposed by the `async_hooks` API.
56041cb0ef41Sopenharmony_ci* `[in] execute`: The native function which should be called to execute the
56051cb0ef41Sopenharmony_ci  logic asynchronously. The given function is called from a worker pool thread
56061cb0ef41Sopenharmony_ci  and can execute in parallel with the main event loop thread.
56071cb0ef41Sopenharmony_ci* `[in] complete`: The native function which will be called when the
56081cb0ef41Sopenharmony_ci  asynchronous logic is completed or is cancelled. The given function is called
56091cb0ef41Sopenharmony_ci  from the main event loop thread. [`napi_async_complete_callback`][] provides
56101cb0ef41Sopenharmony_ci  more details.
56111cb0ef41Sopenharmony_ci* `[in] data`: User-provided data context. This will be passed back into the
56121cb0ef41Sopenharmony_ci  execute and complete functions.
56131cb0ef41Sopenharmony_ci* `[out] result`: `napi_async_work*` which is the handle to the newly created
56141cb0ef41Sopenharmony_ci  async work.
56151cb0ef41Sopenharmony_ci
56161cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
56171cb0ef41Sopenharmony_ci
56181cb0ef41Sopenharmony_ciThis API allocates a work object that is used to execute logic asynchronously.
56191cb0ef41Sopenharmony_ciIt should be freed using [`napi_delete_async_work`][] once the work is no longer
56201cb0ef41Sopenharmony_cirequired.
56211cb0ef41Sopenharmony_ci
56221cb0ef41Sopenharmony_ci`async_resource_name` should be a null-terminated, UTF-8-encoded string.
56231cb0ef41Sopenharmony_ci
56241cb0ef41Sopenharmony_ciThe `async_resource_name` identifier is provided by the user and should be
56251cb0ef41Sopenharmony_cirepresentative of the type of async work being performed. It is also recommended
56261cb0ef41Sopenharmony_cito apply namespacing to the identifier, e.g. by including the module name. See
56271cb0ef41Sopenharmony_cithe [`async_hooks` documentation][async_hooks `type`] for more information.
56281cb0ef41Sopenharmony_ci
56291cb0ef41Sopenharmony_ci### `napi_delete_async_work`
56301cb0ef41Sopenharmony_ci
56311cb0ef41Sopenharmony_ci<!-- YAML
56321cb0ef41Sopenharmony_ciadded: v8.0.0
56331cb0ef41Sopenharmony_cinapiVersion: 1
56341cb0ef41Sopenharmony_ci-->
56351cb0ef41Sopenharmony_ci
56361cb0ef41Sopenharmony_ci```c
56371cb0ef41Sopenharmony_cinapi_status napi_delete_async_work(napi_env env,
56381cb0ef41Sopenharmony_ci                                   napi_async_work work);
56391cb0ef41Sopenharmony_ci```
56401cb0ef41Sopenharmony_ci
56411cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
56421cb0ef41Sopenharmony_ci* `[in] work`: The handle returned by the call to `napi_create_async_work`.
56431cb0ef41Sopenharmony_ci
56441cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
56451cb0ef41Sopenharmony_ci
56461cb0ef41Sopenharmony_ciThis API frees a previously allocated work object.
56471cb0ef41Sopenharmony_ci
56481cb0ef41Sopenharmony_ciThis API can be called even if there is a pending JavaScript exception.
56491cb0ef41Sopenharmony_ci
56501cb0ef41Sopenharmony_ci### `napi_queue_async_work`
56511cb0ef41Sopenharmony_ci
56521cb0ef41Sopenharmony_ci<!-- YAML
56531cb0ef41Sopenharmony_ciadded: v8.0.0
56541cb0ef41Sopenharmony_cinapiVersion: 1
56551cb0ef41Sopenharmony_ci-->
56561cb0ef41Sopenharmony_ci
56571cb0ef41Sopenharmony_ci```c
56581cb0ef41Sopenharmony_cinapi_status napi_queue_async_work(node_api_nogc_env env,
56591cb0ef41Sopenharmony_ci                                  napi_async_work work);
56601cb0ef41Sopenharmony_ci```
56611cb0ef41Sopenharmony_ci
56621cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
56631cb0ef41Sopenharmony_ci* `[in] work`: The handle returned by the call to `napi_create_async_work`.
56641cb0ef41Sopenharmony_ci
56651cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
56661cb0ef41Sopenharmony_ci
56671cb0ef41Sopenharmony_ciThis API requests that the previously allocated work be scheduled
56681cb0ef41Sopenharmony_cifor execution. Once it returns successfully, this API must not be called again
56691cb0ef41Sopenharmony_ciwith the same `napi_async_work` item or the result will be undefined.
56701cb0ef41Sopenharmony_ci
56711cb0ef41Sopenharmony_ci### `napi_cancel_async_work`
56721cb0ef41Sopenharmony_ci
56731cb0ef41Sopenharmony_ci<!-- YAML
56741cb0ef41Sopenharmony_ciadded: v8.0.0
56751cb0ef41Sopenharmony_cinapiVersion: 1
56761cb0ef41Sopenharmony_ci-->
56771cb0ef41Sopenharmony_ci
56781cb0ef41Sopenharmony_ci```c
56791cb0ef41Sopenharmony_cinapi_status napi_cancel_async_work(node_api_nogc_env env,
56801cb0ef41Sopenharmony_ci                                   napi_async_work work);
56811cb0ef41Sopenharmony_ci```
56821cb0ef41Sopenharmony_ci
56831cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
56841cb0ef41Sopenharmony_ci* `[in] work`: The handle returned by the call to `napi_create_async_work`.
56851cb0ef41Sopenharmony_ci
56861cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
56871cb0ef41Sopenharmony_ci
56881cb0ef41Sopenharmony_ciThis API cancels queued work if it has not yet
56891cb0ef41Sopenharmony_cibeen started. If it has already started executing, it cannot be
56901cb0ef41Sopenharmony_cicancelled and `napi_generic_failure` will be returned. If successful,
56911cb0ef41Sopenharmony_cithe `complete` callback will be invoked with a status value of
56921cb0ef41Sopenharmony_ci`napi_cancelled`. The work should not be deleted before the `complete`
56931cb0ef41Sopenharmony_cicallback invocation, even if it has been successfully cancelled.
56941cb0ef41Sopenharmony_ci
56951cb0ef41Sopenharmony_ciThis API can be called even if there is a pending JavaScript exception.
56961cb0ef41Sopenharmony_ci
56971cb0ef41Sopenharmony_ci## Custom asynchronous operations
56981cb0ef41Sopenharmony_ci
56991cb0ef41Sopenharmony_ciThe simple asynchronous work APIs above may not be appropriate for every
57001cb0ef41Sopenharmony_ciscenario. When using any other asynchronous mechanism, the following APIs
57011cb0ef41Sopenharmony_ciare necessary to ensure an asynchronous operation is properly tracked by
57021cb0ef41Sopenharmony_cithe runtime.
57031cb0ef41Sopenharmony_ci
57041cb0ef41Sopenharmony_ci### `napi_async_init`
57051cb0ef41Sopenharmony_ci
57061cb0ef41Sopenharmony_ci<!-- YAML
57071cb0ef41Sopenharmony_ciadded: v8.6.0
57081cb0ef41Sopenharmony_cinapiVersion: 1
57091cb0ef41Sopenharmony_ci-->
57101cb0ef41Sopenharmony_ci
57111cb0ef41Sopenharmony_ci```c
57121cb0ef41Sopenharmony_cinapi_status napi_async_init(napi_env env,
57131cb0ef41Sopenharmony_ci                            napi_value async_resource,
57141cb0ef41Sopenharmony_ci                            napi_value async_resource_name,
57151cb0ef41Sopenharmony_ci                            napi_async_context* result)
57161cb0ef41Sopenharmony_ci```
57171cb0ef41Sopenharmony_ci
57181cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
57191cb0ef41Sopenharmony_ci* `[in] async_resource`: Object associated with the async work
57201cb0ef41Sopenharmony_ci  that will be passed to possible `async_hooks` [`init` hooks][] and can be
57211cb0ef41Sopenharmony_ci  accessed by [`async_hooks.executionAsyncResource()`][].
57221cb0ef41Sopenharmony_ci* `[in] async_resource_name`: Identifier for the kind of resource that is being
57231cb0ef41Sopenharmony_ci  provided for diagnostic information exposed by the `async_hooks` API.
57241cb0ef41Sopenharmony_ci* `[out] result`: The initialized async context.
57251cb0ef41Sopenharmony_ci
57261cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
57271cb0ef41Sopenharmony_ci
57281cb0ef41Sopenharmony_ciThe `async_resource` object needs to be kept alive until
57291cb0ef41Sopenharmony_ci[`napi_async_destroy`][] to keep `async_hooks` related API acts correctly. In
57301cb0ef41Sopenharmony_ciorder to retain ABI compatibility with previous versions, `napi_async_context`s
57311cb0ef41Sopenharmony_ciare not maintaining the strong reference to the `async_resource` objects to
57321cb0ef41Sopenharmony_ciavoid introducing causing memory leaks. However, if the `async_resource` is
57331cb0ef41Sopenharmony_cigarbage collected by JavaScript engine before the `napi_async_context` was
57341cb0ef41Sopenharmony_cidestroyed by `napi_async_destroy`, calling `napi_async_context` related APIs
57351cb0ef41Sopenharmony_cilike [`napi_open_callback_scope`][] and [`napi_make_callback`][] can cause
57361cb0ef41Sopenharmony_ciproblems like loss of async context when using the `AsyncLocalStorage` API.
57371cb0ef41Sopenharmony_ci
57381cb0ef41Sopenharmony_ciIn order to retain ABI compatibility with previous versions, passing `NULL`
57391cb0ef41Sopenharmony_cifor `async_resource` does not result in an error. However, this is not
57401cb0ef41Sopenharmony_cirecommended as this will result in undesirable behavior with  `async_hooks`
57411cb0ef41Sopenharmony_ci[`init` hooks][] and `async_hooks.executionAsyncResource()` as the resource is
57421cb0ef41Sopenharmony_cinow required by the underlying `async_hooks` implementation in order to provide
57431cb0ef41Sopenharmony_cithe linkage between async callbacks.
57441cb0ef41Sopenharmony_ci
57451cb0ef41Sopenharmony_ci### `napi_async_destroy`
57461cb0ef41Sopenharmony_ci
57471cb0ef41Sopenharmony_ci<!-- YAML
57481cb0ef41Sopenharmony_ciadded: v8.6.0
57491cb0ef41Sopenharmony_cinapiVersion: 1
57501cb0ef41Sopenharmony_ci-->
57511cb0ef41Sopenharmony_ci
57521cb0ef41Sopenharmony_ci```c
57531cb0ef41Sopenharmony_cinapi_status napi_async_destroy(napi_env env,
57541cb0ef41Sopenharmony_ci                               napi_async_context async_context);
57551cb0ef41Sopenharmony_ci```
57561cb0ef41Sopenharmony_ci
57571cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
57581cb0ef41Sopenharmony_ci* `[in] async_context`: The async context to be destroyed.
57591cb0ef41Sopenharmony_ci
57601cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
57611cb0ef41Sopenharmony_ci
57621cb0ef41Sopenharmony_ciThis API can be called even if there is a pending JavaScript exception.
57631cb0ef41Sopenharmony_ci
57641cb0ef41Sopenharmony_ci### `napi_make_callback`
57651cb0ef41Sopenharmony_ci
57661cb0ef41Sopenharmony_ci<!-- YAML
57671cb0ef41Sopenharmony_ciadded: v8.0.0
57681cb0ef41Sopenharmony_cinapiVersion: 1
57691cb0ef41Sopenharmony_cichanges:
57701cb0ef41Sopenharmony_ci  - version: v8.6.0
57711cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/15189
57721cb0ef41Sopenharmony_ci    description: Added `async_context` parameter.
57731cb0ef41Sopenharmony_ci-->
57741cb0ef41Sopenharmony_ci
57751cb0ef41Sopenharmony_ci```c
57761cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_make_callback(napi_env env,
57771cb0ef41Sopenharmony_ci                                           napi_async_context async_context,
57781cb0ef41Sopenharmony_ci                                           napi_value recv,
57791cb0ef41Sopenharmony_ci                                           napi_value func,
57801cb0ef41Sopenharmony_ci                                           size_t argc,
57811cb0ef41Sopenharmony_ci                                           const napi_value* argv,
57821cb0ef41Sopenharmony_ci                                           napi_value* result);
57831cb0ef41Sopenharmony_ci```
57841cb0ef41Sopenharmony_ci
57851cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
57861cb0ef41Sopenharmony_ci* `[in] async_context`: Context for the async operation that is
57871cb0ef41Sopenharmony_ci  invoking the callback. This should normally be a value previously
57881cb0ef41Sopenharmony_ci  obtained from [`napi_async_init`][].
57891cb0ef41Sopenharmony_ci  In order to retain ABI compatibility with previous versions, passing `NULL`
57901cb0ef41Sopenharmony_ci  for `async_context` does not result in an error. However, this results
57911cb0ef41Sopenharmony_ci  in incorrect operation of async hooks. Potential issues include loss of
57921cb0ef41Sopenharmony_ci  async context when using the `AsyncLocalStorage` API.
57931cb0ef41Sopenharmony_ci* `[in] recv`: The `this` value passed to the called function.
57941cb0ef41Sopenharmony_ci* `[in] func`: `napi_value` representing the JavaScript function to be invoked.
57951cb0ef41Sopenharmony_ci* `[in] argc`: The count of elements in the `argv` array.
57961cb0ef41Sopenharmony_ci* `[in] argv`: Array of JavaScript values as `napi_value` representing the
57971cb0ef41Sopenharmony_ci  arguments to the function. If `argc` is zero this parameter may be
57981cb0ef41Sopenharmony_ci  omitted by passing in `NULL`.
57991cb0ef41Sopenharmony_ci* `[out] result`: `napi_value` representing the JavaScript object returned.
58001cb0ef41Sopenharmony_ci
58011cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
58021cb0ef41Sopenharmony_ci
58031cb0ef41Sopenharmony_ciThis method allows a JavaScript function object to be called from a native
58041cb0ef41Sopenharmony_ciadd-on. This API is similar to `napi_call_function`. However, it is used to call
58051cb0ef41Sopenharmony_ci_from_ native code back _into_ JavaScript _after_ returning from an async
58061cb0ef41Sopenharmony_cioperation (when there is no other script on the stack). It is a fairly simple
58071cb0ef41Sopenharmony_ciwrapper around `node::MakeCallback`.
58081cb0ef41Sopenharmony_ci
58091cb0ef41Sopenharmony_ciNote it is _not_ necessary to use `napi_make_callback` from within a
58101cb0ef41Sopenharmony_ci`napi_async_complete_callback`; in that situation the callback's async
58111cb0ef41Sopenharmony_cicontext has already been set up, so a direct call to `napi_call_function`
58121cb0ef41Sopenharmony_ciis sufficient and appropriate. Use of the `napi_make_callback` function
58131cb0ef41Sopenharmony_cimay be required when implementing custom async behavior that does not use
58141cb0ef41Sopenharmony_ci`napi_create_async_work`.
58151cb0ef41Sopenharmony_ci
58161cb0ef41Sopenharmony_ciAny `process.nextTick`s or Promises scheduled on the microtask queue by
58171cb0ef41Sopenharmony_ciJavaScript during the callback are ran before returning back to C/C++.
58181cb0ef41Sopenharmony_ci
58191cb0ef41Sopenharmony_ci### `napi_open_callback_scope`
58201cb0ef41Sopenharmony_ci
58211cb0ef41Sopenharmony_ci<!-- YAML
58221cb0ef41Sopenharmony_ciadded: v9.6.0
58231cb0ef41Sopenharmony_cinapiVersion: 3
58241cb0ef41Sopenharmony_ci-->
58251cb0ef41Sopenharmony_ci
58261cb0ef41Sopenharmony_ci```c
58271cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_open_callback_scope(napi_env env,
58281cb0ef41Sopenharmony_ci                                                 napi_value resource_object,
58291cb0ef41Sopenharmony_ci                                                 napi_async_context context,
58301cb0ef41Sopenharmony_ci                                                 napi_callback_scope* result)
58311cb0ef41Sopenharmony_ci```
58321cb0ef41Sopenharmony_ci
58331cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
58341cb0ef41Sopenharmony_ci* `[in] resource_object`: An object associated with the async work
58351cb0ef41Sopenharmony_ci  that will be passed to possible `async_hooks` [`init` hooks][]. This
58361cb0ef41Sopenharmony_ci  parameter has been deprecated and is ignored at runtime. Use the
58371cb0ef41Sopenharmony_ci  `async_resource` parameter in [`napi_async_init`][] instead.
58381cb0ef41Sopenharmony_ci* `[in] context`: Context for the async operation that is invoking the callback.
58391cb0ef41Sopenharmony_ci  This should be a value previously obtained from [`napi_async_init`][].
58401cb0ef41Sopenharmony_ci* `[out] result`: The newly created scope.
58411cb0ef41Sopenharmony_ci
58421cb0ef41Sopenharmony_ciThere are cases (for example, resolving promises) where it is
58431cb0ef41Sopenharmony_cinecessary to have the equivalent of the scope associated with a callback
58441cb0ef41Sopenharmony_ciin place when making certain Node-API calls. If there is no other script on
58451cb0ef41Sopenharmony_cithe stack the [`napi_open_callback_scope`][] and
58461cb0ef41Sopenharmony_ci[`napi_close_callback_scope`][] functions can be used to open/close
58471cb0ef41Sopenharmony_cithe required scope.
58481cb0ef41Sopenharmony_ci
58491cb0ef41Sopenharmony_ci### `napi_close_callback_scope`
58501cb0ef41Sopenharmony_ci
58511cb0ef41Sopenharmony_ci<!-- YAML
58521cb0ef41Sopenharmony_ciadded: v9.6.0
58531cb0ef41Sopenharmony_cinapiVersion: 3
58541cb0ef41Sopenharmony_ci-->
58551cb0ef41Sopenharmony_ci
58561cb0ef41Sopenharmony_ci```c
58571cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_close_callback_scope(napi_env env,
58581cb0ef41Sopenharmony_ci                                                  napi_callback_scope scope)
58591cb0ef41Sopenharmony_ci```
58601cb0ef41Sopenharmony_ci
58611cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
58621cb0ef41Sopenharmony_ci* `[in] scope`: The scope to be closed.
58631cb0ef41Sopenharmony_ci
58641cb0ef41Sopenharmony_ciThis API can be called even if there is a pending JavaScript exception.
58651cb0ef41Sopenharmony_ci
58661cb0ef41Sopenharmony_ci## Version management
58671cb0ef41Sopenharmony_ci
58681cb0ef41Sopenharmony_ci### `napi_get_node_version`
58691cb0ef41Sopenharmony_ci
58701cb0ef41Sopenharmony_ci<!-- YAML
58711cb0ef41Sopenharmony_ciadded: v8.4.0
58721cb0ef41Sopenharmony_cinapiVersion: 1
58731cb0ef41Sopenharmony_ci-->
58741cb0ef41Sopenharmony_ci
58751cb0ef41Sopenharmony_ci```c
58761cb0ef41Sopenharmony_citypedef struct {
58771cb0ef41Sopenharmony_ci  uint32_t major;
58781cb0ef41Sopenharmony_ci  uint32_t minor;
58791cb0ef41Sopenharmony_ci  uint32_t patch;
58801cb0ef41Sopenharmony_ci  const char* release;
58811cb0ef41Sopenharmony_ci} napi_node_version;
58821cb0ef41Sopenharmony_ci
58831cb0ef41Sopenharmony_cinapi_status napi_get_node_version(node_api_nogc_env env,
58841cb0ef41Sopenharmony_ci                                  const napi_node_version** version);
58851cb0ef41Sopenharmony_ci```
58861cb0ef41Sopenharmony_ci
58871cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
58881cb0ef41Sopenharmony_ci* `[out] version`: A pointer to version information for Node.js itself.
58891cb0ef41Sopenharmony_ci
58901cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
58911cb0ef41Sopenharmony_ci
58921cb0ef41Sopenharmony_ciThis function fills the `version` struct with the major, minor, and patch
58931cb0ef41Sopenharmony_civersion of Node.js that is currently running, and the `release` field with the
58941cb0ef41Sopenharmony_civalue of [`process.release.name`][`process.release`].
58951cb0ef41Sopenharmony_ci
58961cb0ef41Sopenharmony_ciThe returned buffer is statically allocated and does not need to be freed.
58971cb0ef41Sopenharmony_ci
58981cb0ef41Sopenharmony_ci### `napi_get_version`
58991cb0ef41Sopenharmony_ci
59001cb0ef41Sopenharmony_ci<!-- YAML
59011cb0ef41Sopenharmony_ciadded: v8.0.0
59021cb0ef41Sopenharmony_cinapiVersion: 1
59031cb0ef41Sopenharmony_ci-->
59041cb0ef41Sopenharmony_ci
59051cb0ef41Sopenharmony_ci```c
59061cb0ef41Sopenharmony_cinapi_status napi_get_version(node_api_nogc_env env,
59071cb0ef41Sopenharmony_ci                             uint32_t* result);
59081cb0ef41Sopenharmony_ci```
59091cb0ef41Sopenharmony_ci
59101cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
59111cb0ef41Sopenharmony_ci* `[out] result`: The highest version of Node-API supported.
59121cb0ef41Sopenharmony_ci
59131cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
59141cb0ef41Sopenharmony_ci
59151cb0ef41Sopenharmony_ciThis API returns the highest Node-API version supported by the
59161cb0ef41Sopenharmony_ciNode.js runtime. Node-API is planned to be additive such that
59171cb0ef41Sopenharmony_cinewer releases of Node.js may support additional API functions.
59181cb0ef41Sopenharmony_ciIn order to allow an addon to use a newer function when running with
59191cb0ef41Sopenharmony_civersions of Node.js that support it, while providing
59201cb0ef41Sopenharmony_cifallback behavior when running with Node.js versions that don't
59211cb0ef41Sopenharmony_cisupport it:
59221cb0ef41Sopenharmony_ci
59231cb0ef41Sopenharmony_ci* Call `napi_get_version()` to determine if the API is available.
59241cb0ef41Sopenharmony_ci* If available, dynamically load a pointer to the function using `uv_dlsym()`.
59251cb0ef41Sopenharmony_ci* Use the dynamically loaded pointer to invoke the function.
59261cb0ef41Sopenharmony_ci* If the function is not available, provide an alternate implementation
59271cb0ef41Sopenharmony_ci  that does not use the function.
59281cb0ef41Sopenharmony_ci
59291cb0ef41Sopenharmony_ci## Memory management
59301cb0ef41Sopenharmony_ci
59311cb0ef41Sopenharmony_ci### `napi_adjust_external_memory`
59321cb0ef41Sopenharmony_ci
59331cb0ef41Sopenharmony_ci<!-- YAML
59341cb0ef41Sopenharmony_ciadded: v8.5.0
59351cb0ef41Sopenharmony_cinapiVersion: 1
59361cb0ef41Sopenharmony_ci-->
59371cb0ef41Sopenharmony_ci
59381cb0ef41Sopenharmony_ci```c
59391cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_adjust_external_memory(node_api_nogc_env env,
59401cb0ef41Sopenharmony_ci                                                    int64_t change_in_bytes,
59411cb0ef41Sopenharmony_ci                                                    int64_t* result);
59421cb0ef41Sopenharmony_ci```
59431cb0ef41Sopenharmony_ci
59441cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
59451cb0ef41Sopenharmony_ci* `[in] change_in_bytes`: The change in externally allocated memory that is kept
59461cb0ef41Sopenharmony_ci  alive by JavaScript objects.
59471cb0ef41Sopenharmony_ci* `[out] result`: The adjusted value
59481cb0ef41Sopenharmony_ci
59491cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
59501cb0ef41Sopenharmony_ci
59511cb0ef41Sopenharmony_ciThis function gives V8 an indication of the amount of externally allocated
59521cb0ef41Sopenharmony_cimemory that is kept alive by JavaScript objects (i.e. a JavaScript object
59531cb0ef41Sopenharmony_cithat points to its own memory allocated by a native addon). Registering
59541cb0ef41Sopenharmony_ciexternally allocated memory will trigger global garbage collections more
59551cb0ef41Sopenharmony_cioften than it would otherwise.
59561cb0ef41Sopenharmony_ci
59571cb0ef41Sopenharmony_ci## Promises
59581cb0ef41Sopenharmony_ci
59591cb0ef41Sopenharmony_ciNode-API provides facilities for creating `Promise` objects as described in
59601cb0ef41Sopenharmony_ci[Section 25.4][] of the ECMA specification. It implements promises as a pair of
59611cb0ef41Sopenharmony_ciobjects. When a promise is created by `napi_create_promise()`, a "deferred"
59621cb0ef41Sopenharmony_ciobject is created and returned alongside the `Promise`. The deferred object is
59631cb0ef41Sopenharmony_cibound to the created `Promise` and is the only means to resolve or reject the
59641cb0ef41Sopenharmony_ci`Promise` using `napi_resolve_deferred()` or `napi_reject_deferred()`. The
59651cb0ef41Sopenharmony_cideferred object that is created by `napi_create_promise()` is freed by
59661cb0ef41Sopenharmony_ci`napi_resolve_deferred()` or `napi_reject_deferred()`. The `Promise` object may
59671cb0ef41Sopenharmony_cibe returned to JavaScript where it can be used in the usual fashion.
59681cb0ef41Sopenharmony_ci
59691cb0ef41Sopenharmony_ciFor example, to create a promise and pass it to an asynchronous worker:
59701cb0ef41Sopenharmony_ci
59711cb0ef41Sopenharmony_ci```c
59721cb0ef41Sopenharmony_cinapi_deferred deferred;
59731cb0ef41Sopenharmony_cinapi_value promise;
59741cb0ef41Sopenharmony_cinapi_status status;
59751cb0ef41Sopenharmony_ci
59761cb0ef41Sopenharmony_ci// Create the promise.
59771cb0ef41Sopenharmony_cistatus = napi_create_promise(env, &deferred, &promise);
59781cb0ef41Sopenharmony_ciif (status != napi_ok) return NULL;
59791cb0ef41Sopenharmony_ci
59801cb0ef41Sopenharmony_ci// Pass the deferred to a function that performs an asynchronous action.
59811cb0ef41Sopenharmony_cido_something_asynchronous(deferred);
59821cb0ef41Sopenharmony_ci
59831cb0ef41Sopenharmony_ci// Return the promise to JS
59841cb0ef41Sopenharmony_cireturn promise;
59851cb0ef41Sopenharmony_ci```
59861cb0ef41Sopenharmony_ci
59871cb0ef41Sopenharmony_ciThe above function `do_something_asynchronous()` would perform its asynchronous
59881cb0ef41Sopenharmony_ciaction and then it would resolve or reject the deferred, thereby concluding the
59891cb0ef41Sopenharmony_cipromise and freeing the deferred:
59901cb0ef41Sopenharmony_ci
59911cb0ef41Sopenharmony_ci```c
59921cb0ef41Sopenharmony_cinapi_deferred deferred;
59931cb0ef41Sopenharmony_cinapi_value undefined;
59941cb0ef41Sopenharmony_cinapi_status status;
59951cb0ef41Sopenharmony_ci
59961cb0ef41Sopenharmony_ci// Create a value with which to conclude the deferred.
59971cb0ef41Sopenharmony_cistatus = napi_get_undefined(env, &undefined);
59981cb0ef41Sopenharmony_ciif (status != napi_ok) return NULL;
59991cb0ef41Sopenharmony_ci
60001cb0ef41Sopenharmony_ci// Resolve or reject the promise associated with the deferred depending on
60011cb0ef41Sopenharmony_ci// whether the asynchronous action succeeded.
60021cb0ef41Sopenharmony_ciif (asynchronous_action_succeeded) {
60031cb0ef41Sopenharmony_ci  status = napi_resolve_deferred(env, deferred, undefined);
60041cb0ef41Sopenharmony_ci} else {
60051cb0ef41Sopenharmony_ci  status = napi_reject_deferred(env, deferred, undefined);
60061cb0ef41Sopenharmony_ci}
60071cb0ef41Sopenharmony_ciif (status != napi_ok) return NULL;
60081cb0ef41Sopenharmony_ci
60091cb0ef41Sopenharmony_ci// At this point the deferred has been freed, so we should assign NULL to it.
60101cb0ef41Sopenharmony_cideferred = NULL;
60111cb0ef41Sopenharmony_ci```
60121cb0ef41Sopenharmony_ci
60131cb0ef41Sopenharmony_ci### `napi_create_promise`
60141cb0ef41Sopenharmony_ci
60151cb0ef41Sopenharmony_ci<!-- YAML
60161cb0ef41Sopenharmony_ciadded: v8.5.0
60171cb0ef41Sopenharmony_cinapiVersion: 1
60181cb0ef41Sopenharmony_ci-->
60191cb0ef41Sopenharmony_ci
60201cb0ef41Sopenharmony_ci```c
60211cb0ef41Sopenharmony_cinapi_status napi_create_promise(napi_env env,
60221cb0ef41Sopenharmony_ci                                napi_deferred* deferred,
60231cb0ef41Sopenharmony_ci                                napi_value* promise);
60241cb0ef41Sopenharmony_ci```
60251cb0ef41Sopenharmony_ci
60261cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
60271cb0ef41Sopenharmony_ci* `[out] deferred`: A newly created deferred object which can later be passed to
60281cb0ef41Sopenharmony_ci  `napi_resolve_deferred()` or `napi_reject_deferred()` to resolve resp. reject
60291cb0ef41Sopenharmony_ci  the associated promise.
60301cb0ef41Sopenharmony_ci* `[out] promise`: The JavaScript promise associated with the deferred object.
60311cb0ef41Sopenharmony_ci
60321cb0ef41Sopenharmony_ciReturns `napi_ok` if the API succeeded.
60331cb0ef41Sopenharmony_ci
60341cb0ef41Sopenharmony_ciThis API creates a deferred object and a JavaScript promise.
60351cb0ef41Sopenharmony_ci
60361cb0ef41Sopenharmony_ci### `napi_resolve_deferred`
60371cb0ef41Sopenharmony_ci
60381cb0ef41Sopenharmony_ci<!-- YAML
60391cb0ef41Sopenharmony_ciadded: v8.5.0
60401cb0ef41Sopenharmony_cinapiVersion: 1
60411cb0ef41Sopenharmony_ci-->
60421cb0ef41Sopenharmony_ci
60431cb0ef41Sopenharmony_ci```c
60441cb0ef41Sopenharmony_cinapi_status napi_resolve_deferred(napi_env env,
60451cb0ef41Sopenharmony_ci                                  napi_deferred deferred,
60461cb0ef41Sopenharmony_ci                                  napi_value resolution);
60471cb0ef41Sopenharmony_ci```
60481cb0ef41Sopenharmony_ci
60491cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
60501cb0ef41Sopenharmony_ci* `[in] deferred`: The deferred object whose associated promise to resolve.
60511cb0ef41Sopenharmony_ci* `[in] resolution`: The value with which to resolve the promise.
60521cb0ef41Sopenharmony_ci
60531cb0ef41Sopenharmony_ciThis API resolves a JavaScript promise by way of the deferred object
60541cb0ef41Sopenharmony_ciwith which it is associated. Thus, it can only be used to resolve JavaScript
60551cb0ef41Sopenharmony_cipromises for which the corresponding deferred object is available. This
60561cb0ef41Sopenharmony_cieffectively means that the promise must have been created using
60571cb0ef41Sopenharmony_ci`napi_create_promise()` and the deferred object returned from that call must
60581cb0ef41Sopenharmony_cihave been retained in order to be passed to this API.
60591cb0ef41Sopenharmony_ci
60601cb0ef41Sopenharmony_ciThe deferred object is freed upon successful completion.
60611cb0ef41Sopenharmony_ci
60621cb0ef41Sopenharmony_ci### `napi_reject_deferred`
60631cb0ef41Sopenharmony_ci
60641cb0ef41Sopenharmony_ci<!-- YAML
60651cb0ef41Sopenharmony_ciadded: v8.5.0
60661cb0ef41Sopenharmony_cinapiVersion: 1
60671cb0ef41Sopenharmony_ci-->
60681cb0ef41Sopenharmony_ci
60691cb0ef41Sopenharmony_ci```c
60701cb0ef41Sopenharmony_cinapi_status napi_reject_deferred(napi_env env,
60711cb0ef41Sopenharmony_ci                                 napi_deferred deferred,
60721cb0ef41Sopenharmony_ci                                 napi_value rejection);
60731cb0ef41Sopenharmony_ci```
60741cb0ef41Sopenharmony_ci
60751cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
60761cb0ef41Sopenharmony_ci* `[in] deferred`: The deferred object whose associated promise to resolve.
60771cb0ef41Sopenharmony_ci* `[in] rejection`: The value with which to reject the promise.
60781cb0ef41Sopenharmony_ci
60791cb0ef41Sopenharmony_ciThis API rejects a JavaScript promise by way of the deferred object
60801cb0ef41Sopenharmony_ciwith which it is associated. Thus, it can only be used to reject JavaScript
60811cb0ef41Sopenharmony_cipromises for which the corresponding deferred object is available. This
60821cb0ef41Sopenharmony_cieffectively means that the promise must have been created using
60831cb0ef41Sopenharmony_ci`napi_create_promise()` and the deferred object returned from that call must
60841cb0ef41Sopenharmony_cihave been retained in order to be passed to this API.
60851cb0ef41Sopenharmony_ci
60861cb0ef41Sopenharmony_ciThe deferred object is freed upon successful completion.
60871cb0ef41Sopenharmony_ci
60881cb0ef41Sopenharmony_ci### `napi_is_promise`
60891cb0ef41Sopenharmony_ci
60901cb0ef41Sopenharmony_ci<!-- YAML
60911cb0ef41Sopenharmony_ciadded: v8.5.0
60921cb0ef41Sopenharmony_cinapiVersion: 1
60931cb0ef41Sopenharmony_ci-->
60941cb0ef41Sopenharmony_ci
60951cb0ef41Sopenharmony_ci```c
60961cb0ef41Sopenharmony_cinapi_status napi_is_promise(napi_env env,
60971cb0ef41Sopenharmony_ci                            napi_value value,
60981cb0ef41Sopenharmony_ci                            bool* is_promise);
60991cb0ef41Sopenharmony_ci```
61001cb0ef41Sopenharmony_ci
61011cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
61021cb0ef41Sopenharmony_ci* `[in] value`: The value to examine
61031cb0ef41Sopenharmony_ci* `[out] is_promise`: Flag indicating whether `promise` is a native promise
61041cb0ef41Sopenharmony_ci  object (that is, a promise object created by the underlying engine).
61051cb0ef41Sopenharmony_ci
61061cb0ef41Sopenharmony_ci## Script execution
61071cb0ef41Sopenharmony_ci
61081cb0ef41Sopenharmony_ciNode-API provides an API for executing a string containing JavaScript using the
61091cb0ef41Sopenharmony_ciunderlying JavaScript engine.
61101cb0ef41Sopenharmony_ci
61111cb0ef41Sopenharmony_ci### `napi_run_script`
61121cb0ef41Sopenharmony_ci
61131cb0ef41Sopenharmony_ci<!-- YAML
61141cb0ef41Sopenharmony_ciadded: v8.5.0
61151cb0ef41Sopenharmony_cinapiVersion: 1
61161cb0ef41Sopenharmony_ci-->
61171cb0ef41Sopenharmony_ci
61181cb0ef41Sopenharmony_ci```c
61191cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_run_script(napi_env env,
61201cb0ef41Sopenharmony_ci                                        napi_value script,
61211cb0ef41Sopenharmony_ci                                        napi_value* result);
61221cb0ef41Sopenharmony_ci```
61231cb0ef41Sopenharmony_ci
61241cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
61251cb0ef41Sopenharmony_ci* `[in] script`: A JavaScript string containing the script to execute.
61261cb0ef41Sopenharmony_ci* `[out] result`: The value resulting from having executed the script.
61271cb0ef41Sopenharmony_ci
61281cb0ef41Sopenharmony_ciThis function executes a string of JavaScript code and returns its result with
61291cb0ef41Sopenharmony_cithe following caveats:
61301cb0ef41Sopenharmony_ci
61311cb0ef41Sopenharmony_ci* Unlike `eval`, this function does not allow the script to access the current
61321cb0ef41Sopenharmony_ci  lexical scope, and therefore also does not allow to access the
61331cb0ef41Sopenharmony_ci  [module scope][], meaning that pseudo-globals such as `require` will not be
61341cb0ef41Sopenharmony_ci  available.
61351cb0ef41Sopenharmony_ci* The script can access the [global scope][]. Function and `var` declarations
61361cb0ef41Sopenharmony_ci  in the script will be added to the [`global`][] object. Variable declarations
61371cb0ef41Sopenharmony_ci  made using `let` and `const` will be visible globally, but will not be added
61381cb0ef41Sopenharmony_ci  to the [`global`][] object.
61391cb0ef41Sopenharmony_ci* The value of `this` is [`global`][] within the script.
61401cb0ef41Sopenharmony_ci
61411cb0ef41Sopenharmony_ci## libuv event loop
61421cb0ef41Sopenharmony_ci
61431cb0ef41Sopenharmony_ciNode-API provides a function for getting the current event loop associated with
61441cb0ef41Sopenharmony_cia specific `napi_env`.
61451cb0ef41Sopenharmony_ci
61461cb0ef41Sopenharmony_ci### `napi_get_uv_event_loop`
61471cb0ef41Sopenharmony_ci
61481cb0ef41Sopenharmony_ci<!-- YAML
61491cb0ef41Sopenharmony_ciadded:
61501cb0ef41Sopenharmony_ci  - v9.3.0
61511cb0ef41Sopenharmony_ci  - v8.10.0
61521cb0ef41Sopenharmony_cinapiVersion: 2
61531cb0ef41Sopenharmony_ci-->
61541cb0ef41Sopenharmony_ci
61551cb0ef41Sopenharmony_ci```c
61561cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_get_uv_event_loop(node_api_nogc_env env,
61571cb0ef41Sopenharmony_ci                                               struct uv_loop_s** loop);
61581cb0ef41Sopenharmony_ci```
61591cb0ef41Sopenharmony_ci
61601cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
61611cb0ef41Sopenharmony_ci* `[out] loop`: The current libuv loop instance.
61621cb0ef41Sopenharmony_ci
61631cb0ef41Sopenharmony_ci## Asynchronous thread-safe function calls
61641cb0ef41Sopenharmony_ci
61651cb0ef41Sopenharmony_ciJavaScript functions can normally only be called from a native addon's main
61661cb0ef41Sopenharmony_cithread. If an addon creates additional threads, then Node-API functions that
61671cb0ef41Sopenharmony_cirequire a `napi_env`, `napi_value`, or `napi_ref` must not be called from those
61681cb0ef41Sopenharmony_cithreads.
61691cb0ef41Sopenharmony_ci
61701cb0ef41Sopenharmony_ciWhen an addon has additional threads and JavaScript functions need to be invoked
61711cb0ef41Sopenharmony_cibased on the processing completed by those threads, those threads must
61721cb0ef41Sopenharmony_cicommunicate with the addon's main thread so that the main thread can invoke the
61731cb0ef41Sopenharmony_ciJavaScript function on their behalf. The thread-safe function APIs provide an
61741cb0ef41Sopenharmony_cieasy way to do this.
61751cb0ef41Sopenharmony_ci
61761cb0ef41Sopenharmony_ciThese APIs provide the type `napi_threadsafe_function` as well as APIs to
61771cb0ef41Sopenharmony_cicreate, destroy, and call objects of this type.
61781cb0ef41Sopenharmony_ci`napi_create_threadsafe_function()` creates a persistent reference to a
61791cb0ef41Sopenharmony_ci`napi_value` that holds a JavaScript function which can be called from multiple
61801cb0ef41Sopenharmony_cithreads. The calls happen asynchronously. This means that values with which the
61811cb0ef41Sopenharmony_ciJavaScript callback is to be called will be placed in a queue, and, for each
61821cb0ef41Sopenharmony_civalue in the queue, a call will eventually be made to the JavaScript function.
61831cb0ef41Sopenharmony_ci
61841cb0ef41Sopenharmony_ciUpon creation of a `napi_threadsafe_function` a `napi_finalize` callback can be
61851cb0ef41Sopenharmony_ciprovided. This callback will be invoked on the main thread when the thread-safe
61861cb0ef41Sopenharmony_cifunction is about to be destroyed. It receives the context and the finalize data
61871cb0ef41Sopenharmony_cigiven during construction, and provides an opportunity for cleaning up after the
61881cb0ef41Sopenharmony_cithreads e.g. by calling `uv_thread_join()`. **Aside from the main loop thread,
61891cb0ef41Sopenharmony_cino threads should be using the thread-safe function after the finalize callback
61901cb0ef41Sopenharmony_cicompletes.**
61911cb0ef41Sopenharmony_ci
61921cb0ef41Sopenharmony_ciThe `context` given during the call to `napi_create_threadsafe_function()` can
61931cb0ef41Sopenharmony_cibe retrieved from any thread with a call to
61941cb0ef41Sopenharmony_ci`napi_get_threadsafe_function_context()`.
61951cb0ef41Sopenharmony_ci
61961cb0ef41Sopenharmony_ci### Calling a thread-safe function
61971cb0ef41Sopenharmony_ci
61981cb0ef41Sopenharmony_ci`napi_call_threadsafe_function()` can be used for initiating a call into
61991cb0ef41Sopenharmony_ciJavaScript. `napi_call_threadsafe_function()` accepts a parameter which controls
62001cb0ef41Sopenharmony_ciwhether the API behaves blockingly. If set to `napi_tsfn_nonblocking`, the API
62011cb0ef41Sopenharmony_cibehaves non-blockingly, returning `napi_queue_full` if the queue was full,
62021cb0ef41Sopenharmony_cipreventing data from being successfully added to the queue. If set to
62031cb0ef41Sopenharmony_ci`napi_tsfn_blocking`, the API blocks until space becomes available in the queue.
62041cb0ef41Sopenharmony_ci`napi_call_threadsafe_function()` never blocks if the thread-safe function was
62051cb0ef41Sopenharmony_cicreated with a maximum queue size of 0.
62061cb0ef41Sopenharmony_ci
62071cb0ef41Sopenharmony_ci`napi_call_threadsafe_function()` should not be called with `napi_tsfn_blocking`
62081cb0ef41Sopenharmony_cifrom a JavaScript thread, because, if the queue is full, it may cause the
62091cb0ef41Sopenharmony_ciJavaScript thread to deadlock.
62101cb0ef41Sopenharmony_ci
62111cb0ef41Sopenharmony_ciThe actual call into JavaScript is controlled by the callback given via the
62121cb0ef41Sopenharmony_ci`call_js_cb` parameter. `call_js_cb` is invoked on the main thread once for each
62131cb0ef41Sopenharmony_civalue that was placed into the queue by a successful call to
62141cb0ef41Sopenharmony_ci`napi_call_threadsafe_function()`. If such a callback is not given, a default
62151cb0ef41Sopenharmony_cicallback will be used, and the resulting JavaScript call will have no arguments.
62161cb0ef41Sopenharmony_ciThe `call_js_cb` callback receives the JavaScript function to call as a
62171cb0ef41Sopenharmony_ci`napi_value` in its parameters, as well as the `void*` context pointer used when
62181cb0ef41Sopenharmony_cicreating the `napi_threadsafe_function`, and the next data pointer that was
62191cb0ef41Sopenharmony_cicreated by one of the secondary threads. The callback can then use an API such
62201cb0ef41Sopenharmony_cias `napi_call_function()` to call into JavaScript.
62211cb0ef41Sopenharmony_ci
62221cb0ef41Sopenharmony_ciThe callback may also be invoked with `env` and `call_js_cb` both set to `NULL`
62231cb0ef41Sopenharmony_cito indicate that calls into JavaScript are no longer possible, while items
62241cb0ef41Sopenharmony_ciremain in the queue that may need to be freed. This normally occurs when the
62251cb0ef41Sopenharmony_ciNode.js process exits while there is a thread-safe function still active.
62261cb0ef41Sopenharmony_ci
62271cb0ef41Sopenharmony_ciIt is not necessary to call into JavaScript via `napi_make_callback()` because
62281cb0ef41Sopenharmony_ciNode-API runs `call_js_cb` in a context appropriate for callbacks.
62291cb0ef41Sopenharmony_ci
62301cb0ef41Sopenharmony_ciZero or more queued items may be invoked in each tick of the event loop.
62311cb0ef41Sopenharmony_ciApplications should not depend on a specific behavior other than progress in
62321cb0ef41Sopenharmony_ciinvoking callbacks will be made and events will be invoked
62331cb0ef41Sopenharmony_cias time moves forward.
62341cb0ef41Sopenharmony_ci
62351cb0ef41Sopenharmony_ci### Reference counting of thread-safe functions
62361cb0ef41Sopenharmony_ci
62371cb0ef41Sopenharmony_ciThreads can be added to and removed from a `napi_threadsafe_function` object
62381cb0ef41Sopenharmony_ciduring its existence. Thus, in addition to specifying an initial number of
62391cb0ef41Sopenharmony_cithreads upon creation, `napi_acquire_threadsafe_function` can be called to
62401cb0ef41Sopenharmony_ciindicate that a new thread will start making use of the thread-safe function.
62411cb0ef41Sopenharmony_ciSimilarly, `napi_release_threadsafe_function` can be called to indicate that an
62421cb0ef41Sopenharmony_ciexisting thread will stop making use of the thread-safe function.
62431cb0ef41Sopenharmony_ci
62441cb0ef41Sopenharmony_ci`napi_threadsafe_function` objects are destroyed when every thread which uses
62451cb0ef41Sopenharmony_cithe object has called `napi_release_threadsafe_function()` or has received a
62461cb0ef41Sopenharmony_cireturn status of `napi_closing` in response to a call to
62471cb0ef41Sopenharmony_ci`napi_call_threadsafe_function`. The queue is emptied before the
62481cb0ef41Sopenharmony_ci`napi_threadsafe_function` is destroyed. `napi_release_threadsafe_function()`
62491cb0ef41Sopenharmony_cishould be the last API call made in conjunction with a given
62501cb0ef41Sopenharmony_ci`napi_threadsafe_function`, because after the call completes, there is no
62511cb0ef41Sopenharmony_ciguarantee that the `napi_threadsafe_function` is still allocated. For the same
62521cb0ef41Sopenharmony_cireason, do not use a thread-safe function
62531cb0ef41Sopenharmony_ciafter receiving a return value of `napi_closing` in response to a call to
62541cb0ef41Sopenharmony_ci`napi_call_threadsafe_function`. Data associated with the
62551cb0ef41Sopenharmony_ci`napi_threadsafe_function` can be freed in its `napi_finalize` callback which
62561cb0ef41Sopenharmony_ciwas passed to `napi_create_threadsafe_function()`. The parameter
62571cb0ef41Sopenharmony_ci`initial_thread_count` of `napi_create_threadsafe_function` marks the initial
62581cb0ef41Sopenharmony_cinumber of acquisitions of the thread-safe functions, instead of calling
62591cb0ef41Sopenharmony_ci`napi_acquire_threadsafe_function` multiple times at creation.
62601cb0ef41Sopenharmony_ci
62611cb0ef41Sopenharmony_ciOnce the number of threads making use of a `napi_threadsafe_function` reaches
62621cb0ef41Sopenharmony_cizero, no further threads can start making use of it by calling
62631cb0ef41Sopenharmony_ci`napi_acquire_threadsafe_function()`. In fact, all subsequent API calls
62641cb0ef41Sopenharmony_ciassociated with it, except `napi_release_threadsafe_function()`, will return an
62651cb0ef41Sopenharmony_cierror value of `napi_closing`.
62661cb0ef41Sopenharmony_ci
62671cb0ef41Sopenharmony_ciThe thread-safe function can be "aborted" by giving a value of `napi_tsfn_abort`
62681cb0ef41Sopenharmony_cito `napi_release_threadsafe_function()`. This will cause all subsequent APIs
62691cb0ef41Sopenharmony_ciassociated with the thread-safe function except
62701cb0ef41Sopenharmony_ci`napi_release_threadsafe_function()` to return `napi_closing` even before its
62711cb0ef41Sopenharmony_cireference count reaches zero. In particular, `napi_call_threadsafe_function()`
62721cb0ef41Sopenharmony_ciwill return `napi_closing`, thus informing the threads that it is no longer
62731cb0ef41Sopenharmony_cipossible to make asynchronous calls to the thread-safe function. This can be
62741cb0ef41Sopenharmony_ciused as a criterion for terminating the thread. **Upon receiving a return value
62751cb0ef41Sopenharmony_ciof `napi_closing` from `napi_call_threadsafe_function()` a thread must not use
62761cb0ef41Sopenharmony_cithe thread-safe function anymore because it is no longer guaranteed to
62771cb0ef41Sopenharmony_cibe allocated.**
62781cb0ef41Sopenharmony_ci
62791cb0ef41Sopenharmony_ci### Deciding whether to keep the process running
62801cb0ef41Sopenharmony_ci
62811cb0ef41Sopenharmony_ciSimilarly to libuv handles, thread-safe functions can be "referenced" and
62821cb0ef41Sopenharmony_ci"unreferenced". A "referenced" thread-safe function will cause the event loop on
62831cb0ef41Sopenharmony_cithe thread on which it is created to remain alive until the thread-safe function
62841cb0ef41Sopenharmony_ciis destroyed. In contrast, an "unreferenced" thread-safe function will not
62851cb0ef41Sopenharmony_ciprevent the event loop from exiting. The APIs `napi_ref_threadsafe_function` and
62861cb0ef41Sopenharmony_ci`napi_unref_threadsafe_function` exist for this purpose.
62871cb0ef41Sopenharmony_ci
62881cb0ef41Sopenharmony_ciNeither does `napi_unref_threadsafe_function` mark the thread-safe functions as
62891cb0ef41Sopenharmony_ciable to be destroyed nor does `napi_ref_threadsafe_function` prevent it from
62901cb0ef41Sopenharmony_cibeing destroyed.
62911cb0ef41Sopenharmony_ci
62921cb0ef41Sopenharmony_ci### `napi_create_threadsafe_function`
62931cb0ef41Sopenharmony_ci
62941cb0ef41Sopenharmony_ci<!-- YAML
62951cb0ef41Sopenharmony_ciadded: v10.6.0
62961cb0ef41Sopenharmony_cinapiVersion: 4
62971cb0ef41Sopenharmony_cichanges:
62981cb0ef41Sopenharmony_ci  - version:
62991cb0ef41Sopenharmony_ci     - v12.6.0
63001cb0ef41Sopenharmony_ci     - v10.17.0
63011cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/27791
63021cb0ef41Sopenharmony_ci    description: Made `func` parameter optional with custom `call_js_cb`.
63031cb0ef41Sopenharmony_ci-->
63041cb0ef41Sopenharmony_ci
63051cb0ef41Sopenharmony_ci```c
63061cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status
63071cb0ef41Sopenharmony_cinapi_create_threadsafe_function(napi_env env,
63081cb0ef41Sopenharmony_ci                                napi_value func,
63091cb0ef41Sopenharmony_ci                                napi_value async_resource,
63101cb0ef41Sopenharmony_ci                                napi_value async_resource_name,
63111cb0ef41Sopenharmony_ci                                size_t max_queue_size,
63121cb0ef41Sopenharmony_ci                                size_t initial_thread_count,
63131cb0ef41Sopenharmony_ci                                void* thread_finalize_data,
63141cb0ef41Sopenharmony_ci                                napi_finalize thread_finalize_cb,
63151cb0ef41Sopenharmony_ci                                void* context,
63161cb0ef41Sopenharmony_ci                                napi_threadsafe_function_call_js call_js_cb,
63171cb0ef41Sopenharmony_ci                                napi_threadsafe_function* result);
63181cb0ef41Sopenharmony_ci```
63191cb0ef41Sopenharmony_ci
63201cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
63211cb0ef41Sopenharmony_ci* `[in] func`: An optional JavaScript function to call from another thread. It
63221cb0ef41Sopenharmony_ci  must be provided if `NULL` is passed to `call_js_cb`.
63231cb0ef41Sopenharmony_ci* `[in] async_resource`: An optional object associated with the async work that
63241cb0ef41Sopenharmony_ci  will be passed to possible `async_hooks` [`init` hooks][].
63251cb0ef41Sopenharmony_ci* `[in] async_resource_name`: A JavaScript string to provide an identifier for
63261cb0ef41Sopenharmony_ci  the kind of resource that is being provided for diagnostic information exposed
63271cb0ef41Sopenharmony_ci  by the `async_hooks` API.
63281cb0ef41Sopenharmony_ci* `[in] max_queue_size`: Maximum size of the queue. `0` for no limit.
63291cb0ef41Sopenharmony_ci* `[in] initial_thread_count`: The initial number of acquisitions, i.e. the
63301cb0ef41Sopenharmony_ci  initial number of threads, including the main thread, which will be making use
63311cb0ef41Sopenharmony_ci  of this function.
63321cb0ef41Sopenharmony_ci* `[in] thread_finalize_data`: Optional data to be passed to `thread_finalize_cb`.
63331cb0ef41Sopenharmony_ci* `[in] thread_finalize_cb`: Optional function to call when the
63341cb0ef41Sopenharmony_ci  `napi_threadsafe_function` is being destroyed.
63351cb0ef41Sopenharmony_ci* `[in] context`: Optional data to attach to the resulting
63361cb0ef41Sopenharmony_ci  `napi_threadsafe_function`.
63371cb0ef41Sopenharmony_ci* `[in] call_js_cb`: Optional callback which calls the JavaScript function in
63381cb0ef41Sopenharmony_ci  response to a call on a different thread. This callback will be called on the
63391cb0ef41Sopenharmony_ci  main thread. If not given, the JavaScript function will be called with no
63401cb0ef41Sopenharmony_ci  parameters and with `undefined` as its `this` value.
63411cb0ef41Sopenharmony_ci  [`napi_threadsafe_function_call_js`][] provides more details.
63421cb0ef41Sopenharmony_ci* `[out] result`: The asynchronous thread-safe JavaScript function.
63431cb0ef41Sopenharmony_ci
63441cb0ef41Sopenharmony_ci**Change History:**
63451cb0ef41Sopenharmony_ci
63461cb0ef41Sopenharmony_ci* Experimental (`NAPI_EXPERIMENTAL` is defined):
63471cb0ef41Sopenharmony_ci
63481cb0ef41Sopenharmony_ci  Uncaught exceptions thrown in `call_js_cb` are handled with the
63491cb0ef41Sopenharmony_ci  [`'uncaughtException'`][] event, instead of being ignored.
63501cb0ef41Sopenharmony_ci
63511cb0ef41Sopenharmony_ci### `napi_get_threadsafe_function_context`
63521cb0ef41Sopenharmony_ci
63531cb0ef41Sopenharmony_ci<!-- YAML
63541cb0ef41Sopenharmony_ciadded: v10.6.0
63551cb0ef41Sopenharmony_cinapiVersion: 4
63561cb0ef41Sopenharmony_ci-->
63571cb0ef41Sopenharmony_ci
63581cb0ef41Sopenharmony_ci```c
63591cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status
63601cb0ef41Sopenharmony_cinapi_get_threadsafe_function_context(napi_threadsafe_function func,
63611cb0ef41Sopenharmony_ci                                     void** result);
63621cb0ef41Sopenharmony_ci```
63631cb0ef41Sopenharmony_ci
63641cb0ef41Sopenharmony_ci* `[in] func`: The thread-safe function for which to retrieve the context.
63651cb0ef41Sopenharmony_ci* `[out] result`: The location where to store the context.
63661cb0ef41Sopenharmony_ci
63671cb0ef41Sopenharmony_ciThis API may be called from any thread which makes use of `func`.
63681cb0ef41Sopenharmony_ci
63691cb0ef41Sopenharmony_ci### `napi_call_threadsafe_function`
63701cb0ef41Sopenharmony_ci
63711cb0ef41Sopenharmony_ci<!-- YAML
63721cb0ef41Sopenharmony_ciadded: v10.6.0
63731cb0ef41Sopenharmony_cinapiVersion: 4
63741cb0ef41Sopenharmony_cichanges:
63751cb0ef41Sopenharmony_ci  - version: v14.5.0
63761cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/33453
63771cb0ef41Sopenharmony_ci    description: Support for `napi_would_deadlock` has been reverted.
63781cb0ef41Sopenharmony_ci  - version: v14.1.0
63791cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/32689
63801cb0ef41Sopenharmony_ci    description: Return `napi_would_deadlock` when called with
63811cb0ef41Sopenharmony_ci                 `napi_tsfn_blocking` from the main thread or a worker thread
63821cb0ef41Sopenharmony_ci                 and the queue is full.
63831cb0ef41Sopenharmony_ci-->
63841cb0ef41Sopenharmony_ci
63851cb0ef41Sopenharmony_ci```c
63861cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status
63871cb0ef41Sopenharmony_cinapi_call_threadsafe_function(napi_threadsafe_function func,
63881cb0ef41Sopenharmony_ci                              void* data,
63891cb0ef41Sopenharmony_ci                              napi_threadsafe_function_call_mode is_blocking);
63901cb0ef41Sopenharmony_ci```
63911cb0ef41Sopenharmony_ci
63921cb0ef41Sopenharmony_ci* `[in] func`: The asynchronous thread-safe JavaScript function to invoke.
63931cb0ef41Sopenharmony_ci* `[in] data`: Data to send into JavaScript via the callback `call_js_cb`
63941cb0ef41Sopenharmony_ci  provided during the creation of the thread-safe JavaScript function.
63951cb0ef41Sopenharmony_ci* `[in] is_blocking`: Flag whose value can be either `napi_tsfn_blocking` to
63961cb0ef41Sopenharmony_ci  indicate that the call should block if the queue is full or
63971cb0ef41Sopenharmony_ci  `napi_tsfn_nonblocking` to indicate that the call should return immediately
63981cb0ef41Sopenharmony_ci  with a status of `napi_queue_full` whenever the queue is full.
63991cb0ef41Sopenharmony_ci
64001cb0ef41Sopenharmony_ciThis API should not be called with `napi_tsfn_blocking` from a JavaScript
64011cb0ef41Sopenharmony_cithread, because, if the queue is full, it may cause the JavaScript thread to
64021cb0ef41Sopenharmony_cideadlock.
64031cb0ef41Sopenharmony_ci
64041cb0ef41Sopenharmony_ciThis API will return `napi_closing` if `napi_release_threadsafe_function()` was
64051cb0ef41Sopenharmony_cicalled with `abort` set to `napi_tsfn_abort` from any thread. The value is only
64061cb0ef41Sopenharmony_ciadded to the queue if the API returns `napi_ok`.
64071cb0ef41Sopenharmony_ci
64081cb0ef41Sopenharmony_ciThis API may be called from any thread which makes use of `func`.
64091cb0ef41Sopenharmony_ci
64101cb0ef41Sopenharmony_ci### `napi_acquire_threadsafe_function`
64111cb0ef41Sopenharmony_ci
64121cb0ef41Sopenharmony_ci<!-- YAML
64131cb0ef41Sopenharmony_ciadded: v10.6.0
64141cb0ef41Sopenharmony_cinapiVersion: 4
64151cb0ef41Sopenharmony_ci-->
64161cb0ef41Sopenharmony_ci
64171cb0ef41Sopenharmony_ci```c
64181cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status
64191cb0ef41Sopenharmony_cinapi_acquire_threadsafe_function(napi_threadsafe_function func);
64201cb0ef41Sopenharmony_ci```
64211cb0ef41Sopenharmony_ci
64221cb0ef41Sopenharmony_ci* `[in] func`: The asynchronous thread-safe JavaScript function to start making
64231cb0ef41Sopenharmony_ci  use of.
64241cb0ef41Sopenharmony_ci
64251cb0ef41Sopenharmony_ciA thread should call this API before passing `func` to any other thread-safe
64261cb0ef41Sopenharmony_cifunction APIs to indicate that it will be making use of `func`. This prevents
64271cb0ef41Sopenharmony_ci`func` from being destroyed when all other threads have stopped making use of
64281cb0ef41Sopenharmony_ciit.
64291cb0ef41Sopenharmony_ci
64301cb0ef41Sopenharmony_ciThis API may be called from any thread which will start making use of `func`.
64311cb0ef41Sopenharmony_ci
64321cb0ef41Sopenharmony_ci### `napi_release_threadsafe_function`
64331cb0ef41Sopenharmony_ci
64341cb0ef41Sopenharmony_ci<!-- YAML
64351cb0ef41Sopenharmony_ciadded: v10.6.0
64361cb0ef41Sopenharmony_cinapiVersion: 4
64371cb0ef41Sopenharmony_ci-->
64381cb0ef41Sopenharmony_ci
64391cb0ef41Sopenharmony_ci```c
64401cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status
64411cb0ef41Sopenharmony_cinapi_release_threadsafe_function(napi_threadsafe_function func,
64421cb0ef41Sopenharmony_ci                                 napi_threadsafe_function_release_mode mode);
64431cb0ef41Sopenharmony_ci```
64441cb0ef41Sopenharmony_ci
64451cb0ef41Sopenharmony_ci* `[in] func`: The asynchronous thread-safe JavaScript function whose reference
64461cb0ef41Sopenharmony_ci  count to decrement.
64471cb0ef41Sopenharmony_ci* `[in] mode`: Flag whose value can be either `napi_tsfn_release` to indicate
64481cb0ef41Sopenharmony_ci  that the current thread will make no further calls to the thread-safe
64491cb0ef41Sopenharmony_ci  function, or `napi_tsfn_abort` to indicate that in addition to the current
64501cb0ef41Sopenharmony_ci  thread, no other thread should make any further calls to the thread-safe
64511cb0ef41Sopenharmony_ci  function. If set to `napi_tsfn_abort`, further calls to
64521cb0ef41Sopenharmony_ci  `napi_call_threadsafe_function()` will return `napi_closing`, and no further
64531cb0ef41Sopenharmony_ci  values will be placed in the queue.
64541cb0ef41Sopenharmony_ci
64551cb0ef41Sopenharmony_ciA thread should call this API when it stops making use of `func`. Passing `func`
64561cb0ef41Sopenharmony_cito any thread-safe APIs after having called this API has undefined results, as
64571cb0ef41Sopenharmony_ci`func` may have been destroyed.
64581cb0ef41Sopenharmony_ci
64591cb0ef41Sopenharmony_ciThis API may be called from any thread which will stop making use of `func`.
64601cb0ef41Sopenharmony_ci
64611cb0ef41Sopenharmony_ci### `napi_ref_threadsafe_function`
64621cb0ef41Sopenharmony_ci
64631cb0ef41Sopenharmony_ci<!-- YAML
64641cb0ef41Sopenharmony_ciadded: v10.6.0
64651cb0ef41Sopenharmony_cinapiVersion: 4
64661cb0ef41Sopenharmony_ci-->
64671cb0ef41Sopenharmony_ci
64681cb0ef41Sopenharmony_ci```c
64691cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status
64701cb0ef41Sopenharmony_cinapi_ref_threadsafe_function(node_api_nogc_env env, napi_threadsafe_function func);
64711cb0ef41Sopenharmony_ci```
64721cb0ef41Sopenharmony_ci
64731cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
64741cb0ef41Sopenharmony_ci* `[in] func`: The thread-safe function to reference.
64751cb0ef41Sopenharmony_ci
64761cb0ef41Sopenharmony_ciThis API is used to indicate that the event loop running on the main thread
64771cb0ef41Sopenharmony_cishould not exit until `func` has been destroyed. Similar to [`uv_ref`][] it is
64781cb0ef41Sopenharmony_cialso idempotent.
64791cb0ef41Sopenharmony_ci
64801cb0ef41Sopenharmony_ciNeither does `napi_unref_threadsafe_function` mark the thread-safe functions as
64811cb0ef41Sopenharmony_ciable to be destroyed nor does `napi_ref_threadsafe_function` prevent it from
64821cb0ef41Sopenharmony_cibeing destroyed. `napi_acquire_threadsafe_function` and
64831cb0ef41Sopenharmony_ci`napi_release_threadsafe_function` are available for that purpose.
64841cb0ef41Sopenharmony_ci
64851cb0ef41Sopenharmony_ciThis API may only be called from the main thread.
64861cb0ef41Sopenharmony_ci
64871cb0ef41Sopenharmony_ci### `napi_unref_threadsafe_function`
64881cb0ef41Sopenharmony_ci
64891cb0ef41Sopenharmony_ci<!-- YAML
64901cb0ef41Sopenharmony_ciadded: v10.6.0
64911cb0ef41Sopenharmony_cinapiVersion: 4
64921cb0ef41Sopenharmony_ci-->
64931cb0ef41Sopenharmony_ci
64941cb0ef41Sopenharmony_ci```c
64951cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status
64961cb0ef41Sopenharmony_cinapi_unref_threadsafe_function(node_api_nogc_env env, napi_threadsafe_function func);
64971cb0ef41Sopenharmony_ci```
64981cb0ef41Sopenharmony_ci
64991cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
65001cb0ef41Sopenharmony_ci* `[in] func`: The thread-safe function to unreference.
65011cb0ef41Sopenharmony_ci
65021cb0ef41Sopenharmony_ciThis API is used to indicate that the event loop running on the main thread
65031cb0ef41Sopenharmony_cimay exit before `func` is destroyed. Similar to [`uv_unref`][] it is also
65041cb0ef41Sopenharmony_ciidempotent.
65051cb0ef41Sopenharmony_ci
65061cb0ef41Sopenharmony_ciThis API may only be called from the main thread.
65071cb0ef41Sopenharmony_ci
65081cb0ef41Sopenharmony_ci## Miscellaneous utilities
65091cb0ef41Sopenharmony_ci
65101cb0ef41Sopenharmony_ci### `node_api_get_module_file_name`
65111cb0ef41Sopenharmony_ci
65121cb0ef41Sopenharmony_ci<!-- YAML
65131cb0ef41Sopenharmony_ciadded:
65141cb0ef41Sopenharmony_ci  - v15.9.0
65151cb0ef41Sopenharmony_ci  - v14.18.0
65161cb0ef41Sopenharmony_ci  - v12.22.0
65171cb0ef41Sopenharmony_cinapiVersion: 9
65181cb0ef41Sopenharmony_ci-->
65191cb0ef41Sopenharmony_ci
65201cb0ef41Sopenharmony_ci```c
65211cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status
65221cb0ef41Sopenharmony_cinode_api_get_module_file_name(node_api_nogc_env env, const char** result);
65231cb0ef41Sopenharmony_ci
65241cb0ef41Sopenharmony_ci```
65251cb0ef41Sopenharmony_ci
65261cb0ef41Sopenharmony_ci* `[in] env`: The environment that the API is invoked under.
65271cb0ef41Sopenharmony_ci* `[out] result`: A URL containing the absolute path of the
65281cb0ef41Sopenharmony_ci  location from which the add-on was loaded. For a file on the local
65291cb0ef41Sopenharmony_ci  file system it will start with `file://`. The string is null-terminated and
65301cb0ef41Sopenharmony_ci  owned by `env` and must thus not be modified or freed.
65311cb0ef41Sopenharmony_ci
65321cb0ef41Sopenharmony_ci`result` may be an empty string if the add-on loading process fails to establish
65331cb0ef41Sopenharmony_cithe add-on's file name during loading.
65341cb0ef41Sopenharmony_ci
65351cb0ef41Sopenharmony_ci[ABI Stability]: https://nodejs.org/en/docs/guides/abi-stability/
65361cb0ef41Sopenharmony_ci[AppVeyor]: https://www.appveyor.com
65371cb0ef41Sopenharmony_ci[C++ Addons]: addons.md
65381cb0ef41Sopenharmony_ci[CMake]: https://cmake.org
65391cb0ef41Sopenharmony_ci[CMake.js]: https://github.com/cmake-js/cmake-js
65401cb0ef41Sopenharmony_ci[ECMAScript Language Specification]: https://tc39.github.io/ecma262/
65411cb0ef41Sopenharmony_ci[Error handling]: #error-handling
65421cb0ef41Sopenharmony_ci[GCC]: https://gcc.gnu.org
65431cb0ef41Sopenharmony_ci[GYP]: https://gyp.gsrc.io
65441cb0ef41Sopenharmony_ci[GitHub releases]: https://help.github.com/en/github/administering-a-repository/about-releases
65451cb0ef41Sopenharmony_ci[LLVM]: https://llvm.org
65461cb0ef41Sopenharmony_ci[Native Abstractions for Node.js]: https://github.com/nodejs/nan
65471cb0ef41Sopenharmony_ci[Node-API Media]: https://github.com/nodejs/abi-stable-node/blob/HEAD/node-api-media.md
65481cb0ef41Sopenharmony_ci[Object lifetime management]: #object-lifetime-management
65491cb0ef41Sopenharmony_ci[Object wrap]: #object-wrap
65501cb0ef41Sopenharmony_ci[Section 12.10.4]: https://tc39.github.io/ecma262/#sec-instanceofoperator
65511cb0ef41Sopenharmony_ci[Section 12.5.5]: https://tc39.github.io/ecma262/#sec-typeof-operator
65521cb0ef41Sopenharmony_ci[Section 19.2]: https://tc39.github.io/ecma262/#sec-function-objects
65531cb0ef41Sopenharmony_ci[Section 19.4]: https://tc39.github.io/ecma262/#sec-symbol-objects
65541cb0ef41Sopenharmony_ci[Section 20.3]: https://tc39.github.io/ecma262/#sec-date-objects
65551cb0ef41Sopenharmony_ci[Section 22.1]: https://tc39.github.io/ecma262/#sec-array-objects
65561cb0ef41Sopenharmony_ci[Section 22.1.4.1]: https://tc39.github.io/ecma262/#sec-properties-of-array-instances-length
65571cb0ef41Sopenharmony_ci[Section 22.2]: https://tc39.github.io/ecma262/#sec-typedarray-objects
65581cb0ef41Sopenharmony_ci[Section 24.1]: https://tc39.github.io/ecma262/#sec-arraybuffer-objects
65591cb0ef41Sopenharmony_ci[Section 24.1.1.2]: https://tc39.es/ecma262/#sec-isdetachedbuffer
65601cb0ef41Sopenharmony_ci[Section 24.1.1.3]: https://tc39.es/ecma262/#sec-detacharraybuffer
65611cb0ef41Sopenharmony_ci[Section 24.3]: https://tc39.github.io/ecma262/#sec-dataview-objects
65621cb0ef41Sopenharmony_ci[Section 25.4]: https://tc39.github.io/ecma262/#sec-promise-objects
65631cb0ef41Sopenharmony_ci[Section 6]: https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values
65641cb0ef41Sopenharmony_ci[Section 6.1]: https://tc39.github.io/ecma262/#sec-ecmascript-language-types
65651cb0ef41Sopenharmony_ci[Section 6.1.4]: https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type
65661cb0ef41Sopenharmony_ci[Section 6.1.6]: https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type
65671cb0ef41Sopenharmony_ci[Section 6.1.7]: https://tc39.github.io/ecma262/#sec-object-type
65681cb0ef41Sopenharmony_ci[Section 6.1.7.1]: https://tc39.github.io/ecma262/#table-2
65691cb0ef41Sopenharmony_ci[Section 7]: https://tc39.github.io/ecma262/#sec-abstract-operations
65701cb0ef41Sopenharmony_ci[Section 7.1.13]: https://tc39.github.io/ecma262/#sec-toobject
65711cb0ef41Sopenharmony_ci[Section 7.1.2]: https://tc39.github.io/ecma262/#sec-toboolean
65721cb0ef41Sopenharmony_ci[Section 7.1.3]: https://tc39.github.io/ecma262/#sec-tonumber
65731cb0ef41Sopenharmony_ci[Section 7.2.14]: https://tc39.github.io/ecma262/#sec-strict-equality-comparison
65741cb0ef41Sopenharmony_ci[Section 7.2.2]: https://tc39.github.io/ecma262/#sec-isarray
65751cb0ef41Sopenharmony_ci[Section 8.7]: https://tc39.es/ecma262/#sec-agents
65761cb0ef41Sopenharmony_ci[Section 9.1.6]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc
65771cb0ef41Sopenharmony_ci[Travis CI]: https://travis-ci.org
65781cb0ef41Sopenharmony_ci[Visual Studio]: https://visualstudio.microsoft.com
65791cb0ef41Sopenharmony_ci[Working with JavaScript properties]: #working-with-javascript-properties
65801cb0ef41Sopenharmony_ci[Xcode]: https://developer.apple.com/xcode/
65811cb0ef41Sopenharmony_ci[`'uncaughtException'`]: process.md#event-uncaughtexception
65821cb0ef41Sopenharmony_ci[`Number.MAX_SAFE_INTEGER`]: https://tc39.github.io/ecma262/#sec-number.max_safe_integer
65831cb0ef41Sopenharmony_ci[`Number.MIN_SAFE_INTEGER`]: https://tc39.github.io/ecma262/#sec-number.min_safe_integer
65841cb0ef41Sopenharmony_ci[`Worker`]: worker_threads.md#class-worker
65851cb0ef41Sopenharmony_ci[`async_hooks.executionAsyncResource()`]: async_hooks.md#async_hooksexecutionasyncresource
65861cb0ef41Sopenharmony_ci[`build_with_cmake`]: https://github.com/nodejs/node-addon-examples/tree/main/build_with_cmake
65871cb0ef41Sopenharmony_ci[`global`]: globals.md#global
65881cb0ef41Sopenharmony_ci[`init` hooks]: async_hooks.md#initasyncid-type-triggerasyncid-resource
65891cb0ef41Sopenharmony_ci[`napi_add_async_cleanup_hook`]: #napi_add_async_cleanup_hook
65901cb0ef41Sopenharmony_ci[`napi_add_env_cleanup_hook`]: #napi_add_env_cleanup_hook
65911cb0ef41Sopenharmony_ci[`napi_add_finalizer`]: #napi_add_finalizer
65921cb0ef41Sopenharmony_ci[`napi_async_cleanup_hook`]: #napi_async_cleanup_hook
65931cb0ef41Sopenharmony_ci[`napi_async_complete_callback`]: #napi_async_complete_callback
65941cb0ef41Sopenharmony_ci[`napi_async_destroy`]: #napi_async_destroy
65951cb0ef41Sopenharmony_ci[`napi_async_init`]: #napi_async_init
65961cb0ef41Sopenharmony_ci[`napi_callback`]: #napi_callback
65971cb0ef41Sopenharmony_ci[`napi_cancel_async_work`]: #napi_cancel_async_work
65981cb0ef41Sopenharmony_ci[`napi_close_callback_scope`]: #napi_close_callback_scope
65991cb0ef41Sopenharmony_ci[`napi_close_escapable_handle_scope`]: #napi_close_escapable_handle_scope
66001cb0ef41Sopenharmony_ci[`napi_close_handle_scope`]: #napi_close_handle_scope
66011cb0ef41Sopenharmony_ci[`napi_create_async_work`]: #napi_create_async_work
66021cb0ef41Sopenharmony_ci[`napi_create_error`]: #napi_create_error
66031cb0ef41Sopenharmony_ci[`napi_create_external_arraybuffer`]: #napi_create_external_arraybuffer
66041cb0ef41Sopenharmony_ci[`napi_create_range_error`]: #napi_create_range_error
66051cb0ef41Sopenharmony_ci[`napi_create_reference`]: #napi_create_reference
66061cb0ef41Sopenharmony_ci[`napi_create_type_error`]: #napi_create_type_error
66071cb0ef41Sopenharmony_ci[`napi_define_class`]: #napi_define_class
66081cb0ef41Sopenharmony_ci[`napi_delete_async_work`]: #napi_delete_async_work
66091cb0ef41Sopenharmony_ci[`napi_delete_reference`]: #napi_delete_reference
66101cb0ef41Sopenharmony_ci[`napi_escape_handle`]: #napi_escape_handle
66111cb0ef41Sopenharmony_ci[`napi_finalize`]: #napi_finalize
66121cb0ef41Sopenharmony_ci[`napi_get_and_clear_last_exception`]: #napi_get_and_clear_last_exception
66131cb0ef41Sopenharmony_ci[`napi_get_array_length`]: #napi_get_array_length
66141cb0ef41Sopenharmony_ci[`napi_get_element`]: #napi_get_element
66151cb0ef41Sopenharmony_ci[`napi_get_last_error_info`]: #napi_get_last_error_info
66161cb0ef41Sopenharmony_ci[`napi_get_property`]: #napi_get_property
66171cb0ef41Sopenharmony_ci[`napi_get_reference_value`]: #napi_get_reference_value
66181cb0ef41Sopenharmony_ci[`napi_get_typedarray_info`]: #napi_get_typedarray_info
66191cb0ef41Sopenharmony_ci[`napi_get_value_external`]: #napi_get_value_external
66201cb0ef41Sopenharmony_ci[`napi_has_property`]: #napi_has_property
66211cb0ef41Sopenharmony_ci[`napi_instanceof`]: #napi_instanceof
66221cb0ef41Sopenharmony_ci[`napi_is_error`]: #napi_is_error
66231cb0ef41Sopenharmony_ci[`napi_is_exception_pending`]: #napi_is_exception_pending
66241cb0ef41Sopenharmony_ci[`napi_is_typedarray`]: #napi_is_typedarray
66251cb0ef41Sopenharmony_ci[`napi_make_callback`]: #napi_make_callback
66261cb0ef41Sopenharmony_ci[`napi_open_callback_scope`]: #napi_open_callback_scope
66271cb0ef41Sopenharmony_ci[`napi_open_escapable_handle_scope`]: #napi_open_escapable_handle_scope
66281cb0ef41Sopenharmony_ci[`napi_open_handle_scope`]: #napi_open_handle_scope
66291cb0ef41Sopenharmony_ci[`napi_property_attributes`]: #napi_property_attributes
66301cb0ef41Sopenharmony_ci[`napi_property_descriptor`]: #napi_property_descriptor
66311cb0ef41Sopenharmony_ci[`napi_queue_async_work`]: #napi_queue_async_work
66321cb0ef41Sopenharmony_ci[`napi_reference_ref`]: #napi_reference_ref
66331cb0ef41Sopenharmony_ci[`napi_reference_unref`]: #napi_reference_unref
66341cb0ef41Sopenharmony_ci[`napi_remove_async_cleanup_hook`]: #napi_remove_async_cleanup_hook
66351cb0ef41Sopenharmony_ci[`napi_remove_env_cleanup_hook`]: #napi_remove_env_cleanup_hook
66361cb0ef41Sopenharmony_ci[`napi_set_instance_data`]: #napi_set_instance_data
66371cb0ef41Sopenharmony_ci[`napi_set_property`]: #napi_set_property
66381cb0ef41Sopenharmony_ci[`napi_threadsafe_function_call_js`]: #napi_threadsafe_function_call_js
66391cb0ef41Sopenharmony_ci[`napi_throw_error`]: #napi_throw_error
66401cb0ef41Sopenharmony_ci[`napi_throw_range_error`]: #napi_throw_range_error
66411cb0ef41Sopenharmony_ci[`napi_throw_type_error`]: #napi_throw_type_error
66421cb0ef41Sopenharmony_ci[`napi_throw`]: #napi_throw
66431cb0ef41Sopenharmony_ci[`napi_unwrap`]: #napi_unwrap
66441cb0ef41Sopenharmony_ci[`napi_wrap`]: #napi_wrap
66451cb0ef41Sopenharmony_ci[`node-addon-api`]: https://github.com/nodejs/node-addon-api
66461cb0ef41Sopenharmony_ci[`node_api.h`]: https://github.com/nodejs/node/blob/HEAD/src/node_api.h
66471cb0ef41Sopenharmony_ci[`node_api_create_external_string_latin1`]: #node_api_create_external_string_latin1
66481cb0ef41Sopenharmony_ci[`node_api_create_external_string_utf16`]: #node_api_create_external_string_utf16
66491cb0ef41Sopenharmony_ci[`node_api_create_syntax_error`]: #node_api_create_syntax_error
66501cb0ef41Sopenharmony_ci[`node_api_nogc_finalize`]: #node_api_nogc_finalize
66511cb0ef41Sopenharmony_ci[`node_api_post_finalizer`]: #node_api_post_finalizer
66521cb0ef41Sopenharmony_ci[`node_api_throw_syntax_error`]: #node_api_throw_syntax_error
66531cb0ef41Sopenharmony_ci[`process.release`]: process.md#processrelease
66541cb0ef41Sopenharmony_ci[`uv_ref`]: https://docs.libuv.org/en/v1.x/handle.html#c.uv_ref
66551cb0ef41Sopenharmony_ci[`uv_unref`]: https://docs.libuv.org/en/v1.x/handle.html#c.uv_unref
66561cb0ef41Sopenharmony_ci[`worker.terminate()`]: worker_threads.md#workerterminate
66571cb0ef41Sopenharmony_ci[async_hooks `type`]: async_hooks.md#type
66581cb0ef41Sopenharmony_ci[context-aware addons]: addons.md#context-aware-addons
66591cb0ef41Sopenharmony_ci[docs]: https://github.com/nodejs/node-addon-api#api-documentation
66601cb0ef41Sopenharmony_ci[external]: #napi_create_external
66611cb0ef41Sopenharmony_ci[externals]: #napi_create_external
66621cb0ef41Sopenharmony_ci[global scope]: globals.md
66631cb0ef41Sopenharmony_ci[gyp-next]: https://github.com/nodejs/gyp-next
66641cb0ef41Sopenharmony_ci[module scope]: modules.md#the-module-scope
66651cb0ef41Sopenharmony_ci[node-gyp]: https://github.com/nodejs/node-gyp
66661cb0ef41Sopenharmony_ci[node-pre-gyp]: https://github.com/mapbox/node-pre-gyp
66671cb0ef41Sopenharmony_ci[prebuild]: https://github.com/prebuild/prebuild
66681cb0ef41Sopenharmony_ci[prebuildify]: https://github.com/prebuild/prebuildify
66691cb0ef41Sopenharmony_ci[worker threads]: https://nodejs.org/api/worker_threads.html
6670