nish 0.42.0 → 0.45.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/README.md +11 -5
  2. package/docs/AI.md +287 -129
  3. package/package.json +9 -9
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/skills/writing-nish/SKILL.md +12 -4
  21. package/std/README.md +6 -6
  22. package/std/array.ts +10 -2
  23. package/std/bytes.ts +239 -26
  24. package/std/cli-args.ts +1072 -606
  25. package/std/cli-flags.ts +180 -98
  26. package/std/cli.ts +21 -13
  27. package/std/collections.ts +245 -127
  28. package/std/compress/checksum.ts +32 -37
  29. package/std/compress/deflate.ts +1102 -542
  30. package/std/compress/gzip.ts +290 -145
  31. package/std/compress/zlib.ts +57 -21
  32. package/std/crypto/aes.ts +153 -75
  33. package/std/crypto/base64url.ts +163 -39
  34. package/std/crypto/bcrypt-batch.ts +164 -90
  35. package/std/crypto/bcrypt.ts +185 -107
  36. package/std/crypto/chacha20poly1305.ts +84 -68
  37. package/std/crypto/ct.ts +9 -7
  38. package/std/crypto/ed25519.ts +64 -27
  39. package/std/crypto/hkdf.ts +105 -63
  40. package/std/crypto/hmac.ts +95 -89
  41. package/std/crypto/jwt.ts +248 -126
  42. package/std/crypto/p256.ts +758 -444
  43. package/std/crypto/p384.ts +20 -13
  44. package/std/crypto/pki-roots.ts +95 -38
  45. package/std/crypto/pki.ts +164 -60
  46. package/std/crypto/rsa.ts +266 -223
  47. package/std/crypto/scram.ts +141 -0
  48. package/std/crypto/sha1.ts +157 -57
  49. package/std/crypto/sha256.ts +502 -422
  50. package/std/crypto/sha512.ts +204 -135
  51. package/std/crypto/x509-random.ts +9 -3
  52. package/std/crypto/x509.ts +331 -171
  53. package/std/host/ring.ts +1 -1
  54. package/std/host/text.ts +1 -1
  55. package/std/json-equal.ts +35 -19
  56. package/std/json-format.ts +215 -108
  57. package/std/json-number.ts +126 -72
  58. package/std/json-pointer.ts +42 -13
  59. package/std/json-tokenizer.ts +498 -307
  60. package/std/json-typed.ts +115 -36
  61. package/std/json-value.ts +341 -152
  62. package/std/json.ts +300 -158
  63. package/std/net/buffer-pool.ts +17 -7
  64. package/std/net/dns-config.ts +427 -236
  65. package/std/net/dns-message.ts +114 -47
  66. package/std/net/dns-resolver.ts +736 -237
  67. package/std/net/dns.ts +205 -52
  68. package/std/net/fetch.ts +320 -184
  69. package/std/net/grpc.ts +948 -0
  70. package/std/net/handler.ts +1276 -218
  71. package/std/net/hpack.ts +395 -180
  72. package/std/net/http-compress.ts +502 -65
  73. package/std/net/http-fields.ts +85 -49
  74. package/std/net/http-server.ts +1764 -603
  75. package/std/net/http1-client-codec.ts +372 -161
  76. package/std/net/http1-codec.ts +665 -331
  77. package/std/net/http1-server.ts +881 -269
  78. package/std/net/http2-cleartext.ts +165 -95
  79. package/std/net/http2-frame.ts +232 -89
  80. package/std/net/http2-tls.ts +42 -12
  81. package/std/net/http2.ts +327 -101
  82. package/std/net/http3-frame.ts +68 -33
  83. package/std/net/http3-server.ts +672 -206
  84. package/std/net/http3.ts +669 -191
  85. package/std/net/oidc.ts +63 -24
  86. package/std/net/outbound.ts +207 -142
  87. package/std/net/postgres-async.ts +1223 -470
  88. package/std/net/postgres-carrier.ts +575 -372
  89. package/std/net/postgres.ts +1521 -632
  90. package/std/net/prefork.ts +254 -98
  91. package/std/net/protobuf.ts +1017 -0
  92. package/std/net/qpack.ts +143 -92
  93. package/std/net/quic-conn-ack.ts +104 -22
  94. package/std/net/quic-conn-cid.ts +66 -26
  95. package/std/net/quic-conn-params.ts +13 -7
  96. package/std/net/quic-datagram.ts +90 -27
  97. package/std/net/quic-frame.ts +324 -121
  98. package/std/net/quic-listener.ts +206 -91
  99. package/std/net/quic-packet.ts +289 -163
  100. package/std/net/quic-recovery.ts +279 -128
  101. package/std/net/quic-stream.ts +466 -167
  102. package/std/net/quic.ts +584 -258
  103. package/std/net/router.ts +450 -99
  104. package/std/net/serve.ts +661 -240
  105. package/std/net/static.ts +1458 -0
  106. package/std/net/tls/client.ts +140 -45
  107. package/std/net/tls/codec.ts +510 -260
  108. package/std/net/tls/record-client.ts +268 -76
  109. package/std/net/tls/record-server.ts +236 -79
  110. package/std/net/tls/record.ts +173 -47
  111. package/std/net/tls/schedule.ts +58 -39
  112. package/std/net/tls/ticket.ts +295 -103
  113. package/std/net/tls/verify.ts +1 -1
  114. package/std/net/tls-tcp.ts +218 -75
  115. package/std/net/tls.ts +260 -152
  116. package/std/net/upstream.ts +283 -86
  117. package/std/net/url.ts +277 -102
  118. package/std/net/websocket.ts +532 -209
  119. package/std/net/webtransport.ts +755 -436
  120. package/std/regex.ts +1659 -829
  121. package/std/sqlite.ts +325 -0
  122. package/std/sync/ring.ts +320 -156
  123. package/std/sync/shm.ts +53 -6
  124. package/std/text.ts +285 -135
  125. package/std/threads.ts +22 -2
