From a44d5848422e719a811bdadd62a917e4e113ac63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20B=C3=A1tyai?= Date: Wed, 17 Jul 2019 14:11:13 +0200 Subject: [PATCH] Fix 'eval' checking in import/export statements. (#2978) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #2975 JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu --- jerry-core/ecma/base/ecma-globals.h | 1 + jerry-core/ecma/operations/ecma-eval.c | 2 ++ jerry-core/parser/js/js-parser-internal.h | 3 ++- jerry-core/parser/js/js-parser-module.c | 3 +-- jerry-core/parser/js/js-parser.c | 5 ++++ .../es2015/regression-test-issue-2975.js | 23 +++++++++++++++++++ 6 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 tests/jerry/es2015/regression-test-issue-2975.js diff --git a/jerry-core/ecma/base/ecma-globals.h b/jerry-core/ecma/base/ecma-globals.h index a2c626fce..5f4f78155 100644 --- a/jerry-core/ecma/base/ecma-globals.h +++ b/jerry-core/ecma/base/ecma-globals.h @@ -105,6 +105,7 @@ typedef enum ECMA_PARSE_HAS_SUPER = (1u << 3), /**< the current context has super reference */ ECMA_PARSE_HAS_IMPL_SUPER = (1u << 4), /**< the current context has implicit parent class */ ECMA_PARSE_HAS_STATIC_SUPER = (1u << 5), /**< the current context is a static class method */ + ECMA_PARSE_EVAL = (1u << 6), /**< eval is called */ } ecma_parse_opts_t; /** diff --git a/jerry-core/ecma/operations/ecma-eval.c b/jerry-core/ecma/operations/ecma-eval.c index 90da28901..fba05dbb5 100644 --- a/jerry-core/ecma/operations/ecma-eval.c +++ b/jerry-core/ecma/operations/ecma-eval.c @@ -91,6 +91,8 @@ ecma_op_eval_chars_buffer (const lit_utf8_byte_t *code_p, /**< code characters b parse_opts &= (uint32_t) ~ECMA_PARSE_STRICT_MODE; } + parse_opts |= ECMA_PARSE_EVAL; + #if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_EVAL); #endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) */ diff --git a/jerry-core/parser/js/js-parser-internal.h b/jerry-core/parser/js/js-parser-internal.h index 130832e43..16a5c6da0 100644 --- a/jerry-core/parser/js/js-parser-internal.h +++ b/jerry-core/parser/js/js-parser-internal.h @@ -82,7 +82,8 @@ 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_STORE_IDENT = (1u << 26), /**< store identifier of the current export statement */ + PARSER_MODULE_STORE_IDENT = (1u << 26), /**< store identifier of the current export statement */ + PARSER_IS_EVAL = (1u << 27), /**< eval code */ #endif /* ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ } parser_general_flags_t; diff --git a/jerry-core/parser/js/js-parser-module.c b/jerry-core/parser/js/js-parser-module.c index bfc053e7f..bb21375cf 100644 --- a/jerry-core/parser/js/js-parser-module.c +++ b/jerry-core/parser/js/js-parser-module.c @@ -493,8 +493,7 @@ parser_module_check_request_place (parser_context_t *context_p) /**< parser cont { if (context_p->last_context_p != NULL || context_p->stack_top_uint8 != 0 - || (JERRY_CONTEXT (status_flags) & ECMA_STATUS_DIRECT_EVAL) != 0 - || (context_p->status_flags & PARSER_IS_FUNCTION) != 0) + || (context_p->status_flags & (PARSER_IS_EVAL | PARSER_IS_FUNCTION)) != 0) { parser_raise_error (context_p, PARSER_ERR_MODULE_UNEXPECTED); } diff --git a/jerry-core/parser/js/js-parser.c b/jerry-core/parser/js/js-parser.c index 1a83e6fb9..bfa088050 100644 --- a/jerry-core/parser/js/js-parser.c +++ b/jerry-core/parser/js/js-parser.c @@ -2397,6 +2397,11 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */ context.status_flags |= parse_opts & PARSER_STRICT_MODE_MASK; #if ENABLED (JERRY_ES2015_MODULE_SYSTEM) + if (parse_opts & ECMA_PARSE_EVAL) + { + context.status_flags |= PARSER_IS_EVAL; + } + context.module_current_node_p = NULL; #endif /* ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ diff --git a/tests/jerry/es2015/regression-test-issue-2975.js b/tests/jerry/es2015/regression-test-issue-2975.js new file mode 100644 index 000000000..d3d53607f --- /dev/null +++ b/tests/jerry/es2015/regression-test-issue-2975.js @@ -0,0 +1,23 @@ +/* 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. + */ + +/* Import/export statements must be in the global scope. */ +var eval = eval.bind(); +try { + eval('import { c } from "tests/jerry/es2015/module-export-01.js";'); + assert (false); +} catch (e) { + assert (e instanceof SyntaxError); +}