diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-07-23 16:48:45 +0300 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-07-23 22:00:23 +0100 |
commit | a0af9b11fba0d8710bdca8df8e5a080431f1b37f (patch) | |
tree | 2543e2000538ec2a58d3dc5518f35f399d06be99 /Userland/Libraries | |
parent | 3bec18432a5a77ec093c67c0af0f2600b1ae7490 (diff) | |
download | serenity-a0af9b11fba0d8710bdca8df8e5a080431f1b37f.zip |
LibJS: Implement Temporal.PlainDate.prototype.month
Diffstat (limited to 'Userland/Libraries')
5 files changed, 54 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index e3379390e8..50dbe0fbfd 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -140,6 +140,27 @@ double calendar_year(GlobalObject& global_object, Object& calendar, Object& date return result.to_integer_or_infinity(global_object); } +// 12.1.10 CalendarMonth ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmonth +double calendar_month(GlobalObject& global_object, Object& calendar, Object& date_like) +{ + auto& vm = global_object.vm(); + // 1. Assert: Type(calendar) is Object. + + // 2. Let result be ? Invoke(calendar, "month", ยซ dateLike ยป). + auto result = calendar.invoke(vm.names.month, &date_like); + if (vm.exception()) + return {}; + + // 3. If result is undefined, throw a RangeError exception. + if (result.is_undefined()) { + vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.month.as_string()); + return {}; + } + + // 4. Return ? ToPositiveIntegerOrInfinity(result). + return to_positive_integer_or_infinity(global_object, result); +} + // 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 0dc770821f..6d28b212e3 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h @@ -35,6 +35,7 @@ Calendar* get_builtin_calendar(GlobalObject&, String const& identifier); Calendar* get_iso8601_calendar(GlobalObject&); Vector<String> calendar_fields(GlobalObject&, Object& calendar, Vector<StringView> const& field_names); double calendar_year(GlobalObject&, Object& calendar, Object& date_like); +double calendar_month(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 5129dccdd2..c633a6245e 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp @@ -28,6 +28,7 @@ void PlainDatePrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.year, year_getter, {}, Attribute::Configurable); + define_native_accessor(vm.names.month, month_getter, {}, Attribute::Configurable); u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.withCalendar, with_calendar, 1, attr); @@ -77,6 +78,22 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::year_getter) return Value(calendar_year(global_object, calendar, *temporal_date)); } +// 3.3.5 get Temporal.PlainDate.prototype.month, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.month +JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::month_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(); + + // 4. Return ? CalendarMonth(calendar, temporalDate). + return Value(calendar_month(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 d055e367f9..97c304024c 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h @@ -21,6 +21,7 @@ public: private: JS_DECLARE_NATIVE_FUNCTION(calendar_getter); JS_DECLARE_NATIVE_FUNCTION(year_getter); + JS_DECLARE_NATIVE_FUNCTION(month_getter); JS_DECLARE_NATIVE_FUNCTION(with_calendar); JS_DECLARE_NATIVE_FUNCTION(equals); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.month.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.month.js new file mode 100644 index 0000000000..954de91f35 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.month.js @@ -0,0 +1,14 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const date = new Temporal.PlainDate(2021, 7, 23); + expect(date.month).toBe(7); + }); +}); + +test("errors", () => { + test("this value must be a Temporal.PlainDate object", () => { + expect(() => { + Reflect.get(Temporal.PlainDate.prototype, "month", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainDate"); + }); +}); |