scriptable 0.0.5 → 0.1.0
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/.claude/settings.local.json +10 -0
- package/README.md +139 -0
- package/dist/binary-ops.d.ts +2 -0
- package/dist/binary-ops.js +16 -0
- package/dist/builtin-functions.d.ts +3 -0
- package/dist/builtin-functions.js +151 -0
- package/dist/environment.d.ts +16 -0
- package/dist/environment.js +111 -0
- package/dist/evaluate.d.ts +5 -0
- package/dist/evaluate.js +232 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +26 -0
- package/dist/logic-ops.d.ts +2 -0
- package/dist/logic-ops.js +5 -0
- package/package.json +17 -50
- package/src/__tests__/{break-out.js → break-out.test.ts} +4 -15
- package/src/__tests__/{builtin-functions-date.js → builtin-functions-date.test.ts} +30 -11
- package/src/__tests__/{builtin-functions.js → builtin-functions.test.ts} +2 -3
- package/src/__tests__/{expressions.js → expressions.test.ts} +2 -3
- package/src/__tests__/{functions.js → functions.test.ts} +7 -8
- package/src/__tests__/{logic.js → logic.test.ts} +2 -3
- package/src/__tests__/new-features.test.ts +158 -0
- package/src/__tests__/security.test.ts +399 -0
- package/src/__tests__/{variables.js → variables.test.ts} +99 -7
- package/src/binary-ops.ts +18 -0
- package/src/builtin-functions.ts +177 -0
- package/src/environment.ts +132 -0
- package/src/evaluate.ts +275 -0
- package/src/index.ts +42 -0
- package/src/logic-ops.ts +6 -0
- package/tsconfig.json +16 -0
- package/.npmignore +0 -1
- package/src/__tests__/index.js +0 -85
- package/src/binary-ops.js +0 -13
- package/src/builtin-functions.js +0 -58
- package/src/environment.js +0 -60
- package/src/evaluate.js +0 -152
- package/src/index.js +0 -26
- package/src/logic-ops.js +0 -6
- package/yarn.lock +0 -2632
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import runner from "../index.js";
|
|
3
|
+
|
|
4
|
+
const run = runner();
|
|
5
|
+
|
|
6
|
+
// ==========================================================================
|
|
7
|
+
// 1. Sandbox escape — prototype chain to Function constructor
|
|
8
|
+
// ==========================================================================
|
|
9
|
+
|
|
10
|
+
describe("sandbox escape: prototype chain", () => {
|
|
11
|
+
it("blocks obj.constructor", () => {
|
|
12
|
+
const code = `a = {}; a.constructor`;
|
|
13
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("blocks obj.constructor.constructor (Function access)", () => {
|
|
17
|
+
const code = `a = {}; a.constructor.constructor`;
|
|
18
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("blocks array.constructor.constructor", () => {
|
|
22
|
+
const code = `a = [1,2]; a.constructor.constructor("return this")()`;
|
|
23
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("blocks string.constructor", () => {
|
|
27
|
+
const code = `a = "hello"; a.constructor`;
|
|
28
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("blocks number.constructor", () => {
|
|
32
|
+
// Wrap in parens to avoid parser ambiguity
|
|
33
|
+
const code = `a = 42; a.constructor`;
|
|
34
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("blocks function.constructor (reaches Function)", () => {
|
|
38
|
+
const code = `
|
|
39
|
+
f = (x) => x;
|
|
40
|
+
f.constructor("return this")();
|
|
41
|
+
`;
|
|
42
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("blocks constructor via computed property", () => {
|
|
46
|
+
const code = `a = {}; a["constructor"]`;
|
|
47
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("blocks constructor via computed string concatenation", () => {
|
|
51
|
+
const code = `a = {}; a["const" + "ructor"]`;
|
|
52
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("blocks constructor via variable indirection", () => {
|
|
56
|
+
const code = `
|
|
57
|
+
a = {};
|
|
58
|
+
key = "constructor";
|
|
59
|
+
a[key];
|
|
60
|
+
`;
|
|
61
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// ==========================================================================
|
|
66
|
+
// 2. Sandbox escape — __proto__ access
|
|
67
|
+
// ==========================================================================
|
|
68
|
+
|
|
69
|
+
describe("sandbox escape: __proto__", () => {
|
|
70
|
+
it("blocks __proto__ read", () => {
|
|
71
|
+
const code = `a = {}; a.__proto__`;
|
|
72
|
+
expect(() => run(code)).toThrow("Access to property '__proto__' is not allowed");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("blocks __proto__ computed read", () => {
|
|
76
|
+
const code = `a = {}; a["__proto__"]`;
|
|
77
|
+
expect(() => run(code)).toThrow("Access to property '__proto__' is not allowed");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("blocks __proto__ assignment", () => {
|
|
81
|
+
const code = `a = {}; a.__proto__ = {}`;
|
|
82
|
+
expect(() => run(code)).toThrow("Access to property '__proto__' is not allowed");
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// ==========================================================================
|
|
87
|
+
// 3. Sandbox escape — prototype access
|
|
88
|
+
// ==========================================================================
|
|
89
|
+
|
|
90
|
+
describe("sandbox escape: prototype", () => {
|
|
91
|
+
it("blocks .prototype access", () => {
|
|
92
|
+
const code = `
|
|
93
|
+
a = {};
|
|
94
|
+
f = a.constructor;
|
|
95
|
+
`;
|
|
96
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("blocks .prototype via computed", () => {
|
|
100
|
+
const code = `a = {}; a["prototype"]`;
|
|
101
|
+
expect(() => run(code)).toThrow("Access to property 'prototype' is not allowed");
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// ==========================================================================
|
|
106
|
+
// 4. Prototype pollution via deep assignment
|
|
107
|
+
// ==========================================================================
|
|
108
|
+
|
|
109
|
+
describe("prototype pollution via deep path assignment", () => {
|
|
110
|
+
it("blocks constructor in deep assignment path", () => {
|
|
111
|
+
const code = `foo.constructor.prototype.polluted = "yes"`;
|
|
112
|
+
expect(() => run(code, { foo: {} })).toThrow(
|
|
113
|
+
"Access to property 'constructor' is not allowed"
|
|
114
|
+
);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("blocks __proto__ in deep assignment path", () => {
|
|
118
|
+
const code = `foo.__proto__.polluted = "yes"`;
|
|
119
|
+
expect(() => run(code, { foo: {} })).toThrow(
|
|
120
|
+
"Access to property '__proto__' is not allowed"
|
|
121
|
+
);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("does not pollute Object.prototype even if attempted", () => {
|
|
125
|
+
// Verify no pollution leaked from prior tests
|
|
126
|
+
expect((Object.prototype as any).polluted).toBeUndefined();
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// ==========================================================================
|
|
131
|
+
// 5. Sandbox escape — legacy property descriptors
|
|
132
|
+
// ==========================================================================
|
|
133
|
+
|
|
134
|
+
describe("sandbox escape: legacy descriptor methods", () => {
|
|
135
|
+
it("blocks __defineGetter__", () => {
|
|
136
|
+
const code = `a = {}; a.__defineGetter__`;
|
|
137
|
+
expect(() => run(code)).toThrow(
|
|
138
|
+
"Access to property '__defineGetter__' is not allowed"
|
|
139
|
+
);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("blocks __defineSetter__", () => {
|
|
143
|
+
const code = `a = {}; a.__defineSetter__`;
|
|
144
|
+
expect(() => run(code)).toThrow(
|
|
145
|
+
"Access to property '__defineSetter__' is not allowed"
|
|
146
|
+
);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("blocks __lookupGetter__", () => {
|
|
150
|
+
const code = `a = {}; a.__lookupGetter__`;
|
|
151
|
+
expect(() => run(code)).toThrow(
|
|
152
|
+
"Access to property '__lookupGetter__' is not allowed"
|
|
153
|
+
);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("blocks __lookupSetter__", () => {
|
|
157
|
+
const code = `a = {}; a.__lookupSetter__`;
|
|
158
|
+
expect(() => run(code)).toThrow(
|
|
159
|
+
"Access to property '__lookupSetter__' is not allowed"
|
|
160
|
+
);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// ==========================================================================
|
|
165
|
+
// 6. Sandbox escape — this / arguments / globals
|
|
166
|
+
// ==========================================================================
|
|
167
|
+
|
|
168
|
+
describe("sandbox escape: forbidden expressions", () => {
|
|
169
|
+
it("blocks this", () => {
|
|
170
|
+
const code = `this`;
|
|
171
|
+
expect(() => run(code)).toThrow("Cannot evaluate ThisExpression");
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("blocks arguments keyword", () => {
|
|
175
|
+
const code = `
|
|
176
|
+
function f() { return arguments; }
|
|
177
|
+
f();
|
|
178
|
+
`;
|
|
179
|
+
expect(() => run(code)).toThrow('"arguments" is not defined');
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("cannot access process", () => {
|
|
183
|
+
const code = `process`;
|
|
184
|
+
expect(() => run(code)).toThrow('"process" is not defined');
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("cannot access require", () => {
|
|
188
|
+
const code = `require("fs")`;
|
|
189
|
+
expect(() => run(code)).toThrow("require is not a function");
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("cannot access globalThis", () => {
|
|
193
|
+
const code = `globalThis`;
|
|
194
|
+
expect(() => run(code)).toThrow('"globalThis" is not defined');
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("cannot access eval", () => {
|
|
198
|
+
const code = `eval("1+1")`;
|
|
199
|
+
expect(() => run(code)).toThrow("eval is not a function");
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("cannot access Function", () => {
|
|
203
|
+
const code = `Function("return 1")`;
|
|
204
|
+
expect(() => run(code)).toThrow("Function is not a function");
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("blocks import()", () => {
|
|
208
|
+
const code = `import("fs")`;
|
|
209
|
+
expect(() => run(code)).toThrow();
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("blocks new expressions", () => {
|
|
213
|
+
const code = `new Array(10)`;
|
|
214
|
+
expect(() => run(code)).toThrow("Cannot evaluate NewExpression");
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// ==========================================================================
|
|
219
|
+
// 7. DoS — infinite recursion
|
|
220
|
+
// ==========================================================================
|
|
221
|
+
|
|
222
|
+
describe("DoS: infinite recursion", () => {
|
|
223
|
+
// V8's call stack limit catches deep recursion before our step counter
|
|
224
|
+
// (each interpreter call creates many real stack frames). Either mechanism
|
|
225
|
+
// is an acceptable guard — the important thing is execution stops.
|
|
226
|
+
|
|
227
|
+
it("stops infinite recursive function", () => {
|
|
228
|
+
const code = `
|
|
229
|
+
function f() { return f(); }
|
|
230
|
+
f();
|
|
231
|
+
`;
|
|
232
|
+
expect(() => run(code)).toThrow();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("stops mutual recursion", () => {
|
|
236
|
+
const code = `
|
|
237
|
+
function a() { return b(); }
|
|
238
|
+
function b() { return a(); }
|
|
239
|
+
a();
|
|
240
|
+
`;
|
|
241
|
+
expect(() => run(code)).toThrow();
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("stops deep self-calling arrow", () => {
|
|
245
|
+
const code = `
|
|
246
|
+
f = (n) => f(n);
|
|
247
|
+
f(1);
|
|
248
|
+
`;
|
|
249
|
+
expect(() => run(code)).toThrow();
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("step counter catches recursion with low limit", () => {
|
|
253
|
+
const tightRun = runner({ maxSteps: 50 });
|
|
254
|
+
const code = `
|
|
255
|
+
function f(n) { if (n > 0) { return f(n - 1); } return 0; }
|
|
256
|
+
f(100);
|
|
257
|
+
`;
|
|
258
|
+
expect(() => tightRun(code)).toThrow("Execution limit exceeded");
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
// ==========================================================================
|
|
263
|
+
// 8. DoS — execution limit configurability
|
|
264
|
+
// ==========================================================================
|
|
265
|
+
|
|
266
|
+
describe("DoS: execution limits", () => {
|
|
267
|
+
it("respects custom maxSteps", () => {
|
|
268
|
+
const tightRun = runner({ maxSteps: 10 });
|
|
269
|
+
// Even simple code will exceed 10 steps
|
|
270
|
+
const code = `a = 1; b = 2; c = 3; d = 4; e = 5;`;
|
|
271
|
+
expect(() => tightRun(code)).toThrow("Execution limit exceeded");
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("allows disabling step limit with 0", () => {
|
|
275
|
+
const noLimitRun = runner({ maxSteps: 0 });
|
|
276
|
+
const code = `a = 1; b = 2; c = a + b;`;
|
|
277
|
+
const { env } = noLimitRun(code);
|
|
278
|
+
expect(env.c).toEqual(3);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("default limit allows normal-sized scripts", () => {
|
|
282
|
+
// Build a moderately complex script
|
|
283
|
+
let code = "";
|
|
284
|
+
for (let i = 0; i < 100; i++) {
|
|
285
|
+
code += `x${i} = ${i};\n`;
|
|
286
|
+
}
|
|
287
|
+
code += `result = x0 + x99;`;
|
|
288
|
+
const { env } = run(code);
|
|
289
|
+
expect(env.result).toEqual(99);
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
// ==========================================================================
|
|
294
|
+
// 9. Sandbox escape via builtin return values
|
|
295
|
+
// ==========================================================================
|
|
296
|
+
|
|
297
|
+
describe("sandbox escape: via builtin return values", () => {
|
|
298
|
+
it("blocks constructor on array returned by map", () => {
|
|
299
|
+
const code = `
|
|
300
|
+
result = map([1,2], (x) => x);
|
|
301
|
+
result.constructor;
|
|
302
|
+
`;
|
|
303
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it("blocks constructor on object returned by assign", () => {
|
|
307
|
+
const code = `
|
|
308
|
+
obj = assign({ a: 1 });
|
|
309
|
+
obj.constructor;
|
|
310
|
+
`;
|
|
311
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it("blocks constructor on string returned by join", () => {
|
|
315
|
+
const code = `
|
|
316
|
+
s = join(["a", "b"], ",");
|
|
317
|
+
s.constructor;
|
|
318
|
+
`;
|
|
319
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
// ==========================================================================
|
|
324
|
+
// 10. Sandbox escape — original break-out.js tests (preserved)
|
|
325
|
+
// ==========================================================================
|
|
326
|
+
|
|
327
|
+
describe("original break-out tests (preserved)", () => {
|
|
328
|
+
it("this.constructor.constructor", () => {
|
|
329
|
+
const code = `bar = this.constructor.constructor`;
|
|
330
|
+
expect(() => run(code)).toThrow("Cannot evaluate ThisExpression");
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it("object.constructor", () => {
|
|
334
|
+
const code = `
|
|
335
|
+
bar = { any: "thing" };
|
|
336
|
+
func = bar.constructor;
|
|
337
|
+
func2 = func("return process");
|
|
338
|
+
process = func2();
|
|
339
|
+
`;
|
|
340
|
+
expect(() => run(code)).toThrow(
|
|
341
|
+
"Access to property 'constructor' is not allowed"
|
|
342
|
+
);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it("arguments.callee.caller", () => {
|
|
346
|
+
const code = `
|
|
347
|
+
function getFunc () {
|
|
348
|
+
return arguments.callee.caller.arguments.callee.caller.arguments.callee.caller.arguments.callee.arguments.callee.caller;
|
|
349
|
+
}
|
|
350
|
+
func = getFunc();
|
|
351
|
+
func.call(func, "var fs = process.mainModule.require('fs');");
|
|
352
|
+
`;
|
|
353
|
+
expect(() => run(code)).toThrow(
|
|
354
|
+
new ReferenceError(`"arguments" is not defined (3:15)`)
|
|
355
|
+
);
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
// ==========================================================================
|
|
360
|
+
// 11. Safe property access still works
|
|
361
|
+
// ==========================================================================
|
|
362
|
+
|
|
363
|
+
describe("safe property access still works", () => {
|
|
364
|
+
it("array .length", () => {
|
|
365
|
+
const code = `[1, 2, 3].length`;
|
|
366
|
+
const { result } = run(code);
|
|
367
|
+
expect(result).toEqual(3);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
it("string .length", () => {
|
|
371
|
+
const code = `a = "hello"; a.length`;
|
|
372
|
+
const { result } = run(code);
|
|
373
|
+
expect(result).toEqual(5);
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
it("object property access", () => {
|
|
377
|
+
const code = `a = { foo: "bar" }; a.foo`;
|
|
378
|
+
const { result } = run(code);
|
|
379
|
+
expect(result).toEqual("bar");
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
it("nested object property access", () => {
|
|
383
|
+
const code = `a.b.c`;
|
|
384
|
+
const { result } = run(code, { a: { b: { c: 42 } } });
|
|
385
|
+
expect(result).toEqual(42);
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
it("computed property access with string", () => {
|
|
389
|
+
const code = `a = { foo: "bar" }; a["foo"]`;
|
|
390
|
+
const { result } = run(code);
|
|
391
|
+
expect(result).toEqual("bar");
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it("array index access", () => {
|
|
395
|
+
const code = `a = [10, 20, 30]; a[1]`;
|
|
396
|
+
const { result } = run(code);
|
|
397
|
+
expect(result).toEqual(20);
|
|
398
|
+
});
|
|
399
|
+
});
|
|
@@ -1,8 +1,105 @@
|
|
|
1
|
-
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import runner from "../index.js";
|
|
2
3
|
|
|
3
4
|
const run = runner();
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
describe("define global vars", () => {
|
|
7
|
+
it(`foo`, () => {
|
|
8
|
+
const code = `foo`;
|
|
9
|
+
const vars = {};
|
|
10
|
+
expect(() => run(code, vars)).toThrow(
|
|
11
|
+
new ReferenceError(`"foo" is not defined (1:0)`)
|
|
12
|
+
);
|
|
13
|
+
});
|
|
14
|
+
it("let foo", () => {
|
|
15
|
+
const code = `let foo;`;
|
|
16
|
+
const vars = {};
|
|
17
|
+
const { result, env } = run(code, vars);
|
|
18
|
+
expect(result).toEqual(undefined);
|
|
19
|
+
expect(env).toEqual({ foo: undefined });
|
|
20
|
+
});
|
|
21
|
+
it("const foo", () => {
|
|
22
|
+
const code = `const foo;`;
|
|
23
|
+
const vars = {};
|
|
24
|
+
expect(() => run(code, vars)).toThrow(SyntaxError);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it(`foo = "bar"`, () => {
|
|
28
|
+
const code = `foo = "bar";`;
|
|
29
|
+
const vars = {};
|
|
30
|
+
const { result, env } = run(code, vars);
|
|
31
|
+
expect(result).toEqual("bar");
|
|
32
|
+
expect(env).toEqual({ foo: "bar" });
|
|
33
|
+
});
|
|
34
|
+
it(`var foo = "bar"`, () => {
|
|
35
|
+
const code = `var foo = "bar";`;
|
|
36
|
+
const vars = {};
|
|
37
|
+
const { result, env } = run(code, vars);
|
|
38
|
+
expect(result).toEqual(undefined);
|
|
39
|
+
expect(env).toEqual({ foo: "bar" });
|
|
40
|
+
});
|
|
41
|
+
it(`let foo = "bar"`, () => {
|
|
42
|
+
const code = `let foo = "bar";`;
|
|
43
|
+
const vars = {};
|
|
44
|
+
const { result, env } = run(code, vars);
|
|
45
|
+
expect(result).toEqual(undefined);
|
|
46
|
+
expect(env).toEqual({ foo: "bar" });
|
|
47
|
+
});
|
|
48
|
+
it(`const foo = "bar"`, () => {
|
|
49
|
+
const code = `const foo = "bar";`;
|
|
50
|
+
const vars = {};
|
|
51
|
+
const { result, env } = run(code, vars);
|
|
52
|
+
expect(result).toEqual(undefined);
|
|
53
|
+
expect(env).toEqual({ foo: "bar" });
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("define vars in scope", () => {
|
|
58
|
+
it(`foo = "foo"`, () => {
|
|
59
|
+
const code = `
|
|
60
|
+
if (true) {
|
|
61
|
+
foo = "foo";
|
|
62
|
+
}
|
|
63
|
+
`;
|
|
64
|
+
const vars = {};
|
|
65
|
+
const { result, env } = run(code, vars);
|
|
66
|
+
expect(result).toEqual("foo");
|
|
67
|
+
expect(env).toEqual({ foo: "foo" });
|
|
68
|
+
});
|
|
69
|
+
it(`var foo = "foo"`, () => {
|
|
70
|
+
const code = `
|
|
71
|
+
if (true) {
|
|
72
|
+
var foo = "foo";
|
|
73
|
+
}
|
|
74
|
+
`;
|
|
75
|
+
const vars = {};
|
|
76
|
+
const { result, env } = run(code, vars);
|
|
77
|
+
expect(result).toEqual(undefined);
|
|
78
|
+
expect(env).toEqual({ foo: "foo" });
|
|
79
|
+
});
|
|
80
|
+
it(`let foo = "foo"`, () => {
|
|
81
|
+
const code = `
|
|
82
|
+
if (true) {
|
|
83
|
+
let foo = "foo";
|
|
84
|
+
}
|
|
85
|
+
`;
|
|
86
|
+
const vars = {};
|
|
87
|
+
const { result, env } = run(code, vars);
|
|
88
|
+
expect(result).toEqual(undefined);
|
|
89
|
+
expect(env).toEqual({ foo: undefined });
|
|
90
|
+
});
|
|
91
|
+
it(`const foo = "foo"`, () => {
|
|
92
|
+
const code = `
|
|
93
|
+
if (true) {
|
|
94
|
+
const foo = "foo";
|
|
95
|
+
}
|
|
96
|
+
`;
|
|
97
|
+
const vars = {};
|
|
98
|
+
const { result, env } = run(code, vars);
|
|
99
|
+
expect(result).toEqual(undefined);
|
|
100
|
+
expect(env).toEqual({ foo: undefined });
|
|
101
|
+
});
|
|
102
|
+
});
|
|
6
103
|
|
|
7
104
|
describe("bare variables eval", () => {
|
|
8
105
|
it("evals a bare var", () => {
|
|
@@ -10,7 +107,6 @@ describe("bare variables eval", () => {
|
|
|
10
107
|
const env = { foo: "bar" };
|
|
11
108
|
const { result } = run(code, env);
|
|
12
109
|
expect(result).toEqual("bar");
|
|
13
|
-
expect(env).toEqual({ foo: "bar" });
|
|
14
110
|
});
|
|
15
111
|
|
|
16
112
|
it("evals a deep bare var", () => {
|
|
@@ -18,7 +114,6 @@ describe("bare variables eval", () => {
|
|
|
18
114
|
const env = { foo: { bar: "baz" } };
|
|
19
115
|
const { result } = run(code, env);
|
|
20
116
|
expect(result).toEqual("baz");
|
|
21
|
-
expect(env).toEqual({ foo: { bar: "baz" } });
|
|
22
117
|
});
|
|
23
118
|
|
|
24
119
|
it("evals an array index", () => {
|
|
@@ -33,7 +128,6 @@ describe("bare variables eval", () => {
|
|
|
33
128
|
const env = { foo: { bar: { baz: "fiz" } } };
|
|
34
129
|
const { result } = run(code, env);
|
|
35
130
|
expect(result).toEqual("fiz");
|
|
36
|
-
expect(env).toEqual({ foo: { bar: { baz: "fiz" } } });
|
|
37
131
|
});
|
|
38
132
|
|
|
39
133
|
it("evals a dotted deep bare var", () => {
|
|
@@ -41,7 +135,6 @@ describe("bare variables eval", () => {
|
|
|
41
135
|
const env = { foo: { bar: "baz" } };
|
|
42
136
|
const { result } = run(code, env);
|
|
43
137
|
expect(result).toEqual("baz");
|
|
44
|
-
expect(env).toEqual({ foo: { bar: "baz" } });
|
|
45
138
|
});
|
|
46
139
|
|
|
47
140
|
it("evals a deeper bare var", () => {
|
|
@@ -49,7 +142,6 @@ describe("bare variables eval", () => {
|
|
|
49
142
|
const env = { foo: { bar: { baz: "fiz" } } };
|
|
50
143
|
const { result } = run(code, env);
|
|
51
144
|
expect(result).toEqual("fiz");
|
|
52
|
-
expect(env).toEqual({ foo: { bar: { baz: "fiz" } } });
|
|
53
145
|
});
|
|
54
146
|
});
|
|
55
147
|
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const binaryOps: Record<string, (l: unknown, r: unknown) => unknown> = {
|
|
2
|
+
"+": (l: any, r: any) => l + r,
|
|
3
|
+
"-": (l: any, r: any) => l - r,
|
|
4
|
+
"*": (l: any, r: any) => l * r,
|
|
5
|
+
"/": (l: any, r: any) => l / r,
|
|
6
|
+
"%": (l: any, r: any) => l % r,
|
|
7
|
+
|
|
8
|
+
">": (l: any, r: any) => l > r,
|
|
9
|
+
"<": (l: any, r: any) => l < r,
|
|
10
|
+
">=": (l: any, r: any) => l >= r,
|
|
11
|
+
"<=": (l: any, r: any) => l <= r,
|
|
12
|
+
"==": (l: any, r: any) => l === r,
|
|
13
|
+
"!=": (l: any, r: any) => l !== r,
|
|
14
|
+
"===": (l: any, r: any) => l === r,
|
|
15
|
+
"!==": (l: any, r: any) => l !== r,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default binaryOps;
|