PlainYearMonthConstructor.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2021, 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. Value PlainYearMonthConstructor::call()
  32. {
  33. auto& vm = this->vm();
  34. // 1. If NewTarget is undefined, throw a TypeError exception.
  35. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.PlainYearMonth");
  36. return {};
  37. }
  38. // 9.1.1 Temporal.PlainYearMonth ( isoYear, isoMonth [ , calendarLike [ , referenceISODay ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth
  39. Value PlainYearMonthConstructor::construct(FunctionObject& new_target)
  40. {
  41. auto& vm = this->vm();
  42. auto& global_object = this->global_object();
  43. auto iso_year = vm.argument(0);
  44. auto iso_month = vm.argument(1);
  45. auto calendar_like = vm.argument(2);
  46. auto reference_iso_day = vm.argument(3);
  47. // 2. If referenceISODay is undefined, then
  48. if (reference_iso_day.is_undefined()) {
  49. // a. Set referenceISODay to 1𝔽.
  50. reference_iso_day = Value(1);
  51. }
  52. // 3. Let y be ? ToIntegerThrowOnInfinity(isoYear).
  53. auto y = to_integer_throw_on_infinity(global_object, iso_year, ErrorType::TemporalInvalidPlainYearMonth);
  54. if (vm.exception())
  55. return {};
  56. // 4. Let m be ? ToIntegerThrowOnInfinity(isoMonth).
  57. auto m = to_integer_throw_on_infinity(global_object, iso_month, ErrorType::TemporalInvalidPlainYearMonth);
  58. if (vm.exception())
  59. return {};
  60. // 5. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
  61. auto* calendar = to_temporal_calendar_with_iso_default(global_object, calendar_like);
  62. if (vm.exception())
  63. return {};
  64. // 6. Let ref be ? ToIntegerThrowOnInfinity(referenceISODay).
  65. auto ref = to_integer_throw_on_infinity(global_object, reference_iso_day, ErrorType::TemporalInvalidPlainYearMonth);
  66. if (vm.exception())
  67. return {};
  68. // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards.
  69. // This does not change the exposed behavior as the call to CreateTemporalYearMonth will immediately check that these values are valid
  70. // ISO values (for years: -273975 - 273975, for months: 1 - 12, for days: 1 - 31) all of which are subsets of this check.
  71. if (!AK::is_within_range<i32>(y) || !AK::is_within_range<u8>(m) || !AK::is_within_range<u8>(ref)) {
  72. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainYearMonth);
  73. return {};
  74. }
  75. // 7. Return ? CreateTemporalYearMonth(y, m, calendar, ref, NewTarget).
  76. return create_temporal_year_month(global_object, y, m, *calendar, ref, &new_target);
  77. }
  78. // 9.2.2 Temporal.PlainYearMonth.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.from
  79. JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthConstructor::from)
  80. {
  81. // 1. Set options to ? GetOptionsObject(options).
  82. auto* options = get_options_object(global_object, vm.argument(1));
  83. if (vm.exception())
  84. return {};
  85. auto item = vm.argument(0);
  86. // 2. If Type(item) is Object and item has an [[InitializedTemporalYearMonth]] internal slot, then
  87. if (item.is_object() && is<PlainYearMonth>(item.as_object())) {
  88. // a. Perform ? ToTemporalOverflow(options).
  89. (void)to_temporal_overflow(global_object, *options);
  90. if (vm.exception())
  91. return {};
  92. auto& plain_year_month_object = static_cast<PlainYearMonth&>(item.as_object());
  93. // b. Return ? CreateTemporalYearMonth(item.[[ISOYear]], item.[[ISOMonth]], item.[[Calendar]], item.[[ISODay]]).
  94. return 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());
  95. }
  96. // 3. Return ? ToTemporalYearMonth(item, options).
  97. return to_temporal_year_month(global_object, item, options);
  98. }
  99. // 9.2.3 Temporal.PlainYearMonth.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.compare
  100. JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthConstructor::compare)
  101. {
  102. // 1. Set one to ? ToTemporalYearMonth(one).
  103. auto* one = to_temporal_year_month(global_object, vm.argument(0));
  104. if (vm.exception())
  105. return {};
  106. // 2. Set two to ? ToTemporalYearMonth(one).
  107. auto* two = to_temporal_year_month(global_object, vm.argument(1));
  108. if (vm.exception())
  109. return {};
  110. // 3. Return 𝔽(! CompareISODate(one.[[ISOYear]], one.[[ISOMonth]], one.[[ISODay]], two.[[ISOYear]], two.[[ISOMonth]], two.[[ISODay]])).
  111. return Value(compare_iso_date(one->iso_year(), one->iso_month(), one->iso_day(), two->iso_year(), two->iso_month(), two->iso_day()));
  112. }
  113. }