InstantConstructor.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCrypto/BigInt/UnsignedBigInteger.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Temporal/Instant.h>
  9. #include <LibJS/Runtime/Temporal/InstantConstructor.h>
  10. namespace JS::Temporal {
  11. // 8.1 The Temporal.Instant Constructor, https://tc39.es/proposal-temporal/#sec-temporal-instant-constructor
  12. InstantConstructor::InstantConstructor(GlobalObject& global_object)
  13. : NativeFunction(vm().names.Instant.as_string(), *global_object.function_prototype())
  14. {
  15. }
  16. void InstantConstructor::initialize(GlobalObject& global_object)
  17. {
  18. NativeFunction::initialize(global_object);
  19. auto& vm = this->vm();
  20. // 8.2.1 Temporal.Instant.prototype, https://tc39.es/proposal-temporal/#sec-temporal-instant-prototype
  21. define_direct_property(vm.names.prototype, global_object.temporal_instant_prototype(), 0);
  22. u8 attr = Attribute::Writable | Attribute::Configurable;
  23. define_native_function(vm.names.from, from, 1, attr);
  24. define_native_function(vm.names.fromEpochSeconds, from_epoch_seconds, 1, attr);
  25. define_native_function(vm.names.fromEpochMilliseconds, from_epoch_milliseconds, 1, attr);
  26. define_native_function(vm.names.fromEpochMicroseconds, from_epoch_microseconds, 1, attr);
  27. define_native_function(vm.names.fromEpochNanoseconds, from_epoch_nanoseconds, 1, attr);
  28. define_native_function(vm.names.compare, compare, 2, attr);
  29. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  30. }
  31. // 8.1.1 Temporal.Instant ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant
  32. Value InstantConstructor::call()
  33. {
  34. auto& vm = this->vm();
  35. // 1. If NewTarget is undefined, then
  36. // a. Throw a TypeError exception.
  37. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.Instant");
  38. return {};
  39. }
  40. // 8.1.1 Temporal.Instant ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant
  41. Value InstantConstructor::construct(FunctionObject& new_target)
  42. {
  43. auto& vm = this->vm();
  44. auto& global_object = this->global_object();
  45. // 2. Let epochNanoseconds be ? ToBigInt(epochNanoseconds).
  46. auto* epoch_nanoseconds = vm.argument(0).to_bigint(global_object);
  47. if (vm.exception())
  48. return {};
  49. // 3. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
  50. if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
  51. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  52. return {};
  53. }
  54. // 4. Return ? CreateTemporalInstant(epochNanoseconds, NewTarget).
  55. return create_temporal_instant(global_object, *epoch_nanoseconds, &new_target);
  56. }
  57. // 8.2.2 Temporal.Instant.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.instant.from
  58. JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from)
  59. {
  60. auto item = vm.argument(0);
  61. // 1. If Type(item) is Object and item has an [[InitializedTemporalInstant]] internal slot, then
  62. if (item.is_object() && is<Instant>(item.as_object())) {
  63. // a. Return ! CreateTemporalInstant(item.[[Nanoseconds]]).
  64. return create_temporal_instant(global_object, *js_bigint(vm.heap(), static_cast<Instant&>(item.as_object()).nanoseconds().big_integer()));
  65. }
  66. // 2. Return ? ToTemporalInstant(item).
  67. return to_temporal_instant(global_object, item);
  68. }
  69. // 8.2.3 Temporal.Instant.fromEpochSeconds ( epochSeconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochseconds
  70. JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_seconds)
  71. {
  72. // 1. Set epochSeconds to ? ToNumber(epochSeconds).
  73. auto epoch_seconds_value = vm.argument(0).to_number(global_object);
  74. if (vm.exception())
  75. return {};
  76. // 2. Set epochSeconds to ? NumberToBigInt(epochSeconds).
  77. auto* epoch_seconds = number_to_bigint(global_object, epoch_seconds_value);
  78. if (vm.exception())
  79. return {};
  80. // 3. Let epochNanoseconds be epochSeconds × 10^9ℤ.
  81. auto* epoch_nanoseconds = js_bigint(vm.heap(), epoch_seconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000'000 }));
  82. // 4. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
  83. if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
  84. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  85. return {};
  86. }
  87. // 5. Return ! CreateTemporalInstant(epochNanoseconds).
  88. return create_temporal_instant(global_object, *epoch_nanoseconds);
  89. }
  90. // 8.2.4 Temporal.Instant.fromEpochMilliseconds ( epochMilliseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochmilliseconds
  91. JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_milliseconds)
  92. {
  93. // 1. Set epochMilliseconds to ? ToNumber(epochMilliseconds).
  94. auto epoch_milliseconds_value = vm.argument(0).to_number(global_object);
  95. if (vm.exception())
  96. return {};
  97. // 2. Set epochMilliseconds to ? NumberToBigInt(epochMilliseconds).
  98. auto* epoch_milliseconds = number_to_bigint(global_object, epoch_milliseconds_value);
  99. if (vm.exception())
  100. return {};
  101. // 3. Let epochNanoseconds be epochMilliseconds × 10^6ℤ.
  102. auto* epoch_nanoseconds = js_bigint(vm.heap(), epoch_milliseconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000 }));
  103. // 4. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
  104. if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
  105. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  106. return {};
  107. }
  108. // 5. Return ! CreateTemporalInstant(epochNanoseconds).
  109. return create_temporal_instant(global_object, *epoch_nanoseconds);
  110. }
  111. // 8.2.5 Temporal.Instant.fromEpochMicroseconds ( epochMicroseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochmicroseconds
  112. JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_microseconds)
  113. {
  114. // 1. Set epochMicroseconds to ? ToBigInt(epochMicroseconds).
  115. auto* epoch_microseconds = vm.argument(0).to_bigint(global_object);
  116. if (vm.exception())
  117. return {};
  118. // 2. Let epochNanoseconds be epochMicroseconds × 1000ℤ.
  119. auto* epoch_nanoseconds = js_bigint(vm.heap(), epoch_microseconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000 }));
  120. // 3. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
  121. if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
  122. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  123. return {};
  124. }
  125. // 4. Return ! CreateTemporalInstant(epochNanoseconds).
  126. return create_temporal_instant(global_object, *epoch_nanoseconds);
  127. }
  128. // 8.2.6 Temporal.Instant.fromEpochNanoseconds ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochnanoseconds
  129. JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_nanoseconds)
  130. {
  131. // 1. Set epochNanoseconds to ? ToBigInt(epochNanoseconds).
  132. auto* epoch_nanoseconds = vm.argument(0).to_bigint(global_object);
  133. if (vm.exception())
  134. return {};
  135. // 2. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
  136. if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
  137. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  138. return {};
  139. }
  140. // 3. Return ! CreateTemporalInstant(epochNanoseconds).
  141. return create_temporal_instant(global_object, *epoch_nanoseconds);
  142. }
  143. // 8.2.7 Temporal.Instant.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.instant.compare
  144. JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::compare)
  145. {
  146. // 1. Set one to ? ToTemporalInstant(one).
  147. auto* one = to_temporal_instant(global_object, vm.argument(0));
  148. if (vm.exception())
  149. return {};
  150. // 2. Set two to ? ToTemporalInstant(two).
  151. auto* two = to_temporal_instant(global_object, vm.argument(1));
  152. if (vm.exception())
  153. return {};
  154. // 3. Return 𝔽(! CompareEpochNanoseconds(one.[[Nanoseconds]], two.[[Nanoseconds]])).
  155. return Value(compare_epoch_nanoseconds(one->nanoseconds(), two->nanoseconds()));
  156. }
  157. }