PlainDateConstructor.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 ? ToIntegerThrowOnInfinity(isoYear).
  44. auto y = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(0), ErrorType::TemporalInvalidPlainDate));
  45. // 3. Let m be ? ToIntegerThrowOnInfinity(isoMonth).
  46. auto m = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(1), ErrorType::TemporalInvalidPlainDate));
  47. // 4. Let d be ? ToIntegerThrowOnInfinity(isoDay).
  48. auto d = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(2), ErrorType::TemporalInvalidPlainDate));
  49. // 5. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
  50. auto* calendar = TRY_OR_DISCARD(to_temporal_calendar_with_iso_default(global_object, vm.argument(3)));
  51. // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards.
  52. // This does not change the exposed behavior as the call to CreateTemporalDate will immediately check that these values are valid
  53. // ISO values (for years: -273975 - 273975, for months: 1 - 12, for days: 1 - 31) all of which are subsets of this check.
  54. if (!AK::is_within_range<i32>(y) || !AK::is_within_range<u8>(m) || !AK::is_within_range<u8>(d)) {
  55. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  56. return {};
  57. }
  58. // 6. Return ? CreateTemporalDate(y, m, d, calendar, NewTarget).
  59. return TRY_OR_DISCARD(create_temporal_date(global_object, y, m, d, *calendar, &new_target));
  60. }
  61. // 3.2.2 Temporal.PlainDate.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.from
  62. JS_DEFINE_NATIVE_FUNCTION(PlainDateConstructor::from)
  63. {
  64. // 1. Set options to ? GetOptionsObject(options).
  65. auto* options = TRY_OR_DISCARD(get_options_object(global_object, vm.argument(1)));
  66. auto item = vm.argument(0);
  67. // 2. If Type(item) is Object and item has an [[InitializedTemporalDate]] internal slot, then
  68. if (item.is_object() && is<PlainDate>(item.as_object())) {
  69. auto& plain_date_item = static_cast<PlainDate&>(item.as_object());
  70. // a. Perform ? ToTemporalOverflow(options).
  71. (void)TRY_OR_DISCARD(to_temporal_overflow(global_object, *options));
  72. // b. Return ? CreateTemporalDate(item.[[ISOYear]], item.[[ISOMonth]], item.[[ISODay]], item.[[Calendar]]).
  73. return TRY_OR_DISCARD(create_temporal_date(global_object, plain_date_item.iso_year(), plain_date_item.iso_month(), plain_date_item.iso_day(), plain_date_item.calendar()));
  74. }
  75. // 3. Return ? ToTemporalDate(item, options).
  76. return TRY_OR_DISCARD(to_temporal_date(global_object, item, options));
  77. }
  78. // 3.2.3 Temporal.PlainDate.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-properties-of-the-temporal-plaindate-constructor
  79. JS_DEFINE_NATIVE_FUNCTION(PlainDateConstructor::compare)
  80. {
  81. // 1. Set one to ? ToTemporalDate(one).
  82. auto* one = TRY_OR_DISCARD(to_temporal_date(global_object, vm.argument(0)));
  83. // 2. Set two to ? ToTemporalDate(two).
  84. auto* two = TRY_OR_DISCARD(to_temporal_date(global_object, vm.argument(1)));
  85. // 3. Return 𝔽(! CompareISODate(one.[[ISOYear]], one.[[ISOMonth]], one.[[ISODay]], two.[[ISOYear]], two.[[ISOMonth]], two.[[ISODay]])).
  86. return Value(compare_iso_date(one->iso_year(), one->iso_month(), one->iso_day(), two->iso_year(), two->iso_month(), two->iso_day()));
  87. }
  88. }