/src/serenity/Userland/Libraries/LibJS/Runtime/NumberConstructor.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <AK/Math/Exponentials.h> |
8 | | #include <LibJS/Runtime/AbstractOperations.h> |
9 | | #include <LibJS/Runtime/BigInt.h> |
10 | | #include <LibJS/Runtime/Error.h> |
11 | | #include <LibJS/Runtime/GlobalObject.h> |
12 | | #include <LibJS/Runtime/NumberConstructor.h> |
13 | | #include <LibJS/Runtime/NumberObject.h> |
14 | | #include <LibJS/Runtime/ValueInlines.h> |
15 | | |
16 | | #if defined(AK_COMPILER_CLANG) |
17 | 0 | # define EPSILON_VALUE AK::exp2(-52.) |
18 | 0 | # define MAX_SAFE_INTEGER_VALUE AK::exp2(53.) - 1 |
19 | 0 | # define MIN_SAFE_INTEGER_VALUE -(AK::exp2(53.) - 1) |
20 | | #else |
21 | | constexpr double const EPSILON_VALUE { __builtin_exp2(-52) }; |
22 | | constexpr double const MAX_SAFE_INTEGER_VALUE { __builtin_exp2(53) - 1 }; |
23 | | constexpr double const MIN_SAFE_INTEGER_VALUE { -(__builtin_exp2(53) - 1) }; |
24 | | #endif |
25 | | |
26 | | namespace JS { |
27 | | |
28 | | JS_DEFINE_ALLOCATOR(NumberConstructor); |
29 | | |
30 | | NumberConstructor::NumberConstructor(Realm& realm) |
31 | 0 | : NativeFunction(realm.vm().names.Number.as_string(), realm.intrinsics().function_prototype()) |
32 | 0 | { |
33 | 0 | } |
34 | | |
35 | | void NumberConstructor::initialize(Realm& realm) |
36 | 0 | { |
37 | 0 | auto& vm = this->vm(); |
38 | 0 | Base::initialize(realm); |
39 | | |
40 | | // 21.1.2.15 Number.prototype, https://tc39.es/ecma262/#sec-number.prototype |
41 | 0 | define_direct_property(vm.names.prototype, realm.intrinsics().number_prototype(), 0); |
42 | |
|
43 | 0 | u8 attr = Attribute::Writable | Attribute::Configurable; |
44 | 0 | define_native_function(realm, vm.names.isFinite, is_finite, 1, attr); |
45 | 0 | define_native_function(realm, vm.names.isInteger, is_integer, 1, attr); |
46 | 0 | define_native_function(realm, vm.names.isNaN, is_nan, 1, attr); |
47 | 0 | define_native_function(realm, vm.names.isSafeInteger, is_safe_integer, 1, attr); |
48 | 0 | define_direct_property(vm.names.parseInt, realm.intrinsics().parse_int_function(), attr); |
49 | 0 | define_direct_property(vm.names.parseFloat, realm.intrinsics().parse_float_function(), attr); |
50 | 0 | define_direct_property(vm.names.EPSILON, Value(EPSILON_VALUE), 0); |
51 | 0 | define_direct_property(vm.names.MAX_VALUE, Value(NumericLimits<double>::max()), 0); |
52 | 0 | define_direct_property(vm.names.MIN_VALUE, Value(NumericLimits<double>::min_denormal()), 0); |
53 | 0 | define_direct_property(vm.names.MAX_SAFE_INTEGER, Value(MAX_SAFE_INTEGER_VALUE), 0); |
54 | 0 | define_direct_property(vm.names.MIN_SAFE_INTEGER, Value(MIN_SAFE_INTEGER_VALUE), 0); |
55 | 0 | define_direct_property(vm.names.NEGATIVE_INFINITY, js_negative_infinity(), 0); |
56 | 0 | define_direct_property(vm.names.POSITIVE_INFINITY, js_infinity(), 0); |
57 | 0 | define_direct_property(vm.names.NaN, js_nan(), 0); |
58 | |
|
59 | 0 | define_direct_property(vm.names.length, Value(1), Attribute::Configurable); |
60 | 0 | } |
61 | | |
62 | | // Most of 21.1.1.1 Number ( value ) factored into a separate function for sharing between call() and construct(). |
63 | | static ThrowCompletionOr<Value> get_value_from_constructor_argument(VM& vm) |
64 | 0 | { |
65 | 0 | Value number; |
66 | | // 1. If value is present, then |
67 | 0 | if (vm.argument_count() > 0) { |
68 | | // a. Let prim be ? ToNumeric(value). |
69 | 0 | auto primitive = TRY(vm.argument(0).to_numeric(vm)); |
70 | | |
71 | | // b. If Type(prim) is BigInt, let n be 𝔽(ℝ(prim)). |
72 | 0 | if (primitive.is_bigint()) { |
73 | 0 | number = Value(primitive.as_bigint().big_integer().to_double(Crypto::UnsignedBigInteger::RoundingMode::ECMAScriptNumberValueFor)); |
74 | 0 | } |
75 | | // c. Otherwise, let n be prim. |
76 | 0 | else { |
77 | 0 | number = primitive; |
78 | 0 | } |
79 | 0 | } |
80 | | // 2. Else, |
81 | 0 | else { |
82 | | // a. Let n be +0𝔽. |
83 | 0 | number = Value(0); |
84 | 0 | } |
85 | 0 | return number; |
86 | 0 | } |
87 | | |
88 | | // 21.1.1.1 Number ( value ), https://tc39.es/ecma262/#sec-number-constructor-number-value |
89 | | ThrowCompletionOr<Value> NumberConstructor::call() |
90 | 0 | { |
91 | | // NOTE: get_value_from_constructor_argument performs steps 1 and 2 and returns n. |
92 | | // 3. If NewTarget is undefined, return n. |
93 | 0 | return get_value_from_constructor_argument(vm()); |
94 | 0 | } |
95 | | |
96 | | // 21.1.1.1 Number ( value ), https://tc39.es/ecma262/#sec-number-constructor-number-value |
97 | | ThrowCompletionOr<NonnullGCPtr<Object>> NumberConstructor::construct(FunctionObject& new_target) |
98 | 0 | { |
99 | 0 | auto& vm = this->vm(); |
100 | | // NOTE: get_value_from_constructor_argument performs steps 1 and 2 and returns n. |
101 | 0 | auto number = TRY(get_value_from_constructor_argument(vm)); |
102 | | |
103 | | // 4. Let O be ? OrdinaryCreateFromConstructor(NewTarget, "%Number.prototype%", « [[NumberData]] »). |
104 | | // 5. Set O.[[NumberData]] to n. |
105 | | // 6. Return O. |
106 | 0 | return TRY(ordinary_create_from_constructor<NumberObject>(vm, new_target, &Intrinsics::number_prototype, number.as_double())); |
107 | 0 | } |
108 | | |
109 | | // 21.1.2.2 Number.isFinite ( number ), https://tc39.es/ecma262/#sec-number.isfinite |
110 | | JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_finite) |
111 | 0 | { |
112 | 0 | auto number = vm.argument(0); |
113 | | |
114 | | // 1. If number is not a Number, return false. |
115 | | // 2. If number is not finite, return false. |
116 | | // 3. Otherwise, return true. |
117 | 0 | return Value(number.is_finite_number()); |
118 | 0 | } |
119 | | |
120 | | // 21.1.2.3 Number.isInteger ( number ), https://tc39.es/ecma262/#sec-number.isinteger |
121 | | JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_integer) |
122 | 0 | { |
123 | 0 | auto number = vm.argument(0); |
124 | | |
125 | | // 1. Return IsIntegralNumber(number). |
126 | 0 | return Value(number.is_integral_number()); |
127 | 0 | } |
128 | | |
129 | | // 21.1.2.4 Number.isNaN ( number ), https://tc39.es/ecma262/#sec-number.isnan |
130 | | JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_nan) |
131 | 0 | { |
132 | 0 | auto number = vm.argument(0); |
133 | | |
134 | | // 1. If number is not a Number, return false. |
135 | | // 2. If number is NaN, return true. |
136 | | // 3. Otherwise, return false. |
137 | 0 | return Value(number.is_nan()); |
138 | 0 | } |
139 | | |
140 | | // 21.1.2.5 Number.isSafeInteger ( number ), https://tc39.es/ecma262/#sec-number.issafeinteger |
141 | | JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_safe_integer) |
142 | 0 | { |
143 | 0 | auto number = vm.argument(0); |
144 | | |
145 | | // 1. If IsIntegralNumber(number) is true, then |
146 | 0 | if (number.is_integral_number()) { |
147 | | // a. If abs(ℝ(number)) ≤ 2^53 - 1, return true. |
148 | 0 | if (fabs(number.as_double()) <= MAX_SAFE_INTEGER_VALUE) |
149 | 0 | return Value(true); |
150 | 0 | } |
151 | | |
152 | | // 2. Return false. |
153 | 0 | return Value(false); |
154 | 0 | } |
155 | | |
156 | | } |