PluralRulesConstructor.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2022, 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. // 16.1 The Intl.PluralRules Constructor, https://tc39.es/ecma402/#sec-intl-pluralrules-constructor
  16. PluralRulesConstructor::PluralRulesConstructor(GlobalObject& global_object)
  17. : NativeFunction(vm().names.PluralRules.as_string(), *global_object.function_prototype())
  18. {
  19. }
  20. void PluralRulesConstructor::initialize(GlobalObject& global_object)
  21. {
  22. NativeFunction::initialize(global_object);
  23. auto& vm = this->vm();
  24. // 16.2.1 Intl.PluralRules.prototype, https://tc39.es/ecma402/#sec-intl.pluralrules.prototype
  25. define_direct_property(vm.names.prototype, global_object.intl_plural_rules_prototype(), 0);
  26. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  27. u8 attr = Attribute::Writable | Attribute::Configurable;
  28. define_native_function(vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  29. }
  30. // 16.1.1 Intl.PluralRules ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.pluralrules
  31. ThrowCompletionOr<Value> PluralRulesConstructor::call()
  32. {
  33. // 1. If NewTarget is undefined, throw a TypeError exception.
  34. return vm().throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Intl.PluralRules");
  35. }
  36. // 16.1.1 Intl.PluralRules ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.pluralrules
  37. ThrowCompletionOr<Object*> PluralRulesConstructor::construct(FunctionObject& new_target)
  38. {
  39. auto& vm = this->vm();
  40. auto& global_object = this->global_object();
  41. auto locales = vm.argument(0);
  42. auto options = vm.argument(1);
  43. // 2. Let pluralRules be ? OrdinaryCreateFromConstructor(NewTarget, "%PluralRules.prototype%", « [[InitializedPluralRules]], [[Locale]], [[Type]], [[MinimumIntegerDigits]], [[MinimumFractionDigits]], [[MaximumFractionDigits]], [[MinimumSignificantDigits]], [[MaximumSignificantDigits]], [[RoundingType]] »).
  44. auto* plural_rules = TRY(ordinary_create_from_constructor<PluralRules>(global_object, new_target, &GlobalObject::intl_plural_rules_prototype));
  45. // 3. Return ? InitializePluralRules(pluralRules, locales, options).
  46. return TRY(initialize_plural_rules(global_object, *plural_rules, locales, options));
  47. }
  48. // 16.2.2 Intl.PluralRules.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.pluralrules.supportedlocalesof
  49. JS_DEFINE_NATIVE_FUNCTION(PluralRulesConstructor::supported_locales_of)
  50. {
  51. auto locales = vm.argument(0);
  52. auto options = vm.argument(1);
  53. // 1. Let availableLocales be %PluralRules%.[[AvailableLocales]].
  54. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  55. auto requested_locales = TRY(canonicalize_locale_list(global_object, locales));
  56. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  57. return TRY(supported_locales(global_object, requested_locales, options));
  58. }
  59. // 16.1.2 InitializePluralRules ( pluralRules, locales, options ), https://tc39.es/ecma402/#sec-initializepluralrules
  60. ThrowCompletionOr<PluralRules*> initialize_plural_rules(GlobalObject& global_object, PluralRules& plural_rules, Value locales_value, Value options_value)
  61. {
  62. auto& vm = global_object.vm();
  63. // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  64. auto requested_locales = TRY(canonicalize_locale_list(global_object, locales_value));
  65. // 2. Set options to ? CoerceOptionsToObject(options).
  66. auto* options = TRY(coerce_options_to_object(global_object, options_value));
  67. // 3. Let opt be a new Record.
  68. LocaleOptions opt {};
  69. // 4. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
  70. auto matcher = TRY(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, AK::Array { "lookup"sv, "best fit"sv }, "best fit"sv));
  71. // 5. Set opt.[[localeMatcher]] to matcher.
  72. opt.locale_matcher = matcher;
  73. // 6. Let t be ? GetOption(options, "type", "string", « "cardinal", "ordinal" », "cardinal").
  74. auto type = TRY(get_option(global_object, *options, vm.names.type, Value::Type::String, AK::Array { "cardinal"sv, "ordinal"sv }, "cardinal"sv));
  75. // 7. Set pluralRules.[[Type]] to t.
  76. plural_rules.set_type(type.as_string().string());
  77. // 8. Perform ? SetNumberFormatDigitOptions(pluralRules, options, +0𝔽, 3𝔽, "standard").
  78. TRY(set_number_format_digit_options(global_object, plural_rules, *options, 0, 3, NumberFormat::Notation::Standard));
  79. // 9. Let localeData be %PluralRules%.[[LocaleData]].
  80. // 10. Let r be ResolveLocale(%PluralRules%.[[AvailableLocales]], requestedLocales, opt, %PluralRules%.[[RelevantExtensionKeys]], localeData).
  81. auto result = resolve_locale(requested_locales, opt, {});
  82. // 11. Set pluralRules.[[Locale]] to r.[[locale]].
  83. plural_rules.set_locale(move(result.locale));
  84. // Non-standard, the data locale is used by our NumberFormat implementation.
  85. plural_rules.set_data_locale(move(result.data_locale));
  86. // 12. Return pluralRules.
  87. return &plural_rules;
  88. }
  89. }