From 85401db5470b834af6bd06e75011567634058568 Mon Sep 17 00:00:00 2001 From: Zoltan Herczeg Date: Wed, 29 Apr 2020 13:50:01 +0200 Subject: [PATCH] Support function statements after 'if' or 'else'. (#3699) JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com --- jerry-core/parser/js/js-parser-statm.c | 82 +++++++++++--- jerry-core/parser/js/js-parser-util.c | 4 + jerry-core/parser/js/js-parser.h | 1 + jerry-core/parser/js/js-scanner-internal.h | 3 +- jerry-core/parser/js/js-scanner-ops.c | 22 ++++ jerry-core/parser/js/js-scanner.c | 22 +++- tests/jerry/es2015/function-if.js | 119 +++++++++++++++++++++ 7 files changed, 233 insertions(+), 20 deletions(-) create mode 100644 tests/jerry/es2015/function-if.js diff --git a/jerry-core/parser/js/js-parser-statm.c b/jerry-core/parser/js/js-parser-statm.c index a900539e1..3fc20b443 100644 --- a/jerry-core/parser/js/js-parser-statm.c +++ b/jerry-core/parser/js/js-parser-statm.c @@ -72,9 +72,10 @@ typedef enum { PARSER_STATM_NO_OPTS = 0, /**< no options */ PARSER_STATM_SINGLE_STATM = (1 << 0), /**< statment can form single statement context */ - PARSER_STATM_BREAK_TARGET = (1 << 1), /**< break target statement */ - PARSER_STATM_CONTINUE_TARGET = (1 << 2), /**< continue target statement */ - PARSER_STATM_CONTEXT_BREAK = (1 << 3), /**< uses another instruction form when crosses their borders */ + PARSER_STATM_HAS_BLOCK = (1 << 1), /**< statement always has a code block */ + PARSER_STATM_BREAK_TARGET = (1 << 2), /**< break target statement */ + PARSER_STATM_CONTINUE_TARGET = (1 << 3), /**< continue target statement */ + PARSER_STATM_CONTEXT_BREAK = (1 << 4), /**< uses another instruction form when crosses their borders */ } parser_statement_flags_t; /** @@ -84,16 +85,16 @@ typedef enum static const uint8_t parser_statement_flags[] = { /* PARSER_STATEMENT_START */ - PARSER_STATM_NO_OPTS, + PARSER_STATM_HAS_BLOCK, /* PARSER_STATEMENT_BLOCK, */ - PARSER_STATM_NO_OPTS, + PARSER_STATM_HAS_BLOCK, #if ENABLED (JERRY_ES2015) /* PARSER_STATEMENT_BLOCK_SCOPE, */ - PARSER_STATM_NO_OPTS, + PARSER_STATM_HAS_BLOCK, /* PARSER_STATEMENT_PRIVATE_SCOPE, */ PARSER_STATM_NO_OPTS, /* PARSER_STATEMENT_BLOCK_CONTEXT, */ - PARSER_STATM_CONTEXT_BREAK, + PARSER_STATM_HAS_BLOCK | PARSER_STATM_CONTEXT_BREAK, /* PARSER_STATEMENT_PRIVATE_CONTEXT, */ PARSER_STATM_NO_OPTS, #endif /* ENABLED (JERRY_ES2015) */ @@ -104,9 +105,9 @@ static const uint8_t parser_statement_flags[] = /* PARSER_STATEMENT_ELSE */ PARSER_STATM_SINGLE_STATM, /* PARSER_STATEMENT_SWITCH */ - PARSER_STATM_BREAK_TARGET, + PARSER_STATM_HAS_BLOCK | PARSER_STATM_BREAK_TARGET, /* PARSER_STATEMENT_SWITCH_NO_DEFAULT */ - PARSER_STATM_BREAK_TARGET, + PARSER_STATM_HAS_BLOCK | PARSER_STATM_BREAK_TARGET, /* PARSER_STATEMENT_DO_WHILE */ PARSER_STATM_BREAK_TARGET | PARSER_STATM_CONTINUE_TARGET | PARSER_STATM_SINGLE_STATM, /* PARSER_STATEMENT_WHILE */ @@ -122,7 +123,7 @@ static const uint8_t parser_statement_flags[] = /* PARSER_STATEMENT_WITH */ PARSER_STATM_CONTEXT_BREAK | PARSER_STATM_SINGLE_STATM, /* PARSER_STATEMENT_TRY */ - PARSER_STATM_CONTEXT_BREAK + PARSER_STATM_HAS_BLOCK | PARSER_STATM_CONTEXT_BREAK }; #if ENABLED (JERRY_ES2015) @@ -613,12 +614,47 @@ parser_parse_function_statement (parser_context_t *context_p) /**< context */ JERRY_ASSERT (context_p->token.type == LEXER_KEYW_FUNCTION); #if ENABLED (JERRY_ES2015) - if ((parser_statement_flags[context_p->stack_top_uint8] & PARSER_STATM_SINGLE_STATM) - && !(context_p->stack_top_uint8 == PARSER_STATEMENT_IF - || context_p->stack_top_uint8 == PARSER_STATEMENT_ELSE - || context_p->stack_top_uint8 == PARSER_STATEMENT_LABEL)) + if (JERRY_UNLIKELY (parser_statement_flags[context_p->stack_top_uint8] & PARSER_STATM_SINGLE_STATM)) { - parser_raise_error (context_p, PARSER_ERR_LEXICAL_SINGLE_STATEMENT); + if (context_p->status_flags & PARSER_IS_STRICT) + { + parser_raise_error (context_p, PARSER_ERR_LEXICAL_SINGLE_STATEMENT); + } + + if (context_p->stack_top_uint8 == PARSER_STATEMENT_IF + || context_p->stack_top_uint8 == PARSER_STATEMENT_ELSE) + { + JERRY_ASSERT (context_p->next_scanner_info_p->source_p == context_p->source_p); + parser_push_block_context (context_p, true); + } + else if (context_p->stack_top_uint8 == PARSER_STATEMENT_LABEL) + { + parser_stack_iterator_t iterator; + parser_stack_iterator_init (context_p, &iterator); + parser_stack_iterator_skip (&iterator, sizeof (parser_label_statement_t) + 1); + + while (true) + { + uint8_t type = parser_stack_iterator_read_uint8 (&iterator); + + if (type == PARSER_STATEMENT_LABEL) + { + parser_stack_iterator_skip (&iterator, sizeof (parser_label_statement_t) + 1); + continue; + } + + if (parser_statement_flags[type] & PARSER_STATM_HAS_BLOCK) + { + break; + } + + parser_raise_error (context_p, PARSER_ERR_LABELLED_FUNC_NOT_IN_BLOCK); + } + } + else + { + parser_raise_error (context_p, PARSER_ERR_LEXICAL_SINGLE_STATEMENT); + } } #endif /* ENABLED (JERRY_ES2015) */ @@ -741,6 +777,12 @@ parser_parse_function_statement (parser_context_t *context_p) /**< context */ parser_flush_cbc (context_p); } + + if (JERRY_UNLIKELY (context_p->stack_top_uint8 == PARSER_STATEMENT_PRIVATE_SCOPE + || context_p->stack_top_uint8 == PARSER_STATEMENT_PRIVATE_CONTEXT)) + { + parser_pop_block_context (context_p); + } } #endif /* ENABLED (JERRY_ES2015) */ @@ -2649,6 +2691,11 @@ parser_parse_statements (parser_context_t *context_p) /**< context */ JERRY_ASSERT (context_p->stack_depth == context_p->context_stack_depth); #endif /* !JERRY_NDEBUG */ +#if ENABLED (JERRY_ES2015) + JERRY_ASSERT (context_p->stack_top_uint8 != PARSER_STATEMENT_PRIVATE_SCOPE + && context_p->stack_top_uint8 != PARSER_STATEMENT_PRIVATE_CONTEXT); +#endif /* ENABLED (JERRY_ES2015) */ + #if ENABLED (JERRY_DEBUGGER) if (JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED && context_p->token.line != context_p->last_breakpoint_line @@ -2990,6 +3037,11 @@ parser_parse_statements (parser_context_t *context_p) /**< context */ { JERRY_ASSERT (context_p->next_scanner_info_p->u8_arg & SCANNER_FUNCTION_ASYNC); + if (parser_statement_flags[context_p->stack_top_uint8] & PARSER_STATM_SINGLE_STATM) + { + parser_raise_error (context_p, PARSER_ERR_LEXICAL_SINGLE_STATEMENT); + } + lexer_next_token (context_p); JERRY_ASSERT (context_p->token.type == LEXER_KEYW_FUNCTION); continue; diff --git a/jerry-core/parser/js/js-parser-util.c b/jerry-core/parser/js/js-parser-util.c index 776eedd45..65c58c1ea 100644 --- a/jerry-core/parser/js/js-parser-util.c +++ b/jerry-core/parser/js/js-parser-util.c @@ -1127,6 +1127,10 @@ parser_error_to_string (parser_error_t error) /**< error code */ { return "Lexical declaration cannot appear in a single-statement context."; } + case PARSER_ERR_LABELLED_FUNC_NOT_IN_BLOCK: + { + return "Labelled functions are only allowed inside blocks."; + } case PARSER_ERR_LEXICAL_LET_BINDING: { return "Let binding cannot appear in let/const declarations."; diff --git a/jerry-core/parser/js/js-parser.h b/jerry-core/parser/js/js-parser.h index 8512c11d4..05a3b8fde 100644 --- a/jerry-core/parser/js/js-parser.h +++ b/jerry-core/parser/js/js-parser.h @@ -127,6 +127,7 @@ typedef enum #if ENABLED (JERRY_ES2015) PARSER_ERR_VARIABLE_REDECLARED, /**< a variable redeclared */ PARSER_ERR_LEXICAL_SINGLE_STATEMENT, /**< lexical declaration in single statement context */ + PARSER_ERR_LABELLED_FUNC_NOT_IN_BLOCK, /**< labelled functions are only allowed inside blocks */ PARSER_ERR_LEXICAL_LET_BINDING, /**< let binding cannot be declared in let/const */ PARSER_ERR_MISSING_ASSIGN_AFTER_CONST, /**< an assignment is required after a const declaration */ diff --git a/jerry-core/parser/js/js-scanner-internal.h b/jerry-core/parser/js/js-scanner-internal.h index 7c0967582..28af6d51e 100644 --- a/jerry-core/parser/js/js-scanner-internal.h +++ b/jerry-core/parser/js/js-scanner-internal.h @@ -101,7 +101,7 @@ typedef enum SCAN_STACK_COMPUTED_GENERATOR_FUNCTION, /**< computed property name */ SCAN_STACK_TEMPLATE_STRING, /**< template string */ SCAN_STACK_TAGGED_TEMPLATE_LITERAL, /**< tagged template literal */ - SCAN_STACK_FOR_BLOCK_END, /**< end of "for" statement with let/const declaration */ + SCAN_STACK_PRIVATE_BLOCK, /**< private block for single statements */ SCAN_STACK_ARROW_ARGUMENTS, /**< might be arguments of an arrow function */ SCAN_STACK_ARROW_EXPRESSION, /**< expression body of an arrow function */ SCAN_STACK_EXPLICIT_CLASS_CONSTRUCTOR, /**< explicit class constructor */ @@ -365,6 +365,7 @@ void scanner_scan_simple_arrow (parser_context_t *context_p, scanner_context_t * const uint8_t *source_p); void scanner_check_arrow_arg (parser_context_t *context_p, scanner_context_t *scanner_context_p); bool scanner_check_async_function (parser_context_t *context_p, scanner_context_t *scanner_context_p); +void scanner_check_function_after_if (parser_context_t *context_p, scanner_context_t *scanner_context_p); #endif /* ENABLED (JERRY_ES2015) */ void scanner_scan_bracket (parser_context_t *context_p, scanner_context_t *scanner_context_p); void scanner_check_directives (parser_context_t *context_p, scanner_context_t *scanner_context_p); diff --git a/jerry-core/parser/js/js-scanner-ops.c b/jerry-core/parser/js/js-scanner-ops.c index aa5d3a346..be5672016 100644 --- a/jerry-core/parser/js/js-scanner-ops.c +++ b/jerry-core/parser/js/js-scanner-ops.c @@ -280,6 +280,28 @@ scanner_check_async_function (parser_context_t *context_p, /**< context */ return false; } /* scanner_check_async_function */ +/** + * Check whether the statement of an if/else construct is a function statement. + */ +void +scanner_check_function_after_if (parser_context_t *context_p, /**< context */ + scanner_context_t *scanner_context_p) /**< scanner context */ +{ + lexer_next_token (context_p); + scanner_context_p->mode = SCAN_MODE_STATEMENT; + + if (JERRY_UNLIKELY (context_p->token.type == LEXER_KEYW_FUNCTION)) + { + scanner_literal_pool_t *literal_pool_p; + literal_pool_p = scanner_push_literal_pool (context_p, + scanner_context_p, + SCANNER_LITERAL_POOL_BLOCK); + + literal_pool_p->source_p = context_p->source_p; + parser_stack_push_uint8 (context_p, SCAN_STACK_PRIVATE_BLOCK); + } +} /* scanner_check_function_after_if */ + /** * Arrow types for scanner_scan_bracket() function. */ diff --git a/jerry-core/parser/js/js-scanner.c b/jerry-core/parser/js/js-scanner.c index 1071b131c..20eeac9ca 100644 --- a/jerry-core/parser/js/js-scanner.c +++ b/jerry-core/parser/js/js-scanner.c @@ -527,8 +527,17 @@ scanner_scan_primary_expression_end (parser_context_t *context_p, /**< context * break; } - scanner_context_p->mode = SCAN_MODE_STATEMENT; parser_stack_pop_uint8 (context_p); + +#if ENABLED (JERRY_ES2015) + if (context_p->stack_top_uint8 == SCAN_STACK_IF_STATEMENT) + { + scanner_check_function_after_if (context_p, scanner_context_p); + return SCAN_KEEP_TOKEN; + } +#endif /* ENABLED (JERRY_ES2015) */ + + scanner_context_p->mode = SCAN_MODE_STATEMENT; return SCAN_NEXT_TOKEN; } #if ENABLED (JERRY_ES2015) @@ -636,7 +645,7 @@ scanner_scan_primary_expression_end (parser_context_t *context_p, /**< context * if (stack_top == SCAN_STACK_FOR_LET_START || stack_top == SCAN_STACK_FOR_CONST_START) { - parser_stack_push_uint8 (context_p, SCAN_STACK_FOR_BLOCK_END); + parser_stack_push_uint8 (context_p, SCAN_STACK_PRIVATE_BLOCK); } #else /* !ENABLED (JERRY_ES2015) */ location_info->info.type = SCANNER_TYPE_FOR_IN; @@ -662,7 +671,7 @@ scanner_scan_primary_expression_end (parser_context_t *context_p, /**< context * #if ENABLED (JERRY_ES2015) if (stack_top == SCAN_STACK_FOR_LET_START || stack_top == SCAN_STACK_FOR_CONST_START) { - parser_stack_push_uint8 (context_p, SCAN_STACK_FOR_BLOCK_END); + parser_stack_push_uint8 (context_p, SCAN_STACK_PRIVATE_BLOCK); } #endif /* ENABLED (JERRY_ES2015) */ @@ -2027,8 +2036,13 @@ scanner_scan_statement_end (parser_context_t *context_p, /**< context */ if (type == LEXER_KEYW_ELSE && (terminator_found || (context_p->token.flags & LEXER_WAS_NEWLINE))) { +#if ENABLED (JERRY_ES2015) + scanner_check_function_after_if (context_p, scanner_context_p); + return SCAN_KEEP_TOKEN; +#else /* !ENABLED (JERRY_ES2015) */ scanner_context_p->mode = SCAN_MODE_STATEMENT; return SCAN_NEXT_TOKEN; +#endif /* ENABLED (JERRY_ES2015) */ } continue; } @@ -2075,7 +2089,7 @@ scanner_scan_statement_end (parser_context_t *context_p, /**< context */ continue; } #if ENABLED (JERRY_ES2015) - case SCAN_STACK_FOR_BLOCK_END: + case SCAN_STACK_PRIVATE_BLOCK: { parser_stack_pop_uint8 (context_p); scanner_pop_literal_pool (context_p, scanner_context_p); diff --git a/tests/jerry/es2015/function-if.js b/tests/jerry/es2015/function-if.js new file mode 100644 index 000000000..3cce20b2c --- /dev/null +++ b/tests/jerry/es2015/function-if.js @@ -0,0 +1,119 @@ +/* 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 check_syntax_error (code) +{ + try { + eval (code) + assert (false) + } catch (e) { + assert (e instanceof SyntaxError) + } +} + +check_syntax_error ("'use strict'; if (true) function f() {}") +check_syntax_error ("'use strict'; if (true) ; else function f() {}") +check_syntax_error ("'use strict'; a: function f() {}") +check_syntax_error ("if (true) async function f() {}") +check_syntax_error ("if (true) ; else async function f() {}") +check_syntax_error ("if (true) a: function f() {}") +check_syntax_error ("if (true) ; else a: function f() {}") + +var g = 1 +var h = 1 + +function f1(p) +{ + assert(g === undefined) + assert(h === undefined) + + if (p) + // Same as: { function g() { return 3 } } + function g() { return 3 } + else + // Same as: { function h() { return 4 } } + function h() { return 4 } + + if (p) { + assert(g() === 3) + assert(h === undefined) + } else { + assert(g === undefined) + assert(h() === 4) + } +} + +f1(true) +f1(false) + +function f2() +{ + assert(g() === 2) + a: b: c: d: function g() { return 2 } + + assert(h === undefined) + + { + assert(h() === 3) + a: b: c: d: function h() { return 3 } + } + + assert(h() === 3) + + try { + assert(h() === 4) + a: b: c: d: function h() { return 4 } + throw 1 + } catch (e) { + assert(h() === 5) + a: b: c: d: function h() { return 5 } + } finally { + assert(h() === 6) + a: b: c: d: function h() { return 6 } + } + + assert(h() === 6) + + switch (1) { + default: + assert(h() === 7) + a: b: c: d: function h() { return 7 } + } + + assert(h() === 7) +} +f2() + +function f3() +{ + assert(h === undefined) + + { + let a = eval("1") + assert(a === 1) + assert(h() === 1) + a: b: c: d: function h() { return 1 } + } + + assert(h() === 1) + + { + eval("a: b: c: d: function h() { return 2 }") + assert(h() === 2) + } + + assert(h() === 2) +} +f3()