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

LibJS: Implement parseInt()

Here's a reasonably faithful implementation of ECMAScript 2021 18.2.5.
Some corner cases are not covered, I've left them as FIXME's in the
included unit test.

Also I had to tweak JS::Value::to_i32() to always convert infinity to
zero, which is in accordance with ToInt32 AFAICT.
This commit is contained in:
Andreas Kling 2020-12-05 13:51:09 +01:00
parent 4ae0de9a37
commit e6dadd9e5b
5 changed files with 125 additions and 3 deletions

View file

@ -356,10 +356,8 @@ i32 Value::to_i32(GlobalObject& global_object) const
auto number = to_number(global_object);
if (global_object.vm().exception())
return 0;
if (number.is_nan())
if (number.is_nan() || number.is_infinity())
return 0;
// FIXME: What about infinity though - that's UB...
// Maybe NumericLimits<i32>::max() for +Infinity and NumericLimits<i32>::min() for -Infinity?
return number.as_i32();
}