11cb0ef41Sopenharmony_ci<!DOCTYPE html> 21cb0ef41Sopenharmony_ci<html lang="en"> 31cb0ef41Sopenharmony_ci<head> 41cb0ef41Sopenharmony_ci <meta charset="utf-8"> 51cb0ef41Sopenharmony_ci <meta name="viewport" content="width=device-width"> 61cb0ef41Sopenharmony_ci <meta name="nodejs.org:node-version" content="v18.20.1"> 71cb0ef41Sopenharmony_ci <title>C++ addons | Node.js v18.20.1 Documentation</title> 81cb0ef41Sopenharmony_ci <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 91cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/style.css"> 101cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/hljs.css"> 111cb0ef41Sopenharmony_ci <link rel="canonical" href="https://nodejs.org/api/addons.html"> 121cb0ef41Sopenharmony_ci <script async defer src="assets/api.js" type="text/javascript"></script> 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci</head> 151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-addons"> 161cb0ef41Sopenharmony_ci <div id="content" class="clearfix"> 171cb0ef41Sopenharmony_ci <div id="column2" class="interior"> 181cb0ef41Sopenharmony_ci <div id="intro" class="interior"> 191cb0ef41Sopenharmony_ci <a href="/" title="Go back to the home page"> 201cb0ef41Sopenharmony_ci Node.js 211cb0ef41Sopenharmony_ci </a> 221cb0ef41Sopenharmony_ci </div> 231cb0ef41Sopenharmony_ci <ul> 241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 261cb0ef41Sopenharmony_ci</ul> 271cb0ef41Sopenharmony_ci<hr class="line"> 281cb0ef41Sopenharmony_ci<ul> 291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons active">C++ addons</a></li> 341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 891cb0ef41Sopenharmony_ci</ul> 901cb0ef41Sopenharmony_ci<hr class="line"> 911cb0ef41Sopenharmony_ci<ul> 921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 931cb0ef41Sopenharmony_ci</ul> 941cb0ef41Sopenharmony_ci </div> 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci <div id="column1" data-id="addons" class="interior"> 971cb0ef41Sopenharmony_ci <header class="header"> 981cb0ef41Sopenharmony_ci <div class="header-container"> 991cb0ef41Sopenharmony_ci <h1>Node.js v18.20.1 documentation</h1> 1001cb0ef41Sopenharmony_ci <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 1011cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 1021cb0ef41Sopenharmony_ci <path fill="none" d="M0 0h24v24H0z" /> 1031cb0ef41Sopenharmony_ci <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"/> 1041cb0ef41Sopenharmony_ci <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"/> 1051cb0ef41Sopenharmony_ci </svg> 1061cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 1071cb0ef41Sopenharmony_ci <path d="M0 0h24v24H0z" fill="none" /> 1081cb0ef41Sopenharmony_ci <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"/> 1091cb0ef41Sopenharmony_ci </svg> 1101cb0ef41Sopenharmony_ci </button> 1111cb0ef41Sopenharmony_ci </div> 1121cb0ef41Sopenharmony_ci <div id="gtoc"> 1131cb0ef41Sopenharmony_ci <ul> 1141cb0ef41Sopenharmony_ci <li class="pinned-header">Node.js v18.20.1</li> 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci <li class="picker-header"> 1171cb0ef41Sopenharmony_ci <a href="#"> 1181cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 1191cb0ef41Sopenharmony_ci Table of contents 1201cb0ef41Sopenharmony_ci </a> 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci <div class="picker"><div class="toc"><ul> 1231cb0ef41Sopenharmony_ci<li><a href="#c-addons">C++ addons</a> 1241cb0ef41Sopenharmony_ci<ul> 1251cb0ef41Sopenharmony_ci<li><a href="#hello-world">Hello world</a> 1261cb0ef41Sopenharmony_ci<ul> 1271cb0ef41Sopenharmony_ci<li><a href="#context-aware-addons">Context-aware addons</a> 1281cb0ef41Sopenharmony_ci<ul> 1291cb0ef41Sopenharmony_ci<li><a href="#worker-support">Worker support</a></li> 1301cb0ef41Sopenharmony_ci</ul> 1311cb0ef41Sopenharmony_ci</li> 1321cb0ef41Sopenharmony_ci<li><a href="#building">Building</a></li> 1331cb0ef41Sopenharmony_ci<li><a href="#linking-to-libraries-included-with-nodejs">Linking to libraries included with Node.js</a></li> 1341cb0ef41Sopenharmony_ci<li><a href="#loading-addons-using-require">Loading addons using <code>require()</code></a></li> 1351cb0ef41Sopenharmony_ci</ul> 1361cb0ef41Sopenharmony_ci</li> 1371cb0ef41Sopenharmony_ci<li><a href="#native-abstractions-for-nodejs">Native abstractions for Node.js</a></li> 1381cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#node-api">Node-API</a></span></li> 1391cb0ef41Sopenharmony_ci<li><a href="#addon-examples">Addon examples</a> 1401cb0ef41Sopenharmony_ci<ul> 1411cb0ef41Sopenharmony_ci<li><a href="#function-arguments">Function arguments</a></li> 1421cb0ef41Sopenharmony_ci<li><a href="#callbacks">Callbacks</a></li> 1431cb0ef41Sopenharmony_ci<li><a href="#object-factory">Object factory</a></li> 1441cb0ef41Sopenharmony_ci<li><a href="#function-factory">Function factory</a></li> 1451cb0ef41Sopenharmony_ci<li><a href="#wrapping-c-objects">Wrapping C++ objects</a></li> 1461cb0ef41Sopenharmony_ci<li><a href="#factory-of-wrapped-objects">Factory of wrapped objects</a></li> 1471cb0ef41Sopenharmony_ci<li><a href="#passing-wrapped-objects-around">Passing wrapped objects around</a></li> 1481cb0ef41Sopenharmony_ci</ul> 1491cb0ef41Sopenharmony_ci</li> 1501cb0ef41Sopenharmony_ci</ul> 1511cb0ef41Sopenharmony_ci</li> 1521cb0ef41Sopenharmony_ci</ul></div></div> 1531cb0ef41Sopenharmony_ci </li> 1541cb0ef41Sopenharmony_ci 1551cb0ef41Sopenharmony_ci 1561cb0ef41Sopenharmony_ci <li class="picker-header"> 1571cb0ef41Sopenharmony_ci <a href="#"> 1581cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 1591cb0ef41Sopenharmony_ci Index 1601cb0ef41Sopenharmony_ci </a> 1611cb0ef41Sopenharmony_ci 1621cb0ef41Sopenharmony_ci <div class="picker"><ul> 1631cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 1641cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ci <li> 1671cb0ef41Sopenharmony_ci <a href="index.html">Index</a> 1681cb0ef41Sopenharmony_ci </li> 1691cb0ef41Sopenharmony_ci </ul> 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_ci<hr class="line"> 1721cb0ef41Sopenharmony_ci<ul> 1731cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 1741cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 1751cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 1761cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 1771cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons active">C++ addons</a></li> 1781cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 1791cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 1801cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 1811cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 1821cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 1831cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 1841cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 1851cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 1861cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 1871cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 1881cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 1891cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 1901cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 1911cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 1921cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 1931cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 1941cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 1951cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 1961cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 1971cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 1981cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 1991cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 2001cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 2011cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 2021cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 2031cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 2041cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 2051cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 2061cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 2071cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 2081cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 2091cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 2101cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 2111cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 2121cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 2131cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 2141cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 2151cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 2161cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 2171cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 2181cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 2191cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 2201cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 2211cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 2221cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 2231cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 2241cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 2251cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 2261cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 2271cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 2281cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 2291cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 2301cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 2311cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 2321cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 2331cb0ef41Sopenharmony_ci</ul> 2341cb0ef41Sopenharmony_ci<hr class="line"> 2351cb0ef41Sopenharmony_ci<ul> 2361cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 2371cb0ef41Sopenharmony_ci</ul></div> 2381cb0ef41Sopenharmony_ci </li> 2391cb0ef41Sopenharmony_ci 2401cb0ef41Sopenharmony_ci 2411cb0ef41Sopenharmony_ci <li class="picker-header"> 2421cb0ef41Sopenharmony_ci <a href="#"> 2431cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 2441cb0ef41Sopenharmony_ci Other versions 2451cb0ef41Sopenharmony_ci </a> 2461cb0ef41Sopenharmony_ci <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/addons.html">21.x</a></li> 2471cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/addons.html">20.x <b>LTS</b></a></li> 2481cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/addons.html">19.x</a></li> 2491cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/addons.html">18.x <b>LTS</b></a></li> 2501cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/addons.html">17.x</a></li> 2511cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/addons.html">16.x</a></li> 2521cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/addons.html">15.x</a></li> 2531cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/addons.html">14.x</a></li> 2541cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/addons.html">13.x</a></li> 2551cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/addons.html">12.x</a></li> 2561cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/addons.html">11.x</a></li> 2571cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/addons.html">10.x</a></li> 2581cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/addons.html">9.x</a></li> 2591cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/addons.html">8.x</a></li> 2601cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v7.x/api/addons.html">7.x</a></li> 2611cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v6.x/api/addons.html">6.x</a></li> 2621cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v5.x/api/addons.html">5.x</a></li> 2631cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v4.x/api/addons.html">4.x</a></li> 2641cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/addons.html">0.12.x</a></li> 2651cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/addons.html">0.10.x</a></li></ol></div> 2661cb0ef41Sopenharmony_ci </li> 2671cb0ef41Sopenharmony_ci 2681cb0ef41Sopenharmony_ci <li class="picker-header"> 2691cb0ef41Sopenharmony_ci <a href="#"> 2701cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 2711cb0ef41Sopenharmony_ci Options 2721cb0ef41Sopenharmony_ci </a> 2731cb0ef41Sopenharmony_ci 2741cb0ef41Sopenharmony_ci <div class="picker"> 2751cb0ef41Sopenharmony_ci <ul> 2761cb0ef41Sopenharmony_ci <li> 2771cb0ef41Sopenharmony_ci <a href="all.html">View on single page</a> 2781cb0ef41Sopenharmony_ci </li> 2791cb0ef41Sopenharmony_ci <li> 2801cb0ef41Sopenharmony_ci <a href="addons.json">View as JSON</a> 2811cb0ef41Sopenharmony_ci </li> 2821cb0ef41Sopenharmony_ci <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/addons.md">Edit on GitHub</a></li> 2831cb0ef41Sopenharmony_ci </ul> 2841cb0ef41Sopenharmony_ci </div> 2851cb0ef41Sopenharmony_ci </li> 2861cb0ef41Sopenharmony_ci </ul> 2871cb0ef41Sopenharmony_ci </div> 2881cb0ef41Sopenharmony_ci <hr> 2891cb0ef41Sopenharmony_ci </header> 2901cb0ef41Sopenharmony_ci 2911cb0ef41Sopenharmony_ci <details id="toc" open><summary>Table of contents</summary><ul> 2921cb0ef41Sopenharmony_ci<li><a href="#c-addons">C++ addons</a> 2931cb0ef41Sopenharmony_ci<ul> 2941cb0ef41Sopenharmony_ci<li><a href="#hello-world">Hello world</a> 2951cb0ef41Sopenharmony_ci<ul> 2961cb0ef41Sopenharmony_ci<li><a href="#context-aware-addons">Context-aware addons</a> 2971cb0ef41Sopenharmony_ci<ul> 2981cb0ef41Sopenharmony_ci<li><a href="#worker-support">Worker support</a></li> 2991cb0ef41Sopenharmony_ci</ul> 3001cb0ef41Sopenharmony_ci</li> 3011cb0ef41Sopenharmony_ci<li><a href="#building">Building</a></li> 3021cb0ef41Sopenharmony_ci<li><a href="#linking-to-libraries-included-with-nodejs">Linking to libraries included with Node.js</a></li> 3031cb0ef41Sopenharmony_ci<li><a href="#loading-addons-using-require">Loading addons using <code>require()</code></a></li> 3041cb0ef41Sopenharmony_ci</ul> 3051cb0ef41Sopenharmony_ci</li> 3061cb0ef41Sopenharmony_ci<li><a href="#native-abstractions-for-nodejs">Native abstractions for Node.js</a></li> 3071cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#node-api">Node-API</a></span></li> 3081cb0ef41Sopenharmony_ci<li><a href="#addon-examples">Addon examples</a> 3091cb0ef41Sopenharmony_ci<ul> 3101cb0ef41Sopenharmony_ci<li><a href="#function-arguments">Function arguments</a></li> 3111cb0ef41Sopenharmony_ci<li><a href="#callbacks">Callbacks</a></li> 3121cb0ef41Sopenharmony_ci<li><a href="#object-factory">Object factory</a></li> 3131cb0ef41Sopenharmony_ci<li><a href="#function-factory">Function factory</a></li> 3141cb0ef41Sopenharmony_ci<li><a href="#wrapping-c-objects">Wrapping C++ objects</a></li> 3151cb0ef41Sopenharmony_ci<li><a href="#factory-of-wrapped-objects">Factory of wrapped objects</a></li> 3161cb0ef41Sopenharmony_ci<li><a href="#passing-wrapped-objects-around">Passing wrapped objects around</a></li> 3171cb0ef41Sopenharmony_ci</ul> 3181cb0ef41Sopenharmony_ci</li> 3191cb0ef41Sopenharmony_ci</ul> 3201cb0ef41Sopenharmony_ci</li> 3211cb0ef41Sopenharmony_ci</ul></details> 3221cb0ef41Sopenharmony_ci 3231cb0ef41Sopenharmony_ci <div id="apicontent"> 3241cb0ef41Sopenharmony_ci <h2>C++ addons<span><a class="mark" href="#c-addons" id="c-addons">#</a></span><a aria-hidden="true" class="legacy" id="addons_c_addons"></a></h2> 3251cb0ef41Sopenharmony_ci 3261cb0ef41Sopenharmony_ci 3271cb0ef41Sopenharmony_ci<p><em>Addons</em> are dynamically-linked shared objects written in C++. The 3281cb0ef41Sopenharmony_ci<a href="modules.html#requireid"><code>require()</code></a> function can load addons as ordinary Node.js modules. 3291cb0ef41Sopenharmony_ciAddons provide an interface between JavaScript and C/C++ libraries.</p> 3301cb0ef41Sopenharmony_ci<p>There are three options for implementing addons: Node-API, nan, or direct 3311cb0ef41Sopenharmony_ciuse of internal V8, libuv, and Node.js libraries. Unless there is a need for 3321cb0ef41Sopenharmony_cidirect access to functionality which is not exposed by Node-API, use Node-API. 3331cb0ef41Sopenharmony_ciRefer to <a href="n-api.html">C/C++ addons with Node-API</a> for more information on 3341cb0ef41Sopenharmony_ciNode-API.</p> 3351cb0ef41Sopenharmony_ci<p>When not using Node-API, implementing addons is complicated, 3361cb0ef41Sopenharmony_ciinvolving knowledge of several components and APIs:</p> 3371cb0ef41Sopenharmony_ci<ul> 3381cb0ef41Sopenharmony_ci<li> 3391cb0ef41Sopenharmony_ci<p><a href="https://v8.dev/">V8</a>: the C++ library Node.js uses to provide the 3401cb0ef41Sopenharmony_ciJavaScript implementation. V8 provides the mechanisms for creating objects, 3411cb0ef41Sopenharmony_cicalling functions, etc. V8's API is documented mostly in the 3421cb0ef41Sopenharmony_ci<code>v8.h</code> header file (<code>deps/v8/include/v8.h</code> in the Node.js source 3431cb0ef41Sopenharmony_citree), which is also available <a href="https://v8docs.nodesource.com/">online</a>.</p> 3441cb0ef41Sopenharmony_ci</li> 3451cb0ef41Sopenharmony_ci<li> 3461cb0ef41Sopenharmony_ci<p><a href="https://github.com/libuv/libuv">libuv</a>: The C library that implements the Node.js event loop, its worker 3471cb0ef41Sopenharmony_cithreads and all of the asynchronous behaviors of the platform. It also 3481cb0ef41Sopenharmony_ciserves as a cross-platform abstraction library, giving easy, POSIX-like 3491cb0ef41Sopenharmony_ciaccess across all major operating systems to many common system tasks, such 3501cb0ef41Sopenharmony_cias interacting with the file system, sockets, timers, and system events. libuv 3511cb0ef41Sopenharmony_cialso provides a threading abstraction similar to POSIX threads for 3521cb0ef41Sopenharmony_cimore sophisticated asynchronous addons that need to move beyond the 3531cb0ef41Sopenharmony_cistandard event loop. Addon authors should 3541cb0ef41Sopenharmony_ciavoid blocking the event loop with I/O or other time-intensive tasks by 3551cb0ef41Sopenharmony_cioffloading work via libuv to non-blocking system operations, worker threads, 3561cb0ef41Sopenharmony_cior a custom use of libuv threads.</p> 3571cb0ef41Sopenharmony_ci</li> 3581cb0ef41Sopenharmony_ci<li> 3591cb0ef41Sopenharmony_ci<p>Internal Node.js libraries. Node.js itself exports C++ APIs that addons can 3601cb0ef41Sopenharmony_ciuse, the most important of which is the <code>node::ObjectWrap</code> class.</p> 3611cb0ef41Sopenharmony_ci</li> 3621cb0ef41Sopenharmony_ci<li> 3631cb0ef41Sopenharmony_ci<p>Node.js includes other statically linked libraries including OpenSSL. These 3641cb0ef41Sopenharmony_ciother libraries are located in the <code>deps/</code> directory in the Node.js source 3651cb0ef41Sopenharmony_citree. Only the libuv, OpenSSL, V8, and zlib symbols are purposefully 3661cb0ef41Sopenharmony_cire-exported by Node.js and may be used to various extents by addons. See 3671cb0ef41Sopenharmony_ci<a href="#linking-to-libraries-included-with-nodejs">Linking to libraries included with Node.js</a> for additional information.</p> 3681cb0ef41Sopenharmony_ci</li> 3691cb0ef41Sopenharmony_ci</ul> 3701cb0ef41Sopenharmony_ci<p>All of the following examples are available for <a href="https://github.com/nodejs/node-addon-examples">download</a> and may 3711cb0ef41Sopenharmony_cibe used as the starting-point for an addon.</p> 3721cb0ef41Sopenharmony_ci<section><h3>Hello world<span><a class="mark" href="#hello-world" id="hello-world">#</a></span><a aria-hidden="true" class="legacy" id="addons_hello_world"></a></h3> 3731cb0ef41Sopenharmony_ci<p>This "Hello world" example is a simple addon, written in C++, that is the 3741cb0ef41Sopenharmony_ciequivalent of the following JavaScript code:</p> 3751cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span>.<span class="hljs-property">hello</span> = <span class="hljs-function">() =></span> <span class="hljs-string">'world'</span>;</code> <button class="copy-button">copy</button></pre> 3761cb0ef41Sopenharmony_ci<p>First, create the file <code>hello.cc</code>:</p> 3771cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-comment">// hello.cc</span> 3781cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node.h></span></span> 3791cb0ef41Sopenharmony_ci 3801cb0ef41Sopenharmony_ci<span class="hljs-keyword">namespace</span> demo { 3811cb0ef41Sopenharmony_ci 3821cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo; 3831cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Isolate; 3841cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Local; 3851cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Object; 3861cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::String; 3871cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Value; 3881cb0ef41Sopenharmony_ci 3891cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Method</span><span class="hljs-params">(<span class="hljs-type">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{ 3901cb0ef41Sopenharmony_ci Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>(); 3911cb0ef41Sopenharmony_ci args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(String::<span class="hljs-built_in">NewFromUtf8</span>( 3921cb0ef41Sopenharmony_ci isolate, <span class="hljs-string">"world"</span>).<span class="hljs-built_in">ToLocalChecked</span>()); 3931cb0ef41Sopenharmony_ci} 3941cb0ef41Sopenharmony_ci 3951cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Initialize</span><span class="hljs-params">(Local<Object> exports)</span> </span>{ 3961cb0ef41Sopenharmony_ci <span class="hljs-built_in">NODE_SET_METHOD</span>(exports, <span class="hljs-string">"hello"</span>, Method); 3971cb0ef41Sopenharmony_ci} 3981cb0ef41Sopenharmony_ci 3991cb0ef41Sopenharmony_ci<span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Initialize) 4001cb0ef41Sopenharmony_ci 4011cb0ef41Sopenharmony_ci} <span class="hljs-comment">// namespace demo</span></code> <button class="copy-button">copy</button></pre> 4021cb0ef41Sopenharmony_ci<p>All Node.js addons must export an initialization function following 4031cb0ef41Sopenharmony_cithe pattern:</p> 4041cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Initialize</span><span class="hljs-params">(Local<Object> exports)</span></span>; 4051cb0ef41Sopenharmony_ci<span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Initialize)</code> <button class="copy-button">copy</button></pre> 4061cb0ef41Sopenharmony_ci<p>There is no semi-colon after <code>NODE_MODULE</code> as it's not a function (see 4071cb0ef41Sopenharmony_ci<code>node.h</code>).</p> 4081cb0ef41Sopenharmony_ci<p>The <code>module_name</code> must match the filename of the final binary (excluding 4091cb0ef41Sopenharmony_cithe <code>.node</code> suffix).</p> 4101cb0ef41Sopenharmony_ci<p>In the <code>hello.cc</code> example, then, the initialization function is <code>Initialize</code> 4111cb0ef41Sopenharmony_ciand the addon module name is <code>addon</code>.</p> 4121cb0ef41Sopenharmony_ci<p>When building addons with <code>node-gyp</code>, using the macro <code>NODE_GYP_MODULE_NAME</code> as 4131cb0ef41Sopenharmony_cithe first parameter of <code>NODE_MODULE()</code> will ensure that the name of the final 4141cb0ef41Sopenharmony_cibinary will be passed to <code>NODE_MODULE()</code>.</p> 4151cb0ef41Sopenharmony_ci<p>Addons defined with <code>NODE_MODULE()</code> can not be loaded in multiple contexts or 4161cb0ef41Sopenharmony_cimultiple threads at the same time.</p> 4171cb0ef41Sopenharmony_ci<h4>Context-aware addons<span><a class="mark" href="#context-aware-addons" id="context-aware-addons">#</a></span><a aria-hidden="true" class="legacy" id="addons_context_aware_addons"></a></h4> 4181cb0ef41Sopenharmony_ci<p>There are environments in which Node.js addons may need to be loaded multiple 4191cb0ef41Sopenharmony_citimes in multiple contexts. For example, the <a href="https://electronjs.org/">Electron</a> runtime runs multiple 4201cb0ef41Sopenharmony_ciinstances of Node.js in a single process. Each instance will have its own 4211cb0ef41Sopenharmony_ci<code>require()</code> cache, and thus each instance will need a native addon to behave 4221cb0ef41Sopenharmony_cicorrectly when loaded via <code>require()</code>. This means that the addon 4231cb0ef41Sopenharmony_cimust support multiple initializations.</p> 4241cb0ef41Sopenharmony_ci<p>A context-aware addon can be constructed by using the macro 4251cb0ef41Sopenharmony_ci<code>NODE_MODULE_INITIALIZER</code>, which expands to the name of a function which Node.js 4261cb0ef41Sopenharmony_ciwill expect to find when it loads an addon. An addon can thus be initialized as 4271cb0ef41Sopenharmony_ciin the following example:</p> 4281cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> v8; 4291cb0ef41Sopenharmony_ci 4301cb0ef41Sopenharmony_ci<span class="hljs-keyword">extern</span> <span class="hljs-string">"C"</span> <span class="hljs-function">NODE_MODULE_EXPORT <span class="hljs-type">void</span> 4311cb0ef41Sopenharmony_ci<span class="hljs-title">NODE_MODULE_INITIALIZER</span><span class="hljs-params">(Local<Object> exports, 4321cb0ef41Sopenharmony_ci Local<Value> <span class="hljs-keyword">module</span>, 4331cb0ef41Sopenharmony_ci Local<Context> context)</span> </span>{ 4341cb0ef41Sopenharmony_ci <span class="hljs-comment">/* Perform addon initialization steps here. */</span> 4351cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 4361cb0ef41Sopenharmony_ci<p>Another option is to use the macro <code>NODE_MODULE_INIT()</code>, which will also 4371cb0ef41Sopenharmony_ciconstruct a context-aware addon. Unlike <code>NODE_MODULE()</code>, which is used to 4381cb0ef41Sopenharmony_ciconstruct an addon around a given addon initializer function, 4391cb0ef41Sopenharmony_ci<code>NODE_MODULE_INIT()</code> serves as the declaration of such an initializer to be 4401cb0ef41Sopenharmony_cifollowed by a function body.</p> 4411cb0ef41Sopenharmony_ci<p>The following three variables may be used inside the function body following an 4421cb0ef41Sopenharmony_ciinvocation of <code>NODE_MODULE_INIT()</code>:</p> 4431cb0ef41Sopenharmony_ci<ul> 4441cb0ef41Sopenharmony_ci<li><code>Local<Object> exports</code>,</li> 4451cb0ef41Sopenharmony_ci<li><code>Local<Value> module</code>, and</li> 4461cb0ef41Sopenharmony_ci<li><code>Local<Context> context</code></li> 4471cb0ef41Sopenharmony_ci</ul> 4481cb0ef41Sopenharmony_ci<p>The choice to build a context-aware addon carries with it the responsibility of 4491cb0ef41Sopenharmony_cicarefully managing global static data. Since the addon may be loaded multiple 4501cb0ef41Sopenharmony_citimes, potentially even from different threads, any global static data stored 4511cb0ef41Sopenharmony_ciin the addon must be properly protected, and must not contain any persistent 4521cb0ef41Sopenharmony_cireferences to JavaScript objects. The reason for this is that JavaScript 4531cb0ef41Sopenharmony_ciobjects are only valid in one context, and will likely cause a crash when 4541cb0ef41Sopenharmony_ciaccessed from the wrong context or from a different thread than the one on which 4551cb0ef41Sopenharmony_cithey were created.</p> 4561cb0ef41Sopenharmony_ci<p>The context-aware addon can be structured to avoid global static data by 4571cb0ef41Sopenharmony_ciperforming the following steps:</p> 4581cb0ef41Sopenharmony_ci<ul> 4591cb0ef41Sopenharmony_ci<li>Define a class which will hold per-addon-instance data and which has a static 4601cb0ef41Sopenharmony_cimember of the form 4611cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-function"><span class="hljs-type">static</span> <span class="hljs-type">void</span> <span class="hljs-title">DeleteInstance</span><span class="hljs-params">(<span class="hljs-type">void</span>* data)</span> </span>{ 4621cb0ef41Sopenharmony_ci <span class="hljs-comment">// Cast `data` to an instance of the class and delete it.</span> 4631cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 4641cb0ef41Sopenharmony_ci</li> 4651cb0ef41Sopenharmony_ci<li>Heap-allocate an instance of this class in the addon initializer. This can be 4661cb0ef41Sopenharmony_ciaccomplished using the <code>new</code> keyword.</li> 4671cb0ef41Sopenharmony_ci<li>Call <code>node::AddEnvironmentCleanupHook()</code>, passing it the above-created 4681cb0ef41Sopenharmony_ciinstance and a pointer to <code>DeleteInstance()</code>. This will ensure the instance is 4691cb0ef41Sopenharmony_cideleted when the environment is torn down.</li> 4701cb0ef41Sopenharmony_ci<li>Store the instance of the class in a <code>v8::External</code>, and</li> 4711cb0ef41Sopenharmony_ci<li>Pass the <code>v8::External</code> to all methods exposed to JavaScript by passing it 4721cb0ef41Sopenharmony_cito <code>v8::FunctionTemplate::New()</code> or <code>v8::Function::New()</code> which creates the 4731cb0ef41Sopenharmony_cinative-backed JavaScript functions. The third parameter of 4741cb0ef41Sopenharmony_ci<code>v8::FunctionTemplate::New()</code> or <code>v8::Function::New()</code> accepts the 4751cb0ef41Sopenharmony_ci<code>v8::External</code> and makes it available in the native callback using the 4761cb0ef41Sopenharmony_ci<code>v8::FunctionCallbackInfo::Data()</code> method.</li> 4771cb0ef41Sopenharmony_ci</ul> 4781cb0ef41Sopenharmony_ci<p>This will ensure that the per-addon-instance data reaches each binding that can 4791cb0ef41Sopenharmony_cibe called from JavaScript. The per-addon-instance data must also be passed into 4801cb0ef41Sopenharmony_ciany asynchronous callbacks the addon may create.</p> 4811cb0ef41Sopenharmony_ci<p>The following example illustrates the implementation of a context-aware addon:</p> 4821cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node.h></span></span> 4831cb0ef41Sopenharmony_ci 4841cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> v8; 4851cb0ef41Sopenharmony_ci 4861cb0ef41Sopenharmony_ci<span class="hljs-keyword">class</span> <span class="hljs-title class_">AddonData</span> { 4871cb0ef41Sopenharmony_ci <span class="hljs-keyword">public</span>: 4881cb0ef41Sopenharmony_ci <span class="hljs-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">AddonData</span><span class="hljs-params">(Isolate* isolate)</span>: 4891cb0ef41Sopenharmony_ci call_count(<span class="hljs-number">0</span>) {</span> 4901cb0ef41Sopenharmony_ci <span class="hljs-comment">// Ensure this per-addon-instance data is deleted at environment cleanup.</span> 4911cb0ef41Sopenharmony_ci node::<span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, DeleteInstance, <span class="hljs-keyword">this</span>); 4921cb0ef41Sopenharmony_ci } 4931cb0ef41Sopenharmony_ci 4941cb0ef41Sopenharmony_ci <span class="hljs-comment">// Per-addon data.</span> 4951cb0ef41Sopenharmony_ci <span class="hljs-type">int</span> call_count; 4961cb0ef41Sopenharmony_ci 4971cb0ef41Sopenharmony_ci <span class="hljs-function"><span class="hljs-type">static</span> <span class="hljs-type">void</span> <span class="hljs-title">DeleteInstance</span><span class="hljs-params">(<span class="hljs-type">void</span>* data)</span> </span>{ 4981cb0ef41Sopenharmony_ci <span class="hljs-keyword">delete</span> <span class="hljs-built_in">static_cast</span><AddonData*>(data); 4991cb0ef41Sopenharmony_ci } 5001cb0ef41Sopenharmony_ci}; 5011cb0ef41Sopenharmony_ci 5021cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">static</span> <span class="hljs-type">void</span> <span class="hljs-title">Method</span><span class="hljs-params">(<span class="hljs-type">const</span> v8::FunctionCallbackInfo<v8::Value>& info)</span> </span>{ 5031cb0ef41Sopenharmony_ci <span class="hljs-comment">// Retrieve the per-addon-instance data.</span> 5041cb0ef41Sopenharmony_ci AddonData* data = 5051cb0ef41Sopenharmony_ci <span class="hljs-built_in">reinterpret_cast</span><AddonData*>(info.<span class="hljs-built_in">Data</span>().<span class="hljs-built_in">As</span><External>()-><span class="hljs-built_in">Value</span>()); 5061cb0ef41Sopenharmony_ci data->call_count++; 5071cb0ef41Sopenharmony_ci info.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>((<span class="hljs-type">double</span>)data->call_count); 5081cb0ef41Sopenharmony_ci} 5091cb0ef41Sopenharmony_ci 5101cb0ef41Sopenharmony_ci<span class="hljs-comment">// Initialize this addon to be context-aware.</span> 5111cb0ef41Sopenharmony_ci<span class="hljs-built_in">NODE_MODULE_INIT</span>(<span class="hljs-comment">/* exports, module, context */</span>) { 5121cb0ef41Sopenharmony_ci Isolate* isolate = context-><span class="hljs-built_in">GetIsolate</span>(); 5131cb0ef41Sopenharmony_ci 5141cb0ef41Sopenharmony_ci <span class="hljs-comment">// Create a new instance of `AddonData` for this instance of the addon and</span> 5151cb0ef41Sopenharmony_ci <span class="hljs-comment">// tie its life cycle to that of the Node.js environment.</span> 5161cb0ef41Sopenharmony_ci AddonData* data = <span class="hljs-keyword">new</span> <span class="hljs-built_in">AddonData</span>(isolate); 5171cb0ef41Sopenharmony_ci 5181cb0ef41Sopenharmony_ci <span class="hljs-comment">// Wrap the data in a `v8::External` so we can pass it to the method we</span> 5191cb0ef41Sopenharmony_ci <span class="hljs-comment">// expose.</span> 5201cb0ef41Sopenharmony_ci Local<External> external = External::<span class="hljs-built_in">New</span>(isolate, data); 5211cb0ef41Sopenharmony_ci 5221cb0ef41Sopenharmony_ci <span class="hljs-comment">// Expose the method `Method` to JavaScript, and make sure it receives the</span> 5231cb0ef41Sopenharmony_ci <span class="hljs-comment">// per-addon-instance data we created above by passing `external` as the</span> 5241cb0ef41Sopenharmony_ci <span class="hljs-comment">// third parameter to the `FunctionTemplate` constructor.</span> 5251cb0ef41Sopenharmony_ci exports-><span class="hljs-built_in">Set</span>(context, 5261cb0ef41Sopenharmony_ci String::<span class="hljs-built_in">NewFromUtf8</span>(isolate, <span class="hljs-string">"method"</span>).<span class="hljs-built_in">ToLocalChecked</span>(), 5271cb0ef41Sopenharmony_ci FunctionTemplate::<span class="hljs-built_in">New</span>(isolate, Method, external) 5281cb0ef41Sopenharmony_ci -><span class="hljs-built_in">GetFunction</span>(context).<span class="hljs-built_in">ToLocalChecked</span>()).<span class="hljs-built_in">FromJust</span>(); 5291cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 5301cb0ef41Sopenharmony_ci<h5>Worker support<span><a class="mark" href="#worker-support" id="worker-support">#</a></span><a aria-hidden="true" class="legacy" id="addons_worker_support"></a></h5> 5311cb0ef41Sopenharmony_ci<div class="api_metadata"> 5321cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 5331cb0ef41Sopenharmony_ci<table> 5341cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 5351cb0ef41Sopenharmony_ci<tr><td>v14.8.0, v12.19.0</td> 5361cb0ef41Sopenharmony_ci<td><p>Cleanup hooks may now be asynchronous.</p></td></tr> 5371cb0ef41Sopenharmony_ci</tbody></table> 5381cb0ef41Sopenharmony_ci</details> 5391cb0ef41Sopenharmony_ci</div> 5401cb0ef41Sopenharmony_ci<p>In order to be loaded from multiple Node.js environments, 5411cb0ef41Sopenharmony_cisuch as a main thread and a Worker thread, an add-on needs to either:</p> 5421cb0ef41Sopenharmony_ci<ul> 5431cb0ef41Sopenharmony_ci<li>Be an Node-API addon, or</li> 5441cb0ef41Sopenharmony_ci<li>Be declared as context-aware using <code>NODE_MODULE_INIT()</code> as described above</li> 5451cb0ef41Sopenharmony_ci</ul> 5461cb0ef41Sopenharmony_ci<p>In order to support <a href="worker_threads.html#class-worker"><code>Worker</code></a> threads, addons need to clean up any resources 5471cb0ef41Sopenharmony_cithey may have allocated when such a thread exists. This can be achieved through 5481cb0ef41Sopenharmony_cithe usage of the <code>AddEnvironmentCleanupHook()</code> function:</p> 5491cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">AddEnvironmentCleanupHook</span><span class="hljs-params">(v8::Isolate* isolate, 5501cb0ef41Sopenharmony_ci <span class="hljs-type">void</span> (*fun)(<span class="hljs-type">void</span>* arg), 5511cb0ef41Sopenharmony_ci <span class="hljs-type">void</span>* arg)</span></span>;</code> <button class="copy-button">copy</button></pre> 5521cb0ef41Sopenharmony_ci<p>This function adds a hook that will run before a given Node.js instance shuts 5531cb0ef41Sopenharmony_cidown. If necessary, such hooks can be removed before they are run using 5541cb0ef41Sopenharmony_ci<code>RemoveEnvironmentCleanupHook()</code>, which has the same signature. Callbacks are 5551cb0ef41Sopenharmony_cirun in last-in first-out order.</p> 5561cb0ef41Sopenharmony_ci<p>If necessary, there is an additional pair of <code>AddEnvironmentCleanupHook()</code> 5571cb0ef41Sopenharmony_ciand <code>RemoveEnvironmentCleanupHook()</code> overloads, where the cleanup hook takes a 5581cb0ef41Sopenharmony_cicallback function. This can be used for shutting down asynchronous resources, 5591cb0ef41Sopenharmony_cisuch as any libuv handles registered by the addon.</p> 5601cb0ef41Sopenharmony_ci<p>The following <code>addon.cc</code> uses <code>AddEnvironmentCleanupHook</code>:</p> 5611cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span> 5621cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node.h></span></span> 5631cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><assert.h></span></span> 5641cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><stdlib.h></span></span> 5651cb0ef41Sopenharmony_ci 5661cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> node::AddEnvironmentCleanupHook; 5671cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::HandleScope; 5681cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Isolate; 5691cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Local; 5701cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Object; 5711cb0ef41Sopenharmony_ci 5721cb0ef41Sopenharmony_ci<span class="hljs-comment">// Note: In a real-world application, do not rely on static/global data.</span> 5731cb0ef41Sopenharmony_ci<span class="hljs-type">static</span> <span class="hljs-type">char</span> cookie[] = <span class="hljs-string">"yum yum"</span>; 5741cb0ef41Sopenharmony_ci<span class="hljs-type">static</span> <span class="hljs-type">int</span> cleanup_cb1_called = <span class="hljs-number">0</span>; 5751cb0ef41Sopenharmony_ci<span class="hljs-type">static</span> <span class="hljs-type">int</span> cleanup_cb2_called = <span class="hljs-number">0</span>; 5761cb0ef41Sopenharmony_ci 5771cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">static</span> <span class="hljs-type">void</span> <span class="hljs-title">cleanup_cb1</span><span class="hljs-params">(<span class="hljs-type">void</span>* arg)</span> </span>{ 5781cb0ef41Sopenharmony_ci Isolate* isolate = <span class="hljs-built_in">static_cast</span><Isolate*>(arg); 5791cb0ef41Sopenharmony_ci <span class="hljs-function">HandleScope <span class="hljs-title">scope</span><span class="hljs-params">(isolate)</span></span>; 5801cb0ef41Sopenharmony_ci Local<Object> obj = Object::<span class="hljs-built_in">New</span>(isolate); 5811cb0ef41Sopenharmony_ci <span class="hljs-built_in">assert</span>(!obj.<span class="hljs-built_in">IsEmpty</span>()); <span class="hljs-comment">// assert VM is still alive</span> 5821cb0ef41Sopenharmony_ci <span class="hljs-built_in">assert</span>(obj-><span class="hljs-built_in">IsObject</span>()); 5831cb0ef41Sopenharmony_ci cleanup_cb1_called++; 5841cb0ef41Sopenharmony_ci} 5851cb0ef41Sopenharmony_ci 5861cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">static</span> <span class="hljs-type">void</span> <span class="hljs-title">cleanup_cb2</span><span class="hljs-params">(<span class="hljs-type">void</span>* arg)</span> </span>{ 5871cb0ef41Sopenharmony_ci <span class="hljs-built_in">assert</span>(arg == <span class="hljs-built_in">static_cast</span><<span class="hljs-type">void</span>*>(cookie)); 5881cb0ef41Sopenharmony_ci cleanup_cb2_called++; 5891cb0ef41Sopenharmony_ci} 5901cb0ef41Sopenharmony_ci 5911cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">static</span> <span class="hljs-type">void</span> <span class="hljs-title">sanity_check</span><span class="hljs-params">(<span class="hljs-type">void</span>*)</span> </span>{ 5921cb0ef41Sopenharmony_ci <span class="hljs-built_in">assert</span>(cleanup_cb1_called == <span class="hljs-number">1</span>); 5931cb0ef41Sopenharmony_ci <span class="hljs-built_in">assert</span>(cleanup_cb2_called == <span class="hljs-number">1</span>); 5941cb0ef41Sopenharmony_ci} 5951cb0ef41Sopenharmony_ci 5961cb0ef41Sopenharmony_ci<span class="hljs-comment">// Initialize this addon to be context-aware.</span> 5971cb0ef41Sopenharmony_ci<span class="hljs-built_in">NODE_MODULE_INIT</span>(<span class="hljs-comment">/* exports, module, context */</span>) { 5981cb0ef41Sopenharmony_ci Isolate* isolate = context-><span class="hljs-built_in">GetIsolate</span>(); 5991cb0ef41Sopenharmony_ci 6001cb0ef41Sopenharmony_ci <span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, sanity_check, <span class="hljs-literal">nullptr</span>); 6011cb0ef41Sopenharmony_ci <span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, cleanup_cb2, cookie); 6021cb0ef41Sopenharmony_ci <span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, cleanup_cb1, isolate); 6031cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 6041cb0ef41Sopenharmony_ci<p>Test in JavaScript by running:</p> 6051cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// test.js</span> 6061cb0ef41Sopenharmony_ci<span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>);</code> <button class="copy-button">copy</button></pre> 6071cb0ef41Sopenharmony_ci<h4>Building<span><a class="mark" href="#building" id="building">#</a></span><a aria-hidden="true" class="legacy" id="addons_building"></a></h4> 6081cb0ef41Sopenharmony_ci<p>Once the source code has been written, it must be compiled into the binary 6091cb0ef41Sopenharmony_ci<code>addon.node</code> file. To do so, create a file called <code>binding.gyp</code> in the 6101cb0ef41Sopenharmony_citop-level of the project describing the build configuration of the module 6111cb0ef41Sopenharmony_ciusing a JSON-like format. This file is used by <a href="https://github.com/nodejs/node-gyp">node-gyp</a>, a tool written 6121cb0ef41Sopenharmony_cispecifically to compile Node.js addons.</p> 6131cb0ef41Sopenharmony_ci<pre><code class="language-json"><span class="hljs-punctuation">{</span> 6141cb0ef41Sopenharmony_ci <span class="hljs-attr">"targets"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> 6151cb0ef41Sopenharmony_ci <span class="hljs-punctuation">{</span> 6161cb0ef41Sopenharmony_ci <span class="hljs-attr">"target_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"addon"</span><span class="hljs-punctuation">,</span> 6171cb0ef41Sopenharmony_ci <span class="hljs-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> <span class="hljs-string">"hello.cc"</span> <span class="hljs-punctuation">]</span> 6181cb0ef41Sopenharmony_ci <span class="hljs-punctuation">}</span> 6191cb0ef41Sopenharmony_ci <span class="hljs-punctuation">]</span> 6201cb0ef41Sopenharmony_ci<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre> 6211cb0ef41Sopenharmony_ci<p>A version of the <code>node-gyp</code> utility is bundled and distributed with 6221cb0ef41Sopenharmony_ciNode.js as part of <code>npm</code>. This version is not made directly available for 6231cb0ef41Sopenharmony_cidevelopers to use and is intended only to support the ability to use the 6241cb0ef41Sopenharmony_ci<code>npm install</code> command to compile and install addons. Developers who wish to 6251cb0ef41Sopenharmony_ciuse <code>node-gyp</code> directly can install it using the command 6261cb0ef41Sopenharmony_ci<code>npm install -g node-gyp</code>. See the <code>node-gyp</code> <a href="https://github.com/nodejs/node-gyp#installation">installation instructions</a> for 6271cb0ef41Sopenharmony_cimore information, including platform-specific requirements.</p> 6281cb0ef41Sopenharmony_ci<p>Once the <code>binding.gyp</code> file has been created, use <code>node-gyp configure</code> to 6291cb0ef41Sopenharmony_cigenerate the appropriate project build files for the current platform. This 6301cb0ef41Sopenharmony_ciwill generate either a <code>Makefile</code> (on Unix platforms) or a <code>vcxproj</code> file 6311cb0ef41Sopenharmony_ci(on Windows) in the <code>build/</code> directory.</p> 6321cb0ef41Sopenharmony_ci<p>Next, invoke the <code>node-gyp build</code> command to generate the compiled <code>addon.node</code> 6331cb0ef41Sopenharmony_cifile. This will be put into the <code>build/Release/</code> directory.</p> 6341cb0ef41Sopenharmony_ci<p>When using <code>npm install</code> to install a Node.js addon, npm uses its own bundled 6351cb0ef41Sopenharmony_civersion of <code>node-gyp</code> to perform this same set of actions, generating a 6361cb0ef41Sopenharmony_cicompiled version of the addon for the user's platform on demand.</p> 6371cb0ef41Sopenharmony_ci<p>Once built, the binary addon can be used from within Node.js by pointing 6381cb0ef41Sopenharmony_ci<a href="modules.html#requireid"><code>require()</code></a> to the built <code>addon.node</code> module:</p> 6391cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// hello.js</span> 6401cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>); 6411cb0ef41Sopenharmony_ci 6421cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(addon.<span class="hljs-title function_">hello</span>()); 6431cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 'world'</span></code> <button class="copy-button">copy</button></pre> 6441cb0ef41Sopenharmony_ci<p>Because the exact path to the compiled addon binary can vary depending on how 6451cb0ef41Sopenharmony_ciit is compiled (i.e. sometimes it may be in <code>./build/Debug/</code>), addons can use 6461cb0ef41Sopenharmony_cithe <a href="https://github.com/TooTallNate/node-bindings">bindings</a> package to load the compiled module.</p> 6471cb0ef41Sopenharmony_ci<p>While the <code>bindings</code> package implementation is more sophisticated in how it 6481cb0ef41Sopenharmony_cilocates addon modules, it is essentially using a <code>try…catch</code> pattern similar to:</p> 6491cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">try</span> { 6501cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon.node'</span>); 6511cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) { 6521cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Debug/addon.node'</span>); 6531cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 6541cb0ef41Sopenharmony_ci<h4>Linking to libraries included with Node.js<span><a class="mark" href="#linking-to-libraries-included-with-nodejs" id="linking-to-libraries-included-with-nodejs">#</a></span><a aria-hidden="true" class="legacy" id="addons_linking_to_libraries_included_with_node_js"></a></h4> 6551cb0ef41Sopenharmony_ci<p>Node.js uses statically linked libraries such as V8, libuv, and OpenSSL. All 6561cb0ef41Sopenharmony_ciaddons are required to link to V8 and may link to any of the other dependencies 6571cb0ef41Sopenharmony_cias well. Typically, this is as simple as including the appropriate 6581cb0ef41Sopenharmony_ci<code>#include <...></code> statements (e.g. <code>#include <v8.h></code>) and <code>node-gyp</code> will locate 6591cb0ef41Sopenharmony_cithe appropriate headers automatically. However, there are a few caveats to be 6601cb0ef41Sopenharmony_ciaware of:</p> 6611cb0ef41Sopenharmony_ci<ul> 6621cb0ef41Sopenharmony_ci<li> 6631cb0ef41Sopenharmony_ci<p>When <code>node-gyp</code> runs, it will detect the specific release version of Node.js 6641cb0ef41Sopenharmony_ciand download either the full source tarball or just the headers. If the full 6651cb0ef41Sopenharmony_cisource is downloaded, addons will have complete access to the full set of 6661cb0ef41Sopenharmony_ciNode.js dependencies. However, if only the Node.js headers are downloaded, 6671cb0ef41Sopenharmony_cithen only the symbols exported by Node.js will be available.</p> 6681cb0ef41Sopenharmony_ci</li> 6691cb0ef41Sopenharmony_ci<li> 6701cb0ef41Sopenharmony_ci<p><code>node-gyp</code> can be run using the <code>--nodedir</code> flag pointing at a local Node.js 6711cb0ef41Sopenharmony_cisource image. Using this option, the addon will have access to the full set of 6721cb0ef41Sopenharmony_cidependencies.</p> 6731cb0ef41Sopenharmony_ci</li> 6741cb0ef41Sopenharmony_ci</ul> 6751cb0ef41Sopenharmony_ci<h4>Loading addons using <code>require()</code><span><a class="mark" href="#loading-addons-using-require" id="loading-addons-using-require">#</a></span><a aria-hidden="true" class="legacy" id="addons_loading_addons_using_require"></a></h4> 6761cb0ef41Sopenharmony_ci<p>The filename extension of the compiled addon binary is <code>.node</code> (as opposed 6771cb0ef41Sopenharmony_cito <code>.dll</code> or <code>.so</code>). The <a href="modules.html#requireid"><code>require()</code></a> function is written to look for 6781cb0ef41Sopenharmony_cifiles with the <code>.node</code> file extension and initialize those as dynamically-linked 6791cb0ef41Sopenharmony_cilibraries.</p> 6801cb0ef41Sopenharmony_ci<p>When calling <a href="modules.html#requireid"><code>require()</code></a>, the <code>.node</code> extension can usually be 6811cb0ef41Sopenharmony_ciomitted and Node.js will still find and initialize the addon. One caveat, 6821cb0ef41Sopenharmony_cihowever, is that Node.js will first attempt to locate and load modules or 6831cb0ef41Sopenharmony_ciJavaScript files that happen to share the same base name. For instance, if 6841cb0ef41Sopenharmony_cithere is a file <code>addon.js</code> in the same directory as the binary <code>addon.node</code>, 6851cb0ef41Sopenharmony_cithen <a href="modules.html#requireid"><code>require('addon')</code></a> will give precedence to the <code>addon.js</code> file 6861cb0ef41Sopenharmony_ciand load it instead.</p> 6871cb0ef41Sopenharmony_ci</section><section><h3>Native abstractions for Node.js<span><a class="mark" href="#native-abstractions-for-nodejs" id="native-abstractions-for-nodejs">#</a></span><a aria-hidden="true" class="legacy" id="addons_native_abstractions_for_node_js"></a></h3> 6881cb0ef41Sopenharmony_ci<p>Each of the examples illustrated in this document directly use the 6891cb0ef41Sopenharmony_ciNode.js and V8 APIs for implementing addons. The V8 API can, and has, changed 6901cb0ef41Sopenharmony_cidramatically from one V8 release to the next (and one major Node.js release to 6911cb0ef41Sopenharmony_cithe next). With each change, addons may need to be updated and recompiled in 6921cb0ef41Sopenharmony_ciorder to continue functioning. The Node.js release schedule is designed to 6931cb0ef41Sopenharmony_ciminimize the frequency and impact of such changes but there is little that 6941cb0ef41Sopenharmony_ciNode.js can do to ensure stability of the V8 APIs.</p> 6951cb0ef41Sopenharmony_ci<p>The <a href="https://github.com/nodejs/nan">Native Abstractions for Node.js</a> (or <code>nan</code>) provide a set of tools that 6961cb0ef41Sopenharmony_ciaddon developers are recommended to use to keep compatibility between past and 6971cb0ef41Sopenharmony_cifuture releases of V8 and Node.js. See the <code>nan</code> <a href="https://github.com/nodejs/nan/tree/HEAD/examples/">examples</a> for an 6981cb0ef41Sopenharmony_ciillustration of how it can be used.</p> 6991cb0ef41Sopenharmony_ci</section><section><h3>Node-API<span><a class="mark" href="#node-api" id="node-api">#</a></span><a aria-hidden="true" class="legacy" id="addons_node_api"></a></h3> 7001cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 7011cb0ef41Sopenharmony_ci<p>Node-API is an API for building native addons. It is independent from 7021cb0ef41Sopenharmony_cithe underlying JavaScript runtime (e.g. V8) and is maintained as part of 7031cb0ef41Sopenharmony_ciNode.js itself. This API will be Application Binary Interface (ABI) stable 7041cb0ef41Sopenharmony_ciacross versions of Node.js. It is intended to insulate addons from 7051cb0ef41Sopenharmony_cichanges in the underlying JavaScript engine and allow modules 7061cb0ef41Sopenharmony_cicompiled for one version to run on later versions of Node.js without 7071cb0ef41Sopenharmony_cirecompilation. Addons are built/packaged with the same approach/tools 7081cb0ef41Sopenharmony_cioutlined in this document (node-gyp, etc.). The only difference is the 7091cb0ef41Sopenharmony_ciset of APIs that are used by the native code. Instead of using the V8 7101cb0ef41Sopenharmony_cior <a href="https://github.com/nodejs/nan">Native Abstractions for Node.js</a> APIs, the functions available 7111cb0ef41Sopenharmony_ciin the Node-API are used.</p> 7121cb0ef41Sopenharmony_ci<p>Creating and maintaining an addon that benefits from the ABI stability 7131cb0ef41Sopenharmony_ciprovided by Node-API carries with it certain 7141cb0ef41Sopenharmony_ci<a href="n-api.html#implications-of-abi-stability">implementation considerations</a>.</p> 7151cb0ef41Sopenharmony_ci<p>To use Node-API in the above "Hello world" example, replace the content of 7161cb0ef41Sopenharmony_ci<code>hello.cc</code> with the following. All other instructions remain the same.</p> 7171cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-comment">// hello.cc using Node-API</span> 7181cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_api.h></span></span> 7191cb0ef41Sopenharmony_ci 7201cb0ef41Sopenharmony_ci<span class="hljs-keyword">namespace</span> demo { 7211cb0ef41Sopenharmony_ci 7221cb0ef41Sopenharmony_ci<span class="hljs-function">napi_value <span class="hljs-title">Method</span><span class="hljs-params">(napi_env env, napi_callback_info args)</span> </span>{ 7231cb0ef41Sopenharmony_ci napi_value greeting; 7241cb0ef41Sopenharmony_ci napi_status status; 7251cb0ef41Sopenharmony_ci 7261cb0ef41Sopenharmony_ci status = <span class="hljs-built_in">napi_create_string_utf8</span>(env, <span class="hljs-string">"world"</span>, NAPI_AUTO_LENGTH, &greeting); 7271cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">nullptr</span>; 7281cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> greeting; 7291cb0ef41Sopenharmony_ci} 7301cb0ef41Sopenharmony_ci 7311cb0ef41Sopenharmony_ci<span class="hljs-function">napi_value <span class="hljs-title">init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> </span>{ 7321cb0ef41Sopenharmony_ci napi_status status; 7331cb0ef41Sopenharmony_ci napi_value fn; 7341cb0ef41Sopenharmony_ci 7351cb0ef41Sopenharmony_ci status = <span class="hljs-built_in">napi_create_function</span>(env, <span class="hljs-literal">nullptr</span>, <span class="hljs-number">0</span>, Method, <span class="hljs-literal">nullptr</span>, &fn); 7361cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">nullptr</span>; 7371cb0ef41Sopenharmony_ci 7381cb0ef41Sopenharmony_ci status = <span class="hljs-built_in">napi_set_named_property</span>(env, exports, <span class="hljs-string">"hello"</span>, fn); 7391cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">nullptr</span>; 7401cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> exports; 7411cb0ef41Sopenharmony_ci} 7421cb0ef41Sopenharmony_ci 7431cb0ef41Sopenharmony_ci<span class="hljs-built_in">NAPI_MODULE</span>(NODE_GYP_MODULE_NAME, init) 7441cb0ef41Sopenharmony_ci 7451cb0ef41Sopenharmony_ci} <span class="hljs-comment">// namespace demo</span></code> <button class="copy-button">copy</button></pre> 7461cb0ef41Sopenharmony_ci<p>The functions available and how to use them are documented in 7471cb0ef41Sopenharmony_ci<a href="n-api.html">C/C++ addons with Node-API</a>.</p> 7481cb0ef41Sopenharmony_ci</section><section><h3>Addon examples<span><a class="mark" href="#addon-examples" id="addon-examples">#</a></span><a aria-hidden="true" class="legacy" id="addons_addon_examples"></a></h3> 7491cb0ef41Sopenharmony_ci<p>Following are some example addons intended to help developers get started. The 7501cb0ef41Sopenharmony_ciexamples use the V8 APIs. Refer to the online <a href="https://v8docs.nodesource.com/">V8 reference</a> 7511cb0ef41Sopenharmony_cifor help with the various V8 calls, and V8's <a href="https://v8.dev/docs/embed">Embedder's Guide</a> for an 7521cb0ef41Sopenharmony_ciexplanation of several concepts used such as handles, scopes, function 7531cb0ef41Sopenharmony_citemplates, etc.</p> 7541cb0ef41Sopenharmony_ci<p>Each of these examples using the following <code>binding.gyp</code> file:</p> 7551cb0ef41Sopenharmony_ci<pre><code class="language-json"><span class="hljs-punctuation">{</span> 7561cb0ef41Sopenharmony_ci <span class="hljs-attr">"targets"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> 7571cb0ef41Sopenharmony_ci <span class="hljs-punctuation">{</span> 7581cb0ef41Sopenharmony_ci <span class="hljs-attr">"target_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"addon"</span><span class="hljs-punctuation">,</span> 7591cb0ef41Sopenharmony_ci <span class="hljs-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> <span class="hljs-string">"addon.cc"</span> <span class="hljs-punctuation">]</span> 7601cb0ef41Sopenharmony_ci <span class="hljs-punctuation">}</span> 7611cb0ef41Sopenharmony_ci <span class="hljs-punctuation">]</span> 7621cb0ef41Sopenharmony_ci<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre> 7631cb0ef41Sopenharmony_ci<p>In cases where there is more than one <code>.cc</code> file, simply add the additional 7641cb0ef41Sopenharmony_cifilename to the <code>sources</code> array:</p> 7651cb0ef41Sopenharmony_ci<pre><code class="language-json"><span class="hljs-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-string">"addon.cc"</span><span class="hljs-punctuation">,</span> <span class="hljs-string">"myexample.cc"</span><span class="hljs-punctuation">]</span></code> <button class="copy-button">copy</button></pre> 7661cb0ef41Sopenharmony_ci<p>Once the <code>binding.gyp</code> file is ready, the example addons can be configured and 7671cb0ef41Sopenharmony_cibuilt using <code>node-gyp</code>:</p> 7681cb0ef41Sopenharmony_ci<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node-gyp configure build</span></code> <button class="copy-button">copy</button></pre> 7691cb0ef41Sopenharmony_ci<h4>Function arguments<span><a class="mark" href="#function-arguments" id="function-arguments">#</a></span><a aria-hidden="true" class="legacy" id="addons_function_arguments"></a></h4> 7701cb0ef41Sopenharmony_ci<p>Addons will typically expose objects and functions that can be accessed from 7711cb0ef41Sopenharmony_ciJavaScript running within Node.js. When functions are invoked from JavaScript, 7721cb0ef41Sopenharmony_cithe input arguments and return value must be mapped to and from the C/C++ 7731cb0ef41Sopenharmony_cicode.</p> 7741cb0ef41Sopenharmony_ci<p>The following example illustrates how to read function arguments passed from 7751cb0ef41Sopenharmony_ciJavaScript and how to return a result:</p> 7761cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span> 7771cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node.h></span></span> 7781cb0ef41Sopenharmony_ci 7791cb0ef41Sopenharmony_ci<span class="hljs-keyword">namespace</span> demo { 7801cb0ef41Sopenharmony_ci 7811cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Exception; 7821cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo; 7831cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Isolate; 7841cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Local; 7851cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Number; 7861cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Object; 7871cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::String; 7881cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Value; 7891cb0ef41Sopenharmony_ci 7901cb0ef41Sopenharmony_ci<span class="hljs-comment">// This is the implementation of the "add" method</span> 7911cb0ef41Sopenharmony_ci<span class="hljs-comment">// Input arguments are passed using the</span> 7921cb0ef41Sopenharmony_ci<span class="hljs-comment">// const FunctionCallbackInfo<Value>& args struct</span> 7931cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Add</span><span class="hljs-params">(<span class="hljs-type">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{ 7941cb0ef41Sopenharmony_ci Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>(); 7951cb0ef41Sopenharmony_ci 7961cb0ef41Sopenharmony_ci <span class="hljs-comment">// Check the number of arguments passed.</span> 7971cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (args.<span class="hljs-built_in">Length</span>() < <span class="hljs-number">2</span>) { 7981cb0ef41Sopenharmony_ci <span class="hljs-comment">// Throw an Error that is passed back to JavaScript</span> 7991cb0ef41Sopenharmony_ci isolate-><span class="hljs-built_in">ThrowException</span>(Exception::<span class="hljs-built_in">TypeError</span>( 8001cb0ef41Sopenharmony_ci String::<span class="hljs-built_in">NewFromUtf8</span>(isolate, 8011cb0ef41Sopenharmony_ci <span class="hljs-string">"Wrong number of arguments"</span>).<span class="hljs-built_in">ToLocalChecked</span>())); 8021cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span>; 8031cb0ef41Sopenharmony_ci } 8041cb0ef41Sopenharmony_ci 8051cb0ef41Sopenharmony_ci <span class="hljs-comment">// Check the argument types</span> 8061cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (!args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">IsNumber</span>() || !args[<span class="hljs-number">1</span>]-><span class="hljs-built_in">IsNumber</span>()) { 8071cb0ef41Sopenharmony_ci isolate-><span class="hljs-built_in">ThrowException</span>(Exception::<span class="hljs-built_in">TypeError</span>( 8081cb0ef41Sopenharmony_ci String::<span class="hljs-built_in">NewFromUtf8</span>(isolate, 8091cb0ef41Sopenharmony_ci <span class="hljs-string">"Wrong arguments"</span>).<span class="hljs-built_in">ToLocalChecked</span>())); 8101cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span>; 8111cb0ef41Sopenharmony_ci } 8121cb0ef41Sopenharmony_ci 8131cb0ef41Sopenharmony_ci <span class="hljs-comment">// Perform the operation</span> 8141cb0ef41Sopenharmony_ci <span class="hljs-type">double</span> value = 8151cb0ef41Sopenharmony_ci args[<span class="hljs-number">0</span>].<span class="hljs-built_in">As</span><Number>()-><span class="hljs-built_in">Value</span>() + args[<span class="hljs-number">1</span>].<span class="hljs-built_in">As</span><Number>()-><span class="hljs-built_in">Value</span>(); 8161cb0ef41Sopenharmony_ci Local<Number> num = Number::<span class="hljs-built_in">New</span>(isolate, value); 8171cb0ef41Sopenharmony_ci 8181cb0ef41Sopenharmony_ci <span class="hljs-comment">// Set the return value (using the passed in</span> 8191cb0ef41Sopenharmony_ci <span class="hljs-comment">// FunctionCallbackInfo<Value>&)</span> 8201cb0ef41Sopenharmony_ci args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(num); 8211cb0ef41Sopenharmony_ci} 8221cb0ef41Sopenharmony_ci 8231cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Init</span><span class="hljs-params">(Local<Object> exports)</span> </span>{ 8241cb0ef41Sopenharmony_ci <span class="hljs-built_in">NODE_SET_METHOD</span>(exports, <span class="hljs-string">"add"</span>, Add); 8251cb0ef41Sopenharmony_ci} 8261cb0ef41Sopenharmony_ci 8271cb0ef41Sopenharmony_ci<span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Init) 8281cb0ef41Sopenharmony_ci 8291cb0ef41Sopenharmony_ci} <span class="hljs-comment">// namespace demo</span></code> <button class="copy-button">copy</button></pre> 8301cb0ef41Sopenharmony_ci<p>Once compiled, the example addon can be required and used from within Node.js:</p> 8311cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// test.js</span> 8321cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>); 8331cb0ef41Sopenharmony_ci 8341cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'This should be eight:'</span>, addon.<span class="hljs-title function_">add</span>(<span class="hljs-number">3</span>, <span class="hljs-number">5</span>));</code> <button class="copy-button">copy</button></pre> 8351cb0ef41Sopenharmony_ci<h4>Callbacks<span><a class="mark" href="#callbacks" id="callbacks">#</a></span><a aria-hidden="true" class="legacy" id="addons_callbacks"></a></h4> 8361cb0ef41Sopenharmony_ci<p>It is common practice within addons to pass JavaScript functions to a C++ 8371cb0ef41Sopenharmony_cifunction and execute them from there. The following example illustrates how 8381cb0ef41Sopenharmony_cito invoke such callbacks:</p> 8391cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span> 8401cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node.h></span></span> 8411cb0ef41Sopenharmony_ci 8421cb0ef41Sopenharmony_ci<span class="hljs-keyword">namespace</span> demo { 8431cb0ef41Sopenharmony_ci 8441cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Context; 8451cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Function; 8461cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo; 8471cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Isolate; 8481cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Local; 8491cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Null; 8501cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Object; 8511cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::String; 8521cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Value; 8531cb0ef41Sopenharmony_ci 8541cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">RunCallback</span><span class="hljs-params">(<span class="hljs-type">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{ 8551cb0ef41Sopenharmony_ci Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>(); 8561cb0ef41Sopenharmony_ci Local<Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>(); 8571cb0ef41Sopenharmony_ci Local<Function> cb = Local<Function>::<span class="hljs-built_in">Cast</span>(args[<span class="hljs-number">0</span>]); 8581cb0ef41Sopenharmony_ci <span class="hljs-type">const</span> <span class="hljs-type">unsigned</span> argc = <span class="hljs-number">1</span>; 8591cb0ef41Sopenharmony_ci Local<Value> argv[argc] = { 8601cb0ef41Sopenharmony_ci String::<span class="hljs-built_in">NewFromUtf8</span>(isolate, 8611cb0ef41Sopenharmony_ci <span class="hljs-string">"hello world"</span>).<span class="hljs-built_in">ToLocalChecked</span>() }; 8621cb0ef41Sopenharmony_ci cb-><span class="hljs-built_in">Call</span>(context, <span class="hljs-built_in">Null</span>(isolate), argc, argv).<span class="hljs-built_in">ToLocalChecked</span>(); 8631cb0ef41Sopenharmony_ci} 8641cb0ef41Sopenharmony_ci 8651cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Init</span><span class="hljs-params">(Local<Object> exports, Local<Object> <span class="hljs-keyword">module</span>)</span> </span>{ 8661cb0ef41Sopenharmony_ci <span class="hljs-built_in">NODE_SET_METHOD</span>(<span class="hljs-keyword">module</span>, <span class="hljs-string">"exports"</span>, RunCallback); 8671cb0ef41Sopenharmony_ci} 8681cb0ef41Sopenharmony_ci 8691cb0ef41Sopenharmony_ci<span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Init) 8701cb0ef41Sopenharmony_ci 8711cb0ef41Sopenharmony_ci} <span class="hljs-comment">// namespace demo</span></code> <button class="copy-button">copy</button></pre> 8721cb0ef41Sopenharmony_ci<p>This example uses a two-argument form of <code>Init()</code> that receives the full 8731cb0ef41Sopenharmony_ci<code>module</code> object as the second argument. This allows the addon to completely 8741cb0ef41Sopenharmony_cioverwrite <code>exports</code> with a single function instead of adding the function as a 8751cb0ef41Sopenharmony_ciproperty of <code>exports</code>.</p> 8761cb0ef41Sopenharmony_ci<p>To test it, run the following JavaScript:</p> 8771cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// test.js</span> 8781cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>); 8791cb0ef41Sopenharmony_ci 8801cb0ef41Sopenharmony_ci<span class="hljs-title function_">addon</span>(<span class="hljs-function">(<span class="hljs-params">msg</span>) =></span> { 8811cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(msg); 8821cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 'hello world'</span> 8831cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 8841cb0ef41Sopenharmony_ci<p>In this example, the callback function is invoked synchronously.</p> 8851cb0ef41Sopenharmony_ci<h4>Object factory<span><a class="mark" href="#object-factory" id="object-factory">#</a></span><a aria-hidden="true" class="legacy" id="addons_object_factory"></a></h4> 8861cb0ef41Sopenharmony_ci<p>Addons can create and return new objects from within a C++ function as 8871cb0ef41Sopenharmony_ciillustrated in the following example. An object is created and returned with a 8881cb0ef41Sopenharmony_ciproperty <code>msg</code> that echoes the string passed to <code>createObject()</code>:</p> 8891cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span> 8901cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node.h></span></span> 8911cb0ef41Sopenharmony_ci 8921cb0ef41Sopenharmony_ci<span class="hljs-keyword">namespace</span> demo { 8931cb0ef41Sopenharmony_ci 8941cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Context; 8951cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo; 8961cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Isolate; 8971cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Local; 8981cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Object; 8991cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::String; 9001cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Value; 9011cb0ef41Sopenharmony_ci 9021cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">CreateObject</span><span class="hljs-params">(<span class="hljs-type">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{ 9031cb0ef41Sopenharmony_ci Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>(); 9041cb0ef41Sopenharmony_ci Local<Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>(); 9051cb0ef41Sopenharmony_ci 9061cb0ef41Sopenharmony_ci Local<Object> obj = Object::<span class="hljs-built_in">New</span>(isolate); 9071cb0ef41Sopenharmony_ci obj-><span class="hljs-built_in">Set</span>(context, 9081cb0ef41Sopenharmony_ci String::<span class="hljs-built_in">NewFromUtf8</span>(isolate, 9091cb0ef41Sopenharmony_ci <span class="hljs-string">"msg"</span>).<span class="hljs-built_in">ToLocalChecked</span>(), 9101cb0ef41Sopenharmony_ci args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">ToString</span>(context).<span class="hljs-built_in">ToLocalChecked</span>()) 9111cb0ef41Sopenharmony_ci .<span class="hljs-built_in">FromJust</span>(); 9121cb0ef41Sopenharmony_ci 9131cb0ef41Sopenharmony_ci args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(obj); 9141cb0ef41Sopenharmony_ci} 9151cb0ef41Sopenharmony_ci 9161cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Init</span><span class="hljs-params">(Local<Object> exports, Local<Object> <span class="hljs-keyword">module</span>)</span> </span>{ 9171cb0ef41Sopenharmony_ci <span class="hljs-built_in">NODE_SET_METHOD</span>(<span class="hljs-keyword">module</span>, <span class="hljs-string">"exports"</span>, CreateObject); 9181cb0ef41Sopenharmony_ci} 9191cb0ef41Sopenharmony_ci 9201cb0ef41Sopenharmony_ci<span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Init) 9211cb0ef41Sopenharmony_ci 9221cb0ef41Sopenharmony_ci} <span class="hljs-comment">// namespace demo</span></code> <button class="copy-button">copy</button></pre> 9231cb0ef41Sopenharmony_ci<p>To test it in JavaScript:</p> 9241cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// test.js</span> 9251cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>); 9261cb0ef41Sopenharmony_ci 9271cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj1 = <span class="hljs-title function_">addon</span>(<span class="hljs-string">'hello'</span>); 9281cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj2 = <span class="hljs-title function_">addon</span>(<span class="hljs-string">'world'</span>); 9291cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(obj1.<span class="hljs-property">msg</span>, obj2.<span class="hljs-property">msg</span>); 9301cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 'hello world'</span></code> <button class="copy-button">copy</button></pre> 9311cb0ef41Sopenharmony_ci<h4>Function factory<span><a class="mark" href="#function-factory" id="function-factory">#</a></span><a aria-hidden="true" class="legacy" id="addons_function_factory"></a></h4> 9321cb0ef41Sopenharmony_ci<p>Another common scenario is creating JavaScript functions that wrap C++ 9331cb0ef41Sopenharmony_cifunctions and returning those back to JavaScript:</p> 9341cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span> 9351cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node.h></span></span> 9361cb0ef41Sopenharmony_ci 9371cb0ef41Sopenharmony_ci<span class="hljs-keyword">namespace</span> demo { 9381cb0ef41Sopenharmony_ci 9391cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Context; 9401cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Function; 9411cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo; 9421cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::FunctionTemplate; 9431cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Isolate; 9441cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Local; 9451cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Object; 9461cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::String; 9471cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Value; 9481cb0ef41Sopenharmony_ci 9491cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyFunction</span><span class="hljs-params">(<span class="hljs-type">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{ 9501cb0ef41Sopenharmony_ci Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>(); 9511cb0ef41Sopenharmony_ci args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(String::<span class="hljs-built_in">NewFromUtf8</span>( 9521cb0ef41Sopenharmony_ci isolate, <span class="hljs-string">"hello world"</span>).<span class="hljs-built_in">ToLocalChecked</span>()); 9531cb0ef41Sopenharmony_ci} 9541cb0ef41Sopenharmony_ci 9551cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">CreateFunction</span><span class="hljs-params">(<span class="hljs-type">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{ 9561cb0ef41Sopenharmony_ci Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>(); 9571cb0ef41Sopenharmony_ci 9581cb0ef41Sopenharmony_ci Local<Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>(); 9591cb0ef41Sopenharmony_ci Local<FunctionTemplate> tpl = FunctionTemplate::<span class="hljs-built_in">New</span>(isolate, MyFunction); 9601cb0ef41Sopenharmony_ci Local<Function> fn = tpl-><span class="hljs-built_in">GetFunction</span>(context).<span class="hljs-built_in">ToLocalChecked</span>(); 9611cb0ef41Sopenharmony_ci 9621cb0ef41Sopenharmony_ci <span class="hljs-comment">// omit this to make it anonymous</span> 9631cb0ef41Sopenharmony_ci fn-><span class="hljs-built_in">SetName</span>(String::<span class="hljs-built_in">NewFromUtf8</span>( 9641cb0ef41Sopenharmony_ci isolate, <span class="hljs-string">"theFunction"</span>).<span class="hljs-built_in">ToLocalChecked</span>()); 9651cb0ef41Sopenharmony_ci 9661cb0ef41Sopenharmony_ci args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(fn); 9671cb0ef41Sopenharmony_ci} 9681cb0ef41Sopenharmony_ci 9691cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Init</span><span class="hljs-params">(Local<Object> exports, Local<Object> <span class="hljs-keyword">module</span>)</span> </span>{ 9701cb0ef41Sopenharmony_ci <span class="hljs-built_in">NODE_SET_METHOD</span>(<span class="hljs-keyword">module</span>, <span class="hljs-string">"exports"</span>, CreateFunction); 9711cb0ef41Sopenharmony_ci} 9721cb0ef41Sopenharmony_ci 9731cb0ef41Sopenharmony_ci<span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Init) 9741cb0ef41Sopenharmony_ci 9751cb0ef41Sopenharmony_ci} <span class="hljs-comment">// namespace demo</span></code> <button class="copy-button">copy</button></pre> 9761cb0ef41Sopenharmony_ci<p>To test:</p> 9771cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// test.js</span> 9781cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>); 9791cb0ef41Sopenharmony_ci 9801cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> fn = <span class="hljs-title function_">addon</span>(); 9811cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">fn</span>()); 9821cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 'hello world'</span></code> <button class="copy-button">copy</button></pre> 9831cb0ef41Sopenharmony_ci<h4>Wrapping C++ objects<span><a class="mark" href="#wrapping-c-objects" id="wrapping-c-objects">#</a></span><a aria-hidden="true" class="legacy" id="addons_wrapping_c_objects"></a></h4> 9841cb0ef41Sopenharmony_ci<p>It is also possible to wrap C++ objects/classes in a way that allows new 9851cb0ef41Sopenharmony_ciinstances to be created using the JavaScript <code>new</code> operator:</p> 9861cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span> 9871cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node.h></span></span> 9881cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobject.h"</span></span> 9891cb0ef41Sopenharmony_ci 9901cb0ef41Sopenharmony_ci<span class="hljs-keyword">namespace</span> demo { 9911cb0ef41Sopenharmony_ci 9921cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Local; 9931cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Object; 9941cb0ef41Sopenharmony_ci 9951cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">InitAll</span><span class="hljs-params">(Local<Object> exports)</span> </span>{ 9961cb0ef41Sopenharmony_ci MyObject::<span class="hljs-built_in">Init</span>(exports); 9971cb0ef41Sopenharmony_ci} 9981cb0ef41Sopenharmony_ci 9991cb0ef41Sopenharmony_ci<span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, InitAll) 10001cb0ef41Sopenharmony_ci 10011cb0ef41Sopenharmony_ci} <span class="hljs-comment">// namespace demo</span></code> <button class="copy-button">copy</button></pre> 10021cb0ef41Sopenharmony_ci<p>Then, in <code>myobject.h</code>, the wrapper class inherits from <code>node::ObjectWrap</code>:</p> 10031cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-comment">// myobject.h</span> 10041cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> MYOBJECT_H</span> 10051cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">define</span> MYOBJECT_H</span> 10061cb0ef41Sopenharmony_ci 10071cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node.h></span></span> 10081cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_object_wrap.h></span></span> 10091cb0ef41Sopenharmony_ci 10101cb0ef41Sopenharmony_ci<span class="hljs-keyword">namespace</span> demo { 10111cb0ef41Sopenharmony_ci 10121cb0ef41Sopenharmony_ci<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyObject</span> : <span class="hljs-keyword">public</span> node::ObjectWrap { 10131cb0ef41Sopenharmony_ci <span class="hljs-keyword">public</span>: 10141cb0ef41Sopenharmony_ci <span class="hljs-function"><span class="hljs-type">static</span> <span class="hljs-type">void</span> <span class="hljs-title">Init</span><span class="hljs-params">(v8::Local<v8::Object> exports)</span></span>; 10151cb0ef41Sopenharmony_ci 10161cb0ef41Sopenharmony_ci <span class="hljs-keyword">private</span>: 10171cb0ef41Sopenharmony_ci <span class="hljs-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">MyObject</span><span class="hljs-params">(<span class="hljs-type">double</span> value = <span class="hljs-number">0</span>)</span></span>; 10181cb0ef41Sopenharmony_ci ~<span class="hljs-built_in">MyObject</span>(); 10191cb0ef41Sopenharmony_ci 10201cb0ef41Sopenharmony_ci <span class="hljs-function"><span class="hljs-type">static</span> <span class="hljs-type">void</span> <span class="hljs-title">New</span><span class="hljs-params">(<span class="hljs-type">const</span> v8::FunctionCallbackInfo<v8::Value>& args)</span></span>; 10211cb0ef41Sopenharmony_ci <span class="hljs-function"><span class="hljs-type">static</span> <span class="hljs-type">void</span> <span class="hljs-title">PlusOne</span><span class="hljs-params">(<span class="hljs-type">const</span> v8::FunctionCallbackInfo<v8::Value>& args)</span></span>; 10221cb0ef41Sopenharmony_ci 10231cb0ef41Sopenharmony_ci <span class="hljs-type">double</span> value_; 10241cb0ef41Sopenharmony_ci}; 10251cb0ef41Sopenharmony_ci 10261cb0ef41Sopenharmony_ci} <span class="hljs-comment">// namespace demo</span> 10271cb0ef41Sopenharmony_ci 10281cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">endif</span></span></code> <button class="copy-button">copy</button></pre> 10291cb0ef41Sopenharmony_ci<p>In <code>myobject.cc</code>, implement the various methods that are to be exposed. 10301cb0ef41Sopenharmony_ciBelow, the method <code>plusOne()</code> is exposed by adding it to the constructor's 10311cb0ef41Sopenharmony_ciprototype:</p> 10321cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-comment">// myobject.cc</span> 10331cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobject.h"</span></span> 10341cb0ef41Sopenharmony_ci 10351cb0ef41Sopenharmony_ci<span class="hljs-keyword">namespace</span> demo { 10361cb0ef41Sopenharmony_ci 10371cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Context; 10381cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Function; 10391cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo; 10401cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::FunctionTemplate; 10411cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Isolate; 10421cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Local; 10431cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Number; 10441cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Object; 10451cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::ObjectTemplate; 10461cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::String; 10471cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Value; 10481cb0ef41Sopenharmony_ci 10491cb0ef41Sopenharmony_ciMyObject::<span class="hljs-built_in">MyObject</span>(<span class="hljs-type">double</span> value) : <span class="hljs-built_in">value_</span>(value) { 10501cb0ef41Sopenharmony_ci} 10511cb0ef41Sopenharmony_ci 10521cb0ef41Sopenharmony_ciMyObject::~<span class="hljs-built_in">MyObject</span>() { 10531cb0ef41Sopenharmony_ci} 10541cb0ef41Sopenharmony_ci 10551cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::Init</span><span class="hljs-params">(Local<Object> exports)</span> </span>{ 10561cb0ef41Sopenharmony_ci Isolate* isolate = exports-><span class="hljs-built_in">GetIsolate</span>(); 10571cb0ef41Sopenharmony_ci Local<Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>(); 10581cb0ef41Sopenharmony_ci 10591cb0ef41Sopenharmony_ci Local<ObjectTemplate> addon_data_tpl = ObjectTemplate::<span class="hljs-built_in">New</span>(isolate); 10601cb0ef41Sopenharmony_ci addon_data_tpl-><span class="hljs-built_in">SetInternalFieldCount</span>(<span class="hljs-number">1</span>); <span class="hljs-comment">// 1 field for the MyObject::New()</span> 10611cb0ef41Sopenharmony_ci Local<Object> addon_data = 10621cb0ef41Sopenharmony_ci addon_data_tpl-><span class="hljs-built_in">NewInstance</span>(context).<span class="hljs-built_in">ToLocalChecked</span>(); 10631cb0ef41Sopenharmony_ci 10641cb0ef41Sopenharmony_ci <span class="hljs-comment">// Prepare constructor template</span> 10651cb0ef41Sopenharmony_ci Local<FunctionTemplate> tpl = FunctionTemplate::<span class="hljs-built_in">New</span>(isolate, New, addon_data); 10661cb0ef41Sopenharmony_ci tpl-><span class="hljs-built_in">SetClassName</span>(String::<span class="hljs-built_in">NewFromUtf8</span>(isolate, <span class="hljs-string">"MyObject"</span>).<span class="hljs-built_in">ToLocalChecked</span>()); 10671cb0ef41Sopenharmony_ci tpl-><span class="hljs-built_in">InstanceTemplate</span>()-><span class="hljs-built_in">SetInternalFieldCount</span>(<span class="hljs-number">1</span>); 10681cb0ef41Sopenharmony_ci 10691cb0ef41Sopenharmony_ci <span class="hljs-comment">// Prototype</span> 10701cb0ef41Sopenharmony_ci <span class="hljs-built_in">NODE_SET_PROTOTYPE_METHOD</span>(tpl, <span class="hljs-string">"plusOne"</span>, PlusOne); 10711cb0ef41Sopenharmony_ci 10721cb0ef41Sopenharmony_ci Local<Function> constructor = tpl-><span class="hljs-built_in">GetFunction</span>(context).<span class="hljs-built_in">ToLocalChecked</span>(); 10731cb0ef41Sopenharmony_ci addon_data-><span class="hljs-built_in">SetInternalField</span>(<span class="hljs-number">0</span>, constructor); 10741cb0ef41Sopenharmony_ci exports-><span class="hljs-built_in">Set</span>(context, String::<span class="hljs-built_in">NewFromUtf8</span>( 10751cb0ef41Sopenharmony_ci isolate, <span class="hljs-string">"MyObject"</span>).<span class="hljs-built_in">ToLocalChecked</span>(), 10761cb0ef41Sopenharmony_ci constructor).<span class="hljs-built_in">FromJust</span>(); 10771cb0ef41Sopenharmony_ci} 10781cb0ef41Sopenharmony_ci 10791cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::New</span><span class="hljs-params">(<span class="hljs-type">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{ 10801cb0ef41Sopenharmony_ci Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>(); 10811cb0ef41Sopenharmony_ci Local<Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>(); 10821cb0ef41Sopenharmony_ci 10831cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (args.<span class="hljs-built_in">IsConstructCall</span>()) { 10841cb0ef41Sopenharmony_ci <span class="hljs-comment">// Invoked as constructor: `new MyObject(...)`</span> 10851cb0ef41Sopenharmony_ci <span class="hljs-type">double</span> value = args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">IsUndefined</span>() ? 10861cb0ef41Sopenharmony_ci <span class="hljs-number">0</span> : args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">NumberValue</span>(context).<span class="hljs-built_in">FromMaybe</span>(<span class="hljs-number">0</span>); 10871cb0ef41Sopenharmony_ci MyObject* obj = <span class="hljs-keyword">new</span> <span class="hljs-built_in">MyObject</span>(value); 10881cb0ef41Sopenharmony_ci obj-><span class="hljs-built_in">Wrap</span>(args.<span class="hljs-built_in">This</span>()); 10891cb0ef41Sopenharmony_ci args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(args.<span class="hljs-built_in">This</span>()); 10901cb0ef41Sopenharmony_ci } <span class="hljs-keyword">else</span> { 10911cb0ef41Sopenharmony_ci <span class="hljs-comment">// Invoked as plain function `MyObject(...)`, turn into construct call.</span> 10921cb0ef41Sopenharmony_ci <span class="hljs-type">const</span> <span class="hljs-type">int</span> argc = <span class="hljs-number">1</span>; 10931cb0ef41Sopenharmony_ci Local<Value> argv[argc] = { args[<span class="hljs-number">0</span>] }; 10941cb0ef41Sopenharmony_ci Local<Function> cons = 10951cb0ef41Sopenharmony_ci args.<span class="hljs-built_in">Data</span>().<span class="hljs-built_in">As</span><Object>()-><span class="hljs-built_in">GetInternalField</span>(<span class="hljs-number">0</span>).<span class="hljs-built_in">As</span><Function>(); 10961cb0ef41Sopenharmony_ci Local<Object> result = 10971cb0ef41Sopenharmony_ci cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_in">ToLocalChecked</span>(); 10981cb0ef41Sopenharmony_ci args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(result); 10991cb0ef41Sopenharmony_ci } 11001cb0ef41Sopenharmony_ci} 11011cb0ef41Sopenharmony_ci 11021cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::PlusOne</span><span class="hljs-params">(<span class="hljs-type">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{ 11031cb0ef41Sopenharmony_ci Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>(); 11041cb0ef41Sopenharmony_ci 11051cb0ef41Sopenharmony_ci MyObject* obj = ObjectWrap::<span class="hljs-built_in">Unwrap</span><MyObject>(args.<span class="hljs-built_in">Holder</span>()); 11061cb0ef41Sopenharmony_ci obj->value_ += <span class="hljs-number">1</span>; 11071cb0ef41Sopenharmony_ci 11081cb0ef41Sopenharmony_ci args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(Number::<span class="hljs-built_in">New</span>(isolate, obj->value_)); 11091cb0ef41Sopenharmony_ci} 11101cb0ef41Sopenharmony_ci 11111cb0ef41Sopenharmony_ci} <span class="hljs-comment">// namespace demo</span></code> <button class="copy-button">copy</button></pre> 11121cb0ef41Sopenharmony_ci<p>To build this example, the <code>myobject.cc</code> file must be added to the 11131cb0ef41Sopenharmony_ci<code>binding.gyp</code>:</p> 11141cb0ef41Sopenharmony_ci<pre><code class="language-json"><span class="hljs-punctuation">{</span> 11151cb0ef41Sopenharmony_ci <span class="hljs-attr">"targets"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> 11161cb0ef41Sopenharmony_ci <span class="hljs-punctuation">{</span> 11171cb0ef41Sopenharmony_ci <span class="hljs-attr">"target_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"addon"</span><span class="hljs-punctuation">,</span> 11181cb0ef41Sopenharmony_ci <span class="hljs-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> 11191cb0ef41Sopenharmony_ci <span class="hljs-string">"addon.cc"</span><span class="hljs-punctuation">,</span> 11201cb0ef41Sopenharmony_ci <span class="hljs-string">"myobject.cc"</span> 11211cb0ef41Sopenharmony_ci <span class="hljs-punctuation">]</span> 11221cb0ef41Sopenharmony_ci <span class="hljs-punctuation">}</span> 11231cb0ef41Sopenharmony_ci <span class="hljs-punctuation">]</span> 11241cb0ef41Sopenharmony_ci<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre> 11251cb0ef41Sopenharmony_ci<p>Test it with:</p> 11261cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// test.js</span> 11271cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>); 11281cb0ef41Sopenharmony_ci 11291cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj = <span class="hljs-keyword">new</span> addon.<span class="hljs-title class_">MyObject</span>(<span class="hljs-number">10</span>); 11301cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(obj.<span class="hljs-title function_">plusOne</span>()); 11311cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 11</span> 11321cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(obj.<span class="hljs-title function_">plusOne</span>()); 11331cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 12</span> 11341cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(obj.<span class="hljs-title function_">plusOne</span>()); 11351cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 13</span></code> <button class="copy-button">copy</button></pre> 11361cb0ef41Sopenharmony_ci<p>The destructor for a wrapper object will run when the object is 11371cb0ef41Sopenharmony_cigarbage-collected. For destructor testing, there are command-line flags that 11381cb0ef41Sopenharmony_cican be used to make it possible to force garbage collection. These flags are 11391cb0ef41Sopenharmony_ciprovided by the underlying V8 JavaScript engine. They are subject to change 11401cb0ef41Sopenharmony_cior removal at any time. They are not documented by Node.js or V8, and they 11411cb0ef41Sopenharmony_cishould never be used outside of testing.</p> 11421cb0ef41Sopenharmony_ci<p>During shutdown of the process or worker threads destructors are not called 11431cb0ef41Sopenharmony_ciby the JS engine. Therefore it's the responsibility of the user to track 11441cb0ef41Sopenharmony_cithese objects and ensure proper destruction to avoid resource leaks.</p> 11451cb0ef41Sopenharmony_ci<h4>Factory of wrapped objects<span><a class="mark" href="#factory-of-wrapped-objects" id="factory-of-wrapped-objects">#</a></span><a aria-hidden="true" class="legacy" id="addons_factory_of_wrapped_objects"></a></h4> 11461cb0ef41Sopenharmony_ci<p>Alternatively, it is possible to use a factory pattern to avoid explicitly 11471cb0ef41Sopenharmony_cicreating object instances using the JavaScript <code>new</code> operator:</p> 11481cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> obj = addon.<span class="hljs-title function_">createObject</span>(); 11491cb0ef41Sopenharmony_ci<span class="hljs-comment">// instead of:</span> 11501cb0ef41Sopenharmony_ci<span class="hljs-comment">// const obj = new addon.Object();</span></code> <button class="copy-button">copy</button></pre> 11511cb0ef41Sopenharmony_ci<p>First, the <code>createObject()</code> method is implemented in <code>addon.cc</code>:</p> 11521cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span> 11531cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node.h></span></span> 11541cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobject.h"</span></span> 11551cb0ef41Sopenharmony_ci 11561cb0ef41Sopenharmony_ci<span class="hljs-keyword">namespace</span> demo { 11571cb0ef41Sopenharmony_ci 11581cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo; 11591cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Isolate; 11601cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Local; 11611cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Object; 11621cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::String; 11631cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Value; 11641cb0ef41Sopenharmony_ci 11651cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">CreateObject</span><span class="hljs-params">(<span class="hljs-type">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{ 11661cb0ef41Sopenharmony_ci MyObject::<span class="hljs-built_in">NewInstance</span>(args); 11671cb0ef41Sopenharmony_ci} 11681cb0ef41Sopenharmony_ci 11691cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">InitAll</span><span class="hljs-params">(Local<Object> exports, Local<Object> <span class="hljs-keyword">module</span>)</span> </span>{ 11701cb0ef41Sopenharmony_ci MyObject::<span class="hljs-built_in">Init</span>(exports-><span class="hljs-built_in">GetIsolate</span>()); 11711cb0ef41Sopenharmony_ci 11721cb0ef41Sopenharmony_ci <span class="hljs-built_in">NODE_SET_METHOD</span>(<span class="hljs-keyword">module</span>, <span class="hljs-string">"exports"</span>, CreateObject); 11731cb0ef41Sopenharmony_ci} 11741cb0ef41Sopenharmony_ci 11751cb0ef41Sopenharmony_ci<span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, InitAll) 11761cb0ef41Sopenharmony_ci 11771cb0ef41Sopenharmony_ci} <span class="hljs-comment">// namespace demo</span></code> <button class="copy-button">copy</button></pre> 11781cb0ef41Sopenharmony_ci<p>In <code>myobject.h</code>, the static method <code>NewInstance()</code> is added to handle 11791cb0ef41Sopenharmony_ciinstantiating the object. This method takes the place of using <code>new</code> in 11801cb0ef41Sopenharmony_ciJavaScript:</p> 11811cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-comment">// myobject.h</span> 11821cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> MYOBJECT_H</span> 11831cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">define</span> MYOBJECT_H</span> 11841cb0ef41Sopenharmony_ci 11851cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node.h></span></span> 11861cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_object_wrap.h></span></span> 11871cb0ef41Sopenharmony_ci 11881cb0ef41Sopenharmony_ci<span class="hljs-keyword">namespace</span> demo { 11891cb0ef41Sopenharmony_ci 11901cb0ef41Sopenharmony_ci<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyObject</span> : <span class="hljs-keyword">public</span> node::ObjectWrap { 11911cb0ef41Sopenharmony_ci <span class="hljs-keyword">public</span>: 11921cb0ef41Sopenharmony_ci <span class="hljs-function"><span class="hljs-type">static</span> <span class="hljs-type">void</span> <span class="hljs-title">Init</span><span class="hljs-params">(v8::Isolate* isolate)</span></span>; 11931cb0ef41Sopenharmony_ci <span class="hljs-function"><span class="hljs-type">static</span> <span class="hljs-type">void</span> <span class="hljs-title">NewInstance</span><span class="hljs-params">(<span class="hljs-type">const</span> v8::FunctionCallbackInfo<v8::Value>& args)</span></span>; 11941cb0ef41Sopenharmony_ci 11951cb0ef41Sopenharmony_ci <span class="hljs-keyword">private</span>: 11961cb0ef41Sopenharmony_ci <span class="hljs-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">MyObject</span><span class="hljs-params">(<span class="hljs-type">double</span> value = <span class="hljs-number">0</span>)</span></span>; 11971cb0ef41Sopenharmony_ci ~<span class="hljs-built_in">MyObject</span>(); 11981cb0ef41Sopenharmony_ci 11991cb0ef41Sopenharmony_ci <span class="hljs-function"><span class="hljs-type">static</span> <span class="hljs-type">void</span> <span class="hljs-title">New</span><span class="hljs-params">(<span class="hljs-type">const</span> v8::FunctionCallbackInfo<v8::Value>& args)</span></span>; 12001cb0ef41Sopenharmony_ci <span class="hljs-function"><span class="hljs-type">static</span> <span class="hljs-type">void</span> <span class="hljs-title">PlusOne</span><span class="hljs-params">(<span class="hljs-type">const</span> v8::FunctionCallbackInfo<v8::Value>& args)</span></span>; 12011cb0ef41Sopenharmony_ci <span class="hljs-type">static</span> v8::Global<v8::Function> constructor; 12021cb0ef41Sopenharmony_ci <span class="hljs-type">double</span> value_; 12031cb0ef41Sopenharmony_ci}; 12041cb0ef41Sopenharmony_ci 12051cb0ef41Sopenharmony_ci} <span class="hljs-comment">// namespace demo</span> 12061cb0ef41Sopenharmony_ci 12071cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">endif</span></span></code> <button class="copy-button">copy</button></pre> 12081cb0ef41Sopenharmony_ci<p>The implementation in <code>myobject.cc</code> is similar to the previous example:</p> 12091cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-comment">// myobject.cc</span> 12101cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node.h></span></span> 12111cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobject.h"</span></span> 12121cb0ef41Sopenharmony_ci 12131cb0ef41Sopenharmony_ci<span class="hljs-keyword">namespace</span> demo { 12141cb0ef41Sopenharmony_ci 12151cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> node::AddEnvironmentCleanupHook; 12161cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Context; 12171cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Function; 12181cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo; 12191cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::FunctionTemplate; 12201cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Global; 12211cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Isolate; 12221cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Local; 12231cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Number; 12241cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Object; 12251cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::String; 12261cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Value; 12271cb0ef41Sopenharmony_ci 12281cb0ef41Sopenharmony_ci<span class="hljs-comment">// Warning! This is not thread-safe, this addon cannot be used for worker</span> 12291cb0ef41Sopenharmony_ci<span class="hljs-comment">// threads.</span> 12301cb0ef41Sopenharmony_ciGlobal<Function> MyObject::constructor; 12311cb0ef41Sopenharmony_ci 12321cb0ef41Sopenharmony_ciMyObject::<span class="hljs-built_in">MyObject</span>(<span class="hljs-type">double</span> value) : <span class="hljs-built_in">value_</span>(value) { 12331cb0ef41Sopenharmony_ci} 12341cb0ef41Sopenharmony_ci 12351cb0ef41Sopenharmony_ciMyObject::~<span class="hljs-built_in">MyObject</span>() { 12361cb0ef41Sopenharmony_ci} 12371cb0ef41Sopenharmony_ci 12381cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::Init</span><span class="hljs-params">(Isolate* isolate)</span> </span>{ 12391cb0ef41Sopenharmony_ci <span class="hljs-comment">// Prepare constructor template</span> 12401cb0ef41Sopenharmony_ci Local<FunctionTemplate> tpl = FunctionTemplate::<span class="hljs-built_in">New</span>(isolate, New); 12411cb0ef41Sopenharmony_ci tpl-><span class="hljs-built_in">SetClassName</span>(String::<span class="hljs-built_in">NewFromUtf8</span>(isolate, <span class="hljs-string">"MyObject"</span>).<span class="hljs-built_in">ToLocalChecked</span>()); 12421cb0ef41Sopenharmony_ci tpl-><span class="hljs-built_in">InstanceTemplate</span>()-><span class="hljs-built_in">SetInternalFieldCount</span>(<span class="hljs-number">1</span>); 12431cb0ef41Sopenharmony_ci 12441cb0ef41Sopenharmony_ci <span class="hljs-comment">// Prototype</span> 12451cb0ef41Sopenharmony_ci <span class="hljs-built_in">NODE_SET_PROTOTYPE_METHOD</span>(tpl, <span class="hljs-string">"plusOne"</span>, PlusOne); 12461cb0ef41Sopenharmony_ci 12471cb0ef41Sopenharmony_ci Local<Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>(); 12481cb0ef41Sopenharmony_ci constructor.<span class="hljs-built_in">Reset</span>(isolate, tpl-><span class="hljs-built_in">GetFunction</span>(context).<span class="hljs-built_in">ToLocalChecked</span>()); 12491cb0ef41Sopenharmony_ci 12501cb0ef41Sopenharmony_ci <span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, [](<span class="hljs-type">void</span>*) { 12511cb0ef41Sopenharmony_ci constructor.<span class="hljs-built_in">Reset</span>(); 12521cb0ef41Sopenharmony_ci }, <span class="hljs-literal">nullptr</span>); 12531cb0ef41Sopenharmony_ci} 12541cb0ef41Sopenharmony_ci 12551cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::New</span><span class="hljs-params">(<span class="hljs-type">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{ 12561cb0ef41Sopenharmony_ci Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>(); 12571cb0ef41Sopenharmony_ci Local<Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>(); 12581cb0ef41Sopenharmony_ci 12591cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (args.<span class="hljs-built_in">IsConstructCall</span>()) { 12601cb0ef41Sopenharmony_ci <span class="hljs-comment">// Invoked as constructor: `new MyObject(...)`</span> 12611cb0ef41Sopenharmony_ci <span class="hljs-type">double</span> value = args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">IsUndefined</span>() ? 12621cb0ef41Sopenharmony_ci <span class="hljs-number">0</span> : args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">NumberValue</span>(context).<span class="hljs-built_in">FromMaybe</span>(<span class="hljs-number">0</span>); 12631cb0ef41Sopenharmony_ci MyObject* obj = <span class="hljs-keyword">new</span> <span class="hljs-built_in">MyObject</span>(value); 12641cb0ef41Sopenharmony_ci obj-><span class="hljs-built_in">Wrap</span>(args.<span class="hljs-built_in">This</span>()); 12651cb0ef41Sopenharmony_ci args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(args.<span class="hljs-built_in">This</span>()); 12661cb0ef41Sopenharmony_ci } <span class="hljs-keyword">else</span> { 12671cb0ef41Sopenharmony_ci <span class="hljs-comment">// Invoked as plain function `MyObject(...)`, turn into construct call.</span> 12681cb0ef41Sopenharmony_ci <span class="hljs-type">const</span> <span class="hljs-type">int</span> argc = <span class="hljs-number">1</span>; 12691cb0ef41Sopenharmony_ci Local<Value> argv[argc] = { args[<span class="hljs-number">0</span>] }; 12701cb0ef41Sopenharmony_ci Local<Function> cons = Local<Function>::<span class="hljs-built_in">New</span>(isolate, constructor); 12711cb0ef41Sopenharmony_ci Local<Object> instance = 12721cb0ef41Sopenharmony_ci cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_in">ToLocalChecked</span>(); 12731cb0ef41Sopenharmony_ci args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(instance); 12741cb0ef41Sopenharmony_ci } 12751cb0ef41Sopenharmony_ci} 12761cb0ef41Sopenharmony_ci 12771cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::NewInstance</span><span class="hljs-params">(<span class="hljs-type">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{ 12781cb0ef41Sopenharmony_ci Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>(); 12791cb0ef41Sopenharmony_ci 12801cb0ef41Sopenharmony_ci <span class="hljs-type">const</span> <span class="hljs-type">unsigned</span> argc = <span class="hljs-number">1</span>; 12811cb0ef41Sopenharmony_ci Local<Value> argv[argc] = { args[<span class="hljs-number">0</span>] }; 12821cb0ef41Sopenharmony_ci Local<Function> cons = Local<Function>::<span class="hljs-built_in">New</span>(isolate, constructor); 12831cb0ef41Sopenharmony_ci Local<Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>(); 12841cb0ef41Sopenharmony_ci Local<Object> instance = 12851cb0ef41Sopenharmony_ci cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_in">ToLocalChecked</span>(); 12861cb0ef41Sopenharmony_ci 12871cb0ef41Sopenharmony_ci args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(instance); 12881cb0ef41Sopenharmony_ci} 12891cb0ef41Sopenharmony_ci 12901cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::PlusOne</span><span class="hljs-params">(<span class="hljs-type">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{ 12911cb0ef41Sopenharmony_ci Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>(); 12921cb0ef41Sopenharmony_ci 12931cb0ef41Sopenharmony_ci MyObject* obj = ObjectWrap::<span class="hljs-built_in">Unwrap</span><MyObject>(args.<span class="hljs-built_in">Holder</span>()); 12941cb0ef41Sopenharmony_ci obj->value_ += <span class="hljs-number">1</span>; 12951cb0ef41Sopenharmony_ci 12961cb0ef41Sopenharmony_ci args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(Number::<span class="hljs-built_in">New</span>(isolate, obj->value_)); 12971cb0ef41Sopenharmony_ci} 12981cb0ef41Sopenharmony_ci 12991cb0ef41Sopenharmony_ci} <span class="hljs-comment">// namespace demo</span></code> <button class="copy-button">copy</button></pre> 13001cb0ef41Sopenharmony_ci<p>Once again, to build this example, the <code>myobject.cc</code> file must be added to the 13011cb0ef41Sopenharmony_ci<code>binding.gyp</code>:</p> 13021cb0ef41Sopenharmony_ci<pre><code class="language-json"><span class="hljs-punctuation">{</span> 13031cb0ef41Sopenharmony_ci <span class="hljs-attr">"targets"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> 13041cb0ef41Sopenharmony_ci <span class="hljs-punctuation">{</span> 13051cb0ef41Sopenharmony_ci <span class="hljs-attr">"target_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"addon"</span><span class="hljs-punctuation">,</span> 13061cb0ef41Sopenharmony_ci <span class="hljs-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> 13071cb0ef41Sopenharmony_ci <span class="hljs-string">"addon.cc"</span><span class="hljs-punctuation">,</span> 13081cb0ef41Sopenharmony_ci <span class="hljs-string">"myobject.cc"</span> 13091cb0ef41Sopenharmony_ci <span class="hljs-punctuation">]</span> 13101cb0ef41Sopenharmony_ci <span class="hljs-punctuation">}</span> 13111cb0ef41Sopenharmony_ci <span class="hljs-punctuation">]</span> 13121cb0ef41Sopenharmony_ci<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre> 13131cb0ef41Sopenharmony_ci<p>Test it with:</p> 13141cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// test.js</span> 13151cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> createObject = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>); 13161cb0ef41Sopenharmony_ci 13171cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj = <span class="hljs-title function_">createObject</span>(<span class="hljs-number">10</span>); 13181cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(obj.<span class="hljs-title function_">plusOne</span>()); 13191cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 11</span> 13201cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(obj.<span class="hljs-title function_">plusOne</span>()); 13211cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 12</span> 13221cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(obj.<span class="hljs-title function_">plusOne</span>()); 13231cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 13</span> 13241cb0ef41Sopenharmony_ci 13251cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj2 = <span class="hljs-title function_">createObject</span>(<span class="hljs-number">20</span>); 13261cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(obj2.<span class="hljs-title function_">plusOne</span>()); 13271cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 21</span> 13281cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(obj2.<span class="hljs-title function_">plusOne</span>()); 13291cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 22</span> 13301cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(obj2.<span class="hljs-title function_">plusOne</span>()); 13311cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 23</span></code> <button class="copy-button">copy</button></pre> 13321cb0ef41Sopenharmony_ci<h4>Passing wrapped objects around<span><a class="mark" href="#passing-wrapped-objects-around" id="passing-wrapped-objects-around">#</a></span><a aria-hidden="true" class="legacy" id="addons_passing_wrapped_objects_around"></a></h4> 13331cb0ef41Sopenharmony_ci<p>In addition to wrapping and returning C++ objects, it is possible to pass 13341cb0ef41Sopenharmony_ciwrapped objects around by unwrapping them with the Node.js helper function 13351cb0ef41Sopenharmony_ci<code>node::ObjectWrap::Unwrap</code>. The following examples shows a function <code>add()</code> 13361cb0ef41Sopenharmony_cithat can take two <code>MyObject</code> objects as input arguments:</p> 13371cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span> 13381cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node.h></span></span> 13391cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_object_wrap.h></span></span> 13401cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobject.h"</span></span> 13411cb0ef41Sopenharmony_ci 13421cb0ef41Sopenharmony_ci<span class="hljs-keyword">namespace</span> demo { 13431cb0ef41Sopenharmony_ci 13441cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Context; 13451cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo; 13461cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Isolate; 13471cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Local; 13481cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Number; 13491cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Object; 13501cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::String; 13511cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Value; 13521cb0ef41Sopenharmony_ci 13531cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">CreateObject</span><span class="hljs-params">(<span class="hljs-type">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{ 13541cb0ef41Sopenharmony_ci MyObject::<span class="hljs-built_in">NewInstance</span>(args); 13551cb0ef41Sopenharmony_ci} 13561cb0ef41Sopenharmony_ci 13571cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Add</span><span class="hljs-params">(<span class="hljs-type">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{ 13581cb0ef41Sopenharmony_ci Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>(); 13591cb0ef41Sopenharmony_ci Local<Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>(); 13601cb0ef41Sopenharmony_ci 13611cb0ef41Sopenharmony_ci MyObject* obj1 = node::ObjectWrap::<span class="hljs-built_in">Unwrap</span><MyObject>( 13621cb0ef41Sopenharmony_ci args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">ToObject</span>(context).<span class="hljs-built_in">ToLocalChecked</span>()); 13631cb0ef41Sopenharmony_ci MyObject* obj2 = node::ObjectWrap::<span class="hljs-built_in">Unwrap</span><MyObject>( 13641cb0ef41Sopenharmony_ci args[<span class="hljs-number">1</span>]-><span class="hljs-built_in">ToObject</span>(context).<span class="hljs-built_in">ToLocalChecked</span>()); 13651cb0ef41Sopenharmony_ci 13661cb0ef41Sopenharmony_ci <span class="hljs-type">double</span> sum = obj1-><span class="hljs-built_in">value</span>() + obj2-><span class="hljs-built_in">value</span>(); 13671cb0ef41Sopenharmony_ci args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(Number::<span class="hljs-built_in">New</span>(isolate, sum)); 13681cb0ef41Sopenharmony_ci} 13691cb0ef41Sopenharmony_ci 13701cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">InitAll</span><span class="hljs-params">(Local<Object> exports)</span> </span>{ 13711cb0ef41Sopenharmony_ci MyObject::<span class="hljs-built_in">Init</span>(exports-><span class="hljs-built_in">GetIsolate</span>()); 13721cb0ef41Sopenharmony_ci 13731cb0ef41Sopenharmony_ci <span class="hljs-built_in">NODE_SET_METHOD</span>(exports, <span class="hljs-string">"createObject"</span>, CreateObject); 13741cb0ef41Sopenharmony_ci <span class="hljs-built_in">NODE_SET_METHOD</span>(exports, <span class="hljs-string">"add"</span>, Add); 13751cb0ef41Sopenharmony_ci} 13761cb0ef41Sopenharmony_ci 13771cb0ef41Sopenharmony_ci<span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, InitAll) 13781cb0ef41Sopenharmony_ci 13791cb0ef41Sopenharmony_ci} <span class="hljs-comment">// namespace demo</span></code> <button class="copy-button">copy</button></pre> 13801cb0ef41Sopenharmony_ci<p>In <code>myobject.h</code>, a new public method is added to allow access to private values 13811cb0ef41Sopenharmony_ciafter unwrapping the object.</p> 13821cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-comment">// myobject.h</span> 13831cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> MYOBJECT_H</span> 13841cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">define</span> MYOBJECT_H</span> 13851cb0ef41Sopenharmony_ci 13861cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node.h></span></span> 13871cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_object_wrap.h></span></span> 13881cb0ef41Sopenharmony_ci 13891cb0ef41Sopenharmony_ci<span class="hljs-keyword">namespace</span> demo { 13901cb0ef41Sopenharmony_ci 13911cb0ef41Sopenharmony_ci<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyObject</span> : <span class="hljs-keyword">public</span> node::ObjectWrap { 13921cb0ef41Sopenharmony_ci <span class="hljs-keyword">public</span>: 13931cb0ef41Sopenharmony_ci <span class="hljs-function"><span class="hljs-type">static</span> <span class="hljs-type">void</span> <span class="hljs-title">Init</span><span class="hljs-params">(v8::Isolate* isolate)</span></span>; 13941cb0ef41Sopenharmony_ci <span class="hljs-function"><span class="hljs-type">static</span> <span class="hljs-type">void</span> <span class="hljs-title">NewInstance</span><span class="hljs-params">(<span class="hljs-type">const</span> v8::FunctionCallbackInfo<v8::Value>& args)</span></span>; 13951cb0ef41Sopenharmony_ci <span class="hljs-function"><span class="hljs-keyword">inline</span> <span class="hljs-type">double</span> <span class="hljs-title">value</span><span class="hljs-params">()</span> <span class="hljs-type">const</span> </span>{ <span class="hljs-keyword">return</span> value_; } 13961cb0ef41Sopenharmony_ci 13971cb0ef41Sopenharmony_ci <span class="hljs-keyword">private</span>: 13981cb0ef41Sopenharmony_ci <span class="hljs-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">MyObject</span><span class="hljs-params">(<span class="hljs-type">double</span> value = <span class="hljs-number">0</span>)</span></span>; 13991cb0ef41Sopenharmony_ci ~<span class="hljs-built_in">MyObject</span>(); 14001cb0ef41Sopenharmony_ci 14011cb0ef41Sopenharmony_ci <span class="hljs-function"><span class="hljs-type">static</span> <span class="hljs-type">void</span> <span class="hljs-title">New</span><span class="hljs-params">(<span class="hljs-type">const</span> v8::FunctionCallbackInfo<v8::Value>& args)</span></span>; 14021cb0ef41Sopenharmony_ci <span class="hljs-type">static</span> v8::Global<v8::Function> constructor; 14031cb0ef41Sopenharmony_ci <span class="hljs-type">double</span> value_; 14041cb0ef41Sopenharmony_ci}; 14051cb0ef41Sopenharmony_ci 14061cb0ef41Sopenharmony_ci} <span class="hljs-comment">// namespace demo</span> 14071cb0ef41Sopenharmony_ci 14081cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">endif</span></span></code> <button class="copy-button">copy</button></pre> 14091cb0ef41Sopenharmony_ci<p>The implementation of <code>myobject.cc</code> is similar to before:</p> 14101cb0ef41Sopenharmony_ci<pre><code class="language-cpp"><span class="hljs-comment">// myobject.cc</span> 14111cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node.h></span></span> 14121cb0ef41Sopenharmony_ci<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobject.h"</span></span> 14131cb0ef41Sopenharmony_ci 14141cb0ef41Sopenharmony_ci<span class="hljs-keyword">namespace</span> demo { 14151cb0ef41Sopenharmony_ci 14161cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> node::AddEnvironmentCleanupHook; 14171cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Context; 14181cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Function; 14191cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::FunctionCallbackInfo; 14201cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::FunctionTemplate; 14211cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Global; 14221cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Isolate; 14231cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Local; 14241cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Object; 14251cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::String; 14261cb0ef41Sopenharmony_ci<span class="hljs-keyword">using</span> v8::Value; 14271cb0ef41Sopenharmony_ci 14281cb0ef41Sopenharmony_ci<span class="hljs-comment">// Warning! This is not thread-safe, this addon cannot be used for worker</span> 14291cb0ef41Sopenharmony_ci<span class="hljs-comment">// threads.</span> 14301cb0ef41Sopenharmony_ciGlobal<Function> MyObject::constructor; 14311cb0ef41Sopenharmony_ci 14321cb0ef41Sopenharmony_ciMyObject::<span class="hljs-built_in">MyObject</span>(<span class="hljs-type">double</span> value) : <span class="hljs-built_in">value_</span>(value) { 14331cb0ef41Sopenharmony_ci} 14341cb0ef41Sopenharmony_ci 14351cb0ef41Sopenharmony_ciMyObject::~<span class="hljs-built_in">MyObject</span>() { 14361cb0ef41Sopenharmony_ci} 14371cb0ef41Sopenharmony_ci 14381cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::Init</span><span class="hljs-params">(Isolate* isolate)</span> </span>{ 14391cb0ef41Sopenharmony_ci <span class="hljs-comment">// Prepare constructor template</span> 14401cb0ef41Sopenharmony_ci Local<FunctionTemplate> tpl = FunctionTemplate::<span class="hljs-built_in">New</span>(isolate, New); 14411cb0ef41Sopenharmony_ci tpl-><span class="hljs-built_in">SetClassName</span>(String::<span class="hljs-built_in">NewFromUtf8</span>(isolate, <span class="hljs-string">"MyObject"</span>).<span class="hljs-built_in">ToLocalChecked</span>()); 14421cb0ef41Sopenharmony_ci tpl-><span class="hljs-built_in">InstanceTemplate</span>()-><span class="hljs-built_in">SetInternalFieldCount</span>(<span class="hljs-number">1</span>); 14431cb0ef41Sopenharmony_ci 14441cb0ef41Sopenharmony_ci Local<Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>(); 14451cb0ef41Sopenharmony_ci constructor.<span class="hljs-built_in">Reset</span>(isolate, tpl-><span class="hljs-built_in">GetFunction</span>(context).<span class="hljs-built_in">ToLocalChecked</span>()); 14461cb0ef41Sopenharmony_ci 14471cb0ef41Sopenharmony_ci <span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, [](<span class="hljs-type">void</span>*) { 14481cb0ef41Sopenharmony_ci constructor.<span class="hljs-built_in">Reset</span>(); 14491cb0ef41Sopenharmony_ci }, <span class="hljs-literal">nullptr</span>); 14501cb0ef41Sopenharmony_ci} 14511cb0ef41Sopenharmony_ci 14521cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::New</span><span class="hljs-params">(<span class="hljs-type">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{ 14531cb0ef41Sopenharmony_ci Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>(); 14541cb0ef41Sopenharmony_ci Local<Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>(); 14551cb0ef41Sopenharmony_ci 14561cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (args.<span class="hljs-built_in">IsConstructCall</span>()) { 14571cb0ef41Sopenharmony_ci <span class="hljs-comment">// Invoked as constructor: `new MyObject(...)`</span> 14581cb0ef41Sopenharmony_ci <span class="hljs-type">double</span> value = args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">IsUndefined</span>() ? 14591cb0ef41Sopenharmony_ci <span class="hljs-number">0</span> : args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">NumberValue</span>(context).<span class="hljs-built_in">FromMaybe</span>(<span class="hljs-number">0</span>); 14601cb0ef41Sopenharmony_ci MyObject* obj = <span class="hljs-keyword">new</span> <span class="hljs-built_in">MyObject</span>(value); 14611cb0ef41Sopenharmony_ci obj-><span class="hljs-built_in">Wrap</span>(args.<span class="hljs-built_in">This</span>()); 14621cb0ef41Sopenharmony_ci args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(args.<span class="hljs-built_in">This</span>()); 14631cb0ef41Sopenharmony_ci } <span class="hljs-keyword">else</span> { 14641cb0ef41Sopenharmony_ci <span class="hljs-comment">// Invoked as plain function `MyObject(...)`, turn into construct call.</span> 14651cb0ef41Sopenharmony_ci <span class="hljs-type">const</span> <span class="hljs-type">int</span> argc = <span class="hljs-number">1</span>; 14661cb0ef41Sopenharmony_ci Local<Value> argv[argc] = { args[<span class="hljs-number">0</span>] }; 14671cb0ef41Sopenharmony_ci Local<Function> cons = Local<Function>::<span class="hljs-built_in">New</span>(isolate, constructor); 14681cb0ef41Sopenharmony_ci Local<Object> instance = 14691cb0ef41Sopenharmony_ci cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_in">ToLocalChecked</span>(); 14701cb0ef41Sopenharmony_ci args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(instance); 14711cb0ef41Sopenharmony_ci } 14721cb0ef41Sopenharmony_ci} 14731cb0ef41Sopenharmony_ci 14741cb0ef41Sopenharmony_ci<span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::NewInstance</span><span class="hljs-params">(<span class="hljs-type">const</span> FunctionCallbackInfo<Value>& args)</span> </span>{ 14751cb0ef41Sopenharmony_ci Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>(); 14761cb0ef41Sopenharmony_ci 14771cb0ef41Sopenharmony_ci <span class="hljs-type">const</span> <span class="hljs-type">unsigned</span> argc = <span class="hljs-number">1</span>; 14781cb0ef41Sopenharmony_ci Local<Value> argv[argc] = { args[<span class="hljs-number">0</span>] }; 14791cb0ef41Sopenharmony_ci Local<Function> cons = Local<Function>::<span class="hljs-built_in">New</span>(isolate, constructor); 14801cb0ef41Sopenharmony_ci Local<Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>(); 14811cb0ef41Sopenharmony_ci Local<Object> instance = 14821cb0ef41Sopenharmony_ci cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_in">ToLocalChecked</span>(); 14831cb0ef41Sopenharmony_ci 14841cb0ef41Sopenharmony_ci args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(instance); 14851cb0ef41Sopenharmony_ci} 14861cb0ef41Sopenharmony_ci 14871cb0ef41Sopenharmony_ci} <span class="hljs-comment">// namespace demo</span></code> <button class="copy-button">copy</button></pre> 14881cb0ef41Sopenharmony_ci<p>Test it with:</p> 14891cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// test.js</span> 14901cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>); 14911cb0ef41Sopenharmony_ci 14921cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj1 = addon.<span class="hljs-title function_">createObject</span>(<span class="hljs-number">10</span>); 14931cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> obj2 = addon.<span class="hljs-title function_">createObject</span>(<span class="hljs-number">20</span>); 14941cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> result = addon.<span class="hljs-title function_">add</span>(obj1, obj2); 14951cb0ef41Sopenharmony_ci 14961cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(result); 14971cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 30</span></code> <button class="copy-button">copy</button></pre></section> 14981cb0ef41Sopenharmony_ci <!-- API END --> 14991cb0ef41Sopenharmony_ci </div> 15001cb0ef41Sopenharmony_ci </div> 15011cb0ef41Sopenharmony_ci </div> 15021cb0ef41Sopenharmony_ci</body> 15031cb0ef41Sopenharmony_ci</html> 1504