summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-07-19 00:29:26 +0300
committerLinus Groh <mail@linusgroh.de>2021-07-19 09:11:20 +0100
commitcc00ccec4119c01110443f057d5771cc8fee6323 (patch)
treece06f75823bc3e6a92735c2468bafdb8a10f012e /Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp
parentff6ca0f02da0e3acf22a3be1a3f41299935e5a87 (diff)
downloadserenity-cc00ccec4119c01110443f057d5771cc8fee6323.zip
LibJS: Start implementing Temporal.PlainDate
This commit adds the PlainDate object itself, its constructor and prototype (currently empty), and several required abstract operations.
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp58
1 files changed, 57 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp
index bf18c9a5c2..efe78699f8 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp
@@ -4,12 +4,68 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <LibJS/Runtime/AbstractOperations.h>
+#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/PlainDate.h>
-#include <LibJS/Runtime/Value.h>
+#include <LibJS/Runtime/Temporal/PlainDateConstructor.h>
+#include <LibJS/Runtime/Temporal/PlainDateTime.h>
namespace JS::Temporal {
+// 3 Temporal.PlainDate Objects, https://tc39.es/proposal-temporal/#sec-temporal-plaindate-objects
+PlainDate::PlainDate(i32 year, i32 month, i32 day, Object& calendar, Object& prototype)
+ : Object(prototype)
+ , m_iso_year(year)
+ , m_iso_month(month)
+ , m_iso_day(day)
+ , m_calendar(calendar)
+{
+}
+
+void PlainDate::visit_edges(Visitor& visitor)
+{
+ visitor.visit(&m_calendar);
+}
+
+// 3.5.1 CreateTemporalDate ( isoYear, isoMonth, isoDay, calendar [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporaldate
+PlainDate* create_temporal_date(GlobalObject& global_object, i32 iso_year, i32 iso_month, i32 iso_day, Object& calendar, FunctionObject* new_target)
+{
+ auto& vm = global_object.vm();
+
+ // 1. Assert: isoYear is an integer.
+ // 2. Assert: isoMonth is an integer.
+ // 3. Assert: isoDay is an integer.
+ // 4. Assert: Type(calendar) is Object.
+
+ // 5. If ! IsValidISODate(isoYear, isoMonth, isoDay) is false, throw a RangeError exception.
+ if (!is_valid_iso_date(iso_year, iso_month, iso_day)) {
+ vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
+ return {};
+ }
+
+ // 6. If ! ISODateTimeWithinLimits(isoYear, isoMonth, isoDay, 12, 0, 0, 0, 0, 0) is false, throw a RangeError exception.
+ if (!iso_date_time_within_limits(global_object, iso_year, iso_month, iso_day, 12, 0, 0, 0, 0, 0)) {
+ vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
+ return {};
+ }
+
+ // 7. If newTarget is not present, set it to %Temporal.PlainDate%.
+ if (!new_target)
+ new_target = global_object.temporal_plain_date_constructor();
+
+ // 8. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.PlainDate.prototype%", ยซ [[InitializedTemporalDate]], [[ISOYear]], [[ISOMonth]], [[ISODay]], [[Calendar]] ยป).
+ // 9. Set object.[[ISOYear]] to isoYear.
+ // 10. Set object.[[ISOMonth]] to isoMonth.
+ // 11. Set object.[[ISODay]] to isoDay.
+ // 12. Set object.[[Calendar]] to calendar.
+ auto* object = ordinary_create_from_constructor<PlainDate>(global_object, *new_target, &GlobalObject::temporal_plain_date_prototype, iso_year, iso_month, iso_day, calendar);
+ if (vm.exception())
+ return {};
+
+ return object;
+}
+
// 3.5.5 IsValidISODate ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-isvalidisodate
bool is_valid_iso_date(i32 year, i32 month, i32 day)
{