From c2ed3ad66bf2f2a3b33d59648861c2233a3c10eb Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 15 Aug 2021 01:07:49 +0100 Subject: [PATCH] LibJS: Implement Temporal.PlainMonthDay.prototype.day --- .../Runtime/Temporal/PlainMonthDayPrototype.cpp | 17 +++++++++++++++++ .../Runtime/Temporal/PlainMonthDayPrototype.h | 1 + .../PlainMonthDay.prototype.day.js | 14 ++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.day.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp index 910bc73f8c..73587cdff4 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp @@ -29,6 +29,7 @@ void PlainMonthDayPrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.monthCode, month_code_getter, {}, Attribute::Configurable); + define_native_accessor(vm.names.day, day_getter, {}, Attribute::Configurable); } static PlainMonthDay* typed_this(GlobalObject& global_object) @@ -73,4 +74,20 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::month_code_getter) return js_string(vm, calendar_month_code(global_object, calendar, *month_day)); } +// 10.3.5 get Temporal.PlainMonthDay.prototype.day, https://tc39.es/proposal-temporal/#sec-get-temporal.plainmonthday.prototype.day +JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::day_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 𝔽(? CalendarDay(calendar, monthDay)). + return Value(calendar_day(global_object, calendar, *month_day)); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h index b3a540497d..037d1c63be 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h @@ -21,6 +21,7 @@ public: private: JS_DECLARE_NATIVE_FUNCTION(calendar_getter); JS_DECLARE_NATIVE_FUNCTION(month_code_getter); + JS_DECLARE_NATIVE_FUNCTION(day_getter); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.day.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.day.js new file mode 100644 index 0000000000..4a0db67db1 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.day.js @@ -0,0 +1,14 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const plainMonthDay = new Temporal.PlainMonthDay(7, 6); + expect(plainMonthDay.day).toBe(6); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainMonthDay object", () => { + expect(() => { + Reflect.get(Temporal.PlainMonthDay.prototype, "day", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainMonthDay"); + }); +});