@cuxt/sandboxjs 0.1.1 → 0.1.3
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/LICENSE +21 -21
- package/README.md +198 -185
- package/{build → dist/cjs}/Sandbox.d.ts +15 -6
- package/dist/cjs/Sandbox.js +126 -0
- package/dist/{SandboxExec.d.ts → cjs/SandboxExec.d.ts} +8 -17
- package/dist/cjs/SandboxExec.js +227 -0
- package/{build/eval.d.ts → dist/cjs/eval/index.d.ts} +10 -2
- package/dist/cjs/eval/index.js +233 -0
- package/dist/cjs/executor/executorUtils.d.ts +161 -0
- package/dist/cjs/executor/executorUtils.js +930 -0
- package/dist/cjs/executor/index.d.ts +1 -0
- package/dist/cjs/executor/index.js +2 -0
- package/dist/cjs/executor/ops/assignment.d.ts +1 -0
- package/dist/cjs/executor/ops/assignment.js +88 -0
- package/dist/cjs/executor/ops/call.d.ts +1 -0
- package/dist/cjs/executor/ops/call.js +160 -0
- package/dist/cjs/executor/ops/comparison.d.ts +1 -0
- package/dist/cjs/executor/ops/comparison.js +36 -0
- package/dist/cjs/executor/ops/control.d.ts +1 -0
- package/dist/cjs/executor/ops/control.js +203 -0
- package/dist/cjs/executor/ops/functions.d.ts +1 -0
- package/dist/cjs/executor/ops/functions.js +55 -0
- package/dist/cjs/executor/ops/index.d.ts +0 -0
- package/dist/cjs/executor/ops/index.js +11 -0
- package/dist/cjs/executor/ops/literals.d.ts +1 -0
- package/dist/cjs/executor/ops/literals.js +45 -0
- package/dist/cjs/executor/ops/misc.d.ts +1 -0
- package/dist/cjs/executor/ops/misc.js +25 -0
- package/dist/cjs/executor/ops/object.d.ts +1 -0
- package/dist/cjs/executor/ops/object.js +51 -0
- package/dist/cjs/executor/ops/prop.d.ts +1 -0
- package/dist/cjs/executor/ops/prop.js +70 -0
- package/dist/cjs/executor/ops/unary.d.ts +1 -0
- package/dist/cjs/executor/ops/unary.js +48 -0
- package/dist/cjs/executor/ops/variables.d.ts +1 -0
- package/dist/cjs/executor/ops/variables.js +16 -0
- package/dist/cjs/executor/opsRegistry.d.ts +33 -0
- package/dist/cjs/executor/opsRegistry.js +8 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/parser/index.d.ts +3 -0
- package/dist/cjs/parser/index.js +1 -0
- package/dist/{parser.d.ts → cjs/parser/lisp.d.ts} +29 -42
- package/dist/cjs/parser/lispTypes/conditionals.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/conditionals.js +135 -0
- package/dist/cjs/parser/lispTypes/control.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/control.js +204 -0
- package/dist/cjs/parser/lispTypes/declarations.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/declarations.js +99 -0
- package/dist/cjs/parser/lispTypes/index.d.ts +3 -0
- package/dist/cjs/parser/lispTypes/index.js +17 -0
- package/dist/cjs/parser/lispTypes/operators.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/operators.js +252 -0
- package/dist/cjs/parser/lispTypes/shared.d.ts +38 -0
- package/dist/cjs/parser/lispTypes/structures.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/structures.js +188 -0
- package/dist/cjs/parser/lispTypes/values.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/values.js +89 -0
- package/dist/cjs/parser/parserUtils.d.ts +34 -0
- package/dist/cjs/parser/parserUtils.js +968 -0
- package/dist/cjs/utils/CodeString.d.ts +16 -0
- package/dist/cjs/utils/CodeString.js +64 -0
- package/dist/cjs/utils/ExecContext.d.ts +34 -0
- package/dist/cjs/utils/ExecContext.js +171 -0
- package/dist/cjs/utils/Prop.d.ts +16 -0
- package/dist/cjs/utils/Prop.js +81 -0
- package/dist/cjs/utils/Scope.d.ts +47 -0
- package/dist/cjs/utils/Scope.js +127 -0
- package/dist/cjs/utils/errors.d.ts +10 -0
- package/dist/cjs/utils/errors.js +12 -0
- package/dist/cjs/utils/functionReplacements.d.ts +11 -0
- package/dist/cjs/utils/functionReplacements.js +362 -0
- package/dist/cjs/utils/index.d.ts +7 -0
- package/dist/cjs/utils/index.js +7 -0
- package/dist/cjs/utils/types.d.ts +221 -0
- package/dist/cjs/utils/types.js +164 -0
- package/dist/cjs/utils/unraw.js +145 -0
- package/dist/{node → esm}/Sandbox.d.ts +15 -6
- package/dist/esm/Sandbox.js +115 -0
- package/dist/esm/Sandbox.js.map +1 -0
- package/{build → dist/esm}/SandboxExec.d.ts +8 -17
- package/dist/esm/SandboxExec.js +224 -0
- package/dist/esm/SandboxExec.js.map +1 -0
- package/dist/{eval.d.ts → esm/eval/index.d.ts} +10 -2
- package/dist/esm/eval/index.js +235 -0
- package/dist/esm/eval/index.js.map +1 -0
- package/dist/esm/executor/executorUtils.d.ts +161 -0
- package/dist/esm/executor/executorUtils.js +898 -0
- package/dist/esm/executor/executorUtils.js.map +1 -0
- package/dist/esm/executor/index.d.ts +1 -0
- package/dist/esm/executor/index.js +2 -0
- package/dist/esm/executor/ops/assignment.d.ts +1 -0
- package/dist/esm/executor/ops/assignment.js +90 -0
- package/dist/esm/executor/ops/assignment.js.map +1 -0
- package/dist/esm/executor/ops/call.d.ts +1 -0
- package/dist/esm/executor/ops/call.js +162 -0
- package/dist/esm/executor/ops/call.js.map +1 -0
- package/dist/esm/executor/ops/comparison.d.ts +1 -0
- package/dist/esm/executor/ops/comparison.js +38 -0
- package/dist/esm/executor/ops/comparison.js.map +1 -0
- package/dist/esm/executor/ops/control.d.ts +1 -0
- package/dist/esm/executor/ops/control.js +205 -0
- package/dist/esm/executor/ops/control.js.map +1 -0
- package/dist/esm/executor/ops/functions.d.ts +1 -0
- package/dist/esm/executor/ops/functions.js +57 -0
- package/dist/esm/executor/ops/functions.js.map +1 -0
- package/dist/esm/executor/ops/index.d.ts +0 -0
- package/dist/esm/executor/ops/index.js +11 -0
- package/dist/esm/executor/ops/literals.d.ts +1 -0
- package/dist/esm/executor/ops/literals.js +47 -0
- package/dist/esm/executor/ops/literals.js.map +1 -0
- package/dist/esm/executor/ops/misc.d.ts +1 -0
- package/dist/esm/executor/ops/misc.js +27 -0
- package/dist/esm/executor/ops/misc.js.map +1 -0
- package/dist/esm/executor/ops/object.d.ts +1 -0
- package/dist/esm/executor/ops/object.js +53 -0
- package/dist/esm/executor/ops/object.js.map +1 -0
- package/dist/esm/executor/ops/prop.d.ts +1 -0
- package/dist/esm/executor/ops/prop.js +72 -0
- package/dist/esm/executor/ops/prop.js.map +1 -0
- package/dist/esm/executor/ops/unary.d.ts +1 -0
- package/dist/esm/executor/ops/unary.js +50 -0
- package/dist/esm/executor/ops/unary.js.map +1 -0
- package/dist/esm/executor/ops/variables.d.ts +1 -0
- package/dist/esm/executor/ops/variables.js +18 -0
- package/dist/esm/executor/ops/variables.js.map +1 -0
- package/dist/esm/executor/opsRegistry.d.ts +33 -0
- package/dist/esm/executor/opsRegistry.js +9 -0
- package/dist/esm/executor/opsRegistry.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/parser/index.d.ts +3 -0
- package/dist/esm/parser/index.js +1 -0
- package/{build/parser.d.ts → dist/esm/parser/lisp.d.ts} +29 -42
- package/dist/esm/parser/lispTypes/conditionals.d.ts +2 -0
- package/dist/esm/parser/lispTypes/conditionals.js +137 -0
- package/dist/esm/parser/lispTypes/conditionals.js.map +1 -0
- package/dist/esm/parser/lispTypes/control.d.ts +2 -0
- package/dist/esm/parser/lispTypes/control.js +206 -0
- package/dist/esm/parser/lispTypes/control.js.map +1 -0
- package/dist/esm/parser/lispTypes/declarations.d.ts +2 -0
- package/dist/esm/parser/lispTypes/declarations.js +101 -0
- package/dist/esm/parser/lispTypes/declarations.js.map +1 -0
- package/dist/esm/parser/lispTypes/index.d.ts +3 -0
- package/dist/esm/parser/lispTypes/index.js +19 -0
- package/dist/esm/parser/lispTypes/index.js.map +1 -0
- package/dist/esm/parser/lispTypes/operators.d.ts +2 -0
- package/dist/esm/parser/lispTypes/operators.js +254 -0
- package/dist/esm/parser/lispTypes/operators.js.map +1 -0
- package/dist/esm/parser/lispTypes/shared.d.ts +38 -0
- package/dist/esm/parser/lispTypes/structures.d.ts +2 -0
- package/dist/esm/parser/lispTypes/structures.js +190 -0
- package/dist/esm/parser/lispTypes/structures.js.map +1 -0
- package/dist/esm/parser/lispTypes/values.d.ts +2 -0
- package/dist/esm/parser/lispTypes/values.js +91 -0
- package/dist/esm/parser/lispTypes/values.js.map +1 -0
- package/dist/esm/parser/parserUtils.d.ts +34 -0
- package/dist/esm/parser/parserUtils.js +959 -0
- package/dist/esm/parser/parserUtils.js.map +1 -0
- package/dist/esm/utils/CodeString.d.ts +16 -0
- package/dist/esm/utils/CodeString.js +66 -0
- package/dist/esm/utils/CodeString.js.map +1 -0
- package/dist/esm/utils/ExecContext.d.ts +34 -0
- package/dist/esm/utils/ExecContext.js +168 -0
- package/dist/esm/utils/ExecContext.js.map +1 -0
- package/dist/esm/utils/Prop.d.ts +16 -0
- package/dist/esm/utils/Prop.js +80 -0
- package/dist/esm/utils/Prop.js.map +1 -0
- package/dist/esm/utils/Scope.d.ts +47 -0
- package/dist/esm/utils/Scope.js +122 -0
- package/dist/esm/utils/Scope.js.map +1 -0
- package/dist/esm/utils/errors.d.ts +10 -0
- package/dist/esm/utils/errors.js +10 -0
- package/dist/esm/utils/errors.js.map +1 -0
- package/dist/esm/utils/functionReplacements.d.ts +11 -0
- package/dist/esm/utils/functionReplacements.js +361 -0
- package/dist/esm/utils/functionReplacements.js.map +1 -0
- package/dist/esm/utils/index.d.ts +7 -0
- package/dist/esm/utils/index.js +7 -0
- package/dist/esm/utils/types.d.ts +221 -0
- package/dist/esm/utils/types.js +160 -0
- package/dist/esm/utils/types.js.map +1 -0
- package/dist/esm/utils/unraw.js +147 -0
- package/dist/esm/utils/unraw.js.map +1 -0
- package/dist/umd/Sandbox.min.js +2 -0
- package/dist/umd/Sandbox.min.js.map +1 -0
- package/dist/umd/SandboxExec.min.js +2 -0
- package/dist/umd/SandboxExec.min.js.map +1 -0
- package/package.json +11 -9
- package/build/Sandbox.js +0 -62
- package/build/SandboxExec.js +0 -214
- package/build/eval.js +0 -205
- package/build/executor.d.ts +0 -124
- package/build/executor.js +0 -1554
- package/build/parser.js +0 -1527
- package/build/unraw.js +0 -168
- package/build/utils.d.ts +0 -264
- package/build/utils.js +0 -362
- package/dist/Sandbox.d.ts +0 -25
- package/dist/Sandbox.js +0 -270
- package/dist/Sandbox.js.map +0 -1
- package/dist/Sandbox.min.js +0 -2
- package/dist/Sandbox.min.js.map +0 -1
- package/dist/SandboxExec.js +0 -218
- package/dist/SandboxExec.js.map +0 -1
- package/dist/SandboxExec.min.js +0 -2
- package/dist/SandboxExec.min.js.map +0 -1
- package/dist/executor.d.ts +0 -124
- package/dist/executor.js +0 -1558
- package/dist/executor.js.map +0 -1
- package/dist/node/Sandbox.js +0 -277
- package/dist/node/SandboxExec.d.ts +0 -66
- package/dist/node/SandboxExec.js +0 -225
- package/dist/node/eval.d.ts +0 -27
- package/dist/node/executor.d.ts +0 -124
- package/dist/node/executor.js +0 -1575
- package/dist/node/parser.d.ts +0 -154
- package/dist/node/parser.js +0 -1704
- package/dist/node/utils.d.ts +0 -264
- package/dist/node/utils.js +0 -385
- package/dist/parser.js +0 -1690
- package/dist/parser.js.map +0 -1
- package/dist/unraw.d.ts +0 -11
- package/dist/utils.d.ts +0 -264
- package/dist/utils.js +0 -365
- package/dist/utils.js.map +0 -1
- /package/{build → dist/cjs/utils}/unraw.d.ts +0 -0
- /package/dist/{node → esm/utils}/unraw.d.ts +0 -0
|
@@ -0,0 +1,898 @@
|
|
|
1
|
+
import { SandboxAccessError, SandboxCapabilityError, SandboxError, SandboxExecutionQuotaExceededError, SandboxExecutionTreeError } from "../utils/errors.js";
|
|
2
|
+
import { AsyncGeneratorFunction, GeneratorFunction, LispType, NON_BLOCKING_THRESHOLD } from "../utils/types.js";
|
|
3
|
+
import { Prop, hasOwnProperty } from "../utils/Prop.js";
|
|
4
|
+
import { LocalScope, Scope, optional, sanitizeProp } from "../utils/Scope.js";
|
|
5
|
+
import { isLisp } from "../utils/ExecContext.js";
|
|
6
|
+
import "../utils/index.js";
|
|
7
|
+
import { ops } from "./opsRegistry.js";
|
|
8
|
+
import "./ops/index.js";
|
|
9
|
+
//#region src/executor/executorUtils.ts
|
|
10
|
+
var ExecReturn = class {
|
|
11
|
+
constructor(auditReport, result, returned, controlFlow) {
|
|
12
|
+
this.auditReport = auditReport;
|
|
13
|
+
this.result = result;
|
|
14
|
+
this.returned = returned;
|
|
15
|
+
this.controlFlow = controlFlow;
|
|
16
|
+
}
|
|
17
|
+
get breakLoop() {
|
|
18
|
+
return this.controlFlow?.type === "break";
|
|
19
|
+
}
|
|
20
|
+
get continueLoop() {
|
|
21
|
+
return this.controlFlow?.type === "continue";
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var emptyControlFlowTargets = [];
|
|
25
|
+
function normalizeStatementLabel(label) {
|
|
26
|
+
return label === void 0 || label === LispType.None ? void 0 : label;
|
|
27
|
+
}
|
|
28
|
+
function normalizeStatementLabels(label) {
|
|
29
|
+
if (label === void 0 || label === LispType.None) return [];
|
|
30
|
+
if (Array.isArray(label) && !isLisp(label)) return label.filter((item) => typeof item === "string");
|
|
31
|
+
return [label];
|
|
32
|
+
}
|
|
33
|
+
function createLoopTarget(label, acceptsUnlabeled = true) {
|
|
34
|
+
return {
|
|
35
|
+
label,
|
|
36
|
+
acceptsBreak: true,
|
|
37
|
+
acceptsContinue: true,
|
|
38
|
+
acceptsUnlabeledBreak: acceptsUnlabeled,
|
|
39
|
+
acceptsUnlabeledContinue: acceptsUnlabeled
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function createSwitchTarget(label) {
|
|
43
|
+
return {
|
|
44
|
+
label,
|
|
45
|
+
acceptsBreak: true,
|
|
46
|
+
acceptsContinue: false,
|
|
47
|
+
acceptsUnlabeledBreak: true,
|
|
48
|
+
acceptsUnlabeledContinue: false
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function createLabeledStatementTarget(label) {
|
|
52
|
+
if (!label) return void 0;
|
|
53
|
+
return {
|
|
54
|
+
label,
|
|
55
|
+
acceptsBreak: true,
|
|
56
|
+
acceptsContinue: false,
|
|
57
|
+
acceptsUnlabeledBreak: false,
|
|
58
|
+
acceptsUnlabeledContinue: false
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function addControlFlowTarget(controlFlowTargets, target) {
|
|
62
|
+
if (!target) return controlFlowTargets;
|
|
63
|
+
return [...controlFlowTargets || emptyControlFlowTargets, target];
|
|
64
|
+
}
|
|
65
|
+
function addControlFlowTargets(controlFlowTargets, targets) {
|
|
66
|
+
return targets.reduce((currentTargets, target) => addControlFlowTarget(currentTargets, target), controlFlowTargets);
|
|
67
|
+
}
|
|
68
|
+
function matchesControlFlowTarget(signal, target) {
|
|
69
|
+
if (signal.type === "continue") {
|
|
70
|
+
if (!target.acceptsContinue) return false;
|
|
71
|
+
return signal.label ? target.label === signal.label : target.acceptsUnlabeledContinue;
|
|
72
|
+
}
|
|
73
|
+
if (!target.acceptsBreak) return false;
|
|
74
|
+
return signal.label ? target.label === signal.label : target.acceptsUnlabeledBreak;
|
|
75
|
+
}
|
|
76
|
+
function findControlFlowTarget(controlFlowTargets, type, label) {
|
|
77
|
+
if (!controlFlowTargets) return void 0;
|
|
78
|
+
for (let i = controlFlowTargets.length - 1; i >= 0; i--) {
|
|
79
|
+
const target = controlFlowTargets[i];
|
|
80
|
+
if (label) {
|
|
81
|
+
if (target.label !== label) continue;
|
|
82
|
+
if (type === "continue" ? target.acceptsContinue : target.acceptsBreak) return target;
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
if (type === "continue" ? target.acceptsUnlabeledContinue : target.acceptsUnlabeledBreak) return target;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function generateArgs(argNames, args) {
|
|
89
|
+
const vars = {};
|
|
90
|
+
argNames.forEach((arg, i) => {
|
|
91
|
+
if (arg.startsWith("...")) vars[arg.substring(3)] = args.slice(i);
|
|
92
|
+
else vars[arg] = args[i];
|
|
93
|
+
});
|
|
94
|
+
return vars;
|
|
95
|
+
}
|
|
96
|
+
function createFunction(argNames, parsed, ticks, context, scope, name, internal = false) {
|
|
97
|
+
if (context.ctx.options.forbidFunctionCreation) throw new SandboxCapabilityError("Function creation is forbidden");
|
|
98
|
+
let func;
|
|
99
|
+
if (name === void 0) func = (...args) => {
|
|
100
|
+
const vars = generateArgs(argNames, args);
|
|
101
|
+
return executeTree(ticks, context, parsed, scope === void 0 ? [] : [new Scope(scope, vars)], void 0, internal).result;
|
|
102
|
+
};
|
|
103
|
+
else func = function sandboxedObject(...args) {
|
|
104
|
+
const vars = generateArgs(argNames, args);
|
|
105
|
+
return executeTree(ticks, context, parsed, scope === void 0 ? [] : [new Scope(scope, vars, this)], void 0, internal).result;
|
|
106
|
+
};
|
|
107
|
+
context.registerSandboxFunction(func);
|
|
108
|
+
context.ctx.sandboxedFunctions.add(func);
|
|
109
|
+
return func;
|
|
110
|
+
}
|
|
111
|
+
function createFunctionAsync(argNames, parsed, ticks, context, scope, name, internal = false) {
|
|
112
|
+
if (context.ctx.options.forbidFunctionCreation) throw new SandboxCapabilityError("Function creation is forbidden");
|
|
113
|
+
if (!context.ctx.prototypeWhitelist?.has(Promise.prototype)) throw new SandboxCapabilityError("Async/await not permitted");
|
|
114
|
+
let func;
|
|
115
|
+
if (name === void 0) func = async (...args) => {
|
|
116
|
+
const vars = generateArgs(argNames, args);
|
|
117
|
+
return (await executeTreeAsync(ticks, context, parsed, scope === void 0 ? [] : [new Scope(scope, vars)], void 0, internal)).result;
|
|
118
|
+
};
|
|
119
|
+
else func = async function sandboxedObject(...args) {
|
|
120
|
+
const vars = generateArgs(argNames, args);
|
|
121
|
+
return (await executeTreeAsync(ticks, context, parsed, scope === void 0 ? [] : [new Scope(scope, vars, this)], void 0, internal)).result;
|
|
122
|
+
};
|
|
123
|
+
context.registerSandboxFunction(func);
|
|
124
|
+
context.ctx.sandboxedFunctions.add(func);
|
|
125
|
+
return func;
|
|
126
|
+
}
|
|
127
|
+
var YieldValue = class {
|
|
128
|
+
constructor(value, delegate) {
|
|
129
|
+
this.value = value;
|
|
130
|
+
this.delegate = delegate;
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
var syncYieldPauseSentinel = Symbol("syncYieldPause");
|
|
134
|
+
function asIterableIterator(value) {
|
|
135
|
+
const iterator = value?.[Symbol.iterator]?.() ?? value;
|
|
136
|
+
if (!iterator || typeof iterator.next !== "function") throw new TypeError("yield* target is not iterable");
|
|
137
|
+
if (typeof iterator[Symbol.iterator] === "function") return iterator;
|
|
138
|
+
return {
|
|
139
|
+
next: iterator.next.bind(iterator),
|
|
140
|
+
throw: iterator.throw?.bind(iterator),
|
|
141
|
+
return: iterator.return?.bind(iterator),
|
|
142
|
+
[Symbol.iterator]() {
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function asAsyncIterableIterator(value) {
|
|
148
|
+
const asyncIterator = value?.[Symbol.asyncIterator]?.();
|
|
149
|
+
if (asyncIterator) return {
|
|
150
|
+
next: asyncIterator.next.bind(asyncIterator),
|
|
151
|
+
throw: asyncIterator.throw?.bind(asyncIterator),
|
|
152
|
+
return: asyncIterator.return?.bind(asyncIterator),
|
|
153
|
+
[Symbol.asyncIterator]() {
|
|
154
|
+
return this;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
const iterator = asIterableIterator(value);
|
|
158
|
+
return {
|
|
159
|
+
async next(nextValue) {
|
|
160
|
+
return iterator.next(nextValue);
|
|
161
|
+
},
|
|
162
|
+
async throw(err) {
|
|
163
|
+
if (typeof iterator.throw === "function") return iterator.throw(err);
|
|
164
|
+
throw err;
|
|
165
|
+
},
|
|
166
|
+
async return(valueToReturn) {
|
|
167
|
+
if (typeof iterator.return === "function") return iterator.return(valueToReturn);
|
|
168
|
+
return {
|
|
169
|
+
value: valueToReturn,
|
|
170
|
+
done: true
|
|
171
|
+
};
|
|
172
|
+
},
|
|
173
|
+
[Symbol.asyncIterator]() {
|
|
174
|
+
return this;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function* executeGenBody(ticks, tree, scope, context, statementLabels, internal) {
|
|
179
|
+
if (!isLisp(tree) && Array.isArray(tree)) {
|
|
180
|
+
const stmts = tree;
|
|
181
|
+
if (stmts.length === 0 || stmts[0] === LispType.None) return new ExecReturn(context.ctx.auditReport, void 0, false);
|
|
182
|
+
for (const stmt of stmts) {
|
|
183
|
+
const res = yield* executeGenBody(ticks, stmt, scope, context, statementLabels, internal);
|
|
184
|
+
if (res instanceof ExecReturn && (res.returned || res.controlFlow)) return res;
|
|
185
|
+
if (isLisp(stmt) && stmt[0] === LispType.Return) return new ExecReturn(context.ctx.auditReport, res.result, true);
|
|
186
|
+
}
|
|
187
|
+
return new ExecReturn(context.ctx.auditReport, void 0, false);
|
|
188
|
+
}
|
|
189
|
+
const [op, a, b] = tree;
|
|
190
|
+
switch (op) {
|
|
191
|
+
case LispType.Yield: {
|
|
192
|
+
const injected = yield sanitizeProp((yield* executeGenBody(ticks, a, scope, context, statementLabels, internal)).result, context);
|
|
193
|
+
return new ExecReturn(context.ctx.auditReport, injected, false);
|
|
194
|
+
}
|
|
195
|
+
case LispType.YieldDelegate: {
|
|
196
|
+
const result = yield* asIterableIterator(sanitizeProp((yield* executeGenBody(ticks, a, scope, context, statementLabels, internal)).result, context));
|
|
197
|
+
return new ExecReturn(context.ctx.auditReport, result, false);
|
|
198
|
+
}
|
|
199
|
+
case LispType.If: {
|
|
200
|
+
const condResult = yield* executeGenBody(ticks, a, scope, context, statementLabels, internal);
|
|
201
|
+
const ifCase = syncDone((d) => execSync(ticks, b, scope, context, d, statementLabels, internal, void 0)).result;
|
|
202
|
+
const branch = sanitizeProp(condResult.result, context) ? ifCase.t : ifCase.f;
|
|
203
|
+
if (branch) return yield* executeGenBody(ticks, branch, scope, context, statementLabels, internal);
|
|
204
|
+
return new ExecReturn(context.ctx.auditReport, void 0, false);
|
|
205
|
+
}
|
|
206
|
+
case LispType.Loop: {
|
|
207
|
+
const [checkFirst, startInternal, getIterator, startStep, step, condition, beforeStep, isForAwait, label] = a;
|
|
208
|
+
if (isForAwait === LispType.True) throw new SyntaxError("for-await-of loops are only allowed inside async functions");
|
|
209
|
+
const loopStatementTargets = [...normalizeStatementLabels(label).map((loopLabel) => createLoopTarget(loopLabel, false)), createLoopTarget()];
|
|
210
|
+
const loopTargets = addControlFlowTargets(statementLabels, loopStatementTargets);
|
|
211
|
+
const loopScope = new Scope(scope, {});
|
|
212
|
+
const internalVars = { $$obj: void 0 };
|
|
213
|
+
const interalScope = new Scope(loopScope, internalVars);
|
|
214
|
+
syncDone((d) => execSync(ticks, startStep, loopScope, context, d, void 0, internal, void 0));
|
|
215
|
+
internalVars["$$obj"] = syncDone((d) => execSync(ticks, getIterator, loopScope, context, d, void 0, internal, void 0)).result;
|
|
216
|
+
syncDone((d) => execSync(ticks, startInternal, interalScope, context, d, void 0, internal, void 0));
|
|
217
|
+
let loop = true;
|
|
218
|
+
if (checkFirst) loop = syncDone((d) => execSync(ticks, condition, interalScope, context, d, void 0, internal, void 0)).result;
|
|
219
|
+
while (loop) {
|
|
220
|
+
const iterScope = new Scope(interalScope, {});
|
|
221
|
+
syncDone((d) => execSync(ticks, beforeStep, iterScope, context, d, void 0, internal, void 0));
|
|
222
|
+
const res = yield* executeGenBody(ticks, b, iterScope, context, loopTargets, internal);
|
|
223
|
+
if (res.returned) return res;
|
|
224
|
+
if (res.controlFlow) {
|
|
225
|
+
if (!loopStatementTargets.some((t) => matchesControlFlowTarget(res.controlFlow, t))) return res;
|
|
226
|
+
if (res.breakLoop) break;
|
|
227
|
+
}
|
|
228
|
+
syncDone((d) => execSync(ticks, step, interalScope, context, d, void 0, internal, void 0));
|
|
229
|
+
loop = syncDone((d) => execSync(ticks, condition, interalScope, context, d, void 0, internal, void 0)).result;
|
|
230
|
+
}
|
|
231
|
+
return new ExecReturn(context.ctx.auditReport, void 0, false);
|
|
232
|
+
}
|
|
233
|
+
case LispType.Try: {
|
|
234
|
+
const [exception, catchBody, finallyBody] = b;
|
|
235
|
+
let result;
|
|
236
|
+
let finalOverride;
|
|
237
|
+
try {
|
|
238
|
+
result = yield* executeGenBody(ticks, a, scope, context, statementLabels, internal);
|
|
239
|
+
} catch (e) {
|
|
240
|
+
if (exception && catchBody?.length > 0) result = yield* executeGenBody(ticks, catchBody, new Scope(scope, { [exception]: e }), context, statementLabels, internal);
|
|
241
|
+
else throw e;
|
|
242
|
+
} finally {
|
|
243
|
+
if (finallyBody?.length > 0) {
|
|
244
|
+
const fr = yield* executeGenBody(ticks, finallyBody, scope, context, statementLabels, internal);
|
|
245
|
+
if (fr.returned || fr.controlFlow) finalOverride = fr;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (finalOverride) return finalOverride;
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
case LispType.Labeled: {
|
|
252
|
+
const target = createLabeledStatementTarget(normalizeStatementLabel(a));
|
|
253
|
+
const res = yield* executeGenBody(ticks, b, scope, context, addControlFlowTargets(statementLabels, target ? [target] : []), internal);
|
|
254
|
+
if (res.controlFlow && target && matchesControlFlowTarget(res.controlFlow, target)) return new ExecReturn(context.ctx.auditReport, res.result, false);
|
|
255
|
+
return res;
|
|
256
|
+
}
|
|
257
|
+
default: {
|
|
258
|
+
let completedYields = 0;
|
|
259
|
+
const yieldResults = [];
|
|
260
|
+
while (true) {
|
|
261
|
+
let currentYieldIdx = 0;
|
|
262
|
+
let capturedValue = void 0;
|
|
263
|
+
let capturedDelegate = false;
|
|
264
|
+
let yielded = false;
|
|
265
|
+
const captureYieldFn = (yv, continueDone) => {
|
|
266
|
+
if (currentYieldIdx < completedYields) {
|
|
267
|
+
continueDone(void 0, yieldResults[currentYieldIdx]);
|
|
268
|
+
currentYieldIdx++;
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
capturedValue = yv.value;
|
|
272
|
+
capturedDelegate = yv.delegate;
|
|
273
|
+
yielded = true;
|
|
274
|
+
currentYieldIdx++;
|
|
275
|
+
throw syncYieldPauseSentinel;
|
|
276
|
+
};
|
|
277
|
+
try {
|
|
278
|
+
const result = syncDone((d) => execSync(ticks, tree, scope, context, d, statementLabels, internal, captureYieldFn)).result;
|
|
279
|
+
if (result instanceof ExecReturn) return result;
|
|
280
|
+
return new ExecReturn(context.ctx.auditReport, result, false);
|
|
281
|
+
} catch (e) {
|
|
282
|
+
if (!yielded || e !== syncYieldPauseSentinel) throw e;
|
|
283
|
+
const resumedValue = capturedDelegate ? yield* asIterableIterator(capturedValue) : yield capturedValue;
|
|
284
|
+
yieldResults.push(resumedValue);
|
|
285
|
+
completedYields++;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function createGeneratorFunction(argNames, parsed, ticks, context, scope, name, internal = false) {
|
|
292
|
+
if (context.ctx.options.forbidFunctionCreation) throw new SandboxCapabilityError("Function creation is forbidden");
|
|
293
|
+
const makeGen = (thisArg, args) => {
|
|
294
|
+
const vars = generateArgs(argNames, args);
|
|
295
|
+
const executionGen = executeGenBody(ticks, parsed, scope === void 0 ? new Scope(null, vars, thisArg) : new Scope(scope, vars, thisArg), context, void 0, internal);
|
|
296
|
+
let isDone = false;
|
|
297
|
+
function drive(action) {
|
|
298
|
+
if (isDone) return {
|
|
299
|
+
value: void 0,
|
|
300
|
+
done: true
|
|
301
|
+
};
|
|
302
|
+
try {
|
|
303
|
+
const r = action();
|
|
304
|
+
if (r.done) {
|
|
305
|
+
isDone = true;
|
|
306
|
+
return {
|
|
307
|
+
value: r.value instanceof ExecReturn ? r.value.result : r.value,
|
|
308
|
+
done: true
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
return {
|
|
312
|
+
value: r.value,
|
|
313
|
+
done: false
|
|
314
|
+
};
|
|
315
|
+
} catch (e) {
|
|
316
|
+
isDone = true;
|
|
317
|
+
throw e;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return {
|
|
321
|
+
next(value) {
|
|
322
|
+
return drive(() => executionGen.next(value));
|
|
323
|
+
},
|
|
324
|
+
return(value) {
|
|
325
|
+
return drive(() => executionGen.return(value));
|
|
326
|
+
},
|
|
327
|
+
throw(err) {
|
|
328
|
+
return drive(() => executionGen.throw(err));
|
|
329
|
+
},
|
|
330
|
+
[Symbol.iterator]() {
|
|
331
|
+
return this;
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
};
|
|
335
|
+
const func = function sandboxedObject(...args) {
|
|
336
|
+
return makeGen(this, args);
|
|
337
|
+
};
|
|
338
|
+
Object.setPrototypeOf(func, GeneratorFunction.prototype);
|
|
339
|
+
context.registerSandboxFunction(func);
|
|
340
|
+
context.ctx.sandboxedFunctions.add(func);
|
|
341
|
+
return func;
|
|
342
|
+
}
|
|
343
|
+
function createAsyncGeneratorFunction(argNames, parsed, ticks, context, scope, name, internal = false) {
|
|
344
|
+
if (context.ctx.options.forbidFunctionCreation) throw new SandboxCapabilityError("Function creation is forbidden");
|
|
345
|
+
if (!context.ctx.prototypeWhitelist?.has(Promise.prototype)) throw new SandboxCapabilityError("Async/await not permitted");
|
|
346
|
+
const makeGen = (thisArg, args) => {
|
|
347
|
+
const vars = generateArgs(argNames, args);
|
|
348
|
+
const genScope = scope === void 0 ? [new Scope(null, vars, thisArg)] : [new Scope(scope, vars, thisArg)];
|
|
349
|
+
return (async function* sandboxedAsyncGenerator() {
|
|
350
|
+
const yieldQueue = [];
|
|
351
|
+
let resolveYield = null;
|
|
352
|
+
const yieldFn = (yv, continueDone) => {
|
|
353
|
+
yieldQueue.push({
|
|
354
|
+
yieldValue: yv,
|
|
355
|
+
continueDone
|
|
356
|
+
});
|
|
357
|
+
if (resolveYield) {
|
|
358
|
+
resolveYield();
|
|
359
|
+
resolveYield = null;
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
const bodyPromise = executeTreeAsync(ticks, context, parsed, genScope, void 0, internal, yieldFn);
|
|
363
|
+
let bodyDone = false;
|
|
364
|
+
let bodyResult;
|
|
365
|
+
let bodyError;
|
|
366
|
+
bodyPromise.then((r) => {
|
|
367
|
+
bodyDone = true;
|
|
368
|
+
bodyResult = r;
|
|
369
|
+
resolveYield?.();
|
|
370
|
+
}, (e) => {
|
|
371
|
+
bodyDone = true;
|
|
372
|
+
bodyError = e;
|
|
373
|
+
resolveYield?.();
|
|
374
|
+
});
|
|
375
|
+
while (true) {
|
|
376
|
+
if (yieldQueue.length === 0 && !bodyDone) await new Promise((res) => {
|
|
377
|
+
resolveYield = res;
|
|
378
|
+
});
|
|
379
|
+
while (yieldQueue.length > 0) {
|
|
380
|
+
const { yieldValue, continueDone } = yieldQueue.shift();
|
|
381
|
+
try {
|
|
382
|
+
const resumedValue = yieldValue.delegate ? yield* asAsyncIterableIterator(yieldValue.value) : yield yieldValue.value;
|
|
383
|
+
continueDone?.(void 0, resumedValue);
|
|
384
|
+
} catch (err) {
|
|
385
|
+
continueDone?.(err);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
if (bodyDone) break;
|
|
389
|
+
}
|
|
390
|
+
if (bodyError !== void 0) throw bodyError;
|
|
391
|
+
return bodyResult?.result;
|
|
392
|
+
})();
|
|
393
|
+
};
|
|
394
|
+
const func = function sandboxedObject(...args) {
|
|
395
|
+
return makeGen(this, args);
|
|
396
|
+
};
|
|
397
|
+
Object.setPrototypeOf(func, AsyncGeneratorFunction.prototype);
|
|
398
|
+
context.registerSandboxFunction(func);
|
|
399
|
+
context.ctx.sandboxedFunctions.add(func);
|
|
400
|
+
return func;
|
|
401
|
+
}
|
|
402
|
+
function assignCheck(obj, context, op = "assign") {
|
|
403
|
+
if (obj.context === void 0) throw new ReferenceError(`Cannot ${op} value to undefined.`);
|
|
404
|
+
if (obj.isConst) throw new TypeError(`Assignment to constant variable.`);
|
|
405
|
+
if (obj.isGlobal) throw new SandboxAccessError(`Cannot ${op} property '${obj.prop.toString()}' of a global object`);
|
|
406
|
+
if (obj.context === null) throw new TypeError("Cannot set properties of null");
|
|
407
|
+
if (typeof obj.context[obj.prop] === "function" && !hasOwnProperty(obj.context, obj.prop)) throw new SandboxAccessError(`Override prototype property '${obj.prop.toString()}' not allowed`);
|
|
408
|
+
if (op === "delete") {
|
|
409
|
+
if (hasOwnProperty(obj.context, obj.prop)) {
|
|
410
|
+
context.changeSubscriptions.get(obj.context)?.forEach((cb) => cb({
|
|
411
|
+
type: "delete",
|
|
412
|
+
prop: obj.prop.toString()
|
|
413
|
+
}));
|
|
414
|
+
context.changeSubscriptionsGlobal.get(obj.context)?.forEach((cb) => cb({
|
|
415
|
+
type: "delete",
|
|
416
|
+
prop: obj.prop.toString()
|
|
417
|
+
}));
|
|
418
|
+
}
|
|
419
|
+
} else if (hasOwnProperty(obj.context, obj.prop)) {
|
|
420
|
+
context.setSubscriptions.get(obj.context)?.get(obj.prop.toString())?.forEach((cb) => cb({ type: "replace" }));
|
|
421
|
+
context.setSubscriptionsGlobal.get(obj.context)?.get(obj.prop.toString())?.forEach((cb) => cb({ type: "replace" }));
|
|
422
|
+
} else {
|
|
423
|
+
context.changeSubscriptions.get(obj.context)?.forEach((cb) => cb({
|
|
424
|
+
type: "create",
|
|
425
|
+
prop: obj.prop.toString()
|
|
426
|
+
}));
|
|
427
|
+
context.changeSubscriptionsGlobal.get(obj.context)?.forEach((cb) => cb({
|
|
428
|
+
type: "create",
|
|
429
|
+
prop: obj.prop.toString()
|
|
430
|
+
}));
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
var arrayChange = new Set([
|
|
434
|
+
[].push,
|
|
435
|
+
[].pop,
|
|
436
|
+
[].shift,
|
|
437
|
+
[].unshift,
|
|
438
|
+
[].splice,
|
|
439
|
+
[].reverse,
|
|
440
|
+
[].sort,
|
|
441
|
+
[].copyWithin
|
|
442
|
+
]);
|
|
443
|
+
var KeyVal = class {
|
|
444
|
+
constructor(key, val) {
|
|
445
|
+
this.key = key;
|
|
446
|
+
this.val = val;
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
var SpreadObject = class {
|
|
450
|
+
constructor(item) {
|
|
451
|
+
this.item = item;
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
var SpreadArray = class {
|
|
455
|
+
constructor(item) {
|
|
456
|
+
this.item = item;
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
var ArrayHole = class {};
|
|
460
|
+
var If = class {
|
|
461
|
+
constructor(t, f, label) {
|
|
462
|
+
this.t = t;
|
|
463
|
+
this.f = f;
|
|
464
|
+
this.label = label;
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
var literalRegex = /(\$\$)*(\$)?\${(\d+)}/g;
|
|
468
|
+
var prorptyKeyTypes = [
|
|
469
|
+
"string",
|
|
470
|
+
"number",
|
|
471
|
+
"symbol"
|
|
472
|
+
];
|
|
473
|
+
function isPropertyKey(val) {
|
|
474
|
+
return prorptyKeyTypes.includes(typeof val);
|
|
475
|
+
}
|
|
476
|
+
function hasPossibleProperties(val) {
|
|
477
|
+
return val !== null && val !== void 0;
|
|
478
|
+
}
|
|
479
|
+
function execMany(ticks, exec, tree, done, scope, context, statementLabels, internal, generatorYield) {
|
|
480
|
+
if (exec === execSync) _execManySync(ticks, tree, done, scope, context, statementLabels, internal, generatorYield);
|
|
481
|
+
else _execManyAsync(ticks, tree, done, scope, context, statementLabels, internal, generatorYield).catch(done);
|
|
482
|
+
}
|
|
483
|
+
function _execManySync(ticks, tree, done, scope, context, statementLabels, internal, generatorYield) {
|
|
484
|
+
const ret = [];
|
|
485
|
+
for (let i = 0; i < tree.length; i++) {
|
|
486
|
+
let res = syncDone((d) => execSync(ticks, tree[i], scope, context, d, statementLabels, internal, generatorYield)).result;
|
|
487
|
+
if (res instanceof ExecReturn && (res.returned || res.breakLoop || res.continueLoop)) {
|
|
488
|
+
done(void 0, res);
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
if (isLisp(tree[i]) && tree[i][0] === LispType.Return) {
|
|
492
|
+
done(void 0, new ExecReturn(context.ctx.auditReport, res, true));
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
ret.push(res);
|
|
496
|
+
}
|
|
497
|
+
done(void 0, ret);
|
|
498
|
+
}
|
|
499
|
+
async function _execManyAsync(ticks, tree, done, scope, context, statementLabels, internal, generatorYield) {
|
|
500
|
+
const ret = [];
|
|
501
|
+
for (let i = 0; i < tree.length; i++) {
|
|
502
|
+
let res;
|
|
503
|
+
try {
|
|
504
|
+
let ad;
|
|
505
|
+
res = (ad = asyncDone((d) => execAsync(ticks, tree[i], scope, context, d, statementLabels, internal, generatorYield))).isInstant === true ? ad.instant : (await ad.p).result;
|
|
506
|
+
} catch (e) {
|
|
507
|
+
done(e);
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
if (res instanceof ExecReturn && (res.returned || res.breakLoop || res.continueLoop)) {
|
|
511
|
+
done(void 0, res);
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
if (isLisp(tree[i]) && tree[i][0] === LispType.Return) {
|
|
515
|
+
done(void 0, new ExecReturn(context.ctx.auditReport, res, true));
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
ret.push(res);
|
|
519
|
+
}
|
|
520
|
+
done(void 0, ret);
|
|
521
|
+
}
|
|
522
|
+
function asyncDone(callback) {
|
|
523
|
+
let isInstant = false;
|
|
524
|
+
let instant;
|
|
525
|
+
const p = new Promise((resolve, reject) => {
|
|
526
|
+
callback((...args) => {
|
|
527
|
+
if (args.length === 1) reject(args[0]);
|
|
528
|
+
else {
|
|
529
|
+
isInstant = true;
|
|
530
|
+
instant = args[1];
|
|
531
|
+
resolve({ result: args[1] });
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
return {
|
|
536
|
+
isInstant,
|
|
537
|
+
instant,
|
|
538
|
+
p
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
function syncDone(callback) {
|
|
542
|
+
let result;
|
|
543
|
+
let err;
|
|
544
|
+
callback((...args) => {
|
|
545
|
+
err = args.length === 1 ? { error: args[0] } : void 0;
|
|
546
|
+
result = args[1];
|
|
547
|
+
});
|
|
548
|
+
if (err) throw err.error;
|
|
549
|
+
return { result };
|
|
550
|
+
}
|
|
551
|
+
async function execAsync(ticks, tree, scope, context, doneOriginal, statementLabels, internal, generatorYield) {
|
|
552
|
+
let done = doneOriginal;
|
|
553
|
+
const p = new Promise((resolve) => {
|
|
554
|
+
done = (...args) => {
|
|
555
|
+
doneOriginal(...args);
|
|
556
|
+
resolve();
|
|
557
|
+
};
|
|
558
|
+
});
|
|
559
|
+
if (!_execNoneRecurse(ticks, tree, scope, context, done, true, statementLabels, internal, generatorYield) && isLisp(tree)) {
|
|
560
|
+
let op = tree[0];
|
|
561
|
+
let obj;
|
|
562
|
+
try {
|
|
563
|
+
let ad;
|
|
564
|
+
obj = (ad = asyncDone((d) => execAsync(ticks, tree[1], scope, context, d, statementLabels, internal, generatorYield))).isInstant === true ? ad.instant : (await ad.p).result;
|
|
565
|
+
} catch (e) {
|
|
566
|
+
done(e);
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
let a = obj;
|
|
570
|
+
try {
|
|
571
|
+
a = obj instanceof Prop ? obj.get(context) : obj;
|
|
572
|
+
} catch (e) {
|
|
573
|
+
done(e);
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
if (op === LispType.PropOptional || op === LispType.CallOptional) {
|
|
577
|
+
if (a === void 0 || a === null) {
|
|
578
|
+
done(void 0, optional);
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
op = op === LispType.PropOptional ? LispType.Prop : LispType.Call;
|
|
582
|
+
}
|
|
583
|
+
if (a === optional) if (op === LispType.Prop || op === LispType.Call) {
|
|
584
|
+
done(void 0, a);
|
|
585
|
+
return;
|
|
586
|
+
} else a = void 0;
|
|
587
|
+
if (op === LispType.NullishCoalescing && a !== void 0 && a !== null) {
|
|
588
|
+
done(void 0, a);
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
let bobj;
|
|
592
|
+
try {
|
|
593
|
+
let ad;
|
|
594
|
+
bobj = (ad = asyncDone((d) => execAsync(ticks, tree[2], scope, context, d, statementLabels, internal, generatorYield))).isInstant === true ? ad.instant : (await ad.p).result;
|
|
595
|
+
} catch (e) {
|
|
596
|
+
done(e);
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
let b = bobj;
|
|
600
|
+
try {
|
|
601
|
+
b = bobj instanceof Prop ? bobj.get(context) : bobj;
|
|
602
|
+
} catch (e) {
|
|
603
|
+
done(e);
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
if (b === optional) b = void 0;
|
|
607
|
+
performOp({
|
|
608
|
+
op,
|
|
609
|
+
exec: execAsync,
|
|
610
|
+
done,
|
|
611
|
+
ticks,
|
|
612
|
+
a,
|
|
613
|
+
b,
|
|
614
|
+
obj,
|
|
615
|
+
context,
|
|
616
|
+
scope,
|
|
617
|
+
bobj,
|
|
618
|
+
statementLabels,
|
|
619
|
+
internal,
|
|
620
|
+
generatorYield,
|
|
621
|
+
tree
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
await p;
|
|
625
|
+
}
|
|
626
|
+
function execSync(ticks, tree, scope, context, done, statementLabels, internal, generatorYield) {
|
|
627
|
+
if (!_execNoneRecurse(ticks, tree, scope, context, done, false, statementLabels, internal, generatorYield) && isLisp(tree)) {
|
|
628
|
+
let op = tree[0];
|
|
629
|
+
let obj = syncDone((d) => execSync(ticks, tree[1], scope, context, d, statementLabels, internal, generatorYield)).result;
|
|
630
|
+
let a = obj instanceof Prop ? obj.get(context) : obj;
|
|
631
|
+
if (op === LispType.PropOptional || op === LispType.CallOptional) {
|
|
632
|
+
if (a === void 0 || a === null) {
|
|
633
|
+
done(void 0, optional);
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
op = op === LispType.PropOptional ? LispType.Prop : LispType.Call;
|
|
637
|
+
}
|
|
638
|
+
if (a === optional) if (op === LispType.Prop || op === LispType.Call) {
|
|
639
|
+
done(void 0, a);
|
|
640
|
+
return;
|
|
641
|
+
} else a = void 0;
|
|
642
|
+
if (op === LispType.NullishCoalescing && a !== void 0 && a !== null) {
|
|
643
|
+
done(void 0, a);
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
646
|
+
let bobj = syncDone((d) => execSync(ticks, tree[2], scope, context, d, statementLabels, internal, generatorYield)).result;
|
|
647
|
+
let b = bobj instanceof Prop ? bobj.get(context) : bobj;
|
|
648
|
+
if (b === optional) b = void 0;
|
|
649
|
+
performOp({
|
|
650
|
+
op,
|
|
651
|
+
exec: execSync,
|
|
652
|
+
done,
|
|
653
|
+
ticks,
|
|
654
|
+
a,
|
|
655
|
+
b,
|
|
656
|
+
obj,
|
|
657
|
+
context,
|
|
658
|
+
scope,
|
|
659
|
+
bobj,
|
|
660
|
+
statementLabels,
|
|
661
|
+
internal,
|
|
662
|
+
generatorYield,
|
|
663
|
+
tree
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
function checkHaltExpectedTicks(params, expectTicks = 0n) {
|
|
668
|
+
const sandbox = params.context.ctx.sandbox;
|
|
669
|
+
const { ticks, scope, context } = params;
|
|
670
|
+
if (sandbox.halted) {
|
|
671
|
+
const sub = sandbox.subscribeResume(() => {
|
|
672
|
+
sub.unsubscribe();
|
|
673
|
+
performOp(params, false);
|
|
674
|
+
});
|
|
675
|
+
return true;
|
|
676
|
+
} else if (ticks.tickLimit !== void 0 && ticks.tickLimit <= ticks.ticks + expectTicks) {
|
|
677
|
+
const error = new SandboxExecutionQuotaExceededError("Execution quota exceeded");
|
|
678
|
+
if (context.ctx.options.haltOnSandboxError) {
|
|
679
|
+
const sub = sandbox.subscribeResume(() => {
|
|
680
|
+
sub.unsubscribe();
|
|
681
|
+
performOp(params);
|
|
682
|
+
});
|
|
683
|
+
sandbox.haltExecution({
|
|
684
|
+
type: "error",
|
|
685
|
+
error,
|
|
686
|
+
ticks,
|
|
687
|
+
scope,
|
|
688
|
+
context
|
|
689
|
+
});
|
|
690
|
+
} else params.done(error);
|
|
691
|
+
return true;
|
|
692
|
+
} else if (ticks.nextYield && ticks.ticks > ticks.nextYield) {
|
|
693
|
+
const sub = sandbox.subscribeResume(() => {
|
|
694
|
+
sub.unsubscribe();
|
|
695
|
+
performOp(params, false);
|
|
696
|
+
});
|
|
697
|
+
ticks.nextYield += NON_BLOCKING_THRESHOLD;
|
|
698
|
+
sandbox.haltExecution({ type: "yield" });
|
|
699
|
+
setTimeout(() => sandbox.resumeExecution());
|
|
700
|
+
return true;
|
|
701
|
+
}
|
|
702
|
+
ticks.ticks += expectTicks;
|
|
703
|
+
return false;
|
|
704
|
+
}
|
|
705
|
+
function performOp(params, count = true) {
|
|
706
|
+
const { done, op, ticks, context, scope } = params;
|
|
707
|
+
if (count) ticks.ticks++;
|
|
708
|
+
const sandbox = context.ctx.sandbox;
|
|
709
|
+
try {
|
|
710
|
+
if (checkHaltExpectedTicks(params)) return;
|
|
711
|
+
const o = ops.get(op);
|
|
712
|
+
if (o === void 0) {
|
|
713
|
+
done(new SandboxExecutionTreeError("Unknown operator: " + op));
|
|
714
|
+
return;
|
|
715
|
+
}
|
|
716
|
+
o(params);
|
|
717
|
+
} catch (err) {
|
|
718
|
+
if (context.ctx.options.haltOnSandboxError && err instanceof SandboxError) {
|
|
719
|
+
const sub = sandbox.subscribeResume(() => {
|
|
720
|
+
sub.unsubscribe();
|
|
721
|
+
done(err);
|
|
722
|
+
});
|
|
723
|
+
sandbox.haltExecution({
|
|
724
|
+
type: "error",
|
|
725
|
+
error: err,
|
|
726
|
+
ticks,
|
|
727
|
+
scope,
|
|
728
|
+
context
|
|
729
|
+
});
|
|
730
|
+
} else done(err);
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
var unexecTypes = new Set([
|
|
734
|
+
LispType.ArrowFunction,
|
|
735
|
+
LispType.Function,
|
|
736
|
+
LispType.InlineFunction,
|
|
737
|
+
LispType.Loop,
|
|
738
|
+
LispType.Try,
|
|
739
|
+
LispType.Switch,
|
|
740
|
+
LispType.IfCase,
|
|
741
|
+
LispType.InlineIfCase,
|
|
742
|
+
LispType.Labeled,
|
|
743
|
+
LispType.Typeof
|
|
744
|
+
]);
|
|
745
|
+
function _execNoneRecurse(ticks, tree, scope, context, done, isAsync, statementLabels, internal, generatorYield) {
|
|
746
|
+
const exec = isAsync ? execAsync : execSync;
|
|
747
|
+
if (tree instanceof Prop) done(void 0, tree.get(context));
|
|
748
|
+
else if (tree === optional) done();
|
|
749
|
+
else if (Array.isArray(tree) && !isLisp(tree)) if (tree[0] === LispType.None) done();
|
|
750
|
+
else execMany(ticks, exec, tree, done, scope, context, statementLabels, internal, generatorYield);
|
|
751
|
+
else if (!isLisp(tree)) done(void 0, tree);
|
|
752
|
+
else if (tree[0] === LispType.Block) execMany(ticks, exec, tree[1], done, new Scope(scope), context, statementLabels, internal, generatorYield);
|
|
753
|
+
else if (tree[0] === LispType.InternalBlock) execMany(ticks, exec, tree[1], done, scope, context, statementLabels, true, generatorYield);
|
|
754
|
+
else if (tree[0] === LispType.Await) if (!isAsync) done(/* @__PURE__ */ new SyntaxError("Illegal use of 'await', must be inside async function"));
|
|
755
|
+
else if (context.ctx.prototypeWhitelist?.has(Promise.prototype)) execAsync(ticks, tree[1], scope, context, async (...args) => {
|
|
756
|
+
if (args.length === 1) done(args[0]);
|
|
757
|
+
else try {
|
|
758
|
+
done(void 0, await sanitizeProp(args[1], context));
|
|
759
|
+
} catch (err) {
|
|
760
|
+
done(err);
|
|
761
|
+
}
|
|
762
|
+
}, statementLabels, internal, generatorYield).catch(done);
|
|
763
|
+
else done(new SandboxCapabilityError("Async/await is not permitted"));
|
|
764
|
+
else if (tree[0] === LispType.Yield || tree[0] === LispType.YieldDelegate) {
|
|
765
|
+
const yieldFn = generatorYield;
|
|
766
|
+
if (!yieldFn) {
|
|
767
|
+
done(/* @__PURE__ */ new SyntaxError("Illegal use of 'yield', must be inside a generator function"));
|
|
768
|
+
return true;
|
|
769
|
+
}
|
|
770
|
+
const isDelegate = tree[0] === LispType.YieldDelegate;
|
|
771
|
+
if (isAsync) execAsync(ticks, tree[1], scope, context, async (...args) => {
|
|
772
|
+
if (args.length === 1) {
|
|
773
|
+
done(args[0]);
|
|
774
|
+
return;
|
|
775
|
+
}
|
|
776
|
+
try {
|
|
777
|
+
yieldFn(new YieldValue(await sanitizeProp(args[1], context), isDelegate), done);
|
|
778
|
+
} catch (err) {
|
|
779
|
+
done(err);
|
|
780
|
+
}
|
|
781
|
+
}, statementLabels, internal, generatorYield).catch(done);
|
|
782
|
+
else try {
|
|
783
|
+
const val = syncDone((d) => execSync(ticks, tree[1], scope, context, d, statementLabels, internal, generatorYield)).result;
|
|
784
|
+
yieldFn(new YieldValue(sanitizeProp(val, context), isDelegate), done);
|
|
785
|
+
} catch (err) {
|
|
786
|
+
if (err === syncYieldPauseSentinel) throw err;
|
|
787
|
+
done(err);
|
|
788
|
+
}
|
|
789
|
+
} else if (unexecTypes.has(tree[0])) performOp({
|
|
790
|
+
op: tree[0],
|
|
791
|
+
exec,
|
|
792
|
+
done,
|
|
793
|
+
ticks,
|
|
794
|
+
a: tree[1],
|
|
795
|
+
b: tree[2],
|
|
796
|
+
obj: tree,
|
|
797
|
+
tree,
|
|
798
|
+
context,
|
|
799
|
+
scope,
|
|
800
|
+
bobj: void 0,
|
|
801
|
+
statementLabels,
|
|
802
|
+
internal,
|
|
803
|
+
generatorYield
|
|
804
|
+
});
|
|
805
|
+
else return false;
|
|
806
|
+
return true;
|
|
807
|
+
}
|
|
808
|
+
function executeTree(ticks, context, executionTree, scopes = [], statementLabels, internal, generatorYield) {
|
|
809
|
+
return syncDone((done) => executeTreeWithDone(execSync, done, ticks, context, executionTree, scopes, statementLabels, internal, generatorYield)).result;
|
|
810
|
+
}
|
|
811
|
+
async function executeTreeAsync(ticks, context, executionTree, scopes = [], statementLabels, internal, generatorYield) {
|
|
812
|
+
let ad;
|
|
813
|
+
return (ad = asyncDone((done) => executeTreeWithDone(execAsync, done, ticks, context, executionTree, scopes, statementLabels, internal, generatorYield))).isInstant === true ? ad.instant : (await ad.p).result;
|
|
814
|
+
}
|
|
815
|
+
function executeTreeWithDone(exec, done, ticks, context, executionTree, scopes = [], statementLabels, internal, generatorYield) {
|
|
816
|
+
if (!executionTree) {
|
|
817
|
+
done();
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
if (!(executionTree instanceof Array)) throw new SyntaxError("Bad execution tree");
|
|
821
|
+
let scope = context.ctx.globalScope;
|
|
822
|
+
let s;
|
|
823
|
+
while (s = scopes.shift()) {
|
|
824
|
+
if (typeof s !== "object") continue;
|
|
825
|
+
if (s instanceof Scope) scope = s;
|
|
826
|
+
else scope = new Scope(scope, s, s instanceof LocalScope ? void 0 : null);
|
|
827
|
+
}
|
|
828
|
+
if (context.ctx.options.audit && !context.ctx.auditReport) context.ctx.auditReport = {
|
|
829
|
+
globalsAccess: /* @__PURE__ */ new Set(),
|
|
830
|
+
prototypeAccess: {}
|
|
831
|
+
};
|
|
832
|
+
if (exec === execSync) _executeWithDoneSync(done, ticks, context, executionTree, scope, statementLabels, internal, generatorYield);
|
|
833
|
+
else _executeWithDoneAsync(done, ticks, context, executionTree, scope, statementLabels, internal, generatorYield).catch(done);
|
|
834
|
+
}
|
|
835
|
+
function _executeWithDoneSync(done, ticks, context, executionTree, scope, statementLabels, internal, generatorYield) {
|
|
836
|
+
if (!(executionTree instanceof Array)) throw new SyntaxError("Bad execution tree");
|
|
837
|
+
let i = 0;
|
|
838
|
+
for (i = 0; i < executionTree.length; i++) {
|
|
839
|
+
let res;
|
|
840
|
+
let err;
|
|
841
|
+
const current = executionTree[i];
|
|
842
|
+
try {
|
|
843
|
+
execSync(ticks, current, scope, context, (...args) => {
|
|
844
|
+
if (args.length === 1) err = { error: args[0] };
|
|
845
|
+
else res = args[1];
|
|
846
|
+
}, statementLabels, internal, generatorYield);
|
|
847
|
+
} catch (e) {
|
|
848
|
+
err = { error: e };
|
|
849
|
+
}
|
|
850
|
+
if (err) {
|
|
851
|
+
done(err.error);
|
|
852
|
+
return;
|
|
853
|
+
}
|
|
854
|
+
if (res instanceof ExecReturn) {
|
|
855
|
+
done(void 0, res);
|
|
856
|
+
return;
|
|
857
|
+
}
|
|
858
|
+
if (isLisp(current) && current[0] === LispType.Return) {
|
|
859
|
+
done(void 0, new ExecReturn(context.ctx.auditReport, res, true));
|
|
860
|
+
return;
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
done(void 0, new ExecReturn(context.ctx.auditReport, void 0, false));
|
|
864
|
+
}
|
|
865
|
+
async function _executeWithDoneAsync(done, ticks, context, executionTree, scope, statementLabels, internal, generatorYield) {
|
|
866
|
+
if (!(executionTree instanceof Array)) throw new SyntaxError("Bad execution tree");
|
|
867
|
+
let i = 0;
|
|
868
|
+
for (i = 0; i < executionTree.length; i++) {
|
|
869
|
+
let res;
|
|
870
|
+
let err;
|
|
871
|
+
const current = executionTree[i];
|
|
872
|
+
try {
|
|
873
|
+
await execAsync(ticks, current, scope, context, (...args) => {
|
|
874
|
+
if (args.length === 1) err = { error: args[0] };
|
|
875
|
+
else res = args[1];
|
|
876
|
+
}, statementLabels, internal, generatorYield);
|
|
877
|
+
} catch (e) {
|
|
878
|
+
err = { error: e };
|
|
879
|
+
}
|
|
880
|
+
if (err) {
|
|
881
|
+
done(err.error);
|
|
882
|
+
return;
|
|
883
|
+
}
|
|
884
|
+
if (res instanceof ExecReturn) {
|
|
885
|
+
done(void 0, res);
|
|
886
|
+
return;
|
|
887
|
+
}
|
|
888
|
+
if (isLisp(current) && current[0] === LispType.Return) {
|
|
889
|
+
done(void 0, new ExecReturn(context.ctx.auditReport, res, true));
|
|
890
|
+
return;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
done(void 0, new ExecReturn(context.ctx.auditReport, void 0, false));
|
|
894
|
+
}
|
|
895
|
+
//#endregion
|
|
896
|
+
export { ArrayHole, ExecReturn, If, KeyVal, SpreadArray, SpreadObject, YieldValue, addControlFlowTarget, addControlFlowTargets, arrayChange, assignCheck, asyncDone, checkHaltExpectedTicks, createAsyncGeneratorFunction, createFunction, createFunctionAsync, createGeneratorFunction, createLabeledStatementTarget, createLoopTarget, createSwitchTarget, execAsync, execMany, execSync, executeTree, executeTreeAsync, executeTreeWithDone, findControlFlowTarget, hasPossibleProperties, isPropertyKey, literalRegex, matchesControlFlowTarget, normalizeStatementLabel, normalizeStatementLabels, prorptyKeyTypes, syncDone };
|
|
897
|
+
|
|
898
|
+
//# sourceMappingURL=executorUtils.js.map
|