From fae1c32267898690e146e6bd3da5e2900aa0471e Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Sat, 1 Jun 2024 03:15:23 -0700 Subject: [PATCH] =?UTF-8?q?Encode=20=C2=B1INFINITY=20as=20=C2=B11e5000?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The V8 behavior of encoding infinity as null doesn't make sense to me. Using ±1e5000 is better, because JSON.parse decodes it as INFINITY and the information is preserved. This could be a breaking change for some --- test/tool/net/encodejson_test.lua | 2 +- test/tool/net/jsontestsuite_okay_test.lua | 15 +++++---------- third_party/double-conversion/wrapper.cc | 2 +- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/test/tool/net/encodejson_test.lua b/test/tool/net/encodejson_test.lua index edd97df91..02f464275 100644 --- a/test/tool/net/encodejson_test.lua +++ b/test/tool/net/encodejson_test.lua @@ -22,7 +22,7 @@ assert(EncodeJson(0) == "0") assert(EncodeJson(0.0) == "0") assert(EncodeJson(3.14) == "3.14") assert(EncodeJson(0/0) == "null") -assert(EncodeJson(math.huge) == "null") +assert(EncodeJson(math.huge) == "1e5000") assert(EncodeJson(123.456e-789) == '0') assert(EncodeJson(9223372036854775807) == '9223372036854775807') assert(EncodeJson(-9223372036854775807 - 1) == '-9223372036854775808') diff --git a/test/tool/net/jsontestsuite_okay_test.lua b/test/tool/net/jsontestsuite_okay_test.lua index cbcc465c7..119a80ad2 100644 --- a/test/tool/net/jsontestsuite_okay_test.lua +++ b/test/tool/net/jsontestsuite_okay_test.lua @@ -176,30 +176,25 @@ assert(DecodeJson(' [123e-10000000] ')) assert(EncodeJson(DecodeJson(' [123e-10000000] ')) == '[0]') assert(EncodeLua(DecodeJson(' [123e-10000000] ')) == '{0.}') --- [jart] consistent with v8 we encode Infinity as null (wut?) -- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/i_number_real_pos_overflow.json assert(DecodeJson(' [123123e100000] ')) -assert(EncodeJson(DecodeJson(' [123123e100000] ')) == '[null]') +assert(EncodeJson(DecodeJson(' [123123e100000] ')) == '[1e5000]') --- [jart] consistent with v8 we encode -Infinity as null (wut?) -- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/i_number_real_neg_overflow.json assert(DecodeJson(' [-123123e100000] ')) -assert(EncodeJson(DecodeJson(' [-123123e100000] ')) == '[null]') +assert(EncodeJson(DecodeJson(' [-123123e100000] ')) == '[-1e5000]') --- [jart] consistent with v8 we encode Infinity as null (wut?) -- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/i_number_pos_double_huge_exp.json assert(DecodeJson(' [1.5e+9999] ')) -assert(EncodeJson(DecodeJson(' [1.5e+9999] ')) == '[null]') +assert(EncodeJson(DecodeJson(' [1.5e+9999] ')) == '[1e5000]') --- [jart] consistent with v8 we encode -Infinity as null (wut?) -- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/i_number_neg_int_huge_exp.json assert(DecodeJson(' [-1e+9999] ')) -assert(EncodeJson(DecodeJson(' [-1e+9999] ')) == '[null]') +assert(EncodeJson(DecodeJson(' [-1e+9999] ')) == '[-1e5000]') --- [jart] consistent with v8 we encode Infinity as null (wut?) -- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/i_number_huge_exp.json assert(DecodeJson(' [0.4e00669999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999969999999006] ')) -assert(EncodeJson(DecodeJson(' [0.4e00669999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999969999999006] ')) == '[null]') +assert(EncodeJson(DecodeJson(' [0.4e00669999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999969999999006] ')) == '[1e5000]') -- [jart] consistent with v8 we encode underflow as 0 -- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/i_number_double_huge_neg_exp.json diff --git a/third_party/double-conversion/wrapper.cc b/third_party/double-conversion/wrapper.cc index 8e737d3f5..11b56291c 100644 --- a/third_party/double-conversion/wrapper.cc +++ b/third_party/double-conversion/wrapper.cc @@ -38,7 +38,7 @@ char* DoubleToJson(char buf[128], double x) { static const DoubleToStringConverter kDoubleToJson( DoubleToStringConverter::UNIQUE_ZERO | DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN, - "null", "null", 'e', -6, 21, 6, 0); + "1e5000", "null", 'e', -6, 21, 6, 0); kDoubleToJson.ToShortest(x, &b); b.Finalize(); if (READ32LE(buf) != READ32LE("-nul")) {