LibJS+LibUnicode: Update the Intl.DateTimeFormat constructor spec steps

This constructor has undergone a handful of editorial changes that we
fell behind on. But we weren't able to take the updates until now due to
a spec bug in those updates. See:
3f029b0

The result is that we can remove the inheritance of Intl::DateTimeFormat
from Unicode::DateTimeFormat; the former now contains the latter as an
internal slot.
This commit is contained in:
Timothy Flynn 2024-08-15 15:06:54 -04:00 committed by Tim Flynn
commit c7dd4afd9c
Notes: github-actions[bot] 2024-08-15 21:22:00 +00:00
5 changed files with 83 additions and 114 deletions

View file

@ -738,9 +738,9 @@ ErrorOr<void> print_intl_date_time_format(JS::PrintContext& print_context, JS::I
TRY(print_value(print_context, JS::PrimitiveString::create(date_time_format.vm(), date_time_format.calendar()), seen_objects)); TRY(print_value(print_context, JS::PrimitiveString::create(date_time_format.vm(), date_time_format.calendar()), seen_objects));
TRY(js_out(print_context, "\n numberingSystem: ")); TRY(js_out(print_context, "\n numberingSystem: "));
TRY(print_value(print_context, JS::PrimitiveString::create(date_time_format.vm(), date_time_format.numbering_system()), seen_objects)); TRY(print_value(print_context, JS::PrimitiveString::create(date_time_format.vm(), date_time_format.numbering_system()), seen_objects));
if (date_time_format.hour_cycle.has_value()) { if (auto const& hour_cycle = date_time_format.date_time_format().hour_cycle; hour_cycle.has_value()) {
TRY(js_out(print_context, "\n hourCycle: ")); TRY(js_out(print_context, "\n hourCycle: "));
TRY(print_value(print_context, JS::PrimitiveString::create(date_time_format.vm(), Unicode::hour_cycle_to_string(*date_time_format.hour_cycle)), seen_objects)); TRY(print_value(print_context, JS::PrimitiveString::create(date_time_format.vm(), Unicode::hour_cycle_to_string(*hour_cycle)), seen_objects));
} }
TRY(js_out(print_context, "\n timeZone: ")); TRY(js_out(print_context, "\n timeZone: "));
TRY(print_value(print_context, JS::PrimitiveString::create(date_time_format.vm(), date_time_format.time_zone()), seen_objects)); TRY(print_value(print_context, JS::PrimitiveString::create(date_time_format.vm(), date_time_format.time_zone()), seen_objects));
@ -753,7 +753,7 @@ ErrorOr<void> print_intl_date_time_format(JS::PrintContext& print_context, JS::I
TRY(print_value(print_context, JS::PrimitiveString::create(date_time_format.vm(), date_time_format.time_style_string()), seen_objects)); TRY(print_value(print_context, JS::PrimitiveString::create(date_time_format.vm(), date_time_format.time_style_string()), seen_objects));
} }
auto result = JS::Intl::for_each_calendar_field(date_time_format.vm(), date_time_format, [&](auto& option, auto const& property, auto const&) -> JS::ThrowCompletionOr<void> { auto result = JS::Intl::for_each_calendar_field(date_time_format.vm(), date_time_format.date_time_format(), [&](auto& option, auto const& property, auto const&) -> JS::ThrowCompletionOr<void> {
using ValueType = typename RemoveReference<decltype(option)>::ValueType; using ValueType = typename RemoveReference<decltype(option)>::ValueType;
if (!option.has_value()) if (!option.has_value())

View file

@ -18,9 +18,7 @@
namespace JS::Intl { namespace JS::Intl {
class DateTimeFormat final class DateTimeFormat final : public Object {
: public Object
, public Unicode::CalendarPattern {
JS_OBJECT(DateTimeFormat, Object); JS_OBJECT(DateTimeFormat, Object);
JS_DECLARE_ALLOCATOR(DateTimeFormat); JS_DECLARE_ALLOCATOR(DateTimeFormat);
@ -58,6 +56,9 @@ public:
StringView time_style_string() const { return Unicode::date_time_style_to_string(*m_time_style); } StringView time_style_string() const { return Unicode::date_time_style_to_string(*m_time_style); }
void set_time_style(StringView style) { m_time_style = Unicode::date_time_style_from_string(style); } void set_time_style(StringView style) { m_time_style = Unicode::date_time_style_from_string(style); }
Unicode::CalendarPattern& date_time_format() { return m_date_time_format; }
void set_date_time_format(Unicode::CalendarPattern date_time_format) { m_date_time_format = move(date_time_format); }
NativeFunction* bound_format() const { return m_bound_format; } NativeFunction* bound_format() const { return m_bound_format; }
void set_bound_format(NativeFunction* bound_format) { m_bound_format = bound_format; } void set_bound_format(NativeFunction* bound_format) { m_bound_format = bound_format; }
@ -75,6 +76,7 @@ private:
String m_time_zone; // [[TimeZone]] String m_time_zone; // [[TimeZone]]
Optional<Unicode::DateTimeStyle> m_date_style; // [[DateStyle]] Optional<Unicode::DateTimeStyle> m_date_style; // [[DateStyle]]
Optional<Unicode::DateTimeStyle> m_time_style; // [[TimeStyle]] Optional<Unicode::DateTimeStyle> m_time_style; // [[TimeStyle]]
Unicode::CalendarPattern m_date_time_format; // [[DateTimeFormat]]
GCPtr<NativeFunction> m_bound_format; // [[BoundFormat]] GCPtr<NativeFunction> m_bound_format; // [[BoundFormat]]
// Non-standard. Stores the ICU date-time formatter for the Intl object's formatting options. // Non-standard. Stores the ICU date-time formatter for the Intl object's formatting options.

View file

@ -84,7 +84,7 @@ JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatConstructor::supported_locales_of)
// 11.1.2 CreateDateTimeFormat ( newTarget, locales, options, required, defaults ), https://tc39.es/ecma402/#sec-createdatetimeformat // 11.1.2 CreateDateTimeFormat ( newTarget, locales, options, required, defaults ), https://tc39.es/ecma402/#sec-createdatetimeformat
ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm, FunctionObject& new_target, Value locales_value, Value options_value, OptionRequired required, OptionDefaults defaults) ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm, FunctionObject& new_target, Value locales_value, Value options_value, OptionRequired required, OptionDefaults defaults)
{ {
// 1. Let dateTimeFormat be ? OrdinaryCreateFromConstructor(newTarget, "%DateTimeFormat.prototype%", « [[InitializedDateTimeFormat]], [[Locale]], [[Calendar]], [[NumberingSystem]], [[TimeZone]], [[Weekday]], [[Era]], [[Year]], [[Month]], [[Day]], [[DayPeriod]], [[Hour]], [[Minute]], [[Second]], [[FractionalSecondDigits]], [[TimeZoneName]], [[HourCycle]], [[DateStyle]], [[TimeStyle]], [[Pattern]], [[RangePatterns]], [[BoundFormat]] »). // 1. Let dateTimeFormat be ? OrdinaryCreateFromConstructor(newTarget, "%Intl.DateTimeFormat.prototype%", « [[InitializedDateTimeFormat]], [[Locale]], [[Calendar]], [[NumberingSystem]], [[TimeZone]], [[HourCycle]], [[DateStyle]], [[TimeStyle]], [[DateTimeFormat]], [[BoundFormat]] »).
auto date_time_format = TRY(ordinary_create_from_constructor<DateTimeFormat>(vm, new_target, &Intrinsics::intl_date_time_format_prototype)); auto date_time_format = TRY(ordinary_create_from_constructor<DateTimeFormat>(vm, new_target, &Intrinsics::intl_date_time_format_prototype));
// 2. Let requestedLocales be ? CanonicalizeLocaleList(locales). // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
@ -143,37 +143,35 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
// 16. Set opt.[[hc]] to hourCycle. // 16. Set opt.[[hc]] to hourCycle.
opt.hc = locale_key_from_value(hour_cycle); opt.hc = locale_key_from_value(hour_cycle);
// 17. Let localeData be %DateTimeFormat%.[[LocaleData]]. // 17. Let r be ResolveLocale(%Intl.DateTimeFormat%.[[AvailableLocales]], requestedLocales, opt, %Intl.DateTimeFormat%.[[RelevantExtensionKeys]], %Intl.DateTimeFormat%.[[LocaleData]]).
// 18. Let r be ResolveLocale(%DateTimeFormat%.[[AvailableLocales]], requestedLocales, opt, %DateTimeFormat%.[[RelevantExtensionKeys]], localeData).
auto result = resolve_locale(requested_locales, opt, DateTimeFormat::relevant_extension_keys()); auto result = resolve_locale(requested_locales, opt, DateTimeFormat::relevant_extension_keys());
// 19. Set dateTimeFormat.[[Locale]] to r.[[locale]]. // 18. Set dateTimeFormat.[[Locale]] to r.[[Locale]].
date_time_format->set_locale(move(result.locale)); date_time_format->set_locale(move(result.locale));
// 20. Let resolvedCalendar be r.[[ca]]. // 19. Let resolvedCalendar be r.[[ca]].
// 21. Set dateTimeFormat.[[Calendar]] to resolvedCalendar. // 20. Set dateTimeFormat.[[Calendar]] to resolvedCalendar.
if (auto* resolved_calendar = result.ca.get_pointer<String>()) if (auto* resolved_calendar = result.ca.get_pointer<String>())
date_time_format->set_calendar(move(*resolved_calendar)); date_time_format->set_calendar(move(*resolved_calendar));
// 22. Set dateTimeFormat.[[NumberingSystem]] to r.[[nu]]. // 21. Set dateTimeFormat.[[NumberingSystem]] to r.[[nu]].
if (auto* resolved_numbering_system = result.nu.get_pointer<String>()) if (auto* resolved_numbering_system = result.nu.get_pointer<String>())
date_time_format->set_numbering_system(move(*resolved_numbering_system)); date_time_format->set_numbering_system(move(*resolved_numbering_system));
// 23. Let dataLocale be r.[[dataLocale]]. // 22. Let resolvedLocaleData be r.[[LocaleData]].
// 24. Let dataLocaleData be localeData.[[<dataLocale>]].
Optional<Unicode::HourCycle> hour_cycle_value; Optional<Unicode::HourCycle> hour_cycle_value;
Optional<bool> hour12_value; Optional<bool> hour12_value;
// 25. If hour12 is true, then // 23. If hour12 is true, then
// a. Let hc be dataLocaleData.[[hourCycle12]]. // a. Let hc be resolvedLocaleData.[[hourCycle12]].
// 26. Else if hour12 is false, then // 24. Else if hour12 is false, then
// a. Let hc be dataLocaleData.[[hourCycle24]]. // a. Let hc be resolvedLocaleData.[[hourCycle24]].
if (hour12.is_boolean()) { if (hour12.is_boolean()) {
// NOTE: We let LibUnicode figure out the appropriate hour cycle. // NOTE: We let LibUnicode figure out the appropriate hour cycle.
hour12_value = hour12.as_bool(); hour12_value = hour12.as_bool();
} }
// 27. Else, // 25. Else,
else { else {
// a. Assert: hour12 is undefined. // a. Assert: hour12 is undefined.
VERIFY(hour12.is_undefined()); VERIFY(hour12.is_undefined());
@ -182,30 +180,27 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
if (auto* resolved_hour_cycle = result.hc.get_pointer<String>()) if (auto* resolved_hour_cycle = result.hc.get_pointer<String>())
hour_cycle_value = Unicode::hour_cycle_from_string(*resolved_hour_cycle); hour_cycle_value = Unicode::hour_cycle_from_string(*resolved_hour_cycle);
// c. If hc is null, set hc to dataLocaleData.[[hourCycle]]. // c. If hc is null, set hc to resolvedLocaleData.[[hourCycle]].
if (!hour_cycle_value.has_value()) if (!hour_cycle_value.has_value())
hour_cycle_value = Unicode::default_hour_cycle(date_time_format->locale()); hour_cycle_value = Unicode::default_hour_cycle(date_time_format->locale());
} }
// 28. Set dateTimeFormat.[[HourCycle]] to hc. // 26. Let timeZone be ? Get(options, "timeZone").
date_time_format->hour_cycle = hour_cycle_value;
// 29. Let timeZone be ? Get(options, "timeZone").
auto time_zone_value = TRY(options->get(vm.names.timeZone)); auto time_zone_value = TRY(options->get(vm.names.timeZone));
String time_zone; String time_zone;
// 30. If timeZone is undefined, then // 27. If timeZone is undefined, then
if (time_zone_value.is_undefined()) { if (time_zone_value.is_undefined()) {
// a. Set timeZone to DefaultTimeZone(). // a. Set timeZone to SystemTimeZoneIdentifier().
time_zone = system_time_zone_identifier(); time_zone = system_time_zone_identifier();
} }
// 31. Else, // 28. Else,
else { else {
// a. Set timeZone to ? ToString(timeZone). // a. Set timeZone to ? ToString(timeZone).
time_zone = TRY(time_zone_value.to_string(vm)); time_zone = TRY(time_zone_value.to_string(vm));
} }
// 32. If IsTimeZoneOffsetString(timeZone) is true, then // 29. If IsTimeZoneOffsetString(timeZone) is true, then
bool is_time_zone_offset_string = JS::is_time_zone_offset_string(time_zone); bool is_time_zone_offset_string = JS::is_time_zone_offset_string(time_zone);
if (is_time_zone_offset_string) { if (is_time_zone_offset_string) {
@ -222,7 +217,7 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
// d. Let offsetNanoseconds be ParseTimeZoneOffsetString(timeZone). // d. Let offsetNanoseconds be ParseTimeZoneOffsetString(timeZone).
auto offset_nanoseconds = parse_time_zone_offset_string(time_zone); auto offset_nanoseconds = parse_time_zone_offset_string(time_zone);
// e. Let offsetMinutes be offsetNanoseconds / (6 × 10^10). // e. Let offsetMinutes be offsetNanoseconds / (6 × 10**10).
auto offset_minutes = offset_nanoseconds / 60'000'000'000; auto offset_minutes = offset_nanoseconds / 60'000'000'000;
// f. Assert: offsetMinutes is an integer. // f. Assert: offsetMinutes is an integer.
@ -231,7 +226,7 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
// g. Set timeZone to FormatOffsetTimeZoneIdentifier(offsetMinutes). // g. Set timeZone to FormatOffsetTimeZoneIdentifier(offsetMinutes).
time_zone = format_offset_time_zone_identifier(offset_minutes); time_zone = format_offset_time_zone_identifier(offset_minutes);
} }
// 33. Else // 30. Else,
else { else {
// a. Let timeZoneIdentifierRecord be GetAvailableNamedTimeZoneIdentifier(timeZone). // a. Let timeZoneIdentifierRecord be GetAvailableNamedTimeZoneIdentifier(timeZone).
auto time_zone_identifier_record = get_available_named_time_zone_identifier(time_zone); auto time_zone_identifier_record = get_available_named_time_zone_identifier(time_zone);
@ -244,29 +239,29 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
time_zone = time_zone_identifier_record->primary_identifier; time_zone = time_zone_identifier_record->primary_identifier;
} }
// 35. Set dateTimeFormat.[[TimeZone]] to timeZone. // 31. Set dateTimeFormat.[[TimeZone]] to timeZone.
date_time_format->set_time_zone(time_zone); date_time_format->set_time_zone(time_zone);
// NOTE: ICU requires time zone offset strings to be of the form "GMT+00:00" // NOTE: ICU requires time zone offset strings to be of the form "GMT+00:00"
if (is_time_zone_offset_string) if (is_time_zone_offset_string)
time_zone = MUST(String::formatted("GMT{}", time_zone)); time_zone = MUST(String::formatted("GMT{}", time_zone));
// 36. Let formatOptions be a new Record. // 32. Let formatOptions be a new Record.
Unicode::CalendarPattern format_options {}; Unicode::CalendarPattern format_options {};
// 37. Set formatOptions.[[hourCycle]] to hc. // 33. Set formatOptions.[[hourCycle]] to hc.
format_options.hour_cycle = hour_cycle_value; format_options.hour_cycle = hour_cycle_value;
format_options.hour12 = hour12_value; format_options.hour12 = hour12_value;
// 38. Let hasExplicitFormatComponents be false. // 34. Let hasExplicitFormatComponents be false.
// NOTE: Instead of using a boolean, we track any explicitly provided component name for nicer exception messages. // NOTE: Instead of using a boolean, we track any explicitly provided component name for nicer exception messages.
PropertyKey const* explicit_format_component = nullptr; PropertyKey const* explicit_format_component = nullptr;
// 39. For each row of Table 6, except the header row, in table order, do // 35. For each row of Table 16, except the header row, in table order, do
TRY(for_each_calendar_field(vm, format_options, [&](auto& option, PropertyKey const& property, auto const& values) -> ThrowCompletionOr<void> { TRY(for_each_calendar_field(vm, format_options, [&](auto& option, PropertyKey const& property, auto const& values) -> ThrowCompletionOr<void> {
using ValueType = typename RemoveReference<decltype(option)>::ValueType; using ValueType = typename RemoveReference<decltype(option)>::ValueType;
// a. Let prop be the name given in the Property column of the row. // a. Let prop be the name given in the Property column of the current row.
// b. If prop is "fractionalSecondDigits", then // b. If prop is "fractionalSecondDigits", then
if constexpr (IsIntegral<ValueType>) { if constexpr (IsIntegral<ValueType>) {
@ -284,7 +279,7 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
} }
// c. Else, // c. Else,
else { else {
// i. Let values be a List whose elements are the strings given in the Values column of the row. // i. Let values be a List whose elements are the strings given in the Values column of the current row.
// ii. Let value be ? GetOption(options, prop, string, values, undefined). // ii. Let value be ? GetOption(options, prop, string, values, undefined).
auto value = TRY(get_option(vm, *options, property, OptionType::String, values, Empty {})); auto value = TRY(get_option(vm, *options, property, OptionType::String, values, Empty {}));
@ -301,24 +296,26 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
return {}; return {};
})); }));
// 40. Let matcher be ? GetOption(options, "formatMatcher", string, « "basic", "best fit" », "best fit"). // 36. Let formatMatcher be ? GetOption(options, "formatMatcher", string, « "basic", "best fit" », "best fit").
matcher = TRY(get_option(vm, *options, vm.names.formatMatcher, OptionType::String, AK::Array { "basic"sv, "best fit"sv }, "best fit"sv)); [[maybe_unused]] auto format_matcher = TRY(get_option(vm, *options, vm.names.formatMatcher, OptionType::String, AK::Array { "basic"sv, "best fit"sv }, "best fit"sv));
// 41. Let dateStyle be ? GetOption(options, "dateStyle", string, « "full", "long", "medium", "short" », undefined). // 37. Let dateStyle be ? GetOption(options, "dateStyle", string, « "full", "long", "medium", "short" », undefined).
auto date_style = TRY(get_option(vm, *options, vm.names.dateStyle, OptionType::String, AK::Array { "full"sv, "long"sv, "medium"sv, "short"sv }, Empty {})); auto date_style = TRY(get_option(vm, *options, vm.names.dateStyle, OptionType::String, AK::Array { "full"sv, "long"sv, "medium"sv, "short"sv }, Empty {}));
// 42. Set dateTimeFormat.[[DateStyle]] to dateStyle. // 38. Set dateTimeFormat.[[DateStyle]] to dateStyle.
if (!date_style.is_undefined()) if (!date_style.is_undefined())
date_time_format->set_date_style(date_style.as_string().utf8_string_view()); date_time_format->set_date_style(date_style.as_string().utf8_string_view());
// 43. Let timeStyle be ? GetOption(options, "timeStyle", string, « "full", "long", "medium", "short" », undefined). // 39. Let timeStyle be ? GetOption(options, "timeStyle", string, « "full", "long", "medium", "short" », undefined).
auto time_style = TRY(get_option(vm, *options, vm.names.timeStyle, OptionType::String, AK::Array { "full"sv, "long"sv, "medium"sv, "short"sv }, Empty {})); auto time_style = TRY(get_option(vm, *options, vm.names.timeStyle, OptionType::String, AK::Array { "full"sv, "long"sv, "medium"sv, "short"sv }, Empty {}));
// 44. Set dateTimeFormat.[[TimeStyle]] to timeStyle. // 40. Set dateTimeFormat.[[TimeStyle]] to timeStyle.
if (!time_style.is_undefined()) if (!time_style.is_undefined())
date_time_format->set_time_style(time_style.as_string().utf8_string_view()); date_time_format->set_time_style(time_style.as_string().utf8_string_view());
// 45. If dateStyle is not undefined or timeStyle is not undefined, then OwnPtr<Unicode::DateTimeFormat> formatter;
// 41. If dateStyle is not undefined or timeStyle is not undefined, then
if (date_time_format->has_date_style() || date_time_format->has_time_style()) { if (date_time_format->has_date_style() || date_time_format->has_time_style()) {
// a. If hasExplicitFormatComponents is true, then // a. If hasExplicitFormatComponents is true, then
if (explicit_format_component != nullptr) { if (explicit_format_component != nullptr) {
@ -338,18 +335,17 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
return vm.throw_completion<TypeError>(ErrorType::IntlInvalidDateTimeFormatOption, "dateStyle"sv, "time"sv); return vm.throw_completion<TypeError>(ErrorType::IntlInvalidDateTimeFormatOption, "dateStyle"sv, "time"sv);
} }
// d. Let styles be dataLocaleData.[[styles]].[[<resolvedCalendar>]]. // d. Let styles be resolvedLocaleData.[[styles]].[[<resolvedCalendar>]].
// e. Let bestFormat be DateTimeStyleFormat(dateStyle, timeStyle, styles). // e. Let bestFormat be DateTimeStyleFormat(dateStyle, timeStyle, styles).
auto formatter = Unicode::DateTimeFormat::create_for_date_and_time_style( formatter = Unicode::DateTimeFormat::create_for_date_and_time_style(
date_time_format->locale(), date_time_format->locale(),
time_zone, time_zone,
format_options.hour_cycle, format_options.hour_cycle,
format_options.hour12, format_options.hour12,
date_time_format->date_style(), date_time_format->date_style(),
date_time_format->time_style()); date_time_format->time_style());
date_time_format->set_formatter(move(formatter));
} }
// 46. Else, // 42. Else,
else { else {
// a. Let needDefaults be true. // a. Let needDefaults be true.
bool needs_defaults = true; bool needs_defaults = true;
@ -359,7 +355,7 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
// i. For each property name prop of « "weekday", "year", "month", "day" », do // i. For each property name prop of « "weekday", "year", "month", "day" », do
auto check_property_value = [&](auto const& value) { auto check_property_value = [&](auto const& value) {
// 1. Let value be formatOptions.[[<prop>]]. // 1. Let value be formatOptions.[[<prop>]].
// 2. If value is not undefined, let needDefaults be false. // 2. If value is not undefined, set needDefaults to false.
if (value.has_value()) if (value.has_value())
needs_defaults = false; needs_defaults = false;
}; };
@ -375,7 +371,7 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
// i. For each property name prop of « "dayPeriod", "hour", "minute", "second", "fractionalSecondDigits" », do // i. For each property name prop of « "dayPeriod", "hour", "minute", "second", "fractionalSecondDigits" », do
auto check_property_value = [&](auto const& value) { auto check_property_value = [&](auto const& value) {
// 1. Let value be formatOptions.[[<prop>]]. // 1. Let value be formatOptions.[[<prop>]].
// 2. If value is not undefined, let needDefaults be false. // 2. If value is not undefined, set needDefaults to false.
if (value.has_value()) if (value.has_value())
needs_defaults = false; needs_defaults = false;
}; };
@ -413,45 +409,28 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
set_property_value(format_options.second); set_property_value(format_options.second);
} }
// f. Let formats be dataLocaleData.[[formats]].[[<resolvedCalendar>]]. // f. Let formats be resolvedLocaleData.[[formats]].[[<resolvedCalendar>]].
// g. If matcher is "basic", then // g. If formatMatcher is "basic", then
// i. Let bestFormat be BasicFormatMatcher(formatOptions, formats). // i. Let bestFormat be BasicFormatMatcher(formatOptions, formats).
// h. Else, // h. Else,
// i. Let bestFormat be BestFitFormatMatcher(formatOptions, formats). // i. Let bestFormat be BestFitFormatMatcher(formatOptions, formats).
auto formatter = Unicode::DateTimeFormat::create_for_pattern_options( formatter = Unicode::DateTimeFormat::create_for_pattern_options(
date_time_format->locale(), date_time_format->locale(),
time_zone, time_zone,
format_options); format_options);
date_time_format->set_formatter(move(formatter));
} }
// 47. For each row in Table 6, except the header row, in table order, do // 43. Set dateTimeFormat.[[DateTimeFormat]] to bestFormat.
date_time_format->for_each_calendar_field_zipped_with(date_time_format->formatter().chosen_pattern(), [&](auto& date_time_format_field, auto const& best_format_field) { date_time_format->set_date_time_format(formatter->chosen_pattern());
// a. Let prop be the name given in the Property column of the row.
// b. If bestFormat has a field [[<prop>]], then
if (best_format_field.has_value()) {
// i. Let p be bestFormat.[[<prop>]].
// ii. Set dateTimeFormat's internal slot whose name is the Internal Slot column of the row to p.
date_time_format_field = best_format_field;
}
});
// 48. If dateTimeFormat.[[Hour]] is undefined, then // 44. If bestFormat has a field [[hour]], then
if (!date_time_format->hour.has_value()) { // a. Set dateTimeFormat.[[HourCycle]] to hc.
// a. Set dateTimeFormat.[[HourCycle]] to undefined. // NOTE: The [[HourCycle]] is stored and accessed from [[DateTimeFormat]].
date_time_format->hour_cycle.clear();
}
// 49. If dateTimeFormat.[[HourCycle]] is "h11" or "h12", then // Non-standard, create an ICU number formatter for this Intl object.
// a. Let pattern be bestFormat.[[pattern12]]. date_time_format->set_formatter(formatter.release_nonnull());
// b. Let rangePatterns be bestFormat.[[rangePatterns12]].
// 50. Else,
// a. Let pattern be bestFormat.[[pattern]].
// b. Let rangePatterns be bestFormat.[[rangePatterns]].
// 51. Set dateTimeFormat.[[Pattern]] to pattern.
// 52. Set dateTimeFormat.[[RangePatterns]] to rangePatterns.
// 53. Return dateTimeFormat. // 45. Return dateTimeFormat.
return date_time_format; return date_time_format;
} }

View file

@ -160,29 +160,34 @@ JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatPrototype::resolved_options)
// 4. Let options be OrdinaryObjectCreate(%Object.prototype%). // 4. Let options be OrdinaryObjectCreate(%Object.prototype%).
auto options = Object::create(realm, realm.intrinsics().object_prototype()); auto options = Object::create(realm, realm.intrinsics().object_prototype());
// 5. For each row of Table 5, except the header row, in table order, do // 5. For each row of Table 15, except the header row, in table order, do
// a. Let p be the Property value of the current row. // a. Let p be the Property value of the current row.
// b. If p is "hour12", then // b. If there is an Internal Slot value in the current row, then
// i. Let hc be dtf.[[HourCycle]].
// ii. If hc is "h11" or "h12", let v be true.
// iii. Else if, hc is "h23" or "h24", let v be false.
// iv. Else, let v be undefined.
// c. Else,
// i. Let v be the value of dtf's internal slot whose name is the Internal Slot value of the current row. // i. Let v be the value of dtf's internal slot whose name is the Internal Slot value of the current row.
// d. If the Internal Slot value of the current row is an Internal Slot value in Table 6, then // c. Else,
// i. If dtf.[[DateStyle]] is not undefined or dtf.[[TimeStyle]] is not undefined, then // i. Let format be dtf.[[DateTimeFormat]].
// ii. If format has a field [[<p>]] and dtf.[[DateStyle]] is undefined and dtf.[[TimeStyle]] is undefined, then
// 1. Let v be format.[[<p>]].
// iii. Else,
// 1. Let v be undefined. // 1. Let v be undefined.
// e. If v is not undefined, then // d. If v is not undefined, then
// i. Perform ! CreateDataPropertyOrThrow(options, p, v). // i. If there is a Conversion value in the current row, then
// 1. Let conversion be the Conversion value of the current row.
// 2. If conversion is hour12, then
// a. If v is "h11" or "h12", set v to true. Otherwise, set v to false.
// 3. Else,
// a. Assert: conversion is number.
// b. Set v to 𝔽(v).
// ii. Perform ! CreateDataPropertyOrThrow(options, p, v).
MUST(options->create_data_property_or_throw(vm.names.locale, PrimitiveString::create(vm, date_time_format->locale()))); MUST(options->create_data_property_or_throw(vm.names.locale, PrimitiveString::create(vm, date_time_format->locale())));
MUST(options->create_data_property_or_throw(vm.names.calendar, PrimitiveString::create(vm, date_time_format->calendar()))); MUST(options->create_data_property_or_throw(vm.names.calendar, PrimitiveString::create(vm, date_time_format->calendar())));
MUST(options->create_data_property_or_throw(vm.names.numberingSystem, PrimitiveString::create(vm, date_time_format->numbering_system()))); MUST(options->create_data_property_or_throw(vm.names.numberingSystem, PrimitiveString::create(vm, date_time_format->numbering_system())));
MUST(options->create_data_property_or_throw(vm.names.timeZone, PrimitiveString::create(vm, date_time_format->time_zone()))); MUST(options->create_data_property_or_throw(vm.names.timeZone, PrimitiveString::create(vm, date_time_format->time_zone())));
if (date_time_format->hour_cycle.has_value()) { if (auto const hour_cycle = date_time_format->date_time_format().hour_cycle; hour_cycle.has_value()) {
MUST(options->create_data_property_or_throw(vm.names.hourCycle, PrimitiveString::create(vm, Unicode::hour_cycle_to_string(*date_time_format->hour_cycle)))); MUST(options->create_data_property_or_throw(vm.names.hourCycle, PrimitiveString::create(vm, Unicode::hour_cycle_to_string(*hour_cycle))));
switch (*date_time_format->hour_cycle) { switch (*hour_cycle) {
case Unicode::HourCycle::H11: case Unicode::HourCycle::H11:
case Unicode::HourCycle::H12: case Unicode::HourCycle::H12:
MUST(options->create_data_property_or_throw(vm.names.hour12, Value(true))); MUST(options->create_data_property_or_throw(vm.names.hour12, Value(true)));
@ -195,7 +200,7 @@ JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatPrototype::resolved_options)
} }
if (!date_time_format->has_date_style() && !date_time_format->has_time_style()) { if (!date_time_format->has_date_style() && !date_time_format->has_time_style()) {
MUST(for_each_calendar_field(vm, date_time_format, [&](auto& option, auto const& property, auto const&) -> ThrowCompletionOr<void> { MUST(for_each_calendar_field(vm, date_time_format->date_time_format(), [&](auto& option, auto const& property, auto const&) -> ThrowCompletionOr<void> {
using ValueType = typename RemoveReference<decltype(option)>::ValueType; using ValueType = typename RemoveReference<decltype(option)>::ValueType;
if (!option.has_value()) if (!option.has_value())

View file

@ -63,23 +63,6 @@ struct CalendarPattern {
static CalendarPattern create_from_pattern(StringView); static CalendarPattern create_from_pattern(StringView);
String to_pattern() const; String to_pattern() const;
template<typename Callback>
void for_each_calendar_field_zipped_with(CalendarPattern const& other, Callback&& callback)
{
callback(hour_cycle, other.hour_cycle);
callback(era, other.era);
callback(year, other.year);
callback(month, other.month);
callback(weekday, other.weekday);
callback(day, other.day);
callback(day_period, other.day_period);
callback(hour, other.hour);
callback(minute, other.minute);
callback(second, other.second);
callback(fractional_second_digits, other.fractional_second_digits);
callback(time_zone_name, other.time_zone_name);
}
Optional<HourCycle> hour_cycle; Optional<HourCycle> hour_cycle;
Optional<bool> hour12; Optional<bool> hour12;