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

Unified Diff: src/d8.js

Issue 12653003: More useful result pretty printing for d8 (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressed comments; minor fixes Created 7 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/d8.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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]";
+ }
+}
« no previous file with comments | « src/d8.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698