1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:57:45 +00:00

LibJS: Convert to_integer_throw_on_infinity() to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-09-15 23:03:38 +01:00
parent fea27143e9
commit 683e31e1ff
10 changed files with 46 additions and 125 deletions

View file

@ -1087,9 +1087,7 @@ double to_positive_integer(GlobalObject& global_object, Value argument)
auto& vm = global_object.vm();
// 1. Let integer be ? ToIntegerThrowOnInfinity(argument).
auto integer = to_integer_throw_on_infinity(global_object, argument, ErrorType::TemporalPropertyMustBePositiveInteger);
if (vm.exception())
return {};
auto integer = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, argument, ErrorType::TemporalPropertyMustBePositiveInteger));
// 2. If integer ≤ 0, then
if (integer <= 0) {
@ -1141,9 +1139,7 @@ Object* prepare_temporal_fields(GlobalObject& global_object, Object const& field
// 1. Let Conversion represent the abstract operation named by the Conversion value of the same row.
// 2. Set value to ? Conversion(value).
if (property.is_one_of("year", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond", "eraYear")) {
value = Value(to_integer_throw_on_infinity(global_object, value, ErrorType::TemporalPropertyMustBeFinite));
if (vm.exception())
return {};
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())

View file

@ -11,6 +11,7 @@
#include <AK/String.h>
#include <AK/Variant.h>
#include <LibJS/Forward.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/GlobalObject.h>
namespace JS::Temporal {
@ -116,20 +117,19 @@ Object* prepare_temporal_fields(GlobalObject&, Object const& fields, Vector<Stri
// 13.46 ToIntegerThrowOnInfinity ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-tointegerthrowoninfinity
template<typename... Args>
double to_integer_throw_on_infinity(GlobalObject& global_object, Value argument, ErrorType error_type, Args... args)
ThrowCompletionOr<double> to_integer_throw_on_infinity(GlobalObject& global_object, Value argument, ErrorType error_type, Args... args)
{
auto& vm = global_object.vm();
// 1. Let integer be ? ToIntegerOrInfinity(argument).
auto integer = argument.to_integer_or_infinity(global_object);
if (vm.exception())
return {};
if (auto* exception = vm.exception())
return throw_completion(exception->value());
// 2. If integer is −∞ or +∞ , then
if (Value(integer).is_infinity()) {
// a. Throw a RangeError exception.
vm.throw_exception<RangeError>(global_object, error_type, args...);
return {};
return vm.template throw_completion<RangeError>(global_object, error_type, args...);
}
// 3. Return integer.

View file

@ -138,7 +138,7 @@ double calendar_year(GlobalObject& global_object, Object& calendar, Object& date
}
// 4. Return ? ToIntegerThrowOnInfinity(result).
return to_integer_throw_on_infinity(global_object, result, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.year.as_string(), vm.names.Infinity.as_string());
return TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, result, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.year.as_string(), vm.names.Infinity.as_string()));
}
// 12.1.10 CalendarMonth ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmonth
@ -321,11 +321,8 @@ Value calendar_era_year(GlobalObject& global_object, Object& calendar, Object& d
return {};
// 3. If result is not undefined, set result to ? ToIntegerThrowOnInfinity(result).
if (!result.is_undefined()) {
result = Value(to_integer_throw_on_infinity(global_object, result, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.eraYear.as_string(), "Infinity"sv));
if (vm.exception())
return {};
}
if (!result.is_undefined())
result = Value(TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, result, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.eraYear.as_string(), "Infinity"sv)));
// 4. Return result.
return result;

View file

@ -51,54 +51,34 @@ Value DurationConstructor::construct(FunctionObject& new_target)
auto& global_object = this->global_object();
// 2. Let y be ? ToIntegerThrowOnInfinity(years).
auto y = to_integer_throw_on_infinity(global_object, vm.argument(0), ErrorType::TemporalInvalidDuration);
if (vm.exception())
return {};
auto y = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(0), ErrorType::TemporalInvalidDuration));
// 3. Let mo be ? ToIntegerThrowOnInfinity(months).
auto mo = to_integer_throw_on_infinity(global_object, vm.argument(1), ErrorType::TemporalInvalidDuration);
if (vm.exception())
return {};
auto mo = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(1), ErrorType::TemporalInvalidDuration));
// 4. Let w be ? ToIntegerThrowOnInfinity(weeks).
auto w = to_integer_throw_on_infinity(global_object, vm.argument(2), ErrorType::TemporalInvalidDuration);
if (vm.exception())
return {};
auto w = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(2), ErrorType::TemporalInvalidDuration));
// 5. Let d be ? ToIntegerThrowOnInfinity(days).
auto d = to_integer_throw_on_infinity(global_object, vm.argument(3), ErrorType::TemporalInvalidDuration);
if (vm.exception())
return {};
auto d = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(3), ErrorType::TemporalInvalidDuration));
// 6. Let h be ? ToIntegerThrowOnInfinity(hours).
auto h = to_integer_throw_on_infinity(global_object, vm.argument(4), ErrorType::TemporalInvalidDuration);
if (vm.exception())
return {};
auto h = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(4), ErrorType::TemporalInvalidDuration));
// 7. Let m be ? ToIntegerThrowOnInfinity(minutes).
auto m = to_integer_throw_on_infinity(global_object, vm.argument(5), ErrorType::TemporalInvalidDuration);
if (vm.exception())
return {};
auto m = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(5), ErrorType::TemporalInvalidDuration));
// 8. Let s be ? ToIntegerThrowOnInfinity(seconds).
auto s = to_integer_throw_on_infinity(global_object, vm.argument(6), ErrorType::TemporalInvalidDuration);
if (vm.exception())
return {};
auto s = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(6), ErrorType::TemporalInvalidDuration));
// 9. Let ms be ? ToIntegerThrowOnInfinity(milliseconds).
auto ms = to_integer_throw_on_infinity(global_object, vm.argument(7), ErrorType::TemporalInvalidDuration);
if (vm.exception())
return {};
auto ms = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(7), ErrorType::TemporalInvalidDuration));
// 10. Let mis be ? ToIntegerThrowOnInfinity(microseconds).
auto mis = to_integer_throw_on_infinity(global_object, vm.argument(8), ErrorType::TemporalInvalidDuration);
if (vm.exception())
return {};
auto mis = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(8), ErrorType::TemporalInvalidDuration));
// 11. Let ns be ? ToIntegerThrowOnInfinity(nanoseconds).
auto ns = to_integer_throw_on_infinity(global_object, vm.argument(9), ErrorType::TemporalInvalidDuration);
if (vm.exception())
return {};
auto ns = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(9), ErrorType::TemporalInvalidDuration));
// 12. Return ? CreateTemporalDuration(y, mo, w, d, h, m, s, ms, mis, ns, NewTarget).
return create_temporal_duration(global_object, y, mo, w, d, h, m, s, ms, mis, ns, &new_target);

