diff --git a/jerry-core/parser/js/js-lexer.c b/jerry-core/parser/js/js-lexer.c index ba979d5c3..7594e731f 100644 --- a/jerry-core/parser/js/js-lexer.c +++ b/jerry-core/parser/js/js-lexer.c @@ -1406,6 +1406,28 @@ lexer_check_next_character (parser_context_t *context_p, /**< context */ && context_p->source_p[0] == (uint8_t) character); } /* lexer_check_next_character */ +/** + * Checks whether the next token starts with either specified characters. + * + * @return true - if the next is the specified character + * false - otherwise + */ +bool +lexer_check_next_characters (parser_context_t *context_p, /**< context */ + lit_utf8_byte_t character1, /**< first alternative character */ + lit_utf8_byte_t character2) /**< second alternative character */ +{ + if (!(context_p->token.flags & LEXER_NO_SKIP_SPACES)) + { + lexer_skip_spaces (context_p); + context_p->token.flags = (uint8_t) (context_p->token.flags | LEXER_NO_SKIP_SPACES); + } + + return (context_p->source_p < context_p->source_end_p + && (context_p->source_p[0] == (uint8_t) character1 + || context_p->source_p[0] == (uint8_t) character2)); +} /* lexer_check_next_characters */ + #if ENABLED (JERRY_ES2015) /** diff --git a/jerry-core/parser/js/js-parser-expr.c b/jerry-core/parser/js/js-parser-expr.c index 4faf2cf99..7431b84df 100644 --- a/jerry-core/parser/js/js-parser-expr.c +++ b/jerry-core/parser/js/js-parser-expr.c @@ -1446,8 +1446,7 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */ } case LEXER_KEYW_SUPER: { - if ((lexer_check_next_character (context_p, LIT_CHAR_DOT) - || lexer_check_next_character (context_p, LIT_CHAR_LEFT_SQUARE)) + if (lexer_check_next_characters (context_p, LIT_CHAR_DOT, LIT_CHAR_LEFT_SQUARE) && context_p->status_flags & (PARSER_CLASS_HAS_SUPER)) { if (!LEXER_IS_BINARY_LVALUE_TOKEN (context_p->stack_top_uint8)) diff --git a/jerry-core/parser/js/js-parser-internal.h b/jerry-core/parser/js/js-parser-internal.h index e1fed8e03..95a29b2fa 100644 --- a/jerry-core/parser/js/js-parser-internal.h +++ b/jerry-core/parser/js/js-parser-internal.h @@ -615,6 +615,8 @@ void parser_set_continues_to_current_position (parser_context_t *context_p, pars void lexer_next_token (parser_context_t *context_p); bool lexer_check_next_character (parser_context_t *context_p, lit_utf8_byte_t character); +bool lexer_check_next_characters (parser_context_t *context_p, lit_utf8_byte_t character1, + lit_utf8_byte_t character2); #if ENABLED (JERRY_ES2015) void lexer_skip_empty_statements (parser_context_t *context_p); bool lexer_check_arrow (parser_context_t *context_p); diff --git a/jerry-core/parser/js/js-parser-statm.c b/jerry-core/parser/js/js-parser-statm.c index bdcbdb246..c5cc19ff1 100644 --- a/jerry-core/parser/js/js-parser-statm.c +++ b/jerry-core/parser/js/js-parser-statm.c @@ -496,8 +496,7 @@ parser_parse_var_statement (parser_context_t *context_p) /**< context */ while (true) { #if ENABLED (JERRY_ES2015) - if (lexer_check_next_character (context_p, LIT_CHAR_LEFT_BRACE) - || lexer_check_next_character (context_p, LIT_CHAR_LEFT_SQUARE)) + if (lexer_check_next_characters (context_p, LIT_CHAR_LEFT_SQUARE, LIT_CHAR_LEFT_BRACE)) { lexer_next_token (context_p); parser_pattern_flags_t options = PARSER_PATTERN_BINDING; @@ -1199,14 +1198,40 @@ parser_parse_for_statement_start (parser_context_t *context_p) /**< context */ #endif /* ENABLED (JERRY_ES2015) */ case LEXER_KEYW_VAR: { - uint16_t literal_index; +#if ENABLED (JERRY_ES2015) + if (lexer_check_next_characters (context_p, LIT_CHAR_LEFT_SQUARE, LIT_CHAR_LEFT_BRACE)) + { + bool is_lexical = (context_p->token.type != LEXER_KEYW_VAR); + + lexer_next_token (context_p); + + parser_emit_cbc_ext (context_p, is_for_in ? CBC_EXT_FOR_IN_GET_NEXT + : CBC_EXT_FOR_OF_GET_NEXT); + + if (context_p->next_scanner_info_p->source_p == context_p->source_p) + { + JERRY_ASSERT (context_p->next_scanner_info_p->type == SCANNER_TYPE_INITIALIZER); + + scanner_release_next (context_p, sizeof (scanner_location_info_t)); + } + + uint32_t flags = (PARSER_PATTERN_BINDING | PARSER_PATTERN_TARGET_ON_STACK); + + if (is_lexical) + { + flags |= PARSER_PATTERN_LEXICAL; + } + + parser_parse_initializer (context_p, (parser_pattern_flags_t) flags); + break; + } +#endif /* ENABLED (JERRY_ES2015) */ lexer_expect_identifier (context_p, LEXER_IDENT_LITERAL); JERRY_ASSERT (context_p->token.type == LEXER_LITERAL && context_p->token.lit_location.type == LEXER_IDENT_LITERAL); - literal_index = context_p->lit_object.index; - + uint16_t literal_index = context_p->lit_object.index; lexer_next_token (context_p); if (context_p->token.type == LEXER_ASSIGN) diff --git a/jerry-core/parser/js/js-parser.c b/jerry-core/parser/js/js-parser.c index 1cc439a04..5e207fc75 100644 --- a/jerry-core/parser/js/js-parser.c +++ b/jerry-core/parser/js/js-parser.c @@ -1638,7 +1638,7 @@ parser_parse_function_arguments (parser_context_t *context_p, /**< context */ } else if (context_p->token.type == LEXER_THREE_DOTS) { - lexer_expect_identifier (context_p, LEXER_IDENT_LITERAL); + lexer_next_token (context_p); if (duplicated_argument_names) { @@ -1647,7 +1647,8 @@ parser_parse_function_arguments (parser_context_t *context_p, /**< context */ context_p->status_flags |= PARSER_FUNCTION_HAS_REST_PARAM | PARSER_FUNCTION_HAS_NON_SIMPLE_PARAM; } - else if (context_p->token.type == LEXER_LEFT_SQUARE || context_p->token.type == LEXER_LEFT_BRACE) + + if (context_p->token.type == LEXER_LEFT_SQUARE || context_p->token.type == LEXER_LEFT_BRACE) { if (duplicated_argument_names) { @@ -1668,6 +1669,12 @@ parser_parse_function_arguments (parser_context_t *context_p, /**< context */ if (context_p->next_scanner_info_p->source_p == context_p->source_p) { JERRY_ASSERT (context_p->next_scanner_info_p->type == SCANNER_TYPE_INITIALIZER); + + if (context_p->status_flags & PARSER_FUNCTION_HAS_REST_PARAM) + { + parser_raise_error (context_p, PARSER_ERR_REST_PARAMETER_DEFAULT_INITIALIZER); + } + flags |= PARSER_PATTERN_TARGET_DEFAULT; } diff --git a/jerry-core/parser/js/js-scanner.c b/jerry-core/parser/js/js-scanner.c index 28971c183..ed0bec039 100644 --- a/jerry-core/parser/js/js-scanner.c +++ b/jerry-core/parser/js/js-scanner.c @@ -2439,11 +2439,11 @@ scanner_scan_all (parser_context_t *context_p, /**< context */ { uint8_t binding_type = SCANNER_BINDING_VAR; - if (stack_top == SCAN_STACK_LET) + if (stack_top == SCAN_STACK_LET || stack_top == SCAN_STACK_FOR_LET_START) { binding_type = SCANNER_BINDING_LET; } - else if (stack_top == SCAN_STACK_CONST) + else if (stack_top == SCAN_STACK_CONST || stack_top == SCAN_STACK_FOR_CONST_START) { binding_type = SCANNER_BINDING_CONST; } diff --git a/tests/jerry/es2015/for-pattern.js b/tests/jerry/es2015/for-pattern.js new file mode 100644 index 000000000..bbcb4da75 --- /dev/null +++ b/tests/jerry/es2015/for-pattern.js @@ -0,0 +1,77 @@ +// 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. + +var idx = 0; +for (var [a,b] of [[1,2], [3,4]]) +{ + if (idx == 0) + { + assert(a === 1); + assert(b === 2); + idx = 1; + } + else + { + assert(a === 3); + assert(b === 4); + } +} + +assert(a === 3); +assert(b === 4); + +idx = 0; +for (let [a,b] of [[5,6], [7,8]]) +{ + if (idx == 0) + { + assert(a === 5); + assert(b === 6); + idx = 1; + } + else + { + assert(a === 7); + assert(b === 8); + } +} + +assert(a === 3); +assert(b === 4); + +idx = 0; +for (let [a,b] of [[11,12], [13,14]]) +{ + if (idx == 0) + { + eval("assert(a === 11)"); + eval("assert(b === 12)"); + idx = 1; + } + else + { + eval("assert(a === 13)"); + eval("assert(b === 14)"); + } +} + +assert(a === 3); +assert(b === 4); + +try { + eval("for (let [a,b] = [1,2] of [[3,4]]) {}"); + assert(false); +} catch (e) { + assert(e instanceof SyntaxError); +} diff --git a/tests/jerry/es2015/function-pattern1.js b/tests/jerry/es2015/function-pattern1.js index f8643fc60..e2d70f403 100644 --- a/tests/jerry/es2015/function-pattern1.js +++ b/tests/jerry/es2015/function-pattern1.js @@ -30,6 +30,7 @@ must_throw ("function f([a], a) {}"); must_throw ("function f(a = b, [b]) {}; f()", ReferenceError); must_throw ("function f([a+b]) {}"); must_throw ("function f([a().b]) {}"); +must_throw ("function f(...[a] = [1]) {}"); function a1([a,b]) { var a, b; diff --git a/tests/jerry/es2015/function-pattern2.js b/tests/jerry/es2015/function-pattern2.js index 3508883c1..74bf5d8ce 100644 --- a/tests/jerry/es2015/function-pattern2.js +++ b/tests/jerry/es2015/function-pattern2.js @@ -59,3 +59,12 @@ function k([a = function() { return a; }]) assert(a() === a); } k([]); + +function l(a = 0, ...[b, c = 1, d = 4]) +{ + assert(a === 1); + assert(b === 2); + assert(c === 3); + assert(d === 4); +} +l(1,2,3);