From 44fa8410c0dfac07ac1b293eab120d467ef8461a Mon Sep 17 00:00:00 2001 From: Pavel Shliak Date: Fri, 25 Oct 2024 14:48:59 +0400 Subject: [PATCH] LibJS: Add calendar id getter to PlainDateTimePrototype --- .../Runtime/Temporal/PlainDateTimePrototype.cpp | 13 +++++++++++++ .../Runtime/Temporal/PlainDateTimePrototype.h | 1 + .../PlainDateTime.prototype.calendarId.js | 15 +++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.calendarId.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp index bec3de77b15..af19d95eec6 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp @@ -38,6 +38,7 @@ void PlainDateTimePrototype::initialize(Realm& realm) define_direct_property(vm.well_known_symbol_to_string_tag(), PrimitiveString::create(vm, "Temporal.PlainDateTime"_string), Attribute::Configurable); define_native_accessor(realm, vm.names.calendar, calendar_getter, {}, Attribute::Configurable); + define_native_accessor(realm, vm.names.calendarId, calendar_id_getter, {}, Attribute::Configurable); define_native_accessor(realm, vm.names.year, year_getter, {}, Attribute::Configurable); define_native_accessor(realm, vm.names.month, month_getter, {}, Attribute::Configurable); define_native_accessor(realm, vm.names.monthCode, month_code_getter, {}, Attribute::Configurable); @@ -806,4 +807,16 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::get_iso_fields) return fields; } +// 5.3.3 get Temporal.PlainDateTime.prototype.calendarId, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindatetime.prototype.calendarid +JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::calendar_id_getter) +{ + // 1. Let dateTime be the this value. + // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). + auto temporal_date = TRY(typed_this_object(vm)); + + // 3. Return dateTime.[[Calendar]]. + auto& calendar = static_cast(temporal_date->calendar()); + return PrimitiveString::create(vm, calendar.identifier()); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h index 7d3ef1721e9..f333433a40c 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h @@ -23,6 +23,7 @@ private: explicit PlainDateTimePrototype(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/PlainDateTime/PlainDateTime.prototype.calendarId.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.calendarId.js new file mode 100644 index 00000000000..25e1c5cfb2a --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.calendarId.js @@ -0,0 +1,15 @@ +describe("correct behavior", () => { + test("calendarId basic functionality", () => { + const calendar = "iso8601"; + const plainDateTime = new Temporal.PlainDateTime(2000, 5, 1, 12, 30, 0, calendar); + expect(plainDateTime.calendarId).toBe("iso8601"); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainDateTime object", () => { + expect(() => { + Reflect.get(Temporal.PlainDateTime.prototype, "calendarId", "foo"); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDateTime"); + }); +});