DurationConstructor.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/TypeCasts.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  10. #include <LibJS/Runtime/Temporal/Duration.h>
  11. #include <LibJS/Runtime/Temporal/DurationConstructor.h>
  12. namespace JS::Temporal {
  13. // 7.1 The Temporal.Duration Constructor, https://tc39.es/proposal-temporal/#sec-temporal-duration-constructor
  14. DurationConstructor::DurationConstructor(GlobalObject& global_object)
  15. : NativeFunction(vm().names.Duration.as_string(), *global_object.function_prototype())
  16. {
  17. }
  18. void DurationConstructor::initialize(GlobalObject& global_object)
  19. {
  20. NativeFunction::initialize(global_object);
  21. auto& vm = this->vm();
  22. // 7.2.1 Temporal.Duration.prototype, https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype
  23. define_direct_property(vm.names.prototype, global_object.temporal_duration_prototype(), 0);
  24. u8 attr = Attribute::Writable | Attribute::Configurable;
  25. define_native_function(vm.names.from, from, 1, attr);
  26. define_native_function(vm.names.compare, compare, 2, attr);
  27. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  28. }
  29. // 7.1.1 Temporal.Duration ( [ years [ , months [ , weeks [ , days [ , hours [ , minutes [ , seconds [ , milliseconds [ , microseconds [ , nanoseconds ] ] ] ] ] ] ] ] ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration
  30. ThrowCompletionOr<Value> DurationConstructor::call()
  31. {
  32. auto& vm = this->vm();
  33. // 1. If NewTarget is undefined, then
  34. // a. Throw a TypeError exception.
  35. return vm.throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.Duration");
  36. }
  37. // 7.1.1 Temporal.Duration ( [ years [ , months [ , weeks [ , days [ , hours [ , minutes [ , seconds [ , milliseconds [ , microseconds [ , nanoseconds ] ] ] ] ] ] ] ] ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration
  38. ThrowCompletionOr<Object*> DurationConstructor::construct(FunctionObject& new_target)
  39. {
  40. auto& vm = this->vm();
  41. auto& global_object = this->global_object();
  42. // 2. Let y be ? ToIntegerWithoutRounding(years).
  43. auto y = TRY(to_integer_without_rounding(global_object, vm.argument(0), ErrorType::TemporalInvalidDuration));
  44. // 3. Let mo be ? ToIntegerWithoutRounding(months).
  45. auto mo = TRY(to_integer_without_rounding(global_object, vm.argument(1), ErrorType::TemporalInvalidDuration));
  46. // 4. Let w be ? ToIntegerWithoutRounding(weeks).
  47. auto w = TRY(to_integer_without_rounding(global_object, vm.argument(2), ErrorType::TemporalInvalidDuration));
  48. // 5. Let d be ? ToIntegerWithoutRounding(days).
  49. auto d = TRY(to_integer_without_rounding(global_object, vm.argument(3), ErrorType::TemporalInvalidDuration));
  50. // 6. Let h be ? ToIntegerWithoutRounding(hours).
  51. auto h = TRY(to_integer_without_rounding(global_object, vm.argument(4), ErrorType::TemporalInvalidDuration));
  52. // 7. Let m be ? ToIntegerWithoutRounding(minutes).
  53. auto m = TRY(to_integer_without_rounding(global_object, vm.argument(5), ErrorType::TemporalInvalidDuration));
  54. // 8. Let s be ? ToIntegerWithoutRounding(seconds).
  55. auto s = TRY(to_integer_without_rounding(global_object, vm.argument(6), ErrorType::TemporalInvalidDuration));
  56. // 9. Let ms be ? ToIntegerWithoutRounding(milliseconds).
  57. auto ms = TRY(to_integer_without_rounding(global_object, vm.argument(7), ErrorType::TemporalInvalidDuration));
  58. // 10. Let mis be ? ToIntegerWithoutRounding(microseconds).
  59. auto mis = TRY(to_integer_without_rounding(global_object, vm.argument(8), ErrorType::TemporalInvalidDuration));
  60. // 11. Let ns be ? ToIntegerWithoutRounding(nanoseconds).
  61. auto ns = TRY(to_integer_without_rounding(global_object, vm.argument(9), ErrorType::TemporalInvalidDuration));
  62. // 12. Return ? CreateTemporalDuration(y, mo, w, d, h, m, s, ms, mis, ns, NewTarget).
  63. return TRY(create_temporal_duration(global_object, y, mo, w, d, h, m, s, ms, mis, ns, &new_target));
  64. }
  65. // 7.2.2 Temporal.Duration.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.duration.from
  66. JS_DEFINE_NATIVE_FUNCTION(DurationConstructor::from)
  67. {
  68. auto item = vm.argument(0);
  69. // 1. If Type(item) is Object and item has an [[InitializedTemporalDuration]] internal slot, then
  70. if (item.is_object() && is<Duration>(item.as_object())) {
  71. auto& duration = static_cast<Duration&>(item.as_object());
  72. // a. Return ? CreateTemporalDuration(item.[[Years]], item.[[Months]], item.[[Weeks]], item.[[Days]], item.[[Hours]], item.[[Minutes]], item.[[Seconds]], item.[[Milliseconds]], item.[[Microseconds]], item.[[Nanoseconds]]).
  73. return TRY(create_temporal_duration(global_object, duration.years(), duration.months(), duration.weeks(), duration.days(), duration.hours(), duration.minutes(), duration.seconds(), duration.milliseconds(), duration.microseconds(), duration.nanoseconds()));
  74. }
  75. // 2. Return ? ToTemporalDuration(item).
  76. return TRY(to_temporal_duration(global_object, item));
  77. }
  78. // 7.2.3 Temporal.Duration.compare ( one, two [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration.compare
  79. JS_DEFINE_NATIVE_FUNCTION(DurationConstructor::compare)
  80. {
  81. // 1. Set one to ? ToTemporalDuration(one).
  82. auto* one = TRY(to_temporal_duration(global_object, vm.argument(0)));
  83. // 2. Set two to ? ToTemporalDuration(two).
  84. auto* two = TRY(to_temporal_duration(global_object, vm.argument(1)));
  85. // 3. Set options to ? GetOptionsObject(options).
  86. auto const* options = TRY(get_options_object(global_object, vm.argument(2)));
  87. // 4. Let relativeTo be ? ToRelativeTemporalObject(options).
  88. auto relative_to = TRY(to_relative_temporal_object(global_object, *options));
  89. // 5. Let shift1 be ? CalculateOffsetShift(relativeTo, one.[[Years]], one.[[Months]], one.[[Weeks]], one.[[Days]], 0, 0, 0, 0, 0, 0, 0).
  90. auto shift1 = TRY(calculate_offset_shift(global_object, relative_to, one->years(), one->months(), one->weeks(), one->days(), 0, 0, 0, 0, 0, 0));
  91. // 6. Let shift2 be ? CalculateOffsetShift(relativeTo, two.[[Years]], two.[[Months]], two.[[Weeks]], two.[[Days]], 0, 0, 0, 0, 0, 0, 0).
  92. auto shift2 = TRY(calculate_offset_shift(global_object, relative_to, two->years(), two->months(), two->weeks(), two->days(), 0, 0, 0, 0, 0, 0));
  93. double days1;
  94. double days2;
  95. // 7. If any of one.[[Years]], two.[[Years]], one.[[Months]], two.[[Months]], one.[[Weeks]], or two.[[Weeks]] are not 0, then
  96. if (one->years() != 0 || two->years() != 0 || one->months() != 0 || two->months() != 0 || one->weeks() != 0 || two->weeks() != 0) {
  97. // a. Let unbalanceResult1 be ? UnbalanceDurationRelative(one.[[Years]], one.[[Months]], one.[[Weeks]], one.[[Days]], "day", relativeTo).
  98. auto unbalance_result1 = TRY(unbalance_duration_relative(global_object, one->years(), one->months(), one->weeks(), one->days(), "day", relative_to));
  99. // b. Let unbalanceResult2 be ? UnbalanceDurationRelative(two.[[Years]], two.[[Months]], two.[[Weeks]], two.[[Days]], "day", relativeTo).
  100. auto unbalance_result2 = TRY(unbalance_duration_relative(global_object, two->years(), two->months(), two->weeks(), two->days(), "day", relative_to));
  101. // c. Let days1 be unbalanceResult1.[[Days]].
  102. days1 = unbalance_result1.days;
  103. // d. Let days2 be unbalanceResult2.[[Days]].
  104. days2 = unbalance_result2.days;
  105. }
  106. // 8. Else,
  107. else {
  108. // a. Let days1 be one.[[Days]].
  109. days1 = one->days();
  110. // b. Let days2 be two.[[Days]].
  111. days2 = two->days();
  112. }
  113. // 9. Let ns1 be ! TotalDurationNanoseconds(days1, one.[[Hours]], one.[[Minutes]], one.[[Seconds]], one.[[Milliseconds]], one.[[Microseconds]], one.[[Nanoseconds]], shift1).
  114. auto ns1 = total_duration_nanoseconds(days1, one->hours(), one->minutes(), one->seconds(), one->milliseconds(), one->microseconds(), Crypto::SignedBigInteger::create_from((i64)one->nanoseconds()), shift1);
  115. // 10. Let ns2 be ! TotalDurationNanoseconds(days2, two.[[Hours]], two.[[Minutes]], two.[[Seconds]], two.[[Milliseconds]], two.[[Microseconds]], two.[[Nanoseconds]], shift2).
  116. auto ns2 = total_duration_nanoseconds(days2, two->hours(), two->minutes(), two->seconds(), two->milliseconds(), two->microseconds(), Crypto::SignedBigInteger::create_from((i64)two->nanoseconds()), shift2);
  117. // 11. If ns1 > ns2, return 1𝔽.
  118. if (ns1 > ns2)
  119. return Value(1);
  120. // 12. If ns1 < ns2, return -1𝔽.
  121. if (ns1 < ns2)
  122. return Value(-1);
  123. // 13. Return +0𝔽.
  124. return Value(0);
  125. }
  126. }