diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp index 7af2cdf406..d63a32767c 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp @@ -4,7 +4,9 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include +#include #include namespace JS::Temporal { @@ -23,6 +25,34 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object) // 9.3.2 Temporal.PlainYearMonth.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype-@@tostringtag define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.PlainYearMonth"), Attribute::Configurable); + + define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable); +} + +static PlainYearMonth* typed_this(GlobalObject& global_object) +{ + auto& vm = global_object.vm(); + auto* this_object = vm.this_value(global_object).to_object(global_object); + if (!this_object) + return {}; + if (!is(this_object)) { + vm.throw_exception(global_object, ErrorType::NotA, "Temporal.PlainYearMonth"); + return {}; + } + return static_cast(this_object); +} + +// 9.3.3 get Temporal.PlainYearMonth.prototype.calendar, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.calendar +JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::calendar_getter) +{ + // 1. Let plainYearMonth be the this value. + // 2. Perform ? RequireInternalSlot(plainYearMonth, [[InitializedTemporalYearMonth]]). + auto* plain_year_month = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Return plainYearMonth.[[Calendar]]. + return Value(&plain_year_month->calendar()); } } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h index 5d1f0f5193..39b3d19ba7 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h @@ -17,6 +17,9 @@ public: explicit PlainYearMonthPrototype(GlobalObject&); virtual void initialize(GlobalObject&) override; virtual ~PlainYearMonthPrototype() override = default; + +private: + JS_DECLARE_NATIVE_FUNCTION(calendar_getter); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.calendar.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.calendar.js new file mode 100644 index 0000000000..9b80816de8 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.calendar.js @@ -0,0 +1,15 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const calendar = new Temporal.Calendar("iso8601"); + const plainYearMonth = new Temporal.PlainYearMonth(2021, 7, calendar); + expect(plainYearMonth.calendar).toBe(calendar); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainYearMonth object", () => { + expect(() => { + Reflect.get(Temporal.PlainYearMonth.prototype, "calendar", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainYearMonth"); + }); +});