Skip to content

Commit 4f7fa58

Browse files
lavenzgmeta-codesync[bot]
authored andcommitted
Allow HermesJSRuntime/HermesRuntimeTargetDelegate to take both jsi::Runtime and HermesRuntime arguments in ctor (#55732)
Summary: Pull Request resolved: #55732 Ideally we only need to pass jsi::Runtime, from which we can cast to IHermes for Hermes specific interface methods. But in some scenarios like instrumenting the runtime or tracing JSI calls, the passed runtime would be a decorator of the actual HermesRuntime, we can't do casting on the decoration type since it doesn't implement IHermes. So this diff changes it to pass both a shared_ptr of jsi::Runtime and HermesRuntime reference. It has no behavioral change. Changelog: [Internal] Reviewed By: OmarBafagih Differential Revision: D94258352 fbshipit-source-id: c09f305a8dfe76b723ed3cb2f0d52d3b61ae4893
1 parent 62fd7a1 commit 4f7fa58

File tree

5 files changed

+39
-25
lines changed

5 files changed

+39
-25
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ HermesExecutor::getRuntimeTargetDelegate() {
265265
if (!targetDelegate_) {
266266
targetDelegate_ =
267267
std::make_unique<jsinspector_modern::HermesRuntimeTargetDelegate>(
268-
hermesRuntime_);
268+
hermesRuntime_, *hermesRuntime_);
269269
}
270270
return *targetDelegate_;
271271
}

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ const uint16_t HERMES_SAMPLING_FREQUENCY_HZ = 10000;
3636
class HermesRuntimeSamplingProfileDelegate {
3737
public:
3838
explicit HermesRuntimeSamplingProfileDelegate(
39-
std::shared_ptr<HermesRuntime> hermesRuntime)
40-
: hermesRuntime_(std::move(hermesRuntime)) {}
39+
std::shared_ptr<jsi::Runtime> runtime,
40+
hermes::HermesRuntime& hermesRuntime)
41+
: runtime_(std::move(runtime)), hermesRuntime_(hermesRuntime) {}
4142

4243
void startSampling() {
4344
auto* hermesAPI = jsi::castInterface<IHermesRootAPI>(makeHermesRootAPI());
@@ -52,11 +53,12 @@ class HermesRuntimeSamplingProfileDelegate {
5253
tracing::RuntimeSamplingProfile collectSamplingProfile() {
5354
return tracing::HermesRuntimeSamplingProfileSerializer::
5455
serializeToTracingSamplingProfile(
55-
hermesRuntime_->dumpSampledTraceToProfile());
56+
hermesRuntime_.dumpSampledTraceToProfile());
5657
}
5758

5859
private:
59-
std::shared_ptr<HermesRuntime> hermesRuntime_;
60+
std::shared_ptr<jsi::Runtime> runtime_;
61+
HermesRuntime& hermesRuntime_;
6062
};
6163

6264
} // namespace
@@ -93,13 +95,16 @@ class HermesRuntimeTargetDelegate::Impl final : public RuntimeTargetDelegate {
9395
public:
9496
explicit Impl(
9597
HermesRuntimeTargetDelegate& delegate,
96-
std::shared_ptr<HermesRuntime> hermesRuntime)
98+
std::shared_ptr<jsi::Runtime> runtime,
99+
HermesRuntime& hermesRuntime)
97100
: delegate_(delegate),
98-
runtime_(hermesRuntime),
99-
cdpDebugAPI_(CDPDebugAPI::create(*runtime_)),
101+
runtime_(runtime),
102+
hermesRuntime_(hermesRuntime),
103+
cdpDebugAPI_(CDPDebugAPI::create(hermesRuntime_)),
100104
samplingProfileDelegate_(
101105
std::make_unique<HermesRuntimeSamplingProfileDelegate>(
102-
std::move(hermesRuntime))) {}
106+
std::move(runtime),
107+
hermesRuntime_)) {}
103108

104109
CDPDebugAPI& getCDPDebugAPI() {
105110
return *cdpDebugAPI_;
@@ -119,7 +124,7 @@ class HermesRuntimeTargetDelegate::Impl final : public RuntimeTargetDelegate {
119124
sessionState,
120125
std::move(previouslyExportedState),
121126
executionContextDescription,
122-
*runtime_,
127+
hermesRuntime_,
123128
delegate_,
124129
std::move(runtimeExecutor)));
125130
}
@@ -209,7 +214,7 @@ class HermesRuntimeTargetDelegate::Impl final : public RuntimeTargetDelegate {
209214
// properly representing the stack trace in other use cases, where native
210215
// frames aren't stripped on serialisation.
211216
return std::make_unique<HermesStackTraceWrapper>(
212-
runtime_->getDebugger().captureStackTrace());
217+
hermesRuntime_.getDebugger().captureStackTrace());
213218
}
214219

