cvm-server
Version:
Cognitive Virtual Machine (CVM) - A deterministic bytecode VM with AI cognitive operations
1,296 lines • 67.9 kB
JavaScript
"use strict";
const mcp_js = require("@modelcontextprotocol/sdk/server/mcp.js");
const stdio_js = require("@modelcontextprotocol/sdk/server/stdio.js");
const zod = require("zod");
const s$2 = require("typescript");
const M = require("fs");
const c$2 = require("path");
const mongodb = require("mongodb");
const promises = require("fs/promises");
const dotenv = require("dotenv");
const url = require("url");
var _documentCurrentScript = typeof document !== "undefined" ? document.currentScript : null;
function _interopNamespaceDefault(e2) {
const n2 = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
if (e2) {
for (const k2 in e2) {
if (k2 !== "default") {
const d2 = Object.getOwnPropertyDescriptor(e2, k2);
Object.defineProperty(n2, k2, d2.get ? d2 : {
enumerable: true,
get: () => e2[k2]
});
}
}
}
n2.default = e2;
return Object.freeze(n2);
}
const s__namespace = /* @__PURE__ */ _interopNamespaceDefault(s$2);
const M__namespace = /* @__PURE__ */ _interopNamespaceDefault(M);
const c__namespace = /* @__PURE__ */ _interopNamespaceDefault(c$2);
const dotenv__namespace = /* @__PURE__ */ _interopNamespaceDefault(dotenv);
var R = Object.defineProperty;
var h$1 = (n2, l2, e2) => l2 in n2 ? R(n2, l2, { enumerable: true, configurable: true, writable: true, value: e2 }) : n2[l2] = e2;
var u$1 = (n2, l2, e2) => h$1(n2, typeof l2 != "symbol" ? l2 + "" : l2, e2);
var r$1 = /* @__PURE__ */ ((n2) => (n2.PUSH = "PUSH", n2.PUSH_UNDEFINED = "PUSH_UNDEFINED", n2.POP = "POP", n2.LOAD = "LOAD", n2.STORE = "STORE", n2.CONCAT = "CONCAT", n2.ARRAY_NEW = "ARRAY_NEW", n2.ARRAY_PUSH = "ARRAY_PUSH", n2.ARRAY_GET = "ARRAY_GET", n2.ARRAY_SET = "ARRAY_SET", n2.ARRAY_LEN = "ARRAY_LEN", n2.STRING_LEN = "STRING_LEN", n2.STRING_SUBSTRING = "STRING_SUBSTRING", n2.STRING_INDEXOF = "STRING_INDEXOF", n2.STRING_SPLIT = "STRING_SPLIT", n2.STRING_SLICE = "STRING_SLICE", n2.STRING_CHARAT = "STRING_CHARAT", n2.STRING_TOUPPERCASE = "STRING_TOUPPERCASE", n2.STRING_TOLOWERCASE = "STRING_TOLOWERCASE", n2.LENGTH = "LENGTH", n2.JSON_PARSE = "JSON_PARSE", n2.TYPEOF = "TYPEOF", n2.ADD = "ADD", n2.SUB = "SUB", n2.MUL = "MUL", n2.DIV = "DIV", n2.MOD = "MOD", n2.UNARY_MINUS = "UNARY_MINUS", n2.UNARY_PLUS = "UNARY_PLUS", n2.INC = "INC", n2.DEC = "DEC", n2.EQ = "EQ", n2.NEQ = "NEQ", n2.LT = "LT", n2.GT = "GT", n2.LTE = "LTE", n2.GTE = "GTE", n2.EQ_STRICT = "EQ_STRICT", n2.NEQ_STRICT = "NEQ_STRICT", n2.JUMP = "JUMP", n2.JUMP_IF = "JUMP_IF", n2.JUMP_IF_FALSE = "JUMP_IF_FALSE", n2.JUMP_IF_TRUE = "JUMP_IF_TRUE", n2.CALL = "CALL", n2.RETURN = "RETURN", n2.AND = "AND", n2.OR = "OR", n2.NOT = "NOT", n2.BREAK = "BREAK", n2.CONTINUE = "CONTINUE", n2.ITER_START = "ITER_START", n2.ITER_NEXT = "ITER_NEXT", n2.ITER_END = "ITER_END", n2.FS_LIST_FILES = "FS_LIST_FILES", n2.CC = "CC", n2.PRINT = "PRINT", n2.HALT = "HALT", n2))(r$1 || {});
function I(n2) {
const l2 = [], e2 = [];
let o2 = false, T = false;
const a2 = s__namespace.createSourceFile(
"program.ts",
n2,
s__namespace.ScriptTarget.Latest,
true
);
function t2(i2) {
if (s__namespace.isFunctionDeclaration(i2) && i2.name && i2.name.text === "main" && (o2 = true, i2.parameters.length > 0 && l2.push("main() must not have parameters")), s__namespace.isExpressionStatement(i2) && i2.parent === a2) {
const c2 = i2.expression;
s__namespace.isCallExpression(c2) && s__namespace.isIdentifier(c2.expression) && c2.expression.text === "main" && (T = true);
}
if (s__namespace.isCallExpression(i2) && s__namespace.isIdentifier(i2.expression)) {
const c2 = i2.expression.text;
["setTimeout", "fetch", "require", "import"].includes(c2) && l2.push(`Unsupported function: ${c2}`);
}
s__namespace.forEachChild(i2, t2);
}
return t2(a2), o2 || l2.push("Program must have a main() function"), o2 && !T && l2.push("main() must be called at the top level"), l2.length === 0 && e2.push({ op: r$1.HALT }), {
bytecode: e2,
errors: l2,
hasMain: o2
};
}
let N$1 = class N {
constructor() {
u$1(this, "bytecode", []);
u$1(this, "contextStack", []);
}
/**
* Emit an instruction and return its index
*/
emit(l2, e2) {
const o2 = this.bytecode.length;
return this.bytecode.push({ op: l2, arg: e2 }), o2;
}
/**
* Get the current address (next instruction index)
*/
currentAddress() {
return this.bytecode.length;
}
/**
* Patch a jump instruction with the target address
*/
patchJump(l2, e2) {
l2 >= 0 && l2 < this.bytecode.length && (this.bytecode[l2].arg = e2);
}
/**
* Patch multiple jump instructions with the same target
*/
patchJumps(l2, e2) {
l2.forEach((o2) => this.patchJump(o2, e2));
}
/**
* Push a new jump context onto the stack
*/
pushContext(l2) {
this.contextStack.push(l2);
}
/**
* Pop the current jump context from the stack
*/
popContext() {
return this.contextStack.pop() || null;
}
/**
* Get the current context without removing it
*/
getCurrentContext() {
return this.contextStack.length > 0 ? this.contextStack[this.contextStack.length - 1] : null;
}
/**
* Find the nearest loop context (for break/continue)
*/
findLoopContext() {
for (let l2 = this.contextStack.length - 1; l2 >= 0; l2--)
if (this.contextStack[l2].type === "loop" || this.contextStack[l2].type === "foreach")
return this.contextStack[l2];
return null;
}
/**
* Get the final bytecode array
*/
getBytecode() {
return this.bytecode;
}
/**
* Check if compilation is in a valid state
*/
isValid() {
return this.contextStack.length === 0;
}
/**
* Get any unclosed contexts (for error reporting)
*/
getUnclosedContexts() {
return [...this.contextStack];
}
};
function k(n2) {
const l2 = I(n2);
if (l2.errors.length > 0)
return {
success: false,
bytecode: [],
errors: l2.errors
};
const e2 = new N$1(), o2 = s__namespace.createSourceFile("program.ts", n2, s__namespace.ScriptTarget.Latest, true);
function T(t2) {
if (s__namespace.isIfStatement(t2)) {
a2(t2.expression);
const i2 = e2.emit(r$1.JUMP_IF_FALSE, -1), c2 = {
type: "if",
endTargets: []
};
if (t2.elseStatement ? c2.elseTarget = i2 : c2.endTargets.push(i2), e2.pushContext(c2), T(t2.thenStatement), t2.elseStatement) {
const x = e2.emit(r$1.JUMP, -1);
c2.endTargets.push(x);
const S = e2.currentAddress();
e2.patchJump(i2, S), T(t2.elseStatement);
}
const m2 = e2.popContext();
if (m2) {
const x = e2.currentAddress();
e2.patchJumps(m2.endTargets, x);
}
} else if (s__namespace.isWhileStatement(t2)) {
const i2 = e2.currentAddress();
a2(t2.expression);
const m2 = {
type: "loop",
breakTargets: [e2.emit(r$1.JUMP_IF_FALSE, -1)],
continueTargets: [],
endTargets: [],
startAddress: i2
};
e2.pushContext(m2), T(t2.statement), e2.emit(r$1.JUMP, i2);
const x = e2.popContext();
if (x) {
const S = e2.currentAddress();
e2.patchJumps(x.breakTargets || [], S);
}
} else if (s__namespace.isForOfStatement(t2)) {
const i2 = t2.initializer, c2 = t2.expression;
let m2;
if (s__namespace.isVariableDeclarationList(i2))
m2 = i2.declarations[0].name.getText();
else if (s__namespace.isIdentifier(i2))
m2 = i2.text;
else
throw new Error("Unsupported for-of variable declaration");
a2(c2), e2.emit(r$1.ITER_START);
const x = e2.currentAddress();
e2.emit(r$1.ITER_NEXT);
const S = e2.emit(r$1.JUMP_IF_FALSE, -1);
e2.emit(r$1.STORE, m2);
const A2 = {
type: "foreach",
breakTargets: [S],
continueTargets: [],
endTargets: [],
startAddress: x,
iterVariable: m2
};
e2.pushContext(A2), T(t2.statement), e2.emit(r$1.JUMP, x);
const f2 = e2.popContext();
if (f2) {
e2.emit(r$1.ITER_END);
const p2 = e2.currentAddress();
e2.patchJumps(f2.breakTargets || [], p2);
}
} else if (s__namespace.isBlock(t2))
t2.statements.forEach((i2) => {
T(i2);
});
else if (s__namespace.isExpressionStatement(t2)) {
const i2 = t2.expression;
if (s__namespace.isBinaryExpression(i2)) {
const c2 = i2.operatorToken.kind;
if (c2 === s__namespace.SyntaxKind.PlusEqualsToken || c2 === s__namespace.SyntaxKind.MinusEqualsToken || c2 === s__namespace.SyntaxKind.AsteriskEqualsToken || c2 === s__namespace.SyntaxKind.SlashEqualsToken || c2 === s__namespace.SyntaxKind.PercentEqualsToken) {
if (s__namespace.isIdentifier(i2.left)) {
switch (e2.emit(r$1.LOAD, i2.left.text), a2(i2.right), c2) {
case s__namespace.SyntaxKind.PlusEqualsToken:
s__namespace.isStringLiteral(i2.right) ? e2.emit(r$1.CONCAT) : e2.emit(r$1.ADD);
break;
case s__namespace.SyntaxKind.MinusEqualsToken:
e2.emit(r$1.SUB);
break;
case s__namespace.SyntaxKind.AsteriskEqualsToken:
e2.emit(r$1.MUL);
break;
case s__namespace.SyntaxKind.SlashEqualsToken:
e2.emit(r$1.DIV);
break;
case s__namespace.SyntaxKind.PercentEqualsToken:
e2.emit(r$1.MOD);
break;
}
e2.emit(r$1.STORE, i2.left.text);
} else if (s__namespace.isElementAccessExpression(i2.left))
throw new Error("Compound assignment to array elements not yet supported");
return;
} else if (c2 === s__namespace.SyntaxKind.EqualsToken) {
if (a2(i2.right), s__namespace.isIdentifier(i2.left))
e2.emit(r$1.STORE, i2.left.text);
else if (s__namespace.isElementAccessExpression(i2.left)) {
const m2 = `__temp_${e2.getBytecode().length}`;
e2.emit(r$1.STORE, m2), a2(i2.left.expression), a2(i2.left.argumentExpression), e2.emit(r$1.LOAD, m2), e2.emit(r$1.ARRAY_SET);
}
return;
}
}
s__namespace.isCallExpression(i2) && s__namespace.isPropertyAccessExpression(i2.expression) && i2.expression.expression.getText() === "console" && i2.expression.name.getText() === "log" ? (i2.arguments.forEach((c2) => {
a2(c2);
}), e2.emit(r$1.PRINT)) : s__namespace.isCallExpression(i2) && s__namespace.isIdentifier(i2.expression) && i2.expression.text === "CC" ? (i2.arguments.length > 0 && a2(i2.arguments[0]), e2.emit(r$1.CC), e2.emit(r$1.POP)) : s__namespace.isCallExpression(i2) && s__namespace.isPropertyAccessExpression(i2.expression) && i2.expression.name.getText() === "push" ? (a2(i2.expression.expression), i2.arguments.length > 0 && a2(i2.arguments[0]), e2.emit(r$1.ARRAY_PUSH)) : (s__namespace.isPostfixUnaryExpression(i2) || s__namespace.isPrefixUnaryExpression(i2)) && (a2(i2), e2.emit(r$1.POP));
} else if (s__namespace.isVariableStatement(t2)) {
const i2 = t2.declarationList.declarations[0];
i2.initializer && (a2(i2.initializer), e2.emit(r$1.STORE, i2.name.getText()));
} else if (s__namespace.isReturnStatement(t2))
t2.expression ? a2(t2.expression) : e2.emit(r$1.PUSH, null), e2.emit(r$1.RETURN);
else if (s__namespace.isBreakStatement(t2)) {
const i2 = e2.findLoopContext();
if (i2) {
i2.type === "foreach" && e2.emit(r$1.ITER_END);
const c2 = e2.emit(r$1.BREAK, -1);
i2.breakTargets = i2.breakTargets || [], i2.breakTargets.push(c2);
} else
throw new Error("break statement not in loop");
} else if (s__namespace.isContinueStatement(t2)) {
const i2 = e2.findLoopContext();
if (i2 && i2.startAddress !== void 0) {
const c2 = e2.emit(r$1.CONTINUE, i2.startAddress);
i2.continueTargets = i2.continueTargets || [], i2.continueTargets.push(c2);
} else
throw new Error("continue statement not in loop");
}
}
function a2(t2) {
if (s__namespace.isStringLiteral(t2))
e2.emit(r$1.PUSH, t2.text);
else if (s__namespace.isNumericLiteral(t2))
e2.emit(r$1.PUSH, Number(t2.text));
else if (t2.kind === s__namespace.SyntaxKind.TrueKeyword)
e2.emit(r$1.PUSH, true);
else if (t2.kind === s__namespace.SyntaxKind.FalseKeyword)
e2.emit(r$1.PUSH, false);
else if (t2.kind === s__namespace.SyntaxKind.NullKeyword)
e2.emit(r$1.PUSH, null);
else if (s__namespace.isArrayLiteralExpression(t2))
e2.emit(r$1.ARRAY_NEW), t2.elements.forEach((i2) => {
a2(i2), e2.emit(r$1.ARRAY_PUSH);
});
else if (s__namespace.isElementAccessExpression(t2))
a2(t2.expression), t2.argumentExpression && a2(t2.argumentExpression), e2.emit(r$1.ARRAY_GET);
else if (s__namespace.isPropertyAccessExpression(t2) && t2.name.text === "length")
a2(t2.expression), e2.emit(r$1.LENGTH);
else if (s__namespace.isIdentifier(t2))
t2.text === "undefined" ? e2.emit(r$1.PUSH_UNDEFINED) : e2.emit(r$1.LOAD, t2.text);
else if (s__namespace.isCallExpression(t2)) {
if (s__namespace.isPropertyAccessExpression(t2.expression) && s__namespace.isIdentifier(t2.expression.expression) && t2.expression.expression.text === "fs" && t2.expression.name.text === "listFiles")
t2.arguments.length > 0 ? a2(t2.arguments[0]) : e2.emit(r$1.PUSH, "."), t2.arguments.length > 1 && a2(t2.arguments[1]), e2.emit(r$1.FS_LIST_FILES);
else if (s__namespace.isPropertyAccessExpression(t2.expression) && s__namespace.isIdentifier(t2.expression.expression) && t2.expression.expression.text === "JSON" && t2.expression.name.text === "parse")
t2.arguments.length > 0 && a2(t2.arguments[0]), e2.emit(r$1.JSON_PARSE);
else if (s__namespace.isIdentifier(t2.expression) && t2.expression.text === "CC")
t2.arguments.length > 0 && a2(t2.arguments[0]), e2.emit(r$1.CC);
else if (s__namespace.isPropertyAccessExpression(t2.expression)) {
const i2 = t2.expression.name.text;
i2 === "substring" ? (a2(t2.expression.expression), t2.arguments.length > 0 ? a2(t2.arguments[0]) : e2.emit(r$1.PUSH, 0), t2.arguments.length > 1 && a2(t2.arguments[1]), e2.emit(r$1.STRING_SUBSTRING)) : i2 === "indexOf" ? (a2(t2.expression.expression), t2.arguments.length > 0 ? a2(t2.arguments[0]) : e2.emit(r$1.PUSH, ""), e2.emit(r$1.STRING_INDEXOF)) : i2 === "split" ? (a2(t2.expression.expression), t2.arguments.length > 0 ? a2(t2.arguments[0]) : e2.emit(r$1.PUSH, ""), e2.emit(r$1.STRING_SPLIT)) : i2 === "slice" ? (a2(t2.expression.expression), t2.arguments.length > 0 ? a2(t2.arguments[0]) : e2.emit(r$1.PUSH, 0), t2.arguments.length > 1 && a2(t2.arguments[1]), e2.emit(r$1.STRING_SLICE)) : i2 === "charAt" ? (a2(t2.expression.expression), t2.arguments.length > 0 ? a2(t2.arguments[0]) : e2.emit(r$1.PUSH, 0), e2.emit(r$1.STRING_CHARAT)) : i2 === "toUpperCase" ? (a2(t2.expression.expression), e2.emit(r$1.STRING_TOUPPERCASE)) : i2 === "toLowerCase" && (a2(t2.expression.expression), e2.emit(r$1.STRING_TOLOWERCASE));
}
} else if (s__namespace.isParenthesizedExpression(t2))
a2(t2.expression);
else if (s__namespace.isBinaryExpression(t2)) {
const i2 = t2.operatorToken.kind;
switch (a2(t2.left), a2(t2.right), i2) {
case s__namespace.SyntaxKind.PlusToken:
E(t2.left, t2.right) ? e2.emit(r$1.CONCAT) : e2.emit(r$1.ADD);
break;
case s__namespace.SyntaxKind.MinusToken:
e2.emit(r$1.SUB);
break;
case s__namespace.SyntaxKind.AsteriskToken:
e2.emit(r$1.MUL);
break;
case s__namespace.SyntaxKind.SlashToken:
e2.emit(r$1.DIV);
break;
case s__namespace.SyntaxKind.PercentToken:
e2.emit(r$1.MOD);
break;
case s__namespace.SyntaxKind.EqualsEqualsToken:
e2.emit(r$1.EQ);
break;
case s__namespace.SyntaxKind.ExclamationEqualsToken:
e2.emit(r$1.NEQ);
break;
case s__namespace.SyntaxKind.LessThanToken:
e2.emit(r$1.LT);
break;
case s__namespace.SyntaxKind.GreaterThanToken:
e2.emit(r$1.GT);
break;
case s__namespace.SyntaxKind.LessThanEqualsToken:
e2.emit(r$1.LTE);
break;
case s__namespace.SyntaxKind.GreaterThanEqualsToken:
e2.emit(r$1.GTE);
break;
case s__namespace.SyntaxKind.EqualsEqualsEqualsToken:
e2.emit(r$1.EQ_STRICT);
break;
case s__namespace.SyntaxKind.ExclamationEqualsEqualsToken:
e2.emit(r$1.NEQ_STRICT);
break;
case s__namespace.SyntaxKind.AmpersandAmpersandToken:
e2.emit(r$1.AND);
break;
case s__namespace.SyntaxKind.BarBarToken:
e2.emit(r$1.OR);
break;
}
} else if (s__namespace.isPrefixUnaryExpression(t2))
switch (t2.operator) {
case s__namespace.SyntaxKind.ExclamationToken:
a2(t2.operand), e2.emit(r$1.NOT);
break;
case s__namespace.SyntaxKind.MinusToken:
a2(t2.operand), e2.emit(r$1.UNARY_MINUS);
break;
case s__namespace.SyntaxKind.PlusToken:
a2(t2.operand), e2.emit(r$1.UNARY_PLUS);
break;
case s__namespace.SyntaxKind.PlusPlusToken:
s__namespace.isIdentifier(t2.operand) && (e2.emit(r$1.PUSH, t2.operand.text), e2.emit(r$1.INC, false));
break;
case s__namespace.SyntaxKind.MinusMinusToken:
s__namespace.isIdentifier(t2.operand) && (e2.emit(r$1.PUSH, t2.operand.text), e2.emit(r$1.DEC, false));
break;
}
else if (s__namespace.isPostfixUnaryExpression(t2))
switch (t2.operator) {
case s__namespace.SyntaxKind.PlusPlusToken:
s__namespace.isIdentifier(t2.operand) && (e2.emit(r$1.PUSH, t2.operand.text), e2.emit(r$1.INC, true));
break;
case s__namespace.SyntaxKind.MinusMinusToken:
s__namespace.isIdentifier(t2.operand) && (e2.emit(r$1.PUSH, t2.operand.text), e2.emit(r$1.DEC, true));
break;
}
else if (s__namespace.isTypeOfExpression(t2))
a2(t2.expression), e2.emit(r$1.TYPEOF);
else if (s__namespace.isConditionalExpression(t2)) {
a2(t2.condition);
const i2 = e2.emit(r$1.JUMP_IF_FALSE, -1);
a2(t2.whenTrue);
const c2 = e2.emit(r$1.JUMP, -1), m2 = e2.currentAddress();
e2.patchJump(i2, m2), a2(t2.whenFalse);
const x = e2.currentAddress();
e2.patchJump(c2, x);
}
}
return o2.forEachChild((t2) => {
var i2;
s__namespace.isFunctionDeclaration(t2) && ((i2 = t2.name) == null ? void 0 : i2.text) === "main" && t2.body && t2.body.statements.forEach((c2) => {
T(c2);
});
}), e2.emit(r$1.HALT), {
success: true,
bytecode: e2.getBytecode(),
errors: []
};
}
function E(n2, l2) {
return !!(s__namespace.isStringLiteral(n2) || s__namespace.isStringLiteral(l2) || s__namespace.isBinaryExpression(n2) && n2.operatorToken.kind === s__namespace.SyntaxKind.PlusToken && E(n2.left, n2.right) || s__namespace.isBinaryExpression(l2) && l2.operatorToken.kind === s__namespace.SyntaxKind.PlusToken && E(l2.left, l2.right));
}
function n(r2) {
return typeof r2 == "string";
}
function t(r2) {
return typeof r2 == "number";
}
function i(r2) {
return typeof r2 == "boolean";
}
function f(r2) {
return r2 === null;
}
function e(r2) {
return r2 !== null && typeof r2 == "object" && "type" in r2 && r2.type === "array";
}
function o(r2) {
return r2 !== null && typeof r2 == "object" && "type" in r2 && r2.type === "undefined";
}
function c$1(r2) {
return n(r2) ? r2 : t(r2) || i(r2) ? r2.toString() : f(r2) ? "null" : o(r2) ? "undefined" : e(r2) ? `[array:${r2.elements.length}]` : String(r2);
}
function y(r2) {
return i(r2) ? r2 : f(r2) || o(r2) ? false : t(r2) ? r2 !== 0 : n(r2) ? r2 !== "" : e(r2) ? true : !!r2;
}
function m(r2) {
return n(r2) ? "string" : t(r2) ? "number" : i(r2) ? "boolean" : f(r2) ? "null" : o(r2) ? "undefined" : e(r2) ? "array" : "unknown";
}
function d(r2) {
if (t(r2)) return r2;
if (i(r2)) return r2 ? 1 : 0;
if (f(r2)) return 0;
if (o(r2)) return NaN;
if (n(r2)) {
const u2 = r2.trim();
return u2 === "" ? 0 : Number(u2);
}
return e(r2) ? NaN : Number(r2);
}
function p$1(r2 = []) {
return { type: "array", elements: r2 };
}
function s$1() {
return { type: "undefined" };
}
var l = Object.defineProperty;
var u = (s2, t2, n2) => t2 in s2 ? l(s2, t2, { enumerable: true, configurable: true, writable: true, value: n2 }) : s2[t2] = n2;
var r = (s2, t2, n2) => u(s2, typeof t2 != "symbol" ? t2 + "" : t2, n2);
class h {
constructor(t2) {
r(this, "connected", false);
r(this, "programsDir");
r(this, "executionsDir");
r(this, "outputsDir");
this.dataDir = t2, this.programsDir = c__namespace.join(t2, "programs"), this.executionsDir = c__namespace.join(t2, "executions"), this.outputsDir = c__namespace.join(t2, "outputs");
}
async connect() {
await M.promises.mkdir(this.dataDir, { recursive: true }), await M.promises.mkdir(this.programsDir, { recursive: true }), await M.promises.mkdir(this.executionsDir, { recursive: true }), await M.promises.mkdir(this.outputsDir, { recursive: true }), this.connected = true;
}
async disconnect() {
this.connected = false;
}
isConnected() {
return this.connected;
}
async saveProgram(t2) {
if (!this.connected) throw new Error("Not connected");
const n2 = c__namespace.join(this.programsDir, `${t2.id}.json`), e2 = JSON.stringify(t2, null, 2);
await M.promises.writeFile(n2, e2, "utf-8");
}
async getProgram(t2) {
if (!this.connected) throw new Error("Not connected");
const n2 = c__namespace.join(this.programsDir, `${t2}.json`);
try {
const e2 = await M.promises.readFile(n2, "utf-8"), o2 = JSON.parse(e2);
return o2.created = new Date(o2.created), o2.updated && (o2.updated = new Date(o2.updated)), o2;
} catch (e2) {
if (e2.code === "ENOENT")
return null;
throw e2;
}
}
async saveExecution(t2) {
if (!this.connected) throw new Error("Not connected");
const n2 = c__namespace.join(this.executionsDir, `${t2.id}.json`), e2 = JSON.stringify(t2, null, 2);
await M.promises.writeFile(n2, e2, "utf-8");
}
async getExecution(t2) {
if (!this.connected) throw new Error("Not connected");
const n2 = c__namespace.join(this.executionsDir, `${t2}.json`);
try {
const e2 = await M.promises.readFile(n2, "utf-8"), o2 = JSON.parse(e2);
return o2.created = new Date(o2.created), o2.updated && (o2.updated = new Date(o2.updated)), o2;
} catch (e2) {
if (e2.code === "ENOENT")
return null;
throw e2;
}
}
async appendOutput(t2, n2) {
if (!this.connected) throw new Error("Not connected");
const e2 = c__namespace.join(this.outputsDir, `${t2}.output`), o2 = n2.join(`
`) + `
`;
await M.promises.appendFile(e2, o2, "utf-8");
}
async getOutput(t2) {
if (!this.connected) throw new Error("Not connected");
const n2 = c__namespace.join(this.outputsDir, `${t2}.output`);
try {
return (await M.promises.readFile(n2, "utf-8")).split(`
`).filter((o2) => o2.length > 0);
} catch (e2) {
if (e2.code === "ENOENT")
return [];
throw e2;
}
}
}
class p {
constructor(t2) {
r(this, "client");
r(this, "db", null);
r(this, "connected", false);
this.connectionString = t2, this.client = new mongodb.MongoClient(t2);
}
async connect() {
var o2;
await this.client.connect();
const t2 = ((o2 = this.connectionString.split("/").pop()) == null ? void 0 : o2.split("?")[0]) || "cvm";
this.db = this.client.db(t2), this.connected = true;
const e2 = (await this.db.listCollections().toArray()).map((a2) => a2.name);
e2.includes("programs") || await this.db.createCollection("programs"), e2.includes("executions") || await this.db.createCollection("executions"), e2.includes("outputs") || await this.db.createCollection("outputs");
}
async disconnect() {
await this.client.close(), this.connected = false, this.db = null;
}
isConnected() {
return this.connected;
}
async getCollections() {
if (!this.db) throw new Error("Not connected to database");
return (await this.db.listCollections().toArray()).map((n2) => n2.name);
}
getCollection(t2) {
if (!this.db) throw new Error("Not connected to database");
return this.db.collection(t2);
}
async saveProgram(t2) {
await this.getCollection("programs").replaceOne(
{ id: t2.id },
t2,
{ upsert: true }
);
}
async getProgram(t2) {
return await this.getCollection("programs").findOne({ id: t2 });
}
async saveExecution(t2) {
await this.getCollection("executions").replaceOne(
{ id: t2.id },
t2,
{ upsert: true }
);
}
async getExecution(t2) {
return await this.getCollection("executions").findOne({ id: t2 });
}
async appendOutput(t2, n2) {
const e2 = this.getCollection("outputs");
await e2.findOne({ executionId: t2 }) ? await e2.updateOne(
{ executionId: t2 },
{ $push: { lines: { $each: n2 } } }
) : await e2.insertOne({ executionId: t2, lines: n2 });
}
async getOutput(t2) {
const e2 = await this.getCollection("outputs").findOne({ executionId: t2 });
return (e2 == null ? void 0 : e2.lines) || [];
}
}
class g {
static create(t2) {
const n2 = (t2 == null ? void 0 : t2.type) || process.env.CVM_STORAGE_TYPE || "file";
switch (n2) {
case "file": {
const e2 = (t2 == null ? void 0 : t2.dataDir) || process.env.CVM_DATA_DIR || ".cvm";
return new h(e2);
}
case "mongodb": {
const e2 = (t2 == null ? void 0 : t2.mongoUri) || process.env.MONGODB_URI || "mongodb://localhost:27017/cvm";
return new p(e2);
}
default:
throw new Error(`Unsupported storage type: ${n2}`);
}
}
}
var C = Object.defineProperty;
var G = (g2, a2, s2) => a2 in g2 ? C(g2, a2, { enumerable: true, configurable: true, writable: true, value: s2 }) : g2[a2] = s2;
var N2 = (g2, a2, s2) => G(g2, typeof a2 != "symbol" ? a2 + "" : a2, s2);
class A {
execute(a2, s2) {
const r2 = {
pc: (s2 == null ? void 0 : s2.pc) ?? 0,
stack: (s2 == null ? void 0 : s2.stack) ?? [],
variables: (s2 == null ? void 0 : s2.variables) ?? /* @__PURE__ */ new Map(),
status: "running",
output: (s2 == null ? void 0 : s2.output) ?? [],
iterators: (s2 == null ? void 0 : s2.iterators) ?? [],
...s2
};
for (; r2.status === "running" && r2.pc < a2.length; ) {
const n$1 = a2[r2.pc];
switch (n$1.op) {
case r$1.HALT:
r2.status = "complete";
break;
case r$1.PUSH:
r2.stack.push(n$1.arg), r2.pc++;
break;
case r$1.PUSH_UNDEFINED:
r2.stack.push(s$1()), r2.pc++;
break;
case r$1.POP:
r2.stack.pop(), r2.pc++;
break;
case r$1.LOAD: {
const t2 = n$1.arg;
r2.variables.has(t2) ? r2.stack.push(r2.variables.get(t2)) : r2.stack.push(s$1()), r2.pc++;
break;
}
case r$1.STORE:
const f$1 = r2.stack.pop();
if (f$1 === void 0) {
r2.status = "error", r2.error = "STORE: Stack underflow";
break;
}
r2.variables.set(n$1.arg, f$1), r2.pc++;
break;
case r$1.CONCAT:
const u2 = r2.stack.pop(), l2 = r2.stack.pop();
if (l2 === void 0 || u2 === void 0) {
r2.status = "error", r2.error = "CONCAT: Stack underflow";
break;
}
r2.stack.push(c$1(l2) + c$1(u2)), r2.pc++;
break;
case r$1.PRINT:
const k2 = r2.stack.pop();
k2 !== void 0 && r2.output.push(c$1(k2)), r2.pc++;
break;
case r$1.CC: {
const t2 = r2.stack.pop();
if (t2 === void 0) {
r2.status = "error", r2.error = "CC: Stack underflow";
break;
}
r2.ccPrompt = c$1(t2), r2.status = "waiting_cc";
break;
}
// Array operations
case r$1.ARRAY_NEW:
r2.stack.push(p$1()), r2.pc++;
break;
case r$1.ARRAY_PUSH: {
const t2 = r2.stack.pop(), e$1 = r2.stack.pop();
if (t2 === void 0 || e$1 === void 0) {
r2.status = "error", r2.error = "ARRAY_PUSH: Stack underflow";
break;
}
if (!e(e$1)) {
r2.status = "error", r2.error = "ARRAY_PUSH requires an array";
break;
}
e$1.elements.push(t2), r2.stack.push(e$1), r2.pc++;
break;
}
case r$1.ARRAY_GET: {
const t$1 = r2.stack.pop(), e$1 = r2.stack.pop();
if (t$1 === void 0 || e$1 === void 0) {
r2.status = "error", r2.error = "ARRAY_GET: Stack underflow";
break;
}
if (!e(e$1)) {
r2.status = "error", r2.error = "ARRAY_GET requires an array";
break;
}
if (!t(t$1)) {
r2.status = "error", r2.error = "ARRAY_GET requires numeric index";
break;
}
const o2 = e$1.elements[t$1] ?? null;
r2.stack.push(o2), r2.pc++;
break;
}
case r$1.ARRAY_SET: {
const t$1 = r2.stack.pop(), e$1 = r2.stack.pop(), o2 = r2.stack.pop();
if (t$1 === void 0 || e$1 === void 0 || o2 === void 0) {
r2.status = "error", r2.error = "ARRAY_SET: Stack underflow";
break;
}
if (!e(o2)) {
r2.status = "error", r2.error = "ARRAY_SET requires an array";
break;
}
if (!t(e$1)) {
r2.status = "error", r2.error = "ARRAY_SET requires numeric index";
break;
}
const i2 = Math.floor(e$1);
if (i2 < 0) {
r2.status = "error", r2.error = "ARRAY_SET: Negative index not allowed";
break;
}
o2.elements[i2] = t$1, r2.pc++;
break;
}
case r$1.ARRAY_LEN: {
const t2 = r2.stack.pop();
if (t2 === void 0) {
r2.status = "error", r2.error = "ARRAY_LEN: Stack underflow";
break;
}
if (!e(t2)) {
r2.status = "error", r2.error = "ARRAY_LEN requires an array";
break;
}
r2.stack.push(t2.elements.length), r2.pc++;
break;
}
case r$1.STRING_LEN: {
const t2 = r2.stack.pop();
if (t2 === void 0) {
r2.status = "error", r2.error = "STRING_LEN: Stack underflow";
break;
}
if (!n(t2)) {
r2.status = "error", r2.error = "STRING_LEN requires a string";
break;
}
r2.stack.push(t2.length), r2.pc++;
break;
}
case r$1.LENGTH: {
const t2 = r2.stack.pop();
if (t2 === void 0) {
r2.status = "error", r2.error = "LENGTH: Stack underflow";
break;
}
if (n(t2))
r2.stack.push(t2.length);
else if (e(t2))
r2.stack.push(t2.elements.length);
else {
r2.status = "error", r2.error = "LENGTH requires a string or array";
break;
}
r2.pc++;
break;
}
case r$1.JSON_PARSE: {
const t2 = r2.stack.pop();
if (t2 === void 0) {
r2.status = "error", r2.error = "JSON_PARSE: Stack underflow";
break;
}
if (!n(t2)) {
r2.status = "error", r2.error = "JSON_PARSE requires a string";
break;
}
try {
const e2 = JSON.parse(t2);
Array.isArray(e2) ? r2.stack.push(p$1(e2)) : r2.stack.push(p$1());
} catch {
r2.stack.push(p$1());
}
r2.pc++;
break;
}
case r$1.TYPEOF: {
const t2 = r2.stack.pop();
if (t2 === void 0) {
r2.status = "error", r2.error = "TYPEOF: Stack underflow";
break;
}
r2.stack.push(m(t2)), r2.pc++;
break;
}
// Arithmetic operations
case r$1.ADD: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (e2 === void 0 || t2 === void 0) {
r2.status = "error", r2.error = "ADD: Stack underflow";
break;
}
const o2 = d(e2), i2 = d(t2);
r2.stack.push(o2 + i2), r2.pc++;
break;
}
case r$1.SUB: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (e2 === void 0 || t2 === void 0) {
r2.status = "error", r2.error = "SUB: Stack underflow";
break;
}
const o2 = d(e2), i2 = d(t2);
r2.stack.push(o2 - i2), r2.pc++;
break;
}
case r$1.MUL: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (e2 === void 0 || t2 === void 0) {
r2.status = "error", r2.error = "MUL: Stack underflow";
break;
}
const o2 = d(e2), i2 = d(t2);
r2.stack.push(o2 * i2), r2.pc++;
break;
}
case r$1.DIV: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (e2 === void 0 || t2 === void 0) {
r2.status = "error", r2.error = "DIV: Stack underflow";
break;
}
const o2 = d(e2), i2 = d(t2);
if (i2 === 0) {
r2.status = "error", r2.error = "Division by zero";
break;
}
r2.stack.push(o2 / i2), r2.pc++;
break;
}
case r$1.MOD: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (e2 === void 0 || t2 === void 0) {
r2.status = "error", r2.error = "MOD: Stack underflow";
break;
}
const o2 = d(e2), i2 = d(t2);
r2.stack.push(o2 % i2), r2.pc++;
break;
}
// Unary operations
case r$1.UNARY_MINUS: {
const t2 = r2.stack.pop();
if (t2 === void 0) {
r2.status = "error", r2.error = "UNARY_MINUS: Stack underflow";
break;
}
const e2 = d(t2);
r2.stack.push(-e2), r2.pc++;
break;
}
case r$1.UNARY_PLUS: {
const t2 = r2.stack.pop();
if (t2 === void 0) {
r2.status = "error", r2.error = "UNARY_PLUS: Stack underflow";
break;
}
const e2 = d(t2);
r2.stack.push(e2), r2.pc++;
break;
}
case r$1.INC: {
const t2 = r2.stack.pop();
if (t2 === void 0 || typeof t2 != "string") {
r2.status = "error", r2.error = "INC: Invalid variable name";
break;
}
const e2 = r2.variables.get(t2) ?? 0, o2 = d(e2) + 1;
r2.variables.set(t2, o2);
const i2 = n$1.arg === true;
r2.stack.push(i2 ? d(e2) : o2), r2.pc++;
break;
}
case r$1.DEC: {
const t2 = r2.stack.pop();
if (t2 === void 0 || typeof t2 != "string") {
r2.status = "error", r2.error = "DEC: Invalid variable name";
break;
}
const e2 = r2.variables.get(t2) ?? 0, o2 = d(e2) - 1;
r2.variables.set(t2, o2);
const i2 = n$1.arg === true;
r2.stack.push(i2 ? d(e2) : o2), r2.pc++;
break;
}
// Comparison operations
case r$1.EQ: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (e2 === void 0 || t2 === void 0) {
r2.status = "error", r2.error = "EQ: Stack underflow";
break;
}
if (f(e2) && o(t2) || o(e2) && f(t2))
r2.stack.push(true);
else if (o(e2) && o(t2))
r2.stack.push(true);
else {
const o2 = d(e2), i2 = d(t2);
!isNaN(o2) && !isNaN(i2) ? r2.stack.push(o2 === i2) : r2.stack.push(c$1(e2) === c$1(t2));
}
r2.pc++;
break;
}
case r$1.NEQ: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (e2 === void 0 || t2 === void 0) {
r2.status = "error", r2.error = "NEQ: Stack underflow";
break;
}
if (f(e2) && o(t2) || o(e2) && f(t2))
r2.stack.push(false);
else if (o(e2) && o(t2))
r2.stack.push(false);
else {
const o2 = d(e2), i2 = d(t2);
!isNaN(o2) && !isNaN(i2) ? r2.stack.push(o2 !== i2) : r2.stack.push(c$1(e2) !== c$1(t2));
}
r2.pc++;
break;
}
case r$1.LT: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (e2 === void 0 || t2 === void 0) {
r2.status = "error", r2.error = "LT: Stack underflow";
break;
}
const o2 = d(e2), i2 = d(t2);
r2.stack.push(o2 < i2), r2.pc++;
break;
}
case r$1.GT: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (e2 === void 0 || t2 === void 0) {
r2.status = "error", r2.error = "GT: Stack underflow";
break;
}
const o2 = d(e2), i2 = d(t2);
r2.stack.push(o2 > i2), r2.pc++;
break;
}
case r$1.LTE: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (e2 === void 0 || t2 === void 0) {
r2.status = "error", r2.error = "LTE: Stack underflow";
break;
}
const o2 = d(e2), i2 = d(t2);
r2.stack.push(o2 <= i2), r2.pc++;
break;
}
case r$1.GTE: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (e2 === void 0 || t2 === void 0) {
r2.status = "error", r2.error = "GTE: Stack underflow";
break;
}
const o2 = d(e2), i2 = d(t2);
r2.stack.push(o2 >= i2), r2.pc++;
break;
}
case r$1.EQ_STRICT: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (e2 === void 0 || t2 === void 0) {
r2.status = "error", r2.error = "EQ_STRICT: Stack underflow";
break;
}
r2.stack.push(e2 === t2), r2.pc++;
break;
}
case r$1.NEQ_STRICT: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (e2 === void 0 || t2 === void 0) {
r2.status = "error", r2.error = "NEQ_STRICT: Stack underflow";
break;
}
r2.stack.push(e2 !== t2), r2.pc++;
break;
}
// Jump operations
case r$1.JUMP: {
if (n$1.arg === void 0) {
r2.status = "error", r2.error = "JUMP requires a target address";
break;
}
const t2 = n$1.arg;
if (t2 < 0 || t2 >= a2.length) {
r2.status = "error", r2.error = `Invalid jump target: ${t2}`;
break;
}
r2.pc = t2;
break;
}
case r$1.JUMP_IF_FALSE: {
const t2 = r2.stack.pop();
if (t2 === void 0) {
r2.status = "error", r2.error = "JUMP_IF_FALSE: Stack underflow";
break;
}
if (n$1.arg === void 0) {
r2.status = "error", r2.error = "JUMP_IF_FALSE requires a target address";
break;
}
const e2 = n$1.arg;
if (e2 < 0 || e2 >= a2.length) {
r2.status = "error", r2.error = `Invalid jump target: ${e2}`;
break;
}
y(t2) ? r2.pc++ : r2.pc = e2;
break;
}
case r$1.ITER_START: {
if (r2.stack.length === 0) {
r2.status = "error", r2.error = "ITER_START: Stack underflow";
break;
}
const t2 = r2.stack.pop();
if (t2 == null) {
r2.status = "error", r2.error = "TypeError: Cannot iterate over null or undefined";
break;
}
if (!e(t2)) {
r2.status = "error", r2.error = "TypeError: Cannot iterate over non-array value";
break;
}
if (r2.iterators.length >= 10) {
r2.status = "error", r2.error = "RuntimeError: Maximum iterator depth exceeded";
break;
}
const e$1 = p$1([...t2.elements]);
r2.iterators.push({
array: e$1,
index: 0
}), r2.pc++;
break;
}
case r$1.ITER_NEXT: {
if (r2.iterators.length === 0) {
r2.status = "error", r2.error = "ITER_NEXT: No active iterator";
break;
}
const t2 = r2.iterators[r2.iterators.length - 1];
t2.index < t2.array.elements.length ? (r2.stack.push(t2.array.elements[t2.index]), r2.stack.push(true), t2.index++) : (r2.stack.push(null), r2.stack.push(false)), r2.pc++;
break;
}
case r$1.ITER_END: {
if (r2.iterators.length === 0) {
r2.status = "error", r2.error = "ITER_END: No active iterator";
break;
}
r2.iterators.pop(), r2.pc++;
break;
}
// Logical operators
case r$1.AND: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (e2 === void 0 || t2 === void 0) {
r2.status = "error", r2.error = "AND: Stack underflow";
break;
}
y(e2) ? r2.stack.push(t2) : r2.stack.push(e2), r2.pc++;
break;
}
case r$1.OR: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (e2 === void 0 || t2 === void 0) {
r2.status = "error", r2.error = "OR: Stack underflow";
break;
}
y(e2) ? r2.stack.push(e2) : r2.stack.push(t2), r2.pc++;
break;
}
case r$1.NOT: {
const t2 = r2.stack.pop();
if (t2 === void 0) {
r2.status = "error", r2.error = "NOT: Stack underflow";
break;
}
r2.stack.push(!y(t2)), r2.pc++;
break;
}
case r$1.RETURN: {
const t2 = r2.stack.pop() ?? null;
r2.returnValue = t2, r2.status = "complete";
break;
}
// String methods
case r$1.STRING_SUBSTRING: {
if (r2.stack.length < 2) {
r2.status = "error", r2.error = "STRING_SUBSTRING: Stack underflow";
break;
}
const t2 = r2.stack.length;
let e2, o2, i2;
const w2 = r2.stack[t2 - 1], _ = r2.stack[t2 - 2];
if (t2 >= 3 && typeof w2 == "number" && typeof _ == "number" ? (i2 = r2.stack.pop(), o2 = r2.stack.pop(), e2 = r2.stack.pop()) : (o2 = r2.stack.pop(), e2 = r2.stack.pop(), i2 = void 0), !n(e2)) {
r2.status = "error", r2.error = "STRING_SUBSTRING requires a string";
break;
}
if (typeof o2 != "number") {
r2.status = "error", r2.error = "STRING_SUBSTRING requires numeric start index";
break;
}
const S = e2.length;
o2 < 0 && (o2 = Math.max(0, S + o2)), i2 !== void 0 && i2 < 0 && (i2 = Math.max(0, S + i2));
const O = i2 !== void 0 ? e2.substring(o2, i2) : e2.substring(o2);
r2.stack.push(O), r2.pc++;
break;
}
case r$1.STRING_INDEXOF: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (t2 === void 0 || e2 === void 0) {
r2.status = "error", r2.error = "STRING_INDEXOF: Stack underflow";
break;
}
if (!n(e2) || !n(t2)) {
r2.status = "error", r2.error = "STRING_INDEXOF requires string arguments";
break;
}
r2.stack.push(e2.indexOf(t2)), r2.pc++;
break;
}
case r$1.STRING_SPLIT: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (t2 === void 0 || e2 === void 0) {
r2.status = "error", r2.error = "STRING_SPLIT: Stack underflow";
break;
}
if (!n(e2) || !n(t2)) {
r2.status = "error", r2.error = "STRING_SPLIT requires string arguments";
break;
}
let o2;
t2 === "" ? o2 = e2.split("") : o2 = e2.split(t2), r2.stack.push(p$1(o2)), r2.pc++;
break;
}
case r$1.STRING_SLICE: {
if (r2.stack.length < 2) {
r2.status = "error", r2.error = "STRING_SLICE: Stack underflow";
break;
}
const t2 = r2.stack.length;
let e2, o2, i2;
const w2 = r2.stack[t2 - 1], _ = r2.stack[t2 - 2];
if (t2 >= 3 && typeof w2 == "number" && typeof _ == "number" ? (i2 = r2.stack.pop(), o2 = r2.stack.pop(), e2 = r2.stack.pop()) : (o2 = r2.stack.pop(), e2 = r2.stack.pop(), i2 = void 0), !n(e2)) {
r2.status = "error", r2.error = "STRING_SLICE requires a string";
break;
}
if (typeof o2 != "number") {
r2.status = "error", r2.error = "STRING_SLICE requires numeric start index";
break;
}
const S = i2 !== void 0 ? e2.slice(o2, i2) : e2.slice(o2);
r2.stack.push(S), r2.pc++;
break;
}
case r$1.STRING_CHARAT: {
const t2 = r2.stack.pop(), e2 = r2.stack.pop();
if (t2 === void 0 || e2 === void 0) {
r2.status = "error", r2.error = "STRING_CHARAT: Stack underflow";
break;
}
if (!n(e2)) {
r2.status = "error", r2.error = "STRING_CHARAT requires a string";
break;
}
if (typeof t2 != "number") {
r2.status = "error", r2.error = "STRING_CHARAT requires numeric index";
break;
}
const o2 = e2.charAt(t2);
r2.stack.push(o2), r2.pc++;
break;
}
case r$1.STRING_TOUPPERCASE: {
const t2 = r2.stack.pop();
if (t2 === void 0) {
r2.status = "error", r2.error = "STRING_TOUPPERCASE: Stack underflow";
break;
}
if (!n(t2)) {
r2.status = "error", r2.error = "STRING_TOUPPERCASE requires a string";
break;
}
r2.stack.push(t2.toUpperCase()), r2.pc++;
break;
}
case r$1.STRING_TOLOWERCASE: {
const t2 = r2.stack.pop();
if (t2 === void 0) {
r2.status = "error", r2.error = "STRING_TOLOWERCASE: Stack underflow";
break;
}
if (!n(t2)) {
r2.status = "error", r2.error = "STRING_TOLOWERCASE requires a string";
break;
}
r2.stack.push(t2.toLowerCase()), r2.pc++;
break;
}
case r$1.BREAK: {
if (n$1.arg === void 0) {
r2.status = "error", r2.error = "BREAK requires a target address";
break;
}
const t2 = n$1.arg;
if (t2 < 0 || t2 >= a2.length) {
r2.status = "error", r2.error = `Invalid break target: ${t2}`;
break;
}
r2.pc = t2;
break;
}
case r$1.CONTINUE: {
if (n$1.arg === void 0) {
r2.status = "error", r2.error = "CONTINUE requires a target address";
break;
}
const t2 = n$1.arg;
if (t2 < 0 || t2 >= a2.length) {
r2.status = "error", r2.error = `Invalid continue target: ${t2}`;
break;
}
r2.pc = t2;
break;
}
case r$1.FS_LIST_FILES: {
if (r2.stack.length < 1) {
r2.status = "error", r2.error = "FS_LIST_FILES: Stack underflow";
break;
}
let t2, e$1 = {};
const o2 = r2.stack[r2.stack.length - 1];
if (r2.stack.length >= 2 && typeof o2 == "object" && o2 !== null && !e(o2) && (e$1 = r2.stack.pop()), t2 = r2.stack.pop(), !n(t2)) {
r2.status = "error", r2.error = "FS_LIST_FILES requires a string path";
break;
}
r2.fsOperation = {
type: "listFiles",
path: t2,
options: e$1
}, r2.status = "waiting_fs";
break;
}
default:
r2.status = "error", r2.error = `Unknown opcode: ${n$1.op} (type: ${typeof n$1.op})`;
}
}
return r2;
}
resume(a2, s2, r2) {
if (a2.status !== "waiting_cc")
throw new Error("Cannot resume: VM not waiting for CC");
const n2 = {
...a2,
stack: [...a2.stack, s2],
status: "running",
ccPrompt: void 0,
pc: a2.pc + 1
};
return this.execute(r2, n2);
}
resumeWithFsResult(a2, s2, r2) {
if (a2.status !== "waiting_fs")
throw new Error("Cannot resume: VM not waiting for FS operation");
const n2 = {
...a2,
stack: [...a2.stack, s2],
status: "running",
fsOperation: void 0,
pc: a2.pc + 1
};
return this.execute(r2, n2);
}
}
class P {
constructor() {
N2(this, "sandboxPaths", []);
const a2 = process.env.CVM_SANDBOX_PATHS, s2 = process.env.CVM_SANDBOX_ROOT;
a2 ? this.sandboxPaths = a2.split(",").map((r2) => c__namespace.resolve(r2.trim())) : s2 && (this.sandboxPaths = [c__namespace.resolve(s2)]);
}
isPathAllowed(a2) {
const s2 = c__namespa