PlainDateTimeConstructor.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@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/PlainDateTime.h>
  12. #include <LibJS/Runtime/Temporal/PlainDateTimeConstructor.h>
  13. namespace JS::Temporal {
  14. // 5.1 The Temporal.PlainDateTime Constructor, https://tc39.es/proposal-temporal/#sec-temporal-plaindatetime-constructor
  15. PlainDateTimeConstructor::PlainDateTimeConstructor(GlobalObject& global_object)
  16. : NativeFunction(vm().names.PlainDateTime.as_string(), *global_object.function_prototype())
  17. {
  18. }
  19. void PlainDateTimeConstructor::initialize(GlobalObject& global_object)
  20. {
  21. NativeFunction::initialize(global_object);
  22. auto& vm = this->vm();
  23. // 5.2.1 Temporal.PlainDateTime.prototype, https://tc39.es/proposal-temporal/#sec-temporal-plaindatetime-prototype
  24. define_direct_property(vm.names.prototype, global_object.temporal_plain_date_time_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. // 5.1.1 Temporal.PlainDateTime ( isoYear, isoMonth, isoDay [ , hour [ , minute [ , second [ , millisecond [ , microsecond [ , nanosecond [ , calendarLike ] ] ] ] ] ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime
  31. Value PlainDateTimeConstructor::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.PlainDateTime");
  36. return {};
  37. }
  38. // 5.1.1 Temporal.PlainDateTime ( isoYear, isoMonth, isoDay [ , hour [ , minute [ , second [ , millisecond [ , microsecond [ , nanosecond [ , calendarLike ] ] ] ] ] ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime
  39. Value PlainDateTimeConstructor::construct(FunctionObject& new_target)
  40. {
  41. auto& vm = this->vm();
  42. auto& global_object = this->global_object();
  43. // 2. Let isoYear be ? ToIntegerThrowOnInfinity(isoYear).
  44. auto iso_year = to_integer_throw_on_infinity(global_object, vm.argument(0), ErrorType::TemporalInvalidPlainDateTime);
  45. if (vm.exception())
  46. return {};
  47. // 3. Let isoMonth be ? ToIntegerThrowOnInfinity(isoMonth).
  48. auto iso_month = to_integer_throw_on_infinity(global_object, vm.argument(1), ErrorType::TemporalInvalidPlainDateTime);
  49. if (vm.exception())
  50. return {};
  51. // 4. Let isoDay be ? ToIntegerThrowOnInfinity(isoDay).
  52. auto iso_day = to_integer_throw_on_infinity(global_object, vm.argument(2), ErrorType::TemporalInvalidPlainDateTime);
  53. if (vm.exception())
  54. return {};
  55. // 5. Let hour be ? ToIntegerThrowOnInfinity(hour).
  56. auto hour = to_integer_throw_on_infinity(global_object, vm.argument(3), ErrorType::TemporalInvalidPlainDateTime);
  57. if (vm.exception())
  58. return {};
  59. // 6. Let minute be ? ToIntegerThrowOnInfinity(minute).
  60. auto minute = to_integer_throw_on_infinity(global_object, vm.argument(4), ErrorType::TemporalInvalidPlainDateTime);
  61. if (vm.exception())
  62. return {};
  63. // 7. Let second be ? ToIntegerThrowOnInfinity(second).
  64. auto second = to_integer_throw_on_infinity(global_object, vm.argument(5), ErrorType::TemporalInvalidPlainDateTime);
  65. if (vm.exception())
  66. return {};
  67. // 8. Let millisecond be ? ToIntegerThrowOnInfinity(millisecond).
  68. auto millisecond = to_integer_throw_on_infinity(global_object, vm.argument(6), ErrorType::TemporalInvalidPlainDateTime);
  69. if (vm.exception())
  70. return {};
  71. // 9. Let microsecond be ? ToIntegerThrowOnInfinity(microsecond).
  72. auto microsecond = to_integer_throw_on_infinity(global_object, vm.argument(7), ErrorType::TemporalInvalidPlainDateTime);
  73. if (vm.exception())
  74. return {};
  75. // 10. Let nanosecond be ? ToIntegerThrowOnInfinity(nanosecond).
  76. auto nanosecond = to_integer_throw_on_infinity(global_object, vm.argument(8), ErrorType::TemporalInvalidPlainDateTime);
  77. if (vm.exception())
  78. return {};
  79. // 11. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
  80. auto* calendar = to_temporal_calendar_with_iso_default(global_object, vm.argument(9));
  81. if (vm.exception())
  82. return {};
  83. // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards.
  84. // This does not change the exposed behavior as the call to CreateTemporalDateTime will immediately check that these values are valid
  85. // ISO values (for years: -273975 - 273975, for months: 1 - 12, for days: 1 - 31, for hours: 0 - 23, for minutes and seconds: 0 - 59,
  86. // milliseconds, microseconds, and nanoseconds: 0 - 999) all of which are subsets of this check.
  87. if (!AK::is_within_range<i32>(iso_year) || !AK::is_within_range<u8>(iso_month) || !AK::is_within_range<u8>(iso_day) || !AK::is_within_range<u8>(hour) || !AK::is_within_range<u8>(minute) || !AK::is_within_range<u8>(second) || !AK::is_within_range<u16>(millisecond) || !AK::is_within_range<u16>(microsecond) || !AK::is_within_range<u16>(nanosecond)) {
  88. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDateTime);
  89. return {};
  90. }
  91. // 12. Return ? CreateTemporalDateTime(isoYear, isoMonth, isoDay, hour, minute, second, millisecond, microsecond, nanosecond, calendar, NewTarget).
  92. return create_temporal_date_time(global_object, iso_year, iso_month, iso_day, hour, minute, second, millisecond, microsecond, nanosecond, *calendar, &new_target);
  93. }
  94. // 5.2.2 Temporal.PlainDateTime.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.from
  95. JS_DEFINE_NATIVE_FUNCTION(PlainDateTimeConstructor::from)
  96. {
  97. auto item = vm.argument(0);
  98. // 1. Set options to ? GetOptionsObject(options).
  99. auto* options = get_options_object(global_object, vm.argument(1));
  100. if (vm.exception())
  101. return {};
  102. // 2. If Type(item) is Object and item has an [[InitializedTemporalDateTime]] internal slot, then
  103. if (item.is_object() && is<PlainDateTime>(item.as_object())) {
  104. auto& plain_date_time = static_cast<PlainDateTime&>(item.as_object());
  105. // a. Perform ? ToTemporalOverflow(options).
  106. (void)to_temporal_overflow(global_object, *options);
  107. if (vm.exception())
  108. return {};
  109. // b. Return ? CreateTemporalDateTime(item.[[ISOYear]], item.[[ISOMonth]], item.[[ISODay]], item.[[ISOHour]], item.[[ISOMinute]], item.[[ISOSecond]], item.[[ISOMillisecond]], item.[[ISOMicrosecond]], item.[[ISONanosecond]], item.[[Calendar]]).
  110. return create_temporal_date_time(global_object, plain_date_time.iso_year(), plain_date_time.iso_month(), plain_date_time.iso_day(), plain_date_time.iso_hour(), plain_date_time.iso_minute(), plain_date_time.iso_second(), plain_date_time.iso_millisecond(), plain_date_time.iso_microsecond(), plain_date_time.iso_nanosecond(), plain_date_time.calendar());
  111. }
  112. // 3. Return ? ToTemporalDateTime(item, options).
  113. return to_temporal_date_time(global_object, item, options);
  114. }
  115. // 5.2.3 Temporal.PlainDateTime.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.compare
  116. JS_DEFINE_NATIVE_FUNCTION(PlainDateTimeConstructor::compare)
  117. {
  118. // 1. Set one to ? ToTemporalDateTime(one).
  119. auto* one = to_temporal_date_time(global_object, vm.argument(0));
  120. if (vm.exception())
  121. return {};
  122. // 2. Set two to ? ToTemporalDateTime(two).
  123. auto two = to_temporal_date_time(global_object, vm.argument(1));
  124. if (vm.exception())
  125. return {};
  126. // 3. Return 𝔽(! CompareISODateTime(one.[[ISOYear]], one.[[ISOMonth]], one.[[ISODay]], one.[[ISOHour]], one.[[ISOMinute]], one.[[ISOSecond]], one.[[ISOMillisecond]], one.[[ISOMicrosecond]], one.[[ISONanosecond]], two.[[ISOYear]], two.[[ISOMonth]], two.[[ISODay]], two.[[ISOHour]], two.[[ISOMinute]], two.[[ISOSecond]], two.[[ISOMillisecond]], two.[[ISOMicrosecond]], two.[[ISONanosecond]])).
  127. return Value(compare_iso_date_time(one->iso_year(), one->iso_month(), one->iso_day(), one->iso_hour(), one->iso_minute(), one->iso_second(), one->iso_millisecond(), one->iso_microsecond(), one->iso_nanosecond(), two->iso_year(), two->iso_month(), two->iso_day(), two->iso_hour(), two->iso_minute(), two->iso_second(), two->iso_millisecond(), two->iso_microsecond(), two->iso_nanosecond()));
  128. }
  129. }