@amritk/nish-x86_64-linux 0.49.1 → 0.51.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 (107) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +237 -160
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +94 -11
  7. package/runtime/nish.h +5 -0
  8. package/runtime/runtime-host.c +21 -5
  9. package/std/README.md +14 -11
  10. package/std/base64.ts +74 -35
  11. package/std/bytes.ts +207 -97
  12. package/std/cli-args.ts +150 -107
  13. package/std/compress/checksum.ts +68 -10
  14. package/std/compress/deflate.ts +213 -115
  15. package/std/compress/gzip.ts +124 -49
  16. package/std/compress/zlib.ts +57 -15
  17. package/std/crypto/aes.ts +51 -9
  18. package/std/crypto/base64url.ts +93 -66
  19. package/std/crypto/bcrypt.ts +25 -21
  20. package/std/crypto/ed25519.ts +12 -10
  21. package/std/crypto/hkdf.ts +405 -283
  22. package/std/crypto/hmac.ts +484 -149
  23. package/std/crypto/jwt.ts +51 -40
  24. package/std/crypto/p256.ts +14 -19
  25. package/std/crypto/pki-roots.ts +35 -12
  26. package/std/crypto/pki.ts +43 -55
  27. package/std/crypto/rsa-sign.ts +1635 -0
  28. package/std/crypto/rsa.ts +26 -4
  29. package/std/crypto/sha256.ts +212 -90
  30. package/std/crypto/sha512.ts +135 -49
  31. package/std/crypto/x25519.ts +12 -11
  32. package/std/crypto/x509.ts +164 -40
  33. package/std/gpu/sys.ts +490 -0
  34. package/std/json-equal.ts +33 -18
  35. package/std/json-format.ts +39 -8
  36. package/std/json-number.ts +15 -3
  37. package/std/json-tokenizer.ts +4 -3
  38. package/std/json-typed.ts +12 -6
  39. package/std/json-value.ts +236 -119
  40. package/std/json.ts +81 -46
  41. package/std/net/buffer-pool.ts +25 -14
  42. package/std/net/dns-config.ts +18 -5
  43. package/std/net/dns-message.ts +60 -27
  44. package/std/net/dns-resolver.ts +35 -16
  45. package/std/net/dns.ts +3 -2
  46. package/std/net/fetch.ts +224 -61
  47. package/std/net/handler.ts +189 -88
  48. package/std/net/hpack.ts +216 -103
  49. package/std/net/http-compress.ts +104 -42
  50. package/std/net/http-fields.ts +327 -31
  51. package/std/net/http-server.ts +326 -24
  52. package/std/net/http1-client-codec.ts +159 -55
  53. package/std/net/http1-codec.ts +427 -237
  54. package/std/net/http1-server.ts +220 -38
  55. package/std/net/http2-cleartext.ts +10 -4
  56. package/std/net/http2-frame.ts +2 -2
  57. package/std/net/http2-tls.ts +3 -3
  58. package/std/net/http2.ts +69 -15
  59. package/std/net/http3-frame.ts +11 -14
  60. package/std/net/http3-server.ts +139 -60
  61. package/std/net/http3.ts +140 -79
  62. package/std/net/multipart.ts +592 -0
  63. package/std/net/outbound.ts +24 -8
  64. package/std/net/postgres-async.ts +97 -64
  65. package/std/net/postgres-carrier.ts +28 -4
  66. package/std/net/postgres.ts +166 -65
  67. package/std/net/prefork.ts +86 -5
  68. package/std/net/protobuf.ts +6 -2
  69. package/std/net/qpack.ts +147 -40
  70. package/std/net/quic-conn-ack.ts +27 -7
  71. package/std/net/quic-conn-cid.ts +13 -7
  72. package/std/net/quic-datagram.ts +6 -5
  73. package/std/net/quic-frame.ts +67 -23
  74. package/std/net/quic-listener.ts +61 -39
  75. package/std/net/quic-packet.ts +91 -50
  76. package/std/net/quic-recovery.ts +11 -9
  77. package/std/net/quic-stream.ts +36 -27
  78. package/std/net/quic.ts +84 -54
  79. package/std/net/router.ts +136 -24
  80. package/std/net/serve.ts +59 -5
  81. package/std/net/static.ts +86 -36
  82. package/std/net/tls/client.ts +81 -34
  83. package/std/net/tls/codec.ts +3 -4
  84. package/std/net/tls/record.ts +80 -35
  85. package/std/net/tls/schedule.ts +339 -149
  86. package/std/net/tls/ticket.ts +6 -5
  87. package/std/net/tls-tcp.ts +69 -30
  88. package/std/net/tls.ts +194 -50
  89. package/std/net/upstream.ts +83 -38
  90. package/std/net/url.ts +170 -94
  91. package/std/net/websocket.ts +19 -20
  92. package/std/net/webtransport.ts +123 -33
  93. package/std/raster.ts +12 -2
  94. package/std/regex.ts +600 -388
  95. package/std/sqlite.ts +37 -19
  96. package/std/sync/ring.ts +232 -115
  97. package/std/sync/shm.ts +11 -4
  98. package/std/temporal.ts +232 -133
  99. package/std/term-io.ts +12 -6
  100. package/std/term-pixels.ts +176 -24
  101. package/std/term-rate.ts +8 -3
  102. package/std/term.ts +4 -4
  103. package/std/text.ts +38 -16
  104. package/std/yaml-json.ts +238 -106
  105. package/std/yaml-lines.ts +47 -27
  106. package/std/yaml-write.ts +58 -24
  107. 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)
