leobalter / shadowrealms-polyfill

MIT License
58 stars 0 forks source link

Automatic wrapping callback functions #6

Closed leobalter closed 3 years ago

leobalter commented 3 years ago

When I presented Realm#wrappedCallbackFunction to the team, @jdalton and @rwaldron suggested we should just wrap it on the go.

Do we have any reason to have a separate wrap?

const r = new Realm();

const redFunction = r.Function('cb', 'return cb() * 2;');

function autoWrapped() {
  return 21;
}

redFunction(autoWrapped); // yields 42

This seems possible. The current polyfill already prevents leakage of function properties.

littledan commented 3 years ago

I like this idea. It feels like a natural complement to #8 . It actually feels simpler to create a fresh wrapped function each time, rather than let wrapped functions through, since then we don't have any sort of issue of having the identity shared on both sides of the Realm boundary.