Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / v8 / test / mjsunit / harmony / proxies-with-unscopables.js
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-proxies
6
7
8 // TODO(arv): Once proxies can intercept symbols, add more tests.
9
10
11 function TestBasics() {
12   var log = [];
13
14   var proxy = Proxy.create({
15     getPropertyDescriptor: function(key) {
16       log.push(key);
17       if (key === 'x') {
18         return {
19           value: 1,
20           configurable: true
21         };
22       }
23       return undefined;
24     }
25   });
26
27   var x = 'local';
28
29   with (proxy) {
30     assertEquals(1, x);
31   }
32
33   // One 'x' for HasBinding and one for GetBindingValue
34   assertEquals(['assertEquals', 'x', 'x'], log);
35 }
36 TestBasics();
37
38
39 function TestInconsistent() {
40   var log = [];
41   var calls = 0;
42
43   var proxy = Proxy.create({
44     getPropertyDescriptor: function(key) {
45       log.push(key);
46       if (key === 'x' && calls < 1) {
47         calls++;
48         return {
49           value: 1,
50           configurable: true
51         };
52       }
53       return undefined;
54     }
55   });
56
57   var x = 'local';
58
59   with (proxy) {
60     assertEquals(void 0, x);
61   }
62
63   // One 'x' for HasBinding and one for GetBindingValue
64   assertEquals(['assertEquals', 'x', 'x'], log);
65 }
66 TestInconsistent();
67
68
69 function TestUseProxyAsUnscopables() {
70   var x = 1;
71   var object = {
72     x: 2
73   };
74   var calls = 0;
75   var proxy = Proxy.create({
76     has: function(key) {
77       calls++;
78       assertEquals('x', key);
79       return calls === 2;
80     },
81     getPropertyDescriptor: function(key) {
82       assertUnreachable();
83     }
84   });
85
86   object[Symbol.unscopables] = proxy;
87
88   with (object) {
89     assertEquals(2, x);
90     assertEquals(1, x);
91   }
92
93   // HasBinding, HasBinding
94   assertEquals(2, calls);
95 }
96 TestUseProxyAsUnscopables();
97
98
99 function TestThrowInHasUnscopables() {
100   var x = 1;
101   var object = {
102     x: 2
103   };
104
105   function CustomError() {}
106
107   var calls = 0;
108   var proxy = Proxy.create({
109     has: function(key) {
110       if (calls++ === 0) {
111         throw new CustomError();
112       }
113       assertUnreachable();
114     },
115     getPropertyDescriptor: function(key) {
116       assertUnreachable();
117     }
118   });
119
120   object[Symbol.unscopables] = proxy;
121
122   assertThrows(function() {
123     with (object) {
124       x;
125     }
126   }, CustomError);
127 }
128 TestThrowInHasUnscopables();
129
130
131 var global = this;
132 function TestGlobalShouldIgnoreUnscopables() {
133   global.x = 1;
134   var proxy = Proxy.create({
135     getPropertyDescriptor: function() {
136       assertUnreachable();
137     }
138   });
139   global[Symbol.unscopables] = proxy;
140
141   assertEquals(1, global.x);
142   assertEquals(1, x);
143
144   global.x = 2;
145   assertEquals(2, global.x);
146   assertEquals(2, x);
147
148   x = 3;
149   assertEquals(3, global.x);
150   assertEquals(3, x);
151 }
152 TestGlobalShouldIgnoreUnscopables();