@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/runtime/nish.d.ts CHANGED
@@ -382,11 +382,81 @@ interface Process {
382
382
  declare var process: Process;
383
383
 
384
384
  // ---- Numeric conversions (there is no cast in Nish) ----------------------
385
- // `Number`, `parseInt` and `parseFloat` are `lib.es2022`'s and are not
386
- // declared here; docs/LANGUAGE.md -> Numeric conversions has their rules.
387
385
  // Nothing converts implicitly: a mixed operation, an initializer of another
388
386
  // numeric type and a call with the wrong numeric type are each refused until
389
- // one of the functions below is written.
387
+ // one of the functions below is written. `Number`, `parseInt` and
388
+ // `parseFloat` are `lib.es2022`'s too. `Number`'s call repeats lib's signature;
389
+ // `parseInt` and `parseFloat` add Nish's own as an overload beside lib's, as
390
+ // `runtime/core/numbers.d.ts` declares them. An `i32` and an `f64` are both
391
+ // `number` to `tsc`, so what it accepts does not change; `nish` refuses lib's
392
+ // radix.
393
+
394
+ /**
395
+ * `Number(value)`: `value` as an `f64`, in both number modes. Nish has no
396
+ * `new Number` and no member of `Number`; the call is the conversion, merged
397
+ * into the `NumberConstructor` `lib.es2022` declares.
398
+ *
399
+ * A string is parsed with JavaScript's `Number(s)` rules: ASCII whitespace
400
+ * (` \t\n\v\f\r`, not the Unicode spaces JavaScript also trims) is trimmed
401
+ * on both sides, then the whole string must be one literal, so `Number("")`
402
+ * and `Number(" ")` are `0`, `Number(" 7.5 ")` is `7.5`, and
403
+ * `Number("12px")`, `Number("1e")` and `Number("nan")` are `NaN`.
404
+ * `Number("0x1A")` is `26`, as in JavaScript, and a hex float such as `0x1p3`
405
+ * is also accepted, which JavaScript rejects; the `0b` and `0o` prefixes are
406
+ * `NaN`, which JavaScript reads. Under `--deterministic` a `0x` string reads
407
+ * as `--emit-ts` reads it on every target: a hex float is `NaN`, and a hex
408
+ * integer is correctly rounded whatever the C library.
409
+ *
410
+ * An `i32` or an `i64` converts as `toF64` does, a `boolean` is `1` or `0`,
411
+ * and an `f64` is itself. A numeric literal argument is typed `f64`
412
+ * (`Number(2.5)` works in i32 mode). Exactly one argument; anything other than
413
+ * a string, a number or a boolean is refused: "`Number` expects a string,
414
+ * number, or boolean, got i32[]". Capability: none.
415
+ */
416
+ interface NumberConstructor {
417
+ /**
418
+ * `value` as an `f64`, by the rules on `NumberConstructor` above.
419
+ * Capability: none.
420
+ *
421
+ * @param value a string, an `i32`, an `i64`, an `f64` or a `boolean`;
422
+ * `nish` requires it, though `lib`'s signature marks it optional.
423
+ * @returns an `f64`, in both number modes.
424
+ */
425
+ (value?: any): number;
426
+ }
427
+
428
+ /**
429
+ * The base-10 integer at the start of `s`, as an `i32`: ASCII whitespace,
430
+ * an optional sign, then digits, stopping at the first other character
431
+ * (`parseInt("42px")` is `42`). Three deviations from JavaScript: there is no
432
+ * `NaN` in an `i32`, so no digits give `0` (`parseInt("abc")`,
433
+ * `parseInt("")`); a value beyond the `i32` range saturates as `toI32` does
434
+ * (`parseInt("99999999999")` is `2147483647`); and a `0x` prefix is not
435
+ * hexadecimal (`parseInt("0x10")` is `0`). Always an `i32`, under
436
+ * `--number-mode f64` too, where `Number` reads a wider value. Exactly one
437
+ * argument: there is no radix, since base 10 is the only one. A non-string is
438
+ * refused: "`parseInt` expects a string, got i32". Capability: none.
439
+ *
440
+ * @param s the text read.
441
+ * @returns the integer, or `0` when there are no digits.
442
+ */
443
+ declare function parseInt(s: string): i32;
444
+ /**
445
+ * The decimal number at the start of `s`, as an `f64`: ASCII whitespace,
446
+ * then the longest decimal literal (`[sign] digits [. digits] [e [sign]
447
+ * digits]`, `. digits`, or `[sign] Infinity`), correctly rounded.
448
+ * `parseFloat("3.14xyz")` is `3.14` and `parseFloat("1e")` is `1`. One
449
+ * deviation from JavaScript: a `0x` prefix is read as hexadecimal
450
+ * (`parseFloat("0x1A")` is `26`, where JavaScript stops at the `x` and gives
451
+ * `0`). Under `--deterministic` a `0x` string reads as `--emit-ts` reads it
452
+ * on every target: the hex integer, correctly rounded, stopping at a hex
453
+ * float's `.` or `p` (`0x1p3` is `1`). Exactly one argument. Capability: none.
454
+ *
455
+ * @param s the text read.
456
+ * @returns the number, or `NaN` when there is none (`parseFloat("abc")`,
457
+ * `parseFloat("")`, `parseFloat(".")`).
458
+ */
459
+ declare function parseFloat(s: string): f64;
390
460
 
391
461
  /**
392
462
  * `x` as an `i32`. An integer of 32 bits or more wraps to its low 32 bits, a
@@ -637,7 +707,7 @@ declare function isExecutableSync(path: string): boolean;
637
707
  // ---- Unsafe (`nish:unsafe`) --------------------------------------------------
638
708
  //
639
709
  // Declared as globals so `tsc` reads a program that imports them, as every
640
- // `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
641
711
  // imported from `nish:unsafe`: the import is the module's visible opt-in. The
642
712
  // refusal reads `` `uncheckedGet` is a `nish:unsafe` function and is called only
643
713
  // through an import of it `` (NL2456). An import may rename (`as`) and cannot be
@@ -752,6 +822,50 @@ declare function wrappingMul<T extends i32 | i64>(a: T, b: T): T;
752
822
  * @returns a copy of the bytes before the first `NUL`.
753
823
  */
754
824
  declare function readCString(p: CPtr): string;
825
+ /**
826
+ * A copy of the C struct at `p`, in a new instance of the `declare class` the
827
+ * call's context names: `const lc: Lconv = readStruct(localeconv())`. A type
828
+ * argument is not written at a call site (`readStruct<Lconv>(p)` is refused,
829
+ * as `identity<i32>(7)` is), so the layout comes from an annotation, a
830
+ * parameter or a return type, as `Ok(v)`'s type does; a call with none, or one
831
+ * whose context is not a `declare class`, is refused. It copies the layout's
832
+ * size in bytes, so the class must describe the struct's first fields (a
833
+ * prefix is enough) and `p` must point at that many readable bytes: undefined
834
+ * behaviour otherwise, as with `readCString`.
835
+ *
836
+ * The copy is the program's own: C may free or overwrite its struct straight
837
+ * after the call, and a `CPtr` field of the copy is still C's pointer, read
838
+ * with `readCString` while C keeps it live. The parameter is `CPtr`, so an
839
+ * unnarrowed `CPtr | null` is refused and `if (p !== null)` comes first.
840
+ *
841
+ * A `NULL` `p` is tested before the copy and panics with the `ffi-null` kind,
842
+ * naming the layout, since a `declare function` declared to answer `CPtr` may
843
+ * still answer `NULL`. No proof removes that test, so `--deny-panics` refuses
844
+ * the call; `--emit-panics` lists it as an `ffi-null` site, with the copy's
845
+ * `oom` beside it.
846
+ *
847
+ * The copy is new on every call and is the calling function's allocation, held
848
+ * by the arena like `new`. It lowers the same on wasm32, where a pointer is 4
849
+ * bytes and the layout is sized to match. It has no Node form.
850
+ * Capability: unsafe.
851
+ *
852
+ * @param p the address of a C struct a `declare function` handed back.
853
+ * @returns a copy of the struct, in the `declare class` its context names.
854
+ */
855
+ declare function readStruct<T>(p: CPtr): T;
856
+
857
+ /**
858
+ * `cErrno()`, from `nish:unsafe` (WP27 S5): the errno the most recent
859
+ * `declare function` call on this thread left, or 0 before any. Naming it
860
+ * anywhere in a program makes every foreign call zero errno just before it
861
+ * and save it just after, before the result is copied or anything allocated,
862
+ * so the runtime's own work after the call cannot change it. Meaningful only
863
+ * where the C function documents that it sets errno. Per thread; refused on a
864
+ * freestanding wasm32 build, which has no libc. Capability: unsafe.
865
+ *
866
+ * @returns the saved errno, an `i32`.
867
+ */
868
+ declare function cErrno(): i32;
755
869
 
756
870
  // ---- Streams and files (globals: Nish has no package resolution) ---------
757
871
 
@@ -853,6 +967,56 @@ declare function readFileSyncOrNull(path: string): string | null;
853
967
  * @returns its bytes, or `null`.
854
968
  */
855
969
  declare function readFileBytesSync(path: string): u8[] | null;
970
+ /**
971
+ * Open a regular file to read a window at a time (WP44): read-only,
972
+ * close-on-exec and non-blocking, so a FIFO or a device never waits. A
973
+ * directory is refused with `-21` and any other kind that is not a regular
974
+ * file with `-22`, the descriptor closed again first. Pair it with `fileClose`
975
+ * in a `try`/`finally`. A freestanding `wasm32` build refuses the call (NL2718);
976
+ * `--profile wasi` reads through WASI. Capability: fs.read.
977
+ *
978
+ * @param path the file to open; a NUL in it is refused as a missing path is.
979
+ * @returns a descriptor (`>= 0`), or the negated Linux errno: `-2` ENOENT,
980
+ * `-13` EACCES, `-20` ENOTDIR, `-21` EISDIR, `-22` EINVAL for a FIFO or
981
+ * device, `-6` ENXIO for a socket, `-23` ENFILE, `-24` EMFILE, `-36`
982
+ * ENAMETOOLONG, `-40` ELOOP; anything else is `-5` EIO.
983
+ */
984
+ declare function fileOpen(path: string): i32;
985
+ /**
986
+ * Read up to `len` bytes from byte `pos` of the file into `buf[off .. off +
987
+ * len)`, without moving any shared offset (`pread`). The window is checked as
988
+ * `buf.slice` checks it and panics the same way when it falls outside `buf`.
989
+ * The array is never grown: memory follows the window. Capability: fs.read.
990
+ *
991
+ * @param fd a descriptor from `fileOpen`.
992
+ * @param buf the array the bytes land in; its length is unchanged.
993
+ * @param off where in `buf` the first byte lands.
994
+ * @param len the most bytes to read.
995
+ * @param pos the file offset to read from, an `i64`.
996
+ * @returns the bytes read, `0` at end of file (and for a `len` of `0`, even on
997
+ * a closed or negative descriptor), or a negative errno: `-22`
998
+ * EINVAL for a negative `pos` or one where `pos + len` overflows, `-9` EBADF
999
+ * for a closed or negative descriptor, `-21` EISDIR, `-29` ESPIPE; anything
1000
+ * else is `-5` EIO.
1001
+ */
1002
+ declare function fileReadAt(fd: i32, buf: u8[], off: i32, len: i32, pos: i64): i32;
1003
+ /**
1004
+ * The file's size in bytes now, from `fstat`: a file may grow or shrink between
1005
+ * this and a read, which then answers what is there. Capability: fs.read.
1006
+ *
1007
+ * @param fd a descriptor from `fileOpen`.
1008
+ * @returns the size, an `i64` past 2^31 when the file is, or a negative errno
1009
+ * (`-9` EBADF for a closed descriptor).
1010
+ */
1011
+ declare function fileSize(fd: i32): i64;
1012
+ /**
1013
+ * Close a descriptor `fileOpen` gave. A second close answers `-9`, unless the
1014
+ * number has been handed out again since, so close once. Capability: fs.read.
1015
+ *
1016
+ * @param fd a descriptor from `fileOpen`.
1017
+ * @returns `0`, or a negative errno (`-9` EBADF).
1018
+ */
1019
+ declare function fileClose(fd: i32): i32;
856
1020
  /**
857
1021
  * The file's contents replaced by `data`, created at 0644 when missing;
858
1022
  * statement position. A symbolic link as the last component of the path is
@@ -1038,6 +1202,14 @@ declare function realpathSync(path: string): string | null;
1038
1202
  * an embedded NUL truncates one. A `wasm32` build and a WASI build have no
1039
1203
  * processes, and a WASI build always answers `-1`. Capability: process.spawn.
1040
1204
  *
1205
+ * Under `--sandbox`, a dependency's `process.spawn=<program>` grants can make
1206
+ * a list of the only programs the build may start. A program this builtin
1207
+ * starts under such a list cannot start another: an exec by it, or by
1208
+ * anything it forks, fails, so a granted `#!` script runs its interpreter's
1209
+ * builtins alone. Where the kernel or a container's seccomp profile refuses
1210
+ * what that takes (Docker's default profile does), the spawn fails and one
1211
+ * line on stderr says so.
1212
+ *
1041
1213
  * @param argv the program and its arguments.
1042
1214
  * @returns the exit status, or `128 + n` when signal `n` killed it (the shell's
1043
1215
  * convention), or `-1` when the vector is empty — there is no `argv[0]` to
@@ -1053,6 +1225,9 @@ declare function spawnSync(argv: string[]): number;
1053
1225
  * separately, with its own offset, so the streams would overwrite rather than
1054
1226
  * interleave; capture them apart and concatenate. Capability: process.spawn.
1055
1227
  *
1228
+ * Under `--sandbox` with a list of granted programs, the program started
1229
+ * cannot start another, as for `spawnSync`.
1230
+ *
1056
1231
  * @param argv the program and its arguments.
1057
1232
  * @param stdoutPath the file the child's stdout goes to, or `""`.
1058
1233
  * @param stderrPath the file the child's stderr goes to, or `""`.
@@ -1079,6 +1254,9 @@ declare function spawnSyncTo(argv: string[], stdoutPath: string, stderrPath: str
1079
1254
  * length is fixed. A `wasm32` build refuses it (NL2404). Capability:
1080
1255
  * process.spawn.
1081
1256
  *
1257
+ * Under `--sandbox` with a list of granted programs, the program started
1258
+ * cannot start another, as for `spawnSync`.
1259
+ *
1082
1260
  * @param argv the program and its arguments.
1083
1261
  * @param stdin the bytes the child reads.
1084
1262
  * @param stdout receives what the child prints on stdout.
@@ -1088,6 +1266,43 @@ declare function spawnSyncTo(argv: string[], stdoutPath: string, stderrPath: str
1088
1266
  * that fails, and a WASI build), or `-2` when a stream passed 16 MiB.
1089
1267
  */
1090
1268
  declare function spawnPiped(argv: string[], stdin: readonly u8[], stdout: u8[], stderr: u8[]): i32;
1269
+ /**
1270
+ * `spawnPiped` with a deadline: the same captured run, killed once `timeoutMs`
1271
+ * milliseconds have passed since it started. A negative `timeoutMs` is no
1272
+ * deadline, as `pollWait`'s is, so `spawnPipedWithin(argv, stdin, stdout,
1273
+ * stderr, -1)` is `spawnPiped(argv, stdin, stdout, stderr)`. When the deadline
1274
+ * passes the child is killed with `SIGKILL` and reaped, the answer is `-3`, and
1275
+ * `stdout` and `stderr` keep every byte read before the kill.
1276
+ *
1277
+ * The deadline covers the whole run, the wait for the child's exit included,
1278
+ * and is measured on the monotonic clock. Only the child's own pid is killed:
1279
+ * a grandchild it started is not, and one that keeps an output pipe open makes
1280
+ * the run wait for it, so a child that exited 0 can still answer `-3`. The
1281
+ * rest is `spawnPiped`'s: the same environment, descriptors, SIGPIPE handling
1282
+ * and 16 MiB cap, `readonly` and typed-array spellings refused for the two
1283
+ * output arrays, refused by a `wasm32` build (NL2404). Capability:
1284
+ * process.spawn.
1285
+ *
1286
+ * Under `--sandbox` with a list of granted programs, the program started
1287
+ * cannot start another, as for `spawnSync`.
1288
+ *
1289
+ * @param argv the program and its arguments.
1290
+ * @param stdin the bytes the child reads.
1291
+ * @param stdout receives what the child prints on stdout.
1292
+ * @param stderr receives what the child prints on stderr.
1293
+ * @param timeoutMs the milliseconds the run may take, or a negative number for
1294
+ * no deadline.
1295
+ * @returns the status as `spawnPiped` answers it (the exit code, `128 + n`,
1296
+ * `-1` for a child that cannot be started or a WASI build, `-2` when a stream
1297
+ * passed 16 MiB), or `-3` when the deadline passed and the child was killed.
1298
+ */
1299
+ declare function spawnPipedWithin(
1300
+ argv: string[],
1301
+ stdin: readonly u8[],
1302
+ stdout: u8[],
1303
+ stderr: u8[],
1304
+ timeoutMs: i32
1305
+ ): i32;
1091
1306
  /**
1092
1307
  * `spawn` with the child holding `fds[i]` as descriptor `3 + i`, close-on-exec
1093
1308
  * cleared there. Its streams 0 to 2 are this process's, and no other descriptor
@@ -1100,12 +1315,18 @@ declare function spawnPiped(argv: string[], stdin: readonly u8[], stdout: u8[],
1100
1315
  * under Node, which throws. A `wasm32` build refuses it (NL2404).
1101
1316
  * Capability: process.spawn.
1102
1317
  *
1318
+ * Under `--sandbox` with a list of granted programs, the program started
1319
+ * cannot start another, as for `spawnSync`.
1320
+ *
1103
1321
  * @param argv the program and its arguments.
1104
1322
  * @param fds the descriptors handed down, at most 64.
1105
1323
  * @returns the child's pid, or a negative errno: -22 more than 64 descriptors
1106
1324
  * or a negative one (before anything is opened or started), -9 one that is
1107
1325
  * not open, -24 (or -22 above the descriptor limit) a scratch descriptor the
1108
- * system cannot give.
1326
+ * system cannot give. Under `--sandbox` with a list of granted
1327
+ * programs, a spawn that cannot be held to one program is refused: -11
1328
+ * (EAGAIN), -38 (ENOSYS), -10 (ECHILD) or the kernel's own error, with a line
1329
+ * on stderr.
1109
1330
  */
1110
1331
  declare function spawnWithFds(argv: string[], fds: readonly i32[]): i32;
1111
1332
  /**
@@ -1120,10 +1341,16 @@ declare function spawnWithFds(argv: string[], fds: readonly i32[]): i32;
1120
1341
  * `nish:process`.
1121
1342
  * Capability: process.spawn.
1122
1343
  *
1344
+ * Under `--sandbox` with a list of granted programs, the program started
1345
+ * cannot start another, as for `spawnSync`.
1346
+ *
1123
1347
  * @param argv the program and its arguments.
1124
1348
  * @returns the child's pid, or a negative errno: -22 (EINVAL) for an empty
1125
1349
  * vector or an argument holding a NUL, and `posix_spawnp`'s own error
1126
- * otherwise, so a program that is not there is -2 (ENOENT).
1350
+ * otherwise, so a program that is not there is -2 (ENOENT). Under `--sandbox` with a list of granted
1351
+ * programs, a spawn that cannot be held to one program is refused: -11
1352
+ * (EAGAIN), -38 (ENOSYS), -10 (ECHILD) or the kernel's own error, with a line
1353
+ * on stderr.
1127
1354
  */
1128
1355
  declare function spawn(argv: string[]): i32;
1129
1356
  /**
@@ -1558,8 +1785,11 @@ declare function connectResult(fd: i32): i32;
1558
1785
  * @param host the numeric address to bind.
1559
1786
  * @param port the port, 0 for any.
1560
1787
  * @param flags flag 1 `SO_REUSEPORT`, so several sockets that all ask for it share a port and
1561
- * the kernel spreads the datagrams across them; flag 2 `UDP_GRO` (-95 on Darwin); any other
1562
- * bit is -22.
1788
+ * the kernel spreads the datagrams across them; flag 2 `UDP_GRO` (-95 on Darwin); flag 4, with
1789
+ * flag 1 and a group size N from 1 to 255 in bits 8 to 15, steers QUIC: the port's group hands
1790
+ * each datagram to its socket number (byte 0 of the Destination Connection ID) mod N, in the
1791
+ * order the sockets were bound, and a refused attach closes the socket and answers the errno
1792
+ * (-95 on Darwin). Flag 4 without 1, with 2 or with N 0, N without 4, or any other bit is -22.
1563
1793
  * @returns the socket's descriptor, or a negative errno.
1564
1794
  */
1565
1795
  declare function udpBind(host: string, port: i32, flags: i32): i32;
@@ -2123,10 +2353,34 @@ declare function setTerminalRestore(bytes: string): i32;
2123
2353
 
2124
2354
  // ---- `Date` and `crypto` (WP34 N3) -----------------------------------------------
2125
2355
  //
2126
- // `lib.es2022` already declares `Date`, whose `now()` is Nish's one member of it,
2127
- // so nothing is added: `tsc` accepting `new Date()` is `tsc` not being Nish's
2128
- // checker. `crypto` is a Web API that `lib.es2022` leaves out, so it is
2129
- // declared with its one member, typed as Nish types it.
2356
+ // `lib.es2022` already declares `Date`, whose `now()` is Nish's one member of it;
2357
+ // `DateConstructor` below repeats lib's signature of it to document it, and adds
2358
+ // nothing: `tsc` accepting `new Date()` is `tsc` not being Nish's checker.
2359
+ // `crypto` is a Web API that `lib.es2022` leaves out, so it is declared with its
2360
+ // one member, typed as Nish types it.
2361
+
2362
+ /**
2363
+ * `Date`, merged into the `DateConstructor` `lib.es2022` declares. Nish has no
2364
+ * `Date` object: `Date.now()` is its one member, and `new Date()`, `Date()`,
2365
+ * `Date.parse`, `Date.UTC`, any other member, and `Date` or `Date.now` used as
2366
+ * a value are each refused: "`new Date()` is refused: Nish has no `Date`
2367
+ * object; the one `Date` member is `Date.now()`, the wall clock in
2368
+ * milliseconds" (NL2401).
2369
+ */
2370
+ interface DateConstructor {
2371
+ /**
2372
+ * The wall clock, `CLOCK_REALTIME`, in whole milliseconds since 1970-01-01
2373
+ * UTC, as JavaScript answers it: the milliseconds are counted as integers and
2374
+ * then converted, so the value is exact and a time before 1970 floors. An
2375
+ * `f64` in both number modes, because an `i32` of milliseconds overflowed in
2376
+ * January 1970. It can go backwards when the clock is corrected, so an
2377
+ * interval is `monotonicNanos`'s job, not this one's. A wasm32 build has no
2378
+ * operating system to ask, and refuses it. Capability: clock.
2379
+ *
2380
+ * @returns the milliseconds, an `f64`.
2381
+ */
2382
+ now(): number;
2383
+ }
2130
2384
 
2131
2385
  /** The system's random source, with its one member. */
2132
2386
  declare var crypto: {
@@ -2239,6 +2493,48 @@ declare module "nish:fs" {
2239
2493
  * answers `null` for.
2240
2494
  */
2241
2495
  export function readFileBytesSync(path: string): u8[] | null;
2496
+ /**
2497
+ * Open a regular file to read a window at a time: read-only, close-on-exec
2498
+ * and non-blocking. A directory is `-21`, any other kind that is not a
2499
+ * regular file `-22`. Close it with `fileClose` in a `finally`.
2500
+ *
2501
+ * @param path The file to open.
2502
+ * @returns A descriptor (`>= 0`), or the negated Linux errno: `-2` ENOENT,
2503
+ * `-13` EACCES, `-20` ENOTDIR, `-21` EISDIR, `-22` for a FIFO or device,
2504
+ * `-6` ENXIO for a socket, `-23` ENFILE, `-24` EMFILE, `-36` ENAMETOOLONG,
2505
+ * `-40` ELOOP; anything else is `-5` EIO.
2506
+ */
2507
+ export function fileOpen(path: string): i32;
2508
+ /**
2509
+ * Read up to `len` bytes from file offset `pos` into `buf[off .. off + len)`
2510
+ * (`pread`). The window panics as `buf.slice` does when it falls outside
2511
+ * `buf`; the array is never grown.
2512
+ *
2513
+ * @param fd A descriptor from `fileOpen`.
2514
+ * @param buf The array the bytes land in.
2515
+ * @param off Where in `buf` the first byte lands.
2516
+ * @param len The most bytes to read.
2517
+ * @param pos The file offset, an `i64`.
2518
+ * @returns The bytes read, `0` at end of file (and for a `len` of `0`, even
2519
+ * on a closed or negative descriptor), or a negative errno: `-22`
2520
+ * for a negative or overflowing `pos`, `-9` for a closed or negative
2521
+ * descriptor, `-21` EISDIR, `-29` ESPIPE; anything else is `-5` EIO.
2522
+ */
2523
+ export function fileReadAt(fd: i32, buf: u8[], off: i32, len: i32, pos: i64): i32;
2524
+ /**
2525
+ * The file's size in bytes now, from `fstat`.
2526
+ *
2527
+ * @param fd A descriptor from `fileOpen`.
2528
+ * @returns The size, or a negative errno.
2529
+ */
2530
+ export function fileSize(fd: i32): i64;
2531
+ /**
2532
+ * Close a descriptor `fileOpen` gave; close it once.
2533
+ *
2534
+ * @param fd A descriptor from `fileOpen`.
2535
+ * @returns `0`, or a negative errno (`-9` EBADF).
2536
+ */
2537
+ export function fileClose(fd: i32): i32;
2242
2538
  /**
2243
2539
  * Create or truncate the file at mode `0644` and write `data`; a statement,
2244
2540
  * not a value. A symbolic link as the last component of `path` is refused
@@ -2526,6 +2822,32 @@ declare module "nish:process" {
2526
2822
  * 16 MiB.
2527
2823
  */
2528
2824
  export function spawnPiped(argv: string[], stdin: readonly u8[], stdout: u8[], stderr: u8[]): i32;
2825
+ /**
2826
+ * `spawnPiped` with a deadline: the same captured run, killed with `SIGKILL`
2827
+ * and reaped once `timeoutMs` milliseconds have passed since it started, and
2828
+ * a negative `timeoutMs` is no deadline, so `-1` makes it `spawnPiped`. The
2829
+ * deadline covers the whole run, the wait for the child's exit included, on
2830
+ * the monotonic clock. Only the child's pid is killed, never a grandchild,
2831
+ * and a grandchild that keeps an output pipe open makes the run wait for it
2832
+ * until the deadline.
2833
+ *
2834
+ * @param argv The program and its arguments.
2835
+ * @param stdin The bytes the child reads.
2836
+ * @param stdout Grows by the child's stdout, the bytes read before a kill
2837
+ * included; neither `readonly` nor a typed array.
2838
+ * @param stderr Grows by the child's stderr, under the same rule.
2839
+ * @param timeoutMs The milliseconds the run may take; negative for none.
2840
+ * @returns `spawnPiped`'s answer (the status or `128 + n`, `-1`, or `-2` for
2841
+ * a stream past 16 MiB), or `-3` when the deadline passed and the child was
2842
+ * killed.
2843
+ */
2844
+ export function spawnPipedWithin(
2845
+ argv: string[],
2846
+ stdin: readonly u8[],
2847
+ stdout: u8[],
2848
+ stderr: u8[],
2849
+ timeoutMs: i32
2850
+ ): i32;
2529
2851
  /**
2530
2852
  * Start `argv[0]`, searched on `PATH`, exactly as `spawnSync` does (the same
2531
2853
  * vector, environment, streams and working directory), without waiting. The
@@ -2536,7 +2858,10 @@ declare module "nish:process" {
2536
2858
  * @param argv The program and its arguments.
2537
2859
  * @returns The child's pid, or a negative errno: `-22` EINVAL for an empty
2538
2860
  * vector or an argument holding a NUL, `posix_spawnp`'s own error
2539
- * otherwise, so a program that is not there is `-2` ENOENT.
2861
+ * otherwise, so a program that is not there is `-2` ENOENT. Under `--sandbox` with a list of granted
2862
+ * programs, a spawn that cannot be held to one program is refused: -11
2863
+ * (EAGAIN), -38 (ENOSYS), -10 (ECHILD) or the kernel's own error, with a line
2864
+ * on stderr.
2540
2865
  */
2541
2866
  export function spawn(argv: string[]): i32;
2542
2867
  /**
@@ -3216,8 +3541,9 @@ declare module "nish:net" {
3216
3541
  }
3217
3542
 
3218
3543
  /**
3219
- * Element access with no bounds check, signed arithmetic that wraps, and a C
3220
- * string read through a pointer nothing proves.
3544
+ * Element access with no bounds check, signed arithmetic that wraps, a C
3545
+ * string or struct read through a pointer nothing proves, and the errno a C
3546
+ * call left.
3221
3547
  *
3222
3548
  * import { uncheckedGet, uncheckedSet, wrappingAdd } from "nish:unsafe";
3223
3549
  *
@@ -3231,12 +3557,13 @@ declare module "nish:net" {
3231
3557
  * is a call a reader can find. `--emit-checked` prints an `unsafe import` line
3232
3558
  * for each name imported and an `unsafe call` line for each site.
3233
3559
  *
3234
- * The global spelling is refused (NL2456): the six are called only through an
3560
+ * The global spelling is refused (NL2456): the eight are called only through an
3235
3561
  * import of `nish:unsafe`. An index outside `[0, xs.length)` is undefined
3236
3562
  * behaviour, which is the whole of the trade. There is no flag that turns the
3237
3563
  * checks off: `--unchecked-indexing` and `--wrapping` are gone, and the CLI
3238
- * refuses each with exit 2. `readCString` has no Node form, because its
3239
- * `CPtr` comes only from a `declare function`, which Node cannot call.
3564
+ * refuses each with exit 2. `readCString`, `readStruct` and `cErrno` have no Node form,
3565
+ * because their `CPtr` or errno comes only from a `declare function`, which Node
3566
+ * cannot call.
3240
3567
  *
3241
3568
  * **Under Node.** `runtime/nish.mjs` answers the import: the get and set are
3242
3569
  * `xs[i]` and `xs[i] = v`, the wrapping three are `(a + b) | 0`,
@@ -3246,8 +3573,10 @@ declare module "nish:net" {
3246
3573
  * **The no-panic scope.** Under `--deny-panics` or `"nish".noPanic`,
3247
3574
  * `uncheckedGet`, `uncheckedSet` and `readCString` are allowed `unchecked`
3248
3575
  * sites, because the import has already said that an index out of range is
3249
- * the author's to rule out. The wrapping three are not sites at all: they
3250
- * are defined for every pair.
3576
+ * the author's to rule out. `readStruct` is not: it tests its pointer and
3577
+ * panics with `ffi-null` on `NULL`, a site no proof removes, so the scope
3578
+ * refuses it. The wrapping three are not sites at all: they are defined for
3579
+ * every pair.
3251
3580
  *
3252
3581
  * Rules: https://github.com/amritk/nish/blob/main/docs/LANGUAGE.md#nishunsafe-unchecked-access-and-defined-wrapping
3253
3582
  */
@@ -3317,6 +3646,27 @@ declare module "nish:unsafe" {
3317
3646
  * @returns A copy of the bytes before the `NUL`; C keeps its buffer.
3318
3647
  */
3319
3648
  export function readCString(p: CPtr): string;
3649
+ /**
3650
+ * A copy of the C struct at `p` in a new instance of the `declare class` the
3651
+ * call's context names (`const lc: Lconv = readStruct(localeconv())`), so C
3652
+ * may free or overwrite its struct straight after. It reads and allocates,
3653
+ * and is native only.
3654
+ *
3655
+ * @param p A pointer from a `declare function`. Undefined behaviour unless
3656
+ * it points at the layout's size of readable bytes.
3657
+ * @returns A copy of the struct; C keeps its own.
3658
+ */
3659
+ export function readStruct<T>(p: CPtr): T;
3660
+ /**
3661
+ * The errno the most recent `declare function` call on this thread left:
3662
+ * a program that names `cErrno` zeroes errno just before every foreign
3663
+ * call and saves it just after, before any result marshalling. Native and
3664
+ * `--profile wasi` only.
3665
+ *
3666
+ * @returns The saved errno; 0 before any foreign call, and 0 after one that
3667
+ * did not set it.
3668
+ */
3669
+ export function cErrno(): i32;
3320
3670
  }
3321
3671
 
3322
3672
  /**
@@ -3366,8 +3716,9 @@ declare module "nish:unsafe" {
3366
3716
  * **Under Node.** `runtime/nish.mjs` answers the import with
3367
3717
  * `runtime/core.mjs`, where a vector is a fresh typed array of its lanes,
3368
3718
  * made once and never written after, whose element type does the lane's
3369
- * arithmetic; a mask is an array of booleans, and a rotate is two shifts and
3370
- * an `or`. `tests/run.js`'s `simd` section holds the native, WASI and Node
3719
+ * arithmetic: an integer lane wraps through its array type (`Uint8Array` for
3720
+ * a `u8` lane), and an `f32` lane rounds through `Float32Array`. A mask is an
3721
+ * array of booleans, and a rotate is two shifts and an `or`. `tests/run.js`'s `simd` section holds the native, WASI and Node
3371
3722
  * outputs to one.
3372
3723
  *
3373
3724
  * Rules: https://github.com/amritk/nish/blob/main/docs/LANGUAGE.md#nishsimd-sixteen-byte-vectors
@@ -3490,22 +3841,22 @@ declare module "nish:simd" {
3490
3841
  * A `u8x16` of sixteen lanes: one `insertelement` per lane, which `opt`
3491
3842
  * folds to a constant vector when every lane is one.
3492
3843
  *
3493
- * @param a0 Lane 0, a `u8`; `a1` to `a15` are lanes 1 to 15 in order.
3494
- * @param a1 Lane 1.
3495
- * @param a2 Lane 2.
3496
- * @param a3 Lane 3.
3497
- * @param a4 Lane 4.
3498
- * @param a5 Lane 5.
3499
- * @param a6 Lane 6.
3500
- * @param a7 Lane 7.
3501
- * @param a8 Lane 8.
3502
- * @param a9 Lane 9.
3503
- * @param a10 Lane 10.
3504
- * @param a11 Lane 11.
3505
- * @param a12 Lane 12.
3506
- * @param a13 Lane 13.
3507
- * @param a14 Lane 14.
3508
- * @param a15 Lane 15.
3844
+ * @param a0 Lane 0, a `u8`.
3845
+ * @param a1 Lane 1, a `u8`.
3846
+ * @param a2 Lane 2, a `u8`.
3847
+ * @param a3 Lane 3, a `u8`.
3848
+ * @param a4 Lane 4, a `u8`.
3849
+ * @param a5 Lane 5, a `u8`.
3850
+ * @param a6 Lane 6, a `u8`.
3851
+ * @param a7 Lane 7, a `u8`.
3852
+ * @param a8 Lane 8, a `u8`.
3853
+ * @param a9 Lane 9, a `u8`.
3854
+ * @param a10 Lane 10, a `u8`.
3855
+ * @param a11 Lane 11, a `u8`.
3856
+ * @param a12 Lane 12, a `u8`.
3857
+ * @param a13 Lane 13, a `u8`.
3858
+ * @param a14 Lane 14, a `u8`.
3859
+ * @param a15 Lane 15, a `u8`.
3509
3860
  * @returns The vector.
3510
3861
  */
3511
3862
  export function u8x16(
@@ -3515,52 +3866,52 @@ declare module "nish:simd" {
3515
3866
  /**
3516
3867
  * A `u16x8` of 8 lanes, one argument per lane, lane 0 first.
3517
3868
  *
3518
- * @param a0 Lane 0, a `u16`; `a1` to `a7` are lanes 1 to 7 in order.
3519
- * @param a1 Lane 1.
3520
- * @param a2 Lane 2.
3521
- * @param a3 Lane 3.
3522
- * @param a4 Lane 4.
3523
- * @param a5 Lane 5.
3524
- * @param a6 Lane 6.
3525
- * @param a7 Lane 7.
3869
+ * @param a0 Lane 0, a `u16`.
3870
+ * @param a1 Lane 1, a `u16`.
3871
+ * @param a2 Lane 2, a `u16`.
3872
+ * @param a3 Lane 3, a `u16`.
3873
+ * @param a4 Lane 4, a `u16`.
3874
+ * @param a5 Lane 5, a `u16`.
3875
+ * @param a6 Lane 6, a `u16`.
3876
+ * @param a7 Lane 7, a `u16`.
3526
3877
  * @returns The vector.
3527
3878
  */
3528
3879
  export function u16x8(a0: u16, a1: u16, a2: u16, a3: u16, a4: u16, a5: u16, a6: u16, a7: u16): u16x8;
3529
3880
  /**
3530
3881
  * An `i32x4` of 4 lanes, one argument per lane, lane 0 first.
3531
3882
  *
3532
- * @param a0 Lane 0, an `i32`; `a1` to `a3` are lanes 1 to 3 in order.
3533
- * @param a1 Lane 1.
3534
- * @param a2 Lane 2.
3535
- * @param a3 Lane 3.
3883
+ * @param a0 Lane 0, an `i32`.
3884
+ * @param a1 Lane 1, an `i32`.
3885
+ * @param a2 Lane 2, an `i32`.
3886
+ * @param a3 Lane 3, an `i32`.
3536
3887
  * @returns The vector.
3537
3888
  */
3538
3889
  export function i32x4(a0: i32, a1: i32, a2: i32, a3: i32): i32x4;
3539
3890
  /**
3540
3891
  * A `u32x4` of 4 lanes, one argument per lane, lane 0 first.
3541
3892
  *
3542
- * @param a0 Lane 0, a `u32`; `a1` to `a3` are lanes 1 to 3 in order.
3543
- * @param a1 Lane 1.
3544
- * @param a2 Lane 2.
3545
- * @param a3 Lane 3.
3893
+ * @param a0 Lane 0, a `u32`.
3894
+ * @param a1 Lane 1, a `u32`.
3895
+ * @param a2 Lane 2, a `u32`.
3896
+ * @param a3 Lane 3, a `u32`.
3546
3897
  * @returns The vector.
3547
3898
  */
3548
3899
  export function u32x4(a0: u32, a1: u32, a2: u32, a3: u32): u32x4;
3549
3900
  /**
3550
3901
  * An `f32x4` of 4 lanes, one argument per lane, lane 0 first.
3551
3902
  *
3552
- * @param a0 Lane 0, an `f32`; `a1` to `a3` are lanes 1 to 3 in order.
3553
- * @param a1 Lane 1.
3554
- * @param a2 Lane 2.
3555
- * @param a3 Lane 3.
3903
+ * @param a0 Lane 0, an `f32`.
3904
+ * @param a1 Lane 1, an `f32`.
3905
+ * @param a2 Lane 2, an `f32`.
3906
+ * @param a3 Lane 3, an `f32`.
3556
3907
  * @returns The vector.
3557
3908
  */
3558
3909
  export function f32x4(a0: f32, a1: f32, a2: f32, a3: f32): f32x4;
3559
3910
  /**
3560
3911
  * An `f64x2` of 2 lanes, one argument per lane, lane 0 first.
3561
3912
  *
3562
- * @param a0 Lane 0, an `f64`; `a1` is lane 1.
3563
- * @param a1 Lane 1.
3913
+ * @param a0 Lane 0, an `f64`.
3914
+ * @param a1 Lane 1, an `f64`.
3564
3915
  * @returns The vector.
3565
3916
  */
3566
3917
  export function f64x2(a0: f64, a1: f64): f64x2;
@@ -4009,6 +4360,7 @@ declare module "nish:simd" {
4009
4360
  export function shr<V extends IntegerVector>(v: V, count: i32): V;
4010
4361
  /**
4011
4362
  * Each lane rotated left by `count`, `llvm.fshl` with `v` as both halves.
4363
+ * Signed and unsigned lanes rotate alike.
4012
4364
  *
4013
4365
  * @param v A `u8x16`, `u16x8`, `i32x4` or `u32x4`.
4014
4366
  * @param count An integer literal below the lane width: 0 to 7, 15 or 31
@@ -4018,6 +4370,7 @@ declare module "nish:simd" {
4018
4370
  export function rotl<V extends IntegerVector>(v: V, count: i32): V;
4019
4371
  /**
4020
4372
  * Each lane rotated right by `count`, `llvm.fshr` with `v` as both halves.
4373
+ * Signed and unsigned lanes rotate alike.
4021
4374
  *
4022
4375
  * @param v A `u8x16`, `u16x8`, `i32x4` or `u32x4`.
4023
4376
  * @param count An integer literal below the lane width: 0 to 7, 15 or 31
@@ -4367,8 +4720,9 @@ declare module "nish:secret" {
4367
4720
  * copy.set(raw)`, then `secret(copy)`.
4368
4721
  *
4369
4722
  * @param value The key material, of a shape a `Secret` may hold (NL2428).
4370
- * @returns The `Secret`, which needs an owner: a `const` or `let`, a
4371
- * `return`, or `wipe` (NL2439 otherwise).
4723
+ * @returns The `Secret`, holding `value` itself and not a copy, so its
4724
+ * storage is the storage `wipe` later zeroes. It needs an owner: a
4725
+ * `const` or `let`, a `return`, or `wipe` (NL2439 otherwise).
4372
4726
  */
4373
4727
  export function secret<T>(value: T): Secret<T>;
4374
4728
  /**
@@ -4379,7 +4733,8 @@ declare module "nish:secret" {
4379
4733
  * (NL2449), and declares a return type that holds no `Secret` (NL2451; an
4380
4734
  * arrow writes it down, NL2452): that type is what leaves.
4381
4735
  *
4382
- * @param s The `Secret` to read, borrowed.
4736
+ * @param s The `Secret` to read, borrowed: it can be exposed again, and is
4737
+ * still to be wiped.
4383
4738
  * @param f The function the value is handed to.
4384
4739
  * @returns What `f` answers.
4385
4740
  */
@@ -4509,6 +4864,28 @@ declare module "nish:region" {
4509
4864
  * @returns The handle.
4510
4865
  */
4511
4866
  export function regionWith<A, T>(arg: A, f: (arg: A) => T): Region<T>;
4867
+ /**
4868
+ * A new region whose parent is the region entered now: `f()` runs with every
4869
+ * allocation landing in it, and what it answers is the root. The child may
4870
+ * keep memory the parent's root reaches, and `free` of the parent frees it
4871
+ * too. Called outside an `enter` or `enterWith` `f` it is NL2702, and it
4872
+ * stops the program when no region is entered.
4873
+ *
4874
+ * @param f Makes the root. It answers memory it allocated, never the
4875
+ * parent's (NL2614).
4876
+ * @returns The handle.
4877
+ */
4878
+ export function child<T>(f: () => T): Region<T>;
4879
+ /**
4880
+ * `child`, with one outside argument handed to `f`.
4881
+ *
4882
+ * @param arg Memory the parent's root reaches, which `f` may keep in the
4883
+ * child, or outer memory it may only read (NL2610).
4884
+ * @param f Makes the root from `arg`. It answers memory it allocated, never
4885
+ * `arg` itself (NL2614).
4886
+ * @returns The handle.
4887
+ */
4888
+ export function childWith<A, T>(arg: A, f: (arg: A) => T): Region<T>;
4512
4889
  /**
4513
4890
  * `f(root)`, with every allocation landing in the region. Stops the program
4514
4891
  * on a dead, foreign or already-entered handle.
@@ -4757,6 +5134,71 @@ declare module "nish:async" {
4757
5134
  * made (an `fd` that is not open). A regular file is always ready.
4758
5135
  */
4759
5136
  export function writable(fd: i32): Promise<boolean>;
5137
+ /**
5138
+ * Suspend the task until `fd` has bytes to read, or its peer has hung up,
5139
+ * or `ms` milliseconds pass, whichever comes first. Written only as
5140
+ * `await readableWithin(fd, ms)` inside an `async` function (NL2671).
5141
+ * Refused under `--emit-ts` (NL4018).
5142
+ *
5143
+ * @param fd An open descriptor: a socket, a pipe or a terminal.
5144
+ * @param ms The most milliseconds to wait; a negative `ms` waits without a
5145
+ * limit, as `readable` does.
5146
+ * @returns `true` once `fd` is ready; `false` when `ms` passed first, when
5147
+ * the wait could not be made, or when the task is cancelled.
5148
+ */
5149
+ export function readableWithin(fd: i32, ms: i32): Promise<boolean>;
5150
+ /**
5151
+ * Suspend the task until `fd` takes bytes without blocking, or `ms`
5152
+ * milliseconds pass, whichever comes first. Written only as
5153
+ * `await writableWithin(fd, ms)` inside an `async` function (NL2671).
5154
+ * Refused under `--emit-ts` (NL4018).
5155
+ *
5156
+ * @param fd An open descriptor: a socket, a pipe or a terminal.
5157
+ * @param ms The most milliseconds to wait; a negative `ms` waits without a
5158
+ * limit, as `writable` does.
5159
+ * @returns `true` once `fd` is ready; `false` when `ms` passed first, when
5160
+ * the wait could not be made, or when the task is cancelled.
5161
+ */
5162
+ export function writableWithin(fd: i32, ms: i32): Promise<boolean>;
5163
+ /**
5164
+ * The running task's handle, to hand to `cancel` or `cancelAfter`. `main`'s
5165
+ * handle is 0, which no call cancels. Refused under `--emit-ts` (NL4018).
5166
+ *
5167
+ * @returns The handle, an `i64` that names this task and no later one.
5168
+ */
5169
+ export function taskId(): i64;
5170
+ /**
5171
+ * Cancel a task: every `sleep`, `readable` or `writable` it is waiting on
5172
+ * answers `false` at once, and so does every one it makes later, so it runs
5173
+ * on to its end, its `using` bindings dispose and its region is freed. A
5174
+ * task is never stopped in the middle of its code. Refused under
5175
+ * `--emit-ts` (NL4018).
5176
+ *
5177
+ * @param task A handle `taskId()` answered.
5178
+ * @returns `true` while the task is still running, so it is now cancelled
5179
+ * (a second `cancel` of it answers `true` too); `false` for a task that
5180
+ * has finished, or for `main`.
5181
+ */
5182
+ export function cancel(task: i64): boolean;
5183
+ /**
5184
+ * Whether the running task has been cancelled. Refused under `--emit-ts`
5185
+ * (NL4018).
5186
+ *
5187
+ * @returns `true` once `cancel` was called on it, or its `cancelAfter`
5188
+ * deadline passed.
5189
+ */
5190
+ export function cancelled(): boolean;
5191
+ /**
5192
+ * Cancel a task `ms` milliseconds from now, unless it finishes first. A
5193
+ * later call replaces the deadline; a negative `ms` removes it. Refused
5194
+ * under `--emit-ts` (NL4018).
5195
+ *
5196
+ * @param task A handle `taskId()` answered.
5197
+ * @param ms The milliseconds until the task is cancelled.
5198
+ * @returns `true` when the task is still running and the deadline is set
5199
+ * (or removed); `false` for a task that has finished or is `main`.
5200
+ */
5201
+ export function cancelAfter(task: i64, ms: i32): boolean;
4760
5202
  }
4761
5203
 
4762
5204
  // ---- Arena (docs/LANGUAGE.md -> Arena) ---------------------------------------
@@ -5150,7 +5592,7 @@ interface Math {
5150
5592
  * there. A numeric literal argument to `push`, `indexOf` or `fill` takes the
5151
5593
  * element type, so `wide.push(3)` on an `i64[]` pushes an `i64` three. An
5152
5594
  * unknown method is refused with the supported list: "Unknown method `shift` on
5153
- * i32[]"; there is no `map`, `shift`, `slice` or `toString` on an array.
5595
+ * i32[]"; there is no `shift`, `find` or `toString` on an array.
5154
5596
  */
5155
5597
  interface Array<T> {
5156
5598
  /** The element count, read-only: assigning to it is refused. */
@@ -5304,6 +5746,72 @@ interface Array<T> {
5304
5746
  * @param offset the first slot written, `0` by default.
5305
5747
  */
5306
5748
  set(source: readonly T[], offset?: number): void;
5749
+ /**
5750
+ * A new array of `callbackfn(element)` for each element, in order, as
5751
+ * `Array.prototype.map` gives. The callback takes the element, or the
5752
+ * element and its index; a third parameter (JavaScript's array argument) is
5753
+ * refused (NL2698), as is a second argument to `map` itself. It is a
5754
+ * top-level function or an arrow reading only `const` locals and parameters,
5755
+ * and the call compiles to a loop that calls it directly (`std/array`'s
5756
+ * `map`). Capability: none.
5757
+ *
5758
+ * @param callbackfn called once per element, with the element and its index.
5759
+ * @returns a new array of the callback's answers, of the receiver's length.
5760
+ */
5761
+ map<U>(callbackfn: (value: T, index: number) => U): U[];
5762
+ /**
5763
+ * A new array of the elements for which `predicate` answers `true`, in
5764
+ * order, as `Array.prototype.filter` gives. The predicate answers a
5765
+ * `boolean` and takes the element, or the element and its index, under the
5766
+ * callback rules of `map`. Capability: none.
5767
+ *
5768
+ * @param predicate called once per element, with the element and its index.
5769
+ * @returns a new array of the elements kept.
5770
+ */
5771
+ filter(predicate: (value: T, index: number) => boolean): T[];
5772
+ /**
5773
+ * Whether `predicate` answers `true` for some element, stopping at the first
5774
+ * that does, as `Array.prototype.some` does; an empty array answers `false`.
5775
+ * The callback rules are `map`'s. Capability: none.
5776
+ *
5777
+ * @param predicate called in order, with the element and its index, until it answers `true`.
5778
+ * @returns `true` when some element satisfies `predicate`.
5779
+ */
5780
+ some(predicate: (value: T, index: number) => boolean): boolean;
5781
+ /**
5782
+ * Whether `predicate` answers `true` for every element, stopping at the
5783
+ * first that does not, as `Array.prototype.every` does; an empty array
5784
+ * answers `true`. The callback rules are `map`'s. Capability: none.
5785
+ *
5786
+ * @param predicate called in order, with the element and its index, until it answers `false`.
5787
+ * @returns `true` when no element fails `predicate`.
5788
+ */
5789
+ every(predicate: (value: T, index: number) => boolean): boolean;
5790
+ /**
5791
+ * The index of the first element for which `predicate` answers `true`, or
5792
+ * `-1`, as `Array.prototype.findIndex` gives. The callback rules are
5793
+ * `map`'s. There is no `find`: TypeScript types its answer `T | undefined`
5794
+ * and Nish has no `undefined`, so `xs.find(...)` is refused (NL2697) and
5795
+ * points here. Capability: none.
5796
+ *
5797
+ * @param predicate called in order, with the element and its index, until it answers `true`.
5798
+ * @returns the first such index, or `-1`.
5799
+ */
5800
+ findIndex(predicate: (value: T, index: number) => boolean): number;
5801
+ /**
5802
+ * A copy of the elements from `start` up to `end`, in a new array, with
5803
+ * string `slice`'s rule: the range must satisfy `0 <= start <= end <=
5804
+ * length`, and anything else panics with `slice out of range: [start, end)
5805
+ * of length n` and exits 1, where JavaScript counts a negative index back
5806
+ * from the length and clamps the rest. `start` is `0` and `end` the length
5807
+ * by default; a third argument is refused (NL2699). Under Node the prelude's
5808
+ * `sliceArray` applies the same check. Capability: none.
5809
+ *
5810
+ * @param start the first index copied, `0` by default.
5811
+ * @param end the index after the last copied, the length by default.
5812
+ * @returns a new array of `end - start` elements.
5813
+ */
5814
+ slice(start?: number, end?: number): T[];
5307
5815
  }
5308
5816
 
5309
5817
  /**
@@ -5314,8 +5822,9 @@ interface Array<T> {
5314
5822
  * nish_array *`.
5315
5823
  *
5316
5824
  * Everything that reads works (`length`, `a[i]`, `for...of`, `indexOf`, `join`,
5317
- * passing it on to another `readonly T[]`); every write is refused under its
5318
- * own name, an element store ("Cannot assign to an element of readonly i32[]
5825
+ * `map`, `filter`, `some`, `every`, `findIndex`, `slice`, passing it on to
5826
+ * another `readonly T[]`); every write is refused under its own name, an
5827
+ * element store ("Cannot assign to an element of readonly i32[]
5319
5828
  * (declare it i32[] to write through it)"), `push`, `pop`, `set`, `fill` and
5320
5829
  * `sort` included. A `T[]` widens into a `readonly T[]`, and never back:
5321
5830
  * "Argument 1 of `fill`: expected i32[], got readonly i32[]". It is shallow, as
@@ -5347,6 +5856,72 @@ interface ReadonlyArray<T> {
5347
5856
  * @returns the joined string.
5348
5857
  */
5349
5858
  join(separator?: string): string;
5859
+ /**
5860
+ * A new array of `callbackfn(element)` for each element, in order, as
5861
+ * `Array.prototype.map` gives. The callback takes the element, or the
5862
+ * element and its index; a third parameter (JavaScript's array argument) is
5863
+ * refused (NL2698), as is a second argument to `map` itself. It is a
5864
+ * top-level function or an arrow reading only `const` locals and parameters,
5865
+ * and the call compiles to a loop that calls it directly (`std/array`'s
5866
+ * `map`). Capability: none.
5867
+ *
5868
+ * @param callbackfn called once per element, with the element and its index.
5869
+ * @returns a new array of the callback's answers, of the receiver's length.
5870
+ */
5871
+ map<U>(callbackfn: (value: T, index: number) => U): U[];
5872
+ /**
5873
+ * A new array of the elements for which `predicate` answers `true`, in
5874
+ * order, as `Array.prototype.filter` gives. The predicate answers a
5875
+ * `boolean` and takes the element, or the element and its index, under the
5876
+ * callback rules of `map`. Capability: none.
5877
+ *
5878
+ * @param predicate called once per element, with the element and its index.
5879
+ * @returns a new array of the elements kept.
5880
+ */
5881
+ filter(predicate: (value: T, index: number) => boolean): T[];
5882
+ /**
5883
+ * Whether `predicate` answers `true` for some element, stopping at the first
5884
+ * that does, as `Array.prototype.some` does; an empty array answers `false`.
5885
+ * The callback rules are `map`'s. Capability: none.
5886
+ *
5887
+ * @param predicate called in order, with the element and its index, until it answers `true`.
5888
+ * @returns `true` when some element satisfies `predicate`.
5889
+ */
5890
+ some(predicate: (value: T, index: number) => boolean): boolean;
5891
+ /**
5892
+ * Whether `predicate` answers `true` for every element, stopping at the
5893
+ * first that does not, as `Array.prototype.every` does; an empty array
5894
+ * answers `true`. The callback rules are `map`'s. Capability: none.
5895
+ *
5896
+ * @param predicate called in order, with the element and its index, until it answers `false`.
5897
+ * @returns `true` when no element fails `predicate`.
5898
+ */
5899
+ every(predicate: (value: T, index: number) => boolean): boolean;
5900
+ /**
5901
+ * The index of the first element for which `predicate` answers `true`, or
5902
+ * `-1`, as `Array.prototype.findIndex` gives. The callback rules are
5903
+ * `map`'s. There is no `find`: TypeScript types its answer `T | undefined`
5904
+ * and Nish has no `undefined`, so `xs.find(...)` is refused (NL2697) and
5905
+ * points here. Capability: none.
5906
+ *
5907
+ * @param predicate called in order, with the element and its index, until it answers `true`.
5908
+ * @returns the first such index, or `-1`.
5909
+ */
5910
+ findIndex(predicate: (value: T, index: number) => boolean): number;
5911
+ /**
5912
+ * A copy of the elements from `start` up to `end`, in a new array, with
5913
+ * string `slice`'s rule: the range must satisfy `0 <= start <= end <=
5914
+ * length`, and anything else panics with `slice out of range: [start, end)
5915
+ * of length n` and exits 1, where JavaScript counts a negative index back
5916
+ * from the length and clamps the rest. `start` is `0` and `end` the length
5917
+ * by default; a third argument is refused (NL2699). Under Node the prelude's
5918
+ * `sliceArray` applies the same check. Capability: none.
5919
+ *
5920
+ * @param start the first index copied, `0` by default.
5921
+ * @param end the index after the last copied, the length by default.
5922
+ * @returns a new array of `end - start` elements.
5923
+ */
5924
+ slice(start?: number, end?: number): T[];
5350
5925
  }
5351
5926
 
5352
5927
  /**
@@ -5444,6 +6019,10 @@ interface String {
5444
6019
  endsWith(searchString: string, endPosition?: number): boolean;
5445
6020
  }
5446
6021
 
6022
+ /**
6023
+ * `String.fromCharCode`, `lib.es2022`'s own member of `StringConstructor`,
6024
+ * repeated so that its byte meaning in Nish is documented.
6025
+ */
5447
6026
  interface StringConstructor {
5448
6027
  /**
5449
6028
  * The one-byte string of `c & 0xFF`, the inverse of `charCodeAt`. One