From 1f1d7144bfdde258266b9f4aeab7e96ae5fc140c Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sat, 7 Aug 2021 23:51:57 +0100 Subject: [PATCH] LibJS: Implement Temporal.PlainYearMonth.prototype.daysInYear --- .../Temporal/PlainYearMonthPrototype.cpp | 17 +++++++++++++++++ .../Runtime/Temporal/PlainYearMonthPrototype.h | 1 + .../PlainYearMonth.prototype.daysInYear.js | 14 ++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.daysInYear.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp index 5f473c13c8..1b0473b692 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp @@ -31,6 +31,7 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.year, year_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.month, month_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.monthCode, month_code_getter, {}, Attribute::Configurable); + define_native_accessor(vm.names.daysInYear, days_in_year_getter, {}, Attribute::Configurable); } static PlainYearMonth* typed_this(GlobalObject& global_object) @@ -107,4 +108,20 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::month_code_getter) return js_string(vm, calendar_month_code(global_object, calendar, *year_month)); } +// 9.3.7 get Temporal.PlainYearMonth.prototype.daysInYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.daysinyear +JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::days_in_year_getter) +{ + // 1. Let yearMonth be the this value. + // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). + auto* year_month = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Let calendar be yearMonth.[[Calendar]]. + auto& calendar = year_month->calendar(); + + // 4. Return ? CalendarDaysInYear(calendar, yearMonth). + return Value(calendar_days_in_year(global_object, calendar, *year_month)); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h index 5b9399a6c7..b556ab4277 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h @@ -23,6 +23,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(year_getter); JS_DECLARE_NATIVE_FUNCTION(month_getter); JS_DECLARE_NATIVE_FUNCTION(month_code_getter); + JS_DECLARE_NATIVE_FUNCTION(days_in_year_getter); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.daysInYear.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.daysInYear.js new file mode 100644 index 0000000000..d810a1da10 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.daysInYear.js @@ -0,0 +1,14 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const plainYearMonth = new Temporal.PlainYearMonth(2021, 7); + expect(plainYearMonth.daysInYear).toBe(365); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainYearMonth object", () => { + expect(() => { + Reflect.get(Temporal.PlainYearMonth.prototype, "daysInYear", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainYearMonth"); + }); +});