Calendar.cpp 48 KB

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