From 9a8a0072188d78be4951a9f4cfa665f0fff57153 Mon Sep 17 00:00:00 2001 From: Aaron Simkin Date: Tue, 22 Nov 2022 16:02:20 +0000 Subject: [PATCH] Use integers as numbers if possible If we encounterd a number and it can be represented as int32 withouth a loss of precision then use the integer representation instead of double. Signed-off-by: Aaron Simkin --- runtime/base/json_parser.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/runtime/base/json_parser.h b/runtime/base/json_parser.h index edbe03dbb..fd6644b0f 100644 --- a/runtime/base/json_parser.h +++ b/runtime/base/json_parser.h @@ -30,6 +30,7 @@ #include "plugins/ecmascript/runtime/js_handle.h" #include "plugins/ecmascript/runtime/js_tagged_value.h" #include "plugins/ecmascript/runtime/object_factory.h" +#include "plugins/ecmascript/es2panda/util/helpers.h" namespace panda::ecmascript::base { constexpr unsigned int UNICODE_DIGIT_LENGTH = 4; @@ -133,7 +134,11 @@ private: if (isFast) { std::string strNum(current_, end_ + 1); current_ = end_; - return JSTaggedValue(std::stod(strNum)); + auto d = std::stod(strNum); + if (es2panda::util::Helpers::IsInteger(d)) { + return JSTaggedValue(static_cast(d)); + } + return JSTaggedValue(d); } } -- Gitee