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 // Hacky solution to circumvent forcing --allow-natives-syntax for d8 | |
caitp (gmail)
2015/12/22 13:21:10
Why not just compile the utility script with --all
| |
12 function isProxy(o) { return false }; | |
13 function JSProxyGetTarget(proxy) { }; | |
14 function JSProxyGetHandler(proxy) { }; | |
15 | |
16 try { | |
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 | |
11 function Stringify(x, depth) { | 25 function Stringify(x, depth) { |
12 if (depth === undefined) | 26 if (depth === undefined) |
13 depth = stringifyDepthLimit; | 27 depth = stringifyDepthLimit; |
14 else if (depth === 0) | 28 else if (depth === 0) |
15 return "*"; | 29 return "..."; |
30 if (isProxy(x)) { | |
31 return StringifyProxy(x, depth); | |
32 } | |
16 switch (typeof x) { | 33 switch (typeof x) { |
17 case "undefined": | 34 case "undefined": |
18 return "undefined"; | 35 return "undefined"; |
19 case "boolean": | 36 case "boolean": |
20 case "number": | 37 case "number": |
21 case "function": | 38 case "function": |
22 return x.toString(); | 39 return x.toString(); |
23 case "string": | 40 case "string": |
24 return "\"" + x.toString() + "\""; | 41 return "\"" + x.toString() + "\""; |
25 case "symbol": | 42 case "symbol": |
(...skipping 30 matching lines...) Expand all Loading... | |
56 if (desc.set) { | 73 if (desc.set) { |
57 var setter = Stringify(desc.set); | 74 var setter = Stringify(desc.set); |
58 props.push("set " + name + setter.slice(setter.indexOf('('))); | 75 props.push("set " + name + setter.slice(setter.indexOf('('))); |
59 } | 76 } |
60 } | 77 } |
61 return "{" + props.join(", ") + "}"; | 78 return "{" + props.join(", ") + "}"; |
62 default: | 79 default: |
63 return "[crazy non-standard value]"; | 80 return "[crazy non-standard value]"; |
64 } | 81 } |
65 } | 82 } |
83 | |
84 function 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 } | |
OLD | NEW |