PluralRulesConstructor.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/Array.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  10. #include <LibJS/Runtime/Intl/PluralRules.h>
  11. #include <LibJS/Runtime/Intl/PluralRulesConstructor.h>
  12. namespace JS::Intl {
  13. // 16.2 The Intl.PluralRules Constructor, https://tc39.es/ecma402/#sec-intl-pluralrules-constructor
  14. PluralRulesConstructor::PluralRulesConstructor(GlobalObject& global_object)
  15. : NativeFunction(vm().names.PluralRules.as_string(), *global_object.function_prototype())
  16. {
  17. }
  18. void PluralRulesConstructor::initialize(GlobalObject& global_object)
  19. {
  20. NativeFunction::initialize(global_object);
  21. auto& vm = this->vm();
  22. // 16.3.1 Intl.PluralRules.prototype, https://tc39.es/ecma402/#sec-intl.pluralrules.prototype
  23. define_direct_property(vm.names.prototype, global_object.intl_plural_rules_prototype(), 0);
  24. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  25. u8 attr = Attribute::Writable | Attribute::Configurable;
  26. define_native_function(vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  27. }
  28. // 16.2.1 Intl.PluralRules ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.pluralrules
  29. ThrowCompletionOr<Value> PluralRulesConstructor::call()
  30. {
  31. // 1. If NewTarget is undefined, throw a TypeError exception.
  32. return vm().throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Intl.PluralRules");
  33. }
  34. // 16.2.1 Intl.PluralRules ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.pluralrules
  35. ThrowCompletionOr<Object*> PluralRulesConstructor::construct(FunctionObject& new_target)
  36. {
  37. auto& vm = this->vm();
  38. auto& global_object = this->global_object();
  39. auto locales = vm.argument(0);
  40. auto options = vm.argument(1);
  41. // 2. Let pluralRules be ? OrdinaryCreateFromConstructor(NewTarget, "%PluralRules.prototype%", « [[InitializedPluralRules]], [[Locale]], [[Type]], [[MinimumIntegerDigits]], [[MinimumFractionDigits]], [[MaximumFractionDigits]], [[MinimumSignificantDigits]], [[MaximumSignificantDigits]], [[RoundingType]] »).
  42. auto* plural_rules = TRY(ordinary_create_from_constructor<PluralRules>(global_object, new_target, &GlobalObject::intl_plural_rules_prototype));
  43. // 3. Return ? InitializePluralRules(pluralRules, locales, options).
  44. return TRY(initialize_plural_rules(global_object, *plural_rules, locales, options));
  45. }
  46. // 16.3.2 Intl.PluralRules.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.pluralrules.supportedlocalesof
  47. JS_DEFINE_NATIVE_FUNCTION(PluralRulesConstructor::supported_locales_of)
  48. {
  49. auto locales = vm.argument(0);
  50. auto options = vm.argument(1);
  51. // 1. Let availableLocales be %PluralRules%.[[AvailableLocales]].
  52. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  53. auto requested_locales = TRY(canonicalize_locale_list(global_object, locales));
  54. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  55. return TRY(supported_locales(global_object, requested_locales, options));
  56. }
  57. }