diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index cdb3492a5a..d034f18472 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include namespace JS::Temporal { @@ -416,4 +417,19 @@ Optional iso_date_from_fields(GlobalObject& global_object, Object& return regulate_iso_date(global_object, year.as_double(), month, day.as_double(), *overflow); } +// 12.1.41 ISOYear ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isoyear +i32 iso_year(Object& temporal_object) +{ + // 1. Assert: temporalObject has an [[ISOYear]] internal slot. + // NOTE: Asserted by the VERIFY_NOT_REACHED at the end + + // 2. Return 𝔽(temporalObject.[[ISOYear]]). + // TODO: add the rest of the builtins with a [[ISOYear]] slot (PlainYearMonth, PlainMonthDay) + if (is(temporal_object)) + return static_cast(temporal_object).iso_year(); + if (is(temporal_object)) + return static_cast(temporal_object).iso_year(); + VERIFY_NOT_REACHED(); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h index c252adb131..e4768be983 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h @@ -44,5 +44,6 @@ i32 iso_days_in_month(i32 year, i32 month); String build_iso_month_code(i32 month); double resolve_iso_month(GlobalObject&, Object& fields); Optional iso_date_from_fields(GlobalObject&, Object& fields, Object& options); +i32 iso_year(Object& temporal_object); } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp index 756f6ac7d4..e8280c27bf 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp @@ -27,9 +27,11 @@ void CalendarPrototype::initialize(GlobalObject& global_object) // 12.4.2 Temporal.Calendar.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype-@@tostringtag define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.Calendar"), Attribute::Configurable); - u8 attr = Attribute::Writable | Attribute::Configurable; define_native_accessor(vm.names.id, id_getter, {}, Attribute::Configurable); + + u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.dateFromFields, date_from_fields, 2, attr); + define_native_function(vm.names.year, year, 1, attr); define_native_function(vm.names.toString, to_string, 0, attr); define_native_function(vm.names.toJSON, to_json, 0, attr); } @@ -91,6 +93,33 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::date_from_fields) return create_temporal_date(global_object, result->year, result->month, result->day, *calendar); } +// 12.4.9 Temporal.Calendar.prototype.year ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.year +// NOTE: This is the minimum year implementation for engines without ECMA-402. +JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::year) +{ + // 1. Let calendar be the this value. + // 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]). + auto* calendar = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Assert: calendar.[[Identifier]] is "iso8601". + VERIFY(calendar->identifier() == "iso8601"sv); + + auto temporal_date_like = vm.argument(0); + // 4. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]] or [[InitializedTemporalYearMonth]] internal slot, then + // TODO PlainYearMonth objects + if (!temporal_date_like.is_object() || !is(temporal_date_like.as_object())) { + // a. Set temporalDateLike to ? ToTemporalDate(temporalDateLike). + temporal_date_like = to_temporal_date(global_object, temporal_date_like); + if (vm.exception()) + return {}; + } + + // 5. Return ! ISOYear(temporalDateLike). + return Value(iso_year(temporal_date_like.as_object())); +} + // 12.4.23 Temporal.Calendar.prototype.toString ( ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.tostring JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::to_string) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h index 714d1f9c51..823245ee85 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h @@ -21,6 +21,7 @@ public: private: JS_DECLARE_NATIVE_FUNCTION(id_getter); JS_DECLARE_NATIVE_FUNCTION(date_from_fields); + JS_DECLARE_NATIVE_FUNCTION(year); JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(to_json); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.year.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.year.js new file mode 100644 index 0000000000..469a87dec9 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.year.js @@ -0,0 +1,11 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.Calendar.prototype.year).toHaveLength(1); + }); + + test("basic functionality", () => { + const calendar = new Temporal.Calendar("iso8601"); + const date = new Temporal.PlainDate(2021, 7, 23); + expect(calendar.year(date)).toBe(2021); + }); +});