From db3625a3b0ded78788d25d691cc3b83a9ee8d2c6 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Fri, 23 Apr 2021 18:14:35 -0700 Subject: [PATCH] Refactor: Move RuntimeExecutor into Instance.cpp Summary: RuntimeExecutor is currently declared inside NativeToJsBridge. It doesn't need to be: Instance.cpp can use NativeToJsBridge::runOnExecutorQueue to schedule work on the JS Thread. So, this diff moves RuntimeExecutor out of NativeToJsBridge into Instance.cpp. Now, both the JS CallInvoker and the RuntimeExecutor are declared in the same file. Changelog: [Internal] Reviewed By: JoshuaGross Differential Revision: D27975840 fbshipit-source-id: aa06f479fa24bb7a15bfd21712df5414a183266c --- ReactCommon/cxxreact/Instance.cpp | 22 +++++++++++++++++++++- ReactCommon/cxxreact/NativeToJsBridge.cpp | 21 --------------------- ReactCommon/cxxreact/NativeToJsBridge.h | 6 ------ 3 files changed, 21 insertions(+), 28 deletions(-) diff --git a/ReactCommon/cxxreact/Instance.cpp b/ReactCommon/cxxreact/Instance.cpp index d809a87c737..8e837596f71 100644 --- a/ReactCommon/cxxreact/Instance.cpp +++ b/ReactCommon/cxxreact/Instance.cpp @@ -7,6 +7,7 @@ #include "Instance.h" +#include "ErrorUtils.h" #include "JSBigString.h" #include "JSBundleType.h" #include "JSExecutor.h" @@ -245,8 +246,27 @@ std::shared_ptr Instance::getJSCallInvoker() { return std::static_pointer_cast(jsCallInvoker_); } +// TODO: Unify with JS CallInvoker RuntimeExecutor Instance::getRuntimeExecutor() { - return nativeToJsBridge_->getRuntimeExecutor(); + std::weak_ptr weakNativeToJsBridge = nativeToJsBridge_; + + auto runtimeExecutor = + [weakNativeToJsBridge]( + std::function &&callback) { + if (auto strongNativeToJsBridge = weakNativeToJsBridge.lock()) { + strongNativeToJsBridge->runOnExecutorQueue( + [callback = std::move(callback)](JSExecutor *executor) { + jsi::Runtime *runtime = + (jsi::Runtime *)executor->getJavaScriptContext(); + try { + callback(*runtime); + } catch (jsi::JSError &originalError) { + handleJSError(*runtime, originalError, true); + } + }); + } + }; + return runtimeExecutor; } std::shared_ptr Instance::getDecoratedNativeCallInvoker( diff --git a/ReactCommon/cxxreact/NativeToJsBridge.cpp b/ReactCommon/cxxreact/NativeToJsBridge.cpp index d0c13554f28..3a695201057 100644 --- a/ReactCommon/cxxreact/NativeToJsBridge.cpp +++ b/ReactCommon/cxxreact/NativeToJsBridge.cpp @@ -340,26 +340,5 @@ std::shared_ptr NativeToJsBridge::getDecoratedNativeCallInvoker( return std::make_shared(m_delegate, nativeInvoker); } -RuntimeExecutor NativeToJsBridge::getRuntimeExecutor() { - auto runtimeExecutor = - [this, isDestroyed = m_destroyed]( - std::function &&callback) { - if (*isDestroyed) { - return; - } - runOnExecutorQueue( - [callback = std::move(callback)](JSExecutor *executor) { - jsi::Runtime *runtime = - (jsi::Runtime *)executor->getJavaScriptContext(); - try { - callback(*runtime); - } catch (jsi::JSError &originalError) { - handleJSError(*runtime, originalError, true); - } - }); - }; - return runtimeExecutor; -} - } // namespace react } // namespace facebook diff --git a/ReactCommon/cxxreact/NativeToJsBridge.h b/ReactCommon/cxxreact/NativeToJsBridge.h index d298f9c2796..f9548c59509 100644 --- a/ReactCommon/cxxreact/NativeToJsBridge.h +++ b/ReactCommon/cxxreact/NativeToJsBridge.h @@ -107,12 +107,6 @@ class NativeToJsBridge { std::shared_ptr getDecoratedNativeCallInvoker( std::shared_ptr nativeInvoker); - /** - * RuntimeExecutor is used on Android to access the jsi::Runtime from Fabric - * and TurboModules - */ - RuntimeExecutor getRuntimeExecutor(); - private: // This is used to avoid a race condition where a proxyCallback gets queued // after ~NativeToJsBridge(), on the same thread. In that case, the callback