Skip to content

Commit ed2260f

Browse files
committed
Don't continue parsing expressions after ternary operators.
Further benefits: new code requires less checks. Fixes #3841 Fixes #3842 JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent fe09200 commit ed2260f

File tree

3 files changed

+58
-19
lines changed

3 files changed

+58
-19
lines changed

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3304,13 +3304,9 @@ parser_parse_initializer_by_next_char (parser_context_t *context_p, /**< context
33043304

33053305
/**
33063306
* Process ternary expression.
3307-
*
3308-
* @return true - continue with primary expression parsing
3309-
* false - otherwise
33103307
*/
3311-
static bool
3312-
parser_process_ternary_expression (parser_context_t *context_p, /**< context */
3313-
size_t grouping_level) /**< grouping level */
3308+
static void
3309+
parser_process_ternary_expression (parser_context_t *context_p) /**< context */
33143310
{
33153311
JERRY_ASSERT (context_p->token.type == LEXER_QUESTION_MARK);
33163312

@@ -3354,7 +3350,6 @@ parser_process_ternary_expression (parser_context_t *context_p, /**< context */
33543350
parser_flush_cbc (context_p);
33553351

33563352
parser_process_binary_opcodes (context_p, 0);
3357-
return grouping_level >= PARSER_GROUPING_LEVEL_INCREASE;
33583353
} /* parser_process_ternary_expression */
33593354

33603355
/**
@@ -3523,12 +3518,6 @@ parser_parse_expression (parser_context_t *context_p, /**< context */
35233518
continue;
35243519
}
35253520

3526-
if (JERRY_UNLIKELY (context_p->token.type == LEXER_QUESTION_MARK)
3527-
&& (grouping_level != PARSE_EXPR_LEFT_HAND_SIDE)
3528-
&& parser_process_ternary_expression (context_p, grouping_level))
3529-
{
3530-
continue;
3531-
}
35323521
break;
35333522
}
35343523

@@ -3537,19 +3526,29 @@ parser_parse_expression (parser_context_t *context_p, /**< context */
35373526
break;
35383527
}
35393528

3540-
if (JERRY_UNLIKELY (context_p->token.type == LEXER_COMMA)
3541-
&& (!(options & PARSE_EXPR_NO_COMMA) || grouping_level >= PARSER_GROUPING_LEVEL_INCREASE))
3529+
if (JERRY_UNLIKELY (context_p->token.type == LEXER_QUESTION_MARK))
35423530
{
3543-
parser_process_expression_sequence (context_p);
3544-
continue;
3545-
}
3531+
parser_process_ternary_expression (context_p);
35463532

3547-
if (LEXER_IS_BINARY_OP_TOKEN (context_p->token.type))
3533+
if (context_p->token.type == LEXER_RIGHT_PAREN)
3534+
{
3535+
goto process_unary_expression;
3536+
}
3537+
}
3538+
else if (LEXER_IS_BINARY_OP_TOKEN (context_p->token.type))
35483539
{
35493540
parser_append_binary_token (context_p);
35503541
lexer_next_token (context_p);
35513542
continue;
35523543
}
3544+
3545+
if (JERRY_UNLIKELY (context_p->token.type == LEXER_COMMA)
3546+
&& (!(options & PARSE_EXPR_NO_COMMA) || grouping_level >= PARSER_GROUPING_LEVEL_INCREASE))
3547+
{
3548+
parser_process_expression_sequence (context_p);
3549+
continue;
3550+
}
3551+
35533552
break;
35543553
}
35553554

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+
eval('$?$:$=>{ }?{ }:$')
17+
assert(false)
18+
} catch (e) {
19+
assert(e instanceof SyntaxError)
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+
eval('while(0 ? 0 : ()=>{} | {})')
17+
assert(false)
18+
} catch (e) {
19+
assert(e instanceof SyntaxError)
20+
}

0 commit comments

Comments
 (0)