PluralRulesConstructor.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Array.h>
  7. #include <LibJS/Runtime/AbstractOperations.h>
  8. #include <LibJS/Runtime/Array.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  11. #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
  12. #include <LibJS/Runtime/Intl/PluralRules.h>
  13. #include <LibJS/Runtime/Intl/PluralRulesConstructor.h>
  14. namespace JS::Intl {
  15. JS_DEFINE_ALLOCATOR(PluralRulesConstructor);
  16. // 16.1 The Intl.PluralRules Constructor, https://tc39.es/ecma402/#sec-intl-pluralrules-constructor
  17. PluralRulesConstructor::PluralRulesConstructor(Realm& realm)
  18. : NativeFunction(realm.vm().names.PluralRules.as_string(), realm.intrinsics().function_prototype())
  19. {
  20. }
  21. void PluralRulesConstructor::initialize(Realm& realm)
  22. {
  23. Base::initialize(realm);
  24. auto& vm = this->vm();
  25. // 16.2.1 Intl.PluralRules.prototype, https://tc39.es/ecma402/#sec-intl.pluralrules.prototype
  26. define_direct_property(vm.names.prototype, realm.intrinsics().intl_plural_rules_prototype(), 0);
  27. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  28. u8 attr = Attribute::Writable | Attribute::Configurable;
  29. define_native_function(realm, vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  30. }
  31. // 16.1.1 Intl.PluralRules ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.pluralrules
  32. ThrowCompletionOr<Value> PluralRulesConstructor::call()
  33. {
  34. // 1. If NewTarget is undefined, throw a TypeError exception.
  35. return vm().throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Intl.PluralRules");
  36. }
  37. // 16.1.1 Intl.PluralRules ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.pluralrules
  38. ThrowCompletionOr<NonnullGCPtr<Object>> PluralRulesConstructor::construct(FunctionObject& new_target)
  39. {
  40. auto& vm = this->vm();
  41. auto locales_value = vm.argument(0);
  42. auto options_value = vm.argument(1);
  43. // 2. Let pluralRules be ? OrdinaryCreateFromConstructor(NewTarget, "%Intl.PluralRules.prototype%", « [[InitializedPluralRules]], [[Locale]], [[Type]], [[MinimumIntegerDigits]], [[MinimumFractionDigits]], [[MaximumFractionDigits]], [[MinimumSignificantDigits]], [[MaximumSignificantDigits]], [[RoundingType]], [[RoundingIncrement]], [[RoundingMode]], [[ComputedRoundingPriority]], [[TrailingZeroDisplay]] »).
  44. auto plural_rules = TRY(ordinary_create_from_constructor<PluralRules>(vm, new_target, &Intrinsics::intl_plural_rules_prototype));
  45. // 3. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  46. auto requested_locales = TRY(canonicalize_locale_list(vm, locales_value));
  47. // 4. Set options to ? CoerceOptionsToObject(options).
  48. auto* options = TRY(coerce_options_to_object(vm, options_value));
  49. // 5. Let opt be a new Record.
  50. LocaleOptions opt {};
  51. // 6. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit").
  52. auto matcher = TRY(get_option(vm, *options, vm.names.localeMatcher, OptionType::String, AK::Array { "lookup"sv, "best fit"sv }, "best fit"sv));
  53. // 7. Set opt.[[localeMatcher]] to matcher.
  54. opt.locale_matcher = matcher;
  55. // 8. Let r be ResolveLocale(%Intl.PluralRules%.[[AvailableLocales]], requestedLocales, opt, %Intl.PluralRules%.[[RelevantExtensionKeys]], %Intl.PluralRules%.[[LocaleData]]).
  56. auto result = resolve_locale(requested_locales, opt, {});
  57. // 9. Set pluralRules.[[Locale]] to r.[[locale]].
  58. plural_rules->set_locale(move(result.locale));
  59. // 10. Let t be ? GetOption(options, "type", string, « "cardinal", "ordinal" », "cardinal").
  60. auto type = TRY(get_option(vm, *options, vm.names.type, OptionType::String, AK::Array { "cardinal"sv, "ordinal"sv }, "cardinal"sv));
  61. // 11. Set pluralRules.[[Type]] to t.
  62. plural_rules->set_type(type.as_string().utf8_string_view());
  63. // 12. Perform ? SetNumberFormatDigitOptions(pluralRules, options, 0, 3, "standard").
  64. TRY(set_number_format_digit_options(vm, plural_rules, *options, 0, 3, Unicode::Notation::Standard));
  65. // Non-standard, create an ICU number formatter for this Intl object.
  66. auto formatter = Unicode::NumberFormat::create(
  67. plural_rules->locale(),
  68. {},
  69. {},
  70. plural_rules->rounding_options());
  71. formatter->create_plural_rules(plural_rules->type());
  72. plural_rules->set_formatter(move(formatter));
  73. // 13. Return pluralRules.
  74. return plural_rules;
  75. }
  76. // 16.2.2 Intl.PluralRules.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.pluralrules.supportedlocalesof
  77. JS_DEFINE_NATIVE_FUNCTION(PluralRulesConstructor::supported_locales_of)
  78. {
  79. auto locales = vm.argument(0);
  80. auto options = vm.argument(1);
  81. // 1. Let availableLocales be %PluralRules%.[[AvailableLocales]].
  82. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  83. auto requested_locales = TRY(canonicalize_locale_list(vm, locales));
  84. // 3. Return ? FilterLocales(availableLocales, requestedLocales, options).
  85. return TRY(filter_locales(vm, requested_locales, options));
  86. }
  87. }