nish 0.39.0 → 0.41.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 (81) hide show
  1. package/README.md +190 -555
  2. package/docs/AI.md +112 -33
  3. package/docs/INSTALL.md +79 -133
  4. package/llms.txt +2 -9
  5. package/package.json +17 -19
  6. package/runtime/nish.d.ts +21 -17
  7. package/runtime/nish.h +10 -0
  8. package/runtime/page-frame.d.ts +123 -0
  9. package/runtime/page-frame.mjs +736 -0
  10. package/runtime/page-game.d.ts +132 -0
  11. package/runtime/page-game.mjs +738 -0
  12. package/runtime/runtime-aes.c +85 -8
  13. package/runtime/runtime-bytes.c +88 -4
  14. package/runtime/runtime-net.c +32 -0
  15. package/runtime/runtime-sandbox.c +55 -28
  16. package/runtime/runtime-uring.c +1194 -0
  17. package/runtime/runtime-wasm.c +422 -0
  18. package/runtime/vite.mjs +1 -1
  19. package/scripts/build.sh +22 -6
  20. package/std/README.md +74 -87
  21. package/std/array.ts +36 -3
  22. package/std/collections.ts +51 -0
  23. package/std/crypto/hmac.ts +37 -10
  24. package/std/crypto/p256.ts +1388 -2664
  25. package/std/crypto/pki.ts +116 -4
  26. package/std/crypto/sha256.ts +34 -6
  27. package/std/crypto/x509.ts +7 -0
  28. package/std/host/game.ts +273 -0
  29. package/std/input.ts +56 -0
  30. package/std/json-tokenizer.ts +325 -112
  31. package/std/json-typed.ts +43 -8
  32. package/std/json-value.ts +144 -3
  33. package/std/json.ts +63 -8
  34. package/std/net/fetch.ts +22 -4
  35. package/std/net/http-server.ts +3382 -0
  36. package/std/net/http1-client-codec.ts +6 -2
  37. package/std/net/http1-codec.ts +137 -12
  38. package/std/net/http1-server.ts +83 -29
  39. package/std/net/http2.ts +47 -14
  40. package/std/net/http3-server.ts +29 -3
  41. package/std/net/http3.ts +180 -7
  42. package/std/net/prefork.ts +49 -13
  43. package/std/net/quic-recovery.ts +240 -9
  44. package/std/net/quic-stream.ts +21 -1
  45. package/std/net/quic.ts +98 -4
  46. package/std/net/router.ts +378 -31
  47. package/std/net/serve.ts +873 -65
  48. package/std/net/tls/verify.ts +19 -1
  49. package/std/net/url.ts +83 -35
  50. package/std/net/websocket.ts +74 -1
  51. package/std/net/webtransport.ts +7 -1
  52. package/std/raster.ts +837 -0
  53. package/std/release.ts +593 -0
  54. package/std/synth.ts +143 -0
  55. package/std/temporal.ts +3091 -0
  56. package/std/threads.ts +298 -0
  57. package/std/yaml-json.ts +368 -28
  58. package/std/yaml.ts +122 -27
  59. package/scripts/bootstrap.sh +0 -375
  60. package/scripts/build-id-smoke.sh +0 -28
  61. package/scripts/changelog-gen.mjs +0 -649
  62. package/scripts/changelog-section.sh +0 -28
  63. package/scripts/check-hardening.mjs +0 -202
  64. package/scripts/ci-profile.mjs +0 -200
  65. package/scripts/codes-registry.js +0 -85
  66. package/scripts/fuzz.sh +0 -280
  67. package/scripts/gen-diagnostic-codes.mjs +0 -243
  68. package/scripts/gen-json-pow5.mjs +0 -130
  69. package/scripts/gen-p256-table.mjs +0 -177
  70. package/scripts/gen-pow10-table.py +0 -80
  71. package/scripts/gen-regex-unicode.mjs +0 -822
  72. package/scripts/gen-union-scale.mjs +0 -294
  73. package/scripts/nish-compiler.sh +0 -19
  74. package/scripts/platform-package.mjs +0 -93
  75. package/scripts/security-scope.mjs +0 -139
  76. package/scripts/shard-costs.mjs +0 -429
  77. package/scripts/size-report.sh +0 -84
  78. package/scripts/smoke.sh +0 -105
  79. package/scripts/std-module-paths.mjs +0 -25
  80. package/scripts/undocumented.txt +0 -14
  81. package/scripts/verify-binaries.sh +0 -214
package/README.md CHANGED
@@ -17,105 +17,61 @@
17
17
 
18
18
  ---
19
19
 
20
- Nish parses source with its own lexer and parser, rejects everything dynamic
21
- (`any`, prototypes, `eval`, exceptions, a garbage collector), and emits
22
- textual LLVM IR (`.ll`). LLVM's own toolchain (`clang` / `llc`) then optimises
23
- and produces native binaries for x86_64, ARM64, or WebAssembly. The compiler
24
- is itself written in Nish, in `src/`, and ships as a native binary with no
25
- runtime dependency.
20
+ You write `.ts` files with the syntax you already know. Nish checks that every
21
+ value has one fixed type and memory layout, refuses everything dynamic
22
+ (`any`, prototypes, `eval`, exceptions), and emits LLVM IR that `clang` turns
23
+ into a small native binary for x86_64, ARM64 or WebAssembly. The compiler is
24
+ itself written in Nish.
26
25
 
27
26
  ```
28
- TypeScript source ──▶ AST ──▶ validator + checker ──▶ LLVM IR (.ll) ──▶ clang/llc ──▶ native binary
29
- (src/parser.ts) (src/checker.ts) (src/emit*.ts) + runtime/runtime.c + runtime-os.c
27
+ TypeScript source ──▶ lexer + parser ──▶ validator + checker ──▶ LLVM IR (.ll) ──▶ clang ──▶ native binary
30
28
  ```
31
29
 
32
- If it compiles, every value has one fixed, known memory layout; binaries are
33
- a few kilobytes; there is no interpreter and no GC anywhere in the pipeline.
34
-
35
30
  > [!CAUTION]
