NumberFormatConstructor.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. initialize_number_format(global_object, *number_format, locales, options);
  45. if (vm.exception())
  46. return {};
  47. // 4. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then
  48. // a. Let this be the this value.
  49. // b. Return ? ChainNumberFormat(numberFormat, NewTarget, this).
  50. // 5. Return numberFormat.
  51. return number_format;
  52. }
  53. // 15.3.2 Intl.NumberFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.numberformat.supportedlocalesof
  54. JS_DEFINE_NATIVE_FUNCTION(NumberFormatConstructor::supported_locales_of)
  55. {
  56. auto locales = vm.argument(0);
  57. auto options = vm.argument(1);
  58. // 1. Let availableLocales be %NumberFormat%.[[AvailableLocales]].
  59. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  60. auto requested_locales = canonicalize_locale_list(global_object, locales);
  61. if (vm.exception())
  62. return {};
  63. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  64. return supported_locales(global_object, requested_locales, options);
  65. }
  66. }