From b1e7e626577d6d8aecdc0a4687ac1166d40ba144 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 15 Sep 2021 23:33:20 +0100 Subject: [PATCH] LibJS: Convert get_string_or_number_option() to ThrowCompletionOr --- .../Runtime/Temporal/AbstractOperations.cpp | 30 ++++++++----------- .../Runtime/Temporal/AbstractOperations.h | 2 +- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp index a29b8b99ee..3eb4227987 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp @@ -171,38 +171,34 @@ ThrowCompletionOr get_option(GlobalObject& global_object, Object const& o // 13.4 GetStringOrNumberOption ( options, property, stringValues, minimum, maximum, fallback ), https://tc39.es/proposal-temporal/#sec-getstringornumberoption template -Optional> get_string_or_number_option(GlobalObject& global_object, Object const& options, PropertyName const& property, Vector const& string_values, NumberType minimum, NumberType maximum, Value fallback) +ThrowCompletionOr> get_string_or_number_option(GlobalObject& global_object, Object const& options, PropertyName const& property, Vector const& string_values, NumberType minimum, NumberType maximum, Value fallback) { auto& vm = global_object.vm(); // 1. Assert: Type(options) is Object. // 2. Let value be ? GetOption(options, property, « Number, String », empty, fallback). - auto value = TRY_OR_DISCARD(get_option(global_object, options, property, { OptionType::Number, OptionType::String }, {}, fallback)); + auto value = TRY(get_option(global_object, options, property, { OptionType::Number, OptionType::String }, {}, fallback)); // 3. If Type(value) is Number, then if (value.is_number()) { // a. If value < minimum or value > maximum, throw a RangeError exception. - if (value.as_double() < minimum || value.as_double() > maximum) { - vm.template throw_exception(global_object, ErrorType::OptionIsNotValidValue, value.as_double(), property.as_string()); - return {}; - } + if (value.as_double() < minimum || value.as_double() > maximum) + return vm.template throw_completion(global_object, ErrorType::OptionIsNotValidValue, value.as_double(), property.as_string()); // b. Return floor(ℝ(value)). - return floor(value.as_double()); + return { static_cast(floor(value.as_double())) }; } // 4. Assert: Type(value) is String. VERIFY(value.is_string()); // 5. If stringValues does not contain value, throw a RangeError exception. - if (!string_values.contains_slow(value.as_string().string())) { - vm.template throw_exception(global_object, ErrorType::OptionIsNotValidValue, value.as_string().string(), property.as_string()); - return {}; - } + if (!string_values.contains_slow(value.as_string().string())) + return vm.template throw_completion(global_object, ErrorType::OptionIsNotValidValue, value.as_string().string(), property.as_string()); // 6. Return value. - return value.as_string().string(); + return { value.as_string().string() }; } // 13.6 ToTemporalOverflow ( normalizedOptions ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaloverflow @@ -337,18 +333,16 @@ Optional to_seconds_string_precision(GlobalObject& globa VERIFY(!smallest_unit.has_value()); // 8. Let digits be ? GetStringOrNumberOption(normalizedOptions, "fractionalSecondDigits", « "auto" », 0, 9, "auto"). - auto digits_variant = get_string_or_number_option(global_object, normalized_options, vm.names.fractionalSecondDigits, { "auto"sv }, 0, 9, js_string(vm, "auto"sv)); - if (vm.exception()) - return {}; + auto digits_variant = TRY_OR_DISCARD(get_string_or_number_option(global_object, normalized_options, vm.names.fractionalSecondDigits, { "auto"sv }, 0, 9, js_string(vm, "auto"sv))); // 9. If digits is "auto", then - if (digits_variant->has()) { - VERIFY(digits_variant->get() == "auto"sv); + if (digits_variant.has()) { + VERIFY(digits_variant.get() == "auto"sv); // a. Return the Record { [[Precision]]: "auto", [[Unit]]: "nanosecond", [[Increment]]: 1 }. return SecondsStringPrecision { .precision = "auto"sv, .unit = "nanosecond"sv, .increment = 1 }; } - auto digits = digits_variant->get(); + auto digits = digits_variant.get(); // 10. If digits is 0, then if (digits == 0) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h index a3fcfaa939..2a4c30b22b 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h @@ -88,7 +88,7 @@ ThrowCompletionOr iterable_to_list_of_type(GlobalObject&, Value ThrowCompletionOr get_options_object(GlobalObject&, Value options); ThrowCompletionOr get_option(GlobalObject&, Object const& options, PropertyName const& property, Vector const& types, Vector const& values, Value fallback); template -Optional> get_string_or_number_option(GlobalObject&, Object const& options, PropertyName const& property, Vector const& string_values, NumberType minimum, NumberType maximum, Value fallback); +ThrowCompletionOr> get_string_or_number_option(GlobalObject&, Object const& options, PropertyName const& property, Vector const& string_values, NumberType minimum, NumberType maximum, Value fallback); Optional to_temporal_overflow(GlobalObject&, Object const& normalized_options); Optional to_temporal_rounding_mode(GlobalObject&, Object const& normalized_options, String const& fallback); Optional to_show_calendar_option(GlobalObject&, Object const& normalized_options);