Calendar.cpp 47 KB

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