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 weakMap.set(new Object(), 44); |
| 59 var weakMapMirror = debug.MakeMirror(weakMap); |
| 60 testMapMirror(weakMapMirror); |
| 61 weakMap.set(new Object(), 55); |
| 62 assertTrue(weakMapMirror.entries().length <= 5); |
| 63 gc(); |
| 64 |
| 65 function testWeakMapEntries(weakMapMirror) { |
| 66 var entries = weakMapMirror.entries(); |
| 67 assertEquals(2, entries.length); |
| 68 var found = 0; |
| 69 for (var i = 0; i < entries.length; i++) { |
| 70 if (Object.is(entries[i].key, o1)) { |
| 71 assertEquals(11, entries[i].value); |
| 72 found++; |
| 73 } |
| 74 if (Object.is(entries[i].key, o3)) { |
| 75 assertEquals(33, entries[i].value); |
| 76 found++; |
| 77 } |
| 78 } |
| 79 assertEquals(2, found); |
| 80 } |
| 81 |
| 82 testWeakMapEntries(weakMapMirror); |
OLD | NEW |