@@ -188,7 +201,12 @@ export class Framebuffer {
188
201
  const area: i32 = pixelsArea("Framebuffer", width, height)
189
202
  this.width = width
190
203
  this.height = height
191
- this.pixels = new Array<i32>(area)
204
+ // `area` is never negative here; the test lets the prover see it.
205
+ let n: i32 = 0
206
+ if (area > 0) {
207
+ n = area
208
+ }
209
+ this.pixels = new Array<i32>(n)
192
210
  this.pixels.fill(0)
193
211
  }
194
212
 
@@ -203,11 +221,76 @@ export class Framebuffer {
203
221
  if (width !== this.width || height !== this.height) {
204
222
  this.width = width
205
223
  this.height = height
206
- this.pixels = new Array<i32>(area)
224
+ // `area` is never negative here; the test lets the prover see it.
225
+ let n: i32 = 0
226
+ if (area > 0) {
227
+ n = area
228
+ }
229
+ this.pixels = new Array<i32>(n)
207
230
  }
208
231
  this.pixels.fill(0)
209
232
  }
210
233
 
234
+ /**
235
+ * Pixels across, as the `width` field holds it.
236
+ *
237
+ * @returns The width, 0 or more.
238
+ */
239
+ pixelWidth(): i32 {
240
+ return this.width
241
+ }
242
+
243
+ /**
244
+ * Pixels down, as the `height` field holds it.
245
+ *
246
+ * @returns The height, 0 or more.
247
+ */
248
+ pixelHeight(): i32 {
249
+ return this.height
250
+ }
251
+
252
+ /**
253
+ * The pixels, one `0xRRGGBB` colour each at `y * pixelWidth() + x`, for `rasterFrame` to draw
254
+ * into, as the `pixels` field holds them.
255
+ *
256
+ * @returns The framebuffer's own array, not a copy.
257
+ */
258
+ pixelData(): i32[] {
259
+ return this.pixels
260
+ }
261
+
262
+ /**
263
+ * `resize` for a caller with an answer to a size it cannot have: make the framebuffer
264
+ * `width × height` and black, replacing the array only when the size changes, or leave it as
265
+ * it was and answer `Err`.
266
+ *
267
+ * @param width The new pixels across, 0 or more.
268
+ * @param height The new pixels down, 0 or more.
269
+ * @returns `Ok`, or `Err` naming the problem for a negative size or more than 2^31 - 1 pixels.
270
+ */
271
+ tryResize(width: i32, height: i32): Result<void, string> {
272
+ const area: i64 = toI64(width) * toI64(height)
273
+ // `area < 0` follows from a negative side; it is written out because it is
274
+ // what proves `length` below in range.
275
+ if (width < 0 || height < 0 || area < toI64(0) || area > toI64(2147483647)) {
276
+ return Err(pixelsSizeProblem("Framebuffer.tryResize", width, height))
277
+ }
278
+ const count: i32 = toI32(area)
279
+ // Never taken: the check above keeps `area` in range. It is written out
280
+ // because it is what proves `length` below.
281
+ if (count < 0) {
282
+ return Err(pixelsSizeProblem("Framebuffer.tryResize", width, height))
283
+ }
284
+ if (width !== this.width || height !== this.height) {
285
+ const length: integer<0, 2147483647> = count
286
+ this.width = width
287
+ this.height = height
288
+ this.pixels = new Array<i32>(length)
289
+ }
290
+ this.pixels.fill(0)
291
+ return Ok()
292
+ }
293
+
211
294
  /**
212
295
  * Set the pixel at `x`, `y` to `colour`'s low 24 bits; a pixel off the framebuffer is not drawn.
213
296
  * Panics when `pixels` does not hold `width × height`, as after setting those public fields
@@ -270,6 +353,26 @@ export class Framebuffer {
270
353
  }
271
354
  }
272
355
 
356
+ /**
357
+ * The `Framebuffer` constructor for a caller with an answer to a size it cannot have: a black
358
+ * framebuffer of `width × height`, or `Err`. It builds an empty framebuffer and sizes it with
359
+ * `tryResize`, so a call still reaches the size checks of the constructor, which panic only on a
360
+ * size an empty one never has; `--deny-panics` reports them at the call.
361
+ *
362
+ * @param width Pixels across, 0 or more.
363
+ * @param height Pixels down, 0 or more.
364
+ * @returns The framebuffer, or `Err` naming the problem for a negative size or more than
365
+ * 2^31 - 1 pixels.
366
+ */
367
+ export const tryFramebuffer = (width: i32, height: i32): Result<Framebuffer, string> => {
368
+ const fb: Framebuffer = new Framebuffer(0, 0)
369
+ const sized: Result<void, string> = fb.tryResize(width, height)
370
+ if (sized.isErr()) {
371
+ return Err(pixelsSizeProblem("tryFramebuffer", width, height))
372
+ }
373
+ return Ok(fb)
374
+ }
375
+
273
376
  /**
274
377
  * Shrink `src` by `divisor` into `dst`, which is resized to
275
378
  * `ceil(width / divisor) × ceil(height / divisor)`: each pixel of `dst` is the
@@ -288,6 +391,12 @@ export const scaleFramebuffer = (src: Framebuffer, divisor: i32, dst: Framebuffe
288
391
  }
289
392
  const w: i32 = pixelsCeilDiv(src.width, divisor)
290
393
  const h: i32 = pixelsCeilDiv(src.height, divisor)
394
+ // Neither side is negative or larger than `src`'s, so only a `src` whose
395
+ // fields were set past its pixels gets here; the test is the size check
396
+ // `resize` makes, stated at the call so the call itself is proven.
397
+ if (w < 0 || h < 0 || toI64(w) * toI64(h) > toI64(2147483647)) {
398
+ panic("scaleFramebuffer: more than 2^31 - 1 pixels")
399
+ }
291
400
  dst.resize(w, h)
292
401
  const from: i32[] = src.pixels
293
402
  const to: i32[] = dst.pixels
@@ -449,6 +558,13 @@ export class PixelBytes {
449
558
  bytes: u8[]
450
559
  /** How many bytes are written. */
