blob: b9a7ad8012488d320a6210dc7bdeb9e62dcb3ec7 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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 Murdochb8a8cc12014-11-26 15:28:44 +00005
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006function TestBasics() {
7 var log = [];
8
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009 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 Murdochb8a8cc12014-11-26 15:28:44 +000018 }
19 });
20
21 var x = 'local';
22
23 with (proxy) {
24 assertEquals(1, x);
25 }
26
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000027 assertEquals(['has assertEquals', 'has x', 'get Symbol(Symbol.unscopables)',
28 'get x'], log);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000029}
30TestBasics();
31
32
33function TestInconsistent() {
34 var log = [];
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000036 var proxy = new Proxy({}, {
37 get: function(target, key) {
38 log.push("get " + String(key));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039 return undefined;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000040 },
41 has: function(target, key) {
42 log.push("has " + String(key));
43 if (key === 'x') return true;
44 return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045 }
46 });
47
48 var x = 'local';
49
50 with (proxy) {
51 assertEquals(void 0, x);
52 }
53
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000054 assertEquals(['has assertEquals', 'has x', 'get Symbol(Symbol.unscopables)',
55 'get x'], log);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056}
57TestInconsistent();
58
59
60function TestUseProxyAsUnscopables() {
61 var x = 1;
62 var object = {
63 x: 2
64 };
65 var calls = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066 var proxy = new Proxy({}, {
67 has: function() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040068 assertUnreachable();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069 },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000070 get: function(target, key) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040071 assertEquals('x', key);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000072 calls++;
73 return calls === 2 ? true : undefined;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 }
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}
87TestUseProxyAsUnscopables();
88
89
90function TestThrowInHasUnscopables() {
91 var x = 1;
92 var object = {
93 x: 2
94 };
95
96 function CustomError() {}
97
98 var calls = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099 var proxy = new Proxy({}, {
100 has: function() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101 assertUnreachable();
102 },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000103 get: function(target, key) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400104 if (calls++ === 0) {
105 throw new CustomError();
106 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000107 assertUnreachable();
108 }
109 });
110
111 object[Symbol.unscopables] = proxy;
112
113 assertThrows(function() {
114 with (object) {
115 x;
116 }
117 }, CustomError);
118}
119TestThrowInHasUnscopables();
120
121
122var global = this;
123function TestGlobalShouldIgnoreUnscopables() {
124 global.x = 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000125 var proxy = new Proxy({}, {
126 get: function() {
127 assertUnreachable();
128 },
129 has: function() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000130 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}
146TestGlobalShouldIgnoreUnscopables();