@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/nish-aarch64-linux",
3
- "version": "0.38.0",
3
+ "version": "0.40.1",
4
4
  "description": "The nish native compiler for Linux on ARM64",
5
5
  "license": "MIT",
6
6
  "os": [
@@ -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
  }
package/runtime/nish.d.ts CHANGED
@@ -33,8 +33,8 @@
33
33
  * `lib.es2022`'s**, which a program is checked against: a second declaration
34
34
  * would clash with it, so the interfaces at the foot of this file merge into
35
35
  * lib's, adding Nish's documentation and no signature narrower than lib's.
36
- * `nish`'s surface is lib's less what it defers — no `entries`, no `forEach`,
37
- * no `for...of` over a `Map` itself — with `keys()` and `values()` admitted
36
+ * `nish`'s surface is lib's less what it defers — no `entries`, no `for...of`
37
+ * over a `Map` itself — with `keys()` and `values()` admitted
38
38
  * only as the iterable of a `for...of`, and `get`'s `V | undefined` only as a
39
39
  * `const`'s initialiser, the left of `??` or an operand of `=== undefined`,
40
40
  * none of which `tsc` can know (docs/LANGUAGE.md -> `Map` and `Set`).
@@ -707,7 +707,7 @@ declare function isExecutableSync(path: string): boolean;
707
707
  // ---- Unsafe (`nish:unsafe`) --------------------------------------------------
708
708
  //
709
709
  // Declared as globals so `tsc` reads a program that imports them, as every
710
- // `nish:` export is, but `nish` refuses a call to any of the six that is not
710
+ // `nish:` export is, but `nish` refuses a call to any of the eight that is not
711
711
  // imported from `nish:unsafe`: the import is the module's visible opt-in. The
712
712
  // refusal reads `` `uncheckedGet` is a `nish:unsafe` function and is called only
713
713
  // through an import of it `` (NL2456). An import may rename (`as`) and cannot be
@@ -805,23 +805,69 @@ declare function wrappingMul<T extends i32 | i64>(a: T, b: T): T;
805
805
  *
806
806
  * The parameter is `CPtr`, so an unnarrowed `CPtr | null` is refused (``
807
807
  * `readCString` expects an argument of type CPtr, got CPtr | null ``) and `if
808
- * (p !== null)` comes first; a `declare function` whose result is declared
809
- * `CPtr` gets no check, so declare a result that may be `NULL` as `CPtr |
810
- * null`.
808
+ * (p !== null)` comes first. A `declare function` declared to answer `CPtr`
809
+ * may still answer `NULL`, so the call tests `p` first and panics with
810
+ * `ffi-null` on `NULL` (`` `readCString` read a NULL pointer ``, exit 1), as
811
+ * `readStruct` does; declare a result that may be `NULL` as `CPtr | null`.
811
812
  *
812
813
  * The copy is new on every call and is the calling function's allocation, and
813
814
  * it is refused inside a `using a = arena()` block that would release one held
814
815
  * outside it (`` `readCString(p)` may be allocated inside the `using arena()`
815
- * block ``). Under `--deny-panics` it is an allowed `unchecked` site, with the
816
- * copy's `oom` beside it, and the pointer it reads is `ffi`. It lowers the same
816
+ * block ``). Under `--deny-panics` its `NULL` test is an `ffi-null` site no
817
+ * proof removes, so the scope refuses the call (the copy's `oom` beside it is
818
+ * allowed), and the pointer it reads is `ffi`. It lowers the same
817
819
  * on wasm32, where whether the program links is the `declare function`'s
818
820
  * question. It has no Node form, as a `CPtr` comes only from a `declare
