cvm-server 0.10.0 → 0.12.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.
Files changed (2) hide show
  1. package/main.cjs +1939 -1105
  2. package/package.json +1 -1
package/main.cjs CHANGED
@@ -2,9 +2,9 @@
2
2
  const mcp_js = require("@modelcontextprotocol/sdk/server/mcp.js");
3
3
  const stdio_js = require("@modelcontextprotocol/sdk/server/stdio.js");
4
4
  const zod = require("zod");
5
- const i$2 = require("typescript");
6
- const M$2 = require("fs");
7
- const c$1 = require("path");
5
+ const s$1 = require("typescript");
6
+ const b$2 = require("fs");
7
+ const r = require("path");
8
8
  const mongodb = require("mongodb");
9
9
  const promises = require("fs/promises");
10
10
  const dotenv = require("dotenv");
@@ -26,47 +26,47 @@ function _interopNamespaceDefault(e2) {
26
26
  n2.default = e2;
27
27
  return Object.freeze(n2);
28
28
  }
29
- const i__namespace = /* @__PURE__ */ _interopNamespaceDefault(i$2);
30
- const M__namespace = /* @__PURE__ */ _interopNamespaceDefault(M$2);
31
- const c__namespace = /* @__PURE__ */ _interopNamespaceDefault(c$1);
29
+ const s__namespace = /* @__PURE__ */ _interopNamespaceDefault(s$1);
30
+ const b__namespace = /* @__PURE__ */ _interopNamespaceDefault(b$2);
31
+ const r__namespace = /* @__PURE__ */ _interopNamespaceDefault(r);
32
32
  const dotenv__namespace = /* @__PURE__ */ _interopNamespaceDefault(dotenv);
