PlainDateConstructor.cpp 5.9 KB

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