AbstractOperations.h 8.9 KB

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