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