From f8f074f8a9ba41347c5fa2d8e0d45a6f4ccb7186 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 15 Sep 2021 23:50:43 +0100 Subject: [PATCH] LibJS: Convert to_temporal_rounding_increment() to ThrowCompletionOr --- .../Runtime/Temporal/AbstractOperations.cpp | 16 ++++++---------- .../LibJS/Runtime/Temporal/AbstractOperations.h | 2 +- .../LibJS/Runtime/Temporal/InstantPrototype.cpp | 12 +++--------- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp index f6074ac64c..f389aa6943 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp @@ -238,7 +238,7 @@ ThrowCompletionOr to_show_calendar_option(GlobalObject& global_object, O } // 13.14 ToTemporalRoundingIncrement ( normalizedOptions, dividend, inclusive ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalroundingincrement -u64 to_temporal_rounding_increment(GlobalObject& global_object, Object const& normalized_options, Optional dividend, bool inclusive) +ThrowCompletionOr to_temporal_rounding_increment(GlobalObject& global_object, Object const& normalized_options, Optional dividend, bool inclusive) { auto& vm = global_object.vm(); @@ -265,25 +265,21 @@ u64 to_temporal_rounding_increment(GlobalObject& global_object, Object const& no } // 5. Let increment be ? GetOption(normalizedOptions, "roundingIncrement", « Number », empty, 1). - auto increment_value = TRY_OR_DISCARD(get_option(global_object, normalized_options, vm.names.roundingIncrement, { OptionType::Number }, {}, Value(1))); + auto increment_value = TRY(get_option(global_object, normalized_options, vm.names.roundingIncrement, { OptionType::Number }, {}, Value(1))); VERIFY(increment_value.is_number()); auto increment = increment_value.as_double(); // 6. If increment < 1 or increment > maximum, throw a RangeError exception. - if (increment < 1 || increment > maximum) { - vm.throw_exception(global_object, ErrorType::OptionIsNotValidValue, increment, "roundingIncrement"); - return {}; - } + if (increment < 1 || increment > maximum) + return vm.throw_completion(global_object, ErrorType::OptionIsNotValidValue, increment, "roundingIncrement"); // 7. Set increment to floor(ℝ(increment)). auto floored_increment = static_cast(increment); // 8. If dividend is not undefined and dividend modulo increment is not zero, then - if (dividend.has_value() && static_cast(*dividend) % floored_increment != 0) { + if (dividend.has_value() && static_cast(*dividend) % floored_increment != 0) // a. Throw a RangeError exception. - vm.throw_exception(global_object, ErrorType::OptionIsNotValidValue, increment, "roundingIncrement"); - return {}; - } + return vm.throw_completion(global_object, ErrorType::OptionIsNotValidValue, increment, "roundingIncrement"); // 9. Return increment. return floored_increment; diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h index 8e6c0ca52e..ec5b6f9c27 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h @@ -92,7 +92,7 @@ ThrowCompletionOr> get_string_or_number_option(Globa ThrowCompletionOr to_temporal_overflow(GlobalObject&, Object const& normalized_options); ThrowCompletionOr to_temporal_rounding_mode(GlobalObject&, Object const& normalized_options, String const& fallback); ThrowCompletionOr to_show_calendar_option(GlobalObject&, Object const& normalized_options); -u64 to_temporal_rounding_increment(GlobalObject&, Object const& normalized_options, Optional dividend, bool inclusive); +ThrowCompletionOr to_temporal_rounding_increment(GlobalObject&, Object const& normalized_options, Optional dividend, bool inclusive); Optional to_seconds_string_precision(GlobalObject&, Object const& normalized_options); Optional to_largest_temporal_unit(GlobalObject&, Object const& normalized_options, Vector const& disallowed_units, String const& fallback, Optional auto_value); Optional to_smallest_temporal_unit(GlobalObject&, Object const& normalized_options, Vector const& disallowed_units, Optional fallback); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp index 3332a6b69e..011637fb18 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp @@ -217,9 +217,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::until) auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit); // 11. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false). - auto rounding_increment = to_temporal_rounding_increment(global_object, *options, *maximum, false); - if (vm.exception()) - return {}; + auto rounding_increment = TRY_OR_DISCARD(to_temporal_rounding_increment(global_object, *options, *maximum, false)); // 12. Let roundedNs be ! DifferenceInstant(instant.[[Nanoseconds]], other.[[Nanoseconds]], roundingIncrement, smallestUnit, roundingMode). auto rounded_ns = difference_instant(global_object, instant->nanoseconds(), other->nanoseconds(), rounding_increment, *smallest_unit, rounding_mode); @@ -273,9 +271,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::since) auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit); // 11. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false). - auto rounding_increment = to_temporal_rounding_increment(global_object, *options, *maximum, false); - if (vm.exception()) - return {}; + auto rounding_increment = TRY_OR_DISCARD(to_temporal_rounding_increment(global_object, *options, *maximum, false)); // 12. Let roundedNs be ! DifferenceInstant(other.[[Nanoseconds]], instant.[[Nanoseconds]], roundingIncrement, smallestUnit, roundingMode). auto rounded_ns = difference_instant(global_object, other->nanoseconds(), instant->nanoseconds(), rounding_increment, *smallest_unit, rounding_mode); @@ -357,9 +353,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::round) } // 14. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, true). - auto rounding_increment = to_temporal_rounding_increment(global_object, *options, maximum, true); - if (vm.exception()) - return {}; + auto rounding_increment = TRY_OR_DISCARD(to_temporal_rounding_increment(global_object, *options, maximum, true)); // 15. Let roundedNs be ? RoundTemporalInstant(instant.[[Nanoseconds]], roundingIncrement, smallestUnit, roundingMode). auto* rounded_ns = round_temporal_instant(global_object, instant->nanoseconds(), rounding_increment, smallest_unit, rounding_mode);