From 1cdb3c6c56c6331d2f15975c1965afe54753211b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20B=C3=A1tyai?= Date: Tue, 30 Apr 2019 10:53:48 +0200 Subject: [PATCH] Fix identifier handling inside export statements (#2843) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #2842 Co-authored-by: Marko Fabo JerryScript-DCO-1.0-Signed-off-by: Marko Fabo mfabo@inf.u-szeged.hu JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu --- jerry-core/parser/js/js-lexer.c | 1 + jerry-core/parser/js/js-parser-expr.c | 7 ++++--- jerry-core/parser/js/js-parser-internal.h | 2 +- jerry-core/parser/js/js-parser-statm.c | 14 ++++++++++---- tests/jerry/es2015/regression-test-issue-2842.js | 16 ++++++++++++++++ tests/jerry/fail/module-031.js | 16 ++++++++++++++++ 6 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 tests/jerry/es2015/regression-test-issue-2842.js create mode 100644 tests/jerry/fail/module-031.js diff --git a/jerry-core/parser/js/js-lexer.c b/jerry-core/parser/js/js-lexer.c index 8c0fec25a..2ed3c22a2 100644 --- a/jerry-core/parser/js/js-lexer.c +++ b/jerry-core/parser/js/js-lexer.c @@ -2289,6 +2289,7 @@ lexer_expect_identifier (parser_context_t *context_p, /**< context */ lexer_construct_literal_object (context_p, (lexer_lit_location_t *) &lexer_default_literal, lexer_default_literal.type); + context_p->status_flags &= (uint32_t) ~(PARSER_MODULE_DEFAULT_CLASS_OR_FUNC); return; } #endif diff --git a/jerry-core/parser/js/js-parser-expr.c b/jerry-core/parser/js/js-parser-expr.c index eb3cf9167..3c195e706 100644 --- a/jerry-core/parser/js/js-parser-expr.c +++ b/jerry-core/parser/js/js-parser-expr.c @@ -571,9 +571,10 @@ parser_parse_class (parser_context_t *context_p, /**< context */ context_p->lit_object.literal_p->status_flags |= LEXER_FLAG_VAR; #if ENABLED (JERRY_ES2015_MODULE_SYSTEM) - if (JERRY_CONTEXT (module_top_context_p) != NULL && context_p->module_current_node_p != NULL) + if (context_p->status_flags & PARSER_MODULE_STORE_IDENT) { context_p->module_identifier_lit_p = context_p->lit_object.literal_p; + context_p->status_flags &= (uint32_t) ~(PARSER_MODULE_STORE_IDENT); } #endif /* ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ @@ -1276,12 +1277,12 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */ &context_p->token.lit_location, context_p->token.lit_location.type); #if ENABLED (JERRY_ES2015_MODULE_SYSTEM) - if (context_p->status_flags & PARSER_MODULE_DEFAULT_EXPR + if (context_p->status_flags & PARSER_MODULE_STORE_IDENT && context_p->token.lit_location.type == LEXER_IDENT_LITERAL) { context_p->lit_object.literal_p->status_flags |= LEXER_FLAG_VAR; context_p->module_identifier_lit_p = context_p->lit_object.literal_p; - context_p->status_flags &= (uint32_t) ~(PARSER_MODULE_DEFAULT_EXPR); + context_p->status_flags &= (uint32_t) ~(PARSER_MODULE_STORE_IDENT); } #endif /* ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ } diff --git a/jerry-core/parser/js/js-parser-internal.h b/jerry-core/parser/js/js-parser-internal.h index a0fcfd777..e65d1b9aa 100644 --- a/jerry-core/parser/js/js-parser-internal.h +++ b/jerry-core/parser/js/js-parser-internal.h @@ -82,7 +82,7 @@ typedef enum #endif /* ENABLED (JERRY_ES2015_CLASS) */ #if ENABLED (JERRY_ES2015_MODULE_SYSTEM) PARSER_MODULE_DEFAULT_CLASS_OR_FUNC = (1u << 25), /**< parsing a function or class default export */ - PARSER_MODULE_DEFAULT_EXPR = (1u << 26), /**< parsing a default export expression */ + PARSER_MODULE_STORE_IDENT = (1u << 26), /**< store identifier of the current export statement */ #endif /* ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ } parser_general_flags_t; diff --git a/jerry-core/parser/js/js-parser-statm.c b/jerry-core/parser/js/js-parser-statm.c index bfc3d4fee..99d1576e4 100644 --- a/jerry-core/parser/js/js-parser-statm.c +++ b/jerry-core/parser/js/js-parser-statm.c @@ -327,9 +327,10 @@ parser_parse_var_statement (parser_context_t *context_p) /**< context */ context_p->lit_object.literal_p->status_flags |= LEXER_FLAG_VAR; #if ENABLED (JERRY_ES2015_MODULE_SYSTEM) - if (JERRY_CONTEXT (module_top_context_p) != NULL && context_p->module_current_node_p != NULL) + if (context_p->status_flags & PARSER_MODULE_STORE_IDENT) { context_p->module_identifier_lit_p = context_p->lit_object.literal_p; + context_p->status_flags &= (uint32_t) ~(PARSER_MODULE_STORE_IDENT); } #endif /* ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ @@ -399,9 +400,10 @@ parser_parse_function_statement (parser_context_t *context_p) /**< context */ name_p = context_p->lit_object.literal_p; #if ENABLED (JERRY_ES2015_MODULE_SYSTEM) - if (JERRY_CONTEXT (module_top_context_p) != NULL && context_p->module_current_node_p != NULL) + if (context_p->status_flags & PARSER_MODULE_STORE_IDENT) { context_p->module_identifier_lit_p = context_p->lit_object.literal_p; + context_p->status_flags &= (uint32_t) ~(PARSER_MODULE_STORE_IDENT); } #endif /* ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ @@ -1815,6 +1817,8 @@ parser_parse_export_statement (parser_context_t *context_p) /**< context */ lexer_range_t range; parser_save_range (context_p, &range, context_p->source_end_p); + context_p->status_flags |= PARSER_MODULE_STORE_IDENT; + lexer_next_token (context_p); if (context_p->token.type == LEXER_KEYW_CLASS) { @@ -1829,7 +1833,6 @@ parser_parse_export_statement (parser_context_t *context_p) /**< context */ else { /* Assignment expression */ - context_p->status_flags |= PARSER_MODULE_DEFAULT_EXPR; parser_set_range (context_p, &range); /* 15.2.3.5 Use the synthetic name '*default*' as the identifier. */ @@ -1866,7 +1869,6 @@ parser_parse_export_statement (parser_context_t *context_p) /**< context */ name_p); ecma_deref_ecma_string (name_p); ecma_deref_ecma_string (export_name_p); - context_p->status_flags &= (uint32_t) ~(PARSER_MODULE_DEFAULT_CLASS_OR_FUNC | PARSER_MODULE_DEFAULT_EXPR); break; } case LEXER_MULTIPLY: @@ -1884,6 +1886,7 @@ parser_parse_export_statement (parser_context_t *context_p) /**< context */ } case LEXER_KEYW_VAR: { + context_p->status_flags |= PARSER_MODULE_STORE_IDENT; parser_parse_var_statement (context_p); ecma_string_t *name_p = ecma_new_ecma_string_from_utf8 (context_p->module_identifier_lit_p->u.char_p, context_p->module_identifier_lit_p->prop.length); @@ -1902,6 +1905,7 @@ parser_parse_export_statement (parser_context_t *context_p) /**< context */ } case LEXER_KEYW_CLASS: { + context_p->status_flags |= PARSER_MODULE_STORE_IDENT; parser_parse_class (context_p, true); ecma_string_t *name_p = ecma_new_ecma_string_from_utf8 (context_p->module_identifier_lit_p->u.char_p, context_p->module_identifier_lit_p->prop.length); @@ -1920,6 +1924,7 @@ parser_parse_export_statement (parser_context_t *context_p) /**< context */ } case LEXER_KEYW_FUNCTION: { + context_p->status_flags |= PARSER_MODULE_STORE_IDENT; parser_parse_function_statement (context_p); ecma_string_t *name_p = ecma_new_ecma_string_from_utf8 (context_p->module_identifier_lit_p->u.char_p, context_p->module_identifier_lit_p->prop.length); @@ -1955,6 +1960,7 @@ parser_parse_export_statement (parser_context_t *context_p) /**< context */ } } + context_p->status_flags &= (uint32_t) ~(PARSER_MODULE_DEFAULT_CLASS_OR_FUNC | PARSER_MODULE_STORE_IDENT); parser_module_add_export_node_to_context (context_p); context_p->module_current_node_p = NULL; } /* parser_parse_export_statement */ diff --git a/tests/jerry/es2015/regression-test-issue-2842.js b/tests/jerry/es2015/regression-test-issue-2842.js new file mode 100644 index 000000000..dd858b06f --- /dev/null +++ b/tests/jerry/es2015/regression-test-issue-2842.js @@ -0,0 +1,16 @@ +/* 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. + */ + +export function $ () { var $, b } diff --git a/tests/jerry/fail/module-031.js b/tests/jerry/fail/module-031.js new file mode 100644 index 000000000..d810d4140 --- /dev/null +++ b/tests/jerry/fail/module-031.js @@ -0,0 +1,16 @@ +/* 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. + */ + +export default function () { function () {} }