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

Unify decorating RN runtime #4773

Merged
merged 4 commits into from
Jul 19, 2023
Merged

Conversation

tomekzaw
Copy link
Member

@tomekzaw tomekzaw commented Jul 19, 2023

Summary

This PR moves duplicated logic of decorating RN runtime from NativeProxy.cpp and REAModule.mm into RuntimeDecorator::decorateRNRuntime. I've also tried to move __reanimatedModuleProxy (NativeReanimatedModule) there but there were some problems with circular headers which I couldn't solve easily so I'm leaving this for the next PR.

Test plan

@tomekzaw tomekzaw requested a review from piaskowyk July 19, 2023 14:17
@tomekzaw tomekzaw added this pull request to the merge queue Jul 19, 2023
Merged via the queue into main with commit 90c3566 Jul 19, 2023
9 checks passed
@tomekzaw tomekzaw deleted the @tomekzaw/unify-runtime-decoration branch July 19, 2023 17:24
github-merge-queue bot pushed a commit that referenced this pull request Aug 26, 2023
<!-- Thanks for submitting a pull request! We appreciate you spending
the time to work on these changes. Please follow the template so that
the reviewers can easily understand what the code changes affect. -->

## Summary

This PR implements and exposes a new API for creating worklet runtimes
as well as running and scheduling worklets both from JS and C++ code.
The motivation of this PR was to allow for easier integration of other
third-party libraries with Reanimated worklets.

The following changes were required but they were merged as separate PRs
for the ease of code review:

* #4608
* #4612
* #4621
* #4722
* #4723
* #4731
* #4765
* #4768
* #4773
* #4775
* #4779
* #4780
* #4785
* #4800
* #4869
* #4871
* #4929

## Changes

1. Removed `RuntimeManager` and `JSRuntimeHelper` in favor of
`WorkletRuntime` class
- `WorkletRuntime` is runtime-agnostic (supports JSC/Hermes/V8), has
built-in debugging support on Hermes (via Chrome DevTools Protocol), is
a `jsi::HostObject` so it can be passed between JS/C++, offers method
for calling worklets via callGuardDEV
- Refactored all usages of UI runtime in `NativeReanimatedModule`,
`AnimatedSensorModule`, `EventHandlerRegistry`
- Added `WorkletRuntimeRegistry` to keep track of whether worklet
runtime is still alive
- Added `WorkletRuntimeCollector` that is a host object that unregisters
worklet runtime in its destructor (i.e. when runtime is terminated)
- Added possibility to customize name of runtime created by
`ReanimatedRuntime::make` for debugging purposes
1. Added `createWorkletRuntime` function along with `runOnRuntime`,
`runOnRuntimeSync` and `backgroundTask`
    - Added `WorkletRuntimeExample.tsx` to test out the changes
1. Removed `RuntimeDecorator` class in favor of having separate classes
in order to decouple code
- Moved `RuntimeDecorator::decorateRuntime` to
`RNRuntimeDecorator::decorate` that injects JSI bindings specific for
React Native runtime (e.g. `_WORKLET_RUNTIME`)
- Moved `RuntimeDecorator::decorateUIRuntime` to
`UIRuntimeDecorator::decorate` that injects UI-specific JSI bindings
(e.g. `_updateProps`)
- Added `WorkletRuntimeDecorator::decorate` that injects
worklet-specific JSI bindings (e.g. `evalWithSourceMap` or
`_makeShareableClone`)
1. Removed `CoreFunction` class and
`NativeReanimatedModule::installCoreFunctions` method in favor of
passing `valueUnpacker` code as a string (this is possible because of
the empty closure) and installing `callGuardDEV` from a worklet
1. Refactored shareables so that they support other runtimes than only
RN and UI runtime
1. Refactored `NativeReanimatedModule` constructor and members
- Eliminated `LayoutAnimationsManager::setJSLogger` method in favor of
passing `JSLogger` in its constructor
    - Moved Paper- and Fabric- only fields to `#ifdef`s
- Added missing trailing underscores in member names for the purpose of
consistency
    - Added `const` keyword wherever possible
1. Moved `jsi::Scope` from `UIScheduler::scheduleOnUI` to
`NativeReanimatedModule::scheduleOnUI` in order to decouple
`RuntimeManager` and `UIScheduler` and also because some UI jobs may not
need runtime access

## Future plans
- Split C++ headers into public and private
- Create UI runtime using `createWorkletRuntime` from JS instead of from
C++
- Pass `valueUnpacker` code via argument of
`ReanimatedModule.installTurboModule` method of instead of calling
`NativeReanimatedModule.installValueUnpacker`
- Implement `createWorkletRuntime` on web?

## Test plan

💣
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants