mirror-debugger.js 63.5 KB
Newer Older
1
// Copyright 2006-2008 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28
// Handle id counters.
29
var next_handle_ = 0;
30 31 32
var next_transient_handle_ = -1;

// Mirror cache.
33 34
var mirror_cache_ = [];

35

36 37 38 39 40 41 42 43 44
/**
 * Clear the mirror handle cache.
 */
function ClearMirrorCache() {
  next_handle_ = 0;
  mirror_cache_ = [];
}


45 46 47 48
/**
 * Returns the mirror for a specified value or object.
 *
 * @param {value or Object} value the value or object to retreive the mirror for
49 50
 * @param {boolean} transient indicate whether this object is transient and
 *    should not be added to the mirror cache. The default is not transient.
51 52
 * @returns {Mirror} the mirror reflects the passed value or object
 */
53
function MakeMirror(value, opt_transient) {
54
  var mirror;
55 56 57 58 59 60 61 62 63 64 65 66 67

  // Look for non transient mirrors in the mirror cache.
  if (!opt_transient) {
    for (id in mirror_cache_) {
      mirror = mirror_cache_[id];
      if (mirror.value() === value) {
        return mirror;
      }
      // Special check for NaN as NaN == NaN is false.
      if (mirror.isNumber() && isNaN(mirror.value()) &&
          typeof value == 'number' && isNaN(value)) {
        return mirror;
      }
68
    }
69
  }
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  if (IS_UNDEFINED(value)) {
    mirror = new UndefinedMirror();
  } else if (IS_NULL(value)) {
    mirror = new NullMirror();
  } else if (IS_BOOLEAN(value)) {
    mirror = new BooleanMirror(value);
  } else if (IS_NUMBER(value)) {
    mirror = new NumberMirror(value);
  } else if (IS_STRING(value)) {
    mirror = new StringMirror(value);
  } else if (IS_ARRAY(value)) {
    mirror = new ArrayMirror(value);
  } else if (IS_DATE(value)) {
    mirror = new DateMirror(value);
  } else if (IS_FUNCTION(value)) {
    mirror = new FunctionMirror(value);
  } else if (IS_REGEXP(value)) {
    mirror = new RegExpMirror(value);
  } else if (IS_ERROR(value)) {
    mirror = new ErrorMirror(value);
91 92
  } else if (IS_SCRIPT(value)) {
    mirror = new ScriptMirror(value);
93
  } else {
94
    mirror = new ObjectMirror(value, OBJECT_TYPE, opt_transient);
95 96 97 98 99 100 101
  }

  mirror_cache_[mirror.handle()] = mirror;
  return mirror;
}


102 103 104 105 106 107 108 109 110 111 112
/**
 * Returns the mirror for a specified mirror handle.
 *
 * @param {number} handle the handle to find the mirror for
 * @returns {Mirror or undefiend} the mirror with the requested handle or
 *     undefined if no mirror with the requested handle was found
 */
function LookupMirror(handle) {
  return mirror_cache_[handle];
}

113

114 115 116 117 118 119 120
/**
 * Returns the mirror for the undefined value.
 *
 * @returns {Mirror} the mirror reflects the undefined value
 */
function GetUndefinedMirror() {
  return MakeMirror(void 0);
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
}


/**
 * Inherit the prototype methods from one constructor into another.
 *
 * The Function.prototype.inherits from lang.js rewritten as a standalone
 * function (not on Function.prototype). NOTE: If this file is to be loaded
 * during bootstrapping this function needs to be revritten using some native
 * functions as prototype setup using normal JavaScript does not work as
 * expected during bootstrapping (see mirror.js in r114903).
 *
 * @param {function} ctor Constructor function which needs to inherit the
 *     prototype
 * @param {function} superCtor Constructor function to inherit prototype from
 */
function inherits(ctor, superCtor) {
  var tempCtor = function(){};
  tempCtor.prototype = superCtor.prototype;
  ctor.super_ = superCtor.prototype;
  ctor.prototype = new tempCtor();
  ctor.prototype.constructor = ctor;
}


// Type names of the different mirrors.
const UNDEFINED_TYPE = 'undefined';
const NULL_TYPE = 'null';
const BOOLEAN_TYPE = 'boolean';
const NUMBER_TYPE = 'number';
const STRING_TYPE = 'string';
const OBJECT_TYPE = 'object';
const FUNCTION_TYPE = 'function';
const REGEXP_TYPE = 'regexp';
const ERROR_TYPE = 'error';
const PROPERTY_TYPE = 'property';
const FRAME_TYPE = 'frame';
const SCRIPT_TYPE = 'script';
159
const CONTEXT_TYPE = 'context';
160
const SCOPE_TYPE = 'scope';
161 162 163 164 165 166 167 168 169 170

// Maximum length when sending strings through the JSON protocol.
const kMaxProtocolStringLength = 80;

// Different kind of properties.
PropertyKind = {};
PropertyKind.Named   = 1;
PropertyKind.Indexed = 2;


171
// A copy of the PropertyType enum from global.h
172
PropertyType = {};
173 174 175 176
PropertyType.Normal                  = 0;
PropertyType.Field                   = 1;
PropertyType.ConstantFunction        = 2;
PropertyType.Callbacks               = 3;
177 178 179 180 181 182
PropertyType.Handler                 = 4;
PropertyType.Interceptor             = 5;
PropertyType.MapTransition           = 6;
PropertyType.ExternalArrayTransition = 7;
PropertyType.ConstantTransition      = 8;
PropertyType.NullDescriptor          = 9;
183

184 185 186 187 188 189 190 191 192

// Different attributes for a property.
PropertyAttribute = {};
PropertyAttribute.None       = NONE;
PropertyAttribute.ReadOnly   = READ_ONLY;
PropertyAttribute.DontEnum   = DONT_ENUM;
PropertyAttribute.DontDelete = DONT_DELETE;


193 194 195 196
// A copy of the scope types from runtime.cc.
ScopeType = { Global: 0,
              Local: 1,
              With: 2,
197
              Closure: 3,
198 199
              Catch: 4,
              Block: 5 };
200 201


202 203 204 205 206 207 208 209
// Mirror hierarchy:
//   - Mirror
//     - ValueMirror
//       - UndefinedMirror
//       - NullMirror
//       - NumberMirror
//       - StringMirror
//       - ObjectMirror
210 211 212 213 214 215
//         - FunctionMirror
//           - UnresolvedFunctionMirror
//         - ArrayMirror
//         - DateMirror
//         - RegExpMirror
//         - ErrorMirror
216 217 218 219 220 221 222 223 224 225 226 227
//     - PropertyMirror
//     - FrameMirror
//     - ScriptMirror


/**
 * Base class for all mirror objects.
 * @param {string} type The type of the mirror
 * @constructor
 */
function Mirror(type) {
  this.type_ = type;
228
}
229 230 231 232 233 234 235


Mirror.prototype.type = function() {
  return this.type_;
};


236 237 238 239 240 241
/**
 * Check whether the mirror reflects a value.
 * @returns {boolean} True if the mirror reflects a value.
 */
Mirror.prototype.isValue = function() {
  return this instanceof ValueMirror;
242
};
243 244


245 246 247 248 249 250
/**
 * Check whether the mirror reflects the undefined value.
 * @returns {boolean} True if the mirror reflects the undefined value.
 */
Mirror.prototype.isUndefined = function() {
  return this instanceof UndefinedMirror;
251
};
252 253 254 255 256 257 258 259


/**
 * Check whether the mirror reflects the null value.
 * @returns {boolean} True if the mirror reflects the null value
 */
Mirror.prototype.isNull = function() {
  return this instanceof NullMirror;
260
};
261 262 263 264 265 266 267 268


/**
 * Check whether the mirror reflects a boolean value.
 * @returns {boolean} True if the mirror reflects a boolean value
 */
Mirror.prototype.isBoolean = function() {
  return this instanceof BooleanMirror;
269
};
270 271 272 273 274 275 276 277


/**
 * Check whether the mirror reflects a number value.
 * @returns {boolean} True if the mirror reflects a number value
 */
Mirror.prototype.isNumber = function() {
  return this instanceof NumberMirror;
278
};
279 280 281 282 283 284 285 286


/**
 * Check whether the mirror reflects a string value.
 * @returns {boolean} True if the mirror reflects a string value
 */
Mirror.prototype.isString = function() {
  return this instanceof StringMirror;
287
};
288 289 290 291 292 293 294 295


/**
 * Check whether the mirror reflects an object.
 * @returns {boolean} True if the mirror reflects an object
 */
Mirror.prototype.isObject = function() {
  return this instanceof ObjectMirror;
296
};
297 298 299 300 301 302 303 304


/**
 * Check whether the mirror reflects a function.
 * @returns {boolean} True if the mirror reflects a function
 */
Mirror.prototype.isFunction = function() {
  return this instanceof FunctionMirror;
305
};
306 307 308 309 310 311 312 313


/**
 * Check whether the mirror reflects an unresolved function.
 * @returns {boolean} True if the mirror reflects an unresolved function
 */
Mirror.prototype.isUnresolvedFunction = function() {
  return this instanceof UnresolvedFunctionMirror;
314
};
315 316 317 318 319 320 321 322


/**
 * Check whether the mirror reflects an array.
 * @returns {boolean} True if the mirror reflects an array
 */
