| 
									
										
										
										
											2021-07-11 21:04:11 +03:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-07-18 21:44:05 +01:00
										 |  |  |  * Copyright (c) 2021, Linus Groh <linusg@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-07-11 21:04:11 +03:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/Forward.h>
 | 
					
						
							|  |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-31 00:15:36 +01:00
										 |  |  | #include <AK/Variant.h>
 | 
					
						
							| 
									
										
										
										
											2021-07-18 21:44:05 +01:00
										 |  |  | #include <LibJS/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-15 23:03:38 +01:00
										 |  |  | #include <LibJS/Runtime/Completion.h>
 | 
					
						
							| 
									
										
										
										
											2021-07-11 21:04:11 +03:00
										 |  |  | #include <LibJS/Runtime/GlobalObject.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS::Temporal { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-11 22:28:49 +03:00
										 |  |  | enum class OptionType { | 
					
						
							|  |  |  |     Boolean, | 
					
						
							|  |  |  |     String, | 
					
						
							|  |  |  |     Number | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2021-07-18 21:41:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-11 21:04:11 +03:00
										 |  |  | struct ISODateTime { | 
					
						
							|  |  |  |     i32 year; | 
					
						
							| 
									
										
										
										
											2021-07-25 20:45:07 +01:00
										 |  |  |     u8 month; | 
					
						
							|  |  |  |     u8 day; | 
					
						
							|  |  |  |     u8 hour; | 
					
						
							|  |  |  |     u8 minute; | 
					
						
							|  |  |  |     u8 second; | 
					
						
							|  |  |  |     u16 millisecond; | 
					
						
							|  |  |  |     u16 microsecond; | 
					
						
							|  |  |  |     u16 nanosecond; | 
					
						
							|  |  |  |     Optional<String> calendar = {}; | 
					
						
							| 
									
										
										
										
											2021-07-11 21:04:11 +03:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2021-07-18 21:41:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-11 21:04:11 +03:00
										 |  |  | struct TemporalInstant { | 
					
						
							|  |  |  |     i32 year; | 
					
						
							| 
									
										
										
										
											2021-07-26 16:48:47 +03:00
										 |  |  |     u8 month; | 
					
						
							|  |  |  |     u8 day; | 
					
						
							|  |  |  |     u8 hour; | 
					
						
							|  |  |  |     u8 minute; | 
					
						
							|  |  |  |     u8 second; | 
					
						
							|  |  |  |     u16 millisecond; | 
					
						
							|  |  |  |     u16 microsecond; | 
					
						
							|  |  |  |     u16 nanosecond; | 
					
						
							| 
									
										
										
										
											2021-07-11 21:04:11 +03:00
										 |  |  |     Optional<String> time_zone_offset; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2021-07-18 21:41:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 22:17:40 +03:00
										 |  |  | struct TemporalDate { | 
					
						
							|  |  |  |     i32 year; | 
					
						
							| 
									
										
										
										
											2021-07-26 16:48:47 +03:00
										 |  |  |     u8 month; | 
					
						
							|  |  |  |     u8 day; | 
					
						
							| 
									
										
										
										
											2021-07-21 22:17:40 +03:00
										 |  |  |     Optional<String> calendar; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 18:02:37 +03:00
										 |  |  | struct TemporalTime { | 
					
						
							|  |  |  |     u8 hour; | 
					
						
							|  |  |  |     u8 minute; | 
					
						
							|  |  |  |     u8 second; | 
					
						
							|  |  |  |     u16 millisecond; | 
					
						
							|  |  |  |     u16 microsecond; | 
					
						
							|  |  |  |     u16 nanosecond; | 
					
						
							|  |  |  |     Optional<String> calendar = {}; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-11 21:04:11 +03:00
										 |  |  | struct TemporalTimeZone { | 
					
						
							|  |  |  |     bool z; | 
					
						
							|  |  |  |     Optional<String> offset; | 
					
						
							|  |  |  |     Optional<String> name; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2021-07-18 21:41:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 06:27:55 +01:00
										 |  |  | struct TemporalYearMonth { | 
					
						
							|  |  |  |     i32 year; | 
					
						
							|  |  |  |     u8 month; | 
					
						
							|  |  |  |     u8 day; | 
					
						
							|  |  |  |     Optional<String> calendar = {}; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-31 00:15:36 +01:00
										 |  |  | struct SecondsStringPrecision { | 
					
						
							| 
									
										
										
										
											2021-09-08 21:29:20 +01:00
										 |  |  |     Variant<StringView, u8> precision; | 
					
						
							| 
									
										
										
										
											2021-08-31 00:15:36 +01:00
										 |  |  |     String unit; | 
					
						
							|  |  |  |     u32 increment; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-15 23:11:02 +01:00
										 |  |  | ThrowCompletionOr<MarkedValueList> iterable_to_list_of_type(GlobalObject&, Value items, Vector<OptionType> const& element_types); | 
					
						
							| 
									
										
										
										
											2021-09-15 23:17:20 +01:00
										 |  |  | ThrowCompletionOr<Object*> get_options_object(GlobalObject&, Value options); | 
					
						
							| 
									
										
										
										
											2021-10-24 16:01:24 +02:00
										 |  |  | ThrowCompletionOr<Value> get_option(GlobalObject&, Object const& options, PropertyKey const& property, Vector<OptionType> const& types, Vector<StringView> const& values, Value fallback); | 
					
						
							| 
									
										
										
										
											2021-08-31 00:15:36 +01:00
										 |  |  | template<typename NumberType> | 
					
						
							| 
									
										
										
										
											2021-10-24 16:01:24 +02:00
										 |  |  | ThrowCompletionOr<Variant<String, NumberType>> get_string_or_number_option(GlobalObject&, Object const& options, PropertyKey const& property, Vector<StringView> const& string_values, NumberType minimum, NumberType maximum, Value fallback); | 
					
						
							| 
									
										
										
										
											2021-09-15 23:39:42 +01:00
										 |  |  | ThrowCompletionOr<String> to_temporal_overflow(GlobalObject&, Object const& normalized_options); | 
					
						
							| 
									
										
										
										
											2021-11-01 14:20:06 +01:00
										 |  |  | ThrowCompletionOr<String> to_temporal_disambiguation(GlobalObject&, Object const& normalized_options); | 
					
						
							| 
									
										
										
										
											2021-09-15 23:43:02 +01:00
										 |  |  | ThrowCompletionOr<String> to_temporal_rounding_mode(GlobalObject&, Object const& normalized_options, String const& fallback); | 
					
						
							| 
									
										
										
										
											2021-09-15 23:45:02 +01:00
										 |  |  | ThrowCompletionOr<String> to_show_calendar_option(GlobalObject&, Object const& normalized_options); | 
					
						
							| 
									
										
										
										
											2021-09-15 23:50:43 +01:00
										 |  |  | ThrowCompletionOr<u64> to_temporal_rounding_increment(GlobalObject&, Object const& normalized_options, Optional<double> dividend, bool inclusive); | 
					
						
							| 
									
										
										
										
											2021-09-15 23:53:35 +01:00
										 |  |  | ThrowCompletionOr<SecondsStringPrecision> to_seconds_string_precision(GlobalObject&, Object const& normalized_options); | 
					
						
							| 
									
										
										
										
											2021-09-15 23:57:06 +01:00
										 |  |  | ThrowCompletionOr<String> to_largest_temporal_unit(GlobalObject&, Object const& normalized_options, Vector<StringView> const& disallowed_units, String const& fallback, Optional<String> auto_value); | 
					
						
							| 
									
										
										
										
											2021-09-16 00:05:01 +01:00
										 |  |  | ThrowCompletionOr<Optional<String>> to_smallest_temporal_unit(GlobalObject&, Object const& normalized_options, Vector<StringView> const& disallowed_units, Optional<String> fallback); | 
					
						
							| 
									
										
										
										
											2021-09-16 14:00:39 +01:00
										 |  |  | ThrowCompletionOr<void> validate_temporal_unit_range(GlobalObject&, StringView largest_unit, StringView smallest_unit); | 
					
						
							| 
									
										
										
										
											2021-09-06 21:21:57 +03:00
										 |  |  | String larger_of_two_temporal_units(StringView, StringView); | 
					
						
							|  |  |  | Optional<u16> maximum_temporal_duration_rounding_increment(StringView unit); | 
					
						
							| 
									
										
										
										
											2021-09-16 14:12:49 +01:00
										 |  |  | ThrowCompletionOr<void> reject_temporal_calendar_type(GlobalObject&, Object&); | 
					
						
							| 
									
										
										
										
											2021-09-08 21:29:20 +01:00
										 |  |  | String format_seconds_string_part(u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, Variant<StringView, u8> const& precision); | 
					
						
							| 
									
										
										
										
											2021-07-21 22:17:40 +03:00
										 |  |  | double constrain_to_range(double x, double minimum, double maximum); | 
					
						
							| 
									
										
										
										
											2021-10-06 22:25:28 +01:00
										 |  |  | i64 round_number_to_increment(double, u64 increment, StringView rounding_mode); | 
					
						
							| 
									
										
										
										
											2021-09-06 22:46:55 +03:00
										 |  |  | BigInt* round_number_to_increment(GlobalObject&, BigInt const&, u64 increment, StringView rounding_mode); | 
					
						
							| 
									
										
										
										
											2021-09-16 14:21:36 +01:00
										 |  |  | ThrowCompletionOr<ISODateTime> parse_iso_date_time(GlobalObject&, String const& iso_string); | 
					
						
							| 
									
										
										
										
											2021-09-16 17:48:04 +01:00
										 |  |  | ThrowCompletionOr<TemporalInstant> parse_temporal_instant_string(GlobalObject&, String const& iso_string); | 
					
						
							| 
									
										
										
										
											2021-09-16 17:50:23 +01:00
										 |  |  | ThrowCompletionOr<String> parse_temporal_calendar_string(GlobalObject&, String const& iso_string); | 
					
						
							| 
									
										
										
										
											2021-09-16 17:51:37 +01:00
										 |  |  | ThrowCompletionOr<TemporalDate> parse_temporal_date_string(GlobalObject&, String const& iso_string); | 
					
						
							| 
									
										
										
										
											2021-09-16 17:52:57 +01:00
										 |  |  | ThrowCompletionOr<ISODateTime> parse_temporal_date_time_string(GlobalObject&, String const& iso_string); | 
					
						
							| 
									
										
										
										
											2021-09-16 17:53:53 +01:00
										 |  |  | ThrowCompletionOr<TemporalDuration> parse_temporal_duration_string(GlobalObject&, String const& iso_string); | 
					
						
							| 
									
										
										
										
											2021-09-16 17:54:42 +01:00
										 |  |  | ThrowCompletionOr<TemporalTime> parse_temporal_time_string(GlobalObject&, String const& iso_string); | 
					
						
							| 
									
										
										
										
											2021-09-16 17:58:24 +01:00
										 |  |  | ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject&, String const& iso_string); | 
					
						
							| 
									
										
										
										
											2021-09-16 17:59:54 +01:00
										 |  |  | ThrowCompletionOr<TemporalYearMonth> parse_temporal_year_month_string(GlobalObject&, String const& iso_string); | 
					
						
							| 
									
										
										
										
											2021-09-16 18:02:55 +01:00
										 |  |  | ThrowCompletionOr<double> to_positive_integer(GlobalObject&, Value argument); | 
					
						
							| 
									
										
										
										
											2021-09-16 18:09:52 +01:00
										 |  |  | ThrowCompletionOr<Object*> prepare_temporal_fields(GlobalObject&, Object const& fields, Vector<String> const& field_names, Vector<StringView> const& required_fields); | 
					
						
							| 
									
										
										
										
											2021-07-11 21:04:11 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-17 20:17:12 +01:00
										 |  |  | // 13.46 ToIntegerThrowOnInfinity ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-tointegerthrowoninfinity
 | 
					
						
							|  |  |  | template<typename... Args> | 
					
						
							| 
									
										
										
										
											2021-09-15 23:03:38 +01:00
										 |  |  | ThrowCompletionOr<double> to_integer_throw_on_infinity(GlobalObject& global_object, Value argument, ErrorType error_type, Args... args) | 
					
						
							| 
									
										
										
										
											2021-08-17 20:17:12 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto& vm = global_object.vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 1. Let integer be ? ToIntegerOrInfinity(argument).
 | 
					
						
							| 
									
										
										
										
											2021-10-18 19:19:57 +01:00
										 |  |  |     auto integer = TRY(argument.to_integer_or_infinity(global_object)); | 
					
						
							| 
									
										
										
										
											2021-08-17 20:17:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. If integer is −∞ or +∞ , then
 | 
					
						
							|  |  |  |     if (Value(integer).is_infinity()) { | 
					
						
							|  |  |  |         // a. Throw a RangeError exception.
 | 
					
						
							| 
									
										
										
										
											2021-09-15 23:03:38 +01:00
										 |  |  |         return vm.template throw_completion<RangeError>(global_object, error_type, args...); | 
					
						
							| 
									
										
										
										
											2021-08-17 20:17:12 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 3. Return integer.
 | 
					
						
							|  |  |  |     return integer; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-11 21:04:11 +03:00
										 |  |  | } |