33
- var f$1 = Object.defineProperty;
34
- var p$1 = (e2, t2, s2) => t2 in e2 ? f$1(e2, t2, { enumerable: true, configurable: true, writable: true, value: s2 }) : e2[t2] = s2;
35
- var T$1 = (e2, t2, s2) => p$1(e2, typeof t2 != "symbol" ? t2 + "" : t2, s2);
36
- var n$1 = /* @__PURE__ */ ((e2) => (e2.PUSH = "PUSH", e2.PUSH_UNDEFINED = "PUSH_UNDEFINED", e2.POP = "POP", e2.LOAD = "LOAD", e2.STORE = "STORE", e2.CONCAT = "CONCAT", e2.ARRAY_NEW = "ARRAY_NEW", e2.ARRAY_PUSH = "ARRAY_PUSH", e2.ARRAY_GET = "ARRAY_GET", e2.ARRAY_SET = "ARRAY_SET", e2.ARRAY_LEN = "ARRAY_LEN", e2.STRING_LEN = "STRING_LEN", e2.STRING_SUBSTRING = "STRING_SUBSTRING", e2.STRING_INDEXOF = "STRING_INDEXOF", e2.STRING_SPLIT = "STRING_SPLIT", e2.STRING_SLICE = "STRING_SLICE", e2.STRING_CHARAT = "STRING_CHARAT", e2.STRING_TOUPPERCASE = "STRING_TOUPPERCASE", e2.STRING_TOLOWERCASE = "STRING_TOLOWERCASE", e2.LENGTH = "LENGTH", e2.TO_STRING = "TO_STRING", e2.JSON_PARSE = "JSON_PARSE", e2.TYPEOF = "TYPEOF", e2.ADD = "ADD", e2.SUB = "SUB", e2.MUL = "MUL", e2.DIV = "DIV", e2.MOD = "MOD", e2.UNARY_MINUS = "UNARY_MINUS", e2.UNARY_PLUS = "UNARY_PLUS", e2.INC = "INC", e2.DEC = "DEC", e2.EQ = "EQ", e2.NEQ = "NEQ", e2.LT = "LT", e2.GT = "GT", e2.LTE = "LTE", e2.GTE = "GTE", e2.EQ_STRICT = "EQ_STRICT", e2.NEQ_STRICT = "NEQ_STRICT", e2.JUMP = "JUMP", e2.JUMP_IF = "JUMP_IF", e2.JUMP_IF_FALSE = "JUMP_IF_FALSE", e2.JUMP_IF_TRUE = "JUMP_IF_TRUE", e2.CALL = "CALL", e2.RETURN = "RETURN", e2.AND = "AND", e2.OR = "OR", e2.NOT = "NOT", e2.BREAK = "BREAK", e2.CONTINUE = "CONTINUE", e2.ITER_START = "ITER_START", e2.ITER_NEXT = "ITER_NEXT", e2.ITER_END = "ITER_END", e2.FS_LIST_FILES = "FS_LIST_FILES", e2.OBJECT_CREATE = "OBJECT_CREATE", e2.PROPERTY_GET = "PROPERTY_GET", e2.PROPERTY_SET = "PROPERTY_SET", e2.JSON_STRINGIFY = "JSON_STRINGIFY", e2.CC = "CC", e2.PRINT = "PRINT", e2.HALT = "HALT", e2))(n$1 || {});
33
+ var E$1 = Object.defineProperty;
34
+ var f$2 = (e2, t2, i2) => t2 in e2 ? E$1(e2, t2, { enumerable: true, configurable: true, writable: true, value: i2 }) : e2[t2] = i2;
35
+ var u$2 = (e2, t2, i2) => f$2(e2, typeof t2 != "symbol" ? t2 + "" : t2, i2);
36
+ var n = /* @__PURE__ */ ((e2) => (e2.PUSH = "PUSH", e2.PUSH_UNDEFINED = "PUSH_UNDEFINED", e2.POP = "POP", e2.DUP = "DUP", e2.DUP2 = "DUP2", e2.SWAP = "SWAP", e2.LOAD = "LOAD", e2.STORE = "STORE", e2.CONCAT = "CONCAT", e2.ARRAY_NEW = "ARRAY_NEW", e2.ARRAY_PUSH = "ARRAY_PUSH", e2.ARRAY_GET = "ARRAY_GET", e2.ARRAY_SET = "ARRAY_SET", e2.ARRAY_LEN = "ARRAY_LEN", e2.ARRAY_MAP_PROP = "ARRAY_MAP_PROP", e2.ARRAY_FILTER_PROP = "ARRAY_FILTER_PROP", e2.STRING_LEN = "STRING_LEN", e2.STRING_SUBSTRING = "STRING_SUBSTRING", e2.STRING_INDEXOF = "STRING_INDEXOF", e2.STRING_SPLIT = "STRING_SPLIT", e2.STRING_SLICE = "STRING_SLICE", e2.STRING_CHARAT = "STRING_CHARAT", e2.STRING_TOUPPERCASE = "STRING_TOUPPERCASE", e2.STRING_TOLOWERCASE = "STRING_TOLOWERCASE", e2.LENGTH = "LENGTH", e2.TO_STRING = "TO_STRING", e2.JSON_PARSE = "JSON_PARSE", e2.TYPEOF = "TYPEOF", e2.ADD = "ADD", e2.SUB = "SUB", e2.MUL = "MUL", e2.DIV = "DIV", e2.MOD = "MOD", e2.UNARY_MINUS = "UNARY_MINUS", e2.UNARY_PLUS = "UNARY_PLUS", e2.INC = "INC", e2.DEC = "DEC", e2.EQ = "EQ", e2.NEQ = "NEQ", e2.LT = "LT", e2.GT = "GT", e2.LTE = "LTE", e2.GTE = "GTE", e2.EQ_STRICT = "EQ_STRICT", e2.NEQ_STRICT = "NEQ_STRICT", e2.JUMP = "JUMP", e2.JUMP_IF = "JUMP_IF", e2.JUMP_IF_FALSE = "JUMP_IF_FALSE", e2.JUMP_IF_TRUE = "JUMP_IF_TRUE", e2.CALL = "CALL", e2.RETURN = "RETURN", e2.AND = "AND", e2.OR = "OR", e2.NOT = "NOT", e2.BREAK = "BREAK", e2.CONTINUE = "CONTINUE", e2.ITER_START = "ITER_START", e2.ITER_NEXT = "ITER_NEXT", e2.ITER_END = "ITER_END", e2.FS_LIST_FILES = "FS_LIST_FILES", e2.FS_READ_FILE = "FS_READ_FILE", e2.FS_WRITE_FILE = "FS_WRITE_FILE", e2.OBJECT_CREATE = "OBJECT_CREATE", e2.PROPERTY_GET = "PROPERTY_GET", e2.PROPERTY_SET = "PROPERTY_SET", e2.JSON_STRINGIFY = "JSON_STRINGIFY", e2.GET = "GET", e2.SET = "SET", e2.CC = "CC", e2.PRINT = "PRINT", e2.HALT = "HALT", e2))(n || {});
37
37
  function R(e2) {
38
- const t2 = [], s2 = [];
39
- let r = false;
40
- const a2 = i__namespace.createSourceFile(
38
+ const t2 = [], i2 = [];
39
+ let r2 = false;
40
+ const a2 = s__namespace.createSourceFile(
41
41
  "program.ts",
42
42
  e2,
43
- i__namespace.ScriptTarget.Latest,
43
+ s__namespace.ScriptTarget.Latest,
44
44
  true
45
45
  );
46
- function S(c2) {
47
- if (i__namespace.isFunctionDeclaration(c2) && c2.name && c2.name.text === "main" && (r = true, c2.parameters.length > 0 && t2.push("main() must not have parameters")), i__namespace.isCallExpression(c2) && i__namespace.isIdentifier(c2.expression)) {
48
- const o2 = c2.expression.text;
49
- ["setTimeout", "fetch", "require", "import"].includes(o2) && t2.push(`Unsupported function: ${o2}`);
46
+ function m2(o2) {
47
+ if (s__namespace.isFunctionDeclaration(o2) && o2.name && o2.name.text === "main" && (r2 = true, o2.parameters.length > 0 && t2.push("main() must not have parameters")), s__namespace.isCallExpression(o2) && s__namespace.isIdentifier(o2.expression)) {
48
+ const S = o2.expression.text;
49
+ ["setTimeout", "fetch", "require", "import"].includes(S) && t2.push(`Unsupported function: ${S}`);
50
50
  }
51
- i__namespace.forEachChild(c2, S);
51
+ s__namespace.forEachChild(o2, m2);
52
52
  }
53
- return S(a2), r || t2.push("Program must have a main() function"), t2.length === 0 && s2.push({ op: n$1.HALT }), {
54
- bytecode: s2,
53
+ return m2(a2), r2 || t2.push("Program must have a main() function"), t2.length === 0 && i2.push({ op: n.HALT }), {
54
+ bytecode: i2,
55
55
  errors: t2,
56
- hasMain: r
56
+ hasMain: r2
57
57
  };
58
58
  }
59
- let A$1 = class A {
59
+ let p$2 = class p {
60
60
  constructor() {
61
- T$1(this, "bytecode", []);
62
- T$1(this, "contextStack", []);
61
+ u$2(this, "bytecode", []);
62
+ u$2(this, "contextStack", []);
63
63
  }
64
64
  /**
65
65
  * Emit an instruction and return its index
66
66
  */
67
- emit(t2, s2) {
68
- const r = this.bytecode.length;
69
- return this.bytecode.push({ op: t2, arg: s2 }), r;
67
+ emit(t2, i2) {
68
+ const r2 = this.bytecode.length;
69
+ return this.bytecode.push({ op: t2, arg: i2 }), r2;
70
70
  }
71
71
  /**
72
72
  * Get the current address (next instruction index)
@@ -77,14 +77,14 @@ let A$1 = class A {
77
77
  /**
78
78
  * Patch a jump instruction with the target address
79
79
  */
80
- patchJump(t2, s2) {
81
- t2 >= 0 && t2 < this.bytecode.length && (this.bytecode[t2].arg = s2);
80
+ patchJump(t2, i2) {
81
+ t2 >= 0 && t2 < this.bytecode.length && (this.bytecode[t2].arg = i2);
82
82
  }
83
83
  /**
84
84
  * Patch multiple jump instructions with the same target
85
85
  */
86
- patchJumps(t2, s2) {
87
- t2.forEach((r) => this.patchJump(r, s2));
86
+ patchJumps(t2, i2) {
87
+ t2.forEach((r2) => this.patchJump(r2, i2));
88
88
  }
89
89
  /**
90
90
  * Push a new jump context onto the stack
@@ -109,9 +109,9 @@ let A$1 = class A {
109
109
  */
110
110
  findLoopContext() {
111
111
  for (let t2 = this.contextStack.length - 1; t2 >= 0; t2--) {
112
- const s2 = this.contextStack[t2];
113
- if (s2.type === "loop" || s2.type === "foreach")
114
- return s2;
112
+ const i2 = this.contextStack[t2];
113
+ if (i2.type === "loop" || i2.type === "foreach")
114
+ return i2;
115
115
  }
116
116
  return null;
117
117
  }
@@ -134,423 +134,469 @@ let A$1 = class A {
134
134
  return [...this.contextStack];
135
135
  }
136
136
  };
137
- const P$1 = (e2, t2, { compileExpression: s2, compileStatement: r }) => {
138
- s2(e2.expression);
139
- const a2 = t2.emit(n$1.JUMP_IF_FALSE, -1), S = {
137
+ const A$1 = (e2, t2, { compileExpression: i2, compileStatement: r2 }) => {
138
+ i2(e2.expression);
139
+ const a2 = t2.emit(n.JUMP_IF_FALSE, -1), m2 = {
140
140
  type: "if",
141
141
  endTargets: []
142
142
  };
143
- if (e2.elseStatement ? S.elseTarget = a2 : S.endTargets.push(a2), t2.pushContext(S), r(e2.thenStatement), e2.elseStatement) {
144
- const o2 = t2.emit(n$1.JUMP, -1);
145
- S.endTargets.push(o2);
146
- const m2 = t2.currentAddress();
147
- t2.patchJump(a2, m2), r(e2.elseStatement);
148
- }
149
- const c2 = t2.popContext();
150
- if (c2) {
151
- const o2 = t2.currentAddress();
152
- t2.patchJumps(c2.endTargets, o2);
153
- }
154
- }, I = (e2, t2, { compileExpression: s2, compileStatement: r }) => {
143
+ if (e2.elseStatement ? m2.elseTarget = a2 : m2.endTargets.push(a2), t2.pushContext(m2), r2(e2.thenStatement), e2.elseStatement) {
144
+ const S = t2.emit(n.JUMP, -1);
145
+ m2.endTargets.push(S);
146
+ const c2 = t2.currentAddress();
147
+ t2.patchJump(a2, c2), r2(e2.elseStatement);
148
+ }
149
+ const o2 = t2.popContext();
150
+ if (o2) {
151
+ const S = t2.currentAddress();
152
+ t2.patchJumps(o2.endTargets, S);
153
+ }
154
+ }, P$1 = (e2, t2, { compileExpression: i2, compileStatement: r2 }) => {
155
155
  const a2 = t2.currentAddress();
156
- s2(e2.expression);
157
- const c2 = {
156
+ i2(e2.expression);
157
+ const o2 = {
158
158
  type: "loop",
159
- breakTargets: [t2.emit(n$1.JUMP_IF_FALSE, -1)],
159
+ breakTargets: [t2.emit(n.JUMP_IF_FALSE, -1)],
160
160
  continueTargets: [],
161
161
  endTargets: [],
162
162
  startAddress: a2
163
163
  };
164
- t2.pushContext(c2), r(e2.statement), t2.emit(n$1.JUMP, a2);
165
- const o2 = t2.popContext();
166
- if (o2) {
167
- const m2 = t2.currentAddress();
168
- t2.patchJumps(o2.breakTargets || [], m2);
169
- }
170
- }, h$2 = (e2, t2, { compileExpression: s2, compileStatement: r }) => {
171
- const a2 = e2.initializer, S = e2.expression;
172
- let c2;
173
- if (i__namespace.isVariableDeclarationList(a2))
174
- c2 = a2.declarations[0].name.getText();
175
- else if (i__namespace.isIdentifier(a2))
176
- c2 = a2.text;
164
+ t2.pushContext(o2), r2(e2.statement), t2.emit(n.JUMP, a2);
165
+ const S = t2.popContext();
166
+ if (S) {
167
+ const c2 = t2.currentAddress();
168
+ t2.patchJumps(S.breakTargets || [], c2);
169
+ }
170
+ }, h$1 = (e2, t2, { compileExpression: i2, compileStatement: r2 }) => {
171
+ const a2 = e2.initializer, m2 = e2.expression;
172
+ let o2;
173
+ if (s__namespace.isVariableDeclarationList(a2))
174
+ o2 = a2.declarations[0].name.getText();
175
+ else if (s__namespace.isIdentifier(a2))
176
+ o2 = a2.text;
177
177
  else
178
178
  throw new Error("Unsupported for-of variable declaration");
179
- s2(S), t2.emit(n$1.ITER_START);
180
- const o2 = t2.currentAddress();
181
- t2.emit(n$1.ITER_NEXT);
182
- const m2 = t2.emit(n$1.JUMP_IF_FALSE, -1);
183
- t2.emit(n$1.STORE, c2);
179
+ i2(m2), t2.emit(n.ITER_START);
180
+ const S = t2.currentAddress();
181
+ t2.emit(n.ITER_NEXT);
182
+ const c2 = t2.emit(n.JUMP_IF_FALSE, -1);
183
+ t2.emit(n.STORE, o2);
184
184
  const l2 = {
185
185
  type: "foreach",
186
- breakTargets: [m2],
186
+ breakTargets: [c2],
187
187
  continueTargets: [],
188
188
  endTargets: [],
189
- startAddress: o2,
190
- iterVariable: c2
189
+ startAddress: S,
190
+ iterVariable: o2
191
191
  };
192
- t2.pushContext(l2), r(e2.statement), t2.emit(n$1.JUMP, o2);
193
- const u2 = t2.popContext();
194
- if (u2) {
195
- const E2 = t2.currentAddress();
196
- t2.patchJumps(u2.breakTargets || [], E2), t2.emit(n$1.ITER_END);
197
- }
198
- }, y$1 = (e2, t2, { compileStatement: s2 }) => {
199
- e2.statements.forEach((r) => {
200
- s2(r);
192
+ t2.pushContext(l2), r2(e2.statement), t2.emit(n.JUMP, S);
193
+ const T = t2.popContext();
194
+ if (T) {
195
+ const x2 = t2.currentAddress();
196
+ t2.patchJumps(T.breakTargets || [], x2), t2.emit(n.ITER_END);
197
+ }
198
+ }, I = (e2, t2, { compileStatement: i2 }) => {
199
+ e2.statements.forEach((r2) => {
200
+ i2(r2);
201
201
  });
202
- }, N$1 = (e2, t2, { compileExpression: s2 }) => {
203
- const r = e2.declarationList.declarations[0];
204
- r.initializer && (s2(r.initializer), t2.emit(n$1.STORE, r.name.getText()));
205
- }, _$1 = (e2, t2, { compileExpression: s2 }) => {
206
- const r = e2.expression;
207
- if (i__namespace.isBinaryExpression(r)) {
208
- const a2 = r.operatorToken.kind;
209
- if (a2 === i__namespace.SyntaxKind.PlusEqualsToken || a2 === i__namespace.SyntaxKind.MinusEqualsToken || a2 === i__namespace.SyntaxKind.AsteriskEqualsToken || a2 === i__namespace.SyntaxKind.SlashEqualsToken || a2 === i__namespace.SyntaxKind.PercentEqualsToken) {
210
- if (i__namespace.isIdentifier(r.left)) {
211
- switch (t2.emit(n$1.LOAD, r.left.text), s2(r.right), a2) {
212
- case i__namespace.SyntaxKind.PlusEqualsToken:
213
- i__namespace.isStringLiteral(r.right) ? t2.emit(n$1.CONCAT) : t2.emit(n$1.ADD);
202
+ }, _$1 = (e2, t2, { compileExpression: i2 }) => {
203
+ const r2 = e2.declarationList.declarations[0];
204
+ r2.initializer && (i2(r2.initializer), t2.emit(n.STORE, r2.name.getText()));
205
+ }, g = (e2, t2, { compileExpression: i2 }) => {
206
+ const r2 = e2.expression;
207
+ if (s__namespace.isBinaryExpression(r2)) {
208
+ const a2 = r2.operatorToken.kind;
209
+ if (a2 === s__namespace.SyntaxKind.PlusEqualsToken || a2 === s__namespace.SyntaxKind.MinusEqualsToken || a2 === s__namespace.SyntaxKind.AsteriskEqualsToken || a2 === s__namespace.SyntaxKind.SlashEqualsToken || a2 === s__namespace.SyntaxKind.PercentEqualsToken) {
210
+ if (s__namespace.isIdentifier(r2.left)) {
211
+ switch (t2.emit(n.LOAD, r2.left.text), i2(r2.right), a2) {
212
+ case s__namespace.SyntaxKind.PlusEqualsToken:
213
+ t2.emit(n.ADD);
214
214
  break;
215
- case i__namespace.SyntaxKind.MinusEqualsToken:
216
- t2.emit(n$1.SUB);
215
+ case s__namespace.SyntaxKind.MinusEqualsToken:
216
+ t2.emit(n.SUB);
217
217
  break;
218
- case i__namespace.SyntaxKind.AsteriskEqualsToken:
219
- t2.emit(n$1.MUL);
218
+ case s__namespace.SyntaxKind.AsteriskEqualsToken:
219
+ t2.emit(n.MUL);
220
220
  break;
221
- case i__namespace.SyntaxKind.SlashEqualsToken:
222
- t2.emit(n$1.DIV);
221
+ case s__namespace.SyntaxKind.SlashEqualsToken:
222
+ t2.emit(n.DIV);
223
223
  break;
224
- case i__namespace.SyntaxKind.PercentEqualsToken:
225
- t2.emit(n$1.MOD);
224
+ case s__namespace.SyntaxKind.PercentEqualsToken:
225
+ t2.emit(n.MOD);
226
226
  break;
227
227
  }
228
- t2.emit(n$1.STORE, r.left.text);
229
- } else if (i__namespace.isElementAccessExpression(r.left))
228
+ t2.emit(n.STORE, r2.left.text);
229
+ } else if (s__namespace.isElementAccessExpression(r2.left))
230
230
  throw new Error("Compound assignment to array elements not yet supported");
231
231
  return;
232
- } else if (a2 === i__namespace.SyntaxKind.EqualsToken) {
233
- if (s2(r.right), i__namespace.isIdentifier(r.left))
234
- t2.emit(n$1.STORE, r.left.text);
235
- else if (i__namespace.isElementAccessExpression(r.left)) {
236
- const S = `__temp_${t2.getBytecode().length}`;
237
- t2.emit(n$1.STORE, S), s2(r.left.expression), s2(r.left.argumentExpression), t2.emit(n$1.LOAD, S), t2.emit(n$1.ARRAY_SET), t2.emit(n$1.POP);
238
- } else if (i__namespace.isPropertyAccessExpression(r.left)) {
239
- const S = `__temp_${t2.getBytecode().length}`;
240
- t2.emit(n$1.STORE, S), s2(r.left.expression), t2.emit(n$1.PUSH, r.left.name.text), t2.emit(n$1.LOAD, S), t2.emit(n$1.PROPERTY_SET), t2.emit(n$1.POP);
232
+ } else if (a2 === s__namespace.SyntaxKind.EqualsToken) {
233
+ if (i2(r2.right), s__namespace.isIdentifier(r2.left))
234
+ t2.emit(n.STORE, r2.left.text);
235
+ else if (s__namespace.isElementAccessExpression(r2.left)) {
236
+ const m2 = `__temp_${t2.getBytecode().length}`;
237
+ t2.emit(n.STORE, m2), i2(r2.left.expression), i2(r2.left.argumentExpression), t2.emit(n.LOAD, m2), t2.emit(n.ARRAY_SET), t2.emit(n.POP);
238
+ } else if (s__namespace.isPropertyAccessExpression(r2.left)) {
239
+ const m2 = `__temp_${t2.getBytecode().length}`;
240
+ t2.emit(n.STORE, m2), i2(r2.left.expression), t2.emit(n.PUSH, r2.left.name.text), t2.emit(n.LOAD, m2), t2.emit(n.PROPERTY_SET), t2.emit(n.POP);
241
241
  }
242
242
  return;
243
243
  }
244
244
  }
245
- i__namespace.isCallExpression(r) && i__namespace.isPropertyAccessExpression(r.expression) && r.expression.expression.getText() === "console" && r.expression.name.getText() === "log" ? (r.arguments.forEach((a2) => {
246
- s2(a2);
247
- }), t2.emit(n$1.PRINT)) : i__namespace.isCallExpression(r) && i__namespace.isIdentifier(r.expression) && r.expression.text === "CC" ? (r.arguments.length > 0 && s2(r.arguments[0]), t2.emit(n$1.CC), t2.emit(n$1.POP)) : i__namespace.isCallExpression(r) && i__namespace.isPropertyAccessExpression(r.expression) && r.expression.name.getText() === "push" ? (s2(r.expression.expression), r.arguments.length > 0 && s2(r.arguments[0]), t2.emit(n$1.ARRAY_PUSH)) : (i__namespace.isPostfixUnaryExpression(r) || i__namespace.isPrefixUnaryExpression(r)) && (s2(r), t2.emit(n$1.POP));
248
- }, g$1 = (e2, t2, { compileExpression: s2 }) => {
249
- e2.expression ? s2(e2.expression) : t2.emit(n$1.PUSH, null), t2.emit(n$1.RETURN);
250
- }, k = (e2, t2, s2) => {
251
- const r = t2.findLoopContext();
252
- if (r) {
253
- r.type === "foreach" && t2.emit(n$1.ITER_END);
254
- const a2 = t2.emit(n$1.BREAK, -1);
255
- r.breakTargets = r.breakTargets || [], r.breakTargets.push(a2);
245
+ s__namespace.isCallExpression(r2) && s__namespace.isPropertyAccessExpression(r2.expression) && r2.expression.expression.getText() === "console" && r2.expression.name.getText() === "log" ? (r2.arguments.forEach((a2) => {
246
+ i2(a2);
247
+ }), t2.emit(n.PRINT)) : s__namespace.isCallExpression(r2) && s__namespace.isIdentifier(r2.expression) && r2.expression.text === "CC" ? (r2.arguments.length > 0 && i2(r2.arguments[0]), t2.emit(n.CC), t2.emit(n.POP)) : s__namespace.isCallExpression(r2) && s__namespace.isPropertyAccessExpression(r2.expression) && r2.expression.name.getText() === "push" ? (i2(r2.expression.expression), r2.arguments.length > 0 && i2(r2.arguments[0]), t2.emit(n.ARRAY_PUSH)) : (s__namespace.isPostfixUnaryExpression(r2) || s__namespace.isPrefixUnaryExpression(r2), i2(r2), t2.emit(n.POP));
248
+ }, y$1 = (e2, t2, { compileExpression: i2 }) => {
249
+ e2.expression ? i2(e2.expression) : t2.emit(n.PUSH, null), t2.emit(n.RETURN);
250
+ }, N = (e2, t2, i2) => {
251
+ const r2 = t2.findLoopContext();
252
+ if (r2) {
253
+ r2.type === "foreach" && t2.emit(n.ITER_END);
254
+ const a2 = t2.emit(n.BREAK, -1);
255
+ r2.breakTargets = r2.breakTargets || [], r2.breakTargets.push(a2);
256
256
  } else
257
- throw new Error("break statement not in loop");
258
- }, U = (e2, t2, s2) => {
259
- const r = t2.findLoopContext();
260
- if (r && r.startAddress !== void 0) {
261
- const a2 = t2.emit(n$1.CONTINUE, r.startAddress);
262
- r.continueTargets = r.continueTargets || [], r.continueTargets.push(a2);
257
+ i2.reportError(e2, "break statement not in loop");
258
+ }, U = (e2, t2, i2) => {
259
+ const r2 = t2.findLoopContext();
260
+ if (r2 && r2.startAddress !== void 0) {
261
+ const a2 = t2.emit(n.CONTINUE, r2.startAddress);
262
+ r2.continueTargets = r2.continueTargets || [], r2.continueTargets.push(a2);
263
263
  } else
264
- throw new Error("continue statement not in loop");
265
- }, L$1 = {
266
- [i__namespace.SyntaxKind.IfStatement]: P$1,
267
- [i__namespace.SyntaxKind.WhileStatement]: I,
268
- [i__namespace.SyntaxKind.ForOfStatement]: h$2,
269
- [i__namespace.SyntaxKind.Block]: y$1,
270
- [i__namespace.SyntaxKind.VariableStatement]: N$1,
271
- [i__namespace.SyntaxKind.ExpressionStatement]: _$1,
272
- [i__namespace.SyntaxKind.ReturnStatement]: g$1,
273
- [i__namespace.SyntaxKind.BreakStatement]: k,
274
- [i__namespace.SyntaxKind.ContinueStatement]: U
275
- }, K = (e2, t2, s2) => {
276
- t2.emit(n$1.PUSH, e2.text);
277
- }, d$1 = (e2, t2, s2) => {
278
- t2.emit(n$1.PUSH, Number(e2.text));
279
- }, b$2 = (e2, t2, s2) => {
280
- t2.emit(n$1.PUSH, true);
281
- }, F$1 = (e2, t2, s2) => {
282
- t2.emit(n$1.PUSH, false);
283
- }, G$1 = (e2, t2, s2) => {
284
- t2.emit(n$1.PUSH, null);
285
- }, D$1 = (e2, t2, s2) => {
286
- e2.text === "undefined" ? t2.emit(n$1.PUSH_UNDEFINED) : t2.emit(n$1.LOAD, e2.text);
287
- }, H$1 = (e2, t2, { compileExpression: s2 }) => {
288
- t2.emit(n$1.ARRAY_NEW), e2.elements.forEach((r) => {
289
- s2(r), t2.emit(n$1.ARRAY_PUSH);
264
+ i2.reportError(e2, "continue statement not in loop");
265
+ }, k = {
266
+ [s__namespace.SyntaxKind.IfStatement]: A$1,
267
+ [s__namespace.SyntaxKind.WhileStatement]: P$1,
268
+ [s__namespace.SyntaxKind.ForOfStatement]: h$1,
269
+ [s__namespace.SyntaxKind.Block]: I,
270
+ [s__namespace.SyntaxKind.VariableStatement]: _$1,
271
+ [s__namespace.SyntaxKind.ExpressionStatement]: g,
272
+ [s__namespace.SyntaxKind.ReturnStatement]: y$1,
273
+ [s__namespace.SyntaxKind.BreakStatement]: N,
274
+ [s__namespace.SyntaxKind.ContinueStatement]: U
275
+ }, L = (e2, t2, i2) => {
276
+ t2.emit(n.PUSH, e2.text);
277
+ }, F$1 = (e2, t2, i2) => {
278
+ t2.emit(n.PUSH, Number(e2.text));
279
+ }, K$1 = (e2, t2, i2) => {
280
+ t2.emit(n.PUSH, true);
281
+ }, b$1 = (e2, t2, i2) => {
282
+ t2.emit(n.PUSH, false);
283
+ }, D$1 = (e2, t2, i2) => {
284
+ t2.emit(n.PUSH, null);
285
+ }, d$3 = (e2, t2, i2) => {
286
+ e2.text === "undefined" ? t2.emit(n.PUSH_UNDEFINED) : t2.emit(n.LOAD, e2.text);
287
+ }, G$1 = (e2, t2, { compileExpression: i2 }) => {
288
+ t2.emit(n.ARRAY_NEW), e2.elements.forEach((r2) => {
289
+ i2(r2), t2.emit(n.ARRAY_PUSH);
290
290
  });
291
- }, M$1 = (e2, t2, { compileExpression: s2 }) => {
292
- s2(e2.expression);
293
- }, J$1 = (e2, t2, { compileExpression: s2 }) => {
294
- e2.name.text === "length" ? (s2(e2.expression), t2.emit(n$1.LENGTH)) : (s2(e2.expression), t2.emit(n$1.PUSH, e2.name.text), t2.emit(n$1.PROPERTY_GET));
295
- }, Y$1 = (e2, t2, { compileExpression: s2, reportError: r }) => {
296
- s2(e2.expression), e2.argumentExpression ? s2(e2.argumentExpression) : r(e2, "Element access requires an index expression"), t2.emit(n$1.ARRAY_GET);
297
- }, C = (e2, t2, { compileExpression: s2 }) => {
291
+ }, H$1 = (e2, t2, { compileExpression: i2 }) => {
292
+ i2(e2.expression);
293
+ }, M$2 = (e2, t2, { compileExpression: i2 }) => {
294
+ e2.name.text === "length" ? (i2(e2.expression), t2.emit(n.LENGTH)) : (i2(e2.expression), t2.emit(n.PUSH, e2.name.text), t2.emit(n.PROPERTY_GET));
295
+ }, Y = (e2, t2, { compileExpression: i2, reportError: r2 }) => {
296
+ i2(e2.expression), e2.argumentExpression ? i2(e2.argumentExpression) : r2(e2, "Element access requires an index expression"), t2.emit(n.ARRAY_GET);
297
+ }, J = (e2, t2, { compileExpression: i2 }) => {
298
298
  switch (e2.operator) {
299
- case i__namespace.SyntaxKind.ExclamationToken:
300
- s2(e2.operand), t2.emit(n$1.NOT);
299
+ case s__namespace.SyntaxKind.ExclamationToken:
300
+ i2(e2.operand), t2.emit(n.NOT);
301
301
  break;
302
- case i__namespace.SyntaxKind.MinusToken:
303
- s2(e2.operand), t2.emit(n$1.UNARY_MINUS);
302
+ case s__namespace.SyntaxKind.MinusToken:
303
+ i2(e2.operand), t2.emit(n.UNARY_MINUS);
304
304
  break;
305
- case i__namespace.SyntaxKind.PlusToken:
306
- s2(e2.operand), t2.emit(n$1.UNARY_PLUS);
305
+ case s__namespace.SyntaxKind.PlusToken:
306
+ i2(e2.operand), t2.emit(n.UNARY_PLUS);
307
307
  break;
308
- case i__namespace.SyntaxKind.PlusPlusToken:
309
- i__namespace.isIdentifier(e2.operand) && (t2.emit(n$1.PUSH, e2.operand.text), t2.emit(n$1.INC, false));
308
+ case s__namespace.SyntaxKind.PlusPlusToken:
309
+ s__namespace.isIdentifier(e2.operand) && (t2.emit(n.PUSH, e2.operand.text), t2.emit(n.INC, false));
310
310
  break;
311
- case i__namespace.SyntaxKind.MinusMinusToken:
312
- i__namespace.isIdentifier(e2.operand) && (t2.emit(n$1.PUSH, e2.operand.text), t2.emit(n$1.DEC, false));
311
+ case s__namespace.SyntaxKind.MinusMinusToken:
312
+ s__namespace.isIdentifier(e2.operand) && (t2.emit(n.PUSH, e2.operand.text), t2.emit(n.DEC, false));
313
313
  break;
314
314
  }
315
- }, w$1 = (e2, t2, { compileExpression: s2 }) => {
315
+ }, w = (e2, t2, { compileExpression: i2 }) => {
316
316
  switch (e2.operator) {
317
- case i__namespace.SyntaxKind.PlusPlusToken:
318
- i__namespace.isIdentifier(e2.operand) && (t2.emit(n$1.PUSH, e2.operand.text), t2.emit(n$1.INC, true));
317
+ case s__namespace.SyntaxKind.PlusPlusToken:
318
+ s__namespace.isIdentifier(e2.operand) && (t2.emit(n.PUSH, e2.operand.text), t2.emit(n.INC, true));
319
319
  break;
320
- case i__namespace.SyntaxKind.MinusMinusToken:
321
- i__namespace.isIdentifier(e2.operand) && (t2.emit(n$1.PUSH, e2.operand.text), t2.emit(n$1.DEC, true));
320
+ case s__namespace.SyntaxKind.MinusMinusToken:
321
+ s__namespace.isIdentifier(e2.operand) && (t2.emit(n.PUSH, e2.operand.text), t2.emit(n.DEC, true));
322
322
  break;
323
323
  }
324
- };
325
- function x$1(e2, t2) {
326
- return !!(i__namespace.isStringLiteral(e2) || i__namespace.isStringLiteral(t2) || i__namespace.isBinaryExpression(e2) && e2.operatorToken.kind === i__namespace.SyntaxKind.PlusToken && x$1(e2.left, e2.right) || i__namespace.isBinaryExpression(t2) && t2.operatorToken.kind === i__namespace.SyntaxKind.PlusToken && x$1(t2.left, t2.right));
327
- }
328
- const B$1 = (e2, t2, { compileExpression: s2 }) => {
329
- const r = e2.operatorToken.kind;
330
- switch (s2(e2.left), s2(e2.right), r) {
331
- case i__namespace.SyntaxKind.PlusToken:
332
- x$1(e2.left, e2.right) ? t2.emit(n$1.CONCAT) : t2.emit(n$1.ADD);
324
+ }, B$1 = (e2, t2, { compileExpression: i2 }) => {
325
+ const r2 = e2.operatorToken.kind;
326
+ switch (i2(e2.left), i2(e2.right), r2) {
327
+ case s__namespace.SyntaxKind.PlusToken:
328
+ t2.emit(n.ADD);
333
329
  break;
334
- case i__namespace.SyntaxKind.MinusToken:
335
- t2.emit(n$1.SUB);
330
+ case s__namespace.SyntaxKind.MinusToken:
331
+ t2.emit(n.SUB);
336
332
  break;
337
- case i__namespace.SyntaxKind.AsteriskToken:
338
- t2.emit(n$1.MUL);
333
+ case s__namespace.SyntaxKind.AsteriskToken:
334
+ t2.emit(n.MUL);
339
335
  break;
340
- case i__namespace.SyntaxKind.SlashToken:
341
- t2.emit(n$1.DIV);
336
+ case s__namespace.SyntaxKind.SlashToken:
337
+ t2.emit(n.DIV);
342
338
  break;
343
- case i__namespace.SyntaxKind.PercentToken:
344
- t2.emit(n$1.MOD);
339
+ case s__namespace.SyntaxKind.PercentToken:
340
+ t2.emit(n.MOD);
345
341
  break;
346
- case i__namespace.SyntaxKind.EqualsEqualsToken:
347
- t2.emit(n$1.EQ);
342
+ case s__namespace.SyntaxKind.EqualsEqualsToken:
343
+ t2.emit(n.EQ);
348
344
  break;
349
- case i__namespace.SyntaxKind.ExclamationEqualsToken:
350
- t2.emit(n$1.NEQ);
345
+ case s__namespace.SyntaxKind.ExclamationEqualsToken:
346
+ t2.emit(n.NEQ);
351
347
  break;
352
- case i__namespace.SyntaxKind.LessThanToken:
353
- t2.emit(n$1.LT);
348
+ case s__namespace.SyntaxKind.LessThanToken:
349
+ t2.emit(n.LT);
354
350
  break;
355
- case i__namespace.SyntaxKind.GreaterThanToken:
356
- t2.emit(n$1.GT);
351
+ case s__namespace.SyntaxKind.GreaterThanToken:
352
+ t2.emit(n.GT);
357
353
  break;
358
- case i__namespace.SyntaxKind.LessThanEqualsToken:
359
- t2.emit(n$1.LTE);
354
+ case s__namespace.SyntaxKind.LessThanEqualsToken:
355
+ t2.emit(n.LTE);
360
356
  break;
361
- case i__namespace.SyntaxKind.GreaterThanEqualsToken:
362
- t2.emit(n$1.GTE);
357
+ case s__namespace.SyntaxKind.GreaterThanEqualsToken:
358
+ t2.emit(n.GTE);
363
359
  break;
364
- case i__namespace.SyntaxKind.EqualsEqualsEqualsToken:
365
- t2.emit(n$1.EQ_STRICT);
360
+ case s__namespace.SyntaxKind.EqualsEqualsEqualsToken:
361
+ t2.emit(n.EQ_STRICT);
366
362
  break;
367
- case i__namespace.SyntaxKind.ExclamationEqualsEqualsToken:
368
- t2.emit(n$1.NEQ_STRICT);
363
+ case s__namespace.SyntaxKind.ExclamationEqualsEqualsToken:
364
+ t2.emit(n.NEQ_STRICT);
369
365
  break;
370
- case i__namespace.SyntaxKind.AmpersandAmpersandToken:
371
- t2.emit(n$1.AND);
366
+ case s__namespace.SyntaxKind.AmpersandAmpersandToken:
367
+ t2.emit(n.AND);
372
368
  break;
373
- case i__namespace.SyntaxKind.BarBarToken:
374
- t2.emit(n$1.OR);
369
+ case s__namespace.SyntaxKind.BarBarToken:
370
+ t2.emit(n.OR);
375
371
  break;
376
372
  }
377
- }, q$1 = (e2, t2, { compileExpression: s2 }) => {
378
- s2(e2.condition);
379
- const r = t2.emit(n$1.JUMP_IF_FALSE, -1);
380
- s2(e2.whenTrue);
381
- const a2 = t2.emit(n$1.JUMP, -1), S = t2.currentAddress();
382
- t2.patchJump(r, S), s2(e2.whenFalse);
383
- const c2 = t2.currentAddress();
384
- t2.patchJump(a2, c2);
385
- }, O = (e2, t2, { compileExpression: s2 }) => {
386
- s2(e2.expression), t2.emit(n$1.TYPEOF);
387
- }, V$1 = (e2, t2, { compileExpression: s2 }) => {
388
- if (i__namespace.isPropertyAccessExpression(e2.expression) && i__namespace.isIdentifier(e2.expression.expression) && e2.expression.expression.text === "fs" && e2.expression.name.text === "listFiles")
389
- e2.arguments.length > 0 ? s2(e2.arguments[0]) : t2.emit(n$1.PUSH, "."), e2.arguments.length > 1 && s2(e2.arguments[1]), t2.emit(n$1.FS_LIST_FILES);
390
- else if (i__namespace.isPropertyAccessExpression(e2.expression) && i__namespace.isIdentifier(e2.expression.expression) && e2.expression.expression.text === "JSON" && e2.expression.name.text === "parse")
391
- e2.arguments.length > 0 && s2(e2.arguments[0]), t2.emit(n$1.JSON_PARSE);
392
- else if (i__namespace.isPropertyAccessExpression(e2.expression) && i__namespace.isIdentifier(e2.expression.expression) && e2.expression.expression.text === "JSON" && e2.expression.name.text === "stringify")
393
- e2.arguments.length > 0 ? s2(e2.arguments[0]) : t2.emit(n$1.PUSH_UNDEFINED), t2.emit(n$1.JSON_STRINGIFY);
394
- else if (i__namespace.isIdentifier(e2.expression) && e2.expression.text === "CC")
395
- e2.arguments.length > 0 && s2(e2.arguments[0]), t2.emit(n$1.CC);
396
- else if (i__namespace.isPropertyAccessExpression(e2.expression)) {
397
- const r = e2.expression.name.text;
398
- if (r === "substring")
399
- s2(e2.expression.expression), e2.arguments.length > 0 ? s2(e2.arguments[0]) : t2.emit(n$1.PUSH, 0), e2.arguments.length > 1 && s2(e2.arguments[1]), t2.emit(n$1.STRING_SUBSTRING);
400
- else if (r === "indexOf")
401
- s2(e2.expression.expression), e2.arguments.length > 0 ? s2(e2.arguments[0]) : t2.emit(n$1.PUSH, ""), t2.emit(n$1.STRING_INDEXOF);
402
- else if (r === "split")
403
- s2(e2.expression.expression), e2.arguments.length > 0 ? s2(e2.arguments[0]) : t2.emit(n$1.PUSH, ""), t2.emit(n$1.STRING_SPLIT);
404
- else if (r === "slice")
405
- s2(e2.expression.expression), e2.arguments.length > 0 ? s2(e2.arguments[0]) : t2.emit(n$1.PUSH, 0), e2.arguments.length > 1 && s2(e2.arguments[1]), t2.emit(n$1.STRING_SLICE);
406
- else if (r === "charAt")
407
- s2(e2.expression.expression), e2.arguments.length > 0 ? s2(e2.arguments[0]) : t2.emit(n$1.PUSH, 0), t2.emit(n$1.STRING_CHARAT);
408
- else if (r === "toUpperCase")
409
- s2(e2.expression.expression), t2.emit(n$1.STRING_TOUPPERCASE);
410
- else if (r === "toLowerCase")
411
- s2(e2.expression.expression), t2.emit(n$1.STRING_TOLOWERCASE);
412
- else if (r === "toString")
413
- s2(e2.expression.expression), t2.emit(n$1.TO_STRING);
373
+ }, q = (e2, t2, { compileExpression: i2 }) => {
374
+ i2(e2.condition);
375
+ const r2 = t2.emit(n.JUMP_IF_FALSE, -1);
376
+ i2(e2.whenTrue);
377
+ const a2 = t2.emit(n.JUMP, -1), m2 = t2.currentAddress();
378
+ t2.patchJump(r2, m2), i2(e2.whenFalse);
379
+ const o2 = t2.currentAddress();
380
+ t2.patchJump(a2, o2);
381
+ }, C$1 = (e2, t2, { compileExpression: i2 }) => {
382
+ i2(e2.expression), t2.emit(n.TYPEOF);
383
+ }, O$1 = (e2, t2, { compileExpression: i2, reportError: r2 }) => {
384
+ if (s__namespace.isPropertyAccessExpression(e2.expression) && s__namespace.isIdentifier(e2.expression.expression) && e2.expression.expression.text === "fs") {
385
+ const a2 = e2.expression.name.text;
386
+ if (a2 === "listFiles")
387
+ e2.arguments.length > 0 ? i2(e2.arguments[0]) : t2.emit(n.PUSH, "."), e2.arguments.length > 1 && i2(e2.arguments[1]), t2.emit(n.FS_LIST_FILES);
388
+ else if (a2 === "readFile")
389
+ e2.arguments.length > 0 ? (i2(e2.arguments[0]), t2.emit(n.FS_READ_FILE)) : r2(e2, "fs.readFile() requires a path argument");
390
+ else if (a2 === "writeFile") {
391
+ if (e2.arguments.length < 2) {
392
+ r2(e2, "fs.writeFile() requires path and content arguments");
393
+ return;
394
+ }
395
+ i2(e2.arguments[0]), i2(e2.arguments[1]), t2.emit(n.FS_WRITE_FILE);
396
+ } else
397
+ r2(e2, `Unsupported fs method: ${a2}`);
398
+ } else if (s__namespace.isPropertyAccessExpression(e2.expression) && s__namespace.isIdentifier(e2.expression.expression) && e2.expression.expression.text === "JSON" && e2.expression.name.text === "parse")
399
+ e2.arguments.length > 0 && i2(e2.arguments[0]), t2.emit(n.JSON_PARSE);
400
+ else if (s__namespace.isPropertyAccessExpression(e2.expression) && s__namespace.isIdentifier(e2.expression.expression) && e2.expression.expression.text === "JSON" && e2.expression.name.text === "stringify")
401
+ e2.arguments.length > 0 ? i2(e2.arguments[0]) : t2.emit(n.PUSH_UNDEFINED), t2.emit(n.JSON_STRINGIFY);
402
+ else if (s__namespace.isIdentifier(e2.expression) && e2.expression.text === "CC")
403
+ e2.arguments.length > 0 && i2(e2.arguments[0]), t2.emit(n.CC);
404
+ else if (s__namespace.isPropertyAccessExpression(e2.expression)) {
405
+ const a2 = e2.expression.name.text;
406
+ if (a2 === "substring")
407
+ i2(e2.expression.expression), e2.arguments.length > 0 ? i2(e2.arguments[0]) : t2.emit(n.PUSH, 0), e2.arguments.length > 1 && i2(e2.arguments[1]), t2.emit(n.STRING_SUBSTRING);
408
+ else if (a2 === "indexOf")
409
+ i2(e2.expression.expression), e2.arguments.length > 0 ? i2(e2.arguments[0]) : t2.emit(n.PUSH, ""), t2.emit(n.STRING_INDEXOF);
410
+ else if (a2 === "split")
411
+ i2(e2.expression.expression), e2.arguments.length > 0 ? i2(e2.arguments[0]) : t2.emit(n.PUSH, ""), t2.emit(n.STRING_SPLIT);
412
+ else if (a2 === "slice")
413
+ i2(e2.expression.expression), e2.arguments.length > 0 ? i2(e2.arguments[0]) : t2.emit(n.PUSH, 0), e2.arguments.length > 1 && i2(e2.arguments[1]), t2.emit(n.STRING_SLICE);
414
+ else if (a2 === "charAt")
415
+ i2(e2.expression.expression), e2.arguments.length > 0 ? i2(e2.arguments[0]) : t2.emit(n.PUSH, 0), t2.emit(n.STRING_CHARAT);
416
+ else if (a2 === "toUpperCase")
417
+ i2(e2.expression.expression), t2.emit(n.STRING_TOUPPERCASE);
418
+ else if (a2 === "toLowerCase")
419
+ i2(e2.expression.expression), t2.emit(n.STRING_TOLOWERCASE);
420
+ else if (a2 === "toString")
421
+ i2(e2.expression.expression), t2.emit(n.TO_STRING);
414
422
  else
415
- throw new Error(`Method call '${r}' is not supported`);
423
+ throw new Error(`Method call '${a2}' is not supported`);
416
424
  } else
417
425
  throw new Error("Unsupported call expression");
418
- }, Q = (e2, t2, { compileExpression: s2 }) => {
419
- t2.emit(n$1.OBJECT_CREATE);
420
- for (const r of e2.properties)
421
- if (i__namespace.isPropertyAssignment(r)) {
426
+ }, V = (e2, t2, { compileExpression: i2 }) => {
427
+ t2.emit(n.OBJECT_CREATE);
428
+ for (const r2 of e2.properties)
429
+ if (s__namespace.isPropertyAssignment(r2)) {
422
430
  let a2;
423
- if (i__namespace.isIdentifier(r.name))
424
- a2 = r.name.text;
425
- else if (i__namespace.isStringLiteral(r.name))
426
- a2 = r.name.text;
431
+ if (s__namespace.isIdentifier(r2.name))
432
+ a2 = r2.name.text;
433
+ else if (s__namespace.isStringLiteral(r2.name))
434
+ a2 = r2.name.text;
427
435
  else
428
436
  throw new Error("Computed property names are not supported");
429
- t2.emit(n$1.PUSH, a2), s2(r.initializer), t2.emit(n$1.PROPERTY_SET);
430
- } else if (i__namespace.isShorthandPropertyAssignment(r)) {
431
- const a2 = r.name.text;
432
- t2.emit(n$1.PUSH, a2), t2.emit(n$1.LOAD, a2), t2.emit(n$1.PROPERTY_SET);
437
+ t2.emit(n.PUSH, a2), i2(r2.initializer), t2.emit(n.PROPERTY_SET);
438
+ } else if (s__namespace.isShorthandPropertyAssignment(r2)) {
439
+ const a2 = r2.name.text;
440
+ t2.emit(n.PUSH, a2), t2.emit(n.LOAD, a2), t2.emit(n.PROPERTY_SET);
433
441
  } else
434
442
  throw new Error("Unsupported property type in object literal");
435
- }, j$2 = {
436
- [i__namespace.SyntaxKind.StringLiteral]: K,
437
- [i__namespace.SyntaxKind.NumericLiteral]: d$1,
438
- [i__namespace.SyntaxKind.TrueKeyword]: b$2,
439
- [i__namespace.SyntaxKind.FalseKeyword]: F$1,
440
- [i__namespace.SyntaxKind.NullKeyword]: G$1,
441
- [i__namespace.SyntaxKind.Identifier]: D$1,
442
- [i__namespace.SyntaxKind.ArrayLiteralExpression]: H$1,
443
- [i__namespace.SyntaxKind.BinaryExpression]: B$1,
444
- [i__namespace.SyntaxKind.PrefixUnaryExpression]: C,
445
- [i__namespace.SyntaxKind.PostfixUnaryExpression]: w$1,
446
- [i__namespace.SyntaxKind.CallExpression]: V$1,
447
- [i__namespace.SyntaxKind.PropertyAccessExpression]: J$1,
448
- [i__namespace.SyntaxKind.ElementAccessExpression]: Y$1,
449
- [i__namespace.SyntaxKind.ConditionalExpression]: q$1,
450
- [i__namespace.SyntaxKind.TypeOfExpression]: O,
451
- [i__namespace.SyntaxKind.ParenthesizedExpression]: M$1,
452
- [i__namespace.SyntaxKind.ObjectLiteralExpression]: Q
443
+ }, W$1 = {
444
+ [s__namespace.SyntaxKind.StringLiteral]: L,
445
+ [s__namespace.SyntaxKind.NumericLiteral]: F$1,
446
+ [s__namespace.SyntaxKind.TrueKeyword]: K$1,
447
+ [s__namespace.SyntaxKind.FalseKeyword]: b$1,
448
+ [s__namespace.SyntaxKind.NullKeyword]: D$1,
449
+ [s__namespace.SyntaxKind.Identifier]: d$3,
450
+ [s__namespace.SyntaxKind.ArrayLiteralExpression]: G$1,
451
+ [s__namespace.SyntaxKind.BinaryExpression]: B$1,
452
+ [s__namespace.SyntaxKind.PrefixUnaryExpression]: J,
453
+ [s__namespace.SyntaxKind.PostfixUnaryExpression]: w,
454
+ [s__namespace.SyntaxKind.CallExpression]: O$1,
455
+ [s__namespace.SyntaxKind.PropertyAccessExpression]: M$2,
456
+ [s__namespace.SyntaxKind.ElementAccessExpression]: Y,
457
+ [s__namespace.SyntaxKind.ConditionalExpression]: q,
458
+ [s__namespace.SyntaxKind.TypeOfExpression]: C$1,
459
+ [s__namespace.SyntaxKind.ParenthesizedExpression]: H$1,
460
+ [s__namespace.SyntaxKind.ObjectLiteralExpression]: V
453
461
  };
454
- function W$1(e2) {
455
- const t2 = R(e2);
456
- if (t2.errors.length > 0)
462
+ function j$2(e2) {
463
+ const t2 = [], i2 = R(e2);
464
+ if (i2.errors.length > 0)
457
465
  return {
458
466
  success: false,
459
467
  bytecode: [],
460
- errors: t2.errors
468
+ errors: i2.errors.map((c2) => ({
469
+ message: c2,
470
+ line: 0,
471
+ character: 0
472
+ }))
461
473
  };
462
- const s2 = new A$1(), r = i__namespace.createSourceFile("program.ts", e2, i__namespace.ScriptTarget.Latest, true), a2 = {
463
- compileStatement: S,
464
- compileExpression: c2,
465
- reportError: (o2, m2) => {
466
- throw new Error(m2);
474
+ const r2 = new p$2(), a2 = s__namespace.createSourceFile("program.ts", e2, s__namespace.ScriptTarget.Latest, true), m2 = {
475
+ compileStatement: o2,
476
+ compileExpression: S,
477
+ reportError: (c2, l2) => {
478
+ const { line: T, character: x2 } = a2.getLineAndCharacterOfPosition(c2.getStart());
479
+ throw t2.push({
480
+ message: l2,
481
+ line: T + 1,
482
+ // TypeScript uses 0-based lines
483
+ character: x2 + 1
484
+ // TypeScript uses 0-based columns
485
+ }), new Error(l2);
467
486
  }
468
487
  };
469
- function S(o2) {
470
- const m2 = L$1[o2.kind];
471
- m2 && m2(o2, s2, a2);
472
- }
473
- function c2(o2) {
474
- const m2 = j$2[o2.kind];
475
- m2 && m2(o2, s2, a2);
476
- }
477
- return r.forEachChild((o2) => {
478
- var m2;
479
- i__namespace.isFunctionDeclaration(o2) && ((m2 = o2.name) == null ? void 0 : m2.text) === "main" && o2.body && o2.body.statements.forEach((l2) => {
480
- S(l2);
488
+ function o2(c2) {
489
+ const l2 = k[c2.kind];
490
+ if (l2)
491
+ l2(c2, r2, m2);
492
+ else {
493
+ const { line: T, character: x2 } = a2.getLineAndCharacterOfPosition(c2.getStart());
494
+ t2.push({
495
+ message: `Unsupported statement: ${s__namespace.SyntaxKind[c2.kind]}`,
496
+ line: T + 1,
497
+ character: x2 + 1
498
+ });
499
+ }
500
+ }
501
+ function S(c2) {
502
+ const l2 = W$1[c2.kind];
503
+ if (l2)
504
+ l2(c2, r2, m2);
505
+ else {
506
+ const { line: T, character: x2 } = a2.getLineAndCharacterOfPosition(c2.getStart());
507
+ t2.push({
508
+ message: `Unsupported expression: ${s__namespace.SyntaxKind[c2.kind]}`,
509
+ line: T + 1,
510
+ character: x2 + 1
511
+ });
512
+ }
513
+ }
514
+ return a2.forEachChild((c2) => {
515
+ var l2;
516
+ s__namespace.isFunctionDeclaration(c2) && ((l2 = c2.name) == null ? void 0 : l2.text) === "main" && c2.body && c2.body.statements.forEach((T) => {
517
+ try {
518
+ o2(T);
519
+ } catch {
520
+ }
481
521
  });
482
- }), s2.emit(n$1.HALT), {
483
- success: true,
484
- bytecode: s2.getBytecode(),
485
- errors: []
522
+ }), r2.emit(n.HALT), {
523
+ success: t2.length === 0,
524
+ bytecode: r2.getBytecode(),
525
+ errors: t2
486
526
  };
487
527
  }
488
- function n(r) {
489
- return typeof r == "string";
528
+ function t(r2) {
529
+ return typeof r2 == "string";
530
+ }
531
+ function i$1(r2) {
532
+ return typeof r2 == "number";
490
533
  }
491
- function t(r) {
492
- return typeof r == "number";
534
+ function e(r2) {
535
+ return typeof r2 == "boolean";
493
536
  }
494
- function i$1(r) {
495
- return typeof r == "boolean";
537
+ function f$1(r2) {
538
+ return r2 === null;
496
539
  }
497
- function e(r) {
498
- return r === null;
540
+ function o(r2) {
541
+ return r2 !== null && typeof r2 == "object" && "type" in r2 && r2.type === "array";
499
542
  }
500
- function f(r) {
501
- return r !== null && typeof r == "object" && "type" in r && r.type === "array";
543
+ function c(r2) {
544
+ return r2 !== null && typeof r2 == "object" && "type" in r2 && r2.type === "undefined";
502
545
  }
503
- function o(r) {
504
- return r !== null && typeof r == "object" && "type" in r && r.type === "undefined";
546
+ function y(r2) {
547
+ return r2 !== null && typeof r2 == "object" && "type" in r2 && r2.type === "object";
505
548
  }
506
- function c(r) {
507
- return r !== null && typeof r == "object" && "type" in r && r.type === "object";
549
+ function u$1(r2) {
550
+ return r2 !== null && typeof r2 == "object" && "type" in r2 && r2.type === "array-ref";
508
551
  }
509
- function y(r) {
510
- return n(r) ? r : t(r) || i$1(r) ? r.toString() : e(r) ? "null" : o(r) ? "undefined" : f(r) ? `[array:${r.elements.length}]` : c(r) ? "[object Object]" : String(r);
552
+ function b(r2) {
553
+ return r2 !== null && typeof r2 == "object" && "type" in r2 && r2.type === "object-ref";
511
554
  }
512
- function b$1(r) {
513
- return i$1(r) ? r : e(r) || o(r) ? false : t(r) ? r !== 0 : n(r) ? r !== "" : f(r) || c(r) ? true : !!r;
555
+ function p$1(r2) {
556
+ return t(r2) ? r2 : i$1(r2) || e(r2) ? r2.toString() : f$1(r2) ? "null" : c(r2) ? "undefined" : o(r2) ? `[array:${r2.elements.length}]` : y(r2) ? "[object Object]" : String(r2);
514
557
  }
515
- function p(r) {
516
- return n(r) ? "string" : t(r) ? "number" : i$1(r) ? "boolean" : e(r) ? "null" : o(r) ? "undefined" : f(r) ? "array" : c(r) ? "object" : "unknown";
558
+ function j$1(r2) {
559
+ return e(r2) ? r2 : f$1(r2) || c(r2) ? false : i$1(r2) ? r2 !== 0 : t(r2) ? r2 !== "" : o(r2) || y(r2) ? true : !!r2;
517
560
  }
518
- function m(r) {
519
- if (t(r)) return r;
520
- if (i$1(r)) return r ? 1 : 0;
521
- if (e(r)) return 0;
522
- if (o(r)) return NaN;
523
- if (n(r)) {
524
- const u2 = r.trim();
525
- return u2 === "" ? 0 : Number(u2);
526
- }
527
- return f(r) ? NaN : Number(r);
561
+ function d$2(r2) {
562
+ return t(r2) ? "string" : i$1(r2) ? "number" : e(r2) ? "boolean" : f$1(r2) ? "null" : c(r2) ? "undefined" : o(r2) || u$1(r2) ? "array" : y(r2) || b(r2) ? "object" : "unknown";
528
563
  }
529
- function d(r = []) {
530
- return { type: "array", elements: r };
564
+ function m(r2) {
565
+ if (i$1(r2)) return r2;
566
+ if (e(r2)) return r2 ? 1 : 0;
567
+ if (f$1(r2)) return 0;
568
+ if (c(r2)) return NaN;
569
+ if (t(r2)) {
570
+ const n2 = r2.trim();
571
+ return n2 === "" ? 0 : Number(n2);
572
+ }
573
+ return o(r2) ? NaN : Number(r2);
531
574
  }
532
- function s() {
575
+ function s(r2 = [], n2) {
576
+ return n2 ? { type: "array", elements: r2, properties: n2 } : { type: "array", elements: r2 };
577
+ }
578
+ function C() {
533
579
  return { type: "undefined" };
534
580
  }
535
- function j$1(r = {}) {
536
- return { type: "object", properties: r };
581
+ function M$1(r2 = {}) {
582
+ return { type: "object", properties: r2 };
537
583
  }
538
- var l = Object.defineProperty;
539
- var u$1 = (s2, t2, e2) => t2 in s2 ? l(s2, t2, { enumerable: true, configurable: true, writable: true, value: e2 }) : s2[t2] = e2;
540
- var a = (s2, t2, e2) => u$1(s2, typeof t2 != "symbol" ? t2 + "" : t2, e2);
541
- let h$1 = class h {
584
+ var d$1 = Object.defineProperty;
585
+ var h = (s2, t2, n2) => t2 in s2 ? d$1(s2, t2, { enumerable: true, configurable: true, writable: true, value: n2 }) : s2[t2] = n2;
586
+ var a = (s2, t2, n2) => h(s2, typeof t2 != "symbol" ? t2 + "" : t2, n2);
587
+ class p2 {
542
588
  constructor(t2) {
543
589
  a(this, "connected", false);
544
590
  a(this, "programsDir");
545
591
  a(this, "executionsDir");
546
592
  a(this, "outputsDir");
547
- this.dataDir = t2, this.programsDir = c__namespace.join(t2, "programs"), this.executionsDir = c__namespace.join(t2, "executions"), this.outputsDir = c__namespace.join(t2, "outputs");
593
+ this.dataDir = t2, this.programsDir = r__namespace.join(t2, "programs"), this.executionsDir = r__namespace.join(t2, "executions"), this.outputsDir = r__namespace.join(t2, "outputs");
548
594
  }
549
595
  get metadataFile() {
550
- return c__namespace.join(this.dataDir, "metadata.json");
596
+ return r__namespace.join(this.dataDir, "metadata.json");
551
597
  }
552
598
  async connect() {
553
- await M$2.promises.mkdir(this.dataDir, { recursive: true }), await M$2.promises.mkdir(this.programsDir, { recursive: true }), await M$2.promises.mkdir(this.executionsDir, { recursive: true }), await M$2.promises.mkdir(this.outputsDir, { recursive: true }), this.connected = true;
599
+ await b$2.promises.mkdir(this.dataDir, { recursive: true }), await b$2.promises.mkdir(this.programsDir, { recursive: true }), await b$2.promises.mkdir(this.executionsDir, { recursive: true }), await b$2.promises.mkdir(this.outputsDir, { recursive: true }), this.connected = true;
554
600
  }
555
601
  async disconnect() {
556
602
  this.connected = false;
@@ -560,71 +606,71 @@ let h$1 = class h {
560
606
  }
561
607
  async saveProgram(t2) {
562
608
  if (!this.connected) throw new Error("Not connected");
563
- const e2 = c__namespace.join(this.programsDir, `${t2.id}.json`), n2 = JSON.stringify(t2, null, 2);
564
- await M$2.promises.writeFile(e2, n2, "utf-8");
609
+ const n2 = r__namespace.join(this.programsDir, `${t2.id}.json`), e2 = JSON.stringify(t2, null, 2);
610
+ await b$2.promises.writeFile(n2, e2, "utf-8");
565
611
  }
566
612
  async getProgram(t2) {
567
613
  if (!this.connected) throw new Error("Not connected");
568
- const e2 = c__namespace.join(this.programsDir, `${t2}.json`);
614
+ const n2 = r__namespace.join(this.programsDir, `${t2}.json`);
569
615
  try {
570
- const n2 = await M$2.promises.readFile(e2, "utf-8"), o2 = JSON.parse(n2);
616
+ const e2 = await b$2.promises.readFile(n2, "utf-8"), o2 = JSON.parse(e2);
571
617
  return o2.created = new Date(o2.created), o2.updated && (o2.updated = new Date(o2.updated)), o2;
572
- } catch (n2) {
573
- if (n2.code === "ENOENT")
618
+ } catch (e2) {
619
+ if (e2.code === "ENOENT")
574
620
  return null;
575
- throw n2;
621
+ throw e2;
576
622
  }
577
623
  }
578
624
  async saveExecution(t2) {
579
625
  if (!this.connected) throw new Error("Not connected");
580
- const e2 = c__namespace.join(this.executionsDir, `${t2.id}.json`), n2 = JSON.stringify(t2, null, 2);
581
- await M$2.promises.writeFile(e2, n2, "utf-8");
626
+ const n2 = r__namespace.join(this.executionsDir, `${t2.id}.json`), e2 = JSON.stringify(t2, null, 2);
627
+ await b$2.promises.writeFile(n2, e2, "utf-8");
582
628
  }
583
629
  async getExecution(t2) {
584
630
  if (!this.connected) throw new Error("Not connected");
585
- const e2 = c__namespace.join(this.executionsDir, `${t2}.json`);
631
+ const n2 = r__namespace.join(this.executionsDir, `${t2}.json`);
586
632
  try {
587
- const n2 = await M$2.promises.readFile(e2, "utf-8"), o2 = JSON.parse(n2);
633
+ const e2 = await b$2.promises.readFile(n2, "utf-8"), o2 = JSON.parse(e2);
588
634
  return o2.created = new Date(o2.created), o2.updated && (o2.updated = new Date(o2.updated)), o2;
589
- } catch (n2) {
590
- if (n2.code === "ENOENT")
635
+ } catch (e2) {
636
+ if (e2.code === "ENOENT")
591
637
  return null;
592
- throw n2;
638
+ throw e2;
593
639
  }
594
640
  }
595
- async appendOutput(t2, e2) {
641
+ async appendOutput(t2, n2) {
596
642
  if (!this.connected) throw new Error("Not connected");
597
- const n2 = c__namespace.join(this.outputsDir, `${t2}.output`), o2 = e2.join(`
643
+ const e2 = r__namespace.join(this.outputsDir, `${t2}.output`), o2 = n2.join(`
598
644
  `) + `
599
645
  `;
600
- await M$2.promises.appendFile(n2, o2, "utf-8");
646
+ await b$2.promises.appendFile(e2, o2, "utf-8");
601
647
  }
602
648
  async getOutput(t2) {
603
649
  if (!this.connected) throw new Error("Not connected");
604
- const e2 = c__namespace.join(this.outputsDir, `${t2}.output`);
650
+ const n2 = r__namespace.join(this.outputsDir, `${t2}.output`);
605
651
  try {
606
- return (await M$2.promises.readFile(e2, "utf-8")).split(`
652
+ return (await b$2.promises.readFile(n2, "utf-8")).split(`
607
653
  `).filter((o2) => o2.length > 0);
608
- } catch (n2) {
609
- if (n2.code === "ENOENT")
654
+ } catch (e2) {
655
+ if (e2.code === "ENOENT")
610
656
  return [];
611
- throw n2;
657
+ throw e2;
612
658
  }
613
659
  }
614
660
  async listExecutions() {
615
661
  if (!this.connected) throw new Error("Not connected");
616
- const t2 = await M$2.promises.readdir(this.executionsDir), e2 = [];
617
- for (const n2 of t2)
618
- if (n2.endsWith(".json")) {
619
- const o2 = n2.slice(0, -5), r = await this.getExecution(o2);
620
- r && e2.push(r);
662
+ const t2 = await b$2.promises.readdir(this.executionsDir), n2 = [];
663
+ for (const e2 of t2)
664
+ if (e2.endsWith(".json")) {
665
+ const o2 = e2.slice(0, -5), i2 = await this.getExecution(o2);
666
+ i2 && n2.push(i2);
621
667
  }
622
- return e2;
668
+ return n2;
623
669
  }
624
670
  async getCurrentExecutionId() {
625
671
  if (!this.connected) throw new Error("Not connected");
626
672
  try {
627
- const t2 = await M$2.promises.readFile(this.metadataFile, "utf-8");
673
+ const t2 = await b$2.promises.readFile(this.metadataFile, "utf-8");
628
674
  return JSON.parse(t2).currentExecutionId || null;
629
675
  } catch (t2) {
630
676
  if (t2.code === "ENOENT")
@@ -634,27 +680,46 @@ let h$1 = class h {
634
680
  }
635
681
  async setCurrentExecutionId(t2) {
636
682
  if (!this.connected) throw new Error("Not connected");
637
- const e2 = { currentExecutionId: t2 };
638
- await M$2.promises.writeFile(this.metadataFile, JSON.stringify(e2, null, 2), "utf-8");
683
+ const n2 = { currentExecutionId: t2 };
684
+ await b$2.promises.writeFile(this.metadataFile, JSON.stringify(n2, null, 2), "utf-8");
639
685
  }
640
686
  async deleteExecution(t2) {
641
687
  if (!this.connected) throw new Error("Not connected");
642
- const e2 = c__namespace.join(this.executionsDir, `${t2}.json`);
688
+ const n2 = r__namespace.join(this.executionsDir, `${t2}.json`);
643
689
  try {
644
- await M$2.promises.unlink(e2);
645
- } catch (r) {
646
- if (r.code !== "ENOENT") throw r;
690
+ await b$2.promises.unlink(n2);
691
+ } catch (i2) {
692
+ if (i2.code !== "ENOENT") throw i2;
647
693
  }
648
- const n2 = c__namespace.join(this.outputsDir, `${t2}.output`);
694
+ const e2 = r__namespace.join(this.outputsDir, `${t2}.output`);
649
695
  try {
650
- await M$2.promises.unlink(n2);
651
- } catch (r) {
652
- if (r.code !== "ENOENT") throw r;
696
+ await b$2.promises.unlink(e2);
697
+ } catch (i2) {
698
+ if (i2.code !== "ENOENT") throw i2;
653
699
  }
654
700
  await this.getCurrentExecutionId() === t2 && await this.setCurrentExecutionId(null);
655
701
  }
656
- };
657
- class w {
702
+ async listPrograms() {
703
+ if (!this.connected) throw new Error("Not connected");
704
+ const t2 = await b$2.promises.readdir(this.programsDir), n2 = [];
705
+ for (const e2 of t2)
706
+ if (e2.endsWith(".json")) {
707
+ const o2 = e2.slice(0, -5), i2 = await this.getProgram(o2);
708
+ i2 && n2.push(i2);
709
+ }
710
+ return n2;
711
+ }
712
+ async deleteProgram(t2) {
713
+ if (!this.connected) throw new Error("Not connected");
714
+ const n2 = r__namespace.join(this.programsDir, `${t2}.json`);
715
+ try {
716
+ await b$2.promises.unlink(n2);
717
+ } catch (e2) {
718
+ if (e2.code !== "ENOENT") throw e2;
719
+ }
720
+ }
721
+ }
722
+ class f {
658
723
  constructor(t2) {
659
724
  a(this, "client");
660
725
  a(this, "db", null);
@@ -666,8 +731,8 @@ class w {
666
731
  await this.client.connect();
667
732
  const t2 = ((o2 = this.connectionString.split("/").pop()) == null ? void 0 : o2.split("?")[0]) || "cvm";
668
733
  this.db = this.client.db(t2), this.connected = true;
669
- const n2 = (await this.db.listCollections().toArray()).map((r) => r.name);
670
- n2.includes("programs") || await this.db.createCollection("programs"), n2.includes("executions") || await this.db.createCollection("executions"), n2.includes("outputs") || await this.db.createCollection("outputs"), n2.includes("metadata") || await this.db.createCollection("metadata");
734
+ const e2 = (await this.db.listCollections().toArray()).map((i2) => i2.name);
735
+ e2.includes("programs") || await this.db.createCollection("programs"), e2.includes("executions") || await this.db.createCollection("executions"), e2.includes("outputs") || await this.db.createCollection("outputs"), e2.includes("metadata") || await this.db.createCollection("metadata");
671
736
  }
672
737
  async disconnect() {
673
738
  await this.client.close(), this.connected = false, this.db = null;
@@ -677,7 +742,7 @@ class w {
677
742
  }
678
743
  async getCollections() {
679
744
  if (!this.db) throw new Error("Not connected to database");
680
- return (await this.db.listCollections().toArray()).map((e2) => e2.name);
745
+ return (await this.db.listCollections().toArray()).map((n2) => n2.name);
681
746
  }
682
747
  getCollection(t2) {
683
748
  if (!this.db) throw new Error("Not connected to database");
@@ -703,23 +768,23 @@ class w {
703
768
  async getExecution(t2) {
704
769
  return await this.getCollection("executions").findOne({ id: t2 });
705
770
  }
706
- async appendOutput(t2, e2) {
707
- const n2 = this.getCollection("outputs");
708
- await n2.findOne({ executionId: t2 }) ? await n2.updateOne(
771
+ async appendOutput(t2, n2) {
772
+ const e2 = this.getCollection("outputs");
773
+ await e2.findOne({ executionId: t2 }) ? await e2.updateOne(
709
774
  { executionId: t2 },
710
- { $push: { lines: { $each: e2 } } }
711
- ) : await n2.insertOne({ executionId: t2, lines: e2 });
775
+ { $push: { lines: { $each: n2 } } }
776
+ ) : await e2.insertOne({ executionId: t2, lines: n2 });
712
777
  }
713
778
  async getOutput(t2) {
714
- const n2 = await this.getCollection("outputs").findOne({ executionId: t2 });
715
- return (n2 == null ? void 0 : n2.lines) || [];
779
+ const e2 = await this.getCollection("outputs").findOne({ executionId: t2 });
780
+ return (e2 == null ? void 0 : e2.lines) || [];
716
781
  }
717
782
  async listExecutions() {
718
783
  return await this.getCollection("executions").find({}).toArray();
719
784
  }
720
785
  async getCurrentExecutionId() {
721
- const e2 = await this.getCollection("metadata").findOne({ _id: "current" });
722
- return (e2 == null ? void 0 : e2.currentExecutionId) || null;
786
+ const n2 = await this.getCollection("metadata").findOne({ _id: "current" });
787
+ return (n2 == null ? void 0 : n2.currentExecutionId) || null;
723
788
  }
724
789
  async setCurrentExecutionId(t2) {
725
790
  await this.getCollection("metadata").replaceOne(
@@ -729,136 +794,224 @@ class w {
729
794
  );
730
795
  }
731
796
  async deleteExecution(t2) {
732
- await this.getCollection("executions").deleteOne({ id: t2 }), await this.getCollection("outputs").deleteOne({ executionId: t2 }), await this.getCurrentExecutionId() === t2 && await this.setCurrentExecutionId(null);
797
+ var o2;
798
+ let n2 = true;
799
+ const e2 = this.client.startSession();
800
+ try {
801
+ await e2.withTransaction(async () => {
802
+ await this.getCollection("executions").deleteOne({ id: t2 }, { session: e2 }), await this.getCollection("outputs").deleteMany({ executionId: t2 }, { session: e2 }), await this.getCurrentExecutionId() === t2 && await this.db.collection("system").updateOne(
803
+ { _id: "current" },
804
+ { $set: { executionId: null } },
805
+ { session: e2 }
806
+ );
807
+ });
808
+ } catch (i2) {
809
+ if ((o2 = i2 == null ? void 0 : i2.message) != null && o2.includes("Transaction numbers are only allowed"))
810
+ n2 = false;
811
+ else
812
+ throw i2;
813
+ } finally {
814
+ await e2.endSession();
815
+ }
816
+ n2 || (await this.getCollection("executions").deleteOne({ id: t2 }), await this.getCollection("outputs").deleteMany({ executionId: t2 }), await this.getCurrentExecutionId() === t2 && await this.setCurrentExecutionId(null));
817
+ }
818
+ async listPrograms() {
819
+ return await this.getCollection("programs").find({}).toArray();
820
+ }
821
+ async deleteProgram(t2) {
822
+ await this.getCollection("programs").deleteOne({ id: t2 });
733
823
  }
734
824
  }
735
825
  class E {
736
826
  static create(t2) {
737
- const e2 = (t2 == null ? void 0 : t2.type) || process.env.CVM_STORAGE_TYPE || "file";
738
- switch (e2) {
827
+ const n2 = (t2 == null ? void 0 : t2.type) || process.env.CVM_STORAGE_TYPE || "file";
828
+ switch (n2) {
739
829
  case "file": {
740
- const n2 = (t2 == null ? void 0 : t2.dataDir) || process.env.CVM_DATA_DIR || ".cvm";
741
- return new h$1(n2);
830
+ const e2 = (t2 == null ? void 0 : t2.dataDir) || process.env.CVM_DATA_DIR || ".cvm";
831
+ return new p2(e2);
742
832
  }
743
833
  case "mongodb": {
744
- const n2 = (t2 == null ? void 0 : t2.mongoUri) || process.env.MONGODB_URI || "mongodb://localhost:27017/cvm";
745
- return new w(n2);
834
+ const e2 = (t2 == null ? void 0 : t2.mongoUri) || process.env.MONGODB_URI || "mongodb://localhost:27017/cvm";
835
+ return new f(e2);
746
836
  }
747
837
  default:
748
- throw new Error(`Unsupported storage type: ${e2}`);
838
+ throw new Error(`Unsupported storage type: ${n2}`);
749
839
  }
750
840
  }
751
841
  }
752
- var A2 = Object.defineProperty;
753
- var b = (e2, r, t2) => r in e2 ? A2(e2, r, { enumerable: true, configurable: true, writable: true, value: t2 }) : e2[r] = t2;
754
- var h2 = (e2, r, t2) => b(e2, typeof r != "symbol" ? r + "" : r, t2);
755
- const G = {
756
- [n$1.ADD]: {
842
+ var H = Object.defineProperty;
843
+ var D = (e2, t2, r2) => t2 in e2 ? H(e2, t2, { enumerable: true, configurable: true, writable: true, value: r2 }) : e2[t2] = r2;
844
+ var _ = (e2, t2, r2) => D(e2, typeof t2 != "symbol" ? t2 + "" : t2, r2);
845
+ function z(e2) {
846
+ return !!(e2 && typeof e2 == "object" && "type" in e2 && "message" in e2);
847
+ }
848
+ function l(e2, t2) {
849
+ return e2.stack.length < 1 ? {
850
+ type: "RuntimeError",
851
+ message: "Stack underflow",
852
+ pc: e2.pc,
853
+ opcode: t2 || n.POP
854
+ } : e2.stack.pop();
855
+ }
856
+ const d = z, B = {
857
+ [n.ADD]: {
757
858
  stackIn: 2,
758
859
  stackOut: 1,
759
- execute: (e2) => {
760
- const r = e2.stack.pop(), t2 = e2.stack.pop(), s2 = m(t2), o2 = m(r);
761
- e2.stack.push(s2 + o2);
860
+ execute: (e2, t$1) => {
861
+ const r2 = l(e2, t$1.op);
862
+ if (d(r2)) return r2;
863
+ const o2 = l(e2, t$1.op);
864
+ if (d(o2)) return o2;
865
+ if (t(o2) || t(r2))
866
+ e2.stack.push(p$1(o2) + p$1(r2));
867
+ else {
868
+ const s2 = m(o2), c2 = m(r2);
869
+ e2.stack.push(s2 + c2);
870
+ }
762
871
  }
763
872
  },
764
- [n$1.SUB]: {
873
+ [n.SUB]: {
765
874
  stackIn: 2,
766
875
  stackOut: 1,
767
- execute: (e2) => {
768
- const r = e2.stack.pop(), t2 = e2.stack.pop(), s2 = m(t2), o2 = m(r);
769
- e2.stack.push(s2 - o2);
876
+ execute: (e2, t2) => {
877
+ const r2 = l(e2, t2.op);
878
+ if (d(r2)) return r2;
879
+ const o2 = l(e2, t2.op);
880
+ if (d(o2)) return o2;
881
+ const s2 = m(o2), c2 = m(r2);
882
+ e2.stack.push(s2 - c2);
770
883
  }
771
884
  },
772
- [n$1.MUL]: {
885
+ [n.MUL]: {
773
886
  stackIn: 2,
774
887
  stackOut: 1,
775
- execute: (e2) => {
776
- const r = e2.stack.pop(), t2 = e2.stack.pop(), s2 = m(t2), o2 = m(r);
777
- e2.stack.push(s2 * o2);
888
+ execute: (e2, t2) => {
889
+ const r2 = l(e2, t2.op);
890
+ if (d(r2)) return r2;
891
+ const o2 = l(e2, t2.op);
892
+ if (d(o2)) return o2;
893
+ const s2 = m(o2), c2 = m(r2);
894
+ e2.stack.push(s2 * c2);
778
895
  }
779
896
  },
780
- [n$1.DIV]: {
897
+ [n.DIV]: {
781
898
  stackIn: 2,
782
899
  stackOut: 1,
783
- execute: (e2, r) => {
784
- const t2 = e2.stack.pop(), s2 = e2.stack.pop(), o2 = m(s2), n2 = m(t2);
785
- if (n2 === 0)
900
+ execute: (e2, t2) => {
901
+ const r2 = l(e2, t2.op);
902
+ if (d(r2)) return r2;
903
+ const o2 = l(e2, t2.op);
904
+ if (d(o2)) return o2;
905
+ const s2 = m(o2), c2 = m(r2);
906
+ if (c2 === 0)
786
907
  return {
787
908
  type: "DivisionByZero",
788
909
  message: "Division by zero",
789
910
  pc: e2.pc,
790
- opcode: r.op
911
+ opcode: t2.op
791
912
  };
792
- e2.stack.push(o2 / n2);
913
+ e2.stack.push(s2 / c2);
793
914
  }
794
915
  },
795
- [n$1.MOD]: {
916
+ [n.MOD]: {
796
917
  stackIn: 2,
797
918
  stackOut: 1,
798
- execute: (e2) => {
799
- const r = e2.stack.pop(), t2 = e2.stack.pop(), s2 = m(t2), o2 = m(r);
800
- e2.stack.push(s2 % o2);
919
+ execute: (e2, t2) => {
920
+ const r2 = l(e2, t2.op);
921
+ if (d(r2)) return r2;
922
+ const o2 = l(e2, t2.op);
923
+ if (d(o2)) return o2;
924
+ const s2 = m(o2), c2 = m(r2);
925
+ e2.stack.push(s2 % c2);
801
926
  }
802
927
  },
803
- [n$1.UNARY_MINUS]: {
928
+ [n.UNARY_MINUS]: {
804
929
  stackIn: 1,
805
930
  stackOut: 1,
806
- execute: (e2) => {
807
- const r = e2.stack.pop(), t2 = m(r);
808
- e2.stack.push(-t2);
931
+ execute: (e2, t2) => {
932
+ const r2 = l(e2, t2.op);
933
+ if (d(r2)) return r2;
934
+ const o2 = m(r2);
935
+ e2.stack.push(-o2);
809
936
  }
810
937
  },
811
- [n$1.UNARY_PLUS]: {
938
+ [n.UNARY_PLUS]: {
812
939
  stackIn: 1,
813
940
  stackOut: 1,
814
- execute: (e2) => {
815
- const r = e2.stack.pop(), t2 = m(r);
816
- e2.stack.push(t2);
941
+ execute: (e2, t2) => {
942
+ const r2 = l(e2, t2.op);
943
+ if (d(r2)) return r2;
944
+ const o2 = m(r2);
945
+ e2.stack.push(o2);
817
946
  }
818
947
  }
819
- }, L = {
820
- [n$1.PUSH]: {
948
+ }, W = {
949
+ [n.PUSH]: {
821
950
  stackIn: 0,
822
951
  stackOut: 1,
823
- execute: (e2, r) => {
824
- e2.stack.push(r.arg);
952
+ execute: (e2, t2) => {
953
+ e2.stack.push(t2.arg);
825
954
  }
826
955
  },
827
- [n$1.PUSH_UNDEFINED]: {
956
+ [n.PUSH_UNDEFINED]: {
828
957
  stackIn: 0,
829
958
  stackOut: 1,
830
959
  execute: (e2) => {
831
- e2.stack.push(s());
960
+ e2.stack.push(C());
832
961
  }
833
962
  },
834
- [n$1.POP]: {
963
+ [n.POP]: {
835
964
  stackIn: 1,
836
965
  stackOut: 0,
837
966
  execute: (e2) => {
838
967
  e2.stack.pop();
839
968
  }
969
+ },
970
+ [n.DUP]: {
971
+ stackIn: 1,
972
+ stackOut: 2,
973
+ execute: (e2, t2) => {
974
+ const r2 = e2.stack.pop();
975
+ e2.stack.push(r2), e2.stack.push(r2);
976
+ }
977
+ },
978
+ [n.SWAP]: {
979
+ stackIn: 2,
980
+ stackOut: 2,
981
+ execute: (e2, t2) => {
982
+ const r2 = e2.stack.pop(), o2 = e2.stack.pop();
983
+ e2.stack.push(r2), e2.stack.push(o2);
984
+ }
985
+ },
986
+ [n.DUP2]: {
987
+ stackIn: 2,
988
+ stackOut: 4,
989
+ execute: (e2, t2) => {
990
+ const r2 = e2.stack.pop(), o2 = e2.stack.pop();
991
+ e2.stack.push(o2), e2.stack.push(r2), e2.stack.push(o2), e2.stack.push(r2);
992
+ }
840
993
  }
841
- }, V = {
842
- [n$1.PRINT]: {
994
+ }, X = {
995
+ [n.PRINT]: {
843
996
  stackIn: 1,
844
997
  stackOut: 0,
845
998
  execute: (e2) => {
846
- const r = e2.stack.pop();
847
- r !== void 0 && e2.output.push(y(r));
999
+ const t2 = e2.stack.pop();
1000
+ t2 !== void 0 && e2.output.push(p$1(t2));
848
1001
  }
849
1002
  },
850
- [n$1.CC]: {
1003
+ [n.CC]: {
851
1004
  stackIn: 1,
852
1005
  stackOut: 0,
853
1006
  controlsPC: true,
854
1007
  // CC pauses execution
855
- execute: (e2, r) => {
856
- const t2 = e2.stack.pop();
857
- e2.ccPrompt = y(t2), e2.status = "waiting_cc";
1008
+ execute: (e2, t2) => {
1009
+ const r2 = e2.stack.pop();
1010
+ e2.ccPrompt = p$1(r2), e2.status = "waiting_cc";
858
1011
  }
859
1012
  }
860
- }, F = {
861
- [n$1.HALT]: {
1013
+ }, K = {
1014
+ [n.HALT]: {
862
1015
  stackIn: 0,
863
1016
  stackOut: 0,
864
1017
  controlsPC: true,
@@ -866,102 +1019,102 @@ const G = {
866
1019
  e2.status = "complete";
867
1020
  }
868
1021
  },
869
- [n$1.JUMP]: {
1022
+ [n.JUMP]: {
870
1023
  stackIn: 0,
871
1024
  stackOut: 0,
872
1025
  controlsPC: true,
873
- execute: (e2, r) => {
874
- if (r.arg === void 0)
1026
+ execute: (e2, t2) => {
1027
+ if (t2.arg === void 0)
875
1028
  return {
876
1029
  type: "RuntimeError",
877
1030
  message: "JUMP requires a target address",
878
1031
  pc: e2.pc,
879
- opcode: r.op
1032
+ opcode: t2.op
880
1033
  };
881
- const t2 = r.arg;
882
- if (t2 < 0)
1034
+ const r2 = t2.arg;
1035
+ if (r2 < 0)
883
1036
  return {
884
1037
  type: "RuntimeError",
885
- message: `Invalid jump target: ${t2}`,
1038
+ message: `Invalid jump target: ${r2}`,
886
1039
  pc: e2.pc,
887
- opcode: r.op
1040
+ opcode: t2.op
888
1041
  };
889
- e2.pc = t2;
1042
+ e2.pc = r2;
890
1043
  }
891
1044
  },
892
- [n$1.JUMP_IF_FALSE]: {
1045
+ [n.JUMP_IF_FALSE]: {
893
1046
  stackIn: 1,
894
1047
  stackOut: 0,
895
1048
  controlsPC: true,
896
- execute: (e2, r) => {
897
- const t2 = e2.stack.pop();
898
- if (r.arg === void 0)
1049
+ execute: (e2, t2) => {
1050
+ const r2 = e2.stack.pop();
1051
+ if (t2.arg === void 0)
899
1052
  return {
900
1053
  type: "RuntimeError",
901
1054
  message: "JUMP_IF_FALSE requires a target address",
902
1055
  pc: e2.pc,
903
- opcode: r.op
1056
+ opcode: t2.op
904
1057
  };
905
- const s2 = r.arg;
906
- if (s2 < 0)
1058
+ const o2 = t2.arg;
1059
+ if (o2 < 0)
907
1060
  return {
908
1061
  type: "RuntimeError",
909
- message: `Invalid jump target: ${s2}`,
1062
+ message: `Invalid jump target: ${o2}`,
910
1063
  pc: e2.pc,
911
- opcode: r.op
1064
+ opcode: t2.op
912
1065
  };
913
- b$1(t2) ? e2.pc++ : e2.pc = s2;
1066
+ j$1(r2) ? e2.pc++ : e2.pc = o2;
914
1067
  }
915
1068
  },
916
- [n$1.JUMP_IF]: {
1069
+ [n.JUMP_IF]: {
917
1070
  stackIn: 1,
918
1071
  stackOut: 0,
919
1072
  controlsPC: true,
920
- execute: (e2, r) => {
921
- const t2 = e2.stack.pop();
922
- if (r.arg === void 0)
1073
+ execute: (e2, t2) => {
1074
+ const r2 = e2.stack.pop();
1075
+ if (t2.arg === void 0)
923
1076
  return {
924
1077
  type: "RuntimeError",
925
1078
  message: "JUMP_IF requires a target address",
926
1079
  pc: e2.pc,
927
- opcode: r.op
1080
+ opcode: t2.op
928
1081
  };
929
- const s2 = r.arg;
930
- if (s2 < 0)
1082
+ const o2 = t2.arg;
1083
+ if (o2 < 0)
931
1084
  return {
932
1085
  type: "RuntimeError",
933
- message: `Invalid jump target: ${s2}`,
1086
+ message: `Invalid jump target: ${o2}`,
934
1087
  pc: e2.pc,
935
- opcode: r.op
1088
+ opcode: t2.op
936
1089
  };
937
- b$1(t2) ? e2.pc = s2 : e2.pc++;
1090
+ j$1(r2) ? e2.pc = o2 : e2.pc++;
938
1091
  }
939
1092
  },
940
- [n$1.JUMP_IF_TRUE]: {
1093
+ [n.JUMP_IF_TRUE]: {
941
1094
  stackIn: 1,
942
1095
  stackOut: 0,
943
1096
  controlsPC: true,
944
- execute: (e2, r) => {
945
- const t2 = e2.stack.pop();
946
- if (r.arg === void 0)
1097
+ execute: (e2, t2) => {
1098
+ const r2 = e2.stack.pop();
1099
+ if (t2.arg === void 0)
947
1100
  return {
948
1101
  type: "RuntimeError",
949
1102
  message: "JUMP_IF_TRUE requires a target address",
950
1103
  pc: e2.pc,
951
- opcode: r.op
1104
+ opcode: t2.op
952
1105
  };
953
- const s2 = r.arg;
954
- if (s2 < 0)
1106
+ const o2 = t2.arg;
1107
+ if (o2 < 0)
955
1108
  return {
956
1109
  type: "RuntimeError",
957
- message: `Invalid jump target: ${s2}`,
1110
+ message: `Invalid jump target: ${o2}`,
958
1111
  pc: e2.pc,
959
- opcode: r.op
1112
+ opcode: t2.op
960
1113
  };
961
- b$1(t2) ? e2.pc = s2 : e2.pc++;
1114
+ j$1(r2) ? e2.pc = o2 : e2.pc++;
962
1115
  }
963
1116
  },
964
- [n$1.CALL]: {
1117
+ [n.CALL]: {
965
1118
  stackIn: 1,
966
1119
  stackOut: 0,
967
1120
  controlsPC: false,
@@ -969,899 +1122,1417 @@ const G = {
969
1122
  type: "RuntimeError",
970
1123
  message: "Functions not implemented",
971
1124
  pc: e2.pc,
972
- opcode: n$1.CALL
1125
+ opcode: n.CALL
973
1126
  })
974
1127
  }
975
- }, j = {
976
- [n$1.LOAD]: {
1128
+ }, Q = {
1129
+ [n.LOAD]: {
977
1130
  stackIn: 0,
978
1131
  stackOut: 1,
979
- execute: (e2, r) => {
980
- const t2 = r.arg;
981
- e2.variables.has(t2) ? e2.stack.push(e2.variables.get(t2)) : e2.stack.push(s());
1132
+ execute: (e2, t2) => {
1133
+ const r2 = t2.arg;
1134
+ e2.variables.has(r2) ? e2.stack.push(e2.variables.get(r2)) : e2.stack.push(C());
982
1135
  }
983
1136
  },
984
- [n$1.STORE]: {
1137
+ [n.STORE]: {
985
1138
  stackIn: 1,
986
1139
  stackOut: 0,
987
- execute: (e2, r) => {
988
- const t2 = e2.stack.pop();
989
- e2.variables.set(r.arg, t2);
1140
+ execute: (e2, t2) => {
1141
+ const r2 = e2.stack.pop();
1142
+ e2.variables.set(t2.arg, r2);
990
1143
  }
991
1144
  }
992
- }, D = {
993
- [n$1.ITER_START]: {
1145
+ }, Z = {
1146
+ [n.ITER_START]: {
994
1147
  stackIn: 1,
995
1148
  stackOut: 0,
996
- execute: (e2, r) => {
997
- const t2 = e2.stack.pop();
998
- if (t2 == null)
1149
+ execute: (e2, t2) => {
1150
+ const r2 = e2.stack.pop();
1151
+ if (r2 == null)
999
1152
  return {
1000
1153
  type: "TypeError",
1001
1154
  message: "TypeError: Cannot iterate over null or undefined",
1002
1155
  pc: e2.pc,
1003
- opcode: r.op
1156
+ opcode: t2.op
1004
1157
  };
1005
- if (!f(t2))
1158
+ let o$1;
1159
+ if (u$1(r2)) {
1160
+ const s2 = e2.heap.get(r2.id);
1161
+ if (!s2 || s2.type !== "array")
1162
+ return {
1163
+ type: "RuntimeError",
1164
+ message: "Invalid array reference",
1165
+ pc: e2.pc,
1166
+ opcode: t2.op
1167
+ };
1168
+ o$1 = s2.data;
1169
+ } else if (o(r2))
1170
+ o$1 = r2;
1171
+ else
1006
1172
  return {
1007
1173
  type: "TypeError",
1008
1174
  message: "TypeError: Cannot iterate over non-array value",
1009
1175
  pc: e2.pc,
1010
- opcode: r.op
1176
+ opcode: t2.op
1011
1177
  };
1012
1178
  if (e2.iterators.length >= 10)
1013
1179
  return {
1014
1180
  type: "RuntimeError",
1015
1181
  message: "RuntimeError: Maximum iterator depth exceeded",
1016
1182
  pc: e2.pc,
1017
- opcode: r.op
1183
+ opcode: t2.op
1018
1184
  };
1019
1185
  e2.iterators.push({
1020
- array: t2,
1186
+ array: o$1,
1021
1187
  index: 0,
1022
- length: t2.elements.length
1188
+ length: o$1.elements.length
1023
1189
  });
1024
1190
  }
1025
1191
  },
1026
- [n$1.ITER_NEXT]: {
1192
+ [n.ITER_NEXT]: {
1027
1193
  stackIn: 0,
1028
1194
  stackOut: 2,
1029
1195
  // Pushes element and hasMore flag
1030
- execute: (e2, r) => {
1196
+ execute: (e2, t2) => {
1031
1197
  if (e2.iterators.length === 0)
1032
1198
  return {
1033
1199
  type: "RuntimeError",
1034
1200
  message: "ITER_NEXT: No active iterator",
1035
1201
  pc: e2.pc,
1036
- opcode: r.op
1202
+ opcode: t2.op
1037
1203
  };
1038
- const t2 = e2.iterators[e2.iterators.length - 1];
1039
- t2.index < t2.length ? (t2.index < t2.array.elements.length ? e2.stack.push(t2.array.elements[t2.index]) : e2.stack.push(s()), e2.stack.push(true), t2.index++) : (e2.stack.push(null), e2.stack.push(false));
1204
+ const r2 = e2.iterators[e2.iterators.length - 1];
1205
+ r2.index < r2.length ? (r2.index < r2.array.elements.length ? e2.stack.push(r2.array.elements[r2.index]) : e2.stack.push(C()), e2.stack.push(true), r2.index++) : (e2.stack.push(null), e2.stack.push(false));
1040
1206
  }
1041
1207
  },
1042
- [n$1.ITER_END]: {
1208
+ [n.ITER_END]: {
1043
1209
  stackIn: 0,
1044
1210
  stackOut: 0,
1045
- execute: (e2, r) => {
1211
+ execute: (e2, t2) => {
1046
1212
  if (e2.iterators.length === 0)
1047
1213
  return {
1048
1214
  type: "RuntimeError",
1049
1215
  message: "ITER_END: No active iterator",
1050
1216
  pc: e2.pc,
1051
- opcode: r.op
1217
+ opcode: t2.op
1052
1218
  };
1053
1219
  e2.iterators.pop();
1054
1220
  }
1055
1221
  }
1056
1222
  };
1057
- function g(e2, r, t2) {
1058
- const s2 = e2.stack.pop(), o2 = e2.stack.pop(), n2 = t2(o2, s2);
1059
- e2.stack.push(n2);
1223
+ function O(e2, t2, r2) {
1224
+ const o2 = l(e2, t2.op);
1225
+ if (d(o2)) return o2;
1226
+ const s2 = l(e2, t2.op);
1227
+ if (d(s2)) return s2;
1228
+ const c2 = r2(s2, o2);
1229
+ e2.stack.push(c2);
1060
1230
  }
1061
- const q = {
1062
- [n$1.EQ]: {
1231
+ const ee = {
1232
+ [n.EQ]: {
1063
1233
  stackIn: 2,
1064
1234
  stackOut: 1,
1065
- execute: (e$1, r) => g(e$1, r, (t2, s2) => {
1066
- if (e(t2) && o(s2) || o(t2) && e(s2))
1235
+ execute: (e2, t2) => O(e2, t2, (r2, o2) => {
1236
+ if (f$1(r2) && c(o2) || c(r2) && f$1(o2))
1067
1237
  return true;
1068
- if (o(t2) && o(s2))
1238
+ if (c(r2) && c(o2))
1069
1239
  return true;
1240
+ if (u$1(r2) || b(r2))
1241
+ return u$1(r2) && u$1(o2) || b(r2) && b(o2) ? r2.id === o2.id : false;
1070
1242
  {
1071
- const o2 = m(t2), n2 = m(s2);
1072
- return !isNaN(o2) && !isNaN(n2) ? o2 === n2 : y(t2) === y(s2);
1243
+ const s2 = m(r2), c2 = m(o2);
1244
+ return !isNaN(s2) && !isNaN(c2) ? s2 === c2 : p$1(r2) === p$1(o2);
1073
1245
  }
1074
1246
  })
1075
1247
  },
1076
- [n$1.NEQ]: {
1248
+ [n.NEQ]: {
1077
1249
  stackIn: 2,
1078
1250
  stackOut: 1,
1079
- execute: (e$1, r) => g(e$1, r, (t2, s2) => {
1080
- if (e(t2) && o(s2) || o(t2) && e(s2))
1251
+ execute: (e2, t2) => O(e2, t2, (r2, o2) => {
1252
+ if (f$1(r2) && c(o2) || c(r2) && f$1(o2))
1081
1253
  return false;
1082
- if (o(t2) && o(s2))
1254
+ if (c(r2) && c(o2))
1083
1255
  return false;
1256
+ if (u$1(r2) || b(r2))
1257
+ return u$1(r2) && u$1(o2) || b(r2) && b(o2) ? r2.id !== o2.id : true;
1084
1258
  {
1085
- const o2 = m(t2), n2 = m(s2);
1086
- return !isNaN(o2) && !isNaN(n2) ? o2 !== n2 : y(t2) !== y(s2);
1259
+ const s2 = m(r2), c2 = m(o2);
1260
+ return !isNaN(s2) && !isNaN(c2) ? s2 !== c2 : p$1(r2) !== p$1(o2);
1087
1261
  }
1088
1262
  })
1089
1263
  },
1090
- [n$1.LT]: {
1264
+ [n.LT]: {
1091
1265
  stackIn: 2,
1092
1266
  stackOut: 1,
1093
- execute: (e2, r) => g(e2, r, (t2, s2) => {
1094
- const o2 = m(t2), n2 = m(s2);
1095
- return o2 < n2;
1267
+ execute: (e2, t2) => O(e2, t2, (r2, o2) => {
1268
+ const s2 = m(r2), c2 = m(o2);
1269
+ return s2 < c2;
1096
1270
  })
1097
1271
  },
1098
- [n$1.GT]: {
1272
+ [n.GT]: {
1099
1273
  stackIn: 2,
1100
1274
  stackOut: 1,
1101
- execute: (e2, r) => g(e2, r, (t2, s2) => {
1102
- const o2 = m(t2), n2 = m(s2);
1103
- return o2 > n2;
1275
+ execute: (e2, t2) => O(e2, t2, (r2, o2) => {
1276
+ const s2 = m(r2), c2 = m(o2);
1277
+ return s2 > c2;
1104
1278
  })
1105
1279
  },
1106
- [n$1.LTE]: {
1280
+ [n.LTE]: {
1107
1281
  stackIn: 2,
1108
1282
  stackOut: 1,
1109
- execute: (e2, r) => g(e2, r, (t2, s2) => {
1110
- const o2 = m(t2), n2 = m(s2);
1111
- return o2 <= n2;
1283
+ execute: (e2, t2) => O(e2, t2, (r2, o2) => {
1284
+ const s2 = m(r2), c2 = m(o2);
1285
+ return s2 <= c2;
1112
1286
  })
1113
1287
  },
1114
- [n$1.GTE]: {
1288
+ [n.GTE]: {
1115
1289
  stackIn: 2,
1116
1290
  stackOut: 1,
1117
- execute: (e2, r) => g(e2, r, (t2, s2) => {
1118
- const o2 = m(t2), n2 = m(s2);
1119
- return o2 >= n2;
1291
+ execute: (e2, t2) => O(e2, t2, (r2, o2) => {
1292
+ const s2 = m(r2), c2 = m(o2);
1293
+ return s2 >= c2;
1120
1294
  })
1121
1295
  },
1122
- [n$1.EQ_STRICT]: {
1296
+ [n.EQ_STRICT]: {
1123
1297
  stackIn: 2,
1124
1298
  stackOut: 1,
1125
- execute: (e2, r) => g(e2, r, (t2, s2) => t2 === s2)
1299
+ execute: (e2, t2) => O(e2, t2, (r2, o2) => u$1(r2) && u$1(o2) || b(r2) && b(o2) ? r2.id === o2.id : r2 === o2)
1126
1300
  },
1127
- [n$1.NEQ_STRICT]: {
1301
+ [n.NEQ_STRICT]: {
1128
1302
  stackIn: 2,
1129
1303
  stackOut: 1,
1130
- execute: (e2, r) => g(e2, r, (t2, s2) => t2 !== s2)
1304
+ execute: (e2, t2) => O(e2, t2, (r2, o2) => u$1(r2) && u$1(o2) || b(r2) && b(o2) ? r2.id !== o2.id : r2 !== o2)
1131
1305
  }
1132
- }, H = {
1133
- [n$1.AND]: {
1306
+ }, re = {
1307
+ [n.AND]: {
1134
1308
  stackIn: 2,
1135
1309
  stackOut: 1,
1136
- execute: (e2, r) => {
1137
- const t2 = e2.stack.pop(), s2 = e2.stack.pop(), o2 = b$1(s2) ? t2 : s2;
1138
- e2.stack.push(o2);
1310
+ execute: (e2, t2) => {
1311
+ const r2 = l(e2, t2.op);
1312
+ if (d(r2)) return r2;
1313
+ const o2 = l(e2, t2.op);
1314
+ if (d(o2)) return o2;
1315
+ const s2 = j$1(o2) ? r2 : o2;
1316
+ e2.stack.push(s2);
1139
1317
  }
1140
1318
  },
1141
- [n$1.OR]: {
1319
+ [n.OR]: {
1142
1320
  stackIn: 2,
1143
1321
  stackOut: 1,
1144
- execute: (e2, r) => {
1145
- const t2 = e2.stack.pop(), s2 = e2.stack.pop(), o2 = b$1(s2) ? s2 : t2;
1146
- e2.stack.push(o2);
1322
+ execute: (e2, t2) => {
1323
+ const r2 = l(e2, t2.op);
1324
+ if (d(r2)) return r2;
1325
+ const o2 = l(e2, t2.op);
1326
+ if (d(o2)) return o2;
1327
+ const s2 = j$1(o2) ? o2 : r2;
1328
+ e2.stack.push(s2);
1147
1329
  }
1148
1330
  },
1149
- [n$1.NOT]: {
1331
+ [n.NOT]: {
1150
1332
  stackIn: 1,
1151
1333
  stackOut: 1,
1152
- execute: (e2, r) => {
1153
- const t2 = e2.stack.pop(), s2 = !b$1(t2);
1154
- e2.stack.push(s2);
1334
+ execute: (e2, t2) => {
1335
+ const r2 = l(e2, t2.op);
1336
+ if (d(r2)) return r2;
1337
+ const o2 = !j$1(r2);
1338
+ e2.stack.push(o2);
1155
1339
  }
1156
1340
  }
1157
- }, $ = {
1158
- [n$1.ARRAY_NEW]: {
1341
+ }, te = {
1342
+ /**
1343
+ * ARRAY_NEW: Creates a new empty array on the heap
1344
+ * Stack: [] -> [array-ref]
1345
+ */
1346
+ [n.ARRAY_NEW]: {
1159
1347
  stackIn: 0,
1160
1348
  stackOut: 1,
1161
- execute: (e2, r) => {
1162
- e2.stack.push(d());
1349
+ execute: (e2, t2) => {
1350
+ const r2 = s(), o2 = e2.heap.allocate("array", r2);
1351
+ e2.stack.push(o2);
1163
1352
  }
1164
1353
  },
1165
- [n$1.ARRAY_PUSH]: {
1354
+ /**
1355
+ * ARRAY_PUSH: Pushes a value to the end of an array
1356
+ * Stack: [array-ref, value] -> [array-ref]
1357
+ */
1358
+ [n.ARRAY_PUSH]: {
1166
1359
  stackIn: 2,
1167
1360
  stackOut: 1,
1168
- execute: (e2, r) => {
1169
- const t2 = e2.stack.pop(), s2 = e2.stack.pop();
1170
- if (!f(s2))
1361
+ execute: (e2, t2) => {
1362
+ const r2 = e2.stack.pop(), o$1 = e2.stack.pop();
1363
+ let s2;
1364
+ if (u$1(o$1)) {
1365
+ const c2 = e2.heap.get(o$1.id);
1366
+ if (!c2 || c2.type !== "array")
1367
+ return {
1368
+ type: "RuntimeError",
1369
+ message: "Invalid array reference",
1370
+ pc: e2.pc,
1371
+ opcode: t2.op
1372
+ };
1373
+ s2 = c2.data;
1374
+ } else if (o(o$1))
1375
+ s2 = o$1;
1376
+ else
1171
1377
  return {
1172
1378
  type: "RuntimeError",
1173
1379
  message: "ARRAY_PUSH requires an array",
1174
1380
  pc: e2.pc,
1175
- opcode: r.op
1381
+ opcode: t2.op
1176
1382
  };
1177
- s2.elements.push(t2), e2.stack.push(s2);
1383
+ s2.elements.push(r2), e2.stack.push(o$1);
1178
1384
  }
1179
1385
  },
1180
- [n$1.ARRAY_GET]: {
1386
+ [n.ARRAY_GET]: {
1181
1387
  stackIn: 2,
1182
1388
  stackOut: 1,
1183
- execute: (e2, r) => {
1184
- const t$1 = e2.stack.pop(), s2 = e2.stack.pop();
1185
- if (f(s2)) {
1186
- if (!t(t$1))
1389
+ execute: (e2, t$1) => {
1390
+ var u2;
1391
+ const r2 = e2.stack.pop(), o$1 = e2.stack.pop();
1392
+ let s2;
1393
+ if (u$1(o$1)) {
1394
+ const a2 = e2.heap.get(o$1.id);
1395
+ if (!a2 || a2.type !== "array")
1187
1396
  return {
1188
1397
  type: "RuntimeError",
1189
- message: "ARRAY_GET requires numeric index for arrays",
1398
+ message: "Invalid array reference",
1190
1399
  pc: e2.pc,
1191
- opcode: r.op
1400
+ opcode: t$1.op
1192
1401
  };
1193
- const o2 = s2.elements[t$1] ?? null;
1194
- e2.stack.push(o2);
1195
- return;
1196
- }
1197
- if (c(s2)) {
1198
- if (!n(t$1))
1402
+ s2 = a2.data;
1403
+ } else if (b(o$1)) {
1404
+ const a2 = e2.heap.get(o$1.id);
1405
+ if (!a2 || a2.type !== "object")
1199
1406
  return {
1200
1407
  type: "RuntimeError",
1201
- message: "ARRAY_GET requires string key for objects",
1408
+ message: "Invalid object reference",
1202
1409
  pc: e2.pc,
1203
- opcode: r.op
1410
+ opcode: t$1.op
1204
1411
  };
1205
- const o2 = s2.properties[t$1] ?? null;
1206
- e2.stack.push(o2);
1412
+ const i2 = a2.data, E2 = p$1(r2), x2 = i2.properties[E2] ?? C();
1413
+ e2.stack.push(x2);
1207
1414
  return;
1208
- }
1209
- return {
1210
- type: "RuntimeError",
1211
- message: "ARRAY_GET requires an array or object",
1212
- pc: e2.pc,
1213
- opcode: r.op
1214
- };
1415
+ } else if (o(o$1))
1416
+ s2 = o$1;
1417
+ else if (t(o$1)) {
1418
+ let a2;
1419
+ if (i$1(r2))
1420
+ a2 = r2;
1421
+ else if (t(r2)) {
1422
+ const i2 = parseInt(r2, 10);
1423
+ !isNaN(i2) && i2.toString() === r2 && (a2 = i2);
1424
+ }
1425
+ a2 !== void 0 && a2 >= 0 && a2 < o$1.length ? e2.stack.push(o$1[a2]) : e2.stack.push(C());
1426
+ return;
1427
+ } else
1428
+ return {
1429
+ type: "RuntimeError",
1430
+ message: "ARRAY_GET requires an array, object, or string",
1431
+ pc: e2.pc,
1432
+ opcode: t$1.op
1433
+ };
1434
+ let c2;
1435
+ if (i$1(r2))
1436
+ c2 = r2;
1437
+ else if (t(r2)) {
1438
+ const a2 = parseInt(r2, 10);
1439
+ if (!isNaN(a2) && a2.toString() === r2 && a2 >= 0)
1440
+ c2 = a2;
1441
+ else {
1442
+ const i2 = ((u2 = s2.properties) == null ? void 0 : u2[r2]) ?? C();
1443
+ e2.stack.push(i2);
1444
+ return;
1445
+ }
1446
+ } else
1447
+ return {
1448
+ type: "RuntimeError",
1449
+ message: "ARRAY_GET requires numeric or numeric string index",
1450
+ pc: e2.pc,
1451
+ opcode: t$1.op
1452
+ };
1453
+ const p3 = s2.elements[c2] ?? C();
1454
+ e2.stack.push(p3);
1215
1455
  }
1216
1456
  },
1217
- [n$1.ARRAY_SET]: {
1457
+ [n.ARRAY_SET]: {
1218
1458
  stackIn: 3,
1219
1459
  stackOut: 1,
1220
- execute: (e2, r) => {
1221
- const t$1 = e2.stack.pop(), s2 = e2.stack.pop(), o2 = e2.stack.pop();
1222
- if (f(o2)) {
1223
- if (!t(s2))
1460
+ execute: (e2, t$1) => {
1461
+ const r2 = e2.stack.pop(), o$1 = e2.stack.pop(), s2 = e2.stack.pop();
1462
+ let c2;
1463
+ if (u$1(s2)) {
1464
+ const a2 = e2.heap.get(s2.id);
1465
+ if (!a2 || a2.type !== "array")
1224
1466
  return {
1225
1467
  type: "RuntimeError",
1226
- message: "ARRAY_SET requires numeric index for arrays",
1468
+ message: "Invalid array reference",
1227
1469
  pc: e2.pc,
1228
- opcode: r.op
1470
+ opcode: t$1.op
1229
1471
  };
1230
- const n2 = Math.floor(s2);
1231
- if (n2 < 0)
1472
+ c2 = a2.data;
1473
+ } else if (b(s2)) {
1474
+ const a2 = e2.heap.get(s2.id);
1475
+ if (!a2 || a2.type !== "object")
1232
1476
  return {
1233
1477
  type: "RuntimeError",
1234
- message: "ARRAY_SET: Negative index not allowed",
1478
+ message: "Invalid object reference",
1235
1479
  pc: e2.pc,
1236
- opcode: r.op
1480
+ opcode: t$1.op
1237
1481
  };
1238
- o2.elements[n2] = t$1, e2.stack.push(o2);
1482
+ const i2 = a2.data, E2 = p$1(o$1);
1483
+ i2.properties[E2] = r2, e2.stack.push(s2);
1239
1484
  return;
1240
- }
1241
- if (c(o2)) {
1242
- if (!n(s2))
1485
+ } else if (o(s2))
1486
+ c2 = s2;
1487
+ else
1488
+ return {
1489
+ type: "RuntimeError",
1490
+ message: "ARRAY_SET requires an array",
1491
+ pc: e2.pc,
1492
+ opcode: t$1.op
1493
+ };
1494
+ let p3;
1495
+ if (i$1(o$1))
1496
+ p3 = o$1;
1497
+ else if (t(o$1)) {
1498
+ const a2 = parseInt(o$1, 10);
1499
+ if (!isNaN(a2) && a2.toString() === o$1 && a2 >= 0)
1500
+ p3 = a2;
1501
+ else {
1502
+ c2.properties || (c2.properties = {}), c2.properties[o$1] = r2, e2.stack.push(s2);
1503
+ return;
1504
+ }
1505
+ } else
1506
+ return {
1507
+ type: "RuntimeError",
1508
+ message: "ARRAY_SET requires numeric or numeric string index",
1509
+ pc: e2.pc,
1510
+ opcode: t$1.op
1511
+ };
1512
+ const u2 = Math.floor(p3);
1513
+ if (u2 < 0)
1514
+ return {
1515
+ type: "RuntimeError",
1516
+ message: "ARRAY_SET: Negative index not allowed",
1517
+ pc: e2.pc,
1518
+ opcode: t$1.op
1519
+ };
1520
+ c2.elements[u2] = r2, e2.stack.push(s2);
1521
+ }
1522
+ },
1523
+ [n.ARRAY_LEN]: {
1524
+ stackIn: 1,
1525
+ stackOut: 1,
1526
+ execute: (e2, t2) => {
1527
+ const r2 = e2.stack.pop();
1528
+ let o$1;
1529
+ if (u$1(r2)) {
1530
+ const s2 = e2.heap.get(r2.id);
1531
+ if (!s2 || s2.type !== "array")
1243
1532
  return {
1244
1533
  type: "RuntimeError",
1245
- message: "ARRAY_SET requires string key for objects",
1534
+ message: "Invalid array reference",
1246
1535
  pc: e2.pc,
1247
- opcode: r.op
1536
+ opcode: t2.op
1248
1537
  };
1249
- o2.properties[s2] = t$1, e2.stack.push(o2);
1250
- return;
1251
- }
1252
- return {
1253
- type: "RuntimeError",
1254
- message: "ARRAY_SET requires an array or object",
1255
- pc: e2.pc,
1256
- opcode: r.op
1257
- };
1538
+ o$1 = s2.data;
1539
+ } else if (o(r2))
1540
+ o$1 = r2;
1541
+ else
1542
+ return {
1543
+ type: "RuntimeError",
1544
+ message: "ARRAY_LEN requires an array",
1545
+ pc: e2.pc,
1546
+ opcode: t2.op
1547
+ };
1548
+ e2.stack.push(o$1.elements.length);
1258
1549
  }
1259
1550
  },
1260
- [n$1.ARRAY_LEN]: {
1261
- stackIn: 1,
1551
+ [n.ARRAY_MAP_PROP]: {
1552
+ stackIn: 2,
1262
1553
  stackOut: 1,
1263
- execute: (e2, r) => {
1264
- const t2 = e2.stack.pop();
1265
- if (!f(t2))
1554
+ execute: (e2, t2) => {
1555
+ const r2 = e2.stack.pop(), o2 = e2.stack.pop();
1556
+ if (!u$1(o2))
1266
1557
  return {
1267
1558
  type: "RuntimeError",
1268
- message: "ARRAY_LEN requires an array",
1559
+ message: "map() requires an array",
1560
+ pc: e2.pc,
1561
+ opcode: t2.op
1562
+ };
1563
+ const s$12 = e2.heap.get(o2.id);
1564
+ if (!s$12 || s$12.type !== "array")
1565
+ return {
1566
+ type: "RuntimeError",
1567
+ message: "Invalid array reference",
1269
1568
  pc: e2.pc,
1270
- opcode: r.op
1569
+ opcode: t2.op
1271
1570
  };
1272
- e2.stack.push(t2.elements.length);
1571
+ const c2 = s$12.data, p3 = s();
1572
+ for (const a2 of c2.elements)
1573
+ if (b(a2)) {
1574
+ const i2 = e2.heap.get(a2.id);
1575
+ if (i2 && i2.type === "object") {
1576
+ const E2 = i2.data, x2 = p$1(r2);
1577
+ p3.elements.push(E2.properties[x2] || null);
1578
+ } else
1579
+ p3.elements.push(null);
1580
+ } else
1581
+ p3.elements.push(null);
1582
+ const u2 = e2.heap.allocate("array", p3);
1583
+ e2.stack.push(u2);
1584
+ }
1585
+ },
1586
+ [n.ARRAY_FILTER_PROP]: {
1587
+ stackIn: 2,
1588
+ stackOut: 1,
1589
+ execute: (e2, t2) => {
1590
+ const r2 = e2.stack.pop(), o2 = e2.stack.pop();
1591
+ if (!u$1(o2))
1592
+ return {
1593
+ type: "RuntimeError",
1594
+ message: "filter() requires an array",
1595
+ pc: e2.pc,
1596
+ opcode: t2.op
1597
+ };
1598
+ const s$12 = e2.heap.get(o2.id);
1599
+ if (!s$12 || s$12.type !== "array")
1600
+ return {
1601
+ type: "RuntimeError",
1602
+ message: "Invalid array reference",
1603
+ pc: e2.pc,
1604
+ opcode: t2.op
1605
+ };
1606
+ const c2 = s$12.data, p3 = s();
1607
+ for (const a2 of c2.elements)
1608
+ if (b(a2)) {
1609
+ const i2 = e2.heap.get(a2.id);
1610
+ if (i2 && i2.type === "object") {
1611
+ const E2 = i2.data, x2 = p$1(r2);
1612
+ x2 in E2.properties && E2.properties[x2] && p3.elements.push(a2);
1613
+ }
1614
+ }
1615
+ const u2 = e2.heap.allocate("array", p3);
1616
+ e2.stack.push(u2);
1273
1617
  }
1274
1618
  }
1275
1619
  };
1276
- function T(e2) {
1620
+ function M(e2, t2) {
1277
1621
  if (e2 === null) return null;
1278
1622
  if (e2 !== void 0) {
1279
1623
  if (typeof e2 == "string" || typeof e2 == "number" || typeof e2 == "boolean" || e2 && typeof e2 == "object" && e2.type === "array" && Array.isArray(e2.elements))
1280
1624
  return e2;
1281
1625
  if (Array.isArray(e2)) {
1282
- const r = d();
1283
- return r.elements = e2.map(T), r;
1626
+ const r2 = s();
1627
+ return r2.elements = e2.map((o2) => M(o2, t2)), t2.allocate("array", r2);
1284
1628
  }
1285
1629
  if (e2 && typeof e2 == "object" && e2.type === "object" && e2.properties)
1286
1630
  return e2;
1287
1631
  if (typeof e2 == "object") {
1288
- const r = j$1();
1289
- for (const [t2, s2] of Object.entries(e2))
1290
- r.properties[t2] = T(s2);
1291
- return r;
1632
+ const r2 = M$1();
1633
+ for (const [o2, s2] of Object.entries(e2))
1634
+ r2.properties[o2] = M(s2, t2);
1635
+ return t2.allocate("object", r2);
1292
1636
  }
1293
1637
  return null;
1294
1638
  }
1295
1639
  }
1296
- const J = {
1297
- [n$1.CONCAT]: {
1640
+ const oe = {
1641
+ [n.CONCAT]: {
1298
1642
  stackIn: 2,
1299
1643
  stackOut: 1,
1300
- execute: (e2, r) => {
1301
- const t2 = e2.stack.pop(), s2 = e2.stack.pop();
1302
- e2.stack.push(y(s2) + y(t2));
1644
+ execute: (e2, t2) => {
1645
+ const r2 = l(e2, t2.op);
1646
+ if (d(r2)) return r2;
1647
+ const o2 = l(e2, t2.op);
1648
+ if (d(o2)) return o2;
1649
+ e2.stack.push(p$1(o2) + p$1(r2));
1303
1650
  }
1304
1651
  },
1305
- [n$1.STRING_LEN]: {
1652
+ [n.STRING_LEN]: {
1306
1653
  stackIn: 1,
1307
1654
  stackOut: 1,
1308
- execute: (e2, r) => {
1309
- const t2 = e2.stack.pop();
1310
- if (!n(t2))
1655
+ execute: (e2, t$1) => {
1656
+ const r2 = l(e2, t$1.op);
1657
+ if (d(r2)) return r2;
1658
+ if (!t(r2))
1311
1659
  return {
1312
1660
  type: "RuntimeError",
1313
1661
  message: "STRING_LEN requires a string",
1314
1662
  pc: e2.pc,
1315
- opcode: r.op
1663
+ opcode: t$1.op
1316
1664
  };
1317
- e2.stack.push(t2.length);
1665
+ e2.stack.push(r2.length);
1318
1666
  }
1319
1667
  },
1320
- [n$1.LENGTH]: {
1668
+ [n.LENGTH]: {
1321
1669
  stackIn: 1,
1322
1670
  stackOut: 1,
1323
- execute: (e2, r) => {
1324
- const t2 = e2.stack.pop();
1325
- if (n(t2))
1326
- e2.stack.push(t2.length);
1327
- else if (f(t2))
1328
- e2.stack.push(t2.elements.length);
1329
- else
1671
+ execute: (e2, t$1) => {
1672
+ const r2 = l(e2, t$1.op);
1673
+ if (d(r2)) return r2;
1674
+ if (t(r2))
1675
+ e2.stack.push(r2.length);
1676
+ else if (o(r2))
1677
+ e2.stack.push(r2.elements.length);
1678
+ else if (u$1(r2)) {
1679
+ const o2 = e2.heap.get(r2.id);
1680
+ if (!o2 || o2.type !== "array")
1681
+ return {
1682
+ type: "RuntimeError",
1683
+ message: "Invalid array reference",
1684
+ pc: e2.pc,
1685
+ opcode: t$1.op
1686
+ };
1687
+ const s2 = o2.data;
1688
+ e2.stack.push(s2.elements.length);
1689
+ } else
1330
1690
  return {
1331
1691
  type: "RuntimeError",
1332
1692
  message: "LENGTH requires a string or array",
1333
1693
  pc: e2.pc,
1334
- opcode: r.op
1694
+ opcode: t$1.op
1335
1695
  };
1336
1696
  }
1337
1697
  },
1338
- [n$1.JSON_PARSE]: {
1698
+ [n.JSON_PARSE]: {
1339
1699
  stackIn: 1,
1340
1700
  stackOut: 1,
1341
- execute: (e2, r) => {
1342
- const t2 = e2.stack.pop();
1343
- if (!n(t2))
1701
+ execute: (e2, t$1) => {
1702
+ const r2 = l(e2, t$1.op);
1703
+ if (d(r2)) return r2;
1704
+ if (!t(r2))
1344
1705
  return {
1345
1706
  type: "RuntimeError",
1346
1707
  message: "JSON_PARSE requires a string",
1347
1708
  pc: e2.pc,
1348
- opcode: r.op
1709
+ opcode: t$1.op
1349
1710
  };
1350
1711
  try {
1351
- const s2 = JSON.parse(t2);
1352
- e2.stack.push(T(s2));
1712
+ const o2 = JSON.parse(r2);
1713
+ e2.stack.push(M(o2, e2.heap));
1353
1714
  } catch {
1354
1715
  e2.stack.push(null);
1355
1716
  }
1356
1717
  }
1357
1718
  },
1358
- [n$1.TYPEOF]: {
1719
+ [n.TYPEOF]: {
1359
1720
  stackIn: 1,
1360
1721
  stackOut: 1,
1361
- execute: (e2, r) => {
1362
- const t2 = e2.stack.pop();
1363
- e2.stack.push(p(t2));
1722
+ execute: (e2, t2) => {
1723
+ const r2 = l(e2, t2.op);
1724
+ if (d(r2)) return r2;
1725
+ e2.stack.push(d$2(r2));
1364
1726
  }
1365
1727
  }
1366
- }, Y = {
1367
- [n$1.INC]: {
1728
+ }, se = {
1729
+ [n.INC]: {
1368
1730
  stackIn: 1,
1369
1731
  stackOut: 1,
1370
- execute: (e2, r) => {
1371
- const t2 = e2.stack.pop();
1372
- if (typeof t2 != "string")
1732
+ execute: (e2, t2) => {
1733
+ const r2 = e2.stack.pop();
1734
+ if (typeof r2 != "string")
1373
1735
  return {
1374
1736
  type: "RuntimeError",
1375
1737
  message: "INC: Invalid variable name",
1376
1738
  pc: e2.pc,
1377
- opcode: r.op
1739
+ opcode: t2.op
1378
1740
  };
1379
- const s2 = e2.variables.get(t2) ?? 0, o2 = m(s2) + 1;
1380
- e2.variables.set(t2, o2);
1381
- const n2 = r.arg === true;
1382
- e2.stack.push(n2 ? m(s2) : o2);
1741
+ const o2 = e2.variables.get(r2) ?? 0, s2 = m(o2) + 1;
1742
+ e2.variables.set(r2, s2);
1743
+ const c2 = t2.arg === true;
1744
+ e2.stack.push(c2 ? m(o2) : s2);
1383
1745
  }
1384
1746
  },
1385
- [n$1.DEC]: {
1747
+ [n.DEC]: {
1386
1748
  stackIn: 1,
1387
1749
  stackOut: 1,
1388
- execute: (e2, r) => {
1389
- const t2 = e2.stack.pop();
1390
- if (typeof t2 != "string")
1750
+ execute: (e2, t2) => {
1751
+ const r2 = e2.stack.pop();
1752
+ if (typeof r2 != "string")
1391
1753
  return {
1392
1754
  type: "RuntimeError",
1393
1755
  message: "DEC: Invalid variable name",
1394
1756
  pc: e2.pc,
1395
- opcode: r.op
1757
+ opcode: t2.op
1396
1758
  };
1397
- const s2 = e2.variables.get(t2) ?? 0, o2 = m(s2) - 1;
1398
- e2.variables.set(t2, o2);
1399
- const n2 = r.arg === true;
1400
- e2.stack.push(n2 ? m(s2) : o2);
1759
+ const o2 = e2.variables.get(r2) ?? 0, s2 = m(o2) - 1;
1760
+ e2.variables.set(r2, s2);
1761
+ const c2 = t2.arg === true;
1762
+ e2.stack.push(c2 ? m(o2) : s2);
1401
1763
  }
1402
1764
  }
1403
- }, B = {
1404
- [n$1.RETURN]: {
1765
+ }, ce = {
1766
+ [n.RETURN]: {
1405
1767
  stackIn: 0,
1406
1768
  stackOut: 0,
1407
1769
  controlsPC: true,
1408
- execute: (e2, r) => {
1409
- const t2 = e2.stack.pop() ?? null;
1410
- e2.returnValue = t2, e2.status = "complete";
1770
+ execute: (e2, t2) => {
1771
+ const r2 = e2.stack.pop() ?? null;
1772
+ e2.returnValue = r2, e2.status = "complete";
1411
1773
  }
1412
1774
  },
1413
- [n$1.BREAK]: {
1775
+ [n.BREAK]: {
1414
1776
  stackIn: 0,
1415
1777
  stackOut: 0,
1416
1778
  controlsPC: true,
1417
- execute: (e2, r) => {
1418
- if (r.arg === void 0)
1779
+ execute: (e2, t2) => {
1780
+ if (t2.arg === void 0)
1419
1781
  return {
1420
1782
  type: "RuntimeError",
1421
1783
  message: "BREAK requires a target address",
1422
1784
  pc: e2.pc,
1423
- opcode: r.op
1785
+ opcode: t2.op
1424
1786
  };
1425
- const t2 = r.arg;
1426
- if (t2 < 0)
1787
+ const r2 = t2.arg;
1788
+ if (r2 < 0)
1427
1789
  return {
1428
1790
  type: "RuntimeError",
1429
- message: `Invalid break target: ${t2}`,
1791
+ message: `Invalid break target: ${r2}`,
1430
1792
  pc: e2.pc,
1431
- opcode: r.op
1793
+ opcode: t2.op
1432
1794
  };
1433
- e2.pc = t2;
1795
+ e2.pc = r2;
1434
1796
  }
1435
1797
  },
1436
- [n$1.CONTINUE]: {
1798
+ [n.CONTINUE]: {
1437
1799
  stackIn: 0,
1438
1800
  stackOut: 0,
1439
1801
  controlsPC: true,
1440
- execute: (e2, r) => {
1441
- if (r.arg === void 0)
1802
+ execute: (e2, t2) => {
1803
+ if (t2.arg === void 0)
1442
1804
  return {
1443
1805
  type: "RuntimeError",
1444
1806
  message: "CONTINUE requires a target address",
1445
1807
  pc: e2.pc,
1446
- opcode: r.op
1808
+ opcode: t2.op
1447
1809
  };
1448
- const t2 = r.arg;
1449
- if (t2 < 0)
1810
+ const r2 = t2.arg;
1811
+ if (r2 < 0)
1450
1812
  return {
1451
1813
  type: "RuntimeError",
1452
- message: `Invalid continue target: ${t2}`,
1814
+ message: `Invalid continue target: ${r2}`,
1453
1815
  pc: e2.pc,
1454
- opcode: r.op
1816
+ opcode: t2.op
1455
1817
  };
1456
- e2.pc = t2;
1818
+ e2.pc = r2;
1457
1819
  }
1458
1820
  },
1459
- [n$1.FS_LIST_FILES]: {
1821
+ [n.FS_LIST_FILES]: {
1460
1822
  stackIn: 1,
1461
1823
  // Minimum 1 argument (path), optionally 2 (path + options)
1462
- stackOut: 0,
1463
- controlsPC: true,
1464
- execute: (e2, r) => {
1824
+ stackOut: 1,
1825
+ execute: (e2, t$1) => {
1465
1826
  if (e2.stack.length < 1)
1466
1827
  return {
1467
1828
  type: "StackUnderflow",
1468
1829
  message: "FS_LIST_FILES: Stack underflow",
1469
1830
  pc: e2.pc,
1470
- opcode: r.op
1831
+ opcode: t$1.op
1471
1832
  };
1472
- let t2, s2 = {};
1473
- const o2 = e2.stack[e2.stack.length - 1];
1474
- if (e2.stack.length >= 2 && typeof o2 == "object" && o2 !== null && !f(o2) && (s2 = e2.stack.pop()), t2 = e2.stack.pop(), !n(t2))
1833
+ let r2, o$1 = {};
1834
+ const s2 = e2.stack[e2.stack.length - 1];
1835
+ if (e2.stack.length >= 2 && typeof s2 == "object" && s2 !== null && !o(s2) && (o$1 = e2.stack.pop()), r2 = e2.stack.pop(), !t(r2))
1475
1836
  return {
1476
1837
  type: "RuntimeError",
1477
1838
  message: "FS_LIST_FILES requires a string path",
1478
1839
  pc: e2.pc,
1479
- opcode: r.op
1840
+ opcode: t$1.op
1841
+ };
1842
+ if (!e2.fileSystem)
1843
+ return {
1844
+ type: "RuntimeError",
1845
+ message: "FileSystem not available",
1846
+ pc: e2.pc,
1847
+ opcode: t$1.op
1848
+ };
1849
+ const c2 = e2.fileSystem.listFiles(r2, o$1);
1850
+ if (o(c2)) {
1851
+ const p3 = e2.heap.allocate("array", c2);
1852
+ e2.stack.push(p3);
1853
+ } else
1854
+ e2.stack.push(c2);
1855
+ }
1856
+ },
1857
+ [n.FS_READ_FILE]: {
1858
+ stackIn: 1,
1859
+ // path
1860
+ stackOut: 1,
1861
+ // Returns string or null
1862
+ execute: (e2, t$1) => {
1863
+ const r2 = e2.stack.pop();
1864
+ if (!r2 || !t(r2))
1865
+ return {
1866
+ type: "RuntimeError",
1867
+ message: "FS_READ_FILE requires a string path",
1868
+ pc: e2.pc,
1869
+ opcode: t$1.op
1870
+ };
1871
+ if (!e2.fileSystem)
1872
+ return {
1873
+ type: "RuntimeError",
1874
+ message: "FileSystem not available",
1875
+ pc: e2.pc,
1876
+ opcode: t$1.op
1480
1877
  };
1481
- e2.fsOperation = {
1482
- type: "listFiles",
1483
- path: t2,
1484
- options: s2
1485
- }, e2.status = "waiting_fs";
1878
+ const o2 = e2.fileSystem.readFile(r2);
1879
+ e2.stack.push(o2);
1880
+ }
1881
+ },
1882
+ [n.FS_WRITE_FILE]: {
1883
+ stackIn: 2,
1884
+ // path, content
1885
+ stackOut: 1,
1886
+ // Returns boolean
1887
+ execute: (e2, t$1) => {
1888
+ const r2 = e2.stack.pop(), o2 = e2.stack.pop();
1889
+ if (!o2 || !t(o2))
1890
+ return {
1891
+ type: "RuntimeError",
1892
+ message: "FS_WRITE_FILE requires a string path",
1893
+ pc: e2.pc,
1894
+ opcode: t$1.op
1895
+ };
1896
+ if (r2 === void 0)
1897
+ return {
1898
+ type: "RuntimeError",
1899
+ message: "FS_WRITE_FILE requires content argument",
1900
+ pc: e2.pc,
1901
+ opcode: t$1.op
1902
+ };
1903
+ if (!e2.fileSystem)
1904
+ return {
1905
+ type: "RuntimeError",
1906
+ message: "FileSystem not available",
1907
+ pc: e2.pc,
1908
+ opcode: t$1.op
1909
+ };
1910
+ const s2 = p$1(r2), c2 = e2.fileSystem.writeFile(o2, s2);
1911
+ e2.stack.push(c2);
1486
1912
  }
1487
1913
  },
1488
1914
  // String method handlers
1489
- [n$1.STRING_SUBSTRING]: {
1915
+ [n.STRING_SUBSTRING]: {
1490
1916
  stackIn: 2,
1491
1917
  // Minimum 2 (string, start), optionally 3 (string, start, end)
1492
1918
  stackOut: 1,
1493
- execute: (e2, r) => {
1919
+ execute: (e2, t$1) => {
1494
1920
  if (e2.stack.length < 2)
1495
1921
  return {
1496
1922
  type: "StackUnderflow",
1497
1923
  message: "STRING_SUBSTRING: Stack underflow",
1498
1924
  pc: e2.pc,
1499
- opcode: r.op
1925
+ opcode: t$1.op
1500
1926
  };
1501
- const t2 = e2.stack.length;
1502
- let s2, o2, n$12;
1503
- const a2 = e2.stack[t2 - 1], i2 = e2.stack[t2 - 2];
1504
- if (t2 >= 3 && typeof a2 == "number" && typeof i2 == "number" ? (n$12 = e2.stack.pop(), o2 = e2.stack.pop(), s2 = e2.stack.pop()) : (o2 = e2.stack.pop(), s2 = e2.stack.pop(), n$12 = void 0), !n(s2))
1927
+ const r2 = e2.stack.length;
1928
+ let o2, s2, c2;
1929
+ const p3 = e2.stack[r2 - 1], u2 = e2.stack[r2 - 2];
1930
+ if (r2 >= 3 && typeof p3 == "number" && typeof u2 == "number" ? (c2 = e2.stack.pop(), s2 = e2.stack.pop(), o2 = e2.stack.pop()) : (s2 = e2.stack.pop(), o2 = e2.stack.pop(), c2 = void 0), !t(o2))
1505
1931
  return {
1506
1932
  type: "RuntimeError",
1507
1933
  message: "STRING_SUBSTRING requires a string",
1508
1934
  pc: e2.pc,
1509
- opcode: r.op
1935
+ opcode: t$1.op
1510
1936
  };
1511
- if (typeof o2 != "number")
1937
+ if (typeof s2 != "number")
1512
1938
  return {
1513
1939
  type: "RuntimeError",
1514
1940
  message: "STRING_SUBSTRING requires numeric start index",
1515
1941
  pc: e2.pc,
1516
- opcode: r.op
1942
+ opcode: t$1.op
1517
1943
  };
1518
- const u2 = s2.length;
1519
- o2 < 0 && (o2 = Math.max(0, u2 + o2)), n$12 !== void 0 && n$12 < 0 && (n$12 = Math.max(0, u2 + n$12));
1520
- const O2 = n$12 !== void 0 ? s2.substring(o2, n$12) : s2.substring(o2);
1521
- e2.stack.push(O2);
1944
+ const a2 = o2.length;
1945
+ s2 < 0 && (s2 = Math.max(0, a2 + s2)), c2 !== void 0 && c2 < 0 && (c2 = Math.max(0, a2 + c2));
1946
+ const i2 = c2 !== void 0 ? o2.substring(s2, c2) : o2.substring(s2);
1947
+ e2.stack.push(i2);
1522
1948
  }
1523
1949
  },
1524
- [n$1.STRING_INDEXOF]: {
1950
+ [n.STRING_INDEXOF]: {
1525
1951
  stackIn: 2,
1526
1952
  stackOut: 1,
1527
- execute: (e2, r) => {
1528
- const t2 = e2.stack.pop(), s2 = e2.stack.pop();
1529
- if (!n(s2) || !n(t2))
1953
+ execute: (e2, t$1) => {
1954
+ const r2 = e2.stack.pop(), o2 = e2.stack.pop();
1955
+ if (!t(o2) || !t(r2))
1530
1956
  return {
1531
1957
  type: "RuntimeError",
1532
1958
  message: "STRING_INDEXOF requires string arguments",
1533
1959
  pc: e2.pc,
1534
- opcode: r.op
1960
+ opcode: t$1.op
1535
1961
  };
1536
- e2.stack.push(s2.indexOf(t2));
1962
+ e2.stack.push(o2.indexOf(r2));
1537
1963
  }
1538
1964
  },
1539
- [n$1.STRING_SPLIT]: {
1965
+ [n.STRING_SPLIT]: {
1540
1966
  stackIn: 2,
1541
1967
  stackOut: 1,
1542
- execute: (e2, r) => {
1543
- const t2 = e2.stack.pop(), s2 = e2.stack.pop();
1544
- if (!n(s2) || !n(t2))
1968
+ execute: (e2, t$1) => {
1969
+ const r2 = e2.stack.pop(), o2 = e2.stack.pop();
1970
+ if (!t(o2) || !t(r2))
1545
1971
  return {
1546
1972
  type: "RuntimeError",
1547
1973
  message: "STRING_SPLIT requires string arguments",
1548
1974
  pc: e2.pc,
1549
- opcode: r.op
1975
+ opcode: t$1.op
1550
1976
  };
1551
- let o2;
1552
- t2 === "" ? o2 = s2.split("") : o2 = s2.split(t2), e2.stack.push(d(o2));
1977
+ let s$12;
1978
+ r2 === "" ? s$12 = o2.split("") : s$12 = o2.split(r2);
1979
+ const c2 = s(s$12), p3 = e2.heap.allocate("array", c2);
1980
+ e2.stack.push(p3);
1553
1981
  }
1554
1982
  },
1555
- [n$1.STRING_SLICE]: {
1983
+ [n.STRING_SLICE]: {
1556
1984
  stackIn: 2,
1557
1985
  // Minimum 2 (string, start), optionally 3 (string, start, end)
1558
1986
  stackOut: 1,
1559
- execute: (e2, r) => {
1987
+ execute: (e2, t$1) => {
1560
1988
  if (e2.stack.length < 2)
1561
1989
  return {
1562
1990
  type: "StackUnderflow",
1563
1991
  message: "STRING_SLICE: Stack underflow",
1564
1992
  pc: e2.pc,
1565
- opcode: r.op
1993
+ opcode: t$1.op
1566
1994
  };
1567
- const t2 = e2.stack.length;
1568
- let s2, o2, n$12;
1569
- const a2 = e2.stack.length >= 1 ? e2.stack[t2 - 1] : void 0, i2 = e2.stack.length >= 2 ? e2.stack[t2 - 2] : void 0;
1570
- if (t2 >= 3 && typeof a2 == "number" && typeof i2 == "number" ? (n$12 = e2.stack.pop(), o2 = e2.stack.pop(), s2 = e2.stack.pop()) : (o2 = e2.stack.pop(), s2 = e2.stack.pop(), n$12 = void 0), !n(s2))
1995
+ const r2 = e2.stack.length;
1996
+ let o2, s2, c2;
1997
+ const p3 = e2.stack.length >= 1 ? e2.stack[r2 - 1] : void 0, u2 = e2.stack.length >= 2 ? e2.stack[r2 - 2] : void 0;
1998
+ if (r2 >= 3 && typeof p3 == "number" && typeof u2 == "number" ? (c2 = e2.stack.pop(), s2 = e2.stack.pop(), o2 = e2.stack.pop()) : (s2 = e2.stack.pop(), o2 = e2.stack.pop(), c2 = void 0), !t(o2))
1571
1999
  return {
1572
2000
  type: "RuntimeError",
1573
2001
  message: "STRING_SLICE requires a string",
1574
2002
  pc: e2.pc,
1575
- opcode: r.op
2003
+ opcode: t$1.op
1576
2004
  };
1577
- if (typeof o2 != "number")
2005
+ if (typeof s2 != "number")
1578
2006
  return {
1579
2007
  type: "RuntimeError",
1580
2008
  message: "STRING_SLICE requires numeric start index",
1581
2009
  pc: e2.pc,
1582
- opcode: r.op
2010
+ opcode: t$1.op
1583
2011
  };
1584
- const u2 = n$12 !== void 0 ? s2.slice(o2, n$12) : s2.slice(o2);
1585
- e2.stack.push(u2);
2012
+ const a2 = c2 !== void 0 ? o2.slice(s2, c2) : o2.slice(s2);
2013
+ e2.stack.push(a2);
1586
2014
  }
1587
2015
  },
1588
- [n$1.STRING_CHARAT]: {
2016
+ [n.STRING_CHARAT]: {
1589
2017
  stackIn: 2,
1590
2018
  stackOut: 1,
1591
- execute: (e2, r) => {
1592
- const t2 = e2.stack.pop(), s2 = e2.stack.pop();
1593
- if (!n(s2))
2019
+ execute: (e2, t$1) => {
2020
+ const r2 = e2.stack.pop(), o2 = e2.stack.pop();
2021
+ if (!t(o2))
1594
2022
  return {
1595
2023
  type: "RuntimeError",
1596
2024
  message: "STRING_CHARAT requires a string",
1597
2025
  pc: e2.pc,
1598
- opcode: r.op
2026
+ opcode: t$1.op
1599
2027
  };
1600
- if (typeof t2 != "number")
2028
+ if (typeof r2 != "number")
1601
2029
  return {
1602
2030
  type: "RuntimeError",
1603
2031
  message: "STRING_CHARAT requires numeric index",
1604
2032
  pc: e2.pc,
1605
- opcode: r.op
2033
+ opcode: t$1.op
1606
2034
  };
1607
- const o2 = s2.charAt(t2);
1608
- e2.stack.push(o2);
2035
+ const s2 = o2.charAt(r2);
2036
+ e2.stack.push(s2);
1609
2037
  }
1610
2038
  },
1611
- [n$1.STRING_TOUPPERCASE]: {
2039
+ [n.STRING_TOUPPERCASE]: {
1612
2040
  stackIn: 1,
1613
2041
  stackOut: 1,
1614
- execute: (e2, r) => {
1615
- const t2 = e2.stack.pop();
1616
- if (!n(t2))
2042
+ execute: (e2, t$1) => {
2043
+ const r2 = e2.stack.pop();
2044
+ if (!t(r2))
1617
2045
  return {
1618
2046
  type: "RuntimeError",
1619
2047
  message: "STRING_TOUPPERCASE requires a string",
1620
2048
  pc: e2.pc,
1621
- opcode: r.op
2049
+ opcode: t$1.op
1622
2050
  };
1623
- e2.stack.push(t2.toUpperCase());
2051
+ e2.stack.push(r2.toUpperCase());
1624
2052
  }
1625
2053
  },
1626
- [n$1.STRING_TOLOWERCASE]: {
2054
+ [n.STRING_TOLOWERCASE]: {
1627
2055
  stackIn: 1,
1628
2056
  stackOut: 1,
1629
- execute: (e2, r) => {
1630
- const t2 = e2.stack.pop();
1631
- if (!n(t2))
2057
+ execute: (e2, t$1) => {
2058
+ const r2 = e2.stack.pop();
2059
+ if (!t(r2))
1632
2060
  return {
1633
2061
  type: "RuntimeError",
1634
2062
  message: "STRING_TOLOWERCASE requires a string",
1635
2063
  pc: e2.pc,
1636
- opcode: r.op
2064
+ opcode: t$1.op
1637
2065
  };
1638
- e2.stack.push(t2.toLowerCase());
2066
+ e2.stack.push(r2.toLowerCase());
1639
2067
  }
1640
2068
  },
1641
- [n$1.TO_STRING]: {
2069
+ [n.TO_STRING]: {
1642
2070
  stackIn: 1,
1643
2071
  stackOut: 1,
1644
- execute: (e2, r) => {
1645
- const t2 = e2.stack.pop();
1646
- if (t2 === void 0)
2072
+ execute: (e2, t2) => {
2073
+ const r2 = e2.stack.pop();
2074
+ if (r2 === void 0)
1647
2075
  return {
1648
2076
  type: "StackUnderflow",
1649
2077
  message: "TO_STRING: Stack underflow",
1650
2078
  pc: e2.pc,
1651
- opcode: r.op
2079
+ opcode: t2.op
1652
2080
  };
1653
- const s2 = y(t2);
1654
- e2.stack.push(s2);
2081
+ const o2 = p$1(r2);
2082
+ e2.stack.push(o2);
1655
2083
  }
1656
2084
  }
1657
2085
  };
1658
- function _(e2) {
1659
- if (f(e2))
1660
- return e2.elements.map(_);
1661
- if (c(e2)) {
1662
- const r = {};
1663
- for (const [t2, s2] of Object.entries(e2.properties))
1664
- r[t2] = _(s2);
1665
- return r;
1666
- }
1667
- if (!o(e2))
2086
+ function j(e2, t2) {
2087
+ if (u$1(e2)) {
2088
+ const r2 = t2.get(e2.id);
2089
+ return r2 && r2.type === "array" ? r2.data.elements.map((s2) => j(s2, t2)) : null;
2090
+ }
2091
+ if (b(e2)) {
2092
+ const r2 = t2.get(e2.id);
2093
+ if (r2 && r2.type === "object") {
2094
+ const o2 = r2.data, s2 = {};
2095
+ for (const [c2, p3] of Object.entries(o2.properties))
2096
+ s2[c2] = j(p3, t2);
2097
+ return s2;
2098
+ }
2099
+ return null;
2100
+ }
2101
+ if (o(e2))
2102
+ return e2.elements.map((r2) => j(r2, t2));
2103
+ if (y(e2)) {
2104
+ const r2 = {};
2105
+ for (const [o2, s2] of Object.entries(e2.properties))
2106
+ r2[o2] = j(s2, t2);
2107
+ return r2;
2108
+ }
2109
+ if (!c(e2))
1668
2110
  return e2;
1669
2111
  }
1670
- const W = {
1671
- [n$1.OBJECT_CREATE]: {
2112
+ const ne = {
2113
+ [n.OBJECT_CREATE]: {
1672
2114
  stackIn: 0,
1673
2115
  stackOut: 1,
1674
2116
  execute: (e2) => {
1675
- e2.stack.push(j$1());
2117
+ const t2 = M$1(), r2 = e2.heap.allocate("object", t2);
2118
+ e2.stack.push(r2);
1676
2119
  }
1677
2120
  },
1678
- [n$1.PROPERTY_SET]: {
2121
+ [n.PROPERTY_SET]: {
1679
2122
  stackIn: 3,
1680
2123
  stackOut: 1,
1681
- execute: (e2, r) => {
1682
- const t2 = e2.stack.pop(), s2 = e2.stack.pop(), o2 = e2.stack.pop();
1683
- if (!c(o2))
2124
+ execute: (e2, t2) => {
2125
+ const r2 = l(e2, t2.op);
2126
+ if (d(r2)) return r2;
2127
+ const o2 = l(e2, t2.op);
2128
+ if (d(o2)) return o2;
2129
+ const s2 = l(e2, t2.op);
2130
+ if (d(s2)) return s2;
2131
+ let c2;
2132
+ if (b(s2)) {
2133
+ const u2 = e2.heap.get(s2.id);
2134
+ if (!u2 || u2.type !== "object")
2135
+ return {
2136
+ type: "RuntimeError",
2137
+ message: "Invalid object reference",
2138
+ pc: e2.pc,
2139
+ opcode: t2.op
2140
+ };
2141
+ c2 = u2.data;
2142
+ } else if (y(s2))
2143
+ c2 = s2;
2144
+ else
1684
2145
  return {
1685
2146
  type: "RuntimeError",
1686
- message: `Cannot set property '${s2}' on ${p(o2)}`,
2147
+ message: `Cannot set property '${o2}' on ${d$2(s2)}`,
1687
2148
  pc: e2.pc,
1688
- opcode: r.op
2149
+ opcode: t2.op
1689
2150
  };
1690
- if (!n(s2))
2151
+ const p3 = p$1(o2);
2152
+ c2.properties[p3] = r2, e2.stack.push(s2);
2153
+ }
2154
+ },
2155
+ [n.PROPERTY_GET]: {
2156
+ stackIn: 2,
2157
+ stackOut: 1,
2158
+ execute: (e2, t2) => {
2159
+ const r2 = l(e2, t2.op);
2160
+ if (d(r2)) return r2;
2161
+ const o2 = l(e2, t2.op);
2162
+ if (d(o2)) return o2;
2163
+ if (f$1(o2) || c(o2))
1691
2164
  return {
1692
2165
  type: "RuntimeError",
1693
- message: "Property key must be a string",
2166
+ message: `Cannot read property '${r2}' of ${d$2(o2)}`,
1694
2167
  pc: e2.pc,
1695
- opcode: r.op
2168
+ opcode: t2.op
1696
2169
  };
1697
- o2.properties[s2] = t2, e2.stack.push(o2);
2170
+ let s2 = null;
2171
+ if (b(o2)) {
2172
+ const p3 = e2.heap.get(o2.id);
2173
+ if (!p3 || p3.type !== "object")
2174
+ return {
2175
+ type: "RuntimeError",
2176
+ message: "Invalid object reference",
2177
+ pc: e2.pc,
2178
+ opcode: t2.op
2179
+ };
2180
+ s2 = p3.data;
2181
+ } else if (y(o2))
2182
+ s2 = o2;
2183
+ else {
2184
+ e2.stack.push(C());
2185
+ return;
2186
+ }
2187
+ const c$1 = s2.properties[p$1(r2)];
2188
+ e2.stack.push(c$1 ?? C());
1698
2189
  }
1699
2190
  },
1700
- [n$1.PROPERTY_GET]: {
2191
+ [n.JSON_STRINGIFY]: {
2192
+ stackIn: 1,
2193
+ stackOut: 1,
2194
+ execute: (e2, t2) => {
2195
+ const r2 = l(e2, t2.op);
2196
+ if (d(r2)) return r2;
2197
+ const o2 = j(r2, e2.heap);
2198
+ e2.stack.push(JSON.stringify(o2));
2199
+ }
2200
+ }
2201
+ }, pe = {
2202
+ [n.GET]: {
1701
2203
  stackIn: 2,
1702
2204
  stackOut: 1,
1703
- execute: (e$1, r) => {
1704
- const t2 = e$1.stack.pop(), s$1 = e$1.stack.pop();
1705
- if (e(s$1) || o(s$1))
2205
+ execute: (e2, t$1) => {
2206
+ var s2;
2207
+ const r2 = e2.stack.pop(), o2 = e2.stack.pop();
2208
+ if (u$1(o2)) {
2209
+ const c2 = e2.heap.get(o2.id);
2210
+ if (!c2 || c2.type !== "array")
2211
+ return {
2212
+ type: "RuntimeError",
2213
+ message: "Invalid array reference",
2214
+ pc: e2.pc,
2215
+ opcode: t$1.op
2216
+ };
2217
+ const p3 = c2.data;
2218
+ let u2;
2219
+ if (i$1(r2))
2220
+ u2 = r2;
2221
+ else if (t(r2)) {
2222
+ const i2 = parseInt(r2, 10);
2223
+ if (!isNaN(i2) && i2.toString() === r2 && i2 >= 0)
2224
+ u2 = i2;
2225
+ else {
2226
+ const E2 = ((s2 = p3.properties) == null ? void 0 : s2[r2]) ?? C();
2227
+ e2.stack.push(E2);
2228
+ return;
2229
+ }
2230
+ } else
2231
+ return {
2232
+ type: "RuntimeError",
2233
+ message: "GET requires numeric or numeric string index for arrays",
2234
+ pc: e2.pc,
2235
+ opcode: t$1.op
2236
+ };
2237
+ const a2 = p3.elements[u2] ?? C();
2238
+ e2.stack.push(a2);
2239
+ return;
2240
+ } else if (b(o2)) {
2241
+ const c2 = e2.heap.get(o2.id);
2242
+ if (!c2 || c2.type !== "object")
2243
+ return {
2244
+ type: "RuntimeError",
2245
+ message: "Invalid object reference",
2246
+ pc: e2.pc,
2247
+ opcode: t$1.op
2248
+ };
2249
+ const p3 = c2.data, u2 = p$1(r2), a2 = p3.properties[u2] ?? C();
2250
+ e2.stack.push(a2);
2251
+ return;
2252
+ } else if (t(o2)) {
2253
+ let c2;
2254
+ if (i$1(r2))
2255
+ c2 = r2;
2256
+ else if (t(r2)) {
2257
+ const p3 = parseInt(r2, 10);
2258
+ !isNaN(p3) && p3.toString() === r2 && (c2 = p3);
2259
+ }
2260
+ c2 !== void 0 && c2 >= 0 && c2 < o2.length ? e2.stack.push(o2[c2]) : e2.stack.push(C());
2261
+ return;
2262
+ } else
1706
2263
  return {
1707
2264
  type: "RuntimeError",
1708
- message: `Cannot read property '${t2}' of ${p(s$1)}`,
1709
- pc: e$1.pc,
1710
- opcode: r.op
2265
+ message: "GET requires array, object, or string",
2266
+ pc: e2.pc,
2267
+ opcode: t$1.op
1711
2268
  };
1712
- if (!c(s$1)) {
1713
- e$1.stack.push(s());
1714
- return;
1715
- }
1716
- const o$1 = s$1.properties[y(t2)];
1717
- e$1.stack.push(o$1 ?? s());
1718
2269
  }
1719
2270
  },
1720
- [n$1.JSON_STRINGIFY]: {
1721
- stackIn: 1,
2271
+ [n.SET]: {
2272
+ stackIn: 3,
1722
2273
  stackOut: 1,
1723
- execute: (e2) => {
1724
- const r = e2.stack.pop(), t2 = _(r);
1725
- e2.stack.push(JSON.stringify(t2));
1726
- }
1727
- }
1728
- }, z = {
1729
- ...G,
1730
- ...L,
1731
- ...V,
1732
- ...F,
1733
- ...j,
1734
- ...D,
1735
- ...q,
1736
- ...H,
1737
- ...$,
1738
- ...J,
1739
- ...Y,
2274
+ execute: (e2, t$1) => {
2275
+ const r2 = e2.stack.pop(), o2 = e2.stack.pop(), s2 = e2.stack.pop();
2276
+ if (u$1(s2)) {
2277
+ const c2 = e2.heap.get(s2.id);
2278
+ if (!c2 || c2.type !== "array")
2279
+ return {
2280
+ type: "RuntimeError",
2281
+ message: "Invalid array reference",
2282
+ pc: e2.pc,
2283
+ opcode: t$1.op
2284
+ };
2285
+ const p3 = c2.data;
2286
+ let u2;
2287
+ if (i$1(o2))
2288
+ u2 = o2;
2289
+ else if (t(o2)) {
2290
+ const i2 = parseInt(o2, 10);
2291
+ if (!isNaN(i2) && i2.toString() === o2 && i2 >= 0)
2292
+ u2 = i2;
2293
+ else {
2294
+ p3.properties || (p3.properties = {}), p3.properties[o2] = r2, e2.stack.push(s2);
2295
+ return;
2296
+ }
2297
+ } else
2298
+ return {
2299
+ type: "RuntimeError",
2300
+ message: "SET requires numeric or numeric string index for arrays",
2301
+ pc: e2.pc,
2302
+ opcode: t$1.op
2303
+ };
2304
+ const a2 = Math.floor(u2);
2305
+ if (a2 < 0)
2306
+ return {
2307
+ type: "RuntimeError",
2308
+ message: "SET: Negative index not allowed",
2309
+ pc: e2.pc,
2310
+ opcode: t$1.op
2311
+ };
2312
+ p3.elements[a2] = r2, e2.stack.push(s2);
2313
+ return;
2314
+ } else if (b(s2)) {
2315
+ const c2 = e2.heap.get(s2.id);
2316
+ if (!c2 || c2.type !== "object")
2317
+ return {
2318
+ type: "RuntimeError",
2319
+ message: "Invalid object reference",
2320
+ pc: e2.pc,
2321
+ opcode: t$1.op
2322
+ };
2323
+ const p3 = c2.data, u2 = p$1(o2);
2324
+ p3.properties[u2] = r2, e2.stack.push(s2);
2325
+ return;
2326
+ } else
2327
+ return {
2328
+ type: "RuntimeError",
2329
+ message: "SET requires array or object",
2330
+ pc: e2.pc,
2331
+ opcode: t$1.op
2332
+ };
2333
+ }
2334
+ }
2335
+ }, F = {
1740
2336
  ...B,
1741
- ...W
2337
+ ...W,
2338
+ ...X,
2339
+ ...K,
2340
+ ...Q,
2341
+ ...Z,
2342
+ ...ee,
2343
+ ...re,
2344
+ ...te,
2345
+ ...oe,
2346
+ ...se,
2347
+ ...ce,
2348
+ ...ne,
2349
+ ...pe
1742
2350
  };
1743
- class N {
1744
- validateStack(r, t2, s2) {
1745
- if (s2.stack.length < r.stackIn)
2351
+ function P() {
2352
+ const e2 = /* @__PURE__ */ new Map();
2353
+ let t2 = 1;
2354
+ return {
2355
+ objects: e2,
2356
+ nextId: t2,
2357
+ allocate(r2, o2) {
2358
+ const s2 = t2++, c2 = { id: s2, type: r2, data: o2 };
2359
+ return e2.set(s2, c2), r2 === "array" ? { type: "array-ref", id: s2 } : { type: "object-ref", id: s2 };
2360
+ },
2361
+ get(r2) {
2362
+ const o2 = e2.get(r2);
2363
+ if (o2)
2364
+ return o2;
2365
+ }
2366
+ };
2367
+ }
2368
+ class A {
2369
+ createInitialState() {
2370
+ return {
2371
+ pc: 0,
2372
+ stack: [],
2373
+ variables: /* @__PURE__ */ new Map(),
2374
+ status: "running",
2375
+ output: [],
2376
+ iterators: [],
2377
+ heap: P()
2378
+ };
2379
+ }
2380
+ executeInstruction(t2, r2) {
2381
+ const o2 = F[r2.op];
2382
+ if (!o2)
2383
+ throw new Error(`Unknown opcode: ${r2.op}`);
2384
+ const s2 = this.validateStack(o2, r2, t2);
2385
+ if (s2) {
2386
+ t2.status = "error", t2.error = s2.message;
2387
+ return;
2388
+ }
2389
+ o2.execute(t2, r2), t2.pc++;
2390
+ }
2391
+ validateStack(t2, r2, o2) {
2392
+ if (o2.stack.length < t2.stackIn)
1746
2393
  return {
1747
2394
  type: "StackUnderflow",
1748
- message: `${n$1[t2.op]}: Stack underflow`,
1749
- pc: s2.pc,
1750
- opcode: t2.op
2395
+ message: `${n[r2.op]}: Stack underflow`,
2396
+ pc: o2.pc,
2397
+ opcode: r2.op
1751
2398
  };
1752
2399
  }
1753
- execute(r, t2) {
2400
+ execute(t2, r2, o2) {
1754
2401
  const s2 = {
1755
- pc: (t2 == null ? void 0 : t2.pc) ?? 0,
1756
- stack: (t2 == null ? void 0 : t2.stack) ?? [],
1757
- variables: (t2 == null ? void 0 : t2.variables) ?? /* @__PURE__ */ new Map(),
2402
+ pc: (r2 == null ? void 0 : r2.pc) ?? 0,
2403
+ stack: (r2 == null ? void 0 : r2.stack) ?? [],
2404
+ variables: (r2 == null ? void 0 : r2.variables) ?? /* @__PURE__ */ new Map(),
1758
2405
  status: "running",
1759
- output: (t2 == null ? void 0 : t2.output) ?? [],
1760
- iterators: (t2 == null ? void 0 : t2.iterators) ?? [],
1761
- ...t2
2406
+ output: (r2 == null ? void 0 : r2.output) ?? [],
2407
+ iterators: (r2 == null ? void 0 : r2.iterators) ?? [],
2408
+ fileSystem: o2,
2409
+ heap: (r2 == null ? void 0 : r2.heap) ?? P(),
2410
+ ...r2
1762
2411
  };
1763
- for (; s2.status === "running" && s2.pc < r.length; ) {
1764
- const o2 = r[s2.pc], n2 = z[o2.op];
1765
- if (n2) {
1766
- const a2 = this.validateStack(n2, o2, s2);
1767
- if (a2) {
1768
- s2.status = "error", s2.error = a2.message;
2412
+ for (; s2.status === "running" && s2.pc < t2.length; ) {
2413
+ const c2 = t2[s2.pc], p3 = F[c2.op];
2414
+ if (p3) {
2415
+ const u2 = this.validateStack(p3, c2, s2);
2416
+ if (u2) {
2417
+ s2.status = "error", s2.error = u2.message;
1769
2418
  break;
1770
2419
  }
1771
- if ((o2.op === n$1.JUMP || o2.op === n$1.JUMP_IF_FALSE || o2.op === n$1.JUMP_IF || o2.op === n$1.JUMP_IF_TRUE || o2.op === n$1.BREAK || o2.op === n$1.CONTINUE) && o2.arg !== void 0 && o2.arg >= r.length) {
2420
+ if ((c2.op === n.JUMP || c2.op === n.JUMP_IF_FALSE || c2.op === n.JUMP_IF || c2.op === n.JUMP_IF_TRUE || c2.op === n.BREAK || c2.op === n.CONTINUE) && c2.arg !== void 0 && c2.arg >= t2.length) {
1772
2421
  s2.status = "error";
1773
- const u2 = o2.op === n$1.JUMP || o2.op === n$1.JUMP_IF_FALSE || o2.op === n$1.JUMP_IF || o2.op === n$1.JUMP_IF_TRUE ? "jump" : o2.op === n$1.BREAK ? "break" : "continue";
1774
- s2.error = `Invalid ${u2} target: ${o2.arg}`;
2422
+ const i2 = c2.op === n.JUMP || c2.op === n.JUMP_IF_FALSE || c2.op === n.JUMP_IF || c2.op === n.JUMP_IF_TRUE ? "jump" : c2.op === n.BREAK ? "break" : "continue";
2423
+ s2.error = `Invalid ${i2} target: ${c2.arg}`;
1775
2424
  break;
1776
2425
  }
1777
- const i2 = n2.execute(s2, o2);
1778
- if (i2) {
1779
- s2.status = "error", s2.error = i2.message;
2426
+ const a2 = p3.execute(s2, c2);
2427
+ if (a2) {
2428
+ const i2 = s2.pc + 1;
2429
+ if (i2 < t2.length && t2[i2].op === n.CC) {
2430
+ s2.ccPrompt = `ERROR: ${a2.message}. How should I proceed?`, s2.status = "waiting_cc", s2.pc = i2;
2431
+ break;
2432
+ }
2433
+ s2.status = "error", s2.error = a2.message;
1780
2434
  break;
1781
2435
  }
1782
- n2.controlsPC || s2.pc++;
2436
+ p3.controlsPC || s2.pc++;
1783
2437
  continue;
1784
2438
  }
1785
- s2.status = "error", s2.error = `Unknown opcode: ${n$1[o2.op]}`;
2439
+ s2.status = "error", s2.error = `Unknown opcode: ${n[c2.op]}`;
1786
2440
  break;
1787
2441
  }
1788
2442
  return s2;
1789
2443
  }
1790
- resume(r, t2, s2) {
1791
- if (r.status !== "waiting_cc")
2444
+ resume(t2, r2, o2, s2) {
2445
+ if (t2.status !== "waiting_cc")
1792
2446
  throw new Error("Cannot resume: VM not waiting for CC");
1793
- const o2 = {
1794
- ...r,
1795
- stack: [...r.stack, t2],
2447
+ const c2 = {
2448
+ ...t2,
2449
+ stack: [...t2.stack, r2],
1796
2450
  status: "running",
1797
2451
  ccPrompt: void 0,
1798
- pc: r.pc + 1
2452
+ pc: t2.pc + 1
1799
2453
  };
1800
- return this.execute(s2, o2);
1801
- }
1802
- resumeWithFsResult(r, t2, s2) {
1803
- if (r.status !== "waiting_fs")
1804
- throw new Error("Cannot resume: VM not waiting for FS operation");
1805
- const o2 = {
1806
- ...r,
1807
- stack: [...r.stack, t2],
1808
- status: "running",
1809
- fsOperation: void 0,
1810
- pc: r.pc + 1
1811
- };
1812
- return this.execute(s2, o2);
2454
+ return this.execute(o2, c2, s2 || t2.fileSystem);
1813
2455
  }
1814
2456
  }
1815
- class P {
2457
+ class G {
1816
2458
  constructor() {
1817
- h2(this, "sandboxPaths", []);
1818
- const r = process.env.CVM_SANDBOX_PATHS, t2 = process.env.CVM_SANDBOX_ROOT;
1819
- r ? this.sandboxPaths = r.split(",").map((s2) => c__namespace.resolve(s2.trim())) : t2 && (this.sandboxPaths = [c__namespace.resolve(t2)]);
1820
- }
1821
- isPathAllowed(r) {
1822
- const t2 = c__namespace.resolve(r);
1823
- return t2.includes("..") || this.sandboxPaths.length === 0 ? false : this.sandboxPaths.some(
1824
- (s2) => t2.startsWith(s2)
2459
+ _(this, "sandboxPaths", []);
2460
+ const t2 = process.env.CVM_SANDBOX_PATHS, r2 = process.env.CVM_SANDBOX_ROOT;
2461
+ t2 ? this.sandboxPaths = t2.split(",").map((o2) => r__namespace.resolve(o2.trim())) : r2 && (this.sandboxPaths = [r__namespace.resolve(r2)]);
2462
+ }
2463
+ isPathAllowed(t2) {
2464
+ const r2 = r__namespace.resolve(t2);
2465
+ return r2.includes("..") || this.sandboxPaths.length === 0 ? false : this.sandboxPaths.some(
2466
+ (o2) => r2.startsWith(o2)
1825
2467
  );
1826
2468
  }
1827
- listFiles(r, t2 = {}) {
1828
- const s2 = d();
1829
- if (!this.isPathAllowed(r))
1830
- return s2;
2469
+ listFiles(t2, r2 = {}) {
2470
+ const o2 = s();
2471
+ if (!this.isPathAllowed(t2))
2472
+ return o2;
1831
2473
  try {
1832
- const o2 = c__namespace.resolve(r);
1833
- this.listFilesRecursive(o2, o2, s2.elements, t2);
2474
+ const s2 = r__namespace.resolve(t2);
2475
+ this.listFilesRecursive(s2, s2, o2.elements, r2);
1834
2476
  } catch {
1835
2477
  }
1836
- return s2;
2478
+ return o2;
1837
2479
  }
1838
- matchesPattern(r, t2) {
1839
- let s2 = t2.replace(/\./g, "\\.").replace(/\*\*/g, "__DOUBLESTAR__").replace(/\*/g, "[^/]*").replace(/__DOUBLESTAR__/g, ".*").replace(/\?/g, ".");
1840
- s2 = "^" + s2 + "$";
2480
+ matchesPattern(t2, r2) {
2481
+ let o2 = r2.replace(/\./g, "\\.").replace(/\*\*/g, "__DOUBLESTAR__").replace(/\*/g, "[^/]*").replace(/__DOUBLESTAR__/g, ".*").replace(/\?/g, ".");
2482
+ o2 = "^" + o2 + "$";
1841
2483
  try {
1842
- return new RegExp(s2).test(r);
2484
+ return new RegExp(o2).test(t2);
1843
2485
  } catch {
1844
2486
  return true;
1845
2487
  }
1846
2488
  }
1847
- listFilesRecursive(r, t2, s2, o2, n2 = 0) {
2489
+ listFilesRecursive(t2, r2, o2, s2, c2 = 0) {
1848
2490
  try {
1849
- const a2 = M__namespace.readdirSync(t2, { withFileTypes: true });
1850
- for (const i2 of a2) {
1851
- const u2 = c__namespace.join(t2, i2.name), O2 = c__namespace.relative(r, u2);
1852
- i2.isSymbolicLink() || o2.filter && !this.matchesPattern(O2, o2.filter) || (s2.push(u2), i2.isDirectory() && o2.recursive && this.listFilesRecursive(r, u2, s2, o2, n2 + 1));
2491
+ const p3 = b__namespace.readdirSync(r2, { withFileTypes: true });
2492
+ for (const u2 of p3) {
2493
+ const a2 = r__namespace.join(r2, u2.name), i2 = r__namespace.relative(t2, a2);
2494
+ u2.isSymbolicLink() || s2.filter && !this.matchesPattern(i2, s2.filter) || (o2.push(a2), u2.isDirectory() && s2.recursive && this.listFilesRecursive(t2, a2, o2, s2, c2 + 1));
1853
2495
  }
1854
2496
  } catch {
1855
2497
  }
1856
2498
  }
2499
+ readFile(t2) {
2500
+ if (!this.isPathAllowed(t2))
2501
+ return null;
2502
+ try {
2503
+ const r2 = r__namespace.resolve(t2), o2 = b__namespace.lstatSync(r2);
2504
+ return o2.isSymbolicLink() || !o2.isFile() ? null : b__namespace.readFileSync(r2, "utf-8");
2505
+ } catch {
2506
+ return null;
2507
+ }
2508
+ }
2509
+ writeFile(t2, r2) {
2510
+ if (!this.isPathAllowed(t2))
2511
+ return false;
2512
+ try {
2513
+ const o2 = r__namespace.resolve(t2), s2 = r__namespace.dirname(o2);
2514
+ if (!this.isPathAllowed(s2))
2515
+ return false;
2516
+ try {
2517
+ if (b__namespace.lstatSync(o2).isSymbolicLink())
2518
+ return false;
2519
+ } catch (c2) {
2520
+ if (c2.code !== "ENOENT")
2521
+ return false;
2522
+ }
2523
+ return b__namespace.mkdirSync(s2, { recursive: true }), b__namespace.writeFileSync(o2, r2, "utf-8"), true;
2524
+ } catch {
2525
+ return false;
2526
+ }
2527
+ }
1857
2528
  }
1858
- new P();
1859
- class te {
1860
- constructor(r, t2) {
1861
- h2(this, "vms", /* @__PURE__ */ new Map());
1862
- h2(this, "storage");
1863
- h2(this, "fileSystem");
1864
- r ? this.storage = r : this.storage = E.create(), this.fileSystem = t2 || new P();
2529
+ new G();
2530
+ class fe {
2531
+ constructor(t2, r2) {
2532
+ _(this, "vms", /* @__PURE__ */ new Map());
2533
+ _(this, "storage");
2534
+ _(this, "fileSystem");
2535
+ t2 ? this.storage = t2 : this.storage = E.create(), this.fileSystem = r2 || new G();
1865
2536
  }
1866
2537
  /**
1867
2538
  * Initialize the VMManager (connect to database)
@@ -1878,152 +2549,138 @@ class te {
1878
2549
  /**
1879
2550
  * Load and compile a program from source code
1880
2551
  */
1881
- async loadProgram(r, t2) {
1882
- const s2 = W$1(t2);
1883
- if (!s2.success)
1884
- throw new Error(`Compilation failed: ${s2.errors.join(", ")}`);
1885
- const o2 = {
1886
- id: r,
1887
- name: r,
1888
- source: t2,
1889
- bytecode: s2.bytecode,
2552
+ async loadProgram(t2, r2) {
2553
+ const o2 = j$2(r2);
2554
+ if (!o2.success)
2555
+ throw new Error(`Compilation failed: ${o2.errors.join(", ")}`);
2556
+ const s2 = {
2557
+ id: t2,
2558
+ name: t2,
2559
+ source: r2,
2560
+ bytecode: o2.bytecode,
1890
2561
  // VM decides internal format
1891
2562
  created: /* @__PURE__ */ new Date()
1892
2563
  };
1893
- await this.storage.saveProgram(o2);
2564
+ await this.storage.saveProgram(s2);
1894
2565
  }
1895
2566
  /**
1896
2567
  * Start execution of a loaded program
1897
2568
  */
1898
- async startExecution(r, t2) {
1899
- if (!await this.storage.getProgram(r))
1900
- throw new Error(`Program not found: ${r}`);
1901
- const o2 = {
1902
- id: t2,
1903
- programId: r,
2569
+ async startExecution(t2, r2) {
2570
+ if (!await this.storage.getProgram(t2))
2571
+ throw new Error(`Program not found: ${t2}`);
2572
+ const s2 = {
2573
+ id: r2,
2574
+ programId: t2,
1904
2575
  state: "READY",
1905
2576
  pc: 0,
1906
2577
  stack: [],
1907
2578
  variables: {},
1908
2579
  created: /* @__PURE__ */ new Date()
1909
2580
  };
1910
- await this.storage.saveExecution(o2);
1911
- const n2 = new N();
1912
- this.vms.set(t2, n2);
2581
+ await this.storage.saveExecution(s2);
2582
+ const c2 = new A();
2583
+ this.vms.set(r2, c2);
1913
2584
  }
1914
2585
  /**
1915
2586
  * Get next action from execution (Claude polls this)
1916
2587
  * This is READ-ONLY - just returns current state
1917
2588
  */
1918
- async getNext(r) {
1919
- const t2 = await this.storage.getExecution(r);
1920
- if (!t2)
1921
- throw new Error(`Execution not found: ${r}`);
1922
- if (t2.state === "READY") {
1923
- const s2 = await this.storage.getProgram(t2.programId);
1924
- if (!s2)
1925
- throw new Error(`Program not found: ${t2.programId}`);
1926
- let o2 = this.vms.get(r);
1927
- o2 || (o2 = new N(), this.vms.set(r, o2));
1928
- const n2 = {
2589
+ async getNext(t2) {
2590
+ const r2 = await this.storage.getExecution(t2);
2591
+ if (!r2)
2592
+ throw new Error(`Execution not found: ${t2}`);
2593
+ if (r2.state === "READY" || r2.state === "RUNNING") {
2594
+ const o2 = await this.storage.getProgram(r2.programId);
2595
+ if (!o2)
2596
+ throw new Error(`Program not found: ${r2.programId}`);
2597
+ let s2 = this.vms.get(t2);
2598
+ s2 || (s2 = new A(), this.vms.set(t2, s2));
2599
+ const c2 = r2.state === "READY" ? {
1929
2600
  pc: 0,
1930
2601
  stack: [],
1931
2602
  variables: /* @__PURE__ */ new Map(),
1932
2603
  output: []
1933
- }, a2 = o2.execute(s2.bytecode, n2);
1934
- if (a2.output.length > 0 && await this.storage.appendOutput(r, a2.output), t2.pc = a2.pc, t2.stack = a2.stack, t2.variables = Object.fromEntries(a2.variables), t2.iterators = a2.iterators, a2.status === "complete")
1935
- return t2.state = "COMPLETED", a2.returnValue !== void 0 && (t2.returnValue = a2.returnValue), await this.storage.saveExecution(t2), this.vms.delete(r), {
2604
+ } : this.deserializeVMState(r2), p3 = s2.execute(o2.bytecode, c2, this.fileSystem);
2605
+ p3.output.length > 0 && await this.storage.appendOutput(t2, p3.output);
2606
+ const u2 = this.serializeVMState(p3);
2607
+ if (Object.assign(r2, u2), p3.status === "complete")
2608
+ return r2.state = "COMPLETED", p3.returnValue !== void 0 && (r2.returnValue = p3.returnValue), await this.storage.saveExecution(r2), this.vms.delete(t2), {
1936
2609
  type: "completed",
1937
2610
  message: "Execution completed",
1938
- result: a2.returnValue
2611
+ result: p3.returnValue
1939
2612
  };
1940
- if (a2.status === "waiting_cc")
1941
- return t2.state = "AWAITING_COGNITIVE_RESULT", t2.ccPrompt = a2.ccPrompt, await this.storage.saveExecution(t2), {
2613
+ if (p3.status === "waiting_cc")
2614
+ return r2.state = "AWAITING_COGNITIVE_RESULT", r2.ccPrompt = p3.ccPrompt, await this.storage.saveExecution(r2), {
1942
2615
  type: "waiting",
1943
- message: a2.ccPrompt || "Waiting for input"
2616
+ message: p3.ccPrompt || "Waiting for input"
1944
2617
  };
1945
- if (a2.status === "waiting_fs") {
1946
- if (a2.fsOperation) {
1947
- const i2 = this.fileSystem.listFiles(a2.fsOperation.path, a2.fsOperation.options), u2 = o2.resumeWithFsResult(a2, i2, s2.bytecode);
1948
- return u2.output.length > 0 && await this.storage.appendOutput(r, u2.output), t2.pc = u2.pc, t2.stack = u2.stack, t2.variables = Object.fromEntries(u2.variables), t2.iterators = u2.iterators, u2.status === "complete" ? (t2.state = "COMPLETED", u2.returnValue !== void 0 && (t2.returnValue = u2.returnValue), await this.storage.saveExecution(t2), this.vms.delete(r), {
1949
- type: "completed",
1950
- message: "Execution completed",
1951
- result: u2.returnValue
1952
- }) : u2.status === "error" ? (t2.state = "ERROR", t2.error = u2.error, await this.storage.saveExecution(t2), this.vms.delete(r), {
1953
- type: "error",
1954
- error: u2.error
1955
- }) : u2.status === "waiting_cc" ? (t2.state = "AWAITING_COGNITIVE_RESULT", t2.ccPrompt = u2.ccPrompt, await this.storage.saveExecution(t2), {
1956
- type: "waiting",
1957
- message: u2.ccPrompt || "Waiting for input"
1958
- }) : (t2.state = "RUNNING", await this.storage.saveExecution(t2), this.getNext(r));
1959
- }
1960
- } else if (a2.status === "error")
1961
- return t2.state = "ERROR", t2.error = a2.error, await this.storage.saveExecution(t2), this.vms.delete(r), {
2618
+ if (p3.status === "error")
2619
+ return r2.state = "ERROR", r2.error = p3.error, await this.storage.saveExecution(r2), this.vms.delete(t2), {
1962
2620
  type: "error",
1963
- error: a2.error
2621
+ error: p3.error
1964
2622
  };
1965
2623
  }
1966
- if (t2.state === "COMPLETED")
2624
+ if (r2.state === "COMPLETED")
1967
2625
  return {
1968
2626
  type: "completed",
1969
2627
  message: "Execution completed"
1970
2628
  };
1971
- if (t2.state === "ERROR")
2629
+ if (r2.state === "ERROR")
1972
2630
  return {
1973
2631
  type: "error",
1974
- error: t2.error || "Unknown error"
2632
+ error: r2.error || "Unknown error"
1975
2633
  };
1976
- if (t2.state === "AWAITING_COGNITIVE_RESULT")
2634
+ if (r2.state === "AWAITING_COGNITIVE_RESULT")
1977
2635
  return {
1978
2636
  type: "waiting",
1979
- message: t2.ccPrompt || "Waiting for input"
2637
+ message: r2.ccPrompt || "Waiting for input"
1980
2638
  };
1981
- throw new Error(`Unexpected execution state: ${t2.state}`);
2639
+ throw new Error(`Unexpected execution state: ${r2.state}`);
1982
2640
  }
1983
2641
  /**
1984
2642
  * Report result from cognitive operation and continue execution
1985
2643
  */
1986
- async reportCCResult(r, t2) {
1987
- const s2 = await this.storage.getExecution(r);
1988
- if (!s2)
1989
- throw new Error(`Execution not found: ${r}`);
1990
- const o2 = await this.storage.getProgram(s2.programId);
2644
+ async reportCCResult(t2, r2) {
2645
+ const o2 = await this.storage.getExecution(t2);
1991
2646
  if (!o2)
1992
- throw new Error(`Program not found: ${s2.programId}`);
1993
- let n2 = this.vms.get(r);
1994
- n2 || (n2 = new N(), this.vms.set(r, n2));
1995
- const a2 = {
1996
- pc: s2.pc,
1997
- stack: s2.stack,
1998
- variables: new Map(Object.entries(s2.variables)),
2647
+ throw new Error(`Execution not found: ${t2}`);
2648
+ const s2 = await this.storage.getProgram(o2.programId);
2649
+ if (!s2)
2650
+ throw new Error(`Program not found: ${o2.programId}`);
2651
+ let c2 = this.vms.get(t2);
2652
+ c2 || (c2 = new A(), this.vms.set(t2, c2));
2653
+ const p3 = {
2654
+ ...this.deserializeVMState(o2),
1999
2655
  status: "waiting_cc",
2000
2656
  output: [],
2001
2657
  // Start with empty output for resumed execution
2002
- ccPrompt: void 0,
2003
- iterators: s2.iterators || []
2004
- }, i2 = n2.resume(a2, t2, o2.bytecode);
2005
- i2.output.length > 0 && await this.storage.appendOutput(r, i2.output), s2.pc = i2.pc, s2.stack = i2.stack, s2.variables = Object.fromEntries(i2.variables), s2.iterators = i2.iterators, i2.status === "complete" ? (s2.state = "COMPLETED", i2.returnValue !== void 0 && (s2.returnValue = i2.returnValue), this.vms.delete(r)) : i2.status === "error" ? (s2.state = "ERROR", s2.error = i2.error, this.vms.delete(r)) : i2.status === "waiting_cc" ? (s2.state = "AWAITING_COGNITIVE_RESULT", s2.ccPrompt = i2.ccPrompt) : s2.state = "RUNNING", await this.storage.saveExecution(s2);
2658
+ ccPrompt: void 0
2659
+ }, u2 = c2.resume(p3, r2, s2.bytecode, this.fileSystem);
2660
+ u2.output.length > 0 && await this.storage.appendOutput(t2, u2.output);
2661
+ const a2 = this.serializeVMState(u2);
2662
+ Object.assign(o2, a2), u2.status === "complete" ? (o2.state = "COMPLETED", u2.returnValue !== void 0 && (o2.returnValue = u2.returnValue), this.vms.delete(t2)) : u2.status === "error" ? (o2.state = "ERROR", o2.error = u2.error, this.vms.delete(t2)) : u2.status === "waiting_cc" ? (o2.state = "AWAITING_COGNITIVE_RESULT", o2.ccPrompt = u2.ccPrompt) : o2.state = "RUNNING", await this.storage.saveExecution(o2);
2006
2663
  }
2007
2664
  /**
2008
2665
  * Get current execution status
2009
2666
  */
2010
- async getExecutionStatus(r) {
2011
- const t2 = await this.storage.getExecution(r);
2012
- if (!t2)
2013
- throw new Error(`Execution not found: ${r}`);
2667
+ async getExecutionStatus(t2) {
2668
+ const r2 = await this.storage.getExecution(t2);
2669
+ if (!r2)
2670
+ throw new Error(`Execution not found: ${t2}`);
2014
2671
  return {
2015
- id: t2.id,
2016
- state: t2.state,
2017
- pc: t2.pc,
2018
- stack: t2.stack,
2019
- variables: t2.variables
2672
+ id: r2.id,
2673
+ state: r2.state,
2674
+ pc: r2.pc,
2675
+ stack: r2.stack,
2676
+ variables: r2.variables
2020
2677
  };
2021
2678
  }
2022
2679
  /**
2023
2680
  * Get output for an execution
2024
2681
  */
2025
- async getExecutionOutput(r) {
2026
- return await this.storage.getOutput(r);
2682
+ async getExecutionOutput(t2) {
2683
+ return await this.storage.getOutput(t2);
2027
2684
  }
2028
2685
  /**
2029
2686
  * List all executions
@@ -2040,35 +2697,138 @@ class te {
2040
2697
  /**
2041
2698
  * Set current execution ID
2042
2699
  */
2043
- async setCurrentExecutionId(r) {
2044
- return await this.storage.setCurrentExecutionId(r);
2700
+ async setCurrentExecutionId(t2) {
2701
+ return await this.storage.setCurrentExecutionId(t2);
2045
2702
  }
2046
2703
  /**
2047
2704
  * Delete an execution
2048
2705
  */
2049
- async deleteExecution(r) {
2050
- return await this.storage.deleteExecution(r);
2706
+ async deleteExecution(t2) {
2707
+ return this.vms.delete(t2), await this.storage.deleteExecution(t2);
2051
2708
  }
2052
2709
  /**
2053
2710
  * Get execution with attempt tracking
2054
2711
  */
2055
- async getExecutionWithAttempts(r) {
2056
- const t2 = await this.storage.getExecution(r);
2057
- if (!t2)
2058
- throw new Error(`Execution not found: ${r}`);
2712
+ async getExecutionWithAttempts(t2) {
2713
+ const r2 = await this.storage.getExecution(t2);
2714
+ if (!r2)
2715
+ throw new Error(`Execution not found: ${t2}`);
2716
+ return r2;
2717
+ }
2718
+ /**
2719
+ * List all programs
2720
+ */
2721
+ async listPrograms() {
2722
+ return await this.storage.listPrograms();
2723
+ }
2724
+ /**
2725
+ * Delete a program
2726
+ */
2727
+ async deleteProgram(t2) {
2728
+ return await this.storage.deleteProgram(t2);
2729
+ }
2730
+ /**
2731
+ * Restart a program (create new execution and set as current)
2732
+ */
2733
+ async restartExecution(t2, r2) {
2734
+ const o2 = r2 || `${t2}-${Date.now()}`;
2735
+ return await this.startExecution(t2, o2), await this.setCurrentExecutionId(o2), o2;
2736
+ }
2737
+ /**
2738
+ * Serialize heap to storage format
2739
+ */
2740
+ serializeHeap(t2) {
2741
+ const r2 = (s2, c2) => u$1(c2) || b(c2) ? { $ref: c2.id } : c2, o2 = {};
2742
+ return t2.objects.forEach((s2, c2) => {
2743
+ o2[c2] = {
2744
+ type: s2.type,
2745
+ // Serialize each object's data individually with replacer
2746
+ data: JSON.parse(JSON.stringify(s2.data, r2))
2747
+ };
2748
+ }), {
2749
+ objects: o2,
2750
+ nextId: t2.nextId
2751
+ };
2752
+ }
2753
+ /**
2754
+ * Deserialize heap from storage format
2755
+ */
2756
+ deserializeHeap(t2) {
2757
+ const r2 = P();
2758
+ r2.nextId = t2.nextId;
2759
+ for (const o2 in t2.objects) {
2760
+ const s2 = parseInt(o2, 10), c2 = t2.objects[s2];
2761
+ r2.objects.set(s2, {
2762
+ id: s2,
2763
+ type: c2.type,
2764
+ data: c2.data
2765
+ // Still contains $ref stubs
2766
+ });
2767
+ }
2768
+ return r2.objects.forEach((o2) => {
2769
+ o2.data = this.restoreReferences(o2.data, r2);
2770
+ }), r2;
2771
+ }
2772
+ /**
2773
+ * Serialize VM state for storage
2774
+ */
2775
+ serializeVMState(t2) {
2776
+ return {
2777
+ pc: t2.pc,
2778
+ stack: t2.stack,
2779
+ variables: Object.fromEntries(t2.variables),
2780
+ heap: t2.heap ? this.serializeHeap(t2.heap) : void 0,
2781
+ iterators: t2.iterators,
2782
+ status: t2.status,
2783
+ error: t2.error,
2784
+ ccPrompt: t2.ccPrompt,
2785
+ output: t2.output,
2786
+ returnValue: t2.returnValue
2787
+ };
2788
+ }
2789
+ /**
2790
+ * Deserialize VM state from storage
2791
+ */
2792
+ deserializeVMState(t2) {
2793
+ return {
2794
+ pc: t2.pc || 0,
2795
+ stack: t2.stack || [],
2796
+ variables: new Map(Object.entries(t2.variables || {})),
2797
+ heap: t2.heap ? this.deserializeHeap(t2.heap) : P(),
2798
+ iterators: t2.iterators || [],
2799
+ status: t2.status || "running",
2800
+ error: t2.error || null,
2801
+ ccPrompt: t2.ccPrompt || null,
2802
+ output: t2.output || [],
2803
+ returnValue: t2.returnValue
2804
+ };
2805
+ }
2806
+ restoreReferences(t2, r2) {
2807
+ if (t2 && typeof t2 == "object" && t2.$ref !== void 0) {
2808
+ const o2 = r2.objects.get(t2.$ref);
2809
+ return (o2 == null ? void 0 : o2.type) === "array" ? { type: "array-ref", id: t2.$ref } : (o2 == null ? void 0 : o2.type) === "object" ? { type: "object-ref", id: t2.$ref } : t2;
2810
+ }
2811
+ if (Array.isArray(t2))
2812
+ return t2.map((o2) => this.restoreReferences(o2, r2));
2813
+ if (t2 && typeof t2 == "object") {
2814
+ const o2 = {};
2815
+ for (const s2 in t2)
2816
+ o2[s2] = this.restoreReferences(t2[s2], r2);
2817
+ return o2;
2818
+ }
2059
2819
  return t2;
2060
2820
  }
2061
2821
  }
2062
2822
  var u = Object.defineProperty;
2063
- var x = (s2, r, e2) => r in s2 ? u(s2, r, { enumerable: true, configurable: true, writable: true, value: e2 }) : s2[r] = e2;
2064
- var i = (s2, r, e2) => x(s2, typeof r != "symbol" ? r + "" : r, e2);
2065
- class M {
2066
- constructor(r = "0.0.1") {
2823
+ var x = (s2, e2, r2) => e2 in s2 ? u(s2, e2, { enumerable: true, configurable: true, writable: true, value: r2 }) : s2[e2] = r2;
2824
+ var i = (s2, e2, r2) => x(s2, typeof e2 != "symbol" ? e2 + "" : e2, r2);
2825
+ class $ {
2826
+ constructor(e2 = "0.0.1") {
2067
2827
  i(this, "server");
2068
2828
  i(this, "transport", null);
2069
2829
  i(this, "vmManager");
2070
2830
  i(this, "version");
2071
- this.version = r, this.vmManager = new te(), this.server = new mcp_js.McpServer({
2831
+ this.version = e2, this.vmManager = new fe(), this.server = new mcp_js.McpServer({
2072
2832
  name: "cvm-server",
2073
2833
  version: this.version
2074
2834
  }), this.setupTools();
@@ -2086,10 +2846,10 @@ class M {
2086
2846
  programId: zod.z.string(),
2087
2847
  source: zod.z.string()
2088
2848
  },
2089
- async ({ programId: r, source: e2 }) => {
2849
+ async ({ programId: e2, source: r2 }) => {
2090
2850
  try {
2091
- return await this.vmManager.loadProgram(r, e2), {
2092
- content: [{ type: "text", text: `Program loaded successfully: ${r}` }]
2851
+ return await this.vmManager.loadProgram(e2, r2), {
2852
+ content: [{ type: "text", text: `Program loaded successfully: ${e2}` }]
2093
2853
  };
2094
2854
  } catch (t2) {
2095
2855
  return {
@@ -2104,23 +2864,23 @@ class M {
2104
2864
  programId: zod.z.string(),
2105
2865
  filePath: zod.z.string()
2106
2866
  },
2107
- async ({ programId: r, filePath: e2 }) => {
2867
+ async ({ programId: e2, filePath: r$1 }) => {
2108
2868
  try {
2109
- const t2 = c$1.resolve(e2), n2 = await promises.readFile(t2, "utf-8");
2110
- return await this.vmManager.loadProgram(r, n2), {
2111
- content: [{ type: "text", text: `Program loaded successfully from ${e2}: ${r}` }]
2869
+ const t2 = r.resolve(r$1), n2 = await promises.readFile(t2, "utf-8");
2870
+ return await this.vmManager.loadProgram(e2, n2), {
2871
+ content: [{ type: "text", text: `Program loaded successfully from ${r$1}: ${e2}` }]
2112
2872
  };
2113
2873
  } catch (t2) {
2114
2874
  if (t2 instanceof Error) {
2115
2875
  const n2 = t2;
2116
2876
  if (n2.code === "ENOENT")
2117
2877
  return {
2118
- content: [{ type: "text", text: `Error: File not found: ${e2}` }],
2878
+ content: [{ type: "text", text: `Error: File not found: ${r$1}` }],
2119
2879
  isError: true
2120
2880
  };
2121
2881
  if (n2.code === "EACCES")
2122
2882
  return {
2123
- content: [{ type: "text", text: `Error: Permission denied: ${e2}` }],
2883
+ content: [{ type: "text", text: `Error: Permission denied: ${r$1}` }],
2124
2884
  isError: true
2125
2885
  };
2126
2886
  }
@@ -2137,10 +2897,10 @@ class M {
2137
2897
  executionId: zod.z.string(),
2138
2898
  setCurrent: zod.z.boolean().optional()
2139
2899
  },
2140
- async ({ programId: r, executionId: e2, setCurrent: t2 = true }) => {
2900
+ async ({ programId: e2, executionId: r2, setCurrent: t2 = true }) => {
2141
2901
  try {
2142
- return await this.vmManager.startExecution(r, e2), t2 && await this.vmManager.setCurrentExecutionId(e2), {
2143
- content: [{ type: "text", text: `Execution started: ${e2}${t2 ? " (set as current)" : ""}` }]
2902
+ return await this.vmManager.startExecution(e2, r2), t2 && await this.vmManager.setCurrentExecutionId(r2), {
2903
+ content: [{ type: "text", text: `Execution started: ${r2}${t2 ? " (set as current)" : ""}` }]
2144
2904
  };
2145
2905
  } catch (n2) {
2146
2906
  return {
@@ -2154,18 +2914,18 @@ class M {
2154
2914
  {
2155
2915
  executionId: zod.z.string().optional()
2156
2916
  },
2157
- async ({ executionId: r }) => {
2917
+ async ({ executionId: e2 }) => {
2158
2918
  try {
2159
- let e2 = r;
2160
- if (!e2) {
2919
+ let r2 = e2;
2920
+ if (!r2) {
2161
2921
  const n2 = await this.vmManager.getCurrentExecutionId();
2162
2922
  if (!n2)
2163
2923
  return {
2164
2924
  content: [{ type: "text", text: "No current execution set. Use list_executions to see available executions." }]
2165
2925
  };
2166
- e2 = n2;
2926
+ r2 = n2;
2167
2927
  }
2168
- const t2 = await this.vmManager.getNext(e2);
2928
+ const t2 = await this.vmManager.getNext(r2);
2169
2929
  return t2.type === "completed" ? {
2170
2930
  content: [{ type: "text", text: t2.result !== void 0 ? `Execution completed with result: ${JSON.stringify(t2.result)}` : "Execution completed" }]
2171
2931
  } : t2.type === "waiting" ? {
@@ -2177,9 +2937,9 @@ class M {
2177
2937
  content: [{ type: "text", text: "Unexpected state" }],
2178
2938
  isError: true
2179
2939
  };
2180
- } catch (e2) {
2940
+ } catch (r2) {
2181
2941
  return {
2182
- content: [{ type: "text", text: `Error: ${e2 instanceof Error ? e2.message : "Unknown error"}` }],
2942
+ content: [{ type: "text", text: `Error: ${r2 instanceof Error ? r2.message : "Unknown error"}` }],
2183
2943
  isError: true
2184
2944
  };
2185
2945
  }
@@ -2190,9 +2950,9 @@ class M {
2190
2950
  executionId: zod.z.string().optional(),
2191
2951
  result: zod.z.string()
2192
2952
  },
2193
- async ({ executionId: r, result: e2 }) => {
2953
+ async ({ executionId: e2, result: r2 }) => {
2194
2954
  try {
2195
- let t2 = r;
2955
+ let t2 = e2;
2196
2956
  if (!t2) {
2197
2957
  const n2 = await this.vmManager.getCurrentExecutionId();
2198
2958
  if (!n2)
@@ -2202,7 +2962,7 @@ class M {
2202
2962
  };
2203
2963
  t2 = n2;
2204
2964
  }
2205
- return await this.vmManager.reportCCResult(t2, e2), {
2965
+ return await this.vmManager.reportCCResult(t2, r2), {
2206
2966
  content: [{ type: "text", text: "Execution resumed" }]
2207
2967
  };
2208
2968
  } catch (t2) {
@@ -2217,25 +2977,25 @@ class M {
2217
2977
  {
2218
2978
  executionId: zod.z.string().optional()
2219
2979
  },
2220
- async ({ executionId: r }) => {
2980
+ async ({ executionId: e2 }) => {
2221
2981
  try {
2222
- let e2 = r;
2223
- if (!e2) {
2982
+ let r2 = e2;
2983
+ if (!r2) {
2224
2984
  const n2 = await this.vmManager.getCurrentExecutionId();
2225
2985
  if (!n2)
2226
2986
  return {
2227
2987
  content: [{ type: "text", text: "Error: No current execution set" }],
2228
2988
  isError: true
2229
2989
  };
2230
- e2 = n2;
2990
+ r2 = n2;
2231
2991
  }
2232
- const t2 = await this.vmManager.getExecutionStatus(e2);
2992
+ const t2 = await this.vmManager.getExecutionStatus(r2);
2233
2993
  return {
2234
2994
  content: [{ type: "text", text: JSON.stringify(t2, null, 2) }]
2235
2995
  };
2236
- } catch (e2) {
2996
+ } catch (r2) {
2237
2997
  return {
2238
- content: [{ type: "text", text: `Error: ${e2 instanceof Error ? e2.message : "Unknown error"}` }],
2998
+ content: [{ type: "text", text: `Error: ${r2 instanceof Error ? r2.message : "Unknown error"}` }],
2239
2999
  isError: true
2240
3000
  };
2241
3001
  }
@@ -2245,12 +3005,12 @@ class M {
2245
3005
  {},
2246
3006
  async () => {
2247
3007
  try {
2248
- const r = await this.vmManager.listExecutions(), e2 = await this.vmManager.getCurrentExecutionId(), t2 = r.map((n2) => ({
3008
+ const e2 = await this.vmManager.listExecutions(), r2 = await this.vmManager.getCurrentExecutionId(), t2 = e2.map((n2) => ({
2249
3009
  executionId: n2.id,
2250
3010
  programId: n2.programId,
2251
3011
  state: n2.state,
2252
3012
  created: n2.created,
2253
- isCurrent: n2.id === e2,
3013
+ isCurrent: n2.id === r2,
2254
3014
  summary: {
2255
3015
  currentPrompt: n2.state === "AWAITING_COGNITIVE_RESULT" ? n2.ccPrompt : void 0,
2256
3016
  tasksCompleted: 0
@@ -2260,9 +3020,9 @@ class M {
2260
3020
  return {
2261
3021
  content: [{ type: "text", text: JSON.stringify(t2, null, 2) }]
2262
3022
  };
2263
- } catch (r) {
3023
+ } catch (e2) {
2264
3024
  return {
2265
- content: [{ type: "text", text: `Error: ${r instanceof Error ? r.message : "Unknown error"}` }],
3025
+ content: [{ type: "text", text: `Error: ${e2 instanceof Error ? e2.message : "Unknown error"}` }],
2266
3026
  isError: true
2267
3027
  };
2268
3028
  }
@@ -2272,19 +3032,19 @@ class M {
2272
3032
  {
2273
3033
  executionId: zod.z.string().optional()
2274
3034
  },
2275
- async ({ executionId: r }) => {
3035
+ async ({ executionId: e2 }) => {
2276
3036
  try {
2277
- let e2 = r;
2278
- if (!e2) {
3037
+ let r2 = e2;
3038
+ if (!r2) {
2279
3039
  const a2 = await this.vmManager.getCurrentExecutionId();
2280
3040
  if (!a2)
2281
3041
  return {
2282
3042
  content: [{ type: "text", text: "Error: No current execution set" }],
2283
3043
  isError: true
2284
3044
  };
2285
- e2 = a2;
3045
+ r2 = a2;
2286
3046
  }
2287
- const t2 = await this.vmManager.getExecutionWithAttempts(e2), n2 = await this.vmManager.getCurrentExecutionId(), c2 = {
3047
+ const t2 = await this.vmManager.getExecutionWithAttempts(r2), n2 = await this.vmManager.getCurrentExecutionId(), c2 = {
2288
3048
  executionId: t2.id,
2289
3049
  programId: t2.programId,
2290
3050
  state: t2.state,
@@ -2305,9 +3065,9 @@ class M {
2305
3065
  return {
2306
3066
  content: [{ type: "text", text: JSON.stringify(c2, null, 2) }]
2307
3067
  };
2308
- } catch (e2) {
3068
+ } catch (r2) {
2309
3069
  return {
2310
- content: [{ type: "text", text: `Error: ${e2 instanceof Error ? e2.message : "Unknown error"}` }],
3070
+ content: [{ type: "text", text: `Error: ${r2 instanceof Error ? r2.message : "Unknown error"}` }],
2311
3071
  isError: true
2312
3072
  };
2313
3073
  }
@@ -2317,14 +3077,14 @@ class M {
2317
3077
  {
2318
3078
  executionId: zod.z.string()
2319
3079
  },
2320
- async ({ executionId: r }) => {
3080
+ async ({ executionId: e2 }) => {
2321
3081
  try {
2322
- return await this.vmManager.setCurrentExecutionId(r), {
2323
- content: [{ type: "text", text: `Current execution set to: ${r}` }]
3082
+ return await this.vmManager.setCurrentExecutionId(e2), {
3083
+ content: [{ type: "text", text: `Current execution set to: ${e2}` }]
2324
3084
  };
2325
- } catch (e2) {
3085
+ } catch (r2) {
2326
3086
  return {
2327
- content: [{ type: "text", text: `Error: ${e2 instanceof Error ? e2.message : "Unknown error"}` }],
3087
+ content: [{ type: "text", text: `Error: ${r2 instanceof Error ? r2.message : "Unknown error"}` }],
2328
3088
  isError: true
2329
3089
  };
2330
3090
  }
@@ -2335,11 +3095,11 @@ class M {
2335
3095
  executionId: zod.z.string(),
2336
3096
  confirmToken: zod.z.string().optional()
2337
3097
  },
2338
- async ({ executionId: r, confirmToken: e2 }) => {
3098
+ async ({ executionId: e2, confirmToken: r2 }) => {
2339
3099
  try {
2340
- const t2 = `delete-${r}-${Date.now()}`;
2341
- return e2 ? e2 && e2.startsWith(`delete-${r}-`) ? (await this.vmManager.deleteExecution(r), {
2342
- content: [{ type: "text", text: `Execution deleted: ${r}` }]
3100
+ const t2 = `delete-${e2}-${Date.now()}`;
3101
+ return r2 ? r2 && r2.startsWith(`delete-${e2}-`) ? (await this.vmManager.deleteExecution(e2), {
3102
+ content: [{ type: "text", text: `Execution deleted: ${e2}` }]
2343
3103
  }) : {
2344
3104
  content: [{ type: "text", text: "Invalid confirmation token" }],
2345
3105
  isError: true
@@ -2348,7 +3108,7 @@ class M {
2348
3108
  type: "text",
2349
3109
  text: JSON.stringify({
2350
3110
  confirmationRequired: true,
2351
- message: `To delete execution '${r}', call this tool again with the confirmation token`,
3111
+ message: `To delete execution '${e2}', call this tool again with the confirmation token`,
2352
3112
  token: t2
2353
3113
  }, null, 2)
2354
3114
  }]
@@ -2360,10 +3120,84 @@ class M {
2360
3120
  };
2361
3121
  }
2362
3122
  }
3123
+ ), this.server.tool(
3124
+ "list_programs",
3125
+ {},
3126
+ async () => {
3127
+ try {
3128
+ const e2 = await this.vmManager.listPrograms();
3129
+ if (e2.length === 0)
3130
+ return {
3131
+ content: [{ type: "text", text: "No programs loaded" }]
3132
+ };
3133
+ const r2 = e2.map((t2) => ({
3134
+ programId: t2.id,
3135
+ name: t2.name,
3136
+ created: t2.created
3137
+ }));
3138
+ return {
3139
+ content: [{ type: "text", text: JSON.stringify(r2, null, 2) }]
3140
+ };
3141
+ } catch (e2) {
3142
+ return {
3143
+ content: [{ type: "text", text: `Error: ${e2 instanceof Error ? e2.message : "Unknown error"}` }],
3144
+ isError: true
3145
+ };
3146
+ }
3147
+ }
3148
+ ), this.server.tool(
3149
+ "delete_program",
3150
+ {
3151
+ programId: zod.z.string(),
3152
+ confirmToken: zod.z.string().optional()
3153
+ },
3154
+ async ({ programId: e2, confirmToken: r2 }) => {
3155
+ try {
3156
+ const t2 = `delete-${e2}-${Date.now()}`;
3157
+ return r2 ? r2 && r2.startsWith(`delete-${e2}-`) ? (await this.vmManager.deleteProgram(e2), {
3158
+ content: [{ type: "text", text: `Program deleted: ${e2}` }]
3159
+ }) : {
3160
+ content: [{ type: "text", text: "Invalid confirmation token" }],
3161
+ isError: true
3162
+ } : {
3163
+ content: [{
3164
+ type: "text",
3165
+ text: JSON.stringify({
3166
+ confirmationRequired: true,
3167
+ message: `To delete program '${e2}', call this tool again with the confirmation token`,
3168
+ token: t2
3169
+ }, null, 2)
3170
+ }]
3171
+ };
3172
+ } catch (t2) {
3173
+ return {
3174
+ content: [{ type: "text", text: `Error: ${t2 instanceof Error ? t2.message : "Unknown error"}` }],
3175
+ isError: true
3176
+ };
3177
+ }
3178
+ }
3179
+ ), this.server.tool(
3180
+ "restart",
3181
+ {
3182
+ programId: zod.z.string(),
3183
+ executionId: zod.z.string().optional()
3184
+ },
3185
+ async ({ programId: e2, executionId: r2 }) => {
3186
+ try {
3187
+ return {
3188
+ content: [{ type: "text", text: `Execution started: ${await this.vmManager.restartExecution(e2, r2)} (set as current)` }]
3189
+ };
3190
+ } catch (t2) {
3191
+ return {
3192
+ content: [{ type: "text", text: `Error: ${t2 instanceof Error ? t2.message : "Unknown error"}` }],
3193
+ isError: true
3194
+ };
3195
+ }
3196
+ }
2363
3197
  );
2364
3198
  }
2365
- async start(r) {
2366
- await this.vmManager.initialize(), this.transport = r || new stdio_js.StdioServerTransport(), await this.server.connect(this.transport);
3199
+ async start(e2) {
3200
+ await this.vmManager.initialize(), this.transport = e2 || new stdio_js.StdioServerTransport(), await this.server.connect(this.transport);
2367
3201
  }
2368
3202
  async stop() {
2369
3203
  this.transport && (await this.transport.close(), this.transport = null), await this.vmManager.dispose();
@@ -2373,7 +3207,7 @@ class M {
2373
3207
  return this.vmManager;
2374
3208
  }
2375
3209
  }
2376
- dotenv__namespace.config({ path: c$1.resolve(__dirname, "../../../.env") });
3210
+ dotenv__namespace.config({ path: r.resolve(__dirname, "../../../.env") });
2377
3211
  function loadConfig() {
2378
3212
  const env = process.env.NODE_ENV || "development";
2379
3213
  const storageType = process.env.CVM_STORAGE_TYPE || "file";
@@ -2454,7 +3288,7 @@ function getLogger() {
2454
3288
  return logger;
2455
3289
  }
2456
3290
  const __filename$1 = url.fileURLToPath(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === "SCRIPT" && _documentCurrentScript.src || new URL("main.cjs", document.baseURI).href);
2457
- const __dirname$1 = c$1.dirname(__filename$1);
3291
+ const __dirname$1 = r.dirname(__filename$1);
2458
3292
  async function main() {
2459
3293
  let cvmServer;
2460
3294
  try {
@@ -2463,16 +3297,16 @@ async function main() {
2463
3297
  const logger2 = getLogger();
2464
3298
  let version = "0.4.3";
2465
3299
  const possiblePaths = [
2466
- c$1.join(__dirname$1, "..", "package.json"),
3300
+ r.join(__dirname$1, "..", "package.json"),
2467
3301
  // Development
2468
- c$1.join(__dirname$1, "package.json"),
3302
+ r.join(__dirname$1, "package.json"),
2469
3303
  // Bundled dist
2470
- c$1.join(process.cwd(), "package.json")
3304
+ r.join(process.cwd(), "package.json")
2471
3305
  // Current directory
2472
3306
  ];
2473
3307
  for (const packageJsonPath of possiblePaths) {
2474
3308
  try {
2475
- const packageJson = JSON.parse(M$2.readFileSync(packageJsonPath, "utf-8"));
3309
+ const packageJson = JSON.parse(b$2.readFileSync(packageJsonPath, "utf-8"));
2476
3310
  if (packageJson.name === "cvm-server" && packageJson.version) {
2477
3311
  version = packageJson.version;
2478
3312
  break;
@@ -2487,13 +3321,13 @@ async function main() {
2487
3321
  });
2488
3322
  if (config.storage.type === "file") {
2489
3323
  const dataDir = config.storage.dataDir || ".cvm";
2490
- const fullPath = c$1.resolve(process.cwd(), dataDir);
3324
+ const fullPath = r.resolve(process.cwd(), dataDir);
2491
3325
  logger2.info(`[CVM] Initializing file storage in: ${fullPath}`);
2492
3326
  logger2.warn(`[CVM] ⚠️ Remember to add '${dataDir}/' to your .gitignore file!`);
2493
3327
  } else {
2494
3328
  logger2.info("[CVM] Using MongoDB storage");
2495
3329
  }
2496
- cvmServer = new M(version);
3330
+ cvmServer = new $(version);
2497
3331
  await cvmServer.start();
2498
3332
  logger2.info("CVM Server is running and ready to accept MCP connections");
2499
3333
  process.on("SIGINT", async () => {