Skip to content

Commit 7cb9f80

Browse files
author
kisbg
authored
Add support test262 $262 global object support (#4329)
Few test-cases in test262 uses a $262 object to run a few method (for example detachedArrayBuffer) JerryScript-DCO-1.0-Signed-off-by: bence gabor kis [email protected]
1 parent 1cb18f0 commit 7cb9f80

File tree

5 files changed

+140
-194
lines changed

5 files changed

+140
-194
lines changed

jerry-main/main-options.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ typedef enum
3232
OPT_HELP,
3333
OPT_VERSION,
3434
OPT_MEM_STATS,
35+
OPT_TEST262_OBJECT,
3536
OPT_PARSE_ONLY,
3637
OPT_SHOW_OP,
3738
OPT_SHOW_RE_OP,
@@ -60,6 +61,8 @@ static const cli_opt_t main_opts[] =
6061
.help = "print tool and library version and exit"),
6162
CLI_OPT_DEF (.id = OPT_MEM_STATS, .longopt = "mem-stats",
6263
.help = "dump memory statistics"),
64+
CLI_OPT_DEF (.id = OPT_TEST262_OBJECT, .longopt = "test262-object",
65+
.help = "create test262 object"),
6366
CLI_OPT_DEF (.id = OPT_PARSE_ONLY, .longopt = "parse-only",
6467
.help = "don't execute JS input"),
6568
CLI_OPT_DEF (.id = OPT_SHOW_OP, .longopt = "show-opcodes",
@@ -180,6 +183,11 @@ main_parse_args (int argc, /**< argc */
180183
}
181184
break;
182185
}
186+
case OPT_TEST262_OBJECT:
187+
{
188+
arguments_p->option_flags |= OPT_FLAG_TEST262_OBJECT;
189+
break;
190+
}
183191
case OPT_PARSE_ONLY:
184192
{
185193
arguments_p->option_flags |= OPT_FLAG_PARSE_ONLY;

jerry-main/main-options.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
*/
2424
typedef enum
2525
{
26-
OPT_FLAG_EMPTY = 0,
27-
OPT_FLAG_PARSE_ONLY = (1 << 0),
28-
OPT_FLAG_DEBUG_SERVER = (1 << 1),
29-
OPT_FLAG_WAIT_SOURCE = (1 << 2),
30-
OPT_FLAG_NO_PROMPT = (1 << 3),
31-
OPT_FLAG_USE_STDIN = (1 << 4),
26+
OPT_FLAG_EMPTY = 0,
27+
OPT_FLAG_PARSE_ONLY = (1 << 0),
28+
OPT_FLAG_DEBUG_SERVER = (1 << 1),
29+
OPT_FLAG_WAIT_SOURCE = (1 << 2),
30+
OPT_FLAG_NO_PROMPT = (1 << 3),
31+
OPT_FLAG_USE_STDIN = (1 << 4),
32+
OPT_FLAG_TEST262_OBJECT = (1u << 5),
3233
} main_option_flags_t;
3334

3435
/**

jerry-main/main-utils.c

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,126 @@ main_register_global_function (const char *name_p, /**< name of the function */
4444
jerry_release_value (result_val);
4545
} /* main_register_global_function */
4646

