nish 0.44.0 → 0.46.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 (119) hide show
  1. package/README.md +1 -1
  2. package/docs/AI.md +18 -16
  3. package/docs/INSTALL.md +3 -1
  4. package/package.json +5 -5
  5. package/runtime/core/numbers.mjs +3 -1
  6. package/runtime/core/strings.mjs +90 -38
  7. package/runtime/core.d.ts +8 -0
  8. package/runtime/core.mjs +16 -0
  9. package/runtime/nish.d.ts +88 -5
  10. package/runtime/nish.h +10 -0
  11. package/runtime/nish.mjs +5 -0
  12. package/runtime/runtime-host.c +15 -0
  13. package/runtime/runtime-net.c +223 -4
  14. package/runtime/runtime-os.c +15 -1
  15. package/runtime/runtime-uring.c +291 -31
  16. package/runtime/sqlite/nish-sqlite.c +301 -128
  17. package/std/README.md +15 -15
  18. package/std/array.ts +10 -2
  19. package/std/bytes.ts +239 -26
  20. package/std/cli-args.ts +1072 -606
  21. package/std/cli-flags.ts +180 -98
  22. package/std/cli.ts +21 -13
  23. package/std/collections.ts +245 -127
  24. package/std/compress/checksum.ts +32 -37
  25. package/std/compress/deflate.ts +1102 -542
  26. package/std/compress/gzip.ts +290 -145
  27. package/std/compress/zlib.ts +57 -21
  28. package/std/crypto/aes.ts +153 -75
  29. package/std/crypto/base64url.ts +84 -61
  30. package/std/crypto/bcrypt-batch.ts +164 -90
  31. package/std/crypto/bcrypt.ts +185 -107
  32. package/std/crypto/chacha20poly1305.ts +84 -68
  33. package/std/crypto/ct.ts +9 -7
  34. package/std/crypto/ed25519.ts +64 -27
  35. package/std/crypto/hkdf.ts +105 -63
  36. package/std/crypto/hmac.ts +95 -89
  37. package/std/crypto/jwt.ts +248 -126
  38. package/std/crypto/p256.ts +758 -444
  39. package/std/crypto/p384.ts +20 -13
  40. package/std/crypto/pki-roots.ts +95 -38
  41. package/std/crypto/pki.ts +164 -60
  42. package/std/crypto/rsa.ts +266 -223
  43. package/std/crypto/scram.ts +3 -4
  44. package/std/crypto/sha1.ts +107 -65
  45. package/std/crypto/sha256.ts +502 -422
  46. package/std/crypto/sha512.ts +204 -135
  47. package/std/crypto/x509-random.ts +9 -3
  48. package/std/crypto/x509.ts +331 -171
  49. package/std/json-equal.ts +35 -19
  50. package/std/json-format.ts +215 -108
  51. package/std/json-number.ts +126 -72
  52. package/std/json-pointer.ts +42 -13
  53. package/std/json-tokenizer.ts +498 -307
  54. package/std/json-typed.ts +115 -36
  55. package/std/json-value.ts +341 -152
  56. package/std/json.ts +300 -158
  57. package/std/net/buffer-pool.ts +17 -7
  58. package/std/net/dns-config.ts +427 -236
  59. package/std/net/dns-message.ts +114 -47
  60. package/std/net/dns-resolver.ts +736 -237
  61. package/std/net/dns.ts +205 -52
  62. package/std/net/fetch.ts +320 -184
  63. package/std/net/grpc.ts +948 -0
  64. package/std/net/handler.ts +1315 -222
  65. package/std/net/hpack.ts +395 -180
  66. package/std/net/http-compress.ts +502 -65
  67. package/std/net/http-fields.ts +85 -49
  68. package/std/net/http-server.ts +1764 -602
  69. package/std/net/http1-client-codec.ts +372 -161
  70. package/std/net/http1-codec.ts +665 -331
  71. package/std/net/http1-server.ts +660 -278
  72. package/std/net/http2-cleartext.ts +165 -95
  73. package/std/net/http2-frame.ts +232 -89
  74. package/std/net/http2-tls.ts +42 -12
  75. package/std/net/http2.ts +327 -101
  76. package/std/net/http3-frame.ts +68 -33
  77. package/std/net/http3-server.ts +647 -192
  78. package/std/net/http3.ts +667 -189
  79. package/std/net/oidc.ts +63 -24
  80. package/std/net/outbound.ts +207 -142
  81. package/std/net/postgres-async.ts +1165 -443
  82. package/std/net/postgres-carrier.ts +575 -372
  83. package/std/net/postgres.ts +1459 -559
  84. package/std/net/prefork.ts +254 -98
  85. package/std/net/protobuf.ts +1017 -0
  86. package/std/net/qpack.ts +143 -92
  87. package/std/net/quic-conn-ack.ts +50 -18
  88. package/std/net/quic-conn-cid.ts +66 -26
  89. package/std/net/quic-conn-params.ts +13 -7
  90. package/std/net/quic-datagram.ts +90 -27
  91. package/std/net/quic-frame.ts +225 -124
  92. package/std/net/quic-listener.ts +197 -89
  93. package/std/net/quic-packet.ts +289 -163
  94. package/std/net/quic-recovery.ts +279 -128
  95. package/std/net/quic-stream.ts +466 -167
  96. package/std/net/quic.ts +568 -257
  97. package/std/net/router.ts +450 -99
  98. package/std/net/serve.ts +651 -232
  99. package/std/net/static.ts +1458 -0
  100. package/std/net/tls/client.ts +140 -45
  101. package/std/net/tls/codec.ts +510 -260
  102. package/std/net/tls/record-client.ts +268 -76
  103. package/std/net/tls/record-server.ts +236 -79
  104. package/std/net/tls/record.ts +173 -47
  105. package/std/net/tls/schedule.ts +58 -39
  106. package/std/net/tls/ticket.ts +295 -103
  107. package/std/net/tls/verify.ts +1 -1
  108. package/std/net/tls-tcp.ts +218 -75
  109. package/std/net/tls.ts +260 -152
  110. package/std/net/upstream.ts +283 -86
  111. package/std/net/url.ts +277 -102
  112. package/std/net/websocket.ts +370 -186
  113. package/std/net/webtransport.ts +755 -436
  114. package/std/regex.ts +1659 -829
  115. package/std/sqlite.ts +171 -0
  116. package/std/sync/ring.ts +320 -156
  117. package/std/sync/shm.ts +53 -6
  118. package/std/text.ts +205 -124
  119. package/std/threads.ts +22 -2
