nish 0.49.1 → 0.50.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/README.md +27 -21
  2. package/docs/AI.md +233 -159
  3. package/docs/INSTALL.md +1 -1
  4. package/package.json +12 -12
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +12 -9
  7. package/std/README.md +8 -5
  8. package/std/base64.ts +74 -1
  9. package/std/bytes.ts +201 -93
  10. package/std/compress/deflate.ts +20 -15
  11. package/std/crypto/base64url.ts +93 -66
  12. package/std/crypto/bcrypt.ts +25 -21
  13. package/std/crypto/hmac.ts +1 -1
  14. package/std/crypto/jwt.ts +32 -19
  15. package/std/crypto/p256.ts +9 -12
  16. package/std/crypto/pki-roots.ts +7 -4
  17. package/std/crypto/pki.ts +43 -55
  18. package/std/crypto/rsa-sign.ts +1632 -0
  19. package/std/crypto/rsa.ts +26 -4
  20. package/std/crypto/sha256.ts +135 -68
  21. package/std/crypto/x25519.ts +12 -11
  22. package/std/crypto/x509.ts +164 -40
  23. package/std/gpu/sys.ts +490 -0
  24. package/std/json-equal.ts +33 -18
  25. package/std/json-format.ts +39 -8
  26. package/std/json-number.ts +15 -3
  27. package/std/json-tokenizer.ts +4 -3
  28. package/std/json-typed.ts +12 -6
  29. package/std/json-value.ts +222 -104
  30. package/std/json.ts +81 -46
  31. package/std/net/dns-config.ts +18 -5
  32. package/std/net/dns-message.ts +60 -27
  33. package/std/net/dns-resolver.ts +35 -16
  34. package/std/net/dns.ts +3 -2
  35. package/std/net/fetch.ts +120 -38
  36. package/std/net/handler.ts +178 -66
  37. package/std/net/hpack.ts +192 -98
  38. package/std/net/http-compress.ts +83 -30
  39. package/std/net/http-fields.ts +327 -31
  40. package/std/net/http-server.ts +317 -20
  41. package/std/net/http1-client-codec.ts +137 -40
  42. package/std/net/http1-codec.ts +391 -202
  43. package/std/net/http1-server.ts +220 -38
  44. package/std/net/http2-cleartext.ts +10 -4
  45. package/std/net/http2-frame.ts +2 -2
  46. package/std/net/http2-tls.ts +3 -3
  47. package/std/net/http2.ts +69 -15
  48. package/std/net/http3-frame.ts +11 -14
  49. package/std/net/http3-server.ts +12 -15
  50. package/std/net/http3.ts +41 -29
  51. package/std/net/multipart.ts +592 -0
  52. package/std/net/outbound.ts +24 -8
  53. package/std/net/postgres-async.ts +92 -54
  54. package/std/net/postgres-carrier.ts +28 -4
  55. package/std/net/postgres.ts +166 -65
  56. package/std/net/prefork.ts +86 -5
  57. package/std/net/protobuf.ts +6 -2
  58. package/std/net/qpack.ts +66 -25
  59. package/std/net/quic-conn-ack.ts +27 -7
  60. package/std/net/quic-conn-cid.ts +13 -7
  61. package/std/net/quic-datagram.ts +6 -5
  62. package/std/net/quic-frame.ts +67 -23
  63. package/std/net/quic-listener.ts +19 -12
  64. package/std/net/quic-recovery.ts +11 -9
  65. package/std/net/quic-stream.ts +7 -5
  66. package/std/net/quic.ts +36 -31
  67. package/std/net/router.ts +136 -24
  68. package/std/net/serve.ts +59 -5
  69. package/std/net/static.ts +81 -33
  70. package/std/net/tls/record.ts +26 -13
  71. package/std/net/tls/ticket.ts +6 -5
  72. package/std/net/tls-tcp.ts +69 -30
  73. package/std/net/tls.ts +152 -38
  74. package/std/net/upstream.ts +65 -19
  75. package/std/net/url.ts +97 -78
  76. package/std/net/websocket.ts +16 -9
  77. package/std/net/webtransport.ts +28 -12
  78. package/std/regex.ts +600 -388
  79. package/std/sqlite.ts +37 -19
  80. package/std/sync/ring.ts +232 -115
  81. package/std/sync/shm.ts +11 -4
  82. package/std/temporal.ts +232 -133
  83. package/std/term-io.ts +12 -6
  84. package/std/term-pixels.ts +158 -22
  85. package/std/term-rate.ts +8 -3
  86. package/std/term.ts +4 -4
  87. package/std/text.ts +33 -11
  88. package/std/yaml-json.ts +238 -106
  89. package/std/yaml-lines.ts +47 -27
  90. package/std/yaml-write.ts +58 -24
  91. package/std/yaml.ts +808 -395
package/std/temporal.ts CHANGED
@@ -115,36 +115,53 @@ const temporalRangeError = (what: string): void => {
115
115
  panic(`RangeError: ${what}`)
116
116
  }
117
117
 
118
- // The checked sums. Each wraps in `u64`, where a sum cannot panic, and then
119
- // tests the signs: `a + b` overflowed exactly when `a` and `b` share a sign the
120
- // result lacks, and `a - b` exactly when `a` and `b` differ in sign and the
121
- // result's sign is not `a`'s. Past the i64 range JavaScript's Temporal throws a
122
- // `RangeError` long before, so these raise one too.
118
+ // The checked sums. Each splits its operands into a high and a low 32-bit
119
+ // half and sums the halves, which never overflow: the result fits the i64
120
+ // range exactly when the high half's sum fits an `i32`, and is that half
121
+ // shifted back over the low one. Past the i64 range JavaScript's Temporal
122
+ // throws a `RangeError` long before, so these raise one too.
123
+
124
+ /**
125
+ * The `i64` whose bits from 32 up are `high` and whose low 32 bits are those
126
+ * of `low`, raising a `RangeError` where `high` does not fit an `i32`.
127
+ */
128
+ const temporalJoin = (high: i64, low: i64): i64 => {
129
+ const h: i32 = toI32(high)
130
+ if (toI64(h) !== high) {
131
+ temporalRangeError("the arithmetic leaves the integer range")
132
+ }
133
+ return (toI64(h) << 32) | (low & 4294967295)
134
+ }
123
135
 