36
- > **Do not use Nish in production.** It is a very early, pre-alpha
37
- > experiment in how far a static subset of TypeScript can go when it is
38
- > compiled straight to native code, not a tool to build on yet. Expect
39
- > miscompilations, crashes, missing features and diagnostics that are wrong
40
- > or confusing. The compiler, the runtime and the standard library,
41
- > cryptography included, have had one audit by the project itself
42
- > ([docs/security/](docs/security/README.md)), which fixed what it found and
43
- > left findings open. No one independent has reviewed them. Nothing it
44
- > produces should handle data, money or systems you care about. To report a
45
- > vulnerability, see [SECURITY.md](SECURITY.md).
46
-
47
- > [!WARNING]
48
- > **Everything still changes.** Until 1.0, the language, the CLI flags and the
49
- > IR the compiler emits all change without notice, often from one minor
50
- > release to the next. Every commit compiles, tests and bootstraps itself (see
51
- > [Project status](#project-status) for what is done and what is next), but
52
- > nothing here is frozen. If you try it anyway, pin an exact version rather
53
- > than a range, expect to fix your source when you move to a newer one, and
54
- > read [CHANGELOG.md](CHANGELOG.md) before you upgrade.
55
-
56
- ---
31
+ > **Nish is pre-alpha. Do not use it in production.** Expect
32
+ > miscompilations, crashes, missing features and confusing diagnostics. Until
33
+ > 1.0 the language, the flags and the output change without notice, often
34
+ > between minor releases: pin an exact version and read the
35
+ > [changelog](CHANGELOG.md) before you upgrade. The code, cryptography
36
+ > included, has had no independent review ([security records](docs/security/README.md)).
57
37
 
58
- ## Quickstart
59
-
60
- Requirements: clang (LLVM 18) + lld, to produce binaries; per-OS install
61
- commands are in [docs/INSTALL.md](docs/INSTALL.md).
38
+ ## A taste
62
39
 
63
40
  ```bash
64
41
  curl -fsSL https://raw.githubusercontent.com/amritk/nish/main/install.sh | sh
65
42
  ```
66
43
 
67
- The script needs only `sh`, `curl` and `tar`. It installs the prebuilt
68
- compiler into `~/.nish` and ends by printing the `export PATH=…` line to add
69
- to your shell profile.
70
-
71
- **Or with npm**, which needs Node.js 22.18+:
72
-
73
- ```bash
74
- npm install -g nish # or per project: npm i -D nish
75
- npx nish --version # or run it without installing
76
- ```
77
-
78
- npm fetches the prebuilt native compiler for your machine alongside it.
79
-
80
- > [!NOTE]
81
- > **From a release tarball rather than the registry, install the pair.** The
82
- > main package is a launcher and carries no compiler: installed on its own it
83
- > has nothing to hand over to and says so, exiting 3.
84
- > The same is true on a platform this project publishes no binary for — musl,
85
- > FreeBSD, 32-bit anything — which since 0.6.0 gets a diagnostic naming the four
86
- > that do rather than the TypeScript compiler under Node that used to ship
87
- > beside it. [docs/INSTALL.md](docs/INSTALL.md) §2 has the table of what is
88
- > built for what, and what is left for a platform outside it.
89
-
90
- > [!TIP]
91
- > Building from source works just as well: `git clone`, `npm ci`,
92
- > `bash scripts/fetch-seed.sh`, `npm run build`, then `build/nish ...` wherever this README says `nish`.
93
- > The build is a bootstrap: the previous release's `nish` (the seed, fetched
94
- > by `scripts/fetch-seed.sh`, or named with `NISH_BOOTSTRAP=<path>`) compiles
95
- > `src/`, and that compiler compiles `src/` again into `build/nish`.
96
-
97
44
  `hello.ts`:
98
45
 
99
46
  ```ts
100
- export const main = (): number => {
47
+ export const main = (): i32 => {
101
48
  console.log("hello from Nish");
102
- return 0; // the process exit code
49
+ return 0; // the process exit code
103
50
  };
104
51
  ```
105
52
 
106
53
  ```bash
107
- nish hello.ts --link hello # writes hello.ll, then builds hello with clang -O3 -flto
108
- ./hello # hello from Nish
109
- nish hello.ts -o hello.ll # IR only
110
- nish run hello.ts # build into a cache and run it: a script, still native
54
+ nish run hello.ts # build into a cache and run it
55
+ nish hello.ts --link hello # a native binary, about 4.7 KB
56
+ nish hello.ts -o hello.ll # just the LLVM IR
111
57
  ```
112
58
 
113
- The IR is readable as is. `examples/add.ts` compiles to:
59
+ A short program can skip `main` and read like a script:
114
60
 
115
- ```llvm
116
- declare extern_weak void @nish_panic_overflow(i32 noundef) #1
117
- declare { i32, i1 } @llvm.sadd.with.overflow.i32(i32, i32) #2
61
+ ```ts
62
+ const square = (n: i32): i32 => n * n
118
63
 
64
+ const sides = [3, 4, 5]
65
+ for (const side of sides) {
66
+ console.log(square(side))
67
+ }
68
+ ```
69
+
70
+ The IR is readable. `examples/add.ts`, `a + b` on two `number`s (32-bit
71
+ integers by default), compiles to a checked add: an overflow panics instead
72
+ of wrapping, and a sum the compiler proves fits is a plain `add nsw`.
73
+
74
+ ```llvm
119
75
  define noundef i32 @add(i32 noundef %a, i32 noundef %b) #0 {
120
76
  entry:
121
77
  %0 = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %a, i32 %b)
@@ -130,519 +86,198 @@ ovf.fail:
130
86
  call void @nish_panic_overflow(i32 0)
131
87
  unreachable
132
88
  }
