Skip to content

Commit

Permalink
avoid constant resetting
Browse files Browse the repository at this point in the history
  • Loading branch information
samijaber committed Apr 29, 2024
1 parent 2c18822 commit 2f40c3d
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions packages/sdks/src/functions/evaluate/node-runtime/node-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ function setIsolateContext(options: IsolateOptions = { memoryLimit: 128 }) {
const isolate = new ivm.Isolate(options);
const context = isolate.createContextSync();

const jail = context.global;

// This makes the global object available in the context as `global`. We use `derefInto()` here
// because otherwise `global` would actually be a Reference{} object in the new isolate.
jail.setSync('global', jail.derefInto());

// We will create a basic `log` function for the new isolate to use.
jail.setSync('log', function (...logArgs: any[]) {
console.log(...logArgs);
});

jail.setSync(INJECTED_IVM_GLOBAL, ivm);

IVM_CONTEXT = context;
return context;
}
Expand Down Expand Up @@ -153,15 +166,6 @@ export const runInNode = ({
const isolateContext = getIsolateContext();
const jail = isolateContext.global;

// This makes the global object available in the context as `global`. We use `derefInto()` here
// because otherwise `global` would actually be a Reference{} object in the new isolate.
jail.setSync('global', jail.derefInto());

// We will create a basic `log` function for the new isolate to use.
jail.setSync('log', function (...logArgs: any[]) {
console.log(...logArgs);
});

/**
* Propagate state changes back to the reactive root state.
*/
Expand All @@ -172,6 +176,7 @@ export const runInNode = ({
// call the `rootSetState` function if it exists
rootSetState?.(rootState);
});

args.forEach(([key, arg]) => {
const val =
typeof arg === 'object'
Expand All @@ -189,12 +194,8 @@ export const runInNode = ({
jail.setSync(getSyncValName(key), val);
});

jail.setSync(INJECTED_IVM_GLOBAL, ivm);
const evalStr = processCode({ code, args });

const evalStr = processCode({
code,
args,
});
const resultStr = isolateContext.evalClosureSync(evalStr);

try {
Expand Down

0 comments on commit 2f40c3d

Please sign in to comment.