@amritk/nish-x86_64-linux 0.49.0 → 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 (92) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +233 -159
  4. package/package.json +1 -1
  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 +29 -12
  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 +33 -15
  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/threads.ts +52 -8
  89. package/std/yaml-json.ts +238 -106
  90. package/std/yaml-lines.ts +47 -27
  91. package/std/yaml-write.ts +58 -24
  92. package/std/yaml.ts +808 -395
package/std/term-io.ts CHANGED
@@ -746,7 +746,7 @@ export class Terminal {
746
746
  let ended: boolean = false
747
747
  let decoded: boolean = false
748
748
  if (waitStdin(waitMs)) {
749
- const n: i32 = readStdin(this.readBuf, 0, TERM_IO_READ)
749
+ const n: i32 = readStdin(this.readBuf, 0, toI32(this.readBuf.length))
750
750
  const nowMs: f64 = termIoNowMs()
751
751
  const only: i32 = this.decoder.waiting() ? -1 : termIoOnlyAcks(this.readBuf, n)
752
752
  if (only > 0) {
@@ -853,6 +853,8 @@ export class Terminal {
853
853
  this.backend = backend
854
854
  }
855
855
  let withPixels: boolean = false
856
+ // A frame past what one buffer holds is not sent: the last one stays, and so does `pixelsShown`.
857
+ let skipped: boolean = false
856
858
  const fb: Framebuffer = this.framebuffer
857
859
  if (fb.width > 0) {
858
860
  this.fit(backend)
@@ -864,11 +866,13 @@ export class Terminal {
864
866
  if (backend === GRAPHICS_KITTY) {
865
867
  this.pixels.reset()
866
868
  kittyFrame(src, this.rect, TERM_IO_IMAGE_ID, this.pixels)
867
- withPixels = true
869
+ skipped = this.pixels.full
870
+ withPixels = !skipped
868
871
  } else if (backend === GRAPHICS_SIXEL) {
869
872
  this.pixels.reset()
870
873
  sixelFrame(src, this.rect, true, this.pixels)
871
- withPixels = true
874
+ skipped = this.pixels.full
875
+ withPixels = !skipped
872
876
  } else if (backend === GRAPHICS_HALFBLOCK) {
873
877
  drawHalfBlocks(src, this.canvas, this.rect)
874
878
  } else {
@@ -881,7 +885,9 @@ export class Terminal {
881
885
  }
882
886
  this.screen.invalidate()
883
887
  }
884
- this.pixelsShown = withPixels
888
+ if (!skipped) {
889
+ this.pixelsShown = withPixels
890
+ }
885
891
  if (this.options.overlay >= 0) {
886
892
  drawStatsOverlay(this.screen, this.stats(), this.options.overlay)
887
893
  }
@@ -943,7 +949,7 @@ export const openTerminal = (options: TerminalOptions): Result<Terminal, string>
943
949
  }
944
950
  const left: i32 = toI32(Math.ceil(probe.sentAtMs + probe.timeoutMs - nowMs))
945
951
  if (waitStdin(left > 0 ? left : 0)) {
946
- const n: i32 = readStdin(buf, 0, TERM_IO_READ)
952
+ const n: i32 = readStdin(buf, 0, toI32(buf.length))
947
953
  if (n > 0) {
948
954
  probe.feed(buf, n, termIoNowMs())
949
955
  } else if (n !== -11) {
@@ -1116,7 +1122,7 @@ export const runTerminal = <G>(
1116
1122
  frame.framebuffer = term.framebuffer
1117
1123
  render(state, frame)
1118
1124
  }
1119
- term.workNs = term.workNs + toF64(monotonicNanos() - work)
1125
+ term.workNs = term.workNs + (toF64(monotonicNanos()) - toF64(work))
1120
1126
  if (draw && !quit) {
1121
1127
  term.present()
1122
1128
  }
@@ -9,20 +9,22 @@
9
9
  * natively and under Node, because the kitty and Sixel output is ASCII and the
10
10
  * cell drawers go through `Screen`, which already is.
11
11
  *
12
- * import { Framebuffer, fitFramebuffer, GRAPHICS_KITTY, kittyFrame, PixelBytes } from "nish/term-pixels"
12
+ * import { fitFramebuffer, GRAPHICS_KITTY, kittyFrame, PixelBytes, tryFramebuffer } from "nish/term-pixels"
13
13
  *
14
- * const fb = new Framebuffer(160, 90)
14
+ * const fb = tryFramebuffer(160, 90).expect("a 160 × 90 framebuffer")
15
15
  * fb.fill(0x000020)
16
16
  * fb.set(80, 45, 0xffcc00)
17
- * const rect = fitFramebuffer(fb.width, fb.height, 80, 24, 0, 0, GRAPHICS_KITTY)
17
+ * const rect = fitFramebuffer(fb.pixelWidth(), fb.pixelHeight(), 80, 24, 0, 0, GRAPHICS_KITTY)
18
18
  * const out = new PixelBytes(1 << 16)
19
19
  * kittyFrame(fb, rect, 1, out)
20
20
  * // out.text() is the frame's bytes, for one write
21
21
  *
22
22
  * - `Framebuffer(width, height)` holds the pixels, with `set`, `get`, `fill`,
23
- * `clear` and `resize`. `scaleFramebuffer(src, divisor, dst)` shrinks one by
24
- * an integer divisor, nearest-neighbour, for the refresh controller's
25
- * resolution downgrade.
23
+ * `clear` and `resize`, and `pixelWidth`, `pixelHeight` and `pixelData` to
24
+ * read its size and array. `tryFramebuffer(width, height)` and `tryResize`
25
+ * answer an `Err` for a size the constructor and `resize` panic on.
26
+ * `scaleFramebuffer(src, divisor, dst)` shrinks one by an integer divisor,
27
+ * nearest-neighbour, for the refresh controller's resolution downgrade.
26
28
  * - `GRAPHICS_ASCII`, `GRAPHICS_HALFBLOCK`, `GRAPHICS_SIXEL` and
27
29
  * `GRAPHICS_KITTY` name the backends, numbered from worst to best so that a
28
30
  * capability and a cap combine with `Math.min`.
@@ -52,7 +54,7 @@
52
54
  * Private helpers share the importing program's flat symbol namespace
53
55
  * (`docs/wp26-stdlib.md` §3e), which is why each one carries the module's name.
54
56
  */
55
- import { base64EncodeInto } from "nish/base64"
57
+ import { base64EncodeIfFits } from "nish/base64"
56
58
  import { COLOR_DEFAULT, ATTR_NONE, Canvas, Screen } from "nish/term"
57
59
 
58
60
  /** Text cells, one luminance character each: the Linux console's backend. */
@@ -163,6 +165,17 @@ const pixelsArea = (who: string, width: i32, height: i32): i32 => {
163
165
  return toI32(area)
164
166
  }
165
167
 
168
+ /** Why `width × height`, asked of `who`, is not a size an array of pixels can have, or "" when it is. */
169
+ const pixelsSizeProblem = (who: string, width: i32, height: i32): string => {
170
+ if (width < 0 || height < 0) {
171
+ return `${who}: a negative size, ${width} × ${height}`
172
+ }
173
+ if (toI64(width) * toI64(height) > toI64(2147483647)) {
174
+ return `${who}: ${width} × ${height} is more than 2^31 - 1 pixels`
175
+ }
176
+ return ""
177
+ }
178
+
166
179
  /** `y * width + x` as an index below `length`, or -1 when it is not one. */
167
180
  const pixelsIndex = (x: i32, y: i32, width: i32, length: i32): i32 => {
168
181
  const wide: i64 = toI64(y) * toI64(width) + toI64(x)
@@ -208,6 +221,66 @@ export class Framebuffer {
208
221
  this.pixels.fill(0)
209
222
  }
210
223
 
224
+ /**
225
+ * Pixels across, as the `width` field holds it.
226
+ *
227
+ * @returns The width, 0 or more.
228
+ */
229
+ pixelWidth(): i32 {
230
+ return this.width
231
+ }
232
+
233
+ /**
234
+ * Pixels down, as the `height` field holds it.
235
+ *
236
+ * @returns The height, 0 or more.
237
+ */
238
+ pixelHeight(): i32 {
239
+ return this.height
240
+ }
241
+
242
+ /**
243
+ * The pixels, one `0xRRGGBB` colour each at `y * pixelWidth() + x`, for `rasterFrame` to draw
244
+ * into, as the `pixels` field holds them.
245
+ *
246
+ * @returns The framebuffer's own array, not a copy.
247
+ */
248
+ pixelData(): i32[] {
249
+ return this.pixels
250
+ }
251
+
252
+ /**
253
+ * `resize` for a caller with an answer to a size it cannot have: make the framebuffer
254
+ * `width × height` and black, replacing the array only when the size changes, or leave it as
255
+ * it was and answer `Err`.
256
+ *
257
+ * @param width The new pixels across, 0 or more.
258
+ * @param height The new pixels down, 0 or more.
259
+ * @returns `Ok`, or `Err` naming the problem for a negative size or more than 2^31 - 1 pixels.
260
+ */
261
+ tryResize(width: i32, height: i32): Result<void, string> {
262
+ const area: i64 = toI64(width) * toI64(height)
263
+ // `area < 0` follows from a negative side; it is written out because it is
264
+ // what proves `length` below in range.
265
+ if (width < 0 || height < 0 || area < toI64(0) || area > toI64(2147483647)) {
266
+ return Err(pixelsSizeProblem("Framebuffer.tryResize", width, height))
267
+ }
268
+ const count: i32 = toI32(area)
269
+ // Never taken: the check above keeps `area` in range. It is written out
270
+ // because it is what proves `length` below.
271
+ if (count < 0) {
272
+ return Err(pixelsSizeProblem("Framebuffer.tryResize", width, height))
273
+ }
274
+ if (width !== this.width || height !== this.height) {
275
+ const length: integer<0, 2147483647> = count
276
+ this.width = width
277
+ this.height = height
278
+ this.pixels = new Array<i32>(length)
279
+ }
280
+ this.pixels.fill(0)
281
+ return Ok()
282
+ }
283
+
211
284
  /**
212
285
  * Set the pixel at `x`, `y` to `colour`'s low 24 bits; a pixel off the framebuffer is not drawn.
213
286
  * Panics when `pixels` does not hold `width × height`, as after setting those public fields
@@ -270,6 +343,26 @@ export class Framebuffer {
270
343
  }
271
344
  }
272
345
 
346
+ /**
347
+ * The `Framebuffer` constructor for a caller with an answer to a size it cannot have: a black
348
+ * framebuffer of `width × height`, or `Err`. It builds an empty framebuffer and sizes it with
349
+ * `tryResize`, so a call still reaches the size checks of the constructor, which panic only on a
350
+ * size an empty one never has; `--deny-panics` reports them at the call.
351
+ *
352
+ * @param width Pixels across, 0 or more.
353
+ * @param height Pixels down, 0 or more.
354
+ * @returns The framebuffer, or `Err` naming the problem for a negative size or more than
355
+ * 2^31 - 1 pixels.
356
+ */
357
+ export const tryFramebuffer = (width: i32, height: i32): Result<Framebuffer, string> => {
358
+ const fb: Framebuffer = new Framebuffer(0, 0)
359
+ const sized: Result<void, string> = fb.tryResize(width, height)
360
+ if (sized.isErr()) {
361
+ return Err(pixelsSizeProblem("tryFramebuffer", width, height))
362
+ }
363
+ return Ok(fb)
364
+ }
365
+
273
366
  /**
274
367
  * Shrink `src` by `divisor` into `dst`, which is resized to
275
368
  * `ceil(width / divisor) × ceil(height / divisor)`: each pixel of `dst` is the
@@ -449,6 +542,13 @@ export class PixelBytes {
449
542
  bytes: u8[]
450
543
  /** How many bytes are written. */
451
544
  length: i32
545
+ /**
546
+ * True once a frame asked for more than one buffer holds: a `put` past
547
+ * 2^31 - 1 bytes, which was dropped, or a `sixelFrame` rect whose working
548
+ * arrays would pass 2^31 - 1 entries. `text()` is then not the whole escape
549
+ * and is not to be written. `reset()` clears it.
550
+ */
551
+ full: boolean
452
552
  /**
453
553
  * One kitty chunk's RGB bytes.
454
554
  * @internal Used by `kittyFrame`.
@@ -490,9 +590,10 @@ export class PixelBytes {
490
590
  * @param capacity The bytes to make room for at the start; a frame's worth saves growing.
491
591
  */
492
592
  constructor(capacity: i32) {
493
- this.bytes = new Array<u8>(Math.max(capacity, 16))
593
+ this.bytes = capacity > 16 ? new Array<u8>(capacity) : new Array<u8>(16)
494
594
  this.bytes.fill(0)
495
595
  this.length = 0
596
+ this.full = false
496
597
  this.kittyRaw = new Array<u8>(PIXELS_KITTY_RAW)
497
598
  this.kittyRaw.fill(0)
498
599
  this.sampleXs = []
@@ -510,29 +611,38 @@ export class PixelBytes {
510
611
  /** Forget what was written. */
511
612
  reset(): void {
512
613
  this.length = 0
614
+ this.full = false
513
615
  }
514
616
 
515
617
  /**
516
618
  * Make room for `n` more bytes, doubling the array as often as that takes.
619
+ * Room past 2^31 - 1 bytes is not made: the buffer is marked `full` instead.
517
620
  *
518
621
  * @param n How many bytes the caller is about to `put`.
519
622
  */
520
623
  reserve(n: i32): void {
624
+ const had: u8[] = this.bytes
625
+ const have: i32 = toI32(had.length)
521
626
  // Summed and doubled in f64, which cannot overflow.
522
627
  const need: f64 = toF64(this.length) + toF64(n)
523
- if (need <= toF64(this.bytes.length)) {
628
+ if (need <= toF64(have)) {
524
629
  return
525
630
  }
526
- if (need > 2147483647.0) {
527
- panic("PixelBytes.reserve: more than 2^31 - 1 bytes")
528
- }
529
- let size: f64 = toF64(this.bytes.length)
631
+ let size: f64 = toF64(have)
530
632
  while (size < need) {
531
633
  size = size * 2.0
532
634
  }
533
- const grown: u8[] = new Array<u8>(toI32(Math.min(size, 2147483647.0)))
635
+ const room: i32 = need <= 2147483647.0 ? toI32(Math.min(size, 2147483647.0)) : -1
636
+ if (room < 0 || room < have) {
637
+ this.full = true
638
+ return
639
+ }
640
+ const length: integer<0, 2147483647> = room
641
+ const grown: u8[] = new Array<u8>(length)
534
642
  grown.fill(0)
535
- grown.set(this.bytes, 0)
643
+ for (let i: i32 = 0; i < toI32(had.length) && i < toI32(grown.length); i++) {
644
+ grown[i] = had[i]
645
+ }
536
646
  this.bytes = grown
537
647
  }
538
648
 
@@ -598,7 +708,11 @@ export class PixelBytes {
598
708
  * computed once per frame rather than once per pixel.
599
709
  */
600
710
  const pixelsSampleMap = (n: i32, source: i32): i32[] => {
601
- const map: i32[] = new Array<i32>(n)
711
+ if (n <= 0) {
712
+ return []
713
+ }
714
+ const length: integer<0, 2147483647> = n
715
+ const map: i32[] = new Array<i32>(length)
602
716
  pixelsSampleMapInto(map, n, source)
603
717
  return map
604
718
  }
@@ -621,7 +735,13 @@ const pixelsAtLeast = (a: i32[], n: i32): i32[] => {
621
735
  return a
622
736
  }
623
737
  const twice: f64 = Math.min(toF64(had) * 2.0, 2147483647.0)
624
- const grown: i32[] = new Array<i32>(twice > toF64(n) ? toI32(twice) : n)
738
+ const size: i32 = twice > toF64(n) ? toI32(twice) : n
739
+ // Never taken: `n` is past `had`, which is 0 or more; it proves the length.
740
+ if (size < 0) {
741
+ return a
742
+ }
743
+ const length: integer<0, 2147483647> = size
744
+ const grown: i32[] = new Array<i32>(length)
625
745
  grown.fill(0)
626
746
  return grown
627
747
  }
@@ -684,7 +804,8 @@ const pixelsStringEnd = (out: PixelBytes): void => {
684
804
  *
685
805
  * `imageId` is 1 to 2^31 - 1, since 0 is the protocol's "no id"; anything
686
806
  * else panics. So does a framebuffer whose `pixels` does not hold
687
- * `width × height`. An empty framebuffer or rect writes nothing.
807
+ * `width × height`. An empty framebuffer or rect writes nothing. A frame that
808
+ * would grow `out` past 2^31 - 1 bytes stops where it is and sets `out.full`.
688
809
  *
689
810
  * @param fb The frame's pixels, at its own size.
690
811
  * @param rect Where to draw it, from `fitFramebuffer` with `GRAPHICS_KITTY`.
@@ -737,7 +858,13 @@ export const kittyFrame = (fb: Framebuffer, rect: PixelRect, imageId: i32, out:
737
858
  out.putText("q=2,")
738
859
  out.putText(pixel < count ? "m=1;" : "m=0;")
739
860
  out.reserve(PIXELS_KITTY_CHUNK)
740
- out.length = base64EncodeInto(raw, 0, n, out.bytes, out.length)
861
+ // -1 only when `reserve` could not grow the buffer, which has set `full`.
862
+ const end: i32 = base64EncodeIfFits(raw, 0, n, out.bytes, out.length)
863
+ if (end < 0) {
864
+ out.full = true
865
+ return
866
+ }
867
+ out.length = end
741
868
  pixelsStringEnd(out)
742
869
  }
743
870
  }
@@ -836,7 +963,8 @@ const pixelsSixelRun = (out: PixelBytes, bits: i32, count: i32): void => {
836
963
  * With `dither`, each pixel's components are offset by the 4×4 Bayer
837
964
  * matrix's threshold before they are quantised (ordered dithering), so a
838
965
  * gradient becomes a fine, stable pattern rather than steps. An empty rect
839
- * writes nothing.
966
+ * writes nothing; a rect wider than 2^31 - 1 / 252 pixels, or of more than
967
+ * 2^31 - 1, is more than one buffer holds and marks `out` `full` instead.
840
968
  *
841
969
  * @param fb The frame's pixels; shrunk or grown to the rect by nearest neighbour.
842
970
  * @param rect Where to draw it, from `fitFramebuffer` with `GRAPHICS_SIXEL`.
@@ -849,8 +977,16 @@ export const sixelFrame = (fb: Framebuffer, rect: PixelRect, dither: boolean, ou
849
977
  if (w <= 0 || h <= 0 || fb.width <= 0 || fb.height <= 0) {
850
978
  return
851
979
  }
852
- const area: i32 = pixelsArea("sixelFrame", w, h)
853
- const bitsArea: i32 = pixelsArea("sixelFrame", w, SIXEL_REGISTERS)
980
+ // A rect whose working arrays would pass 2^31 - 1 entries is not a frame one
981
+ // buffer can hold, as one whose bytes would not be.
982
+ const wideArea: i64 = toI64(w) * toI64(h)
983
+ const wideBits: i64 = toI64(w) * toI64(SIXEL_REGISTERS)
984
+ const area: i32 = wideArea <= toI64(2147483647) ? toI32(wideArea) : -1
985
+ const bitsArea: i32 = wideBits <= toI64(2147483647) ? toI32(wideBits) : -1
986
+ if (area < 0 || bitsArea < 0) {
987
+ out.full = true
988
+ return
989
+ }
854
990
  // The working arrays live on `out`, grown once to the largest frame.
855
991
  out.sampleXs = pixelsAtLeast(out.sampleXs, w)
856
992
  out.sampleYs = pixelsAtLeast(out.sampleYs, h)
package/std/term-rate.ts CHANGED
@@ -607,7 +607,9 @@ export class RateController {
607
607
  return false
608
608
  }
609
609
  if (this.unacked() >= this.maxUnacked || this.stale > 0) {
610
- this.dropped = this.dropped + 1
610
+ // Stops at 2^31 - 1, about 103 days of refused frames at 240 a second.
611
+ const dropped: i32 = this.dropped
612
+ this.dropped = dropped < 2147483647 ? dropped + 1 : dropped
611
613
  return false
612
614
  }
613
615
  return true
@@ -1038,8 +1040,11 @@ export class RateController {
1038
1040
  while (head >= 0 && head < toI32(sent.length) && nowMs - sent[head] > wait) {
1039
1041
  head = head + 1
1040
1042
  this.head = head
1041
- this.lost = this.lost + 1
1042
- this.stale = this.stale + 1
1043
+ // Both stop at 2^31 - 1, as `dropped` does.
1044
+ const lost: i32 = this.lost
1045
+ this.lost = lost < 2147483647 ? lost + 1 : lost
1046
+ const stale: i32 = this.stale
1047
+ this.stale = stale < 2147483647 ? stale + 1 : stale
1043
1048
  this.staleSinceMs = nowMs
1044
1049
  }
1045
1050
  this.ageRate(nowMs)
package/std/term.ts CHANGED
@@ -716,16 +716,16 @@ const termStepOn = (i: i32, step: i32, n: i32): i32 => {
716
716
  * sends the same bytes back out.
717
717
  */
718
718
  const termBytesText = (bytes: u8[], n: i32): string => {
719
- // Every caller passes at most `bytes.length`: a whole buffer, or a screen's `outLen`, which `put` keeps within `out`.
719
+ // Every caller passes at most `bytes.length` (a whole buffer, or a screen's `outLen`, which `put` keeps within `out`);
720
+ // the clamp holds it anyway, and the slice restates it where the prover reads it.
720
721
  const held: i32 = toI32(bytes.length)
721
722
  let end: i32 = n
722
723
  if (end > held) {
723
724
  end = held
724
- }
725
- if (end < 0) {
725
+ } else if (end < 0) {
726
726
  end = 0
727
727
  }
728
- if (termNativeStrings()) {
728
+ if (termNativeStrings() && end >= 0 && end <= toI32(bytes.length)) {
729
729
  return String.fromBytes(bytes, 0, end)
730
730
  }
731
731
  const pieces: string[] = []
package/std/text.ts CHANGED
@@ -295,7 +295,7 @@ export const split = (text: string, sep: string): string[] => {
295
295
  const length: i32 = toI32(text.length)
296
296
  const parts: string[] = []
297
297
  let start: i32 = 0
298
- while (true) {
298
+ while (start >= 0) {
299
299
  // Searching from `start` rather than cutting `text` down after each match
300
300
  // copies nothing until the piece is taken.
301
301
  const at: i32 = toI32(text.indexOf(sep, start))
@@ -312,10 +312,10 @@ export const split = (text: string, sep: string): string[] => {
312
312
  if (start >= 0 && start < length && at >= 0 && at < length) {
313
313
  parts.push(text.substring(start, at))
314
314
  }
315
- // A hit is a whole separator that ends by `length`, so the sum is at
316
- // most `length`: it neither wraps a `u32` nor leaves an `i32`.
317
- start = toI32(toU32(at) + toU32(sepLength))
315
+ // A hit is a whole separator ending by `length`: never the `-1` that ends the loop.
316
+ start = textSpanEnd(length, at, sepLength)
318
317
  }
318
+ return parts
319
319
  }
320
320
 
321
321
  /**
@@ -352,7 +352,7 @@ export const splitLimit = (text: string, sep: string, limit: i32): string[] => {
352
352
  const length: i32 = toI32(text.length)
353
353
  const parts: string[] = []
354
354
  let start: i32 = 0
355
- while (true) {
355
+ while (start >= 0) {
356
356
  // Searching from `start` rather than cutting `text` down after each match
357
357
  // copies nothing until the piece is taken.
358
358
  const at: i32 = toI32(text.indexOf(sep, start))
@@ -372,10 +372,10 @@ export const splitLimit = (text: string, sep: string, limit: i32): string[] => {
372
372
  if (toI32(parts.length) === limit) {
373
373
  return parts
374
374
  }
375
- // A hit is a whole separator that ends by `length`, so the sum is at
376
- // most `length`: it neither wraps a `u32` nor leaves an `i32`.
377
- start = toI32(toU32(at) + toU32(sepLength))
375
+ // A hit is a whole separator ending by `length`: never the `-1` that ends the loop.
376
+ start = textSpanEnd(length, at, sepLength)
378
377
  }
378
+ return parts
379
379
  }
380
380
 
381
381
  /** The most digits `toFixed` takes after the point; JavaScript throws a `RangeError` past it. */
@@ -566,6 +566,24 @@ export const toFixed = (x: f64, digits: i32): string => {
566
566
  return String.fromBytes(bytes, 0, toI32(bytes.length))
567
567
  }
568
568
 
569
+ /**
570
+ * Where the `count` bytes from `at` end, `at + count`, when they end by
571
+ * `length`; `-1` when any of the three is negative or they run past it. Written as
572
+ * differences, each of which `src/bounds.ts` proves, so the end needs no
573
+ * overflow check and no cast through `u32`.
574
+ */
575
+ const textSpanEnd = (length: i32, at: i32, count: i32): i32 => {
576
+ if (length < 0 || at < 0 || count < 0) {
577
+ return -1
578
+ }
579
+ const room: i32 = length - at
580
+ if (room < 0) {
581
+ return -1
582
+ }
583
+ const gap: i32 = room - count
584
+ return gap < 0 ? -1 : length - gap
585
+ }
586
+
569
587
  /**
570
588
  * `text` with every occurrence of `needle` replaced by `replacement`.
571
589
  *
@@ -600,9 +618,13 @@ export const replaceAll = (text: string, needle: string, replacement: string): s
600
618
  return parts.join(replacement)
601
619
  }
602
620
  parts.push(rest.substring(0, at))
603
- // A hit is a whole `needle` inside `rest`, so the sum is at most
604
- // `rest.length`: it neither wraps a `u32` nor leaves an `i32`.
605
- rest = rest.substring(toI32(toU32(at) + toU32(needleLength)), toI32(rest.length))
621
+ // A hit is a whole `needle` inside `rest`, so its end is never `-1`; the
622
+ // test is what keeps the cut inside `rest`.
623
+ const after: i32 = textSpanEnd(toI32(rest.length), at, needleLength)
624
+ if (after < 0 || after > toI32(rest.length)) {
625
+ return parts.join(replacement)
626
+ }
627
+ rest = rest.substring(after, toI32(rest.length))
606
628
  }
607
629
  }
608
630
 
package/std/threads.ts CHANGED
@@ -783,11 +783,21 @@ export class AtomicI64 {
783
783
  }
784
784
 
785
785
  /**
786
- * The panic of an atomic array made with a negative length: the message
787
- * `new Array` of one prints, so the two refuse it alike.
786
+ * Why an atomic array cannot be made `length` long, or "" when it can: a
787
+ * negative length is refused with the message `new Array` of one prints, so
788
+ * the two refuse it alike.
789
+ *
790
+ * @param length the length an atomic array would be made with.
791
+ * @returns the reason the length is refused, or "" when it is not.
788
792
  */
789
- const negativeAtomicLength = (): void => {
790
- panic("array length out of range")
793
+ export const atomicArrayLengthFault = (length: i32): string => (length < 0 ? "array length out of range" : "")
794
+
795
+ /** Panics unless an atomic array can be made `length` long: a negative length is the program's mistake. */
796
+ const atomicArrayCheckLength = (length: i32): void => {
797
+ const fault: string = atomicArrayLengthFault(length)
798
+ if (fault.length > 0) {
799
+ panic(fault)
800
+ }
791
801
  }
792
802
 
793
803
  /**
@@ -821,12 +831,12 @@ export class AtomicI32Array {
821
831
  * `new Array` of one does.
822
832
  */
823
833
  constructor(length: i32) {
834
+ atomicArrayCheckLength(length)
824
835
  this.length = length
825
836
  if (length >= 0) {
826
837
  this.values = new Array<i32>(length)
827
838
  } else {
828
- negativeAtomicLength()
829
- // Never reached: `negativeAtomicLength` stops the program.
839
+ // Never reached: `atomicArrayCheckLength` stops the program.
830
840
  this.values = []
831
841
  }
832
842
  this.values.fill(0)
@@ -977,12 +987,12 @@ export class AtomicI64Array {
977
987
  * `new Array` of one does.
978
988
  */
979
989
  constructor(length: i32) {
990
+ atomicArrayCheckLength(length)
980
991
  this.length = length
981
992
  if (length >= 0) {
982
993
  this.values = new Array<i64>(length)
983
994
  } else {
984
- negativeAtomicLength()
985
- // Never reached: `negativeAtomicLength` stops the program.
995
+ // Never reached: `atomicArrayCheckLength` stops the program.
986
996
  this.values = []
987
997
  }
988
998
  this.values.fill(0)
@@ -1100,6 +1110,40 @@ export class AtomicI64Array {
1100
1110
  }
1101
1111
  }
1102
1112
 
1113
+ /**
1114
+ * A new `AtomicI32Array` of `length` elements, each `0`, or why one cannot
1115
+ * be made: the twin of its constructor for a length read from input, which
1116
+ * refuses a negative length with the message the constructor would panic
1117
+ * with, rather than panicking.
1118
+ *
1119
+ * @param length how many elements.
1120
+ * @returns the array, or the reason the length is refused.
1121
+ */
1122
+ export const atomicI32ArrayChecked = (length: i32): Result<AtomicI32Array, string> => {
1123
+ const fault: string = atomicArrayLengthFault(length)
1124
+ if (fault.length > 0) {
1125
+ return Err(fault)
1126
+ }
1127
+ return Ok(new AtomicI32Array(length))
1128
+ }
1129
+
1130
+ /**
1131
+ * A new `AtomicI64Array` of `length` elements, each `0`, or why one cannot
1132
+ * be made: the twin of its constructor for a length read from input, which
1133
+ * refuses a negative length with the message the constructor would panic
1134
+ * with, rather than panicking.
1135
+ *
1136
+ * @param length how many elements.
1137
+ * @returns the array, or the reason the length is refused.
1138
+ */
1139
+ export const atomicI64ArrayChecked = (length: i32): Result<AtomicI64Array, string> => {
1140
+ const fault: string = atomicArrayLengthFault(length)
1141
+ if (fault.length > 0) {
1142
+ return Err(fault)
1143
+ }
1144
+ return Ok(new AtomicI64Array(length))
1145
+ }
1146
+
1103
1147
  /**
1104
1148
  * A channel of scalars or strings between a scope's tasks (docs/wp29-thread-surface.md
1105
1149
  * §4.4); a string is copied out of the sender's arena at `send` and into the