From c9ae7e1af1f0a586b404aa788c62ba49acb063e7 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Fri, 23 Jul 2021 19:03:19 +0300 Subject: [PATCH] LibJS: Implement Temporal.Calendar.prototype.inLeapYear --- .../LibJS/Runtime/CommonPropertyNames.h | 1 + .../Runtime/Temporal/CalendarPrototype.cpp | 28 +++++++++++++++++++ .../Runtime/Temporal/CalendarPrototype.h | 1 + .../Calendar/Calendar.prototype.inLeapYear.js | 13 +++++++++ 4 files changed, 43 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.inLeapYear.js diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index f1010f6dca..798b9f67c3 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -219,6 +219,7 @@ namespace JS { P(indexOf) \ P(indices) \ P(info) \ + P(inLeapYear) \ P(input) \ P(instant) \ P(is) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp index 54f74f4eca..cea0c69b95 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp @@ -42,6 +42,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.daysInMonth, days_in_month, 1, attr); define_native_function(vm.names.daysInYear, days_in_year, 1, attr); define_native_function(vm.names.monthsInYear, months_in_year, 1, attr); + define_native_function(vm.names.inLeapYear, in_leap_year, 1, attr); define_native_function(vm.names.toString, to_string, 0, attr); define_native_function(vm.names.toJSON, to_json, 0, attr); } @@ -385,6 +386,33 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::months_in_year) return Value(12); } +// 12.4.20 Temporal.Calendar.prototype.inLeapYear ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.inleapyear +// NOTE: This is the minimum inLeapYear implementation for engines without ECMA-402. +JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::in_leap_year) +{ + // 1. Let calendar be the this value. + // 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]). + auto* calendar = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Assert: calendar.[[Identifier]] is "iso8601". + VERIFY(calendar->identifier() == "iso8601"sv); + + auto temporal_date_like = vm.argument(0); + // 4. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]] or [[InitializedTemporalYearMonth]] internal slot, then + // TODO PlainYearMonth objects + if (!temporal_date_like.is_object() || !is(temporal_date_like.as_object())) { + // a. Set temporalDateLike to ? ToTemporalDate(temporalDateLike). + temporal_date_like = to_temporal_date(global_object, temporal_date_like); + if (vm.exception()) + return {}; + } + + // 5. Return ! IsISOLeapYear(temporalDateLike.[[ISOYear]]). + return Value(is_iso_leap_year(iso_year(temporal_date_like.as_object()))); +} + // 12.4.23 Temporal.Calendar.prototype.toString ( ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.tostring JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::to_string) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h index d8785a3875..7cee764bba 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h @@ -32,6 +32,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(days_in_month); JS_DECLARE_NATIVE_FUNCTION(days_in_year); JS_DECLARE_NATIVE_FUNCTION(months_in_year); + JS_DECLARE_NATIVE_FUNCTION(in_leap_year); JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(to_json); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.inLeapYear.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.inLeapYear.js new file mode 100644 index 0000000000..6acd35d6ff --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.inLeapYear.js @@ -0,0 +1,13 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.Calendar.prototype.inLeapYear).toHaveLength(1); + }); + + test("basic functionality", () => { + const calendar = new Temporal.Calendar("iso8601"); + const date = new Temporal.PlainDate(2021, 7, 23); + expect(calendar.inLeapYear(date)).toBeFalse(); + const leapDate = new Temporal.PlainDate(2020, 7, 23); + expect(calendar.inLeapYear(leapDate)).toBeTrue(); + }); +});