InstantConstructor.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  25. }
  26. // 8.1.1 Temporal.Instant ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant
  27. Value InstantConstructor::call()
  28. {
  29. auto& vm = this->vm();
  30. // 1. If NewTarget is undefined, then
  31. // a. Throw a TypeError exception.
  32. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.Instant");
  33. return {};
  34. }
  35. // 8.1.1 Temporal.Instant ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant
  36. Value InstantConstructor::construct(FunctionObject& new_target)
  37. {
  38. auto& vm = this->vm();
  39. auto& global_object = this->global_object();
  40. // 2. Let epochNanoseconds be ? ToBigInt(epochNanoseconds).
  41. auto* epoch_nanoseconds = vm.argument(0).to_bigint(global_object);
  42. if (vm.exception())
  43. return {};
  44. // 3. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
  45. if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
  46. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  47. return {};
  48. }
  49. // 4. Return ? CreateTemporalInstant(epochNanoseconds, NewTarget).
  50. return create_temporal_instant(global_object, *epoch_nanoseconds, &new_target);
  51. }
  52. // 8.2.3 Temporal.Instant.fromEpochSeconds ( epochSeconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochseconds
  53. JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_seconds)
  54. {
  55. // 1. Set epochSeconds to ? ToNumber(epochSeconds).
  56. auto epoch_seconds_value = vm.argument(0).to_number(global_object);
  57. if (vm.exception())
  58. return {};
  59. // 2. Set epochSeconds to ? NumberToBigInt(epochSeconds).
  60. auto* epoch_seconds = number_to_bigint(global_object, epoch_seconds_value);
  61. if (vm.exception())
  62. return {};
  63. // 3. Let epochNanoseconds be epochSeconds × 10^9ℤ.
  64. auto* epoch_nanoseconds = js_bigint(vm.heap(), epoch_seconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000'000 }));
  65. // 4. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
  66. if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
  67. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  68. return {};
  69. }
  70. // 5. Return ? CreateTemporalInstant(epochNanoseconds).
  71. return create_temporal_instant(global_object, *epoch_nanoseconds);
  72. }
  73. }