From 2cf582436fef138e1b080250cee5078342c9cf74 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Fri, 23 Jul 2021 18:21:47 +0300 Subject: [PATCH] LibJS: Implement Temporal.PlainDate.prototype.weekOfYear --- .../LibJS/Runtime/Temporal/Calendar.cpp | 10 ++++++++++ .../Libraries/LibJS/Runtime/Temporal/Calendar.h | 1 + .../Runtime/Temporal/PlainDatePrototype.cpp | 17 +++++++++++++++++ .../LibJS/Runtime/Temporal/PlainDatePrototype.h | 1 + .../PlainDate/PlainDate.prototype.weekOfYear.js | 14 ++++++++++++++ 5 files changed, 43 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.weekOfYear.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index fc25620a35a..b4fc0d0c780 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -223,6 +223,16 @@ Value calendar_day_of_year(GlobalObject& global_object, Object& calendar, Object return calendar.invoke(vm.names.dayOfYear, &date_like); } +// 12.1.15 CalendarWeekOfYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarweekofyear +Value calendar_week_of_year(GlobalObject& global_object, Object& calendar, Object& date_like) +{ + auto& vm = global_object.vm(); + // 1. Assert: Type(calendar) is Object. + + // 2. Return ? Invoke(calendar, "weekOfYear", « dateLike »). + return calendar.invoke(vm.names.weekOfYear, &date_like); +} + // 12.1.21 ToTemporalCalendar ( temporalCalendarLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalcalendar Object* to_temporal_calendar(GlobalObject& global_object, Value temporal_calendar_like) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h index 6d13bf6d6e4..2471ae37270 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h @@ -40,6 +40,7 @@ String calendar_month_code(GlobalObject&, Object& calendar, Object& date_like); double calendar_day(GlobalObject&, Object& calendar, Object& date_like); Value calendar_day_of_week(GlobalObject&, Object& calendar, Object& date_like); Value calendar_day_of_year(GlobalObject&, Object& calendar, Object& date_like); +Value calendar_week_of_year(GlobalObject&, Object& calendar, Object& date_like); Object* to_temporal_calendar(GlobalObject&, Value); Object* to_temporal_calendar_with_iso_default(GlobalObject&, Value); Object* get_temporal_calendar_with_iso_default(GlobalObject&, Object&); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp index faf027bb3a4..f81984f31f2 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp @@ -33,6 +33,7 @@ void PlainDatePrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.day, day_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.dayOfWeek, day_of_week_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.dayOfYear, day_of_year_getter, {}, Attribute::Configurable); + define_native_accessor(vm.names.weekOfYear, week_of_year_getter, {}, Attribute::Configurable); u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.withCalendar, with_calendar, 1, attr); @@ -162,6 +163,22 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::day_of_year_getter) return Value(calendar_day_of_year(global_object, calendar, *temporal_date)); } +// 3.3.10 get Temporal.PlainDate.prototype.weekOfYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.weekofyear +JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::week_of_year_getter) +{ + // 1. Let temporalDate be the this value. + // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]). + auto* temporal_date = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Let calendar be temporalDate.[[Calendar]]. + auto& calendar = temporal_date->calendar(); + + // Return ? CalendarWeekOfYear(calendar, temporalDate). + return Value(calendar_week_of_year(global_object, calendar, *temporal_date)); +} + // 3.3.22 Temporal.PlainDate.prototype.withCalendar ( calendar ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.withcalendar JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::with_calendar) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h index c15bcc5d021..e7beffa25e7 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h @@ -26,6 +26,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(day_getter); JS_DECLARE_NATIVE_FUNCTION(day_of_week_getter); JS_DECLARE_NATIVE_FUNCTION(day_of_year_getter); + JS_DECLARE_NATIVE_FUNCTION(week_of_year_getter); JS_DECLARE_NATIVE_FUNCTION(with_calendar); JS_DECLARE_NATIVE_FUNCTION(equals); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.weekOfYear.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.weekOfYear.js new file mode 100644 index 00000000000..f0bdbfb0568 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.weekOfYear.js @@ -0,0 +1,14 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const date = new Temporal.PlainDate(2021, 7, 23); + expect(date.weekOfYear).toBe(29); + }); +}); + +test("errors", () => { + test("this value must be a Temporal.PlainDate object", () => { + expect(() => { + Reflect.get(Temporal.PlainDate.prototype, "weekOfYear", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainDate"); + }); +});