From 0edec6578b8dbfed22982f61f7e8c5aead419859 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sat, 7 Aug 2021 23:13:45 +0100 Subject: [PATCH] LibJS: Implement Temporal.PlainYearMonth.prototype.month --- .../Temporal/PlainYearMonthPrototype.cpp | 17 +++++++++++++++++ .../Runtime/Temporal/PlainYearMonthPrototype.h | 1 + .../PlainYearMonth.prototype.month.js | 14 ++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.month.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp index 0c1c9a2527..d6151d6775 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp @@ -29,6 +29,7 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.year, year_getter, {}, Attribute::Configurable); + define_native_accessor(vm.names.month, month_getter, {}, Attribute::Configurable); } static PlainYearMonth* typed_this(GlobalObject& global_object) @@ -73,4 +74,20 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::year_getter) return Value(calendar_year(global_object, calendar, *year_month)); } +// 9.3.5 get Temporal.PlainYearMonth.prototype.month, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.month +JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::month_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 𝔽(? CalendarMonth(calendar, yearMonth)). + return Value(calendar_month(global_object, calendar, *year_month)); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h index 1c6fc36aa6..680ed8dd2d 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h @@ -21,6 +21,7 @@ public: private: JS_DECLARE_NATIVE_FUNCTION(calendar_getter); JS_DECLARE_NATIVE_FUNCTION(year_getter); + JS_DECLARE_NATIVE_FUNCTION(month_getter); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.month.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.month.js new file mode 100644 index 0000000000..53bb0a515f --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.month.js @@ -0,0 +1,14 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const plainYearMonth = new Temporal.PlainYearMonth(2021, 7); + expect(plainYearMonth.month).toBe(7); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainYearMonth object", () => { + expect(() => { + Reflect.get(Temporal.PlainYearMonth.prototype, "month", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainYearMonth"); + }); +});