scriptable 0.1.1 → 0.2.1

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/src/evaluate.ts DELETED
@@ -1,275 +0,0 @@
1
- import type { Node } from "acorn";
2
- import binaryOps from "./binary-ops.js";
3
- import logicOps from "./logic-ops.js";
4
- import type Environment from "./environment.js";
5
- import type { FunctionMap } from "./builtin-functions.js";
6
-
7
- type AnyNode = Node & Record<string, any>;
8
-
9
- // Properties that enable sandbox escape (prototype chain → Function constructor)
10
- // or prototype pollution. Blocked in both MemberExpression and deep path access.
11
- const BLOCKED_PROPS = new Set([
12
- "constructor",
13
- "__proto__",
14
- "prototype",
15
- "__defineGetter__",
16
- "__defineSetter__",
17
- "__lookupGetter__",
18
- "__lookupSetter__",
19
- ]);
20
-
21
- function assertSafeProperty(prop: string): void {
22
- if (BLOCKED_PROPS.has(prop)) {
23
- throw new Error(`Access to property '${prop}' is not allowed`);
24
- }
25
- }
26
-
27
- // Execution step counter — reset via resetStepCounter() before each run.
28
- let _steps = 0;
29
- let _maxSteps = 0;
30
-
31
- export function resetStepCounter(maxSteps: number): void {
32
- _steps = 0;
33
- _maxSteps = maxSteps;
34
- }
35
-
36
- function loc(exp: AnyNode) {
37
- return exp.loc!.start;
38
- }
39
-
40
- function makeFunc(
41
- params: string[],
42
- body: AnyNode,
43
- env: Environment,
44
- functions: FunctionMap
45
- ) {
46
- const scope = env.extend();
47
- return (...args: any[]) => {
48
- params.forEach((param, idx) => {
49
- scope.def(param, idx < args.length ? args[idx] : false);
50
- });
51
- return evaluate(body, scope, functions);
52
- };
53
- }
54
-
55
- const evals: Record<
56
- string,
57
- (exp: AnyNode, env: Environment, functions: FunctionMap) => any
58
- > = {
59
- ArrowFunctionExpression(exp, env, functions) {
60
- const params = exp.params.map((param: AnyNode) => evaluate(param));
61
- return makeFunc(params, exp.body, env, functions);
62
- },
63
-
64
- ArrayExpression(exp, env, functions) {
65
- const result: any[] = [];
66
- for (const el of exp.elements) {
67
- if (el && el.type === "SpreadElement") {
68
- const spread = evaluate(el.argument, env, functions);
69
- result.push(...spread);
70
- } else if (el) {
71
- result.push(evaluate(el, env, functions));
72
- }
73
- }
74
- return result;
75
- },
76
-
77
- AssignmentExpression(exp, env, functions) {
78
- const target = evaluate(exp.left);
79
- return env.set(target, evaluate(exp.right, env, functions), loc(exp));
80
- },
81
-
82
- BinaryExpression(exp, env, functions) {
83
- const left = evaluate(exp.left, env, functions);
84
- const right = evaluate(exp.right, env, functions);
85
- return binaryOps[exp.operator](left, right);
86
- },
87
-
88
- BlockStatement(exp, env, functions) {
89
- const scope = env.extend();
90
- let result;
91
- for (const part of exp.body) {
92
- result = evaluate(part, scope, functions);
93
- if (part.type === "ReturnStatement") {
94
- break;
95
- }
96
- }
97
- return result;
98
- },
99
-
100
- CallExpression(exp, env, functions) {
101
- const func = evaluate(exp.callee);
102
-
103
- // Collect args, handling spread
104
- const args: any[] = [];
105
- for (const arg of exp.arguments) {
106
- if (arg.type === "SpreadElement") {
107
- const spread = evaluate(arg.argument, env, functions);
108
- args.push(...spread);
109
- } else {
110
- args.push(evaluate(arg, env, functions));
111
- }
112
- }
113
-
114
- if (functions[func]) {
115
- return functions[func](...args);
116
- }
117
-
118
- if (env.defined(func)) {
119
- const customFunc = env.get(func, loc(exp));
120
- if (typeof customFunc === "function") {
121
- return customFunc(...args);
122
- }
123
- }
124
- const l = loc(exp);
125
- throw new Error(`${func} is not a function (${l.line}:${l.column})`);
126
- },
127
-
128
- ConditionalExpression(exp, env, functions) {
129
- const test = evaluate(exp.test, env, functions);
130
- return test
131
- ? evaluate(exp.consequent, env, functions)
132
- : evaluate(exp.alternate, env, functions);
133
- },
134
-
135
- ExpressionStatement(exp, env, functions) {
136
- return evaluate(exp.expression, env, functions);
137
- },
138
-
139
- FunctionDeclaration(exp, env, functions) {
140
- const name = evaluate(exp.id);
141
- const params = exp.params.map((param: AnyNode) => evaluate(param));
142
- env.set(name, makeFunc(params, exp.body, env, functions), loc(exp));
143
- },
144
-
145
- Identifier(exp, env) {
146
- if (env) {
147
- return env.get(exp.name, loc(exp));
148
- }
149
- return exp.name;
150
- },
151
-
152
- IfStatement(exp, env, functions) {
153
- const test = evaluate(exp.test, env, functions);
154
- if (test) {
155
- return evaluate(exp.consequent, env, functions);
156
- }
157
- if (exp.alternate) {
158
- return evaluate(exp.alternate, env, functions);
159
- }
160
- },
161
-
162
- Literal(exp) {
163
- return exp.value;
164
- },
165
-
166
- LogicalExpression(exp, env, functions) {
167
- const left = evaluate(exp.left, env, functions);
168
- const right = evaluate(exp.right, env, functions);
169
- return logicOps[exp.operator](left, right);
170
- },
171
-
172
- MemberExpression(exp, env, functions) {
173
- if (!env) {
174
- const obj = evaluate(exp.object);
175
- const prop = evaluate(exp.property);
176
- assertSafeProperty(prop);
177
- return `${obj}.${prop}`;
178
- }
179
- const obj = evaluate(exp.object, env, functions);
180
- if (exp.computed) {
181
- const prop = evaluate(exp.property, env, functions);
182
- assertSafeProperty(String(prop));
183
- return obj[prop];
184
- }
185
- const prop = evaluate(exp.property);
186
- assertSafeProperty(prop);
187
- return obj[prop];
188
- },
189
-
190
- ObjectExpression(exp, env, functions) {
191
- const result: Record<string, any> = {};
192
- for (const prop of exp.properties) {
193
- if (prop.type === "SpreadElement") {
194
- const spread = evaluate(prop.argument, env, functions);
195
- Object.assign(result, spread);
196
- } else {
197
- // Property — key can be Identifier or Literal
198
- const key =
199
- prop.key.type === "Identifier" ? prop.key.name : prop.key.value;
200
- result[key] = evaluate(prop.value, env, functions);
201
- }
202
- }
203
- return result;
204
- },
205
-
206
- Program(exp, env, functions) {
207
- let result;
208
- for (const node of exp.body) {
209
- result = evaluate(node, env, functions);
210
- }
211
- return result;
212
- },
213
-
214
- ReturnStatement(exp, env, functions) {
215
- return evaluate(exp.argument, env, functions);
216
- },
217
-
218
- TemplateLiteral(exp, env, functions) {
219
- let result = "";
220
- for (let i = 0; i < exp.quasis.length; i++) {
221
- result += exp.quasis[i].value.cooked;
222
- if (i < exp.expressions.length) {
223
- result += String(evaluate(exp.expressions[i], env, functions));
224
- }
225
- }
226
- return result;
227
- },
228
-
229
- UnaryExpression(exp, env, functions) {
230
- const arg = evaluate(exp.argument, env, functions);
231
- switch (exp.operator) {
232
- case "!":
233
- return !arg;
234
- case "-":
235
- return -arg;
236
- case "+":
237
- return +arg;
238
- case "typeof":
239
- return typeof arg;
240
- default:
241
- throw new Error(`Unsupported unary operator: ${exp.operator}`);
242
- }
243
- },
244
-
245
- VariableDeclaration(exp, env, functions) {
246
- if (exp.kind === "var") {
247
- const globalScope = env.getGlobalScope();
248
- for (const dec of exp.declarations) {
249
- evaluate(dec, globalScope, functions);
250
- }
251
- return;
252
- }
253
- for (const dec of exp.declarations) {
254
- evaluate(dec, env, functions);
255
- }
256
- },
257
-
258
- VariableDeclarator(exp, env, functions) {
259
- const name = evaluate(exp.id);
260
- const value = exp.init ? evaluate(exp.init, env, functions) : undefined;
261
- return env.def(name, value);
262
- },
263
- };
264
-
265
- function evaluate(exp: AnyNode, env?: any, functions?: any): any {
266
- if (_maxSteps > 0 && ++_steps > _maxSteps) {
267
- throw new Error("Execution limit exceeded");
268
- }
269
- if (evals[exp.type]) {
270
- return evals[exp.type](exp, env, functions);
271
- }
272
- throw new Error("Cannot evaluate " + exp.type);
273
- }
274
-
275
- export default evaluate;
package/src/index.ts DELETED
@@ -1,42 +0,0 @@
1
- import * as acorn from "acorn";
2
- import evaluate, { resetStepCounter } from "./evaluate.js";
3
- import builtinFuncs from "./builtin-functions.js";
4
- import type { FunctionMap } from "./builtin-functions.js";
5
- import Environment from "./environment.js";
6
-
7
- function parse(code: string) {
8
- return acorn.parse(code, {
9
- ecmaVersion: "latest",
10
- sourceType: "script",
11
- locations: true,
12
- });
13
- }
14
-
15
- export interface RunnerOptions {
16
- functions?: FunctionMap;
17
- maxSteps?: number;
18
- }
19
-
20
- export interface RunResult {
21
- result: any;
22
- env: Record<string, any>;
23
- }
24
-
25
- const DEFAULT_MAX_STEPS = 100_000;
26
-
27
- export default function runner({ functions = builtinFuncs, maxSteps = DEFAULT_MAX_STEPS }: RunnerOptions = {}) {
28
- return (code: string, env: Record<string, any> = {}, debug?: boolean): RunResult => {
29
- const ast = parse(code);
30
- debug && console.log(JSON.stringify(ast.body, null, 2));
31
- resetStepCounter(maxSteps);
32
- const globalEnv = new Environment();
33
- Object.keys(env).forEach((key) => {
34
- globalEnv.def(key, env[key]);
35
- });
36
- const result = evaluate(ast, globalEnv, functions);
37
- return { result, env: globalEnv.vars };
38
- };
39
- }
40
-
41
- export { Environment };
42
- export type { FunctionMap };
package/src/logic-ops.ts DELETED
@@ -1,6 +0,0 @@
1
- const logicOps: Record<string, (l: unknown, r: unknown) => unknown> = {
2
- "||": (l: any, r: any) => l || r,
3
- "&&": (l: any, r: any) => l && r,
4
- };
5
-
6
- export default logicOps;
package/tsconfig.json DELETED
@@ -1,16 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2020",
4
- "module": "ES2020",
5
- "moduleResolution": "bundler",
6
- "declaration": true,
7
- "outDir": "dist",
8
- "rootDir": "src",
9
- "strict": true,
10
- "esModuleInterop": true,
11
- "skipLibCheck": true,
12
- "forceConsistentCasingInFileNames": true
13
- },
14
- "include": ["src"],
15
- "exclude": ["src/__tests__"]
16
- }