DurationConstructor.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/Duration.h>
  9. #include <LibJS/Runtime/Temporal/DurationConstructor.h>
  10. namespace JS::Temporal {
  11. // 7.1 The Temporal.Duration Constructor, https://tc39.es/proposal-temporal/#sec-temporal-duration-constructor
  12. DurationConstructor::DurationConstructor(GlobalObject& global_object)
  13. : NativeFunction(vm().names.Duration.as_string(), *global_object.function_prototype())
  14. {
  15. }
  16. void DurationConstructor::initialize(GlobalObject& global_object)
  17. {
  18. NativeFunction::initialize(global_object);
  19. auto& vm = this->vm();
  20. // 7.2.1 Temporal.Duration.prototype, https://tc39.es/proposal-temporal/#sec-temporal-duration-prototype
  21. define_direct_property(vm.names.prototype, global_object.temporal_duration_prototype(), 0);
  22. u8 attr = Attribute::Writable | Attribute::Configurable;
  23. define_native_function(vm.names.from, from, 1, attr);
  24. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  25. }
  26. // 7.1.1 Temporal.Duration ( [ years [ , months [ , weeks [ , days [ , hours [ , minutes [ , seconds [ , milliseconds [ , microseconds [ , nanoseconds ] ] ] ] ] ] ] ] ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration
  27. Value DurationConstructor::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.Duration");
  33. return {};
  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. Value DurationConstructor::construct(FunctionObject& new_target)
  37. {
  38. auto& vm = this->vm();
  39. auto& global_object = this->global_object();
  40. // 2. Let y be ? ToIntegerOrInfinity(years).
  41. auto y = vm.argument(0).to_integer_or_infinity(global_object);
  42. if (vm.exception())
  43. return {};
  44. // 3. Let mo be ? ToIntegerOrInfinity(months).
  45. auto mo = vm.argument(1).to_integer_or_infinity(global_object);
  46. if (vm.exception())
  47. return {};
  48. // 4. Let w be ? ToIntegerOrInfinity(weeks).
  49. auto w = vm.argument(2).to_integer_or_infinity(global_object);
  50. if (vm.exception())
  51. return {};
  52. // 5. Let d be ? ToIntegerOrInfinity(days).
  53. auto d = vm.argument(3).to_integer_or_infinity(global_object);
  54. if (vm.exception())
  55. return {};
  56. // 6. Let h be ? ToIntegerOrInfinity(hours).
  57. auto h = vm.argument(4).to_integer_or_infinity(global_object);
  58. if (vm.exception())
  59. return {};
  60. // 7. Let m be ? ToIntegerOrInfinity(minutes).
  61. auto m = vm.argument(5).to_integer_or_infinity(global_object);
  62. if (vm.exception())
  63. return {};
  64. // 8. Let s be ? ToIntegerOrInfinity(seconds).
  65. auto s = vm.argument(6).to_integer_or_infinity(global_object);
  66. if (vm.exception())
  67. return {};
  68. // 9. Let ms be ? ToIntegerOrInfinity(milliseconds).
  69. auto ms = vm.argument(7).to_integer_or_infinity(global_object);
  70. if (vm.exception())
  71. return {};
  72. // 10. Let mis be ? ToIntegerOrInfinity(microseconds).
  73. auto mis = vm.argument(8).to_integer_or_infinity(global_object);
  74. if (vm.exception())
  75. return {};
  76. // 11. Let ns be ? ToIntegerOrInfinity(nanoseconds).
  77. auto ns = vm.argument(9).to_integer_or_infinity(global_object);
  78. if (vm.exception())
  79. return {};
  80. // 12. Return ? CreateTemporalDuration(y, mo, w, d, h, m, s, ms, mis, ns, NewTarget).
  81. return create_temporal_duration(global_object, y, mo, w, d, h, m, s, ms, mis, ns, &new_target);
  82. }
  83. // 7.2.2 Temporal.Duration.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.duration.from
  84. JS_DEFINE_NATIVE_FUNCTION(DurationConstructor::from)
  85. {
  86. auto item = vm.argument(0);
  87. // 1. If Type(item) is Object and item has an [[InitializedTemporalDuration]] internal slot, then
  88. if (item.is_object() && is<Duration>(item.as_object())) {
  89. auto& duration = static_cast<Duration&>(item.as_object());
  90. // a. Return ? CreateTemporalDuration(item.[[Years]], item.[[Months]], item.[[Weeks]], item.[[Days]], item.[[Hours]], item.[[Minutes]], item.[[Seconds]], item.[[Milliseconds]], item.[[Microseconds]], item.[[Nanoseconds]]).
  91. return 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());
  92. }
  93. // 2. Return ? ToTemporalDuration(item).
  94. return to_temporal_duration(global_object, item);
  95. }
  96. }