mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-03 16:16:43 +00:00
LibJS: Implement Temporal.PlainTime.prototype.since/until
This commit is contained in:
parent
1c22011ed6
commit
85eef698b9
Notes:
github-actions[bot]
2024-11-24 00:37:42 +00:00
Author: https://github.com/trflynn89
Commit: 85eef698b9
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2535
6 changed files with 244 additions and 0 deletions
|
@ -606,6 +606,38 @@ Time round_time(Time const& time, u64 increment, Unit unit, RoundingMode roundin
|
|||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
// 4.5.17 DifferenceTemporalPlainTime ( operation, temporalTime, other, options ), https://tc39.es/proposal-temporal/#sec-temporal-differencetemporalplaintime
|
||||
ThrowCompletionOr<GC::Ref<Duration>> difference_temporal_plain_time(VM& vm, DurationOperation operation, PlainTime const& temporal_time, Value other_value, Value options)
|
||||
{
|
||||
// 1. Set other to ? ToTemporalTime(other).
|
||||
auto other = TRY(to_temporal_time(vm, other_value));
|
||||
|
||||
// 2. Let resolvedOptions be ? GetOptionsObject(options).
|
||||
auto resolved_options = TRY(get_options_object(vm, options));
|
||||
|
||||
// 3. Let settings be ? GetDifferenceSettings(operation, resolvedOptions, TIME, « », NANOSECOND, HOUR).
|
||||
auto settings = TRY(get_difference_settings(vm, operation, resolved_options, UnitGroup::Time, {}, Unit::Nanosecond, Unit::Hour));
|
||||
|
||||
// 4. Let timeDuration be DifferenceTime(temporalTime.[[Time]], other.[[Time]]).
|
||||
auto time_duration = difference_time(temporal_time.time(), other->time());
|
||||
|
||||
// 5. Set timeDuration to ! RoundTimeDuration(timeDuration, settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]]).
|
||||
time_duration = MUST(round_time_duration(vm, time_duration, Crypto::UnsignedBigInteger { settings.rounding_increment }, settings.smallest_unit, settings.rounding_mode));
|
||||
|
||||
// 6. Let duration be ! CombineDateAndTimeDuration(ZeroDateDuration(), timeDuration).
|
||||
auto duration = MUST(combine_date_and_time_duration(vm, zero_date_duration(vm), move(time_duration)));
|
||||
|
||||
// 7. Let result be ! TemporalDurationFromInternal(duration, settings.[[LargestUnit]]).
|
||||
auto result = MUST(temporal_duration_from_internal(vm, duration, settings.largest_unit));
|
||||
|
||||
// 8. If operation is SINCE, set result to CreateNegatedTemporalDuration(result).
|
||||
if (operation == DurationOperation::Since)
|
||||
result = create_negated_temporal_duration(vm, result);
|
||||
|
||||
// 9. Return result.
|
||||
return result;
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
|
|
|
@ -63,6 +63,7 @@ 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<Duration>> difference_temporal_plain_time(VM&, DurationOperation, PlainTime const&, Value other, Value options);
|
||||
ThrowCompletionOr<GC::Ref<PlainTime>> add_duration_to_time(VM&, ArithmeticOperation, PlainTime const&, Value temporal_duration_like);
|
||||
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibJS/Runtime/Temporal/Duration.h>
|
||||
#include <LibJS/Runtime/Temporal/PlainTimePrototype.h>
|
||||
|
||||
namespace JS::Temporal {
|
||||
|
@ -37,6 +38,8 @@ void PlainTimePrototype::initialize(Realm& realm)
|
|||
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.until, until, 1, attr);
|
||||
define_native_function(realm, vm.names.since, since, 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);
|
||||
|
@ -95,6 +98,34 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::subtract)
|
|||
return TRY(add_duration_to_time(vm, ArithmeticOperation::Subtract, temporal_time, temporal_duration_like));
|
||||
}
|
||||
|
||||
// 4.3.12 Temporal.PlainTime.prototype.until ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.until
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::until)
|
||||
{
|
||||
auto other = vm.argument(0);
|
||||
auto options = vm.argument(1);
|
||||
|
||||
// 1. Let temporalTime be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
|
||||
auto temporal_time = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Return ? DifferenceTemporalPlainTime(UNTIL, temporalTime, other, options).
|
||||
return TRY(difference_temporal_plain_time(vm, DurationOperation::Until, temporal_time, other, options));
|
||||
}
|
||||
|
||||
// 4.3.13 Temporal.PlainTime.prototype.since ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.since
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::since)
|
||||
{
|
||||
auto other = vm.argument(0);
|
||||
auto options = vm.argument(1);
|
||||
|
||||
// 1. Let temporalTime be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
|
||||
auto temporal_time = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Return ? DifferenceTemporalPlainTime(SINCE, temporalTime, other, options).
|
||||
return TRY(difference_temporal_plain_time(vm, DurationOperation::Since, temporal_time, other, options));
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
|
|
|
@ -31,6 +31,8 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(nanosecond_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(add);
|
||||
JS_DECLARE_NATIVE_FUNCTION(subtract);
|
||||
JS_DECLARE_NATIVE_FUNCTION(until);
|
||||
JS_DECLARE_NATIVE_FUNCTION(since);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_string);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_json);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue