blob: 27a0bc39cd07107cb03fa65f0c12534aed45573a [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2008 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005"use strict";
6
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007// A more universal stringify that supports more types than JSON.
8// Used by the d8 shell to output results.
9var stringifyDepthLimit = 4; // To avoid crashing on cyclic objects
Steve Blocka7e24c12009-10-30 11:49:00 +000010
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011// Hacky solution to circumvent forcing --allow-natives-syntax for d8
12function isProxy(o) { return false };
13function JSProxyGetTarget(proxy) { };
14function JSProxyGetHandler(proxy) { };
15
16try {
17 isProxy = Function(['object'], 'return %_IsJSProxy(object)');
18 JSProxyGetTarget = Function(['proxy'],
19 'return %JSProxyGetTarget(proxy)');
20 JSProxyGetHandler = Function(['proxy'],
21 'return %JSProxyGetHandler(proxy)');
22} catch(e) {};
23
24
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025function Stringify(x, depth) {
26 if (depth === undefined)
27 depth = stringifyDepthLimit;
28 else if (depth === 0)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000029 return "...";
30 if (isProxy(x)) {
31 return StringifyProxy(x, depth);
32 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033 switch (typeof x) {
34 case "undefined":
35 return "undefined";
36 case "boolean":
37 case "number":
38 case "function":
39 return x.toString();
40 case "string":
41 return "\"" + x.toString() + "\"";
42 case "symbol":
43 return x.toString();
44 case "object":
45 if (IS_NULL(x)) return "null";
46 if (x.constructor && x.constructor.name === "Array") {
47 var elems = [];
48 for (var i = 0; i < x.length; ++i) {
49 elems.push(
50 {}.hasOwnProperty.call(x, i) ? Stringify(x[i], depth - 1) : "");
51 }
52 return "[" + elems.join(", ") + "]";
Steve Blocka7e24c12009-10-30 11:49:00 +000053 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000054 try {
55 var string = String(x);
56 if (string && string !== "[object Object]") return string;
57 } catch(e) {}
58 var props = [];
59 var names = Object.getOwnPropertyNames(x);
60 names = names.concat(Object.getOwnPropertySymbols(x));
61 for (var i in names) {
62 var name = names[i];
63 var desc = Object.getOwnPropertyDescriptor(x, name);
64 if (IS_UNDEFINED(desc)) continue;
65 if (IS_SYMBOL(name)) name = "[" + Stringify(name) + "]";
66 if ("value" in desc) {
67 props.push(name + ": " + Stringify(desc.value, depth - 1));
68 }
69 if (desc.get) {
70 var getter = Stringify(desc.get);
71 props.push("get " + name + getter.slice(getter.indexOf('(')));
72 }
73 if (desc.set) {
74 var setter = Stringify(desc.set);
75 props.push("set " + name + setter.slice(setter.indexOf('(')));
76 }
Steve Blocka7e24c12009-10-30 11:49:00 +000077 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078 return "{" + props.join(", ") + "}";
79 default:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000080 return "[crazy non-standard value]";
Steve Blocka7e24c12009-10-30 11:49:00 +000081 }
Steve Blocka7e24c12009-10-30 11:49:00 +000082}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000083
84function StringifyProxy(proxy, depth) {
85 var proxy_type = typeof proxy;
86 var info_object = {
87 target: JSProxyGetTarget(proxy),
88 handler: JSProxyGetHandler(proxy)
89 }
90 return '[' + proxy_type + ' Proxy ' + Stringify(info_object, depth-1) + ']';
91}