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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 const BOOLEAN_TYPE = 'boolean'; | 148 const BOOLEAN_TYPE = 'boolean'; |
149 const NUMBER_TYPE = 'number'; | 149 const NUMBER_TYPE = 'number'; |
150 const STRING_TYPE = 'string'; | 150 const STRING_TYPE = 'string'; |
151 const OBJECT_TYPE = 'object'; | 151 const OBJECT_TYPE = 'object'; |
152 const FUNCTION_TYPE = 'function'; | 152 const FUNCTION_TYPE = 'function'; |
153 const REGEXP_TYPE = 'regexp'; | 153 const REGEXP_TYPE = 'regexp'; |
154 const ERROR_TYPE = 'error'; | 154 const ERROR_TYPE = 'error'; |
155 const PROPERTY_TYPE = 'property'; | 155 const PROPERTY_TYPE = 'property'; |
156 const FRAME_TYPE = 'frame'; | 156 const FRAME_TYPE = 'frame'; |
157 const SCRIPT_TYPE = 'script'; | 157 const SCRIPT_TYPE = 'script'; |
158 const CONTEXT_TYPE = 'context'; | |
158 | 159 |
159 // Maximum length when sending strings through the JSON protocol. | 160 // Maximum length when sending strings through the JSON protocol. |
160 const kMaxProtocolStringLength = 80; | 161 const kMaxProtocolStringLength = 80; |
161 | 162 |
162 // Different kind of properties. | 163 // Different kind of properties. |
163 PropertyKind = {}; | 164 PropertyKind = {}; |
164 PropertyKind.Named = 1; | 165 PropertyKind.Named = 1; |
165 PropertyKind.Indexed = 2; | 166 PropertyKind.Indexed = 2; |
166 | 167 |
167 | 168 |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
357 /** | 358 /** |
358 * Check whether the mirror reflects a script. | 359 * Check whether the mirror reflects a script. |
359 * @returns {boolean} True if the mirror reflects a script | 360 * @returns {boolean} True if the mirror reflects a script |
360 */ | 361 */ |
361 Mirror.prototype.isScript = function() { | 362 Mirror.prototype.isScript = function() { |
362 return this instanceof ScriptMirror; | 363 return this instanceof ScriptMirror; |
363 } | 364 } |
364 | 365 |
365 | 366 |
366 /** | 367 /** |
368 * Check whether the mirror reflects a context. | |
369 * @returns {boolean} True if the mirror reflects a context | |
370 */ | |
371 Mirror.prototype.isContext = function() { | |
372 return this instanceof ContextMirror; | |
373 } | |
374 | |
375 | |
376 /** | |
367 * Allocate a handle id for this object. | 377 * Allocate a handle id for this object. |
368 */ | 378 */ |
369 Mirror.prototype.allocateHandle_ = function() { | 379 Mirror.prototype.allocateHandle_ = function() { |
370 this.handle_ = next_handle_++; | 380 this.handle_ = next_handle_++; |
371 } | 381 } |
372 | 382 |
373 | 383 |
374 Mirror.prototype.toText = function() { | 384 Mirror.prototype.toText = function() { |
375 // Simpel to text which is used when on specialization in subclass. | 385 // Simpel to text which is used when on specialization in subclass. |
376 return "#<" + builtins.GetInstanceName(this.constructor.name) + ">"; | 386 return "#<" + builtins.GetInstanceName(this.constructor.name) + ">"; |
(...skipping 1163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1540 | 1550 |
1541 /** | 1551 /** |
1542 * Mirror object for script source. | 1552 * Mirror object for script source. |
1543 * @param {Script} script The script object | 1553 * @param {Script} script The script object |
1544 * @constructor | 1554 * @constructor |
1545 * @extends Mirror | 1555 * @extends Mirror |
1546 */ | 1556 */ |
1547 function ScriptMirror(script) { | 1557 function ScriptMirror(script) { |
1548 Mirror.call(this, SCRIPT_TYPE); | 1558 Mirror.call(this, SCRIPT_TYPE); |
1549 this.script_ = script; | 1559 this.script_ = script; |
1560 this.context_ = new ContextMirror(script.context_data); | |
1550 this.allocateHandle_(); | 1561 this.allocateHandle_(); |
1551 } | 1562 } |
1552 inherits(ScriptMirror, Mirror); | 1563 inherits(ScriptMirror, Mirror); |
1553 | 1564 |
1554 | 1565 |
1555 ScriptMirror.prototype.value = function() { | 1566 ScriptMirror.prototype.value = function() { |
1556 return this.script_; | 1567 return this.script_; |
1557 }; | 1568 }; |
1558 | 1569 |
1559 | 1570 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1601 position, include_resource_offset) { | 1612 position, include_resource_offset) { |
1602 return this.script_.locationFromPosition(position, include_resource_offset); | 1613 return this.script_.locationFromPosition(position, include_resource_offset); |
1603 } | 1614 } |
1604 | 1615 |
1605 | 1616 |
1606 ScriptMirror.prototype.sourceSlice = function (opt_from_line, opt_to_line) { | 1617 ScriptMirror.prototype.sourceSlice = function (opt_from_line, opt_to_line) { |
1607 return this.script_.sourceSlice(opt_from_line, opt_to_line); | 1618 return this.script_.sourceSlice(opt_from_line, opt_to_line); |
1608 } | 1619 } |
1609 | 1620 |
1610 | 1621 |
1622 ScriptMirror.prototype.context = function() { | |
1623 return this.context_; | |
1624 }; | |
1625 | |
1626 | |
1611 ScriptMirror.prototype.toText = function() { | 1627 ScriptMirror.prototype.toText = function() { |
1612 var result = ''; | 1628 var result = ''; |
1613 result += this.name(); | 1629 result += this.name(); |
1614 result += ' (lines: '; | 1630 result += ' (lines: '; |
1615 if (this.lineOffset() > 0) { | 1631 if (this.lineOffset() > 0) { |
1616 result += this.lineOffset(); | 1632 result += this.lineOffset(); |
1617 result += '-'; | 1633 result += '-'; |
1618 result += this.lineOffset() + this.lineCount() - 1; | 1634 result += this.lineOffset() + this.lineCount() - 1; |
1619 } else { | 1635 } else { |
1620 result += this.lineCount(); | 1636 result += this.lineCount(); |
1621 } | 1637 } |
1622 result += ')'; | 1638 result += ')'; |
1623 return result; | 1639 return result; |
1624 } | 1640 } |
1625 | 1641 |
1626 | 1642 |
1627 /** | 1643 /** |
1644 * Mirror object for context. | |
1645 * @param {Object} data The context data | |
1646 * @constructor | |
1647 * @extends Mirror | |
1648 */ | |
1649 function ContextMirror(data) { | |
1650 Mirror.call(this, CONTEXT_TYPE); | |
1651 this.data_ = data; | |
1652 this.allocateHandle_(); | |
1653 } | |
1654 inherits(ContextMirror, Mirror); | |
1655 | |
1656 | |
1657 ContextMirror.prototype.data = function() { | |
1658 return this.data_; | |
1659 }; | |
1660 | |
1661 | |
1662 /** | |
1628 * Returns a mirror serializer | 1663 * Returns a mirror serializer |
1629 * | 1664 * |
1630 * @param {boolean} details Set to true to include details | 1665 * @param {boolean} details Set to true to include details |
1666 * @param {Object} options Options comtrolling the serialization | |
1667 * The following options can be set: | |
1668 * includeSource: include ths full source of scripts | |
1631 * @returns {MirrorSerializer} mirror serializer | 1669 * @returns {MirrorSerializer} mirror serializer |
1632 */ | 1670 */ |
1633 function MakeMirrorSerializer(details) { | 1671 function MakeMirrorSerializer(details, options) { |
1634 return new JSONProtocolSerializer(details); | 1672 return new JSONProtocolSerializer(details, options); |
1635 } | 1673 } |
1636 | 1674 |
1637 | 1675 |
1638 /** | 1676 /** |
1639 * Object for serializing a mirror objects and its direct references. | 1677 * Object for serializing a mirror objects and its direct references. |
1640 * @param {boolean} details Indicates whether to include details for the mirror | 1678 * @param {boolean} details Indicates whether to include details for the mirror |
1641 * serialized | 1679 * serialized |
1642 * @constructor | 1680 * @constructor |
1643 */ | 1681 */ |
1644 function JSONProtocolSerializer(details) { | 1682 function JSONProtocolSerializer(details, options) { |
1645 this.details_ = details; | 1683 this.details_ = details; |
1684 this.options_ = options; | |
1646 this.mirrors_ = [ ]; | 1685 this.mirrors_ = [ ]; |
1647 } | 1686 } |
1648 | 1687 |
1649 | 1688 |
1650 /** | 1689 /** |
1651 * Returns a serialization of an object reference. The referenced object are | 1690 * Returns a serialization of an object reference. The referenced object are |
1652 * added to the serialization state. | 1691 * added to the serialization state. |
1653 * | 1692 * |
1654 * @param {Mirror} mirror The mirror to serialize | 1693 * @param {Mirror} mirror The mirror to serialize |
1655 * @returns {String} JSON serialization | 1694 * @returns {String} JSON serialization |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1687 | 1726 |
1688 for (var i = 0; i < count; i++) { | 1727 for (var i = 0; i < count; i++) { |
1689 content.push(this.serialize_(this.mirrors_[i], false, false)); | 1728 content.push(this.serialize_(this.mirrors_[i], false, false)); |
1690 } | 1729 } |
1691 | 1730 |
1692 var json = ArrayToJSONArray_(content); | 1731 var json = ArrayToJSONArray_(content); |
1693 return json; | 1732 return json; |
1694 } | 1733 } |
1695 | 1734 |
1696 | 1735 |
1736 JSONProtocolSerializer.prototype.includeSource_ = function() { | |
1737 return this.options_ && this.options_.includeSource; | |
1738 } | |
1739 | |
1740 | |
1697 JSONProtocolSerializer.prototype.add_ = function(mirror) { | 1741 JSONProtocolSerializer.prototype.add_ = function(mirror) { |
1698 // If this mirror is already in the list just return. | 1742 // If this mirror is already in the list just return. |
1699 for (var i = 0; i < this.mirrors_.length; i++) { | 1743 for (var i = 0; i < this.mirrors_.length; i++) { |
1700 if (this.mirrors_[i] === mirror) { | 1744 if (this.mirrors_[i] === mirror) { |
1701 return; | 1745 return; |
1702 } | 1746 } |
1703 } | 1747 } |
1704 | 1748 |
1705 // Add the mirror to the list of mirrors to be serialized. | 1749 // Add the mirror to the list of mirrors to be serialized. |
1706 this.mirrors_.push(mirror); | 1750 this.mirrors_.push(mirror); |
1707 } | 1751 } |
1708 | 1752 |
1709 | 1753 |
1710 JSONProtocolSerializer.prototype.serialize_ = function(mirror, reference, | 1754 JSONProtocolSerializer.prototype.serialize_ = function(mirror, reference, |
1711 details) { | 1755 details) { |
1712 // If serializing a reference to a mirror just return the reference and add | 1756 // If serializing a reference to a mirror just return the reference and add |
1713 // the mirror to the referenced mirrors. | 1757 // the mirror to the referenced mirrors. |
1714 if (reference && | 1758 if (reference && |
1715 (mirror.isValue() || mirror.isScript())) { | 1759 (mirror.isValue() || mirror.isScript() || mirror.isContext())) { |
1716 this.add_(mirror); | 1760 this.add_(mirror); |
1717 return '{"ref":' + mirror.handle() + '}'; | 1761 return '{"ref":' + mirror.handle() + '}'; |
1718 } | 1762 } |
1719 | 1763 |
1720 // Collect the JSON property/value pairs in an array. | 1764 // Collect the JSON property/value pairs in an array. |
1721 var content = new Array(); | 1765 var content = new Array(); |
1722 | 1766 |
1723 // Add the mirror handle. | 1767 // Add the mirror handle. |
1724 if (mirror.isValue() || mirror.isScript()) { | 1768 if (mirror.isValue() || mirror.isScript() || mirror.isContext()) { |
1725 content.push(MakeJSONPair_('handle', NumberToJSON_(mirror.handle()))); | 1769 content.push(MakeJSONPair_('handle', NumberToJSON_(mirror.handle()))); |
1726 } | 1770 } |
1727 | 1771 |
1728 // Always add the type. | 1772 // Always add the type. |
1729 content.push(MakeJSONPair_('type', StringToJSON_(mirror.type()))); | 1773 content.push(MakeJSONPair_('type', StringToJSON_(mirror.type()))); |
1730 | 1774 |
1731 switch (mirror.type()) { | 1775 switch (mirror.type()) { |
1732 case UNDEFINED_TYPE: | 1776 case UNDEFINED_TYPE: |
1733 case NULL_TYPE: | 1777 case NULL_TYPE: |
1734 // Undefined and null are represented just by their type. | 1778 // Undefined and null are represented just by their type. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1780 if (mirror.name()) { | 1824 if (mirror.name()) { |
1781 content.push(MakeJSONPair_('name', StringToJSON_(mirror.name()))); | 1825 content.push(MakeJSONPair_('name', StringToJSON_(mirror.name()))); |
1782 } | 1826 } |
1783 content.push(MakeJSONPair_('id', NumberToJSON_(mirror.id()))); | 1827 content.push(MakeJSONPair_('id', NumberToJSON_(mirror.id()))); |
1784 content.push(MakeJSONPair_('lineOffset', | 1828 content.push(MakeJSONPair_('lineOffset', |
1785 NumberToJSON_(mirror.lineOffset()))); | 1829 NumberToJSON_(mirror.lineOffset()))); |
1786 content.push(MakeJSONPair_('columnOffset', | 1830 content.push(MakeJSONPair_('columnOffset', |
1787 NumberToJSON_(mirror.columnOffset()))); | 1831 NumberToJSON_(mirror.columnOffset()))); |
1788 content.push(MakeJSONPair_('lineCount', | 1832 content.push(MakeJSONPair_('lineCount', |
1789 NumberToJSON_(mirror.lineCount()))); | 1833 NumberToJSON_(mirror.lineCount()))); |
1834 if (mirror.data()) { | |
1835 content.push(MakeJSONPair_('data', JSON.stringify(mirror.data()))); | |
1836 } | |
1837 if (this.includeSource_()) { | |
1838 content.push(MakeJSONPair_('source', | |
1839 StringToJSON_(mirror.source()))); | |
1840 } else { | |
1841 var sourceStart = mirror.source().substring(0, 80); | |
1842 content.push(MakeJSONPair_('sourceStart', | |
1843 StringToJSON_(sourceStart))); | |
1844 } | |
1845 content.push(MakeJSONPair_('sourceLength', | |
yurys
2009/05/06 07:22:34
it seems that you can switch to JSON.stringify for
Søren Thygesen Gjesse
2009/05/06 07:48:54
Sure, that would clean up the code quite a bit.
| |
1846 NumberToJSON_(mirror.source().length))); | |
1790 content.push(MakeJSONPair_('scriptType', | 1847 content.push(MakeJSONPair_('scriptType', |
1791 NumberToJSON_(mirror.scriptType()))); | 1848 NumberToJSON_(mirror.scriptType()))); |
1849 if (mirror.context()) { | |
1850 content.push(MakeJSONPair_('context', | |
1851 this.serializeReference(mirror.context()))); | |
1852 } | |
1853 break; | |
1854 | |
1855 case CONTEXT_TYPE: | |
1856 content.push(MakeJSONPair_('data', JSON.stringify(mirror.data()))); | |
1792 break; | 1857 break; |
1793 } | 1858 } |
1794 | 1859 |
1795 // Always add the text representation. | 1860 // Always add the text representation. |
1796 content.push(MakeJSONPair_('text', StringToJSON_(mirror.toText()))); | 1861 content.push(MakeJSONPair_('text', StringToJSON_(mirror.toText()))); |
1797 | 1862 |
1798 // Create and return the JSON string. | 1863 // Create and return the JSON string. |
1799 return ArrayToJSONObject_(content); | 1864 return ArrayToJSONObject_(content); |
1800 } | 1865 } |
1801 | 1866 |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2085 /** | 2150 /** |
2086 * Convert a Date to ISO 8601 format. To avoid depending on the Date object | 2151 * Convert a Date to ISO 8601 format. To avoid depending on the Date object |
2087 * this method calls the functions in date.js directly and not through the | 2152 * this method calls the functions in date.js directly and not through the |
2088 * value. | 2153 * value. |
2089 * @param {Date} value The Date value to format as JSON | 2154 * @param {Date} value The Date value to format as JSON |
2090 * @return {string} JSON formatted Date value | 2155 * @return {string} JSON formatted Date value |
2091 */ | 2156 */ |
2092 function DateToJSON_(value) { | 2157 function DateToJSON_(value) { |
2093 return '"' + DateToISO8601_(value) + '"'; | 2158 return '"' + DateToISO8601_(value) + '"'; |
2094 } | 2159 } |
OLD | NEW |