diff options
author | Linus Groh <mail@linusgroh.de> | 2022-06-14 21:20:35 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-06-15 17:49:20 +0100 |
commit | 6982e5311323b7b02c57be5e9eb2d770bc094678 (patch) | |
tree | 26e215aca4dc2f355e219d4269fb7b105c4cb454 /Userland/Libraries/LibJS/Runtime/Temporal | |
parent | 9bcd88828f0bcd6a53017af9ad7db1d3a27c4ca0 (diff) | |
download | serenity-6982e5311323b7b02c57be5e9eb2d770bc094678.zip |
LibJS: Leverage ToPartialDuration in ToTemporalDurationRecord
This is an editorial change in the Temporal spec.
See: https://github.com/tc39/proposal-temporal/commit/c3efde0
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/Temporal')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp | 55 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Temporal/Duration.h | 34 |
2 files changed, 39 insertions, 50 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp index 30631f498a..4e725e04f3 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp @@ -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()); } - // 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 {}; - // 4. Let any be false. - auto any = false; - - // 5. For each row of Table 7, except the header row, in table order, do - for (auto& [field, property] : temporal_duration_like_properties<DurationRecord, double>(vm)) { - // a. Let prop be the Property Name value of the current row. + // 4. Let partial be ? ToPartialDuration(temporalDurationLike). + auto partial = TRY(to_partial_duration(global_object, temporal_duration_like)); - // b. Let val be ? Get(temporalDurationLike, prop). - auto value = TRY(temporal_duration_like.as_object().get(property)); + auto duration_record_fields = temporal_duration_record_fields<DurationRecord, double>(vm); + auto partial_duration_record_fields = temporal_duration_record_fields<PartialDurationRecord, Optional<double>>(vm); - // c. If val is undefined, then - if (value.is_undefined()) { - // 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; + // 5. For each row of Table 8, except the header row, in table order, do + 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; - // ii. Let val be ? ToIntegerWithoutRounding(val). - auto value_integer = TRY(to_integer_without_rounding(global_object, value, ErrorType::TemporalInvalidDurationPropertyValueNonIntegral, property.as_string(), value.to_string_without_side_effects())); + // b. Let value be the value of the field of partial whose name is fieldName. + auto value = partial.*partial_field_name; - // iii. Set result's field whose name is the Field Name value of the current row to val. - result.*field = value_integer; + // c. If value is not undefined, then + if (value.has_value()) { + // i. Set the field of result whose name is fieldName to value. + result.*field_name = *value; } } - // 6. If any 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 + // 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 (!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. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidDuration); } - // 8. Return result. + // 7. Return result. return result; } @@ -325,7 +313,7 @@ ThrowCompletionOr<PartialDurationRecord> to_partial_duration(GlobalObject& globa auto any = false; // 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. // b. Let value be ? Get(temporalDurationLike, property). @@ -339,8 +327,9 @@ ThrowCompletionOr<PartialDurationRecord> to_partial_duration(GlobalObject& globa // 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())); - // iii. Set result's field whose name is the Field Name value of the current row to value. - result.*field = value_integer; + // iii. Let fieldName be the Field Name value of the current row. + // iv. Set the field of result whose name is fieldName to value. + result.*field_name = value_integer; } } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Duration.h b/Userland/Libraries/LibJS/Runtime/Temporal/Duration.h index 044cfd68ef..0187b97c57 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Duration.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Duration.h @@ -109,28 +109,28 @@ struct RoundedDuration { 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> -struct TemporalDurationLikeProperty { - ValueT StructT::*field { nullptr }; - PropertyKey property; +struct TemporalDurationRecordField { + ValueT StructT::*field_name { nullptr }; + PropertyKey property_name; }; 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 }, +auto temporal_duration_record_fields = [](VM& vm) { + using FieldT = TemporalDurationRecordField<StructT, ValueT>; + return AK::Array { + FieldT { &StructT::days, vm.names.days }, + FieldT { &StructT::hours, vm.names.hours }, + FieldT { &StructT::microseconds, vm.names.microseconds }, + FieldT { &StructT::milliseconds, vm.names.milliseconds }, + FieldT { &StructT::minutes, vm.names.minutes }, + FieldT { &StructT::months, vm.names.months }, + FieldT { &StructT::nanoseconds, vm.names.nanoseconds }, + FieldT { &StructT::seconds, vm.names.seconds }, + FieldT { &StructT::weeks, vm.names.weeks }, + FieldT { &StructT::years, vm.names.years }, }; }; |