diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp index cab057394b..910bc73f8c 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -27,6 +28,7 @@ void PlainMonthDayPrototype::initialize(GlobalObject& global_object) define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Temporal.PlainMonthDay"), Attribute::Configurable); define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable); + define_native_accessor(vm.names.monthCode, month_code_getter, {}, Attribute::Configurable); } static PlainMonthDay* typed_this(GlobalObject& global_object) @@ -55,4 +57,20 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::calendar_getter) return Value(&plain_month_day->calendar()); } +// 10.3.4 get Temporal.PlainMonthDay.prototype.monthCode, https://tc39.es/proposal-temporal/#sec-get-temporal.plainmonthday.prototype.monthcode +JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::month_code_getter) +{ + // 1. Let monthDay be the this value. + // 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]). + auto* month_day = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Let calendar be monthDay.[[Calendar]]. + auto& calendar = month_day->calendar(); + + // 4. Return ? CalendarMonthCode(calendar, monthDay). + return js_string(vm, calendar_month_code(global_object, calendar, *month_day)); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h index 6d6c3759dd..b3a540497d 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h @@ -20,6 +20,7 @@ public: private: JS_DECLARE_NATIVE_FUNCTION(calendar_getter); + JS_DECLARE_NATIVE_FUNCTION(month_code_getter); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.monthCode.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.monthCode.js new file mode 100644 index 0000000000..b662d26c90 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.monthCode.js @@ -0,0 +1,14 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const plainMonthDay = new Temporal.PlainMonthDay(7, 6); + expect(plainMonthDay.monthCode).toBe("M07"); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainMonthDay object", () => { + expect(() => { + Reflect.get(Temporal.PlainMonthDay.prototype, "monthCode", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainMonthDay"); + }); +});