PlainTimeConstructor.cpp 6.1 KB

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