InstantConstructor.cpp 6.8 KB

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