Mirror.prototype.isArray = function() {
  return this instanceof ArrayMirror;
323
};
324 325 326 327 328 329 330 331


/**
 * Check whether the mirror reflects a date.
 * @returns {boolean} True if the mirror reflects a date
 */
Mirror.prototype.isDate = function() {
  return this instanceof DateMirror;
332
};
333 334 335 336 337 338 339 340


/**
 * Check whether the mirror reflects a regular expression.
 * @returns {boolean} True if the mirror reflects a regular expression
 */
Mirror.prototype.isRegExp = function() {
  return this instanceof RegExpMirror;
341
};
342 343 344 345 346 347 348 349


/**
 * Check whether the mirror reflects an error.
 * @returns {boolean} True if the mirror reflects an error
 */
Mirror.prototype.isError = function() {
  return this instanceof ErrorMirror;
350
};
351 352 353 354 355 356 357 358


/**
 * Check whether the mirror reflects a property.
 * @returns {boolean} True if the mirror reflects a property
 */
Mirror.prototype.isProperty = function() {
  return this instanceof PropertyMirror;
359
};
360 361 362 363 364 365 366 367


/**
 * Check whether the mirror reflects a stack frame.
 * @returns {boolean} True if the mirror reflects a stack frame
 */
Mirror.prototype.isFrame = function() {
  return this instanceof FrameMirror;
368
};
369 370


371 372 373 374 375 376
/**
 * Check whether the mirror reflects a script.
 * @returns {boolean} True if the mirror reflects a script
 */
Mirror.prototype.isScript = function() {
  return this instanceof ScriptMirror;
377
};
378 379


380 381 382 383 384 385
/**
 * Check whether the mirror reflects a context.
 * @returns {boolean} True if the mirror reflects a context
 */
Mirror.prototype.isContext = function() {
  return this instanceof ContextMirror;
386
};
387 388


389 390 391 392 393 394
/**
 * Check whether the mirror reflects a scope.
 * @returns {boolean} True if the mirror reflects a scope
 */
Mirror.prototype.isScope = function() {
  return this instanceof ScopeMirror;
395
};
396 397


398 399 400 401 402
/**
 * Allocate a handle id for this object.
 */
Mirror.prototype.allocateHandle_ = function() {
  this.handle_ = next_handle_++;
403
};
404 405


406 407 408 409 410 411
/**
 * Allocate a transient handle id for this object. Transient handles are
 * negative.
 */
Mirror.prototype.allocateTransientHandle_ = function() {
  this.handle_ = next_transient_handle_--;
412
};
413 414


415 416
Mirror.prototype.toText = function() {
  // Simpel to text which is used when on specialization in subclass.
417
  return "#<" + this.constructor.name + ">";
418
};
419 420 421 422 423 424


/**
 * Base class for all value mirror objects.
 * @param {string} type The type of the mirror
 * @param {value} value The value reflected by this mirror
425 426
 * @param {boolean} transient indicate whether this object is transient with a
 *    transient handle
427 428 429
 * @constructor
 * @extends Mirror
 */
430
function ValueMirror(type, value, transient) {
431
  %_CallFunction(this, type, Mirror);
432
  this.value_ = value;
433 434 435 436 437
  if (!transient) {
    this.allocateHandle_();
  } else {
    this.allocateTransientHandle_();
  }
438
}
439 440 441
inherits(ValueMirror, Mirror);


442 443 444 445 446
Mirror.prototype.handle = function() {
  return this.handle_;
};


447 448 449 450 451 452 453 454 455 456 457 458 459 460
/**
 * Check whether this is a primitive value.
 * @return {boolean} True if the mirror reflects a primitive value
 */
ValueMirror.prototype.isPrimitive = function() {
  var type = this.type();
  return type === 'undefined' ||
         type === 'null' ||
         type === 'boolean' ||
         type === 'number' ||
         type === 'string';
};


461
/**
462 463 464 465 466 467 468 469 470 471 472 473 474 475
 * Get the actual value reflected by this mirror.
 * @return {value} The value reflected by this mirror
 */
ValueMirror.prototype.value = function() {
  return this.value_;
};


/**
 * Mirror object for Undefined.
 * @constructor
 * @extends ValueMirror
 */
function UndefinedMirror() {
476
  %_CallFunction(this, UNDEFINED_TYPE, void 0, ValueMirror);
477
}
478 479 480 481 482
inherits(UndefinedMirror, ValueMirror);


UndefinedMirror.prototype.toText = function() {
  return 'undefined';
483
};
484 485 486 487 488 489 490 491


/**
 * Mirror object for null.
 * @constructor
 * @extends ValueMirror
 */
function NullMirror() {
492
  %_CallFunction(this, NULL_TYPE, null, ValueMirror);
493
}
494 495 496 497 498
inherits(NullMirror, ValueMirror);


NullMirror.prototype.toText = function() {
  return 'null';
499
};
500 501 502 503 504 505 506 507 508


/**
 * Mirror object for boolean values.
 * @param {boolean} value The boolean value reflected by this mirror
 * @constructor
 * @extends ValueMirror
 */
function BooleanMirror(value) {
509
  %_CallFunction(this, BOOLEAN_TYPE, value, ValueMirror);
510
}
511 512 513 514 515
inherits(BooleanMirror, ValueMirror);


BooleanMirror.prototype.toText = function() {
  return this.value_ ? 'true' : 'false';
516
};
517 518 519 520 521 522 523 524 525


/**
 * Mirror object for number values.
 * @param {number} value The number value reflected by this mirror
 * @constructor
 * @extends ValueMirror
 */
function NumberMirror(value) {
526
  %_CallFunction(this, NUMBER_TYPE, value, ValueMirror);
527
}
528 529 530 531 532
inherits(NumberMirror, ValueMirror);


NumberMirror.prototype.toText = function() {
  return %NumberToString(this.value_);
533
};
534 535 536 537 538 539 540 541 542


/**
 * Mirror object for string values.
 * @param {string} value The string value reflected by this mirror
 * @constructor
 * @extends ValueMirror
 */
function StringMirror(value) {
543
  %_CallFunction(this, STRING_TYPE, value, ValueMirror);
544
}
545 546 547 548 549 550 551
inherits(StringMirror, ValueMirror);


StringMirror.prototype.length = function() {
  return this.value_.length;
};

552 553 554
StringMirror.prototype.getTruncatedValue = function(maxLength) {
  if (maxLength != -1 && this.length() > maxLength) {
    return this.value_.substring(0, maxLength) +
555 556
           '... (length: ' + this.length() + ')';
  }
557
  return this.value_;
558
};
559 560 561

StringMirror.prototype.toText = function() {
  return this.getTruncatedValue(kMaxProtocolStringLength);
562
};
563 564 565 566 567


/**
 * Mirror object for objects.
 * @param {object} value The object reflected by this mirror
568 569
 * @param {boolean} transient indicate whether this object is transient with a
 *    transient handle
570 571 572
 * @constructor
 * @extends ValueMirror
 */
573
function ObjectMirror(value, type, transient) {
574
  %_CallFunction(this, type || OBJECT_TYPE, value, transient, ValueMirror);
575
}
576 577 578 579
inherits(ObjectMirror, ValueMirror);


ObjectMirror.prototype.className = function() {
580
  return %_ClassOf(this.value_);
581 582 583 584 585 586 587 588 589 590 591 592 593 594
};


ObjectMirror.prototype.constructorFunction = function() {
  return MakeMirror(%DebugGetProperty(this.value_, 'constructor'));
};


ObjectMirror.prototype.prototypeObject = function() {
  return MakeMirror(%DebugGetProperty(this.value_, 'prototype'));
};


ObjectMirror.prototype.protoObject = function() {
595
  return MakeMirror(%DebugGetPrototype(this.value_));
596 597 598 599 600
};


ObjectMirror.prototype.hasNamedInterceptor = function() {
  // Get information on interceptors for this object.
601
  var x = %GetInterceptorInfo(this.value_);
602 603 604 605 606 607
  return (x & 2) != 0;
};


ObjectMirror.prototype.hasIndexedInterceptor = function() {
  // Get information on interceptors for this object.
608
  var x = %GetInterceptorInfo(this.value_);
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
  return (x & 1) != 0;
};


/**
 * Return the property names for this object.
 * @param {number} kind Indicate whether named, indexed or both kinds of
 *     properties are requested
 * @param {number} limit Limit the number of names returend to the specified
       value
 * @return {Array} Property names for this object
 */
