|
| 1 | +# jerryx_arg types |
| 2 | + |
| 3 | +## jerryx_arg_t |
| 4 | + |
| 5 | +**Summary** |
| 6 | + |
| 7 | +The structure defining a single validation/transformation step. |
| 8 | + |
| 9 | +*Note*: For commonly used validators, `args.h` provides helpers to create the `jerryx_arg_t`s. |
| 10 | +For example, `jerryx_arg_number()`, `jerryx_arg_boolean()`, etc. |
| 11 | + |
| 12 | +**Prototype** |
| 13 | + |
| 14 | +```c |
| 15 | +typedef struct |
| 16 | +{ |
| 17 | + /** the transform function */ |
| 18 | + jerryx_arg_transform_func_t func; |
| 19 | + /** pointer to destination where func should store the result */ |
| 20 | + void *dest; |
| 21 | + /** extra information, specific to func */ |
| 22 | + uintptr_t extra_info; |
| 23 | +} jerryx_arg_t; |
| 24 | +``` |
| 25 | + |
| 26 | +**See also** |
| 27 | + |
| 28 | +- [jerryx_arg_number](#jerryx_arg_number) |
| 29 | +- [jerryx_arg_boolean](#jerryx_arg_boolean) |
| 30 | +- [jerryx_arg_string](#jerryx_arg_string) |
| 31 | +- [jerryx_arg_function](#jerryx_arg_function) |
| 32 | +- [jerryx_arg_native_pointer](#jerryx_arg_native_pointer) |
| 33 | +- [jerryx_arg_ignore](#jerryx_arg_ignore) |
| 34 | + |
| 35 | +## jerryx_arg_transform_func_t |
| 36 | + |
| 37 | +**Summary** |
| 38 | + |
| 39 | +Signature of the transform function. |
| 40 | + |
| 41 | +Users can create custom transformations by implementing a transform function |
| 42 | +and using `jerryx_arg_custom()`. |
| 43 | + |
| 44 | +The function is expected to return `undefined` if it ran successfully or |
| 45 | +return an `Error` in case it failed. The function can use the iterator and the |
| 46 | +helpers `jerryx_arg_js_iterator_pop()` and `jerryx_arg_js_iterator_peek()` to |
| 47 | +get the next input value. |
| 48 | + |
| 49 | +*Note*: A transform function is allowed to consume any number of input values! |
| 50 | +This enables complex validation like handling different JS function signatures, |
| 51 | +mapping multiple input arguments to a C struct, etc. |
| 52 | + |
| 53 | +The function is expected to store the result of |
| 54 | + a successful transformation into `c_arg_p->dest`. In case the validation did |
| 55 | + not pass, the transform should not modify `c_arg_p->dest`. |
| 56 | + |
| 57 | + Additional parameters can be provided to the function through `c_arg_p->extra_info`. |
| 58 | + |
| 59 | +**Prototype** |
| 60 | + |
| 61 | +```c |
| 62 | +typedef jerry_value_t (*jerryx_arg_transform_func_t) (jerryx_arg_js_iterator_t *js_arg_iter_p, |
| 63 | + const jerryx_arg_t *c_arg_p); |
| 64 | +``` |
| 65 | +
|
| 66 | +**See also** |
| 67 | +
|
| 68 | +- [jerryx_arg_custom](#jerryx_arg_custom) |
| 69 | +- [jerryx_arg_js_iterator_pop](#jerryx_arg_js_iterator_pop) |
| 70 | +- [jerryx_arg_js_iterator_peek](#jerryx_arg_js_iterator_peek) |
| 71 | +
|
| 72 | +
|
| 73 | +## jerryx_arg_coerce_t |
| 74 | +
|
| 75 | +Enum that indicates whether an argument is allowed to be coerced into the expected JS type. |
| 76 | +
|
| 77 | + - JERRYX_ARG_COERCE - the transform will invoke toNumber, toBoolean, toString, etc. |
| 78 | + - JERRYX_ARG_NO_COERCE - the type coercion is not allowed. The transform will fail if the type does not match the expectation. |
| 79 | +
|
| 80 | +**See also** |
| 81 | +
|
| 82 | +- [jerryx_arg_number](#jerryx_arg_number) |
| 83 | +- [jerryx_arg_boolean](#jerryx_arg_boolean) |
| 84 | +- [jerryx_arg_string](#jerryx_arg_string) |
| 85 | +
|
| 86 | +## jerryx_arg_optional_t |
| 87 | +
|
| 88 | +Enum that indicates whether an argument is optional or required. |
| 89 | + |
| 90 | + - JERRYX_ARG_OPTIONAL - The argument is optional. If the argument is `undefined` the transform is successful and `c_arg_p->dest` remains untouched. |
| 91 | + - JERRYX_ARG_REQUIRED - The argument is required. If the argument is `undefined` the transform will fail and `c_arg_p->dest` remains untouched. |
| 92 | +
|
| 93 | +**See also** |
| 94 | +
|
| 95 | +- [jerryx_arg_number](#jerryx_arg_number) |
| 96 | +- [jerryx_arg_boolean](#jerryx_arg_boolean) |
| 97 | +- [jerryx_arg_string](#jerryx_arg_string) |
| 98 | +- [jerryx_arg_function](#jerryx_arg_function) |
| 99 | +- [jerryx_arg_native_pointer](#jerryx_arg_native_pointer) |
| 100 | +
|
| 101 | +# Main functions |
| 102 | +
|
| 103 | +## jerryx_arg_transform_args_with_this |
| 104 | +
|
| 105 | +**Summary** |
| 106 | +
|
| 107 | +Validate the this value and the JS arguments, and assign them to the native arguments. |
| 108 | +This function is useful to perform input validation inside external function handlers (see `jerry_external_handler_t`). |
| 109 | +
|
| 110 | +**Prototype** |
| 111 | +
|
| 112 | +```c |
| 113 | +jerry_value_t |
| 114 | +jerryx_arg_transform_args_with_this (const jerry_value_t this_val, |
| 115 | + const jerry_value_t *js_arg_p, |
| 116 | + const jerry_length_t js_arg_cnt, |
| 117 | + const jerryx_arg_t *c_arg_p, |
| 118 | + jerry_length_t c_arg_cnt) |
| 119 | +``` |
| 120 | + |
| 121 | + - `this_val` - `this` value. Note this is processed as the first value, before the array of arguments. |
| 122 | + - `js_arg_p` - points to the array with JS arguments. |
| 123 | + - `js_arg_cnt` - the count of the `js_arg_p` array. |
| 124 | + - `c_arg_p` - points to the array of validation/transformation steps |
| 125 | + - `c_arg_cnt` - the count of the `c_arg_p` array. |
| 126 | + - return value - a `jerry_value_t` representing `undefined` if all validators passed or an `Error` if a validator failed. |
| 127 | + |
| 128 | +**Example** |
| 129 | + |
| 130 | +```c |
| 131 | +// JS signature: function(requiredBool, requiredString, optionalNumber) |
| 132 | +static jerry_value_t my_external_handler(const jerry_value_t function_obj, |
| 133 | + const jerry_value_t this_val, |
| 134 | + const jerry_value_t args_p[], |
| 135 | + const jerry_length_t args_count) |
| 136 | +{ |
| 137 | + bool required_bool; |
| 138 | + char required_str[16]; |
| 139 | + double optional_num = 1234.567; // default value |
| 140 | + |
| 141 | + // "mapping" defines the steps to transform input arguments to C variables: |
| 142 | + const jerryx_arg_t mapping[] = { |
| 143 | + // `this` is the first value. No checking needed on `this` for this function. |
| 144 | + jerryx_arg_ignore(), |
| 145 | + |
| 146 | + jerryx_arg_boolean(&required_bool, JERRYX_ARG_NO_COERCE, JERRYX_ARG_REQUIRED), |
| 147 | + jerryx_arg_string(&required_str, sizeof(required_str) JERRYX_ARG_NO_COERCE, JERRYX_ARG_REQUIRED), |
| 148 | + jerryx_arg_number(&optional_num, JERRYX_ARG_NO_COERCE, JERRYX_ARG_OPTIONAL), |
| 149 | + }; |
| 150 | + |
| 151 | + // Validate and transform: |
| 152 | + const jerry_value_t rv = |
| 153 | + jerryx_arg_transform_args_with_this(this_val, args_p, args_count, |
| 154 | + mapping, ARRAY_LENGTH(mapping)); |
| 155 | + |
| 156 | + if (jerry_value_has_error_flag(rv)) { |
| 157 | + // Handle error |
| 158 | + return rv; |
| 159 | + } |
| 160 | + |
| 161 | + // Validated and tranformed successfully! |
| 162 | + // required_bool, required_str and optional_num can now be used. |
| 163 | + // ... |
| 164 | +} |
| 165 | +``` |
| 166 | +
|
| 167 | +**See also** |
| 168 | +
|
| 169 | +- [jerryx_arg_ignore](#jerryx_arg_ignore) |
| 170 | +- [jerryx_arg_number](#jerryx_arg_number) |
| 171 | +- [jerryx_arg_boolean](#jerryx_arg_boolean) |
| 172 | +- [jerryx_arg_string](#jerryx_arg_string) |
| 173 | +- [jerryx_arg_function](#jerryx_arg_function) |
| 174 | +- [jerryx_arg_native_pointer](#jerryx_arg_native_pointer) |
| 175 | +- [jerryx_arg_custom](#jerryx_arg_custom) |
| 176 | +
|
| 177 | +
|
| 178 | +## jerryx_arg_transform_args |
| 179 | +
|
| 180 | +**Summary** |
| 181 | +
|
| 182 | +Validate an array of `jerry_value_t` and assign them to the native arguments. |
| 183 | +
|
| 184 | +**Prototype** |
| 185 | +
|
| 186 | +```c |
| 187 | +jerry_value_t |
| 188 | +jerryx_arg_transform_args (const jerry_value_t *js_arg_p, |
| 189 | + const jerry_length_t js_arg_cnt, |
| 190 | + const jerryx_arg_t *c_arg_p, |
| 191 | + jerry_length_t c_arg_cnt) |
| 192 | +``` |
| 193 | + |
| 194 | + - `js_arg_p` - points to the array with JS arguments. |
| 195 | + - `js_arg_cnt` - the count of the `js_arg_p` array. |
| 196 | + - `c_arg_p` - points to the array of validation/transformation steps |
| 197 | + - `c_arg_cnt` - the count of the `c_arg_p` array. |
| 198 | + - return value - a `jerry_value_t` representing `undefined` if all validators passed or an `Error` if a validator failed. |
| 199 | + |
| 200 | +**See also** |
| 201 | + |
| 202 | +- [jerryx_arg_transform_args_with_this](#jerryx_arg_transform_args_with_this) |
| 203 | + |
| 204 | + |
| 205 | +# Helpers for commonly used validations |
| 206 | + |
| 207 | +## jerryx_arg_number |
| 208 | + |
| 209 | +**Summary** |
| 210 | + |
| 211 | +Create a validation/transformation step (`jerryx_arg_t`) that expects to consume |
| 212 | +one `number` JS argument and stores it into a C `double`. |
| 213 | + |
| 214 | +**Prototype** |
| 215 | + |
| 216 | +```c |
| 217 | +static inline jerryx_arg_t |
| 218 | +jerryx_arg_number (double *dest, |
| 219 | + jerryx_arg_coerce_t coerce_flag, |
| 220 | + jerryx_arg_optional_t opt_flag) |
| 221 | +``` |
| 222 | +
|
| 223 | + - return value - the created `jerryx_arg_t` instance. |
| 224 | + - `dest` - pointer to the `double` where the result should be stored. |
| 225 | + - `coerce_flag` - whether type coercion is allowed. |
| 226 | + - `opt_flag` - whether the argument is optional. |
| 227 | +
|
| 228 | +**See also** |
| 229 | +
|
| 230 | +- [jerryx_arg_transform_args_with_this](#jerryx_arg_transform_args_with_this) |
| 231 | +
|
| 232 | +## jerryx_arg_boolean |
| 233 | +
|
| 234 | +**Summary** |
| 235 | +
|
| 236 | +Create a validation/transformation step (`jerryx_arg_t`) that expects to |
| 237 | +consume one `boolean` JS argument and stores it into a C `bool`. |
| 238 | +
|
| 239 | +**Prototype** |
| 240 | +
|
| 241 | +```c |
| 242 | +static inline jerryx_arg_t |
| 243 | +jerryx_arg_boolean (bool *dest, |
| 244 | + jerryx_arg_coerce_t coerce_flag, |
| 245 | + jerryx_arg_optional_t opt_flag) |
| 246 | +``` |
| 247 | + - return value - the created `jerryx_arg_t` instance. |
| 248 | + - `dest` - pointer to the `bool` where the result should be stored. |
| 249 | + - `coerce_flag` - whether type coercion is allowed. |
| 250 | + - `opt_flag` - whether the argument is optional. |
| 251 | + |
| 252 | +**See also** |
| 253 | + |
| 254 | +- [jerryx_arg_transform_args_with_this](#jerryx_arg_transform_args_with_this) |
| 255 | + |
| 256 | + |
| 257 | +## jerryx_arg_string |
| 258 | + |
| 259 | +**Summary** |
| 260 | + |
| 261 | +Create a validation/transformation step (`jerryx_arg_t`) that expects to |
| 262 | +consume one `string` JS argument and stores it into a C `char` array. |
| 263 | + |
| 264 | +**Prototype** |
| 265 | + |
| 266 | +```c |
| 267 | +static inline jerryx_arg_t |
| 268 | +jerryx_arg_string (char *dest, |
| 269 | + uint32_t size, |
| 270 | + jerryx_arg_coerce_t coerce_flag, |
| 271 | + jerryx_arg_optional_t opt_flag) |
| 272 | +``` |
| 273 | +
|
| 274 | + - return value - the created `jerryx_arg_t` instance. |
| 275 | + - `dest` - pointer to the native char array where the result should be stored. |
| 276 | + - `size` - the size of native char array. |
| 277 | + - `coerce_flag` - whether type coercion is allowed. |
| 278 | + - `opt_flag` - whether the argument is optional. |
| 279 | +
|
| 280 | +**See also** |
| 281 | +
|
| 282 | +- [jerryx_arg_transform_args_with_this](#jerryx_arg_transform_args_with_this) |
| 283 | +
|
| 284 | +
|
| 285 | +## jerryx_arg_function |
| 286 | +
|
| 287 | +**Summary** |
| 288 | +
|
| 289 | +Create a validation/transformation step (`jerryx_arg_t`) that expects to |
| 290 | +consume one `function` JS argument and stores it into a C `jerry_value_t`. |
| 291 | +
|
| 292 | +**Prototype** |
| 293 | +
|
| 294 | +```c |
| 295 | +static inline jerryx_arg_t |
| 296 | +jerryx_arg_function (jerry_value_t *dest, |
| 297 | + jerryx_arg_optional_t opt_flag) |
| 298 | +
|
| 299 | +``` |
| 300 | + - return value - the created `jerryx_arg_t` instance. |
| 301 | + - `dest` - pointer to the `jerry_value_t` where the result should be stored. |
| 302 | + - `opt_flag` - whether the argument is optional. |
| 303 | + |
| 304 | +**See also** |
| 305 | + |
| 306 | +- [jerryx_arg_transform_args_with_this](#jerryx_arg_transform_args_with_this) |
| 307 | + |
| 308 | +## jerryx_arg_native_pointer |
| 309 | + |
| 310 | +**Summary** |
| 311 | + |
| 312 | +Create a validation/transformation step (`jerryx_arg_t`) that expects to |
| 313 | +consume one `Object` JS argument that is 'backed' with a native pointer with |
| 314 | +a given type info. In case the native pointer info matches, the transform |
| 315 | +will succeed and the object's native pointer will be assigned to `*dest`. |
| 316 | + |
| 317 | +**Prototype** |
| 318 | + |
| 319 | +```c |
| 320 | +static inline jerryx_arg_t |
| 321 | +jerryx_arg_native_pointer (void **dest, |
| 322 | + const jerry_object_native_info_t *info_p, |
| 323 | + jerryx_arg_optional_t opt_flag) |
| 324 | +``` |
| 325 | + - return value - the created `jerryx_arg_t` instance. |
| 326 | + - `dest` - pointer to where the resulting native pointer should be stored. |
| 327 | + - `info_p` - expected the type info. |
| 328 | + - `opt_flag` - whether the argument is optional. |
| 329 | +
|
| 330 | +**See also** |
| 331 | +
|
| 332 | +- [jerryx_arg_transform_args_with_this](#jerryx_arg_transform_args_with_this) |
| 333 | +
|
| 334 | +
|
| 335 | +# Functions to create custom validations |
| 336 | +
|
| 337 | +## jerryx_arg_custom |
| 338 | +
|
| 339 | +**Summary** |
| 340 | +
|
| 341 | +Create a jerryx_arg_t instance with custom transform. |
| 342 | +
|
| 343 | +**Prototype** |
| 344 | +
|
| 345 | +```c |
| 346 | +static inline jerryx_arg_t |
| 347 | +jerryx_arg_custom (void *dest, |
| 348 | + uintptr_t extra_info, |
| 349 | + jerryx_arg_transform_func_t func) |
| 350 | +
|
| 351 | +``` |
| 352 | + - return value - the created `jerryx_arg_t` instance. |
| 353 | + - `dest` - pointer to the native argument where the result should be stored. |
| 354 | + - `extra_info` - the extra parameter data, specific to the transform function. |
| 355 | + - `func` - the custom transform function. |
| 356 | + |
| 357 | +**See also** |
| 358 | + |
| 359 | +- [jerryx_arg_transform_args_with_this](#jerryx_arg_transform_args_with_this) |
| 360 | + |
| 361 | + |
| 362 | + |
| 363 | +## jerryx_arg_js_iterator_pop |
| 364 | + |
| 365 | +**Summary** |
| 366 | + |
| 367 | +Pop the current `jerry_value_t` argument from the iterator. |
| 368 | +It will change the `js_arg_idx` and `js_arg_p` value in the iterator. |
| 369 | + |
| 370 | +**Prototype** |
| 371 | + |
| 372 | +```c |
| 373 | +jerry_value_t |
| 374 | +jerryx_arg_js_iterator_pop (jerryx_arg_js_iterator_t *js_arg_iter_p) |
| 375 | +``` |
| 376 | + - return value - the `jerry_value_t` argument that was popped. |
| 377 | + - `js_arg_iter_p` - the JS arg iterator from which to pop. |
| 378 | +
|
| 379 | +## jerryx_arg_js_iterator_peek |
| 380 | +
|
| 381 | +**Summary** |
| 382 | +
|
| 383 | +Get the current JS argument from the iterator, without moving the iterator forward. |
| 384 | +*Note:* Unlike `jerryx_arg_js_iterator_pop()`, it will not change `js_arg_idx` and |
| 385 | +`js_arg_p` value in the iterator. |
| 386 | +
|
| 387 | +**Prototype** |
| 388 | +
|
| 389 | +```c |
| 390 | +jerry_value_t |
| 391 | +jerryx_arg_js_iterator_peek (jerryx_arg_js_iterator_t *js_arg_iter_p) |
| 392 | +``` |
| 393 | + - return value - the current `jerry_value_t` argument. |
| 394 | + - `js_arg_iter_p` - the JS arg iterator from which to peek. |
0 commit comments