From 8f9b4a5ea6a0e4f90c233d77a382b47532a4d5d1 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Fri, 30 Jul 2021 00:26:55 +0300 Subject: [PATCH] LibJS: Implement Temporal.PlainDateTime.prototype.inLeapYear --- .../Runtime/Temporal/PlainDateTimePrototype.cpp | 17 +++++++++++++++++ .../Runtime/Temporal/PlainDateTimePrototype.h | 1 + .../PlainDateTime.prototype.inLeapYear.js | 14 ++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.inLeapYear.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp index e3dec8d0457..455bb3c0108 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp @@ -45,6 +45,7 @@ void PlainDateTimePrototype::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.valueOf, value_of, 0, attr); @@ -332,6 +333,22 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::months_in_year_getter) return calendar_months_in_year(global_object, calendar, *date_time); } +// 5.3.21 get Temporal.PlainDateTime.prototype.inLeapYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindatetime.prototype.inleapyear +JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::in_leap_year_getter) +{ + // 1. Let dateTime be the this value. + // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). + auto* date_time = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Let calendar be dateTime.[[Calendar]]. + auto& calendar = date_time->calendar(); + + // 4. Return ? CalendarInLeapYear(calendar, dateTime). + return calendar_in_leap_year(global_object, calendar, *date_time); +} + // 5.3.35 Temporal.PlainDateTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.valueof JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::value_of) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h index 9d9cee9d0c4..ae95c3b6489 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h @@ -37,6 +37,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(value_of); JS_DECLARE_NATIVE_FUNCTION(to_plain_date); JS_DECLARE_NATIVE_FUNCTION(get_iso_fields); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.inLeapYear.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.inLeapYear.js new file mode 100644 index 00000000000..30462b3894f --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.inLeapYear.js @@ -0,0 +1,14 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const plainDateTime = new Temporal.PlainDateTime(2021, 7, 30); + expect(plainDateTime.inLeapYear).toBeFalse(); + }); +}); + +test("errors", () => { + test("this value must be a Temporal.PlainDateTime object", () => { + expect(() => { + Reflect.get(Temporal.PlainDateTime.prototype, "inLeapYear", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainDateTime"); + }); +});