Instant.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCrypto/BigInt/SignedBigInteger.h>
  7. #include <LibJS/Runtime/AbstractOperations.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 Temporal.Instant Objects, https://tc39.es/proposal-temporal/#sec-temporal-instant-objects
  13. Instant::Instant(BigInt& nanoseconds, Object& prototype)
  14. : Object(prototype)
  15. , m_nanoseconds(nanoseconds)
  16. {
  17. }
  18. void Instant::visit_edges(Cell::Visitor& visitor)
  19. {
  20. Base::visit_edges(visitor);
  21. visitor.visit(&m_nanoseconds);
  22. }
  23. // 8.5.1 IsValidEpochNanoseconds ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-isvalidepochnanoseconds
  24. bool is_valid_epoch_nanoseconds(BigInt const& epoch_nanoseconds)
  25. {
  26. // 1. Assert: Type(epochNanoseconds) is BigInt.
  27. // 2. If epochNanoseconds < −86400ℤ × 10^17ℤ or epochNanoseconds > 86400ℤ × 10^17ℤ, then
  28. if (epoch_nanoseconds.big_integer() < INSTANT_NANOSECONDS_MIN || epoch_nanoseconds.big_integer() > INSTANT_NANOSECONDS_MAX) {
  29. // a. Return false.
  30. return false;
  31. }
  32. // 3. Return true.
  33. return true;
  34. }
  35. // 8.5.2 CreateTemporalInstant ( epochNanoseconds [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalinstant
  36. Object* create_temporal_instant(GlobalObject& global_object, BigInt& epoch_nanoseconds, FunctionObject* new_target)
  37. {
  38. auto& vm = global_object.vm();
  39. // 1. Assert: Type(epochNanoseconds) is BigInt.
  40. // 2. Assert: ! IsValidEpochNanoseconds(epochNanoseconds) is true.
  41. VERIFY(is_valid_epoch_nanoseconds(epoch_nanoseconds));
  42. // 3. If newTarget is not present, set it to %Temporal.Instant%.
  43. if (!new_target)
  44. new_target = global_object.temporal_instant_constructor();
  45. // 4. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.Instant.prototype%", « [[InitializedTemporalInstant]], [[Nanoseconds]] »).
  46. // 5. Set object.[[Nanoseconds]] to epochNanoseconds.
  47. auto* object = ordinary_create_from_constructor<Instant>(global_object, *new_target, &GlobalObject::temporal_instant_prototype, epoch_nanoseconds);
  48. if (vm.exception())
  49. return {};
  50. // 6. Return object.
  51. return object;
  52. }
  53. }