nish 0.42.0 → 0.45.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -5
- package/docs/AI.md +287 -129
- package/package.json +9 -9
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/skills/writing-nish/SKILL.md +12 -4
- package/std/README.md +6 -6
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +163 -39
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +141 -0
- package/std/crypto/sha1.ts +157 -57
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1276 -218
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +104 -22
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +324 -121
- package/std/net/quic-listener.ts +206 -91
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- package/std/threads.ts +22 -2
package/docs/AI.md
CHANGED
|
@@ -130,11 +130,16 @@ are one node per line, which is to say diffable.
|
|
|
130
130
|
|
|
131
131
|
## A whole program
|
|
132
132
|
|
|
133
|
-
|
|
133
|
+
This program builds under the default: its `%` sits behind the loop's
|
|
134
|
+
`y > 0` test ([Proving it cannot panic](#proving-it-cannot-panic)). Examples
|
|
135
|
+
below that do integer arithmetic nothing bounds build with `--allow-panics`.
|
|
136
|
+
|
|
137
|
+
<!-- nish:ok -->
|
|
138
|
+
```ts
|
|
134
139
|
const gcd = (a: i32, b: i32): i32 => {
|
|
135
140
|
let x = a;
|
|
136
141
|
let y = b;
|
|
137
|
-
while (y
|
|
142
|
+
while (y > 0) {
|
|
138
143
|
const t = y;
|
|
139
144
|
y = x % y;
|
|
140
145
|
x = t;
|
|
@@ -202,7 +207,7 @@ rejects. This table is the highest-value part of the page.
|
|
|
202
207
|
| `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
208
|
| `[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
209
|
| `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 |
|
|
205
|
-
| `function f(): void;` (an overload), `export default function ()` | `Functions must have a body` / `Functions must be named` | one named function, with its body |
|
|
210
|
+
| `function f(): void;` (an overload), `export default function ()` | `Functions must have a body` / `Functions must be named` | one named function, with its body (overload signatures compile under [the compatibility dialect](guide/using-nish/compat.md)'s `overloads` feature) |
|
|
206
211
|
| a method in an `interface` | `` Interface `I` cannot declare methods (interfaces describe layout only) `` | fields only; a top-level function over the interface |
|
|
207
212
|
| `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 |
|
|
208
213
|
| `{ m() { … } }`, `{ "a": 1 }`, `const C = class { }` | ``Unsupported object literal member: MethodDeclaration (only `key: value`)`` / `Object literal keys must be plain identifiers` / `Classes must be named` | fields with identifier keys, a top-level function over the interface, a named top-level class |
|
|
@@ -210,7 +215,7 @@ rejects. This table is the highest-value part of the page.
|
|
|
210
215
|
| `{ [k]: v }`, `[k]: i32` in a class or interface | `Computed property names are forbidden in Nish (member names are fixed at compile time)` | a `Map` for a keyed table |
|
|
211
216
|
| `type Pair = { a: i32 }` (inline object type) | `` Unsupported type `{ a: i32 }` `` | declare an `interface` |
|
|
212
217
|
| `A \| B` unions of anything but tagged classes and interfaces | `` Union types other than `T \| null` are forbidden `` | a [discriminated union](#discriminated-unions): a tag field first in each variant |
|
|
213
|
-
| `String(n)`, `n.toString()` | `` Unknown function `String` `` / `` Unknown method `toString` on i32 `` | `` `${n}` `` |
|
|
218
|
+
| `String(n)`, `n.toString()` | `` Unknown function `String` `` / `` Unknown method `toString` on i32; use a template literal: `${n}` `` | `` `${n}` `` |
|
|
214
219
|
| `xs.length = 0` | `` Cannot assign to `length` of i32[] (array length is read-only; use `push`) `` | build a new array |
|
|
215
220
|
| `import { readFileSync } from "fs"` | `` Cannot find package `fs` `` — a bare specifier is a **package name**, so it is looked for in `node_modules`; one that is installed but has no `nish` condition is `` Package `fs` has no Nish entry point `` | `readFileSync` is a global; no import needed (or `import { readFileSync } from "nish:fs"`) |
|
|
216
221
|
| `export default f` | `` `export default` / `export =` are not supported; use a named `export function` `` | `export const f = …` |
|
|
@@ -225,14 +230,16 @@ rejects. This table is the highest-value part of the page.
|
|
|
225
230
|
|
|
226
231
|
A worked pair. This is the single most common rejection:
|
|
227
232
|
|
|
228
|
-
|
|
233
|
+
<!-- nish:err-body NL2188 -->
|
|
234
|
+
```ts
|
|
229
235
|
const xs = [1, 2, 3];
|
|
230
236
|
if (xs.length) {
|
|
231
237
|
console.log("non-empty");
|
|
232
238
|
}
|
|
233
239
|
```
|
|
234
240
|
|
|
235
|
-
|
|
241
|
+
<!-- nish:ok-body -->
|
|
242
|
+
```ts
|
|
236
243
|
const xs = [1, 2, 3];
|
|
237
244
|
if (xs.length !== 0) {
|
|
238
245
|
console.log("non-empty");
|
|
@@ -256,7 +263,9 @@ only once the program has no other error, so they show up last.
|
|
|
256
263
|
| `if (c) … else if (c) …`, the same condition again | NL2716 | the condition you meant for the second branch |
|
|
257
264
|
| `u < 0` or `u >= 0` on a `u8`, `u16`, `u32`, `u64` | NL2717 | test before the subtraction that would wrap, or use a signed type |
|
|
258
265
|
|
|
259
|
-
|
|
266
|
+
<!-- allow-panics: it shows NL2712, not a panic site; does integer arithmetic nothing bounds -->
|
|
267
|
+
<!-- nish:err-body NL2712 --allow-panics -->
|
|
268
|
+
```ts
|
|
260
269
|
const xs = [3, 4];
|
|
261
270
|
let total = 0;
|
|
262
271
|
for (const x of xs) {
|
|
@@ -266,7 +275,9 @@ const average = total / 2;
|
|
|
266
275
|
console.log(`${total}`);
|
|
267
276
|
```
|
|
268
277
|
|
|
269
|
-
|
|
278
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
279
|
+
<!-- nish:ok-body --allow-panics -->
|
|
280
|
+
```ts
|
|
270
281
|
const xs = [3, 4];
|
|
271
282
|
let total = 0;
|
|
272
283
|
for (const x of xs) {
|
|
@@ -285,7 +296,7 @@ compatible only when their types are identical.
|
|
|
285
296
|
| Type | Is | Notes |
|
|
286
297
|
| --- | --- | --- |
|
|
287
298
|
| `number` | `i32`, or `f64` under `--number-mode f64` | the mode's default width |
|
|
288
|
-
| `i32`, `i64` | signed integers | overflow **panics** (exit 1)
|
|
299
|
+
| `i32`, `i64` | signed integers | overflow not proven to fit is a compile error by default, and **panics** (exit 1) under `--allow-panics`; `wrappingAdd`/`wrappingSub`/`wrappingMul` from `nish:unsafe` wrap |
|
|
289
300
|
| `u8`, `u16`, `u32`, `u64` | unsigned integers | arithmetic **always** wraps; `>>` is logical |
|
|
290
301
|
| `f32`, `f64` | IEEE-754 | `f32` is never the lowering of `number` |
|
|
291
302
|
| `boolean` | `i1` | no truthiness anywhere |
|
|
@@ -311,7 +322,8 @@ left ([Calling C](#calling-c)). Calling one without the import is NL2456. No fla
|
|
|
311
322
|
whole program: `--unchecked-indexing` and `--wrapping` were removed before 1.0
|
|
312
323
|
and are refused with exit 2.
|
|
313
324
|
|
|
314
|
-
|
|
325
|
+
<!-- nish:ok -->
|
|
326
|
+
```ts
|
|
315
327
|
import { uncheckedGet, wrappingAdd, wrappingMul } from "nish:unsafe";
|
|
316
328
|
|
|
317
329
|
export const hash = (bytes: u8[]): i32 => {
|
|
@@ -327,7 +339,9 @@ export const hash = (bytes: u8[]): i32 => {
|
|
|
327
339
|
`u32 + i32` (same LLVM type, different signedness) are rejected. Convert
|
|
328
340
|
explicitly: `toI32` `toI64` `toU8` `toU16` `toU32` `toU64` `toF32` `toF64`.
|
|
329
341
|
|
|
330
|
-
**Signed overflow panics.**
|
|
342
|
+
**Signed overflow panics.** By default an operation the compiler cannot
|
|
343
|
+
prove fits is a compile error (NL2457). Under `--allow-panics`, `+ - *`,
|
|
344
|
+
unary `-`, `++`, `--` and `op=` on an
|
|
331
345
|
`i32` or `i64` stop the program with `attempt to add with overflow` (or
|
|
332
346
|
`subtract`, `multiply`, `negate`) and exit 1 when the result leaves the type;
|
|
333
347
|
it is never a silent wrap and never undefined. The compiler drops the check
|
|
@@ -337,7 +351,8 @@ that is *meant* to wrap — a hash, a PRNG, a checksum — goes in `u32` or `u64
|
|
|
337
351
|
which always wrap and are never checked; convert back with `toI32` (free), or
|
|
338
352
|
call `wrappingAdd` / `wrappingSub` / `wrappingMul` at the site.
|
|
339
353
|
|
|
340
|
-
|
|
354
|
+
<!-- nish:ok -->
|
|
355
|
+
```ts
|
|
341
356
|
// FNV-1a: the multiply overflows on purpose, so the state is a `u32`.
|
|
342
357
|
export const fnv1a = (bytes: u8[]): u32 => {
|
|
343
358
|
let h: u32 = 2166136261;
|
|
@@ -348,13 +363,16 @@ export const fnv1a = (bytes: u8[]): u32 => {
|
|
|
348
363
|
};
|
|
349
364
|
```
|
|
350
365
|
|
|
351
|
-
|
|
366
|
+
<!-- nish:err NL2231 -->
|
|
367
|
+
```ts
|
|
352
368
|
const bad = (a: i32, b: i64): i64 => {
|
|
353
369
|
return a + b;
|
|
354
370
|
};
|
|
355
371
|
```
|
|
356
372
|
|
|
357
|
-
|
|
373
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
374
|
+
<!-- nish:ok --allow-panics -->
|
|
375
|
+
```ts
|
|
358
376
|
const good = (a: i32, b: i64): i64 => {
|
|
359
377
|
return toI64(a) + b;
|
|
360
378
|
};
|
|
@@ -363,8 +381,9 @@ const good = (a: i32, b: i64): i64 => {
|
|
|
363
381
|
**`integer<Lo, Hi>` is an `i32` that stays in `[Lo, Hi]`.** The bounds are two
|
|
364
382
|
integer literals (a sign allowed) inside `i32`, and it is an `i32` everywhere
|
|
365
383
|
the machine can see. Putting a value *into* one is checked: an `i32` or
|
|
366
|
-
another range is compared once
|
|
367
|
-
|
|
384
|
+
another range is compared once (a check the default refuses unless it is
|
|
385
|
+
proven; under `--allow-panics` the program panics, exit 1, outside the
|
|
386
|
+
range), a literal outside it is a compile error, and a literal inside it or a
|
|
368
387
|
narrower range costs nothing. So does a value a loop condition or a guard
|
|
369
388
|
already bounds (`b` below), and `toI32` of a `u8`; a check left inside a loop
|
|
370
389
|
is a performance warning (NL9013) naming the guard that removes it. A range
|
|
@@ -375,7 +394,9 @@ not an `integer<0, 255>` (convert with `toI32`), and a `declare function` may
|
|
|
375
394
|
not mention a range. Put the range on the value you use, not on a loop
|
|
376
395
|
counter, which has to leave the range to end the loop.
|
|
377
396
|
|
|
378
|
-
|
|
397
|
+
<!-- allow-panics: indexes an array without a guard the checker reads; does integer arithmetic nothing bounds -->
|
|
398
|
+
<!-- nish:ok --allow-panics -->
|
|
399
|
+
```ts
|
|
379
400
|
const getByte = (buf: u8[], i: integer<0, 255>): u8 => buf[i];
|
|
380
401
|
|
|
381
402
|
const sumAll = (buf: u8[]): i32 => {
|
|
@@ -388,7 +409,8 @@ const sumAll = (buf: u8[]): i32 => {
|
|
|
388
409
|
};
|
|
389
410
|
```
|
|
390
411
|
|
|
391
|
-
|
|
412
|
+
<!-- nish:err-body NL2384 -->
|
|
413
|
+
```ts
|
|
392
414
|
const digit: integer<0, 9> = 12;
|
|
393
415
|
```
|
|
394
416
|
|
|
@@ -396,7 +418,8 @@ Declaring a type alias, enum, class, interface or function named `integer` is
|
|
|
396
418
|
refused, because the name is the type. A value named `integer` (a local or a
|
|
397
419
|
module constant) is fine.
|
|
398
420
|
|
|
399
|
-
|
|
421
|
+
<!-- nish:err NL2332 -->
|
|
422
|
+
```ts
|
|
400
423
|
class integer {
|
|
401
424
|
value: i32 = 0;
|
|
402
425
|
}
|
|
@@ -418,7 +441,8 @@ Everything else leaves the literal at the default, however obvious the intent.
|
|
|
418
441
|
The three that catch you out: an **object-literal property**, a `Result`
|
|
419
442
|
payload, and a **method's** argument.
|
|
420
443
|
|
|
421
|
-
|
|
444
|
+
<!-- nish:err NL2269 -->
|
|
445
|
+
```ts
|
|
422
446
|
interface Pixel { r: u8; g: u8; b: u8; }
|
|
423
447
|
|
|
424
448
|
export const main = (): i32 => {
|
|
@@ -428,7 +452,8 @@ export const main = (): i32 => {
|
|
|
428
452
|
};
|
|
429
453
|
```
|
|
430
454
|
|
|
431
|
-
|
|
455
|
+
<!-- nish:ok -->
|
|
456
|
+
```ts
|
|
432
457
|
interface Pixel { r: u8; g: u8; b: u8; }
|
|
433
458
|
|
|
434
459
|
export const main = (): i32 => {
|
|
@@ -446,7 +471,8 @@ literals, not a literal in a `u8` context. Name the value first, or convert.
|
|
|
446
471
|
There is no `throw`, no `try`, and no unwinding anywhere in the language. A
|
|
447
472
|
function that can fail says so in its return type.
|
|
448
473
|
|
|
449
|
-
|
|
474
|
+
<!-- nish:ok -->
|
|
475
|
+
```ts
|
|
450
476
|
const half = (n: i32): Result<i32, string> => {
|
|
451
477
|
if (n % 2 !== 0) {
|
|
452
478
|
return Err("odd");
|
|
@@ -490,7 +516,8 @@ reference cannot hold.
|
|
|
490
516
|
is no implicit error conversion — convert by hand with
|
|
491
517
|
`if (r.isErr()) { return Err(…); }`.
|
|
492
518
|
|
|
493
|
-
|
|
519
|
+
<!-- nish:err NL2044 -->
|
|
520
|
+
```ts
|
|
494
521
|
const half = (n: i32): Result<i32, string> => {
|
|
495
522
|
return Ok(n / 2);
|
|
496
523
|
};
|
|
@@ -520,7 +547,9 @@ reference — never a scalar, which has no null value. It is the same pointer
|
|
|
520
547
|
(or, for a function reference, the same tag) with `null` as one more value, so
|
|
521
548
|
nothing is boxed.
|
|
522
549
|
|
|
523
|
-
|
|
550
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
551
|
+
<!-- nish:ok --allow-panics -->
|
|
552
|
+
```ts
|
|
524
553
|
class Node {
|
|
525
554
|
value: i32;
|
|
526
555
|
next: Node | null = null;
|
|
@@ -556,7 +585,8 @@ export const main = (): i32 => {
|
|
|
556
585
|
refused, because Node answers `d` past the end where Nish panics. `??` also
|
|
557
586
|
defaults a `Map.get` result (see [Map and Set](#map-and-set)).
|
|
558
587
|
|
|
559
|
-
|
|
588
|
+
<!-- nish:ok -->
|
|
589
|
+
```ts
|
|
560
590
|
class Node {
|
|
561
591
|
value: i32;
|
|
562
592
|
next: Node | null = null;
|
|
@@ -575,7 +605,8 @@ export const main = (): i32 => {
|
|
|
575
605
|
};
|
|
576
606
|
```
|
|
577
607
|
|
|
578
|
-
|
|
608
|
+
<!-- nish:err-body NL2489 -->
|
|
609
|
+
```ts
|
|
579
610
|
const names = new Array<string | null>(2);
|
|
580
611
|
console.log(names[2] ?? "none");
|
|
581
612
|
```
|
|
@@ -590,7 +621,8 @@ console.log(names[2] ?? "none");
|
|
|
590
621
|
`next` needs `a?.next?.v`. `?.` on something that cannot be `null` is
|
|
591
622
|
NL2490; `?.[i]` and `f?.()` are refused.
|
|
592
623
|
|
|
593
|
-
|
|
624
|
+
<!-- nish:ok -->
|
|
625
|
+
```ts
|
|
594
626
|
class Order {
|
|
595
627
|
id: i32;
|
|
596
628
|
next: Order | null = null;
|
|
@@ -609,7 +641,8 @@ export const main = (): i32 => {
|
|
|
609
641
|
};
|
|
610
642
|
```
|
|
611
643
|
|
|
612
|
-
|
|
644
|
+
<!-- nish:err-body NL1062 -->
|
|
645
|
+
```ts
|
|
613
646
|
const s: string | null = "x";
|
|
614
647
|
const n = s?.length;
|
|
615
648
|
```
|
|
@@ -625,7 +658,9 @@ const n = s?.length;
|
|
|
625
658
|
|
|
626
659
|
### Functions
|
|
627
660
|
|
|
628
|
-
|
|
661
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
662
|
+
<!-- nish:ok --allow-panics -->
|
|
663
|
+
```ts
|
|
629
664
|
const add = (a: i32, b: i32): i32 => {
|
|
630
665
|
return a + b;
|
|
631
666
|
};
|
|
@@ -647,7 +682,9 @@ const double = (n: i32): i32 => n * 2; // a concise body is that one `return`
|
|
|
647
682
|
into a `let` first.
|
|
648
683
|
- A non-`void` function must return on every path.
|
|
649
684
|
- Rejected: generators, a method that is `async`, destructured and optional
|
|
650
|
-
(`x?: T`) parameters, overloads, nested function declarations
|
|
685
|
+
(`x?: T`) parameters, overloads, nested function declarations ([the compatibility dialect](guide/using-nish/compat.md)
|
|
686
|
+
accepts `x?: T` read through `??`, a default that is not a constant and
|
|
687
|
+
overload signatures). A default
|
|
651
688
|
(`n: i32 = 1`) or a rest parameter (`...ns: i32[]`) is allowed on a
|
|
652
689
|
function, a method, a constructor or a module `const` arrow — see
|
|
653
690
|
"Optional parameters" in [Recipes for what is missing](#recipes-for-what-is-missing). Type parameters *are*
|
|
@@ -659,7 +696,8 @@ A function may declare type parameters, and **each instantiation is compiled to
|
|
|
659
696
|
its own specialised function** — no boxing, no dictionary, no runtime type
|
|
660
697
|
information. The IR is what somebody would have written by hand at that type.
|
|
661
698
|
|
|
662
|
-
|
|
699
|
+
<!-- nish:ok -->
|
|
700
|
+
```ts
|
|
663
701
|
const identity = <T>(x: T): T => x;
|
|
664
702
|
|
|
665
703
|
export const main = (): i32 => {
|
|
@@ -696,7 +734,8 @@ export const main = (): i32 => {
|
|
|
696
734
|
It is where the value came from that counts — a local copied from a `T`, an
|
|
697
735
|
element of a `T[]` and a narrowed `T | null` are all a `T` — never its type.
|
|
698
736
|
|
|
699
|
-
|
|
737
|
+
<!-- nish:err NL2329 -->
|
|
738
|
+
```ts
|
|
700
739
|
interface Point { x: i32; y: i32; }
|
|
701
740
|
|
|
702
741
|
const getX = <T>(p: T): i32 => p.x;
|
|
@@ -722,7 +761,9 @@ export const main = (): i32 => {
|
|
|
722
761
|
same fields, so implementing your own is refused the same way; import the
|
|
723
762
|
template's `Shape` instead.
|
|
724
763
|
|
|
725
|
-
|
|
764
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
765
|
+
<!-- nish:ok --allow-panics -->
|
|
766
|
+
```ts
|
|
726
767
|
interface Shape { area: i32; }
|
|
727
768
|
|
|
728
769
|
class Circle implements Shape {
|
|
@@ -761,7 +802,8 @@ export const main = (): i32 => areaOf(new Circle(2)) - 12;
|
|
|
761
802
|
identifier, such as a method `Point.shifted` and a function `Point_shifted`
|
|
762
803
|
(NL4008).
|
|
763
804
|
|
|
764
|
-
|
|
805
|
+
<!-- nish:err NL4007 --emit-dts build/docs-ai/nl4007.d.ts -->
|
|
806
|
+
```ts
|
|
765
807
|
export const identity = <T>(x: T): T => x; // exported, never called
|
|
766
808
|
|
|
767
809
|
export const main = (): i32 => 0;
|
|
@@ -773,7 +815,8 @@ A `class` or an `interface` may take type parameters too, and an instantiation
|
|
|
773
815
|
is **an ordinary struct**: `Box<i32>` is `%struct.Box$i32` with `Box`'s fields
|
|
774
816
|
at `T = i32`, and everything the language does to a struct works on it.
|
|
775
817
|
|
|
776
|
-
|
|
818
|
+
<!-- nish:ok -->
|
|
819
|
+
```ts
|
|
777
820
|
class Box<T> {
|
|
778
821
|
value: T;
|
|
779
822
|
constructor(v: T) { this.value = v; }
|
|
@@ -836,7 +879,9 @@ They are inferred from the arguments like a generic function's, and each
|
|
|
836
879
|
(receiver, method type arguments) pair is its own `define`:
|
|
837
880
|
`@Chooser.pick$i32`, `@Box$i32.keep$str`.
|
|
838
881
|
|
|
839
|
-
|
|
882
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
883
|
+
<!-- nish:ok --allow-panics -->
|
|
884
|
+
```ts
|
|
840
885
|
class Chooser {
|
|
841
886
|
flip: boolean = false;
|
|
842
887
|
pick<T>(a: T, b: T): T { return this.flip ? b : a; }
|
|
@@ -867,7 +912,8 @@ export const main = (): i32 => {
|
|
|
867
912
|
included.
|
|
868
913
|
- **A constructor takes none**; its class's are written after `new`.
|
|
869
914
|
|
|
870
|
-
|
|
915
|
+
<!-- nish:err NL2331 -->
|
|
916
|
+
```ts
|
|
871
917
|
class Box<T> {
|
|
872
918
|
value: T;
|
|
873
919
|
constructor(v: T) { this.value = v; }
|
|
@@ -884,7 +930,9 @@ is a top-level function named at the call, or an arrow written right there, and
|
|
|
884
930
|
each one is compiled into its **own copy** of the function, with a direct call
|
|
885
931
|
where the parameter is called — there is no function pointer at run time.
|
|
886
932
|
|
|
887
|
-
|
|
933
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
934
|
+
<!-- nish:ok --allow-panics -->
|
|
935
|
+
```ts
|
|
888
936
|
const map = <T, U>(xs: T[], f: (x: T) => U): U[] => {
|
|
889
937
|
const out: U[] = [];
|
|
890
938
|
for (const x of xs) {
|
|
@@ -936,7 +984,8 @@ export const main = (): i32 => {
|
|
|
936
984
|
or element is refused, NL2759: read it into a `const`); the runners that check their body (`parallelMap`,
|
|
937
985
|
`parallelReduce`, `spawn`, `expose`, `region`, ...) refuse one (NL2734).
|
|
938
986
|
|
|
939
|
-
|
|
987
|
+
<!-- nish:err NL2585 -->
|
|
988
|
+
```ts
|
|
940
989
|
const apply = (f: (x: i32) => i32, x: i32): i32 => f(x);
|
|
941
990
|
|
|
942
991
|
export const main = (): i32 => {
|
|
@@ -953,7 +1002,8 @@ through an alias. It can sit in a local, a field, an array element or a
|
|
|
953
1002
|
return value, be passed for an alias-typed parameter, called and compared
|
|
954
1003
|
with `===` / `!==`:
|
|
955
1004
|
|
|
956
|
-
|
|
1005
|
+
<!-- nish:ok -->
|
|
1006
|
+
```ts
|
|
957
1007
|
type Op = (x: i32) => i32;
|
|
958
1008
|
|
|
959
1009
|
const inc = (x: i32): i32 => x + 1;
|
|
@@ -1039,14 +1089,15 @@ that can fail answers `Promise<Result<T, E>>`: test the `Result` after the
|
|
|
1039
1089
|
before it returns; Node runs `f` only to its first `await` and the rest
|
|
1040
1090
|
after the spawning task yields. Do not rely on either order.
|
|
1041
1091
|
|
|
1042
|
-
|
|
1043
|
-
|
|
1092
|
+
<!-- nish:err NL2642 -->
|
|
1093
|
+
```ts
|
|
1094
|
+
const load = async (id: i32): Promise<i32> => {
|
|
1044
1095
|
return id;
|
|
1045
|
-
}
|
|
1096
|
+
};
|
|
1046
1097
|
|
|
1047
|
-
async
|
|
1098
|
+
const run = async (): Promise<void> => {
|
|
1048
1099
|
console.log(await load(1));
|
|
1049
|
-
}
|
|
1100
|
+
};
|
|
1050
1101
|
|
|
1051
1102
|
export const main = (): i32 => 0;
|
|
1052
1103
|
```
|
|
@@ -1061,7 +1112,9 @@ else to switch on and no thread count to choose. `parallelMap(src, f)` and
|
|
|
1061
1112
|
`parallelFrom(n, f)` answer a new array instead (`f(src[i])`, `f(i)`), under
|
|
1062
1113
|
the same rules for `f`.
|
|
1063
1114
|
|
|
1064
|
-
|
|
1115
|
+
<!-- allow-panics: calls `parallelMapInto`, whose length check no proof removes yet; calls `std/threads.ts` code that keeps panic sites no proof removes yet -->
|
|
1116
|
+
<!-- nish:ok --allow-panics -->
|
|
1117
|
+
```ts
|
|
1065
1118
|
import { parallelMapInto, parallelReduce } from "nish/threads";
|
|
1066
1119
|
|
|
1067
1120
|
const square = (x: f64): f64 => x * x;
|
|
@@ -1075,7 +1128,9 @@ export const main = (): i32 => {
|
|
|
1075
1128
|
};
|
|
1076
1129
|
```
|
|
1077
1130
|
|
|
1078
|
-
|
|
1131
|
+
<!-- allow-panics: indexes an array without a guard the checker reads; calls `std/threads.ts` code that keeps panic sites no proof removes yet -->
|
|
1132
|
+
<!-- nish:ok --allow-panics -->
|
|
1133
|
+
```ts
|
|
1079
1134
|
import { parallelFrom, parallelMap } from "nish/threads";
|
|
1080
1135
|
|
|
1081
1136
|
export const main = (): i32 => {
|
|
@@ -1120,7 +1175,8 @@ export const main = (): i32 => {
|
|
|
1120
1175
|
costs: a body estimated at 3, such as `(x) => x + 1`, is divided only past
|
|
1121
1176
|
about 700,000 elements, a cheaper one later, one with a loop far sooner.
|
|
1122
1177
|
|
|
1123
|
-
|
|
1178
|
+
<!-- nish:err NL2350 -->
|
|
1179
|
+
```ts
|
|
1124
1180
|
import { parallelReduce } from "nish/threads";
|
|
1125
1181
|
|
|
1126
1182
|
export const main = (): i32 => parallelReduce([1, 2, 3], (a, b) => a + b, 1); // `+` folds from 0
|
|
@@ -1132,7 +1188,9 @@ Different functions at once, one thread each: open a scope with `using`, give
|
|
|
1132
1188
|
it tasks with `spawn(entry, arg, dst, at)`, and every task has run and stored
|
|
1133
1189
|
`entry(arg)` into `dst[at]` when the block ends.
|
|
1134
1190
|
|
|
1135
|
-
|
|
1191
|
+
<!-- allow-panics: indexes an array without a guard the checker reads; does integer arithmetic nothing bounds -->
|
|
1192
|
+
<!-- nish:ok --allow-panics -->
|
|
1193
|
+
```ts
|
|
1136
1194
|
import { scope } from "nish/threads";
|
|
1137
1195
|
|
|
1138
1196
|
const sumOf = (xs: f64[]): f64 => xs[0] + xs[1];
|
|
@@ -1183,7 +1241,8 @@ export const main = (): i32 => {
|
|
|
1183
1241
|
- **Any argument will do** — an array, an object — because nothing writes
|
|
1184
1242
|
memory while the tasks run. There is no thread count: one task, one thread.
|
|
1185
1243
|
|
|
1186
|
-
|
|
1244
|
+
<!-- nish:err NL2388 -->
|
|
1245
|
+
```ts
|
|
1187
1246
|
import { scope } from "nish/threads";
|
|
1188
1247
|
|
|
1189
1248
|
const one = (n: i32): i32 => n + 1;
|
|
@@ -1201,7 +1260,9 @@ export const main = (): i32 => {
|
|
|
1201
1260
|
When tasks must update one value, put it in a `Mutex` and reach it only through
|
|
1202
1261
|
`using g = m.lock()`: the lock is held until that block ends, on every exit.
|
|
1203
1262
|
|
|
1204
|
-
|
|
1263
|
+
<!-- allow-panics: does integer arithmetic nothing bounds; indexes an array without a guard the checker reads -->
|
|
1264
|
+
<!-- nish:ok --allow-panics -->
|
|
1265
|
+
```ts
|
|
1205
1266
|
import { Mutex, scope } from "nish/threads";
|
|
1206
1267
|
|
|
1207
1268
|
class Tally {
|
|
@@ -1275,7 +1336,8 @@ export const main = (): i32 => {
|
|
|
1275
1336
|
not an `f64` sum): the critical sections run in the scheduler's order
|
|
1276
1337
|
natively and in spawn order under Node.
|
|
1277
1338
|
|
|
1278
|
-
|
|
1339
|
+
<!-- nish:err NL2467 -->
|
|
1340
|
+
```ts
|
|
1279
1341
|
import { Mutex } from "nish/threads";
|
|
1280
1342
|
|
|
1281
1343
|
class Counter {
|
|
@@ -1290,7 +1352,9 @@ export const main = (): i32 => {
|
|
|
1290
1352
|
};
|
|
1291
1353
|
```
|
|
1292
1354
|
|
|
1293
|
-
|
|
1355
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
1356
|
+
<!-- nish:ok --allow-panics -->
|
|
1357
|
+
```ts
|
|
1294
1358
|
import { Mutex, MutexGuard } from "nish/threads";
|
|
1295
1359
|
|
|
1296
1360
|
class Stats {
|
|
@@ -1315,7 +1379,9 @@ A shared counter, minimum, maximum or flag is an atomic: `new AtomicI32(0)`,
|
|
|
1315
1379
|
`new AtomicI64(toI64(0))`, updated from any task or parallel `f` by one
|
|
1316
1380
|
indivisible method call.
|
|
1317
1381
|
|
|
1318
|
-
|
|
1382
|
+
<!-- allow-panics: calls `parallelMapInto`, whose length check no proof removes yet; does integer arithmetic nothing bounds -->
|
|
1383
|
+
<!-- nish:ok --allow-panics -->
|
|
1384
|
+
```ts
|
|
1319
1385
|
import { AtomicI32, parallelMapInto } from "nish/threads";
|
|
1320
1386
|
|
|
1321
1387
|
export const main = (): i32 => {
|
|
@@ -1348,7 +1414,8 @@ export const main = (): i32 => {
|
|
|
1348
1414
|
to an `AtomicI32[]`, whose counters are separate objects on separate cache
|
|
1349
1415
|
lines. Same rules as above.
|
|
1350
1416
|
|
|
1351
|
-
|
|
1417
|
+
<!-- nish:err NL2513 -->
|
|
1418
|
+
```ts
|
|
1352
1419
|
import { AtomicI32, scope } from "nish/threads";
|
|
1353
1420
|
|
|
1354
1421
|
const bump = (a: AtomicI32): i32 => a.add(1);
|
|
@@ -1373,7 +1440,9 @@ When one task produces values and another consumes them, give both a
|
|
|
1373
1440
|
`for...of`, which waits for values and ends once every producer has returned.
|
|
1374
1441
|
Nobody closes a channel.
|
|
1375
1442
|
|
|
1376
|
-
|
|
1443
|
+
<!-- allow-panics: does integer arithmetic nothing bounds; indexes an array without a guard the checker reads -->
|
|
1444
|
+
<!-- nish:ok --allow-panics -->
|
|
1445
|
+
```ts
|
|
1377
1446
|
import { Channel, scope } from "nish/threads";
|
|
1378
1447
|
|
|
1379
1448
|
class Pipe {
|
|
@@ -1445,7 +1514,8 @@ export const main = (): i32 => {
|
|
|
1445
1514
|
the scheduler's order natively and in spawn order under Node, so a count or an
|
|
1446
1515
|
integer sum prints the same both ways.
|
|
1447
1516
|
|
|
1448
|
-
|
|
1517
|
+
<!-- nish:err NL2481 -->
|
|
1518
|
+
```ts
|
|
1449
1519
|
import { Channel, scope } from "nish/threads";
|
|
1450
1520
|
|
|
1451
1521
|
const one = (ch: Channel<i32>): i32 => {
|
|
@@ -1498,7 +1568,8 @@ export const main = (): i32 => {
|
|
|
1498
1568
|
[refusal table](guide/using-nish/threads.md#refused-and-what-to-write-instead)
|
|
1499
1569
|
pairs each code with an example that compiles.
|
|
1500
1570
|
|
|
1501
|
-
|
|
1571
|
+
<!-- nish:err NL2506 -->
|
|
1572
|
+
```ts
|
|
1502
1573
|
import { scope } from "nish/threads";
|
|
1503
1574
|
|
|
1504
1575
|
export const main = (): i32 => {
|
|
@@ -1520,7 +1591,8 @@ does not define. The symbol is the identifier, unmangled, in any package
|
|
|
1520
1591
|
call is an ordinary call. Two declarations of one name must have the same
|
|
1521
1592
|
types.
|
|
1522
1593
|
|
|
1523
|
-
|
|
1594
|
+
<!-- nish:ok -->
|
|
1595
|
+
```ts
|
|
1524
1596
|
declare function abs(n: i32): i32;
|
|
1525
1597
|
|
|
1526
1598
|
export const main = (): i32 => {
|
|
@@ -1545,7 +1617,8 @@ export const main = (): i32 => {
|
|
|
1545
1617
|
holds one, however deep (NL2746, NL2756), and a page module (freestanding
|
|
1546
1618
|
wasm, `--emit-dts`) refuses one (NL2757).
|
|
1547
1619
|
|
|
1548
|
-
|
|
1620
|
+
<!-- nish:ok -->
|
|
1621
|
+
```ts
|
|
1549
1622
|
declare class Timespec {
|
|
1550
1623
|
sec: i64;
|
|
1551
1624
|
nsec: i64;
|
|
@@ -1560,7 +1633,8 @@ export const main = (): i32 => {
|
|
|
1560
1633
|
};
|
|
1561
1634
|
```
|
|
1562
1635
|
|
|
1563
|
-
|
|
1636
|
+
<!-- nish:err NL2755 -->
|
|
1637
|
+
```ts
|
|
1564
1638
|
type Coord = number;
|
|
1565
1639
|
|
|
1566
1640
|
declare class Point {
|
|
@@ -1583,7 +1657,8 @@ export const main = (): i32 => {
|
|
|
1583
1657
|
borrows the bytes for the call and may write a `u8[]`; a spawned task may
|
|
1584
1658
|
not make the call.
|
|
1585
1659
|
|
|
1586
|
-
|
|
1660
|
+
<!-- nish:err NL2549 -->
|
|
1661
|
+
```ts
|
|
1587
1662
|
declare function SHA256(d: readonly u8[], n: u64, md: u8[]): CPtr;
|
|
1588
1663
|
|
|
1589
1664
|
export const main = (): i32 => {
|
|
@@ -1598,7 +1673,8 @@ export const main = (): i32 => {
|
|
|
1598
1673
|
panics (`ffi-nul`) before the call, because C would read only up to it; a
|
|
1599
1674
|
literal with no NUL is not checked.
|
|
1600
1675
|
|
|
1601
|
-
|
|
1676
|
+
<!-- nish:ok -->
|
|
1677
|
+
```ts
|
|
1602
1678
|
declare function puts(s: string): i32;
|
|
1603
1679
|
|
|
1604
1680
|
export const main = (): i32 => {
|
|
@@ -1613,7 +1689,9 @@ export const main = (): i32 => {
|
|
|
1613
1689
|
`NULL` panics (`ffi-null`). The copy never frees: a result the caller must
|
|
1614
1690
|
`free` (`strdup`) is declared `CPtr | null` instead.
|
|
1615
1691
|
|
|
1616
|
-
|
|
1692
|
+
<!-- allow-panics: reads a C pointer that may be `NULL` -->
|
|
1693
|
+
<!-- nish:ok --allow-panics -->
|
|
1694
|
+
```ts
|
|
1617
1695
|
declare function strerror(errnum: i32): string;
|
|
1618
1696
|
declare function getenv(name: string): string | null;
|
|
1619
1697
|
|
|
@@ -1630,13 +1708,13 @@ export const main = (): i32 => {
|
|
|
1630
1708
|
`p` into a new string; a dangling or unterminated `p` is undefined
|
|
1631
1709
|
behaviour, which is why it needs the import. It takes a `CPtr`, so narrow a
|
|
1632
1710
|
`CPtr | null` with `!== null` first. A `NULL` `p` panics (`ffi-null`) before
|
|
1633
|
-
the copy, so
|
|
1711
|
+
the copy, so the no-panic default refuses the call.
|
|
1634
1712
|
- **A struct C owns is copied out with `readStruct` from `nish:unsafe`.**
|
|
1635
1713
|
`const lc: Lconv = readStruct(p)` copies the bytes at `p` into a new
|
|
1636
1714
|
instance of the `declare class` the context names; write the annotation,
|
|
1637
1715
|
since `readStruct<Lconv>(p)` is refused (NL2763) and a call with no context
|
|
1638
1716
|
is NL2764. A `NULL` `p` panics (`ffi-null`) before the copy, so
|
|
1639
|
-
|
|
1717
|
+
the no-panic default refuses the call; a dangling `p`, or a class that does not
|
|
1640
1718
|
match C's fields, is undefined behaviour.
|
|
1641
1719
|
- **errno is read with `cErrno()` from `nish:unsafe`.** It answers the errno
|
|
1642
1720
|
the most recent `declare function` call on this thread left; naming it makes
|
|
@@ -1647,7 +1725,9 @@ export const main = (): i32 => {
|
|
|
1647
1725
|
`realpath(path, null)` passes `NULL`; a nullable pointer held in a variable
|
|
1648
1726
|
is NL2539, so C is never handed a `NULL` by accident.
|
|
1649
1727
|
|
|
1650
|
-
|
|
1728
|
+
<!-- allow-panics: reads a C pointer that may be `NULL` -->
|
|
1729
|
+
<!-- nish:ok --allow-panics -->
|
|
1730
|
+
```ts
|
|
1651
1731
|
import { readCString } from "nish:unsafe";
|
|
1652
1732
|
|
|
1653
1733
|
declare function realpath(path: string, resolved: CPtr | null): CPtr | null;
|
|
@@ -1692,7 +1772,8 @@ export const main = (): i32 => {
|
|
|
1692
1772
|
|
|
1693
1773
|
### Module constants
|
|
1694
1774
|
|
|
1695
|
-
|
|
1775
|
+
<!-- nish:ok -->
|
|
1776
|
+
```ts
|
|
1696
1777
|
const TOKEN_NAME: i32 = 1;
|
|
1697
1778
|
const TOKEN_END: i32 = TOKEN_NAME + 1;
|
|
1698
1779
|
export const PROMPT: string = "> ";
|
|
@@ -1714,7 +1795,9 @@ emitted and no initialiser runs** — every use lowers to the value itself.
|
|
|
1714
1795
|
|
|
1715
1796
|
### Module storage
|
|
1716
1797
|
|
|
1717
|
-
|
|
1798
|
+
<!-- allow-panics: does integer arithmetic nothing bounds; indexes an array without a guard the checker reads -->
|
|
1799
|
+
<!-- nish:ok --allow-panics -->
|
|
1800
|
+
```ts
|
|
1718
1801
|
const SCORES = new Int32Array(4);
|
|
1719
1802
|
|
|
1720
1803
|
const sum = (xs: Int32Array): i32 => {
|
|
@@ -1753,7 +1836,9 @@ state a module keeps (there is no top-level `let`).
|
|
|
1753
1836
|
|
|
1754
1837
|
### Classes and interfaces
|
|
1755
1838
|
|
|
1756
|
-
|
|
1839
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
1840
|
+
<!-- nish:ok --allow-panics -->
|
|
1841
|
+
```ts
|
|
1757
1842
|
interface Shape { width: i32; height: i32; }
|
|
1758
1843
|
|
|
1759
1844
|
class Rect implements Shape {
|
|
@@ -1783,7 +1868,8 @@ export const main = (): i32 => {
|
|
|
1783
1868
|
- Fields need annotations; an initializer must be a **literal** of the field's
|
|
1784
1869
|
type. Layout is declaration order, exactly as clang lays out the same C
|
|
1785
1870
|
struct.
|
|
1786
|
-
- At most one constructor, no parameter properties, no overloads
|
|
1871
|
+
- At most one constructor, no parameter properties, no overloads (outside
|
|
1872
|
+
[the compatibility dialect](guide/using-nish/compat.md)). **Definite
|
|
1787
1873
|
assignment** is checked syntactically: after the constructor returns every
|
|
1788
1874
|
field holds a value, and reading `this.f` before every field is assigned is
|
|
1789
1875
|
an error.
|
|
@@ -1807,7 +1893,8 @@ and `push` may move the whole block to grow it. **Holding an element across a
|
|
|
1807
1893
|
`push` on the same array is therefore refused**, because the write would land
|
|
1808
1894
|
in the old storage:
|
|
1809
1895
|
|
|
1810
|
-
|
|
1896
|
+
<!-- nish:err NL2290 -->
|
|
1897
|
+
```ts
|
|
1811
1898
|
interface Point { x: i32; }
|
|
1812
1899
|
|
|
1813
1900
|
export const main = (): i32 => {
|
|
@@ -1821,7 +1908,9 @@ export const main = (): i32 => {
|
|
|
1821
1908
|
|
|
1822
1909
|
Index again after the `push` instead of holding the element across it:
|
|
1823
1910
|
|
|
1824
|
-
|
|
1911
|
+
<!-- allow-panics: indexes an array without a guard the checker reads -->
|
|
1912
|
+
<!-- nish:ok --allow-panics -->
|
|
1913
|
+
```ts
|
|
1825
1914
|
interface Point { x: i32; }
|
|
1826
1915
|
|
|
1827
1916
|
export const main = (): i32 => {
|
|
@@ -1841,7 +1930,9 @@ grows, and the blocks it outgrows stay in the arena; a walk over `kids`
|
|
|
1841
1930
|
follows two pointers per child where the links follow one. On a 4M-node
|
|
1842
1931
|
tree the links took 0.52x the memory and 0.68x the time of `kids`.
|
|
1843
1932
|
|
|
1844
|
-
|
|
1933
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
1934
|
+
<!-- nish:ok --allow-panics -->
|
|
1935
|
+
```ts
|
|
1845
1936
|
class Node {
|
|
1846
1937
|
kind: i32;
|
|
1847
1938
|
parent: Node | null;
|
|
@@ -1896,7 +1987,9 @@ export const main = (): i32 => {
|
|
|
1896
1987
|
- `uncheckedGet` / `uncheckedSet` belong in that table's code only: an index
|
|
1897
1988
|
out of range there is undefined behaviour, not a panic.
|
|
1898
1989
|
|
|
1899
|
-
|
|
1990
|
+
<!-- allow-panics: builds an array whose length it does not bound; does integer arithmetic nothing bounds -->
|
|
1991
|
+
<!-- nish:ok --allow-panics -->
|
|
1992
|
+
```ts
|
|
1900
1993
|
import { uncheckedGet, uncheckedSet } from "nish:unsafe";
|
|
1901
1994
|
|
|
1902
1995
|
// One column per field, each allocated once at its final length.
|
|
@@ -1942,7 +2035,8 @@ enclosing function's return type, the parameter it is passed to, the field it
|
|
|
1942
2035
|
is assigned to, or a field of an enclosing literal. It must set every field
|
|
1943
2036
|
exactly once, with plain identifier keys.
|
|
1944
2037
|
|
|
1945
|
-
|
|
2038
|
+
<!-- nish:ok -->
|
|
2039
|
+
```ts
|
|
1946
2040
|
interface Pair { first: i32; second: i32; }
|
|
1947
2041
|
|
|
1948
2042
|
const swap = (p: Pair): Pair => ({ first: p.second, second: p.first });
|
|
@@ -1962,7 +2056,8 @@ A class with methods is made only by `new`: a literal typed as one would be a
|
|
|
1962
2056
|
plain object under Node, without the methods (NL2556). A class with fields
|
|
1963
2057
|
alone, and an interface, still take a literal.
|
|
1964
2058
|
|
|
1965
|
-
|
|
2059
|
+
<!-- nish:err NL2556 -->
|
|
2060
|
+
```ts
|
|
1966
2061
|
class Counter {
|
|
1967
2062
|
n: i32;
|
|
1968
2063
|
constructor(n: i32) {
|
|
@@ -1981,7 +2076,8 @@ export const main = (): i32 => {
|
|
|
1981
2076
|
|
|
1982
2077
|
### Type aliases and enums
|
|
1983
2078
|
|
|
1984
|
-
|
|
2079
|
+
<!-- nish:ok -->
|
|
2080
|
+
```ts
|
|
1985
2081
|
type Byte = u8;
|
|
1986
2082
|
type Bytes = Byte[];
|
|
1987
2083
|
|
|
@@ -2027,7 +2123,8 @@ export const main = (): i32 => {
|
|
|
2027
2123
|
`Role.parse` yet: test `s === String(Role.Admin)` per member, or decode it
|
|
2028
2124
|
with typed JSON.
|
|
2029
2125
|
|
|
2030
|
-
|
|
2126
|
+
<!-- nish:ok -->
|
|
2127
|
+
```ts
|
|
2031
2128
|
enum Role {
|
|
2032
2129
|
Admin = "admin",
|
|
2033
2130
|
Guest = "guest",
|
|
@@ -2074,7 +2171,8 @@ export const main = (): i32 => {
|
|
|
2074
2171
|
`parallelFrom` or `parallelReduce` body answering one is NL2521: answer
|
|
2075
2172
|
`Kind` instead.
|
|
2076
2173
|
|
|
2077
|
-
|
|
2174
|
+
<!-- nish:ok -->
|
|
2175
|
+
```ts
|
|
2078
2176
|
enum Kind {
|
|
2079
2177
|
Scalar,
|
|
2080
2178
|
Array,
|
|
@@ -2093,7 +2191,8 @@ export const main = (): i32 => {
|
|
|
2093
2191
|
};
|
|
2094
2192
|
```
|
|
2095
2193
|
|
|
2096
|
-
|
|
2194
|
+
<!-- nish:err NL2519 -->
|
|
2195
|
+
```ts
|
|
2097
2196
|
enum Kind {
|
|
2098
2197
|
Scalar,
|
|
2099
2198
|
Array,
|
|
@@ -2117,7 +2216,9 @@ export const main = (): i32 => (pick(Kind.Array) === Kind.Array ? 0 : 1);
|
|
|
2117
2216
|
|
|
2118
2217
|
### Discriminated unions
|
|
2119
2218
|
|
|
2120
|
-
|
|
2219
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
2220
|
+
<!-- nish:ok --allow-panics -->
|
|
2221
|
+
```ts
|
|
2121
2222
|
enum Kind {
|
|
2122
2223
|
Circle,
|
|
2123
2224
|
Square,
|
|
@@ -2249,7 +2350,8 @@ export const main = (): i32 => {
|
|
|
2249
2350
|
from there on. Declare the slot as the variant, or store a value of the
|
|
2250
2351
|
whole union (a call returning `Shape`):
|
|
2251
2352
|
|
|
2252
|
-
|
|
2353
|
+
<!-- nish:err NL2534 -->
|
|
2354
|
+
```ts
|
|
2253
2355
|
enum Kind {
|
|
2254
2356
|
Circle,
|
|
2255
2357
|
Square,
|
|
@@ -2302,9 +2404,10 @@ export const main = (): i32 => {
|
|
|
2302
2404
|
- Functions and enums may be renamed on import; classes and interfaces may
|
|
2303
2405
|
not — the type name is part of the ABI.
|
|
2304
2406
|
- Import cycles are allowed; a shared dependency is compiled once.
|
|
2305
|
-
- **
|
|
2306
|
-
|
|
2307
|
-
|
|
2407
|
+
- **An exported function name is unique across the whole program**; two
|
|
2408
|
+
modules may each keep a *private* function or generic of one name (its
|
|
2409
|
+
symbol becomes `<module>$.<name>`, e.g. `helper$.add`), except under
|
|
2410
|
+
`--no-strict-exports`, where every function name is unique.
|
|
2308
2411
|
- An `export`ed function is an external C-ABI symbol; every other function gets
|
|
2309
2412
|
`internal` linkage so LLVM may inline or drop it.
|
|
2310
2413
|
- **So an exported function is not named after a C library function**
|
|
@@ -2363,7 +2466,9 @@ export const main = (): i32 => {
|
|
|
2363
2466
|
`break`, `continue`, `process.exit`, an `if` whose branches all return, or an
|
|
2364
2467
|
infinite loop.
|
|
2365
2468
|
|
|
2366
|
-
|
|
2469
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
2470
|
+
<!-- nish:ok --allow-panics -->
|
|
2471
|
+
```ts
|
|
2367
2472
|
const firstPair = (limit: i32, target: i32): i32 => {
|
|
2368
2473
|
outer: for (let i: i32 = 0; i < limit; i++) {
|
|
2369
2474
|
for (let j: i32 = 0; j < limit; j++) {
|
|
@@ -2461,6 +2566,10 @@ barrier, so neither ever becomes a branch; select and compare on a secret with
|
|
|
2461
2566
|
these, never with `if`, `?:`, `===` or `table[secret]`, which they cannot fix.
|
|
2462
2567
|
`secureZero(bytes)` sets every byte of a `u8[]` to zero in stores the optimiser
|
|
2463
2568
|
may not drop, as a statement: how a key in a plain array is cleared once used.
|
|
2569
|
+
`mulHigh(a, b)` is the high 64 bits of the 128-bit product of two `u64`s
|
|
2570
|
+
(with `a * b` for the low half, the whole product), one multiply on x86-64 and
|
|
2571
|
+
AArch64 and four 32 × 32 products on wasm32, neither branching on an operand;
|
|
2572
|
+
`mulHighIsNative()` is `true` where it is one instruction.
|
|
2464
2573
|
|
|
2465
2574
|
**Secrets.** Hold a key as a `Secret<T>` from `nish:secret` (`T` an integer
|
|
2466
2575
|
array or a record of integer fields). It is opaque: no printing, interpolating,
|
|
@@ -2482,7 +2591,8 @@ refused with where it was made; pass a fresh value, or a copy
|
|
|
2482
2591
|
`secret(copy)`). `std/crypto`'s `p256`, `x25519` and
|
|
2483
2592
|
`x509ParseP256PrivateKey` take and give keys this way.
|
|
2484
2593
|
|
|
2485
|
-
|
|
2594
|
+
<!-- nish:ok -->
|
|
2595
|
+
```ts
|
|
2486
2596
|
import { Secret, expose, secret, wipe } from "nish:secret";
|
|
2487
2597
|
|
|
2488
2598
|
const key = (): u8[] => [7, 1, 9];
|
|
@@ -2496,7 +2606,8 @@ export const main = (): i32 => {
|
|
|
2496
2606
|
};
|
|
2497
2607
|
```
|
|
2498
2608
|
|
|
2499
|
-
|
|
2609
|
+
<!-- nish:err NL2440 -->
|
|
2610
|
+
```ts
|
|
2500
2611
|
import { Secret, expose, secret } from "nish:secret";
|
|
2501
2612
|
|
|
2502
2613
|
const key = (): u8[] => [7, 1, 9];
|
|
@@ -2508,7 +2619,8 @@ export const main = (): i32 => {
|
|
|
2508
2619
|
};
|
|
2509
2620
|
```
|
|
2510
2621
|
|
|
2511
|
-
|
|
2622
|
+
<!-- nish:err NL2512 -->
|
|
2623
|
+
```ts
|
|
2512
2624
|
import { Secret, secret, wipe } from "nish:secret";
|
|
2513
2625
|
|
|
2514
2626
|
export const main = (): i32 => {
|
|
@@ -2531,7 +2643,8 @@ parameters, calling `wipe(this.k)` itself for every such field on every path
|
|
|
2531
2643
|
`Secret` borrows it: it neither returns it (NL2546) nor wipes it (NL2548). Nothing checks that `wipe()` is called: call it when the
|
|
2532
2644
|
holder is done.
|
|
2533
2645
|
|
|
2534
|
-
|
|
2646
|
+
<!-- nish:ok -->
|
|
2647
|
+
```ts
|
|
2535
2648
|
import { Secret, expose, secret, wipe } from "nish:secret";
|
|
2536
2649
|
|
|
2537
2650
|
const width = (k: u8[]): i32 => toI32(k.length);
|
|
@@ -2557,7 +2670,8 @@ export const main = (): i32 => {
|
|
|
2557
2670
|
};
|
|
2558
2671
|
```
|
|
2559
2672
|
|
|
2560
|
-
|
|
2673
|
+
<!-- nish:err NL2544 -->
|
|
2674
|
+
```ts
|
|
2561
2675
|
import { Secret, secret, wipe } from "nish:secret";
|
|
2562
2676
|
|
|
2563
2677
|
class Session {
|
|
@@ -2792,7 +2906,8 @@ was there) and `clear()`, and `keys()` / `values()` as the iterable of a
|
|
|
2792
2906
|
and `s.forEach((x) => ...)` compile to that `for...of` walk with `f` inlined,
|
|
2793
2907
|
and answer `void`. `entries` is refused by name.
|
|
2794
2908
|
|
|
2795
|
-
|
|
2909
|
+
<!-- nish:ok-body -->
|
|
2910
|
+
```ts
|
|
2796
2911
|
const seen = new Set<string>();
|
|
2797
2912
|
const counts: Map<string, i32> = new Map();
|
|
2798
2913
|
seen.add("a").add("b").add("a");
|
|
@@ -2818,7 +2933,9 @@ console.log(`${seen.size} ${counts.size} ${seen.has("b")}`);
|
|
|
2818
2933
|
three ways: a default with `??`, a `const` tested with `!== undefined` (or
|
|
2819
2934
|
`=== undefined` and an early `return`), or a bare test. Each is one probe.
|
|
2820
2935
|
|
|
2821
|
-
|
|
2936
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
2937
|
+
<!-- nish:ok-body --allow-panics -->
|
|
2938
|
+
```ts
|
|
2822
2939
|
const counts = new Map<string, i32>();
|
|
2823
2940
|
for (const w of ["a", "b", "a"]) {
|
|
2824
2941
|
counts.set(w, (counts.get(w) ?? 0) + 1);
|
|
@@ -2838,7 +2955,8 @@ if (a !== undefined) {
|
|
|
2838
2955
|
be the value type. For a `Map<K, Node | null>` it replaces a stored `null`
|
|
2839
2956
|
too, but the result is still `Node | null`.
|
|
2840
2957
|
|
|
2841
|
-
|
|
2958
|
+
<!-- nish:err-body NL2361 -->
|
|
2959
|
+
```ts
|
|
2842
2960
|
const m = new Map<string, i32>();
|
|
2843
2961
|
let v = m.get("a");
|
|
2844
2962
|
```
|
|
@@ -2849,7 +2967,9 @@ may `set`, `delete` or `clear` the table inside the walk and get exactly what
|
|
|
2849
2967
|
JavaScript gives (a new key is visited later in the same walk; a key deleted
|
|
2850
2968
|
before it is reached is not).
|
|
2851
2969
|
|
|
2852
|
-
|
|
2970
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
2971
|
+
<!-- nish:ok-body --allow-panics -->
|
|
2972
|
+
```ts
|
|
2853
2973
|
const ages = new Map<string, i32>();
|
|
2854
2974
|
ages.set("ada", 36).set("alan", 41);
|
|
2855
2975
|
ages.delete("alan");
|
|
@@ -2869,7 +2989,8 @@ console.log(`${total}`); // 121
|
|
|
2869
2989
|
- `for (const e of m)` over a `Map` itself is refused, because its
|
|
2870
2990
|
`[key, value]` pairs are two-type tuples: walk `m.keys()` and call `m.get(k)`.
|
|
2871
2991
|
|
|
2872
|
-
|
|
2992
|
+
<!-- nish:err-body NL2375 -->
|
|
2993
|
+
```ts
|
|
2873
2994
|
const m = new Map<string, i32>();
|
|
2874
2995
|
for (const e of m) {
|
|
2875
2996
|
}
|
|
@@ -2887,7 +3008,9 @@ string `+`) or assign. Anything else still compiles, as one probe per call.
|
|
|
2887
3008
|
inserting it — and `reserve(m, n)`, which sizes the table for `n` entries; both
|
|
2888
3009
|
are one module with the program and run unchanged under Node.
|
|
2889
3010
|
|
|
2890
|
-
|
|
3011
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
3012
|
+
<!-- nish:ok --allow-panics -->
|
|
3013
|
+
```ts
|
|
2891
3014
|
import { getOrInsert, reserve } from "nish/map";
|
|
2892
3015
|
|
|
2893
3016
|
export const main = (): i32 => {
|
|
@@ -2950,7 +3073,9 @@ tag under one key (NL2721) as a different string (NL2720). A `tag` on a class
|
|
|
2950
3073
|
no union holds does nothing, and a JSON call on that class refuses its
|
|
2951
3074
|
enum-member field.
|
|
2952
3075
|
|
|
2953
|
-
|
|
3076
|
+
<!-- allow-panics: builds an array whose length it does not bound; indexes an array without a guard the checker reads; calls `std/json-typed.ts` code that keeps panic sites no proof removes yet -->
|
|
3077
|
+
<!-- nish:ok --allow-panics -->
|
|
3078
|
+
```ts
|
|
2954
3079
|
import { JsonWriter } from "nish/json";
|
|
2955
3080
|
import { JsonLimits } from "nish/json-tokenizer";
|
|
2956
3081
|
import { JsonDecoder, jsonDecode, jsonEncode } from "nish/json-typed";
|
|
@@ -2974,7 +3099,8 @@ export const main = (): i32 => {
|
|
|
2974
3099
|
};
|
|
2975
3100
|
```
|
|
2976
3101
|
|
|
2977
|
-
|
|
3102
|
+
<!-- nish:err NL2485 -->
|
|
3103
|
+
```ts
|
|
2978
3104
|
import { JsonWriter } from "nish/json";
|
|
2979
3105
|
import { jsonEncode } from "nish/json-typed";
|
|
2980
3106
|
|
|
@@ -2989,7 +3115,9 @@ export const main = (): i32 => {
|
|
|
2989
3115
|
};
|
|
2990
3116
|
```
|
|
2991
3117
|
|
|
2992
|
-
|
|
3118
|
+
<!-- allow-panics: indexes an array without a guard the checker reads; calls `std/json-typed.ts` code that keeps panic sites no proof removes yet -->
|
|
3119
|
+
<!-- nish:ok --allow-panics -->
|
|
3120
|
+
```ts
|
|
2993
3121
|
import { JsonWriter } from "nish/json";
|
|
2994
3122
|
import { JsonDecoder, JsonNumber, jsonDecode, jsonEncode } from "nish/json-typed";
|
|
2995
3123
|
|
|
@@ -3010,7 +3138,8 @@ export const writeOrder = (w: JsonWriter, o: Order): void => {
|
|
|
3010
3138
|
};
|
|
3011
3139
|
```
|
|
3012
3140
|
|
|
3013
|
-
|
|
3141
|
+
<!-- nish:err NL2495 -->
|
|
3142
|
+
```ts
|
|
3014
3143
|
import { JsonWriter } from "nish/json";
|
|
3015
3144
|
import { jsonEncode } from "nish/json-typed";
|
|
3016
3145
|
|
|
@@ -3026,7 +3155,8 @@ export const main = (): i32 => {
|
|
|
3026
3155
|
};
|
|
3027
3156
|
```
|
|
3028
3157
|
|
|
3029
|
-
|
|
3158
|
+
<!-- nish:err NL2485 -->
|
|
3159
|
+
```ts
|
|
3030
3160
|
import { JsonWriter } from "nish/json";
|
|
3031
3161
|
import { jsonEncode } from "nish/json-typed";
|
|
3032
3162
|
|
|
@@ -3065,7 +3195,9 @@ outlive it — so allocate what must outlive the block before it, and let only
|
|
|
3065
3195
|
numbers, booleans and enums out. `arena()` is only ever a `using` initialiser,
|
|
3066
3196
|
and `a` is never read.
|
|
3067
3197
|
|
|
3068
|
-
|
|
3198
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
3199
|
+
<!-- nish:ok-body --allow-panics -->
|
|
3200
|
+
```ts
|
|
3069
3201
|
const rows: string[] = ["a", "bb"];
|
|
3070
3202
|
let total = 0;
|
|
3071
3203
|
for (const row of rows) {
|
|
@@ -3105,7 +3237,9 @@ entry — use a region.** `region(f)` / `regionWith(arg, f)` make one and run
|
|
|
3105
3237
|
gives all of it back. `f` is a named function or an arrow that captures
|
|
3106
3238
|
nothing. `Region<T>` is a number, not a pointer.
|
|
3107
3239
|
|
|
3108
|
-
|
|
3240
|
+
<!-- allow-panics: does integer arithmetic nothing bounds; calls `std/region.ts` code that keeps panic sites no proof removes yet -->
|
|
3241
|
+
<!-- nish:ok --allow-panics -->
|
|
3242
|
+
```ts
|
|
3109
3243
|
import { Region, enter, free, region } from "nish:region";
|
|
3110
3244
|
|
|
3111
3245
|
class Session {
|
|
@@ -3140,7 +3274,9 @@ the module (NL4015).
|
|
|
3140
3274
|
|
|
3141
3275
|
**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:
|
|
3142
3276
|
|
|
3143
|
-
|
|
3277
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
3278
|
+
<!-- nish:ok-body --allow-panics -->
|
|
3279
|
+
```ts
|
|
3144
3280
|
const xs: i32[] = [1, 2, 3];
|
|
3145
3281
|
const doubled = xs.map((x) => x * 2);
|
|
3146
3282
|
let sum = 0;
|
|
@@ -3150,9 +3286,12 @@ for (const x of doubled) {
|
|
|
3150
3286
|
console.log(`${sum}`);
|
|
3151
3287
|
```
|
|
3152
3288
|
|
|
3153
|
-
**Convert a number to a string** —
|
|
3289
|
+
**Convert a number to a string** — a number has no `toString`, and
|
|
3290
|
+
`String()` converts only a [string enum](#type-aliases-and-enums)'s value;
|
|
3291
|
+
write a template literal:
|
|
3154
3292
|
|
|
3155
|
-
|
|
3293
|
+
<!-- nish:ok-body -->
|
|
3294
|
+
```ts
|
|
3156
3295
|
const n = 42;
|
|
3157
3296
|
const s = `${n}`;
|
|
3158
3297
|
console.log(s);
|
|
@@ -3160,7 +3299,8 @@ console.log(s);
|
|
|
3160
3299
|
|
|
3161
3300
|
**Return two values** — there are no tuples: declare an interface.
|
|
3162
3301
|
|
|
3163
|
-
|
|
3302
|
+
<!-- nish:ok -->
|
|
3303
|
+
```ts
|
|
3164
3304
|
interface Split { head: string; rest: string; }
|
|
3165
3305
|
|
|
3166
3306
|
const cut = (s: string, at: i32): Split => ({
|
|
@@ -3186,8 +3326,8 @@ you need to walk them, since there is no iteration yet.
|
|
|
3186
3326
|
**Find the next byte of a few** — `indexOfAny(text, bytes, from)` from
|
|
3187
3327
|
`nish/text` answers the first index at or after `from` whose byte is one of
|
|
3188
3328
|
`bytes`, or -1: the scan for the next `"` or `\\` in a JSON string, the next
|
|
3189
|
-
delimiter in a line. `bytes`
|
|
3190
|
-
|
|
3329
|
+
delimiter in a line. `bytes` is any set, and an ASCII one answers the same
|
|
3330
|
+
natively and under Node; `from` is clamped to `[0, text.length]`. Natively it reads 16 or 32
|
|
3191
3331
|
bytes at a time, so prefer it to a `charCodeAt` loop over a long text — and
|
|
3192
3332
|
never call it on secret data, because its time says where the match is.
|
|
3193
3333
|
(No block: a program importing `nish/text` writes two `.ll` files;
|
|
@@ -3199,7 +3339,8 @@ methods on a number and no optional parameters, so `x.toFixed(n)` is
|
|
|
3199
3339
|
both from `nish/text` and both answering what JavaScript does. `toFixed`
|
|
3200
3340
|
rounds the exact binary value (`toFixed(1.005, 2)` is `"1.00"`) and panics
|
|
3201
3341
|
outside 0 to 100 digits; `splitLimit` keeps the first `limit` pieces, a
|
|
3202
|
-
negative limit keeps all of them, and an empty separator
|
|
3342
|
+
negative limit keeps all of them, and an empty separator splits into code
|
|
3343
|
+
points.
|
|
3203
3344
|
(No block, as for `nish/text`; `tests/link/std_text_to_fixed` and
|
|
3204
3345
|
`tests/link/std_text_split_limit` are its programs.)
|
|
3205
3346
|
|
|
@@ -3351,7 +3492,9 @@ guide's [terminal page](guide/using-nish/terminal.md) its walk-through.)
|
|
|
3351
3492
|
dispatch. Give each class the interface's fields **first**, `implements` it,
|
|
3352
3493
|
and write a free function over the interface:
|
|
3353
3494
|
|
|
3354
|
-
|
|
3495
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
3496
|
+
<!-- nish:ok --allow-panics -->
|
|
3497
|
+
```ts
|
|
3355
3498
|
interface Sized { width: i32; height: i32; }
|
|
3356
3499
|
|
|
3357
3500
|
class Photo implements Sized {
|
|
@@ -3385,10 +3528,16 @@ emitting code that cannot compile.
|
|
|
3385
3528
|
|
|
3386
3529
|
### Proving it cannot panic
|
|
3387
3530
|
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3531
|
+
Every module of the program's own package is held to no panic by default:
|
|
3532
|
+
every place it could stop with a panic is an error (NL2457) until a proof
|
|
3533
|
+
removes it; out of memory is the one kind allowed. `--allow-panics` relaxes
|
|
3534
|
+
that to the modules the root `package.json` lists in
|
|
3535
|
+
`"nish": { "noPanic": [...] }`, and `--deny-panics` spells the default out.
|
|
3536
|
+
**Fix a refusal; do not relax it.** Keep the default, add the guard the error
|
|
3537
|
+
names, and never reach for `--allow-panics`, a `nish:unsafe` import or a guard
|
|
3538
|
+
that proves nothing (one whose `else` branch returns a made-up value the
|
|
3539
|
+
caller cannot tell from a real one) to make an error go away. Each error names
|
|
3540
|
+
its fix, and there are five:
|
|
3392
3541
|
|
|
3393
3542
|
- **Guard** what the checker cannot prove: `if (i >= 0 && i < xs.length)` before
|
|
3394
3543
|
`xs[i]` (`if (toU32(i) < toU32(xs.length))` for a `u8`, `u16` or `u32` index,
|
|
@@ -3406,11 +3555,13 @@ allowed. Each error names its fix, and there are five:
|
|
|
3406
3555
|
supply a value instead of stopping.
|
|
3407
3556
|
- **Swap `readFileSync` for `readFileSyncOrNull`**, which answers `null` where
|
|
3408
3557
|
the other exits.
|
|
3409
|
-
- **Read the list**: `nish file.ts --emit-panics panics.json`
|
|
3410
|
-
per function, proven or not, and the `"panics"` of
|
|
3411
|
-
same.
|
|
3558
|
+
- **Read the list**: `nish file.ts --allow-panics --emit-panics panics.json`
|
|
3559
|
+
writes every site per function, proven or not, and the `"panics"` of
|
|
3560
|
+
`--emit-capabilities` the same. Pass `--allow-panics` to get it: a build the
|
|
3561
|
+
default refuses writes neither file.
|
|
3412
3562
|
|
|
3413
|
-
|
|
3563
|
+
<!-- nish:ok --deny-panics -->
|
|
3564
|
+
```ts
|
|
3414
3565
|
const at = (xs: i32[], i: i32): i32 => {
|
|
3415
3566
|
if (i >= 0 && i < xs.length) {
|
|
3416
3567
|
return xs[i];
|
|
@@ -3445,19 +3596,24 @@ export const main = (): i32 => {
|
|
|
3445
3596
|
|
|
3446
3597
|
The same function without its guard is refused, and the error names the guard:
|
|
3447
3598
|
|
|
3448
|
-
|
|
3599
|
+
<!-- nish:err NL2457 --deny-panics -->
|
|
3600
|
+
```ts
|
|
3449
3601
|
export const at = (xs: i32[], i: i32): i32 => xs[i];
|
|
3450
3602
|
```
|
|
3451
3603
|
|
|
3452
3604
|
A call into the standard library or a dependency that may panic is refused at
|
|
3453
3605
|
the call (NL2458), because only its own author can prove it: call a function
|
|
3454
|
-
that cannot panic
|
|
3606
|
+
that cannot panic. The standard library is not yet panic-free, so `Map`,
|
|
3607
|
+
`Set`, `split` and the `JSON` reads are refused in the scope for now;
|
|
3608
|
+
a program that needs them builds with `--allow-panics`, and says why where it
|
|
3609
|
+
is built.
|
|
3455
3610
|
`uncheckedGet` and `uncheckedSet` are not proofs, but
|
|
3456
3611
|
they cannot panic, and their `nish:unsafe` import is the module's visible
|
|
3457
3612
|
opt-in to an index out of range being undefined behaviour, so the scope allows
|
|
3458
3613
|
them (as `"allowed": true` sites) and a call into them is never refused:
|
|
3459
3614
|
|
|
3460
|
-
|
|
3615
|
+
<!-- nish:ok --deny-panics -->
|
|
3616
|
+
```ts
|
|
3461
3617
|
import { uncheckedGet } from "nish:unsafe";
|
|
3462
3618
|
|
|
3463
3619
|
export const first = (xs: i32[]): i32 => uncheckedGet(xs, 0);
|
|
@@ -3465,7 +3621,8 @@ export const first = (xs: i32[]): i32 => uncheckedGet(xs, 0);
|
|
|
3465
3621
|
|
|
3466
3622
|
Without the import the call is refused (NL2456), scope or not:
|
|
3467
3623
|
|
|
3468
|
-
|
|
3624
|
+
<!-- nish:err NL2456 --deny-panics -->
|
|
3625
|
+
```ts
|
|
3469
3626
|
export const first = (xs: i32[]): i32 => uncheckedGet(xs, 0);
|
|
3470
3627
|
```
|
|
3471
3628
|
|
|
@@ -3483,7 +3640,9 @@ that keeps to it ([LANGUAGE.md](LANGUAGE.md#the-capability-policy)). Under an
|
|
|
3483
3640
|
allowlist `exit` is a capability like the others, and `unsafe` can be denied
|
|
3484
3641
|
but never allowed: importing `nish:unsafe` is the opt-in.
|
|
3485
3642
|
|
|
3486
|
-
|
|
3643
|
+
<!-- allow-panics: it shows NL2459, not a panic site; calls a file or entropy builtin that exits on failure -->
|
|
3644
|
+
<!-- nish:err NL2459 --deny fs.read --allow-panics -->
|
|
3645
|
+
```ts
|
|
3487
3646
|
const settings = (): string => readFileSync("settings.txt");
|
|
3488
3647
|
|
|
3489
3648
|
export const main = (): i32 => settings().length;
|
|
@@ -3530,8 +3689,7 @@ Run it. `nish file.ts --json` is one command and it is the only proof.
|
|
|
3530
3689
|
prints the one-line summary ([LANGUAGE.md](LANGUAGE.md#capabilities)).
|
|
3531
3690
|
A package that must not touch the network says so in `package.json`
|
|
3532
3691
|
([Holding it to its capabilities](#holding-it-to-its-capabilities)).
|
|
3533
|
-
11. Must it never panic?
|
|
3534
|
-
`package.json`'s `"nish": { "noPanic": [...] }`): every index, division,
|
|
3692
|
+
11. Must it never panic? It is held to that by default: every index, division,
|
|
3535
3693
|
signed `+ - *`, `pop`, range entry, `expect` and exiting call it cannot
|
|
3536
3694
|
prove is an error
|
|
3537
3695
|
naming the guard ([Proving it cannot panic](#proving-it-cannot-panic)); an
|
|
@@ -3543,6 +3701,6 @@ Run it. `nish file.ts --json` is one command and it is the only proof.
|
|
|
3543
3701
|
`LANGUAGE.md` rule and cookbook entry, and a conventional commit message,
|
|
3544
3702
|
which is its changelog entry.
|
|
3545
3703
|
|
|
3546
|
-
Every
|
|
3704
|
+
Every block under a `<!-- nish:ok -->` or `<!-- nish:err -->` comment on this page is compiled by
|
|
3547
3705
|
`npm test`, so an example that has gone stale is a failing test rather than a
|
|
3548
3706
|
program that no longer works.
|