This commit is contained in:
Boris D
2025-09-22 18:35:02 +03:00
parent 51f8b0d773
commit fbbbd61838
23 changed files with 282 additions and 67 deletions

View File

@ -28,13 +28,17 @@ export class Vm {
this.jail.set("global", this.jail.derefInto());
for (const mod of this.modules) {
this.jail.setSync(mod.getName(), mod.getSource());
await this.context.eval(mod.getSource());
}
for (const plugin of this.plugins) {
this.jail.setSync(
plugin.getName(),
new ivm.Reference(plugin.run.bind(plugin))
new ivm.Reference(async (...args) => {
const res = await plugin.run(...args);
return res;
})
);
}
@ -61,6 +65,9 @@ export class Vm {
});
// TODO: log
this.setFunction("log", (...args) => {
console.log("vm log:", args);
});
this.setFunction("error", (error: any) => {
console.error("Script error:", error);
@ -82,9 +89,12 @@ export class Vm {
const compiledScript = await this.isolate.compileScript(scriptWithResult);
compiledScript.run(this.context);
this.onFinish();
return await resultPromise;
try {
return await resultPromise;
} finally {
this.onFinish();
}
}
onFinish() {