1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:37:34 +00:00

LibJS: Implement Temporal.PlainDate.prototype.inLeapYear

This commit is contained in:
Idan Horowitz 2021-07-23 19:04:43 +03:00 committed by Linus Groh
parent c9ae7e1af1
commit 1d76be97f5
5 changed files with 45 additions and 0 deletions

View file

@ -273,6 +273,16 @@ Value calendar_months_in_year(GlobalObject& global_object, Object& calendar, Obj
return calendar.invoke(vm.names.monthsInYear, &date_like);
}
// 12.1.20 CalendarInLeapYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarinleapyear
Value calendar_in_leap_year(GlobalObject& global_object, Object& calendar, Object& date_like)
{
auto& vm = global_object.vm();
// 1. Assert: Type(calendar) is Object.
// 2. Return ? Invoke(calendar, "inLeapYear", « dateLike »).
return calendar.invoke(vm.names.inLeapYear, &date_like);
}
// 12.1.21 ToTemporalCalendar ( temporalCalendarLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalcalendar
Object* to_temporal_calendar(GlobalObject& global_object, Value temporal_calendar_like)
{

View file

@ -45,6 +45,7 @@ Value calendar_days_in_week(GlobalObject&, Object& calendar, Object& date_like);
Value calendar_days_in_month(GlobalObject&, Object& calendar, Object& date_like);
Value calendar_days_in_year(GlobalObject&, Object& calendar, Object& date_like);
Value calendar_months_in_year(GlobalObject&, Object& calendar, Object& date_like);
Value calendar_in_leap_year(GlobalObject&, Object& calendar, Object& date_like);
Object* to_temporal_calendar(GlobalObject&, Value);
Object* to_temporal_calendar_with_iso_default(GlobalObject&, Value);
Object* get_temporal_calendar_with_iso_default(GlobalObject&, Object&);

View file

@ -38,6 +38,7 @@ void PlainDatePrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.daysInMonth, days_in_month_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.daysInYear, days_in_year_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.monthsInYear, months_in_year_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.withCalendar, with_calendar, 1, attr);
@ -247,6 +248,22 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::months_in_year_getter)
return Value(calendar_months_in_year(global_object, calendar, *temporal_date));
}
// 3.3.15 get Temporal.PlainDate.prototype.inLeapYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.inleapyear
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::in_leap_year_getter)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = typed_this(global_object);
if (vm.exception())
return {};
// 3. Let calendar be temporalDate.[[Calendar]].
auto& calendar = temporal_date->calendar();
// 4. Return ? CalendarInLeapYear(calendar, temporalDate).
return Value(calendar_in_leap_year(global_object, calendar, *temporal_date));
}
// 3.3.22 Temporal.PlainDate.prototype.withCalendar ( calendar ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.withcalendar
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::with_calendar)
{

View file

@ -31,6 +31,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(days_in_month_getter);
JS_DECLARE_NATIVE_FUNCTION(days_in_year_getter);
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
JS_DECLARE_NATIVE_FUNCTION(equals);

View file

@ -0,0 +1,16 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const date = new Temporal.PlainDate(2021, 7, 23);
expect(date.inLeapYear).toBeFalse();
const leapDate = new Temporal.PlainDate(2020, 7, 23);
expect(leapDate.inLeapYear).toBeTrue();
});
});
test("errors", () => {
test("this value must be a Temporal.PlainDate object", () => {
expect(() => {
Reflect.get(Temporal.PlainDate.prototype, "inLeapYear", "foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.PlainDate");
});
});