diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp index 9b081197e3..5138556df8 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp @@ -49,6 +49,7 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.daysInYear, days_in_year_getter, {}, Attribute::Configurable); 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); u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.withPlainTime, with_plain_time, 1, attr); @@ -359,6 +360,22 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::in_leap_year_getter) return calendar_in_leap_year(global_object, calendar, *date_time); } +// 15.6.6.2 get Temporal.PlainDateTime.prototype.era, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindatetime.prototype.era +JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::era_getter) +{ + // 1. Let plainDateTime be the this value. + // 2. Perform ? RequireInternalSlot(plainDateTime, [[InitializedTemporalDateTime]]). + auto* plain_date_time = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Let calendar be plainDateTime.[[Calendar]]. + auto& calendar = plain_date_time->calendar(); + + // 4. Return ? CalendarEra(calendar, plainDateTime). + return calendar_era(global_object, calendar, *plain_date_time); +} + // 5.3.23 Temporal.PlainDateTime.prototype.withPlainTime ( [ plainTimeLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.withplaintime JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::with_plain_time) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h index f5ecbdb2db..1bb097c32c 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h @@ -38,6 +38,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(days_in_year_getter); 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(with_plain_time); JS_DECLARE_NATIVE_FUNCTION(with_plain_date); JS_DECLARE_NATIVE_FUNCTION(with_calendar); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.era.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.era.js new file mode 100644 index 0000000000..382237413a --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.era.js @@ -0,0 +1,24 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const plainDateTime = new Temporal.PlainDateTime(2021, 7, 6, 18, 14, 47); + expect(plainDateTime.era).toBeUndefined(); + }); + + test("calendar with custom era function", () => { + const calendar = { + era() { + return "foo"; + }, + }; + const plainDateTime = new Temporal.PlainDateTime(2021, 7, 6, 18, 14, 47, 0, 0, 0, calendar); + expect(plainDateTime.era).toBe("foo"); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainDateTime object", () => { + expect(() => { + Reflect.get(Temporal.PlainDateTime.prototype, "era", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainDateTime"); + }); +});