From 8ccb95c31cb90b4bba013e909c8cc86f4fc77327 Mon Sep 17 00:00:00 2001 From: kisbg Date: Mon, 4 May 2020 15:03:12 +0200 Subject: [PATCH] Added new target support to Promise (#3707) JerryScript-DCO-1.0-Signed-off-by: bence gabor kis kisbg@inf.u-szeged.hu --- jerry-core/api/jerry.c | 12 ++++++++- .../ecma/operations/ecma-promise-object.c | 13 ++++++++-- tests/jerry/es2015/promise-new-target.js | 26 +++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 tests/jerry/es2015/promise-new-target.js diff --git a/jerry-core/api/jerry.c b/jerry-core/api/jerry.c index 1d36eff68..786b2a09d 100644 --- a/jerry-core/api/jerry.c +++ b/jerry-core/api/jerry.c @@ -1616,7 +1616,17 @@ jerry_create_promise (void) jerry_assert_api_available (); #if ENABLED (JERRY_ES2015_BUILTIN_PROMISE) - return ecma_op_create_promise_object (ECMA_VALUE_EMPTY, ECMA_PROMISE_EXECUTOR_EMPTY); + ecma_object_t * old_new_target_p = JERRY_CONTEXT (current_new_target); + + if (old_new_target_p == NULL) + { + JERRY_CONTEXT (current_new_target) = ecma_builtin_get (ECMA_BUILTIN_ID_PROMISE); + } + + ecma_value_t promise_value = ecma_op_create_promise_object (ECMA_VALUE_EMPTY, ECMA_PROMISE_EXECUTOR_EMPTY); + + JERRY_CONTEXT (current_new_target) = old_new_target_p; + return promise_value; #else /* !ENABLED (JERRY_ES2015_BUILTIN_PROMISE) */ return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Promise not supported."))); #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROMISE) */ diff --git a/jerry-core/ecma/operations/ecma-promise-object.c b/jerry-core/ecma/operations/ecma-promise-object.c index 705e3fb7b..182f0c20a 100644 --- a/jerry-core/ecma/operations/ecma-promise-object.c +++ b/jerry-core/ecma/operations/ecma-promise-object.c @@ -493,11 +493,20 @@ ecma_value_t ecma_op_create_promise_object (ecma_value_t executor, /**< the executor function or object */ ecma_promise_executor_type_t type) /**< indicates the type of executor */ { + JERRY_ASSERT (JERRY_CONTEXT (current_new_target) != NULL); /* 3. */ - ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_PROMISE_PROTOTYPE); - ecma_object_t *object_p = ecma_create_object (prototype_obj_p, + ecma_object_t *proto_p = ecma_op_get_prototype_from_constructor (JERRY_CONTEXT (current_new_target), + ECMA_BUILTIN_ID_PROMISE_PROTOTYPE); + + if (JERRY_UNLIKELY (proto_p == NULL)) + { + return ECMA_VALUE_ERROR; + } + + ecma_object_t *object_p = ecma_create_object (proto_p, sizeof (ecma_promise_object_t), ECMA_OBJECT_TYPE_CLASS); + ecma_deref_object (proto_p); ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p; ext_object_p->u.class_prop.class_id = LIT_MAGIC_STRING_PROMISE_UL; ext_object_p->u.class_prop.u.value = ECMA_VALUE_UNDEFINED; diff --git a/tests/jerry/es2015/promise-new-target.js b/tests/jerry/es2015/promise-new-target.js new file mode 100644 index 000000000..18d730ea0 --- /dev/null +++ b/tests/jerry/es2015/promise-new-target.js @@ -0,0 +1,26 @@ +/* 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 (Promise); + assert (false); +} catch (e) { + assert (e instanceof TypeError); +} + +class MyPromise extends Promise {}; + +var p1= new MyPromise(function(){}); +assert(Object.getPrototypeOf(p1) == MyPromise.prototype)