package/runtime/nish.d.ts CHANGED
@@ -628,6 +628,36 @@ declare function ctSelect<T extends u32 | u64>(mask: T, a: T, b: T): T;
628
628
  * @returns a mask for `ctSelect`.
629
629
  */
630
630
  declare function ctEq<T extends u32 | u64>(a: T, b: T): T;
631
+ /**
632
+ * The high 64 bits of the 128-bit product of `a` and `b`, the half a `u64`
633
+ * `a * b` drops (WP41). Both operands are `u64`. Any other type is refused as
634
+ * `` `mulHigh` takes u64 operands (convert with toU64), got u32 `` (NL2823):
635
+ * `u32`, `i64`, `number` and a ranged integer included. A bare numeric
636
+ * literal is read as a `u64`, so `mulHigh(x, 3)` works, and one that does not
637
+ * fit is the literal's own refusal. The call takes exactly two arguments, and
638
+ * like every function it is not a value.
639
+ *
640
+ * On x86-64 and AArch64 it is one multiply (`mul`, `mulx` or `umulh`) and
641
+ * calls nothing. wasm32 has no wide multiply, so there it is four 32×32
642
+ * products and their carries, inline. Neither lowering branches on its
643
+ * operands. Under Node it is `(a * b) >> 64n` on BigInts, with the same
644
+ * answer. Capability: none.
645
+ *
646
+ * @param a one factor.
647
+ * @param b the other factor.
648
+ * @returns the top 64 bits of `a * b` computed in 128 bits.
649
+ */
650
+ declare function mulHigh(a: u64, b: u64): u64;
651
+ /**
652
+ * Whether `mulHigh` is one instruction on the build's target (WP41): `true` on
653
+ * x86-64 and AArch64, and `false` on wasm32 and under Node. It is a constant of
654
+ * the build, so a branch on it costs nothing and the path not taken is
655
+ * dropped. Code with a wide-limb algorithm and a narrow-limb one keeps both and
656
+ * picks with it. It takes no arguments. Capability: none.
657
+ *
658
+ * @returns `true` where the 64×64→128 product is native.
659
+ */
660
+ declare function mulHighIsNative(): boolean;
631
661
  /**
632
662
  * Every byte of `bytes` set to zero, in stores neither `-O2` nor `-flto` may
633
663
  * drop as dead (#385): how a key, a secret scalar or another secret is cleared
@@ -712,8 +742,8 @@ declare function isExecutableSync(path: string): boolean;
712
742
  // refusal reads `` `uncheckedGet` is a `nish:unsafe` function and is called only
713
743
  // through an import of it `` (NL2456). An import may rename (`as`) and cannot be
714
744
  // shadowed; a user function of the same name, with no import, is the user's.
715
- // Every import and call is a recorded fact in `--emit-checked`. Under
716
- // `--deny-panics` or `"nish".noPanic` the import is the opt-in the scope takes.
745
+ // Every import and call is a recorded fact in `--emit-checked`. In
746
+ // the no-panic scope, the default, the import is the opt-in the scope takes.
717
747
 
718
748
  /**
719
749
  * `xs[i]` with no bounds check: undefined behaviour when `i` is out of range,
@@ -726,7 +756,7 @@ declare function isExecutableSync(path: string): boolean;
726
756
  *
727
757
  * To the bounds proof it is the load `xs[i]` is, less the check: it moves no
728
758
  * length, and it records no passed check, so a checked `xs[i]` after it keeps
729
- * its check. Under `--deny-panics` it is an allowed `unchecked` site. Under
759
+ * its check. In the no-panic scope it is an allowed `unchecked` site. Under
730
760
  * Node the runtime shim reads `xs[i]`. Capability: unsafe.
731
761
  *
732
762
  * @param xs the array of numbers to read.
@@ -746,7 +776,7 @@ declare function uncheckedGet<T extends number>(xs: readonly T[], i: i32): T;
746
776
  * The store is a write of the array for every rule that asks, the rule for a
747
777
  * parallel body included. To the bounds proof it is the store `xs[i] = v` is,
748
778
  * 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
779
+ * In the no-panic scope it is an allowed `unchecked` site. Under Node the
750
780
  * runtime shim stores `xs[i] = v`. Capability: unsafe.
751
781
  *
752
782
  * @param xs the array of numbers to write.
@@ -766,7 +796,7 @@ declare function uncheckedSet<T extends number>(xs: T[], i: i32, v: T): void;
766
796
  *
767
797
  * In a module constant's initialiser the call folds, wrapping, where
768
798
  * `2147483647 + 1` is refused; it is the one call such an initialiser may make.
769
- * It is not a site under `--deny-panics`, as it cannot panic. Under Node the
799
+ * It is not a site in the no-panic scope, as it cannot panic. Under Node the
770
800
  * runtime shim computes `(a + b) | 0` on an `i32` and `BigInt.asIntN(64, ...)`
771
801
  * on the BigInt an `i64` is there. Capability: unsafe.
772
802
  *
@@ -813,7 +843,7 @@ declare function wrappingMul<T extends i32 | i64>(a: T, b: T): T;
813
843
  * The copy is new on every call and is the calling function's allocation, and
814
844
  * it is refused inside a `using a = arena()` block that would release one held
815
845
  * outside it (`` `readCString(p)` may be allocated inside the `using arena()`
816
- * block ``). Under `--deny-panics` its `NULL` test is an `ffi-null` site no
846
+ * block ``). In the no-panic scope its `NULL` test is an `ffi-null` site no
817
847
  * proof removes, so the scope refuses the call (the copy's `oom` beside it is
818
848
  * allowed), and the pointer it reads is `ffi`. It lowers the same
819
849
  * on wasm32, where whether the program links is the `declare function`'s
@@ -842,7 +872,7 @@ declare function readCString(p: CPtr): string;
842
872
  *
843
873
  * A `NULL` `p` is tested before the copy and panics with the `ffi-null` kind,
844
874
  * naming the layout, since a `declare function` declared to answer `CPtr` may
845
- * still answer `NULL`. No proof removes that test, so `--deny-panics` refuses
875
+ * still answer `NULL`. No proof removes that test, so the no-panic scope refuses
846
876
  * the call; `--emit-panics` lists it as an `ffi-null` site, with the copy's
847
877
  * `oom` beside it.
848
878
  *
@@ -1737,9 +1767,14 @@ declare function netWrite2(
1737
1767
  * Shut one or both directions of a connection. Not callable in a `scope()` task or a parallel body.
1738
1768
  * Refused at compile time on wasm32 and the WASI profile (NL2404). Capability: net.
1739
1769
  *
1770
+ * On a connection a `NET_LISTEN_RING` listener accepted, the shutdown goes behind the
1771
+ * bytes already written, so the peer reads them and then the end of the stream; a later
1772
+ * `netWrite` answers -32 once the write side is shut, and `netRead` answers what is queued
1773
+ * and then 0 once the read side is.
1774
+ *
1740
1775
  * @param fd the socket.
1741
1776
  * @param how the read side (0), the write side (1) or both (2).
1742
- * @returns 0, or a negative errno; -22 for any other `how`.
1777
+ * @returns 0, or a negative errno; -22 for any other `how`, -9 for a closed ring connection.
1743
1778
  */
