Intl.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/DateTimeFormatConstructor.h>
  10. #include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
  11. #include <LibJS/Runtime/Intl/Intl.h>
  12. #include <LibJS/Runtime/Intl/ListFormatConstructor.h>
  13. #include <LibJS/Runtime/Intl/LocaleConstructor.h>
  14. #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
  15. namespace JS::Intl {
  16. // 8 The Intl Object, https://tc39.es/ecma402/#intl-object
  17. Intl::Intl(GlobalObject& global_object)
  18. : Object(*global_object.object_prototype())
  19. {
  20. }
  21. void Intl::initialize(GlobalObject& global_object)
  22. {
  23. Object::initialize(global_object);
  24. auto& vm = this->vm();
  25. // 8.1.1 Intl[ @@toStringTag ], https://tc39.es/ecma402/#sec-Intl-toStringTag
  26. define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Intl"), Attribute::Configurable);
  27. u8 attr = Attribute::Writable | Attribute::Configurable;
  28. define_direct_property(vm.names.DateTimeFormat, global_object.intl_date_time_format_constructor(), attr);
  29. define_direct_property(vm.names.DisplayNames, global_object.intl_display_names_constructor(), attr);
  30. define_direct_property(vm.names.ListFormat, global_object.intl_list_format_constructor(), attr);
  31. define_direct_property(vm.names.Locale, global_object.intl_locale_constructor(), attr);
  32. define_direct_property(vm.names.NumberFormat, global_object.intl_number_format_constructor(), attr);
  33. define_native_function(vm.names.getCanonicalLocales, get_canonical_locales, 1, attr);
  34. }
  35. // 8.3.1 Intl.getCanonicalLocales ( locales ), https://tc39.es/ecma402/#sec-intl.getcanonicallocales
  36. JS_DEFINE_NATIVE_FUNCTION(Intl::get_canonical_locales)
  37. {
  38. auto locales = vm.argument(0);
  39. // 1. Let ll be ? CanonicalizeLocaleList(locales).
  40. auto locale_list = TRY(canonicalize_locale_list(global_object, locales));
  41. MarkedValueList marked_locale_list { vm.heap() };
  42. marked_locale_list.ensure_capacity(locale_list.size());
  43. for (auto& locale : locale_list)
  44. marked_locale_list.append(js_string(vm, move(locale)));
  45. // 2. Return CreateArrayFromList(ll).
  46. return Array::create_from(global_object, marked_locale_list);
  47. }
  48. }