PlainTimeConstructor.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2021, 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/PlainTime.h>
  10. #include <LibJS/Runtime/Temporal/PlainTimeConstructor.h>
  11. namespace JS::Temporal {
  12. // 4.1 The Temporal.PlainTime Constructor, https://tc39.es/proposal-temporal/#sec-temporal-plaintime-constructor
  13. PlainTimeConstructor::PlainTimeConstructor(GlobalObject& global_object)
  14. : NativeFunction(vm().names.PlainTime.as_string(), *global_object.function_prototype())
  15. {
  16. }
  17. void PlainTimeConstructor::initialize(GlobalObject& global_object)
  18. {
  19. NativeFunction::initialize(global_object);
  20. auto& vm = this->vm();
  21. // 4.2.1 Temporal.PlainTime.prototype, https://tc39.es/proposal-temporal/#sec-temporal-plaintime-prototype
  22. define_direct_property(vm.names.prototype, global_object.temporal_plain_time_prototype(), 0);
  23. u8 attr = Attribute::Writable | Attribute::Configurable;
  24. define_native_function(vm.names.from, from, 1, attr);
  25. define_native_function(vm.names.compare, compare, 2, attr);
  26. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  27. }
  28. // 4.1.1 Temporal.PlainTime ( [ hour [ , minute [ , second [ , millisecond [ , microsecond [ , nanosecond ] ] ] ] ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime
  29. Value PlainTimeConstructor::call()
  30. {
  31. auto& vm = this->vm();
  32. // 1. If NewTarget is undefined, throw a TypeError exception.
  33. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.PlainTime");
  34. return {};
  35. }
  36. // 4.1.1 Temporal.PlainTime ( [ hour [ , minute [ , second [ , millisecond [ , microsecond [ , nanosecond ] ] ] ] ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime
  37. Value PlainTimeConstructor::construct(FunctionObject& new_target)
  38. {
  39. auto& vm = this->vm();
  40. auto& global_object = this->global_object();
  41. // 2. Let hour be ? ToIntegerThrowOnInfinity(hour).
  42. auto hour = to_integer_throw_on_infinity(global_object, vm.argument(0), ErrorType::TemporalInvalidPlainTime);
  43. if (vm.exception())
  44. return {};
  45. // 3. Let minute be ? ToIntegerThrowOnInfinity(hour).
  46. auto minute = to_integer_throw_on_infinity(global_object, vm.argument(1), ErrorType::TemporalInvalidPlainTime);
  47. if (vm.exception())
  48. return {};
  49. // 4. Let second be ? ToIntegerThrowOnInfinity(hour).
  50. auto second = to_integer_throw_on_infinity(global_object, vm.argument(2), ErrorType::TemporalInvalidPlainTime);
  51. if (vm.exception())
  52. return {};
  53. // 5. Let millisecond be ? ToIntegerThrowOnInfinity(hour).
  54. auto millisecond = to_integer_throw_on_infinity(global_object, vm.argument(3), ErrorType::TemporalInvalidPlainTime);
  55. if (vm.exception())
  56. return {};
  57. // 6. Let microsecond be ? ToIntegerThrowOnInfinity(hour).
  58. auto microsecond = to_integer_throw_on_infinity(global_object, vm.argument(4), ErrorType::TemporalInvalidPlainTime);
  59. if (vm.exception())
  60. return {};
  61. // 7. Let nanosecond be ? ToIntegerThrowOnInfinity(hour).
  62. auto nanosecond = to_integer_throw_on_infinity(global_object, vm.argument(5), ErrorType::TemporalInvalidPlainTime);
  63. if (vm.exception())
  64. return {};
  65. // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards.
  66. // This does not change the exposed behavior as the call to CreateTemporalTime will immediately check that these values are valid
  67. // ISO values (for hours: 0 - 23, for minutes and seconds: 0 - 59, milliseconds, microseconds, and nanoseconds: 0 - 999) all of which
  68. // are subsets of this check.
  69. if (!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)) {
  70. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainTime);
  71. return {};
  72. }
  73. // 8. Return ? CreateTemporalTime(hour, minute, second, millisecond, microsecond, nanosecond, NewTarget).
  74. return TRY_OR_DISCARD(create_temporal_time(global_object, hour, minute, second, millisecond, microsecond, nanosecond, &new_target));
  75. }
  76. // 4.2.2 Temporal.PlainTime.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.from
  77. JS_DEFINE_NATIVE_FUNCTION(PlainTimeConstructor::from)
  78. {
  79. // 1. Set options to ? GetOptionsObject(options).
  80. auto* options = get_options_object(global_object, vm.argument(1));
  81. if (vm.exception())
  82. return {};
  83. // 2. Let overflow be ? ToTemporalOverflow(options).
  84. auto overflow = to_temporal_overflow(global_object, *options);
  85. if (vm.exception())
  86. return {};
  87. auto item = vm.argument(0);
  88. // 3. If Type(item) is Object and item has an [[InitializedTemporalTime]] internal slot, then
  89. if (item.is_object() && is<PlainTime>(item.as_object())) {
  90. auto& plain_time = static_cast<PlainTime&>(item.as_object());
  91. // a. Return ? CreateTemporalTime(item.[[ISOHour]], item.[[ISOMinute]], item.[[ISOSecond]], item.[[ISOMillisecond]], item.[[ISOMicrosecond]], item.[[ISONanosecond]]).
  92. return TRY_OR_DISCARD(create_temporal_time(global_object, plain_time.iso_hour(), plain_time.iso_minute(), plain_time.iso_second(), plain_time.iso_millisecond(), plain_time.iso_microsecond(), plain_time.iso_nanosecond()));
  93. }
  94. // 4. Return ? ToTemporalTime(item, overflow).
  95. return TRY_OR_DISCARD(to_temporal_time(global_object, item, *overflow));
  96. }
  97. // 4.2.3 Temporal.PlainTime.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.compare
  98. JS_DEFINE_NATIVE_FUNCTION(PlainTimeConstructor::compare)
  99. {
  100. // 1. Set one to ? ToTemporalTime(one).
  101. auto* one = TRY_OR_DISCARD(to_temporal_time(global_object, vm.argument(0)));
  102. // 2. Set two to ? ToTemporalTime(two).
  103. auto* two = TRY_OR_DISCARD(to_temporal_time(global_object, vm.argument(1)));
  104. // 3. Return 𝔽(! CompareTemporalTime(one.[[ISOHour]], one.[[ISOMinute]], one.[[ISOSecond]], one.[[ISOMillisecond]], one.[[ISOMicrosecond]], one.[[ISONanosecond]], two.[[ISOHour]], two.[[ISOMinute]], two.[[ISOSecond]], two.[[ISOMillisecond]], two.[[ISOMicrosecond]], two.[[ISONanosecond]])).
  105. return Value(compare_temporal_time(one->iso_hour(), one->iso_minute(), one->iso_second(), one->iso_millisecond(), one->iso_microsecond(), one->iso_nanosecond(), two->iso_hour(), two->iso_minute(), two->iso_second(), two->iso_millisecond(), two->iso_microsecond(), two->iso_nanosecond()));
  106. }
  107. }