1 #include <js_native_api.h>
2 #include <string.h>
3 #include "../common.h"
4
TestGetElement(napi_env env,napi_callback_info info)5 static napi_value TestGetElement(napi_env env, napi_callback_info info) {
6 size_t argc = 2;
7 napi_value args[2];
8 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
9
10 NAPI_ASSERT(env, argc >= 2, "Wrong number of arguments");
11
12 napi_valuetype valuetype0;
13 NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
14
15 NAPI_ASSERT(env, valuetype0 == napi_object,
16 "Wrong type of arguments. Expects an array as first argument.");
17
18 napi_valuetype valuetype1;
19 NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
20
21 NAPI_ASSERT(env, valuetype1 == napi_number,
22 "Wrong type of arguments. Expects an integer as second argument.");
23
24 napi_value array = args[0];
25 int32_t index;
26 NAPI_CALL(env, napi_get_value_int32(env, args[1], &index));
27
28 NAPI_ASSERT(env, index >= 0, "Invalid index. Expects a positive integer.");
29
30 bool isarray;
31 NAPI_CALL(env, napi_is_array(env, array, &isarray));
32
33 if (!isarray) {
34 return NULL;
35 }
36
37 uint32_t length;
38 NAPI_CALL(env, napi_get_array_length(env, array, &length));
39
40 NAPI_ASSERT(env, ((uint32_t)index < length), "Index out of bounds!");
41
42 napi_value ret;
43 NAPI_CALL(env, napi_get_element(env, array, index, &ret));
44
45 return ret;
46 }
47
TestHasElement(napi_env env,napi_callback_info info)48 static napi_value TestHasElement(napi_env env, napi_callback_info info) {
49 size_t argc = 2;
50 napi_value args[2];
51 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
52
53 NAPI_ASSERT(env, argc >= 2, "Wrong number of arguments");
54
55 napi_valuetype valuetype0;
56 NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
57
58 NAPI_ASSERT(env, valuetype0 == napi_object,
59 "Wrong type of arguments. Expects an array as first argument.");
60
61 napi_valuetype valuetype1;
62 NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
63
64 NAPI_ASSERT(env, valuetype1 == napi_number,
65 "Wrong type of arguments. Expects an integer as second argument.");
66
67 napi_value array = args[0];
68 int32_t index;
69 NAPI_CALL(env, napi_get_value_int32(env, args[1], &index));
70
71 bool isarray;
72 NAPI_CALL(env, napi_is_array(env, array, &isarray));
73
74 if (!isarray) {
75 return NULL;
76 }
77
78 bool has_element;
79 NAPI_CALL(env, napi_has_element(env, array, index, &has_element));
80
81 napi_value ret;
82 NAPI_CALL(env, napi_get_boolean(env, has_element, &ret));
83
84 return ret;
85 }
86
TestDeleteElement(napi_env env,napi_callback_info info)87 static napi_value TestDeleteElement(napi_env env, napi_callback_info info) {
88 size_t argc = 2;
89 napi_value args[2];
90
91 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
92 NAPI_ASSERT(env, argc == 2, "Wrong number of arguments");
93
94 napi_valuetype valuetype0;
95 NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
96 NAPI_ASSERT(env, valuetype0 == napi_object,
97 "Wrong type of arguments. Expects an array as first argument.");
98
99 napi_valuetype valuetype1;
100 NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
101 NAPI_ASSERT(env, valuetype1 == napi_number,
102 "Wrong type of arguments. Expects an integer as second argument.");
103
104 napi_value array = args[0];
105 int32_t index;
106 bool result;
107 napi_value ret;
108
109 NAPI_CALL(env, napi_get_value_int32(env, args[1], &index));
110 NAPI_CALL(env, napi_is_array(env, array, &result));
111
112 if (!result) {
113 return NULL;
114 }
115
116 NAPI_CALL(env, napi_delete_element(env, array, index, &result));
117 NAPI_CALL(env, napi_get_boolean(env, result, &ret));
118
119 return ret;
120 }
121
New(napi_env env,napi_callback_info info)122 static napi_value New(napi_env env, napi_callback_info info) {
123 size_t argc = 1;
124 napi_value args[1];
125 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
126
127 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
128
129 napi_valuetype valuetype0;
130 NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
131
132 NAPI_ASSERT(env, valuetype0 == napi_object,
133 "Wrong type of arguments. Expects an array as first argument.");
134
135 napi_value ret;
136 NAPI_CALL(env, napi_create_array(env, &ret));
137
138 uint32_t i, length;
139 NAPI_CALL(env, napi_get_array_length(env, args[0], &length));
140
141 for (i = 0; i < length; i++) {
142 napi_value e;
143 NAPI_CALL(env, napi_get_element(env, args[0], i, &e));
144 NAPI_CALL(env, napi_set_element(env, ret, i, e));
145 }
146
147 return ret;
148 }
149
NewWithLength(napi_env env,napi_callback_info info)150 static napi_value NewWithLength(napi_env env, napi_callback_info info) {
151 size_t argc = 1;
152 napi_value args[1];
153 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
154
155 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
156
157 napi_valuetype valuetype0;
158 NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
159
160 NAPI_ASSERT(env, valuetype0 == napi_number,
161 "Wrong type of arguments. Expects an integer the first argument.");
162
163 int32_t array_length;
164 NAPI_CALL(env, napi_get_value_int32(env, args[0], &array_length));
165
166 napi_value ret;
167 NAPI_CALL(env, napi_create_array_with_length(env, array_length, &ret));
168
169 return ret;
170 }
171
172 EXTERN_C_START
Init(napi_env env,napi_value exports)173 napi_value Init(napi_env env, napi_value exports) {
174 napi_property_descriptor descriptors[] = {
175 DECLARE_NAPI_PROPERTY("TestGetElement", TestGetElement),
176 DECLARE_NAPI_PROPERTY("TestHasElement", TestHasElement),
177 DECLARE_NAPI_PROPERTY("TestDeleteElement", TestDeleteElement),
178 DECLARE_NAPI_PROPERTY("New", New),
179 DECLARE_NAPI_PROPERTY("NewWithLength", NewWithLength),
180 };
181
182 NAPI_CALL(env, napi_define_properties(
183 env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
184
185 return exports;
186 }
187 EXTERN_C_END
188