diff --git a/jerry-core/parser/js/js-lexer.c b/jerry-core/parser/js/js-lexer.c index 0f51ea1db..cb1e7a41e 100644 --- a/jerry-core/parser/js/js-lexer.c +++ b/jerry-core/parser/js/js-lexer.c @@ -3042,6 +3042,42 @@ lexer_current_is_literal (parser_context_t *context_p, /**< context */ return lexer_compare_identifiers (context_p, left_ident_p, right_ident_p); } /* lexer_current_is_literal */ +/** + * Compares the current string token to "use strict". + * + * Note: + * Escape sequences are not allowed. + * + * @return true if "use strict" is found, false otherwise + */ +inline bool JERRY_ATTR_ALWAYS_INLINE +lexer_string_is_use_strict (parser_context_t *context_p) /**< context */ +{ + JERRY_ASSERT (context_p->token.type == LEXER_LITERAL + && context_p->token.lit_location.type == LEXER_STRING_LITERAL); + + return (context_p->token.lit_location.length == 10 + && !context_p->token.lit_location.has_escape + && memcmp (context_p->token.lit_location.char_p, "use strict", 10) == 0); +} /* lexer_string_is_use_strict */ + +/** + * Checks whether the string before the current token is a directive or a string literal. + * + * @return true if the string is a directive, false otherwise + */ +inline bool JERRY_ATTR_ALWAYS_INLINE +lexer_string_is_directive (parser_context_t *context_p) /**< context */ +{ + return (context_p->token.type == LEXER_SEMICOLON + || context_p->token.type == LEXER_RIGHT_BRACE + || ((context_p->token.flags & LEXER_WAS_NEWLINE) + && !LEXER_IS_BINARY_OP_TOKEN (context_p->token.type) + && context_p->token.type != LEXER_LEFT_PAREN + && context_p->token.type != LEXER_LEFT_SQUARE + && context_p->token.type != LEXER_DOT)); +} /* lexer_string_is_directive */ + #if ENABLED (JERRY_ES2015) /** diff --git a/jerry-core/parser/js/js-parser-internal.h b/jerry-core/parser/js/js-parser-internal.h index 2cce1f5f7..e3dfa5f60 100644 --- a/jerry-core/parser/js/js-parser-internal.h +++ b/jerry-core/parser/js/js-parser-internal.h @@ -649,6 +649,8 @@ bool lexer_compare_identifier_to_string (const lexer_lit_location_t *left_p, con bool lexer_compare_identifiers (parser_context_t *context_p, const lexer_lit_location_t *left_p, const lexer_lit_location_t *right_p); bool lexer_current_is_literal (parser_context_t *context_p, const lexer_lit_location_t *right_ident_p); +bool lexer_string_is_use_strict (parser_context_t *context_p); +bool lexer_string_is_directive (parser_context_t *context_p); #if ENABLED (JERRY_ES2015) bool lexer_token_is_identifier (parser_context_t *context_p, const char *identifier_p, size_t identifier_length); diff --git a/jerry-core/parser/js/js-parser-statm.c b/jerry-core/parser/js/js-parser-statm.c index daaac713b..519df5ae8 100644 --- a/jerry-core/parser/js/js-parser-statm.c +++ b/jerry-core/parser/js/js-parser-statm.c @@ -31,14 +31,6 @@ * @{ */ -/** - * @{ - * Strict mode string literal in directive prologues - */ -#define PARSER_USE_STRICT_LITERAL "use strict" -#define PARSER_USE_STRICT_LENGTH 10 -/** @} */ - /** * Parser statement types. * @@ -2588,35 +2580,24 @@ parser_parse_statements (parser_context_t *context_p) /**< context */ && context_p->token.lit_location.type == LEXER_STRING_LITERAL) { lexer_lit_location_t lit_location; - uint32_t status_flags = context_p->status_flags; + bool is_use_strict = false; JERRY_ASSERT (context_p->stack_depth == JERRY_GET_EXPECTED_DEPTH (context_p)); #ifndef JERRY_NDEBUG JERRY_ASSERT (context_p->context_stack_depth == context_p->stack_depth); #endif /* !JERRY_NDEBUG */ - lit_location = context_p->token.lit_location; - - if (lit_location.length == PARSER_USE_STRICT_LENGTH - && !lit_location.has_escape - && memcmp (PARSER_USE_STRICT_LITERAL, lit_location.char_p, PARSER_USE_STRICT_LENGTH) == 0) + if (lexer_string_is_use_strict (context_p)) { - context_p->status_flags |= PARSER_IS_STRICT; + is_use_strict = true; } + lit_location = context_p->token.lit_location; lexer_next_token (context_p); - if (context_p->token.type != LEXER_SEMICOLON - && context_p->token.type != LEXER_RIGHT_BRACE - && (!(context_p->token.flags & LEXER_WAS_NEWLINE) - || LEXER_IS_BINARY_OP_TOKEN (context_p->token.type) - || context_p->token.type == LEXER_LEFT_PAREN - || context_p->token.type == LEXER_LEFT_SQUARE - || context_p->token.type == LEXER_DOT)) + if (!lexer_string_is_directive (context_p)) { /* The string is part of an expression statement. */ - context_p->status_flags = status_flags; - #if ENABLED (JERRY_DEBUGGER) if (JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED) { @@ -2642,14 +2623,24 @@ parser_parse_statements (parser_context_t *context_p) /**< context */ break; } -#if ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) - if (context_p->is_show_opcodes - && !(status_flags & PARSER_IS_STRICT) - && (context_p->status_flags & PARSER_IS_STRICT)) + if (is_use_strict) { - JERRY_DEBUG_MSG (" Note: switch to strict mode\n\n"); - } +#if ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) + if (context_p->is_show_opcodes + && !(context_p->status_flags & PARSER_IS_STRICT)) + { + JERRY_DEBUG_MSG (" Note: switch to strict mode\n\n"); + } #endif /* ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) */ +#if ENABLED (JERRY_ES2015) + if (context_p->status_flags & PARSER_FUNCTION_HAS_NON_SIMPLE_PARAM) + { + parser_raise_error (context_p, PARSER_ERR_USE_STRICT_NOT_ALLOWED); + } +#endif /* ENABLED (JERRY_ES2015) */ + + context_p->status_flags |= PARSER_IS_STRICT; + } if (context_p->token.type == LEXER_SEMICOLON) { @@ -2665,6 +2656,7 @@ parser_parse_statements (parser_context_t *context_p) /**< context */ parser_emit_cbc_literal_from_token (context_p, CBC_PUSH_LITERAL); parser_emit_cbc (context_p, CBC_POP_BLOCK); parser_flush_cbc (context_p); + break; } } diff --git a/jerry-core/parser/js/js-parser-util.c b/jerry-core/parser/js/js-parser-util.c index a146d6a28..8e63786c7 100644 --- a/jerry-core/parser/js/js-parser-util.c +++ b/jerry-core/parser/js/js-parser-util.c @@ -937,6 +937,10 @@ parser_error_to_string (parser_error_t error) /**< error code */ return "Arguments is not allowed to be used here in strict mode."; } #if ENABLED (JERRY_ES2015) + case PARSER_ERR_USE_STRICT_NOT_ALLOWED: + { + return "The 'use strict' directive is not allowed for functions with non-simple arguments."; + } case PARSER_ERR_YIELD_NOT_ALLOWED: { return "Incorrect use of yield keyword."; diff --git a/jerry-core/parser/js/js-parser.h b/jerry-core/parser/js/js-parser.h index f35a6fae7..505d2c4f7 100644 --- a/jerry-core/parser/js/js-parser.h +++ b/jerry-core/parser/js/js-parser.h @@ -75,6 +75,7 @@ typedef enum PARSER_ERR_EVAL_NOT_ALLOWED, /**< eval is not allowed here in strict mode */ PARSER_ERR_ARGUMENTS_NOT_ALLOWED, /**< arguments is not allowed here in strict mode */ #if ENABLED (JERRY_ES2015) + PARSER_ERR_USE_STRICT_NOT_ALLOWED, /**< use strict directive is not allowed */ PARSER_ERR_YIELD_NOT_ALLOWED, /**< yield keyword is not allowed */ #endif /* ENABLED (JERRY_ES2015) */ PARSER_ERR_DELETE_IDENT_NOT_ALLOWED, /**< identifier delete is not allowed in strict mode */ diff --git a/jerry-core/parser/js/js-scanner-internal.h b/jerry-core/parser/js/js-scanner-internal.h index ba79f7c3f..6a05279f7 100644 --- a/jerry-core/parser/js/js-scanner-internal.h +++ b/jerry-core/parser/js/js-scanner-internal.h @@ -26,6 +26,96 @@ * @{ */ +/** + * Scan mode types types. + */ +typedef enum +{ + SCAN_MODE_PRIMARY_EXPRESSION, /**< scanning primary expression */ + SCAN_MODE_PRIMARY_EXPRESSION_AFTER_NEW, /**< scanning primary expression after new */ + SCAN_MODE_POST_PRIMARY_EXPRESSION, /**< scanning post primary expression */ + SCAN_MODE_PRIMARY_EXPRESSION_END, /**< scanning primary expression end */ + SCAN_MODE_STATEMENT, /**< scanning statement */ + SCAN_MODE_STATEMENT_OR_TERMINATOR, /**< scanning statement or statement end */ + SCAN_MODE_STATEMENT_END, /**< scanning statement end */ + SCAN_MODE_VAR_STATEMENT, /**< scanning var statement */ + SCAN_MODE_PROPERTY_NAME, /**< scanning property name */ + SCAN_MODE_FUNCTION_ARGUMENTS, /**< scanning function arguments */ +#if ENABLED (JERRY_ES2015) + SCAN_MODE_CONTINUE_FUNCTION_ARGUMENTS, /**< continue scanning function arguments */ + SCAN_MODE_BINDING, /**< array or object binding */ + SCAN_MODE_CLASS_DECLARATION, /**< scanning class declaration */ + SCAN_MODE_CLASS_METHOD, /**< scanning class method */ +#endif /* ENABLED (JERRY_ES2015) */ +} scan_modes_t; + +/** + * Scan stack mode types types. + */ +typedef enum +{ + SCAN_STACK_SCRIPT, /**< script */ + SCAN_STACK_SCRIPT_FUNCTION, /**< script is a function body */ + SCAN_STACK_BLOCK_STATEMENT, /**< block statement group */ + SCAN_STACK_FUNCTION_STATEMENT, /**< function statement */ + SCAN_STACK_FUNCTION_EXPRESSION, /**< function expression */ + SCAN_STACK_FUNCTION_PROPERTY, /**< function expression in an object literal or class */ +#if ENABLED (JERRY_ES2015) + SCAN_STACK_FUNCTION_ARROW, /**< arrow function expression */ +#endif /* ENABLED (JERRY_ES2015) */ + SCAN_STACK_SWITCH_BLOCK, /**< block part of "switch" statement */ + SCAN_STACK_IF_STATEMENT, /**< statement part of "if" statements */ + SCAN_STACK_WITH_STATEMENT, /**< statement part of "with" statements */ + SCAN_STACK_WITH_EXPRESSION, /**< expression part of "with" statements */ + SCAN_STACK_DO_STATEMENT, /**< statement part of "do" statements */ + SCAN_STACK_DO_EXPRESSION, /**< expression part of "do" statements */ + SCAN_STACK_WHILE_EXPRESSION, /**< expression part of "while" iterator */ + SCAN_STACK_PAREN_EXPRESSION, /**< expression in brackets */ + SCAN_STACK_STATEMENT_WITH_EXPR, /**< statement which starts with expression enclosed in brackets */ +#if ENABLED (JERRY_ES2015) + SCAN_STACK_BINDING_INIT, /**< post processing after a single initializer */ + SCAN_STACK_BINDING_LIST_INIT, /**< post processing after an initializer list */ + SCAN_STACK_LET, /**< let statement */ + SCAN_STACK_CONST, /**< const statement */ +#endif /* ENABLED (JERRY_ES2015) */ + /* The SCANNER_IS_FOR_START macro needs to be updated when the following constants are reordered. */ + SCAN_STACK_VAR, /**< var statement */ + SCAN_STACK_FOR_VAR_START, /**< start of "for" iterator with var statement */ +#if ENABLED (JERRY_ES2015) + SCAN_STACK_FOR_LET_START, /**< start of "for" iterator with let statement */ + SCAN_STACK_FOR_CONST_START, /**< start of "for" iterator with const statement */ +#endif /* ENABLED (JERRY_ES2015) */ + SCAN_STACK_FOR_START, /**< start of "for" iterator */ + SCAN_STACK_FOR_CONDITION, /**< condition part of "for" iterator */ + SCAN_STACK_FOR_EXPRESSION, /**< expression part of "for" iterator */ + SCAN_STACK_SWITCH_EXPRESSION, /**< expression part of "switch" statement */ + SCAN_STACK_CASE_STATEMENT, /**< case statement inside a switch statement */ + SCAN_STACK_COLON_EXPRESSION, /**< expression between a question mark and colon */ + SCAN_STACK_TRY_STATEMENT, /**< try statement */ + SCAN_STACK_CATCH_STATEMENT, /**< catch statement */ + SCAN_STACK_ARRAY_LITERAL, /**< array literal or destructuring assignment or binding */ + SCAN_STACK_OBJECT_LITERAL, /**< object literal group */ + SCAN_STACK_PROPERTY_ACCESSOR, /**< property accessor in squarey brackets */ +#if ENABLED (JERRY_ES2015) + SCAN_STACK_COMPUTED_PROPERTY, /**< computed property name */ + SCAN_STACK_COMPUTED_GENERATOR_FUNCTION, /**< computed property name */ + SCAN_STACK_TEMPLATE_STRING, /**< template string */ + SCAN_STACK_FOR_BLOCK_END, /**< end of "for" statement with let/const declaration */ + SCAN_STACK_ARROW_ARGUMENTS, /**< might be arguments of an arrow function */ + SCAN_STACK_ARROW_EXPRESSION, /**< expression body of an arrow function */ + SCAN_STACK_CLASS_STATEMENT, /**< class statement */ + SCAN_STACK_CLASS_EXPRESSION, /**< class expression */ + SCAN_STACK_CLASS_EXTENDS, /**< class extends expression */ + SCAN_STACK_FUNCTION_PARAMETERS, /**< function parameter initializer */ +#endif /* ENABLED (JERRY_ES2015) */ +} scan_stack_modes_t; + +/** + * Checks whether the stack top is a for statement start. + */ +#define SCANNER_IS_FOR_START(stack_top) \ + ((stack_top) >= SCAN_STACK_FOR_VAR_START && (stack_top) <= SCAN_STACK_FOR_START) + /** * Generic descriptor which stores only the start position. */ @@ -167,17 +257,21 @@ typedef enum { SCANNER_LITERAL_POOL_FUNCTION = (1 << 0), /**< literal pool represents a function */ SCANNER_LITERAL_POOL_BLOCK = (1 << 1), /**< literal pool represents a code block */ - SCANNER_LITERAL_POOL_NO_REG = (1 << 2), /**< variable declarations cannot be kept in registers */ + SCANNER_LITERAL_POOL_IS_STRICT = (1 << 2), /**< literal pool represents a strict mode code block */ + SCANNER_LITERAL_POOL_NO_REG = (1 << 3), /**< variable declarations cannot be kept in registers */ #if ENABLED (JERRY_ES2015) - SCANNER_LITERAL_POOL_NO_VAR_REG = (1 << 3), /**< non let/const declarations cannot be kept in registers */ + SCANNER_LITERAL_POOL_NO_VAR_REG = (1 << 4), /**< non let/const declarations cannot be kept in registers */ #endif /* ENABLED (JERRY_ES2015) */ - SCANNER_LITERAL_POOL_NO_ARGUMENTS = (1 << 4), /**< arguments object should not be constructed */ - SCANNER_LITERAL_POOL_IN_WITH = (1 << 5), /**< literal pool is in a with statement */ + SCANNER_LITERAL_POOL_NO_ARGUMENTS = (1 << 5), /**< arguments object must not be constructed */ +#if ENABLED (JERRY_ES2015) + SCANNER_LITERAL_POOL_ARGUMENTS_UNMAPPED = (1 << 6), /**< arguments object should be unmapped */ +#endif /* ENABLED (JERRY_ES2015) */ + SCANNER_LITERAL_POOL_IN_WITH = (1 << 7), /**< literal pool is in a with statement */ #if ENABLED (JERRY_ES2015_MODULE_SYSTEM) - SCANNER_LITERAL_POOL_IN_EXPORT = (1 << 6), /**< the declared variables are exported by the module system */ + SCANNER_LITERAL_POOL_IN_EXPORT = (1 << 8), /**< the declared variables are exported by the module system */ #endif /* ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ #if ENABLED (JERRY_ES2015) - SCANNER_LITERAL_POOL_GENERATOR = (1 << 7), /**< generator function */ + SCANNER_LITERAL_POOL_GENERATOR = (1 << 9), /**< generator function */ #endif /* ENABLED (JERRY_ES2015) */ } scanner_literal_pool_flags_t; @@ -204,6 +298,7 @@ typedef struct scanner_literal_pool_t */ struct scanner_context_t { + uint32_t context_status_flags; /**< original status flags of the context */ uint8_t mode; /**< scanner mode */ #if ENABLED (JERRY_DEBUGGER) uint8_t debugger_enabled; /**< debugger is enabled */ @@ -236,6 +331,7 @@ void scanner_pop_literal_pool (parser_context_t *context_p, scanner_context_t *s void scanner_construct_global_block (parser_context_t *context_p, scanner_context_t *scanner_context_p); #endif /* ENABLED (JERRY_ES2015) */ void scanner_filter_arguments (parser_context_t *context_p, scanner_context_t *scanner_context_p); +void scanner_check_directives (parser_context_t *context_p, scanner_context_t *scanner_context_p); lexer_lit_location_t *scanner_add_custom_literal (parser_context_t *context_p, scanner_literal_pool_t *literal_pool_p, const lexer_lit_location_t *literal_location_p); lexer_lit_location_t *scanner_add_literal (parser_context_t *context_p, scanner_context_t *scanner_context_p); diff --git a/jerry-core/parser/js/js-scanner-util.c b/jerry-core/parser/js/js-scanner-util.c index c85af9ad1..d73b152c7 100644 --- a/jerry-core/parser/js/js-scanner-util.c +++ b/jerry-core/parser/js/js-scanner-util.c @@ -412,6 +412,15 @@ scanner_push_literal_pool (parser_context_t *context_p, /**< context */ status_flags |= (uint16_t) (prev_literal_pool_p->status_flags & copied_flags); } + if (prev_literal_pool_p != NULL) + { + const uint16_t copied_flags = SCANNER_LITERAL_POOL_IS_STRICT; + status_flags |= (uint16_t) (prev_literal_pool_p->status_flags & copied_flags); + + /* The logical value of these flags must be the same. */ + JERRY_ASSERT (!(status_flags & SCANNER_LITERAL_POOL_IS_STRICT) == !(context_p->status_flags & PARSER_IS_STRICT)); + } + parser_list_init (&literal_pool_p->literal_pool, sizeof (lexer_lit_location_t), (uint32_t) ((128 - sizeof (void *)) / sizeof (lexer_lit_location_t))); @@ -636,6 +645,17 @@ scanner_pop_literal_pool (parser_context_t *context_p, /**< context */ { no_declarations++; } + +#if ENABLED (JERRY_ES2015) + const uint16_t is_unmapped = SCANNER_LITERAL_POOL_IS_STRICT | SCANNER_LITERAL_POOL_ARGUMENTS_UNMAPPED; +#else /* !ENABLED (JERRY_ES2015) */ + const uint16_t is_unmapped = SCANNER_LITERAL_POOL_IS_STRICT; +#endif /* ENABLED (JERRY_ES2015) */ + + if (literal_pool_p->status_flags & is_unmapped) + { + arguments_required = false; + } } info_p->u8_arg = status_flags; @@ -782,9 +802,18 @@ scanner_pop_literal_pool (parser_context_t *context_p, /**< context */ prev_literal_pool_p->no_declarations = (uint16_t) no_declarations; } -#if ENABLED (JERRY_ES2015) if (is_function && prev_literal_pool_p != NULL) { + if (prev_literal_pool_p->status_flags & SCANNER_LITERAL_POOL_IS_STRICT) + { + context_p->status_flags |= PARSER_IS_STRICT; + } + else + { + context_p->status_flags &= (uint32_t) ~PARSER_IS_STRICT; + } + +#if ENABLED (JERRY_ES2015) if (prev_literal_pool_p->status_flags & SCANNER_LITERAL_POOL_GENERATOR) { context_p->status_flags |= PARSER_IS_GENERATOR_FUNCTION; @@ -793,8 +822,8 @@ scanner_pop_literal_pool (parser_context_t *context_p, /**< context */ { context_p->status_flags &= (uint32_t) ~PARSER_IS_GENERATOR_FUNCTION; } - } #endif /* ENABLED (JERRY_ES2015) */ + } scanner_context_p->active_literal_pool_p = literal_pool_p->prev_p; @@ -955,6 +984,47 @@ scanner_filter_arguments (parser_context_t *context_p, /**< context */ scanner_free (literal_pool_p, sizeof (scanner_literal_pool_t)); } /* scanner_filter_arguments */ +/** + * Check directives before a source block. + */ +void +scanner_check_directives (parser_context_t *context_p, /**< context */ + scanner_context_t *scanner_context_p) /**< scanner context */ +{ + scanner_context_p->mode = SCAN_MODE_STATEMENT_OR_TERMINATOR; + + while (context_p->token.type == LEXER_LITERAL + && context_p->token.lit_location.type == LEXER_STRING_LITERAL) + { + bool is_use_strict = false; + + if (lexer_string_is_use_strict (context_p)) + { + is_use_strict = true; + } + + lexer_next_token (context_p); + + if (!lexer_string_is_directive (context_p)) + { + /* The string is part of an expression statement. */ + scanner_context_p->mode = SCAN_MODE_POST_PRIMARY_EXPRESSION; + break; + } + + if (is_use_strict) + { + context_p->status_flags |= PARSER_IS_STRICT; + scanner_context_p->active_literal_pool_p->status_flags |= SCANNER_LITERAL_POOL_IS_STRICT; + } + + if (context_p->token.type == LEXER_SEMICOLON) + { + lexer_next_token (context_p); + } + } +} /* scanner_check_directives */ + /** * Add any literal to the specified literal pool. * diff --git a/jerry-core/parser/js/js-scanner.c b/jerry-core/parser/js/js-scanner.c index fc721a4a2..f3d15d0f5 100644 --- a/jerry-core/parser/js/js-scanner.c +++ b/jerry-core/parser/js/js-scanner.c @@ -30,96 +30,6 @@ * @{ */ -/** - * Scan mode types types. - */ -typedef enum -{ - SCAN_MODE_PRIMARY_EXPRESSION, /**< scanning primary expression */ - SCAN_MODE_PRIMARY_EXPRESSION_AFTER_NEW, /**< scanning primary expression after new */ - SCAN_MODE_POST_PRIMARY_EXPRESSION, /**< scanning post primary expression */ - SCAN_MODE_PRIMARY_EXPRESSION_END, /**< scanning primary expression end */ - SCAN_MODE_STATEMENT, /**< scanning statement */ - SCAN_MODE_STATEMENT_OR_TERMINATOR, /**< scanning statement or statement end */ - SCAN_MODE_STATEMENT_END, /**< scanning statement end */ - SCAN_MODE_VAR_STATEMENT, /**< scanning var statement */ - SCAN_MODE_PROPERTY_NAME, /**< scanning property name */ - SCAN_MODE_FUNCTION_ARGUMENTS, /**< scanning function arguments */ -#if ENABLED (JERRY_ES2015) - SCAN_MODE_CONTINUE_FUNCTION_ARGUMENTS, /**< continue scanning function arguments */ - SCAN_MODE_BINDING, /**< array or object binding */ - SCAN_MODE_CLASS_DECLARATION, /**< scanning class declaration */ - SCAN_MODE_CLASS_METHOD, /**< scanning class method */ -#endif /* ENABLED (JERRY_ES2015) */ -} scan_modes_t; - -/** - * Scan stack mode types types. - */ -typedef enum -{ - SCAN_STACK_SCRIPT, /**< script */ - SCAN_STACK_SCRIPT_FUNCTION, /**< script is a function body */ - SCAN_STACK_BLOCK_STATEMENT, /**< block statement group */ - SCAN_STACK_FUNCTION_STATEMENT, /**< function statement */ - SCAN_STACK_FUNCTION_EXPRESSION, /**< function expression */ - SCAN_STACK_FUNCTION_PROPERTY, /**< function expression in an object literal or class */ -#if ENABLED (JERRY_ES2015) - SCAN_STACK_FUNCTION_ARROW, /**< arrow function expression */ -#endif /* ENABLED (JERRY_ES2015) */ - SCAN_STACK_SWITCH_BLOCK, /**< block part of "switch" statement */ - SCAN_STACK_IF_STATEMENT, /**< statement part of "if" statements */ - SCAN_STACK_WITH_STATEMENT, /**< statement part of "with" statements */ - SCAN_STACK_WITH_EXPRESSION, /**< expression part of "with" statements */ - SCAN_STACK_DO_STATEMENT, /**< statement part of "do" statements */ - SCAN_STACK_DO_EXPRESSION, /**< expression part of "do" statements */ - SCAN_STACK_WHILE_EXPRESSION, /**< expression part of "while" iterator */ - SCAN_STACK_PAREN_EXPRESSION, /**< expression in brackets */ - SCAN_STACK_STATEMENT_WITH_EXPR, /**< statement which starts with expression enclosed in brackets */ -#if ENABLED (JERRY_ES2015) - SCAN_STACK_BINDING_INIT, /**< post processing after a single initializer */ - SCAN_STACK_BINDING_LIST_INIT, /**< post processing after an initializer list */ - SCAN_STACK_LET, /**< let statement */ - SCAN_STACK_CONST, /**< const statement */ -#endif /* ENABLED (JERRY_ES2015) */ - /* The SCANNER_IS_FOR_START macro needs to be updated when the following constants are reordered. */ - SCAN_STACK_VAR, /**< var statement */ - SCAN_STACK_FOR_VAR_START, /**< start of "for" iterator with var statement */ -#if ENABLED (JERRY_ES2015) - SCAN_STACK_FOR_LET_START, /**< start of "for" iterator with let statement */ - SCAN_STACK_FOR_CONST_START, /**< start of "for" iterator with const statement */ -#endif /* ENABLED (JERRY_ES2015) */ - SCAN_STACK_FOR_START, /**< start of "for" iterator */ - SCAN_STACK_FOR_CONDITION, /**< condition part of "for" iterator */ - SCAN_STACK_FOR_EXPRESSION, /**< expression part of "for" iterator */ - SCAN_STACK_SWITCH_EXPRESSION, /**< expression part of "switch" statement */ - SCAN_STACK_CASE_STATEMENT, /**< case statement inside a switch statement */ - SCAN_STACK_COLON_EXPRESSION, /**< expression between a question mark and colon */ - SCAN_STACK_TRY_STATEMENT, /**< try statement */ - SCAN_STACK_CATCH_STATEMENT, /**< catch statement */ - SCAN_STACK_ARRAY_LITERAL, /**< array literal or destructuring assignment or binding */ - SCAN_STACK_OBJECT_LITERAL, /**< object literal group */ - SCAN_STACK_PROPERTY_ACCESSOR, /**< property accessor in squarey brackets */ -#if ENABLED (JERRY_ES2015) - SCAN_STACK_COMPUTED_PROPERTY, /**< computed property name */ - SCAN_STACK_COMPUTED_GENERATOR_FUNCTION, /**< computed property name */ - SCAN_STACK_TEMPLATE_STRING, /**< template string */ - SCAN_STACK_FOR_BLOCK_END, /**< end of "for" statement with let/const declaration */ - SCAN_STACK_ARROW_ARGUMENTS, /**< might be arguments of an arrow function */ - SCAN_STACK_ARROW_EXPRESSION, /**< expression body of an arrow function */ - SCAN_STACK_CLASS_STATEMENT, /**< class statement */ - SCAN_STACK_CLASS_EXPRESSION, /**< class expression */ - SCAN_STACK_CLASS_EXTENDS, /**< class extends expression */ - SCAN_STACK_FUNCTION_PARAMETERS, /**< function parameter initializer */ -#endif /* ENABLED (JERRY_ES2015) */ -} scan_stack_modes_t; - -/** - * Checks whether the stack top is a for statement start. - */ -#define SCANNER_IS_FOR_START(stack_top) \ - ((stack_top) >= SCAN_STACK_FOR_VAR_START && (stack_top) <= SCAN_STACK_FOR_START) - /** * Scan mode types types. */ @@ -164,8 +74,8 @@ scanner_check_arrow_body (parser_context_t *context_p, /**< context */ } lexer_next_token (context_p); - scanner_context_p->mode = SCAN_MODE_STATEMENT_OR_TERMINATOR; parser_stack_push_uint8 (context_p, SCAN_STACK_FUNCTION_ARROW); + scanner_check_directives (context_p, scanner_context_p); } /* scanner_check_arrow_body */ /** @@ -1821,7 +1731,7 @@ scanner_scan_statement (parser_context_t *context_p, /**< context */ scanner_raise_error (context_p); } - context_p->status_flags |= PARSER_IS_MODULE; + scanner_context_p->context_status_flags |= PARSER_IS_MODULE; scanner_context_p->mode = SCAN_MODE_STATEMENT_END; lexer_next_token (context_p); @@ -1989,7 +1899,7 @@ scanner_scan_statement (parser_context_t *context_p, /**< context */ scanner_raise_error (context_p); } - context_p->status_flags |= PARSER_IS_MODULE; + scanner_context_p->context_status_flags |= PARSER_IS_MODULE; lexer_next_token (context_p); @@ -2587,7 +2497,7 @@ scanner_scan_statement_end (parser_context_t *context_p, /**< context */ /** * Scan the whole source code. */ -void +void JERRY_ATTR_NOINLINE scanner_scan_all (parser_context_t *context_p, /**< context */ const uint8_t *arg_list_p, /**< function argument list */ const uint8_t *arg_list_end_p, /**< end of argument list */ @@ -2603,6 +2513,7 @@ scanner_scan_all (parser_context_t *context_p, /**< context */ } #endif /* ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) */ + scanner_context.context_status_flags = context_p->status_flags; #if ENABLED (JERRY_DEBUGGER) scanner_context.debugger_enabled = (JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED) ? 1 : 0; #endif /* ENABLED (JERRY_DEBUGGER) */ @@ -2635,20 +2546,32 @@ scanner_scan_all (parser_context_t *context_p, /**< context */ uint16_t status_flags = SCANNER_LITERAL_POOL_FUNCTION_WITHOUT_ARGUMENTS | SCANNER_LITERAL_POOL_NO_REG; #endif /* ENABLED (JERRY_DEBUGGER) */ + if (context_p->status_flags & PARSER_IS_STRICT) + { + status_flags |= SCANNER_LITERAL_POOL_IS_STRICT; + } + scanner_literal_pool_t *literal_pool_p = scanner_push_literal_pool (context_p, &scanner_context, status_flags); literal_pool_p->source_p = source_p; - scanner_context.mode = SCAN_MODE_STATEMENT_OR_TERMINATOR; parser_stack_push_uint8 (context_p, SCAN_STACK_SCRIPT); lexer_next_token (context_p); + scanner_check_directives (context_p, &scanner_context); } else { context_p->source_p = arg_list_p; context_p->source_end_p = arg_list_end_p; - scanner_push_literal_pool (context_p, &scanner_context, SCANNER_LITERAL_POOL_FUNCTION); + uint16_t status_flags = SCANNER_LITERAL_POOL_FUNCTION; + + if (context_p->status_flags & PARSER_IS_STRICT) + { + status_flags |= SCANNER_LITERAL_POOL_IS_STRICT; + } + + scanner_push_literal_pool (context_p, &scanner_context, status_flags); scanner_context.mode = SCAN_MODE_FUNCTION_ARGUMENTS; parser_stack_push_uint8 (context_p, SCAN_STACK_SCRIPT_FUNCTION); @@ -2997,6 +2920,8 @@ scanner_scan_all (parser_context_t *context_p, /**< context */ #if ENABLED (JERRY_ES2015) if (context_p->token.type == LEXER_THREE_DOTS) { + scanner_context.active_literal_pool_p->status_flags |= SCANNER_LITERAL_POOL_ARGUMENTS_UNMAPPED; + lexer_next_token (context_p); } @@ -3028,6 +2953,8 @@ scanner_scan_all (parser_context_t *context_p, /**< context */ #if ENABLED (JERRY_ES2015) if (is_destructuring_binding) { + scanner_context.active_literal_pool_p->status_flags |= SCANNER_LITERAL_POOL_ARGUMENTS_UNMAPPED; + parser_stack_push_uint8 (context_p, SCAN_STACK_FUNCTION_PARAMETERS); scanner_append_hole (context_p, &scanner_context); scanner_push_destructuring_pattern (context_p, &scanner_context, SCANNER_BINDING_ARG, false); @@ -3046,6 +2973,8 @@ scanner_scan_all (parser_context_t *context_p, /**< context */ if (context_p->token.type == LEXER_ASSIGN) { + scanner_context.active_literal_pool_p->status_flags |= SCANNER_LITERAL_POOL_ARGUMENTS_UNMAPPED; + parser_stack_push_uint8 (context_p, SCAN_STACK_FUNCTION_PARAMETERS); scanner_context.mode = SCAN_MODE_PRIMARY_EXPRESSION; break; @@ -3067,8 +2996,8 @@ scanner_scan_all (parser_context_t *context_p, /**< context */ context_p->line = 1; context_p->column = 1; - scanner_context.mode = SCAN_MODE_STATEMENT_OR_TERMINATOR; lexer_next_token (context_p); + scanner_check_directives (context_p, &scanner_context); continue; } @@ -3085,8 +3014,9 @@ scanner_scan_all (parser_context_t *context_p, /**< context */ } scanner_filter_arguments (context_p, &scanner_context); - scanner_context.mode = SCAN_MODE_STATEMENT_OR_TERMINATOR; - break; + lexer_next_token (context_p); + scanner_check_directives (context_p, &scanner_context); + continue; } case SCAN_MODE_PROPERTY_NAME: { @@ -3369,7 +3299,7 @@ scan_completed: JERRY_ASSERT (scanner_context.active_literal_pool_p == NULL); #ifndef JERRY_NDEBUG - context_p->status_flags |= PARSER_SCANNING_SUCCESSFUL; + scanner_context.context_status_flags |= PARSER_SCANNING_SUCCESSFUL; #endif /* !JERRY_NDEBUG */ } PARSER_CATCH @@ -3417,6 +3347,7 @@ scan_completed: } PARSER_TRY_END + context_p->status_flags = scanner_context.context_status_flags; scanner_reverse_info_list (context_p); #if ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) diff --git a/tests/jerry/es2015/directive.js b/tests/jerry/es2015/directive.js new file mode 100644 index 000000000..e275e1b49 --- /dev/null +++ b/tests/jerry/es2015/directive.js @@ -0,0 +1,35 @@ +// Copyright JS Foundation and other contributors, http://js.foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +function check_syntax_error (code) +{ + try { + eval (code) + assert (false) + } catch (e) { + assert (e instanceof SyntaxError) + } +} + +eval("function f(a, b = 4) { }") +check_syntax_error ("function f(a, b = 4) { 'use strict' }") + +eval('function f(...a) { }') +check_syntax_error ('function f(...a) { "use strict" }') + +eval("({ f([a,b]) { } })") +check_syntax_error ("({ f([a,b]) { 'use strict' } })") + +eval("function f(a, b = 4) { 'directive1'\n'directive2'\n }") +check_syntax_error ("function f(a, b = 4) { 'directive1'\n'directive2'\n'use strict' }")