Add validation for single statement lexical declarations (#3326)
This patch fixes #3275 and fixes #3276. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
@@ -477,6 +477,21 @@ parser_pop_block_context (parser_context_t *context_p) /**< context */
|
||||
parser_stack_iterator_init (context_p, &context_p->last_statement);
|
||||
} /* parser_pop_block_context */
|
||||
|
||||
/**
|
||||
* Validate lexical context for a declaration.
|
||||
*/
|
||||
static void
|
||||
parser_validate_lexical_context (parser_context_t *context_p) /**< context */
|
||||
{
|
||||
JERRY_ASSERT (context_p->token.type == LEXER_KEYW_LET
|
||||
|| context_p->token.type == LEXER_KEYW_CONST
|
||||
|| context_p->token.type == LEXER_KEYW_CLASS);
|
||||
|
||||
if (parser_statement_flags[context_p->stack_top_uint8] & PARSER_STATM_SINGLE_STATM)
|
||||
{
|
||||
parser_raise_error (context_p, PARSER_ERR_LEXICAL_SINGLE_STATEMENT);
|
||||
}
|
||||
} /* parser_validate_lexical_context */
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
|
||||
/**
|
||||
@@ -491,6 +506,11 @@ parser_parse_var_statement (parser_context_t *context_p) /**< context */
|
||||
|
||||
#if ENABLED (JERRY_ES2015)
|
||||
uint8_t declaration_type = context_p->token.type;
|
||||
|
||||
if (declaration_type != LEXER_KEYW_VAR)
|
||||
{
|
||||
parser_validate_lexical_context (context_p);
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
|
||||
while (true)
|
||||
@@ -612,6 +632,15 @@ parser_parse_function_statement (parser_context_t *context_p) /**< context */
|
||||
|
||||
JERRY_ASSERT (context_p->token.type == LEXER_KEYW_FUNCTION);
|
||||
|
||||
#if ENABLED (JERRY_ES2015)
|
||||
if ((parser_statement_flags[context_p->stack_top_uint8] & PARSER_STATM_SINGLE_STATM)
|
||||
&& !(context_p->stack_top_uint8 == PARSER_STATEMENT_IF
|
||||
|| context_p->stack_top_uint8 == PARSER_STATEMENT_ELSE))
|
||||
{
|
||||
parser_raise_error (context_p, PARSER_ERR_LEXICAL_SINGLE_STATEMENT);
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
|
||||
#if ENABLED (JERRY_DEBUGGER)
|
||||
parser_line_counter_t debugger_line = context_p->token.line;
|
||||
parser_line_counter_t debugger_column = context_p->token.column;
|
||||
@@ -2636,6 +2665,7 @@ parser_parse_statements (parser_context_t *context_p) /**< context */
|
||||
#if ENABLED (JERRY_ES2015)
|
||||
case LEXER_KEYW_CLASS:
|
||||
{
|
||||
parser_validate_lexical_context (context_p);
|
||||
parser_parse_class (context_p, true);
|
||||
goto consume_last_statement;
|
||||
}
|
||||
|
||||
@@ -1089,6 +1089,10 @@ parser_error_to_string (parser_error_t error) /**< error code */
|
||||
{
|
||||
return "Local variable is redeclared.";
|
||||
}
|
||||
case PARSER_ERR_LEXICAL_SINGLE_STATEMENT:
|
||||
{
|
||||
return "Lexical declaration cannot appear in a single-statement context.";
|
||||
}
|
||||
case PARSER_ERR_MISSING_ASSIGN_AFTER_CONST:
|
||||
{
|
||||
return "Value assignment is expected after a const declaration.";
|
||||
|
||||
@@ -115,6 +115,7 @@ typedef enum
|
||||
PARSER_ERR_OBJECT_PROPERTY_REDEFINED, /**< property of object literal redefined */
|
||||
#if ENABLED (JERRY_ES2015)
|
||||
PARSER_ERR_VARIABLE_REDECLARED, /**< a variable redeclared */
|
||||
PARSER_ERR_LEXICAL_SINGLE_STATEMENT, /**< lexical variable in single statement context */
|
||||
PARSER_ERR_MISSING_ASSIGN_AFTER_CONST, /**< an assignment is required after a const declaration */
|
||||
|
||||
PARSER_ERR_MULTIPLE_CLASS_CONSTRUCTORS, /**< multiple class constructor */
|
||||
|
||||
Reference in New Issue
Block a user