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