diff --git a/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.cpp b/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.cpp index 4b1414253d3..163e044b835 100644 --- a/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.cpp +++ b/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.cpp @@ -15,7 +15,6 @@ #include #include -#include #include #include @@ -253,9 +252,10 @@ HermesExecutor::HermesExecutor( RuntimeInstaller runtimeInstaller, HermesRuntime& hermesRuntime) : JSIExecutor(runtime, delegate, timeoutInvoker, runtimeInstaller), - jsQueue_(jsQueue), runtime_(runtime), - hermesRuntime_(hermesRuntime) {} + targetDelegate_( + std::shared_ptr(runtime_, &hermesRuntime), + std::move(jsQueue)) {} std::unique_ptr HermesExecutor::createAgentDelegate( @@ -265,28 +265,11 @@ HermesExecutor::createAgentDelegate( previouslyExportedState, const jsinspector_modern::ExecutionContextDescription& executionContextDescription) { - std::shared_ptr hermesRuntimeShared(runtime_, &hermesRuntime_); - return std::unique_ptr( - new jsinspector_modern::HermesRuntimeAgentDelegate( - frontendChannel, - sessionState, - std::move(previouslyExportedState), - executionContextDescription, - hermesRuntimeShared, - [jsQueueWeak = std::weak_ptr(jsQueue_), - runtimeWeak = std::weak_ptr(runtime_)](auto fn) { - auto jsQueue = jsQueueWeak.lock(); - if (!jsQueue) { - return; - } - jsQueue->runOnQueue([runtimeWeak, fn]() { - auto runtime = runtimeWeak.lock(); - if (!runtime) { - return; - } - fn(*runtime); - }); - })); + return targetDelegate_.createAgentDelegate( + std::move(frontendChannel), + sessionState, + std::move(previouslyExportedState), + executionContextDescription); } } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.h b/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.h index 689a58f7f05..f28e982fd43 100644 --- a/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.h +++ b/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include #include @@ -65,9 +66,8 @@ class HermesExecutor : public JSIExecutor { private: JSIScopedTimeoutInvoker timeoutInvoker_; - std::shared_ptr jsQueue_; std::shared_ptr runtime_; - hermes::HermesRuntime& hermesRuntime_; + jsinspector_modern::HermesRuntimeTargetDelegate targetDelegate_; }; } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeTargetDelegate.cpp b/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeTargetDelegate.cpp new file mode 100644 index 00000000000..8f1719c3399 --- /dev/null +++ b/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeTargetDelegate.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include "HermesRuntimeTargetDelegate.h" +#include "HermesRuntimeAgentDelegate.h" + +#include + +using namespace facebook::hermes; + +namespace facebook::react::jsinspector_modern { + +class HermesRuntimeTargetDelegate::Impl : public RuntimeTargetDelegate { + public: + Impl( + std::shared_ptr hermesRuntime, + std::shared_ptr jsMessageQueueThread) + : Impl( + hermesRuntime, + [msgQueueThreadWeak = std::weak_ptr(jsMessageQueueThread), + runtimeWeak = std::weak_ptr(hermesRuntime)](auto fn) { + auto msgQueueThread = msgQueueThreadWeak.lock(); + if (!msgQueueThread) { + return; + } + msgQueueThread->runOnQueue([runtimeWeak, fn]() { + auto runtime = runtimeWeak.lock(); + if (!runtime) { + return; + } + fn(*runtime); + }); + }) {} + + Impl( + std::shared_ptr hermesRuntime, + RuntimeExecutor runtimeExecutor) + : runtime_(std::move(hermesRuntime)), + runtimeExecutor_(std::move(runtimeExecutor)) {} + + // RuntimeTargetDelegate methods + + std::unique_ptr createAgentDelegate( + FrontendChannel frontendChannel, + SessionState& sessionState, + std::unique_ptr + previouslyExportedState, + const ExecutionContextDescription& executionContextDescription) override { + return std::unique_ptr(new HermesRuntimeAgentDelegate( + frontendChannel, + sessionState, + std::move(previouslyExportedState), + executionContextDescription, + runtime_, + runtimeExecutor_)); + } + + private: + std::shared_ptr runtime_; + RuntimeExecutor runtimeExecutor_; +}; + +HermesRuntimeTargetDelegate::HermesRuntimeTargetDelegate( + std::shared_ptr hermesRuntime, + std::shared_ptr jsMessageQueueThread) + : impl_(std::make_unique( + std::move(hermesRuntime), + std::move(jsMessageQueueThread))) {} + +HermesRuntimeTargetDelegate::HermesRuntimeTargetDelegate( + std::shared_ptr hermesRuntime, + RuntimeExecutor runtimeExecutor) + : impl_(std::make_unique( + std::move(hermesRuntime), + std::move(runtimeExecutor))) {} + +HermesRuntimeTargetDelegate::~HermesRuntimeTargetDelegate() = default; + +std::unique_ptr +HermesRuntimeTargetDelegate::createAgentDelegate( + FrontendChannel frontendChannel, + SessionState& sessionState, + std::unique_ptr + previouslyExportedState, + const ExecutionContextDescription& executionContextDescription) { + return impl_->createAgentDelegate( + frontendChannel, + sessionState, + std::move(previouslyExportedState), + executionContextDescription); +} + +} // namespace facebook::react::jsinspector_modern diff --git a/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeTargetDelegate.h b/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeTargetDelegate.h new file mode 100644 index 00000000000..3703b16917d --- /dev/null +++ b/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeTargetDelegate.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include + +#include +#include +#include + +#include + +namespace facebook::react::jsinspector_modern { + +/** + * A RuntimeTargetDelegate that enables debugging a Hermes runtime over CDP. + */ +class HermesRuntimeTargetDelegate : public RuntimeTargetDelegate { + public: + /** + * Creates a HermesRuntimeTargetDelegate for the given runtime and message + * queue thread. + */ + HermesRuntimeTargetDelegate( + std::shared_ptr hermesRuntime, + std::shared_ptr jsMessageQueueThread); + + /** + * Creates a HermesRuntimeTargetDelegate for the given runtime and executor. + */ + HermesRuntimeTargetDelegate( + std::shared_ptr hermesRuntime, + RuntimeExecutor runtimeExecutor); + + ~HermesRuntimeTargetDelegate() override; + + // RuntimeTargetDelegate methods + + std::unique_ptr createAgentDelegate( + jsinspector_modern::FrontendChannel frontendChannel, + jsinspector_modern::SessionState& sessionState, + std::unique_ptr + previouslyExportedState, + const jsinspector_modern::ExecutionContextDescription& + executionContextDescription) override; + + private: + class Impl; + + std::unique_ptr impl_; +}; + +} // namespace facebook::react::jsinspector_modern diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tests/engines/JsiIntegrationTestHermesEngineAdapter.cpp b/packages/react-native/ReactCommon/jsinspector-modern/tests/engines/JsiIntegrationTestHermesEngineAdapter.cpp index 57efca34257..d5ad319f5bc 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tests/engines/JsiIntegrationTestHermesEngineAdapter.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/tests/engines/JsiIntegrationTestHermesEngineAdapter.cpp @@ -7,8 +7,6 @@ #include -#include - #include "JsiIntegrationTestHermesEngineAdapter.h" using facebook::hermes::makeHermesRuntime; @@ -17,7 +15,9 @@ namespace facebook::react::jsinspector_modern { JsiIntegrationTestHermesEngineAdapter::JsiIntegrationTestHermesEngineAdapter( folly::Executor& jsExecutor) - : runtime_{hermes::makeHermesRuntime()}, jsExecutor_{jsExecutor} {} + : runtime_{hermes::makeHermesRuntime()}, + jsExecutor_{jsExecutor}, + targetDelegate_(runtime_, getRuntimeExecutor()) {} std::unique_ptr JsiIntegrationTestHermesEngineAdapter::createAgentDelegate( @@ -26,14 +26,11 @@ JsiIntegrationTestHermesEngineAdapter::createAgentDelegate( std::unique_ptr previouslyExportedState, const ExecutionContextDescription& executionContextDescription) { - return std::unique_ptr( - new HermesRuntimeAgentDelegate( - frontendChannel, - sessionState, - std::move(previouslyExportedState), - executionContextDescription, - runtime_, - getRuntimeExecutor())); + return targetDelegate_.createAgentDelegate( + std::move(frontendChannel), + sessionState, + std::move(previouslyExportedState), + executionContextDescription); } jsi::Runtime& JsiIntegrationTestHermesEngineAdapter::getRuntime() diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tests/engines/JsiIntegrationTestHermesEngineAdapter.h b/packages/react-native/ReactCommon/jsinspector-modern/tests/engines/JsiIntegrationTestHermesEngineAdapter.h index 08c35800e25..ad2573b0c9a 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tests/engines/JsiIntegrationTestHermesEngineAdapter.h +++ b/packages/react-native/ReactCommon/jsinspector-modern/tests/engines/JsiIntegrationTestHermesEngineAdapter.h @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -39,6 +40,7 @@ class JsiIntegrationTestHermesEngineAdapter : public RuntimeTargetDelegate { private: std::shared_ptr runtime_; folly::Executor& jsExecutor_; + HermesRuntimeTargetDelegate targetDelegate_; }; } // namespace facebook::react::jsinspector_modern diff --git a/packages/react-native/ReactCommon/react/runtime/hermes/HermesInstance.cpp b/packages/react-native/ReactCommon/react/runtime/hermes/HermesInstance.cpp index e21f5f7fb58..ec808954eb5 100644 --- a/packages/react-native/ReactCommon/react/runtime/hermes/HermesInstance.cpp +++ b/packages/react-native/ReactCommon/react/runtime/hermes/HermesInstance.cpp @@ -7,7 +7,7 @@ #include "HermesInstance.h" -#include +#include #include #include #include @@ -98,7 +98,7 @@ class HermesJSRuntime : public JSRuntime { std::unique_ptr runtime, std::shared_ptr msgQueueThread) : runtime_(std::move(runtime)), - msgQueueThread_(std::move(msgQueueThread)) {} + targetDelegate_(runtime_, std::move(msgQueueThread)) {} jsi::Runtime& getRuntime() noexcept override { return *runtime_; @@ -111,32 +111,16 @@ class HermesJSRuntime : public JSRuntime { previouslyExportedState, const jsinspector_modern::ExecutionContextDescription& executionContextDescription) override { - return std::unique_ptr( - new jsinspector_modern::HermesRuntimeAgentDelegate( - frontendChannel, - sessionState, - std::move(previouslyExportedState), - executionContextDescription, - runtime_, - [msgQueueThreadWeak = std::weak_ptr(msgQueueThread_), - runtimeWeak = std::weak_ptr(runtime_)](auto fn) { - auto msgQueueThread = msgQueueThreadWeak.lock(); - if (!msgQueueThread) { - return; - } - msgQueueThread->runOnQueue([runtimeWeak, fn]() { - auto runtime = runtimeWeak.lock(); - if (!runtime) { - return; - } - fn(*runtime); - }); - })); + return targetDelegate_.createAgentDelegate( + std::move(frontendChannel), + sessionState, + std::move(previouslyExportedState), + executionContextDescription); } private: std::shared_ptr runtime_; - std::shared_ptr msgQueueThread_; + jsinspector_modern::HermesRuntimeTargetDelegate targetDelegate_; }; std::unique_ptr HermesInstance::createJSRuntime(