PlainYearMonthConstructor.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/TypeCasts.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  10. #include <LibJS/Runtime/Temporal/Calendar.h>
  11. #include <LibJS/Runtime/Temporal/PlainYearMonth.h>
  12. #include <LibJS/Runtime/Temporal/PlainYearMonthConstructor.h>
  13. namespace JS::Temporal {
  14. // 9.1 The Temporal.PlainYearMonth Constructor, https://tc39.es/proposal-temporal/#sec-temporal-plainyearmonth-constructor
  15. PlainYearMonthConstructor::PlainYearMonthConstructor(Realm& realm)
  16. : NativeFunction(realm.vm().names.PlainYearMonth.as_string(), *realm.intrinsics().function_prototype())
  17. {
  18. }
  19. void PlainYearMonthConstructor::initialize(Realm& realm)
  20. {
  21. NativeFunction::initialize(realm);
  22. auto& vm = this->vm();
  23. // 9.2.1 Temporal.PlainYearMonth.prototype, https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype
  24. define_direct_property(vm.names.prototype, realm.intrinsics().temporal_plain_year_month_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. define_native_function(realm, vm.names.compare, compare, 2, attr);
  29. }
  30. // 9.1.1 Temporal.PlainYearMonth ( isoYear, isoMonth [ , calendarLike [ , referenceISODay ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth
  31. ThrowCompletionOr<Value> PlainYearMonthConstructor::call()
  32. {
  33. auto& vm = this->vm();
  34. // 1. If NewTarget is undefined, throw a TypeError exception.
  35. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Temporal.PlainYearMonth");
  36. }
  37. // 9.1.1 Temporal.PlainYearMonth ( isoYear, isoMonth [ , calendarLike [ , referenceISODay ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth
  38. ThrowCompletionOr<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_like = 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 ? ToIntegerThrowOnInfinity(isoYear).
  51. auto y = TRY(to_integer_throw_on_infinity(vm, iso_year, ErrorType::TemporalInvalidPlainYearMonth));
  52. // 4. Let m be ? ToIntegerThrowOnInfinity(isoMonth).
  53. auto m = TRY(to_integer_throw_on_infinity(vm, iso_month, ErrorType::TemporalInvalidPlainYearMonth));
  54. // 5. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
  55. auto* calendar = TRY(to_temporal_calendar_with_iso_default(vm, calendar_like));
  56. // 6. Let ref be ? ToIntegerThrowOnInfinity(referenceISODay).
  57. auto ref = TRY(to_integer_throw_on_infinity(vm, reference_iso_day, ErrorType::TemporalInvalidPlainYearMonth));
  58. // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards.
  59. // This does not change the exposed behavior as the call to CreateTemporalYearMonth will immediately check that these values are valid
  60. // ISO values (for years: -273975 - 273975, for months: 1 - 12, for days: 1 - 31) all of which are subsets of this check.
  61. if (!AK::is_within_range<i32>(y) || !AK::is_within_range<u8>(m) || !AK::is_within_range<u8>(ref))
  62. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidPlainYearMonth);
  63. // 7. Return ? CreateTemporalYearMonth(y, m, calendar, ref, NewTarget).
  64. return TRY(create_temporal_year_month(vm, y, m, *calendar, ref, &new_target));
  65. }
  66. // 9.2.2 Temporal.PlainYearMonth.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.from
  67. JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthConstructor::from)
  68. {
  69. // 1. Set options to ? GetOptionsObject(options).
  70. auto* options = TRY(get_options_object(vm, vm.argument(1)));
  71. auto item = vm.argument(0);
  72. // 2. If Type(item) is Object and item has an [[InitializedTemporalYearMonth]] internal slot, then
  73. if (item.is_object() && is<PlainYearMonth>(item.as_object())) {
  74. // a. Perform ? ToTemporalOverflow(options).
  75. (void)TRY(to_temporal_overflow(vm, options));
  76. auto& plain_year_month_object = static_cast<PlainYearMonth&>(item.as_object());
  77. // b. Return ! CreateTemporalYearMonth(item.[[ISOYear]], item.[[ISOMonth]], item.[[Calendar]], item.[[ISODay]]).
  78. return MUST(create_temporal_year_month(vm, plain_year_month_object.iso_year(), plain_year_month_object.iso_month(), plain_year_month_object.calendar(), plain_year_month_object.iso_day()));
  79. }
  80. // 3. Return ? ToTemporalYearMonth(item, options).
  81. return TRY(to_temporal_year_month(vm, item, options));
  82. }
  83. // 9.2.3 Temporal.PlainYearMonth.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.compare
  84. JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthConstructor::compare)
  85. {
  86. // 1. Set one to ? ToTemporalYearMonth(one).
  87. auto* one = TRY(to_temporal_year_month(vm, vm.argument(0)));
  88. // 2. Set two to ? ToTemporalYearMonth(one).
  89. auto* two = TRY(to_temporal_year_month(vm, vm.argument(1)));
  90. // 3. Return 𝔽(! CompareISODate(one.[[ISOYear]], one.[[ISOMonth]], one.[[ISODay]], two.[[ISOYear]], two.[[ISOMonth]], two.[[ISODay]])).
  91. return Value(compare_iso_date(one->iso_year(), one->iso_month(), one->iso_day(), two->iso_year(), two->iso_month(), two->iso_day()));
  92. }
  93. }