1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:47:34 +00:00

LibJS: Implement Temporal.Calendar.prototype.monthDayFromFields()

This commit is contained in:
Linus Groh 2021-08-16 18:04:58 +01:00
parent 68d07320cf
commit 31f65b8c50
7 changed files with 184 additions and 0 deletions

View file

@ -276,6 +276,7 @@ namespace JS {
P(minutes) \
P(month) \
P(monthCode) \
P(monthDayFromFields) \
P(months) \
P(monthsInYear) \
P(multiline) \

View file

@ -787,6 +787,85 @@ Optional<ISOYearMonth> iso_year_month_from_fields(GlobalObject& global_object, O
return ISOYearMonth { .year = result->year, .month = result->month, .reference_iso_day = 1 };
}
// 12.1.40 ISOMonthDayFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthdayfromfields
Optional<ISOMonthDay> iso_month_day_from_fields(GlobalObject& global_object, Object& fields, Object& options)
{
auto& vm = global_object.vm();
// 1. Assert: Type(fields) is Object.
// 2. Let overflow be ? ToTemporalOverflow(options).
auto overflow = to_temporal_overflow(global_object, options);
if (vm.exception())
return {};
// 3. Set fields to ? PrepareTemporalFields(fields, « "day", "month", "monthCode", "year" », «»).
auto* prepared_fields = prepare_temporal_fields(global_object, fields, { "day"sv, "month"sv, "monthCode"sv, "year"sv }, {});
if (vm.exception())
return {};
// 4. Let month be ? Get(fields, "month").
auto month_value = prepared_fields->get(vm.names.month);
if (vm.exception())
return {};
// 5. Let monthCode be ? Get(fields, "monthCode").
auto month_code = prepared_fields->get(vm.names.monthCode);
if (vm.exception())
return {};
// 6. Let year be ? Get(fields, "year").
auto year = prepared_fields->get(vm.names.year);
if (vm.exception())
return {};
// 7. If month is not undefined, and monthCode and year are both undefined, then
if (!month_value.is_undefined() && month_code.is_undefined() && year.is_undefined()) {
// a. Throw a TypeError exception.
vm.throw_exception<TypeError>(global_object, ErrorType::TemporalMissingRequiredProperty, "monthCode or year");
return {};
}
// 8. Set month to ? ResolveISOMonth(fields).
auto month = resolve_iso_month(global_object, *prepared_fields);
if (vm.exception())
return {};
// 9. Let day be ? Get(fields, "day").
auto day = prepared_fields->get(vm.names.day);
if (vm.exception())
return {};
// 10. If day is undefined, throw a TypeError exception.
if (day.is_undefined()) {
vm.throw_exception<TypeError>(global_object, ErrorType::TemporalMissingRequiredProperty, vm.names.day.as_string());
return {};
}
// 11. Let referenceISOYear be 1972 (the first leap year after the Unix epoch).
i32 reference_iso_year = 1972;
Optional<ISODate> result;
// 12. If monthCode is undefined, then
if (month_code.is_undefined()) {
// a. Let result be ? RegulateISODate(year, month, day, overflow).
result = regulate_iso_date(global_object, year.as_double(), month, day.as_double(), *overflow);
if (vm.exception())
return {};
}
// 13. Else,
else {
// a. Let result be ? RegulateISODate(referenceISOYear, month, day, overflow).
result = regulate_iso_date(global_object, reference_iso_year, month, day.as_double(), *overflow);
if (vm.exception())
return {};
}
// 14. Return the new Record { [[Month]]: result.[[Month]], [[Day]]: result.[[Day]], [[ReferenceISOYear]]: referenceISOYear }.
return ISOMonthDay { .month = result->month, .day = result->day, .reference_iso_year = reference_iso_year };
}
// 12.1.41 ISOYear ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isoyear
i32 iso_year(Object& temporal_object)
{

View file

@ -9,6 +9,7 @@
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/Temporal/PlainDate.h>
#include <LibJS/Runtime/Temporal/PlainMonthDay.h>
#include <LibJS/Runtime/Temporal/PlainYearMonth.h>
#include <LibJS/Runtime/Value.h>
@ -62,6 +63,7 @@ String build_iso_month_code(u8 month);
double resolve_iso_month(GlobalObject&, Object& fields);
Optional<ISODate> iso_date_from_fields(GlobalObject&, Object& fields, Object& options);
Optional<ISOYearMonth> iso_year_month_from_fields(GlobalObject&, Object& fields, Object& options);
Optional<ISOMonthDay> iso_month_day_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);

View file

@ -36,6 +36,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.dateFromFields, date_from_fields, 2, attr);
define_native_function(vm.names.yearMonthFromFields, year_month_from_fields, 2, attr);
define_native_function(vm.names.monthDayFromFields, month_day_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);
@ -144,6 +145,40 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::year_month_from_fields)
return create_temporal_year_month(global_object, result->year, result->month, *calendar, result->reference_iso_day);
}
// 12.4.6 Temporal.Calendar.prototype.monthDayFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.monthdayfromfields
// NOTE: This is the minimum monthDayFromFields implementation for engines without ECMA-402.
JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::month_day_from_fields)
{
// 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);
// 4. If Type(fields) is not Object, throw a TypeError exception.
auto fields = vm.argument(0);
if (!fields.is_object()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, fields.to_string_without_side_effects());
return {};
}
// 5. Set options to ? GetOptionsObject(options).
auto* options = get_options_object(global_object, vm.argument(1));
if (vm.exception())
return {};
// 6. Let result be ? ISOMonthDayFromFields(fields, options).
auto result = iso_month_day_from_fields(global_object, fields.as_object(), *options);
if (vm.exception())
return {};
// 7. Return ? CreateTemporalMonthDay(result.[[Month]], result.[[Day]], calendar, result.[[ReferenceISOYear]]).
return create_temporal_month_day(global_object, result->month, result->day, *calendar, result->reference_iso_year);
}
// 12.4.9 Temporal.Calendar.prototype.year ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.year
// NOTE: This is the minimum year implementation for engines without ECMA-402.
JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::year)

