Implement previouslyExportedState on HermesRuntimeAgentDelegateNew (#43392)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/43392

## Context

We are migrating to the new Hermes `CDPAgent` and `CDPDebugAPI` APIs in the modern CDP server (previously `HermesCDPHandler`).

## This diff

Wires up `previouslyExportedState` with `CDPAgent`, and re-enables the `ResolveBreakpointAfterReload` integration test.

Changelog: [Internal]

Reviewed By: motiz88

Differential Revision: D54369985

fbshipit-source-id: 5dcb4fe59b8b36b2db9f0385e8487097822e5704
This commit is contained in:
Alex Hunt 2024-03-12 13:03:13 -07:00 committed by Facebook GitHub Bot
parent 55ed1c26ab
commit eef87ca59e
5 changed files with 51 additions and 22 deletions

View File

@ -92,7 +92,7 @@ references:
# Cocoapods - RNTester
pods_cache_key: &pods_cache_key v11-pods-{{ .Environment.CIRCLE_JOB }}-{{ checksum "packages/rn-tester/Podfile.lock.bak" }}-{{ checksum "packages/rn-tester/Podfile" }}
cocoapods_cache_key: &cocoapods_cache_key v11-cocoapods-{{ .Environment.CIRCLE_JOB }}-{{ checksum "packages/rn-tester/Podfile.lock" }}-{{ checksum "packages/rn-tester/Podfile" }}-{{ checksum "/tmp/hermes/hermesversion" }}
rntester_podfile_lock_cache_key: &rntester_podfile_lock_cache_key v9-podfilelock-{{ .Environment.CIRCLE_JOB }}-{{ checksum "packages/rn-tester/Podfile" }}-{{ checksum "/tmp/week_year" }}-{{ checksum "/tmp/hermes/hermesversion" }}
rntester_podfile_lock_cache_key: &rntester_podfile_lock_cache_key v10-podfilelock-{{ .Environment.CIRCLE_JOB }}-{{ checksum "packages/rn-tester/Podfile" }}-{{ checksum "/tmp/week_year" }}-{{ checksum "/tmp/hermes/hermesversion" }}
# Cocoapods - Template
template_cocoapods_cache_key: &template_cocoapods_cache_key v6-cocoapods-{{ .Environment.CIRCLE_JOB }}-{{ checksum "/tmp/iOSTemplateProject/ios/Podfile.lock" }}-{{ checksum "/tmp/iOSTemplateProject/ios/Podfile" }}-{{ checksum "/tmp/hermes/hermesversion" }}-{{ checksum "packages/rn-tester/Podfile.lock" }}

View File

@ -36,7 +36,7 @@ jobs:
uses: actions/cache@v3
with:
path: packages/rn-tester/Pods
key: v2-${{ runner.os }}-RNTesterPods-${{ hashFiles('packages/rn-tester/Podfile.lock') }}-${{ hashFiles('packages/rn-tester/Podfile') }}-${{ hashFiles('tmp/hermes/hermesversion') }}
key: v3-${{ runner.os }}-RNTesterPods-${{ hashFiles('packages/rn-tester/Podfile.lock') }}-${{ hashFiles('packages/rn-tester/Podfile') }}-${{ hashFiles('tmp/hermes/hermesversion') }}
- name: Pod Install
run: |
cd packages/rn-tester

View File

@ -24,6 +24,25 @@ using namespace facebook::hermes;
namespace facebook::react::jsinspector_modern {
class HermesRuntimeAgentDelegateNew::Impl final : public RuntimeAgentDelegate {
using HermesState = hermes::cdp::State;
struct HermesStateWrapper : public ExportedState {
explicit HermesStateWrapper(HermesState state) : state_(std::move(state)) {}
static HermesState unwrapDestructively(ExportedState* wrapper) {
if (!wrapper) {
return {};
}
if (auto* typedWrapper = dynamic_cast<HermesStateWrapper*>(wrapper)) {
return std::move(typedWrapper->state_);
}
return {};
}
private:
HermesState state_;
};
public:
Impl(
FrontendChannel frontendChannel,
@ -44,24 +63,15 @@ class HermesRuntimeAgentDelegateNew::Impl final : public RuntimeAgentDelegate {
runtimeExecutor(
[&runtime, fn = std::move(fn)](auto&) { fn(runtime); });
},
std::move(frontendChannel))) {
// TODO(T178858701): Pass previouslyExportedState to CDPAgent
(void)previouslyExportedState;
}
std::move(frontendChannel),
HermesStateWrapper::unwrapDestructively(
previouslyExportedState.get()))) {}
/**
* Handle a CDP request. The response will be sent over the provided
* \c FrontendChannel synchronously or asynchronously.
* \param req The parsed request.
* \returns true if this agent has responded, or will respond asynchronously,
* to the request (with either a success or error message). False if the
* agent expects another agent to respond to the request instead.
*/
bool handleRequest(const cdp::PreparsedRequest& req) override {
// TODO: Change to string::starts_with when we're on C++20.
if (req.method.rfind("Log.", 0) == 0) {
// Since we know Hermes doesn't do anything useful with Log messages, but
// our containing PageAgent will, just bail out early.
// Since we know Hermes doesn't do anything useful with Log messages,
// but our containing HostAgent will, bail out early.
// TODO: We need a way to negotiate this more dynamically with Hermes
// through the API.
return false;
@ -73,6 +83,10 @@ class HermesRuntimeAgentDelegateNew::Impl final : public RuntimeAgentDelegate {
return true;
}
std::unique_ptr<ExportedState> getExportedState() override {
return std::make_unique<HermesStateWrapper>(hermes_->getState());
}
private:
std::unique_ptr<hermes::cdp::CDPAgent> hermes_;
};
@ -100,6 +114,11 @@ bool HermesRuntimeAgentDelegateNew::handleRequest(
return impl_->handleRequest(req);
}
std::unique_ptr<RuntimeAgentDelegate::ExportedState>
HermesRuntimeAgentDelegateNew::getExportedState() {
return impl_->getExportedState();
}
} // namespace facebook::react::jsinspector_modern
#endif // HERMES_ENABLE_DEBUGGER

