LibJS+LibLocale: Replace canonical locales and display names with ICU

Note: We keep locale parsing and syntactic validation as-is. ECMA-402
places additional restrictions on locales above what is required by the
Unicode spec. ICU doesn't provide methods that let us easily check those
restrictions, whereas LibLocale does. Other browsers also implement
their own validators here.

This introduces a locale cache to re-use parsed locale data and various
related structures (not doing so has a non-negligible performance impact
on Intl tests).

The existing APIs for canonicalization and display names are pretty
intertwined, so they must both be adapted at once here. The results of
canonicalization are slightly different on some edge cases. But the
changed results are actually now aligned with Chrome and Safari.
This commit is contained in:
Timothy Flynn 2024-06-08 11:22:05 -04:00 committed by Andreas Kling
commit 9724a25daf
Notes: sideshowbarker 2024-07-17 08:37:36 +09:00
23 changed files with 693 additions and 1361 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2021-2024, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -10,6 +10,7 @@
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibJS/Runtime/Object.h>
#include <LibLocale/DisplayNames.h>
#include <LibLocale/Locale.h>
namespace JS::Intl {
@ -34,11 +35,6 @@ class DisplayNames final : public Object {
Code,
};
enum class LanguageDisplay {
Dialect,
Standard,
};
public:
virtual ~DisplayNames() override = default;
@ -58,18 +54,18 @@ public:
StringView fallback_string() const;
bool has_language_display() const { return m_language_display.has_value(); }
LanguageDisplay language_display() const { return *m_language_display; }
void set_language_display(StringView language_display);
StringView language_display_string() const;
::Locale::LanguageDisplay language_display() const { return *m_language_display; }
void set_language_display(StringView language_display) { m_language_display = ::Locale::language_display_from_string(language_display); }
StringView language_display_string() const { return ::Locale::language_display_to_string(*m_language_display); }
private:
DisplayNames(Object& prototype);
String m_locale; // [[Locale]]
::Locale::Style m_style { ::Locale::Style::Long }; // [[Style]]
Type m_type { Type::Invalid }; // [[Type]]
Fallback m_fallback { Fallback::Invalid }; // [[Fallback]]
Optional<LanguageDisplay> m_language_display {}; // [[LanguageDisplay]]
String m_locale; // [[Locale]]
::Locale::Style m_style { ::Locale::Style::Long }; // [[Style]]
Type m_type { Type::Invalid }; // [[Type]]
Fallback m_fallback { Fallback::Invalid }; // [[Fallback]]
Optional<::Locale::LanguageDisplay> m_language_display; // [[LanguageDisplay]]
};
ThrowCompletionOr<Value> canonical_code_for_display_names(VM&, DisplayNames::Type, StringView code);