1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:47:34 +00:00

LibJS: Return an enum from ApplyUnsignedRoundingMode

After the Intl MV is implemented, returning a copy of the desired value
here may involve copying non-trivial data. Instead, return an enum to
indicate which decision was made.
This commit is contained in:
Timothy Flynn 2022-07-19 12:27:54 -04:00 committed by Linus Groh
parent 20533c2594
commit 99b79766cd
2 changed files with 18 additions and 13 deletions

View file

@ -1198,7 +1198,7 @@ RawFormatResult to_raw_precision(GlobalObject& global_object, Value number, int
Value n; Value n;
// d. If r is r1, then // d. If r is r1, then
if (is_equal(rounded, rounded1)) { if (rounded == RoundingDecision::LowerValue) {
// i. Let n be n1. // i. Let n be n1.
n = number1; n = number1;
@ -1343,7 +1343,7 @@ RawFormatResult to_raw_fixed(GlobalObject& global_object, Value number, int min_
Value n; Value n;
// 5. If r is r1, then // 5. If r is r1, then
if (is_equal(rounded, rounded1)) { if (rounded == RoundingDecision::LowerValue) {
// a. Let n be n1. // a. Let n be n1.
n = number1; n = number1;
@ -1761,11 +1761,11 @@ NumberFormat::UnsignedRoundingMode get_unsigned_rounding_mode(NumberFormat::Roun
} }
// 1.1.20 ApplyUnsignedRoundingMode ( x, r1, r2, unsignedRoundingMode ), https://tc39.es/proposal-intl-numberformat-v3/out/numberformat/proposed.html#sec-applyunsignedroundingmode // 1.1.20 ApplyUnsignedRoundingMode ( x, r1, r2, unsignedRoundingMode ), https://tc39.es/proposal-intl-numberformat-v3/out/numberformat/proposed.html#sec-applyunsignedroundingmode
Value apply_unsigned_rounding_mode(GlobalObject& global_object, Value x, Value r1, Value r2, Optional<NumberFormat::UnsignedRoundingMode> const& unsigned_rounding_mode) RoundingDecision apply_unsigned_rounding_mode(GlobalObject& global_object, Value x, Value r1, Value r2, Optional<NumberFormat::UnsignedRoundingMode> const& unsigned_rounding_mode)
{ {
// 1. If x is equal to r1, return r1. // 1. If x is equal to r1, return r1.
if (is_equal(x, r1)) if (is_equal(x, r1))
return r1; return RoundingDecision::LowerValue;
// FIXME: We skip this assertion due floating point inaccuracies. For example, entering "1.2345" // FIXME: We skip this assertion due floating point inaccuracies. For example, entering "1.2345"
// in the JS REPL results in "1.234499999999999", and may cause this assertion to fail. // in the JS REPL results in "1.234499999999999", and may cause this assertion to fail.
@ -1780,11 +1780,11 @@ Value apply_unsigned_rounding_mode(GlobalObject& global_object, Value x, Value r
// 4. If unsignedRoundingMode is zero, return r1. // 4. If unsignedRoundingMode is zero, return r1.
if (unsigned_rounding_mode == NumberFormat::UnsignedRoundingMode::Zero) if (unsigned_rounding_mode == NumberFormat::UnsignedRoundingMode::Zero)
return r1; return RoundingDecision::LowerValue;
// 5. If unsignedRoundingMode is infinity, return r2. // 5. If unsignedRoundingMode is infinity, return r2.
if (unsigned_rounding_mode == NumberFormat::UnsignedRoundingMode::Infinity) if (unsigned_rounding_mode == NumberFormat::UnsignedRoundingMode::Infinity)
return r2; return RoundingDecision::HigherValue;
// 6. Let d1 be x r1. // 6. Let d1 be x r1.
auto d1 = subtract(global_object, x, r1); auto d1 = subtract(global_object, x, r1);
@ -1794,22 +1794,22 @@ Value apply_unsigned_rounding_mode(GlobalObject& global_object, Value x, Value r
// 8. If d1 < d2, return r1. // 8. If d1 < d2, return r1.
if (is_less_than(d1, d2)) if (is_less_than(d1, d2))
return r1; return RoundingDecision::LowerValue;
// 9. If d2 < d1, return r2. // 9. If d2 < d1, return r2.
if (is_less_than(d2, d1)) if (is_less_than(d2, d1))
return r2; return RoundingDecision::HigherValue;
// 10. Assert: d1 is equal to d2. // 10. Assert: d1 is equal to d2.
VERIFY(is_equal(d1, d2)); VERIFY(is_equal(d1, d2));
// 11. If unsignedRoundingMode is half-zero, return r1. // 11. If unsignedRoundingMode is half-zero, return r1.
if (unsigned_rounding_mode == NumberFormat::UnsignedRoundingMode::HalfZero) if (unsigned_rounding_mode == NumberFormat::UnsignedRoundingMode::HalfZero)
return r1; return RoundingDecision::LowerValue;
// 12. If unsignedRoundingMode is half-infinity, return r2. // 12. If unsignedRoundingMode is half-infinity, return r2.
if (unsigned_rounding_mode == NumberFormat::UnsignedRoundingMode::HalfInfinity) if (unsigned_rounding_mode == NumberFormat::UnsignedRoundingMode::HalfInfinity)
return r2; return RoundingDecision::HigherValue;
// 13. Assert: unsignedRoundingMode is half-even. // 13. Assert: unsignedRoundingMode is half-even.
VERIFY(unsigned_rounding_mode == NumberFormat::UnsignedRoundingMode::HalfEven); VERIFY(unsigned_rounding_mode == NumberFormat::UnsignedRoundingMode::HalfEven);
@ -1820,10 +1820,10 @@ Value apply_unsigned_rounding_mode(GlobalObject& global_object, Value x, Value r
// 15. If cardinality is 0, return r1. // 15. If cardinality is 0, return r1.
if (modulo_is_zero(cardinality, 2)) if (modulo_is_zero(cardinality, 2))
return r1; return RoundingDecision::LowerValue;
// 16. Return r2. // 16. Return r2.
return r2; return RoundingDecision::HigherValue;
} }
} }

View file

@ -266,6 +266,11 @@ struct RawFormatResult : public FormatResult {
int rounding_magnitude { 0 }; // [[RoundingMagnitude]] int rounding_magnitude { 0 }; // [[RoundingMagnitude]]
}; };
enum class RoundingDecision {
LowerValue,
HigherValue,
};
int currency_digits(StringView currency); int currency_digits(StringView currency);
FormatResult format_numeric_to_string(GlobalObject& global_object, NumberFormatBase const& intl_object, Value number); FormatResult format_numeric_to_string(GlobalObject& global_object, NumberFormatBase const& intl_object, Value number);
Vector<PatternPartition> partition_number_pattern(GlobalObject& global_object, NumberFormat& number_format, Value number); Vector<PatternPartition> partition_number_pattern(GlobalObject& global_object, NumberFormat& number_format, Value number);
@ -279,6 +284,6 @@ Optional<StringView> get_notation_sub_pattern(NumberFormat& number_format, int e
int compute_exponent(GlobalObject& global_object, NumberFormat& number_format, Value number); int compute_exponent(GlobalObject& global_object, NumberFormat& number_format, Value number);
int compute_exponent_for_magnitude(NumberFormat& number_format, int magnitude); int compute_exponent_for_magnitude(NumberFormat& number_format, int magnitude);
NumberFormat::UnsignedRoundingMode get_unsigned_rounding_mode(NumberFormat::RoundingMode rounding_mode, bool is_negative); NumberFormat::UnsignedRoundingMode get_unsigned_rounding_mode(NumberFormat::RoundingMode rounding_mode, bool is_negative);
Value apply_unsigned_rounding_mode(GlobalObject& global_object, Value x, Value r1, Value r2, Optional<NumberFormat::UnsignedRoundingMode> const& unsigned_rounding_mode); RoundingDecision apply_unsigned_rounding_mode(GlobalObject& global_object, Value x, Value r1, Value r2, Optional<NumberFormat::UnsignedRoundingMode> const& unsigned_rounding_mode);
} }