@amritk/nish-x86_64-linux 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 (46) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +29 -18
  3. package/package.json +1 -1
  4. package/runtime/gfx/README.md +55 -0
  5. package/runtime/gfx/nish-gfx.c +1063 -0
  6. package/runtime/gfx/nish-gfx.h +221 -0
  7. package/runtime/gfx/nish-gfx.overrides.json +14 -0
  8. package/runtime/nish.d.ts +22 -5
  9. package/runtime/nish.h +1 -0
  10. package/runtime/nish.mjs +1 -0
  11. package/runtime/runtime-bytes.c +167 -7
  12. package/runtime/runtime-net.c +122 -52
  13. package/runtime/runtime-uring.c +150 -69
  14. package/runtime/sqlite/nish-sqlite.c +166 -39
  15. package/scripts/build.sh +88 -3
  16. package/std/README.md +2 -2
  17. package/std/base64.ts +28 -30
  18. package/std/compress/deflate.ts +1371 -704
  19. package/std/crypto/aes.ts +81 -45
  20. package/std/crypto/bcrypt-batch.ts +23 -4
  21. package/std/crypto/bcrypt.ts +36 -11
  22. package/std/crypto/chacha20poly1305.ts +93 -52
  23. package/std/crypto/x25519.ts +7 -4
  24. package/std/host/game.ts +52 -20
  25. package/std/host/ring.ts +115 -31
  26. package/std/host/text.ts +9 -3
  27. package/std/json.ts +83 -0
  28. package/std/math.ts +161 -131
  29. package/std/net/handler.ts +24 -6
  30. package/std/net/http-server.ts +358 -141
  31. package/std/net/http1-server.ts +44 -13
  32. package/std/net/prefork.ts +1 -2
  33. package/std/net/static.ts +7 -2
  34. package/std/random.ts +34 -51
  35. package/std/raster.ts +377 -113
  36. package/std/regex.ts +46 -15
  37. package/std/release.ts +19 -13
  38. package/std/sqlite.ts +380 -16
  39. package/std/synth.ts +20 -3
  40. package/std/temporal.ts +740 -363
  41. package/std/term-io.ts +81 -33
  42. package/std/term-pixels.ts +287 -112
  43. package/std/term-probe.ts +7 -3
  44. package/std/term-rate.ts +142 -66
  45. package/std/term.ts +782 -406
  46. package/std/testing.ts +14 -7
package/std/raster.ts CHANGED
@@ -90,7 +90,15 @@ export const rasterPixel = (v: f64): i32 => {
90
90
  }
91
91
 
92
92
  /** `v` clamped to a channel's 0..255. */
93
- const rasterByte = (v: i32): i32 => Math.min(Math.max(v, 0), 255)
93
+ const rasterByte = (v: i32): integer<0, 255> => {
94
+ if (v < 0) {
95
+ return 0
96
+ }
97
+ if (v > 255) {
98
+ return 255
99
+ }
100
+ return v
101
+ }
94
102
 
95
103
  /** `x` held to 0..1; NaN becomes 0. */
