summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-07-31 01:39:36 +0300
committerLinus Groh <mail@linusgroh.de>2021-07-31 00:16:41 +0100
commit010761aff4407189cac61ae2c56b704756dfda54 (patch)
tree7d6b797ca7c683606b8533625e275c641e701843 /Userland
parentf657c86d588933f6ec82fda28178303011353992 (diff)
downloadserenity-010761aff4407189cac61ae2c56b704756dfda54.zip
LibJS: Implement Temporal.PlainDateTime.prototype.withPlainDate
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/ErrorTypes.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp35
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp24
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.withPlainDate.js17
7 files changed, 80 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
index 1420fa56ca..93c6df3e0b 100644
--- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
+++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
@@ -407,6 +407,7 @@ namespace JS {
P(weeks) \
P(with) \
P(withCalendar) \
+ P(withPlainDate) \
P(writable) \
P(year) \
P(years)
diff --git a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h
index 39f366061d..6086108a80 100644
--- a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h
+++ b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h
@@ -166,6 +166,7 @@
M(StringNonGlobalRegExp, "RegExp argument is non-global") \
M(StringRawCannotConvert, "Cannot convert property 'raw' to object from {}") \
M(StringRepeatCountMustBe, "repeat count must be a {} number") \
+ M(TemporalInvalidCalendar, "Invalid calendar") \
M(TemporalInvalidCalendarFunctionResult, "Invalid calendar, {}() function returned undefined") \
M(TemporalInvalidCalendarIdentifier, "Invalid calendar identifier '{}'") \
M(TemporalInvalidDuration, "Invalid duration") \
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp
index 0369fdb37e..1f1f6a6fc9 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp
@@ -428,6 +428,41 @@ bool calendar_equals(GlobalObject& global_object, Object& one, Object& two)
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
bool is_iso_leap_year(i32 year)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h
index 9700f23cba..319f7e38ff 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h
@@ -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);
bool calendar_equals(GlobalObject&, Object& one, Object& two);
+Object* consolidate_calendars(GlobalObject&, Object& one, Object& two);
bool is_iso_leap_year(i32 year);
u16 iso_days_in_year(i32 year);
u8 iso_days_in_month(i32 year, u8 month);
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp
index f9e45450d6..4566cb1c68 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp
@@ -48,6 +48,7 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.inLeapYear, in_leap_year_getter, {}, 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.valueOf, value_of, 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);
}
+// 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
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::with_calendar)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h
index 2132b1c36a..ca805461bc 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h
@@ -38,6 +38,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(days_in_year_getter);
JS_DECLARE_NATIVE_FUNCTION(months_in_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(value_of);
JS_DECLARE_NATIVE_FUNCTION(to_plain_date);
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.withPlainDate.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.withPlainDate.js
new file mode 100644
index 0000000000..5219379c2d
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.withPlainDate.js
@@ -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);
+ });
+});