Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix runtime creation #1960

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Common/cpp/NativeModules/NativeReanimatedModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ std::vector<std::shared_ptr<MutableValue>> extractMutablesFromArray(jsi::Runtime

NativeReanimatedModule::NativeReanimatedModule(std::shared_ptr<CallInvoker> jsInvoker,
std::shared_ptr<Scheduler> scheduler,
std::unique_ptr<jsi::Runtime> rt,
std::shared_ptr<jsi::Runtime> rt,
std::shared_ptr<ErrorHandler> errorHandler,
std::function<jsi::Value(jsi::Runtime &, const int, const jsi::String &)> propObtainer,
PlatformDepMethodsHolder platformDepMethodsHolder) :
NativeReanimatedModuleSpec(jsInvoker),
RuntimeManager(std::move(rt), errorHandler, scheduler),
RuntimeManager(rt, errorHandler, scheduler),
mapperRegistry(std::make_shared<MapperRegistry>()),
eventHandlerRegistry(std::make_shared<EventHandlerRegistry>()),
requestRender(platformDepMethodsHolder.requestRender),
Expand Down
2 changes: 1 addition & 1 deletion Common/cpp/headers/NativeModules/NativeReanimatedModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec, public Runtime
public:
NativeReanimatedModule(std::shared_ptr<CallInvoker> jsInvoker,
std::shared_ptr<Scheduler> scheduler,
std::unique_ptr<jsi::Runtime> rt,
std::shared_ptr<jsi::Runtime> rt,
std::shared_ptr<ErrorHandler> errorHandler,
std::function<jsi::Value(jsi::Runtime &, const int, const jsi::String &)> propObtainer,
PlatformDepMethodsHolder platformDepMethodsHolder);
Expand Down
6 changes: 3 additions & 3 deletions Common/cpp/headers/SharedItems/RuntimeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ using namespace facebook;
*/
class RuntimeManager {
public:
RuntimeManager(std::unique_ptr<jsi::Runtime>&& runtime,
RuntimeManager(std::shared_ptr<jsi::Runtime> _runtime,
std::shared_ptr<ErrorHandler> errorHandler,
std::shared_ptr<Scheduler> scheduler): runtime(std::move(runtime)), errorHandler(errorHandler), scheduler(scheduler), workletsCache(std::make_unique<WorkletsCache>()) { }
std::shared_ptr<Scheduler> scheduler): runtime(_runtime), errorHandler(errorHandler), scheduler(scheduler), workletsCache(std::make_unique<WorkletsCache>()) { }
public:
/**
Holds the jsi::Function worklet that is responsible for updating values in JS.
Expand All @@ -28,7 +28,7 @@ class RuntimeManager {
/**
Holds the jsi::Runtime this RuntimeManager is managing.
*/
std::unique_ptr<jsi::Runtime> runtime;
std::shared_ptr<jsi::Runtime> runtime;
/**
Holds the error handler that will be invoked when any kind of error occurs.
*/
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ 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",
"NATIVE_DEBUG=${debugNativeLibraries}",
"-C", file("src/main/JNI").absolutePath,
"--jobs", project.findProperty("jobs") ?: Runtime.runtime.availableProcessors(),
Expand Down Expand Up @@ -464,4 +465,3 @@ dependencies {
extractHeaders("com.facebook.fbjni:fbjni:0.0.2:headers")
extractJNI("com.facebook.fbjni:fbjni:0.0.2")
}

5 changes: 3 additions & 2 deletions android/src/main/JNI/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious that if adding libhermes here, will mark libhermes.so as NEEDED in libreanimated.so again?
For JSC apps to load libreanimated.so, there should be errors as libhermes.so is not packed into apk.

btw thank you @piaskowyk for the awesome work trying to decouple hermes in reanimated 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it won't work because RN doesn't provide libhermes.so if you choose to use jsc.


include $(BUILD_SHARED_LIBRARY)

Expand Down
2 changes: 1 addition & 1 deletion android/src/main/JNI/Application.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion android/src/main/JNI/react/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,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)
Expand Down
28 changes: 12 additions & 16 deletions android/src/main/cpp/NativeProxy.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#include <memory>
#include <string>

#include <fbjni/fbjni.h>
#include <jsi/jsi.h>
#include <react/jni/ReadableNativeArray.h>
#include <react/jni/ReadableNativeMap.h>
#include <jsi/JSIDynamic.h>

#include "NativeProxy.h"
#include "AndroidErrorHandler.h"
#include "AndroidScheduler.h"
#include <android/log.h>
#include "PlatformDepMethodsHolder.h"
#include <hermes/inspector/RuntimeAdapter.h>
#include <hermes/inspector/chrome/Registration.h>

namespace reanimated
{
Expand All @@ -31,18 +28,26 @@ NativeProxy::NativeProxy(
}

JavaScriptExecutorHolder* NativeProxy::_javaScriptExecutor = NULL;
std::shared_ptr<jsi::Runtime> NativeProxy::_animatedRuntime;
RuntimeProvider NativeProxy::runtimeProvider;

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)
JavaScriptExecutorHolder* javaScriptExecutor,
jni::alias_ref<JavaMessageQueueThread::javaobject> messageQueueThread,
bool isDebug,
int runtimeType)
{
auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker();
auto scheduler = androidScheduler->cthis()->getScheduler();
scheduler->setJSCallInvoker(jsCallInvoker);
_javaScriptExecutor = javaScriptExecutor;

_animatedRuntime = runtimeProvider.createRuntime(javaScriptExecutor, messageQueueThread, isDebug, runtimeType);

return makeCxxInstance(jThis, (jsi::Runtime *)jsContext, jsCallInvoker, scheduler);
}

