diff options
author | Linus Groh <mail@linusgroh.de> | 2023-04-14 17:04:34 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-04-15 14:07:28 +0200 |
commit | 63624a3a67b5701da22eb728dc8e750c275da3d8 (patch) | |
tree | 60ef5dc035bf2902cbea39e81300cfd33a8e5832 /Userland/Libraries | |
parent | 23d90965415da9210199d63d595506f4166dec59 (diff) | |
download | serenity-63624a3a67b5701da22eb728dc8e750c275da3d8.zip |
LibJS: Add spec comments to NumberConstructor
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/NumberConstructor.cpp | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/NumberConstructor.cpp b/Userland/Libraries/LibJS/Runtime/NumberConstructor.cpp index d061171e7a..6f11164b89 100644 --- a/Userland/Libraries/LibJS/Runtime/NumberConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/NumberConstructor.cpp @@ -108,30 +108,48 @@ ThrowCompletionOr<NonnullGCPtr<Object>> NumberConstructor::construct(FunctionObj // 21.1.2.2 Number.isFinite ( number ), https://tc39.es/ecma262/#sec-number.isfinite JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_finite) { - return Value(vm.argument(0).is_finite_number()); + auto number = vm.argument(0); + + // 1. If number is not a Number, return false. + // 2. If number is not finite, return false. + // 3. Otherwise, return true. + return Value(number.is_finite_number()); } // 21.1.2.3 Number.isInteger ( number ), https://tc39.es/ecma262/#sec-number.isinteger JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_integer) { - return Value(vm.argument(0).is_integral_number()); + auto number = vm.argument(0); + + // 1. Return IsIntegralNumber(number). + return Value(number.is_integral_number()); } // 21.1.2.4 Number.isNaN ( number ), https://tc39.es/ecma262/#sec-number.isnan JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_nan) { - return Value(vm.argument(0).is_nan()); + auto number = vm.argument(0); + + // 1. If number is not a Number, return false. + // 2. If number is NaN, return true. + // 3. Otherwise, return false. + return Value(number.is_nan()); } // 21.1.2.5 Number.isSafeInteger ( number ), https://tc39.es/ecma262/#sec-number.issafeinteger JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_safe_integer) { - if (!vm.argument(0).is_number()) - return Value(false); - if (!vm.argument(0).is_integral_number()) - return Value(false); - auto value = vm.argument(0).as_double(); - return Value(value >= MIN_SAFE_INTEGER_VALUE && value <= MAX_SAFE_INTEGER_VALUE); + auto number = vm.argument(0); + + // 1. If IsIntegralNumber(number) is true, then + if (number.is_integral_number()) { + // a. If abs(ℝ(number)) ≤ 2^53 - 1, return true. + if (fabs(number.as_double()) <= MAX_SAFE_INTEGER_VALUE) + return Value(true); + } + + // 2. Return false. + return Value(false); } } |