OLD | NEW |
| (Empty) |
1 // Copyright 2011 the V8 project authors. All rights reserved. | |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | |
28 // Flags: --harmony-proxies | |
29 | |
30 // Helper. | |
31 | |
32 function TestWithProxies(test, x, y, z) { | |
33 test(function(h){ return new Proxy({}, h) }, x, y, z) | |
34 } | |
35 | |
36 | |
37 // Iterate over a proxy. | |
38 | |
39 function TestForIn(properties, handler) { | |
40 TestWithProxies(TestForIn2, properties, handler) | |
41 } | |
42 | |
43 function TestForIn2(create, properties, handler) { | |
44 var p = create(handler) | |
45 var found = [] | |
46 for (var x in p) found.push(x) | |
47 assertArrayEquals(properties, found) | |
48 } | |
49 | |
50 TestForIn(["0", "a"], { | |
51 ownKeys() { return ["0", "a"] }, | |
52 has(target, property) { return true }, | |
53 getOwnPropertyDescriptor() { return { enumerable: true, configurable: true }} | |
54 }) | |
55 | |
56 TestForIn(["null", "a"], { | |
57 ownKeys() { return this.enumerate() }, | |
58 enumerate() { return ["null", "a"] }, | |
59 has(target, property) { return true }, | |
60 getOwnPropertyDescriptor() { return { enumerable: true, configurable: true }} | |
61 }) | |
62 | |
63 | |
64 // Iterate over an object with a proxy prototype. | |
65 | |
66 function TestForInDerived(properties, handler) { | |
67 TestWithProxies(TestForInDerived2, properties, handler) | |
68 } | |
69 | |
70 function TestForInDerived2(create, properties, handler) { | |
71 var p = create(handler) | |
72 var o = Object.create(p) | |
73 o.z = 0 | |
74 var found = [] | |
75 for (var x in o) found.push(x) | |
76 assertArrayEquals(["z"].concat(properties), found) | |
77 | |
78 var oo = Object.create(o) | |
79 oo.y = 0 | |
80 var found = [] | |
81 for (var x in oo) found.push(x) | |
82 assertArrayEquals(["y", "z"].concat(properties), found) | |
83 } | |
84 | |
85 TestForInDerived(["0", "a"], { | |
86 ownKeys: function() { return ["0", "a"] }, | |
87 has: function(t, k) { return k == "0" || k == "a" }, | |
88 getOwnPropertyDescriptor() { return { enumerable: true, configurable: true }} | |
89 }) | |
90 | |
91 TestForInDerived(["null", "a"], { | |
92 ownKeys: function() { return this.enumerate() }, | |
93 enumerate: function() { return ["null", "a"] }, | |
94 has: function(t, k) { return k == "null" || k == "a" }, | |
95 getOwnPropertyDescriptor() { return { enumerable: true, configurable: true }} | |
96 }) | |
97 | |
98 | |
99 | |
100 // Throw exception in ownKeys trap. | |
101 | |
102 function TestForInThrow(handler) { | |
103 TestWithProxies(TestForInThrow2, handler) | |
104 } | |
105 | |
106 function TestForInThrow2(create, handler) { | |
107 var p = create(handler) | |
108 var o = Object.create(p) | |
109 assertThrowsEquals(function(){ for (var x in p) {} }, "myexn") | |
110 assertThrowsEquals(function(){ for (var x in o) {} }, "myexn") | |
111 } | |
112 | |
113 TestForInThrow({ | |
114 ownKeys: function() { throw "myexn" } | |
115 }) | |
116 | |
117 TestForInThrow({ | |
118 ownKeys: function() { return this.enumerate() }, | |
119 enumerate: function() { throw "myexn" } | |
120 }) | |
121 | |
122 TestForInThrow(new Proxy({}, { | |
123 get: function(pr, pk) { | |
124 return function() { throw "myexn" } | |
125 } | |
126 })); | |
127 | |
128 (function() { | |
129 var p = new Proxy({}, {ownKeys:function() { return ["0"]; }}); | |
130 var o = [0]; | |
131 o.__proto__ = p; | |
132 var keys = []; | |
133 for (var k in o) { keys.push(k); }; | |
134 assertEquals(["0"], keys); | |
135 })(); | |
136 | |
137 (function () { | |
138 var p = new Proxy({}, {ownKeys: function() { return ["1", Symbol(), "2"] }}); | |
139 assertEquals(["1","2"], Object.getOwnPropertyNames(p)); | |
140 })(); | |
OLD | NEW |