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: --expose-debug-as debug --expose-gc
6
7function testMapMirror(mirror) {
8  // Create JSON representation.
9  var serializer = debug.MakeMirrorSerializer();
10  var json = JSON.stringify(serializer.serializeValue(mirror));
11
12  // Check the mirror hierachy.
13  assertTrue(mirror instanceof debug.Mirror);
14  assertTrue(mirror instanceof debug.ValueMirror);
15  assertTrue(mirror instanceof debug.ObjectMirror);
16  assertTrue(mirror instanceof debug.MapMirror);
17
18  assertTrue(mirror.isMap());
19
20  // Parse JSON representation and check.
21  var fromJSON = eval('(' + json + ')');
22  assertEquals('map', fromJSON.type);
23}
24
25function testSetMirror(mirror) {
26  // Create JSON representation.
27  var serializer = debug.MakeMirrorSerializer();
28  var json = JSON.stringify(serializer.serializeValue(mirror));
29
30  // Check the mirror hierachy.
31  assertTrue(mirror instanceof debug.Mirror);
32  assertTrue(mirror instanceof debug.ValueMirror);
33  assertTrue(mirror instanceof debug.ObjectMirror);
34  assertTrue(mirror instanceof debug.SetMirror);
35
36  assertTrue(mirror.isSet());
37
38  // Parse JSON representation and check.
39  var fromJSON = eval('(' + json + ')');
40  assertEquals('set', fromJSON.type);
41}
42
43var o1 = new Object();
44var o2 = new Object();
45var o3 = new Object();
46
47// Test the mirror object for Maps
48var map = new Map();
49map.set(o1, 11);
50map.set(o2, 22);
51map.delete(o1);
52var mapMirror = debug.MakeMirror(map);
53testMapMirror(mapMirror);
54
55var entries = mapMirror.entries();
56assertEquals(1, entries.length);
57assertSame(o2, entries[0].key);
58assertEquals(22, entries[0].value);
59map.set(o1, 33);
60map.set(o3, o2);
61map.delete(o2);
62map.set(undefined, 44);
63
64entries = mapMirror.entries();
65assertEquals(3, entries.length);
66assertSame(o1, entries[0].key);
67assertEquals(33, entries[0].value);
68assertSame(o3, entries[1].key);
69assertSame(o2, entries[1].value);
70assertEquals(undefined, entries[2].key);
71assertEquals(44, entries[2].value);
72
73assertEquals(3, mapMirror.entries(0).length);
74assertEquals(1, mapMirror.entries(1).length);
75assertEquals(2, mapMirror.entries(2).length);
76
77// Test the mirror object for Sets
78var set = new Set();
79set.add(o1);
80set.add(o2);
81set.delete(o1);
82set.add(undefined);
83var setMirror = debug.MakeMirror(set);
84testSetMirror(setMirror);
85var values = setMirror.values();
86assertEquals(2, values.length);
87assertEquals(1, setMirror.values(1).length);
88assertSame(o2, values[0]);
89assertEquals(undefined, values[1]);
90
91// Test the mirror object for WeakMaps
92var weakMap = new WeakMap();
93weakMap.set(o1, 11);
94weakMap.set(new Object(), 22);
95weakMap.set(o3, 33);
96weakMap.set(new Object(), 44);
97var weakMapMirror = debug.MakeMirror(weakMap);
98testMapMirror(weakMapMirror);
99weakMap.set(new Object(), 55);
100assertTrue(weakMapMirror.entries().length <= 5);
101gc();
102
103function testWeakMapEntries(weakMapMirror) {
104  var entries = weakMapMirror.entries();
105  assertEquals(2, entries.length);
106  assertEquals(2, weakMapMirror.entries(0).length);
107  assertEquals(1, weakMapMirror.entries(1).length);
108  var found = 0;
109  for (var i = 0; i < entries.length; i++) {
110    if (Object.is(entries[i].key, o1)) {
111      assertEquals(11, entries[i].value);
112      found++;
113    }
114    if (Object.is(entries[i].key, o3)) {
115      assertEquals(33, entries[i].value);
116      found++;
117    }
118  }
119  assertEquals(2, found);
120}
121
122testWeakMapEntries(weakMapMirror);
123
124// Test the mirror object for WeakSets
125var weakSet = new WeakSet();
126weakSet.add(o1);
127weakSet.add(new Object());
128weakSet.add(o2);
129weakSet.add(new Object());
130weakSet.add(new Object());
131weakSet.add(o3);
132weakSet.delete(o2);
133var weakSetMirror = debug.MakeMirror(weakSet);
134testSetMirror(weakSetMirror);
135assertTrue(weakSetMirror.values().length <= 5);
136gc();
137
138function testWeakSetValues(weakSetMirror) {
139  var values = weakSetMirror.values();
140  assertEquals(2, values.length);
141  assertEquals(2, weakSetMirror.values(0).length);
142  assertEquals(1, weakSetMirror.values(1).length);
143  var found = 0;
144  for (var i = 0; i < values.length; i++) {
145    if (Object.is(values[i], o1)) {
146      found++;
147    }
148    if (Object.is(values[i], o3)) {
149      found++;
150    }
151  }
152  assertEquals(2, found);
153}
154
155testWeakSetValues(weakSetMirror);
156