ソースを参照

LibJS: Convert to_positive_integer() to ThrowCompletionOr

Linus Groh 3 年 前
コミット
9ac426c906

+ 4 - 7
Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp

@@ -1020,18 +1020,17 @@ ThrowCompletionOr<TemporalYearMonth> parse_temporal_year_month_string(GlobalObje
 }
 
 // 13.46 ToPositiveInteger ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-topositiveinteger
-double to_positive_integer(GlobalObject& global_object, Value argument)
+ThrowCompletionOr<double> to_positive_integer(GlobalObject& global_object, Value argument)
 {
     auto& vm = global_object.vm();
 
     // 1. Let integer be ? ToIntegerThrowOnInfinity(argument).
-    auto integer = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, argument, ErrorType::TemporalPropertyMustBePositiveInteger));
+    auto integer = TRY(to_integer_throw_on_infinity(global_object, argument, ErrorType::TemporalPropertyMustBePositiveInteger));
 
     // 2. If integer ≤ 0, then
     if (integer <= 0) {
         // a. Throw a RangeError exception.
-        vm.throw_exception<RangeError>(global_object, ErrorType::TemporalPropertyMustBePositiveInteger);
-        return {};
+        return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalPropertyMustBePositiveInteger);
     }
 
     // 3. Return integer.
@@ -1079,9 +1078,7 @@ Object* prepare_temporal_fields(GlobalObject& global_object, Object const& field
             if (property.is_one_of("year", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond", "eraYear")) {
                 value = Value(TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, value, ErrorType::TemporalPropertyMustBeFinite)));
             } else if (property.is_one_of("month", "day")) {
-                value = Value(to_positive_integer(global_object, value));
-                if (vm.exception())
-                    return {};
+                value = Value(TRY_OR_DISCARD(to_positive_integer(global_object, value)));
             } else if (property.is_one_of("monthCode", "offset", "era")) {
                 value = value.to_primitive_string(global_object);
                 if (vm.exception())

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h

@@ -112,7 +112,7 @@ ThrowCompletionOr<TemporalDuration> parse_temporal_duration_string(GlobalObject&
 ThrowCompletionOr<TemporalTime> parse_temporal_time_string(GlobalObject&, String const& iso_string);
 ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject&, String const& iso_string);
 ThrowCompletionOr<TemporalYearMonth> parse_temporal_year_month_string(GlobalObject&, String const& iso_string);
-double to_positive_integer(GlobalObject&, Value argument);
+ThrowCompletionOr<double> to_positive_integer(GlobalObject&, Value argument);
 Object* prepare_temporal_fields(GlobalObject&, Object const& fields, Vector<String> const& field_names, Vector<StringView> const& required_fields);
 
 // 13.46 ToIntegerThrowOnInfinity ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-tointegerthrowoninfinity

+ 2 - 2
Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp

@@ -157,7 +157,7 @@ double calendar_month(GlobalObject& global_object, Object& calendar, Object& dat
     }
 
     // 4. Return ? ToPositiveInteger(result).
-    return to_positive_integer(global_object, result);
+    return TRY_OR_DISCARD(to_positive_integer(global_object, result));
 }
 
 // 12.1.11 CalendarMonthCode ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmonthcode
@@ -199,7 +199,7 @@ double calendar_day(GlobalObject& global_object, Object& calendar, Object& date_
     }
 
     // 4. Return ? ToPositiveInteger(result).
-    return to_positive_integer(global_object, result);
+    return TRY_OR_DISCARD(to_positive_integer(global_object, result));
 }
 
 // 12.1.13 CalendarDayOfWeek ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendardayofweek