From dd483d84f856fbaace636734b0cae682c4964696 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 15 Sep 2021 23:53:35 +0100 Subject: [PATCH] LibJS: Convert to_seconds_string_precision() to ThrowCompletionOr --- .../LibJS/Runtime/Temporal/AbstractOperations.cpp | 8 ++++---- .../Libraries/LibJS/Runtime/Temporal/AbstractOperations.h | 2 +- .../Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp | 8 +++----- .../LibJS/Runtime/Temporal/PlainTimePrototype.cpp | 8 +++----- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp index f389aa6943..0d8a2652b2 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp @@ -286,14 +286,14 @@ ThrowCompletionOr to_temporal_rounding_increment(GlobalObject& global_objec } // 13.16 ToSecondsStringPrecision ( normalizedOptions ), https://tc39.es/proposal-temporal/#sec-temporal-tosecondsstringprecision -Optional to_seconds_string_precision(GlobalObject& global_object, Object const& normalized_options) +ThrowCompletionOr to_seconds_string_precision(GlobalObject& global_object, Object const& normalized_options) { auto& vm = global_object.vm(); // Let smallestUnit be ? ToSmallestTemporalUnit(normalizedOptions, « "year", "month", "week", "day", "hour" », undefined). auto smallest_unit = to_smallest_temporal_unit(global_object, normalized_options, { "year"sv, "month"sv, "week"sv, "day"sv, "hour"sv }, {}); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // 2. If smallestUnit is "minute", then if (smallest_unit == "minute"sv) { @@ -329,7 +329,7 @@ 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 = TRY_OR_DISCARD(get_string_or_number_option(global_object, normalized_options, vm.names.fractionalSecondDigits, { "auto"sv }, 0, 9, js_string(vm, "auto"sv))); + auto digits_variant = TRY(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()) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h index ec5b6f9c27..c92bce6c93 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h @@ -93,7 +93,7 @@ ThrowCompletionOr to_temporal_overflow(GlobalObject&, Object const& norm ThrowCompletionOr to_temporal_rounding_mode(GlobalObject&, Object const& normalized_options, String const& fallback); ThrowCompletionOr to_show_calendar_option(GlobalObject&, Object const& normalized_options); ThrowCompletionOr to_temporal_rounding_increment(GlobalObject&, Object const& normalized_options, Optional dividend, bool inclusive); -Optional to_seconds_string_precision(GlobalObject&, Object const& normalized_options); +ThrowCompletionOr 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); void validate_temporal_unit_range(GlobalObject&, StringView largest_unit, StringView smallest_unit); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp index 011637fb18..63978f1bf2 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp @@ -410,15 +410,13 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::to_string) } // 6. Let precision be ? ToSecondsStringPrecision(options). - auto precision = to_seconds_string_precision(global_object, *options); - if (vm.exception()) - return {}; + auto precision = TRY_OR_DISCARD(to_seconds_string_precision(global_object, *options)); // 7. Let roundingMode be ? ToTemporalRoundingMode(options, "trunc"). auto rounding_mode = TRY_OR_DISCARD(to_temporal_rounding_mode(global_object, *options, "trunc"sv)); // 8. Let roundedNs be ? RoundTemporalInstant(instant.[[Nanoseconds]], precision.[[Increment]], precision.[[Unit]], roundingMode). - auto* rounded_ns = round_temporal_instant(global_object, instant->nanoseconds(), precision->increment, precision->unit, rounding_mode); + auto* rounded_ns = round_temporal_instant(global_object, instant->nanoseconds(), precision.increment, precision.unit, rounding_mode); if (vm.exception()) return {}; @@ -426,7 +424,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::to_string) auto* rounded_instant = create_temporal_instant(global_object, *rounded_ns); // 10. Return ? TemporalInstantToString(roundedInstant, timeZone, precision.[[Precision]]). - auto string = temporal_instant_to_string(global_object, *rounded_instant, time_zone, precision->precision); + auto string = temporal_instant_to_string(global_object, *rounded_instant, time_zone, precision.precision); if (vm.exception()) return {}; diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp index d9a9f84bc3..2821656afe 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp @@ -348,18 +348,16 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::to_string) auto* options = TRY_OR_DISCARD(get_options_object(global_object, vm.argument(0))); // 4. Let precision be ? ToSecondsStringPrecision(options). - auto precision = to_seconds_string_precision(global_object, *options); - if (vm.exception()) - return {}; + auto precision = TRY_OR_DISCARD(to_seconds_string_precision(global_object, *options)); // 5. Let roundingMode be ? ToTemporalRoundingMode(options, "trunc"). auto rounding_mode = TRY_OR_DISCARD(to_temporal_rounding_mode(global_object, *options, "trunc"sv)); // 6. Let roundResult be ! RoundTime(temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], precision.[[Increment]], precision.[[Unit]], roundingMode). - auto round_result = round_time(global_object, temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond(), precision->increment, precision->unit, rounding_mode); + auto round_result = round_time(global_object, temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond(), precision.increment, precision.unit, rounding_mode); // 7. Return ! TemporalTimeToString(roundResult.[[Hour]], roundResult.[[Minute]], roundResult.[[Second]], roundResult.[[Millisecond]], roundResult.[[Microsecond]], roundResult.[[Nanosecond]], precision.[[Precision]]). - auto string = temporal_time_to_string(round_result.hour, round_result.minute, round_result.second, round_result.millisecond, round_result.microsecond, round_result.nanosecond, precision->precision); + auto string = temporal_time_to_string(round_result.hour, round_result.minute, round_result.second, round_result.millisecond, round_result.microsecond, round_result.nanosecond, precision.precision); return js_string(vm, move(string)); }