1// Copyright 2015 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 --harmony-reflect
6
7
8
9function toKey(x) {
10  if (typeof x === "symbol") return x;
11  return String(x);
12}
13
14
15const noconf = {configurable: false};
16const noconf_nowrite = {configurable: false, writable: false};
17
18
19var symbol = Symbol();
20
21
22var log = [];
23var logger = {};
24var handler = new Proxy({}, logger);
25
26logger.get = function(t, trap, r) {
27  return function() {
28    log.push([trap, ...arguments]);
29    return Reflect[trap](...arguments);
30  }
31};
32
33
34(function Seal() {
35  var target = [];
36  var proxy = new Proxy(target, handler);
37  log.length = 0;
38
39  target.wurst = 42;
40  target[0] = true;
41  Object.defineProperty(target, symbol, {get: undefined});
42
43  Object.seal(proxy);
44  assertEquals(6, log.length)
45  for (var i in log) assertSame(target, log[i][1]);
46
47  assertArrayEquals(
48      ["preventExtensions", target], log[0]);
49  assertArrayEquals(
50      ["ownKeys", target], log[1]);
51  assertArrayEquals(
52      ["defineProperty", target, toKey(0), noconf], log[2]);
53  assertArrayEquals(
54      ["defineProperty", target, toKey("length"), noconf], log[3]);
55  assertArrayEquals(
56      ["defineProperty", target, toKey("wurst"), noconf], log[4]);
57  assertArrayEquals(
58      ["defineProperty", target, toKey(symbol), noconf], log[5]);
59})();
60
61
62(function Freeze() {
63  var target = [];
64  var proxy = new Proxy(target, handler);
65  log.length = 0;
66
67  target.wurst = 42;
68  target[0] = true;
69  Object.defineProperty(target, symbol, {get: undefined});
70
71  Object.freeze(proxy);
72  assertEquals(10, log.length)
73  for (var i in log) assertSame(target, log[i][1]);
74
75  assertArrayEquals(
76      ["preventExtensions", target], log[0]);
77  assertArrayEquals(
78      ["ownKeys", target], log[1]);
79  assertArrayEquals(
80      ["getOwnPropertyDescriptor", target, toKey(0)], log[2]);
81  assertArrayEquals(
82      ["defineProperty", target, toKey(0), noconf_nowrite], log[3]);
83  assertArrayEquals(
84      ["getOwnPropertyDescriptor", target, toKey("length")], log[4]);
85  assertArrayEquals(
86      ["defineProperty", target, toKey("length"), noconf_nowrite], log[5]);
87  assertArrayEquals(
88      ["getOwnPropertyDescriptor", target, toKey("wurst")], log[6]);
89  assertArrayEquals(
90      ["defineProperty", target, toKey("wurst"), noconf_nowrite], log[7]);
91  assertArrayEquals(
92      ["getOwnPropertyDescriptor", target, toKey(symbol)], log[8]);
93  assertArrayEquals(
94      ["defineProperty", target, toKey(symbol), noconf], log[9]);
95})();
96
97
98(function IsSealed() {
99  var target = [];
100  var proxy = new Proxy(target, handler);
101
102  target.wurst = 42;
103  target[0] = true;
104  Object.defineProperty(target, symbol, {get: undefined});
105
106  // Extensible.
107
108  log.length = 0;
109
110  Object.isSealed(proxy);
111  assertEquals(1, log.length)
112  for (var i in log) assertSame(target, log[i][1]);
113
114  assertArrayEquals(
115      ["isExtensible", target], log[0]);
116
117  // Not extensible but not sealed.
118
119  log.length = 0;
120  Object.preventExtensions(target);
121
122  Object.isSealed(proxy);
123  assertEquals(3, log.length)
124  for (var i in log) assertSame(target, log[i][1]);
125
126  assertArrayEquals(
127      ["isExtensible", target], log[0]);
128  assertArrayEquals(
129      ["ownKeys", target], log[1]);
130  assertArrayEquals(
131      ["getOwnPropertyDescriptor", target, toKey(0)], log[2]);
132
133  // Sealed.
134
135  log.length = 0;
136  Object.seal(target);
137
138  Object.isSealed(proxy);
139  assertEquals(6, log.length)
140  for (var i in log) assertSame(target, log[i][1]);
141
142  assertArrayEquals(
143      ["isExtensible", target], log[0]);
144  assertArrayEquals(
145      ["ownKeys", target], log[1]);
146  assertArrayEquals(
147      ["getOwnPropertyDescriptor", target, toKey(0)], log[2]);
148  assertArrayEquals(
149      ["getOwnPropertyDescriptor", target, toKey("length")], log[3]);
150  assertArrayEquals(
151      ["getOwnPropertyDescriptor", target, toKey("wurst")], log[4]);
152  assertArrayEquals(
153      ["getOwnPropertyDescriptor", target, toKey(symbol)], log[5]);
154})();
155
156
157(function IsFrozen() {
158  var target = [];
159  var proxy = new Proxy(target, handler);
160
161  target.wurst = 42;
162  target[0] = true;
163  Object.defineProperty(target, symbol, {get: undefined});
164
165  // Extensible.
166
167  log.length = 0;
168
169  Object.isFrozen(proxy);
170  assertEquals(1, log.length)
171  for (var i in log) assertSame(target, log[i][1]);
172
173  assertArrayEquals(
174      ["isExtensible", target], log[0]);
175
176  // Not extensible but not frozen.
177
178  log.length = 0;
179  Object.preventExtensions(target);
180
181  Object.isFrozen(proxy);
182  assertEquals(3, log.length)
183  for (var i in log) assertSame(target, log[i][1]);
184
185  assertArrayEquals(
186      ["isExtensible", target], log[0]);
187  assertArrayEquals(
188      ["ownKeys", target], log[1]);
189  assertArrayEquals(
190      ["getOwnPropertyDescriptor", target, toKey(0)], log[2]);
191
192  // Frozen.
193
194  log.length = 0;
195  Object.freeze(target);
196
197  Object.isFrozen(proxy);
198  assertEquals(6, log.length)
199  for (var i in log) assertSame(target, log[i][1]);
200
201  assertArrayEquals(
202      ["isExtensible", target], log[0]);
203  assertArrayEquals(
204      ["ownKeys", target], log[1]);
205  assertArrayEquals(
206      ["getOwnPropertyDescriptor", target, toKey(0)], log[2]);
207  assertArrayEquals(
208      ["getOwnPropertyDescriptor", target, toKey("length")], log[3]);
209  assertArrayEquals(
210      ["getOwnPropertyDescriptor", target, toKey("wurst")], log[4]);
211  assertArrayEquals(
212      ["getOwnPropertyDescriptor", target, toKey(symbol)], log[5]);
213})();
214