blob: e52ee4303102a70a5bd57de7011910f69aaef80c [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2011 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// Flags: --harmony-proxies
29
Ben Murdoch3ef787d2012-04-12 10:51:47 +010030// Helper.
31
32function TestWithProxies(test, x, y, z) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000033 test(function(h){ return new Proxy({}, h) }, x, y, z)
Ben Murdoch3ef787d2012-04-12 10:51:47 +010034}
35
36
37// Iterate over a proxy.
38
39function TestForIn(properties, handler) {
40 TestWithProxies(TestForIn2, properties, handler)
41}
42
43function TestForIn2(create, properties, handler) {
44 var p = create(handler)
45 var found = []
46 for (var x in p) found.push(x)
47 assertArrayEquals(properties, found)
48}
49
50TestForIn(["0", "a"], {
Ben Murdoch097c5b22016-05-18 11:27:45 +010051 ownKeys() { return ["0", "a"] },
52 has(target, property) { return true },
53 getOwnPropertyDescriptor() { return { enumerable: true, configurable: true }}
Ben Murdoch3ef787d2012-04-12 10:51:47 +010054})
55
56TestForIn(["null", "a"], {
Ben Murdoch097c5b22016-05-18 11:27:45 +010057 ownKeys() { return this.enumerate() },
58 enumerate() { return ["null", "a"] },
59 has(target, property) { return true },
60 getOwnPropertyDescriptor() { return { enumerable: true, configurable: true }}
Ben Murdoch3ef787d2012-04-12 10:51:47 +010061})
62
Ben Murdoch3ef787d2012-04-12 10:51:47 +010063
64// Iterate over an object with a proxy prototype.
65
66function TestForInDerived(properties, handler) {
67 TestWithProxies(TestForInDerived2, properties, handler)
68}
69
70function TestForInDerived2(create, properties, handler) {
71 var p = create(handler)
72 var o = Object.create(p)
73 o.z = 0
74 var found = []
75 for (var x in o) found.push(x)
76 assertArrayEquals(["z"].concat(properties), found)
77
78 var oo = Object.create(o)
79 oo.y = 0
80 var found = []
81 for (var x in oo) found.push(x)
82 assertArrayEquals(["y", "z"].concat(properties), found)
83}
84
85TestForInDerived(["0", "a"], {
Ben Murdoch097c5b22016-05-18 11:27:45 +010086 ownKeys: function() { return ["0", "a"] },
87 has: function(t, k) { return k == "0" || k == "a" },
88 getOwnPropertyDescriptor() { return { enumerable: true, configurable: true }}
Ben Murdoch3ef787d2012-04-12 10:51:47 +010089})
90
91TestForInDerived(["null", "a"], {
Ben Murdoch097c5b22016-05-18 11:27:45 +010092 ownKeys: function() { return this.enumerate() },
93 enumerate: function() { return ["null", "a"] },
94 has: function(t, k) { return k == "null" || k == "a" },
95 getOwnPropertyDescriptor() { return { enumerable: true, configurable: true }}
Ben Murdoch3ef787d2012-04-12 10:51:47 +010096})
97
98
99
Ben Murdoch097c5b22016-05-18 11:27:45 +0100100// Throw exception in ownKeys trap.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100101
102function TestForInThrow(handler) {
103 TestWithProxies(TestForInThrow2, handler)
104}
105
106function TestForInThrow2(create, handler) {
107 var p = create(handler)
108 var o = Object.create(p)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000109 assertThrowsEquals(function(){ for (var x in p) {} }, "myexn")
110 assertThrowsEquals(function(){ for (var x in o) {} }, "myexn")
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100111}
112
113TestForInThrow({
Ben Murdoch097c5b22016-05-18 11:27:45 +0100114 ownKeys: function() { throw "myexn" }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100115})
116
117TestForInThrow({
Ben Murdoch097c5b22016-05-18 11:27:45 +0100118 ownKeys: function() { return this.enumerate() },
119 enumerate: function() { throw "myexn" }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100120})
121
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000122TestForInThrow(new Proxy({}, {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100123 get: function(pr, pk) {
124 return function() { throw "myexn" }
125 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000126}));
127
128(function() {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100129 var p = new Proxy({}, {ownKeys:function() { return ["0"]; }});
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000130 var o = [0];
131 o.__proto__ = p;
132 var keys = [];
133 for (var k in o) { keys.push(k); };
134 assertEquals(["0"], keys);
135})();
136
137(function () {
138 var p = new Proxy({}, {ownKeys: function() { return ["1", Symbol(), "2"] }});
139 assertEquals(["1","2"], Object.getOwnPropertyNames(p));
140})();