@amritk/nish-x86_64-linux 0.46.0 → 0.47.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.
package/std/term-io.ts CHANGED
@@ -245,10 +245,11 @@ const termIoOnlyAcks = (buf: u8[], n: i32): i32 => {
245
245
  if (n <= 0 || n % 4 !== 0) {
246
246
  return -1
247
247
  }
248
- if (n > toI32(buf.length)) {
248
+ const len: i32 = toI32(buf.length)
249
+ if (n > len) {
249
250
  return -1
250
251
  }
251
- for (let i: i32 = 0; i >= 0 && i + 3 < toI32(buf.length) && i < n; i = i + 4) {
252
+ for (let i: i32 = 0; i >= 0 && i < len - 3 && i < n; i = i + 4) {
252
253
  if (buf[i] !== 27 || buf[i + 1] !== 91 || buf[i + 2] !== 48 || buf[i + 3] !== 110) {
253
254
  return -1
254
255
  }
@@ -259,6 +260,14 @@ const termIoOnlyAcks = (buf: u8[], n: i32): i32 => {
259
260
  /** Parameter `i` of a reply, or -1 when it has fewer. */
260
261
  const termIoParam = (e: TermEvent, i: i32): i32 => (i < toI32(e.params.length) ? e.params[i] : -1)
261
262
 
263
+ /**
264
+ * The slot of the acknowledgement ring after `at`: the next one, and 0 after the last.
265
+ *
266
+ * @param at A slot of the ring.
267
+ * @returns The slot after it.
268
+ */
269
+ const termIoAckNext = (at: i32): i32 => (at >= 0 && at < TERM_IO_ACKS - 1 ? at + 1 : 0)
270
+
262
271
  /**
263
272
  * An open terminal: what it can do, the `Screen` and `Framebuffer` a program
264
273
  * draws into, and the state that reads its events and paces its frames.
@@ -502,7 +511,8 @@ export class Terminal {
502
511
  * the one known.
503
512
  */
504
513
  private drainAcks(): void {
505
- let owed: i32 = this.rate.unacked() + this.rate.stale
514
+ // Each count is an i32, so their sum is taken in i64, where it fits.
515
+ let owed: i64 = toI64(this.rate.unacked()) + toI64(this.rate.stale)
506
516
  let rtt: f64 = this.rate.rttMs > this.lastRttMs ? this.rate.rttMs : this.lastRttMs
507
517
  rtt = rtt > this.caps.probeRttMs ? rtt : this.caps.probeRttMs
508
518
  let bound: f64 = 2.0 * rtt
@@ -515,14 +525,15 @@ export class Terminal {
515
525
  if (left <= 0 || !waitStdin(left)) {
516
526
  return
517
527
  }
518
- const n: i32 = readStdin(this.readBuf, 0, TERM_IO_READ)
528
+ const buf: u8[] = this.readBuf
529
+ const n: i32 = readStdin(buf, 0, toI32(buf.length))
519
530
  if (n > 0) {
520
- const only: i32 = this.decoder.waiting() ? -1 : termIoOnlyAcks(this.readBuf, n)
531
+ const only: i32 = this.decoder.waiting() ? -1 : termIoOnlyAcks(buf, n)
521
532
  if (only > 0) {
522
- owed = owed - only
533
+ owed = owed - toI64(only)
523
534
  } else {
524
- for (const e of this.decoder.feedEvents(this.readBuf, n, termIoNowMs())) {
525
- if (termIoIsAck(e)) {
535
+ for (const e of this.decoder.feedEvents(buf, n, termIoNowMs())) {
536
+ if (termIoIsAck(e) && owed > 0) {
526
537
  owed = owed - 1
527
538
  }
528
539
  }
@@ -579,20 +590,40 @@ export class Terminal {
579
590
 
580
591
  /** Forget the send times of the frames the controller gave up on, oldest first, so the ring holds only frames it still waits for. */
581
592
  private dropLost(): void {
582
- while (this.ackCount > this.rate.unacked()) {
583
- this.ackHead = (this.ackHead + 1) % TERM_IO_ACKS
584
- this.ackCount = this.ackCount - 1
593
+ const unacked: i32 = this.rate.unacked()
594
+ let head: i32 = this.ackHead
595
+ let count: i32 = this.ackCount
596
+ while (count > 0 && count > unacked) {
597
+ head = termIoAckNext(head)
598
+ count = count - 1
585
599
  }
600
+ this.ackHead = head
601
+ this.ackCount = count
586
602
  }
587
603
 
588
604
  /** Remember that a frame acknowledgement was asked for at `nowMs`. */
589
605
  private ackPush(nowMs: f64): void {
590
- if (this.ackCount === TERM_IO_ACKS) {
591
- this.ackHead = (this.ackHead + 1) % TERM_IO_ACKS
592
- this.ackCount = this.ackCount - 1
606
+ let head: i32 = this.ackHead
607
+ let count: i32 = this.ackCount
608
+ if (count >= TERM_IO_ACKS) {
609
+ head = termIoAckNext(head)
610
+ count = TERM_IO_ACKS - 1
593
611
  }
594
- this.ackSent[(this.ackHead + this.ackCount) % TERM_IO_ACKS] = nowMs
595
- this.ackCount = this.ackCount + 1
612
+ // The ring keeps `0 <= head < TERM_IO_ACKS` and now holds fewer than
613
+ // `TERM_IO_ACKS`; the test states both where the slot is computed.
614
+ const sent: f64[] = this.ackSent
615
+ if (head >= 0 && count >= 0 && count < TERM_IO_ACKS) {
616
+ // The slot `count` past `head`, wrapped once; the sum is taken in i64, where it is exact.
617
+ const end: i64 = toI64(head) + toI64(count)
618
+ const wide: i64 = end < toI64(TERM_IO_ACKS) ? end : end - toI64(TERM_IO_ACKS)
619
+ const at: i32 = wide < toI64(0) || wide >= toI64(sent.length) ? -1 : toI32(wide)
620
+ if (at >= 0 && at < toI32(sent.length)) {
621
+ sent[at] = nowMs
622
+ }
623
+ count = count + 1
624
+ }
625
+ this.ackHead = head
626
+ this.ackCount = count
596
627
  }
597
628
 
598
629
  /**
@@ -603,11 +634,15 @@ export class Terminal {
603
634
  private acked(count: i32, nowMs: f64): void {
604
635
  for (let k: i32 = 0; k < count; k++) {
605
636
  this.dropLost()
606
- if (this.rate.stale === 0 && this.ackCount > 0) {
607
- this.keys.delivered(this.ackSent[this.ackHead])
608
- this.lastRttMs = nowMs - this.ackSent[this.ackHead]
609
- this.ackHead = (this.ackHead + 1) % TERM_IO_ACKS
610
- this.ackCount = this.ackCount - 1
637
+ const head: i32 = this.ackHead
638
+ const pending: i32 = this.ackCount
639
+ const sent: f64[] = this.ackSent
640
+ if (this.rate.stale === 0 && pending > 0 && head >= 0 && head < toI32(sent.length)) {
641
+ const sentAtMs: f64 = sent[head]
642
+ this.keys.delivered(sentAtMs)
643
+ this.lastRttMs = nowMs - sentAtMs
644
+ this.ackHead = termIoAckNext(head)
645
+ this.ackCount = pending - 1
611
646
  }
612
647
  this.rate.ackReceived(nowMs)
613
648
  }
@@ -632,9 +667,14 @@ export class Terminal {
632
667
  if (termIoParam(e, 0) === 6) {
633
668
  h = termIoParam(e, 1)
634
669
  w = termIoParam(e, 2)
635
- } else if (termIoParam(e, 0) === 4 && this.sizeCols > 0 && this.sizeRows > 0) {
636
- h = (termIoParam(e, 1) / this.sizeRows) | 0
637
- w = (termIoParam(e, 2) / this.sizeCols) | 0
670
+ } else if (termIoParam(e, 0) === 4) {
671
+ // Read into locals: the prover clears a divisor that is a local behind `d > 0`.
672
+ const cols: i32 = this.sizeCols
673
+ const rows: i32 = this.sizeRows
674
+ if (cols > 0 && rows > 0) {
675
+ h = (termIoParam(e, 1) / rows) | 0
676
+ w = (termIoParam(e, 2) / cols) | 0
677
+ }
638
678
  }
639
679
  if (termIoCellPx(w) > 0 && termIoCellPx(h) > 0) {
640
680
  this.cellWidthPx = termIoCellPx(w)
@@ -659,20 +699,23 @@ export class Terminal {
659
699
  * the terminal's and is taken here, a late cell size learned.
660
700
  */
661
701
  private take(events: TermEvent[], nowMs: f64, out: TermEvent[]): void {
662
- let replies: i32 = 0
702
+ let replied: boolean = false
663
703
  for (const e of events) {
664
704
  if (e.type === EVENT_REPLY) {
665
- replies = replies + 1
705
+ replied = true
666
706
  }
667
707
  }
668
708
  let program: TermEvent[] = events
669
709
  let acks: i32 = 0
670
- if (replies > 0) {
710
+ if (replied) {
671
711
  program = []
672
- for (const e of events) {
712
+ const n: i32 = toI32(events.length)
713
+ for (let i: i32 = 0; i < n && i < toI32(events.length); i++) {
714
+ const e: TermEvent = events[i]
673
715
  if (e.type !== EVENT_REPLY) {
674
716
  program.push(e)
675
- } else if (termIoIsAck(e)) {
717
+ } else if (termIoIsAck(e) && acks < n) {
718
+ // `acks` counts some of the `n` events, so it stays below `n`.
676
719
  acks = acks + 1
677
720
  } else {
678
721
  this.lateReply(e)
@@ -845,7 +888,9 @@ export class Terminal {
845
888
  const bytes: i32 = this.send(withPixels)
846
889
  const sentAtMs: f64 = toF64(this.sendStartNs) / 1000000.0
847
890
  this.dropLost()
848
- this.rate.frameSent(bytes, this.workNs + toF64(this.sendStartNs - start), sentAtMs)
891
+ // The two clock readings are taken apart in f64, where no difference overflows;
892
+ // past 2^53 ns (about 104 days of uptime) each reading rounds, by a few ns.
893
+ this.rate.frameSent(bytes, this.workNs + (toF64(this.sendStartNs) - toF64(start)), sentAtMs)
849
894
  this.workNs = 0.0
850
895
  this.ackPush(sentAtMs)
851
896
  }
@@ -878,9 +923,12 @@ export const openTerminal = (options: TerminalOptions): Result<Terminal, string>
878
923
  }
879
924
  const env: TermEnv = termIoEnv(options.graphics)
880
925
  const size: i32[] = terminalSize()
881
- const sized: boolean = toI32(size.length) >= 2 && size[0] > 0 && size[1] > 0
882
- const cols: i32 = sized ? size[0] : TERM_IO_COLS
883
- const rows: i32 = sized ? size[1] : TERM_IO_ROWS
926
+ let cols: i32 = TERM_IO_COLS
927
+ let rows: i32 = TERM_IO_ROWS
928
+ if (toI32(size.length) >= 2 && size[0] > 0 && size[1] > 0) {
929
+ cols = size[0]
930
+ rows = size[1]
931
+ }
884
932
  setTerminalRestore(resetStyle() + showCursor() + leaveAltScreen())
885
933
  write(enterAltScreen() + hideCursor() + probeQueries(env))
886
934
  const probe: TermProbe = new TermProbe(env, cols, rows, termIoNowMs())