Skip to content

Commit 85c7987

Browse files
author
Robert Fancsik
authored
Add missing toObject conversion for PrivateSet operation (#4953)
This patch fixes #4936. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent dfbd89d commit 85c7987

File tree

3 files changed

+65
-15
lines changed

3 files changed

+65
-15
lines changed

jerry-core/vm/opcodes.c

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,40 +1403,58 @@ opfunc_private_set (ecma_value_t base, /**< this object */
14031403
ecma_value_t property, /**< property name */
14041404
ecma_value_t value) /**< ecma value */
14051405
{
1406-
ecma_object_t *obj_p = ecma_get_object_from_value (base);
1406+
ecma_value_t base_obj = ecma_op_to_object (base);
1407+
1408+
if (ECMA_IS_VALUE_ERROR (base_obj))
1409+
{
1410+
return base_obj;
1411+
}
1412+
1413+
ecma_object_t *obj_p = ecma_get_object_from_value (base_obj);
14071414
ecma_string_t *prop_name_p = ecma_get_string_from_value (property);
14081415
ecma_string_t *private_key_p = NULL;
14091416

14101417
ecma_property_t *prop_p = opfunc_find_private_element (obj_p, prop_name_p, &private_key_p, true);
14111418

1419+
ecma_value_t result;
1420+
14121421
if (prop_p == NULL)
14131422
{
1414-
return ecma_raise_type_error (ECMA_ERR_CANNOT_WRITE_PRIVATE_MEMBER_TO_AN_OBJECT_WHOSE_CLASS_DID_NOT_DECLARE_IT);
1423+
result = ecma_raise_type_error (ECMA_ERR_CANNOT_WRITE_PRIVATE_MEMBER_TO_AN_OBJECT_WHOSE_CLASS_DID_NOT_DECLARE_IT);
14151424
}
1416-
1417-
if (*prop_p & ECMA_PROPERTY_FLAG_DATA)
1425+
else if (*prop_p & ECMA_PROPERTY_FLAG_DATA)
14181426
{
14191427
JERRY_ASSERT (ecma_prop_name_is_symbol (private_key_p));
14201428

14211429
if (private_key_p->u.hash & ECMA_SYMBOL_FLAG_PRIVATE_INSTANCE_METHOD)
14221430
{
1423-
return ecma_raise_type_error (ECMA_ERR_PRIVATE_METHOD_IS_NOT_WRITABLE);
1431+
result = ecma_raise_type_error (ECMA_ERR_PRIVATE_METHOD_IS_NOT_WRITABLE);
1432+
}
1433+
else
1434+
{
1435+
ecma_value_assign_value (&ECMA_PROPERTY_VALUE_PTR (prop_p)->value, value);
1436+
result = ecma_copy_value (value);
14241437
}
1425-
1426-
ecma_value_assign_value (&ECMA_PROPERTY_VALUE_PTR (prop_p)->value, value);
1427-
return ecma_copy_value (value);
14281438
}
1439+
else
1440+
{
1441+
ecma_getter_setter_pointers_t *get_set_pair_p = ecma_get_named_accessor_property (ECMA_PROPERTY_VALUE_PTR (prop_p));
14291442

1430-
ecma_getter_setter_pointers_t *get_set_pair_p = ecma_get_named_accessor_property (ECMA_PROPERTY_VALUE_PTR (prop_p));
1443+
if (get_set_pair_p->setter_cp == JMEM_CP_NULL)
1444+
{
1445+
result = ecma_raise_type_error (ECMA_ERR_PRIVATE_FIELD_WAS_DEFINED_WITHOUT_A_SETTER);
1446+
}
1447+
else
1448+
{
1449+
ecma_object_t *setter_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, get_set_pair_p->setter_cp);
14311450

1432-
if (get_set_pair_p->setter_cp == JMEM_CP_NULL)
1433-
{
1434-
return ecma_raise_type_error (ECMA_ERR_PRIVATE_FIELD_WAS_DEFINED_WITHOUT_A_SETTER);
1451+
result = ecma_op_function_call (setter_p, base, &value, 1);
1452+
}
14351453
}
14361454

1437-
ecma_object_t *setter_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, get_set_pair_p->setter_cp);
1455+
ecma_deref_object (obj_p);
14381456

1439-
return ecma_op_function_call (setter_p, base, &value, 1);
1457+
return result;
14401458
} /* opfunc_private_set */
14411459

14421460
/**
Lines changed: 33 additions & 0 deletions
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+
class JSEtest {
16+
set #m(v) {
17+
this._v = v;
18+
}
19+
20+
method() {
21+
let self = !this;
22+
self.#m = 'Test262';
23+
}
24+
}
25+
26+
let c = new JSEtest();
27+
28+
try {
29+
c.method();
30+
assert(false);
31+
} catch (e) {
32+
assert(e instanceof TypeError);
33+
}

tests/test262-esnext-excludelist.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,6 @@
522522
<test id="language/statements/class/elements/private-method-double-initialisation-get-and-set.js"><reason></reason></test>
523523
<test id="language/statements/class/elements/private-method-double-initialisation-get.js"><reason></reason></test>
524524
<test id="language/statements/class/elements/private-method-double-initialisation-set.js"><reason></reason></test>
525-
<test id="language/statements/class/elements/privatefieldput-primitive-receiver.js"><reason></reason></test>
526525
<test id="language/statements/class/elements/privatefieldset-evaluation-order-1.js"><reason></reason></test>
527526
<test id="language/statements/class/elements/privatefieldset-evaluation-order-2.js"><reason></reason></test>
528527
<test id="language/statements/class/elements/privatefieldset-evaluation-order-3.js"><reason></reason></test>

0 commit comments

Comments
 (0)