diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-date.c b/jerry-core/ecma/builtin-objects/ecma-builtin-date.c index c0f78603d..e04eea781 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-date.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-date.c @@ -15,6 +15,8 @@ #include +#include "jcontext.h" +#include "ecma-function-object.h" #include "ecma-alloc.h" #include "ecma-builtin-helpers.h" #include "ecma-conversion.h" @@ -734,6 +736,17 @@ ecma_builtin_date_dispatch_construct (const ecma_value_t *arguments_list_p, /**< ecma_number_t prim_value_num = ECMA_NUMBER_ZERO; ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_DATE_PROTOTYPE); +#if ENABLED (JERRY_ES2015) + if (JERRY_CONTEXT (current_new_target)) + { + prototype_obj_p = ecma_op_get_prototype_from_constructor (JERRY_CONTEXT (current_new_target), + ECMA_BUILTIN_ID_DATE_PROTOTYPE); + if (JERRY_UNLIKELY (prototype_obj_p == NULL)) + { + return ECMA_VALUE_ERROR; + } + } +#endif /* !(ENABLED (JERRY_ES2015) */ ecma_object_t *obj_p = ecma_create_object (prototype_obj_p, sizeof (ecma_extended_object_t), ECMA_OBJECT_TYPE_CLASS); @@ -810,7 +823,12 @@ ecma_builtin_date_dispatch_construct (const ecma_value_t *arguments_list_p, /**< JERRY_ASSERT (ECMA_IS_VALUE_ERROR (ret_value)); ecma_deref_object (obj_p); } - +#if ENABLED (JERRY_ES2015) + if (JERRY_CONTEXT (current_new_target)) + { + ecma_deref_object (prototype_obj_p); + } +#endif /* !(ENABLED (JERRY_ES2015) */ return ret_value; } /* ecma_builtin_date_dispatch_construct */ diff --git a/tests/jerry/es2015/new-target-for-date-object.js b/tests/jerry/es2015/new-target-for-date-object.js new file mode 100644 index 000000000..07f64056c --- /dev/null +++ b/tests/jerry/es2015/new-target-for-date-object.js @@ -0,0 +1,40 @@ +/* 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. +*/ + +try { + Reflect.construct (Date,); + assert (false); +} catch (e) { + assert (e instanceof TypeError); +} + +try { + Reflect.construct (Date, "2015-01-01"); + assert (false); +} catch (e) { + assert (e instanceof TypeError); +} + +try { + Reflect.construct (Date, 1420070400000); + assert (false); +} catch (e) { + assert (e instanceof TypeError); +} + +class MyDate extends Date {}; +var d1= new MyDate(); + +assert(Object.getPrototypeOf(d1) == MyDate.prototype)