ObjectMirror.prototype.propertyNames = function(kind, limit) {
  // Find kind and limit and allocate array for the result
  kind = kind || PropertyKind.Named | PropertyKind.Indexed;

  var propertyNames;
  var elementNames;
  var total = 0;
628

629
  // Find all the named properties.
630
  if (kind & PropertyKind.Named) {
631
    // Get the local property names.
632
    propertyNames = %GetLocalPropertyNames(this.value_);
633
    total += propertyNames.length;
634 635 636 637

    // Get names for named interceptor properties if any.
    if (this.hasNamedInterceptor() && (kind & PropertyKind.Named)) {
      var namedInterceptorNames =
638
          %GetNamedInterceptorPropertyNames(this.value_);
639 640 641 642 643
      if (namedInterceptorNames) {
        propertyNames = propertyNames.concat(namedInterceptorNames);
        total += namedInterceptorNames.length;
      }
    }
644
  }
645 646

  // Find all the indexed properties.
647
  if (kind & PropertyKind.Indexed) {
648
    // Get the local element names.
649
    elementNames = %GetLocalElementNames(this.value_);
650
    total += elementNames.length;
651 652 653 654

    // Get names for indexed interceptor properties.
    if (this.hasIndexedInterceptor() && (kind & PropertyKind.Indexed)) {
      var indexedInterceptorNames =
655
          %GetIndexedInterceptorElementNames(this.value_);
656 657 658 659 660
      if (indexedInterceptorNames) {
        elementNames = elementNames.concat(indexedInterceptorNames);
        total += indexedInterceptorNames.length;
      }
    }
661 662 663 664 665
  }
  limit = Math.min(limit || total, total);

  var names = new Array(limit);
  var index = 0;
666

667 668 669 670 671 672
  // Copy names for named properties.
  if (kind & PropertyKind.Named) {
    for (var i = 0; index < limit && i < propertyNames.length; i++) {
      names[index++] = propertyNames[i];
    }
  }
673

674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
  // Copy names for indexed properties.
  if (kind & PropertyKind.Indexed) {
    for (var i = 0; index < limit && i < elementNames.length; i++) {
      names[index++] = elementNames[i];
    }
  }

  return names;
};


/**
 * Return the properties for this object as an array of PropertyMirror objects.
 * @param {number} kind Indicate whether named, indexed or both kinds of
 *     properties are requested
 * @param {number} limit Limit the number of properties returend to the
       specified value
 * @return {Array} Property mirrors for this object
 */
ObjectMirror.prototype.properties = function(kind, limit) {
  var names = this.propertyNames(kind, limit);
  var properties = new Array(names.length);
  for (var i = 0; i < names.length; i++) {
    properties[i] = this.property(names[i]);
  }

  return properties;
};


ObjectMirror.prototype.property = function(name) {
705
  var details = %DebugGetPropertyDetails(this.value_, %ToString(name));
706
  if (details) {
707
    return new PropertyMirror(this, name, details);
708 709 710
  }

  // Nothing found.
711
  return GetUndefinedMirror();
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
};



/**
 * Try to find a property from its value.
 * @param {Mirror} value The property value to look for
 * @return {PropertyMirror} The property with the specified value. If no
 *     property was found with the specified value UndefinedMirror is returned
 */
ObjectMirror.prototype.lookupProperty = function(value) {
  var properties = this.properties();

  // Look for property value in properties.
  for (var i = 0; i < properties.length; i++) {

    // Skip properties which are defined through assessors.
    var property = properties[i];
    if (property.propertyType() != PropertyType.Callbacks) {
731
      if (%_ObjectEquals(property.value_, value.value_)) {
732 733 734 735 736 737
        return property;
      }
    }
  }

  // Nothing found.
738
  return GetUndefinedMirror();
739 740 741 742 743
};


/**
 * Returns objects which has direct references to this object
744 745
 * @param {number} opt_max_objects Optional parameter specifying the maximum
 *     number of referencing objects to return.
746 747
 * @return {Array} The objects which has direct references to this object.
 */
748 749 750 751
ObjectMirror.prototype.referencedBy = function(opt_max_objects) {
  // Find all objects with direct references to this object.
  var result = %DebugReferencedBy(this.value_,
                                  Mirror.prototype, opt_max_objects || 0);
752

753
  // Make mirrors for all the references found.
754 755 756
  for (var i = 0; i < result.length; i++) {
    result[i] = MakeMirror(result[i]);
  }
757

758 759 760 761 762 763 764
  return result;
};


ObjectMirror.prototype.toText = function() {
  var name;
  var ctor = this.constructorFunction();
765
  if (!ctor.isFunction()) {
766 767 768 769 770 771 772
    name = this.className();
  } else {
    name = ctor.name();
    if (!name) {
      name = this.className();
    }
  }
773
  return '#<' + name + '>';
774 775 776 777 778 779 780 781 782 783
};


/**
 * Mirror object for functions.
 * @param {function} value The function object reflected by this mirror.
 * @constructor
 * @extends ObjectMirror
 */
function FunctionMirror(value) {
784
  %_CallFunction(this, value, FUNCTION_TYPE, ObjectMirror);
785
  this.resolved_ = true;
786
}
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
inherits(FunctionMirror, ObjectMirror);


/**
 * Returns whether the function is resolved.
 * @return {boolean} True if the function is resolved. Unresolved functions can
 *     only originate as functions from stack frames
 */
FunctionMirror.prototype.resolved = function() {
  return this.resolved_;
};


/**
 * Returns the name of the function.
 * @return {string} Name of the function
 */
FunctionMirror.prototype.name = function() {
  return %FunctionGetName(this.value_);
};


809 810 811 812 813 814 815 816 817
/**
 * Returns the inferred name of the function.
 * @return {string} Name of the function
 */
FunctionMirror.prototype.inferredName = function() {
  return %FunctionGetInferredName(this.value_);
};


818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
/**
 * Returns the source code for the function.
 * @return {string or undefined} The source code for the function. If the
 *     function is not resolved undefined will be returned.
 */
FunctionMirror.prototype.source = function() {
  // Return source if function is resolved. Otherwise just fall through to
  // return undefined.
  if (this.resolved()) {
    return builtins.FunctionSourceString(this.value_);
  }
};


/**
 * Returns the script object for the function.
 * @return {ScriptMirror or undefined} Script object for the function or
 *     undefined if the function has no script
 */
FunctionMirror.prototype.script = function() {
  // Return script if function is resolved. Otherwise just fall through
  // to return undefined.
  if (this.resolved()) {
    var script = %FunctionGetScript(this.value_);
    if (script) {
843
      return MakeMirror(script);
844 845 846 847 848
    }
  }
};


849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
/**
 * Returns the script source position for the function. Only makes sense
 * for functions which has a script defined.
 * @return {Number or undefined} in-script position for the function
 */
FunctionMirror.prototype.sourcePosition_ = function() {
  // Return script if function is resolved. Otherwise just fall through
  // to return undefined.
  if (this.resolved()) {
    return %FunctionGetScriptSourcePosition(this.value_);
  }
};


/**
 * Returns the script source location object for the function. Only makes sense
 * for functions which has a script defined.
 * @return {Location or undefined} in-script location for the function begin
 */
FunctionMirror.prototype.sourceLocation = function() {
  if (this.resolved() && this.script()) {
    return this.script().locationFromPosition(this.sourcePosition_(),
                                              true);
  }
};


876 877 878 879 880 881 882 883 884 885
/**
 * Returns objects constructed by this function.
 * @param {number} opt_max_instances Optional parameter specifying the maximum
 *     number of instances to return.
 * @return {Array or undefined} The objects constructed by this function.
 */
FunctionMirror.prototype.constructedBy = function(opt_max_instances) {
  if (this.resolved()) {
    // Find all objects constructed from this function.
    var result = %DebugConstructedBy(this.value_, opt_max_instances || 0);
886

887 888 889 890
    // Make mirrors for all the instances found.
    for (var i = 0; i < result.length; i++) {
      result[i] = MakeMirror(result[i]);
    }
891

892 893 894 895 896 897 898 899 900
    return result;
  } else {
    return [];
  }
};


FunctionMirror.prototype.toText = function() {
  return this.source();
901
};
902 903 904 905 906 907 908 909 910 911 912 913


/**
 * Mirror object for unresolved functions.
 * @param {string} value The name for the unresolved function reflected by this
 *     mirror.
 * @constructor
 * @extends ObjectMirror
 */
function UnresolvedFunctionMirror(value) {
  // Construct this using the ValueMirror as an unresolved function is not a
  // real object but just a string.
914
  %_CallFunction(this, FUNCTION_TYPE, value, ValueMirror);
915 916 917
  this.propertyCount_ = 0;
  this.elementCount_ = 0;
  this.resolved_ = false;
918
}
919 920 921 922 923 924 925 926 927
inherits(UnresolvedFunctionMirror, FunctionMirror);


UnresolvedFunctionMirror.prototype.className = function() {
  return 'Function';
};


UnresolvedFunctionMirror.prototype.constructorFunction = function() {
928
  return GetUndefinedMirror();
929 930 931 932
};


UnresolvedFunctionMirror.prototype.prototypeObject = function() {
933
  return GetUndefinedMirror();
934 935 936 937
};


UnresolvedFunctionMirror.prototype.protoObject = function() {
938
  return GetUndefinedMirror();
939 940 941 942 943 944 945 946
};


UnresolvedFunctionMirror.prototype.name = function() {
  return this.value_;
};


947 948 949 950 951
UnresolvedFunctionMirror.prototype.inferredName = function() {
  return undefined;
};


952 953
UnresolvedFunctionMirror.prototype.propertyNames = function(kind, limit) {
  return [];
954
};
955 956 957 958 959 960 961 962 963


/**
 * Mirror object for arrays.
 * @param {Array} value The Array object reflected by this mirror
 * @constructor
 * @extends ObjectMirror
 */
function ArrayMirror(value) {
964
  %_CallFunction(this, value, ObjectMirror);
965
}
966 967 968 969 970 971 972 973
inherits(ArrayMirror, ObjectMirror);


ArrayMirror.prototype.length = function() {
  return this.value_.length;
};