1744
1779
  declare function netShutdown(fd: i32, how: i32): i32;
1745
1780
  /**
@@ -2006,7 +2041,8 @@ declare function netSetOption(fd: i32, option: i32, value: i32): i32;
2006
2041
  * @param fd the socket.
2007
2042
  * @returns the MSS; -1, never an errno, for anything else (a descriptor that is not open, a
2008
2043
  * socket with no peer such as a listener or one never connected, though the kernel answers
2009
- * its default `536` for those, a socket that is not TCP), and always on wasm.
2044
+ * its default `536` for those, a socket that is not TCP, a connection a `NET_LISTEN_RING`
2045
+ * listener accepted, whose ring cannot ask for it), and always on wasm.
2010
2046
  */
2011
2047
  declare function netMss(fd: i32): i32;
2012
2048
  /**
@@ -3246,11 +3282,14 @@ declare module "nish:net" {
3246
3282
  bLen: i32
3247
3283
  ): i32;
3248
3284
  /**
3249
- * Shut one side of a connection, or both.
3285
+ * Shut one side of a connection, or both. On a connection a
3286
+ * `NET_LISTEN_RING` listener accepted, the shutdown goes behind the bytes
3287
+ * already written, so the peer reads them and then the end of the stream.
3250
3288
  *
3251
3289
  * @param fd A connected socket.
3252
3290
  * @param how `0` the read side, `1` the write side, `2` both.
3253
- * @returns `0`, `-22` for any other `how`, or the failure.
3291
+ * @returns `0`, `-22` for any other `how`, `-9` for a closed ring
3292
+ * connection, or the failure.
3254
3293
  */
3255
3294
  export function netShutdown(fd: i32, how: i32): i32;
3256
3295
  /**
@@ -3445,8 +3484,9 @@ declare module "nish:net" {
3445
3484
  * @returns The size, or a plain `-1`, never an errno, for anything else: a
3446
3485
  * descriptor that is not open, a socket with no peer (a listener, or one
3447
3486
  * never connected, though the kernel answers its default `536` for
3448
- * those), a socket that is not TCP, and every call on wasm, where it
3449
- * compiles to the constant with the descriptor still evaluated. It throws
3487
+ * those), a socket that is not TCP, a connection a `NET_LISTEN_RING`
3488
+ * listener accepted, whose ring cannot ask for it, and every call on
3489
+ * wasm, where it compiles to the constant with the descriptor still evaluated. It throws
3450
3490
  * under Node.
3451
3491
  */
3452
3492
  export function netMss(fd: i32): i32;
