diff --git a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h index fa98a1e02b..3debebd25e 100644 --- a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h +++ b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h @@ -166,6 +166,7 @@ M(StringNonGlobalRegExp, "RegExp argument is non-global") \ M(StringRawCannotConvert, "Cannot convert property 'raw' to object from {}") \ M(StringRepeatCountMustBe, "repeat count must be a {} number") \ + M(TemporalAmbiguousMonthOfPlainMonthDay, "Accessing month of PlainMonthDay is ambiguous, use monthCode instead") \ M(TemporalInvalidCalendar, "Invalid calendar") \ M(TemporalInvalidCalendarFunctionResult, "Invalid calendar, {}() function returned undefined") \ M(TemporalInvalidCalendarIdentifier, "Invalid calendar identifier '{}'") \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp index bc76175f7b..ac01702ae7 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include namespace JS::Temporal { @@ -145,11 +146,15 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::month) // 3. Assert: calendar.[[Identifier]] is "iso8601". VERIFY(calendar->identifier() == "iso8601"sv); - // 4. If Type(temporalDateLike) is Object and temporalDateLike has an [[InitializedTemporalMonthDay]] internal slot, then - // a. Throw a TypeError exception. - // TODO - auto temporal_date_like = vm.argument(0); + + // 4. If Type(temporalDateLike) is Object and temporalDateLike has an [[InitializedTemporalMonthDay]] internal slot, then + if (temporal_date_like.is_object() && is(temporal_date_like.as_object())) { + // a. Throw a TypeError exception. + vm.throw_exception(global_object, ErrorType::TemporalAmbiguousMonthOfPlainMonthDay); + return {}; + } + // 5. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]] or [[InitializedTemporalYearMonth]] internal slot, then if (!temporal_date_like.is_object() || !(is(temporal_date_like.as_object()) || is(temporal_date_like.as_object()))) { // a. Set temporalDateLike to ? ToTemporalDate(temporalDateLike). @@ -177,8 +182,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::month_code) auto temporal_date_like = vm.argument(0); // 4. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]], [[InitializedTemporalMonthDay]], or [[InitializedTemporalYearMonth]] internal slot, then - // TODO PlainMonthDay objects - if (!temporal_date_like.is_object() || !(is(temporal_date_like.as_object()) || is(temporal_date_like.as_object()))) { + if (!temporal_date_like.is_object() || !(is(temporal_date_like.as_object()) || is(temporal_date_like.as_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()) @@ -204,8 +208,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::day) 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())) { + if (!temporal_date_like.is_object() || !(is(temporal_date_like.as_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()) diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.month.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.month.js index 66b4045335..92fd756199 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.month.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.month.js @@ -9,3 +9,16 @@ describe("correct behavior", () => { expect(calendar.month(date)).toBe(7); }); }); + +describe("errors", () => { + test("argument must not be a Temporal.PlainMonthDay object", () => { + const calendar = new Temporal.Calendar("iso8601"); + const plainMonthDay = new Temporal.PlainMonthDay(7, 6); + expect(() => { + calendar.month(plainMonthDay); + }).toThrowWithMessage( + TypeError, + "Accessing month of PlainMonthDay is ambiguous, use monthCode instead" + ); + }); +});