974 975
ArrayMirror.prototype.indexedPropertiesFromRange = function(opt_from_index,
                                                            opt_to_index) {
976 977 978 979 980
  var from_index = opt_from_index || 0;
  var to_index = opt_to_index || this.length() - 1;
  if (from_index > to_index) return new Array();
  var values = new Array(to_index - from_index + 1);
  for (var i = from_index; i <= to_index; i++) {
981
    var details = %DebugGetPropertyDetails(this.value_, %ToString(i));
982 983
    var value;
    if (details) {
984
      value = new PropertyMirror(this, i, details);
985
    } else {
986
      value = GetUndefinedMirror();
987 988 989 990
    }
    values[i - from_index] = value;
  }
  return values;
991
};
992 993 994 995 996 997 998 999 1000


/**
 * Mirror object for dates.
 * @param {Date} value The Date object reflected by this mirror
 * @constructor
 * @extends ObjectMirror
 */
function DateMirror(value) {
1001
  %_CallFunction(this, value, ObjectMirror);
1002
}
1003 1004 1005 1006
inherits(DateMirror, ObjectMirror);


DateMirror.prototype.toText = function() {
1007 1008
  var s = JSON.stringify(this.value_);
  return s.substring(1, s.length - 1);  // cut quotes
1009
};
1010 1011 1012 1013 1014 1015 1016 1017 1018


/**
 * Mirror object for regular expressions.
 * @param {RegExp} value The RegExp object reflected by this mirror
 * @constructor
 * @extends ObjectMirror
 */
function RegExpMirror(value) {
1019
  %_CallFunction(this, value, REGEXP_TYPE, ObjectMirror);
1020
}
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
inherits(RegExpMirror, ObjectMirror);


/**
 * Returns the source to the regular expression.
 * @return {string or undefined} The source to the regular expression
 */
RegExpMirror.prototype.source = function() {
  return this.value_.source;
};


/**
 * Returns whether this regular expression has the global (g) flag set.
 * @return {boolean} Value of the global flag
 */
RegExpMirror.prototype.global = function() {
  return this.value_.global;
};


/**
 * Returns whether this regular expression has the ignore case (i) flag set.
 * @return {boolean} Value of the ignore case flag
 */
RegExpMirror.prototype.ignoreCase = function() {
  return this.value_.ignoreCase;
};


/**
 * Returns whether this regular expression has the multiline (m) flag set.
 * @return {boolean} Value of the multiline flag
 */
RegExpMirror.prototype.multiline = function() {
  return this.value_.multiline;
};


RegExpMirror.prototype.toText = function() {
  // Simpel to text which is used when on specialization in subclass.
  return "/" + this.source() + "/";
1063
};
1064 1065 1066 1067 1068 1069 1070 1071 1072


/**
 * Mirror object for error objects.
 * @param {Error} value The error object reflected by this mirror
 * @constructor
 * @extends ObjectMirror
 */
function ErrorMirror(value) {
1073
  %_CallFunction(this, value, ERROR_TYPE, ObjectMirror);
1074
}
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
inherits(ErrorMirror, ObjectMirror);


/**
 * Returns the message for this eror object.
 * @return {string or undefined} The message for this eror object
 */
ErrorMirror.prototype.message = function() {
  return this.value_.message;
};


ErrorMirror.prototype.toText = function() {
  // Use the same text representation as in messages.js.
  var text;
  try {
1091
    str = %_CallFunction(this.value_, builtins.ErrorToString);
1092
  } catch (e) {
1093
    str = '#<Error>';
1094 1095
  }
  return str;
1096
};
1097 1098 1099 1100 1101 1102


/**
 * Base mirror object for properties.
 * @param {ObjectMirror} mirror The mirror object having this property
 * @param {string} name The name of the property
1103
 * @param {Array} details Details about the property
1104 1105 1106
 * @constructor
 * @extends Mirror
 */
1107
function PropertyMirror(mirror, name, details) {
1108
  %_CallFunction(this, PROPERTY_TYPE, Mirror);
1109 1110
  this.mirror_ = mirror;
  this.name_ = name;
1111 1112 1113
  this.value_ = details[0];
  this.details_ = details[1];
  if (details.length > 2) {
1114
    this.exception_ = details[2];
1115 1116 1117
    this.getter_ = details[3];
    this.setter_ = details[4];
  }
1118
}
1119 1120 1121 1122 1123
inherits(PropertyMirror, Mirror);


PropertyMirror.prototype.isReadOnly = function() {
  return (this.attributes() & PropertyAttribute.ReadOnly) != 0;
1124
};
1125 1126 1127 1128


PropertyMirror.prototype.isEnum = function() {
  return (this.attributes() & PropertyAttribute.DontEnum) == 0;
1129
};
1130 1131 1132 1133


PropertyMirror.prototype.canDelete = function() {
  return (this.attributes() & PropertyAttribute.DontDelete) == 0;
1134
};
1135 1136 1137 1138


PropertyMirror.prototype.name = function() {
  return this.name_;
1139
};
1140 1141 1142 1143 1144 1145 1146 1147 1148


PropertyMirror.prototype.isIndexed = function() {
  for (var i = 0; i < this.name_.length; i++) {
    if (this.name_[i] < '0' || '9' < this.name_[i]) {
      return false;
    }
  }
  return true;
1149
};
1150 1151 1152


PropertyMirror.prototype.value = function() {
1153
  return MakeMirror(this.value_, false);
1154
};
1155 1156 1157 1158 1159 1160 1161 1162


/**
 * Returns whether this property value is an exception.
 * @return {booolean} True if this property value is an exception
 */
PropertyMirror.prototype.isException = function() {
  return this.exception_ ? true : false;
1163
};
1164 1165 1166 1167


PropertyMirror.prototype.attributes = function() {
  return %DebugPropertyAttributesFromDetails(this.details_);
1168
};
1169 1170 1171 1172


PropertyMirror.prototype.propertyType = function() {
  return %DebugPropertyTypeFromDetails(this.details_);
1173
};
1174 1175 1176 1177


PropertyMirror.prototype.insertionIndex = function() {
  return %DebugPropertyIndexFromDetails(this.details_);
1178
};
1179 1180 1181


/**
1182 1183
 * Returns whether this property has a getter defined through __defineGetter__.
 * @return {booolean} True if this property has a getter
1184
 */
1185 1186
PropertyMirror.prototype.hasGetter = function() {
  return this.getter_ ? true : false;
1187
};
1188 1189 1190


/**
1191 1192
 * Returns whether this property has a setter defined through __defineSetter__.
 * @return {booolean} True if this property has a setter
1193
 */
1194 1195
PropertyMirror.prototype.hasSetter = function() {
  return this.setter_ ? true : false;
1196
};
1197 1198 1199


/**
1200 1201 1202
 * Returns the getter for this property defined through __defineGetter__.
 * @return {Mirror} FunctionMirror reflecting the getter function or
 *     UndefinedMirror if there is no getter for this property
1203
 */
1204 1205 1206 1207
PropertyMirror.prototype.getter = function() {
  if (this.hasGetter()) {
    return MakeMirror(this.getter_);
  } else {
1208
    return GetUndefinedMirror();
1209
  }
1210
};
1211 1212 1213


/**
1214 1215 1216
 * Returns the setter for this property defined through __defineSetter__.
 * @return {Mirror} FunctionMirror reflecting the setter function or
 *     UndefinedMirror if there is no setter for this property
1217
 */
1218 1219 1220 1221
PropertyMirror.prototype.setter = function() {
  if (this.hasSetter()) {
    return MakeMirror(this.setter_);
  } else {
1222
    return GetUndefinedMirror();
1223
  }
1224
};
1225 1226 1227


/**
1228 1229
 * Returns whether this property is natively implemented by the host or a set
 * through JavaScript code.
1230
 * @return {boolean} True if the property is
1231
 *     UndefinedMirror if there is no setter for this property
1232
 */
1233 1234 1235 1236
PropertyMirror.prototype.isNative = function() {
  return (this.propertyType() == PropertyType.Interceptor) ||
         ((this.propertyType() == PropertyType.Callbacks) &&
          !this.hasGetter() && !this.hasSetter());
1237
};
1238 1239 1240 1241 1242 1243 1244 1245 1246


const kFrameDetailsFrameIdIndex = 0;
const kFrameDetailsReceiverIndex = 1;
const kFrameDetailsFunctionIndex = 2;
const kFrameDetailsArgumentCountIndex = 3;
const kFrameDetailsLocalCountIndex = 4;
const kFrameDetailsSourcePositionIndex = 5;
const kFrameDetailsConstructCallIndex = 6;
1247
const kFrameDetailsAtReturnIndex = 7;
1248
const kFrameDetailsFlagsIndex = 8;
1249
const kFrameDetailsFirstDynamicIndex = 9;
1250 1251 1252 1253 1254

const kFrameDetailsNameIndex = 0;
const kFrameDetailsValueIndex = 1;
const kFrameDetailsNameValueSize = 2;

1255 1256 1257
const kFrameDetailsFlagDebuggerFrameMask = 1 << 0;
const kFrameDetailsFlagOptimizedFrameMask = 1 << 1;
const kFrameDetailsFlagInlinedFrameIndexMask = 7 << 2;
1258