96
104
  const rasterUnit = (x: f64): f64 => {
@@ -117,18 +125,56 @@ export const rasterRgb = (r: i32, g: i32, b: i32): i32 =>
117
125
  * @param t - the fraction, clamped to 0..1; NaN is 0
118
126
  * @returns the weight, 0 to `RASTER_SOLID`
119
127
  */
120
- export const rasterWeight = (t: f64): i32 => {
128
+ export const rasterWeight = (t: f64): i32 => rasterWeightOf(t)
129
+
130
+ /** `rasterWeight`, typed as the 0..256 it returns, which is what lets a mix use it unchecked. */
131
+ const rasterWeightOf = (t: f64): integer<0, 256> => {
121
132
  if (!(t > 0.0)) {
122
133
  return 0
123
134
  }
124
135
  if (t >= 1.0) {
125
- return RASTER_SOLID
136
+ return 256
137
+ }
138
+ const w: i32 = toI32(t * 256.0)
139
+ // Never true: `t` is inside (0, 1), so `w` is 0..255. The test is what
140
+ // proves the result in range, which the float product cannot.
141
+ if (w < 0 || w > 256) {
142
+ return 0
143
+ }
144
+ return w
145
+ }
146
+
147
+ /** A weight clamped to 0..256: at or below 0 is none of the new colour, at or above 256 all of it. */
148
+ const rasterClampWeight = (w: i32): integer<0, 256> => {
149
+ if (w <= 0) {
150
+ return 0
151
+ }
152
+ if (w >= 256) {
153
+ return 256
126
154
  }
127
- return toI32(t * 256.0)
155
+ return w
128
156
  }
129
157
 
130
158
  /** Channel `a` moved toward `b` by `w` 256ths. */
131
- const rasterMixByte = (a: i32, b: i32, w: i32): i32 => a + (((b - a) * w) >> 8)
159
+ const rasterMixByte = (a: integer<0, 255>, b: integer<0, 255>, w: integer<0, 256>): i32 =>
160
+ a + (((b - a) * w) >> 8)
161
+
162
+ /** The channel of colour `c` that sits `shift` bits up: 16 red, 8 green, 0 blue. */
163
+ const rasterChannel = (c: i32, shift: i32): integer<0, 255> => {
164
+ const v: i32 = (c >> shift) & 255
165
+ // Never true: the mask keeps eight bits. The test is what types the result
166
+ // as a channel, which the mask alone does not.
167
+ if (v < 0 || v > 255) {
168
+ return 0
169
+ }
170
+ return v
171
+ }
172
+
173
+ /** `rasterMixWeight` for a weight already in 0..256, which a per-pixel loop holds once outside it. */
174
+ const rasterMixIn = (a: i32, b: i32, w: integer<0, 256>): i32 =>
175
+ (rasterMixByte(rasterChannel(a, 16), rasterChannel(b, 16), w) << 16) |
176
+ (rasterMixByte(rasterChannel(a, 8), rasterChannel(b, 8), w) << 8) |
177
+ rasterMixByte(rasterChannel(a, 0), rasterChannel(b, 0), w)
132
178
 
133
179
  /**
134
180
  * `a` moved toward `b` by a 0..256 weight, which a per-pixel loop computes
@@ -136,13 +182,10 @@ const rasterMixByte = (a: i32, b: i32, w: i32): i32 => a + (((b - a) * w) >> 8)
136
182
  *
137
183
  * @param a - the colour at weight 0
138
184
  * @param b - the colour at weight 256
139
- * @param w - the weight, 0 to `RASTER_SOLID`
185
+ * @param w - the weight, clamped to 0..`RASTER_SOLID`
140
186
  * @returns the mixed colour
141
187
  */
142
- export const rasterMixWeight = (a: i32, b: i32, w: i32): i32 =>
143
- (rasterMixByte((a >> 16) & 255, (b >> 16) & 255, w) << 16) |
144
- (rasterMixByte((a >> 8) & 255, (b >> 8) & 255, w) << 8) |
145
- rasterMixByte(a & 255, b & 255, w)
188
+ export const rasterMixWeight = (a: i32, b: i32, w: i32): i32 => rasterMixIn(a, b, rasterClampWeight(w))
146
189
 
147
190
  /**
148
191
  * `a` moved toward `b` by a fraction.
@@ -152,7 +195,7 @@ export const rasterMixWeight = (a: i32, b: i32, w: i32): i32 =>
152
195
  * @param t - how far, clamped to 0..1
153
196
  * @returns the mixed colour
154
197
  */
155
- export const rasterMix = (a: i32, b: i32, t: f64): i32 => rasterMixWeight(a, b, rasterWeight(t))
198
+ export const rasterMix = (a: i32, b: i32, t: f64): i32 => rasterMixIn(a, b, rasterWeightOf(t))
156
199
 
157
200
  /**
158
201
  * Two colours added channel by channel, each saturating at 255: how light adds.
@@ -173,7 +216,7 @@ export const rasterAdd = (a: i32, b: i32): i32 =>
173
216
  * @param k - the factor every channel is multiplied by, clamped to 0..1
174
217
  * @returns the dimmed colour
175
218
  */
176
- export const rasterDim = (c: i32, k: f64): i32 => rasterMixWeight(0, c, rasterWeight(k))
219
+ export const rasterDim = (c: i32, k: f64): i32 => rasterMixIn(0, c, rasterWeightOf(k))
177
220
 
178
221
  /**
179
222
  * The fully saturated colour at a hue, so a rainbow is
@@ -189,7 +232,7 @@ export const rasterHue = (h: f64): i32 => {
189
232
  }
190
233
  const h6: f64 = turn * 6.0
191
234
  const sector: i32 = toI32(Math.floor(h6))
192
- const rise: i32 = rasterByte(toI32((h6 - Math.floor(h6)) * 255.0))
235
+ const rise: integer<0, 255> = rasterByte(toI32((h6 - Math.floor(h6)) * 255.0))
193
236
  const fall: i32 = 255 - rise
194
237
  if (sector === 0) {
195
238
  return rasterRgb(255, rise, 0)
@@ -219,6 +262,19 @@ const rasterRows = (pixels: Int32Array, width: i32, height: i32): i32 => {
219
262
  return Math.min(height, toI32(pixels.length) / width)
220
263
  }
221
264
 
265
+ /**
266
+ * The index of pixel `(x, y)` in a `width`-wide frame of `size` pixels, or -1
267
+ * when it is before the start or past the end. One past the last pixel is an
268
+ * index, so a span that ends at the frame's end starts somewhere.
269
+ */
270
+ const rasterAt = (size: i32, width: i32, x: i32, y: i32): i32 => {
271
+ const at: i64 = toI64(y) * toI64(width) + toI64(x)
272
+ if (at < 0 || at > toI64(size)) {
273
+ return -1
274
+ }
275
+ return toI32(at)
276
+ }
277
+
222
278
  /** Pixels `x0..x1` (exclusive) of row `y` set to `colour`, or blended by `w` 256ths below 256, clipped. */
223
279
  const rasterSpan = (
224
280
  pixels: Int32Array,
@@ -228,25 +284,55 @@ const rasterSpan = (
228
284
  x1: i32,
229
285
  y: i32,
230
286
  colour: i32,
231
- w: i32
287
+ w: integer<0, 256>
232
288
  ): void => {
233
289
  if (y < 0 || y >= rows) {
234
290
  return
235
291
  }
236
292
  const from: i32 = Math.max(x0, 0)
237
293
  const to: i32 = Math.min(x1, width)
238
- const row: i32 = y * width
294
+ const size: i32 = toI32(pixels.length)
295
+ // `at` walks the row from pixel `from`: the loops end on `x`, and the row
296
+ // is inside the frame because `y < rows`, so `at < size` never ends one.
297
+ // It is what proves the index.
298
+ let at: i32 = rasterAt(size, width, from, y)
299
+ if (at < 0) {
300
+ return
301
+ }
239
302
  if (w >= RASTER_SOLID) {
240
- for (let x: i32 = from; x < to; x++) {
241
- pixels[row + x] = colour
303
+ for (let x: i32 = from; x < to && at < size; x++) {
304
+ pixels[at] = colour
305
+ at = at + 1
242
306
  }
243
307
  return
244
308
  }
245
- for (let x: i32 = from; x < to; x++) {
246
- pixels[row + x] = rasterMixWeight(pixels[row + x], colour, w)
309
+ for (let x: i32 = from; x < to && at < size; x++) {
310
+ pixels[at] = rasterMixIn(pixels[at], colour, w)
311
+ at = at + 1
247
312
  }
248
313
  }
249
314
 
315
+ /** One past pixel `last`, held to `limit`: the exclusive end of a run that ends on `last`. */
316
+ const rasterEnd = (last: i32, limit: i32): i32 => (last >= limit ? limit : last + 1)
317
+
318
+ /** The weight of row `y` of a `height`-row gradient: 0 at the top, 256 at the bottom. */
319
+ const rasterGradientWeight = (y: i32, height: i32): integer<0, 256> => {
320
+ if (height <= 1 || y <= 0) {
321
+ return 0
322
+ }
323
+ if (y >= height - 1) {
324
+ return 256
325
+ }
326
+ // In f64, where `y * 256` cannot overflow; the quotient of two exact
327
+ // integers rounds to nearest, so truncating it is the integer quotient.
328
+ const w: i32 = toI32((toF64(y) * 256.0) / toF64(height - 1))
329
+ // Never true: `y` is inside `1..height - 2`, so `w` is under 256.
330
+ if (w < 0 || w > 256) {
331
+ return 0
332
+ }
333
+ return w
334
+ }
335
+
250
336
  /**
251
337
  * Every pixel of the frame, as a vertical gradient: a background.
252
338
  *
@@ -259,8 +345,8 @@ const rasterSpan = (
259
345
  export const rasterGradient = (pixels: Int32Array, width: i32, height: i32, top: i32, bottom: i32): void => {
260
346
  const rows: i32 = rasterRows(pixels, width, height)
261
347
  for (let y: i32 = 0; y < rows; y++) {
262
- const colour: i32 = rasterMixWeight(top, bottom, height > 1 ? (y * 256) / (height - 1) : 0)
263
- rasterSpan(pixels, width, rows, 0, width, y, colour, RASTER_SOLID)
348
+ const colour: i32 = rasterMixIn(top, bottom, rasterGradientWeight(y, height))
349
+ rasterSpan(pixels, width, rows, 0, width, y, colour, 256)
264
350
  }
265
351
  }
266
352
 
@@ -292,11 +378,12 @@ export const rasterRect = (
292
378
  if (weight <= 0) {
293
379
  return
294
380
  }
381
+ const w: integer<0, 256> = rasterClampWeight(weight)
295
382
  const rows: i32 = rasterRows(pixels, width, height)
296
383
  const from: i32 = Math.max(y0, 0)
297
384
  const to: i32 = Math.min(y1, rows)
298
385
  for (let y: i32 = from; y < to; y++) {
299
- rasterSpan(pixels, width, rows, x0, x1, y, colour, weight)
386
+ rasterSpan(pixels, width, rows, x0, x1, y, colour, w)
300
387
  }
301
388
  }
302
389
 
@@ -332,22 +419,27 @@ export const rasterDisc = (
332
419
  const rows: i32 = rasterRows(pixels, width, height)
333
420
  const inner: f64 = r > 0.5 ? (r - 0.5) * (r - 0.5) : 0.0
334
421
  const outer: f64 = (r + 0.5) * (r + 0.5)
335
- const fullW: i32 = rasterWeight(a)
422
+ const fullW: integer<0, 256> = rasterWeightOf(a)
423
+ const size: i32 = toI32(pixels.length)
336
424
  const x0: i32 = Math.max(rasterPixel(cx - r - 1.0), 0)
337
- const x1: i32 = Math.min(rasterPixel(cx + r + 1.0) + 1, width)
425
+ const x1: i32 = rasterEnd(rasterPixel(cx + r + 1.0), width)
338
426
  const y0: i32 = Math.max(rasterPixel(cy - r - 1.0), 0)
339
- const y1: i32 = Math.min(rasterPixel(cy + r + 1.0) + 1, rows)
427
+ const y1: i32 = rasterEnd(rasterPixel(cy + r + 1.0), rows)
340
428
  for (let y: i32 = y0; y < y1; y++) {
341
429
  const dy: f64 = toF64(y) + 0.5 - cy
342
- const row: i32 = y * width
343
- for (let x: i32 = x0; x < x1; x++) {
430
+ // `at` walks the row as `rasterSpan`'s does.
431
+ let at: i32 = rasterAt(size, width, x0, y)
432
+ if (at < 0) {
433
+ return
434
+ }
435
+ for (let x: i32 = x0; x < x1 && at < size; x++) {
344
436
  const dx: f64 = toF64(x) + 0.5 - cx
345
437
  const d2: f64 = dx * dx + dy * dy
346
- if (d2 >= outer) {
347
- continue
438
+ if (d2 < outer) {
439
+ const w: integer<0, 256> = d2 <= inner ? fullW : rasterWeightOf((r + 0.5 - Math.sqrt(d2)) * a)
440
+ pixels[at] = w >= RASTER_SOLID ? colour : rasterMixIn(pixels[at], colour, w)
348
441
  }
349
- const w: i32 = d2 <= inner ? fullW : rasterWeight((r + 0.5 - Math.sqrt(d2)) * a)
350
- pixels[row + x] = w >= RASTER_SOLID ? colour : rasterMixWeight(pixels[row + x], colour, w)
442
+ at = at + 1
351
443
  }
352
444
  }
353
445
  }
@@ -380,25 +472,30 @@ export const rasterGlow = (
380
472
  }
381
473
  const rows: i32 = rasterRows(pixels, width, height)
382
474
  const r2: f64 = r * r
475
+ const size: i32 = toI32(pixels.length)
383
476
  const x0: i32 = Math.max(rasterPixel(cx - r), 0)
384
- const x1: i32 = Math.min(rasterPixel(cx + r) + 1, width)
477
+ const x1: i32 = rasterEnd(rasterPixel(cx + r), width)
385
478
  const y0: i32 = Math.max(rasterPixel(cy - r), 0)
386
- const y1: i32 = Math.min(rasterPixel(cy + r) + 1, rows)
479
+ const y1: i32 = rasterEnd(rasterPixel(cy + r), rows)
387
480
  for (let y: i32 = y0; y < y1; y++) {
388
481
  const dy: f64 = toF64(y) + 0.5 - cy
389
- const row: i32 = y * width
390
- for (let x: i32 = x0; x < x1; x++) {
482
+ // `at` walks the row as `rasterSpan`'s does.
483
+ let at: i32 = rasterAt(size, width, x0, y)
484
+ if (at < 0) {
485
+ return
486
+ }
487
+ for (let x: i32 = x0; x < x1 && at < size; x++) {
391
488
  const dx: f64 = toF64(x) + 0.5 - cx
392
489
  const d2: f64 = dx * dx + dy * dy
393
- if (d2 >= r2) {
394
- continue
395
- }
396
- const near: f64 = d2 / r2
397
- const fall: f64 = 1.0 - near
398
- const w: i32 = rasterWeight(fall * fall * strength)
399
- if (w > 0) {
400
- pixels[row + x] = rasterAdd(pixels[row + x], rasterMixWeight(0, colour, w))
490
+ if (d2 < r2) {
491
+ const near: f64 = d2 / r2
492
+ const fall: f64 = 1.0 - near
493
+ const w: integer<0, 256> = rasterWeightOf(fall * fall * strength)
494
+ if (w > 0) {
495
+ pixels[at] = rasterAdd(pixels[at], rasterMixIn(0, colour, w))
496
+ }
401
497
  }
498
+ at = at + 1
402
499
  }
403
500
  }
404
501
  }
@@ -428,14 +525,27 @@ const RASTER_FONT: string =
428
525
  // t to ~
429
526
  "88L889600AAAC=00AAA:400AAEE:00A:4:A00AA?1>00O248O244844244444448442448008E200"
430
527
 
431
- /** The bits of row `row` of the glyph for byte `c`; a byte outside 32..126 draws as `?`. */
432
- const rasterGlyphRow = (c: i32, row: i32): i32 => {
433
- const g: i32 = c >= 32 && c <= 126 ? c - 32 : 31
434
- const at: i32 = g * RASTER_GLYPH_H + row
435
- if (row < 0 || row >= RASTER_GLYPH_H || at < 0 || at >= toI32(RASTER_FONT.length)) {
528
+ /** The glyph number of byte `c`, its place in the font from 0 for a space; a byte outside 32..126 is `?`'s. */
529
+ const rasterGlyph = (c: i32): integer<0, 94> => {
530
+ const g: i32 = c - 32
531
+ if (g < 0 || g > 94) {
532
+ return 31
533
+ }
534
+ return g
535
+ }
536
+
537
+ /** The bits of row `row` of the glyph for byte `c` in `font`, which is `RASTER_FONT`. */
538
+ const rasterGlyphRow = (font: string, c: i32, row: i32): i32 => {
539
+ // 7 is `RASTER_GLYPH_H`, written out so the prover sees the product's bound.
540
+ if (row < 0 || row >= 7) {
436
541
  return 0
437
542
  }
438
- return toI32(RASTER_FONT.charCodeAt(at)) - 48
543
+ const at: i32 = rasterGlyph(c) * 7 + row
544
+ // `at < 0` is never true, but the bound on the sum does not survive into `at`.
545
+ if (at < 0 || at >= toI32(font.length)) {
546
+ return 0
547
+ }
548
+ return toI32(font.charCodeAt(at)) - 48
439
549
  }
440
550
 
441
551
  /**
@@ -451,7 +561,34 @@ export const rasterTextWidth = (text: string, scale: i32): i32 => {
451
561
  if (n === 0 || scale < 1) {
452
562
  return 0
453
563
  }
454
- return (n * RASTER_GLYPH_ADVANCE - 1) * Math.min(scale, RASTER_MAX_TEXT_SCALE)
564
+ // In i64: a string long enough to be wider than an `i32` saturates.
565
+ const size: integer<0, 1024> = rasterTextScale(scale)
566
+ // 6 is `RASTER_GLYPH_ADVANCE`, written out so the prover sees the bound.
567
+ const advance: i64 = toI64(size) * 6
568
+ return rasterEdge(toI64(n) * advance - toI64(size))
569
+ }
570
+
571
+ /** `v` held to the `i32` range, which keeps which side of a frame's edge it is on. */
572
+ const rasterEdge = (v: i64): i32 => {
573
+ if (v < -2147483648) {
574
+ return -2147483648
575
+ }
576
+ if (v > 2147483647) {
577
+ return 2147483647
578
+ }
579
+ return toI32(v)
580
+ }
581
+
582
+ /** A text scale held to 1..`RASTER_MAX_TEXT_SCALE`, or 0 for one under 1, which draws nothing. */
583
+ const rasterTextScale = (scale: i32): integer<0, 1024> => {
584
+ if (scale < 0) {
585
+ return 0
586
+ }
587
+ // 1024 is `RASTER_MAX_TEXT_SCALE`, written out because a range takes a literal.
588
+ if (scale > 1024) {
589
+ return 1024
590
+ }
591
+ return scale
455
592
  }
456
593
 
457
594
  /**
@@ -479,27 +616,50 @@ export const rasterText = (
479
616
  text: string,
480
617
  colour: i32
481
618
  ): void => {
482
- if (scale < 1) {
619
+ const size: integer<0, 1024> = rasterTextScale(scale)
620
+ if (size < 1) {
483
621
  return
484
622
  }
485
- const size: i32 = Math.min(scale, RASTER_MAX_TEXT_SCALE)
486
623
  const n: i32 = toI32(text.length)
487
- let gx: i32 = x
488
- for (let i: i32 = 0; i < n && gx < width; i++) {
624
+ // Positions are i64, where a glyph past either edge cannot overflow; each
625
+ // is held to the `i32` range at the draw, which keeps it past that edge.
626
+ const step: i64 = toI64(size)
627
+ // 5 and 6 are `RASTER_GLYPH_W` and `RASTER_GLYPH_ADVANCE`, written out so the
628
+ // prover sees each product's bound.
629
+ const wide: i64 = step * 5
630
+ const advance: i64 = step * 6
631
+ for (let i: i32 = 0; i < n; i++) {
632
+ // Held to the `i32` range, which changes nothing drawn: a glyph clamped
633
+ // to the top is past the right edge and ends the text, and one clamped to
634
+ // the bottom is wholly left of the frame and skipped. `top` and `left`
635
+ // are clamped the same way, so their blocks are clipped whole.
636
+ const gx: i32 = rasterEdge(toI64(x) + toI64(i) * advance)
637
+ if (gx >= width) {
638
+ return
639
+ }
489
640
  const c: i32 = toI32(text.charCodeAt(i))
490
- if (gx + RASTER_GLYPH_W * size > 0) {
641
+ if (toI64(gx) + wide > 0) {
491
642
  for (let row: i32 = 0; row < RASTER_GLYPH_H; row++) {
492
- const bits: i32 = rasterGlyphRow(c, row)
493
- const top: i32 = y + row * size
643
+ const bits: i32 = rasterGlyphRow(RASTER_FONT, c, row)
644
+ const top: i32 = rasterEdge(toI64(y) + toI64(row) * step)
494
645
  for (let col: i32 = 0; col < RASTER_GLYPH_W; col++) {
495
646
  if (((bits >> (RASTER_GLYPH_W - 1 - col)) & 1) !== 0) {
496
- const left: i32 = gx + col * size
497
- rasterRect(pixels, width, height, left, top, left + size, top + size, colour, RASTER_SOLID)
647
+ const left: i32 = rasterEdge(toI64(gx) + toI64(col) * step)
648
+ rasterRect(
649
+ pixels,
650
+ width,
651
+ height,
652
+ left,
653
+ top,
654
+ rasterEdge(toI64(left) + step),
655
+ rasterEdge(toI64(top) + step),
656
+ colour,
657
+ RASTER_SOLID
658
+ )
498
659
  }
499
660
  }
500
661
  }
501
662
  }
502
- gx = gx + RASTER_GLYPH_ADVANCE * size
503
663
  }
504
664
  }
505
665
 
@@ -550,23 +710,29 @@ export class RasterTrail {
550
710
  * @param y - the point's y
551
711
  */
552
712
  export const rasterPushTrail = (trail: RasterTrail, x: f64, y: f64): void => {
553
- const n: i32 = toI32(trail.xs.length)
554
- if (trail.head < 0 || trail.head >= n || n !== toI32(trail.ys.length)) {
713
+ const xs: f64[] = trail.xs
714
+ const ys: f64[] = trail.ys
715
+ const n: i32 = toI32(xs.length)
716
+ const head: i32 = trail.head
717
+ if (head < 0 || head >= n || n !== toI32(ys.length) || head >= toI32(ys.length)) {
555
718
  trail.head = 0
556
719
  return
557
720
  }
558
- trail.xs[trail.head] = x
559
- trail.ys[trail.head] = y
560
- trail.head = trail.head + 1 >= n ? 0 : trail.head + 1
561
- if (trail.count < n) {
562
- trail.count = trail.count + 1
721
+ xs[head] = x
722
+ ys[head] = y
723
+ trail.head = head + 1 >= n ? 0 : head + 1
724
+ const count: i32 = trail.count
725
+ if (count < n) {
726
+ trail.count = count + 1
563
727
  }
564
728
  }
565
729
 
566
730
  /**
567
731
  * A trail as a line of discs from oldest to newest, each larger and more
568
732
  * opaque than the one before: a streak that fades out behind what made it.
569
- * A point at `v` is drawn at pixel `offset + v * scale`.
733
+ * A point at `v` is drawn at pixel `offset + v * scale`. A `head` set
734
+ * outside the ring still draws the points `count` slots behind it that land
735
+ * in the ring, as an in-range head does, and no others.
570
736
  *
571
737
  * @param pixels - the frame, row-major
572
738
  * @param width - the frame's width in pixels
@@ -591,22 +757,46 @@ export const rasterTrail = (
591
757
  offsetX: f64,
592
758
  offsetY: f64
593
759
  ): void => {
594
- const n: i32 = toI32(trail.xs.length)
760
+ const xs: f64[] = trail.xs
761
+ const ys: f64[] = trail.ys
762
+ const n: i32 = toI32(xs.length)
595
763
  const count: i32 = Math.min(trail.count, n)
596
- for (let i: i32 = 0; i < count; i++) {
597
- // The oldest point is `count` slots behind the head, wrapping.
598
- let slot: i32 = trail.head - count + i
599
- if (slot < 0) {
600
- slot = slot + n
601
- }
602
- if (slot < 0 || slot >= n || slot >= toI32(trail.ys.length)) {
603
- continue
764
+ const head: i32 = trail.head
765
+ // The oldest point is `count` slots behind the head, wrapping once. A head
766
+ // out of the ring (a caller can set the field) still has the slots that land
767
+ // in it drawn. Point i sits at `head - count + i`, counted in f64, where it
768
+ // is exact for any head, so no sum can overflow. The points before slot 0
769
+ // wrap to the ring's end and are older than the rest, so they are drawn
770
+ // first, as one run of slots, and the others as a second.
771
+ if (count <= 0) {
772
+ return
773
+ }
774
+ const size: f64 = toF64(n)
775
+ const ysLength: i32 = toI32(ys.length)
776
+ const first: f64 = toF64(head) - toF64(count)
777
+ const steps: f64 = toF64(count) + 1.0
778
+ for (let pass: i32 = 0; pass < 2; pass++) {
779
+ // Point i of this run sits at slot `base + i`.
780
+ const base: f64 = pass === 0 ? first + size : first
781
+ const lo: f64 = Math.max(base, 0.0)
782
+ const hi: f64 = Math.min(base + toF64(count), size)
783
+ if (lo < hi) {
784
+ const end: i32 = toI32(hi)
785
+ // How far along the trail the point is, from 1 for the oldest.
786
+ let step: f64 = lo - base + 1.0
787
+ for (let slot: i32 = toI32(lo); slot < end; slot++) {
788
+ // The first two are always true in the run and prove the indexes; the
789
+ // third skips a slot a shorter `ys` does not hold.
790
+ if (slot >= 0 && slot < n && slot < ysLength) {
791
+ const f: f64 = step / steps
792
+ const radius: f64 = r * (f * 0.6 + 0.4)
793
+ const cx: f64 = offsetX + xs[slot] * scale
794
+ const cy: f64 = offsetY + ys[slot] * scale
795
+ rasterDisc(pixels, width, height, cx, cy, radius * scale, colour, alpha * f)
796
+ }
797
+ step = step + 1.0
798
+ }
604
799
  }
605
- const f: f64 = toF64(i + 1) / toF64(count + 1)
606
- const radius: f64 = r * (f * 0.6 + 0.4)
607
- const cx: f64 = offsetX + trail.xs[slot] * scale
608
- const cy: f64 = offsetY + trail.ys[slot] * scale
609
- rasterDisc(pixels, width, height, cx, cy, radius * scale, colour, alpha * f)
610
800
  }
611
801
  }
612
802
 
@@ -689,10 +879,14 @@ export class RasterParticles {
689
879
  * @returns the live count
690
880
  */
691
881
  live(): i32 {
882
+ const lives: f64[] = this.lives
883
+ const len: i32 = toI32(lives.length)
692
884
  let n: i32 = 0
693
- for (const life of this.lives) {
694
- if (life > 0.0) {
695
- n++
885
+ for (const life of lives) {
886
+ // `n < len` is never false: it counts at most the array's elements. It
887
+ // is what proves the increment.
888
+ if (life > 0.0 && n < len) {
889
+ n = n + 1
696
890
  }
697
891
  }
698
892
  return n
@@ -752,22 +946,47 @@ export const rasterSpawnParticle = (
752
946
  size: f64,
753
947
  colour: i32
754
948
  ): void => {
949
+ const xs: f64[] = pool.xs
950
+ const ys: f64[] = pool.ys
951
+ const prevXs: f64[] = pool.prevXs
952
+ const prevYs: f64[] = pool.prevYs
953
+ const vxs: f64[] = pool.vxs
954
+ const vys: f64[] = pool.vys
955
+ const lives: f64[] = pool.lives
956
+ const spans: f64[] = pool.spans
957
+ const sizes: f64[] = pool.sizes
958
+ const colours: i32[] = pool.colours
755
959
  const i: i32 = pool.next
756
- if (!pool.has(i)) {
960
+ // `pool.has(i)` refuses a pool whose arrays differ in length; the tests
961
+ // after it restate that against each array the stores below index.
962
+ if (
963
+ !pool.has(i) ||
964
+ i < 0 ||
965
+ i >= toI32(lives.length) ||
966
+ i >= toI32(xs.length) ||
967
+ i >= toI32(ys.length) ||
968
+ i >= toI32(prevXs.length) ||
969
+ i >= toI32(prevYs.length) ||
970
+ i >= toI32(vxs.length) ||
971
+ i >= toI32(vys.length) ||
972
+ i >= toI32(spans.length) ||
973
+ i >= toI32(sizes.length) ||
974
+ i >= toI32(colours.length)
975
+ ) {
757
976
  pool.next = 0
758
977
  return
759
978
  }
760
- pool.xs[i] = x
761
- pool.ys[i] = y
762
- pool.prevXs[i] = x
763
- pool.prevYs[i] = y
764
- pool.vxs[i] = vx
765
- pool.vys[i] = vy
766
- pool.lives[i] = life > 0.0 ? life : 0.0
767
- pool.spans[i] = life > 0.0 ? life : 1.0
768
- pool.sizes[i] = size
769
- pool.colours[i] = colour
770
- pool.next = i + 1 >= pool.capacity() ? 0 : i + 1
979
+ xs[i] = x
980
+ ys[i] = y
981
+ prevXs[i] = x
982
+ prevYs[i] = y
983
+ vxs[i] = vx
984
+ vys[i] = vy
985
+ lives[i] = life > 0.0 ? life : 0.0
986
+ spans[i] = life > 0.0 ? life : 1.0
987
+ sizes[i] = size
988
+ colours[i] = colour
989
+ pool.next = i + 1 >= toI32(lives.length) ? 0 : i + 1
771
990
  }
772
991
 
773
992
  /**
@@ -781,17 +1000,39 @@ export const rasterSpawnParticle = (
781
1000
  export const rasterStepParticles = (pool: RasterParticles, dt: f64, gravity: f64, drag: f64): void => {
782
1001
  const lost: f64 = drag * dt
783
1002
  const keep: f64 = rasterUnit(1.0 - lost)
784
- for (let i: i32 = 0; i < pool.capacity(); i++) {
785
- if (!pool.has(i) || pool.lives[i] <= 0.0) {
1003
+ const xs: f64[] = pool.xs
1004
+ const ys: f64[] = pool.ys
1005
+ const prevXs: f64[] = pool.prevXs
1006
+ const prevYs: f64[] = pool.prevYs
1007
+ const vxs: f64[] = pool.vxs
1008
+ const vys: f64[] = pool.vys
1009
+ const lives: f64[] = pool.lives
1010
+ const n: i32 = toI32(lives.length)
1011
+ // A pool whose arrays differ in length steps nothing, as `pool.has` decides.
1012
+ if (!pool.has(toI32(0))) {
1013
+ return
1014
+ }
1015
+ for (let i: i32 = 0; i < n; i++) {
1016
+ // Past the test above the arrays are one length, so this skips only dead
1017
+ // slots; the length tests are what prove each index.
1018
+ if (
1019
+ i >= toI32(xs.length) ||
1020
+ i >= toI32(ys.length) ||
1021
+ i >= toI32(prevXs.length) ||
1022
+ i >= toI32(prevYs.length) ||
1023
+ i >= toI32(vxs.length) ||
1024
+ i >= toI32(vys.length) ||
1025
+ lives[i] <= 0.0
1026
+ ) {
786
1027
  continue
787
1028
  }
788
- pool.prevXs[i] = pool.xs[i]
789
- pool.prevYs[i] = pool.ys[i]
790
- pool.vxs[i] = pool.vxs[i] * keep
791
- pool.vys[i] = (pool.vys[i] + gravity * dt) * keep
792
- pool.xs[i] = pool.xs[i] + pool.vxs[i] * dt
793
- pool.ys[i] = pool.ys[i] + pool.vys[i] * dt
794
- pool.lives[i] = pool.lives[i] - dt
1029
+ prevXs[i] = xs[i]
1030
+ prevYs[i] = ys[i]
1031
+ vxs[i] = vxs[i] * keep
1032
+ vys[i] = (vys[i] + gravity * dt) * keep
1033
+ xs[i] = xs[i] + vxs[i] * dt
1034
+ ys[i] = ys[i] + vys[i] * dt
1035
+ lives[i] = lives[i] - dt
795
1036
  }
796
1037
  }
797
1038
 
@@ -819,19 +1060,42 @@ export const rasterParticles = (
819
1060
  offsetX: f64,
820
1061
  offsetY: f64
821
1062
  ): void => {
822
- for (let i: i32 = 0; i < pool.capacity(); i++) {
823
- if (!pool.has(i) || pool.lives[i] <= 0.0) {
1063
+ const xs: f64[] = pool.xs
1064
+ const ys: f64[] = pool.ys
1065
+ const prevXs: f64[] = pool.prevXs
1066
+ const prevYs: f64[] = pool.prevYs
1067
+ const lives: f64[] = pool.lives
1068
+ const spans: f64[] = pool.spans
1069
+ const sizes: f64[] = pool.sizes
1070
+ const colours: i32[] = pool.colours
1071
+ const n: i32 = toI32(lives.length)
1072
+ // As in `rasterStepParticles`: a malformed pool draws nothing.
1073
+ if (!pool.has(toI32(0))) {
1074
+ return
1075
+ }
1076
+ for (let i: i32 = 0; i < n; i++) {
1077
+ // As in `rasterStepParticles`: only dead slots are skipped.
1078
+ if (
1079
+ i >= toI32(xs.length) ||
1080
+ i >= toI32(ys.length) ||
1081
+ i >= toI32(prevXs.length) ||
1082
+ i >= toI32(prevYs.length) ||
1083
+ i >= toI32(spans.length) ||
1084
+ i >= toI32(sizes.length) ||
1085
+ i >= toI32(colours.length) ||
1086
+ lives[i] <= 0.0
1087
+ ) {
824
1088
  continue
825
1089
  }
826
- const size: f64 = pool.sizes[i]
1090
+ const size: f64 = sizes[i]
827
1091
  const half: f64 = size / 2.0
828
- const x: f64 = pool.prevXs[i] + (pool.xs[i] - pool.prevXs[i]) * alpha - half
829
- const y: f64 = pool.prevYs[i] + (pool.ys[i] - pool.prevYs[i]) * alpha - half
1092
+ const x: f64 = prevXs[i] + (xs[i] - prevXs[i]) * alpha - half
1093
+ const y: f64 = prevYs[i] + (ys[i] - prevYs[i]) * alpha - half
830
1094
  const x0: i32 = rasterPixel(offsetX + x * scale + 0.5)
831
1095
  const y0: i32 = rasterPixel(offsetY + y * scale + 0.5)
832
1096
  const x1: i32 = rasterPixel(offsetX + (x + size) * scale + 0.5)
833
1097
  const y1: i32 = rasterPixel(offsetY + (y + size) * scale + 0.5)
834
- const weight: i32 = rasterWeight(pool.lives[i] / pool.spans[i])
835
- rasterRect(pixels, width, height, x0, y0, x1, y1, pool.colours[i], weight)
1098
+ const weight: i32 = rasterWeight(lives[i] / spans[i])
1099
+ rasterRect(pixels, width, height, x0, y0, x1, y1, colours[i], weight)
836
1100
  }
837
1101
  }