-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
/
Copy pathtest_constructor.c
200 lines (156 loc) Β· 5.77 KB
/
test_constructor.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
#include "test_null.h"
static double value_ = 1;
static double static_value_ = 10;
static napi_value TestDefineClass(napi_env env,
napi_callback_info info) {
napi_status status;
napi_value result, return_value;
napi_property_descriptor property_descriptor = {
"TestDefineClass",
NULL,
TestDefineClass,
NULL,
NULL,
NULL,
napi_enumerable | napi_static,
NULL};
NODE_API_CALL(env, napi_create_object(env, &return_value));
status = napi_define_class(NULL,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
&property_descriptor,
&result);
add_returned_status(env,
"envIsNull",
return_value,
"Invalid argument",
napi_invalid_arg,
status);
napi_define_class(env,
NULL,
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
&property_descriptor,
&result);
add_last_status(env, "nameIsNull", return_value);
napi_define_class(env,
"TrackedFunction",
NAPI_AUTO_LENGTH,
NULL,
NULL,
1,
&property_descriptor,
&result);
add_last_status(env, "cbIsNull", return_value);
napi_define_class(env,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
&property_descriptor,
&result);
add_last_status(env, "cbDataIsNull", return_value);
napi_define_class(env,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
NULL,
&result);
add_last_status(env, "propertiesIsNull", return_value);
napi_define_class(env,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
&property_descriptor,
NULL);
add_last_status(env, "resultIsNull", return_value);
return return_value;
}
static napi_value GetValue(napi_env env, napi_callback_info info) {
size_t argc = 0;
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, NULL, NULL, NULL));
NODE_API_ASSERT(env, argc == 0, "Wrong number of arguments");
napi_value number;
NODE_API_CALL(env, napi_create_double(env, value_, &number));
return number;
}
static napi_value SetValue(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NODE_API_ASSERT(env, argc == 1, "Wrong number of arguments");
NODE_API_CALL(env, napi_get_value_double(env, args[0], &value_));
return NULL;
}
static napi_value Echo(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NODE_API_ASSERT(env, argc == 1, "Wrong number of arguments");
return args[0];
}
static napi_value New(napi_env env, napi_callback_info info) {
napi_value _this;
NODE_API_CALL(env, napi_get_cb_info(env, info, NULL, NULL, &_this, NULL));
return _this;
}
static napi_value GetStaticValue(napi_env env, napi_callback_info info) {
size_t argc = 0;
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, NULL, NULL, NULL));
NODE_API_ASSERT(env, argc == 0, "Wrong number of arguments");
napi_value number;
NODE_API_CALL(env, napi_create_double(env, static_value_, &number));
return number;
}
static napi_value NewExtra(napi_env env, napi_callback_info info) {
napi_value _this;
NODE_API_CALL(env, napi_get_cb_info(env, info, NULL, NULL, &_this, NULL));
return _this;
}
EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_value number, cons;
NODE_API_CALL(env, napi_create_double(env, value_, &number));
NODE_API_CALL(env, napi_define_class(
env, "MyObject_Extra", 8, NewExtra, NULL, 0, NULL, &cons));
napi_property_descriptor properties[] = {
{ "echo", NULL, Echo, NULL, NULL, NULL, napi_enumerable, NULL },
{ "readwriteValue", NULL, NULL, NULL, NULL, number,
napi_enumerable | napi_writable, NULL },
{ "readonlyValue", NULL, NULL, NULL, NULL, number, napi_enumerable,
NULL },
{ "hiddenValue", NULL, NULL, NULL, NULL, number, napi_default, NULL },
{ "readwriteAccessor1", NULL, NULL, GetValue, SetValue, NULL, napi_default,
NULL },
{ "readwriteAccessor2", NULL, NULL, GetValue, SetValue, NULL,
napi_writable, NULL },
{ "readonlyAccessor1", NULL, NULL, GetValue, NULL, NULL, napi_default,
NULL },
{ "readonlyAccessor2", NULL, NULL, GetValue, NULL, NULL, napi_writable,
NULL },
{ "staticReadonlyAccessor1", NULL, NULL, GetStaticValue, NULL, NULL,
napi_default | napi_static, NULL},
{ "constructorName", NULL, NULL, NULL, NULL, cons,
napi_enumerable | napi_static, NULL },
{ "TestDefineClass", NULL, TestDefineClass, NULL, NULL, NULL,
napi_enumerable | napi_static, NULL },
};
NODE_API_CALL(env, napi_define_class(env, "MyObject", NAPI_AUTO_LENGTH, New,
NULL, sizeof(properties)/sizeof(*properties), properties, &cons));
init_test_null(env, cons);
return cons;
}
EXTERN_C_END