AbstractOperations.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Forward.h>
  9. #include <AK/Variant.h>
  10. #include <LibCrypto/BigInt/SignedBigInteger.h>
  11. #include <LibJS/Forward.h>
  12. #include <LibJS/Runtime/Completion.h>
  13. #include <LibJS/Runtime/GlobalObject.h>
  14. #include <LibJS/Runtime/Temporal/ISO8601.h>
  15. #include <LibJS/Runtime/Temporal/TimeZoneMethods.h>
  16. #include <LibJS/Runtime/ValueInlines.h>
  17. namespace JS::Temporal {
  18. enum class ArithmeticOperation {
  19. Add,
  20. Subtract,
  21. };
  22. enum class DifferenceOperation {
  23. Since,
  24. Until,
  25. };
  26. enum class UnsignedRoundingMode {
  27. HalfEven,
  28. HalfInfinity,
  29. HalfZero,
  30. Infinity,
  31. Zero,
  32. };
  33. enum class OptionType {
  34. Boolean,
  35. String,
  36. Number
  37. };
  38. enum class UnitGroup {
  39. Date,
  40. Time,
  41. DateTime,
  42. };
  43. struct TemporalInstant {
  44. i32 year;
  45. u8 month;
  46. u8 day;
  47. u8 hour;
  48. u8 minute;
  49. u8 second;
  50. u16 millisecond;
  51. u16 microsecond;
  52. u16 nanosecond;
  53. Optional<String> time_zone_offset;
  54. };
  55. struct TemporalDate {
  56. i32 year;
  57. u8 month;
  58. u8 day;
  59. Optional<String> calendar;
  60. };
  61. struct TemporalTime {
  62. u8 hour;
  63. u8 minute;
  64. u8 second;
  65. u16 millisecond;
  66. u16 microsecond;
  67. u16 nanosecond;
  68. Optional<String> calendar = {};
  69. };
  70. struct TemporalTimeZone {
  71. bool z;
  72. Optional<String> offset_string;
  73. Optional<String> name;
  74. };
  75. struct TemporalYearMonth {
  76. i32 year;
  77. u8 month;
  78. u8 day;
  79. Optional<String> calendar = {};
  80. };
  81. struct TemporalMonthDay {
  82. Optional<i32> year;
  83. u8 month;
  84. u8 day;
  85. Optional<String> calendar = {};
  86. };
  87. struct ISODateTime {
  88. i32 year;
  89. u8 month;
  90. u8 day;
  91. u8 hour;
  92. u8 minute;
  93. u8 second;
  94. u16 millisecond;
  95. u16 microsecond;
  96. u16 nanosecond;
  97. TemporalTimeZone time_zone { .z = false, .offset_string = {}, .name = {} };
  98. Optional<String> calendar = {};
  99. };
  100. struct SecondsStringPrecision {
  101. Variant<StringView, u8> precision;
  102. StringView unit;
  103. u32 increment;
  104. };
  105. struct DifferenceSettings {
  106. String smallest_unit;
  107. String largest_unit;
  108. String rounding_mode;
  109. u64 rounding_increment;
  110. NonnullGCPtr<Object> options;
  111. };
  112. struct TemporalUnitRequired { };
  113. struct PrepareTemporalFieldsPartial { };
  114. struct GetOptionRequired { };
  115. using OptionDefault = Variant<GetOptionRequired, Empty, bool, StringView, double>;
  116. using TemporalUnitDefault = Variant<TemporalUnitRequired, Optional<StringView>>;
  117. ThrowCompletionOr<MarkedVector<Value>> iterable_to_list_of_type(VM&, Value items, Vector<OptionType> const& element_types);
  118. ThrowCompletionOr<Object*> get_options_object(VM&, Value options);
  119. ThrowCompletionOr<Value> get_option(VM&, Object const& options, PropertyKey const& property, OptionType type, ReadonlySpan<StringView> values, OptionDefault const&);
  120. ThrowCompletionOr<String> to_temporal_overflow(VM&, Object const* options);
  121. ThrowCompletionOr<String> to_temporal_disambiguation(VM&, Object const* options);
  122. ThrowCompletionOr<String> to_temporal_rounding_mode(VM&, Object const& normalized_options, StringView fallback);
  123. StringView negate_temporal_rounding_mode(StringView rounding_mode);
  124. ThrowCompletionOr<String> to_temporal_offset(VM&, Object const* options, StringView fallback);
  125. ThrowCompletionOr<String> to_calendar_name_option(VM&, Object const& normalized_options);
  126. ThrowCompletionOr<String> to_time_zone_name_option(VM&, Object const& normalized_options);
  127. ThrowCompletionOr<String> to_show_offset_option(VM&, Object const& normalized_options);
  128. ThrowCompletionOr<double> to_temporal_rounding_increment(VM& vm, Object const& normalized_options);
  129. ThrowCompletionOr<u64> validate_temporal_rounding_increment(VM& vm, double increment, double dividend, bool inclusive);
  130. ThrowCompletionOr<u64> to_temporal_date_time_rounding_increment(VM&, Object const& normalized_options, StringView smallest_unit);
  131. ThrowCompletionOr<SecondsStringPrecision> to_seconds_string_precision_record(VM&, Object const& normalized_options);
  132. ThrowCompletionOr<Optional<String>> get_temporal_unit(VM&, Object const& normalized_options, PropertyKey const&, UnitGroup, TemporalUnitDefault const& default_, Vector<StringView> const& extra_values = {});
  133. struct RelativeTo {
  134. GCPtr<PlainDate> plain_relative_to; // [[PlainRelativeTo]]
  135. GCPtr<ZonedDateTime> zoned_relative_to; // [[ZonedRelativeTo]]
  136. Optional<TimeZoneMethods> time_zone_record; // [[TimeZoneRec]]
  137. };
  138. ThrowCompletionOr<RelativeTo> to_relative_temporal_object(VM&, Object const& options);
  139. Value relative_to_converted_to_value(RelativeTo const&);
  140. StringView larger_of_two_temporal_units(StringView, StringView);
  141. ThrowCompletionOr<Object*> merge_largest_unit_option(VM&, Object const& options, String largest_unit);
  142. Optional<u16> maximum_temporal_duration_rounding_increment(StringView unit);
  143. ThrowCompletionOr<void> reject_object_with_calendar_or_time_zone(VM&, Object&);
  144. ThrowCompletionOr<String> format_seconds_string_part(VM&, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, Variant<StringView, u8> const& precision);
  145. double sign(double);
  146. double sign(Crypto::SignedBigInteger const&);
  147. UnsignedRoundingMode get_unsigned_rounding_mode(StringView rounding_mode, bool is_negative);
  148. double apply_unsigned_rounding_mode(double x, double r1, double r2, Optional<UnsignedRoundingMode> const&);
  149. Crypto::SignedBigInteger apply_unsigned_rounding_mode(Crypto::SignedDivisionResult const&, Crypto::SignedBigInteger const& r1, Crypto::SignedBigInteger const& r2, Optional<UnsignedRoundingMode> const&, Crypto::UnsignedBigInteger const& increment);
  150. double round_number_to_increment(double, u64 increment, StringView rounding_mode);
  151. Crypto::SignedBigInteger round_number_to_increment(Crypto::SignedBigInteger const&, u64 increment, StringView rounding_mode);
  152. Crypto::SignedBigInteger round_number_to_increment_as_if_positive(Crypto::SignedBigInteger const&, u64 increment, StringView rounding_mode);
  153. ThrowCompletionOr<ISODateTime> parse_iso_date_time(VM&, StringView iso_string);
  154. ThrowCompletionOr<ISODateTime> parse_iso_date_time(VM&, ParseResult const& parse_result);
  155. ThrowCompletionOr<TemporalInstant> parse_temporal_instant_string(VM&, StringView iso_string);
  156. ThrowCompletionOr<ISODateTime> parse_temporal_zoned_date_time_string(VM&, StringView iso_string);
  157. ThrowCompletionOr<String> parse_temporal_calendar_string(VM&, StringView iso_string);
  158. ThrowCompletionOr<TemporalDate> parse_temporal_date_string(VM&, StringView iso_string);
  159. ThrowCompletionOr<ISODateTime> parse_temporal_date_time_string(VM&, StringView iso_string);
  160. ThrowCompletionOr<DurationRecord> parse_temporal_duration_string(VM&, StringView iso_string);
  161. ThrowCompletionOr<TemporalMonthDay> parse_temporal_month_day_string(VM&, StringView iso_string);
  162. ThrowCompletionOr<ISODateTime> parse_temporal_relative_to_string(VM&, StringView iso_string);
  163. ThrowCompletionOr<TemporalTime> parse_temporal_time_string(VM&, StringView iso_string);
  164. ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(VM&, StringView iso_string);
  165. ThrowCompletionOr<TemporalYearMonth> parse_temporal_year_month_string(VM&, StringView iso_string);
  166. ThrowCompletionOr<double> to_positive_integer_with_truncation(VM&, Value argument);
  167. ThrowCompletionOr<Object*> prepare_temporal_fields(VM&, Object const& fields, Vector<String> const& field_names, Variant<PrepareTemporalFieldsPartial, Vector<StringView>> const& required_fields);
  168. 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);
  169. template<size_t Size>
  170. ThrowCompletionOr<Value> get_option(VM& vm, Object const& options, PropertyKey const& property, OptionType type, StringView const (&values)[Size], OptionDefault const& default_)
  171. {
  172. return get_option(vm, options, property, type, ReadonlySpan<StringView> { values }, default_);
  173. }
  174. // 13.40 ToIntegerWithTruncation ( argument ), https://tc39.es/proposal-temporal/#sec-tointegerwithtruncation
  175. template<typename... Args>
  176. ThrowCompletionOr<double> to_integer_with_truncation(VM& vm, Value argument, ErrorType error_type, Args... args)
  177. {
  178. // 1. Let number be ? ToIntegerOrInfinity(argument).
  179. auto number = TRY(argument.to_number(vm));
  180. // 2. If number is NaN, return 0.
  181. if (number.is_nan())
  182. return 0;
  183. // 3. If number is +∞𝔽 or -∞𝔽, throw a RangeError exception.
  184. if (Value(number).is_infinity()) {
  185. return vm.template throw_completion<RangeError>(error_type, args...);
  186. }
  187. // 4. Return truncate(ℝ(number)).
  188. return trunc(number.as_double());
  189. }
  190. // 13.41 ToIntegerIfIntegral ( argument ), https://tc39.es/proposal-temporal/#sec-tointegerifintegral
  191. template<typename... Args>
  192. ThrowCompletionOr<double> to_integer_if_integral(VM& vm, Value argument, ErrorType error_type, Args... args)
  193. {
  194. // 1. Let number be ? ToNumber(argument).
  195. auto number = TRY(argument.to_number(vm));
  196. // 2. If number is NaN, +0𝔽, or -0𝔽, return 0.
  197. if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero())
  198. return 0;
  199. // 3. If IsIntegralNumber(number) is false, throw a RangeError exception.
  200. if (!number.is_integral_number())
  201. return vm.template throw_completion<RangeError>(error_type, args...);
  202. // 4. Return ℝ(number).
  203. return number.as_double();
  204. }
  205. }