Implement Proxy object [[Get]] internal method (#3604)

The algorithm is based on ECMA-262 v6, 9.5.8

JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi aszilagy@inf.u-szeged.hu
This commit is contained in:
Szilagyi Adam
2020-03-11 15:34:46 +01:00
committed by GitHub
parent 38862c39a6
commit 64fd37d1db
5 changed files with 188 additions and 26 deletions
+95 -2
View File
@@ -414,8 +414,101 @@ ecma_proxy_object_get (ecma_object_t *obj_p, /**< proxy object */
ecma_value_t receiver) /**< receiver to invoke getter function */
{
JERRY_ASSERT (ECMA_OBJECT_IS_PROXY (obj_p));
JERRY_UNUSED_3 (obj_p, prop_name_p, receiver);
return ecma_raise_type_error (ECMA_ERR_MSG ("UNIMPLEMENTED: Proxy.[[Get]]"));
ecma_proxy_object_t *proxy_obj_p = (ecma_proxy_object_t *) obj_p;
/* 2. */
ecma_value_t handler = proxy_obj_p->handler;
/* 3. */
if (ecma_is_value_null (handler))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Handler can not be null."));
}
/* 4. */
JERRY_ASSERT (ecma_is_value_object (handler));
/* 5. */
ecma_value_t target = proxy_obj_p->target;
/* 6. */
ecma_value_t trap = ecma_op_get_method_by_magic_id (handler, LIT_MAGIC_STRING_GET);
/* 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_with_receiver (target_obj_p, prop_name_p, receiver);
}
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, receiver };
/* 9. */
ecma_value_t trap_result = ecma_op_function_call (func_obj_p, handler, args, 3);
ecma_deref_object (func_obj_p);
/* 10. */
if (ECMA_IS_VALUE_ERROR (trap_result))
{
return trap_result;
}
/* 11. */
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);
/* 12. */
if (ECMA_IS_VALUE_ERROR (status))
{
return status;
}
/* 13. */
if (ecma_is_value_true (status))
{
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
if ((target_desc.flags & ECMA_PROP_IS_VALUE_DEFINED)
&& !(target_desc.flags & ECMA_PROP_IS_CONFIGURABLE)
&& !(target_desc.flags & ECMA_PROP_IS_WRITABLE)
&& !ecma_op_same_value (trap_result, target_desc.value))
{
ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("given property is a read-only and non-configurable"
" data property on the proxy target"));
}
else if (!(target_desc.flags & ECMA_PROP_IS_CONFIGURABLE)
&& (target_desc.flags & (ECMA_PROP_IS_GET_DEFINED | ECMA_PROP_IS_SET_DEFINED))
&& target_desc.get_p == NULL
&& !ecma_is_value_undefined (trap_result))
{
ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("given property is a non-configurable property and"
" does not have a getter function"));
}
ecma_free_property_descriptor (&target_desc);
if (ECMA_IS_VALUE_ERROR (ret_value))
{
ecma_free_value (trap_result);
return ret_value;
}
}
/* 14. */
return trap_result;
} /* ecma_proxy_object_get */
/**
-7
View File
@@ -29,13 +29,6 @@ try {
assert(e instanceof TypeError);
}
try {
proxy.a;
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
try {
new Proxy(undefined, undefined);
assert(false);
+92 -4
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 = {};
var handler = { get (target) {
@@ -26,7 +28,7 @@ try {
proxy.a
assert(false);
} catch (e) {
assert(e instanceof TypeError);
assert(e === 42);
}
try {
@@ -34,7 +36,7 @@ try {
proxy[2];
assert(false);
} catch (e) {
assert(e instanceof TypeError);
assert(e === 42);
}
try {
@@ -42,5 +44,91 @@ try {
proxy + "foo";
assert(false);
} catch (e) {
assert(e instanceof TypeError);
assert(e === 42);
}
// test basic funcionality
var target = {
target_one: 1,
prop: "value"
};
var handler = {handler: 1};
var proxy = new Proxy(target, handler);
assert(proxy.prop === "value");
assert(proxy.nothing === undefined);
assert(proxy.handler === undefined);
handler.get = function () {return "value 2"};
assert(proxy.prop === "value 2");
assert(proxy.nothing === "value 2");
assert(proxy.handler === "value 2");
var handler2 = new Proxy({get: function() {return "value 3"}}, {});
var proxy2 = new Proxy(target, handler2);
assert(proxy2.prop === "value 3");
assert(proxy2.nothing === "value 3");
assert(proxy2.handler === "value 3");
// test when get throws an error
var handler = new Proxy({}, {get: function() {throw 42;}});
var proxy = new Proxy ({}, handler);
try {
proxy.prop;
assert(false);
} catch (e) {
assert(e === 42);
}
// test when trap is undefined
var handler = new Proxy({}, {get: function() {return undefined}});
var target = {prop: "value"};
var proxy = new Proxy(target, handler);
assert(proxy.prop === "value");
assert(proxy.prop2 === undefined);
// test when invariants gets violated
var target = {};
var handler = {get: function(r, p){if (p != "key4") return "value"}}
var proxy = new Proxy(target, handler);
assert(proxy.key === "value");
assert(proxy.key2 === "value");
assert(proxy.key3 === "value");
assert(proxy.key4 === undefined);
Object.defineProperty(target, "key", {
configurable: false,
writable: false,
value: "different value"
});
try {
proxy.key;
assert(false);
} catch (e) {
assert(e instanceof TypeError)
}
Object.defineProperty(target, "key2", {
configurable: false,
get: function() {return "different value"}
});
assert(proxy.key2 === "value");
Object.defineProperty(target, "key3", {
configurable: false,
set: function() {}
});
try {
proxy.key3;
assert(false);
} catch (e) {
assert(e instanceof TypeError)
}
+1 -6
View File
@@ -23,12 +23,7 @@ var revocable = Proxy.revocable(target, handler);
var proxy = revocable.proxy;
try {
proxy.a; // FIXME: this should return 5 when proxy.[[Get]] has been implemted
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
assert(proxy.a === 5);
revocable.revoke();
-7
View File
@@ -29,10 +29,3 @@ try {
assert(e instanceof TypeError);
}
try {
// 22.1.3.6.11.b
Array.prototype.fill.call(proxy, 'foo', 0, 5);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}