nish 0.49.0 → 0.50.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/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 +29 -12
  29. package/std/json-value.ts +222 -104
  30. package/std/json.ts +81 -46
  31. package/std/net/dns-config.ts +18 -5
  32. package/std/net/dns-message.ts +60 -27
  33. package/std/net/dns-resolver.ts +35 -16
  34. package/std/net/dns.ts +3 -2
  35. package/std/net/fetch.ts +120 -38
  36. package/std/net/handler.ts +178 -66
  37. package/std/net/hpack.ts +192 -98
  38. package/std/net/http-compress.ts +83 -30
  39. package/std/net/http-fields.ts +327 -31
  40. package/std/net/http-server.ts +317 -20
  41. package/std/net/http1-client-codec.ts +137 -40
  42. package/std/net/http1-codec.ts +391 -202
  43. package/std/net/http1-server.ts +220 -38
  44. package/std/net/http2-cleartext.ts +10 -4
  45. package/std/net/http2-frame.ts +2 -2
  46. package/std/net/http2-tls.ts +3 -3
  47. package/std/net/http2.ts +69 -15
  48. package/std/net/http3-frame.ts +11 -14
  49. package/std/net/http3-server.ts +12 -15
  50. package/std/net/http3.ts +41 -29
  51. package/std/net/multipart.ts +592 -0
  52. package/std/net/outbound.ts +24 -8
  53. package/std/net/postgres-async.ts +92 -54
  54. package/std/net/postgres-carrier.ts +28 -4
  55. package/std/net/postgres.ts +166 -65
  56. package/std/net/prefork.ts +86 -5
  57. package/std/net/protobuf.ts +6 -2
  58. package/std/net/qpack.ts +66 -25
  59. package/std/net/quic-conn-ack.ts +27 -7
  60. package/std/net/quic-conn-cid.ts +13 -7
  61. package/std/net/quic-datagram.ts +6 -5
  62. package/std/net/quic-frame.ts +67 -23
  63. package/std/net/quic-listener.ts +19 -12
  64. package/std/net/quic-recovery.ts +11 -9
  65. package/std/net/quic-stream.ts +7 -5
  66. package/std/net/quic.ts +36 -31
  67. package/std/net/router.ts +136 -24
  68. package/std/net/serve.ts +59 -5
  69. package/std/net/static.ts +81 -33
  70. package/std/net/tls/record.ts +26 -13
  71. package/std/net/tls/ticket.ts +6 -5
  72. package/std/net/tls-tcp.ts +69 -30
  73. package/std/net/tls.ts +152 -38
  74. package/std/net/upstream.ts +65 -19
  75. package/std/net/url.ts +97 -78
  76. package/std/net/websocket.ts +33 -15
  77. package/std/net/webtransport.ts +28 -12
  78. package/std/regex.ts +600 -388
  79. package/std/sqlite.ts +37 -19
  80. package/std/sync/ring.ts +232 -115
  81. package/std/sync/shm.ts +11 -4
  82. package/std/temporal.ts +232 -133
  83. package/std/term-io.ts +12 -6
  84. package/std/term-pixels.ts +158 -22
  85. package/std/term-rate.ts +8 -3
  86. package/std/term.ts +4 -4
  87. package/std/text.ts +33 -11
  88. package/std/threads.ts +52 -8
  89. package/std/yaml-json.ts +238 -106
  90. package/std/yaml-lines.ts +47 -27
  91. package/std/yaml-write.ts +58 -24
  92. package/std/yaml.ts +808 -395
package/std/json-value.ts CHANGED
@@ -94,7 +94,7 @@
94
94
  * **Objects keep insertion order**, the order the members were written in or
95
95
  * added. `get` finds a member by its key with a scan, so it is for a handful of
96
96
  * lookups per object; a program that looks up many keys of a large object
97
- * walks it once with `keyAt` and `at`.
97
+ * walks it once with `keys` (or `keyAt`) and `at`.
98
98
  *
99
99
  * **Duplicate keys are refused by default.** `{"a":1,"a":2}` is
100
100
  * `JSON_VALUE_ERR_DUPLICATE`, with `errorAt` at the first key that repeats an
