diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index 2f61d02b91..d89719f1ea 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -909,13 +909,22 @@ ThrowCompletionOr Value::to_i32(VM& vm) const // 7.1.7 ToUint32 ( argument ), https://tc39.es/ecma262/#sec-touint32 ThrowCompletionOr Value::to_u32(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 int32bit be int modulo 2^32. auto int32bit = fmod(int_val, NumericLimits::max() + 1.0); + + // 5. Return 𝔽(int32bit). // Cast to i64 here to ensure that the double --> u32 cast doesn't invoke undefined behavior // Otherwise, negative numbers cause a UBSAN warning. return static_cast(static_cast(int32bit));