diff --git a/jerry-core/ecma/base/ecma-globals.h b/jerry-core/ecma/base/ecma-globals.h index 789d79c2e..e4f1874c5 100644 --- a/jerry-core/ecma/base/ecma-globals.h +++ b/jerry-core/ecma/base/ecma-globals.h @@ -89,26 +89,30 @@ typedef enum #endif /* ENABLED (JERRY_DEBUGGER) */ /** - * Option flags for script parsing. + * Option flags for parser_parse_script and internal flags for global_status_flags in parser context. * Note: - * The enum members must be kept in sync with parser_general_flags_t - * The last 16 bits are reserved for scope chain index + * the last 16 bits is reserved for internal parser flags, because the debugger uses these + * 16 bits to encode the scope chain skip index as well (see ECMA_PARSE_CHAIN_INDEX_SHIFT) */ typedef enum { ECMA_PARSE_NO_OPTS = 0, /**< no options passed */ - ECMA_PARSE_STRICT_MODE = (1u << 0), /**< enable strict mode */ - ECMA_PARSE_DIRECT_EVAL = (1u << 1), /**< eval is called directly (ECMA-262 v5, 15.1.2.1.1) */ + ECMA_PARSE_STRICT_MODE = (1u << 0), /**< enable strict mode, must be same as PARSER_IS_STRICT */ + ECMA_PARSE_MODULE = (1u << 1), /**< module is parsed */ + ECMA_PARSE_EVAL = (1u << 2), /**< eval is called */ + ECMA_PARSE_DIRECT_EVAL = (1u << 3), /**< eval is called directly (ECMA-262 v5, 15.1.2.1.1) */ + /* These four status flags must be in this order. See PARSER_CLASS_PARSE_OPTS_OFFSET. */ - ECMA_PARSE_CLASS_CONSTRUCTOR = (1u << 2), /**< a class constructor is being parsed (this value must be kept in + ECMA_PARSE_CLASS_CONSTRUCTOR = (1u << 4), /**< a class constructor is being parsed (this value must be kept in * in sync with PARSER_CLASS_CONSTRUCTOR) */ - ECMA_PARSE_HAS_SUPER = (1u << 3), /**< the current context has super reference */ - ECMA_PARSE_HAS_IMPL_SUPER = (1u << 4), /**< the current context has implicit parent class */ - ECMA_PARSE_HAS_STATIC_SUPER = (1u << 5), /**< the current context is a static class method */ - ECMA_PARSE_EVAL = (1u << 6), /**< eval is called */ - ECMA_PARSE_MODULE = (1u << 7), /**< module is parsed */ - ECMA_PARSE_FUNCTION = (1u << 8), /**< a function body is parsed or the code is inside a function */ + ECMA_PARSE_HAS_SUPER = (1u << 5), /**< the current context has super reference */ + ECMA_PARSE_HAS_IMPL_SUPER = (1u << 6), /**< the current context has implicit parent class */ + ECMA_PARSE_HAS_STATIC_SUPER = (1u << 7), /**< the current context is a static class method */ + + ECMA_PARSE_CALLED_FROM_FUNCTION = (1u << 8), /**< a function body is parsed or the code is inside a function */ ECMA_PARSE_GENERATOR_FUNCTION = (1u << 9), /**< generator function is parsed */ + + /* These flags are internally used by the parser. */ } ecma_parse_opts_t; /** diff --git a/jerry-core/ecma/operations/ecma-eval.c b/jerry-core/ecma/operations/ecma-eval.c index 6febae8cd..308af65e5 100644 --- a/jerry-core/ecma/operations/ecma-eval.c +++ b/jerry-core/ecma/operations/ecma-eval.c @@ -104,7 +104,7 @@ ecma_op_eval_chars_buffer (const lit_utf8_byte_t *code_p, /**< code characters b if (JERRY_CONTEXT (current_new_target) != JERRY_CONTEXT_INVALID_NEW_TARGET && (JERRY_CONTEXT (status_flags) & ECMA_STATUS_DIRECT_EVAL)) { - parse_opts |= ECMA_PARSE_FUNCTION; + parse_opts |= ECMA_PARSE_CALLED_FROM_FUNCTION; } #endif /* ENABLED (JERRY_ES2015) */ diff --git a/jerry-core/parser/js/js-parser-expr.c b/jerry-core/parser/js/js-parser-expr.c index dd8839a5d..5179e160d 100644 --- a/jerry-core/parser/js/js-parser-expr.c +++ b/jerry-core/parser/js/js-parser-expr.c @@ -1412,7 +1412,8 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */ /* Check if "new.target" is written here. */ if (scanner_try_scan_new_target (context_p)) { - if (!(context_p->status_flags & PARSER_IS_FUNCTION)) + if (!(context_p->status_flags & PARSER_IS_FUNCTION) + && !(context_p->global_status_flags & ECMA_PARSE_CALLED_FROM_FUNCTION)) { parser_raise_error (context_p, PARSER_ERR_NEW_TARGET_NOT_ALLOWED); } diff --git a/jerry-core/parser/js/js-parser-internal.h b/jerry-core/parser/js/js-parser-internal.h index b69cc9342..d7edd0202 100644 --- a/jerry-core/parser/js/js-parser-internal.h +++ b/jerry-core/parser/js/js-parser-internal.h @@ -72,13 +72,10 @@ typedef enum PARSER_CLASS_IMPLICIT_SUPER = (1u << 21), /**< class has implicit parent class */ PARSER_CLASS_STATIC_FUNCTION = (1u << 22), /**< this function is a static class method */ PARSER_CLASS_SUPER_PROP_REFERENCE = (1u << 23), /**< super property call or assignment */ - PARSER_IS_EVAL = (1u << 24), /**< eval code */ - PARSER_IS_DIRECT_EVAL = (1u << 25), /**< direct eval code */ #endif /* ENABLED (JERRY_ES2015) */ #if ENABLED (JERRY_ES2015_MODULE_SYSTEM) - PARSER_IS_MODULE = (1u << 26), /**< an export / import keyword is encountered */ - PARSER_MODULE_DEFAULT_CLASS_OR_FUNC = (1u << 27), /**< parsing a function or class default export */ - PARSER_MODULE_STORE_IDENT = (1u << 28), /**< store identifier of the current export statement */ + PARSER_MODULE_DEFAULT_CLASS_OR_FUNC = (1u << 24), /**< parsing a function or class default export */ + PARSER_MODULE_STORE_IDENT = (1u << 25), /**< store identifier of the current export statement */ #endif /* ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ PARSER_HAS_LATE_LIT_INIT = (1u << 30), /**< there are identifier or string literals which construction * is postponed after the local parser data is freed */ @@ -465,6 +462,7 @@ typedef struct /* Parser members. */ uint32_t status_flags; /**< status flags */ + uint32_t global_status_flags; /**< global status flags */ uint16_t stack_depth; /**< current stack depth */ uint16_t stack_limit; /**< maximum stack depth */ parser_saved_context_t *last_context_p; /**< last saved context */ diff --git a/jerry-core/parser/js/js-parser-module.c b/jerry-core/parser/js/js-parser-module.c index c53016945..79cf61968 100644 --- a/jerry-core/parser/js/js-parser-module.c +++ b/jerry-core/parser/js/js-parser-module.c @@ -555,7 +555,8 @@ parser_module_check_request_place (parser_context_t *context_p) /**< parser cont { if (context_p->last_context_p != NULL || context_p->stack_top_uint8 != 0 - || (context_p->status_flags & (PARSER_IS_EVAL | PARSER_IS_FUNCTION)) != 0) + || (context_p->status_flags & PARSER_IS_FUNCTION) + || (context_p->global_status_flags & ECMA_PARSE_EVAL)) { parser_raise_error (context_p, PARSER_ERR_MODULE_UNEXPECTED); } diff --git a/jerry-core/parser/js/js-parser-statm.c b/jerry-core/parser/js/js-parser-statm.c index c28c38356..d815184e2 100644 --- a/jerry-core/parser/js/js-parser-statm.c +++ b/jerry-core/parser/js/js-parser-statm.c @@ -717,7 +717,8 @@ parser_parse_function_statement (parser_context_t *context_p) /**< context */ if (copy_value) { - if (context_p->status_flags & PARSER_IS_DIRECT_EVAL) + if (!(context_p->status_flags & PARSER_IS_FUNCTION) + && (context_p->global_status_flags & ECMA_PARSE_DIRECT_EVAL)) { if (!scanner_scope_find_let_declaration (context_p, &context_p->token.lit_location)) { @@ -3096,14 +3097,7 @@ parser_parse_statements (parser_context_t *context_p) /**< context */ options |= PARSE_EXPR_HAS_LITERAL; } -#if ENABLED (JERRY_ES2015) - bool is_eval = (context_p->status_flags & PARSER_IS_EVAL) != 0; -#else /* !ENABLED (JERRY_ES2015) */ - /* In case of ES5.1 it does not matter that this is an eval parsing or not. */ - bool is_eval = false; -#endif /* ENABLED (JERRY_ES2015) */ - - if ((context_p->status_flags & PARSER_IS_FUNCTION) && !is_eval) + if (context_p->status_flags & PARSER_IS_FUNCTION) { parser_parse_expression_statement (context_p, options); } diff --git a/jerry-core/parser/js/js-parser.c b/jerry-core/parser/js/js-parser.c index 7aa6869f3..9f12f858f 100644 --- a/jerry-core/parser/js/js-parser.c +++ b/jerry-core/parser/js/js-parser.c @@ -1920,13 +1920,12 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */ error_location_p->error = PARSER_ERR_NO_ERROR; } - if (arg_list_p == NULL) + context.status_flags = parse_opts & PARSER_STRICT_MODE_MASK; + context.global_status_flags = parse_opts; + + if (arg_list_p != NULL) { - context.status_flags = 0; - } - else - { - context.status_flags = PARSER_IS_FUNCTION; + context.status_flags |= PARSER_IS_FUNCTION; #if ENABLED (JERRY_ES2015) if (parse_opts & ECMA_PARSE_GENERATOR_FUNCTION) { @@ -1944,7 +1943,6 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */ context.stack_limit = 0; context.last_context_p = NULL; context.last_statement.current_p = NULL; - context.status_flags |= parse_opts & PARSER_STRICT_MODE_MASK; context.token.flags = 0; context.line = 1; @@ -1995,21 +1993,6 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */ } #endif /* ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) */ -#if ENABLED (JERRY_ES2015) - if (parse_opts & ECMA_PARSE_EVAL) - { - context.status_flags |= PARSER_IS_EVAL; - } - if (parse_opts & ECMA_PARSE_DIRECT_EVAL) - { - context.status_flags |= PARSER_IS_DIRECT_EVAL; - } - if (parse_opts & ECMA_PARSE_FUNCTION) - { - context.status_flags |= PARSER_IS_FUNCTION; - } -#endif /* ENABLED (JERRY_ES2015) */ - scanner_scan_all (&context, arg_list_p, arg_list_p + arg_list_size, @@ -2052,7 +2035,7 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */ #endif /* ENABLED (JERRY_DEBUGGER) */ #if ENABLED (JERRY_ES2015_MODULE_SYSTEM) - if (context.status_flags & PARSER_IS_MODULE) + if (context.global_status_flags & ECMA_PARSE_MODULE) { context.status_flags |= PARSER_IS_STRICT; } diff --git a/jerry-core/parser/js/js-scanner-util.c b/jerry-core/parser/js/js-scanner-util.c index c6922a0c4..f10c78b4a 100644 --- a/jerry-core/parser/js/js-scanner-util.c +++ b/jerry-core/parser/js/js-scanner-util.c @@ -473,7 +473,7 @@ scanner_pop_literal_pool (parser_context_t *context_p, /**< context */ uint8_t no_reg_types = 0; #if ENABLED (JERRY_ES2015) - if (!(context_p->status_flags & PARSER_IS_DIRECT_EVAL)) + if (prev_literal_pool_p == NULL && !(context_p->global_status_flags & ECMA_PARSE_DIRECT_EVAL)) { no_reg_types |= SCANNER_LITERAL_IS_FUNC; } @@ -528,7 +528,7 @@ scanner_pop_literal_pool (parser_context_t *context_p, /**< context */ if (is_function && (type & (SCANNER_LITERAL_IS_FUNC | SCANNER_LITERAL_IS_LOCAL)) == SCANNER_LITERAL_IS_FUNC) { if (prev_literal_pool_p == NULL - && (context_p->status_flags & PARSER_IS_DIRECT_EVAL) + && (context_p->global_status_flags & ECMA_PARSE_DIRECT_EVAL) && scanner_scope_find_let_declaration (context_p, literal_p)) { literal_p->type = 0; @@ -1300,7 +1300,7 @@ scanner_detect_invalid_var (parser_context_t *context_p, /**< context */ } } - if ((context_p->status_flags & PARSER_IS_DIRECT_EVAL) + if ((context_p->global_status_flags & ECMA_PARSE_DIRECT_EVAL) && scanner_scope_find_let_declaration (context_p, var_literal_p)) { scanner_raise_redeclaration_error (context_p); @@ -1624,7 +1624,7 @@ scanner_is_global_context_needed (parser_context_t *context_p) /**< context */ /* Only let/const can be stored in registers */ JERRY_ASSERT ((data & SCANNER_STREAM_NO_REG) - || (type == SCANNER_STREAM_TYPE_FUNC && (context_p->status_flags & PARSER_IS_DIRECT_EVAL)) + || (type == SCANNER_STREAM_TYPE_FUNC && (context_p->global_status_flags & ECMA_PARSE_DIRECT_EVAL)) || type == SCANNER_STREAM_TYPE_LET || type == SCANNER_STREAM_TYPE_CONST); @@ -1645,7 +1645,7 @@ scanner_is_global_context_needed (parser_context_t *context_p) /**< context */ } if (type == SCANNER_STREAM_TYPE_VAR - || (type == SCANNER_STREAM_TYPE_FUNC && !(context_p->status_flags & PARSER_IS_DIRECT_EVAL)) + || (type == SCANNER_STREAM_TYPE_FUNC && !(context_p->global_status_flags & ECMA_PARSE_DIRECT_EVAL)) || type == SCANNER_STREAM_TYPE_IMPORT) { continue; @@ -2138,7 +2138,7 @@ 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->status_flags & PARSER_IS_DIRECT_EVAL)) + if (!(context_p->global_status_flags & ECMA_PARSE_DIRECT_EVAL)) { func_init_opcode = CBC_CREATE_VAR_FUNC_EVAL; } diff --git a/jerry-core/parser/js/js-scanner.c b/jerry-core/parser/js/js-scanner.c index eb575b9a9..3fb3097f0 100644 --- a/jerry-core/parser/js/js-scanner.c +++ b/jerry-core/parser/js/js-scanner.c @@ -1440,7 +1440,7 @@ scanner_scan_statement (parser_context_t *context_p, /**< context */ scanner_raise_error (context_p); } - scanner_context_p->context_status_flags |= PARSER_IS_MODULE; + context_p->global_status_flags |= ECMA_PARSE_MODULE; scanner_context_p->mode = SCAN_MODE_STATEMENT_END; lexer_next_token (context_p); @@ -1608,7 +1608,7 @@ scanner_scan_statement (parser_context_t *context_p, /**< context */ scanner_raise_error (context_p); } - scanner_context_p->context_status_flags |= PARSER_IS_MODULE; + context_p->global_status_flags |= ECMA_PARSE_MODULE; lexer_next_token (context_p); diff --git a/jerry-core/vm/vm.c b/jerry-core/vm/vm.c index 15aabfa09..fbffc5a1b 100644 --- a/jerry-core/vm/vm.c +++ b/jerry-core/vm/vm.c @@ -344,6 +344,7 @@ vm_run_eval (ecma_compiled_code_t *bytecode_data_p, /**< byte-code data */ #if ENABLED (JERRY_DEBUGGER) uint32_t chain_index = parse_opts >> ECMA_PARSE_CHAIN_INDEX_SHIFT; + parse_opts &= (1 << ECMA_PARSE_CHAIN_INDEX_SHIFT) - 1; while (chain_index != 0) {