Calendar.cpp 47 KB

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