Skip to content

Commit 7c5a014

Browse files
motiz88facebook-github-bot
authored andcommitted
Pass whole RuntimeTargetDelegate from RN instead of aggregating its methods (#43346)
Summary: Pull Request resolved: #43346 Changelog: [Internal] (Continuing the theme of reducing integration boilerplate from D54537844.) This diff changes both `JSExecutor` (Bridge) and `JSRuntime` (Bridgeless) to no longer implement `RuntimeTargetDelegate`. Instead, each of them exposes a `getRuntimeTargetDelegate()` method that returns a stable reference to a target delegate that it *owns*. To facilitate this, we create a new `FallbackRuntimeTargetDelegate` for use in non-Hermes cases. This replaces *almost* all direct uses of `FallbackRuntimeAgentDelegate` outside of `jsinspector`. I'll follow up in a separate diff to deal with the last case and make the fallback agent delegate fully private. As a result, changing the `RuntimeTargetDelegate` interface (which we'll need to do for console support) becomes much easier: we only have unit test mocks + two concrete `RuntimeTargetDelegate` implementations (one fallback, one Hermes) to update for each API change. Reviewed By: huntie Differential Revision: D54585658 fbshipit-source-id: 08b61c74008ddc36c2b134a40755ef8e43ab21ed
1 parent 1ea269f commit 7c5a014

18 files changed

Lines changed: 148 additions & 136 deletions

packages/react-native/ReactCommon/cxxreact/JSExecutor.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,12 @@ double JSExecutor::performanceNow() {
3535
return duration / NANOSECONDS_IN_MILLISECOND;
3636
}
3737

38-
std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate>
39-
JSExecutor::createAgentDelegate(
40-
jsinspector_modern::FrontendChannel frontendChannel,
41-
jsinspector_modern::SessionState& sessionState,
42-
std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate::ExportedState>,
43-
const jsinspector_modern::ExecutionContextDescription&
44-
executionContextDescription,
45-
RuntimeExecutor runtimeExecutor) {
46-
(void)executionContextDescription;
47-
(void)runtimeExecutor;
48-
return std::make_unique<jsinspector_modern::FallbackRuntimeAgentDelegate>(
49-
std::move(frontendChannel), sessionState, getDescription());
38+
jsinspector_modern::RuntimeTargetDelegate&
39+
JSExecutor::getRuntimeTargetDelegate() {
40+
if (!runtimeTargetDelegate_) {
41+
runtimeTargetDelegate_.emplace(getDescription());
42+
}
43+
return *runtimeTargetDelegate_;
5044
}
5145

5246
} // namespace facebook::react

packages/react-native/ReactCommon/cxxreact/JSExecutor.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class JSExecutorFactory {
5555
virtual ~JSExecutorFactory() {}
5656
};
5757

58-
class RN_EXPORT JSExecutor : public jsinspector_modern::RuntimeTargetDelegate {
58+
class RN_EXPORT JSExecutor {
5959
public:
6060
/**
6161
* Prepares the JS runtime for React Native by installing global variables.
@@ -130,7 +130,7 @@ class RN_EXPORT JSExecutor : public jsinspector_modern::RuntimeTargetDelegate {
130130
virtual void handleMemoryPressure([[maybe_unused]] int pressureLevel) {}
131131

132132
virtual void destroy() {}
133-
virtual ~JSExecutor() override {}
133+
virtual ~JSExecutor() = default;
134134

135135
virtual void flush() {}
136136

@@ -141,17 +141,19 @@ class RN_EXPORT JSExecutor : public jsinspector_modern::RuntimeTargetDelegate {
141141
static double performanceNow();
142142

143143
/**
144-
* Create a RuntimeAgentDelegate that can be used to debug the JS VM instance.
144+
* Get a reference to the \c RuntimeTargetDelegate owned (or implemented) by
145+
* this executor. This reference must remain valid for the duration of the
146+
* executor's lifetime.
145147
*/
146-
virtual std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate>
147-
createAgentDelegate(
148-
jsinspector_modern::FrontendChannel frontendChannel,
149-
jsinspector_modern::SessionState& sessionState,
150-
std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate::ExportedState>
151-
previouslyExportedState,
152-
const jsinspector_modern::ExecutionContextDescription&
153-
executionContextDescription,
154-
RuntimeExecutor runtimeExecutor) override;
148+
virtual jsinspector_modern::RuntimeTargetDelegate& getRuntimeTargetDelegate();
149+
150+
private:
151+
/**
152+
* Initialized by \c getRuntimeTargetDelegate if not overridden, and then
153+
* never changes.
154+
*/
155+
std::optional<jsinspector_modern::FallbackRuntimeTargetDelegate>
156+
runtimeTargetDelegate_;
155157
};
156158

157159
} // namespace facebook::react

packages/react-native/ReactCommon/cxxreact/NativeToJsBridge.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ NativeToJsBridge::getDecoratedNativeMethodCallInvoker(
345345

346346
jsinspector_modern::RuntimeTargetDelegate&
347347
NativeToJsBridge::getInspectorTargetDelegate() {
348-
return *m_executor;
348+
return m_executor->getRuntimeTargetDelegate();
349349
}
350350

351351
} // namespace facebook::react

packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -256,21 +256,9 @@ HermesExecutor::HermesExecutor(
256256
targetDelegate_{
257257
std::shared_ptr<HermesRuntime>(runtime_, &hermesRuntime)} {}
258258

259-
std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate>
260-
HermesExecutor::createAgentDelegate(
261-
jsinspector_modern::FrontendChannel frontendChannel,
262-
jsinspector_modern::SessionState& sessionState,
263-
std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate::ExportedState>
264-
previouslyExportedState,
265-
const jsinspector_modern::ExecutionContextDescription&
266-
executionContextDescription,
267-
RuntimeExecutor runtimeExecutor) {
268-
return targetDelegate_.createAgentDelegate(
269-
std::move(frontendChannel),
270-
sessionState,
271-
std::move(previouslyExportedState),
272-
executionContextDescription,
273-
std::move(runtimeExecutor));
259+
jsinspector_modern::RuntimeTargetDelegate&
260+
HermesExecutor::getRuntimeTargetDelegate() {
261+
return targetDelegate_;
274262
}
275263

276264
} // namespace facebook::react

packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,8 @@ class HermesExecutor : public JSIExecutor {
5555
RuntimeInstaller runtimeInstaller,
5656
hermes::HermesRuntime& hermesRuntime);
5757

58-
virtual std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate>
59-
createAgentDelegate(
60-
jsinspector_modern::FrontendChannel frontendChannel,
61-
jsinspector_modern::SessionState& sessionState,
62-
std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate::ExportedState>
63-
previouslyExportedState,
64-
const jsinspector_modern::ExecutionContextDescription&
65-
executionContextDescription,
66-
RuntimeExecutor runtimeExecutor) override;
58+
jsinspector_modern::RuntimeTargetDelegate& getRuntimeTargetDelegate()
59+
override;
6760

6861
private:
6962
JSIScopedTimeoutInvoker timeoutInvoker_;

packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeAgentDelegate.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#include <hermes/inspector/RuntimeAdapter.h>
1515
#include <hermes/inspector/chrome/CDPHandler.h>
1616
#else // HERMES_ENABLE_DEBUGGER
17+
// TODO(moti): FallbackRuntimeAgentDelegate should be private. We should fall
18+
// back at the *TargetDelegate* level, in HermesRuntimeTargetDelegate, rather
19+
// than within HermesRuntimeAgentDelegate.
1720
#include <jsinspector-modern/FallbackRuntimeAgentDelegate.h>
1821
#endif // HERMES_ENABLE_DEBUGGER
1922

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include "FallbackRuntimeTargetDelegate.h"
9+
#include "FallbackRuntimeAgentDelegate.h"
10+
11+
namespace facebook::react::jsinspector_modern {
12+
13+
FallbackRuntimeTargetDelegate::FallbackRuntimeTargetDelegate(
14+
std::string engineDescription)
15+
: engineDescription_{std::move(engineDescription)} {}
16+
17+
std::unique_ptr<RuntimeAgentDelegate>
18+
FallbackRuntimeTargetDelegate::createAgentDelegate(
19+
FrontendChannel channel,
20+
SessionState& sessionState,
21+
std::unique_ptr<RuntimeAgentDelegate::ExportedState>
22+
/*previouslyExportedState*/,
23+
const ExecutionContextDescription& /*executionContextDescription*/,
24+
RuntimeExecutor /*runtimeExecutor*/) {
25+
return std::make_unique<jsinspector_modern::FallbackRuntimeAgentDelegate>(
26+
std::move(channel), sessionState, engineDescription_);
27+
}
28+
29+
} // namespace facebook::react::jsinspector_modern
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include "InspectorInterfaces.h"
11+
#include "RuntimeTarget.h"
12+
#include "SessionState.h"
13+
14+
#include <string>
15+
16+
namespace facebook::react::jsinspector_modern {
17+
18+
/**
19+
* A RuntimeTargetDelegate that stubs out debugging functionality for a
20+
* JavaScript runtime that does not natively support debugging.
21+
*/
22+
class FallbackRuntimeTargetDelegate : public RuntimeTargetDelegate {
23+
public:
24+
explicit FallbackRuntimeTargetDelegate(std::string engineDescription);
25+
26+
std::unique_ptr<RuntimeAgentDelegate> createAgentDelegate(
27+
FrontendChannel channel,
28+
SessionState& sessionState,
29+
std::unique_ptr<RuntimeAgentDelegate::ExportedState>
30+
previouslyExportedState,
31+
const ExecutionContextDescription& executionContextDescription,
32+
RuntimeExecutor runtimeExecutor) override;
33+
34+
private:
35+
std::string engineDescription_;
36+
};
37+
38+
} // namespace facebook::react::jsinspector_modern

packages/react-native/ReactCommon/jsinspector-modern/ReactCdp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#pragma once
99

1010
#include <jsinspector-modern/ExecutionContext.h>
11-
#include <jsinspector-modern/FallbackRuntimeAgentDelegate.h>
11+
#include <jsinspector-modern/FallbackRuntimeTargetDelegate.h>
1212
#include <jsinspector-modern/HostTarget.h>
1313
#include <jsinspector-modern/InstanceTarget.h>
1414
#include <jsinspector-modern/RuntimeTarget.h>

packages/react-native/ReactCommon/jsinspector-modern/tests/JsiIntegrationTest.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class JsiIntegrationPortableTest : public Test, private HostTargetDelegate {
5555
JsiIntegrationPortableTest() : engineAdapter_{immediateExecutor_} {
5656
instance_ = &page_->registerInstance(instanceTargetDelegate_);
5757
runtimeTarget_ = &instance_->registerRuntime(
58-
*engineAdapter_, engineAdapter_->getRuntimeExecutor());
58+
engineAdapter_->getRuntimeTargetDelegate(),
59+
engineAdapter_->getRuntimeExecutor());
5960
}
6061

6162
~JsiIntegrationPortableTest() override {
@@ -96,7 +97,8 @@ class JsiIntegrationPortableTest : public Test, private HostTargetDelegate {
9697
engineAdapter_.emplace(immediateExecutor_);
9798
instance_ = &page_->registerInstance(instanceTargetDelegate_);
9899
runtimeTarget_ = &instance_->registerRuntime(
99-
*engineAdapter_, engineAdapter_->getRuntimeExecutor());
100+
engineAdapter_->getRuntimeTargetDelegate(),
101+
engineAdapter_->getRuntimeExecutor());
100102
}
101103

102104
MockRemoteConnection& fromPage() {

0 commit comments

Comments
 (0)