From 3a4cdf77ba4689ad8abc4f013abc5394e50f4c90 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 21 Jul 2023 21:33:55 -0400 Subject: [PATCH] LibJS: Move Intl.DateTimeFormat creation to CreateDateTimeFormat AO This is an editorial change in the ECMA-402 spec. See: https://github.com/tc39/ecma402/commit/b6ed64b https://github.com/tc39/ecma402/commit/6e3b70d This moves Intl.DateTimeFormat creation to InitializeDateTimeFormat and renames that AO to CreateDateTimeFormat. --- .../Libraries/LibJS/Runtime/DatePrototype.cpp | 46 ++--- .../Intl/DateTimeFormatConstructor.cpp | 171 +++++++++--------- .../Runtime/Intl/DateTimeFormatConstructor.h | 3 +- 3 files changed, 107 insertions(+), 113 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp index d6f9425cf6..08bd5d5fd5 100644 --- a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp @@ -984,17 +984,12 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_json) return TRY(this_value.invoke(vm, vm.names.toISOString)); } -static ThrowCompletionOr construct_date_time_format(VM& vm, Value locales, Value options) -{ - auto& realm = *vm.current_realm(); - auto date_time_format = TRY(construct(vm, realm.intrinsics().intl_date_time_format_constructor(), locales, options)); - return static_cast(date_time_format.ptr()); -} - // 21.4.4.38 Date.prototype.toLocaleDateString ( [ reserved1 [ , reserved2 ] ] ), https://tc39.es/ecma262/#sec-date.prototype.tolocaledatestring // 19.4.2 Date.prototype.toLocaleDateString ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sup-date.prototype.tolocaledatestring JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_date_string) { + auto& realm = *vm.current_realm(); + auto locales = vm.argument(0); auto options = vm.argument(1); @@ -1005,14 +1000,11 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_date_string) if (isnan(time)) return MUST_OR_THROW_OOM(PrimitiveString::create(vm, "Invalid Date"sv)); - // 3. Let options be ? ToDateTimeOptions(options, "date", "date"). - options = Value(TRY(Intl::to_date_time_options(vm, options, Intl::OptionRequired::Date, Intl::OptionDefaults::Date))); + // 3. Let dateFormat be ? CreateDateTimeFormat(%DateTimeFormat%, locales, options, "date", "date"). + auto date_format = TRY(Intl::create_date_time_format(vm, realm.intrinsics().intl_date_time_format_constructor(), locales, options, Intl::OptionRequired::Date, Intl::OptionDefaults::Date)); - // 4. Let dateFormat be ? Construct(%DateTimeFormat%, « locales, options »). - auto* date_format = TRY(construct_date_time_format(vm, locales, options)); - - // 5. Return ? FormatDateTime(dateFormat, x). - auto formatted = TRY(Intl::format_date_time(vm, *date_format, time)); + // 4. Return ? FormatDateTime(dateFormat, x). + auto formatted = TRY(Intl::format_date_time(vm, date_format, time)); return PrimitiveString::create(vm, move(formatted)); } @@ -1020,6 +1012,8 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_date_string) // 19.4.1 Date.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sup-date.prototype.tolocalestring JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_string) { + auto& realm = *vm.current_realm(); + auto locales = vm.argument(0); auto options = vm.argument(1); @@ -1030,14 +1024,11 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_string) if (isnan(time)) return MUST_OR_THROW_OOM(PrimitiveString::create(vm, "Invalid Date"sv)); - // 3. Let options be ? ToDateTimeOptions(options, "any", "all"). - options = Value(TRY(Intl::to_date_time_options(vm, options, Intl::OptionRequired::Any, Intl::OptionDefaults::All))); + // 3. Let dateFormat be ? CreateDateTimeFormat(%DateTimeFormat%, locales, options, "any", "all"). + auto date_format = TRY(Intl::create_date_time_format(vm, realm.intrinsics().intl_date_time_format_constructor(), locales, options, Intl::OptionRequired::Any, Intl::OptionDefaults::All)); - // 4. Let dateFormat be ? Construct(%DateTimeFormat%, « locales, options »). - auto* date_format = TRY(construct_date_time_format(vm, locales, options)); - - // 5. Return ? FormatDateTime(dateFormat, x). - auto formatted = TRY(Intl::format_date_time(vm, *date_format, time)); + // 4. Return ? FormatDateTime(dateFormat, x). + auto formatted = TRY(Intl::format_date_time(vm, date_format, time)); return PrimitiveString::create(vm, move(formatted)); } @@ -1045,6 +1036,8 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_string) // 19.4.3 Date.prototype.toLocaleTimeString ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sup-date.prototype.tolocaletimestring JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_time_string) { + auto& realm = *vm.current_realm(); + auto locales = vm.argument(0); auto options = vm.argument(1); @@ -1055,14 +1048,11 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_time_string) if (isnan(time)) return MUST_OR_THROW_OOM(PrimitiveString::create(vm, "Invalid Date"sv)); - // 3. Let options be ? ToDateTimeOptions(options, "time", "time"). - options = Value(TRY(Intl::to_date_time_options(vm, options, Intl::OptionRequired::Time, Intl::OptionDefaults::Time))); + // 3. Let timeFormat be ? CreateDateTimeFormat(%DateTimeFormat%, locales, options, "time", "time"). + auto time_format = TRY(Intl::create_date_time_format(vm, realm.intrinsics().intl_date_time_format_constructor(), locales, options, Intl::OptionRequired::Time, Intl::OptionDefaults::Time)); - // 4. Let timeFormat be ? Construct(%DateTimeFormat%, « locales, options »). - auto* time_format = TRY(construct_date_time_format(vm, locales, options)); - - // 5. Return ? FormatDateTime(dateFormat, x). - auto formatted = TRY(Intl::format_date_time(vm, *time_format, time)); + // 4. Return ? FormatDateTime(timeFormat, x). + auto formatted = TRY(Intl::format_date_time(vm, time_format, time)); return PrimitiveString::create(vm, move(formatted)); } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp index a5feb85b29..06052323d1 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -55,17 +54,14 @@ ThrowCompletionOr> DateTimeFormatConstructor::construct(Fun auto locales = vm.argument(0); auto options = vm.argument(1); - // 2. 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]] »). - auto date_time_format = TRY(ordinary_create_from_constructor(vm, new_target, &Intrinsics::intl_date_time_format_prototype)); + // 2. Let dateTimeFormat be ? CreateDateTimeFormat(newTarget, locales, options, any, date). + auto date_time_format = TRY(create_date_time_format(vm, new_target, locales, options, OptionRequired::Any, OptionDefaults::Date)); - // 3. Perform ? InitializeDateTimeFormat(dateTimeFormat, locales, options). - TRY(initialize_date_time_format(vm, date_time_format, locales, options)); - - // 4. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then + // 3. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then // a. Let this be the this value. // b. Return ? ChainDateTimeFormat(dateTimeFormat, NewTarget, this). - // 5. Return dateTimeFormat. + // 4. Return dateTimeFormat. return date_time_format; } @@ -84,119 +80,126 @@ JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatConstructor::supported_locales_of) return TRY(supported_locales(vm, requested_locales, options)); } -// 11.1.2 InitializeDateTimeFormat ( dateTimeFormat, locales, options ), https://tc39.es/ecma402/#sec-initializedatetimeformat -ThrowCompletionOr initialize_date_time_format(VM& vm, DateTimeFormat& date_time_format, Value locales_value, Value options_value) +// 11.1.2 CreateDateTimeFormat ( newTarget, locales, options, required, defaults ), https://tc39.es/ecma402/#sec-createdatetimeformat +ThrowCompletionOr> create_date_time_format(VM& vm, FunctionObject& new_target, Value locales_value, Value options_value, OptionRequired required, OptionDefaults defaults) { - // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales). + // 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]] »). + auto date_time_format = TRY(ordinary_create_from_constructor(vm, new_target, &Intrinsics::intl_date_time_format_prototype)); + + // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales). auto requested_locales = TRY(canonicalize_locale_list(vm, locales_value)); - // 2. Set options to ? ToDateTimeOptions(options, "any", "date"). + // 3. If required is not "any" or defaults is not "date", then + if (required != OptionRequired::Any || defaults != OptionDefaults::Date) { + // a. Set options to ? ToDateTimeOptions(options, required, defaults). + options_value = TRY(to_date_time_options(vm, options_value, required, defaults)); + } + + // 4. Set options to ? ToDateTimeOptions(options, "any", "date"). auto* options = TRY(to_date_time_options(vm, options_value, OptionRequired::Any, OptionDefaults::Date)); - // 3. Let opt be a new Record. + // 5. Let opt be a new Record. LocaleOptions opt {}; - // 4. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit"). + // 6. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit"). auto matcher = TRY(get_option(vm, *options, vm.names.localeMatcher, OptionType::String, AK::Array { "lookup"sv, "best fit"sv }, "best fit"sv)); - // 5. Set opt.[[localeMatcher]] to matcher. + // 7. Set opt.[[localeMatcher]] to matcher. opt.locale_matcher = matcher; - // 6. Let calendar be ? GetOption(options, "calendar", string, empty, undefined). + // 8. Let calendar be ? GetOption(options, "calendar", string, empty, undefined). auto calendar = TRY(get_option(vm, *options, vm.names.calendar, OptionType::String, {}, Empty {})); - // 7. If calendar is not undefined, then + // 9. If calendar is not undefined, then if (!calendar.is_undefined()) { - // a. If calendar does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception. + // a. If calendar cannot be matched by the type Unicode locale nonterminal, throw a RangeError exception. if (!::Locale::is_type_identifier(TRY(calendar.as_string().utf8_string_view()))) return vm.throw_completion(ErrorType::OptionIsNotValidValue, calendar, "calendar"sv); - // 8. Set opt.[[ca]] to calendar. + // 10. Set opt.[[ca]] to calendar. opt.ca = TRY(calendar.as_string().utf8_string()); } - // 9. Let numberingSystem be ? GetOption(options, "numberingSystem", string, empty, undefined). + // 11. Let numberingSystem be ? GetOption(options, "numberingSystem", string, empty, undefined). auto numbering_system = TRY(get_option(vm, *options, vm.names.numberingSystem, OptionType::String, {}, Empty {})); - // 10. If numberingSystem is not undefined, then + // 12. If numberingSystem is not undefined, then if (!numbering_system.is_undefined()) { - // a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception. + // a. If numberingSystem cannot be matched by the type Unicode locale nonterminal, throw a RangeError exception. if (!::Locale::is_type_identifier(TRY(numbering_system.as_string().utf8_string_view()))) return vm.throw_completion(ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv); - // 11. Set opt.[[nu]] to numberingSystem. + // 13. Set opt.[[nu]] to numberingSystem. opt.nu = TRY(numbering_system.as_string().utf8_string()); } - // 12. Let hour12 be ? GetOption(options, "hour12", boolean, empty, undefined). + // 14. Let hour12 be ? GetOption(options, "hour12", boolean, empty, undefined). auto hour12 = TRY(get_option(vm, *options, vm.names.hour12, OptionType::Boolean, {}, Empty {})); - // 13. Let hourCycle be ? GetOption(options, "hourCycle", string, « "h11", "h12", "h23", "h24" », undefined). + // 15. Let hourCycle be ? GetOption(options, "hourCycle", string, « "h11", "h12", "h23", "h24" », undefined). auto hour_cycle = TRY(get_option(vm, *options, vm.names.hourCycle, OptionType::String, AK::Array { "h11"sv, "h12"sv, "h23"sv, "h24"sv }, Empty {})); - // 14. If hour12 is not undefined, then + // 16. If hour12 is not undefined, then if (!hour12.is_undefined()) { // a. Set hourCycle to null. hour_cycle = js_null(); } - // 15. Set opt.[[hc]] to hourCycle. + // 17. Set opt.[[hc]] to hourCycle. if (!hour_cycle.is_nullish()) opt.hc = TRY(hour_cycle.as_string().utf8_string()); - // 16. Let localeData be %DateTimeFormat%.[[LocaleData]]. - // 17. Let r be ResolveLocale(%DateTimeFormat%.[[AvailableLocales]], requestedLocales, opt, %DateTimeFormat%.[[RelevantExtensionKeys]], localeData). + // 18. Let localeData be %DateTimeFormat%.[[LocaleData]]. + // 19. Let r be ResolveLocale(%DateTimeFormat%.[[AvailableLocales]], requestedLocales, opt, %DateTimeFormat%.[[RelevantExtensionKeys]], localeData). auto result = MUST_OR_THROW_OOM(resolve_locale(vm, requested_locales, opt, DateTimeFormat::relevant_extension_keys())); - // 18. Set dateTimeFormat.[[Locale]] to r.[[locale]]. - date_time_format.set_locale(move(result.locale)); + // 20. Set dateTimeFormat.[[Locale]] to r.[[locale]]. + date_time_format->set_locale(move(result.locale)); - // 19. Set resolvedCalendar to r.[[ca]]. - // 20. Set dateTimeFormat.[[Calendar]] to resolvedCalendar. + // 21. Let resolvedCalendar be r.[[ca]]. + // 22. Set dateTimeFormat.[[Calendar]] to resolvedCalendar. if (result.ca.has_value()) - date_time_format.set_calendar(result.ca.release_value()); + date_time_format->set_calendar(result.ca.release_value()); - // 21. Set dateTimeFormat.[[NumberingSystem]] to r.[[nu]]. + // 23. Set dateTimeFormat.[[NumberingSystem]] to r.[[nu]]. if (result.nu.has_value()) - date_time_format.set_numbering_system(result.nu.release_value()); + date_time_format->set_numbering_system(result.nu.release_value()); - // 22. Let dataLocale be r.[[dataLocale]]. + // 24. Let dataLocale be r.[[dataLocale]]. auto data_locale = move(result.data_locale); // Non-standard, the data locale is needed for LibUnicode lookups while formatting. - date_time_format.set_data_locale(data_locale); + date_time_format->set_data_locale(data_locale); - // 23. Let dataLocaleData be localeData.[[]]. - // 24. Let hcDefault be dataLocaleData.[[hourCycle]]. + // 25. Let dataLocaleData be localeData.[[]]. + // 26. Let hcDefault be dataLocaleData.[[hourCycle]]. auto default_hour_cycle = TRY_OR_THROW_OOM(vm, ::Locale::get_default_regional_hour_cycle(data_locale)); // Non-standard, default_hour_cycle will be empty if Unicode data generation is disabled. if (!default_hour_cycle.has_value()) { - date_time_format.set_time_zone(TRY_OR_THROW_OOM(vm, String::from_utf8(default_time_zone()))); - return &date_time_format; + date_time_format->set_time_zone(TRY_OR_THROW_OOM(vm, String::from_utf8(default_time_zone()))); + return date_time_format; } Optional<::Locale::HourCycle> hour_cycle_value; - // 25. If hour12 is true, then + // 27. If hour12 is true, then if (hour12.is_boolean() && hour12.as_bool()) { // a. If hcDefault is "h11" or "h23", let hc be "h11". Otherwise, let hc be "h12". if ((default_hour_cycle == ::Locale::HourCycle::H11) || (default_hour_cycle == ::Locale::HourCycle::H23)) hour_cycle_value = ::Locale::HourCycle::H11; else hour_cycle_value = ::Locale::HourCycle::H12; - } - // 26. Else if hour12 is false, then + // 28. Else if hour12 is false, then else if (hour12.is_boolean() && !hour12.as_bool()) { // a. If hcDefault is "h11" or "h23", let hc be "h23". Otherwise, let hc be "h24". if ((default_hour_cycle == ::Locale::HourCycle::H11) || (default_hour_cycle == ::Locale::HourCycle::H23)) hour_cycle_value = ::Locale::HourCycle::H23; else hour_cycle_value = ::Locale::HourCycle::H24; - } - // 27. Else, + // 29. Else, else { // a. Assert: hour12 is undefined. VERIFY(hour12.is_undefined()); @@ -210,20 +213,20 @@ ThrowCompletionOr initialize_date_time_format(VM& vm, DateTimeF hour_cycle_value = default_hour_cycle; } - // 28. Set dateTimeFormat.[[HourCycle]] to hc. + // 30. Set dateTimeFormat.[[HourCycle]] to hc. if (hour_cycle_value.has_value()) - date_time_format.set_hour_cycle(*hour_cycle_value); + date_time_format->set_hour_cycle(*hour_cycle_value); - // 29. Let timeZone be ? Get(options, "timeZone"). + // 31. Let timeZone be ? Get(options, "timeZone"). auto time_zone_value = TRY(options->get(vm.names.timeZone)); String time_zone; - // 30. If timeZone is undefined, then + // 32. If timeZone is undefined, then if (time_zone_value.is_undefined()) { // a. Set timeZone to DefaultTimeZone(). time_zone = TRY_OR_THROW_OOM(vm, String::from_utf8(default_time_zone())); } - // 31. Else, + // 33. Else, else { // a. Set timeZone to ? ToString(timeZone). time_zone = TRY(time_zone_value.to_string(vm)); @@ -238,20 +241,20 @@ ThrowCompletionOr initialize_date_time_format(VM& vm, DateTimeF time_zone = MUST_OR_THROW_OOM(Temporal::canonicalize_time_zone_name(vm, time_zone)); } - // 32. Set dateTimeFormat.[[TimeZone]] to timeZone. - date_time_format.set_time_zone(move(time_zone)); + // 34. Set dateTimeFormat.[[TimeZone]] to timeZone. + date_time_format->set_time_zone(move(time_zone)); - // 33. Let formatOptions be a new Record. + // 35. Let formatOptions be a new Record. ::Locale::CalendarPattern format_options {}; - // 34. Set formatOptions.[[hourCycle]] to hc. + // 36. Set formatOptions.[[hourCycle]] to hc. format_options.hour_cycle = hour_cycle_value; - // 35. Let hasExplicitFormatComponents be false. + // 37. Let hasExplicitFormatComponents be false. // NOTE: Instead of using a boolean, we track any explicitly provided component name for nicer exception messages. PropertyKey const* explicit_format_component = nullptr; - // 36. For each row of Table 6, except the header row, in table order, do + // 38. For each row of Table 6, except the header row, in table order, do TRY(for_each_calendar_field(vm, format_options, [&](auto& option, auto const& property, auto const& values) -> ThrowCompletionOr { using ValueType = typename RemoveReference::ValueType; @@ -290,27 +293,27 @@ ThrowCompletionOr initialize_date_time_format(VM& vm, DateTimeF return {}; })); - // 37. Let matcher be ? GetOption(options, "formatMatcher", string, « "basic", "best fit" », "best fit"). + // 39. Let matcher 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)); - // 38. Let dateStyle be ? GetOption(options, "dateStyle", string, « "full", "long", "medium", "short" », undefined). + // 40. 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 {})); - // 39. Set dateTimeFormat.[[DateStyle]] to dateStyle. + // 41. Set dateTimeFormat.[[DateStyle]] to dateStyle. if (!date_style.is_undefined()) - date_time_format.set_date_style(TRY(date_style.as_string().utf8_string_view())); + date_time_format->set_date_style(TRY(date_style.as_string().utf8_string_view())); - // 40. Let timeStyle be ? GetOption(options, "timeStyle", string, « "full", "long", "medium", "short" », undefined). + // 42. 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 {})); - // 41. Set dateTimeFormat.[[TimeStyle]] to timeStyle. + // 43. Set dateTimeFormat.[[TimeStyle]] to timeStyle. if (!time_style.is_undefined()) - date_time_format.set_time_style(TRY(time_style.as_string().utf8_string_view())); + date_time_format->set_time_style(TRY(time_style.as_string().utf8_string_view())); Optional<::Locale::CalendarPattern> best_format {}; - // 42. If dateStyle is not undefined or timeStyle is not undefined, then - if (date_time_format.has_date_style() || date_time_format.has_time_style()) { + // 44. If dateStyle is not undefined or timeStyle is not undefined, then + if (date_time_format->has_date_style() || date_time_format->has_time_style()) { // a. If hasExplicitFormatComponents is true, then if (explicit_format_component != nullptr) { // i. Throw a TypeError exception. @@ -321,10 +324,10 @@ ThrowCompletionOr initialize_date_time_format(VM& vm, DateTimeF // c. Let bestFormat be DateTimeStyleFormat(dateStyle, timeStyle, styles). best_format = MUST_OR_THROW_OOM(date_time_style_format(vm, data_locale, date_time_format)); } - // 43. Else, + // 45. Else, else { // a. Let formats be dataLocaleData.[[formats]].[[]]. - auto formats = TRY_OR_THROW_OOM(vm, ::Locale::get_calendar_available_formats(data_locale, date_time_format.calendar())); + auto formats = TRY_OR_THROW_OOM(vm, ::Locale::get_calendar_available_formats(data_locale, date_time_format->calendar())); // b. If matcher is "basic", then if (TRY(matcher.as_string().utf8_string_view()) == "basic"sv) { @@ -338,8 +341,8 @@ ThrowCompletionOr initialize_date_time_format(VM& vm, DateTimeF } } - // 44. For each row in Table 6, except the header row, in table order, do - date_time_format.for_each_calendar_field_zipped_with(*best_format, [&](auto& date_time_format_field, auto const& best_format_field, auto) { + // 46. For each row in Table 6, except the header row, in table order, do + date_time_format->for_each_calendar_field_zipped_with(*best_format, [&](auto& date_time_format_field, auto const& best_format_field, auto) { // a. Let prop be the name given in the Property column of the row. // b. If bestFormat has a field [[]], then if (best_format_field.has_value()) { @@ -352,13 +355,13 @@ ThrowCompletionOr initialize_date_time_format(VM& vm, DateTimeF String pattern; Vector<::Locale::CalendarRangePattern> range_patterns; - // 45. If dateTimeFormat.[[Hour]] is undefined, then - if (!date_time_format.has_hour()) { + // 47. If dateTimeFormat.[[Hour]] is undefined, then + if (!date_time_format->has_hour()) { // a. Set dateTimeFormat.[[HourCycle]] to undefined. - date_time_format.clear_hour_cycle(); + date_time_format->clear_hour_cycle(); } - // 46. If dateTimeFormat.[[HourCycle]] is "h11" or "h12", then + // 48. If dateTimeFormat.[[HourCycle]] is "h11" or "h12", then if ((hour_cycle_value == ::Locale::HourCycle::H11) || (hour_cycle_value == ::Locale::HourCycle::H12)) { // a. Let pattern be bestFormat.[[pattern12]]. if (best_format->pattern12.has_value()) { @@ -370,25 +373,25 @@ ThrowCompletionOr initialize_date_time_format(VM& vm, DateTimeF } // b. Let rangePatterns be bestFormat.[[rangePatterns12]]. - range_patterns = TRY_OR_THROW_OOM(vm, ::Locale::get_calendar_range12_formats(data_locale, date_time_format.calendar(), best_format->skeleton)); + range_patterns = TRY_OR_THROW_OOM(vm, ::Locale::get_calendar_range12_formats(data_locale, date_time_format->calendar(), best_format->skeleton)); } - // 47. Else, + // 49. Else, else { // a. Let pattern be bestFormat.[[pattern]]. pattern = move(best_format->pattern); // b. Let rangePatterns be bestFormat.[[rangePatterns]]. - range_patterns = TRY_OR_THROW_OOM(vm, ::Locale::get_calendar_range_formats(data_locale, date_time_format.calendar(), best_format->skeleton)); + range_patterns = TRY_OR_THROW_OOM(vm, ::Locale::get_calendar_range_formats(data_locale, date_time_format->calendar(), best_format->skeleton)); } - // 48. Set dateTimeFormat.[[Pattern]] to pattern. - date_time_format.set_pattern(move(pattern)); + // 50. Set dateTimeFormat.[[Pattern]] to pattern. + date_time_format->set_pattern(move(pattern)); - // 49. Set dateTimeFormat.[[RangePatterns]] to rangePatterns. - date_time_format.set_range_patterns(move(range_patterns)); + // 51. Set dateTimeFormat.[[RangePatterns]] to rangePatterns. + date_time_format->set_range_patterns(move(range_patterns)); - // 50. Return dateTimeFormat. - return &date_time_format; + // 52. Return dateTimeFormat. + return date_time_format; } } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.h b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.h index af4075b689..e4d42989c5 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.h @@ -6,6 +6,7 @@ #pragma once +#include #include namespace JS::Intl { @@ -28,6 +29,6 @@ private: JS_DECLARE_NATIVE_FUNCTION(supported_locales_of); }; -ThrowCompletionOr initialize_date_time_format(VM&, DateTimeFormat&, Value locales_value, Value options_value); +ThrowCompletionOr> create_date_time_format(VM&, FunctionObject& new_target, Value locales_value, Value options_value, OptionRequired, OptionDefaults); }