AbstractOperations.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/CharacterTypes.h>
  8. #include <AK/DateTimeLexer.h>
  9. #include <AK/TypeCasts.h>
  10. #include <AK/Variant.h>
  11. #include <LibJS/Runtime/Completion.h>
  12. #include <LibJS/Runtime/IteratorOperations.h>
  13. #include <LibJS/Runtime/PropertyName.h>
  14. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  15. #include <LibJS/Runtime/Temporal/Calendar.h>
  16. #include <LibJS/Runtime/Temporal/Duration.h>
  17. #include <LibJS/Runtime/Temporal/PlainDate.h>
  18. #include <LibJS/Runtime/Temporal/PlainDateTime.h>
  19. #include <LibJS/Runtime/Temporal/PlainTime.h>
  20. #include <LibJS/Runtime/Temporal/TimeZone.h>
  21. #include <LibJS/Runtime/Temporal/ZonedDateTime.h>
  22. namespace JS::Temporal {
  23. static Optional<OptionType> to_option_type(Value value)
  24. {
  25. if (value.is_boolean())
  26. return OptionType::Boolean;
  27. if (value.is_string())
  28. return OptionType::String;
  29. if (value.is_number())
  30. return OptionType::Number;
  31. return {};
  32. }
  33. // 13.1 IterableToListOfType ( items, elementTypes ), https://tc39.es/proposal-temporal/#sec-iterabletolistoftype
  34. ThrowCompletionOr<MarkedValueList> iterable_to_list_of_type(GlobalObject& global_object, Value items, Vector<OptionType> const& element_types)
  35. {
  36. auto& vm = global_object.vm();
  37. auto& heap = global_object.heap();
  38. // 1. Let iteratorRecord be ? GetIterator(items, sync).
  39. auto iterator_record = get_iterator(global_object, items, IteratorHint::Sync);
  40. if (auto* exception = vm.exception())
  41. return throw_completion(exception->value());
  42. // 2. Let values be a new empty List.
  43. MarkedValueList values(heap);
  44. // 3. Let next be true.
  45. auto next = true;
  46. // 4. Repeat, while next is not false,
  47. while (next) {
  48. // a. Set next to ? IteratorStep(iteratorRecord).
  49. auto* iterator_result = iterator_step(global_object, *iterator_record);
  50. if (auto* exception = vm.exception())
  51. return throw_completion(exception->value());
  52. next = iterator_result;
  53. // b. If next is not false, then
  54. if (next) {
  55. // i. Let nextValue be ? IteratorValue(next).
  56. auto next_value = iterator_value(global_object, *iterator_result);
  57. if (auto* exception = vm.exception())
  58. return throw_completion(exception->value());
  59. // ii. If Type(nextValue) is not an element of elementTypes, then
  60. if (auto type = to_option_type(next_value); !type.has_value() || !element_types.contains_slow(*type)) {
  61. // 1. Let completion be ThrowCompletion(a newly created TypeError object).
  62. auto completion = vm.throw_completion<TypeError>(global_object, ErrorType::FixmeAddAnErrorString);
  63. // 2. Return ? IteratorClose(iteratorRecord, completion).
  64. iterator_close(*iterator_record);
  65. return completion;
  66. }
  67. // iii. Append nextValue to the end of the List values.
  68. values.append(next_value);
  69. }
  70. }
  71. // 5. Return values.
  72. return { move(values) };
  73. }
  74. // 13.2 GetOptionsObject ( options ), https://tc39.es/proposal-temporal/#sec-getoptionsobject
  75. ThrowCompletionOr<Object*> get_options_object(GlobalObject& global_object, Value options)
  76. {
  77. auto& vm = global_object.vm();
  78. // 1. If options is undefined, then
  79. if (options.is_undefined()) {
  80. // a. Return ! OrdinaryObjectCreate(null).
  81. return Object::create(global_object, nullptr);
  82. }
  83. // 2. If Type(options) is Object, then
  84. if (options.is_object()) {
  85. // a. Return options.
  86. return &options.as_object();
  87. }
  88. // 3. Throw a TypeError exception.
  89. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, "Options");
  90. }
  91. // 13.3 GetOption ( options, property, types, values, fallback ), https://tc39.es/proposal-temporal/#sec-getoption
  92. ThrowCompletionOr<Value> get_option(GlobalObject& global_object, Object const& options, PropertyName const& property, Vector<OptionType> const& types, Vector<StringView> const& values, Value fallback)
  93. {
  94. VERIFY(property.is_string());
  95. auto& vm = global_object.vm();
  96. // 1. Assert: Type(options) is Object.
  97. // 2. Assert: Each element of types is Boolean, String, or Number.
  98. // 3. Let value be ? Get(options, property).
  99. auto value = options.get(property);
  100. if (auto* exception = vm.exception())
  101. return throw_completion(exception->value());
  102. // 4. If value is undefined, return fallback.
  103. if (value.is_undefined())
  104. return fallback;
  105. OptionType type;
  106. // 5. If types contains Type(value), then
  107. if (auto value_type = to_option_type(value); value_type.has_value() && types.contains_slow(*value_type)) {
  108. // a. Let type be Type(value).
  109. type = *value_type;
  110. }
  111. // 6. Else,
  112. else {
  113. // a. Let type be the last element of types.
  114. type = types.last();
  115. }
  116. // 7. If type is Boolean, then
  117. if (type == OptionType::Boolean) {
  118. // a. Set value to ! ToBoolean(value).
  119. value = Value(value.to_boolean());
  120. }
  121. // 8. Else if type is Number, then
  122. else if (type == OptionType::Number) {
  123. // a. Set value to ? ToNumber(value).
  124. value = value.to_number(global_object);
  125. if (auto* exception = vm.exception())
  126. return throw_completion(exception->value());
  127. // b. If value is NaN, throw a RangeError exception.
  128. if (value.is_nan())
  129. return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, vm.names.NaN.as_string(), property.as_string());
  130. }
  131. // 9. Else,
  132. else {
  133. // a. Set value to ? ToString(value).
  134. value = value.to_primitive_string(global_object);
  135. if (auto* exception = vm.exception())
  136. return throw_completion(exception->value());
  137. }
  138. // 10. If values is not empty, then
  139. if (!values.is_empty()) {
  140. VERIFY(value.is_string());
  141. // a. If values does not contain value, throw a RangeError exception.
  142. if (!values.contains_slow(value.as_string().string()))
  143. return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, value.as_string().string(), property.as_string());
  144. }
  145. // 11. Return value.
  146. return value;
  147. }
  148. // 13.4 GetStringOrNumberOption ( options, property, stringValues, minimum, maximum, fallback ), https://tc39.es/proposal-temporal/#sec-getstringornumberoption
  149. template<typename NumberType>
  150. ThrowCompletionOr<Variant<String, NumberType>> get_string_or_number_option(GlobalObject& global_object, Object const& options, PropertyName const& property, Vector<StringView> const& string_values, NumberType minimum, NumberType maximum, Value fallback)
  151. {
  152. auto& vm = global_object.vm();
  153. // 1. Assert: Type(options) is Object.
  154. // 2. Let value be ? GetOption(options, property, « Number, String », empty, fallback).
  155. auto value = TRY(get_option(global_object, options, property, { OptionType::Number, OptionType::String }, {}, fallback));
  156. // 3. If Type(value) is Number, then
  157. if (value.is_number()) {
  158. // a. If value < minimum or value > maximum, throw a RangeError exception.
  159. if (value.as_double() < minimum || value.as_double() > maximum)
  160. return vm.template throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, value.as_double(), property.as_string());
  161. // b. Return floor(ℝ(value)).
  162. return { static_cast<NumberType>(floor(value.as_double())) };
  163. }
  164. // 4. Assert: Type(value) is String.
  165. VERIFY(value.is_string());
  166. // 5. If stringValues does not contain value, throw a RangeError exception.
  167. if (!string_values.contains_slow(value.as_string().string()))
  168. return vm.template throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, value.as_string().string(), property.as_string());
  169. // 6. Return value.
  170. return { value.as_string().string() };
  171. }
  172. // 13.6 ToTemporalOverflow ( normalizedOptions ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaloverflow
  173. ThrowCompletionOr<String> to_temporal_overflow(GlobalObject& global_object, Object const& normalized_options)
  174. {
  175. auto& vm = global_object.vm();
  176. // 1. Return ? GetOption(normalizedOptions, "overflow", « String », « "constrain", "reject" », "constrain").
  177. auto option = TRY(get_option(global_object, normalized_options, vm.names.overflow, { OptionType::String }, { "constrain"sv, "reject"sv }, js_string(vm, "constrain")));
  178. VERIFY(option.is_string());
  179. return option.as_string().string();
  180. }
  181. // 13.8 ToTemporalRoundingMode ( normalizedOptions, fallback ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalroundingmode
  182. ThrowCompletionOr<String> to_temporal_rounding_mode(GlobalObject& global_object, Object const& normalized_options, String const& fallback)
  183. {
  184. auto& vm = global_object.vm();
  185. // 1. Return ? GetOption(normalizedOptions, "roundingMode", « String », « "ceil", "floor", "trunc", "halfExpand" », fallback).
  186. auto option = TRY(get_option(global_object, normalized_options, vm.names.roundingMode, { OptionType::String }, { "ceil"sv, "floor"sv, "trunc"sv, "halfExpand"sv }, js_string(vm, fallback)));
  187. VERIFY(option.is_string());
  188. return option.as_string().string();
  189. }
  190. // 13.11 ToShowCalendarOption ( normalizedOptions ), https://tc39.es/proposal-temporal/#sec-temporal-toshowcalendaroption
  191. ThrowCompletionOr<String> to_show_calendar_option(GlobalObject& global_object, Object const& normalized_options)
  192. {
  193. auto& vm = global_object.vm();
  194. // 1. Return ? GetOption(normalizedOptions, "calendarName", « String », « "auto", "always", "never" », "auto").
  195. auto option = TRY(get_option(global_object, normalized_options, vm.names.calendarName, { OptionType::String }, { "auto"sv, "always"sv, "never"sv }, js_string(vm, "auto"sv)));
  196. VERIFY(option.is_string());
  197. return option.as_string().string();
  198. }
  199. // 13.14 ToTemporalRoundingIncrement ( normalizedOptions, dividend, inclusive ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalroundingincrement
  200. ThrowCompletionOr<u64> to_temporal_rounding_increment(GlobalObject& global_object, Object const& normalized_options, Optional<double> dividend, bool inclusive)
  201. {
  202. auto& vm = global_object.vm();
  203. double maximum;
  204. // 1. If dividend is undefined, then
  205. if (!dividend.has_value()) {
  206. // a. Let maximum be +∞.
  207. maximum = INFINITY;
  208. }
  209. // 2. Else if inclusive is true, then
  210. else if (inclusive) {
  211. // a. Let maximum be dividend.
  212. maximum = *dividend;
  213. }
  214. // 3. Else if dividend is more than 1, then
  215. else if (*dividend > 1) {
  216. // a. Let maximum be dividend − 1.
  217. maximum = *dividend - 1;
  218. }
  219. // 4. Else,
  220. else {
  221. // a. Let maximum be 1.
  222. maximum = 1;
  223. }
  224. // 5. Let increment be ? GetOption(normalizedOptions, "roundingIncrement", « Number », empty, 1).
  225. auto increment_value = TRY(get_option(global_object, normalized_options, vm.names.roundingIncrement, { OptionType::Number }, {}, Value(1)));
  226. VERIFY(increment_value.is_number());
  227. auto increment = increment_value.as_double();
  228. // 6. If increment < 1 or increment > maximum, throw a RangeError exception.
  229. if (increment < 1 || increment > maximum)
  230. return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, increment, "roundingIncrement");
  231. // 7. Set increment to floor(ℝ(increment)).
  232. auto floored_increment = static_cast<u64>(increment);
  233. // 8. If dividend is not undefined and dividend modulo increment is not zero, then
  234. if (dividend.has_value() && static_cast<u64>(*dividend) % floored_increment != 0)
  235. // a. Throw a RangeError exception.
  236. return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, increment, "roundingIncrement");
  237. // 9. Return increment.
  238. return floored_increment;
  239. }
  240. // 13.16 ToSecondsStringPrecision ( normalizedOptions ), https://tc39.es/proposal-temporal/#sec-temporal-tosecondsstringprecision
  241. ThrowCompletionOr<SecondsStringPrecision> to_seconds_string_precision(GlobalObject& global_object, Object const& normalized_options)
  242. {
  243. auto& vm = global_object.vm();
  244. // Let smallestUnit be ? ToSmallestTemporalUnit(normalizedOptions, « "year", "month", "week", "day", "hour" », undefined).
  245. auto smallest_unit = TRY(to_smallest_temporal_unit(global_object, normalized_options, { "year"sv, "month"sv, "week"sv, "day"sv, "hour"sv }, {}));
  246. // 2. If smallestUnit is "minute", then
  247. if (smallest_unit == "minute"sv) {
  248. // a. Return the Record { [[Precision]]: "minute", [[Unit]]: "minute", [[Increment]]: 1 }.
  249. return SecondsStringPrecision { .precision = "minute"sv, .unit = "minute"sv, .increment = 1 };
  250. }
  251. // 3. If smallestUnit is "second", then
  252. if (smallest_unit == "second"sv) {
  253. // a. Return the Record { [[Precision]]: 0, [[Unit]]: "second", [[Increment]]: 1 }.
  254. return SecondsStringPrecision { .precision = 0, .unit = "second"sv, .increment = 1 };
  255. }
  256. // 4. If smallestUnit is "millisecond", then
  257. if (smallest_unit == "millisecond"sv) {
  258. // a. Return the Record { [[Precision]]: 3, [[Unit]]: "millisecond", [[Increment]]: 1 }.
  259. return SecondsStringPrecision { .precision = 3, .unit = "millisecond"sv, .increment = 1 };
  260. }
  261. // 5. If smallestUnit is "microsecond", then
  262. if (smallest_unit == "microsecond"sv) {
  263. // a. Return the Record { [[Precision]]: 6, [[Unit]]: "microsecond", [[Increment]]: 1 }.
  264. return SecondsStringPrecision { .precision = 6, .unit = "microsecond"sv, .increment = 1 };
  265. }
  266. // 6. If smallestUnit is "nanosecond", then
  267. if (smallest_unit == "nanosecond"sv) {
  268. // a. Return the Record { [[Precision]]: 9, [[Unit]]: "nanosecond", [[Increment]]: 1 }.
  269. return SecondsStringPrecision { .precision = 9, .unit = "nanosecond"sv, .increment = 1 };
  270. }
  271. // 7. Assert: smallestUnit is undefined.
  272. VERIFY(!smallest_unit.has_value());
  273. // 8. Let digits be ? GetStringOrNumberOption(normalizedOptions, "fractionalSecondDigits", « "auto" », 0, 9, "auto").
  274. auto digits_variant = TRY(get_string_or_number_option<u8>(global_object, normalized_options, vm.names.fractionalSecondDigits, { "auto"sv }, 0, 9, js_string(vm, "auto"sv)));
  275. // 9. If digits is "auto", then
  276. if (digits_variant.has<String>()) {
  277. VERIFY(digits_variant.get<String>() == "auto"sv);
  278. // a. Return the Record { [[Precision]]: "auto", [[Unit]]: "nanosecond", [[Increment]]: 1 }.
  279. return SecondsStringPrecision { .precision = "auto"sv, .unit = "nanosecond"sv, .increment = 1 };
  280. }
  281. auto digits = digits_variant.get<u8>();
  282. // 10. If digits is 0, then
  283. if (digits == 0) {
  284. // a. Return the Record { [[Precision]]: 0, [[Unit]]: "second", [[Increment]]: 1 }.
  285. return SecondsStringPrecision { .precision = 0, .unit = "second"sv, .increment = 1 };
  286. }
  287. // 11. If digits is 1, 2, or 3, then
  288. if (digits == 1 || digits == 2 || digits == 3) {
  289. // a. Return the Record { [[Precision]]: digits, [[Unit]]: "millisecond", [[Increment]]: 10^(3 − digits) }.
  290. return SecondsStringPrecision { .precision = digits, .unit = "millisecond"sv, .increment = (u32)pow(10, 3 - digits) };
  291. }
  292. // 12. If digits is 4, 5, or 6, then
  293. if (digits == 4 || digits == 5 || digits == 6) {
  294. // a. Return the Record { [[Precision]]: digits, [[Unit]]: "microsecond", [[Increment]]: 10^(6 − digits) }.
  295. return SecondsStringPrecision { .precision = digits, .unit = "microsecond"sv, .increment = (u32)pow(10, 6 - digits) };
  296. }
  297. // 13. Assert: digits is 7, 8, or 9.
  298. VERIFY(digits == 7 || digits == 8 || digits == 9);
  299. // 14. Return the Record { [[Precision]]: digits, [[Unit]]: "nanosecond", [[Increment]]: 10^(9 − digits) }.
  300. return SecondsStringPrecision { .precision = digits, .unit = "nanosecond"sv, .increment = (u32)pow(10, 9 - digits) };
  301. }
  302. // https://tc39.es/proposal-temporal/#table-temporal-singular-and-plural-units
  303. static HashMap<StringView, StringView> plural_to_singular_units = {
  304. { "years"sv, "year"sv },
  305. { "months"sv, "month"sv },
  306. { "weeks"sv, "week"sv },
  307. { "days"sv, "day"sv },
  308. { "hours"sv, "hour"sv },
  309. { "minutes"sv, "minute"sv },
  310. { "seconds"sv, "second"sv },
  311. { "milliseconds"sv, "millisecond"sv },
  312. { "microseconds"sv, "microsecond"sv },
  313. { "nanoseconds"sv, "nanosecond"sv }
  314. };
  315. // 13.17 ToLargestTemporalUnit ( normalizedOptions, disallowedUnits, fallback [ , autoValue ] ), https://tc39.es/proposal-temporal/#sec-temporal-tolargesttemporalunit
  316. ThrowCompletionOr<String> to_largest_temporal_unit(GlobalObject& global_object, Object const& normalized_options, Vector<StringView> const& disallowed_units, String const& fallback, Optional<String> auto_value)
  317. {
  318. auto& vm = global_object.vm();
  319. // 1. Assert: disallowedUnits does not contain fallback.
  320. // 2. Assert: disallowedUnits does not contain "auto".
  321. // 3. Assert: autoValue is not present or fallback is "auto".
  322. VERIFY(!auto_value.has_value() || fallback == "auto"sv);
  323. // 4. Assert: autoValue is not present or disallowedUnits does not contain autoValue.
  324. // 5. Let largestUnit be ? GetOption(normalizedOptions, "largestUnit", « String », « "auto", "year", "years", "month", "months", "week", "weeks", "day", "days", "hour", "hours", "minute", "minutes", "second", "seconds", "millisecond", "milliseconds", "microsecond", "microseconds", "nanosecond", "nanoseconds" », fallback).
  325. auto largest_unit_value = TRY(get_option(global_object, normalized_options, vm.names.largestUnit, { OptionType::String }, { "auto"sv, "year"sv, "years"sv, "month"sv, "months"sv, "week"sv, "weeks"sv, "day"sv, "days"sv, "hour"sv, "hours"sv, "minute"sv, "minutes"sv, "second"sv, "seconds"sv, "millisecond"sv, "milliseconds"sv, "microsecond"sv, "microseconds"sv, "nanosecond"sv, "nanoseconds"sv }, js_string(vm, fallback)));
  326. auto largest_unit = largest_unit_value.as_string().string();
  327. // 6. If largestUnit is "auto" and autoValue is present, then
  328. if (largest_unit == "auto"sv && auto_value.has_value()) {
  329. // a. Return autoValue.
  330. return *auto_value;
  331. }
  332. // 7. If largestUnit is in the Plural column of Table 12, then
  333. if (auto singular_unit = plural_to_singular_units.get(largest_unit); singular_unit.has_value()) {
  334. // a. Set largestUnit to the corresponding Singular value of the same row.
  335. largest_unit = singular_unit.value();
  336. }
  337. // 8. If disallowedUnits contains largestUnit, then
  338. if (disallowed_units.contains_slow(largest_unit)) {
  339. // a. Throw a RangeError exception.
  340. return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, largest_unit, vm.names.largestUnit.as_string());
  341. }
  342. // 9. Return largestUnit.
  343. return largest_unit;
  344. }
  345. // 13.18 ToSmallestTemporalUnit ( normalizedOptions, disallowedUnits, fallback ), https://tc39.es/proposal-temporal/#sec-temporal-tosmallesttemporalunit
  346. ThrowCompletionOr<Optional<String>> to_smallest_temporal_unit(GlobalObject& global_object, Object const& normalized_options, Vector<StringView> const& disallowed_units, Optional<String> fallback)
  347. {
  348. auto& vm = global_object.vm();
  349. // 1. Assert: disallowedUnits does not contain fallback.
  350. // 2. Let smallestUnit be ? GetOption(normalizedOptions, "smallestUnit", « String », « "year", "years", "month", "months", "week", "weeks", "day", "days", "hour", "hours", "minute", "minutes", "second", "seconds", "millisecond", "milliseconds", "microsecond", "microseconds", "nanosecond", "nanoseconds" », fallback).
  351. auto smallest_unit_value = TRY(get_option(global_object, normalized_options, vm.names.smallestUnit, { OptionType::String }, { "year"sv, "years"sv, "month"sv, "months"sv, "week"sv, "weeks"sv, "day"sv, "days"sv, "hour"sv, "hours"sv, "minute"sv, "minutes"sv, "second"sv, "seconds"sv, "millisecond"sv, "milliseconds"sv, "microsecond"sv, "microseconds"sv, "nanosecond"sv, "nanoseconds"sv }, fallback.has_value() ? js_string(vm, *fallback) : js_undefined()));
  352. // OPTIMIZATION: We skip the following string-only checks for the fallback to tidy up the code a bit
  353. if (smallest_unit_value.is_undefined())
  354. return Optional<String> {};
  355. VERIFY(smallest_unit_value.is_string());
  356. auto smallest_unit = smallest_unit_value.as_string().string();
  357. // 3. If smallestUnit is in the Plural column of Table 12, then
  358. if (auto singular_unit = plural_to_singular_units.get(smallest_unit); singular_unit.has_value()) {
  359. // a. Set smallestUnit to the corresponding Singular value of the same row.
  360. smallest_unit = singular_unit.value();
  361. }
  362. // 4. If disallowedUnits contains smallestUnit, then
  363. if (disallowed_units.contains_slow(smallest_unit)) {
  364. // a. Throw a RangeError exception.
  365. return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, smallest_unit, vm.names.smallestUnit.as_string());
  366. }
  367. // 5. Return smallestUnit.
  368. return { smallest_unit };
  369. }
  370. // 13.22 ValidateTemporalUnitRange ( largestUnit, smallestUnit ), https://tc39.es/proposal-temporal/#sec-temporal-validatetemporalunitrange
  371. ThrowCompletionOr<void> validate_temporal_unit_range(GlobalObject& global_object, StringView largest_unit, StringView smallest_unit)
  372. {
  373. auto& vm = global_object.vm();
  374. // 1. If smallestUnit is "year" and largestUnit is not "year", then
  375. if (smallest_unit == "year"sv && largest_unit != "year"sv) {
  376. // a. Throw a RangeError exception.
  377. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
  378. }
  379. // 2. If smallestUnit is "month" and largestUnit is not "year" or "month", then
  380. if (smallest_unit == "month"sv && !largest_unit.is_one_of("year"sv, "month"sv)) {
  381. // a. Throw a RangeError exception.
  382. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
  383. }
  384. // 3. If smallestUnit is "week" and largestUnit is not one of "year", "month", or "week", then
  385. if (smallest_unit == "week"sv && !largest_unit.is_one_of("year"sv, "month"sv, "week"sv)) {
  386. // a. Throw a RangeError exception.
  387. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
  388. }
  389. // 4. If smallestUnit is "day" and largestUnit is not one of "year", "month", "week", or "day", then
  390. if (smallest_unit == "day"sv && !largest_unit.is_one_of("year"sv, "month"sv, "week"sv, "day"sv)) {
  391. // a. Throw a RangeError exception.
  392. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
  393. }
  394. // 5. If smallestUnit is "hour" and largestUnit is not one of "year", "month", "week", "day", or "hour", then
  395. if (smallest_unit == "hour"sv && !largest_unit.is_one_of("year"sv, "month"sv, "week"sv, "day"sv, "hour"sv)) {
  396. // a. Throw a RangeError exception.
  397. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
  398. }
  399. // 6. If smallestUnit is "minute" and largestUnit is "second", "millisecond", "microsecond", or "nanosecond", then
  400. if (smallest_unit == "minute"sv && largest_unit.is_one_of("second"sv, "millisecond"sv, "microsecond"sv, "nanosecond"sv)) {
  401. // a. Throw a RangeError exception.
  402. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
  403. }
  404. // 7. If smallestUnit is "second" and largestUnit is "millisecond", "microsecond", or "nanosecond", then
  405. if (smallest_unit == "second"sv && largest_unit.is_one_of("millisecond"sv, "microsecond"sv, "nanosecond"sv)) {
  406. // a. Throw a RangeError exception.
  407. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
  408. }
  409. // 8. If smallestUnit is "millisecond" and largestUnit is "microsecond" or "nanosecond", then
  410. if (smallest_unit == "millisecond"sv && largest_unit.is_one_of("microsecond"sv, "nanosecond"sv)) {
  411. // a. Throw a RangeError exception.
  412. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
  413. }
  414. // 9. If smallestUnit is "microsecond" and largestUnit is "nanosecond", then
  415. if (smallest_unit == "microsecond"sv && largest_unit == "nanosecond"sv) {
  416. // a. Throw a RangeError exception.
  417. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidUnitRange, smallest_unit, largest_unit);
  418. }
  419. return {};
  420. }
  421. // 13.23 LargerOfTwoTemporalUnits ( u1, u2 ), https://tc39.es/proposal-temporal/#sec-temporal-largeroftwotemporalunits
  422. String larger_of_two_temporal_units(StringView unit1, StringView unit2)
  423. {
  424. // 1. If either u1 or u2 is "year", return "year".
  425. if (unit1 == "year"sv || unit2 == "year"sv)
  426. return "year"sv;
  427. // 2. If either u1 or u2 is "month", return "month".
  428. if (unit1 == "month"sv || unit2 == "month"sv)
  429. return "month"sv;
  430. // 3. If either u1 or u2 is "week", return "week".
  431. if (unit1 == "week"sv || unit2 == "week"sv)
  432. return "week"sv;
  433. // 4. If either u1 or u2 is "day", return "day".
  434. if (unit1 == "day"sv || unit2 == "day"sv)
  435. return "day"sv;
  436. // 5. If either u1 or u2 is "hour", return "hour".
  437. if (unit1 == "hour"sv || unit2 == "hour"sv)
  438. return "hour"sv;
  439. // 6. If either u1 or u2 is "minute", return "minute".
  440. if (unit1 == "minute"sv || unit2 == "minute"sv)
  441. return "minute"sv;
  442. // 7. If either u1 or u2 is "second", return "second".
  443. if (unit1 == "second"sv || unit2 == "second"sv)
  444. return "second"sv;
  445. // 8. If either u1 or u2 is "millisecond", return "millisecond".
  446. if (unit1 == "millisecond"sv || unit2 == "millisecond"sv)
  447. return "millisecond"sv;
  448. // 9. If either u1 or u2 is "microsecond", return "microsecond".
  449. if (unit1 == "microsecond"sv || unit2 == "microsecond"sv)
  450. return "microsecond"sv;
  451. // 10. Return "nanosecond".
  452. return "nanosecond"sv;
  453. }
  454. // 13.25 MaximumTemporalDurationRoundingIncrement ( unit ), https://tc39.es/proposal-temporal/#sec-temporal-maximumtemporaldurationroundingincrement
  455. Optional<u16> maximum_temporal_duration_rounding_increment(StringView unit)
  456. {
  457. // 1. If unit is "year", "month", "week", or "day", then
  458. if (unit.is_one_of("year"sv, "month"sv, "week"sv, "day"sv)) {
  459. // a. Return undefined.
  460. return {};
  461. }
  462. // 2. If unit is "hour", then
  463. if (unit == "hour"sv) {
  464. // a. Return 24.
  465. return 24;
  466. }
  467. // 3. If unit is "minute" or "second", then
  468. if (unit.is_one_of("minute"sv, "second"sv)) {
  469. // a. Return 60.
  470. return 60;
  471. }
  472. // 4. Assert: unit is one of "millisecond", "microsecond", or "nanosecond".
  473. VERIFY(unit.is_one_of("millisecond"sv, "microsecond"sv, "nanosecond"sv));
  474. // 5. Return 1000.
  475. return 1000;
  476. }
  477. // 13.26 RejectTemporalCalendarType ( object ), https://tc39.es/proposal-temporal/#sec-temporal-rejecttemporalcalendartype
  478. ThrowCompletionOr<void> reject_temporal_calendar_type(GlobalObject& global_object, Object& object)
  479. {
  480. auto& vm = global_object.vm();
  481. // 1. Assert: Type(object) is Object.
  482. // 2. If object has an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]], [[InitializedTemporalMonthDay]], [[InitializedTemporalTime]], [[InitializedTemporalYearMonth]], or [[InitializedTemporalZonedDateTime]] internal slot, then
  483. if (is<PlainDate>(object) || is<PlainDateTime>(object) || is<PlainMonthDay>(object) || is<PlainTime>(object) || is<PlainYearMonth>(object) || is<ZonedDateTime>(object)) {
  484. // a. Throw a TypeError exception.
  485. return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalPlainTimeWithArgumentMustNotHave, "calendar or timeZone");
  486. }
  487. return {};
  488. }
  489. // 13.27 FormatSecondsStringPart ( second, millisecond, microsecond, nanosecond, precision ), https://tc39.es/proposal-temporal/#sec-temporal-formatsecondsstringpart
  490. String format_seconds_string_part(u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, Variant<StringView, u8> const& precision)
  491. {
  492. // 1. Assert: second, millisecond, microsecond and nanosecond are integers.
  493. // Non-standard sanity check
  494. if (precision.has<StringView>())
  495. VERIFY(precision.get<StringView>().is_one_of("minute"sv, "auto"sv));
  496. // 2. If precision is "minute", return "".
  497. if (precision.has<StringView>() && precision.get<StringView>() == "minute"sv)
  498. return String::empty();
  499. // 3. Let secondsString be the string-concatenation of the code unit 0x003A (COLON) and second formatted as a two-digit decimal number, padded to the left with zeroes if necessary.
  500. auto seconds_string = String::formatted(":{:02}", second);
  501. // 4. Let fraction be millisecond × 10^6 + microsecond × 10^3 + nanosecond.
  502. u32 fraction = millisecond * 1'000'000 + microsecond * 1'000 + nanosecond;
  503. String fraction_string;
  504. // 5. If precision is "auto", then
  505. if (precision.has<StringView>() && precision.get<StringView>() == "auto"sv) {
  506. // a. If fraction is 0, return secondsString.
  507. if (fraction == 0)
  508. return seconds_string;
  509. // b. Set fraction to fraction formatted as a nine-digit decimal number, padded to the left with zeroes if necessary.
  510. fraction_string = String::formatted("{:09}", fraction);
  511. // c. Set fraction to the longest possible substring of fraction starting at position 0 and not ending with the code unit 0x0030 (DIGIT ZERO).
  512. fraction_string = fraction_string.trim("0"sv, TrimMode::Right);
  513. }
  514. // 6. Else,
  515. else {
  516. // a. If precision is 0, return secondsString.
  517. if (precision.get<u8>() == 0)
  518. return seconds_string;
  519. // b. Set fraction to fraction formatted as a nine-digit decimal number, padded to the left with zeroes if necessary.
  520. fraction_string = String::formatted("{:09}", fraction);
  521. // c. Set fraction to the substring of fraction from 0 to precision.
  522. fraction_string = fraction_string.substring(0, precision.get<u8>());
  523. }
  524. // 7. Return the string-concatenation of secondsString, the code unit 0x002E (FULL STOP), and fraction.
  525. return String::formatted("{}.{}", seconds_string, fraction_string);
  526. }
  527. // 13.29 ConstrainToRange ( x, minimum, maximum ), https://tc39.es/proposal-temporal/#sec-temporal-constraintorange
  528. double constrain_to_range(double x, double minimum, double maximum)
  529. {
  530. return min(max(x, minimum), maximum);
  531. }
  532. // 13.32 RoundNumberToIncrement ( x, increment, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-roundnumbertoincrement
  533. BigInt* round_number_to_increment(GlobalObject& global_object, BigInt const& x, u64 increment, StringView rounding_mode)
  534. {
  535. auto& heap = global_object.heap();
  536. // 1. Assert: x and increment are mathematical values.
  537. // 2. Assert: roundingMode is "ceil", "floor", "trunc", or "halfExpand".
  538. VERIFY(rounding_mode == "ceil"sv || rounding_mode == "floor"sv || rounding_mode == "trunc"sv || rounding_mode == "halfExpand"sv);
  539. // OPTIMIZATION: If the increment is 1 the number is always rounded
  540. if (increment == 1)
  541. return js_bigint(heap, x.big_integer());
  542. auto increment_big_int = Crypto::UnsignedBigInteger::create_from(increment);
  543. // 3. Let quotient be x / increment.
  544. auto division_result = x.big_integer().divided_by(increment_big_int);
  545. // OPTIMIZATION: If theres no remainder there number is already rounded
  546. if (division_result.remainder == Crypto::UnsignedBigInteger { 0 })
  547. return js_bigint(heap, x.big_integer());
  548. Crypto::SignedBigInteger rounded = move(division_result.quotient);
  549. // 4. If roundingMode is "ceil", then
  550. if (rounding_mode == "ceil"sv) {
  551. // a. Let rounded be −floor(−quotient).
  552. if (!division_result.remainder.is_negative())
  553. rounded = rounded.plus(Crypto::UnsignedBigInteger { 1 });
  554. }
  555. // 5. Else if roundingMode is "floor", then
  556. else if (rounding_mode == "floor"sv) {
  557. // a. Let rounded be floor(quotient).
  558. if (division_result.remainder.is_negative())
  559. rounded = rounded.minus(Crypto::UnsignedBigInteger { 1 });
  560. }
  561. // 6. Else if roundingMode is "trunc", then
  562. else if (rounding_mode == "trunc"sv) {
  563. // a. Let rounded be the integral part of quotient, removing any fractional digits.
  564. // NOTE: This is a no-op
  565. }
  566. // 7. Else,
  567. else {
  568. // a. Let rounded be ! RoundHalfAwayFromZero(quotient).
  569. if (division_result.remainder.multiplied_by(Crypto::UnsignedBigInteger { 2 }).unsigned_value() >= increment_big_int) {
  570. if (division_result.remainder.is_negative())
  571. rounded = rounded.minus(Crypto::UnsignedBigInteger { 1 });
  572. else
  573. rounded = rounded.plus(Crypto::UnsignedBigInteger { 1 });
  574. }
  575. }
  576. // 8. Return rounded × increment.
  577. return js_bigint(heap, rounded.multiplied_by(increment_big_int));
  578. }
  579. // 13.34 ParseISODateTime ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parseisodatetime
  580. ThrowCompletionOr<ISODateTime> parse_iso_date_time(GlobalObject& global_object, [[maybe_unused]] String const& iso_string)
  581. {
  582. auto& vm = global_object.vm();
  583. // 1. Assert: Type(isoString) is String.
  584. // 2. Let year, month, day, hour, minute, second, fraction, and calendar be the parts of isoString produced respectively by the DateYear, DateMonth, DateDay, TimeHour, TimeMinute, TimeSecond, TimeFractionalPart, and CalendarName productions, or undefined if not present.
  585. Optional<StringView> year_part;
  586. Optional<StringView> month_part;
  587. Optional<StringView> day_part;
  588. Optional<StringView> hour_part;
  589. Optional<StringView> minute_part;
  590. Optional<StringView> second_part;
  591. Optional<StringView> fraction_part;
  592. Optional<StringView> calendar_part;
  593. TODO();
  594. // 3. Let year be the part of isoString produced by the DateYear production.
  595. // 4. If the first code unit of year is 0x2212 (MINUS SIGN), replace it with the code unit 0x002D (HYPHEN-MINUS).
  596. String normalized_year;
  597. if (year_part.has_value() && year_part->starts_with("\xE2\x88\x92"sv))
  598. normalized_year = String::formatted("-{}", year_part->substring_view(3));
  599. else
  600. normalized_year = year_part.value_or("");
  601. // 5. Set year to ! ToIntegerOrInfinity(year).
  602. i32 year = Value(js_string(vm, normalized_year)).to_integer_or_infinity(global_object);
  603. u8 month;
  604. // 6. If month is undefined, then
  605. if (!month_part.has_value()) {
  606. // a. Set month to 1.
  607. month = 1;
  608. }
  609. // 7. Else,
  610. else {
  611. // a. Set month to ! ToIntegerOrInfinity(month).
  612. month = *month_part->to_uint<u8>();
  613. }
  614. u8 day;
  615. // 8. If day is undefined, then
  616. if (!day_part.has_value()) {
  617. // a. Set day to 1.
  618. day = 1;
  619. }
  620. // 9. Else,
  621. else {
  622. // a. Set day to ! ToIntegerOrInfinity(day).
  623. day = *day_part->to_uint<u8>();
  624. }
  625. // 10. Set hour to ! ToIntegerOrInfinity(hour).
  626. u8 hour = hour_part->to_uint<u8>().value_or(0);
  627. // 11. Set minute to ! ToIntegerOrInfinity(minute).
  628. u8 minute = minute_part->to_uint<u8>().value_or(0);
  629. // 12. Set second to ! ToIntegerOrInfinity(second).
  630. u8 second = second_part->to_uint<u8>().value_or(0);
  631. // 13. If second is 60, then
  632. if (second == 60) {
  633. // a. Set second to 59.
  634. second = 59;
  635. }
  636. u16 millisecond;
  637. u16 microsecond;
  638. u16 nanosecond;
  639. // 14. If fraction is not undefined, then
  640. if (fraction_part.has_value()) {
  641. // a. Set fraction to the string-concatenation of the previous value of fraction and the string "000000000".
  642. auto fraction = String::formatted("{}000000000", *fraction_part);
  643. // b. Let millisecond be the String value equal to the substring of fraction from 0 to 3.
  644. // c. Set millisecond to ! ToIntegerOrInfinity(millisecond).
  645. millisecond = *fraction.substring(0, 3).to_uint<u16>();
  646. // d. Let microsecond be the String value equal to the substring of fraction from 3 to 6.
  647. // e. Set microsecond to ! ToIntegerOrInfinity(microsecond).
  648. microsecond = *fraction.substring(3, 3).to_uint<u16>();
  649. // f. Let nanosecond be the String value equal to the substring of fraction from 6 to 9.
  650. // g. Set nanosecond to ! ToIntegerOrInfinity(nanosecond).
  651. nanosecond = *fraction.substring(6, 3).to_uint<u16>();
  652. }
  653. // 15. Else,
  654. else {
  655. // a. Let millisecond be 0.
  656. millisecond = 0;
  657. // b. Let microsecond be 0.
  658. microsecond = 0;
  659. // c. Let nanosecond be 0.
  660. nanosecond = 0;
  661. }
  662. // 16. If ! IsValidISODate(year, month, day) is false, throw a RangeError exception.
  663. if (!is_valid_iso_date(year, month, day))
  664. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidISODate);
  665. // 17. If ! IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond) is false, throw a RangeError exception.
  666. if (!is_valid_time(hour, minute, second, millisecond, microsecond, nanosecond))
  667. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidTime);
  668. // 18. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day, [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond, [[Calendar]]: calendar }.
  669. return ISODateTime { .year = year, .month = month, .day = day, .hour = hour, .minute = minute, .second = second, .millisecond = millisecond, .microsecond = microsecond, .nanosecond = nanosecond, .calendar = calendar_part.has_value() ? *calendar_part : Optional<String>() };
  670. }
  671. // 13.35 ParseTemporalInstantString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalinstantstring
  672. ThrowCompletionOr<TemporalInstant> parse_temporal_instant_string(GlobalObject& global_object, String const& iso_string)
  673. {
  674. // 1. Assert: Type(isoString) is String.
  675. // 2. If isoString does not satisfy the syntax of a TemporalInstantString (see 13.33), then
  676. // a. Throw a RangeError exception.
  677. // TODO
  678. // 3. Let result be ! ParseISODateTime(isoString).
  679. auto result = parse_iso_date_time(global_object, iso_string).release_value();
  680. // 4. Let timeZoneResult be ? ParseTemporalTimeZoneString(isoString).
  681. auto time_zone_result = TRY(parse_temporal_time_zone_string(global_object, iso_string));
  682. // 5. Let offsetString be timeZoneResult.[[OffsetString]].
  683. auto offset_string = time_zone_result.offset;
  684. // 6. If timeZoneResult.[[Z]] is true, then
  685. if (time_zone_result.z) {
  686. // a. Set offsetString to "+00:00".
  687. offset_string = "+00:00"sv;
  688. }
  689. // 7. Assert: offsetString is not undefined.
  690. VERIFY(offset_string.has_value());
  691. // 8. Return the Record { [[Year]]: result.[[Year]], [[Month]]: result.[[Month]], [[Day]]: result.[[Day]], [[Hour]]: result.[[Hour]], [[Minute]]: result.[[Minute]], [[Second]]: result.[[Second]], [[Millisecond]]: result.[[Millisecond]], [[Microsecond]]: result.[[Microsecond]], [[Nanosecond]]: result.[[Nanosecond]], [[TimeZoneOffsetString]]: offsetString }.
  692. return TemporalInstant { .year = result.year, .month = result.month, .day = result.day, .hour = result.hour, .minute = result.minute, .second = result.second, .millisecond = result.millisecond, .microsecond = result.microsecond, .nanosecond = result.nanosecond, .time_zone_offset = move(offset_string) };
  693. }
  694. // 13.37 ParseTemporalCalendarString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalcalendarstring
  695. ThrowCompletionOr<String> parse_temporal_calendar_string(GlobalObject& global_object, [[maybe_unused]] String const& iso_string)
  696. {
  697. auto& vm = global_object.vm();
  698. // 1. Assert: Type(isoString) is String.
  699. // 2. If isoString does not satisfy the syntax of a TemporalCalendarString (see 13.33), then
  700. // a. Throw a RangeError exception.
  701. // 3. Let id be the part of isoString produced by the CalendarName production, or undefined if not present.
  702. Optional<StringView> id_part;
  703. TODO();
  704. // 4. If id is undefined, then
  705. if (!id_part.has_value()) {
  706. // a. Return "iso8601".
  707. return { "iso8601"sv };
  708. }
  709. // 5. If ! IsBuiltinCalendar(id) is false, then
  710. if (!is_builtin_calendar(*id_part)) {
  711. // a. Throw a RangeError exception.
  712. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendarIdentifier, *id_part);
  713. }
  714. // 6. Return id.
  715. return { id_part.value() };
  716. }
  717. // 13.38 ParseTemporalDateString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaldatestring
  718. ThrowCompletionOr<TemporalDate> parse_temporal_date_string(GlobalObject& global_object, String const& iso_string)
  719. {
  720. // 1. Assert: Type(isoString) is String.
  721. // 2. If isoString does not satisfy the syntax of a TemporalDateString (see 13.33), then
  722. // a. Throw a RangeError exception.
  723. // TODO
  724. // 3. Let result be ? ParseISODateTime(isoString).
  725. auto result = TRY(parse_iso_date_time(global_object, iso_string));
  726. // 4. Return the Record { [[Year]]: result.[[Year]], [[Month]]: result.[[Month]], [[Day]]: result.[[Day]], [[Calendar]]: result.[[Calendar]] }.
  727. return TemporalDate { .year = result.year, .month = result.month, .day = result.day, .calendar = move(result.calendar) };
  728. }
  729. // 13.39 ParseTemporalDateTimeString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaldatetimestring
  730. ThrowCompletionOr<ISODateTime> parse_temporal_date_time_string(GlobalObject& global_object, String const& iso_string)
  731. {
  732. // 1. Assert: Type(isoString) is String.
  733. // 2. If isoString does not satisfy the syntax of a TemporalDateTimeString (see 13.33), then
  734. // a. Throw a RangeError exception.
  735. // TODO
  736. // 3. Let result be ? ParseISODateTime(isoString).
  737. auto result = TRY(parse_iso_date_time(global_object, iso_string));
  738. // 4. Return result.
  739. return result;
  740. }
  741. // 13.40 ParseTemporalDurationString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaldurationstring
  742. ThrowCompletionOr<TemporalDuration> parse_temporal_duration_string(GlobalObject& global_object, String const& iso_string)
  743. {
  744. (void)global_object;
  745. (void)iso_string;
  746. TODO();
  747. }
  748. // 13.43 ParseTemporalTimeString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaltimestring
  749. ThrowCompletionOr<TemporalTime> parse_temporal_time_string(GlobalObject& global_object, [[maybe_unused]] String const& iso_string)
  750. {
  751. // 1. Assert: Type(isoString) is String.
  752. // 2. If isoString does not satisfy the syntax of a TemporalTimeString (see 13.33), then
  753. // a. Throw a RangeError exception.
  754. // TODO
  755. // 3. Let result be ? ParseISODateTime(isoString).
  756. auto result = TRY(parse_iso_date_time(global_object, iso_string));
  757. // 4. Return the Record { [[Hour]]: result.[[Hour]], [[Minute]]: result.[[Minute]], [[Second]]: result.[[Second]], [[Millisecond]]: result.[[Millisecond]], [[Microsecond]]: result.[[Microsecond]], [[Nanosecond]]: result.[[Nanosecond]], [[Calendar]]: result.[[Calendar]] }.
  758. return TemporalTime { .hour = result.hour, .minute = result.minute, .second = result.second, .millisecond = result.millisecond, .microsecond = result.microsecond, .nanosecond = result.nanosecond, .calendar = move(result.calendar) };
  759. }
  760. // 13.44 ParseTemporalTimeZoneString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaltimezonestring
  761. ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject& global_object, [[maybe_unused]] String const& iso_string)
  762. {
  763. auto& vm = global_object.vm();
  764. // 1. Assert: Type(isoString) is String.
  765. // 2. If isoString does not satisfy the syntax of a TemporalTimeZoneString (see 13.33), then
  766. // a. Throw a RangeError exception.
  767. // 3. Let z, sign, hours, minutes, seconds, fraction and name be the parts of isoString produced respectively by the UTCDesignator, TimeZoneUTCOffsetSign, TimeZoneUTCOffsetHour, TimeZoneUTCOffsetMinute, TimeZoneUTCOffsetSecond, TimeZoneUTCOffsetFraction, and TimeZoneIANAName productions, or undefined if not present.
  768. Optional<StringView> z_part;
  769. Optional<StringView> sign_part;
  770. Optional<StringView> hours_part;
  771. Optional<StringView> minutes_part;
  772. Optional<StringView> seconds_part;
  773. Optional<StringView> fraction_part;
  774. Optional<StringView> name_part;
  775. TODO();
  776. // 4. If z is not undefined, then
  777. if (z_part.has_value()) {
  778. // a. Return the Record { [[Z]]: true, [[OffsetString]]: undefined, [[Name]]: name }.
  779. return TemporalTimeZone { .z = true, .offset = {}, .name = name_part.has_value() ? String { *name_part } : Optional<String> {} };
  780. }
  781. Optional<String> offset;
  782. // 5. If hours is undefined, then
  783. if (!hours_part.has_value()) {
  784. // a. Let offsetString be undefined.
  785. // NOTE: No-op.
  786. }
  787. // 6. Else,
  788. else {
  789. // a. Assert: sign is not undefined.
  790. VERIFY(sign_part.has_value());
  791. // b. Set hours to ! ToIntegerOrInfinity(hours).
  792. u8 hours = Value(js_string(vm, *hours_part)).to_integer_or_infinity(global_object);
  793. u8 sign;
  794. // c. If sign is the code unit 0x002D (HYPHEN-MINUS) or the code unit 0x2212 (MINUS SIGN), then
  795. if (sign_part->is_one_of("-", "\u2212")) {
  796. // i. Set sign to −1.
  797. sign = -1;
  798. }
  799. // d. Else,
  800. else {
  801. // i. Set sign to 1.
  802. sign = 1;
  803. }
  804. // e. Set minutes to ! ToIntegerOrInfinity(minutes).
  805. u8 minutes = Value(js_string(vm, minutes_part.value_or(""sv))).to_integer_or_infinity(global_object);
  806. // f. Set seconds to ! ToIntegerOrInfinity(seconds).
  807. u8 seconds = Value(js_string(vm, seconds_part.value_or(""sv))).to_integer_or_infinity(global_object);
  808. i32 nanoseconds;
  809. // g. If fraction is not undefined, then
  810. if (fraction_part.has_value()) {
  811. // i. Set fraction to the string-concatenation of the previous value of fraction and the string "000000000".
  812. auto fraction = String::formatted("{}000000000", *fraction_part);
  813. // ii. Let nanoseconds be the String value equal to the substring of fraction from 0 to 9.
  814. // iii. Set nanoseconds to ! ToIntegerOrInfinity(nanoseconds).
  815. nanoseconds = Value(js_string(vm, fraction.substring(0, 9))).to_integer_or_infinity(global_object);
  816. }
  817. // h. Else,
  818. else {
  819. // i. Let nanoseconds be 0.
  820. nanoseconds = 0;
  821. }
  822. // i. Let offsetNanoseconds be sign × (((hours × 60 + minutes) × 60 + seconds) × 10^9 + nanoseconds).
  823. auto offset_nanoseconds = sign * (((hours * 60 + minutes) * 60 + seconds) * 1000000000 + nanoseconds);
  824. // j. Let offsetString be ! FormatTimeZoneOffsetString(offsetNanoseconds).
  825. offset = format_time_zone_offset_string(offset_nanoseconds);
  826. }
  827. Optional<String> name;
  828. // 7. If name is not undefined, then
  829. if (name_part.has_value()) {
  830. // a. If ! IsValidTimeZoneName(name) is false, throw a RangeError exception.
  831. if (!is_valid_time_zone_name(*name_part))
  832. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidTimeZoneName);
  833. // b. Set name to ! CanonicalizeTimeZoneName(name).
  834. name = canonicalize_time_zone_name(*name_part);
  835. }
  836. // 8. Return the Record { [[Z]]: false, [[OffsetString]]: offsetString, [[Name]]: name }.
  837. return TemporalTimeZone { .z = false, .offset = offset, .name = name };
  838. }
  839. // 13.45 ParseTemporalYearMonthString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalyearmonthstring
  840. ThrowCompletionOr<TemporalYearMonth> parse_temporal_year_month_string(GlobalObject& global_object, String const& iso_string)
  841. {
  842. // 1. Assert: Type(isoString) is String.
  843. // 2. If isoString does not satisfy the syntax of a TemporalYearMonthString (see 13.33), then
  844. // a. Throw a RangeError exception.
  845. // TODO
  846. // 3. Let result be ? ParseISODateTime(isoString).
  847. auto result = TRY(parse_iso_date_time(global_object, iso_string));
  848. // 4. Return the Record { [[Year]]: result.[[Year]], [[Month]]: result.[[Month]], [[Day]]: result.[[Day]], [[Calendar]]: result.[[Calendar]] }.
  849. return TemporalYearMonth { .year = result.year, .month = result.month, .day = result.day, .calendar = move(result.calendar) };
  850. }
  851. // 13.46 ToPositiveInteger ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-topositiveinteger
  852. ThrowCompletionOr<double> to_positive_integer(GlobalObject& global_object, Value argument)
  853. {
  854. auto& vm = global_object.vm();
  855. // 1. Let integer be ? ToIntegerThrowOnInfinity(argument).
  856. auto integer = TRY(to_integer_throw_on_infinity(global_object, argument, ErrorType::TemporalPropertyMustBePositiveInteger));
  857. // 2. If integer ≤ 0, then
  858. if (integer <= 0) {
  859. // a. Throw a RangeError exception.
  860. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalPropertyMustBePositiveInteger);
  861. }
  862. // 3. Return integer.
  863. return integer;
  864. }
  865. // 13.48 PrepareTemporalFields ( fields, fieldNames, requiredFields ), https://tc39.es/proposal-temporal/#sec-temporal-preparetemporalfields
  866. ThrowCompletionOr<Object*> prepare_temporal_fields(GlobalObject& global_object, Object const& fields, Vector<String> const& field_names, Vector<StringView> const& required_fields)
  867. {
  868. auto& vm = global_object.vm();
  869. // 1. Assert: Type(fields) is Object.
  870. // 2. Let result be ! OrdinaryObjectCreate(%Object.prototype%).
  871. auto* result = Object::create(global_object, global_object.object_prototype());
  872. VERIFY(result);
  873. // 3. For each value property of fieldNames, do
  874. for (auto& property : field_names) {
  875. // a. Let value be ? Get(fields, property).
  876. auto value = fields.get(property);
  877. if (auto* exception = vm.exception())
  878. return throw_completion(exception->value());
  879. // b. If value is undefined, then
  880. if (value.is_undefined()) {
  881. // i. If requiredFields contains property, then
  882. if (required_fields.contains_slow(property)) {
  883. // 1. Throw a TypeError exception.
  884. return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalMissingRequiredProperty, property);
  885. }
  886. // ii. If property is in the Property column of Table 13, then
  887. // NOTE: The other properties in the table are automatically handled as their default value is undefined
  888. if (property.is_one_of("hour", "minute", "second", "millisecond", "microsecond", "nanosecond")) {
  889. // 1. Set value to the corresponding Default value of the same row.
  890. value = Value(0);
  891. }
  892. }
  893. // c. Else,
  894. else {
  895. // i. If property is in the Property column of Table 13 and there is a Conversion value in the same row, then
  896. // 1. Let Conversion represent the abstract operation named by the Conversion value of the same row.
  897. // 2. Set value to ? Conversion(value).
  898. if (property.is_one_of("year", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond", "eraYear")) {
  899. value = Value(TRY(to_integer_throw_on_infinity(global_object, value, ErrorType::TemporalPropertyMustBeFinite)));
  900. } else if (property.is_one_of("month", "day")) {
  901. value = Value(TRY(to_positive_integer(global_object, value)));
  902. } else if (property.is_one_of("monthCode", "offset", "era")) {
  903. value = value.to_primitive_string(global_object);
  904. if (auto* exception = vm.exception())
  905. return throw_completion(exception->value());
  906. }
  907. }
  908. // d. Perform ! CreateDataPropertyOrThrow(result, property, value).
  909. result->create_data_property_or_throw(property, value);
  910. }
  911. // 4. Return result.
  912. return result;
  913. }
  914. }