Calendar.cpp 46 KB

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