Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0a0a56a

Browse filesBrowse files
fhinkelMylesBorins
authored andcommitted
doc: add "Hello world" example for N-API
Our Addons documentation has a "Hello world" example that outlines all steps to build it. Adding the sources for this "Hello world" example for N-API. PR-URL: #17425 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 865c452 commit 0a0a56a
Copy full SHA for 0a0a56a

File tree

Expand file treeCollapse file tree

1 file changed

+36
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+36
-1
lines changed
Open diff view settings
Collapse file

‎doc/api/addons.md‎

Copy file name to clipboardExpand all lines: doc/api/addons.md
+36-1Lines changed: 36 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ illustration of how it can be used.
221221
> Stability: 1 - Experimental
222222
223223
N-API is an API for building native Addons. It is independent from
224-
the underlying JavaScript runtime (ex V8) and is maintained as part of
224+
the underlying JavaScript runtime (e.g., V8) and is maintained as part of
225225
Node.js itself. This API will be Application Binary Interface (ABI) stable
226226
across version of Node.js. It is intended to insulate Addons from
227227
changes in the underlying JavaScript engine and allow modules
@@ -232,6 +232,41 @@ set of APIs that are used by the native code. Instead of using the V8
232232
or [Native Abstractions for Node.js][] APIs, the functions available
233233
in the N-API are used.
234234

235+
To use N-API in the above "Hello world" example, replace the content of
236+
`hello.cc` with the following. All other instructions remain the same.
237+
238+
```cpp
239+
// hello.cc using N-API
240+
#include <node_api.h>
241+
242+
namespace demo {
243+
244+
napi_value Method(napi_env env, napi_callback_info args) {
245+
napi_value greeting;
246+
napi_status status;
247+
248+
status = napi_create_string_utf8(env, "hello", 6, &greeting);
249+
if (status != napi_ok) return nullptr;
250+
return greeting;
251+
}
252+
253+
napi_value init(napi_env env, napi_value exports) {
254+
napi_status status;
255+
napi_value fn;
256+
257+
status = napi_create_function(env, nullptr, 0, Method, nullptr, &fn);
258+
if (status != napi_ok) return nullptr;
259+
260+
status = napi_set_named_property(env, exports, "hello", fn);
261+
if (status != napi_ok) return nullptr;
262+
return exports;
263+
}
264+
265+
NAPI_MODULE(NODE_GYP_MODULE_NAME, init)
266+
267+
} // namespace demo
268+
```
269+
235270
The functions available and how to use them are documented in the
236271
section titled [C/C++ Addons - N-API](n-api.html).
237272

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.