laverdet / isolated-vm

Secure & isolated JS environments for nodejs
ISC License
2.12k stars 151 forks source link

TypeError: #<Promise> could not be cloned. #467

Open justinmann opened 5 months ago

justinmann commented 5 months ago

I am trying to create an async function in unsafe JS that can invoke an async function and return a value.

This code can call the async function "test" from the unsafe script, but when it try to return it produces the error "TypeError: # could not be cloned."

Node.js v18.19.0 isolated-vm v4.7.2

 const context = await isolate.createContext();
  await context.global.set(
    'test',
    new ivm.Callback(
      async (a: string) => {
        console.log(a);
        return a;
      },
      { async: true },
    ),
  );

  const script = `async function execute(a) { await test(a) }`;
  const fn = await isolate.compileScript(script);
  await fn.run(context);
  const fnReference = await context.global.get('execute', { reference: true });
  const result = await fnReference.apply(undefined, ['FOO'], {
    result: { promise: true },
  });
  console.log(result);
siddharthvp commented 4 months ago

It can be done by using a Reference instead of a Callback. See https://github.com/laverdet/isolated-vm/issues/322#issuecomment-2131419114. Also, you appear to be missing a return in the execute function.