mirror of
https://github.com/jerryscript-project/jerryscript.git
synced 2025-12-15 16:29:21 +00:00
Adding Jerry api for calling function with specific this binding.
This commit is contained in:
parent
ef2265ee41
commit
3c2009d331
@ -102,6 +102,9 @@ jerry_api_string_t* jerry_api_create_string (const char *v);
|
||||
extern EXTERN_C
|
||||
jerry_api_object_t* jerry_api_create_object (void);
|
||||
|
||||
extern EXTERN_C
|
||||
bool jerry_api_is_function(const jerry_api_object_t *object_p);
|
||||
|
||||
extern EXTERN_C
|
||||
bool jerry_api_add_object_field (jerry_api_object_t *object_p,
|
||||
const char *field_name_p,
|
||||
@ -121,6 +124,7 @@ bool jerry_api_set_object_field_value (jerry_api_object_t *object_p,
|
||||
|
||||
extern EXTERN_C
|
||||
bool jerry_api_call_function (jerry_api_object_t *function_object_p,
|
||||
jerry_api_object_t *this_arg_p,
|
||||
jerry_api_value_t *retval_p,
|
||||
const jerry_api_value_t args_p [],
|
||||
uint16_t args_count);
|
||||
|
||||
@ -133,6 +133,8 @@ static void
|
||||
jerry_api_convert_api_value_to_ecma_value (ecma_value_t *out_value_p, /**< out: ecma-value */
|
||||
const jerry_api_value_t* api_value_p) /**< value in Jerry API format */
|
||||
{
|
||||
*out_value_p = 0;
|
||||
|
||||
switch (api_value_p->type)
|
||||
{
|
||||
case JERRY_API_DATA_TYPE_UNDEFINED:
|
||||
@ -341,6 +343,20 @@ jerry_api_create_object (void)
|
||||
return ecma_op_create_object_object_noarg ();
|
||||
} /* jerry_api_create_object */
|
||||
|
||||
/**
|
||||
* Check if the specified object is a function object.
|
||||
*
|
||||
* @return true, if the sepcfied object is a function object.
|
||||
* false - otherwise.
|
||||
*/
|
||||
bool
|
||||
jerry_api_is_function(const jerry_api_object_t* object_p)
|
||||
{
|
||||
JERRY_ASSERT (object_p != NULL);
|
||||
|
||||
return ecma_get_object_type (object_p) == ECMA_OBJECT_TYPE_FUNCTION;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create field (named data property) in the specified object
|
||||
*
|
||||
@ -515,6 +531,8 @@ jerry_api_set_object_field_value (jerry_api_object_t *object_p, /**< object */
|
||||
*/
|
||||
bool
|
||||
jerry_api_call_function (jerry_api_object_t *function_object_p, /**< function object to call */
|
||||
jerry_api_object_t *this_arg_p, /**< this arg for this binding
|
||||
* or NULL (set this binding to the global object) */
|
||||
jerry_api_value_t *retval_p, /**< place for function's return value (if it is required)
|
||||
* or NULL (if it should be 'undefined') */
|
||||
const jerry_api_value_t args_p [], /**< function's call arguments
|
||||
@ -535,8 +553,19 @@ jerry_api_call_function (jerry_api_object_t *function_object_p, /**< function ob
|
||||
|
||||
ecma_completion_value_t call_completion;
|
||||
|
||||
ecma_value_t this_arg_val = 0;
|
||||
if (this_arg_p == NULL)
|
||||
{
|
||||
this_arg_val = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
|
||||
}
|
||||
else
|
||||
{
|
||||
ecma_ref_object(this_arg_p);
|
||||
this_arg_val = ecma_make_object_value (this_arg_p);
|
||||
}
|
||||
|
||||
call_completion = ecma_op_function_call (function_object_p,
|
||||
ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED),
|
||||
this_arg_val,
|
||||
arg_values,
|
||||
args_count);
|
||||
|
||||
@ -557,6 +586,7 @@ jerry_api_call_function (jerry_api_object_t *function_object_p, /**< function ob
|
||||
is_successful = false;
|
||||
}
|
||||
|
||||
ecma_free_value (this_arg_val, true);
|
||||
ecma_free_completion_value (call_completion);
|
||||
|
||||
for (uint32_t i = 0; i < args_count; i++)
|
||||
@ -567,7 +597,7 @@ jerry_api_call_function (jerry_api_object_t *function_object_p, /**< function ob
|
||||
MEM_FINALIZE_LOCAL_ARRAY (arg_values);
|
||||
|
||||
return is_successful;
|
||||
} /* jerry_api_call_function */
|
||||
} /* jerry_api_call_funition */
|
||||
|
||||
/**
|
||||
* Get global object
|
||||
|
||||
@ -21,7 +21,20 @@
|
||||
#include "jerry.h"
|
||||
#include "jerry-api.h"
|
||||
|
||||
const char *test_source = "var t = 1;\nfunction f () {\n return t;\n}\nthis.foo = f;\n";
|
||||
const char *test_source =
|
||||
"this.t = 1; "
|
||||
"function f () { "
|
||||
"return this.t; "
|
||||
"} "
|
||||
"this.foo = f; "
|
||||
"this.bar = function (a) { "
|
||||
"return a + t; "
|
||||
"} "
|
||||
"function A () { "
|
||||
"this.t = 12; "
|
||||
"} "
|
||||
"this.A = A; "
|
||||
"this.a = new A (); ";
|
||||
|
||||
/**
|
||||
* Initialize Jerry API value with specified float64 number
|
||||
@ -52,7 +65,7 @@ main (void)
|
||||
|
||||
bool is_ok;
|
||||
ssize_t sz;
|
||||
jerry_api_value_t val_t, val_foo;
|
||||
jerry_api_value_t val_t, val_foo, val_bar, val_A, val_A_prototype, val_a, val_a_foo;
|
||||
jerry_api_object_t* global_obj_p;
|
||||
jerry_api_value_t res, args [2];
|
||||
char buffer [16];
|
||||
@ -65,12 +78,14 @@ main (void)
|
||||
|
||||
global_obj_p = jerry_api_get_global ();
|
||||
|
||||
// Get global.t
|
||||
is_ok = jerry_api_get_object_field_value (global_obj_p, "t", &val_t);
|
||||
assert (is_ok
|
||||
&& val_t.type == JERRY_API_DATA_TYPE_FLOAT64
|
||||
&& val_t.v_float64 == 1.0);
|
||||
jerry_api_release_value (&val_t);
|
||||
|
||||
// Get global.foo
|
||||
is_ok = jerry_api_get_object_field_value (global_obj_p, "foo", &val_foo);
|
||||
assert (is_ok
|
||||
&& val_foo.type == JERRY_API_DATA_TYPE_OBJECT);
|
||||
@ -78,12 +93,27 @@ main (void)
|
||||
test_api_init_api_value_float64 (&args[0], 4);
|
||||
test_api_init_api_value_float64 (&args[1], 2);
|
||||
|
||||
is_ok = jerry_api_call_function (val_foo.v_object, &res, args, 2);
|
||||
// Call foo (4, 2)
|
||||
is_ok = jerry_api_call_function (val_foo.v_object, NULL, &res, args, 2);
|
||||
assert (is_ok
|
||||
&& res.type == JERRY_API_DATA_TYPE_FLOAT64
|
||||
&& res.v_float64 == 1.0);
|
||||
jerry_api_release_value (&res);
|
||||
|
||||
// Get global.bar
|
||||
is_ok = jerry_api_get_object_field_value (global_obj_p, "bar", &val_bar);
|
||||
assert (is_ok
|
||||
&& val_bar.type == JERRY_API_DATA_TYPE_OBJECT);
|
||||
|
||||
// Call bar (4, 2)
|
||||
is_ok = jerry_api_call_function (val_bar.v_object, NULL, &res, args, 2);
|
||||
assert (is_ok
|
||||
&& res.type == JERRY_API_DATA_TYPE_FLOAT64
|
||||
&& res.v_float64 == 5.0);
|
||||
jerry_api_release_value (&res);
|
||||
jerry_api_release_value (&val_bar);
|
||||
|
||||
// Set global.t = "abcd"
|
||||
test_api_init_api_value_string (&args[0], "abcd");
|
||||
is_ok = jerry_api_set_object_field_value (global_obj_p,
|
||||
"t",
|
||||
@ -91,7 +121,8 @@ main (void)
|
||||
assert (is_ok);
|
||||
jerry_api_release_value (&args[0]);
|
||||
|
||||
is_ok = jerry_api_call_function (val_foo.v_object, &res, args, 2);
|
||||
// Call foo (4, 2)
|
||||
is_ok = jerry_api_call_function (val_foo.v_object, NULL, &res, args, 2);
|
||||
assert (is_ok
|
||||
&& res.type == JERRY_API_DATA_TYPE_STRING);
|
||||
sz = jerry_api_string_to_char_buffer (res.v_string, NULL, 0);
|
||||
@ -101,11 +132,59 @@ main (void)
|
||||
jerry_api_release_value (&res);
|
||||
assert (!strcmp (buffer, "abcd"));
|
||||
|
||||
// Get global.A
|
||||
is_ok = jerry_api_get_object_field_value (global_obj_p, "A", &val_A);
|
||||
assert (is_ok
|
||||
&& val_A.type == JERRY_API_DATA_TYPE_OBJECT);
|
||||
|
||||
// Get A.prototype
|
||||
is_ok = jerry_api_is_function (val_A.v_object);
|
||||
assert(is_ok);
|
||||
is_ok = jerry_api_get_object_field_value (val_A.v_object,
|
||||
"prototype",
|
||||
&val_A_prototype);
|
||||
assert (is_ok
|
||||
&& val_A_prototype.type == JERRY_API_DATA_TYPE_OBJECT);
|
||||
jerry_api_release_value (&val_A);
|
||||
|
||||
// Set A.prototype.foo = global.foo
|
||||
is_ok = jerry_api_set_object_field_value (val_A_prototype.v_object,
|
||||
"foo",
|
||||
&val_foo);
|
||||
assert (is_ok);
|
||||
jerry_api_release_value (&val_A_prototype);
|
||||
jerry_api_release_value (&val_foo);
|
||||
|
||||
// Get global.a
|
||||
is_ok = jerry_api_get_object_field_value (global_obj_p, "a", &val_a);
|
||||
assert (is_ok
|
||||
&& val_a.type == JERRY_API_DATA_TYPE_OBJECT);
|
||||
|
||||
// Get a.t
|
||||
is_ok = jerry_api_get_object_field_value (val_a.v_object, "t", &res);
|
||||
assert (is_ok
|
||||
&& res.type == JERRY_API_DATA_TYPE_FLOAT64
|
||||
&& res.v_float64 == 12.0);
|
||||
jerry_api_release_value (&res);
|
||||
|
||||
// Get a.foo
|
||||
is_ok = jerry_api_get_object_field_value (val_a.v_object, "foo", &val_a_foo);
|
||||
assert (is_ok
|
||||
&& val_a_foo.type == JERRY_API_DATA_TYPE_OBJECT);
|
||||
|
||||
// Call a.foo ()
|
||||
is_ok = jerry_api_call_function (val_a_foo.v_object, val_a.v_object, &res, NULL, 0);
|
||||
assert (is_ok
|
||||
&& res.type == JERRY_API_DATA_TYPE_FLOAT64
|
||||
&& res.v_float64 == 12.0);
|
||||
jerry_api_release_value (&res);
|
||||
jerry_api_release_value (&val_a_foo);
|
||||
|
||||
jerry_api_release_value (&val_a);
|
||||
|
||||
jerry_api_release_object (global_obj_p);
|
||||
|
||||
jerry_cleanup ();
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user