2021-07-11 21:04:11 +03:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
2022-01-22 21:13:55 +00:00
|
|
|
|
* Copyright (c) 2021-2022, 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>
|
2021-11-19 18:04:50 +00:00
|
|
|
|
#include <LibJS/Runtime/Temporal/ISO8601.h>
|
2021-07-11 21:04:11 +03:00
|
|
|
|
|
|
|
|
|
|
namespace JS::Temporal {
|
|
|
|
|
|
|
2022-05-06 19:01:15 +02:00
|
|
|
|
enum class ArithmeticOperation {
|
|
|
|
|
|
Add,
|
|
|
|
|
|
Subtract,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-05-07 13:32:19 +02:00
|
|
|
|
enum class DifferenceOperation {
|
|
|
|
|
|
Since,
|
|
|
|
|
|
Until,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-05-24 18:26:13 +01:00
|
|
|
|
enum class UnsignedRoundingMode {
|
|
|
|
|
|
HalfEven,
|
|
|
|
|
|
HalfInfinity,
|
|
|
|
|
|
HalfZero,
|
|
|
|
|
|
Infinity,
|
|
|
|
|
|
Zero,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2021-07-11 22:28:49 +03:00
|
|
|
|
enum class OptionType {
|
|
|
|
|
|
Boolean,
|
|
|
|
|
|
String,
|
|
|
|
|
|
Number
|
|
|
|
|
|
};
|
2021-07-18 21:41:29 +01:00
|
|
|
|
|
2022-06-09 22:42:42 +01:00
|
|
|
|
enum class UnitGroup {
|
|
|
|
|
|
Date,
|
|
|
|
|
|
Time,
|
|
|
|
|
|
DateTime,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
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;
|
2022-01-22 21:13:55 +00:00
|
|
|
|
Optional<String> offset_string;
|
2021-07-11 21:04:11 +03:00
|
|
|
|
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-11-10 22:28:27 +00:00
|
|
|
|
struct TemporalMonthDay {
|
|
|
|
|
|
Optional<i32> year;
|
|
|
|
|
|
u8 month;
|
|
|
|
|
|
u8 day;
|
|
|
|
|
|
Optional<String> calendar = {};
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2021-11-07 00:51:19 +00:00
|
|
|
|
struct TemporalZonedDateTime {
|
|
|
|
|
|
ISODateTime date_time;
|
|
|
|
|
|
TemporalTimeZone time_zone;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-06-09 22:42:42 +01:00
|
|
|
|
struct TemporalUnitRequired { };
|
2022-06-14 20:22:52 +01:00
|
|
|
|
struct PrepareTemporalFieldsPartial { };
|
2022-06-09 22:42:42 +01:00
|
|
|
|
|
2022-02-09 10:06:40 +00:00
|
|
|
|
ThrowCompletionOr<MarkedVector<Value>> 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);
|
2022-06-14 22:17:11 +01:00
|
|
|
|
ThrowCompletionOr<Value> get_option(GlobalObject&, Object const& options, PropertyKey const& property, OptionType type, Vector<StringView> const& values, Value fallback);
|
2022-04-06 23:56:57 +01:00
|
|
|
|
ThrowCompletionOr<String> to_temporal_overflow(GlobalObject&, Object const* options);
|
|
|
|
|
|
ThrowCompletionOr<String> to_temporal_disambiguation(GlobalObject&, Object const* options);
|
2021-09-15 23:43:02 +01:00
|
|
|
|
ThrowCompletionOr<String> to_temporal_rounding_mode(GlobalObject&, Object const& normalized_options, String const& fallback);
|
2021-11-15 21:05:45 +00:00
|
|
|
|
StringView negate_temporal_rounding_mode(String const& rounding_mode);
|
2022-04-06 23:56:57 +01:00
|
|
|
|
ThrowCompletionOr<String> to_temporal_offset(GlobalObject&, Object const* options, String const& fallback);
|
2021-09-15 23:45:02 +01:00
|
|
|
|
ThrowCompletionOr<String> to_show_calendar_option(GlobalObject&, Object const& normalized_options);
|
2021-11-10 00:14:16 +00:00
|
|
|
|
ThrowCompletionOr<String> to_show_time_zone_name_option(GlobalObject&, Object const& normalized_options);
|
|
|
|
|
|
ThrowCompletionOr<String> to_show_offset_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-11-03 18:22:54 +00:00
|
|
|
|
ThrowCompletionOr<u64> to_temporal_date_time_rounding_increment(GlobalObject&, Object const& normalized_options, StringView smallest_unit);
|
2021-09-15 23:53:35 +01:00
|
|
|
|
ThrowCompletionOr<SecondsStringPrecision> to_seconds_string_precision(GlobalObject&, Object const& normalized_options);
|
2022-06-09 22:42:42 +01:00
|
|
|
|
ThrowCompletionOr<Optional<String>> get_temporal_unit(GlobalObject&, Object const& normalized_options, PropertyKey const&, UnitGroup, Variant<TemporalUnitRequired, Optional<StringView>> const& default_, Vector<StringView> const& extra_values = {});
|
2021-11-09 17:19:28 +00:00
|
|
|
|
ThrowCompletionOr<Value> to_relative_temporal_object(GlobalObject&, Object const& options);
|
2021-11-29 18:59:07 -05:00
|
|
|
|
StringView larger_of_two_temporal_units(StringView, StringView);
|
2022-04-07 23:54:17 +01:00
|
|
|
|
ThrowCompletionOr<Object*> merge_largest_unit_option(GlobalObject&, Object const* options, String largest_unit);
|
2021-09-06 21:21:57 +03:00
|
|
|
|
Optional<u16> maximum_temporal_duration_rounding_increment(StringView unit);
|
2021-11-04 16:16:35 +01:00
|
|
|
|
ThrowCompletionOr<void> reject_object_with_calendar_or_time_zone(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-11-07 01:41:52 +00:00
|
|
|
|
double sign(double);
|
2021-11-23 22:30:27 +00:00
|
|
|
|
double sign(Crypto::SignedBigInteger const&);
|
2022-05-24 18:26:13 +01:00
|
|
|
|
UnsignedRoundingMode get_unsigned_rounding_mode(StringView rounding_mode, bool is_negative);
|
|
|
|
|
|
double apply_unsigned_rounding_mode(double x, double r1, double r2, Optional<UnsignedRoundingMode> const&);
|
|
|
|
|
|
Crypto::SignedBigInteger apply_unsigned_rounding_mode(Crypto::SignedDivisionResult const&, Crypto::SignedBigInteger const& r1, Crypto::SignedBigInteger const& r2, Optional<UnsignedRoundingMode> const&, Crypto::UnsignedBigInteger const& increment);
|
|
|
|
|
|
double round_number_to_increment(double, u64 increment, StringView rounding_mode);
|
|
|
|
|
|
Crypto::SignedBigInteger round_number_to_increment(Crypto::SignedBigInteger const&, u64 increment, StringView rounding_mode);
|
2021-11-19 18:04:50 +00:00
|
|
|
|
ThrowCompletionOr<ISODateTime> parse_iso_date_time(GlobalObject&, ParseResult const& parse_result);
|
2021-09-16 17:48:04 +01:00
|
|
|
|
ThrowCompletionOr<TemporalInstant> parse_temporal_instant_string(GlobalObject&, String const& iso_string);
|
2021-11-07 00:51:19 +00:00
|
|
|
|
ThrowCompletionOr<TemporalZonedDateTime> parse_temporal_zoned_date_time_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);
|
2022-03-09 22:59:17 +01:00
|
|
|
|
ThrowCompletionOr<DurationRecord> parse_temporal_duration_string(GlobalObject&, String const& iso_string);
|
2021-11-10 22:28:27 +00:00
|
|
|
|
ThrowCompletionOr<TemporalMonthDay> parse_temporal_month_day_string(GlobalObject&, String const& iso_string);
|
2021-11-09 17:19:28 +00:00
|
|
|
|
ThrowCompletionOr<TemporalZonedDateTime> parse_temporal_relative_to_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);
|
2022-06-14 20:22:52 +01:00
|
|
|
|
ThrowCompletionOr<Object*> prepare_temporal_fields(GlobalObject&, Object const& fields, Vector<String> const& field_names, Variant<PrepareTemporalFieldsPartial, Vector<StringView>> const& required_fields);
|
2021-07-11 21:04:11 +03:00
|
|
|
|
|
2022-06-14 22:03:18 +01:00
|
|
|
|
// 13.40 ToIntegerThrowOnInfinity ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-tointegerthrowoninfinity
|
2021-08-17 20:17:12 +01:00
|
|
|
|
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
|
|
|
|
|
2022-04-29 21:09:10 +02:00
|
|
|
|
// 2. If integer is -∞ or +∞ , then
|
2021-08-17 20:17:12 +01:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-06-14 22:03:18 +01:00
|
|
|
|
// 13.41 ToIntegerWithoutRounding ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-tointegerwithoutrounding
|
2021-11-17 22:20:59 +00:00
|
|
|
|
template<typename... Args>
|
|
|
|
|
|
ThrowCompletionOr<double> to_integer_without_rounding(GlobalObject& global_object, Value argument, ErrorType error_type, Args... args)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& vm = global_object.vm();
|
|
|
|
|
|
|
|
|
|
|
|
// 1. Let number be ? ToNumber(argument).
|
|
|
|
|
|
auto number = TRY(argument.to_number(global_object));
|
|
|
|
|
|
|
2022-04-29 21:09:10 +02:00
|
|
|
|
// 2. If number is NaN, +0𝔽, or -0𝔽 return 0.
|
2021-11-17 22:20:59 +00:00
|
|
|
|
if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero())
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
2022-03-10 18:05:50 +01:00
|
|
|
|
// 3. If IsIntegralNumber(number) is false, throw a RangeError exception.
|
2021-11-17 22:20:59 +00:00
|
|
|
|
if (!number.is_integral_number())
|
|
|
|
|
|
return vm.template throw_completion<RangeError>(global_object, error_type, args...);
|
|
|
|
|
|
|
|
|
|
|
|
// 4. Return ℝ(number).
|
|
|
|
|
|
return number.as_double();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-11 21:04:11 +03:00
|
|
|
|
}
|