diff options
author | Linus Groh <mail@linusgroh.de> | 2021-09-16 00:05:01 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-09-16 22:34:24 +0100 |
commit | 448a3642102620470a677719c08bd8ef28636c76 (patch) | |
tree | 0598548d457e03dcfe0ca9ba8825d2c96bd0bff5 /Userland/Libraries/LibJS | |
parent | 92187591dd70286925caa1892e79541056c577d5 (diff) | |
download | serenity-448a3642102620470a677719c08bd8ef28636c76.zip |
LibJS: Convert to_smallest_temporal_unit() to ThrowCompletionOr
Diffstat (limited to 'Userland/Libraries/LibJS')
3 files changed, 10 insertions, 19 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp index 1349004b8a..a44b732c54 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp @@ -291,9 +291,7 @@ ThrowCompletionOr<SecondsStringPrecision> to_seconds_string_precision(GlobalObje 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 (auto* exception = vm.exception()) - return throw_completion(exception->value()); + auto smallest_unit = TRY(to_smallest_temporal_unit(global_object, normalized_options, { "year"sv, "month"sv, "week"sv, "day"sv, "hour"sv }, {})); // 2. If smallestUnit is "minute", then if (smallest_unit == "minute"sv) { @@ -417,18 +415,18 @@ ThrowCompletionOr<String> to_largest_temporal_unit(GlobalObject& global_object, } // 13.18 ToSmallestTemporalUnit ( normalizedOptions, disallowedUnits, fallback ), https://tc39.es/proposal-temporal/#sec-temporal-tosmallesttemporalunit -Optional<String> to_smallest_temporal_unit(GlobalObject& global_object, Object const& normalized_options, Vector<StringView> const& disallowed_units, Optional<String> fallback) +ThrowCompletionOr<Optional<String>> to_smallest_temporal_unit(GlobalObject& global_object, Object const& normalized_options, Vector<StringView> const& disallowed_units, Optional<String> fallback) { auto& vm = global_object.vm(); // 1. Assert: disallowedUnits does not contain fallback. // 2. Let smallestUnit be ? GetOption(normalizedOptions, "smallestUnit", « String », « "year", "years", "month", "months", "week", "weeks", "day", "days", "hour", "hours", "minute", "minutes", "second", "seconds", "millisecond", "milliseconds", "microsecond", "microseconds", "nanosecond", "nanoseconds" », fallback). - auto smallest_unit_value = TRY_OR_DISCARD(get_option(global_object, normalized_options, vm.names.smallestUnit, { OptionType::String }, { "year"sv, "years"sv, "month"sv, "months"sv, "week"sv, "weeks"sv, "day"sv, "days"sv, "hour"sv, "hours"sv, "minute"sv, "minutes"sv, "second"sv, "seconds"sv, "millisecond"sv, "milliseconds"sv, "microsecond"sv, "microseconds"sv, "nanosecond"sv, "nanoseconds"sv }, fallback.has_value() ? js_string(vm, *fallback) : js_undefined())); + auto smallest_unit_value = TRY(get_option(global_object, normalized_options, vm.names.smallestUnit, { OptionType::String }, { "year"sv, "years"sv, "month"sv, "months"sv, "week"sv, "weeks"sv, "day"sv, "days"sv, "hour"sv, "hours"sv, "minute"sv, "minutes"sv, "second"sv, "seconds"sv, "millisecond"sv, "milliseconds"sv, "microsecond"sv, "microseconds"sv, "nanosecond"sv, "nanoseconds"sv }, fallback.has_value() ? js_string(vm, *fallback) : js_undefined())); // OPTIMIZATION: We skip the following string-only checks for the fallback to tidy up the code a bit if (smallest_unit_value.is_undefined()) - return {}; + return Optional<String> {}; VERIFY(smallest_unit_value.is_string()); auto smallest_unit = smallest_unit_value.as_string().string(); @@ -441,12 +439,11 @@ Optional<String> to_smallest_temporal_unit(GlobalObject& global_object, Object c // 4. If disallowedUnits contains smallestUnit, then if (disallowed_units.contains_slow(smallest_unit)) { // a. Throw a RangeError exception. - vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, smallest_unit, vm.names.smallestUnit.as_string()); - return {}; + return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, smallest_unit, vm.names.smallestUnit.as_string()); } // 5. Return smallestUnit. - return smallest_unit; + return { smallest_unit }; } // 13.22 ValidateTemporalUnitRange ( largestUnit, smallestUnit ), https://tc39.es/proposal-temporal/#sec-temporal-validatetemporalunitrange diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h index 92e60b1d92..36f9f2ffec 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h @@ -95,7 +95,7 @@ ThrowCompletionOr<String> to_show_calendar_option(GlobalObject&, Object const& n ThrowCompletionOr<u64> to_temporal_rounding_increment(GlobalObject&, Object const& normalized_options, Optional<double> dividend, bool inclusive); ThrowCompletionOr<SecondsStringPrecision> to_seconds_string_precision(GlobalObject&, Object const& normalized_options); ThrowCompletionOr<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); +ThrowCompletionOr<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); String larger_of_two_temporal_units(StringView, StringView); Optional<u16> maximum_temporal_duration_rounding_increment(StringView unit); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp index 90337f8567..9c9fb377a7 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp @@ -193,9 +193,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::until) auto* options = TRY_OR_DISCARD(get_options_object(global_object, vm.argument(1))); // 5. Let smallestUnit be ? ToSmallestTemporalUnit(options, « "year", "month", "week", "day" », "nanosecond"). - auto smallest_unit = to_smallest_temporal_unit(global_object, *options, { "year"sv, "month"sv, "week"sv, "day"sv }, "nanosecond"sv); - if (vm.exception()) - return {}; + auto smallest_unit = TRY_OR_DISCARD(to_smallest_temporal_unit(global_object, *options, { "year"sv, "month"sv, "week"sv, "day"sv }, "nanosecond"sv)); // 6. Let defaultLargestUnit be ! LargerOfTwoTemporalUnits("second", smallestUnit). auto default_largest_unit = larger_of_two_temporal_units("second"sv, *smallest_unit); @@ -245,9 +243,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::since) auto* options = TRY_OR_DISCARD(get_options_object(global_object, vm.argument(1))); // 5. Let smallestUnit be ? ToSmallestTemporalUnit(options, « "year", "month", "week", "day" », "nanosecond"). - auto smallest_unit = to_smallest_temporal_unit(global_object, *options, { "year"sv, "month"sv, "week"sv, "day"sv }, "nanosecond"sv); - if (vm.exception()) - return {}; + auto smallest_unit = TRY_OR_DISCARD(to_smallest_temporal_unit(global_object, *options, { "year"sv, "month"sv, "week"sv, "day"sv }, "nanosecond"sv)); // 6. Let defaultLargestUnit be ! LargerOfTwoTemporalUnits("second", smallestUnit). auto default_largest_unit = larger_of_two_temporal_units("second"sv, *smallest_unit); @@ -299,9 +295,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::round) auto* options = TRY_OR_DISCARD(get_options_object(global_object, vm.argument(0))); // 5. Let smallestUnit be ? ToSmallestTemporalUnit(options, « "year", "month", "week", "day" », undefined). - auto smallest_unit_value = to_smallest_temporal_unit(global_object, *options, { "year"sv, "month"sv, "week"sv, "day"sv }, {}); - if (vm.exception()) - return {}; + auto smallest_unit_value = TRY_OR_DISCARD(to_smallest_temporal_unit(global_object, *options, { "year"sv, "month"sv, "week"sv, "day"sv }, {})); // 6. If smallestUnit is undefined, throw a RangeError exception. if (!smallest_unit_value.has_value()) { |