From f59e4d67385e52378dd12b72361a9d0f2c62ab10 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Fri, 27 Aug 2021 20:24:55 +0100 Subject: [PATCH] LibJS: Implement Temporal.PlainYearMonth.prototype.eraYear --- .../Temporal/PlainYearMonthPrototype.cpp | 17 +++++++++++++ .../Temporal/PlainYearMonthPrototype.h | 1 + .../PlainYearMonth.prototype.eraYear.js | 24 +++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.eraYear.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp index e2033f83a8..d92e9c75c0 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp @@ -37,6 +37,7 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.monthsInYear, months_in_year_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.era, era_getter, {}, Attribute::Configurable); + define_native_accessor(vm.names.eraYear, era_year_getter, {}, Attribute::Configurable); u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.toString, to_string, 0, attr); @@ -200,6 +201,22 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::era_getter) return calendar_era(global_object, calendar, *plain_year_month); } +// 15.6.9.3 get Temporal.PlainYearMonth.prototype.eraYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.erayear +JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::era_year_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. Let calendar be plainYearMonth.[[Calendar]]. + auto& calendar = plain_year_month->calendar(); + + // 4. Return ? CalendarEraYear(calendar, plainYearMonth). + return calendar_era_year(global_object, calendar, *plain_year_month); +} + // 9.3.17 Temporal.PlainYearMonth.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tostring JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_string) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h index 9da18aa62f..eb4fa4a93a 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h @@ -28,6 +28,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter); JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter); JS_DECLARE_NATIVE_FUNCTION(era_getter); + JS_DECLARE_NATIVE_FUNCTION(era_year_getter); JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(to_locale_string); JS_DECLARE_NATIVE_FUNCTION(to_json); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.eraYear.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.eraYear.js new file mode 100644 index 0000000000..22bdd70799 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.eraYear.js @@ -0,0 +1,24 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const plainYearMonth = new Temporal.PlainYearMonth(2021, 7); + expect(plainYearMonth.eraYear).toBeUndefined(); + }); + + test("calendar with custom eraYear function", () => { + const calendar = { + eraYear() { + return 123; + }, + }; + const plainYearMonth = new Temporal.PlainYearMonth(2021, 7, calendar); + expect(plainYearMonth.eraYear).toBe(123); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainYearMonth object", () => { + expect(() => { + Reflect.get(Temporal.PlainYearMonth.prototype, "eraYear", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainYearMonth"); + }); +});