LibJS: Implement Temporal.PlainDate.prototype.with/withCalendar

This commit is contained in:
Timothy Flynn 2024-11-22 11:06:13 -05:00 committed by Andreas Kling
parent c2ead84bd9
commit f280a96e35
Notes: github-actions[bot] 2024-11-23 13:48:04 +00:00
4 changed files with 127 additions and 0 deletions

View file

@ -49,6 +49,8 @@ void PlainDatePrototype::initialize(Realm& realm)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(realm, vm.names.add, add, 1, attr);
define_native_function(realm, vm.names.subtract, subtract, 1, attr);
define_native_function(realm, vm.names.with, with, 1, attr);
define_native_function(realm, vm.names.withCalendar, with_calendar, 1, attr);
define_native_function(realm, vm.names.equals, equals, 1, attr);
define_native_function(realm, vm.names.toString, to_string, 0, attr);
define_native_function(realm, vm.names.toLocaleString, to_locale_string, 0, attr);
@ -211,6 +213,61 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::subtract)
return TRY(add_duration_to_date(vm, ArithmeticOperation::Subtract, temporal_date, temporal_duration_like, options));
}
// 3.3.23 Temporal.PlainDate.prototype.with ( temporalDateLike [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.with
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::with)
{
auto temporal_date_like = vm.argument(0);
auto options = vm.argument(1);
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto temporal_date = TRY(typed_this_object(vm));
// 3. If ? IsPartialTemporalObject(temporalDateLike) is false, throw a TypeError exception.
if (!TRY(is_partial_temporal_object(vm, temporal_date_like)))
return vm.throw_completion<TypeError>(ErrorType::TemporalObjectMustBePartialTemporalObject);
// 4. Let calendar be temporalDate.[[Calendar]].
auto const& calendar = temporal_date->calendar();
// 5. Let fields be ISODateToFields(calendar, temporalDate.[[ISODate]], DATE).
auto fields = iso_date_to_fields(calendar, temporal_date->iso_date(), DateType::Date);
// 6. Let partialDate be ? PrepareCalendarFields(calendar, temporalDateLike, « YEAR, MONTH, MONTH-CODE, DAY », « », PARTIAL).
auto partial_date = TRY(prepare_calendar_fields(vm, calendar, temporal_date_like.as_object(), { { CalendarField::Year, CalendarField::Month, CalendarField::MonthCode, CalendarField::Day } }, {}, Partial {}));
// 7. Set fields to CalendarMergeFields(calendar, fields, partialDate).
fields = calendar_merge_fields(calendar, fields, partial_date);
// 8. Let resolvedOptions be ? GetOptionsObject(options).
auto resolved_options = TRY(get_options_object(vm, options));
// 9. Let overflow be ? GetTemporalOverflowOption(resolvedOptions).
auto overflow = TRY(get_temporal_overflow_option(vm, resolved_options));
// 10. Let isoDate be ? CalendarDateFromFields(calendar, fields, overflow).
auto iso_date = TRY(calendar_date_from_fields(vm, calendar, fields, overflow));
// 11. Return ! CreateTemporalDate(isoDate, calendar).
return MUST(create_temporal_date(vm, iso_date, calendar));
}
// 3.3.24 Temporal.PlainDate.prototype.withCalendar ( calendarLike ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.with
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::with_calendar)
{
auto calendar_like = vm.argument(0);
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto temporal_date = TRY(typed_this_object(vm));
// 3. Let calendar be ? ToTemporalCalendarIdentifier(calendarLike).
auto calendar = TRY(to_temporal_calendar_identifier(vm, calendar_like));
// 4. Return ! CreateTemporalDate(temporalDate.[[ISODate]], calendar).
return MUST(create_temporal_date(vm, temporal_date->iso_date(), calendar));
}
// 3.3.27 Temporal.PlainDate.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.equals
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::equals)
{

View file

@ -41,6 +41,8 @@ private:
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
JS_DECLARE_NATIVE_FUNCTION(add);
JS_DECLARE_NATIVE_FUNCTION(subtract);
JS_DECLARE_NATIVE_FUNCTION(with);
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
JS_DECLARE_NATIVE_FUNCTION(equals);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);

View file

@ -0,0 +1,55 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.PlainDate.prototype.with).toHaveLength(1);
});
test("basic functionality", () => {
const plainDate = new Temporal.PlainDate(1970, 1, 1);
const values = [
[{ year: 2021 }, new Temporal.PlainDate(2021, 1, 1)],
[{ year: 2021, month: 7 }, new Temporal.PlainDate(2021, 7, 1)],
[{ year: 2021, month: 7, day: 6 }, new Temporal.PlainDate(2021, 7, 6)],
[{ year: 2021, monthCode: "M07", day: 6 }, new Temporal.PlainDate(2021, 7, 6)],
];
for (const [arg, expected] of values) {
expect(plainDate.with(arg).equals(expected)).toBeTrue();
}
// Supplying the same values doesn't change the date, but still creates a new object
const plainDateLike = { year: plainDate.year, month: plainDate.month, day: plainDate.day };
expect(plainDate.with(plainDateLike)).not.toBe(plainDate);
expect(plainDate.with(plainDateLike).equals(plainDate)).toBeTrue();
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainDate object", () => {
expect(() => {
Temporal.PlainDate.prototype.with.call("foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDate");
});
test("argument must be an object", () => {
expect(() => {
new Temporal.PlainDate(1970, 1, 1).with("foo");
}).toThrowWithMessage(TypeError, "Object must be a partial Temporal object");
expect(() => {
new Temporal.PlainDate(1970, 1, 1).with(42);
}).toThrowWithMessage(TypeError, "Object must be a partial Temporal object");
});
test("argument must have one of 'day', 'month', 'monthCode', 'year'", () => {
expect(() => {
new Temporal.PlainDate(1970, 1, 1).with({});
}).toThrowWithMessage(TypeError, "Object must be a partial Temporal object");
});
test("argument must not have 'calendar' or 'timeZone'", () => {
expect(() => {
new Temporal.PlainDate(1970, 1, 1).with({ calendar: {} });
}).toThrowWithMessage(TypeError, "Object must be a partial Temporal object");
expect(() => {
new Temporal.PlainDate(1970, 1, 1).with({ timeZone: {} });
}).toThrowWithMessage(TypeError, "Object must be a partial Temporal object");
});
});

View file

@ -0,0 +1,13 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.PlainDate.prototype.withCalendar).toHaveLength(1);
});
test("basic functionality", () => {
const calendar = "gregory";
const firstPlainDate = new Temporal.PlainDate(1, 1, 1);
expect(firstPlainDate.calendarId).not.toBe(calendar);
const secondPlainDate = firstPlainDate.withCalendar(calendar);
expect(secondPlainDate.calendarId).toBe(calendar);
});
});