Skip to content

Commit ed22ba6

Browse files
motiz88facebook-github-bot
authored andcommitted
Add RuntimeTargetDelegate::serializeStackTrace API (facebook#54048)
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 71b58ae commit ed22ba6

File tree

6 files changed

+48
-0
lines changed

6 files changed

+48
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ class HermesRuntimeTargetDelegate::Impl final : public RuntimeTargetDelegate {
7878
return &hermesStackTrace_;
7979
}
8080

81+
const HermesStackTrace& operator*() const {
82+
return hermesStackTrace_;
83+
}
84+
85+
const HermesStackTrace* operator->() const {
86+
return &hermesStackTrace_;
87+
}
88+
8189
private:
8290
HermesStackTrace hermesStackTrace_;
8391
};
@@ -216,6 +224,16 @@ class HermesRuntimeTargetDelegate::Impl final : public RuntimeTargetDelegate {
216224
return samplingProfileDelegate_->collectSamplingProfile();
217225
}
218226

227+
std::optional<folly::dynamic> serializeStackTrace(
228+
const StackTrace& stackTrace) override {
229+
if (auto* hermesStackTraceWrapper =
230+
dynamic_cast<const HermesStackTraceWrapper*>(&stackTrace)) {
231+
return folly::parseJson(cdpDebugAPI_->serializeStackTraceToJsonStr(
232+
**hermesStackTraceWrapper));
233+
}
234+
return std::nullopt;
235+
}
236+
219237
private:
220238
HermesRuntimeTargetDelegate& delegate_;
221239
std::shared_ptr<HermesRuntime> runtime_;
@@ -311,6 +329,11 @@ HermesRuntimeTargetDelegate::collectSamplingProfile() {
311329
return impl_->collectSamplingProfile();
312330
}
313331

332+
std::optional<folly::dynamic> HermesRuntimeTargetDelegate::serializeStackTrace(
333+
const StackTrace& stackTrace) {
334+
return impl_->serializeStackTrace(stackTrace);
335+
}
336+
314337
#ifdef HERMES_ENABLE_DEBUGGER
315338
CDPDebugAPI& HermesRuntimeTargetDelegate::getCDPDebugAPI() {
316339
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)