View file

@ -53,19 +53,13 @@ Value PlainDateConstructor::construct(FunctionObject& new_target)
auto& global_object = this->global_object();
// 2. Let y be ? ToIntegerThrowOnInfinity(isoYear).
auto y = to_integer_throw_on_infinity(global_object, vm.argument(0), ErrorType::TemporalInvalidPlainDate);
if (vm.exception())
return {};
auto y = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(0), ErrorType::TemporalInvalidPlainDate));
// 3. Let m be ? ToIntegerThrowOnInfinity(isoMonth).
auto m = to_integer_throw_on_infinity(global_object, vm.argument(1), ErrorType::TemporalInvalidPlainDate);
if (vm.exception())
return {};
auto m = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(1), ErrorType::TemporalInvalidPlainDate));
// 4. Let d be ? ToIntegerThrowOnInfinity(isoDay).
auto d = to_integer_throw_on_infinity(global_object, vm.argument(2), ErrorType::TemporalInvalidPlainDate);
if (vm.exception())
return {};
auto d = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(2), ErrorType::TemporalInvalidPlainDate));
// 5. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
auto* calendar = to_temporal_calendar_with_iso_default(global_object, vm.argument(3));

View file

@ -53,49 +53,31 @@ Value PlainDateTimeConstructor::construct(FunctionObject& new_target)
auto& global_object = this->global_object();
// 2. Let isoYear be ? ToIntegerThrowOnInfinity(isoYear).
auto iso_year = to_integer_throw_on_infinity(global_object, vm.argument(0), ErrorType::TemporalInvalidPlainDateTime);
if (vm.exception())
return {};
auto iso_year = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(0), ErrorType::TemporalInvalidPlainDateTime));
// 3. Let isoMonth be ? ToIntegerThrowOnInfinity(isoMonth).
auto iso_month = to_integer_throw_on_infinity(global_object, vm.argument(1), ErrorType::TemporalInvalidPlainDateTime);
if (vm.exception())
return {};
auto iso_month = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(1), ErrorType::TemporalInvalidPlainDateTime));
// 4. Let isoDay be ? ToIntegerThrowOnInfinity(isoDay).
auto iso_day = to_integer_throw_on_infinity(global_object, vm.argument(2), ErrorType::TemporalInvalidPlainDateTime);
if (vm.exception())
return {};
auto iso_day = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(2), ErrorType::TemporalInvalidPlainDateTime));
// 5. Let hour be ? ToIntegerThrowOnInfinity(hour).
auto hour = to_integer_throw_on_infinity(global_object, vm.argument(3), ErrorType::TemporalInvalidPlainDateTime);
if (vm.exception())
return {};
auto hour = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(3), ErrorType::TemporalInvalidPlainDateTime));
// 6. Let minute be ? ToIntegerThrowOnInfinity(minute).
auto minute = to_integer_throw_on_infinity(global_object, vm.argument(4), ErrorType::TemporalInvalidPlainDateTime);
if (vm.exception())
return {};
auto minute = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(4), ErrorType::TemporalInvalidPlainDateTime));
// 7. Let second be ? ToIntegerThrowOnInfinity(second).
auto second = to_integer_throw_on_infinity(global_object, vm.argument(5), ErrorType::TemporalInvalidPlainDateTime);
if (vm.exception())
return {};
auto second = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(5), ErrorType::TemporalInvalidPlainDateTime));
// 8. Let millisecond be ? ToIntegerThrowOnInfinity(millisecond).
auto millisecond = to_integer_throw_on_infinity(global_object, vm.argument(6), ErrorType::TemporalInvalidPlainDateTime);
if (vm.exception())
return {};
auto millisecond = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(6), ErrorType::TemporalInvalidPlainDateTime));
// 9. Let microsecond be ? ToIntegerThrowOnInfinity(microsecond).
auto microsecond = to_integer_throw_on_infinity(global_object, vm.argument(7), ErrorType::TemporalInvalidPlainDateTime);
if (vm.exception())
return {};
auto microsecond = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(7), ErrorType::TemporalInvalidPlainDateTime));
// 10. Let nanosecond be ? ToIntegerThrowOnInfinity(nanosecond).
auto nanosecond = to_integer_throw_on_infinity(global_object, vm.argument(8), ErrorType::TemporalInvalidPlainDateTime);
if (vm.exception())
return {};
auto nanosecond = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(8), ErrorType::TemporalInvalidPlainDateTime));
// 11. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
auto* calendar = to_temporal_calendar_with_iso_default(global_object, vm.argument(9));

