nish 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 (79) hide show
  1. package/README.md +10 -8
  2. package/docs/AI.md +380 -47
  3. package/docs/INSTALL.md +28 -16
  4. package/package.json +8 -7
  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/scripts/fuzz.sh +58 -8
  28. package/scripts/gen-pow10-table.py +1 -1
  29. package/scripts/postinstall.mjs +8 -7
  30. package/scripts/shard-costs.mjs +429 -0
  31. package/scripts/size-report.sh +2 -2
  32. package/scripts/smoke.sh +12 -2
  33. package/std/LICENSE-fdlibm +8 -0
  34. package/std/README.md +7 -4
  35. package/std/array.ts +266 -0
  36. package/std/compress/checksum.ts +13 -0
  37. package/std/compress/deflate.ts +0 -2
  38. package/std/crypto/p256.ts +247 -74
  39. package/std/crypto/p384.ts +3 -1
  40. package/std/crypto/pki.ts +35 -9
  41. package/std/crypto/x25519.ts +153 -64
  42. package/std/crypto/x509.ts +113 -10
  43. package/std/host/text.ts +86 -0
  44. package/std/json-tokenizer.ts +20 -0
  45. package/std/json-typed.ts +217 -8
  46. package/std/math.ts +743 -41
  47. package/std/net/dns-resolver.ts +46 -6
  48. package/std/net/dns.ts +50 -13
  49. package/std/net/fetch.ts +691 -129
  50. package/std/net/handler.ts +142 -16
  51. package/std/net/http-compress.ts +17 -9
  52. package/std/net/http-fields.ts +19 -4
  53. package/std/net/http1-codec.ts +517 -154
  54. package/std/net/http1-server.ts +508 -25
  55. package/std/net/http2-cleartext.ts +526 -0
  56. package/std/net/http2-tls.ts +48 -0
  57. package/std/net/http2.ts +84 -0
  58. package/std/net/postgres-async.ts +2223 -0
  59. package/std/net/postgres-carrier.ts +343 -10
  60. package/std/net/postgres.ts +129 -11
  61. package/std/net/prefork.ts +195 -11
  62. package/std/net/quic-datagram.ts +30 -0
  63. package/std/net/quic-listener.ts +38 -12
  64. package/std/net/quic-packet.ts +19 -6
  65. package/std/net/quic-recovery.ts +134 -19
  66. package/std/net/quic-stream.ts +174 -36
  67. package/std/net/quic.ts +418 -18
  68. package/std/net/router.ts +39 -17
  69. package/std/net/serve.ts +867 -0
  70. package/std/net/tls/codec.ts +110 -32
  71. package/std/net/tls/record-server.ts +37 -7
  72. package/std/net/tls-tcp.ts +56 -1
  73. package/std/net/tls.ts +196 -50
  74. package/std/net/websocket.ts +55 -18
  75. package/std/net/webtransport.ts +179 -1
  76. package/std/regex.ts +69 -47
  77. package/std/region.ts +62 -11
  78. package/std/secret.ts +26 -59
  79. package/std/text.ts +464 -5
