Jerry API development.
Details: - support of this binding in function calls; - jerry_api_is_function and jerry_api_is_constructor interfaces.
This commit is contained in:
@@ -102,6 +102,11 @@ 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_is_constructor (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 +126,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);
|
||||
|
||||
+45
-1
@@ -341,6 +341,37 @@ 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 specified object is a function object,
|
||||
* false - otherwise.
|
||||
*/
|
||||
bool
|
||||
jerry_api_is_function (const jerry_api_object_t* object_p) /**< an object */
|
||||
{
|
||||
JERRY_ASSERT (object_p != NULL);
|
||||
|
||||
return (ecma_get_object_type (object_p) == ECMA_OBJECT_TYPE_FUNCTION
|
||||
|| ecma_get_object_type (object_p) == ECMA_OBJECT_TYPE_BOUND_FUNCTION
|
||||
|| ecma_get_object_type (object_p) == ECMA_OBJECT_TYPE_BUILT_IN_FUNCTION);
|
||||
} /* jerry_api_is_function */
|
||||
|
||||
/**
|
||||
* Check if the specified object is a constructor function object.
|
||||
*
|
||||
* @return true - if the specified object is a function object that implements [[Construct]],
|
||||
* false - otherwise.
|
||||
*/
|
||||
bool
|
||||
jerry_api_is_constructor (const jerry_api_object_t* object_p) /**< an object */
|
||||
{
|
||||
JERRY_ASSERT (object_p != NULL);
|
||||
|
||||
return (ecma_get_object_type (object_p) == ECMA_OBJECT_TYPE_FUNCTION
|
||||
|| ecma_get_object_type (object_p) == ECMA_OBJECT_TYPE_BOUND_FUNCTION);
|
||||
} /* jerry_api_is_constructor */
|
||||
|
||||
/**
|
||||
* Create field (named data property) in the specified object
|
||||
*
|
||||
@@ -515,6 +546,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 +568,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;
|
||||
|
||||
if (this_arg_p == NULL)
|
||||
{
|
||||
this_arg_val = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
|
||||
}
|
||||
else
|
||||
{
|
||||
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);
|
||||
|
||||
|
||||
+85
-6
@@ -21,7 +21,21 @@
|
||||
#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
|
||||
@@ -35,7 +49,7 @@ test_api_init_api_value_float64 (jerry_api_value_t *out_value_p, /**< out: API v
|
||||
} /* test_api_init_api_value_float64 */
|
||||
|
||||
/**
|
||||
* Initialize Jerry API value with specified float64 number
|
||||
* Initialize Jerry API value with specified string
|
||||
*/
|
||||
static void
|
||||
test_api_init_api_value_string (jerry_api_value_t *out_value_p, /**< out: API value */
|
||||
@@ -52,7 +66,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,25 +79,41 @@ 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);
|
||||
|
||||
// Call foo (4, 2)
|
||||
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);
|
||||
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,8 +132,56 @@ 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_constructor (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 ();
|
||||
|
||||
Reference in New Issue
Block a user