From 3ddf8bf22a38c43618274708f6ff6414f95ca1cb Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Mon, 19 Apr 2021 16:45:54 +0200 Subject: [PATCH 01/12] Connect to flipper --- android/src/main/JNI/Android.mk | 2 +- android/src/main/JNI/Application.mk | 4 +- android/src/main/JNI/react/Android.mk | 2 +- android/src/main/cpp/NativeProxy.cpp | 61 +++++++++++++++++-- android/src/main/cpp/headers/NativeProxy.h | 7 ++- .../com/swmansion/reanimated/NativeProxy.java | 22 ++++++- 6 files changed, 86 insertions(+), 12 deletions(-) diff --git a/android/src/main/JNI/Android.mk b/android/src/main/JNI/Android.mk index c9cc21c0cdd..7bf588e811a 100644 --- a/android/src/main/JNI/Android.mk +++ b/android/src/main/JNI/Android.mk @@ -22,7 +22,7 @@ LOCAL_C_INCLUDES += $(LOCAL_PATH)/../Common/cpp/headers/SharedItems LOCAL_C_INCLUDES += $(LOCAL_PATH)/../Common/cpp/headers/SpecTools LOCAL_C_INCLUDES += $(LOCAL_PATH)/../Common/cpp/headers/Tools -LOCAL_CFLAGS += -DONANDROID -fexceptions -frtti +LOCAL_CFLAGS += -DONANDROID -fexceptions -frtti -DHERMES_ENABLE_DEBUGGER=1 LOCAL_STATIC_LIBRARIES := libjsi callinvokerholder LOCAL_SHARED_LIBRARIES := libfolly_json libfbjni libreactnativejni diff --git a/android/src/main/JNI/Application.mk b/android/src/main/JNI/Application.mk index b90e0e20cd5..1218e96f7fc 100644 --- a/android/src/main/JNI/Application.mk +++ b/android/src/main/JNI/Application.mk @@ -1,7 +1,7 @@ ifdef $(CI) APP_ABI := x86 else -APP_ABI := all +APP_ABI := x86 endif APP_BUILD_SCRIPT := Android.mk @@ -17,7 +17,7 @@ NDK_MODULE_PATH := $(THIRD_PARTY_NDK_DIR)$(HOST_DIRSEP)$(REACT_NATIVE_JNI)$(HOST APP_STL := c++_shared -APP_CFLAGS := -Wall -Werror -fexceptions -frtti -DWITH_INSPECTOR=1 +APP_CFLAGS := -Wall -Werror -fexceptions -frtti -DWITH_INSPECTOR=1 -DHERMES_ENABLE_DEBUGGER=1 APP_CPPFLAGS := -std=c++1y # Make sure every shared lib includes a .note.gnu.build-id header APP_LDFLAGS := -Wl,--build-id diff --git a/android/src/main/JNI/react/Android.mk b/android/src/main/JNI/react/Android.mk index b4332ce7f78..3f72552c144 100644 --- a/android/src/main/JNI/react/Android.mk +++ b/android/src/main/JNI/react/Android.mk @@ -18,7 +18,7 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH) # ./../ == react LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../.. -LOCAL_CFLAGS += -fexceptions -frtti -Wno-unused-lambda-capture +LOCAL_CFLAGS += -fexceptions -frtti -Wno-unused-lambda-capture -DHERMES_ENABLE_DEBUGGER=1 LOCAL_LDLIBS += -landroid diff --git a/android/src/main/cpp/NativeProxy.cpp b/android/src/main/cpp/NativeProxy.cpp index 2616c3ff08e..3762159a412 100644 --- a/android/src/main/cpp/NativeProxy.cpp +++ b/android/src/main/cpp/NativeProxy.cpp @@ -12,6 +12,9 @@ #include "AndroidScheduler.h" #include #include "PlatformDepMethodsHolder.h" +#include +#include "Logger.h" +#include "JMessageQueueThread.h" namespace reanimated { @@ -19,6 +22,26 @@ namespace reanimated using namespace facebook; using namespace react; +class ReanimatedMessageQueueThread : public MessageQueueThread { +public: + ReanimatedMessageQueueThread() { + Logger::log("[mleko] constructor"); + } + ~ReanimatedMessageQueueThread() { + Logger::log("[mleko] destructor"); + } + void runOnQueue(std::function && runnable) { + Logger::log("[mleko] runOnQueue"); + + } + void runOnQueueSync(std::function && runnable) { + Logger::log("[mleko] runOnQueueSync"); + } + void quitSynchronous() { + Logger::log("[mleko] quitSynchronous"); + } +}; + NativeProxy::NativeProxy( jni::alias_ref jThis, jsi::Runtime *rt, @@ -31,18 +54,31 @@ NativeProxy::NativeProxy( } JavaScriptExecutorHolder* NativeProxy::_javaScriptExecutor = NULL; +jni::alias_ref NativeProxy::_messageQueueThread; +std::unique_ptr NativeProxy::_executor; jni::local_ref NativeProxy::initHybrid( jni::alias_ref jThis, jlong jsContext, jni::alias_ref jsCallInvokerHolder, jni::alias_ref androidScheduler, - JavaScriptExecutorHolder* javaScriptExecutor) + JavaScriptExecutorHolder* javaScriptExecutor, + jni::alias_ref messageQueueThread) { auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker(); auto scheduler = androidScheduler->cthis()->getScheduler(); scheduler->setJSCallInvoker(jsCallInvoker); _javaScriptExecutor = javaScriptExecutor; + _messageQueueThread = messageQueueThread; + + std::shared_ptr delegate = std::shared_ptr(); + auto jsQueue = std::make_shared(_messageQueueThread); + // std::shared_ptr jsQueue = std::make_shared(); + // std::shared_ptr jsQueue = std::shared_ptr(); + auto factory = _javaScriptExecutor->getExecutorFactory(); + _executor = factory.get()->createJSExecutor(delegate, jsQueue); + + return makeCxxInstance(jThis, (jsi::Runtime *)jsContext, jsCallInvoker, scheduler); } @@ -93,11 +129,26 @@ void NativeProxy::installJSIBindings() }; std::shared_ptr delegate = std::shared_ptr(); - std::shared_ptr jsQueue = std::shared_ptr(); - factory = _javaScriptExecutor->getExecutorFactory(); - executor = factory.get()->createJSExecutor(delegate, jsQueue); + + // + // std::shared_ptr registry = std::shared_ptr(); // std::make_shared(); + // std::shared_ptr callback = std::shared_ptr(); // std::make_shared(); + // auto delegate = std::make_shared(registry, callback); + // + auto mleko = _messageQueueThread; + if(_messageQueueThread) { + Logger::log("[mleko] ok"); + } else { + Logger::log("[mleko] fail"); + } + // jni::alias_ref jsMessageQueueThread; + // jsQueue = std::make_shared(_messageQueueThread); + // std::shared_ptr jsQueue = std::make_shared(); + // std::shared_ptr jsQueue = std::shared_ptr(); + // factory = _javaScriptExecutor->getExecutorFactory(); + // executor = factory.get()->createJSExecutor(delegate, jsQueue); std::unique_ptr animatedRuntime; - animatedRuntime.reset(static_cast(executor.get()->getJavaScriptContext())); + animatedRuntime.reset(static_cast(_executor.get()->getJavaScriptContext())); std::shared_ptr errorHandler = std::make_shared(scheduler_); diff --git a/android/src/main/cpp/headers/NativeProxy.h b/android/src/main/cpp/headers/NativeProxy.h index 57383a26804..da05ee48a5b 100644 --- a/android/src/main/cpp/headers/NativeProxy.h +++ b/android/src/main/cpp/headers/NativeProxy.h @@ -84,9 +84,12 @@ class NativeProxy : public jni::HybridClass { jlong jsContext, jni::alias_ref jsCallInvokerHolder, jni::alias_ref scheduler, - JavaScriptExecutorHolder* javaScriptExecutor); + JavaScriptExecutorHolder* javaScriptExecutor, + jni::alias_ref messageQueueThread); static void registerNatives(); static JavaScriptExecutorHolder* _javaScriptExecutor; + static jni::alias_ref _messageQueueThread; + static std::unique_ptr _executor; private: @@ -98,6 +101,8 @@ class NativeProxy : public jni::HybridClass { std::shared_ptr jsCallInvoker_; std::shared_ptr _nativeReanimatedModule; std::shared_ptr scheduler_; + std::shared_ptr jsQueue; + // std::shared_ptr jsQueue; void installJSIBindings(); bool isAnyHandlerWaitingForEvent(std::string); diff --git a/android/src/main/java/com/swmansion/reanimated/NativeProxy.java b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java index 6e48143fc04..e7d0b3daa23 100644 --- a/android/src/main/java/com/swmansion/reanimated/NativeProxy.java +++ b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java @@ -10,6 +10,8 @@ import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.WritableArray; import com.facebook.react.bridge.WritableMap; +import com.facebook.react.bridge.queue.MessageQueueThread; +import com.facebook.react.bridge.queue.QueueThreadExceptionHandler; import com.facebook.react.turbomodule.core.CallInvokerHolderImpl; import com.facebook.react.uimanager.UIManagerModule; import com.facebook.react.uimanager.events.RCTEventEmitter; @@ -21,6 +23,10 @@ import com.facebook.react.jscexecutor.JSCExecutorFactory; import com.facebook.soloader.SoLoader; +import com.facebook.react.bridge.queue.MessageQueueThreadImpl; +import com.facebook.react.bridge.queue.ReactQueueConfigurationImpl; +import com.facebook.react.bridge.queue.ReactQueueConfigurationSpec; + public class NativeProxy { static { @@ -75,8 +81,20 @@ public void receiveTouches(String eventName, WritableArray touches, WritableArra private final WeakReference mContext; private JavaScriptExecutor mJavaScriptExecutor; private Scheduler mScheduler = null; + private MessageQueueThread mMessageQueueThread; + private ReactQueueConfigurationImpl queueHolder; public NativeProxy(ReactApplicationContext context) { + queueHolder = ReactQueueConfigurationImpl.create( + ReactQueueConfigurationSpec.createDefault(), + new QueueThreadExceptionHandler() { + @Override + public void handleException(Exception e) { + throw new RuntimeException(e); + } + } + ); + mMessageQueueThread = queueHolder.getUIQueueThread(); try { mJavaScriptExecutor = getDefaultJSExecutorFactory(context).create(); } catch (Exception jscE) { @@ -85,7 +103,7 @@ public NativeProxy(ReactApplicationContext context) { CallInvokerHolderImpl holder = (CallInvokerHolderImpl)context.getCatalystInstance().getJSCallInvokerHolder(); mScheduler = new Scheduler(context); - mHybridData = initHybrid(context.getJavaScriptContextHolder().get(), holder, mScheduler, mJavaScriptExecutor); + mHybridData = initHybrid(context.getJavaScriptContextHolder().get(), holder, mScheduler, mJavaScriptExecutor, mMessageQueueThread); mContext = new WeakReference<>(context); prepare(); } @@ -129,7 +147,7 @@ private JavaScriptExecutorFactory getDefaultJSExecutorFactory(ReactApplicationCo } } - private native HybridData initHybrid(long jsContext, CallInvokerHolderImpl jsCallInvokerHolder, Scheduler scheduler, JavaScriptExecutor mJavaScriptExecutor); + private native HybridData initHybrid(long jsContext, CallInvokerHolderImpl jsCallInvokerHolder, Scheduler scheduler, JavaScriptExecutor mJavaScriptExecutor, MessageQueueThread mMessageQueueThread); private native void installJSIBindings(); public native boolean isAnyHandlerWaitingForEvent(String eventName); From 7a8ae541903ca033225c14a4721f358dd0ea7cbd Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Wed, 21 Apr 2021 09:18:18 +0200 Subject: [PATCH 02/12] Separated runtimes --- .../NativeModules/NativeReanimatedModule.cpp | 4 +- .../NativeModules/NativeReanimatedModule.h | 2 +- .../cpp/headers/SharedItems/RuntimeManager.h | 6 +- android/build.gradle | 4 +- android/src/main/JNI/Android.mk | 7 +- android/src/main/JNI/react/Android.mk | 11 ++- android/src/main/cpp/NativeProxy.cpp | 70 +++++++++++++++---- android/src/main/cpp/headers/NativeProxy.h | 48 +++++++++++++ 8 files changed, 125 insertions(+), 27 deletions(-) diff --git a/Common/cpp/NativeModules/NativeReanimatedModule.cpp b/Common/cpp/NativeModules/NativeReanimatedModule.cpp index 2f8bcd08cb6..f3f3b1eec51 100644 --- a/Common/cpp/NativeModules/NativeReanimatedModule.cpp +++ b/Common/cpp/NativeModules/NativeReanimatedModule.cpp @@ -58,12 +58,12 @@ std::vector> extractMutablesFromArray(jsi::Runtime NativeReanimatedModule::NativeReanimatedModule(std::shared_ptr jsInvoker, std::shared_ptr scheduler, - std::unique_ptr rt, + std::shared_ptr rt, std::shared_ptr errorHandler, std::function propObtainer, PlatformDepMethodsHolder platformDepMethodsHolder) : NativeReanimatedModuleSpec(jsInvoker), - RuntimeManager(std::move(rt), errorHandler, scheduler), + RuntimeManager(rt, errorHandler, scheduler), mapperRegistry(std::make_shared()), eventHandlerRegistry(std::make_shared()), requestRender(platformDepMethodsHolder.requestRender), diff --git a/Common/cpp/headers/NativeModules/NativeReanimatedModule.h b/Common/cpp/headers/NativeModules/NativeReanimatedModule.h index 0389c923806..b60502ac347 100644 --- a/Common/cpp/headers/NativeModules/NativeReanimatedModule.h +++ b/Common/cpp/headers/NativeModules/NativeReanimatedModule.h @@ -28,7 +28,7 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec, public Runtime public: NativeReanimatedModule(std::shared_ptr jsInvoker, std::shared_ptr scheduler, - std::unique_ptr rt, + std::shared_ptr rt, std::shared_ptr errorHandler, std::function propObtainer, PlatformDepMethodsHolder platformDepMethodsHolder); diff --git a/Common/cpp/headers/SharedItems/RuntimeManager.h b/Common/cpp/headers/SharedItems/RuntimeManager.h index 8a26326695f..7f0f7dfed91 100644 --- a/Common/cpp/headers/SharedItems/RuntimeManager.h +++ b/Common/cpp/headers/SharedItems/RuntimeManager.h @@ -16,9 +16,9 @@ using namespace facebook; */ class RuntimeManager { public: - RuntimeManager(std::unique_ptr&& runtime, + RuntimeManager(std::shared_ptr _runtime, std::shared_ptr errorHandler, - std::shared_ptr scheduler): runtime(std::move(runtime)), errorHandler(errorHandler), scheduler(scheduler), workletsCache(std::make_unique()) { } + std::shared_ptr scheduler): runtime(_runtime), errorHandler(errorHandler), scheduler(scheduler), workletsCache(std::make_unique()) { } public: /** Holds the jsi::Function worklet that is responsible for updating values in JS. @@ -28,7 +28,7 @@ class RuntimeManager { /** Holds the jsi::Runtime this RuntimeManager is managing. */ - std::unique_ptr runtime; + std::shared_ptr runtime; /** Holds the error handler that will be invoked when any kind of error occurs. */ diff --git a/android/build.gradle b/android/build.gradle index f0b66f8a641..e3e3d27d8a2 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -330,6 +330,7 @@ task prepareHermes() { } } +// Create Android.mk library module based on jsc from npm task prepareJSC { doLast { def jscPackagePath = findNodeModulePath(projectDir, "jsc-android") @@ -406,6 +407,8 @@ task buildReanimated(dependsOn: [prepareJSC, prepareBoost, prepareDoubleConversi "FIRST_PARTY=$reactNativeThirdParty/../first-party", "REACT_NATIVE_JNI=$reactNative/ReactAndroid/src/main/jni/react/jni", "REACT_NATIVE_REACT=$reactNative/ReactAndroid/src/main/jni/react", + "HERMES_ENGINE=$projectDir/../node_modules/hermes-engine", + "VM=HERMES", "NATIVE_DEBUG=${debugNativeLibraries}", "-C", file("src/main/JNI").absolutePath, "--jobs", project.findProperty("jobs") ?: Runtime.runtime.availableProcessors(), @@ -464,4 +467,3 @@ dependencies { extractHeaders("com.facebook.fbjni:fbjni:0.0.2:headers") extractJNI("com.facebook.fbjni:fbjni:0.0.2") } - diff --git a/android/src/main/JNI/Android.mk b/android/src/main/JNI/Android.mk index 7bf588e811a..a134aa30471 100644 --- a/android/src/main/JNI/Android.mk +++ b/android/src/main/JNI/Android.mk @@ -21,11 +21,12 @@ LOCAL_C_INCLUDES += $(LOCAL_PATH)/../Common/cpp/headers/Registries LOCAL_C_INCLUDES += $(LOCAL_PATH)/../Common/cpp/headers/SharedItems LOCAL_C_INCLUDES += $(LOCAL_PATH)/../Common/cpp/headers/SpecTools LOCAL_C_INCLUDES += $(LOCAL_PATH)/../Common/cpp/headers/Tools +LOCAL_C_INCLUDES += $(HERMES_ENGINE)/android/include -LOCAL_CFLAGS += -DONANDROID -fexceptions -frtti -DHERMES_ENABLE_DEBUGGER=1 +LOCAL_CFLAGS += -DONANDROID -fexceptions -frtti -LOCAL_STATIC_LIBRARIES := libjsi callinvokerholder -LOCAL_SHARED_LIBRARIES := libfolly_json libfbjni libreactnativejni +LOCAL_STATIC_LIBRARIES := libjsi callinvokerholder libhermes-inspector +LOCAL_SHARED_LIBRARIES := libhermes libfolly_json libfbjni libreactnativejni include $(BUILD_SHARED_LIBRARY) diff --git a/android/src/main/JNI/react/Android.mk b/android/src/main/JNI/react/Android.mk index 3f72552c144..e0bdb68f567 100644 --- a/android/src/main/JNI/react/Android.mk +++ b/android/src/main/JNI/react/Android.mk @@ -26,7 +26,7 @@ LOCAL_LDLIBS += -landroid LOCAL_SHARED_LIBRARIES := libfolly_json libfb libfbjni libglog_init libyoga # The static libraries (.a files) that this module depends on. -LOCAL_STATIC_LIBRARIES := libreactnative libcallinvokerholder +LOCAL_STATIC_LIBRARIES := libreactnative libcallinvokerholder libhermes-inspector # Name of this module. # @@ -63,6 +63,7 @@ include $(BUILD_SHARED_LIBRARY) $(call import-module,folly) $(call import-module,fb) $(call import-module,fbjni) +#$(call import-module,jsc) $(call import-module,fbgloginit) $(call import-module,yogajni) $(call import-module,cxxreact) @@ -70,7 +71,6 @@ $(call import-module,jsi) $(call import-module,jsiexecutor) $(call import-module,callinvoker) $(call import-module,hermes) - ifeq ($(shell test $(REACT_NATIVE_TARGET_VERSION) -ge 64; echo $$?),0) $(call import-module,reactperflogger) $(call import-module,runtimeexecutor) @@ -79,6 +79,13 @@ ifeq ($(shell test $(REACT_NATIVE_TARGET_VERSION) -ge 64; echo $$?),0) endif include $(REACT_SRC_DIR)/turbomodule/core/jni/Android.mk +# TODO(ramanpreet): +# Why doesn't this import-module call generate a jscexecutor.so file? +# $(call import-module,jscexecutor) + +#include $(REACT_SRC_DIR)/jscexecutor/Android.mk +#include $(REACT_SRC_DIR)/../hermes/reactexecutor/Android.mk +include $(REACT_SRC_DIR)/../hermes/instrumentation/Android.mk include $(REACT_SRC_DIR)/modules/blob/jni/Android.mk diff --git a/android/src/main/cpp/NativeProxy.cpp b/android/src/main/cpp/NativeProxy.cpp index 3762159a412..cb417ef4624 100644 --- a/android/src/main/cpp/NativeProxy.cpp +++ b/android/src/main/cpp/NativeProxy.cpp @@ -1,3 +1,5 @@ +// trzerba zamienić te runitme z unique na shared chyba + #include #include @@ -14,7 +16,27 @@ #include "PlatformDepMethodsHolder.h" #include #include "Logger.h" -#include "JMessageQueueThread.h" + +// #include +// #include +// #include +// #include "hermes/Public/DebuggerTypes.h" +// #include "hermes/executor/HermesExecutorFactory.cpp" + +// #include +// #include +// #include +// #include +// #include +// #include "HermesExecutorRuntimeAdapter.h" + +#include +#include +#include +#include + +#include +#include namespace reanimated { @@ -56,6 +78,10 @@ NativeProxy::NativeProxy( JavaScriptExecutorHolder* NativeProxy::_javaScriptExecutor = NULL; jni::alias_ref NativeProxy::_messageQueueThread; std::unique_ptr NativeProxy::_executor; +std::unique_ptr NativeProxy::_animatedRuntime; +std::unique_ptr NativeProxy::_animatedRuntimeHermes; +std::shared_ptr NativeProxy::_animatedRuntimeShared; + jni::local_ref NativeProxy::initHybrid( jni::alias_ref jThis, @@ -71,12 +97,32 @@ jni::local_ref NativeProxy::initHybrid( _javaScriptExecutor = javaScriptExecutor; _messageQueueThread = messageQueueThread; - std::shared_ptr delegate = std::shared_ptr(); - auto jsQueue = std::make_shared(_messageQueueThread); // std::shared_ptr jsQueue = std::make_shared(); // std::shared_ptr jsQueue = std::shared_ptr(); - auto factory = _javaScriptExecutor->getExecutorFactory(); - _executor = factory.get()->createJSExecutor(delegate, jsQueue); + + // std::shared_ptr delegate = std::shared_ptr(); + auto jsQueue = std::make_shared(_messageQueueThread); + // auto factory = _javaScriptExecutor->getExecutorFactory(); + // _executor = factory.get()->createJSExecutor(delegate, jsQueue); + // _animatedRuntime.reset(static_cast(_executor.get()->getJavaScriptContext())); + + + //mleko + // SystraceSection s("HermesExecutorFactory::makeHermesRuntimeSystraced"); + _animatedRuntime = facebook::hermes::makeHermesRuntime(); + + _animatedRuntimeHermes = facebook::hermes::makeHermesRuntime(); + // std::shared_ptr hermesRuntimeShared = std::move(_animatedRuntimeHermes); + facebook::hermes::HermesRuntime &hermesRuntimeRef = *_animatedRuntimeHermes; + + // HermesExecutorRuntimeAdapter pies(hermesRuntime2, hermesRuntimeRef, jsQueue); + auto adapter = std::make_unique(std::move(_animatedRuntimeHermes), hermesRuntimeRef, jsQueue); + _animatedRuntimeShared = adapter->runtime_; + facebook::hermes::inspector::chrome::enableDebugging(std::move(adapter), "mleko"); + // _animatedRuntime.reset(static_cast(hermesRuntimeShared.get())); + // _animatedRuntime = std::move(_animatedRuntimeHermes); + // _animatedRuntime = std::move(hermesRuntime2); + // _animatedRuntime = std::make_unique(hermesRuntime); return makeCxxInstance(jThis, (jsi::Runtime *)jsContext, jsCallInvoker, scheduler); @@ -135,21 +181,15 @@ void NativeProxy::installJSIBindings() // std::shared_ptr callback = std::shared_ptr(); // std::make_shared(); // auto delegate = std::make_shared(registry, callback); // - auto mleko = _messageQueueThread; - if(_messageQueueThread) { - Logger::log("[mleko] ok"); - } else { - Logger::log("[mleko] fail"); - } + // jni::alias_ref jsMessageQueueThread; // jsQueue = std::make_shared(_messageQueueThread); // std::shared_ptr jsQueue = std::make_shared(); // std::shared_ptr jsQueue = std::shared_ptr(); // factory = _javaScriptExecutor->getExecutorFactory(); // executor = factory.get()->createJSExecutor(delegate, jsQueue); - std::unique_ptr animatedRuntime; - animatedRuntime.reset(static_cast(_executor.get()->getJavaScriptContext())); - + // std::unique_ptr animatedRuntime; + // animatedRuntime.reset(static_cast(_executor.get()->getJavaScriptContext())); std::shared_ptr errorHandler = std::make_shared(scheduler_); PlatformDepMethodsHolder platformDepMethodsHolder = { @@ -162,7 +202,7 @@ void NativeProxy::installJSIBindings() auto module = std::make_shared(jsCallInvoker_, scheduler_, - std::move(animatedRuntime), + _animatedRuntimeShared, errorHandler, propObtainer, platformDepMethodsHolder); diff --git a/android/src/main/cpp/headers/NativeProxy.h b/android/src/main/cpp/headers/NativeProxy.h index da05ee48a5b..ae7f5928f4b 100644 --- a/android/src/main/cpp/headers/NativeProxy.h +++ b/android/src/main/cpp/headers/NativeProxy.h @@ -15,10 +15,55 @@ #include "Scheduler.h" #include "AndroidScheduler.h" +#include +#include +#include +#include + +#include +#include + namespace reanimated { using namespace facebook; +class HermesExecutorRuntimeAdapter : public facebook::hermes::inspector::RuntimeAdapter { + public: + HermesExecutorRuntimeAdapter( + std::shared_ptr runtime, + facebook::hermes::HermesRuntime &hermesRuntime, + std::shared_ptr thread) + : runtime_(runtime), + hermesRuntime_(hermesRuntime), + thread_(std::move(thread)) {} + + virtual ~HermesExecutorRuntimeAdapter() = default; + + facebook::jsi::Runtime &getRuntime() override { + return *runtime_; + } + + facebook::hermes::debugger::Debugger &getDebugger() override { + return hermesRuntime_.getDebugger(); + } + + void tickleJs() override { + // The queue will ensure that runtime_ is still valid when this + // gets invoked. + thread_->runOnQueue([&runtime = runtime_]() { + auto func = + runtime->global().getPropertyAsFunction(*runtime, "__tickleJs"); + func.call(*runtime); + }); + } + + public: + std::shared_ptr runtime_; + facebook::hermes::HermesRuntime &hermesRuntime_; + + std::shared_ptr thread_; +}; + class AnimationFrameCallback : public HybridClass { public: static auto constexpr kJavaDescriptor = @@ -90,6 +135,9 @@ class NativeProxy : public jni::HybridClass { static JavaScriptExecutorHolder* _javaScriptExecutor; static jni::alias_ref _messageQueueThread; static std::unique_ptr _executor; + static std::unique_ptr _animatedRuntime; + static std::unique_ptr _animatedRuntimeHermes; + static std::shared_ptr _animatedRuntimeShared; private: From 4734871b5dd9070ada01a7270764239a7e2f00fe Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Wed, 21 Apr 2021 13:33:17 +0200 Subject: [PATCH 03/12] Added runtime provider --- android/build.gradle | 1 + android/src/main/JNI/Android.mk | 2 +- android/src/main/JNI/react/Android.mk | 2 +- android/src/main/cpp/NativeProxy.cpp | 84 +++++-------------- android/src/main/cpp/RuntimeProvider.cpp | 37 ++++++++ android/src/main/cpp/headers/NativeProxy.h | 4 +- .../src/main/cpp/headers/RuntimeProvider.h | 36 ++++++++ .../com/swmansion/reanimated/NativeProxy.java | 40 ++++++++- .../com/swmansion/reanimated/RuntimeType.java | 6 ++ package.json | 1 + yarn.lock | 5 ++ 11 files changed, 151 insertions(+), 67 deletions(-) create mode 100644 android/src/main/cpp/RuntimeProvider.cpp create mode 100644 android/src/main/cpp/headers/RuntimeProvider.h create mode 100644 android/src/main/java/com/swmansion/reanimated/RuntimeType.java diff --git a/android/build.gradle b/android/build.gradle index e3e3d27d8a2..d45e7434727 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -461,6 +461,7 @@ repositories { dependencies { //noinspection GradleDynamicVersion + implementation "org.webkit:android-jsc:+" implementation "com.facebook.react:react-native:+" // From node_modules implementation "androidx.transition:transition:1.1.0" api("com.facebook.fbjni:fbjni-java-only:0.0.3") diff --git a/android/src/main/JNI/Android.mk b/android/src/main/JNI/Android.mk index a134aa30471..add43b639ea 100644 --- a/android/src/main/JNI/Android.mk +++ b/android/src/main/JNI/Android.mk @@ -26,7 +26,7 @@ LOCAL_C_INCLUDES += $(HERMES_ENGINE)/android/include LOCAL_CFLAGS += -DONANDROID -fexceptions -frtti LOCAL_STATIC_LIBRARIES := libjsi callinvokerholder libhermes-inspector -LOCAL_SHARED_LIBRARIES := libhermes libfolly_json libfbjni libreactnativejni +LOCAL_SHARED_LIBRARIES := libhermes jscruntime libfolly_json libfbjni libreactnativejni include $(BUILD_SHARED_LIBRARY) diff --git a/android/src/main/JNI/react/Android.mk b/android/src/main/JNI/react/Android.mk index e0bdb68f567..7ee63e77fdc 100644 --- a/android/src/main/JNI/react/Android.mk +++ b/android/src/main/JNI/react/Android.mk @@ -63,7 +63,7 @@ include $(BUILD_SHARED_LIBRARY) $(call import-module,folly) $(call import-module,fb) $(call import-module,fbjni) -#$(call import-module,jsc) +$(call import-module,jsc) $(call import-module,fbgloginit) $(call import-module,yogajni) $(call import-module,cxxreact) diff --git a/android/src/main/cpp/NativeProxy.cpp b/android/src/main/cpp/NativeProxy.cpp index cb417ef4624..83b21cfcad7 100644 --- a/android/src/main/cpp/NativeProxy.cpp +++ b/android/src/main/cpp/NativeProxy.cpp @@ -17,19 +17,6 @@ #include #include "Logger.h" -// #include -// #include -// #include -// #include "hermes/Public/DebuggerTypes.h" -// #include "hermes/executor/HermesExecutorFactory.cpp" - -// #include -// #include -// #include -// #include -// #include -// #include "HermesExecutorRuntimeAdapter.h" - #include #include #include @@ -38,32 +25,16 @@ #include #include +#include + +#include "RuntimeProvider.h" + namespace reanimated { using namespace facebook; using namespace react; -class ReanimatedMessageQueueThread : public MessageQueueThread { -public: - ReanimatedMessageQueueThread() { - Logger::log("[mleko] constructor"); - } - ~ReanimatedMessageQueueThread() { - Logger::log("[mleko] destructor"); - } - void runOnQueue(std::function && runnable) { - Logger::log("[mleko] runOnQueue"); - - } - void runOnQueueSync(std::function && runnable) { - Logger::log("[mleko] runOnQueueSync"); - } - void quitSynchronous() { - Logger::log("[mleko] quitSynchronous"); - } -}; - NativeProxy::NativeProxy( jni::alias_ref jThis, jsi::Runtime *rt, @@ -89,41 +60,30 @@ jni::local_ref NativeProxy::initHybrid( jni::alias_ref jsCallInvokerHolder, jni::alias_ref androidScheduler, JavaScriptExecutorHolder* javaScriptExecutor, - jni::alias_ref messageQueueThread) + jni::alias_ref messageQueueThread, + bool isDebug, + int runtimeType) { auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker(); auto scheduler = androidScheduler->cthis()->getScheduler(); scheduler->setJSCallInvoker(jsCallInvoker); - _javaScriptExecutor = javaScriptExecutor; _messageQueueThread = messageQueueThread; - // std::shared_ptr jsQueue = std::make_shared(); - // std::shared_ptr jsQueue = std::shared_ptr(); - - // std::shared_ptr delegate = std::shared_ptr(); - auto jsQueue = std::make_shared(_messageQueueThread); - // auto factory = _javaScriptExecutor->getExecutorFactory(); - // _executor = factory.get()->createJSExecutor(delegate, jsQueue); - // _animatedRuntime.reset(static_cast(_executor.get()->getJavaScriptContext())); - - - //mleko - // SystraceSection s("HermesExecutorFactory::makeHermesRuntimeSystraced"); - _animatedRuntime = facebook::hermes::makeHermesRuntime(); - - _animatedRuntimeHermes = facebook::hermes::makeHermesRuntime(); - // std::shared_ptr hermesRuntimeShared = std::move(_animatedRuntimeHermes); - facebook::hermes::HermesRuntime &hermesRuntimeRef = *_animatedRuntimeHermes; - - // HermesExecutorRuntimeAdapter pies(hermesRuntime2, hermesRuntimeRef, jsQueue); - auto adapter = std::make_unique(std::move(_animatedRuntimeHermes), hermesRuntimeRef, jsQueue); - _animatedRuntimeShared = adapter->runtime_; - facebook::hermes::inspector::chrome::enableDebugging(std::move(adapter), "mleko"); - // _animatedRuntime.reset(static_cast(hermesRuntimeShared.get())); - // _animatedRuntime = std::move(_animatedRuntimeHermes); - // _animatedRuntime = std::move(hermesRuntime2); - // _animatedRuntime = std::make_unique(hermesRuntime); - + _animatedRuntimeShared = RuntimeProvider::createRuntime(messageQueueThread, isDebug, runtimeType); + //Hremes + // auto jsQueue = std::make_shared(_messageQueueThread); + // // SystraceSection s("HermesExecutorFactory::makeHermesRuntimeSystraced"); + // _animatedRuntime = facebook::hermes::makeHermesRuntime(); + + // _animatedRuntimeHermes = facebook::hermes::makeHermesRuntime(); + // facebook::hermes::HermesRuntime &hermesRuntimeRef = *_animatedRuntimeHermes; + // auto adapter = std::make_unique(std::move(_animatedRuntimeHermes), hermesRuntimeRef, jsQueue); + // _animatedRuntimeShared = adapter->runtime_; + // facebook::hermes::inspector::chrome::enableDebugging(std::move(adapter), "Reanimated Runtime"); + + //JSC + // std::shared_ptr jscRuntime = jsc::makeJSCRuntime(); + // _animatedRuntimeShared = jscRuntime; return makeCxxInstance(jThis, (jsi::Runtime *)jsContext, jsCallInvoker, scheduler); } diff --git a/android/src/main/cpp/RuntimeProvider.cpp b/android/src/main/cpp/RuntimeProvider.cpp new file mode 100644 index 00000000000..fdd1785f415 --- /dev/null +++ b/android/src/main/cpp/RuntimeProvider.cpp @@ -0,0 +1,37 @@ +#include "RuntimeProvider.h" + +namespace reanimated +{ + +std::shared_ptr RuntimeProvider::createRuntime( + jni::alias_ref messageQueueThread, + bool isDebug, + int runtimeType +) { + switch(runtimeType) { + + case 1: { + return jsc::makeJSCRuntime(); + } break; + + default: { + std::shared_ptr runtimeShared; + std::unique_ptr runtime = facebook::hermes::makeHermesRuntime(); + if(isDebug) { + auto jsQueue = std::make_shared(messageQueueThread); + SystraceSection s("RuntimeProvider::createRuntime"); + facebook::hermes::HermesRuntime &hermesRuntimeRef = *runtime; + auto adapter = std::make_unique(std::move(runtime), hermesRuntimeRef, jsQueue); + std::shared_ptr runtimeShared = adapter->runtime_; + facebook::hermes::inspector::chrome::enableDebugging(std::move(adapter), "Reanimated Runtime"); + } + else { + runtimeShared = std::move(runtime); + } + return runtimeShared; + } + + } +} + +} diff --git a/android/src/main/cpp/headers/NativeProxy.h b/android/src/main/cpp/headers/NativeProxy.h index ae7f5928f4b..6f58a6b7bcf 100644 --- a/android/src/main/cpp/headers/NativeProxy.h +++ b/android/src/main/cpp/headers/NativeProxy.h @@ -130,7 +130,9 @@ class NativeProxy : public jni::HybridClass { jni::alias_ref jsCallInvokerHolder, jni::alias_ref scheduler, JavaScriptExecutorHolder* javaScriptExecutor, - jni::alias_ref messageQueueThread); + jni::alias_ref messageQueueThread, + bool isDebug, + int runtimeType); static void registerNatives(); static JavaScriptExecutorHolder* _javaScriptExecutor; static jni::alias_ref _messageQueueThread; diff --git a/android/src/main/cpp/headers/RuntimeProvider.h b/android/src/main/cpp/headers/RuntimeProvider.h new file mode 100644 index 00000000000..9afdb8d552f --- /dev/null +++ b/android/src/main/cpp/headers/RuntimeProvider.h @@ -0,0 +1,36 @@ +#include +#include + +#include +#include +#include +#include +#include + +#include "NativeProxy.h" +#include "PlatformDepMethodsHolder.h" +#include + +#include +#include +#include +#include + +#include +#include + +#include + +namespace reanimated +{ + +class RuntimeProvider { +public: + static std::shared_ptr createRuntime( + jni::alias_ref messageQueueThread, + bool isDebug, + int runtimeType + ); +}; + +} diff --git a/android/src/main/java/com/swmansion/reanimated/NativeProxy.java b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java index e7d0b3daa23..fc85ff79c0c 100644 --- a/android/src/main/java/com/swmansion/reanimated/NativeProxy.java +++ b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java @@ -27,6 +27,8 @@ import com.facebook.react.bridge.queue.ReactQueueConfigurationImpl; import com.facebook.react.bridge.queue.ReactQueueConfigurationSpec; +import com.facebook.react.common.build.ReactBuildConfig; + public class NativeProxy { static { @@ -103,7 +105,15 @@ public void handleException(Exception e) { CallInvokerHolderImpl holder = (CallInvokerHolderImpl)context.getCatalystInstance().getJSCallInvokerHolder(); mScheduler = new Scheduler(context); - mHybridData = initHybrid(context.getJavaScriptContextHolder().get(), holder, mScheduler, mJavaScriptExecutor, mMessageQueueThread); + mHybridData = initHybrid( + context.getJavaScriptContextHolder().get(), + holder, + mScheduler, + mJavaScriptExecutor, + mMessageQueueThread, + BuildConfig.DEBUG, + detectReactNativeRuntime(context).ordinal() + ); mContext = new WeakReference<>(context); prepare(); } @@ -147,7 +157,33 @@ private JavaScriptExecutorFactory getDefaultJSExecutorFactory(ReactApplicationCo } } - private native HybridData initHybrid(long jsContext, CallInvokerHolderImpl jsCallInvokerHolder, Scheduler scheduler, JavaScriptExecutor mJavaScriptExecutor, MessageQueueThread mMessageQueueThread); + private RuntimeType detectReactNativeRuntime(ReactApplicationContext context) { + try { + SoLoader.init(context,false); + SoLoader.loadLibrary("jscexecutor"); + return RuntimeType.JSC; + } catch (UnsatisfiedLinkError jscE) { + if (jscE.getMessage().contains("__cxa_bad_typeid")) { + throw jscE; + } + try { + return RuntimeType.HERMES; + } catch (UnsatisfiedLinkError hermesE) { + hermesE.printStackTrace(); + throw jscE; + } + } + } + + private native HybridData initHybrid( + long jsContext, + CallInvokerHolderImpl jsCallInvokerHolder, + Scheduler scheduler, + JavaScriptExecutor mJavaScriptExecutor, + MessageQueueThread mMessageQueueThread, + boolean isDebug, + int runtimeType + ); private native void installJSIBindings(); public native boolean isAnyHandlerWaitingForEvent(String eventName); diff --git a/android/src/main/java/com/swmansion/reanimated/RuntimeType.java b/android/src/main/java/com/swmansion/reanimated/RuntimeType.java new file mode 100644 index 00000000000..db6cb3d3767 --- /dev/null +++ b/android/src/main/java/com/swmansion/reanimated/RuntimeType.java @@ -0,0 +1,6 @@ +package com.swmansion.reanimated; + +public enum RuntimeType { + HERMES, + JSC +} diff --git a/package.json b/package.json index 0cb37fcc74c..49c33d78bad 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "dependencies": { "@babel/plugin-transform-object-assign": "^7.10.4", "fbjs": "^3.0.0", + "jsc-android": "^250230.2.1", "mockdate": "^3.0.2", "string-hash-64": "^1.0.3" }, diff --git a/yarn.lock b/yarn.lock index 36324ffb3cb..86a3700efbc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6905,6 +6905,11 @@ jsc-android@^245459.0.0: version "245459.0.0" resolved "https://registry.yarnpkg.com/jsc-android/-/jsc-android-245459.0.0.tgz#e584258dd0b04c9159a27fb104cd5d491fd202c9" +jsc-android@^250230.2.1: + version "250230.2.1" + resolved "https://registry.yarnpkg.com/jsc-android/-/jsc-android-250230.2.1.tgz#3790313a970586a03ab0ad47defbc84df54f1b83" + integrity sha512-KmxeBlRjwoqCnBBKGsihFtvsBHyUFlBxJPK4FzeYcIuBfdjv6jFys44JITAgSTbQD+vIdwMEfyZklsuQX0yI1Q== + jscodeshift@^0.11.0: version "0.11.0" resolved "https://registry.yarnpkg.com/jscodeshift/-/jscodeshift-0.11.0.tgz#4f95039408f3f06b0e39bb4d53bc3139f5330e2f" From 0675b8dd9ec80ce36dc7bc93c5302337b4c75e09 Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Wed, 21 Apr 2021 14:07:39 +0200 Subject: [PATCH 04/12] Clean up in C++ --- android/src/main/cpp/NativeProxy.cpp | 47 ++----------------- android/src/main/cpp/RuntimeProvider.cpp | 3 ++ android/src/main/cpp/headers/NativeProxy.h | 10 +--- .../src/main/cpp/headers/RuntimeProvider.h | 13 +---- .../com/swmansion/reanimated/NativeProxy.java | 2 - 5 files changed, 8 insertions(+), 67 deletions(-) diff --git a/android/src/main/cpp/NativeProxy.cpp b/android/src/main/cpp/NativeProxy.cpp index 83b21cfcad7..63d6bd04826 100644 --- a/android/src/main/cpp/NativeProxy.cpp +++ b/android/src/main/cpp/NativeProxy.cpp @@ -46,20 +46,13 @@ NativeProxy::NativeProxy( { } -JavaScriptExecutorHolder* NativeProxy::_javaScriptExecutor = NULL; -jni::alias_ref NativeProxy::_messageQueueThread; -std::unique_ptr NativeProxy::_executor; -std::unique_ptr NativeProxy::_animatedRuntime; -std::unique_ptr NativeProxy::_animatedRuntimeHermes; -std::shared_ptr NativeProxy::_animatedRuntimeShared; - +std::shared_ptr NativeProxy::_animatedRuntime; jni::local_ref NativeProxy::initHybrid( jni::alias_ref jThis, jlong jsContext, jni::alias_ref jsCallInvokerHolder, jni::alias_ref androidScheduler, - JavaScriptExecutorHolder* javaScriptExecutor, jni::alias_ref messageQueueThread, bool isDebug, int runtimeType) @@ -67,23 +60,7 @@ jni::local_ref NativeProxy::initHybrid( auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker(); auto scheduler = androidScheduler->cthis()->getScheduler(); scheduler->setJSCallInvoker(jsCallInvoker); - _messageQueueThread = messageQueueThread; - - _animatedRuntimeShared = RuntimeProvider::createRuntime(messageQueueThread, isDebug, runtimeType); - //Hremes - // auto jsQueue = std::make_shared(_messageQueueThread); - // // SystraceSection s("HermesExecutorFactory::makeHermesRuntimeSystraced"); - // _animatedRuntime = facebook::hermes::makeHermesRuntime(); - - // _animatedRuntimeHermes = facebook::hermes::makeHermesRuntime(); - // facebook::hermes::HermesRuntime &hermesRuntimeRef = *_animatedRuntimeHermes; - // auto adapter = std::make_unique(std::move(_animatedRuntimeHermes), hermesRuntimeRef, jsQueue); - // _animatedRuntimeShared = adapter->runtime_; - // facebook::hermes::inspector::chrome::enableDebugging(std::move(adapter), "Reanimated Runtime"); - - //JSC - // std::shared_ptr jscRuntime = jsc::makeJSCRuntime(); - // _animatedRuntimeShared = jscRuntime; + _animatedRuntime = RuntimeProvider::createRuntime(messageQueueThread, isDebug, runtimeType); return makeCxxInstance(jThis, (jsi::Runtime *)jsContext, jsCallInvoker, scheduler); } @@ -133,25 +110,7 @@ void NativeProxy::installJSIBindings() auto scrollToFunction = [this](int viewTag, double x, double y, bool animated) -> void { scrollTo(viewTag, x, y, animated); }; - - std::shared_ptr delegate = std::shared_ptr(); - - // - // std::shared_ptr registry = std::shared_ptr(); // std::make_shared(); - // std::shared_ptr callback = std::shared_ptr(); // std::make_shared(); - // auto delegate = std::make_shared(registry, callback); - // - - // jni::alias_ref jsMessageQueueThread; - // jsQueue = std::make_shared(_messageQueueThread); - // std::shared_ptr jsQueue = std::make_shared(); - // std::shared_ptr jsQueue = std::shared_ptr(); - // factory = _javaScriptExecutor->getExecutorFactory(); - // executor = factory.get()->createJSExecutor(delegate, jsQueue); - // std::unique_ptr animatedRuntime; - // animatedRuntime.reset(static_cast(_executor.get()->getJavaScriptContext())); std::shared_ptr errorHandler = std::make_shared(scheduler_); - PlatformDepMethodsHolder platformDepMethodsHolder = { requestRender, propUpdater, @@ -162,7 +121,7 @@ void NativeProxy::installJSIBindings() auto module = std::make_shared(jsCallInvoker_, scheduler_, - _animatedRuntimeShared, + _animatedRuntime, errorHandler, propObtainer, platformDepMethodsHolder); diff --git a/android/src/main/cpp/RuntimeProvider.cpp b/android/src/main/cpp/RuntimeProvider.cpp index fdd1785f415..51fe05a2c91 100644 --- a/android/src/main/cpp/RuntimeProvider.cpp +++ b/android/src/main/cpp/RuntimeProvider.cpp @@ -1,5 +1,8 @@ #include "RuntimeProvider.h" +#include +#include + namespace reanimated { diff --git a/android/src/main/cpp/headers/NativeProxy.h b/android/src/main/cpp/headers/NativeProxy.h index 6f58a6b7bcf..602ed372417 100644 --- a/android/src/main/cpp/headers/NativeProxy.h +++ b/android/src/main/cpp/headers/NativeProxy.h @@ -129,17 +129,11 @@ class NativeProxy : public jni::HybridClass { jlong jsContext, jni::alias_ref jsCallInvokerHolder, jni::alias_ref scheduler, - JavaScriptExecutorHolder* javaScriptExecutor, jni::alias_ref messageQueueThread, bool isDebug, int runtimeType); static void registerNatives(); - static JavaScriptExecutorHolder* _javaScriptExecutor; - static jni::alias_ref _messageQueueThread; - static std::unique_ptr _executor; - static std::unique_ptr _animatedRuntime; - static std::unique_ptr _animatedRuntimeHermes; - static std::shared_ptr _animatedRuntimeShared; + static std::shared_ptr _animatedRuntime; private: @@ -151,8 +145,6 @@ class NativeProxy : public jni::HybridClass { std::shared_ptr jsCallInvoker_; std::shared_ptr _nativeReanimatedModule; std::shared_ptr scheduler_; - std::shared_ptr jsQueue; - // std::shared_ptr jsQueue; void installJSIBindings(); bool isAnyHandlerWaitingForEvent(std::string); diff --git a/android/src/main/cpp/headers/RuntimeProvider.h b/android/src/main/cpp/headers/RuntimeProvider.h index 9afdb8d552f..02e7b7ae8e5 100644 --- a/android/src/main/cpp/headers/RuntimeProvider.h +++ b/android/src/main/cpp/headers/RuntimeProvider.h @@ -1,26 +1,15 @@ #include -#include - #include #include -#include -#include #include - -#include "NativeProxy.h" -#include "PlatformDepMethodsHolder.h" #include - #include #include -#include +#include "NativeProxy.h" #include - #include #include -#include - namespace reanimated { diff --git a/android/src/main/java/com/swmansion/reanimated/NativeProxy.java b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java index fc85ff79c0c..835632d65b9 100644 --- a/android/src/main/java/com/swmansion/reanimated/NativeProxy.java +++ b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java @@ -109,7 +109,6 @@ public void handleException(Exception e) { context.getJavaScriptContextHolder().get(), holder, mScheduler, - mJavaScriptExecutor, mMessageQueueThread, BuildConfig.DEBUG, detectReactNativeRuntime(context).ordinal() @@ -179,7 +178,6 @@ private native HybridData initHybrid( long jsContext, CallInvokerHolderImpl jsCallInvokerHolder, Scheduler scheduler, - JavaScriptExecutor mJavaScriptExecutor, MessageQueueThread mMessageQueueThread, boolean isDebug, int runtimeType From fdcdff32877950dbc0d517002fd7743951d6fdba Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Thu, 22 Apr 2021 09:57:45 +0200 Subject: [PATCH 05/12] Clean up in Java --- android/src/main/JNI/Android.mk | 4 +- android/src/main/JNI/react/Android.mk | 6 -- .../com/swmansion/reanimated/NativeProxy.java | 60 +------------------ 3 files changed, 5 insertions(+), 65 deletions(-) diff --git a/android/src/main/JNI/Android.mk b/android/src/main/JNI/Android.mk index add43b639ea..1fcaf8fa605 100644 --- a/android/src/main/JNI/Android.mk +++ b/android/src/main/JNI/Android.mk @@ -25,8 +25,8 @@ LOCAL_C_INCLUDES += $(HERMES_ENGINE)/android/include LOCAL_CFLAGS += -DONANDROID -fexceptions -frtti -LOCAL_STATIC_LIBRARIES := libjsi callinvokerholder libhermes-inspector -LOCAL_SHARED_LIBRARIES := libhermes jscruntime libfolly_json libfbjni libreactnativejni +LOCAL_STATIC_LIBRARIES := libjsi callinvokerholder libhermes-inspector jscruntime +LOCAL_SHARED_LIBRARIES := libhermes libfolly_json libfbjni libreactnativejni include $(BUILD_SHARED_LIBRARY) diff --git a/android/src/main/JNI/react/Android.mk b/android/src/main/JNI/react/Android.mk index 7ee63e77fdc..73d8a4a63fa 100644 --- a/android/src/main/JNI/react/Android.mk +++ b/android/src/main/JNI/react/Android.mk @@ -79,12 +79,6 @@ ifeq ($(shell test $(REACT_NATIVE_TARGET_VERSION) -ge 64; echo $$?),0) endif include $(REACT_SRC_DIR)/turbomodule/core/jni/Android.mk -# TODO(ramanpreet): -# Why doesn't this import-module call generate a jscexecutor.so file? -# $(call import-module,jscexecutor) - -#include $(REACT_SRC_DIR)/jscexecutor/Android.mk -#include $(REACT_SRC_DIR)/../hermes/reactexecutor/Android.mk include $(REACT_SRC_DIR)/../hermes/instrumentation/Android.mk include $(REACT_SRC_DIR)/modules/blob/jni/Android.mk diff --git a/android/src/main/java/com/swmansion/reanimated/NativeProxy.java b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java index 835632d65b9..45d46a77666 100644 --- a/android/src/main/java/com/swmansion/reanimated/NativeProxy.java +++ b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java @@ -5,8 +5,6 @@ import com.facebook.jni.HybridData; import com.facebook.proguard.annotations.DoNotStrip; -import com.facebook.react.bridge.JavaScriptExecutor; -import com.facebook.react.bridge.JavaScriptExecutorFactory; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.WritableArray; import com.facebook.react.bridge.WritableMap; @@ -15,19 +13,12 @@ import com.facebook.react.turbomodule.core.CallInvokerHolderImpl; import com.facebook.react.uimanager.UIManagerModule; import com.facebook.react.uimanager.events.RCTEventEmitter; - -import java.lang.ref.WeakReference; -import java.util.Map; - -import com.facebook.hermes.reactexecutor.HermesExecutorFactory; -import com.facebook.react.jscexecutor.JSCExecutorFactory; import com.facebook.soloader.SoLoader; - -import com.facebook.react.bridge.queue.MessageQueueThreadImpl; import com.facebook.react.bridge.queue.ReactQueueConfigurationImpl; import com.facebook.react.bridge.queue.ReactQueueConfigurationSpec; -import com.facebook.react.common.build.ReactBuildConfig; +import java.lang.ref.WeakReference; +import java.util.Map; public class NativeProxy { @@ -81,7 +72,6 @@ public void receiveTouches(String eventName, WritableArray touches, WritableArra private final HybridData mHybridData; private NodesManager mNodesManager; private final WeakReference mContext; - private JavaScriptExecutor mJavaScriptExecutor; private Scheduler mScheduler = null; private MessageQueueThread mMessageQueueThread; private ReactQueueConfigurationImpl queueHolder; @@ -97,11 +87,6 @@ public void handleException(Exception e) { } ); mMessageQueueThread = queueHolder.getUIQueueThread(); - try { - mJavaScriptExecutor = getDefaultJSExecutorFactory(context).create(); - } catch (Exception jscE) { - jscE.printStackTrace(); - } CallInvokerHolderImpl holder = (CallInvokerHolderImpl)context.getCatalystInstance().getJSCallInvokerHolder(); mScheduler = new Scheduler(context); @@ -117,45 +102,7 @@ public void handleException(Exception e) { prepare(); } - // method from React native - private JavaScriptExecutorFactory getDefaultJSExecutorFactory(ReactApplicationContext context) { - try { - // If JSC is included, use it as normal - SoLoader.init(context, /* native exopackage */ false); - SoLoader.loadLibrary("jscexecutor"); - return new JSCExecutorFactory("Reanimated", "Reanimated"); - } catch (UnsatisfiedLinkError jscE) { - // https://github.com/facebook/hermes/issues/78 shows that - // people who aren't trying to use Hermes are having issues. - // https://github.com/facebook/react-native/issues/25923#issuecomment-554295179 - // includes the actual JSC error in at least one case. - // - // So, if "__cxa_bad_typeid" shows up in the jscE exception - // message, then we will assume that's the failure and just - // throw now. - - if (jscE.getMessage().contains("__cxa_bad_typeid")) { - throw jscE; - } - - // Otherwise use Hermes - try { - return new HermesExecutorFactory(); - } catch (UnsatisfiedLinkError hermesE) { - // If we get here, either this is a JSC build, and of course - // Hermes failed (since it's not in the APK), or it's a Hermes - // build, and Hermes had a problem. - - // We suspect this is a JSC issue (it's the default), so we - // will throw that exception, but we will print hermesE first, - // since it could be a Hermes issue and we don't want to - // swallow that. - hermesE.printStackTrace(); - throw jscE; - } - } - } - + // Method based on method from React Native private RuntimeType detectReactNativeRuntime(ReactApplicationContext context) { try { SoLoader.init(context,false); @@ -225,7 +172,6 @@ private void registerEventHandler(EventHandler handler) { public void onCatalystInstanceDestroy() { mScheduler.deactivate(); mHybridData.resetNative(); - mJavaScriptExecutor.close(); } public void prepare() { From a4a67a73d434c6ee80ffddc339b1dabf5fe4e9a5 Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Thu, 22 Apr 2021 20:15:16 +0200 Subject: [PATCH 06/12] Universal runtime support --- android/build.gradle | 3 +- android/src/main/JNI/Android.mk | 2 +- android/src/main/cpp/NativeProxy.cpp | 17 ++--- android/src/main/cpp/RuntimeProvider.cpp | 48 ++++++------ android/src/main/cpp/headers/NativeProxy.h | 12 +-- .../src/main/cpp/headers/RuntimeProvider.h | 12 ++- .../com/swmansion/reanimated/NativeProxy.java | 76 ++++++++++++++----- .../com/swmansion/reanimated/RuntimeType.java | 4 +- 8 files changed, 107 insertions(+), 67 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index d45e7434727..204fb46899f 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -408,7 +408,6 @@ task buildReanimated(dependsOn: [prepareJSC, prepareBoost, prepareDoubleConversi "REACT_NATIVE_JNI=$reactNative/ReactAndroid/src/main/jni/react/jni", "REACT_NATIVE_REACT=$reactNative/ReactAndroid/src/main/jni/react", "HERMES_ENGINE=$projectDir/../node_modules/hermes-engine", - "VM=HERMES", "NATIVE_DEBUG=${debugNativeLibraries}", "-C", file("src/main/JNI").absolutePath, "--jobs", project.findProperty("jobs") ?: Runtime.runtime.availableProcessors(), @@ -461,7 +460,7 @@ repositories { dependencies { //noinspection GradleDynamicVersion - implementation "org.webkit:android-jsc:+" + // implementation "org.webkit:android-jsc:+" implementation "com.facebook.react:react-native:+" // From node_modules implementation "androidx.transition:transition:1.1.0" api("com.facebook.fbjni:fbjni-java-only:0.0.3") diff --git a/android/src/main/JNI/Android.mk b/android/src/main/JNI/Android.mk index 1fcaf8fa605..a134aa30471 100644 --- a/android/src/main/JNI/Android.mk +++ b/android/src/main/JNI/Android.mk @@ -25,7 +25,7 @@ LOCAL_C_INCLUDES += $(HERMES_ENGINE)/android/include LOCAL_CFLAGS += -DONANDROID -fexceptions -frtti -LOCAL_STATIC_LIBRARIES := libjsi callinvokerholder libhermes-inspector jscruntime +LOCAL_STATIC_LIBRARIES := libjsi callinvokerholder libhermes-inspector LOCAL_SHARED_LIBRARIES := libhermes libfolly_json libfbjni libreactnativejni include $(BUILD_SHARED_LIBRARY) diff --git a/android/src/main/cpp/NativeProxy.cpp b/android/src/main/cpp/NativeProxy.cpp index 63d6bd04826..11fe1f65965 100644 --- a/android/src/main/cpp/NativeProxy.cpp +++ b/android/src/main/cpp/NativeProxy.cpp @@ -1,14 +1,10 @@ -// trzerba zamienić te runitme z unique na shared chyba - #include #include - #include #include #include #include #include - #include "NativeProxy.h" #include "AndroidErrorHandler.h" #include "AndroidScheduler.h" @@ -16,19 +12,13 @@ #include "PlatformDepMethodsHolder.h" #include #include "Logger.h" - #include #include #include #include - #include #include -#include - -#include "RuntimeProvider.h" - namespace reanimated { @@ -46,13 +36,16 @@ NativeProxy::NativeProxy( { } +JavaScriptExecutorHolder* NativeProxy::_javaScriptExecutor = NULL; std::shared_ptr NativeProxy::_animatedRuntime; +RuntimeProvider NativeProxy::runtimeProvider; jni::local_ref NativeProxy::initHybrid( jni::alias_ref jThis, jlong jsContext, jni::alias_ref jsCallInvokerHolder, jni::alias_ref androidScheduler, + JavaScriptExecutorHolder* javaScriptExecutor, jni::alias_ref messageQueueThread, bool isDebug, int runtimeType) @@ -60,7 +53,9 @@ jni::local_ref NativeProxy::initHybrid( auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker(); auto scheduler = androidScheduler->cthis()->getScheduler(); scheduler->setJSCallInvoker(jsCallInvoker); - _animatedRuntime = RuntimeProvider::createRuntime(messageQueueThread, isDebug, runtimeType); + _javaScriptExecutor = javaScriptExecutor; + + _animatedRuntime = runtimeProvider.createRuntime(javaScriptExecutor, messageQueueThread, isDebug, runtimeType); return makeCxxInstance(jThis, (jsi::Runtime *)jsContext, jsCallInvoker, scheduler); } diff --git a/android/src/main/cpp/RuntimeProvider.cpp b/android/src/main/cpp/RuntimeProvider.cpp index 51fe05a2c91..5ccfffe6f9c 100644 --- a/android/src/main/cpp/RuntimeProvider.cpp +++ b/android/src/main/cpp/RuntimeProvider.cpp @@ -1,40 +1,42 @@ #include "RuntimeProvider.h" #include -#include namespace reanimated { std::shared_ptr RuntimeProvider::createRuntime( + JavaScriptExecutorHolder* javaScriptExecutor, jni::alias_ref messageQueueThread, bool isDebug, int runtimeType ) { - switch(runtimeType) { - - case 1: { - return jsc::makeJSCRuntime(); - } break; - - default: { - std::shared_ptr runtimeShared; - std::unique_ptr runtime = facebook::hermes::makeHermesRuntime(); - if(isDebug) { - auto jsQueue = std::make_shared(messageQueueThread); - SystraceSection s("RuntimeProvider::createRuntime"); - facebook::hermes::HermesRuntime &hermesRuntimeRef = *runtime; - auto adapter = std::make_unique(std::move(runtime), hermesRuntimeRef, jsQueue); - std::shared_ptr runtimeShared = adapter->runtime_; - facebook::hermes::inspector::chrome::enableDebugging(std::move(adapter), "Reanimated Runtime"); - } - else { - runtimeShared = std::move(runtime); - } - return runtimeShared; - } + + if(runtimeType == 1) { // JSC, V8 + std::shared_ptr delegate = std::shared_ptr(); + auto jsQueue = std::make_shared(messageQueueThread); + factory = javaScriptExecutor->getExecutorFactory(); + executor = factory.get()->createJSExecutor(delegate, jsQueue); + std::shared_ptr runtimeShared; + runtimeShared.reset(static_cast(executor.get()->getJavaScriptContext())); + return runtimeShared; + } + // Hermes + std::shared_ptr runtimeShared; + std::unique_ptr runtime = facebook::hermes::makeHermesRuntime(); + if(isDebug) { + auto jsQueue = std::make_shared(messageQueueThread); + SystraceSection s("RuntimeProvider::createRuntime"); + facebook::hermes::HermesRuntime &hermesRuntimeRef = *runtime; + auto adapter = std::make_unique(std::move(runtime), hermesRuntimeRef, jsQueue); + std::shared_ptr runtimeShared = adapter->runtime_; + facebook::hermes::inspector::chrome::enableDebugging(std::move(adapter), "Reanimated Runtime"); + } + else { + runtimeShared = std::move(runtime); } + return runtimeShared; } } diff --git a/android/src/main/cpp/headers/NativeProxy.h b/android/src/main/cpp/headers/NativeProxy.h index 602ed372417..81faaff2a86 100644 --- a/android/src/main/cpp/headers/NativeProxy.h +++ b/android/src/main/cpp/headers/NativeProxy.h @@ -10,21 +10,20 @@ #include #include #include - - #include "Scheduler.h" #include "AndroidScheduler.h" - #include #include #include #include - #include #include +#include "RuntimeProvider.h" namespace reanimated { +class RuntimeProvider; + using namespace facebook; class HermesExecutorRuntimeAdapter : public facebook::hermes::inspector::RuntimeAdapter { @@ -129,19 +128,20 @@ class NativeProxy : public jni::HybridClass { jlong jsContext, jni::alias_ref jsCallInvokerHolder, jni::alias_ref scheduler, + JavaScriptExecutorHolder* javaScriptExecutor, jni::alias_ref messageQueueThread, bool isDebug, int runtimeType); static void registerNatives(); + static JavaScriptExecutorHolder* _javaScriptExecutor; static std::shared_ptr _animatedRuntime; + static RuntimeProvider runtimeProvider; private: friend HybridBase; jni::global_ref javaPart_; jsi::Runtime *runtime_; - std::shared_ptr factory; - std::unique_ptr executor; std::shared_ptr jsCallInvoker_; std::shared_ptr _nativeReanimatedModule; std::shared_ptr scheduler_; diff --git a/android/src/main/cpp/headers/RuntimeProvider.h b/android/src/main/cpp/headers/RuntimeProvider.h index 02e7b7ae8e5..7407e5a1bb2 100644 --- a/android/src/main/cpp/headers/RuntimeProvider.h +++ b/android/src/main/cpp/headers/RuntimeProvider.h @@ -1,3 +1,5 @@ +#pragma once + #include #include #include @@ -15,11 +17,19 @@ namespace reanimated class RuntimeProvider { public: - static std::shared_ptr createRuntime( + RuntimeProvider() {} + ~RuntimeProvider() {} + + std::shared_ptr factory; + std::unique_ptr executor; + + std::shared_ptr createRuntime( + JavaScriptExecutorHolder* javaScriptExecutor, jni::alias_ref messageQueueThread, bool isDebug, int runtimeType ); + }; } diff --git a/android/src/main/java/com/swmansion/reanimated/NativeProxy.java b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java index 45d46a77666..ed977da35ab 100644 --- a/android/src/main/java/com/swmansion/reanimated/NativeProxy.java +++ b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java @@ -1,10 +1,17 @@ package com.swmansion.reanimated; +import android.content.Context; +import android.os.Debug; import android.os.SystemClock; import androidx.annotation.Nullable; import com.facebook.jni.HybridData; import com.facebook.proguard.annotations.DoNotStrip; +import com.facebook.react.BuildConfig; +import com.facebook.react.ReactInstanceManager; +import com.facebook.react.ReactInstanceManagerBuilder; +import com.facebook.react.bridge.JavaScriptExecutor; +import com.facebook.react.bridge.JavaScriptExecutorFactory; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.WritableArray; import com.facebook.react.bridge.WritableMap; @@ -18,6 +25,9 @@ import com.facebook.react.bridge.queue.ReactQueueConfigurationSpec; import java.lang.ref.WeakReference; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Map; public class NativeProxy { @@ -73,10 +83,12 @@ public void receiveTouches(String eventName, WritableArray touches, WritableArra private NodesManager mNodesManager; private final WeakReference mContext; private Scheduler mScheduler = null; + private JavaScriptExecutor mJavaScriptExecutor; private MessageQueueThread mMessageQueueThread; private ReactQueueConfigurationImpl queueHolder; public NativeProxy(ReactApplicationContext context) { + RuntimeType runtimeType = detectRuntime(getExecutorFactory(context)); queueHolder = ReactQueueConfigurationImpl.create( ReactQueueConfigurationSpec.createDefault(), new QueueThreadExceptionHandler() { @@ -91,40 +103,62 @@ public void handleException(Exception e) { CallInvokerHolderImpl holder = (CallInvokerHolderImpl)context.getCatalystInstance().getJSCallInvokerHolder(); mScheduler = new Scheduler(context); mHybridData = initHybrid( - context.getJavaScriptContextHolder().get(), - holder, - mScheduler, - mMessageQueueThread, - BuildConfig.DEBUG, - detectReactNativeRuntime(context).ordinal() + context.getJavaScriptContextHolder().get(), + holder, + mScheduler, + mJavaScriptExecutor, + mMessageQueueThread, + BuildConfig.DEBUG, + runtimeType.ordinal() ); mContext = new WeakReference<>(context); prepare(); } - // Method based on method from React Native - private RuntimeType detectReactNativeRuntime(ReactApplicationContext context) { - try { - SoLoader.init(context,false); - SoLoader.loadLibrary("jscexecutor"); - return RuntimeType.JSC; - } catch (UnsatisfiedLinkError jscE) { - if (jscE.getMessage().contains("__cxa_bad_typeid")) { - throw jscE; - } + private RuntimeType detectRuntime(JavaScriptExecutorFactory executorFactory) { + String factoryName = executorFactory.toString(); + if(factoryName.equals("JSIExecutor+HermesRuntime")) { + return RuntimeType.HERMES; + } + return RuntimeType.OTHER; + } + + private JavaScriptExecutorFactory getExecutorFactory(ReactApplicationContext context) { + JavaScriptExecutorFactory factory = null; try { - return RuntimeType.HERMES; - } catch (UnsatisfiedLinkError hermesE) { - hermesE.printStackTrace(); - throw jscE; + Constructor constructor = + (Constructor) + ReactInstanceManagerBuilder.class.getDeclaredConstructors()[0]; + constructor.setAccessible(true); + ReactInstanceManagerBuilder reactInstanceManagerBuilder = constructor.newInstance(); + Method getDefaultJSExecutorFactory = reactInstanceManagerBuilder + .getClass() + .getDeclaredMethod( + "getDefaultJSExecutorFactory", + String.class, + String.class, + Context.class + ); + getDefaultJSExecutorFactory.setAccessible(true); + factory = (JavaScriptExecutorFactory) getDefaultJSExecutorFactory.invoke( + reactInstanceManagerBuilder, + "Reaniamted", + "Reaniamted", + context + ); + assert factory != null; + mJavaScriptExecutor = factory.create(); + } catch (Exception e) { + e.printStackTrace(); } - } + return factory; } private native HybridData initHybrid( long jsContext, CallInvokerHolderImpl jsCallInvokerHolder, Scheduler scheduler, + JavaScriptExecutor mJavaScriptExecutor, MessageQueueThread mMessageQueueThread, boolean isDebug, int runtimeType diff --git a/android/src/main/java/com/swmansion/reanimated/RuntimeType.java b/android/src/main/java/com/swmansion/reanimated/RuntimeType.java index db6cb3d3767..8f2dee6aecf 100644 --- a/android/src/main/java/com/swmansion/reanimated/RuntimeType.java +++ b/android/src/main/java/com/swmansion/reanimated/RuntimeType.java @@ -1,6 +1,6 @@ package com.swmansion.reanimated; public enum RuntimeType { - HERMES, - JSC + HERMES, // Hermes runtime + OTHER // JSC runtime, V8 runtime } From 0b07031b49cb1747b8c5dc1221e2bcf365283543 Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Thu, 22 Apr 2021 20:47:47 +0200 Subject: [PATCH 07/12] Clean up --- android/build.gradle | 2 - android/src/main/JNI/Android.mk | 2 +- android/src/main/JNI/Application.mk | 2 +- android/src/main/JNI/react/Android.mk | 4 +- android/src/main/cpp/NativeProxy.cpp | 9 ---- .../headers/HermesExecutorRuntimeAdapter.h | 50 +++++++++++++++++++ android/src/main/cpp/headers/NativeProxy.h | 43 +--------------- .../src/main/cpp/headers/RuntimeProvider.h | 2 - .../com/swmansion/reanimated/NativeProxy.java | 4 -- package.json | 1 - yarn.lock | 5 -- 11 files changed, 54 insertions(+), 70 deletions(-) create mode 100644 android/src/main/cpp/headers/HermesExecutorRuntimeAdapter.h diff --git a/android/build.gradle b/android/build.gradle index 204fb46899f..83a4f9e149f 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -330,7 +330,6 @@ task prepareHermes() { } } -// Create Android.mk library module based on jsc from npm task prepareJSC { doLast { def jscPackagePath = findNodeModulePath(projectDir, "jsc-android") @@ -460,7 +459,6 @@ repositories { dependencies { //noinspection GradleDynamicVersion - // implementation "org.webkit:android-jsc:+" implementation "com.facebook.react:react-native:+" // From node_modules implementation "androidx.transition:transition:1.1.0" api("com.facebook.fbjni:fbjni-java-only:0.0.3") diff --git a/android/src/main/JNI/Android.mk b/android/src/main/JNI/Android.mk index a134aa30471..3ef8effe154 100644 --- a/android/src/main/JNI/Android.mk +++ b/android/src/main/JNI/Android.mk @@ -23,7 +23,7 @@ LOCAL_C_INCLUDES += $(LOCAL_PATH)/../Common/cpp/headers/SpecTools LOCAL_C_INCLUDES += $(LOCAL_PATH)/../Common/cpp/headers/Tools LOCAL_C_INCLUDES += $(HERMES_ENGINE)/android/include -LOCAL_CFLAGS += -DONANDROID -fexceptions -frtti +LOCAL_CFLAGS += -DONANDROID -fexceptions -frtti LOCAL_STATIC_LIBRARIES := libjsi callinvokerholder libhermes-inspector LOCAL_SHARED_LIBRARIES := libhermes libfolly_json libfbjni libreactnativejni diff --git a/android/src/main/JNI/Application.mk b/android/src/main/JNI/Application.mk index 1218e96f7fc..fcba131f8bd 100644 --- a/android/src/main/JNI/Application.mk +++ b/android/src/main/JNI/Application.mk @@ -1,7 +1,7 @@ ifdef $(CI) APP_ABI := x86 else -APP_ABI := x86 +APP_ABI := all endif APP_BUILD_SCRIPT := Android.mk diff --git a/android/src/main/JNI/react/Android.mk b/android/src/main/JNI/react/Android.mk index 73d8a4a63fa..bbe074a0581 100644 --- a/android/src/main/JNI/react/Android.mk +++ b/android/src/main/JNI/react/Android.mk @@ -18,7 +18,7 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH) # ./../ == react LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../.. -LOCAL_CFLAGS += -fexceptions -frtti -Wno-unused-lambda-capture -DHERMES_ENABLE_DEBUGGER=1 +LOCAL_CFLAGS += -fexceptions -frtti -Wno-unused-lambda-capture LOCAL_LDLIBS += -landroid @@ -63,7 +63,6 @@ include $(BUILD_SHARED_LIBRARY) $(call import-module,folly) $(call import-module,fb) $(call import-module,fbjni) -$(call import-module,jsc) $(call import-module,fbgloginit) $(call import-module,yogajni) $(call import-module,cxxreact) @@ -79,7 +78,6 @@ ifeq ($(shell test $(REACT_NATIVE_TARGET_VERSION) -ge 64; echo $$?),0) endif include $(REACT_SRC_DIR)/turbomodule/core/jni/Android.mk -include $(REACT_SRC_DIR)/../hermes/instrumentation/Android.mk include $(REACT_SRC_DIR)/modules/blob/jni/Android.mk diff --git a/android/src/main/cpp/NativeProxy.cpp b/android/src/main/cpp/NativeProxy.cpp index 11fe1f65965..bd87f93b76f 100644 --- a/android/src/main/cpp/NativeProxy.cpp +++ b/android/src/main/cpp/NativeProxy.cpp @@ -1,21 +1,12 @@ #include #include -#include -#include #include #include #include #include "NativeProxy.h" #include "AndroidErrorHandler.h" #include "AndroidScheduler.h" -#include #include "PlatformDepMethodsHolder.h" -#include -#include "Logger.h" -#include -#include -#include -#include #include #include diff --git a/android/src/main/cpp/headers/HermesExecutorRuntimeAdapter.h b/android/src/main/cpp/headers/HermesExecutorRuntimeAdapter.h new file mode 100644 index 00000000000..6849e165083 --- /dev/null +++ b/android/src/main/cpp/headers/HermesExecutorRuntimeAdapter.h @@ -0,0 +1,50 @@ +#pragma once + +#include +#include +#include +#include + +namespace reanimated { + +using namespace facebook; +using namespace react; + +class HermesExecutorRuntimeAdapter : public facebook::hermes::inspector::RuntimeAdapter { + public: + HermesExecutorRuntimeAdapter( + std::shared_ptr runtime, + facebook::hermes::HermesRuntime &hermesRuntime, + std::shared_ptr thread) + : runtime_(runtime), + hermesRuntime_(hermesRuntime), + thread_(std::move(thread)) {} + + virtual ~HermesExecutorRuntimeAdapter() = default; + + facebook::jsi::Runtime &getRuntime() override { + return *runtime_; + } + + facebook::hermes::debugger::Debugger &getDebugger() override { + return hermesRuntime_.getDebugger(); + } + + void tickleJs() override { + // The queue will ensure that runtime_ is still valid when this + // gets invoked. + thread_->runOnQueue([&runtime = runtime_]() { + auto func = + runtime->global().getPropertyAsFunction(*runtime, "__tickleJs"); + func.call(*runtime); + }); + } + + public: + std::shared_ptr runtime_; + facebook::hermes::HermesRuntime &hermesRuntime_; + + std::shared_ptr thread_; +}; + +} diff --git a/android/src/main/cpp/headers/NativeProxy.h b/android/src/main/cpp/headers/NativeProxy.h index 81faaff2a86..f148ef45412 100644 --- a/android/src/main/cpp/headers/NativeProxy.h +++ b/android/src/main/cpp/headers/NativeProxy.h @@ -14,11 +14,10 @@ #include "AndroidScheduler.h" #include #include -#include -#include #include #include #include "RuntimeProvider.h" +#include "HermesExecutorRuntimeAdapter.h" namespace reanimated { @@ -26,43 +25,6 @@ class RuntimeProvider; using namespace facebook; -class HermesExecutorRuntimeAdapter : public facebook::hermes::inspector::RuntimeAdapter { - public: - HermesExecutorRuntimeAdapter( - std::shared_ptr runtime, - facebook::hermes::HermesRuntime &hermesRuntime, - std::shared_ptr thread) - : runtime_(runtime), - hermesRuntime_(hermesRuntime), - thread_(std::move(thread)) {} - - virtual ~HermesExecutorRuntimeAdapter() = default; - - facebook::jsi::Runtime &getRuntime() override { - return *runtime_; - } - - facebook::hermes::debugger::Debugger &getDebugger() override { - return hermesRuntime_.getDebugger(); - } - - void tickleJs() override { - // The queue will ensure that runtime_ is still valid when this - // gets invoked. - thread_->runOnQueue([&runtime = runtime_]() { - auto func = - runtime->global().getPropertyAsFunction(*runtime, "__tickleJs"); - func.call(*runtime); - }); - } - - public: - std::shared_ptr runtime_; - facebook::hermes::HermesRuntime &hermesRuntime_; - - std::shared_ptr thread_; -}; - class AnimationFrameCallback : public HybridClass { public: static auto constexpr kJavaDescriptor = @@ -161,7 +123,4 @@ class NativeProxy : public jni::HybridClass { std::shared_ptr scheduler); }; - - - } diff --git a/android/src/main/cpp/headers/RuntimeProvider.h b/android/src/main/cpp/headers/RuntimeProvider.h index 7407e5a1bb2..f7951d35814 100644 --- a/android/src/main/cpp/headers/RuntimeProvider.h +++ b/android/src/main/cpp/headers/RuntimeProvider.h @@ -3,12 +3,10 @@ #include #include #include -#include #include #include #include #include "NativeProxy.h" -#include #include #include diff --git a/android/src/main/java/com/swmansion/reanimated/NativeProxy.java b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java index ed977da35ab..195118647ae 100644 --- a/android/src/main/java/com/swmansion/reanimated/NativeProxy.java +++ b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java @@ -1,14 +1,12 @@ package com.swmansion.reanimated; import android.content.Context; -import android.os.Debug; import android.os.SystemClock; import androidx.annotation.Nullable; import com.facebook.jni.HybridData; import com.facebook.proguard.annotations.DoNotStrip; import com.facebook.react.BuildConfig; -import com.facebook.react.ReactInstanceManager; import com.facebook.react.ReactInstanceManagerBuilder; import com.facebook.react.bridge.JavaScriptExecutor; import com.facebook.react.bridge.JavaScriptExecutorFactory; @@ -20,13 +18,11 @@ import com.facebook.react.turbomodule.core.CallInvokerHolderImpl; import com.facebook.react.uimanager.UIManagerModule; import com.facebook.react.uimanager.events.RCTEventEmitter; -import com.facebook.soloader.SoLoader; import com.facebook.react.bridge.queue.ReactQueueConfigurationImpl; import com.facebook.react.bridge.queue.ReactQueueConfigurationSpec; import java.lang.ref.WeakReference; import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Map; diff --git a/package.json b/package.json index 49c33d78bad..0cb37fcc74c 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,6 @@ "dependencies": { "@babel/plugin-transform-object-assign": "^7.10.4", "fbjs": "^3.0.0", - "jsc-android": "^250230.2.1", "mockdate": "^3.0.2", "string-hash-64": "^1.0.3" }, diff --git a/yarn.lock b/yarn.lock index 86a3700efbc..36324ffb3cb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6905,11 +6905,6 @@ jsc-android@^245459.0.0: version "245459.0.0" resolved "https://registry.yarnpkg.com/jsc-android/-/jsc-android-245459.0.0.tgz#e584258dd0b04c9159a27fb104cd5d491fd202c9" -jsc-android@^250230.2.1: - version "250230.2.1" - resolved "https://registry.yarnpkg.com/jsc-android/-/jsc-android-250230.2.1.tgz#3790313a970586a03ab0ad47defbc84df54f1b83" - integrity sha512-KmxeBlRjwoqCnBBKGsihFtvsBHyUFlBxJPK4FzeYcIuBfdjv6jFys44JITAgSTbQD+vIdwMEfyZklsuQX0yI1Q== - jscodeshift@^0.11.0: version "0.11.0" resolved "https://registry.yarnpkg.com/jscodeshift/-/jscodeshift-0.11.0.tgz#4f95039408f3f06b0e39bb4d53bc3139f5330e2f" From a216e54ff4da794cc12c7bba1de93e8dccbee83c Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Fri, 23 Apr 2021 10:18:34 +0200 Subject: [PATCH 08/12] isDebuggable check --- android/src/main/cpp/RuntimeProvider.cpp | 2 +- .../src/main/java/com/swmansion/reanimated/NativeProxy.java | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/android/src/main/cpp/RuntimeProvider.cpp b/android/src/main/cpp/RuntimeProvider.cpp index 5ccfffe6f9c..8f75318eba0 100644 --- a/android/src/main/cpp/RuntimeProvider.cpp +++ b/android/src/main/cpp/RuntimeProvider.cpp @@ -30,7 +30,7 @@ std::shared_ptr RuntimeProvider::createRuntime( SystraceSection s("RuntimeProvider::createRuntime"); facebook::hermes::HermesRuntime &hermesRuntimeRef = *runtime; auto adapter = std::make_unique(std::move(runtime), hermesRuntimeRef, jsQueue); - std::shared_ptr runtimeShared = adapter->runtime_; + runtimeShared = adapter->runtime_; facebook::hermes::inspector::chrome::enableDebugging(std::move(adapter), "Reanimated Runtime"); } else { diff --git a/android/src/main/java/com/swmansion/reanimated/NativeProxy.java b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java index 195118647ae..a1627870fd0 100644 --- a/android/src/main/java/com/swmansion/reanimated/NativeProxy.java +++ b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java @@ -1,12 +1,12 @@ package com.swmansion.reanimated; import android.content.Context; +import android.content.pm.ApplicationInfo; import android.os.SystemClock; import androidx.annotation.Nullable; import com.facebook.jni.HybridData; import com.facebook.proguard.annotations.DoNotStrip; -import com.facebook.react.BuildConfig; import com.facebook.react.ReactInstanceManagerBuilder; import com.facebook.react.bridge.JavaScriptExecutor; import com.facebook.react.bridge.JavaScriptExecutorFactory; @@ -98,13 +98,14 @@ public void handleException(Exception e) { CallInvokerHolderImpl holder = (CallInvokerHolderImpl)context.getCatalystInstance().getJSCallInvokerHolder(); mScheduler = new Scheduler(context); + boolean isDebuggable = ( 0 != ( context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE ) ); mHybridData = initHybrid( context.getJavaScriptContextHolder().get(), holder, mScheduler, mJavaScriptExecutor, mMessageQueueThread, - BuildConfig.DEBUG, + isDebuggable, runtimeType.ordinal() ); mContext = new WeakReference<>(context); From 01817f2fb326959e9703565928ab22df219bf460 Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Fri, 23 Apr 2021 10:22:12 +0200 Subject: [PATCH 09/12] Remove unused lib import --- android/src/main/JNI/react/Android.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/JNI/react/Android.mk b/android/src/main/JNI/react/Android.mk index bbe074a0581..51b9d66c037 100644 --- a/android/src/main/JNI/react/Android.mk +++ b/android/src/main/JNI/react/Android.mk @@ -26,7 +26,7 @@ LOCAL_LDLIBS += -landroid LOCAL_SHARED_LIBRARIES := libfolly_json libfb libfbjni libglog_init libyoga # The static libraries (.a files) that this module depends on. -LOCAL_STATIC_LIBRARIES := libreactnative libcallinvokerholder libhermes-inspector +LOCAL_STATIC_LIBRARIES := libreactnative libcallinvokerholder # Name of this module. # From 0b07f92c77c823c697daef07fc394c3fb2375356 Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Thu, 6 May 2021 18:27:51 +0200 Subject: [PATCH 10/12] Fix typo --- .../src/main/java/com/swmansion/reanimated/NativeProxy.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/swmansion/reanimated/NativeProxy.java b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java index a1627870fd0..a10872a9b82 100644 --- a/android/src/main/java/com/swmansion/reanimated/NativeProxy.java +++ b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java @@ -139,8 +139,8 @@ private JavaScriptExecutorFactory getExecutorFactory(ReactApplicationContext con getDefaultJSExecutorFactory.setAccessible(true); factory = (JavaScriptExecutorFactory) getDefaultJSExecutorFactory.invoke( reactInstanceManagerBuilder, - "Reaniamted", - "Reaniamted", + "Reanimated", + "Reanimated", context ); assert factory != null; From 5e586ee34a7417002323eaeec882f222fb10477f Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Mon, 24 May 2021 10:53:19 +0200 Subject: [PATCH 11/12] Update cmake --- android/CMakeLists.txt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/android/CMakeLists.txt b/android/CMakeLists.txt index 6592f604658..10c32974c16 100644 --- a/android/CMakeLists.txt +++ b/android/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.5.1) set (CMAKE_VERBOSE_MAKEFILE ON) set (CMAKE_CXX_STANDARD 14) -set (CMAKE_CXX_FLAGS "-DFOLLY_NO_CONFIG=1 -DFOLLY_HAVE_CLOCK_GETTIME=1 -DFOLLY_HAVE_MEMRCHR=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_MOBILE=1 -DON_ANDROID -DONANDROID -fexceptions -fno-omit-frame-pointer -frtti -Wno-sign-compare") +set (CMAKE_CXX_FLAGS "-DFOLLY_NO_CONFIG=1 -DFOLLY_HAVE_CLOCK_GETTIME=1 -DFOLLY_HAVE_MEMRCHR=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_MOBILE=1 -DON_ANDROID -DONANDROID -fexceptions -fno-omit-frame-pointer -frtti -Wno-sign-compare -DHERMES_ENABLE_DEBUGGER=1") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "") @@ -63,6 +63,8 @@ target_include_directories( "${NODE_MODULES_DIR}/react-native/ReactCommon/jsi" "${NODE_MODULES_DIR}/react-native/ReactCommon/turbomodule/core" "${NODE_MODULES_DIR}/react-native/ReactCommon/turbomodule" + "${NODE_MODULES_DIR}/react-native/ReactCommon/runtimeexecutor" + "${NODE_MODULES_DIR}/hermes-engine/android/include/" "./src/main/Common/cpp/headers/Tools" "./src/main/Common/cpp/headers/SpecTools" "./src/main/Common/cpp/headers/NativeModules" @@ -125,6 +127,12 @@ find_library( PATHS ${LIBRN_DIR} NO_CMAKE_FIND_ROOT_PATH ) +find_library( + HERMES_INSPECTOR_LIB + hermes-inspector + PATHS ${LIBRN_DIR} + NO_CMAKE_FIND_ROOT_PATH +) # build shared lib set_target_properties(${PACKAGE_NAME} PROPERTIES LINKER_LANGUAGE CXX) @@ -136,6 +144,7 @@ if(${FOR_HERMES}) ${PACKAGE_NAME} ${LOG_LIB} ${HERMES_LIB} + ${HERMES_INSPECTOR_LIB} ${GLOG_LIB} ${FBJNI_LIB} ${FOLLY_JSON_LIB} From ecf4ebe22f45f7ccda553a244114436c3f8b0263 Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Mon, 24 May 2021 12:13:55 +0200 Subject: [PATCH 12/12] Update lib order --- Example/android/app/build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/Example/android/app/build.gradle b/Example/android/app/build.gradle index 06f4eb41878..e1d35e73170 100644 --- a/Example/android/app/build.gradle +++ b/Example/android/app/build.gradle @@ -156,6 +156,7 @@ android { pickFirst 'lib/**/libhermes.so' pickFirst 'lib/**/libfolly_json.so' pickFirst 'lib/**/jscexecutor.so' + pickFirst 'lib/**/libhermes-inspector.so' } }