From 2bdd0f50081194d646dd61c77e442d2ba4983723 Mon Sep 17 00:00:00 2001 From: Szilagyi Adam Date: Wed, 29 Apr 2020 13:04:57 +0200 Subject: [PATCH] Accessor functions do not have [[Construct]] internal method (#3698) JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi aszilagy@inf.u-szeged.hu --- .../ecma/operations/ecma-function-object.c | 9 ++++-- jerry-core/parser/js/byte-code.h | 1 + jerry-core/parser/js/js-parser.c | 5 +++ tests/jerry/es2015/function-accessor.js | 31 +++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 tests/jerry/es2015/function-accessor.js diff --git a/jerry-core/ecma/operations/ecma-function-object.c b/jerry-core/ecma/operations/ecma-function-object.c index b5a253f0e..9e490b600 100644 --- a/jerry-core/ecma/operations/ecma-function-object.c +++ b/jerry-core/ecma/operations/ecma-function-object.c @@ -1212,9 +1212,14 @@ ecma_op_function_construct (ecma_object_t *func_obj_p, /**< Function object */ ecma_extended_object_t *ext_func_obj_p = (ecma_extended_object_t *) func_obj_p; const ecma_compiled_code_t *byte_code_p = ecma_op_function_get_compiled_code (ext_func_obj_p); - if (byte_code_p->status_flags & CBC_CODE_FLAGS_ARROW_FUNCTION) + if (byte_code_p->status_flags & (CBC_CODE_FLAGS_ARROW_FUNCTION | CBC_CODE_FLAGS_ACCESSOR)) { - return ecma_raise_type_error (ECMA_ERR_MSG ("Arrow functions have no constructor.")); + if (byte_code_p->status_flags & CBC_CODE_FLAGS_ARROW_FUNCTION) + { + return ecma_raise_type_error (ECMA_ERR_MSG ("Arrow functions have no constructor.")); + } + + return ecma_raise_type_error (ECMA_ERR_MSG ("Expected a constructor.")); } #if ENABLED (JERRY_ES2015) diff --git a/jerry-core/parser/js/byte-code.h b/jerry-core/parser/js/byte-code.h index 697cb9219..4c922e188 100644 --- a/jerry-core/parser/js/byte-code.h +++ b/jerry-core/parser/js/byte-code.h @@ -797,6 +797,7 @@ typedef enum CBC_CODE_FLAGS_REST_PARAMETER = (1u << 12), /**< this function has rest parameter */ CBC_CODE_FLAG_HAS_TAGGED_LITERALS = (1u << 13), /**< this function has tagged template literal list */ CBC_CODE_FLAGS_LEXICAL_BLOCK_NEEDED = (1u << 14), /**< compiled code needs a lexical block */ + CBC_CODE_FLAGS_ACCESSOR = (1u << 15) /**< accessor propety 'get' and 'set' functions */ } cbc_code_flags; /** diff --git a/jerry-core/parser/js/js-parser.c b/jerry-core/parser/js/js-parser.c index 79c039b0f..6651b65e9 100644 --- a/jerry-core/parser/js/js-parser.c +++ b/jerry-core/parser/js/js-parser.c @@ -1305,6 +1305,11 @@ parser_post_processing (parser_context_t *context_p) /**< context */ } #if ENABLED (JERRY_ES2015) + if (context_p->status_flags & (PARSER_IS_PROPERTY_GETTER | PARSER_IS_PROPERTY_SETTER)) + { + compiled_code_p->status_flags |= CBC_CODE_FLAGS_ACCESSOR; + } + if (context_p->status_flags & PARSER_IS_ARROW_FUNCTION) { compiled_code_p->status_flags |= CBC_CODE_FLAGS_ARROW_FUNCTION; diff --git a/tests/jerry/es2015/function-accessor.js b/tests/jerry/es2015/function-accessor.js new file mode 100644 index 000000000..d8c3c7b5f --- /dev/null +++ b/tests/jerry/es2015/function-accessor.js @@ -0,0 +1,31 @@ +// 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. + +var g = (Object.getOwnPropertyDescriptor({get a(){}}, 'a')).get; + +try { + new g; + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +var s = (Object.getOwnPropertyDescriptor({set a(value){}}, 'a')).set; + +try { + new s; + assert(false); +} catch (e) { + assert(e instanceof TypeError); +}