diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index 301a933b10..6e64673d0c 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -525,4 +525,19 @@ String iso_month_code(Object& temporal_object) VERIFY_NOT_REACHED(); } +// 12.1.44 ISODay ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthcode +u8 iso_day(Object& temporal_object) +{ + // 1. Assert: temporalObject has an [[ISODay]] internal slot. + // NOTE: Asserted by the VERIFY_NOT_REACHED at the end + + // 2. Return 𝔽(temporalObject.[[ISODay]]). + // TODO: add the rest of the builtins with a [[ISODay]] slot (PlainYearMonth, PlainMonthDay) + if (is(temporal_object)) + return static_cast(temporal_object).iso_day(); + if (is(temporal_object)) + return static_cast(temporal_object).iso_day(); + VERIFY_NOT_REACHED(); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h index 4c46fc8e15..95f0cd37cf 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h @@ -50,5 +50,6 @@ Optional iso_date_from_fields(GlobalObject&, Object& fields, Objec i32 iso_year(Object& temporal_object); u8 iso_month(Object& temporal_object); String iso_month_code(Object& temporal_object); +u8 iso_day(Object& temporal_object); } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp index a49f958ab4..0dddef4d0e 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp @@ -34,6 +34,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.year, year, 1, attr); define_native_function(vm.names.month, month, 1, attr); define_native_function(vm.names.monthCode, month_code, 1, attr); + define_native_function(vm.names.day, day, 1, attr); define_native_function(vm.names.toString, to_string, 0, attr); define_native_function(vm.names.toJSON, to_json, 0, attr); } @@ -180,6 +181,33 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::month_code) return js_string(vm, iso_month_code(temporal_date_like.as_object())); } +// 12.4.12 Temporal.Calendar.prototype.day ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.day +// NOTE: This is the minimum day implementation for engines without ECMA-402. +JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::day) +{ + // 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 [[InitializedTemporalMonthDay]] internal slot, then + // TODO PlainMonthDay 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 ! ISODay(temporalDateLike). + return Value(iso_day(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 b5168edc13..3d2a5b0e92 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h @@ -24,6 +24,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(year); JS_DECLARE_NATIVE_FUNCTION(month); JS_DECLARE_NATIVE_FUNCTION(month_code); + JS_DECLARE_NATIVE_FUNCTION(day); JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(to_json); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.day.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.day.js new file mode 100644 index 0000000000..421dd40cee --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.day.js @@ -0,0 +1,11 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.Calendar.prototype.day).toHaveLength(1); + }); + + test("basic functionality", () => { + const calendar = new Temporal.Calendar("iso8601"); + const date = new Temporal.PlainDate(2021, 7, 23); + expect(calendar.day(date)).toBe(23); + }); +});