1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
/**
 * Wrapper for the frame details information retreived from the VM. The frame
 * details from the VM is an array with the following content. See runtime.cc
 * Runtime_GetFrameDetails.
 *     0: Id
 *     1: Receiver
 *     2: Function
 *     3: Argument count
 *     4: Local count
 *     5: Source position
 *     6: Construct call
1270
 *     7: Is at return
1271
 *     8: Flags (debugger frame, optimized frame, inlined frame index)
1272 1273
 *     Arguments name, value
 *     Locals name, value
1274
 *     Return value if any
1275 1276 1277 1278 1279 1280 1281
 * @param {number} break_id Current break id
 * @param {number} index Frame number
 * @constructor
 */
function FrameDetails(break_id, index) {
  this.break_id_ = break_id;
  this.details_ = %GetFrameDetails(break_id, index);
1282
}
1283 1284 1285 1286 1287


FrameDetails.prototype.frameId = function() {
  %CheckExecutionState(this.break_id_);
  return this.details_[kFrameDetailsFrameIdIndex];
1288
};
1289 1290 1291 1292 1293


FrameDetails.prototype.receiver = function() {
  %CheckExecutionState(this.break_id_);
  return this.details_[kFrameDetailsReceiverIndex];
1294
};
1295 1296 1297 1298 1299


FrameDetails.prototype.func = function() {
  %CheckExecutionState(this.break_id_);
  return this.details_[kFrameDetailsFunctionIndex];
1300
};
1301 1302 1303 1304 1305


FrameDetails.prototype.isConstructCall = function() {
  %CheckExecutionState(this.break_id_);
  return this.details_[kFrameDetailsConstructCallIndex];
1306
};
1307 1308


1309 1310 1311
FrameDetails.prototype.isAtReturn = function() {
  %CheckExecutionState(this.break_id_);
  return this.details_[kFrameDetailsAtReturnIndex];
1312
};
1313 1314


1315 1316
FrameDetails.prototype.isDebuggerFrame = function() {
  %CheckExecutionState(this.break_id_);
1317
  var f = kFrameDetailsFlagDebuggerFrameMask;
1318
  return (this.details_[kFrameDetailsFlagsIndex] & f) == f;
1319
};
1320 1321 1322 1323


FrameDetails.prototype.isOptimizedFrame = function() {
  %CheckExecutionState(this.break_id_);
1324
  var f = kFrameDetailsFlagOptimizedFrameMask;
1325
  return (this.details_[kFrameDetailsFlagsIndex] & f) == f;
1326
};
1327 1328 1329


FrameDetails.prototype.isInlinedFrame = function() {
1330
  return this.inlinedFrameIndex() > 0;
1331
};
1332 1333 1334


FrameDetails.prototype.inlinedFrameIndex = function() {
1335
  %CheckExecutionState(this.break_id_);
1336
  var f = kFrameDetailsFlagInlinedFrameIndexMask;
1337 1338
  return (this.details_[kFrameDetailsFlagsIndex] & f) >> 2;
};
1339 1340 1341 1342 1343


FrameDetails.prototype.argumentCount = function() {
  %CheckExecutionState(this.break_id_);
  return this.details_[kFrameDetailsArgumentCountIndex];
1344
};
1345 1346 1347 1348 1349 1350 1351


FrameDetails.prototype.argumentName = function(index) {
  %CheckExecutionState(this.break_id_);
  if (index >= 0 && index < this.argumentCount()) {
    return this.details_[kFrameDetailsFirstDynamicIndex +
                         index * kFrameDetailsNameValueSize +
1352
                         kFrameDetailsNameIndex];
1353
  }
1354
};
1355 1356 1357 1358 1359 1360 1361


FrameDetails.prototype.argumentValue = function(index) {
  %CheckExecutionState(this.break_id_);
  if (index >= 0 && index < this.argumentCount()) {
    return this.details_[kFrameDetailsFirstDynamicIndex +
                         index * kFrameDetailsNameValueSize +
1362
                         kFrameDetailsValueIndex];
1363
  }
1364
};
1365 1366 1367 1368 1369


FrameDetails.prototype.localCount = function() {
  %CheckExecutionState(this.break_id_);
  return this.details_[kFrameDetailsLocalCountIndex];
1370
};
1371 1372 1373 1374 1375


FrameDetails.prototype.sourcePosition = function() {
  %CheckExecutionState(this.break_id_);
  return this.details_[kFrameDetailsSourcePositionIndex];
1376
};
1377 1378 1379 1380 1381


FrameDetails.prototype.localName = function(index) {
  %CheckExecutionState(this.break_id_);
  if (index >= 0 && index < this.localCount()) {
1382
    var locals_offset = kFrameDetailsFirstDynamicIndex +
1383
                        this.argumentCount() * kFrameDetailsNameValueSize;
1384 1385
    return this.details_[locals_offset +
                         index * kFrameDetailsNameValueSize +
1386
                         kFrameDetailsNameIndex];
1387
  }
1388
};
1389 1390 1391 1392 1393


FrameDetails.prototype.localValue = function(index) {
  %CheckExecutionState(this.break_id_);
  if (index >= 0 && index < this.localCount()) {
1394
    var locals_offset = kFrameDetailsFirstDynamicIndex +
1395
                        this.argumentCount() * kFrameDetailsNameValueSize;
1396 1397
    return this.details_[locals_offset +
                         index * kFrameDetailsNameValueSize +
1398
                         kFrameDetailsValueIndex];
1399
  }
1400
};
1401 1402


1403 1404 1405 1406 1407 1408 1409 1410
FrameDetails.prototype.returnValue = function() {
  %CheckExecutionState(this.break_id_);
  var return_value_offset =
      kFrameDetailsFirstDynamicIndex +
      (this.argumentCount() + this.localCount()) * kFrameDetailsNameValueSize;
  if (this.details_[kFrameDetailsAtReturnIndex]) {
    return this.details_[return_value_offset];
  }
1411
};
1412 1413


1414 1415
FrameDetails.prototype.scopeCount = function() {
  return %GetScopeCount(this.break_id_, this.frameId());
1416
};
1417 1418


1419 1420 1421 1422 1423 1424 1425 1426 1427
/**
 * Mirror object for stack frames.
 * @param {number} break_id The break id in the VM for which this frame is
       valid
 * @param {number} index The frame index (top frame is index 0)
 * @constructor
 * @extends Mirror
 */
function FrameMirror(break_id, index) {
1428
  %_CallFunction(this, FRAME_TYPE, Mirror);
1429 1430 1431
  this.break_id_ = break_id;
  this.index_ = index;
  this.details_ = new FrameDetails(break_id, index);
1432
}
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
inherits(FrameMirror, Mirror);


FrameMirror.prototype.index = function() {
  return this.index_;
};


FrameMirror.prototype.func = function() {
  // Get the function for this frame from the VM.
  var f = this.details_.func();
1444

1445 1446 1447 1448
  // Create a function mirror. NOTE: MakeMirror cannot be used here as the
  // value returned from the VM might be a string if the function for the
  // frame is unresolved.
  if (IS_FUNCTION(f)) {
1449
    return MakeMirror(f);
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
  } else {
    return new UnresolvedFunctionMirror(f);
  }
};


FrameMirror.prototype.receiver = function() {
  return MakeMirror(this.details_.receiver());
};


FrameMirror.prototype.isConstructCall = function() {
  return this.details_.isConstructCall();
};


1466 1467 1468 1469 1470
FrameMirror.prototype.isAtReturn = function() {
  return this.details_.isAtReturn();
};


1471 1472 1473 1474 1475
FrameMirror.prototype.isDebuggerFrame = function() {
  return this.details_.isDebuggerFrame();
};


1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
FrameMirror.prototype.isOptimizedFrame = function() {
  return this.details_.isOptimizedFrame();
};


FrameMirror.prototype.isInlinedFrame = function() {
  return this.details_.isInlinedFrame();
};


1486 1487 1488 1489 1490
FrameMirror.prototype.inlinedFrameIndex = function() {
  return this.details_.inlinedFrameIndex();
};


1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
FrameMirror.prototype.argumentCount = function() {
  return this.details_.argumentCount();
};


FrameMirror.prototype.argumentName = function(index) {
  return this.details_.argumentName(index);
};


FrameMirror.prototype.argumentValue = function(index) {
  return MakeMirror(this.details_.argumentValue(index));
};


FrameMirror.prototype.localCount = function() {
  return this.details_.localCount();
};


FrameMirror.prototype.localName = function(index) {
  return this.details_.localName(index);
};


FrameMirror.prototype.localValue = function(index) {
  return MakeMirror(this.details_.localValue(index));
};


1521 1522 1523 1524 1525
FrameMirror.prototype.returnValue = function() {
  return MakeMirror(this.details_.returnValue());
};


1526 1527 1528 1529 1530 1531 1532
FrameMirror.prototype.sourcePosition = function() {
  return this.details_.sourcePosition();
};


FrameMirror.prototype.sourceLocation = function() {
  if (this.func().resolved() && this.func().script()) {
1533 1534
    return this.func().script().locationFromPosition(this.sourcePosition(),
                                                     true);
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
  }
};


FrameMirror.prototype.sourceLine = function() {
  if (this.func().resolved()) {
    var location = this.sourceLocation();
    if (location) {
      return location.line;
    }
  }
};


FrameMirror.prototype.sourceColumn = function() {
  if (this.func().resolved()) {
    var location = this.sourceLocation();
    if (location) {
      return location.column;
    }
  }
};


