jerryx: add jerryx_arg_array (#2052)

Related issue: #2046

JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang zidong.jiang@intel.com
This commit is contained in:
Zidong Jiang
2017-10-30 20:39:14 +01:00
committed by Dániel Bátyai
parent 60bf613c07
commit 5bd72047cc
7 changed files with 339 additions and 12 deletions
+20
View File
@@ -404,6 +404,25 @@ jerryx_arg_transform_object_props (jerryx_arg_js_iterator_t *js_arg_iter_p, /**<
object_props->c_arg_cnt);
} /* jerryx_arg_transform_object_props */
/**
* Check whether the JS array's items have expected types, and transform them into native args.
*
* @return jerry undefined: the transformer passes,
* jerry error: the transformer fails.
*/
jerry_value_t
jerryx_arg_transform_array_items (jerryx_arg_js_iterator_t *js_arg_iter_p, /**< available JS args */
const jerryx_arg_t *c_arg_p) /**< the native arg */
{
jerry_value_t js_arg = jerryx_arg_js_iterator_pop (js_arg_iter_p);
const jerryx_arg_array_items_t *array_items_p = (const jerryx_arg_array_items_t *) c_arg_p->extra_info;
return jerryx_arg_transform_array (js_arg,
array_items_p->c_arg_p,
array_items_p->c_arg_cnt);
} /* jerryx_arg_transform_array_items */
/**
* Define transformer for optional argument.
*/
@@ -424,6 +443,7 @@ JERRYX_ARG_TRANSFORM_OPTIONAL (string_strict)
JERRYX_ARG_TRANSFORM_OPTIONAL (function)
JERRYX_ARG_TRANSFORM_OPTIONAL (native_pointer)
JERRYX_ARG_TRANSFORM_OPTIONAL (object_props)
JERRYX_ARG_TRANSFORM_OPTIONAL (array_items)
JERRYX_ARG_TRANSFORM_OPTIONAL (uint8)
JERRYX_ARG_TRANSFORM_OPTIONAL (uint16)
+43
View File
@@ -143,3 +143,46 @@ jerryx_arg_transform_object_properties (const jerry_value_t obj_val,/**< the JS
return ret;
} /* jerryx_arg_transform_object_properties */
/**
* Validate the items in the JS array and assign them to the native arguments.
*
* @return jerry undefined: all validators passed,
* jerry error: a validator failed.
*/
jerry_value_t
jerryx_arg_transform_array (const jerry_value_t array_val, /**< points to the JS array */
const jerryx_arg_t *c_arg_p, /**< points to the array of validation/transformation steps */
jerry_length_t c_arg_cnt) /**< the count of the `c_arg_p` array */
{
if (!jerry_value_is_array (array_val))
{
return jerry_create_error (JERRY_ERROR_TYPE, (jerry_char_t *) "Not an array.");
}
jerry_value_t arr[c_arg_cnt];
for (jerry_length_t i = 0; i < c_arg_cnt; i++)
{
arr[i] = jerry_get_property_by_index (array_val, i);
if (jerry_value_has_error_flag (arr[i]))
{
for (jerry_length_t j = 0; j < i; j++)
{
jerry_release_value (arr[j]);
}
return arr[i];
}
}
const jerry_value_t ret = jerryx_arg_transform_args (arr, c_arg_cnt, c_arg_p, c_arg_cnt);
for (jerry_length_t i = 0; i < c_arg_cnt; i++)
{
jerry_release_value (arr[i]);
}
return ret;
} /* jerryx_arg_transform_array */
+14
View File
@@ -53,6 +53,15 @@ typedef struct
jerry_length_t c_arg_cnt; /**< the count of the `c_arg_p` array */
} jerryx_arg_object_props_t;
/**
* The structure used in jerryx_arg_array
*/
typedef struct
{
const jerryx_arg_t *c_arg_p; /**< points to the array of transformation steps */
jerry_length_t c_arg_cnt; /**< the count of the `c_arg_p` array */
} jerryx_arg_array_items_t;
/**
* The structure defining a single validation & transformation step.
*/
@@ -79,6 +88,9 @@ jerry_value_t jerryx_arg_transform_object_properties (const jerry_value_t obj_va
const jerry_length_t name_cnt,
const jerryx_arg_t *c_arg_p,
jerry_length_t c_arg_cnt);
jerry_value_t jerryx_arg_transform_array (const jerry_value_t array_val,
const jerryx_arg_t *c_arg_p,
jerry_length_t c_arg_cnt);
/**
* Indicates whether an argument is allowed to be coerced into the expected JS type.
@@ -162,6 +174,8 @@ static inline jerryx_arg_t
jerryx_arg_custom (void *dest, uintptr_t extra_info, jerryx_arg_transform_func_t func);
static inline jerryx_arg_t
jerryx_arg_object_properties (const jerryx_arg_object_props_t *object_props_p, jerryx_arg_optional_t opt_flag);
static inline jerryx_arg_t
jerryx_arg_array (const jerryx_arg_array_items_t *array_items_p, jerryx_arg_optional_t opt_flag);
jerry_value_t
jerryx_arg_transform_optional (jerryx_arg_js_iterator_t *js_arg_iter_p,
@@ -41,6 +41,7 @@ JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (boolean)
JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL (function)
JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL (native_pointer)
JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL (object_props)
JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL (array_items)
jerry_value_t jerryx_arg_transform_ignore (jerryx_arg_js_iterator_t *js_arg_iter_p,
const jerryx_arg_t *c_arg_p);
@@ -361,4 +362,32 @@ jerryx_arg_object_properties (const jerryx_arg_object_props_t *object_props, /**
};
} /* jerryx_arg_object_properties */
/**
* Create a jerryx_arg_t instance for array.
*
* @return a jerryx_arg_t instance.
*/
static inline jerryx_arg_t
jerryx_arg_array (const jerryx_arg_array_items_t *array_items_p, /**< pointer to array items mapping */
jerryx_arg_optional_t opt_flag) /**< whether the argument is optional */
{
jerryx_arg_transform_func_t func;
if (opt_flag == JERRYX_ARG_OPTIONAL)
{
func = jerryx_arg_transform_array_items_optional;
}
else
{
func = jerryx_arg_transform_array_items;
}
return (jerryx_arg_t)
{
.func = func,
.dest = NULL,
.extra_info = (uintptr_t) array_items_p
};
} /* jerryx_arg_array */
#endif /* !JERRYX_ARG_IMPL_H */