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