Closed demon-user closed 1 year ago
promise: true https://github.com/laverdet/isolated-vm#transferoptions
async function getSourceScript() {
return `
async function add(a, b) {
return a + b;
}
`;
}
const vm = new ivm.Isolate({ memoryLimit: 128 });
const context = await vm.createContext();
const sourceScript = await getSourceScript();
await vm.compileScript(sourceScript);
const runFn = await context.global.get('add', { reference: true, promise: true });
const result = await runFn.apply(undefined, [1, 2]);
console.log(result);
Produces the following error:
/src/executor.ts:14
meta: Request['meta'];
^
TypeError: Reference is not a function
at (<isolated-vm boundary>)
at functionExecutor (
You could use a small helper script and run it like this.
const helperScript = `
helper = async (num1, num2) => {
const result = await add(num1, num2);
callback.applySync(undefined, [ result ]);
};
`;
async function getSourceScript() {
return `
async function add(a, b) {
return a + b;
}
`;
}
const vm = new ivm.Isolate({ memoryLimit: 128 });
const context = await vm.createContext();
const sourceScript = await getSourceScript();
// we define a callback in global scope of isolate context to get the result back
await context.global.set('callback', new ivm.Reference(function(result) {
console.log('output of the code', result);
}));
await vm.compileScript(sourceScript).run(context);
// Run our helper code, which eventually calls the code
await context.evalClosure(helperScript);
const helper = await context.global.get('helper', { reference: true, promise: true });
try {
await helper.apply(undefined, [100, 200]);
} catch (error) {
console.log('error', error);
}
I have a simple use case, and am a complete beginner here. 1.I have an async function
I need to run the above code and return the result.