@@ -25,10 +25,12 @@
25
25
  * full 32-bit hash of each. The bucket table `slots` is open addressing with
26
26
  * linear probing, and a bucket is one `u32`:
27
27
  *
28
- * bits 31..24 the top eight bits of the key's hash (the fingerprint)
29
- * bits 23..0 the entry's index plus one
28
+ * the high bits the key's hash above the table's mask (the fingerprint)
29
+ * the mask bits the entry's index plus one
30
30
  *
31
- * An empty bucket is 0. A deleted entry's bucket is `0x01000000` (16777216),
31
+ * The index field is as wide as the mask, `slots.length - 1`: a table holds
32
+ * at most three entries per four buckets, so every index plus one fits. An
33
+ * empty bucket is 0. A deleted entry's bucket is the mask plus one,
32
34
  * fingerprint 1 and index field 0, which no live entry has; its stored hash is
33
35
  * set to 0, which is why a computed hash of 0 is moved to 1. A probe reads the
34
36
  * entry arrays only when a bucket's fingerprint matches, and compares the
@@ -53,16 +55,6 @@
53
55
  * outside it.
54
56
  */
55
57
 
56
- /**
57
- * The most entries a table holds, dead ones included until a rebuild: 2^24 - 1,
58
- * what the 24-bit index field holds as an index plus one. Node's own `Map` and
59
- * `Set` hold one more, 2^24, and throw on the next insert.
60
- */
61
- const INDEX_CAP: i32 = 16777215
62
-
63
- /** The first bucket count. A power of two, as every bucket count is. */
64
- const INITIAL_SLOTS: i32 = 8
65
-
66
58
  /**
67
59
  * The key's hash, never 0: SipHash-1-3 of a string's bytes under the process's
68
60
  * random key, folded to 32 bits (docs/security/json.md, JSON-13); under a 2nd,
@@ -89,35 +81,54 @@ const sameKey = <K>(a: K, b: K): boolean => a === b
89
81
  /** The first bucket for `h`: its low bits, folded with the high half. */
90
82
  const homeBucket = (h: u32, mask: i32): i32 => toI32(h ^ (h >>> 16)) & mask
