2021-07-27 00:21:16 +01:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
|
|
|
|
|
|
*
|
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2021-08-07 22:40:32 +01:00
|
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
|
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2021-08-16 00:35:05 +01:00
|
|
|
|
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
|
2021-08-07 22:40:32 +01:00
|
|
|
|
#include <LibJS/Runtime/Temporal/PlainDate.h>
|
2021-07-27 00:21:16 +01:00
|
|
|
|
#include <LibJS/Runtime/Temporal/PlainYearMonth.h>
|
2021-08-07 22:40:32 +01:00
|
|
|
|
#include <LibJS/Runtime/Temporal/PlainYearMonthConstructor.h>
|
2021-07-27 00:21:16 +01:00
|
|
|
|
|
|
|
|
|
|
namespace JS::Temporal {
|
|
|
|
|
|
|
2021-08-07 22:40:32 +01:00
|
|
|
|
// 9 Temporal.PlainYearMonth Objects, https://tc39.es/proposal-temporal/#sec-temporal-plainyearmonth-objects
|
|
|
|
|
|
PlainYearMonth::PlainYearMonth(i32 iso_year, u8 iso_month, u8 iso_day, Object& calendar, Object& prototype)
|
|
|
|
|
|
: Object(prototype)
|
|
|
|
|
|
, m_iso_year(iso_year)
|
|
|
|
|
|
, m_iso_month(iso_month)
|
|
|
|
|
|
, m_iso_day(iso_day)
|
|
|
|
|
|
, m_calendar(calendar)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlainYearMonth::visit_edges(Visitor& visitor)
|
|
|
|
|
|
{
|
2021-08-14 20:09:26 +02:00
|
|
|
|
Base::visit_edges(visitor);
|
2021-08-07 22:40:32 +01:00
|
|
|
|
visitor.visit(&m_calendar);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-16 00:35:05 +01:00
|
|
|
|
// 9.5.2 RegulateISOYearMonth ( year, month, overflow ), https://tc39.es/proposal-temporal/#sec-temporal-regulateisoyearmonth
|
|
|
|
|
|
Optional<ISOYearMonth> regulate_iso_year_month(GlobalObject& global_object, double year, double month, String const& overflow)
|
2021-07-27 00:21:16 +01:00
|
|
|
|
{
|
2021-08-16 00:35:05 +01:00
|
|
|
|
auto& vm = global_object.vm();
|
|
|
|
|
|
|
2021-07-27 00:21:16 +01:00
|
|
|
|
// 1. Assert: year and month are integers.
|
2021-08-16 00:35:05 +01:00
|
|
|
|
VERIFY(year == trunc(year) && month == trunc(month));
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Assert: overflow is either "constrain" or "reject".
|
|
|
|
|
|
// NOTE: Asserted by the VERIFY_NOT_REACHED at the end
|
|
|
|
|
|
|
|
|
|
|
|
// 3. If overflow is "constrain", then
|
|
|
|
|
|
if (overflow == "constrain"sv) {
|
|
|
|
|
|
// IMPLEMENTATION DEFINED: This is an optimization that allows us to treat `year` (a double) as normal integer from this point onwards.
|
|
|
|
|
|
// This does not change the exposed behaviour as the subsequent call to CreateTemporalYearMonth will check that its value is a valid ISO
|
|
|
|
|
|
// values (for years: -273975 - 273975) which is a subset of this check.
|
|
|
|
|
|
// If RegulateISOYearMonth is ever used outside ISOYearMonthFromFields, this may need to be changed.
|
|
|
|
|
|
if (!AK::is_within_range<i32>(year)) {
|
|
|
|
|
|
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainYearMonth);
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// a. Return ! ConstrainISOYearMonth(year, month).
|
|
|
|
|
|
return constrain_iso_year_month(year, month);
|
|
|
|
|
|
}
|
2021-07-27 00:21:16 +01:00
|
|
|
|
|
2021-08-16 00:35:05 +01:00
|
|
|
|
// 4. If overflow is "reject", then
|
|
|
|
|
|
if (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 behaviour as the call to IsValidISOMonth and subsequent call to CreateTemporalDateTime will check
|
|
|
|
|
|
// that these values are valid ISO values (for years: -273975 - 273975, for months: 1 - 12) all of which are subsets of this check.
|
|
|
|
|
|
if (!AK::is_within_range<i32>(year) || !AK::is_within_range<u8>(month)) {
|
|
|
|
|
|
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainYearMonth);
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// a. If ! IsValidISOMonth(month) is false, throw a RangeError exception.
|
|
|
|
|
|
if (!is_valid_iso_month(month)) {
|
|
|
|
|
|
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainYearMonth);
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 20:38:29 +01:00
|
|
|
|
// b. Return the Record { [[Year]]: year, [[Month]]: month }.
|
2021-08-16 00:35:05 +01:00
|
|
|
|
return ISOYearMonth { .year = static_cast<i32>(year), .month = static_cast<u8>(month), .reference_iso_day = 0 };
|
|
|
|
|
|
}
|
2021-07-27 00:21:16 +01:00
|
|
|
|
|
2021-08-16 00:35:05 +01:00
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
|
}
|
2021-07-27 00:21:16 +01:00
|
|
|
|
|
2021-08-16 00:35:05 +01:00
|
|
|
|
// 9.5.3 IsValidISOMonth ( month ), https://tc39.es/proposal-temporal/#sec-temporal-isvalidisomonth
|
|
|
|
|
|
bool is_valid_iso_month(u8 month)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 1. Assert: month is an integer.
|
|
|
|
|
|
// 2. If month < 1 or month > 12, then
|
|
|
|
|
|
if (month < 1 || month > 12) {
|
|
|
|
|
|
// a.Return false.
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Return true.
|
|
|
|
|
|
return true;
|
2021-07-27 00:21:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-07 22:40:32 +01:00
|
|
|
|
// 9.5.4 ISOYearMonthWithinLimits ( year, month ), https://tc39.es/proposal-temporal/#sec-temporal-isoyearmonthwithinlimits
|
|
|
|
|
|
bool iso_year_month_within_limits(i32 year, u8 month)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 1. Assert: year and month are integers.
|
|
|
|
|
|
|
|
|
|
|
|
// 2. If year < −271821 or year > 275760, then
|
|
|
|
|
|
if (year < -271821 || year > 275760) {
|
|
|
|
|
|
// a. Return false.
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 3. If year is −271821 and month < 4, then
|
|
|
|
|
|
if (year == -271821 && month < 4) {
|
|
|
|
|
|
// a. Return false.
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4. If year is 275760 and month > 9, then
|
|
|
|
|
|
if (year == 275760 && month > 9) {
|
|
|
|
|
|
// a. Return false.
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 5. Return true.
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-16 00:35:05 +01:00
|
|
|
|
// 9.5.5 BalanceISOYearMonth ( year, month ), https://tc39.es/proposal-temporal/#sec-temporal-balanceisoyearmonth
|
|
|
|
|
|
ISOYearMonth balance_iso_year_month(double year, double month)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 1. Assert: year and month are integers.
|
|
|
|
|
|
VERIFY(year == trunc(year) && month == trunc(month));
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Set year to year + floor((month - 1) / 12).
|
|
|
|
|
|
year += floor((month - 1) / 12);
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Set month to (month − 1) modulo 12 + 1.
|
|
|
|
|
|
month = fmod(month - 1, 12) + 1;
|
|
|
|
|
|
|
2021-08-17 20:38:29 +01:00
|
|
|
|
// 4. Return the Record { [[Year]]: year, [[Month]]: month }.
|
2021-08-16 00:35:05 +01:00
|
|
|
|
return ISOYearMonth { .year = static_cast<i32>(year), .month = static_cast<u8>(month), .reference_iso_day = 0 };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 9.5.6 ConstrainISOYearMonth ( year, month ), https://tc39.es/proposal-temporal/#sec-temporal-constrainisoyearmonth
|
|
|
|
|
|
ISOYearMonth constrain_iso_year_month(double year, double month)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 1. Assert: year and month are integers.
|
|
|
|
|
|
VERIFY(year == trunc(year) && month == trunc(month));
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Set month to ! ConstrainToRange(month, 1, 12).
|
|
|
|
|
|
month = constrain_to_range(month, 1, 12);
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Return the Record { [[Year]]: year, [[Month]]: month }.
|
|
|
|
|
|
// NOTE: `year` is known to be in the i32 range.
|
|
|
|
|
|
return ISOYearMonth { .year = static_cast<i32>(year), .month = static_cast<u8>(month), .reference_iso_day = 0 };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-07 22:40:32 +01:00
|
|
|
|
// 9.5.7 CreateTemporalYearMonth ( isoYear, isoMonth, calendar, referenceISODay [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalyearmonth
|
|
|
|
|
|
PlainYearMonth* create_temporal_year_month(GlobalObject& global_object, i32 iso_year, u8 iso_month, Object& calendar, u8 reference_iso_day, FunctionObject* new_target)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& vm = global_object.vm();
|
|
|
|
|
|
|
|
|
|
|
|
// 1. Assert: isoYear, isoMonth, and referenceISODay are integers.
|
|
|
|
|
|
// 2. Assert: Type(calendar) is Object.
|
|
|
|
|
|
|
|
|
|
|
|
// 3. If ! IsValidISODate(isoYear, isoMonth, referenceISODay) is false, throw a RangeError exception.
|
|
|
|
|
|
if (!is_valid_iso_date(iso_year, iso_month, reference_iso_day)) {
|
|
|
|
|
|
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainYearMonth);
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4. If ! ISOYearMonthWithinLimits(isoYear, isoMonth) is false, throw a RangeError exception.
|
|
|
|
|
|
if (!iso_year_month_within_limits(iso_year, iso_month)) {
|
|
|
|
|
|
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainYearMonth);
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 5. If newTarget is not present, set it to %Temporal.PlainYearMonth%.
|
|
|
|
|
|
if (!new_target)
|
|
|
|
|
|
new_target = global_object.temporal_plain_year_month_constructor();
|
|
|
|
|
|
|
|
|
|
|
|
// 6. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.PlainYearMonth.prototype%", « [[InitializedTemporalYearMonth]], [[ISOYear]], [[ISOMonth]], [[ISODay]], [[Calendar]] »).
|
|
|
|
|
|
// 7. Set object.[[ISOYear]] to isoYear.
|
|
|
|
|
|
// 8. Set object.[[ISOMonth]] to isoMonth.
|
|
|
|
|
|
// 9. Set object.[[Calendar]] to calendar.
|
|
|
|
|
|
// 10. Set object.[[ISODay]] to referenceISODay.
|
|
|
|
|
|
auto* object = ordinary_create_from_constructor<PlainYearMonth>(global_object, *new_target, &GlobalObject::temporal_plain_year_month_prototype, iso_year, iso_month, reference_iso_day, calendar);
|
|
|
|
|
|
if (vm.exception())
|
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
|
|
// 11. Return object.
|
|
|
|
|
|
return object;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-27 00:21:16 +01:00
|
|
|
|
}
|