133
-
134
- attributes #0 = { nounwind }
135
- attributes #1 = { nounwind noreturn cold }
136
- attributes #2 = { nounwind willreturn readnone }
137
89
  ```
138
90
 
139
- Nothing bounds `a` and `b`, so the `+` is checked: an overflow panics with
140
- `attempt to add with overflow` rather than wrapping or being undefined, and a
141
- sum the compiler proves fits is a plain `add nsw` instead
142
- ([Semantics decisions](docs/LANGUAGE.md#semantics-decisions)).
91
+ Next step: [Your first program](docs/guide/getting-started/first-program.md).
143
92
 
144
- `--plain` drops the attributes and alignment hints and leaves
145
- `define i32 @add(i32 %a, i32 %b)` with the same body. Every construct's IR
146
- is in [docs/IR_COOKBOOK.md](docs/IR_COOKBOOK.md).
93
+ ## Contents
147
94
 
148
- ---
95
+ - [Install](#install)
96
+ - [The language](#the-language)
97
+ - [Memory without a garbage collector](#memory-without-a-garbage-collector)
98
+ - [The standard library](#the-standard-library)
99
+ - [Threads and async](#threads-and-async)
100
+ - [The command line](#the-command-line)
101
+ - [Performance and binary size](#performance-and-binary-size)
102
+ - [Interop](#interop)
103
+ - [The compiler in a browser](#the-compiler-in-a-browser)
104
+ - [Documentation](#documentation)
105
+ - [Project status](#project-status)
106
+ - [Contributing, security and license](#contributing-security-and-license)
107
+
108
+ ## Install
109
+
110
+ The install script needs only `sh`, `curl` and `tar`, and puts the prebuilt
111
+ compiler in `~/.nish`. With Node.js 22.18+ there is also npm:
112
+
113
+ ```bash
114
+ npm install -g nish
115
+ ```
116
+
117
+ Linking binaries needs clang (LLVM 18) and lld. Per-OS commands, the supported
118
+ platforms and building from source are in
119
+ [Installation](docs/guide/getting-started/installation.md).
149
120
 
150
121
  ## The language
151
122
 
152
- New to Nish? The [user guide](docs/guide/index.md) teaches the language page
153
- by page, from installing it to threads, with every example compiled by
154
- `npm test`.
155
-
156
- Threads come from one import, `nish/threads`: parallel maps and reduces,
157
- scoped tasks, a `Mutex` that owns its data, atomics and channels, with data
158
- races and deadlocks refused at compile time.
159
- [Which one to reach for](docs/guide/using-nish/threads.md) is the guide's
160
- threads page.
161
-
162
- The full reference is [docs/LANGUAGE.md](docs/LANGUAGE.md); every rule
163
- there cites the test case that proves it. If an **AI** is writing the program —
164
- or you want the whole language in one pass rather than as a reference to browse
165
- — read [docs/AI.md](docs/AI.md) instead: the same rules, ordered by which
166
- TypeScript reflex they reject, with every example compiled by `npm test`. Both
167
- ship in the npm package, and [llms.txt](llms.txt) indexes them.
168
-
169
- | Feature | Summary | Reference |
170
- |:---|:---|:---|
171
- | Types | `number` (`i32` by default, `double` with `--number-mode f64`), `i32`, `i64`, unsigned `u8`/`u16`/`u32`/`u64`, `f32`, `f64`, ranged `integer<Lo, Hi>`, `boolean`, `string`, `T[]` and `readonly T[]`, classes, interfaces, `T \| null`, `void`; 1:1 LLVM mapping, no implicit conversions | [Types](docs/LANGUAGE.md#types) |
172
- | Enums, aliases and unions | numeric `enum`s as a distinct type, with members usable as types (`Kind.Circle`); `type` aliases; discriminated unions `Circle \| Square` of classes or interfaces tagged by an enum-member field, narrowed by testing the tag; all exportable across modules | [Enums](docs/LANGUAGE.md#enums), [Type aliases](docs/LANGUAGE.md#type-aliases), [Discriminated unions](docs/LANGUAGE.md#discriminated-unions) |
173
- | Functions and modules | annotated signatures, calls in any order, default and rest parameters, `export`/named relative `import`, packages by name, whole-program attribute facts, `export const main` as the entry (or, in an entry with no `main`, statements at the top level that run as one), `internal` linkage for everything not exported; a parameter may have a function type, and each function or arrow passed to it compiles to its own instantiation with a direct call, never a function pointer, while the arrow may read the caller's `const` locals and parameters | [Declarations](docs/LANGUAGE.md#declarations), [Top-level code](docs/LANGUAGE.md#top-level-code), [Default and rest parameters](docs/LANGUAGE.md#default-and-rest-parameters), [Function parameters](docs/LANGUAGE.md#function-parameters) |
174
- | Async functions | `async` functions and arrows returning `Promise<T>`, `await` on a direct call, `spawn` and `awaitAll` from `nish:async`, `export async function main`; each compiles to a frame and a resume switch sized at link time, with no promise ever a value | [Async functions](docs/LANGUAGE.md#async-functions-async-and-await) |
175
- | Control flow | `if`/`else`, `while`, `do`, `for`, `for...of` (arrays, `Map` keys and values, `Set`), `switch` (exhaustive over a union's tag, a clause may fall into the next), `break`/`continue`, labelled or not, boolean-only conditions, definite return, unreachable-code errors | [Statements](docs/LANGUAGE.md#statements) |
176
- | Expressions | `+ - * / %` (integer `/` and `%` checked: zero divisor or `MIN / -1` panics), shifts and bitwise operators, numeric-only ordering, `=== !==` (strings by content), `&& \|\|`, `?:`, `??` on a `T \| null` and a `?.` chain that ends in `??`, `op=`, `++`/`--`, template literals, contextual numeric literals | [Expressions](docs/LANGUAGE.md#expressions) |
177
- | Strings | immutable UTF-8 (`.length` is the byte length), literals as constant data, `+`, `===`, templates, `console.log` | [Builtins](docs/LANGUAGE.md#builtins), [Semantics](docs/LANGUAGE.md#semantics-decisions) |
178
- | Arrays | `T[]` with one element type, literals, `new Array<T>(n)` zero-filled, bounds-checked `a[i]` (panic, or `uncheckedGet` from `nish:unsafe`), `.length`, `push`/`pop`, `set`/`fill`, `sort`, `map`/`filter`/`some`/`every`/`findIndex`/`slice`, `for...of`; arrays of records stored contiguously and fixed-length array fields inline | [Arrays](docs/LANGUAGE.md#array-literals) |
179
- | `Map` and `Set` | the global `Map<K, V>` and `Set<T>`, insertion-ordered, one probe per `get`/`set`/`has`/`delete`; `get` is `V \| undefined` and narrowed as TypeScript does | [`Map` and `Set`](docs/LANGUAGE.md#map-and-set) |
180
- | Classes and interfaces | LLVM structs with clang's layout, constructors, methods, `readonly`, definite assignment, object literals, `implements` by identical layout; no inheritance | [Classes](docs/LANGUAGE.md#classes) |
181
- | Generics | generic functions, classes, interfaces and methods by monomorphisation: one specialised `define` per instantiation, type arguments inferred at a call and written after `new` or in an annotation, `<T extends Shape>` constraints, exported instantiations callable from C and JavaScript as `nish_gen_identity_i32` | [Generic functions](docs/LANGUAGE.md#generic-functions) |
182
- | Memory | no GC: objects that provably do not escape their function are stack `alloca`s, functions whose temporaries die with them get an automatic arena scope, `using a = arena()` frees a batch the compiler has checked nothing outlives, and `Arena.mark`/`Arena.used` read the arena; a long-running program frees a batch that outlives one call with `nish:region` | [`using a = arena()`](docs/LANGUAGE.md#using-a--arena), [`Arena`](docs/LANGUAGE.md#arena), [Memory](docs/LANGUAGE.md#memory-model) |
183
- | `T \| null` | for class, interface, array and string types; `=== null`, a `??` default, and narrowing to `T` by `if`, early return, `while`, `&&`, `?:`, enforced by the checker | [Nullable types](docs/LANGUAGE.md#nullable-types) |
184
- | Errors | Rust-style `Result<T, E>` with `Ok`/`Err`, `isOk()`/`isErr()`, `orReturn()` (the `?`), `unwrapOr`, `expect`; the checker refuses to let a failure be dropped or the success payload be read before the error is handled. No `throw`, no unwinding: `panic(message)` ends the process | [Result and error handling](docs/LANGUAGE.md#result-and-error-handling) |
185
- | Threads | `nish/threads`: `parallelMap`/`parallelReduce`, scoped tasks (`using s = scope()`), `Mutex<T>`, `AtomicI32`/`AtomicI64`, `Channel<T>`; data races and deadlocks refused at compile time | [Data parallelism](docs/LANGUAGE.md#data-parallelism-nishthreads) |
186
- | Builtins | `console.log`, `Math.*` as LLVM intrinsics (JavaScript's `pow`, `round`, `min`/`max` corner cases included), `Math.random`, `toI32`/`toI64`/`toF64`, `process.exit`, files, directories, subprocesses, the environment and the clock; the runtime-backed ones are also importable from `nish:fs` / `nish:process` / `nish:io` / `nish:net` / `nish:term`, which is the same builtin under a name nothing can shadow; `nish:unsafe` (unchecked access, defined wrapping), `nish:secret` (`Secret<T>`, key material the checker keeps in and wipes), `nish:region`, `nish:simd` (128-bit vector lanes) and `nish:async` (`spawn`, `awaitAll`) are reached only by import | [Builtins](docs/LANGUAGE.md#builtins), [Builtin modules](docs/LANGUAGE.md#builtin-modules-nish) |
187
- | Calling C | `declare function` binds a C symbol, `CPtr` holds an opaque C pointer, and a handle class bound with `using` is closed at every exit of its block | [Calling C](docs/LANGUAGE.md#calling-c) |
188
- | Capabilities and policies | the compiler reports which capabilities (`fs.read`, `net`, `process.spawn`, `ffi`, `unsafe`, ...) every function, module and package reaches, with the call chain; `--allow`/`--deny` and `package.json` refuse a program that reaches one it is not granted, a dependency gets none it is not given, and a capability lockfile fails a build when a dependency reaches a new one. `--deny-panics` or `noPanic` refuses every panic site the compiler cannot prove away | [Capabilities](docs/LANGUAGE.md#capabilities), [The no-panic scope](docs/LANGUAGE.md#the-no-panic-scope) |
189
- | Rejected | `any`, `unknown`, `var`, `==`, `?.` outside a `??` chain, generic type aliases, a promise as a value (`.then`, `Promise.all`), `try`, `throw`, `typeof`, `instanceof`, `delete`, prototypes, `Object.assign`, string-keyed access, ... with exact messages | [Forbidden constructs](docs/LANGUAGE.md#forbidden-constructs-phase-0-validator) |
190
-
191
- Semantics that differ from JavaScript on purpose: signed integer overflow
192
- panics unless the compiler proves the operation fits (`wrappingAdd`,
193
- `wrappingSub` and `wrappingMul` from `nish:unsafe` wrap two's-complement; the
194
- unsigned widths wrap either way), integer division by zero panics instead of
195
- yielding `0`, `.length` counts bytes, there is no `throw` and no unwinding,
196
- `toI32` saturates, `Math.min`/`max` take two arguments. The reasons are in the
197
- [FAQ](docs/FAQ.md); [docs/wp13-differential.md](docs/wp13-differential.md)
198
- lists everything the differential test suite found that still differs from
199
- Node.
200
-
201
- ### The standard library
202
-
203
- [`std/`](std/README.md) is Nish written in Nish, for Nish programs to import.
204
- [`std/testing`](std/testing.ts) is a test runner, so a compiled program can
205
- check itself and return an exit code with no Node involved. For text there is
206
- [`std/text`](std/text.ts) (`trim`, `splitLines`, `splitWhitespace`,
207
- `replaceAll`) and [`std/regex`](std/regex.ts), regular expressions in
208
- JavaScript syntax whose search time is linear in the input. For data there is
209
- `std/json` (plus a tokenizer, typed decoding and a value tree), `std/yaml`,
210
- `std/base64` and `std/bytes`. Programs also get `std/cli` for arguments,
211
- `std/random` (a seeded `Rng`), `std/map`, `std/collections`, `std/threads`,
212
- `std/sqlite` (SQLite, the database in a file) and `std/term` (raw-mode
213
- terminal I/O and styling, with pixel graphics in `std/term-pixels`).
214
- `nish/crypto/*` holds the primitives: SHA-1/2, HMAC, PBKDF2, HKDF,
215
- ChaCha20-Poly1305, AES-GCM, X25519, Ed25519, P-256 ECDSA, bcrypt and X.509,
216
- with secret material held as a wiped `Secret<T>`. `nish/net/*` holds the
217
- protocols built on them: TLS 1.3, QUIC, HTTP/1.1, HTTP/2 (HPACK), HTTP/3
218
- (QPACK), WebSocket (server and client), WebTransport, a URL parser, an HTTP router, a prefork
219
- server and a sans-IO PostgreSQL client. The [standard
220
- library](docs/guide/using-nish/standard-library.md) guide page walks through
221
- them, and the [`std/` README](std/README.md) lists every module.
123
+ The TypeScript you know, held to one rule: every value's type and layout is
124
+ known at compile time.
125
+
126
+ - **Types:** `number`, fixed-width integers (`i32`, `u8`, `i64`, ...), ranged
127
+ `integer<Lo, Hi>`, `f64`, `boolean`, `string`, arrays, `Map` and `Set`,
128
+ classes, interfaces, enums, discriminated unions and `T | null`.
129
+ - **Code:** arrow functions, generics by monomorphisation, modules and
130
+ packages, `for...of`, exhaustive `switch`, `async`/`await`.
131
+ - **Errors:** `Result<T, E>` with `orReturn()` (Rust's `?`), and `panic` for
132
+ what a caller should not handle. No `throw`, no unwinding.
133
+ - **Refused, with the idiom to use instead:** `any`, `unknown`, `var`, `==`,
134
+ `try`, `typeof`, `instanceof`, prototypes, closures stored as values.
135
+
136
+ Some answers differ from JavaScript on purpose: signed overflow and integer
137
+ division by zero panic, and `.length` counts UTF-8 bytes.
138
+
139
+ Start with [Coming from TypeScript](docs/guide/getting-started/from-typescript.md),
140
+ then the language pages: [types](docs/guide/language/types.md),
141
+ [functions](docs/guide/language/functions.md),
142
+ [classes and interfaces](docs/guide/language/classes-and-interfaces.md),
143
+ [unions](docs/guide/language/unions.md),
144
+ [generics](docs/guide/language/generics.md),
145
+ [errors](docs/guide/language/errors.md) and
146
+ [modules](docs/guide/language/modules.md). The normative rules are in the
147
+ [language reference](docs/LANGUAGE.md); writing Nish with an AI, start from
148
+ [docs/AI.md](docs/AI.md) and [llms.txt](llms.txt).
149
+
150
+ ## Memory without a garbage collector
151
+
152
+ There is no GC and no `free`, so use-after-free and double free cannot be
153
+ written. The compiler places each value: on the stack when it provably dies
154
+ with its function, in an arena scope the compiler opens and releases for you,
155
+ or in a region you free in a long-running program. Array indexes are
156
+ bounds-checked unless a proof shows they are in range, `null` is a separate
157
+ type that must be narrowed, and the only ways around a check are imports you
158
+ ask for by name (`nish:unsafe`).
159
+
160
+ Read [Memory](docs/guide/language/memory.md) and
161
+ [Regions](docs/guide/language/regions.md).
162
+
163
+ ## The standard library
164
+
165
+ [`std/`](std/README.md) is Nish written in Nish, compiled with your program,
166
+ so what you never call is dropped at the link:
167
+
168
+ - **Data and text:** JSON, YAML, regular expressions with linear-time search,
169
+ base64, bytes, text helpers, sorting and collections.
170
+ - **Programs:** a test runner, argument parsing, terminal I/O, SQLite, a
171
+ seeded random generator.
172
+ - **Crypto:** SHA-1/2, HMAC, HKDF, PBKDF2, ChaCha20-Poly1305, AES-GCM, X25519,
173
+ Ed25519, P-256 and P-384 ECDSA, RSA, bcrypt and X.509, with keys held as a
174
+ wiped `Secret<T>`.
175
+ - **Network:** TLS 1.3, HTTP/1.1, HTTP/2, HTTP/3 and QUIC, WebSocket,
176
+ WebTransport, an HTTP router and client, DNS and PostgreSQL.
222
177
 
223
178
  ```ts
224
179
  import { Suite } from "nish/testing";
225
180
 
226
- export const main = (): number => {
227
- const t = new Suite("stats");
228
- t.eqI32("sumOf", sumOf([3, 9, 4, 9]), 25);
181
+ const sumOf = (xs: i32[]): i32 => {
182
+ let total = 0;
183
+ for (const x of xs) {
184
+ total = total + x;
185
+ }
186
+ return total;
187
+ };
188
+
189
+ export const main = (): i32 => {
190
+ const t = new Suite("sums");
191
+ t.eqI32("four numbers", sumOf([3, 9, 4, 9]), 25);
229
192
  return t.done(); // prints the report; 0 when nothing failed
230
193
  };
231
194
  ```
232
195
 
233
- A library module is source, not a built artifact, so it compiles with the
234
- program that imports it and the whole-program pass sees straight through it
235
- ([docs/wp21-packages.md](docs/wp21-packages.md)): `nish/<module>` resolves to
236
- `std/<module>.ts` beside the running compiler, and what you import but never
237
- call is dropped at the link. A function can be *passed* as a typed parameter.
238
- Each call site is specialised into a direct call. A function is never a stored
239
- value, so a suite is an object with methods rather than a registry of
240
- `test("name", () => ...)` closures.
241
-
242
- The suite's own golden cases are run by [`tests/nish/run.ts`](tests/nish/run.ts),
243
- which is this repository's test harness written in the language it tests:
244
- `readdirSync` finds the cases, `spawnSyncTo` captures each compile and each run,
245
- and the IR is diffed against the golden line by line. `npm run test:nish` runs it
246
- over the whole corpus.
247
-
248
- [`tests/nish/cli.ts`](tests/nish/cli.ts) is the other half of that idea, pointed
249
- at the compiler's own promises rather than at its output: `--help` on stdout with
250
- exit 0 against the same text on stderr with exit 2, one flat `--json` object per
251
- diagnostic with a stable code, and each documented exit-code band. It reads those
252
- objects with `std/json` and takes the version it expects out of `package.json`, so
253
- a release cannot leave the expectation behind (`npm run test:cli` runs it
254
- against `build/nish`).
196
+ Tour it in [The standard library](docs/guide/using-nish/standard-library.md);
197
+ there are guide pages for an [API server](docs/guide/using-nish/api-server.md),
198
+ an [HTTP client](docs/guide/using-nish/http-client.md),
199
+ [PostgreSQL](docs/guide/using-nish/postgres.md),
200
+ [SQLite](docs/guide/using-nish/sqlite.md),
201
+ [password hashing](docs/guide/using-nish/password-hashing.md) and
202
+ [more](docs/guide/index.md).
255
203
 
256
- ---
204
+ ## Threads and async
257
205
 
258
- ## Memory safety
259
-
260
- There is no garbage collector and no `free`, so the bugs that need one cannot
261
- be written. What is left — an index out of range, a null dereference — is
262
- checked, and every way to give a check up is a flag you pass or a builtin you
263
- call on purpose.
264
-
265
- | Bug class | What Nish does | Reference |
266
- |:---|:---|:---|
267
- | Use-after-free, double free | Not expressible: nothing is freed individually. Four compile-time mechanisms decide where a value lives — a stack `alloca` when escape analysis proves it dies with the frame, an automatic arena scope when a function's temporaries do, a `nish_arena_keep` reclaim at the call site for a returned string, the bump arena otherwise — and the arena goes back when `main` returns | [Memory model](docs/LANGUAGE.md#memory-model), [wp6-memory.md](docs/wp6-memory.md) |
268
- | Out-of-bounds read or write | Every `a[i]`, `a[i] op= v`, `s.charCodeAt(i)`, `s.slice(a, b)` and `a.pop()` is bounds-checked, with an unsigned compare, so a negative index fails too; the failure prints `index out of range: <i> >= <len>` (or, for `slice`, `slice out of range: [<a>, <b>) of length <len>`) and exits 1. Where a flow-sensitive proof shows the index is already in range — a loop condition, a length guard, a hoisted `const n = a.length`, an unsigned index — **no check is emitted at all**, and the checks that survive inside a loop say so as a `performance` warning naming the guard that would remove them | [Element access](docs/LANGUAGE.md#element-access), `tests/cases/arr_bounds_panic`, `arr_bounds_proven` |
269
- | Null dereference | `T \| null` is a separate type, for pointers only; member access needs a narrowing the checker accepts, and `?.` appears only in a chain that ends in a `??` default — which is what lets the emitter put `nonnull dereferenceable` on every pointer that is not one | [Nullable types](docs/LANGUAGE.md#nullable-types) |
270
- | Uninitialised memory | `new Array<T>(n)` zero-fills and rejects pointer element types, because a zeroed pointer would be a null nobody declared; class fields are definitely assigned | [Classes](docs/LANGUAGE.md#classes), `tests/cases/arr_new_zeroed` |
271
- | Unwinding past a release | There is none. Every function is `nounwind`; a failure a caller should handle is a `Result<T, E>` and one it should not is `panic(message)` — stderr, exit 1 | [Result](docs/LANGUAGE.md#result-and-error-handling) |
272
-
273
- ### What happens when the proof fails
274
-
275
- This is where designs actually differ. Asked to place a value it cannot show
276
- dies with its frame, Rust refuses to compile it, Go falls back to the garbage
277
- collector, and Zig hands the question back to you and an allocator. Nish
278
- leaves the value in the arena, where it stays until `main` returns.
279
-
280
- So [`src/escape.ts`](src/escape.ts) and the whole-program fact
281
- fixpoint are optimisations and nothing else: a refusal costs memory and never
282
- correctness, and no program is rejected for a lifetime reason. The compiler
283
- says so out loud where the cost is real: assigning an allocation to a local
284
- that already holds one drops the old value where nothing can free it *and*
285
- costs the whole function its arena scope, and that is a `performance`
286
- diagnostic naming both rewrites ([Diagnostics](docs/LANGUAGE.md#diagnostics-and-debugging-flags), on by
287
- default, never fatal). That is the
288
- trade the whole memory design rests on — the arena discipline a compiler pass
289
- or a frame loop would otherwise be written around by hand, moved into the
290
- compiler. `tests/cases/mem_*` pin the placements, and `Arena.used()` either
291
- side of a 100000-iteration loop is how the suite checks that a scope really
292
- does recycle.
293
-
294
- ### Which language is this like
295
-
296
- Not any one of them; it is more useful to say which piece came from where.
297
-
298
- | Concern | Closest to | Not |
299
- |:---|:---|:---|
300
- | Lifetimes | Go's escape analysis, over a Zig-style arena discipline the compiler writes for you | Rust: no ownership, no borrow checker, no lifetime annotations |
301
- | Bounds and panics | Rust with `panic=abort` | C |
302
- | Null | Kotlin and C# nullable reference types: flow narrowing, not a wrapper type | Rust's `Option<T>` |
303
- | Errors | Rust: `Result<T, E>`, and `orReturn()` is `?` | exceptions, or Go's second return value |
304
- | Signed overflow | Rust's debug profile: a checked panic, in every build, unless the compiler proves the operation fits; Rust's `wrapping_add` as an imported call to opt out at one site | C, where it is undefined |
305
- | Syntax | TypeScript | |
306
-
307
- The older relative is region inference as in Cyclone and MLKit: regions the
308
- compiler infers, bracketed by a mark and a release, which is exactly what the
309
- arena scopes and the call-site reclaim are. This version is deliberately
310
- weaker: one global arena, per-function granularity, no region polymorphism.
311
-
312
- ### Where it is not safe
206
+ `nish/threads` gives parallel maps and reduces, scoped tasks, a `Mutex` that
207
+ owns its data, atomics and channels, with data races and deadlocks refused at
208
+ compile time. `async` functions compile to a frame and a resume switch, with
209
+ no promise ever a value.
313
210
 
314
- > [!CAUTION]
315
- > Two holes, each of them asked for by name.
316
-
317
- - **`uncheckedGet` and `uncheckedSet`** from `nish:unsafe` read and write an
318
- element with no bounds check, after which an out-of-range index at that site
319
- is undefined behaviour. A module reaches them only through the import, so
320
- every module that gives up a check says so at its top and every site is a
321
- call (`tests/cases/unsafe_get_set`). That is a different thing from the
322
- proof above: the proof removes a check the compiler showed was never going
323
- to fire, and changes nothing about what the program means
324
- ([`nish:unsafe`](docs/LANGUAGE.md#nishunsafe-unchecked-access-and-defined-wrapping)).
325
- No flag turns the checks off for a whole program.
326
- - **Interop** hands a pointer to a C, wasm or N-API host, and what happens to
327
- it there is the host's business
328
- ([wp8-interop.md](docs/wp8-interop.md)).
329
-
330
- Memory-safe like Go, allocated like Zig, errors like Rust, nulls like Kotlin,
331
- overflow checked like Rust's debug builds — with two C-shaped holes you have to ask for by name. What it
332
- buys is the output: no GC, no runtime, and the sizes under
333
- [Performance and binary size](#performance-and-binary-size).
211
+ Read [Threads](docs/guide/using-nish/threads.md),
212
+ [Async](docs/guide/language/async.md) and
213
+ [An async server](docs/guide/using-nish/async-server.md).
334
214
 
335
- ---
336
-
337
- ## Command line
215
+ ## The command line
338
216
 
339
217
  ```
340
- usage: nish <file.ts> [more.ts ...] [flags] compile to LLVM IR
341
- nish run [flags] <file.ts> [args ...] build, cache and run it
342
- nish -v, --version | -h, --help
218
+ nish <file.ts> [more.ts ...] [flags] compile to LLVM IR (--link for a binary)
219
+ nish run [flags] <file.ts> [args ...] build, cache and run it
220
+ nish --help every flag, the exit codes, examples
343
221
  ```
344
222
 
345
- `nish --help` prints every flag, grouped by what it is for (Build, Check,
346
- Capabilities, Interop, Inspect, Code generation), then the exit codes and a
347
- few examples; it is coloured on a terminal and plain when piped, under
348
- `NO_COLOR` or when a coding agent runs it. An unknown flag is refused in one
349
- line that points at `--help`. [The CLI](docs/guide/using-nish/cli.md) walks
350
- through the flags you will reach for. Among them: `--link` builds a native
351
- binary, and `--fix` applies the compiler's safe fixes in place.
352
- `--allow`/`--deny` refuse a program that reaches more than a capability
353
- policy grants, and `--update-capability-lock` records what each dependency
354
- reaches. `--deny-panics` refuses any panic the compiler cannot prove away.
355
- `--emit-ts` writes the program out as plain TypeScript instead of IR.
356
-
357
- Exit codes: `0` success, `1` compile error (`file:line:col: error: ...` plus
358
- a caret excerpt), `2` usage error, `3` toolchain error, `70` internal
359
- compiler error (please
360
- [report it](https://github.com/amritk/nish/issues/new/choose) with the input and
361
- the command line).
362
- A compile that fails reports every error it found (statement by statement,
363
- declaration by declaration), in source order, up to 20 before `...and N more
364
- errors`; `--json` gives editors and tools the same list as
365
- `{"file","line","column","endLine","endColumn","severity","code","message"}`
366
- objects, one per line, where `code` is a stable identifier for the rule
367
- (`NL1013`, `NL2231`) and is what to match on rather than the prose. Failures
368
- with no source position — an unusable C toolchain, an internal error — are JSON
369
- objects too, so `--json` never leaves a caller with an empty stdout.
370
- `--help` prints on stdout and exits `0`; only a usage *error* goes to stderr
371
- with `2`. `-g` adds a DWARF line table and variables to the IR so
372
- `gdb`/`lldb` step through the `.ts` source of a `--link`ed binary
373
- ([docs/wp10-ci.md](docs/wp10-ci.md)).
374
- Multi-file programs: `nish examples/multi/main.ts --link build/multi && ./build/multi; echo $?`
375
- prints `49`.
376
-
377
- **`nish run` is the scripting shape.** `nish run tool.ts a b` compiles
378
- `tool.ts`, links it into a cache, and starts it with `a b` as its arguments.
379
- Its stdin, stdout and stderr are the program's, and its exit status is the
380
- program's once it starts (`128 + n` for a signal). The options before the file
381
- are the compiler's (`--number-mode f64`, `--profile speed`, `-g`), and
382
- everything after it is the program's, `--help` included. Nothing is
383
- interpreted: every run compiles, which takes milliseconds, and links only when
384
- the IR, the recipe or the runtime is new, so the first run of an edit pays for
385
- one `--profile debug` link (the default here, and the fast one) and every run
386
- after that starts the cached binary. The cache is `$XDG_CACHE_HOME/nish/run`,
387
- or `~/.cache/nish/run`. Each entry is one program, so `rm -rf` of it is always
388
- safe. Beside it, `nish/runtime` keeps the C runtime compiled once per profile,
389
- `-g`, `--threads` and C compiler, so a link (a run's or `--link`'s) compiles
390
- only the program's own modules, each in parallel; removing it costs one
391
- recompile of the runtime. `-o`, `--link`, `--target`, the `--emit-*` sidecars and
392
- `--profile wasi` write something a run keeps to itself, so they are usage
393
- errors there, and performance warnings are not printed, because stderr belongs
394
- to the program.
395
-
396
- Without `--link`, build the IR yourself: `clang add.ll examples/main.c runtime/runtime.c runtime/runtime-text.c runtime/runtime-os.c -ffp-contract=off -o app`
397
- (a program that uses threads, the host calls or `nish:net` also names
398
- `runtime/runtime-parallel.c`, `runtime-host.c` or `runtime-net.c`; the
399
- `overriding the module target triple` warning is harmless: the IR is
400
- target-neutral unless you pass `--target`; `-Wno-override-module` silences
401
- it), or step by step with `llvm-as`, `llc -O2 -filetype=obj`, and
402
- `opt -S -O2` to watch `mem2reg` turn the `alloca` locals into registers.
403
- `opt` and `llc` only vectorise when the module carries a data layout, so
404
- pass `--target host` (or a triple) when you inspect optimised IR by hand;
405
- `--link` never needs it because clang supplies the layout. Cross-compile
406
- with `--target aarch64-unknown-linux-gnu` / `wasm32-wasi` plus
407
- `clang --target=...`, or with `llc -mtriple=...`.
223
+ Errors arrive all at once as `file:line:col` with a caret, or as JSON with a
224
+ stable code for editors. `--fix` applies safe fixes, `--allow`/`--deny` refuse
225
+ a program that reaches more capabilities (files, network, processes) than you
226
+ grant, and `--emit-ts` writes the program back out as plain TypeScript.
408
227
 
409
- ---
228
+ Read [The CLI](docs/guide/using-nish/cli.md) and
229
+ [Sandboxing](docs/guide/using-nish/sandbox.md).
410
230
 
411
231
  ## Performance and binary size
412
232
 
413
- Rust-class output is the goal: no GC, no embedded engine, aliasing and
414
- purity facts handed to LLVM up front, and a link step that strips everything
415
- unused. `examples/add.ts` + `examples/main.c` + the runtime
416
- (`runtime/runtime.c`, which `scripts/build.sh` pairs with the other four units), x86_64 Linux, glibc
417
- dynamically linked (`npm run size-report`):
418
-
419
- | Profile | Bytes | What it does |
420
- |:---|---:|:---|
421
- | `debug` | 15,072 | `clang` defaults: no optimisation, symbols kept. |
422
- | `speed` | 4,528 | `-O3 -flto`, section GC, unwind tables off, stripped. Rust `--release`. |
423
- | `size` | 4,512 | `-Oz -flto`, plus hidden visibility and no stack protector. Rust `opt-level="z"`. |
424
- | `wasm` | 279 | Freestanding `wasm32` module, every function exported, stripped. |
425
- | `wasi` | 42,228 (`argv.ts`) | `wasm32-wasi` command module: the runtime linked against wasi-libc, `_start` runs `main`; needs a WASI sysroot ([INSTALL.md](docs/INSTALL.md#wasi-optional-for---profile-wasi)). |
426
-
427
- `hello.ts` with `--link` is 4,712 bytes. The runtime is seventeen translation units
428
- with a measured ceiling each, so that the core does not grow every time the
429
- language reaches further into the operating system, and section GC drops
430
- whatever a program does not call: `runtime.c` is the 1,723 bytes every program
431
- touches (one chunked bump arena with O(1) reset and mark/release, string
432
- parsing, `Math.random`, `process.argv`, array growth, the panic paths),
433
- `runtime-text.c` the 1,787 bytes of strings and JavaScript-exact number
434
- formatting, which a wasm module links too, `runtime-os.c` the 1,530 bytes
435
- that wrap a system call (exit, files, directories, subprocesses, the
436
- environment, the clock), and `runtime-parallel.c`, `runtime-cross.c`,
437
- `runtime-host.c`, `runtime-net.c`, `runtime-simd.c`, `runtime-aes.c`,
438
- `runtime-sha256.c`, `runtime-chacha20.c`, `runtime-term.c`, `runtime-bytes.c`, `runtime-checksum.c`, `runtime-sandbox.c`, `runtime-shm.c` and `runtime-region.c` hold threads, the copies that cross between
439
- their arenas, the host calls (wall clock, entropy, signals), `nish:net`'s sockets,
440
- the vector text search, AES-GCM, SHA-256's block, ChaCha20's keystream, `nish:term`'s stdin and terminal, the
441
- byte-class runs JSON steps over, the CRC-32 kernels, `--sandbox`'s confinement, the shared-memory regions of `nish/sync/shm` and the regions a program frees one at a time — see
442
- [docs/wp7-runtime.md](docs/wp7-runtime.md#runtime-additions-and-budget) for the
443
- budgets and the reasoning. `Math.*` calls are LLVM intrinsics, so pure functions
444
- stay `readnone`.
445
-
446
- Memory is the part that usually costs a compiled-JavaScript design its
447
- speed, so it is done statically ([docs/wp6-memory.md](docs/wp6-memory.md)):
448
- a `new`, object literal or array literal that provably never outlives its
449
- function is an `alloca` (LLVM's SROA then turns its fields into registers);
450
- what does reach the arena is bumped inline (a load, an add, a compare and a
451
- store); a function whose arena temporaries all die with it brackets its body
452
- with `nish_arena_mark` / `nish_arena_release`, so hot loops keep the arena
453
- flat. Every LLVM attribute the compiler emits (`nounwind`, `willreturn`,
454
- `readnone`/`readonly`, `noundef`, `zeroext`, `nonnull`, `noalias`,
455
- `nocapture`, `dereferenceable`) is a proved guarantee, never a hint; the
456
- rules are in [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md#attribute-soundness-rules).
457
-
458
- The benchmark suite (`bench/`: fib, n-body, spectral norm, sieve, string
459
- building, a `Vec3` method loop, each in Nish, C and Rust with identical
460
- algorithms and a shared checksum) is run by `node bench/run.mjs`, which
461
- writes [docs/BENCHMARKS.md](docs/BENCHMARKS.md): wall time, binary size and
462
- peak memory per column, plus the exact build commands. The analysis of every
463
- gap, and what `nsw` and PGO (`scripts/build.sh --pgo-generate` /
464
- `--pgo-use`) bought when overflow was still undefined, is in [docs/wp9-optimisation.md](docs/wp9-optimisation.md);
465
- the rules of the game are in [bench/README.md](bench/README.md).
233
+ No GC, no embedded engine, and every LLVM attribute the compiler emits is a
234
+ proved guarantee. `hello.ts` links to about 4.7 KB; a freestanding wasm
235
+ module can be a few hundred bytes. The benchmark suite runs the same
236
+ algorithms in Nish, C and Rust and records wall time, size and memory in
237
+ [docs/BENCHMARKS.md](docs/BENCHMARKS.md).
466
238
 
467
- ---
468
-
469
- ## Interop: export, do not embed
239
+ ## Interop
470
240
 
471
- Nish never embeds a JavaScript engine (see the [FAQ](docs/FAQ.md#why-not-embed-a-javascript-engine-for-npm-packages)).
472
- The supported direction is Node importing Nish:
473
-
474
- ```
475
- [ Node / Bun process ] imports [ Nish .wasm or .node addon ]
476
- I/O, HTTP, npm packages math, parsing, data transforms, hot loops
477
- cross the boundary once per batch, not once per element
478
- ```
479
-
480
- - `scripts/build.sh --profile wasm` produces a module `WebAssembly.instantiate`
481
- loads directly (`examples/node-host.mjs`); exports use the plain C ABI.
482
- Add `runtime/runtime-wasm.c` (arena + arrays, no libc) when a function
483
- takes or returns an array.
484
- - `--emit-napi` + `scripts/build.sh --profile napi` build a `.node` addon
485
- with argument type checks (`examples/node-addon.mjs`).
486
- - Buffers cross as typed arrays: an Nish `Int32Array` / `Float64Array` /
487
- `BigInt64Array` parameter (the spellings of `i32[]` / `f64[]` / `i64[]`,
488
- one layout) is a JS typed array on both paths. The addon borrows it
489
- (zero-copy; writes are visible in JS), the wasm loader that `--emit-dts`
490
- writes next to the `.d.ts` copies it into the module's memory and results
491
- back out; strings cross the addon as copies (`examples/arrays.ts`).
492
- - `--emit-header` writes C prototypes (`int32_t add(int32_t a, int32_t b);`,
493
- `double sumF64(const nish_array *xs);`) next to `runtime/nish.h`, the
494
- public runtime ABI (arena, strings, arrays, `nish_reset_arena`,
495
- `nish_arena_mark` / `nish_arena_release` for a host that manages batches).
496
- - `--emit-ts <dir>` writes the program back out as TypeScript that Node 22.18+
497
- runs with type stripping, one file per module plus an `entry.ts`, importing
498
- its builtins from `nish/runtime`; overflow and bounds checks stay, as
499
- helpers (`docs/guide/using-nish/to-typescript.md`).
500
- - An N-API call costs about 30 ns and a wasm call about 2 ns before any work
501
- is done; one call with a 1M-element `Float64Array` runs at 0.5 ns/element
502
- (`node bench/ffi.mjs`), so pass whole buffers, not elements.
503
-
504
- Details: [docs/wp8-interop.md](docs/wp8-interop.md).
505
-
506
- ---
241
+ Nish never embeds a JavaScript engine; JavaScript calls Nish. A program
242
+ builds to a wasm module or a Node addon with generated `.d.ts` types, the
243
+ addon borrows typed arrays without a copy, and `--emit-header` writes C
244
+ prototypes for a C host. Read [Calling Nish from other languages](docs/guide/using-nish/interop.md)
245
+ and [Browser games](docs/guide/using-nish/browser-games.md).
507
246
 
508
247
  ## The compiler in a browser
509
248
 
510
- `src/` is an Nish program, so the compiler compiles itself to
511
- WebAssembly like any other one:
512
-
513
- ```bash
514
- nish src/compile.ts --link web/nish.wasm --profile wasi
515
- node web/compile.mjs web/nish.wasm examples/add.ts # the IR, from a Web Worker
516
- ```
517
-
518
- That module is about 480 KB (140 KB gzipped) and lexes, checks and emits IR
519
- with no server in the loop; `web/index.html` is a playground built on it and
520
- `web/wasi.mjs` is the in-memory filesystem it runs against. It stops at the
521
- IR — `clang` and `wasm-ld` are not in a page — so `--link` and `--profile` are
522
- refused there. [web/README.md](web/README.md) has the rest.
523
-
524
- ---
525
-
526
- ## Self-hosting
527
-
528
- `src/` is the compiler, and the only one: lexer, parser, checker and emitter,
529
- written in Nish, with no `typescript` package underneath. It compiles its own
530
- source to a fixed point:
249
+ The compiler compiles itself to WebAssembly, about 140 KB gzipped, and checks
250
+ and emits IR in a Web Worker with no server. [web/README.md](web/README.md)
251
+ has the playground.
531
252
 
532
- ```
533
- IR(stage1, src/) == IR(stage2, src/) byte for byte, and stage3 == stage2
534
- ```
535
-
536
- The chain starts from a seed, the previous release's `nish` binary, the way
537
- Rust and Go build themselves: stage1 is `src/` built by the seed, stage2 is
538
- `src/` built by stage1 and is what gets installed, and stage3, built by
539
- stage2, has to reproduce stage2 file for file. `scripts/bootstrap.sh --verify`
540
- asserts both equalities, and `npm test` re-proves them on every run. Compiling
541
- the whole compiler costs it **91 ms and 86 MB**; the TypeScript compiler it
542
- replaced took 786 ms and 178 MB for the same input.
543
-
544
- ```bash
545
- bash scripts/fetch-seed.sh # the last release, into build/seed/
546
- npm run build # seed -> stage1 -> stage2 = build/nish
547
- build/nish hello.ts --link hello # -o, --link, --profile, its own directories
548
- ```
253
+ ## Documentation
549
254
 
550
- Because the seed is the last release, `src/` may only *use* in its own
551
- source what that release compiles. A new construct is implemented in `src/`
552
- and becomes usable inside `src/` from the next release on — the rolling
553
- freeze CI's `bootstrap` job checks. Until R6 there was a second compiler, the
554
- TypeScript one, stage0, which seeded every bootstrap and served as the
555
- oracle each phase of `src/` was compared against; it was deleted once the
556
- self-hosted compiler did everything it did
557
- ([wp19](docs/wp19-stage0-retirement.md)).
558
- Details, and the subset `src/` is written in, are in
559
- [docs/wp14-selfhost.md](docs/wp14-selfhost.md).
560
-
561
- ---
255
+ | Read | For |
256
+ |:---|:---|
257
+ | [The user guide](docs/guide/index.md) | Learning Nish page by page, every example compiled by the test suite |
258
+ | [Installation](docs/guide/getting-started/installation.md) | Platforms, LLVM, WASI, building from source |
259
+ | [Language reference](docs/LANGUAGE.md) | Every rule, each citing the test that proves it |
260
+ | [docs/AI.md](docs/AI.md), [llms.txt](llms.txt) | The whole language in one pass, for people and models writing it |
261
+ | [`std/` README](std/README.md) | Every standard library module |
262
+ | [FAQ](docs/FAQ.md) | Why the design is the way it is |
263
+ | [IR cookbook](docs/IR_COOKBOOK.md) | The exact IR of every construct |
264
+ | [Roadmap](docs/roadmap/index.md) | What is built and what is next |
265
+ | [Changelog](CHANGELOG.md) | What changed in each release |
562
266
 
563
267
  ## Project status
564
268
 
565
- Pre-alpha, as above, and **not for production use**. The milestones below
566
- say what compiles, not what has been hardened: the one security audit so far
567
- was the project's own ([docs/security/](docs/security/README.md), with findings
568
- still open), and there is no compatibility promise. M4 is the
569
- milestone that freezes the language reference and tags a release. Until it
570
- lands, a construct's spelling, a flag's name and the IR any of them lowers
571
- to are all still free to change.
572
-
573
- | Milestone | Contents | State |
574
- |:---|:---|:---|
575
- | M1 "Programs" | pipeline prep, validator, control flow, strings, modules, CI | done |
576
- | M2 "Data" | classes and interfaces, arrays, runtime and intrinsics | done |
577
- | M3 "Rust parity" | interop, memory strategy (stack allocation, arena scopes, `T \| null`), benchmarks with `--target`/`--nsw` (since removed)/PGO, differential testing against Node | done |
578
- | M4 "1.0" | frozen language reference, tagged release | not scheduled: the project stays on 0.x until its owner declares 1.0 |
579
- | M5 "Self-hosting" | `src/`: the compiler, written in Nish, compiling itself | done |
580
- | M6 "One compiler" | the TypeScript twin deleted; `src/` built by the last release, the seed | done |
581
-
582
- Not in the language yet, in the order they are likely to land: optional
583
- reference counting for objects that must outlive an arena reset, and dynamic
584
- dispatch — which would be a trait object over an interface, since inheritance
585
- was removed ([docs/wp25-inheritance.md](docs/wp25-inheritance.md)) and every
586
- method call names one symbol today.
587
- Generic functions, classes, interfaces and methods compile — each
588
- instantiation becomes its own specialised function or struct
589
- ([docs/wp18-generics.md](docs/wp18-generics.md)) — while generic type aliases,
590
- closures as values (an arrow written as a call's argument may read the
591
- caller's `const` locals and parameters, but is never stored) and `try`/`catch` are refusals
592
- rather than gaps, each with the message and the idiom to use instead
593
- ([docs/LANGUAGE.md](docs/LANGUAGE.md#forbidden-constructs-phase-0-validator)).
594
- Release engineering (`--version`, exit codes, npm packaging, tag-driven
595
- releases) landed with WP12; see [CHANGELOG.md](CHANGELOG.md) and
596
- [docs/wp12-release.md](docs/wp12-release.md). The plan itself is
597
- [docs/MASTER_PLAN.md](docs/MASTER_PLAN.md), and what is built and what is next is
598
- the [roadmap](docs/roadmap/index.md).
599
-
600
- ---
601
-
602
- ## Contributing
603
-
604
- ```bash
605
- npm ci
606
- bash scripts/fetch-seed.sh # the last release into build/seed/, once
607
- npm run build # seeded bootstrap into build/nish (NISH_BOOTSTRAP=<nish> names the seed)
608
- npm test # build + goldens, llvm-as, native round trips, runtime, layout, memory, interop, bench checksums, differential
609
- node tests/run.js locals # only cases whose name contains "locals"
610
- npm run test:update # write missing .ll goldens for new cases
611
- npm run test:diff # every whole program natively and under Node (runtime/shim.mjs), compared byte for byte
612
- node tests/differential/fuzz.js --stage1 --count 200 # random integer programs, the released seed's IR against HEAD's; prints the seed
613
- npm run check # tsc --noEmit: an ambient type-check of src/, std/ and tests/nish/
614
- npm run lint # Biome style lint (advisory, never a compile gate)
615
- npm run smoke # build and run every example with a main
616
- scripts/bootstrap.sh --verify # the whole chain, with IR(stage1) == IR(stage2) and stage3 == stage2 asserted
617
- node bench/run.mjs # the benchmark suite; rewrites docs/BENCHMARKS.md (about 3 minutes)
618
- docs/cookbook/regen.sh # refresh docs/IR_COOKBOOK.md; node docs/check-links.mjs checks the links
619
- ```
620
-
621
- Read [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) (the pipeline, side tables,
622
- the add-a-construct checklist, ABI guard tests) and the conventions in
623
- [docs/MASTER_PLAN.md §7](docs/MASTER_PLAN.md#7-conventions-for-every-agent):
624
- every construct ships with a golden `.ll`, an `llvm-as` pass, a native round
625
- trip, a negative test, and its LANGUAGE.md and cookbook entries; no attribute
626
- without a proof; layout changes touch `src/runtime.ts` and `runtime/runtime.c` together.
627
- CI runs the suite on Ubuntu with LLVM 18, and the bootstrap and the CLI
628
- contract on every platform a release ships a compiler for, macOS and ARM Linux
629
- included ([docs/wp10-ci.md](docs/wp10-ci.md)). The documentation index is
630
- [docs/README.md](docs/README.md). Coding guidelines for contributors and
631
- coding agents are in [AGENTS.md](AGENTS.md) and [`.claude/`](.claude/).
632
-
633
- ---
634
-
635
- ## Security
636
-
637
- Report a vulnerability privately, through GitHub's **Security → Report a
638
- vulnerability**, never in a public issue. [SECURITY.md](SECURITY.md) has the
639
- reporting channel, the supported versions and the threat model.
640
- [docs/security/](docs/security/README.md) holds the audit record of every area
641
- and the list of findings still open.
642
-
643
- ---
644
-
645
- ## License
646
-
647
- MIT, see [LICENSE](LICENSE). Third-party code in the repository keeps its own
648
- licence; see [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).
269
+ Pre-alpha and on 0.x, with no compatibility promise: a construct's spelling,
270
+ a flag's name and the IR either lowers to can change between minor releases.
271
+ Every commit compiles, tests and rebuilds the compiler with itself. The
272
+ [roadmap](docs/roadmap/index.md) has what is built and what is next.
273
+
274
+ ## Contributing, security and license
275
+
276
+ - **Contributing:** building the compiler, the tests and the conventions are in
277
+ [CONTRIBUTING.md](CONTRIBUTING.md).
278
+ - **Security:** report a vulnerability privately through GitHub's **Security →
279
+ Report a vulnerability**, never in a public issue; see
280
+ [SECURITY.md](SECURITY.md). The audit records are in
281
+ [docs/security/](docs/security/README.md).
282
+ - **License:** MIT, see [LICENSE](LICENSE). Third-party code keeps its own
283
+ licence; see [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).