Home | History | Annotate | Download | only in es6
      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 
      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 
     55 var entries = mapMirror.entries();
     56 assertEquals(1, entries.length);
     57 assertSame(o2, entries[0].key);
     58 assertEquals(22, entries[0].value);
     59 map.set(o1, 33);
     60 map.set(o3, o2);
     61 map.delete(o2);
     62 map.set(undefined, 44);
     63 
     64 entries = mapMirror.entries();
     65 assertEquals(3, entries.length);
     66 assertSame(o1, entries[0].key);
     67 assertEquals(33, entries[0].value);
     68 assertSame(o3, entries[1].key);
     69 assertSame(o2, entries[1].value);
     70 assertEquals(undefined, entries[2].key);
     71 assertEquals(44, entries[2].value);
     72 
     73 assertEquals(3, mapMirror.entries(0).length);
     74 assertEquals(1, mapMirror.entries(1).length);
     75 assertEquals(2, mapMirror.entries(2).length);
     76 
     77 // Test the mirror object for Sets
     78 var set = new Set();
     79 set.add(o1);
     80 set.add(o2);
     81 set.delete(o1);
     82 set.add(undefined);
     83 var setMirror = debug.MakeMirror(set);
     84 testSetMirror(setMirror);
     85 var values = setMirror.values();
     86 assertEquals(2, values.length);
     87 assertEquals(1, setMirror.values(1).length);
     88 assertSame(o2, values[0]);
     89 assertEquals(undefined, values[1]);
     90 
     91 // Test the mirror object for WeakMaps
     92 var weakMap = new WeakMap();
     93 weakMap.set(o1, 11);
     94 weakMap.set(new Object(), 22);
     95 weakMap.set(o3, 33);
     96 weakMap.set(new Object(), 44);
     97 var weakMapMirror = debug.MakeMirror(weakMap);
     98 testMapMirror(weakMapMirror);
     99 weakMap.set(new Object(), 55);
    100 assertTrue(weakMapMirror.entries().length <= 5);
    101 gc();
    102 
    103 function 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 
    122 testWeakMapEntries(weakMapMirror);
    123 
    124 // Test the mirror object for WeakSets
    125 var weakSet = new WeakSet();
    126 weakSet.add(o1);
    127 weakSet.add(new Object());
    128 weakSet.add(o2);
    129 weakSet.add(new Object());
    130 weakSet.add(new Object());
    131 weakSet.add(o3);
    132 weakSet.delete(o2);
    133 var weakSetMirror = debug.MakeMirror(weakSet);
    134 testSetMirror(weakSetMirror);
    135 assertTrue(weakSetMirror.values().length <= 5);
    136 gc();
    137 
    138 function 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 
    155 testWeakSetValues(weakSetMirror);
    156