Calendar.cpp 47 KB

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