Index: src/d8.js |
diff --git a/src/d8.js b/src/d8.js |
index 6a4ecb73304726203d037ddf38891a6bdb7a2fa1..1ff0ce8980dda2ed8843364eac299931a9a3dfec 100644 |
--- a/src/d8.js |
+++ b/src/d8.js |
@@ -2194,3 +2194,59 @@ function SimpleArrayToJSON_(array) { |
json += ']'; |
return json; |
} |
+ |
+ |
+// A more universal stringify that supports more types than JSON. |
+// Used by the d8 shell to output results. |
+var stringifyDepthLimit = 4; // To avoid crashing on cyclic objects |
+ |
+function Stringify(x, depth) { |
+ if (depth === undefined) |
+ depth = stringifyDepthLimit; |
+ else if (depth === 0) |
+ return "*"; |
+ switch (typeof x) { |
+ case "undefined": |
+ return "undefined"; |
+ case "boolean": |
+ case "number": |
+ case "function": |
+ return x.toString(); |
+ case "string": |
+ return "\"" + x.toString() + "\""; |
+ // TODO(rossberg): add symbol case |
+ case "object": |
+ if (x === null) return "null"; |
+ if (x.constructor && x.constructor.name === "Array") { |
+ var elems = []; |
+ for (var i = 0; i < x.length; ++i) { |
+ elems.push( |
+ {}.hasOwnProperty.call(x, i) ? Stringify(x[i], depth - 1) : ""); |
+ } |
+ return "[" + elems.join(", ") + "]"; |
+ } |
+ try { |
+ var string = String(x); |
+ if (string && string !== "[object Object]") return string; |
+ } catch(e) {} |
+ var props = []; |
+ for (var name in x) { |
+ var desc = Object.getOwnPropertyDescriptor(x, name); |
+ if (desc === void 0) continue; |
+ if ("value" in desc) { |
+ props.push(name + ": " + Stringify(desc.value, depth - 1)); |
+ } |
+ if ("get" in desc) { |
+ var getter = desc.get.toString(); |
+ props.push("get " + name + getter.slice(getter.indexOf('('))); |
+ } |
+ if ("set" in desc) { |
+ var setter = desc.set.toString(); |
+ props.push("set " + name + setter.slice(setter.indexOf('('))); |
+ } |
+ } |
+ return "{" + props.join(", ") + "}"; |
+ default: |
+ return "[crazy non-standard shit]"; |
+ } |
+} |