AbstractOperations.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/DeprecatedString.h>
  9. #include <AK/Forward.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 TemporalInstant {
  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<DeprecatedString> time_zone_offset;
  52. };
  53. struct TemporalDate {
  54. i32 year;
  55. u8 month;
  56. u8 day;
  57. Optional<DeprecatedString> calendar;
  58. };
  59. struct TemporalTime {
  60. u8 hour;
  61. u8 minute;
  62. u8 second;
  63. u16 millisecond;
  64. u16 microsecond;
  65. u16 nanosecond;
  66. Optional<DeprecatedString> calendar = {};
  67. };
  68. struct TemporalTimeZone {
  69. bool z;
  70. Optional<DeprecatedString> offset_string;
  71. Optional<DeprecatedString> name;
  72. };
  73. struct TemporalYearMonth {
  74. i32 year;
  75. u8 month;
  76. u8 day;
  77. Optional<DeprecatedString> calendar = {};
  78. };
  79. struct TemporalMonthDay {
  80. Optional<i32> year;
  81. u8 month;
  82. u8 day;
  83. Optional<DeprecatedString> calendar = {};
  84. };
  85. struct ISODateTime {
  86. i32 year;
  87. u8 month;
  88. u8 day;
  89. u8 hour;
  90. u8 minute;
  91. u8 second;
  92. u16 millisecond;
  93. u16 microsecond;
  94. u16 nanosecond;
  95. TemporalTimeZone time_zone { .z = false, .offset_string = {}, .name = {} };
  96. Optional<DeprecatedString> calendar = {};
  97. };
  98. struct SecondsStringPrecision {
  99. Variant<StringView, u8> precision;
  100. DeprecatedString unit;
  101. u32 increment;
  102. };
  103. struct DifferenceSettings {
  104. DeprecatedString smallest_unit;
  105. DeprecatedString largest_unit;
  106. DeprecatedString rounding_mode;
  107. u64 rounding_increment;
  108. Object& options;
  109. };
  110. struct TemporalUnitRequired { };
  111. struct PrepareTemporalFieldsPartial { };
  112. struct GetOptionRequired { };
  113. using OptionDefault = Variant<GetOptionRequired, Empty, bool, StringView, double>;
  114. using TemporalUnitDefault = Variant<TemporalUnitRequired, Optional<StringView>>;
  115. ThrowCompletionOr<MarkedVector<Value>> iterable_to_list_of_type(VM&, Value items, Vector<OptionType> const& element_types);
  116. ThrowCompletionOr<Object*> get_options_object(VM&, Value options);
  117. ThrowCompletionOr<Value> get_option(VM&, Object const& options, PropertyKey const& property, OptionType type, Span<StringView const> values, OptionDefault const&);
  118. ThrowCompletionOr<String> to_temporal_overflow(VM&, Object const* options);
  119. ThrowCompletionOr<String> to_temporal_disambiguation(VM&, Object const* options);
  120. ThrowCompletionOr<DeprecatedString> to_temporal_rounding_mode(VM&, Object const& normalized_options, DeprecatedString const& fallback);
  121. StringView negate_temporal_rounding_mode(DeprecatedString const& rounding_mode);
  122. ThrowCompletionOr<DeprecatedString> to_temporal_offset(VM&, Object const* options, DeprecatedString const& fallback);
  123. ThrowCompletionOr<DeprecatedString> to_calendar_name_option(VM&, Object const& normalized_options);
  124. ThrowCompletionOr<DeprecatedString> to_time_zone_name_option(VM&, Object const& normalized_options);
  125. ThrowCompletionOr<DeprecatedString> to_show_offset_option(VM&, Object const& normalized_options);
  126. ThrowCompletionOr<u64> to_temporal_rounding_increment(VM&, Object const& normalized_options, Optional<double> dividend, bool inclusive);
  127. ThrowCompletionOr<u64> to_temporal_date_time_rounding_increment(VM&, Object const& normalized_options, StringView smallest_unit);
  128. ThrowCompletionOr<SecondsStringPrecision> to_seconds_string_precision(VM&, Object const& normalized_options);
  129. ThrowCompletionOr<Optional<DeprecatedString>> get_temporal_unit(VM&, Object const& normalized_options, PropertyKey const&, UnitGroup, TemporalUnitDefault const& default_, Vector<StringView> const& extra_values = {});
  130. ThrowCompletionOr<Value> to_relative_temporal_object(VM&, Object const& options);
  131. StringView larger_of_two_temporal_units(StringView, StringView);
  132. ThrowCompletionOr<Object*> merge_largest_unit_option(VM&, Object const& options, DeprecatedString largest_unit);
  133. Optional<u16> maximum_temporal_duration_rounding_increment(StringView unit);
  134. ThrowCompletionOr<void> reject_object_with_calendar_or_time_zone(VM&, Object&);
  135. DeprecatedString format_seconds_string_part(u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, Variant<StringView, u8> const& precision);
  136. double sign(double);
  137. double sign(Crypto::SignedBigInteger const&);
  138. UnsignedRoundingMode get_unsigned_rounding_mode(StringView rounding_mode, bool is_negative);
  139. double apply_unsigned_rounding_mode(double x, double r1, double r2, Optional<UnsignedRoundingMode> const&);
  140. Crypto::SignedBigInteger apply_unsigned_rounding_mode(Crypto::SignedDivisionResult const&, Crypto::SignedBigInteger const& r1, Crypto::SignedBigInteger const& r2, Optional<UnsignedRoundingMode> const&, Crypto::UnsignedBigInteger const& increment);
  141. double round_number_to_increment(double, u64 increment, StringView rounding_mode);
  142. Crypto::SignedBigInteger round_number_to_increment(Crypto::SignedBigInteger const&, u64 increment, StringView rounding_mode);
  143. Crypto::SignedBigInteger round_number_to_increment_as_if_positive(Crypto::SignedBigInteger const&, u64 increment, StringView rounding_mode);
  144. ThrowCompletionOr<ISODateTime> parse_iso_date_time(VM&, StringView iso_string);
  145. ThrowCompletionOr<ISODateTime> parse_iso_date_time(VM&, ParseResult const& parse_result);
  146. ThrowCompletionOr<TemporalInstant> parse_temporal_instant_string(VM&, DeprecatedString const& iso_string);
  147. ThrowCompletionOr<ISODateTime> parse_temporal_zoned_date_time_string(VM&, DeprecatedString const& iso_string);
  148. ThrowCompletionOr<DeprecatedString> parse_temporal_calendar_string(VM&, DeprecatedString const& iso_string);
  149. ThrowCompletionOr<TemporalDate> parse_temporal_date_string(VM&, DeprecatedString const& iso_string);
  150. ThrowCompletionOr<ISODateTime> parse_temporal_date_time_string(VM&, DeprecatedString const& iso_string);
  151. ThrowCompletionOr<DurationRecord> parse_temporal_duration_string(VM&, DeprecatedString const& iso_string);
  152. ThrowCompletionOr<TemporalMonthDay> parse_temporal_month_day_string(VM&, DeprecatedString const& iso_string);
  153. ThrowCompletionOr<ISODateTime> parse_temporal_relative_to_string(VM&, DeprecatedString const& iso_string);
  154. ThrowCompletionOr<TemporalTime> parse_temporal_time_string(VM&, DeprecatedString const& iso_string);
  155. ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(VM&, DeprecatedString const& iso_string);
  156. ThrowCompletionOr<TemporalYearMonth> parse_temporal_year_month_string(VM&, DeprecatedString const& iso_string);
  157. ThrowCompletionOr<double> to_positive_integer_with_truncation(VM&, Value argument);
  158. ThrowCompletionOr<Object*> prepare_temporal_fields(VM&, Object const& fields, Vector<DeprecatedString> const& field_names, Variant<PrepareTemporalFieldsPartial, Vector<StringView>> const& required_fields);
  159. 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);
  160. template<size_t Size>
  161. ThrowCompletionOr<Value> get_option(VM& vm, Object const& options, PropertyKey const& property, OptionType type, StringView const (&values)[Size], OptionDefault const& default_)
  162. {
  163. return get_option(vm, options, property, type, Span<StringView const> { values }, default_);
  164. }
  165. // 13.40 ToIntegerWithTruncation ( argument ), https://tc39.es/proposal-temporal/#sec-tointegerwithtruncation
  166. template<typename... Args>
  167. ThrowCompletionOr<double> to_integer_with_truncation(VM& vm, Value argument, ErrorType error_type, Args... args)
  168. {
  169. // 1. Let number be ? ToIntegerOrInfinity(argument).
  170. auto number = TRY(argument.to_number(vm));
  171. // 2. If number is NaN, return 0.
  172. if (number.is_nan())
  173. return 0;
  174. // 3. If number is +∞𝔽 or -∞𝔽, throw a RangeError exception.
  175. if (Value(number).is_infinity()) {
  176. return vm.template throw_completion<RangeError>(error_type, args...);
  177. }
  178. // 4. Return truncate(ℝ(number)).
  179. return trunc(number.as_double());
  180. }
  181. // 13.41 ToIntegerIfIntegral ( argument ), https://tc39.es/proposal-temporal/#sec-tointegerifintegral
  182. template<typename... Args>
  183. ThrowCompletionOr<double> to_integer_if_integral(VM& vm, Value argument, ErrorType error_type, Args... args)
  184. {
  185. // 1. Let number be ? ToNumber(argument).
  186. auto number = TRY(argument.to_number(vm));
  187. // 2. If number is NaN, +0𝔽, or -0𝔽, return 0.
  188. if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero())
  189. return 0;
  190. // 3. If IsIntegralNumber(number) is false, throw a RangeError exception.
  191. if (!number.is_integral_number())
  192. return vm.template throw_completion<RangeError>(error_type, args...);
  193. // 4. Return ℝ(number).
  194. return number.as_double();
  195. }
  196. }