nish 0.46.0 → 0.48.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 (45) hide show
  1. package/docs/AI.md +29 -18
  2. package/package.json +5 -5
  3. package/runtime/gfx/README.md +55 -0
  4. package/runtime/gfx/nish-gfx.c +1063 -0
  5. package/runtime/gfx/nish-gfx.h +221 -0
  6. package/runtime/gfx/nish-gfx.overrides.json +14 -0
  7. package/runtime/nish.d.ts +22 -5
  8. package/runtime/nish.h +1 -0
  9. package/runtime/nish.mjs +1 -0
  10. package/runtime/runtime-bytes.c +167 -7
  11. package/runtime/runtime-net.c +122 -52
  12. package/runtime/runtime-uring.c +150 -69
  13. package/runtime/sqlite/nish-sqlite.c +166 -39
  14. package/scripts/build.sh +88 -3
  15. package/std/README.md +2 -2
  16. package/std/base64.ts +28 -30
  17. package/std/compress/deflate.ts +1371 -704
  18. package/std/crypto/aes.ts +81 -45
  19. package/std/crypto/bcrypt-batch.ts +23 -4
  20. package/std/crypto/bcrypt.ts +36 -11
  21. package/std/crypto/chacha20poly1305.ts +93 -52
  22. package/std/crypto/x25519.ts +7 -4
  23. package/std/host/game.ts +52 -20
  24. package/std/host/ring.ts +115 -31
  25. package/std/host/text.ts +9 -3
  26. package/std/json.ts +83 -0
  27. package/std/math.ts +161 -131
  28. package/std/net/handler.ts +24 -6
  29. package/std/net/http-server.ts +358 -141
  30. package/std/net/http1-server.ts +44 -13
  31. package/std/net/prefork.ts +1 -2
  32. package/std/net/static.ts +7 -2
  33. package/std/random.ts +34 -51
  34. package/std/raster.ts +377 -113
  35. package/std/regex.ts +46 -15
  36. package/std/release.ts +19 -13
  37. package/std/sqlite.ts +380 -16
  38. package/std/synth.ts +20 -3
  39. package/std/temporal.ts +740 -363
  40. package/std/term-io.ts +81 -33
  41. package/std/term-pixels.ts +287 -112
  42. package/std/term-probe.ts +7 -3
  43. package/std/term-rate.ts +142 -66
  44. package/std/term.ts +782 -406
  45. package/std/testing.ts +14 -7
package/std/term-probe.ts CHANGED
@@ -432,9 +432,13 @@ export class TermProbe {
432
432
  sgrRead = sgrRead || toI32(e.text.indexOf("48:2:")) >= 0 || toI32(e.text.indexOf("48;2;")) >= 0
433
433
  }
434
434
  }
