PlainDateConstructor.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. u8 attr = Attribute::Writable | Attribute::Configurable;
  24. define_native_function(vm.names.from, from, 1, attr);
  25. define_native_function(vm.names.compare, compare, 2, attr);
  26. define_direct_property(vm.names.length, Value(3), Attribute::Configurable);
  27. }
  28. // 3.1.1 Temporal.PlainDate ( isoYear, isoMonth, isoDay [ , calendarLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate
  29. Value PlainDateConstructor::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.PlainDate");
  34. return {};
  35. }
  36. // 3.1.1 Temporal.PlainDate ( isoYear, isoMonth, isoDay [ , calendarLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate
  37. Value PlainDateConstructor::construct(FunctionObject& new_target)
  38. {
  39. auto& vm = this->vm();
  40. auto& global_object = this->global_object();
  41. // 2. Let y be ? ToIntegerOrInfinity(isoYear).
  42. auto y = vm.argument(0).to_integer_or_infinity(global_object);
  43. if (vm.exception())
  44. return {};
  45. // 3. If y is +∞ or -∞, throw a RangeError exception.
  46. if (Value(y).is_infinity()) {
  47. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  48. return {};
  49. }
  50. // 4. Let m be ? ToIntegerOrInfinity(isoMonth).
  51. auto m = vm.argument(1).to_integer_or_infinity(global_object);
  52. if (vm.exception())
  53. return {};
  54. // 5. If m is +∞ or -∞, throw a RangeError exception.
  55. if (Value(m).is_infinity()) {
  56. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  57. return {};
  58. }
  59. // 6. Let d be ? ToIntegerOrInfinity(isoDay).
  60. auto d = vm.argument(2).to_integer_or_infinity(global_object);
  61. if (vm.exception())
  62. return {};
  63. // 7. If d is +∞ or -∞, throw a RangeError exception.
  64. if (Value(d).is_infinity()) {
  65. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  66. return {};
  67. }
  68. // 8. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
  69. auto* calendar = to_temporal_calendar_with_iso_default(global_object, vm.argument(3));
  70. if (vm.exception())
  71. return {};
  72. // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards.
  73. // This does not change the exposed behaviour as the call to CreateTemporalDate will immediately check that these values are valid
  74. // ISO values (for years: -273975 - 273975, for months: 1 - 12, for days: 1 - 31) all of which are subsets of this check.
  75. if (!AK::is_within_range<i32>(y) || !AK::is_within_range<u8>(m) || !AK::is_within_range<u8>(d)) {
  76. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  77. return {};
  78. }
  79. // 9. Return ? CreateTemporalDate(y, m, d, calendar, NewTarget).
  80. return create_temporal_date(global_object, y, m, d, *calendar, &new_target);
  81. }
  82. // 3.2.2 Temporal.PlainDate.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.from
  83. JS_DEFINE_NATIVE_FUNCTION(PlainDateConstructor::from)
  84. {
  85. // 1. Set options to ? GetOptionsObject(options).
  86. auto* options = get_options_object(global_object, vm.argument(1));
  87. if (vm.exception())
  88. return {};
  89. auto item = vm.argument(0);
  90. // 2. If Type(item) is Object and item has an [[InitializedTemporalDate]] internal slot, then
  91. if (item.is_object() && is<PlainDate>(item.as_object())) {
  92. auto& plain_date_item = static_cast<PlainDate&>(item.as_object());
  93. // a. Perform ? ToTemporalOverflow(options).
  94. (void)to_temporal_overflow(global_object, *options);
  95. if (vm.exception())
  96. return {};
  97. // b. Return ? CreateTemporalDate(item.[[ISOYear]], item.[[ISOMonth]], item.[[ISODay]], item.[[Calendar]]).
  98. return create_temporal_date(global_object, plain_date_item.iso_year(), plain_date_item.iso_month(), plain_date_item.iso_day(), plain_date_item.calendar());
  99. }
  100. // 3. Return ? ToTemporalDate(item, options).
  101. return to_temporal_date(global_object, item, options);
  102. }
  103. // 3.2.3 Temporal.PlainDate.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-properties-of-the-temporal-plaindate-constructor
  104. JS_DEFINE_NATIVE_FUNCTION(PlainDateConstructor::compare)
  105. {
  106. // 1. Set one to ? ToTemporalDate(one).
  107. auto* one = to_temporal_date(global_object, vm.argument(0));
  108. if (vm.exception())
  109. return {};
  110. // 2. Set two to ? ToTemporalDate(two).
  111. auto* two = to_temporal_date(global_object, vm.argument(1));
  112. if (vm.exception())
  113. return {};
  114. // 3. Return 𝔽(! CompareISODate(one.[[ISOYear]], one.[[ISOMonth]], one.[[ISODay]], two.[[ISOYear]], two.[[ISOMonth]], two.[[ISODay]])).
  115. return Value(compare_iso_date(one->iso_year(), one->iso_month(), one->iso_day(), two->iso_year(), two->iso_month(), two->iso_day()));
  116. }
  117. }