PlainMonthDayConstructor.cpp 5.2 KB

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