@nyariv/sandboxjs 0.8.23 → 0.8.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +6 -0
- package/.eslintrc.js +22 -0
- package/.prettierrc +4 -0
- package/.vscode/settings.json +4 -0
- package/README.md +1 -1
- package/build/Sandbox.d.ts +11 -79
- package/build/Sandbox.js +21 -216
- package/build/SandboxExec.d.ts +25 -0
- package/build/SandboxExec.js +169 -0
- package/build/eval.d.ts +18 -0
- package/build/eval.js +43 -0
- package/build/executor.d.ts +29 -90
- package/build/executor.js +249 -328
- package/build/parser.d.ts +8 -239
- package/build/parser.js +345 -444
- package/build/unraw.js +13 -16
- package/build/utils.d.ts +242 -0
- package/build/utils.js +276 -0
- package/dist/Sandbox.d.ts +11 -79
- package/dist/Sandbox.js +106 -1
- package/dist/Sandbox.js.map +1 -1
- package/dist/Sandbox.min.js +2 -0
- package/dist/Sandbox.min.js.map +1 -0
- package/dist/SandboxExec.d.ts +25 -0
- package/dist/SandboxExec.js +173 -0
- package/dist/SandboxExec.js.map +1 -0
- package/dist/SandboxExec.min.js +2 -0
- package/dist/SandboxExec.min.js.map +1 -0
- package/dist/eval.d.ts +18 -0
- package/dist/executor.d.ts +29 -90
- package/dist/executor.js +1270 -0
- package/dist/executor.js.map +1 -0
- package/dist/node/Sandbox.d.ts +11 -79
- package/dist/node/Sandbox.js +36 -3150
- package/dist/node/SandboxExec.d.ts +25 -0
- package/dist/node/SandboxExec.js +176 -0
- package/dist/node/eval.d.ts +18 -0
- package/dist/node/executor.d.ts +29 -90
- package/dist/node/executor.js +1289 -0
- package/dist/node/parser.d.ts +8 -239
- package/dist/node/parser.js +1527 -0
- package/dist/node/utils.d.ts +242 -0
- package/dist/node/utils.js +290 -0
- package/dist/parser.d.ts +8 -239
- package/dist/parser.js +1513 -0
- package/dist/parser.js.map +1 -0
- package/dist/utils.d.ts +242 -0
- package/dist/utils.js +279 -0
- package/dist/utils.js.map +1 -0
- package/jest.config.js +200 -0
- package/package.json +16 -5
- package/tsconfig.jest.json +16 -0
- package/.github/workflows/npm-publish.yml +0 -34
- package/rollup.config.mjs +0 -33
package/dist/node/Sandbox.js
CHANGED
|
@@ -2,1836 +2,32 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* @param hex A string containing a hexadecimal number.
|
|
10
|
-
* @returns The parsed integer, or `NaN` if the string is not a valid hex
|
|
11
|
-
* number.
|
|
12
|
-
*/
|
|
13
|
-
function parseHexToInt(hex) {
|
|
14
|
-
const isOnlyHexChars = !hex.match(/[^a-f0-9]/i);
|
|
15
|
-
return isOnlyHexChars ? parseInt(hex, 16) : NaN;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Check the validity and length of a hexadecimal code and optionally enforces
|
|
19
|
-
* a specific number of hex digits.
|
|
20
|
-
* @param hex The string to validate and parse.
|
|
21
|
-
* @param errorName The name of the error message to throw a `SyntaxError` with
|
|
22
|
-
* if `hex` is invalid. This is used to index `errorMessages`.
|
|
23
|
-
* @param enforcedLength If provided, will throw an error if `hex` is not
|
|
24
|
-
* exactly this many characters.
|
|
25
|
-
* @returns The parsed hex number as a normal number.
|
|
26
|
-
* @throws {SyntaxError} If the code is not valid.
|
|
27
|
-
*/
|
|
28
|
-
function validateAndParseHex(hex, errorName, enforcedLength) {
|
|
29
|
-
const parsedHex = parseHexToInt(hex);
|
|
30
|
-
if (Number.isNaN(parsedHex) ||
|
|
31
|
-
(enforcedLength !== undefined && enforcedLength !== hex.length)) {
|
|
32
|
-
throw new SyntaxError(errorName + ': ' + hex);
|
|
33
|
-
}
|
|
34
|
-
return parsedHex;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Parse a two-digit hexadecimal character escape code.
|
|
38
|
-
* @param code The two-digit hexadecimal number that represents the character to
|
|
39
|
-
* output.
|
|
40
|
-
* @returns The single character represented by the code.
|
|
41
|
-
* @throws {SyntaxError} If the code is not valid hex or is not the right
|
|
42
|
-
* length.
|
|
43
|
-
*/
|
|
44
|
-
function parseHexadecimalCode(code) {
|
|
45
|
-
const parsedCode = validateAndParseHex(code, 'Malformed Hexadecimal', 2);
|
|
46
|
-
return String.fromCharCode(parsedCode);
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Parse a four-digit Unicode character escape code.
|
|
50
|
-
* @param code The four-digit unicode number that represents the character to
|
|
51
|
-
* output.
|
|
52
|
-
* @param surrogateCode Optional four-digit unicode surrogate that represents
|
|
53
|
-
* the other half of the character to output.
|
|
54
|
-
* @returns The single character represented by the code.
|
|
55
|
-
* @throws {SyntaxError} If the codes are not valid hex or are not the right
|
|
56
|
-
* length.
|
|
57
|
-
*/
|
|
58
|
-
function parseUnicodeCode(code, surrogateCode) {
|
|
59
|
-
const parsedCode = validateAndParseHex(code, 'Malformed Unicode', 4);
|
|
60
|
-
if (surrogateCode !== undefined) {
|
|
61
|
-
const parsedSurrogateCode = validateAndParseHex(surrogateCode, 'Malformed Unicode', 4);
|
|
62
|
-
return String.fromCharCode(parsedCode, parsedSurrogateCode);
|
|
63
|
-
}
|
|
64
|
-
return String.fromCharCode(parsedCode);
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Test if the text is surrounded by curly braces (`{}`).
|
|
68
|
-
* @param text Text to check.
|
|
69
|
-
* @returns `true` if the text is in the form `{*}`.
|
|
70
|
-
*/
|
|
71
|
-
function isCurlyBraced(text) {
|
|
72
|
-
return text.charAt(0) === "{" && text.charAt(text.length - 1) === "}";
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Parse a Unicode code point character escape code.
|
|
76
|
-
* @param codePoint A unicode escape code point, including the surrounding curly
|
|
77
|
-
* braces.
|
|
78
|
-
* @returns The single character represented by the code.
|
|
79
|
-
* @throws {SyntaxError} If the code is not valid hex or does not have the
|
|
80
|
-
* surrounding curly braces.
|
|
81
|
-
*/
|
|
82
|
-
function parseUnicodeCodePointCode(codePoint) {
|
|
83
|
-
if (!isCurlyBraced(codePoint)) {
|
|
84
|
-
throw new SyntaxError('Malformed Unicode: +' + codePoint);
|
|
85
|
-
}
|
|
86
|
-
const withoutBraces = codePoint.slice(1, -1);
|
|
87
|
-
const parsedCode = validateAndParseHex(withoutBraces, 'Malformed Unicode');
|
|
88
|
-
try {
|
|
89
|
-
return String.fromCodePoint(parsedCode);
|
|
90
|
-
}
|
|
91
|
-
catch (err) {
|
|
92
|
-
throw err instanceof RangeError
|
|
93
|
-
? new SyntaxError('Code Point Limit:' + parsedCode)
|
|
94
|
-
: err;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Map of unescaped letters to their corresponding special JS escape characters.
|
|
99
|
-
* Intentionally does not include characters that map to themselves like "\'".
|
|
100
|
-
*/
|
|
101
|
-
const singleCharacterEscapes = new Map([
|
|
102
|
-
["b", "\b"],
|
|
103
|
-
["f", "\f"],
|
|
104
|
-
["n", "\n"],
|
|
105
|
-
["r", "\r"],
|
|
106
|
-
["t", "\t"],
|
|
107
|
-
["v", "\v"],
|
|
108
|
-
["0", "\0"]
|
|
109
|
-
]);
|
|
110
|
-
/**
|
|
111
|
-
* Parse a single character escape sequence and return the matching character.
|
|
112
|
-
* If none is matched, defaults to `code`.
|
|
113
|
-
* @param code A single character code.
|
|
114
|
-
*/
|
|
115
|
-
function parseSingleCharacterCode(code) {
|
|
116
|
-
return singleCharacterEscapes.get(code) || code;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Matches every escape sequence possible, including invalid ones.
|
|
120
|
-
*
|
|
121
|
-
* All capture groups (described below) are unique (only one will match), except
|
|
122
|
-
* for 4, which can only potentially match if 3 does.
|
|
123
|
-
*
|
|
124
|
-
* **Capture Groups:**
|
|
125
|
-
* 0. A single backslash
|
|
126
|
-
* 1. Hexadecimal code
|
|
127
|
-
* 2. Unicode code point code with surrounding curly braces
|
|
128
|
-
* 3. Unicode escape code with surrogate
|
|
129
|
-
* 4. Surrogate code
|
|
130
|
-
* 5. Unicode escape code without surrogate
|
|
131
|
-
* 6. Octal code _NOTE: includes "0"._
|
|
132
|
-
* 7. A single character (will never be \, x, u, or 0-3)
|
|
133
|
-
*/
|
|
134
|
-
const escapeMatch = /\\(?:(\\)|x([\s\S]{0,2})|u(\{[^}]*\}?)|u([\s\S]{4})\\u([^{][\s\S]{0,3})|u([\s\S]{0,4})|([0-3]?[0-7]{1,2})|([\s\S])|$)/g;
|
|
135
|
-
/**
|
|
136
|
-
* Replace raw escape character strings with their escape characters.
|
|
137
|
-
* @param raw A string where escape characters are represented as raw string
|
|
138
|
-
* values like `\'` rather than `'`.
|
|
139
|
-
* @param allowOctals If `true`, will process the now-deprecated octal escape
|
|
140
|
-
* sequences (ie, `\111`).
|
|
141
|
-
* @returns The processed string, with escape characters replaced by their
|
|
142
|
-
* respective actual Unicode characters.
|
|
143
|
-
*/
|
|
144
|
-
function unraw(raw) {
|
|
145
|
-
return raw.replace(escapeMatch, function (_, backslash, hex, codePoint, unicodeWithSurrogate, surrogate, unicode, octal, singleCharacter) {
|
|
146
|
-
// Compare groups to undefined because empty strings mean different errors
|
|
147
|
-
// Otherwise, `\u` would fail the same as `\` which is wrong.
|
|
148
|
-
if (backslash !== undefined) {
|
|
149
|
-
return "\\";
|
|
150
|
-
}
|
|
151
|
-
if (hex !== undefined) {
|
|
152
|
-
return parseHexadecimalCode(hex);
|
|
153
|
-
}
|
|
154
|
-
if (codePoint !== undefined) {
|
|
155
|
-
return parseUnicodeCodePointCode(codePoint);
|
|
156
|
-
}
|
|
157
|
-
if (unicodeWithSurrogate !== undefined) {
|
|
158
|
-
return parseUnicodeCode(unicodeWithSurrogate, surrogate);
|
|
159
|
-
}
|
|
160
|
-
if (unicode !== undefined) {
|
|
161
|
-
return parseUnicodeCode(unicode);
|
|
162
|
-
}
|
|
163
|
-
if (octal === "0") {
|
|
164
|
-
return "\0";
|
|
165
|
-
}
|
|
166
|
-
if (octal !== undefined) {
|
|
167
|
-
throw new SyntaxError('Octal Deprecation: ' + octal);
|
|
168
|
-
}
|
|
169
|
-
if (singleCharacter !== undefined) {
|
|
170
|
-
return parseSingleCharacterCode(singleCharacter);
|
|
171
|
-
}
|
|
172
|
-
throw new SyntaxError('End of string');
|
|
173
|
-
});
|
|
174
|
-
}
|
|
5
|
+
var utils = require('./utils.js');
|
|
6
|
+
var executor = require('./executor.js');
|
|
7
|
+
var parser = require('./parser.js');
|
|
8
|
+
var SandboxExec = require('./SandboxExec.js');
|
|
175
9
|
|
|
176
|
-
function
|
|
177
|
-
return [obj.op, obj.a, obj.b];
|
|
178
|
-
}
|
|
179
|
-
let lispTypes = new Map();
|
|
180
|
-
class ParseError extends Error {
|
|
181
|
-
constructor(message, code) {
|
|
182
|
-
super(message + ": " + code.substring(0, 40));
|
|
183
|
-
this.code = code;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
const inlineIfElse = /^:/;
|
|
187
|
-
const elseIf = /^else(?![\w\$])/;
|
|
188
|
-
const ifElse = /^if(?![\w\$])/;
|
|
189
|
-
const space = /^\s/;
|
|
190
|
-
let expectTypes = {
|
|
191
|
-
splitter: {
|
|
192
|
-
types: {
|
|
193
|
-
opHigh: /^(\/|\*\*|\*(?!\*)|\%)(?!\=)/,
|
|
194
|
-
op: /^(\+(?!(\+))|\-(?!(\-)))(?!\=)/,
|
|
195
|
-
comparitor: /^(<=|>=|<(?!<)|>(?!>)|!==|!=(?!\=)|===|==)/,
|
|
196
|
-
boolOp: /^(&&|\|\||instanceof(?![\w\$])|in(?![\w\$]))/,
|
|
197
|
-
bitwise: /^(&(?!&)|\|(?!\|)|\^|<<|>>(?!>)|>>>)(?!\=)/,
|
|
198
|
-
},
|
|
199
|
-
next: [
|
|
200
|
-
'modifier',
|
|
201
|
-
'value',
|
|
202
|
-
'prop',
|
|
203
|
-
'incrementerBefore',
|
|
204
|
-
]
|
|
205
|
-
},
|
|
206
|
-
inlineIf: {
|
|
207
|
-
types: {
|
|
208
|
-
inlineIf: /^\?(?!\.(?!\d))/,
|
|
209
|
-
},
|
|
210
|
-
next: [
|
|
211
|
-
'expEnd'
|
|
212
|
-
]
|
|
213
|
-
},
|
|
214
|
-
assignment: {
|
|
215
|
-
types: {
|
|
216
|
-
assignModify: /^(\-=|\+=|\/=|\*\*=|\*=|%=|\^=|\&=|\|=|>>>=|>>=|<<=)/,
|
|
217
|
-
assign: /^(=)(?!=)/
|
|
218
|
-
},
|
|
219
|
-
next: [
|
|
220
|
-
'modifier',
|
|
221
|
-
'value',
|
|
222
|
-
'prop',
|
|
223
|
-
'incrementerBefore',
|
|
224
|
-
]
|
|
225
|
-
},
|
|
226
|
-
incrementerBefore: {
|
|
227
|
-
types: { incrementerBefore: /^(\+\+|\-\-)/ },
|
|
228
|
-
next: [
|
|
229
|
-
'prop',
|
|
230
|
-
]
|
|
231
|
-
},
|
|
232
|
-
expEdge: {
|
|
233
|
-
types: {
|
|
234
|
-
call: /^(\?\.)?[\(]/,
|
|
235
|
-
incrementerAfter: /^(\+\+|\-\-)/
|
|
236
|
-
},
|
|
237
|
-
next: [
|
|
238
|
-
'splitter',
|
|
239
|
-
'expEdge',
|
|
240
|
-
'dot',
|
|
241
|
-
'inlineIf',
|
|
242
|
-
'expEnd'
|
|
243
|
-
]
|
|
244
|
-
},
|
|
245
|
-
modifier: {
|
|
246
|
-
types: {
|
|
247
|
-
not: /^!/,
|
|
248
|
-
inverse: /^~/,
|
|
249
|
-
negative: /^\-(?!\-)/,
|
|
250
|
-
positive: /^\+(?!\+)/,
|
|
251
|
-
typeof: /^typeof(?![\w\$])/,
|
|
252
|
-
delete: /^delete(?![\w\$])/,
|
|
253
|
-
},
|
|
254
|
-
next: [
|
|
255
|
-
'modifier',
|
|
256
|
-
'value',
|
|
257
|
-
'prop',
|
|
258
|
-
'incrementerBefore',
|
|
259
|
-
]
|
|
260
|
-
},
|
|
261
|
-
dot: {
|
|
262
|
-
types: {
|
|
263
|
-
arrayProp: /^(\?\.)?\[/,
|
|
264
|
-
dot: /^(\?)?\.(?=\s*[a-zA-Z\$\_])/,
|
|
265
|
-
},
|
|
266
|
-
next: [
|
|
267
|
-
'splitter',
|
|
268
|
-
'assignment',
|
|
269
|
-
'expEdge',
|
|
270
|
-
'dot',
|
|
271
|
-
'inlineIf',
|
|
272
|
-
'expEnd'
|
|
273
|
-
]
|
|
274
|
-
},
|
|
275
|
-
prop: {
|
|
276
|
-
types: {
|
|
277
|
-
prop: /^[a-zA-Z\$\_][a-zA-Z\d\$\_]*/,
|
|
278
|
-
},
|
|
279
|
-
next: [
|
|
280
|
-
'splitter',
|
|
281
|
-
'assignment',
|
|
282
|
-
'expEdge',
|
|
283
|
-
'dot',
|
|
284
|
-
'inlineIf',
|
|
285
|
-
'expEnd'
|
|
286
|
-
]
|
|
287
|
-
},
|
|
288
|
-
value: {
|
|
289
|
-
types: {
|
|
290
|
-
createObject: /^\{/,
|
|
291
|
-
createArray: /^\[/,
|
|
292
|
-
number: /^(0x[\da-f]+(_[\da-f]+)*|(\d+(_\d+)*(\.\d+(_\d+)*)?|\.\d+(_\d+)*))(e[\+\-]?\d+(_\d+)*)?(n)?(?!\d)/i,
|
|
293
|
-
string: /^"(\d+)"/,
|
|
294
|
-
literal: /^`(\d+)`/,
|
|
295
|
-
regex: /^\/(\d+)\/r(?![\w\$])/,
|
|
296
|
-
boolean: /^(true|false)(?![\w\$])/,
|
|
297
|
-
null: /^null(?![\w\$])/,
|
|
298
|
-
und: /^undefined(?![\w\$])/,
|
|
299
|
-
arrowFunctionSingle: /^(async\s+)?([a-zA-Z\$_][a-zA-Z\d\$_]*)\s*=>\s*({)?/,
|
|
300
|
-
arrowFunction: /^(async\s*)?\(\s*((\.\.\.)?\s*[a-zA-Z\$_][a-zA-Z\d\$_]*(\s*,\s*(\.\.\.)?\s*[a-zA-Z\$_][a-zA-Z\d\$_]*)*)?\s*\)\s*=>\s*({)?/,
|
|
301
|
-
inlineFunction: /^(async\s+)?function(\s*[a-zA-Z\$_][a-zA-Z\d\$_]*)?\s*\(\s*((\.\.\.)?\s*[a-zA-Z\$_][a-zA-Z\d\$_]*(\s*,\s*(\.\.\.)?\s*[a-zA-Z\$_][a-zA-Z\d\$_]*)*)?\s*\)\s*{/,
|
|
302
|
-
group: /^\(/,
|
|
303
|
-
NaN: /^NaN(?![\w\$])/,
|
|
304
|
-
Infinity: /^Infinity(?![\w\$])/,
|
|
305
|
-
void: /^void(?![\w\$])\s*/,
|
|
306
|
-
await: /^await(?![\w\$])\s*/,
|
|
307
|
-
new: /^new(?![\w\$])\s*/,
|
|
308
|
-
},
|
|
309
|
-
next: [
|
|
310
|
-
'splitter',
|
|
311
|
-
'expEdge',
|
|
312
|
-
'dot',
|
|
313
|
-
'inlineIf',
|
|
314
|
-
'expEnd'
|
|
315
|
-
]
|
|
316
|
-
},
|
|
317
|
-
initialize: {
|
|
318
|
-
types: {
|
|
319
|
-
initialize: /^(var|let|const)\s+([a-zA-Z\$_][a-zA-Z\d\$_]*)\s*(=)?/,
|
|
320
|
-
return: /^return(?![\w\$])/,
|
|
321
|
-
throw: /^throw(?![\w\$])\s*/
|
|
322
|
-
},
|
|
323
|
-
next: [
|
|
324
|
-
'modifier',
|
|
325
|
-
'value',
|
|
326
|
-
'prop',
|
|
327
|
-
'incrementerBefore',
|
|
328
|
-
'expEnd'
|
|
329
|
-
]
|
|
330
|
-
},
|
|
331
|
-
spreadObject: {
|
|
332
|
-
types: {
|
|
333
|
-
spreadObject: /^\.\.\./
|
|
334
|
-
},
|
|
335
|
-
next: [
|
|
336
|
-
'value',
|
|
337
|
-
'prop',
|
|
338
|
-
]
|
|
339
|
-
},
|
|
340
|
-
spreadArray: {
|
|
341
|
-
types: {
|
|
342
|
-
spreadArray: /^\.\.\./
|
|
343
|
-
},
|
|
344
|
-
next: [
|
|
345
|
-
'value',
|
|
346
|
-
'prop',
|
|
347
|
-
]
|
|
348
|
-
},
|
|
349
|
-
expEnd: { types: {}, next: [] },
|
|
350
|
-
expFunction: {
|
|
351
|
-
types: {
|
|
352
|
-
function: /^(async\s+)?function(\s*[a-zA-Z\$_][a-zA-Z\d\$_]*)\s*\(\s*((\.\.\.)?\s*[a-zA-Z\$_][a-zA-Z\d\$_]*(\s*,\s*(\.\.\.)?\s*[a-zA-Z\$_][a-zA-Z\d\$_]*)*)?\s*\)\s*{/,
|
|
353
|
-
},
|
|
354
|
-
next: [
|
|
355
|
-
'expEdge',
|
|
356
|
-
'expEnd'
|
|
357
|
-
]
|
|
358
|
-
},
|
|
359
|
-
expSingle: {
|
|
360
|
-
types: {
|
|
361
|
-
for: /^(([a-zA-Z\$\_][\w\$]*)\s*:)?\s*for\s*\(/,
|
|
362
|
-
do: /^(([a-zA-Z\$\_][\w\$]*)\s*:)?\s*do(?![\w\$])\s*(\{)?/,
|
|
363
|
-
while: /^(([a-zA-Z\$\_][\w\$]*)\s*:)?\s*while\s*\(/,
|
|
364
|
-
loopAction: /^(break|continue)(?![\w\$])\s*([a-zA-Z\$\_][\w\$]*)?/,
|
|
365
|
-
if: /^((([a-zA-Z\$\_][\w\$]*)\s*:)?\s*)if\s*\(/,
|
|
366
|
-
try: /^try\s*{/,
|
|
367
|
-
block: /^{/,
|
|
368
|
-
switch: /^(([a-zA-Z\$\_][\w\$]*)\s*:)?\s*switch\s*\(/,
|
|
369
|
-
},
|
|
370
|
-
next: [
|
|
371
|
-
'expEnd'
|
|
372
|
-
]
|
|
373
|
-
}
|
|
374
|
-
};
|
|
375
|
-
let closings = {
|
|
376
|
-
"(": ")",
|
|
377
|
-
"[": "]",
|
|
378
|
-
"{": "}",
|
|
379
|
-
"'": "'",
|
|
380
|
-
'"': '"',
|
|
381
|
-
"`": "`"
|
|
382
|
-
};
|
|
383
|
-
function testMultiple(str, tests) {
|
|
384
|
-
let found;
|
|
385
|
-
for (let i = 0; i < tests.length; i++) {
|
|
386
|
-
const test = tests[i];
|
|
387
|
-
found = test.exec(str);
|
|
388
|
-
if (found)
|
|
389
|
-
break;
|
|
390
|
-
}
|
|
391
|
-
return found;
|
|
392
|
-
}
|
|
393
|
-
class CodeString {
|
|
394
|
-
constructor(str) {
|
|
395
|
-
this.ref = { str: "" };
|
|
396
|
-
if (str instanceof CodeString) {
|
|
397
|
-
this.ref = str.ref;
|
|
398
|
-
this.start = str.start;
|
|
399
|
-
this.end = str.end;
|
|
400
|
-
}
|
|
401
|
-
else {
|
|
402
|
-
this.ref.str = str;
|
|
403
|
-
this.start = 0;
|
|
404
|
-
this.end = str.length;
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
substring(start, end) {
|
|
408
|
-
if (!this.length)
|
|
409
|
-
return this;
|
|
410
|
-
start = this.start + start;
|
|
411
|
-
if (start < 0) {
|
|
412
|
-
start = 0;
|
|
413
|
-
}
|
|
414
|
-
if (start > this.end) {
|
|
415
|
-
start = this.end;
|
|
416
|
-
}
|
|
417
|
-
end = end === undefined ? this.end : this.start + end;
|
|
418
|
-
if (end < 0) {
|
|
419
|
-
end = 0;
|
|
420
|
-
}
|
|
421
|
-
if (end > this.end) {
|
|
422
|
-
end = this.end;
|
|
423
|
-
}
|
|
424
|
-
const code = new CodeString(this);
|
|
425
|
-
code.start = start;
|
|
426
|
-
code.end = end;
|
|
427
|
-
return code;
|
|
428
|
-
}
|
|
429
|
-
get length() {
|
|
430
|
-
const len = this.end - this.start;
|
|
431
|
-
return len < 0 ? 0 : len;
|
|
432
|
-
}
|
|
433
|
-
char(i) {
|
|
434
|
-
if (this.start === this.end)
|
|
435
|
-
return undefined;
|
|
436
|
-
return this.ref.str[this.start + i];
|
|
437
|
-
}
|
|
438
|
-
toString() {
|
|
439
|
-
return this.ref.str.substring(this.start, this.end);
|
|
440
|
-
}
|
|
441
|
-
trimStart() {
|
|
442
|
-
const found = /^\s+/.exec(this.toString());
|
|
443
|
-
const code = new CodeString(this);
|
|
444
|
-
if (found) {
|
|
445
|
-
code.start += found[0].length;
|
|
446
|
-
}
|
|
447
|
-
return code;
|
|
448
|
-
}
|
|
449
|
-
slice(start, end) {
|
|
450
|
-
if (start < 0) {
|
|
451
|
-
start = this.end - this.start + start;
|
|
452
|
-
}
|
|
453
|
-
if (start < 0) {
|
|
454
|
-
start = 0;
|
|
455
|
-
}
|
|
456
|
-
if (end === undefined) {
|
|
457
|
-
end = this.end - this.start;
|
|
458
|
-
}
|
|
459
|
-
if (end < 0) {
|
|
460
|
-
end = this.end - this.start + end;
|
|
461
|
-
}
|
|
462
|
-
if (end < 0) {
|
|
463
|
-
end = 0;
|
|
464
|
-
}
|
|
465
|
-
return this.substring(start, end);
|
|
466
|
-
}
|
|
467
|
-
trim() {
|
|
468
|
-
const code = this.trimStart();
|
|
469
|
-
const found = /\s+$/.exec(code.toString());
|
|
470
|
-
if (found) {
|
|
471
|
-
code.end -= found[0].length;
|
|
472
|
-
}
|
|
473
|
-
return code;
|
|
474
|
-
}
|
|
475
|
-
valueOf() {
|
|
476
|
-
return this.toString();
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
const emptyString = new CodeString("");
|
|
480
|
-
const okFirstChars = /^[\+\-~ !]/;
|
|
481
|
-
const aNumber = expectTypes.value.types.number;
|
|
482
|
-
const wordReg = /^((if|for|else|while|do|function)(?![\w\$])|[\w\$]+)/;
|
|
483
|
-
const semiColon = /^;/;
|
|
484
|
-
const insertedSemicolons = new WeakMap();
|
|
485
|
-
const quoteCache = new WeakMap();
|
|
486
|
-
function restOfExp(constants, part, tests, quote, firstOpening, closingsTests, details = {}) {
|
|
487
|
-
if (!part.length) {
|
|
488
|
-
return part;
|
|
489
|
-
}
|
|
490
|
-
details.words = details.words || [];
|
|
491
|
-
let isStart = true;
|
|
492
|
-
tests = tests || [];
|
|
493
|
-
const hasSemiTest = tests.includes(semiColon);
|
|
494
|
-
if (hasSemiTest) {
|
|
495
|
-
tests = tests.filter((a) => a !== semiColon);
|
|
496
|
-
}
|
|
497
|
-
const insertedSemis = insertedSemicolons.get(part.ref) || [];
|
|
498
|
-
const cache = quoteCache.get(part.ref) || new Map();
|
|
499
|
-
quoteCache.set(part.ref, cache);
|
|
500
|
-
if (quote && cache.has(part.start - 1)) {
|
|
501
|
-
return part.substring(0, cache.get(part.start - 1) - part.start);
|
|
502
|
-
}
|
|
503
|
-
let escape = false;
|
|
504
|
-
let done = false;
|
|
505
|
-
let lastChar = "";
|
|
506
|
-
let isOneLiner = false;
|
|
507
|
-
let i;
|
|
508
|
-
let lastInertedSemi = false;
|
|
509
|
-
for (i = 0; i < part.length && !done; i++) {
|
|
510
|
-
let char = part.char(i);
|
|
511
|
-
if (quote === '"' || quote === "'" || quote === "`") {
|
|
512
|
-
if (quote === "`" && char === "$" && part.char(i + 1) === "{" && !escape) {
|
|
513
|
-
let skip = restOfExp(constants, part.substring(i + 2), [], "{");
|
|
514
|
-
i += skip.length + 2;
|
|
515
|
-
}
|
|
516
|
-
else if (char === quote && !escape) {
|
|
517
|
-
return part.substring(0, i);
|
|
518
|
-
}
|
|
519
|
-
escape = !escape && char === "\\";
|
|
520
|
-
}
|
|
521
|
-
else if (closings[char]) {
|
|
522
|
-
if (!lastInertedSemi && insertedSemis[i + part.start]) {
|
|
523
|
-
lastInertedSemi = true;
|
|
524
|
-
if (hasSemiTest) {
|
|
525
|
-
break;
|
|
526
|
-
}
|
|
527
|
-
i--;
|
|
528
|
-
lastChar = ';';
|
|
529
|
-
continue;
|
|
530
|
-
}
|
|
531
|
-
if (isOneLiner && char === "{") {
|
|
532
|
-
isOneLiner = false;
|
|
533
|
-
}
|
|
534
|
-
if (char === firstOpening) {
|
|
535
|
-
done = true;
|
|
536
|
-
break;
|
|
537
|
-
}
|
|
538
|
-
else {
|
|
539
|
-
let skip = restOfExp(constants, part.substring(i + 1), [], char);
|
|
540
|
-
cache.set(skip.start - 1, skip.end);
|
|
541
|
-
i += skip.length + 1;
|
|
542
|
-
isStart = false;
|
|
543
|
-
if (closingsTests) {
|
|
544
|
-
let sub = part.substring(i);
|
|
545
|
-
let found;
|
|
546
|
-
if (found = testMultiple(sub.toString(), closingsTests)) {
|
|
547
|
-
details.regRes = found;
|
|
548
|
-
done = true;
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
|
-
else if (!quote) {
|
|
554
|
-
let sub = part.substring(i).toString();
|
|
555
|
-
let foundWord;
|
|
556
|
-
let foundNumber;
|
|
557
|
-
if (closingsTests) {
|
|
558
|
-
let found;
|
|
559
|
-
if (found = testMultiple(sub, closingsTests)) {
|
|
560
|
-
details.regRes = found;
|
|
561
|
-
i++;
|
|
562
|
-
done = true;
|
|
563
|
-
break;
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
if (foundNumber = aNumber.exec(sub)) {
|
|
567
|
-
i += foundNumber[0].length - 1;
|
|
568
|
-
sub = part.substring(i).toString();
|
|
569
|
-
}
|
|
570
|
-
else if (lastChar != char) {
|
|
571
|
-
let found;
|
|
572
|
-
if (char === ';' || (insertedSemis[i + part.start] && !isStart && !lastInertedSemi)) {
|
|
573
|
-
if (hasSemiTest) {
|
|
574
|
-
found = [";"];
|
|
575
|
-
}
|
|
576
|
-
else if (insertedSemis[i + part.start]) {
|
|
577
|
-
lastInertedSemi = true;
|
|
578
|
-
i--;
|
|
579
|
-
lastChar = ';';
|
|
580
|
-
continue;
|
|
581
|
-
}
|
|
582
|
-
char = sub = ';';
|
|
583
|
-
}
|
|
584
|
-
else {
|
|
585
|
-
lastInertedSemi = false;
|
|
586
|
-
}
|
|
587
|
-
if (!found) {
|
|
588
|
-
found = testMultiple(sub, tests);
|
|
589
|
-
}
|
|
590
|
-
if (found) {
|
|
591
|
-
done = true;
|
|
592
|
-
}
|
|
593
|
-
if (!done && (foundWord = wordReg.exec(sub))) {
|
|
594
|
-
isOneLiner = true;
|
|
595
|
-
if (foundWord[0].length > 1) {
|
|
596
|
-
details.words.push(foundWord[1]);
|
|
597
|
-
details.lastAnyWord = foundWord[1];
|
|
598
|
-
if (foundWord[2]) {
|
|
599
|
-
details.lastWord = foundWord[2];
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
if (foundWord[0].length > 2) {
|
|
603
|
-
i += foundWord[0].length - 2;
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
if (isStart) {
|
|
608
|
-
if (okFirstChars.test(sub)) {
|
|
609
|
-
done = false;
|
|
610
|
-
}
|
|
611
|
-
else {
|
|
612
|
-
isStart = false;
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
if (done)
|
|
616
|
-
break;
|
|
617
|
-
}
|
|
618
|
-
else if (char === closings[quote]) {
|
|
619
|
-
return part.substring(0, i);
|
|
620
|
-
}
|
|
621
|
-
lastChar = char;
|
|
622
|
-
}
|
|
623
|
-
if (quote) {
|
|
624
|
-
throw new SyntaxError("Unclosed '" + quote + "'");
|
|
625
|
-
}
|
|
626
|
-
if (details) {
|
|
627
|
-
details.oneliner = isOneLiner;
|
|
628
|
-
}
|
|
629
|
-
return part.substring(0, i);
|
|
630
|
-
}
|
|
631
|
-
restOfExp.next = [
|
|
632
|
-
'splitter',
|
|
633
|
-
'expEnd',
|
|
634
|
-
'inlineIf'
|
|
635
|
-
];
|
|
636
|
-
const startingExecpted = ['initialize', 'expSingle', 'expFunction', 'value', 'modifier', 'prop', 'incrementerBefore', 'expEnd'];
|
|
637
|
-
const setLispType = (types, fn) => {
|
|
638
|
-
types.forEach((type) => {
|
|
639
|
-
lispTypes.set(type, fn);
|
|
640
|
-
});
|
|
641
|
-
};
|
|
642
|
-
const closingsCreate = {
|
|
643
|
-
'createArray': /^\]/,
|
|
644
|
-
'createObject': /^\}/,
|
|
645
|
-
'group': /^\)/,
|
|
646
|
-
'arrayProp': /^\]/,
|
|
647
|
-
'call': /^\)/
|
|
648
|
-
};
|
|
649
|
-
const typesCreate = {
|
|
650
|
-
'createArray': 12 /* LispType.CreateArray */,
|
|
651
|
-
'createObject': 22 /* LispType.CreateObject */,
|
|
652
|
-
'group': 23 /* LispType.Group */,
|
|
653
|
-
'arrayProp': 19 /* LispType.ArrayProp */,
|
|
654
|
-
'call': 5 /* LispType.Call */,
|
|
655
|
-
'prop': 1 /* LispType.Prop */,
|
|
656
|
-
'?prop': 20 /* LispType.PropOptional */,
|
|
657
|
-
'?call': 21 /* LispType.CallOptional */,
|
|
658
|
-
};
|
|
659
|
-
setLispType(['createArray', 'createObject', 'group', 'arrayProp', 'call'], (constants, type, part, res, expect, ctx) => {
|
|
660
|
-
let extract = emptyString;
|
|
661
|
-
let arg = [];
|
|
662
|
-
let end = false;
|
|
663
|
-
let i = res[0].length;
|
|
664
|
-
const start = i;
|
|
665
|
-
while (i < part.length && !end) {
|
|
666
|
-
extract = restOfExp(constants, part.substring(i), [
|
|
667
|
-
closingsCreate[type],
|
|
668
|
-
/^,/
|
|
669
|
-
]);
|
|
670
|
-
i += extract.length;
|
|
671
|
-
if (extract.trim().length) {
|
|
672
|
-
arg.push(extract);
|
|
673
|
-
}
|
|
674
|
-
if (part.char(i) !== ',') {
|
|
675
|
-
end = true;
|
|
676
|
-
}
|
|
677
|
-
else {
|
|
678
|
-
i++;
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
const next = ['value', 'modifier', 'prop', 'incrementerBefore', 'expEnd'];
|
|
682
|
-
let l;
|
|
683
|
-
let funcFound;
|
|
684
|
-
switch (type) {
|
|
685
|
-
case 'group':
|
|
686
|
-
case 'arrayProp':
|
|
687
|
-
l = lispifyExpr(constants, part.substring(start, i));
|
|
688
|
-
break;
|
|
689
|
-
case 'call':
|
|
690
|
-
case 'createArray':
|
|
691
|
-
// @TODO: support 'empty' values
|
|
692
|
-
l = arg.map((e) => lispify(constants, e, [...next, 'spreadArray']));
|
|
693
|
-
break;
|
|
694
|
-
case 'createObject':
|
|
695
|
-
l = arg.map((str) => {
|
|
696
|
-
str = str.trimStart();
|
|
697
|
-
let value;
|
|
698
|
-
let key = '';
|
|
699
|
-
funcFound = expectTypes.expFunction.types.function.exec('function ' + str);
|
|
700
|
-
if (funcFound) {
|
|
701
|
-
key = funcFound[2].trimStart();
|
|
702
|
-
value = lispify(constants, new CodeString('function ' + str.toString().replace(key, "")));
|
|
703
|
-
}
|
|
704
|
-
else {
|
|
705
|
-
let extract = restOfExp(constants, str, [/^:/]);
|
|
706
|
-
key = lispify(constants, extract, [...next, 'spreadObject']);
|
|
707
|
-
if (key[0] === 1 /* LispType.Prop */) {
|
|
708
|
-
key = key[2];
|
|
709
|
-
}
|
|
710
|
-
value = lispify(constants, str.substring(extract.length + 1));
|
|
711
|
-
}
|
|
712
|
-
return createLisp({
|
|
713
|
-
op: 6 /* LispType.KeyVal */,
|
|
714
|
-
a: key,
|
|
715
|
-
b: value
|
|
716
|
-
});
|
|
717
|
-
});
|
|
718
|
-
break;
|
|
719
|
-
}
|
|
720
|
-
let lisptype = (type === 'arrayProp' ? (res[1] ? 20 /* LispType.PropOptional */ : 1 /* LispType.Prop */) : (type === 'call' ? (res[1] ? 21 /* LispType.CallOptional */ : 5 /* LispType.Call */) : typesCreate[type]));
|
|
721
|
-
ctx.lispTree = lispify(constants, part.substring(i + 1), expectTypes[expect].next, createLisp({
|
|
722
|
-
op: lisptype,
|
|
723
|
-
a: ctx.lispTree,
|
|
724
|
-
b: l,
|
|
725
|
-
}));
|
|
726
|
-
});
|
|
727
|
-
const modifierTypes = {
|
|
728
|
-
'inverse': 64 /* LispType.Inverse */,
|
|
729
|
-
'not': 24 /* LispType.Not */,
|
|
730
|
-
'positive': 59 /* LispType.Positive */,
|
|
731
|
-
'negative': 58 /* LispType.Negative */,
|
|
732
|
-
'typeof': 60 /* LispType.Typeof */,
|
|
733
|
-
'delete': 61 /* LispType.Delete */
|
|
734
|
-
};
|
|
735
|
-
setLispType(['inverse', 'not', 'negative', 'positive', 'typeof', 'delete'], (constants, type, part, res, expect, ctx) => {
|
|
736
|
-
let extract = restOfExp(constants, part.substring(res[0].length), [/^([^\s\.\?\w\$]|\?[^\.])/]);
|
|
737
|
-
ctx.lispTree = lispify(constants, part.substring(extract.length + res[0].length), restOfExp.next, createLisp({
|
|
738
|
-
op: modifierTypes[type],
|
|
739
|
-
a: ctx.lispTree,
|
|
740
|
-
b: lispify(constants, extract, expectTypes[expect].next),
|
|
741
|
-
}));
|
|
742
|
-
});
|
|
743
|
-
const incrementTypes = {
|
|
744
|
-
'++$': 25 /* LispType.IncrementBefore */,
|
|
745
|
-
'--$': 27 /* LispType.DecrementBefore */,
|
|
746
|
-
'$++': 26 /* LispType.IncrementAfter */,
|
|
747
|
-
'$--': 28 /* LispType.DecrementAfter */
|
|
748
|
-
};
|
|
749
|
-
setLispType(['incrementerBefore'], (constants, type, part, res, expect, ctx) => {
|
|
750
|
-
let extract = restOfExp(constants, part.substring(2), [/^[^\s\.\w\$]/]);
|
|
751
|
-
ctx.lispTree = lispify(constants, part.substring(extract.length + 2), restOfExp.next, createLisp({
|
|
752
|
-
op: incrementTypes[res[0] + "$"],
|
|
753
|
-
a: lispify(constants, extract, expectTypes[expect].next),
|
|
754
|
-
b: 0 /* LispType.None */
|
|
755
|
-
}));
|
|
756
|
-
});
|
|
757
|
-
setLispType(['incrementerAfter'], (constants, type, part, res, expect, ctx) => {
|
|
758
|
-
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next, createLisp({
|
|
759
|
-
op: incrementTypes["$" + res[0]],
|
|
760
|
-
a: ctx.lispTree,
|
|
761
|
-
b: 0 /* LispType.None */
|
|
762
|
-
}));
|
|
763
|
-
});
|
|
764
|
-
const adderTypes = {
|
|
765
|
-
'&&': 29 /* LispType.And */,
|
|
766
|
-
'||': 30 /* LispType.Or */,
|
|
767
|
-
'instanceof': 62 /* LispType.Instanceof */,
|
|
768
|
-
'in': 63 /* LispType.In */,
|
|
769
|
-
'=': 9 /* LispType.Assign */,
|
|
770
|
-
'-=': 65 /* LispType.SubractEquals */,
|
|
771
|
-
'+=': 66 /* LispType.AddEquals */,
|
|
772
|
-
'/=': 67 /* LispType.DivideEquals */,
|
|
773
|
-
'**=': 68 /* LispType.PowerEquals */,
|
|
774
|
-
'*=': 69 /* LispType.MultiplyEquals */,
|
|
775
|
-
'%=': 70 /* LispType.ModulusEquals */,
|
|
776
|
-
'^=': 71 /* LispType.BitNegateEquals */,
|
|
777
|
-
'&=': 72 /* LispType.BitAndEquals */,
|
|
778
|
-
'|=': 73 /* LispType.BitOrEquals */,
|
|
779
|
-
'>>>=': 74 /* LispType.UnsignedShiftRightEquals */,
|
|
780
|
-
'<<=': 76 /* LispType.ShiftLeftEquals */,
|
|
781
|
-
'>>=': 75 /* LispType.ShiftRightEquals */,
|
|
782
|
-
};
|
|
783
|
-
setLispType(['assign', 'assignModify', 'boolOp'], (constants, type, part, res, expect, ctx) => {
|
|
784
|
-
ctx.lispTree = createLisp({
|
|
785
|
-
op: adderTypes[res[0]],
|
|
786
|
-
a: ctx.lispTree,
|
|
787
|
-
b: lispify(constants, part.substring(res[0].length), expectTypes[expect].next)
|
|
788
|
-
});
|
|
789
|
-
});
|
|
790
|
-
const opTypes = {
|
|
791
|
-
'&': 77 /* LispType.BitAnd */,
|
|
792
|
-
'|': 78 /* LispType.BitOr */,
|
|
793
|
-
'^': 79 /* LispType.BitNegate */,
|
|
794
|
-
'<<': 80 /* LispType.BitShiftLeft */,
|
|
795
|
-
'>>': 81 /* LispType.BitShiftRight */,
|
|
796
|
-
'>>>': 82 /* LispType.BitUnsignedShiftRight */,
|
|
797
|
-
'<=': 54 /* LispType.SmallerEqualThan */,
|
|
798
|
-
'>=': 55 /* LispType.LargerEqualThan */,
|
|
799
|
-
'<': 56 /* LispType.SmallerThan */,
|
|
800
|
-
'>': 57 /* LispType.LargerThan */,
|
|
801
|
-
'!==': 31 /* LispType.StrictNotEqual */,
|
|
802
|
-
'!=': 53 /* LispType.NotEqual */,
|
|
803
|
-
'===': 32 /* LispType.StrictEqual */,
|
|
804
|
-
'==': 52 /* LispType.Equal */,
|
|
805
|
-
'+': 33 /* LispType.Plus */,
|
|
806
|
-
'-': 47 /* LispType.Minus */,
|
|
807
|
-
'/': 48 /* LispType.Divide */,
|
|
808
|
-
'**': 49 /* LispType.Power */,
|
|
809
|
-
'*': 50 /* LispType.Multiply */,
|
|
810
|
-
'%': 51 /* LispType.Modulus */,
|
|
811
|
-
};
|
|
812
|
-
setLispType(['opHigh', 'op', 'comparitor', 'bitwise'], (constants, type, part, res, expect, ctx) => {
|
|
813
|
-
const next = [
|
|
814
|
-
expectTypes.inlineIf.types.inlineIf,
|
|
815
|
-
inlineIfElse
|
|
816
|
-
];
|
|
817
|
-
switch (type) {
|
|
818
|
-
case 'opHigh':
|
|
819
|
-
next.push(expectTypes.splitter.types.opHigh);
|
|
820
|
-
case 'op':
|
|
821
|
-
next.push(expectTypes.splitter.types.op);
|
|
822
|
-
case 'comparitor':
|
|
823
|
-
next.push(expectTypes.splitter.types.comparitor);
|
|
824
|
-
case 'bitwise':
|
|
825
|
-
next.push(expectTypes.splitter.types.bitwise);
|
|
826
|
-
next.push(expectTypes.splitter.types.boolOp);
|
|
827
|
-
}
|
|
828
|
-
let extract = restOfExp(constants, part.substring(res[0].length), next);
|
|
829
|
-
ctx.lispTree = lispify(constants, part.substring(extract.length + res[0].length), restOfExp.next, createLisp({
|
|
830
|
-
op: opTypes[res[0]],
|
|
831
|
-
a: ctx.lispTree,
|
|
832
|
-
b: lispify(constants, extract, expectTypes[expect].next),
|
|
833
|
-
}));
|
|
834
|
-
});
|
|
835
|
-
setLispType(['inlineIf'], (constants, type, part, res, expect, ctx) => {
|
|
836
|
-
let found = false;
|
|
837
|
-
let extract = part.substring(0, 0);
|
|
838
|
-
let quoteCount = 1;
|
|
839
|
-
while (!found && extract.length < part.length) {
|
|
840
|
-
extract.end = restOfExp(constants, part.substring(extract.length + 1), [
|
|
841
|
-
expectTypes.inlineIf.types.inlineIf,
|
|
842
|
-
inlineIfElse
|
|
843
|
-
]).end;
|
|
844
|
-
if (part.char(extract.length) === '?') {
|
|
845
|
-
quoteCount++;
|
|
846
|
-
}
|
|
847
|
-
else {
|
|
848
|
-
quoteCount--;
|
|
849
|
-
}
|
|
850
|
-
if (!quoteCount) {
|
|
851
|
-
found = true;
|
|
852
|
-
}
|
|
853
|
-
}
|
|
854
|
-
extract.start = part.start + 1;
|
|
855
|
-
ctx.lispTree = createLisp({
|
|
856
|
-
op: 15 /* LispType.InlineIf */,
|
|
857
|
-
a: ctx.lispTree,
|
|
858
|
-
b: createLisp({
|
|
859
|
-
op: 16 /* LispType.InlineIfCase */,
|
|
860
|
-
a: lispifyExpr(constants, extract),
|
|
861
|
-
b: lispifyExpr(constants, part.substring(res[0].length + extract.length + 1))
|
|
862
|
-
})
|
|
863
|
-
});
|
|
864
|
-
});
|
|
865
|
-
function extractIfElse(constants, part) {
|
|
866
|
-
let count = 0;
|
|
867
|
-
let found = part.substring(0, 0);
|
|
868
|
-
let foundElse = emptyString;
|
|
869
|
-
let foundTrue;
|
|
870
|
-
let first = true;
|
|
871
|
-
let elseReg;
|
|
872
|
-
let details = {};
|
|
873
|
-
while ((found = restOfExp(constants, part.substring(found.end - part.start), [elseIf, ifElse, semiColon], undefined, undefined, undefined, details)).length || first) {
|
|
874
|
-
first = false;
|
|
875
|
-
const f = part.substring(found.end - part.start).toString();
|
|
876
|
-
if (f.startsWith("if")) {
|
|
877
|
-
found.end++;
|
|
878
|
-
count++;
|
|
879
|
-
}
|
|
880
|
-
else if (f.startsWith('else')) {
|
|
881
|
-
foundTrue = part.substring(0, found.end - part.start);
|
|
882
|
-
found.end++;
|
|
883
|
-
count--;
|
|
884
|
-
if (!count) {
|
|
885
|
-
found.end--;
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
else if (elseReg = /^;?\s*else(?![\w\$])/.exec(f)) {
|
|
889
|
-
foundTrue = part.substring(0, found.end - part.start);
|
|
890
|
-
found.end += elseReg[0].length - 1;
|
|
891
|
-
count--;
|
|
892
|
-
if (!count) {
|
|
893
|
-
found.end -= elseReg[0].length - 1;
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
else {
|
|
897
|
-
foundTrue = foundElse.length ? foundTrue : part.substring(0, found.end - part.start);
|
|
898
|
-
break;
|
|
899
|
-
}
|
|
900
|
-
if (!count) {
|
|
901
|
-
let ie = extractIfElse(constants, part.substring(found.end - part.start + (/^;?\s*else(?![\w\$])/.exec(f)?.[0].length)));
|
|
902
|
-
foundElse = ie.all;
|
|
903
|
-
break;
|
|
904
|
-
}
|
|
905
|
-
details = {};
|
|
906
|
-
}
|
|
907
|
-
foundTrue = foundTrue || part.substring(0, found.end - part.start);
|
|
908
|
-
return { all: part.substring(0, Math.max(foundTrue.end, foundElse.end) - part.start), true: foundTrue, false: foundElse };
|
|
909
|
-
}
|
|
910
|
-
setLispType(['if'], (constants, type, part, res, expect, ctx) => {
|
|
911
|
-
let condition = restOfExp(constants, part.substring(res[0].length), [], "(");
|
|
912
|
-
const ie = extractIfElse(constants, part.substring(res[1].length));
|
|
913
|
-
/^\s*\{/.exec(part.substring(res[0].length + condition.length + 1).toString());
|
|
914
|
-
const startTrue = res[0].length - res[1].length + condition.length + 1;
|
|
915
|
-
let trueBlock = ie.true.substring(startTrue);
|
|
916
|
-
let elseBlock = ie.false;
|
|
917
|
-
condition = condition.trim();
|
|
918
|
-
trueBlock = trueBlock.trim();
|
|
919
|
-
elseBlock = elseBlock.trim();
|
|
920
|
-
if (trueBlock.char(0) === "{")
|
|
921
|
-
trueBlock = trueBlock.slice(1, -1);
|
|
922
|
-
if (elseBlock.char(0) === "{")
|
|
923
|
-
elseBlock = elseBlock.slice(1, -1);
|
|
924
|
-
ctx.lispTree = createLisp({
|
|
925
|
-
op: 13 /* LispType.If */,
|
|
926
|
-
a: lispifyExpr(constants, condition),
|
|
927
|
-
b: createLisp({
|
|
928
|
-
op: 14 /* LispType.IfCase */,
|
|
929
|
-
a: lispifyBlock(trueBlock, constants),
|
|
930
|
-
b: lispifyBlock(elseBlock, constants)
|
|
931
|
-
})
|
|
932
|
-
});
|
|
933
|
-
});
|
|
934
|
-
setLispType(['switch'], (constants, type, part, res, expect, ctx) => {
|
|
935
|
-
const test = restOfExp(constants, part.substring(res[0].length), [], "(");
|
|
936
|
-
let start = part.toString().indexOf("{", res[0].length + test.length + 1);
|
|
937
|
-
if (start === -1)
|
|
938
|
-
throw new SyntaxError("Invalid switch");
|
|
939
|
-
let statement = insertSemicolons(constants, restOfExp(constants, part.substring(start + 1), [], "{"));
|
|
940
|
-
let caseFound;
|
|
941
|
-
const caseTest = /^\s*(case\s|default)\s*/;
|
|
942
|
-
let cases = [];
|
|
943
|
-
let defaultFound = false;
|
|
944
|
-
while (caseFound = caseTest.exec(statement.toString())) {
|
|
945
|
-
if (caseFound[1] === 'default') {
|
|
946
|
-
if (defaultFound)
|
|
947
|
-
throw new SyntaxError("Only one default switch case allowed");
|
|
948
|
-
defaultFound = true;
|
|
949
|
-
}
|
|
950
|
-
let cond = restOfExp(constants, statement.substring(caseFound[0].length), [/^:/]);
|
|
951
|
-
let found = emptyString;
|
|
952
|
-
let i = start = caseFound[0].length + cond.length + 1;
|
|
953
|
-
let bracketFound = /^\s*\{/.exec(statement.substring(i).toString());
|
|
954
|
-
let exprs = [];
|
|
955
|
-
if (bracketFound) {
|
|
956
|
-
i += bracketFound[0].length;
|
|
957
|
-
found = restOfExp(constants, statement.substring(i), [], "{");
|
|
958
|
-
i += found.length + 1;
|
|
959
|
-
exprs = lispifyBlock(found, constants);
|
|
960
|
-
}
|
|
961
|
-
else {
|
|
962
|
-
let notEmpty = restOfExp(constants, statement.substring(i), [caseTest]);
|
|
963
|
-
if (!notEmpty.trim().length) {
|
|
964
|
-
exprs = [];
|
|
965
|
-
i += notEmpty.length;
|
|
966
|
-
}
|
|
967
|
-
else {
|
|
968
|
-
while ((found = restOfExp(constants, statement.substring(i), [semiColon])).length) {
|
|
969
|
-
i += found.length + (statement.char(i + found.length) === ';' ? 1 : 0);
|
|
970
|
-
if (caseTest.test(statement.substring(i).toString())) {
|
|
971
|
-
break;
|
|
972
|
-
}
|
|
973
|
-
}
|
|
974
|
-
exprs = lispifyBlock(statement.substring(start, found.end - statement.start), constants);
|
|
975
|
-
}
|
|
976
|
-
}
|
|
977
|
-
statement = statement.substring(i);
|
|
978
|
-
cases.push(createLisp({
|
|
979
|
-
op: 41 /* LispType.SwitchCase */,
|
|
980
|
-
a: caseFound[1] === "default" ? undefined : lispifyExpr(constants, cond),
|
|
981
|
-
b: exprs
|
|
982
|
-
}));
|
|
983
|
-
}
|
|
984
|
-
ctx.lispTree = createLisp({
|
|
985
|
-
op: 40 /* LispType.Switch */,
|
|
986
|
-
a: lispifyExpr(constants, test),
|
|
987
|
-
b: cases
|
|
988
|
-
});
|
|
989
|
-
});
|
|
990
|
-
setLispType(['dot', 'prop'], (constants, type, part, res, expect, ctx) => {
|
|
991
|
-
let prop = res[0];
|
|
992
|
-
let index = res[0].length;
|
|
993
|
-
let op = 'prop';
|
|
994
|
-
if (type === 'dot') {
|
|
995
|
-
if (res[1]) {
|
|
996
|
-
op = '?prop';
|
|
997
|
-
}
|
|
998
|
-
let matches = part.substring(res[0].length).toString().match(expectTypes.prop.types.prop);
|
|
999
|
-
if (matches && matches.length) {
|
|
1000
|
-
prop = matches[0];
|
|
1001
|
-
index = prop.length + res[0].length;
|
|
1002
|
-
}
|
|
1003
|
-
else {
|
|
1004
|
-
throw new SyntaxError('Hanging dot');
|
|
1005
|
-
}
|
|
1006
|
-
}
|
|
1007
|
-
ctx.lispTree = lispify(constants, part.substring(index), expectTypes[expect].next, createLisp({
|
|
1008
|
-
op: typesCreate[op],
|
|
1009
|
-
a: ctx.lispTree,
|
|
1010
|
-
b: prop
|
|
1011
|
-
}));
|
|
1012
|
-
});
|
|
1013
|
-
setLispType(['spreadArray', 'spreadObject'], (constants, type, part, res, expect, ctx) => {
|
|
1014
|
-
ctx.lispTree = createLisp({
|
|
1015
|
-
op: type === 'spreadArray' ? 18 /* LispType.SpreadArray */ : 17 /* LispType.SpreadObject */,
|
|
1016
|
-
a: 0 /* LispType.None */,
|
|
1017
|
-
b: lispify(constants, part.substring(res[0].length), expectTypes[expect].next)
|
|
1018
|
-
});
|
|
1019
|
-
});
|
|
1020
|
-
setLispType(['return', 'throw'], (constants, type, part, res, expect, ctx) => {
|
|
1021
|
-
ctx.lispTree = createLisp({
|
|
1022
|
-
op: type === 'return' ? 8 /* LispType.Return */ : 46 /* LispType.Throw */,
|
|
1023
|
-
a: 0 /* LispType.None */,
|
|
1024
|
-
b: lispifyExpr(constants, part.substring(res[0].length))
|
|
1025
|
-
});
|
|
1026
|
-
});
|
|
1027
|
-
setLispType(['number', 'boolean', 'null', 'und', 'NaN', 'Infinity'], (constants, type, part, res, expect, ctx) => {
|
|
1028
|
-
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next, createLisp({
|
|
1029
|
-
op: type === "number" ? res[10] ? 83 /* LispType.BigInt */ : 7 /* LispType.Number */ : 35 /* LispType.GlobalSymbol */,
|
|
1030
|
-
a: 0 /* LispType.None */,
|
|
1031
|
-
b: res[10] ? res[1] : res[0]
|
|
1032
|
-
}));
|
|
1033
|
-
});
|
|
1034
|
-
setLispType(['string', 'literal', 'regex'], (constants, type, part, res, expect, ctx) => {
|
|
1035
|
-
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next, createLisp({
|
|
1036
|
-
op: type === 'string' ? 2 /* LispType.StringIndex */ : type === 'literal' ? 84 /* LispType.LiteralIndex */ : 85 /* LispType.RegexIndex */,
|
|
1037
|
-
a: 0 /* LispType.None */,
|
|
1038
|
-
b: res[1],
|
|
1039
|
-
}));
|
|
1040
|
-
});
|
|
1041
|
-
setLispType(['initialize'], (constants, type, part, res, expect, ctx) => {
|
|
1042
|
-
const lt = res[1] === 'var' ? 34 /* LispType.Var */ : res[1] === 'let' ? 3 /* LispType.Let */ : 4 /* LispType.Const */;
|
|
1043
|
-
if (!res[3]) {
|
|
1044
|
-
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next, createLisp({
|
|
1045
|
-
op: lt,
|
|
1046
|
-
a: res[2],
|
|
1047
|
-
b: 0 /* LispType.None */
|
|
1048
|
-
}));
|
|
1049
|
-
}
|
|
1050
|
-
else {
|
|
1051
|
-
ctx.lispTree = createLisp({
|
|
1052
|
-
op: lt,
|
|
1053
|
-
a: res[2],
|
|
1054
|
-
b: lispify(constants, part.substring(res[0].length), expectTypes[expect].next)
|
|
1055
|
-
});
|
|
1056
|
-
}
|
|
1057
|
-
});
|
|
1058
|
-
setLispType(['function', 'inlineFunction', 'arrowFunction', 'arrowFunctionSingle'], (constants, type, part, res, expect, ctx) => {
|
|
1059
|
-
const isArrow = type !== 'function' && type !== 'inlineFunction';
|
|
1060
|
-
const isReturn = isArrow && !res[res.length - 1];
|
|
1061
|
-
const argPos = isArrow ? 2 : 3;
|
|
1062
|
-
const isAsync = res[1] ? 88 /* LispType.True */ : 0 /* LispType.None */;
|
|
1063
|
-
const args = res[argPos] ? res[argPos].replace(/\s+/g, "").split(/,/g) : [];
|
|
1064
|
-
if (!isArrow) {
|
|
1065
|
-
args.unshift((res[2] || "").trimStart());
|
|
1066
|
-
}
|
|
1067
|
-
let ended = false;
|
|
1068
|
-
args.forEach((arg) => {
|
|
1069
|
-
if (ended)
|
|
1070
|
-
throw new SyntaxError('Rest parameter must be last formal parameter');
|
|
1071
|
-
if (arg.startsWith('...'))
|
|
1072
|
-
ended = true;
|
|
1073
|
-
});
|
|
1074
|
-
args.unshift(isAsync);
|
|
1075
|
-
const f = restOfExp(constants, part.substring(res[0].length), !isReturn ? [/^}/] : [/^[,\)\}\]]/, semiColon]);
|
|
1076
|
-
const func = (isReturn ? 'return ' + f : f.toString());
|
|
1077
|
-
ctx.lispTree = lispify(constants, part.substring(res[0].length + func.length + 1), expectTypes[expect].next, createLisp({
|
|
1078
|
-
op: isArrow ? 11 /* LispType.ArrowFunction */ : type === 'function' ? 37 /* LispType.Function */ : 10 /* LispType.InlineFunction */,
|
|
1079
|
-
a: args,
|
|
1080
|
-
b: constants.eager ? lispifyFunction(new CodeString(func), constants) : func
|
|
1081
|
-
}));
|
|
1082
|
-
});
|
|
1083
|
-
const iteratorRegex = /^((let|var|const)\s+)?\s*([a-zA-Z\$_][a-zA-Z\d\$_]*)\s+(in|of)(?![\w\$])/;
|
|
1084
|
-
setLispType(['for', 'do', 'while'], (constants, type, part, res, expect, ctx) => {
|
|
1085
|
-
let i = 0;
|
|
1086
|
-
let startStep = 88 /* LispType.True */;
|
|
1087
|
-
let startInternal = [];
|
|
1088
|
-
let getIterator;
|
|
1089
|
-
let beforeStep = 0 /* LispType.None */;
|
|
1090
|
-
let checkFirst = 88 /* LispType.True */;
|
|
1091
|
-
let condition;
|
|
1092
|
-
let step = 88 /* LispType.True */;
|
|
1093
|
-
let body;
|
|
1094
|
-
switch (type) {
|
|
1095
|
-
case 'while':
|
|
1096
|
-
i = part.toString().indexOf("(") + 1;
|
|
1097
|
-
let extract = restOfExp(constants, part.substring(i), [], "(");
|
|
1098
|
-
condition = lispifyReturnExpr(constants, extract);
|
|
1099
|
-
body = restOfExp(constants, part.substring(i + extract.length + 1)).trim();
|
|
1100
|
-
if (body[0] === "{")
|
|
1101
|
-
body = body.slice(1, -1);
|
|
1102
|
-
break;
|
|
1103
|
-
case 'for':
|
|
1104
|
-
i = part.toString().indexOf("(") + 1;
|
|
1105
|
-
let args = [];
|
|
1106
|
-
let extract2 = emptyString;
|
|
1107
|
-
for (let k = 0; k < 3; k++) {
|
|
1108
|
-
extract2 = restOfExp(constants, part.substring(i), [/^[;\)]/]);
|
|
1109
|
-
args.push(extract2.trim());
|
|
1110
|
-
i += extract2.length + 1;
|
|
1111
|
-
if (part.char(i - 1) === ")")
|
|
1112
|
-
break;
|
|
1113
|
-
}
|
|
1114
|
-
let iterator;
|
|
1115
|
-
if (args.length === 1 && (iterator = iteratorRegex.exec(args[0].toString()))) {
|
|
1116
|
-
if (iterator[4] === 'of') {
|
|
1117
|
-
getIterator = lispifyReturnExpr(constants, args[0].substring(iterator[0].length)),
|
|
1118
|
-
startInternal = [
|
|
1119
|
-
ofStart2,
|
|
1120
|
-
ofStart3
|
|
1121
|
-
];
|
|
1122
|
-
condition = ofCondition;
|
|
1123
|
-
step = ofStep;
|
|
1124
|
-
beforeStep = lispify(constants, new CodeString((iterator[1] || 'let ') + iterator[3] + ' = $$next.value'), ['initialize']);
|
|
1125
|
-
}
|
|
1126
|
-
else {
|
|
1127
|
-
getIterator = lispifyReturnExpr(constants, args[0].substring(iterator[0].length)),
|
|
1128
|
-
startInternal = [
|
|
1129
|
-
inStart2,
|
|
1130
|
-
inStart3
|
|
1131
|
-
];
|
|
1132
|
-
step = inStep;
|
|
1133
|
-
condition = inCondition;
|
|
1134
|
-
beforeStep = lispify(constants, new CodeString((iterator[1] || 'let ') + iterator[3] + ' = $$keys[$$keyIndex]'), ['initialize']);
|
|
1135
|
-
}
|
|
1136
|
-
}
|
|
1137
|
-
else if (args.length === 3) {
|
|
1138
|
-
startStep = lispifyExpr(constants, args.shift(), startingExecpted);
|
|
1139
|
-
condition = lispifyReturnExpr(constants, args.shift());
|
|
1140
|
-
step = lispifyExpr(constants, args.shift());
|
|
1141
|
-
}
|
|
1142
|
-
else {
|
|
1143
|
-
throw new SyntaxError("Invalid for loop definition");
|
|
1144
|
-
}
|
|
1145
|
-
body = restOfExp(constants, part.substring(i)).trim();
|
|
1146
|
-
if (body[0] === "{")
|
|
1147
|
-
body = body.slice(1, -1);
|
|
1148
|
-
break;
|
|
1149
|
-
case 'do':
|
|
1150
|
-
checkFirst = 0 /* LispType.None */;
|
|
1151
|
-
const isBlock = !!res[3];
|
|
1152
|
-
body = restOfExp(constants, part.substring(res[0].length), isBlock ? [/^\}/] : [semiColon]);
|
|
1153
|
-
condition = lispifyReturnExpr(constants, restOfExp(constants, part.substring(part.toString().indexOf("(", res[0].length + body.length) + 1), [], "("));
|
|
1154
|
-
break;
|
|
1155
|
-
}
|
|
1156
|
-
const a = [checkFirst, startInternal, getIterator, startStep, step, condition, beforeStep];
|
|
1157
|
-
ctx.lispTree = createLisp({
|
|
1158
|
-
op: 38 /* LispType.Loop */,
|
|
1159
|
-
a,
|
|
1160
|
-
b: lispifyBlock(body, constants)
|
|
1161
|
-
});
|
|
1162
|
-
});
|
|
1163
|
-
setLispType(['block'], (constants, type, part, res, expect, ctx) => {
|
|
1164
|
-
ctx.lispTree = createLisp({
|
|
1165
|
-
op: 42 /* LispType.Block */,
|
|
1166
|
-
a: lispifyBlock(restOfExp(constants, part.substring(1), [], "{"), constants),
|
|
1167
|
-
b: 0 /* LispType.None */
|
|
1168
|
-
});
|
|
1169
|
-
});
|
|
1170
|
-
setLispType(['loopAction'], (constants, type, part, res, expect, ctx) => {
|
|
1171
|
-
ctx.lispTree = createLisp({
|
|
1172
|
-
op: 86 /* LispType.LoopAction */,
|
|
1173
|
-
a: res[1],
|
|
1174
|
-
b: 0 /* LispType.None */
|
|
1175
|
-
});
|
|
1176
|
-
});
|
|
1177
|
-
const catchReg = /^\s*(catch\s*(\(\s*([a-zA-Z\$_][a-zA-Z\d\$_]*)\s*\))?|finally)\s*\{/;
|
|
1178
|
-
setLispType(['try'], (constants, type, part, res, expect, ctx) => {
|
|
1179
|
-
const body = restOfExp(constants, part.substring(res[0].length), [], "{");
|
|
1180
|
-
let catchRes = catchReg.exec(part.substring(res[0].length + body.length + 1).toString());
|
|
1181
|
-
let finallyBody;
|
|
1182
|
-
let exception = "";
|
|
1183
|
-
let catchBody;
|
|
1184
|
-
let offset = 0;
|
|
1185
|
-
if (catchRes[1].startsWith('catch')) {
|
|
1186
|
-
catchRes = catchReg.exec(part.substring(res[0].length + body.length + 1).toString());
|
|
1187
|
-
exception = catchRes[2];
|
|
1188
|
-
catchBody = restOfExp(constants, part.substring(res[0].length + body.length + 1 + catchRes[0].length), [], "{");
|
|
1189
|
-
offset = res[0].length + body.length + 1 + catchRes[0].length + catchBody.length + 1;
|
|
1190
|
-
if ((catchRes = catchReg.exec(part.substring(offset).toString())) && catchRes[1].startsWith('finally')) {
|
|
1191
|
-
finallyBody = restOfExp(constants, part.substring(offset + catchRes[0].length), [], "{");
|
|
1192
|
-
}
|
|
1193
|
-
}
|
|
1194
|
-
else {
|
|
1195
|
-
finallyBody = restOfExp(constants, part.substring(res[0].length + body.length + 1 + catchRes[0].length), [], "{");
|
|
1196
|
-
}
|
|
1197
|
-
const b = [
|
|
1198
|
-
exception,
|
|
1199
|
-
lispifyBlock(insertSemicolons(constants, catchBody || emptyString), constants),
|
|
1200
|
-
lispifyBlock(insertSemicolons(constants, finallyBody || emptyString), constants),
|
|
1201
|
-
];
|
|
1202
|
-
ctx.lispTree = createLisp({
|
|
1203
|
-
op: 39 /* LispType.Try */,
|
|
1204
|
-
a: lispifyBlock(insertSemicolons(constants, body), constants),
|
|
1205
|
-
b
|
|
1206
|
-
});
|
|
1207
|
-
});
|
|
1208
|
-
setLispType(['void', 'await'], (constants, type, part, res, expect, ctx) => {
|
|
1209
|
-
const extract = restOfExp(constants, part.substring(res[0].length), [/^([^\s\.\?\w\$]|\?[^\.])/]);
|
|
1210
|
-
ctx.lispTree = lispify(constants, part.substring(res[0].length + extract.length), expectTypes[expect].next, createLisp({
|
|
1211
|
-
op: type === 'void' ? 87 /* LispType.Void */ : 44 /* LispType.Await */,
|
|
1212
|
-
a: lispify(constants, extract),
|
|
1213
|
-
b: 0 /* LispType.None */
|
|
1214
|
-
}));
|
|
1215
|
-
});
|
|
1216
|
-
setLispType(['new'], (constants, type, part, res, expect, ctx) => {
|
|
1217
|
-
let i = res[0].length;
|
|
1218
|
-
const obj = restOfExp(constants, part.substring(i), [], undefined, "(");
|
|
1219
|
-
i += obj.length + 1;
|
|
1220
|
-
const args = [];
|
|
1221
|
-
if (part.char(i - 1) === "(") {
|
|
1222
|
-
const argsString = restOfExp(constants, part.substring(i), [], "(");
|
|
1223
|
-
i += argsString.length + 1;
|
|
1224
|
-
let found;
|
|
1225
|
-
let j = 0;
|
|
1226
|
-
while ((found = restOfExp(constants, argsString.substring(j), [/^,/])).length) {
|
|
1227
|
-
j += found.length + 1;
|
|
1228
|
-
args.push(found.trim());
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
ctx.lispTree = lispify(constants, part.substring(i), expectTypes.expEdge.next, createLisp({
|
|
1232
|
-
op: 45 /* LispType.New */,
|
|
1233
|
-
a: lispify(constants, obj, expectTypes.initialize.next),
|
|
1234
|
-
b: args.map((arg) => lispify(constants, arg, expectTypes.initialize.next)),
|
|
1235
|
-
}));
|
|
1236
|
-
});
|
|
1237
|
-
const ofStart2 = lispify(undefined, new CodeString('let $$iterator = $$obj[Symbol.iterator]()'), ['initialize']);
|
|
1238
|
-
const ofStart3 = lispify(undefined, new CodeString('let $$next = $$iterator.next()'), ['initialize']);
|
|
1239
|
-
const ofCondition = lispify(undefined, new CodeString('return !$$next.done'), ['initialize']);
|
|
1240
|
-
const ofStep = lispify(undefined, new CodeString('$$next = $$iterator.next()'));
|
|
1241
|
-
const inStart2 = lispify(undefined, new CodeString('let $$keys = Object.keys($$obj)'), ['initialize']);
|
|
1242
|
-
const inStart3 = lispify(undefined, new CodeString('let $$keyIndex = 0'), ['initialize']);
|
|
1243
|
-
const inStep = lispify(undefined, new CodeString('$$keyIndex++'));
|
|
1244
|
-
const inCondition = lispify(undefined, new CodeString('return $$keyIndex < $$keys.length'), ['initialize']);
|
|
1245
|
-
var lastType;
|
|
1246
|
-
function lispify(constants, part, expected, lispTree, topLevel = false) {
|
|
1247
|
-
lispTree = lispTree || [0 /* LispType.None */, 0 /* LispType.None */, 0 /* LispType.None */];
|
|
1248
|
-
expected = expected || expectTypes.initialize.next;
|
|
1249
|
-
if (part === undefined)
|
|
1250
|
-
return lispTree;
|
|
1251
|
-
part = part.trimStart();
|
|
1252
|
-
const str = part.toString();
|
|
1253
|
-
if (!part.length && !expected.includes('expEnd')) {
|
|
1254
|
-
throw new SyntaxError("Unexpected end of expression");
|
|
1255
|
-
}
|
|
1256
|
-
if (!part.length)
|
|
1257
|
-
return lispTree;
|
|
1258
|
-
let ctx = { lispTree: lispTree };
|
|
1259
|
-
let res;
|
|
1260
|
-
for (let expect of expected) {
|
|
1261
|
-
if (expect === 'expEnd') {
|
|
1262
|
-
continue;
|
|
1263
|
-
}
|
|
1264
|
-
for (let type in expectTypes[expect].types) {
|
|
1265
|
-
if (type === 'expEnd') {
|
|
1266
|
-
continue;
|
|
1267
|
-
}
|
|
1268
|
-
if (res = expectTypes[expect].types[type].exec(str)) {
|
|
1269
|
-
lastType = type;
|
|
1270
|
-
try {
|
|
1271
|
-
lispTypes.get(type)(constants, type, part, res, expect, ctx);
|
|
1272
|
-
}
|
|
1273
|
-
catch (e) {
|
|
1274
|
-
if (topLevel && e instanceof SyntaxError) {
|
|
1275
|
-
throw new ParseError(e.message, str);
|
|
1276
|
-
}
|
|
1277
|
-
throw e;
|
|
1278
|
-
}
|
|
1279
|
-
break;
|
|
1280
|
-
}
|
|
1281
|
-
}
|
|
1282
|
-
if (res)
|
|
1283
|
-
break;
|
|
1284
|
-
}
|
|
1285
|
-
if (!res && part.length) {
|
|
1286
|
-
`Unexpected token after ${lastType}: ${part.char(0)}`;
|
|
1287
|
-
if (topLevel) {
|
|
1288
|
-
throw new ParseError(`Unexpected token after ${lastType}: ${part.char(0)}`, str);
|
|
1289
|
-
}
|
|
1290
|
-
throw new SyntaxError(`Unexpected token after ${lastType}: ${part.char(0)}`);
|
|
1291
|
-
}
|
|
1292
|
-
return ctx.lispTree;
|
|
1293
|
-
}
|
|
1294
|
-
const startingExpectedWithoutSingle = startingExecpted.filter((r) => r !== 'expSingle');
|
|
1295
|
-
function lispifyExpr(constants, str, expected) {
|
|
1296
|
-
if (!str.trimStart().length)
|
|
1297
|
-
return undefined;
|
|
1298
|
-
let subExpressions = [];
|
|
1299
|
-
let sub;
|
|
1300
|
-
let pos = 0;
|
|
1301
|
-
expected = expected || expectTypes.initialize.next;
|
|
1302
|
-
if (expected.includes('expSingle')) {
|
|
1303
|
-
if (testMultiple(str.toString(), Object.values(expectTypes.expSingle.types))) {
|
|
1304
|
-
return lispify(constants, str, ['expSingle'], undefined, true);
|
|
1305
|
-
}
|
|
1306
|
-
}
|
|
1307
|
-
if (expected === startingExecpted)
|
|
1308
|
-
expected = startingExpectedWithoutSingle;
|
|
1309
|
-
while ((sub = restOfExp(constants, str.substring(pos), [/^,/])).length) {
|
|
1310
|
-
subExpressions.push(sub.trimStart());
|
|
1311
|
-
pos += sub.length + 1;
|
|
1312
|
-
}
|
|
1313
|
-
if (subExpressions.length === 1) {
|
|
1314
|
-
return lispify(constants, str, expected, undefined, true);
|
|
1315
|
-
}
|
|
1316
|
-
if (expected.includes('initialize')) {
|
|
1317
|
-
let defined = expectTypes.initialize.types.initialize.exec(subExpressions[0].toString());
|
|
1318
|
-
if (defined) {
|
|
1319
|
-
return createLisp({
|
|
1320
|
-
op: 42 /* LispType.Block */,
|
|
1321
|
-
a: subExpressions.map((str, i) => lispify(constants, i ? new CodeString(defined[1] + ' ' + str) : str, ['initialize'], undefined, true)),
|
|
1322
|
-
b: 0 /* LispType.None */
|
|
1323
|
-
});
|
|
1324
|
-
}
|
|
1325
|
-
else if (expectTypes.initialize.types.return.exec(subExpressions[0].toString())) {
|
|
1326
|
-
return lispify(constants, str, expected, undefined, true);
|
|
1327
|
-
}
|
|
1328
|
-
}
|
|
1329
|
-
const exprs = subExpressions.map((str, i) => lispify(constants, str, expected, undefined, true));
|
|
1330
|
-
return createLisp({ op: 43 /* LispType.Expression */, a: exprs, b: 0 /* LispType.None */ });
|
|
1331
|
-
}
|
|
1332
|
-
function lispifyReturnExpr(constants, str) {
|
|
1333
|
-
return createLisp({ op: 8 /* LispType.Return */, a: 0 /* LispType.None */, b: lispifyExpr(constants, str) });
|
|
1334
|
-
}
|
|
1335
|
-
function lispifyBlock(str, constants, expression = false) {
|
|
1336
|
-
str = insertSemicolons(constants, str);
|
|
1337
|
-
if (!str.trim().length)
|
|
1338
|
-
return [];
|
|
1339
|
-
let parts = [];
|
|
1340
|
-
let part;
|
|
1341
|
-
let pos = 0;
|
|
1342
|
-
let start = 0;
|
|
1343
|
-
let details = {};
|
|
1344
|
-
let skipped = false;
|
|
1345
|
-
let isInserted = false;
|
|
1346
|
-
while ((part = restOfExp(constants, str.substring(pos), [semiColon], undefined, undefined, undefined, details)).length) {
|
|
1347
|
-
isInserted = str.char(pos + part.length) && str.char(pos + part.length) !== ';';
|
|
1348
|
-
pos += part.length + (isInserted ? 0 : 1);
|
|
1349
|
-
if (/^\s*else(?![\w\$])/.test(str.substring(pos).toString())) {
|
|
1350
|
-
skipped = true;
|
|
1351
|
-
}
|
|
1352
|
-
else if (details.words.includes('do') && /^\s*while(?![\w\$])/.test(str.substring(pos).toString())) {
|
|
1353
|
-
skipped = true;
|
|
1354
|
-
}
|
|
1355
|
-
else {
|
|
1356
|
-
skipped = false;
|
|
1357
|
-
parts.push(str.substring(start, pos - (isInserted ? 0 : 1)));
|
|
1358
|
-
start = pos;
|
|
1359
|
-
}
|
|
1360
|
-
details = {};
|
|
1361
|
-
if (expression)
|
|
1362
|
-
break;
|
|
1363
|
-
}
|
|
1364
|
-
if (skipped) {
|
|
1365
|
-
parts.push(str.substring(start, pos - (isInserted ? 0 : 1)));
|
|
1366
|
-
}
|
|
1367
|
-
return parts.map((str) => str.trimStart()).filter((str) => str.length).map((str, j) => {
|
|
1368
|
-
return lispifyExpr(constants, str.trimStart(), startingExecpted);
|
|
1369
|
-
});
|
|
1370
|
-
}
|
|
1371
|
-
function lispifyFunction(str, constants, expression = false) {
|
|
1372
|
-
if (!str.trim().length)
|
|
1373
|
-
return [];
|
|
1374
|
-
const tree = lispifyBlock(str, constants, expression);
|
|
1375
|
-
let hoisted = [];
|
|
1376
|
-
hoist(tree, hoisted);
|
|
1377
|
-
return hoisted.concat(tree);
|
|
1378
|
-
}
|
|
1379
|
-
function isLisp(item) {
|
|
1380
|
-
return Array.isArray(item) && typeof item[0] === 'number' && item[0] !== 0 /* LispType.None */ && item[0] !== 88 /* LispType.True */;
|
|
1381
|
-
}
|
|
1382
|
-
function hoist(item, res) {
|
|
1383
|
-
if (isLisp(item)) {
|
|
1384
|
-
const [op, a, b] = item;
|
|
1385
|
-
if (op === 39 /* LispType.Try */ || op === 13 /* LispType.If */ || op === 38 /* LispType.Loop */ || op === 40 /* LispType.Switch */) {
|
|
1386
|
-
hoist(a, res);
|
|
1387
|
-
hoist(b, res);
|
|
1388
|
-
}
|
|
1389
|
-
else if (op === 34 /* LispType.Var */) {
|
|
1390
|
-
res.push(createLisp({ op: 34 /* LispType.Var */, a: a, b: 0 /* LispType.None */ }));
|
|
1391
|
-
}
|
|
1392
|
-
else if (op === 37 /* LispType.Function */ && a[1]) {
|
|
1393
|
-
res.push(item);
|
|
1394
|
-
return true;
|
|
1395
|
-
}
|
|
1396
|
-
}
|
|
1397
|
-
else if (Array.isArray(item)) {
|
|
1398
|
-
const rep = [];
|
|
1399
|
-
for (let it of item) {
|
|
1400
|
-
if (!hoist(it, res)) {
|
|
1401
|
-
rep.push(it);
|
|
1402
|
-
}
|
|
1403
|
-
}
|
|
1404
|
-
if (rep.length !== item.length) {
|
|
1405
|
-
item.length = 0;
|
|
1406
|
-
item.push(...rep);
|
|
1407
|
-
}
|
|
1408
|
-
}
|
|
1409
|
-
return false;
|
|
1410
|
-
}
|
|
1411
|
-
const closingsNoInsertion = /^(\})\s*(catch|finally|else|while|instanceof)(?![\w\$])/;
|
|
1412
|
-
// \w|)|] \n \w = 2 // \} \w|\{ = 5
|
|
1413
|
-
const colonsRegex = /^((([\w\$\]\)\"\'\`]|\+\+|\-\-)\s*\r?\n\s*([\w\$\+\-\!~]))|(\}\s*[\w\$\!~\+\-\{\(\"\'\`]))/;
|
|
1414
|
-
// if () \w \n; \w == \w \n \w | last === if a
|
|
1415
|
-
// if () { }; \w == \} ^else | last === if b
|
|
1416
|
-
// if () \w \n; else \n \w \n; == \w \n \w | last === else a
|
|
1417
|
-
// if () {} else {}; \w == \} \w | last === else b
|
|
1418
|
-
// while () \n \w \n; \w == \w \n \w | last === while a
|
|
1419
|
-
// while () { }; \w == \} \w | last === while b
|
|
1420
|
-
// do \w \n; while (); \w == \w \n while | last === do a
|
|
1421
|
-
// do { } while (); \w == \) \w | last === while c
|
|
1422
|
-
// try {} catch () {}; \w == \} \w | last === catch|finally b
|
|
1423
|
-
// \w \n; \w == \w \n \w | last === none a
|
|
1424
|
-
// cb() \n \w == \) \n \w | last === none a
|
|
1425
|
-
// obj[a] \n \w == \] \n \w | last === none a
|
|
1426
|
-
// {} {} == \} \{ | last === none b
|
|
1427
|
-
function insertSemicolons(constants, str) {
|
|
1428
|
-
let rest = str;
|
|
1429
|
-
let sub = emptyString;
|
|
1430
|
-
let details = {};
|
|
1431
|
-
const inserted = insertedSemicolons.get(str.ref) || new Array(str.ref.str.length);
|
|
1432
|
-
while ((sub = restOfExp(constants, rest, [], undefined, undefined, [colonsRegex], details)).length) {
|
|
1433
|
-
let valid = false;
|
|
1434
|
-
let part = sub;
|
|
1435
|
-
let edge = sub.length;
|
|
1436
|
-
if (details.regRes) {
|
|
1437
|
-
valid = true;
|
|
1438
|
-
const [, , a, , , b] = details.regRes;
|
|
1439
|
-
edge = details.regRes[3] === "++" || details.regRes[3] === "--" ? sub.length + 1 : sub.length;
|
|
1440
|
-
part = rest.substring(0, edge);
|
|
1441
|
-
if (b) {
|
|
1442
|
-
let res = closingsNoInsertion.exec(rest.substring(sub.length - 1).toString());
|
|
1443
|
-
if (res) {
|
|
1444
|
-
if (res[2] === 'while') {
|
|
1445
|
-
valid = details.lastWord !== 'do';
|
|
1446
|
-
}
|
|
1447
|
-
else {
|
|
1448
|
-
valid = false;
|
|
1449
|
-
}
|
|
1450
|
-
}
|
|
1451
|
-
else if (details.lastWord === 'function' && details.regRes[5][0] === "}" && details.regRes[5].slice(-1) === '(') {
|
|
1452
|
-
valid = false;
|
|
1453
|
-
}
|
|
1454
|
-
}
|
|
1455
|
-
else if (a) {
|
|
1456
|
-
if (details.lastWord === 'if' || details.lastWord === 'while' || details.lastWord === 'for' || details.lastWord === 'else') {
|
|
1457
|
-
valid = false;
|
|
1458
|
-
}
|
|
1459
|
-
}
|
|
1460
|
-
}
|
|
1461
|
-
if (valid) {
|
|
1462
|
-
inserted[part.end] = true;
|
|
1463
|
-
}
|
|
1464
|
-
rest = rest.substring(edge);
|
|
1465
|
-
details = {};
|
|
1466
|
-
}
|
|
1467
|
-
insertedSemicolons.set(str.ref, inserted);
|
|
1468
|
-
return str;
|
|
1469
|
-
}
|
|
1470
|
-
function checkRegex(str) {
|
|
1471
|
-
let i = 1;
|
|
1472
|
-
let escape = false;
|
|
1473
|
-
let done = false;
|
|
1474
|
-
let cancel = false;
|
|
1475
|
-
while (i < str.length && !done && !cancel) {
|
|
1476
|
-
done = (str[i] === '/' && !escape);
|
|
1477
|
-
escape = str[i] === '\\' && !escape;
|
|
1478
|
-
cancel = str[i] === '\n';
|
|
1479
|
-
i++;
|
|
1480
|
-
}
|
|
1481
|
-
let after = str.substring(i);
|
|
1482
|
-
cancel = (cancel || !done) || /^\s*\d/.test(after);
|
|
1483
|
-
if (cancel)
|
|
1484
|
-
return null;
|
|
1485
|
-
let flags = /^[a-z]*/.exec(after);
|
|
1486
|
-
if (/^\s+[\w\$]/.test(str.substring(i + flags[0].length))) {
|
|
1487
|
-
return null;
|
|
1488
|
-
}
|
|
10
|
+
function createEvalContext() {
|
|
1489
11
|
return {
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
12
|
+
sandboxFunction,
|
|
13
|
+
sandboxedEval,
|
|
14
|
+
sandboxedSetTimeout,
|
|
15
|
+
sandboxedSetInterval,
|
|
16
|
+
lispifyFunction: parser.lispifyFunction,
|
|
1493
17
|
};
|
|
1494
18
|
}
|
|
1495
|
-
const notDivide = /(typeof|delete|instanceof|return|in|of|throw|new|void|do|if)$/;
|
|
1496
|
-
const possibleDivide = /^([\w\$\]\)]|\+\+|\-\-)[\s\/]/;
|
|
1497
|
-
function extractConstants(constants, str, currentEnclosure = "") {
|
|
1498
|
-
let quote;
|
|
1499
|
-
let extract = [];
|
|
1500
|
-
let escape = false;
|
|
1501
|
-
let regexFound;
|
|
1502
|
-
let comment = "";
|
|
1503
|
-
let commentStart = -1;
|
|
1504
|
-
let currJs = [];
|
|
1505
|
-
let char = "";
|
|
1506
|
-
const strRes = [];
|
|
1507
|
-
const enclosures = [];
|
|
1508
|
-
let isPossibleDivide;
|
|
1509
|
-
for (var i = 0; i < str.length; i++) {
|
|
1510
|
-
char = str[i];
|
|
1511
|
-
if (comment) {
|
|
1512
|
-
if (char === comment) {
|
|
1513
|
-
if (comment === "*" && str[i + 1] === "/") {
|
|
1514
|
-
comment = "";
|
|
1515
|
-
i++;
|
|
1516
|
-
}
|
|
1517
|
-
else if (comment === "\n") {
|
|
1518
|
-
comment = "";
|
|
1519
|
-
}
|
|
1520
|
-
}
|
|
1521
|
-
}
|
|
1522
|
-
else {
|
|
1523
|
-
if (escape) {
|
|
1524
|
-
escape = false;
|
|
1525
|
-
extract.push(char);
|
|
1526
|
-
continue;
|
|
1527
|
-
}
|
|
1528
|
-
if (quote) {
|
|
1529
|
-
if (quote === "`" && char === "$" && str[i + 1] === "{") {
|
|
1530
|
-
let skip = extractConstants(constants, str.substring(i + 2), "{");
|
|
1531
|
-
currJs.push(skip.str);
|
|
1532
|
-
extract.push('${', currJs.length - 1, `}`);
|
|
1533
|
-
i += skip.length + 2;
|
|
1534
|
-
}
|
|
1535
|
-
else if (quote === char) {
|
|
1536
|
-
if (quote === '`') {
|
|
1537
|
-
const li = createLisp({
|
|
1538
|
-
op: 36 /* LispType.Literal */,
|
|
1539
|
-
a: unraw(extract.join("")),
|
|
1540
|
-
b: [],
|
|
1541
|
-
});
|
|
1542
|
-
li.tempJsStrings = currJs;
|
|
1543
|
-
constants.literals.push(li);
|
|
1544
|
-
strRes.push(`\``, constants.literals.length - 1, `\``);
|
|
1545
|
-
}
|
|
1546
|
-
else {
|
|
1547
|
-
constants.strings.push(unraw(extract.join("")));
|
|
1548
|
-
strRes.push(`"`, constants.strings.length - 1, `"`);
|
|
1549
|
-
}
|
|
1550
|
-
quote = null;
|
|
1551
|
-
extract = [];
|
|
1552
|
-
}
|
|
1553
|
-
else {
|
|
1554
|
-
extract.push(char);
|
|
1555
|
-
}
|
|
1556
|
-
}
|
|
1557
|
-
else {
|
|
1558
|
-
if ((char === "'" || char === '"' || char === '`')) {
|
|
1559
|
-
currJs = [];
|
|
1560
|
-
quote = char;
|
|
1561
|
-
}
|
|
1562
|
-
else if (closings[currentEnclosure] === char && !enclosures.length) {
|
|
1563
|
-
return { str: strRes.join(""), length: i };
|
|
1564
|
-
}
|
|
1565
|
-
else if (closings[char]) {
|
|
1566
|
-
enclosures.push(char);
|
|
1567
|
-
strRes.push(char);
|
|
1568
|
-
}
|
|
1569
|
-
else if (closings[enclosures[enclosures.length - 1]] === char) {
|
|
1570
|
-
enclosures.pop();
|
|
1571
|
-
strRes.push(char);
|
|
1572
|
-
}
|
|
1573
|
-
else if (char === "/" && (str[i + 1] === "*" || str[i + 1] === "/")) {
|
|
1574
|
-
comment = str[i + 1] === "*" ? "*" : "\n";
|
|
1575
|
-
commentStart = i;
|
|
1576
|
-
}
|
|
1577
|
-
else if (char === '/' && !isPossibleDivide && (regexFound = checkRegex(str.substring(i)))) {
|
|
1578
|
-
constants.regexes.push(regexFound);
|
|
1579
|
-
strRes.push(`/`, constants.regexes.length - 1, `/r`);
|
|
1580
|
-
i += regexFound.length - 1;
|
|
1581
|
-
}
|
|
1582
|
-
else {
|
|
1583
|
-
strRes.push(char);
|
|
1584
|
-
}
|
|
1585
|
-
if (!isPossibleDivide || !space.test(char)) {
|
|
1586
|
-
if (isPossibleDivide = possibleDivide.exec(str.substring(i))) {
|
|
1587
|
-
if (notDivide.test(str.substring(0, i + isPossibleDivide[1].length))) {
|
|
1588
|
-
isPossibleDivide = null;
|
|
1589
|
-
}
|
|
1590
|
-
}
|
|
1591
|
-
}
|
|
1592
|
-
}
|
|
1593
|
-
escape = quote && char === "\\";
|
|
1594
|
-
}
|
|
1595
|
-
}
|
|
1596
|
-
if (comment) {
|
|
1597
|
-
if (comment === "*") {
|
|
1598
|
-
throw new SyntaxError(`Unclosed comment '/*': ${str.substring(commentStart)}`);
|
|
1599
|
-
}
|
|
1600
|
-
}
|
|
1601
|
-
return { str: strRes.join(""), length: i };
|
|
1602
|
-
}
|
|
1603
|
-
function parse(code, eager = false, expression = false) {
|
|
1604
|
-
if (typeof code !== 'string')
|
|
1605
|
-
throw new ParseError(`Cannot parse ${code}`, code);
|
|
1606
|
-
let str = ' ' + code;
|
|
1607
|
-
const constants = { strings: [], literals: [], regexes: [], eager };
|
|
1608
|
-
str = extractConstants(constants, str).str;
|
|
1609
|
-
for (let l of constants.literals) {
|
|
1610
|
-
l[2] = l.tempJsStrings.map((js) => lispifyExpr(constants, new CodeString(js)));
|
|
1611
|
-
delete l.tempJsStrings;
|
|
1612
|
-
}
|
|
1613
|
-
return { tree: lispifyFunction(new CodeString(str), constants, expression), constants };
|
|
1614
|
-
}
|
|
1615
|
-
|
|
1616
|
-
class ExecReturn {
|
|
1617
|
-
constructor(auditReport, result, returned, breakLoop = false, continueLoop = false) {
|
|
1618
|
-
this.auditReport = auditReport;
|
|
1619
|
-
this.result = result;
|
|
1620
|
-
this.returned = returned;
|
|
1621
|
-
this.breakLoop = breakLoop;
|
|
1622
|
-
this.continueLoop = continueLoop;
|
|
1623
|
-
}
|
|
1624
|
-
}
|
|
1625
|
-
class Prop {
|
|
1626
|
-
constructor(context, prop, isConst = false, isGlobal = false, isVariable = false) {
|
|
1627
|
-
this.context = context;
|
|
1628
|
-
this.prop = prop;
|
|
1629
|
-
this.isConst = isConst;
|
|
1630
|
-
this.isGlobal = isGlobal;
|
|
1631
|
-
this.isVariable = isVariable;
|
|
1632
|
-
}
|
|
1633
|
-
get(context) {
|
|
1634
|
-
if (this.context === undefined)
|
|
1635
|
-
throw new ReferenceError(`${this.prop} is not defined`);
|
|
1636
|
-
context.getSubscriptions.forEach((cb) => cb(this.context, this.prop));
|
|
1637
|
-
return this.context[this.prop];
|
|
1638
|
-
}
|
|
1639
|
-
}
|
|
1640
|
-
const optional = {};
|
|
1641
|
-
const reservedWords = new Set([
|
|
1642
|
-
'instanceof',
|
|
1643
|
-
'typeof',
|
|
1644
|
-
'return',
|
|
1645
|
-
'try',
|
|
1646
|
-
'catch',
|
|
1647
|
-
'if',
|
|
1648
|
-
'finally',
|
|
1649
|
-
'else',
|
|
1650
|
-
'in',
|
|
1651
|
-
'of',
|
|
1652
|
-
'var',
|
|
1653
|
-
'let',
|
|
1654
|
-
'const',
|
|
1655
|
-
'for',
|
|
1656
|
-
'delete',
|
|
1657
|
-
'false',
|
|
1658
|
-
'true',
|
|
1659
|
-
'while',
|
|
1660
|
-
'do',
|
|
1661
|
-
'break',
|
|
1662
|
-
'continue',
|
|
1663
|
-
'new',
|
|
1664
|
-
'function',
|
|
1665
|
-
'async',
|
|
1666
|
-
'await',
|
|
1667
|
-
'switch',
|
|
1668
|
-
'case'
|
|
1669
|
-
]);
|
|
1670
|
-
var VarType;
|
|
1671
|
-
(function (VarType) {
|
|
1672
|
-
VarType["let"] = "let";
|
|
1673
|
-
VarType["const"] = "const";
|
|
1674
|
-
VarType["var"] = "var";
|
|
1675
|
-
})(VarType || (VarType = {}));
|
|
1676
|
-
function keysOnly(obj) {
|
|
1677
|
-
const ret = Object.assign({}, obj);
|
|
1678
|
-
for (let key in ret) {
|
|
1679
|
-
ret[key] = true;
|
|
1680
|
-
}
|
|
1681
|
-
return ret;
|
|
1682
|
-
}
|
|
1683
|
-
class Scope {
|
|
1684
|
-
constructor(parent, vars = {}, functionThis) {
|
|
1685
|
-
this.const = {};
|
|
1686
|
-
this.let = {};
|
|
1687
|
-
this.var = {};
|
|
1688
|
-
const isFuncScope = functionThis !== undefined || parent === null;
|
|
1689
|
-
this.parent = parent;
|
|
1690
|
-
this.allVars = vars;
|
|
1691
|
-
this.let = isFuncScope ? this.let : keysOnly(vars);
|
|
1692
|
-
this.var = isFuncScope ? keysOnly(vars) : this.var;
|
|
1693
|
-
this.globals = parent === null ? keysOnly(vars) : {};
|
|
1694
|
-
this.functionThis = functionThis;
|
|
1695
|
-
}
|
|
1696
|
-
get(key, functionScope = false) {
|
|
1697
|
-
if (key === 'this' && this.functionThis !== undefined) {
|
|
1698
|
-
return new Prop({ this: this.functionThis }, key, true, false, true);
|
|
1699
|
-
}
|
|
1700
|
-
if (reservedWords.has(key))
|
|
1701
|
-
throw new SyntaxError("Unexepected token '" + key + "'");
|
|
1702
|
-
if (this.parent === null || !functionScope || this.functionThis !== undefined) {
|
|
1703
|
-
if (this.globals.hasOwnProperty(key)) {
|
|
1704
|
-
return new Prop(this.functionThis, key, false, true, true);
|
|
1705
|
-
}
|
|
1706
|
-
if (key in this.allVars && (!(key in {}) || this.allVars.hasOwnProperty(key))) {
|
|
1707
|
-
return new Prop(this.allVars, key, this.const.hasOwnProperty(key), this.globals.hasOwnProperty(key), true);
|
|
1708
|
-
}
|
|
1709
|
-
if (this.parent === null) {
|
|
1710
|
-
return new Prop(undefined, key);
|
|
1711
|
-
}
|
|
1712
|
-
}
|
|
1713
|
-
return this.parent.get(key, functionScope);
|
|
1714
|
-
}
|
|
1715
|
-
set(key, val) {
|
|
1716
|
-
if (key === 'this')
|
|
1717
|
-
throw new SyntaxError('"this" cannot be assigned');
|
|
1718
|
-
if (reservedWords.has(key))
|
|
1719
|
-
throw new SyntaxError("Unexepected token '" + key + "'");
|
|
1720
|
-
let prop = this.get(key);
|
|
1721
|
-
if (prop.context === undefined) {
|
|
1722
|
-
throw new ReferenceError(`Variable '${key}' was not declared.`);
|
|
1723
|
-
}
|
|
1724
|
-
if (prop.isConst) {
|
|
1725
|
-
throw new TypeError(`Cannot assign to const variable '${key}'`);
|
|
1726
|
-
}
|
|
1727
|
-
if (prop.isGlobal) {
|
|
1728
|
-
throw new SandboxError(`Cannot override global variable '${key}'`);
|
|
1729
|
-
}
|
|
1730
|
-
prop.context[prop.prop] = val;
|
|
1731
|
-
return prop;
|
|
1732
|
-
}
|
|
1733
|
-
declare(key, type = null, value = undefined, isGlobal = false) {
|
|
1734
|
-
if (key === 'this')
|
|
1735
|
-
throw new SyntaxError('"this" cannot be declared');
|
|
1736
|
-
if (reservedWords.has(key))
|
|
1737
|
-
throw new SyntaxError("Unexepected token '" + key + "'");
|
|
1738
|
-
if (type === 'var' && this.functionThis === undefined && this.parent !== null) {
|
|
1739
|
-
return this.parent.declare(key, type, value, isGlobal);
|
|
1740
|
-
}
|
|
1741
|
-
else if ((this[type].hasOwnProperty(key) && type !== 'const' && !this.globals.hasOwnProperty(key)) || !(key in this.allVars)) {
|
|
1742
|
-
if (isGlobal) {
|
|
1743
|
-
this.globals[key] = true;
|
|
1744
|
-
}
|
|
1745
|
-
this[type][key] = true;
|
|
1746
|
-
this.allVars[key] = value;
|
|
1747
|
-
}
|
|
1748
|
-
else {
|
|
1749
|
-
throw new SandboxError(`Identifier '${key}' has already been declared`);
|
|
1750
|
-
}
|
|
1751
|
-
return new Prop(this.allVars, key, this.const.hasOwnProperty(key), isGlobal);
|
|
1752
|
-
}
|
|
1753
|
-
}
|
|
1754
|
-
class FunctionScope {
|
|
1755
|
-
}
|
|
1756
|
-
class LocalScope {
|
|
1757
|
-
}
|
|
1758
|
-
class SandboxError extends Error {
|
|
1759
|
-
}
|
|
1760
|
-
let currentTicks;
|
|
1761
19
|
function sandboxFunction(context, ticks) {
|
|
1762
20
|
return SandboxFunction;
|
|
1763
21
|
function SandboxFunction(...params) {
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
return createFunction(params, parsed.tree, ticks || currentTicks, {
|
|
22
|
+
const code = params.pop() || '';
|
|
23
|
+
const parsed = parser.default(code);
|
|
24
|
+
return executor.createFunction(params, parsed.tree, ticks || executor.currentTicks.current, {
|
|
1767
25
|
...context,
|
|
1768
26
|
constants: parsed.constants,
|
|
1769
|
-
tree: parsed.tree
|
|
27
|
+
tree: parsed.tree,
|
|
1770
28
|
}, undefined, 'anonymous');
|
|
1771
29
|
}
|
|
1772
30
|
}
|
|
1773
|
-
function generateArgs(argNames, args) {
|
|
1774
|
-
const vars = {};
|
|
1775
|
-
argNames.forEach((arg, i) => {
|
|
1776
|
-
if (arg.startsWith('...')) {
|
|
1777
|
-
vars[arg.substring(3)] = args.slice(i);
|
|
1778
|
-
}
|
|
1779
|
-
else {
|
|
1780
|
-
vars[arg] = args[i];
|
|
1781
|
-
}
|
|
1782
|
-
});
|
|
1783
|
-
return vars;
|
|
1784
|
-
}
|
|
1785
|
-
const sandboxedFunctions = new WeakSet();
|
|
1786
|
-
function createFunction(argNames, parsed, ticks, context, scope, name) {
|
|
1787
|
-
if (context.ctx.options.forbidFunctionCreation) {
|
|
1788
|
-
throw new SandboxError("Function creation is forbidden");
|
|
1789
|
-
}
|
|
1790
|
-
let func;
|
|
1791
|
-
if (name === undefined) {
|
|
1792
|
-
func = (...args) => {
|
|
1793
|
-
const vars = generateArgs(argNames, args);
|
|
1794
|
-
const res = executeTree(ticks, context, parsed, scope === undefined ? [] : [new Scope(scope, vars)]);
|
|
1795
|
-
return res.result;
|
|
1796
|
-
};
|
|
1797
|
-
}
|
|
1798
|
-
else {
|
|
1799
|
-
func = function sandboxedObject(...args) {
|
|
1800
|
-
const vars = generateArgs(argNames, args);
|
|
1801
|
-
const res = executeTree(ticks, context, parsed, scope === undefined ? [] : [new Scope(scope, vars, this)]);
|
|
1802
|
-
return res.result;
|
|
1803
|
-
};
|
|
1804
|
-
}
|
|
1805
|
-
context.registerSandboxFunction(func);
|
|
1806
|
-
sandboxedFunctions.add(func);
|
|
1807
|
-
return func;
|
|
1808
|
-
}
|
|
1809
|
-
function createFunctionAsync(argNames, parsed, ticks, context, scope, name) {
|
|
1810
|
-
if (context.ctx.options.forbidFunctionCreation) {
|
|
1811
|
-
throw new SandboxError("Function creation is forbidden");
|
|
1812
|
-
}
|
|
1813
|
-
if (!context.ctx.prototypeWhitelist?.has(Promise.prototype)) {
|
|
1814
|
-
throw new SandboxError("Async/await not permitted");
|
|
1815
|
-
}
|
|
1816
|
-
let func;
|
|
1817
|
-
if (name === undefined) {
|
|
1818
|
-
func = async (...args) => {
|
|
1819
|
-
const vars = generateArgs(argNames, args);
|
|
1820
|
-
const res = await executeTreeAsync(ticks, context, parsed, scope === undefined ? [] : [new Scope(scope, vars)]);
|
|
1821
|
-
return res.result;
|
|
1822
|
-
};
|
|
1823
|
-
}
|
|
1824
|
-
else {
|
|
1825
|
-
func = async function sandboxedObject(...args) {
|
|
1826
|
-
const vars = generateArgs(argNames, args);
|
|
1827
|
-
const res = await executeTreeAsync(ticks, context, parsed, scope === undefined ? [] : [new Scope(scope, vars, this)]);
|
|
1828
|
-
return res.result;
|
|
1829
|
-
};
|
|
1830
|
-
}
|
|
1831
|
-
context.registerSandboxFunction(func);
|
|
1832
|
-
sandboxedFunctions.add(func);
|
|
1833
|
-
return func;
|
|
1834
|
-
}
|
|
1835
31
|
function sandboxedEval(func) {
|
|
1836
32
|
return sandboxEval;
|
|
1837
33
|
function sandboxEval(code) {
|
|
@@ -1852,1373 +48,63 @@ function sandboxedSetInterval(func) {
|
|
|
1852
48
|
return setInterval(func(handler), ...args);
|
|
1853
49
|
};
|
|
1854
50
|
}
|
|
1855
|
-
function assignCheck(obj, context, op = 'assign') {
|
|
1856
|
-
if (obj.context === undefined) {
|
|
1857
|
-
throw new ReferenceError(`Cannot ${op} value to undefined.`);
|
|
1858
|
-
}
|
|
1859
|
-
if (typeof obj.context !== 'object' && typeof obj.context !== 'function') {
|
|
1860
|
-
throw new SyntaxError(`Cannot ${op} value to a primitive.`);
|
|
1861
|
-
}
|
|
1862
|
-
if (obj.isConst) {
|
|
1863
|
-
throw new TypeError(`Cannot set value to const variable '${obj.prop}'`);
|
|
1864
|
-
}
|
|
1865
|
-
if (obj.isGlobal) {
|
|
1866
|
-
throw new SandboxError(`Cannot ${op} property '${obj.prop}' of a global object`);
|
|
1867
|
-
}
|
|
1868
|
-
if (typeof obj.context[obj.prop] === 'function' && !obj.context.hasOwnProperty(obj.prop)) {
|
|
1869
|
-
throw new SandboxError(`Override prototype property '${obj.prop}' not allowed`);
|
|
1870
|
-
}
|
|
1871
|
-
if (op === "delete") {
|
|
1872
|
-
if (obj.context.hasOwnProperty(obj.prop)) {
|
|
1873
|
-
context.changeSubscriptions.get(obj.context)?.forEach((cb) => cb({ type: "delete", prop: obj.prop }));
|
|
1874
|
-
context.changeSubscriptionsGlobal.get(obj.context)?.forEach((cb) => cb({ type: "delete", prop: obj.prop }));
|
|
1875
|
-
}
|
|
1876
|
-
}
|
|
1877
|
-
else if (obj.context.hasOwnProperty(obj.prop)) {
|
|
1878
|
-
context.setSubscriptions.get(obj.context)?.get(obj.prop)?.forEach((cb) => cb({
|
|
1879
|
-
type: "replace"
|
|
1880
|
-
}));
|
|
1881
|
-
context.setSubscriptionsGlobal.get(obj.context)?.get(obj.prop)?.forEach((cb) => cb({
|
|
1882
|
-
type: "replace"
|
|
1883
|
-
}));
|
|
1884
|
-
}
|
|
1885
|
-
else {
|
|
1886
|
-
context.changeSubscriptions.get(obj.context)?.forEach((cb) => cb({ type: "create", prop: obj.prop }));
|
|
1887
|
-
context.changeSubscriptionsGlobal.get(obj.context)?.forEach((cb) => cb({ type: "create", prop: obj.prop }));
|
|
1888
|
-
}
|
|
1889
|
-
}
|
|
1890
|
-
const arrayChange = new Set([
|
|
1891
|
-
[].push,
|
|
1892
|
-
[].pop,
|
|
1893
|
-
[].shift,
|
|
1894
|
-
[].unshift,
|
|
1895
|
-
[].splice,
|
|
1896
|
-
[].reverse,
|
|
1897
|
-
[].sort,
|
|
1898
|
-
[].copyWithin
|
|
1899
|
-
]);
|
|
1900
|
-
class KeyVal {
|
|
1901
|
-
constructor(key, val) {
|
|
1902
|
-
this.key = key;
|
|
1903
|
-
this.val = val;
|
|
1904
|
-
}
|
|
1905
|
-
}
|
|
1906
|
-
class SpreadObject {
|
|
1907
|
-
constructor(item) {
|
|
1908
|
-
this.item = item;
|
|
1909
|
-
}
|
|
1910
|
-
}
|
|
1911
|
-
class SpreadArray {
|
|
1912
|
-
constructor(item) {
|
|
1913
|
-
this.item = item;
|
|
1914
|
-
}
|
|
1915
|
-
}
|
|
1916
|
-
class If {
|
|
1917
|
-
constructor(t, f) {
|
|
1918
|
-
this.t = t;
|
|
1919
|
-
this.f = f;
|
|
1920
|
-
}
|
|
1921
|
-
}
|
|
1922
|
-
const literalRegex = /(\$\$)*(\$)?\${(\d+)}/g;
|
|
1923
|
-
const ops = new Map();
|
|
1924
|
-
function addOps(type, cb) {
|
|
1925
|
-
ops.set(type, cb);
|
|
1926
|
-
}
|
|
1927
|
-
addOps(1 /* LispType.Prop */, (exec, done, ticks, a, b, obj, context, scope) => {
|
|
1928
|
-
if (a === null) {
|
|
1929
|
-
throw new TypeError(`Cannot get property ${b} of null`);
|
|
1930
|
-
}
|
|
1931
|
-
const type = typeof a;
|
|
1932
|
-
if (type === 'undefined' && obj === undefined) {
|
|
1933
|
-
let prop = scope.get(b);
|
|
1934
|
-
if (prop.context === context.ctx.sandboxGlobal) {
|
|
1935
|
-
if (context.ctx.options.audit) {
|
|
1936
|
-
context.ctx.auditReport.globalsAccess.add(b);
|
|
1937
|
-
}
|
|
1938
|
-
const rep = context.ctx.globalsWhitelist.has(context.ctx.sandboxGlobal[b]) ? context.evals.get(context.ctx.sandboxGlobal[b]) : undefined;
|
|
1939
|
-
if (rep) {
|
|
1940
|
-
done(undefined, rep);
|
|
1941
|
-
return;
|
|
1942
|
-
}
|
|
1943
|
-
}
|
|
1944
|
-
if (prop.context && prop.context[b] === globalThis) {
|
|
1945
|
-
done(undefined, context.ctx.globalScope.get('this'));
|
|
1946
|
-
return;
|
|
1947
|
-
}
|
|
1948
|
-
done(undefined, prop);
|
|
1949
|
-
return;
|
|
1950
|
-
}
|
|
1951
|
-
else if (a === undefined) {
|
|
1952
|
-
throw new SandboxError("Cannot get property '" + b + "' of undefined");
|
|
1953
|
-
}
|
|
1954
|
-
if (type !== 'object') {
|
|
1955
|
-
if (type === 'number') {
|
|
1956
|
-
a = new Number(a);
|
|
1957
|
-
}
|
|
1958
|
-
else if (type === 'string') {
|
|
1959
|
-
a = new String(a);
|
|
1960
|
-
}
|
|
1961
|
-
else if (type === 'boolean') {
|
|
1962
|
-
a = new Boolean(a);
|
|
1963
|
-
}
|
|
1964
|
-
}
|
|
1965
|
-
else if (typeof a.hasOwnProperty === 'undefined') {
|
|
1966
|
-
done(undefined, new Prop(undefined, b));
|
|
1967
|
-
return;
|
|
1968
|
-
}
|
|
1969
|
-
const isFunction = type === 'function';
|
|
1970
|
-
let prototypeAccess = isFunction || !(a.hasOwnProperty(b) || typeof b === 'number');
|
|
1971
|
-
if (context.ctx.options.audit && prototypeAccess) {
|
|
1972
|
-
if (typeof b === 'string') {
|
|
1973
|
-
let prot = Object.getPrototypeOf(a);
|
|
1974
|
-
do {
|
|
1975
|
-
if (prot.hasOwnProperty(b)) {
|
|
1976
|
-
if (!context.ctx.auditReport.prototypeAccess[prot.constructor.name]) {
|
|
1977
|
-
context.ctx.auditReport.prototypeAccess[prot.constructor.name] = new Set();
|
|
1978
|
-
}
|
|
1979
|
-
context.ctx.auditReport.prototypeAccess[prot.constructor.name].add(b);
|
|
1980
|
-
}
|
|
1981
|
-
} while (prot = Object.getPrototypeOf(prot));
|
|
1982
|
-
}
|
|
1983
|
-
}
|
|
1984
|
-
if (prototypeAccess) {
|
|
1985
|
-
if (isFunction) {
|
|
1986
|
-
if (!['name', 'length', 'constructor'].includes(b) && a.hasOwnProperty(b)) {
|
|
1987
|
-
const whitelist = context.ctx.prototypeWhitelist.get(a.prototype);
|
|
1988
|
-
const replace = context.ctx.options.prototypeReplacements.get(a);
|
|
1989
|
-
if (replace) {
|
|
1990
|
-
done(undefined, new Prop(replace(a, true), b));
|
|
1991
|
-
return;
|
|
1992
|
-
}
|
|
1993
|
-
if (whitelist && (!whitelist.size || whitelist.has(b))) ;
|
|
1994
|
-
else {
|
|
1995
|
-
throw new SandboxError(`Static method or property access not permitted: ${a.name}.${b}`);
|
|
1996
|
-
}
|
|
1997
|
-
}
|
|
1998
|
-
}
|
|
1999
|
-
else if (b !== 'constructor') {
|
|
2000
|
-
let prot = a;
|
|
2001
|
-
while (prot = Object.getPrototypeOf(prot)) {
|
|
2002
|
-
if (prot.hasOwnProperty(b)) {
|
|
2003
|
-
const whitelist = context.ctx.prototypeWhitelist.get(prot);
|
|
2004
|
-
const replace = context.ctx.options.prototypeReplacements.get(prot.constuctor);
|
|
2005
|
-
if (replace) {
|
|
2006
|
-
done(undefined, new Prop(replace(a, false), b));
|
|
2007
|
-
return;
|
|
2008
|
-
}
|
|
2009
|
-
if (whitelist && (!whitelist.size || whitelist.has(b))) {
|
|
2010
|
-
break;
|
|
2011
|
-
}
|
|
2012
|
-
throw new SandboxError(`Method or property access not permitted: ${prot.constructor.name}.${b}`);
|
|
2013
|
-
}
|
|
2014
|
-
}
|
|
2015
|
-
}
|
|
2016
|
-
}
|
|
2017
|
-
if (context.evals.has(a[b])) {
|
|
2018
|
-
done(undefined, context.evals.get(a[b]));
|
|
2019
|
-
return;
|
|
2020
|
-
}
|
|
2021
|
-
if (a[b] === globalThis) {
|
|
2022
|
-
done(undefined, context.ctx.globalScope.get('this'));
|
|
2023
|
-
return;
|
|
2024
|
-
}
|
|
2025
|
-
let g = obj.isGlobal || (isFunction && !sandboxedFunctions.has(a)) || context.ctx.globalsWhitelist.has(a);
|
|
2026
|
-
done(undefined, new Prop(a, b, false, g));
|
|
2027
|
-
});
|
|
2028
|
-
addOps(5 /* LispType.Call */, (exec, done, ticks, a, b, obj, context, scope) => {
|
|
2029
|
-
if (context.ctx.options.forbidFunctionCalls)
|
|
2030
|
-
throw new SandboxError("Function invocations are not allowed");
|
|
2031
|
-
if (typeof a !== 'function') {
|
|
2032
|
-
throw new TypeError(`${typeof obj.prop === 'symbol' ? 'Symbol' : obj.prop} is not a function`);
|
|
2033
|
-
}
|
|
2034
|
-
const vals = b.map((item) => {
|
|
2035
|
-
if (item instanceof SpreadArray) {
|
|
2036
|
-
return [...item.item];
|
|
2037
|
-
}
|
|
2038
|
-
else {
|
|
2039
|
-
return [item];
|
|
2040
|
-
}
|
|
2041
|
-
}).flat().map((item) => valueOrProp(item, context));
|
|
2042
|
-
if (typeof obj === 'function') {
|
|
2043
|
-
done(undefined, obj(...vals));
|
|
2044
|
-
return;
|
|
2045
|
-
}
|
|
2046
|
-
if (obj.context[obj.prop] === JSON.stringify && context.getSubscriptions.size) {
|
|
2047
|
-
const cache = new Set();
|
|
2048
|
-
const recurse = (x) => {
|
|
2049
|
-
if (!x || !(typeof x === 'object') || cache.has(x))
|
|
2050
|
-
return;
|
|
2051
|
-
cache.add(x);
|
|
2052
|
-
for (let y in x) {
|
|
2053
|
-
context.getSubscriptions.forEach((cb) => cb(x, y));
|
|
2054
|
-
recurse(x[y]);
|
|
2055
|
-
}
|
|
2056
|
-
};
|
|
2057
|
-
recurse(vals[0]);
|
|
2058
|
-
}
|
|
2059
|
-
if (obj.context instanceof Array && arrayChange.has(obj.context[obj.prop]) && (context.changeSubscriptions.get(obj.context) || context.changeSubscriptionsGlobal.get(obj.context))) {
|
|
2060
|
-
let change;
|
|
2061
|
-
let changed = false;
|
|
2062
|
-
if (obj.prop === "push") {
|
|
2063
|
-
change = {
|
|
2064
|
-
type: "push",
|
|
2065
|
-
added: vals
|
|
2066
|
-
};
|
|
2067
|
-
changed = !!vals.length;
|
|
2068
|
-
}
|
|
2069
|
-
else if (obj.prop === "pop") {
|
|
2070
|
-
change = {
|
|
2071
|
-
type: "pop",
|
|
2072
|
-
removed: obj.context.slice(-1)
|
|
2073
|
-
};
|
|
2074
|
-
changed = !!change.removed.length;
|
|
2075
|
-
}
|
|
2076
|
-
else if (obj.prop === "shift") {
|
|
2077
|
-
change = {
|
|
2078
|
-
type: "shift",
|
|
2079
|
-
removed: obj.context.slice(0, 1)
|
|
2080
|
-
};
|
|
2081
|
-
changed = !!change.removed.length;
|
|
2082
|
-
}
|
|
2083
|
-
else if (obj.prop === "unshift") {
|
|
2084
|
-
change = {
|
|
2085
|
-
type: "unshift",
|
|
2086
|
-
added: vals
|
|
2087
|
-
};
|
|
2088
|
-
changed = !!vals.length;
|
|
2089
|
-
}
|
|
2090
|
-
else if (obj.prop === "splice") {
|
|
2091
|
-
change = {
|
|
2092
|
-
type: "splice",
|
|
2093
|
-
startIndex: vals[0],
|
|
2094
|
-
deleteCount: vals[1] === undefined ? obj.context.length : vals[1],
|
|
2095
|
-
added: vals.slice(2),
|
|
2096
|
-
removed: obj.context.slice(vals[0], vals[1] === undefined ? undefined : vals[0] + vals[1])
|
|
2097
|
-
};
|
|
2098
|
-
changed = !!change.added.length || !!change.removed.length;
|
|
2099
|
-
}
|
|
2100
|
-
else if (obj.prop === "reverse" || obj.prop === "sort") {
|
|
2101
|
-
change = { type: obj.prop };
|
|
2102
|
-
changed = !!obj.context.length;
|
|
2103
|
-
}
|
|
2104
|
-
else if (obj.prop === "copyWithin") {
|
|
2105
|
-
let len = vals[2] === undefined ? obj.context.length - vals[1] : Math.min(obj.context.length, vals[2] - vals[1]);
|
|
2106
|
-
change = {
|
|
2107
|
-
type: "copyWithin",
|
|
2108
|
-
startIndex: vals[0],
|
|
2109
|
-
endIndex: vals[0] + len,
|
|
2110
|
-
added: obj.context.slice(vals[1], vals[1] + len),
|
|
2111
|
-
removed: obj.context.slice(vals[0], vals[0] + len)
|
|
2112
|
-
};
|
|
2113
|
-
changed = !!change.added.length || !!change.removed.length;
|
|
2114
|
-
}
|
|
2115
|
-
if (changed) {
|
|
2116
|
-
context.changeSubscriptions.get(obj.context)?.forEach((cb) => cb(change));
|
|
2117
|
-
context.changeSubscriptionsGlobal.get(obj.context)?.forEach((cb) => cb(change));
|
|
2118
|
-
}
|
|
2119
|
-
}
|
|
2120
|
-
obj.get(context);
|
|
2121
|
-
done(undefined, obj.context[obj.prop](...vals));
|
|
2122
|
-
});
|
|
2123
|
-
addOps(22 /* LispType.CreateObject */, (exec, done, ticks, a, b, obj, context, scope) => {
|
|
2124
|
-
let res = {};
|
|
2125
|
-
for (let item of b) {
|
|
2126
|
-
if (item.key instanceof SpreadObject) {
|
|
2127
|
-
res = { ...res, ...item.key.item };
|
|
2128
|
-
}
|
|
2129
|
-
else {
|
|
2130
|
-
res[item.key] = item.val;
|
|
2131
|
-
}
|
|
2132
|
-
}
|
|
2133
|
-
done(undefined, res);
|
|
2134
|
-
});
|
|
2135
|
-
addOps(6 /* LispType.KeyVal */, (exec, done, ticks, a, b) => done(undefined, new KeyVal(a, b)));
|
|
2136
|
-
addOps(12 /* LispType.CreateArray */, (exec, done, ticks, a, b, obj, context, scope) => {
|
|
2137
|
-
const items = b.map((item) => {
|
|
2138
|
-
if (item instanceof SpreadArray) {
|
|
2139
|
-
return [...item.item];
|
|
2140
|
-
}
|
|
2141
|
-
else {
|
|
2142
|
-
return [item];
|
|
2143
|
-
}
|
|
2144
|
-
}).flat().map((item) => valueOrProp(item, context));
|
|
2145
|
-
done(undefined, items);
|
|
2146
|
-
});
|
|
2147
|
-
addOps(23 /* LispType.Group */, (exec, done, ticks, a, b) => done(undefined, b));
|
|
2148
|
-
addOps(35 /* LispType.GlobalSymbol */, (exec, done, ticks, a, b) => {
|
|
2149
|
-
switch (b) {
|
|
2150
|
-
case 'true': return done(undefined, true);
|
|
2151
|
-
case 'false': return done(undefined, false);
|
|
2152
|
-
case 'null': return done(undefined, null);
|
|
2153
|
-
case 'undefined': return done(undefined, undefined);
|
|
2154
|
-
case 'NaN': return done(undefined, NaN);
|
|
2155
|
-
case 'Infinity': return done(undefined, Infinity);
|
|
2156
|
-
}
|
|
2157
|
-
done(new Error('Unknown symbol: ' + b));
|
|
2158
|
-
});
|
|
2159
|
-
addOps(7 /* LispType.Number */, (exec, done, ticks, a, b) => done(undefined, Number(b)));
|
|
2160
|
-
addOps(83 /* LispType.BigInt */, (exec, done, ticks, a, b) => done(undefined, BigInt(b)));
|
|
2161
|
-
addOps(2 /* LispType.StringIndex */, (exec, done, ticks, a, b, obj, context) => done(undefined, context.constants.strings[parseInt(b)]));
|
|
2162
|
-
addOps(85 /* LispType.RegexIndex */, (exec, done, ticks, a, b, obj, context) => {
|
|
2163
|
-
const reg = context.constants.regexes[parseInt(b)];
|
|
2164
|
-
if (!context.ctx.globalsWhitelist.has(RegExp)) {
|
|
2165
|
-
throw new SandboxError("Regex not permitted");
|
|
2166
|
-
}
|
|
2167
|
-
else {
|
|
2168
|
-
done(undefined, new RegExp(reg.regex, reg.flags));
|
|
2169
|
-
}
|
|
2170
|
-
});
|
|
2171
|
-
addOps(84 /* LispType.LiteralIndex */, (exec, done, ticks, a, b, obj, context, scope) => {
|
|
2172
|
-
let item = context.constants.literals[parseInt(b)];
|
|
2173
|
-
const [, name, js] = item;
|
|
2174
|
-
let found = [];
|
|
2175
|
-
let f;
|
|
2176
|
-
let resnums = [];
|
|
2177
|
-
while (f = literalRegex.exec(name)) {
|
|
2178
|
-
if (!f[2]) {
|
|
2179
|
-
found.push(js[parseInt(f[3], 10)]);
|
|
2180
|
-
resnums.push(f[3]);
|
|
2181
|
-
}
|
|
2182
|
-
}
|
|
2183
|
-
exec(ticks, found, scope, context, (err, processed) => {
|
|
2184
|
-
const reses = {};
|
|
2185
|
-
if (err) {
|
|
2186
|
-
done(err);
|
|
2187
|
-
return;
|
|
2188
|
-
}
|
|
2189
|
-
for (let i in resnums) {
|
|
2190
|
-
const num = resnums[i];
|
|
2191
|
-
reses[num] = processed[i];
|
|
2192
|
-
}
|
|
2193
|
-
done(undefined, name.replace(/(\\\\)*(\\)?\${(\d+)}/g, (match, $$, $, num) => {
|
|
2194
|
-
if ($)
|
|
2195
|
-
return match;
|
|
2196
|
-
let res = reses[num];
|
|
2197
|
-
return ($$ ? $$ : '') + `${valueOrProp(res, context)}`;
|
|
2198
|
-
}));
|
|
2199
|
-
});
|
|
2200
|
-
});
|
|
2201
|
-
addOps(18 /* LispType.SpreadArray */, (exec, done, ticks, a, b, obj, context, scope) => {
|
|
2202
|
-
done(undefined, new SpreadArray(b));
|
|
2203
|
-
});
|
|
2204
|
-
addOps(17 /* LispType.SpreadObject */, (exec, done, ticks, a, b, obj, context, scope) => {
|
|
2205
|
-
done(undefined, new SpreadObject(b));
|
|
2206
|
-
});
|
|
2207
|
-
addOps(24 /* LispType.Not */, (exec, done, ticks, a, b) => done(undefined, !b));
|
|
2208
|
-
addOps(64 /* LispType.Inverse */, (exec, done, ticks, a, b) => done(undefined, ~b));
|
|
2209
|
-
addOps(25 /* LispType.IncrementBefore */, (exec, done, ticks, a, b, obj, context) => {
|
|
2210
|
-
assignCheck(obj, context);
|
|
2211
|
-
done(undefined, ++obj.context[obj.prop]);
|
|
2212
|
-
});
|
|
2213
|
-
addOps(26 /* LispType.IncrementAfter */, (exec, done, ticks, a, b, obj, context) => {
|
|
2214
|
-
assignCheck(obj, context);
|
|
2215
|
-
done(undefined, obj.context[obj.prop]++);
|
|
2216
|
-
});
|
|
2217
|
-
addOps(27 /* LispType.DecrementBefore */, (exec, done, ticks, a, b, obj, context) => {
|
|
2218
|
-
assignCheck(obj, context);
|
|
2219
|
-
done(undefined, --obj.context[obj.prop]);
|
|
2220
|
-
});
|
|
2221
|
-
addOps(28 /* LispType.DecrementAfter */, (exec, done, ticks, a, b, obj, context) => {
|
|
2222
|
-
assignCheck(obj, context);
|
|
2223
|
-
done(undefined, obj.context[obj.prop]--);
|
|
2224
|
-
});
|
|
2225
|
-
addOps(9 /* LispType.Assign */, (exec, done, ticks, a, b, obj, context) => {
|
|
2226
|
-
assignCheck(obj, context);
|
|
2227
|
-
done(undefined, obj.context[obj.prop] = b);
|
|
2228
|
-
});
|
|
2229
|
-
addOps(66 /* LispType.AddEquals */, (exec, done, ticks, a, b, obj, context) => {
|
|
2230
|
-
assignCheck(obj, context);
|
|
2231
|
-
done(undefined, obj.context[obj.prop] += b);
|
|
2232
|
-
});
|
|
2233
|
-
addOps(65 /* LispType.SubractEquals */, (exec, done, ticks, a, b, obj, context) => {
|
|
2234
|
-
assignCheck(obj, context);
|
|
2235
|
-
done(undefined, obj.context[obj.prop] -= b);
|
|
2236
|
-
});
|
|
2237
|
-
addOps(67 /* LispType.DivideEquals */, (exec, done, ticks, a, b, obj, context) => {
|
|
2238
|
-
assignCheck(obj, context);
|
|
2239
|
-
done(undefined, obj.context[obj.prop] /= b);
|
|
2240
|
-
});
|
|
2241
|
-
addOps(69 /* LispType.MultiplyEquals */, (exec, done, ticks, a, b, obj, context) => {
|
|
2242
|
-
assignCheck(obj, context);
|
|
2243
|
-
done(undefined, obj.context[obj.prop] *= b);
|
|
2244
|
-
});
|
|
2245
|
-
addOps(68 /* LispType.PowerEquals */, (exec, done, ticks, a, b, obj, context) => {
|
|
2246
|
-
assignCheck(obj, context);
|
|
2247
|
-
done(undefined, obj.context[obj.prop] **= b);
|
|
2248
|
-
});
|
|
2249
|
-
addOps(70 /* LispType.ModulusEquals */, (exec, done, ticks, a, b, obj, context) => {
|
|
2250
|
-
assignCheck(obj, context);
|
|
2251
|
-
done(undefined, obj.context[obj.prop] %= b);
|
|
2252
|
-
});
|
|
2253
|
-
addOps(71 /* LispType.BitNegateEquals */, (exec, done, ticks, a, b, obj, context) => {
|
|
2254
|
-
assignCheck(obj, context);
|
|
2255
|
-
done(undefined, obj.context[obj.prop] ^= b);
|
|
2256
|
-
});
|
|
2257
|
-
addOps(72 /* LispType.BitAndEquals */, (exec, done, ticks, a, b, obj, context) => {
|
|
2258
|
-
assignCheck(obj, context);
|
|
2259
|
-
done(undefined, obj.context[obj.prop] &= b);
|
|
2260
|
-
});
|
|
2261
|
-
addOps(73 /* LispType.BitOrEquals */, (exec, done, ticks, a, b, obj, context) => {
|
|
2262
|
-
assignCheck(obj, context);
|
|
2263
|
-
done(undefined, obj.context[obj.prop] |= b);
|
|
2264
|
-
});
|
|
2265
|
-
addOps(76 /* LispType.ShiftLeftEquals */, (exec, done, ticks, a, b, obj, context) => {
|
|
2266
|
-
assignCheck(obj, context);
|
|
2267
|
-
done(undefined, obj.context[obj.prop] <<= b);
|
|
2268
|
-
});
|
|
2269
|
-
addOps(75 /* LispType.ShiftRightEquals */, (exec, done, ticks, a, b, obj, context) => {
|
|
2270
|
-
assignCheck(obj, context);
|
|
2271
|
-
done(undefined, obj.context[obj.prop] >>= b);
|
|
2272
|
-
});
|
|
2273
|
-
addOps(74 /* LispType.UnsignedShiftRightEquals */, (exec, done, ticks, a, b, obj, context) => {
|
|
2274
|
-
assignCheck(obj, context);
|
|
2275
|
-
done(undefined, obj.context[obj.prop] >>= b);
|
|
2276
|
-
});
|
|
2277
|
-
addOps(57 /* LispType.LargerThan */, (exec, done, ticks, a, b) => done(undefined, a > b));
|
|
2278
|
-
addOps(56 /* LispType.SmallerThan */, (exec, done, ticks, a, b) => done(undefined, a < b));
|
|
2279
|
-
addOps(55 /* LispType.LargerEqualThan */, (exec, done, ticks, a, b) => done(undefined, a >= b));
|
|
2280
|
-
addOps(54 /* LispType.SmallerEqualThan */, (exec, done, ticks, a, b) => done(undefined, a <= b));
|
|
2281
|
-
addOps(52 /* LispType.Equal */, (exec, done, ticks, a, b) => done(undefined, a == b));
|
|
2282
|
-
addOps(32 /* LispType.StrictEqual */, (exec, done, ticks, a, b) => done(undefined, a === b));
|
|
2283
|
-
addOps(53 /* LispType.NotEqual */, (exec, done, ticks, a, b) => done(undefined, a != b));
|
|
2284
|
-
addOps(31 /* LispType.StrictNotEqual */, (exec, done, ticks, a, b) => done(undefined, a !== b));
|
|
2285
|
-
addOps(29 /* LispType.And */, (exec, done, ticks, a, b) => done(undefined, a && b));
|
|
2286
|
-
addOps(30 /* LispType.Or */, (exec, done, ticks, a, b) => done(undefined, a || b));
|
|
2287
|
-
addOps(77 /* LispType.BitAnd */, (exec, done, ticks, a, b) => done(undefined, a & b));
|
|
2288
|
-
addOps(78 /* LispType.BitOr */, (exec, done, ticks, a, b) => done(undefined, a | b));
|
|
2289
|
-
addOps(33 /* LispType.Plus */, (exec, done, ticks, a, b) => done(undefined, a + b));
|
|
2290
|
-
addOps(47 /* LispType.Minus */, (exec, done, ticks, a, b) => done(undefined, a - b));
|
|
2291
|
-
addOps(59 /* LispType.Positive */, (exec, done, ticks, a, b) => done(undefined, +b));
|
|
2292
|
-
addOps(58 /* LispType.Negative */, (exec, done, ticks, a, b) => done(undefined, -b));
|
|
2293
|
-
addOps(48 /* LispType.Divide */, (exec, done, ticks, a, b) => done(undefined, a / b));
|
|
2294
|
-
addOps(79 /* LispType.BitNegate */, (exec, done, ticks, a, b) => done(undefined, a ^ b));
|
|
2295
|
-
addOps(50 /* LispType.Multiply */, (exec, done, ticks, a, b) => done(undefined, a * b));
|
|
2296
|
-
addOps(51 /* LispType.Modulus */, (exec, done, ticks, a, b) => done(undefined, a % b));
|
|
2297
|
-
addOps(80 /* LispType.BitShiftLeft */, (exec, done, ticks, a, b) => done(undefined, a << b));
|
|
2298
|
-
addOps(81 /* LispType.BitShiftRight */, (exec, done, ticks, a, b) => done(undefined, a >> b));
|
|
2299
|
-
addOps(82 /* LispType.BitUnsignedShiftRight */, (exec, done, ticks, a, b) => done(undefined, a >>> b));
|
|
2300
|
-
addOps(60 /* LispType.Typeof */, (exec, done, ticks, a, b, obj, context, scope) => {
|
|
2301
|
-
exec(ticks, b, scope, context, (e, prop) => {
|
|
2302
|
-
done(undefined, typeof valueOrProp(prop, context));
|
|
2303
|
-
});
|
|
2304
|
-
});
|
|
2305
|
-
addOps(62 /* LispType.Instanceof */, (exec, done, ticks, a, b) => done(undefined, a instanceof b));
|
|
2306
|
-
addOps(63 /* LispType.In */, (exec, done, ticks, a, b) => done(undefined, a in b));
|
|
2307
|
-
addOps(61 /* LispType.Delete */, (exec, done, ticks, a, b, obj, context, scope, bobj) => {
|
|
2308
|
-
if (bobj.context === undefined) {
|
|
2309
|
-
done(undefined, true);
|
|
2310
|
-
return;
|
|
2311
|
-
}
|
|
2312
|
-
assignCheck(bobj, context, 'delete');
|
|
2313
|
-
if (bobj.isVariable) {
|
|
2314
|
-
done(undefined, false);
|
|
2315
|
-
return;
|
|
2316
|
-
}
|
|
2317
|
-
done(undefined, delete bobj.context[bobj.prop]);
|
|
2318
|
-
});
|
|
2319
|
-
addOps(8 /* LispType.Return */, (exec, done, ticks, a, b, obj, context) => done(undefined, b));
|
|
2320
|
-
addOps(34 /* LispType.Var */, (exec, done, ticks, a, b, obj, context, scope, bobj) => {
|
|
2321
|
-
done(undefined, scope.declare(a, VarType.var, b));
|
|
2322
|
-
});
|
|
2323
|
-
addOps(3 /* LispType.Let */, (exec, done, ticks, a, b, obj, context, scope, bobj) => {
|
|
2324
|
-
done(undefined, scope.declare(a, VarType.let, b, bobj && bobj.isGlobal));
|
|
2325
|
-
});
|
|
2326
|
-
addOps(4 /* LispType.Const */, (exec, done, ticks, a, b, obj, context, scope, bobj) => {
|
|
2327
|
-
done(undefined, scope.declare(a, VarType.const, b));
|
|
2328
|
-
});
|
|
2329
|
-
addOps(11 /* LispType.ArrowFunction */, (exec, done, ticks, a, b, obj, context, scope) => {
|
|
2330
|
-
a = [...a];
|
|
2331
|
-
if (typeof obj[2] === "string" || obj[2] instanceof CodeString) {
|
|
2332
|
-
obj[2] = b = lispifyFunction(new CodeString(obj[2]), context.constants);
|
|
2333
|
-
}
|
|
2334
|
-
if (a.shift()) {
|
|
2335
|
-
done(undefined, createFunctionAsync(a, b, ticks, context, scope));
|
|
2336
|
-
}
|
|
2337
|
-
else {
|
|
2338
|
-
done(undefined, createFunction(a, b, ticks, context, scope));
|
|
2339
|
-
}
|
|
2340
|
-
});
|
|
2341
|
-
addOps(37 /* LispType.Function */, (exec, done, ticks, a, b, obj, context, scope) => {
|
|
2342
|
-
if (typeof obj[2] === "string" || obj[2] instanceof CodeString) {
|
|
2343
|
-
obj[2] = b = lispifyFunction(new CodeString(obj[2]), context.constants);
|
|
2344
|
-
}
|
|
2345
|
-
let isAsync = a.shift();
|
|
2346
|
-
let name = a.shift();
|
|
2347
|
-
let func;
|
|
2348
|
-
if (isAsync === 88 /* LispType.True */) {
|
|
2349
|
-
func = createFunctionAsync(a, b, ticks, context, scope, name);
|
|
2350
|
-
}
|
|
2351
|
-
else {
|
|
2352
|
-
func = createFunction(a, b, ticks, context, scope, name);
|
|
2353
|
-
}
|
|
2354
|
-
if (name) {
|
|
2355
|
-
scope.declare(name, VarType.var, func);
|
|
2356
|
-
}
|
|
2357
|
-
done(undefined, func);
|
|
2358
|
-
});
|
|
2359
|
-
addOps(10 /* LispType.InlineFunction */, (exec, done, ticks, a, b, obj, context, scope) => {
|
|
2360
|
-
if (typeof obj[2] === "string" || obj[2] instanceof CodeString) {
|
|
2361
|
-
obj[2] = b = lispifyFunction(new CodeString(obj[2]), context.constants);
|
|
2362
|
-
}
|
|
2363
|
-
let isAsync = a.shift();
|
|
2364
|
-
let name = a.shift();
|
|
2365
|
-
if (name) {
|
|
2366
|
-
scope = new Scope(scope, {});
|
|
2367
|
-
}
|
|
2368
|
-
let func;
|
|
2369
|
-
if (isAsync === 88 /* LispType.True */) {
|
|
2370
|
-
func = createFunctionAsync(a, b, ticks, context, scope, name);
|
|
2371
|
-
}
|
|
2372
|
-
else {
|
|
2373
|
-
func = createFunction(a, b, ticks, context, scope, name);
|
|
2374
|
-
}
|
|
2375
|
-
if (name) {
|
|
2376
|
-
scope.declare(name, VarType.let, func);
|
|
2377
|
-
}
|
|
2378
|
-
done(undefined, func);
|
|
2379
|
-
});
|
|
2380
|
-
addOps(38 /* LispType.Loop */, (exec, done, ticks, a, b, obj, context, scope) => {
|
|
2381
|
-
const [checkFirst, startInternal, getIterator, startStep, step, condition, beforeStep] = a;
|
|
2382
|
-
let loop = true;
|
|
2383
|
-
const loopScope = new Scope(scope, {});
|
|
2384
|
-
let internalVars = {
|
|
2385
|
-
'$$obj': undefined
|
|
2386
|
-
};
|
|
2387
|
-
const interalScope = new Scope(loopScope, internalVars);
|
|
2388
|
-
if (exec === execAsync) {
|
|
2389
|
-
(async () => {
|
|
2390
|
-
let ad;
|
|
2391
|
-
ad = asyncDone((d) => exec(ticks, startStep, loopScope, context, d));
|
|
2392
|
-
internalVars['$$obj'] = (ad = asyncDone((d) => exec(ticks, getIterator, loopScope, context, d))).isInstant === true ? ad.instant : (await ad.p).result;
|
|
2393
|
-
ad = asyncDone((d) => exec(ticks, startInternal, interalScope, context, d));
|
|
2394
|
-
if (checkFirst)
|
|
2395
|
-
loop = (ad = asyncDone((d) => exec(ticks, condition, interalScope, context, d))).isInstant === true ? ad.instant : (await ad.p).result;
|
|
2396
|
-
while (loop) {
|
|
2397
|
-
let innerLoopVars = {};
|
|
2398
|
-
ad = asyncDone((d) => exec(ticks, beforeStep, new Scope(interalScope, innerLoopVars), context, d));
|
|
2399
|
-
ad.isInstant === true ? ad.instant : (await ad.p).result;
|
|
2400
|
-
let res = await executeTreeAsync(ticks, context, b, [new Scope(loopScope, innerLoopVars)], "loop");
|
|
2401
|
-
if (res instanceof ExecReturn && res.returned) {
|
|
2402
|
-
done(undefined, res);
|
|
2403
|
-
return;
|
|
2404
|
-
}
|
|
2405
|
-
if (res instanceof ExecReturn && res.breakLoop) {
|
|
2406
|
-
break;
|
|
2407
|
-
}
|
|
2408
|
-
ad = asyncDone((d) => exec(ticks, step, interalScope, context, d));
|
|
2409
|
-
loop = (ad = asyncDone((d) => exec(ticks, condition, interalScope, context, d))).isInstant === true ? ad.instant : (await ad.p).result;
|
|
2410
|
-
}
|
|
2411
|
-
done();
|
|
2412
|
-
})().catch(done);
|
|
2413
|
-
}
|
|
2414
|
-
else {
|
|
2415
|
-
syncDone((d) => exec(ticks, startStep, loopScope, context, d));
|
|
2416
|
-
internalVars['$$obj'] = syncDone((d) => exec(ticks, getIterator, loopScope, context, d)).result;
|
|
2417
|
-
syncDone((d) => exec(ticks, startInternal, interalScope, context, d));
|
|
2418
|
-
if (checkFirst)
|
|
2419
|
-
loop = (syncDone((d) => exec(ticks, condition, interalScope, context, d))).result;
|
|
2420
|
-
while (loop) {
|
|
2421
|
-
let innerLoopVars = {};
|
|
2422
|
-
syncDone((d) => exec(ticks, beforeStep, new Scope(interalScope, innerLoopVars), context, d));
|
|
2423
|
-
let res = executeTree(ticks, context, b, [new Scope(loopScope, innerLoopVars)], "loop");
|
|
2424
|
-
if (res instanceof ExecReturn && res.returned) {
|
|
2425
|
-
done(undefined, res);
|
|
2426
|
-
return;
|
|
2427
|
-
}
|
|
2428
|
-
if (res instanceof ExecReturn && res.breakLoop) {
|
|
2429
|
-
break;
|
|
2430
|
-
}
|
|
2431
|
-
syncDone((d) => exec(ticks, step, interalScope, context, d));
|
|
2432
|
-
loop = (syncDone((d) => exec(ticks, condition, interalScope, context, d))).result;
|
|
2433
|
-
}
|
|
2434
|
-
done();
|
|
2435
|
-
}
|
|
2436
|
-
});
|
|
2437
|
-
addOps(86 /* LispType.LoopAction */, (exec, done, ticks, a, b, obj, context, scope, bobj, inLoopOrSwitch) => {
|
|
2438
|
-
if ((inLoopOrSwitch === "switch" && a === "continue") || !inLoopOrSwitch) {
|
|
2439
|
-
throw new SandboxError("Illegal " + a + " statement");
|
|
2440
|
-
}
|
|
2441
|
-
done(undefined, new ExecReturn(context.ctx.auditReport, undefined, false, a === "break", a === "continue"));
|
|
2442
|
-
});
|
|
2443
|
-
addOps(13 /* LispType.If */, (exec, done, ticks, a, b, obj, context, scope, bobj, inLoopOrSwitch) => {
|
|
2444
|
-
exec(ticks, valueOrProp(a, context) ? b.t : b.f, scope, context, done);
|
|
2445
|
-
});
|
|
2446
|
-
addOps(15 /* LispType.InlineIf */, (exec, done, ticks, a, b, obj, context, scope) => {
|
|
2447
|
-
exec(ticks, valueOrProp(a, context) ? b.t : b.f, scope, context, done);
|
|
2448
|
-
});
|
|
2449
|
-
addOps(16 /* LispType.InlineIfCase */, (exec, done, ticks, a, b) => done(undefined, new If(a, b)));
|
|
2450
|
-
addOps(14 /* LispType.IfCase */, (exec, done, ticks, a, b) => done(undefined, new If(a, b)));
|
|
2451
|
-
addOps(40 /* LispType.Switch */, (exec, done, ticks, a, b, obj, context, scope) => {
|
|
2452
|
-
exec(ticks, a, scope, context, (err, toTest) => {
|
|
2453
|
-
if (err) {
|
|
2454
|
-
done(err);
|
|
2455
|
-
return;
|
|
2456
|
-
}
|
|
2457
|
-
toTest = valueOrProp(toTest, context);
|
|
2458
|
-
if (exec === execSync) {
|
|
2459
|
-
let res;
|
|
2460
|
-
let isTrue = false;
|
|
2461
|
-
for (let caseItem of b) {
|
|
2462
|
-
if (isTrue || (isTrue = !caseItem[1] || toTest === valueOrProp((syncDone((d) => exec(ticks, caseItem[1], scope, context, d))).result, context))) {
|
|
2463
|
-
if (!caseItem[2])
|
|
2464
|
-
continue;
|
|
2465
|
-
res = executeTree(ticks, context, caseItem[2], [scope], "switch");
|
|
2466
|
-
if (res.breakLoop)
|
|
2467
|
-
break;
|
|
2468
|
-
if (res.returned) {
|
|
2469
|
-
done(undefined, res);
|
|
2470
|
-
return;
|
|
2471
|
-
}
|
|
2472
|
-
if (!caseItem[1]) { // default case
|
|
2473
|
-
break;
|
|
2474
|
-
}
|
|
2475
|
-
}
|
|
2476
|
-
}
|
|
2477
|
-
done();
|
|
2478
|
-
}
|
|
2479
|
-
else {
|
|
2480
|
-
(async () => {
|
|
2481
|
-
let res;
|
|
2482
|
-
let isTrue = false;
|
|
2483
|
-
for (let caseItem of b) {
|
|
2484
|
-
let ad;
|
|
2485
|
-
if (isTrue || (isTrue = !caseItem[1] || toTest === valueOrProp((ad = asyncDone((d) => exec(ticks, caseItem[1], scope, context, d))).isInstant === true ? ad.instant : (await ad.p).result, context))) {
|
|
2486
|
-
if (!caseItem[2])
|
|
2487
|
-
continue;
|
|
2488
|
-
res = await executeTreeAsync(ticks, context, caseItem[2], [scope], "switch");
|
|
2489
|
-
if (res.breakLoop)
|
|
2490
|
-
break;
|
|
2491
|
-
if (res.returned) {
|
|
2492
|
-
done(undefined, res);
|
|
2493
|
-
return;
|
|
2494
|
-
}
|
|
2495
|
-
if (!caseItem[1]) { // default case
|
|
2496
|
-
break;
|
|
2497
|
-
}
|
|
2498
|
-
}
|
|
2499
|
-
}
|
|
2500
|
-
done();
|
|
2501
|
-
})().catch(done);
|
|
2502
|
-
}
|
|
2503
|
-
});
|
|
2504
|
-
});
|
|
2505
|
-
addOps(39 /* LispType.Try */, (exec, done, ticks, a, b, obj, context, scope, bobj, inLoopOrSwitch) => {
|
|
2506
|
-
const [exception, catchBody, finallyBody] = b;
|
|
2507
|
-
executeTreeWithDone(exec, (err, res) => {
|
|
2508
|
-
executeTreeWithDone(exec, (e) => {
|
|
2509
|
-
if (e)
|
|
2510
|
-
done(e);
|
|
2511
|
-
else if (err) {
|
|
2512
|
-
executeTreeWithDone(exec, done, ticks, context, catchBody, [new Scope(scope)], inLoopOrSwitch);
|
|
2513
|
-
}
|
|
2514
|
-
else {
|
|
2515
|
-
done(undefined, res);
|
|
2516
|
-
}
|
|
2517
|
-
}, ticks, context, finallyBody, [new Scope(scope, {})]);
|
|
2518
|
-
}, ticks, context, a, [new Scope(scope)], inLoopOrSwitch);
|
|
2519
|
-
});
|
|
2520
|
-
addOps(87 /* LispType.Void */, (exec, done, ticks, a) => { done(); });
|
|
2521
|
-
addOps(45 /* LispType.New */, (exec, done, ticks, a, b, obj, context) => {
|
|
2522
|
-
if (!context.ctx.globalsWhitelist.has(a) && !sandboxedFunctions.has(a)) {
|
|
2523
|
-
throw new SandboxError(`Object construction not allowed: ${a.constructor.name}`);
|
|
2524
|
-
}
|
|
2525
|
-
done(undefined, new a(...b));
|
|
2526
|
-
});
|
|
2527
|
-
addOps(46 /* LispType.Throw */, (exec, done, ticks, a, b) => { done(b); });
|
|
2528
|
-
addOps(43 /* LispType.Expression */, (exec, done, ticks, a) => done(undefined, a.pop()));
|
|
2529
|
-
addOps(0 /* LispType.None */, (exec, done, ticks, a) => done());
|
|
2530
|
-
function valueOrProp(a, context) {
|
|
2531
|
-
if (a instanceof Prop)
|
|
2532
|
-
return a.get(context);
|
|
2533
|
-
if (a === optional)
|
|
2534
|
-
return undefined;
|
|
2535
|
-
return a;
|
|
2536
|
-
}
|
|
2537
|
-
function execMany(ticks, exec, tree, done, scope, context, inLoopOrSwitch) {
|
|
2538
|
-
if (exec === execSync) {
|
|
2539
|
-
_execManySync(ticks, tree, done, scope, context, inLoopOrSwitch);
|
|
2540
|
-
}
|
|
2541
|
-
else {
|
|
2542
|
-
_execManyAsync(ticks, tree, done, scope, context, inLoopOrSwitch).catch(done);
|
|
2543
|
-
}
|
|
2544
|
-
}
|
|
2545
|
-
function _execManySync(ticks, tree, done, scope, context, inLoopOrSwitch) {
|
|
2546
|
-
let ret = [];
|
|
2547
|
-
for (let i = 0; i < tree.length; i++) {
|
|
2548
|
-
let res;
|
|
2549
|
-
try {
|
|
2550
|
-
res = syncDone((d) => execSync(ticks, tree[i], scope, context, d, inLoopOrSwitch)).result;
|
|
2551
|
-
}
|
|
2552
|
-
catch (e) {
|
|
2553
|
-
done(e);
|
|
2554
|
-
return;
|
|
2555
|
-
}
|
|
2556
|
-
if (res instanceof ExecReturn && (res.returned || res.breakLoop || res.continueLoop)) {
|
|
2557
|
-
done(undefined, res);
|
|
2558
|
-
return;
|
|
2559
|
-
}
|
|
2560
|
-
if (isLisp(tree[i]) && tree[i][0] === 8 /* LispType.Return */) {
|
|
2561
|
-
done(undefined, new ExecReturn(context.ctx.auditReport, res, true));
|
|
2562
|
-
return;
|
|
2563
|
-
}
|
|
2564
|
-
ret.push(res);
|
|
2565
|
-
}
|
|
2566
|
-
done(undefined, ret);
|
|
2567
|
-
}
|
|
2568
|
-
async function _execManyAsync(ticks, tree, done, scope, context, inLoopOrSwitch) {
|
|
2569
|
-
let ret = [];
|
|
2570
|
-
for (let i = 0; i < tree.length; i++) {
|
|
2571
|
-
let res;
|
|
2572
|
-
try {
|
|
2573
|
-
let ad;
|
|
2574
|
-
res = (ad = asyncDone((d) => execAsync(ticks, tree[i], scope, context, d, inLoopOrSwitch))).isInstant === true ? ad.instant : (await ad.p).result;
|
|
2575
|
-
}
|
|
2576
|
-
catch (e) {
|
|
2577
|
-
done(e);
|
|
2578
|
-
return;
|
|
2579
|
-
}
|
|
2580
|
-
if (res instanceof ExecReturn && (res.returned || res.breakLoop || res.continueLoop)) {
|
|
2581
|
-
done(undefined, res);
|
|
2582
|
-
return;
|
|
2583
|
-
}
|
|
2584
|
-
if (isLisp(tree[i]) && tree[i][0] === 8 /* LispType.Return */) {
|
|
2585
|
-
done(undefined, new ExecReturn(context.ctx.auditReport, res, true));
|
|
2586
|
-
return;
|
|
2587
|
-
}
|
|
2588
|
-
ret.push(res);
|
|
2589
|
-
}
|
|
2590
|
-
done(undefined, ret);
|
|
2591
|
-
}
|
|
2592
|
-
function asyncDone(callback) {
|
|
2593
|
-
let isInstant = false;
|
|
2594
|
-
let instant;
|
|
2595
|
-
const p = new Promise((resolve, reject) => {
|
|
2596
|
-
callback((err, result) => {
|
|
2597
|
-
if (err)
|
|
2598
|
-
reject(err);
|
|
2599
|
-
else {
|
|
2600
|
-
isInstant = true;
|
|
2601
|
-
instant = result;
|
|
2602
|
-
resolve({ result });
|
|
2603
|
-
}
|
|
2604
|
-
});
|
|
2605
|
-
});
|
|
2606
|
-
return {
|
|
2607
|
-
isInstant,
|
|
2608
|
-
instant,
|
|
2609
|
-
p
|
|
2610
|
-
};
|
|
2611
|
-
}
|
|
2612
|
-
function syncDone(callback) {
|
|
2613
|
-
let result;
|
|
2614
|
-
let err;
|
|
2615
|
-
callback((e, r) => {
|
|
2616
|
-
err = e;
|
|
2617
|
-
result = r;
|
|
2618
|
-
});
|
|
2619
|
-
if (err)
|
|
2620
|
-
throw err;
|
|
2621
|
-
return { result };
|
|
2622
|
-
}
|
|
2623
|
-
async function execAsync(ticks, tree, scope, context, doneOriginal, inLoopOrSwitch) {
|
|
2624
|
-
let done = doneOriginal;
|
|
2625
|
-
const p = new Promise((resolve) => {
|
|
2626
|
-
done = (e, r) => {
|
|
2627
|
-
doneOriginal(e, r);
|
|
2628
|
-
resolve();
|
|
2629
|
-
};
|
|
2630
|
-
});
|
|
2631
|
-
if (_execNoneRecurse(ticks, tree, scope, context, done, true, inLoopOrSwitch)) ;
|
|
2632
|
-
else if (isLisp(tree)) {
|
|
2633
|
-
let op = tree[0];
|
|
2634
|
-
let obj;
|
|
2635
|
-
try {
|
|
2636
|
-
let ad;
|
|
2637
|
-
obj = (ad = asyncDone((d) => execAsync(ticks, tree[1], scope, context, d, inLoopOrSwitch))).isInstant === true ? ad.instant : (await ad.p).result;
|
|
2638
|
-
}
|
|
2639
|
-
catch (e) {
|
|
2640
|
-
done(e);
|
|
2641
|
-
return;
|
|
2642
|
-
}
|
|
2643
|
-
let a = obj;
|
|
2644
|
-
try {
|
|
2645
|
-
a = obj instanceof Prop ? obj.get(context) : obj;
|
|
2646
|
-
}
|
|
2647
|
-
catch (e) {
|
|
2648
|
-
done(e);
|
|
2649
|
-
return;
|
|
2650
|
-
}
|
|
2651
|
-
if (op === 20 /* LispType.PropOptional */ || op === 21 /* LispType.CallOptional */) {
|
|
2652
|
-
if (a === undefined || a === null) {
|
|
2653
|
-
done(undefined, optional);
|
|
2654
|
-
return;
|
|
2655
|
-
}
|
|
2656
|
-
op = op === 20 /* LispType.PropOptional */ ? 1 /* LispType.Prop */ : 5 /* LispType.Call */;
|
|
2657
|
-
}
|
|
2658
|
-
if (a === optional) {
|
|
2659
|
-
if (op === 1 /* LispType.Prop */ || op === 5 /* LispType.Call */) {
|
|
2660
|
-
done(undefined, a);
|
|
2661
|
-
return;
|
|
2662
|
-
}
|
|
2663
|
-
else {
|
|
2664
|
-
a = undefined;
|
|
2665
|
-
}
|
|
2666
|
-
}
|
|
2667
|
-
let bobj;
|
|
2668
|
-
try {
|
|
2669
|
-
let ad;
|
|
2670
|
-
bobj = (ad = asyncDone((d) => execAsync(ticks, tree[2], scope, context, d, inLoopOrSwitch))).isInstant === true ? ad.instant : (await ad.p).result;
|
|
2671
|
-
}
|
|
2672
|
-
catch (e) {
|
|
2673
|
-
done(e);
|
|
2674
|
-
return;
|
|
2675
|
-
}
|
|
2676
|
-
let b = bobj;
|
|
2677
|
-
try {
|
|
2678
|
-
b = bobj instanceof Prop ? bobj.get(context) : bobj;
|
|
2679
|
-
}
|
|
2680
|
-
catch (e) {
|
|
2681
|
-
done(e);
|
|
2682
|
-
return;
|
|
2683
|
-
}
|
|
2684
|
-
if (b === optional) {
|
|
2685
|
-
b = undefined;
|
|
2686
|
-
}
|
|
2687
|
-
if (ops.has(op)) {
|
|
2688
|
-
try {
|
|
2689
|
-
ops.get(op)(execAsync, done, ticks, a, b, obj, context, scope, bobj, inLoopOrSwitch);
|
|
2690
|
-
}
|
|
2691
|
-
catch (err) {
|
|
2692
|
-
done(err);
|
|
2693
|
-
}
|
|
2694
|
-
}
|
|
2695
|
-
else {
|
|
2696
|
-
done(new SyntaxError('Unknown operator: ' + op));
|
|
2697
|
-
}
|
|
2698
|
-
}
|
|
2699
|
-
await p;
|
|
2700
|
-
}
|
|
2701
|
-
function execSync(ticks, tree, scope, context, done, inLoopOrSwitch) {
|
|
2702
|
-
if (_execNoneRecurse(ticks, tree, scope, context, done, false, inLoopOrSwitch)) ;
|
|
2703
|
-
else if (isLisp(tree)) {
|
|
2704
|
-
let op = tree[0];
|
|
2705
|
-
let obj;
|
|
2706
|
-
try {
|
|
2707
|
-
obj = syncDone((d) => execSync(ticks, tree[1], scope, context, d, inLoopOrSwitch)).result;
|
|
2708
|
-
}
|
|
2709
|
-
catch (e) {
|
|
2710
|
-
done(e);
|
|
2711
|
-
return;
|
|
2712
|
-
}
|
|
2713
|
-
let a = obj;
|
|
2714
|
-
try {
|
|
2715
|
-
a = obj instanceof Prop ? obj.get(context) : obj;
|
|
2716
|
-
}
|
|
2717
|
-
catch (e) {
|
|
2718
|
-
done(e);
|
|
2719
|
-
return;
|
|
2720
|
-
}
|
|
2721
|
-
if (op === 20 /* LispType.PropOptional */ || op === 21 /* LispType.CallOptional */) {
|
|
2722
|
-
if (a === undefined || a === null) {
|
|
2723
|
-
done(undefined, optional);
|
|
2724
|
-
return;
|
|
2725
|
-
}
|
|
2726
|
-
op = op === 20 /* LispType.PropOptional */ ? 1 /* LispType.Prop */ : 5 /* LispType.Call */;
|
|
2727
|
-
}
|
|
2728
|
-
if (a === optional) {
|
|
2729
|
-
if (op === 1 /* LispType.Prop */ || op === 5 /* LispType.Call */) {
|
|
2730
|
-
done(undefined, a);
|
|
2731
|
-
return;
|
|
2732
|
-
}
|
|
2733
|
-
else {
|
|
2734
|
-
a = undefined;
|
|
2735
|
-
}
|
|
2736
|
-
}
|
|
2737
|
-
let bobj;
|
|
2738
|
-
try {
|
|
2739
|
-
bobj = syncDone((d) => execSync(ticks, tree[2], scope, context, d, inLoopOrSwitch)).result;
|
|
2740
|
-
}
|
|
2741
|
-
catch (e) {
|
|
2742
|
-
done(e);
|
|
2743
|
-
return;
|
|
2744
|
-
}
|
|
2745
|
-
let b = bobj;
|
|
2746
|
-
try {
|
|
2747
|
-
b = bobj instanceof Prop ? bobj.get(context) : bobj;
|
|
2748
|
-
}
|
|
2749
|
-
catch (e) {
|
|
2750
|
-
done(e);
|
|
2751
|
-
return;
|
|
2752
|
-
}
|
|
2753
|
-
if (b === optional) {
|
|
2754
|
-
b = undefined;
|
|
2755
|
-
}
|
|
2756
|
-
if (ops.has(op)) {
|
|
2757
|
-
try {
|
|
2758
|
-
ops.get(op)(execSync, done, ticks, a, b, obj, context, scope, bobj, inLoopOrSwitch);
|
|
2759
|
-
}
|
|
2760
|
-
catch (err) {
|
|
2761
|
-
done(err);
|
|
2762
|
-
}
|
|
2763
|
-
}
|
|
2764
|
-
else {
|
|
2765
|
-
done(new SyntaxError('Unknown operator: ' + op));
|
|
2766
|
-
}
|
|
2767
|
-
}
|
|
2768
|
-
}
|
|
2769
|
-
const unexecTypes = new Set([
|
|
2770
|
-
11 /* LispType.ArrowFunction */,
|
|
2771
|
-
37 /* LispType.Function */,
|
|
2772
|
-
10 /* LispType.InlineFunction */,
|
|
2773
|
-
38 /* LispType.Loop */,
|
|
2774
|
-
39 /* LispType.Try */,
|
|
2775
|
-
40 /* LispType.Switch */,
|
|
2776
|
-
14 /* LispType.IfCase */,
|
|
2777
|
-
16 /* LispType.InlineIfCase */,
|
|
2778
|
-
60 /* LispType.Typeof */
|
|
2779
|
-
]);
|
|
2780
|
-
function _execNoneRecurse(ticks, tree, scope, context, done, isAsync, inLoopOrSwitch) {
|
|
2781
|
-
const exec = isAsync ? execAsync : execSync;
|
|
2782
|
-
if (context.ctx.options.executionQuota <= ticks.ticks) {
|
|
2783
|
-
if (typeof context.ctx.options.onExecutionQuotaReached === 'function' && context.ctx.options.onExecutionQuotaReached(ticks, scope, context, tree)) ;
|
|
2784
|
-
else {
|
|
2785
|
-
done(new SandboxError("Execution quota exceeded"));
|
|
2786
|
-
return;
|
|
2787
|
-
}
|
|
2788
|
-
}
|
|
2789
|
-
ticks.ticks++;
|
|
2790
|
-
currentTicks = ticks;
|
|
2791
|
-
if (tree instanceof Prop) {
|
|
2792
|
-
try {
|
|
2793
|
-
done(undefined, tree.get(context));
|
|
2794
|
-
}
|
|
2795
|
-
catch (err) {
|
|
2796
|
-
done(err);
|
|
2797
|
-
}
|
|
2798
|
-
}
|
|
2799
|
-
else if (tree === optional) {
|
|
2800
|
-
done();
|
|
2801
|
-
}
|
|
2802
|
-
else if (Array.isArray(tree) && !isLisp(tree)) {
|
|
2803
|
-
if (tree[0] === 0 /* LispType.None */) {
|
|
2804
|
-
done();
|
|
2805
|
-
}
|
|
2806
|
-
else {
|
|
2807
|
-
execMany(ticks, exec, tree, done, scope, context, inLoopOrSwitch);
|
|
2808
|
-
}
|
|
2809
|
-
}
|
|
2810
|
-
else if (!isLisp(tree)) {
|
|
2811
|
-
done(undefined, tree);
|
|
2812
|
-
}
|
|
2813
|
-
else if (tree[0] === 42 /* LispType.Block */) {
|
|
2814
|
-
execMany(ticks, exec, tree[1], done, scope, context, inLoopOrSwitch);
|
|
2815
|
-
}
|
|
2816
|
-
else if (tree[0] === 44 /* LispType.Await */) {
|
|
2817
|
-
if (!isAsync) {
|
|
2818
|
-
done(new SandboxError("Illegal use of 'await', must be inside async function"));
|
|
2819
|
-
}
|
|
2820
|
-
else if (context.ctx.prototypeWhitelist?.has(Promise.prototype)) {
|
|
2821
|
-
execAsync(ticks, tree[1], scope, context, async (e, r) => {
|
|
2822
|
-
if (e)
|
|
2823
|
-
done(e);
|
|
2824
|
-
else
|
|
2825
|
-
try {
|
|
2826
|
-
done(undefined, await valueOrProp(r, context));
|
|
2827
|
-
}
|
|
2828
|
-
catch (err) {
|
|
2829
|
-
done(err);
|
|
2830
|
-
}
|
|
2831
|
-
}, inLoopOrSwitch).catch(done);
|
|
2832
|
-
}
|
|
2833
|
-
else {
|
|
2834
|
-
done(new SandboxError('Async/await is not permitted'));
|
|
2835
|
-
}
|
|
2836
|
-
}
|
|
2837
|
-
else if (unexecTypes.has(tree[0])) {
|
|
2838
|
-
try {
|
|
2839
|
-
ops.get(tree[0])(exec, done, ticks, tree[1], tree[2], tree, context, scope, undefined, inLoopOrSwitch);
|
|
2840
|
-
}
|
|
2841
|
-
catch (err) {
|
|
2842
|
-
done(err);
|
|
2843
|
-
}
|
|
2844
|
-
}
|
|
2845
|
-
else {
|
|
2846
|
-
return false;
|
|
2847
|
-
}
|
|
2848
|
-
return true;
|
|
2849
|
-
}
|
|
2850
|
-
function executeTree(ticks, context, executionTree, scopes = [], inLoopOrSwitch) {
|
|
2851
|
-
return syncDone((done) => executeTreeWithDone(execSync, done, ticks, context, executionTree, scopes, inLoopOrSwitch)).result;
|
|
2852
|
-
}
|
|
2853
|
-
async function executeTreeAsync(ticks, context, executionTree, scopes = [], inLoopOrSwitch) {
|
|
2854
|
-
let ad;
|
|
2855
|
-
return (ad = asyncDone((done) => executeTreeWithDone(execAsync, done, ticks, context, executionTree, scopes, inLoopOrSwitch))).isInstant === true ? ad.instant : (await ad.p).result;
|
|
2856
|
-
}
|
|
2857
|
-
function executeTreeWithDone(exec, done, ticks, context, executionTree, scopes = [], inLoopOrSwitch) {
|
|
2858
|
-
if (!executionTree) {
|
|
2859
|
-
done();
|
|
2860
|
-
return;
|
|
2861
|
-
}
|
|
2862
|
-
if (!(executionTree instanceof Array)) {
|
|
2863
|
-
throw new SyntaxError('Bad execution tree');
|
|
2864
|
-
}
|
|
2865
|
-
let scope = context.ctx.globalScope;
|
|
2866
|
-
let s;
|
|
2867
|
-
while (s = scopes.shift()) {
|
|
2868
|
-
if (typeof s !== "object")
|
|
2869
|
-
continue;
|
|
2870
|
-
if (s instanceof Scope) {
|
|
2871
|
-
scope = s;
|
|
2872
|
-
}
|
|
2873
|
-
else {
|
|
2874
|
-
scope = new Scope(scope, s, s instanceof LocalScope ? undefined : null);
|
|
2875
|
-
}
|
|
2876
|
-
}
|
|
2877
|
-
if (context.ctx.options.audit && !context.ctx.auditReport) {
|
|
2878
|
-
context.ctx.auditReport = {
|
|
2879
|
-
globalsAccess: new Set(),
|
|
2880
|
-
prototypeAccess: {},
|
|
2881
|
-
};
|
|
2882
|
-
}
|
|
2883
|
-
if (exec === execSync) {
|
|
2884
|
-
_executeWithDoneSync(done, ticks, context, executionTree, scope, inLoopOrSwitch);
|
|
2885
|
-
}
|
|
2886
|
-
else {
|
|
2887
|
-
_executeWithDoneAsync(done, ticks, context, executionTree, scope, inLoopOrSwitch).catch(done);
|
|
2888
|
-
}
|
|
2889
|
-
}
|
|
2890
|
-
function _executeWithDoneSync(done, ticks, context, executionTree, scope, inLoopOrSwitch) {
|
|
2891
|
-
if (!(executionTree instanceof Array))
|
|
2892
|
-
throw new SyntaxError('Bad execution tree');
|
|
2893
|
-
let i = 0;
|
|
2894
|
-
for (i = 0; i < executionTree.length; i++) {
|
|
2895
|
-
let res;
|
|
2896
|
-
let err;
|
|
2897
|
-
const current = executionTree[i];
|
|
2898
|
-
try {
|
|
2899
|
-
execSync(ticks, current, scope, context, (e, r) => {
|
|
2900
|
-
err = e;
|
|
2901
|
-
res = r;
|
|
2902
|
-
}, inLoopOrSwitch);
|
|
2903
|
-
}
|
|
2904
|
-
catch (e) {
|
|
2905
|
-
err = e;
|
|
2906
|
-
}
|
|
2907
|
-
if (err) {
|
|
2908
|
-
done(err);
|
|
2909
|
-
return;
|
|
2910
|
-
}
|
|
2911
|
-
if (res instanceof ExecReturn) {
|
|
2912
|
-
done(undefined, res);
|
|
2913
|
-
return;
|
|
2914
|
-
}
|
|
2915
|
-
if (isLisp(current) && current[0] === 8 /* LispType.Return */) {
|
|
2916
|
-
done(undefined, new ExecReturn(context.ctx.auditReport, res, true));
|
|
2917
|
-
return;
|
|
2918
|
-
}
|
|
2919
|
-
}
|
|
2920
|
-
done(undefined, new ExecReturn(context.ctx.auditReport, undefined, false));
|
|
2921
|
-
}
|
|
2922
|
-
async function _executeWithDoneAsync(done, ticks, context, executionTree, scope, inLoopOrSwitch) {
|
|
2923
|
-
if (!(executionTree instanceof Array))
|
|
2924
|
-
throw new SyntaxError('Bad execution tree');
|
|
2925
|
-
let i = 0;
|
|
2926
|
-
for (i = 0; i < executionTree.length; i++) {
|
|
2927
|
-
let res;
|
|
2928
|
-
let err;
|
|
2929
|
-
const current = executionTree[i];
|
|
2930
|
-
try {
|
|
2931
|
-
await execAsync(ticks, current, scope, context, (e, r) => {
|
|
2932
|
-
err = e;
|
|
2933
|
-
res = r;
|
|
2934
|
-
}, inLoopOrSwitch);
|
|
2935
|
-
}
|
|
2936
|
-
catch (e) {
|
|
2937
|
-
err = e;
|
|
2938
|
-
}
|
|
2939
|
-
if (err) {
|
|
2940
|
-
done(err);
|
|
2941
|
-
return;
|
|
2942
|
-
}
|
|
2943
|
-
if (res instanceof ExecReturn) {
|
|
2944
|
-
done(undefined, res);
|
|
2945
|
-
return;
|
|
2946
|
-
}
|
|
2947
|
-
if (isLisp(current) && current[0] === 8 /* LispType.Return */) {
|
|
2948
|
-
done(undefined, new ExecReturn(context.ctx.auditReport, res, true));
|
|
2949
|
-
return;
|
|
2950
|
-
}
|
|
2951
|
-
}
|
|
2952
|
-
done(undefined, new ExecReturn(context.ctx.auditReport, undefined, false));
|
|
2953
|
-
}
|
|
2954
51
|
|
|
2955
|
-
class
|
|
2956
|
-
constructor(globals) {
|
|
2957
|
-
if (globals === globalThis)
|
|
2958
|
-
return globalThis;
|
|
2959
|
-
for (let i in globals) {
|
|
2960
|
-
this[i] = globals[i];
|
|
2961
|
-
}
|
|
2962
|
-
}
|
|
2963
|
-
}
|
|
2964
|
-
class ExecContext {
|
|
2965
|
-
constructor(ctx, constants, tree, getSubscriptions, setSubscriptions, changeSubscriptions, setSubscriptionsGlobal, changeSubscriptionsGlobal, evals, registerSandboxFunction) {
|
|
2966
|
-
this.ctx = ctx;
|
|
2967
|
-
this.constants = constants;
|
|
2968
|
-
this.tree = tree;
|
|
2969
|
-
this.getSubscriptions = getSubscriptions;
|
|
2970
|
-
this.setSubscriptions = setSubscriptions;
|
|
2971
|
-
this.changeSubscriptions = changeSubscriptions;
|
|
2972
|
-
this.setSubscriptionsGlobal = setSubscriptionsGlobal;
|
|
2973
|
-
this.changeSubscriptionsGlobal = changeSubscriptionsGlobal;
|
|
2974
|
-
this.evals = evals;
|
|
2975
|
-
this.registerSandboxFunction = registerSandboxFunction;
|
|
2976
|
-
}
|
|
2977
|
-
}
|
|
2978
|
-
function subscribeSet(obj, name, callback, context) {
|
|
2979
|
-
const names = context.setSubscriptions.get(obj) || new Map();
|
|
2980
|
-
context.setSubscriptions.set(obj, names);
|
|
2981
|
-
const callbacks = names.get(name) || new Set();
|
|
2982
|
-
names.set(name, callbacks);
|
|
2983
|
-
callbacks.add(callback);
|
|
2984
|
-
let changeCbs;
|
|
2985
|
-
if (obj && obj[name] && typeof obj[name] === "object") {
|
|
2986
|
-
changeCbs = context.changeSubscriptions.get(obj[name]) || new Set();
|
|
2987
|
-
changeCbs.add(callback);
|
|
2988
|
-
context.changeSubscriptions.set(obj[name], changeCbs);
|
|
2989
|
-
}
|
|
2990
|
-
return {
|
|
2991
|
-
unsubscribe: () => {
|
|
2992
|
-
callbacks.delete(callback);
|
|
2993
|
-
changeCbs?.delete(callback);
|
|
2994
|
-
}
|
|
2995
|
-
};
|
|
2996
|
-
}
|
|
2997
|
-
class Sandbox {
|
|
52
|
+
class Sandbox extends SandboxExec.default {
|
|
2998
53
|
constructor(options) {
|
|
2999
|
-
|
|
3000
|
-
this.changeSubscriptions = new WeakMap();
|
|
3001
|
-
this.sandboxFunctions = new WeakMap();
|
|
3002
|
-
options = Object.assign({
|
|
3003
|
-
audit: false,
|
|
3004
|
-
forbidFunctionCalls: false,
|
|
3005
|
-
forbidFunctionCreation: false,
|
|
3006
|
-
globals: Sandbox.SAFE_GLOBALS,
|
|
3007
|
-
prototypeWhitelist: Sandbox.SAFE_PROTOTYPES,
|
|
3008
|
-
prototypeReplacements: new Map(),
|
|
3009
|
-
}, options || {});
|
|
3010
|
-
const sandboxGlobal = new SandboxGlobal(options.globals);
|
|
3011
|
-
this.context = {
|
|
3012
|
-
sandbox: this,
|
|
3013
|
-
globalsWhitelist: new Set(Object.values(options.globals)),
|
|
3014
|
-
prototypeWhitelist: new Map([...options.prototypeWhitelist].map((a) => [a[0].prototype, a[1]])),
|
|
3015
|
-
options,
|
|
3016
|
-
globalScope: new Scope(null, options.globals, sandboxGlobal),
|
|
3017
|
-
sandboxGlobal
|
|
3018
|
-
};
|
|
3019
|
-
this.context.prototypeWhitelist.set(Object.getPrototypeOf([][Symbol.iterator]()), new Set());
|
|
3020
|
-
}
|
|
3021
|
-
static get SAFE_GLOBALS() {
|
|
3022
|
-
return {
|
|
3023
|
-
Function,
|
|
3024
|
-
console: {
|
|
3025
|
-
debug: console.debug,
|
|
3026
|
-
error: console.error,
|
|
3027
|
-
info: console.info,
|
|
3028
|
-
log: console.log,
|
|
3029
|
-
table: console.table,
|
|
3030
|
-
warn: console.warn
|
|
3031
|
-
},
|
|
3032
|
-
isFinite,
|
|
3033
|
-
isNaN,
|
|
3034
|
-
parseFloat,
|
|
3035
|
-
parseInt,
|
|
3036
|
-
decodeURI,
|
|
3037
|
-
decodeURIComponent,
|
|
3038
|
-
encodeURI,
|
|
3039
|
-
encodeURIComponent,
|
|
3040
|
-
escape,
|
|
3041
|
-
unescape,
|
|
3042
|
-
Boolean,
|
|
3043
|
-
Number,
|
|
3044
|
-
BigInt,
|
|
3045
|
-
String,
|
|
3046
|
-
Object,
|
|
3047
|
-
Array,
|
|
3048
|
-
Symbol,
|
|
3049
|
-
Error,
|
|
3050
|
-
EvalError,
|
|
3051
|
-
RangeError,
|
|
3052
|
-
ReferenceError,
|
|
3053
|
-
SyntaxError,
|
|
3054
|
-
TypeError,
|
|
3055
|
-
URIError,
|
|
3056
|
-
Int8Array,
|
|
3057
|
-
Uint8Array,
|
|
3058
|
-
Uint8ClampedArray,
|
|
3059
|
-
Int16Array,
|
|
3060
|
-
Uint16Array,
|
|
3061
|
-
Int32Array,
|
|
3062
|
-
Uint32Array,
|
|
3063
|
-
Float32Array,
|
|
3064
|
-
Float64Array,
|
|
3065
|
-
Map,
|
|
3066
|
-
Set,
|
|
3067
|
-
WeakMap,
|
|
3068
|
-
WeakSet,
|
|
3069
|
-
Promise,
|
|
3070
|
-
Intl,
|
|
3071
|
-
JSON,
|
|
3072
|
-
Math,
|
|
3073
|
-
Date,
|
|
3074
|
-
RegExp
|
|
3075
|
-
};
|
|
3076
|
-
}
|
|
3077
|
-
static get SAFE_PROTOTYPES() {
|
|
3078
|
-
let protos = [
|
|
3079
|
-
SandboxGlobal,
|
|
3080
|
-
Function,
|
|
3081
|
-
Boolean,
|
|
3082
|
-
Number,
|
|
3083
|
-
BigInt,
|
|
3084
|
-
String,
|
|
3085
|
-
Date,
|
|
3086
|
-
Error,
|
|
3087
|
-
Array,
|
|
3088
|
-
Int8Array,
|
|
3089
|
-
Uint8Array,
|
|
3090
|
-
Uint8ClampedArray,
|
|
3091
|
-
Int16Array,
|
|
3092
|
-
Uint16Array,
|
|
3093
|
-
Int32Array,
|
|
3094
|
-
Uint32Array,
|
|
3095
|
-
Float32Array,
|
|
3096
|
-
Float64Array,
|
|
3097
|
-
Map,
|
|
3098
|
-
Set,
|
|
3099
|
-
WeakMap,
|
|
3100
|
-
WeakSet,
|
|
3101
|
-
Promise,
|
|
3102
|
-
Symbol,
|
|
3103
|
-
Date,
|
|
3104
|
-
RegExp
|
|
3105
|
-
];
|
|
3106
|
-
let map = new Map();
|
|
3107
|
-
protos.forEach((proto) => {
|
|
3108
|
-
map.set(proto, new Set());
|
|
3109
|
-
});
|
|
3110
|
-
map.set(Object, new Set([
|
|
3111
|
-
'entries',
|
|
3112
|
-
'fromEntries',
|
|
3113
|
-
'getOwnPropertyNames',
|
|
3114
|
-
'is',
|
|
3115
|
-
'keys',
|
|
3116
|
-
'hasOwnProperty',
|
|
3117
|
-
'isPrototypeOf',
|
|
3118
|
-
'propertyIsEnumerable',
|
|
3119
|
-
'toLocaleString',
|
|
3120
|
-
'toString',
|
|
3121
|
-
'valueOf',
|
|
3122
|
-
'values'
|
|
3123
|
-
]));
|
|
3124
|
-
return map;
|
|
3125
|
-
}
|
|
3126
|
-
subscribeGet(callback, context) {
|
|
3127
|
-
context.getSubscriptions.add(callback);
|
|
3128
|
-
return { unsubscribe: () => context.getSubscriptions.delete(callback) };
|
|
3129
|
-
}
|
|
3130
|
-
subscribeSet(obj, name, callback, context) {
|
|
3131
|
-
return subscribeSet(obj, name, callback, context);
|
|
3132
|
-
}
|
|
3133
|
-
subscribeSetGlobal(obj, name, callback) {
|
|
3134
|
-
return subscribeSet(obj, name, callback, this);
|
|
54
|
+
super(options, createEvalContext());
|
|
3135
55
|
}
|
|
3136
56
|
static audit(code, scopes = []) {
|
|
3137
57
|
const globals = {};
|
|
3138
|
-
for (
|
|
58
|
+
for (const i of Object.getOwnPropertyNames(globalThis)) {
|
|
3139
59
|
globals[i] = globalThis[i];
|
|
3140
60
|
}
|
|
3141
|
-
const sandbox = new
|
|
61
|
+
const sandbox = new SandboxExec.default({
|
|
3142
62
|
globals,
|
|
3143
63
|
audit: true,
|
|
3144
64
|
});
|
|
3145
|
-
return sandbox.executeTree(
|
|
65
|
+
return sandbox.executeTree(utils.createExecContext(sandbox, parser.default(code, true), createEvalContext()), scopes);
|
|
3146
66
|
}
|
|
3147
67
|
static parse(code) {
|
|
3148
|
-
return
|
|
3149
|
-
}
|
|
3150
|
-
createContext(context, executionTree) {
|
|
3151
|
-
const evals = new Map();
|
|
3152
|
-
const execContext = new ExecContext(context, executionTree.constants, executionTree.tree, new Set(), new WeakMap(), new WeakMap(), this.setSubscriptions, this.changeSubscriptions, evals, (fn) => this.sandboxFunctions.set(fn, execContext));
|
|
3153
|
-
const func = sandboxFunction(execContext);
|
|
3154
|
-
evals.set(Function, func);
|
|
3155
|
-
evals.set(eval, sandboxedEval(func));
|
|
3156
|
-
evals.set(setTimeout, sandboxedSetTimeout(func));
|
|
3157
|
-
evals.set(setInterval, sandboxedSetInterval(func));
|
|
3158
|
-
return execContext;
|
|
3159
|
-
}
|
|
3160
|
-
getContext(fn) {
|
|
3161
|
-
return this.sandboxFunctions.get(fn);
|
|
3162
|
-
}
|
|
3163
|
-
executeTree(context, scopes = []) {
|
|
3164
|
-
return executeTree({
|
|
3165
|
-
ticks: BigInt(0),
|
|
3166
|
-
}, context, context.tree, scopes);
|
|
3167
|
-
}
|
|
3168
|
-
executeTreeAsync(context, scopes = []) {
|
|
3169
|
-
return executeTreeAsync({
|
|
3170
|
-
ticks: BigInt(0),
|
|
3171
|
-
}, context, context.tree, scopes);
|
|
68
|
+
return parser.default(code);
|
|
3172
69
|
}
|
|
3173
70
|
compile(code, optimize = false) {
|
|
3174
|
-
const parsed =
|
|
71
|
+
const parsed = parser.default(code, optimize);
|
|
3175
72
|
const exec = (...scopes) => {
|
|
3176
|
-
const context =
|
|
73
|
+
const context = utils.createExecContext(this, parsed, this.evalContext);
|
|
3177
74
|
return { context, run: () => this.executeTree(context, [...scopes]).result };
|
|
3178
75
|
};
|
|
3179
76
|
return exec;
|
|
3180
77
|
}
|
|
3181
|
-
;
|
|
3182
78
|
compileAsync(code, optimize = false) {
|
|
3183
|
-
const parsed =
|
|
79
|
+
const parsed = parser.default(code, optimize);
|
|
3184
80
|
const exec = (...scopes) => {
|
|
3185
|
-
const context =
|
|
3186
|
-
return {
|
|
81
|
+
const context = utils.createExecContext(this, parsed, this.evalContext);
|
|
82
|
+
return {
|
|
83
|
+
context,
|
|
84
|
+
run: () => this.executeTreeAsync(context, [...scopes]).then((ret) => ret.result),
|
|
85
|
+
};
|
|
3187
86
|
};
|
|
3188
87
|
return exec;
|
|
3189
88
|
}
|
|
3190
|
-
;
|
|
3191
89
|
compileExpression(code, optimize = false) {
|
|
3192
|
-
const parsed =
|
|
90
|
+
const parsed = parser.default(code, optimize, true);
|
|
3193
91
|
const exec = (...scopes) => {
|
|
3194
|
-
const context =
|
|
92
|
+
const context = utils.createExecContext(this, parsed, this.evalContext);
|
|
3195
93
|
return { context, run: () => this.executeTree(context, [...scopes]).result };
|
|
3196
94
|
};
|
|
3197
95
|
return exec;
|
|
3198
96
|
}
|
|
3199
97
|
compileExpressionAsync(code, optimize = false) {
|
|
3200
|
-
const parsed =
|
|
98
|
+
const parsed = parser.default(code, optimize, true);
|
|
3201
99
|
const exec = (...scopes) => {
|
|
3202
|
-
const context =
|
|
3203
|
-
return {
|
|
100
|
+
const context = utils.createExecContext(this, parsed, this.evalContext);
|
|
101
|
+
return {
|
|
102
|
+
context,
|
|
103
|
+
run: () => this.executeTreeAsync(context, [...scopes]).then((ret) => ret.result),
|
|
104
|
+
};
|
|
3204
105
|
};
|
|
3205
106
|
return exec;
|
|
3206
107
|
}
|
|
3207
108
|
}
|
|
3208
109
|
|
|
3209
|
-
exports.ExecContext = ExecContext;
|
|
3210
|
-
exports.FunctionScope = FunctionScope;
|
|
3211
|
-
exports.LocalScope = LocalScope;
|
|
3212
|
-
exports.SandboxGlobal = SandboxGlobal;
|
|
3213
|
-
exports.assignCheck = assignCheck;
|
|
3214
|
-
exports.asyncDone = asyncDone;
|
|
3215
110
|
exports.default = Sandbox;
|
|
3216
|
-
exports.execAsync = execAsync;
|
|
3217
|
-
exports.execMany = execMany;
|
|
3218
|
-
exports.execSync = execSync;
|
|
3219
|
-
exports.executeTree = executeTree;
|
|
3220
|
-
exports.executeTreeAsync = executeTreeAsync;
|
|
3221
|
-
exports.executionOps = ops;
|
|
3222
|
-
exports.expectTypes = expectTypes;
|
|
3223
|
-
exports.setLispType = setLispType;
|
|
3224
|
-
exports.syncDone = syncDone;
|