91
83
 
92
- /** The bucket word for entry `index` of hash `h`. */
93
- const slotWord = (h: u32, index: i32): u32 => ((h >>> 24) << 24) | toU32(index + 1)
84
+ /**
85
+ * The bucket word for entry `index` of hash `h` in a table of `mask + 1`
86
+ * buckets: the index plus one in the bits the mask covers, and the hash's bits
87
+ * above them as a fingerprint. The load bound keeps every index below the
88
+ * bucket count, so the index never reaches the fingerprint.
89
+ */
90
+ const slotWord = (h: u32, mask: i32, index: i32): u32 => (h & ~toU32(mask)) | (toU32(index) + 1)
91
+
92
+ /**
93
+ * A deleted entry's bucket: a fingerprint bit set and an index field of 0, so
94
+ * a probe walks past it, its entry index being -1.
95
+ */
96
+ const tombstone = (mask: i32): u32 => toU32(mask) + 1
94
97
 
95
98
  /** A probe that found entry `index`, pointed at by `bucket`. Never negative. */
96
99
  const foundAt = (bucket: i32, index: i32): i64 => (toI64(bucket) << 32) | toI64(index)
97
100
 
98
101
  /** A probe that stopped at the empty `bucket` for hash `h`. Always negative. */
99
- const absentAt = (bucket: i32, h: u32): i64 => toI64(-1) - ((toI64(bucket) << 32) | toI64(h))
102
+ const absentAt = (bucket: i32, h: u32): i64 => ~((toI64(bucket) << 32) | toI64(h))
100
103
 
101
104
  /**
102
105
  * The one probe, for both classes: linear probing from `h`'s home bucket to
103
106
  * the bucket pointing at `key`, or to the first empty one. A tombstone is
104
107
  * walked past — its index field is 0, so its entry index is -1 and the range
105
108
  * test turns it away — and the load bound keeps a quarter of the buckets
106
- * empty, so the walk ends.
109
+ * empty, so the walk ends. A bucket past the end, which a mask as wide as
110
+ * the table never gives, answers absent there, which `insertAt` answers with
111
+ * a rebuild.
107
112
  */
