Skip to content

Commit 8180d30

Browse files
example: add Addon<T> example
Signed-off-by: Gabriel Schulhof <[email protected]> PR-URL: #209 Reviewed-By: Michael Dawson <[email protected]>
1 parent 5464927 commit 8180d30

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "hello",
5+
"cflags!": [ "-fno-exceptions" ],
6+
"cflags_cc!": [ "-fno-exceptions" ],
7+
"sources": [ "hello.cc" ],
8+
"include_dirs": [
9+
"<!@(node -p \"require('node-addon-api').include\")"
10+
],
11+
'defines': ['NAPI_DISABLE_CPP_EXCEPTIONS'],
12+
}
13+
]
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <napi.h>
2+
3+
class HelloAddon : public Napi::Addon<HelloAddon> {
4+
public:
5+
HelloAddon(Napi::Env env, Napi::Object exports) {
6+
DefineAddon(exports, {
7+
InstanceMethod("hello", &HelloAddon::Hello, napi_enumerable)
8+
});
9+
}
10+
11+
private:
12+
Napi::Value Hello(const Napi::CallbackInfo& info) {
13+
return Napi::String::New(info.Env(), "world");
14+
}
15+
};
16+
17+
NODE_API_ADDON(HelloAddon)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var addon = require('bindings')('hello');
2+
3+
console.log(addon.hello()); // 'world'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "hello_world",
3+
"version": "0.0.0",
4+
"description": "Node.js Addons Example #1",
5+
"main": "hello.js",
6+
"private": true,
7+
"dependencies": {
8+
"bindings": "~1.2.1",
9+
"node-addon-api": "^4.0.0"
10+
},
11+
"scripts": {
12+
"test": "node hello.js"
13+
},
14+
"gypfile": true
15+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Implementations of examples are named either after Node.js versions (`node_0.10`
99
- [`nan`](https://github.com/nodejs/nan): C++-based abstraction between Node and direct V8 APIs.
1010
- [`Node-API`](https://nodejs.org/api/n-api.html): C-based API guaranteeing [ABI stability](https://nodejs.org/en/docs/guides/abi-stability/) across different node versions as well as JavaScript engines. (Node-API was previously known as N-API.)
1111
- [`node-addon-api`](https://github.com/nodejs/node-addon-api): header-only C++ wrapper classes which simplify the use of the C-based Node-API.
12+
- [`node-addon-api-addon-class`](https://github.com/nodejs/node-addon-api/tree/main/doc/addon.md): Similar to `node-addon-api`, but deriving from the `Napi::Addon` class. [1_hello_world](./1_hello_world) provides an example.
1213

1314
Implementations against unsupported versions of Node.js are provided for
1415
completeness and historical context. They are not maintained.

0 commit comments

Comments
 (0)