View file

@ -62,14 +62,10 @@ Value PlainMonthDayConstructor::construct(FunctionObject& new_target)
}
// 3. Let m be ? ToIntegerThrowOnInfinity(isoMonth).
auto m = to_integer_throw_on_infinity(global_object, iso_month, ErrorType::TemporalInvalidPlainMonthDay);
if (vm.exception())
return {};
auto m = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, iso_month, ErrorType::TemporalInvalidPlainMonthDay));
// 4. Let d be ? ToIntegerThrowOnInfinity(isoDay).
auto d = to_integer_throw_on_infinity(global_object, iso_day, ErrorType::TemporalInvalidPlainMonthDay);
if (vm.exception())
return {};
auto d = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, iso_day, ErrorType::TemporalInvalidPlainMonthDay));
// 5. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
auto* calendar = to_temporal_calendar_with_iso_default(global_object, calendar_like);
@ -77,9 +73,7 @@ Value PlainMonthDayConstructor::construct(FunctionObject& new_target)
return {};
// 6. Let ref be ? ToIntegerThrowOnInfinity(referenceISOYear).
auto ref = to_integer_throw_on_infinity(global_object, reference_iso_year, ErrorType::TemporalInvalidPlainMonthDay);
if (vm.exception())
return {};
auto ref = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, reference_iso_year, ErrorType::TemporalInvalidPlainMonthDay));
// IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards.
// This does not change the exposed behavior as the call to CreateTemporalMonthDay will immediately check that these values are valid

View file

