mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:57:45 +00:00
LibJS: Implement Temporal.PlainDateTime.prototype.withPlainDate
This commit is contained in:
parent
f657c86d58
commit
010761aff4
7 changed files with 80 additions and 0 deletions
|
@ -407,6 +407,7 @@ namespace JS {
|
||||||
P(weeks) \
|
P(weeks) \
|
||||||
P(with) \
|
P(with) \
|
||||||
P(withCalendar) \
|
P(withCalendar) \
|
||||||
|
P(withPlainDate) \
|
||||||
P(writable) \
|
P(writable) \
|
||||||
P(year) \
|
P(year) \
|
||||||
P(years)
|
P(years)
|
||||||
|
|
|
@ -166,6 +166,7 @@
|
||||||
M(StringNonGlobalRegExp, "RegExp argument is non-global") \
|
M(StringNonGlobalRegExp, "RegExp argument is non-global") \
|
||||||
M(StringRawCannotConvert, "Cannot convert property 'raw' to object from {}") \
|
M(StringRawCannotConvert, "Cannot convert property 'raw' to object from {}") \
|
||||||
M(StringRepeatCountMustBe, "repeat count must be a {} number") \
|
M(StringRepeatCountMustBe, "repeat count must be a {} number") \
|
||||||
|
M(TemporalInvalidCalendar, "Invalid calendar") \
|
||||||
M(TemporalInvalidCalendarFunctionResult, "Invalid calendar, {}() function returned undefined") \
|
M(TemporalInvalidCalendarFunctionResult, "Invalid calendar, {}() function returned undefined") \
|
||||||
M(TemporalInvalidCalendarIdentifier, "Invalid calendar identifier '{}'") \
|
M(TemporalInvalidCalendarIdentifier, "Invalid calendar identifier '{}'") \
|
||||||
M(TemporalInvalidDuration, "Invalid duration") \
|
M(TemporalInvalidDuration, "Invalid duration") \
|
||||||
|
|
|
@ -428,6 +428,41 @@ bool calendar_equals(GlobalObject& global_object, Object& one, Object& two)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 12.1.29 ConsolidateCalendars ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal-consolidatecalendars
|
||||||
|
Object* consolidate_calendars(GlobalObject& global_object, Object& one, Object& two)
|
||||||
|
{
|
||||||
|
auto& vm = global_object.vm();
|
||||||
|
// 1. If one and two are the same Object value, return two.
|
||||||
|
if (&one == &two)
|
||||||
|
return &two;
|
||||||
|
|
||||||
|
// 2. Let calendarOne be ? ToString(one).
|
||||||
|
auto calendar_one = Value(&one).to_string(global_object);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// 3. Let calendarTwo be ? ToString(two).
|
||||||
|
auto calendar_two = Value(&two).to_string(global_object);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// 4. If calendarOne is calendarTwo, return two.
|
||||||
|
if (calendar_one == calendar_two)
|
||||||
|
return &two;
|
||||||
|
|
||||||
|
// 5. If calendarOne is "iso8601", return two.
|
||||||
|
if (calendar_one == "iso8601"sv)
|
||||||
|
return &two;
|
||||||
|
|
||||||
|
// 6. If calendarTwo is "iso8601", return one.
|
||||||
|
if (calendar_two == "iso8601"sv)
|
||||||
|
return &one;
|
||||||
|
|
||||||
|
// 7. Throw a RangeError exception.
|
||||||
|
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidCalendar);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
// 12.1.30 IsISOLeapYear ( year ), https://tc39.es/proposal-temporal/#sec-temporal-isisoleapyear
|
// 12.1.30 IsISOLeapYear ( year ), https://tc39.es/proposal-temporal/#sec-temporal-isisoleapyear
|
||||||
bool is_iso_leap_year(i32 year)
|
bool is_iso_leap_year(i32 year)
|
||||||
{
|
{
|
||||||
|
|
|
@ -51,6 +51,7 @@ Object* to_temporal_calendar_with_iso_default(GlobalObject&, Value);
|
||||||
Object* get_temporal_calendar_with_iso_default(GlobalObject&, Object&);
|
Object* get_temporal_calendar_with_iso_default(GlobalObject&, Object&);
|
||||||
PlainDate* date_from_fields(GlobalObject&, Object& calendar, Object& fields, Object& options);
|
PlainDate* date_from_fields(GlobalObject&, Object& calendar, Object& fields, Object& options);
|
||||||
bool calendar_equals(GlobalObject&, Object& one, Object& two);
|
bool calendar_equals(GlobalObject&, Object& one, Object& two);
|
||||||
|
Object* consolidate_calendars(GlobalObject&, Object& one, Object& two);
|
||||||
bool is_iso_leap_year(i32 year);
|
bool is_iso_leap_year(i32 year);
|
||||||
u16 iso_days_in_year(i32 year);
|
u16 iso_days_in_year(i32 year);
|
||||||
u8 iso_days_in_month(i32 year, u8 month);
|
u8 iso_days_in_month(i32 year, u8 month);
|
||||||
|
|
|
@ -48,6 +48,7 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object)
|
||||||
define_native_accessor(vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable);
|
define_native_accessor(vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable);
|
||||||
|
|
||||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||||
|
define_native_function(vm.names.withPlainDate, with_plain_date, 1, attr);
|
||||||
define_native_function(vm.names.withCalendar, with_calendar, 1, attr);
|
define_native_function(vm.names.withCalendar, with_calendar, 1, attr);
|
||||||
define_native_function(vm.names.valueOf, value_of, 0, attr);
|
define_native_function(vm.names.valueOf, value_of, 0, attr);
|
||||||
define_native_function(vm.names.toPlainDate, to_plain_date, 0, attr);
|
define_native_function(vm.names.toPlainDate, to_plain_date, 0, attr);
|
||||||
|
@ -350,6 +351,29 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::in_leap_year_getter)
|
||||||
return calendar_in_leap_year(global_object, calendar, *date_time);
|
return calendar_in_leap_year(global_object, calendar, *date_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 5.3.24 Temporal.PlainDateTime.prototype.withPlainDate ( plainDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.withplaindate
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::with_plain_date)
|
||||||
|
{
|
||||||
|
// 1. Let temporalDateTime be the this value.
|
||||||
|
// 2. Perform ? RequireInternalSlot(temporalDateTime, [[InitializedTemporalDateTime]]).
|
||||||
|
auto* date_time = typed_this(global_object);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// 3. Let plainDate be ? ToTemporalDate(plainDateLike).
|
||||||
|
auto* plain_date = to_temporal_date(global_object, vm.argument(0));
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// 4. Let calendar be ? ConsolidateCalendars(temporalDateTime.[[Calendar]], plainDate.[[Calendar]]).
|
||||||
|
auto* calendar = consolidate_calendars(global_object, date_time->calendar(), plain_date->calendar());
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// 5. Return ? CreateTemporalDateTime(plainDate.[[ISOYear]], plainDate.[[ISOMonth]], plainDate.[[ISODay]], temporalDateTime.[[ISOHour]], temporalDateTime.[[ISOMinute]], temporalDateTime.[[ISOSecond]], temporalDateTime.[[ISOMillisecond]], temporalDateTime.[[ISOMicrosecond]], temporalDateTime.[[ISONanosecond]], calendar).
|
||||||
|
return create_temporal_date_time(global_object, plain_date->iso_year(), plain_date->iso_month(), plain_date->iso_day(), date_time->iso_hour(), date_time->iso_minute(), date_time->iso_second(), date_time->iso_millisecond(), date_time->iso_microsecond(), date_time->iso_nanosecond(), *calendar);
|
||||||
|
}
|
||||||
|
|
||||||
// 5.3.25 Temporal.PlainDateTime.prototype.withCalendar ( calendar ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.withcalendar
|
// 5.3.25 Temporal.PlainDateTime.prototype.withCalendar ( calendar ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.withcalendar
|
||||||
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::with_calendar)
|
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::with_calendar)
|
||||||
{
|
{
|
||||||
|
|
|
@ -38,6 +38,7 @@ private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(days_in_year_getter);
|
JS_DECLARE_NATIVE_FUNCTION(days_in_year_getter);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
|
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
|
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(with_plain_date);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
|
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(to_plain_date);
|
JS_DECLARE_NATIVE_FUNCTION(to_plain_date);
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
describe("correct behavior", () => {
|
||||||
|
test("length is 1", () => {
|
||||||
|
expect(Temporal.PlainDateTime.prototype.withPlainDate).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("basic functionality", () => {
|
||||||
|
const firstPlainDateTime = new Temporal.PlainDateTime(1, 2, 3, 4, 5, 6);
|
||||||
|
const plainDate = new Temporal.PlainDate(7, 8, 9);
|
||||||
|
const secondPlainDateTime = firstPlainDateTime.withPlainDate(plainDate);
|
||||||
|
expect(secondPlainDateTime.year).toBe(7);
|
||||||
|
expect(secondPlainDateTime.month).toBe(8);
|
||||||
|
expect(secondPlainDateTime.day).toBe(9);
|
||||||
|
expect(secondPlainDateTime.hour).toBe(4);
|
||||||
|
expect(secondPlainDateTime.minute).toBe(5);
|
||||||
|
expect(secondPlainDateTime.second).toBe(6);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue