Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | (function(global) { |
| 6 | |
| 7 | var GetProperties = function(this_name, object) { |
| 8 | var result = {}; |
| 9 | try { |
| 10 | var names = Object.getOwnPropertyNames(object); |
| 11 | } catch(e) { |
| 12 | return; |
| 13 | } |
| 14 | for (var i = 0; i < names.length; ++i) { |
| 15 | var name = names[i]; |
| 16 | if (typeof object === "function") { |
| 17 | if (name === "length" || |
| 18 | name === "name" || |
| 19 | name === "arguments" || |
| 20 | name === "caller" || |
| 21 | name === "prototype") { |
| 22 | continue; |
| 23 | } |
| 24 | } |
| 25 | // Avoid endless recursion. |
| 26 | if (this_name === "prototype" && name === "constructor") continue; |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 27 | // Avoid needless duplication. |
| 28 | if (this_name === "__PROTO__" && name === "constructor") continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 29 | // Could get this from the parent, but having it locally is easier. |
| 30 | var property = { "name": name }; |
| 31 | try { |
| 32 | var value = object[name]; |
| 33 | } catch(e) { |
| 34 | property.type = "getter"; |
| 35 | result[name] = property; |
| 36 | continue; |
| 37 | } |
| 38 | var type = typeof value; |
| 39 | property.type = type; |
| 40 | if (type === "function") { |
| 41 | property.length = value.length; |
| 42 | property.prototype = GetProperties("prototype", value.prototype); |
| 43 | } |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 44 | if (type === "string" || type === "number") { |
| 45 | property.value = value; |
| 46 | } else { |
| 47 | property.properties = GetProperties(name, value); |
| 48 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 49 | result[name] = property; |
| 50 | } |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 51 | // Print the __proto__ if it's not the default Object prototype. |
| 52 | if (typeof object === "object" && object.__proto__ !== null && |
| 53 | !object.__proto__.hasOwnProperty("__proto__")) { |
| 54 | result.__PROTO__ = GetProperties("__PROTO__", object.__proto__); |
| 55 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 56 | return result; |
| 57 | }; |
| 58 | |
| 59 | var g = GetProperties("", global, ""); |
| 60 | print(JSON.stringify(g, undefined, 2)); |
| 61 | |
| 62 | })(this); // Must wrap in anonymous closure or it'll detect itself as builtin. |