2020-04-07 16:17:23 +01:00
|
|
|
|
/*
|
2023-04-13 00:47:15 +02:00
|
|
|
|
* Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
|
2020-04-07 16:17:23 +01:00
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-07 16:17:23 +01:00
|
|
|
|
*/
|
|
|
|
|
|
2021-07-17 18:29:28 +02:00
|
|
|
|
#include <AK/Math.h>
|
2021-06-20 01:09:39 +01:00
|
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
2023-01-28 11:26:03 -05:00
|
|
|
|
#include <LibJS/Runtime/BigInt.h>
|
2020-04-07 16:17:23 +01:00
|
|
|
|
#include <LibJS/Runtime/Error.h>
|
2020-04-18 13:18:06 +02:00
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-04-07 16:17:23 +01:00
|
|
|
|
#include <LibJS/Runtime/NumberConstructor.h>
|
|
|
|
|
#include <LibJS/Runtime/NumberObject.h>
|
2024-07-07 11:01:52 +02:00
|
|
|
|
#include <LibJS/Runtime/ValueInlines.h>
|
2020-04-07 16:17:23 +01:00
|
|
|
|
|
2022-10-04 15:04:13 -04:00
|
|
|
|
#if defined(AK_COMPILER_CLANG)
|
2021-07-17 18:29:28 +02:00
|
|
|
|
# define EPSILON_VALUE AK::exp2(-52.)
|
|
|
|
|
# define MAX_SAFE_INTEGER_VALUE AK::exp2(53.) - 1
|
|
|
|
|
# define MIN_SAFE_INTEGER_VALUE -(AK::exp2(53.) - 1)
|
2020-11-07 00:20:55 +00:00
|
|
|
|
#else
|
2022-04-01 20:58:27 +03:00
|
|
|
|
constexpr double const EPSILON_VALUE { __builtin_exp2(-52) };
|
|
|
|
|
constexpr double const MAX_SAFE_INTEGER_VALUE { __builtin_exp2(53) - 1 };
|
|
|
|
|
constexpr double const MIN_SAFE_INTEGER_VALUE { -(__builtin_exp2(53) - 1) };
|
2020-11-07 00:20:55 +00:00
|
|
|
|
#endif
|
2020-04-07 18:06:44 +01:00
|
|
|
|
|
2020-04-07 16:17:23 +01:00
|
|
|
|
namespace JS {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DEFINE_ALLOCATOR(NumberConstructor);
|
2023-11-19 09:45:05 +01:00
|
|
|
|
|
2022-08-16 00:20:49 +01:00
|
|
|
|
NumberConstructor::NumberConstructor(Realm& realm)
|
2023-04-13 00:47:15 +02:00
|
|
|
|
: NativeFunction(realm.vm().names.Number.as_string(), realm.intrinsics().function_prototype())
|
2020-06-20 15:40:48 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
|
void NumberConstructor::initialize(Realm& realm)
|
2020-04-07 16:17:23 +01:00
|
|
|
|
{
|
2020-10-13 23:49:19 +02:00
|
|
|
|
auto& vm = this->vm();
|
2023-08-07 08:41:28 +02:00
|
|
|
|
Base::initialize(realm);
|
2021-06-13 00:22:35 +01:00
|
|
|
|
|
|
|
|
|
// 21.1.2.15 Number.prototype, https://tc39.es/ecma262/#sec-number.prototype
|
2022-08-27 00:54:55 +01:00
|
|
|
|
define_direct_property(vm.names.prototype, realm.intrinsics().number_prototype(), 0);
|
2021-06-13 00:22:35 +01:00
|
|
|
|
|
2020-04-27 23:05:02 -07:00
|
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
2022-08-22 21:47:35 +01:00
|
|
|
|
define_native_function(realm, vm.names.isFinite, is_finite, 1, attr);
|
|
|
|
|
define_native_function(realm, vm.names.isInteger, is_integer, 1, attr);
|
|
|
|
|
define_native_function(realm, vm.names.isNaN, is_nan, 1, attr);
|
|
|
|
|
define_native_function(realm, vm.names.isSafeInteger, is_safe_integer, 1, attr);
|
2022-08-28 17:09:31 +01:00
|
|
|
|
define_direct_property(vm.names.parseInt, realm.intrinsics().parse_int_function(), attr);
|
|
|
|
|
define_direct_property(vm.names.parseFloat, realm.intrinsics().parse_float_function(), attr);
|
2021-07-06 02:15:08 +03:00
|
|
|
|
define_direct_property(vm.names.EPSILON, Value(EPSILON_VALUE), 0);
|
|
|
|
|
define_direct_property(vm.names.MAX_VALUE, Value(NumericLimits<double>::max()), 0);
|
2023-07-02 18:28:26 +02:00
|
|
|
|
define_direct_property(vm.names.MIN_VALUE, Value(NumericLimits<double>::min_denormal()), 0);
|
2021-07-06 02:15:08 +03:00
|
|
|
|
define_direct_property(vm.names.MAX_SAFE_INTEGER, Value(MAX_SAFE_INTEGER_VALUE), 0);
|
|
|
|
|
define_direct_property(vm.names.MIN_SAFE_INTEGER, Value(MIN_SAFE_INTEGER_VALUE), 0);
|
|
|
|
|
define_direct_property(vm.names.NEGATIVE_INFINITY, js_negative_infinity(), 0);
|
|
|
|
|
define_direct_property(vm.names.POSITIVE_INFINITY, js_infinity(), 0);
|
|
|
|
|
define_direct_property(vm.names.NaN, js_nan(), 0);
|
2021-07-08 02:49:53 +03:00
|
|
|
|
|
|
|
|
|
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
|
2020-04-07 16:17:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-19 01:34:17 +01:00
|
|
|
|
// Most of 21.1.1.1 Number ( value ) factored into a separate function for sharing between call() and construct().
|
2022-08-21 20:38:35 +01:00
|
|
|
|
static ThrowCompletionOr<Value> get_value_from_constructor_argument(VM& vm)
|
2021-06-19 01:34:17 +01:00
|
|
|
|
{
|
|
|
|
|
Value number;
|
2022-08-25 18:02:49 +02:00
|
|
|
|
// 1. If value is present, then
|
2021-06-19 01:34:17 +01:00
|
|
|
|
if (vm.argument_count() > 0) {
|
2022-08-25 18:02:49 +02:00
|
|
|
|
// a. Let prim be ? ToNumeric(value).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto primitive = TRY(vm.argument(0).to_numeric(vm));
|
2022-08-25 18:02:49 +02:00
|
|
|
|
|
|
|
|
|
// b. If Type(prim) is BigInt, let n be 𝔽(ℝ(prim)).
|
2021-06-19 01:34:17 +01:00
|
|
|
|
if (primitive.is_bigint()) {
|
2022-08-25 18:02:49 +02:00
|
|
|
|
number = Value(primitive.as_bigint().big_integer().to_double(Crypto::UnsignedBigInteger::RoundingMode::ECMAScriptNumberValueFor));
|
|
|
|
|
}
|
|
|
|
|
// c. Otherwise, let n be prim.
|
|
|
|
|
else {
|
2021-06-19 01:34:17 +01:00
|
|
|
|
number = primitive;
|
|
|
|
|
}
|
2022-08-25 18:02:49 +02:00
|
|
|
|
}
|
|
|
|
|
// 2. Else,
|
|
|
|
|
else {
|
|
|
|
|
// a. Let n be +0𝔽.
|
2021-06-19 01:34:17 +01:00
|
|
|
|
number = Value(0);
|
|
|
|
|
}
|
|
|
|
|
return number;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 21.1.1.1 Number ( value ), https://tc39.es/ecma262/#sec-number-constructor-number-value
|
2021-10-20 21:16:30 +01:00
|
|
|
|
ThrowCompletionOr<Value> NumberConstructor::call()
|
2020-04-07 16:17:23 +01:00
|
|
|
|
{
|
2022-08-25 18:02:49 +02:00
|
|
|
|
// NOTE: get_value_from_constructor_argument performs steps 1 and 2 and returns n.
|
|
|
|
|
// 3. If NewTarget is undefined, return n.
|
2022-08-21 20:38:35 +01:00
|
|
|
|
return get_value_from_constructor_argument(vm());
|
2020-04-07 16:17:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 21.1.1.1 Number ( value ), https://tc39.es/ecma262/#sec-number-constructor-number-value
|
2024-11-15 04:01:23 +13:00
|
|
|
|
ThrowCompletionOr<GC::Ref<Object>> NumberConstructor::construct(FunctionObject& new_target)
|
2020-04-07 16:17:23 +01:00
|
|
|
|
{
|
2022-08-21 19:24:32 +01:00
|
|
|
|
auto& vm = this->vm();
|
2022-08-25 18:02:49 +02:00
|
|
|
|
// NOTE: get_value_from_constructor_argument performs steps 1 and 2 and returns n.
|
2022-08-21 20:38:35 +01:00
|
|
|
|
auto number = TRY(get_value_from_constructor_argument(vm));
|
2022-08-25 18:02:49 +02:00
|
|
|
|
|
|
|
|
|
// 4. Let O be ? OrdinaryCreateFromConstructor(NewTarget, "%Number.prototype%", « [[NumberData]] »).
|
|
|
|
|
// 5. Set O.[[NumberData]] to n.
|
|
|
|
|
// 6. Return O.
|
2022-12-14 19:18:10 +00:00
|
|
|
|
return TRY(ordinary_create_from_constructor<NumberObject>(vm, new_target, &Intrinsics::number_prototype, number.as_double()));
|
2020-04-07 16:17:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 21.1.2.2 Number.isFinite ( number ), https://tc39.es/ecma262/#sec-number.isfinite
|
2021-10-29 00:46:14 +03:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_finite)
|
2020-04-26 18:24:58 +01:00
|
|
|
|
{
|
2023-04-14 17:04:34 +02:00
|
|
|
|
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());
|
2020-04-26 18:24:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 21.1.2.3 Number.isInteger ( number ), https://tc39.es/ecma262/#sec-number.isinteger
|
2021-10-29 00:46:14 +03:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_integer)
|
2020-04-26 18:49:23 +01:00
|
|
|
|
{
|
2023-04-14 17:04:34 +02:00
|
|
|
|
auto number = vm.argument(0);
|
|
|
|
|
|
|
|
|
|
// 1. Return IsIntegralNumber(number).
|
|
|
|
|
return Value(number.is_integral_number());
|
2020-04-26 18:49:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 21.1.2.4 Number.isNaN ( number ), https://tc39.es/ecma262/#sec-number.isnan
|
2021-10-29 00:46:14 +03:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_nan)
|
2020-04-26 18:31:56 +01:00
|
|
|
|
{
|
2023-04-14 17:04:34 +02:00
|
|
|
|
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());
|
2020-04-26 18:31:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 21.1.2.5 Number.isSafeInteger ( number ), https://tc39.es/ecma262/#sec-number.issafeinteger
|
2021-10-29 00:46:14 +03:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_safe_integer)
|
2020-04-07 18:06:44 +01:00
|
|
|
|
{
|
2023-04-14 17:04:34 +02:00
|
|
|
|
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);
|
2020-04-07 18:06:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 16:17:23 +01:00
|
|
|
|
}
|