Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(247)

Side by Side Diff: test/mjsunit/harmony/proxies-set.js

Issue 1481103002: [proxies] Implement [[Set]]. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/objects.cc ('K') | « src/objects.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Flags: --harmony-proxies --harmony-reflect
6
7
8 // TODO(neis): There is so much more we could test, for instance:
9 // - Test with non-trivial receiver argument.
10 // - Test with function proxies as setters/getters.
11 // - Test with proxies in the prototype chain.
12 // - Check that non-object receiver is passed to proxy in prototype
13 // chain.
14
15
16 function sloppyDefaultSet(o, p, v) { return o[p] = v }
17 function sloppyReflectSet(o, p, v) { return Reflect.set(o, p, v) }
18 function strictDefaultSet(o, p, v) { "use strict"; return o[p] = v }
19 function strictReflectSet(o, p, v) { "use strict"; return Reflect.set(o, p, v) }
20
21 sloppyDefaultSet.shouldThrow = false;
22 sloppyReflectSet.shouldThrow = false;
23 strictDefaultSet.shouldThrow = true;
24 strictReflectSet.shouldThrow = false;
25
26 sloppyDefaultSet.returnsBool = false;
27 sloppyReflectSet.returnsBool = true;
28 strictDefaultSet.returnsBool = false;
29 strictReflectSet.returnsBool = true;
30
31
32 function assertTrueIf(flag, x) { if (flag) assertTrue(x) }
33 function assertFalseIf(flag, x) { if (flag) assertFalse(x) }
34 function assertSetFails(mySet, o, p, v) {
35 if (mySet.shouldThrow) {
36 assertThrows(() => mySet(o, p, v), TypeError);
37 } else {
38 assertFalseIf(mySet.returnsBool, mySet(o, p, v));
39 }
40 }
41
42
43 function dataDescriptor(x) {
44 return {value: x, writable: true, enumerable: true, configurable: true};
45 }
46
47
48 function toKey(x) {
49 if (typeof x === "symbol") return x;
50 return String(x);
51 }
52
53
54 var properties =
55 ["bla", "0", 1, Symbol(), {[Symbol.toPrimitive]() {return "a"}}];
56
57
58 function TestForwarding(handler, mySet) {
59 assertTrue(undefined == handler.set);
60 assertTrue(undefined == handler.getOwnPropertyDescriptor);
61 assertTrue(undefined == handler.defineProperty);
62
63 var target = {};
64 var proxy = new Proxy(target, handler);
65
66 // Property does not exist on target.
67 for (var p of properties) {
68 assertTrueIf(mySet.returnsBool, mySet(proxy, p, 42));
69 assertSame(42, target[p]);
70 }
71
72 // Property exists as writable data on target.
73 for (var p of properties) {
74 assertTrueIf(mySet.returnsBool, mySet(proxy, p, 0));
75 assertSame(0, target[p]);
76 }
77
78 // Property exists as non-writable data on target.
79 for (var p of properties) {
80 Object.defineProperty(target, p,
81 {value: 42, configurable: true, writable: false});
82 assertSetFails(mySet, proxy, p, 42);
83 assertSetFails(mySet, proxy, p, 0);
84 assertEquals(42, target[p]);
85 }
86 };
87
88
89 (function () {
90 // No trap.
91
92 var handler = {};
93
94 TestForwarding(handler, sloppyDefaultSet);
95 TestForwarding(handler, sloppyReflectSet);
96 TestForwarding(handler, strictDefaultSet);
97 TestForwarding(handler, strictReflectSet);
98 })();
99
100
101 (function () {
102 // "Undefined" trap.
103
104 var handler = { set: null };
105
106 TestForwarding(handler, sloppyDefaultSet);
107 TestForwarding(handler, sloppyReflectSet);
108 TestForwarding(handler, strictDefaultSet);
109 TestForwarding(handler, strictReflectSet);
110 })();
111
112
113 function TestForwarding2(mySet) {
114 // Check that setting on a proxy without "set" trap correctly triggers its
115 // "getOwnProperty" trap and its "defineProperty" trap.
116
117 var target = {};
118 var handler = {};
119 var observations = [];
120 var proxy = new Proxy(target, handler);
121
122 handler.getOwnPropertyDescriptor = function() {
123 observations.push(arguments);
124 return Reflect.getOwnPropertyDescriptor(...arguments);
125 }
126
127 handler.defineProperty = function() {
128 observations.push(arguments);
129 return Reflect.defineProperty(...arguments);
130 }
131
132 for (var p of properties) {
133 Reflect.set(proxy, p, 42);
134 assertEquals(2, observations.length)
135 assertArrayEquals([target, toKey(p)], observations[0]);
136 assertArrayEquals([target, toKey(p), dataDescriptor(42)], observations[1]);
137 observations = [];
138
139 Reflect.set(proxy, p, 42);
140 assertEquals(2, observations.length)
141 assertArrayEquals([target, toKey(p)], observations[0]);
142 assertArrayEquals([target, toKey(p), {value: 42}], observations[1]);
143 observations = [];
144 }
145 }
146
147
148 TestForwarding2(sloppyDefaultSet);
149 TestForwarding2(sloppyReflectSet);
150 TestForwarding2(strictDefaultSet);
151 TestForwarding2(strictReflectSet);
152
153
154 function TestInvalidTrap(proxy, mySet) {
155 for (var p of properties) {
156 assertThrows(() => mySet(proxy, p, 42), TypeError);
157 }
158 }
159
160
161 (function () {
162 var target = {};
163 var handler = { set: true };
164 var proxy = new Proxy(target, handler);
165
166 TestInvalidTrap(proxy, sloppyDefaultSet);
167 TestInvalidTrap(proxy, sloppyReflectSet);
168 TestInvalidTrap(proxy, strictDefaultSet);
169 TestInvalidTrap(proxy, strictReflectSet);
170 })();
171
172
173 function TestTrappingFalsish(mySet) {
174 var target = {};
175 var handler = { set() {return ""} };
176 var proxy = new Proxy(target, handler);
177
178 for (var p of properties) {
179 assertSetFails(mySet, proxy, p, 42);
180 }
181 }
182
183
184 TestTrappingFalsish(sloppyDefaultSet);
185 TestTrappingFalsish(sloppyReflectSet);
186 TestTrappingFalsish(strictDefaultSet);
187 TestTrappingFalsish(strictReflectSet);
188
189
190 function TestTrappingTrueish(mySet) {
191 var target = {};
192 var handler = { set() {return 42} };
193 var proxy = new Proxy(target, handler);
194
195 // Trap returns trueish and property does not exist in target.
196 for (var p of properties) {
197 assertTrueIf(mySet.returnsBool, mySet(proxy, p, 0));
198 }
199
200 // Trap returns trueish and target property is configurable or writable.
201 for (var p of properties) {
202 Object.defineProperty(target, p, {configurable: true, writable: true});
203 assertTrueIf(mySet.returnsBool, mySet(proxy, p, 0));
204 Object.defineProperty(target, p, {configurable: true, writable: false});
205 assertTrueIf(mySet.returnsBool, mySet(proxy, p, 0));
206 Object.defineProperty(target, p, {configurable: false, writable: true});
207 assertTrueIf(mySet.returnsBool, mySet(proxy, p, 0));
208 }
209 }
210
211
212 TestTrappingTrueish(sloppyDefaultSet);
213 TestTrappingTrueish(sloppyReflectSet);
214 TestTrappingTrueish(strictDefaultSet);
215 TestTrappingTrueish(strictReflectSet);
216
217
218 function TestTrappingTrueish2(mySet) {
219 var target = {};
220 var handler = { set() {return 42} };
221 var proxy = new Proxy(target, handler);
222
223 // Trap returns trueish but target property is frozen.
224 for (var p of properties) {
225 Object.defineProperty(target, p, {
226 configurable: false, writable: false, value: 0
227 });
228 assertThrows(() => mySet(proxy, p, 666), TypeError); // New value.
229 assertTrueIf(mySet.returnsBool, mySet(proxy, p, 0)); // Old value.
230 }
231 };
232
233
234 TestTrappingTrueish2(sloppyDefaultSet);
235 TestTrappingTrueish2(sloppyReflectSet);
236 TestTrappingTrueish2(strictDefaultSet);
237 TestTrappingTrueish2(strictReflectSet);
OLDNEW
« src/objects.cc ('K') | « src/objects.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698