819
821
  * function`, which Node cannot call. Capability: unsafe.
820
822
  *
821
- * @param p the address a `declare function` handed back.
823
+ * @param p the address a `declare function` handed back; `NULL` panics.
822
824
  * @returns a copy of the bytes before the first `NUL`.
823
825
  */
824
826
  declare function readCString(p: CPtr): string;
827
+ /**
828
+ * A copy of the C struct at `p`, in a new instance of the `declare class` the
829
+ * call's context names: `const lc: Lconv = readStruct(localeconv())`. A type
830
+ * argument is not written at a call site (`readStruct<Lconv>(p)` is refused,
831
+ * as `identity<i32>(7)` is), so the layout comes from an annotation, a
832
+ * parameter or a return type, as `Ok(v)`'s type does; a call with none, or one
833
+ * whose context is not a `declare class`, is refused. It copies the layout's
834
+ * size in bytes, so the class must describe the struct's first fields (a
835
+ * prefix is enough) and `p` must point at that many readable bytes: undefined
836
+ * behaviour otherwise, as with `readCString`.
837
+ *
838
+ * The copy is the program's own: C may free or overwrite its struct straight
839
+ * after the call, and a `CPtr` field of the copy is still C's pointer, read
840
+ * with `readCString` while C keeps it live. The parameter is `CPtr`, so an
841
+ * unnarrowed `CPtr | null` is refused and `if (p !== null)` comes first.
842
+ *
843
+ * A `NULL` `p` is tested before the copy and panics with the `ffi-null` kind,
844
+ * naming the layout, since a `declare function` declared to answer `CPtr` may
845
+ * still answer `NULL`. No proof removes that test, so `--deny-panics` refuses
846
+ * the call; `--emit-panics` lists it as an `ffi-null` site, with the copy's
847
+ * `oom` beside it.
848
+ *
849
+ * The copy is new on every call and is the calling function's allocation, held
850
+ * by the arena like `new`. It lowers the same on wasm32, where a pointer is 4
851
+ * bytes and the layout is sized to match. It has no Node form.
852
+ * Capability: unsafe.
853
+ *
854
+ * @param p the address of a C struct a `declare function` handed back.
855
+ * @returns a copy of the struct, in the `declare class` its context names.
856
+ */
857
+ declare function readStruct<T>(p: CPtr): T;
858
+
859
+ /**
860
+ * `cErrno()`, from `nish:unsafe` (WP27 S5): the errno the most recent
861
+ * `declare function` call on this thread left, or 0 before any. Naming it
862
+ * anywhere in a program makes every foreign call zero errno just before it
863
+ * and save it just after, before the result is copied or anything allocated,
864
+ * so the runtime's own work after the call cannot change it. Meaningful only
865
+ * where the C function documents that it sets errno. Per thread; refused on a
866
+ * freestanding wasm32 build, which has no libc. Capability: unsafe.
867
+ *
868
+ * @returns the saved errno, an `i32`.
869
+ */
870
+ declare function cErrno(): i32;
825
871
 
826
872
  // ---- Streams and files (globals: Nish has no package resolution) ---------
827
873
 
@@ -923,6 +969,56 @@ declare function readFileSyncOrNull(path: string): string | null;
923
969
  * @returns its bytes, or `null`.
924
970
  */
925
971
  declare function readFileBytesSync(path: string): u8[] | null;
972
+ /**
973
+ * Open a regular file to read a window at a time (WP44): read-only,
974
+ * close-on-exec and non-blocking, so a FIFO or a device never waits. A
975
+ * directory is refused with `-21` and any other kind that is not a regular
976
+ * file with `-22`, the descriptor closed again first. Pair it with `fileClose`
977
+ * in a `try`/`finally`. A freestanding `wasm32` build refuses the call (NL2718);
978
+ * `--profile wasi` reads through WASI. Capability: fs.read.
979
+ *
980
+ * @param path the file to open; a NUL in it is refused as a missing path is.
981
+ * @returns a descriptor (`>= 0`), or the negated Linux errno: `-2` ENOENT,
982
+ * `-13` EACCES, `-20` ENOTDIR, `-21` EISDIR, `-22` EINVAL for a FIFO or
983
+ * device, `-6` ENXIO for a socket, `-23` ENFILE, `-24` EMFILE, `-36`
984
+ * ENAMETOOLONG, `-40` ELOOP; anything else is `-5` EIO.
985
+ */
986
+ declare function fileOpen(path: string): i32;
987
+ /**
988
+ * Read up to `len` bytes from byte `pos` of the file into `buf[off .. off +
989
+ * len)`, without moving any shared offset (`pread`). The window is checked as
990
+ * `buf.slice` checks it and panics the same way when it falls outside `buf`.
991
+ * The array is never grown: memory follows the window. Capability: fs.read.
992
+ *
993
+ * @param fd a descriptor from `fileOpen`.
994
+ * @param buf the array the bytes land in; its length is unchanged.
995
+ * @param off where in `buf` the first byte lands.
996
+ * @param len the most bytes to read.
997
+ * @param pos the file offset to read from, an `i64`.
998
+ * @returns the bytes read, `0` at end of file (and for a `len` of `0`, even on
999
+ * a closed or negative descriptor), or a negative errno: `-22`
1000
+ * EINVAL for a negative `pos` or one where `pos + len` overflows, `-9` EBADF
1001
+ * for a closed or negative descriptor, `-21` EISDIR, `-29` ESPIPE; anything
1002
+ * else is `-5` EIO.
1003
+ */
1004
+ declare function fileReadAt(fd: i32, buf: u8[], off: i32, len: i32, pos: i64): i32;
1005
+ /**
1006
+ * The file's size in bytes now, from `fstat`: a file may grow or shrink between
1007
+ * this and a read, which then answers what is there. Capability: fs.read.
1008
+ *
1009
+ * @param fd a descriptor from `fileOpen`.
1010
+ * @returns the size, an `i64` past 2^31 when the file is, or a negative errno
1011
+ * (`-9` EBADF for a closed descriptor).
1012
+ */
1013
+ declare function fileSize(fd: i32): i64;
1014
+ /**
1015
+ * Close a descriptor `fileOpen` gave. A second close answers `-9`, unless the
1016
+ * number has been handed out again since, so close once. Capability: fs.read.
1017
+ *
1018
+ * @param fd a descriptor from `fileOpen`.
1019
+ * @returns `0`, or a negative errno (`-9` EBADF).
1020
+ */
1021
+ declare function fileClose(fd: i32): i32;
926
1022
  /**
927
1023
  * The file's contents replaced by `data`, created at 0644 when missing;
928
1024
  * statement position. A symbolic link as the last component of the path is
@@ -1108,6 +1204,14 @@ declare function realpathSync(path: string): string | null;
1108
1204
  * an embedded NUL truncates one. A `wasm32` build and a WASI build have no
1109
1205
  * processes, and a WASI build always answers `-1`. Capability: process.spawn.
1110
1206
  *
1207
+ * Under `--sandbox`, a dependency's `process.spawn=<program>` grants can make
1208
+ * a list of the only programs the build may start. A program this builtin
1209
+ * starts under such a list cannot start another: an exec by it, or by
1210
+ * anything it forks, fails, so a granted `#!` script runs its interpreter's
1211
+ * builtins alone. Where the kernel or a container's seccomp profile refuses
1212
+ * what that takes (Docker's default profile does), the spawn fails and one
1213
+ * line on stderr says so.
1214
+ *
1111
1215
  * @param argv the program and its arguments.
1112
1216
  * @returns the exit status, or `128 + n` when signal `n` killed it (the shell's
1113
1217
  * convention), or `-1` when the vector is empty — there is no `argv[0]` to
@@ -1123,6 +1227,9 @@ declare function spawnSync(argv: string[]): number;
1123
1227
  * separately, with its own offset, so the streams would overwrite rather than
1124
1228
  * interleave; capture them apart and concatenate. Capability: process.spawn.
1125
1229
  *
1230
+ * Under `--sandbox` with a list of granted programs, the program started
1231
+ * cannot start another, as for `spawnSync`.
1232
+ *
1126
1233
  * @param argv the program and its arguments.
1127
1234
  * @param stdoutPath the file the child's stdout goes to, or `""`.
1128
1235
  * @param stderrPath the file the child's stderr goes to, or `""`.
@@ -1149,6 +1256,9 @@ declare function spawnSyncTo(argv: string[], stdoutPath: string, stderrPath: str
1149
1256
  * length is fixed. A `wasm32` build refuses it (NL2404). Capability:
1150
1257
  * process.spawn.
1151
1258
  *
1259
+ * Under `--sandbox` with a list of granted programs, the program started
1260
+ * cannot start another, as for `spawnSync`.
1261
+ *
1152
1262
  * @param argv the program and its arguments.
1153
1263
  * @param stdin the bytes the child reads.
1154
1264
  * @param stdout receives what the child prints on stdout.
@@ -1158,6 +1268,43 @@ declare function spawnSyncTo(argv: string[], stdoutPath: string, stderrPath: str
1158
1268
  * that fails, and a WASI build), or `-2` when a stream passed 16 MiB.
1159
1269
  */
1160
1270
  declare function spawnPiped(argv: string[], stdin: readonly u8[], stdout: u8[], stderr: u8[]): i32;
1271
+ /**
1272
+ * `spawnPiped` with a deadline: the same captured run, killed once `timeoutMs`
1273
+ * milliseconds have passed since it started. A negative `timeoutMs` is no
1274
+ * deadline, as `pollWait`'s is, so `spawnPipedWithin(argv, stdin, stdout,
1275
+ * stderr, -1)` is `spawnPiped(argv, stdin, stdout, stderr)`. When the deadline
1276
+ * passes the child is killed with `SIGKILL` and reaped, the answer is `-3`, and
1277
+ * `stdout` and `stderr` keep every byte read before the kill.
1278
+ *
1279
+ * The deadline covers the whole run, the wait for the child's exit included,
1280
+ * and is measured on the monotonic clock. Only the child's own pid is killed:
1281
+ * a grandchild it started is not, and one that keeps an output pipe open makes
1282
+ * the run wait for it, so a child that exited 0 can still answer `-3`. The
1283
+ * rest is `spawnPiped`'s: the same environment, descriptors, SIGPIPE handling
1284
+ * and 16 MiB cap, `readonly` and typed-array spellings refused for the two
1285
+ * output arrays, refused by a `wasm32` build (NL2404). Capability:
1286
+ * process.spawn.
1287
+ *
1288
+ * Under `--sandbox` with a list of granted programs, the program started
1289
+ * cannot start another, as for `spawnSync`.
1290
+ *
1291
+ * @param argv the program and its arguments.
1292
+ * @param stdin the bytes the child reads.
1293
+ * @param stdout receives what the child prints on stdout.
1294
+ * @param stderr receives what the child prints on stderr.
1295
+ * @param timeoutMs the milliseconds the run may take, or a negative number for
1296
+ * no deadline.
1297
+ * @returns the status as `spawnPiped` answers it (the exit code, `128 + n`,
1298
+ * `-1` for a child that cannot be started or a WASI build, `-2` when a stream
1299
+ * passed 16 MiB), or `-3` when the deadline passed and the child was killed.
1300
+ */
1301
+ declare function spawnPipedWithin(
1302
+ argv: string[],
1303
+ stdin: readonly u8[],
1304
+ stdout: u8[],
1305
+ stderr: u8[],
1306
+ timeoutMs: i32
1307
+ ): i32;
1161
1308
  /**
1162
1309
  * `spawn` with the child holding `fds[i]` as descriptor `3 + i`, close-on-exec
1163
1310
  * cleared there. Its streams 0 to 2 are this process's, and no other descriptor
@@ -1170,12 +1317,18 @@ declare function spawnPiped(argv: string[], stdin: readonly u8[], stdout: u8[],
1170
1317
  * under Node, which throws. A `wasm32` build refuses it (NL2404).
1171
1318
  * Capability: process.spawn.
1172
1319
  *
1320
+ * Under `--sandbox` with a list of granted programs, the program started
1321
+ * cannot start another, as for `spawnSync`.
1322
+ *
1173
1323
  * @param argv the program and its arguments.
1174
1324
  * @param fds the descriptors handed down, at most 64.
1175
1325
  * @returns the child's pid, or a negative errno: -22 more than 64 descriptors
1176
1326
  * or a negative one (before anything is opened or started), -9 one that is
1177
1327
  * not open, -24 (or -22 above the descriptor limit) a scratch descriptor the
1178
- * system cannot give.
1328
+ * system cannot give. Under `--sandbox` with a list of granted
1329
+ * programs, a spawn that cannot be held to one program is refused: -11
1330
+ * (EAGAIN), -38 (ENOSYS), -10 (ECHILD) or the kernel's own error, with a line
1331
+ * on stderr.
1179
1332
  */
1180
1333
  declare function spawnWithFds(argv: string[], fds: readonly i32[]): i32;
1181
1334
  /**
@@ -1190,10 +1343,16 @@ declare function spawnWithFds(argv: string[], fds: readonly i32[]): i32;
1190
1343
  * `nish:process`.
1191
1344
  * Capability: process.spawn.
1192
1345
  *
1346
+ * Under `--sandbox` with a list of granted programs, the program started
1347
+ * cannot start another, as for `spawnSync`.
1348
+ *
1193
1349
  * @param argv the program and its arguments.
1194
1350
  * @returns the child's pid, or a negative errno: -22 (EINVAL) for an empty
1195
1351
  * vector or an argument holding a NUL, and `posix_spawnp`'s own error
1196
- * otherwise, so a program that is not there is -2 (ENOENT).
1352
+ * otherwise, so a program that is not there is -2 (ENOENT). Under `--sandbox` with a list of granted
1353
+ * programs, a spawn that cannot be held to one program is refused: -11
1354
+ * (EAGAIN), -38 (ENOSYS), -10 (ECHILD) or the kernel's own error, with a line
1355
+ * on stderr.
1197
1356
  */
1198
1357
  declare function spawn(argv: string[]): i32;
1199
1358
  /**
@@ -1628,8 +1787,11 @@ declare function connectResult(fd: i32): i32;
1628
1787
  * @param host the numeric address to bind.
1629
1788
  * @param port the port, 0 for any.
1630
1789
  * @param flags flag 1 `SO_REUSEPORT`, so several sockets that all ask for it share a port and
1631
- * the kernel spreads the datagrams across them; flag 2 `UDP_GRO` (-95 on Darwin); any other
1632
- * bit is -22.
1790
+ * the kernel spreads the datagrams across them; flag 2 `UDP_GRO` (-95 on Darwin); flag 4, with
1791
+ * flag 1 and a group size N from 1 to 255 in bits 8 to 15, steers QUIC: the port's group hands
1792
+ * each datagram to its socket number (byte 0 of the Destination Connection ID) mod N, in the
1793
+ * order the sockets were bound, and a refused attach closes the socket and answers the errno
1794
+ * (-95 on Darwin). Flag 4 without 1, with 2 or with N 0, N without 4, or any other bit is -22.
1633
1795
  * @returns the socket's descriptor, or a negative errno.
1634
1796
  */
1635
1797
  declare function udpBind(host: string, port: i32, flags: i32): i32;
@@ -2333,6 +2495,48 @@ declare module "nish:fs" {
2333
2495
  * answers `null` for.
2334
2496
  */
2335
2497
  export function readFileBytesSync(path: string): u8[] | null;
2498
+ /**
2499
+ * Open a regular file to read a window at a time: read-only, close-on-exec
2500
+ * and non-blocking. A directory is `-21`, any other kind that is not a
2501
+ * regular file `-22`. Close it with `fileClose` in a `finally`.
2502
+ *
2503
+ * @param path The file to open.
2504
+ * @returns A descriptor (`>= 0`), or the negated Linux errno: `-2` ENOENT,
2505
+ * `-13` EACCES, `-20` ENOTDIR, `-21` EISDIR, `-22` for a FIFO or device,
2506
+ * `-6` ENXIO for a socket, `-23` ENFILE, `-24` EMFILE, `-36` ENAMETOOLONG,
2507
+ * `-40` ELOOP; anything else is `-5` EIO.
2508
+ */
2509
+ export function fileOpen(path: string): i32;
2510
+ /**
2511
+ * Read up to `len` bytes from file offset `pos` into `buf[off .. off + len)`
2512
+ * (`pread`). The window panics as `buf.slice` does when it falls outside
2513
+ * `buf`; the array is never grown.
2514
+ *
2515
+ * @param fd A descriptor from `fileOpen`.
2516
+ * @param buf The array the bytes land in.
2517
+ * @param off Where in `buf` the first byte lands.
2518
+ * @param len The most bytes to read.
2519
+ * @param pos The file offset, an `i64`.
2520
+ * @returns The bytes read, `0` at end of file (and for a `len` of `0`, even
2521
+ * on a closed or negative descriptor), or a negative errno: `-22`
2522
+ * for a negative or overflowing `pos`, `-9` for a closed or negative
2523
+ * descriptor, `-21` EISDIR, `-29` ESPIPE; anything else is `-5` EIO.
2524
+ */
2525
+ export function fileReadAt(fd: i32, buf: u8[], off: i32, len: i32, pos: i64): i32;
2526
+ /**
2527
+ * The file's size in bytes now, from `fstat`.
2528
+ *
2529
+ * @param fd A descriptor from `fileOpen`.
2530
+ * @returns The size, or a negative errno.
2531
+ */
2532
+ export function fileSize(fd: i32): i64;
2533
+ /**
2534
+ * Close a descriptor `fileOpen` gave; close it once.
2535
+ *
2536
+ * @param fd A descriptor from `fileOpen`.
2537
+ * @returns `0`, or a negative errno (`-9` EBADF).
2538
+ */
2539
+ export function fileClose(fd: i32): i32;
2336
2540
  /**
2337
2541
  * Create or truncate the file at mode `0644` and write `data`; a statement,
2338
2542
  * not a value. A symbolic link as the last component of `path` is refused
@@ -2620,6 +2824,32 @@ declare module "nish:process" {
2620
2824
  * 16 MiB.
2621
2825
  */
2622
2826
  export function spawnPiped(argv: string[], stdin: readonly u8[], stdout: u8[], stderr: u8[]): i32;
2827
+ /**
2828
+ * `spawnPiped` with a deadline: the same captured run, killed with `SIGKILL`
2829
+ * and reaped once `timeoutMs` milliseconds have passed since it started, and
2830
+ * a negative `timeoutMs` is no deadline, so `-1` makes it `spawnPiped`. The
2831
+ * deadline covers the whole run, the wait for the child's exit included, on
2832
+ * the monotonic clock. Only the child's pid is killed, never a grandchild,
2833
+ * and a grandchild that keeps an output pipe open makes the run wait for it
2834
+ * until the deadline.
2835
+ *
2836
+ * @param argv The program and its arguments.
2837
+ * @param stdin The bytes the child reads.
2838
+ * @param stdout Grows by the child's stdout, the bytes read before a kill
2839
+ * included; neither `readonly` nor a typed array.
2840
+ * @param stderr Grows by the child's stderr, under the same rule.
2841
+ * @param timeoutMs The milliseconds the run may take; negative for none.
2842
+ * @returns `spawnPiped`'s answer (the status or `128 + n`, `-1`, or `-2` for
2843
+ * a stream past 16 MiB), or `-3` when the deadline passed and the child was
2844
+ * killed.
2845
+ */
2846
+ export function spawnPipedWithin(
2847
+ argv: string[],
2848
+ stdin: readonly u8[],
2849
+ stdout: u8[],
2850
+ stderr: u8[],
2851
+ timeoutMs: i32
2852
+ ): i32;
2623
2853
  /**
2624
2854
  * Start `argv[0]`, searched on `PATH`, exactly as `spawnSync` does (the same
2625
2855
  * vector, environment, streams and working directory), without waiting. The
@@ -2630,7 +2860,10 @@ declare module "nish:process" {
2630
2860
  * @param argv The program and its arguments.
2631
2861
  * @returns The child's pid, or a negative errno: `-22` EINVAL for an empty
2632
2862
  * vector or an argument holding a NUL, `posix_spawnp`'s own error
2633
- * otherwise, so a program that is not there is `-2` ENOENT.
2863
+ * otherwise, so a program that is not there is `-2` ENOENT. Under `--sandbox` with a list of granted
2864
+ * programs, a spawn that cannot be held to one program is refused: -11
2865
+ * (EAGAIN), -38 (ENOSYS), -10 (ECHILD) or the kernel's own error, with a line
2866
+ * on stderr.
2634
2867
  */
2635
2868
  export function spawn(argv: string[]): i32;
2636
2869
  /**
@@ -3310,8 +3543,9 @@ declare module "nish:net" {
3310
3543
  }
3311
3544
 
3312
3545
  /**
3313
- * Element access with no bounds check, signed arithmetic that wraps, and a C
3314
- * string read through a pointer nothing proves.
3546
+ * Element access with no bounds check, signed arithmetic that wraps, a C
3547
+ * string or struct read through a pointer nothing proves, and the errno a C
3548
+ * call left.
3315
3549
  *
3316
3550
  * import { uncheckedGet, uncheckedSet, wrappingAdd } from "nish:unsafe";
3317
3551
  *
@@ -3325,12 +3559,13 @@ declare module "nish:net" {
3325
3559
  * is a call a reader can find. `--emit-checked` prints an `unsafe import` line
3326
3560
  * for each name imported and an `unsafe call` line for each site.
3327
3561
  *
3328
- * The global spelling is refused (NL2456): the six are called only through an
3562
+ * The global spelling is refused (NL2456): the eight are called only through an
3329
3563
  * import of `nish:unsafe`. An index outside `[0, xs.length)` is undefined
3330
3564
  * behaviour, which is the whole of the trade. There is no flag that turns the
3331
3565
  * checks off: `--unchecked-indexing` and `--wrapping` are gone, and the CLI
3332
- * refuses each with exit 2. `readCString` has no Node form, because its
3333
- * `CPtr` comes only from a `declare function`, which Node cannot call.
3566
+ * refuses each with exit 2. `readCString`, `readStruct` and `cErrno` have no Node form,
3567
+ * because their `CPtr` or errno comes only from a `declare function`, which Node
3568
+ * cannot call.
3334
3569
  *
3335
3570
  * **Under Node.** `runtime/nish.mjs` answers the import: the get and set are
3336
3571
  * `xs[i]` and `xs[i] = v`, the wrapping three are `(a + b) | 0`,
@@ -3338,10 +3573,12 @@ declare module "nish:net" {
3338
3573
  * `i64`. A program that stays in range prints the same as natively.
3339
3574
  *
3340
3575
  * **The no-panic scope.** Under `--deny-panics` or `"nish".noPanic`,
3341
- * `uncheckedGet`, `uncheckedSet` and `readCString` are allowed `unchecked`
3342
- * sites, because the import has already said that an index out of range is
3343
- * the author's to rule out. The wrapping three are not sites at all: they
3344
- * are defined for every pair.
3576
+ * `uncheckedGet` and `uncheckedSet` are allowed `unchecked` sites, because
3577
+ * the import has already said that an index out of range is the author's to
3578
+ * rule out. `readCString` and `readStruct` are not: each tests its pointer and
3579
+ * panics with `ffi-null` on `NULL`, a site no proof removes, so the scope
3580
+ * refuses them. The wrapping three are not sites at all: they are defined for
3581
+ * every pair.
3345
3582
  *
3346
3583
  * Rules: https://github.com/amritk/nish/blob/main/docs/LANGUAGE.md#nishunsafe-unchecked-access-and-defined-wrapping
3347
3584
  */
@@ -3404,13 +3641,35 @@ declare module "nish:unsafe" {
3404
3641
  /**
3405
3642
  * The `NUL`-terminated bytes at `p`, copied into a new string by
3406
3643
  * `@nish.ffi.str`, the scan-and-copy a `string` result of a
3407
- * `declare function` uses. It reads and allocates, and is native only.
3644
+ * `declare function` uses, after a `NULL` test that panics with
3645
+ * `ffi-null`. It reads and allocates, and is native only.
3408
3646
  *
3409
- * @param p A pointer from a `declare function`. Undefined behaviour unless
3410
- * it points at a live C string.
3647
+ * @param p A pointer from a `declare function`; `NULL` panics. Undefined
3648
+ * behaviour unless it points at a live C string.
3411
3649
  * @returns A copy of the bytes before the `NUL`; C keeps its buffer.
3412
3650
  */
3413
3651
  export function readCString(p: CPtr): string;
3652
+ /**
3653
+ * A copy of the C struct at `p` in a new instance of the `declare class` the
3654
+ * call's context names (`const lc: Lconv = readStruct(localeconv())`), so C
3655
+ * may free or overwrite its struct straight after. It reads and allocates,
3656
+ * and is native only.
3657
+ *
3658
+ * @param p A pointer from a `declare function`. Undefined behaviour unless
3659
+ * it points at the layout's size of readable bytes.
3660
+ * @returns A copy of the struct; C keeps its own.
3661
+ */
3662
+ export function readStruct<T>(p: CPtr): T;
3663
+ /**
3664
+ * The errno the most recent `declare function` call on this thread left:
3665
+ * a program that names `cErrno` zeroes errno just before every foreign
3666
+ * call and saves it just after, before any result marshalling. Native and
3667
+ * `--profile wasi` only.
3668
+ *
3669
+ * @returns The saved errno; 0 before any foreign call, and 0 after one that
3670
+ * did not set it.
3671
+ */
3672
+ export function cErrno(): i32;
3414
3673
  }
3415
3674
 
3416
3675
  /**
@@ -3460,8 +3719,9 @@ declare module "nish:unsafe" {
3460
3719
  * **Under Node.** `runtime/nish.mjs` answers the import with
3461
3720
  * `runtime/core.mjs`, where a vector is a fresh typed array of its lanes,
3462
3721
  * made once and never written after, whose element type does the lane's
3463
- * arithmetic; a mask is an array of booleans, and a rotate is two shifts and
3464
- * an `or`. `tests/run.js`'s `simd` section holds the native, WASI and Node
3722
+ * arithmetic: an integer lane wraps through its array type (`Uint8Array` for
3723
+ * a `u8` lane), and an `f32` lane rounds through `Float32Array`. A mask is an
3724
+ * array of booleans, and a rotate is two shifts and an `or`. `tests/run.js`'s `simd` section holds the native, WASI and Node
3465
3725
  * outputs to one.
3466
3726
  *
3467
3727
  * Rules: https://github.com/amritk/nish/blob/main/docs/LANGUAGE.md#nishsimd-sixteen-byte-vectors
@@ -4103,6 +4363,7 @@ declare module "nish:simd" {
4103
4363
  export function shr<V extends IntegerVector>(v: V, count: i32): V;
4104
4364
  /**
4105
4365
  * Each lane rotated left by `count`, `llvm.fshl` with `v` as both halves.
4366
+ * Signed and unsigned lanes rotate alike.
4106
4367
  *
4107
4368
  * @param v A `u8x16`, `u16x8`, `i32x4` or `u32x4`.
4108
4369
  * @param count An integer literal below the lane width: 0 to 7, 15 or 31
@@ -4112,6 +4373,7 @@ declare module "nish:simd" {
4112
4373
  export function rotl<V extends IntegerVector>(v: V, count: i32): V;
4113
4374
  /**
4114
4375
  * Each lane rotated right by `count`, `llvm.fshr` with `v` as both halves.
4376
+ * Signed and unsigned lanes rotate alike.
4115
4377
  *
4116
4378
  * @param v A `u8x16`, `u16x8`, `i32x4` or `u32x4`.
4117
4379
  * @param count An integer literal below the lane width: 0 to 7, 15 or 31
@@ -4461,8 +4723,9 @@ declare module "nish:secret" {
4461
4723
  * copy.set(raw)`, then `secret(copy)`.
4462
4724
  *
4463
4725
  * @param value The key material, of a shape a `Secret` may hold (NL2428).
4464
- * @returns The `Secret`, which needs an owner: a `const` or `let`, a
4465
- * `return`, or `wipe` (NL2439 otherwise).
4726
+ * @returns The `Secret`, holding `value` itself and not a copy, so its
4727
+ * storage is the storage `wipe` later zeroes. It needs an owner: a
4728
+ * `const` or `let`, a `return`, or `wipe` (NL2439 otherwise).
4466
4729
  */
4467
4730
  export function secret<T>(value: T): Secret<T>;
4468
4731
  /**
@@ -4473,7 +4736,8 @@ declare module "nish:secret" {
4473
4736
  * (NL2449), and declares a return type that holds no `Secret` (NL2451; an
4474
4737
  * arrow writes it down, NL2452): that type is what leaves.
4475
4738
  *
4476
- * @param s The `Secret` to read, borrowed.
4739
+ * @param s The `Secret` to read, borrowed: it can be exposed again, and is
4740
+ * still to be wiped.
4477
4741
  * @param f The function the value is handed to.
4478
4742
  * @returns What `f` answers.
4479
4743
  */
@@ -4603,6 +4867,28 @@ declare module "nish:region" {
4603
4867
  * @returns The handle.
4604
4868
  */
4605
4869
  export function regionWith<A, T>(arg: A, f: (arg: A) => T): Region<T>;
4870
+ /**
4871
+ * A new region whose parent is the region entered now: `f()` runs with every
4872
+ * allocation landing in it, and what it answers is the root. The child may
4873
+ * keep memory the parent's root reaches, and `free` of the parent frees it
4874
+ * too. Called outside an `enter` or `enterWith` `f` it is NL2702, and it
4875
+ * stops the program when no region is entered.
4876
+ *
4877
+ * @param f Makes the root. It answers memory it allocated, never the
4878
+ * parent's (NL2614).
4879
+ * @returns The handle.
4880
+ */
4881
+ export function child<T>(f: () => T): Region<T>;
4882
+ /**
4883
+ * `child`, with one outside argument handed to `f`.
4884
+ *
4885
+ * @param arg Memory the parent's root reaches, which `f` may keep in the
4886
+ * child, or outer memory it may only read (NL2610).
4887
+ * @param f Makes the root from `arg`. It answers memory it allocated, never
4888
+ * `arg` itself (NL2614).
4889
+ * @returns The handle.
4890
+ */
4891
+ export function childWith<A, T>(arg: A, f: (arg: A) => T): Region<T>;
4606
4892
  /**
4607
4893
  * `f(root)`, with every allocation landing in the region. Stops the program
4608
4894
  * on a dead, foreign or already-entered handle.
@@ -5770,8 +6056,9 @@ interface StringConstructor {
5770
6056
  * arguments in this version"); the type arguments are written out, except that
5771
6057
  * a `const` or `let` annotated with the type takes them from the annotation,
5772
6058
  * and a bare `new Map()` is refused: "`Map` is generic: it must be written with
5773
- * its type arguments". The members are JavaScript's, less `entries` and
5774
- * `forEach` (there is no destructuring, and a method cannot take a function);
6059
+ * its type arguments". The members are JavaScript's, less `entries` (there
6060
+ * is no destructuring); `forEach(f)` is the `for...of` walk with `f`, an arrow
6061
+ * or a top-level function of the value and optionally the key, inlined;
5775
6062
  * anything else is refused as if it did not exist. `set` answers the receiver,
5776
6063
  * so calls chain.
5777
6064
  *