Intl.cpp 2.6 KB

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