Skip to content

Commit

Permalink
Merge branch 'main' into @tomekzaw/react-native-0.77
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw committed Dec 10, 2024
2 parents 7068334 + 17e89ca commit a885763
Show file tree
Hide file tree
Showing 35 changed files with 250 additions and 633 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,19 @@ namespace reanimated {
ReanimatedModuleProxy::ReanimatedModuleProxy(
const std::shared_ptr<WorkletsModuleProxy> &workletsModuleProxy,
jsi::Runtime &rnRuntime,
const std::shared_ptr<JSScheduler> &jsScheduler,
const std::shared_ptr<UIScheduler> &uiScheduler,
const std::shared_ptr<CallInvoker> &jsCallInvoker,
const PlatformDepMethodsHolder &platformDepMethodsHolder,
const bool isBridgeless,
const bool isReducedMotion)
: ReanimatedModuleProxySpec(
isBridgeless ? nullptr : jsScheduler->getJSCallInvoker()),
: ReanimatedModuleProxySpec(jsCallInvoker),
isBridgeless_(isBridgeless),
isReducedMotion_(isReducedMotion),
workletsModuleProxy_(workletsModuleProxy),
jsScheduler_(jsScheduler),
uiScheduler_(uiScheduler),
valueUnpackerCode_(workletsModuleProxy->getValueUnpackerCode()),
uiWorkletRuntime_(std::make_shared<WorkletRuntime>(
rnRuntime,
workletsModuleProxy->getJSQueue(),
jsScheduler_,
workletsModuleProxy->getJSScheduler(),
"Reanimated UI runtime",
true /* supportsLocking */,
valueUnpackerCode_)),
Expand All @@ -79,7 +75,8 @@ ReanimatedModuleProxy::ReanimatedModuleProxy(
onRender(timestampMs);
}),
animatedSensorModule_(platformDepMethodsHolder),
jsLogger_(std::make_shared<JSLogger>(jsScheduler_)),
jsLogger_(
std::make_shared<JSLogger>(workletsModuleProxy->getJSScheduler())),
layoutAnimationsManager_(
std::make_shared<LayoutAnimationsManager>(jsLogger_)),
#ifdef RCT_NEW_ARCH_ENABLED
Expand Down Expand Up @@ -207,7 +204,7 @@ void ReanimatedModuleProxy::scheduleOnUI(
const jsi::Value &worklet) {
auto shareableWorklet = extractShareableOrThrow<ShareableWorklet>(
rt, worklet, "[Reanimated] Only worklets can be scheduled to run on UI.");
uiScheduler_->scheduleOnUI(COPY_CAPTURE_WITH_THIS {
workletsModuleProxy_->getUIScheduler()->scheduleOnUI(COPY_CAPTURE_WITH_THIS {
#if JS_RUNTIME_HERMES
// JSI's scope defined here allows for JSI-objects to be cleared up
// after each runtime loop. Within these loops we typically create some
Expand All @@ -233,7 +230,7 @@ jsi::Value ReanimatedModuleProxy::createWorkletRuntime(
auto workletRuntime = std::make_shared<WorkletRuntime>(
rt,
workletsModuleProxy_->getJSQueue(),
jsScheduler_,
workletsModuleProxy_->getJSScheduler(),
name.asString(rt).utf8(rt),
false /* supportsLocking */,
valueUnpackerCode_);
Expand Down Expand Up @@ -264,7 +261,7 @@ jsi::Value ReanimatedModuleProxy::registerEventHandler(
rt, worklet, "[Reanimated] Event handler must be a worklet.");
int emitterReactTagInt = emitterReactTag.asNumber();

uiScheduler_->scheduleOnUI(COPY_CAPTURE_WITH_THIS {
workletsModuleProxy_->getUIScheduler()->scheduleOnUI(COPY_CAPTURE_WITH_THIS {
auto handler = std::make_shared<WorkletEventHandler>(
newRegistrationId, eventNameStr, emitterReactTagInt, handlerShareable);
eventHandlerRegistry_->registerEventHandler(std::move(handler));
Expand All @@ -277,7 +274,7 @@ void ReanimatedModuleProxy::unregisterEventHandler(
jsi::Runtime &,
const jsi::Value &registrationId) {
uint64_t id = registrationId.asNumber();
uiScheduler_->scheduleOnUI(
workletsModuleProxy_->getUIScheduler()->scheduleOnUI(
COPY_CAPTURE_WITH_THIS

{ eventHandlerRegistry_->unregisterEventHandler(id); });
Expand Down Expand Up @@ -349,16 +346,17 @@ jsi::Value ReanimatedModuleProxy::getViewProp(
const auto funPtr = std::make_shared<jsi::Function>(
callback.getObject(rnRuntime).asFunction(rnRuntime));
const auto shadowNode = shadowNodeFromValue(rnRuntime, shadowNodeWrapper);
uiScheduler_->scheduleOnUI([=, this]() {
workletsModuleProxy_->getUIScheduler()->scheduleOnUI(COPY_CAPTURE_WITH_THIS {
jsi::Runtime &uiRuntime = uiWorkletRuntime_->getJSIRuntime();
const auto resultStr =
obtainPropFromShadowNode(uiRuntime, propNameStr, shadowNode);

jsScheduler_->scheduleOnJS([=](jsi::Runtime &rnRuntime) {
const auto resultValue =
jsi::String::createFromUtf8(rnRuntime, resultStr);
funPtr->call(rnRuntime, resultValue);
});
workletsModuleProxy_->getJSScheduler()->scheduleOnJS(
[=](jsi::Runtime &rnRuntime) {
const auto resultValue =
jsi::String::createFromUtf8(rnRuntime, resultStr);
funPtr->call(rnRuntime, resultValue);
});
});
return jsi::Value::undefined();
}
Expand All @@ -376,7 +374,7 @@ jsi::Value ReanimatedModuleProxy::getViewProp(

const int viewTagInt = viewTag.asNumber();

uiScheduler_->scheduleOnUI(
workletsModuleProxy_->getUIScheduler()->scheduleOnUI(
COPY_CAPTURE_WITH_THIS

() {
Expand All @@ -386,8 +384,8 @@ jsi::Value ReanimatedModuleProxy::getViewProp(
const auto resultValue =
obtainPropFunction_(uiRuntime, viewTagInt, propNameValue);
const auto resultStr = resultValue.asString(uiRuntime).utf8(uiRuntime);

jsScheduler_->scheduleOnJS([=](jsi::Runtime &rnRuntime) {
const auto jsScheduler = workletsModuleProxy_->getJSScheduler();
jsScheduler->scheduleOnJS([=](jsi::Runtime &rnRuntime) {
const auto resultValue =
jsi::String::createFromUtf8(rnRuntime, resultStr);
funPtr->call(rnRuntime, resultValue);
Expand Down Expand Up @@ -855,7 +853,7 @@ void ReanimatedModuleProxy::initializeLayoutAnimationsProxy() {
componentDescriptorRegistry,
scheduler->getContextContainer(),
uiWorkletRuntime_->getJSIRuntime(),
uiScheduler_);
workletsModuleProxy_->getUIScheduler());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class ReanimatedModuleProxy : public ReanimatedModuleProxySpec {
ReanimatedModuleProxy(
const std::shared_ptr<WorkletsModuleProxy> &workletsModuleProxy,
jsi::Runtime &rnRuntime,
const std::shared_ptr<JSScheduler> &jsScheduler,
const std::shared_ptr<UIScheduler> &uiScheduler,
const std::shared_ptr<CallInvoker> &jsCallInvoker,
const PlatformDepMethodsHolder &platformDepMethodsHolder,
const bool isBridgeless,
const bool isReducedMotion);
Expand Down Expand Up @@ -194,8 +193,6 @@ class ReanimatedModuleProxy : public ReanimatedModuleProxySpec {
const bool isBridgeless_;
const bool isReducedMotion_;
const std::shared_ptr<WorkletsModuleProxy> workletsModuleProxy_;
const std::shared_ptr<JSScheduler> jsScheduler_;
const std::shared_ptr<UIScheduler> uiScheduler_;
const std::string valueUnpackerCode_;
std::shared_ptr<WorkletRuntime> uiWorkletRuntime_;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ namespace worklets {

WorkletsModuleProxy::WorkletsModuleProxy(
const std::string &valueUnpackerCode,
const std::shared_ptr<MessageQueueThread> &jsQueue)
: WorkletsModuleProxySpec(nullptr),
const std::shared_ptr<MessageQueueThread> &jsQueue,
const std::shared_ptr<CallInvoker> &jsCallInvoker,
const std::shared_ptr<JSScheduler> &jsScheduler,
const std::shared_ptr<UIScheduler> &uiScheduler)
: WorkletsModuleProxySpec(jsCallInvoker),
valueUnpackerCode_(valueUnpackerCode),
jsQueue_(jsQueue) {}
jsQueue_(jsQueue),
jsScheduler_(jsScheduler),
uiScheduler_(uiScheduler) {}

WorkletsModuleProxy::~WorkletsModuleProxy() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#include <cxxreact/MessageQueueThread.h>
#include <worklets/NativeModules/WorkletsModuleProxySpec.h>
#include <worklets/Tools/JSScheduler.h>
#include <worklets/Tools/SingleInstanceChecker.h>
#include <worklets/Tools/UIScheduler.h>
#include <worklets/WorkletRuntime/WorkletRuntime.h>
#include <memory>
#include <string>
Expand All @@ -13,7 +15,10 @@ class WorkletsModuleProxy : public WorkletsModuleProxySpec {
public:
explicit WorkletsModuleProxy(
const std::string &valueUnpackerCode,
const std::shared_ptr<MessageQueueThread> &jsQueue);
const std::shared_ptr<MessageQueueThread> &jsQueue,
const std::shared_ptr<CallInvoker> &jsCallInvoker,
const std::shared_ptr<JSScheduler> &jsScheduler,
const std::shared_ptr<UIScheduler> &uiScheduler);

~WorkletsModuleProxy();

Expand All @@ -31,9 +36,19 @@ class WorkletsModuleProxy : public WorkletsModuleProxySpec {
return jsQueue_;
}

[[nodiscard]] inline std::shared_ptr<JSScheduler> getJSScheduler() const {
return jsScheduler_;
}

[[nodiscard]] inline std::shared_ptr<UIScheduler> getUIScheduler() const {
return uiScheduler_;
}

private:
const std::string valueUnpackerCode_;
const std::shared_ptr<MessageQueueThread> jsQueue_;
const std::shared_ptr<JSScheduler> jsScheduler_;
const std::shared_ptr<UIScheduler> uiScheduler_;
#ifndef NDEBUG
SingleInstanceChecker<WorkletsModuleProxy> singleInstanceChecker_;
#endif // NDEBUG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,12 @@ jsi::Value ShareableHandle::toJSValue(jsi::Runtime &rt) {
remoteRuntime_ = &rt;
}
}
return jsi::Value(rt, *remoteValue_);
if (&rt == remoteRuntime_) {
return jsi::Value(rt, *remoteValue_);
}
auto initObj = initializer_->toJSValue(rt);
return getValueUnpacker(rt).call(
rt, initObj, jsi::String::createFromAscii(rt, "Handle"));
}

jsi::Value ShareableString::toJSValue(jsi::Runtime &rt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,4 @@ JSScheduler::JSScheduler(
rnRuntime_(rnRuntime),
jsCallInvoker_(jsCallInvoker) {}

#ifdef RCT_NEW_ARCH_ENABLED
// With `runtimeExecutor`.
JSScheduler::JSScheduler(
jsi::Runtime &rnRuntime,
RuntimeExecutor runtimeExecutor)
: scheduleOnJS([&](Job job) {
runtimeExecutor_(
[job = std::move(job)](jsi::Runtime &runtime) { job(runtime); });
}),
rnRuntime_(rnRuntime),
runtimeExecutor_(runtimeExecutor) {}
#endif // RCT_NEW_ARCH_ENABLED

const std::shared_ptr<CallInvoker> JSScheduler::getJSCallInvoker() const {
assert(
jsCallInvoker_ != nullptr &&
"[Reanimated] Expected jsCallInvoker, got nullptr instead.");
return jsCallInvoker_;
}

} // namespace worklets
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,14 @@ class JSScheduler {
using Job = std::function<void(jsi::Runtime &rt)>;

public:
// With `jsCallInvoker`.
explicit JSScheduler(
jsi::Runtime &rnRuntime,
const std::shared_ptr<CallInvoker> &jsCallInvoker);

#ifdef RCT_NEW_ARCH_ENABLED
// With `runtimeExecutor`.
explicit JSScheduler(
jsi::Runtime &rnRuntime,
RuntimeExecutor runtimeExecutor);
#endif // RCT_NEW_ARCH_ENABLED

const std::function<void(Job)> scheduleOnJS = nullptr;
const std::shared_ptr<CallInvoker> getJSCallInvoker() const;

protected:
jsi::Runtime &rnRuntime_;
#ifdef RCT_NEW_ARCH_ENABLED
RuntimeExecutor runtimeExecutor_ = nullptr;
#endif // RCT_NEW_ARCH_ENABLED
const std::shared_ptr<CallInvoker> jsCallInvoker_ = nullptr;
};

Expand Down
16 changes: 0 additions & 16 deletions packages/react-native-reanimated/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -362,22 +362,6 @@ android {
}
}

if (IS_NEW_ARCHITECTURE_ENABLED) {
// RuntimeExecutor and CallInvokerHolder
if (REACT_NATIVE_MINOR_VERSION <= 74) {
srcDirs += "src/reactNativeVersionPatch/NativeProxyFabric/74"
} else {
srcDirs += "src/reactNativeVersionPatch/NativeProxyFabric/latest"
}
} else {
// CallInvokerHolder
if (REACT_NATIVE_MINOR_VERSION <= 74) {
srcDirs += "src/reactNativeVersionPatch/NativeProxyPaper/74"
} else {
srcDirs += "src/reactNativeVersionPatch/NativeProxyPaper/latest"
}
}

// BorderRadiiDrawableUtils
if (REACT_NATIVE_MINOR_VERSION <= 74) {
srcDirs += "src/reactNativeVersionPatch/BorderRadiiDrawableUtils/74"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.facebook.jni.HybridData;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.RuntimeExecutor;
import com.facebook.react.common.annotations.FrameworkAPI;
import com.facebook.react.fabric.FabricUIManager;
import com.facebook.react.turbomodule.core.CallInvokerHolderImpl;
Expand All @@ -13,10 +12,14 @@
import com.swmansion.reanimated.layoutReanimation.LayoutAnimations;
import com.swmansion.reanimated.layoutReanimation.NativeMethodsHolder;
import com.swmansion.reanimated.nativeProxy.NativeProxyCommon;
import com.swmansion.worklets.JSCallInvokerResolver;
import com.swmansion.worklets.WorkletsModule;
import java.util.HashMap;
import java.util.Objects;

/**
* @noinspection JavaJniMissingFunction
*/
public class NativeProxy extends NativeProxyCommon {
@DoNotStrip
@SuppressWarnings("unused")
Expand All @@ -32,49 +35,30 @@ public class NativeProxy extends NativeProxyCommon {

LayoutAnimations LayoutAnimations = new LayoutAnimations(context);

if (context.isBridgeless()) {
RuntimeExecutor runtimeExecutor = context.getRuntimeExecutor();
mHybridData =
initHybridBridgeless(
workletsModule,
Objects.requireNonNull(context.getJavaScriptContextHolder()).get(),
runtimeExecutor,
mAndroidUIScheduler,
LayoutAnimations,
fabricUIManager);
} else {
CallInvokerHolderImpl callInvokerHolder =
(CallInvokerHolderImpl) context.getCatalystInstance().getJSCallInvokerHolder();
mHybridData =
initHybrid(
workletsModule,
Objects.requireNonNull(context.getJavaScriptContextHolder()).get(),
callInvokerHolder,
mAndroidUIScheduler,
LayoutAnimations,
fabricUIManager);
}
CallInvokerHolderImpl callInvokerHolder = JSCallInvokerResolver.getJSCallInvokerHolder(context);
mHybridData =
initHybrid(
workletsModule,
Objects.requireNonNull(context.getJavaScriptContextHolder()).get(),
callInvokerHolder,
LayoutAnimations,
context.isBridgeless(),
fabricUIManager);

prepareLayoutAnimations(LayoutAnimations);
installJSIBindings();
if (BuildConfig.DEBUG) {
checkCppVersion();
}
}

@OptIn(markerClass = FrameworkAPI.class)
private native HybridData initHybrid(
WorkletsModule workletsModule,
long jsContext,
CallInvokerHolderImpl jsCallInvokerHolder,
AndroidUIScheduler androidUIScheduler,
LayoutAnimations LayoutAnimations,
FabricUIManager fabricUIManager);

private native HybridData initHybridBridgeless(
WorkletsModule workletsModule,
long jsContext,
RuntimeExecutor runtimeExecutor,
AndroidUIScheduler androidUIScheduler,
LayoutAnimations LayoutAnimations,
boolean isBridgeless,
FabricUIManager fabricUIManager);

public native boolean isAnyHandlerWaitingForEvent(String eventName, int emitterReactTag);
Expand All @@ -86,7 +70,8 @@ protected HybridData getHybridData() {
return mHybridData;
}

public static NativeMethodsHolder createNativeMethodsHolder(LayoutAnimations layoutAnimations) {
public static NativeMethodsHolder createNativeMethodsHolder(
LayoutAnimations ignoredLayoutAnimations) {
return new NativeMethodsHolder() {
@Override
public void startAnimation(int tag, int type, HashMap<String, Object> values) {
Expand Down
Loading

0 comments on commit a885763

Please sign in to comment.