-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patholdcode.txt
More file actions
199 lines (197 loc) · 9.53 KB
/
oldcode.txt
File metadata and controls
199 lines (197 loc) · 9.53 KB
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
SandboxedFunction.prototype.run = function (Arguments) {
if (Arguments === undefined) {
Arguments = {length: 0, parameters: []};
}
function preformAssignment(context, accessChain) {
console.log(context, accessChain);
}
//let nested = 0;
let isStrict = null;
const accessChain = [];
const context = new Proxy({stage: '404', functionChain: [], foundReturn: false, setVar: [], unaryMinus: false},
{
set(target, p, newValue, receiver) {
if (p === 'stage') console.log('Reflect.set', newValue);
return Reflect.set(target, p, newValue);
}
});
for (const token of this.tokensNoFunction) {
if (token.type === "comment") continue;
if (isStrict === null && token.type === 'string' && /(["'])use strict\1/.test(token.value)) {
throw new Error('Strict mode not supported Yet');// isStrict = true;
} else {
isStrict = false;
}
if (context.foundReturn && ((token.type === 'delimiter' && token.type === ';')
|| (token.value === 'whitespace' && /\n/.test(token.value)) || token.type === 'endOfFile')) {
return this.__resolveReference(this.globalObject, accessChain);
}
switch (token.type) {
case'keyword':
if (context.stage === '404') {
if (token.value === 'return') {
context.foundReturn = true;
context.stage = 'expression';
}
}
break;
case'identifier':
case'DotAccess':
accessChain.push(token.value);
console.log(188, context.stage);
if (context.stage === '404')
context.stage = 'Id';
break;
case'number':
let n = parseNumber(token.value, parseNumber.disallowNaN);
n = context.unaryMinus ? -n : n;
if (context.foundReturn && context.stage === 'expression') {
return n;
} else if (context.stage === 'argument') {
context.functionChain[context.functionChain.length - 1].with.push(n);
context.stage = 'argument,';
}
break;
case'BigInt':
let bigint = BigInt(token.value);
bigint = context.unaryMinus ? -bigint : bigint;
if (context.foundReturn && context.stage === 'expression') {
return bigint;
} else if (context.stage === 'argument') {
context.functionChain[context.functionChain.length - 1].with.push(bigint);
context.stage = 'argument,';
}
break;
case'string':
if (context.stage === 'argument') {
context.functionChain[context.functionChain.length - 1].with.push(token.value);
context.stage = 'argument,';
} else if (context.foundReturn) {
return token.value;
}
break;
case'operator':
if (token.value === '=' && context.stage === 'Id') {
context.setVar.push([...accessChain]);
context.stage = 'expression';
accessChain.length = 0;
} else if (token.value === '-') {
context.unaryMinus = true;
}
break;
case'delimiter':
if (token.value === '(') {
if ('argument argument, Id expression'.split(' ').includes(context.stage)) {
console.log('accessChain', accessChain, context.stage);
context.functionChain.push({'accessTo': [...accessChain], with: []});
context.stage = 'argument';
accessChain.length = 0;
}
} else if (token.value === ';' &&
'argument argument, Id expression'.split(' ').includes(context.stage)) {
if (context.stage === 'expression') {
preformAssignment(context, accessChain);
}
} else if (token.value === ';') {
throw new Error(JSON.stringify({context, accessChain}));
} else if (token.value === ')' && 'argument argument,'.split(' ').includes(context.stage)) {
const theFunction = context.functionChain.pop();//[context.functionChain.length - 1];
const theActualFunction = this.__resolveReference(this.globalObject, theFunction.accessTo);
/*console.log(
this.__resolveReference(this.globalObject,
theFunction.accessTo),
theFunction.accessTo, theFunction.with);*/
const type = typeOf(theActualFunction);
if (type === 'undefined' || type === 'NULL') {
console.error(theFunction, theActualFunction);
throw new TypeError(`do not call undefined, you attempted to call (${theFunction.accessTo.join('.')})`);
} else if ((type === 'function' || theActualFunction.constructor === __SandBoxedBuiltInFunction)
|| (type === 'object' && (SandboxedFunction.prototype.__call in theActualFunction))) {
let returnValue = undefined;
if (theActualFunction.constructor === __SandBoxedBuiltInFunction) {
returnValue = theActualFunction.callMe({
length: theFunction.with.length,
parameters: theFunction.with,
});
} else if (type === 'function') {
returnValue = theActualFunction(...theFunction.with);
} else if (type === 'object') {
if (SandboxedFunction.prototype.__call in theActualFunction) {
returnValue = theActualFunction[SandboxedFunction.prototype.__call](...theFunction.with);
} else if (SandboxedFunction.prototype.__callsp in theActualFunction) {
returnValue = theActualFunction[SandboxedFunction.prototype.__callsp]({
length: [...theFunction.with].length, parameters: [...theFunction.with],
});
}
}
const returnIn = context.functionChain[context.functionChain.length - 1];
if (returnIn !== undefined && returnIn.length > 0) {
returnIn.with.push(returnValue);
} else if (context.foundReturn) {
return returnValue;
}
} else {
throw new TypeError(`${type} is not callable (${theActualFunction})`);
}
if (context.functionChain.length > 0) {
context.stage = 'argument,';
} else {
console.info('context', context, accessChain);
context.stage = '404';
}
} else if (token.value === ',' && context.stage === 'argument,') {
context.stage = 'argument';
}
break;
case'whitespace':
if (context.foundReturn && /\n/.test(token.value)) {
console.warn('\\n before return found');
return undefined;
}
break;
case'templateLiteral':
break;
}
}
return undefined;
};
//--
SandboxedFunction.prototype.__tokenize = function (jsCode) {
const tokens = [];
const regexPatterns = [
{type: "keyword", regex: /\b(if|else|return|function|var|let|const|for|while|true|false|null)\b/},
{type: "comment", regex: /\/\/.*|\/\*[\s\S]*?\*\//},
{type: "identifier", regex: /\b[a-zA-Z_][a-zA-Z0-9_]*\b/},
{type: "number", regex: /\b\d+(\.\d+)?\b/},
{type: "string", regex: /(["'])(?:(?!\1).)*\1/},
{type: "operator", regex: /[+\-*/=<>!&|]+/},
{type: "delimiter", regex: /[{}()\[\];,]/},
{type: "whitespace", regex: /\s+/},
{type: "DotAccess", regex: /\.\b[a-zA-Z_][a-zA-Z0-9_]*\b/},
{type: "templateLiteral", regex: /`[^`]*`/},
{type: "BigInt", regex: /\b\d+n\b/},
];
jsCode = normalize_newlines(String(jsCode));
while (jsCode.length > 0) {
let match = null;
for (const {type, regex} of regexPatterns) {
const result = regex.exec(jsCode);
if (result && result.index === 0) {
match = {type, value: result[0]};
break;
}
}
if (!match) {
throw new Error(`Unrecognized token at: \`\`\`${jsCode.slice(0, 10)}\`\`\``);
}
let offset = 0;
/*if (match.type !== "whitespace" && match.type !== "comment") {tokens.push(match);}*/
if (match.type === 'DotAccess') {
match.value = String(match.value).replace(/^\./, '');
offset++;
}
tokens.push(match);
jsCode = jsCode.slice(match.value.length + offset);
}
return __array_append(tokens, {type: 'endOfFile', value: 'EOF'});
};