Skip to content

Commit df4f1a1

Browse files
author
Sampson Gao
committed
Update addons example to use updated napi
1 parent 3133765 commit df4f1a1

File tree

16 files changed

+562
-216
lines changed

16 files changed

+562
-216
lines changed

1_hello_world/abi/hello.cc

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
#include <node_jsvmapi.h>
1+
#include <node_api.h>
2+
#include <assert.h>
23

3-
void Method(napi_env env, napi_func_cb_info info) {
4-
napi_set_return_value(
5-
env,
6-
info,
7-
napi_create_string(env, "world"));
4+
void Method(napi_env env, napi_callback_info info) {
5+
napi_status status;
6+
napi_value world;
7+
status = napi_create_string_utf8(env, "world", -1, &world);
8+
assert(status == napi_ok);
9+
status = napi_set_return_value(env, info, world);
10+
assert(status == napi_ok);
811
}
912

13+
#define DECLARE_NAPI_METHOD(name, func) \
14+
{ name, func, 0, 0, 0, napi_default, 0 }
15+
1016
void Init(napi_env env, napi_value exports, napi_value module) {
11-
napi_set_property(env, exports,
12-
napi_property_name(env, "hello"),
13-
napi_create_function(env, Method));
17+
napi_status status;
18+
napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method);
19+
status = napi_define_properties(env, exports, 1, &desc);
20+
assert(status == napi_ok);
1421
}
1522

16-
NODE_MODULE_ABI(addon, Init)
23+
NAPI_MODULE(addon, Init)
1724

