@enclave-vm/core 2.14.0 → 2.14.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/esm/worker.mjs +682 -0
- package/package.json +4 -3
- package/src/adapters/worker-pool/package.json +4 -3
- package/worker.d.ts +1 -1
- package/worker.js +722 -0
package/esm/worker.mjs
ADDED
|
@@ -0,0 +1,682 @@
|
|
|
1
|
+
// libs/core/src/adapters/interpreter-adapter.ts
|
|
2
|
+
import * as acorn from "acorn";
|
|
3
|
+
|
|
4
|
+
// libs/core/src/interpreter/interpreter.ts
|
|
5
|
+
var BLOCKED_KEYS = /* @__PURE__ */ new Set([
|
|
6
|
+
"__proto__",
|
|
7
|
+
"prototype",
|
|
8
|
+
"constructor",
|
|
9
|
+
"__defineGetter__",
|
|
10
|
+
"__defineSetter__",
|
|
11
|
+
"__lookupGetter__",
|
|
12
|
+
"__lookupSetter__"
|
|
13
|
+
]);
|
|
14
|
+
var MAX_STRING_OP_LENGTH = 1e6;
|
|
15
|
+
var HIGHER_ORDER_ARRAY_METHODS = /* @__PURE__ */ new Set([
|
|
16
|
+
"map",
|
|
17
|
+
"flatMap",
|
|
18
|
+
"filter",
|
|
19
|
+
"forEach",
|
|
20
|
+
"some",
|
|
21
|
+
"every",
|
|
22
|
+
"find",
|
|
23
|
+
"findIndex",
|
|
24
|
+
"reduce",
|
|
25
|
+
"sort"
|
|
26
|
+
]);
|
|
27
|
+
var StepLimitError = class extends Error {
|
|
28
|
+
constructor(limit) {
|
|
29
|
+
super(`Execution step limit exceeded (${limit})`);
|
|
30
|
+
this.name = "StepLimitError";
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var InterpreterError = class extends Error {
|
|
34
|
+
constructor(message) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.name = "InterpreterError";
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
var ReturnSignal = class {
|
|
40
|
+
constructor(value) {
|
|
41
|
+
this.value = value;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
var BreakSignal = class {
|
|
45
|
+
};
|
|
46
|
+
var ContinueSignal = class {
|
|
47
|
+
};
|
|
48
|
+
var Scope = class _Scope {
|
|
49
|
+
constructor(parent) {
|
|
50
|
+
this.parent = parent;
|
|
51
|
+
}
|
|
52
|
+
vars = /* @__PURE__ */ new Map();
|
|
53
|
+
child() {
|
|
54
|
+
return new _Scope(this);
|
|
55
|
+
}
|
|
56
|
+
declare(name, value, mutable) {
|
|
57
|
+
this.vars.set(name, { value, mutable });
|
|
58
|
+
}
|
|
59
|
+
has(name) {
|
|
60
|
+
return this.vars.has(name) || (this.parent?.has(name) ?? false);
|
|
61
|
+
}
|
|
62
|
+
get(name) {
|
|
63
|
+
const slot = this.vars.get(name);
|
|
64
|
+
if (slot) return slot.value;
|
|
65
|
+
if (this.parent) return this.parent.get(name);
|
|
66
|
+
throw new InterpreterError(`'${name}' is not defined`);
|
|
67
|
+
}
|
|
68
|
+
set(name, value) {
|
|
69
|
+
const slot = this.vars.get(name);
|
|
70
|
+
if (slot) {
|
|
71
|
+
if (!slot.mutable) throw new InterpreterError(`Assignment to constant '${name}'`);
|
|
72
|
+
slot.value = value;
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (this.parent) {
|
|
76
|
+
this.parent.set(name, value);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
throw new InterpreterError(`'${name}' is not defined`);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
var Interpreter = class {
|
|
83
|
+
constructor(options) {
|
|
84
|
+
this.options = options;
|
|
85
|
+
this.globals = options.globals;
|
|
86
|
+
}
|
|
87
|
+
steps = 0;
|
|
88
|
+
callDepth = 0;
|
|
89
|
+
globals;
|
|
90
|
+
/** Number of AST nodes evaluated so far (for execution stats). */
|
|
91
|
+
get stepCount() {
|
|
92
|
+
return this.steps;
|
|
93
|
+
}
|
|
94
|
+
async run(program) {
|
|
95
|
+
const root = new Scope();
|
|
96
|
+
for (const stmt of program.body) {
|
|
97
|
+
if (stmt.type === "FunctionDeclaration" && stmt.id) {
|
|
98
|
+
root.declare(stmt.id.name, this.makeFunction(stmt, root), false);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
let last;
|
|
102
|
+
for (const stmt of program.body) {
|
|
103
|
+
if (stmt.type === "FunctionDeclaration") continue;
|
|
104
|
+
last = await this.execStatement(stmt, root);
|
|
105
|
+
}
|
|
106
|
+
if (root.has("__ag_main")) {
|
|
107
|
+
const main = root.get("__ag_main");
|
|
108
|
+
if (typeof main === "function") return main();
|
|
109
|
+
}
|
|
110
|
+
return last;
|
|
111
|
+
}
|
|
112
|
+
tick() {
|
|
113
|
+
if (this.options.signal?.aborted) throw new InterpreterError("Execution aborted");
|
|
114
|
+
if (++this.steps > this.options.maxSteps) throw new StepLimitError(this.options.maxSteps);
|
|
115
|
+
}
|
|
116
|
+
// ── Statements ─────────────────────────────────────────────────────────────
|
|
117
|
+
async execStatement(node, scope) {
|
|
118
|
+
this.tick();
|
|
119
|
+
switch (node.type) {
|
|
120
|
+
case "VariableDeclaration": {
|
|
121
|
+
for (const d of node.declarations) {
|
|
122
|
+
const value = d.init ? await this.evalExpr(d.init, scope) : void 0;
|
|
123
|
+
this.bindPattern(d.id, value, scope, node.kind !== "const");
|
|
124
|
+
}
|
|
125
|
+
return void 0;
|
|
126
|
+
}
|
|
127
|
+
case "FunctionDeclaration":
|
|
128
|
+
if (node.id) scope.declare(node.id.name, this.makeFunction(node, scope), false);
|
|
129
|
+
return void 0;
|
|
130
|
+
case "ExpressionStatement":
|
|
131
|
+
return this.evalExpr(node.expression, scope);
|
|
132
|
+
case "BlockStatement":
|
|
133
|
+
return this.execBlock(node.body, scope.child());
|
|
134
|
+
case "IfStatement":
|
|
135
|
+
if (truthy(await this.evalExpr(node.test, scope))) return this.execStatement(node.consequent, scope);
|
|
136
|
+
if (node.alternate) return this.execStatement(node.alternate, scope);
|
|
137
|
+
return void 0;
|
|
138
|
+
case "ForOfStatement":
|
|
139
|
+
return this.execForOf(node, scope);
|
|
140
|
+
case "ForStatement":
|
|
141
|
+
return this.execFor(node, scope);
|
|
142
|
+
case "WhileStatement":
|
|
143
|
+
return this.execWhile(node, scope);
|
|
144
|
+
case "ReturnStatement":
|
|
145
|
+
throw new ReturnSignal(node.argument ? await this.evalExpr(node.argument, scope) : void 0);
|
|
146
|
+
case "BreakStatement":
|
|
147
|
+
throw new BreakSignal();
|
|
148
|
+
case "ContinueStatement":
|
|
149
|
+
throw new ContinueSignal();
|
|
150
|
+
case "EmptyStatement":
|
|
151
|
+
return void 0;
|
|
152
|
+
default:
|
|
153
|
+
throw new InterpreterError(`Unsupported statement: ${node.type}`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async execBlock(body, scope) {
|
|
157
|
+
for (const stmt of body) {
|
|
158
|
+
if (stmt.type === "FunctionDeclaration" && stmt.id) {
|
|
159
|
+
scope.declare(stmt.id.name, this.makeFunction(stmt, scope), false);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
for (const stmt of body) {
|
|
163
|
+
if (stmt.type === "FunctionDeclaration") continue;
|
|
164
|
+
await this.execStatement(stmt, scope);
|
|
165
|
+
}
|
|
166
|
+
return void 0;
|
|
167
|
+
}
|
|
168
|
+
async execForOf(node, scope) {
|
|
169
|
+
const iterable = await this.evalExpr(node.right, scope);
|
|
170
|
+
if (!isIterable(iterable)) throw new InterpreterError("for-of target is not iterable");
|
|
171
|
+
for (const item of iterable) {
|
|
172
|
+
this.tick();
|
|
173
|
+
const inner = scope.child();
|
|
174
|
+
const decl = node.left;
|
|
175
|
+
if (decl.type === "VariableDeclaration") {
|
|
176
|
+
this.bindPattern(decl.declarations[0].id, item, inner, decl.kind !== "const");
|
|
177
|
+
} else {
|
|
178
|
+
this.assignTo(decl, item, inner);
|
|
179
|
+
}
|
|
180
|
+
try {
|
|
181
|
+
await this.execStatement(node.body, inner);
|
|
182
|
+
} catch (e) {
|
|
183
|
+
if (e instanceof BreakSignal) break;
|
|
184
|
+
if (e instanceof ContinueSignal) continue;
|
|
185
|
+
throw e;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
async execFor(node, scope) {
|
|
190
|
+
const inner = scope.child();
|
|
191
|
+
if (node.init) {
|
|
192
|
+
if (node.init.type === "VariableDeclaration") await this.execStatement(node.init, inner);
|
|
193
|
+
else await this.evalExpr(node.init, inner);
|
|
194
|
+
}
|
|
195
|
+
while (node.test ? truthy(await this.evalExpr(node.test, inner)) : true) {
|
|
196
|
+
this.tick();
|
|
197
|
+
try {
|
|
198
|
+
await this.execStatement(node.body, inner.child());
|
|
199
|
+
} catch (e) {
|
|
200
|
+
if (e instanceof BreakSignal) break;
|
|
201
|
+
if (!(e instanceof ContinueSignal)) throw e;
|
|
202
|
+
}
|
|
203
|
+
if (node.update) await this.evalExpr(node.update, inner);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
async execWhile(node, scope) {
|
|
207
|
+
while (truthy(await this.evalExpr(node.test, scope))) {
|
|
208
|
+
this.tick();
|
|
209
|
+
try {
|
|
210
|
+
await this.execStatement(node.body, scope.child());
|
|
211
|
+
} catch (e) {
|
|
212
|
+
if (e instanceof BreakSignal) break;
|
|
213
|
+
if (!(e instanceof ContinueSignal)) throw e;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
// ── Expressions ──────────────────────────────────────────────────────────--
|
|
218
|
+
async evalExpr(node, scope) {
|
|
219
|
+
this.tick();
|
|
220
|
+
switch (node.type) {
|
|
221
|
+
case "Literal":
|
|
222
|
+
return node.value;
|
|
223
|
+
case "Identifier":
|
|
224
|
+
return scope.has(node.name) ? scope.get(node.name) : this.readGlobal(node.name);
|
|
225
|
+
case "TemplateLiteral":
|
|
226
|
+
return this.evalTemplate(node, scope);
|
|
227
|
+
case "ArrayExpression": {
|
|
228
|
+
const arr = [];
|
|
229
|
+
for (const el of node.elements) {
|
|
230
|
+
if (el == null) {
|
|
231
|
+
arr.push(void 0);
|
|
232
|
+
} else if (el.type === "SpreadElement") {
|
|
233
|
+
const spread = await this.evalExpr(el.argument, scope);
|
|
234
|
+
if (!isIterable(spread)) throw new InterpreterError("Spread target is not iterable");
|
|
235
|
+
for (const v of spread) arr.push(v);
|
|
236
|
+
} else {
|
|
237
|
+
arr.push(await this.evalExpr(el, scope));
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return arr;
|
|
241
|
+
}
|
|
242
|
+
case "ObjectExpression": {
|
|
243
|
+
const obj = {};
|
|
244
|
+
for (const prop of node.properties) {
|
|
245
|
+
if (prop.type === "SpreadElement") {
|
|
246
|
+
const spread = await this.evalExpr(prop.argument, scope);
|
|
247
|
+
if (spread && typeof spread === "object") {
|
|
248
|
+
for (const [k, v] of Object.entries(spread)) {
|
|
249
|
+
if (!BLOCKED_KEYS.has(k)) obj[k] = v;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
const key = await this.propKey(prop, scope);
|
|
255
|
+
if (BLOCKED_KEYS.has(key)) throw new InterpreterError(`Forbidden property key: ${key}`);
|
|
256
|
+
obj[key] = await this.evalExpr(prop.value, scope);
|
|
257
|
+
}
|
|
258
|
+
return obj;
|
|
259
|
+
}
|
|
260
|
+
case "UnaryExpression": {
|
|
261
|
+
const v = await this.evalExpr(node.argument, scope);
|
|
262
|
+
switch (node.operator) {
|
|
263
|
+
case "!":
|
|
264
|
+
return !truthy(v);
|
|
265
|
+
case "-":
|
|
266
|
+
return -v;
|
|
267
|
+
case "+":
|
|
268
|
+
return +v;
|
|
269
|
+
case "typeof":
|
|
270
|
+
return typeof v;
|
|
271
|
+
case "~":
|
|
272
|
+
return ~v;
|
|
273
|
+
default:
|
|
274
|
+
throw new InterpreterError(`Unsupported unary operator: ${node.operator}`);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
case "BinaryExpression":
|
|
278
|
+
return this.evalBinary(node, scope);
|
|
279
|
+
case "LogicalExpression": {
|
|
280
|
+
const left = await this.evalExpr(node.left, scope);
|
|
281
|
+
if (node.operator === "&&") return truthy(left) ? this.evalExpr(node.right, scope) : left;
|
|
282
|
+
if (node.operator === "||") return truthy(left) ? left : this.evalExpr(node.right, scope);
|
|
283
|
+
return left ?? await this.evalExpr(node.right, scope);
|
|
284
|
+
}
|
|
285
|
+
case "ConditionalExpression":
|
|
286
|
+
return truthy(await this.evalExpr(node.test, scope)) ? this.evalExpr(node.consequent, scope) : this.evalExpr(node.alternate, scope);
|
|
287
|
+
case "MemberExpression":
|
|
288
|
+
return (await this.evalMember(node, scope)).value;
|
|
289
|
+
case "CallExpression":
|
|
290
|
+
return this.evalCall(node, scope);
|
|
291
|
+
case "AwaitExpression":
|
|
292
|
+
return await this.evalExpr(node.argument, scope);
|
|
293
|
+
case "AssignmentExpression":
|
|
294
|
+
return this.evalAssignment(node, scope);
|
|
295
|
+
case "ArrowFunctionExpression":
|
|
296
|
+
case "FunctionExpression":
|
|
297
|
+
return this.makeFunction(node, scope);
|
|
298
|
+
default:
|
|
299
|
+
throw new InterpreterError(`Unsupported expression: ${node.type}`);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
async evalTemplate(node, scope) {
|
|
303
|
+
let out = "";
|
|
304
|
+
for (let i = 0; i < node.quasis.length; i++) {
|
|
305
|
+
out += node.quasis[i].value.cooked ?? "";
|
|
306
|
+
if (i < node.expressions.length) out += String(await this.evalExpr(node.expressions[i], scope));
|
|
307
|
+
}
|
|
308
|
+
return out;
|
|
309
|
+
}
|
|
310
|
+
async evalBinary(node, scope) {
|
|
311
|
+
const l = await this.evalExpr(node.left, scope);
|
|
312
|
+
const r = await this.evalExpr(node.right, scope);
|
|
313
|
+
switch (node.operator) {
|
|
314
|
+
case "+":
|
|
315
|
+
return l + r;
|
|
316
|
+
case "-":
|
|
317
|
+
return l - r;
|
|
318
|
+
case "*":
|
|
319
|
+
return l * r;
|
|
320
|
+
case "/":
|
|
321
|
+
return l / r;
|
|
322
|
+
case "%":
|
|
323
|
+
return l % r;
|
|
324
|
+
case "**":
|
|
325
|
+
return l ** r;
|
|
326
|
+
case "==":
|
|
327
|
+
return l == r;
|
|
328
|
+
case "!=":
|
|
329
|
+
return l != r;
|
|
330
|
+
case "===":
|
|
331
|
+
return l === r;
|
|
332
|
+
case "!==":
|
|
333
|
+
return l !== r;
|
|
334
|
+
case "<":
|
|
335
|
+
return l < r;
|
|
336
|
+
case "<=":
|
|
337
|
+
return l <= r;
|
|
338
|
+
case ">":
|
|
339
|
+
return l > r;
|
|
340
|
+
case ">=":
|
|
341
|
+
return l >= r;
|
|
342
|
+
case "&":
|
|
343
|
+
return l & r;
|
|
344
|
+
case "|":
|
|
345
|
+
return l | r;
|
|
346
|
+
case "^":
|
|
347
|
+
return l ^ r;
|
|
348
|
+
case "<<":
|
|
349
|
+
return l << r;
|
|
350
|
+
case ">>":
|
|
351
|
+
return l >> r;
|
|
352
|
+
case ">>>":
|
|
353
|
+
return l >>> r;
|
|
354
|
+
default:
|
|
355
|
+
throw new InterpreterError(`Unsupported binary operator: ${node.operator}`);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
/** Resolve a member access to `{ object, key, value }`, blocking escape keys. */
|
|
359
|
+
async evalMember(node, scope) {
|
|
360
|
+
const object = await this.evalExpr(node.object, scope);
|
|
361
|
+
const key = node.computed ? String(await this.evalExpr(node.property, scope)) : node.property.name;
|
|
362
|
+
if (BLOCKED_KEYS.has(key)) throw new InterpreterError(`Forbidden property access: ${key}`);
|
|
363
|
+
if (object == null) throw new InterpreterError(`Cannot read '${key}' of ${String(object)}`);
|
|
364
|
+
const value = object[key];
|
|
365
|
+
return { object, key, value };
|
|
366
|
+
}
|
|
367
|
+
async evalCall(node, scope) {
|
|
368
|
+
const args = [];
|
|
369
|
+
for (const a of node.arguments) {
|
|
370
|
+
if (a.type === "SpreadElement") {
|
|
371
|
+
const spread = await this.evalExpr(a.argument, scope);
|
|
372
|
+
if (!isIterable(spread)) throw new InterpreterError("Spread target is not iterable");
|
|
373
|
+
for (const v of spread) args.push(v);
|
|
374
|
+
} else {
|
|
375
|
+
args.push(await this.evalExpr(a, scope));
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
let fn;
|
|
379
|
+
let thisArg;
|
|
380
|
+
if (node.callee.type === "MemberExpression") {
|
|
381
|
+
const m = await this.evalMember(node.callee, scope);
|
|
382
|
+
if (Array.isArray(m.object) && HIGHER_ORDER_ARRAY_METHODS.has(m.key) && typeof args[0] === "function") {
|
|
383
|
+
return this.callArrayMethod(m.object, m.key, args);
|
|
384
|
+
}
|
|
385
|
+
if (typeof m.object === "string" && (m.key === "repeat" || m.key === "padStart" || m.key === "padEnd")) {
|
|
386
|
+
const n = Number(args[0]);
|
|
387
|
+
const produced = m.key === "repeat" ? m.object.length * (n > 0 ? n : 0) : Math.max(m.object.length, n > 0 ? n : 0);
|
|
388
|
+
if (produced > MAX_STRING_OP_LENGTH) {
|
|
389
|
+
throw new InterpreterError(
|
|
390
|
+
`String '${m.key}' would produce ${produced} chars, exceeding the ${MAX_STRING_OP_LENGTH} limit`
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
fn = m.value;
|
|
395
|
+
thisArg = m.object;
|
|
396
|
+
} else {
|
|
397
|
+
fn = await this.evalExpr(node.callee, scope);
|
|
398
|
+
thisArg = void 0;
|
|
399
|
+
}
|
|
400
|
+
if (typeof fn !== "function") throw new InterpreterError("Attempted to call a non-function");
|
|
401
|
+
if (++this.callDepth > this.options.maxCallDepth) {
|
|
402
|
+
this.callDepth--;
|
|
403
|
+
throw new InterpreterError(`Max call depth exceeded (${this.options.maxCallDepth})`);
|
|
404
|
+
}
|
|
405
|
+
try {
|
|
406
|
+
return await fn.apply(thisArg, args);
|
|
407
|
+
} finally {
|
|
408
|
+
this.callDepth--;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Async implementations of the higher-order array methods, so interpreter
|
|
413
|
+
* callbacks (which are async — they may `await` tool calls) are awaited. Each
|
|
414
|
+
* iteration ticks the step budget.
|
|
415
|
+
*/
|
|
416
|
+
async callArrayMethod(arr, method, args) {
|
|
417
|
+
const cb = args[0];
|
|
418
|
+
const each = (i) => {
|
|
419
|
+
this.tick();
|
|
420
|
+
return cb(arr[i], i, arr);
|
|
421
|
+
};
|
|
422
|
+
switch (method) {
|
|
423
|
+
case "map": {
|
|
424
|
+
const out = [];
|
|
425
|
+
for (let i = 0; i < arr.length; i++) out.push(await each(i));
|
|
426
|
+
return out;
|
|
427
|
+
}
|
|
428
|
+
case "flatMap": {
|
|
429
|
+
const out = [];
|
|
430
|
+
for (let i = 0; i < arr.length; i++) {
|
|
431
|
+
const v = await each(i);
|
|
432
|
+
if (Array.isArray(v)) out.push(...v);
|
|
433
|
+
else out.push(v);
|
|
434
|
+
}
|
|
435
|
+
return out;
|
|
436
|
+
}
|
|
437
|
+
case "filter": {
|
|
438
|
+
const out = [];
|
|
439
|
+
for (let i = 0; i < arr.length; i++) if (truthy(await each(i))) out.push(arr[i]);
|
|
440
|
+
return out;
|
|
441
|
+
}
|
|
442
|
+
case "forEach":
|
|
443
|
+
for (let i = 0; i < arr.length; i++) await each(i);
|
|
444
|
+
return void 0;
|
|
445
|
+
case "some":
|
|
446
|
+
for (let i = 0; i < arr.length; i++) if (truthy(await each(i))) return true;
|
|
447
|
+
return false;
|
|
448
|
+
case "every":
|
|
449
|
+
for (let i = 0; i < arr.length; i++) if (!truthy(await each(i))) return false;
|
|
450
|
+
return true;
|
|
451
|
+
case "find":
|
|
452
|
+
for (let i = 0; i < arr.length; i++) if (truthy(await each(i))) return arr[i];
|
|
453
|
+
return void 0;
|
|
454
|
+
case "findIndex":
|
|
455
|
+
for (let i = 0; i < arr.length; i++) if (truthy(await each(i))) return i;
|
|
456
|
+
return -1;
|
|
457
|
+
case "reduce": {
|
|
458
|
+
const reducer = cb;
|
|
459
|
+
let acc;
|
|
460
|
+
let start = 0;
|
|
461
|
+
if (args.length > 1) acc = args[1];
|
|
462
|
+
else {
|
|
463
|
+
if (arr.length === 0) throw new InterpreterError("Reduce of empty array with no initial value");
|
|
464
|
+
acc = arr[0];
|
|
465
|
+
start = 1;
|
|
466
|
+
}
|
|
467
|
+
for (let i = start; i < arr.length; i++) {
|
|
468
|
+
this.tick();
|
|
469
|
+
acc = await reducer(acc, arr[i], i, arr);
|
|
470
|
+
}
|
|
471
|
+
return acc;
|
|
472
|
+
}
|
|
473
|
+
case "sort": {
|
|
474
|
+
const cmp = cb;
|
|
475
|
+
for (let i = 1; i < arr.length; i++) {
|
|
476
|
+
let j = i;
|
|
477
|
+
while (j > 0) {
|
|
478
|
+
this.tick();
|
|
479
|
+
if (await cmp(arr[j - 1], arr[j]) <= 0) break;
|
|
480
|
+
[arr[j - 1], arr[j]] = [arr[j], arr[j - 1]];
|
|
481
|
+
j--;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
return arr;
|
|
485
|
+
}
|
|
486
|
+
default:
|
|
487
|
+
throw new InterpreterError(`Unsupported array method: ${method}`);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
async evalAssignment(node, scope) {
|
|
491
|
+
const value = await this.evalExpr(node.right, scope);
|
|
492
|
+
if (node.operator === "=") {
|
|
493
|
+
this.assignTo(node.left, value, scope);
|
|
494
|
+
return value;
|
|
495
|
+
}
|
|
496
|
+
const current = await this.evalExpr(node.left, scope);
|
|
497
|
+
const op = node.operator.slice(0, -1);
|
|
498
|
+
const next = applyBinary(op, current, value);
|
|
499
|
+
this.assignTo(node.left, next, scope);
|
|
500
|
+
return next;
|
|
501
|
+
}
|
|
502
|
+
/** Assign to an Identifier or MemberExpression target (blocking escape keys). */
|
|
503
|
+
assignTo(target, value, scope) {
|
|
504
|
+
if (target.type === "Identifier") {
|
|
505
|
+
scope.set(target.name, value);
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
if (target.type === "MemberExpression") {
|
|
509
|
+
throw new InterpreterError("Member assignment is not supported in AgentScript");
|
|
510
|
+
}
|
|
511
|
+
throw new InterpreterError(`Unsupported assignment target: ${target.type}`);
|
|
512
|
+
}
|
|
513
|
+
bindPattern(pattern, value, scope, mutable) {
|
|
514
|
+
if (pattern.type === "Identifier") {
|
|
515
|
+
scope.declare(pattern.name, value, mutable);
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
if (pattern.type === "ArrayPattern") {
|
|
519
|
+
const arr = isIterable(value) ? [...value] : [];
|
|
520
|
+
pattern.elements.forEach((el, i) => {
|
|
521
|
+
if (el) this.bindPattern(el, arr[i], scope, mutable);
|
|
522
|
+
});
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
if (pattern.type === "ObjectPattern") {
|
|
526
|
+
const obj = value ?? {};
|
|
527
|
+
for (const prop of pattern.properties) {
|
|
528
|
+
if (prop.type === "RestElement") continue;
|
|
529
|
+
const key = prop.key.name;
|
|
530
|
+
if (BLOCKED_KEYS.has(key)) throw new InterpreterError(`Forbidden destructured key: ${key}`);
|
|
531
|
+
this.bindPattern(prop.value, obj[key], scope, mutable);
|
|
532
|
+
}
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
throw new InterpreterError(`Unsupported binding pattern: ${pattern.type}`);
|
|
536
|
+
}
|
|
537
|
+
/** Build an interpreter-backed function (declaration / arrow / expression). */
|
|
538
|
+
makeFunction(node, closure) {
|
|
539
|
+
const self = this;
|
|
540
|
+
return async function interpreted(...args) {
|
|
541
|
+
const fnScope = closure.child();
|
|
542
|
+
node.params.forEach((p, i) => self.bindPattern(p, args[i], fnScope, true));
|
|
543
|
+
try {
|
|
544
|
+
if (node.body.type === "BlockStatement") {
|
|
545
|
+
await self.execBlock(node.body.body, fnScope);
|
|
546
|
+
return void 0;
|
|
547
|
+
}
|
|
548
|
+
return await self.evalExpr(node.body, fnScope);
|
|
549
|
+
} catch (e) {
|
|
550
|
+
if (e instanceof ReturnSignal) return e.value;
|
|
551
|
+
throw e;
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
async propKey(prop, scope) {
|
|
556
|
+
if (prop.computed) return String(await this.evalExpr(prop.key, scope));
|
|
557
|
+
if (prop.key.type === "Identifier") return prop.key.name;
|
|
558
|
+
if (prop.key.type === "Literal") return String(prop.key.value);
|
|
559
|
+
throw new InterpreterError("Unsupported property key");
|
|
560
|
+
}
|
|
561
|
+
readGlobal(name) {
|
|
562
|
+
if (Object.prototype.hasOwnProperty.call(this.globals, name)) return this.globals[name];
|
|
563
|
+
throw new InterpreterError(`'${name}' is not defined`);
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
function truthy(v) {
|
|
567
|
+
return Boolean(v);
|
|
568
|
+
}
|
|
569
|
+
function isIterable(v) {
|
|
570
|
+
return v != null && typeof v[Symbol.iterator] === "function";
|
|
571
|
+
}
|
|
572
|
+
function applyBinary(op, l, r) {
|
|
573
|
+
switch (op) {
|
|
574
|
+
case "+":
|
|
575
|
+
return l + r;
|
|
576
|
+
case "-":
|
|
577
|
+
return l - r;
|
|
578
|
+
case "*":
|
|
579
|
+
return l * r;
|
|
580
|
+
case "/":
|
|
581
|
+
return l / r;
|
|
582
|
+
case "%":
|
|
583
|
+
return l % r;
|
|
584
|
+
case "**":
|
|
585
|
+
return l ** r;
|
|
586
|
+
default:
|
|
587
|
+
throw new InterpreterError(`Unsupported compound operator: ${op}=`);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// libs/core/src/adapters/interpreter-adapter.ts
|
|
592
|
+
var SAFE_GLOBALS = Object.freeze({ Math, JSON });
|
|
593
|
+
var DEFAULT_MAX_STEPS = 5e6;
|
|
594
|
+
var DEFAULT_MAX_CALL_DEPTH = 256;
|
|
595
|
+
var InterpreterAdapter = class {
|
|
596
|
+
constructor(options = {}) {
|
|
597
|
+
this.options = options;
|
|
598
|
+
}
|
|
599
|
+
async execute(code, context) {
|
|
600
|
+
const startTime = Date.now();
|
|
601
|
+
let toolCallCount = 0;
|
|
602
|
+
const maxToolCalls = context.config.maxToolCalls ?? Number.POSITIVE_INFINITY;
|
|
603
|
+
const toolHandler = context.toolHandler ?? context.config.toolHandler;
|
|
604
|
+
const __safe_callTool = async (name, args) => {
|
|
605
|
+
if (++toolCallCount > maxToolCalls) {
|
|
606
|
+
throw new Error(`Tool call limit exceeded (${maxToolCalls})`);
|
|
607
|
+
}
|
|
608
|
+
if (typeof toolHandler !== "function") {
|
|
609
|
+
throw new Error("No toolHandler configured for the sandbox");
|
|
610
|
+
}
|
|
611
|
+
return toolHandler(name, args);
|
|
612
|
+
};
|
|
613
|
+
const globals = { ...SAFE_GLOBALS, __safe_callTool };
|
|
614
|
+
let program;
|
|
615
|
+
try {
|
|
616
|
+
program = acorn.parse(code, { ecmaVersion: "latest", sourceType: "script" });
|
|
617
|
+
} catch (error) {
|
|
618
|
+
return this.fail(error, startTime, toolCallCount, 0, "SyntaxError");
|
|
619
|
+
}
|
|
620
|
+
const timeout = context.config.timeout;
|
|
621
|
+
const timer = timeout && timeout > 0 ? setTimeout(() => context.abortController.abort(), timeout) : void 0;
|
|
622
|
+
const interpreter = new Interpreter({
|
|
623
|
+
globals,
|
|
624
|
+
maxSteps: this.options.maxSteps ?? DEFAULT_MAX_STEPS,
|
|
625
|
+
maxCallDepth: this.options.maxCallDepth ?? DEFAULT_MAX_CALL_DEPTH,
|
|
626
|
+
signal: context.abortController.signal
|
|
627
|
+
});
|
|
628
|
+
let timeoutHandle;
|
|
629
|
+
const TIMED_OUT = /* @__PURE__ */ Symbol("timeout");
|
|
630
|
+
const deadline = timeout && timeout > 0 ? new Promise((resolve) => {
|
|
631
|
+
timeoutHandle = setTimeout(() => resolve(TIMED_OUT), timeout);
|
|
632
|
+
}) : void 0;
|
|
633
|
+
try {
|
|
634
|
+
const run = interpreter.run(program);
|
|
635
|
+
run.catch(() => void 0);
|
|
636
|
+
const outcome = deadline ? await Promise.race([run, deadline]) : await run;
|
|
637
|
+
if (outcome === TIMED_OUT) {
|
|
638
|
+
return this.fail(
|
|
639
|
+
new Error(`Execution timed out after ${timeout}ms`),
|
|
640
|
+
startTime,
|
|
641
|
+
toolCallCount,
|
|
642
|
+
interpreter.stepCount,
|
|
643
|
+
"TimeoutError",
|
|
644
|
+
"EXECUTION_TIMEOUT"
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
return {
|
|
648
|
+
success: true,
|
|
649
|
+
value: outcome,
|
|
650
|
+
stats: this.stats(startTime, toolCallCount, interpreter.stepCount)
|
|
651
|
+
};
|
|
652
|
+
} catch (error) {
|
|
653
|
+
const code2 = error instanceof StepLimitError ? "STEP_LIMIT_EXCEEDED" : void 0;
|
|
654
|
+
return this.fail(error, startTime, toolCallCount, interpreter.stepCount, void 0, code2);
|
|
655
|
+
} finally {
|
|
656
|
+
if (timer) clearTimeout(timer);
|
|
657
|
+
if (timeoutHandle) clearTimeout(timeoutHandle);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
dispose() {
|
|
661
|
+
}
|
|
662
|
+
stats(startTime, toolCallCount, iterationCount) {
|
|
663
|
+
const endTime = Date.now();
|
|
664
|
+
return { duration: endTime - startTime, toolCallCount, iterationCount, startTime, endTime };
|
|
665
|
+
}
|
|
666
|
+
fail(error, startTime, toolCallCount, iterationCount, name, code) {
|
|
667
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
668
|
+
const execError = {
|
|
669
|
+
message: err.message,
|
|
670
|
+
name: name ?? err.name,
|
|
671
|
+
...err.stack ? { stack: err.stack } : {},
|
|
672
|
+
...code ? { code } : {}
|
|
673
|
+
};
|
|
674
|
+
return { success: false, error: execError, stats: this.stats(startTime, toolCallCount, iterationCount) };
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
export {
|
|
678
|
+
Interpreter,
|
|
679
|
+
InterpreterAdapter,
|
|
680
|
+
InterpreterError,
|
|
681
|
+
StepLimitError
|
|
682
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@enclave-vm/core",
|
|
3
|
-
"version": "2.14.
|
|
3
|
+
"version": "2.14.1",
|
|
4
4
|
"description": "Sandbox runtime for secure JavaScript code execution",
|
|
5
5
|
"author": "AgentFront <info@agentfront.dev>",
|
|
6
6
|
"homepage": "https://github.com/agentfront/enclave",
|
|
@@ -44,8 +44,9 @@
|
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@babel/standalone": "^7.29.0",
|
|
47
|
-
"@enclave-vm/ast": "2.14.
|
|
48
|
-
"@enclave-vm/types": "2.14.
|
|
47
|
+
"@enclave-vm/ast": "2.14.1",
|
|
48
|
+
"@enclave-vm/types": "2.14.1",
|
|
49
|
+
"@types/estree": "1.0.8",
|
|
49
50
|
"acorn": "8.15.0",
|
|
50
51
|
"acorn-walk": "8.3.4",
|
|
51
52
|
"astring": "1.9.0",
|