@amritk/nish-aarch64-linux 0.39.0 → 0.41.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 (57) hide show
  1. package/INSTALL.md +79 -133
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +112 -33
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +21 -17
  6. package/runtime/nish.h +10 -0
  7. package/runtime/page-frame.d.ts +123 -0
  8. package/runtime/page-frame.mjs +736 -0
  9. package/runtime/page-game.d.ts +132 -0
  10. package/runtime/page-game.mjs +738 -0
  11. package/runtime/runtime-aes.c +85 -8
  12. package/runtime/runtime-bytes.c +88 -4
  13. package/runtime/runtime-net.c +32 -0
  14. package/runtime/runtime-sandbox.c +55 -28
  15. package/runtime/runtime-uring.c +1194 -0
  16. package/runtime/runtime-wasm.c +422 -0
  17. package/runtime/vite.mjs +1 -1
  18. package/scripts/build.sh +22 -6
  19. package/std/README.md +74 -87
  20. package/std/array.ts +36 -3
  21. package/std/collections.ts +51 -0
  22. package/std/crypto/hmac.ts +37 -10
  23. package/std/crypto/p256.ts +1388 -2664
  24. package/std/crypto/pki.ts +116 -4
  25. package/std/crypto/sha256.ts +34 -6
  26. package/std/crypto/x509.ts +7 -0
  27. package/std/host/game.ts +273 -0
  28. package/std/input.ts +56 -0
  29. package/std/json-tokenizer.ts +325 -112
  30. package/std/json-typed.ts +43 -8
  31. package/std/json-value.ts +144 -3
  32. package/std/json.ts +63 -8
  33. package/std/net/fetch.ts +22 -4
  34. package/std/net/http-server.ts +3382 -0
  35. package/std/net/http1-client-codec.ts +6 -2
  36. package/std/net/http1-codec.ts +137 -12
  37. package/std/net/http1-server.ts +83 -29
  38. package/std/net/http2.ts +47 -14
  39. package/std/net/http3-server.ts +29 -3
  40. package/std/net/http3.ts +180 -7
  41. package/std/net/prefork.ts +49 -13
  42. package/std/net/quic-recovery.ts +240 -9
  43. package/std/net/quic-stream.ts +21 -1
  44. package/std/net/quic.ts +98 -4
  45. package/std/net/router.ts +378 -31
  46. package/std/net/serve.ts +873 -65
  47. package/std/net/tls/verify.ts +19 -1
  48. package/std/net/url.ts +83 -35
  49. package/std/net/websocket.ts +74 -1
  50. package/std/net/webtransport.ts +7 -1
  51. package/std/raster.ts +837 -0
  52. package/std/release.ts +593 -0
  53. package/std/synth.ts +143 -0
  54. package/std/temporal.ts +3091 -0
  55. package/std/threads.ts +298 -0
  56. package/std/yaml-json.ts +368 -28
  57. package/std/yaml.ts +122 -27
package/std/net/router.ts CHANGED
@@ -25,6 +25,16 @@
25
25
  * nothing per request. A `Router` is therefore not reentrant: one match at a
26
26
  * time, which is what a connection loop does.
27
27
  *
28
+ * **A route with no parameter and no `*` is answered without the tree.** `add`
29
+ * also files such a route under a hash of its method and path, in one more
30
+ * flat array, and `match` looks there first, for a target no longer than the
31
+ * longest such route: one pass over the path, at most `ROUTER_PROBES` slots,
32
+ * and a byte comparison for a slot whose method and length agree. The
33
+ * tree stays the definition: a literal is tried first at every segment, so
34
+ * the route the exact table holds for a method and a path is the one the tree
35
+ * would have reached, and anything the table does not hold falls through to
36
+ * the tree unchanged. `matchTree` is the tree alone, for a test of exactly that.
37
+ *
28
38
  * **The rules** (`docs/wp43-http-app-layer.md` §2):
29
39
  *
30
40
  * - The path is the target up to its first `?` (the query is left alone) and
