From 9dfcb9ec3ae6987a3231e4d6dd2bf8fb557440c8 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Tue, 21 May 2024 15:17:34 -0700 Subject: [PATCH] Generate object previews for live console messages Summary: Changelog: [Internal] bypass-github-export-checks Currently, Hermes never generates object/array previews for values logged via the `console` API. This makes console logs significantly less readable than in Chrome. Here we enable the preview generation machinery that already exists in Hermes. We conservatively mimic V8's behaviour of [only generating previews for immediately-emitted messages](https://source.chromium.org/chromium/chromium/src/+/main:v8/src/inspector/v8-console-agent-impl.cc;l=53,64;drc=451a101b0a8bbc323dbf5697dd956b55284ec9ee) and not for buffered messages. I don't know *why* V8 does this, but can only guess it's meant to improve the performance of starting a debugging session, by evaluating less code and sending smaller payloads. (Anyway, we can change our decision later.) Reviewed By: dannysu Differential Revision: D57617059 fbshipit-source-id: 1f5a71ce98ac915a5b874ed6c009d971405a9f2d --- .../tests/ConsoleApiTest.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tests/ConsoleApiTest.cpp b/packages/react-native/ReactCommon/jsinspector-modern/tests/ConsoleApiTest.cpp index 1dec6c86d4c..5cf43e44842 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tests/ConsoleApiTest.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/tests/ConsoleApiTest.cpp @@ -84,6 +84,10 @@ class ConsoleApiTest JsiIntegrationPortableTest::TearDown(); } + /** + * Expect a console API call to be reported with parameters matching \param + * paramsMatcher. + */ void expectConsoleApiCall(Matcher paramsMatcher) { if (runtimeEnabled_) { expectConsoleApiCallImpl(std::move(paramsMatcher)); @@ -92,6 +96,28 @@ class ConsoleApiTest } } + /** + * Expect a console API call to be reported with parameters matching \param + * paramsMatcher, only if the Runtime domain is currently enabled ( = the call + * is reported in real time). + */ + void expectConsoleApiCallImmediate(Matcher paramsMatcher) { + if (runtimeEnabled_) { + expectConsoleApiCallImpl(std::move(paramsMatcher)); + } + } + + /** + * Expect a console API call to be reported with parameters matching \param + * paramsMatcher, only if the Runtime domain is currently disabled ( = the + * call will be buffered and reported later upon enabling the domain). + */ + void expectConsoleApiCallBuffered(Matcher paramsMatcher) { + if (!runtimeEnabled_) { + expectedConsoleApiCalls_.emplace_back(paramsMatcher); + } + } + bool isRuntimeDomainEnabled() const { return runtimeEnabled_; } @@ -758,6 +784,19 @@ TEST_P(ConsoleApiTest, testConsoleLogTwice) { eval("console.log('hello again');"); } +TEST_P(ConsoleApiTest, testConsoleLogWithObjectPreview) { + InSequence s; + expectConsoleApiCallImmediate(AllOf( + AtJsonPtr("/type", "log"), + AtJsonPtr("/args/0/preview/type", "object"), + AtJsonPtr("/args/0/preview/overflow", false), + AtJsonPtr("/args/0/preview/properties/0/name", "string"), + AtJsonPtr("/args/0/preview/properties/0/type", "string"), + AtJsonPtr("/args/0/preview/properties/0/value", "hello"))); + expectConsoleApiCallBuffered(AllOf(AtJsonPtr("/type", "log"))); + eval("console.log({ string: 'hello' });"); +} + static const auto paramValues = testing::Values( Params{ .withConsolePolyfill = true,