1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:37:35 +00:00

LibJS: Implement Temporal.Duration.prototype.with()

This commit is contained in:
Linus Groh 2021-07-16 19:30:52 +01:00
parent 510f668ae3
commit 9aa1e4b885
7 changed files with 253 additions and 0 deletions

View file

@ -70,6 +70,75 @@ bool is_valid_duration(double years, double months, double weeks, double days, d
return true;
}
// 7.5.6 ToPartialDuration ( temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal-topartialduration
PartialDuration to_partial_duration(GlobalObject& global_object, Value temporal_duration_like)
{
auto& vm = global_object.vm();
// 1. If Type(temporalDurationLike) is not Object, then
if (!temporal_duration_like.is_object()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, temporal_duration_like.to_string_without_side_effects());
return {};
}
// 2. Let result be the new Record { [[Years]]: undefined, [[Months]]: undefined, [[Weeks]]: undefined, [[Days]]: undefined, [[Hours]]: undefined, [[Minutes]]: undefined, [[Seconds]]: undefined, [[Milliseconds]]: undefined, [[Microseconds]]: undefined, [[Nanoseconds]]: undefined }.
auto result = PartialDuration {};
// 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) {
// a. Let property be the Property value of the current row.
// b. Let value be ? Get(temporalDurationLike, property).
auto value = temporal_duration_like.as_object().get(property);
if (vm.exception())
return {};
// c. If value is not undefined, then
if (!value.is_undefined()) {
// i. Set any to true.
any = true;
// ii. Set value to ? ToIntegerOrInfinity(value).
auto value_number = value.to_integer_or_infinity(global_object);
if (vm.exception())
return {};
// iii. Set result's internal slot whose name is the Internal Slot value of the current row to value.
result.*internal_slot = value_number;
}
}
// 5. If any is false, then
if (!any) {
// a. Throw a TypeError exception.
vm.throw_exception<TypeError>(global_object, ErrorType::TemporalInvalidDurationLikeObject);
return {};
}
// 6. Return result.
return result;
}
// 7.5.7 CreateTemporalDuration ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalduration
Duration* create_temporal_duration(GlobalObject& global_object, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, FunctionObject* new_target)
{