From d2588d852bbe65d10d0596b7554ab7b49cbf06b7 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 29 Nov 2021 19:11:49 -0500 Subject: [PATCH] LibJS: Change all [[RelevantExtensionKeys]] to return constexpr arrays There's no need to allocate a vector for this internal slot. Similar to commit: bb1143779275f3dfa652c88c11ac2ad6e69ac9cd --- .../LibJS/Runtime/Intl/AbstractOperations.cpp | 2 +- .../LibJS/Runtime/Intl/AbstractOperations.h | 2 +- .../Libraries/LibJS/Runtime/Intl/DateTimeFormat.cpp | 8 -------- .../Libraries/LibJS/Runtime/Intl/DateTimeFormat.h | 8 +++++++- Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp | 12 ------------ Userland/Libraries/LibJS/Runtime/Intl/Locale.h | 12 +++++++++++- .../LibJS/Runtime/Intl/LocaleConstructor.cpp | 8 ++++---- .../Libraries/LibJS/Runtime/Intl/NumberFormat.cpp | 9 --------- Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h | 8 +++++++- 9 files changed, 31 insertions(+), 38 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp index 3b91b5d810..d0ebfe07ee 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp @@ -373,7 +373,7 @@ static auto& find_key_in_value(T& value, StringView key) } // 9.2.7 ResolveLocale ( availableLocales, requestedLocales, options, relevantExtensionKeys, localeData ), https://tc39.es/ecma402/#sec-resolvelocale -LocaleResult resolve_locale(Vector const& requested_locales, LocaleOptions const& options, Vector const& relevant_extension_keys) +LocaleResult resolve_locale(Vector const& requested_locales, LocaleOptions const& options, Span relevant_extension_keys) { // 1. Let matcher be options.[[localeMatcher]]. auto const& matcher = options.locale_matcher; diff --git a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h index 36e7b0eb92..5edddd55eb 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h @@ -46,7 +46,7 @@ bool is_well_formed_unit_identifier(StringView unit_identifier); ThrowCompletionOr> canonicalize_locale_list(GlobalObject&, Value locales); Optional best_available_locale(StringView locale); String insert_unicode_extension_and_canonicalize(Unicode::LocaleID locale_id, Unicode::LocaleExtension extension); -LocaleResult resolve_locale(Vector const& requested_locales, LocaleOptions const& options, Vector const& relevant_extension_keys); +LocaleResult resolve_locale(Vector const& requested_locales, LocaleOptions const& options, Span relevant_extension_keys); Vector lookup_supported_locales(Vector const& requested_locales); Vector best_fit_supported_locales(Vector const& requested_locales); ThrowCompletionOr supported_locales(GlobalObject&, Vector const& requested_locales, Value options); diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.cpp index 60e5b75070..cff3f7cde4 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.cpp @@ -12,14 +12,6 @@ namespace JS::Intl { -Vector const& DateTimeFormat::relevant_extension_keys() -{ - // 11.3.3 Internal slots, https://tc39.es/ecma402/#sec-intl.datetimeformat-internal-slots - // The value of the [[RelevantExtensionKeys]] internal slot is « "ca", "hc", "nu" ». - static Vector relevant_extension_keys { "ca"sv, "hc"sv, "nu"sv }; - return relevant_extension_keys; -} - // 11 DateTimeFormat Objects, https://tc39.es/ecma402/#datetimeformat-objects DateTimeFormat::DateTimeFormat(Object& prototype) : Object(prototype) diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.h b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.h index d8cc8dce0d..16ff75ff38 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include #include @@ -32,7 +33,12 @@ public: Short, }; - static Vector const& relevant_extension_keys(); // [[RelevantExtensionKeys]] + static constexpr auto relevant_extension_keys() + { + // 11.3.3 Internal slots, https://tc39.es/ecma402/#sec-intl.datetimeformat-internal-slots + // The value of the [[RelevantExtensionKeys]] internal slot is « "ca", "hc", "nu" ». + return AK::Array { "ca"sv, "hc"sv, "nu"sv }; + } DateTimeFormat(Object& prototype); virtual ~DateTimeFormat() override = default; diff --git a/Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp b/Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp index 130245699f..23c7be195d 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp @@ -15,18 +15,6 @@ Locale* Locale::create(GlobalObject& global_object, Unicode::LocaleID const& loc return global_object.heap().allocate(global_object, locale_id, *global_object.intl_locale_prototype()); } -Vector const& Locale::relevant_extension_keys() -{ - // 14.2.2 Internal slots, https://tc39.es/ecma402/#sec-intl.locale-internal-slots - // The value of the [[RelevantExtensionKeys]] internal slot is « "ca", "co", "hc", "kf", "kn", "nu" ». - // If %Collator%.[[RelevantExtensionKeys]] does not contain "kf", then remove "kf" from %Locale%.[[RelevantExtensionKeys]]. - // If %Collator%.[[RelevantExtensionKeys]] does not contain "kn", then remove "kn" from %Locale%.[[RelevantExtensionKeys]]. - - // FIXME: We do not yet have an Intl.Collator object. For now, we behave as if "kf" and "kn" exist, as test262 depends on it. - static Vector relevant_extension_keys { "ca"sv, "co"sv, "hc"sv, "kf"sv, "kn"sv, "nu"sv }; - return relevant_extension_keys; -} - // 14 Locale Objects, https://tc39.es/ecma402/#locale-objects Locale::Locale(Object& prototype) : Object(prototype) diff --git a/Userland/Libraries/LibJS/Runtime/Intl/Locale.h b/Userland/Libraries/LibJS/Runtime/Intl/Locale.h index 5d4213a8f3..e54beb3184 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/Locale.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/Locale.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include #include @@ -21,7 +22,16 @@ class Locale final : public Object { public: static Locale* create(GlobalObject&, Unicode::LocaleID const&); - static Vector const& relevant_extension_keys(); // [[RelevantExtensionKeys]] + static constexpr auto relevant_extension_keys() + { + // 14.2.2 Internal slots, https://tc39.es/ecma402/#sec-intl.locale-internal-slots + // The value of the [[RelevantExtensionKeys]] internal slot is « "ca", "co", "hc", "kf", "kn", "nu" ». + // If %Collator%.[[RelevantExtensionKeys]] does not contain "kf", then remove "kf" from %Locale%.[[RelevantExtensionKeys]]. + // If %Collator%.[[RelevantExtensionKeys]] does not contain "kn", then remove "kn" from %Locale%.[[RelevantExtensionKeys]]. + + // FIXME: We do not yet have an Intl.Collator object. For now, we behave as if "kf" and "kn" exist, as test262 depends on it. + return AK::Array { "ca"sv, "co"sv, "hc"sv, "kf"sv, "kn"sv, "nu"sv }; + } Locale(Object& prototype); Locale(Unicode::LocaleID const&, Object& prototype); diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.cpp index 9e323818bb..4970b78f07 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.cpp @@ -108,7 +108,7 @@ static ThrowCompletionOr apply_options_to_tag(GlobalObject& global_objec } // 14.1.2 ApplyUnicodeExtensionToTag ( tag, options, relevantExtensionKeys ), https://tc39.es/ecma402/#sec-apply-unicode-extension-to-tag -static LocaleAndKeys apply_unicode_extension_to_tag(StringView tag, LocaleAndKeys options, Vector const& relevant_extension_keys) +static LocaleAndKeys apply_unicode_extension_to_tag(StringView tag, LocaleAndKeys options, Span relevant_extension_keys) { // 1. Assert: Type(tag) is String. // 2. Assert: tag matches the unicode_locale_id production. @@ -253,7 +253,7 @@ ThrowCompletionOr LocaleConstructor::construct(FunctionObject& new_targ auto options_value = vm.argument(1); // 2. Let relevantExtensionKeys be %Locale%.[[RelevantExtensionKeys]]. - auto const& relevant_extension_keys = Locale::relevant_extension_keys(); + auto relevant_extension_keys = Locale::relevant_extension_keys(); // 3. Let internalSlotsList be « [[InitializedLocale]], [[Locale]], [[Calendar]], [[Collation]], [[HourCycle]], [[NumberingSystem]] ». // 4. If relevantExtensionKeys contains "kf", then @@ -341,14 +341,14 @@ ThrowCompletionOr LocaleConstructor::construct(FunctionObject& new_targ locale->set_hour_cycle(result.hc.release_value()); // 34. If relevantExtensionKeys contains "kf", then - if (relevant_extension_keys.contains_slow("kf"sv)) { + if (relevant_extension_keys.span().contains_slow("kf"sv)) { // a. Set locale.[[CaseFirst]] to r.[[kf]]. if (result.kf.has_value()) locale->set_case_first(result.kf.release_value()); } // 35. If relevantExtensionKeys contains "kn", then - if (relevant_extension_keys.contains_slow("kn"sv)) { + if (relevant_extension_keys.span().contains_slow("kn"sv)) { // a. If ! SameValue(r.[[kn]], "true") is true or r.[[kn]] is the empty String, then if (result.kn.has_value() && (same_value(js_string(vm, *result.kn), js_string(vm, "true")) || result.kn->is_empty())) { // i. Set locale.[[Numeric]] to true. diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp index ba10f46402..2de65ec445 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp @@ -4,7 +4,6 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include #include #include #include @@ -17,14 +16,6 @@ namespace JS::Intl { -Vector const& NumberFormat::relevant_extension_keys() -{ - // 15.3.3 Internal slots, https://tc39.es/ecma402/#sec-intl.numberformat-internal-slots - // The value of the [[RelevantExtensionKeys]] internal slot is « "nu" ». - static Vector relevant_extension_keys { "nu"sv }; - return relevant_extension_keys; -} - // 15 NumberFormat Objects, https://tc39.es/ecma402/#numberformat-objects NumberFormat::NumberFormat(Object& prototype) : Object(prototype) diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h index 21cae9b6aa..986ce177d2 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include #include @@ -72,7 +73,12 @@ public: ExceptZero, }; - static Vector const& relevant_extension_keys(); // [[RelevantExtensionKeys]] + static constexpr auto relevant_extension_keys() + { + // 15.3.3 Internal slots, https://tc39.es/ecma402/#sec-intl.numberformat-internal-slots + // The value of the [[RelevantExtensionKeys]] internal slot is « "nu" ». + return AK::Array { "nu"sv }; + } NumberFormat(Object& prototype); virtual ~NumberFormat() override = default;