From fff5fe44c1f081af90486a2d4b4ab1be7890b12e Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sat, 10 Dec 2022 00:02:10 +0000 Subject: [PATCH] LibJS: Add spec comments to Value::to_u8() --- Userland/Libraries/LibJS/Runtime/Value.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index e6d799bcd9..08eb5c96da 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -1009,15 +1009,24 @@ ThrowCompletionOr Value::to_i8(VM& vm) const // 7.1.11 ToUint8 ( argument ), https://tc39.es/ecma262/#sec-touint8 ThrowCompletionOr Value::to_u8(VM& vm) const { - double value = TRY(to_number(vm)).as_double(); - if (!isfinite(value) || value == 0) + // 1. Let number be ? ToNumber(argument). + double number = TRY(to_number(vm)).as_double(); + + // 2. If number is not finite or number is either +0𝔽 or -0𝔽, return +0𝔽. + if (!isfinite(number) || number == 0) return 0; - auto int_val = floor(fabs(value)); - if (signbit(value)) + + // 3. Let int be the mathematical value whose sign is the sign of number and whose magnitude is floor(abs(ℝ(number))). + auto int_val = floor(fabs(number)); + if (signbit(number)) int_val = -int_val; + + // 4. Let int8bit be int modulo 2^8. auto int8bit = fmod(int_val, NumericLimits::max() + 1.0); if (int8bit < 0) int8bit += NumericLimits::max() + 1.0; + + // 5. Return 𝔽(int8bit). return static_cast(int8bit); }