nish 0.49.1 → 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 (91) hide show
  1. package/README.md +27 -21
  2. package/docs/AI.md +233 -159
  3. package/docs/INSTALL.md +1 -1
  4. package/package.json +12 -12
  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 +12 -6
  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 +16 -9
  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/yaml-json.ts +238 -106
  89. package/std/yaml-lines.ts +47 -27
  90. package/std/yaml-write.ts +58 -24
  91. package/std/yaml.ts +808 -395
package/std/yaml-json.ts CHANGED
@@ -134,7 +134,7 @@ export const YAML_JSON_ERR_NOT_FINITE: i32 = -42
134
134
  export const YAML_JSON_ERR_EXPANSION: i32 = -43
135
135
  /** Collections nest deeper than `maxDepth`, counting copies. */
136
136
  export const YAML_JSON_ERR_DEPTH: i32 = -44
137
- /** A `0x` or `0o` integer with more than `YAML_JSON_MAX_RADIX_DIGITS` digits. */
137
+ /** A `0x` or `0o` integer with more than `YAML_JSON_MAX_RADIX_DIGITS` digits, or with a byte that is not a digit of its base. */
138
138
  export const YAML_JSON_ERR_NUMBER: i32 = -45
139
139
  /**
140
140
  * A merge key's value is not a mapping or a sequence of mappings; the range is
@@ -156,9 +156,10 @@ export const YAML_JSON_SCAN_KEYS: i32 = 16
156
156
 
157
157
  /** How many merge keys deep a merged map is searched, as `YAML_ALIAS_HOPS` bounds `pathNode`. */
158
158
  const YAML_JSON_MERGE_HOPS: i32 = 100
159
-
160
- /** The decimal limb `yamlJsonRadix` works in: four digits, so a limb times 16 plus a carry fits an `i32`. */
161
- const YAML_JSON_LIMB: i32 = 10000
159
+ /** What a merge key's own pair takes from the load's merge budget. */
160
+ const YAML_JSON_MERGE_PAIR: i32 = 1
161
+ /** A part `plus` adds whole, with no merge adjustment taken off. */
162
+ const YAML_JSON_WHOLE: i32 = 0
162
163
 
163
164
  const YAML_JSON_PLUS: i32 = 43
164
165
  const YAML_JSON_DASH: i32 = 45
@@ -182,9 +183,22 @@ const YAML_JSON_NO_WORD: i32 = 0
182
183
  * @returns The code's name without the `YAML_JSON_ERR_` prefix, such as `"DEPTH"`; `""` for any other code.
183
184
  */
184
185
  export const yamlJsonErrorName = (code: i32): string => {
186
+ const codes: i32[] = [
187
+ YAML_JSON_ERR_DOCUMENT,
188
+ YAML_JSON_ERR_YAML,
189
+ YAML_JSON_ERR_NOT_FINITE,
190
+ YAML_JSON_ERR_EXPANSION,
191
+ YAML_JSON_ERR_DEPTH,
192
+ YAML_JSON_ERR_NUMBER,
193
+ YAML_JSON_ERR_MERGE,
194
+ ]
185
195
  const names: string[] = ["DOCUMENT", "YAML", "NOT_FINITE", "EXPANSION", "DEPTH", "NUMBER", "MERGE"]
186
- const i: i32 = YAML_JSON_ERR_DOCUMENT - code
187
- return i >= 0 && i < toI32(names.length) ? names[i] : ""
196
+ for (let i: i32 = 0; i < toI32(codes.length) && i < toI32(names.length); i += 1) {
197
+ if (codes[i] === code) {
198
+ return names[i]
199
+ }
200
+ }
201
+ return ""
188
202
  }
189
203
 
190
204
  /**
@@ -209,13 +223,15 @@ export class YamlJsonLoader {
209
223
  private sizes: i32[]
210
224
  /** The nodes the last count set a size on, the first `countedTop` of them: all a count clears. */
211
225
  private counted: i32[]
212
- private countedTop: i32 = 0
213
226
  /** `quickCount`'s sizes, by node; each pass writes every one it reads. */
214
227
  private quickSizes: i32[]
215
228
  /** The count's own stack of nodes. */
216
229
  private pending: i32[]
217
- /** The most values the document may hold; a size saturates one past it. */
230
+ /** How many of `counted` the last count set. */
231
+ private countedTop: i32 = 0
232
+ /** The most values the document may hold; a size saturates at `over`, one past it. */
218
233
  private cap: i32 = 0
234
+ private over: i32 = 1
219
235
  /** The alias with the largest copy, and its size. */
220
236
  private bigAlias: i32 = -1
221
237
  private bigSize: i32 = 0
