PlainMonthDayConstructor.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2024, Tim Flynn <trflynn89@ladybird.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  8. #include <LibJS/Runtime/Temporal/Calendar.h>
  9. #include <LibJS/Runtime/Temporal/PlainDate.h>
  10. #include <LibJS/Runtime/Temporal/PlainMonthDay.h>
  11. #include <LibJS/Runtime/Temporal/PlainMonthDayConstructor.h>
  12. namespace JS::Temporal {
  13. GC_DEFINE_ALLOCATOR(PlainMonthDayConstructor);
  14. // 10.1 The Temporal.PlainMonthDay Constructor, https://tc39.es/proposal-temporal/#sec-temporal-plainmonthday-constructor
  15. PlainMonthDayConstructor::PlainMonthDayConstructor(Realm& realm)
  16. : NativeFunction(realm.vm().names.PlainMonthDay.as_string(), realm.intrinsics().function_prototype())
  17. {
  18. }
  19. void PlainMonthDayConstructor::initialize(Realm& realm)
  20. {
  21. Base::initialize(realm);
  22. auto& vm = this->vm();
  23. // 10.2.1 Temporal.PlainMonthDay.prototype, https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype
  24. define_direct_property(vm.names.prototype, realm.intrinsics().temporal_plain_month_day_prototype(), 0);
  25. define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  26. u8 attr = Attribute::Writable | Attribute::Configurable;
  27. define_native_function(realm, vm.names.from, from, 1, attr);
  28. }
  29. // 10.1.1 Temporal.PlainMonthDay ( isoMonth, isoDay [ , calendar [ , referenceISOYear ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday
  30. ThrowCompletionOr<Value> PlainMonthDayConstructor::call()
  31. {
  32. auto& vm = this->vm();
  33. // 1. If NewTarget is undefined, throw a TypeError exception.
  34. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Temporal.PlainMonthDay");
  35. }
  36. // 10.1.1 Temporal.PlainMonthDay ( isoMonth, isoDay [ , calendar [ , referenceISOYear ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday
  37. ThrowCompletionOr<GC::Ref<Object>> PlainMonthDayConstructor::construct(FunctionObject& new_target)
  38. {
  39. auto& vm = this->vm();
  40. auto iso_month = vm.argument(0);
  41. auto iso_day = vm.argument(1);
  42. auto calendar_value = vm.argument(2);
  43. auto reference_iso_year = vm.argument(3);
  44. // 2. If referenceISOYear is undefined, then
  45. if (reference_iso_year.is_undefined()) {
  46. // a. Set referenceISOYear to 1972𝔽 (the first ISO 8601 leap year after the epoch).
  47. reference_iso_year = Value { 1972 };
  48. }
  49. // 3. Let m be ? ToIntegerWithTruncation(isoMonth).
  50. auto month = TRY(to_integer_with_truncation(vm, iso_month, ErrorType::TemporalInvalidPlainMonthDay));
  51. // 4. Let d be ? ToIntegerWithTruncation(isoDay).
  52. auto day = TRY(to_integer_with_truncation(vm, iso_day, ErrorType::TemporalInvalidPlainMonthDay));
  53. // 5. If calendar is undefined, set calendar to "iso8601".
  54. if (calendar_value.is_undefined())
  55. calendar_value = PrimitiveString::create(vm, "iso8601"_string);
  56. // 6. If calendar is not a String, throw a TypeError exception.
  57. if (!calendar_value.is_string())
  58. return vm.throw_completion<TypeError>(ErrorType::NotAString, calendar_value);
  59. // 7. Set calendar to ? CanonicalizeCalendar(calendar).
  60. auto calendar = TRY(canonicalize_calendar(vm, calendar_value.as_string().utf8_string_view()));
  61. // 8. Let y be ? ToIntegerWithTruncation(referenceISOYear).
  62. auto year = TRY(to_integer_with_truncation(vm, reference_iso_year, ErrorType::TemporalInvalidPlainMonthDay));
  63. // 9. If IsValidISODate(y, m, d) is false, throw a RangeError exception.
  64. if (!is_valid_iso_date(year, month, day))
  65. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidPlainMonthDay);
  66. // 10. Let isoDate be CreateISODateRecord(y, m, d).
  67. auto iso_date = create_iso_date_record(year, month, day);
  68. // 11. Return ? CreateTemporalMonthDay(isoDate, calendar, NewTarget).
  69. return TRY(create_temporal_month_day(vm, iso_date, move(calendar), &new_target));
  70. }
  71. // 10.2.2 Temporal.PlainMonthDay.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.from
  72. JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayConstructor::from)
  73. {
  74. // 1. Return ? ToTemporalMonthDay(item, options).
  75. return TRY(to_temporal_month_day(vm, vm.argument(0), vm.argument(1)));
  76. }
  77. }