@amritk/nish-aarch64-linux 0.38.0 → 0.40.1

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 (86) hide show
  1. package/INSTALL.md +74 -129
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +455 -65
  4. package/package.json +1 -1
  5. package/runtime/core/host.d.ts +11 -0
  6. package/runtime/core/host.mjs +35 -0
  7. package/runtime/nish.d.ts +320 -33
  8. package/runtime/nish.h +84 -10
  9. package/runtime/nish.mjs +69 -6
  10. package/runtime/node.mjs +117 -0
  11. package/runtime/page-frame.d.ts +123 -0
  12. package/runtime/page-frame.mjs +736 -0
  13. package/runtime/page-game.d.ts +132 -0
  14. package/runtime/page-game.mjs +738 -0
  15. package/runtime/runtime-aes.c +97 -12
  16. package/runtime/runtime-async.c +43 -10
  17. package/runtime/runtime-bytes.c +88 -4
  18. package/runtime/runtime-checksum.c +207 -15
  19. package/runtime/runtime-net.c +64 -3
  20. package/runtime/runtime-os.c +174 -27
  21. package/runtime/runtime-region.c +94 -10
  22. package/runtime/runtime-sandbox.c +404 -17
  23. package/runtime/runtime-sha256.c +11 -4
  24. package/runtime/runtime-wasm.c +437 -0
  25. package/runtime/runtime.c +5 -0
  26. package/runtime/vite.mjs +1 -1
  27. package/scripts/build.sh +42 -8
  28. package/std/README.md +78 -91
  29. package/std/array.ts +36 -3
  30. package/std/collections.ts +51 -0
  31. package/std/compress/checksum.ts +13 -0
  32. package/std/compress/deflate.ts +0 -2
  33. package/std/crypto/hmac.ts +37 -10
  34. package/std/crypto/p256.ts +1629 -2732
  35. package/std/crypto/p384.ts +3 -1
  36. package/std/crypto/pki.ts +151 -13
  37. package/std/crypto/sha256.ts +34 -6
  38. package/std/crypto/x25519.ts +153 -64
  39. package/std/crypto/x509.ts +120 -10
  40. package/std/host/game.ts +273 -0
  41. package/std/input.ts +56 -0
  42. package/std/json-tokenizer.ts +344 -111
  43. package/std/json-typed.ts +256 -12
  44. package/std/json-value.ts +144 -3
  45. package/std/json.ts +63 -8
  46. package/std/net/fetch.ts +22 -4
  47. package/std/net/handler.ts +130 -16
  48. package/std/net/http-server.ts +3382 -0
  49. package/std/net/http1-client-codec.ts +6 -2
  50. package/std/net/http1-codec.ts +636 -162
  51. package/std/net/http1-server.ts +569 -42
  52. package/std/net/http2-cleartext.ts +526 -0
  53. package/std/net/http2-tls.ts +48 -0
  54. package/std/net/http2.ts +131 -14
  55. package/std/net/http3-server.ts +29 -3
  56. package/std/net/http3.ts +180 -7
  57. package/std/net/postgres-async.ts +1304 -86
  58. package/std/net/postgres-carrier.ts +343 -10
  59. package/std/net/postgres.ts +122 -7
  60. package/std/net/prefork.ts +195 -11
  61. package/std/net/quic-datagram.ts +30 -0
  62. package/std/net/quic-listener.ts +38 -12
  63. package/std/net/quic-packet.ts +19 -6
  64. package/std/net/quic-recovery.ts +373 -27
  65. package/std/net/quic-stream.ts +195 -37
  66. package/std/net/quic.ts +516 -22
  67. package/std/net/router.ts +407 -38
  68. package/std/net/serve.ts +1022 -110
  69. package/std/net/tls/codec.ts +110 -32
  70. package/std/net/tls/record-server.ts +37 -7
  71. package/std/net/tls/verify.ts +19 -1
  72. package/std/net/tls-tcp.ts +56 -1
  73. package/std/net/tls.ts +196 -50
  74. package/std/net/url.ts +83 -35
  75. package/std/net/websocket.ts +129 -19
  76. package/std/net/webtransport.ts +185 -1
  77. package/std/raster.ts +837 -0
  78. package/std/regex.ts +69 -47
  79. package/std/region.ts +62 -11
  80. package/std/release.ts +593 -0
  81. package/std/secret.ts +26 -59
  82. package/std/synth.ts +143 -0
  83. package/std/text.ts +233 -0
  84. package/std/threads.ts +298 -0
  85. package/std/yaml-json.ts +368 -28
  86. package/std/yaml.ts +122 -27
