@amritk/nish-aarch64-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/sync/ring.ts CHANGED
@@ -99,6 +99,7 @@
99
99
 
100
100
  import { ACQUIRE, RELAXED, RELEASE, SEQ_CST, SHM_UNSUPPORTED, SharedRegion } from "nish/sync/shm"
101
101
  import { monotonicNanos } from "nish:process"
102
+ import { wrappingAdd, wrappingSub } from "nish:unsafe"
102
103
 
103
104
  /** `tryPop`, `popWait`: the ring is empty (or the wait ran out). */
104
105
  export const RING_EMPTY: i32 = -1
@@ -143,31 +144,76 @@ const RING_ASLEEP: i32 = 1
143
144
  const ringIsPowerOfTwo = (n: i32): boolean => n > 0 && (n & (n - 1)) === 0
144
145
 
145
146
  /**
146
- * An offset into the region, added in wrapping u32 so the sum is never a
147
- * panic site. Every caller adds a header constant or a masked index to a base
148
- * `ringFits` already placed, so the sum cannot pass the region's size; a
149
- * region of 2 GiB or more is refused before any ring is made in it.
147
+ * An i64 offset or size as an i32, held at the i32 range's ends instead of
148
+ * wrapped. A held answer is past every region (a region of 2 GiB or more is
149
+ * refused before any ring is made in it), so an offset that ever reached one
150
+ * would be refused by the region's own bounds check, not read at a wrapped place.
150
151
  */
151
- const ringAdd = (a: i32, b: i32): i32 => toI32(toU32(a) + toU32(b))
152
+ const ringHeld = (n: i64): i32 => {
153
+ if (n > toI64(2147483647)) {
154
+ return 2147483647
155
+ }
156
+ return n < toI64(-2147483648) ? -2147483648 : toI32(n)
157
+ }
158
+
159
+ /**
160
+ * An offset into the region. Every caller adds a header constant or a masked
161
+ * index to a base `ringFits` already placed, so the sum cannot pass the
162
+ * region's size; it is taken in i64 so it is never a panic site.
163
+ */
164
+ const ringAdd = (a: i32, b: i32): i32 => ringHeld(toI64(a) + toI64(b))
165
+
166
+ /** `n`, or 0 for a negative one: an offset `ringAdd` held, which a ring `ringFits` placed is never below 0. */
167
+ const ringFloor = (n: i32): integer<0, 2147483647> => (n >= 0 ? n : 0)
168
+
169
+ /**
170
+ * How many slots position `pos`'s slot is before a ring's last one: `mask -
171
+ * index`, which is `~pos & mask` for a mask one less than a power of two.
172
+ */
173
+ const ringBack = (pos: i64, mask: integer<0, 1073741823>): integer<0, 1073741823> => {
174
+ const back: i32 = ~toI32(pos) & mask
175
+ // Always true (`back` is at most `mask`); stated for the prover, and folded away.
176
+ return back >= 0 && back <= 1073741823 ? back : 0
177
+ }
152
178
 
153
- /** The product of two ring sizes in wrapping u32; the shapes it is given were checked first. */
154
- const ringMul = (a: i32, b: i32): i32 => toI32(toU32(a) * toU32(b))
179
+ /**
180
+ * The offset `back` slots of `stride` bytes before `last`, an MPSC slot's
181
+ * field in the ring's last slot. `ringFits` placed every slot inside the
182
+ * region, so the difference is never below 0; one that were answers -1, which
183
+ * the region's bounds check refuses.
184
+ */
185
+ const ringBackAt = (
186
+ last: integer<0, 2147483647>,
187
+ back: integer<0, 1073741823>,
188
+ stride: integer<0, 2147483647>
189
+ ): i32 => {
190
+ const at: i64 = toI64(last) - toI64(back) * toI64(stride)
191
+ return at >= toI64(0) ? toI32(at) : -1
192
+ }
193
+
194
+ /** The product of two ring sizes, held like `ringAdd`'s sum; the shapes it is given were checked first. */
195
+ const ringMul = (a: i32, b: i32): i32 => ringHeld(toI64(a) * toI64(b))
155
196
 
156
197
  /**
157
- * A ring position plus a count, in wrapping u64. Positions start at 0 and grow
158
- * by at most a ring's size per record, so they reach 2^63 only after
159
- * centuries of traffic; wrapping keeps the sum from being a panic site.
198
+ * A ring position plus a count. Positions are sequence counters that the
199
+ * producer and consumer compare only by their difference (`ringGap`), so
200
+ * they count modulo 2^64 by design: `wrappingAdd` is the counter's answer,
201
+ * not a hidden overflow, and they reach 2^63 only after centuries of traffic.
160
202
  */
161
- const ringPos = (pos: i64, by: i64): i64 => toI64(toU64(pos) + toU64(by))
203
+ const ringPos = (pos: i64, by: i64): i64 => wrappingAdd(pos, by)
162
204
 
