diff options
author | Linus Groh <mail@linusgroh.de> | 2021-09-15 23:53:35 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-09-16 22:34:24 +0100 |
commit | dd483d84f856fbaace636734b0cae682c4964696 (patch) | |
tree | 37c645a4885e69515cec471ada67ab15fd497b39 /Userland/Libraries/LibJS | |
parent | f8f074f8a9ba41347c5fa2d8e0d45a6f4ccb7186 (diff) | |
download | serenity-dd483d84f856fbaace636734b0cae682c4964696.zip |
LibJS: Convert to_seconds_string_precision() to ThrowCompletionOr
Diffstat (limited to 'Userland/Libraries/LibJS')
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<u64> to_temporal_rounding_increment(GlobalObject& global_objec } // 13.16 ToSecondsStringPrecision ( normalizedOptions ), https://tc39.es/proposal-temporal/#sec-temporal-tosecondsstringprecision -Optional<SecondsStringPrecision> to_seconds_string_precision(GlobalObject& global_object, Object const& normalized_options) +ThrowCompletionOr<SecondsStringPrecision> 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<SecondsStringPrecision> 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<u8>(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<u8>(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<String>()) { 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<String> to_temporal_overflow(GlobalObject&, Object const& norm 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<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); +ThrowCompletionOr<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); 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)); } |