LibJS: Fix TemporalDurationLike property order

The table is sorted alphabetically and supposed to be iterated in that
oder. Also move this to a templated lambda for later re-use with
different target structs and value types.
This commit is contained in:
Linus Groh 2021-07-18 22:28:30 +01:00
parent 152251f5a7
commit 7355c23e17
Notes: sideshowbarker 2024-07-18 08:46:37 +09:00
2 changed files with 26 additions and 18 deletions

View File

@ -87,25 +87,8 @@ PartialDuration to_partial_duration(GlobalObject& global_object, Value temporal_
// 3. Let any be false.
auto any = false;
struct PartialDurationProperty {
Optional<double> PartialDuration::*internal_slot { nullptr };
PropertyName property;
};
auto properties = AK::Array<PartialDurationProperty, 10> {
PartialDurationProperty { &PartialDuration::years, vm.names.years },
PartialDurationProperty { &PartialDuration::months, vm.names.months },
PartialDurationProperty { &PartialDuration::weeks, vm.names.weeks },
PartialDurationProperty { &PartialDuration::days, vm.names.days },
PartialDurationProperty { &PartialDuration::hours, vm.names.hours },
PartialDurationProperty { &PartialDuration::minutes, vm.names.minutes },
PartialDurationProperty { &PartialDuration::seconds, vm.names.seconds },
PartialDurationProperty { &PartialDuration::milliseconds, vm.names.milliseconds },
PartialDurationProperty { &PartialDuration::microseconds, vm.names.microseconds },
PartialDurationProperty { &PartialDuration::nanoseconds, vm.names.nanoseconds },
};
// 4. For each row of Table 7, except the header row, in table order, do
for (auto& [internal_slot, property] : properties) {
for (auto& [internal_slot, property] : temporal_duration_like_properties<PartialDuration, Optional<double>>(vm)) {
// a. Let property be the Property value of the current row.
// b. Let value be ? Get(temporalDurationLike, property).

View File

@ -57,6 +57,31 @@ struct PartialDuration {
Optional<double> nanoseconds;
};
// Table 7: Properties of a TemporalDurationLike, https://tc39.es/proposal-temporal/#table-temporal-temporaldurationlike-properties
template<typename StructT, typename ValueT>
struct TemporalDurationLikeProperty {
ValueT StructT::*internal_slot { nullptr };
PropertyName property;
};
template<typename StructT, typename ValueT>
auto temporal_duration_like_properties = [](VM& vm) {
using PropertyT = TemporalDurationLikeProperty<StructT, ValueT>;
return AK::Array<PropertyT, 10> {
PropertyT { &StructT::days, vm.names.days },
PropertyT { &StructT::hours, vm.names.hours },
PropertyT { &StructT::microseconds, vm.names.microseconds },
PropertyT { &StructT::milliseconds, vm.names.milliseconds },
PropertyT { &StructT::minutes, vm.names.minutes },
PropertyT { &StructT::months, vm.names.months },
PropertyT { &StructT::nanoseconds, vm.names.nanoseconds },
PropertyT { &StructT::seconds, vm.names.seconds },
PropertyT { &StructT::weeks, vm.names.weeks },
PropertyT { &StructT::years, vm.names.years },
};
};
i8 duration_sign(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds);
bool is_valid_duration(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds);
PartialDuration to_partial_duration(GlobalObject&, Value temporal_duration_like);