Commit 2b4dd779 authored by Johannes Henkel's avatar Johannes Henkel Committed by Commit Bot

[DevTools] Roll inspector_protocol (V8)

"Tweaks for emitting JSON"
https://chromium-review.googlesource.com/c/deps/inspector_protocol/+/2071518

Change-Id: Ie05ff1390a5340e15c21788c58e8171cb442da10
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2073205Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Commit-Queue: Johannes Henkel <johannes@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66509}
parent c6f611be
......@@ -2,7 +2,7 @@ Name: inspector protocol
Short Name: inspector_protocol
URL: https://chromium.googlesource.com/deps/inspector_protocol/
Version: 0
Revision: 1f2492b6768e8620ece36a784c8ecd7ae7091610
Revision: 81ef742ba3587767fc08652d299df9e9b7051407
License: BSD
License File: LICENSE
Security Critical: no
......
......@@ -285,7 +285,22 @@ class JSONEncoder : public ParserHandler {
Emit("null");
return;
}
// If |value| is a scalar, emit it as an int. Taken from json_writer.cc in
// Chromium.
if (value <= std::numeric_limits<int64_t>::max() &&
value >= std::numeric_limits<int64_t>::min() &&
std::floor(value) == value) {
Emit(std::to_string(static_cast<int64_t>(value)));
return;
}
std::string str_value = json::platform::DToStr(value);
// The following is somewhat paranoid, but also taken from json_writer.cc
// in Chromium:
// Ensure that the number has a .0 if there's no decimal or 'e'. This
// makes sure that when we read the JSON back, it's interpreted as a
// real rather than an int.
if (str_value.find_first_of(".eE") == std::string::npos)
str_value.append(".0");
// DToStr may fail to emit a 0 before the decimal dot. E.g. this is
// the case in base::NumberToString in Chromium (which is based on
......
......@@ -184,6 +184,32 @@ TEST(JsonStdStringWriterTest, HelloWorld) {
out);
}
TEST(JsonStdStringWriterTest, ScalarsAreRenderedAsInt) {
// Test that Number.MIN_SAFE_INTEGER / Number.MAX_SAFE_INTEGER from Javascript
// are rendered as integers (no decimal point / rounding), even when we
// encode them from double. Javascript's Number is an IEE754 double, so
// it has 53 bits to represent integers.
std::string out;
Status status;
std::unique_ptr<ParserHandler> writer = NewJSONEncoder(&out, &status);
writer->HandleMapBegin();
writer->HandleString8(SpanFrom("Number.MIN_SAFE_INTEGER"));
EXPECT_EQ(-0x1fffffffffffff, -9007199254740991); // 53 bits for integers.
writer->HandleDouble(-9007199254740991); // Note HandleDouble here.
writer->HandleString8(SpanFrom("Number.MAX_SAFE_INTEGER"));
EXPECT_EQ(0x1fffffffffffff, 9007199254740991); // 53 bits for integers.
writer->HandleDouble(9007199254740991); // Note HandleDouble here.
writer->HandleMapEnd();
EXPECT_TRUE(status.ok());
EXPECT_EQ(
"{\"Number.MIN_SAFE_INTEGER\":-9007199254740991,"
"\"Number.MAX_SAFE_INTEGER\":9007199254740991}",
out);
}
TEST(JsonStdStringWriterTest, RepresentingNonFiniteValuesAsNull) {
// JSON can't represent +Infinity, -Infinity, or NaN.
// So in practice it's mapped to null.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment