1 #include <js_native_api.h>
2 #include "../common.h"
3
4 static double value_ = 1;
5 static double static_value_ = 10;
6
TestDefineClass(napi_env env,napi_callback_info info)7 static napi_value TestDefineClass(napi_env env,
8 napi_callback_info info) {
9 napi_status status;
10 napi_value result, return_value;
11
12 napi_property_descriptor property_descriptor = {
13 "TestDefineClass",
14 NULL,
15 TestDefineClass,
16 NULL,
17 NULL,
18 NULL,
19 napi_enumerable | napi_static,
20 NULL};
21
22 NAPI_CALL(env, napi_create_object(env, &return_value));
23
24 status = napi_define_class(NULL,
25 "TrackedFunction",
26 NAPI_AUTO_LENGTH,
27 TestDefineClass,
28 NULL,
29 1,
30 &property_descriptor,
31 &result);
32
33 add_returned_status(env,
34 "envIsNull",
35 return_value,
36 "Invalid argument",
37 napi_invalid_arg,
38 status);
39
40 napi_define_class(env,
41 NULL,
42 NAPI_AUTO_LENGTH,
43 TestDefineClass,
44 NULL,
45 1,
46 &property_descriptor,
47 &result);
48
49 add_last_status(env, "nameIsNull", return_value);
50
51 napi_define_class(env,
52 "TrackedFunction",
53 NAPI_AUTO_LENGTH,
54 NULL,
55 NULL,
56 1,
57 &property_descriptor,
58 &result);
59
60 add_last_status(env, "cbIsNull", return_value);
61
62 napi_define_class(env,
63 "TrackedFunction",
64 NAPI_AUTO_LENGTH,
65 TestDefineClass,
66 NULL,
67 1,
68 &property_descriptor,
69 &result);
70
71 add_last_status(env, "cbDataIsNull", return_value);
72
73 napi_define_class(env,
74 "TrackedFunction",
75 NAPI_AUTO_LENGTH,
76 TestDefineClass,
77 NULL,
78 1,
79 NULL,
80 &result);
81
82 add_last_status(env, "propertiesIsNull", return_value);
83
84
85 napi_define_class(env,
86 "TrackedFunction",
87 NAPI_AUTO_LENGTH,
88 TestDefineClass,
89 NULL,
90 1,
91 &property_descriptor,
92 NULL);
93
94 add_last_status(env, "resultIsNull", return_value);
95
96 return return_value;
97 }
98
GetValue(napi_env env,napi_callback_info info)99 static napi_value GetValue(napi_env env, napi_callback_info info) {
100 size_t argc = 0;
101 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, NULL, NULL, NULL));
102
103 NAPI_ASSERT(env, argc == 0, "Wrong number of arguments");
104
105 napi_value number;
106 NAPI_CALL(env, napi_create_double(env, value_, &number));
107
108 return number;
109 }
110
SetValue(napi_env env,napi_callback_info info)111 static napi_value SetValue(napi_env env, napi_callback_info info) {
112 size_t argc = 1;
113 napi_value args[1];
114 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
115
116 NAPI_ASSERT(env, argc == 1, "Wrong number of arguments");
117
118 NAPI_CALL(env, napi_get_value_double(env, args[0], &value_));
119
120 return NULL;
121 }
122
Echo(napi_env env,napi_callback_info info)123 static napi_value Echo(napi_env env, napi_callback_info info) {
124 size_t argc = 1;
125 napi_value args[1];
126 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
127
128 NAPI_ASSERT(env, argc == 1, "Wrong number of arguments");
129
130 return args[0];
131 }
132
New(napi_env env,napi_callback_info info)133 static napi_value New(napi_env env, napi_callback_info info) {
134 napi_value _this;
135 NAPI_CALL(env, napi_get_cb_info(env, info, NULL, NULL, &_this, NULL));
136
137 return _this;
138 }
139
GetStaticValue(napi_env env,napi_callback_info info)140 static napi_value GetStaticValue(napi_env env, napi_callback_info info) {
141 size_t argc = 0;
142 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, NULL, NULL, NULL));
143
144 NAPI_ASSERT(env, argc == 0, "Wrong number of arguments");
145
146 napi_value number;
147 NAPI_CALL(env, napi_create_double(env, static_value_, &number));
148
149 return number;
150 }
151
152
NewExtra(napi_env env,napi_callback_info info)153 static napi_value NewExtra(napi_env env, napi_callback_info info) {
154 napi_value _this;
155 NAPI_CALL(env, napi_get_cb_info(env, info, NULL, NULL, &_this, NULL));
156
157 return _this;
158 }
159
160 EXTERN_C_START
Init(napi_env env,napi_value exports)161 napi_value Init(napi_env env, napi_value exports) {
162 napi_value number, cons;
163 NAPI_CALL(env, napi_create_double(env, value_, &number));
164
165 NAPI_CALL(env, napi_define_class(
166 env, "MyObject_Extra", 8, NewExtra, NULL, 0, NULL, &cons));
167
168 napi_property_descriptor properties[] = {
169 { "echo", NULL, Echo, NULL, NULL, NULL, napi_enumerable, NULL },
170 { "readwriteValue", NULL, NULL, NULL, NULL, number,
171 napi_enumerable | napi_writable, NULL },
172 { "readonlyValue", NULL, NULL, NULL, NULL, number, napi_enumerable,
173 NULL },
174 { "hiddenValue", NULL, NULL, NULL, NULL, number, napi_default, NULL },
175 { "readwriteAccessor1", NULL, NULL, GetValue, SetValue, NULL, napi_default,
176 NULL },
177 { "readwriteAccessor2", NULL, NULL, GetValue, SetValue, NULL,
178 napi_writable, NULL },
179 { "readonlyAccessor1", NULL, NULL, GetValue, NULL, NULL, napi_default,
180 NULL },
181 { "readonlyAccessor2", NULL, NULL, GetValue, NULL, NULL, napi_writable,
182 NULL },
183 { "staticReadonlyAccessor1", NULL, NULL, GetStaticValue, NULL, NULL,
184 napi_default | napi_static, NULL},
185 { "constructorName", NULL, NULL, NULL, NULL, cons,
186 napi_enumerable | napi_static, NULL },
187 { "TestDefineClass", NULL, TestDefineClass, NULL, NULL, NULL,
188 napi_enumerable | napi_static, NULL },
189 };
190
191 NAPI_CALL(env, napi_define_class(env, "MyObject", NAPI_AUTO_LENGTH, New,
192 NULL, sizeof(properties)/sizeof(*properties), properties, &cons));
193
194 return cons;
195 }
196 EXTERN_C_END
197