1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <meta name="nodejs.org:node-version" content="v14.20.1"> 7 <title>Node-API | Node.js v14.20.1 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/n-api.html"> 12</head> 13<body class="alt apidoc" id="api-section-n-api"> 14 <div id="content" class="clearfix"> 15 <div id="column2" class="interior"> 16 <div id="intro" class="interior"> 17 <a href="/" title="Go back to the home page"> 18 Node.js 19 </a> 20 </div> 21 <ul> 22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 24</ul> 25<hr class="line"> 26<ul> 27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 29<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 30<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 31<li><a href="n-api.html" class="nav-n-api active">C/C++ addons with Node-API</a></li> 32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 35<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 36<li><a href="console.html" class="nav-console">Console</a></li> 37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 42<li><a href="dns.html" class="nav-dns">DNS</a></li> 43<li><a href="domain.html" class="nav-domain">Domain</a></li> 44<li><a href="errors.html" class="nav-errors">Errors</a></li> 45<li><a href="events.html" class="nav-events">Events</a></li> 46<li><a href="fs.html" class="nav-fs">File system</a></li> 47<li><a href="globals.html" class="nav-globals">Globals</a></li> 48<li><a href="http.html" class="nav-http">HTTP</a></li> 49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 50<li><a href="https.html" class="nav-https">HTTPS</a></li> 51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 52<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li> 56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 57<li><a href="net.html" class="nav-net">Net</a></li> 58<li><a href="os.html" class="nav-os">OS</a></li> 59<li><a href="path.html" class="nav-path">Path</a></li> 60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 61<li><a href="policy.html" class="nav-policy">Policies</a></li> 62<li><a href="process.html" class="nav-process">Process</a></li> 63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 65<li><a href="readline.html" class="nav-readline">Readline</a></li> 66<li><a href="repl.html" class="nav-repl">REPL</a></li> 67<li><a href="report.html" class="nav-report">Report</a></li> 68<li><a href="stream.html" class="nav-stream">Stream</a></li> 69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 70<li><a href="timers.html" class="nav-timers">Timers</a></li> 71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 73<li><a href="tty.html" class="nav-tty">TTY</a></li> 74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 75<li><a href="url.html" class="nav-url">URL</a></li> 76<li><a href="util.html" class="nav-util">Utilities</a></li> 77<li><a href="v8.html" class="nav-v8">V8</a></li> 78<li><a href="vm.html" class="nav-vm">VM</a></li> 79<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 82</ul> 83<hr class="line"> 84<ul> 85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 86</ul> 87 </div> 88 89 <div id="column1" data-id="n-api" class="interior"> 90 <header> 91 <div class="header-container"> 92 <h1>Node.js v14.20.1 documentation</h1> 93 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 94 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 95 <path fill="none" d="M0 0h24v24H0z" /> 96 <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 97 <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 98 </svg> 99 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 100 <path d="M0 0h24v24H0z" fill="none" /> 101 <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 102 </svg> 103 </button> 104 </div> 105 <div id="gtoc"> 106 <ul> 107 <li> 108 <a href="index.html">Index</a> 109 </li> 110 <li> 111 <a href="all.html">View on single page</a> 112 </li> 113 <li> 114 <a href="n-api.json">View as JSON</a> 115 </li> 116 117 <li class="version-picker"> 118 <a href="#">View another version <span>▼</span></a> 119 <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v18.x/api/n-api.html">18.x</a></li> 120<li><a href="https://nodejs.org/docs/latest-v17.x/api/n-api.html">17.x</a></li> 121<li><a href="https://nodejs.org/docs/latest-v16.x/api/n-api.html">16.x <b>LTS</b></a></li> 122<li><a href="https://nodejs.org/docs/latest-v15.x/api/n-api.html">15.x</a></li> 123<li><a href="https://nodejs.org/docs/latest-v14.x/api/n-api.html">14.x <b>LTS</b></a></li> 124<li><a href="https://nodejs.org/docs/latest-v13.x/api/n-api.html">13.x</a></li> 125<li><a href="https://nodejs.org/docs/latest-v12.x/api/n-api.html">12.x <b>LTS</b></a></li> 126<li><a href="https://nodejs.org/docs/latest-v11.x/api/n-api.html">11.x</a></li> 127<li><a href="https://nodejs.org/docs/latest-v10.x/api/n-api.html">10.x</a></li> 128<li><a href="https://nodejs.org/docs/latest-v9.x/api/n-api.html">9.x</a></li> 129<li><a href="https://nodejs.org/docs/latest-v8.x/api/n-api.html">8.x</a></li></ol> 130 </li> 131 132 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/n-api.md">Edit on GitHub</a></li> 133 </ul> 134 </div> 135 <hr> 136 </header> 137 138 <details id="toc" open><summary>Table of contents</summary><ul> 139<li><span class="stability_2"><a href="#n_api_node_api">Node-API</a></span> 140<ul> 141<li><a href="#n_api_implications_of_abi_stability">Implications of ABI stability</a></li> 142<li><a href="#n_api_building">Building</a> 143<ul> 144<li><a href="#n_api_build_tools">Build tools</a> 145<ul> 146<li><a href="#n_api_node_gyp">node-gyp</a></li> 147<li><a href="#n_api_cmake_js">CMake.js</a></li> 148</ul> 149</li> 150<li><a href="#n_api_uploading_precompiled_binaries">Uploading precompiled binaries</a> 151<ul> 152<li><a href="#n_api_node_pre_gyp">node-pre-gyp</a></li> 153<li><a href="#n_api_prebuild">prebuild</a></li> 154<li><a href="#n_api_prebuildify">prebuildify</a></li> 155</ul> 156</li> 157</ul> 158</li> 159<li><a href="#n_api_usage">Usage</a></li> 160<li><a href="#n_api_node_api_version_matrix">Node-API version matrix</a></li> 161<li><a href="#n_api_environment_life_cycle_apis">Environment life cycle APIs</a> 162<ul> 163<li><a href="#n_api_napi_set_instance_data">napi_set_instance_data</a></li> 164<li><a href="#n_api_napi_get_instance_data">napi_get_instance_data</a></li> 165</ul> 166</li> 167<li><a href="#n_api_basic_node_api_data_types">Basic Node-API data types</a> 168<ul> 169<li><a href="#n_api_napi_status">napi_status</a></li> 170<li><a href="#n_api_napi_extended_error_info">napi_extended_error_info</a></li> 171<li><a href="#n_api_napi_env">napi_env</a></li> 172<li><a href="#n_api_napi_value">napi_value</a></li> 173<li><a href="#n_api_napi_threadsafe_function">napi_threadsafe_function</a></li> 174<li><a href="#n_api_napi_threadsafe_function_release_mode">napi_threadsafe_function_release_mode</a></li> 175<li><a href="#n_api_napi_threadsafe_function_call_mode">napi_threadsafe_function_call_mode</a></li> 176<li><a href="#n_api_node_api_memory_management_types">Node-API memory management types</a> 177<ul> 178<li><a href="#n_api_napi_handle_scope">napi_handle_scope</a></li> 179<li><a href="#n_api_napi_escapable_handle_scope">napi_escapable_handle_scope</a></li> 180<li><a href="#n_api_napi_ref">napi_ref</a></li> 181<li><a href="#n_api_napi_type_tag">napi_type_tag</a></li> 182<li><a href="#n_api_napi_async_cleanup_hook_handle">napi_async_cleanup_hook_handle</a></li> 183</ul> 184</li> 185<li><a href="#n_api_node_api_callback_types">Node-API callback types</a> 186<ul> 187<li><a href="#n_api_napi_callback_info">napi_callback_info</a></li> 188<li><a href="#n_api_napi_callback">napi_callback</a></li> 189<li><a href="#n_api_napi_finalize">napi_finalize</a></li> 190<li><a href="#n_api_napi_async_execute_callback">napi_async_execute_callback</a></li> 191<li><a href="#n_api_napi_async_complete_callback">napi_async_complete_callback</a></li> 192<li><a href="#n_api_napi_threadsafe_function_call_js">napi_threadsafe_function_call_js</a></li> 193<li><a href="#n_api_napi_async_cleanup_hook">napi_async_cleanup_hook</a></li> 194</ul> 195</li> 196</ul> 197</li> 198<li><a href="#n_api_error_handling">Error handling</a> 199<ul> 200<li><a href="#n_api_return_values">Return values</a> 201<ul> 202<li><a href="#n_api_napi_get_last_error_info">napi_get_last_error_info</a></li> 203</ul> 204</li> 205<li><a href="#n_api_exceptions">Exceptions</a> 206<ul> 207<li><a href="#n_api_napi_throw">napi_throw</a></li> 208<li><a href="#n_api_napi_throw_error">napi_throw_error</a></li> 209<li><a href="#n_api_napi_throw_type_error">napi_throw_type_error</a></li> 210<li><a href="#n_api_napi_throw_range_error">napi_throw_range_error</a></li> 211<li><a href="#n_api_napi_is_error">napi_is_error</a></li> 212<li><a href="#n_api_napi_create_error">napi_create_error</a></li> 213<li><a href="#n_api_napi_create_type_error">napi_create_type_error</a></li> 214<li><a href="#n_api_napi_create_range_error">napi_create_range_error</a></li> 215<li><a href="#n_api_napi_get_and_clear_last_exception">napi_get_and_clear_last_exception</a></li> 216<li><a href="#n_api_napi_is_exception_pending">napi_is_exception_pending</a></li> 217<li><a href="#n_api_napi_fatal_exception">napi_fatal_exception</a></li> 218</ul> 219</li> 220<li><a href="#n_api_fatal_errors">Fatal errors</a> 221<ul> 222<li><a href="#n_api_napi_fatal_error">napi_fatal_error</a></li> 223</ul> 224</li> 225</ul> 226</li> 227<li><a href="#n_api_object_lifetime_management">Object lifetime management</a> 228<ul> 229<li><a href="#n_api_making_handle_lifespan_shorter_than_that_of_the_native_method">Making handle lifespan shorter than that of the native method</a> 230<ul> 231<li><a href="#n_api_napi_open_handle_scope">napi_open_handle_scope</a></li> 232<li><a href="#n_api_napi_close_handle_scope">napi_close_handle_scope</a></li> 233<li><a href="#n_api_napi_open_escapable_handle_scope">napi_open_escapable_handle_scope</a></li> 234<li><a href="#n_api_napi_close_escapable_handle_scope">napi_close_escapable_handle_scope</a></li> 235<li><a href="#n_api_napi_escape_handle">napi_escape_handle</a></li> 236</ul> 237</li> 238<li><a href="#n_api_references_to_objects_with_a_lifespan_longer_than_that_of_the_native_method">References to objects with a lifespan longer than that of the native method</a> 239<ul> 240<li><a href="#n_api_napi_create_reference">napi_create_reference</a></li> 241<li><a href="#n_api_napi_delete_reference">napi_delete_reference</a></li> 242<li><a href="#n_api_napi_reference_ref">napi_reference_ref</a></li> 243<li><a href="#n_api_napi_reference_unref">napi_reference_unref</a></li> 244<li><a href="#n_api_napi_get_reference_value">napi_get_reference_value</a></li> 245</ul> 246</li> 247<li><a href="#n_api_cleanup_on_exit_of_the_current_node_js_instance">Cleanup on exit of the current Node.js instance</a> 248<ul> 249<li><a href="#n_api_napi_add_env_cleanup_hook">napi_add_env_cleanup_hook</a></li> 250<li><a href="#n_api_napi_remove_env_cleanup_hook">napi_remove_env_cleanup_hook</a></li> 251<li><a href="#n_api_napi_add_async_cleanup_hook">napi_add_async_cleanup_hook</a></li> 252<li><a href="#n_api_napi_remove_async_cleanup_hook">napi_remove_async_cleanup_hook</a></li> 253</ul> 254</li> 255</ul> 256</li> 257<li><a href="#n_api_module_registration">Module registration</a></li> 258<li><a href="#n_api_working_with_javascript_values">Working with JavaScript values</a> 259<ul> 260<li><a href="#n_api_enum_types">Enum types</a> 261<ul> 262<li><a href="#n_api_napi_key_collection_mode">napi_key_collection_mode</a></li> 263<li><a href="#n_api_napi_key_filter">napi_key_filter</a></li> 264<li><a href="#n_api_napi_key_conversion">napi_key_conversion</a></li> 265<li><a href="#n_api_napi_valuetype">napi_valuetype</a></li> 266<li><a href="#n_api_napi_typedarray_type">napi_typedarray_type</a></li> 267</ul> 268</li> 269<li><a href="#n_api_object_creation_functions">Object creation functions</a> 270<ul> 271<li><a href="#n_api_napi_create_array">napi_create_array</a></li> 272<li><a href="#n_api_napi_create_array_with_length">napi_create_array_with_length</a></li> 273<li><a href="#n_api_napi_create_arraybuffer">napi_create_arraybuffer</a></li> 274<li><a href="#n_api_napi_create_buffer">napi_create_buffer</a></li> 275<li><a href="#n_api_napi_create_buffer_copy">napi_create_buffer_copy</a></li> 276<li><a href="#n_api_napi_create_date">napi_create_date</a></li> 277<li><a href="#n_api_napi_create_external">napi_create_external</a></li> 278<li><a href="#n_api_napi_create_external_arraybuffer">napi_create_external_arraybuffer</a></li> 279<li><a href="#n_api_napi_create_external_buffer">napi_create_external_buffer</a></li> 280<li><a href="#n_api_napi_create_object">napi_create_object</a></li> 281<li><a href="#n_api_napi_create_symbol">napi_create_symbol</a></li> 282<li><a href="#n_api_napi_create_typedarray">napi_create_typedarray</a></li> 283<li><a href="#n_api_napi_create_dataview">napi_create_dataview</a></li> 284</ul> 285</li> 286<li><a href="#n_api_functions_to_convert_from_c_types_to_node_api">Functions to convert from C types to Node-API</a> 287<ul> 288<li><a href="#n_api_napi_create_int32">napi_create_int32</a></li> 289<li><a href="#n_api_napi_create_uint32">napi_create_uint32</a></li> 290<li><a href="#n_api_napi_create_int64">napi_create_int64</a></li> 291<li><a href="#n_api_napi_create_double">napi_create_double</a></li> 292<li><a href="#n_api_napi_create_bigint_int64">napi_create_bigint_int64</a></li> 293<li><a href="#n_api_napi_create_bigint_uint64">napi_create_bigint_uint64</a></li> 294<li><a href="#n_api_napi_create_bigint_words">napi_create_bigint_words</a></li> 295<li><a href="#n_api_napi_create_string_latin1">napi_create_string_latin1</a></li> 296<li><a href="#n_api_napi_create_string_utf16">napi_create_string_utf16</a></li> 297<li><a href="#n_api_napi_create_string_utf8">napi_create_string_utf8</a></li> 298</ul> 299</li> 300<li><a href="#n_api_functions_to_convert_from_node_api_to_c_types">Functions to convert from Node-API to C types</a> 301<ul> 302<li><a href="#n_api_napi_get_array_length">napi_get_array_length</a></li> 303<li><a href="#n_api_napi_get_arraybuffer_info">napi_get_arraybuffer_info</a></li> 304<li><a href="#n_api_napi_get_buffer_info">napi_get_buffer_info</a></li> 305<li><a href="#n_api_napi_get_prototype">napi_get_prototype</a></li> 306<li><a href="#n_api_napi_get_typedarray_info">napi_get_typedarray_info</a></li> 307<li><a href="#n_api_napi_get_dataview_info">napi_get_dataview_info</a></li> 308<li><a href="#n_api_napi_get_date_value">napi_get_date_value</a></li> 309<li><a href="#n_api_napi_get_value_bool">napi_get_value_bool</a></li> 310<li><a href="#n_api_napi_get_value_double">napi_get_value_double</a></li> 311<li><a href="#n_api_napi_get_value_bigint_int64">napi_get_value_bigint_int64</a></li> 312<li><a href="#n_api_napi_get_value_bigint_uint64">napi_get_value_bigint_uint64</a></li> 313<li><a href="#n_api_napi_get_value_bigint_words">napi_get_value_bigint_words</a></li> 314<li><a href="#n_api_napi_get_value_external">napi_get_value_external</a></li> 315<li><a href="#n_api_napi_get_value_int32">napi_get_value_int32</a></li> 316<li><a href="#n_api_napi_get_value_int64">napi_get_value_int64</a></li> 317<li><a href="#n_api_napi_get_value_string_latin1">napi_get_value_string_latin1</a></li> 318<li><a href="#n_api_napi_get_value_string_utf8">napi_get_value_string_utf8</a></li> 319<li><a href="#n_api_napi_get_value_string_utf16">napi_get_value_string_utf16</a></li> 320<li><a href="#n_api_napi_get_value_uint32">napi_get_value_uint32</a></li> 321</ul> 322</li> 323<li><a href="#n_api_functions_to_get_global_instances">Functions to get global instances</a> 324<ul> 325<li><a href="#n_api_napi_get_boolean">napi_get_boolean</a></li> 326<li><a href="#n_api_napi_get_global">napi_get_global</a></li> 327<li><a href="#n_api_napi_get_null">napi_get_null</a></li> 328<li><a href="#n_api_napi_get_undefined">napi_get_undefined</a></li> 329</ul> 330</li> 331</ul> 332</li> 333<li><a href="#n_api_working_with_javascript_values_and_abstract_operations">Working with JavaScript values and abstract operations</a> 334<ul> 335<li><a href="#n_api_napi_coerce_to_bool">napi_coerce_to_bool</a></li> 336<li><a href="#n_api_napi_coerce_to_number">napi_coerce_to_number</a></li> 337<li><a href="#n_api_napi_coerce_to_object">napi_coerce_to_object</a></li> 338<li><a href="#n_api_napi_coerce_to_string">napi_coerce_to_string</a></li> 339<li><a href="#n_api_napi_typeof">napi_typeof</a></li> 340<li><a href="#n_api_napi_instanceof">napi_instanceof</a></li> 341<li><a href="#n_api_napi_is_array">napi_is_array</a></li> 342<li><a href="#n_api_napi_is_arraybuffer">napi_is_arraybuffer</a></li> 343<li><a href="#n_api_napi_is_buffer">napi_is_buffer</a></li> 344<li><a href="#n_api_napi_is_date">napi_is_date</a></li> 345<li><a href="#n_api_napi_is_error_1">napi_is_error</a></li> 346<li><a href="#n_api_napi_is_typedarray">napi_is_typedarray</a></li> 347<li><a href="#n_api_napi_is_dataview">napi_is_dataview</a></li> 348<li><a href="#n_api_napi_strict_equals">napi_strict_equals</a></li> 349<li><a href="#n_api_napi_detach_arraybuffer">napi_detach_arraybuffer</a></li> 350<li><a href="#n_api_napi_is_detached_arraybuffer">napi_is_detached_arraybuffer</a></li> 351</ul> 352</li> 353<li><a href="#n_api_working_with_javascript_properties">Working with JavaScript properties</a> 354<ul> 355<li><a href="#n_api_structures">Structures</a> 356<ul> 357<li><a href="#n_api_napi_property_attributes">napi_property_attributes</a></li> 358<li><a href="#n_api_napi_property_descriptor">napi_property_descriptor</a></li> 359</ul> 360</li> 361<li><a href="#n_api_functions">Functions</a> 362<ul> 363<li><a href="#n_api_napi_get_property_names">napi_get_property_names</a></li> 364<li><a href="#n_api_napi_get_all_property_names">napi_get_all_property_names</a></li> 365<li><a href="#n_api_napi_set_property">napi_set_property</a></li> 366<li><a href="#n_api_napi_get_property">napi_get_property</a></li> 367<li><a href="#n_api_napi_has_property">napi_has_property</a></li> 368<li><a href="#n_api_napi_delete_property">napi_delete_property</a></li> 369<li><a href="#n_api_napi_has_own_property">napi_has_own_property</a></li> 370<li><a href="#n_api_napi_set_named_property">napi_set_named_property</a></li> 371<li><a href="#n_api_napi_get_named_property">napi_get_named_property</a></li> 372<li><a href="#n_api_napi_has_named_property">napi_has_named_property</a></li> 373<li><a href="#n_api_napi_set_element">napi_set_element</a></li> 374<li><a href="#n_api_napi_get_element">napi_get_element</a></li> 375<li><a href="#n_api_napi_has_element">napi_has_element</a></li> 376<li><a href="#n_api_napi_delete_element">napi_delete_element</a></li> 377<li><a href="#n_api_napi_define_properties">napi_define_properties</a></li> 378<li><a href="#n_api_napi_object_freeze">napi_object_freeze</a></li> 379<li><a href="#n_api_napi_object_seal">napi_object_seal</a></li> 380</ul> 381</li> 382</ul> 383</li> 384<li><a href="#n_api_working_with_javascript_functions">Working with JavaScript functions</a> 385<ul> 386<li><a href="#n_api_napi_call_function">napi_call_function</a></li> 387<li><a href="#n_api_napi_create_function">napi_create_function</a></li> 388<li><a href="#n_api_napi_get_cb_info">napi_get_cb_info</a></li> 389<li><a href="#n_api_napi_get_new_target">napi_get_new_target</a></li> 390<li><a href="#n_api_napi_new_instance">napi_new_instance</a></li> 391</ul> 392</li> 393<li><a href="#n_api_object_wrap">Object wrap</a> 394<ul> 395<li><a href="#n_api_napi_define_class">napi_define_class</a></li> 396<li><a href="#n_api_napi_wrap">napi_wrap</a></li> 397<li><a href="#n_api_napi_unwrap">napi_unwrap</a></li> 398<li><a href="#n_api_napi_remove_wrap">napi_remove_wrap</a></li> 399<li><a href="#n_api_napi_type_tag_object">napi_type_tag_object</a></li> 400<li><a href="#n_api_napi_check_object_type_tag">napi_check_object_type_tag</a></li> 401<li><a href="#n_api_napi_add_finalizer">napi_add_finalizer</a></li> 402</ul> 403</li> 404<li><a href="#n_api_simple_asynchronous_operations">Simple asynchronous operations</a> 405<ul> 406<li><a href="#n_api_napi_create_async_work">napi_create_async_work</a></li> 407<li><a href="#n_api_napi_delete_async_work">napi_delete_async_work</a></li> 408<li><a href="#n_api_napi_queue_async_work">napi_queue_async_work</a></li> 409<li><a href="#n_api_napi_cancel_async_work">napi_cancel_async_work</a></li> 410</ul> 411</li> 412<li><a href="#n_api_custom_asynchronous_operations">Custom asynchronous operations</a> 413<ul> 414<li><a href="#n_api_napi_async_init">napi_async_init</a></li> 415<li><a href="#n_api_napi_async_destroy">napi_async_destroy</a></li> 416<li><a href="#n_api_napi_make_callback">napi_make_callback</a></li> 417<li><a href="#n_api_napi_open_callback_scope">napi_open_callback_scope</a></li> 418<li><a href="#n_api_napi_close_callback_scope">napi_close_callback_scope</a></li> 419</ul> 420</li> 421<li><a href="#n_api_version_management">Version management</a> 422<ul> 423<li><a href="#n_api_napi_get_node_version">napi_get_node_version</a></li> 424<li><a href="#n_api_napi_get_version">napi_get_version</a></li> 425</ul> 426</li> 427<li><a href="#n_api_memory_management">Memory management</a> 428<ul> 429<li><a href="#n_api_napi_adjust_external_memory">napi_adjust_external_memory</a></li> 430</ul> 431</li> 432<li><a href="#n_api_promises">Promises</a> 433<ul> 434<li><a href="#n_api_napi_create_promise">napi_create_promise</a></li> 435<li><a href="#n_api_napi_resolve_deferred">napi_resolve_deferred</a></li> 436<li><a href="#n_api_napi_reject_deferred">napi_reject_deferred</a></li> 437<li><a href="#n_api_napi_is_promise">napi_is_promise</a></li> 438</ul> 439</li> 440<li><a href="#n_api_script_execution">Script execution</a> 441<ul> 442<li><a href="#n_api_napi_run_script">napi_run_script</a></li> 443</ul> 444</li> 445<li><a href="#n_api_libuv_event_loop">libuv event loop</a> 446<ul> 447<li><a href="#n_api_napi_get_uv_event_loop">napi_get_uv_event_loop</a></li> 448</ul> 449</li> 450<li><a href="#n_api_asynchronous_thread_safe_function_calls">Asynchronous thread-safe function calls</a> 451<ul> 452<li><a href="#n_api_calling_a_thread_safe_function">Calling a thread-safe function</a></li> 453<li><a href="#n_api_reference_counting_of_thread_safe_functions">Reference counting of thread-safe functions</a></li> 454<li><a href="#n_api_deciding_whether_to_keep_the_process_running">Deciding whether to keep the process running</a></li> 455<li><a href="#n_api_napi_create_threadsafe_function">napi_create_threadsafe_function</a></li> 456<li><a href="#n_api_napi_get_threadsafe_function_context">napi_get_threadsafe_function_context</a></li> 457<li><a href="#n_api_napi_call_threadsafe_function">napi_call_threadsafe_function</a></li> 458<li><a href="#n_api_napi_acquire_threadsafe_function">napi_acquire_threadsafe_function</a></li> 459<li><a href="#n_api_napi_release_threadsafe_function">napi_release_threadsafe_function</a></li> 460<li><a href="#n_api_napi_ref_threadsafe_function">napi_ref_threadsafe_function</a></li> 461<li><a href="#n_api_napi_unref_threadsafe_function">napi_unref_threadsafe_function</a></li> 462</ul> 463</li> 464<li><a href="#n_api_miscellaneous_utilities">Miscellaneous utilities</a></li> 465<li><span class="stability_1"><a href="#n_api_node_api_get_module_file_name">node_api_get_module_file_name</a></span></li> 466</ul> 467</li> 468</ul></details> 469 470 <div id="apicontent"> 471 <h2>Node-API<span><a class="mark" href="#n_api_node_api" id="n_api_node_api">#</a></span></h2> 472 473 474<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p> 475<p>Node-API (formerly N-API) is an API for building native Addons. It is 476independent from the underlying JavaScript runtime (for example, V8) and is 477maintained as part of Node.js itself. This API will be Application Binary 478Interface (ABI) stable across versions of Node.js. It is intended to insulate 479addons from changes in the underlying JavaScript engine and allow modules 480compiled for one major version to run on later major versions of Node.js without 481recompilation. The <a href="https://nodejs.org/en/docs/guides/abi-stability/">ABI Stability</a> guide provides a more in-depth explanation.</p> 482<p>Addons are built/packaged with the same approach/tools outlined in the section 483titled <a href="addons.html">C++ Addons</a>. The only difference is the set of APIs that are used by 484the native code. Instead of using the V8 or <a href="https://github.com/nodejs/nan">Native Abstractions for Node.js</a> 485APIs, the functions available in Node-API are used.</p> 486<p>APIs exposed by Node-API are generally used to create and manipulate 487JavaScript values. Concepts and operations generally map to ideas specified 488in the ECMA-262 Language Specification. The APIs have the following 489properties:</p> 490<ul> 491<li>All Node-API calls return a status code of type <code>napi_status</code>. This 492status indicates whether the API call succeeded or failed.</li> 493<li>The API's return value is passed via an out parameter.</li> 494<li>All JavaScript values are abstracted behind an opaque type named 495<code>napi_value</code>.</li> 496<li>In case of an error status code, additional information can be obtained 497using <code>napi_get_last_error_info</code>. More information can be found in the error 498handling section <a href="#n_api_error_handling">Error handling</a>.</li> 499</ul> 500<p>Node-API is a C API that ensures ABI stability across Node.js versions 501and different compiler levels. A C++ API can be easier to use. 502To support using C++, the project maintains a 503C++ wrapper module called <a href="https://github.com/nodejs/node-addon-api"><code>node-addon-api</code></a>. 504This wrapper provides an inlineable C++ API. Binaries built 505with <code>node-addon-api</code> will depend on the symbols for the Node-API C-based 506functions exported by Node.js. <code>node-addon-api</code> is a more 507efficient way to write code that calls Node-API. Take, for example, the 508following <code>node-addon-api</code> code. The first section shows the 509<code>node-addon-api</code> code and the second section shows what actually gets 510used in the addon.</p> 511<pre><code class="language-cpp">Object obj = Object::<span class="hljs-built_in">New</span>(env); 512obj[<span class="hljs-string">"foo"</span>] = String::<span class="hljs-built_in">New</span>(env, <span class="hljs-string">"bar"</span>);</code></pre> 513<pre><code class="language-cpp">napi_status status; 514napi_value object, string; 515status = <span class="hljs-built_in">napi_create_object</span>(env, &object); 516<span class="hljs-keyword">if</span> (status != napi_ok) { 517 <span class="hljs-built_in">napi_throw_error</span>(env, ...); 518 <span class="hljs-keyword">return</span>; 519} 520 521status = <span class="hljs-built_in">napi_create_string_utf8</span>(env, <span class="hljs-string">"bar"</span>, NAPI_AUTO_LENGTH, &string); 522<span class="hljs-keyword">if</span> (status != napi_ok) { 523 <span class="hljs-built_in">napi_throw_error</span>(env, ...); 524 <span class="hljs-keyword">return</span>; 525} 526 527status = <span class="hljs-built_in">napi_set_named_property</span>(env, object, <span class="hljs-string">"foo"</span>, string); 528<span class="hljs-keyword">if</span> (status != napi_ok) { 529 <span class="hljs-built_in">napi_throw_error</span>(env, ...); 530 <span class="hljs-keyword">return</span>; 531}</code></pre> 532<p>The end result is that the addon only uses the exported C APIs. As a result, 533it still gets the benefits of the ABI stability provided by the C API.</p> 534<p>When using <code>node-addon-api</code> instead of the C APIs, start with the API <a href="https://github.com/nodejs/node-addon-api#api-documentation">docs</a> 535for <code>node-addon-api</code>.</p> 536<p>The <a href="https://nodejs.github.io/node-addon-examples/">Node-API Resource</a> offers 537an excellent orientation and tips for developers just getting started with 538Node-API and <code>node-addon-api</code>.</p> 539<section><h3>Implications of ABI stability<span><a class="mark" href="#n_api_implications_of_abi_stability" id="n_api_implications_of_abi_stability">#</a></span></h3> 540<p>Although Node-API provides an ABI stability guarantee, other parts of Node.js do 541not, and any external libraries used from the addon may not. In particular, 542none of the following APIs provide an ABI stability guarantee across major 543versions:</p> 544<ul> 545<li> 546<p>the Node.js C++ APIs available via any of</p> 547<pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node.h></span></span> 548<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_buffer.h></span></span> 549<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_version.h></span></span> 550<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_object_wrap.h></span></span></code></pre> 551</li> 552<li> 553<p>the libuv APIs which are also included with Node.js and available via</p> 554<pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><uv.h></span></span></code></pre> 555</li> 556<li> 557<p>the V8 API available via</p> 558<pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><v8.h></span></span></code></pre> 559</li> 560</ul> 561<p>Thus, for an addon to remain ABI-compatible across Node.js major versions, it 562must use Node-API exclusively by restricting itself to using</p> 563<pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_api.h></span></span></code></pre> 564<p>and by checking, for all external libraries that it uses, that the external 565library makes ABI stability guarantees similar to Node-API.</p> 566</section><section><h3>Building<span><a class="mark" href="#n_api_building" id="n_api_building">#</a></span></h3> 567<p>Unlike modules written in JavaScript, developing and deploying Node.js 568native addons using Node-API requires an additional set of tools. Besides the 569basic tools required to develop for Node.js, the native addon developer 570requires a toolchain that can compile C and C++ code into a binary. In 571addition, depending upon how the native addon is deployed, the <em>user</em> of 572the native addon will also need to have a C/C++ toolchain installed.</p> 573<p>For Linux developers, the necessary C/C++ toolchain packages are readily 574available. <a href="https://gcc.gnu.org">GCC</a> is widely used in the Node.js community to build and 575test across a variety of platforms. For many developers, the <a href="https://llvm.org">LLVM</a> 576compiler infrastructure is also a good choice.</p> 577<p>For Mac developers, <a href="https://developer.apple.com/xcode/">Xcode</a> offers all the required compiler tools. 578However, it is not necessary to install the entire Xcode IDE. The following 579command installs the necessary toolchain:</p> 580<pre><code class="language-bash">xcode-select --install</code></pre> 581<p>For Windows developers, <a href="https://visualstudio.microsoft.com">Visual Studio</a> offers all the required compiler 582tools. However, it is not necessary to install the entire Visual Studio 583IDE. The following command installs the necessary toolchain:</p> 584<pre><code class="language-bash">npm install --global windows-build-tools</code></pre> 585<p>The sections below describe the additional tools available for developing 586and deploying Node.js native addons.</p> 587<h4>Build tools<span><a class="mark" href="#n_api_build_tools" id="n_api_build_tools">#</a></span></h4> 588<p>Both the tools listed here require that <em>users</em> of the native 589addon have a C/C++ toolchain installed in order to successfully install 590the native addon.</p> 591<h5>node-gyp<span><a class="mark" href="#n_api_node_gyp" id="n_api_node_gyp">#</a></span></h5> 592<p><a href="https://github.com/nodejs/node-gyp">node-gyp</a> is a build system based on the <a href="https://github.com/nodejs/gyp-next">gyp-next</a> fork of 593Google's <a href="https://gyp.gsrc.io">GYP</a> tool and comes bundled with npm. GYP, and therefore node-gyp, 594requires that Python be installed.</p> 595<p>Historically, node-gyp has been the tool of choice for building native 596addons. It has widespread adoption and documentation. However, some 597developers have run into limitations in node-gyp.</p> 598<h5>CMake.js<span><a class="mark" href="#n_api_cmake_js" id="n_api_cmake_js">#</a></span></h5> 599<p><a href="https://github.com/cmake-js/cmake-js">CMake.js</a> is an alternative build system based on <a href="https://cmake.org">CMake</a>.</p> 600<p>CMake.js is a good choice for projects that already use CMake or for 601developers affected by limitations in node-gyp.</p> 602<h4>Uploading precompiled binaries<span><a class="mark" href="#n_api_uploading_precompiled_binaries" id="n_api_uploading_precompiled_binaries">#</a></span></h4> 603<p>The three tools listed here permit native addon developers and maintainers 604to create and upload binaries to public or private servers. These tools are 605typically integrated with CI/CD build systems like <a href="https://travis-ci.org">Travis CI</a> and 606<a href="https://www.appveyor.com">AppVeyor</a> to build and upload binaries for a variety of platforms and 607architectures. These binaries are then available for download by users who 608do not need to have a C/C++ toolchain installed.</p> 609<h5>node-pre-gyp<span><a class="mark" href="#n_api_node_pre_gyp" id="n_api_node_pre_gyp">#</a></span></h5> 610<p><a href="https://github.com/mapbox/node-pre-gyp">node-pre-gyp</a> is a tool based on node-gyp that adds the ability to 611upload binaries to a server of the developer's choice. node-pre-gyp has 612particularly good support for uploading binaries to Amazon S3.</p> 613<h5>prebuild<span><a class="mark" href="#n_api_prebuild" id="n_api_prebuild">#</a></span></h5> 614<p><a href="https://github.com/prebuild/prebuild">prebuild</a> is a tool that supports builds using either node-gyp or 615CMake.js. Unlike node-pre-gyp which supports a variety of servers, prebuild 616uploads binaries only to <a href="https://help.github.com/en/github/administering-a-repository/about-releases">GitHub releases</a>. prebuild is a good choice for 617GitHub projects using CMake.js.</p> 618<h5>prebuildify<span><a class="mark" href="#n_api_prebuildify" id="n_api_prebuildify">#</a></span></h5> 619<p><a href="https://github.com/prebuild/prebuildify">prebuildify</a> is a tool based on node-gyp. The advantage of prebuildify is 620that the built binaries are bundled with the native module when it's 621uploaded to npm. The binaries are downloaded from npm and are immediately 622available to the module user when the native module is installed.</p> 623</section><section><h3>Usage<span><a class="mark" href="#n_api_usage" id="n_api_usage">#</a></span></h3> 624<p>In order to use the Node-API functions, include the file <a href="https://github.com/nodejs/node/blob/HEAD/src/node_api.h"><code>node_api.h</code></a> which 625is located in the src directory in the node development tree:</p> 626<pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_api.h></span></span></code></pre> 627<p>This will opt into the default <code>NAPI_VERSION</code> for the given release of Node.js. 628In order to ensure compatibility with specific versions of Node-API, the version 629can be specified explicitly when including the header:</p> 630<pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_VERSION 3</span> 631<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_api.h></span></span></code></pre> 632<p>This restricts the Node-API surface to just the functionality that was available 633in the specified (and earlier) versions.</p> 634<p>Some of the Node-API surface is experimental and requires explicit opt-in:</p> 635<pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_EXPERIMENTAL</span> 636<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_api.h></span></span></code></pre> 637<p>In this case the entire API surface, including any experimental APIs, will be 638available to the module code.</p> 639</section><section><h3>Node-API version matrix<span><a class="mark" href="#n_api_node_api_version_matrix" id="n_api_node_api_version_matrix">#</a></span></h3> 640<p>Node-API versions are additive and versioned independently from Node.js. 641Version 4 is an extension to version 3 in that it has all of the APIs 642from version 3 with some additions. This means that it is not necessary 643to recompile for new versions of Node.js which are 644listed as supporting a later version.</p> 645<!-- For accessibility purposes, this table needs row headers. That means we 646 can't do it in markdown. Hence, the raw HTML. --> 647<table> 648 <tbody><tr> 649 <td></td> 650 <th scope="col">1</th> 651 <th scope="col">2</th> 652 <th scope="col">3</th> 653 </tr> 654 <tr> 655 <th scope="row">v6.x</th> 656 <td></td> 657 <td></td> 658 <td>v6.14.2*</td> 659 </tr> 660 <tr> 661 <th scope="row">v8.x</th> 662 <td>v8.6.0**</td> 663 <td>v8.10.0*</td> 664 <td>v8.11.2</td> 665 </tr> 666 <tr> 667 <th scope="row">v9.x</th> 668 <td>v9.0.0*</td> 669 <td>v9.3.0*</td> 670 <td>v9.11.0*</td> 671 </tr> 672 <tr> 673 <th scope="row">≥ v10.x</th> 674 <td>all releases</td> 675 <td>all releases</td> 676 <td>all releases</td> 677 </tr> 678</tbody></table> 679<table> 680 <tbody><tr> 681 <td></td> 682 <th scope="col">4</th> 683 <th scope="col">5</th> 684 <th scope="col">6</th> 685 <th scope="col">7</th> 686 <th scope="col">8</th> 687 </tr> 688 <tr> 689 <th scope="row">v10.x</th> 690 <td>v10.16.0</td> 691 <td>v10.17.0</td> 692 <td>v10.20.0</td> 693 <td>v10.23.0</td> 694 <td></td> 695 </tr> 696 <tr> 697 <th scope="row">v11.x</th> 698 <td>v11.8.0</td> 699 <td></td> 700 <td></td> 701 <td></td> 702 <td></td> 703 </tr> 704 <tr> 705 <th scope="row">v12.x</th> 706 <td>v12.0.0</td> 707 <td>v12.11.0</td> 708 <td>v12.17.0</td> 709 <td>v12.19.0</td> 710 <td>v12.22.0</td> 711 </tr> 712 <tr> 713 <th scope="row">v13.x</th> 714 <td>v13.0.0</td> 715 <td>v13.0.0</td> 716 <td></td> 717 <td></td> 718 <td></td> 719 </tr> 720 <tr> 721 <th scope="row">v14.x</th> 722 <td>v14.0.0</td> 723 <td>v14.0.0</td> 724 <td>v14.0.0</td> 725 <td>v14.12.0</td> 726 <td>v14.17.0</td> 727 </tr> 728 <tr> 729 <th scope="row">v15.x</th> 730 <td>v15.0.0</td> 731 <td>v15.0.0</td> 732 <td>v15.0.0</td> 733 <td>v15.0.0</td> 734 <td>v15.12.0</td> 735 </tr> 736 <tr> 737 <th scope="row">v16.x</th> 738 <td>v16.0.0</td> 739 <td>v16.0.0</td> 740 <td>v16.0.0</td> 741 <td>v16.0.0</td> 742 <td>v16.0.0</td> 743 </tr> 744</tbody></table> 745<p>* Node-API was experimental.</p> 746<p>** Node.js 8.0.0 included Node-API as experimental. It was released as 747Node-API version 1 but continued to evolve until Node.js 8.6.0. The API is 748different in versions prior to Node.js 8.6.0. We recommend Node-API version 3 or 749later.</p> 750<p>Each API documented for Node-API will have a header named <code>added in:</code>, and APIs 751which are stable will have the additional header <code>Node-API version:</code>. 752APIs are directly usable when using a Node.js version which supports 753the Node-API version shown in <code>Node-API version:</code> or higher. 754When using a Node.js version that does not support the 755<code>Node-API version:</code> listed or if there is no <code>Node-API version:</code> listed, 756then the API will only be available if 757<code>#define NAPI_EXPERIMENTAL</code> precedes the inclusion of <code>node_api.h</code> 758or <code>js_native_api.h</code>. If an API appears not to be available on 759a version of Node.js which is later than the one shown in <code>added in:</code> then 760this is most likely the reason for the apparent absence.</p> 761<p>The Node-APIs associated strictly with accessing ECMAScript features from native 762code can be found separately in <code>js_native_api.h</code> and <code>js_native_api_types.h</code>. 763The APIs defined in these headers are included in <code>node_api.h</code> and 764<code>node_api_types.h</code>. The headers are structured in this way in order to allow 765implementations of Node-API outside of Node.js. For those implementations the 766Node.js specific APIs may not be applicable.</p> 767<p>The Node.js-specific parts of an addon can be separated from the code that 768exposes the actual functionality to the JavaScript environment so that the 769latter may be used with multiple implementations of Node-API. In the example 770below, <code>addon.c</code> and <code>addon.h</code> refer only to <code>js_native_api.h</code>. This ensures 771that <code>addon.c</code> can be reused to compile against either the Node.js 772implementation of Node-API or any implementation of Node-API outside of Node.js.</p> 773<p><code>addon_node.c</code> is a separate file that contains the Node.js specific entry point 774to the addon and which instantiates the addon by calling into <code>addon.c</code> when the 775addon is loaded into a Node.js environment.</p> 776<pre><code class="language-c"><span class="hljs-comment">// addon.h</span> 777<span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> _ADDON_H_</span> 778<span class="hljs-meta">#<span class="hljs-keyword">define</span> _ADDON_H_</span> 779<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><js_native_api.h></span></span> 780napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_env env)</span>; 781<span class="hljs-meta">#<span class="hljs-keyword">endif</span> <span class="hljs-comment">// _ADDON_H_</span></span></code></pre> 782<pre><code class="language-c"><span class="hljs-comment">// addon.c</span> 783<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"addon.h"</span></span> 784 785<span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_CALL(env, call) \ 786 do { \ 787 napi_status status = (call); \ 788 <span class="hljs-keyword">if</span> (status != napi_ok) { \ 789 const napi_extended_error_info* error_info = NULL; \ 790 napi_get_last_error_info((env), &error_info); \ 791 bool is_pending; \ 792 napi_is_exception_pending((env), &is_pending); \ 793 <span class="hljs-keyword">if</span> (!is_pending) { \ 794 const char* message = (error_info->error_message == NULL) \ 795 ? <span class="hljs-string">"empty error message"</span> \ 796 : error_info->error_message; \ 797 napi_throw_error((env), NULL, message); \ 798 return NULL; \ 799 } \ 800 } \ 801 } while(0)</span> 802 803<span class="hljs-keyword">static</span> napi_value 804<span class="hljs-title function_">DoSomethingUseful</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> { 805 <span class="hljs-comment">// Do something useful.</span> 806 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 807} 808 809napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_env env)</span> { 810 napi_value result; 811 NAPI_CALL(env, napi_create_object(env, &result)); 812 813 napi_value exported_function; 814 NAPI_CALL(env, napi_create_function(env, 815 <span class="hljs-string">"doSomethingUseful"</span>, 816 NAPI_AUTO_LENGTH, 817 DoSomethingUseful, 818 <span class="hljs-literal">NULL</span>, 819 &exported_function)); 820 821 NAPI_CALL(env, napi_set_named_property(env, 822 result, 823 <span class="hljs-string">"doSomethingUseful"</span>, 824 exported_function)); 825 826 <span class="hljs-keyword">return</span> result; 827}</code></pre> 828<pre><code class="language-c"><span class="hljs-comment">// addon_node.c</span> 829<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_api.h></span></span> 830<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"addon.h"</span></span> 831 832NAPI_MODULE_INIT() { 833 <span class="hljs-comment">// This function body is expected to return a `napi_value`.</span> 834 <span class="hljs-comment">// The variables `napi_env env` and `napi_value exports` may be used within</span> 835 <span class="hljs-comment">// the body, as they are provided by the definition of `NAPI_MODULE_INIT()`.</span> 836 <span class="hljs-keyword">return</span> create_addon(env); 837}</code></pre> 838</section><section><h3>Environment life cycle APIs<span><a class="mark" href="#n_api_environment_life_cycle_apis" id="n_api_environment_life_cycle_apis">#</a></span></h3> 839<p><a href="https://tc39.es/ecma262/#sec-agents">Section 8.7</a> of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a> defines the concept 840of an "Agent" as a self-contained environment in which JavaScript code runs. 841Multiple such Agents may be started and terminated either concurrently or in 842sequence by the process.</p> 843<p>A Node.js environment corresponds to an ECMAScript Agent. In the main process, 844an environment is created at startup, and additional environments can be created 845on separate threads to serve as <a href="https://nodejs.org/api/worker_threads.html">worker threads</a>. When Node.js is embedded in 846another application, the main thread of the application may also construct and 847destroy a Node.js environment multiple times during the life cycle of the 848application process such that each Node.js environment created by the 849application may, in turn, during its life cycle create and destroy additional 850environments as worker threads.</p> 851<p>From the perspective of a native addon this means that the bindings it provides 852may be called multiple times, from multiple contexts, and even concurrently from 853multiple threads.</p> 854<p>Native addons may need to allocate global state which they use during 855their entire life cycle such that the state must be unique to each instance of 856the addon.</p> 857<p>To this end, Node-API provides a way to allocate data such that its life cycle 858is tied to the life cycle of the Agent.</p> 859<h4>napi_set_instance_data<span><a class="mark" href="#n_api_napi_set_instance_data" id="n_api_napi_set_instance_data">#</a></span></h4> 860<div class="api_metadata"> 861<span>Added in: v12.8.0, v10.20.0</span> 862<span>N-API version: 6</span> 863</div> 864<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_instance_data</span><span class="hljs-params">(napi_env env, 865 <span class="hljs-type">void</span>* data, 866 napi_finalize finalize_cb, 867 <span class="hljs-type">void</span>* finalize_hint)</span>;</code></pre> 868<ul> 869<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 870<li><code>[in] data</code>: The data item to make available to bindings of this instance.</li> 871<li><code>[in] finalize_cb</code>: The function to call when the environment is being torn 872down. The function receives <code>data</code> so that it might free it. 873<a href="#n_api_napi_finalize"><code>napi_finalize</code></a> provides more details.</li> 874<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during 875collection.</li> 876</ul> 877<p>Returns <code>napi_ok</code> if the API succeeded.</p> 878<p>This API associates <code>data</code> with the currently running Agent. <code>data</code> can later 879be retrieved using <code>napi_get_instance_data()</code>. Any existing data associated with 880the currently running Agent which was set by means of a previous call to 881<code>napi_set_instance_data()</code> will be overwritten. If a <code>finalize_cb</code> was provided 882by the previous call, it will not be called.</p> 883<h4>napi_get_instance_data<span><a class="mark" href="#n_api_napi_get_instance_data" id="n_api_napi_get_instance_data">#</a></span></h4> 884<div class="api_metadata"> 885<span>Added in: v12.8.0, v10.20.0</span> 886<span>N-API version: 6</span> 887</div> 888<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_instance_data</span><span class="hljs-params">(napi_env env, 889 <span class="hljs-type">void</span>** data)</span>;</code></pre> 890<ul> 891<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 892<li><code>[out] data</code>: The data item that was previously associated with the currently 893running Agent by a call to <code>napi_set_instance_data()</code>.</li> 894</ul> 895<p>Returns <code>napi_ok</code> if the API succeeded.</p> 896<p>This API retrieves data that was previously associated with the currently 897running Agent via <code>napi_set_instance_data()</code>. If no data is set, the call will 898succeed and <code>data</code> will be set to <code>NULL</code>.</p> 899</section><section><h3>Basic Node-API data types<span><a class="mark" href="#n_api_basic_node_api_data_types" id="n_api_basic_node_api_data_types">#</a></span></h3> 900<p>Node-API exposes the following fundamental datatypes as abstractions that are 901consumed by the various APIs. These APIs should be treated as opaque, 902introspectable only with other Node-API calls.</p> 903<h4>napi_status<span><a class="mark" href="#n_api_napi_status" id="n_api_napi_status">#</a></span></h4> 904<div class="api_metadata"> 905<span>Added in: v8.0.0</span> 906<span>N-API version: 1</span> 907</div> 908<p>Integral status code indicating the success or failure of a Node-API call. 909Currently, the following status codes are supported.</p> 910<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> 911 napi_ok, 912 napi_invalid_arg, 913 napi_object_expected, 914 napi_string_expected, 915 napi_name_expected, 916 napi_function_expected, 917 napi_number_expected, 918 napi_boolean_expected, 919 napi_array_expected, 920 napi_generic_failure, 921 napi_pending_exception, 922 napi_cancelled, 923 napi_escape_called_twice, 924 napi_handle_scope_mismatch, 925 napi_callback_scope_mismatch, 926 napi_queue_full, 927 napi_closing, 928 napi_bigint_expected, 929 napi_date_expected, 930 napi_arraybuffer_expected, 931 napi_detachable_arraybuffer_expected, 932 napi_would_deadlock, <span class="hljs-comment">/* unused */</span> 933} napi_status;</code></pre> 934<p>If additional information is required upon an API returning a failed status, 935it can be obtained by calling <code>napi_get_last_error_info</code>.</p> 936<h4>napi_extended_error_info<span><a class="mark" href="#n_api_napi_extended_error_info" id="n_api_napi_extended_error_info">#</a></span></h4> 937<div class="api_metadata"> 938<span>Added in: v8.0.0</span> 939<span>N-API version: 1</span> 940</div> 941<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span> 942 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* error_message; 943 <span class="hljs-type">void</span>* engine_reserved; 944 <span class="hljs-type">uint32_t</span> engine_error_code; 945 napi_status error_code; 946} napi_extended_error_info;</code></pre> 947<ul> 948<li><code>error_message</code>: UTF8-encoded string containing a VM-neutral description of 949the error.</li> 950<li><code>engine_reserved</code>: Reserved for VM-specific error details. This is currently 951not implemented for any VM.</li> 952<li><code>engine_error_code</code>: VM-specific error code. This is currently 953not implemented for any VM.</li> 954<li><code>error_code</code>: The Node-API status code that originated with the last error.</li> 955</ul> 956<p>See the <a href="#n_api_error_handling">Error handling</a> section for additional information.</p> 957<h4>napi_env<span><a class="mark" href="#n_api_napi_env" id="n_api_napi_env">#</a></span></h4> 958<p><code>napi_env</code> is used to represent a context that the underlying Node-API 959implementation can use to persist VM-specific state. This structure is passed 960to native functions when they're invoked, and it must be passed back when 961making Node-API calls. Specifically, the same <code>napi_env</code> that was passed in when 962the initial native function was called must be passed to any subsequent 963nested Node-API calls. Caching the <code>napi_env</code> for the purpose of general reuse, 964and passing the <code>napi_env</code> between instances of the same addon running on 965different <a href="worker_threads.html#worker_threads_class_worker"><code>Worker</code></a> threads is not allowed. The <code>napi_env</code> becomes invalid 966when an instance of a native addon is unloaded. Notification of this event is 967delivered through the callbacks given to <a href="#n_api_napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a> and 968<a href="#n_api_napi_set_instance_data"><code>napi_set_instance_data</code></a>.</p> 969<h4>napi_value<span><a class="mark" href="#n_api_napi_value" id="n_api_napi_value">#</a></span></h4> 970<p>This is an opaque pointer that is used to represent a JavaScript value.</p> 971<h4>napi_threadsafe_function<span><a class="mark" href="#n_api_napi_threadsafe_function" id="n_api_napi_threadsafe_function">#</a></span></h4> 972<div class="api_metadata"> 973<span>Added in: v10.6.0</span> 974<span>N-API version: 4</span> 975</div> 976<p>This is an opaque pointer that represents a JavaScript function which can be 977called asynchronously from multiple threads via 978<code>napi_call_threadsafe_function()</code>.</p> 979<h4>napi_threadsafe_function_release_mode<span><a class="mark" href="#n_api_napi_threadsafe_function_release_mode" id="n_api_napi_threadsafe_function_release_mode">#</a></span></h4> 980<div class="api_metadata"> 981<span>Added in: v10.6.0</span> 982<span>N-API version: 4</span> 983</div> 984<p>A value to be given to <code>napi_release_threadsafe_function()</code> to indicate whether 985the thread-safe function is to be closed immediately (<code>napi_tsfn_abort</code>) or 986merely released (<code>napi_tsfn_release</code>) and thus available for subsequent use via 987<code>napi_acquire_threadsafe_function()</code> and <code>napi_call_threadsafe_function()</code>.</p> 988<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> 989 napi_tsfn_release, 990 napi_tsfn_abort 991} napi_threadsafe_function_release_mode;</code></pre> 992<h4>napi_threadsafe_function_call_mode<span><a class="mark" href="#n_api_napi_threadsafe_function_call_mode" id="n_api_napi_threadsafe_function_call_mode">#</a></span></h4> 993<div class="api_metadata"> 994<span>Added in: v10.6.0</span> 995<span>N-API version: 4</span> 996</div> 997<p>A value to be given to <code>napi_call_threadsafe_function()</code> to indicate whether 998the call should block whenever the queue associated with the thread-safe 999function is full.</p> 1000<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> 1001 napi_tsfn_nonblocking, 1002 napi_tsfn_blocking 1003} napi_threadsafe_function_call_mode;</code></pre> 1004<h4>Node-API memory management types<span><a class="mark" href="#n_api_node_api_memory_management_types" id="n_api_node_api_memory_management_types">#</a></span></h4> 1005<h5>napi_handle_scope<span><a class="mark" href="#n_api_napi_handle_scope" id="n_api_napi_handle_scope">#</a></span></h5> 1006<p>This is an abstraction used to control and modify the lifetime of objects 1007created within a particular scope. In general, Node-API values are created 1008within the context of a handle scope. When a native method is called from 1009JavaScript, a default handle scope will exist. If the user does not explicitly 1010create a new handle scope, Node-API values will be created in the default handle 1011scope. For any invocations of code outside the execution of a native method 1012(for instance, during a libuv callback invocation), the module is required to 1013create a scope before invoking any functions that can result in the creation 1014of JavaScript values.</p> 1015<p>Handle scopes are created using <a href="#n_api_napi_open_handle_scope"><code>napi_open_handle_scope</code></a> and are destroyed 1016using <a href="#n_api_napi_close_handle_scope"><code>napi_close_handle_scope</code></a>. Closing the scope can indicate to the GC 1017that all <code>napi_value</code>s created during the lifetime of the handle scope are no 1018longer referenced from the current stack frame.</p> 1019<p>For more details, review the <a href="#n_api_object_lifetime_management">Object lifetime management</a>.</p> 1020<h5>napi_escapable_handle_scope<span><a class="mark" href="#n_api_napi_escapable_handle_scope" id="n_api_napi_escapable_handle_scope">#</a></span></h5> 1021<div class="api_metadata"> 1022<span>Added in: v8.0.0</span> 1023<span>N-API version: 1</span> 1024</div> 1025<p>Escapable handle scopes are a special type of handle scope to return values 1026created within a particular handle scope to a parent scope.</p> 1027<h5>napi_ref<span><a class="mark" href="#n_api_napi_ref" id="n_api_napi_ref">#</a></span></h5> 1028<div class="api_metadata"> 1029<span>Added in: v8.0.0</span> 1030<span>N-API version: 1</span> 1031</div> 1032<p>This is the abstraction to use to reference a <code>napi_value</code>. This allows for 1033users to manage the lifetimes of JavaScript values, including defining their 1034minimum lifetimes explicitly.</p> 1035<p>For more details, review the <a href="#n_api_object_lifetime_management">Object lifetime management</a>.</p> 1036<h5>napi_type_tag<span><a class="mark" href="#n_api_napi_type_tag" id="n_api_napi_type_tag">#</a></span></h5> 1037<div class="api_metadata"> 1038<span>Added in: v14.8.0, v12.19.0</span> 1039<span>N-API version: 8</span> 1040</div> 1041<p>A 128-bit value stored as two unsigned 64-bit integers. It serves as a UUID 1042with which JavaScript objects can be "tagged" in order to ensure that they are 1043of a certain type. This is a stronger check than <a href="#n_api_napi_instanceof"><code>napi_instanceof</code></a>, because 1044the latter can report a false positive if the object's prototype has been 1045manipulated. Type-tagging is most useful in conjunction with <a href="#n_api_napi_wrap"><code>napi_wrap</code></a> 1046because it ensures that the pointer retrieved from a wrapped object can be 1047safely cast to the native type corresponding to the type tag that had been 1048previously applied to the JavaScript object.</p> 1049<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span> 1050 <span class="hljs-type">uint64_t</span> lower; 1051 <span class="hljs-type">uint64_t</span> upper; 1052} napi_type_tag;</code></pre> 1053<h5>napi_async_cleanup_hook_handle<span><a class="mark" href="#n_api_napi_async_cleanup_hook_handle" id="n_api_napi_async_cleanup_hook_handle">#</a></span></h5> 1054<div class="api_metadata"> 1055<span>Added in: v14.10.0</span> 1056</div> 1057<p>An opaque value returned by <a href="#n_api_napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>. It must be passed 1058to <a href="#n_api_napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a> when the chain of asynchronous cleanup 1059events completes.</p> 1060<h4>Node-API callback types<span><a class="mark" href="#n_api_node_api_callback_types" id="n_api_node_api_callback_types">#</a></span></h4> 1061<h5>napi_callback_info<span><a class="mark" href="#n_api_napi_callback_info" id="n_api_napi_callback_info">#</a></span></h5> 1062<div class="api_metadata"> 1063<span>Added in: v8.0.0</span> 1064<span>N-API version: 1</span> 1065</div> 1066<p>Opaque datatype that is passed to a callback function. It can be used for 1067getting additional information about the context in which the callback was 1068invoked.</p> 1069<h5>napi_callback<span><a class="mark" href="#n_api_napi_callback" id="n_api_napi_callback">#</a></span></h5> 1070<div class="api_metadata"> 1071<span>Added in: v8.0.0</span> 1072<span>N-API version: 1</span> 1073</div> 1074<p>Function pointer type for user-provided native functions which are to be 1075exposed to JavaScript via Node-API. Callback functions should satisfy the 1076following signature:</p> 1077<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">napi_value</span> <span class="hljs-params">(*napi_callback)</span><span class="hljs-params">(napi_env, napi_callback_info)</span>;</code></pre> 1078<p>Unless for reasons discussed in <a href="#n_api_object_lifetime_management">Object Lifetime Management</a>, creating a 1079handle and/or callback scope inside a <code>napi_callback</code> is not necessary.</p> 1080<h5>napi_finalize<span><a class="mark" href="#n_api_napi_finalize" id="n_api_napi_finalize">#</a></span></h5> 1081<div class="api_metadata"> 1082<span>Added in: v8.0.0</span> 1083<span>N-API version: 1</span> 1084</div> 1085<p>Function pointer type for add-on provided functions that allow the user to be 1086notified when externally-owned data is ready to be cleaned up because the 1087object with which it was associated with, has been garbage-collected. The user 1088must provide a function satisfying the following signature which would get 1089called upon the object's collection. Currently, <code>napi_finalize</code> can be used for 1090finding out when objects that have external data are collected.</p> 1091<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_finalize)</span><span class="hljs-params">(napi_env env, 1092 <span class="hljs-type">void</span>* finalize_data, 1093 <span class="hljs-type">void</span>* finalize_hint)</span>;</code></pre> 1094<p>Unless for reasons discussed in <a href="#n_api_object_lifetime_management">Object Lifetime Management</a>, creating a 1095handle and/or callback scope inside the function body is not necessary.</p> 1096<h5>napi_async_execute_callback<span><a class="mark" href="#n_api_napi_async_execute_callback" id="n_api_napi_async_execute_callback">#</a></span></h5> 1097<div class="api_metadata"> 1098<span>Added in: v8.0.0</span> 1099<span>N-API version: 1</span> 1100</div> 1101<p>Function pointer used with functions that support asynchronous 1102operations. Callback functions must satisfy the following signature:</p> 1103<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_execute_callback)</span><span class="hljs-params">(napi_env env, <span class="hljs-type">void</span>* data)</span>;</code></pre> 1104<p>Implementations of this function must avoid making Node-API calls that execute 1105JavaScript or interact with JavaScript objects. Node-API calls should be in the 1106<code>napi_async_complete_callback</code> instead. Do not use the <code>napi_env</code> parameter as 1107it will likely result in execution of JavaScript.</p> 1108<h5>napi_async_complete_callback<span><a class="mark" href="#n_api_napi_async_complete_callback" id="n_api_napi_async_complete_callback">#</a></span></h5> 1109<div class="api_metadata"> 1110<span>Added in: v8.0.0</span> 1111<span>N-API version: 1</span> 1112</div> 1113<p>Function pointer used with functions that support asynchronous 1114operations. Callback functions must satisfy the following signature:</p> 1115<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_complete_callback)</span><span class="hljs-params">(napi_env env, 1116 napi_status status, 1117 <span class="hljs-type">void</span>* data)</span>;</code></pre> 1118<p>Unless for reasons discussed in <a href="#n_api_object_lifetime_management">Object Lifetime Management</a>, creating a 1119handle and/or callback scope inside the function body is not necessary.</p> 1120<h5>napi_threadsafe_function_call_js<span><a class="mark" href="#n_api_napi_threadsafe_function_call_js" id="n_api_napi_threadsafe_function_call_js">#</a></span></h5> 1121<div class="api_metadata"> 1122<span>Added in: v10.6.0</span> 1123<span>N-API version: 4</span> 1124</div> 1125<p>Function pointer used with asynchronous thread-safe function calls. The callback 1126will be called on the main thread. Its purpose is to use a data item arriving 1127via the queue from one of the secondary threads to construct the parameters 1128necessary for a call into JavaScript, usually via <code>napi_call_function</code>, and then 1129make the call into JavaScript.</p> 1130<p>The data arriving from the secondary thread via the queue is given in the <code>data</code> 1131parameter and the JavaScript function to call is given in the <code>js_callback</code> 1132parameter.</p> 1133<p>Node-API sets up the environment prior to calling this callback, so it is 1134sufficient to call the JavaScript function via <code>napi_call_function</code> rather than 1135via <code>napi_make_callback</code>.</p> 1136<p>Callback functions must satisfy the following signature:</p> 1137<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_threadsafe_function_call_js)</span><span class="hljs-params">(napi_env env, 1138 napi_value js_callback, 1139 <span class="hljs-type">void</span>* context, 1140 <span class="hljs-type">void</span>* data)</span>;</code></pre> 1141<ul> 1142<li><code>[in] env</code>: The environment to use for API calls, or <code>NULL</code> if the thread-safe 1143function is being torn down and <code>data</code> may need to be freed.</li> 1144<li><code>[in] js_callback</code>: The JavaScript function to call, or <code>NULL</code> if the 1145thread-safe function is being torn down and <code>data</code> may need to be freed. It 1146may also be <code>NULL</code> if the thread-safe function was created without 1147<code>js_callback</code>.</li> 1148<li><code>[in] context</code>: The optional data with which the thread-safe function was 1149created.</li> 1150<li><code>[in] data</code>: Data created by the secondary thread. It is the responsibility of 1151the callback to convert this native data to JavaScript values (with Node-API 1152functions) that can be passed as parameters when <code>js_callback</code> is invoked. 1153This pointer is managed entirely by the threads and this callback. Thus this 1154callback should free the data.</li> 1155</ul> 1156<p>Unless for reasons discussed in <a href="#n_api_object_lifetime_management">Object Lifetime Management</a>, creating a 1157handle and/or callback scope inside the function body is not necessary.</p> 1158<h5>napi_async_cleanup_hook<span><a class="mark" href="#n_api_napi_async_cleanup_hook" id="n_api_napi_async_cleanup_hook">#</a></span></h5> 1159<div class="api_metadata"> 1160<span>Added in: v14.10.0</span> 1161</div> 1162<p>Function pointer used with <a href="#n_api_napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>. It will be called 1163when the environment is being torn down.</p> 1164<p>Callback functions must satisfy the following signature:</p> 1165<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_cleanup_hook)</span><span class="hljs-params">(napi_async_cleanup_hook_handle handle, 1166 <span class="hljs-type">void</span>* data)</span>;</code></pre> 1167<ul> 1168<li><code>[in] handle</code>: The handle that must be passed to 1169<a href="#n_api_napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a> after completion of the asynchronous 1170cleanup.</li> 1171<li><code>[in] data</code>: The data that was passed to <a href="#n_api_napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>.</li> 1172</ul> 1173<p>The body of the function should initiate the asynchronous cleanup actions at the 1174end of which <code>handle</code> must be passed in a call to 1175<a href="#n_api_napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a>.</p> 1176</section><section><h3>Error handling<span><a class="mark" href="#n_api_error_handling" id="n_api_error_handling">#</a></span></h3> 1177<p>Node-API uses both return values and JavaScript exceptions for error handling. 1178The following sections explain the approach for each case.</p> 1179<h4>Return values<span><a class="mark" href="#n_api_return_values" id="n_api_return_values">#</a></span></h4> 1180<p>All of the Node-API functions share the same error handling pattern. The 1181return type of all API functions is <code>napi_status</code>.</p> 1182<p>The return value will be <code>napi_ok</code> if the request was successful and 1183no uncaught JavaScript exception was thrown. If an error occurred AND 1184an exception was thrown, the <code>napi_status</code> value for the error 1185will be returned. If an exception was thrown, and no error occurred, 1186<code>napi_pending_exception</code> will be returned.</p> 1187<p>In cases where a return value other than <code>napi_ok</code> or 1188<code>napi_pending_exception</code> is returned, <a href="#n_api_napi_is_exception_pending"><code>napi_is_exception_pending</code></a> 1189must be called to check if an exception is pending. 1190See the section on exceptions for more details.</p> 1191<p>The full set of possible <code>napi_status</code> values is defined 1192in <code>napi_api_types.h</code>.</p> 1193<p>The <code>napi_status</code> return value provides a VM-independent representation of 1194the error which occurred. In some cases it is useful to be able to get 1195more detailed information, including a string representing the error as well as 1196VM (engine)-specific information.</p> 1197<p>In order to retrieve this information <a href="#n_api_napi_get_last_error_info"><code>napi_get_last_error_info</code></a> 1198is provided which returns a <code>napi_extended_error_info</code> structure. 1199The format of the <code>napi_extended_error_info</code> structure is as follows:</p> 1200<div class="api_metadata"> 1201<span>Added in: v8.0.0</span> 1202<span>N-API version: 1</span> 1203</div> 1204<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">napi_extended_error_info</span> {</span> 1205 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* error_message; 1206 <span class="hljs-type">void</span>* engine_reserved; 1207 <span class="hljs-type">uint32_t</span> engine_error_code; 1208 napi_status error_code; 1209};</code></pre> 1210<ul> 1211<li><code>error_message</code>: Textual representation of the error that occurred.</li> 1212<li><code>engine_reserved</code>: Opaque handle reserved for engine use only.</li> 1213<li><code>engine_error_code</code>: VM specific error code.</li> 1214<li><code>error_code</code>: Node-API status code for the last error.</li> 1215</ul> 1216<p><a href="#n_api_napi_get_last_error_info"><code>napi_get_last_error_info</code></a> returns the information for the last 1217Node-API call that was made.</p> 1218<p>Do not rely on the content or format of any of the extended information as it 1219is not subject to SemVer and may change at any time. It is intended only for 1220logging purposes.</p> 1221<h5>napi_get_last_error_info<span><a class="mark" href="#n_api_napi_get_last_error_info" id="n_api_napi_get_last_error_info">#</a></span></h5> 1222<div class="api_metadata"> 1223<span>Added in: v8.0.0</span> 1224<span>N-API version: 1</span> 1225</div> 1226<pre><code class="language-c">napi_status 1227<span class="hljs-title function_">napi_get_last_error_info</span><span class="hljs-params">(napi_env env, 1228 <span class="hljs-keyword">const</span> napi_extended_error_info** result)</span>;</code></pre> 1229<ul> 1230<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1231<li><code>[out] result</code>: The <code>napi_extended_error_info</code> structure with more 1232information about the error.</li> 1233</ul> 1234<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1235<p>This API retrieves a <code>napi_extended_error_info</code> structure with information 1236about the last error that occurred.</p> 1237<p>The content of the <code>napi_extended_error_info</code> returned is only valid up until 1238a Node-API function is called on the same <code>env</code>.</p> 1239<p>Do not rely on the content or format of any of the extended information as it 1240is not subject to SemVer and may change at any time. It is intended only for 1241logging purposes.</p> 1242<p>This API can be called even if there is a pending JavaScript exception.</p> 1243<h4>Exceptions<span><a class="mark" href="#n_api_exceptions" id="n_api_exceptions">#</a></span></h4> 1244<p>Any Node-API function call may result in a pending JavaScript exception. This is 1245the case for any of the API functions, even those that may not cause the 1246execution of JavaScript.</p> 1247<p>If the <code>napi_status</code> returned by a function is <code>napi_ok</code> then no 1248exception is pending and no additional action is required. If the 1249<code>napi_status</code> returned is anything other than <code>napi_ok</code> or 1250<code>napi_pending_exception</code>, in order to try to recover and continue 1251instead of simply returning immediately, <a href="#n_api_napi_is_exception_pending"><code>napi_is_exception_pending</code></a> 1252must be called in order to determine if an exception is pending or not.</p> 1253<p>In many cases when a Node-API function is called and an exception is 1254already pending, the function will return immediately with a 1255<code>napi_status</code> of <code>napi_pending_exception</code>. However, this is not the case 1256for all functions. Node-API allows a subset of the functions to be 1257called to allow for some minimal cleanup before returning to JavaScript. 1258In that case, <code>napi_status</code> will reflect the status for the function. It 1259will not reflect previous pending exceptions. To avoid confusion, check 1260the error status after every function call.</p> 1261<p>When an exception is pending one of two approaches can be employed.</p> 1262<p>The first approach is to do any appropriate cleanup and then return so that 1263execution will return to JavaScript. As part of the transition back to 1264JavaScript, the exception will be thrown at the point in the JavaScript 1265code where the native method was invoked. The behavior of most Node-API calls 1266is unspecified while an exception is pending, and many will simply return 1267<code>napi_pending_exception</code>, so do as little as possible and then return to 1268JavaScript where the exception can be handled.</p> 1269<p>The second approach is to try to handle the exception. There will be cases 1270where the native code can catch the exception, take the appropriate action, 1271and then continue. This is only recommended in specific cases 1272where it is known that the exception can be safely handled. In these 1273cases <a href="#n_api_napi_get_and_clear_last_exception"><code>napi_get_and_clear_last_exception</code></a> can be used to get and 1274clear the exception. On success, result will contain the handle to 1275the last JavaScript <code>Object</code> thrown. If it is determined, after 1276retrieving the exception, the exception cannot be handled after all 1277it can be re-thrown it with <a href="#n_api_napi_throw"><code>napi_throw</code></a> where error is the 1278JavaScript value to be thrown.</p> 1279<p>The following utility functions are also available in case native code 1280needs to throw an exception or determine if a <code>napi_value</code> is an instance 1281of a JavaScript <code>Error</code> object: <a href="#n_api_napi_throw_error"><code>napi_throw_error</code></a>, 1282<a href="#n_api_napi_throw_type_error"><code>napi_throw_type_error</code></a>, <a href="#n_api_napi_throw_range_error"><code>napi_throw_range_error</code></a> and 1283<a href="#n_api_napi_is_error"><code>napi_is_error</code></a>.</p> 1284<p>The following utility functions are also available in case native 1285code needs to create an <code>Error</code> object: <a href="#n_api_napi_create_error"><code>napi_create_error</code></a>, 1286<a href="#n_api_napi_create_type_error"><code>napi_create_type_error</code></a>, and <a href="#n_api_napi_create_range_error"><code>napi_create_range_error</code></a>, 1287where result is the <code>napi_value</code> that refers to the newly created 1288JavaScript <code>Error</code> object.</p> 1289<p>The Node.js project is adding error codes to all of the errors 1290generated internally. The goal is for applications to use these 1291error codes for all error checking. The associated error messages 1292will remain, but will only be meant to be used for logging and 1293display with the expectation that the message can change without 1294SemVer applying. In order to support this model with Node-API, both 1295in internal functionality and for module specific functionality 1296(as its good practice), the <code>throw_</code> and <code>create_</code> functions 1297take an optional code parameter which is the string for the code 1298to be added to the error object. If the optional parameter is <code>NULL</code> 1299then no code will be associated with the error. If a code is provided, 1300the name associated with the error is also updated to be:</p> 1301<pre><code class="language-text">originalName [code]</code></pre> 1302<p>where <code>originalName</code> is the original name associated with the error 1303and <code>code</code> is the code that was provided. For example, if the code 1304is <code>'ERR_ERROR_1'</code> and a <code>TypeError</code> is being created the name will be:</p> 1305<pre><code class="language-text">TypeError [ERR_ERROR_1]</code></pre> 1306<h5>napi_throw<span><a class="mark" href="#n_api_napi_throw" id="n_api_napi_throw">#</a></span></h5> 1307<div class="api_metadata"> 1308<span>Added in: v8.0.0</span> 1309<span>N-API version: 1</span> 1310</div> 1311<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw</span><span class="hljs-params">(napi_env env, napi_value error)</span>;</code></pre> 1312<ul> 1313<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1314<li><code>[in] error</code>: The JavaScript value to be thrown.</li> 1315</ul> 1316<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1317<p>This API throws the JavaScript value provided.</p> 1318<h5>napi_throw_error<span><a class="mark" href="#n_api_napi_throw_error" id="n_api_napi_throw_error">#</a></span></h5> 1319<div class="api_metadata"> 1320<span>Added in: v8.0.0</span> 1321<span>N-API version: 1</span> 1322</div> 1323<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_error</span><span class="hljs-params">(napi_env env, 1324 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* code, 1325 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* msg)</span>;</code></pre> 1326<ul> 1327<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1328<li><code>[in] code</code>: Optional error code to be set on the error.</li> 1329<li><code>[in] msg</code>: C string representing the text to be associated with the error.</li> 1330</ul> 1331<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1332<p>This API throws a JavaScript <code>Error</code> with the text provided.</p> 1333<h5>napi_throw_type_error<span><a class="mark" href="#n_api_napi_throw_type_error" id="n_api_napi_throw_type_error">#</a></span></h5> 1334<div class="api_metadata"> 1335<span>Added in: v8.0.0</span> 1336<span>N-API version: 1</span> 1337</div> 1338<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_type_error</span><span class="hljs-params">(napi_env env, 1339 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* code, 1340 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* msg)</span>;</code></pre> 1341<ul> 1342<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1343<li><code>[in] code</code>: Optional error code to be set on the error.</li> 1344<li><code>[in] msg</code>: C string representing the text to be associated with the error.</li> 1345</ul> 1346<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1347<p>This API throws a JavaScript <code>TypeError</code> with the text provided.</p> 1348<h5>napi_throw_range_error<span><a class="mark" href="#n_api_napi_throw_range_error" id="n_api_napi_throw_range_error">#</a></span></h5> 1349<div class="api_metadata"> 1350<span>Added in: v8.0.0</span> 1351<span>N-API version: 1</span> 1352</div> 1353<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_range_error</span><span class="hljs-params">(napi_env env, 1354 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* code, 1355 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* msg)</span>;</code></pre> 1356<ul> 1357<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1358<li><code>[in] code</code>: Optional error code to be set on the error.</li> 1359<li><code>[in] msg</code>: C string representing the text to be associated with the error.</li> 1360</ul> 1361<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1362<p>This API throws a JavaScript <code>RangeError</code> with the text provided.</p> 1363<h5>napi_is_error<span><a class="mark" href="#n_api_napi_is_error" id="n_api_napi_is_error">#</a></span></h5> 1364<div class="api_metadata"> 1365<span>Added in: v8.0.0</span> 1366<span>N-API version: 1</span> 1367</div> 1368<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_is_error</span><span class="hljs-params">(napi_env env, 1369 napi_value value, 1370 <span class="hljs-type">bool</span>* result)</span>;</code></pre> 1371<ul> 1372<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1373<li><code>[in] value</code>: The <code>napi_value</code> to be checked.</li> 1374<li><code>[out] result</code>: Boolean value that is set to true if <code>napi_value</code> represents 1375an error, false otherwise.</li> 1376</ul> 1377<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1378<p>This API queries a <code>napi_value</code> to check if it represents an error object.</p> 1379<h5>napi_create_error<span><a class="mark" href="#n_api_napi_create_error" id="n_api_napi_create_error">#</a></span></h5> 1380<div class="api_metadata"> 1381<span>Added in: v8.0.0</span> 1382<span>N-API version: 1</span> 1383</div> 1384<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_error</span><span class="hljs-params">(napi_env env, 1385 napi_value code, 1386 napi_value msg, 1387 napi_value* result)</span>;</code></pre> 1388<ul> 1389<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1390<li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be 1391associated with the error.</li> 1392<li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as 1393the message for the <code>Error</code>.</li> 1394<li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li> 1395</ul> 1396<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1397<p>This API returns a JavaScript <code>Error</code> with the text provided.</p> 1398<h5>napi_create_type_error<span><a class="mark" href="#n_api_napi_create_type_error" id="n_api_napi_create_type_error">#</a></span></h5> 1399<div class="api_metadata"> 1400<span>Added in: v8.0.0</span> 1401<span>N-API version: 1</span> 1402</div> 1403<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_type_error</span><span class="hljs-params">(napi_env env, 1404 napi_value code, 1405 napi_value msg, 1406 napi_value* result)</span>;</code></pre> 1407<ul> 1408<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1409<li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be 1410associated with the error.</li> 1411<li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as 1412the message for the <code>Error</code>.</li> 1413<li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li> 1414</ul> 1415<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1416<p>This API returns a JavaScript <code>TypeError</code> with the text provided.</p> 1417<h5>napi_create_range_error<span><a class="mark" href="#n_api_napi_create_range_error" id="n_api_napi_create_range_error">#</a></span></h5> 1418<div class="api_metadata"> 1419<span>Added in: v8.0.0</span> 1420<span>N-API version: 1</span> 1421</div> 1422<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_range_error</span><span class="hljs-params">(napi_env env, 1423 napi_value code, 1424 napi_value msg, 1425 napi_value* result)</span>;</code></pre> 1426<ul> 1427<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1428<li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be 1429associated with the error.</li> 1430<li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as 1431the message for the <code>Error</code>.</li> 1432<li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li> 1433</ul> 1434<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1435<p>This API returns a JavaScript <code>RangeError</code> with the text provided.</p> 1436<h5>napi_get_and_clear_last_exception<span><a class="mark" href="#n_api_napi_get_and_clear_last_exception" id="n_api_napi_get_and_clear_last_exception">#</a></span></h5> 1437<div class="api_metadata"> 1438<span>Added in: v8.0.0</span> 1439<span>N-API version: 1</span> 1440</div> 1441<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_and_clear_last_exception</span><span class="hljs-params">(napi_env env, 1442 napi_value* result)</span>;</code></pre> 1443<ul> 1444<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1445<li><code>[out] result</code>: The exception if one is pending, <code>NULL</code> otherwise.</li> 1446</ul> 1447<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1448<p>This API can be called even if there is a pending JavaScript exception.</p> 1449<h5>napi_is_exception_pending<span><a class="mark" href="#n_api_napi_is_exception_pending" id="n_api_napi_is_exception_pending">#</a></span></h5> 1450<div class="api_metadata"> 1451<span>Added in: v8.0.0</span> 1452<span>N-API version: 1</span> 1453</div> 1454<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_exception_pending</span><span class="hljs-params">(napi_env env, <span class="hljs-type">bool</span>* result)</span>;</code></pre> 1455<ul> 1456<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1457<li><code>[out] result</code>: Boolean value that is set to true if an exception is pending.</li> 1458</ul> 1459<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1460<p>This API can be called even if there is a pending JavaScript exception.</p> 1461<h5>napi_fatal_exception<span><a class="mark" href="#n_api_napi_fatal_exception" id="n_api_napi_fatal_exception">#</a></span></h5> 1462<div class="api_metadata"> 1463<span>Added in: v9.10.0</span> 1464<span>N-API version: 3</span> 1465</div> 1466<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_fatal_exception</span><span class="hljs-params">(napi_env env, napi_value err)</span>;</code></pre> 1467<ul> 1468<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1469<li><code>[in] err</code>: The error that is passed to <code>'uncaughtException'</code>.</li> 1470</ul> 1471<p>Trigger an <code>'uncaughtException'</code> in JavaScript. Useful if an async 1472callback throws an exception with no way to recover.</p> 1473<h4>Fatal errors<span><a class="mark" href="#n_api_fatal_errors" id="n_api_fatal_errors">#</a></span></h4> 1474<p>In the event of an unrecoverable error in a native module, a fatal error can be 1475thrown to immediately terminate the process.</p> 1476<h5>napi_fatal_error<span><a class="mark" href="#n_api_napi_fatal_error" id="n_api_napi_fatal_error">#</a></span></h5> 1477<div class="api_metadata"> 1478<span>Added in: v8.2.0</span> 1479<span>N-API version: 1</span> 1480</div> 1481<pre><code class="language-c">NAPI_NO_RETURN <span class="hljs-type">void</span> <span class="hljs-title function_">napi_fatal_error</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* location, 1482 <span class="hljs-type">size_t</span> location_len, 1483 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* message, 1484 <span class="hljs-type">size_t</span> message_len)</span>;</code></pre> 1485<ul> 1486<li><code>[in] location</code>: Optional location at which the error occurred.</li> 1487<li><code>[in] location_len</code>: The length of the location in bytes, or 1488<code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li> 1489<li><code>[in] message</code>: The message associated with the error.</li> 1490<li><code>[in] message_len</code>: The length of the message in bytes, or <code>NAPI_AUTO_LENGTH</code> 1491if it is null-terminated.</li> 1492</ul> 1493<p>The function call does not return, the process will be terminated.</p> 1494<p>This API can be called even if there is a pending JavaScript exception.</p> 1495</section><section><h3>Object lifetime management<span><a class="mark" href="#n_api_object_lifetime_management" id="n_api_object_lifetime_management">#</a></span></h3> 1496<p>As Node-API calls are made, handles to objects in the heap for the underlying 1497VM may be returned as <code>napi_values</code>. These handles must hold the 1498objects 'live' until they are no longer required by the native code, 1499otherwise the objects could be collected before the native code was 1500finished using them.</p> 1501<p>As object handles are returned they are associated with a 1502'scope'. The lifespan for the default scope is tied to the lifespan 1503of the native method call. The result is that, by default, handles 1504remain valid and the objects associated with these handles will be 1505held live for the lifespan of the native method call.</p> 1506<p>In many cases, however, it is necessary that the handles remain valid for 1507either a shorter or longer lifespan than that of the native method. 1508The sections which follow describe the Node-API functions that can be used 1509to change the handle lifespan from the default.</p> 1510<h4>Making handle lifespan shorter than that of the native method<span><a class="mark" href="#n_api_making_handle_lifespan_shorter_than_that_of_the_native_method" id="n_api_making_handle_lifespan_shorter_than_that_of_the_native_method">#</a></span></h4> 1511<p>It is often necessary to make the lifespan of handles shorter than 1512the lifespan of a native method. For example, consider a native method 1513that has a loop which iterates through the elements in a large array:</p> 1514<pre><code class="language-c"><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">1000000</span>; i++) { 1515 napi_value result; 1516 napi_status status = napi_get_element(env, object, i, &result); 1517 <span class="hljs-keyword">if</span> (status != napi_ok) { 1518 <span class="hljs-keyword">break</span>; 1519 } 1520 <span class="hljs-comment">// do something with element</span> 1521}</code></pre> 1522<p>This would result in a large number of handles being created, consuming 1523substantial resources. In addition, even though the native code could only 1524use the most recent handle, all of the associated objects would also be 1525kept alive since they all share the same scope.</p> 1526<p>To handle this case, Node-API provides the ability to establish a new 'scope' to 1527which newly created handles will be associated. Once those handles 1528are no longer required, the scope can be 'closed' and any handles associated 1529with the scope are invalidated. The methods available to open/close scopes are 1530<a href="#n_api_napi_open_handle_scope"><code>napi_open_handle_scope</code></a> and <a href="#n_api_napi_close_handle_scope"><code>napi_close_handle_scope</code></a>.</p> 1531<p>Node-API only supports a single nested hierarchy of scopes. There is only one 1532active scope at any time, and all new handles will be associated with that 1533scope while it is active. Scopes must be closed in the reverse order from 1534which they are opened. In addition, all scopes created within a native method 1535must be closed before returning from that method.</p> 1536<p>Taking the earlier example, adding calls to <a href="#n_api_napi_open_handle_scope"><code>napi_open_handle_scope</code></a> and 1537<a href="#n_api_napi_close_handle_scope"><code>napi_close_handle_scope</code></a> would ensure that at most a single handle 1538is valid throughout the execution of the loop:</p> 1539<pre><code class="language-c"><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">1000000</span>; i++) { 1540 napi_handle_scope scope; 1541 napi_status status = napi_open_handle_scope(env, &scope); 1542 <span class="hljs-keyword">if</span> (status != napi_ok) { 1543 <span class="hljs-keyword">break</span>; 1544 } 1545 napi_value result; 1546 status = napi_get_element(env, object, i, &result); 1547 <span class="hljs-keyword">if</span> (status != napi_ok) { 1548 <span class="hljs-keyword">break</span>; 1549 } 1550 <span class="hljs-comment">// do something with element</span> 1551 status = napi_close_handle_scope(env, scope); 1552 <span class="hljs-keyword">if</span> (status != napi_ok) { 1553 <span class="hljs-keyword">break</span>; 1554 } 1555}</code></pre> 1556<p>When nesting scopes, there are cases where a handle from an 1557inner scope needs to live beyond the lifespan of that scope. Node-API supports 1558an 'escapable scope' in order to support this case. An escapable scope 1559allows one handle to be 'promoted' so that it 'escapes' the 1560current scope and the lifespan of the handle changes from the current 1561scope to that of the outer scope.</p> 1562<p>The methods available to open/close escapable scopes are 1563<a href="#n_api_napi_open_escapable_handle_scope"><code>napi_open_escapable_handle_scope</code></a> and 1564<a href="#n_api_napi_close_escapable_handle_scope"><code>napi_close_escapable_handle_scope</code></a>.</p> 1565<p>The request to promote a handle is made through <a href="#n_api_napi_escape_handle"><code>napi_escape_handle</code></a> which 1566can only be called once.</p> 1567<h5>napi_open_handle_scope<span><a class="mark" href="#n_api_napi_open_handle_scope" id="n_api_napi_open_handle_scope">#</a></span></h5> 1568<div class="api_metadata"> 1569<span>Added in: v8.0.0</span> 1570<span>N-API version: 1</span> 1571</div> 1572<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_open_handle_scope</span><span class="hljs-params">(napi_env env, 1573 napi_handle_scope* result)</span>;</code></pre> 1574<ul> 1575<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1576<li><code>[out] result</code>: <code>napi_value</code> representing the new scope.</li> 1577</ul> 1578<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1579<p>This API opens a new scope.</p> 1580<h5>napi_close_handle_scope<span><a class="mark" href="#n_api_napi_close_handle_scope" id="n_api_napi_close_handle_scope">#</a></span></h5> 1581<div class="api_metadata"> 1582<span>Added in: v8.0.0</span> 1583<span>N-API version: 1</span> 1584</div> 1585<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_close_handle_scope</span><span class="hljs-params">(napi_env env, 1586 napi_handle_scope scope)</span>;</code></pre> 1587<ul> 1588<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1589<li><code>[in] scope</code>: <code>napi_value</code> representing the scope to be closed.</li> 1590</ul> 1591<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1592<p>This API closes the scope passed in. Scopes must be closed in the 1593reverse order from which they were created.</p> 1594<p>This API can be called even if there is a pending JavaScript exception.</p> 1595<h5>napi_open_escapable_handle_scope<span><a class="mark" href="#n_api_napi_open_escapable_handle_scope" id="n_api_napi_open_escapable_handle_scope">#</a></span></h5> 1596<div class="api_metadata"> 1597<span>Added in: v8.0.0</span> 1598<span>N-API version: 1</span> 1599</div> 1600<pre><code class="language-c">NAPI_EXTERN napi_status 1601 <span class="hljs-title function_">napi_open_escapable_handle_scope</span><span class="hljs-params">(napi_env env, 1602 napi_handle_scope* result)</span>;</code></pre> 1603<ul> 1604<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1605<li><code>[out] result</code>: <code>napi_value</code> representing the new scope.</li> 1606</ul> 1607<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1608<p>This API opens a new scope from which one object can be promoted 1609to the outer scope.</p> 1610<h5>napi_close_escapable_handle_scope<span><a class="mark" href="#n_api_napi_close_escapable_handle_scope" id="n_api_napi_close_escapable_handle_scope">#</a></span></h5> 1611<div class="api_metadata"> 1612<span>Added in: v8.0.0</span> 1613<span>N-API version: 1</span> 1614</div> 1615<pre><code class="language-c">NAPI_EXTERN napi_status 1616 <span class="hljs-title function_">napi_close_escapable_handle_scope</span><span class="hljs-params">(napi_env env, 1617 napi_handle_scope scope)</span>;</code></pre> 1618<ul> 1619<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1620<li><code>[in] scope</code>: <code>napi_value</code> representing the scope to be closed.</li> 1621</ul> 1622<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1623<p>This API closes the scope passed in. Scopes must be closed in the 1624reverse order from which they were created.</p> 1625<p>This API can be called even if there is a pending JavaScript exception.</p> 1626<h5>napi_escape_handle<span><a class="mark" href="#n_api_napi_escape_handle" id="n_api_napi_escape_handle">#</a></span></h5> 1627<div class="api_metadata"> 1628<span>Added in: v8.0.0</span> 1629<span>N-API version: 1</span> 1630</div> 1631<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_escape_handle</span><span class="hljs-params">(napi_env env, 1632 napi_escapable_handle_scope scope, 1633 napi_value escapee, 1634 napi_value* result)</span>;</code></pre> 1635<ul> 1636<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1637<li><code>[in] scope</code>: <code>napi_value</code> representing the current scope.</li> 1638<li><code>[in] escapee</code>: <code>napi_value</code> representing the JavaScript <code>Object</code> to be 1639escaped.</li> 1640<li><code>[out] result</code>: <code>napi_value</code> representing the handle to the escaped <code>Object</code> 1641in the outer scope.</li> 1642</ul> 1643<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1644<p>This API promotes the handle to the JavaScript object so that it is valid 1645for the lifetime of the outer scope. It can only be called once per scope. 1646If it is called more than once an error will be returned.</p> 1647<p>This API can be called even if there is a pending JavaScript exception.</p> 1648<h4>References to objects with a lifespan longer than that of the native method<span><a class="mark" href="#n_api_references_to_objects_with_a_lifespan_longer_than_that_of_the_native_method" id="n_api_references_to_objects_with_a_lifespan_longer_than_that_of_the_native_method">#</a></span></h4> 1649<p>In some cases an addon will need to be able to create and reference objects 1650with a lifespan longer than that of a single native method invocation. For 1651example, to create a constructor and later use that constructor 1652in a request to creates instances, it must be possible to reference 1653the constructor object across many different instance creation requests. This 1654would not be possible with a normal handle returned as a <code>napi_value</code> as 1655described in the earlier section. The lifespan of a normal handle is 1656managed by scopes and all scopes must be closed before the end of a native 1657method.</p> 1658<p>Node-API provides methods to create persistent references to an object. 1659Each persistent reference has an associated count with a value of 0 1660or higher. The count determines if the reference will keep 1661the corresponding object live. References with a count of 0 do not 1662prevent the object from being collected and are often called 'weak' 1663references. Any count greater than 0 will prevent the object 1664from being collected.</p> 1665<p>References can be created with an initial reference count. The count can 1666then be modified through <a href="#n_api_napi_reference_ref"><code>napi_reference_ref</code></a> and 1667<a href="#n_api_napi_reference_unref"><code>napi_reference_unref</code></a>. If an object is collected while the count 1668for a reference is 0, all subsequent calls to 1669get the object associated with the reference <a href="#n_api_napi_get_reference_value"><code>napi_get_reference_value</code></a> 1670will return <code>NULL</code> for the returned <code>napi_value</code>. An attempt to call 1671<a href="#n_api_napi_reference_ref"><code>napi_reference_ref</code></a> for a reference whose object has been collected 1672results in an error.</p> 1673<p>References must be deleted once they are no longer required by the addon. When 1674a reference is deleted, it will no longer prevent the corresponding object from 1675being collected. Failure to delete a persistent reference results in 1676a 'memory leak' with both the native memory for the persistent reference and 1677the corresponding object on the heap being retained forever.</p> 1678<p>There can be multiple persistent references created which refer to the same 1679object, each of which will either keep the object live or not based on its 1680individual count.</p> 1681<h5>napi_create_reference<span><a class="mark" href="#n_api_napi_create_reference" id="n_api_napi_create_reference">#</a></span></h5> 1682<div class="api_metadata"> 1683<span>Added in: v8.0.0</span> 1684<span>N-API version: 1</span> 1685</div> 1686<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_reference</span><span class="hljs-params">(napi_env env, 1687 napi_value value, 1688 <span class="hljs-type">uint32_t</span> initial_refcount, 1689 napi_ref* result)</span>;</code></pre> 1690<ul> 1691<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1692<li><code>[in] value</code>: <code>napi_value</code> representing the <code>Object</code> to which we want a 1693reference.</li> 1694<li><code>[in] initial_refcount</code>: Initial reference count for the new reference.</li> 1695<li><code>[out] result</code>: <code>napi_ref</code> pointing to the new reference.</li> 1696</ul> 1697<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1698<p>This API create a new reference with the specified reference count 1699to the <code>Object</code> passed in.</p> 1700<h5>napi_delete_reference<span><a class="mark" href="#n_api_napi_delete_reference" id="n_api_napi_delete_reference">#</a></span></h5> 1701<div class="api_metadata"> 1702<span>Added in: v8.0.0</span> 1703<span>N-API version: 1</span> 1704</div> 1705<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_delete_reference</span><span class="hljs-params">(napi_env env, napi_ref ref)</span>;</code></pre> 1706<ul> 1707<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1708<li><code>[in] ref</code>: <code>napi_ref</code> to be deleted.</li> 1709</ul> 1710<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1711<p>This API deletes the reference passed in.</p> 1712<p>This API can be called even if there is a pending JavaScript exception.</p> 1713<h5>napi_reference_ref<span><a class="mark" href="#n_api_napi_reference_ref" id="n_api_napi_reference_ref">#</a></span></h5> 1714<div class="api_metadata"> 1715<span>Added in: v8.0.0</span> 1716<span>N-API version: 1</span> 1717</div> 1718<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_reference_ref</span><span class="hljs-params">(napi_env env, 1719 napi_ref ref, 1720 <span class="hljs-type">uint32_t</span>* result)</span>;</code></pre> 1721<ul> 1722<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1723<li><code>[in] ref</code>: <code>napi_ref</code> for which the reference count will be incremented.</li> 1724<li><code>[out] result</code>: The new reference count.</li> 1725</ul> 1726<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1727<p>This API increments the reference count for the reference 1728passed in and returns the resulting reference count.</p> 1729<h5>napi_reference_unref<span><a class="mark" href="#n_api_napi_reference_unref" id="n_api_napi_reference_unref">#</a></span></h5> 1730<div class="api_metadata"> 1731<span>Added in: v8.0.0</span> 1732<span>N-API version: 1</span> 1733</div> 1734<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_reference_unref</span><span class="hljs-params">(napi_env env, 1735 napi_ref ref, 1736 <span class="hljs-type">uint32_t</span>* result)</span>;</code></pre> 1737<ul> 1738<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1739<li><code>[in] ref</code>: <code>napi_ref</code> for which the reference count will be decremented.</li> 1740<li><code>[out] result</code>: The new reference count.</li> 1741</ul> 1742<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1743<p>This API decrements the reference count for the reference 1744passed in and returns the resulting reference count.</p> 1745<h5>napi_get_reference_value<span><a class="mark" href="#n_api_napi_get_reference_value" id="n_api_napi_get_reference_value">#</a></span></h5> 1746<div class="api_metadata"> 1747<span>Added in: v8.0.0</span> 1748<span>N-API version: 1</span> 1749</div> 1750<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_reference_value</span><span class="hljs-params">(napi_env env, 1751 napi_ref ref, 1752 napi_value* result)</span>;</code></pre> 1753<p>the <code>napi_value passed</code> in or out of these methods is a handle to the 1754object to which the reference is related.</p> 1755<ul> 1756<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1757<li><code>[in] ref</code>: <code>napi_ref</code> for which we requesting the corresponding <code>Object</code>.</li> 1758<li><code>[out] result</code>: The <code>napi_value</code> for the <code>Object</code> referenced by the 1759<code>napi_ref</code>.</li> 1760</ul> 1761<p>Returns <code>napi_ok</code> if the API succeeded.</p> 1762<p>If still valid, this API returns the <code>napi_value</code> representing the 1763JavaScript <code>Object</code> associated with the <code>napi_ref</code>. Otherwise, result 1764will be <code>NULL</code>.</p> 1765<h4>Cleanup on exit of the current Node.js instance<span><a class="mark" href="#n_api_cleanup_on_exit_of_the_current_node_js_instance" id="n_api_cleanup_on_exit_of_the_current_node_js_instance">#</a></span></h4> 1766<p>While a Node.js process typically releases all its resources when exiting, 1767embedders of Node.js, or future Worker support, may require addons to register 1768clean-up hooks that will be run once the current Node.js instance exits.</p> 1769<p>Node-API provides functions for registering and un-registering such callbacks. 1770When those callbacks are run, all resources that are being held by the addon 1771should be freed up.</p> 1772<h5>napi_add_env_cleanup_hook<span><a class="mark" href="#n_api_napi_add_env_cleanup_hook" id="n_api_napi_add_env_cleanup_hook">#</a></span></h5> 1773<div class="api_metadata"> 1774<span>Added in: v10.2.0</span> 1775<span>N-API version: 3</span> 1776</div> 1777<pre><code class="language-c">NODE_EXTERN napi_status <span class="hljs-title function_">napi_add_env_cleanup_hook</span><span class="hljs-params">(napi_env env, 1778 <span class="hljs-type">void</span> (*fun)(<span class="hljs-type">void</span>* arg), 1779 <span class="hljs-type">void</span>* arg)</span>;</code></pre> 1780<p>Registers <code>fun</code> as a function to be run with the <code>arg</code> parameter once the 1781current Node.js environment exits.</p> 1782<p>A function can safely be specified multiple times with different 1783<code>arg</code> values. In that case, it will be called multiple times as well. 1784Providing the same <code>fun</code> and <code>arg</code> values multiple times is not allowed 1785and will lead the process to abort.</p> 1786<p>The hooks will be called in reverse order, i.e. the most recently added one 1787will be called first.</p> 1788<p>Removing this hook can be done by using <a href="#n_api_napi_remove_env_cleanup_hook"><code>napi_remove_env_cleanup_hook</code></a>. 1789Typically, that happens when the resource for which this hook was added 1790is being torn down anyway.</p> 1791<p>For asynchronous cleanup, <a href="#n_api_napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a> is available.</p> 1792<h5>napi_remove_env_cleanup_hook<span><a class="mark" href="#n_api_napi_remove_env_cleanup_hook" id="n_api_napi_remove_env_cleanup_hook">#</a></span></h5> 1793<div class="api_metadata"> 1794<span>Added in: v10.2.0</span> 1795<span>N-API version: 3</span> 1796</div> 1797<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remove_env_cleanup_hook</span><span class="hljs-params">(napi_env env, 1798 <span class="hljs-type">void</span> (*fun)(<span class="hljs-type">void</span>* arg), 1799 <span class="hljs-type">void</span>* arg)</span>;</code></pre> 1800<p>Unregisters <code>fun</code> as a function to be run with the <code>arg</code> parameter once the 1801current Node.js environment exits. Both the argument and the function value 1802need to be exact matches.</p> 1803<p>The function must have originally been registered 1804with <code>napi_add_env_cleanup_hook</code>, otherwise the process will abort.</p> 1805<h5>napi_add_async_cleanup_hook<span><a class="mark" href="#n_api_napi_add_async_cleanup_hook" id="n_api_napi_add_async_cleanup_hook">#</a></span></h5> 1806<div class="api_metadata"> 1807<details class="changelog"><summary>History</summary> 1808<table> 1809<tbody><tr><th>Version</th><th>Changes</th></tr> 1810<tr><td>v14.10.0</td> 1811<td><p>Changed signature of the <code>hook</code> callback.</p></td></tr> 1812<tr><td>v14.8.0, v12.19.0</td> 1813<td><p><span>Added in: v14.8.0, v12.19.0</span></p></td></tr> 1814</tbody></table> 1815</details> 1816<span>N-API version: 8</span> 1817</div> 1818<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_add_async_cleanup_hook</span><span class="hljs-params">( 1819 napi_env env, 1820 napi_async_cleanup_hook hook, 1821 <span class="hljs-type">void</span>* arg, 1822 napi_async_cleanup_hook_handle* remove_handle)</span>;</code></pre> 1823<ul> 1824<li><code>[in] env</code>: The environment that the API is invoked under.</li> 1825<li><code>[in] hook</code>: The function pointer to call at environment teardown.</li> 1826<li><code>[in] arg</code>: The pointer to pass to <code>hook</code> when it gets called.</li> 1827<li><code>[out] remove_handle</code>: Optional handle that refers to the asynchronous cleanup 1828hook.</li> 1829</ul> 1830<p>Registers <code>hook</code>, which is a function of type <a href="#n_api_napi_async_cleanup_hook"><code>napi_async_cleanup_hook</code></a>, as 1831a function to be run with the <code>remove_handle</code> and <code>arg</code> parameters once the 1832current Node.js environment exits.</p> 1833<p>Unlike <a href="#n_api_napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a>, the hook is allowed to be asynchronous.</p> 1834<p>Otherwise, behavior generally matches that of <a href="#n_api_napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a>.</p> 1835<p>If <code>remove_handle</code> is not <code>NULL</code>, an opaque value will be stored in it 1836that must later be passed to <a href="#n_api_napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a>, 1837regardless of whether the hook has already been invoked. 1838Typically, that happens when the resource for which this hook was added 1839is being torn down anyway.</p> 1840<h5>napi_remove_async_cleanup_hook<span><a class="mark" href="#n_api_napi_remove_async_cleanup_hook" id="n_api_napi_remove_async_cleanup_hook">#</a></span></h5> 1841<div class="api_metadata"> 1842<details class="changelog"><summary>History</summary> 1843<table> 1844<tbody><tr><th>Version</th><th>Changes</th></tr> 1845<tr><td>v14.10.0</td> 1846<td><p>Removed <code>env</code> parameter.</p></td></tr> 1847<tr><td>v14.8.0</td> 1848<td><p><span>Added in: v14.8.0</span></p></td></tr> 1849</tbody></table> 1850</details> 1851</div> 1852<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remove_async_cleanup_hook</span><span class="hljs-params">( 1853 napi_async_cleanup_hook_handle remove_handle)</span>;</code></pre> 1854<ul> 1855<li><code>[in] remove_handle</code>: The handle to an asynchronous cleanup hook that was 1856created with <a href="#n_api_napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>.</li> 1857</ul> 1858<p>Unregisters the cleanup hook corresponding to <code>remove_handle</code>. This will prevent 1859the hook from being executed, unless it has already started executing. 1860This must be called on any <code>napi_async_cleanup_hook_handle</code> value obtained 1861from <a href="#n_api_napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>.</p> 1862</section><section><h3>Module registration<span><a class="mark" href="#n_api_module_registration" id="n_api_module_registration">#</a></span></h3> 1863<p>Node-API modules are registered in a manner similar to other modules 1864except that instead of using the <code>NODE_MODULE</code> macro the following 1865is used:</p> 1866<pre><code class="language-c">NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)</code></pre> 1867<p>The next difference is the signature for the <code>Init</code> method. For a Node-API 1868module it is as follows:</p> 1869<pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span>;</code></pre> 1870<p>The return value from <code>Init</code> is treated as the <code>exports</code> object for the module. 1871The <code>Init</code> method is passed an empty object via the <code>exports</code> parameter as a 1872convenience. If <code>Init</code> returns <code>NULL</code>, the parameter passed as <code>exports</code> is 1873exported by the module. Node-API modules cannot modify the <code>module</code> object but 1874can specify anything as the <code>exports</code> property of the module.</p> 1875<p>To add the method <code>hello</code> as a function so that it can be called as a method 1876provided by the addon:</p> 1877<pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> { 1878 napi_status status; 1879 napi_property_descriptor desc = { 1880 <span class="hljs-string">"hello"</span>, 1881 <span class="hljs-literal">NULL</span>, 1882 Method, 1883 <span class="hljs-literal">NULL</span>, 1884 <span class="hljs-literal">NULL</span>, 1885 <span class="hljs-literal">NULL</span>, 1886 napi_writable | napi_enumerable | napi_configurable, 1887 <span class="hljs-literal">NULL</span> 1888 }; 1889 status = napi_define_properties(env, exports, <span class="hljs-number">1</span>, &desc); 1890 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 1891 <span class="hljs-keyword">return</span> exports; 1892}</code></pre> 1893<p>To set a function to be returned by the <code>require()</code> for the addon:</p> 1894<pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> { 1895 napi_value method; 1896 napi_status status; 1897 status = napi_create_function(env, <span class="hljs-string">"exports"</span>, NAPI_AUTO_LENGTH, Method, <span class="hljs-literal">NULL</span>, &method); 1898 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 1899 <span class="hljs-keyword">return</span> method; 1900}</code></pre> 1901<p>To define a class so that new instances can be created (often used with 1902<a href="#n_api_object_wrap">Object wrap</a>):</p> 1903<pre><code class="language-c"><span class="hljs-comment">// <span class="hljs-doctag">NOTE:</span> partial example, not all referenced code is included</span> 1904napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> { 1905 napi_status status; 1906 napi_property_descriptor properties[] = { 1907 { <span class="hljs-string">"value"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, GetValue, SetValue, <span class="hljs-literal">NULL</span>, napi_writable | napi_configurable, <span class="hljs-literal">NULL</span> }, 1908 DECLARE_NAPI_METHOD(<span class="hljs-string">"plusOne"</span>, PlusOne), 1909 DECLARE_NAPI_METHOD(<span class="hljs-string">"multiply"</span>, Multiply), 1910 }; 1911 1912 napi_value cons; 1913 status = 1914 napi_define_class(env, <span class="hljs-string">"MyObject"</span>, New, <span class="hljs-literal">NULL</span>, <span class="hljs-number">3</span>, properties, &cons); 1915 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 1916 1917 status = napi_create_reference(env, cons, <span class="hljs-number">1</span>, &constructor); 1918 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 1919 1920 status = napi_set_named_property(env, exports, <span class="hljs-string">"MyObject"</span>, cons); 1921 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 1922 1923 <span class="hljs-keyword">return</span> exports; 1924}</code></pre> 1925<p>You can also use the <code>NAPI_MODULE_INIT</code> macro, which acts as a shorthand 1926for <code>NAPI_MODULE</code> and defining an <code>Init</code> function:</p> 1927<pre><code class="language-c">NAPI_MODULE_INIT() { 1928 napi_value answer; 1929 napi_status result; 1930 1931 status = napi_create_int64(env, <span class="hljs-number">42</span>, &answer); 1932 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 1933 1934 status = napi_set_named_property(env, exports, <span class="hljs-string">"answer"</span>, answer); 1935 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 1936 1937 <span class="hljs-keyword">return</span> exports; 1938}</code></pre> 1939<p>All Node-API addons are context-aware, meaning they may be loaded multiple 1940times. There are a few design considerations when declaring such a module. 1941The documentation on <a href="addons.html#addons_context_aware_addons">context-aware addons</a> provides more details.</p> 1942<p>The variables <code>env</code> and <code>exports</code> will be available inside the function body 1943following the macro invocation.</p> 1944<p>For more details on setting properties on objects, see the section on 1945<a href="#n_api_working_with_javascript_properties">Working with JavaScript properties</a>.</p> 1946<p>For more details on building addon modules in general, refer to the existing 1947API.</p> 1948</section><section><h3>Working with JavaScript values<span><a class="mark" href="#n_api_working_with_javascript_values" id="n_api_working_with_javascript_values">#</a></span></h3> 1949<p>Node-API exposes a set of APIs to create all types of JavaScript values. 1950Some of these types are documented under <a href="https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values">Section 6</a> 1951of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p> 1952<p>Fundamentally, these APIs are used to do one of the following:</p> 1953<ol> 1954<li>Create a new JavaScript object</li> 1955<li>Convert from a primitive C type to a Node-API value</li> 1956<li>Convert from Node-API value to a primitive C type</li> 1957<li>Get global instances including <code>undefined</code> and <code>null</code></li> 1958</ol> 1959<p>Node-API values are represented by the type <code>napi_value</code>. 1960Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>. 1961In some cases, the API does check the type of the <code>napi_value</code> up-front. 1962However, for better performance, it's better for the caller to make sure that 1963the <code>napi_value</code> in question is of the JavaScript type expected by the API.</p> 1964<h4>Enum types<span><a class="mark" href="#n_api_enum_types" id="n_api_enum_types">#</a></span></h4> 1965<h5>napi_key_collection_mode<span><a class="mark" href="#n_api_napi_key_collection_mode" id="n_api_napi_key_collection_mode">#</a></span></h5> 1966<div class="api_metadata"> 1967<span>Added in: v13.7.0, v10.20.0</span> 1968<span>N-API version: 6</span> 1969</div> 1970<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> 1971 napi_key_include_prototypes, 1972 napi_key_own_only 1973} napi_key_collection_mode;</code></pre> 1974<p>Describes the <code>Keys/Properties</code> filter enums:</p> 1975<p><code>napi_key_collection_mode</code> limits the range of collected properties.</p> 1976<p><code>napi_key_own_only</code> limits the collected properties to the given 1977object only. <code>napi_key_include_prototypes</code> will include all keys 1978of the objects's prototype chain as well.</p> 1979<h5>napi_key_filter<span><a class="mark" href="#n_api_napi_key_filter" id="n_api_napi_key_filter">#</a></span></h5> 1980<div class="api_metadata"> 1981<span>Added in: v13.7.0, v10.20.0</span> 1982<span>N-API version: 6</span> 1983</div> 1984<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> 1985 napi_key_all_properties = <span class="hljs-number">0</span>, 1986 napi_key_writable = <span class="hljs-number">1</span>, 1987 napi_key_enumerable = <span class="hljs-number">1</span> << <span class="hljs-number">1</span>, 1988 napi_key_configurable = <span class="hljs-number">1</span> << <span class="hljs-number">2</span>, 1989 napi_key_skip_strings = <span class="hljs-number">1</span> << <span class="hljs-number">3</span>, 1990 napi_key_skip_symbols = <span class="hljs-number">1</span> << <span class="hljs-number">4</span> 1991} napi_key_filter;</code></pre> 1992<p>Property filter bits. They can be or'ed to build a composite filter.</p> 1993<h5>napi_key_conversion<span><a class="mark" href="#n_api_napi_key_conversion" id="n_api_napi_key_conversion">#</a></span></h5> 1994<div class="api_metadata"> 1995<span>Added in: v13.7.0, v10.20.0</span> 1996<span>N-API version: 6</span> 1997</div> 1998<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> 1999 napi_key_keep_numbers, 2000 napi_key_numbers_to_strings 2001} napi_key_conversion;</code></pre> 2002<p><code>napi_key_numbers_to_strings</code> will convert integer indices to 2003strings. <code>napi_key_keep_numbers</code> will return numbers for integer 2004indices.</p> 2005<h5>napi_valuetype<span><a class="mark" href="#n_api_napi_valuetype" id="n_api_napi_valuetype">#</a></span></h5> 2006<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> 2007 <span class="hljs-comment">// ES6 types (corresponds to typeof)</span> 2008 napi_undefined, 2009 napi_null, 2010 napi_boolean, 2011 napi_number, 2012 napi_string, 2013 napi_symbol, 2014 napi_object, 2015 napi_function, 2016 napi_external, 2017 napi_bigint, 2018} napi_valuetype;</code></pre> 2019<p>Describes the type of a <code>napi_value</code>. This generally corresponds to the types 2020described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types">Section 6.1</a> of the ECMAScript Language Specification. 2021In addition to types in that section, <code>napi_valuetype</code> can also represent 2022<code>Function</code>s and <code>Object</code>s with external data.</p> 2023<p>A JavaScript value of type <code>napi_external</code> appears in JavaScript as a plain 2024object such that no properties can be set on it, and no prototype.</p> 2025<h5>napi_typedarray_type<span><a class="mark" href="#n_api_napi_typedarray_type" id="n_api_napi_typedarray_type">#</a></span></h5> 2026<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> 2027 napi_int8_array, 2028 napi_uint8_array, 2029 napi_uint8_clamped_array, 2030 napi_int16_array, 2031 napi_uint16_array, 2032 napi_int32_array, 2033 napi_uint32_array, 2034 napi_float32_array, 2035 napi_float64_array, 2036 napi_bigint64_array, 2037 napi_biguint64_array, 2038} napi_typedarray_type;</code></pre> 2039<p>This represents the underlying binary scalar datatype of the <code>TypedArray</code>. 2040Elements of this enum correspond to 2041<a href="https://tc39.github.io/ecma262/#sec-typedarray-objects">Section 22.2</a> of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p> 2042<h4>Object creation functions<span><a class="mark" href="#n_api_object_creation_functions" id="n_api_object_creation_functions">#</a></span></h4> 2043<h5>napi_create_array<span><a class="mark" href="#n_api_napi_create_array" id="n_api_napi_create_array">#</a></span></h5> 2044<div class="api_metadata"> 2045<span>Added in: v8.0.0</span> 2046<span>N-API version: 1</span> 2047</div> 2048<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_array</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code></pre> 2049<ul> 2050<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 2051<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Array</code>.</li> 2052</ul> 2053<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2054<p>This API returns a Node-API value corresponding to a JavaScript <code>Array</code> type. 2055JavaScript arrays are described in 2056<a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Language Specification.</p> 2057<h5>napi_create_array_with_length<span><a class="mark" href="#n_api_napi_create_array_with_length" id="n_api_napi_create_array_with_length">#</a></span></h5> 2058<div class="api_metadata"> 2059<span>Added in: v8.0.0</span> 2060<span>N-API version: 1</span> 2061</div> 2062<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_array_with_length</span><span class="hljs-params">(napi_env env, 2063 <span class="hljs-type">size_t</span> length, 2064 napi_value* result)</span></code></pre> 2065<ul> 2066<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2067<li><code>[in] length</code>: The initial length of the <code>Array</code>.</li> 2068<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Array</code>.</li> 2069</ul> 2070<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2071<p>This API returns a Node-API value corresponding to a JavaScript <code>Array</code> type. 2072The <code>Array</code>'s length property is set to the passed-in length parameter. 2073However, the underlying buffer is not guaranteed to be pre-allocated by the VM 2074when the array is created. That behavior is left to the underlying VM 2075implementation. If the buffer must be a contiguous block of memory that can be 2076directly read and/or written via C, consider using 2077<a href="#n_api_napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code></a>.</p> 2078<p>JavaScript arrays are described in 2079<a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Language Specification.</p> 2080<h5>napi_create_arraybuffer<span><a class="mark" href="#n_api_napi_create_arraybuffer" id="n_api_napi_create_arraybuffer">#</a></span></h5> 2081<div class="api_metadata"> 2082<span>Added in: v8.0.0</span> 2083<span>N-API version: 1</span> 2084</div> 2085<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_arraybuffer</span><span class="hljs-params">(napi_env env, 2086 <span class="hljs-type">size_t</span> byte_length, 2087 <span class="hljs-type">void</span>** data, 2088 napi_value* result)</span></code></pre> 2089<ul> 2090<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2091<li><code>[in] length</code>: The length in bytes of the array buffer to create.</li> 2092<li><code>[out] data</code>: Pointer to the underlying byte buffer of the <code>ArrayBuffer</code>.</li> 2093<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>ArrayBuffer</code>.</li> 2094</ul> 2095<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2096<p>This API returns a Node-API value corresponding to a JavaScript <code>ArrayBuffer</code>. 2097<code>ArrayBuffer</code>s are used to represent fixed-length binary data buffers. They are 2098normally used as a backing-buffer for <code>TypedArray</code> objects. 2099The <code>ArrayBuffer</code> allocated will have an underlying byte buffer whose size is 2100determined by the <code>length</code> parameter that's passed in. 2101The underlying buffer is optionally returned back to the caller in case the 2102caller wants to directly manipulate the buffer. This buffer can only be 2103written to directly from native code. To write to this buffer from JavaScript, 2104a typed array or <code>DataView</code> object would need to be created.</p> 2105<p>JavaScript <code>ArrayBuffer</code> objects are described in 2106<a href="https://tc39.github.io/ecma262/#sec-arraybuffer-objects">Section 24.1</a> of the ECMAScript Language Specification.</p> 2107<h5>napi_create_buffer<span><a class="mark" href="#n_api_napi_create_buffer" id="n_api_napi_create_buffer">#</a></span></h5> 2108<div class="api_metadata"> 2109<span>Added in: v8.0.0</span> 2110<span>N-API version: 1</span> 2111</div> 2112<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer</span><span class="hljs-params">(napi_env env, 2113 <span class="hljs-type">size_t</span> size, 2114 <span class="hljs-type">void</span>** data, 2115 napi_value* result)</span></code></pre> 2116<ul> 2117<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2118<li><code>[in] size</code>: Size in bytes of the underlying buffer.</li> 2119<li><code>[out] data</code>: Raw pointer to the underlying buffer.</li> 2120<li><code>[out] result</code>: A <code>napi_value</code> representing a <code>node::Buffer</code>.</li> 2121</ul> 2122<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2123<p>This API allocates a <code>node::Buffer</code> object. While this is still a 2124fully-supported data structure, in most cases using a <code>TypedArray</code> will suffice.</p> 2125<h5>napi_create_buffer_copy<span><a class="mark" href="#n_api_napi_create_buffer_copy" id="n_api_napi_create_buffer_copy">#</a></span></h5> 2126<div class="api_metadata"> 2127<span>Added in: v8.0.0</span> 2128<span>N-API version: 1</span> 2129</div> 2130<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer_copy</span><span class="hljs-params">(napi_env env, 2131 <span class="hljs-type">size_t</span> length, 2132 <span class="hljs-keyword">const</span> <span class="hljs-type">void</span>* data, 2133 <span class="hljs-type">void</span>** result_data, 2134 napi_value* result)</span></code></pre> 2135<ul> 2136<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2137<li><code>[in] size</code>: Size in bytes of the input buffer (should be the same as the size 2138of the new buffer).</li> 2139<li><code>[in] data</code>: Raw pointer to the underlying buffer to copy from.</li> 2140<li><code>[out] result_data</code>: Pointer to the new <code>Buffer</code>'s underlying data buffer.</li> 2141<li><code>[out] result</code>: A <code>napi_value</code> representing a <code>node::Buffer</code>.</li> 2142</ul> 2143<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2144<p>This API allocates a <code>node::Buffer</code> object and initializes it with data copied 2145from the passed-in buffer. While this is still a fully-supported data 2146structure, in most cases using a <code>TypedArray</code> will suffice.</p> 2147<h5>napi_create_date<span><a class="mark" href="#n_api_napi_create_date" id="n_api_napi_create_date">#</a></span></h5> 2148<div class="api_metadata"> 2149<span>Added in: v11.11.0, v10.17.0</span> 2150<span>N-API version: 5</span> 2151</div> 2152<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_date</span><span class="hljs-params">(napi_env env, 2153 <span class="hljs-type">double</span> time, 2154 napi_value* result)</span>;</code></pre> 2155<ul> 2156<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2157<li><code>[in] time</code>: ECMAScript time value in milliseconds since 01 January, 1970 UTC.</li> 2158<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Date</code>.</li> 2159</ul> 2160<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2161<p>This API does not observe leap seconds; they are ignored, as 2162ECMAScript aligns with POSIX time specification.</p> 2163<p>This API allocates a JavaScript <code>Date</code> object.</p> 2164<p>JavaScript <code>Date</code> objects are described in 2165<a href="https://tc39.github.io/ecma262/#sec-date-objects">Section 20.3</a> of the ECMAScript Language Specification.</p> 2166<h5>napi_create_external<span><a class="mark" href="#n_api_napi_create_external" id="n_api_napi_create_external">#</a></span></h5> 2167<div class="api_metadata"> 2168<span>Added in: v8.0.0</span> 2169<span>N-API version: 1</span> 2170</div> 2171<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external</span><span class="hljs-params">(napi_env env, 2172 <span class="hljs-type">void</span>* data, 2173 napi_finalize finalize_cb, 2174 <span class="hljs-type">void</span>* finalize_hint, 2175 napi_value* result)</span></code></pre> 2176<ul> 2177<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2178<li><code>[in] data</code>: Raw pointer to the external data.</li> 2179<li><code>[in] finalize_cb</code>: Optional callback to call when the external value is being 2180collected. <a href="#n_api_napi_finalize"><code>napi_finalize</code></a> provides more details.</li> 2181<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during 2182collection.</li> 2183<li><code>[out] result</code>: A <code>napi_value</code> representing an external value.</li> 2184</ul> 2185<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2186<p>This API allocates a JavaScript value with external data attached to it. This 2187is used to pass external data through JavaScript code, so it can be retrieved 2188later by native code using <a href="#n_api_napi_get_value_external"><code>napi_get_value_external</code></a>.</p> 2189<p>The API adds a <code>napi_finalize</code> callback which will be called when the JavaScript 2190object just created is ready for garbage collection. It is similar to 2191<code>napi_wrap()</code> except that:</p> 2192<ul> 2193<li>the native data cannot be retrieved later using <code>napi_unwrap()</code>,</li> 2194<li>nor can it be removed later using <code>napi_remove_wrap()</code>, and</li> 2195<li>the object created by the API can be used with <code>napi_wrap()</code>.</li> 2196</ul> 2197<p>The created value is not an object, and therefore does not support additional 2198properties. It is considered a distinct value type: calling <code>napi_typeof()</code> with 2199an external value yields <code>napi_external</code>.</p> 2200<h5>napi_create_external_arraybuffer<span><a class="mark" href="#n_api_napi_create_external_arraybuffer" id="n_api_napi_create_external_arraybuffer">#</a></span></h5> 2201<div class="api_metadata"> 2202<span>Added in: v8.0.0</span> 2203<span>N-API version: 1</span> 2204</div> 2205<pre><code class="language-c">napi_status 2206<span class="hljs-title function_">napi_create_external_arraybuffer</span><span class="hljs-params">(napi_env env, 2207 <span class="hljs-type">void</span>* external_data, 2208 <span class="hljs-type">size_t</span> byte_length, 2209 napi_finalize finalize_cb, 2210 <span class="hljs-type">void</span>* finalize_hint, 2211 napi_value* result)</span></code></pre> 2212<ul> 2213<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2214<li><code>[in] external_data</code>: Pointer to the underlying byte buffer of the 2215<code>ArrayBuffer</code>.</li> 2216<li><code>[in] byte_length</code>: The length in bytes of the underlying buffer.</li> 2217<li><code>[in] finalize_cb</code>: Optional callback to call when the <code>ArrayBuffer</code> is being 2218collected. <a href="#n_api_napi_finalize"><code>napi_finalize</code></a> provides more details.</li> 2219<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during 2220collection.</li> 2221<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>ArrayBuffer</code>.</li> 2222</ul> 2223<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2224<p>This API returns a Node-API value corresponding to a JavaScript <code>ArrayBuffer</code>. 2225The underlying byte buffer of the <code>ArrayBuffer</code> is externally allocated and 2226managed. The caller must ensure that the byte buffer remains valid until the 2227finalize callback is called.</p> 2228<p>The API adds a <code>napi_finalize</code> callback which will be called when the JavaScript 2229object just created is ready for garbage collection. It is similar to 2230<code>napi_wrap()</code> except that:</p> 2231<ul> 2232<li>the native data cannot be retrieved later using <code>napi_unwrap()</code>,</li> 2233<li>nor can it be removed later using <code>napi_remove_wrap()</code>, and</li> 2234<li>the object created by the API can be used with <code>napi_wrap()</code>.</li> 2235</ul> 2236<p>JavaScript <code>ArrayBuffer</code>s are described in 2237<a href="https://tc39.github.io/ecma262/#sec-arraybuffer-objects">Section 24.1</a> of the ECMAScript Language Specification.</p> 2238<h5>napi_create_external_buffer<span><a class="mark" href="#n_api_napi_create_external_buffer" id="n_api_napi_create_external_buffer">#</a></span></h5> 2239<div class="api_metadata"> 2240<span>Added in: v8.0.0</span> 2241<span>N-API version: 1</span> 2242</div> 2243<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external_buffer</span><span class="hljs-params">(napi_env env, 2244 <span class="hljs-type">size_t</span> length, 2245 <span class="hljs-type">void</span>* data, 2246 napi_finalize finalize_cb, 2247 <span class="hljs-type">void</span>* finalize_hint, 2248 napi_value* result)</span></code></pre> 2249<ul> 2250<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2251<li><code>[in] length</code>: Size in bytes of the input buffer (should be the same as the 2252size of the new buffer).</li> 2253<li><code>[in] data</code>: Raw pointer to the underlying buffer to expose to JavaScript.</li> 2254<li><code>[in] finalize_cb</code>: Optional callback to call when the <code>ArrayBuffer</code> is being 2255collected. <a href="#n_api_napi_finalize"><code>napi_finalize</code></a> provides more details.</li> 2256<li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during 2257collection.</li> 2258<li><code>[out] result</code>: A <code>napi_value</code> representing a <code>node::Buffer</code>.</li> 2259</ul> 2260<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2261<p>This API allocates a <code>node::Buffer</code> object and initializes it with data 2262backed by the passed in buffer. While this is still a fully-supported data 2263structure, in most cases using a <code>TypedArray</code> will suffice.</p> 2264<p>The API adds a <code>napi_finalize</code> callback which will be called when the JavaScript 2265object just created is ready for garbage collection. It is similar to 2266<code>napi_wrap()</code> except that:</p> 2267<ul> 2268<li>the native data cannot be retrieved later using <code>napi_unwrap()</code>,</li> 2269<li>nor can it be removed later using <code>napi_remove_wrap()</code>, and</li> 2270<li>the object created by the API can be used with <code>napi_wrap()</code>.</li> 2271</ul> 2272<p>For Node.js >=4 <code>Buffers</code> are <code>Uint8Array</code>s.</p> 2273<h5>napi_create_object<span><a class="mark" href="#n_api_napi_create_object" id="n_api_napi_create_object">#</a></span></h5> 2274<div class="api_metadata"> 2275<span>Added in: v8.0.0</span> 2276<span>N-API version: 1</span> 2277</div> 2278<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_object</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code></pre> 2279<ul> 2280<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2281<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Object</code>.</li> 2282</ul> 2283<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2284<p>This API allocates a default JavaScript <code>Object</code>. 2285It is the equivalent of doing <code>new Object()</code> in JavaScript.</p> 2286<p>The JavaScript <code>Object</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-object-type">Section 6.1.7</a> of the 2287ECMAScript Language Specification.</p> 2288<h5>napi_create_symbol<span><a class="mark" href="#n_api_napi_create_symbol" id="n_api_napi_create_symbol">#</a></span></h5> 2289<div class="api_metadata"> 2290<span>Added in: v8.0.0</span> 2291<span>N-API version: 1</span> 2292</div> 2293<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_symbol</span><span class="hljs-params">(napi_env env, 2294 napi_value description, 2295 napi_value* result)</span></code></pre> 2296<ul> 2297<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2298<li><code>[in] description</code>: Optional <code>napi_value</code> which refers to a JavaScript 2299<code>string</code> to be set as the description for the symbol.</li> 2300<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>symbol</code>.</li> 2301</ul> 2302<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2303<p>This API creates a JavaScript <code>symbol</code> value from a UTF8-encoded C string.</p> 2304<p>The JavaScript <code>symbol</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-symbol-objects">Section 19.4</a> 2305of the ECMAScript Language Specification.</p> 2306<h5>napi_create_typedarray<span><a class="mark" href="#n_api_napi_create_typedarray" id="n_api_napi_create_typedarray">#</a></span></h5> 2307<div class="api_metadata"> 2308<span>Added in: v8.0.0</span> 2309<span>N-API version: 1</span> 2310</div> 2311<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_typedarray</span><span class="hljs-params">(napi_env env, 2312 napi_typedarray_type type, 2313 <span class="hljs-type">size_t</span> length, 2314 napi_value arraybuffer, 2315 <span class="hljs-type">size_t</span> byte_offset, 2316 napi_value* result)</span></code></pre> 2317<ul> 2318<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2319<li><code>[in] type</code>: Scalar datatype of the elements within the <code>TypedArray</code>.</li> 2320<li><code>[in] length</code>: Number of elements in the <code>TypedArray</code>.</li> 2321<li><code>[in] arraybuffer</code>: <code>ArrayBuffer</code> underlying the typed array.</li> 2322<li><code>[in] byte_offset</code>: The byte offset within the <code>ArrayBuffer</code> from which to 2323start projecting the <code>TypedArray</code>.</li> 2324<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>TypedArray</code>.</li> 2325</ul> 2326<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2327<p>This API creates a JavaScript <code>TypedArray</code> object over an existing 2328<code>ArrayBuffer</code>. <code>TypedArray</code> objects provide an array-like view over an 2329underlying data buffer where each element has the same underlying binary scalar 2330datatype.</p> 2331<p>It's required that <code>(length * size_of_element) + byte_offset</code> should 2332be <= the size in bytes of the array passed in. If not, a <code>RangeError</code> exception 2333is raised.</p> 2334<p>JavaScript <code>TypedArray</code> objects are described in 2335<a href="https://tc39.github.io/ecma262/#sec-typedarray-objects">Section 22.2</a> of the ECMAScript Language Specification.</p> 2336<h5>napi_create_dataview<span><a class="mark" href="#n_api_napi_create_dataview" id="n_api_napi_create_dataview">#</a></span></h5> 2337<div class="api_metadata"> 2338<span>Added in: v8.3.0</span> 2339<span>N-API version: 1</span> 2340</div> 2341<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_dataview</span><span class="hljs-params">(napi_env env, 2342 <span class="hljs-type">size_t</span> byte_length, 2343 napi_value arraybuffer, 2344 <span class="hljs-type">size_t</span> byte_offset, 2345 napi_value* result)</span></code></pre> 2346<ul> 2347<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2348<li><code>[in] length</code>: Number of elements in the <code>DataView</code>.</li> 2349<li><code>[in] arraybuffer</code>: <code>ArrayBuffer</code> underlying the <code>DataView</code>.</li> 2350<li><code>[in] byte_offset</code>: The byte offset within the <code>ArrayBuffer</code> from which to 2351start projecting the <code>DataView</code>.</li> 2352<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>DataView</code>.</li> 2353</ul> 2354<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2355<p>This API creates a JavaScript <code>DataView</code> object over an existing <code>ArrayBuffer</code>. 2356<code>DataView</code> objects provide an array-like view over an underlying data buffer, 2357but one which allows items of different size and type in the <code>ArrayBuffer</code>.</p> 2358<p>It is required that <code>byte_length + byte_offset</code> is less than or equal to the 2359size in bytes of the array passed in. If not, a <code>RangeError</code> exception is 2360raised.</p> 2361<p>JavaScript <code>DataView</code> objects are described in 2362<a href="https://tc39.github.io/ecma262/#sec-dataview-objects">Section 24.3</a> of the ECMAScript Language Specification.</p> 2363<h4>Functions to convert from C types to Node-API<span><a class="mark" href="#n_api_functions_to_convert_from_c_types_to_node_api" id="n_api_functions_to_convert_from_c_types_to_node_api">#</a></span></h4> 2364<h5>napi_create_int32<span><a class="mark" href="#n_api_napi_create_int32" id="n_api_napi_create_int32">#</a></span></h5> 2365<div class="api_metadata"> 2366<span>Added in: v8.4.0</span> 2367<span>N-API version: 1</span> 2368</div> 2369<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_int32</span><span class="hljs-params">(napi_env env, <span class="hljs-type">int32_t</span> value, napi_value* result)</span></code></pre> 2370<ul> 2371<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2372<li><code>[in] value</code>: Integer value to be represented in JavaScript.</li> 2373<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li> 2374</ul> 2375<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2376<p>This API is used to convert from the C <code>int32_t</code> type to the JavaScript 2377<code>number</code> type.</p> 2378<p>The JavaScript <code>number</code> type is described in 2379<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a> of the ECMAScript Language Specification.</p> 2380<h5>napi_create_uint32<span><a class="mark" href="#n_api_napi_create_uint32" id="n_api_napi_create_uint32">#</a></span></h5> 2381<div class="api_metadata"> 2382<span>Added in: v8.4.0</span> 2383<span>N-API version: 1</span> 2384</div> 2385<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_uint32</span><span class="hljs-params">(napi_env env, <span class="hljs-type">uint32_t</span> value, napi_value* result)</span></code></pre> 2386<ul> 2387<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2388<li><code>[in] value</code>: Unsigned integer value to be represented in JavaScript.</li> 2389<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li> 2390</ul> 2391<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2392<p>This API is used to convert from the C <code>uint32_t</code> type to the JavaScript 2393<code>number</code> type.</p> 2394<p>The JavaScript <code>number</code> type is described in 2395<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a> of the ECMAScript Language Specification.</p> 2396<h5>napi_create_int64<span><a class="mark" href="#n_api_napi_create_int64" id="n_api_napi_create_int64">#</a></span></h5> 2397<div class="api_metadata"> 2398<span>Added in: v8.4.0</span> 2399<span>N-API version: 1</span> 2400</div> 2401<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_int64</span><span class="hljs-params">(napi_env env, <span class="hljs-type">int64_t</span> value, napi_value* result)</span></code></pre> 2402<ul> 2403<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2404<li><code>[in] value</code>: Integer value to be represented in JavaScript.</li> 2405<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li> 2406</ul> 2407<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2408<p>This API is used to convert from the C <code>int64_t</code> type to the JavaScript 2409<code>number</code> type.</p> 2410<p>The JavaScript <code>number</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a> 2411of the ECMAScript Language Specification. Note the complete range of <code>int64_t</code> 2412cannot be represented with full precision in JavaScript. Integer values 2413outside the range of <a href="https://tc39.github.io/ecma262/#sec-number.min_safe_integer"><code>Number.MIN_SAFE_INTEGER</code></a> <code>-(2**53 - 1)</code> - 2414<a href="https://tc39.github.io/ecma262/#sec-number.max_safe_integer"><code>Number.MAX_SAFE_INTEGER</code></a> <code>(2**53 - 1)</code> will lose precision.</p> 2415<h5>napi_create_double<span><a class="mark" href="#n_api_napi_create_double" id="n_api_napi_create_double">#</a></span></h5> 2416<div class="api_metadata"> 2417<span>Added in: v8.4.0</span> 2418<span>N-API version: 1</span> 2419</div> 2420<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_double</span><span class="hljs-params">(napi_env env, <span class="hljs-type">double</span> value, napi_value* result)</span></code></pre> 2421<ul> 2422<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2423<li><code>[in] value</code>: Double-precision value to be represented in JavaScript.</li> 2424<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li> 2425</ul> 2426<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2427<p>This API is used to convert from the C <code>double</code> type to the JavaScript 2428<code>number</code> type.</p> 2429<p>The JavaScript <code>number</code> type is described in 2430<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a> of the ECMAScript Language Specification.</p> 2431<h5>napi_create_bigint_int64<span><a class="mark" href="#n_api_napi_create_bigint_int64" id="n_api_napi_create_bigint_int64">#</a></span></h5> 2432<div class="api_metadata"> 2433<span>Added in: v10.7.0</span> 2434<span>N-API version: 6</span> 2435</div> 2436<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_int64</span><span class="hljs-params">(napi_env env, 2437 <span class="hljs-type">int64_t</span> value, 2438 napi_value* result)</span>;</code></pre> 2439<ul> 2440<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2441<li><code>[in] value</code>: Integer value to be represented in JavaScript.</li> 2442<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>BigInt</code>.</li> 2443</ul> 2444<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2445<p>This API converts the C <code>int64_t</code> type to the JavaScript <code>BigInt</code> type.</p> 2446<h5>napi_create_bigint_uint64<span><a class="mark" href="#n_api_napi_create_bigint_uint64" id="n_api_napi_create_bigint_uint64">#</a></span></h5> 2447<div class="api_metadata"> 2448<span>Added in: v10.7.0</span> 2449<span>N-API version: 6</span> 2450</div> 2451<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_uint64</span><span class="hljs-params">(napi_env env, 2452 <span class="hljs-type">uint64_t</span> value, 2453 napi_value* result)</span>;</code></pre> 2454<ul> 2455<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2456<li><code>[in] value</code>: Unsigned integer value to be represented in JavaScript.</li> 2457<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>BigInt</code>.</li> 2458</ul> 2459<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2460<p>This API converts the C <code>uint64_t</code> type to the JavaScript <code>BigInt</code> type.</p> 2461<h5>napi_create_bigint_words<span><a class="mark" href="#n_api_napi_create_bigint_words" id="n_api_napi_create_bigint_words">#</a></span></h5> 2462<div class="api_metadata"> 2463<span>Added in: v10.7.0</span> 2464<span>N-API version: 6</span> 2465</div> 2466<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_words</span><span class="hljs-params">(napi_env env, 2467 <span class="hljs-type">int</span> sign_bit, 2468 <span class="hljs-type">size_t</span> word_count, 2469 <span class="hljs-keyword">const</span> <span class="hljs-type">uint64_t</span>* words, 2470 napi_value* result)</span>;</code></pre> 2471<ul> 2472<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2473<li><code>[in] sign_bit</code>: Determines if the resulting <code>BigInt</code> will be positive or 2474negative.</li> 2475<li><code>[in] word_count</code>: The length of the <code>words</code> array.</li> 2476<li><code>[in] words</code>: An array of <code>uint64_t</code> little-endian 64-bit words.</li> 2477<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>BigInt</code>.</li> 2478</ul> 2479<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2480<p>This API converts an array of unsigned 64-bit words into a single <code>BigInt</code> 2481value.</p> 2482<p>The resulting <code>BigInt</code> is calculated as: (–1)<sup><code>sign_bit</code></sup> (<code>words[0]</code> 2483× (2<sup>64</sup>)<sup>0</sup> + <code>words[1]</code> × (2<sup>64</sup>)<sup>1</sup> + …)</p> 2484<h5>napi_create_string_latin1<span><a class="mark" href="#n_api_napi_create_string_latin1" id="n_api_napi_create_string_latin1">#</a></span></h5> 2485<div class="api_metadata"> 2486<span>Added in: v8.0.0</span> 2487<span>N-API version: 1</span> 2488</div> 2489<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_latin1</span><span class="hljs-params">(napi_env env, 2490 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* str, 2491 <span class="hljs-type">size_t</span> length, 2492 napi_value* result)</span>;</code></pre> 2493<ul> 2494<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2495<li><code>[in] str</code>: Character buffer representing an ISO-8859-1-encoded string.</li> 2496<li><code>[in] length</code>: The length of the string in bytes, or <code>NAPI_AUTO_LENGTH</code> if it 2497is null-terminated.</li> 2498<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li> 2499</ul> 2500<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2501<p>This API creates a JavaScript <code>string</code> value from an ISO-8859-1-encoded C 2502string. The native string is copied.</p> 2503<p>The JavaScript <code>string</code> type is described in 2504<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p> 2505<h5>napi_create_string_utf16<span><a class="mark" href="#n_api_napi_create_string_utf16" id="n_api_napi_create_string_utf16">#</a></span></h5> 2506<div class="api_metadata"> 2507<span>Added in: v8.0.0</span> 2508<span>N-API version: 1</span> 2509</div> 2510<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf16</span><span class="hljs-params">(napi_env env, 2511 <span class="hljs-keyword">const</span> <span class="hljs-type">char16_t</span>* str, 2512 <span class="hljs-type">size_t</span> length, 2513 napi_value* result)</span></code></pre> 2514<ul> 2515<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2516<li><code>[in] str</code>: Character buffer representing a UTF16-LE-encoded string.</li> 2517<li><code>[in] length</code>: The length of the string in two-byte code units, or 2518<code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li> 2519<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li> 2520</ul> 2521<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2522<p>This API creates a JavaScript <code>string</code> value from a UTF16-LE-encoded C string. 2523The native string is copied.</p> 2524<p>The JavaScript <code>string</code> type is described in 2525<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p> 2526<h5>napi_create_string_utf8<span><a class="mark" href="#n_api_napi_create_string_utf8" id="n_api_napi_create_string_utf8">#</a></span></h5> 2527<div class="api_metadata"> 2528<span>Added in: v8.0.0</span> 2529<span>N-API version: 1</span> 2530</div> 2531<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf8</span><span class="hljs-params">(napi_env env, 2532 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* str, 2533 <span class="hljs-type">size_t</span> length, 2534 napi_value* result)</span></code></pre> 2535<ul> 2536<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2537<li><code>[in] str</code>: Character buffer representing a UTF8-encoded string.</li> 2538<li><code>[in] length</code>: The length of the string in bytes, or <code>NAPI_AUTO_LENGTH</code> if it 2539is null-terminated.</li> 2540<li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li> 2541</ul> 2542<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2543<p>This API creates a JavaScript <code>string</code> value from a UTF8-encoded C string. 2544The native string is copied.</p> 2545<p>The JavaScript <code>string</code> type is described in 2546<a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p> 2547<h4>Functions to convert from Node-API to C types<span><a class="mark" href="#n_api_functions_to_convert_from_node_api_to_c_types" id="n_api_functions_to_convert_from_node_api_to_c_types">#</a></span></h4> 2548<h5>napi_get_array_length<span><a class="mark" href="#n_api_napi_get_array_length" id="n_api_napi_get_array_length">#</a></span></h5> 2549<div class="api_metadata"> 2550<span>Added in: v8.0.0</span> 2551<span>N-API version: 1</span> 2552</div> 2553<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_array_length</span><span class="hljs-params">(napi_env env, 2554 napi_value value, 2555 <span class="hljs-type">uint32_t</span>* result)</span></code></pre> 2556<ul> 2557<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2558<li><code>[in] value</code>: <code>napi_value</code> representing the JavaScript <code>Array</code> whose length is 2559being queried.</li> 2560<li><code>[out] result</code>: <code>uint32</code> representing length of the array.</li> 2561</ul> 2562<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2563<p>This API returns the length of an array.</p> 2564<p><code>Array</code> length is described in <a href="https://tc39.github.io/ecma262/#sec-properties-of-array-instances-length">Section 22.1.4.1</a> of the ECMAScript Language 2565Specification.</p> 2566<h5>napi_get_arraybuffer_info<span><a class="mark" href="#n_api_napi_get_arraybuffer_info" id="n_api_napi_get_arraybuffer_info">#</a></span></h5> 2567<div class="api_metadata"> 2568<span>Added in: v8.0.0</span> 2569<span>N-API version: 1</span> 2570</div> 2571<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_arraybuffer_info</span><span class="hljs-params">(napi_env env, 2572 napi_value arraybuffer, 2573 <span class="hljs-type">void</span>** data, 2574 <span class="hljs-type">size_t</span>* byte_length)</span></code></pre> 2575<ul> 2576<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2577<li><code>[in] arraybuffer</code>: <code>napi_value</code> representing the <code>ArrayBuffer</code> being queried.</li> 2578<li><code>[out] data</code>: The underlying data buffer of the <code>ArrayBuffer</code>. If byte_length 2579is <code>0</code>, this may be <code>NULL</code> or any other pointer value.</li> 2580<li><code>[out] byte_length</code>: Length in bytes of the underlying data buffer.</li> 2581</ul> 2582<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2583<p>This API is used to retrieve the underlying data buffer of an <code>ArrayBuffer</code> and 2584its length.</p> 2585<p><em>WARNING</em>: Use caution while using this API. The lifetime of the underlying data 2586buffer is managed by the <code>ArrayBuffer</code> even after it's returned. A 2587possible safe way to use this API is in conjunction with 2588<a href="#n_api_napi_create_reference"><code>napi_create_reference</code></a>, which can be used to guarantee control over the 2589lifetime of the <code>ArrayBuffer</code>. It's also safe to use the returned data buffer 2590within the same callback as long as there are no calls to other APIs that might 2591trigger a GC.</p> 2592<h5>napi_get_buffer_info<span><a class="mark" href="#n_api_napi_get_buffer_info" id="n_api_napi_get_buffer_info">#</a></span></h5> 2593<div class="api_metadata"> 2594<span>Added in: v8.0.0</span> 2595<span>N-API version: 1</span> 2596</div> 2597<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_buffer_info</span><span class="hljs-params">(napi_env env, 2598 napi_value value, 2599 <span class="hljs-type">void</span>** data, 2600 <span class="hljs-type">size_t</span>* length)</span></code></pre> 2601<ul> 2602<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2603<li><code>[in] value</code>: <code>napi_value</code> representing the <code>node::Buffer</code> being queried.</li> 2604<li><code>[out] data</code>: The underlying data buffer of the <code>node::Buffer</code>. 2605If length is <code>0</code>, this may be <code>NULL</code> or any other pointer value.</li> 2606<li><code>[out] length</code>: Length in bytes of the underlying data buffer.</li> 2607</ul> 2608<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2609<p>This API is used to retrieve the underlying data buffer of a <code>node::Buffer</code> 2610and it's length.</p> 2611<p><em>Warning</em>: Use caution while using this API since the underlying data buffer's 2612lifetime is not guaranteed if it's managed by the VM.</p> 2613<h5>napi_get_prototype<span><a class="mark" href="#n_api_napi_get_prototype" id="n_api_napi_get_prototype">#</a></span></h5> 2614<div class="api_metadata"> 2615<span>Added in: v8.0.0</span> 2616<span>N-API version: 1</span> 2617</div> 2618<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_prototype</span><span class="hljs-params">(napi_env env, 2619 napi_value object, 2620 napi_value* result)</span></code></pre> 2621<ul> 2622<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2623<li><code>[in] object</code>: <code>napi_value</code> representing JavaScript <code>Object</code> whose prototype 2624to return. This returns the equivalent of <code>Object.getPrototypeOf</code> (which is 2625not the same as the function's <code>prototype</code> property).</li> 2626<li><code>[out] result</code>: <code>napi_value</code> representing prototype of the given object.</li> 2627</ul> 2628<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2629<h5>napi_get_typedarray_info<span><a class="mark" href="#n_api_napi_get_typedarray_info" id="n_api_napi_get_typedarray_info">#</a></span></h5> 2630<div class="api_metadata"> 2631<span>Added in: v8.0.0</span> 2632<span>N-API version: 1</span> 2633</div> 2634<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_typedarray_info</span><span class="hljs-params">(napi_env env, 2635 napi_value typedarray, 2636 napi_typedarray_type* type, 2637 <span class="hljs-type">size_t</span>* length, 2638 <span class="hljs-type">void</span>** data, 2639 napi_value* arraybuffer, 2640 <span class="hljs-type">size_t</span>* byte_offset)</span></code></pre> 2641<ul> 2642<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2643<li><code>[in] typedarray</code>: <code>napi_value</code> representing the <code>TypedArray</code> whose 2644properties to query.</li> 2645<li><code>[out] type</code>: Scalar datatype of the elements within the <code>TypedArray</code>.</li> 2646<li><code>[out] length</code>: The number of elements in the <code>TypedArray</code>.</li> 2647<li><code>[out] data</code>: The data buffer underlying the <code>TypedArray</code> adjusted by 2648the <code>byte_offset</code> value so that it points to the first element in the 2649<code>TypedArray</code>. If the length of the array is <code>0</code>, this may be <code>NULL</code> or 2650any other pointer value.</li> 2651<li><code>[out] arraybuffer</code>: The <code>ArrayBuffer</code> underlying the <code>TypedArray</code>.</li> 2652<li><code>[out] byte_offset</code>: The byte offset within the underlying native array 2653at which the first element of the arrays is located. The value for the data 2654parameter has already been adjusted so that data points to the first element 2655in the array. Therefore, the first byte of the native array would be at 2656<code>data - byte_offset</code>.</li> 2657</ul> 2658<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2659<p>This API returns various properties of a typed array.</p> 2660<p><em>Warning</em>: Use caution while using this API since the underlying data buffer 2661is managed by the VM.</p> 2662<h5>napi_get_dataview_info<span><a class="mark" href="#n_api_napi_get_dataview_info" id="n_api_napi_get_dataview_info">#</a></span></h5> 2663<div class="api_metadata"> 2664<span>Added in: v8.3.0</span> 2665<span>N-API version: 1</span> 2666</div> 2667<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_dataview_info</span><span class="hljs-params">(napi_env env, 2668 napi_value dataview, 2669 <span class="hljs-type">size_t</span>* byte_length, 2670 <span class="hljs-type">void</span>** data, 2671 napi_value* arraybuffer, 2672 <span class="hljs-type">size_t</span>* byte_offset)</span></code></pre> 2673<ul> 2674<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2675<li><code>[in] dataview</code>: <code>napi_value</code> representing the <code>DataView</code> whose 2676properties to query.</li> 2677<li><code>[out] byte_length</code>: Number of bytes in the <code>DataView</code>.</li> 2678<li><code>[out] data</code>: The data buffer underlying the <code>DataView</code>. 2679If byte_length is <code>0</code>, this may be <code>NULL</code> or any other pointer value.</li> 2680<li><code>[out] arraybuffer</code>: <code>ArrayBuffer</code> underlying the <code>DataView</code>.</li> 2681<li><code>[out] byte_offset</code>: The byte offset within the data buffer from which 2682to start projecting the <code>DataView</code>.</li> 2683</ul> 2684<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2685<p>This API returns various properties of a <code>DataView</code>.</p> 2686<h5>napi_get_date_value<span><a class="mark" href="#n_api_napi_get_date_value" id="n_api_napi_get_date_value">#</a></span></h5> 2687<div class="api_metadata"> 2688<span>Added in: v11.11.0, v10.17.0</span> 2689<span>N-API version: 5</span> 2690</div> 2691<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_date_value</span><span class="hljs-params">(napi_env env, 2692 napi_value value, 2693 <span class="hljs-type">double</span>* result)</span></code></pre> 2694<ul> 2695<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2696<li><code>[in] value</code>: <code>napi_value</code> representing a JavaScript <code>Date</code>.</li> 2697<li><code>[out] result</code>: Time value as a <code>double</code> represented as milliseconds since 2698midnight at the beginning of 01 January, 1970 UTC.</li> 2699</ul> 2700<p>This API does not observe leap seconds; they are ignored, as 2701ECMAScript aligns with POSIX time specification.</p> 2702<p>Returns <code>napi_ok</code> if the API succeeded. If a non-date <code>napi_value</code> is passed 2703in it returns <code>napi_date_expected</code>.</p> 2704<p>This API returns the C double primitive of time value for the given JavaScript 2705<code>Date</code>.</p> 2706<h5>napi_get_value_bool<span><a class="mark" href="#n_api_napi_get_value_bool" id="n_api_napi_get_value_bool">#</a></span></h5> 2707<div class="api_metadata"> 2708<span>Added in: v8.0.0</span> 2709<span>N-API version: 1</span> 2710</div> 2711<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bool</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code></pre> 2712<ul> 2713<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2714<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>Boolean</code>.</li> 2715<li><code>[out] result</code>: C boolean primitive equivalent of the given JavaScript 2716<code>Boolean</code>.</li> 2717</ul> 2718<p>Returns <code>napi_ok</code> if the API succeeded. If a non-boolean <code>napi_value</code> is 2719passed in it returns <code>napi_boolean_expected</code>.</p> 2720<p>This API returns the C boolean primitive equivalent of the given JavaScript 2721<code>Boolean</code>.</p> 2722<h5>napi_get_value_double<span><a class="mark" href="#n_api_napi_get_value_double" id="n_api_napi_get_value_double">#</a></span></h5> 2723<div class="api_metadata"> 2724<span>Added in: v8.0.0</span> 2725<span>N-API version: 1</span> 2726</div> 2727<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_double</span><span class="hljs-params">(napi_env env, 2728 napi_value value, 2729 <span class="hljs-type">double</span>* result)</span></code></pre> 2730<ul> 2731<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2732<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li> 2733<li><code>[out] result</code>: C double primitive equivalent of the given JavaScript 2734<code>number</code>.</li> 2735</ul> 2736<p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code> is passed 2737in it returns <code>napi_number_expected</code>.</p> 2738<p>This API returns the C double primitive equivalent of the given JavaScript 2739<code>number</code>.</p> 2740<h5>napi_get_value_bigint_int64<span><a class="mark" href="#n_api_napi_get_value_bigint_int64" id="n_api_napi_get_value_bigint_int64">#</a></span></h5> 2741<div class="api_metadata"> 2742<span>Added in: v10.7.0</span> 2743<span>N-API version: 6</span> 2744</div> 2745<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_int64</span><span class="hljs-params">(napi_env env, 2746 napi_value value, 2747 <span class="hljs-type">int64_t</span>* result, 2748 <span class="hljs-type">bool</span>* lossless)</span>;</code></pre> 2749<ul> 2750<li><code>[in] env</code>: The environment that the API is invoked under</li> 2751<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>BigInt</code>.</li> 2752<li><code>[out] result</code>: C <code>int64_t</code> primitive equivalent of the given JavaScript 2753<code>BigInt</code>.</li> 2754<li><code>[out] lossless</code>: Indicates whether the <code>BigInt</code> value was converted 2755losslessly.</li> 2756</ul> 2757<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it 2758returns <code>napi_bigint_expected</code>.</p> 2759<p>This API returns the C <code>int64_t</code> primitive equivalent of the given JavaScript 2760<code>BigInt</code>. If needed it will truncate the value, setting <code>lossless</code> to <code>false</code>.</p> 2761<h5>napi_get_value_bigint_uint64<span><a class="mark" href="#n_api_napi_get_value_bigint_uint64" id="n_api_napi_get_value_bigint_uint64">#</a></span></h5> 2762<div class="api_metadata"> 2763<span>Added in: v10.7.0</span> 2764<span>N-API version: 6</span> 2765</div> 2766<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_uint64</span><span class="hljs-params">(napi_env env, 2767 napi_value value, 2768 <span class="hljs-type">uint64_t</span>* result, 2769 <span class="hljs-type">bool</span>* lossless)</span>;</code></pre> 2770<ul> 2771<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2772<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>BigInt</code>.</li> 2773<li><code>[out] result</code>: C <code>uint64_t</code> primitive equivalent of the given JavaScript 2774<code>BigInt</code>.</li> 2775<li><code>[out] lossless</code>: Indicates whether the <code>BigInt</code> value was converted 2776losslessly.</li> 2777</ul> 2778<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it 2779returns <code>napi_bigint_expected</code>.</p> 2780<p>This API returns the C <code>uint64_t</code> primitive equivalent of the given JavaScript 2781<code>BigInt</code>. If needed it will truncate the value, setting <code>lossless</code> to <code>false</code>.</p> 2782<h5>napi_get_value_bigint_words<span><a class="mark" href="#n_api_napi_get_value_bigint_words" id="n_api_napi_get_value_bigint_words">#</a></span></h5> 2783<div class="api_metadata"> 2784<span>Added in: v10.7.0</span> 2785<span>N-API version: 6</span> 2786</div> 2787<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_words</span><span class="hljs-params">(napi_env env, 2788 napi_value value, 2789 <span class="hljs-type">int</span>* sign_bit, 2790 <span class="hljs-type">size_t</span>* word_count, 2791 <span class="hljs-type">uint64_t</span>* words)</span>;</code></pre> 2792<ul> 2793<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2794<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>BigInt</code>.</li> 2795<li><code>[out] sign_bit</code>: Integer representing if the JavaScript <code>BigInt</code> is positive 2796or negative.</li> 2797<li><code>[in/out] word_count</code>: Must be initialized to the length of the <code>words</code> 2798array. Upon return, it will be set to the actual number of words that 2799would be needed to store this <code>BigInt</code>.</li> 2800<li><code>[out] words</code>: Pointer to a pre-allocated 64-bit word array.</li> 2801</ul> 2802<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2803<p>This API converts a single <code>BigInt</code> value into a sign bit, 64-bit little-endian 2804array, and the number of elements in the array. <code>sign_bit</code> and <code>words</code> may be 2805both set to <code>NULL</code>, in order to get only <code>word_count</code>.</p> 2806<h5>napi_get_value_external<span><a class="mark" href="#n_api_napi_get_value_external" id="n_api_napi_get_value_external">#</a></span></h5> 2807<div class="api_metadata"> 2808<span>Added in: v8.0.0</span> 2809<span>N-API version: 1</span> 2810</div> 2811<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_external</span><span class="hljs-params">(napi_env env, 2812 napi_value value, 2813 <span class="hljs-type">void</span>** result)</span></code></pre> 2814<ul> 2815<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2816<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript external value.</li> 2817<li><code>[out] result</code>: Pointer to the data wrapped by the JavaScript external value.</li> 2818</ul> 2819<p>Returns <code>napi_ok</code> if the API succeeded. If a non-external <code>napi_value</code> is 2820passed in it returns <code>napi_invalid_arg</code>.</p> 2821<p>This API retrieves the external data pointer that was previously passed to 2822<code>napi_create_external()</code>.</p> 2823<h5>napi_get_value_int32<span><a class="mark" href="#n_api_napi_get_value_int32" id="n_api_napi_get_value_int32">#</a></span></h5> 2824<div class="api_metadata"> 2825<span>Added in: v8.0.0</span> 2826<span>N-API version: 1</span> 2827</div> 2828<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int32</span><span class="hljs-params">(napi_env env, 2829 napi_value value, 2830 <span class="hljs-type">int32_t</span>* result)</span></code></pre> 2831<ul> 2832<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2833<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li> 2834<li><code>[out] result</code>: C <code>int32</code> primitive equivalent of the given JavaScript 2835<code>number</code>.</li> 2836</ul> 2837<p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code> 2838is passed in <code>napi_number_expected</code>.</p> 2839<p>This API returns the C <code>int32</code> primitive equivalent 2840of the given JavaScript <code>number</code>.</p> 2841<p>If the number exceeds the range of the 32 bit integer, then the result is 2842truncated to the equivalent of the bottom 32 bits. This can result in a large 2843positive number becoming a negative number if the value is > 2<sup>31</sup> - 1.</p> 2844<p>Non-finite number values (<code>NaN</code>, <code>+Infinity</code>, or <code>-Infinity</code>) set the 2845result to zero.</p> 2846<h5>napi_get_value_int64<span><a class="mark" href="#n_api_napi_get_value_int64" id="n_api_napi_get_value_int64">#</a></span></h5> 2847<div class="api_metadata"> 2848<span>Added in: v8.0.0</span> 2849<span>N-API version: 1</span> 2850</div> 2851<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int64</span><span class="hljs-params">(napi_env env, 2852 napi_value value, 2853 <span class="hljs-type">int64_t</span>* result)</span></code></pre> 2854<ul> 2855<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2856<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li> 2857<li><code>[out] result</code>: C <code>int64</code> primitive equivalent of the given JavaScript 2858<code>number</code>.</li> 2859</ul> 2860<p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code> 2861is passed in it returns <code>napi_number_expected</code>.</p> 2862<p>This API returns the C <code>int64</code> primitive equivalent of the given JavaScript 2863<code>number</code>.</p> 2864<p><code>number</code> values outside the range of <a href="https://tc39.github.io/ecma262/#sec-number.min_safe_integer"><code>Number.MIN_SAFE_INTEGER</code></a> 2865<code>-(2**53 - 1)</code> - <a href="https://tc39.github.io/ecma262/#sec-number.max_safe_integer"><code>Number.MAX_SAFE_INTEGER</code></a> <code>(2**53 - 1)</code> will lose 2866precision.</p> 2867<p>Non-finite number values (<code>NaN</code>, <code>+Infinity</code>, or <code>-Infinity</code>) set the 2868result to zero.</p> 2869<h5>napi_get_value_string_latin1<span><a class="mark" href="#n_api_napi_get_value_string_latin1" id="n_api_napi_get_value_string_latin1">#</a></span></h5> 2870<div class="api_metadata"> 2871<span>Added in: v8.0.0</span> 2872<span>N-API version: 1</span> 2873</div> 2874<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_latin1</span><span class="hljs-params">(napi_env env, 2875 napi_value value, 2876 <span class="hljs-type">char</span>* buf, 2877 <span class="hljs-type">size_t</span> bufsize, 2878 <span class="hljs-type">size_t</span>* result)</span></code></pre> 2879<ul> 2880<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2881<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript string.</li> 2882<li><code>[in] buf</code>: Buffer to write the ISO-8859-1-encoded string into. If <code>NULL</code> is 2883passed in, the length of the string in bytes and excluding the null terminator 2884is returned in <code>result</code>.</li> 2885<li><code>[in] bufsize</code>: Size of the destination buffer. When this value is 2886insufficient, the returned string is truncated and null-terminated.</li> 2887<li><code>[out] result</code>: Number of bytes copied into the buffer, excluding the null 2888terminator.</li> 2889</ul> 2890<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value</code> 2891is passed in it returns <code>napi_string_expected</code>.</p> 2892<p>This API returns the ISO-8859-1-encoded string corresponding the value passed 2893in.</p> 2894<h5>napi_get_value_string_utf8<span><a class="mark" href="#n_api_napi_get_value_string_utf8" id="n_api_napi_get_value_string_utf8">#</a></span></h5> 2895<div class="api_metadata"> 2896<span>Added in: v8.0.0</span> 2897<span>N-API version: 1</span> 2898</div> 2899<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_utf8</span><span class="hljs-params">(napi_env env, 2900 napi_value value, 2901 <span class="hljs-type">char</span>* buf, 2902 <span class="hljs-type">size_t</span> bufsize, 2903 <span class="hljs-type">size_t</span>* result)</span></code></pre> 2904<ul> 2905<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2906<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript string.</li> 2907<li><code>[in] buf</code>: Buffer to write the UTF8-encoded string into. If <code>NULL</code> is passed 2908in, the length of the string in bytes and excluding the null terminator is 2909returned in <code>result</code>.</li> 2910<li><code>[in] bufsize</code>: Size of the destination buffer. When this value is 2911insufficient, the returned string is truncated and null-terminated.</li> 2912<li><code>[out] result</code>: Number of bytes copied into the buffer, excluding the null 2913terminator.</li> 2914</ul> 2915<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value</code> 2916is passed in it returns <code>napi_string_expected</code>.</p> 2917<p>This API returns the UTF8-encoded string corresponding the value passed in.</p> 2918<h5>napi_get_value_string_utf16<span><a class="mark" href="#n_api_napi_get_value_string_utf16" id="n_api_napi_get_value_string_utf16">#</a></span></h5> 2919<div class="api_metadata"> 2920<span>Added in: v8.0.0</span> 2921<span>N-API version: 1</span> 2922</div> 2923<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_utf16</span><span class="hljs-params">(napi_env env, 2924 napi_value value, 2925 <span class="hljs-type">char16_t</span>* buf, 2926 <span class="hljs-type">size_t</span> bufsize, 2927 <span class="hljs-type">size_t</span>* result)</span></code></pre> 2928<ul> 2929<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2930<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript string.</li> 2931<li><code>[in] buf</code>: Buffer to write the UTF16-LE-encoded string into. If <code>NULL</code> is 2932passed in, the length of the string in 2-byte code units and excluding the 2933null terminator is returned.</li> 2934<li><code>[in] bufsize</code>: Size of the destination buffer. When this value is 2935insufficient, the returned string is truncated and null-terminated.</li> 2936<li><code>[out] result</code>: Number of 2-byte code units copied into the buffer, excluding 2937the null terminator.</li> 2938</ul> 2939<p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value</code> 2940is passed in it returns <code>napi_string_expected</code>.</p> 2941<p>This API returns the UTF16-encoded string corresponding the value passed in.</p> 2942<h5>napi_get_value_uint32<span><a class="mark" href="#n_api_napi_get_value_uint32" id="n_api_napi_get_value_uint32">#</a></span></h5> 2943<div class="api_metadata"> 2944<span>Added in: v8.0.0</span> 2945<span>N-API version: 1</span> 2946</div> 2947<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_uint32</span><span class="hljs-params">(napi_env env, 2948 napi_value value, 2949 <span class="hljs-type">uint32_t</span>* result)</span></code></pre> 2950<ul> 2951<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2952<li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li> 2953<li><code>[out] result</code>: C primitive equivalent of the given <code>napi_value</code> as a 2954<code>uint32_t</code>.</li> 2955</ul> 2956<p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code> 2957is passed in it returns <code>napi_number_expected</code>.</p> 2958<p>This API returns the C primitive equivalent of the given <code>napi_value</code> as a 2959<code>uint32_t</code>.</p> 2960<h4>Functions to get global instances<span><a class="mark" href="#n_api_functions_to_get_global_instances" id="n_api_functions_to_get_global_instances">#</a></span></h4> 2961<h5>napi_get_boolean<span><a class="mark" href="#n_api_napi_get_boolean" id="n_api_napi_get_boolean">#</a></span></h5> 2962<div class="api_metadata"> 2963<span>Added in: v8.0.0</span> 2964<span>N-API version: 1</span> 2965</div> 2966<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_boolean</span><span class="hljs-params">(napi_env env, <span class="hljs-type">bool</span> value, napi_value* result)</span></code></pre> 2967<ul> 2968<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2969<li><code>[in] value</code>: The value of the boolean to retrieve.</li> 2970<li><code>[out] result</code>: <code>napi_value</code> representing JavaScript <code>Boolean</code> singleton to 2971retrieve.</li> 2972</ul> 2973<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2974<p>This API is used to return the JavaScript singleton object that is used to 2975represent the given boolean value.</p> 2976<h5>napi_get_global<span><a class="mark" href="#n_api_napi_get_global" id="n_api_napi_get_global">#</a></span></h5> 2977<div class="api_metadata"> 2978<span>Added in: v8.0.0</span> 2979<span>N-API version: 1</span> 2980</div> 2981<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_global</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code></pre> 2982<ul> 2983<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2984<li><code>[out] result</code>: <code>napi_value</code> representing JavaScript <code>global</code> object.</li> 2985</ul> 2986<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2987<p>This API returns the <code>global</code> object.</p> 2988<h5>napi_get_null<span><a class="mark" href="#n_api_napi_get_null" id="n_api_napi_get_null">#</a></span></h5> 2989<div class="api_metadata"> 2990<span>Added in: v8.0.0</span> 2991<span>N-API version: 1</span> 2992</div> 2993<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_null</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code></pre> 2994<ul> 2995<li><code>[in] env</code>: The environment that the API is invoked under.</li> 2996<li><code>[out] result</code>: <code>napi_value</code> representing JavaScript <code>null</code> object.</li> 2997</ul> 2998<p>Returns <code>napi_ok</code> if the API succeeded.</p> 2999<p>This API returns the <code>null</code> object.</p> 3000<h5>napi_get_undefined<span><a class="mark" href="#n_api_napi_get_undefined" id="n_api_napi_get_undefined">#</a></span></h5> 3001<div class="api_metadata"> 3002<span>Added in: v8.0.0</span> 3003<span>N-API version: 1</span> 3004</div> 3005<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_undefined</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code></pre> 3006<ul> 3007<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3008<li><code>[out] result</code>: <code>napi_value</code> representing JavaScript Undefined value.</li> 3009</ul> 3010<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3011<p>This API returns the Undefined object.</p> 3012</section><section><h3>Working with JavaScript values and abstract operations<span><a class="mark" href="#n_api_working_with_javascript_values_and_abstract_operations" id="n_api_working_with_javascript_values_and_abstract_operations">#</a></span></h3> 3013<p>Node-API exposes a set of APIs to perform some abstract operations on JavaScript 3014values. Some of these operations are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Section 7</a> 3015of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p> 3016<p>These APIs support doing one of the following:</p> 3017<ol> 3018<li>Coerce JavaScript values to specific JavaScript types (such as <code>number</code> or 3019<code>string</code>).</li> 3020<li>Check the type of a JavaScript value.</li> 3021<li>Check for equality between two JavaScript values.</li> 3022</ol> 3023<h4>napi_coerce_to_bool<span><a class="mark" href="#n_api_napi_coerce_to_bool" id="n_api_napi_coerce_to_bool">#</a></span></h4> 3024<div class="api_metadata"> 3025<span>Added in: v8.0.0</span> 3026<span>N-API version: 1</span> 3027</div> 3028<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_bool</span><span class="hljs-params">(napi_env env, 3029 napi_value value, 3030 napi_value* result)</span></code></pre> 3031<ul> 3032<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3033<li><code>[in] value</code>: The JavaScript value to coerce.</li> 3034<li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>Boolean</code>.</li> 3035</ul> 3036<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3037<p>This API implements the abstract operation <code>ToBoolean()</code> as defined in 3038<a href="https://tc39.github.io/ecma262/#sec-toboolean">Section 7.1.2</a> of the ECMAScript Language Specification. 3039This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p> 3040<h4>napi_coerce_to_number<span><a class="mark" href="#n_api_napi_coerce_to_number" id="n_api_napi_coerce_to_number">#</a></span></h4> 3041<div class="api_metadata"> 3042<span>Added in: v8.0.0</span> 3043<span>N-API version: 1</span> 3044</div> 3045<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_number</span><span class="hljs-params">(napi_env env, 3046 napi_value value, 3047 napi_value* result)</span></code></pre> 3048<ul> 3049<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3050<li><code>[in] value</code>: The JavaScript value to coerce.</li> 3051<li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>number</code>.</li> 3052</ul> 3053<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3054<p>This API implements the abstract operation <code>ToNumber()</code> as defined in 3055<a href="https://tc39.github.io/ecma262/#sec-tonumber">Section 7.1.3</a> of the ECMAScript Language Specification. 3056This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p> 3057<h4>napi_coerce_to_object<span><a class="mark" href="#n_api_napi_coerce_to_object" id="n_api_napi_coerce_to_object">#</a></span></h4> 3058<div class="api_metadata"> 3059<span>Added in: v8.0.0</span> 3060<span>N-API version: 1</span> 3061</div> 3062<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_object</span><span class="hljs-params">(napi_env env, 3063 napi_value value, 3064 napi_value* result)</span></code></pre> 3065<ul> 3066<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3067<li><code>[in] value</code>: The JavaScript value to coerce.</li> 3068<li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>Object</code>.</li> 3069</ul> 3070<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3071<p>This API implements the abstract operation <code>ToObject()</code> as defined in 3072<a href="https://tc39.github.io/ecma262/#sec-toobject">Section 7.1.13</a> of the ECMAScript Language Specification. 3073This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p> 3074<h4>napi_coerce_to_string<span><a class="mark" href="#n_api_napi_coerce_to_string" id="n_api_napi_coerce_to_string">#</a></span></h4> 3075<div class="api_metadata"> 3076<span>Added in: v8.0.0</span> 3077<span>N-API version: 1</span> 3078</div> 3079<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_string</span><span class="hljs-params">(napi_env env, 3080 napi_value value, 3081 napi_value* result)</span></code></pre> 3082<ul> 3083<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3084<li><code>[in] value</code>: The JavaScript value to coerce.</li> 3085<li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>string</code>.</li> 3086</ul> 3087<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3088<p>This API implements the abstract operation <code>ToString()</code> as defined in 3089<a href="https://tc39.github.io/ecma262/#sec-toobject">Section 7.1.13</a> of the ECMAScript Language Specification. 3090This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p> 3091<h4>napi_typeof<span><a class="mark" href="#n_api_napi_typeof" id="n_api_napi_typeof">#</a></span></h4> 3092<div class="api_metadata"> 3093<span>Added in: v8.0.0</span> 3094<span>N-API version: 1</span> 3095</div> 3096<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_typeof</span><span class="hljs-params">(napi_env env, napi_value value, napi_valuetype* result)</span></code></pre> 3097<ul> 3098<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3099<li><code>[in] value</code>: The JavaScript value whose type to query.</li> 3100<li><code>[out] result</code>: The type of the JavaScript value.</li> 3101</ul> 3102<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3103<ul> 3104<li><code>napi_invalid_arg</code> if the type of <code>value</code> is not a known ECMAScript type and 3105<code>value</code> is not an External value.</li> 3106</ul> 3107<p>This API represents behavior similar to invoking the <code>typeof</code> Operator on 3108the object as defined in <a href="https://tc39.github.io/ecma262/#sec-typeof-operator">Section 12.5.5</a> of the ECMAScript Language 3109Specification. However, there are some differences:</p> 3110<ol> 3111<li>It has support for detecting an External value.</li> 3112<li>It detects <code>null</code> as a separate type, while ECMAScript <code>typeof</code> would detect 3113<code>object</code>.</li> 3114</ol> 3115<p>If <code>value</code> has a type that is invalid, an error is returned.</p> 3116<h4>napi_instanceof<span><a class="mark" href="#n_api_napi_instanceof" id="n_api_napi_instanceof">#</a></span></h4> 3117<div class="api_metadata"> 3118<span>Added in: v8.0.0</span> 3119<span>N-API version: 1</span> 3120</div> 3121<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_instanceof</span><span class="hljs-params">(napi_env env, 3122 napi_value object, 3123 napi_value constructor, 3124 <span class="hljs-type">bool</span>* result)</span></code></pre> 3125<ul> 3126<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3127<li><code>[in] object</code>: The JavaScript value to check.</li> 3128<li><code>[in] constructor</code>: The JavaScript function object of the constructor function 3129to check against.</li> 3130<li><code>[out] result</code>: Boolean that is set to true if <code>object instanceof constructor</code> 3131is true.</li> 3132</ul> 3133<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3134<p>This API represents invoking the <code>instanceof</code> Operator on the object as 3135defined in <a href="https://tc39.github.io/ecma262/#sec-instanceofoperator">Section 12.10.4</a> of the ECMAScript Language Specification.</p> 3136<h4>napi_is_array<span><a class="mark" href="#n_api_napi_is_array" id="n_api_napi_is_array">#</a></span></h4> 3137<div class="api_metadata"> 3138<span>Added in: v8.0.0</span> 3139<span>N-API version: 1</span> 3140</div> 3141<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_array</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code></pre> 3142<ul> 3143<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3144<li><code>[in] value</code>: The JavaScript value to check.</li> 3145<li><code>[out] result</code>: Whether the given object is an array.</li> 3146</ul> 3147<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3148<p>This API represents invoking the <code>IsArray</code> operation on the object 3149as defined in <a href="https://tc39.github.io/ecma262/#sec-isarray">Section 7.2.2</a> of the ECMAScript Language Specification.</p> 3150<h4>napi_is_arraybuffer<span><a class="mark" href="#n_api_napi_is_arraybuffer" id="n_api_napi_is_arraybuffer">#</a></span></h4> 3151<div class="api_metadata"> 3152<span>Added in: v8.0.0</span> 3153<span>N-API version: 1</span> 3154</div> 3155<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_arraybuffer</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code></pre> 3156<ul> 3157<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3158<li><code>[in] value</code>: The JavaScript value to check.</li> 3159<li><code>[out] result</code>: Whether the given object is an <code>ArrayBuffer</code>.</li> 3160</ul> 3161<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3162<p>This API checks if the <code>Object</code> passed in is an array buffer.</p> 3163<h4>napi_is_buffer<span><a class="mark" href="#n_api_napi_is_buffer" id="n_api_napi_is_buffer">#</a></span></h4> 3164<div class="api_metadata"> 3165<span>Added in: v8.0.0</span> 3166<span>N-API version: 1</span> 3167</div> 3168<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_buffer</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code></pre> 3169<ul> 3170<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3171<li><code>[in] value</code>: The JavaScript value to check.</li> 3172<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a <code>node::Buffer</code> 3173object.</li> 3174</ul> 3175<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3176<p>This API checks if the <code>Object</code> passed in is a buffer.</p> 3177<h4>napi_is_date<span><a class="mark" href="#n_api_napi_is_date" id="n_api_napi_is_date">#</a></span></h4> 3178<div class="api_metadata"> 3179<span>Added in: v11.11.0, v10.17.0</span> 3180<span>N-API version: 5</span> 3181</div> 3182<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_date</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code></pre> 3183<ul> 3184<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3185<li><code>[in] value</code>: The JavaScript value to check.</li> 3186<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a JavaScript <code>Date</code> 3187object.</li> 3188</ul> 3189<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3190<p>This API checks if the <code>Object</code> passed in is a date.</p> 3191<h4>napi_is_error<span><a class="mark" href="#n_api_napi_is_error_1" id="n_api_napi_is_error_1">#</a></span></h4> 3192<div class="api_metadata"> 3193<span>Added in: v8.0.0</span> 3194<span>N-API version: 1</span> 3195</div> 3196<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_error</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code></pre> 3197<ul> 3198<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3199<li><code>[in] value</code>: The JavaScript value to check.</li> 3200<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents an <code>Error</code> object.</li> 3201</ul> 3202<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3203<p>This API checks if the <code>Object</code> passed in is an <code>Error</code>.</p> 3204<h4>napi_is_typedarray<span><a class="mark" href="#n_api_napi_is_typedarray" id="n_api_napi_is_typedarray">#</a></span></h4> 3205<div class="api_metadata"> 3206<span>Added in: v8.0.0</span> 3207<span>N-API version: 1</span> 3208</div> 3209<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_typedarray</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code></pre> 3210<ul> 3211<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3212<li><code>[in] value</code>: The JavaScript value to check.</li> 3213<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a <code>TypedArray</code>.</li> 3214</ul> 3215<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3216<p>This API checks if the <code>Object</code> passed in is a typed array.</p> 3217<h4>napi_is_dataview<span><a class="mark" href="#n_api_napi_is_dataview" id="n_api_napi_is_dataview">#</a></span></h4> 3218<div class="api_metadata"> 3219<span>Added in: v8.3.0</span> 3220<span>N-API version: 1</span> 3221</div> 3222<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_dataview</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code></pre> 3223<ul> 3224<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3225<li><code>[in] value</code>: The JavaScript value to check.</li> 3226<li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a <code>DataView</code>.</li> 3227</ul> 3228<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3229<p>This API checks if the <code>Object</code> passed in is a <code>DataView</code>.</p> 3230<h4>napi_strict_equals<span><a class="mark" href="#n_api_napi_strict_equals" id="n_api_napi_strict_equals">#</a></span></h4> 3231<div class="api_metadata"> 3232<span>Added in: v8.0.0</span> 3233<span>N-API version: 1</span> 3234</div> 3235<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_strict_equals</span><span class="hljs-params">(napi_env env, 3236 napi_value lhs, 3237 napi_value rhs, 3238 <span class="hljs-type">bool</span>* result)</span></code></pre> 3239<ul> 3240<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3241<li><code>[in] lhs</code>: The JavaScript value to check.</li> 3242<li><code>[in] rhs</code>: The JavaScript value to check against.</li> 3243<li><code>[out] result</code>: Whether the two <code>napi_value</code> objects are equal.</li> 3244</ul> 3245<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3246<p>This API represents the invocation of the Strict Equality algorithm as 3247defined in <a href="https://tc39.github.io/ecma262/#sec-strict-equality-comparison">Section 7.2.14</a> of the ECMAScript Language Specification.</p> 3248<h4>napi_detach_arraybuffer<span><a class="mark" href="#n_api_napi_detach_arraybuffer" id="n_api_napi_detach_arraybuffer">#</a></span></h4> 3249<div class="api_metadata"> 3250<span>Added in: v13.0.0, v12.16.0, v10.22.0</span> 3251<span>N-API version: 7</span> 3252</div> 3253<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_detach_arraybuffer</span><span class="hljs-params">(napi_env env, 3254 napi_value arraybuffer)</span></code></pre> 3255<ul> 3256<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3257<li><code>[in] arraybuffer</code>: The JavaScript <code>ArrayBuffer</code> to be detached.</li> 3258</ul> 3259<p>Returns <code>napi_ok</code> if the API succeeded. If a non-detachable <code>ArrayBuffer</code> is 3260passed in it returns <code>napi_detachable_arraybuffer_expected</code>.</p> 3261<p>Generally, an <code>ArrayBuffer</code> is non-detachable if it has been detached before. 3262The engine may impose additional conditions on whether an <code>ArrayBuffer</code> is 3263detachable. For example, V8 requires that the <code>ArrayBuffer</code> be external, 3264that is, created with <a href="#n_api_napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code></a>.</p> 3265<p>This API represents the invocation of the <code>ArrayBuffer</code> detach operation as 3266defined in <a href="https://tc39.es/ecma262/#sec-detacharraybuffer">Section 24.1.1.3</a> of the ECMAScript Language Specification.</p> 3267<h4>napi_is_detached_arraybuffer<span><a class="mark" href="#n_api_napi_is_detached_arraybuffer" id="n_api_napi_is_detached_arraybuffer">#</a></span></h4> 3268<div class="api_metadata"> 3269<span>Added in: v13.3.0, v12.16.0, v10.22.0</span> 3270<span>N-API version: 7</span> 3271</div> 3272<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_detached_arraybuffer</span><span class="hljs-params">(napi_env env, 3273 napi_value arraybuffer, 3274 <span class="hljs-type">bool</span>* result)</span></code></pre> 3275<ul> 3276<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3277<li><code>[in] arraybuffer</code>: The JavaScript <code>ArrayBuffer</code> to be checked.</li> 3278<li><code>[out] result</code>: Whether the <code>arraybuffer</code> is detached.</li> 3279</ul> 3280<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3281<p>The <code>ArrayBuffer</code> is considered detached if its internal data is <code>null</code>.</p> 3282<p>This API represents the invocation of the <code>ArrayBuffer</code> <code>IsDetachedBuffer</code> 3283operation as defined in <a href="https://tc39.es/ecma262/#sec-isdetachedbuffer">Section 24.1.1.2</a> of the ECMAScript Language 3284Specification.</p> 3285</section><section><h3>Working with JavaScript properties<span><a class="mark" href="#n_api_working_with_javascript_properties" id="n_api_working_with_javascript_properties">#</a></span></h3> 3286<p>Node-API exposes a set of APIs to get and set properties on JavaScript 3287objects. Some of these types are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Section 7</a> of the 3288<a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p> 3289<p>Properties in JavaScript are represented as a tuple of a key and a value. 3290Fundamentally, all property keys in Node-API can be represented in one of the 3291following forms:</p> 3292<ul> 3293<li>Named: a simple UTF8-encoded string</li> 3294<li>Integer-Indexed: an index value represented by <code>uint32_t</code></li> 3295<li>JavaScript value: these are represented in Node-API by <code>napi_value</code>. This can 3296be a <code>napi_value</code> representing a <code>string</code>, <code>number</code>, or <code>symbol</code>.</li> 3297</ul> 3298<p>Node-API values are represented by the type <code>napi_value</code>. 3299Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>. 3300However, it's the caller's responsibility to make sure that the 3301<code>napi_value</code> in question is of the JavaScript type expected by the API.</p> 3302<p>The APIs documented in this section provide a simple interface to 3303get and set properties on arbitrary JavaScript objects represented by 3304<code>napi_value</code>.</p> 3305<p>For instance, consider the following JavaScript code snippet:</p> 3306<pre><code class="language-js"><span class="hljs-keyword">const</span> obj = {}; 3307obj.<span class="hljs-property">myProp</span> = <span class="hljs-number">123</span>;</code></pre> 3308<p>The equivalent can be done using Node-API values with the following snippet:</p> 3309<pre><code class="language-c">napi_status status = napi_generic_failure; 3310 3311<span class="hljs-comment">// const obj = {}</span> 3312napi_value obj, value; 3313status = napi_create_object(env, &obj); 3314<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status; 3315 3316<span class="hljs-comment">// Create a napi_value for 123</span> 3317status = napi_create_int32(env, <span class="hljs-number">123</span>, &value); 3318<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status; 3319 3320<span class="hljs-comment">// obj.myProp = 123</span> 3321status = napi_set_named_property(env, obj, <span class="hljs-string">"myProp"</span>, value); 3322<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code></pre> 3323<p>Indexed properties can be set in a similar manner. Consider the following 3324JavaScript snippet:</p> 3325<pre><code class="language-js"><span class="hljs-keyword">const</span> arr = []; 3326arr[<span class="hljs-number">123</span>] = <span class="hljs-string">'hello'</span>;</code></pre> 3327<p>The equivalent can be done using Node-API values with the following snippet:</p> 3328<pre><code class="language-c">napi_status status = napi_generic_failure; 3329 3330<span class="hljs-comment">// const arr = [];</span> 3331napi_value arr, value; 3332status = napi_create_array(env, &arr); 3333<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status; 3334 3335<span class="hljs-comment">// Create a napi_value for 'hello'</span> 3336status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &value); 3337<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status; 3338 3339<span class="hljs-comment">// arr[123] = 'hello';</span> 3340status = napi_set_element(env, arr, <span class="hljs-number">123</span>, value); 3341<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code></pre> 3342<p>Properties can be retrieved using the APIs described in this section. 3343Consider the following JavaScript snippet:</p> 3344<pre><code class="language-js"><span class="hljs-keyword">const</span> arr = []; 3345<span class="hljs-keyword">const</span> value = arr[<span class="hljs-number">123</span>];</code></pre> 3346<p>The following is the approximate equivalent of the Node-API counterpart:</p> 3347<pre><code class="language-c">napi_status status = napi_generic_failure; 3348 3349<span class="hljs-comment">// const arr = []</span> 3350napi_value arr, value; 3351status = napi_create_array(env, &arr); 3352<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status; 3353 3354<span class="hljs-comment">// const value = arr[123]</span> 3355status = napi_get_element(env, arr, <span class="hljs-number">123</span>, &value); 3356<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code></pre> 3357<p>Finally, multiple properties can also be defined on an object for performance 3358reasons. Consider the following JavaScript:</p> 3359<pre><code class="language-js"><span class="hljs-keyword">const</span> obj = {}; 3360<span class="hljs-title class_">Object</span>.<span class="hljs-title function_">defineProperties</span>(obj, { 3361 <span class="hljs-string">'foo'</span>: { <span class="hljs-attr">value</span>: <span class="hljs-number">123</span>, <span class="hljs-attr">writable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">configurable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">enumerable</span>: <span class="hljs-literal">true</span> }, 3362 <span class="hljs-string">'bar'</span>: { <span class="hljs-attr">value</span>: <span class="hljs-number">456</span>, <span class="hljs-attr">writable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">configurable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">enumerable</span>: <span class="hljs-literal">true</span> } 3363});</code></pre> 3364<p>The following is the approximate equivalent of the Node-API counterpart:</p> 3365<pre><code class="language-c">napi_status status = napi_status_generic_failure; 3366 3367<span class="hljs-comment">// const obj = {};</span> 3368napi_value obj; 3369status = napi_create_object(env, &obj); 3370<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status; 3371 3372<span class="hljs-comment">// Create napi_values for 123 and 456</span> 3373napi_value fooValue, barValue; 3374status = napi_create_int32(env, <span class="hljs-number">123</span>, &fooValue); 3375<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status; 3376status = napi_create_int32(env, <span class="hljs-number">456</span>, &barValue); 3377<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status; 3378 3379<span class="hljs-comment">// Set the properties</span> 3380napi_property_descriptor descriptors[] = { 3381 { <span class="hljs-string">"foo"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, fooValue, napi_writable | napi_configurable, <span class="hljs-literal">NULL</span> }, 3382 { <span class="hljs-string">"bar"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, barValue, napi_writable | napi_configurable, <span class="hljs-literal">NULL</span> } 3383} 3384status = napi_define_properties(env, 3385 obj, 3386 <span class="hljs-keyword">sizeof</span>(descriptors) / <span class="hljs-keyword">sizeof</span>(descriptors[<span class="hljs-number">0</span>]), 3387 descriptors); 3388<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code></pre> 3389<h4>Structures<span><a class="mark" href="#n_api_structures" id="n_api_structures">#</a></span></h4> 3390<h5>napi_property_attributes<span><a class="mark" href="#n_api_napi_property_attributes" id="n_api_napi_property_attributes">#</a></span></h5> 3391<div class="api_metadata"> 3392<details class="changelog"><summary>History</summary> 3393<table> 3394<tbody><tr><th>Version</th><th>Changes</th></tr> 3395<tr><td>v14.12.0</td> 3396<td><p>added <code>napi_default_method</code> and <code>napi_default_property</code>.</p></td></tr> 3397</tbody></table> 3398</details> 3399</div> 3400<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> 3401 napi_default = <span class="hljs-number">0</span>, 3402 napi_writable = <span class="hljs-number">1</span> << <span class="hljs-number">0</span>, 3403 napi_enumerable = <span class="hljs-number">1</span> << <span class="hljs-number">1</span>, 3404 napi_configurable = <span class="hljs-number">1</span> << <span class="hljs-number">2</span>, 3405 3406 <span class="hljs-comment">// Used with napi_define_class to distinguish static properties</span> 3407 <span class="hljs-comment">// from instance properties. Ignored by napi_define_properties.</span> 3408 napi_static = <span class="hljs-number">1</span> << <span class="hljs-number">10</span>, 3409 3410 <span class="hljs-comment">// Default for class methods.</span> 3411 napi_default_method = napi_writable | napi_configurable, 3412 3413 <span class="hljs-comment">// Default for object properties, like in JS obj[prop].</span> 3414 napi_default_jsproperty = napi_writable | 3415 napi_enumerable | 3416 napi_configurable, 3417} napi_property_attributes;</code></pre> 3418<p><code>napi_property_attributes</code> are flags used to control the behavior of properties 3419set on a JavaScript object. Other than <code>napi_static</code> they correspond to the 3420attributes listed in <a href="https://tc39.github.io/ecma262/#table-2">Section 6.1.7.1</a> 3421of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>. 3422They can be one or more of the following bitflags:</p> 3423<ul> 3424<li><code>napi_default</code>: No explicit attributes are set on the property. By default, a 3425property is read only, not enumerable and not configurable.</li> 3426<li><code>napi_writable</code>: The property is writable.</li> 3427<li><code>napi_enumerable</code>: The property is enumerable.</li> 3428<li><code>napi_configurable</code>: The property is configurable as defined in 3429<a href="https://tc39.github.io/ecma262/#table-2">Section 6.1.7.1</a> of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</li> 3430<li><code>napi_static</code>: The property will be defined as a static property on a class as 3431opposed to an instance property, which is the default. This is used only by 3432<a href="#n_api_napi_define_class"><code>napi_define_class</code></a>. It is ignored by <code>napi_define_properties</code>.</li> 3433<li><code>napi_default_method</code>: Like a method in a JS class, the property is 3434configurable and writeable, but not enumerable.</li> 3435<li><code>napi_default_jsproperty</code>: Like a property set via assignment in JavaScript, 3436the property is writable, enumerable, and configurable.</li> 3437</ul> 3438<h5>napi_property_descriptor<span><a class="mark" href="#n_api_napi_property_descriptor" id="n_api_napi_property_descriptor">#</a></span></h5> 3439<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span> 3440 <span class="hljs-comment">// One of utf8name or name should be NULL.</span> 3441 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8name; 3442 napi_value name; 3443 3444 napi_callback method; 3445 napi_callback getter; 3446 napi_callback setter; 3447 napi_value value; 3448 3449 napi_property_attributes attributes; 3450 <span class="hljs-type">void</span>* data; 3451} napi_property_descriptor;</code></pre> 3452<ul> 3453<li><code>utf8name</code>: Optional string describing the key for the property, 3454encoded as UTF8. One of <code>utf8name</code> or <code>name</code> must be provided for the 3455property.</li> 3456<li><code>name</code>: Optional <code>napi_value</code> that points to a JavaScript string or symbol 3457to be used as the key for the property. One of <code>utf8name</code> or <code>name</code> must 3458be provided for the property.</li> 3459<li><code>value</code>: The value that's retrieved by a get access of the property if the 3460property is a data property. If this is passed in, set <code>getter</code>, <code>setter</code>, 3461<code>method</code> and <code>data</code> to <code>NULL</code> (since these members won't be used).</li> 3462<li><code>getter</code>: A function to call when a get access of the property is performed. 3463If this is passed in, set <code>value</code> and <code>method</code> to <code>NULL</code> (since these members 3464won't be used). The given function is called implicitly by the runtime when 3465the property is accessed from JavaScript code (or if a get on the property is 3466performed using a Node-API call). <a href="#n_api_napi_callback"><code>napi_callback</code></a> provides more details.</li> 3467<li><code>setter</code>: A function to call when a set access of the property is performed. 3468If this is passed in, set <code>value</code> and <code>method</code> to <code>NULL</code> (since these members 3469won't be used). The given function is called implicitly by the runtime when 3470the property is set from JavaScript code (or if a set on the property is 3471performed using a Node-API call). <a href="#n_api_napi_callback"><code>napi_callback</code></a> provides more details.</li> 3472<li><code>method</code>: Set this to make the property descriptor object's <code>value</code> 3473property to be a JavaScript function represented by <code>method</code>. If this is 3474passed in, set <code>value</code>, <code>getter</code> and <code>setter</code> to <code>NULL</code> (since these members 3475won't be used). <a href="#n_api_napi_callback"><code>napi_callback</code></a> provides more details.</li> 3476<li><code>attributes</code>: The attributes associated with the particular property. See 3477<a href="#n_api_napi_property_attributes"><code>napi_property_attributes</code></a>.</li> 3478<li><code>data</code>: The callback data passed into <code>method</code>, <code>getter</code> and <code>setter</code> if this 3479function is invoked.</li> 3480</ul> 3481<h4>Functions<span><a class="mark" href="#n_api_functions" id="n_api_functions">#</a></span></h4> 3482<h5>napi_get_property_names<span><a class="mark" href="#n_api_napi_get_property_names" id="n_api_napi_get_property_names">#</a></span></h5> 3483<div class="api_metadata"> 3484<span>Added in: v8.0.0</span> 3485<span>N-API version: 1</span> 3486</div> 3487<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_property_names</span><span class="hljs-params">(napi_env env, 3488 napi_value object, 3489 napi_value* result)</span>;</code></pre> 3490<ul> 3491<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 3492<li><code>[in] object</code>: The object from which to retrieve the properties.</li> 3493<li><code>[out] result</code>: A <code>napi_value</code> representing an array of JavaScript values 3494that represent the property names of the object. The API can be used to 3495iterate over <code>result</code> using <a href="#n_api_napi_get_array_length"><code>napi_get_array_length</code></a> 3496and <a href="#n_api_napi_get_element"><code>napi_get_element</code></a>.</li> 3497</ul> 3498<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3499<p>This API returns the names of the enumerable properties of <code>object</code> as an array 3500of strings. The properties of <code>object</code> whose key is a symbol will not be 3501included.</p> 3502<h5>napi_get_all_property_names<span><a class="mark" href="#n_api_napi_get_all_property_names" id="n_api_napi_get_all_property_names">#</a></span></h5> 3503<div class="api_metadata"> 3504<span>Added in: v13.7.0, v10.20.0</span> 3505<span>N-API version: 6</span> 3506</div> 3507<pre><code class="language-c">napi_get_all_property_names(napi_env env, 3508 napi_value object, 3509 napi_key_collection_mode key_mode, 3510 napi_key_filter key_filter, 3511 napi_key_conversion key_conversion, 3512 napi_value* result);</code></pre> 3513<ul> 3514<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 3515<li><code>[in] object</code>: The object from which to retrieve the properties.</li> 3516<li><code>[in] key_mode</code>: Whether to retrieve prototype properties as well.</li> 3517<li><code>[in] key_filter</code>: Which properties to retrieve 3518(enumerable/readable/writable).</li> 3519<li><code>[in] key_conversion</code>: Whether to convert numbered property keys to strings.</li> 3520<li><code>[out] result</code>: A <code>napi_value</code> representing an array of JavaScript values 3521that represent the property names of the object. <a href="#n_api_napi_get_array_length"><code>napi_get_array_length</code></a> 3522and <a href="#n_api_napi_get_element"><code>napi_get_element</code></a> can be used to iterate over <code>result</code>.</li> 3523</ul> 3524<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3525<p>This API returns an array containing the names of the available properties 3526of this object.</p> 3527<h5>napi_set_property<span><a class="mark" href="#n_api_napi_set_property" id="n_api_napi_set_property">#</a></span></h5> 3528<div class="api_metadata"> 3529<span>Added in: v8.0.0</span> 3530<span>N-API version: 1</span> 3531</div> 3532<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_property</span><span class="hljs-params">(napi_env env, 3533 napi_value object, 3534 napi_value key, 3535 napi_value value)</span>;</code></pre> 3536<ul> 3537<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 3538<li><code>[in] object</code>: The object on which to set the property.</li> 3539<li><code>[in] key</code>: The name of the property to set.</li> 3540<li><code>[in] value</code>: The property value.</li> 3541</ul> 3542<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3543<p>This API set a property on the <code>Object</code> passed in.</p> 3544<h5>napi_get_property<span><a class="mark" href="#n_api_napi_get_property" id="n_api_napi_get_property">#</a></span></h5> 3545<div class="api_metadata"> 3546<span>Added in: v8.0.0</span> 3547<span>N-API version: 1</span> 3548</div> 3549<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_property</span><span class="hljs-params">(napi_env env, 3550 napi_value object, 3551 napi_value key, 3552 napi_value* result)</span>;</code></pre> 3553<ul> 3554<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 3555<li><code>[in] object</code>: The object from which to retrieve the property.</li> 3556<li><code>[in] key</code>: The name of the property to retrieve.</li> 3557<li><code>[out] result</code>: The value of the property.</li> 3558</ul> 3559<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3560<p>This API gets the requested property from the <code>Object</code> passed in.</p> 3561<h5>napi_has_property<span><a class="mark" href="#n_api_napi_has_property" id="n_api_napi_has_property">#</a></span></h5> 3562<div class="api_metadata"> 3563<span>Added in: v8.0.0</span> 3564<span>N-API version: 1</span> 3565</div> 3566<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_property</span><span class="hljs-params">(napi_env env, 3567 napi_value object, 3568 napi_value key, 3569 <span class="hljs-type">bool</span>* result)</span>;</code></pre> 3570<ul> 3571<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 3572<li><code>[in] object</code>: The object to query.</li> 3573<li><code>[in] key</code>: The name of the property whose existence to check.</li> 3574<li><code>[out] result</code>: Whether the property exists on the object or not.</li> 3575</ul> 3576<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3577<p>This API checks if the <code>Object</code> passed in has the named property.</p> 3578<h5>napi_delete_property<span><a class="mark" href="#n_api_napi_delete_property" id="n_api_napi_delete_property">#</a></span></h5> 3579<div class="api_metadata"> 3580<span>Added in: v8.2.0</span> 3581<span>N-API version: 1</span> 3582</div> 3583<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_property</span><span class="hljs-params">(napi_env env, 3584 napi_value object, 3585 napi_value key, 3586 <span class="hljs-type">bool</span>* result)</span>;</code></pre> 3587<ul> 3588<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 3589<li><code>[in] object</code>: The object to query.</li> 3590<li><code>[in] key</code>: The name of the property to delete.</li> 3591<li><code>[out] result</code>: Whether the property deletion succeeded or not. <code>result</code> can 3592optionally be ignored by passing <code>NULL</code>.</li> 3593</ul> 3594<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3595<p>This API attempts to delete the <code>key</code> own property from <code>object</code>.</p> 3596<h5>napi_has_own_property<span><a class="mark" href="#n_api_napi_has_own_property" id="n_api_napi_has_own_property">#</a></span></h5> 3597<div class="api_metadata"> 3598<span>Added in: v8.2.0</span> 3599<span>N-API version: 1</span> 3600</div> 3601<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_own_property</span><span class="hljs-params">(napi_env env, 3602 napi_value object, 3603 napi_value key, 3604 <span class="hljs-type">bool</span>* result)</span>;</code></pre> 3605<ul> 3606<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 3607<li><code>[in] object</code>: The object to query.</li> 3608<li><code>[in] key</code>: The name of the own property whose existence to check.</li> 3609<li><code>[out] result</code>: Whether the own property exists on the object or not.</li> 3610</ul> 3611<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3612<p>This API checks if the <code>Object</code> passed in has the named own property. <code>key</code> must 3613be a <code>string</code> or a <code>symbol</code>, or an error will be thrown. Node-API will not 3614perform any conversion between data types.</p> 3615<h5>napi_set_named_property<span><a class="mark" href="#n_api_napi_set_named_property" id="n_api_napi_set_named_property">#</a></span></h5> 3616<div class="api_metadata"> 3617<span>Added in: v8.0.0</span> 3618<span>N-API version: 1</span> 3619</div> 3620<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_named_property</span><span class="hljs-params">(napi_env env, 3621 napi_value object, 3622 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8Name, 3623 napi_value value)</span>;</code></pre> 3624<ul> 3625<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 3626<li><code>[in] object</code>: The object on which to set the property.</li> 3627<li><code>[in] utf8Name</code>: The name of the property to set.</li> 3628<li><code>[in] value</code>: The property value.</li> 3629</ul> 3630<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3631<p>This method is equivalent to calling <a href="#n_api_napi_set_property"><code>napi_set_property</code></a> with a <code>napi_value</code> 3632created from the string passed in as <code>utf8Name</code>.</p> 3633<h5>napi_get_named_property<span><a class="mark" href="#n_api_napi_get_named_property" id="n_api_napi_get_named_property">#</a></span></h5> 3634<div class="api_metadata"> 3635<span>Added in: v8.0.0</span> 3636<span>N-API version: 1</span> 3637</div> 3638<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_named_property</span><span class="hljs-params">(napi_env env, 3639 napi_value object, 3640 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8Name, 3641 napi_value* result)</span>;</code></pre> 3642<ul> 3643<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 3644<li><code>[in] object</code>: The object from which to retrieve the property.</li> 3645<li><code>[in] utf8Name</code>: The name of the property to get.</li> 3646<li><code>[out] result</code>: The value of the property.</li> 3647</ul> 3648<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3649<p>This method is equivalent to calling <a href="#n_api_napi_get_property"><code>napi_get_property</code></a> with a <code>napi_value</code> 3650created from the string passed in as <code>utf8Name</code>.</p> 3651<h5>napi_has_named_property<span><a class="mark" href="#n_api_napi_has_named_property" id="n_api_napi_has_named_property">#</a></span></h5> 3652<div class="api_metadata"> 3653<span>Added in: v8.0.0</span> 3654<span>N-API version: 1</span> 3655</div> 3656<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_named_property</span><span class="hljs-params">(napi_env env, 3657 napi_value object, 3658 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8Name, 3659 <span class="hljs-type">bool</span>* result)</span>;</code></pre> 3660<ul> 3661<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 3662<li><code>[in] object</code>: The object to query.</li> 3663<li><code>[in] utf8Name</code>: The name of the property whose existence to check.</li> 3664<li><code>[out] result</code>: Whether the property exists on the object or not.</li> 3665</ul> 3666<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3667<p>This method is equivalent to calling <a href="#n_api_napi_has_property"><code>napi_has_property</code></a> with a <code>napi_value</code> 3668created from the string passed in as <code>utf8Name</code>.</p> 3669<h5>napi_set_element<span><a class="mark" href="#n_api_napi_set_element" id="n_api_napi_set_element">#</a></span></h5> 3670<div class="api_metadata"> 3671<span>Added in: v8.0.0</span> 3672<span>N-API version: 1</span> 3673</div> 3674<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_element</span><span class="hljs-params">(napi_env env, 3675 napi_value object, 3676 <span class="hljs-type">uint32_t</span> index, 3677 napi_value value)</span>;</code></pre> 3678<ul> 3679<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 3680<li><code>[in] object</code>: The object from which to set the properties.</li> 3681<li><code>[in] index</code>: The index of the property to set.</li> 3682<li><code>[in] value</code>: The property value.</li> 3683</ul> 3684<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3685<p>This API sets and element on the <code>Object</code> passed in.</p> 3686<h5>napi_get_element<span><a class="mark" href="#n_api_napi_get_element" id="n_api_napi_get_element">#</a></span></h5> 3687<div class="api_metadata"> 3688<span>Added in: v8.0.0</span> 3689<span>N-API version: 1</span> 3690</div> 3691<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_element</span><span class="hljs-params">(napi_env env, 3692 napi_value object, 3693 <span class="hljs-type">uint32_t</span> index, 3694 napi_value* result)</span>;</code></pre> 3695<ul> 3696<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 3697<li><code>[in] object</code>: The object from which to retrieve the property.</li> 3698<li><code>[in] index</code>: The index of the property to get.</li> 3699<li><code>[out] result</code>: The value of the property.</li> 3700</ul> 3701<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3702<p>This API gets the element at the requested index.</p> 3703<h5>napi_has_element<span><a class="mark" href="#n_api_napi_has_element" id="n_api_napi_has_element">#</a></span></h5> 3704<div class="api_metadata"> 3705<span>Added in: v8.0.0</span> 3706<span>N-API version: 1</span> 3707</div> 3708<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_element</span><span class="hljs-params">(napi_env env, 3709 napi_value object, 3710 <span class="hljs-type">uint32_t</span> index, 3711 <span class="hljs-type">bool</span>* result)</span>;</code></pre> 3712<ul> 3713<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 3714<li><code>[in] object</code>: The object to query.</li> 3715<li><code>[in] index</code>: The index of the property whose existence to check.</li> 3716<li><code>[out] result</code>: Whether the property exists on the object or not.</li> 3717</ul> 3718<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3719<p>This API returns if the <code>Object</code> passed in has an element at the 3720requested index.</p> 3721<h5>napi_delete_element<span><a class="mark" href="#n_api_napi_delete_element" id="n_api_napi_delete_element">#</a></span></h5> 3722<div class="api_metadata"> 3723<span>Added in: v8.2.0</span> 3724<span>N-API version: 1</span> 3725</div> 3726<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_element</span><span class="hljs-params">(napi_env env, 3727 napi_value object, 3728 <span class="hljs-type">uint32_t</span> index, 3729 <span class="hljs-type">bool</span>* result)</span>;</code></pre> 3730<ul> 3731<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 3732<li><code>[in] object</code>: The object to query.</li> 3733<li><code>[in] index</code>: The index of the property to delete.</li> 3734<li><code>[out] result</code>: Whether the element deletion succeeded or not. <code>result</code> can 3735optionally be ignored by passing <code>NULL</code>.</li> 3736</ul> 3737<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3738<p>This API attempts to delete the specified <code>index</code> from <code>object</code>.</p> 3739<h5>napi_define_properties<span><a class="mark" href="#n_api_napi_define_properties" id="n_api_napi_define_properties">#</a></span></h5> 3740<div class="api_metadata"> 3741<span>Added in: v8.0.0</span> 3742<span>N-API version: 1</span> 3743</div> 3744<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_properties</span><span class="hljs-params">(napi_env env, 3745 napi_value object, 3746 <span class="hljs-type">size_t</span> property_count, 3747 <span class="hljs-keyword">const</span> napi_property_descriptor* properties)</span>;</code></pre> 3748<ul> 3749<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 3750<li><code>[in] object</code>: The object from which to retrieve the properties.</li> 3751<li><code>[in] property_count</code>: The number of elements in the <code>properties</code> array.</li> 3752<li><code>[in] properties</code>: The array of property descriptors.</li> 3753</ul> 3754<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3755<p>This method allows the efficient definition of multiple properties on a given 3756object. The properties are defined using property descriptors (see 3757<a href="#n_api_napi_property_descriptor"><code>napi_property_descriptor</code></a>). Given an array of such property descriptors, 3758this API will set the properties on the object one at a time, as defined by 3759<code>DefineOwnProperty()</code> (described in <a href="https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc">Section 9.1.6</a> of the ECMA-262 3760specification).</p> 3761<h5>napi_object_freeze<span><a class="mark" href="#n_api_napi_object_freeze" id="n_api_napi_object_freeze">#</a></span></h5> 3762<div class="api_metadata"> 3763<span>Added in: v14.14.0, v12.20.0</span> 3764<span>N-API version: 8</span> 3765</div> 3766<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_freeze</span><span class="hljs-params">(napi_env env, 3767 napi_value object)</span>;</code></pre> 3768<ul> 3769<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 3770<li><code>[in] object</code>: The object to freeze.</li> 3771</ul> 3772<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3773<p>This method freezes a given object. This prevents new properties from 3774being added to it, existing properties from being removed, prevents 3775changing the enumerability, configurability, or writability of existing 3776properties, and prevents the values of existing properties from being changed. 3777It also prevents the object's prototype from being changed. This is described 3778in <a href="https://tc39.es/ecma262/#sec-object.freeze">Section 19.1.2.6</a> of the 3779ECMA-262 specification.</p> 3780<h5>napi_object_seal<span><a class="mark" href="#n_api_napi_object_seal" id="n_api_napi_object_seal">#</a></span></h5> 3781<div class="api_metadata"> 3782<span>Added in: v14.14.0, v12.20.0</span> 3783<span>N-API version: 8</span> 3784</div> 3785<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_seal</span><span class="hljs-params">(napi_env env, 3786 napi_value object)</span>;</code></pre> 3787<ul> 3788<li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> 3789<li><code>[in] object</code>: The object to seal.</li> 3790</ul> 3791<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3792<p>This method seals a given object. This prevents new properties from being 3793added to it, as well as marking all existing properties as non-configurable. 3794This is described in <a href="https://tc39.es/ecma262/#sec-object.seal">Section 19.1.2.20</a> 3795of the ECMA-262 specification.</p> 3796</section><section><h3>Working with JavaScript functions<span><a class="mark" href="#n_api_working_with_javascript_functions" id="n_api_working_with_javascript_functions">#</a></span></h3> 3797<p>Node-API provides a set of APIs that allow JavaScript code to 3798call back into native code. Node-APIs that support calling back 3799into native code take in a callback functions represented by 3800the <code>napi_callback</code> type. When the JavaScript VM calls back to 3801native code, the <code>napi_callback</code> function provided is invoked. The APIs 3802documented in this section allow the callback function to do the 3803following:</p> 3804<ul> 3805<li>Get information about the context in which the callback was invoked.</li> 3806<li>Get the arguments passed into the callback.</li> 3807<li>Return a <code>napi_value</code> back from the callback.</li> 3808</ul> 3809<p>Additionally, Node-API provides a set of functions which allow calling 3810JavaScript functions from native code. One can either call a function 3811like a regular JavaScript function call, or as a constructor 3812function.</p> 3813<p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> field of the 3814<code>napi_property_descriptor</code> items can be associated with <code>object</code> and freed 3815whenever <code>object</code> is garbage-collected by passing both <code>object</code> and the data to 3816<a href="#n_api_napi_add_finalizer"><code>napi_add_finalizer</code></a>.</p> 3817<h4>napi_call_function<span><a class="mark" href="#n_api_napi_call_function" id="n_api_napi_call_function">#</a></span></h4> 3818<div class="api_metadata"> 3819<span>Added in: v8.0.0</span> 3820<span>N-API version: 1</span> 3821</div> 3822<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_call_function</span><span class="hljs-params">(napi_env env, 3823 napi_value recv, 3824 napi_value func, 3825 <span class="hljs-type">size_t</span> argc, 3826 <span class="hljs-keyword">const</span> napi_value* argv, 3827 napi_value* result)</span>;</code></pre> 3828<ul> 3829<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3830<li><code>[in] recv</code>: The <code>this</code> value passed to the called function.</li> 3831<li><code>[in] func</code>: <code>napi_value</code> representing the JavaScript function to be invoked.</li> 3832<li><code>[in] argc</code>: The count of elements in the <code>argv</code> array.</li> 3833<li><code>[in] argv</code>: Array of <code>napi_values</code> representing JavaScript values passed in 3834as arguments to the function.</li> 3835<li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned.</li> 3836</ul> 3837<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3838<p>This method allows a JavaScript function object to be called from a native 3839add-on. This is the primary mechanism of calling back <em>from</em> the add-on's 3840native code <em>into</em> JavaScript. For the special case of calling into JavaScript 3841after an async operation, see <a href="#n_api_napi_make_callback"><code>napi_make_callback</code></a>.</p> 3842<p>A sample use case might look as follows. Consider the following JavaScript 3843snippet:</p> 3844<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">AddTwo</span>(<span class="hljs-params">num</span>) { 3845 <span class="hljs-keyword">return</span> num + <span class="hljs-number">2</span>; 3846}</code></pre> 3847<p>Then, the above function can be invoked from a native add-on using the 3848following code:</p> 3849<pre><code class="language-c"><span class="hljs-comment">// Get the function named "AddTwo" on the global object</span> 3850napi_value global, add_two, arg; 3851napi_status status = napi_get_global(env, &global); 3852<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>; 3853 3854status = napi_get_named_property(env, global, <span class="hljs-string">"AddTwo"</span>, &add_two); 3855<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>; 3856 3857<span class="hljs-comment">// const arg = 1337</span> 3858status = napi_create_int32(env, <span class="hljs-number">1337</span>, &arg); 3859<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>; 3860 3861napi_value* argv = &arg; 3862<span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>; 3863 3864<span class="hljs-comment">// AddTwo(arg);</span> 3865napi_value return_val; 3866status = napi_call_function(env, global, add_two, argc, argv, &return_val); 3867<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>; 3868 3869<span class="hljs-comment">// Convert the result back to a native type</span> 3870<span class="hljs-type">int32_t</span> result; 3871status = napi_get_value_int32(env, return_val, &result); 3872<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;</code></pre> 3873<h4>napi_create_function<span><a class="mark" href="#n_api_napi_create_function" id="n_api_napi_create_function">#</a></span></h4> 3874<div class="api_metadata"> 3875<span>Added in: v8.0.0</span> 3876<span>N-API version: 1</span> 3877</div> 3878<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_function</span><span class="hljs-params">(napi_env env, 3879 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8name, 3880 <span class="hljs-type">size_t</span> length, 3881 napi_callback cb, 3882 <span class="hljs-type">void</span>* data, 3883 napi_value* result)</span>;</code></pre> 3884<ul> 3885<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3886<li><code>[in] utf8Name</code>: The name of the function encoded as UTF8. This is visible 3887within JavaScript as the new function object's <code>name</code> property.</li> 3888<li><code>[in] length</code>: The length of the <code>utf8name</code> in bytes, or <code>NAPI_AUTO_LENGTH</code> if 3889it is null-terminated.</li> 3890<li><code>[in] cb</code>: The native function which should be called when this function 3891object is invoked. <a href="#n_api_napi_callback"><code>napi_callback</code></a> provides more details.</li> 3892<li><code>[in] data</code>: User-provided data context. This will be passed back into the 3893function when invoked later.</li> 3894<li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript function object for 3895the newly created function.</li> 3896</ul> 3897<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3898<p>This API allows an add-on author to create a function object in native code. 3899This is the primary mechanism to allow calling <em>into</em> the add-on's native code 3900<em>from</em> JavaScript.</p> 3901<p>The newly created function is not automatically visible from script after this 3902call. Instead, a property must be explicitly set on any object that is visible 3903to JavaScript, in order for the function to be accessible from script.</p> 3904<p>In order to expose a function as part of the 3905add-on's module exports, set the newly created function on the exports 3906object. A sample module might look as follows:</p> 3907<pre><code class="language-c">napi_value <span class="hljs-title function_">SayHello</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> { 3908 <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello\n"</span>); 3909 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 3910} 3911 3912napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> { 3913 napi_status status; 3914 3915 napi_value fn; 3916 status = napi_create_function(env, <span class="hljs-literal">NULL</span>, <span class="hljs-number">0</span>, SayHello, <span class="hljs-literal">NULL</span>, &fn); 3917 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 3918 3919 status = napi_set_named_property(env, exports, <span class="hljs-string">"sayHello"</span>, fn); 3920 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 3921 3922 <span class="hljs-keyword">return</span> exports; 3923} 3924 3925NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)</code></pre> 3926<p>Given the above code, the add-on can be used from JavaScript as follows:</p> 3927<pre><code class="language-js"><span class="hljs-keyword">const</span> myaddon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./addon'</span>); 3928myaddon.<span class="hljs-title function_">sayHello</span>();</code></pre> 3929<p>The string passed to <code>require()</code> is the name of the target in <code>binding.gyp</code> 3930responsible for creating the <code>.node</code> file.</p> 3931<p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> parameter can 3932be associated with the resulting JavaScript function (which is returned in the 3933<code>result</code> parameter) and freed whenever the function is garbage-collected by 3934passing both the JavaScript function and the data to <a href="#n_api_napi_add_finalizer"><code>napi_add_finalizer</code></a>.</p> 3935<p>JavaScript <code>Function</code>s are described in <a href="https://tc39.github.io/ecma262/#sec-function-objects">Section 19.2</a> of the ECMAScript 3936Language Specification.</p> 3937<h4>napi_get_cb_info<span><a class="mark" href="#n_api_napi_get_cb_info" id="n_api_napi_get_cb_info">#</a></span></h4> 3938<div class="api_metadata"> 3939<span>Added in: v8.0.0</span> 3940<span>N-API version: 1</span> 3941</div> 3942<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_cb_info</span><span class="hljs-params">(napi_env env, 3943 napi_callback_info cbinfo, 3944 <span class="hljs-type">size_t</span>* argc, 3945 napi_value* argv, 3946 napi_value* thisArg, 3947 <span class="hljs-type">void</span>** data)</span></code></pre> 3948<ul> 3949<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3950<li><code>[in] cbinfo</code>: The callback info passed into the callback function.</li> 3951<li><code>[in-out] argc</code>: Specifies the length of the provided <code>argv</code> array and 3952receives the actual count of arguments.</li> 3953<li><code>[out] argv</code>: Buffer to which the <code>napi_value</code> representing the arguments are 3954copied. If there are more arguments than the provided count, only the 3955requested number of arguments are copied. If there are fewer arguments 3956provided than claimed, the rest of <code>argv</code> is filled with <code>napi_value</code> values 3957that represent <code>undefined</code>.</li> 3958<li><code>[out] this</code>: Receives the JavaScript <code>this</code> argument for the call.</li> 3959<li><code>[out] data</code>: Receives the data pointer for the callback.</li> 3960</ul> 3961<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3962<p>This method is used within a callback function to retrieve details about the 3963call like the arguments and the <code>this</code> pointer from a given callback info.</p> 3964<h4>napi_get_new_target<span><a class="mark" href="#n_api_napi_get_new_target" id="n_api_napi_get_new_target">#</a></span></h4> 3965<div class="api_metadata"> 3966<span>Added in: v8.6.0</span> 3967<span>N-API version: 1</span> 3968</div> 3969<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_new_target</span><span class="hljs-params">(napi_env env, 3970 napi_callback_info cbinfo, 3971 napi_value* result)</span></code></pre> 3972<ul> 3973<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3974<li><code>[in] cbinfo</code>: The callback info passed into the callback function.</li> 3975<li><code>[out] result</code>: The <code>new.target</code> of the constructor call.</li> 3976</ul> 3977<p>Returns <code>napi_ok</code> if the API succeeded.</p> 3978<p>This API returns the <code>new.target</code> of the constructor call. If the current 3979callback is not a constructor call, the result is <code>NULL</code>.</p> 3980<h4>napi_new_instance<span><a class="mark" href="#n_api_napi_new_instance" id="n_api_napi_new_instance">#</a></span></h4> 3981<div class="api_metadata"> 3982<span>Added in: v8.0.0</span> 3983<span>N-API version: 1</span> 3984</div> 3985<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_new_instance</span><span class="hljs-params">(napi_env env, 3986 napi_value cons, 3987 <span class="hljs-type">size_t</span> argc, 3988 napi_value* argv, 3989 napi_value* result)</span></code></pre> 3990<ul> 3991<li><code>[in] env</code>: The environment that the API is invoked under.</li> 3992<li><code>[in] cons</code>: <code>napi_value</code> representing the JavaScript function to be invoked 3993as a constructor.</li> 3994<li><code>[in] argc</code>: The count of elements in the <code>argv</code> array.</li> 3995<li><code>[in] argv</code>: Array of JavaScript values as <code>napi_value</code> representing the 3996arguments to the constructor.</li> 3997<li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned, 3998which in this case is the constructed object.</li> 3999</ul> 4000<p>This method is used to instantiate a new JavaScript value using a given 4001<code>napi_value</code> that represents the constructor for the object. For example, 4002consider the following snippet:</p> 4003<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">MyObject</span>(<span class="hljs-params">param</span>) { 4004 <span class="hljs-variable language_">this</span>.<span class="hljs-property">param</span> = param; 4005} 4006 4007<span class="hljs-keyword">const</span> arg = <span class="hljs-string">'hello'</span>; 4008<span class="hljs-keyword">const</span> value = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyObject</span>(arg);</code></pre> 4009<p>The following can be approximated in Node-API using the following snippet:</p> 4010<pre><code class="language-c"><span class="hljs-comment">// Get the constructor function MyObject</span> 4011napi_value global, constructor, arg, value; 4012napi_status status = napi_get_global(env, &global); 4013<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>; 4014 4015status = napi_get_named_property(env, global, <span class="hljs-string">"MyObject"</span>, &constructor); 4016<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>; 4017 4018<span class="hljs-comment">// const arg = "hello"</span> 4019status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &arg); 4020<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>; 4021 4022napi_value* argv = &arg; 4023<span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>; 4024 4025<span class="hljs-comment">// const value = new MyObject(arg)</span> 4026status = napi_new_instance(env, constructor, argc, argv, &value);</code></pre> 4027<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4028</section><section><h3>Object wrap<span><a class="mark" href="#n_api_object_wrap" id="n_api_object_wrap">#</a></span></h3> 4029<p>Node-API offers a way to "wrap" C++ classes and instances so that the class 4030constructor and methods can be called from JavaScript.</p> 4031<ol> 4032<li>The <a href="#n_api_napi_define_class"><code>napi_define_class</code></a> API defines a JavaScript class with constructor, 4033static properties and methods, and instance properties and methods that 4034correspond to the C++ class.</li> 4035<li>When JavaScript code invokes the constructor, the constructor callback 4036uses <a href="#n_api_napi_wrap"><code>napi_wrap</code></a> to wrap a new C++ instance in a JavaScript object, 4037then returns the wrapper object.</li> 4038<li>When JavaScript code invokes a method or property accessor on the class, 4039the corresponding <code>napi_callback</code> C++ function is invoked. For an instance 4040callback, <a href="#n_api_napi_unwrap"><code>napi_unwrap</code></a> obtains the C++ instance that is the target of 4041the call.</li> 4042</ol> 4043<p>For wrapped objects it may be difficult to distinguish between a function 4044called on a class prototype and a function called on an instance of a class. 4045A common pattern used to address this problem is to save a persistent 4046reference to the class constructor for later <code>instanceof</code> checks.</p> 4047<pre><code class="language-c">napi_value MyClass_constructor = <span class="hljs-literal">NULL</span>; 4048status = napi_get_reference_value(env, MyClass::es_constructor, &MyClass_constructor); 4049assert(napi_ok == status); 4050<span class="hljs-type">bool</span> is_instance = <span class="hljs-literal">false</span>; 4051status = napi_instanceof(env, es_this, MyClass_constructor, &is_instance); 4052assert(napi_ok == status); 4053<span class="hljs-keyword">if</span> (is_instance) { 4054 <span class="hljs-comment">// napi_unwrap() ...</span> 4055} <span class="hljs-keyword">else</span> { 4056 <span class="hljs-comment">// otherwise...</span> 4057}</code></pre> 4058<p>The reference must be freed once it is no longer needed.</p> 4059<p>There are occasions where <code>napi_instanceof()</code> is insufficient for ensuring that 4060a JavaScript object is a wrapper for a certain native type. This is the case 4061especially when wrapped JavaScript objects are passed back into the addon via 4062static methods rather than as the <code>this</code> value of prototype methods. In such 4063cases there is a chance that they may be unwrapped incorrectly.</p> 4064<pre><code class="language-js"><span class="hljs-keyword">const</span> myAddon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/my_addon.node'</span>); 4065 4066<span class="hljs-comment">// `openDatabase()` returns a JavaScript object that wraps a native database</span> 4067<span class="hljs-comment">// handle.</span> 4068<span class="hljs-keyword">const</span> dbHandle = myAddon.<span class="hljs-title function_">openDatabase</span>(); 4069 4070<span class="hljs-comment">// `query()` returns a JavaScript object that wraps a native query handle.</span> 4071<span class="hljs-keyword">const</span> queryHandle = myAddon.<span class="hljs-title function_">query</span>(dbHandle, <span class="hljs-string">'Gimme ALL the things!'</span>); 4072 4073<span class="hljs-comment">// There is an accidental error in the line below. The first parameter to</span> 4074<span class="hljs-comment">// `myAddon.queryHasRecords()` should be the database handle (`dbHandle`), not</span> 4075<span class="hljs-comment">// the query handle (`query`), so the correct condition for the while-loop</span> 4076<span class="hljs-comment">// should be</span> 4077<span class="hljs-comment">//</span> 4078<span class="hljs-comment">// myAddon.queryHasRecords(dbHandle, queryHandle)</span> 4079<span class="hljs-comment">//</span> 4080<span class="hljs-keyword">while</span> (myAddon.<span class="hljs-title function_">queryHasRecords</span>(queryHandle, dbHandle)) { 4081 <span class="hljs-comment">// retrieve records</span> 4082}</code></pre> 4083<p>In the above example <code>myAddon.queryHasRecords()</code> is a method that accepts two 4084arguments. The first is a database handle and the second is a query handle. 4085Internally, it unwraps the first argument and casts the resulting pointer to a 4086native database handle. It then unwraps the second argument and casts the 4087resulting pointer to a query handle. If the arguments are passed in the wrong 4088order, the casts will work, however, there is a good chance that the underlying 4089database operation will fail, or will even cause an invalid memory access.</p> 4090<p>To ensure that the pointer retrieved from the first argument is indeed a pointer 4091to a database handle and, similarly, that the pointer retrieved from the second 4092argument is indeed a pointer to a query handle, the implementation of 4093<code>queryHasRecords()</code> has to perform a type validation. Retaining the JavaScript 4094class constructor from which the database handle was instantiated and the 4095constructor from which the query handle was instantiated in <code>napi_ref</code>s can 4096help, because <code>napi_instanceof()</code> can then be used to ensure that the instances 4097passed into <code>queryHashRecords()</code> are indeed of the correct type.</p> 4098<p>Unfortunately, <code>napi_instanceof()</code> does not protect against prototype 4099manipulation. For example, the prototype of the database handle instance can be 4100set to the prototype of the constructor for query handle instances. In this 4101case, the database handle instance can appear as a query handle instance, and it 4102will pass the <code>napi_instanceof()</code> test for a query handle instance, while still 4103containing a pointer to a database handle.</p> 4104<p>To this end, Node-API provides type-tagging capabilities.</p> 4105<p>A type tag is a 128-bit integer unique to the addon. Node-API provides the 4106<code>napi_type_tag</code> structure for storing a type tag. When such a value is passed 4107along with a JavaScript object stored in a <code>napi_value</code> to 4108<code>napi_type_tag_object()</code>, the JavaScript object will be "marked" with the 4109type tag. The "mark" is invisible on the JavaScript side. When a JavaScript 4110object arrives into a native binding, <code>napi_check_object_type_tag()</code> can be used 4111along with the original type tag to determine whether the JavaScript object was 4112previously "marked" with the type tag. This creates a type-checking capability 4113of a higher fidelity than <code>napi_instanceof()</code> can provide, because such type- 4114tagging survives prototype manipulation and addon unloading/reloading.</p> 4115<p>Continuing the above example, the following skeleton addon implementation 4116illustrates the use of <code>napi_type_tag_object()</code> and 4117<code>napi_check_object_type_tag()</code>.</p> 4118<pre><code class="language-c"><span class="hljs-comment">// This value is the type tag for a database handle. The command</span> 4119<span class="hljs-comment">//</span> 4120<span class="hljs-comment">// uuidgen | sed -r -e 's/-//g' -e 's/(.{16})(.*)/0x\1, 0x\2/'</span> 4121<span class="hljs-comment">//</span> 4122<span class="hljs-comment">// can be used to obtain the two values with which to initialize the structure.</span> 4123<span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> napi_type_tag DatabaseHandleTypeTag = { 4124 <span class="hljs-number">0x1edf75a38336451d</span>, <span class="hljs-number">0xa5ed9ce2e4c00c38</span> 4125}; 4126 4127<span class="hljs-comment">// This value is the type tag for a query handle.</span> 4128<span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> napi_type_tag QueryHandleTypeTag = { 4129 <span class="hljs-number">0x9c73317f9fad44a3</span>, <span class="hljs-number">0x93c3920bf3b0ad6a</span> 4130}; 4131 4132<span class="hljs-keyword">static</span> napi_value 4133<span class="hljs-title function_">openDatabase</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> { 4134 napi_status status; 4135 napi_value result; 4136 4137 <span class="hljs-comment">// Perform the underlying action which results in a database handle.</span> 4138 DatabaseHandle* dbHandle = open_database(); 4139 4140 <span class="hljs-comment">// Create a new, empty JS object.</span> 4141 status = napi_create_object(env, &result); 4142 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 4143 4144 <span class="hljs-comment">// Tag the object to indicate that it holds a pointer to a `DatabaseHandle`.</span> 4145 status = napi_type_tag_object(env, result, &DatabaseHandleTypeTag); 4146 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 4147 4148 <span class="hljs-comment">// Store the pointer to the `DatabaseHandle` structure inside the JS object.</span> 4149 status = napi_wrap(env, result, dbHandle, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>); 4150 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 4151 4152 <span class="hljs-keyword">return</span> result; 4153} 4154 4155<span class="hljs-comment">// Later when we receive a JavaScript object purporting to be a database handle</span> 4156<span class="hljs-comment">// we can use `napi_check_object_type_tag()` to ensure that it is indeed such a</span> 4157<span class="hljs-comment">// handle.</span> 4158 4159<span class="hljs-keyword">static</span> napi_value 4160<span class="hljs-title function_">query</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> { 4161 napi_status status; 4162 <span class="hljs-type">size_t</span> argc = <span class="hljs-number">2</span>; 4163 napi_value argv[<span class="hljs-number">2</span>]; 4164 <span class="hljs-type">bool</span> is_db_handle; 4165 4166 status = napi_get_cb_info(env, info, &argc, argv, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>); 4167 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 4168 4169 <span class="hljs-comment">// Check that the object passed as the first parameter has the previously</span> 4170 <span class="hljs-comment">// applied tag.</span> 4171 status = napi_check_object_type_tag(env, 4172 argv[<span class="hljs-number">0</span>], 4173 &DatabaseHandleTypeTag, 4174 &is_db_handle); 4175 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 4176 4177 <span class="hljs-comment">// Throw a `TypeError` if it doesn't.</span> 4178 <span class="hljs-keyword">if</span> (!is_db_handle) { 4179 <span class="hljs-comment">// Throw a TypeError.</span> 4180 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 4181 } 4182}</code></pre> 4183<h4>napi_define_class<span><a class="mark" href="#n_api_napi_define_class" id="n_api_napi_define_class">#</a></span></h4> 4184<div class="api_metadata"> 4185<span>Added in: v8.0.0</span> 4186<span>N-API version: 1</span> 4187</div> 4188<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_class</span><span class="hljs-params">(napi_env env, 4189 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8name, 4190 <span class="hljs-type">size_t</span> length, 4191 napi_callback constructor, 4192 <span class="hljs-type">void</span>* data, 4193 <span class="hljs-type">size_t</span> property_count, 4194 <span class="hljs-keyword">const</span> napi_property_descriptor* properties, 4195 napi_value* result)</span>;</code></pre> 4196<ul> 4197<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4198<li><code>[in] utf8name</code>: Name of the JavaScript constructor function; When wrapping a 4199C++ class, we recommend for clarity that this name be the same as that of 4200the C++ class.</li> 4201<li><code>[in] length</code>: The length of the <code>utf8name</code> in bytes, or <code>NAPI_AUTO_LENGTH</code> 4202if it is null-terminated.</li> 4203<li><code>[in] constructor</code>: Callback function that handles constructing instances 4204of the class. When wrapping a C++ class, this method must be a static member 4205with the <a href="#n_api_napi_callback"><code>napi_callback</code></a> signature. A C++ class constructor cannot be 4206used. <a href="#n_api_napi_callback"><code>napi_callback</code></a> provides more details.</li> 4207<li><code>[in] data</code>: Optional data to be passed to the constructor callback as 4208the <code>data</code> property of the callback info.</li> 4209<li><code>[in] property_count</code>: Number of items in the <code>properties</code> array argument.</li> 4210<li><code>[in] properties</code>: Array of property descriptors describing static and 4211instance data properties, accessors, and methods on the class 4212See <code>napi_property_descriptor</code>.</li> 4213<li><code>[out] result</code>: A <code>napi_value</code> representing the constructor function for 4214the class.</li> 4215</ul> 4216<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4217<p>Defines a JavaScript class, including:</p> 4218<ul> 4219<li>A JavaScript constructor function that has the class name. When wrapping a 4220corresponding C++ class, the callback passed via <code>constructor</code> can be used to 4221instantiate a new C++ class instance, which can then be placed inside the 4222JavaScript object instance being constructed using <a href="#n_api_napi_wrap"><code>napi_wrap</code></a>.</li> 4223<li>Properties on the constructor function whose implementation can call 4224corresponding <em>static</em> data properties, accessors, and methods of the C++ 4225class (defined by property descriptors with the <code>napi_static</code> attribute).</li> 4226<li>Properties on the constructor function's <code>prototype</code> object. When wrapping a 4227C++ class, <em>non-static</em> data properties, accessors, and methods of the C++ 4228class can be called from the static functions given in the property 4229descriptors without the <code>napi_static</code> attribute after retrieving the C++ class 4230instance placed inside the JavaScript object instance by using 4231<a href="#n_api_napi_unwrap"><code>napi_unwrap</code></a>.</li> 4232</ul> 4233<p>When wrapping a C++ class, the C++ constructor callback passed via <code>constructor</code> 4234should be a static method on the class that calls the actual class constructor, 4235then wraps the new C++ instance in a JavaScript object, and returns the wrapper 4236object. See <a href="#n_api_napi_wrap"><code>napi_wrap</code></a> for details.</p> 4237<p>The JavaScript constructor function returned from <a href="#n_api_napi_define_class"><code>napi_define_class</code></a> is 4238often saved and used later to construct new instances of the class from native 4239code, and/or to check whether provided values are instances of the class. In 4240that case, to prevent the function value from being garbage-collected, a 4241strong persistent reference to it can be created using 4242<a href="#n_api_napi_create_reference"><code>napi_create_reference</code></a>, ensuring that the reference count is kept >= 1.</p> 4243<p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> parameter or via 4244the <code>data</code> field of the <code>napi_property_descriptor</code> array items can be associated 4245with the resulting JavaScript constructor (which is returned in the <code>result</code> 4246parameter) and freed whenever the class is garbage-collected by passing both 4247the JavaScript function and the data to <a href="#n_api_napi_add_finalizer"><code>napi_add_finalizer</code></a>.</p> 4248<h4>napi_wrap<span><a class="mark" href="#n_api_napi_wrap" id="n_api_napi_wrap">#</a></span></h4> 4249<div class="api_metadata"> 4250<span>Added in: v8.0.0</span> 4251<span>N-API version: 1</span> 4252</div> 4253<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_wrap</span><span class="hljs-params">(napi_env env, 4254 napi_value js_object, 4255 <span class="hljs-type">void</span>* native_object, 4256 napi_finalize finalize_cb, 4257 <span class="hljs-type">void</span>* finalize_hint, 4258 napi_ref* result)</span>;</code></pre> 4259<ul> 4260<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4261<li><code>[in] js_object</code>: The JavaScript object that will be the wrapper for the 4262native object.</li> 4263<li><code>[in] native_object</code>: The native instance that will be wrapped in the 4264JavaScript object.</li> 4265<li><code>[in] finalize_cb</code>: Optional native callback that can be used to free the 4266native instance when the JavaScript object is ready for garbage-collection. 4267<a href="#n_api_napi_finalize"><code>napi_finalize</code></a> provides more details.</li> 4268<li><code>[in] finalize_hint</code>: Optional contextual hint that is passed to the 4269finalize callback.</li> 4270<li><code>[out] result</code>: Optional reference to the wrapped object.</li> 4271</ul> 4272<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4273<p>Wraps a native instance in a JavaScript object. The native instance can be 4274retrieved later using <code>napi_unwrap()</code>.</p> 4275<p>When JavaScript code invokes a constructor for a class that was defined using 4276<code>napi_define_class()</code>, the <code>napi_callback</code> for the constructor is invoked. 4277After constructing an instance of the native class, the callback must then call 4278<code>napi_wrap()</code> to wrap the newly constructed instance in the already-created 4279JavaScript object that is the <code>this</code> argument to the constructor callback. 4280(That <code>this</code> object was created from the constructor function's <code>prototype</code>, 4281so it already has definitions of all the instance properties and methods.)</p> 4282<p>Typically when wrapping a class instance, a finalize callback should be 4283provided that simply deletes the native instance that is received as the <code>data</code> 4284argument to the finalize callback.</p> 4285<p>The optional returned reference is initially a weak reference, meaning it 4286has a reference count of 0. Typically this reference count would be incremented 4287temporarily during async operations that require the instance to remain valid.</p> 4288<p><em>Caution</em>: The optional returned reference (if obtained) should be deleted via 4289<a href="#n_api_napi_delete_reference"><code>napi_delete_reference</code></a> ONLY in response to the finalize callback 4290invocation. If it is deleted before then, then the finalize callback may never 4291be invoked. Therefore, when obtaining a reference a finalize callback is also 4292required in order to enable correct disposal of the reference.</p> 4293<p>Calling <code>napi_wrap()</code> a second time on an object will return an error. To 4294associate another native instance with the object, use <code>napi_remove_wrap()</code> 4295first.</p> 4296<h4>napi_unwrap<span><a class="mark" href="#n_api_napi_unwrap" id="n_api_napi_unwrap">#</a></span></h4> 4297<div class="api_metadata"> 4298<span>Added in: v8.0.0</span> 4299<span>N-API version: 1</span> 4300</div> 4301<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_unwrap</span><span class="hljs-params">(napi_env env, 4302 napi_value js_object, 4303 <span class="hljs-type">void</span>** result)</span>;</code></pre> 4304<ul> 4305<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4306<li><code>[in] js_object</code>: The object associated with the native instance.</li> 4307<li><code>[out] result</code>: Pointer to the wrapped native instance.</li> 4308</ul> 4309<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4310<p>Retrieves a native instance that was previously wrapped in a JavaScript 4311object using <code>napi_wrap()</code>.</p> 4312<p>When JavaScript code invokes a method or property accessor on the class, the 4313corresponding <code>napi_callback</code> is invoked. If the callback is for an instance 4314method or accessor, then the <code>this</code> argument to the callback is the wrapper 4315object; the wrapped C++ instance that is the target of the call can be obtained 4316then by calling <code>napi_unwrap()</code> on the wrapper object.</p> 4317<h4>napi_remove_wrap<span><a class="mark" href="#n_api_napi_remove_wrap" id="n_api_napi_remove_wrap">#</a></span></h4> 4318<div class="api_metadata"> 4319<span>Added in: v8.5.0</span> 4320<span>N-API version: 1</span> 4321</div> 4322<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_remove_wrap</span><span class="hljs-params">(napi_env env, 4323 napi_value js_object, 4324 <span class="hljs-type">void</span>** result)</span>;</code></pre> 4325<ul> 4326<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4327<li><code>[in] js_object</code>: The object associated with the native instance.</li> 4328<li><code>[out] result</code>: Pointer to the wrapped native instance.</li> 4329</ul> 4330<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4331<p>Retrieves a native instance that was previously wrapped in the JavaScript 4332object <code>js_object</code> using <code>napi_wrap()</code> and removes the wrapping. If a finalize 4333callback was associated with the wrapping, it will no longer be called when the 4334JavaScript object becomes garbage-collected.</p> 4335<h4>napi_type_tag_object<span><a class="mark" href="#n_api_napi_type_tag_object" id="n_api_napi_type_tag_object">#</a></span></h4> 4336<div class="api_metadata"> 4337<span>Added in: v14.8.0, v12.19.0</span> 4338<span>N-API version: 8</span> 4339</div> 4340<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_type_tag_object</span><span class="hljs-params">(napi_env env, 4341 napi_value js_object, 4342 <span class="hljs-keyword">const</span> napi_type_tag* type_tag)</span>;</code></pre> 4343<ul> 4344<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4345<li><code>[in] js_object</code>: The JavaScript object to be marked.</li> 4346<li><code>[in] type_tag</code>: The tag with which the object is to be marked.</li> 4347</ul> 4348<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4349<p>Associates the value of the <code>type_tag</code> pointer with the JavaScript object. 4350<code>napi_check_object_type_tag()</code> can then be used to compare the tag that was 4351attached to the object with one owned by the addon to ensure that the object 4352has the right type.</p> 4353<p>If the object already has an associated type tag, this API will return 4354<code>napi_invalid_arg</code>.</p> 4355<h4>napi_check_object_type_tag<span><a class="mark" href="#n_api_napi_check_object_type_tag" id="n_api_napi_check_object_type_tag">#</a></span></h4> 4356<div class="api_metadata"> 4357<span>Added in: v14.8.0, v12.19.0</span> 4358<span>N-API version: 8</span> 4359</div> 4360<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_check_object_type_tag</span><span class="hljs-params">(napi_env env, 4361 napi_value js_object, 4362 <span class="hljs-keyword">const</span> napi_type_tag* type_tag, 4363 <span class="hljs-type">bool</span>* result)</span>;</code></pre> 4364<ul> 4365<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4366<li><code>[in] js_object</code>: The JavaScript object whose type tag to examine.</li> 4367<li><code>[in] type_tag</code>: The tag with which to compare any tag found on the object.</li> 4368<li><code>[out] result</code>: Whether the type tag given matched the type tag on the 4369object. <code>false</code> is also returned if no type tag was found on the object.</li> 4370</ul> 4371<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4372<p>Compares the pointer given as <code>type_tag</code> with any that can be found on 4373<code>js_object</code>. If no tag is found on <code>js_object</code> or, if a tag is found but it does 4374not match <code>type_tag</code>, then <code>result</code> is set to <code>false</code>. If a tag is found and it 4375matches <code>type_tag</code>, then <code>result</code> is set to <code>true</code>.</p> 4376<h4>napi_add_finalizer<span><a class="mark" href="#n_api_napi_add_finalizer" id="n_api_napi_add_finalizer">#</a></span></h4> 4377<div class="api_metadata"> 4378<span>Added in: v8.0.0</span> 4379<span>N-API version: 5</span> 4380</div> 4381<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_add_finalizer</span><span class="hljs-params">(napi_env env, 4382 napi_value js_object, 4383 <span class="hljs-type">void</span>* native_object, 4384 napi_finalize finalize_cb, 4385 <span class="hljs-type">void</span>* finalize_hint, 4386 napi_ref* result)</span>;</code></pre> 4387<ul> 4388<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4389<li><code>[in] js_object</code>: The JavaScript object to which the native data will be 4390attached.</li> 4391<li><code>[in] native_object</code>: The native data that will be attached to the JavaScript 4392object.</li> 4393<li><code>[in] finalize_cb</code>: Native callback that will be used to free the 4394native data when the JavaScript object is ready for garbage-collection. 4395<a href="#n_api_napi_finalize"><code>napi_finalize</code></a> provides more details.</li> 4396<li><code>[in] finalize_hint</code>: Optional contextual hint that is passed to the 4397finalize callback.</li> 4398<li><code>[out] result</code>: Optional reference to the JavaScript object.</li> 4399</ul> 4400<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4401<p>Adds a <code>napi_finalize</code> callback which will be called when the JavaScript object 4402in <code>js_object</code> is ready for garbage collection. This API is similar to 4403<code>napi_wrap()</code> except that:</p> 4404<ul> 4405<li>the native data cannot be retrieved later using <code>napi_unwrap()</code>,</li> 4406<li>nor can it be removed later using <code>napi_remove_wrap()</code>, and</li> 4407<li>the API can be called multiple times with different data items in order to 4408attach each of them to the JavaScript object, and</li> 4409<li>the object manipulated by the API can be used with <code>napi_wrap()</code>.</li> 4410</ul> 4411<p><em>Caution</em>: The optional returned reference (if obtained) should be deleted via 4412<a href="#n_api_napi_delete_reference"><code>napi_delete_reference</code></a> ONLY in response to the finalize callback 4413invocation. If it is deleted before then, then the finalize callback may never 4414be invoked. Therefore, when obtaining a reference a finalize callback is also 4415required in order to enable correct disposal of the reference.</p> 4416</section><section><h3>Simple asynchronous operations<span><a class="mark" href="#n_api_simple_asynchronous_operations" id="n_api_simple_asynchronous_operations">#</a></span></h3> 4417<p>Addon modules often need to leverage async helpers from libuv as part of their 4418implementation. This allows them to schedule work to be executed asynchronously 4419so that their methods can return in advance of the work being completed. This 4420allows them to avoid blocking overall execution of the Node.js application.</p> 4421<p>Node-API provides an ABI-stable interface for these 4422supporting functions which covers the most common asynchronous use cases.</p> 4423<p>Node-API defines the <code>napi_async_work</code> structure which is used to manage 4424asynchronous workers. Instances are created/deleted with 4425<a href="#n_api_napi_create_async_work"><code>napi_create_async_work</code></a> and <a href="#n_api_napi_delete_async_work"><code>napi_delete_async_work</code></a>.</p> 4426<p>The <code>execute</code> and <code>complete</code> callbacks are functions that will be 4427invoked when the executor is ready to execute and when it completes its 4428task respectively.</p> 4429<p>The <code>execute</code> function should avoid making any Node-API calls 4430that could result in the execution of JavaScript or interaction with 4431JavaScript objects. Most often, any code that needs to make Node-API 4432calls should be made in <code>complete</code> callback instead. 4433Avoid using the <code>napi_env</code> parameter in the execute callback as 4434it will likely execute JavaScript.</p> 4435<p>These functions implement the following interfaces:</p> 4436<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_execute_callback)</span><span class="hljs-params">(napi_env env, 4437 <span class="hljs-type">void</span>* data)</span>; 4438<span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_complete_callback)</span><span class="hljs-params">(napi_env env, 4439 napi_status status, 4440 <span class="hljs-type">void</span>* data)</span>;</code></pre> 4441<p>When these methods are invoked, the <code>data</code> parameter passed will be the 4442addon-provided <code>void*</code> data that was passed into the 4443<code>napi_create_async_work</code> call.</p> 4444<p>Once created the async worker can be queued 4445for execution using the <a href="#n_api_napi_queue_async_work"><code>napi_queue_async_work</code></a> function:</p> 4446<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work</span><span class="hljs-params">(napi_env env, 4447 napi_async_work work)</span>;</code></pre> 4448<p><a href="#n_api_napi_cancel_async_work"><code>napi_cancel_async_work</code></a> can be used if the work needs 4449to be cancelled before the work has started execution.</p> 4450<p>After calling <a href="#n_api_napi_cancel_async_work"><code>napi_cancel_async_work</code></a>, the <code>complete</code> callback 4451will be invoked with a status value of <code>napi_cancelled</code>. 4452The work should not be deleted before the <code>complete</code> 4453callback invocation, even when it was cancelled.</p> 4454<h4>napi_create_async_work<span><a class="mark" href="#n_api_napi_create_async_work" id="n_api_napi_create_async_work">#</a></span></h4> 4455<div class="api_metadata"> 4456<details class="changelog"><summary>History</summary> 4457<table> 4458<tbody><tr><th>Version</th><th>Changes</th></tr> 4459<tr><td>v8.6.0</td> 4460<td><p>Added <code>async_resource</code> and <code>async_resource_name</code> parameters.</p></td></tr> 4461<tr><td>v8.0.0</td> 4462<td><p><span>Added in: v8.0.0</span></p></td></tr> 4463</tbody></table> 4464</details> 4465<span>N-API version: 1</span> 4466</div> 4467<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_async_work</span><span class="hljs-params">(napi_env env, 4468 napi_value async_resource, 4469 napi_value async_resource_name, 4470 napi_async_execute_callback execute, 4471 napi_async_complete_callback complete, 4472 <span class="hljs-type">void</span>* data, 4473 napi_async_work* result)</span>;</code></pre> 4474<ul> 4475<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4476<li><code>[in] async_resource</code>: An optional object associated with the async work 4477that will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource"><code>init</code> hooks</a>.</li> 4478<li><code>[in] async_resource_name</code>: Identifier for the kind of resource that is being 4479provided for diagnostic information exposed by the <code>async_hooks</code> API.</li> 4480<li><code>[in] execute</code>: The native function which should be called to execute the 4481logic asynchronously. The given function is called from a worker pool thread 4482and can execute in parallel with the main event loop thread.</li> 4483<li><code>[in] complete</code>: The native function which will be called when the 4484asynchronous logic is completed or is cancelled. The given function is called 4485from the main event loop thread. <a href="#n_api_napi_async_complete_callback"><code>napi_async_complete_callback</code></a> provides 4486more details.</li> 4487<li><code>[in] data</code>: User-provided data context. This will be passed back into the 4488execute and complete functions.</li> 4489<li><code>[out] result</code>: <code>napi_async_work*</code> which is the handle to the newly created 4490async work.</li> 4491</ul> 4492<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4493<p>This API allocates a work object that is used to execute logic asynchronously. 4494It should be freed using <a href="#n_api_napi_delete_async_work"><code>napi_delete_async_work</code></a> once the work is no longer 4495required.</p> 4496<p><code>async_resource_name</code> should be a null-terminated, UTF-8-encoded string.</p> 4497<p>The <code>async_resource_name</code> identifier is provided by the user and should be 4498representative of the type of async work being performed. It is also recommended 4499to apply namespacing to the identifier, e.g. by including the module name. See 4500the <a href="async_hooks.html#async_hooks_type"><code>async_hooks</code> documentation</a> for more information.</p> 4501<h4>napi_delete_async_work<span><a class="mark" href="#n_api_napi_delete_async_work" id="n_api_napi_delete_async_work">#</a></span></h4> 4502<div class="api_metadata"> 4503<span>Added in: v8.0.0</span> 4504<span>N-API version: 1</span> 4505</div> 4506<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_async_work</span><span class="hljs-params">(napi_env env, 4507 napi_async_work work)</span>;</code></pre> 4508<ul> 4509<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4510<li><code>[in] work</code>: The handle returned by the call to <code>napi_create_async_work</code>.</li> 4511</ul> 4512<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4513<p>This API frees a previously allocated work object.</p> 4514<p>This API can be called even if there is a pending JavaScript exception.</p> 4515<h4>napi_queue_async_work<span><a class="mark" href="#n_api_napi_queue_async_work" id="n_api_napi_queue_async_work">#</a></span></h4> 4516<div class="api_metadata"> 4517<span>Added in: v8.0.0</span> 4518<span>N-API version: 1</span> 4519</div> 4520<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work</span><span class="hljs-params">(napi_env env, 4521 napi_async_work work)</span>;</code></pre> 4522<ul> 4523<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4524<li><code>[in] work</code>: The handle returned by the call to <code>napi_create_async_work</code>.</li> 4525</ul> 4526<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4527<p>This API requests that the previously allocated work be scheduled 4528for execution. Once it returns successfully, this API must not be called again 4529with the same <code>napi_async_work</code> item or the result will be undefined.</p> 4530<h4>napi_cancel_async_work<span><a class="mark" href="#n_api_napi_cancel_async_work" id="n_api_napi_cancel_async_work">#</a></span></h4> 4531<div class="api_metadata"> 4532<span>Added in: v8.0.0</span> 4533<span>N-API version: 1</span> 4534</div> 4535<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_cancel_async_work</span><span class="hljs-params">(napi_env env, 4536 napi_async_work work)</span>;</code></pre> 4537<ul> 4538<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4539<li><code>[in] work</code>: The handle returned by the call to <code>napi_create_async_work</code>.</li> 4540</ul> 4541<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4542<p>This API cancels queued work if it has not yet 4543been started. If it has already started executing, it cannot be 4544cancelled and <code>napi_generic_failure</code> will be returned. If successful, 4545the <code>complete</code> callback will be invoked with a status value of 4546<code>napi_cancelled</code>. The work should not be deleted before the <code>complete</code> 4547callback invocation, even if it has been successfully cancelled.</p> 4548<p>This API can be called even if there is a pending JavaScript exception.</p> 4549</section><section><h3>Custom asynchronous operations<span><a class="mark" href="#n_api_custom_asynchronous_operations" id="n_api_custom_asynchronous_operations">#</a></span></h3> 4550<p>The simple asynchronous work APIs above may not be appropriate for every 4551scenario. When using any other asynchronous mechanism, the following APIs 4552are necessary to ensure an asynchronous operation is properly tracked by 4553the runtime.</p> 4554<h4>napi_async_init<span><a class="mark" href="#n_api_napi_async_init" id="n_api_napi_async_init">#</a></span></h4> 4555<div class="api_metadata"> 4556<span>Added in: v8.6.0</span> 4557<span>N-API version: 1</span> 4558</div> 4559<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_init</span><span class="hljs-params">(napi_env env, 4560 napi_value async_resource, 4561 napi_value async_resource_name, 4562 napi_async_context* result)</span></code></pre> 4563<ul> 4564<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4565<li><code>[in] async_resource</code>: Object associated with the async work 4566that will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource"><code>init</code> hooks</a> and can be 4567accessed by <a href="async_hooks.html#async_hooks_async_hooks_executionasyncresource"><code>async_hooks.executionAsyncResource()</code></a>.</li> 4568<li><code>[in] async_resource_name</code>: Identifier for the kind of resource that is being 4569provided for diagnostic information exposed by the <code>async_hooks</code> API.</li> 4570<li><code>[out] result</code>: The initialized async context.</li> 4571</ul> 4572<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4573<p>The <code>async_resource</code> object needs to be kept alive until 4574<a href="#n_api_napi_async_destroy"><code>napi_async_destroy</code></a> to keep <code>async_hooks</code> related API acts correctly. In 4575order to retain ABI compatibility with previous versions, <code>napi_async_context</code>s 4576are not maintaining the strong reference to the <code>async_resource</code> objects to 4577avoid introducing causing memory leaks. However, if the <code>async_resource</code> is 4578garbage collected by JavaScript engine before the <code>napi_async_context</code> was 4579destroyed by <code>napi_async_destroy</code>, calling <code>napi_async_context</code> related APIs 4580like <a href="#n_api_napi_open_callback_scope"><code>napi_open_callback_scope</code></a> and <a href="#n_api_napi_make_callback"><code>napi_make_callback</code></a> can cause 4581problems like loss of async context when using the <code>AsyncLocalStoage</code> API.</p> 4582<p>In order to retain ABI compatibility with previous versions, passing <code>NULL</code> 4583for <code>async_resource</code> does not result in an error. However, this is not 4584recommended as this will result poor results with <code>async_hooks</code> 4585<a href="async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource"><code>init</code> hooks</a> and <code>async_hooks.executionAsyncResource()</code> as the resource is 4586now required by the underlying <code>async_hooks</code> implementation in order to provide 4587the linkage between async callbacks.</p> 4588<h4>napi_async_destroy<span><a class="mark" href="#n_api_napi_async_destroy" id="n_api_napi_async_destroy">#</a></span></h4> 4589<div class="api_metadata"> 4590<span>Added in: v8.6.0</span> 4591<span>N-API version: 1</span> 4592</div> 4593<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_destroy</span><span class="hljs-params">(napi_env env, 4594 napi_async_context async_context)</span>;</code></pre> 4595<ul> 4596<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4597<li><code>[in] async_context</code>: The async context to be destroyed.</li> 4598</ul> 4599<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4600<p>This API can be called even if there is a pending JavaScript exception.</p> 4601<h4>napi_make_callback<span><a class="mark" href="#n_api_napi_make_callback" id="n_api_napi_make_callback">#</a></span></h4> 4602<div class="api_metadata"> 4603<details class="changelog"><summary>History</summary> 4604<table> 4605<tbody><tr><th>Version</th><th>Changes</th></tr> 4606<tr><td>v8.6.0</td> 4607<td><p>Added <code>async_context</code> parameter.</p></td></tr> 4608<tr><td>v8.0.0</td> 4609<td><p><span>Added in: v8.0.0</span></p></td></tr> 4610</tbody></table> 4611</details> 4612<span>N-API version: 1</span> 4613</div> 4614<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_make_callback</span><span class="hljs-params">(napi_env env, 4615 napi_async_context async_context, 4616 napi_value recv, 4617 napi_value func, 4618 <span class="hljs-type">size_t</span> argc, 4619 <span class="hljs-keyword">const</span> napi_value* argv, 4620 napi_value* result)</span>;</code></pre> 4621<ul> 4622<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4623<li><code>[in] async_context</code>: Context for the async operation that is 4624invoking the callback. This should normally be a value previously 4625obtained from <a href="#n_api_napi_async_init"><code>napi_async_init</code></a>. 4626In order to retain ABI compatibility with previous versions, passing <code>NULL</code> 4627for <code>async_context</code> does not result in an error. However, this results 4628in incorrect operation of async hooks. Potential issues include loss of 4629async context when using the <code>AsyncLocalStorage</code> API.</li> 4630<li><code>[in] recv</code>: The <code>this</code> value passed to the called function.</li> 4631<li><code>[in] func</code>: <code>napi_value</code> representing the JavaScript function to be invoked.</li> 4632<li><code>[in] argc</code>: The count of elements in the <code>argv</code> array.</li> 4633<li><code>[in] argv</code>: Array of JavaScript values as <code>napi_value</code> representing the 4634arguments to the function.</li> 4635<li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned.</li> 4636</ul> 4637<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4638<p>This method allows a JavaScript function object to be called from a native 4639add-on. This API is similar to <code>napi_call_function</code>. However, it is used to call 4640<em>from</em> native code back <em>into</em> JavaScript <em>after</em> returning from an async 4641operation (when there is no other script on the stack). It is a fairly simple 4642wrapper around <code>node::MakeCallback</code>.</p> 4643<p>Note it is <em>not</em> necessary to use <code>napi_make_callback</code> from within a 4644<code>napi_async_complete_callback</code>; in that situation the callback's async 4645context has already been set up, so a direct call to <code>napi_call_function</code> 4646is sufficient and appropriate. Use of the <code>napi_make_callback</code> function 4647may be required when implementing custom async behavior that does not use 4648<code>napi_create_async_work</code>.</p> 4649<p>Any <code>process.nextTick</code>s or Promises scheduled on the microtask queue by 4650JavaScript during the callback are ran before returning back to C/C++.</p> 4651<h4>napi_open_callback_scope<span><a class="mark" href="#n_api_napi_open_callback_scope" id="n_api_napi_open_callback_scope">#</a></span></h4> 4652<div class="api_metadata"> 4653<span>Added in: v9.6.0</span> 4654<span>N-API version: 3</span> 4655</div> 4656<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_open_callback_scope</span><span class="hljs-params">(napi_env env, 4657 napi_value resource_object, 4658 napi_async_context context, 4659 napi_callback_scope* result)</span></code></pre> 4660<ul> 4661<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4662<li><code>[in] resource_object</code>: An object associated with the async work 4663that will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource"><code>init</code> hooks</a>. This 4664parameter has been deprecated and is ignored at runtime. Use the 4665<code>async_resource</code> parameter in <a href="#n_api_napi_async_init"><code>napi_async_init</code></a> instead.</li> 4666<li><code>[in] context</code>: Context for the async operation that is invoking the callback. 4667This should be a value previously obtained from <a href="#n_api_napi_async_init"><code>napi_async_init</code></a>.</li> 4668<li><code>[out] result</code>: The newly created scope.</li> 4669</ul> 4670<p>There are cases (for example, resolving promises) where it is 4671necessary to have the equivalent of the scope associated with a callback 4672in place when making certain Node-API calls. If there is no other script on 4673the stack the <a href="#n_api_napi_open_callback_scope"><code>napi_open_callback_scope</code></a> and 4674<a href="#n_api_napi_close_callback_scope"><code>napi_close_callback_scope</code></a> functions can be used to open/close 4675the required scope.</p> 4676<h4>napi_close_callback_scope<span><a class="mark" href="#n_api_napi_close_callback_scope" id="n_api_napi_close_callback_scope">#</a></span></h4> 4677<div class="api_metadata"> 4678<span>Added in: v9.6.0</span> 4679<span>N-API version: 3</span> 4680</div> 4681<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_close_callback_scope</span><span class="hljs-params">(napi_env env, 4682 napi_callback_scope scope)</span></code></pre> 4683<ul> 4684<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4685<li><code>[in] scope</code>: The scope to be closed.</li> 4686</ul> 4687<p>This API can be called even if there is a pending JavaScript exception.</p> 4688</section><section><h3>Version management<span><a class="mark" href="#n_api_version_management" id="n_api_version_management">#</a></span></h3> 4689<h4>napi_get_node_version<span><a class="mark" href="#n_api_napi_get_node_version" id="n_api_napi_get_node_version">#</a></span></h4> 4690<div class="api_metadata"> 4691<span>Added in: v8.4.0</span> 4692<span>N-API version: 1</span> 4693</div> 4694<pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span> 4695 <span class="hljs-type">uint32_t</span> major; 4696 <span class="hljs-type">uint32_t</span> minor; 4697 <span class="hljs-type">uint32_t</span> patch; 4698 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* release; 4699} napi_node_version; 4700 4701napi_status <span class="hljs-title function_">napi_get_node_version</span><span class="hljs-params">(napi_env env, 4702 <span class="hljs-keyword">const</span> napi_node_version** version)</span>;</code></pre> 4703<ul> 4704<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4705<li><code>[out] version</code>: A pointer to version information for Node.js itself.</li> 4706</ul> 4707<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4708<p>This function fills the <code>version</code> struct with the major, minor, and patch 4709version of Node.js that is currently running, and the <code>release</code> field with the 4710value of <a href="process.html#process_process_release"><code>process.release.name</code></a>.</p> 4711<p>The returned buffer is statically allocated and does not need to be freed.</p> 4712<h4>napi_get_version<span><a class="mark" href="#n_api_napi_get_version" id="n_api_napi_get_version">#</a></span></h4> 4713<div class="api_metadata"> 4714<span>Added in: v8.0.0</span> 4715<span>N-API version: 1</span> 4716</div> 4717<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_version</span><span class="hljs-params">(napi_env env, 4718 <span class="hljs-type">uint32_t</span>* result)</span>;</code></pre> 4719<ul> 4720<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4721<li><code>[out] result</code>: The highest version of Node-API supported.</li> 4722</ul> 4723<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4724<p>This API returns the highest Node-API version supported by the 4725Node.js runtime. Node-API is planned to be additive such that 4726newer releases of Node.js may support additional API functions. 4727In order to allow an addon to use a newer function when running with 4728versions of Node.js that support it, while providing 4729fallback behavior when running with Node.js versions that don't 4730support it:</p> 4731<ul> 4732<li>Call <code>napi_get_version()</code> to determine if the API is available.</li> 4733<li>If available, dynamically load a pointer to the function using <code>uv_dlsym()</code>.</li> 4734<li>Use the dynamically loaded pointer to invoke the function.</li> 4735<li>If the function is not available, provide an alternate implementation 4736that does not use the function.</li> 4737</ul> 4738</section><section><h3>Memory management<span><a class="mark" href="#n_api_memory_management" id="n_api_memory_management">#</a></span></h3> 4739<h4>napi_adjust_external_memory<span><a class="mark" href="#n_api_napi_adjust_external_memory" id="n_api_napi_adjust_external_memory">#</a></span></h4> 4740<div class="api_metadata"> 4741<span>Added in: v8.5.0</span> 4742<span>N-API version: 1</span> 4743</div> 4744<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_adjust_external_memory</span><span class="hljs-params">(napi_env env, 4745 <span class="hljs-type">int64_t</span> change_in_bytes, 4746 <span class="hljs-type">int64_t</span>* result)</span>;</code></pre> 4747<ul> 4748<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4749<li><code>[in] change_in_bytes</code>: The change in externally allocated memory that is kept 4750alive by JavaScript objects.</li> 4751<li><code>[out] result</code>: The adjusted value</li> 4752</ul> 4753<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4754<p>This function gives V8 an indication of the amount of externally allocated 4755memory that is kept alive by JavaScript objects (i.e. a JavaScript object 4756that points to its own memory allocated by a native module). Registering 4757externally allocated memory will trigger global garbage collections more 4758often than it would otherwise.</p> 4759</section><section><h3>Promises<span><a class="mark" href="#n_api_promises" id="n_api_promises">#</a></span></h3> 4760<p>Node-API provides facilities for creating <code>Promise</code> objects as described in 4761<a href="https://tc39.github.io/ecma262/#sec-promise-objects">Section 25.4</a> of the ECMA specification. It implements promises as a pair of 4762objects. When a promise is created by <code>napi_create_promise()</code>, a "deferred" 4763object is created and returned alongside the <code>Promise</code>. The deferred object is 4764bound to the created <code>Promise</code> and is the only means to resolve or reject the 4765<code>Promise</code> using <code>napi_resolve_deferred()</code> or <code>napi_reject_deferred()</code>. The 4766deferred object that is created by <code>napi_create_promise()</code> is freed by 4767<code>napi_resolve_deferred()</code> or <code>napi_reject_deferred()</code>. The <code>Promise</code> object may 4768be returned to JavaScript where it can be used in the usual fashion.</p> 4769<p>For example, to create a promise and pass it to an asynchronous worker:</p> 4770<pre><code class="language-c">napi_deferred deferred; 4771napi_value promise; 4772napi_status status; 4773 4774<span class="hljs-comment">// Create the promise.</span> 4775status = napi_create_promise(env, &deferred, &promise); 4776<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 4777 4778<span class="hljs-comment">// Pass the deferred to a function that performs an asynchronous action.</span> 4779do_something_asynchronous(deferred); 4780 4781<span class="hljs-comment">// Return the promise to JS</span> 4782<span class="hljs-keyword">return</span> promise;</code></pre> 4783<p>The above function <code>do_something_asynchronous()</code> would perform its asynchronous 4784action and then it would resolve or reject the deferred, thereby concluding the 4785promise and freeing the deferred:</p> 4786<pre><code class="language-c">napi_deferred deferred; 4787napi_value undefined; 4788napi_status status; 4789 4790<span class="hljs-comment">// Create a value with which to conclude the deferred.</span> 4791status = napi_get_undefined(env, &undefined); 4792<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 4793 4794<span class="hljs-comment">// Resolve or reject the promise associated with the deferred depending on</span> 4795<span class="hljs-comment">// whether the asynchronous action succeeded.</span> 4796<span class="hljs-keyword">if</span> (asynchronous_action_succeeded) { 4797 status = napi_resolve_deferred(env, deferred, undefined); 4798} <span class="hljs-keyword">else</span> { 4799 status = napi_reject_deferred(env, deferred, undefined); 4800} 4801<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 4802 4803<span class="hljs-comment">// At this point the deferred has been freed, so we should assign NULL to it.</span> 4804deferred = <span class="hljs-literal">NULL</span>;</code></pre> 4805<h4>napi_create_promise<span><a class="mark" href="#n_api_napi_create_promise" id="n_api_napi_create_promise">#</a></span></h4> 4806<div class="api_metadata"> 4807<span>Added in: v8.5.0</span> 4808<span>N-API version: 1</span> 4809</div> 4810<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_promise</span><span class="hljs-params">(napi_env env, 4811 napi_deferred* deferred, 4812 napi_value* promise)</span>;</code></pre> 4813<ul> 4814<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4815<li><code>[out] deferred</code>: A newly created deferred object which can later be passed to 4816<code>napi_resolve_deferred()</code> or <code>napi_reject_deferred()</code> to resolve resp. reject 4817the associated promise.</li> 4818<li><code>[out] promise</code>: The JavaScript promise associated with the deferred object.</li> 4819</ul> 4820<p>Returns <code>napi_ok</code> if the API succeeded.</p> 4821<p>This API creates a deferred object and a JavaScript promise.</p> 4822<h4>napi_resolve_deferred<span><a class="mark" href="#n_api_napi_resolve_deferred" id="n_api_napi_resolve_deferred">#</a></span></h4> 4823<div class="api_metadata"> 4824<span>Added in: v8.5.0</span> 4825<span>N-API version: 1</span> 4826</div> 4827<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_resolve_deferred</span><span class="hljs-params">(napi_env env, 4828 napi_deferred deferred, 4829 napi_value resolution)</span>;</code></pre> 4830<ul> 4831<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4832<li><code>[in] deferred</code>: The deferred object whose associated promise to resolve.</li> 4833<li><code>[in] resolution</code>: The value with which to resolve the promise.</li> 4834</ul> 4835<p>This API resolves a JavaScript promise by way of the deferred object 4836with which it is associated. Thus, it can only be used to resolve JavaScript 4837promises for which the corresponding deferred object is available. This 4838effectively means that the promise must have been created using 4839<code>napi_create_promise()</code> and the deferred object returned from that call must 4840have been retained in order to be passed to this API.</p> 4841<p>The deferred object is freed upon successful completion.</p> 4842<h4>napi_reject_deferred<span><a class="mark" href="#n_api_napi_reject_deferred" id="n_api_napi_reject_deferred">#</a></span></h4> 4843<div class="api_metadata"> 4844<span>Added in: v8.5.0</span> 4845<span>N-API version: 1</span> 4846</div> 4847<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_reject_deferred</span><span class="hljs-params">(napi_env env, 4848 napi_deferred deferred, 4849 napi_value rejection)</span>;</code></pre> 4850<ul> 4851<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4852<li><code>[in] deferred</code>: The deferred object whose associated promise to resolve.</li> 4853<li><code>[in] rejection</code>: The value with which to reject the promise.</li> 4854</ul> 4855<p>This API rejects a JavaScript promise by way of the deferred object 4856with which it is associated. Thus, it can only be used to reject JavaScript 4857promises for which the corresponding deferred object is available. This 4858effectively means that the promise must have been created using 4859<code>napi_create_promise()</code> and the deferred object returned from that call must 4860have been retained in order to be passed to this API.</p> 4861<p>The deferred object is freed upon successful completion.</p> 4862<h4>napi_is_promise<span><a class="mark" href="#n_api_napi_is_promise" id="n_api_napi_is_promise">#</a></span></h4> 4863<div class="api_metadata"> 4864<span>Added in: v8.5.0</span> 4865<span>N-API version: 1</span> 4866</div> 4867<pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_promise</span><span class="hljs-params">(napi_env env, 4868 napi_value value, 4869 <span class="hljs-type">bool</span>* is_promise)</span>;</code></pre> 4870<ul> 4871<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4872<li><code>[in] value</code>: The value to examine</li> 4873<li><code>[out] is_promise</code>: Flag indicating whether <code>promise</code> is a native promise 4874object (that is, a promise object created by the underlying engine).</li> 4875</ul> 4876</section><section><h3>Script execution<span><a class="mark" href="#n_api_script_execution" id="n_api_script_execution">#</a></span></h3> 4877<p>Node-API provides an API for executing a string containing JavaScript using the 4878underlying JavaScript engine.</p> 4879<h4>napi_run_script<span><a class="mark" href="#n_api_napi_run_script" id="n_api_napi_run_script">#</a></span></h4> 4880<div class="api_metadata"> 4881<span>Added in: v8.5.0</span> 4882<span>N-API version: 1</span> 4883</div> 4884<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_run_script</span><span class="hljs-params">(napi_env env, 4885 napi_value script, 4886 napi_value* result)</span>;</code></pre> 4887<ul> 4888<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4889<li><code>[in] script</code>: A JavaScript string containing the script to execute.</li> 4890<li><code>[out] result</code>: The value resulting from having executed the script.</li> 4891</ul> 4892<p>This function executes a string of JavaScript code and returns its result with 4893the following caveats:</p> 4894<ul> 4895<li>Unlike <code>eval</code>, this function does not allow the script to access the current 4896lexical scope, and therefore also does not allow to access the 4897<a href="modules.html#modules_the_module_scope">module scope</a>, meaning that pseudo-globals such as <code>require</code> will not be 4898available.</li> 4899<li>The script can access the <a href="globals.html">global scope</a>. Function and <code>var</code> declarations 4900in the script will be added to the <a href="globals.html#globals_global"><code>global</code></a> object. Variable declarations 4901made using <code>let</code> and <code>const</code> will be visible globally, but will not be added 4902to the <a href="globals.html#globals_global"><code>global</code></a> object.</li> 4903<li>The value of <code>this</code> is <a href="globals.html#globals_global"><code>global</code></a> within the script.</li> 4904</ul> 4905</section><section><h3>libuv event loop<span><a class="mark" href="#n_api_libuv_event_loop" id="n_api_libuv_event_loop">#</a></span></h3> 4906<p>Node-API provides a function for getting the current event loop associated with 4907a specific <code>napi_env</code>.</p> 4908<h4>napi_get_uv_event_loop<span><a class="mark" href="#n_api_napi_get_uv_event_loop" id="n_api_napi_get_uv_event_loop">#</a></span></h4> 4909<div class="api_metadata"> 4910<span>Added in: v9.3.0, v8.10.0</span> 4911<span>N-API version: 2</span> 4912</div> 4913<pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_uv_event_loop</span><span class="hljs-params">(napi_env env, 4914 <span class="hljs-keyword">struct</span> uv_loop_s** loop)</span>;</code></pre> 4915<ul> 4916<li><code>[in] env</code>: The environment that the API is invoked under.</li> 4917<li><code>[out] loop</code>: The current libuv loop instance.</li> 4918</ul> 4919</section><section><h3>Asynchronous thread-safe function calls<span><a class="mark" href="#n_api_asynchronous_thread_safe_function_calls" id="n_api_asynchronous_thread_safe_function_calls">#</a></span></h3> 4920<p>JavaScript functions can normally only be called from a native addon's main 4921thread. If an addon creates additional threads, then Node-API functions that 4922require a <code>napi_env</code>, <code>napi_value</code>, or <code>napi_ref</code> must not be called from those 4923threads.</p> 4924<p>When an addon has additional threads and JavaScript functions need to be invoked 4925based on the processing completed by those threads, those threads must 4926communicate with the addon's main thread so that the main thread can invoke the 4927JavaScript function on their behalf. The thread-safe function APIs provide an 4928easy way to do this.</p> 4929<p>These APIs provide the type <code>napi_threadsafe_function</code> as well as APIs to 4930create, destroy, and call objects of this type. 4931<code>napi_create_threadsafe_function()</code> creates a persistent reference to a 4932<code>napi_value</code> that holds a JavaScript function which can be called from multiple 4933threads. The calls happen asynchronously. This means that values with which the 4934JavaScript callback is to be called will be placed in a queue, and, for each 4935value in the queue, a call will eventually be made to the JavaScript function.</p> 4936<p>Upon creation of a <code>napi_threadsafe_function</code> a <code>napi_finalize</code> callback can be 4937provided. This callback will be invoked on the main thread when the thread-safe 4938function is about to be destroyed. It receives the context and the finalize data 4939given during construction, and provides an opportunity for cleaning up after the 4940threads e.g. by calling <code>uv_thread_join()</code>. <strong>Aside from the main loop thread, 4941no threads should be using the thread-safe function after the finalize callback 4942completes.</strong></p> 4943<p>The <code>context</code> given during the call to <code>napi_create_threadsafe_function()</code> can 4944be retrieved from any thread with a call to 4945<code>napi_get_threadsafe_function_context()</code>.</p> 4946<h4>Calling a thread-safe function<span><a class="mark" href="#n_api_calling_a_thread_safe_function" id="n_api_calling_a_thread_safe_function">#</a></span></h4> 4947<p><code>napi_call_threadsafe_function()</code> can be used for initiating a call into 4948JavaScript. <code>napi_call_threadsafe_function()</code> accepts a parameter which controls 4949whether the API behaves blockingly. If set to <code>napi_tsfn_nonblocking</code>, the API 4950behaves non-blockingly, returning <code>napi_queue_full</code> if the queue was full, 4951preventing data from being successfully added to the queue. If set to 4952<code>napi_tsfn_blocking</code>, the API blocks until space becomes available in the queue. 4953<code>napi_call_threadsafe_function()</code> never blocks if the thread-safe function was 4954created with a maximum queue size of 0.</p> 4955<p><code>napi_call_threadsafe_function()</code> should not be called with <code>napi_tsfn_blocking</code> 4956from a JavaScript thread, because, if the queue is full, it may cause the 4957JavaScript thread to deadlock.</p> 4958<p>The actual call into JavaScript is controlled by the callback given via the 4959<code>call_js_cb</code> parameter. <code>call_js_cb</code> is invoked on the main thread once for each 4960value that was placed into the queue by a successful call to 4961<code>napi_call_threadsafe_function()</code>. If such a callback is not given, a default 4962callback will be used, and the resulting JavaScript call will have no arguments. 4963The <code>call_js_cb</code> callback receives the JavaScript function to call as a 4964<code>napi_value</code> in its parameters, as well as the <code>void*</code> context pointer used when 4965creating the <code>napi_threadsafe_function</code>, and the next data pointer that was 4966created by one of the secondary threads. The callback can then use an API such 4967as <code>napi_call_function()</code> to call into JavaScript.</p> 4968<p>The callback may also be invoked with <code>env</code> and <code>call_js_cb</code> both set to <code>NULL</code> 4969to indicate that calls into JavaScript are no longer possible, while items 4970remain in the queue that may need to be freed. This normally occurs when the 4971Node.js process exits while there is a thread-safe function still active.</p> 4972<p>It is not necessary to call into JavaScript via <code>napi_make_callback()</code> because 4973Node-API runs <code>call_js_cb</code> in a context appropriate for callbacks.</p> 4974<h4>Reference counting of thread-safe functions<span><a class="mark" href="#n_api_reference_counting_of_thread_safe_functions" id="n_api_reference_counting_of_thread_safe_functions">#</a></span></h4> 4975<p>Threads can be added to and removed from a <code>napi_threadsafe_function</code> object 4976during its existence. Thus, in addition to specifying an initial number of 4977threads upon creation, <code>napi_acquire_threadsafe_function</code> can be called to 4978indicate that a new thread will start making use of the thread-safe function. 4979Similarly, <code>napi_release_threadsafe_function</code> can be called to indicate that an 4980existing thread will stop making use of the thread-safe function.</p> 4981<p><code>napi_threadsafe_function</code> objects are destroyed when every thread which uses 4982the object has called <code>napi_release_threadsafe_function()</code> or has received a 4983return status of <code>napi_closing</code> in response to a call to 4984<code>napi_call_threadsafe_function</code>. The queue is emptied before the 4985<code>napi_threadsafe_function</code> is destroyed. <code>napi_release_threadsafe_function()</code> 4986should be the last API call made in conjunction with a given 4987<code>napi_threadsafe_function</code>, because after the call completes, there is no 4988guarantee that the <code>napi_threadsafe_function</code> is still allocated. For the same 4989reason, do not use a thread-safe function 4990after receiving a return value of <code>napi_closing</code> in response to a call to 4991<code>napi_call_threadsafe_function</code>. Data associated with the 4992<code>napi_threadsafe_function</code> can be freed in its <code>napi_finalize</code> callback which 4993was passed to <code>napi_create_threadsafe_function()</code>. The parameter 4994<code>initial_thread_count</code> of <code>napi_create_threadsafe_function</code> marks the initial 4995number of aquisitions of the thread-safe functions, instead of calling 4996<code>napi_acquire_threadsafe_function</code> multiple times at creation.</p> 4997<p>Once the number of threads making use of a <code>napi_threadsafe_function</code> reaches 4998zero, no further threads can start making use of it by calling 4999<code>napi_acquire_threadsafe_function()</code>. In fact, all subsequent API calls 5000associated with it, except <code>napi_release_threadsafe_function()</code>, will return an 5001error value of <code>napi_closing</code>.</p> 5002<p>The thread-safe function can be "aborted" by giving a value of <code>napi_tsfn_abort</code> 5003to <code>napi_release_threadsafe_function()</code>. This will cause all subsequent APIs 5004associated with the thread-safe function except 5005<code>napi_release_threadsafe_function()</code> to return <code>napi_closing</code> even before its 5006reference count reaches zero. In particular, <code>napi_call_threadsafe_function()</code> 5007will return <code>napi_closing</code>, thus informing the threads that it is no longer 5008possible to make asynchronous calls to the thread-safe function. This can be 5009used as a criterion for terminating the thread. <strong>Upon receiving a return value 5010of <code>napi_closing</code> from <code>napi_call_threadsafe_function()</code> a thread must not use 5011the thread-safe function anymore because it is no longer guaranteed to 5012be allocated.</strong></p> 5013<h4>Deciding whether to keep the process running<span><a class="mark" href="#n_api_deciding_whether_to_keep_the_process_running" id="n_api_deciding_whether_to_keep_the_process_running">#</a></span></h4> 5014<p>Similarly to libuv handles, thread-safe functions can be "referenced" and 5015"unreferenced". A "referenced" thread-safe function will cause the event loop on 5016the thread on which it is created to remain alive until the thread-safe function 5017is destroyed. In contrast, an "unreferenced" thread-safe function will not 5018prevent the event loop from exiting. The APIs <code>napi_ref_threadsafe_function</code> and 5019<code>napi_unref_threadsafe_function</code> exist for this purpose.</p> 5020<p>Neither does <code>napi_unref_threadsafe_function</code> mark the thread-safe functions as 5021able to be destroyed nor does <code>napi_ref_threadsafe_function</code> prevent it from 5022being destroyed.</p> 5023<h4>napi_create_threadsafe_function<span><a class="mark" href="#n_api_napi_create_threadsafe_function" id="n_api_napi_create_threadsafe_function">#</a></span></h4> 5024<div class="api_metadata"> 5025<details class="changelog"><summary>History</summary> 5026<table> 5027<tbody><tr><th>Version</th><th>Changes</th></tr> 5028<tr><td>v12.6.0, v10.17.0</td> 5029<td><p>Made <code>func</code> parameter optional with custom <code>call_js_cb</code>.</p></td></tr> 5030<tr><td>v10.6.0</td> 5031<td><p><span>Added in: v10.6.0</span></p></td></tr> 5032</tbody></table> 5033</details> 5034<span>N-API version: 4</span> 5035</div> 5036<pre><code class="language-c">NAPI_EXTERN napi_status 5037<span class="hljs-title function_">napi_create_threadsafe_function</span><span class="hljs-params">(napi_env env, 5038 napi_value func, 5039 napi_value async_resource, 5040 napi_value async_resource_name, 5041 <span class="hljs-type">size_t</span> max_queue_size, 5042 <span class="hljs-type">size_t</span> initial_thread_count, 5043 <span class="hljs-type">void</span>* thread_finalize_data, 5044 napi_finalize thread_finalize_cb, 5045 <span class="hljs-type">void</span>* context, 5046 napi_threadsafe_function_call_js call_js_cb, 5047 napi_threadsafe_function* result)</span>;</code></pre> 5048<ul> 5049<li><code>[in] env</code>: The environment that the API is invoked under.</li> 5050<li><code>[in] func</code>: An optional JavaScript function to call from another thread. It 5051must be provided if <code>NULL</code> is passed to <code>call_js_cb</code>.</li> 5052<li><code>[in] async_resource</code>: An optional object associated with the async work that 5053will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource"><code>init</code> hooks</a>.</li> 5054<li><code>[in] async_resource_name</code>: A JavaScript string to provide an identifier for 5055the kind of resource that is being provided for diagnostic information exposed 5056by the <code>async_hooks</code> API.</li> 5057<li><code>[in] max_queue_size</code>: Maximum size of the queue. <code>0</code> for no limit.</li> 5058<li><code>[in] initial_thread_count</code>: The initial number of acquisitions, i.e. the 5059initial number of threads, including the main thread, which will be making use 5060of this function.</li> 5061<li><code>[in] thread_finalize_data</code>: Optional data to be passed to <code>thread_finalize_cb</code>.</li> 5062<li><code>[in] thread_finalize_cb</code>: Optional function to call when the 5063<code>napi_threadsafe_function</code> is being destroyed.</li> 5064<li><code>[in] context</code>: Optional data to attach to the resulting 5065<code>napi_threadsafe_function</code>.</li> 5066<li><code>[in] call_js_cb</code>: Optional callback which calls the JavaScript function in 5067response to a call on a different thread. This callback will be called on the 5068main thread. If not given, the JavaScript function will be called with no 5069parameters and with <code>undefined</code> as its <code>this</code> value. 5070<a href="#n_api_napi_threadsafe_function_call_js"><code>napi_threadsafe_function_call_js</code></a> provides more details.</li> 5071<li><code>[out] result</code>: The asynchronous thread-safe JavaScript function.</li> 5072</ul> 5073<h4>napi_get_threadsafe_function_context<span><a class="mark" href="#n_api_napi_get_threadsafe_function_context" id="n_api_napi_get_threadsafe_function_context">#</a></span></h4> 5074<div class="api_metadata"> 5075<span>Added in: v10.6.0</span> 5076<span>N-API version: 4</span> 5077</div> 5078<pre><code class="language-c">NAPI_EXTERN napi_status 5079<span class="hljs-title function_">napi_get_threadsafe_function_context</span><span class="hljs-params">(napi_threadsafe_function func, 5080 <span class="hljs-type">void</span>** result)</span>;</code></pre> 5081<ul> 5082<li><code>[in] func</code>: The thread-safe function for which to retrieve the context.</li> 5083<li><code>[out] result</code>: The location where to store the context.</li> 5084</ul> 5085<p>This API may be called from any thread which makes use of <code>func</code>.</p> 5086<h4>napi_call_threadsafe_function<span><a class="mark" href="#n_api_napi_call_threadsafe_function" id="n_api_napi_call_threadsafe_function">#</a></span></h4> 5087<div class="api_metadata"> 5088<details class="changelog"><summary>History</summary> 5089<table> 5090<tbody><tr><th>Version</th><th>Changes</th></tr> 5091<tr><td>v14.5.0</td> 5092<td><p>Support for <code>napi_would_deadlock</code> has been reverted.</p></td></tr> 5093<tr><td>v14.1.0</td> 5094<td><p>Return <code>napi_would_deadlock</code> when called with <code>napi_tsfn_blocking</code> from the main thread or a worker thread and the queue is full.</p></td></tr> 5095<tr><td>v10.6.0</td> 5096<td><p><span>Added in: v10.6.0</span></p></td></tr> 5097</tbody></table> 5098</details> 5099<span>N-API version: 4</span> 5100</div> 5101<pre><code class="language-c">NAPI_EXTERN napi_status 5102<span class="hljs-title function_">napi_call_threadsafe_function</span><span class="hljs-params">(napi_threadsafe_function func, 5103 <span class="hljs-type">void</span>* data, 5104 napi_threadsafe_function_call_mode is_blocking)</span>;</code></pre> 5105<ul> 5106<li><code>[in] func</code>: The asynchronous thread-safe JavaScript function to invoke.</li> 5107<li><code>[in] data</code>: Data to send into JavaScript via the callback <code>call_js_cb</code> 5108provided during the creation of the thread-safe JavaScript function.</li> 5109<li><code>[in] is_blocking</code>: Flag whose value can be either <code>napi_tsfn_blocking</code> to 5110indicate that the call should block if the queue is full or 5111<code>napi_tsfn_nonblocking</code> to indicate that the call should return immediately 5112with a status of <code>napi_queue_full</code> whenever the queue is full.</li> 5113</ul> 5114<p>This API should not be called with <code>napi_tsfn_blocking</code> from a JavaScript 5115thread, because, if the queue is full, it may cause the JavaScript thread to 5116deadlock.</p> 5117<p>This API will return <code>napi_closing</code> if <code>napi_release_threadsafe_function()</code> was 5118called with <code>abort</code> set to <code>napi_tsfn_abort</code> from any thread. The value is only 5119added to the queue if the API returns <code>napi_ok</code>.</p> 5120<p>This API may be called from any thread which makes use of <code>func</code>.</p> 5121<h4>napi_acquire_threadsafe_function<span><a class="mark" href="#n_api_napi_acquire_threadsafe_function" id="n_api_napi_acquire_threadsafe_function">#</a></span></h4> 5122<div class="api_metadata"> 5123<span>Added in: v10.6.0</span> 5124<span>N-API version: 4</span> 5125</div> 5126<pre><code class="language-c">NAPI_EXTERN napi_status 5127<span class="hljs-title function_">napi_acquire_threadsafe_function</span><span class="hljs-params">(napi_threadsafe_function func)</span>;</code></pre> 5128<ul> 5129<li><code>[in] func</code>: The asynchronous thread-safe JavaScript function to start making 5130use of.</li> 5131</ul> 5132<p>A thread should call this API before passing <code>func</code> to any other thread-safe 5133function APIs to indicate that it will be making use of <code>func</code>. This prevents 5134<code>func</code> from being destroyed when all other threads have stopped making use of 5135it.</p> 5136<p>This API may be called from any thread which will start making use of <code>func</code>.</p> 5137<h4>napi_release_threadsafe_function<span><a class="mark" href="#n_api_napi_release_threadsafe_function" id="n_api_napi_release_threadsafe_function">#</a></span></h4> 5138<div class="api_metadata"> 5139<span>Added in: v10.6.0</span> 5140<span>N-API version: 4</span> 5141</div> 5142<pre><code class="language-c">NAPI_EXTERN napi_status 5143<span class="hljs-title function_">napi_release_threadsafe_function</span><span class="hljs-params">(napi_threadsafe_function func, 5144 napi_threadsafe_function_release_mode mode)</span>;</code></pre> 5145<ul> 5146<li><code>[in] func</code>: The asynchronous thread-safe JavaScript function whose reference 5147count to decrement.</li> 5148<li><code>[in] mode</code>: Flag whose value can be either <code>napi_tsfn_release</code> to indicate 5149that the current thread will make no further calls to the thread-safe 5150function, or <code>napi_tsfn_abort</code> to indicate that in addition to the current 5151thread, no other thread should make any further calls to the thread-safe 5152function. If set to <code>napi_tsfn_abort</code>, further calls to 5153<code>napi_call_threadsafe_function()</code> will return <code>napi_closing</code>, and no further 5154values will be placed in the queue.</li> 5155</ul> 5156<p>A thread should call this API when it stops making use of <code>func</code>. Passing <code>func</code> 5157to any thread-safe APIs after having called this API has undefined results, as 5158<code>func</code> may have been destroyed.</p> 5159<p>This API may be called from any thread which will stop making use of <code>func</code>.</p> 5160<h4>napi_ref_threadsafe_function<span><a class="mark" href="#n_api_napi_ref_threadsafe_function" id="n_api_napi_ref_threadsafe_function">#</a></span></h4> 5161<div class="api_metadata"> 5162<span>Added in: v10.6.0</span> 5163<span>N-API version: 4</span> 5164</div> 5165<pre><code class="language-c">NAPI_EXTERN napi_status 5166<span class="hljs-title function_">napi_ref_threadsafe_function</span><span class="hljs-params">(napi_env env, napi_threadsafe_function func)</span>;</code></pre> 5167<ul> 5168<li><code>[in] env</code>: The environment that the API is invoked under.</li> 5169<li><code>[in] func</code>: The thread-safe function to reference.</li> 5170</ul> 5171<p>This API is used to indicate that the event loop running on the main thread 5172should not exit until <code>func</code> has been destroyed. Similar to <a href="https://docs.libuv.org/en/v1.x/handle.html#c.uv_ref"><code>uv_ref</code></a> it is 5173also idempotent.</p> 5174<p>Neither does <code>napi_unref_threadsafe_function</code> mark the thread-safe functions as 5175able to be destroyed nor does <code>napi_ref_threadsafe_function</code> prevent it from 5176being destroyed. <code>napi_acquire_threadsafe_function</code> and 5177<code>napi_release_threadsafe_function</code> are available for that purpose.</p> 5178<p>This API may only be called from the main thread.</p> 5179<h4>napi_unref_threadsafe_function<span><a class="mark" href="#n_api_napi_unref_threadsafe_function" id="n_api_napi_unref_threadsafe_function">#</a></span></h4> 5180<div class="api_metadata"> 5181<span>Added in: v10.6.0</span> 5182<span>N-API version: 4</span> 5183</div> 5184<pre><code class="language-c">NAPI_EXTERN napi_status 5185<span class="hljs-title function_">napi_unref_threadsafe_function</span><span class="hljs-params">(napi_env env, napi_threadsafe_function func)</span>;</code></pre> 5186<ul> 5187<li><code>[in] env</code>: The environment that the API is invoked under.</li> 5188<li><code>[in] func</code>: The thread-safe function to unreference.</li> 5189</ul> 5190<p>This API is used to indicate that the event loop running on the main thread 5191may exit before <code>func</code> is destroyed. Similar to <a href="https://docs.libuv.org/en/v1.x/handle.html#c.uv_unref"><code>uv_unref</code></a> it is also 5192idempotent.</p> 5193<p>This API may only be called from the main thread.</p> 5194</section><section><h3>Miscellaneous utilities<span><a class="mark" href="#n_api_miscellaneous_utilities" id="n_api_miscellaneous_utilities">#</a></span></h3> 5195</section><section><h3>node_api_get_module_file_name<span><a class="mark" href="#n_api_node_api_get_module_file_name" id="n_api_node_api_get_module_file_name">#</a></span></h3> 5196<div class="api_metadata"> 5197<span>Added in: v14.18.0</span> 5198</div> 5199<p></p><div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experimental</div><p></p> 5200<pre><code class="language-c">NAPI_EXTERN napi_status 5201<span class="hljs-title function_">node_api_get_module_file_name</span><span class="hljs-params">(napi_env env, <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>** result)</span>; 5202</code></pre> 5203<ul> 5204<li><code>[in] env</code>: The environment that the API is invoked under.</li> 5205<li><code>[out] result</code>: A URL containing the absolute path of the 5206location from which the add-on was loaded. For a file on the local 5207file system it will start with <code>file://</code>. The string is null-terminated and 5208owned by <code>env</code> and must thus not be modified or freed.</li> 5209</ul> 5210<p><code>result</code> may be an empty string if the add-on loading process fails to establish 5211the add-on's file name during loading.</p></section> 5212 <!-- API END --> 5213 </div> 5214 </div> 5215 </div> 5216 <script> 5217 'use strict'; 5218 { 5219 const kCustomPreference = 'customDarkTheme'; 5220 const userSettings = sessionStorage.getItem(kCustomPreference); 5221 const themeToggleButton = document.getElementById('theme-toggle-btn'); 5222 if (userSettings === null && window.matchMedia) { 5223 const mq = window.matchMedia('(prefers-color-scheme: dark)'); 5224 if ('onchange' in mq) { 5225 function mqChangeListener(e) { 5226 document.body.classList.toggle('dark-mode', e.matches); 5227 } 5228 mq.addEventListener('change', mqChangeListener); 5229 if (themeToggleButton) { 5230 themeToggleButton.addEventListener('click', function() { 5231 mq.removeEventListener('change', mqChangeListener); 5232 }, { once: true }); 5233 } 5234 } 5235 if (mq.matches) { 5236 document.body.classList.add('dark-mode'); 5237 } 5238 } else if (userSettings === 'true') { 5239 document.body.classList.add('dark-mode'); 5240 } 5241 if (themeToggleButton) { 5242 themeToggleButton.hidden = false; 5243 themeToggleButton.addEventListener('click', function() { 5244 sessionStorage.setItem( 5245 kCustomPreference, 5246 document.body.classList.toggle('dark-mode') 5247 ); 5248 }); 5249 } 5250 } 5251 </script> 5252</body> 5253</html> 5254