View file

@ -22,6 +22,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(id_getter);
JS_DECLARE_NATIVE_FUNCTION(date_from_fields);
JS_DECLARE_NATIVE_FUNCTION(year_month_from_fields);
JS_DECLARE_NATIVE_FUNCTION(month_day_from_fields);
JS_DECLARE_NATIVE_FUNCTION(year);
JS_DECLARE_NATIVE_FUNCTION(month);
JS_DECLARE_NATIVE_FUNCTION(month_code);

View file

@ -33,6 +33,12 @@ private:
Object& m_calendar; // [[Calendar]]
};
struct ISOMonthDay {
u8 month;
u8 day;
i32 reference_iso_year;
};
PlainMonthDay* create_temporal_month_day(GlobalObject&, u8 iso_month, u8 iso_day, Object& calendar, i32 reference_iso_year, FunctionObject* new_target = nullptr);
}

View file

@ -0,0 +1,60 @@
describe("correct behavior", () => {
test("length is 2", () => {
expect(Temporal.Calendar.prototype.monthDayFromFields).toHaveLength(2);
});
test("basic functionality", () => {
const calendar = new Temporal.Calendar("iso8601");
const plainMonthDay = calendar.monthDayFromFields({ year: 2021, month: 7, day: 6 });
expect(plainMonthDay.calendar).toBe(calendar);
expect(plainMonthDay.monthCode).toBe("M07");
expect(plainMonthDay.day).toBe(6);
const fields = plainMonthDay.getISOFields();
expect(fields.isoYear).toBe(1972); // No, this isn't a mistake
});
test("with monthCode", () => {
const calendar = new Temporal.Calendar("iso8601");
const plainMonthDay = calendar.monthDayFromFields({ monthCode: "M07", day: 6 });
expect(plainMonthDay.calendar).toBe(calendar);
expect(plainMonthDay.monthCode).toBe("M07");
expect(plainMonthDay.day).toBe(6);
const fields = plainMonthDay.getISOFields();
expect(fields.isoYear).toBe(1972);
});
});
describe("errors", () => {
test("first argument must be an object", () => {
const calendar = new Temporal.Calendar("iso8601");
expect(() => {
calendar.monthDayFromFields(42);
}).toThrowWithMessage(TypeError, "42 is not an object");
});
test("month or monthCode field is required", () => {
const calendar = new Temporal.Calendar("iso8601");
expect(() => {
calendar.monthDayFromFields({ year: 2021 });
}).toThrowWithMessage(TypeError, "Required property month is missing or undefined");
});
test("day field is required", () => {
const calendar = new Temporal.Calendar("iso8601");
expect(() => {
calendar.monthDayFromFields({ year: 2021, month: 7 });
}).toThrowWithMessage(TypeError, "Required property day is missing or undefined");
});
test("monthCode or year field is required when month is given", () => {
const calendar = new Temporal.Calendar("iso8601");
expect(() => {
calendar.monthDayFromFields({ month: 7, day: 6 });
}).toThrowWithMessage(
TypeError,
"Required property monthCode or year is missing or undefined"
);
});
});