163
- /** `a - b` in wrapping u32, for the same offsets `ringAdd` sums. */
164
- const ringSub = (a: i32, b: i32): i32 => toI32(toU32(a) - toU32(b))
205
+ /** `a - b`, for the same offsets `ringAdd` sums, held like its sum. */
206
+ const ringSub = (a: i32, b: i32): i32 => ringHeld(toI64(a) - toI64(b))
165
207
 
166
- /** How far position `a` is past `b`, in wrapping u64, as `ringPos` moves them. */
167
- const ringGap = (a: i64, b: i64): i64 => toI64(toU64(a) - toU64(b))
208
+ /** How far position `a` is past `b`: the difference of two `ringPos` counters, modulo 2^64 as they count. */
209
+ const ringGap = (a: i64, b: i64): i64 => wrappingSub(a, b)
168
210
 
169
- /** The bytes an SPSC record of `len` takes: its length word and the payload, rounded up to 4. */
170
- const ringRecordBytes = (len: i32): i32 => ringAdd(toI32((toU32(len) + toU32(3)) & ~toU32(3)), 4)
211
+ /**
212
+ * The bytes an SPSC record of `len` takes: its length word and the payload,
213
+ * rounded up to 4. Its callers have checked `len` is from 0 to `capacity - 4`,
214
+ * so the sum cannot leave i64.
215
+ */
216
+ const ringRecordWide = (len: i32): i64 => (toI64(len) + toI64(7)) & toI64(-4)
171
217
 
172
218
  /**
173
219
  * Whether a ring of `bytes` fits its region from `base` with its header
@@ -195,7 +241,10 @@ const ringCheckPlace = (region: SharedRegion, base: i32, bytes: i64, what: strin
195
241
  * 256-byte header and the data. Nothing is checked here; `initSpscRing`
196
242
  * refuses a capacity that is not a power of two from 8.
197
243
  * @param capacity the ring's data bytes, as `initSpscRing` will be given them
198
- * @returns how many bytes from the ring's base the region must hold
244
+ * @returns how many bytes from the ring's base the region must hold; a
245
+ * capacity whose size passes the i32 range answers 2147483647, which no
246
+ * power-of-two capacity `initSpscRing` takes reaches, and which `ringFits`
247
+ * refuses as a place in any region
199
248
  */
200
249
  export const spscRingBytes = (capacity: i32): i32 => ringAdd(RING_DATA_AT, capacity)
201
250
 
@@ -207,7 +256,9 @@ export const spscRingBytes = (capacity: i32): i32 => ringAdd(RING_DATA_AT, capac
207
256
  * will be given it
208
257
  * @param slotBytes the longest record, in bytes, as `initMpscRing` will be
209
258
  * given it
210
- * @returns how many bytes from the ring's base the region must hold
259
+ * @returns how many bytes from the ring's base the region must hold; a shape
260
+ * whose size passes the i32 range answers 2147483647, and `initMpscRing`
261
+ * and `tryInitMpscRing` refuse that shape through `ringFits`
211
262
  */
212
263
  export const mpscRingBytes = (slots: i32, slotBytes: i32): i32 =>
213
264
  ringAdd(RING_DATA_AT, ringMul(slots, ringAdd(RING_SLOT_HEAD, slotBytes)))
@@ -228,17 +279,28 @@ const ringRing = (region: SharedRegion, base: i32): void => {
228
279
  }
229
280
  }
230
281
 
231
- /** `monotonicNanos()` past which a wait of `timeoutMs` gives up; 0 for no limit. */
282
+ /**
283
+ * `monotonicNanos()` past which a wait of `timeoutMs` gives up; 0 for no limit.
284
+ * The clock is read only through `ringLeftMs`'s difference, so the deadline
285
+ * counts modulo 2^64 like a `ringPos` position.
286
+ */
232
287
  const ringDeadline = (timeoutMs: i32): i64 =>
233
- timeoutMs < 0 ? toI64(0) : ringPos(monotonicNanos(), toI64(toU64(timeoutMs) * toU64(1000000)))
288
+ timeoutMs < 0 ? toI64(0) : wrappingAdd(monotonicNanos(), toI64(timeoutMs) * toI64(1000000))
234
289
 
235
290
  /** The milliseconds left before `deadline`, rounded up: -1 for no limit, 0 when it has passed. */
236
291
  const ringLeftMs = (deadline: i64): i32 => {
237
292
  if (deadline === toI64(0)) {
238
293
  return -1
239
294
  }
240
- const left: i64 = toI64(toU64(deadline) - toU64(monotonicNanos()))
241
- return left <= toI64(0) ? 0 : toI32((toU64(left) + toU64(999999)) / toU64(1000000))
295
+ const left: i64 = wrappingSub(deadline, monotonicNanos())
296
+ if (left <= toI64(0)) {
297
+ return 0
298
+ }
299
+ const ms: i32 = toI32(left / toI64(1000000))
300
+ if (ms < 0 || ms > 2147483646) {
301
+ return 2147483647
302
+ }
303
+ return left % toI64(1000000) === toI64(0) ? ms : ms + 1
242
304
  }
243
305
 
