DurationConstructor.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TypeCasts.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  9. #include <LibJS/Runtime/Temporal/Duration.h>
  10. #include <LibJS/Runtime/Temporal/DurationConstructor.h>
  11. namespace JS::Temporal {
  12. // 7.1 The Temporal.Duration Constructor, https://tc39.es/proposal-temporal/#sec-temporal-duration-constructor
  13. DurationConstructor::DurationConstructor(GlobalObject& global_object)
  14. : NativeFunction(vm().names.Duration.as_string(), *global_object.function_prototype())
  15. {
  16. }
  17. void DurationConstructor::initialize(GlobalObject& global_object)
  18. {
  19. NativeFunction::initialize(global_object);
  20. auto& vm = this->vm();
  21. // 7.2.1 Temporal.Duration.prototype, https://tc39.es/proposal-temporal/#sec-temporal-duration-prototype
  22. define_direct_property(vm.names.prototype, global_object.temporal_duration_prototype(), 0);
  23. u8 attr = Attribute::Writable | Attribute::Configurable;
  24. define_native_function(vm.names.from, from, 1, attr);
  25. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  26. }
  27. // 7.1.1 Temporal.Duration ( [ years [ , months [ , weeks [ , days [ , hours [ , minutes [ , seconds [ , milliseconds [ , microseconds [ , nanoseconds ] ] ] ] ] ] ] ] ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration
  28. ThrowCompletionOr<Value> DurationConstructor::call()
  29. {
  30. auto& vm = this->vm();
  31. // 1. If NewTarget is undefined, then
  32. // a. Throw a TypeError exception.
  33. return vm.throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.Duration");
  34. }
  35. // 7.1.1 Temporal.Duration ( [ years [ , months [ , weeks [ , days [ , hours [ , minutes [ , seconds [ , milliseconds [ , microseconds [ , nanoseconds ] ] ] ] ] ] ] ] ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration
  36. ThrowCompletionOr<Object*> DurationConstructor::construct(FunctionObject& new_target)
  37. {
  38. auto& vm = this->vm();
  39. auto& global_object = this->global_object();
  40. // 2. Let y be ? ToIntegerThrowOnInfinity(years).
  41. auto y = TRY(to_integer_throw_on_infinity(global_object, vm.argument(0), ErrorType::TemporalInvalidDuration));
  42. // 3. Let mo be ? ToIntegerThrowOnInfinity(months).
  43. auto mo = TRY(to_integer_throw_on_infinity(global_object, vm.argument(1), ErrorType::TemporalInvalidDuration));
  44. // 4. Let w be ? ToIntegerThrowOnInfinity(weeks).
  45. auto w = TRY(to_integer_throw_on_infinity(global_object, vm.argument(2), ErrorType::TemporalInvalidDuration));
  46. // 5. Let d be ? ToIntegerThrowOnInfinity(days).
  47. auto d = TRY(to_integer_throw_on_infinity(global_object, vm.argument(3), ErrorType::TemporalInvalidDuration));
  48. // 6. Let h be ? ToIntegerThrowOnInfinity(hours).
  49. auto h = TRY(to_integer_throw_on_infinity(global_object, vm.argument(4), ErrorType::TemporalInvalidDuration));
  50. // 7. Let m be ? ToIntegerThrowOnInfinity(minutes).
  51. auto m = TRY(to_integer_throw_on_infinity(global_object, vm.argument(5), ErrorType::TemporalInvalidDuration));
  52. // 8. Let s be ? ToIntegerThrowOnInfinity(seconds).
  53. auto s = TRY(to_integer_throw_on_infinity(global_object, vm.argument(6), ErrorType::TemporalInvalidDuration));
  54. // 9. Let ms be ? ToIntegerThrowOnInfinity(milliseconds).
  55. auto ms = TRY(to_integer_throw_on_infinity(global_object, vm.argument(7), ErrorType::TemporalInvalidDuration));
  56. // 10. Let mis be ? ToIntegerThrowOnInfinity(microseconds).
  57. auto mis = TRY(to_integer_throw_on_infinity(global_object, vm.argument(8), ErrorType::TemporalInvalidDuration));
  58. // 11. Let ns be ? ToIntegerThrowOnInfinity(nanoseconds).
  59. auto ns = TRY(to_integer_throw_on_infinity(global_object, vm.argument(9), ErrorType::TemporalInvalidDuration));
  60. // 12. Return ? CreateTemporalDuration(y, mo, w, d, h, m, s, ms, mis, ns, NewTarget).
  61. return TRY(create_temporal_duration(global_object, y, mo, w, d, h, m, s, ms, mis, ns, &new_target));
  62. }
  63. // 7.2.2 Temporal.Duration.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.duration.from
  64. JS_DEFINE_NATIVE_FUNCTION(DurationConstructor::from)
  65. {
  66. auto item = vm.argument(0);
  67. // 1. If Type(item) is Object and item has an [[InitializedTemporalDuration]] internal slot, then
  68. if (item.is_object() && is<Duration>(item.as_object())) {
  69. auto& duration = static_cast<Duration&>(item.as_object());
  70. // a. Return ? CreateTemporalDuration(item.[[Years]], item.[[Months]], item.[[Weeks]], item.[[Days]], item.[[Hours]], item.[[Minutes]], item.[[Seconds]], item.[[Milliseconds]], item.[[Microseconds]], item.[[Nanoseconds]]).
  71. return TRY(create_temporal_duration(global_object, duration.years(), duration.months(), duration.weeks(), duration.days(), duration.hours(), duration.minutes(), duration.seconds(), duration.milliseconds(), duration.microseconds(), duration.nanoseconds()));
  72. }
  73. // 2. Return ? ToTemporalDuration(item).
  74. return TRY(to_temporal_duration(global_object, item));
  75. }
  76. }