108
113
  const probeTable = <K>(slots: u32[], mask: i32, hashes: u32[], keys: K[], key: K): i64 => {
109
114
  const h = hashKey(key)
110
- const fingerprint = h >>> 24
111
- let bucket = homeBucket(h, mask)
115
+ const low = toU32(mask)
116
+ const fingerprint = h & ~low
117
+ let bucket = toU32(homeBucket(h, mask))
112
118
  // The length is read in the condition rather than once: the key compare is
113
- // a call, and a call ends every length fact the bounds proof holds.
114
- while (bucket >= 0 && bucket < toI32(slots.length)) {
115
- const word = slots[bucket]
119
+ // a call, and a call ends every length fact the bounds proof holds. The
120
+ // bucket is a `u32`, whose `+` wraps, so the call leaves nothing it needs
121
+ // proven. The load bound keeps a quarter of the buckets empty, so the walk
122
+ // ends at one; the test on `b` keeps a table with none from indexing past
123
+ // its end.
124
+ let b = toI32(bucket)
125
+ while (b >= 0 && b < toI32(slots.length)) {
126
+ const word = slots[b]
116
127
  if (word === 0) {
117
- return absentAt(bucket, h)
128
+ return absentAt(b, h)
118
129
  }
119
- if (word >>> 24 === fingerprint) {
120
- const at = toI32(word & 16777215) - 1
130
+ if ((word & ~low) === fingerprint) {
131
+ const at = toI32((word & low) - 1)
121
132
  if (
122
133
  at >= 0 &&
123
134
  at < toI32(hashes.length) &&
@@ -125,28 +136,33 @@ const probeTable = <K>(slots: u32[], mask: i32, hashes: u32[], keys: K[], key: K
125
136
  at < toI32(keys.length) &&
126
137
  sameKey(keys[at], key)
127
138
  ) {
128
- return foundAt(bucket, at)
139
+ return foundAt(b, at)
129
140
  }
130
141
  }
131
- bucket = (bucket + 1) & mask
142
+ bucket = (bucket + 1) & low
143
+ b = toI32(bucket)
132
144
  }
133
- panic("collections: a probe ran out of buckets")
145
+ return absentAt(toI32(slots.length), h)
134
146
  }
135
147
 
136
148
  /**
137
149
  * Point the first empty bucket from `h`'s home at entry `index`. A rebuild's
138
150
  * re-filing: every key is already known to be distinct, so no key is
139
- * compared, and the stored hash is all it needs.
151
+ * compared, and the stored hash is all it needs. The load bound leaves a
152
+ * quarter of the buckets empty, so one pass finds one.
140
153
  */
141
154
  const fileEntry = (slots: u32[], mask: i32, h: u32, index: i32): void => {
142
- const word = slotWord(h, index)
143
- let bucket = homeBucket(h, mask)
144
- while (bucket >= 0 && bucket < toI32(slots.length)) {
145
- if (slots[bucket] === 0) {
146
- slots[bucket] = word
155
+ const word = slotWord(h, mask, index)
156
+ const low = toU32(mask)
157
+ let bucket = toU32(homeBucket(h, mask))
158
+ let b = toI32(bucket)
159
+ while (b >= 0 && b < toI32(slots.length)) {
160
+ if (slots[b] === 0) {
161
+ slots[b] = word
147
162
  return
148
163
  }
149
- bucket = (bucket + 1) & mask
164
+ bucket = (bucket + 1) & low
165
+ b = toI32(bucket)
150
166
  }
151
167
  }
152
168
 
@@ -160,7 +176,7 @@ const compactEntries = <T>(items: T[], hashes: u32[]): void => {
160
176
  to++
161
177
  }
162
178
  }
163
- while (toI32(items.length) > to) {
179
+ while (toI32(items.length) > to && items.length > 0) {
164
180
  items.pop()
165
181
  }
166
182
  }
@@ -176,25 +192,49 @@ const compactHashes = (hashes: u32[]): void => {
176
192
  to++
177
193
  }
178
194
  }
179
- while (toI32(hashes.length) > to) {
195
+ while (toI32(hashes.length) > to && hashes.length > 0) {
180
196
  hashes.pop()
181
197
  }
182
198
  }
183
199
 
184
200
  /**
185
- * The bucket table after a rebuild of `live` entries out of `used`. More than
186
- * half dead compacts at the same size and clears the table in place — a new
187
- * array would leave the old one in the arena, once per compaction, which a
188
- * table that churns forever cannot afford (§6.1). Otherwise it doubles, and
189
- * the table never shrinks.
201
+ * The bucket table after a rebuild of `live` entries out of `used`, cleared.
202
+ * More than half dead compacts at the same size and clears the table in place
203
+ * — a new array would leave the old one in the arena, once per compaction,
204
+ * which a table that churns forever cannot afford (§6.1). Otherwise it
205
+ * doubles, and the table never shrinks.
190
206
  */
191
207
  const rebuiltSlots = (slots: u32[], live: i32, used: i32): u32[] => {
192
- const n = toI32(slots.length)
193
- if (live * 2 < used) {
208
+ if (toI64(live) * 2 < toI64(used)) {
194
209
  clearSlots(slots)
195
210
  return slots
196
211
  }
197
- return new Array<u32>(n * 2)
212
+ const n = toI32(slots.length)
213
+ if (n >= 0 && n < 32768) {
214
+ // A `u16` length is in range by its type; a larger table grows in place.
215
+ return new Array<u32>(toU16(n * 2))
216
+ }
217
+ widenSlots(slots, 2)
218
+ clearSlots(slots)
219
+ return slots
220
+ }
221
+
222
+ /**
223
+ * Grow `slots` in place to `times` buckets for each it holds; the caller
224
+ * clears or re-files them. Only a table past 2^15 buckets grows this way; a
225
+ * smaller one is allocated fresh, whose `u16` length needs no check. It grows by
226
+ * `push`, so a table past the largest array, 2^31 - 1 elements, stops the
227
+ * program as out of memory, as any allocation that size does: that is a `Map`
228
+ * or `Set` of more than 2^29 entries. (Node's hold 2^24 and throw on the next
229
+ * insert.)
230
+ */
231
+ const widenSlots = (slots: u32[], times: i32): void => {
232
+ const n = toI32(slots.length)
233
+ for (let k: i32 = 1; k < times; k++) {
234
+ for (let i: i32 = 0; i < n; i++) {
235
+ slots.push(0)
236
+ }
237
+ }
198
238
  }
199
239
 
200
240
  /**
@@ -217,11 +257,11 @@ const refile = (slots: u32[], hashes: u32[]): void => {
217
257
  * tombstone and the entry's stored hash 0. The key and value stay in the entry
218
258
  * until a rebuild compacts them away (§6.1).
219
259
  */
220
- const killEntry = (slots: u32[], hashes: u32[], found: i64): void => {
260
+ const killEntry = (slots: u32[], mask: i32, hashes: u32[], found: i64): void => {
221
261
  const at = toI32(found)
222
262
  const bucket = toI32(found >> 32)
223
263
  if (bucket >= 0 && bucket < toI32(slots.length)) {
224
- slots[bucket] = 16777216
264
+ slots[bucket] = tombstone(mask)
225
265
  }
226
266
  if (at >= 0 && at < toI32(hashes.length)) {
227
267
  hashes[at] = 0
@@ -264,15 +304,21 @@ const truncate = <T>(items: T[]): void => {
264
304
 
265
305
  /**
266
306
  * Point bucket `bucket`, the empty one an absent probe stopped at, at the entry
267
- * just appended as index `used - 1`; or, when a rebuild made room first and
268
- * moved the buckets (`bucket` is -1), file it from its hash.
307
+ * just appended to `hashes`, and answer true; or answer false, and file
308
+ * nothing, when the entry needs a rebuild first: the bucket is past the end,
309
+ * or the entries, dead ones included, are past the load bound of three
310
+ * quarters of the buckets.
269
311
  */
270
- const fileAppended = (slots: u32[], mask: i32, bucket: i32, h: u32, used: i32): void => {
271
- if (bucket >= 0 && bucket < toI32(slots.length)) {
272
- slots[bucket] = slotWord(h, used - 1)
273
- } else {
274
- fileEntry(slots, mask, h, used - 1)
312
+ const fileAppended = (slots: u32[], mask: i32, bucket: i32, hashes: u32[]): boolean => {
313
+ const used = toI32(hashes.length)
314
+ const size = toI32(slots.length)
315
+ if (toI64(used) * 4 > toI64(size) * 3 || bucket < 0 || bucket >= size) {
316
+ return false
275
317
  }
318
+ if (used > 0) {
319
+ slots[bucket] = slotWord(hashes[used - 1], mask, used - 1)
320
+ }
321
+ return true
276
322
  }
277
323
 
278
324
  /**
@@ -299,11 +345,11 @@ export class Map<K, V> {
299
345
  * a rebuild doubles rather than compacts, and `clear` marks entries dead
300
346
  * rather than truncating, so no entry moves under a walk's cursor (§6.2).
301
347
  */
302
- private walks: i32 = 0
348
+ private walks: u32 = 0
303
349
 
304
350
  /** An empty map. */
305
351
  constructor() {
306
- this.slots = new Array<u32>(INITIAL_SLOTS)
352
+ this.slots = new Array<u32>(8)
307
353
  this.entryKeys = []
308
354
  this.entryValues = []
309
355
  this.entryHashes = []
@@ -360,9 +406,8 @@ export class Map<K, V> {
360
406
  if (found < 0) {
361
407
  return false
362
408
  }
363
- killEntry(this.slots, this.entryHashes, found)
364
- this.live = this.live - 1
365
- this.size = this.size - 1
409
+ killEntry(this.slots, this.mask, this.entryHashes, found)
410
+ this.dropOne()
366
411
  return true
367
412
  }
368
413
 
@@ -394,7 +439,7 @@ export class Map<K, V> {
394
439
  * src/emit-map.ts.
395
440
  */
396
441
  walkOpen(): void {
397
- this.walks = this.walks + 1
442
+ this.walks = this.walks + 1 // a `u32`: no program nests 2^32 walks
398
443
  }
399
444
 
400
445
  /** @internal Called by name by the code the compiler writes for a `Map`, src/emit-map.ts. */
@@ -413,11 +458,11 @@ export class Map<K, V> {
413
458
  * @internal Called by name by the code the compiler writes for a `Map`,
414
459
  * src/emit-map.ts.
415
460
  */
416
- keyAt(index: i32): K {
417
- if (index < 0 || index >= toI32(this.entryKeys.length)) {
418
- panic("Map: no entry at this index")
461
+ keyAt(index: i32, none: K): K {
462
+ if (index >= 0 && index < toI32(this.entryKeys.length)) {
463
+ return this.entryKeys[index]
419
464
  }
420
- return this.entryKeys[index]
465
+ return none
421
466
  }
422
467
 
423
468
  /**
@@ -426,11 +471,11 @@ export class Map<K, V> {
426
471
  * @internal Called by name by the code the compiler writes for a `Map`,
427
472
  * src/emit-map.ts.
428
473
  */
429
- valueAt(index: i32): V {
430
- if (index < 0 || index >= toI32(this.entryValues.length)) {
431
- panic("Map: no entry at this index")
474
+ valueAt(index: i32, none: V): V {
475
+ if (index >= 0 && index < toI32(this.entryValues.length)) {
476
+ return this.entryValues[index]
432
477
  }
433
- return this.entryValues[index]
478
+ return none
434
479
  }
435
480
 
436
481
  /**
@@ -454,29 +499,42 @@ export class Map<K, V> {
454
499
  */
455
500
  insertAt(absent: i64, key: K, value: V): void {
456
501
  const packed = -1 - absent
457
- let bucket = toI32(packed >> 32)
458
- const h = toU32(packed)
459
- if (toI32(this.entryKeys.length) >= INDEX_CAP) {
460
- // Dead entries hold the cap: compact them away, then find the bucket
461
- // again. A walk defers compaction, so under one the cap is full.
462
- if (this.live >= INDEX_CAP || this.walks > 0) {
463
- panic("Map maximum size exceeded")
464
- }
465
- this.rebuild()
466
- bucket = -1
467
- }
468
502
  this.entryKeys.push(storedKey(key))
469
503
  this.entryValues.push(value)
470
- this.entryHashes.push(h)
471
- this.live = this.live + 1
472
- this.size = this.size + 1
504
+ this.entryHashes.push(toU32(packed))
505
+ this.addOne()
473
506
  // Every entry takes a bucket, live or dead, until a rebuild, which files
474
507
  // the new entry with the rest; otherwise it takes the bucket the probe found.
475
- const used = toI32(this.entryKeys.length)
476
- if (used * 4 > toI32(this.slots.length) * 3) {
508
+ if (!fileAppended(this.slots, this.mask, toI32(packed >> 32), this.entryHashes)) {
477
509
  this.rebuild()
478
- } else {
479
- fileAppended(this.slots, this.mask, bucket, h, used)
510
+ }
511
+ }
512
+
513
+ /**
514
+ * Count one more live entry. The entries, dead ones included, are fewer
515
+ * than 2^31 - 1, the most an array holds, so neither count is ever at the
516
+ * bound the test names.
517
+ */
518
+ private addOne(): void {
519
+ const live = this.live
520
+ if (live < 2147483647) {
521
+ this.live = live + 1
522
+ }
523
+ const size = this.size
524
+ if (size < 2147483647) {
525
+ this.size = size + 1
526
+ }
527
+ }
528
+
529
+ /** Count one live entry fewer: `delete`'s, which found one, so neither count is 0. */
530
+ private dropOne(): void {
531
+ const live = this.live
532
+ if (live > 0) {
533
+ this.live = live - 1
534
+ }
535
+ const size = this.size
536
+ if (size > 0) {
537
+ this.size = size - 1
480
538
  }
481
539
  }
482
540
 
@@ -506,28 +564,59 @@ export class Map<K, V> {
506
564
  * and re-files them from the stored hashes, so no entry moves and it is as
507
565
  * safe under a walk as the doubling a walk already allows (§6.2). A count
508
566
  * that is not positive, or not a number, does nothing, as `reserve` does
509
- * under Node; one past the entry cap is the cap.
567
+ * under Node. The table grows to at most 2^30 buckets, the largest power of
568
+ * two an array holds; filling one that size runs out of memory first.
510
569
  *
511
570
  * @internal Called by name by the code the compiler writes for `reserve` from
512
571
  * `nish/map`, src/emit-map.ts.
513
572
  */
514
573
  reserveSlots(n: number): void {
515
- if (!(n > 0)) {
574
+ const want = toF64(n)
575
+ if (!(want > 0)) {
516
576
  return
517
577
  }
518
- const want: i32 = n >= 16777215 ? INDEX_CAP : toI32(n)
519
- const have = toI32(this.slots.length)
578
+ // In `f64`, which has no overflow to prove away.
579
+ const have = toF64(toI32(this.slots.length))
520
580
  let size = have
521
- while (want * 4 > size * 3) {
581
+ while (want * 4 > size * 3 && size < 1073741824) {
522
582
  size = size * 2
523
583
  }
524
- if (size > have) {
525
- const slots = new Array<u32>(size)
526
- this.slots = slots
527
- this.mask = size - 1
584
+ if (size > have && have > 0) {
585
+ const wanted = toI32(size)
586
+ if (wanted > 0 && wanted <= 65535) {
587
+ const slots = new Array<u32>(toU16(wanted))
588
+ this.slots = slots
589
+ this.mask = wanted - 1
590
+ refile(slots, this.entryHashes)
591
+ return
592
+ }
593
+ const slots = this.slots
594
+ widenSlots(slots, toI32(size / have))
595
+ clearSlots(slots)
596
+ this.mask = toI32(slots.length) - 1
528
597
  refile(slots, this.entryHashes)
529
598
  }
530
599
  }
600
+
601
+ /**
602
+ * The entry keys, dead ones included, for `forEachEntry`'s walk to index
603
+ * with what `walkNext` found. Read again after every call it makes, since
604
+ * `clear` replaces them.
605
+ *
606
+ * @internal Called by `forEachEntry`, `forEachValue` and `forEachElement`.
607
+ */
608
+ walkKeys(): K[] {
609
+ return this.entryKeys
610
+ }
611
+
612
+ /**
613
+ * The entry values, as `walkKeys`.
614
+ *
615
+ * @internal Called by `forEachEntry` and `forEachValue`.
616
+ */
617
+ walkValues(): V[] {
618
+ return this.entryValues
619
+ }
531
620
  }
532
621
 
533
622
  /** A set of keys in insertion order: `Map`'s table with no values, and the same probe. */
@@ -541,11 +630,11 @@ export class Set<T> {
541
630
  private entryKeys: T[]
542
631
  private entryHashes: u32[]
543
632
  /** `Map`'s walk count: how many `for...of` loops are walking the table now. */
544
- private walks: i32 = 0
633
+ private walks: u32 = 0
545
634
 
546
635
  /** An empty set. */
547
636
  constructor() {
548
- this.slots = new Array<u32>(INITIAL_SLOTS)
637
+ this.slots = new Array<u32>(8)
549
638
  this.entryKeys = []
550
639
  this.entryHashes = []
551
640
  }
@@ -591,9 +680,8 @@ export class Set<T> {
591
680
  if (found < 0) {
592
681
  return false
593
682
  }
594
- killEntry(this.slots, this.entryHashes, found)
595
- this.live = this.live - 1
596
- this.size = this.size - 1
683
+ killEntry(this.slots, this.mask, this.entryHashes, found)
684
+ this.dropOne()
597
685
  return true
598
686
  }
599
687
 
@@ -618,7 +706,7 @@ export class Set<T> {
618
706
  * src/emit-map.ts.
619
707
  */
620
708
  walkOpen(): void {
621
- this.walks = this.walks + 1
709
+ this.walks = this.walks + 1 // a `u32`: no program nests 2^32 walks
622
710
  }
623
711
 
624
712
  /** @internal Called by name by the code the compiler writes for a `Set`, src/emit-map.ts. */
@@ -632,36 +720,47 @@ export class Set<T> {
632
720
  }
633
721
 
634
722
  /** @internal Called by name by the code the compiler writes for a `Set`, src/emit-map.ts. */
635
- keyAt(index: i32): T {
636
- if (index < 0 || index >= toI32(this.entryKeys.length)) {
637
- panic("Set: no entry at this index")
723
+ keyAt(index: i32, none: T): T {
724
+ if (index >= 0 && index < toI32(this.entryKeys.length)) {
725
+ return this.entryKeys[index]
638
726
  }
639
- return this.entryKeys[index]
727
+ return none
640
728
  }
641
729
 
642
730
  /** @internal Called by name by the code the compiler writes for a `Set`, src/emit-map.ts. */
643
731
  insertAt(absent: i64, key: T): void {
644
732
  const packed = -1 - absent
645
- let bucket = toI32(packed >> 32)
646
- const h = toU32(packed)
647
- if (toI32(this.entryKeys.length) >= INDEX_CAP) {
648
- if (this.live >= INDEX_CAP || this.walks > 0) {
649
- panic("Set maximum size exceeded")
650
- }
651
- this.rebuild()
652
- bucket = -1
653
- }
654
733
  this.entryKeys.push(storedKey(key))
655
- this.entryHashes.push(h)
656
- this.live = this.live + 1
657
- this.size = this.size + 1
734
+ this.entryHashes.push(toU32(packed))
735
+ this.addOne()
658
736
  // Every entry takes a bucket, live or dead, until a rebuild, which files
659
737
  // the new entry with the rest; otherwise it takes the bucket the probe found.
660
- const used = toI32(this.entryKeys.length)
661
- if (used * 4 > toI32(this.slots.length) * 3) {
738
+ if (!fileAppended(this.slots, this.mask, toI32(packed >> 32), this.entryHashes)) {
662
739
  this.rebuild()
663
- } else {
664
- fileAppended(this.slots, this.mask, bucket, h, used)
740
+ }
741
+ }
742
+
743
+ /** `Map`'s count of one more live entry. */
744
+ private addOne(): void {
745
+ const live = this.live
746
+ if (live < 2147483647) {
747
+ this.live = live + 1
748
+ }
749
+ const size = this.size
750
+ if (size < 2147483647) {
751
+ this.size = size + 1
752
+ }
753
+ }
754
+
755
+ /** `Map`'s count of one live entry fewer. */
756
+ private dropOne(): void {
757
+ const live = this.live
758
+ if (live > 0) {
759
+ this.live = live - 1
760
+ }
761
+ const size = this.size
762
+ if (size > 0) {
763
+ this.size = size - 1
665
764
  }
666
765
  }
667
766
 
@@ -677,6 +776,15 @@ export class Set<T> {
677
776
  this.mask = toI32(slots.length) - 1
678
777
  refile(slots, this.entryHashes)
679
778
  }
779
+
780
+ /**
781
+ * The entry keys, dead ones included, as `Map.walkKeys`.
782
+ *
783
+ * @internal Called by `forEachElement`.
784
+ */
785
+ walkKeys(): T[] {
786
+ return this.entryKeys
787
+ }
680
788
  }
681
789
 
682
790
  /**
@@ -702,8 +810,12 @@ const storedKey = <K>(key: K): K => key
702
810
  */
703
811
  export const forEachEntry = <K, V>(m: Map<K, V>, f: (value: V, key: K) => void): void => {
704
812
  m.walkOpen()
705
- for (let i: i32 = m.walkNext(0); i >= 0; i = m.walkNext(i + 1)) {
706
- f(m.valueAt(i), m.keyAt(i))
813
+ for (let i: i32 = m.walkNext(0); i >= 0; i = i < 2147483647 ? m.walkNext(i + 1) : -1) {
814
+ const keys = m.walkKeys()
815
+ const values = m.walkValues()
816
+ if (i < keys.length && i < values.length) {
817
+ f(values[i], keys[i])
818
+ }
707
819
  }
708
820
  m.walkClose()
709
821
  }
@@ -718,8 +830,11 @@ export const forEachEntry = <K, V>(m: Map<K, V>, f: (value: V, key: K) => void):
718
830
  */
719
831
  export const forEachValue = <K, V>(m: Map<K, V>, f: (value: V) => void): void => {
720
832
  m.walkOpen()
721
- for (let i: i32 = m.walkNext(0); i >= 0; i = m.walkNext(i + 1)) {
722
- f(m.valueAt(i))
833
+ for (let i: i32 = m.walkNext(0); i >= 0; i = i < 2147483647 ? m.walkNext(i + 1) : -1) {
834
+ const values = m.walkValues()
835
+ if (i < values.length) {
836
+ f(values[i])
837
+ }
723
838
  }
724
839
  m.walkClose()
725
840
  }
@@ -734,8 +849,11 @@ export const forEachValue = <K, V>(m: Map<K, V>, f: (value: V) => void): void =>
734
849
  */
735
850
  export const forEachElement = <T>(s: Set<T>, f: (value: T) => void): void => {
736
851
  s.walkOpen()
737
- for (let i: i32 = s.walkNext(0); i >= 0; i = s.walkNext(i + 1)) {
738
- f(s.keyAt(i))
852
+ for (let i: i32 = s.walkNext(0); i >= 0; i = i < 2147483647 ? s.walkNext(i + 1) : -1) {
853
+ const keys = s.walkKeys()
854
+ if (i < keys.length) {
855
+ f(keys[i])
856
+ }
739
857
  }
740
858
  s.walkClose()
741
859
  }