Skip to content

Add missing toObject conversion for PrivateSet operation #4953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions jerry-core/vm/opcodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1398,40 +1398,58 @@ opfunc_private_set (ecma_value_t base, /**< this object */
ecma_value_t property, /**< property name */
ecma_value_t value) /**< ecma value */
{
ecma_object_t *obj_p = ecma_get_object_from_value (base);
ecma_value_t base_obj = ecma_op_to_object (base);

if (ECMA_IS_VALUE_ERROR (base_obj))
{
return base_obj;
}

ecma_object_t *obj_p = ecma_get_object_from_value (base_obj);
ecma_string_t *prop_name_p = ecma_get_string_from_value (property);
ecma_string_t *private_key_p = NULL;

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

ecma_value_t result;

if (prop_p == NULL)
{
return ecma_raise_type_error (ECMA_ERR_CANNOT_WRITE_PRIVATE_MEMBER_TO_AN_OBJECT_WHOSE_CLASS_DID_NOT_DECLARE_IT);
result = ecma_raise_type_error (ECMA_ERR_CANNOT_WRITE_PRIVATE_MEMBER_TO_AN_OBJECT_WHOSE_CLASS_DID_NOT_DECLARE_IT);
}

if (*prop_p & ECMA_PROPERTY_FLAG_DATA)
else if (*prop_p & ECMA_PROPERTY_FLAG_DATA)
{
JERRY_ASSERT (ecma_prop_name_is_symbol (private_key_p));

if (private_key_p->u.hash & ECMA_SYMBOL_FLAG_PRIVATE_INSTANCE_METHOD)
{
return ecma_raise_type_error (ECMA_ERR_PRIVATE_METHOD_IS_NOT_WRITABLE);
result = ecma_raise_type_error (ECMA_ERR_PRIVATE_METHOD_IS_NOT_WRITABLE);
}
else
{
ecma_value_assign_value (&ECMA_PROPERTY_VALUE_PTR (prop_p)->value, value);
result = ecma_copy_value (value);
}

ecma_value_assign_value (&ECMA_PROPERTY_VALUE_PTR (prop_p)->value, value);
return ecma_copy_value (value);
}
else
{
ecma_getter_setter_pointers_t *get_set_pair_p = ecma_get_named_accessor_property (ECMA_PROPERTY_VALUE_PTR (prop_p));

ecma_getter_setter_pointers_t *get_set_pair_p = ecma_get_named_accessor_property (ECMA_PROPERTY_VALUE_PTR (prop_p));
if (get_set_pair_p->setter_cp == JMEM_CP_NULL)
{
result = ecma_raise_type_error (ECMA_ERR_PRIVATE_FIELD_WAS_DEFINED_WITHOUT_A_SETTER);
}
else
{
ecma_object_t *setter_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, get_set_pair_p->setter_cp);

if (get_set_pair_p->setter_cp == JMEM_CP_NULL)
{
return ecma_raise_type_error (ECMA_ERR_PRIVATE_FIELD_WAS_DEFINED_WITHOUT_A_SETTER);
result = ecma_op_function_call (setter_p, base, &value, 1);
}
}

ecma_object_t *setter_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, get_set_pair_p->setter_cp);
ecma_deref_object (obj_p);

return ecma_op_function_call (setter_p, base, &value, 1);
return result;
} /* opfunc_private_set */

/**
Expand Down
33 changes: 33 additions & 0 deletions tests/jerry/es.next/regression-test-issue-4936.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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.

class JSEtest {
set #m(v) {
this._v = v;
}

method() {
let self = !this;
self.#m = 'Test262';
}
}

let c = new JSEtest();

try {
c.method();
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
1 change: 0 additions & 1 deletion tests/test262-esnext-excludelist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@
<test id="language/statements/class/elements/private-method-double-initialisation-get-and-set.js"><reason></reason></test>
<test id="language/statements/class/elements/private-method-double-initialisation-get.js"><reason></reason></test>
<test id="language/statements/class/elements/private-method-double-initialisation-set.js"><reason></reason></test>
<test id="language/statements/class/elements/privatefieldput-primitive-receiver.js"><reason></reason></test>
<test id="language/statements/class/elements/privatefieldset-evaluation-order-1.js"><reason></reason></test>
<test id="language/statements/class/elements/privatefieldset-evaluation-order-2.js"><reason></reason></test>
<test id="language/statements/class/elements/privatefieldset-evaluation-order-3.js"><reason></reason></test>
Expand Down