@amritk/nish-x86_64-linux 0.36.0 → 0.38.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 (103) hide show
  1. package/INSTALL.md +92 -71
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +75 -14
  4. package/package.json +1 -1
  5. package/runtime/async.d.mts +2 -0
  6. package/runtime/async.mjs +7 -0
  7. package/runtime/core/arrays.d.ts +4 -0
  8. package/runtime/core/arrays.mjs +33 -0
  9. package/runtime/nish.d.ts +1045 -512
  10. package/runtime/nish.h +53 -6
  11. package/runtime/page.d.ts +53 -0
  12. package/runtime/page.mjs +146 -0
  13. package/runtime/runtime-async.c +740 -0
  14. package/runtime/runtime-cross.c +2 -1
  15. package/runtime/runtime-region.c +85 -7
  16. package/runtime/runtime-text.c +1068 -0
  17. package/runtime/runtime-wasm.c +65 -9
  18. package/runtime/runtime.c +50 -961
  19. package/runtime/vite.mjs +48 -11
  20. package/scripts/build.sh +231 -31
  21. package/std/LICENSE-fdlibm +8 -0
  22. package/std/README.md +109 -82
  23. package/std/array.ts +266 -0
  24. package/std/bytes.ts +23 -19
  25. package/std/compress/checksum.ts +1 -1
  26. package/std/crypto/aes.ts +48 -43
  27. package/std/crypto/base64url.ts +7 -5
  28. package/std/crypto/bcrypt-batch.ts +13 -13
  29. package/std/crypto/bcrypt.ts +33 -27
  30. package/std/crypto/chacha20poly1305.ts +16 -14
  31. package/std/crypto/ed25519.ts +19 -16
  32. package/std/crypto/hmac.ts +7 -6
  33. package/std/crypto/jwt.ts +23 -20
  34. package/std/crypto/p256.ts +63 -56
  35. package/std/crypto/p384.ts +23 -20
  36. package/std/crypto/pki.ts +18 -15
  37. package/std/crypto/rsa.ts +23 -19
  38. package/std/crypto/x25519.ts +11 -10
  39. package/std/crypto/x509.ts +69 -62
  40. package/std/host/ring.ts +124 -0
  41. package/std/host/text.ts +86 -0
  42. package/std/json-equal.ts +12 -11
  43. package/std/json-number.ts +12 -12
  44. package/std/json-tokenizer.ts +26 -22
  45. package/std/json-typed.ts +92 -78
  46. package/std/json-value.ts +38 -33
  47. package/std/json.ts +32 -28
  48. package/std/math.ts +743 -41
  49. package/std/net/dns-resolver.ts +46 -6
  50. package/std/net/dns.ts +50 -13
  51. package/std/net/fetch.ts +1744 -295
  52. package/std/net/handler.ts +30 -0
  53. package/std/net/hpack.ts +29 -26
  54. package/std/net/http-compress.ts +42 -28
  55. package/std/net/http-fields.ts +19 -4
  56. package/std/net/http1-client-codec.ts +29 -25
  57. package/std/net/http1-codec.ts +62 -91
  58. package/std/net/http1-server.ts +79 -57
  59. package/std/net/http2-frame.ts +17 -16
  60. package/std/net/http2-tls.ts +17 -15
  61. package/std/net/http2.ts +67 -54
  62. package/std/net/http3-frame.ts +11 -10
  63. package/std/net/http3-server.ts +36 -35
  64. package/std/net/http3.ts +70 -58
  65. package/std/net/postgres-async.ts +1005 -0
  66. package/std/net/postgres.ts +185 -164
  67. package/std/net/prefork.ts +38 -34
  68. package/std/net/qpack.ts +15 -14
  69. package/std/net/quic-conn-ack.ts +1 -0
  70. package/std/net/quic-conn-cid.ts +11 -0
  71. package/std/net/quic-conn-params.ts +41 -14
  72. package/std/net/quic-datagram.ts +7 -0
  73. package/std/net/quic-frame.ts +48 -12
  74. package/std/net/quic-listener.ts +61 -44
  75. package/std/net/quic-packet.ts +71 -52
  76. package/std/net/quic-recovery.ts +59 -21
  77. package/std/net/quic-stream.ts +83 -55
  78. package/std/net/quic.ts +204 -152
  79. package/std/net/serve.ts +763 -0
  80. package/std/net/tls/client.ts +1 -0
  81. package/std/net/tls/codec.ts +81 -12
  82. package/std/net/tls/record-client.ts +30 -18
  83. package/std/net/tls/record-server.ts +74 -46
  84. package/std/net/tls/record.ts +33 -24
  85. package/std/net/tls/ticket.ts +2 -0
  86. package/std/net/tls/verify.ts +8 -7
  87. package/std/net/tls-tcp.ts +58 -50
  88. package/std/net/tls.ts +55 -43
  89. package/std/net/url.ts +12 -9
  90. package/std/net/websocket.ts +23 -19
  91. package/std/net/webtransport.ts +24 -18
  92. package/std/random.ts +8 -6
  93. package/std/regex.ts +70 -59
  94. package/std/sync/ring.ts +24 -21
  95. package/std/sync/shm.ts +9 -7
  96. package/std/term-io.ts +17 -14
  97. package/std/term-probe.ts +11 -10
  98. package/std/term-rate.ts +71 -61
  99. package/std/term.ts +73 -59
  100. package/std/text.ts +242 -15
  101. package/std/threads.ts +19 -15
  102. package/std/yaml-json.ts +17 -15
  103. package/std/yaml-write.ts +21 -14
package/runtime/nish.d.ts CHANGED
@@ -38,8 +38,9 @@
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`).
41
- * `nish/map`'s `reserve` and `getOrInsert` are not declared here: they are
42
- * ordinary source, `std/map.ts`, which `tsconfig.json` maps `nish/*` to.
41
+ *
42
+ * `nish/map`'s `reserve` and `getOrInsert` are not declared here: they are
43
+ * ordinary source, `std/map.ts`, which `tsconfig.json` maps `nish/*` to.
43
44
  */
44
45
 
45
46
  // ---- Numeric widths (docs/LANGUAGE.md -> Types) ------------------------------
@@ -131,9 +132,19 @@ type integer<Lo extends number, Hi extends number> = number;
131
132
  // `nish`.
132
133
 
133
134
  /** The success arm of a `Result`, as the discriminant proves it. */
134
- type ResultOk<T> = { readonly ok: true; readonly value: T };
135
+ type ResultOk<T> = {
136
+ /** `true` on this arm: the discriminant `if (r.ok)` tests. */
137
+ readonly ok: true;
138
+ /** The success payload, readable only where `r.ok` or `r.isOk()` holds. */
139
+ readonly value: T;
140
+ };
135
141
  /** The failure arm. */
136
- type ResultErr<E> = { readonly ok: false; readonly error: E };
142
+ type ResultErr<E> = {
143
+ /** `false` on this arm: where `if (r.ok)` fails, `r` is this arm. */
144
+ readonly ok: false;
145
+ /** The failure payload, readable only where `!r.ok` or `r.isErr()` holds. */
146
+ readonly error: E;
147
+ };
137
148
 
138
149
  /**
139
150
  * The methods every `Result` carries. `r.ok` is the same bit as a plain
@@ -209,7 +220,11 @@ type Result<T, E> = (ResultOk<T> | ResultErr<E>) & ResultMethods<T, E>;
209
220
  * @returns a `Result` on its success arm.
210
221
  */
211
222
  declare function Ok<T, E>(value: T): Result<T, E>;
212
- /** The success of a `Result<void, E>`, with no payload. Capability: none. */
223
+ /**
224
+ * The success of a `Result<void, E>`, with no payload. Capability: none.
225
+ *
226
+ * @returns a `Result<void, E>` on its success arm.
227
+ */
213
228
  declare function Ok<E>(): Result<void, E>;
214
229
  /**
215
230
  * The failure value, likewise: its `Result` type comes from the context.
@@ -227,15 +242,18 @@ declare function Err<T, E>(error: E): Result<T, E>;
227
242
  * `nish/threads`'s scope, a `Mutex`'s lock, `arena()` or a handle class needs
228
243
  * no `"ESNext.Disposable"` in its `lib`. `nish` takes `using` only for a
229
244
  * `scope()`, a `lock()`, an `arena()` or a `new` of a handle class, and any
230
- * other `using` initialiser is refused. A handle class is a class with exactly
231
- * one `readonly` `CPtr` or `CPtr | null` field: it owns the C resource the
232
- * pointer names, and its `[Symbol.dispose](): void` is `close(this.file);` or
233
- * `const p = this.file; if (p !== null) { close(p); }` for a `declare function`
234
- * `close` of one parameter. `using` of a handle class closes it at every exit
235
- * of its block, once, in the reverse of the order the declarators opened; a
236
- * panic and `process.exit` do not close. `--deny ffi` refuses that `using`.
237
- * Any other class that declares `[Symbol.dispose]` is refused. The compiler
238
- * emits the join, the release, the unlock or the close itself, and
245
+ * other `using` initialiser is refused.
246
+ *
247
+ * A handle class is a class with exactly one `readonly` `CPtr` or `CPtr | null`
248
+ * field: it owns the C resource the pointer names, and its `[Symbol.dispose]():
249
+ * void` is `close(this.file);` or `const p = this.file; if (p !== null) {
250
+ * close(p); }` for a `declare function` `close` of one parameter. `using` of a
251
+ * handle class closes it at every exit of its block, once, in the reverse of
252
+ * the order the declarators opened; a panic and `process.exit` do not close.
253
+ * `--deny ffi` refuses that `using`. Any other class that declares
254
+ * `[Symbol.dispose]` is refused.
255
+ *
256
+ * The compiler emits the join, the release, the unlock or the close itself, and
239
257
  * `[Symbol.dispose]` is never called by name.
240
258
  */
