OLD | NEW |
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 976 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
987 json += ',"command":' + StringToJSON_(this.command); | 987 json += ',"command":' + StringToJSON_(this.command); |
988 } | 988 } |
989 if (this.success) { | 989 if (this.success) { |
990 json += ',"success":' + this.success; | 990 json += ',"success":' + this.success; |
991 } else { | 991 } else { |
992 json += ',"success":false'; | 992 json += ',"success":false'; |
993 } | 993 } |
994 if (this.body) { | 994 if (this.body) { |
995 json += ',"body":'; | 995 json += ',"body":'; |
996 // Encode the body part. | 996 // Encode the body part. |
997 if (this.body.toJSONProtocol) { | 997 var serializer = MakeMirrorSerializer(true); |
998 json += this.body.toJSONProtocol(true); | 998 if (this.body instanceof Mirror) { |
| 999 json += serializer.serializeValue(this.body); |
999 } else if (this.body instanceof Array) { | 1000 } else if (this.body instanceof Array) { |
1000 json += '['; | 1001 json += '['; |
1001 for (var i = 0; i < this.body.length; i++) { | 1002 for (var i = 0; i < this.body.length; i++) { |
1002 if (i != 0) json += ','; | 1003 if (i != 0) json += ','; |
1003 if (this.body[i].toJSONProtocol) { | 1004 if (this.body[i] instanceof Mirror) { |
1004 json += this.body[i].toJSONProtocol(true) | 1005 json += serializer.serializeValue(this.body[i]); |
1005 } else { | 1006 } else { |
1006 json += SimpleObjectToJSON_(this.body[i]); | 1007 json += SimpleObjectToJSON_(this.body[i], serializer); |
1007 } | 1008 } |
1008 } | 1009 } |
1009 json += ']'; | 1010 json += ']'; |
1010 } else { | 1011 } else { |
1011 json += SimpleObjectToJSON_(this.body); | 1012 json += SimpleObjectToJSON_(this.body, serializer); |
1012 } | 1013 } |
| 1014 json += ',"refs":'; |
| 1015 json += serializer.serializeReferencedObjects(); |
1013 } | 1016 } |
1014 if (this.message) { | 1017 if (this.message) { |
1015 json += ',"message":' + StringToJSON_(this.message) ; | 1018 json += ',"message":' + StringToJSON_(this.message) ; |
1016 } | 1019 } |
1017 if (this.running) { | 1020 if (this.running) { |
1018 json += ',"running":true'; | 1021 json += ',"running":true'; |
1019 } else { | 1022 } else { |
1020 json += ',"running":false'; | 1023 json += ',"running":false'; |
1021 } | 1024 } |
1022 json += '}'; | 1025 json += '}'; |
(...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1561 | 1564 |
1562 /** | 1565 /** |
1563 * Convert an Object to its JSON representation (see http://www.json.org/). | 1566 * Convert an Object to its JSON representation (see http://www.json.org/). |
1564 * This implementation simply runs through all string property names and adds | 1567 * This implementation simply runs through all string property names and adds |
1565 * each property to the JSON representation for some predefined types. For type | 1568 * each property to the JSON representation for some predefined types. For type |
1566 * "object" the function calls itself recursively unless the object has the | 1569 * "object" the function calls itself recursively unless the object has the |
1567 * function property "toJSONProtocol" in which case that is used. This is not | 1570 * function property "toJSONProtocol" in which case that is used. This is not |
1568 * a general implementation but sufficient for the debugger. Note that circular | 1571 * a general implementation but sufficient for the debugger. Note that circular |
1569 * structures will cause infinite recursion. | 1572 * structures will cause infinite recursion. |
1570 * @param {Object} object The object to format as JSON | 1573 * @param {Object} object The object to format as JSON |
| 1574 * @param {MirrorSerializer} mirror_serializer The serializer to use if any |
| 1575 * mirror objects are encountered. |
1571 * @return {string} JSON formatted object value | 1576 * @return {string} JSON formatted object value |
1572 */ | 1577 */ |
1573 function SimpleObjectToJSON_(object) { | 1578 function SimpleObjectToJSON_(object, mirror_serializer) { |
1574 var content = []; | 1579 var content = []; |
1575 for (var key in object) { | 1580 for (var key in object) { |
1576 // Only consider string keys. | 1581 // Only consider string keys. |
1577 if (typeof key == 'string') { | 1582 if (typeof key == 'string') { |
1578 var property_value = object[key]; | 1583 var property_value = object[key]; |
1579 | 1584 |
1580 // Format the value based on its type. | 1585 // Format the value based on its type. |
1581 var property_value_json; | 1586 var property_value_json; |
1582 switch (typeof property_value) { | 1587 switch (typeof property_value) { |
1583 case 'object': | 1588 case 'object': |
1584 if (typeof property_value.toJSONProtocol == 'function') { | 1589 if (typeof property_value.toJSONProtocol == 'function') { |
1585 property_value_json = property_value.toJSONProtocol(true) | 1590 property_value_json = property_value.toJSONProtocol(true) |
1586 } else if (IS_ARRAY(property_value)){ | 1591 } else if (IS_ARRAY(property_value)){ |
1587 property_value_json = SimpleArrayToJSON_(property_value); | 1592 property_value_json = SimpleArrayToJSON_(property_value, mirror_seri
alizer); |
1588 } else { | 1593 } else { |
1589 property_value_json = SimpleObjectToJSON_(property_value); | 1594 property_value_json = SimpleObjectToJSON_(property_value, mirror_ser
ializer); |
1590 } | 1595 } |
1591 break; | 1596 break; |
1592 | 1597 |
1593 case 'boolean': | 1598 case 'boolean': |
1594 property_value_json = BooleanToJSON_(property_value); | 1599 property_value_json = BooleanToJSON_(property_value); |
1595 break; | 1600 break; |
1596 | 1601 |
1597 case 'number': | 1602 case 'number': |
1598 property_value_json = NumberToJSON_(property_value); | 1603 property_value_json = NumberToJSON_(property_value); |
1599 break; | 1604 break; |
(...skipping 13 matching lines...) Expand all Loading... |
1613 } | 1618 } |
1614 } | 1619 } |
1615 | 1620 |
1616 // Make JSON object representation. | 1621 // Make JSON object representation. |
1617 return '{' + content.join(',') + '}'; | 1622 return '{' + content.join(',') + '}'; |
1618 } | 1623 } |
1619 | 1624 |
1620 /** | 1625 /** |
1621 * Convert an array to its JSON representation. This is a VERY simple | 1626 * Convert an array to its JSON representation. This is a VERY simple |
1622 * implementation just to support what is needed for the debugger. | 1627 * implementation just to support what is needed for the debugger. |
1623 * @param {Array} arrya The array to format as JSON | 1628 * @param {Array} array The array to format as JSON |
| 1629 * @param {MirrorSerializer} mirror_serializer The serializer to use if any |
| 1630 * mirror objects are encountered. |
1624 * @return {string} JSON formatted array value | 1631 * @return {string} JSON formatted array value |
1625 */ | 1632 */ |
1626 function SimpleArrayToJSON_(array) { | 1633 function SimpleArrayToJSON_(array, mirror_serializer) { |
1627 // Make JSON array representation. | 1634 // Make JSON array representation. |
1628 var json = '['; | 1635 var json = '['; |
1629 for (var i = 0; i < array.length; i++) { | 1636 for (var i = 0; i < array.length; i++) { |
1630 if (i != 0) { | 1637 if (i != 0) { |
1631 json += ','; | 1638 json += ','; |
1632 } | 1639 } |
1633 var elem = array[i]; | 1640 var elem = array[i]; |
1634 if (elem.toJSONProtocol) { | 1641 if (elem instanceof Mirror) { |
1635 json += elem.toJSONProtocol(true) | 1642 json += mirror_serializer.serializeValue(elem); |
1636 } else if (IS_OBJECT(elem)) { | 1643 } else if (IS_OBJECT(elem)) { |
1637 json += SimpleObjectToJSON_(elem); | 1644 json += SimpleObjectToJSON_(elem); |
1638 } else if (IS_BOOLEAN(elem)) { | 1645 } else if (IS_BOOLEAN(elem)) { |
1639 json += BooleanToJSON_(elem); | 1646 json += BooleanToJSON_(elem); |
1640 } else if (IS_NUMBER(elem)) { | 1647 } else if (IS_NUMBER(elem)) { |
1641 json += NumberToJSON_(elem); | 1648 json += NumberToJSON_(elem); |
1642 } else if (IS_STRING(elem)) { | 1649 } else if (IS_STRING(elem)) { |
1643 json += StringToJSON_(elem); | 1650 json += StringToJSON_(elem); |
1644 } else { | 1651 } else { |
1645 json += elem; | 1652 json += elem; |
1646 } | 1653 } |
1647 } | 1654 } |
1648 json += ']'; | 1655 json += ']'; |
1649 return json; | 1656 return json; |
1650 } | 1657 } |
OLD | NEW |