PlainMonthDayConstructor.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TypeCasts.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  9. #include <LibJS/Runtime/Temporal/Calendar.h>
  10. #include <LibJS/Runtime/Temporal/PlainMonthDay.h>
  11. #include <LibJS/Runtime/Temporal/PlainMonthDayConstructor.h>
  12. namespace JS::Temporal {
  13. // 10.1 The Temporal.PlainMonthDay Constructor, https://tc39.es/proposal-temporal/#sec-temporal-plainmonthday-constructor
  14. PlainMonthDayConstructor::PlainMonthDayConstructor(Realm& realm)
  15. : NativeFunction(realm.vm().names.PlainMonthDay.as_string(), *realm.intrinsics().function_prototype())
  16. {
  17. }
  18. void PlainMonthDayConstructor::initialize(Realm& realm)
  19. {
  20. NativeFunction::initialize(realm);
  21. auto& vm = this->vm();
  22. // 10.2.1 Temporal.PlainMonthDay.prototype, https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype
  23. define_direct_property(vm.names.prototype, realm.intrinsics().temporal_plain_month_day_prototype(), 0);
  24. define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  25. u8 attr = Attribute::Writable | Attribute::Configurable;
  26. define_native_function(realm, vm.names.from, from, 1, attr);
  27. }
  28. // 10.1.1 Temporal.PlainMonthDay ( isoMonth, isoDay [ , calendarLike [ , referenceISOYear ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday
  29. ThrowCompletionOr<Value> PlainMonthDayConstructor::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.PlainMonthDay");
  34. }
  35. // 10.1.1 Temporal.PlainMonthDay ( isoMonth, isoDay [ , calendarLike [ , referenceISOYear ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday
  36. ThrowCompletionOr<Object*> PlainMonthDayConstructor::construct(FunctionObject& new_target)
  37. {
  38. auto& vm = this->vm();
  39. auto iso_month = vm.argument(0);
  40. auto iso_day = vm.argument(1);
  41. auto calendar_like = vm.argument(2);
  42. auto reference_iso_year = vm.argument(3);
  43. // 2. If referenceISOYear is undefined, then
  44. if (reference_iso_year.is_undefined()) {
  45. // a. Set referenceISOYear to 1972𝔽.
  46. reference_iso_year = Value(1972);
  47. }
  48. // 3. Let m be ? ToIntegerThrowOnInfinity(isoMonth).
  49. auto m = TRY(to_integer_throw_on_infinity(vm, iso_month, ErrorType::TemporalInvalidPlainMonthDay));
  50. // 4. Let d be ? ToIntegerThrowOnInfinity(isoDay).
  51. auto d = TRY(to_integer_throw_on_infinity(vm, iso_day, ErrorType::TemporalInvalidPlainMonthDay));
  52. // 5. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
  53. auto* calendar = TRY(to_temporal_calendar_with_iso_default(vm, calendar_like));
  54. // 6. Let ref be ? ToIntegerThrowOnInfinity(referenceISOYear).
  55. auto ref = TRY(to_integer_throw_on_infinity(vm, reference_iso_year, ErrorType::TemporalInvalidPlainMonthDay));
  56. // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards.
  57. // This does not change the exposed behavior as the call to CreateTemporalMonthDay will immediately check that these values are valid
  58. // ISO values (for years: -273975 - 273975, for months: 1 - 12, for days: 1 - 31) all of which are subsets of this check.
  59. if (!AK::is_within_range<i32>(ref) || !AK::is_within_range<u8>(m) || !AK::is_within_range<u8>(d))
  60. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidPlainMonthDay);
  61. // 7. Return ? CreateTemporalMonthDay(m, d, calendar, ref, NewTarget).
  62. return TRY(create_temporal_month_day(vm, m, d, *calendar, ref, &new_target));
  63. }
  64. // 10.2.2 Temporal.PlainMonthDay.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.from
  65. JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayConstructor::from)
  66. {
  67. auto item = vm.argument(0);
  68. // 1. Set options to ? GetOptionsObject(options).
  69. auto const* options = TRY(get_options_object(vm, vm.argument(1)));
  70. // 2. If Type(item) is Object and item has an [[InitializedTemporalMonthDay]] internal slot, then
  71. if (item.is_object() && is<PlainMonthDay>(item.as_object())) {
  72. // a. Perform ? ToTemporalOverflow(options).
  73. (void)TRY(to_temporal_overflow(vm, options));
  74. auto& plain_month_day_object = static_cast<PlainMonthDay&>(item.as_object());
  75. // b. Return ! CreateTemporalMonthDay(item.[[ISOMonth]], item.[[ISODay]], item.[[Calendar]], item.[[ISOYear]]).
  76. return MUST(create_temporal_month_day(vm, plain_month_day_object.iso_month(), plain_month_day_object.iso_day(), plain_month_day_object.calendar(), plain_month_day_object.iso_year()));
  77. }
  78. // 3. Return ? ToTemporalMonthDay(item, options).
  79. return TRY(to_temporal_month_day(vm, item, options));
  80. }
  81. }