From 2c78ee7f54f73d8f76fdc17a249c372d885c2345 Mon Sep 17 00:00:00 2001 From: Youngil Choi Date: Mon, 29 Aug 2016 18:02:34 +0900 Subject: [PATCH] FunctionExpression name binding should be immutable JerryScript-DCO-1.0-Signed-off-by: Youngil Choi duddlf.choi@samsung.com --- jerry-core/vm/vm.c | 30 ++++++++++++++------- tests/jerry/regression-test-issue-1292.js | 32 +++++++++++++++++++++++ 2 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 tests/jerry/regression-test-issue-1292.js diff --git a/jerry-core/vm/vm.c b/jerry-core/vm/vm.c index b63690937..a3b30530c 100644 --- a/jerry-core/vm/vm.c +++ b/jerry-core/vm/vm.c @@ -530,6 +530,8 @@ vm_init_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ uint16_t register_end; jmem_cpointer_t *literal_start_p = frame_ctx_p->literal_start_p; bool is_strict = ((frame_ctx_p->bytecode_header_p->status_flags & CBC_CODE_FLAGS_STRICT_MODE) != 0); + jmem_cpointer_t self_reference; + ECMA_SET_NON_NULL_POINTER (self_reference, bytecode_header_p); /* Prepare. */ if (!(bytecode_header_p->status_flags & CBC_CODE_FLAGS_FULL_LITERAL_ENCODING)) @@ -602,12 +604,8 @@ vm_init_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ ecma_string_t *name_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_string_t, literal_start_p[literal_index]); - vm_var_decl (frame_ctx_p, name_p); - READ_LITERAL_INDEX (value_index); - ecma_object_t *ref_base_lex_env_p = ecma_op_resolve_reference_base (frame_ctx_p->lex_env_p, name_p); - if (value_index < register_end) { lit_value = frame_ctx_p->registers_p[value_index]; @@ -618,11 +616,25 @@ vm_init_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ literal_start_p[value_index]); } - /* TODO: check the return value */ - ecma_op_put_value_lex_env_base (ref_base_lex_env_p, - name_p, - is_strict, - lit_value); + if (self_reference == literal_start_p[value_index]) + { + ecma_op_create_immutable_binding (frame_ctx_p->lex_env_p, name_p); + ecma_op_initialize_immutable_binding (frame_ctx_p->lex_env_p, + name_p, + lit_value); + } + else + { + vm_var_decl (frame_ctx_p, name_p); + + ecma_object_t *ref_base_lex_env_p = ecma_op_resolve_reference_base (frame_ctx_p->lex_env_p, name_p); + + /* TODO: check the return value */ + ecma_op_put_value_lex_env_base (ref_base_lex_env_p, + name_p, + is_strict, + lit_value); + } if (value_index >= register_end) { diff --git a/tests/jerry/regression-test-issue-1292.js b/tests/jerry/regression-test-issue-1292.js new file mode 100644 index 000000000..638c0d6a0 --- /dev/null +++ b/tests/jerry/regression-test-issue-1292.js @@ -0,0 +1,32 @@ +// Copyright 2016 Samsung Electronics Co., Ltd. +// Copyright 2016 University of Szeged. +// +// 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 nonStrictFuncExpr() { + assert(typeof nonStrictFuncExpr == "function"); + nonStrictFuncExpr = 123; + assert(typeof nonStrictFuncExpr == "function"); +})(); + +(function strictFuncExpr() { + "use strict"; + assert(typeof strictFuncExpr == "function"); + try { + strictFuncExpr = 123; + assert(false); + } catch (e) { + assert(e instanceof TypeError); + } + assert(typeof strictFuncExpr == "function"); +})();