2_function_arguments/abi/addon.cc

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,59 @@
1-
#include <node_jsvmapi.h>
2-
3-
void Add(napi_env env, napi_func_cb_info info) {
4-
if (napi_get_cb_args_length(env, info) < 2) {
5-
napi_throw(
6-
env,
7-
napi_create_type_error(
8-
env,
9-
napi_create_string(env, "Wrong number of arguments")));
1+
#include <node_api.h>
2+
#include <assert.h>
3+
4+
void Add(napi_env env, napi_callback_info info) {
5+
napi_status status;
6+
7+
int argc;
8+
status = napi_get_cb_args_length(env, info, &argc);
9+
assert(status == napi_ok);
10+
11+
if (argc < 2) {
12+
napi_throw_type_error(env, "Wrong number of arguments");
1013
return;
1114
}
1215

1316
napi_value args[2];
14-
napi_get_cb_args(env, info, args, 2);
15-
16-
if (napi_get_type_of_value(env, args[0]) != napi_number ||
17-
napi_get_type_of_value(env, args[1]) != napi_number) {
18-
napi_throw(
19-
env,
20-
napi_create_type_error(
21-
env,
22-
napi_create_string(env, "Wrong arguments")));
17+
status = napi_get_cb_args(env, info, args, 2);
18+
assert(status == napi_ok);
19+
20+
napi_valuetype valuetype0;
21+
status = napi_get_type_of_value(env, args[0], &valuetype0);
22+
assert(status == napi_ok);
23+
24+
napi_valuetype valuetype1;
25+
status = napi_get_type_of_value(env, args[1], &valuetype1);
26+
assert(status == napi_ok);
27+
28+
if (valuetype0 != napi_number || valuetype1 != napi_number) {
29+
napi_throw_type_error(env, "Wrong arguments");
2330
return;
2431
}
2532

26-
double value = napi_get_number_from_value(env, args[0])
27-
+ napi_get_number_from_value(env, args[1]);
28-
napi_value num = napi_create_number(env, value);
33+
double value0;
34+
status = napi_get_value_double(env, args[0], &value0);
35+
assert(status == napi_ok);
36+
37+
double value1;
38+
status = napi_get_value_double(env, args[1], &value1);
39+
assert(status == napi_ok);
40+
41+
napi_value sum;
42+
status = napi_create_number(env, value0 + value1, &sum);
43+
assert(status == napi_ok);
2944

30-
napi_set_return_value(env, info, num);
45+
status = napi_set_return_value(env, info, sum);
46+
assert(status == napi_ok);
3147
}
3248

49+
#define DECLARE_NAPI_METHOD(name, func) \
50+
{ name, func, 0, 0, 0, napi_default, 0 }
3351

3452
void Init(napi_env env, napi_value exports, napi_value module) {
35-
napi_set_property(env, exports,
36-
napi_property_name(env, "add"),
37-
napi_create_function(env, Add));
53+
napi_status status;
54+
napi_property_descriptor addDescriptor = DECLARE_NAPI_METHOD("add", Add);
55+
status = napi_define_properties(env, exports, 1, &addDescriptor);
56+
assert(status == napi_ok);
3857
}
3958

40-
NODE_MODULE_ABI(addon, Init)
59+
NAPI_MODULE(addon, Init)

3_callbacks/abi/addon.cc

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
1-
#include <node_jsvmapi.h>
1+
#include <node_api.h>
2+
#include <assert.h>
3+
4+
void RunCallback(napi_env env, const napi_callback_info info) {
5+
napi_status status;
26

3-
void RunCallback(napi_env env, const napi_func_cb_info info) {
47
napi_value args[1];
5-
napi_get_cb_args(env, info, args, 1);
8+
status = napi_get_cb_args(env, info, args, 1);
9+
assert(status == napi_ok);
10+
611
napi_value cb = args[0];
712

813
napi_value argv[1];
9-
argv[0] = napi_create_string(env, "hello world");
10-
napi_call_function(env, napi_get_global_scope(env) , cb, 1, argv);
14+
status = napi_create_string_utf8(env, "hello world", -1, argv);
15+
assert(status == napi_ok);
16+
17+
napi_value global;
18+
status = napi_get_global(env, &global);
19+
assert(status == napi_ok);
20+
21+
napi_value result;
22+
status = napi_call_function(env, global, cb, 1, argv, &result);
23+
assert(status == napi_ok);
1124
}
1225

26+
#define DECLARE_NAPI_METHOD(name, func) \
27+
{ name, func, 0, 0, 0, napi_default, 0 }
28+
1329
void Init(napi_env env, napi_value exports, napi_value module) {
14-
napi_set_property(env, module,
15-
napi_property_name(env, "exports"),
16-
napi_create_function(env, RunCallback));
30+
napi_status status;
31+
napi_property_descriptor desc = DECLARE_NAPI_METHOD("exports", RunCallback);
32+
status = napi_define_properties(env, module, 1, &desc);
33+
assert(status == napi_ok);
1734
}
1835

19-
NODE_MODULE_ABI(addon, Init)
36+
NAPI_MODULE(addon, Init)

3_callbacks/abi/addon.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ var addon = require('bindings')('addon');
22

33
addon(function(msg){
44
console.log(msg); // 'hello world'
5-
});
5+
});

4_object_factory/abi/addon.cc

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
1-
#include <node_jsvmapi.h>
1+
#include <node_api.h>
2+
#include <assert.h>
3+
4+
void CreateObject(napi_env env, const napi_callback_info info) {
5+
napi_status status;
26

3-
void napi_create_object(napi_env env, const napi_func_cb_info info) {
47
napi_value args[1];
5-
napi_get_cb_args(env, info, args, 1);
8+
status = napi_get_cb_args(env, info, args, 1);
9+
assert(status == napi_ok);
10+
11+
napi_value obj;
12+
status = napi_create_object(env, &obj);
13+
assert(status == napi_ok);
614

7-
napi_value obj = napi_create_object(env);
8-
napi_set_property(env, obj, napi_property_name(env, "msg"),
9-
args[0]);
15+
status = napi_set_named_property(env, obj, "msg", args[0]);
16+
assert(status == napi_ok);
1017

11-
napi_set_return_value(env, info, obj);
18+
status = napi_set_return_value(env, info, obj);
19+
assert(status == napi_ok);
1220
}
1321

22+
#define DECLARE_NAPI_METHOD(name, func) \
23+
{ name, func, 0, 0, 0, napi_default, 0 }
24+
1425
void Init(napi_env env, napi_value exports, napi_value module) {
15-
napi_set_property(env, module,
16-
napi_property_name(env, "exports"),
17-
napi_create_function(env, napi_create_object));
26+
napi_status status;
27+
napi_property_descriptor desc = DECLARE_NAPI_METHOD("exports", CreateObject);
28+
status = napi_define_properties(env, module, 1, &desc);
29+
assert(status == napi_ok);
1830
}
1931

20-
NODE_MODULE_ABI(addon, Init)
32+
NAPI_MODULE(addon, Init)

5_function_factory/abi/addon.cc

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
1-
#include <node_jsvmapi.h>
1+
#include <node_api.h>
2+
#include <assert.h>
23

3-
void MyFunction(napi_env env, napi_func_cb_info info) {
4-
napi_set_return_value(env, info, napi_create_string(env, "hello world"));
4+
void MyFunction(napi_env env, napi_callback_info info) {
5+
napi_status status;
6+
7+
napi_value str;
8+
status = napi_create_string_utf8(env, "hello world", -1, &str);
9+
assert(status == napi_ok);
10+
11+
status = napi_set_return_value(env, info, str);
12+
assert(status == napi_ok);
513
}
614

7-
void napi_create_function(napi_env env, napi_func_cb_info info) {
8-
napi_value fn = napi_create_function(env, MyFunction);
15+
void CreateFunction(napi_env env, napi_callback_info info) {
16+
napi_status status;
917

10-
// omit this to make it anonymous
11-
napi_set_function_name(env, fn, napi_property_name(env, "theFunction"));
18+
napi_value fn;
19+
status = napi_create_function(env, "theFunction", MyFunction, nullptr, &fn);
20+
assert(status == napi_ok);
1221

13-
napi_set_return_value(env, info, fn);
22+
status = napi_set_return_value(env, info, fn);
23+
assert(status == napi_ok);
1424
}
1525

26+
#define DECLARE_NAPI_METHOD(name, func) \
27+
{ name, func, 0, 0, 0, napi_default, 0 }
28+
1629
void Init(napi_env env, napi_value exports, napi_value module) {
17-
napi_set_property(env, module,
18-
napi_property_name(env, "exports"),
19-
napi_create_function(env, napi_create_function));
30+
napi_status status;
31+
napi_property_descriptor desc =
32+
DECLARE_NAPI_METHOD("exports", CreateFunction);
33+
status = napi_define_properties(env, module, 1, &desc);
34+
assert(status == napi_ok);
2035
}
2136

22-
NODE_MODULE_ABI(addon, Init)
37+
NAPI_MODULE(addon, Init)
2338

6_object_wrap/abi/addon.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ void Init(napi_env env, napi_value exports, napi_value module) {
44
MyObject::Init(env, exports);
55
}
66

7-
NODE_MODULE_ABI(addon, Init)
7+
NAPI_MODULE(addon, Init)

6_object_wrap/abi/addon.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ console.log( obj.plusOne() ); // 11
55
console.log( obj.plusOne() ); // 12
66
console.log( obj.plusOne() ); // 13
77

8-
console.log( obj.multiply().napi_value() ); // 13
9-
console.log( obj.multiply(10).napi_value() ); // 130
8+
console.log( obj.multiply().value ); // 13
9+
console.log( obj.multiply(10).value ); // 130
1010

1111
var newobj = obj.multiply(-1);
12-
console.log( newobj.napi_value() ); // -13
12+
console.log( newobj.value ); // -13
1313
console.log( obj === newobj ); // false

0 commit comments

Comments
 (0)