@@ -3642,7 +3682,7 @@ declare module "nish:net" {
3642
3682
  * `(a - b) | 0` and `Math.imul` on an `i32` and `BigInt.asIntN(64, …)` on an
3643
3683
  * `i64`. A program that stays in range prints the same as natively.
3644
3684
  *
3645
- * **The no-panic scope.** Under `--deny-panics` or `"nish".noPanic`,
3685
+ * **The no-panic scope.** In the scope, every module of the package by default,
3646
3686
  * `uncheckedGet` and `uncheckedSet` are allowed `unchecked` sites, because
3647
3687
  * the import has already said that an index out of range is the author's to
3648
3688
  * rule out. `readCString` and `readStruct` are not: each tests its pointer and
@@ -3742,6 +3782,52 @@ declare module "nish:unsafe" {
3742
3782
  export function cErrno(): i32;
3743
3783
  }
3744
3784
 
3785
+ /**
3786
+ * `nish:compact`: the marker interfaces that pick a class's layout (WP48).
3787
+ * `class R implements Compact` stores every field typed exactly `R` or
3788
+ * `R | null`, in any class, as a 32-bit offset into one reserved address range,
3789
+ * so a tree or list node of an `i32` and two links is 12 bytes, not 24. Locals,
3790
+ * parameters, returns, arrays, maps and other unions stay full pointers.
3791
+ *
3792
+ * A native `--link` build also compacts, with no marker, each class whose
3793
+ * links take at least a quarter off a class made in a loop or recursion
3794
+ * (`--explain-layout` prints each pick and why the others are left alone).
3795
+ * `implements Wide` keeps one class out of that, and `--compact off` keeps
3796
+ * every class, `implements Compact` included, at full pointers.
3797
+ *
3798
+ * A `--emit-header` build keeps a compact class, and every class holding one
3799
+ * of its links, opaque: C holds it as a full `struct X *` handle and links the
3800
+ * range runtime (`scripts/build.sh --arena-range`). The compiler refuses a
3801
+ * compact class where Node or the runtime would read its layout: a build with
3802
+ * `--emit-napi` or `--emit-napi-async` (also beside `--emit-header`), an
3803
+ * N-API export whose signature holds one, a `declare class`, and a class
3804
+ * holding a `Secret`. wasm32, a host `--emit-dts` page module and an ASAN build compile
3805
+ * it uncompressed: a pointer there is already 4 bytes, the page runs on the
3806
+ * wasm runtime, or the range cannot hold the sanitizer's blocks.
3807
+ *
3808
+ * **Under Node.** `runtime/nish.mjs` answers the import with
3809
+ * `runtime/core.mjs`, where both names are empty values: a reference is the
3810
+ * engine's own and the marker changes nothing a program prints.
3811
+ *
3812
+ * Rules: https://github.com/amritk/nish/blob/main/docs/LANGUAGE.md#compact-classes
3813
+ */
3814
+ declare module "nish:compact" {
3815
+ /**
3816
+ * The marker a class implements to store its links as 32-bit offsets. It
3817
+ * has no members, so `implements Compact` adds no field.
3818
+ */
3819
+ export interface Compact {}
3820
+
3821
+ /**
3822
+ * The marker a class implements to keep its links full pointers. The
3823
+ * compiler compacts a class on its own when its rule finds the saving pays
3824
+ * (`--explain-layout` names each pick); `implements Wide` refuses that
3825
+ * pick, the way `--compact off` refuses every one. It has no members, so it
3826
+ * adds no field, and a class cannot write it with `Compact`.
3827
+ */
3828
+ export interface Wide {}
3829
+ }
3830
+
3745
3831
  /**
3746
3832
  * Sixteen-byte vectors of one lane type, and the lane-wise operations on them,
3747
3833
  * each lowered to target-independent LLVM vector IR.
@@ -4999,15 +5085,15 @@ declare module "nish:region" {
4999
5085
  *
5000
5086
  * import { awaitAll, spawn } from "nish:async";
5001
5087
  *
5002
- * async function double(x: i32): Promise<i32> {
5088
+ * const double = async (x: i32): Promise<i32> => {
5003
5089
  * return x * 2;
5004
5090
  * }
5005
5091
  *
5006
- * async function log(x: i32): Promise<void> {
5092
+ * const log = async (x: i32): Promise<void> => {
5007
5093
  * console.log(x);
5008
5094
  * }
5009
5095
  *
5010
- * async function run(): Promise<void> {
5096
+ * const run = async (): Promise<void> => {
5011
5097
  * const [c, d] = await awaitAll(double(1), double(2));
5012
5098
  * spawn(log(c + d));
5013
5099
  * }
@@ -5024,7 +5110,7 @@ declare module "nish:region" {
5024
5110
  *
5025
5111
  * import { readable, sleep } from "nish:async";
5026
5112
  *
5027
- * async function tick(): Promise<void> {
5113
+ * const tick = async (): Promise<void> => {
5028
5114
  * await sleep(10);
5029
5115
  * console.log("tick");
5030
5116
  * }
@@ -5272,6 +5358,18 @@ declare module "nish:async" {
5272
5358
  * (or removed); `false` for a task that has finished or is `main`.
5273
5359
  */
5274
5360
  export function cancelAfter(task: i64, ms: i32): boolean;
5361
+ /**
5362
+ * Read `view`, an object or array its spawner owns, in place, without
5363
+ * copying it into the task's region. Inside a spawned task the view, and
5364
+ * what is built from it, is lent only while the task runs: the task may
5365
+ * read it, write numbers into it and copy out of it, but may not store a
5366
+ * pointer into it, grow it, or keep it, its parts or what holds it past its
5367
+ * end (NL2822). Outside a task, and under `--emit-ts`, it is the identity.
5368
+ *
5369
+ * @param view The spawner's object or array the task reads.
5370
+ * @returns `view` itself.
5371
+ */
5372
+ export function lend<T>(view: T): T;
5275
5373
  }
5276
5374
 
5277
5375
  // ---- Arena (docs/LANGUAGE.md -> Arena) ---------------------------------------
