1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:17:44 +00:00

LibJS: Generalize Intl.NumberFormat to operate on Value types

Intl.NumberFormat is meant to format both Number and BigInt types. To
prepare for formatting BigInt types, this generalizes our NumberFormat
implementation to operate on Value instances rather than doubles. All
arithmetic is moved to static helpers that can now be updated with
BigInt semantics.
This commit is contained in:
Timothy Flynn 2022-01-30 10:41:07 -05:00 committed by Linus Groh
parent 4ba4e4c777
commit a0253af8c1
7 changed files with 150 additions and 79 deletions

View file

@ -899,7 +899,7 @@ ThrowCompletionOr<Vector<PatternPartition>> format_date_time_pattern(GlobalObjec
value = floor(value * pow(10, static_cast<int>(*fractional_second_digits) - 3));
// iii. Let fv be FormatNumeric(nf3, v).
auto formatted_value = format_numeric(*number_format3, value);
auto formatted_value = format_numeric(global_object, *number_format3, Value(value));
// iv. Append a new Record { [[Type]]: "fractionalSecond", [[Value]]: fv } as the last element of result.
result.append({ "fractionalSecond"sv, move(formatted_value) });
@ -983,13 +983,13 @@ ThrowCompletionOr<Vector<PatternPartition>> format_date_time_pattern(GlobalObjec
// viii. If f is "numeric", then
case Unicode::CalendarPatternStyle::Numeric:
// 1. Let fv be FormatNumeric(nf, v).
formatted_value = format_numeric(*number_format, value);
formatted_value = format_numeric(global_object, *number_format, Value(value));
break;
// ix. Else if f is "2-digit", then
case Unicode::CalendarPatternStyle::TwoDigit:
// 1. Let fv be FormatNumeric(nf2, v).
formatted_value = format_numeric(*number_format2, value);
formatted_value = format_numeric(global_object, *number_format2, Value(value));
// 2. If the "length" property of fv is greater than 2, let fv be the substring of fv containing the last two characters.
// NOTE: The first length check here isn't enough, but lets us avoid UTF-16 transcoding when the formatted value is ASCII.