Skip to content

Commit

Permalink
JSON encode remote object strings
Browse files Browse the repository at this point in the history
Summary: Backports D56031659 from `CDPAgent` to `CDPHandler` in order to fix a visible regression from RN 0.72 --> 0.73 (which introduced the breakage in `CDPHandler`).

Reviewed By: mattbfb

Differential Revision: D56417968

fbshipit-source-id: 04e9ae17c83b1acf1960767efb81aa8b1ac34345
  • Loading branch information
motiz88 authored and huntie committed Apr 25, 2024
1 parent 999cfd9 commit b54a3a0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
12 changes: 9 additions & 3 deletions API/hermes/inspector/chrome/RemoteObjectConverters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,15 @@ m::runtime::RemoteObject m::runtime::makeRemoteObject(
}
} else if (value.isString()) {
result.type = "string";
// result.value is a blob of well-formed JSON. Therefore, we need to add
// surrounding quotes to this string in order for it to be valid JSON.
result.value = "\"" + value.getString(runtime).utf8(runtime) + "\"";

// result.value is a blob of well-formed JSON. Therefore, we need to encode
// the string as JSON.
std::string encodedValue;
llvh::raw_string_ostream stream{encodedValue};
::hermes::JSONEmitter json{stream};
json.emitValue(value.getString(runtime).utf8(runtime));
stream.flush();
result.value = std::move(encodedValue);
} else if (value.isSymbol()) {
result.type = "symbol";
auto sym = value.getSymbol(runtime);
Expand Down
8 changes: 6 additions & 2 deletions API/hermes/inspector/chrome/tests/ConnectionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2592,7 +2592,7 @@ TEST_F(ConnectionTests, testConsoleLog) {
debugger; [1]
var object1 = {number1: 1, bool1: false};
var object2 = {number2: 2, bool2: true};
console.warn('string value', object1, object2);
console.warn('string value', object1, object2, '{"number3": 3}');
debugger; // Hit debugger statement so that we receive console
// api notification before VM gets destroyed.
Expand All @@ -2611,7 +2611,7 @@ TEST_F(ConnectionTests, testConsoleLog) {
auto warningNote =
expectNotification<m::runtime::ConsoleAPICalledNotification>(conn);
EXPECT_EQ(warningNote.type, "warning");
EXPECT_EQ(warningNote.args.size(), 3);
EXPECT_EQ(warningNote.args.size(), 4);

EXPECT_EQ(warningNote.args[0].type, "string");
EXPECT_EQ(*warningNote.args[0].value, "\"string value\"");
Expand All @@ -2620,6 +2620,10 @@ TEST_F(ConnectionTests, testConsoleLog) {

EXPECT_EQ(warningNote.args[2].type, "object");

EXPECT_EQ(warningNote.args[3].type, "string");
// This assertion is equivalent to CDPAgentTest::RuntimeConsoleLogJSON
EXPECT_EQ(*warningNote.args[3].value, R"("{\"number3\": 3}")");

expectPaused(conn, "other", {{"global", 6, 1}});

// Requesting object properties sends requests and expects response messages
Expand Down

0 comments on commit b54a3a0

Please sign in to comment.