@@ -5871,6 +5969,22 @@ interface Array<T> {
5871
5969
  * @returns the first such index, or `-1`.
5872
5970
  */
5873
5971
  findIndex(predicate: (value: T, index: number) => boolean): number;
5972
+ /**
5973
+ * Call `callbackfn` with each element in order, as `Array.prototype.forEach`
5974
+ * does, up to the length the array had when the call began: an element the
5975
+ * callback pushes is not visited, and one that shrinks the array ends the
5976
+ * walk at its new length. The call answers `void`, and so does the
5977
+ * callback; what a concise arrow body computes is dropped. The callback takes the element, or the element and its
5978
+ * index; one that declares a third parameter, JavaScript's array, is refused
5979
+ * (NL2698). It is `map`'s callback otherwise: a top-level function or an arrow
5980
+ * written at the call, which may not assign a name of the function around it
5981
+ * (NL2586), so a running total is a `for...of` loop. The call compiles to a
5982
+ * counted loop with the callback inlined (`std/array`'s `forEach`, or
5983
+ * `forEachIndexed` for a two-parameter callback). Capability: none.
5984
+ *
5985
+ * @param callbackfn called once per element, with the element and its index.
5986
+ */
5987
+ forEach(callbackfn: (value: T, index: number) => void): void;
5874
5988
  /**
5875
5989
  * A copy of the elements from `start` up to `end`, in a new array, with
5876
5990
  * string `slice`'s rule: the range must satisfy `0 <= start <= end <=
@@ -5895,7 +6009,7 @@ interface Array<T> {
5895
6009
  * nish_array *`.
5896
6010
  *
5897
6011
  * Everything that reads works (`length`, `a[i]`, `for...of`, `indexOf`, `join`,
5898
- * `map`, `filter`, `some`, `every`, `findIndex`, `slice`, passing it on to
6012
+ * `map`, `filter`, `some`, `every`, `findIndex`, `forEach`, `slice`, passing it on to
5899
6013
  * another `readonly T[]`); every write is refused under its own name, an
5900
6014
  * element store ("Cannot assign to an element of readonly i32[]
5901
6015
  * (declare it i32[] to write through it)"), `push`, `pop`, `set`, `fill` and
@@ -5981,6 +6095,22 @@ interface ReadonlyArray<T> {
5981
6095
  * @returns the first such index, or `-1`.
5982
6096
  */
5983
6097
  findIndex(predicate: (value: T, index: number) => boolean): number;
6098
+ /**
6099
+ * Call `callbackfn` with each element in order, as `Array.prototype.forEach`
6100
+ * does, up to the length the array had when the call began: an element the
6101
+ * callback pushes is not visited, and one that shrinks the array ends the
6102
+ * walk at its new length. The call answers `void`, and so does the
6103
+ * callback; what a concise arrow body computes is dropped. The callback takes the element, or the element and its
6104
+ * index; one that declares a third parameter, JavaScript's array, is refused
6105
+ * (NL2698). It is `map`'s callback otherwise: a top-level function or an arrow
6106
+ * written at the call, which may not assign a name of the function around it
6107
+ * (NL2586), so a running total is a `for...of` loop. The call compiles to a
6108
+ * counted loop with the callback inlined (`std/array`'s `forEach`, or
6109
+ * `forEachIndexed` for a two-parameter callback). Capability: none.
6110
+ *
6111
+ * @param callbackfn called once per element, with the element and its index.
6112
+ */
6113
+ forEach(callbackfn: (value: T, index: number) => void): void;
5984
6114
  /**
5985
6115
  * A copy of the elements from `start` up to `end`, in a new array, with
5986
6116
  * string `slice`'s rule: the range must satisfy `0 <= start <= end <=
@@ -6139,9 +6269,9 @@ interface StringConstructor {
6139
6269
  * reached, visits a key set during the walk later in the same walk, and keeps a
6140
6270
  * re-set key in its place, as JavaScript's does.
6141
6271
  *
6142
- * At most 2^24 − 1 entries (one fewer than Node's 2^24); one more panics with
6143
- * `Map maximum size exceeded`, after first compacting away dead entries (not
6144
- * while a loop walks the table). String keys are hashed with SipHash-1-3 under
6272
+ * No entry cap of its own: past 2^29 entries the bucket table would outgrow
6273
+ * the largest array, and the program stops as out of memory, as any
6274
+ * allocation that size does (Node's `Map` throws at 2^24). String keys are hashed with SipHash-1-3 under
6145
6275
  * a per-process random key drawn from the system's entropy source before
6146
6276
  * `main`, and an integer, `boolean` or enum key under a per-process random
6147
6277
  * multiply-add-shift; if the entropy source fails the program panics before
@@ -6213,8 +6343,8 @@ interface Map<K, V> {
6213
6343
  * `new Set<T>()` takes no arguments ("`new Set<string>` takes no arguments in
6214
6344
  * this version"). `keys()` and `values()` are one walk, as JavaScript's are,
6215
6345
  * admitted only as the iterable of a `for...of`, which also takes the set
6216
- * itself: `for (const x of s)`. At most 2^24 − 1 members; one more panics with
6217
- * `Set maximum size exceeded`. Hashing, the entropy source and the walk's
6346
+ * itself: `for (const x of s)`. Its size is bounded as a `Map`'s is, by
6347
+ * memory alone. Hashing, the entropy source and the walk's
6218
6348
  * rules are `Map`'s. `add` and `has` are one probe each, and `if (!s.has(x)) {
6219
6349
  * s.add(x); ... }` with the `add` first in the branch asks about the member
6220
6350
  * once.
@@ -6247,6 +6377,19 @@ interface Set<T> {
6247
6377
  delete(value: T): boolean;
6248
6378
  /** Remove every member. Capability: none. */
6249
6379
  clear(): void;
6380
+ /**
6381
+ * Call `callbackfn` with each member in insertion order, as
6382
+ * `Set.prototype.forEach` does: the `for...of` walk over the set with the
6383
+ * callback inlined (`std/collections`' `forEachElement`), so a member added
6384
+ * during the walk is visited and one deleted before the walk reaches it is
6385
+ * not. The callback takes the member alone; JavaScript passes it again as a
6386
+ * second argument and the set as a third, and a callback that declares a
6387
+ * second parameter is refused (NL2772). The callback rules are those of an
6388
+ * array's `forEach`. Capability: none.
6389
+ *
6390
+ * @param callbackfn called once per member, with the member.
6391
+ */
6392
+ forEach(callbackfn: (value: T) => void): void;
6250
6393
  }
