Skip to content

Commit f2dffae

Browse files
committed
Optional arguments should advance the iterator in jerryx_arg_transform_optional
This patch fixes #2288 JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
1 parent c304b9a commit f2dffae

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

jerry-ext/arg/arg-transform-functions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jerryx_arg_transform_optional (jerryx_arg_js_iterator_t *js_arg_iter_p, /**< ava
3434

3535
if (jerry_value_is_undefined (js_arg))
3636
{
37-
return js_arg;
37+
return jerryx_arg_js_iterator_pop (js_arg_iter_p);
3838
}
3939

4040
return func (js_arg_iter_p, c_arg_p);

tests/unit-ext/test-ext-arg.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ static const jerry_char_t test_source[] = TEST_STRING_LITERAL (
3535
"arg2 = new Number(10.5);"
3636
"test_validator1(arg1, arg2, arg3);"
3737
"test_validator1(arg1, 10.5, 'abcdef');"
38+
"test_validator3(arg1);"
39+
"test_validator3();"
3840
"var obj_a = new MyObjectA();"
3941
"var obj_b = new MyObjectB();"
4042
"test_validator2.call(obj_a, 5);"
@@ -86,6 +88,7 @@ static my_type_b_t my_thing_b;
8688

8789
static int validator1_count = 0;
8890
static int validator2_count = 0;
91+
static int validator3_count = 0;
8992
static int validator_int_count = 0;
9093
static int validator_prop_count = 0;
9194
static int validator_array_count = 0;
@@ -242,6 +245,52 @@ test_validator2_handler (const jerry_value_t func_obj_val __attribute__((unused)
242245
return jerry_create_undefined ();
243246
} /* test_validator2_handler */
244247

248+
/**
249+
* The handler should have following arguments:
250+
* arg1: Bool. It is an optional argument.
251+
*
252+
*/
253+
static jerry_value_t
254+
test_validator3_handler (const jerry_value_t func_obj_val __attribute__((unused)), /**< function object */
255+
const jerry_value_t this_val, /**< this value */
256+
const jerry_value_t args_p[], /**< arguments list */
257+
const jerry_length_t args_cnt) /**< arguments length */
258+
{
259+
260+
bool arg1 = false;
261+
262+
jerryx_arg_t mapping[] =
263+
{
264+
/* ignore this */
265+
jerryx_arg_ignore (),
266+
/* 1th argument should be boolean, and it is optional */
267+
jerryx_arg_boolean (&arg1, JERRYX_ARG_COERCE, JERRYX_ARG_OPTIONAL),
268+
};
269+
270+
jerry_value_t is_ok = jerryx_arg_transform_this_and_args (this_val,
271+
args_p,
272+
args_cnt,
273+
mapping,
274+
ARRAY_SIZE (mapping));
275+
276+
if (validator3_count == 0)
277+
{
278+
TEST_ASSERT (!jerry_value_is_error (is_ok));
279+
TEST_ASSERT (arg1);
280+
}
281+
else if (validator3_count == 1)
282+
{
283+
TEST_ASSERT (!jerry_value_is_error (is_ok));
284+
/* arg1 must be unchanged */
285+
TEST_ASSERT (!arg1);
286+
}
287+
288+
jerry_release_value (is_ok);
289+
validator3_count++;
290+
291+
return jerry_create_undefined ();
292+
} /* test_validator3_handler */
293+
245294
/**
246295
* Calling jerryx_arg_transform_object_properties directly.
247296
*/
@@ -808,6 +857,7 @@ main (void)
808857

809858
register_js_function ("test_validator1", test_validator1_handler);
810859
register_js_function ("test_validator2", test_validator2_handler);
860+
register_js_function ("test_validator3", test_validator3_handler);
811861
register_js_function ("test_validator_int1", test_validator_int1_handler);
812862
register_js_function ("test_validator_int2", test_validator_int2_handler);
813863
register_js_function ("test_validator_int3", test_validator_int3_handler);

0 commit comments

Comments
 (0)