Do not create RuntimeExecutor on non-JSI executors (#38125) (#38142)

Co-authored-by: Pieter De Baets <pieterdb@meta.com>
resolved: https://github.com/facebook/react-native/pull/38125
This commit is contained in:
Luna
2023-07-10 11:27:43 +01:00
committed by GitHub
co-authored by Pieter De Baets
parent e22bd7f6a9
commit d73b61c7c7
4 changed files with 44 additions and 32 deletions
@@ -418,8 +418,11 @@ CatalystInstanceImpl::getNativeCallInvokerHolder() {
jni::alias_ref<JRuntimeExecutor::javaobject>
CatalystInstanceImpl::getRuntimeExecutor() {
if (!runtimeExecutor_) {
runtimeExecutor_ = jni::make_global(
JRuntimeExecutor::newObjectCxxArgs(instance_->getRuntimeExecutor()));
auto executor = instance_->getRuntimeExecutor();
if (executor) {
runtimeExecutor_ =
jni::make_global(JRuntimeExecutor::newObjectCxxArgs(executor));
}
}
return runtimeExecutor_;
}
@@ -428,15 +431,16 @@ jni::alias_ref<JRuntimeScheduler::javaobject>
CatalystInstanceImpl::getRuntimeScheduler() {
if (!runtimeScheduler_) {
auto runtimeExecutor = instance_->getRuntimeExecutor();
auto runtimeScheduler = std::make_shared<RuntimeScheduler>(runtimeExecutor);
runtimeScheduler_ =
jni::make_global(JRuntimeScheduler::newObjectCxxArgs(runtimeScheduler));
runtimeExecutor([runtimeScheduler](jsi::Runtime &runtime) {
RuntimeSchedulerBinding::createAndInstallIfNeeded(
runtime, runtimeScheduler);
});
if (runtimeExecutor) {
auto runtimeScheduler =
std::make_shared<RuntimeScheduler>(runtimeExecutor);
runtimeScheduler_ = jni::make_global(
JRuntimeScheduler::newObjectCxxArgs(runtimeScheduler));
runtimeExecutor([scheduler =
std::move(runtimeScheduler)](jsi::Runtime &runtime) {
RuntimeSchedulerBinding::createAndInstallIfNeeded(runtime, scheduler);
});
}
}
return runtimeScheduler_;
@@ -27,4 +27,5 @@ target_link_libraries(reactnative
jsinspector
logger
reactperflogger
runtimeexecutor)
runtimeexecutor
react_debug)
@@ -21,6 +21,7 @@
#include <cxxreact/JSIndexedRAMBundle.h>
#include <folly/MoveWrapper.h>
#include <folly/json.h>
#include <react/debug/react_native_assert.h>
#include <glog/logging.h>
@@ -212,26 +213,31 @@ std::shared_ptr<CallInvoker> Instance::getJSCallInvoker() {
}
RuntimeExecutor Instance::getRuntimeExecutor() {
std::weak_ptr<NativeToJsBridge> weakNativeToJsBridge = nativeToJsBridge_;
// HACK: RuntimeExecutor is not compatible with non-JSIExecutor, we return
// a null callback, which the caller should handle.
if (!getJavaScriptContext()) {
return nullptr;
}
auto runtimeExecutor =
[weakNativeToJsBridge](
std::function<void(jsi::Runtime & runtime)> &&callback) {
if (auto strongNativeToJsBridge = weakNativeToJsBridge.lock()) {
strongNativeToJsBridge->runOnExecutorQueue(
[callback = std::move(callback)](JSExecutor *executor) {
jsi::Runtime *runtime =
(jsi::Runtime *)executor->getJavaScriptContext();
try {
callback(*runtime);
executor->flush();
} catch (jsi::JSError &originalError) {
handleJSError(*runtime, originalError, true);
}
});
}
};
return runtimeExecutor;
std::weak_ptr<NativeToJsBridge> weakNativeToJsBridge = nativeToJsBridge_;
return [weakNativeToJsBridge](
std::function<void(jsi::Runtime & runtime)> &&callback) {
if (auto strongNativeToJsBridge = weakNativeToJsBridge.lock()) {
strongNativeToJsBridge->runOnExecutorQueue(
[callback = std::move(callback)](JSExecutor *executor) {
// Assumes the underlying executor is a JSIExecutor
jsi::Runtime *runtime =
(jsi::Runtime *)executor->getJavaScriptContext();
try {
react_native_assert(runtime != nullptr);
callback(*runtime);
executor->flush();
} catch (jsi::JSError &originalError) {
handleJSError(*runtime, originalError, true);
}
});
}
};
}
std::shared_ptr<CallInvoker> Instance::getDecoratedNativeCallInvoker(
@@ -33,7 +33,7 @@ Pod::Spec.new do |s|
s.source_files = "*.{cpp,h}"
s.exclude_files = "SampleCxxModule.*"
s.compiler_flags = folly_compiler_flags + ' ' + boost_compiler_flags
s.pod_target_xcconfig = { "HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\" \"$(PODS_ROOT)/RCT-Folly\" \"$(PODS_ROOT)/DoubleConversion\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-runtimeexecutor/React_runtimeexecutor.framework/Headers\"",
s.pod_target_xcconfig = { "HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\" \"$(PODS_ROOT)/RCT-Folly\" \"$(PODS_ROOT)/DoubleConversion\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-runtimeexecutor/React_runtimeexecutor.framework/Headers\" \"$(PODS_CONFIGURATION_BUILD_DIR)/React-debug/React_debug.framework/Headers\"",
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17" }
s.header_dir = "cxxreact"
@@ -47,6 +47,7 @@ Pod::Spec.new do |s|
s.dependency "React-perflogger", version
s.dependency "React-jsi", version
s.dependency "React-logger", version
s.dependency "React-debug", version
if ENV['USE_HERMES'] == nil || ENV['USE_HERMES'] == "1"
s.dependency 'hermes-engine'