Skip to content

Commit b847923

Browse files
Anisha Rohramhdawson
authored andcommitted
Added working node-addon-api examples for examples
PR-URL: nodejs/abi-stable-node-addon-examples#8 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Sampson Gao <sampsong@ca.ibm.com>
1 parent a1eb8eb commit b847923

File tree

35 files changed

+549
-2
lines changed

35 files changed

+549
-2
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#include <napi.h>
2-
#include <uv.h>
32

43
Napi::String Method(const Napi::CallbackInfo& info) {
54
Napi::Env env = info.Env();
65
return Napi::String::New(env, "world");
76
}
87

9-
void Init(Napi::Env env, Napi::Object exports, Napi::Object module) {
8+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
109
exports.Set(Napi::String::New(env, "hello"),
1110
Napi::Function::New(env, Method));
11+
return exports;
1212
}
1313

1414
NODE_API_MODULE(hello, Init)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <napi.h>
2+
3+
Napi::Value Add(const Napi::CallbackInfo& info) {
4+
Napi::Env env = info.Env();
5+
6+
if (info.Length() < 2) {
7+
Napi::TypeError::New(env, "Wrong number of arguments").ThrowAsJavaScriptException();
8+
return env.Null();
9+
}
10+
11+
if (!info[0].IsNumber() || !info[1].IsNumber()) {
12+
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
13+
return env.Null();
14+
}
15+
16+
double arg0 = info[0].As<Napi::Number>().DoubleValue();
17+
double arg1 = info[1].As<Napi::Number>().DoubleValue();
18+
Napi::Number num = Napi::Number::New(env, arg0 + arg1);
19+
20+
return num;
21+
}
22+
23+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
24+
exports.Set(Napi::String::New(env, "add"),
25+
Napi::Function::New(env, Add));
26+
return exports;
27+
}
28+
29+
NODE_API_MODULE(addon, Init)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var addon = require('bindings')('addon.node')
2+
3+
console.log('This should be eight:', addon.add(3, 5))
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": "addon",
5+
"cflags!": [ "-fno-exceptions" ],
6+
"cflags_cc!": [ "-fno-exceptions" ],
7+
"sources": [ "addon.cc" ],
8+
"include_dirs": [
9+
"<!@(node -p \"require('node-addon-api').include\")"
10+
],
11+
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
12+
}
13+
]
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "function_arguments",
3+
"version": "0.0.0",
4+
"description": "Node.js Addons Example #2",
5+
"main": "addon.js",
6+
"private": true,
7+
"dependencies": {
8+
"bindings": "~1.2.1",
9+
"node-addon-api": "^0.6.3"
10+
},
11+
"scripts": {
12+
"test": "node addon.js"
13+
},
14+
"gypfile": true
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <napi.h>
2+
3+
void RunCallback(const Napi::CallbackInfo& info) {
4+
Napi::Env env = info.Env();
5+
Napi::Function cb = info[0].As<Napi::Function>();
6+
cb.MakeCallback(env.Global(), { Napi::String::New(env, "hello world") });
7+
}
8+
9+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
10+
return Napi::Function::New(env, RunCallback);
11+
}
12+
13+
NODE_API_MODULE(addon, Init)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var addon = require('bindings')('addon');
2+
3+
addon(function(msg){
4+
console.log(msg); // 'hello world'
5+
});
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": "addon",
5+
"cflags!": [ "-fno-exceptions" ],
6+
"cflags_cc!": [ "-fno-exceptions" ],
7+
"sources": [ "addon.cc" ],
8+
"include_dirs": [
9+
"<!@(node -p \"require('node-addon-api').include\")"
10+
],
11+
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
12+
}
13+
]
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "callbacks",
3+
"version": "0.0.0",
4+
"description": "Node.js Addons Example #3",
5+
"main": "addon.js",
6+
"private": true,
7+
"gypfile": true,
8+
"dependencies": {
9+
"bindings": "~1.2.1",
10+
"node-addon-api": "^0.6.3"
11+
}
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <napi.h>
2+
3+
Napi::Object CreateObject(const Napi::CallbackInfo& info) {
4+
Napi::Env env = info.Env();
5+
Napi::Object obj = Napi::Object::New(env);
6+
obj.Set(Napi::String::New(env, "msg"), info[0].ToString());
7+
8+
return obj;
9+
}
10+
11+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
12+
return Napi::Function::New(env, CreateObject, "createObject");
13+
}
14+
15+
NODE_API_MODULE(addon, Init)

0 commit comments

Comments
 (0)