InstantConstructor.cpp 8.1 KB

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