-
Notifications
You must be signed in to change notification settings - Fork 1
/
subProperties.js
352 lines (309 loc) · 10.2 KB
/
subProperties.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
function* subProperties (thing=globalThis) {
let props = []
let done = []
let todo = [thing]
while (todo.length > 0) {
let currentThing = todo.pop()
if (!props.includes(currentThing)) {
yield currentThing
props.push(currentThing)
}
if (done.includes(currentThing)) {
continue
} else {
done.push(currentThing)
}
if (typeof currentThing === undefined || typeof currentThing === null) {
continue
}
let properties;
try {
properties = [...Object.getOwnPropertyNames(currentThing), ...Object.getOwnPropertySymbols(currentThing)]
} catch (error) {
yield ['Actual Error, type 0', error]
console.warn('Cannot get properties, skipping this one: %o', currentThing)
continue
}
for (const property of properties) {
try {
if ('get' in Object.getOwnPropertyDescriptor(currentThing, property)) {
console.warn(
'"get" in Object.getOwnPropertyDescriptor(%o, %o).' +
'This means that the value of the property could just change in the middle of this function.',
currentThing,
property
)
}
} catch (error) {
yield ['Actual Error, type 1', error]
}
try {
if (!props.includes(currentThing[property])) {
yield currentThing[property]
props.push(currentThing[property])
}
if (!(done.includes(currentThing[property])) && !(todo.includes(currentThing[property]))) {
todo.push(currentThing[property])
}
} catch (error) {
yield ['Actual Error, type 2', error]
}
}
}
return props
}
// Version 0.2
function* generateEverythingPossible () {
yield globalThis
const nouns = new Set()
const done = new Set()
const nounsLeft = new Set()
nounsLeft.add(globalThis)
const newNounsLeft = new Set()
while (nounsLeft.size > 0) {
for (const noun of nounsLeft) {
nounsLeft.delete(noun)
done.add(noun)
if (noun === undefined || noun === null) {
continue
}
try {
newNounsLeft.add(Object.getPrototypeOf(noun))
} catch (error) {
newNounsLeft.add(error.message)
}
try {
const propertyNames = new Set()
for (const [name, propertyDescriptor] of Object.entries(Object.getOwnPropertyDescriptors(noun))) {
// enumerable and configurable ignored
// value and writable, or get and set
if ("value" in propertyDescriptor) {
propertyNames.add(name)
} else {
if ("get" in propertyDescriptor) {
newNounsLeft.add(propertyDescriptor.get)
propertyNames.add(name)
}
if ("set" in propertyDescriptor) {
newNounsLeft.add(propertyDescriptor.set)
}
}
}
// obj extends from obj[[prototype]].prototype, obj[[prototype]][[prototype]].prototype, etc.
// confusing names though
let prototype = Object.getPrototypeOf(noun)
try {
while (prototype !== null) {
if (prototype.prototype !== undefined && prototype.prototype !== null) {
for (const propertyName of [
...Object.getOwnPropertyNames(prototype.prototype),
...Object.getOwnPropertySymbols(prototype.prototype)
])
{
//if (!propertyNames.has(propertyName)) {
propertyNames.add(propertyName)
//}
}
}
prototype = Object.getPrototypeOf(prototype)
}
} catch (error) {
console.warn('Error with %o,\n %o', prototype, error)
newNounsLeft.add(error.message)
}
for (const name of propertyNames) {
newNounsLeft.add(noun[name])
}
} catch (error) {
console.warn('Error with %o,\n %o', noun, error)
newNounsLeft.add(error.message)
}
if (newNounsLeft.size !== 0) {
console.group(noun)
}
// nouns
// newNouns
// newNounsLeft
// globalThis - in nouns and nounsleft
// globalThis - in nouns and done
// property - in newnounsleft
// property - in nouns and nounsleft
// property - in nouns and done
// property.property
for (const newNoun of newNounsLeft) {
if (!done.has(newNoun) && !nounsLeft.has(newNoun)) {
yield newNoun
nounsLeft.add(newNoun)
nouns.add(newNoun)
}
}
if (newNounsLeft.size !== 0) {
console.groupEnd()
}
newNounsLeft.clear()
}
}
}
{
let generator = generateEverythingPossible()
let x = setInterval(() => {
let next = generator.next()
if (next.done) {
console.log("%cDone!", "color:green;")
clearInterval(x)
} else {
if (next.value instanceof Function) {
console.group('String(func) === ' + String(next.value))
console.log(next.value)
console.groupEnd()
} else {
console.log(next.value)
}
}
}, 100)
}
// Version 0.3
/* async */ function *thingGenerator (start = globalThis) {
yield start
const things = [start]
const newThings = [start]
const checkedThings = []
const functions = [] // Unimplemented
const propertyTests = new Map() // thing, Set<thing>
const callTests = new Map() // thing, [ [], [thing], [thing, thing], [thing, thing, thing], ...] // Unimplemented
// Also unimplemented: await
function* checkNew(thing) {
if (!things.includes(thing)) {
try {
if (thing instanceof Error) {
for (const thing2 of things) {
let thing2Constructor, thing2Message
try {
thing2Constructor = thing2.constructor
thing2Message = thing2.message
} catch {continue}
if (thing.constructor === thing2Constructor && thing.message === thing2.message) {
return
}
}
}
} catch {}
things.push(thing)
newThings.push(thing)
yield thing
}
}
function update(map, key, value) {
if (map.has(key)) {
const set = map.get(key)
set.add(value)
} else {
map.set(key, new Set([value]))
}
}
function updateCalls () {
}
function* main() {
const next = things.shift()
function* propertySearch() {
// for in catch
try {
for (const property in next) yield* checkNew(property);
} catch (error) {
yield* checkNew(error)
}
}
function* propertyChecks() {
for (const thing of checkedThings) {
// typeof catch
try {
if (typeof thing === "string" || typeof thing === "symbol") {
// property access catch
try {
yield* checkNew(thing[next])
update(propertyTests, next, thing)
} catch (error) {
yield* checkNew(error)
}
}
} catch (error) {
yield* checkNew(error)
}
}
try {
if (typeof next === "string" || typeof next === "symbol") {
for (const thing of checkedThings) {
try {
yield* checkNew(thing[next])
update(propertyTests, thing, next)
} catch (error) {
yield* checkNew(error)
}
}
}
} catch (error) {
yield* checkNew(error)
}
}
function* functionCalls(){}
yield* propertySearch()
yield* propertyChecks()
yield* functionCalls() // Unimplemented
checkedThings.push(next)
// unimplemented: if typeof next === "function"
}
while (newThings.length) {
yield* main()
}
}
// v0.4
async function* search(obj=globalThis, done=new Set(), todo=[]) {
todo.push = val => {
if(!done.has(val))Array.prototype.push.call(todo,val)
}
async function* main() {
if (done.has(obj)) return;
console.log(obj)
// "undefined" "number" "boolean" "symbol" "symbol" "bigint"
// "object" "function"
if (obj !== null && (typeof obj === "object" || typeof obj === "function")) {
try {
const props = Object.getOwnPropertyDescriptors(obj)
for (const prop in props) {
const d = props[prop]
if ("get" in d) {
todo.push(d.get)
}
if ("set" in d) {
todo.push(d.set)
}
const val = obj[prop]
todo.push(val)
if (val instanceof Promise) {
const result = await val;
todo.push(result)
}
}
} catch (error) {
todo.push(error)
}
try {
todo.push(Object.getPrototypeOf(obj))
} catch (error) {
todo.push(error)
}
}
console.groupEnd()
}
yield* main()
for (; todo.length; obj = todo.pop()) {
yield* main()
}
}
// {
// const s = search()
// const i = setInterval(async () => {
// const n = await s.next()
// if (n.done) clearInterval(i)
// }, 500)
// }