Calendar.cpp 45 KB

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