View File

@ -66,6 +66,9 @@ class HermesRuntimeAgentDelegateNew : public RuntimeAgentDelegate {
*/
bool handleRequest(const cdp::PreparsedRequest& req) override;
std::unique_ptr<RuntimeAgentDelegate::ExportedState> getExportedState()
override;
private:
class Impl;

View File

@ -559,16 +559,23 @@ TYPED_TEST(JsiIntegrationHermesTest, EvaluateExpressionInExecutionContext) {
std::to_string(executionContextId)));
}
// TODO(T178858701): Restore breakpoint reload persistence under
// HermesRuntimeAgentDelegateNew
TYPED_TEST(JsiIntegrationHermesLegacyTest, ResolveBreakpointAfterReload) {
TYPED_TEST(JsiIntegrationHermesTest, ResolveBreakpointAfterReload) {
this->connect();
InSequence s;
this->expectMessageFromPage(JsonParsed(AtJsonPtr("/id", 1)));
this->expectMessageFromPage(JsonEq(R"({
"id": 1,
"result": {}
})"));
this->toPage_->sendMessage(R"({
"id": 1,
"method": "Debugger.enable"
})");
this->expectMessageFromPage(JsonParsed(AtJsonPtr("/id", 2)));
this->toPage_->sendMessage(R"({
"id": 2,
"method": "Debugger.setBreakpointByUrl",
"params": {"lineNumber": 2, "url": "breakpointTest.js"}
})");
@ -576,11 +583,11 @@ TYPED_TEST(JsiIntegrationHermesLegacyTest, ResolveBreakpointAfterReload) {
this->reload();
this->expectMessageFromPage(JsonEq(R"({
"id": 2,
"id": 3,
"result": {}
})"));
this->toPage_->sendMessage(R"({
"id": 2,
"id": 3,
"method": "Debugger.enable"
})");