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
This commit is contained in:
kisbg 2020-05-04 14:24:39 +02:00 committed by GitHub
parent b5d9699788
commit 14cea9c1ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 133 additions and 22 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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)