NumberFormatConstructor.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  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/NumberFormat.h>
  11. #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
  12. namespace JS::Intl {
  13. // 15.2 The Intl.NumberFormat Constructor, https://tc39.es/ecma402/#sec-intl-numberformat-constructor
  14. NumberFormatConstructor::NumberFormatConstructor(GlobalObject& global_object)
  15. : NativeFunction(vm().names.NumberFormat.as_string(), *global_object.function_prototype())
  16. {
  17. }
  18. void NumberFormatConstructor::initialize(GlobalObject& global_object)
  19. {
  20. NativeFunction::initialize(global_object);
  21. auto& vm = this->vm();
  22. // 15.3.1 Intl.NumberFormat.prototype, https://tc39.es/ecma402/#sec-intl.numberformat.prototype
  23. define_direct_property(vm.names.prototype, global_object.intl_number_format_prototype(), 0);
  24. u8 attr = Attribute::Writable | Attribute::Configurable;
  25. define_native_function(vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  26. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  27. }
  28. // 15.2.1 Intl.NumberFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.numberformat
  29. Value NumberFormatConstructor::call()
  30. {
  31. // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
  32. return construct(*this);
  33. }
  34. // 15.2.1 Intl.NumberFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.numberformat
  35. Value NumberFormatConstructor::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 numberFormat be ? OrdinaryCreateFromConstructor(newTarget, "%NumberFormat.prototype%", « [[InitializedNumberFormat]], [[Locale]], [[DataLocale]], [[NumberingSystem]], [[Style]], [[Unit]], [[UnitDisplay]], [[Currency]], [[CurrencyDisplay]], [[CurrencySign]], [[MinimumIntegerDigits]], [[MinimumFractionDigits]], [[MaximumFractionDigits]], [[MinimumSignificantDigits]], [[MaximumSignificantDigits]], [[RoundingType]], [[Notation]], [[CompactDisplay]], [[UseGrouping]], [[SignDisplay]], [[BoundFormat]] »).
  42. auto* number_format = TRY_OR_DISCARD(ordinary_create_from_constructor<NumberFormat>(global_object, new_target, &GlobalObject::intl_number_format_prototype));
  43. // 3. Perform ? InitializeNumberFormat(numberFormat, locales, options).
  44. TRY_OR_DISCARD(initialize_number_format(global_object, *number_format, locales, options));
  45. // 4. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then
  46. // a. Let this be the this value.
  47. // b. Return ? ChainNumberFormat(numberFormat, NewTarget, this).
  48. // 5. Return numberFormat.
  49. return number_format;
  50. }
  51. // 15.3.2 Intl.NumberFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.numberformat.supportedlocalesof
  52. JS_DEFINE_OLD_NATIVE_FUNCTION(NumberFormatConstructor::supported_locales_of)
  53. {
  54. auto locales = vm.argument(0);
  55. auto options = vm.argument(1);
  56. // 1. Let availableLocales be %NumberFormat%.[[AvailableLocales]].
  57. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  58. auto requested_locales = TRY_OR_DISCARD(canonicalize_locale_list(global_object, locales));
  59. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  60. return TRY_OR_DISCARD(supported_locales(global_object, requested_locales, options));
  61. }
  62. }