FrameMirror.prototype.sourceLineText = function() {
  if (this.func().resolved()) {
    var location = this.sourceLocation();
    if (location) {
      return location.sourceText();
    }
  }
};


1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
FrameMirror.prototype.scopeCount = function() {
  return this.details_.scopeCount();
};


FrameMirror.prototype.scope = function(index) {
  return new ScopeMirror(this, index);
};


1579 1580
FrameMirror.prototype.evaluate = function(source, disable_break,
                                          opt_context_object) {
1581 1582 1583 1584 1585 1586
  var result = %DebugEvaluate(this.break_id_,
                              this.details_.frameId(),
                              this.details_.inlinedFrameIndex(),
                              source,
                              Boolean(disable_break),
                              opt_context_object);
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
  return MakeMirror(result);
};


FrameMirror.prototype.invocationText = function() {
  // Format frame invoaction (receiver, function and arguments).
  var result = '';
  var func = this.func();
  var receiver = this.receiver();
  if (this.isConstructCall()) {
    // For constructor frames display new followed by the function name.
    result += 'new ';
    result += func.name() ? func.name() : '[anonymous]';
1600
  } else if (this.isDebuggerFrame()) {
1601 1602 1603
    result += '[debugger]';
  } else {
    // If the receiver has a className which is 'global' don't display it.
1604 1605
    var display_receiver =
      !receiver.className || (receiver.className() != 'global');
1606 1607 1608 1609 1610
    if (display_receiver) {
      result += receiver.toText();
    }
    // Try to find the function as a property in the receiver. Include the
    // prototype chain in the lookup.
1611
    var property = GetUndefinedMirror();
1612 1613 1614 1615
    if (receiver.isObject()) {
      for (var r = receiver;
           !r.isNull() && property.isUndefined();
           r = r.protoObject()) {
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
        property = r.lookupProperty(func);
      }
    }
    if (!property.isUndefined()) {
      // The function invoked was found on the receiver. Use the property name
      // for the backtrace.
      if (!property.isIndexed()) {
        if (display_receiver) {
          result += '.';
        }
        result += property.name();
      } else {
        result += '[';
        result += property.name();
        result += ']';
      }
      // Also known as - if the name in the function doesn't match the name
      // under which it was looked up.
      if (func.name() && func.name() != property.name()) {
        result += '(aka ' + func.name() + ')';
1636
      }
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
    } else {
      // The function invoked was not found on the receiver. Use the function
      // name if available for the backtrace.
      if (display_receiver) {
        result += '.';
      }
      result += func.name() ? func.name() : '[anonymous]';
    }
  }

  // Render arguments for normal frames.
  if (!this.isDebuggerFrame()) {
    result += '(';
    for (var i = 0; i < this.argumentCount(); i++) {
      if (i != 0) result += ', ';
      if (this.argumentName(i)) {
        result += this.argumentName(i);
        result += '=';
      }
      result += this.argumentValue(i).toText();
    }
    result += ')';
  }
1660

1661 1662 1663 1664
  if (this.isAtReturn()) {
    result += ' returning ';
    result += this.returnValue().toText();
  }
1665

1666
  return result;
1667
};
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698


FrameMirror.prototype.sourceAndPositionText = function() {
  // Format source and position.
  var result = '';
  var func = this.func();
  if (func.resolved()) {
    if (func.script()) {
      if (func.script().name()) {
        result += func.script().name();
      } else {
        result += '[unnamed]';
      }
      if (!this.isDebuggerFrame()) {
        var location = this.sourceLocation();
        result += ' line ';
        result += !IS_UNDEFINED(location) ? (location.line + 1) : '?';
        result += ' column ';
        result += !IS_UNDEFINED(location) ? (location.column + 1) : '?';
        if (!IS_UNDEFINED(this.sourcePosition())) {
          result += ' (position ' + (this.sourcePosition() + 1) + ')';
        }
      }
    } else {
      result += '[no source]';
    }
  } else {
    result += '[unresolved]';
  }

  return result;
1699
};
1700 1701 1702 1703 1704


FrameMirror.prototype.localsText = function() {
  // Format local variables.
  var result = '';
1705
  var locals_count = this.localCount();
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
  if (locals_count > 0) {
    for (var i = 0; i < locals_count; ++i) {
      result += '      var ';
      result += this.localName(i);
      result += ' = ';
      result += this.localValue(i).toText();
      if (i < locals_count - 1) result += '\n';
    }
  }

  return result;
1717
};
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731


FrameMirror.prototype.toText = function(opt_locals) {
  var result = '';
  result += '#' + (this.index() <= 9 ? '0' : '') + this.index();
  result += ' ';
  result += this.invocationText();
  result += ' ';
  result += this.sourceAndPositionText();
  if (opt_locals) {
    result += '\n';
    result += this.localsText();
  }
  return result;
1732
};
1733 1734


1735 1736 1737 1738 1739 1740 1741
const kScopeDetailsTypeIndex = 0;
const kScopeDetailsObjectIndex = 1;

function ScopeDetails(frame, index) {
  this.break_id_ = frame.break_id_;
  this.details_ = %GetScopeDetails(frame.break_id_,
                                   frame.details_.frameId(),
1742
                                   frame.details_.inlinedFrameIndex(),
1743 1744 1745 1746 1747 1748 1749
                                   index);
}


ScopeDetails.prototype.type = function() {
  %CheckExecutionState(this.break_id_);
  return this.details_[kScopeDetailsTypeIndex];
1750
};
1751 1752 1753 1754 1755


ScopeDetails.prototype.object = function() {
  %CheckExecutionState(this.break_id_);
  return this.details_[kScopeDetailsObjectIndex];
1756
};
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766


/**
 * Mirror object for scope.
 * @param {FrameMirror} frame The frame this scope is a part of
 * @param {number} index The scope index in the frame
 * @constructor
 * @extends Mirror
 */
function ScopeMirror(frame, index) {
1767
  %_CallFunction(this, SCOPE_TYPE, Mirror);
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
  this.frame_index_ = frame.index_;
  this.scope_index_ = index;
  this.details_ = new ScopeDetails(frame, index);
}
inherits(ScopeMirror, Mirror);


ScopeMirror.prototype.frameIndex = function() {
  return this.frame_index_;
};


ScopeMirror.prototype.scopeIndex = function() {
  return this.scope_index_;
};


ScopeMirror.prototype.scopeType = function() {
  return this.details_.type();
};


ScopeMirror.prototype.scopeObject = function() {
  // For local and closure scopes create a transient mirror as these objects are
  // created on the fly materializing the local or closure scopes and
  // therefore will not preserve identity.
  var transient = this.scopeType() == ScopeType.Local ||
                  this.scopeType() == ScopeType.Closure;
  return MakeMirror(this.details_.object(), transient);
};


1800 1801 1802 1803 1804 1805 1806
/**
 * Mirror object for script source.
 * @param {Script} script The script object
 * @constructor
 * @extends Mirror
 */
function ScriptMirror(script) {
1807
  %_CallFunction(this, SCRIPT_TYPE, Mirror);
1808
  this.script_ = script;
1809
  this.context_ = new ContextMirror(script.context_data);
1810
  this.allocateHandle_();
1811
}
1812 1813 1814
inherits(ScriptMirror, Mirror);


1815 1816 1817 1818 1819
ScriptMirror.prototype.value = function() {
  return this.script_;
};


1820
ScriptMirror.prototype.name = function() {
1821
  return this.script_.name || this.script_.nameOrSourceURL();
1822 1823 1824
};


1825 1826 1827 1828 1829
ScriptMirror.prototype.id = function() {
  return this.script_.id;
};


1830 1831 1832 1833 1834
ScriptMirror.prototype.source = function() {
  return this.script_.source;
};


1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
ScriptMirror.prototype.lineOffset = function() {
  return this.script_.line_offset;
};


ScriptMirror.prototype.columnOffset = function() {
  return this.script_.column_offset;
};


1845 1846 1847 1848 1849
ScriptMirror.prototype.data = function() {
  return this.script_.data;
};


1850 1851 1852 1853 1854
ScriptMirror.prototype.scriptType = function() {
  return this.script_.type;
};


1855 1856 1857 1858 1859
ScriptMirror.prototype.compilationType = function() {
  return this.script_.compilation_type;
};


1860 1861 1862 1863 1864
ScriptMirror.prototype.lineCount = function() {
  return this.script_.lineCount();
};


1865 1866 1867
ScriptMirror.prototype.locationFromPosition = function(
    position, include_resource_offset) {
  return this.script_.locationFromPosition(position, include_resource_offset);
1868
};
1869 1870 1871 1872


ScriptMirror.prototype.sourceSlice = function (opt_from_line, opt_to_line) {
  return this.script_.sourceSlice(opt_from_line, opt_to_line);
1873
};
1874 1875


1876 1877 1878 1879 1880
ScriptMirror.prototype.context = function() {
  return this.context_;
};


1881 1882 1883 1884 1885 1886 1887
ScriptMirror.prototype.evalFromScript = function() {
  return MakeMirror(this.script_.eval_from_script);
};


ScriptMirror.prototype.evalFromFunctionName = function() {
  return MakeMirror(this.script_.eval_from_function_name);
1888 1889 1890 1891
};


