-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsforce.js
368 lines (329 loc) · 10.9 KB
/
jsforce.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"use strict";
const POSSIBLE_CHARS = "0123456789ftuarNnldseiIy.+[objc A]-,O"
const POSSIBLE_STRING = /^[0123456789ftuarNnldseiIy.+[objc A\]\-,O]*$/
const JSF_PROPERTIES = [
// "'false'",
// "'true'",
// "'undefined'",
// "'NaN'",
"'is'",
"'flat'",
// "'Infinity'",
"'fill'",
"'find'",
"'seal'",
"'filter'",
"'entries'",
// "'[object Array Iterator]'",
"'sort'",
"'call'",
"'bind'",
"'slice'",
"'create'",
"'reduce'",
"'includes'",
"'join'",
"'concat'",
"'constructor'",
// "'[object Object]'",
]
const JSF_UNITS = ["[]", "![]", "!![]", "+[![]]", "[][[]]"] // Beyond this there's no reason to use "!"
const JSF_SURROUND = ["()", "[]"]
const JSF_JOIN = ["+", ""] // Joins: something[] something() something+something
// Function calls are done by surrounding "expression" to get (expression) and then joining "[]" with "(expression)"
const textarea = (() => {
if ("document" in globalThis && globalThis?.location.href === "about:blank") {
let result = document.createElement('textarea')
result.cols = 50
result.rows = 32
document.body.appendChild(result)
return result
} else {
// same thing to the code
return { value: "" }
}
})()
const nestingMap = {
'[': ']',
'(': ')'
}
let depth = 1
let combinations = []
let passedCombinations = {}
let failedCombinations = []
let undefinedCombinations = [ "[][[]]", "[][+[]]", "[][![]]", "[][!![]]" ]
let triedCombinations = []
let stringCounts = {}
let again = new Set()
let againFrom = new Map()
// let combosDone = new Set()
// Don't need to check, each string is added by one different char each time,
// and they won't collide
// optimization
// let wait = Object.create(null)
// e.g. ([( can only happen one way
function reset () {
depth = 1
combinations = []
passedCombinations = {}
failedCombinations = []
undefinedCombinations = [ "[][[]]", "[][+[]]", "[][![]]", "[][!![]]" ]
triedCombinations = []
textarea.value = ''
again = new Set()
againFrom = new Map()
// wait = Object.create(null)
console.clear()
}
String.prototype._howmany = function _howmany (substring) {
return this.split(substring).length - 1
}
function tryJSF (code) {
try {
return { 'pass': eval(code) }
} catch (error) {
console.log(code, error.message)
return { 'fail': error }
}
}
function shouldError (code) {
try {
eval(code)
console.log(code, "This should error")
} catch {}
}
function stringify (value) {
if (typeof value === "string") {
return `"${value.replaceAll('\\', '\\\\').replaceAll('"', '\\"')}"`
} else if (typeof value === "object" && value !== null) {
let oldTostring = Array.prototype.toString
Array.prototype.toString = function () {
return `[${this.join(', ')}]`
}
let seen = new WeakSet()
const result = JSON.stringify(value, function (key, keyvalue) {
if (typeof keyvalue === "object" && keyvalue !== null) {
if (keyvalue instanceof Number) return `~Number { ${keyvalue} }~`
if (keyvalue instanceof String) return `~String { ${keyvalue} }~`
if (keyvalue instanceof Boolean) return `~Boolean { ${keyvalue} }~`
if (seen.has(keyvalue)) return "~[[Cyclic value]]~"
else seen.add(keyvalue)
} else if (typeof keyvalue === "function") {
return `~function ${keyvalue.name}()~`
} else if (keyvalue === undefined || keyvalue === NaN) {
return "~undefined~"
}
return keyvalue
}).replaceAll('"~', '').replaceAll('~"', '')
Array.prototype.toString = oldTostring
return result
}
return value
}
// Or mismatched []()
// Or (false)()
function usesArrayAsFunction (str) {
if (!(str.includes("[") && str.includes("("))) {
return false;
}
let stack = []
let isFunction = [0] // 0, 1, 2
for (let i = 0; i < str.length; i++) {
if (str[i] === "[" || str[i] === "(") {
stack.push(str[i])
isFunction[stack.length] = 0 // same as push
} else if (str[i] === "]") {
if (stack[stack.length - 1] === "(" || stack.length === 0) return true
stack.pop()
const temp = isFunction.pop()
isFunction[stack.length]++
if (str[i + 1] === "(" && isFunction[stack.length] < 2) return true
} else if (str[i] === ")") {
if (stack[stack.length - 1] === "[" || stack.length === 0) return true
stack.pop()
const temp = isFunction.pop()
if (temp < isFunction[stack.length]) {
isFunction[stack.length] = temp
}
if (str[i + 1] === "(" && isFunction[stack.length] < 2) return true
}
if (stack.length < 0 || stack.length > 4) return "!"
}
if (stack.length) return stack
return false
}
function check (newcombo, char) {
if (triedCombinations.includes(newcombo)) {
// console.trace(newcombo)
return
}
triedCombinations.push(newcombo)
// a === b instead of startsWith
if (
// newcombo.includes("(]") ||
// newcombo.includes("[)") ||
newcombo.includes("!)") ||
newcombo.includes("!]") ||
newcombo.includes(")!") ||
newcombo.includes("]!") ||
newcombo.includes(")[]") ||
newcombo.includes("][]") ||
newcombo.includes("+()") ||
// newcombo.includes("!()") ||
newcombo.includes("(()") ||
newcombo.includes("[()") ||
// newcombo.includes("!!!") ||
newcombo.includes("++!") ||
newcombo.includes("++(") ||
newcombo.includes("++[") ||
newcombo.includes("+++)") ||
newcombo.includes("+++]") ||
newcombo === "()" ||
// newcombo.endsWith("!++[]") ||
newcombo.endsWith("+++[]") ||
(newcombo.startsWith("((") && newcombo.endsWith("))")) ||
// (newcombo.lastIndexOf("(") < newcombo.lastIndexOf("[") &&
// newcombo.lastIndexOf("[") < newcombo.lastIndexOf(")")) ||
// (newcombo.lastIndexOf("[") < newcombo.lastIndexOf("(") &&
// newcombo.lastIndexOf("(") < newcombo.lastIndexOf("]")) ||
undefinedCombinations.some(undefinedCombo => newcombo.includes(undefinedCombo + '[') || newcombo.includes(undefinedCombo + '(')) ||
// newcombo._howmany("[") < newcombo._howmany("]") ||
// newcombo._howmany("(") < newcombo._howmany(")") ||
/[\(\[]{3}/.test(newcombo) ||
/[\)\]]{3}/.test(newcombo) ||
/\]\[!!?\[\]\]/.test(newcombo) ||
/\[[^\[\]]+\]\[\[\]\]/.test(newcombo) ||
/[^+]\+[!)\]]/.test(newcombo) ||
/([!+[(]|^)\+\+\+(\+|!|]|\)|$)/.test(newcombo) ||
/([![(]|^)\+\+[+!)\]]/.test(newcombo) ||
/([![(]|^)\+\+\[]([^[]|$)/.test(newcombo) ||
/([+![(]|^)\[]\+\+/.test(newcombo))
{
// shouldError(newcombo)
return;
}
// if (wait[combo] > depth) {
// combinations[depth][newcombo] = "waiting"
// wait[newcombo] = wait[combo]
// return;
// }
const check1 = usesArrayAsFunction(newcombo)
if (check1 === true || check1 === "!") {
// shouldError(newcombo)
return;
}
if (// newcombo._howmany("[") > newcombo._howmany("]") ||
// newcombo._howmany("(") > newcombo._howmany(")") ||
newcombo.endsWith("!") ||
newcombo.endsWith("+") ||
(/\+\+\[.*]/.test(newcombo) && !/\+\+\[.*]\[/.test(newcombo)) ||
(/\[.*]\+\+/.test(newcombo) && !/\]\[.*]\+\+/.test(newcombo)))
{
combinations[depth][newcombo] = "nothing here"
return;
}
const result = combinations[depth][newcombo] = tryJSF(newcombo)
if ("pass" in result) {
if (result.pass === undefined) {
undefinedCombinations.push(newcombo)
} else if (typeof result.pass === "string") {
if (result.pass.includes("function") || POSSIBLE_STRING.test(result.pass)) {
failedCombinations.push(newcombo)
delete combinations[depth][newcombo]
return
}
}
const resStr = stringify(result.pass)
if (/(true|false|undefined|NaN|[0-9]){3}/.test(resStr) || resStr?.name === "anonymous") {
failedCombinations.push(newcombo)
delete combinations[depth][newcombo]
return
}
if (!again.has(resStr)) {
again.add(resStr)
againFrom.set(resStr, [depth, newcombo])
let out = typeof resStr === "function" ? `function ${resStr.name}()` : resStr
textarea.value += `${depth}: ${newcombo}\n${out}\n`
} else if (char !== "()") {
let [oldDepth, oldcombo] = againFrom.get(resStr)
if (oldcombo.length > newcombo.length) {
failedCombinations.push(oldcombo)
delete combinations[oldDepth][oldcombo]
delete passedCombinations[oldcombo]
againFrom.set(resStr, [depth, newcombo])
textarea.value += ` New record!\n ${depth}: ${newcombo}\n ${resStr}\n`
} else {
failedCombinations.push(newcombo)
delete combinations[depth][newcombo]
}
}
} else {
failedCombinations.push(newcombo)
delete combinations[depth][newcombo]
}
}
function comboChecks (combo) {
for (const char of JSF_UNITS) {
const newcombo = combo + char
check(newcombo, char)
}
if (passedCombinations[combo] === "nothing here") return;
// So now we know combo is a completed expression
for (const property of JSF_PROPERTIES) {
check(`${combo}[${property}]`)
}
for (const surround of JSF_SURROUND) {
const newcombo = `${surround[0]}${combo}${surround[1]}`
check(newcombo, surround)
}
if (combo[0] !== '+') {
check(`+${combo}`)
}
for (const join of JSF_JOIN) {
// Currently only has the previous depths' combinations
for (const combo2 in passedCombinations) {
if (passedCombinations[combo2] === "nothing here") continue;
const newcombo = `${combo}${join}${combo2}`
const newcombo2 = `${combo2}${join}${combo}`
check(newcombo)
check(newcombo2)
}
}
}
async function* _search () {
combinations[depth] = Object.create(null)
if (depth === 1) {
for (const unit of JSF_UNITS) {
check(unit)
}
} else {
const countLabel = `${depth} (${Object.keys(combinations[depth - 1]).length} elements)`
for (const combo in combinations[depth - 1]) {
console.count(countLabel)
yield comboChecks(combo)
await new Promise(resolve => setTimeout(resolve, 0))
}
console.countReset(countLabel)
}
// clear each depth
// wait = Object.create(null)
// Add this depth's combinations to "passed"
Object.assign(passedCombinations, combinations[depth])
depth++
}
async function time (times=10) {
reset()
console.time('search')
for (let i = 0; i < times; i++) {
console.time(`depth ${depth}`)
const search = _search()
while (!((await search.next()).done)) continue;
console.timeEnd(`depth ${depth - 1}`)
}
console.timeEnd('search')
}
function time2 () {
console.time('search')
search()
console.timeEnd('search')
}