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