Skip to content

Fix assignment lookahead in parser_process_group_expression #3828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 10 additions & 23 deletions jerry-core/parser/js/js-parser-expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -2467,11 +2467,6 @@ parser_append_binary_single_assignment_token (parser_context_t *context_p, /**<
assign_opcode = CBC_ASSIGN_SET_IDENT;

#if ENABLED (JERRY_ES2015)
if (pattern_flags & PARSER_PATTERN_GROUP_EXPR)
{
parser_stack_push_uint8 (context_p, LEXER_ASSIGN_GROUP_EXPR);
}

if (!(pattern_flags & (PARSER_PATTERN_LET | PARSER_PATTERN_CONST | PARSER_PATTERN_LOCAL)))
{
if (scanner_literal_is_const_reg (context_p, literal_index))
Expand Down Expand Up @@ -3380,7 +3375,7 @@ parser_process_expression_sequence (parser_context_t *context_p) /**< context */
/**
* Process group expression.
*/
static bool
static void
parser_process_group_expression (parser_context_t *context_p, /**< context */
size_t *grouping_level_p) /**< grouping level */
{
Expand All @@ -3398,21 +3393,17 @@ parser_process_group_expression (parser_context_t *context_p, /**< context */
parser_stack_pop_uint8 (context_p);
lexer_next_token (context_p);

if (context_p->token.type == LEXER_ASSIGN)
{
uint32_t flags = 0;
#if ENABLED (JERRY_ES2015)
if (JERRY_UNLIKELY (token == LEXER_LEFT_PAREN))
{
flags = PARSER_PATTERN_GROUP_EXPR;
}
#endif /* ENABLED (JERRY_ES2015) */
parser_append_binary_single_assignment_token (context_p, flags);
lexer_next_token (context_p);
return true;
/* Lookahead for anonymous function declaration after '=' token when the assignment base is LHS expression
with a single indentifier in it. e.g.: (a) = function () {} */
if (JERRY_UNLIKELY (context_p->token.type == LEXER_ASSIGN
&& PARSER_IS_PUSH_LITERALS_WITH_THIS (context_p->last_cbc_opcode)
&& context_p->last_cbc.literal_type == LEXER_IDENT_LITERAL))
{
parser_stack_push_uint8 (context_p, LEXER_ASSIGN_GROUP_EXPR);
}
#endif /* ENABLED (JERRY_ES2015) */

return false;
} /* parser_process_group_expression */

/**
Expand Down Expand Up @@ -3473,7 +3464,6 @@ parser_parse_expression (parser_context_t *context_p, /**< context */

while (true)
{
parse_unary_expression:
if (parser_parse_unary_expression (context_p, &grouping_level))
{
parser_process_binary_opcodes (context_p, 0);
Expand Down Expand Up @@ -3520,10 +3510,7 @@ parser_parse_expression (parser_context_t *context_p, /**< context */
&& (context_p->stack_top_uint8 == LEXER_LEFT_PAREN
|| context_p->stack_top_uint8 == LEXER_COMMA_SEP_LIST))
{
if (parser_process_group_expression (context_p, &grouping_level))
{
goto parse_unary_expression;
}
parser_process_group_expression (context_p, &grouping_level);
continue;
}

Expand Down
1 change: 0 additions & 1 deletion jerry-core/parser/js/js-parser-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ typedef enum
PARSER_PATTERN_REST_ELEMENT = (1u << 7), /**< parse rest array initializer */
PARSER_PATTERN_ARGUMENTS = (1u << 8), /**< parse arguments binding */
PARSER_PATTERN_ARRAY = (1u << 9), /**< array pattern is being parsed */
PARSER_PATTERN_GROUP_EXPR = (1u << 10), /**< group expression is being assigned */
} parser_pattern_flags_t;

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/jerry/es2015/regression-test-issue-3819.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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.

try {
typeof (global.v2) = 123;
assert (false);
} catch (e) {
assert (e instanceof ReferenceError);
}
20 changes: 20 additions & 0 deletions tests/jerry/es2015/regression-test-issue-3820.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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.

try {
(isNaN(parseFloat("."))) = 'abcd';
assert (false);
} catch (e) {
assert (e instanceof ReferenceError);
}
22 changes: 22 additions & 0 deletions tests/jerry/regression-test-issue-3815.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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 a() {}

try {
(a()) = a
assert (false);
} catch (e) {
assert (e instanceof ReferenceError);
}