-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.c
51 lines (41 loc) · 1.34 KB
/
example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <node_api.h>
napi_value add(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
napi_get_cb_info(env, info, &argc, args, NULL, NULL);
double a;
napi_get_value_double(env, args[0], &a);
double b;
napi_get_value_double(env, args[1], &b);
napi_value sum;
double result = a + b;
napi_create_double(env, result, &sum);
return sum;
}
napi_value add_callback(napi_env env, napi_callback_info info) {
size_t argc = 3;
napi_value args[3];
napi_get_cb_info(env, info, &argc, args, NULL, NULL);
double a;
napi_get_value_double(env, args[0], &a);
double b;
napi_get_value_double(env, args[1], &b);
napi_value cb_args[2];
double result = a + b;
napi_get_null(env, &cb_args[0]);
napi_create_double(env, result, &cb_args[1]);
napi_value js_this, cb_result;
napi_get_null(env, &js_this);
napi_call_function(env, js_this, args[2], 2, cb_args, &cb_result);
return cb_result;
}
napi_value init(napi_env env, napi_value exports) {
napi_value fn_add;
napi_create_function(env, 0, 0, add, 0, &fn_add);
napi_set_named_property(env, exports, "add", fn_add);
napi_value fn_add_callback;
napi_create_function(env, 0, 0, add_callback, 0, &fn_add_callback);
napi_set_named_property(env, exports, "addCallback", fn_add_callback);
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, init)