1 /* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef JERRYX_ARG_IMPL_H
17 #define JERRYX_ARG_IMPL_H
18
19 /* transform functions for each type. */
20
21 #define JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL(type) \
22 jerry_value_t jerryx_arg_transform_ ## type (jerryx_arg_js_iterator_t *js_arg_iter_p, \
23 const jerryx_arg_t *c_arg_p); \
24 jerry_value_t jerryx_arg_transform_ ## type ## _optional (jerryx_arg_js_iterator_t *js_arg_iter_p, \
25 const jerryx_arg_t *c_arg_p);
26
27 #define JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT(type) \
28 JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL (type) \
29 JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL (type ## _strict)
30
31 JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (uint8)
32 JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (int8)
33 JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (uint16)
34 JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (int16)
35 JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (uint32)
36 JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (int32)
37 JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (number)
38 JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (string)
39 JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (utf8_string)
40 JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (boolean)
41
42 JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL (function)
43 JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL (native_pointer)
44 JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL (object_props)
45 JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL (array_items)
46
47 jerry_value_t jerryx_arg_transform_ignore (jerryx_arg_js_iterator_t *js_arg_iter_p,
48 const jerryx_arg_t *c_arg_p);
49
50 #undef JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL
51 #undef JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT
52
53 /**
54 * The structure indicates the options used to transform integer argument.
55 * It will be passed into jerryx_arg_t's extra_info field.
56 */
57 typedef struct
58 {
59 uint8_t round; /**< rounding policy */
60 uint8_t clamp; /**< clamping policy */
61 } jerryx_arg_int_option_t;
62
63 /**
64 * The macro used to generate jerryx_arg_xxx for int type.
65 */
66 #define JERRYX_ARG_INT(type) \
67 static inline jerryx_arg_t \
68 jerryx_arg_ ## type (type ## _t *dest, \
69 jerryx_arg_round_t round_flag, \
70 jerryx_arg_clamp_t clamp_flag, \
71 jerryx_arg_coerce_t coerce_flag, \
72 jerryx_arg_optional_t opt_flag) \
73 { \
74 jerryx_arg_transform_func_t func; \
75 if (coerce_flag == JERRYX_ARG_NO_COERCE) \
76 { \
77 if (opt_flag == JERRYX_ARG_OPTIONAL) \
78 { \
79 func = jerryx_arg_transform_ ## type ## _strict_optional; \
80 } \
81 else \
82 { \
83 func = jerryx_arg_transform_ ## type ## _strict; \
84 } \
85 } \
86 else \
87 { \
88 if (opt_flag == JERRYX_ARG_OPTIONAL) \
89 { \
90 func = jerryx_arg_transform_ ## type ## _optional; \
91 } \
92 else \
93 { \
94 func = jerryx_arg_transform_ ## type; \
95 } \
96 } \
97 union \
98 { \
99 jerryx_arg_int_option_t int_option; \
100 uintptr_t extra_info; \
101 } u = { .int_option = { .round = (uint8_t) round_flag, .clamp = (uint8_t) clamp_flag } }; \
102 return (jerryx_arg_t) \
103 { \
104 .func = func, \
105 .dest = (void *) dest, \
106 .extra_info = u.extra_info \
107 }; \
108 }
109
110 JERRYX_ARG_INT (uint8)
JERRYX_ARG_INT(int8)111 JERRYX_ARG_INT (int8)
112 JERRYX_ARG_INT (uint16)
113 JERRYX_ARG_INT (int16)
114 JERRYX_ARG_INT (uint32)
115 JERRYX_ARG_INT (int32)
116
117 #undef JERRYX_ARG_INT
118
119 /**
120 * Create a validation/transformation step (`jerryx_arg_t`) that expects to
121 * consume one `number` JS argument and stores it into a C `double`.
122 *
123 * @return a jerryx_arg_t instance.
124 */
125 static inline jerryx_arg_t
126 jerryx_arg_number (double *dest, /**< pointer to the double where the result should be stored */
127 jerryx_arg_coerce_t coerce_flag, /**< whether type coercion is allowed */
128 jerryx_arg_optional_t opt_flag) /**< whether the argument is optional */
129 {
130 jerryx_arg_transform_func_t func;
131
132 if (coerce_flag == JERRYX_ARG_NO_COERCE)
133 {
134 if (opt_flag == JERRYX_ARG_OPTIONAL)
135 {
136 func = jerryx_arg_transform_number_strict_optional;
137 }
138 else
139 {
140 func = jerryx_arg_transform_number_strict;
141 }
142 }
143 else
144 {
145 if (opt_flag == JERRYX_ARG_OPTIONAL)
146 {
147 func = jerryx_arg_transform_number_optional;
148 }
149 else
150 {
151 func = jerryx_arg_transform_number;
152 }
153 }
154
155 return (jerryx_arg_t)
156 {
157 .func = func,
158 .dest = (void *) dest
159 };
160 } /* jerryx_arg_number */
161
162 /**
163 * Create a validation/transformation step (`jerryx_arg_t`) that expects to
164 * consume one `boolean` JS argument and stores it into a C `bool`.
165 *
166 * @return a jerryx_arg_t instance.
167 */
168 static inline jerryx_arg_t
jerryx_arg_boolean(bool * dest,jerryx_arg_coerce_t coerce_flag,jerryx_arg_optional_t opt_flag)169 jerryx_arg_boolean (bool *dest, /**< points to the native bool */
170 jerryx_arg_coerce_t coerce_flag, /**< whether type coercion is allowed */
171 jerryx_arg_optional_t opt_flag) /**< whether the argument is optional */
172 {
173 jerryx_arg_transform_func_t func;
174
175 if (coerce_flag == JERRYX_ARG_NO_COERCE)
176 {
177 if (opt_flag == JERRYX_ARG_OPTIONAL)
178 {
179 func = jerryx_arg_transform_boolean_strict_optional;
180 }
181 else
182 {
183 func = jerryx_arg_transform_boolean_strict;
184 }
185 }
186 else
187 {
188 if (opt_flag == JERRYX_ARG_OPTIONAL)
189 {
190 func = jerryx_arg_transform_boolean_optional;
191 }
192 else
193 {
194 func = jerryx_arg_transform_boolean;
195 }
196 }
197
198 return (jerryx_arg_t)
199 {
200 .func = func,
201 .dest = (void *) dest
202 };
203 } /* jerryx_arg_boolean */
204
205 /**
206 * Create a validation/transformation step (`jerryx_arg_t`) that expects to
207 * consume one `string` JS argument and stores it into a C `char` array.
208 *
209 * @return a jerryx_arg_t instance.
210 */
211 static inline jerryx_arg_t
jerryx_arg_string(char * dest,uint32_t size,jerryx_arg_coerce_t coerce_flag,jerryx_arg_optional_t opt_flag)212 jerryx_arg_string (char *dest, /**< pointer to the native char array where the result should be stored */
213 uint32_t size, /**< the size of native char array */
214 jerryx_arg_coerce_t coerce_flag, /**< whether type coercion is allowed */
215 jerryx_arg_optional_t opt_flag) /**< whether the argument is optional */
216 {
217 jerryx_arg_transform_func_t func;
218
219 if (coerce_flag == JERRYX_ARG_NO_COERCE)
220 {
221 if (opt_flag == JERRYX_ARG_OPTIONAL)
222 {
223 func = jerryx_arg_transform_string_strict_optional;
224 }
225 else
226 {
227 func = jerryx_arg_transform_string_strict;
228 }
229 }
230 else
231 {
232 if (opt_flag == JERRYX_ARG_OPTIONAL)
233 {
234 func = jerryx_arg_transform_string_optional;
235 }
236 else
237 {
238 func = jerryx_arg_transform_string;
239 }
240 }
241
242 return (jerryx_arg_t)
243 {
244 .func = func,
245 .dest = (void *) dest,
246 .extra_info = (uintptr_t) size
247 };
248 } /* jerryx_arg_string */
249
250 /**
251 * Create a validation/transformation step (`jerryx_arg_t`) that expects to
252 * consume one `string` JS argument and stores it into a C utf8 `char` array.
253 *
254 * @return a jerryx_arg_t instance.
255 */
256 static inline jerryx_arg_t
jerryx_arg_utf8_string(char * dest,uint32_t size,jerryx_arg_coerce_t coerce_flag,jerryx_arg_optional_t opt_flag)257 jerryx_arg_utf8_string (char *dest, /**< [out] pointer to the native char array where the result should be stored */
258 uint32_t size, /**< the size of native char array */
259 jerryx_arg_coerce_t coerce_flag, /**< whether type coercion is allowed */
260 jerryx_arg_optional_t opt_flag) /**< whether the argument is optional */
261 {
262 jerryx_arg_transform_func_t func;
263
264 if (coerce_flag == JERRYX_ARG_NO_COERCE)
265 {
266 if (opt_flag == JERRYX_ARG_OPTIONAL)
267 {
268 func = jerryx_arg_transform_utf8_string_strict_optional;
269 }
270 else
271 {
272 func = jerryx_arg_transform_utf8_string_strict;
273 }
274 }
275 else
276 {
277 if (opt_flag == JERRYX_ARG_OPTIONAL)
278 {
279 func = jerryx_arg_transform_utf8_string_optional;
280 }
281 else
282 {
283 func = jerryx_arg_transform_utf8_string;
284 }
285 }
286
287 return (jerryx_arg_t)
288 {
289 .func = func,
290 .dest = (void *) dest,
291 .extra_info = (uintptr_t) size
292 };
293 } /* jerryx_arg_utf8_string */
294
295 /**
296 * Create a validation/transformation step (`jerryx_arg_t`) that expects to
297 * consume one `function` JS argument and stores it into a C `jerry_value_t`.
298 *
299 * @return a jerryx_arg_t instance.
300 */
301 static inline jerryx_arg_t
jerryx_arg_function(jerry_value_t * dest,jerryx_arg_optional_t opt_flag)302 jerryx_arg_function (jerry_value_t *dest, /**< pointer to the jerry_value_t where the result should be stored */
303 jerryx_arg_optional_t opt_flag) /**< whether the argument is optional */
304 {
305 jerryx_arg_transform_func_t func;
306
307 if (opt_flag == JERRYX_ARG_OPTIONAL)
308 {
309 func = jerryx_arg_transform_function_optional;
310 }
311 else
312 {
313 func = jerryx_arg_transform_function;
314 }
315
316 return (jerryx_arg_t)
317 {
318 .func = func,
319 .dest = (void *) dest
320 };
321 } /* jerryx_arg_function */
322
323 /**
324 * Create a validation/transformation step (`jerryx_arg_t`) that expects to
325 * consume one `object` JS argument that is 'backed' with a native pointer with
326 * a given type info. In case the native pointer info matches, the transform
327 * will succeed and the object's native pointer will be assigned to *dest.
328 *
329 * @return a jerryx_arg_t instance.
330 */
331 static inline jerryx_arg_t
jerryx_arg_native_pointer(void ** dest,const jerry_object_native_info_t * info_p,jerryx_arg_optional_t opt_flag)332 jerryx_arg_native_pointer (void **dest, /**< pointer to where the resulting native pointer should be stored */
333 const jerry_object_native_info_t *info_p, /**< expected the type info */
334 jerryx_arg_optional_t opt_flag) /**< whether the argument is optional */
335 {
336 jerryx_arg_transform_func_t func;
337
338 if (opt_flag == JERRYX_ARG_OPTIONAL)
339 {
340 func = jerryx_arg_transform_native_pointer_optional;
341 }
342 else
343 {
344 func = jerryx_arg_transform_native_pointer;
345 }
346
347 return (jerryx_arg_t)
348 {
349 .func = func,
350 .dest = (void *) dest,
351 .extra_info = (uintptr_t) info_p
352 };
353 } /* jerryx_arg_native_pointer */
354
355 /**
356 * Create a jerryx_arg_t instance for ignored argument.
357 *
358 * @return a jerryx_arg_t instance.
359 */
360 static inline jerryx_arg_t
jerryx_arg_ignore(void)361 jerryx_arg_ignore (void)
362 {
363 return (jerryx_arg_t)
364 {
365 .func = jerryx_arg_transform_ignore
366 };
367 } /* jerryx_arg_ignore */
368
369 /**
370 * Create a jerryx_arg_t instance with custom transform.
371 *
372 * @return a jerryx_arg_t instance.
373 */
374 static inline jerryx_arg_t
jerryx_arg_custom(void * dest,uintptr_t extra_info,jerryx_arg_transform_func_t func)375 jerryx_arg_custom (void *dest, /**< pointer to the native argument where the result should be stored */
376 uintptr_t extra_info, /**< the extra parameter, specific to the transform function */
377 jerryx_arg_transform_func_t func) /**< the custom transform function */
378 {
379 return (jerryx_arg_t)
380 {
381 .func = func,
382 .dest = dest,
383 .extra_info = extra_info
384 };
385 } /* jerryx_arg_custom */
386
387 /**
388 * Create a jerryx_arg_t instance for object properties.
389 *
390 * @return a jerryx_arg_t instance.
391 */
392 static inline jerryx_arg_t
jerryx_arg_object_properties(const jerryx_arg_object_props_t * object_props,jerryx_arg_optional_t opt_flag)393 jerryx_arg_object_properties (const jerryx_arg_object_props_t *object_props, /**< pointer to object property mapping */
394 jerryx_arg_optional_t opt_flag) /**< whether the argument is optional */
395 {
396 jerryx_arg_transform_func_t func;
397
398 if (opt_flag == JERRYX_ARG_OPTIONAL)
399 {
400 func = jerryx_arg_transform_object_props_optional;
401 }
402 else
403 {
404 func = jerryx_arg_transform_object_props;
405 }
406
407 return (jerryx_arg_t)
408 {
409 .func = func,
410 .dest = NULL,
411 .extra_info = (uintptr_t) object_props
412 };
413 } /* jerryx_arg_object_properties */
414
415 /**
416 * Create a jerryx_arg_t instance for array.
417 *
418 * @return a jerryx_arg_t instance.
419 */
420 static inline jerryx_arg_t
jerryx_arg_array(const jerryx_arg_array_items_t * array_items_p,jerryx_arg_optional_t opt_flag)421 jerryx_arg_array (const jerryx_arg_array_items_t *array_items_p, /**< pointer to array items mapping */
422 jerryx_arg_optional_t opt_flag) /**< whether the argument is optional */
423 {
424 jerryx_arg_transform_func_t func;
425
426 if (opt_flag == JERRYX_ARG_OPTIONAL)
427 {
428 func = jerryx_arg_transform_array_items_optional;
429 }
430 else
431 {
432 func = jerryx_arg_transform_array_items;
433 }
434
435 return (jerryx_arg_t)
436 {
437 .func = func,
438 .dest = NULL,
439 .extra_info = (uintptr_t) array_items_p
440 };
441 } /* jerryx_arg_array */
442
443 #endif /* !JERRYX_ARG_IMPL_H */
444