2021-07-11 21:04:11 +03:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
2023-01-26 12:07:38 +00:00
|
|
|
|
* Copyright (c) 2021-2023, 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>
|
2021-08-31 00:15:36 +01:00
|
|
|
|
#include <AK/Variant.h>
|
2023-01-28 11:26:03 -05:00
|
|
|
|
#include <LibCrypto/BigInt/SignedBigInteger.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 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;
|
2023-01-26 15:00:53 +00:00
|
|
|
|
Optional<String> time_zone_offset;
|
2021-07-11 21:04:11 +03:00
|
|
|
|
};
|
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;
|
2023-01-26 15:00:53 +00:00
|
|
|
|
Optional<String> calendar;
|
2021-07-21 22:17:40 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
2021-08-27 18:02:37 +03:00
|
|
|
|
struct TemporalTime {
|
|
|
|
|
|
u8 hour;
|
|
|
|
|
|
u8 minute;
|
|
|
|
|
|
u8 second;
|
|
|
|
|
|
u16 millisecond;
|
|
|
|
|
|
u16 microsecond;
|
|
|
|
|
|
u16 nanosecond;
|
2023-01-26 15:00:53 +00:00
|
|
|
|
Optional<String> calendar = {};
|
2021-08-27 18:02:37 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
2021-07-11 21:04:11 +03:00
|
|
|
|
struct TemporalTimeZone {
|
|
|
|
|
|
bool z;
|
2023-01-26 15:00:53 +00:00
|
|
|
|
Optional<String> offset_string;
|
|
|
|
|
|
Optional<String> name;
|
2021-07-11 21:04:11 +03:00
|
|
|
|
};
|
2021-07-18 21:41:29 +01:00
|
|
|
|
|
2021-09-09 06:27:55 +01:00
|
|
|
|
struct TemporalYearMonth {
|
|
|
|
|
|
i32 year;
|
|
|
|
|
|
u8 month;
|
|
|
|
|
|
u8 day;
|
2023-01-26 15:00:53 +00:00
|
|
|
|
Optional<String> calendar = {};
|
2021-09-09 06:27:55 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
2021-11-10 22:28:27 +00:00
|
|
|
|
struct TemporalMonthDay {
|
|
|
|
|
|
Optional<i32> year;
|
|
|
|
|
|
u8 month;
|
|
|
|
|
|
u8 day;
|
2023-01-26 15:00:53 +00:00
|
|
|
|
Optional<String> calendar = {};
|
2021-11-10 22:28:27 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
2022-10-15 03:32:18 +05:30
|
|
|
|
struct ISODateTime {
|
|
|
|
|
|
i32 year;
|
|
|
|
|
|
u8 month;
|
|
|
|
|
|
u8 day;
|
|
|
|
|
|
u8 hour;
|
|
|
|
|
|
u8 minute;
|
|
|
|
|
|
u8 second;
|
|
|
|
|
|
u16 millisecond;
|
|
|
|
|
|
u16 microsecond;
|
|
|
|
|
|
u16 nanosecond;
|
|
|
|
|
|
TemporalTimeZone time_zone { .z = false, .offset_string = {}, .name = {} };
|
2023-01-26 15:00:53 +00:00
|
|
|
|
Optional<String> calendar = {};
|
2021-11-07 00:51:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
2021-08-31 00:15:36 +01:00
|
|
|
|
struct SecondsStringPrecision {
|
2021-09-08 21:29:20 +01:00
|
|
|
|
Variant<StringView, u8> precision;
|
2023-01-26 15:03:27 +00:00
|
|
|
|
StringView unit;
|
2021-08-31 00:15:36 +01:00
|
|
|
|
u32 increment;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-06-24 00:27:29 +01:00
|
|
|
|
struct DifferenceSettings {
|
2023-01-26 14:43:42 +00:00
|
|
|
|
String smallest_unit;
|
|
|
|
|
|
String largest_unit;
|
|
|
|
|
|
String rounding_mode;
|
2022-06-24 00:27:29 +01:00
|
|
|
|
u64 rounding_increment;
|
2023-02-26 16:09:02 -07:00
|
|
|
|
NonnullGCPtr<Object> options;
|
2022-06-24 00:27:29 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
2022-06-09 22:42:42 +01:00
|
|
|
|
struct TemporalUnitRequired { };
|
2022-06-14 20:22:52 +01:00
|
|
|
|
struct PrepareTemporalFieldsPartial { };
|
2022-06-14 23:03:25 +01:00
|
|
|
|
struct GetOptionRequired { };
|
|
|
|
|
|
|
|
|
|
|
|
using OptionDefault = Variant<GetOptionRequired, Empty, bool, StringView, double>;
|
2022-06-24 00:27:29 +01:00
|
|
|
|
using TemporalUnitDefault = Variant<TemporalUnitRequired, Optional<StringView>>;
|
2022-06-09 22:42:42 +01:00
|
|
|
|
|
2022-08-20 08:52:42 +01:00
|
|
|
|
ThrowCompletionOr<MarkedVector<Value>> iterable_to_list_of_type(VM&, Value items, Vector<OptionType> const& element_types);
|
|
|
|
|
|
ThrowCompletionOr<Object*> get_options_object(VM&, Value options);
|
2023-02-05 19:02:54 +00:00
|
|
|
|
ThrowCompletionOr<Value> get_option(VM&, Object const& options, PropertyKey const& property, OptionType type, ReadonlySpan<StringView> values, OptionDefault const&);
|
2023-01-26 12:07:38 +00:00
|
|
|
|
ThrowCompletionOr<String> to_temporal_overflow(VM&, Object const* options);
|
2023-01-26 12:09:01 +00:00
|
|
|
|
ThrowCompletionOr<String> to_temporal_disambiguation(VM&, Object const* options);
|
2023-01-26 12:09:47 +00:00
|
|
|
|
ThrowCompletionOr<String> to_temporal_rounding_mode(VM&, Object const& normalized_options, StringView fallback);
|
|
|
|
|
|
StringView negate_temporal_rounding_mode(StringView rounding_mode);
|
2023-01-26 12:17:54 +00:00
|
|
|
|
ThrowCompletionOr<String> to_temporal_offset(VM&, Object const* options, StringView fallback);
|
2023-01-26 12:19:23 +00:00
|
|
|
|
ThrowCompletionOr<String> to_calendar_name_option(VM&, Object const& normalized_options);
|
2023-01-26 12:19:55 +00:00
|
|
|
|
ThrowCompletionOr<String> to_time_zone_name_option(VM&, Object const& normalized_options);
|
2023-01-26 12:20:18 +00:00
|
|
|
|
ThrowCompletionOr<String> to_show_offset_option(VM&, Object const& normalized_options);
|
2022-08-20 08:52:42 +01:00
|
|
|
|
ThrowCompletionOr<u64> to_temporal_rounding_increment(VM&, Object const& normalized_options, Optional<double> dividend, bool inclusive);
|
|
|
|
|
|
ThrowCompletionOr<u64> to_temporal_date_time_rounding_increment(VM&, Object const& normalized_options, StringView smallest_unit);
|
|
|
|
|
|
ThrowCompletionOr<SecondsStringPrecision> to_seconds_string_precision(VM&, Object const& normalized_options);
|
2023-01-26 14:03:01 +00:00
|
|
|
|
ThrowCompletionOr<Optional<String>> get_temporal_unit(VM&, Object const& normalized_options, PropertyKey const&, UnitGroup, TemporalUnitDefault const& default_, Vector<StringView> const& extra_values = {});
|
2022-08-20 08:52:42 +01:00
|
|
|
|
ThrowCompletionOr<Value> to_relative_temporal_object(VM&, Object const& options);
|
2021-11-29 18:59:07 -05:00
|
|
|
|
StringView larger_of_two_temporal_units(StringView, StringView);
|
2023-01-26 14:07:33 +00:00
|
|
|
|
ThrowCompletionOr<Object*> merge_largest_unit_option(VM&, Object const& options, String largest_unit);
|
2021-09-06 21:21:57 +03:00
|
|
|
|
Optional<u16> maximum_temporal_duration_rounding_increment(StringView unit);
|
2022-08-20 08:52:42 +01:00
|
|
|
|
ThrowCompletionOr<void> reject_object_with_calendar_or_time_zone(VM&, Object&);
|
2023-01-26 14:15:13 +00:00
|
|
|
|
ThrowCompletionOr<String> format_seconds_string_part(VM&, 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);
|
2022-06-15 00:58:04 +01:00
|
|
|
|
Crypto::SignedBigInteger round_number_to_increment_as_if_positive(Crypto::SignedBigInteger const&, u64 increment, StringView rounding_mode);
|
2022-08-30 17:43:27 +01:00
|
|
|
|
ThrowCompletionOr<ISODateTime> parse_iso_date_time(VM&, StringView iso_string);
|
2022-08-20 08:52:42 +01:00
|
|
|
|
ThrowCompletionOr<ISODateTime> parse_iso_date_time(VM&, ParseResult const& parse_result);
|
2023-01-26 14:22:47 +00:00
|
|
|
|
ThrowCompletionOr<TemporalInstant> parse_temporal_instant_string(VM&, StringView iso_string);
|
|
|
|
|
|
ThrowCompletionOr<ISODateTime> parse_temporal_zoned_date_time_string(VM&, StringView iso_string);
|
2023-01-26 14:48:42 +00:00
|
|
|
|
ThrowCompletionOr<String> parse_temporal_calendar_string(VM&, StringView iso_string);
|
2023-01-26 14:22:47 +00:00
|
|
|
|
ThrowCompletionOr<TemporalDate> parse_temporal_date_string(VM&, StringView iso_string);
|
|
|
|
|
|
ThrowCompletionOr<ISODateTime> parse_temporal_date_time_string(VM&, StringView iso_string);
|
|
|
|
|
|
ThrowCompletionOr<DurationRecord> parse_temporal_duration_string(VM&, StringView iso_string);
|
|
|
|
|
|
ThrowCompletionOr<TemporalMonthDay> parse_temporal_month_day_string(VM&, StringView iso_string);
|
|
|
|
|
|
ThrowCompletionOr<ISODateTime> parse_temporal_relative_to_string(VM&, StringView iso_string);
|
|
|
|
|
|
ThrowCompletionOr<TemporalTime> parse_temporal_time_string(VM&, StringView iso_string);
|
|
|
|
|
|
ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(VM&, StringView iso_string);
|
|
|
|
|
|
ThrowCompletionOr<TemporalYearMonth> parse_temporal_year_month_string(VM&, StringView iso_string);
|
2022-12-31 03:17:56 +01:00
|
|
|
|
ThrowCompletionOr<double> to_positive_integer_with_truncation(VM&, Value argument);
|
2023-01-26 14:37:52 +00:00
|
|
|
|
ThrowCompletionOr<Object*> prepare_temporal_fields(VM&, Object const& fields, Vector<String> const& field_names, Variant<PrepareTemporalFieldsPartial, Vector<StringView>> const& required_fields);
|
2022-08-20 08:52:42 +01:00
|
|
|
|
ThrowCompletionOr<DifferenceSettings> get_difference_settings(VM&, DifferenceOperation, Value options_value, UnitGroup unit_group, Vector<StringView> const& disallowed_units, TemporalUnitDefault const& fallback_smallest_unit, StringView smallest_largest_default_unit);
|
2021-07-11 21:04:11 +03:00
|
|
|
|
|
2022-06-14 23:03:25 +01:00
|
|
|
|
template<size_t Size>
|
2022-08-20 08:52:42 +01:00
|
|
|
|
ThrowCompletionOr<Value> get_option(VM& vm, Object const& options, PropertyKey const& property, OptionType type, StringView const (&values)[Size], OptionDefault const& default_)
|
2022-06-14 23:03:25 +01:00
|
|
|
|
{
|
2023-02-05 19:02:54 +00:00
|
|
|
|
return get_option(vm, options, property, type, ReadonlySpan<StringView> { values }, default_);
|
2022-06-14 23:03:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-06 03:31:29 +01:00
|
|
|
|
// 13.40 ToIntegerWithTruncation ( argument ), https://tc39.es/proposal-temporal/#sec-tointegerwithtruncation
|
2021-08-17 20:17:12 +01:00
|
|
|
|
template<typename... Args>
|
2023-01-06 03:31:29 +01:00
|
|
|
|
ThrowCompletionOr<double> to_integer_with_truncation(VM& vm, Value argument, ErrorType error_type, Args... args)
|
2021-08-17 20:17:12 +01:00
|
|
|
|
{
|
2023-01-06 03:31:29 +01:00
|
|
|
|
// 1. Let number be ? ToIntegerOrInfinity(argument).
|
|
|
|
|
|
auto number = TRY(argument.to_number(vm));
|
|
|
|
|
|
|
|
|
|
|
|
// 2. If number is NaN, return 0.
|
|
|
|
|
|
if (number.is_nan())
|
|
|
|
|
|
return 0;
|
2021-08-17 20:17:12 +01:00
|
|
|
|
|
2023-01-06 03:31:29 +01:00
|
|
|
|
// 3. If number is +∞𝔽 or -∞𝔽, throw a RangeError exception.
|
|
|
|
|
|
if (Value(number).is_infinity()) {
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.template throw_completion<RangeError>(error_type, args...);
|
2021-08-17 20:17:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-06 03:31:29 +01:00
|
|
|
|
// 4. Return truncate(ℝ(number)).
|
|
|
|
|
|
return trunc(number.as_double());
|
2021-08-17 20:17:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-13 15:11:55 -06:00
|
|
|
|
// 13.41 ToIntegerIfIntegral ( argument ), https://tc39.es/proposal-temporal/#sec-tointegerifintegral
|
2021-11-17 22:20:59 +00:00
|
|
|
|
template<typename... Args>
|
2022-11-13 15:11:55 -06:00
|
|
|
|
ThrowCompletionOr<double> to_integer_if_integral(VM& vm, Value argument, ErrorType error_type, Args... args)
|
2021-11-17 22:20:59 +00:00
|
|
|
|
{
|
|
|
|
|
|
// 1. Let number be ? ToNumber(argument).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto number = TRY(argument.to_number(vm));
|
2021-11-17 22:20:59 +00:00
|
|
|
|
|
2022-06-14 23:32:13 +01: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())
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.template throw_completion<RangeError>(error_type, args...);
|
2021-11-17 22:20:59 +00:00
|
|
|
|
|
|
|
|
|
|
// 4. Return ℝ(number).
|
|
|
|
|
|
return number.as_double();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-11 21:04:11 +03:00
|
|
|
|
}
|