1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 09:57:34 +00:00

LibJS: Convert to_temporal_rounding_increment() to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-09-15 23:50:43 +01:00
parent 65d26dd25e
commit f8f074f8a9
3 changed files with 10 additions and 20 deletions

View file

@ -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 // 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(); 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). // 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()); VERIFY(increment_value.is_number());
auto increment = increment_value.as_double(); auto increment = increment_value.as_double();
// 6. If increment < 1 or increment > maximum, throw a RangeError exception. // 6. If increment < 1 or increment > maximum, throw a RangeError exception.
if (increment < 1 || increment > maximum) { if (increment < 1 || increment > maximum)
vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, increment, "roundingIncrement"); return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, increment, "roundingIncrement");
return {};
}
// 7. Set increment to floor((increment)). // 7. Set increment to floor((increment)).
auto floored_increment = static_cast<u64>(increment); auto floored_increment = static_cast<u64>(increment);
// 8. If dividend is not undefined and dividend modulo increment is not zero, then // 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. // a. Throw a RangeError exception.
vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, increment, "roundingIncrement"); return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, increment, "roundingIncrement");
return {};
}
// 9. Return increment. // 9. Return increment.
return floored_increment; return floored_increment;

View file

@ -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_overflow(GlobalObject&, Object const& normalized_options);
ThrowCompletionOr<String> to_temporal_rounding_mode(GlobalObject&, Object const& normalized_options, String const& fallback); 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); 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<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_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); Optional<String> to_smallest_temporal_unit(GlobalObject&, Object const& normalized_options, Vector<StringView> const& disallowed_units, Optional<String> fallback);

View file

@ -217,9 +217,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::until)
auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit); auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
// 11. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false). // 11. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false).
auto rounding_increment = to_temporal_rounding_increment(global_object, *options, *maximum, false); auto rounding_increment = TRY_OR_DISCARD(to_temporal_rounding_increment(global_object, *options, *maximum, false));
if (vm.exception())
return {};
// 12. Let roundedNs be ! DifferenceInstant(instant.[[Nanoseconds]], other.[[Nanoseconds]], roundingIncrement, smallestUnit, roundingMode). // 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); 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); auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
// 11. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false). // 11. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false).
auto rounding_increment = to_temporal_rounding_increment(global_object, *options, *maximum, false); auto rounding_increment = TRY_OR_DISCARD(to_temporal_rounding_increment(global_object, *options, *maximum, false));
if (vm.exception())
return {};
// 12. Let roundedNs be ! DifferenceInstant(other.[[Nanoseconds]], instant.[[Nanoseconds]], roundingIncrement, smallestUnit, roundingMode). // 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); 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). // 14. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, true).
auto rounding_increment = to_temporal_rounding_increment(global_object, *options, maximum, true); auto rounding_increment = TRY_OR_DISCARD(to_temporal_rounding_increment(global_object, *options, maximum, true));
if (vm.exception())
return {};
// 15. Let roundedNs be ? RoundTemporalInstant(instant.[[Nanoseconds]], roundingIncrement, smallestUnit, roundingMode). // 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); auto* rounded_ns = round_temporal_instant(global_object, instant->nanoseconds(), rounding_increment, smallest_unit, rounding_mode);