@amritk/nish-aarch64-linux 0.37.0 → 0.39.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 (73) hide show
  1. package/INSTALL.md +28 -16
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +380 -47
  4. package/package.json +1 -1
  5. package/runtime/core/arrays.d.ts +4 -0
  6. package/runtime/core/arrays.mjs +33 -0
  7. package/runtime/core/host.d.ts +11 -0
  8. package/runtime/core/host.mjs +35 -0
  9. package/runtime/nish.d.ts +645 -66
  10. package/runtime/nish.h +101 -15
  11. package/runtime/nish.mjs +69 -6
  12. package/runtime/node.mjs +117 -0
  13. package/runtime/runtime-aes.c +12 -4
  14. package/runtime/runtime-async.c +267 -51
  15. package/runtime/runtime-checksum.c +207 -15
  16. package/runtime/runtime-cross.c +2 -1
  17. package/runtime/runtime-net.c +32 -3
  18. package/runtime/runtime-os.c +174 -27
  19. package/runtime/runtime-region.c +170 -8
  20. package/runtime/runtime-sandbox.c +377 -17
  21. package/runtime/runtime-sha256.c +11 -4
  22. package/runtime/runtime-text.c +1068 -0
  23. package/runtime/runtime-wasm.c +18 -3
  24. package/runtime/runtime.c +55 -961
  25. package/runtime/vite.mjs +48 -11
  26. package/scripts/build.sh +113 -36
  27. package/std/LICENSE-fdlibm +8 -0
  28. package/std/README.md +7 -4
  29. package/std/array.ts +266 -0
  30. package/std/compress/checksum.ts +13 -0
  31. package/std/compress/deflate.ts +0 -2
  32. package/std/crypto/p256.ts +247 -74
  33. package/std/crypto/p384.ts +3 -1
  34. package/std/crypto/pki.ts +35 -9
  35. package/std/crypto/x25519.ts +153 -64
  36. package/std/crypto/x509.ts +113 -10
  37. package/std/host/text.ts +86 -0
  38. package/std/json-tokenizer.ts +20 -0
  39. package/std/json-typed.ts +217 -8
  40. package/std/math.ts +743 -41
  41. package/std/net/dns-resolver.ts +46 -6
  42. package/std/net/dns.ts +50 -13
  43. package/std/net/fetch.ts +691 -129
  44. package/std/net/handler.ts +142 -16
  45. package/std/net/http-compress.ts +17 -9
  46. package/std/net/http-fields.ts +19 -4
  47. package/std/net/http1-codec.ts +517 -154
  48. package/std/net/http1-server.ts +508 -25
  49. package/std/net/http2-cleartext.ts +526 -0
  50. package/std/net/http2-tls.ts +48 -0
  51. package/std/net/http2.ts +84 -0
  52. package/std/net/postgres-async.ts +2223 -0
  53. package/std/net/postgres-carrier.ts +343 -10
  54. package/std/net/postgres.ts +129 -11
  55. package/std/net/prefork.ts +195 -11
  56. package/std/net/quic-datagram.ts +30 -0
  57. package/std/net/quic-listener.ts +38 -12
  58. package/std/net/quic-packet.ts +19 -6
  59. package/std/net/quic-recovery.ts +134 -19
  60. package/std/net/quic-stream.ts +174 -36
  61. package/std/net/quic.ts +418 -18
  62. package/std/net/router.ts +39 -17
  63. package/std/net/serve.ts +867 -0
  64. package/std/net/tls/codec.ts +110 -32
  65. package/std/net/tls/record-server.ts +37 -7
  66. package/std/net/tls-tcp.ts +56 -1
  67. package/std/net/tls.ts +196 -50
  68. package/std/net/websocket.ts +55 -18
  69. package/std/net/webtransport.ts +179 -1
  70. package/std/regex.ts +69 -47
  71. package/std/region.ts +62 -11
  72. package/std/secret.ts +26 -59
  73. package/std/text.ts +464 -5