6251
6394
 
6252
6395
  // ---- What this file cannot say ----------------------------------------------
package/runtime/nish.mjs CHANGED
@@ -251,6 +251,10 @@ provide("f32ToBits", shim.f32ToBits);
251
251
  // agree with a native run; the timing does not, and is not claimed here.
252
252
  provide("ctSelect", shim.ctSelect);
253
253
  provide("ctEq", shim.ctEq);
254
+ // `mulHigh` (WP41): exact on BigInts. `mulHighIsNative` is `false`, so std
255
+ // takes its narrow-limb path here.
256
+ provide("mulHigh", shim.mulHigh);
257
+ provide("mulHighIsNative", shim.mulHighIsNative);
254
258
  // `secureZero` (#385): the bytes are zero afterwards here too, though what a
255
259
  // collector may have copied before is out of reach.
256
260
  provide("secureZero", shim.secureZero);
@@ -345,6 +349,8 @@ provide("arena", shim.arena);
345
349
  // `nish:simd` is `runtime/core.mjs`, whose vectors are typed arrays made once
346
350
  // and never written after, so `--emit-ts` output and a program run as it was
347
351
  // written share one implementation.
352
+ // `nish:compact` is `runtime/core.mjs` too: its two marker interfaces pick a
353
+ // native layout and are empty values under Node.
348
354
  const UNSAFE_EXPORTS = ["uncheckedGet", "uncheckedSet", "wrappingAdd", "wrappingSub", "wrappingMul"];
349
355
  const unsafeSource = UNSAFE_EXPORTS.map((name) => `export const ${name} = globalThis.${name};`).join("\n");
350
356
  // `nish:region` has no allocator to swap under Node: a region is an entry in a
