InstantConstructor.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.fromEpochSeconds, from_epoch_seconds, 1, attr);
  24. define_native_function(vm.names.fromEpochMilliseconds, from_epoch_milliseconds, 1, attr);
  25. define_native_function(vm.names.fromEpochMicroseconds, from_epoch_microseconds, 1, attr);
  26. define_native_function(vm.names.fromEpochNanoseconds, from_epoch_nanoseconds, 1, attr);
  27. define_native_function(vm.names.compare, compare, 2, attr);
  28. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  29. }
  30. // 8.1.1 Temporal.Instant ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant
  31. Value InstantConstructor::call()
  32. {
  33. auto& vm = this->vm();
  34. // 1. If NewTarget is undefined, then
  35. // a. Throw a TypeError exception.
  36. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.Instant");
  37. return {};
  38. }
  39. // 8.1.1 Temporal.Instant ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant
  40. Value InstantConstructor::construct(FunctionObject& new_target)
  41. {
  42. auto& vm = this->vm();
  43. auto& global_object = this->global_object();
  44. // 2. Let epochNanoseconds be ? ToBigInt(epochNanoseconds).
  45. auto* epoch_nanoseconds = vm.argument(0).to_bigint(global_object);
  46. if (vm.exception())
  47. return {};
  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 create_temporal_instant(global_object, *epoch_nanoseconds, &new_target);
  55. }
  56. // 8.2.3 Temporal.Instant.fromEpochSeconds ( epochSeconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochseconds
  57. JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_seconds)
  58. {
  59. // 1. Set epochSeconds to ? ToNumber(epochSeconds).
  60. auto epoch_seconds_value = vm.argument(0).to_number(global_object);
  61. if (vm.exception())
  62. return {};
  63. // 2. Set epochSeconds to ? NumberToBigInt(epochSeconds).
  64. auto* epoch_seconds = number_to_bigint(global_object, epoch_seconds_value);
  65. if (vm.exception())
  66. return {};
  67. // 3. Let epochNanoseconds be epochSeconds × 10^9ℤ.
  68. auto* epoch_nanoseconds = js_bigint(vm.heap(), epoch_seconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000'000 }));
  69. // 4. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
  70. if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
  71. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  72. return {};
  73. }
  74. // 5. Return ? CreateTemporalInstant(epochNanoseconds).
  75. return create_temporal_instant(global_object, *epoch_nanoseconds);
  76. }
  77. // 8.2.4 Temporal.Instant.fromEpochMilliseconds ( epochMilliseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochmilliseconds
  78. JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_milliseconds)
  79. {
  80. // 1. Set epochMilliseconds to ? ToNumber(epochMilliseconds).
  81. auto epoch_milliseconds_value = vm.argument(0).to_number(global_object);
  82. if (vm.exception())
  83. return {};
  84. // 2. Set epochMilliseconds to ? NumberToBigInt(epochMilliseconds).
  85. auto* epoch_milliseconds = number_to_bigint(global_object, epoch_milliseconds_value);
  86. if (vm.exception())
  87. return {};
  88. // 3. Let epochNanoseconds be epochMilliseconds × 10^6ℤ.
  89. auto* epoch_nanoseconds = js_bigint(vm.heap(), epoch_milliseconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000 }));
  90. // 4. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
  91. if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
  92. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  93. return {};
  94. }
  95. // 5. Return ? CreateTemporalInstant(epochNanoseconds).
  96. return create_temporal_instant(global_object, *epoch_nanoseconds);
  97. }
  98. // 8.2.5 Temporal.Instant.fromEpochMicroseconds ( epochMicroseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochmicroseconds
  99. JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_microseconds)
  100. {
  101. // 1. Set epochMicroseconds to ? ToBigInt(epochMicroseconds).
  102. auto* epoch_microseconds = vm.argument(0).to_bigint(global_object);
  103. if (vm.exception())
  104. return {};
  105. // 2. Let epochNanoseconds be epochMicroseconds × 1000ℤ.
  106. auto* epoch_nanoseconds = js_bigint(vm.heap(), epoch_microseconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000 }));
  107. // 3. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
  108. if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
  109. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  110. return {};
  111. }
  112. // 4. Return ? CreateTemporalInstant(epochNanoseconds).
  113. return create_temporal_instant(global_object, *epoch_nanoseconds);
  114. }
  115. // 8.2.6 Temporal.Instant.fromEpochNanoseconds ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochnanoseconds
  116. JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_nanoseconds)
  117. {
  118. // 1. Set epochNanoseconds to ? ToBigInt(epochNanoseconds).
  119. auto* epoch_nanoseconds = vm.argument(0).to_bigint(global_object);
  120. if (vm.exception())
  121. return {};
  122. // 2. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
  123. if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
  124. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  125. return {};
  126. }
  127. // 3. Return ? CreateTemporalInstant(epochNanoseconds).
  128. return create_temporal_instant(global_object, *epoch_nanoseconds);
  129. }
  130. // 8.2.7 Temporal.Instant.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.instant.compare
  131. JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::compare)
  132. {
  133. // 1. Set one to ? ToTemporalInstant(one).
  134. auto* one = to_temporal_instant(global_object, vm.argument(0));
  135. if (vm.exception())
  136. return {};
  137. // 2. Set two to ? ToTemporalInstant(two).
  138. auto* two = to_temporal_instant(global_object, vm.argument(1));
  139. if (vm.exception())
  140. return {};
  141. // 3. Return 𝔽(! CompareEpochNanoseconds(one.[[Nanoseconds]], two.[[Nanoseconds]])).
  142. return Value(compare_epoch_nanoseconds(one->nanoseconds(), two->nanoseconds()));
  143. }
  144. }