PlainDateConstructor.cpp 5.9 KB

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