@@ -442,7 +448,7 @@ registerHooks({
442
448
  if (specifier === "nish:unsafe") {
443
449
  return { url: `data:text/javascript,${encodeURIComponent(unsafeSource)}`, shortCircuit: true };
444
450
  }
445
- if (specifier === "nish:simd") {
451
+ if (specifier === "nish:simd" || specifier === "nish:compact") {
446
452
  return next(new URL("./core.mjs", import.meta.url).href, context);
447
453
  }
448
454
  if (specifier === "nish:async") {
@@ -216,6 +216,8 @@ void *nish_cross_keep(void *v, uint64_t esize, uint64_t mark) {
216
216
  if (!x) {
217
217
  v = nish_cross_copy(v, esize);
218
218
  } else {
219
+ /* `x->off` needs no realigning under NISH_ARENA_RANGE: only this
220
+ code bumps a transfer block, by `nish_cross_size`'s 8-byte multiples. */
219
221
  uint64_t n = nish_cross_size(v, esize);
220
222
  if (!x->block || x->block->cap - x->off < n) {
221
223
  uint64_t want = x->block && x->block->cap < 32768 ? x->block->cap * 2 : x->block ? 65536 : 4096;
@@ -243,7 +245,14 @@ void *nish_cross_in(void *p, uint64_t esize) {
243
245
  void *v = (void *)((uintptr_t)p & ~(uintptr_t)1);
244
246
  nish_xchunk *c = (nish_xchunk *)v - 1;
245
247
  uint64_t n = c->cap;
248
+ #ifdef NISH_ARENA_RANGE
249
+ /* What `nish_alloc_struct` would have left: it starts at `off` rounded up
250
+ to 8 (runtime.c gives the reason). */
251
+ uint64_t at = (nish_arena.off + 7) & ~(uint64_t)7;
252
+ if (n >= 65536 && (at > nish_arena.cap || n > nish_arena.cap - at)) {
253
+ #else
246
254
  if (n >= 65536 && n > nish_arena.cap - nish_arena.off) {
255
+ #endif
247
256
  nish_cross_link(c);
248
257
  return v;
249
258
  }
@@ -250,10 +250,13 @@ int32_t nish_net_address(nish_array *out, const nish_str *host, int32_t port) {
250
250
  keeps every handle below 2^31; their ids are not reused, so a closed
251
251
  listener's handles answer -9 for good.
252
252
 
253
- Every call that takes a handle and is not one of the slot paths here
254
- (`sendFd`, `netShutdown`, `connectResult`, `peerCredentials`, ...) passes it
255
- to the kernel as a descriptor, which no process holds at 2^20 or above, so
256
- it answers -9 (EBADF) with nothing to check.
253
+ `netShutdown` queues an io_uring SHUTDOWN behind the slot's sends, so the
254
+ peer reads every byte written before it and then the FIN (NURING-39);
255
+ `netMss` answers -1, its "not known", for a slot. Every other call that
256
+ takes a handle and is not one of the slot paths here (`sendFd`,
257
+ `connectResult`, `peerCredentials`, ...) passes it to the kernel as a
258
+ descriptor, which no process holds at 2^20 or above, so it answers -9
259
+ (EBADF) with nothing to check.
257
260
 
258
261
  Readiness keeps the descriptor contract: a read with nothing queued, a write
259
262
  to a full send area and an accept with nothing accepted answer -11, and the
@@ -291,6 +294,7 @@ int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms);
291
294
  int32_t nish_uring_wait(struct nish_uring_set *s, int32_t timeout_ms, struct nish_uring_event *ev, int32_t cap);
292
295
  int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n);
293
296
  int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot);
297
+ int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t how);
294
298
  int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t slot);
295
299
  int32_t nish_uring_full(const struct nish_uring_set *s);
296
300
  int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on);
@@ -305,6 +309,9 @@ void nish_uring_destroy(struct nish_uring_set *s);
305
309
  /* What a held slot's queue may still take from the kernel before the cancel
306
310
  lands; past it the connection ends with -105 (ENOBUFS). */
307
311
  #define NR_RECV_MAX (16 * 1024 * 1024)
312
+ /* A slot's first receive queue, doubled as it fills: small enough for
313
+ glibc's thread cache, and freed once drained (NURING-38). */
314
+ #define NR_RECV_FIRST 1024
308
315
  /* The most slots per ring: a table is the kernel's memory, sized up front. */
309
316
  #define NR_SLOTS_MAX 16384
310
317
  #define NR_EVENTS 256
@@ -320,11 +327,12 @@ struct nr_vent {
320
327
 
321
328
  /* One slot's connection. `gen` counts the slot's closes; `lingering` is a
322
329
  close the program made while bytes were still queued, which ends when they
323
- are sent, the peer goes or a send fails. */
330
+ are sent, the peer goes or a send fails. `wshut` is a shutdown of the
331
+ write side, after which a write answers -32 as send's EPIPE would. */
324
332
  struct nr_conn {
325
333
  struct nr_vent v;
326
334
  uint32_t gen;
327
- uint8_t open, lingering, hup, wblocked, rheld;
335
+ uint8_t open, lingering, hup, wblocked, rheld, wshut;
328
336
  int32_t hup_res, werr;
329
337
  uint8_t *rbuf;
330
338
  uint32_t rlen, roff, rcap;
@@ -471,13 +479,10 @@ static void nr_dispatch(struct nr_set *S, const struct nish_uring_event *e) {
471
479
  }
472
480
  struct nr_conn *c = &S->conns[e->ring][e->slot];
473
481
  if (e->kind == 1) {
482
+ /* nr_close freed the queue of a slot reused here (NURING-38). */
474
483
  uint32_t gen = c->gen;
475
- uint8_t *buf = c->rbuf;
476
- uint32_t cap = c->rcap;
477
484
  memset(c, 0, sizeof *c);
478
485
  c->gen = gen;
479
- c->rbuf = buf;
480
- c->rcap = cap;
481
486
  c->open = 1;
482
487
  c->v.loop = -1;
483
488
  uint32_t i = (S->fhead + S->flen) % (S->fcap ? S->fcap : 1);
@@ -507,7 +512,6 @@ static void nr_dispatch(struct nr_set *S, const struct nish_uring_event *e) {
507
512
  so a read never runs past the failure it reports. */
508
513
  if (c->lingering || c->hup || e->res <= 0) return;
509
514
  uint32_t n = (uint32_t)e->res;
510
- if (c->roff == c->rlen) c->roff = c->rlen = 0;
511
515
  if ((uint64_t)(c->rlen - c->roff) + n > NR_RECV_MAX) {
512
516
  c->hup = 1;
513
517
  c->hup_res = -105;
@@ -521,7 +525,7 @@ static void nr_dispatch(struct nr_set *S, const struct nish_uring_event *e) {
521
525
  c->roff = 0;
522
526
  }
523
527
  if (c->rlen + n > c->rcap) {
524
- uint32_t nc = c->rcap ? c->rcap : 4096;
528
+ uint32_t nc = c->rcap ? c->rcap : NR_RECV_FIRST;
525
529
  while (nc < c->rlen + n) nc *= 2;
526
530
  uint8_t *p = realloc(c->rbuf, nc);
527
531
  if (!p) {
@@ -712,6 +716,13 @@ static int32_t nr_read(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_
712
716
  c->roff += n;
713
717
  /* A held receive goes again once the program has read half the queue. */
714
718
  if (c->rheld && c->rlen - c->roff < NR_AREA / 2 && nish_uring_hold(S->u, ring, slot, 0) == 0) c->rheld = 0;
719
+ /* A drained queue is freed and emptied, so an idle slot holds none
720
+ (NURING-38) and a receive never finds roff at rlen past 0. */
721
+ if (c->roff == c->rlen) {
722
+ free(c->rbuf);
723
+ c->rbuf = NULL;
724
+ c->rcap = c->roff = c->rlen = 0;
725
+ }
715
726
  return (int32_t)n;
716
727
  }
717
728
  if (c->hup) return c->hup_res < 0 ? c->hup_res : 0;
@@ -730,6 +741,7 @@ static int32_t nr_fail_code(const struct nr_conn *c) {
730
741
  queue. -11 when it has none, or the connection's errno once it has failed. */
731
742
  static int32_t nr_send(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_t slot, const char *p, int64_t len) {
732
743
  if (nr_failed(c)) return nr_fail_code(c);
744
+ if (c->wshut) return -32;
733
745
  int64_t q = nish_uring_queued(S->u, ring, slot);
734
746
  if (q < 0) return -9;
735
747
  if (q >= NR_AREA) {
@@ -748,6 +760,22 @@ static int32_t nr_send(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_
748
760
  return len > 0 && n == 0 ? -11 : n;
749
761
  }
750
762
 
763
+ /* Half-closes a slot (NURING-39). The SHUTDOWN goes behind the queued sends,
764
+ so the peer reads them and then the FIN. A shut read side reads as the
765
+ peer's end: what is queued, then 0, with later bytes dropped. */
766
+ static int32_t nr_shutdown(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_t slot, int32_t how) {
767
+ if (nr_failed(c)) return -107;
768
+ int32_t r = nish_uring_shutdown(S->u, ring, slot, how);
769
+ if (r < 0) return r;
770
+ if (how != 0) c->wshut = 1;
771
+ if (how != 1 && !c->hup) {
772
+ c->hup = 1;
773
+ c->hup_res = 0;
774
+ nr_touch(&c->v);
775
+ }
776
+ return 0;
777
+ }
778
+
751
779
  static int32_t nr_close(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_t slot) {
752
780
  nr_unregister(&c->v);
753
781
  c->gen++;
@@ -1290,6 +1318,11 @@ int32_t nish_net_write2(int32_t fd, const nish_array *a, int64_t aoff, int64_t a
1290
1318
  are `SHUT_RD`, `SHUT_WR` and `SHUT_RDWR` on both platforms. */
1291
1319
  int32_t nish_net_shutdown(int32_t fd, int32_t how) {
1292
1320
  if ((uint32_t)how > 2) return -22;
1321
+ #if defined(__linux__)
1322
+ if (fd >= NR_BASE) { struct nr_set *S; int32_t ring, slot;
1323
+ struct nr_conn *c = nr_decode(fd, &S, &ring, &slot);
1324
+ return c ? nr_shutdown(S, c, ring, slot, how) : -9; }
1325
+ #endif
1293
1326
  return shutdown(fd, how) != 0 ? nish_net_fail() : 0;
1294
1327
  }
1295
1328
 
@@ -1363,8 +1396,13 @@ int32_t nish_net_set_option(int32_t fd, int32_t option, int32_t value) {
1363
1396
  options below. Linux's `TCP_INFO` answers the current `tcpi_snd_mss`; the
1364
1397
  kernel copies at most the struct it has and says how much, so an answer
1365
1398
  too short to reach the field is not read. `TCP_MAXSEG` is the fallback,
1366
- and Darwin's only reading. */
1399
+ and Darwin's only reading. A ring slot is -1 too (NURING-39): its socket
1400
+ is a fixed file with no descriptor to ask, and io_uring's getsockopt
1401
+ command reads only SOL_SOCKET options, not TCP's. */
1367
1402
  int32_t nish_net_mss(int32_t fd) {
1403
+ #if defined(__linux__)
1404
+ if (fd >= NR_BASE) return -1;
1405
+ #endif
1368
1406
  struct sockaddr_storage peer;
1369
1407
  socklen_t len = sizeof peer;
1370
1408
  if (getpeername(fd, (struct sockaddr *)&peer, &len) != 0) return -1;
@@ -92,6 +92,10 @@ extern NISH_TLS uint64_t nish_region_chunk;
92
92
  #ifdef NISH_ARENA_DEBUG
93
93
  /* runtime.c's: retire a debug arena chunk, or free one C code malloc'd. */
94
94
  void nish_debug_drop(void *c);
95
+ #elif defined NISH_ARENA_RANGE && !defined NISH_ASAN
96
+ /* runtime.c's: put a compact-range chunk on its class's free list, or free
97
+ one malloc made. */
98
+ void nish_range_drop(void *c);
95
99
  #endif
96
100
 
97
101
  #define NISH_REGION_SLOT_BITS 24
@@ -386,6 +390,8 @@ void nish_region_adopt(uint64_t r) {
386
390
  nish_chunk_link *next = head->next;
387
391
  #ifdef NISH_ARENA_DEBUG
388
392
  if (!(head->cap & NISH_CHUNK_CARVED)) nish_debug_drop(head);
393
+ #elif defined NISH_ARENA_RANGE && !defined NISH_ASAN
394
+ if (!(head->cap & NISH_CHUNK_CARVED)) nish_range_drop(head);
389
395
  #else
390
396
  if (!(head->cap & NISH_CHUNK_CARVED)) free(head);
391
397
  #endif
@@ -397,6 +403,13 @@ void nish_region_adopt(uint64_t r) {
397
403
  nish_chunk_link *last = head;
398
404
  while (last->next) last = last->next;
399
405
  struct nish_arena caller = nish_arena;
406
+ #ifdef NISH_ARENA_RANGE
407
+ /* A compact class's 4-byte bump may leave `off` 4 past an 8-byte
408
+ boundary (runtime.c's `nish_alloc_struct`): the carved chunk's header,
409
+ and every block bumped from it, start at the next one. */
410
+ caller.off = (caller.off + 7) & ~(uint64_t)7;
411
+ if (caller.off > caller.cap) caller.off = caller.cap;
412
+ #endif
400
413
  uint64_t left = caller.buf ? caller.cap - caller.off : 0;
401
414
  last->next = caller.chunks;
402
415
  nish_arena = s->arena;
@@ -70,11 +70,22 @@ extern NISH_TEXT_TLS struct nish_arena nish_arena;
70
70
  void *nish_arena_grow(uint64_t size);
71
71
  static inline void *nish_text_alloc(uint64_t size) {
72
72
  size = (size + 7) & ~(uint64_t)7;
73
+ #ifdef NISH_ARENA_RANGE
74
+ /* runtime.c's `nish_alloc_struct` gives the reason: `off` may be 4 bytes
75
+ past an 8-byte boundary once a compact class has been bumped. */
76
+ uint64_t at = (nish_arena.off + 7) & ~(uint64_t)7;
77
+ if (at + size <= nish_arena.cap) {
78
+ void *p = nish_arena.buf + at;
79
+ nish_arena.off = at + size;
80
+ return p;
81
+ }
82
+ #else
73
83
  if (nish_arena.off + size <= nish_arena.cap) {
74
84
  void *p = nish_arena.buf + nish_arena.off;
75
85
  nish_arena.off += size;
76
86
  return p;
77
87
  }
88
+ #endif
78
89
  return nish_arena_grow(size);
79
90
  }
80
91
  #else