OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --harmony-proxies | 5 // Flags: --harmony-proxies |
6 | 6 |
7 var target = { | 7 var target = { |
8 "target_one": 1 | 8 "target_one": 1 |
9 }; | 9 }; |
10 target.__proto__ = { | 10 target.__proto__ = { |
11 "target_two": 2 | 11 "target_two": 2 |
12 }; | 12 }; |
13 var handler = { | 13 var handler = { |
14 enumerate: function(target) { | 14 enumerate: function(target) { |
15 function* keys() { | 15 function* keys() { |
16 yield "foo"; | 16 yield "foo"; |
17 yield "bar"; | 17 yield "bar"; |
18 } | 18 } |
19 return keys(); | 19 return keys(); |
| 20 }, |
| 21 // For-in calls "has" on every iteration, so for TestForIn() below to |
| 22 // detect all results of the "enumerate" trap, "has" must return true. |
| 23 has: function(target, name) { |
| 24 return true; |
20 } | 25 } |
21 } | 26 } |
22 | 27 |
23 var proxy = new Proxy(target, handler); | 28 var proxy = new Proxy(target, handler); |
24 | 29 |
25 function TestForIn(receiver, expected) { | 30 function TestForIn(receiver, expected) { |
26 var result = []; | 31 var result = []; |
27 for (var k in receiver) { | 32 for (var k in receiver) { |
28 result.push(k); | 33 result.push(k); |
29 } | 34 } |
30 assertEquals(expected, result); | 35 assertEquals(expected, result); |
31 } | 36 } |
32 | 37 |
33 TestForIn(proxy, ["foo", "bar"]); | 38 TestForIn(proxy, ["foo", "bar"]); |
34 | 39 |
35 // Properly call traps on proxies on the prototype chain. | 40 // Properly call traps on proxies on the prototype chain. |
36 var receiver = { | 41 var receiver = { |
37 "receiver_one": 1 | 42 "receiver_one": 1 |
38 }; | 43 }; |
39 receiver.__proto__ = proxy; | 44 receiver.__proto__ = proxy; |
40 // TODO(jkummerow): Needs proper 'has' trap; implement that and enable this! | 45 TestForIn(receiver, ["receiver_one", "foo", "bar"]); |
41 // TestForIn(receiver, ["receiver_one", "foo", "bar"]); | |
42 | 46 |
43 // Fall through to default behavior when trap is undefined. | 47 // Fall through to default behavior when trap is undefined. |
44 handler.enumerate = undefined; | 48 handler.enumerate = undefined; |
45 TestForIn(proxy, ["target_one", "target_two"]); | 49 TestForIn(proxy, ["target_one", "target_two"]); |
46 delete handler.enumerate; | 50 delete handler.enumerate; |
47 TestForIn(proxy, ["target_one", "target_two"]); | 51 TestForIn(proxy, ["target_one", "target_two"]); |
48 | 52 |
49 // Non-string keys must be filtered. | 53 // Non-string keys must be filtered. |
50 function TestNonStringKey(key) { | 54 function TestNonStringKey(key) { |
51 handler.enumerate = function(target) { | 55 handler.enumerate = function(target) { |
52 function* keys() { yield key; } | 56 function* keys() { yield key; } |
53 return keys(); | 57 return keys(); |
54 } | 58 } |
55 assertThrows("for (var k in proxy) {}", TypeError); | 59 assertThrows("for (var k in proxy) {}", TypeError); |
56 } | 60 } |
57 | 61 |
58 TestNonStringKey(1); | 62 TestNonStringKey(1); |
59 TestNonStringKey(3.14); | 63 TestNonStringKey(3.14); |
60 TestNonStringKey(Symbol("foo")); | 64 TestNonStringKey(Symbol("foo")); |
61 TestNonStringKey({bad: "value"}); | 65 TestNonStringKey({bad: "value"}); |
62 TestNonStringKey(null); | 66 TestNonStringKey(null); |
63 TestNonStringKey(undefined); | 67 TestNonStringKey(undefined); |
64 TestNonStringKey(true); | 68 TestNonStringKey(true); |
OLD | NEW |