OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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-unscopables |
| 6 // Flags: --harmony-proxies |
| 7 |
| 8 |
| 9 // TODO(arv): Once proxies can intercept symbols, add more tests. |
| 10 |
| 11 |
| 12 function TestBasics() { |
| 13 var log = []; |
| 14 |
| 15 var proxy = Proxy.create({ |
| 16 getPropertyDescriptor: function(key) { |
| 17 log.push(key); |
| 18 if (key === 'x') { |
| 19 return { |
| 20 value: 1, |
| 21 configurable: true |
| 22 }; |
| 23 } |
| 24 return undefined; |
| 25 } |
| 26 }); |
| 27 |
| 28 var x = 'local'; |
| 29 |
| 30 with (proxy) { |
| 31 assertEquals(1, x); |
| 32 } |
| 33 |
| 34 // One 'x' for HasBinding and two for GetBindingValue |
| 35 assertEquals(['assertEquals', 'x', 'x', 'x'], log); |
| 36 } |
| 37 TestBasics(); |
| 38 |
| 39 |
| 40 function TestInconsistent() { |
| 41 var log = []; |
| 42 var calls = 0; |
| 43 |
| 44 var proxy = Proxy.create({ |
| 45 getPropertyDescriptor: function(key) { |
| 46 log.push(key); |
| 47 if (key === 'x' && calls < 2) { |
| 48 calls++; |
| 49 return { |
| 50 value: 1, |
| 51 configurable: true |
| 52 }; |
| 53 } |
| 54 return undefined; |
| 55 } |
| 56 }); |
| 57 |
| 58 var x = 'local'; |
| 59 |
| 60 with (proxy) { |
| 61 assertEquals(void 0, x); |
| 62 } |
| 63 |
| 64 // One 'x' for HasBinding and two for GetBindingValue |
| 65 assertEquals(['assertEquals', 'x', 'x', 'x'], log); |
| 66 } |
| 67 TestInconsistent(); |
| 68 |
| 69 |
| 70 function TestInconsistent2() { |
| 71 var log = []; |
| 72 var calls = 0; |
| 73 |
| 74 var proxy = Proxy.create({ |
| 75 getPropertyDescriptor: function(key) { |
| 76 log.push(key); |
| 77 if (key === 'x' && calls < 1) { |
| 78 calls++; |
| 79 return { |
| 80 value: 1, |
| 81 configurable: true |
| 82 }; |
| 83 } |
| 84 return undefined; |
| 85 } |
| 86 }); |
| 87 |
| 88 var x = 'local'; |
| 89 |
| 90 with (proxy) { |
| 91 assertEquals(void 0, x); |
| 92 } |
| 93 |
| 94 // One 'x' for HasBinding and one for GetBindingValue, the second one from |
| 95 // previous test is not called since the proxy reported that there is no 'x' |
| 96 // property when we did GetBindingValue. |
| 97 assertEquals(['assertEquals', 'x', 'x'], log); |
| 98 } |
| 99 TestInconsistent2(); |
| 100 |
| 101 |
| 102 function TestUseProxyAsUnscopables() { |
| 103 var x = 1; |
| 104 var object = { |
| 105 x: 2 |
| 106 }; |
| 107 var calls = 0; |
| 108 var proxy = Proxy.create({ |
| 109 has: function(key) { |
| 110 calls++; |
| 111 assertEquals('x', key); |
| 112 return calls === 3; |
| 113 }, |
| 114 getPropertyDescriptor: function(key) { |
| 115 assertUnreachable(); |
| 116 } |
| 117 }); |
| 118 |
| 119 object[Symbol.unscopables] = proxy; |
| 120 |
| 121 with (object) { |
| 122 assertEquals(2, x); |
| 123 assertEquals(1, x); |
| 124 } |
| 125 |
| 126 // HasBinding, GetBindingValue, HasBinding |
| 127 assertEquals(3, calls); |
| 128 } |
| 129 TestUseProxyAsUnscopables(); |
| 130 |
| 131 |
| 132 function TestThrowInHasOwnUnscopables() { |
| 133 var x = 1; |
| 134 var object = { |
| 135 x: 2 |
| 136 }; |
| 137 |
| 138 function CustomError() {} |
| 139 |
| 140 var calls = 0; |
| 141 var proxy = Proxy.create({ |
| 142 has: function(key) { |
| 143 if (calls++ === 0) { |
| 144 throw new CustomError(); |
| 145 } |
| 146 assertUnreachable(); |
| 147 }, |
| 148 getPropertyDescriptor: function(key) { |
| 149 assertUnreachable(); |
| 150 } |
| 151 }); |
| 152 |
| 153 object[Symbol.unscopables] = proxy; |
| 154 |
| 155 assertThrows(function() { |
| 156 with (object) { |
| 157 x; |
| 158 } |
| 159 }, CustomError); |
| 160 } |
| 161 TestThrowInHasOwnUnscopables(); |
| 162 |
| 163 |
| 164 function TestThrowInHasOwnUnscopables2() { |
| 165 var x = 1; |
| 166 var object = { |
| 167 x: 2 |
| 168 }; |
| 169 |
| 170 function CustomError() {} |
| 171 |
| 172 var calls = 0; |
| 173 var proxy = Proxy.create({ |
| 174 has: function(key) { |
| 175 if (calls++ === 1) { |
| 176 throw new CustomError(); |
| 177 } |
| 178 return false; |
| 179 }, |
| 180 getPropertyDescriptor: function(key) { |
| 181 assertUnreachable(); |
| 182 } |
| 183 }); |
| 184 |
| 185 object[Symbol.unscopables] = proxy; |
| 186 |
| 187 assertThrows(function() { |
| 188 with (object) { |
| 189 x; |
| 190 } |
| 191 }, CustomError); |
| 192 } |
| 193 TestThrowInHasOwnUnscopables2(); |
OLD | NEW |