PlainDateTimeConstructor.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2021-2023, 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. JS_DEFINE_ALLOCATOR(PlainDateTimeConstructor);
  15. // 5.1 The Temporal.PlainDateTime Constructor, https://tc39.es/proposal-temporal/#sec-temporal-plaindatetime-constructor
  16. PlainDateTimeConstructor::PlainDateTimeConstructor(Realm& realm)
  17. : NativeFunction(realm.vm().names.PlainDateTime.as_string(), realm.intrinsics().function_prototype())
  18. {
  19. }
  20. void PlainDateTimeConstructor::initialize(Realm& realm)
  21. {
  22. Base::initialize(realm);
  23. auto& vm = this->vm();
  24. // 5.2.1 Temporal.PlainDateTime.prototype, https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype
  25. define_direct_property(vm.names.prototype, realm.intrinsics().temporal_plain_date_time_prototype(), 0);
  26. u8 attr = Attribute::Writable | Attribute::Configurable;
  27. define_native_function(realm, vm.names.from, from, 1, attr);
  28. define_native_function(realm, vm.names.compare, compare, 2, attr);
  29. define_direct_property(vm.names.length, Value(3), Attribute::Configurable);
  30. }
  31. // 5.1.1 Temporal.PlainDateTime ( isoYear, isoMonth, isoDay [ , hour [ , minute [ , second [ , millisecond [ , microsecond [ , nanosecond [ , calendarLike ] ] ] ] ] ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime
  32. ThrowCompletionOr<Value> PlainDateTimeConstructor::call()
  33. {
  34. auto& vm = this->vm();
  35. // 1. If NewTarget is undefined, throw a TypeError exception.
  36. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Temporal.PlainDateTime");
  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. ThrowCompletionOr<NonnullGCPtr<Object>> PlainDateTimeConstructor::construct(FunctionObject& new_target)
  40. {
  41. auto& vm = this->vm();
  42. // 2. Let isoYear be ? ToIntegerWithTruncation(isoYear).
  43. auto iso_year = TRY(to_integer_with_truncation(vm, vm.argument(0), ErrorType::TemporalInvalidPlainDateTime));
  44. // 3. Let isoMonth be ? ToIntegerWithTruncation(isoMonth).
  45. auto iso_month = TRY(to_integer_with_truncation(vm, vm.argument(1), ErrorType::TemporalInvalidPlainDateTime));
  46. // 4. Let isoDay be ? ToIntegerWithTruncation(isoDay).
  47. auto iso_day = TRY(to_integer_with_truncation(vm, vm.argument(2), ErrorType::TemporalInvalidPlainDateTime));
  48. // 5. Let hour be ? ToIntegerWithTruncation(hour).
  49. auto hour = TRY(to_integer_with_truncation(vm, vm.argument(3), ErrorType::TemporalInvalidPlainDateTime));
  50. // 6. Let minute be ? ToIntegerWithTruncation(minute).
  51. auto minute = TRY(to_integer_with_truncation(vm, vm.argument(4), ErrorType::TemporalInvalidPlainDateTime));
  52. // 7. Let second be ? ToIntegerWithTruncation(second).
  53. auto second = TRY(to_integer_with_truncation(vm, vm.argument(5), ErrorType::TemporalInvalidPlainDateTime));
  54. // 8. Let millisecond be ? ToIntegerWithTruncation(millisecond).
  55. auto millisecond = TRY(to_integer_with_truncation(vm, vm.argument(6), ErrorType::TemporalInvalidPlainDateTime));
  56. // 9. Let microsecond be ? ToIntegerWithTruncation(microsecond).
  57. auto microsecond = TRY(to_integer_with_truncation(vm, vm.argument(7), ErrorType::TemporalInvalidPlainDateTime));
  58. // 10. Let nanosecond be ? ToIntegerWithTruncation(nanosecond).
  59. auto nanosecond = TRY(to_integer_with_truncation(vm, vm.argument(8), ErrorType::TemporalInvalidPlainDateTime));
  60. // 11. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
  61. auto* calendar = TRY(to_temporal_calendar_with_iso_default(vm, vm.argument(9)));
  62. // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards.
  63. // This does not change the exposed behavior as the call to CreateTemporalDateTime will immediately check that these values are valid
  64. // ISO values (for years: -273975 - 273975, for months: 1 - 12, for days: 1 - 31, for hours: 0 - 23, for minutes and seconds: 0 - 59,
  65. // milliseconds, microseconds, and nanoseconds: 0 - 999) all of which are subsets of this check.
  66. 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))
  67. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidPlainDateTime);
  68. // 12. Return ? CreateTemporalDateTime(isoYear, isoMonth, isoDay, hour, minute, second, millisecond, microsecond, nanosecond, calendar, NewTarget).
  69. return *TRY(create_temporal_date_time(vm, iso_year, iso_month, iso_day, hour, minute, second, millisecond, microsecond, nanosecond, *calendar, &new_target));
  70. }
  71. // 5.2.2 Temporal.PlainDateTime.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.from
  72. JS_DEFINE_NATIVE_FUNCTION(PlainDateTimeConstructor::from)
  73. {
  74. auto item = vm.argument(0);
  75. // 1. Set options to ? GetOptionsObject(options).
  76. auto* options = TRY(get_options_object(vm, vm.argument(1)));
  77. // 2. If Type(item) is Object and item has an [[InitializedTemporalDateTime]] internal slot, then
  78. if (item.is_object() && is<PlainDateTime>(item.as_object())) {
  79. auto& plain_date_time = static_cast<PlainDateTime&>(item.as_object());
  80. // a. Perform ? ToTemporalOverflow(options).
  81. (void)TRY(to_temporal_overflow(vm, options));
  82. // b. Return ! CreateTemporalDateTime(item.[[ISOYear]], item.[[ISOMonth]], item.[[ISODay]], item.[[ISOHour]], item.[[ISOMinute]], item.[[ISOSecond]], item.[[ISOMillisecond]], item.[[ISOMicrosecond]], item.[[ISONanosecond]], item.[[Calendar]]).
  83. return MUST(create_temporal_date_time(vm, 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()));
  84. }
  85. // 3. Return ? ToTemporalDateTime(item, options).
  86. return TRY(to_temporal_date_time(vm, item, options));
  87. }
  88. // 5.2.3 Temporal.PlainDateTime.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.compare
  89. JS_DEFINE_NATIVE_FUNCTION(PlainDateTimeConstructor::compare)
  90. {
  91. // 1. Set one to ? ToTemporalDateTime(one).
  92. auto* one = TRY(to_temporal_date_time(vm, vm.argument(0)));
  93. // 2. Set two to ? ToTemporalDateTime(two).
  94. auto* two = TRY(to_temporal_date_time(vm, vm.argument(1)));
  95. // 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]])).
  96. 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()));
  97. }
  98. }