Adding Jerry api for calling function with specific this binding.

This commit is contained in:
Ilyong Cho
2015-03-31 21:01:12 +09:00
parent ef2265ee41
commit 3c2009d331
3 changed files with 120 additions and 7 deletions
+84 -5
View File
@@ -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;
}
}