1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:07:35 +00:00

LibJS: Fix that non-double numbers from JSON were truncated to i32

This commit is contained in:
davidot 2022-10-12 02:11:23 +02:00 committed by Linus Groh
parent c9aa664eb0
commit 9921f80817
2 changed files with 26 additions and 3 deletions

View file

@ -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())

View file

@ -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);
});