LibJS: Start implementing Temporal.PlainYearMonth

This commit adds the PlainYearMonth object itself, its constructor and
prototype (currently empty), and the CreateTemporalYearMonth and
ISOYearMonthWithinLimits abstract operations.
This commit is contained in:
Linus Groh 2021-08-07 22:40:32 +01:00
parent 5b12542d39
commit 0a8edd5ce7
Notes: sideshowbarker 2024-07-18 07:13:43 +09:00
12 changed files with 360 additions and 10 deletions

View File

@ -144,6 +144,8 @@ set(SOURCES
Runtime/Temporal/PlainTimeConstructor.cpp
Runtime/Temporal/PlainTimePrototype.cpp
Runtime/Temporal/PlainYearMonth.cpp
Runtime/Temporal/PlainYearMonthConstructor.cpp
Runtime/Temporal/PlainYearMonthPrototype.cpp
Runtime/Temporal/Temporal.cpp
Runtime/Temporal/TimeZone.cpp
Runtime/Temporal/TimeZoneConstructor.cpp

View File

@ -76,14 +76,15 @@
__JS_ENUMERATE(Float32Array, float32_array, Float32ArrayPrototype, Float32ArrayConstructor, float) \
__JS_ENUMERATE(Float64Array, float64_array, Float64ArrayPrototype, Float64ArrayConstructor, double)
#define JS_ENUMERATE_TEMPORAL_OBJECTS \
__JS_ENUMERATE(Calendar, calendar, CalendarPrototype, CalendarConstructor) \
__JS_ENUMERATE(Duration, duration, DurationPrototype, DurationConstructor) \
__JS_ENUMERATE(Instant, instant, InstantPrototype, InstantConstructor) \
__JS_ENUMERATE(PlainDate, plain_date, PlainDatePrototype, PlainDateConstructor) \
__JS_ENUMERATE(PlainDateTime, plain_date_time, PlainDateTimePrototype, PlainDateTimeConstructor) \
__JS_ENUMERATE(PlainTime, plain_time, PlainTimePrototype, PlainTimeConstructor) \
__JS_ENUMERATE(TimeZone, time_zone, TimeZonePrototype, TimeZoneConstructor) \
#define JS_ENUMERATE_TEMPORAL_OBJECTS \
__JS_ENUMERATE(Calendar, calendar, CalendarPrototype, CalendarConstructor) \
__JS_ENUMERATE(Duration, duration, DurationPrototype, DurationConstructor) \
__JS_ENUMERATE(Instant, instant, InstantPrototype, InstantConstructor) \
__JS_ENUMERATE(PlainDate, plain_date, PlainDatePrototype, PlainDateConstructor) \
__JS_ENUMERATE(PlainDateTime, plain_date_time, PlainDateTimePrototype, PlainDateTimeConstructor) \
__JS_ENUMERATE(PlainTime, plain_time, PlainTimePrototype, PlainTimeConstructor) \
__JS_ENUMERATE(PlainYearMonth, plain_year_month, PlainYearMonthPrototype, PlainYearMonthConstructor) \
__JS_ENUMERATE(TimeZone, time_zone, TimeZonePrototype, TimeZoneConstructor) \
__JS_ENUMERATE(ZonedDateTime, zoned_date_time, ZonedDateTimePrototype, ZonedDateTimeConstructor)
#define JS_ENUMERATE_ITERATOR_PROTOTYPES \

View File

@ -180,6 +180,7 @@
M(TemporalInvalidPlainDate, "Invalid plain date") \
M(TemporalInvalidPlainDateTime, "Invalid plain date time") \
M(TemporalInvalidPlainTime, "Invalid plain time") \
M(TemporalInvalidPlainYearMonth, "Invalid plain year month") \
M(TemporalInvalidTime, "Invalid time") \
M(TemporalInvalidTimeZoneName, "Invalid time zone name") \
M(TemporalMissingRequiredProperty, "Required property {} is missing or undefined") \

View File

@ -81,6 +81,8 @@
#include <LibJS/Runtime/Temporal/PlainDateTimePrototype.h>
#include <LibJS/Runtime/Temporal/PlainTimeConstructor.h>
#include <LibJS/Runtime/Temporal/PlainTimePrototype.h>
#include <LibJS/Runtime/Temporal/PlainYearMonthConstructor.h>
#include <LibJS/Runtime/Temporal/PlainYearMonthPrototype.h>
#include <LibJS/Runtime/Temporal/Temporal.h>
#include <LibJS/Runtime/Temporal/TimeZoneConstructor.h>
#include <LibJS/Runtime/Temporal/TimeZonePrototype.h>

View File

@ -4,10 +4,29 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/PlainDate.h>
#include <LibJS/Runtime/Temporal/PlainYearMonth.h>
#include <LibJS/Runtime/Temporal/PlainYearMonthConstructor.h>
namespace JS::Temporal {
// 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)
{
visitor.visit(&m_calendar);
}
// 9.5.5 BalanceISOYearMonth ( year, month ), https://tc39.es/proposal-temporal/#sec-temporal-balanceisoyearmonth
ISOYearMonth balance_iso_year_month(i32 year, i32 month)
{
@ -23,4 +42,68 @@ ISOYearMonth balance_iso_year_month(i32 year, i32 month)
return ISOYearMonth { .year = year, .month = static_cast<u8>(month) };
}
// 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;
}
// 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;
}
}

