Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ class HermesRuntimeTargetDelegate::Impl final : public RuntimeTargetDelegate {
return &hermesStackTrace_;
}

const HermesStackTrace& operator*() const {
return hermesStackTrace_;
}

const HermesStackTrace* operator->() const {
return &hermesStackTrace_;
}

private:
HermesStackTrace hermesStackTrace_;
};
Expand Down Expand Up @@ -216,6 +224,47 @@ class HermesRuntimeTargetDelegate::Impl final : public RuntimeTargetDelegate {
return samplingProfileDelegate_->collectSamplingProfile();
}

std::optional<folly::dynamic> serializeStackTrace(
const StackTrace& stackTrace) override {
if (auto* hermesStackTraceWrapper =
dynamic_cast<const HermesStackTraceWrapper*>(&stackTrace)) {
// The logic below is duplicated from
// facebook::hermes::cdp::message::makeCallFrames in
// hermes/cdp/MessageConverters.cpp (and rewritten to use Folly).
// TODO: Use a suitable Hermes API (D83560910 / D83560972 / D83562078) to
// serialize the stack trace to CDP-formatted JSON.
folly::dynamic cdpStackTrace = folly::dynamic::object();
auto& hermesStackTrace = **hermesStackTraceWrapper;
if (hermesStackTrace.callFrameCount() > 0) {
folly::dynamic callFrames = folly::dynamic::array();
callFrames.reserve(hermesStackTrace.callFrameCount());
for (int i = 0, n = hermesStackTrace.callFrameCount(); i != n; i++) {
auto callFrame = hermesStackTrace.callFrameForIndex(i);
if (callFrame.location.fileId ==
facebook::hermes::debugger::kInvalidLocation) {
continue;
}
folly::dynamic callFrameObj = folly::dynamic::object();
callFrameObj["functionName"] = callFrame.functionName;
callFrameObj["scriptId"] = std::to_string(callFrame.location.fileId);
callFrameObj["url"] = callFrame.location.fileName;
if (callFrame.location.line !=
facebook::hermes::debugger::kInvalidLocation) {
callFrameObj["lineNumber"] = callFrame.location.line - 1;
}
if (callFrame.location.column !=
facebook::hermes::debugger::kInvalidLocation) {
callFrameObj["columnNumber"] = callFrame.location.column - 1;
}
callFrames.push_back(std::move(callFrameObj));
}
cdpStackTrace["callFrames"] = std::move(callFrames);
}
return cdpStackTrace;
}
return std::nullopt;
}

private:
HermesRuntimeTargetDelegate& delegate_;
std::shared_ptr<HermesRuntime> runtime_;
Expand Down Expand Up @@ -311,6 +360,11 @@ HermesRuntimeTargetDelegate::collectSamplingProfile() {
return impl_->collectSamplingProfile();
}

std::optional<folly::dynamic> HermesRuntimeTargetDelegate::serializeStackTrace(
const StackTrace& stackTrace) {
return impl_->serializeStackTrace(stackTrace);
}

