| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | /*
 | 
					
						
							|  |  |  |  |  * Copyright (c) 2021, Linus Groh <linusg@serenityos.org> | 
					
						
							|  |  |  |  |  * | 
					
						
							|  |  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #include <AK/Checked.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-22 23:16:42 +01:00
										 |  |  |  | #include <AK/TypeCasts.h>
 | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | #include <LibJS/Runtime/GlobalObject.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-17 20:17:12 +01:00
										 |  |  |  | #include <LibJS/Runtime/Temporal/AbstractOperations.h>
 | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | #include <LibJS/Runtime/Temporal/Calendar.h>
 | 
					
						
							|  |  |  |  | #include <LibJS/Runtime/Temporal/PlainDateTime.h>
 | 
					
						
							|  |  |  |  | #include <LibJS/Runtime/Temporal/PlainDateTimeConstructor.h>
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace JS::Temporal { | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // 5.1 The Temporal.PlainDateTime Constructor, https://tc39.es/proposal-temporal/#sec-temporal-plaindatetime-constructor
 | 
					
						
							|  |  |  |  | PlainDateTimeConstructor::PlainDateTimeConstructor(GlobalObject& global_object) | 
					
						
							|  |  |  |  |     : NativeFunction(vm().names.PlainDateTime.as_string(), *global_object.function_prototype()) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void PlainDateTimeConstructor::initialize(GlobalObject& global_object) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     NativeFunction::initialize(global_object); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 5.2.1 Temporal.PlainDateTime.prototype, https://tc39.es/proposal-temporal/#sec-temporal-plaindatetime-prototype
 | 
					
						
							|  |  |  |  |     define_direct_property(vm.names.prototype, global_object.temporal_plain_date_time_prototype(), 0); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-22 23:16:42 +01:00
										 |  |  |  |     u8 attr = Attribute::Writable | Attribute::Configurable; | 
					
						
							| 
									
										
										
										
											2021-10-21 20:09:59 +01:00
										 |  |  |  |     define_native_function(vm.names.from, from, 1, attr); | 
					
						
							|  |  |  |  |     define_native_function(vm.names.compare, compare, 2, attr); | 
					
						
							| 
									
										
										
										
											2021-08-22 23:16:42 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  |     define_direct_property(vm.names.length, Value(3), Attribute::Configurable); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // 5.1.1 Temporal.PlainDateTime ( isoYear, isoMonth, isoDay [ , hour [ , minute [ , second [ , millisecond [ , microsecond [ , nanosecond [ , calendarLike ] ] ] ] ] ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |  | ThrowCompletionOr<Value> PlainDateTimeConstructor::call() | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 1. If NewTarget is undefined, throw a TypeError exception.
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |  |     return vm.throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.PlainDateTime"); | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // 5.1.1 Temporal.PlainDateTime ( isoYear, isoMonth, isoDay [ , hour [ , minute [ , second [ , millisecond [ , microsecond [ , nanosecond [ , calendarLike ] ] ] ] ] ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |  | ThrowCompletionOr<Object*> PlainDateTimeConstructor::construct(FunctionObject& new_target) | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  |  |     auto& global_object = this->global_object(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-17 20:17:12 +01:00
										 |  |  |  |     // 2. Let isoYear be ? ToIntegerThrowOnInfinity(isoYear).
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |  |     auto iso_year = TRY(to_integer_throw_on_infinity(global_object, vm.argument(0), ErrorType::TemporalInvalidPlainDateTime)); | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-17 20:17:12 +01:00
										 |  |  |  |     // 3. Let isoMonth be ? ToIntegerThrowOnInfinity(isoMonth).
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |  |     auto iso_month = TRY(to_integer_throw_on_infinity(global_object, vm.argument(1), ErrorType::TemporalInvalidPlainDateTime)); | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-17 20:17:12 +01:00
										 |  |  |  |     // 4. Let isoDay be ? ToIntegerThrowOnInfinity(isoDay).
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |  |     auto iso_day = TRY(to_integer_throw_on_infinity(global_object, vm.argument(2), ErrorType::TemporalInvalidPlainDateTime)); | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-17 20:17:12 +01:00
										 |  |  |  |     // 5. Let hour be ? ToIntegerThrowOnInfinity(hour).
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |  |     auto hour = TRY(to_integer_throw_on_infinity(global_object, vm.argument(3), ErrorType::TemporalInvalidPlainDateTime)); | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-17 20:17:12 +01:00
										 |  |  |  |     // 6. Let minute be ? ToIntegerThrowOnInfinity(minute).
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |  |     auto minute = TRY(to_integer_throw_on_infinity(global_object, vm.argument(4), ErrorType::TemporalInvalidPlainDateTime)); | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-17 20:17:12 +01:00
										 |  |  |  |     // 7. Let second be ? ToIntegerThrowOnInfinity(second).
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |  |     auto second = TRY(to_integer_throw_on_infinity(global_object, vm.argument(5), ErrorType::TemporalInvalidPlainDateTime)); | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-17 20:17:12 +01:00
										 |  |  |  |     // 8. Let millisecond be ? ToIntegerThrowOnInfinity(millisecond).
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |  |     auto millisecond = TRY(to_integer_throw_on_infinity(global_object, vm.argument(6), ErrorType::TemporalInvalidPlainDateTime)); | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-17 20:17:12 +01:00
										 |  |  |  |     // 9. Let microsecond be ? ToIntegerThrowOnInfinity(microsecond).
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |  |     auto microsecond = TRY(to_integer_throw_on_infinity(global_object, vm.argument(7), ErrorType::TemporalInvalidPlainDateTime)); | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-17 20:17:12 +01:00
										 |  |  |  |     // 10. Let nanosecond be ? ToIntegerThrowOnInfinity(nanosecond).
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |  |     auto nanosecond = TRY(to_integer_throw_on_infinity(global_object, vm.argument(8), ErrorType::TemporalInvalidPlainDateTime)); | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-17 20:17:12 +01:00
										 |  |  |  |     // 11. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |  |     auto* calendar = TRY(to_temporal_calendar_with_iso_default(global_object, vm.argument(9))); | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards.
 | 
					
						
							| 
									
										
										
										
											2021-09-07 12:56:50 +02:00
										 |  |  |  |     // This does not change the exposed behavior as the call to CreateTemporalDateTime will immediately check that these values are valid
 | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  |     // ISO values (for years: -273975 - 273975, for months: 1 - 12, for days: 1 - 31, for hours: 0 - 23, for minutes and seconds: 0 - 59,
 | 
					
						
							|  |  |  |  |     // milliseconds, microseconds, and nanoseconds: 0 - 999) all of which are subsets of this check.
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |  |     if (!AK::is_within_range<i32>(iso_year) || !AK::is_within_range<u8>(iso_month) || !AK::is_within_range<u8>(iso_day) || !AK::is_within_range<u8>(hour) || !AK::is_within_range<u8>(minute) || !AK::is_within_range<u8>(second) || !AK::is_within_range<u16>(millisecond) || !AK::is_within_range<u16>(microsecond) || !AK::is_within_range<u16>(nanosecond)) | 
					
						
							|  |  |  |  |         return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainDateTime); | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-17 20:17:12 +01:00
										 |  |  |  |     // 12. Return ? CreateTemporalDateTime(isoYear, isoMonth, isoDay, hour, minute, second, millisecond, microsecond, nanosecond, calendar, NewTarget).
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |  |     return TRY(create_temporal_date_time(global_object, iso_year, iso_month, iso_day, hour, minute, second, millisecond, microsecond, nanosecond, *calendar, &new_target)); | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-22 23:16:42 +01:00
										 |  |  |  | // 5.2.2 Temporal.PlainDateTime.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.from
 | 
					
						
							| 
									
										
										
										
											2021-10-21 20:09:59 +01:00
										 |  |  |  | JS_DEFINE_NATIVE_FUNCTION(PlainDateTimeConstructor::from) | 
					
						
							| 
									
										
										
										
											2021-08-22 23:16:42 +01:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     auto item = vm.argument(0); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 1. Set options to ? GetOptionsObject(options).
 | 
					
						
							| 
									
										
										
										
											2021-10-21 20:09:59 +01:00
										 |  |  |  |     auto* options = TRY(get_options_object(global_object, vm.argument(1))); | 
					
						
							| 
									
										
										
										
											2021-08-22 23:16:42 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. If Type(item) is Object and item has an [[InitializedTemporalDateTime]] internal slot, then
 | 
					
						
							|  |  |  |  |     if (item.is_object() && is<PlainDateTime>(item.as_object())) { | 
					
						
							|  |  |  |  |         auto& plain_date_time = static_cast<PlainDateTime&>(item.as_object()); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // a. Perform ? ToTemporalOverflow(options).
 | 
					
						
							| 
									
										
										
										
											2021-10-21 20:09:59 +01:00
										 |  |  |  |         (void)TRY(to_temporal_overflow(global_object, *options)); | 
					
						
							| 
									
										
										
										
											2021-08-22 23:16:42 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         // b. Return ? CreateTemporalDateTime(item.[[ISOYear]], item.[[ISOMonth]], item.[[ISODay]], item.[[ISOHour]], item.[[ISOMinute]], item.[[ISOSecond]], item.[[ISOMillisecond]], item.[[ISOMicrosecond]], item.[[ISONanosecond]], item.[[Calendar]]).
 | 
					
						
							| 
									
										
										
										
											2021-10-21 20:09:59 +01:00
										 |  |  |  |         return TRY(create_temporal_date_time(global_object, plain_date_time.iso_year(), plain_date_time.iso_month(), plain_date_time.iso_day(), plain_date_time.iso_hour(), plain_date_time.iso_minute(), plain_date_time.iso_second(), plain_date_time.iso_millisecond(), plain_date_time.iso_microsecond(), plain_date_time.iso_nanosecond(), plain_date_time.calendar())); | 
					
						
							| 
									
										
										
										
											2021-08-22 23:16:42 +01:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. Return ? ToTemporalDateTime(item, options).
 | 
					
						
							| 
									
										
										
										
											2021-10-21 20:09:59 +01:00
										 |  |  |  |     return TRY(to_temporal_date_time(global_object, item, options)); | 
					
						
							| 
									
										
										
										
											2021-08-22 23:16:42 +01:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 16:42:21 +03:00
										 |  |  |  | // 5.2.3 Temporal.PlainDateTime.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.compare
 | 
					
						
							| 
									
										
										
										
											2021-10-21 20:09:59 +01:00
										 |  |  |  | JS_DEFINE_NATIVE_FUNCTION(PlainDateTimeConstructor::compare) | 
					
						
							| 
									
										
										
										
											2021-08-27 16:42:21 +03:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // 1. Set one to ? ToTemporalDateTime(one).
 | 
					
						
							| 
									
										
										
										
											2021-10-21 20:09:59 +01:00
										 |  |  |  |     auto* one = TRY(to_temporal_date_time(global_object, vm.argument(0))); | 
					
						
							| 
									
										
										
										
											2021-08-27 16:42:21 +03:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. Set two to ? ToTemporalDateTime(two).
 | 
					
						
							| 
									
										
										
										
											2021-10-21 20:09:59 +01:00
										 |  |  |  |     auto two = TRY(to_temporal_date_time(global_object, vm.argument(1))); | 
					
						
							| 
									
										
										
										
											2021-08-27 16:42:21 +03:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. Return 𝔽(! CompareISODateTime(one.[[ISOYear]], one.[[ISOMonth]], one.[[ISODay]], one.[[ISOHour]], one.[[ISOMinute]], one.[[ISOSecond]], one.[[ISOMillisecond]], one.[[ISOMicrosecond]], one.[[ISONanosecond]], two.[[ISOYear]], two.[[ISOMonth]], two.[[ISODay]], two.[[ISOHour]], two.[[ISOMinute]], two.[[ISOSecond]], two.[[ISOMillisecond]], two.[[ISOMicrosecond]], two.[[ISONanosecond]])).
 | 
					
						
							|  |  |  |  |     return Value(compare_iso_date_time(one->iso_year(), one->iso_month(), one->iso_day(), one->iso_hour(), one->iso_minute(), one->iso_second(), one->iso_millisecond(), one->iso_microsecond(), one->iso_nanosecond(), two->iso_year(), two->iso_month(), two->iso_day(), two->iso_hour(), two->iso_minute(), two->iso_second(), two->iso_millisecond(), two->iso_microsecond(), two->iso_nanosecond())); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-22 19:47:07 +01:00
										 |  |  |  | } |