ScriptMirror.prototype.evalFromLocation = function() {
1892 1893 1894 1895
  var eval_from_script = this.evalFromScript();
  if (!eval_from_script.isUndefined()) {
    var position = this.script_.eval_from_script_position;
    return eval_from_script.locationFromPosition(position, true);
1896 1897 1898 1899
  }
};


1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
ScriptMirror.prototype.toText = function() {
  var result = '';
  result += this.name();
  result += ' (lines: ';
  if (this.lineOffset() > 0) {
    result += this.lineOffset();
    result += '-';
    result += this.lineOffset() + this.lineCount() - 1;
  } else {
    result += this.lineCount();
  }
  result += ')';
  return result;
1913
};
1914 1915


1916 1917 1918 1919 1920 1921 1922
/**
 * Mirror object for context.
 * @param {Object} data The context data
 * @constructor
 * @extends Mirror
 */
function ContextMirror(data) {
1923
  %_CallFunction(this, CONTEXT_TYPE, Mirror);
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
  this.data_ = data;
  this.allocateHandle_();
}
inherits(ContextMirror, Mirror);


ContextMirror.prototype.data = function() {
  return this.data_;
};


1935 1936 1937 1938
/**
 * Returns a mirror serializer
 *
 * @param {boolean} details Set to true to include details
1939 1940 1941
 * @param {Object} options Options comtrolling the serialization
 *     The following options can be set:
 *       includeSource: include ths full source of scripts
1942 1943
 * @returns {MirrorSerializer} mirror serializer
 */
1944 1945
function MakeMirrorSerializer(details, options) {
  return new JSONProtocolSerializer(details, options);
1946 1947 1948 1949 1950 1951 1952 1953 1954
}


/**
 * Object for serializing a mirror objects and its direct references.
 * @param {boolean} details Indicates whether to include details for the mirror
 *     serialized
 * @constructor
 */
1955
function JSONProtocolSerializer(details, options) {
1956
  this.details_ = details;
1957
  this.options_ = options;
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
  this.mirrors_ = [ ];
}


/**
 * Returns a serialization of an object reference. The referenced object are
 * added to the serialization state.
 *
 * @param {Mirror} mirror The mirror to serialize
 * @returns {String} JSON serialization
 */
JSONProtocolSerializer.prototype.serializeReference = function(mirror) {
  return this.serialize_(mirror, true, true);
1971
};
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983


/**
 * Returns a serialization of an object value. The referenced objects are
 * added to the serialization state.
 *
 * @param {Mirror} mirror The mirror to serialize
 * @returns {String} JSON serialization
 */
JSONProtocolSerializer.prototype.serializeValue = function(mirror) {
  var json = this.serialize_(mirror, false, true);
  return json;
1984
};
1985 1986 1987 1988 1989


/**
 * Returns a serialization of all the objects referenced.
 *
1990 1991 1992
 * @param {Mirror} mirror The mirror to serialize.
 * @returns {Array.<Object>} Array of the referenced objects converted to
 *     protcol objects.
1993 1994
 */
JSONProtocolSerializer.prototype.serializeReferencedObjects = function() {
1995 1996
  // Collect the protocol representation of the referenced objects in an array.
  var content = [];
1997

1998 1999
  // Get the number of referenced objects.
  var count = this.mirrors_.length;
2000

2001 2002 2003 2004
  for (var i = 0; i < count; i++) {
    content.push(this.serialize_(this.mirrors_[i], false, false));
  }

2005
  return content;
2006
};
2007 2008


2009 2010
JSONProtocolSerializer.prototype.includeSource_ = function() {
  return this.options_ && this.options_.includeSource;
2011
};
2012 2013


2014 2015
JSONProtocolSerializer.prototype.inlineRefs_ = function() {
  return this.options_ && this.options_.inlineRefs;
2016
};
2017 2018


2019 2020 2021 2022 2023 2024
JSONProtocolSerializer.prototype.maxStringLength_ = function() {
  if (IS_UNDEFINED(this.options_) ||
      IS_UNDEFINED(this.options_.maxStringLength)) {
    return kMaxProtocolStringLength;
  }
  return this.options_.maxStringLength;
2025
};
2026 2027


2028 2029 2030 2031 2032 2033 2034
JSONProtocolSerializer.prototype.add_ = function(mirror) {
  // If this mirror is already in the list just return.
  for (var i = 0; i < this.mirrors_.length; i++) {
    if (this.mirrors_[i] === mirror) {
      return;
    }
  }
2035

2036 2037
  // Add the mirror to the list of mirrors to be serialized.
  this.mirrors_.push(mirror);
2038
};
2039 2040


2041 2042 2043 2044 2045 2046
/**
 * Formats mirror object to protocol reference object with some data that can
 * be used to display the value in debugger.
 * @param {Mirror} mirror Mirror to serialize.
 * @return {Object} Protocol reference object.
 */
2047
JSONProtocolSerializer.prototype.serializeReferenceWithDisplayData_ =
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
    function(mirror) {
  var o = {};
  o.ref = mirror.handle();
  o.type = mirror.type();
  switch (mirror.type()) {
    case UNDEFINED_TYPE:
    case NULL_TYPE:
    case BOOLEAN_TYPE:
    case NUMBER_TYPE:
      o.value = mirror.value();
      break;
    case STRING_TYPE:
2060
      o.value = mirror.getTruncatedValue(this.maxStringLength_());
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
      break;
    case FUNCTION_TYPE:
      o.name = mirror.name();
      o.inferredName = mirror.inferredName();
      if (mirror.script()) {
        o.scriptId = mirror.script().id();
      }
      break;
    case ERROR_TYPE:
    case REGEXP_TYPE:
      o.value = mirror.toText();
      break;
    case OBJECT_TYPE:
      o.className = mirror.className();
      break;
  }
  return o;
};

2080

2081 2082
JSONProtocolSerializer.prototype.serialize_ = function(mirror, reference,
                                                       details) {
2083 2084 2085
  // If serializing a reference to a mirror just return the reference and add
  // the mirror to the referenced mirrors.
  if (reference &&
2086
      (mirror.isValue() || mirror.isScript() || mirror.isContext())) {
2087
    if (this.inlineRefs_() && mirror.isValue()) {
2088 2089 2090 2091 2092
      return this.serializeReferenceWithDisplayData_(mirror);
    } else {
      this.add_(mirror);
      return {'ref' : mirror.handle()};
    }
2093
  }
2094

2095 2096
  // Collect the JSON property/value pairs.
  var content = {};
2097

2098
  // Add the mirror handle.
2099
  if (mirror.isValue() || mirror.isScript() || mirror.isContext()) {
2100
    content.handle = mirror.handle();
2101 2102 2103
  }

  // Always add the type.
2104
  content.type = mirror.type();
2105 2106 2107 2108 2109 2110 2111 2112 2113

  switch (mirror.type()) {
    case UNDEFINED_TYPE:
    case NULL_TYPE:
      // Undefined and null are represented just by their type.
      break;

    case BOOLEAN_TYPE:
      // Boolean values are simply represented by their value.
2114
      content.value = mirror.value();
2115 2116 2117 2118
      break;

    case NUMBER_TYPE:
      // Number values are simply represented by their value.
2119
      content.value = NumberToJSON_(mirror.value());
2120 2121 2122 2123
      break;

    case STRING_TYPE:
      // String values might have their value cropped to keep down size.
2124 2125 2126
      if (this.maxStringLength_() != -1 &&
          mirror.length() > this.maxStringLength_()) {
        var str = mirror.getTruncatedValue(this.maxStringLength_());
2127 2128
        content.value = str;
        content.fromIndex = 0;
2129
        content.toIndex = this.maxStringLength_();
2130
      } else {
2131
        content.value = mirror.value();
2132
      }
2133
      content.length = mirror.length();
2134 2135 2136 2137 2138 2139 2140
      break;

    case OBJECT_TYPE:
    case FUNCTION_TYPE:
    case ERROR_TYPE:
    case REGEXP_TYPE:
      // Add object representation.
2141
      this.serializeObject_(mirror, content, details);
2142 2143 2144
      break;

    case PROPERTY_TYPE:
2145
      throw new Error('PropertyMirror cannot be serialized independeltly');
2146 2147 2148 2149 2150 2151 2152
      break;

    case FRAME_TYPE:
      // Add object representation.
      this.serializeFrame_(mirror, content);
      break;

2153 2154 2155 2156 2157
    case SCOPE_TYPE:
      // Add object representation.
      this.serializeScope_(mirror, content);
      break;

2158
    case SCRIPT_TYPE:
2159
      // Script is represented by id, name and source attributes.
2160
      if (mirror.name()) {
2161
        content.name = mirror.name();
2162
      }
2163 2164 2165 2166
      content.id = mirror.id();
      content.lineOffset = mirror.lineOffset();
      content.columnOffset = mirror.columnOffset();
      content.lineCount = mirror.lineCount();
2167
      if (mirror.data()) {
2168
        content.data = mirror.data();
2169 2170
      }
      if (this.includeSource_()) {
2171
        content.source = mirror.source();
2172 2173
      } else {
        var sourceStart = mirror.source().substring(0, 80);
2174
        content.sourceStart = sourceStart;
2175
      }
2176 2177
      content.sourceLength = mirror.source().length;
      content.scriptType = mirror.scriptType();
2178
      content.compilationType = mirror.compilationType();
2179 2180 2181
      // For compilation type eval emit information on the script from which
      // eval was called if a script is present.
      if (mirror.compilationType() == 1 &&
2182
          mirror.evalFromScript()) {
2183
        content.evalFromScript =
2184
            this.serializeReference(mirror.evalFromScript());
2185
        var evalFromLocation = mirror.evalFromLocation();
2186 2187 2188 2189
        if (evalFromLocation) {
          content.evalFromLocation = { line: evalFromLocation.line,
                                       column: evalFromLocation.column };
        }
2190 2191 2192
        if (mirror.evalFromFunctionName()) {
          content.evalFromFunctionName = mirror.evalFromFunctionName();
        }
2193
      }
2194
      if (mirror.context()) {
2195
        content.context = this.serializeReference(mirror.context());
2196 2197 2198 2199
      }
      break;

    case CONTEXT_TYPE:
2200
      content.data = mirror.data();
2201 2202 2203 2204
      break;
  }

  // Always add the text representation.
2205
  content.text = mirror.toText();
2206

2207
  // Create and return the JSON string.
2208
  return content;
2209
};
2210 2211