47+
/**
48+
* Register a method for the $262 object.
49+
*/
50+
static void
51+
test262_register_function (jerry_value_t test262_obj, /** $262 object */
52+
const char *name_p, /**< name of the function */
53+
jerry_external_handler_t handler_p) /**< function callback */
54+
{
55+
jerry_value_t function_name_val = jerry_create_string ((const jerry_char_t *) name_p);
56+
jerry_value_t function_val = jerry_create_external_function (handler_p);
57+
58+
jerry_value_t result_val = jerry_set_property (test262_obj, function_name_val, function_val);
59+
60+
jerry_release_value (function_val);
61+
jerry_release_value (function_name_val);
62+
63+
assert (!jerry_value_is_error (result_val));
64+
jerry_release_value (result_val);
65+
} /* test262_register_function */
66+
67+
/**
68+
* $262.detachArrayBuffer
69+
*
70+
* A function which implements the DetachArrayBuffer abstract operation
71+
*
72+
* @return null value - if success
73+
* value marked with error flag - otherwise
74+
*/
75+
static jerry_value_t
76+
test262_detach_array_buffer (const jerry_value_t func_obj_val, /**< function object */
77+
const jerry_value_t this_p, /**< this arg */
78+
const jerry_value_t args_p[], /**< function arguments */
79+
const jerry_length_t args_cnt) /**< number of function arguments */
80+
{
81+
(void) func_obj_val; /* unused */
82+
(void) this_p; /* unused */
83+
84+
if (args_cnt < 1 || !jerry_value_is_arraybuffer (args_p[0]))
85+
{
86+
return jerry_create_error (JERRY_ERROR_TYPE, (jerry_char_t *) "Expected an ArrayBuffer object");
87+
}
88+
89+
/* TODO: support the optional 'key' argument */
90+
91+
return jerry_detach_arraybuffer (args_p[0]);
92+
} /* test262_detach_array_buffer */
93+
94+
/**
95+
* $262.evalScript
96+
*
97+
* A function which accepts a string value as its first argument and executes it
98+
*
99+
* @return completion of the script parsing and execution.
100+
*/
101+
static jerry_value_t
102+
test262_eval_script (const jerry_value_t func_obj_val, /**< function object */
103+
const jerry_value_t this_p, /**< this arg */
104+
const jerry_value_t args_p[], /**< function arguments */
105+
const jerry_length_t args_cnt) /**< number of function arguments */
106+
{
107+
(void) func_obj_val; /* unused */
108+
(void) this_p; /* unused */
109+
110+
if (args_cnt < 1 || !jerry_value_is_string (args_p[0]))
111+
{
112+
return jerry_create_error (JERRY_ERROR_TYPE, (jerry_char_t *) "Expected a string");
113+
}
114+
115+
jerry_size_t str_size = jerry_get_utf8_string_size (args_p[0]);
116+
jerry_char_t *str_buf_p = malloc (str_size * sizeof (jerry_char_t));
117+
118+
if (str_buf_p == NULL || jerry_string_to_utf8_char_buffer (args_p[0], str_buf_p, str_size) != str_size)
119+
{
120+
free (str_buf_p);
121+
return jerry_create_error (JERRY_ERROR_RANGE, (jerry_char_t *) "Internal error");
122+
}
123+
124+
jerry_value_t ret_value = jerry_parse (NULL, 0, str_buf_p, str_size, JERRY_PARSE_NO_OPTS);
125+
126+
if (!jerry_value_is_error (ret_value))
127+
{
128+
jerry_value_t func_val = ret_value;
129+
ret_value = jerry_run (func_val);
130+
jerry_release_value (func_val);
131+
}
132+
133+
free (str_buf_p);
134+
135+
return ret_value;
136+
} /* test262_eval_script */
137+
138+
/**
139+
* Init the $262 object
140+
*/
141+
static void
142+
register_test262 (void)
143+
{
144+
jerry_value_t global_obj = jerry_get_global_object ();
145+
jerry_value_t test262_object = jerry_create_object ();
146+
147+
test262_register_function (test262_object, "detachArrayBuffer", test262_detach_array_buffer);
148+
test262_register_function (test262_object, "evalScript", test262_eval_script);
149+
test262_register_function (test262_object, "gc", jerryx_handler_gc);
150+
151+
jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "global");
152+
jerry_value_t result = jerry_set_property (test262_object, prop_name, global_obj);
153+
assert (!jerry_value_is_error (result));
154+
jerry_release_value (prop_name);
155+
jerry_release_value (result);
156+
prop_name = jerry_create_string ((const jerry_char_t *) "$262");
157+
result = jerry_set_property (global_obj, prop_name, test262_object);
158+
159+
jerry_release_value (prop_name);
160+
assert (!jerry_value_is_error (result));
161+
162+
jerry_release_value (global_obj);
163+
jerry_release_value (test262_object);
164+
jerry_release_value (result);
165+
} /* register_test262 */
166+
47167
/**
48168
* Inits the engine and the debugger
49169
*/
@@ -76,7 +196,10 @@ main_init_engine (main_args_t *arguments_p) /** main arguments */
76196
jerryx_debugger_after_connect (protocol && jerryx_debugger_ws_create ());
77197
}
78198
}
79-
199+
if (arguments_p->option_flags & OPT_FLAG_TEST262_OBJECT)
200+
{
201+
register_test262 ();
202+
}
80203
main_register_global_function ("assert", jerryx_handler_assert);
81204
main_register_global_function ("gc", jerryx_handler_gc);
82205
main_register_global_function ("print", jerryx_handler_print);

0 commit comments

Comments
 (0)