@@ -42,9 +52,20 @@
42
52
  * - A path that matches only routes of other methods answers
43
53
  * `ROUTER_METHOD_NOT_ALLOWED`, so a server can tell 405 from 404.
44
54
  *
55
+ * **What is unchecked.** `routerSameBytes` reads with `nish:unsafe`'s
56
+ * `uncheckedGet`, after one check that both ranges are inside their arrays;
57
+ * a range that is not compares unequal, as a byte that differs does. It is
58
+ * the one byte compare a lookup makes: a method of more than seven bytes
59
+ * against each known one (a shorter one is one packed word, `routerMethodKey`),
60
+ * a path against a static route's, a segment against a label.
61
+ * `routerMethodKey` reads its at most seven bytes the same way, after the same
62
+ * one range check; a range outside the array packs to 0, which no method is.
63
+ * Every other read is checked.
64
+ *
45
65
  * Private helpers share the importing program's flat symbol namespace
46
66
  * (`docs/wp26-stdlib.md` §3e), which is why each one carries the module's name.
47
67
  */
68
+ import { uncheckedGet, wrappingAdd } from "nish:unsafe"
48
69
 
49
70
  /** `add`: the pattern is empty, does not start with `/`, or misuses `:`, `*`, `%`, `?` or a byte outside `!` to `~`. */
50
71
  export const ROUTER_BAD_PATTERN: i32 = -1
@@ -73,6 +94,12 @@ export const ROUTER_NO_ROOM: i32 = -11
73
94
  const ROUTER_MAX_METHOD: i32 = 32
74
95
  /** No node or route: an `i32` spelled once, so that `--number-mode f64` does not read a bare `-1` as a double. */
75
96
  const ROUTER_NONE: i32 = -1
97
+ /**
98
+ * The most slots a lookup in the exact-path table reads, and so the furthest
99
+ * from its home slot `add` will file a route. A static route with no free slot
100
+ * that near is left to the tree, which answers it as it always did.
101
+ */
102
+ const ROUTER_PROBES: i32 = 8
76
103
  /** A pattern segment's kind, in `Router.add`'s scratch. */
77
104
  const ROUTER_LITERAL: i32 = 0
78
105
  const ROUTER_PARAM: i32 = 1
