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 all 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
1 change: 1 addition & 0 deletions Example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ android {
pickFirst 'lib/**/libhermes.so'
pickFirst 'lib/**/libfolly_json.so'
pickFirst 'lib/**/jscexecutor.so'
pickFirst 'lib/**/libhermes-inspector.so'
}
}

Expand Down
11 changes: 10 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 -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 "")
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -136,6 +144,7 @@ if(${FOR_HERMES})
${PACKAGE_NAME}
${LOG_LIB}
${HERMES_LIB}
${HERMES_INSPECTOR_LIB}
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe Hermes inspector is available only in debug build so we cannot merge this in the current shape.

${GLOG_LIB}
${FBJNI_LIB}
${FOLLY_JSON_LIB}
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