From 6ff299c8314afbca083b60cf1d23000b1808f2b5 Mon Sep 17 00:00:00 2001 From: Daniella Barsony Date: Thu, 30 Apr 2020 15:51:23 +0200 Subject: [PATCH] Add [[GetOwnProperty]] internal Proxy method (#3672) Depends on #3662 JerryScript-DCO-1.0-Signed-off-by: Daniella Barsony bella@inf.u-szeged.hu --- .../ecma/operations/ecma-objects-general.c | 22 ++ .../ecma/operations/ecma-objects-general.h | 2 + .../ecma/operations/ecma-proxy-object.c | 161 +++++++++++- .../proxy_get_own_property_descriptor.js | 234 ++++++++++++++++-- 4 files changed, 398 insertions(+), 21 deletions(-) diff --git a/jerry-core/ecma/operations/ecma-objects-general.c b/jerry-core/ecma/operations/ecma-objects-general.c index 255023c7f..a0b8517bb 100644 --- a/jerry-core/ecma/operations/ecma-objects-general.c +++ b/jerry-core/ecma/operations/ecma-objects-general.c @@ -758,6 +758,28 @@ ecma_op_is_compatible_property_descriptor (const ecma_property_descriptor_t *des return true; } /* ecma_op_is_compatible_property_descriptor */ + +/** + * CompletePropertyDescriptor method for proxy internal method + * + * See also: + * ECMA-262 v6, 6.2.4.5 + */ +void +ecma_op_to_complete_property_descriptor (ecma_property_descriptor_t *desc_p) /**< target descriptor */ +{ + /* 4. */ + if (!(desc_p->flags & (ECMA_PROP_IS_GET_DEFINED | ECMA_PROP_IS_SET_DEFINED))) + { + /* a. */ + desc_p->flags |= ECMA_PROP_IS_VALUE_DEFINED; + } + /* 5. */ + else + { + desc_p->flags |= (ECMA_PROP_IS_GET_DEFINED | ECMA_PROP_IS_SET_DEFINED); + } +} /* ecma_op_to_complete_property_descriptor */ #endif /* ENABLED (JERRY_ES2015) */ /** diff --git a/jerry-core/ecma/operations/ecma-objects-general.h b/jerry-core/ecma/operations/ecma-objects-general.h index 14e48dd51..7ab797194 100644 --- a/jerry-core/ecma/operations/ecma-objects-general.h +++ b/jerry-core/ecma/operations/ecma-objects-general.h @@ -38,6 +38,8 @@ ecma_value_t ecma_op_general_object_define_own_property (ecma_object_t *object_p const ecma_property_descriptor_t *property_desc_p); #if ENABLED (JERRY_ES2015) +void ecma_op_to_complete_property_descriptor (ecma_property_descriptor_t *desc_p); + bool ecma_op_is_compatible_property_descriptor (const ecma_property_descriptor_t *desc_p, const ecma_property_descriptor_t *current_p, bool is_extensible); diff --git a/jerry-core/ecma/operations/ecma-proxy-object.c b/jerry-core/ecma/operations/ecma-proxy-object.c index 35618656f..3e831b85e 100644 --- a/jerry-core/ecma/operations/ecma-proxy-object.c +++ b/jerry-core/ecma/operations/ecma-proxy-object.c @@ -672,9 +672,164 @@ ecma_proxy_object_get_own_property_descriptor (ecma_object_t *obj_p, /**< proxy ecma_property_descriptor_t *prop_desc_p) /**< [out] 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.[[GetOwnProperty]]")); + 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_GET_OWN_PROPERTY_DESCRIPTOR_UL); + + ecma_value_t target = proxy_obj_p->target; + + /* 7. */ + if (ECMA_IS_VALUE_ERROR (trap)) + { + return trap; + } + + ecma_object_t *target_obj_p = ecma_get_object_from_value (target); + + /* 8. */ + if (ecma_is_value_undefined (trap)) + { + return ecma_op_object_get_own_property_descriptor (target_obj_p, prop_name_p, 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 args[] = { target, prop_value }; + + /* 9. */ + ecma_value_t trap_result = ecma_op_function_call (func_obj_p, handler, args, 2); + ecma_deref_object (func_obj_p); + + /* 10. */ + if (ECMA_IS_VALUE_ERROR (trap_result)) + { + return trap_result; + } + + /* 11. */ + if (!ecma_is_value_object (trap_result) && !ecma_is_value_undefined (trap_result)) + { + ecma_free_value (trap_result); + return ecma_raise_type_error (ECMA_ERR_MSG ("Trap is not object nor undefined.")); + } + + /* 12. */ + ecma_property_descriptor_t target_desc; + ecma_value_t target_status = ecma_op_object_get_own_property_descriptor (target_obj_p, prop_name_p, &target_desc); + + /* 13. */ + if (ECMA_IS_VALUE_ERROR (target_status)) + { + ecma_free_value (trap_result); + return target_status; + } + + /* 14. */ + if (ecma_is_value_undefined (trap_result)) + { + /* .a */ + if (ecma_is_value_false (target_status)) + { + return ECMA_VALUE_UNDEFINED; + } + /* .b */ + if (!(target_desc.flags & ECMA_PROP_IS_CONFIGURABLE)) + { + ecma_free_property_descriptor (&target_desc); + return ecma_raise_type_error (ECMA_ERR_MSG ("Given property is a non-configurable" + " data property on the proxy target")); + } + + /* .c */ + ecma_free_property_descriptor (&target_desc); + ecma_value_t extensible_target = ecma_builtin_object_object_is_extensible (target_obj_p); + + /* .d */ + if (ECMA_IS_VALUE_ERROR (extensible_target)) + { + return extensible_target; + } + + /* .e */ + JERRY_ASSERT (ecma_is_value_boolean (extensible_target)); + + /* .f */ + if (ecma_is_value_false (extensible_target)) + { + return ecma_raise_type_error (ECMA_ERR_MSG ("Target not extensible.")); + } + + /* .g */ + return ECMA_VALUE_FALSE; + } + + /* 15. */ + ecma_value_t extensible_target = ecma_builtin_object_object_is_extensible (target_obj_p); + + /* 16. */ + if (ECMA_IS_VALUE_ERROR (extensible_target)) + { + if (ecma_is_value_true (target_status)) + { + ecma_free_property_descriptor (&target_desc); + } + ecma_free_value (trap_result); + return extensible_target; + } + + /* 17, 19 */ + ecma_value_t result_val = ecma_op_to_property_descriptor (trap_result, prop_desc_p); + + ecma_op_to_complete_property_descriptor (prop_desc_p); + ecma_free_value (trap_result); + + /* 18. */ + if (ECMA_IS_VALUE_ERROR (result_val)) + { + if (ecma_is_value_true (target_status)) + { + ecma_free_property_descriptor (&target_desc); + } + return result_val; + } + + /* 20. */ + bool is_extensible = ecma_is_value_true (extensible_target); + + bool is_valid = ecma_op_is_compatible_property_descriptor (prop_desc_p, + (ecma_is_value_true (target_status) ? &target_desc : NULL), + is_extensible); + + bool target_has_desc = ecma_is_value_true (target_status); + bool target_is_configurable = false; + + if (target_has_desc) + { + target_is_configurable = ((target_desc.flags & ECMA_PROP_IS_CONFIGURABLE) != 0); + ecma_free_property_descriptor (&target_desc); + } + + /* 21. */ + if (!is_valid) + { + ecma_free_property_descriptor (prop_desc_p); + return ecma_raise_type_error (ECMA_ERR_MSG ("The two descriptors are not compatible.")); + } + + /* 22. */ + else if (!(prop_desc_p->flags & ECMA_PROP_IS_CONFIGURABLE)) + { + if (!target_has_desc || target_is_configurable) + { + ecma_free_property_descriptor (prop_desc_p); + return ecma_raise_type_error (ECMA_ERR_MSG ("Not compatible.")); + } + } + return ECMA_VALUE_TRUE; } /* ecma_proxy_object_get_own_property_descriptor */ /** diff --git a/tests/jerry/es2015/proxy_get_own_property_descriptor.js b/tests/jerry/es2015/proxy_get_own_property_descriptor.js index 4cd88bace..432187281 100644 --- a/tests/jerry/es2015/proxy_get_own_property_descriptor.js +++ b/tests/jerry/es2015/proxy_get_own_property_descriptor.js @@ -12,11 +12,13 @@ // 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 = {}; var handler = { getOwnPropertyDescriptor (target) { - throw 42; + throw "cat"; }}; var proxy = new Proxy(target, handler); @@ -25,48 +27,244 @@ try { // 19.1.3.2.5 Object.prototype.hasOwnProperty.call(proxy); assert(false); -} catch (e) { - assert(e instanceof TypeError); +} catch(e) { + assert(e == "cat"); } try { // 19.1.3.4 Object.prototype.propertyIsEnumerable.call(proxy) assert(false); -} catch (e) { - assert(e instanceof TypeError); +} catch(e) { + assert(e == "cat"); } try { // 19.1.2.6.5 Object.getOwnPropertyDescriptor(proxy) assert(false); -} catch (e) { +} catch(e) { + assert(e == "cat"); +} + +var target = {}; +var configurable_desc = { + value: 123, + configurable: true, + writable: true, + enumerable: false, +}; +Object.defineProperty(target, "configurable", configurable_desc); + +var nonconfigurable_desc = { + value: 234, + configurable: false, + writable: false, + enumerable: true +} +Object.defineProperty(target, "nonconfigurable", nonconfigurable_desc); + +var proxied_desc = { + value: 345, + configurable: true +}; + +var proxied_desc = { + value: 345, + configurable: true +}; + +var handler = { + "getOwnPropertyDescriptor": function(target, name) { + if (name === "proxied") { + return proxied_desc; + } + if (name === "return_null") { + return null; + } + return Object.getOwnPropertyDescriptor(target, name); + } +}; + +var proxy = new Proxy(target, handler); +var proxy_without_handler = new Proxy(target, {}); + +// Checking basic functionality: + +var configurable_obj = Object.getOwnPropertyDescriptor(proxy, "configurable"); + +assert(configurable_desc.value == configurable_obj.value); +assert(configurable_desc.configurable == configurable_obj.configurable); +assert(configurable_desc.writable == configurable_obj.writable); +assert(configurable_desc.enumerable == configurable_obj.enumerable); + +var nonconfigurable_obj = Object.getOwnPropertyDescriptor(proxy, "nonconfigurable"); +assert(nonconfigurable_obj.value == nonconfigurable_desc.value); +assert(nonconfigurable_obj.configurable == nonconfigurable_desc.configurable); +assert(nonconfigurable_obj.writable == nonconfigurable_desc.writable); +assert(nonconfigurable_obj.enumerable == nonconfigurable_desc.enumerable); + +var other_obj = { value: proxied_desc.value, + configurable: proxied_desc.configurable, + enumerable: false, + writable: false } +var proxied_obj = Object.getOwnPropertyDescriptor(proxy, "proxied"); + +assert(other_obj.value == proxied_obj.value); +assert(other_obj.configurable == proxied_obj.configurable); +assert(other_obj.writable == proxied_obj.writable); +assert(other_obj.enumerable == proxied_obj.enumerable); + +var other_obj2 = Object.getOwnPropertyDescriptor(proxy_without_handler, "configurable"); + +assert(other_obj2.value == configurable_desc.value); +assert(other_obj2.configurable == configurable_desc.configurable); +assert(other_obj2.writable == configurable_desc.writable); +assert(other_obj2.enumerable == configurable_desc.enumerable); + +var other_obj3 = Object.getOwnPropertyDescriptor(proxy_without_handler, "nonconfigurable"); + +assert(other_obj3.value == nonconfigurable_desc.value); +assert(other_obj3.configurable == nonconfigurable_desc.configurable); +assert(other_obj3.writable == nonconfigurable_desc.writable); +assert(other_obj3.enumerable == nonconfigurable_desc.enumerable); + +try { + Object.getOwnPropertyDescriptor(proxy, "return_null"); + assert(false); +} catch(e) { assert(e instanceof TypeError); } -/* TODO: enable these test when Proxy.[[isExtensible]] has been implemted +// Checking invariants mentioned explicitly by the ES spec: + +// (Inv-1) "A property cannot be reported as non-existent, if it exists as a +// non-configurable own property of the target object." +handler.getOwnPropertyDescriptor = function(target, name) { return undefined; }; + try { - // 19.1.2.5.2.9.a.i - Object.freeze(proxy) + Object.getOwnPropertyDescriptor(proxy, "nonconfigurable"); assert(false); -} catch (e) { +} catch(e) { + assert(e instanceof TypeError); +} + +assert(Object.getOwnPropertyDescriptor(proxy, "configurable") == undefined) + +// (Inv-2) "A property cannot be reported as non-configurable, if it does not +// exist as an own property of the target object or if it exists as a +// configurable own property of the target object." +handler.getOwnPropertyDescriptor = function(target, name) { + return {value: 234, configurable: false, enumerable: true}; +}; + +try { + Object.getOwnPropertyDescriptor(proxy, "nonexistent"); + assert(false); +} catch(e) { assert(e instanceof TypeError); } try { - // 19.1.2.12.2.7 - Object.isFrozen(proxy) + Object.getOwnPropertyDescriptor(proxy, "configurable"); assert(false); -} catch (e) { +} catch(e) { + assert(e instanceof TypeError); +} + +assert(!Object.getOwnPropertyDescriptor(proxy, "nonconfigurable").configurable); + +// (Inv-3) "A property cannot be reported as non-existent, if it exists as an +// own property of the target object and the target object is not extensible." +Object.seal(target); +handler.getOwnPropertyDescriptor = function(target, name) { return undefined; }; + +try { + Object.getOwnPropertyDescriptor(proxy, "configurable"); + assert(false); +} catch(e) { assert(e instanceof TypeError); } try { - // 19.1.2.13.2.7 - Object.isSealed(proxy) + Object.getOwnPropertyDescriptor(proxy, "nonconfigurable"); assert(false); -} catch (e) { +} catch(e) { assert(e instanceof TypeError); } -*/ + +assert(undefined == Object.getOwnPropertyDescriptor(proxy, "nonexistent")); + +// (Inv-4) "A property cannot be reported as existent, if it does not exist as +// an own property of the target object and the target object is not +// extensible." +var existent_desc = {value: "yes"}; +handler.getOwnPropertyDescriptor = function() { return existent_desc; }; + +try { + Object.getOwnPropertyDescriptor(proxy, "nonexistent"); + assert(false); +} catch(e) { + assert(e instanceof TypeError); +} + +var new_obj = { value: "yes", + writable: false, + enumerable: false, + configurable: false }; +var conf_proxied = Object.getOwnPropertyDescriptor(proxy, "configurable"); + +assert(new_obj.value == conf_proxied.value); +assert(new_obj.configurable == conf_proxied.configurable); +assert(new_obj.writable == conf_proxied.writable); +assert(new_obj.enumerable == conf_proxied.enumerable); + +// Checking individual bailout points in the implementation: + +// Step 6: Trap is not callable. +handler.getOwnPropertyDescriptor = {}; + +try { + Object.getOwnPropertyDescriptor(proxy, "configurable"); + assert(false); +} catch(e) { + assert(e instanceof TypeError); +} + +// Step 8: Trap throws. +handler.getOwnPropertyDescriptor = function() { throw "unicorn"; }; + +try { + Object.getOwnPropertyDescriptor(proxy, "configurable"); + assert(false); +} catch(e) { + assert(e == "unicorn"); +} + +// Step 9: Trap result is neither undefined nor an object. +handler.getOwnPropertyDescriptor = function() { return 1; } + +try { + Object.getOwnPropertyDescriptor(proxy, "configurable"); + assert(false); +} catch(e) { + assert(e instanceof TypeError); +} + +// Step 11b: See (Inv-1) above. +// Step 11e: See (Inv-3) above. + +// Step 16: Incompatible PropertyDescriptor; a non-configurable property +// cannot be reported as configurable. (Inv-4) above checks more cases. +handler.getOwnPropertyDescriptor = function(target, name) { + return {value: 456, configurable: true, writable: true} +}; + +try { + Object.getOwnPropertyDescriptor(proxy, "nonconfigurable"); + assert(false); +} catch(e) { + assert(e instanceof TypeError); +} + +// Step 17: See (Inv-2) above.