InstantConstructor.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/GlobalObject.h>
  7. #include <LibJS/Runtime/Temporal/Instant.h>
  8. #include <LibJS/Runtime/Temporal/InstantConstructor.h>
  9. namespace JS::Temporal {
  10. // 8.1 The Temporal.Instant Constructor, https://tc39.es/proposal-temporal/#sec-temporal-instant-constructor
  11. InstantConstructor::InstantConstructor(GlobalObject& global_object)
  12. : NativeFunction(vm().names.Instant.as_string(), *global_object.function_prototype())
  13. {
  14. }
  15. void InstantConstructor::initialize(GlobalObject& global_object)
  16. {
  17. NativeFunction::initialize(global_object);
  18. auto& vm = this->vm();
  19. // 8.2.1 Temporal.Instant.prototype, https://tc39.es/proposal-temporal/#sec-temporal-instant-prototype
  20. define_direct_property(vm.names.prototype, global_object.temporal_instant_prototype(), 0);
  21. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  22. }
  23. // 8.1.1 Temporal.Instant ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant
  24. Value InstantConstructor::call()
  25. {
  26. auto& vm = this->vm();
  27. // 1. If NewTarget is undefined, then
  28. // a. Throw a TypeError exception.
  29. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.Instant");
  30. return {};
  31. }
  32. // 8.1.1 Temporal.Instant ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant
  33. Value InstantConstructor::construct(FunctionObject& new_target)
  34. {
  35. auto& vm = this->vm();
  36. auto& global_object = this->global_object();
  37. // 2. Let epochNanoseconds be ? ToBigInt(epochNanoseconds).
  38. auto* epoch_nanoseconds = vm.argument(0).to_bigint(global_object);
  39. if (vm.exception())
  40. return {};
  41. // 3. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
  42. if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
  43. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  44. return {};
  45. }
  46. // 4. Return ? CreateTemporalInstant(epochNanoseconds, NewTarget).
  47. return create_temporal_instant(global_object, *epoch_nanoseconds, &new_target);
  48. }
  49. }