From dcf8ccfd038f36fda8567fe94699ad2ef207b74e Mon Sep 17 00:00:00 2001 From: Zoltan Herczeg Date: Fri, 24 Apr 2020 10:45:14 +0200 Subject: [PATCH] Function declaration outside of blocks in direct eval must be ES5.1 compatible. (#3690) JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com --- jerry-core/parser/js/js-parser-statm.c | 46 +++++++++----------------- jerry-core/parser/js/js-scanner-util.c | 5 ++- tests/jerry/eval.js | 7 ++++ 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/jerry-core/parser/js/js-parser-statm.c b/jerry-core/parser/js/js-parser-statm.c index bfc877cf7..2d53b47f6 100644 --- a/jerry-core/parser/js/js-parser-statm.c +++ b/jerry-core/parser/js/js-parser-statm.c @@ -698,7 +698,8 @@ parser_parse_function_statement (parser_context_t *context_p) /**< context */ JERRY_ASSERT (scope_stack_p[1].map_from == PARSER_SCOPE_STACK_FUNC); #if ENABLED (JERRY_ES2015) - if (!(context_p->status_flags & PARSER_IS_STRICT)) + if (!(context_p->status_flags & PARSER_IS_STRICT) + && (scope_stack_p >= context_p->scope_stack_p + context_p->scope_stack_global_end)) { bool copy_value = true; @@ -717,40 +718,25 @@ parser_parse_function_statement (parser_context_t *context_p) /**< context */ if (copy_value) { - if (!(context_p->status_flags & PARSER_IS_FUNCTION) - && (context_p->global_status_flags & ECMA_PARSE_DIRECT_EVAL)) + stack_p = context_p->scope_stack_p; + + while (stack_p < scope_stack_p) { - if (!scanner_scope_find_let_declaration (context_p, &context_p->token.lit_location)) + if (literal_index == stack_p->map_from) { - context_p->lit_object.literal_p->status_flags |= LEXER_FLAG_USED; + JERRY_ASSERT (!(stack_p->map_to & PARSER_SCOPE_STACK_IS_LEXICAL)); + + uint16_t map_to = scanner_decode_map_to (stack_p); + uint16_t opcode = ((map_to >= PARSER_REGISTER_START) ? CBC_ASSIGN_LITERAL_SET_IDENT + : CBC_COPY_TO_GLOBAL); + parser_emit_cbc_literal_value (context_p, - CBC_COPY_TO_GLOBAL, + opcode, scanner_decode_map_to (scope_stack_p), - literal_index); - } - } - else - { - stack_p = context_p->scope_stack_p; - - while (stack_p < scope_stack_p) - { - if (literal_index == stack_p->map_from) - { - JERRY_ASSERT (!(stack_p->map_to & PARSER_SCOPE_STACK_IS_LEXICAL)); - - uint16_t map_to = scanner_decode_map_to (stack_p); - uint16_t opcode = ((map_to >= PARSER_REGISTER_START) ? CBC_ASSIGN_LITERAL_SET_IDENT - : CBC_COPY_TO_GLOBAL); - - parser_emit_cbc_literal_value (context_p, - opcode, - scanner_decode_map_to (scope_stack_p), - map_to); - break; - } - stack_p++; + map_to); + break; } + stack_p++; } parser_flush_cbc (context_p); diff --git a/jerry-core/parser/js/js-scanner-util.c b/jerry-core/parser/js/js-scanner-util.c index c1e86c172..be84e8ced 100644 --- a/jerry-core/parser/js/js-scanner-util.c +++ b/jerry-core/parser/js/js-scanner-util.c @@ -2170,10 +2170,13 @@ scanner_create_variables (parser_context_t *context_p, /**< context */ if (func_init_opcode == CBC_INIT_LOCAL && (option_flags & SCANNER_CREATE_VARS_IS_SCRIPT)) { #if ENABLED (JERRY_ES2015) - if (!(context_p->global_status_flags & ECMA_PARSE_DIRECT_EVAL)) + literal.char_p -= data_p[1]; + if (!(context_p->global_status_flags & ECMA_PARSE_DIRECT_EVAL) + || !scanner_scope_find_let_declaration (context_p, &literal)) { func_init_opcode = CBC_CREATE_VAR_FUNC_EVAL; } + literal.char_p += data_p[1]; #else /* !ENABLED (JERRY_ES2015) */ func_init_opcode = CBC_CREATE_VAR_FUNC_EVAL; #endif /* ENABLED (JERRY_ES2015) */ diff --git a/tests/jerry/eval.js b/tests/jerry/eval.js index a63861665..b5974b18b 100644 --- a/tests/jerry/eval.js +++ b/tests/jerry/eval.js @@ -152,3 +152,10 @@ function f6() { (((((e)))("assert(p1 === 0)"))); } f6(); + +y = 1; +function f7() { + function x() { return y; } + eval("assert(x()() === 5); function y() { return 5 } assert(x()() === 5)"); +} +f7()