From 77ef8f881f2e4067894b412f308e2a80042c946f Mon Sep 17 00:00:00 2001 From: Martin Sherburn Date: Wed, 13 May 2020 07:18:21 -0700 Subject: [PATCH] When debugger is attached continue to receive console messages Summary: I noticed an issue when the hermes debugger was attached. Console messages would no longer appear in the terminal. This is because the hermes inspector overrides the console object with its own to intercept messages and send them to the debug client. With this change I made it so that messages are sent to the original console as well instead of completely replacing it. Changelog: [General][Fixed] - When Hermes debugger is enabled continue to send log messages to the console Reviewed By: mhorowitz Differential Revision: D21509895 fbshipit-source-id: 6d91c4b82682e404679533be14b3e5f12e687e79 --- ReactCommon/hermes/inspector/Inspector.cpp | 52 +++++++++++++++------- ReactCommon/hermes/inspector/Inspector.h | 1 + 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/ReactCommon/hermes/inspector/Inspector.cpp b/ReactCommon/hermes/inspector/Inspector.cpp index 3cca8ce1553b22..60a31bef749b96 100644 --- a/ReactCommon/hermes/inspector/Inspector.cpp +++ b/ReactCommon/hermes/inspector/Inspector.cpp @@ -137,6 +137,7 @@ Inspector::~Inspector() { void Inspector::installConsoleFunction( jsi::Object &console, + std::shared_ptr &originalConsole, const std::string &name, const std::string &chromeTypeDefault = "") { jsi::Runtime &rt = adapter_->getRuntime(); @@ -150,11 +151,22 @@ void Inspector::installConsoleFunction( rt, nameID, 1, - [weakInspector, chromeType]( + [weakInspector, originalConsole, name, chromeType]( jsi::Runtime &runtime, const jsi::Value &thisVal, const jsi::Value *args, size_t count) { + if (originalConsole) { + auto val = originalConsole->getProperty(runtime, name.c_str()); + if (val.isObject()) { + auto obj = val.getObject(runtime); + if (obj.isFunction(runtime)) { + auto func = obj.getFunction(runtime); + func.call(runtime, args, count); + } + } + } + if (auto inspector = weakInspector.lock()) { jsi::Array argsArray(runtime, count); for (size_t index = 0; index < count; ++index) @@ -170,22 +182,28 @@ void Inspector::installConsoleFunction( void Inspector::installLogHandler() { jsi::Runtime &rt = adapter_->getRuntime(); auto console = jsi::Object(rt); - installConsoleFunction(console, "assert"); - installConsoleFunction(console, "clear"); - installConsoleFunction(console, "debug"); - installConsoleFunction(console, "dir"); - installConsoleFunction(console, "dirxml"); - installConsoleFunction(console, "error"); - installConsoleFunction(console, "group", "startGroup"); - installConsoleFunction(console, "groupCollapsed", "startGroupCollapsed"); - installConsoleFunction(console, "groupEnd", "endGroup"); - installConsoleFunction(console, "info"); - installConsoleFunction(console, "log"); - installConsoleFunction(console, "profile"); - installConsoleFunction(console, "profileEnd"); - installConsoleFunction(console, "table"); - installConsoleFunction(console, "trace"); - installConsoleFunction(console, "warn", "warning"); + auto val = rt.global().getProperty(rt, "console"); + std::shared_ptr originalConsole; + if (val.isObject()) { + originalConsole = std::make_shared(val.getObject(rt)); + } + installConsoleFunction(console, originalConsole, "assert"); + installConsoleFunction(console, originalConsole, "clear"); + installConsoleFunction(console, originalConsole, "debug"); + installConsoleFunction(console, originalConsole, "dir"); + installConsoleFunction(console, originalConsole, "dirxml"); + installConsoleFunction(console, originalConsole, "error"); + installConsoleFunction(console, originalConsole, "group", "startGroup"); + installConsoleFunction( + console, originalConsole, "groupCollapsed", "startGroupCollapsed"); + installConsoleFunction(console, originalConsole, "groupEnd", "endGroup"); + installConsoleFunction(console, originalConsole, "info"); + installConsoleFunction(console, originalConsole, "log"); + installConsoleFunction(console, originalConsole, "profile"); + installConsoleFunction(console, originalConsole, "profileEnd"); + installConsoleFunction(console, originalConsole, "table"); + installConsoleFunction(console, originalConsole, "trace"); + installConsoleFunction(console, originalConsole, "warn", "warning"); rt.global().setProperty(rt, "console", console); } diff --git a/ReactCommon/hermes/inspector/Inspector.h b/ReactCommon/hermes/inspector/Inspector.h index bf4fc553c1da1f..ad77ba54467842 100644 --- a/ReactCommon/hermes/inspector/Inspector.h +++ b/ReactCommon/hermes/inspector/Inspector.h @@ -292,6 +292,7 @@ class Inspector : public facebook::hermes::debugger::EventObserver, void installConsoleFunction( jsi::Object &console, + std::shared_ptr &originalConsole, const std::string &name, const std::string &chromeType);