diff options
author | Luke Wilde <lukew@serenityos.org> | 2021-11-29 22:30:46 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-11-29 22:56:35 +0000 |
commit | ac8c3919cb964dc8e97ecb04fe8149c9fe1469f8 (patch) | |
tree | f1da65e4437342cfe9b9bb9a78e220b91682bed3 /Userland/Libraries/LibJS/Runtime | |
parent | acf8729a81c744dbe183d3660bc3b332cd1c3aa6 (diff) | |
download | serenity-ac8c3919cb964dc8e97ecb04fe8149c9fe1469f8.zip |
LibJS: Implement Temporal.Duration.prototype.add
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp | 24 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h | 1 |
2 files changed, 25 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp index fe6ffcd734..6f862b4dcc 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp @@ -46,6 +46,7 @@ void DurationPrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.with, with, 1, attr); define_native_function(vm.names.negated, negated, 0, attr); define_native_function(vm.names.abs, abs, 0, attr); + define_native_function(vm.names.add, add, 1, attr); define_native_function(vm.names.round, round, 1, attr); define_native_function(vm.names.total, total, 1, attr); define_native_function(vm.names.toString, to_string, 0, attr); @@ -289,6 +290,29 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::abs) return TRY(create_temporal_duration(global_object, fabs(duration->years()), fabs(duration->months()), fabs(duration->weeks()), fabs(duration->days()), fabs(duration->hours()), fabs(duration->minutes()), fabs(duration->seconds()), fabs(duration->milliseconds()), fabs(duration->microseconds()), fabs(duration->nanoseconds()))); } +// 7.3.18 Temporal.Duration.prototype.add ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.add +JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::add) +{ + // 1. Let duration be the this value. + // 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). + auto* duration = TRY(typed_this_object(global_object)); + + // 3. Set other to ? ToLimitedTemporalDuration(other, ยซ ยป). + auto other = TRY(to_limited_temporal_duration(global_object, vm.argument(0), {})); + + // 4. Set options to ? GetOptionsObject(options). + auto* options = TRY(get_options_object(global_object, vm.argument(1))); + + // 5. Let relativeTo be ? ToRelativeTemporalObject(options). + auto relative_to = TRY(to_relative_temporal_object(global_object, *options)); + + // 6. Let result be ? AddDuration(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]], other.[[Years]], other.[[Months]], other.[[Weeks]], other.[[Days]], other.[[Hours]], other.[[Minutes]], other.[[Seconds]], other.[[Milliseconds]], other.[[Microseconds]], other.[[Nanoseconds]], relativeTo). + auto result = TRY(add_duration(global_object, duration->years(), duration->months(), duration->weeks(), duration->days(), duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), duration->nanoseconds(), other.years, other.months, other.weeks, other.days, other.hours, other.minutes, other.seconds, other.milliseconds, other.microseconds, other.nanoseconds, relative_to)); + + // 7. Return ? CreateTemporalDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]). + return TRY(create_temporal_duration(global_object, result.years, result.months, result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds)); +} + // 7.3.20 Temporal.Duration.prototype.round ( roundTo ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.round JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::round) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h index 94d2304cf7..ece335210a 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h @@ -35,6 +35,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(with); JS_DECLARE_NATIVE_FUNCTION(negated); JS_DECLARE_NATIVE_FUNCTION(abs); + JS_DECLARE_NATIVE_FUNCTION(add); JS_DECLARE_NATIVE_FUNCTION(round); JS_DECLARE_NATIVE_FUNCTION(total); JS_DECLARE_NATIVE_FUNCTION(to_string); |