@@ -121,6 +148,18 @@ export class Router {
121
148
  private routeMethod: i32[]
122
149
  private endpointNext: i32[]
123
150
  private routeParams: i32[]
151
+ /**
152
+ * The exact-path table: per slot a static route, or -1. Its length is a
153
+ * power of two, at least four slots for each static route, and a route sits
154
+ * within `ROUTER_PROBES` slots of the one its hash names.
155
+ */
156
+ private staticSlots: i32[]
157
+ /** Per route: the hash of its method and path, and its path `staticPaths[routePathStart .. routePathEnd)`; zeros unless it is static. */
158
+ private routeHash: u32[]
159
+ private routePathStart: i32[]
160
+ private routePathEnd: i32[]
161
+ /** Every static route's path, back to back. */
162
+ private staticPaths: u8[]
124
163
  /**
125
164
  * The methods the table knows, `methodBytes[methodStart[m] .. methodEnd[m])`, compared exactly (RFC 9110 §9.1).
126
165
  * @internal Read by std/net/handler.ts, which matches a path against each known method to build a 405's `Allow` value.
@@ -136,6 +175,12 @@ export class Router {
136
175
  * @internal Read by std/net/handler.ts beside `methodStart`, for the same matches.
137
176
  */
138
177
  methodEnd: i32[]
178
+ /**
179
+ * Per method: its bytes and length packed by `routerMethodKey`, or 0 for one
180
+ * too long to pack. Private and written only by `add`, beside the public
181
+ * arrays it is made from, so a lookup compares one word for a method.
182
+ */
183
+ private methodKey: u64[]
139
184
  /** `match`'s scratch, sized by the constructor: each segment's span, then the pairs found so far. */
140
185
  private segStart: i32[]
141
186
  private segEnd: i32[]
@@ -154,6 +199,11 @@ export class Router {
154
199
  maxParams: i32 = 0
155
200
  private maxRoutes: i32 = 0
156
201
  private maxMethods: i32 = 0
202
+ /** How many routes have no parameter and no `*`, and how many of them `staticSlots` holds. */
203
+ private statics: i32 = 0
204
+ private staticHeld: i32 = 0
205
+ /** The longest static route's path, in bytes: a longer target is not hashed at all. */
206
+ private staticLongest: i32 = 0
157
207
  /** The most segments any route has: a path is split no further than one past it. */
158
208
  private deepest: i32 = 0
159
209
  /** During a match: how many segments the path has, the method's index, and the pairs in `found`. */
@@ -167,7 +217,7 @@ export class Router {
167
217
  private foundCount: i32 = 0
168
218
  /** During a match: whether a route of another method ended at the path's end. */
169
219
  private otherMethod: boolean = false
170
- /** The nodes the last `match` visited: never more than `nodeCount()`, which the tests pin. */
220
+ /** The nodes the last `match` visited: never more than `nodeCount()`, which the tests pin, and none when the exact-path table answered. */
171
221
  private visited: i32 = 0
172
222
 
173
223
  /**
@@ -196,9 +246,15 @@ export class Router {
196
246
  this.routeMethod = []
197
247
  this.endpointNext = []
198
248
  this.routeParams = []
249
+ this.staticSlots = [-1, -1, -1, -1, -1, -1, -1, -1]
250
+ this.routeHash = []
251
+ this.routePathStart = []
252
+ this.routePathEnd = []
253
+ this.staticPaths = []
199
254
  this.methodBytes = []
200
255
  this.methodStart = []
201
256
  this.methodEnd = []
257
+ this.methodKey = []
202
258
  this.segStart = new Array<i32>(limits.maxSegments + 1)
203
259
  this.segEnd = new Array<i32>(limits.maxSegments + 1)
204
260
  this.found = new Array<i32>(2 * limits.maxParams)
@@ -227,14 +283,24 @@ export class Router {
227
283
  */
228
284
  paramCount(route: i32): i32 {
229
285
  if (route < 0 || route >= this.routes()) {
230
- panic(`Router.paramCount: route ${route} is not in a table of ${this.routes()}`)
286
+ routerNoRoute(route, this.routes())
231
287
  }
232
288
  return this.routeParams[route]
233
289
  }
234
290
 
235
291
  /**
236
- * How many nodes the last `match` visited.
237
- * @returns the visit count, never more than `nodeCount()`, which the tests use to pin a lookup's cost
292
+ * How many routes the exact-path table holds: the routes `match` answers
293
+ * without the tree. Every route with no parameter and no `*` is one, but for
294
+ * a route whose slots were all taken, which the tree answers instead.
295
+ * @returns the count, never more than the routes that have no parameter and no `*`
296
+ */
297
+ staticCount(): i32 {
298
+ return this.staticHeld
299
+ }
300
+
301
+ /**
302
+ * How many nodes of the tree the last `match` visited.
303
+ * @returns the visit count, never more than `nodeCount()`, and 0 when the exact-path table answered, which the tests use to pin a lookup's cost
238
304
  */
239
305
  lastVisited(): i32 {
240
306
  return this.visited
@@ -305,6 +371,7 @@ export class Router {
305
371
  this.methodBytes.push(toU8(toI32(method.charCodeAt(k))))
306
372
  }
307
373
  this.methodEnd.push(toI32(this.methodBytes.length))
374
+ this.methodKey.push(routerMethodKey(this.methodBytes, this.methodStart[methodId], methodLength))
308
375
  }
309
376
  node = 0
310
377
  for (let k: i32 = 0; k < count; k++) {
@@ -319,9 +386,64 @@ export class Router {
319
386
  if (count > this.deepest) {
320
387
  this.deepest = count
321
388
  }
389
+ this.routePathStart.push(toI32(this.staticPaths.length))
390
+ let hash: u32 = routerHashOf(methodId)
391
+ if (params === 0) {
392
+ for (let k: i32 = 0; k < length; k++) {
393
+ const c: i32 = toI32(pattern.charCodeAt(k))
394
+ this.staticPaths.push(toU8(c))
395
+ hash = routerHashWith(hash, c)
396
+ }
397
+ }
398
+ this.routePathEnd.push(toI32(this.staticPaths.length))
399
+ this.routeHash.push(hash)
400
+ if (params === 0) {
401
+ this.statics += 1
402
+ if (length > this.staticLongest) {
403
+ this.staticLongest = length
404
+ }
405
+ if (4 * this.statics > toI32(this.staticSlots.length)) {
406
+ this.growStatic()
407
+ } else {
408
+ this.fileStatic(route)
409
+ }
410
+ }
322
411
  return route
323
412
  }
324
413
 
414
+ /** Doubles the exact-path table and files every static route again, in the order they were added. */
415
+ private growStatic(): void {
416
+ const slots: i32 = 2 * toI32(this.staticSlots.length)
417
+ this.staticSlots = new Array<i32>(slots)
418
+ for (let k: i32 = 0; k < slots; k++) {
419
+ this.staticSlots[k] = ROUTER_NONE
420
+ }
421
+ this.staticHeld = 0
422
+ for (let r: i32 = 0; r < this.routes(); r++) {
423
+ if (this.routeParams[r] === 0) {
424
+ this.fileStatic(r)
425
+ }
426
+ }
427
+ }
428
+
429
+ /**
430
+ * Files the static route `route` in the first free slot of the
431
+ * `ROUTER_PROBES` from its hash's, or nowhere when all are taken: the tree
432
+ * holds every route, so one the table lacks is still answered.
433
+ */
434
+ private fileStatic(route: i32): void {
435
+ const mask: i32 = toI32(this.staticSlots.length) - 1
436
+ let slot: i32 = routerSlot(this.routeHash[route], mask)
437
+ for (let probe: i32 = 0; probe < ROUTER_PROBES; probe++) {
438
+ if (this.staticSlots[slot] < 0) {
439
+ this.staticSlots[slot] = route
440
+ this.staticHeld += 1
441
+ return
442
+ }
443
+ slot = (slot + 1) & mask
444
+ }
445
+ }
446
+
325
447
  /**
326
448
  * Splits `pattern`, which starts with `/`, into `patKind`/`patStart`/`patEnd`
327
449
  * and answers how many segments it has, or the code that refuses it.
@@ -445,11 +567,57 @@ export class Router {
445
567
  * @returns the matched route's index, or `ROUTER_NOT_FOUND`, `ROUTER_METHOD_NOT_ALLOWED` or `ROUTER_TOO_MANY_PARAMS`
446
568
  */
447
569
  match(method: u8[], mStart: i32, mEnd: i32, target: u8[], tStart: i32, tEnd: i32, params: i32[]): i32 {
570
+ if (!this.enter(method, mStart, mEnd, target, tStart, tEnd)) {
571
+ return ROUTER_NOT_FOUND
572
+ }
573
+ // A static route's path is never longer than the longest one: a target
574
+ // past that, query and all, is the tree's without a byte of it read here.
575
+ if (tEnd - tStart <= this.staticLongest) {
576
+ const route: i32 = this.staticRoute(target, tStart, tEnd)
577
+ if (route !== ROUTER_NONE) {
578
+ return route
579
+ }
580
+ // The pass that hashed the path split it too, so the tree reads no byte again.
581
+ return this.answer(target, params)
582
+ }
583
+ return this.descend(target, tStart, tEnd, params)
584
+ }
585
+
586
+ /**
587
+ * What `match` answers, found by the tree alone: the exact-path table is not
588
+ * read. The two agree on every method and target, in the route or code they
589
+ * answer, in `params` and in where the path ends; this one is the definition
590
+ * and exists so that a test can hold `match` to it.
591
+ * @param method the buffer holding the request's method
592
+ * @param mStart where the method starts in `method`
593
+ * @param mEnd where the method ends in `method`
594
+ * @param target the buffer holding the request target, which may be the same buffer as `method`
595
+ * @param tStart where the target starts in `target`
596
+ * @param tEnd where the target ends in `target`, the query included
597
+ * @param params the caller's scratch for the parameter pairs, two slots for each pair
598
+ * @returns the matched route's index, or `ROUTER_NOT_FOUND`, `ROUTER_METHOD_NOT_ALLOWED` or `ROUTER_TOO_MANY_PARAMS`
599
+ */
600
+ matchTree(method: u8[], mStart: i32, mEnd: i32, target: u8[], tStart: i32, tEnd: i32, params: i32[]): i32 {
601
+ if (!this.enter(method, mStart, mEnd, target, tStart, tEnd)) {
602
+ return ROUTER_NOT_FOUND
603
+ }
604
+ return this.descend(target, tStart, tEnd, params)
605
+ }
606
+
607
+ /**
608
+ * The start of every lookup: the range checks, and `wanted` set to the
609
+ * method's index, or -1 for one the table has never seen. False for a target
610
+ * that is not `/`-rooted, which is `ROUTER_NOT_FOUND` with `pathEnd` set.
611
+ */
612
+ private enter(method: u8[], mStart: i32, mEnd: i32, target: u8[], tStart: i32, tEnd: i32): boolean {
448
613
  if (mStart < 0 || mEnd < mStart || mEnd > toI32(method.length)) {
449
- panic(`Router.match: the method range ${mStart} to ${mEnd} is outside its ${method.length} bytes`)
614
+ routerOutside("method", mStart, mEnd, toI32(method.length))
450
615
  }
451
616
  if (tStart < 0 || tEnd < tStart || tEnd > toI32(target.length)) {
452
- panic(`Router.match: the target range ${tStart} to ${tEnd} is outside its ${target.length} bytes`)
617
+ routerOutside("target", tStart, tEnd, toI32(target.length))
618
+ // Not reached: the helper panics. The return is what tells the prover
619
+ // that `tStart .. tEnd` is inside `target` from here on.
620
+ return false
453
621
  }
454
622
  this.visited = 0
455
623
  if (tStart === tEnd || toI32(target[tStart]) !== 47) {
@@ -458,6 +626,43 @@ export class Router {
458
626
  end += 1
459
627
  }
460
628
  this.pathEnd = end
629
+ return false
630
+ }
631
+ const methodLength: i32 = mEnd - mStart
632
+ const key: u64 = routerMethodKey(method, mStart, methodLength)
633
+ if (key !== toU64(0)) {
634
+ for (let m: i32 = 0; m < toI32(this.methodKey.length); m++) {
635
+ if (this.methodKey[m] === key) {
636
+ this.wanted = m
637
+ return true
638
+ }
639
+ }
640
+ this.wanted = -1
641
+ return true
642
+ }
643
+ // A method too long for a key is compared byte by byte with each known one.
644
+ for (let m: i32 = 0; m < toI32(this.methodStart.length); m++) {
645
+ const at: i32 = this.methodStart[m]
646
+ // Checked, the compare's ranges included: `methodBytes`, `methodStart`
647
+ // and `methodEnd` are public, so nothing here can rely on `add` being
648
+ // the only writer.
649
+ if (
650
+ this.methodEnd[m] - at === methodLength &&
651
+ routerSameBytes(this.methodBytes, at, method, mStart, methodLength)
652
+ ) {
653
+ this.wanted = m
654
+ return true
655
+ }
656
+ }
657
+ this.wanted = -1
658
+ return true
659
+ }
660
+
661
+ /** The tree's answer for a target `enter` took and no scan has split: the split, then `answer`. */
662
+ private descend(target: u8[], tStart: i32, tEnd: i32, params: i32[]): i32 {
663
+ // `enter` took this range, so this is never true; it is here because a
664
+ // proof does not cross a call, and the reads below need this one.
665
+ if (tStart < 0 || tEnd > toI32(target.length)) {
461
666
  return ROUTER_NOT_FOUND
462
667
  }
463
668
  // One pass over the path: where it ends, and each segment's span up to one
@@ -485,20 +690,13 @@ export class Router {
485
690
  this.segStart[segments] = start
486
691
  this.segEnd[segments] = end
487
692
  }
488
- segments += 1
489
- this.segments = segments
693
+ this.segments = segments + 1
490
694
  this.pathEnd = end
491
- this.wanted = -1
492
- const methodLength: i32 = mEnd - mStart
493
- for (let m: i32 = 0; m < toI32(this.methodStart.length) && this.wanted < 0; m++) {
494
- const at: i32 = this.methodStart[m]
495
- if (
496
- this.methodEnd[m] - at === methodLength &&
497
- routerSameBytes(this.methodBytes, at, method, mStart, methodLength)
498
- ) {
499
- this.wanted = m
500
- }
501
- }
695
+ return this.answer(target, params)
696
+ }
697
+
698
+ /** The tree's answer for a path already split into `segStart`/`segEnd`: the walk and the pairs. */
699
+ private answer(target: u8[], params: i32[]): i32 {
502
700
  this.foundCount = 0
503
701
  this.otherMethod = false
504
702
  const route: i32 = this.walk(target, toI32(0), toI32(0))
@@ -515,6 +713,77 @@ export class Router {
515
713
  return route
516
714
  }
517
715
 
716
+ /**
717
+ * The static route of the wanted method whose path is `target` from `tStart`
718
+ * to its first `?` or `tEnd`, or -1 when the exact-path table holds none. On
719
+ * a route `pathEnd` is set; it has no pairs, so `params` is not needed.
720
+ * Either way the path is left split, as `descend` splits it, for `answer`;
721
+ * a range outside `target`, which `enter` never lets through, is
722
+ * `ROUTER_NOT_FOUND` before any split.
723
+ *
724
+ * `match` calls this only for a target no longer than the longest static
725
+ * route. One pass hashes and splits the path; then at most `ROUTER_PROBES` slots are
726
+ * read, and a slot's path is compared only when its method and length
727
+ * agree. The hash picks the slot and decides nothing else: a route is
728
+ * answered for its method and its bytes. The cost is bounded by the longest static route's length,
729
+ * whatever the target and however many routes the table holds.
730
+ */
731
+ private staticRoute(target: u8[], tStart: i32, tEnd: i32): i32 {
732
+ // Never true, as in `descend`: the guard is the proof the loop's reads
733
+ // need. It answers as `descend`'s does, so no split is left for `answer`.
734
+ if (tStart < 0 || tEnd > toI32(target.length)) {
735
+ return ROUTER_NOT_FOUND
736
+ }
737
+ // The first byte is the `/` `enter` found; each later one is hashed, and
738
+ // a `/` among them also closes a segment, as in `descend`.
739
+ let hash: u32 = routerHashWith(routerHashOf(this.wanted), 47)
740
+ const keep: i32 = this.deepest + 1
741
+ let segments: i32 = 0
742
+ let start: i32 = tStart + 1
743
+ let end: i32 = start
744
+ while (end < tEnd) {
745
+ const c: i32 = toI32(target[end])
746
+ if (c === 63) {
747
+ break
748
+ }
749
+ if (c === 47) {
750
+ if (segments < keep) {
751
+ this.segStart[segments] = start
752
+ this.segEnd[segments] = end
753
+ }
754
+ segments += 1
755
+ start = end + 1
756
+ }
757
+ hash = routerHashWith(hash, c)
758
+ end += 1
759
+ }
760
+ if (segments < keep) {
761
+ this.segStart[segments] = start
762
+ this.segEnd[segments] = end
763
+ }
764
+ this.segments = segments + 1
765
+ this.pathEnd = end
766
+ const length: i32 = end - tStart
767
+ const mask: i32 = toI32(this.staticSlots.length) - 1
768
+ let slot: i32 = routerSlot(hash, mask)
769
+ for (let probe: i32 = 0; probe < ROUTER_PROBES; probe++) {
770
+ const route: i32 = this.staticSlots[slot]
771
+ if (route < 0) {
772
+ return ROUTER_NONE
773
+ }
774
+ const at: i32 = this.routePathStart[route]
775
+ if (
776
+ this.routeMethod[route] === this.wanted &&
777
+ this.routePathEnd[route] - at === length &&
778
+ routerSameBytes(this.staticPaths, at, target, tStart, length)
779
+ ) {
780
+ return route
781
+ }
782
+ slot = (slot + 1) & mask
783
+ }
784
+ return ROUTER_NONE
785
+ }
786
+
518
787
  /**
519
788
  * The route below `node`, which has consumed `depth` segments, for the
520
789
  * wanted method, or -1. Literal, then parameter, then `*`: the first branch
@@ -522,7 +791,8 @@ export class Router {
522
791
  * pairs it pushed popped.
523
792
  */
524
793
  private walk(target: u8[], node: i32, depth: i32): i32 {
525
- this.visited += 1
794
+ // Never more than the nodes, so the count needs no overflow check.
795
+ this.visited = wrappingAdd(this.visited, toI32(1))
526
796
  if (depth === this.segments) {
527
797
  return this.endpointFor(node)
528
798
  }
@@ -554,7 +824,7 @@ export class Router {
554
824
  }
555
825
  const wild: i32 = this.wildChild[node]
556
826
  if (wild >= 0) {
557
- this.visited += 1
827
+ this.visited = wrappingAdd(this.visited, toI32(1))
558
828
  const route: i32 = this.endpointFor(wild)
559
829
  if (route >= 0) {
560
830
  const top: i32 = this.foundCount
@@ -631,6 +901,71 @@ export const routerDecode = (target: u8[], start: i32, end: i32, out: u8[]): i32
631
901
  return length
632
902
  }
633
903
 
904
+ /**
905
+ * Where the hash the exact-path table files a static route under begins: at
906
+ * the method's index, so that two methods of one path do not share a slot.
907
+ */
908
+ const routerHashOf = (method: i32): u32 => toU32(method + 1)
909
+
910
+ /**
911
+ * `bytes[at .. at + length)` packed into one word, its first byte lowest and
912
+ * its length in the top byte, so two methods of up to 7 bytes have the same
913
+ * key exactly when they have the same bytes. 0, which no packed method is,
914
+ * for a length outside 1 to 7 or a range outside `bytes`.
915
+ */
916
+ const routerMethodKey = (bytes: u8[], at: i32, length: i32): u64 => {
917
+ if (length < 1 || length > 7 || at < 0 || length > toI32(bytes.length) - at) {
918
+ return toU64(0)
919
+ }
920
+ // The test above puts `[at, at + length)` inside `bytes`, a range the
921
+ // prover cannot follow through `stop`, so each read skips its check and
922
+ // the cursor its overflow test: `stop` is at most `bytes.length`, an i32.
923
+ let key: u64 = toU64(length) << toU64(56)
924
+ let shift: u64 = toU64(0)
925
+ const stop: i32 = wrappingAdd(at, length)
926
+ for (let i: i32 = at; i < stop; i = wrappingAdd(i, toI32(1))) {
927
+ key = key | (toU64(uncheckedGet(bytes, i)) << shift)
928
+ shift = shift + toU64(8)
929
+ }
930
+ return key
931
+ }
932
+
933
+ /**
934
+ * `hash` with the path byte `byte` taken in, as FNV-1a takes one: an exclusive
935
+ * or and a product with its 32-bit prime. `u32` arithmetic wraps.
936
+ */
937
+ const routerHashWith = (hash: u32, byte: i32): u32 => {
938
+ const prime: u32 = 16777619
939
+ return (hash ^ toU32(byte)) * prime
940
+ }
941
+
942
+ /**
943
+ * The slot of a table of `mask + 1`, a power of two, that `hash` names: its
944
+ * product with 2^32 over the golden ratio, the high bits folded into the low
945
+ * ones the mask keeps.
946
+ */
947
+ const routerSlot = (hash: u32, mask: i32): i32 => {
948
+ const spread: u32 = 2654435769
949
+ const mixed: u32 = hash * spread
950
+ return toI32((mixed ^ (mixed >>> 15)) & toU32(mask))
951
+ }
952
+
953
+ /**
954
+ * Panics for a `what` range `start` to `end` outside its `length` bytes. Apart
955
+ * from `enter`, so that a lookup holds no string of its own.
956
+ */
957
+ const routerOutside = (what: string, start: i32, end: i32, length: i32): void => {
958
+ panic(`Router.match: the ${what} range ${start} to ${end} is outside its ${length} bytes`)
959
+ }
960
+
961
+ /**
962
+ * Panics for a `route` that is not one of the table's `routes`. Apart from
963
+ * `paramCount`, which a server calls per request, for the reason `routerOutside` is.
964
+ */
965
+ const routerNoRoute = (route: i32, routes: i32): void => {
966
+ panic(`Router.paramCount: route ${route} is not in a table of ${routes}`)
967
+ }
968
+
634
969
  /** Panics, naming `RouterLimits.<field>`, unless `value` is in `low .. high`. */
635
970
  const routerLimit = (field: string, value: i32, low: i32, high: i32): void => {
636
971
  if (value < low || value > high) {
@@ -713,21 +1048,33 @@ const routerLabelIsText = (bytes: u8[], at: i32, text: string, from: i32, length
713
1048
  return true
714
1049
  }
715
1050
 
716
- /** Whether `a[at .. at + length)` and `b[from .. from + length)` hold the same bytes; a range outside its array does not. */
1051
+ /**
1052
+ * Whether `a[at .. at + length)` and `b[from .. from + length)` hold the same
1053
+ * bytes; a negative length, or a range outside its array, does not. The
1054
+ * ranges are checked once, here, and the bytes are then read without a check
1055
+ * per byte.
1056
+ */
717
1057
  const routerSameBytes = (a: u8[], at: i32, b: u8[], from: i32, length: i32): boolean => {
718
- // Two cursors, so no index is a sum that must be checked for overflow. `stop`
719
- // is one sum, but every caller passes a label's or a method's offset and
720
- // length inside an array whose length is an i32, so it cannot overflow; a
721
- // range past either end fails the per-byte checks and answers false.
722
- const stop: i32 = at + length
1058
+ if (at < 0 || from < 0 || length > toI32(a.length) - at || length > toI32(b.length) - from) {
1059
+ return false
1060
+ }
1061
+ // Two cursors, each stepped without an overflow check: `at` and `from` are
1062
+ // not negative and `length` is at most what is left of each array after
1063
+ // them, so neither passes its array's length, an i32. A negative `length`
1064
+ // puts `stop` below `at` (no overflow, `at` being at least 0), so the loop
1065
+ // never runs and the closing test answers false: a separate `length < 0`
1066
+ // test up front measured 5 to 47 Ir more per match.
1067
+ const stop: i32 = wrappingAdd(at, length)
723
1068
  let j: i32 = from
724
- for (let i: i32 = at; i < stop; i++) {
725
- if (i < 0 || i >= toI32(a.length) || j < 0 || j >= toI32(b.length) || a[i] !== b[j]) {
1069
+ let i: i32 = at
1070
+ while (i < stop) {
1071
+ if (uncheckedGet(a, i) !== uncheckedGet(b, j)) {
726
1072
  return false
727
1073
  }
728
- j++
1074
+ i = wrappingAdd(i, toI32(1))
1075
+ j = wrappingAdd(j, toI32(1))
729
1076
  }
730
- return true
1077
+ return i === stop
731
1078
  }
732
1079
 
733
1080
  /** The value of the hex digit `c`, in either case, or -1. */