PlainYearMonthConstructor.cpp 5.2 KB

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