package/std/array.ts ADDED
@@ -0,0 +1,266 @@
1
+ /**
2
+ * `std/array` — the array methods that take a callback, and `slice`, in Nish:
3
+ * each a plain loop, compiled once per callback.
4
+ *
5
+ * export const main = (): i32 => {
6
+ * const xs: i32[] = [3, 1, 2];
7
+ * const doubled = xs.map((x) => x * 2); // [6, 2, 4]
8
+ * const big = xs.filter((x) => x > 1); // [3, 2]
9
+ * return doubled[0] + big.length + xs.findIndex((x) => x === 2); // 6 + 2 + 2
10
+ * };
11
+ *
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
15
+ * `importArrayMethods`), and the checker routes the call to the template here
16
+ * that matches it: `xs.map(f)` is `map(xs, f)` when `f` takes one parameter
17
+ * and `mapIndexed(xs, f)` when it takes two. `f` is a compile-time function
18
+ * parameter, so each callback is an instantiation of its own whose body calls
19
+ * it directly — there is no function pointer, and importing a template here
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
22
+ * platform's own `Array.prototype` methods, writes `slice` as the runtime's
23
+ * checked `sliceArray`, and copies an answer that holds records inline with
24
+ * `copyRecords`, so nothing here runs there.
25
+ *
26
+ * **Same answers as Node.** Each walks the array from index 0 upward and
27
+ * calls the callback once per element, in order, until the length it had when
28
+ * the call began, as `Array.prototype` does; `some`, `every` and `findIndex`
29
+ * stop at the first element that decides the answer. A callback that shrinks
30
+ * the array ends the walk at the new length, where JavaScript goes on and
31
+ * skips the missing indices.
32
+ */
33
+
34
+ /**
35
+ * The elements of `xs`, each passed through `f`, in a new array of the same
36
+ * length: what `xs.map(f)` compiles to when `f` takes one parameter.
37
+ *
38
+ * @param xs the array read; it is not changed.
39
+ * @param f called once per element, in order.
40
+ * @returns a new array whose element `i` is `f(xs[i])`.
41
+ */
42
+ export const map = <T, U>(xs: readonly T[], f: (x: T) => U): U[] => {
43
+ const out: U[] = []
44
+ const n = xs.length
45
+ for (let i: number = 0; i < n && i < xs.length; i++) {
46
+ out.push(f(xs[i]))
47
+ }
48
+ return out
49
+ }
50
+
51
+ /**
52
+ * The elements of `xs`, each passed through `f` with its index: what
53
+ * `xs.map(f)` compiles to when `f` takes two parameters.
54
+ *
55
+ * @param xs the array read; it is not changed.
56
+ * @param f called once per element, in order, with the element and its index.
57
+ * @returns a new array whose element `i` is `f(xs[i], i)`.
58
+ */
59
+ export const mapIndexed = <T, U>(xs: readonly T[], f: (x: T, i: number) => U): U[] => {
60
+ const out: U[] = []
61
+ const n = xs.length
62
+ for (let i: number = 0; i < n && i < xs.length; i++) {
63
+ out.push(f(xs[i], i))
64
+ }
65
+ return out
66
+ }
67
+
68
+ /**
69
+ * The elements of `xs` for which `pred` answers `true`, in order, in a new
70
+ * array: what `xs.filter(pred)` compiles to when `pred` takes one parameter.
71
+ *
72
+ * @param xs the array read; it is not changed.
73
+ * @param pred called once per element, in order.
74
+ * @returns a new array of the elements kept.
75
+ */
76
+ export const filter = <T>(xs: readonly T[], pred: (x: T) => boolean): T[] => {
77
+ const out: T[] = []
78
+ const n = xs.length
79
+ for (let i: number = 0; i < n && i < xs.length; i++) {
80
+ const x = xs[i]
81
+ if (pred(x)) {
82
+ out.push(x)
83
+ }
84
+ }
85
+ return out
86
+ }
87
+
88
+ /**
89
+ * The elements of `xs` for which `pred` answers `true`, given each element and
90
+ * its index: what `xs.filter(pred)` compiles to when `pred` takes two
91
+ * parameters.
92
+ *
93
+ * @param xs the array read; it is not changed.
94
+ * @param pred called once per element, in order, with the element and its index.
95
+ * @returns a new array of the elements kept.
96
+ */
97
+ export const filterIndexed = <T>(xs: readonly T[], pred: (x: T, i: number) => boolean): T[] => {
98
+ const out: T[] = []
99
+ const n = xs.length
100
+ for (let i: number = 0; i < n && i < xs.length; i++) {
101
+ const x = xs[i]
102
+ if (pred(x, i)) {
103
+ out.push(x)
104
+ }
105
+ }
106
+ return out
107
+ }
108
+
109
+ /**
110
+ * Whether `pred` answers `true` for at least one element of `xs`, stopping at
111
+ * the first that does: what `xs.some(pred)` compiles to when `pred` takes one
112
+ * parameter. An empty array answers `false`.
113
+ *
114
+ * @param xs the array read; it is not changed.
115
+ * @param pred called in order until it answers `true`.
116
+ * @returns `true` when some element satisfies `pred`.
117
+ */
118
+ export const some = <T>(xs: readonly T[], pred: (x: T) => boolean): boolean => {
119
+ const n = xs.length
120
+ for (let i: number = 0; i < n && i < xs.length; i++) {
121
+ if (pred(xs[i])) {
122
+ return true
123
+ }
124
+ }
125
+ return false
126
+ }
127
+
128
+ /**
129
+ * Whether `pred` answers `true` for at least one element of `xs`, given each
130
+ * element and its index: what `xs.some(pred)` compiles to when `pred` takes
131
+ * two parameters.
132
+ *
133
+ * @param xs the array read; it is not changed.
134
+ * @param pred called in order, with the element and its index, until it answers `true`.
135
+ * @returns `true` when some element satisfies `pred`.
136
+ */
137
+ export const someIndexed = <T>(xs: readonly T[], pred: (x: T, i: number) => boolean): boolean => {
138
+ const n = xs.length
139
+ for (let i: number = 0; i < n && i < xs.length; i++) {
140
+ if (pred(xs[i], i)) {
141
+ return true
142
+ }
143
+ }
144
+ return false
145
+ }
146
+
147
+ /**
148
+ * Whether `pred` answers `true` for every element of `xs`, stopping at the
149
+ * first that does not: what `xs.every(pred)` compiles to when `pred` takes one
150
+ * parameter. An empty array answers `true`.
151
+ *
152
+ * @param xs the array read; it is not changed.
153
+ * @param pred called in order until it answers `false`.
154
+ * @returns `true` when no element fails `pred`.
155
+ */
156
+ export const every = <T>(xs: readonly T[], pred: (x: T) => boolean): boolean => {
157
+ const n = xs.length
158
+ for (let i: number = 0; i < n && i < xs.length; i++) {
159
+ if (!pred(xs[i])) {
160
+ return false
161
+ }
162
+ }
163
+ return true
164
+ }
165
+
166
+ /**
167
+ * Whether `pred` answers `true` for every element of `xs`, given each element
168
+ * and its index: what `xs.every(pred)` compiles to when `pred` takes two
169
+ * parameters.
170
+ *
171
+ * @param xs the array read; it is not changed.
172
+ * @param pred called in order, with the element and its index, until it answers `false`.
173
+ * @returns `true` when no element fails `pred`.
174
+ */
175
+ export const everyIndexed = <T>(xs: readonly T[], pred: (x: T, i: number) => boolean): boolean => {
176
+ const n = xs.length
177
+ for (let i: number = 0; i < n && i < xs.length; i++) {
178
+ if (!pred(xs[i], i)) {
179
+ return false
180
+ }
181
+ }
182
+ return true
183
+ }
184
+
185
+ /**
186
+ * The index of the first element of `xs` for which `pred` answers `true`:
187
+ * what `xs.findIndex(pred)` compiles to when `pred` takes one parameter.
188
+ *
189
+ * @param xs the array read; it is not changed.
190
+ * @param pred called in order until it answers `true`.
191
+ * @returns the first such index, or `-1` when there is none.
192
+ */
193
+ export const findIndex = <T>(xs: readonly T[], pred: (x: T) => boolean): number => {
194
+ const n = xs.length
195
+ for (let i: number = 0; i < n && i < xs.length; i++) {
196
+ if (pred(xs[i])) {
197
+ return i
198
+ }
199
+ }
200
+ return -1
201
+ }
202
+
203
+ /**
204
+ * The index of the first element of `xs` for which `pred` answers `true`,
205
+ * given each element and its index: what `xs.findIndex(pred)` compiles to when
206
+ * `pred` takes two parameters.
207
+ *
208
+ * @param xs the array read; it is not changed.
209
+ * @param pred called in order, with the element and its index, until it answers `true`.
210
+ * @returns the first such index, or `-1` when there is none.
211
+ */
212
+ export const findIndexIndexed = <T>(xs: readonly T[], pred: (x: T, i: number) => boolean): number => {
213
+ const n = xs.length
214
+ for (let i: number = 0; i < n && i < xs.length; i++) {
215
+ if (pred(xs[i], i)) {
216
+ return i
217
+ }
218
+ }
219
+ return -1
220
+ }
221
+
222
+ /**
223
+ * A copy of `xs[start..end)` in a new array: what `xs.slice(start, end)`
224
+ * compiles to, with string `slice`'s rule. The range must satisfy
225
+ * `0 <= start <= end <= xs.length`, and anything else panics with
226
+ * `slice out of range: [start, end) of length n` and exits 1, where
227
+ * JavaScript counts a negative end back from the length and clamps the rest.
228
+ * A float bound is truncated toward zero and saturates (NaN to 0, an infinity
229
+ * to the `i64` limit), natively and under Node alike.
230
+ *
231
+ * @param xs the array copied from; it is not changed.
232
+ * @param start the first index copied.
233
+ * @param end the index after the last copied.
234
+ * @returns a new array of `end - start` elements.
235
+ */
236
+ export const slice = <T>(xs: readonly T[], start: number, end: number): T[] => {
237
+ const from = toI64(start)
238
+ const to = toI64(end)
239
+ const len = toI64(xs.length)
240
+ if (from < 0 || from > to || to > len) {
241
+ panic(`slice out of range: [${from}, ${to}) of length ${len}`)
242
+ }
243
+ const out: T[] = []
244
+ for (let i: i32 = toI32(from); i >= 0 && i < toI32(to) && i < toI32(xs.length); i++) {
245
+ out.push(xs[i])
246
+ }
247
+ return out
248
+ }
249
+
250
+ /**
251
+ * A copy of `xs[start..)`: what `xs.slice(start)` compiles to. `start` must
252
+ * satisfy `0 <= start <= xs.length`, or the call panics as `slice` does.
253
+ *
254
+ * @param xs the array copied from; it is not changed.
255
+ * @param start the first index copied.
256
+ * @returns a new array of the elements from `start` on.
257
+ */
258
+ export const sliceFrom = <T>(xs: readonly T[], start: number): T[] => slice(xs, start, xs.length)
259
+
260
+ /**
261
+ * A copy of the whole of `xs`: what `xs.slice()` compiles to.
262
+ *
263
+ * @param xs the array copied; it is not changed.
264
+ * @returns a new array with the same elements.
265
+ */
266
+ export const sliceAll = <T>(xs: readonly T[]): T[] => slice(xs, 0, xs.length)
@@ -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