From 0e1e8c908e59766ceafbdcbb6dd07ff0e06c5632 Mon Sep 17 00:00:00 2001 From: Pavel Shliak Date: Fri, 25 Oct 2024 15:46:39 +0400 Subject: [PATCH] LibJS: Add calendar id getter to PlainYearMonthPrototype --- .../Runtime/Temporal/PlainYearMonthPrototype.cpp | 13 +++++++++++++ .../Runtime/Temporal/PlainYearMonthPrototype.h | 1 + .../PlainYearMonth.prototype.calendarId.js | 15 +++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.calendarId.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp index a34cfa4cf01..ef5041567ee 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp @@ -34,6 +34,7 @@ void PlainYearMonthPrototype::initialize(Realm& realm) define_direct_property(vm.well_known_symbol_to_string_tag(), PrimitiveString::create(vm, "Temporal.PlainYearMonth"_string), Attribute::Configurable); define_native_accessor(realm, vm.names.calendar, calendar_getter, {}, Attribute::Configurable); + define_native_accessor(realm, vm.names.calendarId, calendar_id_getter, {}, Attribute::Configurable); define_native_accessor(realm, vm.names.year, year_getter, {}, Attribute::Configurable); define_native_accessor(realm, vm.names.month, month_getter, {}, Attribute::Configurable); define_native_accessor(realm, vm.names.monthCode, month_code_getter, {}, Attribute::Configurable); @@ -447,4 +448,16 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::get_iso_fields) return fields; } +// 9.3.3 get Temporal.PlainYearMonth.prototype.calendarId +JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::calendar_id_getter) +{ + // Step 1: Let yearMonth be the this value + // Step 2: Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). + auto year_month = TRY(typed_this_object(vm)); + + // Step 3: Return yearMonth.[[Calendar]].identifier + auto& calendar = static_cast(year_month->calendar()); + return PrimitiveString::create(vm, calendar.identifier()); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h index f5e1cbfd3ac..ab2464d9f68 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h @@ -23,6 +23,7 @@ private: explicit PlainYearMonthPrototype(Realm&); JS_DECLARE_NATIVE_FUNCTION(calendar_getter); + JS_DECLARE_NATIVE_FUNCTION(calendar_id_getter); JS_DECLARE_NATIVE_FUNCTION(year_getter); JS_DECLARE_NATIVE_FUNCTION(month_getter); JS_DECLARE_NATIVE_FUNCTION(month_code_getter); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.calendarId.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.calendarId.js new file mode 100644 index 00000000000..800f2413e51 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.calendarId.js @@ -0,0 +1,15 @@ +describe("correct behavior", () => { + test("calendarId basic functionality", () => { + const calendar = "iso8601"; + const plainYearMonth = new Temporal.PlainYearMonth(2000, 5, calendar); + expect(plainYearMonth.calendarId).toBe("iso8601"); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainYearMonth object", () => { + expect(() => { + Reflect.get(Temporal.PlainYearMonth.prototype, "calendarId", "foo"); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainYearMonth"); + }); +});