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