PlainYearMonthConstructor.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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(GlobalObject& global_object)
  16. : NativeFunction(vm().names.PlainYearMonth.as_string(), *global_object.function_prototype())
  17. {
  18. }
  19. void PlainYearMonthConstructor::initialize(GlobalObject& global_object)
  20. {
  21. NativeFunction::initialize(global_object);
  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, global_object.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(vm.names.from, from, 1, attr);
  28. define_native_function(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>(global_object(), 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& global_object = this->global_object();
  42. auto iso_year = vm.argument(0);
  43. auto iso_month = vm.argument(1);
  44. auto calendar_like = vm.argument(2);
  45. auto reference_iso_day = vm.argument(3);
  46. // 2. If referenceISODay is undefined, then
  47. if (reference_iso_day.is_undefined()) {
  48. // a. Set referenceISODay to 1𝔽.
  49. reference_iso_day = Value(1);
  50. }
  51. // 3. Let y be ? ToIntegerThrowOnInfinity(isoYear).
  52. auto y = TRY(to_integer_throw_on_infinity(global_object, iso_year, ErrorType::TemporalInvalidPlainYearMonth));
  53. // 4. Let m be ? ToIntegerThrowOnInfinity(isoMonth).
  54. auto m = TRY(to_integer_throw_on_infinity(global_object, iso_month, ErrorType::TemporalInvalidPlainYearMonth));
  55. // 5. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
  56. auto* calendar = TRY(to_temporal_calendar_with_iso_default(global_object, calendar_like));
  57. // 6. Let ref be ? ToIntegerThrowOnInfinity(referenceISODay).
  58. auto ref = TRY(to_integer_throw_on_infinity(global_object, reference_iso_day, ErrorType::TemporalInvalidPlainYearMonth));
  59. // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards.
  60. // This does not change the exposed behavior as the call to CreateTemporalYearMonth will immediately check that these values are valid
  61. // ISO values (for years: -273975 - 273975, for months: 1 - 12, for days: 1 - 31) all of which are subsets of this check.
  62. if (!AK::is_within_range<i32>(y) || !AK::is_within_range<u8>(m) || !AK::is_within_range<u8>(ref))
  63. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainYearMonth);
  64. // 7. Return ? CreateTemporalYearMonth(y, m, calendar, ref, NewTarget).
  65. return TRY(create_temporal_year_month(global_object, y, m, *calendar, ref, &new_target));
  66. }
  67. // 9.2.2 Temporal.PlainYearMonth.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.from
  68. JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthConstructor::from)
  69. {
  70. // 1. Set options to ? GetOptionsObject(options).
  71. auto* options = TRY(get_options_object(global_object, vm.argument(1)));
  72. auto item = vm.argument(0);
  73. // 2. If Type(item) is Object and item has an [[InitializedTemporalYearMonth]] internal slot, then
  74. if (item.is_object() && is<PlainYearMonth>(item.as_object())) {
  75. // a. Perform ? ToTemporalOverflow(options).
  76. (void)TRY(to_temporal_overflow(global_object, options));
  77. auto& plain_year_month_object = static_cast<PlainYearMonth&>(item.as_object());
  78. // b. Return ? CreateTemporalYearMonth(item.[[ISOYear]], item.[[ISOMonth]], item.[[Calendar]], item.[[ISODay]]).
  79. return TRY(create_temporal_year_month(global_object, plain_year_month_object.iso_year(), plain_year_month_object.iso_month(), plain_year_month_object.calendar(), plain_year_month_object.iso_day()));
  80. }
  81. // 3. Return ? ToTemporalYearMonth(item, options).
  82. return TRY(to_temporal_year_month(global_object, item, options));
  83. }
  84. // 9.2.3 Temporal.PlainYearMonth.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.compare
  85. JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthConstructor::compare)
  86. {
  87. // 1. Set one to ? ToTemporalYearMonth(one).
  88. auto* one = TRY(to_temporal_year_month(global_object, vm.argument(0)));
  89. // 2. Set two to ? ToTemporalYearMonth(one).
  90. auto* two = TRY(to_temporal_year_month(global_object, vm.argument(1)));
  91. // 3. Return 𝔽(! CompareISODate(one.[[ISOYear]], one.[[ISOMonth]], one.[[ISODay]], two.[[ISOYear]], two.[[ISOMonth]], two.[[ISODay]])).
  92. return Value(compare_iso_date(one->iso_year(), one->iso_month(), one->iso_day(), two->iso_year(), two->iso_month(), two->iso_day()));
  93. }
  94. }