Expand Down Expand Up @@ -91,16 +96,7 @@ void NativeProxy::installJSIBindings()
auto scrollToFunction = [this](int viewTag, double x, double y, bool animated) -> void {
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()));

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

PlatformDepMethodsHolder platformDepMethodsHolder = {
requestRender,
propUpdater,
Expand All @@ -111,7 +107,7 @@ void NativeProxy::installJSIBindings()

auto module = std::make_shared<NativeReanimatedModule>(jsCallInvoker_,
scheduler_,
std::move(animatedRuntime),
_animatedRuntime,
errorHandler,
propObtainer,
platformDepMethodsHolder);
Expand Down
42 changes: 42 additions & 0 deletions android/src/main/cpp/RuntimeProvider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "RuntimeProvider.h"

#include <hermes/hermes.h>

namespace reanimated
{

std::shared_ptr<jsi::Runtime> RuntimeProvider::createRuntime(
JavaScriptExecutorHolder* javaScriptExecutor,
jni::alias_ref<JavaMessageQueueThread::javaobject> messageQueueThread,
bool isDebug,
int runtimeType
) {

if(runtimeType == 1) { // JSC, V8
std::shared_ptr<ExecutorDelegate> delegate = std::shared_ptr<ExecutorDelegate>();
auto jsQueue = std::make_shared<JMessageQueueThread>(messageQueueThread);
factory = javaScriptExecutor->getExecutorFactory();
executor = factory.get()->createJSExecutor(delegate, jsQueue);
std::shared_ptr<jsi::Runtime> runtimeShared;
runtimeShared.reset(static_cast<jsi::Runtime*>(executor.get()->getJavaScriptContext()));
return runtimeShared;
}

// Hermes
std::shared_ptr<jsi::Runtime> runtimeShared;
std::unique_ptr<facebook::hermes::HermesRuntime> runtime = facebook::hermes::makeHermesRuntime();
if(isDebug) {
auto jsQueue = std::make_shared<JMessageQueueThread>(messageQueueThread);
SystraceSection s("RuntimeProvider::createRuntime");
facebook::hermes::HermesRuntime &hermesRuntimeRef = *runtime;
auto adapter = std::make_unique<HermesExecutorRuntimeAdapter>(std::move(runtime), hermesRuntimeRef, jsQueue);
runtimeShared = adapter->runtime_;
facebook::hermes::inspector::chrome::enableDebugging(std::move(adapter), "Reanimated Runtime");
}
else {
runtimeShared = std::move(runtime);
}
return runtimeShared;
}

}
50 changes: 50 additions & 0 deletions android/src/main/cpp/headers/HermesExecutorRuntimeAdapter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include <memory>
#include <hermes/hermes.h>
#include <hermes/inspector/RuntimeAdapter.h>
#include <hermes/inspector/chrome/Registration.h>

namespace reanimated {

using namespace facebook;
using namespace react;

class HermesExecutorRuntimeAdapter : public facebook::hermes::inspector::RuntimeAdapter {
public:
HermesExecutorRuntimeAdapter(
std::shared_ptr<facebook::jsi::Runtime> runtime,
facebook::hermes::HermesRuntime &hermesRuntime,
std::shared_ptr<MessageQueueThread> 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<facebook::jsi::Runtime> runtime_;
facebook::hermes::HermesRuntime &hermesRuntime_;

std::shared_ptr<MessageQueueThread> thread_;
};

}
22 changes: 14 additions & 8 deletions android/src/main/cpp/headers/NativeProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
#include <react/jni/JavaScriptExecutorHolder.h>
#include <memory>
#include <unordered_map>


#include "Scheduler.h"
#include "AndroidScheduler.h"
#include <cxxreact/MessageQueueThread.h>
#include <cxxreact/SystraceSection.h>
#include <hermes/inspector/RuntimeAdapter.h>
#include <hermes/inspector/chrome/Registration.h>
#include "RuntimeProvider.h"
#include "HermesExecutorRuntimeAdapter.h"

namespace reanimated {

class RuntimeProvider;

using namespace facebook;

class AnimationFrameCallback : public HybridClass<AnimationFrameCallback> {
Expand Down Expand Up @@ -84,17 +90,20 @@ class NativeProxy : public jni::HybridClass<NativeProxy> {
jlong jsContext,
jni::alias_ref<facebook::react::CallInvokerHolder::javaobject> jsCallInvokerHolder,
jni::alias_ref<AndroidScheduler::javaobject> scheduler,
JavaScriptExecutorHolder* javaScriptExecutor);
JavaScriptExecutorHolder* javaScriptExecutor,
jni::alias_ref<JavaMessageQueueThread::javaobject> messageQueueThread,
bool isDebug,
int runtimeType);
static void registerNatives();
static JavaScriptExecutorHolder* _javaScriptExecutor;
static std::shared_ptr<jsi::Runtime> _animatedRuntime;
static RuntimeProvider runtimeProvider;


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 All @@ -114,7 +123,4 @@ class NativeProxy : public jni::HybridClass<NativeProxy> {
std::shared_ptr<Scheduler> scheduler);
};




}
33 changes: 33 additions & 0 deletions android/src/main/cpp/headers/RuntimeProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <memory>
#include <fbjni/fbjni.h>
#include <jsi/jsi.h>
#include <cxxreact/NativeToJsBridge.h>
#include <cxxreact/MessageQueueThread.h>
#include <cxxreact/SystraceSection.h>
#include "NativeProxy.h"
#include <hermes/inspector/RuntimeAdapter.h>
#include <hermes/inspector/chrome/Registration.h>

namespace reanimated
{

class RuntimeProvider {
public:
RuntimeProvider() {}
~RuntimeProvider() {}

std::shared_ptr<JSExecutorFactory> factory;
std::unique_ptr<JSExecutor> executor;

std::shared_ptr<jsi::Runtime> createRuntime(
JavaScriptExecutorHolder* javaScriptExecutor,
jni::alias_ref<JavaMessageQueueThread::javaobject> messageQueueThread,
bool isDebug,
int runtimeType
);

};

}
Loading