215220
void enableSamplingProfiler() override {
@@ -267,7 +272,8 @@ class HermesRuntimeTargetDelegate::Impl final : public RuntimeTargetDelegate {
267272

268273
private:
269274
HermesRuntimeTargetDelegate& delegate_;
270-
std::shared_ptr<HermesRuntime> runtime_;
275+
std::shared_ptr<jsi::Runtime> runtime_;
276+
HermesRuntime& hermesRuntime_;
271277
const std::unique_ptr<CDPDebugAPI> cdpDebugAPI_;
272278
std::unique_ptr<HermesRuntimeSamplingProfileDelegate>
273279
samplingProfileDelegate_;
@@ -284,11 +290,13 @@ class HermesRuntimeTargetDelegate::Impl final
284290
public:
285291
explicit Impl(
286292
HermesRuntimeTargetDelegate&,
287-
std::shared_ptr<HermesRuntime> hermesRuntime)
288-
: FallbackRuntimeTargetDelegate{hermesRuntime->description()},
293+
std::shared_ptr<jsi::Runtime> runtime,
294+
HermesRuntime& hermesRuntime)
295+
: FallbackRuntimeTargetDelegate{hermesRuntime.description()},
289296
samplingProfileDelegate_(
290297
std::make_unique<HermesRuntimeSamplingProfileDelegate>(
291-
std::move(hermesRuntime))) {}
298+
std::move(runtime),
299+
hermesRuntime)) {}
292300

293301
void enableSamplingProfiler() override {
294302
samplingProfileDelegate_->startSampling();
@@ -310,8 +318,9 @@ class HermesRuntimeTargetDelegate::Impl final
310318
#endif // HERMES_ENABLE_DEBUGGER
311319

312320
HermesRuntimeTargetDelegate::HermesRuntimeTargetDelegate(
313-
std::shared_ptr<HermesRuntime> hermesRuntime)
314-
: impl_(std::make_unique<Impl>(*this, std::move(hermesRuntime))) {}
321+
std::shared_ptr<jsi::Runtime> runtime,
322+
HermesRuntime& hermesRuntime)
323+
: impl_(std::make_unique<Impl>(*this, std::move(runtime), hermesRuntime)) {}
315324

316325
HermesRuntimeTargetDelegate::~HermesRuntimeTargetDelegate() = default;
317326

packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeTargetDelegate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class HermesRuntimeTargetDelegate : public RuntimeTargetDelegate {
2929
/**
3030
* Creates a HermesRuntimeTargetDelegate for the given runtime.
3131
*/
32-
explicit HermesRuntimeTargetDelegate(std::shared_ptr<hermes::HermesRuntime> hermesRuntime);
32+
explicit HermesRuntimeTargetDelegate(std::shared_ptr<jsi::Runtime> runtime, hermes::HermesRuntime &hermesRuntime);
3333

3434
~HermesRuntimeTargetDelegate() override;
3535

packages/react-native/ReactCommon/jsinspector-modern/tests/engines/JsiIntegrationTestHermesEngineAdapter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ JsiIntegrationTestHermesEngineAdapter::JsiIntegrationTestHermesEngineAdapter(
1717
::hermes::vm::CompilationMode::ForceLazyCompilation)
1818
.build())},
1919
jsExecutor_{jsExecutor},
20-
runtimeTargetDelegate_{runtime_} {
20+
runtimeTargetDelegate_{runtime_, *runtime_} {
2121
// NOTE: In React Native, registerForProfiling is called by
2222
// HermesInstance::unstable_initializeOnJsThread, called from
2323
// ReactInstance::initializeRuntime. Ideally, we should figure out how to

packages/react-native/ReactCommon/react/runtime/hermes/HermesInstance.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ class DecoratedRuntime : public jsi::RuntimeDecorator<jsi::Runtime> {
9898

9999
class HermesJSRuntime : public JSRuntime {
100100
public:
101-
HermesJSRuntime(std::unique_ptr<HermesRuntime> runtime)
102-
: runtime_(std::move(runtime)) {}
101+
HermesJSRuntime(
102+
std::shared_ptr<jsi::Runtime> runtime,
103+
HermesRuntime& hermesRuntime)
104+
: runtime_(std::move(runtime)), hermesRuntime_(hermesRuntime) {}
103105

104106
jsi::Runtime& getRuntime() noexcept override {
105107
return *runtime_;
@@ -108,17 +110,18 @@ class HermesJSRuntime : public JSRuntime {
108110
jsinspector_modern::RuntimeTargetDelegate& getRuntimeTargetDelegate()
109111
override {
110112
if (!targetDelegate_) {
111-
targetDelegate_.emplace(runtime_);
113+
targetDelegate_.emplace(runtime_, hermesRuntime_);
112114
}
113115
return *targetDelegate_;
114116
}
115117

116118
void unstable_initializeOnJsThread() override {
117-
runtime_->registerForProfiling();
119+
hermesRuntime_.registerForProfiling();
118120
}
119121

120122
private:
121-
std::shared_ptr<HermesRuntime> runtime_;
123+
std::shared_ptr<jsi::Runtime> runtime_;
124+
HermesRuntime& hermesRuntime_;
122125
std::optional<jsinspector_modern::HermesRuntimeTargetDelegate>
123126
targetDelegate_;
124127
};
@@ -173,7 +176,9 @@ std::unique_ptr<JSRuntime> HermesInstance::createJSRuntime(
173176
(void)msgQueueThread;
174177
#endif
175178

176-
return std::make_unique<HermesJSRuntime>(std::move(hermesRuntime));
179+
HermesRuntime& hermesRuntimeRef = *hermesRuntime;
180+
return std::make_unique<HermesJSRuntime>(
181+
std::move(hermesRuntime), hermesRuntimeRef);
177182
}
178183

179184
} // namespace facebook::react

0 commit comments

Comments
 (0)