LibJS: Implement Temporal.PlainDate.prototype.toPlainMonthDay()

This commit is contained in:
Linus Groh 2021-08-16 18:13:47 +01:00
parent 31f65b8c50
commit 795e077eb8
6 changed files with 76 additions and 0 deletions

View file

@ -392,6 +392,7 @@ namespace JS {
P(toLowerCase) \
P(toPlainDate) \
P(toPlainDateTime) \
P(toPlainMonthDay) \
P(toPlainTime) \
P(toPlainYearMonth) \
P(toString) \

View file

@ -447,6 +447,36 @@ PlainYearMonth* year_month_from_fields(GlobalObject& global_object, Object& cale
return static_cast<PlainYearMonth*>(year_month_object);
}
// 12.1.26 MonthDayFromFields ( calendar, fields [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal-monthdayfromfields
PlainMonthDay* month_day_from_fields(GlobalObject& global_object, Object& calendar, Object& fields, Object* options)
{
auto& vm = global_object.vm();
// 1. Assert: Type(calendar) is Object.
// 2. Assert: Type(fields) is Object.
// 3. If options is not present, then
// a. Set options to undefined.
// 4. Else,
// a. Assert: Type(options) is Object.
// 5. Let monthDay be ? Invoke(calendar, "monthDayFromFields", « fields, options »).
auto month_day = Value(&calendar).invoke(global_object, vm.names.monthDayFromFields, &fields, options ?: js_undefined());
if (vm.exception())
return {};
// 6. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
auto* month_day_object = month_day.to_object(global_object);
if (!month_day_object)
return {};
if (!is<PlainMonthDay>(month_day_object)) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Temporal.PlainMonthDay");
return {};
}
// 7. Return monthDay.
return static_cast<PlainMonthDay*>(month_day_object);
}
// 12.1.28 CalendarEquals ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal-calendarequals
bool calendar_equals(GlobalObject& global_object, Object& one, Object& two)
{

View file

@ -51,6 +51,7 @@ Object* to_temporal_calendar_with_iso_default(GlobalObject&, Value);
Object* get_temporal_calendar_with_iso_default(GlobalObject&, Object&);
PlainDate* date_from_fields(GlobalObject&, Object& calendar, Object& fields, Object& options);
PlainYearMonth* year_month_from_fields(GlobalObject&, Object& calendar, Object& fields, Object* options = nullptr);
PlainMonthDay* month_day_from_fields(GlobalObject& global_object, Object& calendar, Object& fields, Object* options = nullptr);
bool calendar_equals(GlobalObject&, Object& one, Object& two);
Object* consolidate_calendars(GlobalObject&, Object& one, Object& two);
bool is_iso_leap_year(i32 year);

View file

@ -11,6 +11,7 @@
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/PlainDate.h>
#include <LibJS/Runtime/Temporal/PlainDatePrototype.h>
#include <LibJS/Runtime/Temporal/PlainMonthDay.h>
#include <LibJS/Runtime/Temporal/PlainYearMonth.h>
namespace JS::Temporal {
@ -46,6 +47,7 @@ void PlainDatePrototype::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.toPlainYearMonth, to_plain_year_month, 0, attr);
define_native_function(vm.names.toPlainMonthDay, to_plain_month_day, 0, attr);
define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr);
define_native_function(vm.names.withCalendar, with_calendar, 1, attr);
define_native_function(vm.names.equals, equals, 1, attr);
@ -296,6 +298,32 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_plain_year_month)
return year_month_from_fields(global_object, calendar, *fields);
}
// 3.3.17 Temporal.PlainDate.prototype.toPlainMonthDay ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.toplainmonthday
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_plain_month_day)
{
// 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. Let fieldNames be ? CalendarFields(calendar, « "day", "monthCode" »).
auto field_names = calendar_fields(global_object, calendar, { "day"sv, "monthCode"sv });
if (vm.exception())
return {};
// 5. Let fields be ? PrepareTemporalFields(temporalDate, fieldNames, «»).
auto* fields = prepare_temporal_fields(global_object, *temporal_date, field_names, {});
if (vm.exception())
return {};
// 6. Return ? MonthDayFromFields(calendar, fields).
return month_day_from_fields(global_object, calendar, *fields);
}
// 3.3.18 Temporal.PlainDate.prototype.getISOFields ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.getisofields
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::get_iso_fields)
{

View file

@ -33,6 +33,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
JS_DECLARE_NATIVE_FUNCTION(to_plain_year_month);
JS_DECLARE_NATIVE_FUNCTION(to_plain_month_day);
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
JS_DECLARE_NATIVE_FUNCTION(equals);

View file

@ -0,0 +1,15 @@
describe("correct behavior", () => {
test("length is 0", () => {
expect(Temporal.PlainDate.prototype.toPlainMonthDay).toHaveLength(0);
});
test("basic functionality", () => {
const plainDate = new Temporal.PlainDate(2021, 7, 6);
const plainMonthDay = plainDate.toPlainMonthDay();
expect(plainMonthDay.calendar).toBe(plainDate.calendar);
expect(plainMonthDay.monthCode).toBe("M07");
expect(plainMonthDay.day).toBe(6);
const fields = plainMonthDay.getISOFields();
expect(fields.isoYear).toBe(1972);
});
});