Calendar.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. #include <LibJS/Runtime/AbstractOperations.h>
  8. #include <LibJS/Runtime/Array.h>
  9. #include <LibJS/Runtime/Completion.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  12. #include <LibJS/Runtime/Temporal/Calendar.h>
  13. #include <LibJS/Runtime/Temporal/CalendarConstructor.h>
  14. #include <LibJS/Runtime/Temporal/Duration.h>
  15. #include <LibJS/Runtime/Temporal/PlainDate.h>
  16. #include <LibJS/Runtime/Temporal/PlainDateTime.h>
  17. #include <LibJS/Runtime/Temporal/PlainMonthDay.h>
  18. #include <LibJS/Runtime/Temporal/PlainTime.h>
  19. #include <LibJS/Runtime/Temporal/PlainYearMonth.h>
  20. #include <LibJS/Runtime/Temporal/ZonedDateTime.h>
  21. #include <LibJS/Runtime/Value.h>
  22. #include <LibUnicode/Locale.h>
  23. namespace JS::Temporal {
  24. // 12 Temporal.Calendar Objects, https://tc39.es/proposal-temporal/#sec-temporal-calendar-objects
  25. Calendar::Calendar(String identifier, Object& prototype)
  26. : Object(prototype)
  27. , m_identifier(move(identifier))
  28. {
  29. }
  30. // 12.1.1 IsBuiltinCalendar ( id ), https://tc39.es/proposal-temporal/#sec-temporal-isbuiltincalendar
  31. bool is_builtin_calendar(String const& identifier)
  32. {
  33. // 1. Let calendars be AvailableCalendars().
  34. auto calendars = available_calendars();
  35. // 2. If calendars contains id, return true.
  36. if (calendars.contains_slow(identifier))
  37. return true;
  38. // 3. Return false.
  39. return false;
  40. }
  41. // 12.1.2 AvailableCalendars ( ), https://tc39.es/proposal-temporal/#sec-temporal-availablecalendars
  42. Span<StringView const> available_calendars()
  43. {
  44. // 1. Let calendars be the List of String values representing calendar types supported by the implementation.
  45. // NOTE: This can be removed in favor of using `Unicode::get_available_calendars()` once everything is updated to handle non-iso8601 calendars.
  46. static constexpr AK::Array calendars { "iso8601"sv };
  47. // 2. Assert: calendars contains "iso8601".
  48. // 3. Assert: calendars does not contain any element that does not identify a calendar type in the Unicode Common Locale Data Repository (CLDR).
  49. // 4. Sort calendars in order as if an Array of the same values had been sorted using %Array.prototype.sort% with undefined as comparefn.
  50. // 5. Return calendars.
  51. return calendars.span();
  52. }
  53. // 12.2.1 CreateTemporalCalendar ( identifier [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalcalendar
  54. ThrowCompletionOr<Calendar*> create_temporal_calendar(GlobalObject& global_object, String const& identifier, FunctionObject const* new_target)
  55. {
  56. // 1. Assert: IsBuiltinCalendar(identifier) is true.
  57. VERIFY(is_builtin_calendar(identifier));
  58. // 2. If newTarget is not provided, set newTarget to %Temporal.Calendar%.
  59. if (!new_target)
  60. new_target = global_object.temporal_calendar_constructor();
  61. // 3. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.Calendar.prototype%", « [[InitializedTemporalCalendar]], [[Identifier]] »).
  62. // 4. Set object.[[Identifier]] to identifier.
  63. auto* object = TRY(ordinary_create_from_constructor<Calendar>(global_object, *new_target, &GlobalObject::temporal_calendar_prototype, identifier));
  64. // 5. Return object.
  65. return object;
  66. }
  67. // 12.2.2 GetBuiltinCalendar ( id ), https://tc39.es/proposal-temporal/#sec-temporal-getbuiltincalendar
  68. ThrowCompletionOr<Calendar*> get_builtin_calendar(GlobalObject& global_object, String const& identifier)
  69. {
  70. auto& vm = global_object.vm();
  71. // 1. If IsBuiltinCalendar(id) is false, throw a RangeError exception.
  72. if (!is_builtin_calendar(identifier))
  73. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendarIdentifier, identifier);
  74. // 2. Return ! CreateTemporalCalendar(id).
  75. return MUST(create_temporal_calendar(global_object, identifier));
  76. }
  77. // 12.2.3 GetISO8601Calendar ( ), https://tc39.es/proposal-temporal/#sec-temporal-getiso8601calendar
  78. Calendar* get_iso8601_calendar(GlobalObject& global_object)
  79. {
  80. // 1. Return ! GetBuiltinCalendar("iso8601").
  81. return MUST(get_builtin_calendar(global_object, "iso8601"));
  82. }
  83. // 12.2.4 CalendarFields ( calendar, fieldNames ), https://tc39.es/proposal-temporal/#sec-temporal-calendarfields
  84. ThrowCompletionOr<Vector<String>> calendar_fields(GlobalObject& global_object, Object& calendar, Vector<StringView> const& field_names)
  85. {
  86. auto& vm = global_object.vm();
  87. // 1. Let fields be ? GetMethod(calendar, "fields").
  88. auto fields = TRY(Value(&calendar).get_method(global_object, vm.names.fields));
  89. // 2. Let fieldsArray be CreateArrayFromList(fieldNames).
  90. auto field_names_values = MarkedVector<Value> { vm.heap() };
  91. for (auto& field_name : field_names)
  92. field_names_values.append(js_string(vm, field_name));
  93. Value fields_array = Array::create_from(global_object, field_names_values);
  94. // 3. If fields is not undefined, then
  95. if (fields) {
  96. // a. Set fieldsArray to ? Call(fields, calendar, « fieldsArray »).
  97. fields_array = TRY(call(global_object, *fields, &calendar, fields_array));
  98. }
  99. // 4. Return ? IterableToListOfType(fieldsArray, « String »).
  100. auto list = TRY(iterable_to_list_of_type(global_object, fields_array, { OptionType::String }));
  101. Vector<String> result;
  102. for (auto& value : list)
  103. result.append(value.as_string().string());
  104. return result;
  105. }
  106. // 12.2.5 CalendarMergeFields ( calendar, fields, additionalFields ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmergefields
  107. ThrowCompletionOr<Object*> calendar_merge_fields(GlobalObject& global_object, Object& calendar, Object& fields, Object& additional_fields)
  108. {
  109. auto& vm = global_object.vm();
  110. // 1. Let mergeFields be ? GetMethod(calendar, "mergeFields").
  111. auto* merge_fields = TRY(Value(&calendar).get_method(global_object, vm.names.mergeFields));
  112. // 2. If mergeFields is undefined, then
  113. if (!merge_fields) {
  114. // a. Return ? DefaultMergeCalendarFields(fields, additionalFields).
  115. return TRY(default_merge_calendar_fields(global_object, fields, additional_fields));
  116. }
  117. // 3. Let result be ? Call(mergeFields, calendar, « fields, additionalFields »).
  118. auto result = TRY(call(global_object, merge_fields, &calendar, &fields, &additional_fields));
  119. // 4. If Type(result) is not Object, throw a TypeError exception.
  120. if (!result.is_object())
  121. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, result.to_string_without_side_effects());
  122. // 5. Return result.
  123. return &result.as_object();
  124. }
  125. // 12.2.6 CalendarDateAdd ( calendar, date, duration [ , options [ , dateAdd ] ] ), https://tc39.es/proposal-temporal/#sec-temporal-calendardateadd
  126. ThrowCompletionOr<PlainDate*> calendar_date_add(GlobalObject& global_object, Object& calendar, Value date, Duration& duration, Object* options, FunctionObject* date_add)
  127. {
  128. // NOTE: `date` is a `Value` because we sometimes need to pass a PlainDate, sometimes a PlainDateTime, and sometimes undefined.
  129. auto& vm = global_object.vm();
  130. // 1. Assert: Type(calendar) is Object.
  131. // 2. If options is not present, set options to undefined.
  132. // 3. Assert: Type(options) is Object or Undefined.
  133. // 4. If dateAdd is not present, set dateAdd to ? GetMethod(calendar, "dateAdd").
  134. if (!date_add)
  135. date_add = TRY(Value(&calendar).get_method(global_object, vm.names.dateAdd));
  136. // 5. Let addedDate be ? Call(dateAdd, calendar, « date, duration, options »).
  137. auto added_date = TRY(call(global_object, date_add ?: js_undefined(), &calendar, date, &duration, options ?: js_undefined()));
  138. // 6. Perform ? RequireInternalSlot(addedDate, [[InitializedTemporalDate]]).
  139. auto* added_date_object = TRY(added_date.to_object(global_object));
  140. if (!is<PlainDate>(added_date_object))
  141. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObjectOfType, "Temporal.PlainDate");
  142. // 7. Return addedDate.
  143. return static_cast<PlainDate*>(added_date_object);
  144. }
  145. // 12.2.7 CalendarDateUntil ( calendar, one, two, options [ , dateUntil ] ), https://tc39.es/proposal-temporal/#sec-temporal-calendardateuntil
  146. ThrowCompletionOr<Duration*> calendar_date_until(GlobalObject& global_object, Object& calendar, Value one, Value two, Object& options, FunctionObject* date_until)
  147. {
  148. auto& vm = global_object.vm();
  149. // 1. Assert: Type(calendar) is Object.
  150. // 2. If dateUntil is not present, set dateUntil to ? GetMethod(calendar, "dateUntil").
  151. if (!date_until)
  152. date_until = TRY(Value(&calendar).get_method(global_object, vm.names.dateUntil));
  153. // 3. Let duration be ? Call(dateUntil, calendar, « one, two, options »).
  154. auto duration = TRY(call(global_object, date_until ?: js_undefined(), &calendar, one, two, &options));
  155. // 4. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
  156. auto* duration_object = TRY(duration.to_object(global_object));
  157. if (!is<Duration>(duration_object))
  158. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObjectOfType, "Temporal.Duration");
  159. // 5. Return duration.
  160. return static_cast<Duration*>(duration_object);
  161. }
  162. // 12.2.8 CalendarYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendaryear
  163. ThrowCompletionOr<double> calendar_year(GlobalObject& global_object, Object& calendar, Object& date_like)
  164. {
  165. auto& vm = global_object.vm();
  166. // 1. Assert: Type(calendar) is Object.
  167. // 2. Let result be ? Invoke(calendar, "year", « dateLike »).
  168. auto result = TRY(Value(&calendar).invoke(global_object, vm.names.year, &date_like));
  169. // 3. If result is undefined, throw a RangeError exception.
  170. if (result.is_undefined())
  171. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.year.as_string(), vm.names.undefined.as_string());
  172. // 4. Return ? ToIntegerThrowOnInfinity(result).
  173. return TRY(to_integer_throw_on_infinity(global_object, result, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.year.as_string(), vm.names.Infinity.as_string()));
  174. }
  175. // 12.2.9 CalendarMonth ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmonth
  176. ThrowCompletionOr<double> calendar_month(GlobalObject& global_object, Object& calendar, Object& date_like)
  177. {
  178. auto& vm = global_object.vm();
  179. // 1. Assert: Type(calendar) is Object.
  180. // 2. Let result be ? Invoke(calendar, "month", « dateLike »).
  181. auto result = TRY(Value(&calendar).invoke(global_object, vm.names.month, &date_like));
  182. // NOTE: Explicitly handled for a better error message similar to the other calendar property AOs
  183. if (result.is_undefined())
  184. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.month.as_string(), vm.names.undefined.as_string());
  185. // 3. Return ? ToPositiveInteger(result).
  186. return TRY(to_positive_integer(global_object, result));
  187. }
  188. // 12.2.10 CalendarMonthCode ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmonthcode
  189. ThrowCompletionOr<String> calendar_month_code(GlobalObject& global_object, Object& calendar, Object& date_like)
  190. {
  191. auto& vm = global_object.vm();
  192. // 1. Assert: Type(calendar) is Object.
  193. // 2. Let result be ? Invoke(calendar, "monthCode", « dateLike »).
  194. auto result = TRY(Value(&calendar).invoke(global_object, vm.names.monthCode, &date_like));
  195. // 3. If result is undefined, throw a RangeError exception.
  196. if (result.is_undefined())
  197. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.monthCode.as_string(), vm.names.undefined.as_string());
  198. // 4. Return ? ToString(result).
  199. return result.to_string(global_object);
  200. }
  201. // 12.2.11 CalendarDay ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarday
  202. ThrowCompletionOr<double> calendar_day(GlobalObject& global_object, Object& calendar, Object& date_like)
  203. {
  204. auto& vm = global_object.vm();
  205. // 1. Assert: Type(calendar) is Object.
  206. // 2. Let result be ? Invoke(calendar, "day", « dateLike »).
  207. auto result = TRY(Value(&calendar).invoke(global_object, vm.names.day, &date_like));
  208. // NOTE: Explicitly handled for a better error message similar to the other calendar property AOs
  209. if (result.is_undefined())
  210. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.day.as_string(), vm.names.undefined.as_string());
  211. // 3. Return ? ToPositiveInteger(result).
  212. return TRY(to_positive_integer(global_object, result));
  213. }
  214. // 12.2.12 CalendarDayOfWeek ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendardayofweek
  215. ThrowCompletionOr<Value> calendar_day_of_week(GlobalObject& global_object, Object& calendar, Object& date_like)
  216. {
  217. auto& vm = global_object.vm();
  218. // 1. Assert: Type(calendar) is Object.
  219. // 2. Return ? Invoke(calendar, "dayOfWeek", « dateLike »).
  220. return TRY(Value(&calendar).invoke(global_object, vm.names.dayOfWeek, &date_like));
  221. }
  222. // 12.2.13 CalendarDayOfYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendardayofyear
  223. ThrowCompletionOr<Value> calendar_day_of_year(GlobalObject& global_object, Object& calendar, Object& date_like)
  224. {
  225. auto& vm = global_object.vm();
  226. // 1. Assert: Type(calendar) is Object.
  227. // 2. Return ? Invoke(calendar, "dayOfYear", « dateLike »).
  228. return TRY(Value(&calendar).invoke(global_object, vm.names.dayOfYear, &date_like));
  229. }
  230. // 12.2.14 CalendarWeekOfYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarweekofyear
  231. ThrowCompletionOr<Value> calendar_week_of_year(GlobalObject& global_object, Object& calendar, Object& date_like)
  232. {
  233. auto& vm = global_object.vm();
  234. // 1. Assert: Type(calendar) is Object.
  235. // 2. Return ? Invoke(calendar, "weekOfYear", « dateLike »).
  236. return TRY(Value(&calendar).invoke(global_object, vm.names.weekOfYear, &date_like));
  237. }
  238. // 12.2.14 CalendarDaysInWeek ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendardaysinweek
  239. ThrowCompletionOr<Value> calendar_days_in_week(GlobalObject& global_object, Object& calendar, Object& date_like)
  240. {
  241. auto& vm = global_object.vm();
  242. // 1. Assert: Type(calendar) is Object.
  243. // 2. Return ? Invoke(calendar, "daysInWeek", « dateLike »).
  244. return TRY(Value(&calendar).invoke(global_object, vm.names.daysInWeek, &date_like));
  245. }
  246. // 12.2.16 CalendarDaysInMonth ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendardaysinmonth
  247. ThrowCompletionOr<Value> calendar_days_in_month(GlobalObject& global_object, Object& calendar, Object& date_like)
  248. {
  249. auto& vm = global_object.vm();
  250. // 1. Assert: Type(calendar) is Object.
  251. // 2. Return ? Invoke(calendar, "daysInMonth", « dateLike »).
  252. return TRY(Value(&calendar).invoke(global_object, vm.names.daysInMonth, &date_like));
  253. }
  254. // 12.2.17 CalendarDaysInYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendardaysinyear
  255. ThrowCompletionOr<Value> calendar_days_in_year(GlobalObject& global_object, Object& calendar, Object& date_like)
  256. {
  257. auto& vm = global_object.vm();
  258. // 1. Assert: Type(calendar) is Object.
  259. // 2. Return ? Invoke(calendar, "daysInYear", « dateLike »).
  260. return TRY(Value(&calendar).invoke(global_object, vm.names.daysInYear, &date_like));
  261. }
  262. // 12.2.18 CalendarMonthsInYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmonthsinyear
  263. ThrowCompletionOr<Value> calendar_months_in_year(GlobalObject& global_object, Object& calendar, Object& date_like)
  264. {
  265. auto& vm = global_object.vm();
  266. // 1. Assert: Type(calendar) is Object.
  267. // 2. Return ? Invoke(calendar, "monthsInYear", « dateLike »).
  268. return TRY(Value(&calendar).invoke(global_object, vm.names.monthsInYear, &date_like));
  269. }
  270. // 12.2.29 CalendarInLeapYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarinleapyear
  271. ThrowCompletionOr<Value> calendar_in_leap_year(GlobalObject& global_object, Object& calendar, Object& date_like)
  272. {
  273. auto& vm = global_object.vm();
  274. // 1. Assert: Type(calendar) is Object.
  275. // 2. Return ? Invoke(calendar, "inLeapYear", « dateLike »).
  276. return TRY(Value(&calendar).invoke(global_object, vm.names.inLeapYear, &date_like));
  277. }
  278. // 15.6.1.1 CalendarEra ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarera
  279. ThrowCompletionOr<Value> calendar_era(GlobalObject& global_object, Object& calendar, Object& date_like)
  280. {
  281. auto& vm = global_object.vm();
  282. // 1. Assert: Type(calendar) is Object.
  283. // 2. Let result be ? Invoke(calendar, "era", « dateLike »).
  284. auto result = TRY(Value(&calendar).invoke(global_object, vm.names.era, &date_like));
  285. // 3. If result is not undefined, set result to ? ToString(result).
  286. if (!result.is_undefined())
  287. result = js_string(vm, TRY(result.to_string(global_object)));
  288. // 4. Return result.
  289. return result;
  290. }
  291. // 15.6.1.2 CalendarEraYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarerayear
  292. ThrowCompletionOr<Value> calendar_era_year(GlobalObject& global_object, Object& calendar, Object& date_like)
  293. {
  294. auto& vm = global_object.vm();
  295. // 1. Assert: Type(calendar) is Object.
  296. // 2. Let result be ? Invoke(calendar, "eraYear", « dateLike »).
  297. auto result = TRY(Value(&calendar).invoke(global_object, vm.names.eraYear, &date_like));
  298. // 3. If result is not undefined, set result to ? ToIntegerThrowOnInfinity(result).
  299. if (!result.is_undefined())
  300. result = Value(TRY(to_integer_throw_on_infinity(global_object, result, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.eraYear.as_string(), "Infinity"sv)));
  301. // 4. Return result.
  302. return result;
  303. }
  304. // 12.2.20 ToTemporalCalendar ( temporalCalendarLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalcalendar
  305. ThrowCompletionOr<Object*> to_temporal_calendar(GlobalObject& global_object, Value temporal_calendar_like)
  306. {
  307. auto& vm = global_object.vm();
  308. // 1. If Type(temporalCalendarLike) is Object, then
  309. if (temporal_calendar_like.is_object()) {
  310. auto& temporal_calendar_like_object = temporal_calendar_like.as_object();
  311. // a. If temporalCalendarLike has an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]], [[InitializedTemporalMonthDay]], [[InitializedTemporalTime]], [[InitializedTemporalYearMonth]], or [[InitializedTemporalZonedDateTime]] internal slot, then
  312. // i. Return temporalCalendarLike.[[Calendar]].
  313. if (is<PlainDate>(temporal_calendar_like_object))
  314. return &static_cast<PlainDate&>(temporal_calendar_like_object).calendar();
  315. if (is<PlainDateTime>(temporal_calendar_like_object))
  316. return &static_cast<PlainDateTime&>(temporal_calendar_like_object).calendar();
  317. if (is<PlainMonthDay>(temporal_calendar_like_object))
  318. return &static_cast<PlainMonthDay&>(temporal_calendar_like_object).calendar();
  319. if (is<PlainTime>(temporal_calendar_like_object))
  320. return &static_cast<PlainTime&>(temporal_calendar_like_object).calendar();
  321. if (is<PlainYearMonth>(temporal_calendar_like_object))
  322. return &static_cast<PlainYearMonth&>(temporal_calendar_like_object).calendar();
  323. if (is<ZonedDateTime>(temporal_calendar_like_object))
  324. return &static_cast<ZonedDateTime&>(temporal_calendar_like_object).calendar();
  325. // b. If ? HasProperty(temporalCalendarLike, "calendar") is false, return temporalCalendarLike.
  326. if (!TRY(temporal_calendar_like_object.has_property(vm.names.calendar)))
  327. return &temporal_calendar_like_object;
  328. // c. Set temporalCalendarLike to ? Get(temporalCalendarLike, "calendar").
  329. temporal_calendar_like = TRY(temporal_calendar_like_object.get(vm.names.calendar));
  330. // d. If Type(temporalCalendarLike) is Object and ? HasProperty(temporalCalendarLike, "calendar") is false, return temporalCalendarLike.
  331. if (temporal_calendar_like.is_object() && !TRY(temporal_calendar_like.as_object().has_property(vm.names.calendar)))
  332. return &temporal_calendar_like.as_object();
  333. }
  334. // 2. Let identifier be ? ToString(temporalCalendarLike).
  335. auto identifier = TRY(temporal_calendar_like.to_string(global_object));
  336. // 3. If IsBuiltinCalendar(identifier) is false, then
  337. if (!is_builtin_calendar(identifier)) {
  338. // a. Set identifier to ? ParseTemporalCalendarString(identifier).
  339. identifier = TRY(parse_temporal_calendar_string(global_object, identifier));
  340. // b. If IsBuiltinCalendar(identifier) is false, throw a RangeError exception.
  341. if (!is_builtin_calendar(identifier))
  342. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendarIdentifier, identifier);
  343. }
  344. // 4. Return ! CreateTemporalCalendar(identifier).
  345. return MUST(create_temporal_calendar(global_object, identifier));
  346. }
  347. // 12.2.21 ToTemporalCalendarWithISODefault ( temporalCalendarLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalcalendarwithisodefault
  348. ThrowCompletionOr<Object*> to_temporal_calendar_with_iso_default(GlobalObject& global_object, Value temporal_calendar_like)
  349. {
  350. // 1. If temporalCalendarLike is undefined, then
  351. if (temporal_calendar_like.is_undefined()) {
  352. // a. Return ! GetISO8601Calendar().
  353. return get_iso8601_calendar(global_object);
  354. }
  355. // 2. Return ? ToTemporalCalendar(temporalCalendarLike).
  356. return to_temporal_calendar(global_object, temporal_calendar_like);
  357. }
  358. // 12.2.22 GetTemporalCalendarWithISODefault ( item ), https://tc39.es/proposal-temporal/#sec-temporal-gettemporalcalendarwithisodefault
  359. ThrowCompletionOr<Object*> get_temporal_calendar_with_iso_default(GlobalObject& global_object, Object& item)
  360. {
  361. auto& vm = global_object.vm();
  362. // 1. If item has an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]], [[InitializedTemporalMonthDay]], [[InitializedTemporalTime]], [[InitializedTemporalYearMonth]], or [[InitializedTemporalZonedDateTime]] internal slot, then
  363. // a. Return item.[[Calendar]].
  364. if (is<PlainDate>(item))
  365. return &static_cast<PlainDate&>(item).calendar();
  366. if (is<PlainDateTime>(item))
  367. return &static_cast<PlainDateTime&>(item).calendar();
  368. if (is<PlainMonthDay>(item))
  369. return &static_cast<PlainMonthDay&>(item).calendar();
  370. if (is<PlainTime>(item))
  371. return &static_cast<PlainTime&>(item).calendar();
  372. if (is<PlainYearMonth>(item))
  373. return &static_cast<PlainYearMonth&>(item).calendar();
  374. if (is<ZonedDateTime>(item))
  375. return &static_cast<ZonedDateTime&>(item).calendar();
  376. // 2. Let calendarLike be ? Get(item, "calendar").
  377. auto calendar_like = TRY(item.get(vm.names.calendar));
  378. // 3. Return ? ToTemporalCalendarWithISODefault(calendarLike).
  379. return to_temporal_calendar_with_iso_default(global_object, calendar_like);
  380. }
  381. // 12.2.23 CalendarDateFromFields ( calendar, fields [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal-calendardatefromfields
  382. ThrowCompletionOr<PlainDate*> calendar_date_from_fields(GlobalObject& global_object, Object& calendar, Object const& fields, Object const* options)
  383. {
  384. auto& vm = global_object.vm();
  385. // 1. If options is not present, set options to undefined.
  386. // 2. Let date be ? Invoke(calendar, "dateFromFields", « fields, options »).
  387. auto date = TRY(Value(&calendar).invoke(global_object, vm.names.dateFromFields, &fields, options ?: js_undefined()));
  388. // 3. Perform ? RequireInternalSlot(date, [[InitializedTemporalDate]]).
  389. auto* date_object = TRY(date.to_object(global_object));
  390. if (!is<PlainDate>(date_object))
  391. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObjectOfType, "Temporal.PlainDate");
  392. // 4. Return date.
  393. return static_cast<PlainDate*>(date_object);
  394. }
  395. // 12.2.24 CalendarYearMonthFromFields ( calendar, fields [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal-calendaryearmonthfromfields
  396. ThrowCompletionOr<PlainYearMonth*> calendar_year_month_from_fields(GlobalObject& global_object, Object& calendar, Object const& fields, Object const* options)
  397. {
  398. auto& vm = global_object.vm();
  399. // 1. If options is not present, set options to undefined.
  400. // 2. Let yearMonth be ? Invoke(calendar, "yearMonthFromFields", « fields, options »).
  401. auto year_month = TRY(Value(&calendar).invoke(global_object, vm.names.yearMonthFromFields, &fields, options ?: js_undefined()));
  402. // 3. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
  403. auto* year_month_object = TRY(year_month.to_object(global_object));
  404. if (!is<PlainYearMonth>(year_month_object))
  405. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObjectOfType, "Temporal.PlainYearMonth");
  406. // 4. Return yearMonth.
  407. return static_cast<PlainYearMonth*>(year_month_object);
  408. }
  409. // 12.2.25 CalendarMonthDayFromFields ( calendar, fields [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmonthdayfromfields
  410. ThrowCompletionOr<PlainMonthDay*> calendar_month_day_from_fields(GlobalObject& global_object, Object& calendar, Object const& fields, Object const* options)
  411. {
  412. auto& vm = global_object.vm();
  413. // 1. If options is not present, set options to undefined.
  414. // 2. Let monthDay be ? Invoke(calendar, "monthDayFromFields", « fields, options »).
  415. auto month_day = TRY(Value(&calendar).invoke(global_object, vm.names.monthDayFromFields, &fields, options ?: js_undefined()));
  416. // 3. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
  417. auto* month_day_object = TRY(month_day.to_object(global_object));
  418. if (!is<PlainMonthDay>(month_day_object))
  419. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObjectOfType, "Temporal.PlainMonthDay");
  420. // 4. Return monthDay.
  421. return static_cast<PlainMonthDay*>(month_day_object);
  422. }
  423. // 12.2.26 FormatCalendarAnnotation ( id, showCalendar ), https://tc39.es/proposal-temporal/#sec-temporal-formatcalendarannotation
  424. String format_calendar_annotation(StringView id, StringView show_calendar)
  425. {
  426. // 1. Assert: showCalendar is "auto", "always", or "never".
  427. VERIFY(show_calendar == "auto"sv || show_calendar == "always"sv || show_calendar == "never"sv);
  428. // 2. If showCalendar is "never", return the empty String.
  429. if (show_calendar == "never"sv)
  430. return String::empty();
  431. // 3. If showCalendar is "auto" and id is "iso8601", return the empty String.
  432. if (show_calendar == "auto"sv && id == "iso8601"sv)
  433. return String::empty();
  434. // 4. Return the string-concatenation of "[u-ca=", id, and "]".
  435. return String::formatted("[u-ca={}]", id);
  436. }
  437. // 12.2.27 CalendarEquals ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal-calendarequals
  438. ThrowCompletionOr<bool> calendar_equals(GlobalObject& global_object, Object& one, Object& two)
  439. {
  440. // 1. If one and two are the same Object value, return true.
  441. if (&one == &two)
  442. return true;
  443. // 2. Let calendarOne be ? ToString(one).
  444. auto calendar_one = TRY(Value(&one).to_string(global_object));
  445. // 3. Let calendarTwo be ? ToString(two).
  446. auto calendar_two = TRY(Value(&two).to_string(global_object));
  447. // 4. If calendarOne is calendarTwo, return true.
  448. if (calendar_one == calendar_two)
  449. return true;
  450. // 5. Return false.
  451. return false;
  452. }
  453. // 12.2.28 ConsolidateCalendars ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal-consolidatecalendars
  454. ThrowCompletionOr<Object*> consolidate_calendars(GlobalObject& global_object, Object& one, Object& two)
  455. {
  456. auto& vm = global_object.vm();
  457. // 1. If one and two are the same Object value, return two.
  458. if (&one == &two)
  459. return &two;
  460. // 2. Let calendarOne be ? ToString(one).
  461. auto calendar_one = TRY(Value(&one).to_string(global_object));
  462. // 3. Let calendarTwo be ? ToString(two).
  463. auto calendar_two = TRY(Value(&two).to_string(global_object));
  464. // 4. If calendarOne is calendarTwo, return two.
  465. if (calendar_one == calendar_two)
  466. return &two;
  467. // 5. If calendarOne is "iso8601", return two.
  468. if (calendar_one == "iso8601"sv)
  469. return &two;
  470. // 6. If calendarTwo is "iso8601", return one.
  471. if (calendar_two == "iso8601"sv)
  472. return &one;
  473. // 7. Throw a RangeError exception.
  474. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendar);
  475. }
  476. // 12.2.29 ISODaysInMonth ( year, month ), https://tc39.es/proposal-temporal/#sec-temporal-isodaysinmonth
  477. u8 iso_days_in_month(i32 year, u8 month)
  478. {
  479. // 1. Assert: year is an integer.
  480. // 2. Assert: month is an integer, month ≥ 1, and month ≤ 12.
  481. VERIFY(month >= 1 && month <= 12);
  482. // 3. If month is 1, 3, 5, 7, 8, 10, or 12, return 31.
  483. if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
  484. return 31;
  485. // 4. If month is 4, 6, 9, or 11, return 30.
  486. if (month == 4 || month == 6 || month == 9 || month == 11)
  487. return 30;
  488. // 5. Return 28 + ℝ(InLeapYear(TimeFromYear(𝔽(year)))).
  489. return 28 + JS::in_leap_year(time_from_year(year));
  490. }
  491. // 12.2.30 ToISOWeekOfYear ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-toisoweekofyear
  492. u8 to_iso_week_of_year(i32 year, u8 month, u8 day)
  493. {
  494. // 1. Assert: year is an integer.
  495. // 2. Assert: month is an integer.
  496. // 3. Assert: day is an integer.
  497. // 4. Let date be the date given by year, month, and day.
  498. // 5. Return date's week number according to ISO-8601 as an integer.
  499. auto t = make_date(make_day(year, month - 1, day), 0);
  500. auto day_of_year = day_within_year(t) + 1;
  501. auto day_of_week = week_day(t);
  502. if (day_of_week == 0)
  503. day_of_week = 7;
  504. auto week = (day_of_year - day_of_week + 10) / 7;
  505. if (week < 1) {
  506. // NOTE: The resulting week is actually part of the previous year. If that year ends with a
  507. // Thursday (i.e. the first day of the given year is a Friday, or day 5), or the previous
  508. // year is a leap year and ends with a Friday (i.e. the first day of the given year is a
  509. // Saturday, or day 6), it has 53 weeks, and 52 weeks otherwise.
  510. auto day_of_jump = week_day(make_date(make_day(year, 0, 1), 0));
  511. if (day_of_jump == 5 || (in_leap_year(time_from_year(year - 1)) && day_of_jump == 6))
  512. return 53;
  513. else
  514. return 52;
  515. } else if (week == 53) {
  516. auto days_in_year = JS::days_in_year(year);
  517. if (days_in_year - day_of_year < 4 - day_of_week)
  518. return 1;
  519. }
  520. return week;
  521. }
  522. // 12.2.31 BuildISOMonthCode ( month ), https://tc39.es/proposal-temporal/#sec-buildisomonthcode
  523. String build_iso_month_code(u8 month)
  524. {
  525. // 1. Let numberPart be ToZeroPaddedDecimalString(month, 2).
  526. // 2. Return the string-concatenation of "M" and numberPart.
  527. return String::formatted("M{:02}", month);
  528. }
  529. // 12.2.32 ResolveISOMonth ( fields ), https://tc39.es/proposal-temporal/#sec-temporal-resolveisomonth
  530. ThrowCompletionOr<double> resolve_iso_month(GlobalObject& global_object, Object const& fields)
  531. {
  532. auto& vm = global_object.vm();
  533. // 1. Assert: fields is an ordinary object with no more and no less than the own data properties listed in Table 13.
  534. // 2. Let month be ! Get(fields, "month").
  535. auto month = MUST(fields.get(vm.names.month));
  536. // 3. Let monthCode be ! Get(fields, "monthCode").
  537. auto month_code = MUST(fields.get(vm.names.monthCode));
  538. // 4. If monthCode is undefined, then
  539. if (month_code.is_undefined()) {
  540. // a. If month is undefined, throw a TypeError exception.
  541. if (month.is_undefined())
  542. return vm.throw_completion<TypeError>(global_object, ErrorType::MissingRequiredProperty, vm.names.month.as_string());
  543. // b. Assert: Type(month) is Number.
  544. VERIFY(month.is_number());
  545. // c. Return ℝ(month).
  546. return month.as_double();
  547. }
  548. // 5. Assert: Type(monthCode) is String.
  549. VERIFY(month_code.is_string());
  550. auto& month_code_string = month_code.as_string().string();
  551. // 6. Let monthLength be the length of monthCode.
  552. auto month_length = month_code_string.length();
  553. // 7. If monthLength is not 3, throw a RangeError exception.
  554. if (month_length != 3)
  555. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidMonthCode);
  556. // 8. Let numberPart be the substring of monthCode from 1.
  557. auto number_part = month_code_string.substring(1);
  558. // 9. Set numberPart to ! ToIntegerOrInfinity(numberPart).
  559. auto number_part_integer = MUST(Value(js_string(vm, move(number_part))).to_integer_or_infinity(global_object));
  560. // 10. If numberPart < 1 or numberPart > 12, throw a RangeError exception.
  561. if (number_part_integer < 1 || number_part_integer > 12)
  562. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidMonthCode);
  563. // 11. If month is not undefined and month ≠ numberPart, then
  564. if (!month.is_undefined() && month.as_double() != number_part_integer) {
  565. // a. Throw a RangeError exception.
  566. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidMonthCode);
  567. }
  568. // 12. If SameValueNonNumeric(monthCode, ! BuildISOMonthCode(numberPart)) is false, then
  569. if (month_code_string != build_iso_month_code(number_part_integer)) {
  570. // a. Throw a RangeError exception.
  571. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidMonthCode);
  572. }
  573. // 13. Return numberPart.
  574. return number_part_integer;
  575. }
  576. // 12.2.33 ISODateFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isodatefromfields
  577. ThrowCompletionOr<ISODateRecord> iso_date_from_fields(GlobalObject& global_object, Object const& fields, Object const& options)
  578. {
  579. auto& vm = global_object.vm();
  580. // 1. Assert: Type(fields) is Object.
  581. // 2. Let overflow be ? ToTemporalOverflow(options).
  582. auto overflow = TRY(to_temporal_overflow(global_object, &options));
  583. // 3. Set fields to ? PrepareTemporalFields(fields, « "day", "month", "monthCode", "year" », « "year", "day" »).
  584. auto* prepared_fields = TRY(prepare_temporal_fields(global_object, fields, { "day", "month", "monthCode", "year" }, Vector<StringView> { "year"sv, "day"sv }));
  585. // 4. Let year be ! Get(fields, "year").
  586. auto year = MUST(prepared_fields->get(vm.names.year));
  587. // 5. Assert: Type(year) is Number.
  588. VERIFY(year.is_number());
  589. // 6. Let month be ? ResolveISOMonth(fields).
  590. auto month = TRY(resolve_iso_month(global_object, *prepared_fields));
  591. // 7. Let day be ! Get(fields, "day").
  592. auto day = MUST(prepared_fields->get(vm.names.day));
  593. // 8. Assert: Type(day) is Number.
  594. VERIFY(day.is_number());
  595. // 9. Return ? RegulateISODate(ℝ(year), month, ℝ(day), overflow).
  596. return regulate_iso_date(global_object, year.as_double(), month, day.as_double(), overflow);
  597. }
  598. // 12.2.34 ISOYearMonthFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isoyearmonthfromfields
  599. ThrowCompletionOr<ISOYearMonth> iso_year_month_from_fields(GlobalObject& global_object, Object const& fields, Object const& options)
  600. {
  601. auto& vm = global_object.vm();
  602. // 1. Assert: Type(fields) is Object.
  603. // 2. Let overflow be ? ToTemporalOverflow(options).
  604. auto overflow = TRY(to_temporal_overflow(global_object, &options));
  605. // 3. Set fields to ? PrepareTemporalFields(fields, « "month", "monthCode", "year" », « "year" »).
  606. auto* prepared_fields = TRY(prepare_temporal_fields(global_object, fields, { "month"sv, "monthCode"sv, "year"sv }, Vector<StringView> { "year"sv }));
  607. // 4. Let year be ! Get(fields, "year").
  608. auto year = MUST(prepared_fields->get(vm.names.year));
  609. // 5. Assert: Type(year) is Number.
  610. VERIFY(year.is_number());
  611. // 6. Let month be ? ResolveISOMonth(fields).
  612. auto month = TRY(resolve_iso_month(global_object, *prepared_fields));
  613. // 7. Let result be ? RegulateISOYearMonth(ℝ(year), month, overflow).
  614. auto result = TRY(regulate_iso_year_month(global_object, year.as_double(), month, overflow));
  615. // 8. Return the Record { [[Year]]: result.[[Year]], [[Month]]: result.[[Month]], [[ReferenceISODay]]: 1 }.
  616. return ISOYearMonth { .year = result.year, .month = result.month, .reference_iso_day = 1 };
  617. }
  618. // 12.2.35 ISOMonthDayFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthdayfromfields
  619. ThrowCompletionOr<ISOMonthDay> iso_month_day_from_fields(GlobalObject& global_object, Object const& fields, Object const& options)
  620. {
  621. auto& vm = global_object.vm();
  622. // 1. Assert: Type(fields) is Object.
  623. // 2. Let overflow be ? ToTemporalOverflow(options).
  624. auto overflow = TRY(to_temporal_overflow(global_object, &options));
  625. // 3. Set fields to ? PrepareTemporalFields(fields, « "day", "month", "monthCode", "year" », « "day" »).
  626. auto* prepared_fields = TRY(prepare_temporal_fields(global_object, fields, { "day"sv, "month"sv, "monthCode"sv, "year"sv }, Vector<StringView> { "day"sv }));
  627. // 4. Let month be ! Get(fields, "month").
  628. auto month_value = MUST(prepared_fields->get(vm.names.month));
  629. // 5. Let monthCode be ! Get(fields, "monthCode").
  630. auto month_code = MUST(prepared_fields->get(vm.names.monthCode));
  631. // 6. Let year be ! Get(fields, "year").
  632. auto year = MUST(prepared_fields->get(vm.names.year));
  633. // 7. If month is not undefined, and monthCode and year are both undefined, then
  634. if (!month_value.is_undefined() && month_code.is_undefined() && year.is_undefined()) {
  635. // a. Throw a TypeError exception.
  636. return vm.throw_completion<TypeError>(global_object, ErrorType::MissingRequiredProperty, "monthCode or year");
  637. }
  638. // 8. Set month to ? ResolveISOMonth(fields).
  639. auto month = TRY(resolve_iso_month(global_object, *prepared_fields));
  640. // 9. Let day be ! Get(fields, "day").
  641. auto day = MUST(prepared_fields->get(vm.names.day));
  642. // 10. Assert: Type(day) is Number.
  643. VERIFY(day.is_number());
  644. // 11. Let referenceISOYear be 1972 (the first leap year after the Unix epoch).
  645. i32 reference_iso_year = 1972;
  646. Optional<ISODateRecord> result;
  647. // 12. If monthCode is undefined, then
  648. if (month_code.is_undefined()) {
  649. // a. Assert: Type(year) is Number.
  650. VERIFY(year.is_number());
  651. // b. Let result be ? RegulateISODate(ℝ(year), month, ℝ(day), overflow).
  652. result = TRY(regulate_iso_date(global_object, year.as_double(), month, day.as_double(), overflow));
  653. }
  654. // 13. Else,
  655. else {
  656. // a. Let result be ? RegulateISODate(referenceISOYear, month, ℝ(day), overflow).
  657. result = TRY(regulate_iso_date(global_object, reference_iso_year, month, day.as_double(), overflow));
  658. }
  659. // 14. Return the Record { [[Month]]: result.[[Month]], [[Day]]: result.[[Day]], [[ReferenceISOYear]]: referenceISOYear }.
  660. return ISOMonthDay { .month = result->month, .day = result->day, .reference_iso_year = reference_iso_year };
  661. }
  662. // 12.2.36 ISOYear ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isoyear
  663. i32 iso_year(Object& temporal_object)
  664. {
  665. // 1. Assert: temporalObject has an [[ISOYear]] internal slot.
  666. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  667. // 2. Return 𝔽(temporalObject.[[ISOYear]]).
  668. if (is<PlainDate>(temporal_object))
  669. return static_cast<PlainDate&>(temporal_object).iso_year();
  670. if (is<PlainDateTime>(temporal_object))
  671. return static_cast<PlainDateTime&>(temporal_object).iso_year();
  672. if (is<PlainYearMonth>(temporal_object))
  673. return static_cast<PlainYearMonth&>(temporal_object).iso_year();
  674. if (is<PlainMonthDay>(temporal_object))
  675. return static_cast<PlainMonthDay&>(temporal_object).iso_year();
  676. VERIFY_NOT_REACHED();
  677. }
  678. // 12.2.37 ISOMonth ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonth
  679. u8 iso_month(Object& temporal_object)
  680. {
  681. // 1. Assert: temporalObject has an [[ISOMonth]] internal slot.
  682. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  683. // 2. Return 𝔽(temporalObject.[[ISOMonth]]).
  684. if (is<PlainDate>(temporal_object))
  685. return static_cast<PlainDate&>(temporal_object).iso_month();
  686. if (is<PlainDateTime>(temporal_object))
  687. return static_cast<PlainDateTime&>(temporal_object).iso_month();
  688. if (is<PlainYearMonth>(temporal_object))
  689. return static_cast<PlainYearMonth&>(temporal_object).iso_month();
  690. if (is<PlainMonthDay>(temporal_object))
  691. return static_cast<PlainMonthDay&>(temporal_object).iso_month();
  692. VERIFY_NOT_REACHED();
  693. }
  694. // 12.2.38 ISOMonthCode ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthcode
  695. String iso_month_code(Object& temporal_object)
  696. {
  697. // 1. Assert: temporalObject has an [[ISOMonth]] internal slot.
  698. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  699. // 2. Return ! BuildISOMonthCode(temporalObject.[[ISOMonth]]).
  700. if (is<PlainDate>(temporal_object))
  701. return build_iso_month_code(static_cast<PlainDate&>(temporal_object).iso_month());
  702. if (is<PlainDateTime>(temporal_object))
  703. return build_iso_month_code(static_cast<PlainDateTime&>(temporal_object).iso_month());
  704. if (is<PlainYearMonth>(temporal_object))
  705. return build_iso_month_code(static_cast<PlainYearMonth&>(temporal_object).iso_month());
  706. if (is<PlainMonthDay>(temporal_object))
  707. return build_iso_month_code(static_cast<PlainMonthDay&>(temporal_object).iso_month());
  708. VERIFY_NOT_REACHED();
  709. }
  710. // 12.2.49 ISODay ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthcode
  711. u8 iso_day(Object& temporal_object)
  712. {
  713. // 1. Assert: temporalObject has an [[ISODay]] internal slot.
  714. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  715. // 2. Return 𝔽(temporalObject.[[ISODay]]).
  716. if (is<PlainDate>(temporal_object))
  717. return static_cast<PlainDate&>(temporal_object).iso_day();
  718. if (is<PlainDateTime>(temporal_object))
  719. return static_cast<PlainDateTime&>(temporal_object).iso_day();
  720. if (is<PlainYearMonth>(temporal_object))
  721. return static_cast<PlainYearMonth&>(temporal_object).iso_day();
  722. if (is<PlainMonthDay>(temporal_object))
  723. return static_cast<PlainMonthDay&>(temporal_object).iso_day();
  724. VERIFY_NOT_REACHED();
  725. }
  726. // 12.2.40 DefaultMergeCalendarFields ( fields, additionalFields ), https://tc39.es/proposal-temporal/#sec-temporal-defaultmergecalendarfields
  727. ThrowCompletionOr<Object*> default_merge_calendar_fields(GlobalObject& global_object, Object const& fields, Object const& additional_fields)
  728. {
  729. auto& vm = global_object.vm();
  730. // 1. Let merged be OrdinaryObjectCreate(%Object.prototype%).
  731. auto* merged = Object::create(global_object, global_object.object_prototype());
  732. // 2. Let fieldsKeys be ? EnumerableOwnPropertyNames(fields, key).
  733. auto fields_keys = TRY(fields.enumerable_own_property_names(Object::PropertyKind::Key));
  734. // 3. For each element key of fieldsKeys, do
  735. for (auto& key : fields_keys) {
  736. // a. If key is not "month" or "monthCode", then
  737. if (key.as_string().string() != vm.names.month.as_string() && key.as_string().string() != vm.names.monthCode.as_string()) {
  738. auto property_key = MUST(PropertyKey::from_value(global_object, key));
  739. // i. Let propValue be ? Get(fields, key).
  740. auto prop_value = TRY(fields.get(property_key));
  741. // ii. If propValue is not undefined, then
  742. if (!prop_value.is_undefined()) {
  743. // 1. Perform ! CreateDataPropertyOrThrow(merged, key, propValue).
  744. MUST(merged->create_data_property_or_throw(property_key, prop_value));
  745. }
  746. }
  747. }
  748. // 4. Let additionalFieldsKeys be ? EnumerableOwnPropertyNames(additionalFields, key).
  749. auto additional_fields_keys = TRY(additional_fields.enumerable_own_property_names(Object::PropertyKind::Key));
  750. // IMPLEMENTATION DEFINED: This is an optimization, so we don't have to iterate new_keys three times (worst case), but only once.
  751. bool additional_fields_keys_contains_month_or_month_code_property = false;
  752. // 5. For each element key of additionalFieldsKeys, do
  753. for (auto& key : additional_fields_keys) {
  754. auto property_key = MUST(PropertyKey::from_value(global_object, key));
  755. // a. Let propValue be ? Get(additionalFields, key).
  756. auto prop_value = TRY(additional_fields.get(property_key));
  757. // b. If propValue is not undefined, then
  758. if (!prop_value.is_undefined()) {
  759. // i. Perform ! CreateDataPropertyOrThrow(merged, key, propValue).
  760. MUST(merged->create_data_property_or_throw(property_key, prop_value));
  761. }
  762. // See comment above.
  763. additional_fields_keys_contains_month_or_month_code_property |= key.as_string().string() == vm.names.month.as_string() || key.as_string().string() == vm.names.monthCode.as_string();
  764. }
  765. // 6. If additionalFieldsKeys does not contain either "month" or "monthCode", then
  766. if (!additional_fields_keys_contains_month_or_month_code_property) {
  767. // a. Let month be ? Get(fields, "month").
  768. auto month = TRY(fields.get(vm.names.month));
  769. // b. If month is not undefined, then
  770. if (!month.is_undefined()) {
  771. // i. Perform ! CreateDataPropertyOrThrow(merged, "month", month).
  772. MUST(merged->create_data_property_or_throw(vm.names.month, month));
  773. }
  774. // c. Let monthCode be ? Get(fields, "monthCode").
  775. auto month_code = TRY(fields.get(vm.names.monthCode));
  776. // d. If monthCode is not undefined, then
  777. if (!month_code.is_undefined()) {
  778. // i. Perform ! CreateDataPropertyOrThrow(merged, "monthCode", monthCode).
  779. MUST(merged->create_data_property_or_throw(vm.names.monthCode, month_code));
  780. }
  781. }
  782. // 7. Return merged.
  783. return merged;
  784. }
  785. }