AbstractOperations.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 OptionType {
  21. Boolean,
  22. String,
  23. Number
  24. };
  25. struct ISODateTime {
  26. i32 year;
  27. u8 month;
  28. u8 day;
  29. u8 hour;
  30. u8 minute;
  31. u8 second;
  32. u16 millisecond;
  33. u16 microsecond;
  34. u16 nanosecond;
  35. Optional<String> calendar = {};
  36. };
  37. struct TemporalInstant {
  38. i32 year;
  39. u8 month;
  40. u8 day;
  41. u8 hour;
  42. u8 minute;
  43. u8 second;
  44. u16 millisecond;
  45. u16 microsecond;
  46. u16 nanosecond;
  47. Optional<String> time_zone_offset;
  48. };
  49. struct TemporalDate {
  50. i32 year;
  51. u8 month;
  52. u8 day;
  53. Optional<String> calendar;
  54. };
  55. struct TemporalTime {
  56. u8 hour;
  57. u8 minute;
  58. u8 second;
  59. u16 millisecond;
  60. u16 microsecond;
  61. u16 nanosecond;
  62. Optional<String> calendar = {};
  63. };
  64. struct TemporalTimeZone {
  65. bool z;
  66. Optional<String> offset_string;
  67. Optional<String> name;
  68. };
  69. struct TemporalYearMonth {
  70. i32 year;
  71. u8 month;
  72. u8 day;
  73. Optional<String> calendar = {};
  74. };
  75. struct TemporalMonthDay {
  76. Optional<i32> year;
  77. u8 month;
  78. u8 day;
  79. Optional<String> calendar = {};
  80. };
  81. struct TemporalZonedDateTime {
  82. ISODateTime date_time;
  83. TemporalTimeZone time_zone;
  84. };
  85. struct SecondsStringPrecision {
  86. Variant<StringView, u8> precision;
  87. String unit;
  88. u32 increment;
  89. };
  90. ThrowCompletionOr<MarkedVector<Value>> iterable_to_list_of_type(GlobalObject&, Value items, Vector<OptionType> const& element_types);
  91. ThrowCompletionOr<Object*> get_options_object(GlobalObject&, Value options);
  92. ThrowCompletionOr<Value> get_option(GlobalObject&, Object const& options, PropertyKey const& property, Vector<OptionType> const& types, Vector<StringView> const& values, Value fallback);
  93. template<typename NumberType>
  94. ThrowCompletionOr<Variant<String, NumberType>> get_string_or_number_option(GlobalObject&, Object const& options, PropertyKey const& property, Vector<StringView> const& string_values, NumberType minimum, NumberType maximum, Value fallback);
  95. ThrowCompletionOr<String> to_temporal_overflow(GlobalObject&, Object const* options);
  96. ThrowCompletionOr<String> to_temporal_disambiguation(GlobalObject&, Object const* options);
  97. ThrowCompletionOr<String> to_temporal_rounding_mode(GlobalObject&, Object const& normalized_options, String const& fallback);
  98. StringView negate_temporal_rounding_mode(String const& rounding_mode);
  99. ThrowCompletionOr<String> to_temporal_offset(GlobalObject&, Object const* options, String const& fallback);
  100. ThrowCompletionOr<String> to_show_calendar_option(GlobalObject&, Object const& normalized_options);
  101. ThrowCompletionOr<String> to_show_time_zone_name_option(GlobalObject&, Object const& normalized_options);
  102. ThrowCompletionOr<String> to_show_offset_option(GlobalObject&, Object const& normalized_options);
  103. ThrowCompletionOr<u64> to_temporal_rounding_increment(GlobalObject&, Object const& normalized_options, Optional<double> dividend, bool inclusive);
  104. ThrowCompletionOr<u64> to_temporal_date_time_rounding_increment(GlobalObject&, Object const& normalized_options, StringView smallest_unit);
  105. ThrowCompletionOr<SecondsStringPrecision> to_seconds_string_precision(GlobalObject&, Object const& normalized_options);
  106. ThrowCompletionOr<Optional<String>> to_largest_temporal_unit(GlobalObject&, Object const& normalized_options, Vector<StringView> const& disallowed_units, Optional<String> fallback, Optional<String> auto_value = {});
  107. ThrowCompletionOr<Optional<String>> to_smallest_temporal_unit(GlobalObject&, Object const& normalized_options, Vector<StringView> const& disallowed_units, Optional<String> fallback);
  108. ThrowCompletionOr<String> to_temporal_duration_total_unit(GlobalObject& global_object, Object const& normalized_options);
  109. ThrowCompletionOr<Value> to_relative_temporal_object(GlobalObject&, Object const& options);
  110. ThrowCompletionOr<void> validate_temporal_unit_range(GlobalObject&, StringView largest_unit, StringView smallest_unit);
  111. StringView larger_of_two_temporal_units(StringView, StringView);
  112. ThrowCompletionOr<Object*> merge_largest_unit_option(GlobalObject&, Object const* options, String largest_unit);
  113. Optional<u16> maximum_temporal_duration_rounding_increment(StringView unit);
  114. ThrowCompletionOr<void> reject_object_with_calendar_or_time_zone(GlobalObject&, Object&);
  115. String format_seconds_string_part(u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, Variant<StringView, u8> const& precision);
  116. double sign(double);
  117. double sign(Crypto::SignedBigInteger const&);
  118. i64 round_number_to_increment(double, u64 increment, StringView rounding_mode);
  119. BigInt* round_number_to_increment(GlobalObject&, BigInt const&, u64 increment, StringView rounding_mode);
  120. ThrowCompletionOr<ISODateTime> parse_iso_date_time(GlobalObject&, ParseResult const& parse_result);
  121. ThrowCompletionOr<TemporalInstant> parse_temporal_instant_string(GlobalObject&, String const& iso_string);
  122. ThrowCompletionOr<TemporalZonedDateTime> parse_temporal_zoned_date_time_string(GlobalObject&, String const& iso_string);
  123. ThrowCompletionOr<String> parse_temporal_calendar_string(GlobalObject&, String const& iso_string);
  124. ThrowCompletionOr<TemporalDate> parse_temporal_date_string(GlobalObject&, String const& iso_string);
  125. ThrowCompletionOr<ISODateTime> parse_temporal_date_time_string(GlobalObject&, String const& iso_string);
  126. ThrowCompletionOr<DurationRecord> parse_temporal_duration_string(GlobalObject&, String const& iso_string);
  127. ThrowCompletionOr<TemporalMonthDay> parse_temporal_month_day_string(GlobalObject&, String const& iso_string);
  128. ThrowCompletionOr<TemporalZonedDateTime> parse_temporal_relative_to_string(GlobalObject&, String const& iso_string);
  129. ThrowCompletionOr<TemporalTime> parse_temporal_time_string(GlobalObject&, String const& iso_string);
  130. ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject&, String const& iso_string);
  131. ThrowCompletionOr<TemporalYearMonth> parse_temporal_year_month_string(GlobalObject&, String const& iso_string);
  132. ThrowCompletionOr<double> to_positive_integer(GlobalObject&, Value argument);
  133. ThrowCompletionOr<Object*> prepare_temporal_fields(GlobalObject&, Object const& fields, Vector<String> const& field_names, Vector<StringView> const& required_fields);
  134. ThrowCompletionOr<Object*> prepare_partial_temporal_fields(GlobalObject&, Object const& fields, Vector<String> const& field_names);
  135. // 13.44 ToIntegerThrowOnInfinity ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-tointegerthrowoninfinity
  136. template<typename... Args>
  137. ThrowCompletionOr<double> to_integer_throw_on_infinity(GlobalObject& global_object, Value argument, ErrorType error_type, Args... args)
  138. {
  139. auto& vm = global_object.vm();
  140. // 1. Let integer be ? ToIntegerOrInfinity(argument).
  141. auto integer = TRY(argument.to_integer_or_infinity(global_object));
  142. // 2. If integer is -∞ or +∞ , then
  143. if (Value(integer).is_infinity()) {
  144. // a. Throw a RangeError exception.
  145. return vm.template throw_completion<RangeError>(global_object, error_type, args...);
  146. }
  147. // 3. Return integer.
  148. return integer;
  149. }
  150. // 13.45 ToIntegerWithoutRounding ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-tointegerwithoutrounding
  151. template<typename... Args>
  152. ThrowCompletionOr<double> to_integer_without_rounding(GlobalObject& global_object, Value argument, ErrorType error_type, Args... args)
  153. {
  154. auto& vm = global_object.vm();
  155. // 1. Let number be ? ToNumber(argument).
  156. auto number = TRY(argument.to_number(global_object));
  157. // 2. If number is NaN, +0𝔽, or -0𝔽 return 0.
  158. if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero())
  159. return 0;
  160. // 3. If IsIntegralNumber(number) is false, throw a RangeError exception.
  161. if (!number.is_integral_number())
  162. return vm.template throw_completion<RangeError>(global_object, error_type, args...);
  163. // 4. Return ℝ(number).
  164. return number.as_double();
  165. }
  166. }