Detect assignment pattern for for-in/of (#4140)

Furthermore do not allow default value for rest parameter

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2020-08-24 14:47:10 +02:00
committed by GitHub
parent 5b3f01af9d
commit 84bc1e03fb
10 changed files with 123 additions and 446 deletions
+16 -8
View File
@@ -3091,10 +3091,7 @@ parser_pattern_get_target (parser_context_t *context_p, /**< context */
}
else
{
if (context_p->next_scanner_info_p->type != SCANNER_TYPE_INITIALIZER)
{
parser_raise_error (context_p, PARSER_ERR_INVALID_DESTRUCTURING_PATTERN);
}
JERRY_ASSERT (context_p->next_scanner_info_p->type == SCANNER_TYPE_INITIALIZER);
scanner_get_location (&start_location, context_p);
scanner_set_location (context_p, &((scanner_location_info_t *) context_p->next_scanner_info_p)->location);
@@ -3205,7 +3202,7 @@ parser_pattern_form_assignment (parser_context_t *context_p, /**< context */
parser_pattern_emit_rhs (context_p, rhs_opcode, literal_index);
if (context_p->token.type == LEXER_ASSIGN)
if (context_p->token.type == LEXER_ASSIGN && !(flags & PARSER_PATTERN_REST_ELEMENT))
{
parser_branch_t skip_init;
lexer_next_token (context_p);
@@ -3273,12 +3270,23 @@ parser_pattern_process_nested_pattern (parser_context_t *context_p, /**< context
| PARSER_PATTERN_LET
| PARSER_PATTERN_CONST
| PARSER_PATTERN_LOCAL
| PARSER_PATTERN_REST_ELEMENT
| PARSER_PATTERN_ARGUMENTS)));
if (context_p->next_scanner_info_p->source_p == context_p->source_p)
JERRY_ASSERT (context_p->next_scanner_info_p->source_p != context_p->source_p
|| context_p->next_scanner_info_p->type == SCANNER_TYPE_INITIALIZER
|| context_p->next_scanner_info_p->type == SCANNER_TYPE_OBJECT_LITERAL_WITH_SUPER);
if (context_p->next_scanner_info_p->source_p == context_p->source_p
&& context_p->next_scanner_info_p->type == SCANNER_TYPE_INITIALIZER)
{
options |= PARSER_PATTERN_TARGET_DEFAULT;
if (!(flags & PARSER_PATTERN_REST_ELEMENT))
{
options |= PARSER_PATTERN_TARGET_DEFAULT;
}
else
{
scanner_release_next (context_p, sizeof (scanner_location_info_t));
}
}
parser_pattern_emit_rhs (context_p, rhs_opcode, literal_index);
+24
View File
@@ -1324,6 +1324,11 @@ parser_parse_for_statement_start (parser_context_t *context_p) /**< context */
if (token_type == LEXER_EOS)
{
lexer_next_token (context_p);
if (context_p->token.type == LEXER_LEFT_SQUARE || context_p->token.type == LEXER_LEFT_BRACE)
{
token_type = context_p->token.type;
}
}
#else /* !ENABLED (JERRY_ESNEXT) */
lexer_next_token (context_p);
@@ -1411,6 +1416,25 @@ parser_parse_for_statement_start (parser_context_t *context_p) /**< context */
#endif /* ENABLED (JERRY_ESNEXT) */
break;
}
#if ENABLED (JERRY_ESNEXT)
case LEXER_LEFT_BRACE:
case LEXER_LEFT_SQUARE:
{
if (context_p->next_scanner_info_p->source_p == context_p->source_p
&& context_p->next_scanner_info_p->type == SCANNER_TYPE_FOR_PATTERN)
{
parser_emit_cbc_ext (context_p, is_for_in ? CBC_EXT_FOR_IN_GET_NEXT
: CBC_EXT_FOR_OF_GET_NEXT);
scanner_release_next (context_p, sizeof (scanner_info_t));
parser_parse_initializer (context_p, PARSER_PATTERN_TARGET_ON_STACK);
/* Pop the value returned by GET_NEXT. */
parser_emit_cbc (context_p, CBC_POP);
break;
}
/* FALLTHRU */
}
#endif /* ENABLED (JERRY_ESNEXT) */
default:
{
uint16_t opcode;
@@ -114,6 +114,7 @@ typedef enum
SCAN_STACK_CLASS_EXPRESSION, /**< class expression */
SCAN_STACK_CLASS_EXTENDS, /**< class extends expression */
SCAN_STACK_FUNCTION_PARAMETERS, /**< function parameter initializer */
SCAN_STACK_FOR_START_PATTERN, /**< possible assignment pattern for "for" iterator */
SCAN_STACK_USE_ASYNC, /**< an "async" identifier is used */
SCAN_STACK_OBJECT_LITERAL_WITH_SUPER, /**< object literal with inner super reference */
#endif /* ENABLED (JERRY_ESNEXT) */
+1
View File
@@ -1679,6 +1679,7 @@ scanner_cleanup (parser_context_t *context_p) /**< context */
#if ENABLED (JERRY_ESNEXT)
JERRY_ASSERT (scanner_info_p->type == SCANNER_TYPE_END_ARGUMENTS
|| scanner_info_p->type == SCANNER_TYPE_LET_EXPRESSION
|| scanner_info_p->type == SCANNER_TYPE_FOR_PATTERN
|| scanner_info_p->type == SCANNER_TYPE_CLASS_CONSTRUCTOR
|| scanner_info_p->type == SCANNER_TYPE_OBJECT_LITERAL_WITH_SUPER
|| scanner_info_p->type == SCANNER_TYPE_ERR_REDECLARED
+30 -3
View File
@@ -873,6 +873,20 @@ scanner_scan_primary_expression_end (parser_context_t *context_p, /**< context *
return SCAN_NEXT_TOKEN;
}
if (context_p->stack_top_uint8 == SCAN_STACK_FOR_START_PATTERN)
{
JERRY_ASSERT (binding_type == SCANNER_BINDING_NONE);
parser_stack_change_last_uint8 (context_p, SCAN_STACK_FOR_START);
if (context_p->token.type == LEXER_KEYW_IN || SCANNER_IDENTIFIER_IS_OF ())
{
scanner_info_t *info_p = scanner_insert_info (context_p, source_start.source_p, sizeof (scanner_info_t));
info_p->type = SCANNER_TYPE_FOR_PATTERN;
return SCAN_KEEP_TOKEN;
}
}
if (context_p->token.type != LEXER_ASSIGN)
{
if (SCANNER_NEEDS_BINDING_LIST (binding_type))
@@ -1243,6 +1257,12 @@ scanner_scan_statement (parser_context_t *context_p, /**< context */
break;
}
#if ENABLED (JERRY_ESNEXT)
case LEXER_LEFT_BRACE:
case LEXER_LEFT_SQUARE:
{
stack_mode = SCAN_STACK_FOR_START_PATTERN;
break;
}
case LEXER_LITERAL:
{
if (!lexer_token_is_let (context_p))
@@ -3515,16 +3535,16 @@ scan_completed:
print_location = true;
break;
}
case SCANNER_TYPE_OBJECT_LITERAL_WITH_SUPER:
case SCANNER_TYPE_FOR_PATTERN:
{
JERRY_DEBUG_MSG (" OBJECT-LITERAL-WITH-SUPER: source:%d\n",
JERRY_DEBUG_MSG (" SCANNER_TYPE_FOR_PATTERN: source:%d\n",
(int) (info_p->source_p - source_start_p));
print_location = false;
break;
}
case SCANNER_TYPE_CLASS_CONSTRUCTOR:
{
JERRY_DEBUG_MSG (" CLASS-CONSTRUCTOR: source:%d\n",
JERRY_DEBUG_MSG (" CLASS_CONSTRUCTOR: source:%d\n",
(int) (info_p->source_p - source_start_p));
print_location = false;
break;
@@ -3547,6 +3567,13 @@ scan_completed:
(int) (info_p->source_p - source_start_p));
break;
}
case SCANNER_TYPE_OBJECT_LITERAL_WITH_SUPER:
{
JERRY_DEBUG_MSG (" OBJECT_LITERAL_WITH_SUPER: source:%d\n",
(int) (info_p->source_p - source_start_p));
print_location = false;
break;
}
#endif /* ENABLED (JERRY_ESNEXT) */
}
+1
View File
@@ -46,6 +46,7 @@ typedef enum
SCANNER_TYPE_CASE, /**< case statement */
#if ENABLED (JERRY_ESNEXT)
SCANNER_TYPE_INITIALIZER, /**< destructuring binding or assignment pattern with initializer */
SCANNER_TYPE_FOR_PATTERN, /**< assignment pattern for for-in or for-of interators */
SCANNER_TYPE_CLASS_CONSTRUCTOR, /**< class constructor */
SCANNER_TYPE_LET_EXPRESSION, /**< let expression */
SCANNER_TYPE_ERR_REDECLARED, /**< syntax error: a variable is redeclared */