summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavidot <davidot@serenityos.org>2022-10-12 02:11:23 +0200
committerLinus Groh <mail@linusgroh.de>2022-10-23 15:48:45 +0200
commit9921f808177dfffdb99826713e74c99e50c0a140 (patch)
treefd2e694c3119c4d3ec64bb129a89472c22a75e60
parentc9aa664eb0f7dbf315f628b8255f3e25de5c5452 (diff)
downloadserenity-9921f808177dfffdb99826713e74c99e50c0a140.zip
LibJS: Fix that non-double numbers from JSON were truncated to i32
-rw-r--r--Userland/Libraries/LibJS/Runtime/JSONObject.cpp6
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/JSON/JSON.parse.js23
2 files changed, 26 insertions, 3 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/JSONObject.cpp b/Userland/Libraries/LibJS/Runtime/JSONObject.cpp
index 1bf5b28d53..349a5e43a9 100644
--- a/Userland/Libraries/LibJS/Runtime/JSONObject.cpp
+++ b/Userland/Libraries/LibJS/Runtime/JSONObject.cpp
@@ -416,10 +416,10 @@ Value JSONObject::parse_json_value(VM& vm, JsonValue const& value)
return Value(parse_json_array(vm, value.as_array()));
if (value.is_null())
return js_null();
- if (value.is_double())
- return Value(value.as_double());
+ if (value.is_i32())
+ return Value(value.as_i32());
if (value.is_number())
- return Value(value.to_i32(0));
+ return Value(value.to_double(0));
if (value.is_string())
return js_string(vm, value.to_string());
if (value.is_bool())
diff --git a/Userland/Libraries/LibJS/Tests/builtins/JSON/JSON.parse.js b/Userland/Libraries/LibJS/Tests/builtins/JSON/JSON.parse.js
index ff4d4bd30e..c42dbafa57 100644
--- a/Userland/Libraries/LibJS/Tests/builtins/JSON/JSON.parse.js
+++ b/Userland/Libraries/LibJS/Tests/builtins/JSON/JSON.parse.js
@@ -51,3 +51,26 @@ test("negative zero", () => {
test("long decimal parse", () => {
expect(JSON.parse("1644452550.6489999294281")).toEqual(1644452550.6489999294281);
});
+
+test("does not truncate large integers", () => {
+ expect(JSON.parse("1234567890123")).toEqual(1234567890123);
+ expect(JSON.parse("4294967295")).toEqual(4294967295);
+ expect(JSON.parse("4294967296")).toEqual(4294967296);
+ expect(JSON.parse("4294967297")).toEqual(4294967297);
+ expect(JSON.parse("4294967298")).toEqual(4294967298);
+
+ expect(JSON.parse("2147483647")).toEqual(2147483647);
+ expect(JSON.parse("2147483648")).toEqual(2147483648);
+ expect(JSON.parse("2147483649")).toEqual(2147483649);
+ expect(JSON.parse("2147483650")).toEqual(2147483650);
+
+ expect(JSON.parse("9007199254740991")).toEqual(9007199254740991);
+ expect(JSON.parse("9007199254740992")).toEqual(9007199254740992);
+ expect(JSON.parse("9007199254740993")).toEqual(9007199254740993);
+ expect(JSON.parse("9007199254740994")).toEqual(9007199254740994);
+ expect(JSON.parse("9008199254740994")).toEqual(9008199254740994);
+
+ expect(JSON.parse("18446744073709551615")).toEqual(18446744073709551615);
+ expect(JSON.parse("18446744073709551616")).toEqual(18446744073709551616);
+ expect(JSON.parse("18446744073709551617")).toEqual(18446744073709551617);
+});