Skip to content

Commit 54147a5

Browse files
author
bence gabor kis
committed
Add support test262 $262 global object support
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 5d916fb commit 54147a5

File tree

7 files changed

+163
-187
lines changed

7 files changed

+163
-187
lines changed

docs/01.CONFIGURATION.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,17 @@ This option is disabled by default.
255255
| CMake: | `-DJERRY_SYSTEM_ALLOCATOR=ON/OFF` |
256256
| Python: | `--system-allocator=ON/OFF` |
257257

258+
### Test262
259+
260+
This option enables the use of $262 object.
261+
This option is disabled by default.
262+
263+
| Options | |
264+
|---------|----------------------------------------------|
265+
| C: | `-DJERRY_TEST262=0/1` |
266+
| CMake: | `-DJERRY_TEST262=ON/OFF` |
267+
| Python: | `--test262=ON/OFF` |
268+
258269
### Valgrind support
259270

260271
This option enables valgrind support for the internal allocator. When enabled, valgrind will be able to properly identify allocated memory regions, and report leaks or out-of-bounds memory accesses.

jerry-core/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ set(JERRY_REGEXP_DUMP_BYTE_CODE OFF CACHE BOOL "Enable regexp byt
3636
set(JERRY_SNAPSHOT_EXEC OFF CACHE BOOL "Enable executing snapshot files?")
3737
set(JERRY_SNAPSHOT_SAVE OFF CACHE BOOL "Enable saving snapshot files?")
3838
set(JERRY_SYSTEM_ALLOCATOR OFF CACHE BOOL "Enable system allocator?")
39+
set(JERRY_TEST262 OFF CACHE BOOL "Enable test262 support?")
3940
set(JERRY_VALGRIND OFF CACHE BOOL "Enable Valgrind support?")
4041
set(JERRY_VM_EXEC_STOP OFF CACHE BOOL "Enable VM execution stopping?")
4142
set(JERRY_GLOBAL_HEAP_SIZE "(512)" CACHE STRING "Size of memory heap, in kilobytes")
@@ -100,6 +101,7 @@ message(STATUS "JERRY_REGEXP_DUMP_BYTE_CODE " ${JERRY_REGEXP_DUMP_BYTE_CODE})
100101
message(STATUS "JERRY_SNAPSHOT_EXEC " ${JERRY_SNAPSHOT_EXEC} ${JERRY_SNAPSHOT_EXEC_MESSAGE})
101102
message(STATUS "JERRY_SNAPSHOT_SAVE " ${JERRY_SNAPSHOT_SAVE} ${JERRY_SNAPSHOT_SAVE_MESSAGE})
102103
message(STATUS "JERRY_SYSTEM_ALLOCATOR " ${JERRY_SYSTEM_ALLOCATOR})
104+
message(STATUS "JERRY_TEST262 " ${JERRY_TEST262})
103105
message(STATUS "JERRY_VALGRIND " ${JERRY_VALGRIND})
104106
message(STATUS "JERRY_VM_EXEC_STOP " ${JERRY_VM_EXEC_STOP})
105107
message(STATUS "JERRY_GLOBAL_HEAP_SIZE " ${JERRY_GLOBAL_HEAP_SIZE})
@@ -296,6 +298,9 @@ jerry_add_define01(JERRY_SNAPSHOT_SAVE)
296298
# Enable system allocator
297299
jerry_add_define01(JERRY_SYSTEM_ALLOCATOR)
298300

301+
# Enable test262
302+
jerry_add_define01(JERRY_TEST262)
303+
299304
# Valgrind
300305
jerry_add_define01(JERRY_VALGRIND)
301306
if(JERRY_VALGRIND)

jerry-core/config.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,19 @@
432432
# define JERRY_VM_EXEC_STOP 0
433433
#endif /* !defined (JERRY_VM_EXEC_STOP) */
434434

435+
/**
436+
* Enable/Disable test262 support
437+
*
438+
* Allowed values:
439+
* 0: Disable test262 support.
440+
* 1: Enable test262 support.
441+
*
442+
* Default value: 0
443+
*/
444+
#ifndef JERRY_TEST262
445+
# define JERRY_TEST262 0
446+
#endif /* JERRY_TEST262 */
447+
435448
/**
436449
* Advanced section configurations.
437450
*/
@@ -682,6 +695,10 @@
682695
|| ((JERRY_VM_EXEC_STOP != 0) && (JERRY_VM_EXEC_STOP != 1))
683696
# error "Invalid value for 'JERRY_VM_EXEC_STOP' macro."
684697
#endif
698+
#if !defined (JERRY_TEST262) \
699+
|| ((JERRY_TEST262 != 0) && (JERRY_TEST262 != 1))
700+
# error "Invalid value for 'JERRY_TEST262' macro."
701+
#endif
685702

686703
#define ENABLED(FEATURE) ((FEATURE) == 1)
687704
#define DISABLED(FEATURE) ((FEATURE) != 1)

jerry-main/main-utils.c

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

86212
/**

0 commit comments

Comments
 (0)