@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,25 @@
|
|
|
1
|
+
const require_errors = require("../../utils/errors.js");
|
|
2
|
+
const require_types = require("../../utils/types.js");
|
|
3
|
+
require("../../utils/index.js");
|
|
4
|
+
const require_opsRegistry = require("../opsRegistry.js");
|
|
5
|
+
const require_executorUtils = require("../executorUtils.js");
|
|
6
|
+
//#region src/executor/ops/misc.ts
|
|
7
|
+
require_opsRegistry.addOps(require_types.LispType.Internal, ({ done, a, b, scope, bobj, internal }) => {
|
|
8
|
+
if (!internal) throw new require_errors.SandboxCapabilityError("Internal variables are not accessible");
|
|
9
|
+
done(void 0, scope.declare(a, require_types.VarType.internal, b, bobj?.isGlobal || false, internal));
|
|
10
|
+
});
|
|
11
|
+
require_opsRegistry.addOps(require_types.LispType.LoopAction, ({ done, a, b, context, statementLabels }) => {
|
|
12
|
+
const label = require_executorUtils.normalizeStatementLabel(b);
|
|
13
|
+
const target = require_executorUtils.findControlFlowTarget(statementLabels, a, label);
|
|
14
|
+
if (target === null) throw new TypeError("Illegal continue statement");
|
|
15
|
+
if (!target) throw new TypeError(label ? `Undefined label '${label}'` : "Illegal " + a + " statement");
|
|
16
|
+
done(void 0, new require_executorUtils.ExecReturn(context.ctx.auditReport, void 0, false, {
|
|
17
|
+
type: a,
|
|
18
|
+
label
|
|
19
|
+
}));
|
|
20
|
+
});
|
|
21
|
+
require_opsRegistry.addOps(require_types.LispType.Throw, ({ done, b }) => {
|
|
22
|
+
done(b);
|
|
23
|
+
});
|
|
24
|
+
require_opsRegistry.addOps(require_types.LispType.None, ({ done }) => done());
|
|
25
|
+
//#endregion
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const require_types = require("../../utils/types.js");
|
|
2
|
+
const require_Scope = require("../../utils/Scope.js");
|
|
3
|
+
require("../../utils/index.js");
|
|
4
|
+
const require_opsRegistry = require("../opsRegistry.js");
|
|
5
|
+
const require_executorUtils = require("../executorUtils.js");
|
|
6
|
+
//#region src/executor/ops/object.ts
|
|
7
|
+
require_opsRegistry.addOps(require_types.LispType.CreateObject, (params) => {
|
|
8
|
+
const { done, b } = params;
|
|
9
|
+
let res = {};
|
|
10
|
+
for (const item of b) if (item.key instanceof require_executorUtils.SpreadObject) {
|
|
11
|
+
const keys = Object.keys(item.key.item);
|
|
12
|
+
if (require_executorUtils.checkHaltExpectedTicks(params, BigInt(keys.length))) return;
|
|
13
|
+
res = {
|
|
14
|
+
...res,
|
|
15
|
+
...item.key.item
|
|
16
|
+
};
|
|
17
|
+
} else res[item.key] = item.val;
|
|
18
|
+
done(void 0, res);
|
|
19
|
+
});
|
|
20
|
+
require_opsRegistry.addOps(require_types.LispType.KeyVal, ({ done, a, b }) => done(void 0, new require_executorUtils.KeyVal(a, b)));
|
|
21
|
+
require_opsRegistry.addOps(require_types.LispType.CreateArray, (params) => {
|
|
22
|
+
const { done, b, context } = params;
|
|
23
|
+
const items = [];
|
|
24
|
+
for (const item of b) if (item instanceof require_executorUtils.SpreadArray) {
|
|
25
|
+
const expanded = Array.isArray(item.item) ? item.item : [...item.item];
|
|
26
|
+
if (require_executorUtils.checkHaltExpectedTicks(params, BigInt(expanded.length))) return;
|
|
27
|
+
for (const v of expanded) items.push(require_Scope.sanitizeProp(v, context));
|
|
28
|
+
} else if (item instanceof require_executorUtils.ArrayHole) items.length++;
|
|
29
|
+
else items.push(require_Scope.sanitizeProp(item, context));
|
|
30
|
+
done(void 0, items);
|
|
31
|
+
});
|
|
32
|
+
require_opsRegistry.addOps(require_types.LispType.Hole, ({ done }) => done(void 0, new require_executorUtils.ArrayHole()));
|
|
33
|
+
require_opsRegistry.addOps(require_types.LispType.Group, ({ done, b }) => done(void 0, b));
|
|
34
|
+
require_opsRegistry.addOps(require_types.LispType.GlobalSymbol, ({ done, b }) => {
|
|
35
|
+
switch (b) {
|
|
36
|
+
case "true": return done(void 0, true);
|
|
37
|
+
case "false": return done(void 0, false);
|
|
38
|
+
case "null": return done(void 0, null);
|
|
39
|
+
case "undefined": return done(void 0, void 0);
|
|
40
|
+
case "NaN": return done(void 0, NaN);
|
|
41
|
+
case "Infinity": return done(void 0, Infinity);
|
|
42
|
+
}
|
|
43
|
+
done(/* @__PURE__ */ new Error("Unknown symbol: " + b));
|
|
44
|
+
});
|
|
45
|
+
require_opsRegistry.addOps(require_types.LispType.SpreadArray, ({ done, b }) => {
|
|
46
|
+
done(void 0, new require_executorUtils.SpreadArray(b));
|
|
47
|
+
});
|
|
48
|
+
require_opsRegistry.addOps(require_types.LispType.SpreadObject, ({ done, b }) => {
|
|
49
|
+
done(void 0, new require_executorUtils.SpreadObject(b));
|
|
50
|
+
});
|
|
51
|
+
//#endregion
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
const require_errors = require("../../utils/errors.js");
|
|
2
|
+
const require_types = require("../../utils/types.js");
|
|
3
|
+
const require_Prop = require("../../utils/Prop.js");
|
|
4
|
+
require("../../utils/index.js");
|
|
5
|
+
const require_opsRegistry = require("../opsRegistry.js");
|
|
6
|
+
const require_executorUtils = require("../executorUtils.js");
|
|
7
|
+
//#region src/executor/ops/prop.ts
|
|
8
|
+
require_opsRegistry.addOps(require_types.LispType.Prop, ({ done, a, b, obj, context, scope, internal }) => {
|
|
9
|
+
if (a === null) throw new TypeError(`Cannot read properties of null (reading '${b?.toString()}')`);
|
|
10
|
+
if (!require_executorUtils.isPropertyKey(b)) b = `${b}`;
|
|
11
|
+
if (a === void 0 && obj === void 0 && typeof b === "string") {
|
|
12
|
+
const prop = scope.get(b, internal);
|
|
13
|
+
if (prop.context === void 0) throw new ReferenceError(`${b} is not defined`);
|
|
14
|
+
if (prop.context === context.ctx.sandboxGlobal) {
|
|
15
|
+
if (context.ctx.options.audit) context.ctx.auditReport?.globalsAccess.add(b);
|
|
16
|
+
}
|
|
17
|
+
const val = prop.context[prop.prop];
|
|
18
|
+
done(void 0, require_Prop.resolveSandboxProp(val, context, prop) || prop);
|
|
19
|
+
return;
|
|
20
|
+
} else if (a === void 0) throw new TypeError(`Cannot read properties of undefined (reading '${b.toString()}')`);
|
|
21
|
+
if (!require_executorUtils.hasPossibleProperties(a)) {
|
|
22
|
+
done(void 0, new require_Prop.Prop(void 0, b));
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const prototypeAccess = typeof a === "function" || !require_Prop.hasOwnProperty(a, b);
|
|
26
|
+
if (context.ctx.options.audit && prototypeAccess) {
|
|
27
|
+
let prot = Object.getPrototypeOf(a);
|
|
28
|
+
do
|
|
29
|
+
if (require_Prop.hasOwnProperty(prot, b)) {
|
|
30
|
+
if (context.ctx.auditReport && !context.ctx.auditReport.prototypeAccess[prot.constructor.name]) context.ctx.auditReport.prototypeAccess[prot.constructor.name] = /* @__PURE__ */ new Set();
|
|
31
|
+
context.ctx.auditReport?.prototypeAccess[prot.constructor.name].add(b);
|
|
32
|
+
}
|
|
33
|
+
while (prot = Object.getPrototypeOf(prot));
|
|
34
|
+
}
|
|
35
|
+
if (prototypeAccess) {
|
|
36
|
+
if (typeof a === "function") {
|
|
37
|
+
if (require_Prop.hasOwnProperty(a, b)) {
|
|
38
|
+
const whitelist = context.ctx.prototypeWhitelist.get(a.prototype);
|
|
39
|
+
if (!(whitelist && (!whitelist.size || whitelist.has(b))) && !context.ctx.sandboxedFunctions.has(a)) throw new require_errors.SandboxAccessError(`Static method or property access not permitted: ${a.name}.${b.toString()}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
let prot = a;
|
|
43
|
+
while (prot = Object.getPrototypeOf(prot)) if (require_Prop.hasOwnProperty(prot, b) || b === "__proto__") {
|
|
44
|
+
const whitelist = context.ctx.prototypeWhitelist.get(prot);
|
|
45
|
+
if (whitelist && (!whitelist.size || whitelist.has(b)) || context.ctx.sandboxedFunctions.has(prot.constructor)) break;
|
|
46
|
+
if (b === "__proto__") throw new require_errors.SandboxAccessError(`Access to prototype of global object is not permitted`);
|
|
47
|
+
throw new require_errors.SandboxAccessError(`Method or property access not permitted: ${prot.constructor.name}.${b.toString()}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (typeof a === "function") {
|
|
51
|
+
if (b === "prototype" && !context.ctx.sandboxedFunctions.has(a)) throw new require_errors.SandboxAccessError(`Access to prototype of global object is not permitted`);
|
|
52
|
+
if ([
|
|
53
|
+
"caller",
|
|
54
|
+
"callee",
|
|
55
|
+
"arguments"
|
|
56
|
+
].includes(b)) throw new require_errors.SandboxAccessError(`Access to '${b}' property is not permitted`);
|
|
57
|
+
}
|
|
58
|
+
const val = a[b];
|
|
59
|
+
if (b === "__proto__" && !context.ctx.sandboxedFunctions.has(val?.constructor)) throw new require_errors.SandboxAccessError(`Access to prototype of global object is not permitted`);
|
|
60
|
+
const p = require_Prop.resolveSandboxProp(val, context, new require_Prop.Prop(a, b, false, false));
|
|
61
|
+
if (p) {
|
|
62
|
+
done(void 0, p);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const isSandboxGlobal = a === context.ctx.sandboxGlobal;
|
|
66
|
+
const g = !isSandboxGlobal && obj instanceof require_Prop.Prop && obj.isGlobal || typeof a === "function" && !context.ctx.sandboxedFunctions.has(a) || context.ctx.globalsWhitelist.has(a) || isSandboxGlobal && typeof b === "string" && require_Prop.hasOwnProperty(context.ctx.globalScope.globals, b);
|
|
67
|
+
done(void 0, new require_Prop.Prop(a, b, false, g, false));
|
|
68
|
+
});
|
|
69
|
+
require_opsRegistry.addOps(require_types.LispType.StringIndex, ({ done, b, context }) => done(void 0, context.constants.strings[parseInt(b)]));
|
|
70
|
+
//#endregion
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const require_types = require("../../utils/types.js");
|
|
2
|
+
const require_Prop = require("../../utils/Prop.js");
|
|
3
|
+
const require_Scope = require("../../utils/Scope.js");
|
|
4
|
+
require("../../utils/index.js");
|
|
5
|
+
const require_opsRegistry = require("../opsRegistry.js");
|
|
6
|
+
const require_executorUtils = require("../executorUtils.js");
|
|
7
|
+
//#region src/executor/ops/unary.ts
|
|
8
|
+
require_opsRegistry.addOps(require_types.LispType.Not, ({ done, b }) => done(void 0, !b));
|
|
9
|
+
require_opsRegistry.addOps(require_types.LispType.Inverse, ({ done, b }) => done(void 0, ~b));
|
|
10
|
+
require_opsRegistry.addOps(require_types.LispType.IncrementBefore, ({ done, obj, context }) => {
|
|
11
|
+
require_executorUtils.assignCheck(obj, context);
|
|
12
|
+
done(void 0, ++obj.context[obj.prop]);
|
|
13
|
+
});
|
|
14
|
+
require_opsRegistry.addOps(require_types.LispType.IncrementAfter, ({ done, obj, context }) => {
|
|
15
|
+
require_executorUtils.assignCheck(obj, context);
|
|
16
|
+
done(void 0, obj.context[obj.prop]++);
|
|
17
|
+
});
|
|
18
|
+
require_opsRegistry.addOps(require_types.LispType.DecrementBefore, ({ done, obj, context }) => {
|
|
19
|
+
require_executorUtils.assignCheck(obj, context);
|
|
20
|
+
done(void 0, --obj.context[obj.prop]);
|
|
21
|
+
});
|
|
22
|
+
require_opsRegistry.addOps(require_types.LispType.DecrementAfter, ({ done, obj, context }) => {
|
|
23
|
+
require_executorUtils.assignCheck(obj, context);
|
|
24
|
+
done(void 0, obj.context[obj.prop]--);
|
|
25
|
+
});
|
|
26
|
+
require_opsRegistry.addOps(require_types.LispType.Positive, ({ done, b }) => done(void 0, +b));
|
|
27
|
+
require_opsRegistry.addOps(require_types.LispType.Negative, ({ done, b }) => done(void 0, -b));
|
|
28
|
+
require_opsRegistry.addOps(require_types.LispType.Typeof, ({ exec, done, ticks, b, context, scope, internal, generatorYield }) => {
|
|
29
|
+
exec(ticks, b, scope, context, (e, prop) => {
|
|
30
|
+
done(void 0, typeof require_Scope.sanitizeProp(prop, context));
|
|
31
|
+
}, void 0, internal, generatorYield);
|
|
32
|
+
});
|
|
33
|
+
require_opsRegistry.addOps(require_types.LispType.Delete, ({ done, context, bobj }) => {
|
|
34
|
+
if (!(bobj instanceof require_Prop.Prop)) {
|
|
35
|
+
done(void 0, true);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
require_executorUtils.assignCheck(bobj, context, "delete");
|
|
39
|
+
if (bobj.isVariable) {
|
|
40
|
+
done(void 0, false);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
done(void 0, delete bobj.context?.[bobj.prop]);
|
|
44
|
+
});
|
|
45
|
+
require_opsRegistry.addOps(require_types.LispType.Void, ({ done }) => {
|
|
46
|
+
done();
|
|
47
|
+
});
|
|
48
|
+
//#endregion
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const require_types = require("../../utils/types.js");
|
|
2
|
+
require("../../utils/index.js");
|
|
3
|
+
const require_opsRegistry = require("../opsRegistry.js");
|
|
4
|
+
require("../executorUtils.js");
|
|
5
|
+
//#region src/executor/ops/variables.ts
|
|
6
|
+
require_opsRegistry.addOps(require_types.LispType.Return, ({ done, b }) => done(void 0, b));
|
|
7
|
+
require_opsRegistry.addOps(require_types.LispType.Var, ({ done, a, b, scope, bobj, internal }) => {
|
|
8
|
+
done(void 0, scope.declare(a, require_types.VarType.var, b, bobj?.isGlobal || false, internal));
|
|
9
|
+
});
|
|
10
|
+
require_opsRegistry.addOps(require_types.LispType.Let, ({ done, a, b, scope, bobj, internal }) => {
|
|
11
|
+
done(void 0, scope.declare(a, require_types.VarType.let, b, bobj?.isGlobal || false, internal));
|
|
12
|
+
});
|
|
13
|
+
require_opsRegistry.addOps(require_types.LispType.Const, ({ done, a, b, scope, bobj, internal }) => {
|
|
14
|
+
done(void 0, scope.declare(a, require_types.VarType.const, b, bobj?.isGlobal || false, internal));
|
|
15
|
+
});
|
|
16
|
+
//#endregion
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { LispType, IExecContext, Ticks, Scope } from '../utils';
|
|
2
|
+
import { LispItem } from '../parser';
|
|
3
|
+
import { YieldValue, Done } from './executorUtils';
|
|
4
|
+
export type ControlFlowAction = 'break' | 'continue';
|
|
5
|
+
export interface ControlFlowTarget {
|
|
6
|
+
label?: string;
|
|
7
|
+
acceptsBreak: boolean;
|
|
8
|
+
acceptsContinue: boolean;
|
|
9
|
+
acceptsUnlabeledBreak: boolean;
|
|
10
|
+
acceptsUnlabeledContinue: boolean;
|
|
11
|
+
}
|
|
12
|
+
export type ControlFlowTargets = readonly ControlFlowTarget[] | undefined;
|
|
13
|
+
export type Execution = <T = any>(ticks: Ticks, tree: LispItem, scope: Scope, context: IExecContext, done: Done<T>, statementLabels: ControlFlowTargets, internal: boolean, generatorYield: ((yv: YieldValue, done?: Done) => void) | undefined) => void;
|
|
14
|
+
export type OpsCallbackParams<a, b, obj, bobj> = {
|
|
15
|
+
op: LispType;
|
|
16
|
+
exec: Execution;
|
|
17
|
+
a: a;
|
|
18
|
+
b: b;
|
|
19
|
+
obj: obj;
|
|
20
|
+
bobj: bobj;
|
|
21
|
+
ticks: Ticks;
|
|
22
|
+
tree: LispItem;
|
|
23
|
+
scope: Scope;
|
|
24
|
+
context: IExecContext;
|
|
25
|
+
done: Done;
|
|
26
|
+
statementLabels: ControlFlowTargets;
|
|
27
|
+
internal: boolean;
|
|
28
|
+
generatorYield: ((yv: YieldValue, done?: Done) => void) | undefined;
|
|
29
|
+
};
|
|
30
|
+
type OpCallback<a, b, obj, bobj> = (params: OpsCallbackParams<a, b, obj, bobj>) => void;
|
|
31
|
+
export declare const ops: Map<LispType, OpCallback<any, any, any, any>>;
|
|
32
|
+
export declare function addOps<a = unknown, b = unknown, obj = unknown, bobj = unknown>(type: LispType, cb: OpCallback<a, b, obj, bobj>): void;
|
|
33
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require("./parserUtils.js");
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CodeString, LispType } from '
|
|
1
|
+
import { CodeString, LispType } from '../utils';
|
|
2
2
|
export type DefineLisp<op extends LispType, a extends LispItem | LispItem, b extends LispItem | LispItem> = [op, a, b];
|
|
3
3
|
export type ExtractLispOp<L> = L extends DefineLisp<infer i, any, any> ? i : never;
|
|
4
4
|
export type ExtractLispA<L> = L extends DefineLisp<any, infer i, any> ? i : never;
|
|
@@ -9,15 +9,17 @@ export type Lisp = [LispType, LispItem, LispItem];
|
|
|
9
9
|
export type Literal = DefineLisp<LispType.Literal, string, Lisp[]> & {
|
|
10
10
|
tempJsStrings?: string[];
|
|
11
11
|
};
|
|
12
|
-
export type
|
|
12
|
+
export type IfLisp = DefineLisp<LispType.If, Lisp, IfCase>;
|
|
13
13
|
export type InlineIf = DefineLisp<LispType.InlineIf, Lisp, InlineIfCase>;
|
|
14
|
+
export type StatementLabel = string | LispType.None;
|
|
14
15
|
export type IfCase = DefineLisp<LispType.IfCase, Lisp[], Lisp[]>;
|
|
15
16
|
export type InlineIfCase = DefineLisp<LispType.InlineIfCase, Lisp, Lisp>;
|
|
16
|
-
export type
|
|
17
|
-
export type
|
|
18
|
-
export type
|
|
17
|
+
export type Labeled = DefineLisp<LispType.Labeled, StatementLabel, Lisp>;
|
|
18
|
+
export type KeyValLisp = DefineLisp<LispType.KeyVal, string | Lisp, Lisp>;
|
|
19
|
+
export type SpreadObjectLisp = DefineLisp<LispType.SpreadObject, LispType.None, Lisp>;
|
|
20
|
+
export type SpreadArrayLisp = DefineLisp<LispType.SpreadArray, LispType.None, Lisp>;
|
|
19
21
|
export type ArrayProp = DefineLisp<LispType.ArrayProp, Lisp, Lisp>;
|
|
20
|
-
export type
|
|
22
|
+
export type PropLisp = DefineLisp<LispType.Prop, Lisp, string | Lisp>;
|
|
21
23
|
export type PropOptional = DefineLisp<LispType.PropOptional, Lisp, Lisp[]>;
|
|
22
24
|
export type Call = DefineLisp<LispType.Call, Lisp, Lisp[]>;
|
|
23
25
|
export type CallOptional = DefineLisp<LispType.CallOptional, Lisp, Lisp[]>;
|
|
@@ -75,6 +77,7 @@ export type Divide = DefineLisp<LispType.Divide, Lisp, Lisp>;
|
|
|
75
77
|
export type Power = DefineLisp<LispType.Power, Lisp, Lisp>;
|
|
76
78
|
export type Multiply = DefineLisp<LispType.Multiply, Lisp, Lisp>;
|
|
77
79
|
export type Modulus = DefineLisp<LispType.Modulus, Lisp, Lisp>;
|
|
80
|
+
export type InternalCode = DefineLisp<LispType.InternalBlock, Lisp[], LispType.None>;
|
|
78
81
|
export type Block = DefineLisp<LispType.Block, Lisp[], LispType.None>;
|
|
79
82
|
export type Expression = DefineLisp<LispType.Expression, Lisp[], LispType.None>;
|
|
80
83
|
export type Return = DefineLisp<LispType.Return, LispType.None, Lisp>;
|
|
@@ -84,6 +87,7 @@ export type SwitchCase = DefineLisp<LispType.SwitchCase, LispType.None | Lisp, L
|
|
|
84
87
|
export type Var = DefineLisp<LispType.Var, string, Lisp | LispType.None>;
|
|
85
88
|
export type Let = DefineLisp<LispType.Let, string, Lisp | LispType.None>;
|
|
86
89
|
export type Const = DefineLisp<LispType.Const, string, Lisp | LispType.None>;
|
|
90
|
+
export type Internal = DefineLisp<LispType.Internal, string, Lisp | LispType.None>;
|
|
87
91
|
export type Number = DefineLisp<LispType.Number, LispType.None, string>;
|
|
88
92
|
export type BigInt = DefineLisp<LispType.BigInt, LispType.None, string>;
|
|
89
93
|
export type GlobalSymbol = DefineLisp<LispType.GlobalSymbol, LispType.None, string>;
|
|
@@ -94,13 +98,15 @@ export type Function = DefineLisp<LispType.Function, (string | LispType.None | L
|
|
|
94
98
|
export type InlineFunction = DefineLisp<LispType.InlineFunction, string[], string | Lisp[]>;
|
|
95
99
|
export type ArrowFunction = DefineLisp<LispType.ArrowFunction, string[], string | Lisp[]>;
|
|
96
100
|
export type Loop = DefineLisp<LispType.Loop, LispItem, Lisp[]>;
|
|
97
|
-
export type LoopAction = DefineLisp<LispType.LoopAction, string,
|
|
101
|
+
export type LoopAction = DefineLisp<LispType.LoopAction, string, StatementLabel>;
|
|
98
102
|
export type Try = DefineLisp<LispType.Try, Lisp[], LispItem>;
|
|
99
103
|
export type Void = DefineLisp<LispType.Void, Lisp, LispType.None>;
|
|
100
104
|
export type Await = DefineLisp<LispType.Await, Lisp, LispType.None>;
|
|
105
|
+
export type Yield = DefineLisp<LispType.Yield, Lisp, LispType.None>;
|
|
106
|
+
export type YieldDelegate = DefineLisp<LispType.YieldDelegate, Lisp, LispType.None>;
|
|
101
107
|
export type New = DefineLisp<LispType.New, Lisp, Lisp[]>;
|
|
102
108
|
export type None = DefineLisp<LispType.None, LispType.None, LispType.None>;
|
|
103
|
-
export type LispFamily = Literal |
|
|
109
|
+
export type LispFamily = Literal | IfLisp | InlineIf | IfCase | InlineIfCase | Labeled | KeyValLisp | SpreadObjectLisp | SpreadArrayLisp | ArrayProp | PropLisp | PropOptional | Call | CallOptional | CreateArray | CreateObject | Group | Inverse | Not | Negative | Positive | Typeof | Delete | IncrementBefore | IncrementAfter | DecrementBefore | DecrementAfter | And | Or | NullishCoalescing | Instanceof | In | Assigns | SubractEquals | AddEquals | DivideEquals | PowerEquals | MultiplyEquals | ModulusEquals | BitNegateEquals | BitAndEquals | BitOrEquals | UnsignedShiftRightEquals | ShiftLeftEquals | ShiftRightEquals | AndEquals | OrEquals | NullishCoalescingEquals | BitAnd | BitOr | BitNegate | BitShiftLeft | BitShiftRight | BitUnsignedShiftRight | SmallerEqualThan | LargerEqualThan | SmallerThan | LargerThan | StrictNotEqual | NotEqual | StrictEqual | Equal | Plus | Minus | Divide | Power | Multiply | Modulus | InternalCode | Expression | Return | Throw | Switch | SwitchCase | Var | Let | Const | Number | BigInt | GlobalSymbol | LiteralIndex | StringIndex | RegexIndex | Function | InlineFunction | ArrowFunction | Loop | LoopAction | Try | Void | Await | Yield | YieldDelegate | New | Block | Internal | None;
|
|
104
110
|
export interface IRegEx {
|
|
105
111
|
regex: string;
|
|
106
112
|
flags: string;
|
|
@@ -111,44 +117,25 @@ export interface IConstants {
|
|
|
111
117
|
literals: Literal[];
|
|
112
118
|
regexes: IRegEx[];
|
|
113
119
|
eager: boolean;
|
|
120
|
+
maxDepth: number;
|
|
114
121
|
}
|
|
115
122
|
export interface IExecutionTree {
|
|
116
123
|
tree: Lisp[];
|
|
117
124
|
constants: IConstants;
|
|
118
125
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
code: string;
|
|
124
|
-
constructor(message: string, code: string);
|
|
126
|
+
export interface LispDepthCtx {
|
|
127
|
+
generatorDepth: number;
|
|
128
|
+
asyncDepth: number;
|
|
129
|
+
lispDepth: number;
|
|
125
130
|
}
|
|
126
|
-
export
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
words?: string[];
|
|
134
|
-
lastWord?: string;
|
|
135
|
-
lastAnyWord?: string;
|
|
136
|
-
regRes?: RegExpExecArray;
|
|
137
|
-
bodyContentAfterKeyword?: boolean;
|
|
138
|
-
}
|
|
139
|
-
export declare function restOfExp(constants: IConstants, part: CodeString, tests?: RegExp[], quote?: string, firstOpening?: string, closingsTests?: RegExp[], details?: restDetails): CodeString;
|
|
140
|
-
export declare namespace restOfExp {
|
|
141
|
-
var next: string[];
|
|
131
|
+
export interface LispCallbackCtx extends LispDepthCtx {
|
|
132
|
+
constants: IConstants;
|
|
133
|
+
type: string;
|
|
134
|
+
part: CodeString;
|
|
135
|
+
res: string[];
|
|
136
|
+
expect: string;
|
|
137
|
+
lispTree: Lisp;
|
|
142
138
|
}
|
|
143
|
-
export
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
export declare function lispifyFunction(str: CodeString, constants: IConstants, expression?: boolean): Lisp[];
|
|
147
|
-
export declare function insertSemicolons(constants: IConstants, str: CodeString): CodeString;
|
|
148
|
-
export declare function checkRegex(str: string): IRegEx | null;
|
|
149
|
-
export declare function extractConstants(constants: IConstants, str: string, currentEnclosure?: string): {
|
|
150
|
-
str: string;
|
|
151
|
-
length: number;
|
|
152
|
-
};
|
|
153
|
-
export default function parse(code: string, eager?: boolean, expression?: boolean): IExecutionTree;
|
|
154
|
-
export {};
|
|
139
|
+
export type LispCallback<T> = (ctx: LispCallbackCtx & {
|
|
140
|
+
type: T;
|
|
141
|
+
}) => any;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { RegisterLispTypesDeps } from './shared';
|
|
2
|
+
export declare function registerConditionalLispTypes({ createLisp, emptyString, extractStatementLabels, insertSemicolons, lispifyBlock, lispifyExpr, restOfExp, semiColon, setLispType, wrapLabeledStatement, }: RegisterLispTypesDeps): void;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
const require_errors = require("../../utils/errors.js");
|
|
2
|
+
const require_types = require("../../utils/types.js");
|
|
3
|
+
require("../../utils/index.js");
|
|
4
|
+
//#region src/parser/lispTypes/conditionals.ts
|
|
5
|
+
function registerConditionalLispTypes({ createLisp, emptyString, extractStatementLabels, insertSemicolons, lispifyBlock, lispifyExpr, restOfExp, semiColon, setLispType, wrapLabeledStatement }) {
|
|
6
|
+
const elseIf = /^else(?![\w$])/;
|
|
7
|
+
const ifElse = /^if(?![\w$])/;
|
|
8
|
+
function extractIfElse(constants, part, depth = 0) {
|
|
9
|
+
if (depth > constants.maxDepth) throw new require_errors.SandboxCapabilityError("Maximum expression depth exceeded");
|
|
10
|
+
let count = 0;
|
|
11
|
+
let found = part.substring(0, 0);
|
|
12
|
+
let foundElse = emptyString;
|
|
13
|
+
let foundTrue;
|
|
14
|
+
let first = true;
|
|
15
|
+
let elseReg;
|
|
16
|
+
let details = {};
|
|
17
|
+
while ((found = restOfExp(constants, part.substring(found.end - part.start), [
|
|
18
|
+
elseIf,
|
|
19
|
+
ifElse,
|
|
20
|
+
semiColon
|
|
21
|
+
], void 0, void 0, void 0, details)).length || first) {
|
|
22
|
+
first = false;
|
|
23
|
+
const f = part.substring(found.end - part.start).toString();
|
|
24
|
+
if (f.startsWith("if")) {
|
|
25
|
+
found.end++;
|
|
26
|
+
count++;
|
|
27
|
+
} else if (f.startsWith("else")) {
|
|
28
|
+
foundTrue = part.substring(0, found.end - part.start);
|
|
29
|
+
found.end++;
|
|
30
|
+
count--;
|
|
31
|
+
if (!count) found.end--;
|
|
32
|
+
} else if (elseReg = /^;?\s*else(?![\w$])/.exec(f)) {
|
|
33
|
+
foundTrue = part.substring(0, found.end - part.start);
|
|
34
|
+
found.end += elseReg[0].length - 1;
|
|
35
|
+
count--;
|
|
36
|
+
if (!count) found.end -= elseReg[0].length - 1;
|
|
37
|
+
} else {
|
|
38
|
+
foundTrue = foundElse.length ? foundTrue : part.substring(0, found.end - part.start);
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
if (!count) {
|
|
42
|
+
foundElse = extractIfElse(constants, part.substring(found.end - part.start + (/^;?\s*else(?![\w$])/.exec(f)?.[0].length || 0)), depth + 1).all;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
details = {};
|
|
46
|
+
}
|
|
47
|
+
foundTrue = foundTrue || part.substring(0, found.end - part.start);
|
|
48
|
+
return {
|
|
49
|
+
all: part.substring(0, Math.max(foundTrue.end, foundElse.end) - part.start),
|
|
50
|
+
true: foundTrue,
|
|
51
|
+
false: foundElse
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
setLispType(["if"], (ctx) => {
|
|
55
|
+
const { constants, part, res } = ctx;
|
|
56
|
+
const labels = extractStatementLabels(res[1]);
|
|
57
|
+
let condition = restOfExp(constants, part.substring(res[0].length), [], "(");
|
|
58
|
+
const ie = extractIfElse(constants, part.substring(res[1].length));
|
|
59
|
+
const startTrue = res[0].length - res[1].length + condition.length + 1;
|
|
60
|
+
let trueBlock = ie.true.substring(startTrue);
|
|
61
|
+
let elseBlock = ie.false;
|
|
62
|
+
condition = condition.trim();
|
|
63
|
+
trueBlock = trueBlock.trim();
|
|
64
|
+
elseBlock = elseBlock.trim();
|
|
65
|
+
if (!trueBlock.length || /^else(?![\w$])/.test(trueBlock.toString())) throw new SyntaxError("Unexpected token");
|
|
66
|
+
if (trueBlock.char(0) === "{") trueBlock = trueBlock.slice(1, -1);
|
|
67
|
+
if (elseBlock.char(0) === "{") elseBlock = elseBlock.slice(1, -1);
|
|
68
|
+
ctx.lispTree = wrapLabeledStatement(labels, createLisp({
|
|
69
|
+
op: require_types.LispType.If,
|
|
70
|
+
a: lispifyExpr(constants, condition, void 0, ctx),
|
|
71
|
+
b: createLisp({
|
|
72
|
+
op: require_types.LispType.IfCase,
|
|
73
|
+
a: lispifyBlock(trueBlock, constants, false, ctx),
|
|
74
|
+
b: lispifyBlock(elseBlock, constants, false, ctx)
|
|
75
|
+
})
|
|
76
|
+
}));
|
|
77
|
+
});
|
|
78
|
+
setLispType(["switch"], (ctx) => {
|
|
79
|
+
const { constants, part, res } = ctx;
|
|
80
|
+
const labels = extractStatementLabels(res[1]);
|
|
81
|
+
const test = restOfExp(constants, part.substring(res[0].length), [], "(");
|
|
82
|
+
let start = part.toString().indexOf("{", res[0].length + test.length + 1);
|
|
83
|
+
if (start === -1) throw new SyntaxError("Invalid switch");
|
|
84
|
+
let statement = insertSemicolons(constants, restOfExp(constants, part.substring(start + 1), [], "{"));
|
|
85
|
+
let caseFound;
|
|
86
|
+
const caseTest = /^\s*(case\s|default)\s*/;
|
|
87
|
+
const caseNoTestReg = /^\s*case\s*:/;
|
|
88
|
+
const cases = [];
|
|
89
|
+
let defaultFound = false;
|
|
90
|
+
while ((caseFound = caseTest.exec(statement.toString())) || caseNoTestReg.test(statement.toString())) {
|
|
91
|
+
if (!caseFound) throw new SyntaxError("Unexpected end of expression");
|
|
92
|
+
if (caseFound[1] === "default") {
|
|
93
|
+
if (defaultFound) throw new SyntaxError("Only one default switch case allowed");
|
|
94
|
+
defaultFound = true;
|
|
95
|
+
}
|
|
96
|
+
const cond = restOfExp(constants, statement.substring(caseFound[0].length), [/^:/]);
|
|
97
|
+
if (caseFound[1] !== "default" && !cond.trimStart().length) throw new SyntaxError("Unexpected end of expression");
|
|
98
|
+
let found = emptyString;
|
|
99
|
+
let i = start = caseFound[0].length + cond.length + 1;
|
|
100
|
+
const bracketFound = /^\s*\{/.exec(statement.substring(i).toString());
|
|
101
|
+
let exprs = [];
|
|
102
|
+
if (bracketFound) {
|
|
103
|
+
i += bracketFound[0].length;
|
|
104
|
+
found = restOfExp(constants, statement.substring(i), [], "{");
|
|
105
|
+
i += found.length + 1;
|
|
106
|
+
exprs = lispifyBlock(found, constants, false, ctx);
|
|
107
|
+
} else {
|
|
108
|
+
const notEmpty = restOfExp(constants, statement.substring(i), [caseTest]);
|
|
109
|
+
if (!notEmpty.trim().length) {
|
|
110
|
+
exprs = [];
|
|
111
|
+
i += notEmpty.length;
|
|
112
|
+
} else {
|
|
113
|
+
while ((found = restOfExp(constants, statement.substring(i), [semiColon])).length) {
|
|
114
|
+
i += found.length + (statement.char(i + found.length) === ";" ? 1 : 0);
|
|
115
|
+
if (caseTest.test(statement.substring(i).toString())) break;
|
|
116
|
+
}
|
|
117
|
+
exprs = lispifyBlock(statement.substring(start, found.end - statement.start), constants, false, ctx);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
statement = statement.substring(i);
|
|
121
|
+
cases.push(createLisp({
|
|
122
|
+
op: require_types.LispType.SwitchCase,
|
|
123
|
+
a: caseFound[1] === "default" ? require_types.LispType.None : lispifyExpr(constants, cond, void 0, ctx),
|
|
124
|
+
b: exprs
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
127
|
+
ctx.lispTree = wrapLabeledStatement(labels, createLisp({
|
|
128
|
+
op: require_types.LispType.Switch,
|
|
129
|
+
a: lispifyExpr(constants, test, void 0, ctx),
|
|
130
|
+
b: cases
|
|
131
|
+
}));
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
//#endregion
|
|
135
|
+
exports.registerConditionalLispTypes = registerConditionalLispTypes;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { RegisterLispTypesDeps } from './shared';
|
|
2
|
+
export declare function registerControlLispTypes({ ParseError, createLisp, emptyString, expandDestructure, extractStatementLabels, insertSemicolons, lispify, lispifyBlock, lispifyExpr, lispifyReturnExpr, restOfExp, semiColon, setLispType, startingExecpted, wrapLabeledStatement, }: RegisterLispTypesDeps): void;
|