@amritk/nish-aarch64-linux 0.34.1 → 0.36.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 (126) hide show
  1. package/INSTALL.md +22 -13
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +86 -16
  4. package/package.json +1 -1
  5. package/runtime/async.d.mts +49 -0
  6. package/runtime/async.mjs +27 -0
  7. package/runtime/nish.d.ts +4681 -427
  8. package/runtime/nish.h +6 -2
  9. package/runtime/nish.mjs +4 -0
  10. package/runtime/runtime-net.c +22 -0
  11. package/runtime/runtime-shm.c +93 -2
  12. package/runtime/vite.d.ts +30 -0
  13. package/runtime/vite.mjs +150 -0
  14. package/scripts/build.sh +108 -13
  15. package/std/README.md +131 -131
  16. package/std/base64.ts +34 -5
  17. package/std/bytes.ts +258 -42
  18. package/std/cli-args.ts +296 -70
  19. package/std/cli-flags.ts +184 -19
  20. package/std/cli.ts +475 -54
  21. package/std/collections.ts +121 -23
  22. package/std/compress/checksum.ts +44 -14
  23. package/std/compress/deflate.ts +224 -111
  24. package/std/compress/gzip.ts +217 -53
  25. package/std/compress/zlib.ts +63 -7
  26. package/std/crypto/aes.ts +80 -9
  27. package/std/crypto/base64url.ts +22 -0
  28. package/std/crypto/bcrypt-batch.ts +88 -21
  29. package/std/crypto/bcrypt.ts +60 -0
  30. package/std/crypto/chacha20poly1305.ts +53 -0
  31. package/std/crypto/ct.ts +32 -13
  32. package/std/crypto/ed25519.ts +47 -0
  33. package/std/crypto/hkdf.ts +198 -13
  34. package/std/crypto/hmac.ts +235 -31
  35. package/std/crypto/jwt.ts +192 -63
  36. package/std/crypto/p256.ts +159 -16
  37. package/std/crypto/p384.ts +28 -3
  38. package/std/crypto/pki-roots.ts +32 -2
  39. package/std/crypto/pki.ts +81 -15
  40. package/std/crypto/rsa.ts +33 -3
  41. package/std/crypto/sha1.ts +3 -0
  42. package/std/crypto/sha256.ts +24 -1
  43. package/std/crypto/sha512.ts +99 -14
  44. package/std/crypto/x25519.ts +152 -14
  45. package/std/crypto/x509-random.ts +10 -0
  46. package/std/crypto/x509.ts +67 -7
  47. package/std/json-equal.ts +19 -0
  48. package/std/json-format.ts +112 -13
  49. package/std/json-number.ts +64 -14
  50. package/std/json-pointer.ts +23 -1
  51. package/std/json-tokenizer.ts +100 -29
  52. package/std/json-typed.ts +713 -66
  53. package/std/json-value.ts +400 -124
  54. package/std/json.ts +138 -10
  55. package/std/map.ts +25 -7
  56. package/std/math.ts +36 -4
  57. package/std/net/buffer-pool.ts +42 -11
  58. package/std/net/dns-config.ts +109 -23
  59. package/std/net/dns-message.ts +62 -7
  60. package/std/net/dns-resolver.ts +247 -53
  61. package/std/net/dns.ts +203 -35
  62. package/std/net/fetch.ts +151 -61
  63. package/std/net/handler.ts +278 -80
  64. package/std/net/hpack.ts +262 -52
  65. package/std/net/http-compress.ts +125 -15
  66. package/std/net/http-fields.ts +85 -9
  67. package/std/net/http1-client-codec.ts +100 -19
  68. package/std/net/http1-codec.ts +297 -77
  69. package/std/net/http1-server.ts +430 -94
  70. package/std/net/http2-frame.ts +272 -35
  71. package/std/net/http2-tls.ts +135 -34
  72. package/std/net/http2.ts +247 -106
  73. package/std/net/http3-frame.ts +154 -17
  74. package/std/net/http3-server.ts +275 -101
  75. package/std/net/http3.ts +307 -115
  76. package/std/net/outbound.ts +37 -3
  77. package/std/net/postgres-carrier.ts +144 -8
  78. package/std/net/postgres.ts +801 -112
  79. package/std/net/prefork.ts +164 -31
  80. package/std/net/qpack.ts +105 -18
  81. package/std/net/quic-conn-ack.ts +59 -12
  82. package/std/net/quic-conn-cid.ts +124 -27
  83. package/std/net/quic-conn-params.ts +70 -10
  84. package/std/net/quic-datagram.ts +43 -3
  85. package/std/net/quic-frame.ts +286 -12
  86. package/std/net/quic-listener.ts +101 -32
  87. package/std/net/quic-packet.ts +302 -21
  88. package/std/net/quic-recovery.ts +264 -52
  89. package/std/net/quic-stream.ts +353 -91
  90. package/std/net/quic.ts +1124 -201
  91. package/std/net/router.ts +87 -40
  92. package/std/net/tls/client.ts +166 -51
  93. package/std/net/tls/codec.ts +413 -56
  94. package/std/net/tls/record-client.ts +110 -55
  95. package/std/net/tls/record-server.ts +159 -59
  96. package/std/net/tls/record.ts +109 -12
  97. package/std/net/tls/schedule.ts +235 -26
  98. package/std/net/tls/ticket.ts +159 -40
  99. package/std/net/tls/verify.ts +49 -3
  100. package/std/net/tls-tcp.ts +250 -45
  101. package/std/net/tls.ts +221 -59
  102. package/std/net/upstream.ts +81 -45
  103. package/std/net/url.ts +97 -13
  104. package/std/net/websocket.ts +122 -12
  105. package/std/net/webtransport.ts +220 -89
  106. package/std/pair.ts +10 -2
  107. package/std/random.ts +34 -9
  108. package/std/regex.ts +352 -198
  109. package/std/region.ts +70 -15
  110. package/std/secret.ts +52 -11
  111. package/std/sort.ts +17 -2
  112. package/std/sqlite.ts +256 -35
  113. package/std/sync/ring.ts +430 -40
  114. package/std/sync/shm.ts +222 -15
  115. package/std/term-io.ts +137 -64
  116. package/std/term-pixels.ts +144 -19
  117. package/std/term-probe.ts +66 -25
  118. package/std/term-rate.ts +128 -57
  119. package/std/term.ts +332 -121
  120. package/std/testing.ts +130 -32
  121. package/std/text.ts +73 -7
  122. package/std/threads.ts +229 -41
  123. package/std/yaml-json.ts +94 -82
  124. package/std/yaml-lines.ts +47 -15
  125. package/std/yaml-write.ts +24 -16
  126. package/std/yaml.ts +764 -384
