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); } }