1 #define NAPI_VERSION 9
2 #include <js_native_api.h>
3 #include "../common.h"
4 #include "../entry_point.h"
5
checkError(napi_env env,napi_callback_info info)6 static napi_value checkError(napi_env env, napi_callback_info info) {
7 size_t argc = 1;
8 napi_value args[1];
9 NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
10
11 bool r;
12 NODE_API_CALL(env, napi_is_error(env, args[0], &r));
13
14 napi_value result;
15 NODE_API_CALL(env, napi_get_boolean(env, r, &result));
16
17 return result;
18 }
19
throwExistingError(napi_env env,napi_callback_info info)20 static napi_value throwExistingError(napi_env env, napi_callback_info info) {
21 napi_value message;
22 napi_value error;
23 NODE_API_CALL(env, napi_create_string_utf8(
24 env, "existing error", NAPI_AUTO_LENGTH, &message));
25 NODE_API_CALL(env, napi_create_error(env, NULL, message, &error));
26 NODE_API_CALL(env, napi_throw(env, error));
27 return NULL;
28 }
29
throwError(napi_env env,napi_callback_info info)30 static napi_value throwError(napi_env env, napi_callback_info info) {
31 NODE_API_CALL(env, napi_throw_error(env, NULL, "error"));
32 return NULL;
33 }
34
throwRangeError(napi_env env,napi_callback_info info)35 static napi_value throwRangeError(napi_env env, napi_callback_info info) {
36 NODE_API_CALL(env, napi_throw_range_error(env, NULL, "range error"));
37 return NULL;
38 }
39
throwTypeError(napi_env env,napi_callback_info info)40 static napi_value throwTypeError(napi_env env, napi_callback_info info) {
41 NODE_API_CALL(env, napi_throw_type_error(env, NULL, "type error"));
42 return NULL;
43 }
44
throwSyntaxError(napi_env env,napi_callback_info info)45 static napi_value throwSyntaxError(napi_env env, napi_callback_info info) {
46 NODE_API_CALL(env, node_api_throw_syntax_error(env, NULL, "syntax error"));
47 return NULL;
48 }
49
throwErrorCode(napi_env env,napi_callback_info info)50 static napi_value throwErrorCode(napi_env env, napi_callback_info info) {
51 NODE_API_CALL(env, napi_throw_error(env, "ERR_TEST_CODE", "Error [error]"));
52 return NULL;
53 }
54
throwRangeErrorCode(napi_env env,napi_callback_info info)55 static napi_value throwRangeErrorCode(napi_env env, napi_callback_info info) {
56 NODE_API_CALL(env,
57 napi_throw_range_error(env, "ERR_TEST_CODE", "RangeError [range error]"));
58 return NULL;
59 }
60
throwTypeErrorCode(napi_env env,napi_callback_info info)61 static napi_value throwTypeErrorCode(napi_env env, napi_callback_info info) {
62 NODE_API_CALL(env,
63 napi_throw_type_error(env, "ERR_TEST_CODE", "TypeError [type error]"));
64 return NULL;
65 }
66
throwSyntaxErrorCode(napi_env env,napi_callback_info info)67 static napi_value throwSyntaxErrorCode(napi_env env, napi_callback_info info) {
68 NODE_API_CALL(env,
69 node_api_throw_syntax_error(env, "ERR_TEST_CODE", "SyntaxError [syntax error]"));
70 return NULL;
71 }
72
createError(napi_env env,napi_callback_info info)73 static napi_value createError(napi_env env, napi_callback_info info) {
74 napi_value result;
75 napi_value message;
76 NODE_API_CALL(env, napi_create_string_utf8(
77 env, "error", NAPI_AUTO_LENGTH, &message));
78 NODE_API_CALL(env, napi_create_error(env, NULL, message, &result));
79 return result;
80 }
81
createRangeError(napi_env env,napi_callback_info info)82 static napi_value createRangeError(napi_env env, napi_callback_info info) {
83 napi_value result;
84 napi_value message;
85 NODE_API_CALL(env, napi_create_string_utf8(
86 env, "range error", NAPI_AUTO_LENGTH, &message));
87 NODE_API_CALL(env, napi_create_range_error(env, NULL, message, &result));
88 return result;
89 }
90
createTypeError(napi_env env,napi_callback_info info)91 static napi_value createTypeError(napi_env env, napi_callback_info info) {
92 napi_value result;
93 napi_value message;
94 NODE_API_CALL(env, napi_create_string_utf8(
95 env, "type error", NAPI_AUTO_LENGTH, &message));
96 NODE_API_CALL(env, napi_create_type_error(env, NULL, message, &result));
97 return result;
98 }
99
createSyntaxError(napi_env env,napi_callback_info info)100 static napi_value createSyntaxError(napi_env env, napi_callback_info info) {
101 napi_value result;
102 napi_value message;
103 NODE_API_CALL(env, napi_create_string_utf8(
104 env, "syntax error", NAPI_AUTO_LENGTH, &message));
105 NODE_API_CALL(env, node_api_create_syntax_error(env, NULL, message, &result));
106 return result;
107 }
108
createErrorCode(napi_env env,napi_callback_info info)109 static napi_value createErrorCode(napi_env env, napi_callback_info info) {
110 napi_value result;
111 napi_value message;
112 napi_value code;
113 NODE_API_CALL(env, napi_create_string_utf8(
114 env, "Error [error]", NAPI_AUTO_LENGTH, &message));
115 NODE_API_CALL(env, napi_create_string_utf8(
116 env, "ERR_TEST_CODE", NAPI_AUTO_LENGTH, &code));
117 NODE_API_CALL(env, napi_create_error(env, code, message, &result));
118 return result;
119 }
120
createRangeErrorCode(napi_env env,napi_callback_info info)121 static napi_value createRangeErrorCode(napi_env env, napi_callback_info info) {
122 napi_value result;
123 napi_value message;
124 napi_value code;
125 NODE_API_CALL(env,
126 napi_create_string_utf8(
127 env, "RangeError [range error]", NAPI_AUTO_LENGTH, &message));
128 NODE_API_CALL(env, napi_create_string_utf8(
129 env, "ERR_TEST_CODE", NAPI_AUTO_LENGTH, &code));
130 NODE_API_CALL(env, napi_create_range_error(env, code, message, &result));
131 return result;
132 }
133
createTypeErrorCode(napi_env env,napi_callback_info info)134 static napi_value createTypeErrorCode(napi_env env, napi_callback_info info) {
135 napi_value result;
136 napi_value message;
137 napi_value code;
138 NODE_API_CALL(env,
139 napi_create_string_utf8(
140 env, "TypeError [type error]", NAPI_AUTO_LENGTH, &message));
141 NODE_API_CALL(env, napi_create_string_utf8(
142 env, "ERR_TEST_CODE", NAPI_AUTO_LENGTH, &code));
143 NODE_API_CALL(env, napi_create_type_error(env, code, message, &result));
144 return result;
145 }
146
createSyntaxErrorCode(napi_env env,napi_callback_info info)147 static napi_value createSyntaxErrorCode(napi_env env, napi_callback_info info) {
148 napi_value result;
149 napi_value message;
150 napi_value code;
151 NODE_API_CALL(env,
152 napi_create_string_utf8(
153 env, "SyntaxError [syntax error]", NAPI_AUTO_LENGTH, &message));
154 NODE_API_CALL(env, napi_create_string_utf8(
155 env, "ERR_TEST_CODE", NAPI_AUTO_LENGTH, &code));
156 NODE_API_CALL(env, node_api_create_syntax_error(env, code, message, &result));
157 return result;
158 }
159
throwArbitrary(napi_env env,napi_callback_info info)160 static napi_value throwArbitrary(napi_env env, napi_callback_info info) {
161 napi_value arbitrary;
162 size_t argc = 1;
163 NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, &arbitrary, NULL, NULL));
164 NODE_API_CALL(env, napi_throw(env, arbitrary));
165 return NULL;
166 }
167
168 EXTERN_C_START
Init(napi_env env,napi_value exports)169 napi_value Init(napi_env env, napi_value exports) {
170 napi_property_descriptor descriptors[] = {
171 DECLARE_NODE_API_PROPERTY("checkError", checkError),
172 DECLARE_NODE_API_PROPERTY("throwExistingError", throwExistingError),
173 DECLARE_NODE_API_PROPERTY("throwError", throwError),
174 DECLARE_NODE_API_PROPERTY("throwRangeError", throwRangeError),
175 DECLARE_NODE_API_PROPERTY("throwTypeError", throwTypeError),
176 DECLARE_NODE_API_PROPERTY("throwSyntaxError", throwSyntaxError),
177 DECLARE_NODE_API_PROPERTY("throwErrorCode", throwErrorCode),
178 DECLARE_NODE_API_PROPERTY("throwRangeErrorCode", throwRangeErrorCode),
179 DECLARE_NODE_API_PROPERTY("throwTypeErrorCode", throwTypeErrorCode),
180 DECLARE_NODE_API_PROPERTY("throwSyntaxErrorCode", throwSyntaxErrorCode),
181 DECLARE_NODE_API_PROPERTY("throwArbitrary", throwArbitrary),
182 DECLARE_NODE_API_PROPERTY("createError", createError),
183 DECLARE_NODE_API_PROPERTY("createRangeError", createRangeError),
184 DECLARE_NODE_API_PROPERTY("createTypeError", createTypeError),
185 DECLARE_NODE_API_PROPERTY("createSyntaxError", createSyntaxError),
186 DECLARE_NODE_API_PROPERTY("createErrorCode", createErrorCode),
187 DECLARE_NODE_API_PROPERTY("createRangeErrorCode", createRangeErrorCode),
188 DECLARE_NODE_API_PROPERTY("createTypeErrorCode", createTypeErrorCode),
189 DECLARE_NODE_API_PROPERTY("createSyntaxErrorCode", createSyntaxErrorCode),
190 };
191
192 NODE_API_CALL(env, napi_define_properties(
193 env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
194
195 return exports;
196 }
197 EXTERN_C_END
198