124
136
  /** `a + b`, raising a `RangeError` where it leaves the i64 range. */
125
137
  const temporalAdd = (a: i64, b: i64): i64 => {
126
- const r: i64 = toI64(toU64(a) + toU64(b))
127
- if (((a ^ r) & (b ^ r)) < 0) {
128
- temporalRangeError("the arithmetic leaves the integer range")
138
+ // Operands under 2^62 in magnitude, a shift left and back leaving them as
139
+ // they are, sum without overflow; the halves are the path for the rest.
140
+ if ((a << 1) >> 1 === a && (b << 1) >> 1 === b) {
141
+ return ((a << 1) >> 1) + ((b << 1) >> 1)
129
142
  }
130
- return r
143
+ const low: i64 = (a & 4294967295) + (b & 4294967295)
144
+ const high: i64 = (a >> 32) + (b >> 32) + (low >> 32)
145
+ return temporalJoin(high, low)
131
146
  }
132
147
 
133
148
  /** `a - b`, raising a `RangeError` where it leaves the i64 range. */
134
149
  const temporalSub = (a: i64, b: i64): i64 => {
135
- const r: i64 = toI64(toU64(a) - toU64(b))
136
- if (((a ^ b) & (a ^ r)) < 0) {
137
- temporalRangeError("the arithmetic leaves the integer range")
150
+ if ((a << 1) >> 1 === a && (b << 1) >> 1 === b) {
151
+ return ((a << 1) >> 1) - ((b << 1) >> 1)
138
152
  }
139
- return r
153
+ // The low halves' difference is in (-2^32, 2^32): its shift is the borrow.
154
+ const low: i64 = (a & 4294967295) - (b & 4294967295)
155
+ const high: i64 = (a >> 32) - (b >> 32) + (low >> 32)
156
+ return temporalJoin(high, low)
140
157
  }
141
158
 
142
159
  /**
143
160
  * A `Duration` field negated. The constructor refuses the least `i64` in every
144
161
  * field (its bounds, or for microseconds and nanoseconds, a test of its own),
145
- * so the wrapping negation is exact.
162
+ * so the checked negation never raises.
146
163
  */
147
- const temporalNegField = (x: i64): i64 => toI64(0 - toU64(x))
164
+ const temporalNegField = (x: i64): i64 => temporalSub(0, x)
148
165
 
149
166
  /** The magnitude of a `Duration` field `x` whose sign is negative when `negative`. */
150
167
  const temporalMagnitude = (x: i64, negative: boolean): i64 => (negative ? temporalNegField(x) : x)
@@ -152,13 +169,20 @@ const temporalMagnitude = (x: i64, negative: boolean): i64 => (negative ? tempor
152
169
  /** `x` negated when `negative`, raising a `RangeError` where that leaves the i64 range. */
153
170
  const temporalSigned = (x: i64, negative: boolean): i64 => (negative ? temporalSub(0, x) : x)
154
171
 
155
- /** `a * k` for a factor `k` of at least 1, raising a `RangeError` where it leaves the i64 range. */
156
- const temporalMul = (a: i64, k: i64): i64 => {
157
- const r: i64 = toI64(toU64(a) * toU64(k))
158
- if (k < 1 || r / k !== a) {
159
- temporalRangeError("the arithmetic leaves the integer range")
172
+ /** `a * k` for a factor `k` from 1 to 2^20, raising a `RangeError` where it leaves the i64 range. */
173
+ const temporalMul = (a: i64, k: integer<1, 1048576>): i64 => {
174
+ // The low half goes in two 16-bit pieces, so every product and sum below
175
+ // stays under 2^53: `mid` is the low half's product over 2^16.
176
+ // An `a` that fits an `i32` multiplies directly: the product is under 2^51.
177
+ const small: i32 = toI32(a)
178
+ if (toI64(small) === a) {
179
+ return toI64(small) * toI64(k)
160
180
  }
161
- return r
181
+ const k64: i64 = toI64(k)
182
+ const bottom: i64 = (a & 65535) * k64
183
+ const mid: i64 = ((a >> 16) & 65535) * k64 + (bottom >> 16)
184
+ const high: i64 = (a >> 32) * k64 + (mid >> 16)
185
+ return temporalJoin(high, ((mid & 65535) << 16) | (bottom & 65535))
162
186
  }
163
187
 
164
188
  // The divisions. Every divisor here is a constant from 2 to a day's
@@ -188,6 +212,16 @@ const temporalFloorMod = (a: i64, b: i64): i64 => {
188
212
  return r < 0 ? r + b : r
189
213
  }
190
214
 
215
+ /**
216
+ * `a` modulo `b` in `[0, b)`, for `2 <= b <= 10^9`: `temporalFloorMod` with
217
+ * a remainder that `i32` holds, so the sums built on it need no check.
218
+ */
219
+ const temporalModSmall = (a: i64, b: integer<2, 1000000000>): integer<0, 999999999> => {
220
+ // The remainder is in `[0, b)`, so the truncation is exact and the test always true.
221
+ const r: i32 = toI32(temporalFloorMod(a, toI64(b)))
222
+ return r >= 0 && r <= 999999999 ? r : 0
223
+ }
224
+
191
225
  /** `a / b` rounded toward zero, for `2 <= b <= TEMPORAL_MAX_DIVISOR`. */
192
226
  const temporalTruncDiv = (a: i64, b: i64): i64 => {
193
227
  if (b < 2 || b > TEMPORAL_MAX_DIVISOR) {
@@ -574,32 +608,38 @@ const temporalEpochDay = (year: i32, month: i32, day: i32): i64 => {
574
608
  /** The days from 1 January to the given date, plus one, in a year that is a leap year when `leap`. */
575
609
  const temporalDayOfYear = (month: i32, day: i32, leap: boolean): i32 => {
576
610
  const before: i32 = month >= 1 && month <= 12 ? TEMPORAL_DAYS_BEFORE_MONTH[month - 1] : 0
577
- // A checked day is below 32, so the `u32` sum is exact.
578
- return toI32(toU32(before) + toU32(day) + (leap && month > 2 ? 1 : 0))
611
+ // A checked day is below 32 and `before` below 335: the `i64` sum is exact and fits an `i32`.
612
+ return toI32(toI64(before) + toI64(day) + toI64(leap && month > 2 ? 1 : 0))
579
613
  }
580
614
 
581
615
  /**
582
616
  * The ISO weekday of an epoch day in Temporal's range: 1 for Monday to 7 for
583
- * Sunday. The day counts from 1,000 eras before 0000-03-01, a nonnegative `u64`
584
- * there, and 146,816,470 days is 3 more than a whole number of weeks, which
585
- * puts 1970-01-01, a Thursday, on 4.
617
+ * Sunday. The day counts from 1,000 eras before 0000-03-01, nonnegative there,
618
+ * and 146,816,470 days is 3 more than a whole number of weeks, which puts
619
+ * 1970-01-01, a Thursday, on 4.
586
620
  */
587
- const temporalDayOfWeek = (epochDay: i64): i32 => toI32(((toU64(epochDay) + 146816470) % 7) + 1)
621
+ const temporalDayOfWeek = (epochDay: i64): i32 => {
622
+ // Every caller checked the day is in Temporal's range, far inside `i32`, so
623
+ // the truncation is exact and the test always true.
624
+ const day: i32 = toI32(epochDay)
625
+ const up: i32 = day >= -146816470 && day <= 146816470 ? day + 146816470 : 0
626
+ return (up % 7) + 1
627
+ }
588
628
 
589
629
  /** How many ISO weeks `year` has: 53 when it starts on a Thursday, or is a leap year starting on a Wednesday. */
590
630
  const temporalWeeksInYear = (year: i32): i32 =>
591
- temporalWeekdayOfDecember31(toI64(year)) === 4 || temporalWeekdayOfDecember31(toI64(year) - 1) === 3
592
- ? 53
593
- : 52
631
+ temporalWeekdayOfDecember31(year, 0) === 4 || temporalWeekdayOfDecember31(year, 1) === 3 ? 53 : 52
594
632
 
595
633
  /**
596
- * The weekday of 31 December of `y`, a year in Temporal's range, 0 for Sunday
597
- * to 6 for Saturday. The year counts from 400,000 years before 0, a
598
- * nonnegative `u64` there; the shift adds 497,000 days, a whole number of
599
- * weeks, so the weekday is unchanged.
634
+ * The weekday of 31 December of the year `back` years before `y`, a year in
635
+ * Temporal's range, 0 for Sunday to 6 for Saturday. The year counts from
636
+ * 400,000 years before 0, nonnegative there; the shift adds 497,000 days, a
637
+ * whole number of weeks, so the weekday is unchanged.
600
638
  */
601
- const temporalWeekdayOfDecember31 = (y: i64): i64 => {
602
- const ys: u64 = toU64(y) + 400000
639
+ const temporalWeekdayOfDecember31 = (y: i32, back: integer<0, 1>): i64 => {
640
+ // Temporal's years are within 275,760 of 0, so the test is always true.
641
+ const up: i32 = y >= -399999 && y <= 400000 ? y + 400000 - back : 0
642
+ const ys: integer<0, 800000> = up >= 0 && up <= 800000 ? up : 0
603
643
  return toI64((ys + ys / 4 - ys / 100 + ys / 400) % 7)
604
644
  }
605
645
 
@@ -712,15 +752,15 @@ const temporalDateOfEpochDay = (epochDay: i64): PlainDate => {
712
752
  * stays below 2^31 over Temporal's whole range. `temporalCivilYear`,
713
753
  * `temporalCivilMonth` and `temporalCivilDay` take it apart. Every caller has
714
754
  * checked the day is in that range, so the test is never true for one here.
715
- * The days count from 0000-03-01 less 1,000 eras of 400 years, so every term
716
- * is a nonnegative `u64` below 2^28 and the arithmetic is exact.
755
+ * The days count from 0000-03-01 less 1,000 eras of 400 years, so in range
756
+ * every term is a nonnegative `u64` below 2^28 and the arithmetic is exact.
717
757
  */
718
758
  const temporalCivilOfEpochDay = (epochDay: i64): i64 => {
719
- const z: u64 = toU64(epochDay) + 146816468
720
- if (z < 46816467 || z > 246816468) {
759
+ if (epochDay < -100000001 || epochDay > 100000000) {
721
760
  temporalRangeError("PlainDate: the date is outside Temporal's range")
722
761
  return 0
723
762
  }
763
+ const z: u64 = toU64(epochDay + 146816468)
724
764
  const doe: u64 = z % 146097
725
765
  const yoe: u64 = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365
726
766
  const doy: u64 = doe - (yoe * 365 + yoe / 4 - yoe / 100)
@@ -731,8 +771,8 @@ const temporalCivilOfEpochDay = (epochDay: i64): i64 => {
731
771
  return toI64(year * 512 + month * 32 + day)
732
772
  }
733
773
 
734
- /** The year of a packed civil date: the packed year less 400,000, which `u32` holds and `i32` reads back exactly. */
735
- const temporalCivilYear = (civil: i64): i32 => toI32(toU32(toI64(civil / 512)) - 400000)
774
+ /** The year of a packed civil date: the packed year less 400,000, which a packed date's year keeps within `i32`. */
775
+ const temporalCivilYear = (civil: i64): i32 => toI32(civil / 512 - 400000)
736
776
 
737
777
  /** The month of a packed civil date. */
738
778
  const temporalCivilMonth = (civil: i64): i32 => toI32(toI64(civil / 32) % 16)
@@ -839,7 +879,7 @@ const temporalDurationSeconds = (d: Duration, withDays: boolean): i64 => {
839
879
  d.seconds
840
880
  )
841
881
  if (withDays) {
842
- seconds = temporalAdd(seconds, temporalMul(d.days, TEMPORAL_SECONDS_PER_DAY))
882
+ seconds = temporalAdd(seconds, temporalMul(d.days, 86400))
843
883
  }
844
884
  if (d.milliseconds !== 0) {
845
885
  seconds = temporalAdd(seconds, temporalFloorDiv(d.milliseconds, 1000))
@@ -898,8 +938,7 @@ const temporalMovedEpochDay = (
898
938
  if (years !== 0 || months !== 0) {
899
939
  const month1: i64 = temporalSub(temporalAdd(toI64(month0), months), 1)
900
940
  const year: i64 = temporalAdd(temporalAdd(toI64(year0), years), temporalFloorDiv(month1, 12))
901
- // The remainder is in [0, 12), so the `u64` sum is exact.
902
- const month: i64 = toI64(toU64(temporalFloorMod(month1, 12)) + 1)
941
+ const month: i64 = toI64(temporalModSmall(month1, 12)) + 1
903
942
  if (year < -271821 || year > 275760) {
904
943
  return TEMPORAL_MAX_EPOCH_DAY + 1
905
944
  }
@@ -974,7 +1013,10 @@ const temporalBalance = (span: TemporalSpan, largest: i32): Duration => {
974
1013
  ms = 0
975
1014
  } else {
976
1015
  ns = temporalAdd(
977
- temporalAdd(temporalAdd(temporalMul(s, 1000000000), temporalMul(ms, 1000000)), temporalMul(us, 1000)),
1016
+ temporalAdd(
1017
+ temporalAdd(temporalMul(temporalMul(s, 1000000), 1000), temporalMul(ms, 1000000)),
1018
+ temporalMul(us, 1000)
1019
+ ),
978
1020
  ns
979
1021
  )
980
1022
  ms = 0
@@ -1262,11 +1304,14 @@ class TemporalReader {
1262
1304
  this.ok = false
1263
1305
  return
1264
1306
  }
1265
- // Every field is a small count of digits here, so the u64 sum never wraps.
1266
- const total: u64 =
1267
- (toU64(hours) * 3600 + toU64(minutes) * 60 + toU64(seconds)) * 1000000000 + toU64(fraction)
1307
+ // The tests above leave each field in range, so these restate the ranges for the sum.
1308
+ const h: integer<0, 23> = hours >= 0 && hours <= 23 ? hours : 0
1309
+ const m: integer<0, 59> = minutes >= 0 && minutes <= 59 ? minutes : 0
1310
+ const sec: integer<0, 59> = seconds >= 0 && seconds <= 59 ? seconds : 0
1311
+ const frac: integer<0, 999999999> = fraction >= 0 && fraction <= 999999999 ? fraction : 0
1312
+ const total: i64 = (toI64(h) * 3600 + toI64(m) * 60 + toI64(sec)) * 1000000000 + toI64(frac)
1268
1313
  this.hasOffset = true
1269
- this.offsetNanos = c === 43 ? toI64(total) : toI64(0 - total)
1314
+ this.offsetNanos = c === 43 ? total : 0 - total
1270
1315
  }
1271
1316
 
1272
1317
  /**
@@ -1479,7 +1524,7 @@ const temporalCharAt = (text: string, at: i32): i32 =>
1479
1524
  const temporalIsTimeDesignator = (c: i32): boolean => c === 84 || c === 116 || c === 32
1480
1525
 
1481
1526
  /** The two decimal digits at `at` as a number, or -1 when either is not a digit. */
1482
- const temporalFastTwo = (text: string, at: i32): i32 => {
1527
+ const temporalFastTwo = (text: string, at: i32): integer<-1, 99> => {
1483
1528
  if (at < 0 || at >= toI32(text.length) - 1) {
1484
1529
  return -1
1485
1530
  }
@@ -1489,7 +1534,9 @@ const temporalFastTwo = (text: string, at: i32): i32 => {
1489
1534
  if (high > 9 || low > 9) {
1490
1535
  return -1
1491
1536
  }
1492
- return toI32(high * 10 + low)
1537
+ // `high` and `low` are digits, so the test is always true; it states the range.
1538
+ const v: i32 = toI32(high * 10 + low)
1539
+ return v >= 0 && v <= 99 ? v : -1
1493
1540
  }
1494
1541
 
1495
1542
  /**
@@ -1498,8 +1545,8 @@ const temporalFastTwo = (text: string, at: i32): i32 => {
1498
1545
  * with one in exactly that form or the date does not exist.
1499
1546
  */
1500
1547
  const temporalFastDate = (text: string): i64 => {
1501
- const century: i32 = temporalFastTwo(text, 0)
1502
- const yy: i32 = temporalFastTwo(text, 2)
1548
+ const century: integer<-1, 99> = temporalFastTwo(text, 0)
1549
+ const yy: integer<-1, 99> = temporalFastTwo(text, 2)
1503
1550
  const month: i32 = temporalFastTwo(text, 5)
1504
1551
  const day: i32 = temporalFastTwo(text, 8)
1505
1552
  if (century < 0 || yy < 0 || month < 1 || month > 12 || day < 1) {
@@ -1508,12 +1555,11 @@ const temporalFastDate = (text: string): i64 => {
1508
1555
  if (temporalCharAt(text, 4) !== 45 || temporalCharAt(text, 7) !== 45) {
1509
1556
  return -1
1510
1557
  }
1511
- // Each field is two digits, so the unsigned sums never wrap.
1512
- const year: u32 = toU32(century) * 100 + toU32(yy)
1558
+ const year: i32 = century * 100 + yy
1513
1559
  if (day > temporalDaysInMonth(toI64(year), month)) {
1514
1560
  return -1
1515
1561
  }
1516
- return toI64((toU64(year) + 400000) * 512 + toU64(month) * 32 + toU64(day))
1562
+ return (toI64(year) + 400000) * 512 + toI64(month) * 32 + toI64(day)
1517
1563
  }
1518
1564
 
1519
1565
  /**
@@ -1562,8 +1608,7 @@ const temporalFastTime = (text: string, end: i32): i64 => {
1562
1608
  if (i !== end) {
1563
1609
  return -1
1564
1610
  }
1565
- // Two-digit fields and a nine-digit fraction: the u64 sum never wraps.
1566
- return toI64((toU64(hour) * 3600 + toU64(minute) * 60 + toU64(second)) * 1000000000 + toU64(fraction))
1611
+ return (toI64(hour) * 3600 + toI64(minute) * 60 + toI64(second)) * 1000000000 + toI64(toI32(fraction))
1567
1612
  }
1568
1613
 
1569
1614
  /**
@@ -1980,10 +2025,11 @@ export class PlainTime {
1980
2025
  */
1981
2026
  toString(): string {
1982
2027
  const out: u8[] = new Array<u8>(TEMPORAL_TEXT_MAX)
1983
- // Each field is under 1000, so the u32 sum never wraps.
1984
- const sub: u32 =
1985
- toU32(this.millisecond) * 1000000 + toU32(this.microsecond) * 1000 + toU32(this.nanosecond)
1986
- const end: i32 = temporalPutTime(out, 0, this.hour, this.minute, this.second, toI32(sub))
2028
+ // The nanoseconds past the second: each field is under 1000, so this is exact.
2029
+ const sub: i32 = toI32(
2030
+ toI64(this.millisecond) * 1000000 + toI64(this.microsecond) * 1000 + toI64(this.nanosecond)
2031
+ )
2032
+ const end: i32 = temporalPutTime(out, 0, this.hour, this.minute, this.second, sub)
1987
2033
  return temporalText(out, end)
1988
2034
  }
1989
2035
 
@@ -2015,11 +2061,11 @@ export class PlainTime {
2015
2061
  */
2016
2062
  add(duration: Duration): PlainTime {
2017
2063
  const span: TemporalSpan = temporalSpanOfDuration(duration, false)
2018
- const seconds: i64 = temporalFloorMod(span.seconds, TEMPORAL_SECONDS_PER_DAY)
2019
- // `seconds` is below a day, `span.nanos` below a second and `nanoOfDay` below a day's
2020
- // nanoseconds, so the `u64` sum stays under 2 × 10^14 and never wraps.
2021
- const ns: u64 = toU64(seconds) * 1000000000 + toU64(span.nanos) + toU64(this.nanoOfDay)
2022
- return temporalTimeOfNanos(temporalFloorMod(toI64(ns), TEMPORAL_NS_PER_DAY))
2064
+ const seconds: i32 = temporalModSmall(span.seconds, 86400)
2065
+ // `seconds` is below a day and `span.nanos` below a second, so their sum
2066
+ // in nanoseconds is under 10^14; `nanoOfDay` is below a day's nanoseconds.
2067
+ const ns: i64 = temporalAdd(toI64(seconds) * 1000000000 + toI64(span.nanos), this.nanoOfDay)
2068
+ return temporalTimeOfNanos(temporalFloorMod(ns, TEMPORAL_NS_PER_DAY))
2023
2069
  }
2024
2070
 
2025
2071
  /**
@@ -2299,9 +2345,9 @@ export class PlainDateTime {
2299
2345
  */
2300
2346
  toString(): string {
2301
2347
  const out: u8[] = new Array<u8>(TEMPORAL_TEXT_MAX)
2302
- // Each field is under 1000, so the u32 sum never wraps.
2348
+ // The nanoseconds past the second: each field is under 1000, so this is exact.
2303
2349
  const sub: i32 = toI32(
2304
- toU32(this.millisecond) * 1000000 + toU32(this.microsecond) * 1000 + toU32(this.nanosecond)
2350
+ toI64(this.millisecond) * 1000000 + toI64(this.microsecond) * 1000 + toI64(this.nanosecond)
2305
2351
  )
2306
2352
  if (this.year >= 0 && this.year <= 9999) {
2307
2353
  const seconds: i32 = temporalPutDateTime4(
@@ -2396,15 +2442,11 @@ export class PlainDateTime {
2396
2442
  let nanoOfDay: i64 = this.nanoOfDay
2397
2443
  if (!temporalNoTime(duration)) {
2398
2444
  const seconds: i64 = temporalDurationSeconds(duration, false)
2399
- // The duration's nanoseconds are below 3 × 10^9 and `nanoOfDay` below a day's,
2400
- // so neither `u64` sum below wraps.
2401
- const nanos: i64 = toI64(toU64(temporalDurationNanos(duration)) + toU64(this.nanoOfDay))
2445
+ const nanos: i64 = temporalAdd(temporalDurationNanos(duration), this.nanoOfDay)
2402
2446
  const total: i64 = temporalAdd(seconds, temporalFloorDiv(nanos, TEMPORAL_NS_PER_SECOND))
2403
2447
  carryDays = temporalFloorDiv(total, TEMPORAL_SECONDS_PER_DAY)
2404
- nanoOfDay = toI64(
2405
- toU64(temporalFloorMod(total, TEMPORAL_SECONDS_PER_DAY)) * 1000000000 +
2406
- toU64(temporalFloorMod(nanos, TEMPORAL_NS_PER_SECOND))
2407
- )
2448
+ nanoOfDay =
2449
+ toI64(temporalModSmall(total, 86400)) * 1000000000 + toI64(temporalModSmall(nanos, 1000000000))
2408
2450
  }
2409
2451
  const epochDay: i64 = temporalAddToDate(
2410
2452
  this.year,
@@ -2466,7 +2508,7 @@ export class PlainDateTime {
2466
2508
  /** The span from `a` to `b`, every day 24 hours. */
2467
2509
  const temporalDateTimeSpan = (a: PlainDateTime, b: PlainDateTime): TemporalSpan =>
2468
2510
  new TemporalSpan(
2469
- temporalMul(temporalSub(b.epochDay, a.epochDay), TEMPORAL_SECONDS_PER_DAY),
2511
+ temporalMul(temporalSub(b.epochDay, a.epochDay), 86400),
2470
2512
  temporalSub(b.nanoOfDay, a.nanoOfDay)
2471
2513
  )
2472
2514
 
@@ -2631,28 +2673,30 @@ export class Duration {
2631
2673
  if (years <= low || years >= c || months <= low || months >= c || weeks <= low || weeks >= c) {
2632
2674
  temporalRangeError("Duration: years, months and weeks must each be below 2^32")
2633
2675
  }
2634
- const m: i64 = TEMPORAL_MAX_DURATION_SECONDS
2676
+ // Within these bounds days to milliseconds come to at most 2^50 seconds
2677
+ // each, microseconds and nanoseconds to under 2^44 and their remainders'
2678
+ // carry to 2, so the fields come to under 2^53 seconds: only a field past
2679
+ // them has its sum taken.
2635
2680
  if (
2636
- days < -104249991374 ||
2637
- days > 104249991374 ||
2638
- hours < -2501999792983 ||
2639
- hours > 2501999792983 ||
2640
- minutes < -150119987579016 ||
2641
- minutes > 150119987579016 ||
2642
- seconds < TEMPORAL_MIN_DURATION_SECONDS ||
2643
- seconds > m
2681
+ days < -8589934592 ||
2682
+ days > 8589934592 ||
2683
+ hours < -274877906944 ||
2684
+ hours > 274877906944 ||
2685
+ minutes < -17592186044416 ||
2686
+ minutes > 17592186044416 ||
2687
+ seconds < -1125899906842624 ||
2688
+ seconds > 1125899906842624 ||
2689
+ milliseconds < -4503599627370496 ||
2690
+ milliseconds > 4503599627370496
2644
2691
  ) {
2645
- temporalRangeError("Duration: the time fields come to 2^53 seconds or more")
2646
- }
2647
- if (temporalWholeSeconds(days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds) > m) {
2648
- temporalRangeError("Duration: the time fields come to 2^53 seconds or more")
2692
+ temporalCheckLength(days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds)
2649
2693
  }
2650
2694
  // The least `i64` is under 2^53 seconds as microseconds or nanoseconds, but
2651
2695
  // its negation does not fit, so `negated` and `abs` could not answer it. It
2652
- // is the one negative value whose wrapping negation is negative too.
2696
+ // is the one negative value that a shift left by one takes to zero.
2653
2697
  if (
2654
- (microseconds < 0 && temporalNegField(microseconds) < 0) ||
2655
- (nanoseconds < 0 && temporalNegField(nanoseconds) < 0)
2698
+ (microseconds < 0 && microseconds << 1 === toI64(0)) ||
2699
+ (nanoseconds < 0 && nanoseconds << 1 === toI64(0))
2656
2700
  ) {
2657
2701
  temporalRangeError("Duration: microseconds and nanoseconds must be above the least i64")
2658
2702
  }
@@ -2714,12 +2758,9 @@ export class Duration {
2714
2758
  const us: u64 = toU64(temporalMagnitude(this.microseconds, k))
2715
2759
  const ns: u64 = toU64(temporalMagnitude(this.nanoseconds, k))
2716
2760
  let fraction: u64 = (ms % 1000) * 1000000 + (us % 1000000) * 1000 + (ns % 1000000000)
2717
- const wholeSeconds: i64 = toI64(
2718
- toU64(temporalMagnitude(this.seconds, k)) +
2719
- ms / 1000 +
2720
- us / 1000000 +
2721
- ns / 1000000000 +
2722
- fraction / 1000000000
2761
+ const wholeSeconds: i64 = temporalAdd(
2762
+ temporalMagnitude(this.seconds, k),
2763
+ toI64(ms / 1000 + us / 1000000 + ns / 1000000000 + fraction / 1000000000)
2723
2764
  )
2724
2765
  fraction = fraction % 1000000000
2725
2766
  const withSeconds: boolean = wholeSeconds !== 0 || fraction !== 0 || zeroAbove
@@ -2814,6 +2855,38 @@ export class Duration {
2814
2855
  }
2815
2856
  }
2816
2857
 
2858
+ /**
2859
+ * The constructor's test that the time fields, of one sign, come to under
2860
+ * 2^53 seconds, a `RangeError` where they do not: each field's own bound
2861
+ * first, so that their sum fits an `i64`, then the sum.
2862
+ */
2863
+ const temporalCheckLength = (
2864
+ days: i64,
2865
+ hours: i64,
2866
+ minutes: i64,
2867
+ seconds: i64,
2868
+ milliseconds: i64,
2869
+ microseconds: i64,
2870
+ nanoseconds: i64
2871
+ ): void => {
2872
+ const m: i64 = TEMPORAL_MAX_DURATION_SECONDS
2873
+ if (
2874
+ days < -104249991374 ||
2875
+ days > 104249991374 ||
2876
+ hours < -2501999792983 ||
2877
+ hours > 2501999792983 ||
2878
+ minutes < -150119987579016 ||
2879
+ minutes > 150119987579016 ||
2880
+ seconds < TEMPORAL_MIN_DURATION_SECONDS ||
2881
+ seconds > m
2882
+ ) {
2883
+ temporalRangeError("Duration: the time fields come to 2^53 seconds or more")
2884
+ }
2885
+ if (temporalWholeSeconds(days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds) > m) {
2886
+ temporalRangeError("Duration: the time fields come to 2^53 seconds or more")
2887
+ }
2888
+ }
2889
+
2817
2890
  /** `sign` after a field `n`: its sign, or a `RangeError` when it differs from a non-zero `sign`. */
2818
2891
  const temporalSignOf = (sign: i32, n: i64): i32 => {
2819
2892
  if (n === 0) {
@@ -2841,25 +2914,47 @@ const temporalWholeSeconds = (
2841
2914
  microseconds: i64,
2842
2915
  nanoseconds: i64
2843
2916
  ): i64 => {
2844
- // With the fields so bounded every term below is under 2^54 in magnitude, so
2845
- // the sums, done in wrapping `u64` arithmetic on the two's-complement bits,
2846
- // are the exact signed sums.
2847
- const remainders: i64 = toI64(
2848
- toU64(milliseconds % 1000) * 1000000 +
2849
- toU64(microseconds % 1000000) * 1000 +
2850
- toU64(nanoseconds % 1000000000)
2851
- )
2852
- const whole: i64 = toI64(
2853
- toU64(days) * 86400 +
2854
- toU64(hours) * 3600 +
2855
- toU64(minutes) * 60 +
2856
- toU64(seconds) +
2857
- toU64(milliseconds / 1000) +
2858
- toU64(microseconds / 1000000) +
2859
- toU64(nanoseconds / 1000000000) +
2860
- toU64(remainders / 1000000000)
2861
- )
2862
- return whole < 0 ? toI64(0 - toU64(whole)) : whole
2917
+ // With the fields so bounded every term below is under 2^54 in magnitude.
2918
+ // The terms are summed as two halves, the bits from 32 up in `high` and the
2919
+ // low 32 bits in `low`, so no partial sum overflows; a product's three
2920
+ // 16-bit pieces go to the halves they land in. The join carries `low` into
2921
+ // `high` and raises if the total leaves the `i64` range.
2922
+ const remainders: i64 =
2923
+ (milliseconds % 1000) * 1000000 + (microseconds % 1000000) * 1000 + (nanoseconds % 1000000000)
2924
+ const ms: i64 = milliseconds / 1000
2925
+ const us: i64 = microseconds / 1000000
2926
+ const ns: i64 = nanoseconds / 1000000000
2927
+ const rest: i64 = remainders / 1000000000
2928
+ const dMid: i64 = ((days >> 16) & 65535) * 86400
2929
+ const hMid: i64 = ((hours >> 16) & 65535) * 3600
2930
+ const mMid: i64 = ((minutes >> 16) & 65535) * 60
2931
+ const low: i64 =
2932
+ (days & 65535) * 86400 +
2933
+ (dMid & 65535) * 65536 +
2934
+ (hours & 65535) * 3600 +
2935
+ (hMid & 65535) * 65536 +
2936
+ (minutes & 65535) * 60 +
2937
+ (mMid & 65535) * 65536 +
2938
+ (seconds & 4294967295) +
2939
+ (ms & 4294967295) +
2940
+ (us & 4294967295) +
2941
+ (ns & 4294967295) +
2942
+ (rest & 4294967295)
2943
+ const high: i64 =
2944
+ (days >> 32) * 86400 +
2945
+ (dMid >> 16) +
2946
+ (hours >> 32) * 3600 +
2947
+ (hMid >> 16) +
2948
+ (minutes >> 32) * 60 +
2949
+ (mMid >> 16) +
2950
+ (seconds >> 32) +
2951
+ (ms >> 32) +
2952
+ (us >> 32) +
2953
+ (ns >> 32) +
2954
+ (rest >> 32) +
2955
+ (low >> 32)
2956
+ const whole: i64 = temporalJoin(high, low)
2957
+ return whole < 0 ? temporalSub(0, whole) : whole
2863
2958
  }
2864
2959
 
2865
2960
  /**
@@ -3083,7 +3178,7 @@ export const durationFromOrNull = (text: string): Duration | null => {
3083
3178
  return null
3084
3179
  }
3085
3180
  if (negative) {
3086
- // Every value is below 10^16, so the wrapping negation is exact.
3181
+ // Every value is below 10^16, so its negation fits and never raises.
3087
3182
  for (let i: i32 = 0; i < toI32(values.length); i++) {
3088
3183
  values[i] = temporalNegField(values[i])
3089
3184
  }
@@ -3219,8 +3314,9 @@ export class Instant {
3219
3314
  }
3220
3315
  this.epochSeconds = seconds
3221
3316
  this.nanosecondOfSecond = toI32(nanos)
3222
- // In range, `seconds` is within ±8.64 × 10^12, so the wrapping sum is exact.
3223
- this.epochMilliseconds = toF64(toI64(toU64(seconds) * 1000 + toU64(nanos / 1000000)))
3317
+ // In range, `seconds` is within ±8.64 × 10^12, so the milliseconds are an
3318
+ // integer below 2^53 in magnitude, which every `f64` step here holds exactly.
3319
+ this.epochMilliseconds = toF64(seconds) * 1000 + toF64(nanos / 1000000)
3224
3320
  }
3225
3321
 
3226
3322
  /**
@@ -3235,8 +3331,8 @@ export class Instant {
3235
3331
  const civil: i64 = temporalCivilOfEpochDay(day)
3236
3332
  const out: u8[] = new Array<u8>(TEMPORAL_TEXT_MAX)
3237
3333
  const year: i32 = temporalCivilYear(civil)
3238
- // `secondOfDay` is below a day, so the `u64` sum never wraps.
3239
- const nanoOfDay: i64 = toI64(toU64(secondOfDay) * 1000000000 + toU64(this.nanosecondOfSecond))
3334
+ const nanoOfDay: i64 =
3335
+ toI64(temporalModSmall(this.epochSeconds, 86400)) * 1000000000 + toI64(this.nanosecondOfSecond)
3240
3336
  let i: i32 = 0
3241
3337
  if (year >= 0 && year <= 9999) {
3242
3338
  const secs: i32 = toI32(secondOfDay)
@@ -3349,7 +3445,7 @@ export const instantFromEpochMilliseconds = (ms: f64): Instant => {
3349
3445
  temporalRangeError("Instant: epoch milliseconds must be a whole number within ±8.64e15")
3350
3446
  }
3351
3447
  const whole: i64 = toI64(ms)
3352
- return new Instant(temporalFloorDiv(whole, 1000), toI64(toU64(temporalFloorMod(whole, 1000)) * 1000000))
3448
+ return new Instant(temporalFloorDiv(whole, 1000), toI64(temporalModSmall(whole, 1000)) * 1000000)
3353
3449
  }
3354
3450
 
3355
3451
  /**
@@ -3398,8 +3494,8 @@ export const instantFromOrNull = (text: string): Instant | null => {
3398
3494
  ) {
3399
3495
  if (minutes >= 0 && minutes <= 59) {
3400
3496
  timeEnd = length - 6
3401
- const offset: u64 = toU64(hours) * 3600 + toU64(minutes) * 60
3402
- offsetSeconds = sign === 43 ? toI64(offset) : toI64(0 - offset)
3497
+ const offset: i64 = toI64(hours) * 3600 + toI64(minutes) * 60
3498
+ offsetSeconds = sign === 43 ? offset : 0 - offset
3403
3499
  }
3404
3500
  }
3405
3501
  }
@@ -3413,9 +3509,9 @@ export const instantFromOrNull = (text: string): Instant | null => {
3413
3509
  temporalCivilDay(civil)
3414
3510
  )
3415
3511
  // A four-digit year keeps `day` within ±4 × 10^6 and the offset is below a
3416
- // day, so the wrapping sum is the exact one.
3512
+ // day, so the checked sums never raise.
3417
3513
  return new Instant(
3418
- toI64(toU64(day) * 86400 + toU64(nanoOfDay / TEMPORAL_NS_PER_SECOND) - toU64(offsetSeconds)),
3514
+ temporalSub(temporalAdd(temporalMul(day, 86400), nanoOfDay / TEMPORAL_NS_PER_SECOND), offsetSeconds),
3419
3515
  nanoOfDay % TEMPORAL_NS_PER_SECOND
3420
3516
  )
3421
3517
  }
@@ -3426,10 +3522,13 @@ export const instantFromOrNull = (text: string): Instant | null => {
3426
3522
  }
3427
3523
  const day: i64 = temporalEpochDay(r.year, r.month, r.day)
3428
3524
  // The years in range keep `day` within ±4 × 10^8 and the offset is
3429
- // below a day, so these wrapping sums are the exact ones.
3430
- const seconds: u64 = toU64(day) * 86400 + toU64(r.hour) * 3600 + toU64(r.minute) * 60 + toU64(r.second)
3431
- const nanos: i64 = toI64(toU64(r.fraction) - toU64(r.offsetNanos))
3432
- const exactSeconds: i64 = toI64(seconds + toU64(temporalFloorDiv(nanos, TEMPORAL_NS_PER_SECOND)))
3525
+ // below a day, so these checked sums never raise.
3526
+ const seconds: i64 = temporalAdd(
3527
+ temporalMul(day, 86400),
3528
+ toI64(r.hour) * 3600 + toI64(r.minute) * 60 + toI64(r.second)
3529
+ )
3530
+ const nanos: i64 = temporalSub(toI64(r.fraction), r.offsetNanos)
3531
+ const exactSeconds: i64 = temporalAdd(seconds, temporalFloorDiv(nanos, TEMPORAL_NS_PER_SECOND))
3433
3532
  const exactNanos: i64 = temporalFloorMod(nanos, TEMPORAL_NS_PER_SECOND)
3434
3533
  if (!temporalInstantInRange(exactSeconds, exactNanos)) {
3435
3534
  return null