2022-07-19 12:07:00 -04:00
|
|
|
/*
|
2024-06-09 14:36:48 -04:00
|
|
|
* Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
|
2022-07-19 12:07:00 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2024-06-09 20:01:21 -04:00
|
|
|
#include <AK/String.h>
|
2022-07-19 12:07:00 -04:00
|
|
|
#include <AK/Variant.h>
|
|
|
|
#include <LibCrypto/BigInt/SignedBigInteger.h>
|
2023-01-28 11:26:03 -05:00
|
|
|
#include <LibJS/Runtime/BigInt.h>
|
2022-07-19 12:07:00 -04:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
2024-06-23 09:14:27 -04:00
|
|
|
#include <LibUnicode/NumberFormat.h>
|
2022-07-19 12:07:00 -04:00
|
|
|
|
|
|
|
namespace JS::Intl {
|
|
|
|
|
2023-04-11 08:45:51 -04:00
|
|
|
// https://tc39.es/ecma402/#intl-mathematical-value
|
2025-06-28 21:39:13 -07:00
|
|
|
class JS_API MathematicalValue {
|
2022-07-19 12:07:00 -04:00
|
|
|
public:
|
|
|
|
enum class Symbol {
|
|
|
|
PositiveInfinity,
|
|
|
|
NegativeInfinity,
|
|
|
|
NegativeZero,
|
|
|
|
NotANumber,
|
|
|
|
};
|
|
|
|
|
|
|
|
MathematicalValue() = default;
|
|
|
|
|
|
|
|
explicit MathematicalValue(double value)
|
|
|
|
: m_value(value_from_number(value))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-06-09 20:01:21 -04:00
|
|
|
explicit MathematicalValue(String value)
|
2022-07-19 12:07:00 -04:00
|
|
|
: m_value(move(value))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit MathematicalValue(Symbol symbol)
|
|
|
|
: m_value(symbol)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MathematicalValue(Value value)
|
|
|
|
: m_value(value.is_number()
|
2024-04-24 06:53:44 -04:00
|
|
|
? value_from_number(value.as_double())
|
2024-06-09 20:01:21 -04:00
|
|
|
: ValueType(MUST(value.as_bigint().big_integer().to_base(10))))
|
2022-07-19 12:07:00 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_number() const;
|
|
|
|
double as_number() const;
|
|
|
|
|
2024-06-09 20:01:21 -04:00
|
|
|
bool is_string() const;
|
|
|
|
String const& as_string() const;
|
2022-07-19 12:07:00 -04:00
|
|
|
|
|
|
|
bool is_mathematical_value() const;
|
|
|
|
bool is_positive_infinity() const;
|
|
|
|
bool is_negative_infinity() const;
|
|
|
|
bool is_negative_zero() const;
|
|
|
|
bool is_nan() const;
|
|
|
|
|
2024-06-23 09:14:27 -04:00
|
|
|
Unicode::NumberFormat::Value to_value() const;
|
2022-07-19 12:07:00 -04:00
|
|
|
|
|
|
|
private:
|
2024-06-09 20:01:21 -04:00
|
|
|
using ValueType = Variant<double, String, Symbol>;
|
2022-07-19 12:07:00 -04:00
|
|
|
|
|
|
|
static ValueType value_from_number(double number);
|
|
|
|
|
2022-11-15 07:12:53 -05:00
|
|
|
ValueType m_value { 0.0 };
|
2022-07-19 12:07:00 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|