Refactor code structure for improved readability and maintainability

This commit is contained in:
lborv
2025-09-17 17:02:03 +03:00
parent afb1f343e0
commit db58d6ecb1
28 changed files with 674 additions and 132 deletions

31
src/vm/vm.class.ts Normal file
View File

@ -0,0 +1,31 @@
import * as ivm from "isolated-vm";
import { Module } from "./module.class";
export class Vm {
private memoryLimit: number;
private modules: Module[];
private context: any;
private jail: any;
constructor(configs: { memoryLimit: number; modules: Module[] }) {
this.memoryLimit = configs.memoryLimit;
this.modules = configs.modules;
}
async init() {
const isolate = new ivm.Isolate({ memoryLimit: this.memoryLimit });
this.context = isolate.createContext();
this.jail = this.context.global;
this.jail.set("global", this.jail.derefInto());
for (const mod of this.modules) {
this.jail.setSync(mod.getName(), mod.getSource());
}
}
async runScript(script: string) {
const compiledScript = await this.context.isolate.compileScript(script);
return compiledScript.run(this.context);
}
}