From 285916d0bfbfe2f56e2d5c7365348ad0de2e40ff Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 20 Feb 2024 11:11:27 +0000 Subject: [PATCH] Some PR comments/build issue. --- packages/server/src/jsRunner/vm/builtin-vm.ts | 11 +++++++++++ packages/server/src/jsRunner/vm/isolated-vm.ts | 4 ++-- packages/server/src/jsRunner/vm/vm2.ts | 4 ++-- packages/types/src/sdk/vm.ts | 2 +- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/server/src/jsRunner/vm/builtin-vm.ts b/packages/server/src/jsRunner/vm/builtin-vm.ts index b4c9f775f9..849abdd6f2 100644 --- a/packages/server/src/jsRunner/vm/builtin-vm.ts +++ b/packages/server/src/jsRunner/vm/builtin-vm.ts @@ -15,6 +15,17 @@ export class BuiltInVM implements VM { this.span = span } + withContext(context: Record, executeWithContext: () => T): T { + this.ctx = vm.createContext(context) + try { + return executeWithContext() + } finally { + for (const key in context) { + delete this.ctx[key] + } + } + } + execute(code: string) { const perRequestLimit = env.JS_PER_REQUEST_TIMEOUT_MS let track: TrackerFn = f => f() diff --git a/packages/server/src/jsRunner/vm/isolated-vm.ts b/packages/server/src/jsRunner/vm/isolated-vm.ts index 78250acdd2..e5c431666d 100644 --- a/packages/server/src/jsRunner/vm/isolated-vm.ts +++ b/packages/server/src/jsRunner/vm/isolated-vm.ts @@ -97,11 +97,11 @@ export class IsolatedVM implements VM { return this } - withContext(context: Record, f: () => T) { + withContext(context: Record, executeWithContext: () => T) { this.addToContext(context) try { - return f() + return executeWithContext() } finally { this.removeFromContext(Object.keys(context)) } diff --git a/packages/server/src/jsRunner/vm/vm2.ts b/packages/server/src/jsRunner/vm/vm2.ts index 75e3899064..5825025d26 100644 --- a/packages/server/src/jsRunner/vm/vm2.ts +++ b/packages/server/src/jsRunner/vm/vm2.ts @@ -16,10 +16,10 @@ export class VM2 implements VM { this.vm.setGlobal("results", this.results) } - withContext(context: Record, fn: () => T): T { + withContext(context: Record, executeWithContext: () => T): T { this.vm.setGlobals(context) try { - return fn() + return executeWithContext() } finally { for (const key in context) { this.vm.setGlobal(key, undefined) diff --git a/packages/types/src/sdk/vm.ts b/packages/types/src/sdk/vm.ts index 314883e0c5..f1099524bc 100644 --- a/packages/types/src/sdk/vm.ts +++ b/packages/types/src/sdk/vm.ts @@ -1,4 +1,4 @@ export interface VM { execute(code: string): any - withContext(context: Record, fn: () => T): T + withContext(context: Record, executeWithContext: () => T): T }