| 
									
										
										
										
											2021-08-24 22:27:05 -04:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-01-12 13:52:51 -05:00
										 |  |  |  * Copyright (c) 2021-2022, Tim Flynn <trflynn89@pm.me> | 
					
						
							| 
									
										
										
										
											2021-08-24 22:27:05 -04:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/AbstractOperations.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-04 17:08:57 +01:00
										 |  |  | #include <LibJS/Runtime/Array.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-24 22:27:05 -04:00
										 |  |  | #include <LibJS/Runtime/GlobalObject.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-24 22:58:45 -04:00
										 |  |  | #include <LibJS/Runtime/Intl/AbstractOperations.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-24 22:27:05 -04:00
										 |  |  | #include <LibJS/Runtime/Intl/DisplayNames.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-24 22:58:45 -04:00
										 |  |  | #include <LibJS/Runtime/Temporal/AbstractOperations.h>
 | 
					
						
							|  |  |  | #include <LibUnicode/Locale.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-24 22:27:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace JS::Intl { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 12.2 The Intl.DisplayNames Constructor, https://tc39.es/ecma402/#sec-intl-displaynames-constructor
 | 
					
						
							|  |  |  | DisplayNamesConstructor::DisplayNamesConstructor(GlobalObject& global_object) | 
					
						
							|  |  |  |     : NativeFunction(vm().names.DisplayNames.as_string(), *global_object.function_prototype()) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DisplayNamesConstructor::initialize(GlobalObject& global_object) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     NativeFunction::initialize(global_object); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 12.3.1 Intl.DisplayNames.prototype, https://tc39.es/ecma402/#sec-Intl.DisplayNames.prototype
 | 
					
						
							|  |  |  |     define_direct_property(vm.names.prototype, global_object.intl_display_names_prototype(), 0); | 
					
						
							| 
									
										
										
										
											2021-09-04 17:08:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     u8 attr = Attribute::Writable | Attribute::Configurable; | 
					
						
							| 
									
										
										
										
											2021-10-22 22:48:25 +01:00
										 |  |  |     define_native_function(vm.names.supportedLocalesOf, supported_locales_of, 1, attr); | 
					
						
							| 
									
										
										
										
											2021-09-04 17:08:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 22:27:05 -04:00
										 |  |  |     define_direct_property(vm.names.length, Value(2), Attribute::Configurable); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 12.2.1 Intl.DisplayNames ( locales, options ), https://tc39.es/ecma402/#sec-Intl.DisplayNames
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  | ThrowCompletionOr<Value> DisplayNamesConstructor::call() | 
					
						
							| 
									
										
										
										
											2021-08-24 22:27:05 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     // 1. If NewTarget is undefined, throw a TypeError exception.
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |     return vm().throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Intl.DisplayNames"); | 
					
						
							| 
									
										
										
										
											2021-08-24 22:27:05 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 12.2.1 Intl.DisplayNames ( locales, options ), https://tc39.es/ecma402/#sec-Intl.DisplayNames
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  | ThrowCompletionOr<Object*> DisplayNamesConstructor::construct(FunctionObject& new_target) | 
					
						
							| 
									
										
										
										
											2021-08-24 22:27:05 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  |     auto& global_object = this->global_object(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-11 08:24:17 -04:00
										 |  |  |     auto locale_value = vm.argument(0); | 
					
						
							|  |  |  |     auto options_value = vm.argument(1); | 
					
						
							| 
									
										
										
										
											2021-08-24 22:58:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-12 13:52:51 -05:00
										 |  |  |     // 2. Let displayNames be ? OrdinaryCreateFromConstructor(NewTarget, "%DisplayNames.prototype%", « [[InitializedDisplayNames]], [[Locale]], [[Style]], [[Type]], [[Fallback]], [[LanguageDisplay]], [[Fields]] »).
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |     auto* display_names = TRY(ordinary_create_from_constructor<DisplayNames>(global_object, new_target, &GlobalObject::intl_display_names_prototype)); | 
					
						
							| 
									
										
										
										
											2021-08-24 22:27:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 22:58:45 -04:00
										 |  |  |     // 3. Let requestedLocales be ? CanonicalizeLocaleList(locales).
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |     auto requested_locales = TRY(canonicalize_locale_list(global_object, locale_value)); | 
					
						
							| 
									
										
										
										
											2021-08-24 22:58:45 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 4. If options is undefined, throw a TypeError exception.
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |     if (options_value.is_undefined()) | 
					
						
							|  |  |  |         return vm.throw_completion<TypeError>(global_object, ErrorType::IsUndefined, "options"sv); | 
					
						
							| 
									
										
										
										
											2021-08-24 22:58:45 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 5. Set options to ? GetOptionsObject(options).
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |     auto* options = TRY(Temporal::get_options_object(global_object, options_value)); | 
					
						
							| 
									
										
										
										
											2021-08-24 22:58:45 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 6. Let opt be a new Record.
 | 
					
						
							|  |  |  |     LocaleOptions opt {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 7. Let localeData be %DisplayNames%.[[LocaleData]].
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 8. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |     auto matcher = TRY(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, { "lookup"sv, "best fit"sv }, "best fit"sv)); | 
					
						
							| 
									
										
										
										
											2021-08-24 22:58:45 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 9. Set opt.[[localeMatcher]] to matcher.
 | 
					
						
							|  |  |  |     opt.locale_matcher = matcher; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 10. Let r be ResolveLocale(%DisplayNames%.[[AvailableLocales]], requestedLocales, opt, %DisplayNames%.[[RelevantExtensionKeys]]).
 | 
					
						
							|  |  |  |     auto result = resolve_locale(requested_locales, opt, {}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 11. Let style be ? GetOption(options, "style", "string", « "narrow", "short", "long" », "long").
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |     auto style = TRY(get_option(global_object, *options, vm.names.style, Value::Type::String, { "narrow"sv, "short"sv, "long"sv }, "long"sv)); | 
					
						
							| 
									
										
										
										
											2021-08-24 22:58:45 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 12. Set displayNames.[[Style]] to style.
 | 
					
						
							|  |  |  |     display_names->set_style(style.as_string().string()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-12 13:52:51 -05:00
										 |  |  |     // 13. Let type be ? GetOption(options, "type", "string", « "language", "region", "script", "currency", "calendar", "dateTimeField" », undefined).
 | 
					
						
							|  |  |  |     auto type = TRY(get_option(global_object, *options, vm.names.type, Value::Type::String, { "language"sv, "region"sv, "script"sv, "currency"sv, "calendar"sv, "dateTimeField"sv }, Empty {})); | 
					
						
							| 
									
										
										
										
											2021-08-24 22:58:45 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 14. If type is undefined, throw a TypeError exception.
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |     if (type.is_undefined()) | 
					
						
							|  |  |  |         return vm.throw_completion<TypeError>(global_object, ErrorType::IsUndefined, "options.type"sv); | 
					
						
							| 
									
										
										
										
											2021-08-24 22:58:45 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 15. Set displayNames.[[Type]] to type.
 | 
					
						
							|  |  |  |     display_names->set_type(type.as_string().string()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 16. Let fallback be ? GetOption(options, "fallback", "string", « "code", "none" », "code").
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |     auto fallback = TRY(get_option(global_object, *options, vm.names.fallback, Value::Type::String, { "code"sv, "none"sv }, "code"sv)); | 
					
						
							| 
									
										
										
										
											2021-08-24 22:58:45 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 17. Set displayNames.[[Fallback]] to fallback.
 | 
					
						
							|  |  |  |     display_names->set_fallback(fallback.as_string().string()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 18. Set displayNames.[[Locale]] to r.[[locale]].
 | 
					
						
							|  |  |  |     display_names->set_locale(move(result.locale)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-12 13:52:51 -05:00
										 |  |  |     // Note: Several of the steps below are skipped in favor of deferring to LibUnicode.
 | 
					
						
							| 
									
										
										
										
											2021-08-24 22:58:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-12 13:52:51 -05:00
										 |  |  |     // 19. Let dataLocale be r.[[dataLocale]].
 | 
					
						
							|  |  |  |     // 20. Let dataLocaleData be localeData.[[<dataLocale>]].
 | 
					
						
							|  |  |  |     // 21. Let types be dataLocaleData.[[types]].
 | 
					
						
							|  |  |  |     // 22. Assert: types is a Record (see 12.4.3).
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 23. Let languageDisplay be ? GetOption(options, "languageDisplay", "string", « "dialect", "standard" », "dialect").
 | 
					
						
							|  |  |  |     auto language_display = TRY(get_option(global_object, *options, vm.names.languageDisplay, Value::Type::String, { "dialect"sv, "standard"sv }, "dialect"sv)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 24. Let typeFields be types.[[<type>]].
 | 
					
						
							|  |  |  |     // 25. Assert: typeFields is a Record (see 12.4.3).
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 26. If type is "language", then
 | 
					
						
							|  |  |  |     if (display_names->type() == DisplayNames::Type::Language) { | 
					
						
							|  |  |  |         // a. Set displayNames.[[LanguageDisplay]] to languageDisplay.
 | 
					
						
							|  |  |  |         display_names->set_language_display(language_display.as_string().string()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // b. Let typeFields be typeFields.[[<languageDisplay>]].
 | 
					
						
							|  |  |  |         // c. Assert: typeFields is a Record (see 12.4.3).
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 27. Let styleFields be typeFields.[[<style>]].
 | 
					
						
							|  |  |  |     // 28. Assert: styleFields is a Record (see 12.4.3).
 | 
					
						
							|  |  |  |     // 29. Set displayNames.[[Fields]] to styleFields.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 30. Return displayNames.
 | 
					
						
							| 
									
										
										
										
											2021-08-24 22:27:05 -04:00
										 |  |  |     return display_names; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-04 17:08:57 +01:00
										 |  |  | // 12.3.2 Intl.DisplayNames.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-Intl.DisplayNames.supportedLocalesOf
 | 
					
						
							| 
									
										
										
										
											2021-10-22 22:48:25 +01:00
										 |  |  | JS_DEFINE_NATIVE_FUNCTION(DisplayNamesConstructor::supported_locales_of) | 
					
						
							| 
									
										
										
										
											2021-09-04 17:08:57 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto locales = vm.argument(0); | 
					
						
							|  |  |  |     auto options = vm.argument(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 1. Let availableLocales be %DisplayNames%.[[AvailableLocales]].
 | 
					
						
							|  |  |  |     // No-op, availability of each requested locale is checked via Unicode::is_locale_available()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
 | 
					
						
							| 
									
										
										
										
											2021-10-22 22:48:25 +01:00
										 |  |  |     auto requested_locales = TRY(canonicalize_locale_list(global_object, locales)); | 
					
						
							| 
									
										
										
										
											2021-09-04 17:08:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
 | 
					
						
							| 
									
										
										
										
											2021-10-22 22:48:25 +01:00
										 |  |  |     return TRY(supported_locales(global_object, requested_locales, options)); | 
					
						
							| 
									
										
										
										
											2021-09-04 17:08:57 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 22:27:05 -04:00
										 |  |  | } |