From a7dfe9096c09f22b73374eafc463f1f371f78f7f Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Tue, 14 Jun 2022 23:50:24 +0100 Subject: [PATCH] LibJS: Switch branches in RegulateISODate This is an editorial change in the Temporal spec. See: https://github.com/tc39/proposal-temporal/commit/2e4a06f --- .../LibJS/Runtime/Temporal/PlainDate.cpp | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp index 4ce2f1cf85..5ef61b0bb0 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp @@ -310,8 +310,31 @@ ThrowCompletionOr regulate_iso_date(GlobalObject& global_object, VERIFY(year == trunc(year) && month == trunc(month) && day == trunc(day)); - // 1. If overflow is "reject", then - if (overflow == "reject"sv) { + // 1. If overflow is "constrain", then + if (overflow == "constrain"sv) { + // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat this double as normal integer from this point onwards. This + // does not change the exposed behavior as the parent's call to CreateTemporalDate will immediately check that this value is a valid + // ISO value for years: -273975 - 273975, which is a subset of this check. + if (!AK::is_within_range(year)) + return vm.throw_completion(global_object, ErrorType::TemporalInvalidPlainDate); + + // a. Set month to the result of clamping month between 1 and 12. + month = clamp(month, 1, 12); + + // b. Let daysInMonth be ! ISODaysInMonth(year, month). + auto days_in_month = iso_days_in_month(static_cast(year), static_cast(month)); + + // c. Set day to the result of clamping day between 1 and daysInMonth. + day = clamp(day, 1, days_in_month); + + // d. Return CreateISODateRecord(year, month, day). + return create_iso_date_record(static_cast(year), static_cast(month), static_cast(day)); + } + // 2. Else, + else { + // a. Assert: overflow is "reject". + VERIFY(overflow == "reject"sv); + // 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 IsValidISODate 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. @@ -321,36 +344,13 @@ ThrowCompletionOr regulate_iso_date(GlobalObject& global_object, auto y = static_cast(year); auto m = static_cast(month); auto d = static_cast(day); - // a. If IsValidISODate(year, month, day) is false, throw a RangeError exception. + // b. If IsValidISODate(year, month, day) is false, throw a RangeError exception. if (!is_valid_iso_date(y, m, d)) return vm.throw_completion(global_object, ErrorType::TemporalInvalidPlainDate); - // b. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day }. + // c. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day }. return ISODateRecord { .year = y, .month = m, .day = d }; } - // 2. Else, - else { - // a. Assert: overflow is "constrain". - VERIFY(overflow == "constrain"sv); - - // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat this double as normal integer from this point onwards. This - // does not change the exposed behavior as the parent's call to CreateTemporalDate will immediately check that this value is a valid - // ISO value for years: -273975 - 273975, which is a subset of this check. - if (!AK::is_within_range(year)) - return vm.throw_completion(global_object, ErrorType::TemporalInvalidPlainDate); - - // b. Set month to the result of clamping month between 1 and 12. - month = clamp(month, 1, 12); - - // c. Let daysInMonth be ! ISODaysInMonth(year, month). - auto days_in_month = iso_days_in_month(static_cast(year), static_cast(month)); - - // d. Set day to the result of clamping day between 1 and daysInMonth. - day = clamp(day, 1, days_in_month); - - // e. Return CreateISODateRecord(year, month, day). - return create_iso_date_record(static_cast(year), static_cast(month), static_cast(day)); - } } // 3.5.5 IsValidISODate ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-isvalidisodate