summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-07-23 17:08:07 +0300
committerLinus Groh <mail@linusgroh.de>2021-07-23 22:00:23 +0100
commit59dc0e8421c320b496c84bdcb918ec06c80e4246 (patch)
treebcff00e85a9d2641823a4a1a51ac54afefe981ad /Userland
parent8a20f258f012c50b51456e0f2630db80cb3cf589 (diff)
downloadserenity-59dc0e8421c320b496c84bdcb918ec06c80e4246.zip
LibJS: Implement Temporal.PlainDate.prototype.day
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp21
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp17
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.day.js14
5 files changed, 54 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp
index 6e64673d0c..e7b2e07dd3 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp
@@ -182,6 +182,27 @@ String calendar_month_code(GlobalObject& global_object, Object& calendar, Object
return result.to_string(global_object);
}
+// 12.1.12 CalendarDay ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarday
+double calendar_day(GlobalObject& global_object, Object& calendar, Object& date_like)
+{
+ auto& vm = global_object.vm();
+ // 1. Assert: Type(calendar) is Object.
+
+ // 2. Let result be ? Invoke(calendar, "day", ยซ dateLike ยป).
+ auto result = calendar.invoke(vm.names.day, &date_like);
+ if (vm.exception())
+ return {};
+
+ // 3. If result is undefined, throw a RangeError exception.
+ if (result.is_undefined()) {
+ vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.day.as_string());
+ return {};
+ }
+
+ // 4. Return ? ToPositiveIntegerOrInfinity(result).
+ return to_positive_integer_or_infinity(global_object, result);
+}
+
// 12.1.21 ToTemporalCalendar ( temporalCalendarLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalcalendar
Object* to_temporal_calendar(GlobalObject& global_object, Value temporal_calendar_like)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h
index 95f0cd37cf..a822542c27 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h
@@ -37,6 +37,7 @@ Vector<String> calendar_fields(GlobalObject&, Object& calendar, Vector<StringVie
double calendar_year(GlobalObject&, Object& calendar, Object& date_like);
double calendar_month(GlobalObject&, Object& calendar, Object& date_like);
String calendar_month_code(GlobalObject&, Object& calendar, Object& date_like);
+double calendar_day(GlobalObject&, Object& calendar, Object& date_like);
Object* to_temporal_calendar(GlobalObject&, Value);
Object* to_temporal_calendar_with_iso_default(GlobalObject&, Value);
Object* get_temporal_calendar_with_iso_default(GlobalObject&, Object&);
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp
index 811469a8bd..561244daf2 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp
@@ -30,6 +30,7 @@ void PlainDatePrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.year, year_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.month, month_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.monthCode, month_code_getter, {}, Attribute::Configurable);
+ define_native_accessor(vm.names.day, day_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.withCalendar, with_calendar, 1, attr);
@@ -111,6 +112,22 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::month_code_getter)
return js_string(vm, calendar_month_code(global_object, calendar, *temporal_date));
}
+// 3.3.7 get Temporal.PlainDate.prototype.day, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.day
+JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::day_getter)
+{
+ // 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. Return ? CalendarDay(calendar, temporalDate).
+ return Value(calendar_day(global_object, calendar, *temporal_date));
+}
+
// 3.3.22 Temporal.PlainDate.prototype.withCalendar ( calendar ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.withcalendar
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::with_calendar)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h
index 0288e2033c..26e9e83e41 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h
@@ -23,6 +23,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(year_getter);
JS_DECLARE_NATIVE_FUNCTION(month_getter);
JS_DECLARE_NATIVE_FUNCTION(month_code_getter);
+ JS_DECLARE_NATIVE_FUNCTION(day_getter);
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
JS_DECLARE_NATIVE_FUNCTION(equals);
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.day.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.day.js
new file mode 100644
index 0000000000..3f71be275f
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.day.js
@@ -0,0 +1,14 @@
+describe("correct behavior", () => {
+ test("basic functionality", () => {
+ const date = new Temporal.PlainDate(2021, 7, 23);
+ expect(date.day).toBe(23);
+ });
+});
+
+test("errors", () => {
+ test("this value must be a Temporal.PlainDate object", () => {
+ expect(() => {
+ Reflect.get(Temporal.PlainDate.prototype, "day", "foo");
+ }).toThrowWithMessage(TypeError, "Not a Temporal.PlainDate");
+ });
+});