react-native-reanimated icon indicating copy to clipboard operation
react-native-reanimated copied to clipboard

[PoC] Use lazy closures in worklets

Open tjzel opened this issue 1 year ago • 0 comments

Summary

This PR was motivated by #5365. Fixes #5365.

As you can read there, currently a variable used in a worklet but defined after it will cause it to be undefined in a worklet. Example:

function foo(){
  'worklet'
  console.log(bar);
}

const bar = 1;

runOnUI(foo)();

Will print undefined.

This PR moves unpacking of a worklet's closure from its worklet factory to the actual usage of a worklet.

Instead of having (removed irrelevant lines):

var foo = function () {
  var foo = function foo() {
    console.log(bar);
  };
  foo.__closure = {
    bar: bar
  };
  return foo;
}();

we will get:

var foo = function () {
  var foo = function foo() {
    console.log(bar);
  };
  foo.__closure = function () {
    return {
      bar: bar
    };
  };
  return foo;
}();

Don't mind all the typeof ... ? currently as it was a fail-safe for test-purposes. With current implementation all of Reanimated seems to be working properly.

Test plan

Test all of Reanimated 🤯

tjzel avatar Dec 28 '23 17:12 tjzel