1
0
Fork 0
mirror of synced 2024-09-11 06:56:23 +12:00

Avoid crossing results

This commit is contained in:
Adria Navarro 2024-02-14 11:47:34 +01:00
parent 7d6e49f8ca
commit bb6500cc91

View file

@ -26,6 +26,7 @@ export class IsolatedVM implements VM {
private codeWrapper: (code: string) => string = code => code private codeWrapper: (code: string) => string = code => code
private readonly resultKey = "results" private readonly resultKey = "results"
private runResultKey: string
constructor({ constructor({
memoryLimit, memoryLimit,
@ -41,8 +42,9 @@ export class IsolatedVM implements VM {
this.jail = this.vm.global this.jail = this.vm.global
this.jail.setSync("global", this.jail.derefInto()) this.jail.setSync("global", this.jail.derefInto())
this.runResultKey = crypto.randomUUID()
this.addToContext({ this.addToContext({
[this.resultKey]: { out: "" }, [this.resultKey]: { [this.runResultKey]: "" },
}) })
this.invocationTimeout = invocationTimeout this.invocationTimeout = invocationTimeout
@ -163,7 +165,7 @@ export class IsolatedVM implements VM {
} }
} }
code = `results.out=${this.codeWrapper(code)}` code = `results['${this.runResultKey}']=${this.codeWrapper(code)}`
const script = this.isolate.compileScriptSync(code) const script = this.isolate.compileScriptSync(code)
@ -171,7 +173,7 @@ export class IsolatedVM implements VM {
// We can't rely on the script run result as it will not work for non-transferable values // We can't rely on the script run result as it will not work for non-transferable values
const result = this.getFromContext(this.resultKey) const result = this.getFromContext(this.resultKey)
return result.out return result[this.runResultKey]
} }
private registerCallbacks(functions: Record<string, any>) { private registerCallbacks(functions: Record<string, any>) {