Rework external function handlers (#4599)

Instead of a fixed number of arguments, a call info structure is passed
to the handlers, which can be extended in the future without breaknig the
API. This structure holds new.target value, so its getter function is removed.

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2021-02-17 17:52:19 +01:00
committed by GitHub
parent 488a0bf7e8
commit 112ad83aaa
51 changed files with 322 additions and 586 deletions
+2 -4
View File
@@ -19,13 +19,11 @@
#include "test-common.h"
static jerry_value_t
callback_func (const jerry_value_t function_obj,
const jerry_value_t this_val,
callback_func (const jerry_call_info_t *call_info_p,
const jerry_value_t args_p[],
const jerry_length_t args_count)
{
JERRY_UNUSED (function_obj);
JERRY_UNUSED (this_val);
JERRY_UNUSED (call_info_p);
JERRY_UNUSED (args_p);
JERRY_UNUSED (args_count);
@@ -28,13 +28,11 @@ typedef struct
} test_entry_t;
static jerry_value_t
my_constructor (const jerry_value_t func_val, /**< function */
const jerry_value_t this_val, /**< this */
my_constructor (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t argv[], /**< arguments */
const jerry_length_t argc) /**< number of arguments */
{
(void) func_val;
(void) this_val;
(void) call_info_p;
(void) argv;
(void) argc;
return jerry_create_undefined ();
+2 -4
View File
@@ -30,13 +30,11 @@ typedef struct
#define ENTRY_IF(TYPE, VALUE, FEATURE, ASYNC) { TYPE, VALUE, jerry_is_feature_enabled (FEATURE), ASYNC }
#define EVALUATE(BUFF) (jerry_eval ((BUFF), sizeof ((BUFF)) - 1, JERRY_PARSE_NO_OPTS))
static jerry_value_t
test_ext_function (const jerry_value_t function_obj, /**< function object */
const jerry_value_t this_val, /**< function this value */
test_ext_function (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< array of arguments */
const jerry_length_t args_cnt) /**< number of arguments */
{
(void) function_obj;
(void) this_val;
(void) call_info_p;
(void) args_p;
(void) args_cnt;
return jerry_create_boolean (true);
+2 -4
View File
@@ -29,13 +29,11 @@ typedef struct
#define ENTRY_IF(TYPE, VALUE, FEATURE) { TYPE, VALUE, jerry_is_feature_enabled (FEATURE) }
#define EVALUATE(BUFF) (jerry_eval ((BUFF), sizeof ((BUFF)) - 1, JERRY_PARSE_NO_OPTS))
static jerry_value_t
test_ext_function (const jerry_value_t function_obj, /**< function object */
const jerry_value_t this_val, /**< function this value */
test_ext_function (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< array of arguments */
const jerry_length_t args_cnt) /**< number of arguments */
{
(void) function_obj;
(void) this_val;
(void) call_info_p;
(void) args_p;
(void) args_cnt;
return jerry_create_boolean (true);
+2 -4
View File
@@ -27,13 +27,11 @@ typedef struct
#define ENTRY(TYPE, VALUE) { TYPE, VALUE }
static jerry_value_t
test_ext_function (const jerry_value_t function_obj, /**< function object */
const jerry_value_t this_val, /**< function this value */
test_ext_function (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< array of arguments */
const jerry_length_t args_cnt) /**< number of arguments */
{
(void) function_obj;
(void) this_val;
(void) call_info_p;
(void) args_p;
(void) args_cnt;
return jerry_create_boolean (true);
+23 -16
View File
@@ -62,8 +62,7 @@ const jerry_char_t test_source[] = TEST_STRING_LITERAL (
bool test_api_is_free_callback_was_called = false;
static jerry_value_t
handler (const jerry_value_t func_obj_val, /**< function object */
const jerry_value_t this_val, /**< this value */
handler (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< arguments list */
const jerry_length_t args_cnt) /**< arguments length */
{
@@ -71,7 +70,10 @@ handler (const jerry_value_t func_obj_val, /**< function object */
jerry_size_t sz;
printf ("ok %u %u %p %u\n",
(unsigned int) func_obj_val, (unsigned int) this_val, (void *) args_p, (unsigned int) args_cnt);
(unsigned int) call_info_p->function,
(unsigned int) call_info_p->this_value,
(void *) args_p,
(unsigned int) args_cnt);
TEST_ASSERT (args_cnt == 2);
@@ -90,13 +92,15 @@ handler (const jerry_value_t func_obj_val, /**< function object */
} /* handler */
static jerry_value_t
handler_throw_test (const jerry_value_t func_obj_val, /**< function object */
const jerry_value_t this_val, /**< this value */
handler_throw_test (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< arguments list */
const jerry_length_t args_cnt) /**< arguments length */
{
printf ("ok %u %u %p %u\n",
(unsigned int) func_obj_val, (unsigned int) this_val, (void *) args_p, (unsigned int) args_cnt);
(unsigned int) call_info_p->function,
(unsigned int) call_info_p->this_value,
(void *) args_p,
(unsigned int) args_cnt);
return jerry_create_error (JERRY_ERROR_TYPE, (jerry_char_t *) "error");
} /* handler_throw_test */
@@ -138,50 +142,53 @@ JERRY_DEFINE_NATIVE_HANDLE_INFO (bind2, handler_construct_2_freecb);
JERRY_DEFINE_NATIVE_HANDLE_INFO (bind3, NULL);
static jerry_value_t
handler_construct (const jerry_value_t func_obj_val, /**< function object */
const jerry_value_t this_val, /**< this value */
handler_construct (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< arguments list */
const jerry_length_t args_cnt) /**< arguments length */
{
printf ("ok construct %u %u %p %u\n",
(unsigned int) func_obj_val, (unsigned int) this_val, (void *) args_p, (unsigned int) args_cnt);
(unsigned int) call_info_p->function,
(unsigned int) call_info_p->this_value,
(void *) args_p,
(unsigned int) args_cnt);
TEST_ASSERT (jerry_value_is_object (this_val));
TEST_ASSERT (jerry_value_is_object (call_info_p->this_value));
TEST_ASSERT (args_cnt == 1);
TEST_ASSERT (jerry_value_is_boolean (args_p[0]));
TEST_ASSERT (jerry_get_boolean_value (args_p[0]) == true);
jerry_value_t this_value = call_info_p->this_value;
jerry_value_t field_name = jerry_create_string ((jerry_char_t *) "value_field");
jerry_value_t res = jerry_set_property (this_val, field_name, args_p[0]);
jerry_value_t res = jerry_set_property (this_value, field_name, args_p[0]);
TEST_ASSERT (!jerry_value_is_error (res));
TEST_ASSERT (jerry_value_is_boolean (res) && jerry_get_boolean_value (res));
jerry_release_value (res);
jerry_release_value (field_name);
/* Set a native pointer. */
jerry_set_object_native_pointer (this_val,
jerry_set_object_native_pointer (this_value,
(void *) 0x0000000000000000ull,
&JERRY_NATIVE_HANDLE_INFO_FOR_CTYPE (bind1));
/* Check that the native pointer was set. */
void *ptr = NULL;
bool is_ok = jerry_get_object_native_pointer (this_val, &ptr, &JERRY_NATIVE_HANDLE_INFO_FOR_CTYPE (bind1));
bool is_ok = jerry_get_object_native_pointer (this_value, &ptr, &JERRY_NATIVE_HANDLE_INFO_FOR_CTYPE (bind1));
TEST_ASSERT (is_ok
&& (uintptr_t) ptr == (uintptr_t) 0x0000000000000000ull);
/* Set a second native pointer. */
jerry_set_object_native_pointer (this_val,
jerry_set_object_native_pointer (this_value,
(void *) 0x0012345678abcdefull,
&JERRY_NATIVE_HANDLE_INFO_FOR_CTYPE (bind2));
/* Check that a second native pointer was set. */
is_ok = jerry_get_object_native_pointer (this_val, &ptr, &JERRY_NATIVE_HANDLE_INFO_FOR_CTYPE (bind2));
is_ok = jerry_get_object_native_pointer (this_value, &ptr, &JERRY_NATIVE_HANDLE_INFO_FOR_CTYPE (bind2));
TEST_ASSERT (is_ok
&& (uintptr_t) ptr == (uintptr_t) 0x0012345678abcdefull);
/* Check that the first native pointer is still set. */
is_ok = jerry_get_object_native_pointer (this_val, &ptr, &JERRY_NATIVE_HANDLE_INFO_FOR_CTYPE (bind1));
is_ok = jerry_get_object_native_pointer (this_value, &ptr, &JERRY_NATIVE_HANDLE_INFO_FOR_CTYPE (bind1));
TEST_ASSERT (is_ok
&& (uintptr_t) ptr == (uintptr_t) 0x0000000000000000ull);
return jerry_create_boolean (true);
+2 -4
View File
@@ -38,13 +38,11 @@ register_js_value (const char *name_p, /**< name of the function */
} /* register_js_value */
static jerry_value_t
assert_handler (const jerry_value_t func_obj_val, /**< function object */
const jerry_value_t this_val, /**< this arg */
assert_handler (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
JERRY_UNUSED (func_obj_val);
JERRY_UNUSED (this_val);
JERRY_UNUSED (call_info_p);
if (args_cnt > 0
&& jerry_value_is_boolean (args_p[0])
+4 -8
View File
@@ -18,13 +18,11 @@
#include "test-common.h"
static jerry_value_t
backtrace_handler (const jerry_value_t function_obj, /**< function object */
const jerry_value_t this_val, /**< this value */
backtrace_handler (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< argument list */
const jerry_length_t args_count) /**< argument count */
{
JERRY_UNUSED (function_obj);
JERRY_UNUSED (this_val);
JERRY_UNUSED (call_info_p);
uint32_t max_depth = 0;
@@ -98,13 +96,11 @@ backtrace_callback (jerry_backtrace_frame_t *frame_p, /* frame information */
} /* backtrace_callback */
static jerry_value_t
capture_handler (const jerry_value_t function_obj, /**< function object */
const jerry_value_t this_val, /**< this value */
capture_handler (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< argument list */
const jerry_length_t args_count) /**< argument count */
{
JERRY_UNUSED (function_obj);
JERRY_UNUSED (this_val);
JERRY_UNUSED (call_info_p);
JERRY_UNUSED (args_p);
JERRY_UNUSED (args_count);
+2 -4
View File
@@ -19,13 +19,11 @@
#include "test-common.h"
static jerry_value_t
custom_to_json (const jerry_value_t func_obj_val, /**< function object */
const jerry_value_t this_val, /**< this value */
custom_to_json (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< arguments list */
const jerry_length_t args_cnt) /**< arguments length */
{
JERRY_UNUSED (func_obj_val);
JERRY_UNUSED (this_val);
JERRY_UNUSED (call_info_p);
JERRY_UNUSED (args_p);
JERRY_UNUSED (args_cnt);
+2 -4
View File
@@ -19,13 +19,11 @@
static const char instanceof_source[] = "var x = function(o, c) {return (o instanceof c);}; x";
static jerry_value_t
external_function (const jerry_value_t function_obj,
const jerry_value_t this_arg,
external_function (const jerry_call_info_t *call_info_p,
const jerry_value_t args_p[],
const jerry_size_t args_count)
{
(void) function_obj;
(void) this_arg;
(void) call_info_p;
(void) args_p;
(void) args_count;
+10 -14
View File
@@ -47,13 +47,10 @@ enum
};
static jerry_value_t
construct_handler (const jerry_value_t func_obj_val, /**< function object */
const jerry_value_t this_val, /**< this arg */
construct_handler (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
JERRY_UNUSED (func_obj_val);
JERRY_UNUSED (this_val);
JERRY_UNUSED (args_p);
if (args_cnt != 1 || !jerry_value_is_number (args_p[0]))
@@ -68,32 +65,31 @@ construct_handler (const jerry_value_t func_obj_val, /**< function object */
case TEST_ID_SIMPLE_CONSTRUCT:
{
/* Method was called with "new": new.target should be equal to the function object. */
jerry_value_t target = jerry_get_new_target ();
jerry_value_t target = call_info_p->new_target;
TEST_ASSERT (!jerry_value_is_undefined (target));
TEST_ASSERT (target == func_obj_val);
jerry_release_value (target);
TEST_ASSERT (target == call_info_p->function);
break;
}
case TEST_ID_SIMPLE_CALL:
{
/* Method was called directly without "new": new.target should be equal undefined. */
jerry_value_t target = jerry_get_new_target ();
jerry_value_t target = call_info_p->new_target;
TEST_ASSERT (jerry_value_is_undefined (target));
TEST_ASSERT (target != func_obj_val);
jerry_release_value (target);
TEST_ASSERT (target != call_info_p->function);
break;
}
case TEST_ID_CONSTRUCT_AND_CALL_SUB:
{
/* Method was called with "new": new.target should be equal to the function object. */
jerry_value_t target = jerry_get_new_target ();
jerry_value_t target = call_info_p->new_target;
TEST_ASSERT (!jerry_value_is_undefined (target));
TEST_ASSERT (target == func_obj_val);
jerry_release_value (target);
TEST_ASSERT (target == call_info_p->function);
/* Calling a function should hide the old "new.target". */
jerry_value_t sub_arg = jerry_create_number (TEST_ID_SIMPLE_CALL);
jerry_value_t func_call_result = jerry_call_function (func_obj_val, this_val, &sub_arg, 1);
jerry_value_t func_call_result;
func_call_result = jerry_call_function (call_info_p->function, call_info_p->this_value, &sub_arg, 1);
TEST_ASSERT (!jerry_value_is_error (func_call_result));
TEST_ASSERT (jerry_value_is_undefined (func_call_result));
break;
+6 -12
View File
@@ -36,13 +36,11 @@ static const jerry_char_t s1[] = "resolved";
static const jerry_char_t s2[] = "rejected";
static jerry_value_t
create_promise1_handler (const jerry_value_t func_obj_val, /**< function object */
const jerry_value_t this_val, /**< this value */
create_promise1_handler (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< arguments list */
const jerry_length_t args_cnt) /**< arguments length */
{
JERRY_UNUSED (func_obj_val);
JERRY_UNUSED (this_val);
JERRY_UNUSED (call_info_p);
JERRY_UNUSED (args_p);
JERRY_UNUSED (args_cnt);
@@ -53,13 +51,11 @@ create_promise1_handler (const jerry_value_t func_obj_val, /**< function object
} /* create_promise1_handler */
static jerry_value_t
create_promise2_handler (const jerry_value_t func_obj_val, /**< function object */
const jerry_value_t this_val, /**< this value */
create_promise2_handler (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< arguments list */
const jerry_length_t args_cnt) /**< arguments length */
{
JERRY_UNUSED (func_obj_val);
JERRY_UNUSED (this_val);
JERRY_UNUSED (call_info_p);
JERRY_UNUSED (args_p);
JERRY_UNUSED (args_cnt);
@@ -70,13 +66,11 @@ create_promise2_handler (const jerry_value_t func_obj_val, /**< function object
} /* create_promise2_handler */
static jerry_value_t
assert_handler (const jerry_value_t func_obj_val, /**< function object */
const jerry_value_t this_val, /**< this arg */
assert_handler (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
JERRY_UNUSED (func_obj_val);
JERRY_UNUSED (this_val);
JERRY_UNUSED (call_info_p);
count_in_assert++;
+10 -16
View File
@@ -58,13 +58,11 @@ assert (pdemo.value === 13);
static int demo_value = 0;
static jerry_value_t
handler_get (const jerry_value_t function_obj, /**< function object */
const jerry_value_t this_val, /**< this arg */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_count) /**< number of function arguments */
handler_get (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_count) /**< number of function arguments */
{
JERRY_UNUSED (function_obj);
JERRY_UNUSED (this_val);
JERRY_UNUSED (call_info_p);
TEST_ASSERT (args_count == 3);
TEST_ASSERT (jerry_value_is_object (args_p[0])); /* target */
@@ -84,13 +82,11 @@ handler_get (const jerry_value_t function_obj, /**< function object */
} /* handler_get */
static jerry_value_t
handler_set (const jerry_value_t function_obj, /**< function object */
const jerry_value_t this_val, /**< this arg */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_count) /**< number of function arguments */
handler_set (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_count) /**< number of function arguments */
{
JERRY_UNUSED (function_obj);
JERRY_UNUSED (this_val);
JERRY_UNUSED (call_info_p);
JERRY_UNUSED (args_p);
JERRY_UNUSED (args_count);
@@ -167,13 +163,11 @@ static const jerry_object_native_info_t proxy_native_info =
};
static jerry_value_t
proxy_native_handler_get (const jerry_value_t function_obj, /**< function object */
const jerry_value_t this_val, /**< this arg */
proxy_native_handler_get (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_count) /**< number of function arguments */
{
JERRY_UNUSED (function_obj);
JERRY_UNUSED (this_val);
JERRY_UNUSED (call_info_p);
TEST_ASSERT (args_count == 3);
/* 3rd argument (Receiver) should be the Proxy here. */
+2 -4
View File
@@ -22,13 +22,11 @@
* Empty constructor
*/
static jerry_value_t
construct_handler (const jerry_value_t func_obj_val, /**< function object */
const jerry_value_t this_val, /**< this arg */
construct_handler (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
JERRY_UNUSED (func_obj_val);
JERRY_UNUSED (this_val);
JERRY_UNUSED (call_info_p);
TEST_ASSERT (args_cnt == 1);
TEST_ASSERT (jerry_get_number_value (args_p[0]) == 1.0);
+2 -4
View File
@@ -18,13 +18,11 @@
#include "test-common.h"
static jerry_value_t
resource_name_handler (const jerry_value_t function_obj, /**< function object */
const jerry_value_t this_val, /**< this value */
resource_name_handler (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< argument list */
const jerry_length_t args_count) /**< argument count */
{
(void) function_obj;
(void) this_val;
(void) call_info_p;
jerry_value_t undefined_value = jerry_create_undefined ();
jerry_value_t resource_name = jerry_get_resource_name (args_count > 0 ? args_p[0] : undefined_value);
+2 -4
View File
@@ -18,13 +18,11 @@
#include "test-common.h"
static jerry_value_t
create_special_proxy_handler (const jerry_value_t function_obj, /**< function object */
const jerry_value_t this_val, /**< this value */
create_special_proxy_handler (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< argument list */
const jerry_length_t args_count) /**< argument count */
{
JERRY_UNUSED (function_obj);
JERRY_UNUSED (this_val);
JERRY_UNUSED (call_info_p);
if (args_count < 2)
{
+2 -4
View File
@@ -50,13 +50,11 @@ register_js_value (const char *name_p, /**< name of the function */
} /* register_js_value */
static jerry_value_t
assert_handler (const jerry_value_t func_obj_val, /**< function object */
const jerry_value_t this_val, /**< this arg */
assert_handler (const jerry_call_info_t *call_info_p, /**< call information */
const jerry_value_t args_p[], /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
JERRY_UNUSED (func_obj_val);
JERRY_UNUSED (this_val);
JERRY_UNUSED (call_info_p);
if (jerry_value_is_boolean (args_p[0])
&& jerry_get_boolean_value (args_p[0]))