|
| 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 JERRY_ARG_INIT_INLINE_H |
| 17 | +#define JERRY_ARG_INIT_INLINE_H |
| 18 | + |
| 19 | +/* validator functions for each type. */ |
| 20 | +jerry_value_t jerry_arg_validator_number_strict (jerry_arg_iterator_t *iterator_p); |
| 21 | +jerry_value_t jerry_arg_validator_number_strict_optional (jerry_arg_iterator_t *iterator_p); |
| 22 | +jerry_value_t jerry_arg_validator_number_optional (jerry_arg_iterator_t *iterator_p); |
| 23 | +jerry_value_t jerry_arg_validator_number (jerry_arg_iterator_t *iterator_p); |
| 24 | +jerry_value_t jerry_arg_validator_boolean_strict (jerry_arg_iterator_t *iterator_p); |
| 25 | +jerry_value_t jerry_arg_validator_boolean_strict_optional (jerry_arg_iterator_t *iterator_p); |
| 26 | +jerry_value_t jerry_arg_validator_boolean_optional (jerry_arg_iterator_t *iterator_p); |
| 27 | +jerry_value_t jerry_arg_validator_boolean (jerry_arg_iterator_t *iterator_p); |
| 28 | +jerry_value_t jerry_arg_validator_string_strict (jerry_arg_iterator_t *iterator_p); |
| 29 | +jerry_value_t jerry_arg_validator_string_strict_optional (jerry_arg_iterator_t *iterator_p); |
| 30 | +jerry_value_t jerry_arg_validator_string_optional (jerry_arg_iterator_t *iterator_p); |
| 31 | +jerry_value_t jerry_arg_validator_string (jerry_arg_iterator_t *iterator_p); |
| 32 | +jerry_value_t jerry_arg_validator_function (jerry_arg_iterator_t *iterator_p); |
| 33 | +jerry_value_t jerry_arg_validator_function_optional (jerry_arg_iterator_t *iterator_p); |
| 34 | +jerry_value_t jerry_arg_validator_constructor (jerry_arg_iterator_t *iterator_p); |
| 35 | +jerry_value_t jerry_arg_validator_constructor_optional (jerry_arg_iterator_t *iterator_p); |
| 36 | +jerry_value_t jerry_arg_validator_native_pointer (jerry_arg_iterator_t *iterator_p); |
| 37 | +jerry_value_t jerry_arg_validator_native_pointer_optional (jerry_arg_iterator_t *iterator_p); |
| 38 | +jerry_value_t jerry_arg_validator_ignore (jerry_arg_iterator_t *iterator_p); |
| 39 | + |
| 40 | +/** |
| 41 | + * Create a jerry_arg_t instance for number argument. |
| 42 | + * |
| 43 | + * @return a jerry_arg_t instance. |
| 44 | + */ |
| 45 | +static inline jerry_arg_t |
| 46 | +jerry_arg_number (double *dest, /**< points to the native number */ |
| 47 | + bool is_strict, /**< whether it is strict type (do not automatically convert inside) */ |
| 48 | + bool is_optional) /**< whether it is optional */ |
| 49 | +{ |
| 50 | + jerry_arg_func_t func; |
| 51 | + |
| 52 | + if (is_strict) |
| 53 | + { |
| 54 | + if (is_optional) |
| 55 | + { |
| 56 | + func = jerry_arg_validator_number_strict_optional; |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + func = jerry_arg_validator_number_strict; |
| 61 | + } |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + if (is_optional) |
| 66 | + { |
| 67 | + func = jerry_arg_validator_number_optional; |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + func = jerry_arg_validator_number; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return (jerry_arg_t) |
| 76 | + { |
| 77 | + .func = func, |
| 78 | + .dest = (void *) dest |
| 79 | + }; |
| 80 | +} /* jerry_arg_number */ |
| 81 | + |
| 82 | +/** |
| 83 | + * Create a jerry_arg_t instance for boolean argument. |
| 84 | + * |
| 85 | + * @return a jerry_arg_t instance. |
| 86 | + */ |
| 87 | +static inline jerry_arg_t |
| 88 | +jerry_arg_boolean (bool *dest, /**< points to the native bool */ |
| 89 | + bool is_strict, /**< whether it is strict type (do not automatically convert inside) */ |
| 90 | + bool is_optional) /**< whether it is optional */ |
| 91 | +{ |
| 92 | + jerry_arg_func_t func; |
| 93 | + |
| 94 | + if (is_strict) |
| 95 | + { |
| 96 | + if (is_optional) |
| 97 | + { |
| 98 | + func = jerry_arg_validator_boolean_strict_optional; |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + func = jerry_arg_validator_boolean_strict; |
| 103 | + } |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + if (is_optional) |
| 108 | + { |
| 109 | + func = jerry_arg_validator_boolean_optional; |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + func = jerry_arg_validator_boolean; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return (jerry_arg_t) |
| 118 | + { |
| 119 | + .func = func, |
| 120 | + .dest = (void *) dest |
| 121 | + }; |
| 122 | +} /* jerry_arg_boolean */ |
| 123 | + |
| 124 | +/** |
| 125 | + * Create a jerry_arg_t instance for string argument. |
| 126 | + * |
| 127 | + * @return a jerry_arg_t instance. |
| 128 | + */ |
| 129 | +static inline jerry_arg_t |
| 130 | +jerry_arg_string (char *dest, /**< points to the native char array */ |
| 131 | + uint32_t size, /**< the size of native char array */ |
| 132 | + bool is_strict, /**< whether it is strict type (do not automatically convert inside) */ |
| 133 | + bool is_optional) /**< whether it is optional */ |
| 134 | +{ |
| 135 | + jerry_arg_func_t func; |
| 136 | + |
| 137 | + if (is_strict) |
| 138 | + { |
| 139 | + if (is_optional) |
| 140 | + { |
| 141 | + func = jerry_arg_validator_string_strict_optional; |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + func = jerry_arg_validator_string_strict; |
| 146 | + } |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + if (is_optional) |
| 151 | + { |
| 152 | + func = jerry_arg_validator_string_optional; |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + func = jerry_arg_validator_string; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return (jerry_arg_t) |
| 161 | + { |
| 162 | + .func = func, |
| 163 | + .dest = (void *) dest, |
| 164 | + .info.length = size |
| 165 | + }; |
| 166 | +} /* jerry_arg_string */ |
| 167 | + |
| 168 | +/** |
| 169 | + * Create a jerry_arg_t instance for function argument. |
| 170 | + * |
| 171 | + * @return a jerry_arg_t instance. |
| 172 | + */ |
| 173 | +static inline jerry_arg_t |
| 174 | +jerry_arg_function (jerry_value_t *dest, /**< points to the js function value */ |
| 175 | + bool is_optional) /**< whether it is optional */ |
| 176 | +{ |
| 177 | + jerry_arg_func_t func; |
| 178 | + |
| 179 | + if (is_optional) |
| 180 | + { |
| 181 | + func = jerry_arg_validator_function_optional; |
| 182 | + } |
| 183 | + else |
| 184 | + { |
| 185 | + func = jerry_arg_validator_function; |
| 186 | + } |
| 187 | + |
| 188 | + return (jerry_arg_t) |
| 189 | + { |
| 190 | + .func = func, |
| 191 | + .dest = (void *) dest |
| 192 | + }; |
| 193 | +} /* jerry_arg_function */ |
| 194 | + |
| 195 | +/** |
| 196 | + * Create a jerry_arg_t instance for native pointer argument. |
| 197 | + * |
| 198 | + * @return a jerry_arg_t instance. |
| 199 | + */ |
| 200 | +static inline jerry_arg_t |
| 201 | +jerry_arg_native_pointer (void **dest, /**< points to the native pointer */ |
| 202 | + const jerry_object_native_info_t *info_p, /**< expected the type info */ |
| 203 | + bool is_optional) /**< whether it is optional */ |
| 204 | +{ |
| 205 | + jerry_arg_func_t func; |
| 206 | + |
| 207 | + if (is_optional) |
| 208 | + { |
| 209 | + func = jerry_arg_validator_native_pointer_optional; |
| 210 | + } |
| 211 | + else |
| 212 | + { |
| 213 | + func = jerry_arg_validator_native_pointer; |
| 214 | + } |
| 215 | + |
| 216 | + return (jerry_arg_t) |
| 217 | + { |
| 218 | + .func = func, |
| 219 | + .dest = (void *) dest, |
| 220 | + .info.obj_p = (void *) info_p |
| 221 | + }; |
| 222 | +} /* jerry_arg_native_pointer */ |
| 223 | + |
| 224 | +/** |
| 225 | + * Create a jerry_arg_t instance for ignored argument. |
| 226 | + * |
| 227 | + * @return a jerry_arg_t instance. |
| 228 | + */ |
| 229 | +static inline jerry_arg_t |
| 230 | +jerry_arg_ignore (void) |
| 231 | +{ |
| 232 | + return (jerry_arg_t) |
| 233 | + { |
| 234 | + .func = jerry_arg_validator_ignore |
| 235 | + }; |
| 236 | +} /* jerry_arg_ignore */ |
| 237 | + |
| 238 | +/** |
| 239 | + * Create a jerry_arg_t instance with custome validator. |
| 240 | + * |
| 241 | + * @return a jerry_arg_t instance. |
| 242 | + */ |
| 243 | +static inline jerry_arg_t |
| 244 | +jerry_arg_custome (void *dest, /**< points to the native argument */ |
| 245 | + void *extra_info, /**< the extra infomation of the jerry_arg_t */ |
| 246 | + jerry_arg_func_t func) /**< the custome validator function */ |
| 247 | +{ |
| 248 | + return (jerry_arg_t) |
| 249 | + { |
| 250 | + .func = func, |
| 251 | + .dest = dest, |
| 252 | + .info.obj_p = extra_info |
| 253 | + }; |
| 254 | +} /* jerry_arg_custome */ |
| 255 | + |
| 256 | +#endif /* !JERRY_ARG_INIT_INLINE_H */ |
0 commit comments