blob: e49c6b74580637c6da18b26d72d4fd34ea8f59cf [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 Murdochda12d292016-06-02 14:46:10 +01005(function() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006"use strict";
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008// A more universal stringify that supports more types than JSON.
9// Used by the d8 shell to output results.
10var stringifyDepthLimit = 4; // To avoid crashing on cyclic objects
Steve Blocka7e24c12009-10-30 11:49:00 +000011
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012// Hacky solution to circumvent forcing --allow-natives-syntax for d8
13function isProxy(o) { return false };
14function JSProxyGetTarget(proxy) { };
15function JSProxyGetHandler(proxy) { };
16
17try {
18 isProxy = Function(['object'], 'return %_IsJSProxy(object)');
19 JSProxyGetTarget = Function(['proxy'],
20 'return %JSProxyGetTarget(proxy)');
21 JSProxyGetHandler = Function(['proxy'],
22 'return %JSProxyGetHandler(proxy)');
23} catch(e) {};
24
25
Ben Murdochb8a8cc12014-11-26 15:28:44 +000026function Stringify(x, depth) {
27 if (depth === undefined)
28 depth = stringifyDepthLimit;
29 else if (depth === 0)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000030 return "...";
31 if (isProxy(x)) {
32 return StringifyProxy(x, depth);
33 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000034 switch (typeof x) {
35 case "undefined":
36 return "undefined";
37 case "boolean":
38 case "number":
39 case "function":
40 return x.toString();
41 case "string":
42 return "\"" + x.toString() + "\"";
43 case "symbol":
44 return x.toString();
45 case "object":
46 if (IS_NULL(x)) return "null";
47 if (x.constructor && x.constructor.name === "Array") {
48 var elems = [];
49 for (var i = 0; i < x.length; ++i) {
50 elems.push(
51 {}.hasOwnProperty.call(x, i) ? Stringify(x[i], depth - 1) : "");
52 }
53 return "[" + elems.join(", ") + "]";
Steve Blocka7e24c12009-10-30 11:49:00 +000054 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055 try {
56 var string = String(x);
57 if (string && string !== "[object Object]") return string;
58 } catch(e) {}
59 var props = [];
60 var names = Object.getOwnPropertyNames(x);
61 names = names.concat(Object.getOwnPropertySymbols(x));
62 for (var i in names) {
63 var name = names[i];
64 var desc = Object.getOwnPropertyDescriptor(x, name);
65 if (IS_UNDEFINED(desc)) continue;
66 if (IS_SYMBOL(name)) name = "[" + Stringify(name) + "]";
67 if ("value" in desc) {
68 props.push(name + ": " + Stringify(desc.value, depth - 1));
69 }
70 if (desc.get) {
71 var getter = Stringify(desc.get);
72 props.push("get " + name + getter.slice(getter.indexOf('(')));
73 }
74 if (desc.set) {
75 var setter = Stringify(desc.set);
76 props.push("set " + name + setter.slice(setter.indexOf('(')));
77 }
Steve Blocka7e24c12009-10-30 11:49:00 +000078 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 return "{" + props.join(", ") + "}";
80 default:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081 return "[crazy non-standard value]";
Steve Blocka7e24c12009-10-30 11:49:00 +000082 }
Steve Blocka7e24c12009-10-30 11:49:00 +000083}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000084
85function StringifyProxy(proxy, depth) {
86 var proxy_type = typeof proxy;
87 var info_object = {
88 target: JSProxyGetTarget(proxy),
89 handler: JSProxyGetHandler(proxy)
90 }
91 return '[' + proxy_type + ' Proxy ' + Stringify(info_object, depth-1) + ']';
92}
Ben Murdochda12d292016-06-02 14:46:10 +010093
94return Stringify;
95})();