Skip to content

Commit

Permalink
Fix runtime creation (#2047)
Browse files Browse the repository at this point in the history
## Description

Replacement for PR #1960

## Checklist

- [x] Ensured that CI passes
  • Loading branch information
piaskowyk authored May 26, 2021
1 parent 458fa65 commit b9532cf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 69 deletions.
3 changes: 2 additions & 1 deletion android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 "")
Expand Down Expand Up @@ -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"
Expand Down
23 changes: 12 additions & 11 deletions android/src/main/cpp/NativeProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#include <react/jni/ReadableNativeMap.h>
#include <jsi/JSIDynamic.h>

#if FOR_HERMES
#import <hermes/hermes.h>
#else
#import <jsi/JSCRuntime.h>
#endif

#include "NativeProxy.h"
#include "AndroidErrorHandler.h"
#include "AndroidScheduler.h"
Expand All @@ -30,19 +36,15 @@ NativeProxy::NativeProxy(
{
}

JavaScriptExecutorHolder* NativeProxy::_javaScriptExecutor = NULL;

jni::local_ref<NativeProxy::jhybriddata> NativeProxy::initHybrid(
jni::alias_ref<jhybridobject> jThis,
jlong jsContext,
jni::alias_ref<facebook::react::CallInvokerHolder::javaobject> jsCallInvokerHolder,
jni::alias_ref<AndroidScheduler::javaobject> androidScheduler,
JavaScriptExecutorHolder* javaScriptExecutor)
jni::alias_ref<AndroidScheduler::javaobject> androidScheduler)
{
auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker();
auto scheduler = androidScheduler->cthis()->getScheduler();
scheduler->setJSCallInvoker(jsCallInvoker);
_javaScriptExecutor = javaScriptExecutor;
return makeCxxInstance(jThis, (jsi::Runtime *)jsContext, jsCallInvoker, scheduler);
}

Expand Down Expand Up @@ -92,12 +94,11 @@ void NativeProxy::installJSIBindings()
scrollTo(viewTag, x, y, animated);
};

std::shared_ptr<ExecutorDelegate> delegate = std::shared_ptr<ExecutorDelegate>();
std::shared_ptr<MessageQueueThread> jsQueue = std::shared_ptr<MessageQueueThread>();
factory = _javaScriptExecutor->getExecutorFactory();
executor = factory.get()->createJSExecutor(delegate, jsQueue);
std::unique_ptr<jsi::Runtime> animatedRuntime;
animatedRuntime.reset(static_cast<jsi::Runtime*>(executor.get()->getJavaScriptContext()));
#if FOR_HERMES
auto animatedRuntime = facebook::hermes::makeHermesRuntime();
#else
auto animatedRuntime = facebook::jsc::makeJSCRuntime();
#endif

std::shared_ptr<ErrorHandler> errorHandler = std::make_shared<AndroidErrorHandler>(scheduler_);

Expand Down
6 changes: 1 addition & 5 deletions android/src/main/cpp/headers/NativeProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,14 @@ class NativeProxy : public jni::HybridClass<NativeProxy> {
jni::alias_ref<jhybridobject> jThis,
jlong jsContext,
jni::alias_ref<facebook::react::CallInvokerHolder::javaobject> jsCallInvokerHolder,
jni::alias_ref<AndroidScheduler::javaobject> scheduler,
JavaScriptExecutorHolder* javaScriptExecutor);
jni::alias_ref<AndroidScheduler::javaobject> scheduler);
static void registerNatives();
static JavaScriptExecutorHolder* _javaScriptExecutor;


private:
friend HybridBase;
jni::global_ref<NativeProxy::javaobject> javaPart_;
jsi::Runtime *runtime_;
std::shared_ptr<JSExecutorFactory> factory;
std::unique_ptr<JSExecutor> executor;
std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker_;
std::shared_ptr<NativeReanimatedModule> _nativeReanimatedModule;
std::shared_ptr<Scheduler> scheduler_;
Expand Down
54 changes: 2 additions & 52 deletions android/src/main/java/com/swmansion/reanimated/NativeProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b9532cf

Please sign in to comment.