PluralRulesConstructor.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright (c) 2022, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Intl/PluralRules.h>
  9. #include <LibJS/Runtime/Intl/PluralRulesConstructor.h>
  10. namespace JS::Intl {
  11. // 16.2 The Intl.PluralRules Constructor, https://tc39.es/ecma402/#sec-intl-pluralrules-constructor
  12. PluralRulesConstructor::PluralRulesConstructor(GlobalObject& global_object)
  13. : NativeFunction(vm().names.PluralRules.as_string(), *global_object.function_prototype())
  14. {
  15. }
  16. void PluralRulesConstructor::initialize(GlobalObject& global_object)
  17. {
  18. NativeFunction::initialize(global_object);
  19. auto& vm = this->vm();
  20. // 16.3.1 Intl.PluralRules.prototype, https://tc39.es/ecma402/#sec-intl.pluralrules.prototype
  21. define_direct_property(vm.names.prototype, global_object.intl_plural_rules_prototype(), 0);
  22. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  23. }
  24. // 16.2.1 Intl.PluralRules ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.pluralrules
  25. ThrowCompletionOr<Value> PluralRulesConstructor::call()
  26. {
  27. // 1. If NewTarget is undefined, throw a TypeError exception.
  28. return vm().throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Intl.PluralRules");
  29. }
  30. // 16.2.1 Intl.PluralRules ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.pluralrules
  31. ThrowCompletionOr<Object*> PluralRulesConstructor::construct(FunctionObject& new_target)
  32. {
  33. auto& global_object = this->global_object();
  34. // 2. Let pluralRules be ? OrdinaryCreateFromConstructor(NewTarget, "%PluralRules.prototype%", « [[InitializedPluralRules]], [[Locale]], [[Type]], [[MinimumIntegerDigits]], [[MinimumFractionDigits]], [[MaximumFractionDigits]], [[MinimumSignificantDigits]], [[MaximumSignificantDigits]], [[RoundingType]] »).
  35. auto* plural_rules = TRY(ordinary_create_from_constructor<PluralRules>(global_object, new_target, &GlobalObject::intl_plural_rules_prototype));
  36. // 3. Return ? InitializePluralRules(pluralRules, locales, options).
  37. return plural_rules;
  38. }
  39. }