summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2022-03-10 19:47:38 +0100
committerLinus Groh <mail@linusgroh.de>2022-03-10 23:20:39 +0100
commit55f973331621f45dbf395e47c8afb4c3f2d233e1 (patch)
tree8fb8ad621937517ec54588d4857a24657127a6c6
parentf75052ff7c6e1e32bd1944f7209fb294a6da7e23 (diff)
downloadserenity-55f973331621f45dbf395e47c8afb4c3f2d233e1.zip
LibJS: Add missing check in ParseTemporalInstant
This is an editorial change in the Temporal spec. See: https://github.com/tc39/proposal-temporal/commit/baead4d
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp13
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.from.js16
2 files changed, 27 insertions, 2 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp
index 0ae6b23cf5..d40d0c6624 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp
@@ -127,8 +127,17 @@ ThrowCompletionOr<BigInt*> parse_temporal_instant(GlobalObject& global_object, S
// 7. Let offsetNanoseconds be ? ParseTimeZoneOffsetString(offsetString).
auto offset_nanoseconds = TRY(parse_time_zone_offset_string(global_object, *offset_string));
- // 8. Return utc − offsetNanoseconds.
- return js_bigint(vm, utc->big_integer().minus(Crypto::SignedBigInteger::create_from(offset_nanoseconds)));
+ // 8. Let result be utc − offsetNanoseconds.
+ auto* result_ns = js_bigint(vm, utc->big_integer().minus(Crypto::SignedBigInteger::create_from(offset_nanoseconds)));
+
+ // 9. If ! IsValidEpochNanoseconds(ℤ(result)) is false, then
+ if (!is_valid_epoch_nanoseconds(*result_ns)) {
+ // a. Throw a RangeError exception.
+ return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
+ }
+
+ // 10. Return result.
+ return result_ns;
}
// 8.5.5 CompareEpochNanoseconds ( epochNanosecondsOne, epochNanosecondsTwo ), https://tc39.es/proposal-temporal/#sec-temporal-compareepochnanoseconds
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.from.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.from.js
index 4192ab867c..177dd000ab 100644
--- a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.from.js
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.from.js
@@ -32,4 +32,20 @@ describe("errors", () => {
Temporal.Instant.from("foo");
}).toThrowWithMessage(RangeError, "Invalid instant string 'foo'");
});
+
+ test("invalid epoch nanoseconds", () => {
+ // Test cases from https://github.com/tc39/proposal-temporal/commit/baead4d85bc3e9ecab1e9824c3d3fe4fdd77fc3a
+ expect(() => {
+ Temporal.Instant.from("-271821-04-20T00:00:00+00:01");
+ }).toThrowWithMessage(
+ RangeError,
+ "Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
+ );
+ expect(() => {
+ Temporal.Instant.from("+275760-09-13T00:00:00-00:01");
+ }).toThrowWithMessage(
+ RangeError,
+ "Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
+ );
+ });
});