OLD | NEW |
| (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 | |
6 | |
7 var target = { | |
8 "target_one": 1 | |
9 }; | |
10 target.__proto__ = { | |
11 "target_two": 2 | |
12 }; | |
13 var handler = { | |
14 enumerate: function(target) { | |
15 function* keys() { | |
16 yield "foo"; | |
17 yield "bar"; | |
18 } | |
19 return keys(); | |
20 } | |
21 } | |
22 | |
23 var proxy = new Proxy(target, handler); | |
24 | |
25 function TestForIn(receiver, expected) { | |
26 var result = []; | |
27 for (var k in receiver) { | |
28 result.push(k); | |
29 } | |
30 assertEquals(expected, result); | |
31 } | |
32 | |
33 TestForIn(proxy, ["foo", "bar"]); | |
34 | |
35 // Properly call traps on proxies on the prototype chain. | |
36 var receiver = { | |
37 "receiver_one": 1 | |
38 }; | |
39 receiver.__proto__ = proxy; | |
40 // TODO(jkummerow): Needs proper 'has' trap; implement that and enable this! | |
41 // TestForIn(receiver, ["receiver_one", "foo", "bar"]); | |
42 | |
43 // Fall through to default behavior when trap is undefined. | |
44 handler.enumerate = undefined; | |
45 TestForIn(proxy, ["target_one", "target_two"]); | |
46 delete handler.enumerate; | |
47 TestForIn(proxy, ["target_one", "target_two"]); | |
48 | |
49 // Non-string keys must be filtered. | |
50 function TestNonStringKey(key) { | |
51 handler.enumerate = function(target) { | |
52 function* keys() { yield key; } | |
53 return keys(); | |
54 } | |
55 assertThrows("for (var k in proxy) {}", TypeError); | |
56 } | |
57 | |
58 TestNonStringKey(1); | |
59 TestNonStringKey(3.14); | |
60 TestNonStringKey(Symbol("foo")); | |
61 TestNonStringKey({bad: "value"}); | |
62 TestNonStringKey(null); | |
63 TestNonStringKey(undefined); | |
64 TestNonStringKey(true); | |
OLD | NEW |