Chromium Code Reviews| 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. | 8 function str(x) { |
| 9 if (typeof x === "symbol") return "<Symbol>"; | |
| 10 return "" + x; | |
| 11 } | |
| 9 | 12 |
|
neis
2015/12/14 15:53:55
Why not use String(x)? Then you can test for "Sym
Jakob Kummerow
2015/12/14 15:59:12
Done.
| |
| 10 | 13 |
| 11 function TestBasics() { | 14 function TestBasics() { |
| 12 var log = []; | 15 var log = []; |
| 13 | 16 |
| 14 var proxy = new Proxy({}, { | 17 var proxy = new Proxy({}, { |
| 15 getPropertyDescriptor: function(key) { | 18 get: function(target, key) { |
| 16 log.push(key); | 19 log.push("get " + str(key)); |
| 17 if (key === 'x') { | 20 if (key === 'x') return 1; |
| 18 return { | 21 }, |
| 19 value: 1, | 22 has: function(target, key) { |
| 20 configurable: true | 23 log.push("has " + str(key)); |
| 21 }; | 24 if (key === 'x') return true; |
| 22 } | 25 return false; |
| 23 return undefined; | |
| 24 } | 26 } |
| 25 }); | 27 }); |
| 26 | 28 |
| 27 var x = 'local'; | 29 var x = 'local'; |
| 28 | 30 |
| 29 with (proxy) { | 31 with (proxy) { |
| 30 assertEquals(1, x); | 32 assertEquals(1, x); |
| 31 } | 33 } |
| 32 | 34 |
| 33 // One 'x' for HasBinding and one for GetBindingValue | 35 assertEquals(['has assertEquals', 'has x', 'get <Symbol>', 'get x'], log); |
| 34 assertEquals(['assertEquals', 'x', 'x'], log); | |
| 35 } | 36 } |
| 36 TestBasics(); | 37 TestBasics(); |
| 37 | 38 |
| 38 | 39 |
| 39 function TestInconsistent() { | 40 function TestInconsistent() { |
| 40 var log = []; | 41 var log = []; |
| 41 var calls = 0; | |
| 42 | 42 |
| 43 var proxy = new Proxy({}, { | 43 var proxy = new Proxy({}, { |
| 44 getPropertyDescriptor: function(key) { | 44 get: function(target, key) { |
| 45 log.push(key); | 45 log.push("get " + str(key)); |
| 46 if (key === 'x' && calls < 1) { | |
| 47 calls++; | |
| 48 return { | |
| 49 value: 1, | |
| 50 configurable: true | |
| 51 }; | |
| 52 } | |
| 53 return undefined; | 46 return undefined; |
| 47 }, | |
| 48 has: function(target, key) { | |
| 49 log.push("has " + str(key)); | |
| 50 if (key === 'x') return true; | |
| 51 return false; | |
| 54 } | 52 } |
| 55 }); | 53 }); |
| 56 | 54 |
| 57 var x = 'local'; | 55 var x = 'local'; |
| 58 | 56 |
| 59 with (proxy) { | 57 with (proxy) { |
| 60 assertEquals(void 0, x); | 58 assertEquals(void 0, x); |
| 61 } | 59 } |
| 62 | 60 |
| 63 // One 'x' for HasBinding and one for GetBindingValue | 61 assertEquals(['has assertEquals', 'has x', 'get <Symbol>', 'get x'], log); |
| 64 assertEquals(['assertEquals', 'x', 'x'], log); | |
| 65 } | 62 } |
| 66 TestInconsistent(); | 63 TestInconsistent(); |
| 67 | 64 |
| 68 | 65 |
| 69 function TestUseProxyAsUnscopables() { | 66 function TestUseProxyAsUnscopables() { |
| 70 var x = 1; | 67 var x = 1; |
| 71 var object = { | 68 var object = { |
| 72 x: 2 | 69 x: 2 |
| 73 }; | 70 }; |
| 74 var calls = 0; | 71 var calls = 0; |
| 75 var proxy = new Proxy({}, { | 72 var proxy = new Proxy({}, { |
| 76 has: function(key) { | 73 has: function() { |
| 77 assertUnreachable(); | 74 assertUnreachable(); |
| 78 }, | 75 }, |
| 79 getPropertyDescriptor: function(key) { | 76 get: function(target, key) { |
| 77 assertEquals('x', key); | |
| 80 calls++; | 78 calls++; |
| 81 assertEquals('x', key); | 79 return calls === 2 ? true : undefined; |
| 82 return { | |
| 83 value: calls === 2 ? true : undefined, | |
| 84 configurable: true, | |
| 85 enumerable: true, | |
| 86 writable: true, | |
| 87 }; | |
| 88 } | 80 } |
| 89 }); | 81 }); |
| 90 | 82 |
| 91 object[Symbol.unscopables] = proxy; | 83 object[Symbol.unscopables] = proxy; |
| 92 | 84 |
| 93 with (object) { | 85 with (object) { |
| 94 assertEquals(2, x); | 86 assertEquals(2, x); |
| 95 assertEquals(1, x); | 87 assertEquals(1, x); |
| 96 } | 88 } |
| 97 | 89 |
| 98 // HasBinding, HasBinding | 90 // HasBinding, HasBinding |
| 99 assertEquals(2, calls); | 91 assertEquals(2, calls); |
| 100 } | 92 } |
| 101 TestUseProxyAsUnscopables(); | 93 TestUseProxyAsUnscopables(); |
| 102 | 94 |
| 103 | 95 |
| 104 function TestThrowInHasUnscopables() { | 96 function TestThrowInHasUnscopables() { |
| 105 var x = 1; | 97 var x = 1; |
| 106 var object = { | 98 var object = { |
| 107 x: 2 | 99 x: 2 |
| 108 }; | 100 }; |
| 109 | 101 |
| 110 function CustomError() {} | 102 function CustomError() {} |
| 111 | 103 |
| 112 var calls = 0; | 104 var calls = 0; |
| 113 var proxy = new Proxy({}, { | 105 var proxy = new Proxy({}, { |
| 114 has: function(key) { | 106 has: function() { |
| 115 assertUnreachable(); | 107 assertUnreachable(); |
| 116 }, | 108 }, |
| 117 getPropertyDescriptor: function(key) { | 109 get: function(target, key) { |
| 118 if (calls++ === 0) { | 110 if (calls++ === 0) { |
| 119 throw new CustomError(); | 111 throw new CustomError(); |
| 120 } | 112 } |
| 121 assertUnreachable(); | 113 assertUnreachable(); |
| 122 } | 114 } |
| 123 }); | 115 }); |
| 124 | 116 |
| 125 object[Symbol.unscopables] = proxy; | 117 object[Symbol.unscopables] = proxy; |
| 126 | 118 |
| 127 assertThrows(function() { | 119 assertThrows(function() { |
| 128 with (object) { | 120 with (object) { |
| 129 x; | 121 x; |
| 130 } | 122 } |
| 131 }, CustomError); | 123 }, CustomError); |
| 132 } | 124 } |
| 133 TestThrowInHasUnscopables(); | 125 TestThrowInHasUnscopables(); |
| 134 | 126 |
| 135 | 127 |
| 136 var global = this; | 128 var global = this; |
| 137 function TestGlobalShouldIgnoreUnscopables() { | 129 function TestGlobalShouldIgnoreUnscopables() { |
| 138 global.x = 1; | 130 global.x = 1; |
| 139 var proxy = new Proxy({}, { | 131 var proxy = new Proxy({}, { |
| 140 getPropertyDescriptor: function() { | 132 get: function() { |
| 133 assertUnreachable(); | |
| 134 }, | |
| 135 has: function() { | |
| 141 assertUnreachable(); | 136 assertUnreachable(); |
| 142 } | 137 } |
| 143 }); | 138 }); |
| 144 global[Symbol.unscopables] = proxy; | 139 global[Symbol.unscopables] = proxy; |
| 145 | 140 |
| 146 assertEquals(1, global.x); | 141 assertEquals(1, global.x); |
| 147 assertEquals(1, x); | 142 assertEquals(1, x); |
| 148 | 143 |
| 149 global.x = 2; | 144 global.x = 2; |
| 150 assertEquals(2, global.x); | 145 assertEquals(2, global.x); |
| 151 assertEquals(2, x); | 146 assertEquals(2, x); |
| 152 | 147 |
| 153 x = 3; | 148 x = 3; |
| 154 assertEquals(3, global.x); | 149 assertEquals(3, global.x); |
| 155 assertEquals(3, x); | 150 assertEquals(3, x); |
| 156 } | 151 } |
| 157 TestGlobalShouldIgnoreUnscopables(); | 152 TestGlobalShouldIgnoreUnscopables(); |
| OLD | NEW |