blob: 05433f0dce1b9edcf420f132496fc04cf238d3c5 [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
Ben Murdoch3ef787d2012-04-12 10:51:47 +010028
29// Helper.
30
31function TestWithProxies(test, construct, handler) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000032 test(construct, handler, function(h) { return new Proxy({}, h) })
33 // TODO(cbruni): Adapt and enable once we have [[Call]] working.
34 // test(construct, handler, function(h) {
35 // return Proxy.createFunction(h, function() {})
36 // })
Ben Murdoch3ef787d2012-04-12 10:51:47 +010037}
38
39
40// Sets.
41
42function TestSet(construct, fix) {
43 TestWithProxies(TestSet2, construct, fix)
44}
45
46function TestSet2(construct, fix, create) {
47 var handler = {fix: function() { return {} }}
48 var p1 = create(handler)
49 var p2 = create(handler)
50 var p3 = create(handler)
51 fix(p3)
52
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053 var s = new construct();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010054 s.add(p1);
55 s.add(p2);
56 assertTrue(s.has(p1));
57 assertTrue(s.has(p2));
58 assertFalse(s.has(p3));
59
60 fix(p1)
61 fix(p2)
62 assertTrue(s.has(p1));
63 assertTrue(s.has(p2));
64 assertFalse(s.has(p3));
65
66 s.delete(p2);
67 assertTrue(s.has(p1));
68 assertFalse(s.has(p2));
69 assertFalse(s.has(p3));
70}
71
72TestSet(Set, Object.seal)
73TestSet(Set, Object.freeze)
74TestSet(Set, Object.preventExtensions)
75
76
77// Maps and weak maps.
78
79function TestMap(construct, fix) {
80 TestWithProxies(TestMap2, construct, fix)
81}
82
83function TestMap2(construct, fix, create) {
84 var handler = {fix: function() { return {} }}
85 var p1 = create(handler)
86 var p2 = create(handler)
87 var p3 = create(handler)
88 fix(p3)
89
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090 var m = new construct();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010091 m.set(p1, 123);
92 m.set(p2, 321);
93 assertTrue(m.has(p1));
94 assertTrue(m.has(p2));
95 assertFalse(m.has(p3));
96 assertSame(123, m.get(p1));
97 assertSame(321, m.get(p2));
98
99 fix(p1)
100 fix(p2)
101 assertTrue(m.has(p1));
102 assertTrue(m.has(p2));
103 assertFalse(m.has(p3));
104 assertSame(123, m.get(p1));
105 assertSame(321, m.get(p2));
106
107 m.delete(p2);
108 assertTrue(m.has(p1));
109 assertFalse(m.has(p2));
110 assertFalse(m.has(p3));
111 assertSame(123, m.get(p1));
112 assertSame(undefined, m.get(p2));
113}
114
115TestMap(Map, Object.seal)
116TestMap(Map, Object.freeze)
117TestMap(Map, Object.preventExtensions)
118
119TestMap(WeakMap, Object.seal)
120TestMap(WeakMap, Object.freeze)
121TestMap(WeakMap, Object.preventExtensions)