Skip to content

Commit 1937f82

Browse files
author
kisbg
authored
Added new target support to Boolean, String, Number Object (#4368)
JerryScript-DCO-1.0-Signed-off-by: bence gabor kis [email protected]
1 parent e2be8f4 commit 1937f82

9 files changed

+211
-17
lines changed

jerry-core/ecma/operations/ecma-boolean-object.c

+26-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
#include "jcontext.h"
1617
#include "ecma-alloc.h"
1718
#include "ecma-boolean-object.h"
1819
#include "ecma-builtins.h"
@@ -22,6 +23,7 @@
2223
#include "ecma-helpers.h"
2324
#include "ecma-objects.h"
2425
#include "ecma-objects-general.h"
26+
#include "ecma-function-object.h"
2527

2628
/** \addtogroup ecma ECMA
2729
* @{
@@ -42,13 +44,28 @@ ecma_value_t
4244
ecma_op_create_boolean_object (ecma_value_t arg) /**< argument passed to the Boolean constructor */
4345
{
4446
bool boolean_value = ecma_op_to_boolean (arg);
47+
ecma_builtin_id_t proto_id;
4548

4649
#if ENABLED (JERRY_BUILTIN_BOOLEAN)
47-
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_BOOLEAN_PROTOTYPE);
50+
proto_id = ECMA_BUILTIN_ID_BOOLEAN_PROTOTYPE;
4851
#else /* ENABLED (JERRY_BUILTIN_BOOLEAN) */
49-
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_OBJECT_PROTOTYPE);
52+
proto_id = ECMA_BUILTIN_ID_OBJECT_PROTOTYPE;
5053
#endif /* !(ENABLED (JERRY_BUILTIN_BOOLEAN) */
5154

55+
ecma_object_t *prototype_obj_p = ecma_builtin_get (proto_id);
56+
57+
#if ENABLED (JERRY_ESNEXT)
58+
ecma_object_t *new_target = JERRY_CONTEXT (current_new_target);
59+
if (new_target)
60+
{
61+
prototype_obj_p = ecma_op_get_prototype_from_constructor (new_target, proto_id);
62+
63+
if (JERRY_UNLIKELY (prototype_obj_p == NULL))
64+
{
65+
return ECMA_VALUE_ERROR;
66+
}
67+
}
68+
#endif /* ENABLED (JERRY_ESNEXT) */
5269
ecma_object_t *object_p = ecma_create_object (prototype_obj_p,
5370
sizeof (ecma_extended_object_t),
5471
ECMA_OBJECT_TYPE_CLASS);
@@ -57,6 +74,13 @@ ecma_op_create_boolean_object (ecma_value_t arg) /**< argument passed to the Boo
5774
ext_object_p->u.class_prop.class_id = LIT_MAGIC_STRING_BOOLEAN_UL;
5875
ext_object_p->u.class_prop.u.value = ecma_make_boolean_value (boolean_value);
5976

77+
#if ENABLED (JERRY_ESNEXT)
78+
if (new_target)
79+
{
80+
ecma_deref_object (prototype_obj_p);
81+
}
82+
#endif /* ENABLED (JERRY_ESNEXT) */
83+
6084
return ecma_make_object_value (object_p);
6185
} /* ecma_op_create_boolean_object */
6286

jerry-core/ecma/operations/ecma-conversion.c

