mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:17:35 +00:00
LibJS: Convert validate_temporal_unit_range() to ThrowCompletionOr
This commit is contained in:
parent
448a364210
commit
8dd45a1ba2
3 changed files with 15 additions and 26 deletions
|
@ -447,64 +447,57 @@ ThrowCompletionOr<Optional<String>> to_smallest_temporal_unit(GlobalObject& glob
|
|||
}
|
||||
|
||||
// 13.22 ValidateTemporalUnitRange ( largestUnit, smallestUnit ), https://tc39.es/proposal-temporal/#sec-temporal-validatetemporalunitrange
|
||||
void validate_temporal_unit_range(GlobalObject& global_object, StringView largest_unit, StringView smallest_unit)
|
||||
ThrowCompletionOr<void> validate_temporal_unit_range(GlobalObject& global_object, StringView largest_unit, StringView smallest_unit)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
// 1. If smallestUnit is "year" and largestUnit is not "year", then
|
||||
if (smallest_unit == "year"sv && largest_unit != "year"sv) {
|
||||
// a. Throw a RangeError exception.
|
||||
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
return;
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
}
|
||||
// 2. If smallestUnit is "month" and largestUnit is not "year" or "month", then
|
||||
if (smallest_unit == "month"sv && !largest_unit.is_one_of("year"sv, "month"sv)) {
|
||||
// a. Throw a RangeError exception.
|
||||
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
return;
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
}
|
||||
// 3. If smallestUnit is "week" and largestUnit is not one of "year", "month", or "week", then
|
||||
if (smallest_unit == "week"sv && !largest_unit.is_one_of("year"sv, "month"sv, "week"sv)) {
|
||||
// a. Throw a RangeError exception.
|
||||
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
return;
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
}
|
||||
// 4. If smallestUnit is "day" and largestUnit is not one of "year", "month", "week", or "day", then
|
||||
if (smallest_unit == "day"sv && !largest_unit.is_one_of("year"sv, "month"sv, "week"sv, "day"sv)) {
|
||||
// a. Throw a RangeError exception.
|
||||
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
return;
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
}
|
||||
// 5. If smallestUnit is "hour" and largestUnit is not one of "year", "month", "week", "day", or "hour", then
|
||||
if (smallest_unit == "hour"sv && !largest_unit.is_one_of("year"sv, "month"sv, "week"sv, "day"sv, "hour"sv)) {
|
||||
// a. Throw a RangeError exception.
|
||||
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
return;
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
}
|
||||
// 6. If smallestUnit is "minute" and largestUnit is "second", "millisecond", "microsecond", or "nanosecond", then
|
||||
if (smallest_unit == "minute"sv && largest_unit.is_one_of("second"sv, "millisecond"sv, "microsecond"sv, "nanosecond"sv)) {
|
||||
// a. Throw a RangeError exception.
|
||||
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
return;
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
}
|
||||
// 7. If smallestUnit is "second" and largestUnit is "millisecond", "microsecond", or "nanosecond", then
|
||||
if (smallest_unit == "second"sv && largest_unit.is_one_of("millisecond"sv, "microsecond"sv, "nanosecond"sv)) {
|
||||
// a. Throw a RangeError exception.
|
||||
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
return;
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
}
|
||||
// 8. If smallestUnit is "millisecond" and largestUnit is "microsecond" or "nanosecond", then
|
||||
if (smallest_unit == "millisecond"sv && largest_unit.is_one_of("microsecond"sv, "nanosecond"sv)) {
|
||||
// a. Throw a RangeError exception.
|
||||
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
return;
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
}
|
||||
// 9. If smallestUnit is "microsecond" and largestUnit is "nanosecond", then
|
||||
if (smallest_unit == "microsecond"sv && largest_unit == "nanosecond"sv) {
|
||||
// a. Throw a RangeError exception.
|
||||
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
return;
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
// 13.23 LargerOfTwoTemporalUnits ( u1, u2 ), https://tc39.es/proposal-temporal/#sec-temporal-largeroftwotemporalunits
|
||||
|
|
|
@ -96,7 +96,7 @@ ThrowCompletionOr<u64> to_temporal_rounding_increment(GlobalObject&, Object cons
|
|||
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);
|
||||
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);
|
||||
ThrowCompletionOr<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);
|
||||
void reject_temporal_calendar_type(GlobalObject&, Object&);
|
||||
|
|
|
@ -202,9 +202,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::until)
|
|||
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);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
TRY_OR_DISCARD(validate_temporal_unit_range(global_object, largest_unit, *smallest_unit));
|
||||
|
||||
// 9. Let roundingMode be ? ToTemporalRoundingMode(options, "trunc").
|
||||
auto rounding_mode = TRY_OR_DISCARD(to_temporal_rounding_mode(global_object, *options, "trunc"sv));
|
||||
|
@ -252,9 +250,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::since)
|
|||
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);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
TRY_OR_DISCARD(validate_temporal_unit_range(global_object, largest_unit, *smallest_unit));
|
||||
|
||||
// 9. Let roundingMode be ? ToTemporalRoundingMode(options, "trunc").
|
||||
auto rounding_mode = TRY_OR_DISCARD(to_temporal_rounding_mode(global_object, *options, "trunc"sv));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue