1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:47:34 +00:00

LibJS: Add and use newly introduced ToIntegerThrowOnInfinity Temporal AO

See: 2ed58f4
This commit is contained in:
Linus Groh 2021-08-17 20:17:12 +01:00
parent 4073fe497a
commit d46c531023
7 changed files with 84 additions and 199 deletions

View file

@ -661,7 +661,7 @@ double to_positive_integer_or_infinity(GlobalObject& global_object, Value argume
return integer;
}
// 13.46 PrepareTemporalFields ( fields, fieldNames, requiredFields ), https://tc39.es/proposal-temporal/#sec-temporal-preparetemporalfields
// 13.47 PrepareTemporalFields ( fields, fieldNames, requiredFields ), https://tc39.es/proposal-temporal/#sec-temporal-preparetemporalfields
Object* prepare_temporal_fields(GlobalObject& global_object, Object& fields, Vector<String> const& field_names, Vector<StringView> const& required_fields)
{
auto& vm = global_object.vm();

View file

@ -77,4 +77,26 @@ Optional<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject&, String
double to_positive_integer_or_infinity(GlobalObject&, Value argument);
Object* prepare_temporal_fields(GlobalObject&, Object& fields, Vector<String> const& field_names, Vector<StringView> const& required_fields);
// 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)
{
auto& vm = global_object.vm();
// 1. Let integer be ? ToIntegerOrInfinity(argument).
auto integer = argument.to_integer_or_infinity(global_object);
if (vm.exception())
return {};
// 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 {};
}
// 3. Return integer.
return integer;
}
}

View file

@ -52,37 +52,22 @@ Value PlainDateConstructor::construct(FunctionObject& new_target)
auto& vm = this->vm();
auto& global_object = this->global_object();
// 2. Let y be ? ToIntegerOrInfinity(isoYear).
auto y = vm.argument(0).to_integer_or_infinity(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 {};
// 3. If y is +∞ or -∞, throw a RangeError exception.
if (Value(y).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
return {};
}
// 4. Let m be ? ToIntegerOrInfinity(isoMonth).
auto m = vm.argument(1).to_integer_or_infinity(global_object);
// 3. Let m be ? ToIntegerThrowOnInfinity(isoMonth).
auto m = to_integer_throw_on_infinity(global_object, vm.argument(1), ErrorType::TemporalInvalidPlainDate);
if (vm.exception())
return {};
// 5. If m is +∞ or -∞, throw a RangeError exception.
if (Value(m).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
return {};
}
// 6. Let d be ? ToIntegerOrInfinity(isoDay).
auto d = vm.argument(2).to_integer_or_infinity(global_object);
// 4. Let d be ? ToIntegerThrowOnInfinity(isoDay).
auto d = to_integer_throw_on_infinity(global_object, vm.argument(2), ErrorType::TemporalInvalidPlainDate);
if (vm.exception())
return {};
// 7. If d is +∞ or -∞, throw a RangeError exception.
if (Value(d).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
return {};
}
// 8. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
// 5. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
auto* calendar = to_temporal_calendar_with_iso_default(global_object, vm.argument(3));
if (vm.exception())
return {};
@ -95,7 +80,7 @@ Value PlainDateConstructor::construct(FunctionObject& new_target)
return {};
}
// 9. Return ? CreateTemporalDate(y, m, d, calendar, NewTarget).
// 6. Return ? CreateTemporalDate(y, m, d, calendar, NewTarget).
return create_temporal_date(global_object, y, m, d, *calendar, &new_target);
}

View file

@ -6,6 +6,7 @@
#include <AK/Checked.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/PlainDateTime.h>
#include <LibJS/Runtime/Temporal/PlainDateTimeConstructor.h>
@ -46,106 +47,52 @@ Value PlainDateTimeConstructor::construct(FunctionObject& new_target)
auto& vm = this->vm();
auto& global_object = this->global_object();
// 2. Let isoYear be ? ToIntegerOrInfinity(isoYear).
auto iso_year = vm.argument(0).to_integer_or_infinity(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 {};
// 3. If isoYear is +∞ or -∞, throw a RangeError exception.
if (Value(iso_year).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDateTime);
return {};
}
// 4. Let isoMonth be ? ToIntegerOrInfinity(isoMonth).
auto iso_month = vm.argument(1).to_integer_or_infinity(global_object);
// 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 {};
// 5. If isoMonth is +∞ or -∞, throw a RangeError exception.
if (Value(iso_month).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDateTime);
return {};
}
// 6. Let isoDay be ? ToIntegerOrInfinity(isoDay).
auto iso_day = vm.argument(2).to_integer_or_infinity(global_object);
// 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 {};
// 7. If isoDay is +∞ or -∞, throw a RangeError exception.
if (Value(iso_day).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDateTime);
return {};
}
// 8. Let hour be ? ToIntegerOrInfinity(hour).
auto hour = vm.argument(3).to_integer_or_infinity(global_object);
// 5. Let hour be ? ToIntegerThrowOnInfinity(hour).
auto hour = to_integer_throw_on_infinity(global_object, vm.argument(3), ErrorType::TemporalInvalidPlainDateTime);
if (vm.exception())
return {};
// 9. If hour is +∞ or -∞, throw a RangeError exception.
if (Value(hour).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDateTime);
return {};
}
// Let minute be ? ToIntegerOrInfinity(minute).
auto minute = vm.argument(4).to_integer_or_infinity(global_object);
// 6. Let minute be ? ToIntegerThrowOnInfinity(minute).
auto minute = to_integer_throw_on_infinity(global_object, vm.argument(4), ErrorType::TemporalInvalidPlainDateTime);
if (vm.exception())
return {};
// 11. If minute is +∞ or -∞, throw a RangeError exception.
if (Value(minute).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDateTime);
return {};
}
// 12. Let second be ? ToIntegerOrInfinity(second).
auto second = vm.argument(5).to_integer_or_infinity(global_object);
// 7. Let second be ? ToIntegerThrowOnInfinity(second).
auto second = to_integer_throw_on_infinity(global_object, vm.argument(5), ErrorType::TemporalInvalidPlainDateTime);
if (vm.exception())
return {};
// 13. If second is +∞ or -∞, throw a RangeError exception.
if (Value(second).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDateTime);
return {};
}
// 14. Let millisecond be ? ToIntegerOrInfinity(millisecond).
auto millisecond = vm.argument(6).to_integer_or_infinity(global_object);
// 8. Let millisecond be ? ToIntegerThrowOnInfinity(millisecond).
auto millisecond = to_integer_throw_on_infinity(global_object, vm.argument(6), ErrorType::TemporalInvalidPlainDateTime);
if (vm.exception())
return {};
// 15. If millisecond is +∞ or -∞, throw a RangeError exception.
if (Value(millisecond).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDateTime);
return {};
}
// 16. Let microsecond be ? ToIntegerOrInfinity(microsecond).
auto microsecond = vm.argument(7).to_integer_or_infinity(global_object);
// 9. Let microsecond be ? ToIntegerThrowOnInfinity(microsecond).
auto microsecond = to_integer_throw_on_infinity(global_object, vm.argument(7), ErrorType::TemporalInvalidPlainDateTime);
if (vm.exception())
return {};
// 17. If microsecond is +∞ or -∞, throw a RangeError exception.
if (Value(microsecond).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDateTime);
return {};
}
// 18. Let nanosecond be ? ToIntegerOrInfinity(nanosecond).
auto nanosecond = vm.argument(8).to_integer_or_infinity(global_object);
// 10. Let nanosecond be ? ToIntegerThrowOnInfinity(nanosecond).
auto nanosecond = to_integer_throw_on_infinity(global_object, vm.argument(8), ErrorType::TemporalInvalidPlainDateTime);
if (vm.exception())
return {};
// 19. If nanosecond is +∞ or -∞, throw a RangeError exception.
if (Value(nanosecond).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDateTime);
return {};
}
// 20. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
// 11. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
auto* calendar = to_temporal_calendar_with_iso_default(global_object, vm.argument(9));
if (vm.exception())
return {};
@ -159,7 +106,7 @@ Value PlainDateTimeConstructor::construct(FunctionObject& new_target)
return {};
}
// 21. Return ? CreateTemporalDateTime(isoYear, isoMonth, isoDay, hour, minute, second, millisecond, microsecond, nanosecond, calendar, NewTarget).
// 12. Return ? CreateTemporalDateTime(isoYear, isoMonth, isoDay, hour, minute, second, millisecond, microsecond, nanosecond, calendar, NewTarget).
return create_temporal_date_time(global_object, iso_year, iso_month, iso_day, hour, minute, second, millisecond, microsecond, nanosecond, *calendar, &new_target);
}

View file

@ -5,6 +5,7 @@
*/
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/PlainMonthDay.h>
#include <LibJS/Runtime/Temporal/PlainMonthDayConstructor.h>
@ -56,44 +57,26 @@ Value PlainMonthDayConstructor::construct(FunctionObject& new_target)
reference_iso_year = Value(1972);
}
// 3. Let m be ? ToIntegerOrInfinity(isoMonth).
auto m = iso_month.to_integer_or_infinity(global_object);
// 3. Let m be ? ToIntegerThrowOnInfinity(isoMonth).
auto m = to_integer_throw_on_infinity(global_object, iso_month, ErrorType::TemporalInvalidPlainMonthDay);
if (vm.exception())
return {};
// 4. If m is +∞ or -∞, throw a RangeError exception.
if (Value(m).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainMonthDay);
return {};
}
// 5. Let d be ? ToIntegerOrInfinity(isoDay).
auto d = iso_day.to_integer_or_infinity(global_object);
// 4. Let d be ? ToIntegerThrowOnInfinity(isoDay).
auto d = to_integer_throw_on_infinity(global_object, iso_day, ErrorType::TemporalInvalidPlainMonthDay);
if (vm.exception())
return {};
// 6. If d is +∞ or -∞, throw a RangeError exception.
if (Value(d).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainMonthDay);
return {};
}
// 7. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
// 5. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
auto* calendar = to_temporal_calendar_with_iso_default(global_object, calendar_like);
if (vm.exception())
return {};
// 8. Let ref be ? ToIntegerOrInfinity(referenceISOYear).
auto ref = reference_iso_year.to_integer_or_infinity(global_object);
// 6. Let ref be ? ToIntegerThrowOnInfinity(referenceISOYear).
auto ref = to_integer_throw_on_infinity(global_object, reference_iso_year, ErrorType::TemporalInvalidPlainMonthDay);
if (vm.exception())
return {};
// 9. If ref is +∞ or -∞, throw a RangeError exception.
if (Value(ref).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainMonthDay);
return {};
}
// 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 behaviour as the call to CreateTemporalMonthDay will immediately check that these values are valid
// ISO values (for years: -273975 - 273975, for months: 1 - 12, for days: 1 - 31) all of which are subsets of this check.
@ -102,7 +85,7 @@ Value PlainMonthDayConstructor::construct(FunctionObject& new_target)
return {};
}
// 10. Return ? CreateTemporalMonthDay(m, d, calendar, ref, NewTarget).
// 7. Return ? CreateTemporalMonthDay(m, d, calendar, ref, NewTarget).
return create_temporal_month_day(global_object, m, d, *calendar, ref, &new_target);
}

View file

@ -5,6 +5,7 @@
*/
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/PlainTime.h>
#include <LibJS/Runtime/Temporal/PlainTimeConstructor.h>
@ -44,72 +45,36 @@ Value PlainTimeConstructor::construct(FunctionObject& new_target)
auto& vm = this->vm();
auto& global_object = this->global_object();
// 2. Let hour be ? ToIntegerOrInfinity(hour).
auto hour = vm.argument(0).to_integer_or_infinity(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 {};
// 3. If hour is +∞ or -∞, throw a RangeError exception.
if (Value(hour).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainTime);
return {};
}
// 4. Let minute be ? ToIntegerOrInfinity(hour).
auto minute = vm.argument(1).to_integer_or_infinity(global_object);
// 3. Let minute be ? ToIntegerThrowOnInfinity(hour).
auto minute = to_integer_throw_on_infinity(global_object, vm.argument(1), ErrorType::TemporalInvalidPlainTime);
if (vm.exception())
return {};
// 5. If minute is +∞ or -∞, throw a RangeError exception.
if (Value(minute).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainTime);
return {};
}
// 6. Let second be ? ToIntegerOrInfinity(hour).
auto second = vm.argument(2).to_integer_or_infinity(global_object);
// 4. Let second be ? ToIntegerThrowOnInfinity(hour).
auto second = to_integer_throw_on_infinity(global_object, vm.argument(2), ErrorType::TemporalInvalidPlainTime);
if (vm.exception())
return {};
// 7. If second is +∞ or -∞, throw a RangeError exception.
if (Value(second).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainTime);
return {};
}
// 8. Let millisecond be ? ToIntegerOrInfinity(hour).
auto millisecond = vm.argument(3).to_integer_or_infinity(global_object);
// 5. Let millisecond be ? ToIntegerThrowOnInfinity(hour).
auto millisecond = to_integer_throw_on_infinity(global_object, vm.argument(3), ErrorType::TemporalInvalidPlainTime);
if (vm.exception())
return {};
// 9. If millisecond is +∞ or -∞, throw a RangeError exception.
if (Value(millisecond).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainTime);
return {};
}
// 10. Let microsecond be ? ToIntegerOrInfinity(hour).
auto microsecond = vm.argument(4).to_integer_or_infinity(global_object);
// 6. Let microsecond be ? ToIntegerThrowOnInfinity(hour).
auto microsecond = to_integer_throw_on_infinity(global_object, vm.argument(4), ErrorType::TemporalInvalidPlainTime);
if (vm.exception())
return {};
// 11. If microsecond is +∞ or -∞, throw a RangeError exception.
if (Value(microsecond).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainTime);
return {};
}
// 12. Let nanosecond be ? ToIntegerOrInfinity(hour).
auto nanosecond = vm.argument(5).to_integer_or_infinity(global_object);
// 7. Let nanosecond be ? ToIntegerThrowOnInfinity(hour).
auto nanosecond = to_integer_throw_on_infinity(global_object, vm.argument(5), ErrorType::TemporalInvalidPlainTime);
if (vm.exception())
return {};
// 13. If nanosecond is +∞ or -∞, throw a RangeError exception.
if (Value(nanosecond).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainTime);
return {};
}
// 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 behaviour as the call to CreateTemporalTime will immediately check that these values are valid
// ISO values (for hours: 0 - 23, for minutes and seconds: 0 - 59, milliseconds, microseconds, and nanoseconds: 0 - 999) all of which
@ -119,7 +84,7 @@ Value PlainTimeConstructor::construct(FunctionObject& new_target)
return {};
}
// 14. Return ? CreateTemporalTime(hour, minute, second, millisecond, microsecond, nanosecond, NewTarget).
// 8. Return ? CreateTemporalTime(hour, minute, second, millisecond, microsecond, nanosecond, NewTarget).
return create_temporal_time(global_object, hour, minute, second, millisecond, microsecond, nanosecond, &new_target);
}

View file

@ -5,6 +5,7 @@
*/
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/PlainYearMonth.h>
#include <LibJS/Runtime/Temporal/PlainYearMonthConstructor.h>
@ -56,44 +57,26 @@ Value PlainYearMonthConstructor::construct(FunctionObject& new_target)
reference_iso_day = Value(1);
}
// 3. Let y be ? ToIntegerOrInfinity(isoYear).
auto y = iso_year.to_integer_or_infinity(global_object);
// 3. Let y be ? ToIntegerThrowOnInfinity(isoYear).
auto y = to_integer_throw_on_infinity(global_object, iso_year, ErrorType::TemporalInvalidPlainYearMonth);
if (vm.exception())
return {};
// 4. If y is +∞ or -∞, throw a RangeError exception.
if (Value(y).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainYearMonth);
return {};
}
// 5. Let m be ? ToIntegerOrInfinity(isoMonth).
auto m = iso_month.to_integer_or_infinity(global_object);
// 4. Let m be ? ToIntegerThrowOnInfinity(isoMonth).
auto m = to_integer_throw_on_infinity(global_object, iso_month, ErrorType::TemporalInvalidPlainYearMonth);
if (vm.exception())
return {};
// 6. If m is +∞ or -∞, throw a RangeError exception.
if (Value(m).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainYearMonth);
return {};
}
// 7. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
// 5. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
auto* calendar = to_temporal_calendar_with_iso_default(global_object, calendar_like);
if (vm.exception())
return {};
// 8. Let ref be ? ToIntegerOrInfinity(referenceISODay).
auto ref = reference_iso_day.to_integer_or_infinity(global_object);
// 6. Let ref be ? ToIntegerThrowOnInfinity(referenceISODay).
auto ref = to_integer_throw_on_infinity(global_object, reference_iso_day, ErrorType::TemporalInvalidPlainYearMonth);
if (vm.exception())
return {};
// 9. If ref is +∞ or -∞, throw a RangeError exception.
if (Value(ref).is_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainYearMonth);
return {};
}
// 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 behaviour as the call to CreateTemporalYearMonth will immediately check that these values are valid
// ISO values (for years: -273975 - 273975, for months: 1 - 12, for days: 1 - 31) all of which are subsets of this check.
@ -102,7 +85,7 @@ Value PlainYearMonthConstructor::construct(FunctionObject& new_target)
return {};
}
// 10. Return ? CreateTemporalYearMonth(y, m, calendar, ref, NewTarget).
// 7. Return ? CreateTemporalYearMonth(y, m, calendar, ref, NewTarget).
return create_temporal_year_month(global_object, y, m, *calendar, ref, &new_target);
}