summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-01-28 12:52:32 -0500
committerLinus Groh <mail@linusgroh.de>2022-01-28 19:38:47 +0000
commitac3e42a8de835a04e9008d182d3620307a8b907a (patch)
tree9a1a3e7e9b7334b5ad0174ce96b6791e5f801818 /Userland/Libraries/LibJS/Runtime
parente1ad9326df316949ded81dfba4c68bc4eea608c8 (diff)
downloadserenity-ac3e42a8de835a04e9008d182d3620307a8b907a.zip
LibJS: Move some Intl.NumberFormat fields into a NumberFormatBase class
Other Intl objects, such as PluralRules, are to be treated as a NumberFormat object in some AOs. There's only a handful of fields which are to be shared between those objects - move them to a base class for shared reuse. This also updates the couple of NumberFormat AOs that are meant to operate on these NumberFormat-like objects. Alternatively, we could just have objects like PluralRules inherit from NumberFormat directly. But that messes up the is<NumberFormat> runtime checks, so this feels safer.
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp33
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h125
2 files changed, 91 insertions, 67 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp
index 20ff8e59b8..2adbe53e79 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp
@@ -15,9 +15,14 @@
namespace JS::Intl {
+NumberFormatBase::NumberFormatBase(Object& prototype)
+ : Object(prototype)
+{
+}
+
// 15 NumberFormat Objects, https://tc39.es/ecma402/#numberformat-objects
NumberFormat::NumberFormat(Object& prototype)
- : Object(prototype)
+ : NumberFormatBase(prototype)
{
}
@@ -144,7 +149,7 @@ StringView NumberFormat::currency_sign_string() const
}
}
-StringView NumberFormat::rounding_type_string() const
+StringView NumberFormatBase::rounding_type_string() const
{
switch (m_rounding_type) {
case RoundingType::SignificantDigits:
@@ -248,7 +253,7 @@ static ALWAYS_INLINE int log10floor(double value)
}
// 15.1.1 SetNumberFormatDigitOptions ( intlObj, options, mnfdDefault, mxfdDefault, notation ), https://tc39.es/ecma402/#sec-setnfdigitoptions
-ThrowCompletionOr<void> set_number_format_digit_options(GlobalObject& global_object, NumberFormat& intl_object, Object const& options, int default_min_fraction_digits, int default_max_fraction_digits, NumberFormat::Notation notation)
+ThrowCompletionOr<void> set_number_format_digit_options(GlobalObject& global_object, NumberFormatBase& intl_object, Object const& options, int default_min_fraction_digits, int default_max_fraction_digits, NumberFormat::Notation notation)
{
auto& vm = global_object.vm();
@@ -348,17 +353,17 @@ ThrowCompletionOr<void> set_number_format_digit_options(GlobalObject& global_obj
// 16. If needSd is false and needFd is false, then
if (!need_significant_digits && !need_fraction_digits) {
// a. Set intlObj.[[RoundingType]] to compactRounding.
- intl_object.set_rounding_type(NumberFormat::RoundingType::CompactRounding);
+ intl_object.set_rounding_type(NumberFormatBase::RoundingType::CompactRounding);
}
// 17. Else if hasSd is true, then
else if (has_significant_digits) {
// a. Set intlObj.[[RoundingType]] to significantDigits.
- intl_object.set_rounding_type(NumberFormat::RoundingType::SignificantDigits);
+ intl_object.set_rounding_type(NumberFormatBase::RoundingType::SignificantDigits);
}
// 18. Else,
else {
// a. Set intlObj.[[RoundingType]] to fractionDigits.
- intl_object.set_rounding_type(NumberFormat::RoundingType::FractionDigits);
+ intl_object.set_rounding_type(NumberFormatBase::RoundingType::FractionDigits);
}
return {};
@@ -491,7 +496,7 @@ int currency_digits(StringView currency)
}
// 15.1.5 FormatNumericToString ( intlObject, x ), https://tc39.es/ecma402/#sec-formatnumberstring
-FormatResult format_numeric_to_string(NumberFormat& number_format, double number)
+FormatResult format_numeric_to_string(NumberFormatBase& intl_object, double number)
{
// 1. If x < 0 or x is -0𝔽, let isNegative be true; else let isNegative be false.
bool is_negative = (number < 0.0) || Value(number).is_negative_zero();
@@ -504,21 +509,21 @@ FormatResult format_numeric_to_string(NumberFormat& number_format, double number
RawFormatResult result {};
- switch (number_format.rounding_type()) {
+ switch (intl_object.rounding_type()) {
// 3. If intlObject.[[RoundingType]] is significantDigits, then
- case NumberFormat::RoundingType::SignificantDigits:
+ case NumberFormatBase::RoundingType::SignificantDigits:
// a. Let result be ToRawPrecision(x, intlObject.[[MinimumSignificantDigits]], intlObject.[[MaximumSignificantDigits]]).
- result = to_raw_precision(number, number_format.min_significant_digits(), number_format.max_significant_digits());
+ result = to_raw_precision(number, intl_object.min_significant_digits(), intl_object.max_significant_digits());
break;
// 4. Else if intlObject.[[RoundingType]] is fractionDigits, then
- case NumberFormat::RoundingType::FractionDigits:
+ case NumberFormatBase::RoundingType::FractionDigits:
// a. Let result be ToRawFixed(x, intlObject.[[MinimumFractionDigits]], intlObject.[[MaximumFractionDigits]]).
- result = to_raw_fixed(number, number_format.min_fraction_digits(), number_format.max_fraction_digits());
+ result = to_raw_fixed(number, intl_object.min_fraction_digits(), intl_object.max_fraction_digits());
break;
// 5. Else,
- case NumberFormat::RoundingType::CompactRounding:
+ case NumberFormatBase::RoundingType::CompactRounding:
// a. Assert: intlObject.[[RoundingType]] is compactRounding.
// b. Let result be ToRawPrecision(x, 1, 2).
result = to_raw_precision(number, 1, 2);
@@ -545,7 +550,7 @@ FormatResult format_numeric_to_string(NumberFormat& number_format, double number
int digits = result.digits;
// 9. Let minInteger be intlObject.[[MinimumIntegerDigits]].
- int min_integer = number_format.min_integer_digits();
+ int min_integer = intl_object.min_integer_digits();
// 10. If int < minInteger, then
if (digits < min_integer) {
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h
index c19ee700f8..516834510b 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h
+++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h
@@ -16,8 +16,62 @@
namespace JS::Intl {
-class NumberFormat final : public Object {
- JS_OBJECT(NumberFormat, Object);
+class NumberFormatBase : public Object {
+ JS_OBJECT(NumberFormatBase, Object);
+
+public:
+ enum class RoundingType {
+ Invalid,
+ SignificantDigits,
+ FractionDigits,
+ CompactRounding,
+ };
+
+ NumberFormatBase(Object& prototype);
+ virtual ~NumberFormatBase() override = default;
+
+ String const& locale() const { return m_locale; }
+ void set_locale(String locale) { m_locale = move(locale); }
+
+ String const& data_locale() const { return m_data_locale; }
+ void set_data_locale(String data_locale) { m_data_locale = move(data_locale); }
+
+ int min_integer_digits() const { return m_min_integer_digits; }
+ void set_min_integer_digits(int min_integer_digits) { m_min_integer_digits = min_integer_digits; }
+
+ bool has_min_fraction_digits() const { return m_min_fraction_digits.has_value(); }
+ int min_fraction_digits() const { return *m_min_fraction_digits; }
+ void set_min_fraction_digits(int min_fraction_digits) { m_min_fraction_digits = min_fraction_digits; }
+
+ bool has_max_fraction_digits() const { return m_max_fraction_digits.has_value(); }
+ int max_fraction_digits() const { return *m_max_fraction_digits; }
+ void set_max_fraction_digits(int max_fraction_digits) { m_max_fraction_digits = max_fraction_digits; }
+
+ bool has_min_significant_digits() const { return m_min_significant_digits.has_value(); }
+ int min_significant_digits() const { return *m_min_significant_digits; }
+ void set_min_significant_digits(int min_significant_digits) { m_min_significant_digits = min_significant_digits; }
+
+ bool has_max_significant_digits() const { return m_max_significant_digits.has_value(); }
+ int max_significant_digits() const { return *m_max_significant_digits; }
+ void set_max_significant_digits(int max_significant_digits) { m_max_significant_digits = max_significant_digits; }
+
+ RoundingType rounding_type() const { return m_rounding_type; }
+ StringView rounding_type_string() const;
+ void set_rounding_type(RoundingType rounding_type) { m_rounding_type = rounding_type; }
+
+private:
+ String m_locale; // [[Locale]]
+ String m_data_locale; // [[DataLocale]]
+ int m_min_integer_digits { 0 }; // [[MinimumIntegerDigits]]
+ Optional<int> m_min_fraction_digits {}; // [[MinimumFractionDigits]]
+ Optional<int> m_max_fraction_digits {}; // [[MaximumFractionDigits]]
+ Optional<int> m_min_significant_digits {}; // [[MinimumSignificantDigits]]
+ Optional<int> m_max_significant_digits {}; // [[MaximumSignificantDigits]]
+ RoundingType m_rounding_type { RoundingType::Invalid }; // [[RoundingType]]
+};
+
+class NumberFormat final : public NumberFormatBase {
+ JS_OBJECT(NumberFormat, NumberFormatBase);
public:
enum class Style {
@@ -78,12 +132,6 @@ public:
NumberFormat(Object& prototype);
virtual ~NumberFormat() override = default;
- String const& locale() const { return m_locale; }
- void set_locale(String locale) { m_locale = move(locale); }
-
- String const& data_locale() const { return m_data_locale; }
- void set_data_locale(String data_locale) { m_data_locale = move(data_locale); }
-
String const& numbering_system() const { return m_numbering_system; }
void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); }
@@ -115,32 +163,9 @@ public:
StringView unit_display_string() const { return Unicode::style_to_string(*m_unit_display); }
void set_unit_display(StringView unit_display) { m_unit_display = Unicode::style_from_string(unit_display); }
- int min_integer_digits() const { return m_min_integer_digits; }
- void set_min_integer_digits(int min_integer_digits) { m_min_integer_digits = min_integer_digits; }
-
- bool has_min_fraction_digits() const { return m_min_fraction_digits.has_value(); }
- int min_fraction_digits() const { return *m_min_fraction_digits; }
- void set_min_fraction_digits(int min_fraction_digits) { m_min_fraction_digits = min_fraction_digits; }
-
- bool has_max_fraction_digits() const { return m_max_fraction_digits.has_value(); }
- int max_fraction_digits() const { return *m_max_fraction_digits; }
- void set_max_fraction_digits(int max_fraction_digits) { m_max_fraction_digits = max_fraction_digits; }
-
- bool has_min_significant_digits() const { return m_min_significant_digits.has_value(); }
- int min_significant_digits() const { return *m_min_significant_digits; }
- void set_min_significant_digits(int min_significant_digits) { m_min_significant_digits = min_significant_digits; }
-
- bool has_max_significant_digits() const { return m_max_significant_digits.has_value(); }
- int max_significant_digits() const { return *m_max_significant_digits; }
- void set_max_significant_digits(int max_significant_digits) { m_max_significant_digits = max_significant_digits; }
-
bool use_grouping() const { return m_use_grouping; }
void set_use_grouping(bool use_grouping) { m_use_grouping = use_grouping; }
- RoundingType rounding_type() const { return m_rounding_type; }
- StringView rounding_type_string() const;
- void set_rounding_type(RoundingType rounding_type) { m_rounding_type = rounding_type; }
-
Notation notation() const { return m_notation; }
StringView notation_string() const;
void set_notation(StringView notation);
@@ -164,26 +189,20 @@ public:
private:
virtual void visit_edges(Visitor&) override;
- String m_locale; // [[Locale]]
- String m_data_locale; // [[DataLocale]]
- String m_numbering_system; // [[NumberingSystem]]
- Style m_style { Style::Invalid }; // [[Style]]
- Optional<String> m_currency {}; // [[Currency]]
- Optional<CurrencyDisplay> m_currency_display {}; // [[CurrencyDisplay]]
- Optional<CurrencySign> m_currency_sign {}; // [[CurrencySign]]
- Optional<String> m_unit {}; // [[Unit]]
- Optional<Unicode::Style> m_unit_display {}; // [[UnitDisplay]]
- int m_min_integer_digits { 0 }; // [[MinimumIntegerDigits]]
- Optional<int> m_min_fraction_digits {}; // [[MinimumFractionDigits]]
- Optional<int> m_max_fraction_digits {}; // [[MaximumFractionDigits]]
- Optional<int> m_min_significant_digits {}; // [[MinimumSignificantDigits]]
- Optional<int> m_max_significant_digits {}; // [[MaximumSignificantDigits]]
- bool m_use_grouping { false }; // [[UseGrouping]]
- RoundingType m_rounding_type { RoundingType::Invalid }; // [[RoundingType]]
- Notation m_notation { Notation::Invalid }; // [[Notation]]
- Optional<CompactDisplay> m_compact_display {}; // [[CompactDisplay]]
- SignDisplay m_sign_display { SignDisplay::Invalid }; // [[SignDisplay]]
- NativeFunction* m_bound_format { nullptr }; // [[BoundFormat]]
+ String m_locale; // [[Locale]]
+ String m_data_locale; // [[DataLocale]]
+ String m_numbering_system; // [[NumberingSystem]]
+ Style m_style { Style::Invalid }; // [[Style]]
+ Optional<String> m_currency {}; // [[Currency]]
+ Optional<CurrencyDisplay> m_currency_display {}; // [[CurrencyDisplay]]
+ Optional<CurrencySign> m_currency_sign {}; // [[CurrencySign]]
+ Optional<String> m_unit {}; // [[Unit]]
+ Optional<Unicode::Style> m_unit_display {}; // [[UnitDisplay]]
+ bool m_use_grouping { false }; // [[UseGrouping]]
+ Notation m_notation { Notation::Invalid }; // [[Notation]]
+ Optional<CompactDisplay> m_compact_display {}; // [[CompactDisplay]]
+ SignDisplay m_sign_display { SignDisplay::Invalid }; // [[SignDisplay]]
+ NativeFunction* m_bound_format { nullptr }; // [[BoundFormat]]
// Non-standard. Stores the resolved currency display string based on [[Locale]], [[Currency]], and [[CurrencyDisplay]].
Optional<StringView> m_resolved_currency_display;
@@ -201,10 +220,10 @@ struct RawFormatResult : public FormatResult {
int digits { 0 }; // [[IntegerDigitsCount]]
};
-ThrowCompletionOr<void> set_number_format_digit_options(GlobalObject& global_object, NumberFormat& intl_object, Object const& options, int default_min_fraction_digits, int default_max_fraction_digits, NumberFormat::Notation notation);
+ThrowCompletionOr<void> set_number_format_digit_options(GlobalObject& global_object, NumberFormatBase& intl_object, Object const& options, int default_min_fraction_digits, int default_max_fraction_digits, NumberFormat::Notation notation);
ThrowCompletionOr<NumberFormat*> initialize_number_format(GlobalObject& global_object, NumberFormat& number_format, Value locales_value, Value options_value);
int currency_digits(StringView currency);
-FormatResult format_numeric_to_string(NumberFormat& number_format, double number);
+FormatResult format_numeric_to_string(NumberFormatBase& intl_object, double number);
Vector<PatternPartition> partition_number_pattern(NumberFormat& number_format, double number);
Vector<PatternPartition> partition_notation_sub_pattern(NumberFormat& number_format, double number, String formatted_string, int exponent);
String format_numeric(NumberFormat& number_format, double number);