AbstractOperations.h 8.8 KB

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