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.
- package/README.md +190 -555
- package/docs/AI.md +112 -33
- package/docs/INSTALL.md +79 -133
- package/llms.txt +2 -9
- package/package.json +17 -19
- package/runtime/nish.d.ts +21 -17
- package/runtime/nish.h +10 -0
- package/runtime/page-frame.d.ts +123 -0
- package/runtime/page-frame.mjs +736 -0
- package/runtime/page-game.d.ts +132 -0
- package/runtime/page-game.mjs +738 -0
- package/runtime/runtime-aes.c +85 -8
- package/runtime/runtime-bytes.c +88 -4
- package/runtime/runtime-net.c +32 -0
- package/runtime/runtime-sandbox.c +55 -28
- package/runtime/runtime-uring.c +1194 -0
- package/runtime/runtime-wasm.c +422 -0
- package/runtime/vite.mjs +1 -1
- package/scripts/build.sh +22 -6
- package/std/README.md +74 -87
- package/std/array.ts +36 -3
- package/std/collections.ts +51 -0
- package/std/crypto/hmac.ts +37 -10
- package/std/crypto/p256.ts +1388 -2664
- package/std/crypto/pki.ts +116 -4
- package/std/crypto/sha256.ts +34 -6
- package/std/crypto/x509.ts +7 -0
- package/std/host/game.ts +273 -0
- package/std/input.ts +56 -0
- package/std/json-tokenizer.ts +325 -112
- package/std/json-typed.ts +43 -8
- package/std/json-value.ts +144 -3
- package/std/json.ts +63 -8
- package/std/net/fetch.ts +22 -4
- package/std/net/http-server.ts +3382 -0
- package/std/net/http1-client-codec.ts +6 -2
- package/std/net/http1-codec.ts +137 -12
- package/std/net/http1-server.ts +83 -29
- package/std/net/http2.ts +47 -14
- package/std/net/http3-server.ts +29 -3
- package/std/net/http3.ts +180 -7
- package/std/net/prefork.ts +49 -13
- package/std/net/quic-recovery.ts +240 -9
- package/std/net/quic-stream.ts +21 -1
- package/std/net/quic.ts +98 -4
- package/std/net/router.ts +378 -31
- package/std/net/serve.ts +873 -65
- package/std/net/tls/verify.ts +19 -1
- package/std/net/url.ts +83 -35
- package/std/net/websocket.ts +74 -1
- package/std/net/webtransport.ts +7 -1
- package/std/raster.ts +837 -0
- package/std/release.ts +593 -0
- package/std/synth.ts +143 -0
- package/std/temporal.ts +3091 -0
- package/std/threads.ts +298 -0
- package/std/yaml-json.ts +368 -28
- package/std/yaml.ts +122 -27
- package/scripts/bootstrap.sh +0 -375
- package/scripts/build-id-smoke.sh +0 -28
- package/scripts/changelog-gen.mjs +0 -649
- package/scripts/changelog-section.sh +0 -28
- package/scripts/check-hardening.mjs +0 -202
- package/scripts/ci-profile.mjs +0 -200
- package/scripts/codes-registry.js +0 -85
- package/scripts/fuzz.sh +0 -280
- package/scripts/gen-diagnostic-codes.mjs +0 -243
- package/scripts/gen-json-pow5.mjs +0 -130
- package/scripts/gen-p256-table.mjs +0 -177
- package/scripts/gen-pow10-table.py +0 -80
- package/scripts/gen-regex-unicode.mjs +0 -822
- package/scripts/gen-union-scale.mjs +0 -294
- package/scripts/nish-compiler.sh +0 -19
- package/scripts/platform-package.mjs +0 -93
- package/scripts/security-scope.mjs +0 -139
- package/scripts/shard-costs.mjs +0 -429
- package/scripts/size-report.sh +0 -84
- package/scripts/smoke.sh +0 -105
- package/scripts/std-module-paths.mjs +0 -25
- package/scripts/undocumented.txt +0 -14
- package/scripts/verify-binaries.sh +0 -214
package/docs/AI.md
CHANGED
|
@@ -53,7 +53,7 @@ stdout, nothing on stderr, every field 1-based with `endLine`/`endColumn`
|
|
|
53
53
|
exclusive:
|
|
54
54
|
|
|
55
55
|
```json
|
|
56
|
-
{"file":"p.ts","line":3,"column":17,"endLine":3,"endColumn":23,"severity":"error","code":"NL2249","message":"Unknown method `reduce` on i32[] (supported: push, pop, indexOf, join, set, fill, sort, map, filter, some, every, findIndex, slice)"}
|
|
56
|
+
{"file":"p.ts","line":3,"column":17,"endLine":3,"endColumn":23,"severity":"error","code":"NL2249","message":"Unknown method `reduce` on i32[] (supported: push, pop, indexOf, join, set, fill, sort, map, filter, some, every, findIndex, forEach, slice)"}
|
|
57
57
|
```
|
|
58
58
|
|
|
59
59
|
- **Key on `code`, never on `message`.** A code is a promise: `NL2249` means
|
|
@@ -153,7 +153,7 @@ everywhere, a template literal instead of `String(n)`.
|
|
|
153
153
|
|
|
154
154
|
- A module is one `.ts` file. At the top level it may hold **only**
|
|
155
155
|
declarations: `const` arrows (functions), `class`, `interface`, module
|
|
156
|
-
`const`s, `type` aliases,
|
|
156
|
+
`const`s, `type` aliases, `enum`s (numeric or string), and `import`s. There is no
|
|
157
157
|
top-level code, so there is no initialisation order to think about. The
|
|
158
158
|
one exception is an entry module with no `main`: its top-level statements
|
|
159
159
|
run as `main` (`docs/LANGUAGE.md` → "Top-level code"). Declare every
|
|
@@ -177,7 +177,7 @@ rejects. This table is the highest-value part of the page.
|
|
|
177
177
|
| `x == y` | `Loose equality is forbidden` | `x === y` |
|
|
178
178
|
| `throw new Error(m)` | `` `throw` is forbidden `` | `return Err(m)`, or `panic(m)` to end the process |
|
|
179
179
|
| `try { … } catch { … }` | `` `try`/`catch`/`finally` is forbidden `` | `Result<T, E>` and `isErr()` |
|
|
180
|
-
| `xs.reduce(f)`, `.
|
|
180
|
+
| `xs.reduce(f)`, `.shift` | `` Unknown method `reduce` on i32[] (supported: push, pop, indexOf, join, set, fill, sort, map, filter, some, every, findIndex, forEach, slice) `` | a `for` loop; `map`, `filter`, `forEach`, `some`, `every`, `findIndex` and `slice` work, with a one- or two-parameter callback |
|
|
181
181
|
| `xs.find(f)` | `` `find` is not supported: TypeScript types its answer `T \| undefined` … `` (NL2697) | `const i = xs.findIndex(f); if (i >= 0) { ... xs[i] ... }` |
|
|
182
182
|
| `s.toUpperCase()`, `s.split()`, `s.trim()`, `s.replace()` | `` Unknown method … on string `` | index bytes with `charCodeAt` / `substring` |
|
|
183
183
|
| `x.toFixed(2)`, `s.split(sep, limit)` | `` Unknown method `toFixed` on f64 `` | `toFixed(x, 2)` and `splitLimit(s, sep, limit)` from `nish/text` |
|
|
@@ -200,7 +200,8 @@ rejects. This table is the highest-value part of the page.
|
|
|
200
200
|
| `keyof T`, `<T = i32>` (a default type argument) | `` Unsupported type `keyof T` (Phase 1 supports …) `` / `a default type argument … is not supported` | name the type; a type argument is always inferred |
|
|
201
201
|
| `class B extends A` | `` `extends` is not supported: Nish has no inheritance `` | repeat the fields and `implements` an interface |
|
|
202
202
|
| `static` members, `get x()` / `set x(v)` | `` … `static` members are not supported `` / `` Getters and setters are not supported in class `C` (use a method) `` | module `const`s and plain methods |
|
|
203
|
-
| `
|
|
203
|
+
| `[a, b] = [b, a]`, `const { x = 0 } = p`, `({ x }: Point) =>`, `(n?: i32)`, `(n = 1)`, `f(...xs)` | `Destructuring assignment is not supported` / `A default in a destructuring pattern is not supported` / `Destructured parameters are not supported` / `Optional parameters are not supported` / `` Parameter `n` has a default and no type annotation `` / `Spread arguments are not supported` | one name per binding; `n: i32 = 1` (a constant or an earlier parameter) for a default, `o: Opts | null = null` for an options argument; `...ns: i32[]` as the last parameter, or pass the array itself |
|
|
204
|
+
| `const { a, b } = config` at module level | `Destructured constants are not supported` | one module `const` per name (a module constant is a compile-time value folded into each use, with nothing at run time for a pattern to read), or destructure inside a function |
|
|
204
205
|
| `function f(): void;` (an overload), `export default function ()` | `Functions must have a body` / `Functions must be named` | one named function, with its body |
|
|
205
206
|
| a method in an `interface` | `` Interface `I` cannot declare methods (interfaces describe layout only) `` | fields only; a top-level function over the interface |
|
|
206
207
|
| `abstract class`, a `declare class` with methods, `constructor(public x: i32)`, `interface B extends A`, `[k: string]: T` | `Abstract classes are not supported` / `` `declare class Point` cannot have a method or accessor `` (a `declare class` is a C struct: fields only) / `Parameter properties … are not supported; declare the field and assign it` / ``Interface inheritance (`extends`) is not supported; list every field`` / `Index signatures are not supported …` | a plain class with its fields declared and assigned in the constructor; every field listed; a `Map` for a keyed table |
|
|
@@ -218,7 +219,7 @@ rejects. This table is the highest-value part of the page.
|
|
|
218
219
|
| `total += xs[i]` in a loop, with `i` from somewhere the compiler cannot bound | it compiles, with an `NL9007` performance warning: `` `i` is not proven to be in range for `xs` here, so this access keeps its bounds check `` | a guard before the statement, `if (!(i >= 0 && i < toI32(xs.length))) { panic("index out of range") }`, which proves both ends in either number mode; `nish --fix` inserts it where it changes nothing the program does |
|
|
219
220
|
| `new Date()` | `` `new Date()` is refused: Nish has no `Date` object; the one `Date` member is `Date.now()`, the wall clock in milliseconds `` | `Date.now()` for the wall clock, `monotonicNanos()` for elapsed time; there is no calendar |
|
|
220
221
|
| `JSON.parse`, `RegExp`, `/re/` literals, `Promise` | unknown / forbidden | none of these are built in: `regexCompile(pattern, flags)` from `nish/regex` for a pattern, `jsonDecode` from `nish/json-typed` for JSON; restructure around `Promise` |
|
|
221
|
-
| `for (const [k, v] of m)`, `m.
|
|
222
|
+
| `for (const [k, v] of m)`, `m.entries()`, `new Map(entries)` | each refused by name | `Map` and `Set` exist, with `size`, `get`, `set` / `add`, `has`, `delete`, `clear`, `forEach`, and `keys()` / `values()` in a `for...of` — see [Map and Set](#map-and-set) |
|
|
222
223
|
| `let v = m.get(k)`, `f(m.get(k))`, `m.get(k) + 1` | `` `m.get(k)` is `i32 \| undefined` and cannot be held in a `let` `` (and one message per place) | `m.get(k) ?? 0`, or `const v = m.get(k); if (v !== undefined) { … }` |
|
|
223
224
|
| `let x = 5; x = "s"` | `Cannot initialize …` | types never change and never convert implicitly |
|
|
224
225
|
|
|
@@ -293,6 +294,7 @@ compatible only when their types are identical.
|
|
|
293
294
|
| `Int32Array`, `Float32Array`, `Float64Array`, `BigInt64Array` | aliases of `i32[]`, `f32[]`, `f64[]`, `i64[]` | not distinct types; fixed length, so no `push` or `pop` (NL2415): declare `f64[]` to grow one |
|
|
294
295
|
| `class C`, `interface I` | a struct, fields in declaration order | no header, no vtable |
|
|
295
296
|
| `enum K` | a **distinct** type represented as `i32` | never interchangeable with `i32` |
|
|
297
|
+
| `enum K { A = "a" }` | a string enum: a distinct type, its member's ordinal as `i32` | never interchangeable with `string`; `String(k)` reads the member's string |
|
|
296
298
|
| `K.M` | an enum member type: the one member `K.M`, as `i32` | holds only that member; a read is a `K` |
|
|
297
299
|
| `T \| null` | `T` a class, interface, array, string or function reference | a scalar can never be nullable |
|
|
298
300
|
| `A \| B` of tagged variants | a pointer to the variant; its first field is the tag | [Discriminated unions](#discriminated-unions); only the tag is read until a test of it narrows |
|
|
@@ -918,8 +920,9 @@ export const main = (): i32 => {
|
|
|
918
920
|
read `this` (bind `const self = this`), or read a function-typed parameter
|
|
919
921
|
of its function. An arrow inside another arrow, or given to a method, a
|
|
920
922
|
constructor, `expose` or `exposeWith`, captures nothing; the callbacks of
|
|
921
|
-
the array methods `sort`, `map`, `filter`, `some`, `every
|
|
922
|
-
are the exception, and capture
|
|
923
|
+
the array methods `sort`, `map`, `filter`, `some`, `every`, `findIndex` and
|
|
924
|
+
`forEach`, and of `Map` and `Set` `forEach`, are the exception, and capture
|
|
925
|
+
as an arrow argument does.
|
|
923
926
|
- **The argument's type must match exactly**: `(x: i32) => i64` for a
|
|
924
927
|
`(x: i32) => i32` parameter is refused. A generic function cannot be passed
|
|
925
928
|
by name — write `(x) => identity(x)`.
|
|
@@ -1097,8 +1100,8 @@ export const main = (): i32 => {
|
|
|
1097
1100
|
NL9012: each element pays for building it and for the release. Storing an
|
|
1098
1101
|
allocation into another object (NL2352) and touching `Arena` (NL2351) are
|
|
1099
1102
|
refused, because either would make that release unsound.
|
|
1100
|
-
- **A map's result is a number, a `boolean`, an enum
|
|
1101
|
-
of numbers, booleans or enums**: a string or a scalar array is copied out of
|
|
1103
|
+
- **A map's result is a number, a `boolean`, an enum (of either kind), a
|
|
1104
|
+
`string` or an array of numbers, booleans or enums**: a string or a scalar array is copied out of
|
|
1102
1105
|
the worker's arena at the join. An array of strings, a nested array, a class,
|
|
1103
1106
|
a `Map` or a `Set` is refused (NL2348): answer a string or a number per
|
|
1104
1107
|
element and build the structure after the call. **A reduce's `T` stays a number, a
|
|
@@ -1174,7 +1177,7 @@ export const main = (): i32 => {
|
|
|
1174
1177
|
above the scope and `s.spawn(entry, arg, answer, 0)`. Either follows the
|
|
1175
1178
|
data-parallel rules: it writes nothing anybody else can see but what it
|
|
1176
1179
|
writes through a `Mutex`'s guard or sends on a `Channel` (below), answers a number,
|
|
1177
|
-
a `boolean`, an enum, a `string` or an array of numbers, booleans or enums
|
|
1180
|
+
a `boolean`, an enum of either kind, a `string` or an array of numbers, booleans or enums
|
|
1178
1181
|
(copied out of its arena at the join), and leaves `Arena` alone. A task cannot open a scope
|
|
1179
1182
|
of its own or call `parallelMapInto`.
|
|
1180
1183
|
- **Any argument will do** — an array, an object — because nothing writes
|
|
@@ -1241,7 +1244,7 @@ export const main = (): i32 => {
|
|
|
1241
1244
|
```
|
|
1242
1245
|
|
|
1243
1246
|
- **Only `g.value`, and only as the base of a field or element access.** Read
|
|
1244
|
-
a number, a `boolean`, an enum or a `string` through it, or store one into a
|
|
1247
|
+
a number, a `boolean`, an enum of either kind or a `string` through it, or store one into a
|
|
1245
1248
|
field or an element that already exists (a string is copied each way) (for a `Mutex<i32>`, `g.value = g.value + 1`).
|
|
1246
1249
|
Don't bind, store, return or walk `g` itself (NL2466), nor bind, store, return
|
|
1247
1250
|
or walk `g.value` or an array it holds (NL2467); store only a scalar or a
|
|
@@ -1338,6 +1341,12 @@ export const main = (): i32 => {
|
|
|
1338
1341
|
- **Add once per batch**: count into a local array, then one `add` per bin.
|
|
1339
1342
|
One `add` per value makes the cores wait on each other.
|
|
1340
1343
|
- **`AtomicI64` takes `i64` values**, exact under Node within ±2^53.
|
|
1344
|
+
- **Many counters — a histogram, a count per route — go in one atomic
|
|
1345
|
+
array**: `new AtomicI32Array(16)` (or `AtomicI64Array`) is 16 counters in
|
|
1346
|
+
one buffer, each `0`; every method takes the index first,
|
|
1347
|
+
`bins.add(b, 1)`, `bins.load(b)`, and checks it as an array index. Prefer it
|
|
1348
|
+
to an `AtomicI32[]`, whose counters are separate objects on separate cache
|
|
1349
|
+
lines. Same rules as above.
|
|
1341
1350
|
|
|
1342
1351
|
```ts nish:err NL2513
|
|
1343
1352
|
import { AtomicI32, scope } from "nish/threads";
|
|
@@ -1406,7 +1415,7 @@ export const main = (): i32 => {
|
|
|
1406
1415
|
};
|
|
1407
1416
|
```
|
|
1408
1417
|
|
|
1409
|
-
- **It carries a number, a `boolean`, an enum or a `string`** (copied in by
|
|
1418
|
+
- **It carries a number, a `boolean`, an enum of either kind or a `string`** (copied in by
|
|
1410
1419
|
`send`, out by the receive), never an array or an object.
|
|
1411
1420
|
- **Declare it as a `const` right before its scope** — in the block that holds
|
|
1412
1421
|
`using s = scope()`, or in the block around the scope's block — and hand it
|
|
@@ -1468,7 +1477,9 @@ export const main = (): i32 => {
|
|
|
1468
1477
|
|
|
1469
1478
|
- **Crosses, by copy:** a number, a `boolean`, an enum, a `string`, an array
|
|
1470
1479
|
of numbers, booleans or enums — a task's answer, a `parallelMap` element, a
|
|
1471
|
-
`Channel<string>` value, a string stored through a guard.
|
|
1480
|
+
`Channel<string>` value, a string stored through a guard. A string enum
|
|
1481
|
+
crosses as its `i32` ordinal, like a numeric one: its strings sit in a
|
|
1482
|
+
constant table every thread reads. Each thread moves
|
|
1472
1483
|
the `parallelMap` elements it answers into blocks of its own, and the caller
|
|
1473
1484
|
takes the blocks whole at the join; a task's answer under 64 KB is copied in
|
|
1474
1485
|
by the joining thread. On one thread every element still pays the move, so
|
|
@@ -1618,7 +1629,8 @@ export const main = (): i32 => {
|
|
|
1618
1629
|
freed.** `readCString(p: CPtr): string` copies the NUL-terminated bytes at
|
|
1619
1630
|
`p` into a new string; a dangling or unterminated `p` is undefined
|
|
1620
1631
|
behaviour, which is why it needs the import. It takes a `CPtr`, so narrow a
|
|
1621
|
-
`CPtr | null` with `!== null` first.
|
|
1632
|
+
`CPtr | null` with `!== null` first. A `NULL` `p` panics (`ffi-null`) before
|
|
1633
|
+
the copy, so `--deny-panics` refuses the call.
|
|
1622
1634
|
- **A struct C owns is copied out with `readStruct` from `nish:unsafe`.**
|
|
1623
1635
|
`const lc: Lconv = readStruct(p)` copies the bytes at `p` into a new
|
|
1624
1636
|
instance of the `declare class` the context names; write the annotation,
|
|
@@ -2001,6 +2013,43 @@ export const main = (): i32 => {
|
|
|
2001
2013
|
point: `Kind` and `i32` never convert in either direction. Members must be
|
|
2002
2014
|
numeric literals. `===` and `!==` are the **only** operators — no arithmetic,
|
|
2003
2015
|
no bitwise, so **bit flags stay `i32` module constants**.
|
|
2016
|
+
- A **string enum** gives every member a string literal (`Admin = "admin"`),
|
|
2017
|
+
and is just as distinct: it is stored as its member's `i32` ordinal, `===`,
|
|
2018
|
+
`!==` and `switch` compare ordinals, and `String(r)` is the only way to its
|
|
2019
|
+
string (one load from a constant table, no allocation). `Role` and `string`
|
|
2020
|
+
never convert either way — `r === "admin"` is NL2788,
|
|
2021
|
+
`const s: string = r` and `` `${r}` `` NL2789, `const r: Role = "admin"`
|
|
2022
|
+
NL2790 — because a string compare or a hidden conversion is exactly the cost
|
|
2023
|
+
the ordinal exists to avoid: compare members, or write `String(r)` once.
|
|
2024
|
+
Mixing string and numeric members is NL1043, a member without a value
|
|
2025
|
+
NL1064, a template or computed value NL1065/NL1066, two members of one string
|
|
2026
|
+
NL2786, a value that is not valid UTF-8 NL2787. A `Role | null` is NL2010, as for every enum. There is no
|
|
2027
|
+
`Role.parse` yet: test `s === String(Role.Admin)` per member, or decode it
|
|
2028
|
+
with typed JSON.
|
|
2029
|
+
|
|
2030
|
+
```ts nish:ok
|
|
2031
|
+
enum Role {
|
|
2032
|
+
Admin = "admin",
|
|
2033
|
+
Guest = "guest",
|
|
2034
|
+
}
|
|
2035
|
+
|
|
2036
|
+
const parse = (s: string): Role => (s === String(Role.Admin) ? Role.Admin : Role.Guest);
|
|
2037
|
+
|
|
2038
|
+
const rank = (r: Role): i32 => {
|
|
2039
|
+
switch (r) {
|
|
2040
|
+
case Role.Admin:
|
|
2041
|
+
return 2;
|
|
2042
|
+
default:
|
|
2043
|
+
return 1;
|
|
2044
|
+
}
|
|
2045
|
+
};
|
|
2046
|
+
|
|
2047
|
+
export const main = (): i32 => {
|
|
2048
|
+
const r = parse("admin");
|
|
2049
|
+
console.log(`${String(r)} ${rank(r)}`);
|
|
2050
|
+
return r === Role.Admin ? 0 : 1;
|
|
2051
|
+
};
|
|
2052
|
+
```
|
|
2004
2053
|
- **A member is also a type.** `Kind.While` as an annotation is a slot that
|
|
2005
2054
|
holds exactly that member — the shape of a tag field that says which case
|
|
2006
2055
|
its object is. Only the member's own constant (or a value already of that
|
|
@@ -2113,10 +2162,14 @@ export const main = (): i32 => {
|
|
|
2113
2162
|
other union is still NL1053.
|
|
2114
2163
|
- A union is a local, parameter, field, return, array element, or nullable
|
|
2115
2164
|
(`Shape | null`), and a `Map`'s value is any of those (`Map<string, Shape>`,
|
|
2116
|
-
`Map<string, Shape[]>`, `Map<string, Shape | null>`)
|
|
2165
|
+
`Map<string, Shape[]>`, `Map<string, Shape | null>`), and a type argument:
|
|
2166
|
+
`Box<Shape>`, `Stack<Shape>`, a generic call binding `T` to a union,
|
|
2167
|
+
`Result<Shape, E>` / `Result<T, Shape>`, and `nish/map`'s `getOrInsert` /
|
|
2168
|
+
`reserve` on a `Map<K, Shape>`. An alias may name another union, which is flattened. A
|
|
2117
2169
|
variant, or a smaller union, converts into it wherever the union is
|
|
2118
2170
|
expected — argument, `return`, `push`, an annotated array literal, a
|
|
2119
|
-
ternary arm or `??` default whose other side is the union
|
|
2171
|
+
ternary arm or `??` default whose other side is the union, and a generic's
|
|
2172
|
+
`T` bound to it (`new Box<Shape>(new Circle())`, `Ok(new Square())`). A class that
|
|
2120
2173
|
implements an interface variant converts to that interface first. A union
|
|
2121
2174
|
crosses modules but not a sidecar (NL4009).
|
|
2122
2175
|
- **Test the tag to read a variant.** Before a test, the tag is the only
|
|
@@ -2184,12 +2237,13 @@ export const main = (): i32 => {
|
|
|
2184
2237
|
falls back to every member the body stored
|
|
2185
2238
|
anywhere or the whole type, which accepts a few comparisons `tsc` refuses
|
|
2186
2239
|
(TS2367; LANGUAGE.md, "Accepted here, refused by `tsc`").
|
|
2187
|
-
- **Refused by design:**
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2240
|
+
- **Refused by design:** unions as `Map`/`Set` keys (NL2353), a generic
|
|
2241
|
+
class as a variant (NL2522), covariant arrays (`Circle[]` is not
|
|
2242
|
+
`Shape[]`), inferred unions (an unannotated ternary or array of two
|
|
2243
|
+
variants, or one a generic call would infer: NL2172), a union meeting a
|
|
2244
|
+
constraint `<T extends I>` (NL2328), and a union inside a `Secret`
|
|
2245
|
+
(NL2428), a typed-JSON class (NL2486) or a C header / N-API signature
|
|
2246
|
+
(NL4009), generic instance or not.
|
|
2193
2247
|
- **Declare what you store.** A store of a value that is only some of the
|
|
2194
2248
|
variants into a union slot is NL2534, because `tsc` would narrow the slot
|
|
2195
2249
|
from there on. Declare the slot as the variant, or store a value of the
|
|
@@ -2248,7 +2302,9 @@ export const main = (): i32 => {
|
|
|
2248
2302
|
- Functions and enums may be renamed on import; classes and interfaces may
|
|
2249
2303
|
not — the type name is part of the ABI.
|
|
2250
2304
|
- Import cycles are allowed; a shared dependency is compiled once.
|
|
2251
|
-
- **A function name is unique across the whole program**, exported or not
|
|
2305
|
+
- **A function name is unique across the whole program**, exported or not
|
|
2306
|
+
(a proposal to relax this for private functions is
|
|
2307
|
+
[WP57](https://github.com/amritk/nish/blob/main/docs/wp57-module-scoped-names.md); it has not shipped).
|
|
2252
2308
|
- An `export`ed function is an external C-ABI symbol; every other function gets
|
|
2253
2309
|
`internal` linkage so LLVM may inline or drop it.
|
|
2254
2310
|
- **So an exported function is not named after a C library function**
|
|
@@ -2271,7 +2327,16 @@ export const main = (): i32 => {
|
|
|
2271
2327
|
- `let` / `const` need an initializer and take their type from it; an
|
|
2272
2328
|
annotation must match **exactly**. `const` freezes the binding, not the
|
|
2273
2329
|
contents: `xs[0] = 1` and `xs.push(1)` on a `const xs` are fine.
|
|
2274
|
-
- `var` is forbidden.
|
|
2330
|
+
- `var` is forbidden.
|
|
2331
|
+
- A local or a `for...of` head may destructure: `const { x, y: py } = p`,
|
|
2332
|
+
`let [a, , c] = xs`, `for (const { x, y } of points)`. Each name is one
|
|
2333
|
+
field read, or one bounds-checked element read (past the end panics; it is
|
|
2334
|
+
never `undefined`). No defaults, rests, nested patterns, computed keys or
|
|
2335
|
+
assignment patterns; no pattern over a `Map`, a `string` or a nullable
|
|
2336
|
+
value, and no pattern as a parameter (`const { w, h } = s` over a
|
|
2337
|
+
parameter `s` is fine). A module constant may not destructure either, because a
|
|
2338
|
+
module constant is a compile-time value folded into each use, and a
|
|
2339
|
+
pattern's names are reads of a receiver at run time.
|
|
2275
2340
|
- **Semicolons are optional**, where TypeScript would insert one: at a line
|
|
2276
2341
|
break, before `}` and at the end of the file. It is TypeScript's rule, so a
|
|
2277
2342
|
line starting with `(` or `[` continues the one before it, and `return`
|
|
@@ -2723,7 +2788,9 @@ order, SameValueZero keys (`-0` is `+0`, `NaN` finds `NaN`). What they have is
|
|
|
2723
2788
|
exactly `size` (a read-only `number`), `get(k)`, `set(k, v)` / `add(x)` (both
|
|
2724
2789
|
answer the receiver, so they chain), `has(k)`, `delete(k)` (answers whether it
|
|
2725
2790
|
was there) and `clear()`, and `keys()` / `values()` as the iterable of a
|
|
2726
|
-
`for...of` and nowhere else
|
|
2791
|
+
`for...of` and nowhere else, and `forEach(f)`: `m.forEach((value, key) => ...)`
|
|
2792
|
+
and `s.forEach((x) => ...)` compile to that `for...of` walk with `f` inlined,
|
|
2793
|
+
and answer `void`. `entries` is refused by name.
|
|
2727
2794
|
|
|
2728
2795
|
```ts nish:ok-body
|
|
2729
2796
|
const seen = new Set<string>();
|
|
@@ -2799,8 +2866,8 @@ console.log(`${total}`); // 121
|
|
|
2799
2866
|
|
|
2800
2867
|
- An iterator is not a value: `keys()` / `values()` stored, passed, returned or
|
|
2801
2868
|
spread are refused. Pass the map itself and walk it where it is used.
|
|
2802
|
-
- `for (const e of m)` over a `Map` itself is refused, because
|
|
2803
|
-
`[key, value]`
|
|
2869
|
+
- `for (const e of m)` over a `Map` itself is refused, because its
|
|
2870
|
+
`[key, value]` pairs are two-type tuples: walk `m.keys()` and call `m.get(k)`.
|
|
2804
2871
|
|
|
2805
2872
|
```ts nish:err-body NL2375
|
|
2806
2873
|
const m = new Map<string, i32>();
|
|
@@ -2843,14 +2910,18 @@ export const main = (): i32 => {
|
|
|
2843
2910
|
|
|
2844
2911
|
`jsonDecode(d, out)` reads one JSON object into a class and `jsonEncode(w,
|
|
2845
2912
|
value)` writes one; the compiler writes the code at each call from the class's
|
|
2846
|
-
fields. A field is `i32`, `u32`, `i64`, `u64`, `f64`, `boolean`, `string`,
|
|
2847
|
-
|
|
2913
|
+
fields. A field is `i32`, `u32`, `i64`, `u64`, `f64`, `boolean`, `string`, an
|
|
2914
|
+
enum, a class, an array of one of those, a `Map<string, V>` of any of
|
|
2848
2915
|
those, or a `string`, class, array or map `| null`; anything else is refused at
|
|
2849
2916
|
the call (NL2485), a `Map` with non-`string` keys and a `Set` included, and so
|
|
2850
2917
|
is a value that is not a class (NL2486). Decoding is strict: every field
|
|
2851
2918
|
required, each key once, an unknown key an error unless `d.ignoreUnknown`, and
|
|
2852
2919
|
every outcome a sticky negative code in `d.failed`, never a panic. An enum is
|
|
2853
|
-
|
|
2920
|
+
read and written as a numeric one's number or a string one's string, and a
|
|
2921
|
+
value that is no member's is `JSON_DECODE_TYPE` (the field keeps its member) —
|
|
2922
|
+
so a string enum field is how a program checks a closed set of strings from
|
|
2923
|
+
input (`role: Role`, `{"role":"admin"}`); a `Role | null` field is NL2010 and a
|
|
2924
|
+
`Map` keyed by an enum NL2485; a
|
|
2854
2925
|
`u64` is exact to 2^64 - 1, and negative is `JSON_DECODE_RANGE`; a map is
|
|
2855
2926
|
emptied and filled in the input's key order, a repeated key an error, and
|
|
2856
2927
|
more keys over all the maps of one decode than `d.maxMapKeys` (4,096 unless
|
|
@@ -2868,7 +2939,8 @@ a rename to a non-string (NL2496) or to a key that is not valid UTF-8
|
|
|
2868
2939
|
`JsonNumber` field keeps a number's text byte for byte (`text`, `i64`, `u64`,
|
|
2869
2940
|
`f64`, `setText`). A tagged union of classes is a field, an array element or
|
|
2870
2941
|
`jsonEncode`'s value too: the object with its tag key first, the tag the
|
|
2871
|
-
member's name or the variant's `tag`
|
|
2942
|
+
member's name (a string enum's member's string) or the variant's `tag`
|
|
2943
|
+
option. Decode one with
|
|
2872
2944
|
`const u = jsonDecodeUnion(d, prev)` (never `jsonDecode`, NL2719, and no type
|
|
2873
2945
|
argument): it answers `prev` refilled when the tag matches, else a new variant,
|
|
2874
2946
|
and `prev` on failure (only at the top: a nested union slot may hold the new
|
|
@@ -3010,7 +3082,14 @@ every pass's string, because nothing frees a value a later pass replaced:
|
|
|
3010
3082
|
value with `const` inside the loop when only the pass reads it, or keep
|
|
3011
3083
|
numbers across passes and build the value once after the loop. A server, or
|
|
3012
3084
|
anything that runs for a long time, should compile with `--deny-retention`,
|
|
3013
|
-
which makes this warning and the
|
|
3085
|
+
which makes this warning and the four like it errors.
|
|
3086
|
+
|
|
3087
|
+
**Build a request's temporaries in an arena block.** A pass of a loop with
|
|
3088
|
+
no bound on its passes (`while (true)`, a server's poll loop) that calls
|
|
3089
|
+
`accept`, `respond` or anything else that stores is refused its own scope,
|
|
3090
|
+
so a reply string it builds stays until the loop ends: NL9018. Build it in
|
|
3091
|
+
`{ using a = arena(); ... }` inside the loop body, copy what must outlive the
|
|
3092
|
+
block into a buffer made before the loop, and call what stores after it.
|
|
3014
3093
|
|
|
3015
3094
|
Everything else is bumped from the arena, which is released when `main`
|
|
3016
3095
|
returns. To give memory back sooner, scope it with `using a = arena()`;
|
|
@@ -3059,7 +3138,7 @@ the module (NL4015).
|
|
|
3059
3138
|
|
|
3060
3139
|
## Recipes for what is missing
|
|
3061
3140
|
|
|
3062
|
-
**Iterate and accumulate** — there is no `reduce`
|
|
3141
|
+
**Iterate and accumulate** — there is no `reduce` (`map`, `filter`, `forEach`, `some`, `every`, `findIndex` and `slice` work, but a callback may not assign a local of its caller); fold in a loop:
|
|
3063
3142
|
|
|
3064
3143
|
```ts nish:ok-body
|
|
3065
3144
|
const xs: i32[] = [1, 2, 3];
|