Skip to content

Commit 2d9b0b7

Browse files
committed
napi例子
1 parent 3272a04 commit 2d9b0b7

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed
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() ); // hello
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "hello",
5+
"sources": [ "./src/hello.cc" ]
6+
}
7+
]
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "2017.06.03-node8-napi-example",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"bindings": "^1.2.1"
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <node_api.h>
2+
3+
napi_value HelloMethod (napi_env env, napi_callback_info info) {
4+
napi_value world;
5+
napi_create_string_utf8(env, "world", 5, &world);
6+
return world;
7+
}
8+
9+
void Init (napi_env env, napi_value exports, napi_value module, void* priv) {
10+
napi_property_descriptor desc = { "hello", 0, HelloMethod, 0, 0, 0, napi_default, 0 };
11+
napi_define_properties(env, exports, 1, &desc);
12+
}
13+
14+
NAPI_MODULE(hello, Init);

0 commit comments

Comments
 (0)