2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
/**
 * Serialize object information to the following JSON format.
 *
 *   {"className":"<class name>",
 *    "constructorFunction":{"ref":<number>},
 *    "protoObject":{"ref":<number>},
 *    "prototypeObject":{"ref":<number>},
 *    "namedInterceptor":<boolean>,
 *    "indexedInterceptor":<boolean>,
 *    "properties":[<properties>]}
 */
JSONProtocolSerializer.prototype.serializeObject_ = function(mirror, content,
                                                             details) {
  // Add general object properties.
2226 2227 2228 2229 2230
  content.className = mirror.className();
  content.constructorFunction =
      this.serializeReference(mirror.constructorFunction());
  content.protoObject = this.serializeReference(mirror.protoObject());
  content.prototypeObject = this.serializeReference(mirror.prototypeObject());
2231 2232

  // Add flags to indicate whether there are interceptors.
2233
  if (mirror.hasNamedInterceptor()) {
2234
    content.namedInterceptor = true;
2235 2236
  }
  if (mirror.hasIndexedInterceptor()) {
2237
    content.indexedInterceptor = true;
2238
  }
2239

2240
  // Add function specific properties.
2241 2242
  if (mirror.isFunction()) {
    // Add function specific properties.
2243
    content.name = mirror.name();
2244
    if (!IS_UNDEFINED(mirror.inferredName())) {
2245
      content.inferredName = mirror.inferredName();
2246
    }
2247
    content.resolved = mirror.resolved();
2248
    if (mirror.resolved()) {
2249
      content.source = mirror.source();
2250 2251
    }
    if (mirror.script()) {
2252
      content.script = this.serializeReference(mirror.script());
2253
      content.scriptId = mirror.script().id();
2254

2255
      serializeLocationFields(mirror.sourceLocation(), content);
2256
    }
2257 2258 2259 2260
  }

  // Add date specific properties.
  if (mirror.isDate()) {
2261
    // Add date specific properties.
2262
    content.value = mirror.value();
2263
  }
2264 2265 2266 2267 2268 2269

  // Add actual properties - named properties followed by indexed properties.
  var propertyNames = mirror.propertyNames(PropertyKind.Named);
  var propertyIndexes = mirror.propertyNames(PropertyKind.Indexed);
  var p = new Array(propertyNames.length + propertyIndexes.length);
  for (var i = 0; i < propertyNames.length; i++) {
2270 2271
    var propertyMirror = mirror.property(propertyNames[i]);
    p[i] = this.serializeProperty_(propertyMirror);
2272
    if (details) {
2273
      this.add_(propertyMirror.value());
2274 2275 2276
    }
  }
  for (var i = 0; i < propertyIndexes.length; i++) {
2277 2278
    var propertyMirror = mirror.property(propertyIndexes[i]);
    p[propertyNames.length + i] = this.serializeProperty_(propertyMirror);
2279
    if (details) {
2280
      this.add_(propertyMirror.value());
2281 2282
    }
  }
2283
  content.properties = p;
2284
};
2285 2286


2287 2288 2289 2290 2291 2292
/**
 * Serialize location information to the following JSON format:
 *
 *   "position":"<position>",
 *   "line":"<line>",
 *   "column":"<column>",
2293
 *
2294 2295 2296 2297 2298
 * @param {SourceLocation} location The location to serialize, may be undefined.
 */
function serializeLocationFields (location, content) {
  if (!location) {
    return;
2299
  }
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311
  content.position = location.position;
  var line = location.line;
  if (!IS_UNDEFINED(line)) {
    content.line = line;
  }
  var column = location.column;
  if (!IS_UNDEFINED(column)) {
    content.column = column;
  }
}


2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327
/**
 * Serialize property information to the following JSON format for building the
 * array of properties.
 *
 *   {"name":"<property name>",
 *    "attributes":<number>,
 *    "propertyType":<number>,
 *    "ref":<number>}
 *
 * If the attribute for the property is PropertyAttribute.None it is not added.
 * If the propertyType for the property is PropertyType.Normal it is not added.
 * Here are a couple of examples.
 *
 *   {"name":"hello","ref":1}
 *   {"name":"length","attributes":7,"propertyType":3,"ref":2}
 *
2328 2329
 * @param {PropertyMirror} propertyMirror The property to serialize.
 * @returns {Object} Protocol object representing the property.
2330
 */
2331 2332
JSONProtocolSerializer.prototype.serializeProperty_ = function(propertyMirror) {
  var result = {};
2333

2334
  result.name = propertyMirror.name();
2335
  var propertyValue = propertyMirror.value();
2336
  if (this.inlineRefs_() && propertyValue.isValue()) {
2337 2338 2339 2340 2341 2342 2343 2344 2345
    result.value = this.serializeReferenceWithDisplayData_(propertyValue);
  } else {
    if (propertyMirror.attributes() != PropertyAttribute.None) {
      result.attributes = propertyMirror.attributes();
    }
    if (propertyMirror.propertyType() != PropertyType.Normal) {
      result.propertyType = propertyMirror.propertyType();
    }
    result.ref = propertyValue.handle();
2346
  }
2347
  return result;
2348
};
2349 2350 2351


JSONProtocolSerializer.prototype.serializeFrame_ = function(mirror, content) {
2352 2353
  content.index = mirror.index();
  content.receiver = this.serializeReference(mirror.receiver());
2354
  var func = mirror.func();
2355
  content.func = this.serializeReference(func);
2356
  if (func.script()) {
2357
    content.script = this.serializeReference(func.script());
2358
  }
2359
  content.constructCall = mirror.isConstructCall();
2360 2361 2362 2363
  content.atReturn = mirror.isAtReturn();
  if (mirror.isAtReturn()) {
    content.returnValue = this.serializeReference(mirror.returnValue());
  }
2364
  content.debuggerFrame = mirror.isDebuggerFrame();
2365 2366
  var x = new Array(mirror.argumentCount());
  for (var i = 0; i < mirror.argumentCount(); i++) {
2367
    var arg = {};
2368
    var argument_name = mirror.argumentName(i);
2369
    if (argument_name) {
2370
      arg.name = argument_name;
2371
    }
2372 2373
    arg.value = this.serializeReference(mirror.argumentValue(i));
    x[i] = arg;
2374
  }
2375
  content.arguments = x;
2376 2377
  var x = new Array(mirror.localCount());
  for (var i = 0; i < mirror.localCount(); i++) {
2378 2379 2380 2381
    var local = {};
    local.name = mirror.localName(i);
    local.value = this.serializeReference(mirror.localValue(i));
    x[i] = local;
2382
  }
2383
  content.locals = x;
2384
  serializeLocationFields(mirror.sourceLocation(), content);
2385 2386
  var source_line_text = mirror.sourceLineText();
  if (!IS_UNDEFINED(source_line_text)) {
2387
    content.sourceLineText = source_line_text;
2388
  }
2389

2390 2391 2392 2393 2394 2395 2396 2397
  content.scopes = [];
  for (var i = 0; i < mirror.scopeCount(); i++) {
    var scope = mirror.scope(i);
    content.scopes.push({
      type: scope.scopeType(),
      index: i
    });
  }
2398
};
2399 2400


2401 2402 2403 2404
JSONProtocolSerializer.prototype.serializeScope_ = function(mirror, content) {
  content.index = mirror.scopeIndex();
  content.frameIndex = mirror.frameIndex();
  content.type = mirror.scopeType();
2405 2406 2407
  content.object = this.inlineRefs_() ?
                   this.serializeValue(mirror.scopeObject()) :
                   this.serializeReference(mirror.scopeObject());
2408
};
2409 2410


2411
/**
2412 2413
 * Convert a number to a protocol value. For all finite numbers the number
 * itself is returned. For non finite numbers NaN, Infinite and
2414
 * -Infinite the string representation "NaN", "Infinite" or "-Infinite"
2415
 * (not including the quotes) is returned.
2416
 *
2417 2418
 * @param {number} value The number value to convert to a protocol value.
 * @returns {number|string} Protocol value.
2419
 */
2420
function NumberToJSON_(value) {
2421
  if (isNaN(value)) {
2422
    return 'NaN';
2423
  }
2424
  if (!NUMBER_IS_FINITE(value)) {
2425
    if (value > 0) {
2426
      return 'Infinity';
2427
    } else {
2428
      return '-Infinity';
2429 2430
    }
  }
2431
  return value;
2432
}