diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/AnimatedSensor/AnimatedSensorModule.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/AnimatedSensor/AnimatedSensorModule.cpp index 25e93ce6d39e..356cbfe765e2 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/AnimatedSensor/AnimatedSensorModule.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/AnimatedSensor/AnimatedSensorModule.cpp @@ -11,7 +11,7 @@ AnimatedSensorModule::AnimatedSensorModule(const PlatformDepMethodsHolder &platf platformUnregisterSensorFunction_(platformDepMethodsHolder.unregisterSensor) {} jsi::Value AnimatedSensorModule::registerSensor( - jsi::Runtime &rt, + jsi::Runtime &rnRuntime, const std::shared_ptr &uiWorkletRuntime, const jsi::Value &sensorTypeValue, const jsi::Value &interval, @@ -19,8 +19,11 @@ jsi::Value AnimatedSensorModule::registerSensor( const jsi::Value &sensorDataHandler) { SensorType sensorType = static_cast(sensorTypeValue.asNumber()); - auto serializableHandler = extractSerializableOrThrow( - rt, sensorDataHandler, "[Reanimated] Sensor event handler must be a worklet."); + auto serializableHandler = extractSerializable( + rnRuntime, + sensorDataHandler, + "[Reanimated] Sensor event handler must be a worklet.", + Serializable::ValueType::WorkletType); int sensorId = platformRegisterSensorFunction_( static_cast(sensorType), @@ -33,7 +36,7 @@ jsi::Value AnimatedSensorModule::registerSensor( return; } - jsi::Runtime &uiRuntime = uiWorkletRuntime->getJSIRuntime(); + jsi::Runtime &uiRuntime = getJSIRuntimeFromWorkletRuntime(uiWorkletRuntime); jsi::Object value(uiRuntime); if (sensorType == SensorType::ROTATION_VECTOR) { // TODO: timestamp should be provided by the platform implementation @@ -53,7 +56,7 @@ jsi::Value AnimatedSensorModule::registerSensor( } value.setProperty(uiRuntime, "interfaceOrientation", orientationDegrees); - uiWorkletRuntime->runSync(serializableHandler, value); + runSyncOnRuntime(uiWorkletRuntime, serializableHandler, jsi::Value(uiRuntime, value)); }); if (sensorId != -1) { sensorsIds_.insert(sensorId); diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/AnimatedSensor/AnimatedSensorModule.h b/packages/react-native-reanimated/Common/cpp/reanimated/AnimatedSensor/AnimatedSensorModule.h index 988fbbb2b4dd..75ec289f6011 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/AnimatedSensor/AnimatedSensorModule.h +++ b/packages/react-native-reanimated/Common/cpp/reanimated/AnimatedSensor/AnimatedSensorModule.h @@ -2,8 +2,7 @@ #include -#include -#include +#include #include @@ -32,8 +31,8 @@ class AnimatedSensorModule { explicit AnimatedSensorModule(const PlatformDepMethodsHolder &platformDepMethodsHolder); jsi::Value registerSensor( - jsi::Runtime &rt, - const std::shared_ptr &uiWorkletRuntime, + jsi::Runtime &rnRuntime, + const std::shared_ptr &uiRuntime, const jsi::Value &sensorType, const jsi::Value &interval, const jsi::Value &iosReferenceFrame, diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/CSS/common/values/CSSValueVariant.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/CSS/common/values/CSSValueVariant.cpp index f8afdb6ebd69..63af32c7f2b3 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/CSS/common/values/CSSValueVariant.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/CSS/common/values/CSSValueVariant.cpp @@ -13,7 +13,7 @@ #include #include -#include +#include #include #include @@ -41,7 +41,7 @@ CSSValueVariant::CSSValueVariant(jsi::Runtime &rt, const jsi::V // Try constructing with each allowed type until one succeeds if (!(tryOne.template operator()() || ...)) { throw std::runtime_error( - "[Reanimated] No compatible type found for construction from: " + worklets::stringifyJSIValue(rt, jsiValue)); + "[Reanimated] No compatible type found for construction from: " + worklets::JSIValueToStdString(rt, jsiValue)); } } diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/CSS/interpolation/PropertyInterpolator.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/CSS/interpolation/PropertyInterpolator.cpp index 34fd6922f490..3bfee7b82f0c 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/CSS/interpolation/PropertyInterpolator.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/CSS/interpolation/PropertyInterpolator.cpp @@ -1,6 +1,6 @@ #include -#include +#include #include #include @@ -35,7 +35,7 @@ std::vector> PropertyInterpolator::parseJSIKeyfram throw std::invalid_argument( "[Reanimated] Received invalid keyframes object for property: " + getPropertyPathString() + ".\n\nExpected an array of objects with 'offset' and 'value' properties, got: " + - worklets::stringifyJSIValue(rt, keyframes)); + worklets::JSIValueToStdString(rt, keyframes)); } const auto keyframeArray = keyframes.asObject(rt).asArray(rt); diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandler.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandler.cpp index 9263721c5e75..6bd328c4d0f9 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandler.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandler.cpp @@ -11,7 +11,7 @@ void UIEventHandler::process( const std::shared_ptr &uiRuntime, const double eventTimestamp, const jsi::Value &eventValue) const { - uiRuntime->runSync(handlerFunction_, jsi::Value(eventTimestamp), eventValue); + runSyncOnRuntime(uiRuntime, handlerFunction_, jsi::Value(eventTimestamp), eventValue); } uint64_t UIEventHandler::getHandlerId() const { diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandler.h b/packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandler.h index a2c703e01c79..b815e9260004 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandler.h +++ b/packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandler.h @@ -1,8 +1,7 @@ #pragma once #include -#include -#include +#include #include #include @@ -15,14 +14,14 @@ class UIEventHandler { const uint64_t handlerId_; const uint64_t emitterReactTag_; const std::string eventName_; - const std::shared_ptr handlerFunction_; + const std::shared_ptr handlerFunction_; public: UIEventHandler( const uint64_t handlerId, const std::string &eventName, const uint64_t emitterReactTag, - const std::shared_ptr &handlerFunction) + const std::shared_ptr &handlerFunction) : handlerId_(handlerId), emitterReactTag_(emitterReactTag), eventName_(eventName), diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandlerRegistry.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandlerRegistry.cpp index 431f4f47f70e..d70076993f20 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandlerRegistry.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandlerRegistry.cpp @@ -77,8 +77,9 @@ void UIEventHandlerRegistry::processEvent( } } - jsi::Runtime &rt = uiWorkletRuntime->getJSIRuntime(); - eventPayload.asObject(rt).setProperty(rt, "eventName", jsi::String::createFromUtf8(rt, eventName)); + jsi::Runtime &uiRuntime = getJSIRuntimeFromWorkletRuntime(uiWorkletRuntime); + eventPayload.asObject(uiRuntime).setProperty( + uiRuntime, "eventName", jsi::String::createFromUtf8(uiRuntime, eventName)); for (const auto &handler : handlersForEvent) { handler->process(uiWorkletRuntime, eventTimestamp, eventPayload); } diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandlerRegistry.h b/packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandlerRegistry.h index f92fce21b004..431b38b8d2bc 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandlerRegistry.h +++ b/packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandlerRegistry.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsManager.h b/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsManager.h index dd6b9aa040bf..de6c0ab06911 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsManager.h +++ b/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsManager.h @@ -4,7 +4,7 @@ #include #include -#include +#include #include #include diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxyCommon.h b/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxyCommon.h index 3efba79ad053..e6990108be76 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxyCommon.h +++ b/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxyCommon.h @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Experimental.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Experimental.cpp index 22b00a2c82a0..ba22e753391e 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Experimental.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Experimental.cpp @@ -538,7 +538,7 @@ void LayoutAnimationsProxy_Experimental::maybeCancelAnimation(const int tag) con return; } layoutAnimations_.erase(tag); - uiScheduler_->scheduleOnUI([weakThis = weak_from_this(), tag]() { + scheduleOnUI(uiScheduler_, [weakThis = weak_from_this(), tag]() { auto strongThis = weakThis.lock(); if (!strongThis) { return; @@ -679,8 +679,8 @@ void LayoutAnimationsProxy_Experimental::startEnteringAnimation(const std::share react_native_assert(parent && "Parent node is nullptr"); const auto parentTag = parent->current.tag; - uiScheduler_->scheduleOnUI( - [weakThis = weak_from_this(), finalView, currentView, newChildShadowView, parentTag, opacity]() { + scheduleOnUI( + uiScheduler_, [weakThis = weak_from_this(), finalView, currentView, newChildShadowView, parentTag, opacity]() { auto strongThis = weakThis.lock(); if (!strongThis) { return; @@ -724,7 +724,7 @@ void LayoutAnimationsProxy_Experimental::startExitingAnimation(const std::shared react_native_assert(parent && "Parent node is nullptr"); const auto parentTag = parent->current.tag; - uiScheduler_->scheduleOnUI([weakThis = weak_from_this(), tag, parentTag, oldChildShadowView, surfaceId]() { + scheduleOnUI(uiScheduler_, [weakThis = weak_from_this(), tag, parentTag, oldChildShadowView, surfaceId]() { auto strongThis = weakThis.lock(); if (!strongThis) { return; @@ -766,8 +766,8 @@ void LayoutAnimationsProxy_Experimental::startLayoutAnimation(const std::shared_ react_native_assert(parent && "Parent node is nullptr"); const auto parentTag = parent->current.tag; - uiScheduler_->scheduleOnUI( - [weakThis = weak_from_this(), surfaceId, oldChildShadowView, newChildShadowView, parentTag, tag]() { + scheduleOnUI( + uiScheduler_, [weakThis = weak_from_this(), surfaceId, oldChildShadowView, newChildShadowView, parentTag, tag]() { auto strongThis = weakThis.lock(); if (!strongThis) { return; @@ -811,7 +811,7 @@ void LayoutAnimationsProxy_Experimental::startSharedTransition( const ShadowView &before, const ShadowView &after, SurfaceId surfaceId) const { - uiScheduler_->scheduleOnUI([weakThis = weak_from_this(), before, after, surfaceId, tag]() { + scheduleOnUI(uiScheduler_, [weakThis = weak_from_this(), before, after, surfaceId, tag]() { auto strongThis = weakThis.lock(); if (!strongThis) { return; @@ -844,7 +844,7 @@ void LayoutAnimationsProxy_Experimental::startProgressTransition( const ShadowView &before, const ShadowView &after, SurfaceId surfaceId) const { - uiScheduler_->scheduleOnUI([weakThis = weak_from_this(), before, after, surfaceId]() { + scheduleOnUI(uiScheduler_, [weakThis = weak_from_this(), before, after, surfaceId]() { auto strongThis = weakThis.lock(); if (!strongThis) { return; diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Experimental.h b/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Experimental.h index 91875a5e9697..12420865bf40 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Experimental.h +++ b/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Experimental.h @@ -5,7 +5,7 @@ #include #include -#include +#include #include #include diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Legacy.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Legacy.cpp index 6a8315e6faec..f39ba96f8770 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Legacy.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Legacy.cpp @@ -636,7 +636,7 @@ void LayoutAnimationsProxy_Legacy::startEnteringAnimation(const int tag, ShadowV auto &viewProps = static_cast(*mutation.newChildShadowView.props); auto opacity = viewProps.opacity; - uiScheduler_->scheduleOnUI([weakThis = weak_from_this(), finalView, current, mutation, opacity, tag]() { + scheduleOnUI(uiScheduler_, [weakThis = weak_from_this(), finalView, current, mutation, opacity, tag]() { auto strongThis = weakThis.lock(); if (!strongThis) { return; @@ -675,7 +675,7 @@ void LayoutAnimationsProxy_Legacy::startExitingAnimation(const int tag, ShadowVi #endif auto surfaceId = mutation.oldChildShadowView.surfaceId; - uiScheduler_->scheduleOnUI([weakThis = weak_from_this(), tag, mutation, surfaceId]() { + scheduleOnUI(uiScheduler_, [weakThis = weak_from_this(), tag, mutation, surfaceId]() { auto strongThis = weakThis.lock(); if (!strongThis) { return; @@ -714,7 +714,7 @@ void LayoutAnimationsProxy_Legacy::startLayoutAnimation(const int tag, const Sha #endif auto surfaceId = mutation.oldChildShadowView.surfaceId; - uiScheduler_->scheduleOnUI([weakThis = weak_from_this(), mutation, surfaceId, tag]() { + scheduleOnUI(uiScheduler_, [weakThis = weak_from_this(), mutation, surfaceId, tag]() { auto strongThis = weakThis.lock(); if (!strongThis) { return; @@ -763,7 +763,7 @@ void LayoutAnimationsProxy_Legacy::maybeCancelAnimation(const int tag) const { return; } layoutAnimations_.erase(tag); - uiScheduler_->scheduleOnUI([weakThis = weak_from_this(), tag]() { + scheduleOnUI(uiScheduler_, [weakThis = weak_from_this(), tag]() { auto strongThis = weakThis.lock(); if (!strongThis) { return; diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Legacy.h b/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Legacy.h index 5cef0f8ea595..bcd0b801fd19 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Legacy.h +++ b/packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy_Legacy.h @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp index a8d623b48c01..d5ba6c01e527 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp @@ -8,7 +8,7 @@ #include #include #include -#include +#include #ifdef __ANDROID__ #include @@ -172,7 +172,7 @@ void ReanimatedModuleProxy::init(const PlatformDepMethodsHolder &platformDepMeth return strongThis->obtainProp(rt, shadowNodeWrapper, propName); }; - jsi::Runtime &uiRuntime = uiRuntime_->getJSIRuntime(); + jsi::Runtime &uiRuntime = getJSIRuntimeFromWorkletRuntime(uiRuntime_); UIRuntimeDecorator::decorate( uiRuntime, obtainProp, @@ -201,11 +201,11 @@ jsi::Value ReanimatedModuleProxy::registerEventHandler( uint64_t newRegistrationId = NEXT_EVENT_HANDLER_ID++; auto eventNameStr = eventName.asString(rt).utf8(rt); - auto handlerSerializable = extractSerializableOrThrow( - rt, worklet, "[Reanimated] Event handler must be a serializable worklet."); + auto handlerSerializable = extractSerializable( + rt, worklet, "[Reanimated] Event handler must be a serializable worklet.", Serializable::ValueType::WorkletType); int emitterReactTagInt = emitterReactTag.asNumber(); - uiScheduler_->scheduleOnUI([=, weakThis = weak_from_this()]() { + scheduleOnUI(uiScheduler_, [=, weakThis = weak_from_this()]() { auto strongThis = weakThis.lock(); if (!strongThis) { return; @@ -220,7 +220,7 @@ jsi::Value ReanimatedModuleProxy::registerEventHandler( void ReanimatedModuleProxy::unregisterEventHandler(jsi::Runtime &, const jsi::Value ®istrationId) { uint64_t id = registrationId.asNumber(); - uiScheduler_->scheduleOnUI([=, weakThis = weak_from_this()]() { + scheduleOnUI(uiScheduler_, [=, weakThis = weak_from_this()]() { auto strongThis = weakThis.lock(); if (!strongThis) { return; @@ -246,12 +246,12 @@ jsi::Value ReanimatedModuleProxy::getViewProp( const auto propNameStr = propName.asString(rnRuntime).utf8(rnRuntime); const auto funPtr = std::make_shared(callback.getObject(rnRuntime).asFunction(rnRuntime)); const auto shadowNode = shadowNodeFromValue(rnRuntime, shadowNodeWrapper); - uiScheduler_->scheduleOnUI([=, weakThis = weak_from_this()]() { + scheduleOnUI(uiScheduler_, [=, weakThis = weak_from_this()]() { auto strongThis = weakThis.lock(); if (!strongThis) { return; } - jsi::Runtime &uiRuntime = strongThis->uiRuntime_->getJSIRuntime(); + jsi::Runtime &uiRuntime = getJSIRuntimeFromWorkletRuntime(strongThis->uiRuntime_); const auto resultStr = strongThis->obtainPropFromShadowNode(uiRuntime, propNameStr, shadowNode); strongThis->jsInvoker_->invokeAsync([=](jsi::Runtime &rnRuntime) { @@ -287,8 +287,8 @@ jsi::Value ReanimatedModuleProxy::configureLayoutAnimationBatch( if (config.isUndefined()) { batchItem.config = nullptr; } else { - batchItem.config = extractSerializableOrThrow( - rt, config, "[Reanimated] Layout animation config must be an object."); + batchItem.config = extractSerializable( + rt, config, "[Reanimated] Layout animation config must be an object.", Serializable::ValueType::ObjectType); } auto sharedTag = item.getProperty(rt, "sharedTransitionTag"); if (!sharedTag.isUndefined()) { @@ -506,7 +506,7 @@ bool ReanimatedModuleProxy::handleRawEvent(const RawEvent &rawEvent, double curr if constexpr (StaticFeatureFlags::getFlag("ENABLE_SHARED_ELEMENT_TRANSITIONS")) { if (eventType == "onTransitionProgress") { - jsi::Runtime &uiRuntime = uiRuntime_->getJSIRuntime(); + jsi::Runtime &uiRuntime = getJSIRuntimeFromWorkletRuntime(uiRuntime_); const auto &eventPayload = rawEvent.eventPayload; jsi::Object payload = eventPayload->asJSIValue(uiRuntime).asObject(uiRuntime); auto progress = payload.getProperty(uiRuntime, "progress").asNumber(); @@ -543,7 +543,7 @@ bool ReanimatedModuleProxy::handleRawEvent(const RawEvent &rawEvent, double curr return false; } - jsi::Runtime &uiRuntime = uiRuntime_->getJSIRuntime(); + jsi::Runtime &uiRuntime = getJSIRuntimeFromWorkletRuntime(uiRuntime_); const auto &eventPayload = rawEvent.eventPayload; jsi::Value payload = eventPayload->asJSIValue(uiRuntime); @@ -580,7 +580,7 @@ void ReanimatedModuleProxy::maybeRunCSSLoop() { cssLoopRunning_ = true; - uiScheduler_->scheduleOnUI([=, weakThis = weak_from_this()]() { + scheduleOnUI(uiScheduler_, [=, weakThis = weak_from_this()]() { auto strongThis = weakThis.lock(); if (!strongThis) { return; @@ -612,7 +612,7 @@ void ReanimatedModuleProxy::performOperations(const bool isTriggeredByEvent) { } } - jsi::Runtime &uiRuntime = uiRuntime_->getJSIRuntime(); + jsi::Runtime &uiRuntime = getJSIRuntimeFromWorkletRuntime(uiRuntime_); UpdatesBatch updatesBatch; { @@ -1213,7 +1213,7 @@ void ReanimatedModuleProxy::dispatchCommand( jsi::String ReanimatedModuleProxy::obtainProp(jsi::Runtime &rt, const jsi::Value &shadowNodeWrapper, const jsi::Value &propName) { - jsi::Runtime &uiRuntime = uiRuntime_->getJSIRuntime(); + jsi::Runtime &uiRuntime = getJSIRuntimeFromWorkletRuntime(uiRuntime_); const auto propNameStr = propName.asString(rt).utf8(rt); const auto shadowNode = shadowNodeFromValue(rt, shadowNodeWrapper); const auto resultStr = obtainPropFromShadowNode(uiRuntime, propNameStr, shadowNode); @@ -1283,7 +1283,7 @@ void ReanimatedModuleProxy::initializeLayoutAnimationsProxy() { layoutAnimationsManager_, componentDescriptorRegistry, scheduler->getContextContainer(), - uiRuntime_->getJSIRuntime(), + getJSIRuntimeFromWorkletRuntime(uiRuntime_), uiScheduler_ #ifdef ANDROID , @@ -1301,7 +1301,7 @@ void ReanimatedModuleProxy::initializeLayoutAnimationsProxy() { layoutAnimationsManager_, componentDescriptorRegistry, scheduler->getContextContainer(), - uiRuntime_->getJSIRuntime(), + getJSIRuntimeFromWorkletRuntime(uiRuntime_), uiScheduler_ #ifdef ANDROID , @@ -1348,15 +1348,18 @@ jsi::Value ReanimatedModuleProxy::subscribeForKeyboardEvents( const jsi::Value &handlerWorklet, const jsi::Value &isStatusBarTranslucent, const jsi::Value &isNavigationBarTranslucent) { - auto serializableHandler = extractSerializableOrThrow( - rt, handlerWorklet, "[Reanimated] Keyboard event handler must be a worklet."); + auto serializableHandler = extractSerializable( + rt, + handlerWorklet, + "[Reanimated] Keyboard event handler must be a worklet.", + Serializable::ValueType::WorkletType); return subscribeForKeyboardEventsFunction_( [=, weakThis = weak_from_this()](int keyboardState, int height) { auto strongThis = weakThis.lock(); if (!strongThis) { return; } - strongThis->uiRuntime_->runSync(serializableHandler, jsi::Value(keyboardState), jsi::Value(height)); + runSyncOnRuntime(strongThis->uiRuntime_, serializableHandler, jsi::Value(keyboardState), jsi::Value(height)); }, isStatusBarTranslucent.getBool(), isNavigationBarTranslucent.getBool()); diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.h b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.h index 23f018698603..d200360655e7 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.h +++ b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.h @@ -24,8 +24,7 @@ #include #include #include -#include -#include +#include #include #include diff --git a/packages/react-native-reanimated/android/src/main/cpp/reanimated/android/NativeProxy.cpp b/packages/react-native-reanimated/android/src/main/cpp/reanimated/android/NativeProxy.cpp index 9c500ab0f8e1..0b4d0e0c8221 100644 --- a/packages/react-native-reanimated/android/src/main/cpp/reanimated/android/NativeProxy.cpp +++ b/packages/react-native-reanimated/android/src/main/cpp/reanimated/android/NativeProxy.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include @@ -71,16 +71,11 @@ jni::local_ref NativeProxy::initHybrid( auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker(); auto &rnRuntime = *reinterpret_cast(jsContext); // NOLINT //(performance-no-int-to-ptr) const auto global = rnRuntime.global(); - const auto uiRuntime = - std::static_pointer_cast( - global.getProperty(rnRuntime, "__UI_WORKLET_RUNTIME_HOLDER").asObject(rnRuntime).getNativeState(rnRuntime)) - ->runtime_; + getWorkletRuntimeFromHolder(rnRuntime, global.getPropertyAsObject(rnRuntime, "__UI_WORKLET_RUNTIME_HOLDER")); const auto uiScheduler = - std::static_pointer_cast( - global.getProperty(rnRuntime, "__UI_SCHEDULER_HOLDER").asObject(rnRuntime).getNativeState(rnRuntime)) - ->scheduler_; + getUISchedulerFromHolder(rnRuntime, global.getPropertyAsObject(rnRuntime, "__UI_SCHEDULER_HOLDER")); return makeCxxInstance(jThis, &rnRuntime, jsCallInvoker, fabricUIManager, uiRuntime, uiScheduler); } @@ -120,7 +115,8 @@ void NativeProxy::injectCppVersion() { void NativeProxy::installJSIBindings() { jsi::Runtime &rnRuntime = *rnRuntime_; - RNRuntimeDecorator::decorate(rnRuntime, uiRuntime_->getJSIRuntime(), reanimatedModuleProxy_); + auto &uiRuntime = getJSIRuntimeFromWorkletRuntime(uiRuntime_); + RNRuntimeDecorator::decorate(rnRuntime, uiRuntime, reanimatedModuleProxy_); } bool NativeProxy::isAnyHandlerWaitingForEvent(const std::string &eventName, const int emitterReactTag) { @@ -258,7 +254,7 @@ void NativeProxy::handleEvent( return; } - auto &uiRuntime = uiRuntime_->getJSIRuntime(); + auto &uiRuntime = getJSIRuntimeFromWorkletRuntime(uiRuntime_); jsi::Value payload; try { payload = jsi::Value::createFromJsonUtf8(uiRuntime, reinterpret_cast(&eventJSON[0]), eventJSON.size()); diff --git a/packages/react-native-reanimated/android/src/main/cpp/reanimated/android/NativeProxy.h b/packages/react-native-reanimated/android/src/main/cpp/reanimated/android/NativeProxy.h index 6d1577606956..1b8a2252df10 100644 --- a/packages/react-native-reanimated/android/src/main/cpp/reanimated/android/NativeProxy.h +++ b/packages/react-native-reanimated/android/src/main/cpp/reanimated/android/NativeProxy.h @@ -7,8 +7,7 @@ #include #include #include -#include -#include +#include #include #include diff --git a/packages/react-native-reanimated/apple/reanimated/apple/ReanimatedModule.mm b/packages/react-native-reanimated/apple/reanimated/apple/ReanimatedModule.mm index ef9f44efa72a..97ff0a31c753 100644 --- a/packages/react-native-reanimated/apple/reanimated/apple/ReanimatedModule.mm +++ b/packages/react-native-reanimated/apple/reanimated/apple/ReanimatedModule.mm @@ -11,7 +11,7 @@ #import #import -#import +#import using namespace facebook::react; using namespace reanimated; @@ -161,8 +161,7 @@ - (void)checkBridgeless auto reanimatedModuleProxy = reanimated::createReanimatedModuleProxy( _nodesManager, _moduleRegistry, rnRuntime, jsCallInvoker, uiWorkletRuntime, uiScheduler); - auto &uiRuntime = uiWorkletRuntime->getJSIRuntime(); - + auto &uiRuntime = getJSIRuntimeFromWorkletRuntime(uiWorkletRuntime); RNRuntimeDecorator::decorate(rnRuntime, uiRuntime, reanimatedModuleProxy); [self attachReactEventListener:reanimatedModuleProxy]; @@ -188,20 +187,16 @@ - (void)checkBridgeless - (std::shared_ptr)getUIRuntime:(jsi::Runtime &)rnRuntime { const auto global = rnRuntime.global(); - const auto uiRuntime = global.getProperty(rnRuntime, "__UI_WORKLET_RUNTIME_HOLDER") - .asObject(rnRuntime) - .getNativeState(rnRuntime) - ->runtime_; + const auto uiRuntime = + getWorkletRuntimeFromHolder(rnRuntime, global.getPropertyAsObject(rnRuntime, "__UI_WORKLET_RUNTIME_HOLDER")); return uiRuntime; } - (std::shared_ptr)getUIScheduler:(jsi::Runtime &)rnRuntime { const auto global = rnRuntime.global(); - const auto uiScheduler = global.getProperty(rnRuntime, "__UI_SCHEDULER_HOLDER") - .asObject(rnRuntime) - .getNativeState(rnRuntime) - ->scheduler_; + const auto uiScheduler = + getUISchedulerFromHolder(rnRuntime, global.getPropertyAsObject(rnRuntime, "__UI_SCHEDULER_HOLDER")); return uiScheduler; } diff --git a/packages/react-native-reanimated/apple/reanimated/apple/native/NativeProxy.h b/packages/react-native-reanimated/apple/reanimated/apple/native/NativeProxy.h index 60f8c62c79b3..e571568081c2 100644 --- a/packages/react-native-reanimated/apple/reanimated/apple/native/NativeProxy.h +++ b/packages/react-native-reanimated/apple/reanimated/apple/native/NativeProxy.h @@ -2,8 +2,7 @@ #import #import -#import -#import +#import namespace reanimated { diff --git a/packages/react-native-reanimated/apple/reanimated/apple/native/NativeProxy.mm b/packages/react-native-reanimated/apple/reanimated/apple/native/NativeProxy.mm index cd2d490bf767..9d4b0b8b92a9 100644 --- a/packages/react-native-reanimated/apple/reanimated/apple/native/NativeProxy.mm +++ b/packages/react-native-reanimated/apple/reanimated/apple/native/NativeProxy.mm @@ -31,7 +31,7 @@ - (void *)runtime; uiWorkletRuntime, uiScheduler, rnRuntime, jsInvoker, platformDepMethodsHolder, getIsReducedMotion()); reanimatedModuleProxy->init(platformDepMethodsHolder); - jsi::Runtime &uiRuntime = uiWorkletRuntime->getJSIRuntime(); + auto &uiRuntime = getJSIRuntimeFromWorkletRuntime(uiWorkletRuntime); [nodesManager registerEventHandler:^(id event) { // handles RCTEvents from RNGestureHandler diff --git a/packages/react-native-worklets/Common/cpp/worklets/Compat/StableApi.cpp b/packages/react-native-worklets/Common/cpp/worklets/Compat/StableApi.cpp new file mode 100644 index 000000000000..a2ce71336de0 --- /dev/null +++ b/packages/react-native-worklets/Common/cpp/worklets/Compat/StableApi.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace worklets { + +std::string JSIValueToStdString(facebook::jsi::Runtime &rt, const facebook::jsi::Value &value) { + return worklets::stringifyJSIValue(rt, value); +} + +void scheduleOnUI(const std::shared_ptr &uiScheduler, const std::function &job) { + uiScheduler->scheduleOnUI(job); +} + +facebook::jsi::Runtime &getJSIRuntimeFromWorkletRuntime(const std::shared_ptr &workletRuntime) { + return workletRuntime->getJSIRuntime(); +} + +std::weak_ptr getWeakRuntimeFromJSIRuntime(jsi::Runtime &rt) { + return WorkletRuntime::getWeakRuntimeFromJSIRuntime(rt); +} + +std::shared_ptr +extractSerializable(facebook::jsi::Runtime &rt, const facebook::jsi::Value &value, const std::string &errorMessage) { + return extractSerializableOrThrow(rt, value, errorMessage); +} + +std::shared_ptr extractSerializable( + facebook::jsi::Runtime &rt, + const facebook::jsi::Value &value, + const std::string &errorMessage, + const Serializable::ValueType expectedType) { + switch (expectedType) { + case Serializable::ValueType::WorkletType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::UndefinedType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::NullType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::BooleanType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::NumberType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::BigIntType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::StringType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::ObjectType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::ArrayType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::MapType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::SetType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::RemoteFunctionType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::HandleType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::HostObjectType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::HostFunctionType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::ArrayBufferType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::TurboModuleLikeType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::ImportType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::SynchronizableType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::CustomType: + return extractSerializableOrThrow(rt, value, errorMessage); + case Serializable::ValueType::SymbolType: + throw std::runtime_error("[Worklets] Not implemented."); + case Serializable::ValueType::ShareableType: + throw std::runtime_error("[Worklets] Not implemented."); + default: + throw std::runtime_error("[Worklets] Invalid expected type provided to extractSerializable."); + } +} + +void runSyncOnRuntime( + const std::shared_ptr &workletRuntime, + const std::shared_ptr &worklet) { + workletRuntime->runSync(std::static_pointer_cast(worklet)); +} + +void runSyncOnRuntime( + const std::shared_ptr &workletRuntime, + const std::shared_ptr &worklet, + const facebook::jsi::Value &arg0) { + workletRuntime->runSync(std::static_pointer_cast(worklet), arg0); +} + +void runSyncOnRuntime( + const std::shared_ptr &workletRuntime, + const std::shared_ptr &worklet, + const facebook::jsi::Value &arg0, + const facebook::jsi::Value &arg1) { + workletRuntime->runSync(std::static_pointer_cast(worklet), arg0, arg1); +} + +std::shared_ptr getWorkletRuntimeFromHolder( + facebook::jsi::Runtime &rt, + const facebook::jsi::Object &object) { + return object.getNativeState(rt)->runtime_; +} + +std::shared_ptr getUISchedulerFromHolder(facebook::jsi::Runtime &rt, const facebook::jsi::Object &object) { + return object.getNativeState(rt)->scheduler_; +} + +} // namespace worklets diff --git a/packages/react-native-worklets/Common/cpp/worklets/Compat/StableApi.h b/packages/react-native-worklets/Common/cpp/worklets/Compat/StableApi.h new file mode 100644 index 000000000000..bc48e7c7c30f --- /dev/null +++ b/packages/react-native-worklets/Common/cpp/worklets/Compat/StableApi.h @@ -0,0 +1,102 @@ +#pragma once + +#include +#include +#include +#include + +namespace facebook::jsi { +class Runtime; +class Value; +class Object; +} // namespace facebook::jsi + +namespace worklets { + +class WorkletRuntime; +class UIScheduler; + +class Serializable { + public: + virtual facebook::jsi::Value toJSValue(facebook::jsi::Runtime &rt) = 0; + + virtual ~Serializable(); + + enum class ValueType : std::uint8_t { + UndefinedType, + NullType, + BooleanType, + NumberType, + BigIntType, + StringType, + ObjectType, + ArrayType, + MapType, + SetType, + WorkletType, + RemoteFunctionType, + HandleType, + HostObjectType, + HostFunctionType, + ArrayBufferType, + TurboModuleLikeType, + ImportType, + SynchronizableType, + CustomType, + SymbolType, /* unused */ + ShareableType + }; + + explicit Serializable(ValueType valueType) : valueType_(valueType) {} + + inline ValueType valueType() const { + return valueType_; + } + + static std::shared_ptr undefined(); + + protected: + ValueType valueType_; +}; + +extern std::shared_ptr getWorkletRuntimeFromHolder( + facebook::jsi::Runtime &rt, + const facebook::jsi::Object &object); + +extern std::shared_ptr getUISchedulerFromHolder( + facebook::jsi::Runtime &rt, + const facebook::jsi::Object &object); + +extern facebook::jsi::Runtime &getJSIRuntimeFromWorkletRuntime(const std::shared_ptr &workletRuntime); + +extern std::weak_ptr getWeakRuntimeFromJSIRuntime(facebook::jsi::Runtime &rt); + +extern void scheduleOnUI(const std::shared_ptr &uiScheduler, const std::function &job); + +extern std::string JSIValueToStdString(facebook::jsi::Runtime &rt, const facebook::jsi::Value &value); + +extern std::shared_ptr +extractSerializable(facebook::jsi::Runtime &rt, const facebook::jsi::Value &value, const std::string &errorMessage); + +extern std::shared_ptr extractSerializable( + facebook::jsi::Runtime &rt, + const facebook::jsi::Value &value, + const std::string &errorMessage, + const Serializable::ValueType expectedType); + +extern void runSyncOnRuntime( + const std::shared_ptr &workletRuntime, + const std::shared_ptr &worklet); + +extern void runSyncOnRuntime( + const std::shared_ptr &workletRuntime, + const std::shared_ptr &worklet, + const facebook::jsi::Value &arg0); + +extern void runSyncOnRuntime( + const std::shared_ptr &workletRuntime, + const std::shared_ptr &worklet, + const facebook::jsi::Value &arg0, + const facebook::jsi::Value &arg1); + +} // namespace worklets diff --git a/packages/react-native-worklets/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.cpp b/packages/react-native-worklets/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.cpp index 6065ea697004..7dcf706a1770 100644 --- a/packages/react-native-worklets/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.cpp +++ b/packages/react-native-worklets/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -619,8 +620,13 @@ jsi::Value JSIWorkletsModuleProxy::get(jsi::Runtime &rt, const jsi::PropNameID & 0, [uiWorkletRuntime = uiWorkletRuntime_]( jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) { + const auto strongUIWorkletRuntime = uiWorkletRuntime.lock(); + if (!strongUIWorkletRuntime) { + throw jsi::JSError(rt, "[Worklets] UI Worklet Runtime is not available."); + } + auto nativeState = std::make_shared(strongUIWorkletRuntime); auto obj = jsi::Object(rt); - obj.setNativeState(rt, std::make_shared(uiWorkletRuntime.lock())); + obj.setNativeState(rt, std::move(nativeState)); return obj; }); } @@ -632,8 +638,9 @@ jsi::Value JSIWorkletsModuleProxy::get(jsi::Runtime &rt, const jsi::PropNameID & 0, [uiScheduler = uiScheduler_]( jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) { + auto nativeState = std::make_shared(uiScheduler); auto obj = jsi::Object(rt); - obj.setNativeState(rt, std::make_shared(uiScheduler)); + obj.setNativeState(rt, std::move(nativeState)); return obj; }); } diff --git a/packages/react-native-worklets/Common/cpp/worklets/SharedItems/Serializable.h b/packages/react-native-worklets/Common/cpp/worklets/SharedItems/Serializable.h index 085e3a8e2141..1c20158074ff 100644 --- a/packages/react-native-worklets/Common/cpp/worklets/SharedItems/Serializable.h +++ b/packages/react-native-worklets/Common/cpp/worklets/SharedItems/Serializable.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -39,49 +40,6 @@ inline void cleanupIfRuntimeExists(jsi::Runtime *rt, std::unique_ptr } } -class Serializable { - public: - virtual jsi::Value toJSValue(jsi::Runtime &rt) = 0; - - virtual ~Serializable(); - - enum class ValueType : std::uint8_t { - UndefinedType, - NullType, - BooleanType, - NumberType, - BigIntType, - StringType, - ObjectType, - ArrayType, - MapType, - SetType, - WorkletType, - RemoteFunctionType, - HandleType, - HostObjectType, - HostFunctionType, - ArrayBufferType, - TurboModuleLikeType, - ImportType, - SynchronizableType, - CustomType, - SymbolType, /* unused */ - ShareableType, - }; - - explicit Serializable(ValueType valueType) : valueType_(valueType) {} - - inline ValueType valueType() const { - return valueType_; - } - - static std::shared_ptr undefined(); - - protected: - ValueType valueType_; -}; - template class RetainingSerializable : virtual public BaseClass { private: diff --git a/packages/react-native-worklets/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp b/packages/react-native-worklets/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp index e0032ab960ce..0102f6bd7005 100644 --- a/packages/react-native-worklets/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +++ b/packages/react-native-worklets/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp @@ -265,8 +265,8 @@ void scheduleOnRuntime( workletRuntime->schedule(serializableWorklet); } -#if REACT_NATIVE_MINOR_VERSION >= 81 std::weak_ptr WorkletRuntime::getWeakRuntimeFromJSIRuntime(jsi::Runtime &rt) { +#if REACT_NATIVE_MINOR_VERSION >= 81 auto runtimeData = rt.getRuntimeData(RuntimeData::weakRuntimeUUID); if (!runtimeData) [[unlikely]] { throw std::runtime_error( @@ -275,8 +275,11 @@ std::weak_ptr WorkletRuntime::getWeakRuntimeFromJSIRuntime(jsi:: } auto weakHolder = std::static_pointer_cast(runtimeData); return weakHolder->weakRuntime; -} +#else + throw std::runtime_error( + "[Worklets] Retrieving WorkletRuntime from JSI Runtime is not supported in React Native versions below 0.81."); #endif // REACT_NATIVE_MINOR_VERSION >= 81 +} /* #region deprecated */ diff --git a/packages/react-native-worklets/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h b/packages/react-native-worklets/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h index f02335ffc338..3099eb531414 100644 --- a/packages/react-native-worklets/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h +++ b/packages/react-native-worklets/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h @@ -158,15 +158,15 @@ class WorkletRuntime : public jsi::HostObject, public std::enable_shared_from_th /* #endregion */ -#if REACT_NATIVE_MINOR_VERSION >= 81 /** * Retrieves a weak reference to the WorkletRuntime associated with the * provided jsi::Runtime. * * Throws when invoked with a non-worklet runtime. + * + * Available only on React Native 0.81 and higher. */ static std::weak_ptr getWeakRuntimeFromJSIRuntime(jsi::Runtime &rt); -#endif // REACT_NATIVE_MINOR_VERSION >= 81 #ifndef NDEBUG static jsi::Function getCallGuard(jsi::Runtime &rt);