Intl.cpp 2.2 KB

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