View File

@ -6,16 +6,40 @@
#pragma once
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/Runtime/Object.h>
namespace JS::Temporal {
class PlainYearMonth final : public Object {
JS_OBJECT(PlainYearMonth, Object);
public:
PlainYearMonth(i32 iso_year, u8 iso_month, u8 iso_day, Object& calendar, Object& prototype);
virtual ~PlainYearMonth() override = default;
[[nodiscard]] i32 iso_year() const { return m_iso_year; }
[[nodiscard]] u8 iso_month() const { return m_iso_month; }
[[nodiscard]] u8 iso_day() const { return m_iso_day; }
[[nodiscard]] Object const& calendar() const { return m_calendar; }
[[nodiscard]] Object& calendar() { return m_calendar; }
private:
virtual void visit_edges(Visitor&) override;
// 9.4 Properties of Temporal.PlainYearMonth Instances, https://tc39.es/proposal-temporal/#sec-properties-of-temporal-plainyearmonth-instances
i32 m_iso_year { 0 }; // [[ISOYear]]
u8 m_iso_month { 0 }; // [[ISOMonth]]
u8 m_iso_day { 0 }; // [[ISODay]]
Object& m_calendar; // [[Calendar]]
};
struct ISOYearMonth {
i32 year;
u8 month;
};
ISOYearMonth balance_iso_year_month(i32 year, i32 month);
bool iso_year_month_within_limits(i32 year, u8 month);
PlainYearMonth* create_temporal_year_month(GlobalObject&, i32 iso_year, u8 iso_month, Object& calendar, u8 reference_iso_day, FunctionObject* new_target = nullptr);
}

View File

@ -0,0 +1,109 @@
/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/PlainYearMonth.h>
#include <LibJS/Runtime/Temporal/PlainYearMonthConstructor.h>
namespace JS::Temporal {
// 9.1 The Temporal.PlainYearMonth Constructor, https://tc39.es/proposal-temporal/#sec-temporal-plainyearmonth-constructor
PlainYearMonthConstructor::PlainYearMonthConstructor(GlobalObject& global_object)
: NativeFunction(vm().names.PlainYearMonth.as_string(), *global_object.function_prototype())
{
}
void PlainYearMonthConstructor::initialize(GlobalObject& global_object)
{
NativeFunction::initialize(global_object);
auto& vm = this->vm();
// 9.2.1 Temporal.PlainYearMonth.prototype, https://tc39.es/proposal-temporal/#sec-temporal-plainyearmonth-prototype
define_direct_property(vm.names.prototype, global_object.temporal_plain_year_month_prototype(), 0);
define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
}
// 9.1.1 Temporal.PlainYearMonth ( isoYear, isoMonth [ , calendarLike [ , referenceISODay ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth
Value PlainYearMonthConstructor::call()
{
auto& vm = this->vm();
// 1. If NewTarget is undefined, throw a TypeError exception.
vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.PlainYearMonth");
return {};
}
// 9.1.1 Temporal.PlainYearMonth ( isoYear, isoMonth [ , calendarLike [ , referenceISODay ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth
Value PlainYearMonthConstructor::construct(FunctionObject& new_target)
{
auto& vm = this->vm();
auto& global_object = this->global_object();
auto iso_year = vm.argument(0);
auto iso_month = vm.argument(1);
auto calendar_like = vm.argument(2);
auto reference_iso_day = vm.argument(3);
// 2. If referenceISODay is undefined, then
if (reference_iso_day.is_undefined()) {
// a. Set referenceISODay to 1𝔽.
reference_iso_day = Value(1);
}
// 3. Let y be ? ToIntegerOrInfinity(isoYear).
auto y = iso_year.to_integer_or_infinity(global_object);
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);
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).
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);
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.
if (!AK::is_within_range<i32>(y) || !AK::is_within_range<u8>(m) || !AK::is_within_range<u8>(ref)) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainYearMonth);
return {};
}
// 10. Return ? CreateTemporalYearMonth(y, m, calendar, ref, NewTarget).
return create_temporal_year_month(global_object, y, m, *calendar, ref, &new_target);
}
}

View File

