From 14cea9c1ec67ba37ab285a27ff0aec4ab55081da Mon Sep 17 00:00:00 2001 From: kisbg Date: Mon, 4 May 2020 14:24:39 +0200 Subject: [PATCH] Added new target support in Map, WeakMap, Set and WeakSet (#3701) JerryScript-DCO-1.0-Signed-off-by: bence gabor kis kisbg@inf.u-szeged.hu --- jerry-core/api/jerry.c | 50 +++++++---- .../ecma/operations/ecma-container-object.c | 19 ++-- .../jerry/es2015/new-target-for-containers.js | 86 +++++++++++++++++++ 3 files changed, 133 insertions(+), 22 deletions(-) create mode 100644 tests/jerry/es2015/new-target-for-containers.js diff --git a/jerry-core/api/jerry.c b/jerry-core/api/jerry.c index 362a13435..1d36eff68 100644 --- a/jerry-core/api/jerry.c +++ b/jerry-core/api/jerry.c @@ -4422,42 +4422,46 @@ jerry_create_container (jerry_container_type_t container_type, /**< Type of the } } + lit_magic_string_id_t lit_id; + ecma_builtin_id_t proto_id; + ecma_builtin_id_t ctor_id; + switch (container_type) { #if ENABLED (JERRY_ES2015_BUILTIN_MAP) case JERRY_CONTAINER_TYPE_MAP: { - return ecma_op_container_create (arguments_list_p, - arguments_list_len, - LIT_MAGIC_STRING_MAP_UL, - ECMA_BUILTIN_ID_MAP_PROTOTYPE); + lit_id = LIT_MAGIC_STRING_MAP_UL; + proto_id = ECMA_BUILTIN_ID_MAP_PROTOTYPE; + ctor_id = ECMA_BUILTIN_ID_MAP; + break; } #endif /* ENABLED (JERRY_ES2015_BUILTIN_MAP) */ #if ENABLED (JERRY_ES2015_BUILTIN_SET) case JERRY_CONTAINER_TYPE_SET: { - return ecma_op_container_create (arguments_list_p, - arguments_list_len, - LIT_MAGIC_STRING_SET_UL, - ECMA_BUILTIN_ID_SET_PROTOTYPE); + lit_id = LIT_MAGIC_STRING_SET_UL; + proto_id = ECMA_BUILTIN_ID_SET_PROTOTYPE; + ctor_id = ECMA_BUILTIN_ID_SET; + break; } #endif /* ENABLED (JERRY_ES2015_BUILTIN_SET) */ #if ENABLED (JERRY_ES2015_BUILTIN_WEAKMAP) case JERRY_CONTAINER_TYPE_WEAKMAP: { - return ecma_op_container_create (arguments_list_p, - arguments_list_len, - LIT_MAGIC_STRING_WEAKMAP_UL, - ECMA_BUILTIN_ID_WEAKMAP_PROTOTYPE); + lit_id = LIT_MAGIC_STRING_WEAKMAP_UL; + proto_id = ECMA_BUILTIN_ID_WEAKMAP_PROTOTYPE; + ctor_id = ECMA_BUILTIN_ID_WEAKMAP; + break; } #endif /* ENABLED (JERRY_ES2015_BUILTIN_WEAKMAP) */ #if ENABLED (JERRY_ES2015_BUILTIN_WEAKSET) case JERRY_CONTAINER_TYPE_WEAKSET: { - return ecma_op_container_create (arguments_list_p, - arguments_list_len, - LIT_MAGIC_STRING_WEAKSET_UL, - ECMA_BUILTIN_ID_WEAKSET_PROTOTYPE); + lit_id = LIT_MAGIC_STRING_WEAKSET_UL; + proto_id = ECMA_BUILTIN_ID_WEAKSET_PROTOTYPE; + ctor_id = ECMA_BUILTIN_ID_WEAKSET; + break; } #endif /* ENABLED (JERRY_ES2015_BUILTIN_WEAKSET) */ default: @@ -4465,6 +4469,20 @@ jerry_create_container (jerry_container_type_t container_type, /**< Type of the return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Invalid container type."))); } } + 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 (ctor_id); + } + + ecma_value_t container_value = ecma_op_container_create (arguments_list_p, + arguments_list_len, + lit_id, + proto_id); + + JERRY_CONTEXT (current_new_target) = old_new_target_p; + return container_value; #else /* !ENABLED (JERRY_ES2015_BUILTIN_CONTAINER) */ JERRY_UNUSED (arguments_list_p); JERRY_UNUSED (arguments_list_len); diff --git a/jerry-core/ecma/operations/ecma-container-object.c b/jerry-core/ecma/operations/ecma-container-object.c index c246cb637..cc3639ebf 100644 --- a/jerry-core/ecma/operations/ecma-container-object.c +++ b/jerry-core/ecma/operations/ecma-container-object.c @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#include "jcontext.h" #include "ecma-alloc.h" #include "ecma-array-object.h" #include "ecma-builtins.h" @@ -370,13 +370,20 @@ ecma_op_container_create (const ecma_value_t *arguments_list_p, /**< arguments l || lit_id == LIT_MAGIC_STRING_SET_UL || lit_id == LIT_MAGIC_STRING_WEAKMAP_UL || lit_id == LIT_MAGIC_STRING_WEAKSET_UL); + JERRY_ASSERT (JERRY_CONTEXT (current_new_target) != NULL); + + ecma_object_t *proto_p = ecma_op_get_prototype_from_constructor (JERRY_CONTEXT (current_new_target), proto_id); + + if (JERRY_UNLIKELY (proto_p == NULL)) + { + return ECMA_VALUE_ERROR; + } ecma_collection_t *container_p = ecma_op_create_internal_buffer (); - - ecma_object_t *object_p = ecma_create_object (ecma_builtin_get (proto_id), - sizeof (ecma_extended_object_t), - ECMA_OBJECT_TYPE_CLASS); - + ecma_object_t *object_p = ecma_create_object (proto_p, + sizeof (ecma_extended_object_t), + ECMA_OBJECT_TYPE_CLASS); + ecma_deref_object (proto_p); ecma_extended_object_t *map_obj_p = (ecma_extended_object_t *) object_p; map_obj_p->u.class_prop.extra_info = ECMA_CONTAINER_FLAGS_EMPTY; map_obj_p->u.class_prop.class_id = (uint16_t) lit_id; diff --git a/tests/jerry/es2015/new-target-for-containers.js b/tests/jerry/es2015/new-target-for-containers.js new file mode 100644 index 000000000..e78af2191 --- /dev/null +++ b/tests/jerry/es2015/new-target-for-containers.js @@ -0,0 +1,86 @@ +/* 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 o = new Proxy (function f () {}, { get(t,p,r) { if (p == "prototype") { throw 42.1 } Reflect.get(...arguments) }}) + +try { + Reflect.construct (Map, [], o); + assert (false); +} catch (e) { + assert(e == 42.1) +} + +try { + Reflect.construct (Set, [], o); + assert (false); +} catch (e) { + assert(e == 42.1) +} + +try { + Reflect.construct (WeakMap, [], o); + assert (false); +} catch (e) { + assert(e == 42.1) +} + +try { + Reflect.construct (WeakSet, [], o); + assert (false); +} catch (e) { + assert(e == 42.1) +} + +try { + Reflect.construct (Map); + assert (false); +} catch (e) { + assert (e instanceof TypeError); +} + +try { + Reflect.construct (Set); + assert (false); +} catch (e) { + assert (e instanceof TypeError); +} + +try { + Reflect.construct (WeakMap); + assert (false); +} catch (e) { + assert (e instanceof TypeError); +} + +try { + Reflect.construct (WeakSet); + assert (false); +} catch (e) { + assert (e instanceof TypeError); +} + +class MyMap extends Map {}; +class MySet extends Set {}; +class MyWeakMap extends WeakMap {}; +class MyWeakSet extends WeakSet {}; +var m1= new MyMap(); +var s1= new MySet(); +var wm1= new MyWeakMap(); +var ws1= new MyWeakSet(); + +assert(Object.getPrototypeOf(m1) == MyMap.prototype) +assert(Object.getPrototypeOf(s1) == MySet.prototype) +assert(Object.getPrototypeOf(wm1) == MyWeakMap.prototype) +assert(Object.getPrototypeOf(ws1) == MyWeakSet.prototype)