+39-5
Original file line numberDiff line numberDiff line change
@@ -531,14 +531,22 @@ ecma_value_t
531531
ecma_op_to_object (ecma_value_t value) /**< ecma value */
532532
{
533533
ecma_check_value_type_is_spec_defined (value);
534+
ecma_builtin_id_t proto_id = ECMA_BUILTIN_ID_OBJECT_PROTOTYPE;
535+
uint16_t lit_id;
534536

535537
if (ecma_is_value_number (value))
536538
{
537-
return ecma_op_create_number_object (value);
539+
#if ENABLED (JERRY_BUILTIN_NUMBER)
540+
proto_id = ECMA_BUILTIN_ID_NUMBER_PROTOTYPE;
541+
#endif /* ENABLED (JERRY_BUILTIN_NUMBER) */
542+
lit_id = LIT_MAGIC_STRING_NUMBER_UL;
538543
}
539544
else if (ecma_is_value_string (value))
540545
{
541-
return ecma_op_create_string_object (&value, 1);
546+
#if ENABLED (JERRY_BUILTIN_STRING)
547+
proto_id = ECMA_BUILTIN_ID_STRING_PROTOTYPE;
548+
#endif /* ENABLED (JERRY_BUILTIN_STRING) */
549+
lit_id = LIT_MAGIC_STRING_STRING_UL;
542550
}
543551
else if (ecma_is_value_object (value))
544552
{
@@ -547,7 +555,8 @@ ecma_op_to_object (ecma_value_t value) /**< ecma value */
547555
#if ENABLED (JERRY_ESNEXT)
548556
else if (ecma_is_value_symbol (value))
549557
{
550-
return ecma_op_create_symbol_object (value);
558+
proto_id = ECMA_BUILTIN_ID_SYMBOL_PROTOTYPE;
559+
lit_id = LIT_MAGIC_STRING_SYMBOL_UL;
551560
}
552561
#endif /* ENABLED (JERRY_ESNEXT) */
553562
#if ENABLED (JERRY_BUILTIN_BIGINT)
@@ -566,12 +575,37 @@ ecma_op_to_object (ecma_value_t value) /**< ecma value */
566575
else
567576
{
568577
JERRY_ASSERT (ecma_is_value_boolean (value));
569-
570-
return ecma_op_create_boolean_object (value);
578+
#if ENABLED (JERRY_BUILTIN_BOOLEAN)
579+
proto_id = ECMA_BUILTIN_ID_BOOLEAN_PROTOTYPE;
580+
#endif /* ENABLED (JERRY_BUILTIN_BOOLEAN) */
581+
lit_id = LIT_MAGIC_STRING_BOOLEAN_UL;
571582
}
572583
}
584+
585+
return ecma_op_create_class_object (proto_id, value, lit_id);
573586
} /* ecma_op_to_object */
574587

588+
/**
589+
* Create a ECMA_OBJECT_TYPE_CLASS object from the given arguments.
590+
*
591+
* @return ecma_value - constructed object
592+
*/
593+
ecma_value_t
594+
ecma_op_create_class_object (ecma_builtin_id_t proto_id, /**< prototype id */
595+
ecma_value_t value, /**< ecma value */
596+
uint16_t class_id) /**< magic string id */
597+
{
598+
ecma_object_t *object_p = ecma_create_object (ecma_builtin_get (proto_id),
599+
sizeof (ecma_extended_object_t),
600+
ECMA_OBJECT_TYPE_CLASS);
601+
602+
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
603+
ext_object_p->u.class_prop.class_id = class_id;
604+
ext_object_p->u.class_prop.u.value = ecma_copy_value_if_not_object (value);
605+
606+
return ecma_make_object_value (object_p);
607+
} /* ecma_op_create_class_object */
608+
575609
/**
576610
* FromPropertyDescriptor operation.
577611
*

jerry-core/ecma/operations/ecma-conversion.h

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "ecma-globals.h"
2020
#include "ecma-helpers.h"
21+
#include "ecma-builtins.h"
2122

2223
/** \addtogroup ecma ECMA
2324
* @{
@@ -59,6 +60,9 @@ ecma_string_t *ecma_op_to_string (ecma_value_t value);
5960
ecma_string_t *ecma_op_to_property_key (ecma_value_t value);
6061
ecma_value_t ecma_op_to_object (ecma_value_t value);
6162
bool ecma_op_is_integer (ecma_number_t value);
63+
ecma_value_t ecma_op_create_class_object (ecma_builtin_id_t proto_id,
64+
ecma_value_t value,
65+
uint16_t lit_id);
6266
ecma_value_t ecma_op_to_integer (ecma_value_t value, ecma_number_t *number_p);
6367
ecma_value_t ecma_op_to_length (ecma_value_t value, ecma_length_t *length);
6468
#if ENABLED (JERRY_ESNEXT)

jerry-core/ecma/operations/ecma-number-object.c

+23-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "ecma-number-object.h"
2323
#include "ecma-objects.h"
2424
#include "ecma-objects-general.h"
25+
#include "ecma-function-object.h"
26+
#include "jcontext.h"
2527

2628
/** \addtogroup ecma ECMA
2729
* @{
@@ -50,12 +52,24 @@ ecma_op_create_number_object (ecma_value_t arg) /**< argument passed to the Numb
5052
}
5153

5254
conv_to_num_completion = ecma_make_number_value (num);
55+
ecma_builtin_id_t proto_id;
5356
#if ENABLED (JERRY_BUILTIN_NUMBER)
54-
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_NUMBER_PROTOTYPE);
57+
proto_id = ECMA_BUILTIN_ID_NUMBER_PROTOTYPE;
5558
#else /* ENABLED (JERRY_BUILTIN_NUMBER) */
56-
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_OBJECT_PROTOTYPE);
59+
proto_id = ECMA_BUILTIN_ID_OBJECT_PROTOTYPE;
5760
#endif /* ENABLED (JERRY_BUILTIN_NUMBER) */
58-
61+
ecma_object_t *prototype_obj_p = ecma_builtin_get (proto_id);
62+
#if ENABLED (JERRY_ESNEXT)
63+
ecma_object_t *new_target = JERRY_CONTEXT (current_new_target);
64+
if (new_target)
65+
{
66+
prototype_obj_p = ecma_op_get_prototype_from_constructor (new_target, proto_id);
67+
if (JERRY_UNLIKELY (prototype_obj_p == NULL))
68+
{
69+
return ECMA_VALUE_ERROR;
70+
}
71+
}
72+
#endif /* ENABLED (JERRY_ESNEXT) */
5973
ecma_object_t *object_p = ecma_create_object (prototype_obj_p,
6074
sizeof (ecma_extended_object_t),
6175
ECMA_OBJECT_TYPE_CLASS);
@@ -65,7 +79,12 @@ ecma_op_create_number_object (ecma_value_t arg) /**< argument passed to the Numb
6579

6680
/* Pass reference (no need to free conv_to_num_completion). */
6781
ext_object_p->u.class_prop.u.value = conv_to_num_completion;
68-
82+
#if ENABLED (JERRY_ESNEXT)
83+
if (new_target)
84+
{
85+
ecma_deref_object (prototype_obj_p);
86+
}
87+
#endif /* ENABLED (JERRY_ESNEXT) */
6988
return ecma_make_object_value (object_p);
7089
} /* ecma_op_create_number_object */
7190