435
- if (caps.cellWidthPx <= 0 && areaW > 0 && this.cols > 0 && areaH > 0 && this.rows > 0) {
436
- caps.cellWidthPx = (areaW / this.cols) | 0
437
- caps.cellHeightPx = (areaH / this.rows) | 0
435
+ // The grid size is read into locals: a divisor the prover clears is a
436
+ // local behind `d > 0`, which a field read twice is not.
437
+ const cols: i32 = this.cols
438
+ const rows: i32 = this.rows
439
+ if (caps.cellWidthPx <= 0 && areaW > 0 && cols > 0 && areaH > 0 && rows > 0) {
440
+ caps.cellWidthPx = (areaW / cols) | 0
441
+ caps.cellHeightPx = (areaH / rows) | 0
438
442
  }
439
443
  if (caps.cellWidthPx <= 0 || caps.cellHeightPx <= 0) {
440
444
  caps.cellWidthPx = 0
package/std/term-rate.ts CHANGED
@@ -198,6 +198,9 @@ const rateLocalRttMs: f64 = 5.0
198
198
  /** The deepest detail step: 256 colours on top of the others. */
199
199
  const rateMaxLevel: i32 = 4
200
200
 
201
+ /** The slowest tier, which every controller's tiers end at. */
202
+ const rateLeastTier: i32 = 5
203
+
201
204
  /** The least time the link is taken to spend on a frame, so one fast answer cannot read as unbounded bandwidth. */
202
205
  const rateMinBusyMs: f64 = 0.5
203
206
 
@@ -236,9 +239,9 @@ const rateLevelScale = (from: i32, to: i32): f64 => {
236
239
  level = level + 1
237
240
  scale = scale / rateLevelFactor(level)
238
241
  }
239
- while (level > to) {
240
- scale = scale * rateLevelFactor(level)
241
- level = level - 1
242
+ // Counted up like the loop above, which the prover clears; the factors are powers of two, so the order changes no bit.
243
+ for (let k: i32 = to; k < from; k++) {
244
+ scale = scale * rateLevelFactor(k + 1)
242
245
  }
243
246
  return scale
244
247
  }
@@ -347,6 +350,12 @@ export class TermStats {
347
350
  export class RateController {
348
351
  /** The tiers allowed, fastest first, ending at 5. */
349
352
  private tiers: i32[]
353
+ /** The first of `tiers`, the fastest allowed. */
354
+ private fastestFps: i32
355
+ /** The frame rate `stepUp` found, when it answers true. */
356
+ private upFps: i32
357
+ /** The detail step `stepUp` found, when it answers true. */
358
+ private upLevel: i32
350
359
  /**
351
360
  * Paces frames at `tier`.
352
361
  * @internal Used by `termIoWait` in term-io.ts.
@@ -476,13 +485,21 @@ export class RateController {
476
485
  this.fixedFps = capped > 0 ? capped : 0
477
486
  const all: i32[] = [120, 60, 30, 20, 15, 10, 5]
478
487
  this.tiers = []
488
+ // Taken while pushing, not read back by index: 5 is always pushed, so it is set.
489
+ let fastest: i32 = 0
479
490
  for (const t of all) {
480
491
  if (t === 5 || (t <= options.maxFps && (t !== 120 || options.allow120))) {
481
492
  this.tiers.push(t)
493
+ if (fastest === 0) {
494
+ fastest = t
495
+ }
482
496
  }
483
497
  }
498
+ this.fastestFps = fastest
499
+ this.upFps = fastest
500
+ this.upLevel = 0
484
501
  // The least tier at or above `minFps`; the tiers end at 5, which is below every sensible floor.
485
- let minTier: i32 = this.tiers[0]
502
+ let minTier: i32 = fastest
486
503
  for (const t of this.tiers) {
487
504
  if (t >= options.minFps) {
488
505
  minTier = t
@@ -492,13 +509,14 @@ export class RateController {
492
509
  this.floorFps = this.prefer === DETAIL_FIRST ? 5 : minTier
493
510
  const remote: boolean =
494
511
  options.remoteHint && !(options.probeRttMs > 0.0 && options.probeRttMs < rateLocalRttMs)
495
- let want: i32 = this.tiers[0]
512
+ let want: i32 = fastest
496
513
  if (remote) {
497
514
  want = 15
498
515
  } else if (options.probeRttMs >= rateLocalRttMs) {
499
516
  want = 30
500
517
  }
501
- let start: i32 = this.tiers[this.tiers.length - 1]
518
+ // The last tier, 5, which is pushed whatever the options.
519
+ let start: i32 = rateLeastTier
502
520
  for (const t of this.tiers) {
503
521
  if (t <= want && t > start) {
504
522
  start = t
@@ -557,7 +575,10 @@ export class RateController {
557
575
  * @returns How many frames await their acknowledgement, from 0.
558
576
  */
559
577
  unacked(): i32 {
560
- return toI32(this.sentMs.length) - this.head
578
+ // `head` is never negative; the test only lets the prover see the difference fit.
579
+ const head: i32 = this.head
580
+ const length: i32 = toI32(this.sentMs.length)
581
+ return head > 0 ? length - head : length
561
582
  }
562
583
 
563
584
  /**
@@ -607,16 +628,26 @@ export class RateController {
607
628
  return
608
629
  }
609
630
  const size: i32 = bytes > 0 ? bytes : 0
610
- const length: i32 = toI32(this.sentMs.length)
611
- if (this.head > 0 && this.head * 2 >= length) {
631
+ const sent: f64[] = this.sentMs
632
+ const sizes: i32[] = this.sentBytes
633
+ const length: i32 = toI32(sent.length)
634
+ const head: i32 = this.head
635
+ if (head > 0 && head >= length - head) {
612
636
  // Drop the answered prefix once it is half the queue: a link always a frame behind never empties it.
613
- for (let i: i32 = this.head; i < length; i++) {
614
- this.sentMs[i - this.head] = this.sentMs[i]
615
- this.sentBytes[i - this.head] = this.sentBytes[i]
637
+ // Locals and a bounds test at each access, so the prover sees every index in range.
638
+ for (let i: i32 = head; i < length; i++) {
639
+ const to: i32 = i - head
640
+ if (i >= 0 && to >= 0 && to < length && i < toI32(sizes.length) && to < toI32(sizes.length)) {
641
+ sent[to] = sent[i]
642
+ sizes[to] = sizes[i]
643
+ }
616
644
  }
617
- for (let i: i32 = 0; i < this.head; i++) {
618
- this.sentMs.pop()
619
- this.sentBytes.pop()
645
+ const keep: i32 = length - head
646
+ while (sent.length > 0 && toI32(sent.length) > keep) {
647
+ sent.pop()
648
+ }
649
+ while (sizes.length > 0 && toI32(sizes.length) > keep) {
650
+ sizes.pop()
620
651
  }
621
652
  this.head = 0
622
653
  }
@@ -640,16 +671,22 @@ export class RateController {
640
671
  * @param nowMs When the acknowledgement was read, in milliseconds on the clock every call uses.
641
672
  */
642
673
  ackReceived(nowMs: f64): void {
643
- if (this.stale > 0) {
644
- this.stale = this.stale - 1
674
+ // Fields read into locals, which the prover keeps facts on.
675
+ const stale: i32 = this.stale
676
+ if (stale > 0) {
677
+ this.stale = stale - 1
645
678
  return
646
679
  }
647
- if (this.unacked() === 0) {
680
+ const head: i32 = this.head
681
+ const sentAt: f64[] = this.sentMs
682
+ const sizes: i32[] = this.sentBytes
683
+ if (head < 0 || head >= toI32(sentAt.length) || head >= toI32(sizes.length)) {
684
+ // No frame waiting: a stray answer.
648
685
  return
649
686
  }
650
- const sent: f64 = this.sentMs[this.head]
651
- const bytes: f64 = toF64(this.sentBytes[this.head])
652
- this.head = this.head + 1
687
+ const sent: f64 = sentAt[head]
688
+ const bytes: f64 = toF64(sizes[head])
689
+ this.head = head + 1
653
690
  if (!rateFinite(nowMs)) {
654
691
  return
655
692
  }
@@ -678,7 +715,10 @@ export class RateController {
678
715
  // The aged sample still reported is kept while trips take what it predicts: the quickest trip when it was taken,
679
716
  // which holds the whole latency and perhaps some bytes, so the prediction errs long, plus these bytes' time.
680
717
  const expected: f64 = this.rateQuickMs + (bytes * this.ackBusyMs) / this.ackBytes
681
- this.contradicted = rtt > rateStaleFactor * expected ? this.contradicted + 1 : 0
718
+ // Never past the threshold, which resets it, so the count stops there.
719
+ const was: i32 = this.contradicted
720
+ const more: i32 = was < rateStaleTrips ? was + 1 : rateStaleTrips
721
+ this.contradicted = rtt > rateStaleFactor * expected ? more : 0
682
722
  if (this.contradicted >= rateStaleTrips) {
683
723
  this.ackBytes = 0.0
684
724
  this.ackBusyMs = 0.0
@@ -688,11 +728,14 @@ export class RateController {
688
728
  }
689
729
  if (bytes > 0.0 && rtt > rateMinBusyMs) {
690
730
  // Whatever the latency and however late the answer was read, the bytes took no longer than the whole trip.
691
- if (toI32(this.floorPerSec.length) < rateTripCount) {
692
- this.floorPerSec.push((bytes * 1000.0) / rtt)
693
- } else {
694
- this.floorPerSec[this.floorNext] = (bytes * 1000.0) / rtt
695
- this.floorNext = (this.floorNext + 1) % rateTripCount
731
+ const floors: f64[] = this.floorPerSec
732
+ const next: i32 = this.floorNext
733
+ if (toI32(floors.length) < rateTripCount) {
734
+ floors.push((bytes * 1000.0) / rtt)
735
+ } else if (next >= 0 && next < toI32(floors.length)) {
736
+ // `floorNext` stays in [0, rateTripCount), the full list's length; the test shows the prover.
737
+ floors[next] = (bytes * 1000.0) / rtt
738
+ this.floorNext = (next + 1) % rateTripCount
696
739
  }
697
740
  this.updateRate()
698
741
  }
@@ -709,15 +752,20 @@ export class RateController {
709
752
  * down to their round trip.
710
753
  */
711
754
  private measureTrip(rtt: f64, bytes: f64): void {
755
+ const trips: f64[] = this.tripMs
756
+ const sizes: f64[] = this.tripBytes
757
+ // The quickest trip's values are kept as found, so no index outlives the loop's bound.
712
758
  let quickest: i32 = -1
713
- for (let i: i32 = 0; i < toI32(this.tripMs.length); i++) {
714
- if (quickest < 0 || this.tripMs[i] < this.tripMs[quickest]) {
759
+ let otherMs: f64 = 0.0
760
+ let otherBytes: f64 = 0.0
761
+ for (let i: i32 = 0; i < toI32(trips.length) && i < toI32(sizes.length); i++) {
762
+ if (quickest < 0 || trips[i] < otherMs) {
715
763
  quickest = i
764
+ otherMs = trips[i]
765
+ otherBytes = sizes[i]
716
766
  }
717
767
  }
718
768
  if (quickest >= 0) {
719
- const otherMs: f64 = this.tripMs[quickest]
720
- const otherBytes: f64 = this.tripBytes[quickest]
721
769
  const big: boolean = bytes > otherBytes
722
770
  const moreBytes: f64 = big ? bytes - otherBytes : otherBytes - bytes
723
771
  const moreMs: f64 = big ? rtt - otherMs : otherMs - rtt
@@ -726,13 +774,18 @@ export class RateController {
726
774
  this.measureRate(moreBytes, moreMs)
727
775
  }
728
776
  }
729
- if (toI32(this.tripMs.length) < rateTripCount) {
730
- this.tripMs.push(rtt)
731
- this.tripBytes.push(bytes)
732
- } else {
733
- this.tripMs[this.tripNext] = rtt
734
- this.tripBytes[this.tripNext] = bytes
735
- this.tripNext = (this.tripNext + 1) % rateTripCount
777
+ // Read again after `measureRate`: facts end at a call.
778
+ const kept: f64[] = this.tripMs
779
+ const keptBytes: f64[] = this.tripBytes
780
+ const next: i32 = this.tripNext
781
+ if (toI32(kept.length) < rateTripCount) {
782
+ kept.push(rtt)
783
+ keptBytes.push(bytes)
784
+ } else if (next >= 0 && next < toI32(kept.length) && next < toI32(keptBytes.length)) {
785
+ // `tripNext` stays in [0, rateTripCount), the full lists' length; the test shows the prover.
786
+ kept[next] = rtt
787
+ keptBytes[next] = bytes
788
+ this.tripNext = (next + 1) % rateTripCount
736
789
  }
737
790
  }
738
791
 
@@ -796,10 +849,13 @@ export class RateController {
796
849
  */
797
850
  private leastLatency(): f64 {
798
851
  let least: f64 = -1.0
799
- for (let i: i32 = 0; i < toI32(this.tripMs.length); i++) {
800
- const bytes: f64 = this.tripBytes[i]
852
+ const trips: f64[] = this.tripMs
853
+ const sizes: f64[] = this.tripBytes
854
+ // The lists grow together; bounding by both lets the prover see each index.
855
+ for (let i: i32 = 0; i < toI32(trips.length) && i < toI32(sizes.length); i++) {
856
+ const bytes: f64 = sizes[i]
801
857
  if (bytes <= 0.0 || this.haveRate) {
802
- const left: f64 = bytes > 0.0 ? this.tripMs[i] - (bytes * 1000.0) / this.bytesPerSec : this.tripMs[i]
858
+ const left: f64 = bytes > 0.0 ? trips[i] - (bytes * 1000.0) / this.bytesPerSec : trips[i]
803
859
  const latency: f64 = left > 0.0 ? left : 0.0
804
860
  if (least < 0.0 || latency < least) {
805
861
  least = latency
@@ -857,8 +913,11 @@ export class RateController {
857
913
  }
858
914
  if (this.haveRtt) {
859
915
  let rtt: f64 = this.rttMs
860
- if (this.unacked() > 0 && nowMs - this.sentMs[this.head] > rtt) {
861
- rtt = nowMs - this.sentMs[this.head]
916
+ // A frame waits exactly when `head` is inside the queue; tested on locals so the prover sees the index.
917
+ const sent: f64[] = this.sentMs
918
+ const head: i32 = this.head
919
+ if (head >= 0 && head < toI32(sent.length) && nowMs - sent[head] > rtt) {
920
+ rtt = nowMs - sent[head]
862
921
  }
863
922
  const base: f64 = this.fixedPart(rtt, scale)
864
923
  const trip: f64 =
@@ -910,7 +969,7 @@ export class RateController {
910
969
 
911
970
  /** One step down the path at `nowMs`, when over budget: fewer frames, or less detail. */
912
971
  private stepDown(nowMs: f64): void {
913
- const least: i32 = this.tiers[this.tiers.length - 1]
972
+ const least: i32 = rateLeastTier
914
973
  if (this.tier > this.floorFps) {
915
974
  // Straight to the fastest tier down to the floor that fits.
916
975
  let fps: i32 = this.slowerTier(this.tier, this.floorFps)
@@ -920,9 +979,10 @@ export class RateController {
920
979
  this.moveTo(fps, this.level, nowMs)
921
980
  return
922
981
  }
923
- const here: f64 = this.load(this.tier, this.level, nowMs)
924
- if (this.level < rateMaxLevel && this.load(this.tier, this.level + 1, nowMs) <= here * rateStepGain) {
925
- this.moveTo(this.tier, this.level + 1, nowMs)
982
+ const level: i32 = this.level
983
+ const here: f64 = this.load(this.tier, level, nowMs)
984
+ if (level < rateMaxLevel && this.load(this.tier, level + 1, nowMs) <= here * rateStepGain) {
985
+ this.moveTo(this.tier, level + 1, nowMs)
926
986
  return
927
987
  }
928
988
  if (this.tier > least) {
@@ -935,18 +995,23 @@ export class RateController {
935
995
  }
936
996
 
937
997
  /**
938
- * The next state up the path, as `fps * 8 + level`, or -1 at the top. It is
939
- * the descent backwards: tiers below the floor first, then detail, then
940
- * tiers above it.
998
+ * Find the next state up the path into `upFps` and `upLevel`, or answer
999
+ * false at the top. It is the descent backwards: tiers below the floor
1000
+ * first, then detail, then tiers above it.
941
1001
  */
942
- private stepUp(): i32 {
943
- if (this.tier < this.floorFps || (this.level === 0 && this.tier < this.tiers[0])) {
944
- return this.fasterTier(this.tier) * 8 + this.level
1002
+ private stepUp(): boolean {
1003
+ const level: i32 = this.level
1004
+ if (this.tier < this.floorFps || (level === 0 && this.tier < this.fastestFps)) {
1005
+ this.upFps = this.fasterTier(this.tier)
1006
+ this.upLevel = level
1007
+ return true
945
1008
  }
946
- if (this.level > 0) {
947
- return this.tier * 8 + this.level - 1
1009
+ if (level > 0) {
1010
+ this.upFps = this.tier
1011
+ this.upLevel = level - 1
1012
+ return true
948
1013
  }
949
- return -1
1014
+ return false
950
1015
  }
951
1016
 
952
1017
  /**
@@ -967,8 +1032,12 @@ export class RateController {
967
1032
  // Their answers were lost too; with nothing drawn since, the next answer is a new frame's.
968
1033
  this.stale = 0
969
1034
  }
970
- while (this.unacked() > 0 && nowMs - this.sentMs[this.head] > wait) {
971
- this.head = this.head + 1
1035
+ // On locals, so the prover sees `head` inside the queue at each read.
1036
+ const sent: f64[] = this.sentMs
1037
+ let head: i32 = this.head
1038
+ while (head >= 0 && head < toI32(sent.length) && nowMs - sent[head] > wait) {
1039
+ head = head + 1
1040
+ this.head = head
972
1041
  this.lost = this.lost + 1
973
1042
  this.stale = this.stale + 1
974
1043
  this.staleSinceMs = nowMs
@@ -994,8 +1063,7 @@ export class RateController {
994
1063
  return
995
1064
  }
996
1065
  this.overSinceMs = -1.0
997
- const up: i32 = this.stepUp()
998
- if (up < 0 || this.load((up / 8) | 0, up % 8, nowMs) > rateRiseMargin) {
1066
+ if (!this.stepUp() || this.load(this.upFps, this.upLevel, nowMs) > rateRiseMargin) {
999
1067
  this.underSinceMs = -1.0
1000
1068
  return
1001
1069
  }
@@ -1003,7 +1071,7 @@ export class RateController {
1003
1071
  this.underSinceMs = nowMs
1004
1072
  }
1005
1073
  if (settled && nowMs - this.underSinceMs >= rateRiseAfterMs) {
1006
- this.moveTo((up / 8) | 0, up % 8, nowMs)
1074
+ this.moveTo(this.upFps, this.upLevel, nowMs)
1007
1075
  }
1008
1076
  }
1009
1077
 
@@ -1011,7 +1079,8 @@ export class RateController {
1011
1079
  private publish(): void {
1012
1080
  const d: RateDecision = this.current
1013
1081
  d.fps = this.tier
1014
- d.resolutionDivisor = this.level >= 2 ? 4 : this.level + 1
1082
+ const level: i32 = this.level
1083
+ d.resolutionDivisor = level >= 2 ? 4 : level + 1
1015
1084
  d.backendCap = this.level >= 3 ? BACKEND_CAP_HALFBLOCK : BACKEND_CAP_KITTY
1016
1085
  d.palette256 = this.level >= rateMaxLevel
1017
1086
  const s: TermStats = this.measured
@@ -1076,11 +1145,18 @@ const rateBackendName = (cap: i32): string => {
1076
1145
  export const drawStatsOverlay = (screen: Screen, stats: TermStats, corner: i32): void => {
1077
1146
  const top: string = `${stats.tier}fps ${rateWhole(stats.rttMs)}ms ${rateWhole(stats.bytesPerSec / 1000.0)}kB/s`
1078
1147
  const bottom: string = `${rateWhole(stats.frameCostMs)}ms drop ${stats.dropped} ${rateBackendName(stats.backend)}`
1079
- const width: i32 = toI32(top.length > bottom.length ? top.length : bottom.length)
1148
+ const topWidth: i32 = toI32(top.length)
1149
+ const bottomWidth: i32 = toI32(bottom.length)
1150
+ const width: i32 = topWidth > bottomWidth ? topWidth : bottomWidth
1080
1151
  const right: boolean = corner === CORNER_TOP_RIGHT || corner === CORNER_BOTTOM_RIGHT
1081
1152
  const below: boolean = corner === CORNER_BOTTOM_LEFT || corner === CORNER_BOTTOM_RIGHT
1082
- const x: i32 = right && screen.width > width ? screen.width - width : 0
1083
- const y: i32 = below && screen.height > 2 ? screen.height - 2 : 0
1153
+ // The screen's size in locals, which the prover keeps facts on; each row is worked out apart, so no sum can overflow.
1154
+ // Both widths are lengths and so never negative: the two tests only let the prover see the difference fit.
1155
+ const screenWidth: i32 = screen.width
1156
+ const screenHeight: i32 = screen.height
1157
+ const x: i32 = right && width >= 0 && screenWidth >= 0 && screenWidth > width ? screenWidth - width : 0
1158
+ const y: i32 = below && screenHeight > 2 ? screenHeight - 2 : 0
1159
+ const next: i32 = below && screenHeight > 2 ? screenHeight - 1 : 1
1084
1160
  screen.text(x, y, top, COLOR_DEFAULT, COLOR_DEFAULT, ATTR_INVERSE)
1085
- screen.text(x, y + 1, bottom, COLOR_DEFAULT, COLOR_DEFAULT, ATTR_INVERSE)
1161
+ screen.text(x, next, bottom, COLOR_DEFAULT, COLOR_DEFAULT, ATTR_INVERSE)
1086
1162
  }