Intl.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Array.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  9. #include <LibJS/Runtime/Intl/CollatorConstructor.h>
  10. #include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
  11. #include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
  12. #include <LibJS/Runtime/Intl/Intl.h>
  13. #include <LibJS/Runtime/Intl/ListFormatConstructor.h>
  14. #include <LibJS/Runtime/Intl/LocaleConstructor.h>
  15. #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
  16. #include <LibJS/Runtime/Intl/PluralRulesConstructor.h>
  17. #include <LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h>
  18. namespace JS::Intl {
  19. // 8 The Intl Object, https://tc39.es/ecma402/#intl-object
  20. Intl::Intl(GlobalObject& global_object)
  21. : Object(*global_object.object_prototype())
  22. {
  23. }
  24. void Intl::initialize(GlobalObject& global_object)
  25. {
  26. Object::initialize(global_object);
  27. auto& vm = this->vm();
  28. // 8.1.1 Intl[ @@toStringTag ], https://tc39.es/ecma402/#sec-Intl-toStringTag
  29. define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Intl"), Attribute::Configurable);
  30. u8 attr = Attribute::Writable | Attribute::Configurable;
  31. define_direct_property(vm.names.Collator, global_object.intl_collator_constructor(), attr);
  32. define_direct_property(vm.names.DateTimeFormat, global_object.intl_date_time_format_constructor(), attr);
  33. define_direct_property(vm.names.DisplayNames, global_object.intl_display_names_constructor(), attr);
  34. define_direct_property(vm.names.ListFormat, global_object.intl_list_format_constructor(), attr);
  35. define_direct_property(vm.names.Locale, global_object.intl_locale_constructor(), attr);
  36. define_direct_property(vm.names.NumberFormat, global_object.intl_number_format_constructor(), attr);
  37. define_direct_property(vm.names.PluralRules, global_object.intl_plural_rules_constructor(), attr);
  38. define_direct_property(vm.names.RelativeTimeFormat, global_object.intl_relative_time_format_constructor(), attr);
  39. define_native_function(vm.names.getCanonicalLocales, get_canonical_locales, 1, attr);
  40. }
  41. // 8.3.1 Intl.getCanonicalLocales ( locales ), https://tc39.es/ecma402/#sec-intl.getcanonicallocales
  42. JS_DEFINE_NATIVE_FUNCTION(Intl::get_canonical_locales)
  43. {
  44. auto locales = vm.argument(0);
  45. // 1. Let ll be ? CanonicalizeLocaleList(locales).
  46. auto locale_list = TRY(canonicalize_locale_list(global_object, locales));
  47. MarkedValueList marked_locale_list { vm.heap() };
  48. marked_locale_list.ensure_capacity(locale_list.size());
  49. for (auto& locale : locale_list)
  50. marked_locale_list.append(js_string(vm, move(locale)));
  51. // 2. Return CreateArrayFromList(ll).
  52. return Array::create_from(global_object, marked_locale_list);
  53. }
  54. }