jerry-core/ecma/operations/ecma-string-object.c

+23-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "ecma-objects.h"
2323
#include "ecma-objects-general.h"
2424
#include "ecma-string-object.h"
25+
#include "ecma-function-object.h"
26+
#include "jcontext.h"
2527

2628
/** \addtogroup ecma ECMA
2729
* @{
@@ -60,12 +62,24 @@ ecma_op_create_string_object (const ecma_value_t *arguments_list_p, /**< list of
6062
prim_value = ecma_make_string_value (str_p);
6163
}
6264

65+
ecma_builtin_id_t proto_id;
6366
#if ENABLED (JERRY_BUILTIN_STRING)
64-
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_STRING_PROTOTYPE);
67+
proto_id = ECMA_BUILTIN_ID_STRING_PROTOTYPE;
6568
#else /* !ENABLED (JERRY_BUILTIN_STRING) */
66-
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_OBJECT_PROTOTYPE);
69+
proto_id = ECMA_BUILTIN_ID_OBJECT_PROTOTYPE;
6770
#endif /* ENABLED (JERRY_BUILTIN_STRING) */
68-
71+
ecma_object_t *prototype_obj_p = ecma_builtin_get (proto_id);
72+
#if ENABLED (JERRY_ESNEXT)
73+
ecma_object_t *new_target = JERRY_CONTEXT (current_new_target);
74+
if (new_target)
75+
{
76+
prototype_obj_p = ecma_op_get_prototype_from_constructor (new_target, proto_id);
77+
if (JERRY_UNLIKELY (prototype_obj_p == NULL))
78+
{
79+
return ECMA_VALUE_ERROR;
80+
}
81+
}
82+
#endif /* ENABLED (JERRY_ESNEXT) */
6983
ecma_object_t *object_p = ecma_create_object (prototype_obj_p,
7084
sizeof (ecma_extended_object_t),
7185
ECMA_OBJECT_TYPE_CLASS);
@@ -74,6 +88,12 @@ ecma_op_create_string_object (const ecma_value_t *arguments_list_p, /**< list of
7488
ext_object_p->u.class_prop.class_id = LIT_MAGIC_STRING_STRING_UL;
7589
ext_object_p->u.class_prop.u.value = prim_value;
7690

91+
#if ENABLED (JERRY_ESNEXT)
92+
if (new_target)
93+
{
94+
ecma_deref_object (prototype_obj_p);
95+
}
96+
#endif /* ENABLED (JERRY_ESNEXT) */
7797
return ecma_make_object_value (object_p);
7898
} /* ecma_op_create_string_object */
7999

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
try {
16+
Reflect.construct (Boolean, true);
17+
assert (false);
18+
} catch (e) {
19+
assert (e instanceof TypeError);
20+
}
21+
22+
try {
23+
Reflect.construct (Boolean, false);
24+
assert (false);
25+
} catch (e) {
26+
assert (e instanceof TypeError);
27+
}
28+
29+
class MyBoolean extends Boolean {};
30+
var b1= new MyBoolean();
31+
assert(Object.getPrototypeOf(b1) == MyBoolean.prototype)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
try {
16+
Reflect.construct (Number);
17+
assert (false);
18+
} catch (e) {
19+
assert (e instanceof TypeError);
20+
}
21+
22+
try {
23+
Reflect.construct (Number, 1);
24+
assert (false);
25+
} catch (e) {
26+
assert (e instanceof TypeError);
27+
}
28+
29+
class MyNumber extends Number {};
30+
var n1= new MyNumber();
31+
32+
assert(Object.getPrototypeOf(n1) == MyNumber.prototype)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
try {
17+
Reflect.construct (String, "");
18+
assert (false);
19+
} catch (e) {
20+
assert (e instanceof TypeError);
21+
}
22+
23+
try {
24+
Reflect.construct (String, "randomText");
25+
assert (false);
26+
} catch (e) {
27+
assert (e instanceof TypeError);
28+
}
29+
30+
class MyString extends String {};
31+
var s1= new MyString();
32+
33+
assert(Object.getPrototypeOf(s1) == MyString.prototype)

