OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 "use strict"; | 5 "use strict"; |
6 | 6 |
7 // A more universal stringify that supports more types than JSON. | 7 // A more universal stringify that supports more types than JSON. |
8 // Used by the d8 shell to output results. | 8 // Used by the d8 shell to output results. |
9 var stringifyDepthLimit = 4; // To avoid crashing on cyclic objects | 9 var stringifyDepthLimit = 4; // To avoid crashing on cyclic objects |
10 | 10 |
11 function Stringify(x, depth) { | 11 function Stringify(x, depth) { |
12 if (depth === undefined) | 12 if (depth === undefined) |
13 depth = stringifyDepthLimit; | 13 depth = stringifyDepthLimit; |
14 else if (depth === 0) | 14 else if (depth === 0) |
15 return "*"; | 15 return "*"; |
| 16 if (IS_PROXY(x)) { |
| 17 return StringifyProxy(x, depth); |
| 18 } |
16 switch (typeof x) { | 19 switch (typeof x) { |
17 case "undefined": | 20 case "undefined": |
18 return "undefined"; | 21 return "undefined"; |
19 case "boolean": | 22 case "boolean": |
20 case "number": | 23 case "number": |
21 case "function": | 24 case "function": |
22 return x.toString(); | 25 return x.toString(); |
23 case "string": | 26 case "string": |
24 return "\"" + x.toString() + "\""; | 27 return "\"" + x.toString() + "\""; |
25 case "symbol": | 28 case "symbol": |
(...skipping 30 matching lines...) Expand all Loading... |
56 if (desc.set) { | 59 if (desc.set) { |
57 var setter = Stringify(desc.set); | 60 var setter = Stringify(desc.set); |
58 props.push("set " + name + setter.slice(setter.indexOf('('))); | 61 props.push("set " + name + setter.slice(setter.indexOf('('))); |
59 } | 62 } |
60 } | 63 } |
61 return "{" + props.join(", ") + "}"; | 64 return "{" + props.join(", ") + "}"; |
62 default: | 65 default: |
63 return "[crazy non-standard value]"; | 66 return "[crazy non-standard value]"; |
64 } | 67 } |
65 } | 68 } |
| 69 |
| 70 function StringifyProxy(proxy, depth) { |
| 71 var proxy_type = typeof proxy; |
| 72 return '[' + proxy_type + ' Proxy {' + |
| 73 'target: ' + |
| 74 StringifyProxyInternal(%JSProxyGetTarget(proxy), depth) + ', ' + |
| 75 'handler: '+ |
| 76 StringifyProxyInternal(%JSProxyGetHandler(proxy), depth) + |
| 77 '}]'; |
| 78 } |
| 79 |
| 80 function StringifyProxyInternal(object, depth) { |
| 81 // Special case to handle proxy __proto__ recursions on target or handler. |
| 82 try { |
| 83 return Stringify(object, depth-1); |
| 84 } catch(RangeError) { |
| 85 return '{*}' |
| 86 } |
| 87 } |
OLD | NEW |