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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
#include <node_api.h>
static double value_ = 1;
napi_ref constructor_;
void GetValue(napi_env env, napi_callback_info info) {
napi_status status;
size_t argc;
status = napi_get_cb_args_length(env, info, &argc);
if (status != napi_ok) return;
if (argc != 0) {
napi_throw_type_error(env, "Wrong number of arguments");
return;
}
napi_value number;
status = napi_create_number(env, value_, &number);
if (status != napi_ok) return;
status = napi_set_return_value(env, info, number);
if (status != napi_ok) return;
}
void SetValue(napi_env env, napi_callback_info info) {
napi_status status;
size_t argc;
status = napi_get_cb_args_length(env, info, &argc);
if (status != napi_ok) return;
if (argc != 1) {
napi_throw_type_error(env, "Wrong number of arguments");
return;
}
napi_value arg;
status = napi_get_cb_args(env, info, &arg, 1);
if (status != napi_ok) return;
status = napi_get_value_double(env, arg, &value_);
if (status != napi_ok) return;
}
void Echo(napi_env env, napi_callback_info info) {
napi_status status;
size_t argc;
status = napi_get_cb_args_length(env, info, &argc);
if (status != napi_ok) return;
if (argc != 1) {
napi_throw_type_error(env, "Wrong number of arguments");
return;
}
napi_value arg;
status = napi_get_cb_args(env, info, &arg, 1);
if (status != napi_ok) return;
status = napi_set_return_value(env, info, arg);
if (status != napi_ok) return;
}
void New(napi_env env, napi_callback_info info) {
napi_status status;
napi_value jsthis;
status = napi_get_cb_this(env, info, &jsthis);
if (status != napi_ok) return;
status = napi_set_return_value(env, info, jsthis);
if (status != napi_ok) return;
}
void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
napi_status status;
napi_value number;
status = napi_create_number(env, value_, &number);
if (status != napi_ok) return;
napi_property_descriptor properties[] = {
{ "echo", Echo, 0, 0, 0, napi_enumerable, 0 },
{ "readwriteValue", 0, 0, 0, number, napi_enumerable | napi_writable, 0 },
{ "readonlyValue", 0, 0, 0, number, napi_enumerable, 0},
{ "hiddenValue", 0, 0, 0, number, napi_default, 0},
{ "readwriteAccessor1", 0, GetValue, SetValue, 0, napi_default, 0},
{ "readwriteAccessor2", 0, GetValue, SetValue, 0, napi_writable, 0},
{ "readonlyAccessor1", 0, GetValue, NULL, 0, napi_default, 0},
{ "readonlyAccessor2", 0, GetValue, NULL, 0, napi_writable, 0},
};
napi_value cons;
status = napi_define_class(env, "MyObject", New,
NULL, sizeof(properties)/sizeof(*properties), properties, &cons);
if (status != napi_ok) return;
status = napi_set_named_property(env, module, "exports", cons);
if (status != napi_ok) return;
status = napi_create_reference(env, cons, 1, &constructor_);
if (status != napi_ok) return;
}
NAPI_MODULE(addon, Init)
|