PlainDateConstructor.cpp 5.2 KB

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