OLD | NEW |
| (Empty) |
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 --harmony-collections | |
6 | |
7 function 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 | |
25 function 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 | |
43 var o1 = new Object(); | |
44 var o2 = new Object(); | |
45 var o3 = new Object(); | |
46 | |
47 // Test the mirror object for Maps | |
48 var map = new Map(); | |
49 map.set(o1, 11); | |
50 map.set(o2, 22); | |
51 map.delete(o1); | |
52 var mapMirror = debug.MakeMirror(map); | |
53 testMapMirror(mapMirror); | |
54 var entries = mapMirror.entries(); | |
55 assertEquals(1, entries.length); | |
56 assertSame(o2, entries[0].key); | |
57 assertEquals(22, entries[0].value); | |
58 map.set(o1, 33); | |
59 map.set(o3, o2); | |
60 map.delete(o2); | |
61 map.set(undefined, 44); | |
62 entries = mapMirror.entries(); | |
63 assertEquals(3, entries.length); | |
64 assertSame(o1, entries[0].key); | |
65 assertEquals(33, entries[0].value); | |
66 assertSame(o3, entries[1].key); | |
67 assertSame(o2, entries[1].value); | |
68 assertEquals(undefined, entries[2].key); | |
69 assertEquals(44, entries[2].value); | |
70 | |
71 // Test the mirror object for Sets | |
72 var set = new Set(); | |
73 set.add(o1); | |
74 set.add(o2); | |
75 set.delete(o1); | |
76 set.add(undefined); | |
77 var setMirror = debug.MakeMirror(set); | |
78 testSetMirror(setMirror); | |
79 var values = setMirror.values(); | |
80 assertEquals(2, values.length); | |
81 assertSame(o2, values[0]); | |
82 assertEquals(undefined, values[1]); | |
83 | |
84 // Test the mirror object for WeakMaps | |
85 var weakMap = new WeakMap(); | |
86 weakMap.set(o1, 11); | |
87 weakMap.set(new Object(), 22); | |
88 weakMap.set(o3, 33); | |
89 weakMap.set(new Object(), 44); | |
90 var weakMapMirror = debug.MakeMirror(weakMap); | |
91 testMapMirror(weakMapMirror); | |
92 weakMap.set(new Object(), 55); | |
93 assertTrue(weakMapMirror.entries().length <= 5); | |
94 gc(); | |
95 | |
96 function testWeakMapEntries(weakMapMirror) { | |
97 var entries = weakMapMirror.entries(); | |
98 assertEquals(2, entries.length); | |
99 var found = 0; | |
100 for (var i = 0; i < entries.length; i++) { | |
101 if (Object.is(entries[i].key, o1)) { | |
102 assertEquals(11, entries[i].value); | |
103 found++; | |
104 } | |
105 if (Object.is(entries[i].key, o3)) { | |
106 assertEquals(33, entries[i].value); | |
107 found++; | |
108 } | |
109 } | |
110 assertEquals(2, found); | |
111 } | |
112 | |
113 testWeakMapEntries(weakMapMirror); | |
114 | |
115 // Test the mirror object for WeakSets | |
116 var weakSet = new WeakSet(); | |
117 weakSet.add(o1); | |
118 weakSet.add(new Object()); | |
119 weakSet.add(o2); | |
120 weakSet.add(new Object()); | |
121 weakSet.add(new Object()); | |
122 weakSet.add(o3); | |
123 weakSet.delete(o2); | |
124 var weakSetMirror = debug.MakeMirror(weakSet); | |
125 testSetMirror(weakSetMirror); | |
126 assertTrue(weakSetMirror.values().length <= 5); | |
127 gc(); | |
128 | |
129 function testWeakSetValues(weakSetMirror) { | |
130 var values = weakSetMirror.values(); | |
131 assertEquals(2, values.length); | |
132 var found = 0; | |
133 for (var i = 0; i < values.length; i++) { | |
134 if (Object.is(values[i], o1)) { | |
135 found++; | |
136 } | |
137 if (Object.is(values[i], o3)) { | |
138 found++; | |
139 } | |
140 } | |
141 assertEquals(2, found); | |
142 } | |
143 | |
144 testWeakSetValues(weakSetMirror); | |
OLD | NEW |