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 // TODO(arv): Once proxies can intercept symbols, add more tests. |
9 | 9 |
10 | 10 |
11 function TestBasics() { | 11 function TestBasics() { |
12 var log = []; | 12 var log = []; |
13 | 13 |
14 var proxy = Proxy.create({ | 14 var proxy = new Proxy({}, { |
15 getPropertyDescriptor: function(key) { | 15 getPropertyDescriptor: function(key) { |
16 log.push(key); | 16 log.push(key); |
17 if (key === 'x') { | 17 if (key === 'x') { |
18 return { | 18 return { |
19 value: 1, | 19 value: 1, |
20 configurable: true | 20 configurable: true |
21 }; | 21 }; |
22 } | 22 } |
23 return undefined; | 23 return undefined; |
24 } | 24 } |
25 }); | 25 }); |
26 | 26 |
27 var x = 'local'; | 27 var x = 'local'; |
28 | 28 |
29 with (proxy) { | 29 with (proxy) { |
30 assertEquals(1, x); | 30 assertEquals(1, x); |
31 } | 31 } |
32 | 32 |
33 // One 'x' for HasBinding and one for GetBindingValue | 33 // One 'x' for HasBinding and one for GetBindingValue |
34 assertEquals(['assertEquals', 'x', 'x'], log); | 34 assertEquals(['assertEquals', 'x', 'x'], log); |
35 } | 35 } |
36 TestBasics(); | 36 TestBasics(); |
37 | 37 |
38 | 38 |
39 function TestInconsistent() { | 39 function TestInconsistent() { |
40 var log = []; | 40 var log = []; |
41 var calls = 0; | 41 var calls = 0; |
42 | 42 |
43 var proxy = Proxy.create({ | 43 var proxy = new Proxy({}, { |
44 getPropertyDescriptor: function(key) { | 44 getPropertyDescriptor: function(key) { |
45 log.push(key); | 45 log.push(key); |
46 if (key === 'x' && calls < 1) { | 46 if (key === 'x' && calls < 1) { |
47 calls++; | 47 calls++; |
48 return { | 48 return { |
49 value: 1, | 49 value: 1, |
50 configurable: true | 50 configurable: true |
51 }; | 51 }; |
52 } | 52 } |
53 return undefined; | 53 return undefined; |
(...skipping 11 matching lines...) Expand all Loading... |
65 } | 65 } |
66 TestInconsistent(); | 66 TestInconsistent(); |
67 | 67 |
68 | 68 |
69 function TestUseProxyAsUnscopables() { | 69 function TestUseProxyAsUnscopables() { |
70 var x = 1; | 70 var x = 1; |
71 var object = { | 71 var object = { |
72 x: 2 | 72 x: 2 |
73 }; | 73 }; |
74 var calls = 0; | 74 var calls = 0; |
75 var proxy = Proxy.create({ | 75 var proxy = new Proxy({}, { |
76 has: function(key) { | 76 has: function(key) { |
77 assertUnreachable(); | 77 assertUnreachable(); |
78 }, | 78 }, |
79 getPropertyDescriptor: function(key) { | 79 getPropertyDescriptor: function(key) { |
80 calls++; | 80 calls++; |
81 assertEquals('x', key); | 81 assertEquals('x', key); |
82 return { | 82 return { |
83 value: calls === 2 ? true : undefined, | 83 value: calls === 2 ? true : undefined, |
84 configurable: true, | 84 configurable: true, |
85 enumerable: true, | 85 enumerable: true, |
(...skipping 17 matching lines...) Expand all Loading... |
103 | 103 |
104 function TestThrowInHasUnscopables() { | 104 function TestThrowInHasUnscopables() { |
105 var x = 1; | 105 var x = 1; |
106 var object = { | 106 var object = { |
107 x: 2 | 107 x: 2 |
108 }; | 108 }; |
109 | 109 |
110 function CustomError() {} | 110 function CustomError() {} |
111 | 111 |
112 var calls = 0; | 112 var calls = 0; |
113 var proxy = Proxy.create({ | 113 var proxy = new Proxy({}, { |
114 has: function(key) { | 114 has: function(key) { |
115 assertUnreachable(); | 115 assertUnreachable(); |
116 }, | 116 }, |
117 getPropertyDescriptor: function(key) { | 117 getPropertyDescriptor: function(key) { |
118 if (calls++ === 0) { | 118 if (calls++ === 0) { |
119 throw new CustomError(); | 119 throw new CustomError(); |
120 } | 120 } |
121 assertUnreachable(); | 121 assertUnreachable(); |
122 } | 122 } |
123 }); | 123 }); |
124 | 124 |
125 object[Symbol.unscopables] = proxy; | 125 object[Symbol.unscopables] = proxy; |
126 | 126 |
127 assertThrows(function() { | 127 assertThrows(function() { |
128 with (object) { | 128 with (object) { |
129 x; | 129 x; |
130 } | 130 } |
131 }, CustomError); | 131 }, CustomError); |
132 } | 132 } |
133 TestThrowInHasUnscopables(); | 133 TestThrowInHasUnscopables(); |
134 | 134 |
135 | 135 |
136 var global = this; | 136 var global = this; |
137 function TestGlobalShouldIgnoreUnscopables() { | 137 function TestGlobalShouldIgnoreUnscopables() { |
138 global.x = 1; | 138 global.x = 1; |
139 var proxy = Proxy.create({ | 139 var proxy = new Proxy({}, { |
140 getPropertyDescriptor: function() { | 140 getPropertyDescriptor: function() { |
141 assertUnreachable(); | 141 assertUnreachable(); |
142 } | 142 } |
143 }); | 143 }); |
144 global[Symbol.unscopables] = proxy; | 144 global[Symbol.unscopables] = proxy; |
145 | 145 |
146 assertEquals(1, global.x); | 146 assertEquals(1, global.x); |
147 assertEquals(1, x); | 147 assertEquals(1, x); |
148 | 148 |
149 global.x = 2; | 149 global.x = 2; |
150 assertEquals(2, global.x); | 150 assertEquals(2, global.x); |
151 assertEquals(2, x); | 151 assertEquals(2, x); |
152 | 152 |
153 x = 3; | 153 x = 3; |
154 assertEquals(3, global.x); | 154 assertEquals(3, global.x); |
155 assertEquals(3, x); | 155 assertEquals(3, x); |
156 } | 156 } |
157 TestGlobalShouldIgnoreUnscopables(); | 157 TestGlobalShouldIgnoreUnscopables(); |
OLD | NEW |