mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 09:30:24 +00:00
LibJS: Add spec comments to Value::to_u32()
This commit is contained in:
parent
f958b19b19
commit
1ef2b43e4c
Notes:
sideshowbarker
2024-07-17 18:46:30 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/1ef2b43e4c Pull-request: https://github.com/SerenityOS/serenity/pull/16405 Reviewed-by: https://github.com/davidot ✅
1 changed files with 13 additions and 4 deletions
|
@ -909,13 +909,22 @@ ThrowCompletionOr<i32> Value::to_i32(VM& vm) const
|
|||
// 7.1.7 ToUint32 ( argument ), https://tc39.es/ecma262/#sec-touint32
|
||||
ThrowCompletionOr<u32> 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<u32>::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<u32>(static_cast<i64>(int32bit));
|
||||
|
|
Loading…
Reference in a new issue