451
560
  length: i32
561
+ /**
562
+ * True once a frame asked for more than one buffer holds: a `put` past
563
+ * 2^31 - 1 bytes, which was dropped, or a `sixelFrame` rect whose working
564
+ * arrays would pass 2^31 - 1 entries. `text()` is then not the whole escape
565
+ * and is not to be written. `reset()` clears it.
566
+ */
567
+ full: boolean
452
568
  /**
453
569
  * One kitty chunk's RGB bytes.
454
570
  * @internal Used by `kittyFrame`.
@@ -490,9 +606,10 @@ export class PixelBytes {
490
606
  * @param capacity The bytes to make room for at the start; a frame's worth saves growing.
491
607
  */
492
608
  constructor(capacity: i32) {
493
- this.bytes = new Array<u8>(Math.max(capacity, 16))
609
+ this.bytes = capacity > 16 ? new Array<u8>(capacity) : new Array<u8>(16)
494
610
  this.bytes.fill(0)
495
611
  this.length = 0
612
+ this.full = false
496
613
  this.kittyRaw = new Array<u8>(PIXELS_KITTY_RAW)
497
614
  this.kittyRaw.fill(0)
498
615
  this.sampleXs = []
@@ -510,29 +627,38 @@ export class PixelBytes {
510
627
  /** Forget what was written. */
511
628
  reset(): void {
512
629
  this.length = 0
630
+ this.full = false
513
631
  }
514
632
 
515
633
  /**
516
634
  * Make room for `n` more bytes, doubling the array as often as that takes.
635
+ * Room past 2^31 - 1 bytes is not made: the buffer is marked `full` instead.
517
636
  *
518
637
  * @param n How many bytes the caller is about to `put`.
519
638
  */
520
639
  reserve(n: i32): void {
640
+ const had: u8[] = this.bytes
641
+ const have: i32 = toI32(had.length)
521
642
  // Summed and doubled in f64, which cannot overflow.
522
643
  const need: f64 = toF64(this.length) + toF64(n)
523
- if (need <= toF64(this.bytes.length)) {
644
+ if (need <= toF64(have)) {
524
645
  return
525
646
  }
526
- if (need > 2147483647.0) {
527
- panic("PixelBytes.reserve: more than 2^31 - 1 bytes")
528
- }
529
- let size: f64 = toF64(this.bytes.length)
647
+ let size: f64 = toF64(have)
530
648
  while (size < need) {
531
649
  size = size * 2.0
532
650
  }
533
- const grown: u8[] = new Array<u8>(toI32(Math.min(size, 2147483647.0)))
651
+ const room: i32 = need <= 2147483647.0 ? toI32(Math.min(size, 2147483647.0)) : -1
652
+ if (room < 0 || room < have) {
653
+ this.full = true
654
+ return
655
+ }
656
+ const length: integer<0, 2147483647> = room
657
+ const grown: u8[] = new Array<u8>(length)
534
658
  grown.fill(0)
535
- grown.set(this.bytes, 0)
659
+ for (let i: i32 = 0; i < toI32(had.length) && i < toI32(grown.length); i++) {
660
+ grown[i] = had[i]
661
+ }
536
662
  this.bytes = grown
537
663
  }
538
664
 
@@ -598,7 +724,11 @@ export class PixelBytes {
598
724
  * computed once per frame rather than once per pixel.
599
725
  */
600
726
  const pixelsSampleMap = (n: i32, source: i32): i32[] => {
601
- const map: i32[] = new Array<i32>(n)
727
+ if (n <= 0) {
728
+ return []
729
+ }
730
+ const length: integer<0, 2147483647> = n
731
+ const map: i32[] = new Array<i32>(length)
602
732
  pixelsSampleMapInto(map, n, source)
603
733
  return map
604
734
  }
@@ -621,7 +751,13 @@ const pixelsAtLeast = (a: i32[], n: i32): i32[] => {
621
751
  return a
622
752
  }
623
753
  const twice: f64 = Math.min(toF64(had) * 2.0, 2147483647.0)
624
- const grown: i32[] = new Array<i32>(twice > toF64(n) ? toI32(twice) : n)
754
+ const size: i32 = twice > toF64(n) ? toI32(twice) : n
755
+ // Never taken: `n` is past `had`, which is 0 or more; it proves the length.
756
+ if (size < 0) {
757
+ return a
758
+ }
759
+ const length: integer<0, 2147483647> = size
760
+ const grown: i32[] = new Array<i32>(length)
625
761
  grown.fill(0)
626
762
  return grown
627
763
  }
@@ -684,7 +820,8 @@ const pixelsStringEnd = (out: PixelBytes): void => {
684
820
  *
685
821
  * `imageId` is 1 to 2^31 - 1, since 0 is the protocol's "no id"; anything
686
822
  * else panics. So does a framebuffer whose `pixels` does not hold
687
- * `width × height`. An empty framebuffer or rect writes nothing.
823
+ * `width × height`. An empty framebuffer or rect writes nothing. A frame that
824
+ * would grow `out` past 2^31 - 1 bytes stops where it is and sets `out.full`.
688
825
  *
689
826
  * @param fb The frame's pixels, at its own size.
690
827
  * @param rect Where to draw it, from `fitFramebuffer` with `GRAPHICS_KITTY`.
@@ -737,7 +874,13 @@ export const kittyFrame = (fb: Framebuffer, rect: PixelRect, imageId: i32, out:
737
874
  out.putText("q=2,")
738
875
  out.putText(pixel < count ? "m=1;" : "m=0;")
739
876
  out.reserve(PIXELS_KITTY_CHUNK)
740
- out.length = base64EncodeInto(raw, 0, n, out.bytes, out.length)
877
+ // -1 only when `reserve` could not grow the buffer, which has set `full`.
878
+ const end: i32 = base64EncodeIfFits(raw, 0, n, out.bytes, out.length)
879
+ if (end < 0) {
880
+ out.full = true
881
+ return
882
+ }
883
+ out.length = end
741
884
  pixelsStringEnd(out)
742
885
  }
743
886
  }
@@ -836,7 +979,8 @@ const pixelsSixelRun = (out: PixelBytes, bits: i32, count: i32): void => {
836
979
  * With `dither`, each pixel's components are offset by the 4×4 Bayer
837
980
  * matrix's threshold before they are quantised (ordered dithering), so a
838
981
  * gradient becomes a fine, stable pattern rather than steps. An empty rect
839
- * writes nothing.
982
+ * writes nothing; a rect wider than 2^31 - 1 / 252 pixels, or of more than
983
+ * 2^31 - 1, is more than one buffer holds and marks `out` `full` instead.
840
984
  *
841
985
  * @param fb The frame's pixels; shrunk or grown to the rect by nearest neighbour.
842
986
  * @param rect Where to draw it, from `fitFramebuffer` with `GRAPHICS_SIXEL`.
@@ -849,8 +993,16 @@ export const sixelFrame = (fb: Framebuffer, rect: PixelRect, dither: boolean, ou
849
993
  if (w <= 0 || h <= 0 || fb.width <= 0 || fb.height <= 0) {
850
994
  return
851
995
  }
852
- const area: i32 = pixelsArea("sixelFrame", w, h)
853
- const bitsArea: i32 = pixelsArea("sixelFrame", w, SIXEL_REGISTERS)
996
+ // A rect whose working arrays would pass 2^31 - 1 entries is not a frame one
997
+ // buffer can hold, as one whose bytes would not be.
998
+ const wideArea: i64 = toI64(w) * toI64(h)
999
+ const wideBits: i64 = toI64(w) * toI64(SIXEL_REGISTERS)
1000
+ const area: i32 = wideArea <= toI64(2147483647) ? toI32(wideArea) : -1
1001
+ const bitsArea: i32 = wideBits <= toI64(2147483647) ? toI32(wideBits) : -1
1002
+ if (area < 0 || bitsArea < 0) {
1003
+ out.full = true
1004
+ return
1005
+ }
854
1006
  // The working arrays live on `out`, grown once to the largest frame.
855
1007
  out.sampleXs = pixelsAtLeast(out.sampleXs, w)
856
1008
  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,15 +372,12 @@ 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
- /** The most digits `toFixed` takes after the point; JavaScript throws a `RangeError` past it. */
382
- const TO_FIXED_MAX_DIGITS: i32 = 100
383
-
384
381
  /** Where `toFixed` stops writing fixed notation and answers `String(x)`, as JavaScript does. */
385
382
  const TO_FIXED_LIMIT: f64 = 1e21
386
383
 
@@ -479,7 +476,10 @@ const fixedDigitsAdd = (digits: u8[], doubling: boolean, amount: i32): void => {
479
476
  * 1e21 in size.
480
477
  */
481
478
  export const toFixed = (x: f64, digits: i32): string => {
482
- if (digits < 0 || digits > TO_FIXED_MAX_DIGITS) {
479
+ // `digits * digits > 100 * 100` is `digits > 100` for a `digits` not below
480
+ // zero; the cap 10000 is the caller contract's product of 100 and 100
481
+ // (`src/caller-contract.ts`), so a call with a constant count proves it.
482
+ if (digits < 0 || toI64(digits) * toI64(digits) > toI64(10000)) {
483
483
  panic("toFixed: digits must be 0 to 100")
484
484
  }
485
485
  // biome-ignore lint/suspicious/noSelfCompare: NaN is the only value that is not itself; Nish has no Number.isNaN
@@ -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
 
@@ -883,7 +905,7 @@ const shiftTextAsciiRange = (text: string, low: i32, high: i32, toUpper: boolean
883
905
  bytes.push(toU8(code >= low && code <= high ? moved : code))
884
906
  i += 1
885
907
  }
886
- return String.fromBytes(bytes, 0, length)
908
+ return String.fromBytes(bytes, 0, toI32(bytes.length))
887
909
  }
888
910
  const parts: string[] = []
889
911
  let start: i32 = 0