Skip to content

Commit fe09200

Browse files
authored
Fix assignment lookahead in parser_process_group_expression (#3828)
This patch fixes #3815 and fixes #3819. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent 4660bab commit fe09200

File tree

5 files changed

+72
-24
lines changed

5 files changed

+72
-24
lines changed

jerry-core/parser/js/js-parser-expr.c

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2476,11 +2476,6 @@ parser_append_binary_single_assignment_token (parser_context_t *context_p, /**<
24762476
assign_opcode = CBC_ASSIGN_SET_IDENT;
24772477

24782478
#if ENABLED (JERRY_ES2015)
2479-
if (pattern_flags & PARSER_PATTERN_GROUP_EXPR)
2480-
{
2481-
parser_stack_push_uint8 (context_p, LEXER_ASSIGN_GROUP_EXPR);
2482-
}
2483-
24842479
if (!(pattern_flags & (PARSER_PATTERN_LET | PARSER_PATTERN_CONST | PARSER_PATTERN_LOCAL)))
24852480
{
24862481
if (scanner_literal_is_const_reg (context_p, literal_index))
@@ -3389,7 +3384,7 @@ parser_process_expression_sequence (parser_context_t *context_p) /**< context */
33893384
/**
33903385
* Process group expression.
33913386
*/
3392-
static bool
3387+
static void
33933388
parser_process_group_expression (parser_context_t *context_p, /**< context */
33943389
size_t *grouping_level_p) /**< grouping level */
33953390
{
@@ -3407,21 +3402,17 @@ parser_process_group_expression (parser_context_t *context_p, /**< context */
34073402
parser_stack_pop_uint8 (context_p);
34083403
lexer_next_token (context_p);
34093404

3410-
if (context_p->token.type == LEXER_ASSIGN)
3411-
{
3412-
uint32_t flags = 0;
34133405
#if ENABLED (JERRY_ES2015)
3414-
if (JERRY_UNLIKELY (token == LEXER_LEFT_PAREN))
3415-
{
3416-
flags = PARSER_PATTERN_GROUP_EXPR;
3417-
}
3418-
#endif /* ENABLED (JERRY_ES2015) */
3419-
parser_append_binary_single_assignment_token (context_p, flags);
3420-
lexer_next_token (context_p);
3421-
return true;
3406+
/* Lookahead for anonymous function declaration after '=' token when the assignment base is LHS expression
3407+
with a single indentifier in it. e.g.: (a) = function () {} */
3408+
if (JERRY_UNLIKELY (context_p->token.type == LEXER_ASSIGN
3409+
&& PARSER_IS_PUSH_LITERALS_WITH_THIS (context_p->last_cbc_opcode)
3410+
&& context_p->last_cbc.literal_type == LEXER_IDENT_LITERAL))
3411+
{
3412+
parser_stack_push_uint8 (context_p, LEXER_ASSIGN_GROUP_EXPR);
34223413
}
3414+
#endif /* ENABLED (JERRY_ES2015) */
34233415

3424-
return false;
34253416
} /* parser_process_group_expression */
34263417

34273418
/**
@@ -3482,7 +3473,6 @@ parser_parse_expression (parser_context_t *context_p, /**< context */
34823473

34833474
while (true)
34843475
{
3485-
parse_unary_expression:
34863476
if (parser_parse_unary_expression (context_p, &grouping_level))
34873477
{
34883478
parser_process_binary_opcodes (context_p, 0);
@@ -3529,10 +3519,7 @@ parser_parse_expression (parser_context_t *context_p, /**< context */
35293519
&& (context_p->stack_top_uint8 == LEXER_LEFT_PAREN
35303520
|| context_p->stack_top_uint8 == LEXER_COMMA_SEP_LIST))
35313521
{
3532-
if (parser_process_group_expression (context_p, &grouping_level))
3533-
{
3534-
goto parse_unary_expression;
3535-
}
3522+
parser_process_group_expression (context_p, &grouping_level);
35363523
continue;
35373524
}
35383525

jerry-core/parser/js/js-parser-internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ typedef enum
116116
PARSER_PATTERN_REST_ELEMENT = (1u << 7), /**< parse rest array initializer */
117117
PARSER_PATTERN_ARGUMENTS = (1u << 8), /**< parse arguments binding */
118118
PARSER_PATTERN_ARRAY = (1u << 9), /**< array pattern is being parsed */
119-
PARSER_PATTERN_GROUP_EXPR = (1u << 10), /**< group expression is being assigned */
120119
} parser_pattern_flags_t;
121120

122121
/**
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
try {
16+
typeof (global.v2) = 123;
17+
assert (false);
18+
} catch (e) {
19+
assert (e instanceof ReferenceError);
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
try {
16+
(isNaN(parseFloat("."))) = 'abcd';
17+
assert (false);
18+
} catch (e) {
19+
assert (e instanceof ReferenceError);
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
function a() {}
16+
17+
try {
18+
(a()) = a
19+
assert (false);
20+
} catch (e) {
21+
assert (e instanceof ReferenceError);
22+
}

0 commit comments

Comments
 (0)