From 0735873fbcdf7914ac63ae651e9118a9530d528b Mon Sep 17 00:00:00 2001 From: Marc Horowitz Date: Wed, 17 Apr 2019 21:17:51 -0700 Subject: [PATCH] Make isInspectable safe to call from any thread Summary: The bridge was not properly isolating isInspectable onto the JS thread. Reviewed By: fkgozali Differential Revision: D14991970 fbshipit-source-id: 92a06c90bade8f92bfa81fa3b7dfb23b17db6117 --- ReactCommon/cxxreact/NativeToJsBridge.cpp | 13 +++++++------ ReactCommon/cxxreact/NativeToJsBridge.h | 5 +++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/ReactCommon/cxxreact/NativeToJsBridge.cpp b/ReactCommon/cxxreact/NativeToJsBridge.cpp index 27c54a3739c..2c3893e87e6 100644 --- a/ReactCommon/cxxreact/NativeToJsBridge.cpp +++ b/ReactCommon/cxxreact/NativeToJsBridge.cpp @@ -84,14 +84,15 @@ private: }; NativeToJsBridge::NativeToJsBridge( - JSExecutorFactory* jsExecutorFactory, + JSExecutorFactory *jsExecutorFactory, std::shared_ptr registry, std::shared_ptr jsQueue, std::shared_ptr callback) - : m_destroyed(std::make_shared(false)) - , m_delegate(std::make_shared(registry, callback)) - , m_executor(jsExecutorFactory->createJSExecutor(m_delegate, jsQueue)) - , m_executorMessageQueueThread(std::move(jsQueue)) {} + : m_destroyed(std::make_shared(false)), + m_delegate(std::make_shared(registry, callback)), + m_executor(jsExecutorFactory->createJSExecutor(m_delegate, jsQueue)), + m_executorMessageQueueThread(std::move(jsQueue)), + m_inspectable(m_executor->isInspectable()) {} // This must be called on the same thread on which the constructor was called. NativeToJsBridge::~NativeToJsBridge() { @@ -225,7 +226,7 @@ void* NativeToJsBridge::getJavaScriptContext() { } bool NativeToJsBridge::isInspectable() { - return m_executor->isInspectable(); + return m_inspectable; } bool NativeToJsBridge::isBatchActive() { diff --git a/ReactCommon/cxxreact/NativeToJsBridge.h b/ReactCommon/cxxreact/NativeToJsBridge.h index c80114230e8..bf5746a0e7f 100644 --- a/ReactCommon/cxxreact/NativeToJsBridge.h +++ b/ReactCommon/cxxreact/NativeToJsBridge.h @@ -96,6 +96,11 @@ private: std::unique_ptr m_executor; std::shared_ptr m_executorMessageQueueThread; + // Memoize this on the JS thread, so that it can be inspected from + // any thread later. This assumes inspectability doesn't change for + // a JSExecutor instance, which is true for all existing implementations. + bool m_inspectable; + // Keep track of whether the JS bundle containing the application logic causes // exception when evaluated initially. If so, more calls to JS will very // likely fail as well, so this flag can help prevent them.