244
306
  /**
@@ -271,8 +333,6 @@ export class SpscRing {
271
333
  private base: i32
272
334
  /** Data bytes, a power of two. */
273
335
  capacity: i32
274
- /** Whether a push rings the consumer's doorbell. */
275
- private doorbell: boolean
276
336
  /** The producer's position: bytes ever pushed (this object's, as a producer). */
277
337
  private tail: i64
278
338
  /** The consumer's position: bytes ever popped (this object's, as a consumer). */
@@ -297,6 +357,22 @@ export class SpscRing {
297
357
  private peekedEnd: i64 = -1
298
358
  /** The position of the record `lengthAt` last read, past any skip marker. */
299
359
  private found: i64 = 0
360
+ /** `capacity - 1`, which turns a position into an index; 0 for a capacity no ring has. */
361
+ private mask: integer<0, 1073741823> = 0
362
+ /** The data's offset in the region. */
363
+ private dataAt: integer<0, 2147483647> = 0
364
+ /**
365
+ * The offset of the data's last byte. An index is reached counting back
366
+ * from it by `mask - index`, which `~pos & mask` gives: an i32 difference
367
+ * of a place `ringFits` checked and a masked count, so it needs no hold.
368
+ */
369
+ private lastAt: integer<0, 2147483647> = 0
370
+ /** The producer's position's offset in the region. */
371
+ private tailAt: i32 = 0
372
+ /** The consumer's position's offset in the region. */
373
+ private headAt: i32 = 0
374
+ /** Whether a push rings the consumer's doorbell. */
375
+ private doorbell: boolean
300
376
 
301
377
  /**
302
378
  * Reads the header of the ring at `base`: its capacity and doorbell, and
@@ -310,9 +386,15 @@ export class SpscRing {
310
386
  this.region = region
311
387
  this.base = base
312
388
  this.capacity = region.loadI32(ringAdd(base, RING_SIZE_AT), RELAXED)
389
+ const mask: i32 = ringSub(this.capacity, 1)
390
+ this.mask = mask >= 0 && mask <= 1073741823 ? mask : 0
391
+ this.dataAt = ringFloor(ringAdd(base, RING_DATA_AT))
392
+ this.lastAt = ringFloor(ringAdd(this.dataAt, this.mask))
393
+ this.tailAt = ringAdd(base, RING_TAIL_AT)
394
+ this.headAt = ringAdd(base, RING_HEAD_AT)
313
395
  this.doorbell = region.loadI32(ringAdd(base, RING_BELL_AT), RELAXED) !== 0
314
- this.tail = region.loadI64(ringAdd(base, RING_TAIL_AT), ACQUIRE)
315
- this.head = region.loadI64(ringAdd(base, RING_HEAD_AT), ACQUIRE)
396
+ this.tail = region.loadI64(this.tailAt, ACQUIRE)
397
+ this.head = region.loadI64(this.headAt, ACQUIRE)
316
398
  this.seenHead = this.head
317
399
  this.seenTail = this.tail
318
400
  }
@@ -334,28 +416,28 @@ export class SpscRing {
334
416
  if (this.reserved >= 0) {
335
417
  panic("nish/sync/ring: tryPush while a reservation is open; commit or abandon it first")
336
418
  }
337
- // The length is tested before it is rounded: within 3 of 2^31 the rounded
338
- // size wraps negative and would pass the size test alone.
339
- const need: i32 = ringRecordBytes(len)
340
- if (len < 0 || len > ringSub(this.capacity, 4) || need > this.capacity) {
419
+ const mask: integer<0, 1073741823> = this.mask
420
+ // A length past `capacity - 4` (`mask - 3`) needs more than `capacity` bytes.
421
+ if (len < 0 || len > mask - 3) {
341
422
  panic(`nish/sync/ring: a record of ${len} bytes does not fit a ring of ${this.capacity}`)
342
423
  }
343
- const end: i64 = ringPos(this.tail, toI64(need))
424
+ const end: i64 = ringPos(this.tail, ringRecordWide(len))
344
425
  if (ringGap(end, this.seenHead) > toI64(this.capacity)) {
345
- this.seenHead = this.region.loadI64(ringAdd(this.base, RING_HEAD_AT), ACQUIRE)
426
+ this.seenHead = this.region.loadI64(this.headAt, ACQUIRE)
346
427
  if (ringGap(end, this.seenHead) > toI64(this.capacity)) {
347
428
  return false
348
429
  }
349
430
  }
350
- const mask: i32 = ringSub(this.capacity, 1)
351
- const data: i32 = ringAdd(this.base, RING_DATA_AT)
352
- const at: i32 = toI32(this.tail & toI64(mask))
353
- this.region.storeI32(ringAdd(data, at), len, RELAXED)
354
- const from: i32 = ringAdd(at, 4) & mask
355
- const first: i32 = len < ringSub(this.capacity, from) ? len : ringSub(this.capacity, from)
356
- this.region.write(ringAdd(data, from), src, off, first)
431
+ const last: integer<0, 2147483647> = this.lastAt
432
+ const back: i32 = ~toI32(this.tail) & mask
433
+ this.region.storeI32(last - back, len, RELAXED)
434
+ // The payload starts 4 past the length word: 4 fewer to count back, mod the lap.
435
+ const fromBack: i32 = (back - 4) & mask
436
+ const room: i32 = fromBack + 1
437
+ const first: i32 = len < room ? len : room
438
+ this.region.write(last - fromBack, src, off, first)
357
439
  if (first < len) {
358
- this.region.write(data, src, ringAdd(off, first), ringSub(len, first))
440
+ this.region.write(this.dataAt, src, ringAdd(off, first), ringSub(len, first))
359
441
  }
360
442
  this.publish(end)
361
443
  return true
@@ -365,10 +447,10 @@ export class SpscRing {
365
447
  private publish(end: i64): void {
366
448
  this.tail = end
367
449
  if (this.doorbell) {
368
- this.region.storeI64(ringAdd(this.base, RING_TAIL_AT), end, SEQ_CST)
450
+ this.region.storeI64(this.tailAt, end, SEQ_CST)
369
451
  ringRing(this.region, this.base)
370
452
  } else {
371
- this.region.storeI64(ringAdd(this.base, RING_TAIL_AT), end, RELEASE)
453
+ this.region.storeI64(this.tailAt, end, RELEASE)
372
454
  }
373
455
  }
374
456
 
@@ -378,7 +460,7 @@ export class SpscRing {
378
460
  return window
379
461
  }
380
462
  const none: i32 = 0
381
- const w: u8[] = this.region.lend(ringAdd(this.base, RING_DATA_AT), none, window)
463
+ const w: u8[] = this.region.lend(this.dataAt, none, window)
382
464
  this.region.giveBack(w)
383
465
  return w
384
466
  }
@@ -411,21 +493,24 @@ export class SpscRing {
411
493
  if (n < 1 || n > most) {
412
494
  panic(`nish/sync/ring: a reservation of ${n} bytes is not from 1 to ${most}, half the ring less 4`)
413
495
  }
414
- const need: i32 = ringRecordBytes(n)
415
- const at: i32 = toI32(this.tail & toI64(ringSub(this.capacity, 1)))
416
- // The rest of the lap is padding when the record would cross the end.
417
- const pad: i32 = need <= ringSub(this.capacity, at) ? 0 : ringSub(this.capacity, at)
418
- const end: i64 = ringPos(this.tail, toI64(ringAdd(pad, need)))
496
+ const need: i64 = ringRecordWide(n)
497
+ const mask: integer<0, 1073741823> = this.mask
498
+ const back: i32 = ~toI32(this.tail) & mask
499
+ // The rest of the lap, `back + 1` bytes, is padding when the record would cross the end.
500
+ const rest: i32 = back + 1
501
+ const pad: i32 = need <= toI64(rest) ? 0 : rest
502
+ const end: i64 = ringPos(ringPos(this.tail, toI64(pad)), need)
419
503
  if (ringGap(end, this.seenHead) > toI64(this.capacity)) {
420
- this.seenHead = this.region.loadI64(ringAdd(this.base, RING_HEAD_AT), ACQUIRE)
504
+ this.seenHead = this.region.loadI64(this.headAt, ACQUIRE)
421
505
  if (ringGap(end, this.seenHead) > toI64(this.capacity)) {
422
506
  const none: u8[] = this.emptyWindow(this.wnd)
423
507
  this.wnd = none
424
508
  return none
425
509
  }
426
510
  }
427
- const from: i32 = pad > 0 ? 0 : at
428
- const w: u8[] = this.region.lend(ringAdd(ringAdd(this.base, RING_DATA_AT), ringAdd(from, 4)), n, this.wnd)
511
+ const last: integer<0, 2147483647> = this.lastAt
512
+ const lengthWord: i32 = pad > 0 ? this.dataAt : last - back
513
+ const w: u8[] = this.region.lend(ringAdd(lengthWord, 4), n, this.wnd)
429
514
  this.wnd = w
430
515
  this.reserved = n
431
516
  this.reservedPad = pad
@@ -456,14 +541,14 @@ export class SpscRing {
456
541
  panic(`nish/sync/ring: commit of ${len} bytes from a reservation of ${n}`)
457
542
  }
458
543
  this.endReservation("commit")
459
- const mask: i32 = ringSub(this.capacity, 1)
460
- const data: i32 = ringAdd(this.base, RING_DATA_AT)
461
- const at: i32 = toI32(this.tail & toI64(mask))
544
+ const last: integer<0, 2147483647> = this.lastAt
545
+ const mask: integer<0, 1073741823> = this.mask
546
+ const here: i32 = last - (~toI32(this.tail) & mask)
462
547
  if (this.reservedPad > 0) {
463
- this.region.storeI32(ringAdd(data, at), RING_SKIP, RELAXED)
548
+ this.region.storeI32(here, RING_SKIP, RELAXED)
464
549
  }
465
- this.region.storeI32(this.reservedPad > 0 ? data : ringAdd(data, at), len, RELAXED)
466
- this.publish(ringPos(this.tail, toI64(ringAdd(this.reservedPad, ringRecordBytes(len)))))
550
+ this.region.storeI32(this.reservedPad > 0 ? this.dataAt : here, len, RELAXED)
551
+ this.publish(ringPos(ringPos(this.tail, toI64(this.reservedPad)), ringRecordWide(len)))
467
552
  }
468
553
 
469
554
  /** Drops the open reservation, publishing nothing; panics without one. */
@@ -476,7 +561,7 @@ export class SpscRing {
476
561
  if (this.head !== this.seenTail) {
477
562
  return true
478
563
  }
479
- this.seenTail = this.region.loadI64(ringAdd(this.base, RING_TAIL_AT), order)
564
+ this.seenTail = this.region.loadI64(this.tailAt, order)
480
565
  return this.head !== this.seenTail
481
566
  }
482
567
 
@@ -486,17 +571,18 @@ export class SpscRing {
486
571
  * `found` is set to where that record is.
487
572
  */
488
573
  private lengthAt(pos: i64, end: i64): i32 {
489
- const mask: i32 = ringSub(this.capacity, 1)
490
- const data: i32 = ringAdd(this.base, RING_DATA_AT)
574
+ const mask: integer<0, 1073741823> = this.mask
575
+ const last: integer<0, 2147483647> = this.lastAt
491
576
  let at: i64 = pos
492
- let len: i32 = this.region.loadI32(ringAdd(data, toI32(pos & toI64(mask))), RELAXED)
577
+ const back: i32 = ~toI32(pos) & mask
578
+ let len: i32 = this.region.loadI32(last - back, RELAXED)
493
579
  if (len === RING_SKIP) {
494
- at = ringPos(pos, toI64(ringSub(this.capacity, toI32(pos & toI64(mask)))))
495
- len = this.region.loadI32(ringAdd(data, toI32(at & toI64(mask))), RELAXED)
580
+ // The rest of the lap, `back + 1` bytes, is padding.
581
+ at = ringPos(pos, toI64(back + 1))
582
+ len = this.region.loadI32(this.dataAt, RELAXED)
496
583
  }
497
- // The capacity first: a length a broken producer wrote near 2^31 would
498
- // overflow the rounding below and panic with the wrong reason.
499
- if (len < 0 || len > ringSub(this.capacity, 4) || toI64(ringRecordBytes(len)) > ringGap(end, at)) {
584
+ // A length past `capacity - 4` (`mask - 3`) needs more than `capacity` bytes.
585
+ if (len < 0 || len > mask - 3 || ringRecordWide(len) > ringGap(end, at)) {
500
586
  panic(`nish/sync/ring: a record of length ${len} at position ${at}: the ring is corrupt`)
501
587
  }
502
588
  this.found = at
@@ -512,13 +598,14 @@ export class SpscRing {
512
598
 
513
599
  /** Copies the record at `pos`, `len` bytes, into `dst` at `off`. */
514
600
  private copyOut(pos: i64, len: i32, dst: u8[], off: i32): void {
515
- const mask: i32 = ringSub(this.capacity, 1)
516
- const data: i32 = ringAdd(this.base, RING_DATA_AT)
517
- const from: i32 = toI32(ringPos(pos, toI64(4)) & toI64(mask))
518
- const first: i32 = len < ringSub(this.capacity, from) ? len : ringSub(this.capacity, from)
519
- this.region.read(ringAdd(data, from), dst, off, first)
601
+ const mask: integer<0, 1073741823> = this.mask
602
+ const fromBack: i32 = ((~toI32(pos) & mask) - 4) & mask
603
+ const room: i32 = fromBack + 1
604
+ const first: i32 = len < room ? len : room
605
+ const last: integer<0, 2147483647> = this.lastAt
606
+ this.region.read(last - fromBack, dst, off, first)
520
607
  if (first < len) {
521
- this.region.read(data, dst, ringAdd(off, first), ringSub(len, first))
608
+ this.region.read(this.dataAt, dst, ringAdd(off, first), ringSub(len, first))
522
609
  }
523
610
  }
524
611
 
@@ -540,12 +627,12 @@ export class SpscRing {
540
627
  return RING_EMPTY
541
628
  }
542
629
  const len: i32 = this.lengthAt(this.head, this.seenTail)
543
- if (len > ringSub(toI32(dst.length), off)) {
630
+ if (toI64(len) + toI64(off) > toI64(dst.length)) {
544
631
  return RING_TOO_SMALL
545
632
  }
546
633
  this.copyOut(this.found, len, dst, off)
547
- this.head = ringPos(this.found, toI64(ringRecordBytes(len)))
548
- this.region.storeI64(ringAdd(this.base, RING_HEAD_AT), this.head, RELEASE)
634
+ this.head = ringPos(this.found, ringRecordWide(len))
635
+ this.region.storeI64(this.headAt, this.head, RELEASE)
549
636
  return len
550
637
  }
551
638
 
@@ -567,13 +654,13 @@ export class SpscRing {
567
654
  this.notPeeked("popBatch")
568
655
  // Always the producer's position now, not the cache: a cache that still
569
656
  // holds a record would hide every one pushed since it was loaded.
570
- this.seenTail = this.region.loadI64(ringAdd(this.base, RING_TAIL_AT), ACQUIRE)
657
+ this.seenTail = this.region.loadI64(this.tailAt, ACQUIRE)
571
658
  let count: i32 = 0
572
659
  let used: i32 = 0
573
660
  let pos: i64 = this.head
574
661
  while (count < toI32(lens.length) && pos !== this.seenTail) {
575
662
  const len: i32 = this.lengthAt(pos, this.seenTail)
576
- if (len > ringSub(toI32(dst.length), used)) {
663
+ if (toI64(len) + toI64(used) > toI64(dst.length)) {
577
664
  break
578
665
  }
579
666
  this.copyOut(this.found, len, dst, used)
@@ -582,11 +669,11 @@ export class SpscRing {
582
669
  }
583
670
  count = count + 1
584
671
  used = ringAdd(used, len)
585
- pos = ringPos(this.found, toI64(ringRecordBytes(len)))
672
+ pos = ringPos(this.found, ringRecordWide(len))
586
673
  }
587
674
  if (count > 0) {
588
675
  this.head = pos
589
- this.region.storeI64(ringAdd(this.base, RING_HEAD_AT), pos, RELEASE)
676
+ this.region.storeI64(this.headAt, pos, RELEASE)
590
677
  }
591
678
  return count
592
679
  }
@@ -652,15 +739,17 @@ export class SpscRing {
652
739
  return none
653
740
  }
654
741
  const len: i32 = this.lengthAt(this.head, this.seenTail)
655
- const from: i32 = toI32(ringPos(this.found, toI64(4)) & toI64(ringSub(this.capacity, 1)))
656
- if (len === 0 || len > ringSub(this.capacity, from)) {
742
+ const mask: integer<0, 1073741823> = this.mask
743
+ const fromBack: i32 = ((~toI32(this.found) & mask) - 4) & mask
744
+ if (len === 0 || len > fromBack + 1) {
657
745
  const none: u8[] = this.emptyWindow(this.view)
658
746
  this.view = none
659
747
  return none
660
748
  }
661
- const r: u8[] = this.region.lend(ringAdd(ringAdd(this.base, RING_DATA_AT), from), len, this.view)
749
+ const last: integer<0, 2147483647> = this.lastAt
750
+ const r: u8[] = this.region.lend(last - fromBack, len, this.view)
662
751
  this.view = r
663
- this.peekedEnd = ringPos(this.found, toI64(ringRecordBytes(len)))
752
+ this.peekedEnd = ringPos(this.found, ringRecordWide(len))
664
753
  return r
665
754
  }
666
755
 
@@ -676,7 +765,7 @@ export class SpscRing {
676
765
  this.region.giveBack(r)
677
766
  this.head = this.peekedEnd
678
767
  this.peekedEnd = -1
679
- this.region.storeI64(ringAdd(this.base, RING_HEAD_AT), this.head, RELEASE)
768
+ this.region.storeI64(this.headAt, this.head, RELEASE)
680
769
  }
681
770
  }
682
771
 
@@ -760,6 +849,22 @@ export class MpscRing {
760
849
  private doorbell: boolean
761
850
  /** The consumer's position: records ever popped (this object's, as the consumer). */
762
851
  head: i64
852
+ /** `slots - 1`, which turns a position into a slot's index; 0 for a count no ring has. */
853
+ private mask: integer<0, 1073741823> = 0
854
+ /** The bytes from one slot to the next: its head and its payload. */
855
+ private stride: integer<0, 2147483647> = 0
856
+ /**
857
+ * The offsets of the last slot's sequence number, length word and payload.
858
+ * A slot is reached counting back from them by `(mask - index) * stride`
859
+ * bytes, with `ringBack` and `ringBackAt`.
860
+ */
861
+ private lastSeqAt: integer<0, 2147483647> = 0
862
+ private lastLenAt: integer<0, 2147483647> = 0
863
+ private lastPayloadAt: integer<0, 2147483647> = 0
864
+ /** The producers' position's offset in the region. */
865
+ private tailAt: i32 = 0
866
+ /** The consumer's position's offset in the region. */
867
+ private headAt: i32 = 0
763
868
 
764
869
  /**
765
870
  * Reads the header of the ring at `base`: its shape, its doorbell and the
@@ -774,14 +879,16 @@ export class MpscRing {
774
879
  this.base = base
775
880
  this.slots = region.loadI32(ringAdd(base, RING_SIZE_AT), RELAXED)
776
881
  this.slotBytes = region.loadI32(ringAdd(base, RING_SLOT_AT), RELAXED)
882
+ const mask: i32 = ringSub(this.slots, 1)
883
+ this.mask = mask >= 0 && mask <= 1073741823 ? mask : 0
884
+ this.stride = ringFloor(ringAdd(RING_SLOT_HEAD, this.slotBytes))
885
+ this.lastSeqAt = ringFloor(ringAdd(ringAdd(base, RING_DATA_AT), ringMul(this.mask, this.stride)))
886
+ this.lastLenAt = ringFloor(ringAdd(this.lastSeqAt, 8))
887
+ this.lastPayloadAt = ringFloor(ringAdd(this.lastSeqAt, RING_SLOT_HEAD))
888
+ this.tailAt = ringAdd(base, RING_TAIL_AT)
889
+ this.headAt = ringAdd(base, RING_HEAD_AT)
777
890
  this.doorbell = region.loadI32(ringAdd(base, RING_BELL_AT), RELAXED) !== 0
778
- this.head = region.loadI64(ringAdd(base, RING_HEAD_AT), ACQUIRE)
779
- }
780
-
781
- /** The offset of position `pos`'s slot. */
782
- private slotAt(pos: i64): i32 {
783
- const index: i32 = toI32(pos & toI64(ringSub(this.slots, 1)))
784
- return ringAdd(ringAdd(this.base, RING_DATA_AT), ringMul(index, ringAdd(RING_SLOT_HEAD, this.slotBytes)))
891
+ this.head = region.loadI64(this.headAt, ACQUIRE)
785
892
  }
786
893
 
787
894
  /**
@@ -829,10 +936,13 @@ export class MpscRing {
829
936
  if (len < 0 || len > this.slotBytes) {
830
937
  return RING_TOO_LONG
831
938
  }
832
- const tailAt: i32 = ringAdd(this.base, RING_TAIL_AT)
939
+ const tailAt: i32 = this.tailAt
833
940
  let pos: i64 = this.region.loadI64(tailAt, RELAXED)
834
941
  while (true) {
835
- const seq: i64 = this.region.loadI64(this.slotAt(pos), ACQUIRE)
942
+ const seq: i64 = this.region.loadI64(
943
+ ringBackAt(this.lastSeqAt, ringBack(pos, this.mask), this.stride),
944
+ ACQUIRE
945
+ )
836
946
  if (seq === pos) {
837
947
  const seen: i64 = this.region.compareExchangeI64(tailAt, pos, ringPos(pos, toI64(1)), RELAXED)
838
948
  if (seen === pos) {
@@ -852,9 +962,10 @@ export class MpscRing {
852
962
  pos = again
853
963
  }
854
964
  }
855
- const slot: i32 = this.slotAt(pos)
856
- this.region.storeI32(ringAdd(slot, 8), len, RELAXED)
857
- this.region.write(ringAdd(slot, RING_SLOT_HEAD), src, off, len)
965
+ const by: integer<0, 1073741823> = ringBack(pos, this.mask)
966
+ const slot: i32 = ringBackAt(this.lastSeqAt, by, this.stride)
967
+ this.region.storeI32(ringBackAt(this.lastLenAt, by, this.stride), len, RELAXED)
968
+ this.region.write(ringBackAt(this.lastPayloadAt, by, this.stride), src, off, len)
858
969
  if (this.doorbell) {
859
970
  this.region.storeI64(slot, ringPos(pos, toI64(1)), SEQ_CST)
860
971
  ringRing(this.region, this.base)
@@ -866,25 +977,31 @@ export class MpscRing {
866
977
 
867
978
  /** Whether the slot at the consumer's position is filled. */
868
979
  private ready(order: i32): boolean {
869
- return this.region.loadI64(this.slotAt(this.head), order) === ringPos(this.head, toI64(1))
980
+ return (
981
+ this.region.loadI64(ringBackAt(this.lastSeqAt, ringBack(this.head, this.mask), this.stride), order) ===
982
+ ringPos(this.head, toI64(1))
983
+ )
870
984
  }
871
985
 
872
986
  /** Empties the consumer's slot for the producer one lap on. */
873
987
  private release(slot: i32): void {
874
988
  this.region.storeI64(slot, ringPos(this.head, toI64(this.slots)), RELEASE)
875
989
  this.head = ringPos(this.head, toI64(1))
876
- this.region.storeI64(ringAdd(this.base, RING_HEAD_AT), this.head, RELAXED)
990
+ this.region.storeI64(this.headAt, this.head, RELAXED)
877
991
  }
878
992
 
879
- /** The length in the consumer's slot, or `RING_CORRUPT` for one no producer writes. */
880
- private lengthAt(slot: i32): i32 {
881
- const len: i32 = this.region.loadI32(ringAdd(slot, 8), RELAXED)
993
+ /** The length in the consumer's slot, whose length word is at `at`, or `RING_CORRUPT` for one no producer writes. */
994
+ private lengthAt(at: i32): i32 {
995
+ const len: i32 = this.region.loadI32(at, RELAXED)
882
996
  return len < 0 || len > this.slotBytes ? RING_CORRUPT : len
883
997
  }
884
998
 
885
999
  /** The panic `tryPop` and `popWait` give for a slot `take` found corrupt. */
886
1000
  private corrupt(): void {
887
- const len: i32 = this.region.loadI32(ringAdd(this.slotAt(this.head), 8), RELAXED)
1001
+ const len: i32 = this.region.loadI32(
1002
+ ringBackAt(this.lastLenAt, ringBack(this.head, this.mask), this.stride),
1003
+ RELAXED
1004
+ )
888
1005
  panic(`nish/sync/ring: a record of length ${len} at position ${this.head}: the ring is corrupt`)
889
1006
  }
890
1007
 
@@ -920,16 +1037,16 @@ export class MpscRing {
920
1037
  if (!this.ready(ACQUIRE)) {
921
1038
  return RING_EMPTY
922
1039
  }
923
- const slot: i32 = this.slotAt(this.head)
924
- const len: i32 = this.lengthAt(slot)
1040
+ const by: integer<0, 1073741823> = ringBack(this.head, this.mask)
1041
+ const len: i32 = this.lengthAt(ringBackAt(this.lastLenAt, by, this.stride))
925
1042
  if (len === RING_CORRUPT) {
926
1043
  return RING_CORRUPT
927
1044
  }
928
- if (len > ringSub(toI32(dst.length), off)) {
1045
+ if (toI64(len) + toI64(off) > toI64(dst.length)) {
929
1046
  return RING_TOO_SMALL
930
1047
  }
931
- this.region.read(ringAdd(slot, RING_SLOT_HEAD), dst, off, len)
932
- this.release(slot)
1048
+ this.region.read(ringBackAt(this.lastPayloadAt, by, this.stride), dst, off, len)
1049
+ this.release(ringBackAt(this.lastSeqAt, by, this.stride))
933
1050
  return len
934
1051
  }
935
1052
 
@@ -950,16 +1067,16 @@ export class MpscRing {
950
1067
  let count: i32 = 0
951
1068
  let used: i32 = 0
952
1069
  while (count < toI32(lens.length) && this.ready(ACQUIRE)) {
953
- const slot: i32 = this.slotAt(this.head)
954
- const len: i32 = this.lengthAt(slot)
1070
+ const by: integer<0, 1073741823> = ringBack(this.head, this.mask)
1071
+ const len: i32 = this.lengthAt(ringBackAt(this.lastLenAt, by, this.stride))
955
1072
  if (len === RING_CORRUPT) {
956
1073
  this.corrupt()
957
1074
  }
958
- if (len > ringSub(toI32(dst.length), used)) {
1075
+ if (toI64(len) + toI64(used) > toI64(dst.length)) {
959
1076
  break
960
1077
  }
961
- this.region.read(ringAdd(slot, RING_SLOT_HEAD), dst, used, len)
962
- this.release(slot)
1078
+ this.region.read(ringBackAt(this.lastPayloadAt, by, this.stride), dst, used, len)
1079
+ this.release(ringBackAt(this.lastSeqAt, by, this.stride))
963
1080
  if (count >= 0 && count < toI32(lens.length)) {
964
1081
  lens[count] = len
965
1082
  }
@@ -1062,9 +1179,9 @@ export const initMpscRing = (
1062
1179
  return made.value
1063
1180
  }
1064
1181
 
1065
- /** `mpscRingBytes` in wrapping u64, where the most any i32 shape takes is far below 2^63. */
1182
+ /** `mpscRingBytes` in i64, where the most any i32 shape takes is far below 2^63. */
1066
1183
  const mpscRingBytesWide = (slots: i32, slotBytes: i32): i64 =>
1067
- toI64(toU64(RING_DATA_AT) + toU64(slots) * (toU64(RING_SLOT_HEAD) + toU64(slotBytes)))
1184
+ toI64(RING_DATA_AT) + toI64(slots) * toI64(RING_SLOT_HEAD) + toI64(slots) * toI64(slotBytes)
1068
1185
 
1069
1186
  /**
1070
1187
  * `initMpscRing`, with a shape or a place it cannot use answered as an `Err`
package/std/sync/shm.ts CHANGED
@@ -157,11 +157,18 @@ export class ShmError {
157
157
  }
158
158
 
159
159
  /**
160
- * The errno a failed runtime call answered, negated back to positive. The
161
- * negation wraps through `u32`, so a stray `-2147483648` from C stays itself
162
- * instead of overflowing; the runtime only answers small errnos.
160
+ * The errno a failed runtime call answered, negated back to positive: `-rc`,
161
+ * spelled `~rc + 1`. The runtime only answers small errnos; a stray
162
+ * `-2147483648` from C, whose negation is past an `i32`, answers `2147483647`
163
+ * instead of overflowing, and the test with the literal is what proves the sum.
163
164
  */
164
- const shmErrno = (rc: i32): i32 => toI32(toU32(0) - toU32(rc))
165
+ const shmErrno = (rc: i32): i32 => {
166
+ const m: i32 = ~rc
167
+ if (m < 0 || m > 2147483646) {
168
+ return 2147483647
169
+ }
170
+ return m + 1
171
+ }
165
172
 
166
173
  /** `Ok()` for 0, the error for a negated errno. */
167
174
  const shmDone = (rc: i32): Result<void, ShmError> => {