OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 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 2176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2187 json += NumberToJSON_(elem); | 2187 json += NumberToJSON_(elem); |
2188 } else if (typeof(elem) === 'string') { | 2188 } else if (typeof(elem) === 'string') { |
2189 json += StringToJSON_(elem); | 2189 json += StringToJSON_(elem); |
2190 } else { | 2190 } else { |
2191 json += elem; | 2191 json += elem; |
2192 } | 2192 } |
2193 } | 2193 } |
2194 json += ']'; | 2194 json += ']'; |
2195 return json; | 2195 return json; |
2196 } | 2196 } |
| 2197 |
| 2198 |
| 2199 // A more universal stringify that supports more types than JSON. |
| 2200 // Used by the d8 shell to output results. |
| 2201 var stringifyDepthLimit = 4; // To avoid crashing on cyclic objects |
| 2202 |
| 2203 function Stringify(x, depth) { |
| 2204 if (depth === undefined) |
| 2205 depth = stringifyDepthLimit; |
| 2206 else if (depth === 0) |
| 2207 return "*"; |
| 2208 switch (typeof x) { |
| 2209 case "undefined": |
| 2210 return "undefined"; |
| 2211 case "boolean": |
| 2212 case "number": |
| 2213 case "function": |
| 2214 return x.toString(); |
| 2215 case "string": |
| 2216 return "\"" + x.toString() + "\""; |
| 2217 // TODO(rossberg): add symbol case |
| 2218 case "object": |
| 2219 if (x === null) return "null"; |
| 2220 if (x.constructor && x.constructor.name === "Array") { |
| 2221 var elems = []; |
| 2222 for (var i = 0; i < x.length; ++i) { |
| 2223 elems.push( |
| 2224 {}.hasOwnProperty.call(x, i) ? Stringify(x[i], depth - 1) : ""); |
| 2225 } |
| 2226 return "[" + elems.join(", ") + "]"; |
| 2227 } |
| 2228 try { |
| 2229 var string = String(x); |
| 2230 if (string && string !== "[object Object]") return string; |
| 2231 } catch(e) {} |
| 2232 var props = []; |
| 2233 for (var name in x) { |
| 2234 var desc = Object.getOwnPropertyDescriptor(x, name); |
| 2235 if (desc === void 0) continue; |
| 2236 if ("value" in desc) { |
| 2237 props.push(name + ": " + Stringify(desc.value, depth - 1)); |
| 2238 } |
| 2239 if ("get" in desc) { |
| 2240 var getter = desc.get.toString(); |
| 2241 props.push("get " + name + getter.slice(getter.indexOf('('))); |
| 2242 } |
| 2243 if ("set" in desc) { |
| 2244 var setter = desc.set.toString(); |
| 2245 props.push("set " + name + setter.slice(setter.indexOf('('))); |
| 2246 } |
| 2247 } |
| 2248 return "{" + props.join(", ") + "}"; |
| 2249 default: |
| 2250 return "[crazy non-standard shit]"; |
| 2251 } |
| 2252 } |
OLD | NEW |