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

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

Issue 113399: Use JSON.stringify to serialize debugger messages (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 11 years, 7 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 | « no previous file | src/mirror-delay.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 815 matching lines...) Expand 10 before | Expand all | Expand 10 after
826 return this.break_points_hit_; 826 return this.break_points_hit_;
827 }; 827 };
828 828
829 829
830 BreakEvent.prototype.toJSONProtocol = function() { 830 BreakEvent.prototype.toJSONProtocol = function() {
831 var o = { seq: next_response_seq++, 831 var o = { seq: next_response_seq++,
832 type: "event", 832 type: "event",
833 event: "break", 833 event: "break",
834 body: { invocationText: this.exec_state_.frame(0).invocationText(), 834 body: { invocationText: this.exec_state_.frame(0).invocationText(),
835 } 835 }
836 } 836 };
837 837
838 // Add script related information to the event if available. 838 // Add script related information to the event if available.
839 var script = this.func().script(); 839 var script = this.func().script();
840 if (script) { 840 if (script) {
841 o.body.sourceLine = this.sourceLine(), 841 o.body.sourceLine = this.sourceLine(),
842 o.body.sourceColumn = this.sourceColumn(), 842 o.body.sourceColumn = this.sourceColumn(),
843 o.body.sourceLineText = this.sourceLineText(), 843 o.body.sourceLineText = this.sourceLineText(),
844 o.body.script = MakeScriptObject_(script, false); 844 o.body.script = MakeScriptObject_(script, false);
845 } 845 }
846 846
847 // Add an Array of break points hit if any. 847 // Add an Array of break points hit if any.
848 if (this.breakPointsHit()) { 848 if (this.breakPointsHit()) {
849 o.body.breakpoints = []; 849 o.body.breakpoints = [];
850 for (var i = 0; i < this.breakPointsHit().length; i++) { 850 for (var i = 0; i < this.breakPointsHit().length; i++) {
851 // Find the break point number. For break points originating from a 851 // Find the break point number. For break points originating from a
852 // script break point supply the script break point number. 852 // script break point supply the script break point number.
853 var breakpoint = this.breakPointsHit()[i]; 853 var breakpoint = this.breakPointsHit()[i];
854 var script_break_point = breakpoint.script_break_point(); 854 var script_break_point = breakpoint.script_break_point();
855 var number; 855 var number;
856 if (script_break_point) { 856 if (script_break_point) {
857 number = script_break_point.number(); 857 number = script_break_point.number();
858 } else { 858 } else {
859 number = breakpoint.number(); 859 number = breakpoint.number();
860 } 860 }
861 o.body.breakpoints.push(number); 861 o.body.breakpoints.push(number);
862 } 862 }
863 } 863 }
864 864 return JSON.stringify(ObjectToProtocolObject_(o));
865 return SimpleObjectToJSON_(o);
866 }; 865 };
867 866
868 867
869 function MakeExceptionEvent(exec_state, exception, uncaught) { 868 function MakeExceptionEvent(exec_state, exception, uncaught) {
870 return new ExceptionEvent(exec_state, exception, uncaught); 869 return new ExceptionEvent(exec_state, exception, uncaught);
871 } 870 }
872 871
873 872
874 function ExceptionEvent(exec_state, exception, uncaught) { 873 function ExceptionEvent(exec_state, exception, uncaught) {
875 this.exec_state_ = exec_state; 874 this.exec_state_ = exec_state;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
916 ExceptionEvent.prototype.sourceLineText = function() { 915 ExceptionEvent.prototype.sourceLineText = function() {
917 return this.exec_state_.frame(0).sourceLineText(); 916 return this.exec_state_.frame(0).sourceLineText();
918 }; 917 };
919 918
920 919
921 ExceptionEvent.prototype.toJSONProtocol = function() { 920 ExceptionEvent.prototype.toJSONProtocol = function() {
922 var o = new ProtocolMessage(); 921 var o = new ProtocolMessage();
923 o.event = "exception"; 922 o.event = "exception";
924 o.body = { uncaught: this.uncaught_, 923 o.body = { uncaught: this.uncaught_,
925 exception: MakeMirror(this.exception_) 924 exception: MakeMirror(this.exception_)
926 } 925 };
927 926
928 // Exceptions might happen whithout any JavaScript frames. 927 // Exceptions might happen whithout any JavaScript frames.
929 if (this.exec_state_.frameCount() > 0) { 928 if (this.exec_state_.frameCount() > 0) {
930 o.body.sourceLine = this.sourceLine(); 929 o.body.sourceLine = this.sourceLine();
931 o.body.sourceColumn = this.sourceColumn(); 930 o.body.sourceColumn = this.sourceColumn();
932 o.body.sourceLineText = this.sourceLineText(); 931 o.body.sourceLineText = this.sourceLineText();
933 932
934 // Add script information to the event if available. 933 // Add script information to the event if available.
935 var script = this.func().script(); 934 var script = this.func().script();
936 if (script) { 935 if (script) {
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
1072 1071
1073 1072
1074 ProtocolMessage.prototype.failed = function(message) { 1073 ProtocolMessage.prototype.failed = function(message) {
1075 this.success = false; 1074 this.success = false;
1076 this.message = message; 1075 this.message = message;
1077 } 1076 }
1078 1077
1079 1078
1080 ProtocolMessage.prototype.toJSONProtocol = function() { 1079 ProtocolMessage.prototype.toJSONProtocol = function() {
1081 // Encode the protocol header. 1080 // Encode the protocol header.
1082 var json = '{'; 1081 var json = {};
1083 json += '"seq":' + this.seq; 1082 json.seq= this.seq;
1084 if (this.request_seq) { 1083 if (this.request_seq) {
1085 json += ',"request_seq":' + this.request_seq; 1084 json.request_seq = this.request_seq;
1086 } 1085 }
1087 json += ',"type":"' + this.type + '"'; 1086 json.type = this.type;
1088 if (this.event) { 1087 if (this.event) {
1089 json += ',"event":' + StringToJSON_(this.event); 1088 json.event = this.event;
1090 } 1089 }
1091 if (this.command) { 1090 if (this.command) {
1092 json += ',"command":' + StringToJSON_(this.command); 1091 json.command = this.command;
1093 } 1092 }
1094 if (this.success) { 1093 if (this.success) {
1095 json += ',"success":' + this.success; 1094 json.success = this.success;
1096 } else { 1095 } else {
1097 json += ',"success":false'; 1096 json.success = false;
1098 } 1097 }
1099 if (this.body) { 1098 if (this.body) {
1100 json += ',"body":';
1101 // Encode the body part. 1099 // Encode the body part.
1100 var bodyJson;
1102 var serializer = MakeMirrorSerializer(true, this.options_); 1101 var serializer = MakeMirrorSerializer(true, this.options_);
1103 if (this.body instanceof Mirror) { 1102 if (this.body instanceof Mirror) {
1104 json += serializer.serializeValue(this.body); 1103 bodyJson = serializer.serializeValue(this.body);
1105 } else if (this.body instanceof Array) { 1104 } else if (this.body instanceof Array) {
1106 json += '['; 1105 bodyJson = [];
1107 for (var i = 0; i < this.body.length; i++) { 1106 for (var i = 0; i < this.body.length; i++) {
1108 if (i != 0) json += ',';
1109 if (this.body[i] instanceof Mirror) { 1107 if (this.body[i] instanceof Mirror) {
1110 json += serializer.serializeValue(this.body[i]); 1108 bodyJson.push(serializer.serializeValue(this.body[i]));
1111 } else { 1109 } else {
1112 json += SimpleObjectToJSON_(this.body[i], serializer); 1110 bodyJson.push(ObjectToProtocolObject_(this.body[i], serializer));
1113 } 1111 }
1114 } 1112 }
1115 json += ']';
1116 } else { 1113 } else {
1117 json += SimpleObjectToJSON_(this.body, serializer); 1114 bodyJson = ObjectToProtocolObject_(this.body, serializer);
1118 } 1115 }
1119 json += ',"refs":'; 1116 json.body = bodyJson;
1120 json += serializer.serializeReferencedObjects(); 1117 json.refs = serializer.serializeReferencedObjects();
1121 } 1118 }
1122 if (this.message) { 1119 if (this.message) {
1123 json += ',"message":' + StringToJSON_(this.message) ; 1120 json.message = this.message;
1124 } 1121 }
1125 if (this.running) { 1122 if (this.running) {
1126 json += ',"running":true'; 1123 json.running = true;
1127 } else { 1124 } else {
1128 json += ',"running":false'; 1125 json.running = false;
1129 } 1126 }
1130 json += '}'; 1127 return JSON.stringify(json);
1131 return json;
1132 } 1128 }
1133 1129
1134 1130
1135 DebugCommandProcessor.prototype.createResponse = function(request) { 1131 DebugCommandProcessor.prototype.createResponse = function(request) {
1136 return new ProtocolMessage(request); 1132 return new ProtocolMessage(request);
1137 }; 1133 };
1138 1134
1139 1135
1140 DebugCommandProcessor.prototype.processDebugJSONRequest = function(json_request) { 1136 DebugCommandProcessor.prototype.processDebugJSONRequest = function(json_request) {
1141 var request; // Current request. 1137 var request; // Current request.
(...skipping 650 matching lines...) Expand 10 before | Expand all | Expand 10 after
1792 var result = ""; 1788 var result = "";
1793 result += "0x" + NumberToHex8Str(cframe_value.address); 1789 result += "0x" + NumberToHex8Str(cframe_value.address);
1794 if (!IS_UNDEFINED(cframe_value.text)) { 1790 if (!IS_UNDEFINED(cframe_value.text)) {
1795 result += " " + cframe_value.text; 1791 result += " " + cframe_value.text;
1796 } 1792 }
1797 return result; 1793 return result;
1798 } 1794 }
1799 1795
1800 1796
1801 /** 1797 /**
1802 * Convert an Object to its JSON representation (see http://www.json.org/). 1798 * Convert an Object to its debugger protocol representation. The representation
1803 * This implementation simply runs through all string property names and adds 1799 * may be serilized to a JSON object using JSON.stringify().
1804 * each property to the JSON representation for some predefined types. For type 1800 * This implementation simply runs through all string property names, converts
1805 * "object" the function calls itself recursively unless the object has the 1801 * each property value to a protocol value and adds the property to the result
1806 * function property "toJSONProtocol" in which case that is used. This is not 1802 * object. For type "object" the function will be called recursively. Note that
1807 * a general implementation but sufficient for the debugger. Note that circular 1803 * circular structures will cause infinite recursion.
1808 * structures will cause infinite recursion. 1804 * @param {Object} object The object to format as protocol object.
1809 * @param {Object} object The object to format as JSON
1810 * @param {MirrorSerializer} mirror_serializer The serializer to use if any 1805 * @param {MirrorSerializer} mirror_serializer The serializer to use if any
1811 * mirror objects are encountered. 1806 * mirror objects are encountered.
1812 * @return {string} JSON formatted object value 1807 * @return {Object} Protocol object value.
1813 */ 1808 */
1814 function SimpleObjectToJSON_(object, mirror_serializer) { 1809 function ObjectToProtocolObject_(object, mirror_serializer) {
1815 var content = []; 1810 var content = {};
1816 for (var key in object) { 1811 for (var key in object) {
1817 // Only consider string keys. 1812 // Only consider string keys.
1818 if (typeof key == 'string') { 1813 if (typeof key == 'string') {
1819 var property_value = object[key];
1820
1821 // Format the value based on its type. 1814 // Format the value based on its type.
1822 var property_value_json; 1815 var property_value_json = ValueToProtocolValue_(object[key],
1823 switch (typeof property_value) { 1816 mirror_serializer);
1824 case 'object':
1825 if (property_value instanceof Mirror) {
1826 property_value_json = mirror_serializer.serializeValue(property_valu e);
1827 } else if (typeof property_value.toJSONProtocol == 'function') {
1828 property_value_json = property_value.toJSONProtocol(true)
1829 } else if (IS_ARRAY(property_value)){
1830 property_value_json = SimpleArrayToJSON_(property_value, mirror_seri alizer);
1831 } else {
1832 property_value_json = SimpleObjectToJSON_(property_value, mirror_ser ializer);
1833 }
1834 break;
1835
1836 case 'boolean':
1837 property_value_json = BooleanToJSON_(property_value);
1838 break;
1839
1840 case 'number':
1841 property_value_json = NumberToJSON_(property_value);
1842 break;
1843
1844 case 'string':
1845 property_value_json = StringToJSON_(property_value);
1846 break;
1847
1848 default:
1849 property_value_json = null;
1850 }
1851
1852 // Add the property if relevant. 1817 // Add the property if relevant.
1853 if (property_value_json) { 1818 if (!IS_UNDEFINED(property_value_json)) {
1854 content.push(StringToJSON_(key) + ':' + property_value_json); 1819 content[key] = property_value_json;
1855 } 1820 }
1856 } 1821 }
1857 } 1822 }
1858 1823
1859 // Make JSON object representation. 1824 return content;
1860 return '{' + content.join(',') + '}';
1861 } 1825 }
1862 1826
1827
1863 /** 1828 /**
1864 * Convert an array to its JSON representation. This is a VERY simple 1829 * Convert an array to its debugger protocol representation. It will convert
1865 * implementation just to support what is needed for the debugger. 1830 * each array element to a protocol value.
1866 * @param {Array} array The array to format as JSON 1831 * @param {Array} array The array to format as protocol array.
1867 * @param {MirrorSerializer} mirror_serializer The serializer to use if any 1832 * @param {MirrorSerializer} mirror_serializer The serializer to use if any
1868 * mirror objects are encountered. 1833 * mirror objects are encountered.
1869 * @return {string} JSON formatted array value 1834 * @return {Array} Protocol array value.
1870 */ 1835 */
1871 function SimpleArrayToJSON_(array, mirror_serializer) { 1836 function ArrayToProtocolArray_(array, mirror_serializer) {
1872 // Make JSON array representation. 1837 var json = [];
1873 var json = '[';
1874 for (var i = 0; i < array.length; i++) { 1838 for (var i = 0; i < array.length; i++) {
1875 if (i != 0) { 1839 json.push(ValueToProtocolValue_(array[i], mirror_serializer));
1876 json += ',';
1877 }
1878 var elem = array[i];
1879 if (elem instanceof Mirror) {
1880 json += mirror_serializer.serializeValue(elem);
1881 } else if (IS_OBJECT(elem)) {
1882 json += SimpleObjectToJSON_(elem);
1883 } else if (IS_BOOLEAN(elem)) {
1884 json += BooleanToJSON_(elem);
1885 } else if (IS_NUMBER(elem)) {
1886 json += NumberToJSON_(elem);
1887 } else if (IS_STRING(elem)) {
1888 json += StringToJSON_(elem);
1889 } else {
1890 json += elem;
1891 }
1892 } 1840 }
1893 json += ']';
1894 return json; 1841 return json;
1895 } 1842 }
1843
1844
1845 /**
1846 * Convert a value to its debugger protocol representation.
1847 * @param {*} value The value to format as protocol value.
1848 * @param {MirrorSerializer} mirror_serializer The serializer to use if any
1849 * mirror objects are encountered.
1850 * @return {*} Protocol value.
1851 */
1852 function ValueToProtocolValue_(value, mirror_serializer) {
1853 // Format the value based on its type.
1854 var json;
1855 switch (typeof value) {
1856 case 'object':
1857 if (value instanceof Mirror) {
1858 json = mirror_serializer.serializeValue(value);
1859 } else if (IS_ARRAY(value)){
1860 json = ArrayToProtocolArray_(value, mirror_serializer);
1861 } else {
1862 json = ObjectToProtocolObject_(value, mirror_serializer);
1863 }
1864 break;
1865
1866 case 'boolean':
1867 case 'string':
1868 case 'number':
1869 json = value;
1870 break
1871
1872 default:
1873 json = null;
1874 }
1875 return json;
1876 }
OLDNEW
« no previous file with comments | « no previous file | src/mirror-delay.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698