Skip to content

Commit bf26cf9

Browse files
tsaichienfacebook-github-bot
authored andcommitted
Add Hermes Root API in all other versions of Hermes (facebook#51082)
Summary: Pull Request resolved: facebook#51082 Introduces the `IHermesRootAPI` interface and a class implementation. This root API will contain the previously static methods on `HermesRuntime`. The root API will serve as an entry point for users to create the Hermes runtime and invoke methods that do not necessarily require a runtime. Diff also moves all usages of the static methods on `HermesRuntime` to getting the methods from the root API. Multiple places are depending on Hermes, Hermes snapshot, and Shermes, so this diff will also update all of these verions of Hermes at once. Changelog: [Internal] Reviewed By: neildhar Differential Revision: D71132855 fbshipit-source-id: fddda83517682779b4aef062efe84e29fd027fae
1 parent 0455f8f commit bf26cf9

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

packages/react-native/ReactAndroid/src/main/jni/react/hermes/instrumentation/HermesSamplingProfiler.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,23 @@ namespace jsi {
1414
namespace jni {
1515

1616
void HermesSamplingProfiler::enable(jni::alias_ref<jclass>) {
17-
hermes::HermesRuntime::enableSamplingProfiler();
17+
auto* hermesAPI =
18+
castInterface<hermes::IHermesRootAPI>(hermes::makeHermesRootAPI());
19+
hermesAPI->enableSamplingProfiler();
1820
}
1921

2022
void HermesSamplingProfiler::disable(jni::alias_ref<jclass>) {
21-
hermes::HermesRuntime::disableSamplingProfiler();
23+
auto* hermesAPI =
24+
castInterface<hermes::IHermesRootAPI>(hermes::makeHermesRootAPI());
25+
hermesAPI->disableSamplingProfiler();
2226
}
2327

2428
void HermesSamplingProfiler::dumpSampledTraceToFile(
2529
jni::alias_ref<jclass>,
2630
std::string filename) {
27-
hermes::HermesRuntime::dumpSampledTraceToFile(filename);
31+
auto* hermesAPI =
32+
castInterface<hermes::IHermesRootAPI>(hermes::makeHermesRootAPI());
33+
hermesAPI->dumpSampledTraceToFile(filename);
2834
}
2935

3036
void HermesSamplingProfiler::registerNatives() {

packages/react-native/ReactAndroid/src/main/jni/react/hermes/reactexecutor/OnLoad.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ class HermesExecutorHolder
5757
JReactMarker::setLogPerfMarkerIfNeeded();
5858

5959
std::call_once(flag, []() {
60-
facebook::hermes::HermesRuntime::setFatalHandler(hermesFatalHandler);
60+
auto* fatalHandlerInterface =
61+
castInterface<facebook::hermes::ISetFatalHandler>(
62+
facebook::hermes::makeHermesRootAPI());
63+
if (fatalHandlerInterface) {
64+
fatalHandlerInterface->setFatalHandler(hermesFatalHandler);
65+
}
6166
});
6267
auto factory = std::make_unique<HermesExecutorFactory>(installBindings);
6368
factory->setEnableDebugger(enableDebugger);
@@ -75,7 +80,10 @@ class HermesExecutorHolder
7580
JReactMarker::setLogPerfMarkerIfNeeded();
7681
auto runtimeConfig = makeRuntimeConfig(heapSizeMB);
7782
std::call_once(flag, []() {
78-
facebook::hermes::HermesRuntime::setFatalHandler(hermesFatalHandler);
83+
auto fatalHandlerInterface =
84+
castInterface<facebook::hermes::ISetFatalHandler>(
85+
facebook::hermes::makeHermesRootAPI());
86+
fatalHandlerInterface->setFatalHandler(hermesFatalHandler);
7987
});
8088
auto factory = std::make_unique<HermesExecutorFactory>(
8189
installBindings, JSIExecutor::defaultTimeoutInvoker, runtimeConfig);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,13 @@ class HermesRuntimeTargetDelegate::Impl final : public RuntimeTargetDelegate {
175175
}
176176

177177
void enableSamplingProfiler() override {
178-
runtime_->enableSamplingProfiler(HERMES_SAMPLING_FREQUENCY_HZ);
178+
auto* hermesAPI = castInterface<IHermesRootAPI>(makeHermesRootAPI());
179+
hermesAPI->enableSamplingProfiler(HERMES_SAMPLING_FREQUENCY_HZ);
179180
}
180181

181182
void disableSamplingProfiler() override {
182-
runtime_->disableSamplingProfiler();
183+
auto* hermesAPI = castInterface<IHermesRootAPI>(makeHermesRootAPI());
184+
hermesAPI->disableSamplingProfiler();
183185
}
184186

185187
tracing::RuntimeSamplingProfile collectSamplingProfile() override {

packages/react-native/ReactCommon/reactperflogger/reactperflogger/FuseboxPerfettoDataSource.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,18 @@ void logHermesProfileToFusebox(const std::string& traceStr) {
115115

116116
void FuseboxPerfettoDataSource::OnStart(const StartArgs&) {
117117
FuseboxTracer::getFuseboxTracer().startTracing();
118-
facebook::hermes::HermesRuntime::enableSamplingProfiler(SAMPLING_HZ);
118+
auto* hermesAPI =
119+
castInterface<hermes::IHermesRootAPI>(hermes::makeHermesRootAPI());
120+
hermesAPI->enableSamplingProfiler(SAMPLING_HZ);
119121
}
120122

121123
void FuseboxPerfettoDataSource::OnFlush(const FlushArgs&) {}
122124

123125
void FuseboxPerfettoDataSource::OnStop(const StopArgs& a) {
124126
std::stringstream stream;
125-
facebook::hermes::HermesRuntime::dumpSampledTraceToStream(stream);
127+
auto* hermesAPI =
128+
castInterface<hermes::IHermesRootAPI>(hermes::makeHermesRootAPI());
129+
hermesAPI->dumpSampledTraceToStream(stream);
126130
std::string trace = stream.str();
127131
logHermesProfileToFusebox(trace);
128132

packages/react-native/ReactCommon/reactperflogger/reactperflogger/HermesPerfettoDataSource.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ void logHermesProfileToPerfetto(const std::string& traceStr) {
106106
} // namespace
107107

108108
void HermesPerfettoDataSource::OnStart(const StartArgs&) {
109-
facebook::hermes::HermesRuntime::enableSamplingProfiler(SAMPLING_HZ);
109+
auto* hermesAPI =
110+
castInterface<hermes::IHermesRootAPI>(hermes::makeHermesRootAPI());
111+
hermesAPI->enableSamplingProfiler(SAMPLING_HZ);
110112
TRACE_EVENT_INSTANT(
111113
"react-native",
112114
perfetto::DynamicString{"Profiling Started"},
@@ -117,14 +119,18 @@ void HermesPerfettoDataSource::OnStart(const StartArgs&) {
117119
void HermesPerfettoDataSource::OnFlush(const FlushArgs&) {
118120
// NOTE: We write data during OnFlush and not OnStop because we can't
119121
// use the TRACE_EVENT macros in OnStop.
122+
auto* hermesAPI =
123+
castInterface<hermes::IHermesRootAPI>(hermes::makeHermesRootAPI());
120124
std::stringstream stream;
121-
facebook::hermes::HermesRuntime::dumpSampledTraceToStream(stream);
125+
hermesAPI->dumpSampledTraceToStream(stream);
122126
std::string trace = stream.str();
123127
logHermesProfileToPerfetto(trace);
124128
}
125129

126130
void HermesPerfettoDataSource::OnStop(const StopArgs& a) {
127-
facebook::hermes::HermesRuntime::disableSamplingProfiler();
131+
auto* hermesAPI =
132+
castInterface<hermes::IHermesRootAPI>(hermes::makeHermesRootAPI());
133+
hermesAPI->disableSamplingProfiler();
128134
}
129135

130136
} // namespace facebook::react

0 commit comments

Comments
 (0)