Fix adding to context

This commit is contained in:
Adria Navarro 2024-02-08 13:51:36 +01:00
parent 2ffe3d7153
commit c57ccbc046
2 changed files with 10 additions and 3 deletions

View File

@ -13,11 +13,16 @@ export function init() {
let { vm } = bbCtx let { vm } = bbCtx
if (!vm) { if (!vm) {
// Can't copy the native helpers into the isolate. We just ignore them as they are handled properly from the helpersSource
const { helpers, ...ctxToPass } = ctx
vm = new IsolatedVM({ vm = new IsolatedVM({
memoryLimit: env.JS_RUNNER_MEMORY_LIMIT, memoryLimit: env.JS_RUNNER_MEMORY_LIMIT,
timeout: env.JS_PER_EXECUTION_TIME_LIMIT_MS, timeout: env.JS_PER_EXECUTION_TIME_LIMIT_MS,
perRequestLimit: env.JS_PER_REQUEST_TIME_LIMIT_MS, perRequestLimit: env.JS_PER_REQUEST_TIME_LIMIT_MS,
}).withHelpers() })
.withContext(ctxToPass)
.withHelpers()
bbCtx.vm = vm bbCtx.vm = vm
} }

View File

@ -185,10 +185,12 @@ export class IsolatedVM implements VM {
private addToContext(context: Record<string, any>) { private addToContext(context: Record<string, any>) {
for (let key in context) { for (let key in context) {
const value = context[key]
this.jail.setSync( this.jail.setSync(
key, key,
context[key] typeof value === "function"
//new ivm.ExternalCopy(context[key]).copyInto({ release: true }) ? value
: new ivm.ExternalCopy(value).copyInto({ release: true })
) )
} }
} }