@cuxt/sandboxjs 0.1.1 → 0.1.4
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 +17 -16
- 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,50 @@
|
|
|
1
|
+
import { LispType } from "../../utils/types.js";
|
|
2
|
+
import { Prop } from "../../utils/Prop.js";
|
|
3
|
+
import { sanitizeProp } from "../../utils/Scope.js";
|
|
4
|
+
import "../../utils/index.js";
|
|
5
|
+
import { addOps } from "../opsRegistry.js";
|
|
6
|
+
import { assignCheck } from "../executorUtils.js";
|
|
7
|
+
//#region src/executor/ops/unary.ts
|
|
8
|
+
addOps(LispType.Not, ({ done, b }) => done(void 0, !b));
|
|
9
|
+
addOps(LispType.Inverse, ({ done, b }) => done(void 0, ~b));
|
|
10
|
+
addOps(LispType.IncrementBefore, ({ done, obj, context }) => {
|
|
11
|
+
assignCheck(obj, context);
|
|
12
|
+
done(void 0, ++obj.context[obj.prop]);
|
|
13
|
+
});
|
|
14
|
+
addOps(LispType.IncrementAfter, ({ done, obj, context }) => {
|
|
15
|
+
assignCheck(obj, context);
|
|
16
|
+
done(void 0, obj.context[obj.prop]++);
|
|
17
|
+
});
|
|
18
|
+
addOps(LispType.DecrementBefore, ({ done, obj, context }) => {
|
|
19
|
+
assignCheck(obj, context);
|
|
20
|
+
done(void 0, --obj.context[obj.prop]);
|
|
21
|
+
});
|
|
22
|
+
addOps(LispType.DecrementAfter, ({ done, obj, context }) => {
|
|
23
|
+
assignCheck(obj, context);
|
|
24
|
+
done(void 0, obj.context[obj.prop]--);
|
|
25
|
+
});
|
|
26
|
+
addOps(LispType.Positive, ({ done, b }) => done(void 0, +b));
|
|
27
|
+
addOps(LispType.Negative, ({ done, b }) => done(void 0, -b));
|
|
28
|
+
addOps(LispType.Typeof, ({ exec, done, ticks, b, context, scope, internal, generatorYield }) => {
|
|
29
|
+
exec(ticks, b, scope, context, (e, prop) => {
|
|
30
|
+
done(void 0, typeof sanitizeProp(prop, context));
|
|
31
|
+
}, void 0, internal, generatorYield);
|
|
32
|
+
});
|
|
33
|
+
addOps(LispType.Delete, ({ done, context, bobj }) => {
|
|
34
|
+
if (!(bobj instanceof Prop)) {
|
|
35
|
+
done(void 0, true);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
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
|
+
addOps(LispType.Void, ({ done }) => {
|
|
46
|
+
done();
|
|
47
|
+
});
|
|
48
|
+
//#endregion
|
|
49
|
+
|
|
50
|
+
//# sourceMappingURL=unary.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unary.js","names":[],"sources":["../../../../src/executor/ops/unary.ts"],"sourcesContent":["import { addOps, assignCheck } from '../executorUtils';\nimport type { LispItem } from '../../parser';\nimport { LispType, Prop, sanitizeProp } from '../../utils';\n\naddOps<unknown, unknown>(LispType.Not, ({ done, b }) => done(undefined, !b));\n\naddOps<unknown, number>(LispType.Inverse, ({ done, b }) => done(undefined, ~b));\n\naddOps<unknown, unknown, Prop<any>>(LispType.IncrementBefore, ({ done, obj, context }) => {\n assignCheck(obj, context);\n done(undefined, ++obj.context[obj.prop]);\n});\n\naddOps<unknown, unknown, Prop<any>>(LispType.IncrementAfter, ({ done, obj, context }) => {\n assignCheck(obj, context);\n done(undefined, obj.context[obj.prop]++);\n});\n\naddOps<unknown, unknown, Prop<any>>(LispType.DecrementBefore, ({ done, obj, context }) => {\n assignCheck(obj, context);\n done(undefined, --obj.context[obj.prop]);\n});\n\naddOps<unknown, unknown, Prop<any>>(LispType.DecrementAfter, ({ done, obj, context }) => {\n assignCheck(obj, context);\n done(undefined, obj.context[obj.prop]--);\n});\n\naddOps<number, number>(LispType.Positive, ({ done, b }) => done(undefined, +b));\n\naddOps<number, number>(LispType.Negative, ({ done, b }) => done(undefined, -b));\n\naddOps<unknown, LispItem>(\n LispType.Typeof,\n ({ exec, done, ticks, b, context, scope, internal, generatorYield }) => {\n exec(\n ticks,\n b,\n scope,\n context,\n (e, prop) => {\n done(undefined, typeof sanitizeProp(prop, context));\n },\n undefined,\n internal,\n generatorYield,\n );\n },\n);\n\naddOps<unknown, unknown>(LispType.Delete, ({ done, context, bobj }) => {\n if (!(bobj instanceof Prop)) {\n done(undefined, true);\n return;\n }\n assignCheck(bobj, context, 'delete');\n if (bobj.isVariable) {\n done(undefined, false);\n return;\n }\n done(undefined, delete (bobj.context as any)?.[bobj.prop]);\n});\n\naddOps(LispType.Void, ({ done }) => {\n done();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,MAAM,SAAS,SAAS;AACpC,KAAI,KAAK,YAAY;AACnB,OAAK,KAAA,GAAW,MAAM;AACtB;;AAEF,MAAK,KAAA,GAAW,OAAQ,KAAK,UAAkB,KAAK,MAAM;EAC1D;AAEF,OAAO,SAAS,OAAO,EAAE,WAAW;AAClC,OAAM;EACN"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { LispType, VarType } from "../../utils/types.js";
|
|
2
|
+
import "../../utils/index.js";
|
|
3
|
+
import { addOps } from "../opsRegistry.js";
|
|
4
|
+
import "../executorUtils.js";
|
|
5
|
+
//#region src/executor/ops/variables.ts
|
|
6
|
+
addOps(LispType.Return, ({ done, b }) => done(void 0, b));
|
|
7
|
+
addOps(LispType.Var, ({ done, a, b, scope, bobj, internal }) => {
|
|
8
|
+
done(void 0, scope.declare(a, VarType.var, b, bobj?.isGlobal || false, internal));
|
|
9
|
+
});
|
|
10
|
+
addOps(LispType.Let, ({ done, a, b, scope, bobj, internal }) => {
|
|
11
|
+
done(void 0, scope.declare(a, VarType.let, b, bobj?.isGlobal || false, internal));
|
|
12
|
+
});
|
|
13
|
+
addOps(LispType.Const, ({ done, a, b, scope, bobj, internal }) => {
|
|
14
|
+
done(void 0, scope.declare(a, VarType.const, b, bobj?.isGlobal || false, internal));
|
|
15
|
+
});
|
|
16
|
+
//#endregion
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=variables.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"variables.js","names":[],"sources":["../../../../src/executor/ops/variables.ts"],"sourcesContent":["import { addOps } from '../executorUtils';\nimport { LispType, Prop, VarType } from '../../utils';\n\naddOps(LispType.Return, ({ done, b }) => done(undefined, b));\n\naddOps<string, unknown, unknown, Prop>(LispType.Var, ({ done, a, b, scope, bobj, internal }) => {\n done(undefined, scope.declare(a, VarType.var, b, bobj?.isGlobal || false, internal));\n});\n\naddOps<string, unknown, unknown, Prop>(LispType.Let, ({ done, a, b, scope, bobj, internal }) => {\n done(undefined, scope.declare(a, VarType.let, b, bobj?.isGlobal || false, internal));\n});\n\naddOps<string, unknown, unknown, Prop>(LispType.Const, ({ done, a, b, scope, bobj, internal }) => {\n done(undefined, scope.declare(a, VarType.const, b, bobj?.isGlobal || false, internal));\n});\n"],"mappings":";;;;;AAGA,OAAO,SAAS,SAAS,EAAE,MAAM,QAAQ,KAAK,KAAA,GAAW,EAAE,CAAC;AAE5D,OAAuC,SAAS,MAAM,EAAE,MAAM,GAAG,GAAG,OAAO,MAAM,eAAe;AAC9F,MAAK,KAAA,GAAW,MAAM,QAAQ,GAAG,QAAQ,KAAK,GAAG,MAAM,YAAY,OAAO,SAAS,CAAC;EACpF;AAEF,OAAuC,SAAS,MAAM,EAAE,MAAM,GAAG,GAAG,OAAO,MAAM,eAAe;AAC9F,MAAK,KAAA,GAAW,MAAM,QAAQ,GAAG,QAAQ,KAAK,GAAG,MAAM,YAAY,OAAO,SAAS,CAAC;EACpF;AAEF,OAAuC,SAAS,QAAQ,EAAE,MAAM,GAAG,GAAG,OAAO,MAAM,eAAe;AAChG,MAAK,KAAA,GAAW,MAAM,QAAQ,GAAG,QAAQ,OAAO,GAAG,MAAM,YAAY,OAAO,SAAS,CAAC;EACtF"}
|
|
@@ -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
|
+
{"version":3,"file":"opsRegistry.js","names":[],"sources":["../../../src/executor/opsRegistry.ts"],"sourcesContent":["/**\n * Ops registry — kept separate from executorUtils.ts so that ops/*.ts files\n * can import addOps without creating a circular initialization problem.\n *\n * executorUtils.ts imports from here; ops/*.ts imports from here too.\n * No imports from executorUtils.ts allowed in this file.\n */\nimport { LispType } from '../utils';\nimport type { LispItem } from '../parser';\nimport type { IExecContext, Ticks } from '../utils';\nimport type { Scope } from '../utils';\nimport type { YieldValue, Done } from './executorUtils';\n\nexport type ControlFlowAction = 'break' | 'continue';\n\nexport interface ControlFlowTarget {\n label?: string;\n acceptsBreak: boolean;\n acceptsContinue: boolean;\n acceptsUnlabeledBreak: boolean;\n acceptsUnlabeledContinue: boolean;\n}\n\nexport type ControlFlowTargets = readonly ControlFlowTarget[] | undefined;\n\nexport type Execution = <T = any>(\n ticks: Ticks,\n tree: LispItem,\n scope: Scope,\n context: IExecContext,\n done: Done<T>,\n statementLabels: ControlFlowTargets,\n internal: boolean,\n generatorYield: ((yv: YieldValue, done?: Done) => void) | undefined,\n) => void;\n\nexport type OpsCallbackParams<a, b, obj, bobj> = {\n op: LispType;\n exec: Execution;\n a: a;\n b: b;\n obj: obj;\n bobj: bobj;\n ticks: Ticks;\n tree: LispItem;\n scope: Scope;\n context: IExecContext;\n done: Done;\n statementLabels: ControlFlowTargets;\n internal: boolean;\n generatorYield: ((yv: YieldValue, done?: Done) => void) | undefined;\n};\n\ntype OpCallback<a, b, obj, bobj> = (params: OpsCallbackParams<a, b, obj, bobj>) => void;\n\nexport const ops = new Map<LispType, OpCallback<any, any, any, any>>();\n\nexport function addOps<a = unknown, b = unknown, obj = unknown, bobj = unknown>(\n type: LispType,\n cb: OpCallback<a, b, obj, bobj>,\n) {\n ops.set(type, cb);\n}\n"],"mappings":";AAuDA,IAAa,sBAAM,IAAI,KAA+C;AAEtE,SAAgB,OACd,MACA,IACA;AACA,KAAI,IAAI,MAAM,GAAG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "./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,137 @@
|
|
|
1
|
+
import { SandboxCapabilityError } from "../../utils/errors.js";
|
|
2
|
+
import { LispType } from "../../utils/types.js";
|
|
3
|
+
import "../../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 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: LispType.If,
|
|
70
|
+
a: lispifyExpr(constants, condition, void 0, ctx),
|
|
71
|
+
b: createLisp({
|
|
72
|
+
op: 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: LispType.SwitchCase,
|
|
123
|
+
a: caseFound[1] === "default" ? LispType.None : lispifyExpr(constants, cond, void 0, ctx),
|
|
124
|
+
b: exprs
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
127
|
+
ctx.lispTree = wrapLabeledStatement(labels, createLisp({
|
|
128
|
+
op: LispType.Switch,
|
|
129
|
+
a: lispifyExpr(constants, test, void 0, ctx),
|
|
130
|
+
b: cases
|
|
131
|
+
}));
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
//#endregion
|
|
135
|
+
export { registerConditionalLispTypes };
|
|
136
|
+
|
|
137
|
+
//# sourceMappingURL=conditionals.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conditionals.js","names":[],"sources":["../../../../src/parser/lispTypes/conditionals.ts"],"sourcesContent":["import { CodeString, LispType, SandboxCapabilityError } from '../../utils';\nimport type { IConstants, IfLisp, IfCase, Lisp, Switch, SwitchCase } from '../lisp';\nimport type { RegisterLispTypesDeps } from './shared';\n\nexport function registerConditionalLispTypes({\n createLisp,\n emptyString,\n extractStatementLabels,\n insertSemicolons,\n lispifyBlock,\n lispifyExpr,\n restOfExp,\n semiColon,\n setLispType,\n wrapLabeledStatement,\n}: RegisterLispTypesDeps) {\n const inlineIfElse = /^:/;\n const elseIf = /^else(?![\\w$])/;\n const ifElse = /^if(?![\\w$])/;\n\n function extractIfElse(constants: IConstants, part: CodeString, depth = 0) {\n if (depth > constants.maxDepth) {\n throw new SandboxCapabilityError('Maximum expression depth exceeded');\n }\n let count = 0;\n let found = part.substring(0, 0);\n let foundElse = emptyString;\n let foundTrue: CodeString | undefined;\n let first = true;\n let elseReg: RegExpExecArray | null;\n let details: any = {};\n while (\n (found = restOfExp(\n constants,\n part.substring(found.end - part.start),\n [elseIf, ifElse, semiColon],\n undefined,\n undefined,\n undefined,\n details,\n )).length ||\n first\n ) {\n first = false;\n const f = part.substring(found.end - part.start).toString();\n\n if (f.startsWith('if')) {\n found.end++;\n count++;\n } else if (f.startsWith('else')) {\n foundTrue = part.substring(0, found.end - part.start);\n found.end++;\n count--;\n if (!count) {\n found.end--;\n }\n } else if ((elseReg = /^;?\\s*else(?![\\w$])/.exec(f))) {\n foundTrue = part.substring(0, found.end - part.start);\n found.end += elseReg[0].length - 1;\n count--;\n if (!count) {\n found.end -= elseReg[0].length - 1;\n }\n } else {\n foundTrue = foundElse.length ? foundTrue : part.substring(0, found.end - part.start);\n break;\n }\n if (!count) {\n const ie = extractIfElse(\n constants,\n part.substring(found.end - part.start + (/^;?\\s*else(?![\\w$])/.exec(f)?.[0].length || 0)),\n depth + 1,\n );\n foundElse = ie.all;\n break;\n }\n details = {};\n }\n foundTrue = foundTrue || part.substring(0, found.end - part.start);\n return {\n all: part.substring(0, Math.max(foundTrue.end, foundElse.end) - part.start),\n true: foundTrue,\n false: foundElse,\n };\n }\n\n setLispType(['if'] as const, (ctx) => {\n const { constants, part, res } = ctx;\n const labels = extractStatementLabels(res[1]);\n let condition = restOfExp(constants, part.substring(res[0].length), [], '(');\n const ie = extractIfElse(constants, part.substring(res[1].length));\n const startTrue = res[0].length - res[1].length + condition.length + 1;\n\n let trueBlock = ie.true.substring(startTrue);\n let elseBlock = ie.false;\n\n condition = condition.trim();\n trueBlock = trueBlock.trim();\n elseBlock = elseBlock.trim();\n\n if (!trueBlock.length || /^else(?![\\w$])/.test(trueBlock.toString())) {\n throw new SyntaxError('Unexpected token');\n }\n if (trueBlock.char(0) === '{') trueBlock = trueBlock.slice(1, -1);\n if (elseBlock.char(0) === '{') elseBlock = elseBlock.slice(1, -1);\n ctx.lispTree = wrapLabeledStatement(\n labels,\n createLisp<IfLisp>({\n op: LispType.If,\n a: lispifyExpr(constants, condition, undefined, ctx),\n b: createLisp<IfCase>({\n op: LispType.IfCase,\n a: lispifyBlock(trueBlock, constants, false, ctx),\n b: lispifyBlock(elseBlock, constants, false, ctx),\n }),\n }),\n ) as Lisp;\n });\n\n setLispType(['switch'] as const, (ctx) => {\n const { constants, part, res } = ctx;\n const labels = extractStatementLabels(res[1]);\n const test = restOfExp(constants, part.substring(res[0].length), [], '(');\n let start = part.toString().indexOf('{', res[0].length + test.length + 1);\n if (start === -1) throw new SyntaxError('Invalid switch');\n let statement = insertSemicolons(\n constants,\n restOfExp(constants, part.substring(start + 1), [], '{'),\n );\n let caseFound: RegExpExecArray | null;\n const caseTest = /^\\s*(case\\s|default)\\s*/;\n const caseNoTestReg = /^\\s*case\\s*:/;\n const cases: SwitchCase[] = [];\n let defaultFound = false;\n while (\n (caseFound = caseTest.exec(statement.toString())) ||\n caseNoTestReg.test(statement.toString())\n ) {\n if (!caseFound) {\n throw new SyntaxError('Unexpected end of expression');\n }\n if (caseFound[1] === 'default') {\n if (defaultFound) throw new SyntaxError('Only one default switch case allowed');\n defaultFound = true;\n }\n const cond = restOfExp(constants, statement.substring(caseFound[0].length), [/^:/]);\n if (caseFound[1] !== 'default' && !cond.trimStart().length) {\n throw new SyntaxError('Unexpected end of expression');\n }\n let found = emptyString;\n let i = (start = caseFound[0].length + cond.length + 1);\n const bracketFound = /^\\s*\\{/.exec(statement.substring(i).toString());\n let exprs: Lisp[] = [];\n if (bracketFound) {\n i += bracketFound[0].length;\n found = restOfExp(constants, statement.substring(i), [], '{');\n i += found.length + 1;\n exprs = lispifyBlock(found, constants, false, ctx);\n } else {\n const notEmpty = restOfExp(constants, statement.substring(i), [caseTest]);\n if (!notEmpty.trim().length) {\n exprs = [];\n i += notEmpty.length;\n } else {\n while ((found = restOfExp(constants, statement.substring(i), [semiColon])).length) {\n i += found.length + (statement.char(i + found.length) === ';' ? 1 : 0);\n if (caseTest.test(statement.substring(i).toString())) {\n break;\n }\n }\n exprs = lispifyBlock(\n statement.substring(start, found.end - statement.start),\n constants,\n false,\n ctx,\n );\n }\n }\n statement = statement.substring(i);\n cases.push(\n createLisp<SwitchCase>({\n op: LispType.SwitchCase,\n a:\n caseFound[1] === 'default'\n ? LispType.None\n : lispifyExpr(constants, cond, undefined, ctx),\n b: exprs,\n }),\n );\n }\n ctx.lispTree = wrapLabeledStatement(\n labels,\n createLisp<Switch>({\n op: LispType.Switch,\n a: lispifyExpr(constants, test, undefined, ctx),\n b: cases,\n }),\n ) as Lisp;\n });\n\n void inlineIfElse;\n}\n"],"mappings":";;;;AAIA,SAAgB,6BAA6B,EAC3C,YACA,aACA,wBACA,kBACA,cACA,aACA,WACA,WACA,aACA,wBACwB;CAExB,MAAM,SAAS;CACf,MAAM,SAAS;CAEf,SAAS,cAAc,WAAuB,MAAkB,QAAQ,GAAG;AACzE,MAAI,QAAQ,UAAU,SACpB,OAAM,IAAI,uBAAuB,oCAAoC;EAEvE,IAAI,QAAQ;EACZ,IAAI,QAAQ,KAAK,UAAU,GAAG,EAAE;EAChC,IAAI,YAAY;EAChB,IAAI;EACJ,IAAI,QAAQ;EACZ,IAAI;EACJ,IAAI,UAAe,EAAE;AACrB,UACG,QAAQ,UACP,WACA,KAAK,UAAU,MAAM,MAAM,KAAK,MAAM,EACtC;GAAC;GAAQ;GAAQ;GAAU,EAC3B,KAAA,GACA,KAAA,GACA,KAAA,GACA,QACD,EAAE,UACH,OACA;AACA,WAAQ;GACR,MAAM,IAAI,KAAK,UAAU,MAAM,MAAM,KAAK,MAAM,CAAC,UAAU;AAE3D,OAAI,EAAE,WAAW,KAAK,EAAE;AAC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ACb,QACA,WAAmB;GACjB,IAAI,SAAS;GACb,GAAG,YAAY,WAAW,MAAM,KAAA,GAAW,IAAI;GAC/C,GAAG;GACJ,CAAC,CACH;GACD"}
|
|
@@ -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;
|