package/std/array.ts CHANGED
@@ -10,15 +10,16 @@
10
10
  * };
11
11
  *
12
12
  * **A program does not need to import this module.** A `.map(...)`,
13
- * `.filter(...)`, `.some(...)`, `.every(...)`, `.findIndex(...)` or
14
- * `.slice(...)` call on an array is what loads it (the checker's
13
+ * `.filter(...)`, `.some(...)`, `.every(...)`, `.findIndex(...)`,
14
+ * `.forEach(...)` or `.slice(...)` call on an array is what loads it (the checker's
15
15
  * `importArrayMethods`), and the checker routes the call to the template here
16
16
  * that matches it: `xs.map(f)` is `map(xs, f)` when `f` takes one parameter
17
17
  * and `mapIndexed(xs, f)` when it takes two. `f` is a compile-time function
18
18
  * parameter, so each callback is an instantiation of its own whose body calls
19
19
  * it directly — there is no function pointer, and importing a template here
20
20
  * by name and calling it gives the same code as the method. Under Node,
21
- * `--emit-ts` leaves `map`, `filter`, `some`, `every` and `findIndex` as the
21
+ * `--emit-ts` leaves `map`, `filter`, `some`, `every`, `findIndex` and
22
+ * `forEach` as the
22
23
  * platform's own `Array.prototype` methods, writes `slice` as the runtime's
23
24
  * checked `sliceArray`, and copies an answer that holds records inline with
24
25
  * `copyRecords`, so nothing here runs there.
@@ -219,6 +220,38 @@ export const findIndexIndexed = <T>(xs: readonly T[], pred: (x: T, i: number) =>
219
220
  return -1
220
221
  }
221
222
 
223
+ /**
224
+ * Calls `f` with each element of `xs`, in order: what `xs.forEach(f)`
225
+ * compiles to when `f` takes one parameter. The walk stops at the length the
226
+ * array had when the call began, so an element the callback pushes is not
227
+ * visited, and at the current length, so one it pops is not either, as
228
+ * `Array.prototype.forEach` does.
229
+ *
230
+ * @param xs the array read; the callback may change it.
231
+ * @param f called once per element, in order.
232
+ */
233
+ export const forEach = <T>(xs: readonly T[], f: (x: T) => void): void => {
234
+ const n = xs.length
235
+ for (let i: number = 0; i < n && i < xs.length; i++) {
236
+ f(xs[i])
237
+ }
238
+ }
239
+
240
+ /**
241
+ * Calls `f` with each element of `xs` and its index, in order: what
242
+ * `xs.forEach(f)` compiles to when `f` takes two parameters. The walk stops
243
+ * where `forEach`'s does.
244
+ *
245
+ * @param xs the array read; the callback may change it.
246
+ * @param f called once per element, in order, with the element and its index.
247
+ */
248
+ export const forEachIndexed = <T>(xs: readonly T[], f: (x: T, i: number) => void): void => {
249
+ const n = xs.length
250
+ for (let i: number = 0; i < n && i < xs.length; i++) {
251
+ f(xs[i], i)
252
+ }
253
+ }
254
+
222
255
  /**
223
256
  * A copy of `xs[start..end)` in a new array: what `xs.slice(start, end)`
224
257
  * compiles to, with string `slice`'s rule. The range must satisfy
@@ -688,3 +688,54 @@ export class Set<T> {
688
688
  * of an earlier function records.
689
689
  */
690
690
  const storedKey = <K>(key: K): K => key
691
+
692
+ /**
693
+ * Calls `f` with the value and the key of each entry of `m`, in insertion
694
+ * order: what `m.forEach(f)` compiles to when `f` takes two parameters. It is
695
+ * the walk a `for...of` over `m.keys()` makes, and reads the value from the
696
+ * entry the walk is on, so each entry costs no probe. An entry the callback
697
+ * adds is visited and one it deletes is not, as `Map.prototype.forEach` does.
698
+ * Placed after `storedKey` so that no earlier line moved.
699
+ *
700
+ * @param m the table walked; the callback may change it.
701
+ * @param f called once per live entry, with its value and then its key.
702
+ */
703
+ export const forEachEntry = <K, V>(m: Map<K, V>, f: (value: V, key: K) => void): void => {
704
+ m.walkOpen()
705
+ for (let i: i32 = m.walkNext(0); i >= 0; i = m.walkNext(i + 1)) {
706
+ f(m.valueAt(i), m.keyAt(i))
707
+ }
708
+ m.walkClose()
709
+ }
710
+
711
+ /**
712
+ * Calls `f` with the value of each entry of `m`, in insertion order: what
713
+ * `m.forEach(f)` compiles to when `f` takes one parameter. The walk is
714
+ * `forEachEntry`'s.
715
+ *
716
+ * @param m the table walked; the callback may change it.
717
+ * @param f called once per live entry, with its value.
718
+ */
719
+ export const forEachValue = <K, V>(m: Map<K, V>, f: (value: V) => void): void => {
720
+ m.walkOpen()
721
+ for (let i: i32 = m.walkNext(0); i >= 0; i = m.walkNext(i + 1)) {
722
+ f(m.valueAt(i))
723
+ }
724
+ m.walkClose()
725
+ }
726
+
727
+ /**
728
+ * Calls `f` with each element of `s`, in insertion order: what `s.forEach(f)`
729
+ * compiles to. The walk is a `for...of` over `s`, with its rule for elements
730
+ * the callback adds or deletes.
731
+ *
732
+ * @param s the set walked; the callback may change it.
733
+ * @param f called once per live element.
734
+ */
735
+ export const forEachElement = <T>(s: Set<T>, f: (value: T) => void): void => {
736
+ s.walkOpen()
737
+ for (let i: i32 = s.walkNext(0); i >= 0; i = s.walkNext(i + 1)) {
738
+ f(s.keyAt(i))
739
+ }
740
+ s.walkClose()
741
+ }
@@ -291,6 +291,19 @@ export const crc32 = (buf: u8[], off: i32, len: i32): u32 => new Crc32().update(
291
291
  */
292
292
  export const adler32Update = (adler: u32, buf: u8[], off: i32, len: i32): u32 => {
293
293
  checksumWindow("adler32Update", buf, off, len)
294
+ return adler32Walk(adler, buf, off, len)
295
+ }
296
+
297
+ /**
298
+ * `adler32Update`'s loop: the Adler-32 of the bytes `adler` was the Adler-32
299
+ * of, followed by `buf[off .. off + len)`, a window its one caller has
300
+ * checked. Natively the compiler puts the call behind a branch on
301
+ * `nish_adler32_hw` and hands the same window to the runtime's
302
+ * `nish_adler32_update`, which runs it on SSSE3 or NEON vectors
303
+ * (`src/kernels.ts`); a machine without SSSE3, a process run with
304
+ * `NISH_ADLER32=soft`, a wasm32 build and Node run this.
305
+ */
306
+ const adler32Walk = (adler: u32, buf: u8[], off: i32, len: i32): u32 => {
294
307
  let s1: u32 = adler & 0xffff
295
308
  let s2: u32 = adler >>> 16
296
309
  let p: i32 = off
@@ -613,7 +613,6 @@ const deflateLengths = (freq: i32[], n: i32, limit: i32, lens: i32[], w: Deflate
613
613
  let usedHere: i32 = 0
614
614
  let depth: i32 = 0
615
615
  root = used - 2
616
- let next: i32 = used - 1
617
616
  while (avail > 0) {
618
617
  while (root >= 0 && uncheckedGet(keys, root) === depth) {
619
618
  usedHere += 1
@@ -623,7 +622,6 @@ const deflateLengths = (freq: i32[], n: i32, limit: i32, lens: i32[], w: Deflate
623
622
  if (depth < 33) {
624
623
  per[depth] += 1
625
624
  }
626
- next -= 1
627
625
  avail -= 1
628
626
  }
629
627
  avail = 2 * usedHere
@@ -152,6 +152,10 @@ const hmacCheckKey = (key: u8[]): void => {
152
152
  }
153
153
  }
154
154
 
155
+ /** Whether `[off, off + len)` is a window inside an array of `length` elements. */
156
+ const hmacWindowFits = (length: i32, off: i32, len: i32): boolean =>
157
+ off >= 0 && len >= 0 && off <= length - len
158
+
155
159
  /**
156
160
  * Keys `inner` with `k XOR ipad` and `outer` with `k XOR opad`, `k` no longer
157
161
  * than SHA-256's block, in one block that is rewritten between the two and
@@ -250,17 +254,26 @@ export class HmacSha256 {
250
254
  inner: Sha256
251
255
  /** H(K XOR opad, …), fed the key block in the constructor and the inner digest last. */
252
256
  outer: Sha256
257
+ /**
258
+ * Where `digestInto` puts the inner digest on its way into `outer`, so that
259
+ * it allocates nothing; zero except during that call, which wipes it.
260
+ */
261
+ innerDigest: u8[]
253
262
 
254
263
  /**
255
264
  * Keys the computation. A key longer than 64 bytes is replaced by its
256
265
  * SHA-256 (RFC 2104 §2), which is wiped once used (`hmacKeySha256`).
257
266
  *
267
+ * Besides the two hashers it allocates the 32-byte `innerDigest` buffer
268
+ * that every `digest` and `digestInto` reuses.
269
+ *
258
270
  * @param key The secret key; it is read, not kept, and the caller still
259
271
  * wipes it.
260
272
  */
261
273
  constructor(key: u8[]) {
262
274
  this.inner = new Sha256()
263
275
  this.outer = new Sha256()
276
+ this.innerDigest = new Array<u8>(SHA256_SIZE)
264
277
  hmacKeySha256(this.inner, this.outer, key)
265
278
  }
266
279
 
@@ -277,18 +290,36 @@ export class HmacSha256 {
277
290
  }
278
291
 
279
292
  /**
280
- * Ends the computation and wipes both hashers.
293
+ * Ends the computation and wipes both hashers and the inner digest. The tag
294
+ * is the one allocation.
281
295
  *
282
296
  * @returns A fresh 32-byte tag, which the caller owns.
283
297
  */
284
298
  digest(): u8[] {
285
- const innerDigest: u8[] = this.inner.digest()
286
- this.outer.update(innerDigest, HMAC_FROM, SHA256_SIZE)
287
- wipe(innerDigest)
288
- const tag: u8[] = this.outer.digest()
299
+ const tag: u8[] = new Array<u8>(SHA256_SIZE)
300
+ this.digestInto(tag, HMAC_FROM)
301
+ return tag
302
+ }
303
+
304
+ /**
305
+ * Writes the 32-byte tag at `out[at .. at + 32)`, ends the computation and
306
+ * wipes both hashers and the inner digest, with nothing allocated. A window
307
+ * outside `out` panics before anything is written; so does a second
308
+ * `digest` or `digestInto`, in the inner hasher.
309
+ *
310
+ * @param out The caller's array the tag is written into.
311
+ * @param at Where the 32 tag bytes start in `out`.
312
+ */
313
+ digestInto(out: u8[], at: i32): void {
314
+ if (!hmacWindowFits(toI32(out.length), at, SHA256_SIZE)) {
315
+ panic("HmacSha256: the tag window is outside its array")
316
+ }
317
+ this.inner.digestInto(this.innerDigest, HMAC_FROM)
318
+ this.outer.update(this.innerDigest, HMAC_FROM, SHA256_SIZE)
319
+ wipe(this.innerDigest)
320
+ this.outer.digestInto(out, at)
289
321
  hmacWipeSha256(this.inner)
290
322
  hmacWipeSha256(this.outer)
291
- return tag
292
323
  }
293
324
  }
294
325
 
@@ -580,10 +611,6 @@ export const hmacCopySha384 = (from: Sha384, to: Sha384): void => {
580
611
  to.engine.finished = from.engine.finished
581
612
  }
582
613
 
583
- /** Whether `[off, off + len)` is a window inside an array of `length` elements. */
584
- const hmacWindowFits = (length: i32, off: i32, len: i32): boolean =>
585
- off >= 0 && len >= 0 && off <= length - len
586
-
587
614
  /**
588
615
  * HMAC-SHA-256 over hashers the caller owns: allocate one, then for each tag
589
616
  * `begin` with the key, `update` with the message, and `finishInto` the