package/docs/INSTALL.md CHANGED
@@ -56,8 +56,12 @@ the Homebrew LLVM is needed for the `wasm`, `wasm-host` and `wasi` profiles (`wa
56
56
 
57
57
  `wasm-opt` (binaryen: `apt-get install binaryen`, `brew install binaryen`) is
58
58
  optional. When it is on `PATH`, `scripts/build.sh --profile wasm-host` runs
59
- `wasm-opt -Oz` on the module, and the build line says whether it ran. Without
60
- it the module is complete, just not as small.
59
+ `wasm-opt -Oz --strip-target-features` on the module, and the build line says
60
+ whether it ran: `add.wasm` goes from 102 bytes to 74. Without it the module
61
+ is complete and works the same. clang 18 also runs a `wasm-opt` it finds,
62
+ after any optimising wasm link, and keeps a `target_features` section for it
63
+ that build.sh's pass then drops. `NISH_WASM_OPT=0` links with neither pass,
64
+ even with binaryen installed: the bytes a machine without it builds.
61
65
 
62
66
  <a id="wasi-optional-for---profile-wasi" style="scroll-margin-top: 65px"></a>
63
67
 
@@ -261,11 +265,11 @@ routes are
261
265
  # in the glibc container, where build/nish runs
262
266
  build/nish app.ts -o app.ll
263
267
  # on the musl host, with its own clang and the runtime from this repository
264
- clang app.ll runtime/runtime.c runtime/runtime-os.c runtime/runtime-parallel.c \
268
+ clang app.ll runtime/runtime.c runtime/runtime-text.c runtime/runtime-os.c runtime/runtime-parallel.c \
265
269
  runtime/runtime-cross.c runtime/runtime-host.c runtime/runtime-net.c \
266
270
  runtime/runtime-simd.c runtime/runtime-aes.c runtime/runtime-sha256.c runtime/runtime-chacha20.c \
267
271
  runtime/runtime-term.c runtime/runtime-bytes.c runtime/runtime-checksum.c runtime/runtime-sandbox.c \
268
- runtime/runtime-shm.c runtime/runtime-region.c -lm -o app
272
+ runtime/runtime-shm.c runtime/runtime-region.c -ffp-contract=off -lm -o app
269
273
  ```
270
274
 
271
275
  `--link`ing inside the container is the mistake to avoid: it shells out to the
@@ -374,7 +378,8 @@ release exists, take its version from
374
378
  Unpack it and run `bin/nish` from wherever you like; put that on `PATH` if you
375
379
  want it there. Keep the directory intact rather than moving the binary out of
376
380
  it: `--link` runs `scripts/build.sh` and compiles the C runtime
377
- (`runtime/runtime.c` and `runtime/runtime-os.c`, the system-call half), and the
381
+ (`runtime/runtime.c`, `runtime/runtime-text.c`, the strings and number
382
+ formatting, `runtime/runtime-os.c`, the system-call half, and the rest), and the
378
383
  compiler finds all of them relative to its own location — `bin/nish` alone in a
379
384
  directory can still emit IR with `-o`, but `--link` will tell you it cannot
380
385
  find `scripts/build.sh`.
@@ -401,7 +406,7 @@ it into `build/seed/`; `NISH_BOOTSTRAP=<path>` names one you already have and
401
406
  skips the download. §2a has what the build does with it.
402
407
 
403
408
  The published package ships `bin/` (the `nish` command, which is a launcher),
404
- `runtime/` (the C runtime — sixteen translation units, the wasm subset and their
409
+ `runtime/` (the C runtime — seventeen translation units, the wasm subset and their
405
410
  header — and `runtime/sqlite/`, SQLite's vendored source),
406
411
  `scripts/build.sh` (the link pipeline), `std/` (the standard library) and
407
412
  `docs/AI.md` (the rules card). The
@@ -416,19 +421,24 @@ any working directory — and the binary runs from inside its own platform
416
421
  package for exactly that reason.
417
422
 
418
423
  Building the IR yourself rather than through `--link` means naming the runtime
419
- on the `clang` line, and it is sixteen files:
424
+ on the `clang` line, and it is seventeen files:
420
425
 
421
426
  ```bash
422
- clang app.ll runtime/runtime.c runtime/runtime-os.c runtime/runtime-parallel.c \
427
+ clang app.ll runtime/runtime.c runtime/runtime-text.c runtime/runtime-os.c runtime/runtime-parallel.c \
423
428
  runtime/runtime-cross.c runtime/runtime-host.c runtime/runtime-net.c \
424
429
  runtime/runtime-simd.c runtime/runtime-aes.c runtime/runtime-sha256.c runtime/runtime-chacha20.c \
425
430
  runtime/runtime-term.c runtime/runtime-bytes.c runtime/runtime-checksum.c runtime/runtime-sandbox.c \
426
- runtime/runtime-shm.c runtime/runtime-region.c -lm -o app
431
+ runtime/runtime-shm.c runtime/runtime-region.c -ffp-contract=off -lm -o app
427
432
  ```
428
433
 
429
- `runtime.c` is the part every program touches — the arena, strings, arrays,
430
- number formatting, the panics — and `runtime-os.c` wraps the system calls:
431
- files, directories, subprocesses, `getenv`, the monotonic clock.
434
+ `runtime.c` is the part every program touches — the arena, arrays, the
435
+ panics — `runtime-text.c` holds strings and number formatting, and
436
+ `runtime-os.c` wraps the system calls: files, directories, subprocesses,
437
+ `getenv`, the monotonic clock.
438
+
439
+ `-ffp-contract=off` keeps clang from fusing a C `a*b+c` into one rounding
440
+ where the CPU has a fused multiply-add (every arm64, x86-64 at v3): the runtime
441
+ then prints the same float on every target, as `scripts/build.sh` compiles it.
432
442
 
433
443
  `runtime-parallel.c` divides work across threads for `nish/threads` (a program
434
444
  built with `--threads`, as every importer of `nish/threads` is, also needs
@@ -449,12 +459,14 @@ kernels of `nish/compress/checksum`, `runtime-sandbox.c` the confinement a
449
459
  `nish/sync/shm`. They are separate so that each carries its own measured size
450
460
  ceiling
451
461
  ([docs/wp7-runtime.md](https://github.com/amritk/nish/blob/main/docs/wp7-runtime.md));
452
- nothing in the core calls into the others, so a line that names only
453
- `runtime.c` and `runtime-os.c` still links a program that uses none of the
454
- other nine.
462
+ the core is three files that call each other — `runtime-os.c` builds strings
463
+ with `runtime-text.c`'s `nish_str_new` — and calls into none of the others, so a
464
+ line that names only `runtime.c`, `runtime-text.c` and `runtime-os.c` links a
465
+ program that uses none of the other fourteen. Leave out `runtime-text.c` and it
466
+ does not link at all: `nish_str_new` is undefined.
455
467
 
456
468
  `scripts/build.sh` compiles the other
457
- nine beside any
469
+ sixteen beside any
458
470
  `runtime.c` it is handed, so a build that goes through it — every `--link`,
459
471
  and every `--profile` recipe in these documents — needs to name only the one.
460
472
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nish",
3
- "version": "0.37.0",
3
+ "version": "0.39.0",
4
4
  "description": "Nish: an ahead-of-time compiler from a static subset of TypeScript to LLVM IR",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -51,6 +51,7 @@
51
51
  "!scripts/ddc-chain.sh",
52
52
  "!scripts/gen-reference.mjs",
53
53
  "!scripts/typedoc-nish-widths.mjs",
54
+ "!scripts/pin-platform-packages.mjs",
54
55
  "std",
55
56
  "README.md",
56
57
  "LICENSE",
@@ -104,17 +105,17 @@
104
105
  "webassembly",
105
106
  "nish"
106
107
  ],
107
- "optionalDependencies": {
108
- "@amritk/nish-x86_64-linux": "0.37.0",
109
- "@amritk/nish-aarch64-linux": "0.37.0",
110
- "@amritk/nish-aarch64-darwin": "0.37.0",
111
- "@amritk/nish-x86_64-darwin": "0.37.0"
112
- },
113
108
  "devDependencies": {
114
109
  "@biomejs/biome": "2.5.12",
115
110
  "@types/node": "^22.0.0",
116
111
  "knip": "6.38.0",
117
112
  "typedoc": "0.28.20",
118
113
  "typescript": "^5.6.0"
114
+ },
115
+ "optionalDependencies": {
116
+ "@amritk/nish-aarch64-darwin": "0.39.0",
117
+ "@amritk/nish-aarch64-linux": "0.39.0",
118
+ "@amritk/nish-x86_64-darwin": "0.39.0",
119
+ "@amritk/nish-x86_64-linux": "0.39.0"
119
120
  }
120
121
  }
@@ -11,6 +11,10 @@ export declare function storeIdx<T>(a: T[], i: number | bigint, v: T): T;
11
11
  export declare function arraySet<T>(dst: T[], src: readonly T[], offset?: number): void;
12
12
  /** `dst.set(src, offset)` as `--emit-ts` writes it: a 64-bit offset is a bigint, checked exactly. */
13
13
  export declare function arraySetAt<T>(dst: T[], src: readonly T[], offset?: number | bigint): void;
14
+ /** `xs.slice(start, end)` as `--emit-ts` writes it: an out-of-range bound panics, as the native one does. */
15
+ export declare function sliceArray<T>(a: readonly T[], start?: number | bigint, end?: number | bigint): T[];
16
+ /** The answer of `xs.map`, `xs.filter` or `xs.slice` with each record copied, as an array holds it inline. */
17
+ export declare function copyRecords<T>(a: T[]): T[];
14
18
  /** `new Array<T>(n)`: `--emit-ts` writes `T` out, so the zero it fills with is not what `T` is read from. */
15
19
  export declare function newArray<T>(n: number | bigint, zero: unknown): T[];
16
20
  export declare function arenaUsed(): i64;
@@ -66,6 +66,39 @@ export function copyIn(v) {
66
66
  return isRecord(v) ? { ...v } : v;
67
67
  }
68
68
 
69
+ /**
70
+ * `xs.slice(start, end)` as `nish --emit-ts` writes it: the native rule, where
71
+ * the range must satisfy `0 <= start <= end <= xs.length` and anything else
72
+ * panics, not JavaScript's, which counts a negative bound from the end and
73
+ * clamps the rest.
74
+ */
75
+ export function sliceArray(a, start, end) {
76
+ const from = start === undefined ? 0n : sliceBound(start);
77
+ const to = end === undefined ? BigInt(a.length) : sliceBound(end);
78
+ if (!(from >= 0n && from <= to && to <= BigInt(a.length))) panicSlice(from, to, a.length);
79
+ return a.slice(Number(from), Number(to));
80
+ }
81
+
82
+ // A `slice` bound as the native template converts it to `i64`: a `number` is
83
+ // truncated toward zero and saturates, NaN to 0 and an infinity to the i64
84
+ // limit, so a panic prints the same offsets natively and here.
85
+ function sliceBound(i) {
86
+ if (typeof i === "bigint") return i;
87
+ if (Number.isNaN(i)) return 0n;
88
+ if (i >= 2 ** 63) return (1n << 63n) - 1n;
89
+ if (i <= -(2 ** 63)) return -(1n << 63n);
90
+ return BigInt(Math.trunc(i));
91
+ }
92
+
93
+ /**
94
+ * The array `xs.map`, `xs.filter` or `xs.slice` answers, as the native one
95
+ * holds it: each record a fresh copy, as an array holds a record inline.
96
+ */
97
+ export function copyRecords(a) {
98
+ for (let i = 0; i < a.length; i++) a[i] = copyIn(a[i]);
99
+ return a;
100
+ }
101
+
69
102
  /**
70
103
  * `a[i] = v` from a generic body: as `setIdx`, but a record is written field
71
104
  * by field into the record already in the slot, as an inline element is.
@@ -42,6 +42,10 @@ export declare function argv(): string[];
42
42
  export declare function readFileSync(path: string): string;
43
43
  export declare function readFileSyncOrNull(path: string): string | null;
44
44
  export declare function readFileBytesSync(path: string): u8[] | null;
45
+ export declare function fileOpen(path: string): i32;
46
+ export declare function fileReadAt(fd: i32, buf: u8[], off: i32, len: i32, pos: i64): i32;
47
+ export declare function fileSize(fd: i32): i64;
48
+ export declare function fileClose(fd: i32): i32;
45
49
  export declare function writeFileSync(path: string, data: string): void;
46
50
  export declare function appendFileSync(path: string, data: string): void;
47
51
  export declare function writeFileBytesSync(path: string, bytes: readonly u8[]): i32;
@@ -60,6 +64,13 @@ export declare function getenv(name: string): string | null;
60
64
  export declare function spawnSync(argv: string[]): number;
61
65
  export declare function spawnSyncTo(argv: string[], stdoutPath: string, stderrPath: string): number;
62
66
  export declare function spawnPiped(argv: string[], stdin: readonly u8[], stdout: u8[], stderr: u8[]): i32;
67
+ export declare function spawnPipedWithin(
68
+ argv: string[],
69
+ stdin: readonly u8[],
70
+ stdout: u8[],
71
+ stderr: u8[],
72
+ timeoutMs: i32
73
+ ): i32;
63
74
  export declare function kill(pid: i32, sig: i32): i32;
64
75
  export declare function availableParallelism(): i32;
65
76
  export declare function totalMemory(): i64;
@@ -137,6 +137,30 @@ export function readFileBytesSync(path) {
137
137
  return host.call("readFileBytesSync", [path]);
138
138
  }
139
139
 
140
+ export function fileOpen(path) {
141
+ return host.call("fileOpen", [path]);
142
+ }
143
+
144
+ /**
145
+ * `fileReadAt(fd, buf, off, len, pos)`: the window checked as the compiled
146
+ * call checks it, `0 <= off <= off + len <= buf.length`, through the slice
147
+ * panic, then the host's positional read.
148
+ */
149
+ export function fileReadAt(fd, buf, off, len, pos) {
150
+ const from = toIndex(off);
151
+ const to = from + toIndex(len);
152
+ if (!(from >= 0 && from <= to && to <= buf.length)) panicSlice(from, to, buf.length);
153
+ return host.call("fileReadAt", [fd, buf, from, to - from, BigInt(pos)]);
154
+ }
155
+
156
+ export function fileSize(fd) {
157
+ return host.call("fileSize", [fd]);
158
+ }
159
+
160
+ export function fileClose(fd) {
161
+ return host.call("fileClose", [fd]);
162
+ }
163
+
140
164
  export function writeFileSync(path, data) {
141
165
  return host.call("writeFileSync", [path, data]);
142
166
  }
@@ -217,6 +241,17 @@ export function spawnPiped(argv, input, out, err) {
217
241
  return status;
218
242
  }
219
243
 
244
+ /**
245
+ * `spawnPipedWithin(argv, stdin, stdout, stderr, timeoutMs)`: `spawnPiped`
246
+ * with a deadline the host enforces, and -3 when it passed.
247
+ */
248
+ export function spawnPipedWithin(argv, input, out, err, timeoutMs) {
249
+ const [status, outBytes, errBytes] = host.call("spawnPipedWithin", [argv, input, timeoutMs]);
250
+ for (const b of outBytes) out.push(b);
251
+ for (const b of errBytes) err.push(b);
252
+ return status;
253
+ }
254
+
220
255
  export function kill(pid, sig) {
221
256
  return host.call("kill", [pid, sig]);
222
257
  }