241
259
  interface SymbolConstructor {
@@ -263,16 +281,18 @@ interface StringConstructor {
263
281
  /**
264
282
  * The bytes of `bytes[start..end)` as one string, with one range check, one
265
283
  * allocation and one copy. The range is `s.slice`'s, `0 <= start <= end <=
266
- * bytes.length`, checked once after all three arguments are evaluated; anything
267
- * else panics with `slice out of range: [start, end) of length len` and exits 1.
268
- * `bytes` is a `u8[]` reached any way an array is, `readonly` or not; a wider
269
- * element or a `u8[] | null` is refused (NL2268). Both ends are required (NL2060)
270
- * and are numbers (NL2058). Nothing validates the bytes as UTF-8: a multi-byte
271
- * character copied whole is that character, and a range that cuts one is bytes
272
- * that are not a valid string. Natively the string's bytes are the array's, so
273
- * `write` or `writeFileSync` sends any bytes back out unchanged; bytes that are
274
- * not text go out through `writeBytes` and `writeFileBytesSync`, which agree on
275
- * every host. Capability: none.
284
+ * bytes.length`, checked once after all three arguments are evaluated;
285
+ * anything else panics with `slice out of range: [start, end) of length len`
286
+ * and exits 1. `bytes` is a `u8[]` reached any way an array is, `readonly` or
287
+ * not; a wider element or a `u8[] | null` is refused (NL2268). Both ends are
288
+ * required (NL2060) and are numbers (NL2058).
289
+ *
290
+ * Nothing validates the bytes as UTF-8: a multi-byte character copied whole
291
+ * is that character, and a range that cuts one is bytes that are not a valid
292
+ * string. Natively the string's bytes are the array's, so `write` or
293
+ * `writeFileSync` sends any bytes back out unchanged; bytes that are not text
294
+ * go out through `writeBytes` and `writeFileBytesSync`, which agree on every
295
+ * host. Capability: none.
276
296
  *
277
297
  * @param bytes the buffer to read.
278
298
  * @param start the first byte taken.
@@ -362,11 +382,81 @@ interface Process {
362
382
  declare var process: Process;
363
383
 
364
384
  // ---- Numeric conversions (there is no cast in Nish) ----------------------
365
- // `Number`, `parseInt` and `parseFloat` are `lib.es2022`'s and are not
366
- // declared here; docs/LANGUAGE.md -> Numeric conversions has their rules.
367
385
  // Nothing converts implicitly: a mixed operation, an initializer of another
368
386
  // numeric type and a call with the wrong numeric type are each refused until
369
- // 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;
370
460
 
371
461
  /**
372
462
  * `x` as an `i32`. An integer of 32 bits or more wraps to its low 32 bits, a
@@ -375,12 +465,13 @@ declare var process: Process;
375
465
  * integer of the same width and another signedness keeps its bits. The float
376
466
  * rule is Rust's `as` cast, not JavaScript's modulo-2^32 `ToInt32`:
377
467
  * `toI32(5e10)` is `2147483647`, while the integer rule gives
378
- * `toI32(5000000000)` as `705032704`. The result of a `u8`, a `u16` or a ranged
379
- * integer lies in the declared range of `x`, so
380
- * `const r: integer<0, 255> = toI32(b)` costs no check for a `u8` `b`; under
381
- * `--number-mode f64` this is how a `number` enters an `integer<Lo, Hi>`. An
382
- * argument that is not a number is refused, as `` `toI32` expects a number
383
- * (i32, i64, or f64), got string ``. Capability: none.
468
+ * `toI32(5000000000)` as `705032704`.
469
+ *
470
+ * The result of a `u8`, a `u16` or a ranged integer lies in the declared range
471
+ * of `x`, so `const r: integer<0, 255> = toI32(b)` costs no check for a `u8`
472
+ * `b`; under `--number-mode f64` this is how a `number` enters an `integer<Lo,
473
+ * Hi>`. An argument that is not a number is refused, as `` `toI32` expects a
474
+ * number (i32, i64, or f64), got string ``. Capability: none.
384
475
  *
385
476
  * @param x the value to convert.
386
477
  * @returns `x` in 32 signed bits.
@@ -492,22 +583,25 @@ declare function f32ToBits(x: f32): i32;
492
583
  * never turned into a branch: `a` for an all-ones mask, `b` for zero. Every
493
584
  * operand is one type, `u32` or `u64`. Both are `number` here, so one generic
494
585
  * declaration stands for the two. Any other type is refused, as `` `ctEq` takes
495
- * u32 or u64 operands (convert with toU32 or toU64), got i64 `` (NL2399): `i32`,
496
- * `i64`, `u8`, `u16`, a ranged integer, `f32`, `f64` and `boolean` included, and
497
- * a boolean mask in particular. Operands of two types are `` `ctSelect` needs
498
- * every operand of one type, got u32 and u64 `` (NL2400). A bare numeric
499
- * literal takes its type from the first operand that is not one, wherever it
500
- * stands. The call takes exactly three arguments, and like every function it is
501
- * not a value. The all-ones-or-zero convention for the mask is not checked,
502
- * because the check would be a branch; any other mask is a defined bitwise
503
- * blend, so `ctSelect(0xf0f0f0f0, a, b)` takes the high nibble of each byte
504
- * from `a`. The promise is no branch and no secret-indexed load in the
505
- * primitive itself and nothing about the caller's own code, which branches on a
506
- * secret if it writes `if (ctEq(a, b) !== 0)`. On `wasm32` the same barrier is
507
- * emitted, but the engine compiles the module again, so nothing is promised
508
- * there. Under Node the runtime shim computes the same answer as a pure
509
- * function (over `u32`, put back in range with `>>> 0`; over `u64`, on BigInts),
510
- * and only the timing is not claimed. Capability: none.
586
+ * u32 or u64 operands (convert with toU32 or toU64), got i64 `` (NL2399):
587
+ * `i32`, `i64`, `u8`, `u16`, a ranged integer, `f32`, `f64` and `boolean`
588
+ * included, and a boolean mask in particular.
589
+ *
590
+ * Operands of two types are `` `ctSelect` needs every operand of one type, got
591
+ * u32 and u64 `` (NL2400). A bare numeric literal takes its type from the first
592
+ * operand that is not one, wherever it stands. The call takes exactly three
593
+ * arguments, and like every function it is not a value. The all-ones-or-zero
594
+ * convention for the mask is not checked, because the check would be a branch;
595
+ * any other mask is a defined bitwise blend, so `ctSelect(0xf0f0f0f0, a, b)`
596
+ * takes the high nibble of each byte from `a`.
597
+ *
598
+ * The promise is no branch and no secret-indexed load in the primitive itself
599
+ * and nothing about the caller's own code, which branches on a secret if it
600
+ * writes `if (ctEq(a, b) !== 0)`. On `wasm32` the same barrier is emitted, but
601
+ * the engine compiles the module again, so nothing is promised there. Under
602
+ * Node the runtime shim computes the same answer as a pure function (over
603
+ * `u32`, put back in range with `>>> 0`; over `u64`, on BigInts), and only the
604
+ * timing is not claimed. Capability: none.
511
605
  *
512
606
  * @param mask all ones to choose `a`, zero to choose `b`.
513
607
  * @param a the value an all-ones mask selects.
@@ -521,11 +615,13 @@ declare function ctSelect<T extends u32 | u64>(mask: T, a: T, b: T): T;
521
615
  * takes, so `ctSelect(ctEq(a, b), x, y)` never shows the optimiser a boolean.
522
616
  * Both operands are one type, `u32` or `u64`; any other type, a boolean and a
523
617
  * ranged integer included, is refused with NL2399, and operands of two types
524
- * with NL2400, as for `ctSelect`. A bare numeric literal takes its type from
525
- * the other operand, so `ctEq(0, x)` works, while `ctEq(1, 2)`, with no width to
526
- * take, is an `i32` and refused. The call takes exactly two arguments, and like
527
- * every function it is not a value. The no-branch promise and its limits are
528
- * `ctSelect`'s, and so are the `wasm32` and Node readings. Capability: none.
618
+ * with NL2400, as for `ctSelect`.
619
+ *
620
+ * A bare numeric literal takes its type from the other operand, so `ctEq(0, x)`
621
+ * works, while `ctEq(1, 2)`, with no width to take, is an `i32` and refused.
622
+ * The call takes exactly two arguments, and like every function it is not a
623
+ * value. The no-branch promise and its limits are `ctSelect`'s, and so are the
624
+ * `wasm32` and Node readings. Capability: none.
529
625
  *
530
626
  * @param a one operand.
531
627
  * @param b the other, of the same type.
@@ -536,42 +632,49 @@ declare function ctEq<T extends u32 | u64>(a: T, b: T): T;
536
632
  * Every byte of `bytes` set to zero, in stores neither `-O2` nor `-flto` may
537
633
  * drop as dead (#385): how a key, a secret scalar or another secret is cleared
538
634
  * once it is used. A statement: used as a value it is `` `secureZero` returns
539
- * void and can only be used as a statement `` (NL2105), and it takes exactly one
540
- * argument (NL2061). It fills a `u8[]` only; anything else is `` `secureZero`
541
- * fills a `u8[]`, got i32[] `` (NL2402), a string included, which is immutable.
635
+ * void and can only be used as a statement `` (NL2105), and it takes exactly
636
+ * one argument (NL2061). It fills a `u8[]` only; anything else is ``
637
+ * `secureZero` fills a `u8[]`, got i32[] `` (NL2402), a string included, which
638
+ * is immutable.
639
+ *
542
640
  * The call writes its argument, so a `readonly u8[]` is `` Cannot `secureZero`
543
- * through readonly u8[] (declare it u8[] to write through it) `` (NL2264), and a
544
- * function that wipes a parameter does not get `readonly` on it; in a scope's
641
+ * through readonly u8[] (declare it u8[] to write through it) `` (NL2264), and
642
+ * a function that wipes a parameter does not get `readonly` on it; in a scope's
545
643
  * region it is refused where `fill` is (NL2394). An empty array is a call that
546
- * clears nothing. It clears the bytes of the array it is handed and nothing
547
- * else: a copy taken before (`slice`, a `push` into another array, an element
548
- * read into a local, a register spilled to the stack) is the program's to
549
- * clear, and nothing clears what the operating system swapped out. It works
550
- * under the wasi profile; a freestanding wasm module has no wipe. Under Node it
551
- * is a loop that sets every element to zero, and a collector that moved the
552
- * array may have left the old bytes behind. Capability: none.
644
+ * clears nothing.
645
+ *
646
+ * It clears the bytes of the array it is handed and nothing else: a copy taken
647
+ * before (`slice`, a `push` into another array, an element read into a local, a
648
+ * register spilled to the stack) is the program's to clear, and nothing clears
649
+ * what the operating system swapped out. It works under the wasi profile; a
650
+ * freestanding wasm module has no wipe. Under Node it is a loop that sets every
651
+ * element to zero, and a collector that moved the array may have left the old
652
+ * bytes behind. Capability: none.
553
653
  *
554
654
  * @param bytes the buffer to clear.
555
655
  */
556
656
  declare function secureZero(bytes: u8[]): void;
557
657
  /**
558
658
  * One `lstat` of `path`, so a symbolic link answers for itself and not for what
559
- * it names: the owner's uid in the high 32 bits and `st_mode` (the file type and
560
- * the permission bits) in the low 32, or -1 when the path does not resolve or
561
- * holds a NUL (#386). Both come out of one `lstat`, so they describe the same
562
- * file however the path changes in between. Take them apart with
563
- * `(om >> 32) & 0xffffffff` (the owner) and `om & 0xfff` (the permission bits)
564
- * or `om & 0xf000` (the type: `0x8000` a regular file, `0x4000` a directory,
565
- * `0xa000` a symbolic link). The mask on the owner is not optional: a uid of
566
- * 2^31 or more fills bit 63, which `>>` on an `i64` copies down, so `om >> 32`
567
- * alone answers a negative owner that never equals `geteuid()`. The answer is
568
- * an `i64` in both number modes, and an `i32` variable is refused rather than
569
- * truncated (`` Cannot initialize i32 variable `om` with i64 ``). It takes a
570
- * `string` path and exactly one argument. A wasm32 build is refused, with
571
- * `` `lstatOwnerModeSync` reaches the operating system, and a wasm32 build has
572
- * none to reach ``. Under Node the runtime shim packs `fs.lstatSync`'s `uid` and
573
- * `mode` the same way as a BigInt. It is a global, not a `nish:` export.
574
- * Capability: fs.read.
659
+ * it names: the owner's uid in the high 32 bits and `st_mode` (the file type
660
+ * and the permission bits) in the low 32, or -1 when the path does not resolve
661
+ * or holds a NUL (#386). Both come out of one `lstat`, so they describe the
662
+ * same file however the path changes in between.
663
+ *
664
+ * Take them apart with `(om >> 32) & 0xffffffff` (the owner) and `om & 0xfff`
665
+ * (the permission bits) or `om & 0xf000` (the type: `0x8000` a regular file,
666
+ * `0x4000` a directory, `0xa000` a symbolic link). The mask on the owner is not
667
+ * optional: a uid of 2^31 or more fills bit 63, which `>>` on an `i64` copies
668
+ * down, so `om >> 32` alone answers a negative owner that never equals
669
+ * `geteuid()`.
670
+ *
671
+ * The answer is an `i64` in both number modes, and an `i32` variable is refused
672
+ * rather than truncated (`` Cannot initialize i32 variable `om` with i64 ``).
673
+ * It takes a `string` path and exactly one argument. A wasm32 build is refused,
674
+ * with `` `lstatOwnerModeSync` reaches the operating system, and a wasm32 build
675
+ * has none to reach ``. Under Node the runtime shim packs `fs.lstatSync`'s
676
+ * `uid` and `mode` the same way as a BigInt. It is a global, not a `nish:`
677
+ * export. Capability: fs.read.
575
678
  *
576
679
  * @param path the path to stat, not followed through a final link.
577
680
  * @returns `(uid << 32) | st_mode`, or -1.
@@ -615,14 +718,16 @@ declare function isExecutableSync(path: string): boolean;
615
718
  /**
616
719
  * `xs[i]` with no bounds check: undefined behaviour when `i` is out of range,
617
720
  * which is the whole of the trade. The array holds numbers (`i32`, `i64`, `u8`,
618
- * `u16`, `u32`, `u64`, `f32` or `f64`) and nothing else, or it is `` `uncheckedGet`
619
- * takes an array of numbers (i32, i64, u8, u16, u32, u64, f32 or f64), got
620
- * string[] `` (NL2455). The index is an `i32`, so an `f64` one is `` `uncheckedGet`
621
- * expects an argument of type i32, got f64 ``. A `readonly` array may be read.
721
+ * `u16`, `u32`, `u64`, `f32` or `f64`) and nothing else, or it is ``
722
+ * `uncheckedGet` takes an array of numbers (i32, i64, u8, u16, u32, u64, f32 or
723
+ * f64), got string[] `` (NL2455). The index is an `i32`, so an `f64` one is ``
724
+ * `uncheckedGet` expects an argument of type i32, got f64 ``. A `readonly`
725
+ * array may be read.
726
+ *
622
727
  * To the bounds proof it is the load `xs[i]` is, less the check: it moves no
623
728
  * length, and it records no passed check, so a checked `xs[i]` after it keeps
624
- * its check. Under `--deny-panics` it is an allowed `unchecked` site. Under Node
625
- * the runtime shim reads `xs[i]`. Capability: unsafe.
729
+ * its check. Under `--deny-panics` it is an allowed `unchecked` site. Under
730
+ * Node the runtime shim reads `xs[i]`. Capability: unsafe.
626
731
  *
627
732
  * @param xs the array of numbers to read.
628
733
  * @param i the index, trusted to be in range.
@@ -636,12 +741,13 @@ declare function uncheckedGet<T extends number>(xs: readonly T[], i: i32): T;
636
741
  * array may not be written (`` Cannot `uncheckedSet` through readonly u8[] ``).
637
742
  * A number is copied in and held by nothing, so the store needs none of the
638
743
  * escape and inline-array reasoning `xs[i] = v` carries; an array field handed
639
- * to it is a value and is not stored inline. The store is a write of the array
640
- * for every rule that asks, the rule for a parallel body included. To the bounds
641
- * proof it is the store `xs[i] = v` is, less the check, and a resize in the
642
- * value stored still takes the length away. Under `--deny-panics` it is an
643
- * allowed `unchecked` site. Under Node the runtime shim stores `xs[i] = v`.
644
- * Capability: unsafe.
744
+ * to it is a value and is not stored inline.
745
+ *
746
+ * The store is a write of the array for every rule that asks, the rule for a
747
+ * parallel body included. To the bounds proof it is the store `xs[i] = v` is,
748
+ * less the check, and a resize in the value stored still takes the length away.
749
+ * Under `--deny-panics` it is an allowed `unchecked` site. Under Node the
750
+ * runtime shim stores `xs[i] = v`. Capability: unsafe.
645
751
  *
646
752
  * @param xs the array of numbers to write.
647
753
  * @param i the index, trusted to be in range.
@@ -651,12 +757,14 @@ declare function uncheckedSet<T extends number>(xs: T[], i: i32, v: T): void;
651
757
  /**
652
758
  * `a + b` wrapped to the operands' width, `i32` or `i64`: defined for every
653
759
  * pair, where the operator `+` panics on signed overflow. The two operands are
654
- * one type, which is also the answer: `` `wrappingMul` takes i32 or i64 operands
655
- * (convert with toI32 or toI64), got u32 `` is NL2454 (the unsigned words
656
- * already wrap), and two widths are `` `wrappingAdd` needs every operand of one
657
- * type, got i32 and i64 `` (NL2400). A bare literal takes the other operand's
658
- * type, wherever it stands. `wrappingAdd(2147483647, 1)` is `-2147483648`. In a
659
- * module constant's initialiser the call folds, wrapping, where
760
+ * one type, which is also the answer: `` `wrappingMul` takes i32 or i64
761
+ * operands (convert with toI32 or toI64), got u32 `` is NL2454 (the unsigned
762
+ * words already wrap), and two widths are `` `wrappingAdd` needs every operand
763
+ * of one type, got i32 and i64 `` (NL2400). A bare literal takes the other
764
+ * operand's type, wherever it stands. `wrappingAdd(2147483647, 1)` is
765
+ * `-2147483648`.
766
+ *
767
+ * In a module constant's initialiser the call folds, wrapping, where
660
768
  * `2147483647 + 1` is refused; it is the one call such an initialiser may make.
661
769
  * It is not a site under `--deny-panics`, as it cannot panic. Under Node the
662
770
  * runtime shim computes `(a + b) | 0` on an `i32` and `BigInt.asIntN(64, ...)`
@@ -691,21 +799,24 @@ declare function wrappingMul<T extends i32 | i64>(a: T, b: T): T;
691
799
  * The `NUL`-terminated bytes at `p`, copied into a new string of the program's
692
800
  * own: undefined behaviour unless `p` is a live C string, since nothing proves
693
801
  * the bytes end (a dangling or freed pointer, or a buffer with no `NUL`). It is
694
- * the copy a `string` result of a `declare function` makes, for the pointers
695
- * C allocated for the caller to free, and C's buffer is never held past the
696
- * call, so `free(p)` straight after it is safe. The parameter is `CPtr`, so an
697
- * unnarrowed `CPtr | null` is refused (`` `readCString` expects an argument of
698
- * type CPtr, got CPtr | null ``) and `if (p !== null)` comes first; a
699
- * `declare function` whose result is declared `CPtr` gets no check, so declare
700
- * a result that may be `NULL` as `CPtr | null`. The copy is new on every call
701
- * and is the calling function's allocation, and it is refused inside a
702
- * `using a = arena()` block that would release one held outside it
703
- * (`` `readCString(p)` may be allocated inside the `using arena()` block ``).
704
- * Under `--deny-panics` it is an allowed `unchecked` site, with the copy's
705
- * `oom` beside it, and the pointer it reads is `ffi`. It lowers the same on
706
- * wasm32, where whether the program links is the `declare function`'s question.
707
- * It has no Node form, as a `CPtr` comes only from a `declare function`, which
708
- * Node cannot call. Capability: unsafe.
802
+ * the copy a `string` result of a `declare function` makes, for the pointers C
803
+ * allocated for the caller to free, and C's buffer is never held past the call,
804
+ * so `free(p)` straight after it is safe.
805
+ *
806
+ * The parameter is `CPtr`, so an unnarrowed `CPtr | null` is refused (``
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`.
811
+ *
812
+ * The copy is new on every call and is the calling function's allocation, and
813
+ * it is refused inside a `using a = arena()` block that would release one held
814
+ * 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
817
+ * on wasm32, where whether the program links is the `declare function`'s
818
+ * question. It has no Node form, as a `CPtr` comes only from a `declare
819
+ * function`, which Node cannot call. Capability: unsafe.
709
820
  *
710
821
  * @param p the address a `declare function` handed back.
711
822
  * @returns a copy of the bytes before the first `NUL`.
@@ -741,14 +852,15 @@ declare function writeError(s: string): void;
741
852
  * — the one way to send bytes that are not text, a byte of 0x80 or above or a
742
853
  * zero byte included. A loop of `write(2)` sends them all, retried on an
743
854
  * interrupted call. A pipe whose reader has gone raises SIGPIPE natively, as it
744
- * does for `write`, where Node answers `-32` EPIPE. `off` and `len` are
745
- * `netWrite`'s buffer range, checked once after the arguments are evaluated:
746
- * `0 <= off`, `0 <= len` and `off + len <= bytes.length`, and anything else
747
- * panics, as `dst.set` does, with `slice out of range: [off, off+len) of length
748
- * n` and exits 1. The shape is `netWrite`'s with the descriptor fixed at 1.
749
- * Under Node the range is the same check and the bytes go to
750
- * `fs.writeSync(1, ...)`. A `wasm32` build has no stdout to reach and refuses it. Also exported
751
- * by `nish:io`. Capability: none.
855
+ * does for `write`, where Node answers `-32` EPIPE.
856
+ *
857
+ * `off` and `len` are `netWrite`'s buffer range, checked once after the
858
+ * arguments are evaluated: `0 <= off`, `0 <= len` and `off + len <=
859
+ * bytes.length`, and anything else panics, as `dst.set` does, with `slice out
860
+ * of range: [off, off+len) of length n` and exits 1. The shape is `netWrite`'s
861
+ * with the descriptor fixed at 1. Under Node the range is the same check and
862
+ * the bytes go to `fs.writeSync(1, ...)`. A `wasm32` build has no stdout to
863
+ * reach and refuses it. Also exported by `nish:io`. Capability: none.
752
864
  *
753
865
  * @param bytes the buffer to send.
754
866
  * @param off the first byte sent; `0 <= off`.
@@ -873,21 +985,24 @@ declare function renameSync(from: string, to: string): i32;
873
985
  */
874
986
  declare function unlinkSync(path: string): i32;
875
987
  /**
876
- * `writeFileSync` with an explicit `mode`: the file is exactly `mode`
877
- * (`0o600` for a secret) before a byte of `data` is in it, whatever the umask
878
- * or a file already there had. The open passes `mode` and an `fchmod(2)` sets
879
- * it before the first byte is written, so the umask cannot widen it, a file that
880
- * already existed at 0644 is narrowed before it holds the new contents, and no
881
- * reader that opens it afterwards finds the secret under the old mode. A
882
- * descriptor opened before, while the file was wider, is not revoked and still
883
- * reads what is written. A symbolic link at the path is refused (-40), its
884
- * target untouched. The file is truncated and written in place, so a crash or a
885
- * failed write leaves it empty or partial, and a file this process may write but
886
- * does not own is emptied before the mode change answers -1 (`EPERM`). For a file
887
- * that must be the old one or the new one, write a temporary name beside the
888
- * target with this and move it over with `renameSync`. Never an exit and never a
889
- * panic. A `wasm32` build refuses it, and a WASI build answers `-ENOSYS`, as
890
- * WASI has no file modes to set. Capability: fs.write.
988
+ * `writeFileSync` with an explicit `mode`: the file is exactly `mode` (`0o600`
989
+ * for a secret) before a byte of `data` is in it, whatever the umask or a file
990
+ * already there had. The open passes `mode` and an `fchmod(2)` sets it before
991
+ * the first byte is written, so the umask cannot widen it, a file that already
992
+ * existed at 0644 is narrowed before it holds the new contents, and no reader
993
+ * that opens it afterwards finds the secret under the old mode.
994
+ *
995
+ * A descriptor opened before, while the file was wider, is not revoked and
996
+ * still reads what is written. A symbolic link at the path is refused (-40),
997
+ * its target untouched. The file is truncated and written in place, so a crash
998
+ * or a failed write leaves it empty or partial, and a file this process may
999
+ * write but does not own is emptied before the mode change answers -1
1000
+ * (`EPERM`).
1001
+ *
1002
+ * For a file that must be the old one or the new one, write a temporary name
1003
+ * beside the target with this and move it over with `renameSync`. Never an exit
1004
+ * and never a panic. A `wasm32` build refuses it, and a WASI build answers
1005
+ * `-ENOSYS`, as WASI has no file modes to set. Capability: fs.write.
891
1006
  *
892
1007
  * @param path the file to write.
893
1008
  * @param data its new contents.
@@ -961,8 +1076,10 @@ declare function isDirectorySync(path: string): boolean;
961
1076
  * like every string comparison here, so it agrees with Node's `.sort()` for
962
1077
  * ASCII names and can differ for names that are not. `null` when the directory
963
1078
  * cannot be read at all — a missing path, a plain file, a parent that cannot be
964
- * searched. An empty directory is an empty array, so the null check is about
965
- * the directory and not about its contents. A WASI build always answers `null`:
1079
+ * searched.
1080
+ *
1081
+ * An empty directory is an empty array, so the null check is about the
1082
+ * directory and not about its contents. A WASI build always answers `null`:
966
1083
  * `fd_readdir` lists a preopened directory rather than a path, which is a
967
1084
  * different contract. Capability: fs.read.
968
1085
  *
@@ -1019,17 +1136,18 @@ declare function spawnSyncTo(argv: string[], stdoutPath: string, stderrPath: str
1019
1136
  * `stdin`'s bytes and then end of file, and whatever it prints is appended to
1020
1137
  * `stdout` and `stderr`, which keep what they held. The runtime writes `stdin`
1021
1138
  * and drains both outputs in one `poll` loop, so a child that fills one pipe
1022
- * while the parent waits on another cannot deadlock it. When a stream passes
1023
- * 16 MiB (`NISH_PIPED_CAP`) the child is killed with `SIGKILL` and that array
1024
- * keeps the first 16 MiB. A child that exits without reading its stdin is not a
1025
- * failure. The child inherits the environment and working directory, and no
1026
- * descriptor of the program but its three streams; it starts with SIGPIPE as
1027
- * the program had it, and the program's own SIGPIPE from writing to a child
1028
- * that stopped reading is absorbed. The two output arrays are grown by the
1029
- * runtime, so `readonly` is refused for them, as is a typed-array spelling
1030
- * (`Int32Array`, ...), whose length is fixed. A `wasm32` build refuses it
1031
- * (NL2404).
1032
- * Capability: process.spawn.
1139
+ * while the parent waits on another cannot deadlock it. When a stream passes 16
1140
+ * MiB (`NISH_PIPED_CAP`) the child is killed with `SIGKILL` and that array
1141
+ * keeps the first 16 MiB.
1142
+ *
1143
+ * A child that exits without reading its stdin is not a failure. The child
1144
+ * inherits the environment and working directory, and no descriptor of the
1145
+ * program but its three streams; it starts with SIGPIPE as the program had it,
1146
+ * and the program's own SIGPIPE from writing to a child that stopped reading is
1147
+ * absorbed. The two output arrays are grown by the runtime, so `readonly` is
1148
+ * refused for them, as is a typed-array spelling (`Int32Array`, ...), whose
1149
+ * length is fixed. A `wasm32` build refuses it (NL2404). Capability:
1150
+ * process.spawn.
1033
1151
  *
1034
1152
  * @param argv the program and its arguments.
1035
1153
  * @param stdin the bytes the child reads.
@@ -1101,13 +1219,14 @@ declare function kill(pid: i32, sig: i32): i32;
1101
1219
  * tuple). `pid` is a child's, or -1 for any child. After `signalFd()` a
1102
1220
  * blocking wait never sees EINTR, because that handler is installed with
1103
1221
  * `SA_RESTART` and the wait restarts, and it cannot be woken by SIGTERM either:
1104
- * a supervisor that must answer a signal while its children run reaps with
1105
- * flag 1 (WNOHANG) on each tick of a `pollWait` loop that also watches the
1106
- * signal descriptor, and stops its children with `kill` when `readSignal`
1107
- * answers. No reading under Node, which throws, naming itself. A `wasm32` build
1108
- * refuses it (NL2404), and a scope's task or a parallel body that calls it is
1109
- * refused as any shared write is. Also exported by `nish:process`.
1110
- * Capability: process.spawn.
1222
+ * a supervisor that must answer a signal while its children run reaps with flag
1223
+ * 1 (WNOHANG) on each tick of a `pollWait` loop that also watches the signal
1224
+ * descriptor, and stops its children with `kill` when `readSignal` answers.
1225
+ *
1226
+ * No reading under Node, which throws, naming itself. A `wasm32` build refuses
1227
+ * it (NL2404), and a scope's task or a parallel body that calls it is refused
1228
+ * as any shared write is. Also exported by `nish:process`. Capability:
1229
+ * process.spawn.
1111
1230
  *
1112
1231
  * @param pid the child to wait for, or -1.
1113
1232
  * @param flags 0, or 1 for WNOHANG; any other bit is `[-22, 0]`, as is a pid
@@ -1151,11 +1270,12 @@ declare function totalMemory(): i64;
1151
1270
  * calling thread's and every thread started after it, so it belongs at the top
1152
1271
  * of `main`, before a `scope()` or a parallel loop starts the pool, and a task
1153
1272
  * or a parallel body that calls it is refused as any shared write is. It never
1154
- * crashes the program: a failure is only the answer. A `wasm32` build refuses
1155
- * it (NL2404). Throws under Node. Also exported by `nish:process`. The label is
1156
- * `kill`'s: neither reaches another process, but the priority is the one the
1157
- * program's children inherit, and a step down cannot always be taken back.
1158
- * Capability: process.spawn.
1273
+ * crashes the program: a failure is only the answer.
1274
+ *
1275
+ * A `wasm32` build refuses it (NL2404). Throws under Node. Also exported by
1276
+ * `nish:process`. The label is `kill`'s: neither reaches another process, but
1277
+ * the priority is the one the program's children inherit, and a step down
1278
+ * cannot always be taken back. Capability: process.spawn.
1159
1279
  *
1160
1280
  * @param n the nice value, 0..19.
1161
1281
  * @returns 0, or a negative errno: `n` outside 0..19 is -22 (EINVAL), and `n`
@@ -1194,15 +1314,16 @@ declare function setIdlePriority(): i32;
1194
1314
  declare function setCpuAffinity(cpu: i32): i32;
1195
1315
  /**
1196
1316
  * One environment variable, as the call runs. The value is what `getenv(3)`
1197
- * answers, so it is bytes: a variable holding something that is not valid
1198
- * UTF-8 comes back as those bytes, as `readFileSync` does, and every offset
1199
- * into it is a byte offset. The bytes are copied into the arena, so the result
1200
- * is an ordinary string that outlives any later change to the environment, and
1201
- * it is narrowed with `if (cc !== null)` like any other nullable. It is a call
1202
- * and not `process.env.CC`, because a member access on a key chosen at runtime
1203
- * is forbidden. Two reads of one variable are two calls: a `setenv` from linked
1204
- * C may change the answer between them. Also exported by `nish:process`.
1205
- * Capability: env.
1317
+ * answers, so it is bytes: a variable holding something that is not valid UTF-8
1318
+ * comes back as those bytes, as `readFileSync` does, and every offset into it
1319
+ * is a byte offset. The bytes are copied into the arena, so the result is an
1320
+ * ordinary string that outlives any later change to the environment, and it is
1321
+ * narrowed with `if (cc !== null)` like any other nullable.
1322
+ *
1323
+ * It is a call and not `process.env.CC`, because a member access on a key
1324
+ * chosen at runtime is forbidden. Two reads of one variable are two calls: a
1325
+ * `setenv` from linked C may change the answer between them. Also exported by
1326
+ * `nish:process`. Capability: env.
1206
1327
  *
1207
1328
  * @param name the variable's name.
1208
1329
  * @returns its value, or `null` when it is unset (an empty value, as `CC=`, is a
@@ -1250,9 +1371,11 @@ declare function statMtimeSync(path: string): f64;
1250
1371
  * A descriptor that becomes readable when SIGTERM or SIGINT arrives. It is the
1251
1372
  * read end of a pipe that a `sigaction` handler in the runtime's C writes one
1252
1373
  * byte to per signal, so the signals' default action (ending the process) never
1253
- * runs. The handler runs in whichever thread the signal lands on, so a `scope()`
1254
- * task or a parallel worker running when it is called cannot take the signal and
1255
- * end the process, and nothing is blocked for a spawned child to inherit.
1374
+ * runs. The handler runs in whichever thread the signal lands on, so a
1375
+ * `scope()` task or a parallel worker running when it is called cannot take the
1376
+ * signal and end the process, and nothing is blocked for a spawned child to
1377
+ * inherit.
1378
+ *
1256
1379
  * Installing the handler overrides a disposition the process started with, so a
1257
1380
  * signal its parent ignored is heard, as `process.on("SIGTERM")` hears it under
1258
1381
  * Node. Made once: every later call answers the same descriptor. A child that
@@ -1260,9 +1383,11 @@ declare function statMtimeSync(path: string): f64;
1260
1383
  * signal, because `exec` resets a caught signal to its default. Signals other
1261
1384
  * than SIGTERM and SIGINT are not in the language. A descriptor rather than a
1262
1385
  * handler, so a server's loop can wait on it beside its sockets with
1263
- * `pollWait`. A `wasm32` build and the `wasi` profile refuse it (NL2404). No
1264
- * reading under Node, which delivers a signal only to its event loop: it throws.
1265
- * Also exported by `nish:process`. Capability: signal.
1386
+ * `pollWait`.
1387
+ *
1388
+ * A `wasm32` build and the `wasi` profile refuse it (NL2404). No reading under
1389
+ * Node, which delivers a signal only to its event loop: it throws. Also
1390
+ * exported by `nish:process`. Capability: signal.
1266
1391
  *
1267
1392
  * @returns the descriptor, or -1 when it cannot be made.
1268
1393
  */
@@ -1335,20 +1460,27 @@ declare function netLocalPort(fd: i32): i32;
1335
1460
  */
1336
1461
  declare function tcpListen(host: string, port: i32, backlog: i32): i32;
1337
1462
  /**
1338
- * `tcpListen`'s socket, with `SO_REUSEPORT` for flag 1 and `TCP_NODELAY` on each accepted socket
1339
- * for flag 2. Flag 1 lets several listeners that all ask for it share one port, each with its own
1340
- * accept queue, whether one process holds them all or each of several processes holds one. On Linux
1341
- * the connections are spread by a hash of the connection's 4-tuple and only listeners of the same
1342
- * user may share; on Darwin the listeners share the port but one of them takes the connections, so
1343
- * the flag buys a restart without a gap rather than a spread. Flag 2 sets `TCP_NODELAY` on every
1344
- * connection `tcpAccept` takes from the listener, on every platform: the runtime sets it on each
1345
- * accepted socket, and a connection on which it cannot be set is closed and its failure answered.
1346
- * Flag 4, with flag 1, steers by CPU: a new connection goes to the group's listener whose bind
1347
- * order is the index of the CPU that received it (Linux; -95 elsewhere); a CPU past the group's
1348
- * size falls back to the hash. Flags add: 3 is both, 5 and 7 steer. Flag 0 is `tcpListen` exactly.
1349
- * A port held by a listener without flag 1 is -98 to a listener that asks for it, and the other way
1350
- * round. Refused at compile time on wasm32 and the WASI profile (NL2404). Not callable in a
1351
- * `scope()` task or a parallel body. Capability: net.
1463
+ * `tcpListen`'s socket, with `SO_REUSEPORT` for flag 1 and `TCP_NODELAY` on
1464
+ * each accepted socket for flag 2. Flag 1 lets several listeners that all ask
1465
+ * for it share one port, each with its own accept queue, whether one process
1466
+ * holds them all or each of several processes holds one.
1467
+ *
1468
+ * On Linux the connections are spread by a hash of the connection's 4-tuple and
1469
+ * only listeners of the same user may share; on Darwin the listeners share the
1470
+ * port but one of them takes the connections, so the flag buys a restart
1471
+ * without a gap rather than a spread. Flag 2 sets `TCP_NODELAY` on every
1472
+ * connection `tcpAccept` takes from the listener, on every platform: the
1473
+ * runtime sets it on each accepted socket, and a connection on which it cannot
1474
+ * be set is closed and its failure answered.
1475
+ *
1476
+ * Flag 4, with flag 1, steers by CPU: a new connection goes to the group's
1477
+ * listener whose bind order is the index of the CPU that received it (Linux;
1478
+ * -95 elsewhere); a CPU past the group's size falls back to the hash. Flags
1479
+ * add: 3 is both, 5 and 7 steer. Flag 0 is `tcpListen` exactly. A port held by
1480
+ * a listener without flag 1 is -98 to a listener that asks for it, and the
1481
+ * other way round. Refused at compile time on wasm32 and the WASI profile
1482
+ * (NL2404). Not callable in a `scope()` task or a parallel body. Capability:
1483
+ * net.
1352
1484
  *
1353
1485
  * @param host the numeric address to bind.
1354
1486
  * @param port the port, 0 for any.
@@ -1406,8 +1538,8 @@ declare function netRead(fd: i32, buf: u8[], off: i32, len: i32): i32;
1406
1538
  declare function netWrite(fd: i32, buf: readonly u8[], off: i32, len: i32): i32;
1407
1539
  /**
1408
1540
  * `netWrite` of `a`'s range then `b`'s, in one system call (`sendmsg` with two buffers), so a
1409
- * header and a body leave together with no copy into one buffer. The count may be fewer than `aLen
1410
- * + bLen` and may end inside `a`; the program sends the rest, as after a short `netWrite`. A gone
1541
+ * header and a body leave together with no copy into one buffer. The count may be fewer than
1542
+ * `aLen + bLen` and may end inside `a`; the program sends the rest, as after a short `netWrite`. A gone
1411
1543
  * peer is -32, never SIGPIPE. Both ranges are checked as `netRead`'s is, `a`'s first, and either
1412
1544
  * may be empty. Not callable in a `scope()` task or a parallel body. Refused at compile time on
1413
1545
  * wasm32 and the WASI profile (NL2404). Capability: net.
@@ -1534,18 +1666,24 @@ declare function udpSendTo(
1534
1666
  ecn: i32
1535
1667
  ): i32;
1536
1668
  /**
1537
- * The next datagram, or several GRO coalesced, into `buf[off, off + len)` in one `recvmsg`, cut to
1538
- * `len` if it is longer. An empty datagram is a datagram: the answer is 0 for it. Not `willreturn`,
1539
- * as `udpSendTo`. The range is checked as `netRead`'s is, with the same panic. `meta` must be an
1540
- * `i32[]` (NL2406), and none of the written arrays may be `readonly`. On a socket bound with
1541
- * `udpBind` flag 2, one GSO send arrives as one datagram of up to 64 KB with `meta[0]` its segment
1542
- * size, so walk the buffer in steps of that size, the last datagram possibly shorter; without it
1543
- * the datagrams arrive apart with `meta[0]` 0. The kernel coalesces only datagrams of one flow that
1544
- * arrive after the option is set. On Darwin `meta` is always `0, 0`, and a `meta[1]` of 0 is also
1545
- * what a datagram that is not ECN-capable reads as. Not callable in a `scope()` task or a parallel
1546
- * body. A call that writes `buf`, `from` or `meta` is refused in a scope's region on an array a
1547
- * task may read, as `fill` is. Refused at compile time on wasm32 and the WASI profile (NL2404).
1548
- * Capability: net.
1669
+ * The next datagram, or several GRO coalesced, into `buf[off, off + len)` in
1670
+ * one `recvmsg`, cut to `len` if it is longer. An empty datagram is a datagram:
1671
+ * the answer is 0 for it. Not `willreturn`, as `udpSendTo`. The range is
1672
+ * checked as `netRead`'s is, with the same panic. `meta` must be an `i32[]`
1673
+ * (NL2406), and none of the written arrays may be `readonly`.
1674
+ *
1675
+ * On a socket bound with `udpBind` flag 2, one GSO send arrives as one datagram
1676
+ * of up to 64 KB with `meta[0]` its segment size, so walk the buffer in steps
1677
+ * of that size, the last datagram possibly shorter; without it the datagrams
1678
+ * arrive apart with `meta[0]` 0. The kernel coalesces only datagrams of one
1679
+ * flow that arrive after the option is set. On Darwin `meta` is always `0, 0`,
1680
+ * and a `meta[1]` of 0 is also what a datagram that is not ECN-capable reads
1681
+ * as.
1682
+ *
1683
+ * Not callable in a `scope()` task or a parallel body. A call that writes
1684
+ * `buf`, `from` or `meta` is refused in a scope's region on an array a task may
1685
+ * read, as `fill` is. Refused at compile time on wasm32 and the WASI profile
1686
+ * (NL2404). Capability: net.
1549
1687
  *
1550
1688
  * @param fd the UDP socket.
1551
1689
  * @param buf the buffer filled.
@@ -1624,18 +1762,23 @@ declare function pollModify(loop: i32, fd: i32, events: i32, token: i32): i32;
1624
1762
  */
1625
1763
  declare function pollRemove(loop: i32, fd: i32): i32;
1626
1764
  /**
1627
- * Wait for readiness, filling pairs: `ready[2k]` token, `ready[2k + 1]` events (1 readable, 2
1628
- * writable, with 4 added for a hang-up or an error). Not `willreturn`, because it can wait forever.
1629
- * It never writes past `ready.length`. `ready` must be an `i32[]` (the tokens are the program's own
1630
- * `i32`s) and not `readonly`. A signal that interrupts the wait makes it answer 0, as a timeout
1631
- * does, rather than wait past the deadline; the descriptor `signalFd()` answers can be added to the
1632
- * loop, and is readable when SIGTERM or SIGINT arrives. A UDP socket is always writable, and on
1633
- * Linux one whose both sides are shut is readable, writable and hung up, 7. On Darwin a descriptor
1634
- * both readable and writable is one pair, 3; a descriptor watching nothing is never reported (epoll
1635
- * still reports its hang-up or error as 4); and a peer's half-close is 5 (`1|4`) where Linux
1636
- * reports 1. Not callable in a `scope()` task or a parallel body. A call that writes `ready` is
1637
- * refused in a scope's region on an array a task may read, as `fill` is. Refused at compile time on
1638
- * wasm32 and the WASI profile (NL2404). Capability: net.
1765
+ * Wait for readiness, filling pairs: `ready[2k]` token, `ready[2k + 1]` events
1766
+ * (1 readable, 2 writable, with 4 added for a hang-up or an error). Not
1767
+ * `willreturn`, because it can wait forever. It never writes past
1768
+ * `ready.length`. `ready` must be an `i32[]` (the tokens are the program's own
1769
+ * `i32`s) and not `readonly`. A signal that interrupts the wait makes it answer
1770
+ * 0, as a timeout does, rather than wait past the deadline; the descriptor
1771
+ * `signalFd()` answers can be added to the loop, and is readable when SIGTERM
1772
+ * or SIGINT arrives.
1773
+ *
1774
+ * A UDP socket is always writable, and on Linux one whose both sides are shut
1775
+ * is readable, writable and hung up, 7. On Darwin a descriptor both readable
1776
+ * and writable is one pair, 3; a descriptor watching nothing is never reported
1777
+ * (epoll still reports its hang-up or error as 4); and a peer's half-close is 5
1778
+ * (`1|4`) where Linux reports 1. Not callable in a `scope()` task or a parallel
1779
+ * body. A call that writes `ready` is refused in a scope's region on an array a
1780
+ * task may read, as `fill` is. Refused at compile time on wasm32 and the WASI
1781
+ * profile (NL2404). Capability: net.
1639
1782
  *
1640
1783
  * @param loop the loop from `pollCreate`.
1641
1784
  * @param ready receives the pairs; shorter than 2 is -22.
@@ -1720,22 +1863,29 @@ declare const NET_SO_SNDBUF: i32;
1720
1863
  */
1721
1864
  declare const NET_SO_RCVBUF: i32;
1722
1865
  /**
1723
- * A listening Unix stream socket at `path` (`@name`: Linux's abstract namespace), its file mode
1724
- * 0600, so only its owner may connect. A Unix socket is a descriptor like any other: `netRead`,
1725
- * `netWrite`, `netShutdown`, `netClose` and the readiness loop take it, and a gone peer is -32. The
1726
- * path is bytes up to the first NUL, so a NUL inside it is -22, as an empty path is. It must fit
1727
- * `sun_path` with its NUL: 107 bytes on Linux, 103 on Darwin, measured on the string as written; a
1728
- * longer one is -36 before a socket exists. An abstract name leaves no file; its name is at most
1729
- * 107 bytes, a bare `@` is -22, and on Darwin every `@` path is -95. A file whose name starts with
1730
- * `@` is spelled `./@name`. A bind never replaces a file. Closing a listener does not unlink its
1731
- * file: `unlinkSync` it when the server stops, or let the next start's `NET_UNIX_UNLINK_STALE`
1732
- * clear it. A failure after the bind closes the socket and unlinks the file it made. A connect
1733
- * needs write permission on the file and search permission on every directory above it, which is
1734
- * the real boundary: put the socket in a directory only the users who may connect can reach. With
1735
- * `NET_UNIX_UNLINK_STALE`, the runtime connects to an existing socket file and unlinks it only on a
1736
- * refusal; between the probe and the unlink another process can bind the path, so the directory
1737
- * must be the program's own. Refused at compile time on wasm32 and the WASI profile (NL2404). Not
1738
- * callable in a `scope()` task or a parallel body. Capability: net.
1866
+ * A listening Unix stream socket at `path` (`@name`: Linux's abstract
1867
+ * namespace), its file mode 0600, so only its owner may connect. A Unix socket
1868
+ * is a descriptor like any other: `netRead`, `netWrite`, `netShutdown`,
1869
+ * `netClose` and the readiness loop take it, and a gone peer is -32. The path
1870
+ * is bytes up to the first NUL, so a NUL inside it is -22, as an empty path is.
1871
+ *
1872
+ * It must fit `sun_path` with its NUL: 107 bytes on Linux, 103 on Darwin,
1873
+ * measured on the string as written; a longer one is -36 before a socket
1874
+ * exists. An abstract name leaves no file; its name is at most 107 bytes, a
1875
+ * bare `@` is -22, and on Darwin every `@` path is -95. A file whose name
1876
+ * starts with `@` is spelled `./@name`. A bind never replaces a file. Closing a
1877
+ * listener does not unlink its file: `unlinkSync` it when the server stops, or
1878
+ * let the next start's `NET_UNIX_UNLINK_STALE` clear it.
1879
+ *
1880
+ * A failure after the bind closes the socket and unlinks the file it made. A
1881
+ * connect needs write permission on the file and search permission on every
1882
+ * directory above it, which is the real boundary: put the socket in a directory
1883
+ * only the users who may connect can reach. With `NET_UNIX_UNLINK_STALE`, the
1884
+ * runtime connects to an existing socket file and unlinks it only on a refusal;
1885
+ * between the probe and the unlink another process can bind the path, so the
1886
+ * directory must be the program's own. Refused at compile time on wasm32 and
1887
+ * the WASI profile (NL2404). Not callable in a `scope()` task or a parallel
1888
+ * body. Capability: net.
1739
1889
  *
1740
1890
  * @param path the socket file, or `@name`; it must be a `string`.
1741
1891
  * @param backlog the kernel's queue of unaccepted connections.
@@ -1795,14 +1945,17 @@ declare function unixAccept(fd: i32): i32;
1795
1945
  */
1796
1946
  declare function unixSocketPair(kind: i32, fds: i32[]): i32;
1797
1947
  /**
1798
- * Send `buf[off..off+len)` over the Unix socket `sock` with a copy of the descriptor `fd`
1799
- * (`SCM_RIGHTS`). The receiver's `recvFd` gets a new descriptor for the same open file, with its
1800
- * offset and status flags shared, as `dup` gives; `fd` stays open here. The descriptor rides on at
1801
- * least one byte of data, so the receiver's read is what tells it a descriptor came. A stream
1802
- * socket can send short: the descriptor travels with the first bytes, so send the rest with
1803
- * `netWrite`, not `sendFd`, or the receiver gets a second copy. The range is checked as `netRead`'s
1804
- * is, with the same panic; `buf` may be `readonly`. Not callable in a `scope()` task or a parallel
1805
- * body. Refused at compile time on wasm32 and the WASI profile (NL2404). Capability: net.
1948
+ * Send `buf[off..off+len)` over the Unix socket `sock` with a copy of the
1949
+ * descriptor `fd` (`SCM_RIGHTS`). The receiver's `recvFd` gets a new descriptor
1950
+ * for the same open file, with its offset and status flags shared, as `dup`
1951
+ * gives; `fd` stays open here. The descriptor rides on at least one byte of
1952
+ * data, so the receiver's read is what tells it a descriptor came. A stream
1953
+ * socket can send short: the descriptor travels with the first bytes, so send
1954
+ * the rest with `netWrite`, not `sendFd`, or the receiver gets a second copy.
1955
+ *
1956
+ * The range is checked as `netRead`'s is, with the same panic; `buf` may be
1957
+ * `readonly`. Not callable in a `scope()` task or a parallel body. Refused at
1958
+ * compile time on wasm32 and the WASI profile (NL2404). Capability: net.
1806
1959
  *
1807
1960
  * @param sock the Unix socket.
1808
1961
  * @param fd the descriptor sent.
@@ -1815,19 +1968,25 @@ declare function unixSocketPair(kind: i32, fds: i32[]): i32;
1815
1968
  */
1816
1969
  declare function sendFd(sock: i32, fd: i32, buf: readonly u8[], off: i32, len: i32): i32;
1817
1970
  /**
1818
- * Read at most `len` bytes into `buf[off..]` from the Unix socket `sock`, and the first descriptor
1819
- * that came with them into `fd[0]`, or -1 when the message carried none. Every other descriptor in
1820
- * the message is closed before the answer, so a peer cannot leave this process holding descriptors
1821
- * it never asked for; room is made for 8, so the kernel drops the rest of a message that carries
1822
- * more and the bytes are still answered. The one kept is close-on-exec, so a later `spawn` does not
1823
- * hand it on, and keeps the sender's blocking mode (it is not made non-blocking, so `netRead` on a
1824
- * blocking one waits: set what the program needs on it before it joins a loop). The range is
1825
- * checked as `netRead`'s is, with the same panic. `buf` and `fd` must not be `readonly`, and `fd`
1826
- * must be an `i32[]`. On Darwin the descriptor is made close-on-exec by a later `fcntl`, so a child
1827
- * another thread spawns between the two inherits it. Not callable in a `scope()` task or a parallel
1828
- * body. A call that writes `buf` or `fd` is refused in a scope's region on an array a task may
1829
- * read, as `fill` is. Refused at compile time on wasm32 and the WASI profile (NL2404). Capability:
1830
- * net.
1971
+ * Read at most `len` bytes into `buf[off..]` from the Unix socket `sock`, and
1972
+ * the first descriptor that came with them into `fd[0]`, or -1 when the message
1973
+ * carried none. Every other descriptor in the message is closed before the
1974
+ * answer, so a peer cannot leave this process holding descriptors it never
1975
+ * asked for; room is made for 8, so the kernel drops the rest of a message that
1976
+ * carries more and the bytes are still answered.
1977
+ *
1978
+ * The one kept is close-on-exec, so a later `spawn` does not hand it on, and
1979
+ * keeps the sender's blocking mode (it is not made non-blocking, so `netRead`
1980
+ * on a blocking one waits: set what the program needs on it before it joins a
1981
+ * loop). The range is checked as `netRead`'s is, with the same panic. `buf` and
1982
+ * `fd` must not be `readonly`, and `fd` must be an `i32[]`. On Darwin the
1983
+ * descriptor is made close-on-exec by a later `fcntl`, so a child another
1984
+ * thread spawns between the two inherits it.
1985
+ *
1986
+ * Not callable in a `scope()` task or a parallel body. A call that writes `buf`
1987
+ * or `fd` is refused in a scope's region on an array a task may read, as `fill`
1988
+ * is. Refused at compile time on wasm32 and the WASI profile (NL2404).
1989
+ * Capability: net.
1831
1990
  *
1832
1991
  * @param sock the Unix socket.
1833
1992
  * @param buf the buffer filled.
@@ -1840,14 +1999,17 @@ declare function sendFd(sock: i32, fd: i32, buf: readonly u8[], off: i32, len: i
1840
1999
  */
1841
2000
  declare function recvFd(sock: i32, buf: u8[], off: i32, len: i32, fd: i32[]): i32;
1842
2001
  /**
1843
- * The peer of the Unix socket `fd` as the kernel recorded it when the connection was made (the
1844
- * `connect`, or the `socketpair`): `out` gets its pid, uid and gid. A peer that has since changed
1845
- * its uid, passed the socket on or closed still reads as it was then. The pid is -1 where Darwin
1846
- * has no `LOCAL_PEERPID` or the kernel refuses it. A uid or gid of 2^31 or above wraps (-2 is uid
1847
- * 4294967294). `out` must be an `i32[]` and not `readonly`. Not callable in a `scope()` task or a
1848
- * parallel body. A call that writes `out` is refused in a scope's region on an array a task may
1849
- * read, as `fill` is. Refused at compile time on wasm32 and the WASI profile (NL2404). Capability:
1850
- * net.
2002
+ * The peer of the Unix socket `fd` as the kernel recorded it when the
2003
+ * connection was made (the `connect`, or the `socketpair`): `out` gets its pid,
2004
+ * uid and gid. A peer that has since changed its uid, passed the socket on or
2005
+ * closed still reads as it was then. The pid is -1 where Darwin has no
2006
+ * `LOCAL_PEERPID` or the kernel refuses it. A uid or gid of 2^31 or above wraps
2007
+ * (-2 is uid 4294967294). `out` must be an `i32[]` and not `readonly`. Not
2008
+ * callable in a `scope()` task or a parallel body.
2009
+ *
2010
+ * A call that writes `out` is refused in a scope's region on an array a task
2011
+ * may read, as `fill` is. Refused at compile time on wasm32 and the WASI
2012
+ * profile (NL2404). Capability: net.
1851
2013
  *
1852
2014
  * @param fd the connected Unix socket.
1853
2015
  * @param out at least 3 slots, receiving pid, uid and gid.
@@ -1923,15 +2085,18 @@ declare const NET_UNIX_SEQPACKET: i32;
1923
2085
  */
1924
2086
  declare function readStdin(buf: u8[], off: i32, len: i32): i32;
1925
2087
  /**
1926
- * Wait for stdin to be readable, or `timeoutMs` milliseconds to pass: the frame clock of a
1927
- * terminal program, which costs nothing when a key arrives early. Its end and an error count
1928
- * as readable, so the read after it answers at once, with 0 or a negative errno that is not
1929
- * -11, which a loop treats as the end. A signal that ends the wait early gives `false`, except
1930
- * a Ctrl-Z and `fg` once `setRawMode(true)` has caught SIGTSTP, after which the wait goes on
1931
- * for what is left of the timeout; before raw mode the system's default stop decides (Linux
1932
- * goes on with what is left, Darwin ends the wait). Not `willreturn`. Under `--profile wasi`
1933
- * it waits at least 10 ms; under Node it throws; refused at compile time on a freestanding
1934
- * wasm32 target. Capability: tty.
2088
+ * Wait for stdin to be readable, or `timeoutMs` milliseconds to pass: the frame
2089
+ * clock of a terminal program, which costs nothing when a key arrives early.
2090
+ * Its end and an error count as readable, so the read after it answers at once,
2091
+ * with 0 or a negative errno that is not -11, which a loop treats as the end.
2092
+ *
2093
+ * A signal that ends the wait early gives `false`, except a Ctrl-Z and `fg`
2094
+ * once `setRawMode(true)` has caught SIGTSTP, after which the wait goes on for
2095
+ * what is left of the timeout; before raw mode the system's default stop
2096
+ * decides (Linux goes on with what is left, Darwin ends the wait). Not
2097
+ * `willreturn`. Under `--profile wasi` it waits at least 10 ms; under Node it
2098
+ * throws; refused at compile time on a freestanding wasm32 target. Capability:
2099
+ * tty.
1935
2100
  *
1936
2101
  * @param timeoutMs the most milliseconds to wait; negative: forever.
1937
2102
  * @returns `true` when it is readable, `false` when the time passed or a signal ended the wait.
@@ -1958,32 +2123,45 @@ declare function isTerminal(fd: i32): boolean;
1958
2123
  declare function terminalSize(): i32[];
1959
2124
  /**
1960
2125
  * Raw mode on stdin's terminal, or the mode it had back. No echo, no line
1961
- * editing, no CR-to-NL translation, no flow control (Ctrl-S, Ctrl-Q) and no parity stripping;
1962
- * bytes are 8 bits wide, and a read answers as soon as one byte is there, so a key arrives as
1963
- * its bytes (an arrow as the three of `ESC [ A`). `ISIG` stays on: Ctrl-C is still SIGINT and
1964
- * ends the program, Ctrl-\ is still SIGQUIT and Ctrl-Z still stops it, so none of them arrives
1965
- * as a byte (3, 28, 26); a program that wants Ctrl-C as a key asks for SIGINT through
1966
- * `signalFd` and handles it in its loop. `OPOST` stays on, so "\n" still moves to the start of
1967
- * the next line and output needs no "\r\n". Each `setRawMode(true)` that enters raw mode saves
1968
- * the termios that was there; it comes back on `setRawMode(false)`, when `main` returns or
1969
- * `process.exit` is called, on every panic, and on every signal whose default action ends the
1970
- * program, SIGKILL aside (SIGINT, SIGTERM, SIGQUIT, SIGHUP, SIGPIPE, SIGALRM, SIGUSR1,
1971
- * SIGUSR2, SIGPROF, SIGVTALRM, SIGXCPU, SIGXFSZ, SIGSYS, and the faults SIGILL, SIGTRAP,
1972
- * SIGABRT, SIGFPE, SIGBUS and SIGSEGV). For each, a handler is installed only when the program
1973
- * left it at its default action: it restores the terminal, puts the default back and raises
1974
- * the signal again, so the program still dies of it and its parent sees that (exit status 143
1975
- * for SIGTERM). A program that took SIGTERM or SIGINT through `signalFd` keeps it and puts
1976
- * the terminal back itself, or by returning. SIGKILL leaves raw mode behind, and so does a
1977
- * stack overflow on a thread other than the one that called `setRawMode(true)`; `stty sane`
1978
- * undoes either, as it does a stop by SIGSTOP. Ctrl-Z (SIGTSTP) restores the terminal, stops
1979
- * the program, and once `fg` or `bg` continues it enters raw mode again; a program continued
1980
- * in the background stops again, by SIGTTOU, until `fg`. A program started in the background
1981
- * (`app &`) that calls `setRawMode(true)` is stopped by SIGTTOU, as any program that sets the
1982
- * terminal is, and enters raw mode on `fg`. Two threads that call it at once take turns, so
1983
- * the termios saved is the one from before either. Asking for the mode it is already in
1984
- * changes nothing and answers 0, so `setRawMode(false)` is always safe. Under `--profile
1985
- * wasi`, on a pipe or a file, `true` is -25 and `false` is 0; under Node and in a browser it
1986
- * throws; refused at compile time on a freestanding wasm32 target. Capability: tty.
2126
+ * editing, no CR-to-NL translation, no flow control (Ctrl-S, Ctrl-Q) and no
2127
+ * parity stripping; bytes are 8 bits wide, and a read answers as soon as one
2128
+ * byte is there, so a key arrives as its bytes (an arrow as the three of `ESC [
2129
+ * A`).
2130
+ *
2131
+ * `ISIG` stays on: Ctrl-C is still SIGINT and ends the program, Ctrl-\ is still
2132
+ * SIGQUIT and Ctrl-Z still stops it, so none of them arrives as a byte (3, 28,
2133
+ * 26); a program that wants Ctrl-C as a key asks for SIGINT through `signalFd`
2134
+ * and handles it in its loop. `OPOST` stays on, so "\n" still moves to the
2135
+ * start of the next line and output needs no "\r\n".
2136
+ *
2137
+ * Each `setRawMode(true)` that enters raw mode saves the termios that was
2138
+ * there; it comes back on `setRawMode(false)`, when `main` returns or
2139
+ * `process.exit` is called, on every panic, and on every signal whose default
2140
+ * action ends the program, SIGKILL aside (SIGINT, SIGTERM, SIGQUIT, SIGHUP,
2141
+ * SIGPIPE, SIGALRM, SIGUSR1, SIGUSR2, SIGPROF, SIGVTALRM, SIGXCPU, SIGXFSZ,
2142
+ * SIGSYS, and the faults SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS and SIGSEGV).
2143
+ *
2144
+ * For each, a handler is installed only when the program left it at its default
2145
+ * action: it restores the terminal, puts the default back and raises the signal
2146
+ * again, so the program still dies of it and its parent sees that (exit status
2147
+ * 143 for SIGTERM). A program that took SIGTERM or SIGINT through `signalFd`
2148
+ * keeps it and puts the terminal back itself, or by returning.
2149
+ *
2150
+ * SIGKILL leaves raw mode behind, and so does a stack overflow on a thread
2151
+ * other than the one that called `setRawMode(true)`; `stty sane` undoes either,
2152
+ * as it does a stop by SIGSTOP. Ctrl-Z (SIGTSTP) restores the terminal, stops
2153
+ * the program, and once `fg` or `bg` continues it enters raw mode again; a
2154
+ * program continued in the background stops again, by SIGTTOU, until `fg`. A
2155
+ * program started in the background (`app &`) that calls `setRawMode(true)` is
2156
+ * stopped by SIGTTOU, as any program that sets the terminal is, and enters raw
2157
+ * mode on `fg`.
2158
+ *
2159
+ * Two threads that call it at once take turns, so the termios saved is the one
2160
+ * from before either. Asking for the mode it is already in changes nothing and
2161
+ * answers 0, so `setRawMode(false)` is always safe. Under `--profile wasi`, on
2162
+ * a pipe or a file, `true` is -25 and `false` is 0; under Node and in a browser
2163
+ * it throws; refused at compile time on a freestanding wasm32 target.
2164
+ * Capability: tty.
1987
2165
  *
1988
2166
  * @param on `true` for raw mode, `false` for the mode before.
1989
2167
  * @returns 0, -25 when stdin is not a terminal, or the host's `-errno` when the terminal
@@ -1991,18 +2169,21 @@ declare function terminalSize(): i32[];
1991
2169
  */
1992
2170
  declare function setRawMode(on: boolean): i32;
1993
2171
  /**
1994
- * Bytes the runtime writes to the terminal on stdout at exit, on a panic and
1995
- * on every signal that ends the program (SIGKILL aside), before termios is
1996
- * restored; raw mode or not, and setting them installs the same handlers. Typical bytes are
1997
- * the escapes that leave an alternate screen, show the cursor or turn mouse reporting off.
1998
- * Replaces the previous string; "" clears it. The cap is 4096
1999
- * bytes. They are written to stdout once, with `write` alone, at most once however many of
2000
- * those paths race, and after all the program printed (output is not buffered). Ctrl-Z and
2001
- * `setRawMode(false)` do not end the program, so they write nothing. A signal that lands while
2002
- * the string is replaced writes the old one or the new one, whole. A program sets them once it
2003
- * has switched the terminal and again with each change, last the set that undoes them all.
2004
- * Under `--profile wasi` it keeps nothing; under Node and in a browser it throws; refused at
2005
- * compile time on a freestanding wasm32 target. Capability: tty.
2172
+ * Bytes the runtime writes to the terminal on stdout at exit, on a panic and on
2173
+ * every signal that ends the program (SIGKILL aside), before termios is
2174
+ * restored; raw mode or not, and setting them installs the same handlers.
2175
+ * Typical bytes are the escapes that leave an alternate screen, show the cursor
2176
+ * or turn mouse reporting off. Replaces the previous string; "" clears it. The
2177
+ * cap is 4096 bytes.
2178
+ *
2179
+ * They are written to stdout once, with `write` alone, at most once however
2180
+ * many of those paths race, and after all the program printed (output is not
2181
+ * buffered). Ctrl-Z and `setRawMode(false)` do not end the program, so they
2182
+ * write nothing. A signal that lands while the string is replaced writes the
2183
+ * old one or the new one, whole. A program sets them once it has switched the
2184
+ * terminal and again with each change, last the set that undoes them all. Under
2185
+ * `--profile wasi` it keeps nothing; under Node and in a browser it throws;
2186
+ * refused at compile time on a freestanding wasm32 target. Capability: tty.
2006
2187
  *
2007
2188
  * @param bytes the bytes written at the end.
2008
2189
  * @returns 0, -25 when stdout is not a terminal (`""` is still 0 there), -7 (E2BIG) past
@@ -2012,27 +2193,55 @@ declare function setTerminalRestore(bytes: string): i32;
2012
2193
 
2013
2194
  // ---- `Date` and `crypto` (WP34 N3) -----------------------------------------------
2014
2195
  //
2015
- // `lib.es2022` already declares `Date`, whose `now()` is Nish's one member of it,
2016
- // so nothing is added: `tsc` accepting `new Date()` is `tsc` not being Nish's
2017
- // checker. `crypto` is a Web API that `lib.es2022` leaves out, so it is
2018
- // declared with its one member, typed as Nish types it.
2196
+ // `lib.es2022` already declares `Date`, whose `now()` is Nish's one member of it;
2197
+ // `DateConstructor` below repeats lib's signature of it to document it, and adds
2198
+ // nothing: `tsc` accepting `new Date()` is `tsc` not being Nish's checker.
2199
+ // `crypto` is a Web API that `lib.es2022` leaves out, so it is declared with its
2200
+ // one member, typed as Nish types it.
2201
+
2202
+ /**
2203
+ * `Date`, merged into the `DateConstructor` `lib.es2022` declares. Nish has no
2204
+ * `Date` object: `Date.now()` is its one member, and `new Date()`, `Date()`,
2205
+ * `Date.parse`, `Date.UTC`, any other member, and `Date` or `Date.now` used as
2206
+ * a value are each refused: "`new Date()` is refused: Nish has no `Date`
2207
+ * object; the one `Date` member is `Date.now()`, the wall clock in
2208
+ * milliseconds" (NL2401).
2209
+ */
2210
+ interface DateConstructor {
2211
+ /**
2212
+ * The wall clock, `CLOCK_REALTIME`, in whole milliseconds since 1970-01-01
2213
+ * UTC, as JavaScript answers it: the milliseconds are counted as integers and
2214
+ * then converted, so the value is exact and a time before 1970 floors. An
2215
+ * `f64` in both number modes, because an `i32` of milliseconds overflowed in
2216
+ * January 1970. It can go backwards when the clock is corrected, so an
2217
+ * interval is `monotonicNanos`'s job, not this one's. A wasm32 build has no
2218
+ * operating system to ask, and refuses it. Capability: clock.
2219
+ *
2220
+ * @returns the milliseconds, an `f64`.
2221
+ */
2222
+ now(): number;
2223
+ }
2019
2224
 
2020
2225
  /** The system's random source, with its one member. */
2021
2226
  declare var crypto: {
2022
2227
  /**
2023
- * Every byte of `bytes` from the kernel's CSPRNG (`getrandom(2)` on Linux, looping over
2024
- * `EINTR` and short reads; `getentropy(3)` in 256-byte chunks on macOS); at most 65,536
2025
- * bytes a call. A `u8[]` only, where the Web API takes any integer typed array: the byte is
2026
- * the unit a key or a nonce is written in, and anything else is refused at compile time
2027
- * (NL2402). A statement, where the Web API also answers its argument, which the caller
2028
- * already holds. An empty array is filled with nothing. The array is written, so a
2029
- * `readonly u8[]` is refused (and, inside a `scope()` region, an array a task may read).
2030
- * More than 65,536 bytes is a panic, `crypto.getRandomValues: <n> bytes asked for, and one
2031
- * call fills at most 65536`, exit 1. A source that fails is a panic too; nothing weaker is
2032
- * ever substituted. It is the source for a key. `crypto` has no other member (NL2403, for
2033
- * example `crypto.randomUUID`). Refused at compile time on wasm32 and the WASI profile
2034
- * (NL2404). Under Node the prelude fills a plain `u8[]` too and panics past 65,536 with the
2035
- * same words; without the prelude it takes only a typed array. Capability: entropy.
2228
+ * Every byte of `bytes` from the kernel's CSPRNG (`getrandom(2)` on Linux,
2229
+ * looping over `EINTR` and short reads; `getentropy(3)` in 256-byte chunks on
2230
+ * macOS); at most 65,536 bytes a call. A `u8[]` only, where the Web API takes
2231
+ * any integer typed array: the byte is the unit a key or a nonce is written
2232
+ * in, and anything else is refused at compile time (NL2402). A statement,
2233
+ * where the Web API also answers its argument, which the caller already
2234
+ * holds. An empty array is filled with nothing.
2235
+ *
2236
+ * The array is written, so a `readonly u8[]` is refused (and, inside a
2237
+ * `scope()` region, an array a task may read). More than 65,536 bytes is a
2238
+ * panic, `crypto.getRandomValues: <n> bytes asked for, and one call fills at
2239
+ * most 65536`, exit 1. A source that fails is a panic too; nothing weaker is
2240
+ * ever substituted. It is the source for a key. `crypto` has no other member
2241
+ * (NL2403, for example `crypto.randomUUID`). Refused at compile time on
2242
+ * wasm32 and the WASI profile (NL2404). Under Node the prelude fills a plain
2243
+ * `u8[]` too and panics past 65,536 with the same words; without the prelude
2244
+ * it takes only a typed array. Capability: entropy.
2036
2245
  *
2037
2246
  * @param bytes the buffer filled.
2038
2247
  */
@@ -2388,14 +2597,16 @@ declare module "nish:process" {
2388
2597
  /**
2389
2598
  * The same run as `spawnSync`, with every stream a pipe: the child reads
2390
2599
  * `stdin`'s bytes and then end of file, and whatever it writes to its stdout
2391
- * and stderr is appended to `stdout` and `stderr`, which keep what they
2392
- * held. It is the run for a child that takes a secret on stdin, which `ps`
2393
- * and `/proc` do not show. A child that exits without reading its stdin is
2394
- * not a failure. The child inherits the environment and working directory,
2395
- * and no descriptor of the program but its three streams; it starts with
2396
- * SIGPIPE as the program had it, and the program's own SIGPIPE from writing
2397
- * to a child that stopped reading is absorbed. One `poll` loop writes and
2398
- * drains, so a child that fills one pipe cannot deadlock the parent.
2600
+ * and stderr is appended to `stdout` and `stderr`, which keep what they held.
2601
+ * It is the run for a child that takes a secret on stdin, which `ps` and
2602
+ * `/proc` do not show. A child that exits without reading its stdin is not a
2603
+ * failure.
2604
+ *
2605
+ * The child inherits the environment and working directory, and no descriptor
2606
+ * of the program but its three streams; it starts with SIGPIPE as the program
2607
+ * had it, and the program's own SIGPIPE from writing to a child that stopped
2608
+ * reading is absorbed. One `poll` loop writes and drains, so a child that
2609
+ * fills one pipe cannot deadlock the parent.
2399
2610
  *
2400
2611
  * @param argv The program and its arguments.
2401
2612
  * @param stdin The bytes the child reads.
@@ -3373,7 +3584,22 @@ declare module "nish:simd" {
3373
3584
  * A `u8x16` of sixteen lanes: one `insertelement` per lane, which `opt`
3374
3585
  * folds to a constant vector when every lane is one.
3375
3586
  *
3376
- * @param a0 Lane 0, a `u8`; `a1` to `a15` are lanes 1 to 15 in order.
3587
+ * @param a0 Lane 0, a `u8`.
3588
+ * @param a1 Lane 1, a `u8`.
3589
+ * @param a2 Lane 2, a `u8`.
3590
+ * @param a3 Lane 3, a `u8`.
3591
+ * @param a4 Lane 4, a `u8`.
3592
+ * @param a5 Lane 5, a `u8`.
3593
+ * @param a6 Lane 6, a `u8`.
3594
+ * @param a7 Lane 7, a `u8`.
3595
+ * @param a8 Lane 8, a `u8`.
3596
+ * @param a9 Lane 9, a `u8`.
3597
+ * @param a10 Lane 10, a `u8`.
3598
+ * @param a11 Lane 11, a `u8`.
3599
+ * @param a12 Lane 12, a `u8`.
3600
+ * @param a13 Lane 13, a `u8`.
3601
+ * @param a14 Lane 14, a `u8`.
3602
+ * @param a15 Lane 15, a `u8`.
3377
3603
  * @returns The vector.
3378
3604
  */
3379
3605
  export function u8x16(
@@ -3383,35 +3609,52 @@ declare module "nish:simd" {
3383
3609
  /**
3384
3610
  * A `u16x8` of 8 lanes, one argument per lane, lane 0 first.
3385
3611
  *
3386
- * @param a0 Lane 0, a `u16`; `a1` to `a7` are lanes 1 to 7 in order.
3612
+ * @param a0 Lane 0, a `u16`.
3613
+ * @param a1 Lane 1, a `u16`.
3614
+ * @param a2 Lane 2, a `u16`.
3615
+ * @param a3 Lane 3, a `u16`.
3616
+ * @param a4 Lane 4, a `u16`.
3617
+ * @param a5 Lane 5, a `u16`.
3618
+ * @param a6 Lane 6, a `u16`.
3619
+ * @param a7 Lane 7, a `u16`.
3387
3620
  * @returns The vector.
3388
3621
  */
3389
3622
  export function u16x8(a0: u16, a1: u16, a2: u16, a3: u16, a4: u16, a5: u16, a6: u16, a7: u16): u16x8;
3390
3623
  /**
3391
3624
  * An `i32x4` of 4 lanes, one argument per lane, lane 0 first.
3392
3625
  *
3393
- * @param a0 Lane 0, an `i32`; `a1` to `a3` are lanes 1 to 3 in order.
3626
+ * @param a0 Lane 0, an `i32`.
3627
+ * @param a1 Lane 1, an `i32`.
3628
+ * @param a2 Lane 2, an `i32`.
3629
+ * @param a3 Lane 3, an `i32`.
3394
3630
  * @returns The vector.
3395
3631
  */
3396
3632
  export function i32x4(a0: i32, a1: i32, a2: i32, a3: i32): i32x4;
3397
3633
  /**
3398
3634
  * A `u32x4` of 4 lanes, one argument per lane, lane 0 first.
3399
3635
  *
3400
- * @param a0 Lane 0, a `u32`; `a1` to `a3` are lanes 1 to 3 in order.
3636
+ * @param a0 Lane 0, a `u32`.
3637
+ * @param a1 Lane 1, a `u32`.
3638
+ * @param a2 Lane 2, a `u32`.
3639
+ * @param a3 Lane 3, a `u32`.
3401
3640
  * @returns The vector.
3402
3641
  */
3403
3642
  export function u32x4(a0: u32, a1: u32, a2: u32, a3: u32): u32x4;
3404
3643
  /**
3405
3644
  * An `f32x4` of 4 lanes, one argument per lane, lane 0 first.
3406
3645
  *
3407
- * @param a0 Lane 0, an `f32`; `a1` to `a3` are lanes 1 to 3 in order.
3646
+ * @param a0 Lane 0, an `f32`.
3647
+ * @param a1 Lane 1, an `f32`.
3648
+ * @param a2 Lane 2, an `f32`.
3649
+ * @param a3 Lane 3, an `f32`.
3408
3650
  * @returns The vector.
3409
3651
  */
3410
3652
  export function f32x4(a0: f32, a1: f32, a2: f32, a3: f32): f32x4;
3411
3653
  /**
3412
3654
  * An `f64x2` of 2 lanes, one argument per lane, lane 0 first.
3413
3655
  *
3414
- * @param a0 Lane 0, an `f64`; `a1` is lane 1.
3656
+ * @param a0 Lane 0, an `f64`.
3657
+ * @param a1 Lane 1, an `f64`.
3415
3658
  * @returns The vector.
3416
3659
  */
3417
3660
  export function f64x2(a0: f64, a1: f64): f64x2;
@@ -4413,9 +4656,22 @@ declare module "nish:region" {
4413
4656
  * spawn(log(c + d));
4414
4657
  * }
4415
4658
  *
4416
- * The two are reached only through the import. Each argument is a direct call
4417
- * to an `async` function, written where it is made, which the checker holds
4418
- * it to (`src/async-check.ts`); `tsc` sees ordinary promises.
4659
+ * All five are reached only through the import. Each argument to `awaitAll`
4660
+ * or `spawn` is a direct call to an `async` function, written where it is
4661
+ * made, which the checker holds it to (`src/async-check.ts`); `tsc` sees ordinary promises. A spawned
4662
+ * call's arguments are numbers, `boolean`s, enums or `string`s (NL2673): the
4663
+ * task outlives the function that spawned it.
4664
+ *
4665
+ * `sleep(ms)`, `readable(fd)` and `writable(fd)` are the waits a task makes
4666
+ * on the executor, an epoll (kqueue on Darwin) loop that resumes ready tasks
4667
+ * in the kernel's order and then expired timers by deadline:
4668
+ *
4669
+ * import { readable, sleep } from "nish:async";
4670
+ *
4671
+ * async function tick(): Promise<void> {
4672
+ * await sleep(10);
4673
+ * console.log("tick");
4674
+ * }
4419
4675
  *
4420
4676
  * There is no `async` in a wasm build: under a `wasm32` target or
4421
4677
  * `--profile wasi` an `async` function is NL2652.
@@ -4564,6 +4820,102 @@ declare module "nish:async" {
4564
4820
  g: Promise<G>,
4565
4821
  h: Promise<H>,
4566
4822
  ): Promise<[A, B, C, D, E, F, G, H]>;
4823
+ /**
4824
+ * Suspend the task for `ms` milliseconds while the executor runs the
4825
+ * others. Written only as `await sleep(ms)` inside an `async` function
4826
+ * (NL2671).
4827
+ *
4828
+ * @param ms The milliseconds to wait, an `i32`.
4829
+ * @returns `true` once the time has passed; `false` at once for a negative
4830
+ * `ms`, which makes no wait.
4831
+ */
4832
+ export function sleep(ms: i32): Promise<boolean>;
4833
+ /**
4834
+ * Suspend the task until `fd` has bytes to read, or its peer has hung up,
4835
+ * while the executor runs the others. Written only as
4836
+ * `await readable(fd)` inside an `async` function (NL2671). Refused under
4837
+ * `--emit-ts` (NL4017).
4838
+ *
4839
+ * @param fd An open descriptor: a socket, a pipe or a terminal.
4840
+ * @returns `true` once `fd` is ready; `false` when the wait could not be
4841
+ * made (an `fd` that is not open). A regular file is always ready.
4842
+ */
4843
+ export function readable(fd: i32): Promise<boolean>;
4844
+ /**
4845
+ * Suspend the task until `fd` takes bytes without blocking, while the
4846
+ * executor runs the others. Written only as `await writable(fd)` inside an
4847
+ * `async` function (NL2671). Refused under `--emit-ts` (NL4017).
4848
+ *
4849
+ * @param fd An open descriptor: a socket, a pipe or a terminal.
4850
+ * @returns `true` once `fd` is ready; `false` when the wait could not be
4851
+ * made (an `fd` that is not open). A regular file is always ready.
4852
+ */
4853
+ export function writable(fd: i32): Promise<boolean>;
4854
+ /**
4855
+ * Suspend the task until `fd` has bytes to read, or its peer has hung up,
4856
+ * or `ms` milliseconds pass, whichever comes first. Written only as
4857
+ * `await readableWithin(fd, ms)` inside an `async` function (NL2671).
4858
+ * Refused under `--emit-ts` (NL4018).
4859
+ *
4860
+ * @param fd An open descriptor: a socket, a pipe or a terminal.
4861
+ * @param ms The most milliseconds to wait; a negative `ms` waits without a
4862
+ * limit, as `readable` does.
4863
+ * @returns `true` once `fd` is ready; `false` when `ms` passed first, when
4864
+ * the wait could not be made, or when the task is cancelled.
4865
+ */
4866
+ export function readableWithin(fd: i32, ms: i32): Promise<boolean>;
4867
+ /**
4868
+ * Suspend the task until `fd` takes bytes without blocking, or `ms`
4869
+ * milliseconds pass, whichever comes first. Written only as
4870
+ * `await writableWithin(fd, ms)` inside an `async` function (NL2671).
4871
+ * Refused under `--emit-ts` (NL4018).
4872
+ *
4873
+ * @param fd An open descriptor: a socket, a pipe or a terminal.
4874
+ * @param ms The most milliseconds to wait; a negative `ms` waits without a
4875
+ * limit, as `writable` does.
4876
+ * @returns `true` once `fd` is ready; `false` when `ms` passed first, when
4877
+ * the wait could not be made, or when the task is cancelled.
4878
+ */
4879
+ export function writableWithin(fd: i32, ms: i32): Promise<boolean>;
4880
+ /**
4881
+ * The running task's handle, to hand to `cancel` or `cancelAfter`. `main`'s
4882
+ * handle is 0, which no call cancels. Refused under `--emit-ts` (NL4018).
4883
+ *
4884
+ * @returns The handle, an `i64` that names this task and no later one.
4885
+ */
4886
+ export function taskId(): i64;
4887
+ /**
4888
+ * Cancel a task: every `sleep`, `readable` or `writable` it is waiting on
4889
+ * answers `false` at once, and so does every one it makes later, so it runs
4890
+ * on to its end, its `using` bindings dispose and its region is freed. A
4891
+ * task is never stopped in the middle of its code. Refused under
4892
+ * `--emit-ts` (NL4018).
4893
+ *
4894
+ * @param task A handle `taskId()` answered.
4895
+ * @returns `true` while the task is still running, so it is now cancelled
4896
+ * (a second `cancel` of it answers `true` too); `false` for a task that
4897
+ * has finished, or for `main`.
4898
+ */
4899
+ export function cancel(task: i64): boolean;
4900
+ /**
4901
+ * Whether the running task has been cancelled. Refused under `--emit-ts`
4902
+ * (NL4018).
4903
+ *
4904
+ * @returns `true` once `cancel` was called on it, or its `cancelAfter`
4905
+ * deadline passed.
4906
+ */
4907
+ export function cancelled(): boolean;
4908
+ /**
4909
+ * Cancel a task `ms` milliseconds from now, unless it finishes first. A
4910
+ * later call replaces the deadline; a negative `ms` removes it. Refused
4911
+ * under `--emit-ts` (NL4018).
4912
+ *
4913
+ * @param task A handle `taskId()` answered.
4914
+ * @param ms The milliseconds until the task is cancelled.
4915
+ * @returns `true` when the task is still running and the deadline is set
4916
+ * (or removed); `false` for a task that has finished or is `main`.
4917
+ */
4918
+ export function cancelAfter(task: i64, ms: i32): boolean;
4567
4919
  }
4568
4920
 
4569
4921
  // ---- Arena (docs/LANGUAGE.md -> Arena) ---------------------------------------
@@ -4605,25 +4957,30 @@ declare const Arena: {
4605
4957
  * initialiser, and the binding is never read; under Node there is no arena and
4606
4958
  * the disposal does nothing.
4607
4959
  *
4608
- * `arena()` anywhere but the initialiser of a `using` is NL2416, and reading the
4609
- * binding is NL2417. A number, a `boolean` or an `enum` leaves the block freely,
4610
- * as does a value older than it. These routes are refused for anything else
4611
- * allocated inside: assigning it to a local declared outside the block (NL2418),
4612
- * storing it into a field (NL2419) or an element (NL2420), storing it any other
4613
- * way (NL2421), a `push` onto an array the block does not bind to a fresh array
4614
- * (NL2422), a `return` or `orReturn()` (NL2423), a call to a function that
4615
- * stores an allocation (NL2424) and a `spawn` on a scope declared outside the
4616
- * block (NL2427), on a scope the same `using` declares ahead of `arena()`
4617
- * (`using s = scope(), a = arena()`, NL2427, since `s` joins after the release)
4618
- * or on one ahead of the statement's last `arena()`
4619
- * (`using a = arena(), s = scope(), b = arena()`, NL2427, since `b` releases
4620
- * before `s` joins). The rule is the escape analysis's, so it is conservative.
4621
- * A parallel body or a task may not open one (NL2351). A map that answers numbers
4622
- * or strings may run inside one, and a task filed on a scope the block declares
4623
- * may read what it allocated. It nests with `scope()` and `lock()` on the same
4624
- * stack, innermost first. A `return g(n)` whose arguments are all numbers is a
4625
- * tail call with the release moved ahead of it, unless `g` reads the arena.
4626
- * A panic ends the process. Capability: none.
4960
+ * `arena()` anywhere but the initialiser of a `using` is NL2416, and reading
4961
+ * the binding is NL2417. A number, a `boolean` or an `enum` leaves the block
4962
+ * freely, as does a value older than it.
4963
+ *
4964
+ * These routes are refused for anything else allocated inside:
4965
+ * - assigning it to a local declared outside the block (NL2418),
4966
+ * - storing it into a field (NL2419) or an element (NL2420),
4967
+ * - storing it any other way (NL2421),
4968
+ * - a `push` onto an array the block does not bind to a fresh array (NL2422),
4969
+ * - a `return` or `orReturn()` (NL2423),
4970
+ * - a call to a function that stores an allocation (NL2424)
4971
+ * - and a `spawn` on a scope declared outside the block (NL2427), on a scope
4972
+ * the same `using` declares ahead of `arena()` (`using s = scope(), a =
4973
+ * arena()`, NL2427, since `s` joins after the release) or on one ahead of the
4974
+ * statement's last `arena()` (`using a = arena(), s = scope(), b = arena()`,
4975
+ * NL2427, since `b` releases before `s` joins).
4976
+ *
4977
+ * The rule is the escape analysis's, so it is conservative. A parallel body or
4978
+ * a task may not open one (NL2351). A map that answers numbers or strings may
4979
+ * run inside one, and a task filed on a scope the block declares may read what
4980
+ * it allocated. It nests with `scope()` and `lock()` on the same stack,
4981
+ * innermost first. A `return g(n)` whose arguments are all numbers is a tail
4982
+ * call with the release moved ahead of it, unless `g` reads the arena. A panic
4983
+ * ends the process. Capability: none.
4627
4984
  *
4628
4985
  * @returns the scope the `using` declaration disposes.
4629
4986
  */
@@ -4631,18 +4988,20 @@ declare function arena(): Disposable;
4631
4988
 
4632
4989
  /**
4633
4990
  * `CPtr`: the address a `declare function` hands back, opaque and eight bytes
4634
- * wide (`i8*` in the IR). It is C's, not arena memory, and its lifetime is the C
4635
- * library's business. `docs/LANGUAGE.md` has the rules — it may be written in a
4636
- * `declare function` signature and on a local that holds one, compared with
4637
- * `null` or with another `CPtr`, assigned, and passed back to C, and that is all.
4638
- * A field, an array element, a `Result` arm, a type argument, and the parameters
4639
- * or return type of a function the program defines are each refused (`` `CPtr`
4640
- * cannot be ...: a foreign pointer may only appear in a `declare function`
4641
- * signature or on a local bound to one ``). There is no arithmetic (`p + 1`), no
4642
- * index (`` Cannot index a value of type CPtr ``), no dereference and no member.
4643
- * A `CPtr | null` reaches C only as the literal `null` or a pointer narrowed
4644
- * with `!== null` (NL2539). `readCString` from `nish:unsafe` copies the string
4645
- * at one.
4991
+ * wide (`i8*` in the IR). It is C's, not arena memory, and its lifetime is the
4992
+ * C library's business. `docs/LANGUAGE.md` has the rules — it may be written in
4993
+ * a `declare function` signature and on a local that holds one, compared with
4994
+ * `null` or with another `CPtr`, assigned, and passed back to C, and that is
4995
+ * all.
4996
+ *
4997
+ * A field, an array element, a `Result` arm, a type argument, and the
4998
+ * parameters or return type of a function the program defines are each refused
4999
+ * (`` `CPtr` cannot be ...: a foreign pointer may only appear in a `declare
5000
+ * function` signature or on a local bound to one ``). There is no arithmetic
5001
+ * (`p + 1`), no index (`` Cannot index a value of type CPtr ``), no dereference
5002
+ * and no member. A `CPtr | null` reaches C only as the literal `null` or a
5003
+ * pointer narrowed with `!== null` (NL2539). `readCString` from `nish:unsafe`
5004
+ * copies the string at one.
4646
5005
  *
4647
5006
  * An empty interface with a private brand rather than `unknown` or a type
4648
5007
  * alias: `tsc` has to refuse the arithmetic and the dereference `nish` refuses,
@@ -4651,6 +5010,7 @@ declare function arena(): Disposable;
4651
5010
  * declaration costs a reader nothing.
4652
5011
  */
4653
5012
  declare interface CPtr {
5013
+ /** The private brand that makes `CPtr` nominal. Not a field: nothing reads or writes it. */
4654
5014
  readonly __nishForeignPointer: unique symbol;
4655
5015
  }
4656
5016
 
@@ -4685,16 +5045,25 @@ type HostHandle<K extends string> = (number & { readonly __nishHostKind: K }) |
4685
5045
  * an `i64` or `u64` crosses as a `bigint`, every other width as a `number`.
4686
5046
  */
4687
5047
  declare module "nish" {
5048
+ /** A 32-bit signed integer, a `number` on the page. */
4688
5049
  export type i32 = number;
5050
+ /** A 64-bit signed integer, a `bigint` on the page. */
4689
5051
  export type i64 = bigint;
5052
+ /** An 8-bit unsigned integer, a `number` on the page. */
4690
5053
  export type u8 = number;
5054
+ /** A 16-bit unsigned integer, a `number` on the page. */
4691
5055
  export type u16 = number;
5056
+ /** A 32-bit unsigned integer, a `number` on the page. */
4692
5057
  export type u32 = number;
5058
+ /** A 64-bit unsigned integer, a `bigint` on the page. */
4693
5059
  export type u64 = bigint;
5060
+ /** A 32-bit float, a `number` on the page. */
4694
5061
  export type f32 = number;
5062
+ /** A 64-bit float, a `number` on the page. */
4695
5063
  export type f64 = number;
4696
5064
  /** A ranged integer, which the loader checks on the way in. */
4697
5065
  export type integer<Lo extends number, Hi extends number> = number;
5066
+ /** A host object's handle, the global `HostHandle`'s twin, spelled alike so the two are one type. */
4698
5067
  export type HostHandle<K extends string> = (number & { readonly __nishHostKind: K }) | 0;
4699
5068
  }
4700
5069
 
@@ -4919,26 +5288,28 @@ interface Math {
4919
5288
  /**
4920
5289
  * An array. `Int32Array`, `Float32Array`, `Float64Array` and `BigInt64Array`
4921
5290
  * name `i32[]`, `f32[]`, `f64[]` and `i64[]`, the same type with no second
4922
- * layout, but a receiver spelled with one of the four refuses `push` and
4923
- * `pop` (NL2415), because a typed array in TypeScript has a fixed length and a
4924
- * call to either is a `TypeError` there: "`push` is not a method of
4925
- * `Float64Array`, whose length is fixed: declare it `f64[]` to push and pop".
4926
- * Every other array operation is unchanged, and a `Float64Array` is an `f64[]`
4927
- * wherever one is expected. The refusal follows the spelling the program
4928
- * wrote, carried by: `new X(n)`; an identifier annotated with the name
4929
- * (directly, as the one member of `X | null`, or through a `type` alias) or
4930
- * unannotated and initialised with such a receiver; a field declared with the
4931
- * name; a call whose callee declares it as its return type; a call of a
4932
- * generic function whose return type is a type parameter that an argument
4933
- * binds to such a receiver; a value read out of a `Map` whose value type is
4934
- * spelled with a name; an element of an array spelled with a name; and a
4935
- * ternary or `??` either of whose operands carries it. A member of a generic
4936
- * class instantiated at one of the names does not carry it, and a typed array
4937
- * that flows into a binding spelled `T[]` may be pushed there. A numeric
4938
- * literal argument to `push`, `indexOf` or `fill` takes the element type, so
4939
- * `wide.push(3)` on an `i64[]` pushes an `i64` three. An unknown method is
4940
- * refused with the supported list: "Unknown method `shift` on i32[]"; there
4941
- * is no `map`, `shift`, `slice` or `toString` on an array.
5291
+ * layout, but a receiver spelled with one of the four refuses `push` and `pop`
5292
+ * (NL2415), because a typed array in TypeScript has a fixed length and a call
5293
+ * to either is a `TypeError` there: "`push` is not a method of `Float64Array`,
5294
+ * whose length is fixed: declare it `f64[]` to push and pop". Every other array
5295
+ * operation is unchanged, and a `Float64Array` is an `f64[]` wherever one is
5296
+ * expected.
5297
+ *
5298
+ * The refusal follows the spelling the program wrote, carried by: `new X(n)`;
5299
+ * an identifier annotated with the name (directly, as the one member of `X |
5300
+ * null`, or through a `type` alias) or unannotated and initialised with such a
5301
+ * receiver; a field declared with the name; a call whose callee declares it as
5302
+ * its return type; a call of a generic function whose return type is a type
5303
+ * parameter that an argument binds to such a receiver; a value read out of a
5304
+ * `Map` whose value type is spelled with a name; an element of an array spelled
5305
+ * with a name; and a ternary or `??` either of whose operands carries it.
5306
+ *
5307
+ * A member of a generic class instantiated at one of the names does not carry
5308
+ * it, and a typed array that flows into a binding spelled `T[]` may be pushed
5309
+ * there. A numeric literal argument to `push`, `indexOf` or `fill` takes the
5310
+ * element type, so `wide.push(3)` on an `i64[]` pushes an `i64` three. An
5311
+ * unknown method is refused with the supported list: "Unknown method `shift` on
5312
+ * i32[]"; there is no `shift`, `find` or `toString` on an array.
4942
5313
  */
4943
5314
  interface Array<T> {
4944
5315
  /** The element count, read-only: assigning to it is refused. */
@@ -4973,6 +5344,7 @@ interface Array<T> {
4973
5344
  * argument of another type than the element is refused. Capability: none.
4974
5345
  *
4975
5346
  * @param searchElement the element looked for.
5347
+ * @param fromIndex declared to match TypeScript's `lib`; `nish` refuses a second argument.
4976
5348
  * @returns its index, or `-1`.
4977
5349
  */
4978
5350
  indexOf(searchElement: T, fromIndex?: number): number;
@@ -4990,24 +5362,28 @@ interface Array<T> {
4990
5362
  join(separator?: string): string;
4991
5363
  /**
4992
5364
  * Store `value` into every slot of `[start, end)`, each end relative (a
4993
- * negative one counts back from the length) and then clamped into
4994
- * `[0, a.length]`, as `TypedArray.prototype.fill`'s are, so no range is out
4995
- * of range, a reversed range fills nothing, and `fill` never panics. Arrays
4996
- * of numbers only, of any width and ranged integers included (a ranged
4997
- * element checks `value` once, before the fill); on any other array it is
4998
- * refused: "`set` copies bytes, so it needs an array whose elements are
4999
- * numbers (`u8[]`, `i32[]`, `f64[]`, ...), got string[]" (NL2395). `value`
5000
- * is the element type ("`fill` expects u8 (the element type of u8[]), got
5001
- * string"), one to three arguments are taken (NL2398), and each end is a
5365
+ * negative one counts back from the length) and then clamped into `[0,
5366
+ * a.length]`, as `TypedArray.prototype.fill`'s are, so no range is out of
5367
+ * range, a reversed range fills nothing, and `fill` never panics. Arrays of
5368
+ * numbers only, of any width and ranged integers included (a ranged element
5369
+ * checks `value` once, before the fill); on any other array it is refused:
5370
+ * "`set` copies bytes, so it needs an array whose elements are numbers
5371
+ * (`u8[]`, `i32[]`, `f64[]`, ...), got string[]" (NL2395).
5372
+ *
5373
+ * `value` is the element type ("`fill` expects u8 (the element type of u8[]),
5374
+ * got string"), one to three arguments are taken (NL2398), and each end is a
5002
5375
  * number. It is a statement: its answer cannot be used as a value, and
5003
- * JavaScript's answer is the array the program already holds. A float end
5004
- * converts as JavaScript's `ToIntegerOrInfinity` does: `NaN` is `0`, a
5005
- * fraction truncates toward zero (`2.7` is `2`, `-0.5` is `0`), and an
5376
+ * JavaScript's answer is the array the program already holds.
5377
+ *
5378
+ * A float end converts as JavaScript's `ToIntegerOrInfinity` does: `NaN` is
5379
+ * `0`, a fraction truncates toward zero (`2.7` is `2`, `-0.5` is `0`), and an
5006
5380
  * infinity or a value past the `i64` range saturates, then clamps, so in f64
5007
5381
  * mode `a.fill(v, 0, 1 / 0)` fills all of it. An unsigned end is never
5008
5382
  * negative, whatever its width: a `u64` of 2^63 or more as a start fills
5009
5383
  * nothing and as an end fills to the length. Natively a `u8[]` fill is one
5010
- * `memset`. Under Node it is `Array.prototype.fill`, which has
5384
+ * `memset`.
5385
+ *
5386
+ * Under Node it is `Array.prototype.fill`, which has
5011
5387
  * `TypedArray.prototype.fill`'s meaning for every argument Nish admits,
5012
5388
  * except that an `i64` or `u64` end is a `bigint` there and throws a
5013
5389
  * `TypeError`, as all 64-bit arithmetic does. A `readonly T[]` refuses it:
@@ -5017,6 +5393,7 @@ interface Array<T> {
5017
5393
  * @param value the value stored.
5018
5394
  * @param start the first slot, `0` by default.
5019
5395
  * @param end the slot after the last, `a.length` by default.
5396
+ * @returns the array, to `tsc` only: `nish` refuses the call as a value.
5020
5397
  */
5021
5398
  fill(value: T, start?: number, end?: number): this;
5022
5399
  /**
@@ -5026,17 +5403,19 @@ interface Array<T> {
5026
5403
  * "`sort` needs a comparator: write `xs.sort((a, b) => a - b)`") and is the
5027
5404
  * one argument (NL2619: "`sort` takes one comparator"); only "positive or
5028
5405
  * not" is read of its answer, so `-0` and `NaN` count as zero. A `readonly
5029
- * T[]` ("Cannot `sort` through readonly") and `process.argv`
5030
- * ("`process.argv` is read-only") are refused. The comparator is a
5031
- * compile-time function argument: a top-level function, or an arrow reading
5032
- * only the `const` locals and parameters of the function it is written in;
5033
- * an arrow reading a `let` is refused. The sort costs one scratch array of
5034
- * `n` elements, released before it returns. `(a, b) => a - b` is checked
5035
- * arithmetic on `i32` elements: two a full range apart (`[2147483647, -1]`)
5036
- * panic with `attempt to subtract with overflow`, where
5037
- * `(a, b) => (a > b ? 1 : a < b ? -1 : 0)` cannot overflow. Where
5038
- * JavaScript leaves the order to the implementation, so does Nish: an array
5039
- * holding `NaN` under `(a, b) => a - b`, or a comparator that is not a
5406
+ * T[]` ("Cannot `sort` through readonly") and `process.argv` ("`process.argv`
5407
+ * is read-only") are refused.
5408
+ *
5409
+ * The comparator is a compile-time function argument: a top-level function,
5410
+ * or an arrow reading only the `const` locals and parameters of the function
5411
+ * it is written in; an arrow reading a `let` is refused. The sort costs one
5412
+ * scratch array of `n` elements, released before it returns. `(a, b) => a -
5413
+ * b` is checked arithmetic on `i32` elements: two a full range apart
5414
+ * (`[2147483647, -1]`) panic with `attempt to subtract with overflow`, where
5415
+ * `(a, b) => (a > b ? 1 : a < b ? -1 : 0)` cannot overflow.
5416
+ *
5417
+ * Where JavaScript leaves the order to the implementation, so does Nish: an
5418
+ * array holding `NaN` under `(a, b) => a - b`, or a comparator that is not a
5040
5419
  * consistent order, comes out a permutation of its elements but not
5041
5420
  * necessarily Node's permutation; give `NaN` a place of its own in the
5042
5421
  * comparator to agree. Under Node and `--emit-ts` the call is the platform's
@@ -5051,55 +5430,127 @@ interface Array<T> {
5051
5430
  /**
5052
5431
  * Copy every element of `source` into this array from `offset` on, with
5053
5432
  * `TypedArray.prototype.set`'s meaning. Arrays of numbers only, of any width
5054
- * and ranged integers included, and `source` has this array's element type
5055
- * (a `readonly` array may be the source, since it is only read); another
5056
- * element type is refused: "`set` copies from an array of the receiver's
5057
- * element type, so it expects u8[], got i32[]" (NL2396), and a non-number
5058
- * receiver as for `fill` (NL2395). One or two arguments (NL2397). It is a
5059
- * statement: "`set` returns void and can only be used as a statement". The
5060
- * range written is checked: `0 <= offset` and
5061
- * `offset + source.length <= a.length`, or the program panics with
5062
- * `slice out of range: [offset, offset + src.length) of length
5063
- * a.length` and exits 1, the message `s.slice` gives; a range that ends
5064
- * exactly at `a.length`, and an empty `source` at `a.length`, are in range,
5065
- * where JavaScript throws a `RangeError` for exactly these offsets. An
5066
- * overlapping `source` (`a.set(a)`, or two headers over one buffer from a C
5067
- * host) is copied as if it were read first. A float offset converts as for
5068
- * `fill`'s ends (`NaN` is `0`, a fraction truncates, an infinity or a value
5069
- * past the `i64` range saturates), and a saturated offset fails the range
5070
- * check with the saturated value in the message, for example
5071
- * `slice out of range: [9223372036854775807, -9223372036854775808) of
5072
- * length 4` for `1e300`. An unsigned offset is never negative: a `u64` of
5073
- * 2^63 or more panics, printed signed. A `readonly T[]` refuses it:
5074
- * "Cannot `set` through readonly u8[] (declare it u8[] to write through
5075
- * it)". Under Node, `runtime/nish.mjs` adds `set` to `Array.prototype` with
5076
- * the copy-first semantics and the native panic; a TypeScript host without
5077
- * it has no `set` on a plain array, and an `i64` or `u64` offset is a
5078
- * `bigint` there and throws a `TypeError`. Capability: none.
5433
+ * and ranged integers included, and `source` has this array's element type (a
5434
+ * `readonly` array may be the source, since it is only read); another element
5435
+ * type is refused: "`set` copies from an array of the receiver's element
5436
+ * type, so it expects u8[], got i32[]" (NL2396), and a non-number receiver as
5437
+ * for `fill` (NL2395). One or two arguments (NL2397). It is a statement:
5438
+ * "`set` returns void and can only be used as a statement".
5439
+ *
5440
+ * The range written is checked: `0 <= offset` and `offset + source.length <=
5441
+ * a.length`, or the program panics with `slice out of range: [offset,
5442
+ * offset + src.length) of length a.length` and exits 1, the message `s.slice` gives;
5443
+ * a range that ends exactly at `a.length`, and an empty `source` at
5444
+ * `a.length`, are in range, where JavaScript throws a `RangeError` for
5445
+ * exactly these offsets. An overlapping `source` (`a.set(a)`, or two headers
5446
+ * over one buffer from a C host) is copied as if it were read first.
5447
+ *
5448
+ * A float offset converts as for `fill`'s ends (`NaN` is `0`, a fraction
5449
+ * truncates, an infinity or a value past the `i64` range saturates), and a
5450
+ * saturated offset fails the range check with the saturated value in the
5451
+ * message, for example `slice out of range: [9223372036854775807,
5452
+ * -9223372036854775808) of length 4` for `1e300`. An unsigned offset is never
5453
+ * negative: a `u64` of 2^63 or more panics, printed signed. A `readonly T[]`
5454
+ * refuses it: "Cannot `set` through readonly u8[] (declare it u8[] to write
5455
+ * through it)".
5456
+ *
5457
+ * Under Node, `runtime/nish.mjs` adds `set` to `Array.prototype` with the
5458
+ * copy-first semantics and the native panic; a TypeScript host without it has
5459
+ * no `set` on a plain array, and an `i64` or `u64` offset is a `bigint` there
5460
+ * and throws a `TypeError`. Capability: none.
5079
5461
  *
5080
5462
  * @param source the elements copied.
5081
5463
  * @param offset the first slot written, `0` by default.
5082
5464
  */
5083
5465
  set(source: readonly T[], offset?: number): void;
5466
+ /**
5467
+ * A new array of `callbackfn(element)` for each element, in order, as
5468
+ * `Array.prototype.map` gives. The callback takes the element, or the
5469
+ * element and its index; a third parameter (JavaScript's array argument) is
5470
+ * refused (NL2698), as is a second argument to `map` itself. It is a
5471
+ * top-level function or an arrow reading only `const` locals and parameters,
5472
+ * and the call compiles to a loop that calls it directly (`std/array`'s
5473
+ * `map`). Capability: none.
5474
+ *
5475
+ * @param callbackfn called once per element, with the element and its index.
5476
+ * @returns a new array of the callback's answers, of the receiver's length.
5477
+ */
5478
+ map<U>(callbackfn: (value: T, index: number) => U): U[];
5479
+ /**
5480
+ * A new array of the elements for which `predicate` answers `true`, in
5481
+ * order, as `Array.prototype.filter` gives. The predicate answers a
5482
+ * `boolean` and takes the element, or the element and its index, under the
5483
+ * callback rules of `map`. Capability: none.
5484
+ *
5485
+ * @param predicate called once per element, with the element and its index.
5486
+ * @returns a new array of the elements kept.
5487
+ */
5488
+ filter(predicate: (value: T, index: number) => boolean): T[];
5489
+ /**
5490
+ * Whether `predicate` answers `true` for some element, stopping at the first
5491
+ * that does, as `Array.prototype.some` does; an empty array answers `false`.
5492
+ * The callback rules are `map`'s. Capability: none.
5493
+ *
5494
+ * @param predicate called in order, with the element and its index, until it answers `true`.
5495
+ * @returns `true` when some element satisfies `predicate`.
5496
+ */
5497
+ some(predicate: (value: T, index: number) => boolean): boolean;
5498
+ /**
5499
+ * Whether `predicate` answers `true` for every element, stopping at the
5500
+ * first that does not, as `Array.prototype.every` does; an empty array
5501
+ * answers `true`. The callback rules are `map`'s. Capability: none.
5502
+ *
5503
+ * @param predicate called in order, with the element and its index, until it answers `false`.
5504
+ * @returns `true` when no element fails `predicate`.
5505
+ */
5506
+ every(predicate: (value: T, index: number) => boolean): boolean;
5507
+ /**
5508
+ * The index of the first element for which `predicate` answers `true`, or
5509
+ * `-1`, as `Array.prototype.findIndex` gives. The callback rules are
5510
+ * `map`'s. There is no `find`: TypeScript types its answer `T | undefined`
5511
+ * and Nish has no `undefined`, so `xs.find(...)` is refused (NL2697) and
5512
+ * points here. Capability: none.
5513
+ *
5514
+ * @param predicate called in order, with the element and its index, until it answers `true`.
5515
+ * @returns the first such index, or `-1`.
5516
+ */
5517
+ findIndex(predicate: (value: T, index: number) => boolean): number;
5518
+ /**
5519
+ * A copy of the elements from `start` up to `end`, in a new array, with
5520
+ * string `slice`'s rule: the range must satisfy `0 <= start <= end <=
5521
+ * length`, and anything else panics with `slice out of range: [start, end)
5522
+ * of length n` and exits 1, where JavaScript counts a negative index back
5523
+ * from the length and clamps the rest. `start` is `0` and `end` the length
5524
+ * by default; a third argument is refused (NL2699). Under Node the prelude's
5525
+ * `sliceArray` applies the same check. Capability: none.
5526
+ *
5527
+ * @param start the first index copied, `0` by default.
5528
+ * @param end the index after the last copied, the length by default.
5529
+ * @returns a new array of `end - start` elements.
5530
+ */
5531
+ slice(start?: number, end?: number): T[];
5084
5532
  }
5085
5533
 
5086
5534
  /**
5087
5535
  * `readonly T[]`, or `ReadonlyArray<T>` under the other spelling: the same
5088
5536
  * array, which may not be written through. It is the same header, the same
5089
- * pointer and the same layout as `T[]`; the annotation is a promise the
5090
- * checker keeps, and a `--emit-header` prototype spells such a parameter
5091
- * `const nish_array *`. Everything that reads works (`length`, `a[i]`,
5092
- * `for...of`, `indexOf`, `join`, passing it on to another `readonly T[]`);
5093
- * every write is refused under its own name, an element store
5094
- * ("Cannot assign to an element of readonly i32[] (declare it i32[] to write
5095
- * through it)"), `push`, `pop`, `set`, `fill` and `sort` included. A `T[]`
5096
- * widens into a `readonly T[]`, and never back: "Argument 1 of `fill`:
5097
- * expected i32[], got readonly i32[]". It is shallow, as TypeScript's is: the
5098
- * element of a `readonly T[][]` is a mutable `T[]`. It is a type, not a
5099
- * parameter modifier, so it is legal wherever an array type is (a class
5100
- * field, a return type, a `const`'s annotation). `readonly` goes on an array
5101
- * type and nothing else: "`readonly` is only permitted on an array type, got
5102
- * i32". `process.argv` is read-only under a rule of its own.
5537
+ * pointer and the same layout as `T[]`; the annotation is a promise the checker
5538
+ * keeps, and a `--emit-header` prototype spells such a parameter `const
5539
+ * nish_array *`.
5540
+ *
5541
+ * Everything that reads works (`length`, `a[i]`, `for...of`, `indexOf`, `join`,
5542
+ * `map`, `filter`, `some`, `every`, `findIndex`, `slice`, passing it on to
5543
+ * another `readonly T[]`); every write is refused under its own name, an
5544
+ * element store ("Cannot assign to an element of readonly i32[]
5545
+ * (declare it i32[] to write through it)"), `push`, `pop`, `set`, `fill` and
5546
+ * `sort` included. A `T[]` widens into a `readonly T[]`, and never back:
5547
+ * "Argument 1 of `fill`: expected i32[], got readonly i32[]". It is shallow, as
5548
+ * TypeScript's is: the element of a `readonly T[][]` is a mutable `T[]`.
5549
+ *
5550
+ * It is a type, not a parameter modifier, so it is legal wherever an array type
5551
+ * is (a class field, a return type, a `const`'s annotation). `readonly` goes on
5552
+ * an array type and nothing else: "`readonly` is only permitted on an array
5553
+ * type, got i32". `process.argv` is read-only under a rule of its own.
5103
5554
  */
5104
5555
  interface ReadonlyArray<T> {
5105
5556
  /** The element count. */
@@ -5110,6 +5561,7 @@ interface ReadonlyArray<T> {
5110
5561
  * and a `NaN` element is never found. Capability: none.
5111
5562
  *
5112
5563
  * @param searchElement the element looked for.
5564
+ * @param fromIndex declared to match TypeScript's `lib`; `nish` refuses a second argument.
5113
5565
  * @returns its index, or `-1`.
5114
5566
  */
5115
5567
  indexOf(searchElement: T, fromIndex?: number): number;
@@ -5121,6 +5573,72 @@ interface ReadonlyArray<T> {
5121
5573
  * @returns the joined string.
5122
5574
  */
5123
5575
  join(separator?: string): string;
5576
+ /**
5577
+ * A new array of `callbackfn(element)` for each element, in order, as
5578
+ * `Array.prototype.map` gives. The callback takes the element, or the
5579
+ * element and its index; a third parameter (JavaScript's array argument) is
5580
+ * refused (NL2698), as is a second argument to `map` itself. It is a
5581
+ * top-level function or an arrow reading only `const` locals and parameters,
5582
+ * and the call compiles to a loop that calls it directly (`std/array`'s
5583
+ * `map`). Capability: none.
5584
+ *
5585
+ * @param callbackfn called once per element, with the element and its index.
5586
+ * @returns a new array of the callback's answers, of the receiver's length.
5587
+ */
5588
+ map<U>(callbackfn: (value: T, index: number) => U): U[];
5589
+ /**
5590
+ * A new array of the elements for which `predicate` answers `true`, in
5591
+ * order, as `Array.prototype.filter` gives. The predicate answers a
5592
+ * `boolean` and takes the element, or the element and its index, under the
5593
+ * callback rules of `map`. Capability: none.
5594
+ *
5595
+ * @param predicate called once per element, with the element and its index.
5596
+ * @returns a new array of the elements kept.
5597
+ */
5598
+ filter(predicate: (value: T, index: number) => boolean): T[];
5599
+ /**
5600
+ * Whether `predicate` answers `true` for some element, stopping at the first
5601
+ * that does, as `Array.prototype.some` does; an empty array answers `false`.
5602
+ * The callback rules are `map`'s. Capability: none.
5603
+ *
5604
+ * @param predicate called in order, with the element and its index, until it answers `true`.
5605
+ * @returns `true` when some element satisfies `predicate`.
5606
+ */
5607
+ some(predicate: (value: T, index: number) => boolean): boolean;
5608
+ /**
5609
+ * Whether `predicate` answers `true` for every element, stopping at the
5610
+ * first that does not, as `Array.prototype.every` does; an empty array
5611
+ * answers `true`. The callback rules are `map`'s. Capability: none.
5612
+ *
5613
+ * @param predicate called in order, with the element and its index, until it answers `false`.
5614
+ * @returns `true` when no element fails `predicate`.
5615
+ */
5616
+ every(predicate: (value: T, index: number) => boolean): boolean;
5617
+ /**
5618
+ * The index of the first element for which `predicate` answers `true`, or
5619
+ * `-1`, as `Array.prototype.findIndex` gives. The callback rules are
5620
+ * `map`'s. There is no `find`: TypeScript types its answer `T | undefined`
5621
+ * and Nish has no `undefined`, so `xs.find(...)` is refused (NL2697) and
5622
+ * points here. Capability: none.
5623
+ *
5624
+ * @param predicate called in order, with the element and its index, until it answers `true`.
5625
+ * @returns the first such index, or `-1`.
5626
+ */
5627
+ findIndex(predicate: (value: T, index: number) => boolean): number;
5628
+ /**
5629
+ * A copy of the elements from `start` up to `end`, in a new array, with
5630
+ * string `slice`'s rule: the range must satisfy `0 <= start <= end <=
5631
+ * length`, and anything else panics with `slice out of range: [start, end)
5632
+ * of length n` and exits 1, where JavaScript counts a negative index back
5633
+ * from the length and clamps the rest. `start` is `0` and `end` the length
5634
+ * by default; a third argument is refused (NL2699). Under Node the prelude's
5635
+ * `sliceArray` applies the same check. Capability: none.
5636
+ *
5637
+ * @param start the first index copied, `0` by default.
5638
+ * @param end the index after the last copied, the length by default.
5639
+ * @returns a new array of `end - start` elements.
5640
+ */
5641
+ slice(start?: number, end?: number): T[];
5124
5642
  }
5125
5643
 
5126
5644
  /**
@@ -5180,15 +5698,16 @@ interface String {
5180
5698
  slice(start?: number, end?: number): string;
5181
5699
  /**
5182
5700
  * The first byte offset at or after `position` at which `searchString`
5183
- * occurs. `position` is clamped into `[0, s.length]` first, as in
5184
- * JavaScript: a negative start searches from `0`, a start past the end
5185
- * finds nothing, and the empty needle answers the clamped start, so
5186
- * `s.indexOf("")` is `0`, `s.indexOf("", 3)` is `3` and `s.indexOf("", 99)`
5187
- * is `s.length`. A float start converts as JavaScript's `ToIntegerOrInfinity`
5188
- * does (a fraction truncates and `NaN` is `0`), and an unsigned one is
5189
- * clamped from above only, so a `u64` past 2^63 is past the end. The search
5190
- * allocates nothing, so a loop `at = s.indexOf(sub, at + sub.length)`
5191
- * copies nothing. Under Node it is the same call to
5701
+ * occurs. `position` is clamped into `[0, s.length]` first, as in JavaScript:
5702
+ * a negative start searches from `0`, a start past the end finds nothing, and
5703
+ * the empty needle answers the clamped start, so `s.indexOf("")` is `0`,
5704
+ * `s.indexOf("", 3)` is `3` and `s.indexOf("", 99)` is `s.length`. A float
5705
+ * start converts as JavaScript's `ToIntegerOrInfinity` does (a fraction
5706
+ * truncates and `NaN` is `0`), and an unsigned one is clamped from above
5707
+ * only, so a `u64` past 2^63 is past the end.
5708
+ *
5709
+ * The search allocates nothing, so a loop `at = s.indexOf(sub, at +
5710
+ * sub.length)` copies nothing. Under Node it is the same call to
5192
5711
  * `String.prototype.indexOf`, whose answers differ only where every offset
5193
5712
  * does, on text that is not ASCII (NL8001). Capability: none.
5194
5713
  *
@@ -5202,6 +5721,7 @@ interface String {
5202
5721
  * Capability: none.
5203
5722
  *
5204
5723
  * @param searchString the prefix.
5724
+ * @param position declared to match TypeScript's `lib`; `nish` refuses a second argument.
5205
5725
  * @returns whether it is one.
5206
5726
  */
5207
5727
  startsWith(searchString: string, position?: number): boolean;
@@ -5210,11 +5730,16 @@ interface String {
5210
5730
  * `false`. One argument. Capability: none.
5211
5731
  *
5212
5732
  * @param searchString the suffix.
5733
+ * @param endPosition declared to match TypeScript's `lib`; `nish` refuses a second argument.
5213
5734
  * @returns whether it is one.
5214
5735
  */
5215
5736
  endsWith(searchString: string, endPosition?: number): boolean;
5216
5737
  }
5217
5738
 
5739
+ /**
5740
+ * `String.fromCharCode`, `lib.es2022`'s own member of `StringConstructor`,
5741
+ * repeated so that its byte meaning in Nish is documented.
5742
+ */
5218
5743
  interface StringConstructor {
5219
5744
  /**
5220
5745
  * The one-byte string of `c & 0xFF`, the inverse of `charCodeAt`. One
@@ -5230,42 +5755,49 @@ interface StringConstructor {
5230
5755
 
5231
5756
  /**
5232
5757
  * An insertion-ordered map with SameValueZero keys (`-0` is `+0` and `NaN` is
5233
- * `NaN`), a builtin with no import. A key is a `string`, a number of any
5234
- * width in either mode (`f32` and `f64` included), a `boolean`, an `enum` or a
5235
- * class instance (by identity); not an interface, an array, a nullable or a
5236
- * `Result`: "`Point` cannot be the key type of `Map<Point, i32>`: a key is
5237
- * hashed and compared by value, or by identity for a class instance". A `-0`
5238
- * key is stored as `+0` by every insert path, so a walk over `keys()` yields
5239
- * `+0`. A value is anything but `void` and an interface, whose record would be
5240
- * copied into the table rather than shared: "`Point` cannot be the value type
5241
- * of `Map<string, Point>`". `new Map<K, V>()` takes no arguments ("`new
5242
- * Map<string, i32>` takes no arguments in this version"); the type arguments
5243
- * are written out, except that a `const` or `let` annotated with the type
5244
- * takes them from the annotation, and a bare `new Map()` is refused: "`Map` is
5245
- * generic: it must be written with its type arguments". The members are
5246
- * JavaScript's, less `entries` and `forEach` (there is no destructuring, and
5247
- * a method cannot take a function); anything else is refused as if it did not
5248
- * exist. `set` answers the receiver, so calls chain. `keys()` and `values()`
5249
- * are iterators, admitted only as the iterable of a `for...of`: stored,
5250
- * passed, returned or dropped, they are refused (NL2372, NL2373, NL2374,
5251
- * NL2358), and `for (const e of m)` needs `entries()` (NL2375). The walk
5252
- * visits entries in insertion order, skips a deleted key not yet reached,
5253
- * visits a key set during the walk later in the same walk, and keeps a
5254
- * re-set key in its place, as JavaScript's does. At most 2^24 − 1 entries
5255
- * (one fewer than Node's 2^24); one more panics with
5758
+ * `NaN`), a builtin with no import. A key is a `string`, a number of any width
5759
+ * in either mode (`f32` and `f64` included), a `boolean`, an `enum` or a class
5760
+ * instance (by identity); not an interface, an array, a nullable or a `Result`:
5761
+ * "`Point` cannot be the key type of `Map<Point, i32>`: a key is hashed and
5762
+ * compared by value, or by identity for a class instance".
5763
+ *
5764
+ * A `-0` key is stored as `+0` by every insert path, so a walk over `keys()`
5765
+ * yields `+0`. A value is anything but `void` and an interface, whose record
5766
+ * would be copied into the table rather than shared: "`Point` cannot be the
5767
+ * value type of `Map<string, Point>`".
5768
+ *
5769
+ * `new Map<K, V>()` takes no arguments ("`new Map<string, i32>` takes no
5770
+ * arguments in this version"); the type arguments are written out, except that
5771
+ * a `const` or `let` annotated with the type takes them from the annotation,
5772
+ * 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);
5775
+ * anything else is refused as if it did not exist. `set` answers the receiver,
5776
+ * so calls chain.
5777
+ *
5778
+ * `keys()` and `values()` are iterators, admitted only as the iterable of a
5779
+ * `for...of`: stored, passed, returned or dropped, they are refused (NL2372,
5780
+ * NL2373, NL2374, NL2358), and `for (const e of m)` needs `entries()` (NL2375).
5781
+ * The walk visits entries in insertion order, skips a deleted key not yet
5782
+ * reached, visits a key set during the walk later in the same walk, and keeps a
5783
+ * re-set key in its place, as JavaScript's does.
5784
+ *
5785
+ * At most 2^24 − 1 entries (one fewer than Node's 2^24); one more panics with
5256
5786
  * `Map maximum size exceeded`, after first compacting away dead entries (not
5257
- * while a loop walks the table). String keys are hashed with SipHash-1-3
5258
- * under a per-process random key drawn from the system's entropy source
5259
- * before `main`, and an integer, `boolean` or enum key under a per-process
5260
- * random multiply-add-shift; if the entropy source fails the program panics
5261
- * before `main`: "Map: the system's entropy source failed". A float key and a
5262
- * class instance key are not keyed, so a program keying a table by floats a
5263
- * peer chose converts them to a string or a scaled integer first. On wasm32
5264
- * the key comes from WASI's `random_get`, and a reactor module is keyed only
5265
- * once its host calls `_initialize`. A pair of `i32`s is one `i64` key:
5266
- * `(toI64(hi) << toI64(32)) | toI64(toU32(lo))`. `nish/map` adds `reserve(m,
5267
- * n)` and `getOrInsert(m, k, v)`, which JavaScript's `Map` has no call for.
5268
- * A module that declares or imports its own `Map` gets no implicit import.
5787
+ * while a loop walks the table). String keys are hashed with SipHash-1-3 under
5788
+ * a per-process random key drawn from the system's entropy source before
5789
+ * `main`, and an integer, `boolean` or enum key under a per-process random
5790
+ * multiply-add-shift; if the entropy source fails the program panics before
5791
+ * `main`: "Map: the system's entropy source failed".
5792
+ *
5793
+ * A float key and a class instance key are not keyed, so a program keying a
5794
+ * table by floats a peer chose converts them to a string or a scaled integer
5795
+ * first. On wasm32 the key comes from WASI's `random_get`, and a reactor module
5796
+ * is keyed only once its host calls `_initialize`. A pair of `i32`s is one
5797
+ * `i64` key: `(toI64(hi) << toI64(32)) | toI64(toU32(lo))`. `nish/map` adds
5798
+ * `reserve(m, n)` and `getOrInsert(m, k, v)`, which JavaScript's `Map` has no
5799
+ * call for. A module that declares or imports its own `Map` gets no implicit
5800
+ * import.
5269
5801
  */
5270
5802
  interface Map<K, V> {
5271
5803
  /** The entry count, read-only: assigning to it is refused. */
@@ -5273,16 +5805,17 @@ interface Map<K, V> {
5273
5805
  /**
5274
5806
  * The value under `key`, one probe. The `V | undefined` is a maybe, admitted
5275
5807
  * only as the initialiser of a `const` (unannotated, or annotated with the
5276
- * maybe type), the left of `??`, or an operand of `=== undefined` or
5277
- * `!== undefined`. A `const` so bound reads as `V` in the region a test
5278
- * proves it present (`if (a === undefined) return` included), and nowhere
5279
- * else. Any other place is refused with a message naming it: a `let`
5280
- * (NL2361), an argument (NL2362), a `return` (NL2363), a field (NL2364), an
5281
- * array element (NL2365), a template literal hole (NL2366), an operand of
5282
- * another operator (NL2367), the default of another `??` (NL2371), a `const`
5283
- * annotated `V` (NL2368), or anywhere else (NL2369). When `V` is nullable,
5284
- * `??` replaces a stored `null` as well. `get` is called, never read as a
5285
- * value. Capability: none.
5808
+ * maybe type), the left of `??`, or an operand of `=== undefined` or `!==
5809
+ * undefined`. A `const` so bound reads as `V` in the region a test proves it
5810
+ * present (`if (a === undefined) return` included), and nowhere else.
5811
+ *
5812
+ * Any other place is refused with a message naming it: a `let` (NL2361), an
5813
+ * argument (NL2362), a `return` (NL2363), a field (NL2364), an array element
5814
+ * (NL2365), a template literal hole (NL2366), an operand of another operator
5815
+ * (NL2367), the default of another `??` (NL2371), a `const` annotated `V`
5816
+ * (NL2368), or anywhere else (NL2369). When `V` is nullable, `??` replaces a
5817
+ * stored `null` as well. `get` is called, never read as a value. Capability:
5818
+ * none.
5286
5819
  *
5287
5820
  * @param key the key looked up.
5288
5821
  * @returns the value, or `undefined`.