From 9d9ba29cae269b1378d5bd8e86a84a00c45d96cb Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Fri, 23 Jul 2021 16:56:38 +0300 Subject: [PATCH] LibJS: Implement Temporal.Calendar.prototype.monthCode --- .../LibJS/Runtime/Temporal/Calendar.cpp | 15 ++++++++++ .../LibJS/Runtime/Temporal/Calendar.h | 1 + .../Runtime/Temporal/CalendarPrototype.cpp | 28 +++++++++++++++++++ .../Runtime/Temporal/CalendarPrototype.h | 1 + .../Calendar/Calendar.prototype.monthCode.js | 11 ++++++++ 5 files changed, 56 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.monthCode.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index 50dbe0fbfd..071c5b1735 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -489,4 +489,19 @@ u8 iso_month(Object& temporal_object) VERIFY_NOT_REACHED(); } +// 12.1.43 ISOMonthCode ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthcode +String iso_month_code(Object& temporal_object) +{ + // 1. Assert: temporalObject has an [[ISOMonth]] internal slot. + // NOTE: Asserted by the VERIFY_NOT_REACHED at the end + + // 2. Return ! BuildISOMonthCode(temporalObject.[[ISOMonth]]). + // TODO: add the rest of the builtins with a [[ISOMonth]] slot (PlainYearMonth, PlainMonthDay) + if (is(temporal_object)) + return build_iso_month_code(static_cast(temporal_object).iso_month()); + if (is(temporal_object)) + return build_iso_month_code(static_cast(temporal_object).iso_month()); + VERIFY_NOT_REACHED(); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h index 6d28b212e3..cb5279fbf2 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h @@ -48,5 +48,6 @@ double resolve_iso_month(GlobalObject&, Object& fields); Optional iso_date_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 deb5217fa7..a49f958ab4 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp @@ -33,6 +33,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object) 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.month, month, 1, attr); + define_native_function(vm.names.monthCode, month_code, 1, attr); define_native_function(vm.names.toString, to_string, 0, attr); define_native_function(vm.names.toJSON, to_json, 0, attr); } @@ -152,6 +153,33 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::month) return Value(iso_month(temporal_date_like.as_object())); } +// 12.4.11 Temporal.Calendar.prototype.monthCode ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.monthcode +// NOTE: This is the minimum monthCode implementation for engines without ECMA-402. +JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::month_code) +{ + // 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]], [[InitializedTemporalMonthDay]], or [[InitializedTemporalYearMonth]] internal slot, then + // TODO PlainMonthDay & 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 ! ISOMonthCode(temporalDateLike). + return js_string(vm, iso_month_code(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 1531652a74..b5168edc13 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h @@ -23,6 +23,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(date_from_fields); JS_DECLARE_NATIVE_FUNCTION(year); JS_DECLARE_NATIVE_FUNCTION(month); + JS_DECLARE_NATIVE_FUNCTION(month_code); JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(to_json); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.monthCode.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.monthCode.js new file mode 100644 index 0000000000..c701cfd668 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.monthCode.js @@ -0,0 +1,11 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.Calendar.prototype.monthCode).toHaveLength(1); + }); + + test("basic functionality", () => { + const calendar = new Temporal.Calendar("iso8601"); + const date = new Temporal.PlainDate(2021, 7, 23); + expect(calendar.monthCode(date)).toBe("M07"); + }); +});