| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Flags: --harmony-proxies | 5 // Flags: --harmony-proxies |
| 6 | 6 |
| 7 | 7 |
| 8 // TODO(arv): Once proxies can intercept symbols, add more tests. | |
| 9 | |
| 10 | |
| 11 function TestBasics() { | 8 function TestBasics() { |
| 12 var log = []; | 9 var log = []; |
| 13 | 10 |
| 14 var proxy = new Proxy({}, { | 11 var proxy = new Proxy({}, { |
| 15 getPropertyDescriptor: function(key) { | 12 get: function(target, key) { |
| 16 log.push(key); | 13 log.push("get " + String(key)); |
| 17 if (key === 'x') { | 14 if (key === 'x') return 1; |
| 18 return { | 15 }, |
| 19 value: 1, | 16 has: function(target, key) { |
| 20 configurable: true | 17 log.push("has " + String(key)); |
| 21 }; | 18 if (key === 'x') return true; |
| 22 } | 19 return false; |
| 23 return undefined; | |
| 24 } | 20 } |
| 25 }); | 21 }); |
| 26 | 22 |
| 27 var x = 'local'; | 23 var x = 'local'; |
| 28 | 24 |
| 29 with (proxy) { | 25 with (proxy) { |
| 30 assertEquals(1, x); | 26 assertEquals(1, x); |
| 31 } | 27 } |
| 32 | 28 |
| 33 // One 'x' for HasBinding and one for GetBindingValue | 29 assertEquals(['has assertEquals', 'has x', 'get Symbol(Symbol.unscopables)', |
| 34 assertEquals(['assertEquals', 'x', 'x'], log); | 30 'get x'], log); |
| 35 } | 31 } |
| 36 TestBasics(); | 32 TestBasics(); |
| 37 | 33 |
| 38 | 34 |
| 39 function TestInconsistent() { | 35 function TestInconsistent() { |
| 40 var log = []; | 36 var log = []; |
| 41 var calls = 0; | |
| 42 | 37 |
| 43 var proxy = new Proxy({}, { | 38 var proxy = new Proxy({}, { |
| 44 getPropertyDescriptor: function(key) { | 39 get: function(target, key) { |
| 45 log.push(key); | 40 log.push("get " + String(key)); |
| 46 if (key === 'x' && calls < 1) { | |
| 47 calls++; | |
| 48 return { | |
| 49 value: 1, | |
| 50 configurable: true | |
| 51 }; | |
| 52 } | |
| 53 return undefined; | 41 return undefined; |
| 42 }, |
| 43 has: function(target, key) { |
| 44 log.push("has " + String(key)); |
| 45 if (key === 'x') return true; |
| 46 return false; |
| 54 } | 47 } |
| 55 }); | 48 }); |
| 56 | 49 |
| 57 var x = 'local'; | 50 var x = 'local'; |
| 58 | 51 |
| 59 with (proxy) { | 52 with (proxy) { |
| 60 assertEquals(void 0, x); | 53 assertEquals(void 0, x); |
| 61 } | 54 } |
| 62 | 55 |
| 63 // One 'x' for HasBinding and one for GetBindingValue | 56 assertEquals(['has assertEquals', 'has x', 'get Symbol(Symbol.unscopables)', |
| 64 assertEquals(['assertEquals', 'x', 'x'], log); | 57 'get x'], log); |
| 65 } | 58 } |
| 66 TestInconsistent(); | 59 TestInconsistent(); |
| 67 | 60 |
| 68 | 61 |
| 69 function TestUseProxyAsUnscopables() { | 62 function TestUseProxyAsUnscopables() { |
| 70 var x = 1; | 63 var x = 1; |
| 71 var object = { | 64 var object = { |
| 72 x: 2 | 65 x: 2 |
| 73 }; | 66 }; |
| 74 var calls = 0; | 67 var calls = 0; |
| 75 var proxy = new Proxy({}, { | 68 var proxy = new Proxy({}, { |
| 76 has: function(key) { | 69 has: function() { |
| 77 assertUnreachable(); | 70 assertUnreachable(); |
| 78 }, | 71 }, |
| 79 getPropertyDescriptor: function(key) { | 72 get: function(target, key) { |
| 73 assertEquals('x', key); |
| 80 calls++; | 74 calls++; |
| 81 assertEquals('x', key); | 75 return calls === 2 ? true : undefined; |
| 82 return { | |
| 83 value: calls === 2 ? true : undefined, | |
| 84 configurable: true, | |
| 85 enumerable: true, | |
| 86 writable: true, | |
| 87 }; | |
| 88 } | 76 } |
| 89 }); | 77 }); |
| 90 | 78 |
| 91 object[Symbol.unscopables] = proxy; | 79 object[Symbol.unscopables] = proxy; |
| 92 | 80 |
| 93 with (object) { | 81 with (object) { |
| 94 assertEquals(2, x); | 82 assertEquals(2, x); |
| 95 assertEquals(1, x); | 83 assertEquals(1, x); |
| 96 } | 84 } |
| 97 | 85 |
| 98 // HasBinding, HasBinding | 86 // HasBinding, HasBinding |
| 99 assertEquals(2, calls); | 87 assertEquals(2, calls); |
| 100 } | 88 } |
| 101 TestUseProxyAsUnscopables(); | 89 TestUseProxyAsUnscopables(); |
| 102 | 90 |
| 103 | 91 |
| 104 function TestThrowInHasUnscopables() { | 92 function TestThrowInHasUnscopables() { |
| 105 var x = 1; | 93 var x = 1; |
| 106 var object = { | 94 var object = { |
| 107 x: 2 | 95 x: 2 |
| 108 }; | 96 }; |
| 109 | 97 |
| 110 function CustomError() {} | 98 function CustomError() {} |
| 111 | 99 |
| 112 var calls = 0; | 100 var calls = 0; |
| 113 var proxy = new Proxy({}, { | 101 var proxy = new Proxy({}, { |
| 114 has: function(key) { | 102 has: function() { |
| 115 assertUnreachable(); | 103 assertUnreachable(); |
| 116 }, | 104 }, |
| 117 getPropertyDescriptor: function(key) { | 105 get: function(target, key) { |
| 118 if (calls++ === 0) { | 106 if (calls++ === 0) { |
| 119 throw new CustomError(); | 107 throw new CustomError(); |
| 120 } | 108 } |
| 121 assertUnreachable(); | 109 assertUnreachable(); |
| 122 } | 110 } |
| 123 }); | 111 }); |
| 124 | 112 |
| 125 object[Symbol.unscopables] = proxy; | 113 object[Symbol.unscopables] = proxy; |
| 126 | 114 |
| 127 assertThrows(function() { | 115 assertThrows(function() { |
| 128 with (object) { | 116 with (object) { |
| 129 x; | 117 x; |
| 130 } | 118 } |
| 131 }, CustomError); | 119 }, CustomError); |
| 132 } | 120 } |
| 133 TestThrowInHasUnscopables(); | 121 TestThrowInHasUnscopables(); |
| 134 | 122 |
| 135 | 123 |
| 136 var global = this; | 124 var global = this; |
| 137 function TestGlobalShouldIgnoreUnscopables() { | 125 function TestGlobalShouldIgnoreUnscopables() { |
| 138 global.x = 1; | 126 global.x = 1; |
| 139 var proxy = new Proxy({}, { | 127 var proxy = new Proxy({}, { |
| 140 getPropertyDescriptor: function() { | 128 get: function() { |
| 129 assertUnreachable(); |
| 130 }, |
| 131 has: function() { |
| 141 assertUnreachable(); | 132 assertUnreachable(); |
| 142 } | 133 } |
| 143 }); | 134 }); |
| 144 global[Symbol.unscopables] = proxy; | 135 global[Symbol.unscopables] = proxy; |
| 145 | 136 |
| 146 assertEquals(1, global.x); | 137 assertEquals(1, global.x); |
| 147 assertEquals(1, x); | 138 assertEquals(1, x); |
| 148 | 139 |
| 149 global.x = 2; | 140 global.x = 2; |
| 150 assertEquals(2, global.x); | 141 assertEquals(2, global.x); |
| 151 assertEquals(2, x); | 142 assertEquals(2, x); |
| 152 | 143 |
| 153 x = 3; | 144 x = 3; |
| 154 assertEquals(3, global.x); | 145 assertEquals(3, global.x); |
| 155 assertEquals(3, x); | 146 assertEquals(3, x); |
| 156 } | 147 } |
| 157 TestGlobalShouldIgnoreUnscopables(); | 148 TestGlobalShouldIgnoreUnscopables(); |
| OLD | NEW |