Fix lexical scoping between scripts (#3558)

This change fixes the handling of lexical blocks when executing multiple
scripts, and also fixes a few issues with module environments.

After this change, all script files will run in the same context and
will have access to lexically scoped global variables of previous
scripts, and module environments will no longer have a bound global
'this' value.

The REPL implementation in main-unix is also fixed to correctly handle
lexically scoped variables.

Fixes #3561.

JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
This commit is contained in:
Dániel Bátyai
2020-02-21 13:30:48 +01:00
committed by GitHub
parent b0a575b049
commit 68909fc5de
22 changed files with 416 additions and 129 deletions
+41
View File
@@ -811,6 +811,47 @@ main (void)
jerry_cleanup ();
}
/* Test parsing/executing scripts with lexically scoped global variables multiple times. */
if (jerry_is_feature_enabled (JERRY_FEATURE_SYMBOL))
{
jerry_init (JERRY_INIT_EMPTY);
const jerry_char_t scoped_src_p[] = "let a;";
jerry_value_t parse_result = jerry_parse (NULL,
0,
scoped_src_p,
sizeof (scoped_src_p) - 1,
JERRY_PARSE_NO_OPTS);
TEST_ASSERT (!jerry_value_is_error (parse_result));
jerry_release_value (parse_result);
parse_result = jerry_parse (NULL,
0,
scoped_src_p,
sizeof (scoped_src_p) - 1,
JERRY_PARSE_NO_OPTS);
TEST_ASSERT (!jerry_value_is_error (parse_result));
jerry_value_t run_result = jerry_run (parse_result);
TEST_ASSERT (!jerry_value_is_error (run_result));
jerry_release_value (run_result);
/* Should be a syntax error due to redeclaration. */
run_result = jerry_run (parse_result);
TEST_ASSERT (jerry_value_is_error (run_result));
jerry_release_value (run_result);
jerry_release_value (parse_result);
/* The variable should have no effect on parsing. */
parse_result = jerry_parse (NULL,
0,
scoped_src_p,
sizeof (scoped_src_p) - 1,
JERRY_PARSE_NO_OPTS);
TEST_ASSERT (!jerry_value_is_error (parse_result));
jerry_release_value (parse_result);
jerry_cleanup ();
}
/* Test: parser error location */
if (jerry_is_feature_enabled (JERRY_FEATURE_ERROR_MESSAGES))
{
+7 -4
View File
@@ -223,15 +223,15 @@ main (void)
/* Check the snapshot data. Unused bytes should be filled with zeroes */
const uint8_t expected_data[] =
{
0x4A, 0x52, 0x52, 0x59, 0x26, 0x00, 0x00, 0x00,
0x4A, 0x52, 0x52, 0x59, 0x27, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
0x03, 0x00, 0x01, 0x00, 0x41, 0x00, 0x01, 0x00,
0x00, 0x00, 0x00, 0x01, 0x18, 0x00, 0x00, 0x00,
0x2C, 0x00, 0xC1, 0x4E, 0x00, 0x00, 0x00, 0x00,
0x2C, 0x00, 0xC3, 0x50, 0x00, 0x00, 0x00, 0x00,
0x03, 0x00, 0x01, 0x00, 0x41, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x01, 0x07, 0x00, 0x00, 0x00,
0x4F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x14, 0x00, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67,
0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x73, 0x6E,
0x61, 0x70, 0x73, 0x68, 0x6F, 0x74,
@@ -416,7 +416,10 @@ main (void)
size_t snapshot_size = (size_t) jerry_get_number_value (generate_result);
jerry_release_value (generate_result);
TEST_ASSERT (snapshot_size == 124);
/* In ES2015 we emit extra bytecode instructions to check global variable redeclaration. */
const size_t expected_size = (jerry_is_feature_enabled (JERRY_FEATURE_SYMBOL)) ? 132 : 124;
TEST_ASSERT (snapshot_size == expected_size);
const size_t lit_c_buf_sz = jerry_get_literals_from_snapshot (literal_snapshot_buffer,
snapshot_size,