PlainDateConstructor.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Checked.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Temporal/Calendar.h>
  9. #include <LibJS/Runtime/Temporal/PlainDate.h>
  10. #include <LibJS/Runtime/Temporal/PlainDateConstructor.h>
  11. namespace JS::Temporal {
  12. // 3.1 The Temporal.PlainDate Constructor, https://tc39.es/proposal-temporal/#sec-temporal-plaindate-constructor
  13. PlainDateConstructor::PlainDateConstructor(GlobalObject& global_object)
  14. : NativeFunction(vm().names.PlainDate.as_string(), *global_object.function_prototype())
  15. {
  16. }
  17. void PlainDateConstructor::initialize(GlobalObject& global_object)
  18. {
  19. NativeFunction::initialize(global_object);
  20. auto& vm = this->vm();
  21. // 3.2.1 Temporal.PlainDate.prototype, https://tc39.es/proposal-temporal/#sec-temporal-plaindate-prototype
  22. define_direct_property(vm.names.prototype, global_object.temporal_plain_date_prototype(), 0);
  23. define_direct_property(vm.names.length, Value(3), Attribute::Configurable);
  24. }
  25. // 3.1.1 Temporal.PlainDate ( isoYear, isoMonth, isoDay [ , calendarLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate
  26. Value PlainDateConstructor::call()
  27. {
  28. auto& vm = this->vm();
  29. // 1. If NewTarget is undefined, throw a TypeError exception.
  30. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.PlainDate");
  31. return {};
  32. }
  33. // 3.1.1 Temporal.PlainDate ( isoYear, isoMonth, isoDay [ , calendarLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate
  34. Value PlainDateConstructor::construct(FunctionObject& new_target)
  35. {
  36. auto& vm = this->vm();
  37. auto& global_object = this->global_object();
  38. // 2. Let y be ? ToIntegerOrInfinity(isoYear).
  39. auto y = vm.argument(0).to_integer_or_infinity(global_object);
  40. if (vm.exception())
  41. return {};
  42. // 3. If y is +∞ or -∞, throw a RangeError exception.
  43. if (Value(y).is_infinity()) {
  44. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  45. return {};
  46. }
  47. // 4. Let m be ? ToIntegerOrInfinity(isoMonth).
  48. auto m = vm.argument(1).to_integer_or_infinity(global_object);
  49. if (vm.exception())
  50. return {};
  51. // 5. If m is +∞ or -∞, throw a RangeError exception.
  52. if (Value(m).is_infinity()) {
  53. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  54. return {};
  55. }
  56. // 6. Let d be ? ToIntegerOrInfinity(isoDay).
  57. auto d = vm.argument(2).to_integer_or_infinity(global_object);
  58. if (vm.exception())
  59. return {};
  60. // 7. If d is +∞ or -∞, throw a RangeError exception.
  61. if (Value(d).is_infinity()) {
  62. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  63. return {};
  64. }
  65. // 8. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
  66. auto* calendar = to_temporal_calendar_with_iso_default(global_object, vm.argument(3));
  67. if (vm.exception())
  68. return {};
  69. // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards.
  70. // This does not change the exposed behaviour as the call to CreateTemporalDate will immediately check that these values are valid
  71. // ISO values (for years: -273975 - 273975, for months: 1 - 12, for days: 1 - 31) all of which are subsets of this check.
  72. if (!AK::is_within_range<i32>(y) || !AK::is_within_range<u8>(m) || !AK::is_within_range<u8>(d)) {
  73. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  74. return {};
  75. }
  76. // 9. Return ? CreateTemporalDate(y, m, d, calendar, NewTarget).
  77. return create_temporal_date(global_object, y, m, d, *calendar, &new_target);
  78. }
  79. }