1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:27:45 +00:00

LibJS: Leverage ToPartialDuration in ToTemporalDurationRecord

This is an editorial change in the Temporal spec.

See: c3efde0
This commit is contained in:
Linus Groh 2022-06-14 21:20:35 +01:00
parent 9bcd88828f
commit 6982e53113
2 changed files with 39 additions and 50 deletions

View file

@ -174,50 +174,38 @@ ThrowCompletionOr<DurationRecord> to_temporal_duration_record(GlobalObject& glob
return create_duration_record(duration.years(), duration.months(), duration.weeks(), duration.days(), duration.hours(), duration.minutes(), duration.seconds(), duration.milliseconds(), duration.microseconds(), duration.nanoseconds()); return create_duration_record(duration.years(), duration.months(), duration.weeks(), duration.days(), duration.hours(), duration.minutes(), duration.seconds(), duration.milliseconds(), duration.microseconds(), duration.nanoseconds());
} }
// 3. Let result be a new Duration Record. // 3. Let result be a new Duration Record with each field set to 0.
auto result = DurationRecord {}; auto result = DurationRecord {};
// 4. Let any be false. // 4. Let partial be ? ToPartialDuration(temporalDurationLike).
auto any = false; auto partial = TRY(to_partial_duration(global_object, temporal_duration_like));
// 5. For each row of Table 7, except the header row, in table order, do auto duration_record_fields = temporal_duration_record_fields<DurationRecord, double>(vm);
for (auto& [field, property] : temporal_duration_like_properties<DurationRecord, double>(vm)) { auto partial_duration_record_fields = temporal_duration_record_fields<PartialDurationRecord, Optional<double>>(vm);
// a. Let prop be the Property Name value of the current row.
// b. Let val be ? Get(temporalDurationLike, prop). // 5. For each row of Table 8, except the header row, in table order, do
auto value = TRY(temporal_duration_like.as_object().get(property)); for (size_t i = 0; i < duration_record_fields.size(); ++i) {
// a. Let fieldName be the Field Name value of the current row.
auto field_name = duration_record_fields[i].field_name;
auto partial_field_name = partial_duration_record_fields[i].field_name;
// c. If val is undefined, then // b. Let value be the value of the field of partial whose name is fieldName.
if (value.is_undefined()) { auto value = partial.*partial_field_name;
// i. Set result's field whose name is the Field Name value of the current row to 0.
result.*field = 0;
}
// d. Else,
else {
// i. Set any to true.
any = true;
// ii. Let val be ? ToIntegerWithoutRounding(val). // c. If value is not undefined, then
auto value_integer = TRY(to_integer_without_rounding(global_object, value, ErrorType::TemporalInvalidDurationPropertyValueNonIntegral, property.as_string(), value.to_string_without_side_effects())); if (value.has_value()) {
// i. Set the field of result whose name is fieldName to value.
// iii. Set result's field whose name is the Field Name value of the current row to val. result.*field_name = *value;
result.*field = value_integer;
} }
} }
// 6. If any is false, then // 6. If ! IsValidDuration(result.[[Years]], result.[[Months]], result.[[Weeks]] result.[[Days]], result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]) is false, then
if (!any) {
// a. Throw a TypeError exception.
return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalInvalidDurationLikeObject);
}
// 7. If ! IsValidDuration(result.[[Years]], result.[[Months]], result.[[Weeks]] result.[[Days]], result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]) is false, then
if (!is_valid_duration(result.years, result.months, result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds)) { if (!is_valid_duration(result.years, result.months, result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds)) {
// a. Throw a RangeError exception. // a. Throw a RangeError exception.
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidDuration); return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidDuration);
} }
// 8. Return result. // 7. Return result.
return result; return result;
} }
@ -325,7 +313,7 @@ ThrowCompletionOr<PartialDurationRecord> to_partial_duration(GlobalObject& globa
auto any = false; auto any = false;
// 4. For each row of Table 7, except the header row, in table order, do // 4. For each row of Table 7, except the header row, in table order, do
for (auto& [field, property] : temporal_duration_like_properties<PartialDurationRecord, Optional<double>>(vm)) { for (auto& [field_name, property] : temporal_duration_record_fields<PartialDurationRecord, Optional<double>>(vm)) {
// a. Let property be the Property Name value of the current row. // a. Let property be the Property Name value of the current row.
// b. Let value be ? Get(temporalDurationLike, property). // b. Let value be ? Get(temporalDurationLike, property).
@ -339,8 +327,9 @@ ThrowCompletionOr<PartialDurationRecord> to_partial_duration(GlobalObject& globa
// ii. Set value to ? ToIntegerWithoutRounding(value). // ii. Set value to ? ToIntegerWithoutRounding(value).
auto value_integer = TRY(to_integer_without_rounding(global_object, value, ErrorType::TemporalInvalidDurationPropertyValueNonIntegral, property.as_string(), value.to_string_without_side_effects())); auto value_integer = TRY(to_integer_without_rounding(global_object, value, ErrorType::TemporalInvalidDurationPropertyValueNonIntegral, property.as_string(), value.to_string_without_side_effects()));
// iii. Set result's field whose name is the Field Name value of the current row to value. // iii. Let fieldName be the Field Name value of the current row.
result.*field = value_integer; // iv. Set the field of result whose name is fieldName to value.
result.*field_name = value_integer;
} }
} }

View file

@ -109,28 +109,28 @@ struct RoundedDuration {
double remainder; double remainder;
}; };
// Table 7: Properties of a TemporalDurationLike, https://tc39.es/proposal-temporal/#table-temporal-temporaldurationlike-properties // Table 8: Duration Record Fields, https://tc39.es/proposal-temporal/#table-temporal-duration-record-fields
template<typename StructT, typename ValueT> template<typename StructT, typename ValueT>
struct TemporalDurationLikeProperty { struct TemporalDurationRecordField {
ValueT StructT::*field { nullptr }; ValueT StructT::*field_name { nullptr };
PropertyKey property; PropertyKey property_name;
}; };
template<typename StructT, typename ValueT> template<typename StructT, typename ValueT>
auto temporal_duration_like_properties = [](VM& vm) { auto temporal_duration_record_fields = [](VM& vm) {
using PropertyT = TemporalDurationLikeProperty<StructT, ValueT>; using FieldT = TemporalDurationRecordField<StructT, ValueT>;
return AK::Array<PropertyT, 10> { return AK::Array {
PropertyT { &StructT::days, vm.names.days }, FieldT { &StructT::days, vm.names.days },
PropertyT { &StructT::hours, vm.names.hours }, FieldT { &StructT::hours, vm.names.hours },
PropertyT { &StructT::microseconds, vm.names.microseconds }, FieldT { &StructT::microseconds, vm.names.microseconds },
PropertyT { &StructT::milliseconds, vm.names.milliseconds }, FieldT { &StructT::milliseconds, vm.names.milliseconds },
PropertyT { &StructT::minutes, vm.names.minutes }, FieldT { &StructT::minutes, vm.names.minutes },
PropertyT { &StructT::months, vm.names.months }, FieldT { &StructT::months, vm.names.months },
PropertyT { &StructT::nanoseconds, vm.names.nanoseconds }, FieldT { &StructT::nanoseconds, vm.names.nanoseconds },
PropertyT { &StructT::seconds, vm.names.seconds }, FieldT { &StructT::seconds, vm.names.seconds },
PropertyT { &StructT::weeks, vm.names.weeks }, FieldT { &StructT::weeks, vm.names.weeks },
PropertyT { &StructT::years, vm.names.years }, FieldT { &StructT::years, vm.names.years },
}; };
}; };