@ -0,0 +1,28 @@
/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/NativeFunction.h>
namespace JS::Temporal {
class PlainYearMonthConstructor final : public NativeFunction {
JS_OBJECT(PlainYearMonthConstructor, NativeFunction);
public:
explicit PlainYearMonthConstructor(GlobalObject&);
virtual void initialize(GlobalObject&) override;
virtual ~PlainYearMonthConstructor() override = default;
virtual Value call() override;
virtual Value construct(FunctionObject& new_target) override;
private:
virtual bool has_constructor() const override { return true; }
};
}

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/PlainYearMonthPrototype.h>
namespace JS::Temporal {
// 9.3 Properties of the Temporal.PlainYearMonth Prototype Object, https://tc39.es/proposal-temporal/#sec-properties-of-the-temporal-plainyearmonth-prototype-object
PlainYearMonthPrototype::PlainYearMonthPrototype(GlobalObject& global_object)
: Object(*global_object.object_prototype())
{
}
void PlainYearMonthPrototype::initialize(GlobalObject& global_object)
{
Object::initialize(global_object);
}
}

View File

@ -0,0 +1,22 @@
/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/Object.h>
namespace JS::Temporal {
class PlainYearMonthPrototype final : public Object {
JS_OBJECT(PlainYearMonthPrototype, Object);
public:
explicit PlainYearMonthPrototype(GlobalObject&);
virtual void initialize(GlobalObject&) override;
virtual ~PlainYearMonthPrototype() override = default;
};
}

View File

@ -12,6 +12,7 @@
#include <LibJS/Runtime/Temporal/PlainDateConstructor.h>
#include <LibJS/Runtime/Temporal/PlainDateTimeConstructor.h>
#include <LibJS/Runtime/Temporal/PlainTimeConstructor.h>
#include <LibJS/Runtime/Temporal/PlainYearMonthConstructor.h>
#include <LibJS/Runtime/Temporal/Temporal.h>
#include <LibJS/Runtime/Temporal/TimeZoneConstructor.h>
#include <LibJS/Runtime/Temporal/ZonedDateTimeConstructor.h>
@ -41,6 +42,7 @@ void Temporal::initialize(GlobalObject& global_object)
define_direct_property(vm.names.PlainDate, global_object.temporal_plain_date_constructor(), attr);
define_direct_property(vm.names.PlainDateTime, global_object.temporal_plain_date_time_constructor(), attr);
define_direct_property(vm.names.PlainTime, global_object.temporal_plain_time_constructor(), attr);
define_direct_property(vm.names.PlainYearMonth, global_object.temporal_plain_year_month_constructor(), attr);
define_direct_property(vm.names.TimeZone, global_object.temporal_time_zone_constructor(), attr);
define_direct_property(vm.names.ZonedDateTime, global_object.temporal_zoned_date_time_constructor(), attr);
}

View File

@ -0,0 +1,53 @@
describe("errors", () => {
test("called without new", () => {
expect(() => {
Temporal.PlainYearMonth();
}).toThrowWithMessage(
TypeError,
"Temporal.PlainYearMonth constructor must be called with 'new'"
);
});
test("cannot pass Infinity", () => {
expect(() => {
new Temporal.PlainYearMonth(Infinity);
}).toThrowWithMessage(RangeError, "Invalid plain year month");
expect(() => {
new Temporal.PlainYearMonth(0, Infinity);
}).toThrowWithMessage(RangeError, "Invalid plain year month");
expect(() => {
new Temporal.PlainYearMonth(0, 0, {}, Infinity);
}).toThrowWithMessage(RangeError, "Invalid plain year month");
expect(() => {
new Temporal.PlainYearMonth(-Infinity);
}).toThrowWithMessage(RangeError, "Invalid plain year month");
expect(() => {
new Temporal.PlainYearMonth(0, -Infinity);
}).toThrowWithMessage(RangeError, "Invalid plain year month");
expect(() => {
new Temporal.PlainYearMonth(0, 0, {}, -Infinity);
}).toThrowWithMessage(RangeError, "Invalid plain year month");
});
test("cannot pass invalid ISO month/day", () => {
expect(() => {
new Temporal.PlainYearMonth(0, 0);
}).toThrowWithMessage(RangeError, "Invalid plain year month");
expect(() => {
new Temporal.PlainYearMonth(0, 1, {}, 0);
}).toThrowWithMessage(RangeError, "Invalid plain year month");
});
});
describe("normal behavior", () => {
test("length is 2", () => {
expect(Temporal.PlainYearMonth).toHaveLength(2);
});
test("basic functionality", () => {
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
expect(typeof plainYearMonth).toBe("object");
expect(plainYearMonth).toBeInstanceOf(Temporal.PlainYearMonth);
expect(Object.getPrototypeOf(plainYearMonth)).toBe(Temporal.PlainYearMonth.prototype);
});
});