Skip to content

Commit 32d12e8

Browse files
Kudofacebook-github-bot
authored andcommitted
add debugging settings (#34489)
Summary: For brownfield apps, it is possible to have multiple hermes runtimes serving different JS bundles. Hermes inspector currently only supports single JS bundle. The latest loaded JS bundle will overwrite previous JS bundle. This is because we always use the ["Hermes React Native" as the inspector page name](https://github.com/facebook/react-native/blob/de75a7a22eebbe6b7106377bdd697a2d779b91b0/ReactCommon/hermes/executor/HermesExecutorFactory.cpp#L157) and [the latest page name will overwrite previous one](https://github.com/facebook/react-native/blob/de75a7a22eebbe6b7106377bdd697a2d779b91b0/ReactCommon/hermes/inspector/chrome/ConnectionDemux.cpp#L77-L86). This PR adds more customization for HermesExecutorFactory: - `setEnableDebugger`: provide a way to disable debugging features for the hermes runtime - `setDebuggerName`: provide a way to customize inspector page name other than the default "Hermes React Native" ## Changelog [General] [Added] - Add more debugging settings for *HermesExecutorFactory* Pull Request resolved: #34489 Test Plan: Verify the features by RNTester. 1. `setEnableDebugger` ```diff --- a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java +++ b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java @@ -10,10 +10,12 @@ package com.facebook.react.uiapp; import android.app.Application; import androidx.annotation.NonNull; import com.facebook.fbreact.specs.SampleTurboModule; +import com.facebook.hermes.reactexecutor.HermesExecutorFactory; import com.facebook.react.ReactApplication; import com.facebook.react.ReactNativeHost; import com.facebook.react.ReactPackage; import com.facebook.react.TurboReactPackage; +import com.facebook.react.bridge.JavaScriptExecutorFactory; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.config.ReactFeatureFlags; @@ -50,6 +52,13 @@ public class RNTesterApplication extends Application implements ReactApplication return BuildConfig.DEBUG; } + Override + protected JavaScriptExecutorFactory getJavaScriptExecutorFactory() { + HermesExecutorFactory factory = new HermesExecutorFactory(); + factory.setEnableDebugger(false); + return factory; + } + Override public List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( ``` after app launched, the metro inspector should return empty array. Run `curl http://localhost:8081/json` and returns `[]` 2. `setDebuggerName` ```diff --- a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java +++ b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java @@ -10,10 +10,12 @@ package com.facebook.react.uiapp; import android.app.Application; import androidx.annotation.NonNull; import com.facebook.fbreact.specs.SampleTurboModule; +import com.facebook.hermes.reactexecutor.HermesExecutorFactory; import com.facebook.react.ReactApplication; import com.facebook.react.ReactNativeHost; import com.facebook.react.ReactPackage; import com.facebook.react.TurboReactPackage; +import com.facebook.react.bridge.JavaScriptExecutorFactory; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.config.ReactFeatureFlags; @@ -50,6 +52,13 @@ public class RNTesterApplication extends Application implements ReactApplication return BuildConfig.DEBUG; } + Override + protected JavaScriptExecutorFactory getJavaScriptExecutorFactory() { + HermesExecutorFactory factory = new HermesExecutorFactory(); + factory.setDebuggerName("Custom Hermes Debugger"); + return factory; + } + Override public List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( ``` after app launched, the metro inspector should return an entry with *Custom Hermes Debugger* Run `curl http://localhost:8081/json` and returns ```json [ { "id": "2-1", "description": "com.facebook.react.uiapp", "title": "Custom Hermes Debugger", "faviconUrl": "https://reactjs.org/favicon.ico", "devtoolsFrontendUrl": "devtools://devtools/bundled/js_app.html?experiments=true&v8only=true&ws=%5B%3A%3A1%5D%3A8081%2Finspector%2Fdebug%3Fdevice%3D2 (e5c5dcd9e26e9443f59864d9763b049e0bda98e7)&page=1 (ea93151)", "type": "node", "webSocketDebuggerUrl": "ws://[::1]:8081/inspector/debug?device=2&page=1", "vm": "Hermes" } ] ``` Reviewed By: mdvacca Differential Revision: D38982104 Pulled By: cipolleschi fbshipit-source-id: 78003c173db55448a751145986985b3e1d1c71bb
1 parent ac1fe3b commit 32d12e8

File tree

5 files changed

+72
-17
lines changed

5 files changed

+72
-17
lines changed

ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutor.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ public static void loadLibrary() throws UnsatisfiedLinkError {
3333
}
3434
}
3535

36-
HermesExecutor(@Nullable RuntimeConfig config) {
37-
super(config == null ? initHybridDefaultConfig() : initHybrid(config.heapSizeMB));
36+
HermesExecutor(@Nullable RuntimeConfig config, boolean enableDebugger, String debuggerName) {
37+
super(
38+
config == null
39+
? initHybridDefaultConfig(enableDebugger, debuggerName)
40+
: initHybrid(enableDebugger, debuggerName, config.heapSizeMB));
3841
}
3942

4043
@Override
@@ -51,7 +54,9 @@ public String getName() {
5154
*/
5255
public static native boolean canLoadFile(String path);
5356

54-
private static native HybridData initHybridDefaultConfig();
57+
private static native HybridData initHybridDefaultConfig(
58+
boolean enableDebugger, String debuggerName);
5559

56-
private static native HybridData initHybrid(long heapSizeMB);
60+
private static native HybridData initHybrid(
61+
boolean enableDebugger, String debuggerName, long heapSizeMB);
5762
}

ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutorFactory.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class HermesExecutorFactory implements JavaScriptExecutorFactory {
1515
private static final String TAG = "Hermes";
1616

1717
private final RuntimeConfig mConfig;
18+
private boolean mEnableDebugger = true;
19+
private String mDebuggerName = "";
1820

1921
public HermesExecutorFactory() {
2022
this(null);
@@ -24,9 +26,17 @@ public HermesExecutorFactory(RuntimeConfig config) {
2426
mConfig = config;
2527
}
2628

29+
public void setEnableDebugger(boolean enableDebugger) {
30+
mEnableDebugger = enableDebugger;
31+
}
32+
33+
public void setDebuggerName(String debuggerName) {
34+
mDebuggerName = debuggerName;
35+
}
36+
2737
@Override
2838
public JavaScriptExecutor create() {
29-
return new HermesExecutor(mConfig);
39+
return new HermesExecutor(mConfig, mEnableDebugger, mDebuggerName);
3040
}
3141

3242
@Override

ReactAndroid/src/main/jni/react/hermes/reactexecutor/OnLoad.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,39 @@ class HermesExecutorHolder
6969
"Lcom/facebook/hermes/reactexecutor/HermesExecutor;";
7070

7171
static jni::local_ref<jhybriddata> initHybridDefaultConfig(
72-
jni::alias_ref<jclass>) {
72+
jni::alias_ref<jclass>,
73+
bool enableDebugger,
74+
std::string debuggerName) {
7375
JReactMarker::setLogPerfMarkerIfNeeded();
7476

7577
std::call_once(flag, []() {
7678
facebook::hermes::HermesRuntime::setFatalHandler(hermesFatalHandler);
7779
});
78-
return makeCxxInstance(
79-
std::make_unique<HermesExecutorFactory>(installBindings));
80+
auto factory = std::make_unique<HermesExecutorFactory>(installBindings);
81+
factory->setEnableDebugger(enableDebugger);
82+
if (!debuggerName.empty()) {
83+
factory->setDebuggerName(debuggerName);
84+
}
85+
return makeCxxInstance(std::move(factory));
8086
}
8187

8288
static jni::local_ref<jhybriddata> initHybrid(
8389
jni::alias_ref<jclass>,
90+
bool enableDebugger,
91+
std::string debuggerName,
8492
jlong heapSizeMB) {
8593
JReactMarker::setLogPerfMarkerIfNeeded();
8694
auto runtimeConfig = makeRuntimeConfig(heapSizeMB);
8795
std::call_once(flag, []() {
8896
facebook::hermes::HermesRuntime::setFatalHandler(hermesFatalHandler);
8997
});
90-
return makeCxxInstance(std::make_unique<HermesExecutorFactory>(
91-
installBindings, JSIExecutor::defaultTimeoutInvoker, runtimeConfig));
98+
auto factory = std::make_unique<HermesExecutorFactory>(
99+
installBindings, JSIExecutor::defaultTimeoutInvoker, runtimeConfig);
100+
factory->setEnableDebugger(enableDebugger);
101+
if (!debuggerName.empty()) {
102+
factory->setDebuggerName(debuggerName);
103+
}
104+
return makeCxxInstance(std::move(factory));
92105
}
93106

94107
static bool canLoadFile(jni::alias_ref<jclass>, const std::string &path) {

ReactCommon/hermes/executor/HermesExecutorFactory.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,28 @@ class DecoratedRuntime : public jsi::WithRuntimeDecorator<ReentrancyCheck> {
147147
DecoratedRuntime(
148148
std::unique_ptr<Runtime> runtime,
149149
HermesRuntime &hermesRuntime,
150-
std::shared_ptr<MessageQueueThread> jsQueue)
150+
std::shared_ptr<MessageQueueThread> jsQueue,
151+
bool enableDebugger,
152+
const std::string &debuggerName)
151153
: jsi::WithRuntimeDecorator<ReentrancyCheck>(*runtime, reentrancyCheck_),
152154
runtime_(std::move(runtime)) {
153155
#ifdef HERMES_ENABLE_DEBUGGER
154-
std::shared_ptr<HermesRuntime> rt(runtime_, &hermesRuntime);
155-
auto adapter = std::make_unique<HermesExecutorRuntimeAdapter>(rt, jsQueue);
156-
debugToken_ = facebook::hermes::inspector::chrome::enableDebugging(
157-
std::move(adapter), "Hermes React Native");
156+
enableDebugger_ = enableDebugger;
157+
if (enableDebugger_) {
158+
std::shared_ptr<HermesRuntime> rt(runtime_, &hermesRuntime);
159+
auto adapter =
160+
std::make_unique<HermesExecutorRuntimeAdapter>(rt, jsQueue);
161+
debugToken_ = facebook::hermes::inspector::chrome::enableDebugging(
162+
std::move(adapter), debuggerName);
163+
}
158164
#endif
159165
}
160166

161167
~DecoratedRuntime() {
162168
#ifdef HERMES_ENABLE_DEBUGGER
163-
facebook::hermes::inspector::chrome::disableDebugging(debugToken_);
169+
if (enableDebugger_) {
170+
facebook::hermes::inspector::chrome::disableDebugging(debugToken_);
171+
}
164172
#endif
165173
}
166174

@@ -175,20 +183,33 @@ class DecoratedRuntime : public jsi::WithRuntimeDecorator<ReentrancyCheck> {
175183
std::shared_ptr<Runtime> runtime_;
176184
ReentrancyCheck reentrancyCheck_;
177185
#ifdef HERMES_ENABLE_DEBUGGER
186+
bool enableDebugger_;
178187
facebook::hermes::inspector::chrome::DebugSessionToken debugToken_;
179188
#endif
180189
};
181190

182191
} // namespace
183192

193+
void HermesExecutorFactory::setEnableDebugger(bool enableDebugger) {
194+
enableDebugger_ = enableDebugger;
195+
}
196+
197+
void HermesExecutorFactory::setDebuggerName(const std::string &debuggerName) {
198+
debuggerName_ = debuggerName;
199+
}
200+
184201
std::unique_ptr<JSExecutor> HermesExecutorFactory::createJSExecutor(
185202
std::shared_ptr<ExecutorDelegate> delegate,
186203
std::shared_ptr<MessageQueueThread> jsQueue) {
187204
std::unique_ptr<HermesRuntime> hermesRuntime =
188205
makeHermesRuntimeSystraced(runtimeConfig_);
189206
HermesRuntime &hermesRuntimeRef = *hermesRuntime;
190207
auto decoratedRuntime = std::make_shared<DecoratedRuntime>(
191-
std::move(hermesRuntime), hermesRuntimeRef, jsQueue);
208+
std::move(hermesRuntime),
209+
hermesRuntimeRef,
210+
jsQueue,
211+
enableDebugger_,
212+
debuggerName_);
192213

193214
// So what do we have now?
194215
// DecoratedRuntime -> HermesRuntime

ReactCommon/hermes/executor/HermesExecutorFactory.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class HermesExecutorFactory : public JSExecutorFactory {
2828
assert(timeoutInvoker_ && "Should not have empty timeoutInvoker");
2929
}
3030

31+
void setEnableDebugger(bool enableDebugger);
32+
33+
void setDebuggerName(const std::string &debuggerName);
34+
3135
std::unique_ptr<JSExecutor> createJSExecutor(
3236
std::shared_ptr<ExecutorDelegate> delegate,
3337
std::shared_ptr<MessageQueueThread> jsQueue) override;
@@ -38,6 +42,8 @@ class HermesExecutorFactory : public JSExecutorFactory {
3842
JSIExecutor::RuntimeInstaller runtimeInstaller_;
3943
JSIScopedTimeoutInvoker timeoutInvoker_;
4044
::hermes::vm::RuntimeConfig runtimeConfig_;
45+
bool enableDebugger_ = true;
46+
std::string debuggerName_ = "Hermes React Native";
4147
};
4248

4349
class HermesExecutor : public JSIExecutor {

0 commit comments

Comments
 (0)