@@ -469,6 +469,9 @@ export class JsonDocument {
469
469
  keySource = tok.tokStart
470
470
  keyAt = this.byteTop
471
471
  keyLength = this.storeString(tok)
472
+ if (keyLength < 0) {
473
+ return this.parseFailed(JSON_VALUE_ERR_VALUES, tok.tokStart)
474
+ }
472
475
  } else if (k === JSON_TOK_OBJECT_BEGIN || k === JSON_TOK_ARRAY_BEGIN) {
473
476
  const c: i32 = this.newNode(
474
477
  k === JSON_TOK_OBJECT_BEGIN ? JSON_VALUE_OBJECT : JSON_VALUE_ARRAY,
@@ -499,9 +502,13 @@ export class JsonDocument {
499
502
  keySource = jsonValueGet(this.openKeySource, depth)
500
503
  v = c
501
504
  } else if (k === JSON_TOK_STRING) {
502
- v = this.newNode(JSON_VALUE_STRING, this.byteTop, this.storeString(tok))
505
+ const at: i32 = this.byteTop
506
+ const n: i32 = this.storeString(tok)
507
+ v = n < 0 ? JSON_VALUE_ERR_VALUES : this.newNode(JSON_VALUE_STRING, at, n)
503
508
  } else if (k === JSON_TOK_NUMBER) {
504
- v = this.newNode(JSON_VALUE_NUMBER, this.byteTop, this.storeSource(tok))
509
+ const at: i32 = this.byteTop
510
+ const n: i32 = this.storeSource(tok)
511
+ v = n < 0 ? JSON_VALUE_ERR_VALUES : this.newNode(JSON_VALUE_NUMBER, at, n)
505
512
  } else if (k === JSON_TOK_NULL) {
506
513
  v = this.newNode(JSON_VALUE_NULL, JSON_VALUE_NO_WORD, JSON_VALUE_NO_WORD)
507
514
  } else {
@@ -576,6 +583,9 @@ export class JsonDocument {
576
583
  keyEnd = tok.tokEnd
577
584
  keyAt = this.byteTop
578
585
  keyLength = this.storeString(tok)
586
+ if (keyLength < 0) {
587
+ return this.parseFailed(JSON_VALUE_ERR_VALUES, tok.tokStart)
588
+ }
579
589
  } else if (k === JSON_TOK_OBJECT_BEGIN || k === JSON_TOK_ARRAY_BEGIN) {
580
590
  const c: i32 = this.newNode(
581
591
  k === JSON_TOK_OBJECT_BEGIN ? JSON_VALUE_OBJECT : JSON_VALUE_ARRAY,
@@ -612,9 +622,13 @@ export class JsonDocument {
612
622
  keyEnd = jsonValueGet(r.openKeyEnd, depth)
613
623
  v = c
614
624
  } else if (k === JSON_TOK_STRING) {
615
- v = this.newNode(JSON_VALUE_STRING, this.byteTop, this.storeString(tok))
625
+ const at: i32 = this.byteTop
626
+ const n: i32 = this.storeString(tok)
627
+ v = n < 0 ? JSON_VALUE_ERR_VALUES : this.newNode(JSON_VALUE_STRING, at, n)
616
628
  } else if (k === JSON_TOK_NUMBER) {
617
- v = this.newNode(JSON_VALUE_NUMBER, this.byteTop, this.storeSourceRanged(tok))
629
+ const at: i32 = this.byteTop
630
+ const n: i32 = this.storeSourceRanged(tok)
631
+ v = n < 0 ? JSON_VALUE_ERR_VALUES : this.newNode(JSON_VALUE_NUMBER, at, n)
618
632
  } else if (k === JSON_TOK_NULL) {
619
633
  v = this.newNode(JSON_VALUE_NULL, JSON_VALUE_NO_WORD, JSON_VALUE_NO_WORD)
620
634
  } else {
@@ -714,8 +728,11 @@ export class JsonDocument {
714
728
  private close(c: i32, base: i32): i32 {
715
729
  const end: i32 = this.pendingTop
716
730
  // `base` is the pending top when the container opened, and members are
717
- // only added since, so the count cannot wrap.
718
- const n: i32 = toI32(toU32(end) - toU32(base))
731
+ // only added since, so `end` is never below it.
732
+ const n: i32 = jsonValueSpan(base, end)
733
+ if (n < 0) {
734
+ return JSON_VALUE_ERR_VALUES
735
+ }
719
736
  const object: boolean = this.kindAt(c) === JSON_VALUE_OBJECT
720
737
  if (object && n > 1) {
721
738
  const code: i32 = this.duplicateKeys(base, end, n)
@@ -725,21 +742,30 @@ export class JsonDocument {
725
742
  }
726
743
  const start: i32 = this.slotTop
727
744
  // A parse places each value but the root in one slot, and `newNode` holds
728
- // the values under the cap, so `s` cannot wrap.
729
- let s: u32 = toU32(start)
730
- for (let p: i32 = base; p < end; p += 1) {
745
+ // the values under the cap, so the run's end is found and the loop places
746
+ // at most `n`, one per pending member: `s < last` never stops it early.
747
+ const last: i32 = jsonValueEnd(start, n)
748
+ if (last < 0) {
749
+ return JSON_VALUE_ERR_VALUES
750
+ }
751
+ let s: i32 = start
752
+ for (let p: i32 = base; p < end && s < last; p += 1) {
731
753
  const v: i32 = jsonValueGet(this.pendingValue, p)
732
754
  if (v < 0) {
733
755
  continue
734
756
  }
735
- jsonValuePut(this.slotValue, toI32(s), v)
736
- jsonValuePut(this.slotKeyAt, toI32(s), jsonValueGet(this.pendingKeyAt, p))
737
- jsonValuePut(this.slotKeyLength, toI32(s), jsonValueGet(this.pendingKeyLength, p))
757
+ jsonValuePut(this.slotValue, s, v)
758
+ jsonValuePut(this.slotKeyAt, s, jsonValueGet(this.pendingKeyAt, p))
759
+ jsonValuePut(this.slotKeyLength, s, jsonValueGet(this.pendingKeyLength, p))
738
760
  jsonValuePut(this.parent, v, c)
739
761
  s += 1
740
762
  }
741
- const kept: i32 = toI32(s - toU32(start))
742
- this.slotTop = toI32(s)
763
+ // `s` only rose from `start`, so the span is found.
764
+ const kept: i32 = jsonValueSpan(start, s)
765
+ if (kept < 0) {
766
+ return JSON_VALUE_ERR_VALUES
767
+ }
768
+ this.slotTop = s
743
769
  jsonValuePut(this.first, c, start)
744
770
  jsonValuePut(this.count, c, kept)
745
771
  jsonValuePut(this.room, c, kept)
@@ -751,8 +777,11 @@ export class JsonDocument {
751
777
  private closeRanged(c: i32, base: i32): i32 {
752
778
  const end: i32 = this.pendingTop
753
779
  // `base` is the pending top when the container opened, and members are
754
- // only added since, so the count cannot wrap.
755
- const n: i32 = toI32(toU32(end) - toU32(base))
780
+ // only added since, so `end` is never below it.
781
+ const n: i32 = jsonValueSpan(base, end)
782
+ if (n < 0) {
783
+ return JSON_VALUE_ERR_VALUES
784
+ }
756
785
  const object: boolean = this.kindAt(c) === JSON_VALUE_OBJECT
757
786
  if (object && n > 1) {
758
787
  const code: i32 = this.duplicateKeysRanged(base, end, n)
@@ -763,23 +792,32 @@ export class JsonDocument {
763
792
  const r: JsonValueRanges = this.rangeArrays()
764
793
  const start: i32 = this.slotTop
765
794
  // A parse places each value but the root in one slot, and `newNode` holds
766
- // the values under the cap, so `s` cannot wrap.
767
- let s: u32 = toU32(start)
768
- for (let p: i32 = base; p < end; p += 1) {
795
+ // the values under the cap, so the run's end is found and the loop places
796
+ // at most `n`, one per pending member: `s < last` never stops it early.
797
+ const last: i32 = jsonValueEnd(start, n)
798
+ if (last < 0) {
799
+ return JSON_VALUE_ERR_VALUES
800
+ }
801
+ let s: i32 = start
802
+ for (let p: i32 = base; p < end && s < last; p += 1) {
769
803
  const v: i32 = jsonValueGet(this.pendingValue, p)
770
804
  if (v < 0) {
771
805
  continue
772
806
  }
773
- jsonValuePut(this.slotValue, toI32(s), v)
774
- jsonValuePut(this.slotKeyAt, toI32(s), jsonValueGet(this.pendingKeyAt, p))
775
- jsonValuePut(this.slotKeyLength, toI32(s), jsonValueGet(this.pendingKeyLength, p))
776
- jsonValuePut(r.slotKeyStart, toI32(s), jsonValueGet(this.pendingSource, p))
777
- jsonValuePut(r.slotKeyEnd, toI32(s), jsonValueGet(r.pendingKeyEnd, p))
807
+ jsonValuePut(this.slotValue, s, v)
808
+ jsonValuePut(this.slotKeyAt, s, jsonValueGet(this.pendingKeyAt, p))
809
+ jsonValuePut(this.slotKeyLength, s, jsonValueGet(this.pendingKeyLength, p))
810
+ jsonValuePut(r.slotKeyStart, s, jsonValueGet(this.pendingSource, p))
811
+ jsonValuePut(r.slotKeyEnd, s, jsonValueGet(r.pendingKeyEnd, p))
778
812
  jsonValuePut(this.parent, v, c)
779
813
  s += 1
780
814
  }
781
- const kept: i32 = toI32(s - toU32(start))
782
- this.slotTop = toI32(s)
815
+ // `s` only rose from `start`, so the span is found.
816
+ const kept: i32 = jsonValueSpan(start, s)
817
+ if (kept < 0) {
818
+ return JSON_VALUE_ERR_VALUES
819
+ }
820
+ this.slotTop = s
783
821
  jsonValuePut(this.first, c, start)
784
822
  jsonValuePut(this.count, c, kept)
785
823
  jsonValuePut(this.room, c, kept)
@@ -1012,14 +1050,18 @@ export class JsonDocument {
1012
1050
 
1013
1051
  /**
1014
1052
  * The tokenizer's current string or key, decoded onto the end of `bytes`;
1015
- * answers its length. A parse decodes no more bytes than it reads, so the
1016
- * store's end stays under the text's `i32` length and the `u32` sum cannot
1017
- * wrap; `storeSource` leans on the same bound.
1053
+ * answers its length, or `-1`, storing nothing, when the store's end would
1054
+ * pass 2^31 - 1. A parse decodes no more bytes than it reads, so that end
1055
+ * stays under the text's `i32` length and the `-1` is never seen; the
1056
+ * callers still fail with `JSON_VALUE_ERR_VALUES` on it.
1018
1057
  */
1019
1058
  private storeString(tok: JsonTokenizer): i32 {
1020
1059
  const n: i32 = tok.stringLength()
1021
1060
  const at: i32 = this.byteTop
1022
- const end: i32 = toI32(toU32(at) + toU32(n))
1061
+ const end: i32 = jsonValueEnd(at, n)
1062
+ if (end < 0) {
1063
+ return -1
1064
+ }
1023
1065
  const buf: u8[] = jsonValueRoom(this.bytes, end)
1024
1066
  this.bytes = buf
1025
1067
  tok.stringInto(buf, at)
@@ -1027,16 +1069,19 @@ export class JsonDocument {
1027
1069
  return n
1028
1070
  }
1029
1071
 
1030
- /** The tokenizer's current token as written, a number's text, onto the end of `bytes`; answers its length. Keep in step with `storeSourceRanged`. */
1072
+ /** The tokenizer's current token as written, a number's text, onto the end of `bytes`; answers its length, or `-1` as `storeString` does. Keep in step with `storeSourceRanged`. */
1031
1073
  private storeSource(tok: JsonTokenizer): i32 {
1032
1074
  const src: u8[] = tok.src
1033
1075
  const start: i32 = tok.tokStart
1034
1076
  const stop: i32 = tok.tokEnd
1035
- // The token lies in the text, `start <= stop`, so neither its length nor
1036
- // the store's end can wrap (`storeString` has the bound).
1037
- const n: i32 = toI32(toU32(stop) - toU32(start))
1077
+ // The token lies in the text, `start <= stop`, so its length and the
1078
+ // store's end are found; `-1` as `storeString` has it.
1079
+ const n: i32 = jsonValueSpan(start, stop)
1038
1080
  const at: i32 = this.byteTop
1039
- const end: i32 = toI32(toU32(at) + toU32(n))
1081
+ const end: i32 = jsonValueEnd(at, n)
1082
+ if (end < 0) {
1083
+ return -1
1084
+ }
1040
1085
  const buf: u8[] = jsonValueRoom(this.bytes, end)
1041
1086
  this.bytes = buf
1042
1087
  let o: i32 = at
@@ -1061,11 +1106,14 @@ export class JsonDocument {
1061
1106
  const src: u8[] = tok.src
1062
1107
  const start: i32 = tok.tokStart
1063
1108
  const stop: i32 = tok.tokEnd
1064
- // The token lies in the text, `start <= stop`, so neither its length nor
1065
- // the store's end can wrap (`storeString` has the bound).
1066
- const n: i32 = toI32(toU32(stop) - toU32(start))
1109
+ // The token lies in the text, `start <= stop`, so its length and the
1110
+ // store's end are found; `-1` as `storeString` has it.
1111
+ const n: i32 = jsonValueSpan(start, stop)
1067
1112
  const at: i32 = this.byteTop
1068
- const end: i32 = toI32(toU32(at) + toU32(n))
1113
+ const end: i32 = jsonValueEnd(at, n)
1114
+ if (end < 0) {
1115
+ return -1
1116
+ }
1069
1117
  const buf: u8[] = jsonValueRoom(this.bytes, end)
1070
1118
  this.bytes = buf
1071
1119
  let o: i32 = at
@@ -1185,8 +1233,8 @@ export class JsonDocument {
1185
1233
  return -1
1186
1234
  }
1187
1235
  // A container's run of slots lies inside the slot arrays, whose length is
1188
- // an `i32`, so the slot of a member it has cannot wrap.
1189
- return toI32(toU32(jsonValueGet(this.first, x)) + toU32(i))
1236
+ // an `i32`, so the slot of a member it has is found.
1237
+ return jsonValueEnd(jsonValueGet(this.first, x), i)
1190
1238
  }
1191
1239
 
1192
1240
  /**
@@ -1198,8 +1246,11 @@ export class JsonDocument {
1198
1246
  * @returns the element or member value, or `-1`.
1199
1247
  */
1200
1248
  at(v: JsonValue, i: i32): JsonValue {
1201
- const s: i32 = this.slotAt(this.index(v), i)
1202
- return s < 0 ? -1 : this.handle(jsonValueGet(this.slotValue, s))
1249
+ const x: i32 = this.index(v)
1250
+ if (i < 0 || i >= this.lengthAt(x)) {
1251
+ return -1
1252
+ }
1253
+ return this.handle(jsonValueGetAt(this.slotValue, jsonValueGet(this.first, x), i))
1203
1254
  }
1204
1255
 
1205
1256
  /**
@@ -1222,6 +1273,36 @@ export class JsonDocument {
1222
1273
  return this.textOf(jsonValueGet(this.slotKeyAt, s), jsonValueGet(this.slotKeyLength, s))
1223
1274
  }
1224
1275
 
1276
+ /**
1277
+ * Every key of an object, decoded, in member order: `keyAt` for each position
1278
+ * in one pass. Empty when `v` is not an object.
1279
+ *
1280
+ * @param v - an object handle.
1281
+ * @returns the keys, a new array of new strings.
1282
+ */
1283
+ keys(v: JsonValue): string[] {
1284
+ const out: string[] = []
1285
+ const x: i32 = this.index(v)
1286
+ if (this.kindAt(x) !== JSON_VALUE_OBJECT) {
1287
+ return out
1288
+ }
1289
+ const start: i32 = jsonValueGet(this.first, x)
1290
+ const end: i32 = jsonValueEnd(start, jsonValueGet(this.count, x))
1291
+ const keyAt: i32[] = this.slotKeyAt
1292
+ const keyLength: i32[] = this.slotKeyLength
1293
+ // The object's run of slots lies inside both slot arrays, as `slotAt`
1294
+ // says, so the loop's last three tests never end it; they are what proves
1295
+ // the two reads. A `-1` end reads no key.
1296
+ for (
1297
+ let s: i32 = start;
1298
+ s < end && s >= 0 && s < toI32(keyAt.length) && s < toI32(keyLength.length);
1299
+ s += 1
1300
+ ) {
1301
+ out.push(this.textOf(keyAt[s], keyLength[s]))
1302
+ }
1303
+ return out
1304
+ }
1305
+
1225
1306
  /**
1226
1307
  * The value of the member of object `v` whose key is `key`'s bytes; `-1` when
1227
1308
  * there is none.
@@ -1242,8 +1323,9 @@ export class JsonDocument {
1242
1323
  /** The slot of the member of the object at index `x` whose key is `key`'s bytes, or `-1`. */
1243
1324
  private findKey(x: i32, key: string): i32 {
1244
1325
  const start: i32 = jsonValueGet(this.first, x)
1245
- // The object's run of slots lies inside the slot arrays, as `slotAt` says.
1246
- const end: i32 = toI32(toU32(start) + toU32(jsonValueGet(this.count, x)))
1326
+ // The object's run of slots lies inside the slot arrays, as `slotAt` says;
1327
+ // a `-1` end would scan nothing.
1328
+ const end: i32 = jsonValueEnd(start, jsonValueGet(this.count, x))
1247
1329
  for (let s: i32 = start; s < end; s += 1) {
1248
1330
  if (this.bytesEqual(jsonValueGet(this.slotKeyAt, s), jsonValueGet(this.slotKeyLength, s), key)) {
1249
1331
  return s
@@ -1361,9 +1443,9 @@ export class JsonDocument {
1361
1443
  }
1362
1444
  const bytes: u8[] = this.bytes
1363
1445
  const start: i32 = jsonValueGet(this.first, x)
1364
- // A number's text lies inside `bytes`, whose length is an `i32`, so its end cannot wrap.
1365
- const end: i32 = toI32(toU32(start) + toU32(jsonValueGet(this.count, x)))
1366
- if (start < 0 || start >= toI32(bytes.length) || toI32(bytes[start]) === JSON_VALUE_MINUS) {
1446
+ // A number's text lies inside `bytes`, whose length is an `i32`, so its end is found.
1447
+ const end: i32 = jsonValueEnd(start, jsonValueGet(this.count, x))
1448
+ if (end < 0 || start < 0 || start >= toI32(bytes.length) || toI32(bytes[start]) === JSON_VALUE_MINUS) {
1367
1449
  return toU64(0)
1368
1450
  }
1369
1451
  const ten: u64 = toU64(10)
@@ -1426,9 +1508,9 @@ export class JsonDocument {
1426
1508
  /** `bytes[at, at + n)` as a string, in one allocation. */
1427
1509
  private textOf(at: i32, n: i32): string {
1428
1510
  const bytes: u8[] = this.bytes
1429
- // With both below 2^31 the `u32` sum is exact; one past 2^31 - 1 turns
1430
- // negative, below `at`, and like any end past `bytes` names no text.
1431
- const end: i32 = toI32(toU32(at) + toU32(n))
1511
+ // Summed in `i64`, exact; an end past `bytes` is `-1` and names no text.
1512
+ const sum: i64 = toI64(at) + toI64(n)
1513
+ const end: i32 = sum > toI64(toI32(bytes.length)) ? -1 : toI32(sum)
1432
1514
  if (at < 0 || n <= 0 || end < at || end > toI32(bytes.length)) {
1433
1515
  return ""
1434
1516
  }
@@ -1621,8 +1703,8 @@ export class JsonDocument {
1621
1703
  jsonValuePut(r.slotKeyStart, t, -1)
1622
1704
  jsonValuePut(r.slotKeyEnd, t, -1)
1623
1705
  }
1624
- jsonValuePut(r.slotKeyStart, toI32(s), start)
1625
- jsonValuePut(r.slotKeyEnd, toI32(s), end)
1706
+ jsonValuePut(r.slotKeyStart, s, start)
1707
+ jsonValuePut(r.slotKeyEnd, s, end)
1626
1708
  // `s` is a member's slot, so below `slotTop`, and the second test never fails.
1627
1709
  const top: i32 = this.slotTop
1628
1710
  if (s >= this.slotRangeTop && s < top) {
@@ -1781,8 +1863,8 @@ export class JsonDocument {
1781
1863
  if (code < 0) {
1782
1864
  return code
1783
1865
  }
1784
- this.append(ax, vx, JSON_VALUE_NO_WORD, JSON_VALUE_NO_WORD)
1785
- return JSON_VALUE_OK
1866
+ const slot: i32 = this.append(ax, vx, JSON_VALUE_NO_WORD, JSON_VALUE_NO_WORD)
1867
+ return slot < 0 ? slot : JSON_VALUE_OK
1786
1868
  }
1787
1869
 
1788
1870
  /**
@@ -1820,8 +1902,8 @@ export class JsonDocument {
1820
1902
  if (keyLength < 0) {
1821
1903
  return JSON_VALUE_ERR_VALUES
1822
1904
  }
1823
- this.append(ox, vx, keyAt, keyLength)
1824
- return JSON_VALUE_OK
1905
+ const slot: i32 = this.append(ox, vx, keyAt, keyLength)
1906
+ return slot < 0 ? slot : JSON_VALUE_OK
1825
1907
  }
1826
1908
 
1827
1909
  /**
@@ -1853,39 +1935,54 @@ export class JsonDocument {
1853
1935
  * first. A container whose slots are full moves them to the end of the slot
1854
1936
  * arrays with twice the room, unless they are at the end already, where they
1855
1937
  * grow in place; the slots it leaves are not reused until `clear`.
1856
- * Answers the new member's slot.
1938
+ * Answers the new member's slot, or `JSON_VALUE_ERR_VALUES`, adding
1939
+ * nothing, when a slot or count would pass 2^31 - 1, which the bound below
1940
+ * keeps from happening.
1857
1941
  *
1858
1942
  * @internal Read by std/yaml-json.ts, which builds a document from YAML
1859
1943
  * through it.
1860
1944
  */
1861
1945
  append(c: i32, v: i32, keyAt: i32, keyLength: i32): i32 {
1862
- // The sums here are `u32`, and none reaches 2^31, because the slots stay
1863
- // under 2^30 + 5 * 2^22: `reserve` holds its rooms under 2^30, a parse
1864
- // puts each value in at most one slot, and growth by doubling takes at
1865
- // most 4 slots per member appended. Each append takes a value in no
1866
- // container, and `set` frees one only by taking another, so appends are
1867
- // at most the 2^22 values a document can hold.
1946
+ // No sum here reaches 2^31, because the slots stay under 2^30 + 5 * 2^22:
1947
+ // `reserve` holds its rooms under 2^30, a parse puts each value in at
1948
+ // most one slot, and growth by doubling takes at most 4 slots per member
1949
+ // appended. Each append takes a value in no container, and `set` frees
1950
+ // one only by taking another, so appends are at most the 2^22 values a
1951
+ // document can hold. The `-1` checks are that bound, never reached.
1868
1952
  let start: i32 = jsonValueGet(this.first, c)
1869
1953
  const n: i32 = jsonValueGet(this.count, c)
1870
1954
  const capacity: i32 = jsonValueGet(this.room, c)
1955
+ const after: i32 = jsonValueEnd(n, 1)
1956
+ if (after < 0) {
1957
+ return JSON_VALUE_ERR_VALUES
1958
+ }
1871
1959
  if (n === capacity) {
1872
1960
  const grown: i32 =
1873
- capacity < JSON_VALUE_FIRST_SLOTS ? JSON_VALUE_FIRST_SLOTS : toI32(toU32(capacity) * 2)
1874
- if (toI32(toU32(start) + toU32(capacity)) !== this.slotTop) {
1961
+ capacity < JSON_VALUE_FIRST_SLOTS ? JSON_VALUE_FIRST_SLOTS : jsonValueEnd(capacity, capacity)
1962
+ const top: i32 = this.slotTop
1963
+ const inPlace: boolean = jsonValueEnd(start, capacity) === top
1964
+ const grownTop: i32 = jsonValueEnd(inPlace ? start : top, grown)
1965
+ if (grown < 0 || grownTop < 0) {
1966
+ return JSON_VALUE_ERR_VALUES
1967
+ }
1968
+ if (!inPlace) {
1875
1969
  start = this.moveSlots(start, n)
1876
1970
  jsonValuePut(this.first, c, start)
1877
1971
  }
1878
- this.slotTop = toI32(toU32(start) + toU32(grown))
1972
+ this.slotTop = grownTop
1879
1973
  this.slotValue = jsonValueRoomI32(this.slotValue, this.slotTop)
1880
1974
  this.slotKeyAt = jsonValueRoomI32(this.slotKeyAt, this.slotTop)
1881
1975
  this.slotKeyLength = jsonValueRoomI32(this.slotKeyLength, this.slotTop)
1882
1976
  jsonValuePut(this.room, c, grown)
1883
1977
  }
1884
- const s: i32 = toI32(toU32(start) + toU32(n))
1978
+ const s: i32 = jsonValueEnd(start, n)
1979
+ if (s < 0) {
1980
+ return JSON_VALUE_ERR_VALUES
1981
+ }
1885
1982
  jsonValuePut(this.slotValue, s, v)
1886
1983
  jsonValuePut(this.slotKeyAt, s, keyAt)
1887
1984
  jsonValuePut(this.slotKeyLength, s, keyLength)
1888
- jsonValuePut(this.count, c, toI32(toU32(n) + 1))
1985
+ jsonValuePut(this.count, c, after)
1889
1986
  jsonValuePut(this.parent, v, c)
1890
1987
  return s
1891
1988
  }
@@ -1904,12 +2001,11 @@ export class JsonDocument {
1904
2001
  if (n <= 0 || top < 0 || jsonValueGet(this.first, c) !== top || jsonValueGet(this.room, c) !== 0) {
1905
2002
  return
1906
2003
  }
1907
- // Both below 2^31, so the `u32` sum is exact.
1908
- const reserved: u32 = toU32(top) + toU32(n)
1909
- if (reserved > toU32(JSON_VALUE_RESERVE_LIMIT)) {
2004
+ const reserved: i32 = jsonValueEnd(top, n)
2005
+ if (reserved < 0 || reserved > JSON_VALUE_RESERVE_LIMIT) {
1910
2006
  return
1911
2007
  }
1912
- this.slotTop = toI32(reserved)
2008
+ this.slotTop = reserved
1913
2009
  this.slotValue = jsonValueRoomI32(this.slotValue, this.slotTop)
1914
2010
  this.slotKeyAt = jsonValueRoomI32(this.slotKeyAt, this.slotTop)
1915
2011
  this.slotKeyLength = jsonValueRoomI32(this.slotKeyLength, this.slotTop)
@@ -1935,12 +2031,10 @@ export class JsonDocument {
1935
2031
  if (from < 0 || to < from || to > toI32(text.length) || at < 0) {
1936
2032
  return -1
1937
2033
  }
1938
- // `from <= to`, so the `u32` difference is exact.
1939
- const n: i32 = toI32(toU32(to) - toU32(from))
1940
- // Both below 2^31, so the `u32` sum is exact; past 2^31 - 1 it turns
1941
- // negative, below `at`, and the store is full.
1942
- const end: i32 = toI32(toU32(at) + toU32(n))
1943
- if (end < at) {
2034
+ // `from <= to`, so the span is found; an end past 2^31 - 1 is `-1`, and the store is full.
2035
+ const n: i32 = jsonValueSpan(from, to)
2036
+ const end: i32 = jsonValueEnd(at, n)
2037
+ if (end < 0) {
1944
2038
  return -1
1945
2039
  }
1946
2040
  const buf: u8[] = jsonValueRoom(this.bytes, end)
@@ -1982,11 +2076,10 @@ export class JsonDocument {
1982
2076
  if (from < 0 || to < from || to > toI32(text.length) || at < 0) {
1983
2077
  return -1
1984
2078
  }
1985
- // As in `storeSpan`: `from <= to` keeps the difference exact, and an
1986
- // end below `at` is past 2^31 - 1.
1987
- const n: i32 = toI32(toU32(to) - toU32(from))
1988
- const end: i32 = toI32(toU32(at) + toU32(n))
1989
- if (end < at) {
2079
+ // As in `storeSpan`: the span is found, and a `-1` end is past 2^31 - 1.
2080
+ const n: i32 = jsonValueSpan(from, to)
2081
+ const end: i32 = jsonValueEnd(at, n)
2082
+ if (end < 0) {
1990
2083
  return -1
1991
2084
  }
1992
2085
  const buf: u8[] = jsonValueRoom(this.bytes, end)
@@ -2043,9 +2136,9 @@ export class JsonDocument {
2043
2136
  private moveSlots(start: i32, n: i32): i32 {
2044
2137
  const moved: i32 = this.slotTop
2045
2138
  for (let i: i32 = 0; i < n; i += 1) {
2046
- // The slots stay under 2^31, as `append` says, so neither sum wraps.
2047
- const from: i32 = toI32(toU32(start) + toU32(i))
2048
- const to: i32 = toI32(toU32(moved) + toU32(i))
2139
+ // `append` has found the grown run's end past `moved + n`, so neither is `-1`.
2140
+ const from: i32 = jsonValueEnd(start, i)
2141
+ const to: i32 = jsonValueEnd(moved, i)
2049
2142
  jsonValuePut(this.slotValue, to, jsonValueGet(this.slotValue, from))
2050
2143
  jsonValuePut(this.slotKeyAt, to, jsonValueGet(this.slotKeyAt, from))
2051
2144
  jsonValuePut(this.slotKeyLength, to, jsonValueGet(this.slotKeyLength, from))
@@ -2071,15 +2164,15 @@ export class JsonDocument {
2071
2164
  jsonValuePut(r.slotKeyStart, t, -1)
2072
2165
  jsonValuePut(r.slotKeyEnd, t, -1)
2073
2166
  }
2074
- // The slots stay under 2^31, as `append` says, so no sum here wraps.
2167
+ // `append` has found the grown run's end past `to + n`, so no sum here is `-1`.
2075
2168
  for (let i: i32 = 0; i < n; i += 1) {
2076
- const a: i32 = toI32(toU32(from) + toU32(i))
2077
- const b: i32 = toI32(toU32(to) + toU32(i))
2169
+ const a: i32 = jsonValueEnd(from, i)
2170
+ const b: i32 = jsonValueEnd(to, i)
2078
2171
  const has: boolean = a < top
2079
2172
  jsonValuePut(r.slotKeyStart, b, has ? jsonValueGet(r.slotKeyStart, a) : -1)
2080
2173
  jsonValuePut(r.slotKeyEnd, b, has ? jsonValueGet(r.slotKeyEnd, a) : -1)
2081
2174
  }
2082
- this.slotRangeTop = toI32(toU32(to) + toU32(n))
2175
+ this.slotRangeTop = jsonValueEnd(to, n)
2083
2176
  }
2084
2177
  }
2085
2178
 
@@ -2096,10 +2189,9 @@ export class JsonDocument {
2096
2189
  storeText(s: string): i32 {
2097
2190
  const n: i32 = toI32(s.length)
2098
2191
  const at: i32 = this.byteTop
2099
- // Both below 2^31, so the `u32` sum is exact; past 2^31 - 1 it turns
2100
- // negative, below `at`, and the store is full.
2101
- const end: i32 = toI32(toU32(at) + toU32(n))
2102
- if (end < at) {
2192
+ // Past 2^31 - 1 the end is `-1`, and the store is full.
2193
+ const end: i32 = jsonValueEnd(at, n)
2194
+ if (end < 0) {
2103
2195
  return -1
2104
2196
  }
2105
2197
  const buf: u8[] = jsonValueRoom(this.bytes, end)
@@ -2148,8 +2240,8 @@ export class JsonDocument {
2148
2240
  // A container: its members are the slots from `first`, read directly.
2149
2241
  const object: boolean = k === JSON_VALUE_OBJECT
2150
2242
  const start: i32 = jsonValueGet(this.first, x)
2151
- // The run lies inside the slot arrays, as `slotAt` says.
2152
- const end: i32 = toI32(toU32(start) + toU32(jsonValueGet(this.count, x)))
2243
+ // The run lies inside the slot arrays, as `slotAt` says; a `-1` end would write no member.
2244
+ const end: i32 = jsonValueEnd(start, jsonValueGet(this.count, x))
2153
2245
  if (object) {
2154
2246
  w.beginObject()
2155
2247
  } else {
@@ -2182,26 +2274,40 @@ export class JsonDocument {
2182
2274
  stringify(v: JsonValue): string {
2183
2275
  // Every byte stored and a few of punctuation per value: one pass fits most
2184
2276
  // documents, and a text with many escapes doubles from there.
2185
- // In `u32`, which cannot wrap: under 2^31 bytes and 8 * 2^22 values. The
2186
- // longest array caps it.
2187
- const guess: u32 = toU32(this.byteTop) + 8 * toU32(this.top) + 64
2188
- let size: u32 = guess < toU32(JSON_VALUE_I32_MAX) ? guess : toU32(JSON_VALUE_I32_MAX)
2189
- while (true) {
2277
+ // In `i64`, where the sum of three `i32`s fits; a guess past the
2278
+ // longest array asks for the longest.
2279
+ const guess: i64 = toI64(this.byteTop) + toI64(8) * toI64(this.top) + toI64(64)
2280
+ let size: i32 = guess < toI64(JSON_VALUE_I32_MAX) ? toI32(guess) : JSON_VALUE_I32_MAX
2281
+ // Never false at the start, as the counts are never below zero, and
2282
+ // never false after a doubling; it states the length is a length.
2283
+ while (size >= 0) {
2190
2284
  const buf: u8[] = new Array<u8>(size)
2191
2285
  const w: JsonWriter = new JsonWriter(buf, 0)
2192
2286
  this.write(v, w)
2193
- if (w.failed !== JSON_WRITE_FULL || size > toU32(JSON_VALUE_I32_MAX / 2)) {
2287
+ // Past half of `JSON_VALUE_I32_MAX`, a doubling would not fit. A literal,
2288
+ // because the prover reads a literal's value and not a constant's here.
2289
+ if (w.failed !== JSON_WRITE_FULL || size > 1073741823) {
2194
2290
  const end: i32 = w.end
2195
2291
  return w.complete() && end >= 0 && end <= toI32(buf.length) ? String.fromBytes(buf, 0, end) : ""
2196
2292
  }
2197
2293
  size = size * 2
2198
2294
  }
2295
+ return ""
2199
2296
  }
2200
2297
  }
2201
2298
 
2202
2299
  /** `a[i]`, or `-1` when `i` is outside `a`. */
2203
2300
  const jsonValueGet = (a: i32[], i: i32): i32 => (i >= 0 && i < toI32(a.length) ? a[i] : -1)
2204
2301
 
2302
+ /**
2303
+ * `a[at + i]`, or `-1` when `at` or `i` is negative or the sum is outside `a`;
2304
+ * the sum is in `i64`, so it cannot overflow.
2305
+ */
2306
+ const jsonValueGetAt = (a: i32[], at: i32, i: i32): i32 => {
2307
+ const s: i64 = toI64(at) + toI64(i)
2308
+ return at >= 0 && i >= 0 && s < toI64(toI32(a.length)) ? jsonValueGet(a, toI32(s)) : -1
2309
+ }
2310
+
2205
2311
  /** `a[i] = x`, growing `a` by one when `i` is its length; an `i` further out writes nothing. */
2206
2312
  const jsonValuePut = (a: i32[], i: i32, x: i32): void => {
2207
2313
  if (i >= 0 && i < toI32(a.length)) {
@@ -2211,6 +2317,18 @@ const jsonValuePut = (a: i32[], i: i32, x: i32): void => {
2211
2317
  }
2212
2318
  }
2213
2319
 
2320
+ /**
2321
+ * `at + n`, the end of `n` items from `at`, or `-1` when either is negative
2322
+ * or the end is past 2^31 - 1, which no array of the document reaches.
2323
+ */
2324
+ const jsonValueEnd = (at: i32, n: i32): i32 => {
2325
+ const end: i64 = toI64(at) + toI64(n)
2326
+ return at < 0 || n < 0 || end > 2147483647 ? -1 : toI32(end)
2327
+ }
2328
+
2329
+ /** `to - from`, the items in `[from, to)`, or `-1` when either is negative or `to < from`. */
2330
+ const jsonValueSpan = (from: i32, to: i32): i32 => (from < 0 || to < from ? -1 : to - from)
2331
+
2214
2332
  /**
2215
2333
  * `a` when it holds at least `n` elements, and otherwise a copy of it at
2216
2334
  * least twice as long, for the caller to keep in `a`'s place. Doubling is