nish 0.40.1 → 0.42.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.
package/README.md CHANGED
@@ -13,6 +13,8 @@
13
13
  ![WebAssembly](https://img.shields.io/badge/wasm-wasm32%20%C2%B7%20wasi-654ff0?style=flat-square&logo=webassembly&logoColor=white)
14
14
  ![vibe coded](https://img.shields.io/badge/vibe-coded-a855f7?style=flat-square)
15
15
 
16
+ **[nishlang.com](https://nishlang.com/)** · [Get started](https://nishlang.com/getting-started/installation) · [Language reference](https://nishlang.com/reference/language) · [Changelog](https://nishlang.com/reference/changelog)
17
+
16
18
  </div>
17
19
 
18
20
  ---
@@ -252,6 +254,10 @@ has the playground.
252
254
 
253
255
  ## Documentation
254
256
 
257
+ The documentation site, **[nishlang.com](https://nishlang.com/)**, publishes
258
+ the user guide, the language and library reference and the changelog; the
259
+ files below are its sources.
260
+
255
261
  | Read | For |
256
262
  |:---|:---|
257
263
  | [The user guide](docs/guide/index.md) | Learning Nish page by page, every example compiled by the test suite |
@@ -0,0 +1,233 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * The `nish-skills` command: review the agent skills of every installed Nish
4
+ * package and link the ones the user accepts into the project's
5
+ * `.claude/skills/`.
6
+ *
7
+ * A skill can grant an agent tools, so installing a package never links one:
8
+ * this command does, and only for a skill accepted at its current hash -- a
9
+ * `y` at the prompt on a terminal, its name on the command line, or `--yes`
10
+ * for a skill that grants nothing. The rules -- which packages qualify, what
11
+ * is hashed and recorded, which entries the linker owns, what it prunes -- are
12
+ * `scripts/link-skills.mjs`'s.
13
+ *
14
+ * A separate node bin rather than a `nish` subcommand on purpose: it is
15
+ * install tooling, and the compiler stays out of it.
16
+ *
17
+ * Exit 0 when it ran, even if some skills were not linked; 2 for a usage
18
+ * error, an unknown skill name, or when no project is found; 1 when a write
19
+ * failed; 130 when Ctrl-C interrupts a question, and then nothing is written.
20
+ * `--help` is usage on stdout and exit 0, whatever else is on the line, the
21
+ * contract `nish --help` keeps.
22
+ */
23
+ import fs from "node:fs"
24
+ import path from "node:path"
25
+ import readline from "node:readline"
26
+ import { clean, projectOf, review } from "../scripts/link-skills.mjs"
27
+
28
+ const USAGE = `Usage: nish-skills [--dir <dir>] [--yes] [<skill>...]
29
+
30
+ Review the agent skills of every installed Nish package and link the ones you
31
+ accept into <dir>/.claude/skills/. A skill is linked only once accepted, and
32
+ accepted again whenever its files change: the acceptance is recorded with a
33
+ hash of the skill in <dir>/.claude/skills/.nish-skills.json.
34
+
35
+ On a terminal, with no <skill> and no --yes, each skill not yet accepted is
36
+ shown with what it grants and asked about; only y accepts. Otherwise nothing is
37
+ asked: a named <skill> is accepted, --yes accepts every skill that grants
38
+ nothing, and a skill that grants anything (tools, hooks, a shell command run at
39
+ load, or a frontmatter key not known to be inert) is accepted only by name.
40
+ Ctrl-C at a question exits 130 and writes nothing.
41
+
42
+ <dir> defaults to the project this copy of nish is installed in: the current
43
+ directory or the nearest ancestor holding a package.json whose node_modules/nish
44
+ is this copy (a workspace's root, run from a member), else the nearest ancestor
45
+ holding a package.json. Only links that resolve inside <dir>/node_modules/ are
46
+ ever replaced or removed; anything else is left alone.
47
+
48
+ Options:
49
+ --dir <dir> the project to link skills into
50
+ -y, --yes accept every skill that grants nothing
51
+ -h, --help print this and exit
52
+
53
+ Exit status: 0 ran (some skills may not be linked), 1 a write failed,
54
+ 2 usage error, unknown skill, or no project, 130 interrupted.
55
+ `
56
+
57
+ /** Print a usage error and set exit 2. */
58
+ const usageError = (message) => {
59
+ process.stderr.write(`nish-skills: ${message}\n${USAGE}`)
60
+ process.exitCode = 2
61
+ }
62
+
63
+ /** This package's root: the directory holding `bin/` and `scripts/`. */
64
+ const root = path.resolve(import.meta.dirname, "..")
65
+
66
+ /** The nearest directory from `start` upwards that holds a `package.json`, or null. */
67
+ const findProject = (start) => {
68
+ for (let dir = path.resolve(start); ; dir = path.dirname(dir)) {
69
+ if (fs.existsSync(path.join(dir, "package.json"))) {
70
+ return dir
71
+ }
72
+ if (path.dirname(dir) === dir) {
73
+ return null
74
+ }
75
+ }
76
+ }
77
+
78
+ /**
79
+ * The arguments as options, or a usage error's message.
80
+ *
81
+ * @param {string[]} args
82
+ * @returns {{ dir: string | null, yes: boolean, names: string[], help: boolean } | string}
83
+ */
84
+ const parse = (args) => {
85
+ const options = { dir: null, yes: false, names: [], help: false }
86
+ const end = args.indexOf("--")
87
+ if ((end === -1 ? args : args.slice(0, end)).some((a) => a === "-h" || a === "--help")) {
88
+ // --help wins over anything else on the line, a bad option included.
89
+ return { ...options, help: true }
90
+ }
91
+ for (let i = 0; i < args.length; i++) {
92
+ const arg = args[i]
93
+ if (arg === "--") {
94
+ options.names.push(...args.slice(i + 1))
95
+ break
96
+ }
97
+ if (arg === "-h" || arg === "--help") {
98
+ options.help = true
99
+ } else if (arg === "-y" || arg === "--yes") {
100
+ options.yes = true
101
+ } else if (arg === "--dir" || arg.startsWith("--dir=")) {
102
+ const dir = arg === "--dir" ? args[++i] : arg.slice("--dir=".length)
103
+ if (dir === undefined || dir === "") {
104
+ return "--dir needs a directory"
105
+ }
106
+ options.dir = dir
107
+ } else if (arg.startsWith("-")) {
108
+ return `unknown option ${arg}`
109
+ } else {
110
+ options.names.push(arg)
111
+ }
112
+ }
113
+ return options
114
+ }
115
+
116
+ /** Thrown by a question that Ctrl-C interrupted. */
117
+ class Interrupted extends Error {}
118
+
119
+ /**
120
+ * Discard whatever is already waiting on the terminal, for about `ms`
121
+ * milliseconds: keys typed while the skills were being found must not answer a
122
+ * question the user has not seen yet. Raw mode lets a part line go too, not
123
+ * only whole lines.
124
+ */
125
+ const drainTypeAhead = async (ms) => {
126
+ const discard = () => {
127
+ // Typed before the question was shown, so it answers nothing.
128
+ }
129
+ process.stdin.setRawMode?.(true)
130
+ process.stdin.on("data", discard)
131
+ process.stdin.resume()
132
+ await new Promise((resolve) => setTimeout(resolve, ms))
133
+ process.stdin.pause()
134
+ process.stdin.off("data", discard)
135
+ process.stdin.setRawMode?.(false)
136
+ }
137
+
138
+ /**
139
+ * Questions asked on the terminal: each answers its line, or null at end of
140
+ * input, and Ctrl-C rejects it with `Interrupted`. The terminal is first opened
141
+ * at the first question, which comes after every skill has been found. Each
142
+ * question gets a terminal of its own, opened after what was typed ahead is
143
+ * thrown away and closed once it is answered, so no line typed before a
144
+ * question, a second line pasted at the one before included, can answer it.
145
+ */
146
+ const terminalAsk = () => {
147
+ let ended = false
148
+ return {
149
+ ask: async (question) => {
150
+ if (ended) {
151
+ return null
152
+ }
153
+ await drainTypeAhead(50)
154
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: true })
155
+ try {
156
+ /** @type {{ promise: Promise<never>, reject: (err: Error) => void }} */
157
+ const { promise: interrupted, reject } = Promise.withResolvers()
158
+ // Without a listener readline only pauses on Ctrl-C, and the question would hang.
159
+ rl.on("SIGINT", () => reject(new Interrupted("interrupted")))
160
+ process.stdout.write(question)
161
+ const next = await Promise.race([rl[Symbol.asyncIterator]().next(), interrupted])
162
+ if (next.done === true) {
163
+ ended = true
164
+ return null
165
+ }
166
+ return next.value
167
+ } finally {
168
+ rl.close()
169
+ }
170
+ },
171
+ }
172
+ }
173
+
174
+ /** Review and link for the project the arguments name, and report one line per skill. */
175
+ const run = async (args) => {
176
+ const options = parse(args)
177
+ if (typeof options === "string") {
178
+ usageError(options)
179
+ return
180
+ }
181
+ if (options.help) {
182
+ process.stdout.write(USAGE)
183
+ return
184
+ }
185
+ let project
186
+ if (options.dir !== null) {
187
+ if (!fs.statSync(options.dir, { throwIfNoEntry: false })?.isDirectory()) {
188
+ usageError(`${options.dir} is not a directory`)
189
+ return
190
+ }
191
+ project = path.resolve(options.dir)
192
+ } else {
193
+ // The rule postinstall uses, so a workspace member links at the root nish
194
+ // was hoisted to; a copy installed nowhere above (npx's cache) falls back.
195
+ project = projectOf(process.cwd(), root) ?? findProject(process.cwd())
196
+ if (project === null) {
197
+ usageError("no package.json in this directory or any above it")
198
+ return
199
+ }
200
+ }
201
+ const interactive =
202
+ options.names.length === 0 &&
203
+ !options.yes &&
204
+ process.stdin.isTTY === true &&
205
+ process.stdout.isTTY === true
206
+ const terminal = interactive ? terminalAsk() : null
207
+ try {
208
+ const { unknown } = await review(project, {
209
+ names: options.names,
210
+ yes: options.yes,
211
+ ask: terminal?.ask ?? null,
212
+ })
213
+ if (unknown.length > 0) {
214
+ const dirs = unknown.filter((n) => fs.statSync(n, { throwIfNoEntry: false })?.isDirectory() === true)
215
+ const hint = dirs.length > 0 ? `; did you mean --dir ${clean(dirs[0], 200)}?` : ""
216
+ usageError(
217
+ `no installed Nish package ships a skill named ${unknown.map((n) => clean(JSON.stringify(n), 200)).join(", ")}${hint}`
218
+ )
219
+ }
220
+ } catch (err) {
221
+ if (err instanceof Interrupted) {
222
+ // Every question is asked before anything is linked, so nothing was written.
223
+ process.stdout.write("\n")
224
+ process.stderr.write("nish-skills: interrupted; nothing was written\n")
225
+ process.exitCode = 130
226
+ return
227
+ }
228
+ process.stderr.write(`nish-skills: ${clean(err instanceof Error ? err.message : String(err), 1000)}\n`)
229
+ process.exitCode = 1
230
+ }
231
+ }
232
+
233
+ await run(process.argv.slice(2))
package/docs/INSTALL.md CHANGED
@@ -286,7 +286,7 @@ so the practical routes are
286
286
  build/nish app.ts -o app.ll
287
287
  # on the musl host, with its own clang and the runtime from this repository
288
288
  clang app.ll runtime/runtime.c runtime/runtime-text.c runtime/runtime-os.c runtime/runtime-parallel.c \
289
- runtime/runtime-cross.c runtime/runtime-host.c runtime/runtime-net.c \
289
+ runtime/runtime-cross.c runtime/runtime-host.c runtime/runtime-net.c runtime/runtime-uring.c \
290
290
  runtime/runtime-simd.c runtime/runtime-aes.c runtime/runtime-sha256.c runtime/runtime-chacha20.c \
291
291
  runtime/runtime-term.c runtime/runtime-bytes.c runtime/runtime-checksum.c runtime/runtime-sandbox.c \
292
292
  runtime/runtime-shm.c runtime/runtime-region.c -ffp-contract=off -lm -o app
@@ -393,7 +393,7 @@ It needs Node, clang and a released `nish` to start from, which the build
393
393
  fetches for you.
394
394
 
395
395
  The published package ships `bin/` (the `nish` command, which is a launcher),
396
- `runtime/` (the C runtime — seventeen translation units, the wasm subset and their
396
+ `runtime/` (the C runtime — nineteen translation units, the wasm subset and their
397
397
  header — and `runtime/sqlite/`, SQLite's vendored source),
398
398
  `scripts/build.sh` (the link pipeline), `std/` (the standard library) and
399
399
  `docs/AI.md` (the rules card). The
@@ -408,11 +408,12 @@ any working directory — and the binary runs from inside its own platform
408
408
  package for exactly that reason.
409
409
 
410
410
  Building the IR yourself rather than through `--link` means naming the runtime
411
- on the `clang` line, and it is seventeen files:
411
+ on the `clang` line, and it is eighteen files (`runtime-async.c` joins them only
412
+ for a program that awaits):
412
413
 
413
414
  ```bash
414
415
  clang app.ll runtime/runtime.c runtime/runtime-text.c runtime/runtime-os.c runtime/runtime-parallel.c \
415
- runtime/runtime-cross.c runtime/runtime-host.c runtime/runtime-net.c \
416
+ runtime/runtime-cross.c runtime/runtime-host.c runtime/runtime-net.c runtime/runtime-uring.c \
416
417
  runtime/runtime-simd.c runtime/runtime-aes.c runtime/runtime-sha256.c runtime/runtime-chacha20.c \
417
418
  runtime/runtime-term.c runtime/runtime-bytes.c runtime/runtime-checksum.c runtime/runtime-sandbox.c \
418
419
  runtime/runtime-shm.c runtime/runtime-region.c -ffp-contract=off -lm -o app
package/llms.txt CHANGED
@@ -25,6 +25,10 @@ Exit codes: 0 ok, 1 the program was rejected, 2 usage, 3 the C toolchain or the
25
25
  prebuilt compiler could not be run (one NL0002 object under --json), 70 a bug in
26
26
  the compiler.
27
27
 
28
+ The documentation site is https://nishlang.com/: every page is also served as
29
+ Markdown at its route plus `.md`, and https://nishlang.com/llms-full.txt is the
30
+ whole site in one file.
31
+
28
32
  ## Writing Nish
29
33
 
30
34
  - [Rules card](https://raw.githubusercontent.com/amritk/nish/main/docs/AI.md): the whole language as rules, sized for one read — the traps first, then types, `Result`, nullables, declarations, statements, the complete builtin inventory, and recipes for what is missing. Ships in the npm package as `docs/AI.md`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nish",
3
- "version": "0.40.1",
3
+ "version": "0.42.0",
4
4
  "description": "Nish: an ahead-of-time compiler from a static subset of TypeScript to LLVM IR",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -42,13 +42,16 @@
42
42
  "./*": "./std/*.ts"
43
43
  },
44
44
  "bin": {
45
- "nish": "bin/nish"
45
+ "nish": "bin/nish",
46
+ "nish-skills": "bin/nish-skills.js"
46
47
  },
47
48
  "files": [
48
49
  "bin",
49
50
  "runtime",
50
51
  "scripts/build.sh",
52
+ "scripts/link-skills.mjs",
51
53
  "scripts/postinstall.mjs",
54
+ "skills",
52
55
  "std",
53
56
  "README.md",
54
57
  "LICENSE",
@@ -89,7 +92,7 @@
89
92
  "bugs": {
90
93
  "url": "https://github.com/amritk/nish/issues"
91
94
  },
92
- "homepage": "https://github.com/amritk/nish#readme",
95
+ "homepage": "https://nishlang.com/",
93
96
  "keywords": [
94
97
  "typescript",
95
98
  "compiler",
@@ -111,9 +114,9 @@
111
114
  "typescript": "^5.6.0"
112
115
  },
113
116
  "optionalDependencies": {
114
- "@amritk/nish-aarch64-darwin": "0.40.1",
115
- "@amritk/nish-aarch64-linux": "0.40.1",
116
- "@amritk/nish-x86_64-darwin": "0.40.1",
117
- "@amritk/nish-x86_64-linux": "0.40.1"
117
+ "@amritk/nish-aarch64-darwin": "0.42.0",
118
+ "@amritk/nish-aarch64-linux": "0.42.0",
119
+ "@amritk/nish-x86_64-darwin": "0.42.0",
120
+ "@amritk/nish-x86_64-linux": "0.42.0"
118
121
  }
119
122
  }
package/runtime/nish.d.ts CHANGED
@@ -1641,13 +1641,18 @@ declare function tcpListen(host: string, port: i32, backlog: i32): i32;
1641
1641
  * (NL2404). Not callable in a `scope()` task or a parallel body. Capability:
1642
1642
  * net.
1643
1643
  *
1644
+ * Flag 8 (`NET_LISTEN_RING` in `nish:net`) accepts into io_uring slots on
1645
+ * Linux, so connections are not bound by the descriptor limit; where rings are
1646
+ * refused the listener serves descriptors and `netRingState` says why. Elsewhere
1647
+ * the flag is accepted and does nothing.
1648
+ *
1644
1649
  * @param host the numeric address to bind.
1645
1650
  * @param port the port, 0 for any.
1646
1651
  * @param backlog the kernel's queue of unaccepted connections.
1647
- * @param flags 1, 2 and 4 as above.
1652
+ * @param flags 1, 2, 4 and 8 as above.
1648
1653
  * @returns the listener's descriptor, or a negative errno: -22 for any other bit (a negative
1649
1654
  * `flags` included) or 4 without 1, before a socket is made; -95 for flag 4 off Linux; -98
1650
- * for a port in use.
1655
+ * for a port in use; -23 once the process has opened 31 ring listeners, closed or not.
1651
1656
  */
1652
1657
  declare function tcpListenFlags(host: string, port: i32, backlog: i32, flags: i32): i32;
1653
1658
  /**
@@ -1661,7 +1666,13 @@ declare function tcpListenFlags(host: string, port: i32, backlog: i32, flags: i3
1661
1666
  * @param peer at least 18 bytes, receiving the peer's address in the 18-byte form; it must not
1662
1667
  * be `readonly`. A shorter `peer` is -22 before anything is accepted, so no connection is
1663
1668
  * taken and lost.
1664
- * @returns the connection's descriptor; -11 when none waits; -95 for a datagram socket.
1669
+ * @returns the connection's descriptor, or a slot handle at or above 2^20 from a `NET_LISTEN_RING`
1670
+ * listener, whose `peer` is written as zeros; -11 when none waits; -23 when a ring listener's
1671
+ * slots are all taken; -95 for a datagram socket. A ring listener whose accept found memory
1672
+ * short answers -12 or -105 once, as a descriptor does, then pauses accepting for a back-off of
1673
+ * 10 ms, doubling to 640 ms while it recurs, and accepts again. Any other errno but -11, -4,
1674
+ * -103 or -71 means the listener is broken: it stops accepting and answers that errno from then
1675
+ * on, so close it and listen again.
1665
1676
  */
1666
1677
  declare function tcpAccept(fd: i32, peer: u8[]): i32;
1667
1678
  /**
@@ -1898,8 +1909,8 @@ declare function pollCreate(): i32;
1898
1909
  * @param events 1 readable, 2 writable, 3 both, 0 neither.
1899
1910
  * @param token an `i32` of the program's choosing, which `pollWait` reports it under as it went
1900
1911
  * in.
1901
- * @returns 0, or a negative errno: -17 for a descriptor the loop already watches, -22 for an
1902
- * event bit past 2.
1912
+ * @returns 0, or a negative errno: -17 for a descriptor the loop already watches, or for a slot
1913
+ * handle another loop watches (a slot is in one loop at a time), -22 for an event bit past 2.
1903
1914
  */
1904
1915
  declare function pollAdd(loop: i32, fd: i32, events: i32, token: i32): i32;
1905
1916
  /**
@@ -1931,7 +1942,10 @@ declare function pollRemove(loop: i32, fd: i32): i32;
1931
1942
  * `i32`s) and not `readonly`. A signal that interrupts the wait makes it answer
1932
1943
  * 0, as a timeout does, rather than wait past the deadline; the descriptor
1933
1944
  * `signalFd()` answers can be added to the loop, and is readable when SIGTERM
1934
- * or SIGINT arrives.
1945
+ * or SIGINT arrives. Once the process has opened a `NET_LISTEN_RING`
1946
+ * listener with rings, for the rest of its life, an interrupted wait goes on
1947
+ * to its deadline, since the rings' own work interrupts it too: watch
1948
+ * `signalFd()` to see a signal.
1935
1949
  *
1936
1950
  * A UDP socket is always writable, and on Linux one whose both sides are shut
1937
1951
  * is readable, writable and hung up, 7. On Darwin a descriptor both readable
@@ -2196,6 +2210,28 @@ declare const NET_UNIX_STREAM: i32;
2196
2210
  * Capability: none.
2197
2211
  */
2198
2212
  declare const NET_UNIX_SEQPACKET: i32;
2213
+ /**
2214
+ * `tcpListenFlags` flag 8: accept into io_uring slots instead of descriptors (WP63 S2, Linux). A
2215
+ * connection accepted from such a listener is a slot handle, a number at or above 2^20 that
2216
+ * `netRead`, `netWrite`, `netWrite2`, `netClose`, `netLocalPort` and the poll loop take as they
2217
+ * take a descriptor, except that a slot is in one poll loop at a time (a second loop's `pollAdd`
2218
+ * answers -17); `netSetOption` answers -95 and every other call -9. A program can so hold
2219
+ * more connections than its descriptor limit. Where the kernel refuses rings the listener serves
2220
+ * descriptors, and `netRingState` says why. Inert on Darwin. Capability: none.
2221
+ */
2222
+ declare const NET_LISTEN_RING: i32;
2223
+ /**
2224
+ * Whether listeners opened with `NET_LISTEN_RING` accept into rings: 1 when they do, 0 until a
2225
+ * listener has found rings or a refusal, or the negative errno the kernel refused rings with (-1
2226
+ * when a seccomp filter refuses `io_uring_setup`, -38 for a kernel without it), in which case they
2227
+ * serve descriptors. Decided once per process, by the first listener that finds rings or a
2228
+ * refusal; one that finds descriptors or memory short serves descriptors itself and leaves the
2229
+ * state at 0, and a later listener asks again. -95 on Darwin. Refused at
2230
+ * compile time on wasm32 and the WASI profile (NL2404). Capability: net.
2231
+ *
2232
+ * @returns 1, 0 or the negative errno.
2233
+ */
2234
+ declare function netRingState(): i32;
2199
2235
 
2200
2236
  // ---- `nish:term` ------------------------------------------------------------------
2201
2237
  //
@@ -3115,14 +3151,27 @@ declare module "nish:net" {
3115
3151
  * the index of the CPU that received it, in the order the group's listeners
3116
3152
  * were bound; a CPU past the group's size falls back to the hash.
3117
3153
  *
3154
+ * Flag 8, `NET_LISTEN_RING`, accepts into io_uring slots on Linux: each
3155
+ * connection is a slot handle at or above 2^20 rather than a descriptor, so
3156
+ * a listener holds connections past `RLIMIT_NOFILE`. `netRead`, `netWrite`,
3157
+ * `netWrite2`, `netClose`, `netLocalPort` and the poll loop take a slot
3158
+ * handle; `netSetOption` answers `-95`, and calls that need a descriptor,
3159
+ * `sendFd` among them, answer `-9`.
3160
+ * The first listener that finds rings or a refusal decides for the process:
3161
+ * where the kernel refuses rings it serves descriptors, and `netRingState`
3162
+ * answers the errno. A listener whose rings fail for want of descriptors or
3163
+ * memory serves descriptors itself, and a later listener asks again. Off
3164
+ * Linux the flag is accepted and does nothing.
3165
+ *
3118
3166
  * @param host A numeric host, as for `tcpListen`.
3119
3167
  * @param port The port.
3120
3168
  * @param backlog The length of the accept queue.
3121
- * @param flags The sum of the flags: `3` is both, `5` and `7` steer, and `0`
3122
- * is `tcpListen` exactly.
3169
+ * @param flags The sum of the flags: `3` is both, `5` and `7` steer, `8`
3170
+ * accepts into rings, and `0` is `tcpListen` exactly.
3123
3171
  * @returns The listener's descriptor or the failure: `-22` for any other
3124
3172
  * bit, a negative `flags`, or flag 4 without 1, before a socket is made;
3125
- * `-95` for flag 4 off Linux.
3173
+ * `-95` for flag 4 off Linux; `-23` once the process has opened 31 ring
3174
+ * listeners, closed or not.
3126
3175
  */
3127
3176
  export function tcpListenFlags(host: string, port: i32, backlog: i32, flags: i32): i32;
3128
3177
  /**
@@ -3132,9 +3181,16 @@ declare module "nish:net" {
3132
3181
  *
3133
3182
  * @param fd The listener.
3134
3183
  * @param peer The array the peer's address form is written into.
3135
- * @returns The connection's descriptor, `-11` when none is waiting, `-22`
3136
- * for a `peer` shorter than 18 bytes before anything is accepted (so no
3137
- * connection is taken and lost), or `-95` for a datagram socket.
3184
+ * @returns The connection's descriptor (a slot handle from a
3185
+ * `NET_LISTEN_RING` listener, whose `peer` is written as zeros), `-11`
3186
+ * when none is waiting, `-23` when a ring listener's slots are all
3187
+ * taken, `-22` for a `peer` shorter than 18 bytes before anything is
3188
+ * accepted (so no connection is taken and lost), or `-95` for a
3189
+ * datagram socket. A ring listener whose accept found memory short
3190
+ * answers `-12` or `-105` once, pauses for a back-off (10 ms, doubling
3191
+ * to 640 ms while it recurs) and accepts again. Any other errno but
3192
+ * `-11`, `-4`, `-103` or `-71` stops it accepting and is answered from
3193
+ * then on; close it and listen again.
3138
3194
  */
3139
3195
  export function tcpAccept(fd: i32, peer: u8[]): i32;
3140
3196
  /**
@@ -3316,7 +3372,9 @@ declare module "nish:net" {
3316
3372
  * @param token An `i32` of the program's choosing that comes back as it went
3317
3373
  * in.
3318
3374
  * @returns `0`, `-17` for a descriptor the loop already watches (on Darwin
3319
- * it is left under the new token), or `-22` for an event bit past `2`.
3375
+ * it is left under the new token) or for a slot handle another loop
3376
+ * watches (a slot is in one loop at a time), or `-22` for an event bit
3377
+ * past `2`.
3320
3378
  */
3321
3379
  export function pollAdd(loop: i32, fd: i32, events: i32, token: i32): i32;
3322
3380
  /**
@@ -3342,7 +3400,9 @@ declare module "nish:net" {
3342
3400
  * one pair per ready descriptor: `ready[2k]` its token and `ready[2k + 1]`
3343
3401
  * its events, `1` readable and `2` writable, with `4` added for a hang-up or
3344
3402
  * an error. A signal that interrupts the wait answers `0`, as a timeout
3345
- * does. Not `willreturn`, because it can wait forever. Nothing allocates,
3403
+ * does, until the process opens a `NET_LISTEN_RING` listener with rings:
3404
+ * from then on, for the rest of its life, the wait goes on to its deadline, and `signalFd()` is how a loop sees a signal. Not
3405
+ * `willreturn`, because it can wait forever. Nothing allocates,
3346
3406
  * and `ready` is never written past its length.
3347
3407
  *
3348
3408
  * @param loop The loop.
@@ -3540,6 +3600,16 @@ declare module "nish:net" {
3540
3600
  export const NET_UNIX_STREAM: i32;
3541
3601
  /** `unixSocketPair` kind: each write one message (`SOCK_SEQPACKET`). */
3542
3602
  export const NET_UNIX_SEQPACKET: i32;
3603
+ /** `tcpListenFlags` flag 8: accept into io_uring slots instead of descriptors. */
3604
+ export const NET_LISTEN_RING: i32;
3605
+ /**
3606
+ * Whether `NET_LISTEN_RING` listeners accept into rings.
3607
+ *
3608
+ * @returns `1` when they do, `0` until a listener has found rings or a
3609
+ * refusal, or the negative errno rings were refused with, in which case
3610
+ * they serve descriptors.
3611
+ */
3612
+ export function netRingState(): i32;
3543
3613
  }
3544
3614
 
3545
3615
  /**
package/runtime/nish.h CHANGED
@@ -822,7 +822,16 @@ bool nish_is_executable(const nish_str *path);
822
822
  * with `SO_REUSEPORT` for flag 1, `TCP_NODELAY` on each accepted socket for
823
823
  * flag 2, and, for flag 4 with flag 1, a reuseport program steering each new
824
824
  * connection to the listener bound at the index of the CPU that received it
825
- * (Linux; -95 elsewhere). Any other bit, or 4 without 1, is -22.
825
+ * (Linux; -95 elsewhere). Flag 8 accepts into io_uring slots on Linux,
826
+ * whose handles are at or above 2^20; any other bit, or 4 without 1, is -22.
827
+ * `nish_net_ring_state()`: 1 when flag 8 listeners get rings, 0 before one
828
+ * has asked, or the negative errno that refused them; -95 on Darwin.
829
+ * `nish_net_ring_premake(n, fds)`: `--sandbox`'s call, before its filter,
830
+ * that makes the `n` restricted rings every flag 8 listener then takes (WP63
831
+ * S3), writing each ring's descriptor to `fds`, then the descriptor of the
832
+ * keeper ring a freed one is replaced with (room for 65, NURING-36); 0
833
+ * records that no ring may be made. The rings made, or the errno of the step
834
+ * that failed; -95 on Darwin.
826
835
  * `nish_tcp_accept(fd, peer)`: a new descriptor and the peer's address.
827
836
  * `nish_net_read` / `nish_net_write(fd, buf, off, len)`: bytes `[off, off +
828
837
  * len)` of `buf`, which the compiled call has range-checked; a write to a
@@ -895,6 +904,8 @@ int32_t nish_net_address(nish_array *out, const nish_str *host, int32_t port);
895
904
  int32_t nish_net_local_port(int32_t fd);
896
905
  int32_t nish_tcp_listen(const nish_str *host, int32_t port, int32_t backlog);
897
906
  int32_t nish_tcp_listen_flags(const nish_str *host, int32_t port, int32_t backlog, int32_t flags);
907
+ int32_t nish_net_ring_state(void);
908
+ int32_t nish_net_ring_premake(int32_t n, int32_t *fds);
898
909
  int32_t nish_tcp_accept(int32_t fd, nish_array *peer);
899
910
  int32_t nish_net_read(int32_t fd, nish_array *buf, int64_t off, int64_t len);
900
911
  int32_t nish_net_write(int32_t fd, const nish_array *buf, int64_t off, int64_t len);
@@ -1011,8 +1022,13 @@ int64_t nish_cpu_online(void);
1011
1022
  Landlock scopes `fs.read` and `fs.write` to, `exec_paths` the programs
1012
1023
  `process.spawn` may execute, each a run of NUL-terminated paths ended by an
1013
1024
  empty one; null leaves the capability whole.
1014
- `nish_cpu_prime` caches the pool's CPU count while /sys is still readable. */
1015
- void nish_sandbox(int32_t caps, int32_t mode, const char *read_dirs, const char *write_dirs, const char *exec_paths);
1025
+ `nish_cpu_prime` caches the pool's CPU count while /sys is still readable.
1026
+ `net_rings` (WP63 S3) is how many restricted io_uring rings to make before
1027
+ the filter loads, for `NET_LISTEN_RING`: the compiler passes 0 unless `net`
1028
+ is granted and `main` may reach the bit; rings are made only when
1029
+ `process.spawn` is not granted, since that filter keeps setup allowed. */
1030
+ void nish_sandbox(int32_t caps, int32_t mode, const char *read_dirs, const char *write_dirs, const char *exec_paths,
1031
+ int32_t net_rings);
1016
1032
  void nish_sandbox_check(void);
1017
1033
  /* SBX-1: set by `nish_sandbox` when the program has an exec allowlist;
1018
1034
  `nish_spawn_start` (runtime-os.c) then leaves the spawn to it. It checks
package/runtime/nish.mjs CHANGED
@@ -169,6 +169,7 @@ for (const name of [
169
169
  "sendFd",
170
170
  "recvFd",
171
171
  "peerCredentials",
172
+ "netRingState",
172
173
  ]) {
173
174
  provide(name, () => shim.noNetReading(name));
174
175
  }
@@ -182,6 +183,7 @@ provide("NET_SO_RCVBUF", 5);
182
183
  provide("NET_UNIX_UNLINK_STALE", 1);
183
184
  provide("NET_UNIX_STREAM", 1);
184
185
  provide("NET_UNIX_SEQPACKET", 2);
186
+ provide("NET_LISTEN_RING", 8);
185
187
 
186
188
  // `nish:term`. Whether a descriptor is a terminal and how big stdout's is have
187
189
  // Node's answers; reading stdin, waiting for it and raw mode throw, each naming
@@ -177,6 +177,12 @@ void nish_cross_end(void *state) {
177
177
  * in no chunk below the newest leaves the arena as it was, as the release
178
178
  * does. */
179
179
  static void nish_rewind(uint64_t mark) {
180
+ #ifdef NISH_ARENA_DEBUG
181
+ /* Keeping a chunk to bump in would let the next element's rewind stay in
182
+ compiled code, and never retire what it gave back. */
183
+ nish_arena_release(mark);
184
+ return;
185
+ #endif
180
186
  struct nish_arena *a = &nish_arena;
181
187
  nish_xchunk *top = a->chunks;
182
188
  char *buf = (char *)(top + 1);