Bläddra i källkod

LibJS: Convert to_temporal_rounding_increment() to ThrowCompletionOr

Linus Groh 3 år sedan
förälder
incheckning
f8f074f8a9

+ 6 - 10
Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp

@@ -238,7 +238,7 @@ ThrowCompletionOr<String> 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<double> dividend, bool inclusive)
+ThrowCompletionOr<u64> to_temporal_rounding_increment(GlobalObject& global_object, Object const& normalized_options, Optional<double> 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<RangeError>(global_object, ErrorType::OptionIsNotValidValue, increment, "roundingIncrement");
-        return {};
-    }
+    if (increment < 1 || increment > maximum)
+        return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, increment, "roundingIncrement");
 
     // 7. Set increment to floor(ℝ(increment)).
     auto floored_increment = static_cast<u64>(increment);
 
     // 8. If dividend is not undefined and dividend modulo increment is not zero, then
-    if (dividend.has_value() && static_cast<u64>(*dividend) % floored_increment != 0) {
+    if (dividend.has_value() && static_cast<u64>(*dividend) % floored_increment != 0)
         // a. Throw a RangeError exception.
-        vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, increment, "roundingIncrement");
-        return {};
-    }
+        return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, increment, "roundingIncrement");
 
     // 9. Return increment.
     return floored_increment;

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h

@@ -92,7 +92,7 @@ ThrowCompletionOr<Variant<String, NumberType>> get_string_or_number_option(Globa
 ThrowCompletionOr<String> to_temporal_overflow(GlobalObject&, Object const& normalized_options);
 ThrowCompletionOr<String> to_temporal_rounding_mode(GlobalObject&, Object const& normalized_options, String const& fallback);
 ThrowCompletionOr<String> to_show_calendar_option(GlobalObject&, Object const& normalized_options);
-u64 to_temporal_rounding_increment(GlobalObject&, Object const& normalized_options, Optional<double> dividend, bool inclusive);
+ThrowCompletionOr<u64> to_temporal_rounding_increment(GlobalObject&, Object const& normalized_options, Optional<double> dividend, bool inclusive);
 Optional<SecondsStringPrecision> to_seconds_string_precision(GlobalObject&, Object const& normalized_options);
 Optional<String> to_largest_temporal_unit(GlobalObject&, Object const& normalized_options, Vector<StringView> const& disallowed_units, String const& fallback, Optional<String> auto_value);
 Optional<String> to_smallest_temporal_unit(GlobalObject&, Object const& normalized_options, Vector<StringView> const& disallowed_units, Optional<String> fallback);

+ 3 - 9
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);