From b9532cf33cabff14fd50f9aedd47b996a9edeaa1 Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Wed, 26 May 2021 11:19:54 +0200 Subject: [PATCH] Fix runtime creation (#2047) ## Description Replacement for PR https://github.com/software-mansion/react-native-reanimated/pull/1960 ## Checklist - [x] Ensured that CI passes --- android/CMakeLists.txt | 3 +- android/src/main/cpp/NativeProxy.cpp | 23 ++++---- android/src/main/cpp/headers/NativeProxy.h | 6 +-- .../com/swmansion/reanimated/NativeProxy.java | 54 +------------------ 4 files changed, 17 insertions(+), 69 deletions(-) diff --git a/android/CMakeLists.txt b/android/CMakeLists.txt index 6592f604658..46a23c11b8d 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 -DFOR_HERMES=${FOR_HERMES} -fexceptions -fno-omit-frame-pointer -frtti -Wno-sign-compare") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "") @@ -63,6 +63,7 @@ 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}/hermes-engine/android/include/" "./src/main/Common/cpp/headers/Tools" "./src/main/Common/cpp/headers/SpecTools" "./src/main/Common/cpp/headers/NativeModules" diff --git a/android/src/main/cpp/NativeProxy.cpp b/android/src/main/cpp/NativeProxy.cpp index 2616c3ff08e..a8f4057b453 100644 --- a/android/src/main/cpp/NativeProxy.cpp +++ b/android/src/main/cpp/NativeProxy.cpp @@ -7,6 +7,12 @@ #include #include +#if FOR_HERMES +#import +#else +#import +#endif + #include "NativeProxy.h" #include "AndroidErrorHandler.h" #include "AndroidScheduler.h" @@ -30,19 +36,15 @@ NativeProxy::NativeProxy( { } -JavaScriptExecutorHolder* NativeProxy::_javaScriptExecutor = NULL; - jni::local_ref NativeProxy::initHybrid( jni::alias_ref jThis, jlong jsContext, jni::alias_ref jsCallInvokerHolder, - jni::alias_ref androidScheduler, - JavaScriptExecutorHolder* javaScriptExecutor) + jni::alias_ref androidScheduler) { auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker(); auto scheduler = androidScheduler->cthis()->getScheduler(); scheduler->setJSCallInvoker(jsCallInvoker); - _javaScriptExecutor = javaScriptExecutor; return makeCxxInstance(jThis, (jsi::Runtime *)jsContext, jsCallInvoker, scheduler); } @@ -92,12 +94,11 @@ void NativeProxy::installJSIBindings() scrollTo(viewTag, x, y, animated); }; - std::shared_ptr delegate = std::shared_ptr(); - 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())); +#if FOR_HERMES + auto animatedRuntime = facebook::hermes::makeHermesRuntime(); +#else + auto animatedRuntime = facebook::jsc::makeJSCRuntime(); +#endif 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..51221949d16 100644 --- a/android/src/main/cpp/headers/NativeProxy.h +++ b/android/src/main/cpp/headers/NativeProxy.h @@ -83,18 +83,14 @@ class NativeProxy : public jni::HybridClass { jni::alias_ref jThis, jlong jsContext, jni::alias_ref jsCallInvokerHolder, - jni::alias_ref scheduler, - JavaScriptExecutorHolder* javaScriptExecutor); + jni::alias_ref scheduler); static void registerNatives(); - static JavaScriptExecutorHolder* _javaScriptExecutor; 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/java/com/swmansion/reanimated/NativeProxy.java b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java index 6e48143fc04..4636782836f 100644 --- a/android/src/main/java/com/swmansion/reanimated/NativeProxy.java +++ b/android/src/main/java/com/swmansion/reanimated/NativeProxy.java @@ -6,7 +6,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; @@ -17,10 +16,6 @@ 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; - public class NativeProxy { static { @@ -77,59 +72,14 @@ public void receiveTouches(String eventName, WritableArray touches, WritableArra private Scheduler mScheduler = null; public NativeProxy(ReactApplicationContext context) { - try { - mJavaScriptExecutor = getDefaultJSExecutorFactory(context).create(); - } catch (Exception jscE) { - jscE.printStackTrace(); - } - 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); mContext = new WeakReference<>(context); 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; - } - } - } - - private native HybridData initHybrid(long jsContext, CallInvokerHolderImpl jsCallInvokerHolder, Scheduler scheduler, JavaScriptExecutor mJavaScriptExecutor); + private native HybridData initHybrid(long jsContext, CallInvokerHolderImpl jsCallInvokerHolder, Scheduler scheduler); private native void installJSIBindings(); public native boolean isAnyHandlerWaitingForEvent(String eventName);