From bb1143779275f3dfa652c88c11ac2ad6e69ac9cd Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 27 Nov 2021 10:12:58 -0500 Subject: [PATCH] LibJS: Change Intl's GetOption AO to accept a Span rather than a Vector Allocating a Vector for each of these invocations is a bit silly when the values are basically all compile-time arrays. This AO is used even more heavily by Intl.DateTimeFormat, so change it to accept a Span to reduce its cost. This also adds an overload to accept a fixed-size C-array so callers do not have to be prefixed with AK::Array, i.e. this: get_option(..., AK::Array { "a"sv, "b"sv }, ...); Reduces to: get_option(..., { "a"sv, "b"sv }, ...); (Which is how all call sites were already written to construct a Vector in place). --- .../Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp | 2 +- .../Libraries/LibJS/Runtime/Intl/AbstractOperations.h | 9 ++++++++- .../Libraries/LibJS/Runtime/Intl/LocaleConstructor.cpp | 6 +++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp index 6e99f34408..2c08b22495 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp @@ -597,7 +597,7 @@ ThrowCompletionOr coerce_options_to_object(GlobalObject& global_object, } // 9.2.13 GetOption ( options, property, type, values, fallback ), https://tc39.es/ecma402/#sec-getoption -ThrowCompletionOr get_option(GlobalObject& global_object, Object const& options, PropertyKey const& property, Value::Type type, Vector const& values, Fallback fallback) +ThrowCompletionOr get_option(GlobalObject& global_object, Object const& options, PropertyKey const& property, Value::Type type, Span values, Fallback fallback) { auto& vm = global_object.vm(); diff --git a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h index dbd75a3930..47bf132297 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include #include @@ -46,9 +47,15 @@ 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); ThrowCompletionOr coerce_options_to_object(GlobalObject& global_object, Value options); -ThrowCompletionOr get_option(GlobalObject& global_object, Object const& options, PropertyKey const& property, Value::Type type, Vector const& values, Fallback fallback); +ThrowCompletionOr get_option(GlobalObject& global_object, Object const& options, PropertyKey const& property, Value::Type type, Span values, Fallback fallback); ThrowCompletionOr> default_number_option(GlobalObject& global_object, Value value, int minimum, int maximum, Optional fallback); ThrowCompletionOr> get_number_option(GlobalObject& global_object, Object const& options, PropertyKey const& property, int minimum, int maximum, Optional fallback); Vector partition_pattern(StringView pattern); +template +ThrowCompletionOr get_option(GlobalObject& global_object, Object const& options, PropertyKey const& property, Value::Type type, StringView const (&values)[Size], Fallback fallback) +{ + return get_option(global_object, options, property, type, Span { values }, fallback); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.cpp index 25cd2946d4..9e323818bb 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.cpp @@ -26,7 +26,7 @@ struct LocaleAndKeys { }; // Note: This is not an AO in the spec. This just serves to abstract very similar steps in ApplyOptionsToTag and the Intl.Locale constructor. -static ThrowCompletionOr> get_string_option(GlobalObject& global_object, Object const& options, PropertyKey const& property, Function validator, Vector const& values = {}) +static ThrowCompletionOr> get_string_option(GlobalObject& global_object, Object const& options, PropertyKey const& property, Function validator, Span values = {}) { auto& vm = global_object.vm(); @@ -305,11 +305,11 @@ ThrowCompletionOr LocaleConstructor::construct(FunctionObject& new_targ // 19. Let hc be ? GetOption(options, "hourCycle", "string", « "h11", "h12", "h23", "h24" », undefined). // 20. Set opt.[[hc]] to hc. - opt.hc = TRY(get_string_option(global_object, *options, vm.names.hourCycle, nullptr, { "h11"sv, "h12"sv, "h23"sv, "h24"sv })); + opt.hc = TRY(get_string_option(global_object, *options, vm.names.hourCycle, nullptr, AK::Array { "h11"sv, "h12"sv, "h23"sv, "h24"sv })); // 21. Let kf be ? GetOption(options, "caseFirst", "string", « "upper", "lower", "false" », undefined). // 22. Set opt.[[kf]] to kf. - opt.kf = TRY(get_string_option(global_object, *options, vm.names.caseFirst, nullptr, { "upper"sv, "lower"sv, "false"sv })); + opt.kf = TRY(get_string_option(global_object, *options, vm.names.caseFirst, nullptr, AK::Array { "upper"sv, "lower"sv, "false"sv })); // 23. Let kn be ? GetOption(options, "numeric", "boolean", undefined, undefined). auto kn = TRY(get_option(global_object, *options, vm.names.numeric, Value::Type::Boolean, {}, Empty {}));