OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 22 matching lines...) Expand all Loading... |
33 function TestWithProxies(test, x, y, z) { | 33 function TestWithProxies(test, x, y, z) { |
34 test(function(h){ return new Proxy({}, h) }, x, y, z) | 34 test(function(h){ return new Proxy({}, h) }, x, y, z) |
35 test(function(h) { | 35 test(function(h) { |
36 return Proxy.createFunction(h, function() {}) | 36 return Proxy.createFunction(h, function() {}) |
37 }, x, y, z) | 37 }, x, y, z) |
38 } | 38 } |
39 | 39 |
40 | 40 |
41 // Iterate over a proxy. | 41 // Iterate over a proxy. |
42 | 42 |
| 43 Array.prototype.values = function() { return this[Symbol.iterator]() } |
| 44 |
43 function TestForIn(properties, handler) { | 45 function TestForIn(properties, handler) { |
44 TestWithProxies(TestForIn2, properties, handler) | 46 TestWithProxies(TestForIn2, properties, handler) |
45 } | 47 } |
46 | 48 |
47 function TestForIn2(create, properties, handler) { | 49 function TestForIn2(create, properties, handler) { |
48 var p = create(handler) | 50 var p = create(handler) |
49 var found = [] | 51 var found = [] |
50 for (var x in p) found.push(x) | 52 for (var x in p) found.push(x) |
51 assertArrayEquals(properties, found) | 53 assertArrayEquals(properties, found) |
52 } | 54 } |
53 | 55 |
54 TestForIn(["0", "a"], { | 56 TestForIn(["0", "a"], { |
55 enumerate: function() { return [0, "a"] } | 57 enumerate: function() { return ["0", "a"].values() } |
56 }) | 58 }) |
57 | 59 |
58 TestForIn(["null", "a"], { | 60 TestForIn(["null", "a"], { |
59 enumerate: function() { return this.enumerate2() }, | 61 enumerate: function() { return this.enumerate2() }, |
60 enumerate2: function() { return [null, "a"] } | 62 enumerate2: function() { return ["null", "a"].values() } |
61 }) | |
62 | |
63 TestForIn(["b", "d"], { | |
64 getPropertyNames: function() { return ["a", "b", "c", "d", "e"] }, | |
65 getPropertyDescriptor: function(k) { | |
66 switch (k) { | |
67 case "a": return {enumerable: false, value: "3", configurable: true}; | |
68 case "b": return {enumerable: true, get get() {}, configurable: true}; | |
69 case "c": return {value: 4, configurable: true}; | |
70 case "d": return {get enumerable() { return true }, configurable: true}; | |
71 default: return undefined; | |
72 } | |
73 } | |
74 }) | 63 }) |
75 | 64 |
76 TestForIn(["b", "a", "0", "c"], new Proxy({}, { | 65 TestForIn(["b", "a", "0", "c"], new Proxy({}, { |
77 get: function(pr, pk) { | 66 get: function(pr, pk) { |
78 return function() { return ["b", "a", 0, "c"] } | 67 return function() { return ["b", "a", "0", "c"].values() } |
79 } | 68 } |
80 })) | 69 })) |
81 | 70 |
82 | 71 |
83 | 72 |
84 // Iterate over an object with a proxy prototype. | 73 // Iterate over an object with a proxy prototype. |
85 | 74 |
86 function TestForInDerived(properties, handler) { | 75 function TestForInDerived(properties, handler) { |
87 TestWithProxies(TestForInDerived2, properties, handler) | 76 TestWithProxies(TestForInDerived2, properties, handler) |
88 } | 77 } |
89 | 78 |
90 function TestForInDerived2(create, properties, handler) { | 79 function TestForInDerived2(create, properties, handler) { |
91 var p = create(handler) | 80 var p = create(handler) |
92 var o = Object.create(p) | 81 var o = Object.create(p) |
93 o.z = 0 | 82 o.z = 0 |
94 var found = [] | 83 var found = [] |
95 for (var x in o) found.push(x) | 84 for (var x in o) found.push(x) |
96 assertArrayEquals(["z"].concat(properties), found) | 85 assertArrayEquals(["z"].concat(properties), found) |
97 | 86 |
98 var oo = Object.create(o) | 87 var oo = Object.create(o) |
99 oo.y = 0 | 88 oo.y = 0 |
100 var found = [] | 89 var found = [] |
101 for (var x in oo) found.push(x) | 90 for (var x in oo) found.push(x) |
102 assertArrayEquals(["y", "z"].concat(properties), found) | 91 assertArrayEquals(["y", "z"].concat(properties), found) |
103 } | 92 } |
104 | 93 |
105 TestForInDerived(["0", "a"], { | 94 TestForInDerived(["0", "a"], { |
106 enumerate: function() { return [0, "a"] }, | 95 enumerate: function() { return ["0", "a"].values() }, |
107 getPropertyDescriptor: function(k) { | 96 has: function(t, k) { return k == "0" || k == "a" } |
108 return k == "0" || k == "a" ? {configurable: true} : undefined | |
109 } | |
110 }) | 97 }) |
111 | 98 |
112 TestForInDerived(["null", "a"], { | 99 TestForInDerived(["null", "a"], { |
113 enumerate: function() { return this.enumerate2() }, | 100 enumerate: function() { return this.enumerate2() }, |
114 enumerate2: function() { return [null, "a"] }, | 101 enumerate2: function() { return ["null", "a"].values() }, |
115 getPropertyDescriptor: function(k) { | 102 has: function(t, k) { return k == "null" || k == "a" } |
116 return k == "null" || k == "a" ? {configurable: true} : undefined | |
117 } | |
118 }) | |
119 | |
120 TestForInDerived(["b", "d"], { | |
121 getPropertyNames: function() { return ["a", "b", "c", "d", "e"] }, | |
122 getPropertyDescriptor: function(k) { | |
123 switch (k) { | |
124 case "a": return {enumerable: false, value: "3", configurable: true}; | |
125 case "b": return {enumerable: true, get get() {}, configurable: true}; | |
126 case "c": return {value: 4, configurable: true}; | |
127 case "d": return {get enumerable() { return true }, configurable: true}; | |
128 default: return undefined; | |
129 } | |
130 } | |
131 }) | 103 }) |
132 | 104 |
133 | 105 |
134 | 106 |
135 // Throw exception in enumerate trap. | 107 // Throw exception in enumerate trap. |
136 | 108 |
137 function TestForInThrow(handler) { | 109 function TestForInThrow(handler) { |
138 TestWithProxies(TestForInThrow2, handler) | 110 TestWithProxies(TestForInThrow2, handler) |
139 } | 111 } |
140 | 112 |
141 function TestForInThrow2(create, handler) { | 113 function TestForInThrow2(create, handler) { |
142 var p = create(handler) | 114 var p = create(handler) |
143 var o = Object.create(p) | 115 var o = Object.create(p) |
144 assertThrows(function(){ for (var x in p) {} }, "myexn") | 116 assertThrows(function(){ for (var x in p) {} }, "myexn") |
145 assertThrows(function(){ for (var x in o) {} }, "myexn") | 117 assertThrows(function(){ for (var x in o) {} }, "myexn") |
146 } | 118 } |
147 | 119 |
148 TestForInThrow({ | 120 TestForInThrow({ |
149 enumerate: function() { throw "myexn" } | 121 enumerate: function() { throw "myexn" } |
150 }) | 122 }) |
151 | 123 |
152 TestForInThrow({ | 124 TestForInThrow({ |
153 enumerate: function() { return this.enumerate2() }, | 125 enumerate: function() { return this.enumerate2() }, |
154 enumerate2: function() { throw "myexn" } | 126 enumerate2: function() { throw "myexn" } |
155 }) | 127 }) |
156 | 128 |
157 TestForInThrow({ | |
158 getPropertyNames: function() { throw "myexn" } | |
159 }) | |
160 | |
161 TestForInThrow({ | |
162 getPropertyNames: function() { return ["a"] }, | |
163 getPropertyDescriptor: function() { throw "myexn" } | |
164 }) | |
165 | |
166 TestForInThrow(new Proxy({}, { | 129 TestForInThrow(new Proxy({}, { |
167 get: function(pr, pk) { | 130 get: function(pr, pk) { |
168 return function() { throw "myexn" } | 131 return function() { throw "myexn" } |
169 } | 132 } |
170 })); | 133 })); |
171 | 134 |
172 (function() { | 135 (function() { |
173 var p = new Proxy({}, {enumerate:function() { return [0]; }}); | 136 var p = new Proxy({}, {enumerate:function() { return ["0"].values(); }}); |
174 var o = [0]; | 137 var o = [0]; |
175 o.__proto__ = p; | 138 o.__proto__ = p; |
176 var keys = []; | 139 var keys = []; |
177 for (var k in o) { keys.push(k); }; | 140 for (var k in o) { keys.push(k); }; |
178 assertEquals(["0"], keys); | 141 assertEquals(["0"], keys); |
179 })(); | 142 })(); |
180 | 143 |
181 (function () { | 144 (function () { |
182 var p = new Proxy({}, {getOwnPropertyNames: | 145 var p = new Proxy({}, {getOwnPropertyNames: |
183 function() { return [1, Symbol(), 2] }}); | 146 function() { return [1, Symbol(), 2] }}); |
184 assertEquals(["1","2"], Object.getOwnPropertyNames(p)); | 147 assertEquals(["1","2"], Object.getOwnPropertyNames(p)); |
185 })(); | 148 })(); |
OLD | NEW |