From 73a876dfcbbf579c4e136aa3d2c1011e8a4aecd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= <40713406+tjzel@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:26:14 +0100 Subject: [PATCH] refactor(Worklets): move PlatformLogger (#6794) ## Summary `PlatformLogger` remained in Reanimated during early steps of establishing Worklets Module due to how difficult it was to move it back then. Now it's very simple, so let's do it. ## Test plan See in app if `LogExample` works. --- .../NativeModules/ReanimatedModuleProxy.cpp | 2 -- .../ReanimatedWorkletRuntimeDecorator.cpp | 19 ------------------- .../ReanimatedWorkletRuntimeDecorator.h | 14 -------------- .../Tools/PlatformLogger.h | 4 ++-- .../WorkletRuntimeDecorator.cpp | 6 ++++++ .../android/PlatformLogger.cpp | 8 ++++---- .../apple}/PlatformLogger.mm | 6 +++--- 7 files changed, 15 insertions(+), 44 deletions(-) delete mode 100644 packages/react-native-reanimated/Common/cpp/reanimated/RuntimeDecorators/ReanimatedWorkletRuntimeDecorator.cpp delete mode 100644 packages/react-native-reanimated/Common/cpp/reanimated/RuntimeDecorators/ReanimatedWorkletRuntimeDecorator.h rename packages/react-native-reanimated/Common/cpp/{reanimated => worklets}/Tools/PlatformLogger.h (83%) rename packages/react-native-reanimated/android/src/main/cpp/{reanimated => worklets}/android/PlatformLogger.cpp (78%) rename packages/react-native-reanimated/apple/{reanimated/apple/native => worklets/apple}/PlatformLogger.mm (82%) 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 2be3ac637e8..77f6d501aef 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -242,7 +241,6 @@ jsi::Value ReanimatedModuleProxy::createWorkletRuntime( auto initializerShareable = extractShareableOrThrow( rt, initializer, "[Reanimated] Initializer must be a worklet."); workletRuntime->runGuarded(initializerShareable); - ReanimatedWorkletRuntimeDecorator::decorate(workletRuntime->getJSIRuntime()); return jsi::Object::createFromHostObject(rt, workletRuntime); } diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/RuntimeDecorators/ReanimatedWorkletRuntimeDecorator.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/RuntimeDecorators/ReanimatedWorkletRuntimeDecorator.cpp deleted file mode 100644 index acd03e73677..00000000000 --- a/packages/react-native-reanimated/Common/cpp/reanimated/RuntimeDecorators/ReanimatedWorkletRuntimeDecorator.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include - -#include -#include -#include - -namespace reanimated { - -using namespace worklets; - -void ReanimatedWorkletRuntimeDecorator::decorate(jsi::Runtime &rt) { - jsi_utils::installJsiFunction( - rt, "_log", [](jsi::Runtime &rt, const jsi::Value &value) { - PlatformLogger::log(stringifyJSIValue(rt, value)); - }); -} - -} // namespace reanimated diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/RuntimeDecorators/ReanimatedWorkletRuntimeDecorator.h b/packages/react-native-reanimated/Common/cpp/reanimated/RuntimeDecorators/ReanimatedWorkletRuntimeDecorator.h deleted file mode 100644 index da7f2a923cd..00000000000 --- a/packages/react-native-reanimated/Common/cpp/reanimated/RuntimeDecorators/ReanimatedWorkletRuntimeDecorator.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include - -using namespace facebook; - -namespace reanimated { - -class ReanimatedWorkletRuntimeDecorator { - public: - static void decorate(jsi::Runtime &rt); -}; - -} // namespace reanimated diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/Tools/PlatformLogger.h b/packages/react-native-reanimated/Common/cpp/worklets/Tools/PlatformLogger.h similarity index 83% rename from packages/react-native-reanimated/Common/cpp/reanimated/Tools/PlatformLogger.h rename to packages/react-native-reanimated/Common/cpp/worklets/Tools/PlatformLogger.h index 37db18e3326..de58cb93098 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/Tools/PlatformLogger.h +++ b/packages/react-native-reanimated/Common/cpp/worklets/Tools/PlatformLogger.h @@ -2,7 +2,7 @@ #include -namespace reanimated { +namespace worklets { class PlatformLogger { public: @@ -13,4 +13,4 @@ class PlatformLogger { static void log(const bool b); }; -} // namespace reanimated +} // namespace worklets diff --git a/packages/react-native-reanimated/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp b/packages/react-native-reanimated/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp index 07caee39767..4d677927155 100644 --- a/packages/react-native-reanimated/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp +++ b/packages/react-native-reanimated/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -76,6 +77,11 @@ void WorkletRuntimeDecorator::decorate( evalWithSourceUrl)); #endif // NDEBUG + jsi_utils::installJsiFunction( + rt, "_log", [](jsi::Runtime &rt, const jsi::Value &value) { + PlatformLogger::log(stringifyJSIValue(rt, value)); + }); + jsi_utils::installJsiFunction( rt, "_toString", [](jsi::Runtime &rt, const jsi::Value &value) { return jsi::String::createFromUtf8(rt, stringifyJSIValue(rt, value)); diff --git a/packages/react-native-reanimated/android/src/main/cpp/reanimated/android/PlatformLogger.cpp b/packages/react-native-reanimated/android/src/main/cpp/worklets/android/PlatformLogger.cpp similarity index 78% rename from packages/react-native-reanimated/android/src/main/cpp/reanimated/android/PlatformLogger.cpp rename to packages/react-native-reanimated/android/src/main/cpp/worklets/android/PlatformLogger.cpp index 03495d35a03..e931aa904a4 100644 --- a/packages/react-native-reanimated/android/src/main/cpp/reanimated/android/PlatformLogger.cpp +++ b/packages/react-native-reanimated/android/src/main/cpp/worklets/android/PlatformLogger.cpp @@ -1,10 +1,10 @@ #include -#include +#include -constexpr const auto tag = "Reanimated"; +constexpr const auto tag = "Worklets"; -namespace reanimated { +namespace worklets { void PlatformLogger::log(const char *str) { __android_log_print(ANDROID_LOG_VERBOSE, tag, "%s", str); @@ -26,4 +26,4 @@ void PlatformLogger::log(const bool b) { log(b ? "true" : "false"); } -} // namespace reanimated +} // namespace worklets diff --git a/packages/react-native-reanimated/apple/reanimated/apple/native/PlatformLogger.mm b/packages/react-native-reanimated/apple/worklets/apple/PlatformLogger.mm similarity index 82% rename from packages/react-native-reanimated/apple/reanimated/apple/native/PlatformLogger.mm rename to packages/react-native-reanimated/apple/worklets/apple/PlatformLogger.mm index bca68ec2dd3..129180e05c5 100644 --- a/packages/react-native-reanimated/apple/reanimated/apple/native/PlatformLogger.mm +++ b/packages/react-native-reanimated/apple/worklets/apple/PlatformLogger.mm @@ -1,7 +1,7 @@ #import -#import +#import -namespace reanimated { +namespace worklets { void PlatformLogger::log(const char *str) { @@ -28,4 +28,4 @@ log(b ? "true" : "false"); } -} // namespace reanimated +} // namespace worklets