Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workletizable Context Objects #6229

Merged

Conversation

tjzel
Copy link
Collaborator

@tjzel tjzel commented Jul 8, 2024

This pull request introduces the concept of Context Objects on the Worklet Runtime.

What?

Context Object is a shareable object that doesn't lose its this binding when it goes to the Worklet Runtime. To define an object as a Context Object, the user has to define property __workletObject on it. (Should it be __contextObject?)

Why?

Currently it's not possible to refer to an object's methods from its other methods.

const obj = {
  foo() {
    console.log("foo");
  }
  bar() {
    this.foo();
  }
}

runOnUI(() => {
  obj.bar(); // Crash, the binding is lost on serialization.
})();

How?

Context Objects are handled as another type of shareable entity on the React Runtime.

  1. The plugin adds a special property __workletObjectFactory to Context Objects. It's a function that creates an identical object.
  2. We serialize the factory instead of the object itself.
  3. We recreate the object on the Worklet Runtime via ShareableHandle mechanism.

Test plan

  • Add unit tests
  • Add a runtime test suite

@tjzel tjzel changed the base branch from main to @tjzel/plugin/allow-file-workletization July 8, 2024 10:40
@tjzel tjzel force-pushed the @tjzel/plugin/allow-file-workletization branch from 133377c to 13bfdf9 Compare July 8, 2024 11:06
@tjzel tjzel marked this pull request as ready for review July 8, 2024 13:55
@tjzel tjzel merged commit 4dcf605 into @tjzel/plugin/allow-file-workletization Jul 17, 2024
7 checks passed
@tjzel tjzel deleted the @tjzel/plugin/context-objects branch July 17, 2024 11:28
@tjzel tjzel restored the @tjzel/plugin/context-objects branch July 17, 2024 11:28
tjzel added a commit that referenced this pull request Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant