diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index c7a8995fc9..017e35cc81 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -276,6 +276,7 @@ namespace JS { P(minutes) \ P(month) \ P(monthCode) \ + P(monthDayFromFields) \ P(months) \ P(monthsInYear) \ P(multiline) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index c86ea7125b..63577dfb71 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -787,6 +787,85 @@ Optional iso_year_month_from_fields(GlobalObject& global_object, O return ISOYearMonth { .year = result->year, .month = result->month, .reference_iso_day = 1 }; } +// 12.1.40 ISOMonthDayFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthdayfromfields +Optional iso_month_day_from_fields(GlobalObject& global_object, Object& fields, Object& options) +{ + auto& vm = global_object.vm(); + + // 1. Assert: Type(fields) is Object. + + // 2. Let overflow be ? ToTemporalOverflow(options). + auto overflow = to_temporal_overflow(global_object, options); + if (vm.exception()) + return {}; + + // 3. Set fields to ? PrepareTemporalFields(fields, « "day", "month", "monthCode", "year" », «»). + auto* prepared_fields = prepare_temporal_fields(global_object, fields, { "day"sv, "month"sv, "monthCode"sv, "year"sv }, {}); + if (vm.exception()) + return {}; + + // 4. Let month be ? Get(fields, "month"). + auto month_value = prepared_fields->get(vm.names.month); + if (vm.exception()) + return {}; + + // 5. Let monthCode be ? Get(fields, "monthCode"). + auto month_code = prepared_fields->get(vm.names.monthCode); + if (vm.exception()) + return {}; + + // 6. Let year be ? Get(fields, "year"). + auto year = prepared_fields->get(vm.names.year); + if (vm.exception()) + return {}; + + // 7. If month is not undefined, and monthCode and year are both undefined, then + if (!month_value.is_undefined() && month_code.is_undefined() && year.is_undefined()) { + // a. Throw a TypeError exception. + vm.throw_exception(global_object, ErrorType::TemporalMissingRequiredProperty, "monthCode or year"); + return {}; + } + + // 8. Set month to ? ResolveISOMonth(fields). + auto month = resolve_iso_month(global_object, *prepared_fields); + if (vm.exception()) + return {}; + + // 9. Let day be ? Get(fields, "day"). + auto day = prepared_fields->get(vm.names.day); + if (vm.exception()) + return {}; + + // 10. If day is undefined, throw a TypeError exception. + if (day.is_undefined()) { + vm.throw_exception(global_object, ErrorType::TemporalMissingRequiredProperty, vm.names.day.as_string()); + return {}; + } + + // 11. Let referenceISOYear be 1972 (the first leap year after the Unix epoch). + i32 reference_iso_year = 1972; + + Optional result; + + // 12. If monthCode is undefined, then + if (month_code.is_undefined()) { + // a. Let result be ? RegulateISODate(year, month, day, overflow). + result = regulate_iso_date(global_object, year.as_double(), month, day.as_double(), *overflow); + if (vm.exception()) + return {}; + } + // 13. Else, + else { + // a. Let result be ? RegulateISODate(referenceISOYear, month, day, overflow). + result = regulate_iso_date(global_object, reference_iso_year, month, day.as_double(), *overflow); + if (vm.exception()) + return {}; + } + + // 14. Return the new Record { [[Month]]: result.[[Month]], [[Day]]: result.[[Day]], [[ReferenceISOYear]]: referenceISOYear }. + return ISOMonthDay { .month = result->month, .day = result->day, .reference_iso_year = reference_iso_year }; +} + // 12.1.41 ISOYear ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isoyear i32 iso_year(Object& temporal_object) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h index 1589efbee5..f75b5194fb 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -62,6 +63,7 @@ String build_iso_month_code(u8 month); double resolve_iso_month(GlobalObject&, Object& fields); Optional iso_date_from_fields(GlobalObject&, Object& fields, Object& options); Optional iso_year_month_from_fields(GlobalObject&, Object& fields, Object& options); +Optional iso_month_day_from_fields(GlobalObject&, Object& fields, Object& options); i32 iso_year(Object& temporal_object); u8 iso_month(Object& temporal_object); String iso_month_code(Object& temporal_object); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp index 7330973396..352623e330 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp @@ -36,6 +36,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.dateFromFields, date_from_fields, 2, attr); define_native_function(vm.names.yearMonthFromFields, year_month_from_fields, 2, attr); + define_native_function(vm.names.monthDayFromFields, month_day_from_fields, 2, attr); 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); @@ -144,6 +145,40 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::year_month_from_fields) return create_temporal_year_month(global_object, result->year, result->month, *calendar, result->reference_iso_day); } +// 12.4.6 Temporal.Calendar.prototype.monthDayFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.monthdayfromfields +// NOTE: This is the minimum monthDayFromFields implementation for engines without ECMA-402. +JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::month_day_from_fields) +{ + // 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); + + // 4. If Type(fields) is not Object, throw a TypeError exception. + auto fields = vm.argument(0); + if (!fields.is_object()) { + vm.throw_exception(global_object, ErrorType::NotAnObject, fields.to_string_without_side_effects()); + return {}; + } + + // 5. Set options to ? GetOptionsObject(options). + auto* options = get_options_object(global_object, vm.argument(1)); + if (vm.exception()) + return {}; + + // 6. Let result be ? ISOMonthDayFromFields(fields, options). + auto result = iso_month_day_from_fields(global_object, fields.as_object(), *options); + if (vm.exception()) + return {}; + + // 7. Return ? CreateTemporalMonthDay(result.[[Month]], result.[[Day]], calendar, result.[[ReferenceISOYear]]). + return create_temporal_month_day(global_object, result->month, result->day, *calendar, result->reference_iso_year); +} + // 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) diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h index d208f73d81..b044cea2c6 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h @@ -22,6 +22,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(id_getter); JS_DECLARE_NATIVE_FUNCTION(date_from_fields); JS_DECLARE_NATIVE_FUNCTION(year_month_from_fields); + JS_DECLARE_NATIVE_FUNCTION(month_day_from_fields); JS_DECLARE_NATIVE_FUNCTION(year); JS_DECLARE_NATIVE_FUNCTION(month); JS_DECLARE_NATIVE_FUNCTION(month_code); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.h index e8209eaef2..0b4d6acec2 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.h @@ -33,6 +33,12 @@ private: Object& m_calendar; // [[Calendar]] }; +struct ISOMonthDay { + u8 month; + u8 day; + i32 reference_iso_year; +}; + PlainMonthDay* create_temporal_month_day(GlobalObject&, u8 iso_month, u8 iso_day, Object& calendar, i32 reference_iso_year, FunctionObject* new_target = nullptr); } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.monthDayFromFields.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.monthDayFromFields.js new file mode 100644 index 0000000000..6d55420c28 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.monthDayFromFields.js @@ -0,0 +1,60 @@ +describe("correct behavior", () => { + test("length is 2", () => { + expect(Temporal.Calendar.prototype.monthDayFromFields).toHaveLength(2); + }); + + test("basic functionality", () => { + const calendar = new Temporal.Calendar("iso8601"); + const plainMonthDay = calendar.monthDayFromFields({ year: 2021, month: 7, day: 6 }); + expect(plainMonthDay.calendar).toBe(calendar); + expect(plainMonthDay.monthCode).toBe("M07"); + expect(plainMonthDay.day).toBe(6); + + const fields = plainMonthDay.getISOFields(); + expect(fields.isoYear).toBe(1972); // No, this isn't a mistake + }); + + test("with monthCode", () => { + const calendar = new Temporal.Calendar("iso8601"); + const plainMonthDay = calendar.monthDayFromFields({ monthCode: "M07", day: 6 }); + expect(plainMonthDay.calendar).toBe(calendar); + expect(plainMonthDay.monthCode).toBe("M07"); + expect(plainMonthDay.day).toBe(6); + + const fields = plainMonthDay.getISOFields(); + expect(fields.isoYear).toBe(1972); + }); +}); + +describe("errors", () => { + test("first argument must be an object", () => { + const calendar = new Temporal.Calendar("iso8601"); + expect(() => { + calendar.monthDayFromFields(42); + }).toThrowWithMessage(TypeError, "42 is not an object"); + }); + + test("month or monthCode field is required", () => { + const calendar = new Temporal.Calendar("iso8601"); + expect(() => { + calendar.monthDayFromFields({ year: 2021 }); + }).toThrowWithMessage(TypeError, "Required property month is missing or undefined"); + }); + + test("day field is required", () => { + const calendar = new Temporal.Calendar("iso8601"); + expect(() => { + calendar.monthDayFromFields({ year: 2021, month: 7 }); + }).toThrowWithMessage(TypeError, "Required property day is missing or undefined"); + }); + + test("monthCode or year field is required when month is given", () => { + const calendar = new Temporal.Calendar("iso8601"); + expect(() => { + calendar.monthDayFromFields({ month: 7, day: 6 }); + }).toThrowWithMessage( + TypeError, + "Required property monthCode or year is missing or undefined" + ); + }); +});