package/std/json.ts CHANGED
@@ -612,11 +612,19 @@ const jsonKeyEquals = (text: string, at: i32, end: i32, name: string): boolean =
612
612
  * what keeps it a stack slot rather than an arena allocation.
613
613
  */
614
614
  class JsonMember {
615
+ /** @internal Set by `jsonReadMember` and read by `jsonField` and `jsonFields`. */
615
616
  keyAt: i32 = 0
617
+ /** @internal Set by `jsonReadMember` and read by `jsonField` and `jsonFields`. */
616
618
  keyEnd: i32 = 0
619
+ /** @internal Set by `jsonReadMember` and read by `jsonField` and `jsonFields`. */
617
620
  valueAt: i32 = 0
621
+ /** @internal Set by `jsonReadMember` and read by `jsonField` and `jsonFields`. */
618
622
  valueEnd: i32 = 0
619
- /** The opening quote of the next key, or `-1` when no comma follows the value. */
623
+ /**
624
+ * The opening quote of the next key, or `-1` when no comma follows the value.
625
+ *
626
+ * @internal Set by `jsonReadMember` and read by `jsonField` and `jsonFields`.
627
+ */
620
628
  next: i32 = -1
621
629
  }
622
630
 
@@ -711,6 +719,10 @@ const jsonValueText = (object: string, member: JsonMember): string => {
711
719
  * A nested value is stepped over rather than searched, so `name` is a field of
712
720
  * this object and not a path into it: that is the "flat" in the module header,
713
721
  * and it is the shape the compiler's `--json` line has.
722
+ *
723
+ * @param object - the text of one JSON object.
724
+ * @param name - the field to look for, matched against each key's decoded text.
725
+ * @returns the first such field's value, or `null` when there is none.
714
726
  */
715
727
  export const jsonField = (object: string, name: string): string | null => {
716
728
  const member: JsonMember = new JsonMember()
@@ -751,6 +763,11 @@ export const jsonField = (object: string, name: string): string | null => {
751
763
  * Reading `values` back, passing it to a function, or storing it anywhere
752
764
  * before the return would undo this; `tests/link/std_json` pins the flat
753
765
  * arena and the `using` block.
766
+ *
767
+ * @param object - the text of one JSON object.
768
+ * @param names - the fields to look for.
769
+ * @returns one value per name, in the order of `names`, each `null` when that
770
+ * field is missing.
754
771
  */
755
772
  export const jsonFields = (object: string, names: string[]): (string | null)[] => {
756
773
  const count: i32 = toI32(names.length)
@@ -901,8 +918,9 @@ const jsonEscapedWidth = (code: i32): i32 => {
901
918
  * a caller writes the whole value and reads `failed` once at the end.
902
919
  */
903
920
  export class JsonWriter {
921
+ /** The caller's buffer the text is written into; the writer never grows it. */
904
922
  buf: u8[]
905
- start: i32 = 0
923
+ private start: i32 = 0
906
924
  /** The cursor: the bytes written are `buf[start .. end)`. */
907
925
  end: i32 = 0
908
926
  /** `JSON_WRITE_OK`, or the first `JSON_WRITE_*` code a call hit. */
@@ -918,8 +936,15 @@ export class JsonWriter {
918
936
  * would save. Two `i32` words rather than an `i64` or an array, so that a
919
937
  * writer allocates nothing and the module still runs under Node, where an
920
938
  * `i64` is a BigInt.
939
+ *
940
+ * @internal Read and written by the `jsonWriter*` free functions of this
941
+ * module, which do the writer's work.
921
942
  */
922
943
  objectsLow: i32 = 0
944
+ /**
945
+ * @internal Read and written by the `jsonWriter*` free functions of this
946
+ * module, which do the writer's work.
947
+ */
923
948
  objectsHigh: i32 = 0
924
949
  /**
925
950
  * What goes before the next key or element of the innermost container,
@@ -927,6 +952,9 @@ export class JsonWriter {
927
952
  * line break or a comma and a line break. The minified text tests it only
928
953
  * where it would test a "comma due" flag, against 0 and 1, so the indented
929
954
  * states cost it nothing; bit 1 is the indent and every step keeps it.
955
+ *
956
+ * @internal Read and written by the `jsonWriter*` free functions of this
957
+ * module, which do the writer's work.
930
958
  */
931
959
  sep: i32 = 0
932
960
  /**
@@ -935,15 +963,37 @@ export class JsonWriter {
935
963
  * keeps it, so a writer set up once writes every text the same way.
936
964
  */
937
965
  indentWidth: i32 = 0
938
- /** The byte an indent is made of: a space, or a tab after `setIndentTab`. */
966
+ /**
967
+ * The byte an indent is made of: a space, or a tab after `setIndentTab`.
968
+ *
969
+ * @internal Read and written by the `jsonWriter*` free functions of this
970
+ * module, which do the writer's work.
971
+ */
939
972
  indentByte: i32 = 32
940
- /** Whether a key has been written in the innermost object and its value is due. */
973
+ /**
974
+ * Whether a key has been written in the innermost object and its value is
975
+ * due.
976
+ *
977
+ * @internal Read and written by the `jsonWriter*` free functions of this
978
+ * module, which do the writer's work.
979
+ */
941
980
  afterKey: boolean = false
942
- /** Whether the top-level value has been written whole. */
981
+ /**
982
+ * Whether the top-level value has been written whole.
983
+ *
984
+ * @internal Read and written by the `jsonWriter*` free functions of this
985
+ * module, which do the writer's work.
986
+ */
943
987
  done: boolean = false
944
988
  /** Whether a NaN or infinite `f64` is `JSON_WRITE_NON_FINITE` rather than `null`. Kept by `reset`. */
945
989
  strict: boolean = false
946
990
 
991
+ /**
992
+ * A writer that starts its text at `buf[start ..)`, as `reset` does.
993
+ *
994
+ * @param buf - the buffer to write into, which the caller keeps.
995
+ * @param start - the offset in `buf` the text starts at.
996
+ */
947
997
  constructor(buf: u8[], start: i32) {
948
998
  this.buf = buf
949
999
  this.reset(buf, start)
@@ -953,6 +1003,9 @@ export class JsonWriter {
953
1003
  * Starts a new text at `buf[start ..)`, forgetting everything the last one
954
1004
  * wrote and every error it hit: what a server calls once per response, on
955
1005
  * the buffer it reuses. A `start` outside `buf` is `JSON_WRITE_BAD_WINDOW`.
1006
+ *
1007
+ * @param buf - the buffer to write into, which the caller keeps.
1008
+ * @param start - the offset in `buf` the text starts at.
956
1009
  */
957
1010
  reset(buf: u8[], start: i32): void {
958
1011
  this.buf = buf
@@ -974,6 +1027,8 @@ export class JsonWriter {
974
1027
  * is clamped to 10 as `JSON.stringify` clamps it, and below 1 is the
975
1028
  * minified text, the default. A mode that `reset` keeps; set it before the
976
1029
  * text, since a change in the middle of one applies from the next byte.
1030
+ *
1031
+ * @param spaces - the spaces per level, 1 to 10; anything less is minified.
977
1032
  */
978
1033
  setIndent(spaces: i32): void {
979
1034
  let width: i32 = spaces
@@ -1001,17 +1056,29 @@ export class JsonWriter {
1001
1056
  * the `null` `JSON.stringify` writes. Off by default; a mode that `reset`
1002
1057
  * keeps. Strict is for a caller to whom a `null` where a number was due is
1003
1058
  * a corrupt record rather than a missing one.
1059
+ *
1060
+ * @param strict - true to fail on a NaN or an infinity, false to write
1061
+ * `null`.
1004
1062
  */
1005
1063
  setStrict(strict: boolean): void {
1006
1064
  this.strict = strict
1007
1065
  }
1008
1066
 
1009
- /** How many bytes have been written since `reset`. */
1067
+ /**
1068
+ * How many bytes have been written since `reset`.
1069
+ *
1070
+ * @returns the byte count, `end - start`.
1071
+ */
1010
1072
  written(): i32 {
1011
1073
  return this.end - this.start
1012
1074
  }
1013
1075
 
1014
- /** Whether one whole value has been written, every container closed, and nothing failed. */
1076
+ /**
1077
+ * Whether one whole value has been written, every container closed, and
1078
+ * nothing failed.
1079
+ *
1080
+ * @returns true when the text is one whole JSON value.
1081
+ */
1015
1082
  complete(): boolean {
1016
1083
  return this.failed === JSON_WRITE_OK && this.done
1017
1084
  }
@@ -1020,6 +1087,8 @@ export class JsonWriter {
1020
1087
  * The bytes written so far, `buf[start .. end)`, as a string. A convenience
1021
1088
  * that allocates a byte at a time, for a caller that wants text rather than
1022
1089
  * bytes; a server sends `buf` itself.
1090
+ *
1091
+ * @returns a new string of the bytes written.
1023
1092
  */
1024
1093
  text(): string {
1025
1094
  const buf: u8[] = this.buf
@@ -1030,10 +1099,21 @@ export class JsonWriter {
1030
1099
  return parts.join("")
1031
1100
  }
1032
1101
 
1102
+ /**
1103
+ * Opens an object where a value is due. `JSON_WRITE_KEY_EXPECTED` inside an
1104
+ * object before its key, `JSON_WRITE_TOO_DEEP` past `JSON_WRITER_MAX_DEPTH`
1105
+ * open containers, `JSON_WRITE_DONE` after the top-level value, and
1106
+ * `JSON_WRITE_FULL` when `buf` has no room.
1107
+ */
1033
1108
  beginObject(): void {
1034
1109
  jsonWriterOpen(this, true, JSON_OPEN_BRACE)
1035
1110
  }
1036
1111
 
1112
+ /**
1113
+ * Closes the innermost object. `JSON_WRITE_UNBALANCED` when that is not an
1114
+ * object, or nothing is open, and `JSON_WRITE_VALUE_EXPECTED` when a key is
1115
+ * still waiting for its value.
1116
+ */
1037
1117
  endObject(): void {
1038
1118
  if (this.sep === JSON_WRITER_SEP_COMMA_BREAK) {
1039
1119
  jsonWriterCloseIndented(this, true, JSON_CLOSE_BRACE)
@@ -1042,10 +1122,15 @@ export class JsonWriter {
1042
1122
  }
1043
1123
  }
1044
1124
 
1125
+ /** Opens an array where a value is due, with the same errors as `beginObject`. */
1045
1126
  beginArray(): void {
1046
1127
  jsonWriterOpen(this, false, JSON_OPEN_BRACKET)
1047
1128
  }
1048
1129
 
1130
+ /**
1131
+ * Closes the innermost array. `JSON_WRITE_UNBALANCED` when that is not an
1132
+ * array, or nothing is open.
1133
+ */
1049
1134
  endArray(): void {
1050
1135
  if (this.sep === JSON_WRITER_SEP_COMMA_BREAK) {
1051
1136
  jsonWriterCloseIndented(this, false, JSON_CLOSE_BRACKET)
@@ -1054,7 +1139,11 @@ export class JsonWriter {
1054
1139
  }
1055
1140
  }
1056
1141
 
1057
- /** The next member's key, with the comma before it and the colon after it. */
1142
+ /**
1143
+ * The next member's key, with the comma before it and the colon after it.
1144
+ *
1145
+ * @param name - the key, escaped as `string` escapes a value.
1146
+ */
1058
1147
  key(name: string): void {
1059
1148
  if (this.failed !== JSON_WRITE_OK) {
1060
1149
  return
@@ -1082,7 +1171,12 @@ export class JsonWriter {
1082
1171
  }
1083
1172
  }
1084
1173
 
1085
- /** `s` quoted and escaped as `JSON.stringify` escapes it; every other byte goes through as itself. */
1174
+ /**
1175
+ * `s` quoted and escaped as `JSON.stringify` escapes it; every other byte
1176
+ * goes through as itself.
1177
+ *
1178
+ * @param s - the text to write.
1179
+ */
1086
1180
  string(s: string): void {
1087
1181
  if (jsonWriterValueDue(this) && jsonWriterPutString(this, s)) {
1088
1182
  jsonWriterValueDone(this)
@@ -1096,6 +1190,10 @@ export class JsonWriter {
1096
1190
  * `JsonDocument`, writes without making a string per key. A window outside
1097
1191
  * `bytes` is `JSON_WRITE_BAD_WINDOW`, checked once, before anything is
1098
1192
  * written.
1193
+ *
1194
+ * @param bytes - the array holding the text.
1195
+ * @param off - the offset in `bytes` of its first byte.
1196
+ * @param len - how many bytes it is.
1099
1197
  */
1100
1198
  keyBytes(bytes: u8[], off: i32, len: i32): void {
1101
1199
  if (this.failed !== JSON_WRITE_OK) {
@@ -1135,6 +1233,10 @@ export class JsonWriter {
1135
1233
  * quotes and escapes, byte for byte, and no allocation. A window outside
1136
1234
  * `bytes` is `JSON_WRITE_BAD_WINDOW`, checked once, before anything is
1137
1235
  * written.
1236
+ *
1237
+ * @param bytes - the array holding the text.
1238
+ * @param off - the offset in `bytes` of its first byte.
1239
+ * @param len - how many bytes it is.
1138
1240
  */
1139
1241
  stringBytes(bytes: u8[], off: i32, len: i32): void {
1140
1242
  if (this.failed !== JSON_WRITE_OK) {
@@ -1148,6 +1250,11 @@ export class JsonWriter {
1148
1250
  }
1149
1251
  }
1150
1252
 
1253
+ /**
1254
+ * `v` in decimal, as `String(v)` spells it.
1255
+ *
1256
+ * @param v - the value to write.
1257
+ */
1151
1258
  i32(v: i32): void {
1152
1259
  if (jsonWriterValueDue(this) && jsonWriterPutDigits(this, v, -1)) {
1153
1260
  jsonWriterValueDone(this)
@@ -1157,6 +1264,8 @@ export class JsonWriter {
1157
1264
  /**
1158
1265
  * One past `i32`'s range is ten digits, so the last digit is split off in
1159
1266
  * `u32` and the nine before it are an `i32`'s.
1267
+ *
1268
+ * @param v - the value to write.
1160
1269
  */
1161
1270
  u32(v: u32): void {
1162
1271
  if (!jsonWriterValueDue(this)) {
@@ -1177,7 +1286,12 @@ export class JsonWriter {
1177
1286
  }
1178
1287
  }
1179
1288
 
1180
- /** The digits of `v`, the minimum included: the arithmetic stays on the negative side, where it fits. */
1289
+ /**
1290
+ * The digits of `v`, the minimum included: the arithmetic stays on the
1291
+ * negative side, where it fits.
1292
+ *
1293
+ * @param v - the value to write.
1294
+ */
1181
1295
  i64(v: i64): void {
1182
1296
  if (jsonWriterValueDue(this) && jsonWriterPutDigits64(this, v)) {
1183
1297
  jsonWriterValueDone(this)
@@ -1189,6 +1303,8 @@ export class JsonWriter {
1189
1303
  * shortest digits that read back as `v`, `-0` as `0`, and `null` for NaN
1190
1304
  * and both infinities, which JSON cannot spell — or, once `setStrict(true)`,
1191
1305
  * `JSON_WRITE_NON_FINITE` for those three, with nothing written.
1306
+ *
1307
+ * @param v - the value to write.
1192
1308
  */
1193
1309
  f64(v: f64): void {
1194
1310
  // `v - v` is `0` for every finite `v`, and NaN for NaN and both infinities.
@@ -1203,6 +1319,11 @@ export class JsonWriter {
1203
1319
  }
1204
1320
  }
1205
1321
 
1322
+ /**
1323
+ * `true` or `false`.
1324
+ *
1325
+ * @param v - the value to write.
1326
+ */
1206
1327
  boolean(v: boolean): void {
1207
1328
  if (jsonWriterValueDue(this) && jsonWriterPutText(this, v ? "true" : "false")) {
1208
1329
  jsonWriterValueDone(this)
@@ -1226,6 +1347,10 @@ export class JsonWriter {
1226
1347
  * In an indented text the fragment goes in where a value would, after its
1227
1348
  * line break and indent, but its own bytes are not re-indented: an object
1228
1349
  * fragment stays on one line, where `JSON.stringify` would spread it out.
1350
+ *
1351
+ * @param bytes - the array holding the fragment.
1352
+ * @param off - the offset in `bytes` of its first byte.
1353
+ * @param len - how many bytes it is.
1229
1354
  */
1230
1355
  raw(bytes: u8[], off: i32, len: i32): void {
1231
1356
  if (this.failed !== JSON_WRITE_OK) {
@@ -1836,6 +1961,9 @@ const jsonWriterPutF64 = (w: JsonWriter, v: f64): boolean => {
1836
1961
  * `JsonWriter.string` writes — as a string. A thin layer on the writer: it
1837
1962
  * sizes a buffer to fit exactly, writes into it and reads it back. A string
1838
1963
  * with nothing to escape skips the writer and is only quoted.
1964
+ *
1965
+ * @param s - the text to quote.
1966
+ * @returns the quoted, escaped text.
1839
1967
  */
1840
1968
  export const jsonStringifyString = (s: string): string => {
1841
1969
  const n: i32 = toI32(s.length)
package/std/map.ts CHANGED
@@ -3,15 +3,24 @@
3
3
  * have, written so that a program using them still runs unmodified under Node
4
4
  * (docs/wp32-map.md §9.2).
5
5
  *
6
- * The bodies below are the meaning, and they are what runs under Node:
6
+ * import { reserve, getOrInsert } from "nish/map"
7
+ *
8
+ * const counts = new Map<string, i32>()
9
+ * reserve(counts, 64) // room for 64 words before the table grows
10
+ * for (const w of ["to", "be", "or", "not", "to", "be"]) {
11
+ * counts.set(w, getOrInsert(counts, w, 0) + 1) // a new word starts at 0
12
+ * }
13
+ * counts.get("to") // 2
14
+ *
15
+ * **The bodies below are the meaning**, and they are what runs under Node:
7
16
  * `reserve` does nothing, and `getOrInsert` is a `get`, and a `set` when the
8
- * key was missing. Natively neither body is ever called. The compiler lowers
9
- * every call in place, as it lowers `hashKey` and `sameKey`: `reserve` is the
10
- * table's `reserveSlots`, which grows the buckets so that `n` entries fit
11
- * without a rebuild, and `getOrInsert` is one `probe` of the key, then the
17
+ * key was missing. **Natively neither body is ever called.** The compiler
18
+ * lowers every call in place, as it lowers `hashKey` and `sameKey`: `reserve`
19
+ * is the table's `reserveSlots`, which grows the buckets so that `n` entries
20
+ * fit without a rebuild, and `getOrInsert` is one `probe` of the key, then the
12
21
  * value it found or an insert through the empty bucket the probe stopped at,
13
- * with the hash it already computed. So this module writes no `.ll` of its
14
- * own, and a one-file program that imports it is still one module.
22
+ * with the hash it already computed. So this module writes no `.ll` of its own,
23
+ * and a one-file program that imports it is still one module.
15
24
  */
16
25
 
17
26
  /**
@@ -19,6 +28,10 @@
19
28
  * grows the table. Only the speed of a program depends on it: natively it
20
29
  * presizes the bucket table, and under Node, which has no such call, it does
21
30
  * nothing. A count that is not positive does nothing either way.
31
+ *
32
+ * @param _m - the map to presize.
33
+ * @param _n - how many entries it should hold, those already in it counted,
34
+ * without growing.
22
35
  */
23
36
  export const reserve = <K, V>(_m: Map<K, V>, _n: number): void => {
24
37
  // Nothing to do here: natively the compiler lowers the call itself, and Node has no table to presize.
@@ -28,6 +41,11 @@ export const reserve = <K, V>(_m: Map<K, V>, _n: number): void => {
28
41
  * The value of `key` in `m`; or, when `key` is missing, `value`, after
29
42
  * setting `key` to it. One hash and one probe natively, whichever it was.
30
43
  * `value` is evaluated either way, as every argument is.
44
+ *
45
+ * @param m - the map to look in, and to add to when `key` is missing.
46
+ * @param key - the key to find.
47
+ * @param value - the value to store when `key` is missing.
48
+ * @returns the value `key` already had, or `value` when it had none.
31
49
  */
32
50
  export const getOrInsert = <K, V>(m: Map<K, V>, key: K, value: V): V => {
33
51
  const found = m.get(key)
package/std/math.ts CHANGED
@@ -3,7 +3,14 @@
3
3
  * rather than libm, so a program answers the same on every target, wasm
4
4
  * included, and the optimiser sees the whole function.
5
5
  *
6
- * A program does not import this module. A call to one of the three is what
6
+ * export const main = (): i32 => {
7
+ * const slope: f64 = Math.tan(0.5);
8
+ * const angle: f64 = Math.atan2(1.0, -1.0); // 3 * pi / 4
9
+ * const side: f64 = Math.hypot(3.0, 4.0); // 5
10
+ * return side === 5.0 ? 0 : 1;
11
+ * };
12
+ *
13
+ * **A program does not import this module.** A call to one of the three is what
7
14
  * loads it (the checker's `importMath`), and the compiler copies each function
8
15
  * here that the call reaches into the calling module as an `internal`
9
16
  * function, so this file writes no `.ll` of its own. Under Node and `tsc` the
@@ -344,7 +351,14 @@ const kernelTan = (x0: f64, y0: f64, odd: boolean): f64 => {
344
351
  const t: f64 = clearLo(a)
345
352
  return t + a * (t * zz + 1.0 + t * vv)
346
353
  }
347
- /** `Math.tan(x)`: fdlibm's reduction and kernel, with `tanLarge` past 2^20 * pi/2. */
354
+ /**
355
+ * `Math.tan(x)`: fdlibm's reduction and kernel, with `tanLarge` past
356
+ * 2^20 * pi/2.
357
+ *
358
+ * @param x The angle, in radians.
359
+ * @returns The tangent of `x`; NaN for a NaN or an infinite `x`, and `x`
360
+ * itself when `|x| < 2^-27`, a zero's sign kept.
361
+ */
348
362
  export const tanF64 = (x: f64): f64 => {
349
363
  const ax: f64 = Math.abs(x)
350
364
  if (ax < 0.7853984832763672) {
@@ -412,7 +426,16 @@ const atanlo = (id: i32): f64 => {
412
426
  }
413
427
  }
414
428
 
415
- /** `Math.atan2(y, x)`: fdlibm's, with a compensated quotient and a double-double pi - atan. */
429
+ /**
430
+ * `Math.atan2(y, x)`: fdlibm's, with a compensated quotient and a
431
+ * double-double pi - atan.
432
+ *
433
+ * @param y The point's vertical coordinate.
434
+ * @param x The point's horizontal coordinate.
435
+ * @returns The angle from the positive x axis to the point `(x, y)`, in
436
+ * radians in [-pi, pi], with JavaScript's value for every signed zero,
437
+ * infinity and NaN.
438
+ */
416
439
  export const atan2F64 = (y: f64, x: f64): f64 => {
417
440
  const piO4: f64 = 7.85398163397448279e-1
418
441
  const piO2: f64 = 1.570796326794896558
@@ -592,7 +615,16 @@ const hypotRest = (ax: f64, ay: f64, a: f64, b: f64): f64 => {
592
615
  return pow2(-600) * hypotCoreF(a * up, b * up)
593
616
  }
594
617
 
595
- /** `Math.hypot(x, y)`: fdlibm's core, rescaled by a power of two outside [1e-150, 1e150]. */
618
+ /**
619
+ * `Math.hypot(x, y)`: fdlibm's core, rescaled by a power of two outside
620
+ * [1e-150, 1e150].
621
+ *
622
+ * @param x One side's length; only its magnitude matters.
623
+ * @param y The other side's length; only its magnitude matters.
624
+ * @returns The square root of `x * x + y * y`, computed without the squares
625
+ * overflowing or underflowing: Infinity when either is infinite, even beside
626
+ * a NaN, and NaN when either is otherwise NaN.
627
+ */
596
628
  export const hypotF64 = (x: f64, y: f64): f64 => {
597
629
  const ax: f64 = Math.abs(x)
598
630
  const ay: f64 = Math.abs(y)
@@ -9,9 +9,10 @@
9
9
  * // ... write out[0 .. used) and send it ...
10
10
  * pool.giveSecret(out, used); // or pool.give(out, used) for bytes that were never secret
11
11
  *
12
- * It has the shape of `nish/net/http1-server`'s `Http1OutputPool` (`take`,
13
- * `give`, `made`, a `u8[][]` free list), so the two read alike and can merge,
14
- * with three things added:
12
+ * **The shape of `Http1OutputPool`.** It has the shape of
13
+ * `nish/net/http1-server`'s `Http1OutputPool` (`take`, `give`, `made`, a
14
+ * `u8[][]` free list), so the two read alike and can merge, with three things
15
+ * added:
15
16
  *
16
17
  * - **A ceiling.** At most `ceiling` buffers ever exist. A server sizes it to
17
18
  * what preallocating every slot would have cost, so a full server behaves as
@@ -26,12 +27,13 @@
26
27
  * buffer, or a give-back with nothing lent is a borrower's bug, and each
27
28
  * panics with the pool's name rather than corrupting the free list quietly.
28
29
  *
29
- * The pool grows to its high-water mark and never shrinks: after warm-up a
30
+ * **It never shrinks.** The pool grows to its high-water mark: after warm-up a
30
31
  * `take` pops the free list and a `give` pushes onto capacity it already has,
31
32
  * so a warmed take/give loop moves the arena by nothing.
32
33
  *
33
- * Private helpers share the importing program's flat symbol namespace
34
- * (`docs/wp26-stdlib.md` §3e), which is why each one carries the module's name.
34
+ * **Helper names.** Private helpers share the importing program's flat symbol
35
+ * namespace (`docs/wp26-stdlib.md` §3e), which is why each one carries the
36
+ * module's name.
35
37
  */
36
38
 
37
39
  /**
@@ -42,13 +44,27 @@
42
44
  */
43
45
  export class BufferPool {
44
46
  // References, then 32-bit fields: the order the layout packs without padding.
45
- /** The buffers given back and not yet taken again; the last one is lent next. */
47
+ /**
48
+ * The buffers given back and not yet taken again; the last one is lent next.
49
+ * @internal Read by std/net/quic-stream.ts, and grown ahead of need by
50
+ * std/net/http3.ts and std/net/http3-server.ts.
51
+ */
46
52
  free: u8[][]
47
- /** Names the pool in every panic, so a leak or a stray buffer says whose it is. */
53
+ /**
54
+ * Names the pool in every panic, so a leak or a stray buffer says whose it is.
55
+ * @internal Read by `bufferPoolCheckGive` in this module to name the pool in its panics.
56
+ */
48
57
  name: string
49
- /** The length of every buffer the pool makes and takes back. */
58
+ /**
59
+ * The length of every buffer the pool makes and takes back.
60
+ * @internal Read by `bufferPoolCheckGive` here, and by std/net/http3-server.ts,
61
+ * std/net/http3.ts and std/net/quic-stream.ts to know how much room a borrowed buffer has.
62
+ */
50
63
  size: i32 = 0
51
- /** The most buffers the pool will ever make. */
64
+ /**
65
+ * The most buffers the pool will ever make.
66
+ * @internal Read by `take` here, and by std/net/quic-stream.ts to cap what it primes and borrows.
67
+ */
52
68
  ceiling: i32 = 0
53
69
  /** How many buffers the pool has made: its high-water mark, since it never frees one. */
54
70
  made: i32 = 0
@@ -59,6 +75,10 @@ export class BufferPool {
59
75
  * `"Http2TlsServer.output"`). Panics, naming the pool, unless `size` is
60
76
  * above 0 and `ceiling` at least 1: a pool of neither makes a buffer
61
77
  * nobody can use or none at all.
78
+ *
79
+ * @param name the label every panic of this pool prints, to say whose buffer leaked
80
+ * @param size how many bytes each buffer holds, the same for every buffer the pool lends
81
+ * @param ceiling the most buffers the pool may ever make, sized to what every slot holding one at once would need
62
82
  */
63
83
  constructor(name: string, size: i32, ceiling: i32) {
64
84
  if (size <= 0) {
@@ -82,6 +102,8 @@ export class BufferPool {
82
102
  * Panics when all `ceiling` buffers are lent: the ceiling is what every
83
103
  * slot holding a buffer at once would need, so one more means a buffer was
84
104
  * never given back.
105
+ *
106
+ * @returns a buffer of `size` bytes the caller now borrows until it hands it back through `give` or `giveSecret`
85
107
  */
86
108
  take(): u8[] {
87
109
  if (this.free.length > 0) {
@@ -104,6 +126,9 @@ export class BufferPool {
104
126
  *
105
127
  * Panics when `buf` is not `size` bytes, when `used` is outside
106
128
  * `0 .. size`, or when nothing is lent.
129
+ *
130
+ * @param buf the buffer `take` lent, which the pool owns again once this returns
131
+ * @param used how many bytes from the start of `buf` the borrower wrote, the extent that gets zeroed
107
132
  */
108
133
  give(buf: u8[], used: i32): void {
109
134
  bufferPoolCheckGive(this, buf, used)
@@ -119,6 +144,9 @@ export class BufferPool {
119
144
  * above 0, and costs nothing for a buffer that was never written.
120
145
  *
121
146
  * Panics as `give` does.
147
+ *
148
+ * @param buf the buffer `take` lent, which held secret bytes and which the pool owns again once this returns
149
+ * @param used how many bytes from the start of `buf` the borrower wrote; above 0, the whole buffer is wiped
122
150
  */
123
151
  giveSecret(buf: u8[], used: i32): void {
124
152
  bufferPoolCheckGive(this, buf, used)
@@ -128,7 +156,10 @@ export class BufferPool {
128
156
  this.free.push(buf)
129
157
  }
130
158
 
131
- /** How many buffers are lent now: made, less those waiting on the free list. */
159
+ /**
160
+ * How many buffers are lent now: made, less those waiting on the free list.
161
+ * @returns the count of buffers borrowed and not yet given back, which is 0 when every borrower kept its promise
162
+ */
132
163
  lent(): i32 {
133
164
  return this.made - toI32(this.free.length)
134
165
  }