diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-07-23 18:48:07 +0300 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-07-23 22:00:23 +0100 |
commit | b1dd09840bd4df15c623a1c097ba876dd9f11660 (patch) | |
tree | 802c4741de5d2f89b21eedc57492c2e2e1f108e0 /Userland/Libraries | |
parent | 7f270353422e5a455ff8bb5040104185ee59d58c (diff) | |
download | serenity-b1dd09840bd4df15c623a1c097ba876dd9f11660.zip |
LibJS: Implement Temporal.PlainDate.prototype.daysInYear
Diffstat (limited to 'Userland/Libraries')
5 files changed, 43 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index 152cac346a..fd2f34ffff 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -253,6 +253,16 @@ Value calendar_days_in_month(GlobalObject& global_object, Object& calendar, Obje return calendar.invoke(vm.names.daysInMonth, &date_like); } +// 12.1.18 CalendarDaysInYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendardaysinyear +Value calendar_days_in_year(GlobalObject& global_object, Object& calendar, Object& date_like) +{ + auto& vm = global_object.vm(); + // 1. Assert: Type(calendar) is Object. + + // 2. Return ? Invoke(calendar, "daysInYear", ยซ dateLike ยป). + return calendar.invoke(vm.names.daysInYear, &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) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h index 5814ba7f4d..883507cf3a 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h @@ -43,6 +43,7 @@ Value calendar_day_of_year(GlobalObject&, Object& calendar, Object& date_like); Value calendar_week_of_year(GlobalObject&, Object& calendar, Object& date_like); 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); Object* to_temporal_calendar(GlobalObject&, Value); Object* to_temporal_calendar_with_iso_default(GlobalObject&, Value); Object* get_temporal_calendar_with_iso_default(GlobalObject&, Object&); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp index 0760e0894b..d9daaeb537 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp @@ -36,6 +36,7 @@ void PlainDatePrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.weekOfYear, week_of_year_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.daysInWeek, days_in_week_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.daysInMonth, days_in_month_getter, {}, Attribute::Configurable); + define_native_accessor(vm.names.daysInYear, days_in_year_getter, {}, Attribute::Configurable); u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.withCalendar, with_calendar, 1, attr); @@ -213,6 +214,22 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::days_in_month_getter) return Value(calendar_days_in_month(global_object, calendar, *temporal_date)); } +// 3.3.13 get Temporal.PlainDate.prototype.daysInYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.daysinyear +JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::days_in_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 ? CalendarDaysInYear(calendar, temporalDate). + return Value(calendar_days_in_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) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h index ca4d4e8fac..e23ae39934 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h @@ -29,6 +29,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(week_of_year_getter); JS_DECLARE_NATIVE_FUNCTION(days_in_week_getter); JS_DECLARE_NATIVE_FUNCTION(days_in_month_getter); + JS_DECLARE_NATIVE_FUNCTION(days_in_year_getter); JS_DECLARE_NATIVE_FUNCTION(with_calendar); JS_DECLARE_NATIVE_FUNCTION(equals); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.daysInYear.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.daysInYear.js new file mode 100644 index 0000000000..c58e7bfa5a --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.daysInYear.js @@ -0,0 +1,14 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const date = new Temporal.PlainDate(2021, 7, 23); + expect(date.daysInYear).toBe(365); + }); +}); + +test("errors", () => { + test("this value must be a Temporal.PlainDate object", () => { + expect(() => { + Reflect.get(Temporal.PlainDate.prototype, "daysInYear", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainDate"); + }); +}); |