DurationConstructor.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 = to_integer_throw_on_infinity(global_object, vm.argument(0), ErrorType::TemporalInvalidDuration);
  43. if (vm.exception())
  44. return {};
  45. // 3. Let mo be ? ToIntegerThrowOnInfinity(months).
  46. auto mo = to_integer_throw_on_infinity(global_object, vm.argument(1), ErrorType::TemporalInvalidDuration);
  47. if (vm.exception())
  48. return {};
  49. // 4. Let w be ? ToIntegerThrowOnInfinity(weeks).
  50. auto w = to_integer_throw_on_infinity(global_object, vm.argument(2), ErrorType::TemporalInvalidDuration);
  51. if (vm.exception())
  52. return {};
  53. // 5. Let d be ? ToIntegerThrowOnInfinity(days).
  54. auto d = to_integer_throw_on_infinity(global_object, vm.argument(3), ErrorType::TemporalInvalidDuration);
  55. if (vm.exception())
  56. return {};
  57. // 6. Let h be ? ToIntegerThrowOnInfinity(hours).
  58. auto h = to_integer_throw_on_infinity(global_object, vm.argument(4), ErrorType::TemporalInvalidDuration);
  59. if (vm.exception())
  60. return {};
  61. // 7. Let m be ? ToIntegerThrowOnInfinity(minutes).
  62. auto m = to_integer_throw_on_infinity(global_object, vm.argument(5), ErrorType::TemporalInvalidDuration);
  63. if (vm.exception())
  64. return {};
  65. // 8. Let s be ? ToIntegerThrowOnInfinity(seconds).
  66. auto s = to_integer_throw_on_infinity(global_object, vm.argument(6), ErrorType::TemporalInvalidDuration);
  67. if (vm.exception())
  68. return {};
  69. // 9. Let ms be ? ToIntegerThrowOnInfinity(milliseconds).
  70. auto ms = to_integer_throw_on_infinity(global_object, vm.argument(7), ErrorType::TemporalInvalidDuration);
  71. if (vm.exception())
  72. return {};
  73. // 10. Let mis be ? ToIntegerThrowOnInfinity(microseconds).
  74. auto mis = to_integer_throw_on_infinity(global_object, vm.argument(8), ErrorType::TemporalInvalidDuration);
  75. if (vm.exception())
  76. return {};
  77. // 11. Let ns be ? ToIntegerThrowOnInfinity(nanoseconds).
  78. auto ns = to_integer_throw_on_infinity(global_object, vm.argument(9), ErrorType::TemporalInvalidDuration);
  79. if (vm.exception())
  80. return {};
  81. // 12. Return ? CreateTemporalDuration(y, mo, w, d, h, m, s, ms, mis, ns, NewTarget).
  82. return create_temporal_duration(global_object, y, mo, w, d, h, m, s, ms, mis, ns, &new_target);
  83. }
  84. // 7.2.2 Temporal.Duration.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.duration.from
  85. JS_DEFINE_NATIVE_FUNCTION(DurationConstructor::from)
  86. {
  87. auto item = vm.argument(0);
  88. // 1. If Type(item) is Object and item has an [[InitializedTemporalDuration]] internal slot, then
  89. if (item.is_object() && is<Duration>(item.as_object())) {
  90. auto& duration = static_cast<Duration&>(item.as_object());
  91. // a. Return ? CreateTemporalDuration(item.[[Years]], item.[[Months]], item.[[Weeks]], item.[[Days]], item.[[Hours]], item.[[Minutes]], item.[[Seconds]], item.[[Milliseconds]], item.[[Microseconds]], item.[[Nanoseconds]]).
  92. 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());
  93. }
  94. // 2. Return ? ToTemporalDuration(item).
  95. return to_temporal_duration(global_object, item);
  96. }
  97. }