DurationConstructor.cpp 4.7 KB

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