summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2022-12-10 00:03:24 +0000
committerLinus Groh <mail@linusgroh.de>2022-12-10 11:23:23 +0000
commit058a247c610d41089b7077e8dbc20a161850cd7a (patch)
tree0cee40725252cec6c854296bffaa5ae7bb36390b /Userland/Libraries/LibJS/Runtime
parent72ee346f64ae93d7b1eb7a5aeb21c74c335f2fbc (diff)
downloadserenity-058a247c610d41089b7077e8dbc20a161850cd7a.zip
LibJS: Add spec comments to Value::to_integer_or_infinity()
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Value.cpp16
1 files changed, 16 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp
index 57184267d6..f3aaa0c8d5 100644
--- a/Userland/Libraries/LibJS/Runtime/Value.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Value.cpp
@@ -1123,14 +1123,28 @@ ThrowCompletionOr<size_t> Value::to_index(VM& vm) const
// 7.1.5 ToIntegerOrInfinity ( argument ), https://tc39.es/ecma262/#sec-tointegerorinfinity
ThrowCompletionOr<double> Value::to_integer_or_infinity(VM& vm) const
{
+ // 1. Let number be ? ToNumber(argument).
auto number = TRY(to_number(vm));
+
+ // 2. If number is NaN, +0𝔽, or -0𝔽, return 0.
if (number.is_nan() || number.as_double() == 0)
return 0;
+
+ // 3. If number is +∞𝔽, return +∞.
+ // 4. If number is -∞𝔽, return -∞.
if (number.is_infinity())
return number.as_double();
+
+ // 5. Let integer be floor(abs(ℝ(number))).
auto integer = floor(fabs(number.as_double()));
+
+ // 6. If number < -0𝔽, set integer to -integer.
+ // NOTE: The zero check is required as 'integer' is a double here but an MV in the spec,
+ // which doesn't have negative zero.
if (number.as_double() < 0 && integer != 0)
integer = -integer;
+
+ // 7. Return integer.
return integer;
}
@@ -1155,6 +1169,8 @@ double to_integer_or_infinity(double number)
auto integer = floor(fabs(number));
// 6. If number < -0𝔽, set integer to -integer.
+ // NOTE: The zero check is required as 'integer' is a double here but an MV in the spec,
+ // which doesn't have negative zero.
if (number < 0 && integer != 0)
integer = -integer;