PlainDateConstructor.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2024, Tim Flynn <trflynn89@ladybird.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/Temporal/Calendar.h>
  8. #include <LibJS/Runtime/Temporal/PlainDate.h>
  9. #include <LibJS/Runtime/Temporal/PlainDateConstructor.h>
  10. namespace JS::Temporal {
  11. GC_DEFINE_ALLOCATOR(PlainDateConstructor);
  12. // 3.1 The Temporal.PlainDate Constructor, https://tc39.es/proposal-temporal/#sec-temporal-plaindate-constructor
  13. PlainDateConstructor::PlainDateConstructor(Realm& realm)
  14. : NativeFunction(realm.vm().names.PlainDate.as_string(), realm.intrinsics().function_prototype())
  15. {
  16. }
  17. void PlainDateConstructor::initialize(Realm& realm)
  18. {
  19. Base::initialize(realm);
  20. auto& vm = this->vm();
  21. // 3.2.1 Temporal.PlainDate.prototype, https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype
  22. define_direct_property(vm.names.prototype, realm.intrinsics().temporal_plain_date_prototype(), 0);
  23. u8 attr = Attribute::Writable | Attribute::Configurable;
  24. define_native_function(realm, vm.names.from, from, 1, attr);
  25. define_native_function(realm, vm.names.compare, compare, 2, attr);
  26. define_direct_property(vm.names.length, Value(3), Attribute::Configurable);
  27. }
  28. // 3.1.1 Temporal.PlainDate ( isoYear, isoMonth, isoDay [ , calendar ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate
  29. ThrowCompletionOr<Value> PlainDateConstructor::call()
  30. {
  31. auto& vm = this->vm();
  32. // 1. If NewTarget is undefined, throw a TypeError exception.
  33. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Temporal.PlainDate");
  34. }
  35. // 3.1.1 Temporal.PlainDate ( isoYear, isoMonth, isoDay [ , calendar ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate
  36. ThrowCompletionOr<GC::Ref<Object>> PlainDateConstructor::construct(FunctionObject& new_target)
  37. {
  38. auto& vm = this->vm();
  39. auto iso_year = vm.argument(0);
  40. auto iso_month = vm.argument(1);
  41. auto iso_day = vm.argument(2);
  42. auto calendar_value = vm.argument(3);
  43. // 2. Let y be ? ToIntegerWithTruncation(isoYear).
  44. auto year = TRY(to_integer_with_truncation(vm, iso_year, ErrorType::TemporalInvalidPlainDate));
  45. // 3. Let m be ? ToIntegerWithTruncation(isoMonth).
  46. auto month = TRY(to_integer_with_truncation(vm, iso_month, ErrorType::TemporalInvalidPlainDate));
  47. // 4. Let d be ? ToIntegerWithTruncation(isoDay).
  48. auto day = TRY(to_integer_with_truncation(vm, iso_day, ErrorType::TemporalInvalidPlainDate));
  49. // 5. If calendar is undefined, set calendar to "iso8601".
  50. if (calendar_value.is_undefined())
  51. calendar_value = PrimitiveString::create(vm, "iso8601"_string);
  52. // 6. If calendar is not a String, throw a TypeError exception.
  53. if (!calendar_value.is_string())
  54. return vm.throw_completion<TypeError>(ErrorType::NotAString, "calendar"sv);
  55. // 7. Set calendar to ? CanonicalizeCalendar(calendar).
  56. auto calendar = TRY(canonicalize_calendar(vm, calendar_value.as_string().utf8_string_view()));
  57. // 8. If IsValidISODate(y, m, d) is false, throw a RangeError exception.
  58. if (!is_valid_iso_date(year, month, day))
  59. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidPlainDate);
  60. // 9. Let isoDate be CreateISODateRecord(y, m, d).
  61. auto iso_date = create_iso_date_record(year, month, day);
  62. // 10. Return ? CreateTemporalDate(isoDate, calendar, NewTarget).
  63. return TRY(create_temporal_date(vm, iso_date, move(calendar), &new_target));
  64. }
  65. // 3.2.2 3.2.2 Temporal.PlainDate.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.from
  66. JS_DEFINE_NATIVE_FUNCTION(PlainDateConstructor::from)
  67. {
  68. // 1. Return ? ToTemporalDate(item, options).
  69. return TRY(to_temporal_date(vm, vm.argument(0), vm.argument(1)));
  70. }
  71. // 3.2.3 Temporal.PlainDate.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.compare
  72. JS_DEFINE_NATIVE_FUNCTION(PlainDateConstructor::compare)
  73. {
  74. // 1. Set one to ? ToTemporalDate(one).
  75. auto one = TRY(to_temporal_date(vm, vm.argument(0)));
  76. // 2. Set two to ? ToTemporalDate(two).
  77. auto two = TRY(to_temporal_date(vm, vm.argument(1)));
  78. // 3. Return 𝔽(CompareISODate(one.[[ISODate]], two.[[ISODate]])).
  79. return compare_iso_date(one->iso_date(), two->iso_date());
  80. }
  81. }