OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 "use strict"; | 5 "use strict"; |
6 | 6 |
7 String.prototype.startsWith = function (str) { | 7 String.prototype.startsWith = function (str) { |
8 if (str.length > this.length) { | 8 if (str.length > this.length) { |
9 return false; | 9 return false; |
10 } | 10 } |
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 this.command = command; | 496 this.command = command; |
497 } | 497 } |
498 | 498 |
499 | 499 |
500 RequestPacket.prototype.toJSONProtocol = function() { | 500 RequestPacket.prototype.toJSONProtocol = function() { |
501 // Encode the protocol header. | 501 // Encode the protocol header. |
502 var json = '{'; | 502 var json = '{'; |
503 json += '"seq":' + this.seq; | 503 json += '"seq":' + this.seq; |
504 json += ',"type":"' + this.type + '"'; | 504 json += ',"type":"' + this.type + '"'; |
505 if (this.command) { | 505 if (this.command) { |
506 json += ',"command":' + StringToJSON_(this.command); | 506 json += ',"command":' + JSON.stringify(this.command); |
507 } | 507 } |
508 if (this.arguments) { | 508 if (this.arguments) { |
509 json += ',"arguments":'; | 509 json += ',"arguments":'; |
510 // Encode the arguments part. | 510 // Encode the arguments part. |
511 if (this.arguments.toJSONProtocol) { | 511 if (this.arguments.toJSONProtocol) { |
512 json += this.arguments.toJSONProtocol(); | 512 json += this.arguments.toJSONProtocol(); |
513 } else { | 513 } else { |
514 json += SimpleObjectToJSON_(this.arguments); | 514 json += JSON.stringify(this.arguments); |
515 } | 515 } |
516 } | 516 } |
517 json += '}'; | 517 json += '}'; |
518 return json; | 518 return json; |
519 }; | 519 }; |
520 | 520 |
521 | 521 |
522 DebugRequest.prototype.createRequest = function(command) { | 522 DebugRequest.prototype.createRequest = function(command) { |
523 return new RequestPacket(command); | 523 return new RequestPacket(command); |
524 }; | 524 }; |
(...skipping 1433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1958 function ProtocolReference(handle) { | 1958 function ProtocolReference(handle) { |
1959 this.handle_ = handle; | 1959 this.handle_ = handle; |
1960 } | 1960 } |
1961 | 1961 |
1962 | 1962 |
1963 ProtocolReference.prototype.handle = function() { | 1963 ProtocolReference.prototype.handle = function() { |
1964 return this.handle_; | 1964 return this.handle_; |
1965 }; | 1965 }; |
1966 | 1966 |
1967 | 1967 |
1968 function MakeJSONPair_(name, value) { | |
1969 return '"' + name + '":' + value; | |
1970 } | |
1971 | |
1972 | |
1973 function ArrayToJSONObject_(content) { | |
1974 return '{' + content.join(',') + '}'; | |
1975 } | |
1976 | |
1977 | |
1978 function ArrayToJSONArray_(content) { | |
1979 return '[' + content.join(',') + ']'; | |
1980 } | |
1981 | |
1982 | |
1983 function BooleanToJSON_(value) { | |
1984 return String(value); | |
1985 } | |
1986 | |
1987 | |
1988 function NumberToJSON_(value) { | |
1989 return String(value); | |
1990 } | |
1991 | |
1992 | |
1993 // Mapping of some control characters to avoid the \uXXXX syntax for most | |
1994 // commonly used control cahracters. | |
1995 var ctrlCharMap_ = { | |
1996 '\b': '\\b', | |
1997 '\t': '\\t', | |
1998 '\n': '\\n', | |
1999 '\f': '\\f', | |
2000 '\r': '\\r', | |
2001 '"' : '\\"', | |
2002 '\\': '\\\\' | |
2003 }; | |
2004 | |
2005 | |
2006 // Regular expression testing for ", \ and control characters (0x00 - 0x1F). | |
2007 var ctrlCharTest_ = new RegExp('["\\\\\x00-\x1F]'); | |
2008 | |
2009 | |
2010 // Regular expression matching ", \ and control characters (0x00 - 0x1F) | |
2011 // globally. | |
2012 var ctrlCharMatch_ = new RegExp('["\\\\\x00-\x1F]', 'g'); | |
2013 | |
2014 | |
2015 /** | |
2016 * Convert a String to its JSON representation (see http://www.json.org/). To | |
2017 * avoid depending on the String object this method calls the functions in | |
2018 * string.js directly and not through the value. | |
2019 * @param {String} value The String value to format as JSON | |
2020 * @return {string} JSON formatted String value | |
2021 */ | |
2022 function StringToJSON_(value) { | |
2023 // Check for" , \ and control characters (0x00 - 0x1F). No need to call | |
2024 // RegExpTest as ctrlchar is constructed using RegExp. | |
2025 if (ctrlCharTest_.test(value)) { | |
2026 // Replace ", \ and control characters (0x00 - 0x1F). | |
2027 return '"' + | |
2028 value.replace(ctrlCharMatch_, function (char) { | |
2029 // Use charmap if possible. | |
2030 var mapped = ctrlCharMap_[char]; | |
2031 if (mapped) return mapped; | |
2032 mapped = char.charCodeAt(); | |
2033 // Convert control character to unicode escape sequence. | |
2034 return '\\u00' + | |
2035 '0' + // TODO %NumberToRadixString(Math.floor(mapped / 16), 16) + | |
2036 '0'; // TODO %NumberToRadixString(mapped % 16, 16) | |
2037 }) | |
2038 + '"'; | |
2039 } | |
2040 | |
2041 // Simple string with no special characters. | |
2042 return '"' + value + '"'; | |
2043 } | |
2044 | |
2045 | |
2046 /** | |
2047 * Convert a Date to ISO 8601 format. To avoid depending on the Date object | |
2048 * this method calls the functions in date.js directly and not through the | |
2049 * value. | |
2050 * @param {Date} value The Date value to format as JSON | |
2051 * @return {string} JSON formatted Date value | |
2052 */ | |
2053 function DateToISO8601_(value) { | |
2054 var f = function(n) { | |
2055 return n < 10 ? '0' + n : n; | |
2056 }; | |
2057 var g = function(n) { | |
2058 return n < 10 ? '00' + n : n < 100 ? '0' + n : n; | |
2059 }; | |
2060 return builtins.GetUTCFullYearFrom(value) + '-' + | |
2061 f(builtins.GetUTCMonthFrom(value) + 1) + '-' + | |
2062 f(builtins.GetUTCDateFrom(value)) + 'T' + | |
2063 f(builtins.GetUTCHoursFrom(value)) + ':' + | |
2064 f(builtins.GetUTCMinutesFrom(value)) + ':' + | |
2065 f(builtins.GetUTCSecondsFrom(value)) + '.' + | |
2066 g(builtins.GetUTCMillisecondsFrom(value)) + 'Z'; | |
2067 } | |
2068 | |
2069 | |
2070 /** | |
2071 * Convert a Date to ISO 8601 format. To avoid depending on the Date object | |
2072 * this method calls the functions in date.js directly and not through the | |
2073 * value. | |
2074 * @param {Date} value The Date value to format as JSON | |
2075 * @return {string} JSON formatted Date value | |
2076 */ | |
2077 function DateToJSON_(value) { | |
2078 return '"' + DateToISO8601_(value) + '"'; | |
2079 } | |
2080 | |
2081 | |
2082 /** | |
2083 * Convert an Object to its JSON representation (see http://www.json.org/). | |
2084 * This implementation simply runs through all string property names and adds | |
2085 * each property to the JSON representation for some predefined types. For type | |
2086 * "object" the function calls itself recursively unless the object has the | |
2087 * function property "toJSONProtocol" in which case that is used. This is not | |
2088 * a general implementation but sufficient for the debugger. Note that circular | |
2089 * structures will cause infinite recursion. | |
2090 * @param {Object} object The object to format as JSON | |
2091 * @return {string} JSON formatted object value | |
2092 */ | |
2093 function SimpleObjectToJSON_(object) { | |
2094 var content = []; | |
2095 for (var key in object) { | |
2096 // Only consider string keys. | |
2097 if (typeof key == 'string') { | |
2098 var property_value = object[key]; | |
2099 | |
2100 // Format the value based on its type. | |
2101 var property_value_json; | |
2102 switch (typeof property_value) { | |
2103 case 'object': | |
2104 if (IS_NULL(property_value)) { | |
2105 property_value_json = 'null'; | |
2106 } else if (typeof property_value.toJSONProtocol == 'function') { | |
2107 property_value_json = property_value.toJSONProtocol(true); | |
2108 } else if (property_value.constructor.name == 'Array'){ | |
2109 property_value_json = SimpleArrayToJSON_(property_value); | |
2110 } else { | |
2111 property_value_json = SimpleObjectToJSON_(property_value); | |
2112 } | |
2113 break; | |
2114 | |
2115 case 'boolean': | |
2116 property_value_json = BooleanToJSON_(property_value); | |
2117 break; | |
2118 | |
2119 case 'number': | |
2120 property_value_json = NumberToJSON_(property_value); | |
2121 break; | |
2122 | |
2123 case 'string': | |
2124 property_value_json = StringToJSON_(property_value); | |
2125 break; | |
2126 | |
2127 default: | |
2128 property_value_json = null; | |
2129 } | |
2130 | |
2131 // Add the property if relevant. | |
2132 if (property_value_json) { | |
2133 content.push(StringToJSON_(key) + ':' + property_value_json); | |
2134 } | |
2135 } | |
2136 } | |
2137 | |
2138 // Make JSON object representation. | |
2139 return '{' + content.join(',') + '}'; | |
2140 } | |
2141 | |
2142 | |
2143 /** | |
2144 * Convert an array to its JSON representation. This is a VERY simple | |
2145 * implementation just to support what is needed for the debugger. | |
2146 * @param {Array} arrya The array to format as JSON | |
2147 * @return {string} JSON formatted array value | |
2148 */ | |
2149 function SimpleArrayToJSON_(array) { | |
2150 // Make JSON array representation. | |
2151 var json = '['; | |
2152 for (var i = 0; i < array.length; i++) { | |
2153 if (i != 0) { | |
2154 json += ','; | |
2155 } | |
2156 var elem = array[i]; | |
2157 if (elem.toJSONProtocol) { | |
2158 json += elem.toJSONProtocol(true); | |
2159 } else if (typeof(elem) === 'object') { | |
2160 json += SimpleObjectToJSON_(elem); | |
2161 } else if (typeof(elem) === 'boolean') { | |
2162 json += BooleanToJSON_(elem); | |
2163 } else if (typeof(elem) === 'number') { | |
2164 json += NumberToJSON_(elem); | |
2165 } else if (typeof(elem) === 'string') { | |
2166 json += StringToJSON_(elem); | |
2167 } else { | |
2168 json += elem; | |
2169 } | |
2170 } | |
2171 json += ']'; | |
2172 return json; | |
2173 } | |
2174 | |
2175 | |
2176 // A more universal stringify that supports more types than JSON. | 1968 // A more universal stringify that supports more types than JSON. |
2177 // Used by the d8 shell to output results. | 1969 // Used by the d8 shell to output results. |
2178 var stringifyDepthLimit = 4; // To avoid crashing on cyclic objects | 1970 var stringifyDepthLimit = 4; // To avoid crashing on cyclic objects |
2179 | 1971 |
2180 function Stringify(x, depth) { | 1972 function Stringify(x, depth) { |
2181 if (depth === undefined) | 1973 if (depth === undefined) |
2182 depth = stringifyDepthLimit; | 1974 depth = stringifyDepthLimit; |
2183 else if (depth === 0) | 1975 else if (depth === 0) |
2184 return "*"; | 1976 return "*"; |
2185 switch (typeof x) { | 1977 switch (typeof x) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2221 if ("set" in desc) { | 2013 if ("set" in desc) { |
2222 var setter = desc.set.toString(); | 2014 var setter = desc.set.toString(); |
2223 props.push("set " + name + setter.slice(setter.indexOf('('))); | 2015 props.push("set " + name + setter.slice(setter.indexOf('('))); |
2224 } | 2016 } |
2225 } | 2017 } |
2226 return "{" + props.join(", ") + "}"; | 2018 return "{" + props.join(", ") + "}"; |
2227 default: | 2019 default: |
2228 return "[crazy non-standard shit]"; | 2020 return "[crazy non-standard shit]"; |
2229 } | 2021 } |
2230 } | 2022 } |
OLD | NEW |