Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: src/mirror-delay.js

Issue 18092: Added handles to the mirror objects. When a mirror for an object is created... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 11 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/debug-delay.js ('k') | test/mjsunit/debug-backtrace.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 15 matching lines...) Expand all
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 // Touch the RegExp and Date functions to make sure that date-delay.js and 28 // Touch the RegExp and Date functions to make sure that date-delay.js and
29 // regexp-delay.js has been loaded. This is required as the mirrors use 29 // regexp-delay.js has been loaded. This is required as the mirrors use
30 // functions within these files through the builtins object. See the 30 // functions within these files through the builtins object. See the
31 // function DateToISO8601_ as an example. 31 // function DateToISO8601_ as an example.
32 RegExp; 32 RegExp;
33 Date; 33 Date;
34 34
35 35
36 var next_handle_ = 0;
37 var mirror_cache_ = [];
38
39 /**
40 * Clear the mirror handle cache.
41 */
42 function ClearMirrorCache() {
43 next_handle_ = 0;
44 mirror_cache_ = [];
45 }
46
47
36 /** 48 /**
37 * Returns the mirror for a specified value or object. 49 * Returns the mirror for a specified value or object.
38 * 50 *
39 * @param {value or Object} value the value or object to retreive the mirror for 51 * @param {value or Object} value the value or object to retreive the mirror for
40 * @returns {Mirror} the mirror reflects the passed value or object 52 * @returns {Mirror} the mirror reflects the passed value or object
41 */ 53 */
42 function MakeMirror(value) { 54 function MakeMirror(value) {
43 if (IS_UNDEFINED(value)) return new UndefinedMirror(); 55 var mirror;
44 if (IS_NULL(value)) return new NullMirror(); 56 for (id in mirror_cache_) {
45 if (IS_BOOLEAN(value)) return new BooleanMirror(value); 57 mirror = mirror_cache_[id];
46 if (IS_NUMBER(value)) return new NumberMirror(value); 58 if (mirror.value() === value) {
47 if (IS_STRING(value)) return new StringMirror(value); 59 return mirror;
48 if (IS_ARRAY(value)) return new ArrayMirror(value); 60 }
49 if (IS_DATE(value)) return new DateMirror(value); 61 }
50 if (IS_FUNCTION(value)) return new FunctionMirror(value); 62
51 if (IS_REGEXP(value)) return new RegExpMirror(value); 63 if (IS_UNDEFINED(value)) {
52 if (IS_ERROR(value)) return new ErrorMirror(value); 64 mirror = new UndefinedMirror();
53 return new ObjectMirror(value); 65 } else if (IS_NULL(value)) {
66 mirror = new NullMirror();
67 } else if (IS_BOOLEAN(value)) {
68 mirror = new BooleanMirror(value);
69 } else if (IS_NUMBER(value)) {
70 mirror = new NumberMirror(value);
71 } else if (IS_STRING(value)) {
72 mirror = new StringMirror(value);
73 } else if (IS_ARRAY(value)) {
74 mirror = new ArrayMirror(value);
75 } else if (IS_DATE(value)) {
76 mirror = new DateMirror(value);
77 } else if (IS_FUNCTION(value)) {
78 mirror = new FunctionMirror(value);
79 } else if (IS_REGEXP(value)) {
80 mirror = new RegExpMirror(value);
81 } else if (IS_ERROR(value)) {
82 mirror = new ErrorMirror(value);
83 } else {
84 mirror = new ObjectMirror(value);
85 }
86
87 mirror_cache_[mirror.handle()] = mirror;
88 return mirror;
89 }
90
91
92 /**
93 * Returns the mirror for the undefined value.
94 *
95 * @returns {Mirror} the mirror reflects the undefined value
96 */
97 function GetUndefinedMirror() {
98 return MakeMirror(void 0);
54 } 99 }
55 100
56 101
57 /** 102 /**
58 * Inherit the prototype methods from one constructor into another. 103 * Inherit the prototype methods from one constructor into another.
59 * 104 *
60 * The Function.prototype.inherits from lang.js rewritten as a standalone 105 * The Function.prototype.inherits from lang.js rewritten as a standalone
61 * function (not on Function.prototype). NOTE: If this file is to be loaded 106 * function (not on Function.prototype). NOTE: If this file is to be loaded
62 * during bootstrapping this function needs to be revritten using some native 107 * during bootstrapping this function needs to be revritten using some native
63 * functions as prototype setup using normal JavaScript does not work as 108 * functions as prototype setup using normal JavaScript does not work as
(...skipping 16 matching lines...) Expand all
80 const UNDEFINED_TYPE = 'undefined'; 125 const UNDEFINED_TYPE = 'undefined';
81 const NULL_TYPE = 'null'; 126 const NULL_TYPE = 'null';
82 const BOOLEAN_TYPE = 'boolean'; 127 const BOOLEAN_TYPE = 'boolean';
83 const NUMBER_TYPE = 'number'; 128 const NUMBER_TYPE = 'number';
84 const STRING_TYPE = 'string'; 129 const STRING_TYPE = 'string';
85 const OBJECT_TYPE = 'object'; 130 const OBJECT_TYPE = 'object';
86 const FUNCTION_TYPE = 'function'; 131 const FUNCTION_TYPE = 'function';
87 const REGEXP_TYPE = 'regexp'; 132 const REGEXP_TYPE = 'regexp';
88 const ERROR_TYPE = 'error'; 133 const ERROR_TYPE = 'error';
89 const PROPERTY_TYPE = 'property'; 134 const PROPERTY_TYPE = 'property';
90 const ACCESSOR_TYPE = 'accessor';
91 const FRAME_TYPE = 'frame'; 135 const FRAME_TYPE = 'frame';
92 const SCRIPT_TYPE = 'script'; 136 const SCRIPT_TYPE = 'script';
93 137
94 // Maximum length when sending strings through the JSON protocol. 138 // Maximum length when sending strings through the JSON protocol.
95 const kMaxProtocolStringLength = 80; 139 const kMaxProtocolStringLength = 80;
96 140
97 // Different kind of properties. 141 // Different kind of properties.
98 PropertyKind = {}; 142 PropertyKind = {};
99 PropertyKind.Named = 1; 143 PropertyKind.Named = 1;
100 PropertyKind.Indexed = 2; 144 PropertyKind.Indexed = 2;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 this.type_ = type; 192 this.type_ = type;
149 }; 193 };
150 194
151 195
152 Mirror.prototype.type = function() { 196 Mirror.prototype.type = function() {
153 return this.type_; 197 return this.type_;
154 }; 198 };
155 199
156 200
157 /** 201 /**
202 * Check whether the mirror reflects a value.
203 * @returns {boolean} True if the mirror reflects a value.
204 */
205 Mirror.prototype.isValue = function() {
206 return this instanceof ValueMirror;
207 }
208
209
210 /**
158 * Check whether the mirror reflects the undefined value. 211 * Check whether the mirror reflects the undefined value.
159 * @returns {boolean} True if the mirror reflects the undefined value. 212 * @returns {boolean} True if the mirror reflects the undefined value.
160 */ 213 */
161 Mirror.prototype.isUndefined = function() { 214 Mirror.prototype.isUndefined = function() {
162 return this instanceof UndefinedMirror; 215 return this instanceof UndefinedMirror;
163 } 216 }
164 217
165 218
166 /** 219 /**
167 * Check whether the mirror reflects the null value. 220 * Check whether the mirror reflects the null value.
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 335
283 /** 336 /**
284 * Check whether the mirror reflects a script. 337 * Check whether the mirror reflects a script.
285 * @returns {boolean} True if the mirror reflects a script 338 * @returns {boolean} True if the mirror reflects a script
286 */ 339 */
287 Mirror.prototype.isScript = function() { 340 Mirror.prototype.isScript = function() {
288 return this instanceof ScriptMirror; 341 return this instanceof ScriptMirror;
289 } 342 }
290 343
291 344
292 /**
293 * Serialize object in JSON format. The actual serialization is handled by the
294 * JSONProtocolSerializer.
295 * @param {boolean} details Indicate level of details to include
296 * @return {string} JSON serialization
297 */
298 Mirror.prototype.toJSONProtocol = function(details) {
299 var serializer = new JSONProtocolSerializer(details)
300 return serializer.serialize(this)
301 }
302
303
304 Mirror.prototype.toText = function() { 345 Mirror.prototype.toText = function() {
305 // Simpel to text which is used when on specialization in subclass. 346 // Simpel to text which is used when on specialization in subclass.
306 return "#<" + builtins.GetInstanceName(this.constructor.name) + ">"; 347 return "#<" + builtins.GetInstanceName(this.constructor.name) + ">";
307 } 348 }
308 349
309 350
310 /** 351 /**
311 * Base class for all value mirror objects. 352 * Base class for all value mirror objects.
312 * @param {string} type The type of the mirror 353 * @param {string} type The type of the mirror
313 * @param {value} value The value reflected by this mirror 354 * @param {value} value The value reflected by this mirror
314 * @constructor 355 * @constructor
315 * @extends Mirror 356 * @extends Mirror
316 */ 357 */
317 function ValueMirror(type, value) { 358 function ValueMirror(type, value) {
318 Mirror.call(this, type); 359 Mirror.call(this, type);
360 this.handle_ = next_handle_++;
319 this.value_ = value; 361 this.value_ = value;
320 } 362 }
321 inherits(ValueMirror, Mirror); 363 inherits(ValueMirror, Mirror);
322 364
323 365
366 Mirror.prototype.handle = function() {
367 return this.handle_;
368 };
369
370
324 /** 371 /**
325 * Check whether this is a primitive value. 372 * Check whether this is a primitive value.
326 * @return {boolean} True if the mirror reflects a primitive value 373 * @return {boolean} True if the mirror reflects a primitive value
327 */ 374 */
328 ValueMirror.prototype.isPrimitive = function() { 375 ValueMirror.prototype.isPrimitive = function() {
329 var type = this.type(); 376 var type = this.type();
330 return type === 'undefined' || 377 return type === 'undefined' ||
331 type === 'null' || 378 type === 'null' ||
332 type === 'boolean' || 379 type === 'boolean' ||
333 type === 'number' || 380 type === 'number' ||
334 type === 'string'; 381 type === 'string';
335 }; 382 };
336 383
337 384
338 /** 385 /**
339 * Get the actual value reflected by this mirror. 386 * Get the actual value reflected by this mirror.
340 * @return {value} The value reflected by this mirror 387 * @return {value} The value reflected by this mirror
341 */ 388 */
342 ValueMirror.prototype.value = function() { 389 ValueMirror.prototype.value = function() {
343 return this.value_; 390 return this.value_;
344 }; 391 };
345 392
346 393
347 /** 394 /**
348 * Mirror object for Undefined. 395 * Mirror object for Undefined.
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
574 }; 621 };
575 622
576 623
577 ObjectMirror.prototype.property = function(name) { 624 ObjectMirror.prototype.property = function(name) {
578 var details = %DebugGetPropertyDetails(this.value_, %ToString(name)); 625 var details = %DebugGetPropertyDetails(this.value_, %ToString(name));
579 if (details) { 626 if (details) {
580 return new PropertyMirror(this, name, details); 627 return new PropertyMirror(this, name, details);
581 } 628 }
582 629
583 // Nothing found. 630 // Nothing found.
584 return new UndefinedMirror(); 631 return GetUndefinedMirror();
585 }; 632 };
586 633
587 634
588 635
589 /** 636 /**
590 * Try to find a property from its value. 637 * Try to find a property from its value.
591 * @param {Mirror} value The property value to look for 638 * @param {Mirror} value The property value to look for
592 * @return {PropertyMirror} The property with the specified value. If no 639 * @return {PropertyMirror} The property with the specified value. If no
593 * property was found with the specified value UndefinedMirror is returned 640 * property was found with the specified value UndefinedMirror is returned
594 */ 641 */
595 ObjectMirror.prototype.lookupProperty = function(value) { 642 ObjectMirror.prototype.lookupProperty = function(value) {
596 var properties = this.properties(); 643 var properties = this.properties();
597 644
598 // Look for property value in properties. 645 // Look for property value in properties.
599 for (var i = 0; i < properties.length; i++) { 646 for (var i = 0; i < properties.length; i++) {
600 647
601 // Skip properties which are defined through assessors. 648 // Skip properties which are defined through assessors.
602 var property = properties[i]; 649 var property = properties[i];
603 if (property.propertyType() != PropertyType.Callbacks) { 650 if (property.propertyType() != PropertyType.Callbacks) {
604 if (%_ObjectEquals(property.value_, value.value_)) { 651 if (%_ObjectEquals(property.value_, value.value_)) {
605 return property; 652 return property;
606 } 653 }
607 } 654 }
608 } 655 }
609 656
610 // Nothing found. 657 // Nothing found.
611 return new UndefinedMirror(); 658 return GetUndefinedMirror();
612 }; 659 };
613 660
614 661
615 /** 662 /**
616 * Returns objects which has direct references to this object 663 * Returns objects which has direct references to this object
617 * @param {number} opt_max_instances Optional parameter specifying the maximum 664 * @param {number} opt_max_instances Optional parameter specifying the maximum
618 * number of instances to return. 665 * number of instances to return.
619 * @return {Array} The objects which has direct references to this object. 666 * @return {Array} The objects which has direct references to this object.
620 */ 667 */
621 ObjectMirror.prototype.referencedBy = function(opt_max_instances) { 668 ObjectMirror.prototype.referencedBy = function(opt_max_instances) {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
680 727
681 /** 728 /**
682 * Returns the source code for the function. 729 * Returns the source code for the function.
683 * @return {string or undefined} The source code for the function. If the 730 * @return {string or undefined} The source code for the function. If the
684 * function is not resolved undefined will be returned. 731 * function is not resolved undefined will be returned.
685 */ 732 */
686 FunctionMirror.prototype.source = function() { 733 FunctionMirror.prototype.source = function() {
687 // Return source if function is resolved. Otherwise just fall through to 734 // Return source if function is resolved. Otherwise just fall through to
688 // return undefined. 735 // return undefined.
689 if (this.resolved()) { 736 if (this.resolved()) {
690 // This builtins function is context independant (only uses runtime
691 // calls and typeof.
692 return builtins.FunctionSourceString(this.value_); 737 return builtins.FunctionSourceString(this.value_);
693 } 738 }
694 }; 739 };
695 740
696 741
697 /** 742 /**
698 * Returns the script object for the function. 743 * Returns the script object for the function.
699 * @return {ScriptMirror or undefined} Script object for the function or 744 * @return {ScriptMirror or undefined} Script object for the function or
700 * undefined if the function has no script 745 * undefined if the function has no script
701 */ 746 */
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
756 } 801 }
757 inherits(UnresolvedFunctionMirror, FunctionMirror); 802 inherits(UnresolvedFunctionMirror, FunctionMirror);
758 803
759 804
760 UnresolvedFunctionMirror.prototype.className = function() { 805 UnresolvedFunctionMirror.prototype.className = function() {
761 return 'Function'; 806 return 'Function';
762 }; 807 };
763 808
764 809
765 UnresolvedFunctionMirror.prototype.constructorFunction = function() { 810 UnresolvedFunctionMirror.prototype.constructorFunction = function() {
766 return new UndefinedMirror(); 811 return GetUndefinedMirror();
767 }; 812 };
768 813
769 814
770 UnresolvedFunctionMirror.prototype.prototypeObject = function() { 815 UnresolvedFunctionMirror.prototype.prototypeObject = function() {
771 return new UndefinedMirror(); 816 return GetUndefinedMirror();
772 }; 817 };
773 818
774 819
775 UnresolvedFunctionMirror.prototype.protoObject = function() { 820 UnresolvedFunctionMirror.prototype.protoObject = function() {
776 return new UndefinedMirror(); 821 return GetUndefinedMirror();
777 }; 822 };
778 823
779 824
780 UnresolvedFunctionMirror.prototype.name = function() { 825 UnresolvedFunctionMirror.prototype.name = function() {
781 return this.value_; 826 return this.value_;
782 }; 827 };
783 828
784 829
785 UnresolvedFunctionMirror.prototype.propertyNames = function(kind, limit) { 830 UnresolvedFunctionMirror.prototype.propertyNames = function(kind, limit) {
786 return []; 831 return [];
(...skipping 21 matching lines...) Expand all
808 var from_index = opt_from_index || 0; 853 var from_index = opt_from_index || 0;
809 var to_index = opt_to_index || this.length() - 1; 854 var to_index = opt_to_index || this.length() - 1;
810 if (from_index > to_index) return new Array(); 855 if (from_index > to_index) return new Array();
811 var values = new Array(to_index - from_index + 1); 856 var values = new Array(to_index - from_index + 1);
812 for (var i = from_index; i <= to_index; i++) { 857 for (var i = from_index; i <= to_index; i++) {
813 var details = %DebugGetPropertyDetails(this.value_, %ToString(i)); 858 var details = %DebugGetPropertyDetails(this.value_, %ToString(i));
814 var value; 859 var value;
815 if (details) { 860 if (details) {
816 value = new PropertyMirror(this, i, details); 861 value = new PropertyMirror(this, i, details);
817 } else { 862 } else {
818 value = new UndefinedMirror(); 863 value = GetUndefinedMirror();
819 } 864 }
820 values[i - from_index] = value; 865 values[i - from_index] = value;
821 } 866 }
822 return values; 867 return values;
823 } 868 }
824 869
825 870
826 /** 871 /**
827 * Mirror object for dates. 872 * Mirror object for dates.
828 * @param {Date} value The Date object reflected by this mirror 873 * @param {Date} value The Date object reflected by this mirror
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after
1218 1263
1219 1264
1220 FrameMirror.prototype.func = function() { 1265 FrameMirror.prototype.func = function() {
1221 // Get the function for this frame from the VM. 1266 // Get the function for this frame from the VM.
1222 var f = this.details_.func(); 1267 var f = this.details_.func();
1223 1268
1224 // Create a function mirror. NOTE: MakeMirror cannot be used here as the 1269 // Create a function mirror. NOTE: MakeMirror cannot be used here as the
1225 // value returned from the VM might be a string if the function for the 1270 // value returned from the VM might be a string if the function for the
1226 // frame is unresolved. 1271 // frame is unresolved.
1227 if (IS_FUNCTION(f)) { 1272 if (IS_FUNCTION(f)) {
1228 return new FunctionMirror(f); 1273 return MakeMirror(f);
1229 } else { 1274 } else {
1230 return new UnresolvedFunctionMirror(f); 1275 return new UnresolvedFunctionMirror(f);
1231 } 1276 }
1232 }; 1277 };
1233 1278
1234 1279
1235 FrameMirror.prototype.receiver = function() { 1280 FrameMirror.prototype.receiver = function() {
1236 return MakeMirror(this.details_.receiver()); 1281 return MakeMirror(this.details_.receiver());
1237 }; 1282 };
1238 1283
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
1338 } else if (this.isDebuggerFrame()) { 1383 } else if (this.isDebuggerFrame()) {
1339 result += '[debugger]'; 1384 result += '[debugger]';
1340 } else { 1385 } else {
1341 // If the receiver has a className which is 'global' don't display it. 1386 // If the receiver has a className which is 'global' don't display it.
1342 var display_receiver = !receiver.className || receiver.className() != 'globa l'; 1387 var display_receiver = !receiver.className || receiver.className() != 'globa l';
1343 if (display_receiver) { 1388 if (display_receiver) {
1344 result += receiver.toText(); 1389 result += receiver.toText();
1345 } 1390 }
1346 // Try to find the function as a property in the receiver. Include the 1391 // Try to find the function as a property in the receiver. Include the
1347 // prototype chain in the lookup. 1392 // prototype chain in the lookup.
1348 var property = new UndefinedMirror(); 1393 var property = GetUndefinedMirror();
1349 if (!receiver.isUndefined()) { 1394 if (!receiver.isUndefined()) {
1350 for (var r = receiver; !r.isNull() && property.isUndefined(); r = r.protoO bject()) { 1395 for (var r = receiver; !r.isNull() && property.isUndefined(); r = r.protoO bject()) {
1351 property = r.lookupProperty(func); 1396 property = r.lookupProperty(func);
1352 } 1397 }
1353 } 1398 }
1354 if (!property.isUndefined()) { 1399 if (!property.isUndefined()) {
1355 // The function invoked was found on the receiver. Use the property name 1400 // The function invoked was found on the receiver. Use the property name
1356 // for the backtrace. 1401 // for the backtrace.
1357 if (!property.isIndexed()) { 1402 if (!property.isIndexed()) {
1358 if (display_receiver) { 1403 if (display_receiver) {
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
1519 result += '-'; 1564 result += '-';
1520 result += this.lineOffset() + this.lineCount() - 1; 1565 result += this.lineOffset() + this.lineCount() - 1;
1521 } else { 1566 } else {
1522 result += this.lineCount(); 1567 result += this.lineCount();
1523 } 1568 }
1524 result += ')'; 1569 result += ')';
1525 return result; 1570 return result;
1526 } 1571 }
1527 1572
1528 1573
1529 function JSONProtocolSerializer(details) { 1574 /**
1530 this.details_ = details; 1575 * Returns a mirror serializer
1576 *
1577 * @param {boolean} details Set to true to include details
1578 * @returns {MirrorSerializer} mirror serializer
1579 */
1580 function MakeMirrorSerializer(details) {
1581 return new JSONProtocolSerializer(details);
1531 } 1582 }
1532 1583
1533 1584
1534 JSONProtocolSerializer.prototype.serialize = function(mirror) { 1585 /**
1535 // Collect the JSON property/value pairs in a array. 1586 * Object for serializing a mirror objects and its direct references.
1587 * @param {boolean} details Indicates whether to include details for the mirror
1588 * serialized
1589 * @constructor
1590 */
1591 function JSONProtocolSerializer(details) {
1592 this.details_ = details;
1593 this.mirrors_ = [ ];
1594 }
1595
1596
1597 /**
1598 * Returns a serialization of an object reference. The referenced object are
1599 * added to the serialization state.
1600 *
1601 * @param {Mirror} mirror The mirror to serialize
1602 * @returns {String} JSON serialization
1603 */
1604 JSONProtocolSerializer.prototype.serializeReference = function(mirror) {
1605 return this.serialize_(mirror, true, true);
1606 }
1607
1608
1609 /**
1610 * Returns a serialization of an object value. The referenced objects are
1611 * added to the serialization state.
1612 *
1613 * @param {Mirror} mirror The mirror to serialize
1614 * @returns {String} JSON serialization
1615 */
1616 JSONProtocolSerializer.prototype.serializeValue = function(mirror) {
1617 var json = this.serialize_(mirror, false, true);
1618 return json;
1619 }
1620
1621
1622 /**
1623 * Returns a serialization of all the objects referenced.
1624 *
1625 * @param {Mirror} mirror The mirror to serialize
1626 * @returns {String} JSON serialization
1627 */
1628 JSONProtocolSerializer.prototype.serializeReferencedObjects = function() {
1629 // Collect the JSON serialization of the referenced objects in an array.
1630 var content = new Array();
1631
1632 // Get the number of referenced objects.
1633 var count = this.mirrors_.length;
1634
1635 for (var i = 0; i < count; i++) {
1636 content.push(this.serialize_(this.mirrors_[i], false, false));
1637 }
1638
1639 var json = ArrayToJSONArray_(content);
1640 return json;
1641 }
1642
1643
1644 JSONProtocolSerializer.prototype.add_ = function(mirror) {
1645 // If this mirror is already in the list just return.
1646 for (var i = 0; i < this.mirrors_.length; i++) {
1647 if (this.mirrors_[i] === mirror) {
1648 return;
1649 }
1650 }
1651
1652 // Add the mirror to the list of mirrors to be serialized.
1653 this.mirrors_.push(mirror);
1654 }
1655
1656
1657 JSONProtocolSerializer.prototype.serialize_ = function(mirror, reference,
1658 details) {
1659 // If serializing a reference to a value just return the reference and add the
1660 // mirror to the referenced mirrors.
1661 if (reference && mirror.isValue()) {
1662 this.add_(mirror);
1663 return '{ref:' + mirror.handle() + '}';
1664 }
1665
1666 // Collect the JSON property/value pairs in an array.
1536 var content = new Array(); 1667 var content = new Array();
1537 1668
1538 // Always add the type 1669 // Add the handle for value mirrors.
1670 if (mirror.isValue()) {
1671 content.push(MakeJSONPair_('handle', NumberToJSON_(mirror.handle())));
1672 }
1673
1674 // Always add the type.
1539 content.push(MakeJSONPair_('type', StringToJSON_(mirror.type()))); 1675 content.push(MakeJSONPair_('type', StringToJSON_(mirror.type())));
1540 1676
1541 switch (mirror.type()) { 1677 switch (mirror.type()) {
1542 case UNDEFINED_TYPE: 1678 case UNDEFINED_TYPE:
1543 case NULL_TYPE: 1679 case NULL_TYPE:
1544 // Undefined and null are represented just by their type. 1680 // Undefined and null are represented just by their type.
1545 break; 1681 break;
1546 1682
1547 case BOOLEAN_TYPE: 1683 case BOOLEAN_TYPE:
1548 // Boolean values are simply represented by their value. 1684 // Boolean values are simply represented by their value.
(...skipping 17 matching lines...) Expand all
1566 content.push(MakeJSONPair_('value', StringToJSON_(mirror.value()))); 1702 content.push(MakeJSONPair_('value', StringToJSON_(mirror.value())));
1567 } 1703 }
1568 content.push(MakeJSONPair_('length', NumberToJSON_(mirror.length()))); 1704 content.push(MakeJSONPair_('length', NumberToJSON_(mirror.length())));
1569 break; 1705 break;
1570 1706
1571 case OBJECT_TYPE: 1707 case OBJECT_TYPE:
1572 case FUNCTION_TYPE: 1708 case FUNCTION_TYPE:
1573 case ERROR_TYPE: 1709 case ERROR_TYPE:
1574 case REGEXP_TYPE: 1710 case REGEXP_TYPE:
1575 // Add object representation. 1711 // Add object representation.
1576 this.serializeObject_(mirror, content); 1712 this.serializeObject_(mirror, content, details);
1577 break; 1713 break;
1578 1714
1579 case PROPERTY_TYPE: 1715 case PROPERTY_TYPE:
1580 // Properties are represented by name, value, attributes and type. 1716 throw new Error('PropertyMirror cannot be serialized independeltly')
1581 content.push(MakeJSONPair_('name',
1582 StringToJSON_(mirror.name())));
1583 content.push(MakeJSONPair_('value',
1584 mirror.value().toJSONProtocol(this.details_)));
1585 if (mirror.attributes() != PropertyAttribute.None) {
1586 content.push(MakeJSONPair_('attributes',
1587 NumberToJSON_(mirror.attributes())));
1588 }
1589 if (mirror.propertyType() != PropertyType.Normal) {
1590 content.push(MakeJSONPair_('propertyType',
1591 NumberToJSON_(mirror.propertyType())));
1592 }
1593 break;
1594
1595 case ACCESSOR_TYPE:
1596 // An accessor can either be native or defined through JavaScript.
1597 if (mirror.isNative()) {
1598 content.push(MakeJSONPair_('native', BooleanToJSON_(true)));
1599 } else {
1600 content.push(MakeJSONPair_('getter',
1601 mirror.getter().toJSONProtocol(false)));
1602 content.push(MakeJSONPair_('setter',
1603 mirror.setter().toJSONProtocol(false)));
1604 }
1605 break; 1717 break;
1606 1718
1607 case FRAME_TYPE: 1719 case FRAME_TYPE:
1608 // Add object representation. 1720 // Add object representation.
1609 this.serializeFrame_(mirror, content); 1721 this.serializeFrame_(mirror, content);
1610 break; 1722 break;
1611 1723
1612 case SCRIPT_TYPE: 1724 case SCRIPT_TYPE:
1613 // Script is represented by name and source attributes. 1725 // Script is represented by name and source attributes.
1614 if (mirror.name()) { 1726 if (mirror.name()) {
1615 content.push(MakeJSONPair_('name', StringToJSON_(mirror.name()))); 1727 content.push(MakeJSONPair_('name', StringToJSON_(mirror.name())));
1616 } 1728 }
1617 content.push(MakeJSONPair_('lineOffset', 1729 content.push(MakeJSONPair_('lineOffset',
1618 NumberToJSON_(mirror.lineOffset()))); 1730 NumberToJSON_(mirror.lineOffset())));
1619 content.push(MakeJSONPair_('columnOffset', 1731 content.push(MakeJSONPair_('columnOffset',
1620 NumberToJSON_(mirror.columnOffset()))); 1732 NumberToJSON_(mirror.columnOffset())));
1621 content.push(MakeJSONPair_('lineCount', 1733 content.push(MakeJSONPair_('lineCount',
1622 NumberToJSON_(mirror.lineCount()))); 1734 NumberToJSON_(mirror.lineCount())));
1623 content.push(MakeJSONPair_('scriptType', 1735 content.push(MakeJSONPair_('scriptType',
1624 NumberToJSON_(mirror.scriptType()))); 1736 NumberToJSON_(mirror.scriptType())));
1625 break; 1737 break;
1626
1627 } 1738 }
1628 1739
1629 // Always add the text representation. 1740 // Always add the text representation.
1630 content.push(MakeJSONPair_('text', StringToJSON_(mirror.toText()))); 1741 content.push(MakeJSONPair_('text', StringToJSON_(mirror.toText())));
1631 1742
1632 // Create and return the JSON string. 1743 // Create and return the JSON string.
1633 return ArrayToJSONObject_(content); 1744 return ArrayToJSONObject_(content);
1634 } 1745 }
1635 1746
1636 1747
1637 JSONProtocolSerializer.prototype.serializeObject_ = function(mirror, content) { 1748 /**
1749 * Serialize object information to the following JSON format.
1750 *
1751 * {"className":"<class name>",
1752 * "constructorFunction":{"ref":<number>},
1753 * "protoObject":{"ref":<number>},
1754 * "prototypeObject":{"ref":<number>},
1755 * "namedInterceptor":<boolean>,
1756 * "indexedInterceptor":<boolean>,
1757 * "properties":[<properties>]}
1758 */
1759 JSONProtocolSerializer.prototype.serializeObject_ = function(mirror, content,
1760 details) {
1761 // Add general object properties.
1638 content.push(MakeJSONPair_('className', 1762 content.push(MakeJSONPair_('className',
1639 StringToJSON_(mirror.className()))); 1763 StringToJSON_(mirror.className())));
1764 content.push(MakeJSONPair_('constructorFunction',
1765 this.serializeReference(mirror.constructorFunction())));
1766 content.push(MakeJSONPair_('protoObject',
1767 this.serializeReference(mirror.protoObject())));
1768 content.push(MakeJSONPair_('prototypeObject',
1769 this.serializeReference(mirror.prototypeObject())));
1640 1770
1641 if (this.details_) { 1771 // Add flags to indicate whether there are interceptors.
1642 content.push(MakeJSONPair_('constructorFunction',
1643 mirror.constructorFunction().toJSONProtocol(false)));
1644 content.push(MakeJSONPair_('protoObject',
1645 mirror.protoObject().toJSONProtocol(false)));
1646 content.push(MakeJSONPair_('prototypeObject',
1647 mirror.prototypeObject().toJSONProtocol(false)));
1648
1649 // Add properties. For arrays don't include indexed proeprties.
1650 var kind = PropertyKind.Named;
1651 if (!mirror.isArray()) {
1652 kind |= PropertyKind.Indexed
1653 }
1654 var propertyNames = mirror.propertyNames(kind);
1655 var x = new Array(propertyNames.length);
1656 for (var i = 0; i < propertyNames.length; i++) {
1657 x[i] = mirror.property(propertyNames[i]).toJSONProtocol(false);
1658 }
1659 content.push(MakeJSONPair_('properties', ArrayToJSONArray_(x)));
1660
1661 // For arrays the indexed properties are added separately and the length is
1662 // added as well.
1663 if (mirror.isArray()) {
1664 var propertyNames = mirror.propertyNames(PropertyKind.Indexed);
1665 var x = new Array(propertyNames.length);
1666 for (var i = 0; i < propertyNames.length; i++) {
1667 x[i] = mirror.property(propertyNames[i]).toJSONProtocol(false);
1668 }
1669 content.push(MakeJSONPair_('indexedProperties', ArrayToJSONArray_(x)));
1670
1671 // Add the array length.
1672 content.push(MakeJSONPair_('length', NumberToJSON_(mirror.length())));
1673 }
1674 }
1675
1676 if (mirror.hasNamedInterceptor()) { 1772 if (mirror.hasNamedInterceptor()) {
1677 content.push(MakeJSONPair_('namedInterceptor', BooleanToJSON_(true))); 1773 content.push(MakeJSONPair_('namedInterceptor', BooleanToJSON_(true)));
1678 } 1774 }
1679
1680 if (mirror.hasIndexedInterceptor()) { 1775 if (mirror.hasIndexedInterceptor()) {
1681 content.push(MakeJSONPair_('indexedInterceptor', BooleanToJSON_(true))); 1776 content.push(MakeJSONPair_('indexedInterceptor', BooleanToJSON_(true)));
1682 } 1777 }
1683 1778
1779 // Add function specific properties.
1684 if (mirror.isFunction()) { 1780 if (mirror.isFunction()) {
1685 // Add function specific properties. 1781 // Add function specific properties.
1686 content.push(MakeJSONPair_('name', StringToJSON_(mirror.name()))); 1782 content.push(MakeJSONPair_('name', StringToJSON_(mirror.name())));
1687 content.push(MakeJSONPair_('resolved', BooleanToJSON_(mirror.resolved()))); 1783 content.push(MakeJSONPair_('resolved', BooleanToJSON_(mirror.resolved())));
1688 if (this.details_ && mirror.resolved()) { 1784 if (mirror.resolved()) {
1689 content.push(MakeJSONPair_('source', StringToJSON_(mirror.source()))); 1785 content.push(MakeJSONPair_('source', StringToJSON_(mirror.source())));
1690 } 1786 }
1691 if (mirror.script()) { 1787 if (mirror.script()) {
1692 content.push(MakeJSONPair_('script', mirror.script().toJSONProtocol())); 1788 content.push(MakeJSONPair_('script', this.serializeValue(mirror.script())) );
1693 } 1789 }
1694 } else if (mirror.isDate()) { 1790 }
1791
1792 // Add date specific properties.
1793 if (mirror.isDate()) {
1695 // Add date specific properties. 1794 // Add date specific properties.
1696 content.push(MakeJSONPair_('value', DateToJSON_(mirror.value()))); 1795 content.push(MakeJSONPair_('value', DateToJSON_(mirror.value())));
1697 } else if (mirror.isRegExp()) {
1698 // Add regexp specific properties.
1699 content.push(MakeJSONPair_('source', StringToJSON_(mirror.source())));
1700 content.push(MakeJSONPair_('global', BooleanToJSON_(mirror.global())));
1701 content.push(MakeJSONPair_('ignoreCase',
1702 BooleanToJSON_(mirror.ignoreCase())));
1703 content.push(MakeJSONPair_('multiline',
1704 BooleanToJSON_(mirror.multiline())));
1705 } else if (mirror.isError()) {
1706 // Add error specific properties.
1707 content.push(MakeJSONPair_('message', StringToJSON_(mirror.message())));
1708 } 1796 }
1797
1798 // Add actual properties - named properties followed by indexed properties.
1799 var propertyNames = mirror.propertyNames(PropertyKind.Named);
1800 var propertyIndexes = mirror.propertyNames(PropertyKind.Indexed);
1801 var p = new Array(propertyNames.length + propertyIndexes.length);
1802 for (var i = 0; i < propertyNames.length; i++) {
1803 var property_mirror = mirror.property(propertyNames[i]);
1804 p[i] = this.serializeProperty_(property_mirror);
1805 if (details) {
1806 this.add_(property_mirror.value());
1807 }
1808 }
1809 for (var i = 0; i < propertyIndexes.length; i++) {
1810 var property_mirror = mirror.property(propertyIndexes[i]);
1811 p[propertyNames.length + i] = this.serializeProperty_(property_mirror);
1812 if (details) {
1813 this.add_(property_mirror.value());
1814 }
1815 }
1816 content.push(MakeJSONPair_('properties', ArrayToJSONArray_(p)));
1817 }
1818
1819
1820 /**
1821 * Serialize property information to the following JSON format for building the
1822 * array of properties.
1823 *
1824 * {"name":"<property name>",
1825 * "attributes":<number>,
1826 * "propertyType":<number>,
1827 * "ref":<number>}
1828 *
1829 * If the attribute for the property is PropertyAttribute.None it is not added.
1830 * If the propertyType for the property is PropertyType.Normal it is not added.
1831 * Here are a couple of examples.
1832 *
1833 * {"name":"hello","ref":1}
1834 * {"name":"length","attributes":7,"propertyType":3,"ref":2}
1835 *
1836 * @param {PropertyMirror} property_mirror The property to serialize
1837 * @returns {String} JSON serialization
1838 */
1839 JSONProtocolSerializer.prototype.serializeProperty_ = function(property_mirror) {
1840 var builder = new builtins.StringBuilder();
1841 builder.add('{"name":');
1842 builder.add(StringToJSON_(property_mirror.name()));
1843 if (property_mirror.attributes() != PropertyAttribute.None) {
1844 builder.add(',"attributes":');
1845 builder.add(NumberToJSON_(property_mirror.attributes()));
1846 }
1847 if (property_mirror.propertyType() != PropertyType.Normal) {
1848 builder.add(',"propertyType":');
1849 builder.add(NumberToJSON_(property_mirror.propertyType()));
1850 }
1851 builder.add(',"ref":');
1852 builder.add(NumberToJSON_(property_mirror.value().handle()));
1853 builder.add('}');
1854 return builder.generate();
1709 } 1855 }
1710 1856
1711 1857
1712 JSONProtocolSerializer.prototype.serializeFrame_ = function(mirror, content) { 1858 JSONProtocolSerializer.prototype.serializeFrame_ = function(mirror, content) {
1713 content.push(MakeJSONPair_('index', NumberToJSON_(mirror.index()))); 1859 content.push(MakeJSONPair_('index', NumberToJSON_(mirror.index())));
1714 content.push(MakeJSONPair_('receiver', 1860 content.push(MakeJSONPair_('receiver',
1715 mirror.receiver().toJSONProtocol(false))); 1861 this.serializeReference(mirror.receiver())));
1716 content.push(MakeJSONPair_('func', mirror.func().toJSONProtocol(false))); 1862 content.push(MakeJSONPair_('func', this.serializeReference(mirror.func())));
1717 content.push(MakeJSONPair_('constructCall', 1863 content.push(MakeJSONPair_('constructCall',
1718 BooleanToJSON_(mirror.isConstructCall()))); 1864 BooleanToJSON_(mirror.isConstructCall())));
1719 content.push(MakeJSONPair_('debuggerFrame', 1865 content.push(MakeJSONPair_('debuggerFrame',
1720 BooleanToJSON_(mirror.isDebuggerFrame()))); 1866 BooleanToJSON_(mirror.isDebuggerFrame())));
1721 var x = new Array(mirror.argumentCount()); 1867 var x = new Array(mirror.argumentCount());
1722 for (var i = 0; i < mirror.argumentCount(); i++) { 1868 for (var i = 0; i < mirror.argumentCount(); i++) {
1723 arg = new Array(); 1869 arg = new Array();
1724 var argument_name = mirror.argumentName(i) 1870 var argument_name = mirror.argumentName(i)
1725 if (argument_name) { 1871 if (argument_name) {
1726 arg.push(MakeJSONPair_('name', StringToJSON_(argument_name))); 1872 arg.push(MakeJSONPair_('name', StringToJSON_(argument_name)));
1727 } 1873 }
1728 arg.push(MakeJSONPair_('value', 1874 arg.push(MakeJSONPair_('value',
1729 mirror.argumentValue(i).toJSONProtocol(false))); 1875 this.serializeReference(mirror.argumentValue(i))));
1730 x[i] = ArrayToJSONObject_(arg); 1876 x[i] = ArrayToJSONObject_(arg);
1731 } 1877 }
1732 content.push(MakeJSONPair_('arguments', ArrayToJSONArray_(x))); 1878 content.push(MakeJSONPair_('arguments', ArrayToJSONArray_(x)));
1733 var x = new Array(mirror.localCount()); 1879 var x = new Array(mirror.localCount());
1734 for (var i = 0; i < mirror.localCount(); i++) { 1880 for (var i = 0; i < mirror.localCount(); i++) {
1735 var name = MakeJSONPair_('name', StringToJSON_(mirror.localName(i))); 1881 var name = MakeJSONPair_('name', StringToJSON_(mirror.localName(i)));
1736 var value = MakeJSONPair_('value', 1882 var value = MakeJSONPair_('value',
1737 mirror.localValue(i).toJSONProtocol(false)); 1883 this.serializeReference(mirror.localValue(i)));
1738 x[i] = '{' + name + ',' + value + '}'; 1884 x[i] = '{' + name + ',' + value + '}';
1739 } 1885 }
1740 content.push(MakeJSONPair_('locals', ArrayToJSONArray_(x))); 1886 content.push(MakeJSONPair_('locals', ArrayToJSONArray_(x)));
1741 content.push(MakeJSONPair_('position', 1887 content.push(MakeJSONPair_('position',
1742 NumberToJSON_(mirror.sourcePosition()))); 1888 NumberToJSON_(mirror.sourcePosition())));
1743 var line = mirror.sourceLine(); 1889 var line = mirror.sourceLine();
1744 if (!IS_UNDEFINED(line)) { 1890 if (!IS_UNDEFINED(line)) {
1745 content.push(MakeJSONPair_('line', NumberToJSON_(line))); 1891 content.push(MakeJSONPair_('line', NumberToJSON_(line)));
1746 } 1892 }
1747 var column = mirror.sourceColumn(); 1893 var column = mirror.sourceColumn();
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
1860 /** 2006 /**
1861 * Convert a Date to ISO 8601 format. To avoid depending on the Date object 2007 * Convert a Date to ISO 8601 format. To avoid depending on the Date object
1862 * this method calls the functions in date.js directly and not through the 2008 * this method calls the functions in date.js directly and not through the
1863 * value. 2009 * value.
1864 * @param {Date} value The Date value to format as JSON 2010 * @param {Date} value The Date value to format as JSON
1865 * @return {string} JSON formatted Date value 2011 * @return {string} JSON formatted Date value
1866 */ 2012 */
1867 function DateToJSON_(value) { 2013 function DateToJSON_(value) {
1868 return '"' + DateToISO8601_(value) + '"'; 2014 return '"' + DateToISO8601_(value) + '"';
1869 } 2015 }
OLDNEW
« no previous file with comments | « src/debug-delay.js ('k') | test/mjsunit/debug-backtrace.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698