| 1 |
// Copyright (C) 2020 Alexey Shvayka. All rights reserved. |
| 2 |
// This code is governed by the BSD license found in the LICENSE file. |
| 3 |
|
| 4 |
/*--- |
| 5 |
esid: sec-proxy-object-internal-methods-and-internal-slots-set-p-v-receiver |
| 6 |
description: > |
| 7 |
If "set" trap is missing, the call is properly forwarded to ordinary |
| 8 |
[[Set]] that invokes [[GetOwnProperty]] and [[DefineOwnProperty]] methods |
| 9 |
on receiver (that is a Proxy itself) every time it is called. |
| 10 |
info: | |
| 11 |
[[Set]] ( P, V, Receiver ) |
| 12 |
|
| 13 |
[...] |
| 14 |
6. Let trap be ? GetMethod(handler, "set"). |
| 15 |
7. If trap is undefined, then |
| 16 |
a. Return ? target.[[Set]](P, V, Receiver). |
| 17 |
|
| 18 |
OrdinarySetWithOwnDescriptor ( O, P, V, Receiver, ownDesc ) |
| 19 |
|
| 20 |
[...] |
| 21 |
3. If IsDataDescriptor(ownDesc) is true, then |
| 22 |
[...] |
| 23 |
c. Let existingDescriptor be ? Receiver.[[GetOwnProperty]](P). |
| 24 |
d. If existingDescriptor is not undefined, then |
| 25 |
[...] |
| 26 |
iii. Let valueDesc be the PropertyDescriptor { [[Value]]: V }. |
| 27 |
iv. Return ? Receiver.[[DefineOwnProperty]](P, valueDesc). |
| 28 |
e. Else, |
| 29 |
i. Assert: Receiver does not currently have a property P. |
| 30 |
ii. Return ? CreateDataProperty(Receiver, P, V). |
| 31 |
[...] |
| 32 |
|
| 33 |
[[DefineOwnProperty]] ( P, Desc ) |
| 34 |
|
| 35 |
[...] |
| 36 |
9. Let booleanTrapResult be ! ToBoolean(? Call(trap, handler, « target, P, descObj »)). |
| 37 |
[...] |
| 38 |
17. Return true. |
| 39 |
features: [Proxy, Reflect] |
| 40 |
includes: [compareArray.js] |
| 41 |
---*/ |
| 42 |
|
| 43 |
var getOwnPropertyKeys = []; |
| 44 |
var definePropertyKeys = []; |
| 45 |
|
| 46 |
var p = new Proxy({ |
| 47 |
foo: 1, |
| 48 |
}, { |
| 49 |
getOwnPropertyDescriptor: function(target, key) { |
| 50 |
getOwnPropertyKeys.push(key); |
| 51 |
return Reflect.getOwnPropertyDescriptor(target, key); |
| 52 |
}, |
| 53 |
defineProperty: function(target, key, desc) { |
| 54 |
definePropertyKeys.push(key); |
| 55 |
return Reflect.defineProperty(target, key, desc); |
| 56 |
}, |
| 57 |
}); |
| 58 |
|
| 59 |
p["foo"] = 2; |
| 60 |
p.foo = 2; |
| 61 |
p.foo = 2; |
| 62 |
|
| 63 |
assert.compareArray(getOwnPropertyKeys, ["foo", "foo", "foo"], |
| 64 |
"getOwnPropertyDescriptor: key present on [[ProxyTarget]]"); |
| 65 |
assert.compareArray(definePropertyKeys, ["foo", "foo", "foo"], |
| 66 |
"defineProperty: key present on [[ProxyTarget]]"); |
| 67 |
|
| 68 |
getOwnPropertyKeys = []; |
| 69 |
definePropertyKeys = []; |
| 70 |
|
| 71 |
p.bar = 3; |
| 72 |
p["bar"] = 3; |
| 73 |
p.bar = 3; |
| 74 |
|
| 75 |
assert.compareArray(getOwnPropertyKeys, ["bar", "bar", "bar"], |
| 76 |
"getOwnPropertyDescriptor: key absent on [[ProxyTarget]]"); |
| 77 |
assert.compareArray(definePropertyKeys, ["bar", "bar", "bar"], |
| 78 |
"defineProperty: key absent on [[ProxyTarget]]"); |
| 79 |
|
| 80 |
reportCompare(0, 0); |