From ab2e8217778b95977821ccbcd9a11346f5749fc2 Mon Sep 17 00:00:00 2001 From: Zoltan Herczeg Date: Mon, 8 Jun 2020 10:25:34 +0200 Subject: [PATCH] Fix three async function issues. (#3863) - Invalid assert - Add missing async prefix check when an identifier is enclosed in brackets - Adding a new byte-code Fixes #3855 Fixes #3856 Fixes #3857 JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com --- jerry-core/include/jerryscript-snapshot.h | 2 +- jerry-core/parser/js/byte-code.c | 2 +- jerry-core/parser/js/byte-code.h | 2 ++ jerry-core/parser/js/js-parser-statm.c | 3 +-- jerry-core/parser/js/js-parser.c | 12 ++++++------ jerry-core/parser/js/js-scanner-ops.c | 17 +++++++++++++---- .../es2015/regresssion-test-issue-3856.js | 18 ++++++++++++++++++ .../es2015/regresssion-test-issue-3857.js | 17 +++++++++++++++++ 8 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 tests/jerry/es2015/regresssion-test-issue-3856.js create mode 100644 tests/jerry/es2015/regresssion-test-issue-3857.js diff --git a/jerry-core/include/jerryscript-snapshot.h b/jerry-core/include/jerryscript-snapshot.h index 071b0033a..e43f6df54 100644 --- a/jerry-core/include/jerryscript-snapshot.h +++ b/jerry-core/include/jerryscript-snapshot.h @@ -30,7 +30,7 @@ extern "C" /** * Jerry snapshot format version. */ -#define JERRY_SNAPSHOT_VERSION (45u) +#define JERRY_SNAPSHOT_VERSION (46u) /** * Flags for jerry_generate_snapshot and jerry_generate_function_snapshot. diff --git a/jerry-core/parser/js/byte-code.c b/jerry-core/parser/js/byte-code.c index b275a2509..edf9e5521 100644 --- a/jerry-core/parser/js/byte-code.c +++ b/jerry-core/parser/js/byte-code.c @@ -27,7 +27,7 @@ JERRY_STATIC_ASSERT ((sizeof (cbc_uint16_arguments_t) % sizeof (jmem_cpointer_t) */ JERRY_STATIC_ASSERT (CBC_END == 238, number_of_cbc_opcodes_changed); -JERRY_STATIC_ASSERT (CBC_EXT_END == 115, +JERRY_STATIC_ASSERT (CBC_EXT_END == 116, number_of_cbc_ext_opcodes_changed); #if ENABLED (JERRY_PARSER) diff --git a/jerry-core/parser/js/byte-code.h b/jerry-core/parser/js/byte-code.h index 0bb5d3838..d58c53ea3 100644 --- a/jerry-core/parser/js/byte-code.h +++ b/jerry-core/parser/js/byte-code.h @@ -726,6 +726,8 @@ VM_OC_EXT_RETURN | VM_OC_GET_STACK) \ CBC_OPCODE (CBC_EXT_RETURN_PROMISE, CBC_NO_FLAG, -1, \ VM_OC_RETURN_PROMISE | VM_OC_GET_STACK) \ + CBC_OPCODE (CBC_EXT_RETURN_PROMISE_UNDEFINED, CBC_NO_FLAG, 0, \ + VM_OC_RETURN_PROMISE) \ CBC_OPCODE (CBC_EXT_PUSH_NEW_TARGET, CBC_NO_FLAG, 1, \ VM_OC_PUSH_NEW_TARGET | VM_OC_PUT_STACK) \ \ diff --git a/jerry-core/parser/js/js-parser-statm.c b/jerry-core/parser/js/js-parser-statm.c index 109f0a295..50dc8f2c5 100644 --- a/jerry-core/parser/js/js-parser-statm.c +++ b/jerry-core/parser/js/js-parser-statm.c @@ -3017,8 +3017,7 @@ parser_parse_statements (parser_context_t *context_p) /**< context */ #if ENABLED (JERRY_ES2015) if (context_p->status_flags & PARSER_IS_ASYNC_FUNCTION) { - parser_emit_cbc (context_p, CBC_PUSH_UNDEFINED); - parser_emit_cbc_ext (context_p, CBC_EXT_RETURN_PROMISE); + parser_emit_cbc_ext (context_p, CBC_EXT_RETURN_PROMISE_UNDEFINED); break; } #endif /* ENABLED (JERRY_ES2015) */ diff --git a/jerry-core/parser/js/js-parser.c b/jerry-core/parser/js/js-parser.c index 0f75ce86b..5e6dfabb6 100644 --- a/jerry-core/parser/js/js-parser.c +++ b/jerry-core/parser/js/js-parser.c @@ -1018,7 +1018,8 @@ parser_post_processing (parser_context_t *context_p) /**< context */ length++; #if ENABLED (JERRY_ES2015) - if (ext_opcode == CBC_EXT_RETURN_PROMISE) + if (ext_opcode == CBC_EXT_RETURN_PROMISE + || ext_opcode == CBC_EXT_RETURN_PROMISE_UNDEFINED) { last_opcode = CBC_RETURN; } @@ -1163,7 +1164,7 @@ parser_post_processing (parser_context_t *context_p) /**< context */ #if ENABLED (JERRY_ES2015) if (context_p->status_flags & PARSER_IS_ASYNC_FUNCTION) { - length += 2; + length++; } #endif /* ENABLED (JERRY_ES2015) */ @@ -1540,10 +1541,9 @@ parser_post_processing (parser_context_t *context_p) /**< context */ #if ENABLED (JERRY_ES2015) if (context_p->status_flags & PARSER_IS_ASYNC_FUNCTION) { - dst_p[-1] = CBC_PUSH_UNDEFINED; - dst_p[0] = CBC_EXT_OPCODE; - dst_p[1] = CBC_EXT_RETURN_PROMISE; - dst_p += 2; + dst_p[-1] = CBC_EXT_OPCODE; + dst_p[0] = CBC_EXT_RETURN_PROMISE_UNDEFINED; + dst_p++; } #endif /* ENABLED (JERRY_ES2015) */ } diff --git a/jerry-core/parser/js/js-scanner-ops.c b/jerry-core/parser/js/js-scanner-ops.c index 569be23f7..325b3786d 100644 --- a/jerry-core/parser/js/js-scanner-ops.c +++ b/jerry-core/parser/js/js-scanner-ops.c @@ -260,7 +260,8 @@ scanner_check_async_function (parser_context_t *context_p, /**< context */ scanner_context_t *scanner_context_p) /**< scanner context */ { JERRY_ASSERT (lexer_token_is_async (context_p)); - JERRY_ASSERT (scanner_context_p->mode == SCAN_MODE_PRIMARY_EXPRESSION); + JERRY_ASSERT (scanner_context_p->mode == SCAN_MODE_PRIMARY_EXPRESSION + || scanner_context_p->mode == SCAN_MODE_PRIMARY_EXPRESSION_AFTER_NEW); JERRY_ASSERT (scanner_context_p->async_source_p != NULL); lexer_lit_location_t async_literal = context_p->token.lit_location; @@ -430,10 +431,18 @@ scanner_scan_bracket (parser_context_t *context_p, /**< context */ async_source_p = source_p; } } - else if (depth == total_depth - 1 && lexer_check_arrow (context_p)) + else if (depth == total_depth - 1) { - arrow_type = SCANNER_SCAN_BRACKET_ARROW_WITH_ONE_ARG; - break; + if (lexer_check_arrow (context_p)) + { + arrow_type = SCANNER_SCAN_BRACKET_ARROW_WITH_ONE_ARG; + break; + } + + if (context_p->stack_top_uint8 == SCAN_STACK_USE_ASYNC) + { + scanner_add_async_literal (context_p, scanner_context_p); + } } arrow_source_p = NULL; diff --git a/tests/jerry/es2015/regresssion-test-issue-3856.js b/tests/jerry/es2015/regresssion-test-issue-3856.js new file mode 100644 index 000000000..7040a5347 --- /dev/null +++ b/tests/jerry/es2015/regresssion-test-issue-3856.js @@ -0,0 +1,18 @@ +// 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. + +let str = ''; +function async() {} + +async(str) diff --git a/tests/jerry/es2015/regresssion-test-issue-3857.js b/tests/jerry/es2015/regresssion-test-issue-3857.js new file mode 100644 index 000000000..7cd6c40b8 --- /dev/null +++ b/tests/jerry/es2015/regresssion-test-issue-3857.js @@ -0,0 +1,17 @@ +// 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 async() {} + +new async