Calendar.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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 <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. namespace JS::Temporal {
  23. // 12 Temporal.Calendar Objects, https://tc39.es/proposal-temporal/#sec-temporal-calendar-objects
  24. Calendar::Calendar(String identifier, Object& prototype)
  25. : Object(prototype)
  26. , m_identifier(move(identifier))
  27. {
  28. }
  29. // 12.1.1 CreateTemporalCalendar ( identifier [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalcalendar
  30. ThrowCompletionOr<Calendar*> create_temporal_calendar(GlobalObject& global_object, String const& identifier, FunctionObject const* new_target)
  31. {
  32. // 1. Assert: ! IsBuiltinCalendar(identifier) is true.
  33. VERIFY(is_builtin_calendar(identifier));
  34. // 2. If newTarget is not provided, set newTarget to %Temporal.Calendar%.
  35. if (!new_target)
  36. new_target = global_object.temporal_calendar_constructor();
  37. // 3. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.Calendar.prototype%", « [[InitializedTemporalCalendar]], [[Identifier]] »).
  38. // 4. Set object.[[Identifier]] to identifier.
  39. auto* object = TRY(ordinary_create_from_constructor<Calendar>(global_object, *new_target, &GlobalObject::temporal_calendar_prototype, identifier));
  40. // 5. Return object.
  41. return object;
  42. }
  43. // 12.1.2 IsBuiltinCalendar ( id ), https://tc39.es/proposal-temporal/#sec-temporal-isbuiltincalendar
  44. // NOTE: This is the minimum IsBuiltinCalendar implementation for engines without ECMA-402.
  45. bool is_builtin_calendar(String const& identifier)
  46. {
  47. // 1. If id is not "iso8601", return false.
  48. if (identifier != "iso8601"sv)
  49. return false;
  50. // 2. Return true.
  51. return true;
  52. }
  53. // 12.1.3 GetBuiltinCalendar ( id ), https://tc39.es/proposal-temporal/#sec-temporal-getbuiltincalendar
  54. ThrowCompletionOr<Calendar*> get_builtin_calendar(GlobalObject& global_object, String const& identifier)
  55. {
  56. auto& vm = global_object.vm();
  57. // 1. If ! IsBuiltinCalendar(id) is false, throw a RangeError exception.
  58. if (!is_builtin_calendar(identifier))
  59. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendarIdentifier, identifier);
  60. // 2. Return ? Construct(%Temporal.Calendar%, « id »).
  61. MarkedValueList arguments(vm.heap());
  62. arguments.append(js_string(vm, identifier));
  63. return static_cast<Calendar*>(TRY(construct(global_object, *global_object.temporal_calendar_constructor(), move(arguments))));
  64. }
  65. // 12.1.4 GetISO8601Calendar ( )
  66. Calendar* get_iso8601_calendar(GlobalObject& global_object)
  67. {
  68. // 1. Return ! GetBuiltinCalendar("iso8601").
  69. return MUST(get_builtin_calendar(global_object, "iso8601"));
  70. }
  71. // 12.1.5 CalendarFields ( calendar, fieldNames ), https://tc39.es/proposal-temporal/#sec-temporal-calendarfields
  72. ThrowCompletionOr<Vector<String>> calendar_fields(GlobalObject& global_object, Object& calendar, Vector<StringView> const& field_names)
  73. {
  74. auto& vm = global_object.vm();
  75. // 1. Let fields be ? GetMethod(calendar, "fields").
  76. auto fields = TRY(Value(&calendar).get_method(global_object, vm.names.fields));
  77. // 2. Let fieldsArray be ! CreateArrayFromList(fieldNames).
  78. auto field_names_values = MarkedValueList { vm.heap() };
  79. for (auto& field_name : field_names)
  80. field_names_values.append(js_string(vm, field_name));
  81. Value fields_array = Array::create_from(global_object, field_names_values);
  82. // 3. If fields is not undefined, then
  83. if (fields) {
  84. // a. Set fieldsArray to ? Call(fields, calendar, « fieldsArray »).
  85. fields_array = TRY(vm.call(*fields, &calendar, fields_array));
  86. }
  87. // 4. Return ? IterableToListOfType(fieldsArray, « String »).
  88. auto list = TRY(iterable_to_list_of_type(global_object, fields_array, { OptionType::String }));
  89. Vector<String> result;
  90. for (auto& value : list)
  91. result.append(value.as_string().string());
  92. return result;
  93. }
  94. // 12.1.6 CalendarMergeFields ( calendar, fields, additionalFields ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmergefields
  95. ThrowCompletionOr<Object*> calendar_merge_fields(GlobalObject& global_object, Object& calendar, Object& fields, Object& additional_fields)
  96. {
  97. auto& vm = global_object.vm();
  98. // 1. Let mergeFields be ? GetMethod(calendar, "mergeFields").
  99. auto* merge_fields = TRY(Value(&calendar).get_method(global_object, vm.names.mergeFields));
  100. // 2. If mergeFields is undefined, then
  101. if (!merge_fields) {
  102. // a. Return ? DefaultMergeFields(fields, additionalFields).
  103. return TRY(default_merge_fields(global_object, fields, additional_fields));
  104. }
  105. // 3. Let result be ? Call(mergeFields, calendar, « fields, additionalFields »).
  106. auto result = TRY(call(global_object, merge_fields, &calendar, &fields, &additional_fields));
  107. // 4. If Type(result) is not Object, throw a TypeError exception.
  108. if (!result.is_object())
  109. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, result.to_string_without_side_effects());
  110. // 5. Return result.
  111. return &result.as_object();
  112. }
  113. // 12.1.7 CalendarDateAdd ( calendar, date, duration, options [ , dateAdd ] ), https://tc39.es/proposal-temporal/#sec-temporal-calendardateadd
  114. ThrowCompletionOr<PlainDate*> calendar_date_add(GlobalObject& global_object, Object& calendar, PlainDate& date, Duration& duration, Object* options, FunctionObject* date_add)
  115. {
  116. auto& vm = global_object.vm();
  117. // 1. Assert: Type(calendar) is Object.
  118. // 2. If dateAdd is not present, set dateAdd to ? GetMethod(calendar, "dateAdd").
  119. if (!date_add)
  120. date_add = TRY(Value(&calendar).get_method(global_object, vm.names.dateAdd));
  121. // 3. Let addedDate be ? Call(dateAdd, calendar, « date, duration, options »).
  122. auto added_date = TRY(call(global_object, date_add ?: js_undefined(), &calendar, &date, &duration, options ?: js_undefined()));
  123. // 4. Perform ? RequireInternalSlot(addedDate, [[InitializedTemporalDate]]).
  124. auto* added_date_object = TRY(added_date.to_object(global_object));
  125. if (!is<PlainDate>(added_date_object))
  126. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObjectOfType, "Temporal.PlainDate");
  127. // 5. Return addedDate.
  128. return static_cast<PlainDate*>(added_date_object);
  129. }
  130. // 12.1.9 CalendarYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendaryear
  131. ThrowCompletionOr<double> calendar_year(GlobalObject& global_object, Object& calendar, Object& date_like)
  132. {
  133. auto& vm = global_object.vm();
  134. // 1. Assert: Type(calendar) is Object.
  135. // 2. Let result be ? Invoke(calendar, "year", « dateLike »).
  136. auto result = TRY(Value(&calendar).invoke(global_object, vm.names.year, &date_like));
  137. // 3. If result is undefined, throw a RangeError exception.
  138. if (result.is_undefined())
  139. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.year.as_string(), vm.names.undefined.as_string());
  140. // 4. Return ? ToIntegerThrowOnInfinity(result).
  141. return TRY(to_integer_throw_on_infinity(global_object, result, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.year.as_string(), vm.names.Infinity.as_string()));
  142. }
  143. // 12.1.10 CalendarMonth ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmonth
  144. ThrowCompletionOr<double> calendar_month(GlobalObject& global_object, Object& calendar, Object& date_like)
  145. {
  146. auto& vm = global_object.vm();
  147. // 1. Assert: Type(calendar) is Object.
  148. // 2. Let result be ? Invoke(calendar, "month", « dateLike »).
  149. auto result = TRY(Value(&calendar).invoke(global_object, vm.names.month, &date_like));
  150. // 3. If result is undefined, throw a RangeError exception.
  151. if (result.is_undefined())
  152. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.month.as_string(), vm.names.undefined.as_string());
  153. // 4. Return ? ToPositiveInteger(result).
  154. return TRY(to_positive_integer(global_object, result));
  155. }
  156. // 12.1.11 CalendarMonthCode ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmonthcode
  157. ThrowCompletionOr<String> calendar_month_code(GlobalObject& global_object, Object& calendar, Object& date_like)
  158. {
  159. auto& vm = global_object.vm();
  160. // 1. Assert: Type(calendar) is Object.
  161. // 2. Let result be ? Invoke(calendar, "monthCode", « dateLike »).
  162. auto result = TRY(Value(&calendar).invoke(global_object, vm.names.monthCode, &date_like));
  163. // 3. If result is undefined, throw a RangeError exception.
  164. if (result.is_undefined())
  165. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.monthCode.as_string(), vm.names.undefined.as_string());
  166. // 4. Return ? ToString(result).
  167. return result.to_string(global_object);
  168. }
  169. // 12.1.12 CalendarDay ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarday
  170. ThrowCompletionOr<double> calendar_day(GlobalObject& global_object, Object& calendar, Object& date_like)
  171. {
  172. auto& vm = global_object.vm();
  173. // 1. Assert: Type(calendar) is Object.
  174. // 2. Let result be ? Invoke(calendar, "day", « dateLike »).
  175. auto result = TRY(Value(&calendar).invoke(global_object, vm.names.day, &date_like));
  176. // 3. If result is undefined, throw a RangeError exception.
  177. if (result.is_undefined())
  178. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.day.as_string(), vm.names.undefined.as_string());
  179. // 4. Return ? ToPositiveInteger(result).
  180. return TRY(to_positive_integer(global_object, result));
  181. }
  182. // 12.1.13 CalendarDayOfWeek ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendardayofweek
  183. ThrowCompletionOr<Value> calendar_day_of_week(GlobalObject& global_object, Object& calendar, Object& date_like)
  184. {
  185. auto& vm = global_object.vm();
  186. // 1. Assert: Type(calendar) is Object.
  187. // 2. Return ? Invoke(calendar, "dayOfWeek", « dateLike »).
  188. return TRY(Value(&calendar).invoke(global_object, vm.names.dayOfWeek, &date_like));
  189. }
  190. // 12.1.14 CalendarDayOfYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendardayofyear
  191. ThrowCompletionOr<Value> calendar_day_of_year(GlobalObject& global_object, Object& calendar, Object& date_like)
  192. {
  193. auto& vm = global_object.vm();
  194. // 1. Assert: Type(calendar) is Object.
  195. // 2. Return ? Invoke(calendar, "dayOfYear", « dateLike »).
  196. return TRY(Value(&calendar).invoke(global_object, vm.names.dayOfYear, &date_like));
  197. }
  198. // 12.1.15 CalendarWeekOfYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarweekofyear
  199. ThrowCompletionOr<Value> calendar_week_of_year(GlobalObject& global_object, Object& calendar, Object& date_like)
  200. {
  201. auto& vm = global_object.vm();
  202. // 1. Assert: Type(calendar) is Object.
  203. // 2. Return ? Invoke(calendar, "weekOfYear", « dateLike »).
  204. return TRY(Value(&calendar).invoke(global_object, vm.names.weekOfYear, &date_like));
  205. }
  206. // 12.1.16 CalendarDaysInWeek ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendardaysinweek
  207. ThrowCompletionOr<Value> calendar_days_in_week(GlobalObject& global_object, Object& calendar, Object& date_like)
  208. {
  209. auto& vm = global_object.vm();
  210. // 1. Assert: Type(calendar) is Object.
  211. // 2. Return ? Invoke(calendar, "daysInWeek", « dateLike »).
  212. return TRY(Value(&calendar).invoke(global_object, vm.names.daysInWeek, &date_like));
  213. }
  214. // 12.1.17 CalendarDaysInMonth ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendardaysinmonth
  215. ThrowCompletionOr<Value> calendar_days_in_month(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, "daysInMonth", « dateLike »).
  220. return TRY(Value(&calendar).invoke(global_object, vm.names.daysInMonth, &date_like));
  221. }
  222. // 12.1.18 CalendarDaysInYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendardaysinyear
  223. ThrowCompletionOr<Value> calendar_days_in_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, "daysInYear", « dateLike »).
  228. return TRY(Value(&calendar).invoke(global_object, vm.names.daysInYear, &date_like));
  229. }
  230. // 12.1.19 CalendarMonthsInYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmonthsinyear
  231. ThrowCompletionOr<Value> calendar_months_in_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, "monthsInYear", « dateLike »).
  236. return TRY(Value(&calendar).invoke(global_object, vm.names.monthsInYear, &date_like));
  237. }
  238. // 12.1.20 CalendarInLeapYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarinleapyear
  239. ThrowCompletionOr<Value> calendar_in_leap_year(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, "inLeapYear", « dateLike »).
  244. return TRY(Value(&calendar).invoke(global_object, vm.names.inLeapYear, &date_like));
  245. }
  246. // 15.6.1.2 CalendarEra ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarera
  247. ThrowCompletionOr<Value> calendar_era(GlobalObject& global_object, Object& calendar, Object& date_like)
  248. {
  249. auto& vm = global_object.vm();
  250. // 1. Assert: Type(calendar) is Object.
  251. // 2. Let result be ? Invoke(calendar, "era", « dateLike »).
  252. auto result = TRY(Value(&calendar).invoke(global_object, vm.names.era, &date_like));
  253. // 3. If result is not undefined, set result to ? ToString(result).
  254. if (!result.is_undefined())
  255. result = js_string(vm, TRY(result.to_string(global_object)));
  256. // 4. Return result.
  257. return result;
  258. }
  259. // 15.6.1.3 CalendarEraYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarerayear
  260. ThrowCompletionOr<Value> calendar_era_year(GlobalObject& global_object, Object& calendar, Object& date_like)
  261. {
  262. auto& vm = global_object.vm();
  263. // 1. Assert: Type(calendar) is Object.
  264. // 2. Let result be ? Invoke(calendar, "eraYear", « dateLike »).
  265. auto result = TRY(Value(&calendar).invoke(global_object, vm.names.eraYear, &date_like));
  266. // 3. If result is not undefined, set result to ? ToIntegerThrowOnInfinity(result).
  267. if (!result.is_undefined())
  268. result = Value(TRY(to_integer_throw_on_infinity(global_object, result, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.eraYear.as_string(), "Infinity"sv)));
  269. // 4. Return result.
  270. return result;
  271. }
  272. // 12.1.21 ToTemporalCalendar ( temporalCalendarLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalcalendar
  273. ThrowCompletionOr<Object*> to_temporal_calendar(GlobalObject& global_object, Value temporal_calendar_like)
  274. {
  275. auto& vm = global_object.vm();
  276. // 1. If Type(temporalCalendarLike) is Object, then
  277. if (temporal_calendar_like.is_object()) {
  278. auto& temporal_calendar_like_object = temporal_calendar_like.as_object();
  279. // a. If temporalCalendarLike has an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]], [[InitializedTemporalMonthDay]], [[InitializedTemporalTime]], [[InitializedTemporalYearMonth]], or [[InitializedTemporalZonedDateTime]] internal slot, then
  280. // i. Return temporalCalendarLike.[[Calendar]].
  281. if (is<PlainDate>(temporal_calendar_like_object))
  282. return &static_cast<PlainDate&>(temporal_calendar_like_object).calendar();
  283. if (is<PlainDateTime>(temporal_calendar_like_object))
  284. return &static_cast<PlainDateTime&>(temporal_calendar_like_object).calendar();
  285. if (is<PlainMonthDay>(temporal_calendar_like_object))
  286. return &static_cast<PlainMonthDay&>(temporal_calendar_like_object).calendar();
  287. if (is<PlainTime>(temporal_calendar_like_object))
  288. return &static_cast<PlainTime&>(temporal_calendar_like_object).calendar();
  289. if (is<PlainYearMonth>(temporal_calendar_like_object))
  290. return &static_cast<PlainYearMonth&>(temporal_calendar_like_object).calendar();
  291. if (is<ZonedDateTime>(temporal_calendar_like_object))
  292. return &static_cast<ZonedDateTime&>(temporal_calendar_like_object).calendar();
  293. // b. If ? HasProperty(temporalCalendarLike, "calendar") is false, return temporalCalendarLike.
  294. if (!TRY(temporal_calendar_like_object.has_property(vm.names.calendar)))
  295. return &temporal_calendar_like_object;
  296. // c. Set temporalCalendarLike to ? Get(temporalCalendarLike, "calendar").
  297. temporal_calendar_like = TRY(temporal_calendar_like_object.get(vm.names.calendar));
  298. // d. If Type(temporalCalendarLike) is Object and ? HasProperty(temporalCalendarLike, "calendar") is false, return temporalCalendarLike.
  299. if (temporal_calendar_like.is_object() && !TRY(temporal_calendar_like.as_object().has_property(vm.names.calendar)))
  300. return &temporal_calendar_like.as_object();
  301. }
  302. // 2. Let identifier be ? ToString(temporalCalendarLike).
  303. auto identifier = TRY(temporal_calendar_like.to_string(global_object));
  304. // 3. If ! IsBuiltinCalendar(identifier) is false, then
  305. if (!is_builtin_calendar(identifier)) {
  306. // a. Let identifier be ? ParseTemporalCalendarString(identifier).
  307. identifier = TRY(parse_temporal_calendar_string(global_object, identifier));
  308. }
  309. // 4. Return ! CreateTemporalCalendar(identifier).
  310. return MUST(create_temporal_calendar(global_object, identifier));
  311. }
  312. // 12.1.22 ToTemporalCalendarWithISODefault ( temporalCalendarLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalcalendarwithisodefault
  313. ThrowCompletionOr<Object*> to_temporal_calendar_with_iso_default(GlobalObject& global_object, Value temporal_calendar_like)
  314. {
  315. // 1. If temporalCalendarLike is undefined, then
  316. if (temporal_calendar_like.is_undefined()) {
  317. // a. Return ! GetISO8601Calendar().
  318. return get_iso8601_calendar(global_object);
  319. }
  320. // 2. Return ? ToTemporalCalendar(temporalCalendarLike).
  321. return to_temporal_calendar(global_object, temporal_calendar_like);
  322. }
  323. // 12.1.23 GetTemporalCalendarWithISODefault ( item ), https://tc39.es/proposal-temporal/#sec-temporal-gettemporalcalendarwithisodefault
  324. ThrowCompletionOr<Object*> get_temporal_calendar_with_iso_default(GlobalObject& global_object, Object& item)
  325. {
  326. auto& vm = global_object.vm();
  327. // 1. If item has an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]], [[InitializedTemporalMonthDay]], [[InitializedTemporalTime]], [[InitializedTemporalYearMonth]], or [[InitializedTemporalZonedDateTime]] internal slot, then
  328. // a. Return item.[[Calendar]].
  329. if (is<PlainDate>(item))
  330. return &static_cast<PlainDate&>(item).calendar();
  331. if (is<PlainDateTime>(item))
  332. return &static_cast<PlainDateTime&>(item).calendar();
  333. if (is<PlainMonthDay>(item))
  334. return &static_cast<PlainMonthDay&>(item).calendar();
  335. if (is<PlainTime>(item))
  336. return &static_cast<PlainTime&>(item).calendar();
  337. if (is<PlainYearMonth>(item))
  338. return &static_cast<PlainYearMonth&>(item).calendar();
  339. if (is<ZonedDateTime>(item))
  340. return &static_cast<ZonedDateTime&>(item).calendar();
  341. // 2. Let calendar be ? Get(item, "calendar").
  342. auto calendar = TRY(item.get(vm.names.calendar));
  343. // 3. Return ? ToTemporalCalendarWithISODefault(calendar).
  344. return to_temporal_calendar_with_iso_default(global_object, calendar);
  345. }
  346. // 12.1.24 DateFromFields ( calendar, fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-datefromfields
  347. ThrowCompletionOr<PlainDate*> date_from_fields(GlobalObject& global_object, Object& calendar, Object const& fields, Object const& options)
  348. {
  349. auto& vm = global_object.vm();
  350. // 1. Assert: Type(calendar) is Object.
  351. // 2. Assert: Type(fields) is Object.
  352. // 3. Let date be ? Invoke(calendar, "dateFromFields", « fields, options »).
  353. auto date = TRY(Value(&calendar).invoke(global_object, vm.names.dateFromFields, &fields, &options));
  354. // 4. Perform ? RequireInternalSlot(date, [[InitializedTemporalDate]]).
  355. auto* date_object = TRY(date.to_object(global_object));
  356. if (!is<PlainDate>(date_object))
  357. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObjectOfType, "Temporal.PlainDate");
  358. // 5. Return date.
  359. return static_cast<PlainDate*>(date_object);
  360. }
  361. // 12.1.25 YearMonthFromFields ( calendar, fields [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal-yearmonthfromfields
  362. ThrowCompletionOr<PlainYearMonth*> year_month_from_fields(GlobalObject& global_object, Object& calendar, Object const& fields, Object const* options)
  363. {
  364. auto& vm = global_object.vm();
  365. // 1. Assert: Type(calendar) is Object.
  366. // 2. Assert: Type(fields) is Object.
  367. // 3. If options is not present, then
  368. // a. Set options to undefined.
  369. // 4. Else,
  370. // a. Assert: Type(options) is Object.
  371. // 5. Let yearMonth be ? Invoke(calendar, "yearMonthFromFields", « fields, options »).
  372. auto year_month = TRY(Value(&calendar).invoke(global_object, vm.names.yearMonthFromFields, &fields, options ?: js_undefined()));
  373. // 6. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
  374. auto* year_month_object = TRY(year_month.to_object(global_object));
  375. if (!is<PlainYearMonth>(year_month_object))
  376. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObjectOfType, "Temporal.PlainYearMonth");
  377. // 7. Return yearMonth.
  378. return static_cast<PlainYearMonth*>(year_month_object);
  379. }
  380. // 12.1.26 MonthDayFromFields ( calendar, fields [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal-monthdayfromfields
  381. ThrowCompletionOr<PlainMonthDay*> month_day_from_fields(GlobalObject& global_object, Object& calendar, Object const& fields, Object const* options)
  382. {
  383. auto& vm = global_object.vm();
  384. // 1. Assert: Type(calendar) is Object.
  385. // 2. Assert: Type(fields) is Object.
  386. // 3. If options is not present, then
  387. // a. Set options to undefined.
  388. // 4. Else,
  389. // a. Assert: Type(options) is Object.
  390. // 5. Let monthDay be ? Invoke(calendar, "monthDayFromFields", « fields, options »).
  391. auto month_day = TRY(Value(&calendar).invoke(global_object, vm.names.monthDayFromFields, &fields, options ?: js_undefined()));
  392. // 6. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
  393. auto* month_day_object = TRY(month_day.to_object(global_object));
  394. if (!is<PlainMonthDay>(month_day_object))
  395. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObjectOfType, "Temporal.PlainMonthDay");
  396. // 7. Return monthDay.
  397. return static_cast<PlainMonthDay*>(month_day_object);
  398. }
  399. // 12.1.27 FormatCalendarAnnotation ( id, showCalendar ), https://tc39.es/proposal-temporal/#sec-temporal-formatcalendarannotation
  400. String format_calendar_annotation(StringView id, StringView show_calendar)
  401. {
  402. // 1. Assert: showCalendar is "auto", "always", or "never".
  403. VERIFY(show_calendar == "auto"sv || show_calendar == "always"sv || show_calendar == "never"sv);
  404. // 2. If showCalendar is "never", return the empty String.
  405. if (show_calendar == "never"sv)
  406. return String::empty();
  407. // 3. If showCalendar is "auto" and id is "iso8601", return the empty String.
  408. if (show_calendar == "auto"sv && id == "iso8601"sv)
  409. return String::empty();
  410. // 4. Return the string-concatenation of "[u-ca=", id, and "]".
  411. return String::formatted("[u-ca={}]", id);
  412. }
  413. // 12.1.28 CalendarEquals ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal-calendarequals
  414. ThrowCompletionOr<bool> calendar_equals(GlobalObject& global_object, Object& one, Object& two)
  415. {
  416. // 1. If one and two are the same Object value, return true.
  417. if (&one == &two)
  418. return true;
  419. // 2. Let calendarOne be ? ToString(one).
  420. auto calendar_one = TRY(Value(&one).to_string(global_object));
  421. // 3. Let calendarTwo be ? ToString(two).
  422. auto calendar_two = TRY(Value(&two).to_string(global_object));
  423. // 4. If calendarOne is calendarTwo, return true.
  424. if (calendar_one == calendar_two)
  425. return true;
  426. // 5. Return false.
  427. return false;
  428. }
  429. // 12.1.29 ConsolidateCalendars ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal-consolidatecalendars
  430. ThrowCompletionOr<Object*> consolidate_calendars(GlobalObject& global_object, Object& one, Object& two)
  431. {
  432. auto& vm = global_object.vm();
  433. // 1. If one and two are the same Object value, return two.
  434. if (&one == &two)
  435. return &two;
  436. // 2. Let calendarOne be ? ToString(one).
  437. auto calendar_one = TRY(Value(&one).to_string(global_object));
  438. // 3. Let calendarTwo be ? ToString(two).
  439. auto calendar_two = TRY(Value(&two).to_string(global_object));
  440. // 4. If calendarOne is calendarTwo, return two.
  441. if (calendar_one == calendar_two)
  442. return &two;
  443. // 5. If calendarOne is "iso8601", return two.
  444. if (calendar_one == "iso8601"sv)
  445. return &two;
  446. // 6. If calendarTwo is "iso8601", return one.
  447. if (calendar_two == "iso8601"sv)
  448. return &one;
  449. // 7. Throw a RangeError exception.
  450. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendar);
  451. }
  452. // 12.1.30 IsISOLeapYear ( year ), https://tc39.es/proposal-temporal/#sec-temporal-isisoleapyear
  453. bool is_iso_leap_year(i32 year)
  454. {
  455. // 1. Assert: year is an integer.
  456. // 2. If year modulo 4 ≠ 0, return false.
  457. if (year % 4 != 0)
  458. return false;
  459. // 3. If year modulo 400 = 0, return true.
  460. if (year % 400 == 0)
  461. return true;
  462. // 4. If year modulo 100 = 0, return false.
  463. if (year % 100 == 0)
  464. return false;
  465. // 5. Return true.
  466. return true;
  467. }
  468. // 12.1.31 ISODaysInYear ( year ), https://tc39.es/proposal-temporal/#sec-temporal-isodaysinyear
  469. u16 iso_days_in_year(i32 year)
  470. {
  471. // 1. Assert: year is an integer.
  472. // 2. If ! IsISOLeapYear(year) is true, then
  473. if (is_iso_leap_year(year)) {
  474. // a. Return 366.
  475. return 366;
  476. }
  477. // 3. Return 365.
  478. return 365;
  479. }
  480. // 12.1.32 ISODaysInMonth ( year, month ), https://tc39.es/proposal-temporal/#sec-temporal-isodaysinmonth
  481. u8 iso_days_in_month(i32 year, u8 month)
  482. {
  483. // 1. Assert: year is an integer.
  484. // 2. Assert: month is an integer, month ≥ 1, and month ≤ 12.
  485. VERIFY(month >= 1 && month <= 12);
  486. // 3. If month is 1, 3, 5, 7, 8, 10, or 12, return 31.
  487. if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
  488. return 31;
  489. // 4. If month is 4, 6, 9, or 11, return 30.
  490. if (month == 4 || month == 6 || month == 9 || month == 11)
  491. return 30;
  492. // 5. If ! IsISOLeapYear(year) is true, return 29.
  493. if (is_iso_leap_year(year))
  494. return 29;
  495. // 6. Return 28.
  496. return 28;
  497. }
  498. // 12.1.33 ToISODayOfWeek ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-toisodayofweek
  499. u8 to_iso_day_of_week(i32 year, u8 month, u8 day)
  500. {
  501. // 1. Assert: year is an integer.
  502. // 2. Assert: month is an integer.
  503. // 3. Assert: day is an integer.
  504. // 4. Let date be the date given by year, month, and day.
  505. // 5. Return date's day of the week according to ISO-8601 as an integer.
  506. // NOTE: Implemented based on https://cs.uwaterloo.ca/~alopez-o/math-faq/node73.html
  507. auto normalized_month = month + (month < 3 ? 10 : -2);
  508. auto normalized_year = year - (month < 3 ? 1 : 0);
  509. auto century = normalized_year / 100;
  510. auto truncated_year = normalized_year - (century * 100);
  511. auto result = (day + static_cast<u8>((2.6 * normalized_month) - 0.2) - (2 * century) + truncated_year + (truncated_year / 4) + (century / 4)) % 7;
  512. if (result <= 0) // Mathematical modulo
  513. result += 7;
  514. return result;
  515. }
  516. // 12.1.34 ToISODayOfYear ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-toisodayofyear
  517. u16 to_iso_day_of_year(i32 year, u8 month, u8 day)
  518. {
  519. // 1. Assert: year is an integer.
  520. // 2. Assert: month is an integer.
  521. // 3. Assert: day is an integer.
  522. // 4. Let date be the date given by year, month, and day.
  523. // 5. Return date's ordinal date in the year according to ISO-8601 as an integer.
  524. u16 days = day;
  525. for (u8 i = month - 1; i > 0; --i)
  526. days += iso_days_in_month(year, i);
  527. return days;
  528. }
  529. // 12.1.35 ToISOWeekOfYear ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-toisoweekofyear
  530. u8 to_iso_week_of_year(i32 year, u8 month, u8 day)
  531. {
  532. // 1. Assert: year is an integer.
  533. // 2. Assert: month is an integer.
  534. // 3. Assert: day is an integer.
  535. // 4. Let date be the date given by year, month, and day.
  536. // 5. Return date's week number according to ISO-8601 as an integer.
  537. auto day_of_year = to_iso_day_of_year(year, month, day);
  538. auto day_of_week = to_iso_day_of_week(year, month, day);
  539. auto week = (day_of_year - day_of_week + 10) / 7;
  540. if (week < 1) {
  541. auto day_of_jump = to_iso_day_of_week(year, 1, 1);
  542. if (day_of_jump == 5 || (is_iso_leap_year(year) && day_of_jump == 6))
  543. return 53;
  544. else
  545. return 52;
  546. } else if (week == 53) {
  547. auto days_in_year = iso_days_in_year(year);
  548. if (days_in_year - day_of_year < 4 - day_of_week)
  549. return 1;
  550. }
  551. return week;
  552. }
  553. // 12.1.36 BuildISOMonthCode ( month ), https://tc39.es/proposal-temporal/#sec-buildisomonthcode
  554. String build_iso_month_code(u8 month)
  555. {
  556. return String::formatted("M{:02}", month);
  557. }
  558. // 12.1.37 ResolveISOMonth ( fields ), https://tc39.es/proposal-temporal/#sec-temporal-resolveisomonth
  559. ThrowCompletionOr<double> resolve_iso_month(GlobalObject& global_object, Object const& fields)
  560. {
  561. auto& vm = global_object.vm();
  562. // 1. Let month be ? Get(fields, "month").
  563. auto month = TRY(fields.get(vm.names.month));
  564. // 2. Let monthCode be ? Get(fields, "monthCode").
  565. auto month_code = TRY(fields.get(vm.names.monthCode));
  566. // 3. If monthCode is undefined, then
  567. if (month_code.is_undefined()) {
  568. // a. If month is undefined, throw a TypeError exception.
  569. if (month.is_undefined())
  570. return vm.throw_completion<TypeError>(global_object, ErrorType::MissingRequiredProperty, vm.names.month.as_string());
  571. // b. Return month.
  572. return month.as_double();
  573. }
  574. // 4. Assert: Type(monthCode) is String.
  575. VERIFY(month_code.is_string());
  576. auto& month_code_string = month_code.as_string().string();
  577. // 5. Let monthLength be the length of monthCode.
  578. auto month_length = month_code_string.length();
  579. // 6. If monthLength is not 3, throw a RangeError exception.
  580. if (month_length != 3)
  581. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidMonthCode);
  582. // 7. Let numberPart be the substring of monthCode from 1.
  583. auto number_part = month_code_string.substring(1);
  584. // 8. Set numberPart to ! ToIntegerOrInfinity(numberPart).
  585. auto number_part_integer = MUST(Value(js_string(vm, move(number_part))).to_integer_or_infinity(global_object));
  586. // 9. If numberPart < 1 or numberPart > 12, throw a RangeError exception.
  587. if (number_part_integer < 1 || number_part_integer > 12)
  588. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidMonthCode);
  589. // 10. If month is not undefined, and month ≠ numberPart, then
  590. if (!month.is_undefined() && month.as_double() != number_part_integer) {
  591. // a. Throw a RangeError exception.
  592. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidMonthCode);
  593. }
  594. // 11. If ! SameValueNonNumeric(monthCode, ! BuildISOMonthCode(numberPart)) is false, then
  595. if (month_code_string != build_iso_month_code(number_part_integer)) {
  596. // a. Throw a RangeError exception.
  597. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidMonthCode);
  598. }
  599. // 12. Return numberPart.
  600. return number_part_integer;
  601. }
  602. // 12.1.38 ISODateFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isodatefromfields
  603. ThrowCompletionOr<ISODate> iso_date_from_fields(GlobalObject& global_object, Object const& fields, Object const& options)
  604. {
  605. auto& vm = global_object.vm();
  606. // 1. Assert: Type(fields) is Object.
  607. // 2. Let overflow be ? ToTemporalOverflow(options).
  608. auto overflow = TRY(to_temporal_overflow(global_object, options));
  609. // 3. Set fields to ? PrepareTemporalFields(fields, « "day", "month", "monthCode", "year" », «»).
  610. auto* prepared_fields = TRY(prepare_temporal_fields(global_object, fields, { "day", "month", "monthCode", "year" }, {}));
  611. // 4. Let year be ? Get(fields, "year").
  612. auto year = TRY(prepared_fields->get(vm.names.year));
  613. // 5. If year is undefined, throw a TypeError exception.
  614. if (year.is_undefined())
  615. return vm.throw_completion<TypeError>(global_object, ErrorType::MissingRequiredProperty, vm.names.year.as_string());
  616. // 6. Let month be ? ResolveISOMonth(fields).
  617. auto month = TRY(resolve_iso_month(global_object, *prepared_fields));
  618. // 7. Let day be ? Get(fields, "day").
  619. auto day = TRY(prepared_fields->get(vm.names.day));
  620. // 8. If day is undefined, throw a TypeError exception.
  621. if (day.is_undefined())
  622. return vm.throw_completion<TypeError>(global_object, ErrorType::MissingRequiredProperty, vm.names.day.as_string());
  623. // 9. Return ? RegulateISODate(year, month, day, overflow).
  624. return regulate_iso_date(global_object, year.as_double(), month, day.as_double(), overflow);
  625. }
  626. // 12.1.39 ISOYearMonthFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isoyearmonthfromfields
  627. ThrowCompletionOr<ISOYearMonth> iso_year_month_from_fields(GlobalObject& global_object, Object const& fields, Object const& options)
  628. {
  629. auto& vm = global_object.vm();
  630. // 1. Assert: Type(fields) is Object.
  631. // 2. Let overflow be ? ToTemporalOverflow(options).
  632. auto overflow = TRY(to_temporal_overflow(global_object, options));
  633. // 3. Set fields to ? PrepareTemporalFields(fields, « "month", "monthCode", "year" », «»).
  634. auto* prepared_fields = TRY(prepare_temporal_fields(global_object, fields, { "month"sv, "monthCode"sv, "year"sv }, {}));
  635. // 4. Let year be ? Get(fields, "year").
  636. auto year = TRY(prepared_fields->get(vm.names.year));
  637. // 5. If year is undefined, throw a TypeError exception.
  638. if (year.is_undefined())
  639. return vm.throw_completion<TypeError>(global_object, ErrorType::MissingRequiredProperty, vm.names.year.as_string());
  640. // 6. Let month be ? ResolveISOMonth(fields).
  641. auto month = TRY(resolve_iso_month(global_object, *prepared_fields));
  642. // 7. Let result be ? RegulateISOYearMonth(year, month, overflow).
  643. auto result = TRY(regulate_iso_year_month(global_object, year.as_double(), month, overflow));
  644. // 8. Return the Record { [[Year]]: result.[[Year]], [[Month]]: result.[[Month]], [[ReferenceISODay]]: 1 }.
  645. return ISOYearMonth { .year = result.year, .month = result.month, .reference_iso_day = 1 };
  646. }
  647. // 12.1.40 ISOMonthDayFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthdayfromfields
  648. ThrowCompletionOr<ISOMonthDay> iso_month_day_from_fields(GlobalObject& global_object, Object const& fields, Object const& options)
  649. {
  650. auto& vm = global_object.vm();
  651. // 1. Assert: Type(fields) is Object.
  652. // 2. Let overflow be ? ToTemporalOverflow(options).
  653. auto overflow = TRY(to_temporal_overflow(global_object, options));
  654. // 3. Set fields to ? PrepareTemporalFields(fields, « "day", "month", "monthCode", "year" », «»).
  655. auto* prepared_fields = TRY(prepare_temporal_fields(global_object, fields, { "day"sv, "month"sv, "monthCode"sv, "year"sv }, {}));
  656. // 4. Let month be ? Get(fields, "month").
  657. auto month_value = TRY(prepared_fields->get(vm.names.month));
  658. // 5. Let monthCode be ? Get(fields, "monthCode").
  659. auto month_code = TRY(prepared_fields->get(vm.names.monthCode));
  660. // 6. Let year be ? Get(fields, "year").
  661. auto year = TRY(prepared_fields->get(vm.names.year));
  662. // 7. If month is not undefined, and monthCode and year are both undefined, then
  663. if (!month_value.is_undefined() && month_code.is_undefined() && year.is_undefined()) {
  664. // a. Throw a TypeError exception.
  665. return vm.throw_completion<TypeError>(global_object, ErrorType::MissingRequiredProperty, "monthCode or year");
  666. }
  667. // 8. Set month to ? ResolveISOMonth(fields).
  668. auto month = TRY(resolve_iso_month(global_object, *prepared_fields));
  669. // 9. Let day be ? Get(fields, "day").
  670. auto day = TRY(prepared_fields->get(vm.names.day));
  671. // 10. If day is undefined, throw a TypeError exception.
  672. if (day.is_undefined())
  673. return vm.throw_completion<TypeError>(global_object, ErrorType::MissingRequiredProperty, vm.names.day.as_string());
  674. // 11. Let referenceISOYear be 1972 (the first leap year after the Unix epoch).
  675. i32 reference_iso_year = 1972;
  676. Optional<ISODate> result;
  677. // 12. If monthCode is undefined, then
  678. if (month_code.is_undefined()) {
  679. // a. Let result be ? RegulateISODate(year, month, day, overflow).
  680. result = TRY(regulate_iso_date(global_object, year.as_double(), month, day.as_double(), overflow));
  681. }
  682. // 13. Else,
  683. else {
  684. // a. Let result be ? RegulateISODate(referenceISOYear, month, day, overflow).
  685. result = TRY(regulate_iso_date(global_object, reference_iso_year, month, day.as_double(), overflow));
  686. }
  687. // 14. Return the Record { [[Month]]: result.[[Month]], [[Day]]: result.[[Day]], [[ReferenceISOYear]]: referenceISOYear }.
  688. return ISOMonthDay { .month = result->month, .day = result->day, .reference_iso_year = reference_iso_year };
  689. }
  690. // 12.1.41 ISOYear ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isoyear
  691. i32 iso_year(Object& temporal_object)
  692. {
  693. // 1. Assert: temporalObject has an [[ISOYear]] internal slot.
  694. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  695. // 2. Return 𝔽(temporalObject.[[ISOYear]]).
  696. if (is<PlainDate>(temporal_object))
  697. return static_cast<PlainDate&>(temporal_object).iso_year();
  698. if (is<PlainDateTime>(temporal_object))
  699. return static_cast<PlainDateTime&>(temporal_object).iso_year();
  700. if (is<PlainYearMonth>(temporal_object))
  701. return static_cast<PlainYearMonth&>(temporal_object).iso_year();
  702. if (is<PlainMonthDay>(temporal_object))
  703. return static_cast<PlainMonthDay&>(temporal_object).iso_year();
  704. VERIFY_NOT_REACHED();
  705. }
  706. // 12.1.42 ISOMonth ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonth
  707. u8 iso_month(Object& temporal_object)
  708. {
  709. // 1. Assert: temporalObject has an [[ISOMonth]] internal slot.
  710. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  711. // 2. Return 𝔽(temporalObject.[[ISOMonth]]).
  712. if (is<PlainDate>(temporal_object))
  713. return static_cast<PlainDate&>(temporal_object).iso_month();
  714. if (is<PlainDateTime>(temporal_object))
  715. return static_cast<PlainDateTime&>(temporal_object).iso_month();
  716. if (is<PlainYearMonth>(temporal_object))
  717. return static_cast<PlainYearMonth&>(temporal_object).iso_month();
  718. if (is<PlainMonthDay>(temporal_object))
  719. return static_cast<PlainMonthDay&>(temporal_object).iso_month();
  720. VERIFY_NOT_REACHED();
  721. }
  722. // 12.1.43 ISOMonthCode ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthcode
  723. String iso_month_code(Object& temporal_object)
  724. {
  725. // 1. Assert: temporalObject has an [[ISOMonth]] internal slot.
  726. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  727. // 2. Return ! BuildISOMonthCode(temporalObject.[[ISOMonth]]).
  728. if (is<PlainDate>(temporal_object))
  729. return build_iso_month_code(static_cast<PlainDate&>(temporal_object).iso_month());
  730. if (is<PlainDateTime>(temporal_object))
  731. return build_iso_month_code(static_cast<PlainDateTime&>(temporal_object).iso_month());
  732. if (is<PlainYearMonth>(temporal_object))
  733. return build_iso_month_code(static_cast<PlainYearMonth&>(temporal_object).iso_month());
  734. if (is<PlainMonthDay>(temporal_object))
  735. return build_iso_month_code(static_cast<PlainMonthDay&>(temporal_object).iso_month());
  736. VERIFY_NOT_REACHED();
  737. }
  738. // 12.1.44 ISODay ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthcode
  739. u8 iso_day(Object& temporal_object)
  740. {
  741. // 1. Assert: temporalObject has an [[ISODay]] internal slot.
  742. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  743. // 2. Return 𝔽(temporalObject.[[ISODay]]).
  744. if (is<PlainDate>(temporal_object))
  745. return static_cast<PlainDate&>(temporal_object).iso_day();
  746. if (is<PlainDateTime>(temporal_object))
  747. return static_cast<PlainDateTime&>(temporal_object).iso_day();
  748. if (is<PlainYearMonth>(temporal_object))
  749. return static_cast<PlainYearMonth&>(temporal_object).iso_day();
  750. if (is<PlainMonthDay>(temporal_object))
  751. return static_cast<PlainMonthDay&>(temporal_object).iso_day();
  752. VERIFY_NOT_REACHED();
  753. }
  754. // 12.1.45 DefaultMergeFields ( fields, additionalFields ), https://tc39.es/proposal-temporal/#sec-temporal-defaultmergefields
  755. ThrowCompletionOr<Object*> default_merge_fields(GlobalObject& global_object, Object const& fields, Object const& additional_fields)
  756. {
  757. auto& vm = global_object.vm();
  758. // 1. Let merged be ! OrdinaryObjectCreate(%Object.prototype%).
  759. auto* merged = Object::create(global_object, global_object.object_prototype());
  760. // 2. Let originalKeys be ? EnumerableOwnPropertyNames(fields, key).
  761. auto original_keys = TRY(fields.enumerable_own_property_names(Object::PropertyKind::Key));
  762. // 3. For each element nextKey of originalKeys, do
  763. for (auto& next_key : original_keys) {
  764. // a. If nextKey is not "month" or "monthCode", then
  765. if (next_key.as_string().string() != vm.names.month.as_string() && next_key.as_string().string() != vm.names.monthCode.as_string()) {
  766. auto property_name = PropertyKey::from_value(global_object, next_key);
  767. // i. Let propValue be ? Get(fields, nextKey).
  768. auto prop_value = TRY(fields.get(property_name));
  769. // ii. If propValue is not undefined, then
  770. if (!prop_value.is_undefined()) {
  771. // 1. Perform ! CreateDataPropertyOrThrow(merged, nextKey, propValue).
  772. MUST(merged->create_data_property_or_throw(property_name, prop_value));
  773. }
  774. }
  775. }
  776. // 4. Let newKeys be ? EnumerableOwnPropertyNames(additionalFields, key).
  777. auto new_keys = TRY(additional_fields.enumerable_own_property_names(Object::PropertyKind::Key));
  778. // IMPLEMENTATION DEFINED: This is an optimization, so we don't have to iterate new_keys three times (worst case), but only once.
  779. bool new_keys_contains_month_or_month_code_property = false;
  780. // 5. For each element nextKey of newKeys, do
  781. for (auto& next_key : new_keys) {
  782. auto property_name = PropertyKey::from_value(global_object, next_key);
  783. // a. Let propValue be ? Get(additionalFields, nextKey).
  784. auto prop_value = TRY(additional_fields.get(property_name));
  785. // b. If propValue is not undefined, then
  786. if (!prop_value.is_undefined()) {
  787. // i. Perform ! CreateDataPropertyOrThrow(merged, nextKey, propValue).
  788. MUST(merged->create_data_property_or_throw(property_name, prop_value));
  789. }
  790. // See comment above.
  791. new_keys_contains_month_or_month_code_property |= next_key.as_string().string() == vm.names.month.as_string() || next_key.as_string().string() == vm.names.monthCode.as_string();
  792. }
  793. // 6. If newKeys does not contain either "month" or "monthCode", then
  794. if (!new_keys_contains_month_or_month_code_property) {
  795. // a. Let month be ? Get(fields, "month").
  796. auto month = TRY(fields.get(vm.names.month));
  797. // b. If month is not undefined, then
  798. if (!month.is_undefined()) {
  799. // i. Perform ! CreateDataPropertyOrThrow(merged, "month", month).
  800. MUST(merged->create_data_property_or_throw(vm.names.month, month));
  801. }
  802. // c. Let monthCode be ? Get(fields, "monthCode").
  803. auto month_code = TRY(fields.get(vm.names.monthCode));
  804. // d. If monthCode is not undefined, then
  805. if (!month_code.is_undefined()) {
  806. // i. Perform ! CreateDataPropertyOrThrow(merged, "monthCode", monthCode).
  807. MUST(merged->create_data_property_or_throw(vm.names.monthCode, month_code));
  808. }
  809. }
  810. // 7. Return merged.
  811. return merged;
  812. }
  813. }