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

LibJS: Move Intl.DateTimeFormat creation to CreateDateTimeFormat AO

This is an editorial change in the ECMA-402 spec. See:
b6ed64b
6e3b70d

This moves Intl.DateTimeFormat creation to InitializeDateTimeFormat and
renames that AO to CreateDateTimeFormat.
This commit is contained in:
Timothy Flynn 2023-07-21 21:33:55 -04:00 committed by Linus Groh
parent ea774111e8
commit 3a4cdf77ba
3 changed files with 107 additions and 113 deletions

View file

@ -984,17 +984,12 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_json)
return TRY(this_value.invoke(vm, vm.names.toISOString)); return TRY(this_value.invoke(vm, vm.names.toISOString));
} }
static ThrowCompletionOr<Intl::DateTimeFormat*> 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<Intl::DateTimeFormat*>(date_time_format.ptr());
}
// 21.4.4.38 Date.prototype.toLocaleDateString ( [ reserved1 [ , reserved2 ] ] ), https://tc39.es/ecma262/#sec-date.prototype.tolocaledatestring // 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 // 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) JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_date_string)
{ {
auto& realm = *vm.current_realm();
auto locales = vm.argument(0); auto locales = vm.argument(0);
auto options = vm.argument(1); auto options = vm.argument(1);
@ -1005,14 +1000,11 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_date_string)
if (isnan(time)) if (isnan(time))
return MUST_OR_THROW_OOM(PrimitiveString::create(vm, "Invalid Date"sv)); return MUST_OR_THROW_OOM(PrimitiveString::create(vm, "Invalid Date"sv));
// 3. Let options be ? ToDateTimeOptions(options, "date", "date"). // 3. Let dateFormat be ? CreateDateTimeFormat(%DateTimeFormat%, locales, options, "date", "date").
options = Value(TRY(Intl::to_date_time_options(vm, options, Intl::OptionRequired::Date, Intl::OptionDefaults::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 »). // 4. Return ? FormatDateTime(dateFormat, x).
auto* date_format = TRY(construct_date_time_format(vm, locales, options)); auto formatted = TRY(Intl::format_date_time(vm, date_format, time));
// 5. Return ? FormatDateTime(dateFormat, x).
auto formatted = TRY(Intl::format_date_time(vm, *date_format, time));
return PrimitiveString::create(vm, move(formatted)); 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 // 19.4.1 Date.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sup-date.prototype.tolocalestring
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_string) JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_string)
{ {
auto& realm = *vm.current_realm();
auto locales = vm.argument(0); auto locales = vm.argument(0);
auto options = vm.argument(1); auto options = vm.argument(1);
@ -1030,14 +1024,11 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_string)
if (isnan(time)) if (isnan(time))
return MUST_OR_THROW_OOM(PrimitiveString::create(vm, "Invalid Date"sv)); return MUST_OR_THROW_OOM(PrimitiveString::create(vm, "Invalid Date"sv));
// 3. Let options be ? ToDateTimeOptions(options, "any", "all"). // 3. Let dateFormat be ? CreateDateTimeFormat(%DateTimeFormat%, locales, options, "any", "all").
options = Value(TRY(Intl::to_date_time_options(vm, options, Intl::OptionRequired::Any, Intl::OptionDefaults::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 »). // 4. Return ? FormatDateTime(dateFormat, x).
auto* date_format = TRY(construct_date_time_format(vm, locales, options)); auto formatted = TRY(Intl::format_date_time(vm, date_format, time));
// 5. Return ? FormatDateTime(dateFormat, x).
auto formatted = TRY(Intl::format_date_time(vm, *date_format, time));
return PrimitiveString::create(vm, move(formatted)); 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 // 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) JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_time_string)
{ {
auto& realm = *vm.current_realm();
auto locales = vm.argument(0); auto locales = vm.argument(0);
auto options = vm.argument(1); auto options = vm.argument(1);
@ -1055,14 +1048,11 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_time_string)
if (isnan(time)) if (isnan(time))
return MUST_OR_THROW_OOM(PrimitiveString::create(vm, "Invalid Date"sv)); return MUST_OR_THROW_OOM(PrimitiveString::create(vm, "Invalid Date"sv));
// 3. Let options be ? ToDateTimeOptions(options, "time", "time"). // 3. Let timeFormat be ? CreateDateTimeFormat(%DateTimeFormat%, locales, options, "time", "time").
options = Value(TRY(Intl::to_date_time_options(vm, options, Intl::OptionRequired::Time, Intl::OptionDefaults::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 »). // 4. Return ? FormatDateTime(timeFormat, x).
auto* time_format = TRY(construct_date_time_format(vm, locales, options)); auto formatted = TRY(Intl::format_date_time(vm, time_format, time));
// 5. Return ? FormatDateTime(dateFormat, x).
auto formatted = TRY(Intl::format_date_time(vm, *time_format, time));
return PrimitiveString::create(vm, move(formatted)); return PrimitiveString::create(vm, move(formatted));
} }

View file

@ -9,7 +9,6 @@
#include <LibJS/Runtime/Date.h> #include <LibJS/Runtime/Date.h>
#include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Intl/AbstractOperations.h> #include <LibJS/Runtime/Intl/AbstractOperations.h>
#include <LibJS/Runtime/Intl/DateTimeFormat.h>
#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h> #include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
#include <LibJS/Runtime/Temporal/TimeZone.h> #include <LibJS/Runtime/Temporal/TimeZone.h>
#include <LibLocale/DateTimeFormat.h> #include <LibLocale/DateTimeFormat.h>
@ -55,17 +54,14 @@ ThrowCompletionOr<NonnullGCPtr<Object>> DateTimeFormatConstructor::construct(Fun
auto locales = vm.argument(0); auto locales = vm.argument(0);
auto options = vm.argument(1); 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]] »). // 2. Let dateTimeFormat be ? CreateDateTimeFormat(newTarget, locales, options, any, date).
auto date_time_format = TRY(ordinary_create_from_constructor<DateTimeFormat>(vm, new_target, &Intrinsics::intl_date_time_format_prototype)); auto date_time_format = TRY(create_date_time_format(vm, new_target, locales, options, OptionRequired::Any, OptionDefaults::Date));
// 3. Perform ? InitializeDateTimeFormat(dateTimeFormat, locales, options). // 3. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then
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
// a. Let this be the this value. // a. Let this be the this value.
// b. Return ? ChainDateTimeFormat(dateTimeFormat, NewTarget, this). // b. Return ? ChainDateTimeFormat(dateTimeFormat, NewTarget, this).
// 5. Return dateTimeFormat. // 4. Return dateTimeFormat.
return date_time_format; return date_time_format;
} }
@ -84,119 +80,126 @@ JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatConstructor::supported_locales_of)
return TRY(supported_locales(vm, requested_locales, options)); return TRY(supported_locales(vm, requested_locales, options));
} }
// 11.1.2 InitializeDateTimeFormat ( dateTimeFormat, locales, options ), https://tc39.es/ecma402/#sec-initializedatetimeformat // 11.1.2 CreateDateTimeFormat ( newTarget, locales, options, required, defaults ), https://tc39.es/ecma402/#sec-createdatetimeformat
ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(VM& vm, DateTimeFormat& date_time_format, Value locales_value, Value options_value) ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> 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<DateTimeFormat>(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)); 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)); 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 {}; 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)); 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; 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 {})); 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()) { 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()))) if (!::Locale::is_type_identifier(TRY(calendar.as_string().utf8_string_view())))
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, calendar, "calendar"sv); return vm.throw_completion<RangeError>(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()); 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 {})); 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()) { 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()))) if (!::Locale::is_type_identifier(TRY(numbering_system.as_string().utf8_string_view())))
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv); return vm.throw_completion<RangeError>(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()); 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 {})); 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 {})); 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()) { if (!hour12.is_undefined()) {
// a. Set hourCycle to null. // a. Set hourCycle to null.
hour_cycle = js_null(); hour_cycle = js_null();
} }
// 15. Set opt.[[hc]] to hourCycle. // 17. Set opt.[[hc]] to hourCycle.
if (!hour_cycle.is_nullish()) if (!hour_cycle.is_nullish())
opt.hc = TRY(hour_cycle.as_string().utf8_string()); opt.hc = TRY(hour_cycle.as_string().utf8_string());
// 16. Let localeData be %DateTimeFormat%.[[LocaleData]]. // 18. Let localeData be %DateTimeFormat%.[[LocaleData]].
// 17. Let r be ResolveLocale(%DateTimeFormat%.[[AvailableLocales]], requestedLocales, opt, %DateTimeFormat%.[[RelevantExtensionKeys]], 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())); auto result = MUST_OR_THROW_OOM(resolve_locale(vm, requested_locales, opt, DateTimeFormat::relevant_extension_keys()));
// 18. Set dateTimeFormat.[[Locale]] to r.[[locale]]. // 20. Set dateTimeFormat.[[Locale]] to r.[[locale]].
date_time_format.set_locale(move(result.locale)); date_time_format->set_locale(move(result.locale));
// 19. Set resolvedCalendar to r.[[ca]]. // 21. Let resolvedCalendar be r.[[ca]].
// 20. Set dateTimeFormat.[[Calendar]] to resolvedCalendar. // 22. Set dateTimeFormat.[[Calendar]] to resolvedCalendar.
if (result.ca.has_value()) 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()) 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); auto data_locale = move(result.data_locale);
// Non-standard, the data locale is needed for LibUnicode lookups while formatting. // 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.[[<dataLocale>]]. // 25. Let dataLocaleData be localeData.[[<dataLocale>]].
// 24. Let hcDefault be dataLocaleData.[[hourCycle]]. // 26. Let hcDefault be dataLocaleData.[[hourCycle]].
auto default_hour_cycle = TRY_OR_THROW_OOM(vm, ::Locale::get_default_regional_hour_cycle(data_locale)); 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. // Non-standard, default_hour_cycle will be empty if Unicode data generation is disabled.
if (!default_hour_cycle.has_value()) { if (!default_hour_cycle.has_value()) {
date_time_format.set_time_zone(TRY_OR_THROW_OOM(vm, String::from_utf8(default_time_zone()))); date_time_format->set_time_zone(TRY_OR_THROW_OOM(vm, String::from_utf8(default_time_zone())));
return &date_time_format; return date_time_format;
} }
Optional<::Locale::HourCycle> hour_cycle_value; 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()) { if (hour12.is_boolean() && hour12.as_bool()) {
// a. If hcDefault is "h11" or "h23", let hc be "h11". Otherwise, let hc be "h12". // 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)) if ((default_hour_cycle == ::Locale::HourCycle::H11) || (default_hour_cycle == ::Locale::HourCycle::H23))
hour_cycle_value = ::Locale::HourCycle::H11; hour_cycle_value = ::Locale::HourCycle::H11;
else else
hour_cycle_value = ::Locale::HourCycle::H12; 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()) { else if (hour12.is_boolean() && !hour12.as_bool()) {
// a. If hcDefault is "h11" or "h23", let hc be "h23". Otherwise, let hc be "h24". // 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)) if ((default_hour_cycle == ::Locale::HourCycle::H11) || (default_hour_cycle == ::Locale::HourCycle::H23))
hour_cycle_value = ::Locale::HourCycle::H23; hour_cycle_value = ::Locale::HourCycle::H23;
else else
hour_cycle_value = ::Locale::HourCycle::H24; hour_cycle_value = ::Locale::HourCycle::H24;
} }
// 27. Else, // 29. Else,
else { else {
// a. Assert: hour12 is undefined. // a. Assert: hour12 is undefined.
VERIFY(hour12.is_undefined()); VERIFY(hour12.is_undefined());
@ -210,20 +213,20 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(VM& vm, DateTimeF
hour_cycle_value = default_hour_cycle; hour_cycle_value = default_hour_cycle;
} }
// 28. Set dateTimeFormat.[[HourCycle]] to hc. // 30. Set dateTimeFormat.[[HourCycle]] to hc.
if (hour_cycle_value.has_value()) 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)); auto time_zone_value = TRY(options->get(vm.names.timeZone));
String time_zone; String time_zone;
// 30. If timeZone is undefined, then // 32. 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 DefaultTimeZone().
time_zone = TRY_OR_THROW_OOM(vm, String::from_utf8(default_time_zone())); time_zone = TRY_OR_THROW_OOM(vm, String::from_utf8(default_time_zone()));
} }
// 31. Else, // 33. 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));
@ -238,20 +241,20 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(VM& vm, DateTimeF
time_zone = MUST_OR_THROW_OOM(Temporal::canonicalize_time_zone_name(vm, time_zone)); time_zone = MUST_OR_THROW_OOM(Temporal::canonicalize_time_zone_name(vm, time_zone));
} }
// 32. Set dateTimeFormat.[[TimeZone]] to timeZone. // 34. Set dateTimeFormat.[[TimeZone]] to timeZone.
date_time_format.set_time_zone(move(time_zone)); 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 {}; ::Locale::CalendarPattern format_options {};
// 34. Set formatOptions.[[hourCycle]] to hc. // 36. Set formatOptions.[[hourCycle]] to hc.
format_options.hour_cycle = hour_cycle_value; 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. // 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;
// 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<void> { TRY(for_each_calendar_field(vm, format_options, [&](auto& option, auto const& property, auto const& values) -> ThrowCompletionOr<void> {
using ValueType = typename RemoveReference<decltype(option)>::ValueType; using ValueType = typename RemoveReference<decltype(option)>::ValueType;
@ -290,27 +293,27 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(VM& vm, DateTimeF
return {}; 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)); 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 {})); 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()) 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 {})); 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()) 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 {}; Optional<::Locale::CalendarPattern> best_format {};
// 42. If dateStyle is not undefined or timeStyle is not undefined, then // 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()) { 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) {
// i. Throw a TypeError exception. // i. Throw a TypeError exception.
@ -321,10 +324,10 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(VM& vm, DateTimeF
// c. Let bestFormat be DateTimeStyleFormat(dateStyle, timeStyle, styles). // c. Let bestFormat be DateTimeStyleFormat(dateStyle, timeStyle, styles).
best_format = MUST_OR_THROW_OOM(date_time_style_format(vm, data_locale, date_time_format)); best_format = MUST_OR_THROW_OOM(date_time_style_format(vm, data_locale, date_time_format));
} }
// 43. Else, // 45. Else,
else { else {
// a. Let formats be dataLocaleData.[[formats]].[[<resolvedCalendar>]]. // a. Let formats be dataLocaleData.[[formats]].[[<resolvedCalendar>]].
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 // b. If matcher is "basic", then
if (TRY(matcher.as_string().utf8_string_view()) == "basic"sv) { if (TRY(matcher.as_string().utf8_string_view()) == "basic"sv) {
@ -338,8 +341,8 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(VM& vm, DateTimeF
} }
} }
// 44. For each row in Table 6, except the header row, in table order, do // 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) { 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. // a. Let prop be the name given in the Property column of the row.
// b. If bestFormat has a field [[<prop>]], then // b. If bestFormat has a field [[<prop>]], then
if (best_format_field.has_value()) { if (best_format_field.has_value()) {
@ -352,13 +355,13 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(VM& vm, DateTimeF
String pattern; String pattern;
Vector<::Locale::CalendarRangePattern> range_patterns; Vector<::Locale::CalendarRangePattern> range_patterns;
// 45. If dateTimeFormat.[[Hour]] is undefined, then // 47. If dateTimeFormat.[[Hour]] is undefined, then
if (!date_time_format.has_hour()) { if (!date_time_format->has_hour()) {
// a. Set dateTimeFormat.[[HourCycle]] to undefined. // 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)) { if ((hour_cycle_value == ::Locale::HourCycle::H11) || (hour_cycle_value == ::Locale::HourCycle::H12)) {
// a. Let pattern be bestFormat.[[pattern12]]. // a. Let pattern be bestFormat.[[pattern12]].
if (best_format->pattern12.has_value()) { if (best_format->pattern12.has_value()) {
@ -370,25 +373,25 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(VM& vm, DateTimeF
} }
// b. Let rangePatterns be bestFormat.[[rangePatterns12]]. // 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 { else {
// a. Let pattern be bestFormat.[[pattern]]. // a. Let pattern be bestFormat.[[pattern]].
pattern = move(best_format->pattern); pattern = move(best_format->pattern);
// b. Let rangePatterns be bestFormat.[[rangePatterns]]. // 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. // 50. Set dateTimeFormat.[[Pattern]] to pattern.
date_time_format.set_pattern(move(pattern)); date_time_format->set_pattern(move(pattern));
// 49. Set dateTimeFormat.[[RangePatterns]] to rangePatterns. // 51. Set dateTimeFormat.[[RangePatterns]] to rangePatterns.
date_time_format.set_range_patterns(move(range_patterns)); date_time_format->set_range_patterns(move(range_patterns));
// 50. Return dateTimeFormat. // 52. Return dateTimeFormat.
return &date_time_format; return date_time_format;
} }
} }

View file

@ -6,6 +6,7 @@
#pragma once #pragma once
#include <LibJS/Runtime/Intl/DateTimeFormat.h>
#include <LibJS/Runtime/NativeFunction.h> #include <LibJS/Runtime/NativeFunction.h>
namespace JS::Intl { namespace JS::Intl {
@ -28,6 +29,6 @@ private:
JS_DECLARE_NATIVE_FUNCTION(supported_locales_of); JS_DECLARE_NATIVE_FUNCTION(supported_locales_of);
}; };
ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(VM&, DateTimeFormat&, Value locales_value, Value options_value); ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM&, FunctionObject& new_target, Value locales_value, Value options_value, OptionRequired, OptionDefaults);
} }