Skip to content

Optional arguments should advance the iterator in jerryx_arg_transform_optional #2962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jerry-ext/arg/arg-transform-functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jerryx_arg_transform_optional (jerryx_arg_js_iterator_t *js_arg_iter_p, /**< ava

if (jerry_value_is_undefined (js_arg))
{
return js_arg;
return jerryx_arg_js_iterator_pop (js_arg_iter_p);
}

return func (js_arg_iter_p, c_arg_p);
Expand Down
73 changes: 73 additions & 0 deletions tests/unit-ext/test-ext-arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ static const jerry_char_t test_source[] = TEST_STRING_LITERAL (
"arg2 = new Number(10.5);"
"test_validator1(arg1, arg2, arg3);"
"test_validator1(arg1, 10.5, 'abcdef');"
"test_validator3(arg1, arg1);"
"test_validator3(arg1);"
"test_validator3();"
"test_validator3(undefined, undefined);"
"var obj_a = new MyObjectA();"
"var obj_b = new MyObjectB();"
"test_validator2.call(obj_a, 5);"
Expand Down Expand Up @@ -86,6 +90,7 @@ static my_type_b_t my_thing_b;

static int validator1_count = 0;
static int validator2_count = 0;
static int validator3_count = 0;
static int validator_int_count = 0;
static int validator_prop_count = 0;
static int validator_array_count = 0;
Expand Down Expand Up @@ -242,6 +247,73 @@ test_validator2_handler (const jerry_value_t func_obj_val __attribute__((unused)
return jerry_create_undefined ();
} /* test_validator2_handler */

/**
* The handler should have following arguments:
* arg1: Bool. It is an optional argument.
*
*/
static jerry_value_t
test_validator3_handler (const jerry_value_t func_obj_val __attribute__((unused)), /**< function object */
const jerry_value_t this_val, /**< this value */
const jerry_value_t args_p[], /**< arguments list */
const jerry_length_t args_cnt) /**< arguments length */
{

bool arg1 = false;
bool arg2 = false;

jerryx_arg_t mapping[] =
{
/* ignore this */
jerryx_arg_ignore (),
/* 1th argument should be boolean, and it is optional */
jerryx_arg_boolean (&arg1, JERRYX_ARG_COERCE, JERRYX_ARG_OPTIONAL),
/* 2nd argument should be boolean, and it is optional */
jerryx_arg_boolean (&arg2, JERRYX_ARG_COERCE, JERRYX_ARG_OPTIONAL),
};

jerry_value_t is_ok = jerryx_arg_transform_this_and_args (this_val,
args_p,
args_cnt,
mapping,
ARRAY_SIZE (mapping));

if (validator3_count == 0)
{
TEST_ASSERT (!jerry_value_is_error (is_ok));
TEST_ASSERT (arg1);
TEST_ASSERT (arg2);
}
else if (validator3_count == 1)
{
TEST_ASSERT (!jerry_value_is_error (is_ok));
TEST_ASSERT (arg1);
/* arg2 must be unchanged */
TEST_ASSERT (!arg2);
}
else if (validator3_count == 2)
{
TEST_ASSERT (!jerry_value_is_error (is_ok));
/* arg1 must be unchanged */
TEST_ASSERT (!arg1);
/* arg2 must be unchanged */
TEST_ASSERT (!arg2);
}
else if (validator3_count == 3)
{
TEST_ASSERT (!jerry_value_is_error (is_ok));
/* arg1 must be unchanged */
TEST_ASSERT (!arg1);
/* arg2 must be unchanged */
TEST_ASSERT (!arg2);
}

jerry_release_value (is_ok);
validator3_count++;

return jerry_create_undefined ();
} /* test_validator3_handler */

/**
* Calling jerryx_arg_transform_object_properties directly.
*/
Expand Down Expand Up @@ -808,6 +880,7 @@ main (void)

register_js_function ("test_validator1", test_validator1_handler);
register_js_function ("test_validator2", test_validator2_handler);
register_js_function ("test_validator3", test_validator3_handler);
register_js_function ("test_validator_int1", test_validator_int1_handler);
register_js_function ("test_validator_int2", test_validator_int2_handler);
register_js_function ("test_validator_int3", test_validator_int3_handler);
Expand Down