diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp index 0d8a2652b2..1349004b8a 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp @@ -380,7 +380,7 @@ static HashMap plural_to_singular_units = { }; // 13.17 ToLargestTemporalUnit ( normalizedOptions, disallowedUnits, fallback [ , autoValue ] ), https://tc39.es/proposal-temporal/#sec-temporal-tolargesttemporalunit -Optional to_largest_temporal_unit(GlobalObject& global_object, Object const& normalized_options, Vector const& disallowed_units, String const& fallback, Optional auto_value) +ThrowCompletionOr to_largest_temporal_unit(GlobalObject& global_object, Object const& normalized_options, Vector const& disallowed_units, String const& fallback, Optional auto_value) { auto& vm = global_object.vm(); @@ -391,13 +391,13 @@ Optional to_largest_temporal_unit(GlobalObject& global_object, Object co // 4. Assert: autoValue is not present or disallowedUnits does not contain autoValue. // 5. Let largestUnit be ? GetOption(normalizedOptions, "largestUnit", « String », « "auto", "year", "years", "month", "months", "week", "weeks", "day", "days", "hour", "hours", "minute", "minutes", "second", "seconds", "millisecond", "milliseconds", "microsecond", "microseconds", "nanosecond", "nanoseconds" », fallback). - auto largest_unit_value = TRY_OR_DISCARD(get_option(global_object, normalized_options, vm.names.largestUnit, { OptionType::String }, { "auto"sv, "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 }, js_string(vm, fallback))); + auto largest_unit_value = TRY(get_option(global_object, normalized_options, vm.names.largestUnit, { OptionType::String }, { "auto"sv, "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 }, js_string(vm, fallback))); auto largest_unit = largest_unit_value.as_string().string(); // 6. If largestUnit is "auto" and autoValue is present, then if (largest_unit == "auto"sv && auto_value.has_value()) { // a. Return autoValue. - return auto_value; + return *auto_value; } // 7. If largestUnit is in the Plural column of Table 12, then @@ -409,8 +409,7 @@ Optional to_largest_temporal_unit(GlobalObject& global_object, Object co // 8. If disallowedUnits contains largestUnit, then if (disallowed_units.contains_slow(largest_unit)) { // a. Throw a RangeError exception. - vm.throw_exception(global_object, ErrorType::OptionIsNotValidValue, largest_unit, vm.names.largestUnit.as_string()); - return {}; + return vm.throw_completion(global_object, ErrorType::OptionIsNotValidValue, largest_unit, vm.names.largestUnit.as_string()); } // 9. Return largestUnit. diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h index c92bce6c93..92e60b1d92 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h @@ -94,7 +94,7 @@ ThrowCompletionOr to_temporal_rounding_mode(GlobalObject&, Object const& ThrowCompletionOr to_show_calendar_option(GlobalObject&, Object const& normalized_options); ThrowCompletionOr to_temporal_rounding_increment(GlobalObject&, Object const& normalized_options, Optional dividend, bool inclusive); 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); +ThrowCompletionOr 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); String larger_of_two_temporal_units(StringView, StringView); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp index 63978f1bf2..90337f8567 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp @@ -201,12 +201,10 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::until) auto default_largest_unit = larger_of_two_temporal_units("second"sv, *smallest_unit); // 7. Let largestUnit be ? ToLargestTemporalUnit(options, « "year", "month", "week", "day" », "auto", defaultLargestUnit). - auto largest_unit = to_largest_temporal_unit(global_object, *options, { "year"sv, "month"sv, "week"sv, "day"sv }, "auto"sv, move(default_largest_unit)); - if (vm.exception()) - return {}; + auto largest_unit = TRY_OR_DISCARD(to_largest_temporal_unit(global_object, *options, { "year"sv, "month"sv, "week"sv, "day"sv }, "auto"sv, move(default_largest_unit))); // 8. Perform ? ValidateTemporalUnitRange(largestUnit, smallestUnit). - validate_temporal_unit_range(global_object, *largest_unit, *smallest_unit); + validate_temporal_unit_range(global_object, largest_unit, *smallest_unit); if (vm.exception()) return {}; @@ -223,7 +221,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::until) auto rounded_ns = difference_instant(global_object, instant->nanoseconds(), other->nanoseconds(), rounding_increment, *smallest_unit, rounding_mode); // 13. Let result be ! BalanceDuration(0, 0, 0, 0, 0, 0, roundedNs, largestUnit). - auto result = balance_duration(global_object, 0, 0, 0, 0, 0, 0, *rounded_ns, *largest_unit); + auto result = balance_duration(global_object, 0, 0, 0, 0, 0, 0, *rounded_ns, largest_unit); // 14. Return ? CreateTemporalDuration(0, 0, 0, 0, result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]). return create_temporal_duration(global_object, 0, 0, 0, 0, result->hours, result->minutes, result->seconds, result->milliseconds, result->microseconds, result->nanoseconds); @@ -255,12 +253,10 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::since) auto default_largest_unit = larger_of_two_temporal_units("second"sv, *smallest_unit); // 7. Let largestUnit be ? ToLargestTemporalUnit(options, « "year", "month", "week", "day" », "auto", defaultLargestUnit). - auto largest_unit = to_largest_temporal_unit(global_object, *options, { "year"sv, "month"sv, "week"sv, "day"sv }, "auto"sv, move(default_largest_unit)); - if (vm.exception()) - return {}; + auto largest_unit = TRY_OR_DISCARD(to_largest_temporal_unit(global_object, *options, { "year"sv, "month"sv, "week"sv, "day"sv }, "auto"sv, move(default_largest_unit))); // 8. Perform ? ValidateTemporalUnitRange(largestUnit, smallestUnit). - validate_temporal_unit_range(global_object, *largest_unit, *smallest_unit); + validate_temporal_unit_range(global_object, largest_unit, *smallest_unit); if (vm.exception()) return {}; @@ -277,7 +273,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::since) auto rounded_ns = difference_instant(global_object, other->nanoseconds(), instant->nanoseconds(), rounding_increment, *smallest_unit, rounding_mode); // 13. Let result be ! BalanceDuration(0, 0, 0, 0, 0, 0, roundedNs, largestUnit). - auto result = balance_duration(global_object, 0, 0, 0, 0, 0, 0, *rounded_ns, *largest_unit); + auto result = balance_duration(global_object, 0, 0, 0, 0, 0, 0, *rounded_ns, largest_unit); // 14. Return ? CreateTemporalDuration(0, 0, 0, 0, result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]). return create_temporal_duration(global_object, 0, 0, 0, 0, result->hours, result->minutes, result->seconds, result->milliseconds, result->microseconds, result->nanoseconds);