/src/serenity/Userland/Libraries/LibJS/Runtime/Intl/MathematicalValue.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/Checked.h> |
10 | | #include <AK/String.h> |
11 | | #include <AK/Variant.h> |
12 | | #include <LibCrypto/BigInt/SignedBigInteger.h> |
13 | | #include <LibJS/Runtime/BigInt.h> |
14 | | #include <LibJS/Runtime/Value.h> |
15 | | |
16 | | namespace JS::Intl { |
17 | | |
18 | | // https://tc39.es/ecma402/#intl-mathematical-value |
19 | | class MathematicalValue { |
20 | | public: |
21 | | enum class Symbol { |
22 | | PositiveInfinity, |
23 | | NegativeInfinity, |
24 | | NegativeZero, |
25 | | NotANumber, |
26 | | }; |
27 | | |
28 | 0 | MathematicalValue() = default; |
29 | | |
30 | | explicit MathematicalValue(double value) |
31 | 0 | : m_value(value_from_number(value)) |
32 | 0 | { |
33 | 0 | } |
34 | | |
35 | | explicit MathematicalValue(Crypto::SignedBigInteger value) |
36 | 0 | : m_value(move(value)) |
37 | 0 | { |
38 | 0 | } |
39 | | |
40 | | explicit MathematicalValue(Symbol symbol) |
41 | 0 | : m_value(symbol) |
42 | 0 | { |
43 | 0 | } |
44 | | |
45 | | MathematicalValue(Value value) |
46 | 0 | : m_value(value.is_number() |
47 | 0 | ? value_from_number(value.as_double()) |
48 | 0 | : ValueType(value.as_bigint().big_integer())) |
49 | 0 | { |
50 | 0 | } |
51 | | |
52 | | bool is_number() const; |
53 | | double as_number() const; |
54 | | |
55 | | bool is_bigint() const; |
56 | | Crypto::SignedBigInteger const& as_bigint() const; |
57 | | |
58 | | bool is_mathematical_value() const; |
59 | | bool is_positive_infinity() const; |
60 | | bool is_negative_infinity() const; |
61 | | bool is_negative_zero() const; |
62 | | bool is_nan() const; |
63 | | |
64 | | void negate(); |
65 | | |
66 | | MathematicalValue plus(Checked<i32> addition) const; |
67 | | MathematicalValue plus(MathematicalValue const& addition) const; |
68 | | |
69 | | MathematicalValue minus(Checked<i32> subtraction) const; |
70 | | MathematicalValue minus(MathematicalValue const& subtraction) const; |
71 | | |
72 | | MathematicalValue multiplied_by(Checked<i32> multiplier) const; |
73 | | MathematicalValue multiplied_by(MathematicalValue const& multiplier) const; |
74 | | |
75 | | MathematicalValue divided_by(Checked<i32> divisor) const; |
76 | | MathematicalValue divided_by(MathematicalValue const& divisor) const; |
77 | | |
78 | | MathematicalValue multiplied_by_power(Checked<i32> exponent) const; |
79 | | MathematicalValue divided_by_power(Checked<i32> exponent) const; |
80 | | |
81 | | bool modulo_is_zero(Checked<i32> mod) const; |
82 | | |
83 | | int logarithmic_floor() const; |
84 | | |
85 | | bool is_equal_to(MathematicalValue const&) const; |
86 | | bool is_less_than(MathematicalValue const&) const; |
87 | | |
88 | | bool is_negative() const; |
89 | | bool is_positive() const; |
90 | | bool is_zero() const; |
91 | | |
92 | | String to_string() const; |
93 | | Value to_value(VM&) const; |
94 | | |
95 | | private: |
96 | | using ValueType = Variant<double, Crypto::SignedBigInteger, Symbol>; |
97 | | |
98 | | static ValueType value_from_number(double number); |
99 | | |
100 | | ValueType m_value { 0.0 }; |
101 | | }; |
102 | | |
103 | | } |