LibJS: Throw RangeError for non-integral values in ToPartialDuration

This is a normative change in the Temporal spec.

See: https://github.com/tc39/proposal-temporal/commit/895c8e5
This commit is contained in:
Linus Groh 2021-09-02 18:58:50 +01:00
parent 7acd174c85
commit 0e6d503317
Notes: sideshowbarker 2024-07-18 04:52:36 +09:00
2 changed files with 19 additions and 7 deletions

View file

@ -214,13 +214,20 @@ PartialDuration to_partial_duration(GlobalObject& global_object, Value temporal_
// i. Set any to true.
any = true;
// ii. Set value to ? ToIntegerOrInfinity(value).
auto value_number = value.to_integer_or_infinity(global_object);
// ii. Set value to ? ToNumber(value).
value = value.to_number(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;
// iii. If ! IsIntegralNumber(value) is false, then
if (!value.is_integral_number()) {
// 1. Throw a RangeError exception.
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDurationPropertyValueNonIntegral, property.as_string(), value.to_string_without_side_effects());
return {};
}
// iv. Set result's internal slot whose name is the Internal Slot value of the current row to value.
result.*internal_slot = value.as_double();
}
}

View file

@ -79,9 +79,14 @@ describe("errors", () => {
test("invalid duration value", () => {
for (const property of DURATION_PROPERTIES) {
expect(() => {
new Temporal.Duration().with({ [property]: Infinity });
}).toThrowWithMessage(RangeError, "Invalid duration");
for (const value of [1.23, NaN, Infinity]) {
expect(() => {
new Temporal.Duration().with({ [property]: value });
}).toThrowWithMessage(
RangeError,
`Invalid value for duration property '${property}': must be an integer, got ${value}`
);
}
}
});
});