DurationConstructor.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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(Realm& realm)
  15. : NativeFunction(realm.vm().names.Duration.as_string(), *realm.intrinsics().function_prototype())
  16. {
  17. }
  18. void DurationConstructor::initialize(Realm& realm)
  19. {
  20. NativeFunction::initialize(realm);
  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, realm.intrinsics().temporal_duration_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. // 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>(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. // 2. Let y be ? ToIntegerWithoutRounding(years).
  42. auto y = TRY(to_integer_without_rounding(vm, vm.argument(0), ErrorType::TemporalInvalidDuration));
  43. // 3. Let mo be ? ToIntegerWithoutRounding(months).
  44. auto mo = TRY(to_integer_without_rounding(vm, vm.argument(1), ErrorType::TemporalInvalidDuration));
  45. // 4. Let w be ? ToIntegerWithoutRounding(weeks).
  46. auto w = TRY(to_integer_without_rounding(vm, vm.argument(2), ErrorType::TemporalInvalidDuration));
  47. // 5. Let d be ? ToIntegerWithoutRounding(days).
  48. auto d = TRY(to_integer_without_rounding(vm, vm.argument(3), ErrorType::TemporalInvalidDuration));
  49. // 6. Let h be ? ToIntegerWithoutRounding(hours).
  50. auto h = TRY(to_integer_without_rounding(vm, vm.argument(4), ErrorType::TemporalInvalidDuration));
  51. // 7. Let m be ? ToIntegerWithoutRounding(minutes).
  52. auto m = TRY(to_integer_without_rounding(vm, vm.argument(5), ErrorType::TemporalInvalidDuration));
  53. // 8. Let s be ? ToIntegerWithoutRounding(seconds).
  54. auto s = TRY(to_integer_without_rounding(vm, vm.argument(6), ErrorType::TemporalInvalidDuration));
  55. // 9. Let ms be ? ToIntegerWithoutRounding(milliseconds).
  56. auto ms = TRY(to_integer_without_rounding(vm, vm.argument(7), ErrorType::TemporalInvalidDuration));
  57. // 10. Let mis be ? ToIntegerWithoutRounding(microseconds).
  58. auto mis = TRY(to_integer_without_rounding(vm, vm.argument(8), ErrorType::TemporalInvalidDuration));
  59. // 11. Let ns be ? ToIntegerWithoutRounding(nanoseconds).
  60. auto ns = TRY(to_integer_without_rounding(vm, vm.argument(9), ErrorType::TemporalInvalidDuration));
  61. // 12. Return ? CreateTemporalDuration(y, mo, w, d, h, m, s, ms, mis, ns, NewTarget).
  62. return TRY(create_temporal_duration(vm, y, mo, w, d, h, m, s, ms, mis, ns, &new_target));
  63. }
  64. // 7.2.2 Temporal.Duration.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.duration.from
  65. JS_DEFINE_NATIVE_FUNCTION(DurationConstructor::from)
  66. {
  67. auto item = vm.argument(0);
  68. // 1. If Type(item) is Object and item has an [[InitializedTemporalDuration]] internal slot, then
  69. if (item.is_object() && is<Duration>(item.as_object())) {
  70. auto& duration = static_cast<Duration&>(item.as_object());
  71. // a. Return ! CreateTemporalDuration(item.[[Years]], item.[[Months]], item.[[Weeks]], item.[[Days]], item.[[Hours]], item.[[Minutes]], item.[[Seconds]], item.[[Milliseconds]], item.[[Microseconds]], item.[[Nanoseconds]]).
  72. return MUST(create_temporal_duration(vm, duration.years(), duration.months(), duration.weeks(), duration.days(), duration.hours(), duration.minutes(), duration.seconds(), duration.milliseconds(), duration.microseconds(), duration.nanoseconds()));
  73. }
  74. // 2. Return ? ToTemporalDuration(item).
  75. return TRY(to_temporal_duration(vm, item));
  76. }
  77. // 7.2.3 Temporal.Duration.compare ( one, two [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration.compare
  78. JS_DEFINE_NATIVE_FUNCTION(DurationConstructor::compare)
  79. {
  80. // 1. Set one to ? ToTemporalDuration(one).
  81. auto* one = TRY(to_temporal_duration(vm, vm.argument(0)));
  82. // 2. Set two to ? ToTemporalDuration(two).
  83. auto* two = TRY(to_temporal_duration(vm, vm.argument(1)));
  84. // 3. Set options to ? GetOptionsObject(options).
  85. auto const* options = TRY(get_options_object(vm, vm.argument(2)));
  86. // 4. Let relativeTo be ? ToRelativeTemporalObject(options).
  87. auto relative_to = TRY(to_relative_temporal_object(vm, *options));
  88. // 5. Let shift1 be ? CalculateOffsetShift(relativeTo, one.[[Years]], one.[[Months]], one.[[Weeks]], one.[[Days]]).
  89. auto shift1 = TRY(calculate_offset_shift(vm, relative_to, one->years(), one->months(), one->weeks(), one->days()));
  90. // 6. Let shift2 be ? CalculateOffsetShift(relativeTo, two.[[Years]], two.[[Months]], two.[[Weeks]], two.[[Days]]).
  91. auto shift2 = TRY(calculate_offset_shift(vm, relative_to, two->years(), two->months(), two->weeks(), two->days()));
  92. double days1;
  93. double days2;
  94. // 7. If any of one.[[Years]], two.[[Years]], one.[[Months]], two.[[Months]], one.[[Weeks]], or two.[[Weeks]] are not 0, then
  95. if (one->years() != 0 || two->years() != 0 || one->months() != 0 || two->months() != 0 || one->weeks() != 0 || two->weeks() != 0) {
  96. // a. Let unbalanceResult1 be ? UnbalanceDurationRelative(one.[[Years]], one.[[Months]], one.[[Weeks]], one.[[Days]], "day", relativeTo).
  97. auto unbalance_result1 = TRY(unbalance_duration_relative(vm, one->years(), one->months(), one->weeks(), one->days(), "day", relative_to));
  98. // b. Let unbalanceResult2 be ? UnbalanceDurationRelative(two.[[Years]], two.[[Months]], two.[[Weeks]], two.[[Days]], "day", relativeTo).
  99. auto unbalance_result2 = TRY(unbalance_duration_relative(vm, two->years(), two->months(), two->weeks(), two->days(), "day", relative_to));
  100. // c. Let days1 be unbalanceResult1.[[Days]].
  101. days1 = unbalance_result1.days;
  102. // d. Let days2 be unbalanceResult2.[[Days]].
  103. days2 = unbalance_result2.days;
  104. }
  105. // 8. Else,
  106. else {
  107. // a. Let days1 be one.[[Days]].
  108. days1 = one->days();
  109. // b. Let days2 be two.[[Days]].
  110. days2 = two->days();
  111. }
  112. // 9. Let ns1 be ! TotalDurationNanoseconds(days1, one.[[Hours]], one.[[Minutes]], one.[[Seconds]], one.[[Milliseconds]], one.[[Microseconds]], one.[[Nanoseconds]], shift1).
  113. auto ns1 = total_duration_nanoseconds(days1, one->hours(), one->minutes(), one->seconds(), one->milliseconds(), one->microseconds(), Crypto::SignedBigInteger { one->nanoseconds() }, shift1);
  114. // 10. Let ns2 be ! TotalDurationNanoseconds(days2, two.[[Hours]], two.[[Minutes]], two.[[Seconds]], two.[[Milliseconds]], two.[[Microseconds]], two.[[Nanoseconds]], shift2).
  115. auto ns2 = total_duration_nanoseconds(days2, two->hours(), two->minutes(), two->seconds(), two->milliseconds(), two->microseconds(), Crypto::SignedBigInteger { two->nanoseconds() }, shift2);
  116. // 11. If ns1 > ns2, return 1𝔽.
  117. if (ns1 > ns2)
  118. return Value(1);
  119. // 12. If ns1 < ns2, return -1𝔽.
  120. if (ns1 < ns2)
  121. return Value(-1);
  122. // 13. Return +0𝔽.
  123. return Value(0);
  124. }
  125. }