tests/test262-esnext-excludelist.xml

-3
Original file line numberDiff line numberDiff line change
@@ -9641,7 +9641,6 @@
96419641
<test id="built-ins/Atomics/waitAsync/value-not-equal-agent.js"><reason></reason></test>
96429642
<test id="built-ins/Atomics/waitAsync/waiterlist-block-indexedposition-wake.js"><reason></reason></test>
96439643
<test id="built-ins/Atomics/waitAsync/was-woken-before-timeout.js"><reason></reason></test>
9644-
<test id="built-ins/Boolean/proto-from-ctor-realm.js"><reason></reason></test>
96459644
<test id="built-ins/DataView/proto-from-ctor-realm-sab.js"><reason></reason></test>
96469645
<test id="built-ins/Error/proto-from-ctor-realm.js"><reason></reason></test>
96479646
<test id="built-ins/FinalizationRegistry/gc-has-one-chance-to-call-cleanupCallback.js"><reason></reason></test>
@@ -9656,7 +9655,6 @@
96569655
<test id="built-ins/NativeErrors/SyntaxError/proto-from-ctor-realm.js"><reason></reason></test>
96579656
<test id="built-ins/NativeErrors/TypeError/proto-from-ctor-realm.js"><reason></reason></test>
96589657
<test id="built-ins/NativeErrors/URIError/proto-from-ctor-realm.js"><reason></reason></test>
9659-
<test id="built-ins/Number/proto-from-ctor-realm.js"><reason></reason></test>
96609658
<test id="built-ins/Proxy/defineProperty/targetdesc-undefined-target-is-not-extensible-realm.js"><reason></reason></test>
96619659
<test id="built-ins/Proxy/get-fn-realm-recursive.js"><reason></reason></test>
96629660
<test id="built-ins/Proxy/get-fn-realm.js"><reason></reason></test>
@@ -9668,7 +9666,6 @@
96689666
<test id="built-ins/RegExp/prototype/sticky/cross-realm.js"><reason></reason></test>
96699667
<test id="built-ins/RegExp/prototype/unicode/cross-realm.js"><reason></reason></test>
96709668
<test id="built-ins/SharedArrayBuffer/proto-from-ctor-realm.js"><reason></reason></test>
9671-
<test id="built-ins/String/proto-from-ctor-realm.js"><reason></reason></test>
96729669
<test id="built-ins/TypedArrayConstructors/ctors-bigint/buffer-arg/proto-from-ctor-realm-sab.js"><reason></reason></test>
96739670
<test id="built-ins/TypedArrayConstructors/ctors-bigint/buffer-arg/proto-from-ctor-realm.js"><reason></reason></test>
96749671
<test id="built-ins/TypedArrayConstructors/ctors-bigint/length-arg/proto-from-ctor-realm.js"><reason></reason></test>

0 commit comments

Comments
 (0)