2021-09-05 23:05:16 -04:00
/*
2025-02-28 08:54:42 -05:00
* Copyright ( c ) 2021 - 2025 , Tim Flynn < trflynn89 @ ladybird . org >
2021-09-05 23:05:16 -04:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# include <LibJS/Runtime/AbstractOperations.h>
2021-09-06 09:00:06 -04:00
# include <LibJS/Runtime/Array.h>
2021-09-05 23:05:16 -04:00
# include <LibJS/Runtime/GlobalObject.h>
2021-09-06 08:53:28 -04:00
# include <LibJS/Runtime/Intl/AbstractOperations.h>
2021-09-05 23:05:16 -04:00
# include <LibJS/Runtime/Intl/ListFormat.h>
# include <LibJS/Runtime/Intl/ListFormatConstructor.h>
namespace JS : : Intl {
2024-11-15 04:01:23 +13:00
GC_DEFINE_ALLOCATOR ( ListFormatConstructor ) ;
2023-11-19 09:45:05 +01:00
2025-02-28 08:54:42 -05:00
// 14.1 The Intl.ListFormat Constructor, https://tc39.es/ecma402/#sec-intl-listformat-constructor
2022-08-16 00:20:49 +01:00
ListFormatConstructor : : ListFormatConstructor ( Realm & realm )
2023-04-13 00:47:15 +02:00
: NativeFunction ( realm . vm ( ) . names . ListFormat . as_string ( ) , realm . intrinsics ( ) . function_prototype ( ) )
2021-09-05 23:05:16 -04:00
{
}
2023-08-07 08:41:28 +02:00
void ListFormatConstructor : : initialize ( Realm & realm )
2021-09-05 23:05:16 -04:00
{
2023-08-07 08:41:28 +02:00
Base : : initialize ( realm ) ;
2021-09-05 23:05:16 -04:00
auto & vm = this - > vm ( ) ;
2025-02-28 08:54:42 -05:00
// 14.2.1 Intl.ListFormat.prototype, https://tc39.es/ecma402/#sec-Intl.ListFormat.prototype
2022-08-27 00:54:55 +01:00
define_direct_property ( vm . names . prototype , realm . intrinsics ( ) . intl_list_format_prototype ( ) , 0 ) ;
2021-09-06 09:00:06 -04:00
u8 attr = Attribute : : Writable | Attribute : : Configurable ;
2022-08-22 21:47:35 +01:00
define_native_function ( realm , vm . names . supportedLocalesOf , supported_locales_of , 1 , attr ) ;
2021-09-06 09:00:06 -04:00
2021-09-05 23:05:16 -04:00
define_direct_property ( vm . names . length , Value ( 0 ) , Attribute : : Configurable ) ;
}
2025-02-28 08:54:42 -05:00
// 14.1.1 Intl.ListFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.ListFormat
2021-10-20 21:16:30 +01:00
ThrowCompletionOr < Value > ListFormatConstructor : : call ( )
2021-09-05 23:05:16 -04:00
{
// 1. If NewTarget is undefined, throw a TypeError exception.
2022-08-16 20:33:17 +01:00
return vm ( ) . throw_completion < TypeError > ( ErrorType : : ConstructorWithoutNew , " Intl.ListFormat " ) ;
2021-09-05 23:05:16 -04:00
}
2025-02-28 08:54:42 -05:00
// 14.1.1 Intl.ListFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.ListFormat
2024-11-15 04:01:23 +13:00
ThrowCompletionOr < GC : : Ref < Object > > ListFormatConstructor : : construct ( FunctionObject & new_target )
2021-09-05 23:05:16 -04:00
{
auto & vm = this - > vm ( ) ;
2021-09-11 08:24:17 -04:00
auto locale_value = vm . argument ( 0 ) ;
auto options_value = vm . argument ( 1 ) ;
2021-09-06 08:53:28 -04:00
2024-06-17 16:46:59 -04:00
// 2. Let listFormat be ? OrdinaryCreateFromConstructor(NewTarget, "%Intl.ListFormat.prototype%", « [[InitializedListFormat]], [[Locale]], [[Type]], [[Style]], [[Templates]] »).
2022-12-14 18:34:32 +00:00
auto list_format = TRY ( ordinary_create_from_constructor < ListFormat > ( vm , new_target , & Intrinsics : : intl_list_format_prototype ) ) ;
2021-09-05 23:05:16 -04:00
2021-09-06 08:53:28 -04:00
// 3. Let requestedLocales be ? CanonicalizeLocaleList(locales).
2022-08-20 08:25:24 +01:00
auto requested_locales = TRY ( canonicalize_locale_list ( vm , locale_value ) ) ;
2021-09-06 08:53:28 -04:00
// 4. Set options to ? GetOptionsObject(options).
2025-02-28 12:15:49 -05:00
auto options = TRY ( get_options_object ( vm , options_value ) ) ;
2021-09-06 08:53:28 -04:00
// 5. Let opt be a new Record.
LocaleOptions opt { } ;
2023-01-12 10:22:37 -05:00
// 6. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit").
2022-08-20 08:52:42 +01:00
auto matcher = TRY ( get_option ( vm , * options , vm . names . localeMatcher , OptionType : : String , { " lookup " sv , " best fit " sv } , " best fit " sv ) ) ;
2021-09-06 08:53:28 -04:00
// 7. Set opt.[[localeMatcher]] to matcher.
opt . locale_matcher = matcher ;
2024-06-17 16:46:59 -04:00
// 8. Let r be ResolveLocale(%Intl.ListFormat%.[[AvailableLocales]], requestedLocales, opt, %Intl.ListFormat%.[[RelevantExtensionKeys]], %Intl.ListFormat%.[[LocaleData]]).
2023-08-30 11:08:15 -04:00
auto result = resolve_locale ( requested_locales , opt , { } ) ;
2021-09-06 08:53:28 -04:00
2024-06-17 16:46:59 -04:00
// 9. Set listFormat.[[Locale]] to r.[[Locale]].
2021-09-06 08:53:28 -04:00
list_format - > set_locale ( move ( result . locale ) ) ;
2024-06-17 16:46:59 -04:00
// 10. Let type be ? GetOption(options, "type", string, « "conjunction", "disjunction", "unit" », "conjunction").
2022-08-20 08:52:42 +01:00
auto type = TRY ( get_option ( vm , * options , vm . names . type , OptionType : : String , { " conjunction " sv , " disjunction " sv , " unit " sv } , " conjunction " sv ) ) ;
2021-09-06 08:53:28 -04:00
2024-06-17 16:46:59 -04:00
// 11. Set listFormat.[[Type]] to type.
2023-08-08 19:17:55 +02:00
list_format - > set_type ( type . as_string ( ) . utf8_string_view ( ) ) ;
2021-09-06 08:53:28 -04:00
2024-06-17 16:46:59 -04:00
// 12. Let style be ? GetOption(options, "style", string, « "long", "short", "narrow" », "long").
2022-08-20 08:52:42 +01:00
auto style = TRY ( get_option ( vm , * options , vm . names . style , OptionType : : String , { " long " sv , " short " sv , " narrow " sv } , " long " sv ) ) ;
2021-09-06 08:53:28 -04:00
2025-02-28 08:54:42 -05:00
// 14. Set listFormat.[[Style]] to style.
2023-08-08 19:17:55 +02:00
list_format - > set_style ( style . as_string ( ) . utf8_string_view ( ) ) ;
2021-09-06 08:53:28 -04:00
2024-06-17 16:46:59 -04:00
// 14. Let resolvedLocaleData be r.[[LocaleData]].
// 15. Let dataLocaleTypes be resolvedLocaleData.[[<type>]].
// 16. Set listFormat.[[Templates]] to dataLocaleTypes.[[<style>]].
2024-06-23 09:14:27 -04:00
auto formatter = Unicode : : ListFormat : : create (
2024-06-17 16:46:59 -04:00
list_format - > locale ( ) ,
list_format - > type ( ) ,
list_format - > style ( ) ) ;
list_format - > set_formatter ( move ( formatter ) ) ;
2021-09-06 08:53:28 -04:00
2024-06-17 16:46:59 -04:00
// 17. Return listFormat.
2022-12-14 19:18:10 +00:00
return list_format ;
2021-09-05 23:05:16 -04:00
}
2025-02-28 08:54:42 -05:00
// 14.2.2 Intl.ListFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-Intl.ListFormat.supportedLocalesOf
2021-10-22 22:49:07 +01:00
JS_DEFINE_NATIVE_FUNCTION ( ListFormatConstructor : : supported_locales_of )
2021-09-06 09:00:06 -04:00
{
auto locales = vm . argument ( 0 ) ;
auto options = vm . argument ( 1 ) ;
2021-09-08 13:34:30 -04:00
// 1. Let availableLocales be %ListFormat%.[[AvailableLocales]].
2021-09-06 09:00:06 -04:00
// 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
2022-08-20 08:25:24 +01:00
auto requested_locales = TRY ( canonicalize_locale_list ( vm , locales ) ) ;
2021-09-06 09:00:06 -04:00
2024-06-18 10:13:30 -04:00
// 3. Return ? FilterLocales(availableLocales, requestedLocales, options).
return TRY ( filter_locales ( vm , requested_locales , options ) ) ;
2021-09-06 09:00:06 -04:00
}
2021-09-05 23:05:16 -04:00
}