@ -154,9 +154,7 @@ ThrowCompletionOr<PartialUnregulatedTemporalTime> to_partial_time(GlobalObject&
any = true;
// ii. Set value to ? ToIntegerThrowOnInfinity(value).
auto value_number = to_integer_throw_on_infinity(global_object, value, ErrorType::TemporalPropertyMustBeFinite);
if (auto* exception = vm.exception())
return throw_completion(exception->value());
auto value_number = TRY(to_integer_throw_on_infinity(global_object, value, ErrorType::TemporalPropertyMustBeFinite));
// iii. Set result's internal slot whose name is the Internal Slot value of the current row to value.
result.*internal_slot = value_number;
@ -384,9 +382,7 @@ ThrowCompletionOr<UnregulatedTemporalTime> to_temporal_time_record(GlobalObject&
}
// d. Set value to ? ToIntegerThrowOnInfinity(value).
auto value_number = to_integer_throw_on_infinity(global_object, value, ErrorType::TemporalPropertyMustBeFinite);
if (auto* exception = vm.exception())
return throw_completion(exception->value());
auto value_number = TRY(to_integer_throw_on_infinity(global_object, value, ErrorType::TemporalPropertyMustBeFinite));
// e. Set result's internal slot whose name is the Internal Slot value of the current row to value.
result.*internal_slot = value_number;

View file

@ -51,34 +51,22 @@ Value PlainTimeConstructor::construct(FunctionObject& new_target)
auto& global_object = this->global_object();
// 2. Let hour be ? ToIntegerThrowOnInfinity(hour).
auto hour = to_integer_throw_on_infinity(global_object, vm.argument(0), ErrorType::TemporalInvalidPlainTime);
if (vm.exception())
return {};
auto hour = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(0), ErrorType::TemporalInvalidPlainTime));
// 3. Let minute be ? ToIntegerThrowOnInfinity(hour).
auto minute = to_integer_throw_on_infinity(global_object, vm.argument(1), ErrorType::TemporalInvalidPlainTime);
if (vm.exception())
return {};
auto minute = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(1), ErrorType::TemporalInvalidPlainTime));
// 4. Let second be ? ToIntegerThrowOnInfinity(hour).
auto second = to_integer_throw_on_infinity(global_object, vm.argument(2), ErrorType::TemporalInvalidPlainTime);
if (vm.exception())
return {};
auto second = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(2), ErrorType::TemporalInvalidPlainTime));
// 5. Let millisecond be ? ToIntegerThrowOnInfinity(hour).
auto millisecond = to_integer_throw_on_infinity(global_object, vm.argument(3), ErrorType::TemporalInvalidPlainTime);
if (vm.exception())
return {};
auto millisecond = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(3), ErrorType::TemporalInvalidPlainTime));
// 6. Let microsecond be ? ToIntegerThrowOnInfinity(hour).
auto microsecond = to_integer_throw_on_infinity(global_object, vm.argument(4), ErrorType::TemporalInvalidPlainTime);
if (vm.exception())
return {};
auto microsecond = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(4), ErrorType::TemporalInvalidPlainTime));
// 7. Let nanosecond be ? ToIntegerThrowOnInfinity(hour).
auto nanosecond = to_integer_throw_on_infinity(global_object, vm.argument(5), ErrorType::TemporalInvalidPlainTime);
if (vm.exception())
return {};
auto nanosecond = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(5), ErrorType::TemporalInvalidPlainTime));
// IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards.
// This does not change the exposed behavior as the call to CreateTemporalTime will immediately check that these values are valid

View file

@ -64,14 +64,10 @@ Value PlainYearMonthConstructor::construct(FunctionObject& new_target)
}
// 3. Let y be ? ToIntegerThrowOnInfinity(isoYear).
auto y = to_integer_throw_on_infinity(global_object, iso_year, ErrorType::TemporalInvalidPlainYearMonth);
if (vm.exception())
return {};
auto y = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, iso_year, ErrorType::TemporalInvalidPlainYearMonth));
// 4. Let m be ? ToIntegerThrowOnInfinity(isoMonth).
auto m = to_integer_throw_on_infinity(global_object, iso_month, ErrorType::TemporalInvalidPlainYearMonth);
if (vm.exception())
return {};
auto m = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, iso_month, ErrorType::TemporalInvalidPlainYearMonth));
// 5. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
auto* calendar = to_temporal_calendar_with_iso_default(global_object, calendar_like);
@ -79,9 +75,7 @@ Value PlainYearMonthConstructor::construct(FunctionObject& new_target)
return {};
// 6. Let ref be ? ToIntegerThrowOnInfinity(referenceISODay).
auto ref = to_integer_throw_on_infinity(global_object, reference_iso_day, ErrorType::TemporalInvalidPlainYearMonth);
if (vm.exception())
return {};
auto ref = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, reference_iso_day, ErrorType::TemporalInvalidPlainYearMonth));
// IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards.
// This does not change the exposed behavior as the call to CreateTemporalYearMonth will immediately check that these values are valid