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
      6 // Test the mirror object for functions.
      7 
      8 function *generator(f) {
      9   "use strict";
     10   yield;
     11   f();
     12   yield;
     13 }
     14 
     15 function MirrorRefCache(json_refs) {
     16   var tmp = eval('(' + json_refs + ')');
     17   this.refs_ = [];
     18   for (var i = 0; i < tmp.length; i++) {
     19     this.refs_[tmp[i].handle] = tmp[i];
     20   }
     21 }
     22 
     23 MirrorRefCache.prototype.lookup = function(handle) {
     24   return this.refs_[handle];
     25 }
     26 
     27 function TestGeneratorMirror(g, test) {
     28   // Create mirror and JSON representation.
     29   var mirror = debug.MakeMirror(g);
     30   var serializer = debug.MakeMirrorSerializer();
     31   var json = JSON.stringify(serializer.serializeValue(mirror));
     32   var refs = new MirrorRefCache(
     33       JSON.stringify(serializer.serializeReferencedObjects()));
     34 
     35   // Check the mirror hierachy.
     36   assertTrue(mirror instanceof debug.Mirror);
     37   assertTrue(mirror instanceof debug.ValueMirror);
     38   assertTrue(mirror instanceof debug.ObjectMirror);
     39   assertTrue(mirror instanceof debug.GeneratorMirror);
     40 
     41   // Check the mirror properties.
     42   assertTrue(mirror.isGenerator());
     43   assertEquals('generator', mirror.type());
     44   assertFalse(mirror.isPrimitive());
     45   assertEquals('Generator', mirror.className());
     46 
     47   assertTrue(mirror.receiver().isUndefined());
     48   assertEquals(generator, mirror.func().value());
     49 
     50   test(mirror);
     51 }
     52 
     53 var iter = generator(function () {
     54   assertEquals('running', debug.MakeMirror(iter).status());
     55 })
     56 
     57 // Note that line numbers are 0-based, not 1-based.
     58 function assertSourceLocation(loc, line, column) {
     59   assertEquals(line, loc.line);
     60   assertEquals(column, loc.column);
     61 }
     62 
     63 TestGeneratorMirror(iter, function (mirror) {
     64   assertEquals('suspended', mirror.status())
     65   assertSourceLocation(mirror.sourceLocation(), 7, 19);
     66 });
     67 
     68 iter.next();
     69 TestGeneratorMirror(iter, function (mirror) {
     70   assertEquals('suspended', mirror.status())
     71   assertSourceLocation(mirror.sourceLocation(), 9, 2);
     72 });
     73 
     74 iter.next();
     75 TestGeneratorMirror(iter, function (mirror) {
     76   assertEquals('suspended', mirror.status())
     77   assertSourceLocation(mirror.sourceLocation(), 11, 2);
     78 });
     79 
     80 iter.next();
     81 TestGeneratorMirror(iter, function (mirror) {
     82   assertEquals('closed', mirror.status())
     83   assertEquals(undefined, mirror.sourceLocation());
     84 });
     85