Skip to content

Commit 22eabd0

Browse files
zherczegdbatyai
authored andcommitted
Allow an extra comma before the end of a function call. (#3520)
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
1 parent daf3b36 commit 22eabd0

2 files changed

Lines changed: 81 additions & 22 deletions

File tree

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

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,43 +1894,64 @@ parser_process_unary_expression (parser_context_t *context_p, /**< context */
18941894
}
18951895
else
18961896
{
1897-
#endif /* ENABLED (JERRY_ES2015) */
18981897
lexer_next_token (context_p);
18991898

1900-
if (context_p->token.type != LEXER_RIGHT_PAREN)
1899+
while (context_p->token.type != LEXER_RIGHT_PAREN)
19011900
{
1902-
while (true)
1901+
if (++call_arguments > CBC_MAXIMUM_BYTE_VALUE)
19031902
{
1904-
if (++call_arguments > CBC_MAXIMUM_BYTE_VALUE)
1905-
{
1906-
parser_raise_error (context_p, PARSER_ERR_ARGUMENT_LIMIT_REACHED);
1907-
}
1903+
parser_raise_error (context_p, PARSER_ERR_ARGUMENT_LIMIT_REACHED);
1904+
}
19081905

1909-
#if ENABLED (JERRY_ES2015)
1910-
if (context_p->token.type == LEXER_THREE_DOTS)
1911-
{
1912-
has_spread_element = true;
1913-
call_arguments++;
1914-
parser_emit_cbc_ext (context_p, CBC_EXT_PUSH_SPREAD_ELEMENT);
1915-
lexer_next_token (context_p);
1916-
}
1917-
#endif /* ENABLED (JERRY_ES2015) */
1906+
if (context_p->token.type == LEXER_THREE_DOTS)
1907+
{
1908+
has_spread_element = true;
1909+
call_arguments++;
1910+
parser_emit_cbc_ext (context_p, CBC_EXT_PUSH_SPREAD_ELEMENT);
1911+
lexer_next_token (context_p);
1912+
}
19181913

1919-
parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA);
1914+
parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA);
19201915

1921-
if (context_p->token.type != LEXER_COMMA)
1922-
{
1923-
break;
1924-
}
1916+
if (context_p->token.type == LEXER_COMMA)
1917+
{
19251918
lexer_next_token (context_p);
1919+
continue;
19261920
}
19271921

19281922
if (context_p->token.type != LEXER_RIGHT_PAREN)
19291923
{
19301924
parser_raise_error (context_p, PARSER_ERR_RIGHT_PAREN_EXPECTED);
19311925
}
1926+
1927+
break;
1928+
}
1929+
}
1930+
#else /* !ENABLED (JERRY_ES2015) */
1931+
lexer_next_token (context_p);
1932+
1933+
if (context_p->token.type != LEXER_RIGHT_PAREN)
1934+
{
1935+
while (true)
1936+
{
1937+
if (++call_arguments > CBC_MAXIMUM_BYTE_VALUE)
1938+
{
1939+
parser_raise_error (context_p, PARSER_ERR_ARGUMENT_LIMIT_REACHED);
1940+
}
1941+
1942+
parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA);
1943+
1944+
if (context_p->token.type != LEXER_COMMA)
1945+
{
1946+
break;
1947+
}
1948+
lexer_next_token (context_p);
1949+
}
1950+
1951+
if (context_p->token.type != LEXER_RIGHT_PAREN)
1952+
{
1953+
parser_raise_error (context_p, PARSER_ERR_RIGHT_PAREN_EXPECTED);
19321954
}
1933-
#if ENABLED (JERRY_ES2015)
19341955
}
19351956
#endif /* ENABLED (JERRY_ES2015) */
19361957

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 check_syntax_error (code)
16+
{
17+
try {
18+
eval (code)
19+
assert (false)
20+
} catch (e) {
21+
assert (e instanceof SyntaxError)
22+
}
23+
}
24+
25+
function f(...a)
26+
{
27+
return a.length
28+
}
29+
30+
check_syntax_error ("f(,)")
31+
check_syntax_error ("f(,1)")
32+
check_syntax_error ("f(1,,)")
33+
check_syntax_error ("f(1,,2)")
34+
35+
assert(f(10) === 1)
36+
assert(f(10,) === 1)
37+
assert(f(10,11) === 2)
38+
assert(f(10,11,) === 2)

0 commit comments

Comments
 (0)