Calendar.cpp 43 KB

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