#ifdef HERMES_ENABLE_DEBUGGER
CDPDebugAPI& HermesRuntimeTargetDelegate::getCDPDebugAPI() {
return impl_->getCDPDebugAPI();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class HermesRuntimeTargetDelegate : public RuntimeTargetDelegate {

tracing::RuntimeSamplingProfile collectSamplingProfile() override;

std::optional<folly::dynamic> serializeStackTrace(
const StackTrace& stackTrace) override;

private:
// We use the private implementation idiom to ensure this class has the same
// layout regardless of whether HERMES_ENABLE_DEBUGGER is defined. The net
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@ FallbackRuntimeTargetDelegate::collectSamplingProfile() {
"Sampling Profiler capabilities are not supported for Runtime fallback");
}

std::optional<folly::dynamic>
FallbackRuntimeTargetDelegate::serializeStackTrace(
const StackTrace& /*stackTrace*/) {
return std::nullopt;
}

} // namespace facebook::react::jsinspector_modern
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class FallbackRuntimeTargetDelegate : public RuntimeTargetDelegate {

tracing::RuntimeSamplingProfile collectSamplingProfile() override;

std::optional<folly::dynamic> serializeStackTrace(
const StackTrace& stackTrace) override;

private:
std::string engineDescription_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ class RuntimeTargetDelegate {
* Return recorded sampling profile for the previous sampling session.
*/
virtual tracing::RuntimeSamplingProfile collectSamplingProfile() = 0;

/**
* \returns a JSON representation of the given stack trace, conforming to the
* @cdp Runtime.StackTrace type, if the runtime supports it. Otherwise,
* returns std::nullopt.
*/
virtual std::optional<folly::dynamic> serializeStackTrace(
const StackTrace& stackTrace) = 0;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ class MockRuntimeTargetDelegate : public RuntimeTargetDelegate {
collectSamplingProfile,
(),
(override));
MOCK_METHOD(
std::optional<folly::dynamic>,
serializeStackTrace,
(const StackTrace& stackTrace),
(override));

inline MockRuntimeTargetDelegate() {
using namespace testing;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,88 @@ TYPED_TEST(JsiIntegrationHermesTest, ReleaseRemoteObjectGroup) {
})");
}

// A low-level test for captureStackTrace and serializeStackTrace in
// HermesRuntimeTargetDelegate. This functionality is not directly exposed
// to user code, but serves as a building block for higher-level CDP domains.
TYPED_TEST(JsiIntegrationHermesTest, testCaptureAndSerializeStackTrace) {
auto& runtimeTargetDelegate = this->dangerouslyGetRuntimeTargetDelegate();
auto& runtime = this->dangerouslyGetRuntime();
runtime.global().setProperty(
runtime,
"captureCdpStackTrace",
jsi::Function::createFromHostFunction(
runtime,
jsi::PropNameID::forAscii(runtime, "captureCdpStackTrace"),
0,
[&runtimeTargetDelegate](
jsi::Runtime& rt,
const jsi::Value& /* thisVal */,
const jsi::Value* /* args */,
size_t /* count */) -> jsi::Value {
auto stackTraceDynamic = runtimeTargetDelegate.serializeStackTrace(
*runtimeTargetDelegate.captureStackTrace(rt));
if (!stackTraceDynamic.has_value()) {
return jsi::Value::undefined();
}
return jsi::String::createFromUtf8(
rt, folly::toJson(*stackTraceDynamic));
}));

this->connect();

InSequence s;

this->expectMessageFromPage(JsonEq(R"({
"id": 1,
"result": {}
})"));
this->toPage_->sendMessage(R"({
"id": 1,
"method": "Debugger.enable"
})");

auto scriptInfo = this->expectMessageFromPage(JsonParsed(AllOf(
AtJsonPtr("/method", "Debugger.scriptParsed"),
AtJsonPtr("/params/url", "stackTraceTest.js"))));

auto stackTrace = this->eval(R"( // line 0
function inner() { // line 1
return globalThis.captureCdpStackTrace(); // line 2
} // line 3
function outer() { // line 4
return inner(); // line 5
} // line 6
outer(); // line 7
//# sourceURL=stackTraceTest.js
)")
.getString(runtime)
.utf8(runtime);

ASSERT_TRUE(scriptInfo->has_value());

EXPECT_THAT(
stackTrace,
JsonParsed(AllOf(
AtJsonPtr("/callFrames/0/functionName", "inner"),
AtJsonPtr(
"/callFrames/0/scriptId",
scriptInfo->value()["params"]["scriptId"]),
AtJsonPtr("/callFrames/0/lineNumber", 2),
AtJsonPtr("/callFrames/0/columnNumber", 44),
AtJsonPtr("/callFrames/1/functionName", "outer"),
AtJsonPtr(
"/callFrames/1/scriptId",
scriptInfo->value()["params"]["scriptId"]),
AtJsonPtr("/callFrames/1/lineNumber", 5),
AtJsonPtr("/callFrames/1/columnNumber", 18),
AtJsonPtr("/callFrames/2/functionName", "global"),
AtJsonPtr(
"/callFrames/2/scriptId",
scriptInfo->value()["params"]["scriptId"]),
AtJsonPtr("/callFrames/2/lineNumber", 7),
AtJsonPtr("/callFrames/2/columnNumber", 9))));
}

#pragma endregion // AllHermesVariants

} // namespace facebook::react::jsinspector_modern
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ class JsiIntegrationPortableTestBase : public ::testing::Test,
return result;
}

RuntimeTargetDelegate& dangerouslyGetRuntimeTargetDelegate() {
return engineAdapter_->getRuntimeTargetDelegate();
}

jsi::Runtime& dangerouslyGetRuntime() {
return engineAdapter_->getRuntime();
}

std::shared_ptr<HostTarget> page_;
InstanceTarget* instance_{};
RuntimeTarget* runtimeTarget_{};
Expand Down
Loading