Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6 | function TestBasics() { |
| 7 | var log = []; |
| 8 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9 | var proxy = new Proxy({}, { |
| 10 | get: function(target, key) { |
| 11 | log.push("get " + String(key)); |
| 12 | if (key === 'x') return 1; |
| 13 | }, |
| 14 | has: function(target, key) { |
| 15 | log.push("has " + String(key)); |
| 16 | if (key === 'x') return true; |
| 17 | return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18 | } |
| 19 | }); |
| 20 | |
| 21 | var x = 'local'; |
| 22 | |
| 23 | with (proxy) { |
| 24 | assertEquals(1, x); |
| 25 | } |
| 26 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 27 | assertEquals(['has assertEquals', 'has x', 'get Symbol(Symbol.unscopables)', |
| 28 | 'get x'], log); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 29 | } |
| 30 | TestBasics(); |
| 31 | |
| 32 | |
| 33 | function TestInconsistent() { |
| 34 | var log = []; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 35 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 36 | var proxy = new Proxy({}, { |
| 37 | get: function(target, key) { |
| 38 | log.push("get " + String(key)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 39 | return undefined; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 40 | }, |
| 41 | has: function(target, key) { |
| 42 | log.push("has " + String(key)); |
| 43 | if (key === 'x') return true; |
| 44 | return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 45 | } |
| 46 | }); |
| 47 | |
| 48 | var x = 'local'; |
| 49 | |
| 50 | with (proxy) { |
| 51 | assertEquals(void 0, x); |
| 52 | } |
| 53 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 54 | assertEquals(['has assertEquals', 'has x', 'get Symbol(Symbol.unscopables)', |
| 55 | 'get x'], log); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 56 | } |
| 57 | TestInconsistent(); |
| 58 | |
| 59 | |
| 60 | function TestUseProxyAsUnscopables() { |
| 61 | var x = 1; |
| 62 | var object = { |
| 63 | x: 2 |
| 64 | }; |
| 65 | var calls = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 66 | var proxy = new Proxy({}, { |
| 67 | has: function() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 68 | assertUnreachable(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 69 | }, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 70 | get: function(target, key) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 71 | assertEquals('x', key); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 72 | calls++; |
| 73 | return calls === 2 ? true : undefined; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 74 | } |
| 75 | }); |
| 76 | |
| 77 | object[Symbol.unscopables] = proxy; |
| 78 | |
| 79 | with (object) { |
| 80 | assertEquals(2, x); |
| 81 | assertEquals(1, x); |
| 82 | } |
| 83 | |
| 84 | // HasBinding, HasBinding |
| 85 | assertEquals(2, calls); |
| 86 | } |
| 87 | TestUseProxyAsUnscopables(); |
| 88 | |
| 89 | |
| 90 | function TestThrowInHasUnscopables() { |
| 91 | var x = 1; |
| 92 | var object = { |
| 93 | x: 2 |
| 94 | }; |
| 95 | |
| 96 | function CustomError() {} |
| 97 | |
| 98 | var calls = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 99 | var proxy = new Proxy({}, { |
| 100 | has: function() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 101 | assertUnreachable(); |
| 102 | }, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 103 | get: function(target, key) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 104 | if (calls++ === 0) { |
| 105 | throw new CustomError(); |
| 106 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 107 | assertUnreachable(); |
| 108 | } |
| 109 | }); |
| 110 | |
| 111 | object[Symbol.unscopables] = proxy; |
| 112 | |
| 113 | assertThrows(function() { |
| 114 | with (object) { |
| 115 | x; |
| 116 | } |
| 117 | }, CustomError); |
| 118 | } |
| 119 | TestThrowInHasUnscopables(); |
| 120 | |
| 121 | |
| 122 | var global = this; |
| 123 | function TestGlobalShouldIgnoreUnscopables() { |
| 124 | global.x = 1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 125 | var proxy = new Proxy({}, { |
| 126 | get: function() { |
| 127 | assertUnreachable(); |
| 128 | }, |
| 129 | has: function() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 130 | assertUnreachable(); |
| 131 | } |
| 132 | }); |
| 133 | global[Symbol.unscopables] = proxy; |
| 134 | |
| 135 | assertEquals(1, global.x); |
| 136 | assertEquals(1, x); |
| 137 | |
| 138 | global.x = 2; |
| 139 | assertEquals(2, global.x); |
| 140 | assertEquals(2, x); |
| 141 | |
| 142 | x = 3; |
| 143 | assertEquals(3, global.x); |
| 144 | assertEquals(3, x); |
| 145 | } |
| 146 | TestGlobalShouldIgnoreUnscopables(); |