LibJS: Implement Temporal.PlainTime.prototype.add/subtract

This commit is contained in:
Timothy Flynn 2024-11-23 13:31:32 -05:00 committed by Tim Flynn
commit 1c22011ed6
Notes: github-actions[bot] 2024-11-24 00:37:49 +00:00
6 changed files with 105 additions and 0 deletions

View file

@ -606,4 +606,24 @@ Time round_time(Time const& time, u64 increment, Unit unit, RoundingMode roundin
VERIFY_NOT_REACHED();
}
// 4.5.18 AddDurationToTime ( operation, temporalTime, temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal-adddurationtotime
ThrowCompletionOr<GC::Ref<PlainTime>> add_duration_to_time(VM& vm, ArithmeticOperation operation, PlainTime const& temporal_time, Value temporal_duration_like)
{
// 1. Let duration be ? ToTemporalDuration(temporalDurationLike).
auto duration = TRY(to_temporal_duration(vm, temporal_duration_like));
// 2. If operation is SUBTRACT, set duration to CreateNegatedTemporalDuration(duration).
if (operation == ArithmeticOperation::Subtract)
duration = create_negated_temporal_duration(vm, duration);
// 3. Let internalDuration be ToInternalDurationRecord(duration).
auto internal_duration = to_internal_duration_record(vm, duration);
// 4. Let result be AddTime(temporalTime.[[Time]], internalDuration.[[Time]]).
auto result = add_time(temporal_time.time(), internal_duration.time);
// 5. Return ! CreateTemporalTime(result).
return MUST(create_temporal_time(vm, result));
}
}

View file

@ -63,5 +63,6 @@ String time_record_to_string(Time const&, SecondsStringPrecision::Precision);
i8 compare_time_record(Time const&, Time const&);
Time add_time(Time const&, TimeDuration const& time_duration);
Time round_time(Time const&, u64 increment, Unit, RoundingMode);
ThrowCompletionOr<GC::Ref<PlainTime>> add_duration_to_time(VM&, ArithmeticOperation, PlainTime const&, Value temporal_duration_like);
}

View file

@ -35,6 +35,8 @@ void PlainTimePrototype::initialize(Realm& realm)
define_native_accessor(realm, vm.names.nanosecond, nanosecond_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(realm, vm.names.add, add, 1, attr);
define_native_function(realm, vm.names.subtract, subtract, 1, attr);
define_native_function(realm, vm.names.toString, to_string, 0, attr);
define_native_function(realm, vm.names.toLocaleString, to_locale_string, 0, attr);
define_native_function(realm, vm.names.toJSON, to_json, 0, attr);
@ -67,6 +69,32 @@ void PlainTimePrototype::initialize(Realm& realm)
JS_ENUMERATE_PLAIN_TIME_FIELDS
#undef __JS_ENUMERATE
// 4.3.9 Temporal.PlainTime.prototype.add ( temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.add
JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::add)
{
auto temporal_duration_like = vm.argument(0);
// 1. Let temporalTime be the this value.
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
auto temporal_time = TRY(typed_this_object(vm));
// 3. Return ? AddDurationToTime(ADD, temporalTime, temporalDurationLike).
return TRY(add_duration_to_time(vm, ArithmeticOperation::Add, temporal_time, temporal_duration_like));
}
// 4.3.10 Temporal.PlainTime.prototype.subtract ( temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.subtract
JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::subtract)
{
auto temporal_duration_like = vm.argument(0);
// 1. Let temporalTime be the this value.
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
auto temporal_time = TRY(typed_this_object(vm));
// 3. Return ? AddDurationToTime(SUBTRACT, temporalTime, temporalDurationLike).
return TRY(add_duration_to_time(vm, ArithmeticOperation::Subtract, temporal_time, temporal_duration_like));
}
// 4.3.16 Temporal.PlainTime.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.tostring
JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::to_string)
{

View file

@ -29,6 +29,8 @@ private:
JS_DECLARE_NATIVE_FUNCTION(millisecond_getter);
JS_DECLARE_NATIVE_FUNCTION(microsecond_getter);
JS_DECLARE_NATIVE_FUNCTION(nanosecond_getter);
JS_DECLARE_NATIVE_FUNCTION(add);
JS_DECLARE_NATIVE_FUNCTION(subtract);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(to_json);