DurationConstructor.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. Value DurationConstructor::call()
  29. {
  30. auto& vm = this->vm();
  31. // 1. If NewTarget is undefined, then
  32. // a. Throw a TypeError exception.
  33. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.Duration");
  34. return {};
  35. }
  36. // 7.1.1 Temporal.Duration ( [ years [ , months [ , weeks [ , days [ , hours [ , minutes [ , seconds [ , milliseconds [ , microseconds [ , nanoseconds ] ] ] ] ] ] ] ] ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration
  37. Value DurationConstructor::construct(FunctionObject& new_target)
  38. {
  39. auto& vm = this->vm();
  40. auto& global_object = this->global_object();
  41. // 2. Let y be ? ToIntegerThrowOnInfinity(years).
  42. auto y = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(0), ErrorType::TemporalInvalidDuration));
  43. // 3. Let mo be ? ToIntegerThrowOnInfinity(months).
  44. auto mo = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(1), ErrorType::TemporalInvalidDuration));
  45. // 4. Let w be ? ToIntegerThrowOnInfinity(weeks).
  46. auto w = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(2), ErrorType::TemporalInvalidDuration));
  47. // 5. Let d be ? ToIntegerThrowOnInfinity(days).
  48. auto d = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(3), ErrorType::TemporalInvalidDuration));
  49. // 6. Let h be ? ToIntegerThrowOnInfinity(hours).
  50. auto h = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(4), ErrorType::TemporalInvalidDuration));
  51. // 7. Let m be ? ToIntegerThrowOnInfinity(minutes).
  52. auto m = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(5), ErrorType::TemporalInvalidDuration));
  53. // 8. Let s be ? ToIntegerThrowOnInfinity(seconds).
  54. auto s = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(6), ErrorType::TemporalInvalidDuration));
  55. // 9. Let ms be ? ToIntegerThrowOnInfinity(milliseconds).
  56. auto ms = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(7), ErrorType::TemporalInvalidDuration));
  57. // 10. Let mis be ? ToIntegerThrowOnInfinity(microseconds).
  58. auto mis = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(8), ErrorType::TemporalInvalidDuration));
  59. // 11. Let ns be ? ToIntegerThrowOnInfinity(nanoseconds).
  60. auto ns = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(9), ErrorType::TemporalInvalidDuration));
  61. // 12. Return ? CreateTemporalDuration(y, mo, w, d, h, m, s, ms, mis, ns, NewTarget).
  62. return TRY_OR_DISCARD(create_temporal_duration(global_object, y, mo, w, d, h, m, s, ms, mis, ns, &new_target));
  63. }
  64. // 7.2.2 Temporal.Duration.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.duration.from
  65. JS_DEFINE_NATIVE_FUNCTION(DurationConstructor::from)
  66. {
  67. auto item = vm.argument(0);
  68. // 1. If Type(item) is Object and item has an [[InitializedTemporalDuration]] internal slot, then
  69. if (item.is_object() && is<Duration>(item.as_object())) {
  70. auto& duration = static_cast<Duration&>(item.as_object());
  71. // a. Return ? CreateTemporalDuration(item.[[Years]], item.[[Months]], item.[[Weeks]], item.[[Days]], item.[[Hours]], item.[[Minutes]], item.[[Seconds]], item.[[Milliseconds]], item.[[Microseconds]], item.[[Nanoseconds]]).
  72. return TRY_OR_DISCARD(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()));
  73. }
  74. // 2. Return ? ToTemporalDuration(item).
  75. return TRY_OR_DISCARD(to_temporal_duration(global_object, item));
  76. }
  77. }