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 var o1 = new Object(); | |
26 var o2 = new Object(); | |
27 var o3 = new Object(); | |
28 | |
29 // Test the mirror object for Maps | |
30 var map = new Map(); | |
31 map.set(o1, 11); | |
32 map.set(o2, 22); | |
33 map.delete(o1); | |
34 var mapMirror = debug.MakeMirror(map); | |
35 testMapMirror(mapMirror); | |
36 var entries = mapMirror.entries(); | |
37 assertEquals(1, entries.length); | |
38 assertSame(o2, entries[0].key); | |
39 assertEquals(22, entries[0].value); | |
40 map.set(o1, 33); | |
41 map.set(o3, o2); | |
42 map.delete(o2); | |
43 map.set(undefined, 44); | |
44 entries = mapMirror.entries(); | |
45 assertEquals(3, entries.length); | |
46 assertSame(o1, entries[0].key); | |
47 assertEquals(33, entries[0].value); | |
48 assertSame(o3, entries[1].key); | |
49 assertSame(o2, entries[1].value); | |
50 assertEquals(undefined, entries[2].key); | |
51 assertEquals(44, entries[2].value); | |
52 | |
53 // Test the mirror object for WeakMaps | |
54 var weakMap = new WeakMap(); | |
55 weakMap.set(o1, 11); | |
56 weakMap.set(new Object(), 22); | |
57 weakMap.set(o3, 33); | |
58 var weakMapMirror = debug.MakeMirror(weakMap); | |
59 testMapMirror(weakMapMirror); | |
60 | |
61 function testWeakMapEntries(entries, gcDone) { | |
62 var found = 0; | |
63 for (var i = 0; i < entries.length; i++) { | |
64 if (Object.is(entries[i].key, o1)) { | |
65 assertEquals(11, entries[i].value); | |
66 found++; | |
67 continue; | |
68 } | |
69 if (Object.is(entries[i].key, o3)) { | |
70 assertEquals(33, entries[i].value); | |
71 found++; | |
72 continue; | |
73 } | |
74 if (!gcDone && entries[i].value == 22) | |
75 found++; | |
76 } | |
77 return found; | |
78 } | |
79 | |
80 function testWeakMapMirrorBeforeGC(mirror) { | |
81 var entries = mirror.entries(); | |
82 assertEquals(3, entries.length); | |
83 assertEquals(3, testWeakMapEntries(entries, false)); | |
84 } | |
85 | |
86 testWeakMapMirrorBeforeGC(weakMapMirror); | |
87 gc(); | |
88 | |
89 function testWeakMapMirrorAfterGC(mirror) { | |
90 var entries = mirror.entries(); | |
91 assertEquals(2, entries.length); | |
92 assertEquals(2, testWeakMapEntries(entries, true)); | |
93 } | |
94 | |
95 testWeakMapMirrorAfterGC(weakMapMirror); | |
OLD | NEW |