Bug 1845441 - get rid of global constructors in dom/html/HTMLInputElement.cpp r=mstange,dom-core,mccr8

This requires to make Decimal constructor constexpr.

Differential Revision: https://phabricator.services.mozilla.com/D184552
This commit is contained in:
serge-sans-paille 2023-07-27 11:40:18 +00:00
Родитель 92a9f1a8c5
Коммит f65ac86b62
3 изменённых файлов: 53 добавлений и 67 удалений

Просмотреть файл

@ -187,16 +187,16 @@ static const nsAttrValue::EnumTable kCaptureTable[] = {
static const nsAttrValue::EnumTable* kCaptureDefault = &kCaptureTable[2];
const Decimal HTMLInputElement::kStepScaleFactorDate = Decimal(86400000);
const Decimal HTMLInputElement::kStepScaleFactorNumberRange = Decimal(1);
const Decimal HTMLInputElement::kStepScaleFactorTime = Decimal(1000);
const Decimal HTMLInputElement::kStepScaleFactorMonth = Decimal(1);
const Decimal HTMLInputElement::kStepScaleFactorWeek = Decimal(7 * 86400000);
const Decimal HTMLInputElement::kDefaultStepBase = Decimal(0);
const Decimal HTMLInputElement::kDefaultStepBaseWeek = Decimal(-259200000);
const Decimal HTMLInputElement::kDefaultStep = Decimal(1);
const Decimal HTMLInputElement::kDefaultStepTime = Decimal(60);
const Decimal HTMLInputElement::kStepAny = Decimal(0);
constexpr Decimal HTMLInputElement::kStepScaleFactorDate(86400000);
constexpr Decimal HTMLInputElement::kStepScaleFactorNumberRange(1);
constexpr Decimal HTMLInputElement::kStepScaleFactorTime(1000);
constexpr Decimal HTMLInputElement::kStepScaleFactorMonth(1);
constexpr Decimal HTMLInputElement::kStepScaleFactorWeek(7 * 86400000);
constexpr Decimal HTMLInputElement::kDefaultStepBase(0);
constexpr Decimal HTMLInputElement::kDefaultStepBaseWeek(-259200000);
constexpr Decimal HTMLInputElement::kDefaultStep(1);
constexpr Decimal HTMLInputElement::kDefaultStepTime(60);
constexpr Decimal HTMLInputElement::kStepAny(0);
const double HTMLInputElement::kMinimumYear = 1;
const double HTMLInputElement::kMaximumYear = 275760;

Просмотреть файл

@ -41,12 +41,6 @@ namespace blink {
namespace DecimalPrivate {
static int const ExponentMax = 1023;
static int const ExponentMin = -1023;
static int const Precision = 18;
static const uint64_t MaxCoefficient = UINT64_C(0xDE0B6B3A763FFFF); // 999999999999999999 == 18 9's
// This class handles Decimal special values.
class SpecialValueHandler {
STACK_ALLOCATED();
@ -230,43 +224,6 @@ static uint64_t scaleUp(uint64_t x, int n)
using namespace DecimalPrivate;
Decimal::EncodedData::EncodedData(Sign sign, FormatClass formatClass)
: m_coefficient(0)
, m_exponent(0)
, m_formatClass(formatClass)
, m_sign(sign)
{
}
Decimal::EncodedData::EncodedData(Sign sign, int exponent, uint64_t coefficient)
: m_formatClass(coefficient ? ClassNormal : ClassZero)
, m_sign(sign)
{
if (exponent >= ExponentMin && exponent <= ExponentMax) {
while (coefficient > MaxCoefficient) {
coefficient /= 10;
++exponent;
}
}
if (exponent > ExponentMax) {
m_coefficient = 0;
m_exponent = 0;
m_formatClass = ClassInfinity;
return;
}
if (exponent < ExponentMin) {
m_coefficient = 0;
m_exponent = 0;
m_formatClass = ClassZero;
return;
}
m_coefficient = coefficient;
m_exponent = static_cast<int16_t>(exponent);
}
bool Decimal::EncodedData::operator==(const EncodedData& another) const
{
return m_sign == another.m_sign
@ -275,16 +232,6 @@ bool Decimal::EncodedData::operator==(const EncodedData& another) const
&& m_coefficient == another.m_coefficient;
}
Decimal::Decimal(int32_t i32)
: m_data(i32 < 0 ? Negative : Positive, 0, i32 < 0 ? static_cast<uint64_t>(-static_cast<int64_t>(i32)) : static_cast<uint64_t>(i32))
{
}
Decimal::Decimal(Sign sign, int exponent, uint64_t coefficient)
: m_data(sign, coefficient ? exponent : 0, coefficient)
{
}
Decimal::Decimal(const EncodedData& data)
: m_data(data)
{

Просмотреть файл

@ -65,6 +65,11 @@
namespace blink {
namespace DecimalPrivate {
constexpr int ExponentMax = 1023;
constexpr int ExponentMin = -1023;
constexpr int Precision = 18;
static const uint64_t MaxCoefficient = UINT64_C(0xDE0B6B3A763FFFF); // 999999999999999999 == 18 9's
class SpecialValueHandler;
}
@ -88,7 +93,32 @@ public:
friend class Decimal;
friend class DecimalPrivate::SpecialValueHandler;
public:
EncodedData(Sign, int exponent, uint64_t coefficient);
constexpr EncodedData(Sign sign, int exponent, uint64_t coefficient)
: m_coefficient(0),
m_exponent(0),
m_formatClass(coefficient ? ClassNormal : ClassZero),
m_sign(sign) {
if (exponent >= DecimalPrivate::ExponentMin &&
exponent <= DecimalPrivate::ExponentMax) {
while (coefficient > DecimalPrivate::MaxCoefficient) {
coefficient /= 10;
++exponent;
}
}
if (exponent > DecimalPrivate::ExponentMax) {
m_formatClass = ClassInfinity;
return;
}
if (exponent < DecimalPrivate::ExponentMin) {
m_formatClass = ClassZero;
return;
}
m_coefficient = coefficient;
m_exponent = static_cast<int16_t>(exponent);
}
bool operator==(const EncodedData&) const;
bool operator!=(const EncodedData& another) const { return !operator==(another); }
@ -112,7 +142,12 @@ public:
ClassZero,
};
EncodedData(Sign, FormatClass);
constexpr EncodedData(Sign sign, FormatClass formatClass)
: m_coefficient(0),
m_exponent(0),
m_formatClass(formatClass),
m_sign(sign) {}
FormatClass formatClass() const { return m_formatClass; }
uint64_t m_coefficient;
@ -121,8 +156,12 @@ public:
Sign m_sign;
};
MFBT_API explicit Decimal(int32_t = 0);
MFBT_API Decimal(Sign, int exponent, uint64_t coefficient);
MFBT_API constexpr explicit Decimal(int32_t i32 = 0)
: m_data(i32 < 0 ? Negative : Positive, 0,
i32 < 0 ? static_cast<uint64_t>(-static_cast<int64_t>(i32))
: static_cast<uint64_t>(i32)) {}
MFBT_API constexpr Decimal(Sign sign, int exponent, uint64_t coefficient)
: m_data(sign, coefficient ? exponent : 0, coefficient) {}
MFBT_API Decimal(const Decimal&);
MFBT_API Decimal& operator=(const Decimal&);