Implement Proxy object [[DefineOwnProperty]] internal method (#3662)

The algorithm is based on ECMA-262 v6, 9.5.6

JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi aszilagy@inf.u-szeged.hu
This commit is contained in:
Szilagyi Adam
2020-04-28 09:51:01 +02:00
committed by GitHub
parent f254b1a8b7
commit 62730f2ae1
4 changed files with 407 additions and 3 deletions
@@ -636,6 +636,130 @@ ecma_op_general_object_define_own_property (ecma_object_t *object_p, /**< the ob
#undef ECMA_PROPERTY_TYPE_GENERIC
#if ENABLED (JERRY_ES2015)
/**
* The IsCompatiblePropertyDescriptor method for Proxy object internal methods
*
* See also:
* ECMAScript v6, 9.1.6.2
*
* @return bool
*/
bool
ecma_op_is_compatible_property_descriptor (const ecma_property_descriptor_t *desc_p, /**< target descriptor */
const ecma_property_descriptor_t *current_p, /**< current descriptor */
bool is_extensible) /**< true - if target object is extensible
false - otherwise */
{
JERRY_ASSERT (desc_p != NULL);
/* 2. */
if (current_p == NULL)
{
return is_extensible;
}
/* 3. */
if (desc_p->flags == 0)
{
return true;
}
/* 4. */
if ((current_p->flags & desc_p->flags) == desc_p->flags)
{
if ((current_p->flags & ECMA_PROP_IS_VALUE_DEFINED)
&& ecma_op_same_value (current_p->value, desc_p->value))
{
return true;
}
if ((current_p->flags & (ECMA_PROP_IS_GET_DEFINED | ECMA_PROP_IS_SET_DEFINED)
&& current_p->get_p == desc_p->get_p
&& current_p->set_p == desc_p->set_p))
{
return true;
}
}
/* 5. */
if (!(current_p->flags & ECMA_PROP_IS_CONFIGURABLE))
{
if (desc_p->flags & ECMA_PROP_IS_CONFIGURABLE)
{
return false;
}
if ((desc_p->flags & ECMA_PROP_IS_ENUMERABLE_DEFINED)
&& ((current_p->flags & ECMA_PROP_IS_ENUMERABLE) != (desc_p->flags & ECMA_PROP_IS_ENUMERABLE)))
{
return false;
}
}
const uint32_t accessor_desc_flags = (ECMA_PROP_IS_SET_DEFINED | ECMA_PROP_IS_GET_DEFINED);
const uint32_t data_desc_flags = (ECMA_PROP_IS_VALUE_DEFINED | ECMA_PROP_IS_WRITABLE_DEFINED);
bool desc_is_accessor = (desc_p->flags & accessor_desc_flags) != 0;
bool desc_is_data = (desc_p->flags & data_desc_flags) != 0;
bool current_is_data = (current_p->flags & data_desc_flags) != 0;
/* 6. */
if (!desc_is_accessor && !desc_is_data)
{
return true;
}
/* 7. */
if (current_is_data != desc_is_data)
{
return (current_p->flags & ECMA_PROP_IS_CONFIGURABLE) != 0;
}
/* 8. */
if (current_is_data)
{
if (!(current_p->flags & ECMA_PROP_IS_CONFIGURABLE))
{
if (!(current_p->flags & ECMA_PROP_IS_WRITABLE)
&& (desc_p->flags & ECMA_PROP_IS_WRITABLE))
{
return false;
}
if (!(current_p->flags & ECMA_PROP_IS_WRITABLE)
&& (desc_p->flags & ECMA_PROP_IS_VALUE_DEFINED)
&& !ecma_op_same_value (desc_p->value, current_p->value))
{
return false;
}
}
return true;
}
JERRY_ASSERT ((current_p->flags & (ECMA_PROP_IS_GET_DEFINED | ECMA_PROP_IS_SET_DEFINED)) != 0);
JERRY_ASSERT ((desc_p->flags & (ECMA_PROP_IS_GET_DEFINED | ECMA_PROP_IS_SET_DEFINED)) != 0);
/* 9. */
if (!(current_p->flags & ECMA_PROP_IS_CONFIGURABLE))
{
if ((desc_p->flags & ECMA_PROP_IS_SET_DEFINED)
&& desc_p->set_p != current_p->set_p)
{
return false;
}
if ((desc_p->flags & ECMA_PROP_IS_GET_DEFINED)
&& desc_p->get_p != current_p->get_p)
{
return false;
}
}
return true;
} /* ecma_op_is_compatible_property_descriptor */
#endif /* ENABLED (JERRY_ES2015) */
/**
* @}
* @}
@@ -37,6 +37,12 @@ ecma_value_t ecma_op_general_object_ordinary_value (ecma_object_t *obj_p, ecma_p
ecma_value_t ecma_op_general_object_define_own_property (ecma_object_t *object_p, ecma_string_t *property_name_p,
const ecma_property_descriptor_t *property_desc_p);
#if ENABLED (JERRY_ES2015)
bool ecma_op_is_compatible_property_descriptor (const ecma_property_descriptor_t *desc_p,
const ecma_property_descriptor_t *current_p,
bool is_extensible);
#endif /* ENABLED (JERRY_ES2015) */
/**
* @}
* @}
+127 -2
View File
@@ -694,8 +694,133 @@ ecma_proxy_object_define_own_property (ecma_object_t *obj_p, /**< proxy object *
const ecma_property_descriptor_t *prop_desc_p) /**< property descriptor */
{
JERRY_ASSERT (ECMA_OBJECT_IS_PROXY (obj_p));
JERRY_UNUSED_3 (obj_p, prop_name_p, prop_desc_p);
return ecma_raise_type_error (ECMA_ERR_MSG ("UNIMPLEMENTED: Proxy.[[DefineOwnProperty]]"));
ecma_proxy_object_t *proxy_obj_p = (ecma_proxy_object_t *) obj_p;
/* 2. */
ecma_value_t handler = proxy_obj_p->handler;
/* 3-6. */
ecma_value_t trap = ecma_validate_proxy_object (handler, LIT_MAGIC_STRING_DEFINE_PROPERTY_UL);
/* 7. */
if (ECMA_IS_VALUE_ERROR (trap))
{
return trap;
}
ecma_value_t target = proxy_obj_p->target;
ecma_object_t *target_obj_p = ecma_get_object_from_value (target);
/* 8. */
if (ecma_is_value_undefined (trap))
{
return ecma_op_object_define_own_property (target_obj_p, prop_name_p, prop_desc_p);
}
/* 9. */
ecma_object_t *desc_obj = ecma_op_from_property_descriptor (prop_desc_p);
ecma_object_t *func_obj_p = ecma_get_object_from_value (trap);
ecma_value_t prop_value = ecma_make_prop_name_value (prop_name_p);
ecma_value_t desc_obj_value = ecma_make_object_value (desc_obj);
ecma_value_t args[] = {target, prop_value, desc_obj_value};
/* 10. */
ecma_value_t trap_result = ecma_op_function_call (func_obj_p, handler, args, 3);
ecma_deref_object (func_obj_p);
ecma_deref_object (desc_obj);
/* 11. */
if (ECMA_IS_VALUE_ERROR (trap_result))
{
return trap_result;
}
bool boolean_trap_result = ecma_op_to_boolean (trap_result);
ecma_free_value (trap_result);
/* 12. */
if (!boolean_trap_result)
{
return ECMA_VALUE_FALSE;
}
/* 13. */
ecma_property_descriptor_t target_desc;
ecma_value_t status = ecma_op_object_get_own_property_descriptor (target_obj_p, prop_name_p, &target_desc);
/* 14. */
if (ECMA_IS_VALUE_ERROR (status))
{
return status;
}
bool target_prop_found = ecma_is_value_true (status);
/* 15. */
ecma_value_t extensible_target = ecma_builtin_object_object_is_extensible (target_obj_p);
bool is_target_ext = ecma_is_value_true (extensible_target);
/* 16. */
if (ECMA_IS_VALUE_ERROR (extensible_target))
{
if (target_prop_found)
{
ecma_free_property_descriptor (&target_desc);
}
return extensible_target;
}
/* 17. */
bool setting_config_false = ((prop_desc_p->flags & ECMA_PROP_IS_CONFIGURABLE_DEFINED)
&& !(prop_desc_p->flags & ECMA_PROP_IS_CONFIGURABLE));
/* 19. */
if (!target_prop_found)
{
if (!is_target_ext)
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Trap returned truish for adding property "
"to the non-extensible target"));
}
if (setting_config_false)
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Trap returned truish for defining non-configurable property "
"which is non-existent in the target"));
}
}
/* 20. */
else
{
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
if (!ecma_op_is_compatible_property_descriptor (prop_desc_p, &target_desc, is_target_ext))
{
ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("Trap returned truish for adding property that is "
"incompatible with the existing property in the target"));
}
else if (setting_config_false && (target_desc.flags & ECMA_PROP_IS_CONFIGURABLE))
{
ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("Trap returned truish for defining non-configurable property "
"which is configurable in the target"));
}
ecma_free_property_descriptor (&target_desc);
if (ECMA_IS_VALUE_ERROR (ret_value))
{
return ret_value;
}
}
return ECMA_VALUE_TRUE;
} /* ecma_proxy_object_define_own_property */
/**
+150 -1
View File
@@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// TODO: Update these tests when the internal routine has been implemented
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var target = function () {};
var handler = { defineProperty (target) {
@@ -25,3 +27,150 @@ var proxy = new Proxy(target, handler);
// 22.1.2.3.8.c
Array.of.call(proxy, 5)
// test basic functionality
var g_target, g_name;
var handler = {
defineProperty: function(target, name, desc) {
g_target = target;
g_name = name;
return true;
}
}
var target = {};
var proxy = new Proxy(target, handler);
var desc = { value: 1, writable: true, configurable: true };
Object.defineProperty(proxy, "foo", desc);
assert(target === g_target);
assert("foo" === g_name);
var handler = {
defineProperty: function(target, name, desc) {
Object.defineProperty(target, name, desc);
}
}
var proxy = new Proxy(target, handler);
Object.defineProperty(proxy, "bar", desc);
assert(proxy.bar === 1);
/* TODO - remove this comment when [[GetOwnProperty]] is implemented
proxy.bar = 2;
assert(proxy.bar === 2);
*/
delete proxy.bar;
assert(proxy.bar === undefined);
/* TODO - remove this comment when [[GetOwnProperty]] is implemented
Object.defineProperty(proxy, "name", {
get() {
return this._name;
},
set(value) {
this._name = value;
}
});
proxy.name = "foo";
assert(proxy.name === "foo");
assert(target.name === "foo");
*/
// test when trap is not callable
var target = {};
var handler = {
defineProperty: 1
}
var proxy = new Proxy(target, handler);
try {
Object.defineProperty(proxy, "foo", {value: "foo"});
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
// test when trap is undefined
var target = {};
var handler = {
defineProperty: undefined
}
var proxy = new Proxy(target, handler);
var desc = { value: 1 };
Object.defineProperty(proxy, "prop1", desc);
assert(proxy.prop1 === 1);
var target2 = {};
var proxy2 = new Proxy(target2, {});
Object.defineProperty(proxy2, "prop2", desc);
assert(proxy2.prop2 === 1);
// test when invariants gets violated
var target = {};
var handler = {
defineProperty: function(target, name, desc) {
return true;
}
}
var proxy = new Proxy(target, handler);
Object.preventExtensions(target);
try {
Object.defineProperty(proxy, "foo", {value: 1});
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
var target = {};
var desc = {value: 1, writable: true, configurable: false, enumerable: true};
var proxy = new Proxy(target, handler);
try {
Object.defineProperty(proxy, "foo", desc);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
var target = {};
var handler = {
defineProperty: function(target, name, desc) {
return true;
}
}
var proxy = new Proxy(target, handler);
Object.defineProperty(target, "foo", {value: 1, writable: false, configurable: false});
try {
Object.defineProperty(proxy, 'foo', {value: 2});
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
target.bar = "baz";
try {
Object.defineProperty(proxy, 'bar', {value: 2, configurable: false});
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}