PlainYearMonthConstructor.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  4. * Copyright (c) 2024, Tim Flynn <trflynn89@ladybird.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibJS/Runtime/Temporal/Calendar.h>
  9. #include <LibJS/Runtime/Temporal/PlainYearMonth.h>
  10. #include <LibJS/Runtime/Temporal/PlainYearMonthConstructor.h>
  11. namespace JS::Temporal {
  12. GC_DEFINE_ALLOCATOR(PlainYearMonthConstructor);
  13. // 9.1 The Temporal.PlainYearMonth Constructor, https://tc39.es/proposal-temporal/#sec-temporal-plainyearmonth-constructor
  14. PlainYearMonthConstructor::PlainYearMonthConstructor(Realm& realm)
  15. : NativeFunction(realm.vm().names.PlainYearMonth.as_string(), realm.intrinsics().function_prototype())
  16. {
  17. }
  18. void PlainYearMonthConstructor::initialize(Realm& realm)
  19. {
  20. Base::initialize(realm);
  21. auto& vm = this->vm();
  22. // 9.2.1 Temporal.PlainYearMonth.prototype, https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype
  23. define_direct_property(vm.names.prototype, realm.intrinsics().temporal_plain_year_month_prototype(), 0);
  24. define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  25. u8 attr = Attribute::Writable | Attribute::Configurable;
  26. define_native_function(realm, vm.names.from, from, 1, attr);
  27. define_native_function(realm, vm.names.compare, compare, 2, attr);
  28. }
  29. // 9.1.1 Temporal.PlainYearMonth ( isoYear, isoMonth [ , calendar [ , referenceISODay ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth
  30. ThrowCompletionOr<Value> PlainYearMonthConstructor::call()
  31. {
  32. auto& vm = this->vm();
  33. // 1. If NewTarget is undefined, then
  34. // a. Throw a TypeError exception.
  35. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Temporal.PlainYearMonth");
  36. }
  37. // 9.1.1 Temporal.PlainYearMonth ( isoYear, isoMonth [ , calendar [ , referenceISODay ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth
  38. ThrowCompletionOr<GC::Ref<Object>> PlainYearMonthConstructor::construct(FunctionObject& new_target)
  39. {
  40. auto& vm = this->vm();
  41. auto iso_year = vm.argument(0);
  42. auto iso_month = vm.argument(1);
  43. auto calendar_value = vm.argument(2);
  44. auto reference_iso_day = vm.argument(3);
  45. // 2. If referenceISODay is undefined, then
  46. if (reference_iso_day.is_undefined()) {
  47. // a. Set referenceISODay to 1𝔽.
  48. reference_iso_day = Value { 1 };
  49. }
  50. // 3. Let y be ? ToIntegerWithTruncation(isoYear).
  51. auto year = TRY(to_integer_with_truncation(vm, iso_year, ErrorType::TemporalInvalidPlainYearMonth));
  52. // 4. Let m be ? ToIntegerWithTruncation(isoMonth).
  53. auto month = TRY(to_integer_with_truncation(vm, iso_month, ErrorType::TemporalInvalidPlainYearMonth));
  54. // 5. If calendar is undefined, set calendar to "iso8601".
  55. if (calendar_value.is_undefined())
  56. calendar_value = PrimitiveString::create(vm, "iso8601"_string);
  57. // 6. If calendar is not a String, throw a TypeError exception.
  58. if (!calendar_value.is_string())
  59. return vm.throw_completion<TypeError>(ErrorType::NotAString, "calendar"sv);
  60. // 7. Set calendar to ? CanonicalizeCalendar(calendar).
  61. auto calendar = TRY(canonicalize_calendar(vm, calendar_value.as_string().utf8_string_view()));
  62. // 8. Let ref be ? ToIntegerWithTruncation(referenceISODay).
  63. auto reference = TRY(to_integer_with_truncation(vm, reference_iso_day, ErrorType::TemporalInvalidPlainYearMonth));
  64. // 9. If IsValidISODate(y, m, ref) is false, throw a RangeError exception.
  65. if (!is_valid_iso_date(year, month, reference))
  66. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidPlainYearMonth);
  67. // 10. Let isoDate be CreateISODateRecord(y, m, ref).
  68. auto iso_date = create_iso_date_record(year, month, reference);
  69. // 11. Return ? CreateTemporalYearMonth(isoDate, calendar, NewTarget).
  70. return TRY(create_temporal_year_month(vm, iso_date, move(calendar), &new_target));
  71. }
  72. // 9.2.2 Temporal.PlainYearMonth.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.from
  73. JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthConstructor::from)
  74. {
  75. // 1. Return ? ToTemporalYearMonth(item, options).
  76. return TRY(to_temporal_year_month(vm, vm.argument(0), vm.argument(1)));
  77. }
  78. // 9.2.3 Temporal.PlainYearMonth.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.compare
  79. JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthConstructor::compare)
  80. {
  81. // 1. Set one to ? ToTemporalYearMonth(one).
  82. auto one = TRY(to_temporal_year_month(vm, vm.argument(0)));
  83. // 2. Set two to ? ToTemporalYearMonth(two).
  84. auto two = TRY(to_temporal_year_month(vm, vm.argument(1)));
  85. // 3. Return 𝔽(CompareISODate(one.[[ISODate]], two.[[ISODate]])).
  86. return compare_iso_date(one->iso_date(), two->iso_date());
  87. }
  88. }