From f7cf7382c9b4502b4292ed6aa592cfe4727ec9cf Mon Sep 17 00:00:00 2001 From: Pavel Shliak Date: Fri, 25 Oct 2024 14:47:48 +0400 Subject: [PATCH] LibJS: Add calendar id getter to PlainDatePrototype --- .../LibJS/Runtime/Temporal/PlainDatePrototype.cpp | 13 +++++++++++++ .../LibJS/Runtime/Temporal/PlainDatePrototype.h | 1 + .../PlainDate/PlainDate.prototype.calendarId.js | 15 +++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.calendarId.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp index 47d6ab105d9..918289b83c0 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp @@ -52,6 +52,7 @@ void PlainDatePrototype::initialize(Realm& realm) define_native_accessor(realm, vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable); define_native_accessor(realm, vm.names.era, era_getter, {}, Attribute::Configurable); define_native_accessor(realm, vm.names.eraYear, era_year_getter, {}, Attribute::Configurable); + define_native_accessor(realm, vm.names.calendarId, calendar_id_getter, {}, Attribute::Configurable); u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(realm, vm.names.toPlainYearMonth, to_plain_year_month, 0, attr); @@ -649,4 +650,16 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::value_of) return vm.throw_completion(ErrorType::Convert, "Temporal.PlainDate", "a primitive value"); } +// 3.3.3 get Temporal.PlainDate.prototype.calendarId, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.calendarid +JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::calendar_id_getter) +{ + // 1. Let temporalDate be the this value. + // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]). + auto temporal_date = TRY(typed_this_object(vm)); + + // 3. Return temporalDate.[[Calendar]]. + auto& calendar = static_cast(temporal_date->calendar()); + return PrimitiveString::create(vm, calendar.identifier()); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h index 9a909ed423e..8b988229634 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h @@ -23,6 +23,7 @@ private: explicit PlainDatePrototype(Realm&); JS_DECLARE_NATIVE_FUNCTION(calendar_getter); + JS_DECLARE_NATIVE_FUNCTION(calendar_id_getter); JS_DECLARE_NATIVE_FUNCTION(year_getter); JS_DECLARE_NATIVE_FUNCTION(month_getter); JS_DECLARE_NATIVE_FUNCTION(month_code_getter); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.calendarId.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.calendarId.js new file mode 100644 index 00000000000..ae43185fd4f --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.calendarId.js @@ -0,0 +1,15 @@ +describe("correct behavior", () => { + test("calendarId basic functionality", () => { + const calendar = "iso8601"; + const plainDate = new Temporal.PlainDate(2000, 5, 1, calendar); + expect(plainDate.calendarId).toBe("iso8601"); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainDate object", () => { + expect(() => { + Reflect.get(Temporal.PlainDate.prototype, "calendarId", "foo"); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDate"); + }); +});