2008-10-21 09:12:27 +00:00
|
|
|
// Copyright 2008 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-10-21 09:12:27 +00:00
|
|
|
|
2012-02-20 13:48:24 +00:00
|
|
|
"use strict";
|
|
|
|
|
More useful result pretty printing for d8
For example:
d8> ["hi", 5, [2,3],Object.prototype.toString, {get a() { return a}, b: 9, set a(x) {this.b=x}}, null, undefined]
["hi", 5, [2, 3], function toString() { [native code] }, {get a() { return a}, set a(x) {this.b=x}, b: 9}, null, undefined]
Deactivated in test mode.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/12653003
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13966 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-03-18 14:03:15 +00:00
|
|
|
// 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
|
|
|
|
|
2015-12-22 13:49:23 +00:00
|
|
|
// Hacky solution to circumvent forcing --allow-natives-syntax for d8
|
|
|
|
function isProxy(o) { return false };
|
|
|
|
function JSProxyGetTarget(proxy) { };
|
|
|
|
function JSProxyGetHandler(proxy) { };
|
|
|
|
|
|
|
|
try {
|
|
|
|
isProxy = Function(['object'], 'return %_IsJSProxy(object)');
|
|
|
|
JSProxyGetTarget = Function(['proxy'],
|
|
|
|
'return %JSProxyGetTarget(proxy)');
|
|
|
|
JSProxyGetHandler = Function(['proxy'],
|
|
|
|
'return %JSProxyGetHandler(proxy)');
|
|
|
|
} catch(e) {};
|
|
|
|
|
|
|
|
|
More useful result pretty printing for d8
For example:
d8> ["hi", 5, [2,3],Object.prototype.toString, {get a() { return a}, b: 9, set a(x) {this.b=x}}, null, undefined]
["hi", 5, [2, 3], function toString() { [native code] }, {get a() { return a}, set a(x) {this.b=x}, b: 9}, null, undefined]
Deactivated in test mode.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/12653003
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13966 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-03-18 14:03:15 +00:00
|
|
|
function Stringify(x, depth) {
|
|
|
|
if (depth === undefined)
|
|
|
|
depth = stringifyDepthLimit;
|
|
|
|
else if (depth === 0)
|
2015-12-22 13:49:23 +00:00
|
|
|
return "...";
|
|
|
|
if (isProxy(x)) {
|
|
|
|
return StringifyProxy(x, depth);
|
|
|
|
}
|
More useful result pretty printing for d8
For example:
d8> ["hi", 5, [2,3],Object.prototype.toString, {get a() { return a}, b: 9, set a(x) {this.b=x}}, null, undefined]
["hi", 5, [2, 3], function toString() { [native code] }, {get a() { return a}, set a(x) {this.b=x}, b: 9}, null, undefined]
Deactivated in test mode.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/12653003
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13966 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-03-18 14:03:15 +00:00
|
|
|
switch (typeof x) {
|
|
|
|
case "undefined":
|
|
|
|
return "undefined";
|
|
|
|
case "boolean":
|
|
|
|
case "number":
|
|
|
|
case "function":
|
|
|
|
return x.toString();
|
|
|
|
case "string":
|
|
|
|
return "\"" + x.toString() + "\"";
|
2013-03-22 16:33:50 +00:00
|
|
|
case "symbol":
|
2014-07-14 14:01:30 +00:00
|
|
|
return x.toString();
|
More useful result pretty printing for d8
For example:
d8> ["hi", 5, [2,3],Object.prototype.toString, {get a() { return a}, b: 9, set a(x) {this.b=x}}, null, undefined]
["hi", 5, [2, 3], function toString() { [native code] }, {get a() { return a}, set a(x) {this.b=x}, b: 9}, null, undefined]
Deactivated in test mode.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/12653003
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13966 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-03-18 14:03:15 +00:00
|
|
|
case "object":
|
2013-10-17 10:02:45 +00:00
|
|
|
if (IS_NULL(x)) return "null";
|
2015-12-22 08:57:32 +00:00
|
|
|
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(", ") + "]";
|
|
|
|
}
|
More useful result pretty printing for d8
For example:
d8> ["hi", 5, [2,3],Object.prototype.toString, {get a() { return a}, b: 9, set a(x) {this.b=x}}, null, undefined]
["hi", 5, [2, 3], function toString() { [native code] }, {get a() { return a}, set a(x) {this.b=x}, b: 9}, null, undefined]
Deactivated in test mode.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/12653003
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13966 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-03-18 14:03:15 +00:00
|
|
|
try {
|
2015-12-22 08:57:32 +00:00
|
|
|
var string = String(x);
|
|
|
|
if (string && string !== "[object Object]") return string;
|
|
|
|
} catch(e) {}
|
|
|
|
var props = [];
|
|
|
|
var names = Object.getOwnPropertyNames(x);
|
|
|
|
names = names.concat(Object.getOwnPropertySymbols(x));
|
|
|
|
for (var i in names) {
|
|
|
|
var name = names[i];
|
|
|
|
var desc = Object.getOwnPropertyDescriptor(x, name);
|
|
|
|
if (IS_UNDEFINED(desc)) continue;
|
|
|
|
if (IS_SYMBOL(name)) name = "[" + Stringify(name) + "]";
|
|
|
|
if ("value" in desc) {
|
|
|
|
props.push(name + ": " + Stringify(desc.value, depth - 1));
|
|
|
|
}
|
|
|
|
if (desc.get) {
|
|
|
|
var getter = Stringify(desc.get);
|
|
|
|
props.push("get " + name + getter.slice(getter.indexOf('(')));
|
|
|
|
}
|
|
|
|
if (desc.set) {
|
|
|
|
var setter = Stringify(desc.set);
|
|
|
|
props.push("set " + name + setter.slice(setter.indexOf('(')));
|
|
|
|
}
|
More useful result pretty printing for d8
For example:
d8> ["hi", 5, [2,3],Object.prototype.toString, {get a() { return a}, b: 9, set a(x) {this.b=x}}, null, undefined]
["hi", 5, [2, 3], function toString() { [native code] }, {get a() { return a}, set a(x) {this.b=x}, b: 9}, null, undefined]
Deactivated in test mode.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/12653003
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13966 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-03-18 14:03:15 +00:00
|
|
|
}
|
2015-12-22 08:57:32 +00:00
|
|
|
return "{" + props.join(", ") + "}";
|
More useful result pretty printing for d8
For example:
d8> ["hi", 5, [2,3],Object.prototype.toString, {get a() { return a}, b: 9, set a(x) {this.b=x}}, null, undefined]
["hi", 5, [2, 3], function toString() { [native code] }, {get a() { return a}, set a(x) {this.b=x}, b: 9}, null, undefined]
Deactivated in test mode.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/12653003
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13966 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-03-18 14:03:15 +00:00
|
|
|
default:
|
2015-07-23 09:49:19 +00:00
|
|
|
return "[crazy non-standard value]";
|
More useful result pretty printing for d8
For example:
d8> ["hi", 5, [2,3],Object.prototype.toString, {get a() { return a}, b: 9, set a(x) {this.b=x}}, null, undefined]
["hi", 5, [2, 3], function toString() { [native code] }, {get a() { return a}, set a(x) {this.b=x}, b: 9}, null, undefined]
Deactivated in test mode.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/12653003
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13966 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-03-18 14:03:15 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-22 13:49:23 +00:00
|
|
|
|
|
|
|
function StringifyProxy(proxy, depth) {
|
|
|
|
var proxy_type = typeof proxy;
|
|
|
|
var info_object = {
|
|
|
|
target: JSProxyGetTarget(proxy),
|
|
|
|
handler: JSProxyGetHandler(proxy)
|
|
|
|
}
|
|
|
|
return '[' + proxy_type + ' Proxy ' + Stringify(info_object, depth-1) + ']';
|
|
|
|
}
|