@@ -236,8 +252,13 @@ export class YamlJsonLoader {
236
252
  /** Per node, the stamp of the last map it was merged into; and that map's stamp. */
237
253
  private mergeMarks: i32[]
238
254
  private mergeStamp: i32 = 0
239
- /** The merged pairs the load may still visit. */
240
- private mergeBudget: i32 = 0
255
+ /**
256
+ * The merged pairs the load may still visit: `mergeNodes` more, then
257
+ * `mergeRoom` more. Two halves, the stream's node count and the values
258
+ * the document may hold, so that neither sum can overflow.
259
+ */
260
+ private mergeNodes: i32 = 0
261
+ private mergeRoom: i32 = 0
241
262
 
242
263
  /** A loader that holds collections to `JSON_WRITER_MAX_DEPTH`, so whatever it loads can be written back out. */
243
264
  constructor() {
@@ -290,13 +311,22 @@ export class YamlJsonLoader {
290
311
  return this.nullAt(at)
291
312
  }
292
313
  const room: i32 = doc.maxValues < JSON_VALUE_MAX_VALUES ? doc.maxValues : JSON_VALUE_MAX_VALUES
293
- if (this.quickCount(this.firstNode(d), root, room) > room && this.count(root, room) > room) {
314
+ // Never taken: `JSON_VALUE_MAX_VALUES` is 2^22. The literal, under 2^30,
315
+ // is what the prover reads to bound `room + 1`, the size a count
316
+ // saturates at; it does not read a constant's value.
317
+ if (room > 1073741823) {
318
+ return this.fail(JSON_VALUE_ERR_VALUES, stream.start(root), stream.end(root))
319
+ }
320
+ this.cap = room
321
+ this.over = room + 1
322
+ if (this.quickCount(this.firstNode(d), root) > room && this.count(root) > room) {
294
323
  if (this.bigAlias >= 0) {
295
324
  return this.fail(YAML_JSON_ERR_EXPANSION, stream.start(this.bigAlias), stream.end(this.bigAlias))
296
325
  }
297
326
  return this.fail(JSON_VALUE_ERR_VALUES, stream.start(root), stream.end(root))
298
327
  }
299
- this.mergeBudget = room + toI32(stream.kinds.length)
328
+ this.mergeNodes = toI32(stream.kinds.length)
329
+ this.mergeRoom = room
300
330
  return this.build(root, origin)
301
331
  }
302
332
 
@@ -331,13 +361,13 @@ export class YamlJsonLoader {
331
361
  // ---- Counting ------------------------------------------------------------------
332
362
 
333
363
  /**
334
- * How many values node `root` expands to, saturating at `cap + 1`.
364
+ * How many values node `root` expands to, saturating at `over`, `cap + 1`.
335
365
  * Each node is counted once and its count kept, on a stack of its own
336
366
  * rather than by recursion, because a chain of aliases can be as long as
337
367
  * the text. Sets `bigAlias`. Clears only the sizes the last count set, so a
338
368
  * stream of many documents is not cleared whole for each.
339
369
  */
340
- private count(root: i32, cap: i32): i32 {
370
+ private count(root: i32): i32 {
341
371
  const stream: YamlStream = this.stream
342
372
  const nodes: i32 = toI32(stream.kinds.length)
343
373
  const sizes: i32[] = this.sizes
@@ -352,13 +382,13 @@ export class YamlJsonLoader {
352
382
  }
353
383
  }
354
384
  this.countedTop = 0
355
- this.cap = cap
356
385
  this.bigAlias = -1
357
386
  this.bigSize = 0
358
387
  const pending: i32[] = this.pending
359
388
  let top: i32 = 0
360
389
  top = this.pushNode(top, root)
361
- while (top > 0) {
390
+ // `top` is never past the stack: `pushNode` answers at most its length.
391
+ while (top > 0 && top <= toI32(pending.length)) {
362
392
  const n: i32 = pending[top - 1]
363
393
  // A pushed node is in range; the test proves it to the bounds check.
364
394
  if (n < 0 || n >= toI32(sizes.length) || sizes[n] > 0) {
@@ -366,12 +396,15 @@ export class YamlJsonLoader {
366
396
  } else if (sizes[n] === 0) {
367
397
  // First visit: count the nodes it is made of first.
368
398
  sizes[n] = -1
369
- if (this.countedTop < toI32(counted.length)) {
370
- counted[this.countedTop] = n
399
+ const c: i32 = this.countedTop
400
+ if (c >= 0 && c < toI32(counted.length)) {
401
+ counted[c] = n
402
+ this.countedTop = c + 1
371
403
  } else {
404
+ // `c` is the length here, never past it: the push makes it `c + 1`.
372
405
  counted.push(n)
406
+ this.countedTop = toI32(counted.length)
373
407
  }
374
- this.countedTop += 1
375
408
  top = this.pushParts(top, n)
376
409
  } else {
377
410
  const size: i32 = this.sizeOf(n)
@@ -381,22 +414,30 @@ export class YamlJsonLoader {
381
414
  top -= 1
382
415
  }
383
416
  }
384
- return sizes[root]
417
+ // `top` is above 0 here only if the loop left early, which the test above
418
+ // never does; the count is unfinished then, and counts as too many.
419
+ if (top > 0) {
420
+ return this.over
421
+ }
422
+ // `sizes` holds every node, so `root` is in it; a root outside counts as too many.
423
+ return root >= 0 && root < toI32(sizes.length) ? sizes[root] : this.over
385
424
  }
386
425
 
387
426
  /** The first node of document `d`: the one after the last earlier document's root. */
388
427
  private firstNode(d: i32): i32 {
389
- for (let e: i32 = d - 1; e >= 0; e -= 1) {
390
- const root: i32 = this.stream.documentRoot(e)
428
+ for (let e: i32 = d; e > 0; e -= 1) {
429
+ const root: i32 = this.stream.documentRoot(e - 1)
391
430
  if (root >= 0) {
392
- return root + 1
431
+ // A root is a node, so below the node count; were it not, node 0,
432
+ // which `quickCount` can always start from, is the answer.
433
+ return root < toI32(this.stream.kinds.length) ? root + 1 : 0
393
434
  }
394
435
  }
395
436
  return 0
396
437
  }
397
438
 
398
439
  /**
399
- * What `count(root, cap)` answers, found in one pass over nodes `lo` to
440
+ * What `count(root)` answers, found in one pass over nodes `lo` to
400
441
  * `root` when that is enough: the parser numbers a node after its parts and
401
442
  * an alias after its anchor, so each part's size is known when its node is
402
443
  * reached. A part outside that span (a cycle, or a node of another
@@ -404,10 +445,11 @@ export class YamlJsonLoader {
404
445
  * `count`, which also finds the alias to blame. Sizes are kept apart from
405
446
  * `count`'s, and every one read was written by this pass, so none is cleared.
406
447
  */
407
- private quickCount(lo: i32, root: i32, cap: i32): i32 {
448
+ private quickCount(lo: i32, root: i32): i32 {
408
449
  const stream: YamlStream = this.stream
409
450
  const kinds: u8[] = stream.kinds
410
- const over: i32 = cap + 1
451
+ const cap: i32 = this.cap
452
+ const over: i32 = this.over
411
453
  if (lo < 0 || root < lo || root >= toI32(kinds.length) || root >= stream.top) {
412
454
  return over
413
455
  }
@@ -415,7 +457,6 @@ export class YamlJsonLoader {
415
457
  while (toI32(sizes.length) <= root) {
416
458
  sizes.push(0)
417
459
  }
418
- this.cap = cap
419
460
  const firsts: i32[] = stream.firsts
420
461
  const counts: i32[] = stream.counts
421
462
  const kids: i32[] = stream.kids
@@ -430,9 +471,9 @@ export class YamlJsonLoader {
430
471
  const from: i32 = yamlJsonAt(firsts, n)
431
472
  const parts: i32 = yamlJsonAt(counts, n)
432
473
  for (let i: i32 = 0; i < parts; i += 1) {
433
- const child: i32 = yamlJsonAt(kids, from + i)
474
+ const child: i32 = yamlJsonAtPlus(kids, from, i)
434
475
  if (kind === YAML_SEQ) {
435
- total = this.plus(total, this.quickPart(child, lo, n))
476
+ total = this.plus(total, this.quickPart(child, lo, n), YAML_JSON_WHOLE, YAML_JSON_WHOLE)
436
477
  } else {
437
478
  // As `sizeOf` takes a merge's members in, less the merge itself;
438
479
  // read straight, as `stream.value` and `stream.key` read a pair.
@@ -441,11 +482,10 @@ export class YamlJsonLoader {
441
482
  const value: i32 = pair ? yamlJsonAt(counts, child) : -1
442
483
  const merged: boolean = pair && merging && this.mergeShaped(yamlJsonAt(firsts, child))
443
484
  const part: i32 = this.quickPart(value, lo, n)
444
- let less: i32 = merged ? 1 : 0
445
- if (merged && stream.kind(stream.resolved(value)) === YAML_SEQ) {
446
- less += stream.count(stream.resolved(value))
447
- }
448
- total = part > cap ? over : this.plus(total, part - less)
485
+ const listed: boolean = merged && stream.kind(stream.resolved(value)) === YAML_SEQ
486
+ const maps: i32 = listed ? stream.count(stream.resolved(value)) : 0
487
+ const less: i32 = merged ? 1 : 0
488
+ total = part > cap ? over : this.plus(total, part, less, maps)
449
489
  }
450
490
  }
451
491
  }
@@ -453,7 +493,8 @@ export class YamlJsonLoader {
453
493
  sizes[n] = total
454
494
  }
455
495
  }
456
- return sizes[root]
496
+ // `sizes` was grown past `root` above; a root outside counts as too many.
497
+ return root >= 0 && root < toI32(sizes.length) ? sizes[root] : over
457
498
  }
458
499
 
459
500
  /** Part `p`'s size in `quickCount` at node `n`: `1` for a missing node, too many for one outside `[lo, n)`. */
@@ -462,22 +503,26 @@ export class YamlJsonLoader {
462
503
  return 1
463
504
  }
464
505
  if (p < lo || p >= n || p >= toI32(this.quickSizes.length)) {
465
- return this.cap + 1
506
+ return this.over
466
507
  }
467
508
  return this.quickSizes[p]
468
509
  }
469
510
 
470
511
  /** Pushes node `n` onto the count's stack unless it is counted; answers the new top. */
471
512
  private pushNode(top: i32, n: i32): i32 {
472
- if (n < 0 || this.sizes[n] !== 0) {
513
+ const sizes: i32[] = this.sizes
514
+ // `sizes` holds every node, so a node past it is never pushed: there is none.
515
+ if (n < 0 || n >= toI32(sizes.length) || sizes[n] !== 0) {
473
516
  return top
474
517
  }
475
- if (top < toI32(this.pending.length)) {
476
- this.pending[top] = n
477
- } else {
478
- this.pending.push(n)
518
+ const pending: i32[] = this.pending
519
+ if (top >= 0 && top < toI32(pending.length)) {
520
+ pending[top] = n
521
+ return top + 1
479
522
  }
480
- return top + 1
523
+ // `top` is the length here, never past it: the push makes it `top + 1`.
524
+ pending.push(n)
525
+ return toI32(pending.length)
481
526
  }
482
527
 
483
528
  /** Pushes the nodes node `n`'s count is the sum of. */
@@ -518,7 +563,7 @@ export class YamlJsonLoader {
518
563
  let total: i32 = 1
519
564
  if (kind === YAML_SEQ) {
520
565
  for (let i: i32 = 0; i < stream.count(n); i += 1) {
521
- total = this.plus(total, this.partSize(stream.child(n, i)))
566
+ total = this.plus(total, this.partSize(stream.child(n, i)), YAML_JSON_WHOLE, YAML_JSON_WHOLE)
522
567
  }
523
568
  } else if (kind === YAML_MAP) {
524
569
  for (let i: i32 = 0; i < stream.count(n); i += 1) {
@@ -527,32 +572,40 @@ export class YamlJsonLoader {
527
572
  // A merged map's members join this one's; its own value is not one.
528
573
  const merged: boolean = this.isMergeKey(stream.key(pair))
529
574
  const part: i32 = this.partSize(value)
530
- let less: i32 = merged ? 1 : 0
531
- if (merged && stream.kind(stream.resolved(value)) === YAML_SEQ) {
532
- // A list of maps: the list itself is not one either, nor any map in it.
533
- less += stream.count(stream.resolved(value))
534
- }
575
+ // A list of maps: the list itself is not one either, nor any map in it.
576
+ const listed: boolean = merged && stream.kind(stream.resolved(value)) === YAML_SEQ
577
+ const maps: i32 = listed ? stream.count(stream.resolved(value)) : 0
578
+ const less: i32 = merged ? 1 : 0
535
579
  // A saturated part stays saturated: taking the adjustment off it
536
580
  // would bring a bomb back under the cap.
537
- total = part > this.cap ? this.cap + 1 : this.plus(total, part - less)
581
+ total = part > this.cap ? this.over : this.plus(total, part, less, maps)
538
582
  }
539
583
  }
540
584
  return total
541
585
  }
542
586
 
543
- /** A counted part's size: `1` for a missing node, too many for one still being counted. */
587
+ /**
588
+ * A counted part's size: `1` for a missing node, too many for one still
589
+ * being counted (or past `sizes`, which holds every node).
590
+ */
544
591
  private partSize(n: i32): i32 {
545
592
  if (n < 0) {
546
593
  return 1
547
594
  }
548
- const size: i32 = this.sizes[n]
549
- return size < 0 ? this.cap + 1 : size
595
+ const sizes: i32[] = this.sizes
596
+ const size: i32 = n < toI32(sizes.length) ? sizes[n] : -1
597
+ return size < 0 ? this.over : size
550
598
  }
551
599
 
552
- /** `a + b`, saturating at `cap + 1`; both are at most that, so the sum fits. */
553
- private plus(a: i32, b: i32): i32 {
554
- const sum: i32 = a + b
555
- return sum > this.cap ? this.cap + 1 : sum
600
+ /**
601
+ * `a + b - less - maps`, saturating at `over`: a size plus a part's, less
602
+ * a merge key's adjustment. Summed in `i64`, where four `i32`s fit. A part
603
+ * is at least its adjustment, so the sum is never below zero; were it, it
604
+ * would count as too many.
605
+ */
606
+ private plus(a: i32, b: i32, less: i32, maps: i32): i32 {
607
+ const sum: i64 = toI64(a) + toI64(b) - toI64(less) - toI64(maps)
608
+ return sum > toI64(this.cap) || sum < 0 ? this.over : toI32(sum)
556
609
  }
557
610
 
558
611
  // ---- Building ------------------------------------------------------------------
@@ -685,7 +738,7 @@ export class YamlJsonLoader {
685
738
  return -1
686
739
  }
687
740
  let i: i32 = from
688
- if (toI32(source.charCodeAt(i)) === YAML_JSON_DASH) {
741
+ if (yamlJsonByte(source, i) === YAML_JSON_DASH) {
689
742
  i += 1
690
743
  }
691
744
  // A sign alone: the byte past it is not this scalar's.
@@ -703,14 +756,14 @@ export class YamlJsonLoader {
703
756
  } else {
704
757
  return -1
705
758
  }
706
- if (i < to && toI32(source.charCodeAt(i)) === YAML_JSON_DOT) {
759
+ if (i < to && yamlJsonByte(source, i) === YAML_JSON_DOT) {
707
760
  const fraction: i32 = yamlJsonDigits(source, i + 1, to)
708
761
  if (fraction === i + 1) {
709
762
  return -1
710
763
  }
711
764
  i = fraction
712
765
  }
713
- if (i < to && (toI32(source.charCodeAt(i)) | 32) === 101) {
766
+ if (i < to && (yamlJsonByte(source, i) | 32) === 101) {
714
767
  i += 1
715
768
  const sign: i32 = yamlJsonByte(source, i)
716
769
  if (i < to && (sign === YAML_JSON_PLUS || sign === YAML_JSON_DASH)) {
@@ -768,13 +821,16 @@ export class YamlJsonLoader {
768
821
  const kids: i32[] = stream.kids
769
822
  const first: i32 = yamlJsonAt(stream.firsts, at)
770
823
  for (let i: i32 = 0; i < n; i += 1) {
771
- const c: i32 = yamlJsonAt(kids, first + i)
824
+ const c: i32 = yamlJsonAtPlus(kids, first, i)
772
825
  const v: JsonValue = c < 0 ? this.nullAt(stream.end(at)) : this.build(c, depth)
773
826
  if (v < 0) {
774
827
  return v
775
828
  }
776
829
  // `v` was just made, so it is in no container: what `push` would check.
777
- doc.append(ax, doc.index(v), YAML_JSON_NO_WORD, YAML_JSON_NO_WORD)
830
+ const slot: i32 = doc.append(ax, doc.index(v), YAML_JSON_NO_WORD, YAML_JSON_NO_WORD)
831
+ if (slot < 0) {
832
+ return this.fail(slot, stream.start(at), stream.end(at))
833
+ }
778
834
  }
779
835
  return a
780
836
  }
@@ -794,7 +850,7 @@ export class YamlJsonLoader {
794
850
  const kids: i32[] = stream.kids
795
851
  const first: i32 = yamlJsonAt(stream.firsts, at)
796
852
  for (let i: i32 = 0; i < pairs; i += 1) {
797
- const key: i32 = stream.key(yamlJsonAt(kids, first + i))
853
+ const key: i32 = stream.key(yamlJsonAtPlus(kids, first, i))
798
854
  if (key < 0) {
799
855
  own = false
800
856
  } else if (this.mergeShaped(key)) {
@@ -830,15 +886,26 @@ export class YamlJsonLoader {
830
886
  return this.fail(o, stream.start(at), stream.end(at))
831
887
  }
832
888
  const ox: i32 = doc.index(o)
833
- doc.reserve(ox, top - base)
834
- for (let e: i32 = base; e < top; e += 1) {
835
- const pair: i32 = this.entryPairs[e]
836
- const valueNode: i32 = this.entryValues[e]
889
+ if (base >= 0 && top >= base) {
890
+ doc.reserve(ox, top - base)
891
+ }
892
+ const keys: string[] = this.entryKeys
893
+ const entryPairs: i32[] = this.entryPairs
894
+ const values: i32[] = this.entryValues
895
+ // The three arrays grow together and hold every entry below `entryTop`,
896
+ // so the bounds on `e` past `top` never stop the loop early.
897
+ for (
898
+ let e: i32 = base;
899
+ e >= 0 && e < top && e < toI32(keys.length) && e < toI32(entryPairs.length) && e < toI32(values.length);
900
+ e += 1
901
+ ) {
902
+ const pair: i32 = entryPairs[e]
903
+ const valueNode: i32 = values[e]
904
+ const key: string = keys[e]
837
905
  const v: JsonValue = valueNode < 0 ? this.nullAt(stream.end(pair)) : this.build(valueNode, depth)
838
906
  if (v < 0) {
839
907
  return v
840
908
  }
841
- const key: string = this.entryKeys[e]
842
909
  const keyAt: i32 = doc.byteTop
843
910
  const keyNode: i32 = stream.key(pair)
844
911
  // `key` is `keyText(keyNode)`, which `storeKey` spells from the node without the copy.
@@ -847,6 +914,9 @@ export class YamlJsonLoader {
847
914
  if (doc.byteTop > this.byteCap) {
848
915
  return this.tooMuchText(keyNode < 0 ? pair : keyNode)
849
916
  }
917
+ if (slot < 0) {
918
+ return this.fail(slot, stream.start(pair), stream.end(pair))
919
+ }
850
920
  const keyStart: i32 = keyNode < 0 ? stream.start(pair) : stream.start(keyNode)
851
921
  const keyEnd: i32 = keyNode < 0 ? keyStart : stream.end(keyNode)
852
922
  if (this.keyRange(slot, keyStart, keyEnd) < 0) {
@@ -879,7 +949,7 @@ export class YamlJsonLoader {
879
949
  const counts: i32[] = stream.counts
880
950
  const first: i32 = yamlJsonAt(firsts, at)
881
951
  for (let i: i32 = 0; i < pairs; i += 1) {
882
- const pair: i32 = yamlJsonAt(kids, first + i)
952
+ const pair: i32 = yamlJsonAtPlus(kids, first, i)
883
953
  const valueNode: i32 = yamlJsonAt(counts, pair)
884
954
  const v: JsonValue = valueNode < 0 ? this.nullAt(stream.end(pair)) : this.build(valueNode, depth)
885
955
  if (v < 0) {
@@ -891,6 +961,9 @@ export class YamlJsonLoader {
891
961
  if (doc.byteTop > this.byteCap) {
892
962
  return this.tooMuchText(keyNode)
893
963
  }
964
+ if (slot < 0) {
965
+ return this.fail(slot, stream.start(pair), stream.end(pair))
966
+ }
894
967
  if (this.keyRange(slot, stream.start(keyNode), stream.end(keyNode)) < 0) {
895
968
  return this.failed
896
969
  }
@@ -928,8 +1001,7 @@ export class YamlJsonLoader {
928
1001
  }
929
1002
  } else if (source >= 0 && stream.kind(source) === YAML_SEQ) {
930
1003
  // The walk itself is charged, since its items may all be merged already.
931
- this.mergeBudget -= stream.count(source)
932
- if (this.mergeBudget < 0) {
1004
+ if (!this.spendMerges(stream.count(source))) {
933
1005
  return this.fail(YAML_JSON_ERR_EXPANSION, stream.start(pair), stream.end(pair))
934
1006
  }
935
1007
  for (let j: i32 = 0; j < stream.count(source); j += 1) {
@@ -962,8 +1034,7 @@ export class YamlJsonLoader {
962
1034
  if (!this.markMerged(m)) {
963
1035
  return 0
964
1036
  }
965
- this.mergeBudget -= stream.count(m) + 1
966
- if (this.mergeBudget < 0) {
1037
+ if (!this.spendMerges(stream.count(m)) || !this.spendMerges(YAML_JSON_MERGE_PAIR)) {
967
1038
  return this.fail(YAML_JSON_ERR_EXPANSION, stream.start(pair), stream.end(pair))
968
1039
  }
969
1040
  for (let i: i32 = 0; i < stream.count(m); i += 1) {
@@ -975,6 +1046,29 @@ export class YamlJsonLoader {
975
1046
  return this.mergeInto(base, m, hops)
976
1047
  }
977
1048
 
1049
+ /**
1050
+ * Takes `n` merged pairs from the load's budget, `mergeNodes` first;
1051
+ * `false`, when the budget does not hold them, is an expansion.
1052
+ */
1053
+ private spendMerges(n: i32): boolean {
1054
+ const nodes: i32 = this.mergeNodes
1055
+ const room: i32 = this.mergeRoom
1056
+ if (n < 0 || nodes < 0 || room < 0) {
1057
+ return false
1058
+ }
1059
+ if (n <= nodes) {
1060
+ this.mergeNodes = nodes - n
1061
+ return true
1062
+ }
1063
+ const rest: i32 = n - nodes
1064
+ if (rest < 0 || rest > room) {
1065
+ return false
1066
+ }
1067
+ this.mergeNodes = 0
1068
+ this.mergeRoom = room - rest
1069
+ return true
1070
+ }
1071
+
978
1072
  /** Starts a map's merges, so no node is marked as merged into it yet. */
979
1073
  private newMergeStamp(): void {
980
1074
  const nodes: i32 = toI32(this.stream.kinds.length)
@@ -982,8 +1076,10 @@ export class YamlJsonLoader {
982
1076
  while (toI32(marks.length) < nodes) {
983
1077
  marks.push(0)
984
1078
  }
985
- this.mergeStamp += 1
986
- if (this.mergeStamp >= JSON_VALUE_MAX_VALUES) {
1079
+ const stamp: i32 = this.mergeStamp
1080
+ if (stamp < JSON_VALUE_MAX_VALUES && stamp + 1 < JSON_VALUE_MAX_VALUES) {
1081
+ this.mergeStamp = stamp + 1
1082
+ } else {
987
1083
  // Far past any one load: start the stamps over rather than let them wrap.
988
1084
  for (let i: i32 = 0; i < toI32(marks.length); i += 1) {
989
1085
  marks[i] = 0
@@ -1020,21 +1116,30 @@ export class YamlJsonLoader {
1020
1116
  const start: i32 = keyNode < 0 ? stream.start(pair) : stream.start(keyNode)
1021
1117
  return this.fail(JSON_VALUE_ERR_DUPLICATE, start, keyNode < 0 ? start : stream.end(keyNode))
1022
1118
  }
1023
- this.entryValues[found] = stream.value(pair)
1119
+ // An entry `findEntry` answers is below `entryTop`, so in the array.
1120
+ const values: i32[] = this.entryValues
1121
+ if (found < toI32(values.length)) {
1122
+ values[found] = stream.value(pair)
1123
+ }
1024
1124
  return 0
1025
1125
  }
1026
1126
  const e: i32 = this.entryTop
1027
- if (e < toI32(this.entryPairs.length)) {
1028
- this.entryKeys[e] = key
1029
- this.entryPairs[e] = pair
1030
- this.entryValues[e] = stream.value(pair)
1127
+ const keys: string[] = this.entryKeys
1128
+ const pairs: i32[] = this.entryPairs
1129
+ const values: i32[] = this.entryValues
1130
+ if (e >= 0 && e < toI32(keys.length) && e < toI32(pairs.length) && e < toI32(values.length)) {
1131
+ keys[e] = key
1132
+ pairs[e] = pair
1133
+ values[e] = stream.value(pair)
1134
+ this.entryTop = e + 1
1031
1135
  } else {
1032
- this.entryKeys.push(key)
1033
- this.entryPairs.push(pair)
1034
- this.entryValues.push(stream.value(pair))
1136
+ // `e` is the arrays' length here, never past it: the pushes make it `e + 1`.
1137
+ keys.push(key)
1138
+ pairs.push(pair)
1139
+ values.push(stream.value(pair))
1140
+ this.entryTop = toI32(pairs.length)
1035
1141
  }
1036
- this.entryTop = e + 1
1037
- if (e - base >= YAML_JSON_SCAN_KEYS) {
1142
+ if (toI64(e) - toI64(base) >= toI64(YAML_JSON_SCAN_KEYS)) {
1038
1143
  this.seen.set(key, e)
1039
1144
  }
1040
1145
  return 0
@@ -1046,13 +1151,17 @@ export class YamlJsonLoader {
1046
1151
  */
1047
1152
  private findEntry(base: i32, key: string): i32 {
1048
1153
  const top: i32 = this.entryTop
1049
- const scanned: i32 = top - base < YAML_JSON_SCAN_KEYS ? top : base + YAML_JSON_SCAN_KEYS
1050
- for (let e: i32 = base; e < scanned; e += 1) {
1051
- if (this.entryKeys[e] === key) {
1154
+ // In `i64`, where a difference or sum of two `i32`s fits.
1155
+ const held: i64 = toI64(top) - toI64(base)
1156
+ const scanned: i64 =
1157
+ held < toI64(YAML_JSON_SCAN_KEYS) ? toI64(top) : toI64(base) + toI64(YAML_JSON_SCAN_KEYS)
1158
+ const keys: string[] = this.entryKeys
1159
+ for (let e: i32 = base; toI64(e) < scanned && e >= 0 && e < toI32(keys.length); e += 1) {
1160
+ if (keys[e] === key) {
1052
1161
  return e
1053
1162
  }
1054
1163
  }
1055
- if (top - base <= YAML_JSON_SCAN_KEYS) {
1164
+ if (held <= toI64(YAML_JSON_SCAN_KEYS)) {
1056
1165
  return -1
1057
1166
  }
1058
1167
  const e: i32 | undefined = this.seen.get(key)
@@ -1084,8 +1193,8 @@ export class YamlJsonLoader {
1084
1193
  const start: i32 = stream.start(key)
1085
1194
  if (
1086
1195
  start < 0 ||
1196
+ start > toI32(stream.source.length) - 2 ||
1087
1197
  stream.end(key) !== start + 2 ||
1088
- start + 2 > toI32(stream.source.length) ||
1089
1198
  toI32(stream.source.charCodeAt(start)) !== YAML_JSON_LT ||
1090
1199
  toI32(stream.source.charCodeAt(start + 1)) !== YAML_JSON_LT ||
1091
1200
  stream.kind(key) !== YAML_SCALAR ||
@@ -1101,12 +1210,18 @@ export class YamlJsonLoader {
1101
1210
  /** `a[i]`, or `-1` outside it, as `YamlStream`'s accessors read their arrays. */
1102
1211
  const yamlJsonAt = (a: i32[], i: i32): i32 => (i >= 0 && i < toI32(a.length) ? a[i] : -1)
1103
1212
 
1213
+ /** `a[from + i]`, or `-1` outside it; the sum is in `i64`, so it cannot overflow. */
1214
+ const yamlJsonAtPlus = (a: i32[], from: i32, i: i32): i32 => {
1215
+ const at: i64 = toI64(from) + toI64(i)
1216
+ return at >= 0 && at < toI64(toI32(a.length)) ? yamlJsonAt(a, toI32(at)) : -1
1217
+ }
1218
+
1104
1219
  // ---- Numbers ---------------------------------------------------------------------
1105
1220
 
1106
1221
  /**
1107
1222
  * The JSON spelling of a core-schema number's text: `""` for `.inf`, `-.inf`
1108
1223
  * and `.nan`, `"#"` for a `0x` or `0o` integer longer than
1109
- * `YAML_JSON_MAX_RADIX_DIGITS` digits.
1224
+ * `YAML_JSON_MAX_RADIX_DIGITS` digits or with a byte that is not one of its digits.
1110
1225
  */
1111
1226
  const yamlJsonNumber = (text: string): string => {
1112
1227
  const n: i32 = toI32(text.length)
@@ -1134,11 +1249,12 @@ const yamlJsonNumber = (text: string): string => {
1134
1249
  whole += 1
1135
1250
  }
1136
1251
  let lead: i32 = i
1137
- while (lead + 1 < whole && yamlJsonByte(text, lead) === YAML_JSON_ZERO) {
1252
+ while (lead < whole - 1 && yamlJsonByte(text, lead) === YAML_JSON_ZERO) {
1138
1253
  lead += 1
1139
1254
  }
1140
1255
  const wholeDigits: string = whole === i ? "0" : text.substring(lead, whole)
1141
- if (whole === n) {
1256
+ // `whole` stops at `n`, so this is `whole === n`.
1257
+ if (whole >= n) {
1142
1258
  return wholeDigits === "0" ? "0" : `${sign}${wholeDigits}`
1143
1259
  }
1144
1260
  let rest: string = text.substring(whole, n)
@@ -1149,25 +1265,35 @@ const yamlJsonNumber = (text: string): string => {
1149
1265
  return `${sign}${wholeDigits}${rest}`
1150
1266
  }
1151
1267
 
1152
- /** `text[from, end)`, digits in `base` 16 or 8, written in decimal: four digits a limb, the lowest first. */
1268
+ /**
1269
+ * `text[from, end)`, digits in `base` 16 or 8, written in decimal: four digits
1270
+ * a limb (base 10000), the lowest first; `"#"` if a byte is not a digit of `base`, which the
1271
+ * core schema never resolves as an integer. The limbs are `u32`: each is below
1272
+ * 10000 and a carry below 16, so `limb * base + carry` stays under
1273
+ * 2^18 and no step comes near a wrap.
1274
+ */
1153
1275
  const yamlJsonRadix = (text: string, from: i32, base: i32): string => {
1154
- const limbs: i32[] = [0]
1276
+ const limbs: u32[] = [0]
1155
1277
  for (let i: i32 = from; i < toI32(text.length); i += 1) {
1156
- let carry: i32 = yamlJsonHexValue(yamlJsonByte(text, i))
1278
+ const digit: i32 = yamlJsonHexValue(yamlJsonByte(text, i))
1279
+ if (digit < 0 || digit >= base) {
1280
+ return "#"
1281
+ }
1282
+ let carry: u32 = toU32(digit)
1157
1283
  for (let l: i32 = 0; l < toI32(limbs.length); l += 1) {
1158
- const x: i32 = limbs[l] * base + carry
1159
- limbs[l] = x % YAML_JSON_LIMB
1160
- carry = x / YAML_JSON_LIMB
1284
+ const x: u32 = limbs[l] * toU32(base) + carry
1285
+ limbs[l] = x % 10000
1286
+ carry = x / 10000
1161
1287
  }
1162
1288
  if (carry > 0) {
1163
1289
  limbs.push(carry)
1164
1290
  }
1165
1291
  }
1166
- const top: i32 = toI32(limbs.length) - 1
1167
- const parts: string[] = [`${limbs[top]}`]
1168
- for (let l: i32 = top - 1; l >= 0 && l < toI32(limbs.length); l -= 1) {
1292
+ // The highest limb as it is, every lower one padded to four digits.
1293
+ const parts: string[] = []
1294
+ for (let l: i32 = toI32(limbs.length) - 1; l >= 0 && l < toI32(limbs.length); l -= 1) {
1169
1295
  const limb: string = `${limbs[l]}`
1170
- parts.push(`${"000".substring(0, 4 - toI32(limb.length))}${limb}`)
1296
+ parts.push(parts.length === 0 ? limb : `${"000".substring(0, 4 - toI32(limb.length))}${limb}`)
1171
1297
  }
1172
1298
  return parts.join("")
1173
1299
  }
@@ -1186,10 +1312,16 @@ const yamlJsonDigits = (text: string, from: i32, to: i32): i32 => {
1186
1312
  return i
1187
1313
  }
1188
1314
 
1189
- /** A hex digit's value; the text is the stream's, already checked to be digits of its base. */
1315
+ /** A hex digit's value, or `-1` for a byte that is not one. Literals, as the prover reads a literal's value and not a constant's. */
1190
1316
  const yamlJsonHexValue = (c: i32): i32 => {
1191
- if (c >= YAML_JSON_ZERO && c <= 57) {
1192
- return c - YAML_JSON_ZERO
1317
+ if (c >= 48 && c <= 57) {
1318
+ return c - 48
1319
+ }
1320
+ if (c >= 97 && c <= 102) {
1321
+ return c - 87
1322
+ }
1323
+ if (c >= 65 && c <= 70) {
1324
+ return c - 55
1193
1325
  }
1194
- return (c | 32) - 87
1326
+ return -1
1195
1327
  }