OLD | NEW |
| (Empty) |
1 // * |globalObject| should be the global (usually |this|). | |
2 // * |propertyNamesInGlobal| should be a list of properties captured before | |
3 // other scripts are loaded, using: Object.getOwnPropertyNames(this); | |
4 // * |outputFunc| is called back with each line of output. | |
5 | |
6 function globalInterfaceListing(globalObject, propertyNamesInGlobal, outputFunc)
{ | |
7 | |
8 // List of builtin JS constructors; Blink is not controlling what properties the
se | |
9 // objects have, so exercising them in a Blink test doesn't make sense. | |
10 // | |
11 // If new builtins are added, please update this list along with the one in | |
12 // LayoutTests/http/tests/serviceworker/webexposed/resources/global-interface-li
sting-worker.js | |
13 var jsBuiltins = new Set([ | |
14 'Array', | |
15 'ArrayBuffer', | |
16 'Boolean', | |
17 'Date', | |
18 'Error', | |
19 'EvalError', | |
20 'Float32Array', | |
21 'Float64Array', | |
22 'Function', | |
23 'Infinity', | |
24 'Int16Array', | |
25 'Int32Array', | |
26 'Int8Array', | |
27 'Intl', | |
28 'JSON', | |
29 'Map', | |
30 'Math', | |
31 'NaN', | |
32 'Number', | |
33 'Object', | |
34 'Promise', | |
35 'Proxy', | |
36 'RangeError', | |
37 'ReferenceError', | |
38 'Reflect', | |
39 'RegExp', | |
40 'Set', | |
41 'String', | |
42 'Symbol', | |
43 'SyntaxError', | |
44 'TypeError', | |
45 'URIError', | |
46 'Uint16Array', | |
47 'Uint32Array', | |
48 'Uint8Array', | |
49 'Uint8ClampedArray', | |
50 'WeakMap', | |
51 'WeakSet', | |
52 'WebAssembly', | |
53 'decodeURI', | |
54 'decodeURIComponent', | |
55 'encodeURI', | |
56 'encodeURIComponent', | |
57 'escape', | |
58 'eval', | |
59 'isFinite', | |
60 'isNaN', | |
61 'parseFloat', | |
62 'parseInt', | |
63 'undefined', | |
64 'unescape', | |
65 ]); | |
66 | |
67 function isWebIDLConstructor(propertyKey) { | |
68 if (jsBuiltins.has(propertyKey)) | |
69 return false; | |
70 var descriptor = Object.getOwnPropertyDescriptor(this, propertyKey); | |
71 if (descriptor.value == undefined || descriptor.value.prototype == undefined
) | |
72 return false; | |
73 return descriptor.writable && !descriptor.enumerable && descriptor.configura
ble; | |
74 } | |
75 | |
76 var wellKnownSymbols = new Map([ | |
77 [Symbol.hasInstance, "@@hasInstance"], | |
78 [Symbol.isConcatSpreadable, "@@isConcatSpreadable"], | |
79 [Symbol.iterator, "@@iterator"], | |
80 [Symbol.match, "@@match"], | |
81 [Symbol.replace, "@@replace"], | |
82 [Symbol.search, "@@search"], | |
83 [Symbol.species, "@@species"], | |
84 [Symbol.split, "@@split"], | |
85 [Symbol.toPrimitive, "@@toPrimitive"], | |
86 [Symbol.toStringTag, "@@toStringTag"], | |
87 [Symbol.unscopables, "@@unscopables"] | |
88 ]); | |
89 | |
90 function collectPropertyInfo(object, propertyKey, output) { | |
91 var propertyString = wellKnownSymbols.get(propertyKey) || propertyKey.toStri
ng(); | |
92 var keywords = Object.prototype.hasOwnProperty.call(object, 'prototype') ? '
static ' : ''; | |
93 var descriptor = Object.getOwnPropertyDescriptor(object, propertyKey); | |
94 if ('value' in descriptor) { | |
95 var type = typeof descriptor.value === 'function' ? 'method' : 'attribut
e'; | |
96 output.push(' ' + keywords + type + ' ' + propertyString); | |
97 } else { | |
98 if (descriptor.get) | |
99 output.push(' ' + keywords + 'getter ' + propertyString); | |
100 if (descriptor.set) | |
101 output.push(' ' + keywords + 'setter ' + propertyString); | |
102 } | |
103 } | |
104 | |
105 function ownEnumerableSymbols(object) { | |
106 return Object.getOwnPropertySymbols(object). | |
107 filter(function(name) { | |
108 return Object.getOwnPropertyDescriptor(object, name).enumerable; | |
109 }); | |
110 } | |
111 | |
112 function collectPropertyKeys(object) { | |
113 if (Object.prototype.hasOwnProperty.call(object, 'prototype')) { | |
114 // Skip properties that aren't static (e.g. consts), or are inherited. | |
115 // TODO(caitp): Don't exclude non-enumerable properties | |
116 var protoProperties = new Set(Object.keys(object.prototype).concat( | |
117 Object.keys(object.__proto__), | |
118 ownEnumerableSymbols(object.prototype), | |
119 ownEnumerableSymbols(object.__proto__))); | |
120 return Object.keys(object). | |
121 concat(ownEnumerableSymbols(object)). | |
122 filter(function(name) { | |
123 return !protoProperties.has(name); | |
124 }); | |
125 } | |
126 return Object.getOwnPropertyNames(object).concat(Object.getOwnPropertySymbols
(object)); | |
127 } | |
128 | |
129 // FIXME: List interfaces with NoInterfaceObject specified in their IDL file. | |
130 outputFunc('[INTERFACES]'); | |
131 var interfaceNames = Object.getOwnPropertyNames(this).filter(isWebIDLConstructor
); | |
132 interfaceNames.sort(); | |
133 interfaceNames.forEach(function(interfaceName) { | |
134 var inheritsFrom = this[interfaceName].__proto__.name; | |
135 if (inheritsFrom) | |
136 outputFunc('interface ' + interfaceName + ' : ' + inheritsFrom); | |
137 else | |
138 outputFunc('interface ' + interfaceName); | |
139 // List static properties then prototype properties. | |
140 [this[interfaceName], this[interfaceName].prototype].forEach(function(object
) { | |
141 var propertyKeys = collectPropertyKeys(object); | |
142 var propertyStrings = []; | |
143 propertyKeys.forEach(function(propertyKey) { | |
144 collectPropertyInfo(object, propertyKey, propertyStrings); | |
145 }); | |
146 propertyStrings.sort().forEach(outputFunc); | |
147 }); | |
148 }); | |
149 | |
150 outputFunc('[GLOBAL OBJECT]'); | |
151 var propertyStrings = []; | |
152 var memberNames = propertyNamesInGlobal.filter(function(propertyKey) { | |
153 return !jsBuiltins.has(propertyKey) && !isWebIDLConstructor(propertyKey); | |
154 }); | |
155 memberNames.forEach(function(propertyKey) { | |
156 collectPropertyInfo(globalObject, propertyKey, propertyStrings); | |
157 }); | |
158 propertyStrings.sort().forEach(outputFunc); | |
159 | |
160 } | |
OLD | NEW |