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 = 12; // To avoid crashing on cyclic objects | |
Yang
2013/03/12 12:56:41
You could also use a stack to detect cycles.
rossberg
2013/03/18 14:03:07
I'd need an object set to do it properly, which I
| |
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 case "object": | |
2218 if (x === null) return "null"; | |
2219 if (x.constructor.name === "Array") { | |
2220 var elems = []; | |
2221 for (var i = 0; i < x.length; ++i) { | |
2222 elems.push(x.hasOwnProperty(i) ? Stringify(x[i], depth - 1) : ""); | |
Yang
2013/03/12 12:56:41
Would it make sense to try to avoid monkey-patched
rossberg
2013/03/18 14:03:07
There are many ways in which this code could break
| |
2223 } | |
2224 return "[" + elems.join(", ") + "]"; | |
2225 } | |
2226 if (String(x) !== "[object Object]") return String(x); | |
2227 var props = []; | |
2228 for (var name in x) { | |
2229 var desc = Object.getOwnPropertyDescriptor(x, name); | |
2230 if (desc === undefined) continue; | |
2231 if ("value" in desc) { | |
2232 props.push(name + ": " + Stringify(desc.value, depth - 1)); | |
2233 } | |
2234 if ("get" in desc) { | |
2235 var getter = desc.get.toString(); | |
2236 props.push("get " + name + getter.slice(getter.indexOf('('))); | |
2237 } | |
2238 if ("set" in desc) { | |
2239 var setter = desc.set.toString(); | |
2240 props.push("set " + name + setter.slice(setter.indexOf('('))); | |
2241 } | |
2242 } | |
2243 return "{" + props.join(", ") + "}"; | |
2244 default: | |
2245 return "[crazy non-standard shit]"; | |
2246 } | |
2247 } | |
OLD | NEW |