Skip to content

Commit 6e062c5

Browse files
motiz88facebook-github-bot
authored andcommitted
Add RuntimeTargetDelegate::serializeStackTrace API
Summary: Changelog: [Internal] Adds an engine-agnostic mechanism for serialising a previously captured stack trace as a CDP [`Runtime.StackTrace`](https://cdpstatus.reactnative.dev/devtools-protocol/tot/Runtime#type-StackTrace). This complements the existing `RuntimeTargetDelegate::captureStackTrace` method, which returns an opaque, engine-specific representation of a stack trace. This can be used as a building block for implementing higher-level CDP message types like [`Network.Initiator`](https://cdpstatus.reactnative.dev/devtools-protocol/tot/Network#type-Initiator) within React Native, while keeping the underlying stack trace representation private to each engine. Differential Revision: D83754142
1 parent d205318 commit 6e062c5

File tree

6 files changed

+49
-0
lines changed

6 files changed

+49
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class HermesRuntimeSamplingProfileDelegate {
6262
} // namespace
6363

6464
#ifdef HERMES_ENABLE_DEBUGGER
65+
6566
class HermesRuntimeTargetDelegate::Impl final : public RuntimeTargetDelegate {
6667
using HermesStackTrace = debugger::StackTrace;
6768

@@ -78,6 +79,14 @@ class HermesRuntimeTargetDelegate::Impl final : public RuntimeTargetDelegate {
7879
return &hermesStackTrace_;
7980
}
8081

82+
const HermesStackTrace& operator*() const {
83+
return hermesStackTrace_;
84+
}
85+
86+
const HermesStackTrace* operator->() const {
87+
return &hermesStackTrace_;
88+
}
89+
8190
private:
8291
HermesStackTrace hermesStackTrace_;
8392
};
@@ -216,6 +225,16 @@ class HermesRuntimeTargetDelegate::Impl final : public RuntimeTargetDelegate {
216225
return samplingProfileDelegate_->collectSamplingProfile();
217226
}
218227

228+
std::optional<folly::dynamic> serializeStackTrace(
229+
const StackTrace& stackTrace) override {
230+
if (auto* hermesStackTraceWrapper =
231+
dynamic_cast<const HermesStackTraceWrapper*>(&stackTrace)) {
232+
return folly::parseJson(cdpDebugAPI_->serializeStackTraceToJsonStr(
233+
**hermesStackTraceWrapper));
234+
}
235+
return std::nullopt;
236+
}
237+
219238
private:
220239
HermesRuntimeTargetDelegate& delegate_;
221240
std::shared_ptr<HermesRuntime> runtime_;
@@ -311,6 +330,11 @@ HermesRuntimeTargetDelegate::collectSamplingProfile() {
311330
return impl_->collectSamplingProfile();
312331
}
313332

333+
std::optional<folly::dynamic> HermesRuntimeTargetDelegate::serializeStackTrace(
334+
const StackTrace& stackTrace) {
335+
return impl_->serializeStackTrace(stackTrace);
336+
}
337+
314338
#ifdef HERMES_ENABLE_DEBUGGER
315339
CDPDebugAPI& HermesRuntimeTargetDelegate::getCDPDebugAPI() {
316340
return impl_->getCDPDebugAPI();

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class HermesRuntimeTargetDelegate : public RuntimeTargetDelegate {
6060

6161
tracing::RuntimeSamplingProfile collectSamplingProfile() override;
6262

63+
std::optional<folly::dynamic> serializeStackTrace(
64+
const StackTrace& stackTrace) override;
65+
6366
private:
6467
// We use the private implementation idiom to ensure this class has the same
6568
// layout regardless of whether HERMES_ENABLE_DEBUGGER is defined. The net

packages/react-native/ReactCommon/jsinspector-modern/FallbackRuntimeTargetDelegate.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,10 @@ FallbackRuntimeTargetDelegate::collectSamplingProfile() {
5858
"Sampling Profiler capabilities are not supported for Runtime fallback");
5959
}
6060

61+
std::optional<folly::dynamic>
62+
FallbackRuntimeTargetDelegate::serializeStackTrace(
63+
const StackTrace& stackTrace) {
64+
return std::nullopt;
65+
}
66+
6167
} // namespace facebook::react::jsinspector_modern

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class FallbackRuntimeTargetDelegate : public RuntimeTargetDelegate {
4646

4747
tracing::RuntimeSamplingProfile collectSamplingProfile() override;
4848

49+
std::optional<folly::dynamic> serializeStackTrace(
50+
const StackTrace& stackTrace) override;
51+
4952
private:
5053
std::string engineDescription_;
5154
};

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ class RuntimeTargetDelegate {
108108
* Return recorded sampling profile for the previous sampling session.
109109
*/
110110
virtual tracing::RuntimeSamplingProfile collectSamplingProfile() = 0;
111+
112+
/**
113+
* \returns a JSON representation of the given stack trace, conforming to the
114+
* @cdp Runtime.StackTrace type, if the runtime supports it. Otherwise,
115+
* returns std::nullopt.
116+
*/
117+
virtual std::optional<folly::dynamic> serializeStackTrace(
118+
const StackTrace& stackTrace) = 0;
111119
};
112120

113121
/**

packages/react-native/ReactCommon/jsinspector-modern/tests/InspectorMocks.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ class MockRuntimeTargetDelegate : public RuntimeTargetDelegate {
169169
collectSamplingProfile,
170170
(),
171171
(override));
172+
MOCK_METHOD(
173+
std::optional<folly::dynamic>,
174+
serializeStackTrace,
175+
(const StackTrace& stackTrace),
176+
(override));
172177

173178
inline MockRuntimeTargetDelegate() {
174179
using namespace testing;

0 commit comments

Comments
 (0)