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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
8997026d18
commit
5279035d93
@ -221,7 +221,7 @@ illustration of how it can be used.
|
|||||||
> Stability: 1 - Experimental
|
> Stability: 1 - Experimental
|
||||||
|
|
||||||
N-API is an API for building native Addons. It is independent from
|
N-API is an API for building native Addons. It is independent from
|
||||||
the underlying JavaScript runtime (ex V8) and is maintained as part of
|
the underlying JavaScript runtime (e.g., V8) and is maintained as part of
|
||||||
Node.js itself. This API will be Application Binary Interface (ABI) stable
|
Node.js itself. This API will be Application Binary Interface (ABI) stable
|
||||||
across version of Node.js. It is intended to insulate Addons from
|
across version of Node.js. It is intended to insulate Addons from
|
||||||
changes in the underlying JavaScript engine and allow modules
|
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
|
|||||||
or [Native Abstractions for Node.js][] APIs, the functions available
|
or [Native Abstractions for Node.js][] APIs, the functions available
|
||||||
in the N-API are used.
|
in the N-API are used.
|
||||||
|
|
||||||
|
To use N-API in the above "Hello world" example, replace the content of
|
||||||
|
`hello.cc` with the following. All other instructions remain the same.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
// hello.cc using N-API
|
||||||
|
#include <node_api.h>
|
||||||
|
|
||||||
|
namespace demo {
|
||||||
|
|
||||||
|
napi_value Method(napi_env env, napi_callback_info args) {
|
||||||
|
napi_value greeting;
|
||||||
|
napi_status status;
|
||||||
|
|
||||||
|
status = napi_create_string_utf8(env, "hello", 6, &greeting);
|
||||||
|
if (status != napi_ok) return nullptr;
|
||||||
|
return greeting;
|
||||||
|
}
|
||||||
|
|
||||||
|
napi_value init(napi_env env, napi_value exports) {
|
||||||
|
napi_status status;
|
||||||
|
napi_value fn;
|
||||||
|
|
||||||
|
status = napi_create_function(env, nullptr, 0, Method, nullptr, &fn);
|
||||||
|
if (status != napi_ok) return nullptr;
|
||||||
|
|
||||||
|
status = napi_set_named_property(env, exports, "hello", fn);
|
||||||
|
if (status != napi_ok) return nullptr;
|
||||||
|
return exports;
|
||||||
|
}
|
||||||
|
|
||||||
|
NAPI_MODULE(NODE_GYP_MODULE_NAME, init)
|
||||||
|
|
||||||
|
} // namespace demo
|
||||||
|
```
|
||||||
|
|
||||||
The functions available and how to use them are documented in the
|
The functions available and how to use them are documented in the
|
||||||
section titled [C/C++ Addons - N-API](n-api.html).
|
section titled [C/C++ Addons - N-API](n-api.html).
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user