nish 0.41.0 → 0.44.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 +16 -4
- package/bin/nish-skills.js +233 -0
- package/docs/AI.md +282 -126
- package/llms.txt +4 -0
- package/package.json +14 -11
- package/runtime/core.d.ts +16 -0
- package/runtime/core.mjs +5 -0
- package/runtime/nish.d.ts +204 -33
- package/runtime/nish.h +19 -3
- package/runtime/nish.mjs +5 -1
- package/runtime/runtime-cross.c +15 -0
- package/runtime/runtime-net.c +876 -10
- package/runtime/runtime-region.c +21 -0
- package/runtime/runtime-sandbox.c +112 -15
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +533 -77
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +474 -5
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +125 -29
- package/scripts/link-skills.mjs +1039 -0
- package/scripts/postinstall.mjs +43 -0
- package/skills/writing-nish/SKILL.md +144 -0
- package/std/README.md +3 -3
- package/std/crypto/base64url.ts +102 -1
- package/std/crypto/scram.ts +142 -0
- package/std/crypto/sha1.ts +99 -41
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/net/fetch.ts +9 -1
- package/std/net/handler.ts +79 -15
- package/std/net/http-server.ts +2 -3
- package/std/net/http1-server.ts +294 -51
- package/std/net/http3-server.ts +26 -15
- package/std/net/http3.ts +2 -2
- package/std/net/oidc.ts +1107 -0
- package/std/net/postgres-async.ts +64 -33
- package/std/net/postgres.ts +65 -76
- package/std/net/quic-conn-ack.ts +57 -7
- package/std/net/quic-frame.ts +104 -2
- package/std/net/quic-listener.ts +9 -2
- package/std/net/quic.ts +16 -1
- package/std/net/serve.ts +10 -8
- package/std/net/websocket.ts +188 -49
- package/std/sqlite.ts +325 -0
- package/std/text.ts +80 -11
package/docs/AI.md
CHANGED
|
@@ -130,11 +130,18 @@ are one node per line, which is to say diffable.
|
|
|
130
130
|
|
|
131
131
|
## A whole program
|
|
132
132
|
|
|
133
|
-
|
|
133
|
+
This program divides by a value it does not test, so it builds with
|
|
134
|
+
`--allow-panics` ([Proving it cannot panic](#proving-it-cannot-panic)); a
|
|
135
|
+
guard that the divisor is not zero would let it build under the default. Other
|
|
136
|
+
examples below that do integer arithmetic nothing bounds build the same way.
|
|
137
|
+
|
|
138
|
+
<!-- allow-panics: divides by a value it does not test -->
|
|
139
|
+
<!-- nish:ok --allow-panics -->
|
|
140
|
+
```ts
|
|
134
141
|
const gcd = (a: i32, b: i32): i32 => {
|
|
135
142
|
let x = a;
|
|
136
143
|
let y = b;
|
|
137
|
-
while (y
|
|
144
|
+
while (y > 0) {
|
|
138
145
|
const t = y;
|
|
139
146
|
y = x % y;
|
|
140
147
|
x = t;
|
|
@@ -202,7 +209,7 @@ rejects. This table is the highest-value part of the page.
|
|
|
202
209
|
| `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
210
|
| `[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
211
|
| `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 |
|
|
212
|
+
| `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
213
|
| a method in an `interface` | `` Interface `I` cannot declare methods (interfaces describe layout only) `` | fields only; a top-level function over the interface |
|
|
207
214
|
| `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
215
|
| `{ 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 +217,7 @@ rejects. This table is the highest-value part of the page.
|
|
|
210
217
|
| `{ [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
218
|
| `type Pair = { a: i32 }` (inline object type) | `` Unsupported type `{ a: i32 }` `` | declare an `interface` |
|
|
212
219
|
| `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}` `` |
|
|
220
|
+
| `String(n)`, `n.toString()` | `` Unknown function `String` `` / `` Unknown method `toString` on i32; use a template literal: `${n}` `` | `` `${n}` `` |
|
|
214
221
|
| `xs.length = 0` | `` Cannot assign to `length` of i32[] (array length is read-only; use `push`) `` | build a new array |
|
|
215
222
|
| `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
223
|
| `export default f` | `` `export default` / `export =` are not supported; use a named `export function` `` | `export const f = …` |
|
|
@@ -225,14 +232,16 @@ rejects. This table is the highest-value part of the page.
|
|
|
225
232
|
|
|
226
233
|
A worked pair. This is the single most common rejection:
|
|
227
234
|
|
|
228
|
-
|
|
235
|
+
<!-- nish:err-body NL2188 -->
|
|
236
|
+
```ts
|
|
229
237
|
const xs = [1, 2, 3];
|
|
230
238
|
if (xs.length) {
|
|
231
239
|
console.log("non-empty");
|
|
232
240
|
}
|
|
233
241
|
```
|
|
234
242
|
|
|
235
|
-
|
|
243
|
+
<!-- nish:ok-body -->
|
|
244
|
+
```ts
|
|
236
245
|
const xs = [1, 2, 3];
|
|
237
246
|
if (xs.length !== 0) {
|
|
238
247
|
console.log("non-empty");
|
|
@@ -256,7 +265,9 @@ only once the program has no other error, so they show up last.
|
|
|
256
265
|
| `if (c) … else if (c) …`, the same condition again | NL2716 | the condition you meant for the second branch |
|
|
257
266
|
| `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
267
|
|
|
259
|
-
|
|
268
|
+
<!-- allow-panics: it shows NL2712, not a panic site; does integer arithmetic nothing bounds -->
|
|
269
|
+
<!-- nish:err-body NL2712 --allow-panics -->
|
|
270
|
+
```ts
|
|
260
271
|
const xs = [3, 4];
|
|
261
272
|
let total = 0;
|
|
262
273
|
for (const x of xs) {
|
|
@@ -266,7 +277,9 @@ const average = total / 2;
|
|
|
266
277
|
console.log(`${total}`);
|
|
267
278
|
```
|
|
268
279
|
|
|
269
|
-
|
|
280
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
281
|
+
<!-- nish:ok-body --allow-panics -->
|
|
282
|
+
```ts
|
|
270
283
|
const xs = [3, 4];
|
|
271
284
|
let total = 0;
|
|
272
285
|
for (const x of xs) {
|
|
@@ -285,7 +298,7 @@ compatible only when their types are identical.
|
|
|
285
298
|
| Type | Is | Notes |
|
|
286
299
|
| --- | --- | --- |
|
|
287
300
|
| `number` | `i32`, or `f64` under `--number-mode f64` | the mode's default width |
|
|
288
|
-
| `i32`, `i64` | signed integers | overflow **panics** (exit 1)
|
|
301
|
+
| `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
302
|
| `u8`, `u16`, `u32`, `u64` | unsigned integers | arithmetic **always** wraps; `>>` is logical |
|
|
290
303
|
| `f32`, `f64` | IEEE-754 | `f32` is never the lowering of `number` |
|
|
291
304
|
| `boolean` | `i1` | no truthiness anywhere |
|
|
@@ -311,7 +324,8 @@ left ([Calling C](#calling-c)). Calling one without the import is NL2456. No fla
|
|
|
311
324
|
whole program: `--unchecked-indexing` and `--wrapping` were removed before 1.0
|
|
312
325
|
and are refused with exit 2.
|
|
313
326
|
|
|
314
|
-
|
|
327
|
+
<!-- nish:ok -->
|
|
328
|
+
```ts
|
|
315
329
|
import { uncheckedGet, wrappingAdd, wrappingMul } from "nish:unsafe";
|
|
316
330
|
|
|
317
331
|
export const hash = (bytes: u8[]): i32 => {
|
|
@@ -327,7 +341,9 @@ export const hash = (bytes: u8[]): i32 => {
|
|
|
327
341
|
`u32 + i32` (same LLVM type, different signedness) are rejected. Convert
|
|
328
342
|
explicitly: `toI32` `toI64` `toU8` `toU16` `toU32` `toU64` `toF32` `toF64`.
|
|
329
343
|
|
|
330
|
-
**Signed overflow panics.**
|
|
344
|
+
**Signed overflow panics.** By default an operation the compiler cannot
|
|
345
|
+
prove fits is a compile error (NL2457). Under `--allow-panics`, `+ - *`,
|
|
346
|
+
unary `-`, `++`, `--` and `op=` on an
|
|
331
347
|
`i32` or `i64` stop the program with `attempt to add with overflow` (or
|
|
332
348
|
`subtract`, `multiply`, `negate`) and exit 1 when the result leaves the type;
|
|
333
349
|
it is never a silent wrap and never undefined. The compiler drops the check
|
|
@@ -337,7 +353,8 @@ that is *meant* to wrap — a hash, a PRNG, a checksum — goes in `u32` or `u64
|
|
|
337
353
|
which always wrap and are never checked; convert back with `toI32` (free), or
|
|
338
354
|
call `wrappingAdd` / `wrappingSub` / `wrappingMul` at the site.
|
|
339
355
|
|
|
340
|
-
|
|
356
|
+
<!-- nish:ok -->
|
|
357
|
+
```ts
|
|
341
358
|
// FNV-1a: the multiply overflows on purpose, so the state is a `u32`.
|
|
342
359
|
export const fnv1a = (bytes: u8[]): u32 => {
|
|
343
360
|
let h: u32 = 2166136261;
|
|
@@ -348,13 +365,16 @@ export const fnv1a = (bytes: u8[]): u32 => {
|
|
|
348
365
|
};
|
|
349
366
|
```
|
|
350
367
|
|
|
351
|
-
|
|
368
|
+
<!-- nish:err NL2231 -->
|
|
369
|
+
```ts
|
|
352
370
|
const bad = (a: i32, b: i64): i64 => {
|
|
353
371
|
return a + b;
|
|
354
372
|
};
|
|
355
373
|
```
|
|
356
374
|
|
|
357
|
-
|
|
375
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
376
|
+
<!-- nish:ok --allow-panics -->
|
|
377
|
+
```ts
|
|
358
378
|
const good = (a: i32, b: i64): i64 => {
|
|
359
379
|
return toI64(a) + b;
|
|
360
380
|
};
|
|
@@ -363,8 +383,9 @@ const good = (a: i32, b: i64): i64 => {
|
|
|
363
383
|
**`integer<Lo, Hi>` is an `i32` that stays in `[Lo, Hi]`.** The bounds are two
|
|
364
384
|
integer literals (a sign allowed) inside `i32`, and it is an `i32` everywhere
|
|
365
385
|
the machine can see. Putting a value *into* one is checked: an `i32` or
|
|
366
|
-
another range is compared once
|
|
367
|
-
|
|
386
|
+
another range is compared once (a check the default refuses unless it is
|
|
387
|
+
proven; under `--allow-panics` the program panics, exit 1, outside the
|
|
388
|
+
range), a literal outside it is a compile error, and a literal inside it or a
|
|
368
389
|
narrower range costs nothing. So does a value a loop condition or a guard
|
|
369
390
|
already bounds (`b` below), and `toI32` of a `u8`; a check left inside a loop
|
|
370
391
|
is a performance warning (NL9013) naming the guard that removes it. A range
|
|
@@ -375,7 +396,9 @@ not an `integer<0, 255>` (convert with `toI32`), and a `declare function` may
|
|
|
375
396
|
not mention a range. Put the range on the value you use, not on a loop
|
|
376
397
|
counter, which has to leave the range to end the loop.
|
|
377
398
|
|
|
378
|
-
|
|
399
|
+
<!-- allow-panics: indexes an array without a guard the checker reads; does integer arithmetic nothing bounds -->
|
|
400
|
+
<!-- nish:ok --allow-panics -->
|
|
401
|
+
```ts
|
|
379
402
|
const getByte = (buf: u8[], i: integer<0, 255>): u8 => buf[i];
|
|
380
403
|
|
|
381
404
|
const sumAll = (buf: u8[]): i32 => {
|
|
@@ -388,7 +411,8 @@ const sumAll = (buf: u8[]): i32 => {
|
|
|
388
411
|
};
|
|
389
412
|
```
|
|
390
413
|
|
|
391
|
-
|
|
414
|
+
<!-- nish:err-body NL2384 -->
|
|
415
|
+
```ts
|
|
392
416
|
const digit: integer<0, 9> = 12;
|
|
393
417
|
```
|
|
394
418
|
|
|
@@ -396,7 +420,8 @@ Declaring a type alias, enum, class, interface or function named `integer` is
|
|
|
396
420
|
refused, because the name is the type. A value named `integer` (a local or a
|
|
397
421
|
module constant) is fine.
|
|
398
422
|
|
|
399
|
-
|
|
423
|
+
<!-- nish:err NL2332 -->
|
|
424
|
+
```ts
|
|
400
425
|
class integer {
|
|
401
426
|
value: i32 = 0;
|
|
402
427
|
}
|
|
@@ -418,7 +443,8 @@ Everything else leaves the literal at the default, however obvious the intent.
|
|
|
418
443
|
The three that catch you out: an **object-literal property**, a `Result`
|
|
419
444
|
payload, and a **method's** argument.
|
|
420
445
|
|
|
421
|
-
|
|
446
|
+
<!-- nish:err NL2269 -->
|
|
447
|
+
```ts
|
|
422
448
|
interface Pixel { r: u8; g: u8; b: u8; }
|
|
423
449
|
|
|
424
450
|
export const main = (): i32 => {
|
|
@@ -428,7 +454,8 @@ export const main = (): i32 => {
|
|
|
428
454
|
};
|
|
429
455
|
```
|
|
430
456
|
|
|
431
|
-
|
|
457
|
+
<!-- nish:ok -->
|
|
458
|
+
```ts
|
|
432
459
|
interface Pixel { r: u8; g: u8; b: u8; }
|
|
433
460
|
|
|
434
461
|
export const main = (): i32 => {
|
|
@@ -446,7 +473,8 @@ literals, not a literal in a `u8` context. Name the value first, or convert.
|
|
|
446
473
|
There is no `throw`, no `try`, and no unwinding anywhere in the language. A
|
|
447
474
|
function that can fail says so in its return type.
|
|
448
475
|
|
|
449
|
-
|
|
476
|
+
<!-- nish:ok -->
|
|
477
|
+
```ts
|
|
450
478
|
const half = (n: i32): Result<i32, string> => {
|
|
451
479
|
if (n % 2 !== 0) {
|
|
452
480
|
return Err("odd");
|
|
@@ -490,7 +518,8 @@ reference cannot hold.
|
|
|
490
518
|
is no implicit error conversion — convert by hand with
|
|
491
519
|
`if (r.isErr()) { return Err(…); }`.
|
|
492
520
|
|
|
493
|
-
|
|
521
|
+
<!-- nish:err NL2044 -->
|
|
522
|
+
```ts
|
|
494
523
|
const half = (n: i32): Result<i32, string> => {
|
|
495
524
|
return Ok(n / 2);
|
|
496
525
|
};
|
|
@@ -520,7 +549,9 @@ reference — never a scalar, which has no null value. It is the same pointer
|
|
|
520
549
|
(or, for a function reference, the same tag) with `null` as one more value, so
|
|
521
550
|
nothing is boxed.
|
|
522
551
|
|
|
523
|
-
|
|
552
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
553
|
+
<!-- nish:ok --allow-panics -->
|
|
554
|
+
```ts
|
|
524
555
|
class Node {
|
|
525
556
|
value: i32;
|
|
526
557
|
next: Node | null = null;
|
|
@@ -556,7 +587,8 @@ export const main = (): i32 => {
|
|
|
556
587
|
refused, because Node answers `d` past the end where Nish panics. `??` also
|
|
557
588
|
defaults a `Map.get` result (see [Map and Set](#map-and-set)).
|
|
558
589
|
|
|
559
|
-
|
|
590
|
+
<!-- nish:ok -->
|
|
591
|
+
```ts
|
|
560
592
|
class Node {
|
|
561
593
|
value: i32;
|
|
562
594
|
next: Node | null = null;
|
|
@@ -575,7 +607,8 @@ export const main = (): i32 => {
|
|
|
575
607
|
};
|
|
576
608
|
```
|
|
577
609
|
|
|
578
|
-
|
|
610
|
+
<!-- nish:err-body NL2489 -->
|
|
611
|
+
```ts
|
|
579
612
|
const names = new Array<string | null>(2);
|
|
580
613
|
console.log(names[2] ?? "none");
|
|
581
614
|
```
|
|
@@ -590,7 +623,8 @@ console.log(names[2] ?? "none");
|
|
|
590
623
|
`next` needs `a?.next?.v`. `?.` on something that cannot be `null` is
|
|
591
624
|
NL2490; `?.[i]` and `f?.()` are refused.
|
|
592
625
|
|
|
593
|
-
|
|
626
|
+
<!-- nish:ok -->
|
|
627
|
+
```ts
|
|
594
628
|
class Order {
|
|
595
629
|
id: i32;
|
|
596
630
|
next: Order | null = null;
|
|
@@ -609,7 +643,8 @@ export const main = (): i32 => {
|
|
|
609
643
|
};
|
|
610
644
|
```
|
|
611
645
|
|
|
612
|
-
|
|
646
|
+
<!-- nish:err-body NL1062 -->
|
|
647
|
+
```ts
|
|
613
648
|
const s: string | null = "x";
|
|
614
649
|
const n = s?.length;
|
|
615
650
|
```
|
|
@@ -625,7 +660,9 @@ const n = s?.length;
|
|
|
625
660
|
|
|
626
661
|
### Functions
|
|
627
662
|
|
|
628
|
-
|
|
663
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
664
|
+
<!-- nish:ok --allow-panics -->
|
|
665
|
+
```ts
|
|
629
666
|
const add = (a: i32, b: i32): i32 => {
|
|
630
667
|
return a + b;
|
|
631
668
|
};
|
|
@@ -647,7 +684,9 @@ const double = (n: i32): i32 => n * 2; // a concise body is that one `return`
|
|
|
647
684
|
into a `let` first.
|
|
648
685
|
- A non-`void` function must return on every path.
|
|
649
686
|
- Rejected: generators, a method that is `async`, destructured and optional
|
|
650
|
-
(`x?: T`) parameters, overloads, nested function declarations
|
|
687
|
+
(`x?: T`) parameters, overloads, nested function declarations ([the compatibility dialect](guide/using-nish/compat.md)
|
|
688
|
+
accepts `x?: T` read through `??`, a default that is not a constant and
|
|
689
|
+
overload signatures). A default
|
|
651
690
|
(`n: i32 = 1`) or a rest parameter (`...ns: i32[]`) is allowed on a
|
|
652
691
|
function, a method, a constructor or a module `const` arrow — see
|
|
653
692
|
"Optional parameters" in [Recipes for what is missing](#recipes-for-what-is-missing). Type parameters *are*
|
|
@@ -659,7 +698,8 @@ A function may declare type parameters, and **each instantiation is compiled to
|
|
|
659
698
|
its own specialised function** — no boxing, no dictionary, no runtime type
|
|
660
699
|
information. The IR is what somebody would have written by hand at that type.
|
|
661
700
|
|
|
662
|
-
|
|
701
|
+
<!-- nish:ok -->
|
|
702
|
+
```ts
|
|
663
703
|
const identity = <T>(x: T): T => x;
|
|
664
704
|
|
|
665
705
|
export const main = (): i32 => {
|
|
@@ -696,7 +736,8 @@ export const main = (): i32 => {
|
|
|
696
736
|
It is where the value came from that counts — a local copied from a `T`, an
|
|
697
737
|
element of a `T[]` and a narrowed `T | null` are all a `T` — never its type.
|
|
698
738
|
|
|
699
|
-
|
|
739
|
+
<!-- nish:err NL2329 -->
|
|
740
|
+
```ts
|
|
700
741
|
interface Point { x: i32; y: i32; }
|
|
701
742
|
|
|
702
743
|
const getX = <T>(p: T): i32 => p.x;
|
|
@@ -722,7 +763,9 @@ export const main = (): i32 => {
|
|
|
722
763
|
same fields, so implementing your own is refused the same way; import the
|
|
723
764
|
template's `Shape` instead.
|
|
724
765
|
|
|
725
|
-
|
|
766
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
767
|
+
<!-- nish:ok --allow-panics -->
|
|
768
|
+
```ts
|
|
726
769
|
interface Shape { area: i32; }
|
|
727
770
|
|
|
728
771
|
class Circle implements Shape {
|
|
@@ -761,7 +804,8 @@ export const main = (): i32 => areaOf(new Circle(2)) - 12;
|
|
|
761
804
|
identifier, such as a method `Point.shifted` and a function `Point_shifted`
|
|
762
805
|
(NL4008).
|
|
763
806
|
|
|
764
|
-
|
|
807
|
+
<!-- nish:err NL4007 --emit-dts build/docs-ai/nl4007.d.ts -->
|
|
808
|
+
```ts
|
|
765
809
|
export const identity = <T>(x: T): T => x; // exported, never called
|
|
766
810
|
|
|
767
811
|
export const main = (): i32 => 0;
|
|
@@ -773,7 +817,8 @@ A `class` or an `interface` may take type parameters too, and an instantiation
|
|
|
773
817
|
is **an ordinary struct**: `Box<i32>` is `%struct.Box$i32` with `Box`'s fields
|
|
774
818
|
at `T = i32`, and everything the language does to a struct works on it.
|
|
775
819
|
|
|
776
|
-
|
|
820
|
+
<!-- nish:ok -->
|
|
821
|
+
```ts
|
|
777
822
|
class Box<T> {
|
|
778
823
|
value: T;
|
|
779
824
|
constructor(v: T) { this.value = v; }
|
|
@@ -836,7 +881,9 @@ They are inferred from the arguments like a generic function's, and each
|
|
|
836
881
|
(receiver, method type arguments) pair is its own `define`:
|
|
837
882
|
`@Chooser.pick$i32`, `@Box$i32.keep$str`.
|
|
838
883
|
|
|
839
|
-
|
|
884
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
885
|
+
<!-- nish:ok --allow-panics -->
|
|
886
|
+
```ts
|
|
840
887
|
class Chooser {
|
|
841
888
|
flip: boolean = false;
|
|
842
889
|
pick<T>(a: T, b: T): T { return this.flip ? b : a; }
|
|
@@ -867,7 +914,8 @@ export const main = (): i32 => {
|
|
|
867
914
|
included.
|
|
868
915
|
- **A constructor takes none**; its class's are written after `new`.
|
|
869
916
|
|
|
870
|
-
|
|
917
|
+
<!-- nish:err NL2331 -->
|
|
918
|
+
```ts
|
|
871
919
|
class Box<T> {
|
|
872
920
|
value: T;
|
|
873
921
|
constructor(v: T) { this.value = v; }
|
|
@@ -884,7 +932,9 @@ is a top-level function named at the call, or an arrow written right there, and
|
|
|
884
932
|
each one is compiled into its **own copy** of the function, with a direct call
|
|
885
933
|
where the parameter is called — there is no function pointer at run time.
|
|
886
934
|
|
|
887
|
-
|
|
935
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
936
|
+
<!-- nish:ok --allow-panics -->
|
|
937
|
+
```ts
|
|
888
938
|
const map = <T, U>(xs: T[], f: (x: T) => U): U[] => {
|
|
889
939
|
const out: U[] = [];
|
|
890
940
|
for (const x of xs) {
|
|
@@ -936,7 +986,8 @@ export const main = (): i32 => {
|
|
|
936
986
|
or element is refused, NL2759: read it into a `const`); the runners that check their body (`parallelMap`,
|
|
937
987
|
`parallelReduce`, `spawn`, `expose`, `region`, ...) refuse one (NL2734).
|
|
938
988
|
|
|
939
|
-
|
|
989
|
+
<!-- nish:err NL2585 -->
|
|
990
|
+
```ts
|
|
940
991
|
const apply = (f: (x: i32) => i32, x: i32): i32 => f(x);
|
|
941
992
|
|
|
942
993
|
export const main = (): i32 => {
|
|
@@ -953,7 +1004,8 @@ through an alias. It can sit in a local, a field, an array element or a
|
|
|
953
1004
|
return value, be passed for an alias-typed parameter, called and compared
|
|
954
1005
|
with `===` / `!==`:
|
|
955
1006
|
|
|
956
|
-
|
|
1007
|
+
<!-- nish:ok -->
|
|
1008
|
+
```ts
|
|
957
1009
|
type Op = (x: i32) => i32;
|
|
958
1010
|
|
|
959
1011
|
const inc = (x: i32): i32 => x + 1;
|
|
@@ -1039,14 +1091,15 @@ that can fail answers `Promise<Result<T, E>>`: test the `Result` after the
|
|
|
1039
1091
|
before it returns; Node runs `f` only to its first `await` and the rest
|
|
1040
1092
|
after the spawning task yields. Do not rely on either order.
|
|
1041
1093
|
|
|
1042
|
-
|
|
1043
|
-
|
|
1094
|
+
<!-- nish:err NL2642 -->
|
|
1095
|
+
```ts
|
|
1096
|
+
const load = async (id: i32): Promise<i32> => {
|
|
1044
1097
|
return id;
|
|
1045
|
-
}
|
|
1098
|
+
};
|
|
1046
1099
|
|
|
1047
|
-
async
|
|
1100
|
+
const run = async (): Promise<void> => {
|
|
1048
1101
|
console.log(await load(1));
|
|
1049
|
-
}
|
|
1102
|
+
};
|
|
1050
1103
|
|
|
1051
1104
|
export const main = (): i32 => 0;
|
|
1052
1105
|
```
|
|
@@ -1061,7 +1114,9 @@ else to switch on and no thread count to choose. `parallelMap(src, f)` and
|
|
|
1061
1114
|
`parallelFrom(n, f)` answer a new array instead (`f(src[i])`, `f(i)`), under
|
|
1062
1115
|
the same rules for `f`.
|
|
1063
1116
|
|
|
1064
|
-
|
|
1117
|
+
<!-- allow-panics: calls `parallelMapInto`, whose length check no proof removes yet; calls `std/threads.ts` code that keeps panic sites no proof removes yet -->
|
|
1118
|
+
<!-- nish:ok --allow-panics -->
|
|
1119
|
+
```ts
|
|
1065
1120
|
import { parallelMapInto, parallelReduce } from "nish/threads";
|
|
1066
1121
|
|
|
1067
1122
|
const square = (x: f64): f64 => x * x;
|
|
@@ -1075,7 +1130,9 @@ export const main = (): i32 => {
|
|
|
1075
1130
|
};
|
|
1076
1131
|
```
|
|
1077
1132
|
|
|
1078
|
-
|
|
1133
|
+
<!-- allow-panics: indexes an array without a guard the checker reads; calls `std/threads.ts` code that keeps panic sites no proof removes yet -->
|
|
1134
|
+
<!-- nish:ok --allow-panics -->
|
|
1135
|
+
```ts
|
|
1079
1136
|
import { parallelFrom, parallelMap } from "nish/threads";
|
|
1080
1137
|
|
|
1081
1138
|
export const main = (): i32 => {
|
|
@@ -1120,7 +1177,8 @@ export const main = (): i32 => {
|
|
|
1120
1177
|
costs: a body estimated at 3, such as `(x) => x + 1`, is divided only past
|
|
1121
1178
|
about 700,000 elements, a cheaper one later, one with a loop far sooner.
|
|
1122
1179
|
|
|
1123
|
-
|
|
1180
|
+
<!-- nish:err NL2350 -->
|
|
1181
|
+
```ts
|
|
1124
1182
|
import { parallelReduce } from "nish/threads";
|
|
1125
1183
|
|
|
1126
1184
|
export const main = (): i32 => parallelReduce([1, 2, 3], (a, b) => a + b, 1); // `+` folds from 0
|
|
@@ -1132,7 +1190,9 @@ Different functions at once, one thread each: open a scope with `using`, give
|
|
|
1132
1190
|
it tasks with `spawn(entry, arg, dst, at)`, and every task has run and stored
|
|
1133
1191
|
`entry(arg)` into `dst[at]` when the block ends.
|
|
1134
1192
|
|
|
1135
|
-
|
|
1193
|
+
<!-- allow-panics: indexes an array without a guard the checker reads; does integer arithmetic nothing bounds -->
|
|
1194
|
+
<!-- nish:ok --allow-panics -->
|
|
1195
|
+
```ts
|
|
1136
1196
|
import { scope } from "nish/threads";
|
|
1137
1197
|
|
|
1138
1198
|
const sumOf = (xs: f64[]): f64 => xs[0] + xs[1];
|
|
@@ -1183,7 +1243,8 @@ export const main = (): i32 => {
|
|
|
1183
1243
|
- **Any argument will do** — an array, an object — because nothing writes
|
|
1184
1244
|
memory while the tasks run. There is no thread count: one task, one thread.
|
|
1185
1245
|
|
|
1186
|
-
|
|
1246
|
+
<!-- nish:err NL2388 -->
|
|
1247
|
+
```ts
|
|
1187
1248
|
import { scope } from "nish/threads";
|
|
1188
1249
|
|
|
1189
1250
|
const one = (n: i32): i32 => n + 1;
|
|
@@ -1201,7 +1262,9 @@ export const main = (): i32 => {
|
|
|
1201
1262
|
When tasks must update one value, put it in a `Mutex` and reach it only through
|
|
1202
1263
|
`using g = m.lock()`: the lock is held until that block ends, on every exit.
|
|
1203
1264
|
|
|
1204
|
-
|
|
1265
|
+
<!-- allow-panics: does integer arithmetic nothing bounds; indexes an array without a guard the checker reads -->
|
|
1266
|
+
<!-- nish:ok --allow-panics -->
|
|
1267
|
+
```ts
|
|
1205
1268
|
import { Mutex, scope } from "nish/threads";
|
|
1206
1269
|
|
|
1207
1270
|
class Tally {
|
|
@@ -1275,7 +1338,8 @@ export const main = (): i32 => {
|
|
|
1275
1338
|
not an `f64` sum): the critical sections run in the scheduler's order
|
|
1276
1339
|
natively and in spawn order under Node.
|
|
1277
1340
|
|
|
1278
|
-
|
|
1341
|
+
<!-- nish:err NL2467 -->
|
|
1342
|
+
```ts
|
|
1279
1343
|
import { Mutex } from "nish/threads";
|
|
1280
1344
|
|
|
1281
1345
|
class Counter {
|
|
@@ -1290,7 +1354,9 @@ export const main = (): i32 => {
|
|
|
1290
1354
|
};
|
|
1291
1355
|
```
|
|
1292
1356
|
|
|
1293
|
-
|
|
1357
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
1358
|
+
<!-- nish:ok --allow-panics -->
|
|
1359
|
+
```ts
|
|
1294
1360
|
import { Mutex, MutexGuard } from "nish/threads";
|
|
1295
1361
|
|
|
1296
1362
|
class Stats {
|
|
@@ -1315,7 +1381,9 @@ A shared counter, minimum, maximum or flag is an atomic: `new AtomicI32(0)`,
|
|
|
1315
1381
|
`new AtomicI64(toI64(0))`, updated from any task or parallel `f` by one
|
|
1316
1382
|
indivisible method call.
|
|
1317
1383
|
|
|
1318
|
-
|
|
1384
|
+
<!-- allow-panics: calls `parallelMapInto`, whose length check no proof removes yet; does integer arithmetic nothing bounds -->
|
|
1385
|
+
<!-- nish:ok --allow-panics -->
|
|
1386
|
+
```ts
|
|
1319
1387
|
import { AtomicI32, parallelMapInto } from "nish/threads";
|
|
1320
1388
|
|
|
1321
1389
|
export const main = (): i32 => {
|
|
@@ -1348,7 +1416,8 @@ export const main = (): i32 => {
|
|
|
1348
1416
|
to an `AtomicI32[]`, whose counters are separate objects on separate cache
|
|
1349
1417
|
lines. Same rules as above.
|
|
1350
1418
|
|
|
1351
|
-
|
|
1419
|
+
<!-- nish:err NL2513 -->
|
|
1420
|
+
```ts
|
|
1352
1421
|
import { AtomicI32, scope } from "nish/threads";
|
|
1353
1422
|
|
|
1354
1423
|
const bump = (a: AtomicI32): i32 => a.add(1);
|
|
@@ -1373,7 +1442,9 @@ When one task produces values and another consumes them, give both a
|
|
|
1373
1442
|
`for...of`, which waits for values and ends once every producer has returned.
|
|
1374
1443
|
Nobody closes a channel.
|
|
1375
1444
|
|
|
1376
|
-
|
|
1445
|
+
<!-- allow-panics: does integer arithmetic nothing bounds; indexes an array without a guard the checker reads -->
|
|
1446
|
+
<!-- nish:ok --allow-panics -->
|
|
1447
|
+
```ts
|
|
1377
1448
|
import { Channel, scope } from "nish/threads";
|
|
1378
1449
|
|
|
1379
1450
|
class Pipe {
|
|
@@ -1445,7 +1516,8 @@ export const main = (): i32 => {
|
|
|
1445
1516
|
the scheduler's order natively and in spawn order under Node, so a count or an
|
|
1446
1517
|
integer sum prints the same both ways.
|
|
1447
1518
|
|
|
1448
|
-
|
|
1519
|
+
<!-- nish:err NL2481 -->
|
|
1520
|
+
```ts
|
|
1449
1521
|
import { Channel, scope } from "nish/threads";
|
|
1450
1522
|
|
|
1451
1523
|
const one = (ch: Channel<i32>): i32 => {
|
|
@@ -1498,7 +1570,8 @@ export const main = (): i32 => {
|
|
|
1498
1570
|
[refusal table](guide/using-nish/threads.md#refused-and-what-to-write-instead)
|
|
1499
1571
|
pairs each code with an example that compiles.
|
|
1500
1572
|
|
|
1501
|
-
|
|
1573
|
+
<!-- nish:err NL2506 -->
|
|
1574
|
+
```ts
|
|
1502
1575
|
import { scope } from "nish/threads";
|
|
1503
1576
|
|
|
1504
1577
|
export const main = (): i32 => {
|
|
@@ -1520,7 +1593,8 @@ does not define. The symbol is the identifier, unmangled, in any package
|
|
|
1520
1593
|
call is an ordinary call. Two declarations of one name must have the same
|
|
1521
1594
|
types.
|
|
1522
1595
|
|
|
1523
|
-
|
|
1596
|
+
<!-- nish:ok -->
|
|
1597
|
+
```ts
|
|
1524
1598
|
declare function abs(n: i32): i32;
|
|
1525
1599
|
|
|
1526
1600
|
export const main = (): i32 => {
|
|
@@ -1545,7 +1619,8 @@ export const main = (): i32 => {
|
|
|
1545
1619
|
holds one, however deep (NL2746, NL2756), and a page module (freestanding
|
|
1546
1620
|
wasm, `--emit-dts`) refuses one (NL2757).
|
|
1547
1621
|
|
|
1548
|
-
|
|
1622
|
+
<!-- nish:ok -->
|
|
1623
|
+
```ts
|
|
1549
1624
|
declare class Timespec {
|
|
1550
1625
|
sec: i64;
|
|
1551
1626
|
nsec: i64;
|
|
@@ -1560,7 +1635,8 @@ export const main = (): i32 => {
|
|
|
1560
1635
|
};
|
|
1561
1636
|
```
|
|
1562
1637
|
|
|
1563
|
-
|
|
1638
|
+
<!-- nish:err NL2755 -->
|
|
1639
|
+
```ts
|
|
1564
1640
|
type Coord = number;
|
|
1565
1641
|
|
|
1566
1642
|
declare class Point {
|
|
@@ -1583,7 +1659,8 @@ export const main = (): i32 => {
|
|
|
1583
1659
|
borrows the bytes for the call and may write a `u8[]`; a spawned task may
|
|
1584
1660
|
not make the call.
|
|
1585
1661
|
|
|
1586
|
-
|
|
1662
|
+
<!-- nish:err NL2549 -->
|
|
1663
|
+
```ts
|
|
1587
1664
|
declare function SHA256(d: readonly u8[], n: u64, md: u8[]): CPtr;
|
|
1588
1665
|
|
|
1589
1666
|
export const main = (): i32 => {
|
|
@@ -1598,7 +1675,8 @@ export const main = (): i32 => {
|
|
|
1598
1675
|
panics (`ffi-nul`) before the call, because C would read only up to it; a
|
|
1599
1676
|
literal with no NUL is not checked.
|
|
1600
1677
|
|
|
1601
|
-
|
|
1678
|
+
<!-- nish:ok -->
|
|
1679
|
+
```ts
|
|
1602
1680
|
declare function puts(s: string): i32;
|
|
1603
1681
|
|
|
1604
1682
|
export const main = (): i32 => {
|
|
@@ -1613,7 +1691,9 @@ export const main = (): i32 => {
|
|
|
1613
1691
|
`NULL` panics (`ffi-null`). The copy never frees: a result the caller must
|
|
1614
1692
|
`free` (`strdup`) is declared `CPtr | null` instead.
|
|
1615
1693
|
|
|
1616
|
-
|
|
1694
|
+
<!-- allow-panics: reads a C pointer that may be `NULL` -->
|
|
1695
|
+
<!-- nish:ok --allow-panics -->
|
|
1696
|
+
```ts
|
|
1617
1697
|
declare function strerror(errnum: i32): string;
|
|
1618
1698
|
declare function getenv(name: string): string | null;
|
|
1619
1699
|
|
|
@@ -1630,13 +1710,13 @@ export const main = (): i32 => {
|
|
|
1630
1710
|
`p` into a new string; a dangling or unterminated `p` is undefined
|
|
1631
1711
|
behaviour, which is why it needs the import. It takes a `CPtr`, so narrow a
|
|
1632
1712
|
`CPtr | null` with `!== null` first. A `NULL` `p` panics (`ffi-null`) before
|
|
1633
|
-
the copy, so
|
|
1713
|
+
the copy, so the no-panic default refuses the call.
|
|
1634
1714
|
- **A struct C owns is copied out with `readStruct` from `nish:unsafe`.**
|
|
1635
1715
|
`const lc: Lconv = readStruct(p)` copies the bytes at `p` into a new
|
|
1636
1716
|
instance of the `declare class` the context names; write the annotation,
|
|
1637
1717
|
since `readStruct<Lconv>(p)` is refused (NL2763) and a call with no context
|
|
1638
1718
|
is NL2764. A `NULL` `p` panics (`ffi-null`) before the copy, so
|
|
1639
|
-
|
|
1719
|
+
the no-panic default refuses the call; a dangling `p`, or a class that does not
|
|
1640
1720
|
match C's fields, is undefined behaviour.
|
|
1641
1721
|
- **errno is read with `cErrno()` from `nish:unsafe`.** It answers the errno
|
|
1642
1722
|
the most recent `declare function` call on this thread left; naming it makes
|
|
@@ -1647,7 +1727,9 @@ export const main = (): i32 => {
|
|
|
1647
1727
|
`realpath(path, null)` passes `NULL`; a nullable pointer held in a variable
|
|
1648
1728
|
is NL2539, so C is never handed a `NULL` by accident.
|
|
1649
1729
|
|
|
1650
|
-
|
|
1730
|
+
<!-- allow-panics: reads a C pointer that may be `NULL` -->
|
|
1731
|
+
<!-- nish:ok --allow-panics -->
|
|
1732
|
+
```ts
|
|
1651
1733
|
import { readCString } from "nish:unsafe";
|
|
1652
1734
|
|
|
1653
1735
|
declare function realpath(path: string, resolved: CPtr | null): CPtr | null;
|
|
@@ -1692,7 +1774,8 @@ export const main = (): i32 => {
|
|
|
1692
1774
|
|
|
1693
1775
|
### Module constants
|
|
1694
1776
|
|
|
1695
|
-
|
|
1777
|
+
<!-- nish:ok -->
|
|
1778
|
+
```ts
|
|
1696
1779
|
const TOKEN_NAME: i32 = 1;
|
|
1697
1780
|
const TOKEN_END: i32 = TOKEN_NAME + 1;
|
|
1698
1781
|
export const PROMPT: string = "> ";
|
|
@@ -1714,7 +1797,9 @@ emitted and no initialiser runs** — every use lowers to the value itself.
|
|
|
1714
1797
|
|
|
1715
1798
|
### Module storage
|
|
1716
1799
|
|
|
1717
|
-
|
|
1800
|
+
<!-- allow-panics: does integer arithmetic nothing bounds; indexes an array without a guard the checker reads -->
|
|
1801
|
+
<!-- nish:ok --allow-panics -->
|
|
1802
|
+
```ts
|
|
1718
1803
|
const SCORES = new Int32Array(4);
|
|
1719
1804
|
|
|
1720
1805
|
const sum = (xs: Int32Array): i32 => {
|
|
@@ -1753,7 +1838,9 @@ state a module keeps (there is no top-level `let`).
|
|
|
1753
1838
|
|
|
1754
1839
|
### Classes and interfaces
|
|
1755
1840
|
|
|
1756
|
-
|
|
1841
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
1842
|
+
<!-- nish:ok --allow-panics -->
|
|
1843
|
+
```ts
|
|
1757
1844
|
interface Shape { width: i32; height: i32; }
|
|
1758
1845
|
|
|
1759
1846
|
class Rect implements Shape {
|
|
@@ -1783,7 +1870,8 @@ export const main = (): i32 => {
|
|
|
1783
1870
|
- Fields need annotations; an initializer must be a **literal** of the field's
|
|
1784
1871
|
type. Layout is declaration order, exactly as clang lays out the same C
|
|
1785
1872
|
struct.
|
|
1786
|
-
- At most one constructor, no parameter properties, no overloads
|
|
1873
|
+
- At most one constructor, no parameter properties, no overloads (outside
|
|
1874
|
+
[the compatibility dialect](guide/using-nish/compat.md)). **Definite
|
|
1787
1875
|
assignment** is checked syntactically: after the constructor returns every
|
|
1788
1876
|
field holds a value, and reading `this.f` before every field is assigned is
|
|
1789
1877
|
an error.
|
|
@@ -1807,7 +1895,8 @@ and `push` may move the whole block to grow it. **Holding an element across a
|
|
|
1807
1895
|
`push` on the same array is therefore refused**, because the write would land
|
|
1808
1896
|
in the old storage:
|
|
1809
1897
|
|
|
1810
|
-
|
|
1898
|
+
<!-- nish:err NL2290 -->
|
|
1899
|
+
```ts
|
|
1811
1900
|
interface Point { x: i32; }
|
|
1812
1901
|
|
|
1813
1902
|
export const main = (): i32 => {
|
|
@@ -1821,7 +1910,9 @@ export const main = (): i32 => {
|
|
|
1821
1910
|
|
|
1822
1911
|
Index again after the `push` instead of holding the element across it:
|
|
1823
1912
|
|
|
1824
|
-
|
|
1913
|
+
<!-- allow-panics: indexes an array without a guard the checker reads -->
|
|
1914
|
+
<!-- nish:ok --allow-panics -->
|
|
1915
|
+
```ts
|
|
1825
1916
|
interface Point { x: i32; }
|
|
1826
1917
|
|
|
1827
1918
|
export const main = (): i32 => {
|
|
@@ -1841,7 +1932,9 @@ grows, and the blocks it outgrows stay in the arena; a walk over `kids`
|
|
|
1841
1932
|
follows two pointers per child where the links follow one. On a 4M-node
|
|
1842
1933
|
tree the links took 0.52x the memory and 0.68x the time of `kids`.
|
|
1843
1934
|
|
|
1844
|
-
|
|
1935
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
1936
|
+
<!-- nish:ok --allow-panics -->
|
|
1937
|
+
```ts
|
|
1845
1938
|
class Node {
|
|
1846
1939
|
kind: i32;
|
|
1847
1940
|
parent: Node | null;
|
|
@@ -1896,7 +1989,9 @@ export const main = (): i32 => {
|
|
|
1896
1989
|
- `uncheckedGet` / `uncheckedSet` belong in that table's code only: an index
|
|
1897
1990
|
out of range there is undefined behaviour, not a panic.
|
|
1898
1991
|
|
|
1899
|
-
|
|
1992
|
+
<!-- allow-panics: builds an array whose length it does not bound; does integer arithmetic nothing bounds -->
|
|
1993
|
+
<!-- nish:ok --allow-panics -->
|
|
1994
|
+
```ts
|
|
1900
1995
|
import { uncheckedGet, uncheckedSet } from "nish:unsafe";
|
|
1901
1996
|
|
|
1902
1997
|
// One column per field, each allocated once at its final length.
|
|
@@ -1942,7 +2037,8 @@ enclosing function's return type, the parameter it is passed to, the field it
|
|
|
1942
2037
|
is assigned to, or a field of an enclosing literal. It must set every field
|
|
1943
2038
|
exactly once, with plain identifier keys.
|
|
1944
2039
|
|
|
1945
|
-
|
|
2040
|
+
<!-- nish:ok -->
|
|
2041
|
+
```ts
|
|
1946
2042
|
interface Pair { first: i32; second: i32; }
|
|
1947
2043
|
|
|
1948
2044
|
const swap = (p: Pair): Pair => ({ first: p.second, second: p.first });
|
|
@@ -1962,7 +2058,8 @@ A class with methods is made only by `new`: a literal typed as one would be a
|
|
|
1962
2058
|
plain object under Node, without the methods (NL2556). A class with fields
|
|
1963
2059
|
alone, and an interface, still take a literal.
|
|
1964
2060
|
|
|
1965
|
-
|
|
2061
|
+
<!-- nish:err NL2556 -->
|
|
2062
|
+
```ts
|
|
1966
2063
|
class Counter {
|
|
1967
2064
|
n: i32;
|
|
1968
2065
|
constructor(n: i32) {
|
|
@@ -1981,7 +2078,8 @@ export const main = (): i32 => {
|
|
|
1981
2078
|
|
|
1982
2079
|
### Type aliases and enums
|
|
1983
2080
|
|
|
1984
|
-
|
|
2081
|
+
<!-- nish:ok -->
|
|
2082
|
+
```ts
|
|
1985
2083
|
type Byte = u8;
|
|
1986
2084
|
type Bytes = Byte[];
|
|
1987
2085
|
|
|
@@ -2027,7 +2125,8 @@ export const main = (): i32 => {
|
|
|
2027
2125
|
`Role.parse` yet: test `s === String(Role.Admin)` per member, or decode it
|
|
2028
2126
|
with typed JSON.
|
|
2029
2127
|
|
|
2030
|
-
|
|
2128
|
+
<!-- nish:ok -->
|
|
2129
|
+
```ts
|
|
2031
2130
|
enum Role {
|
|
2032
2131
|
Admin = "admin",
|
|
2033
2132
|
Guest = "guest",
|
|
@@ -2074,7 +2173,8 @@ export const main = (): i32 => {
|
|
|
2074
2173
|
`parallelFrom` or `parallelReduce` body answering one is NL2521: answer
|
|
2075
2174
|
`Kind` instead.
|
|
2076
2175
|
|
|
2077
|
-
|
|
2176
|
+
<!-- nish:ok -->
|
|
2177
|
+
```ts
|
|
2078
2178
|
enum Kind {
|
|
2079
2179
|
Scalar,
|
|
2080
2180
|
Array,
|
|
@@ -2093,7 +2193,8 @@ export const main = (): i32 => {
|
|
|
2093
2193
|
};
|
|
2094
2194
|
```
|
|
2095
2195
|
|
|
2096
|
-
|
|
2196
|
+
<!-- nish:err NL2519 -->
|
|
2197
|
+
```ts
|
|
2097
2198
|
enum Kind {
|
|
2098
2199
|
Scalar,
|
|
2099
2200
|
Array,
|
|
@@ -2117,7 +2218,9 @@ export const main = (): i32 => (pick(Kind.Array) === Kind.Array ? 0 : 1);
|
|
|
2117
2218
|
|
|
2118
2219
|
### Discriminated unions
|
|
2119
2220
|
|
|
2120
|
-
|
|
2221
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
2222
|
+
<!-- nish:ok --allow-panics -->
|
|
2223
|
+
```ts
|
|
2121
2224
|
enum Kind {
|
|
2122
2225
|
Circle,
|
|
2123
2226
|
Square,
|
|
@@ -2249,7 +2352,8 @@ export const main = (): i32 => {
|
|
|
2249
2352
|
from there on. Declare the slot as the variant, or store a value of the
|
|
2250
2353
|
whole union (a call returning `Shape`):
|
|
2251
2354
|
|
|
2252
|
-
|
|
2355
|
+
<!-- nish:err NL2534 -->
|
|
2356
|
+
```ts
|
|
2253
2357
|
enum Kind {
|
|
2254
2358
|
Circle,
|
|
2255
2359
|
Square,
|
|
@@ -2302,9 +2406,10 @@ export const main = (): i32 => {
|
|
|
2302
2406
|
- Functions and enums may be renamed on import; classes and interfaces may
|
|
2303
2407
|
not — the type name is part of the ABI.
|
|
2304
2408
|
- Import cycles are allowed; a shared dependency is compiled once.
|
|
2305
|
-
- **
|
|
2306
|
-
|
|
2307
|
-
|
|
2409
|
+
- **An exported function name is unique across the whole program**; two
|
|
2410
|
+
modules may each keep a *private* function or generic of one name (its
|
|
2411
|
+
symbol becomes `<module>$.<name>`, e.g. `helper$.add`), except under
|
|
2412
|
+
`--no-strict-exports`, where every function name is unique.
|
|
2308
2413
|
- An `export`ed function is an external C-ABI symbol; every other function gets
|
|
2309
2414
|
`internal` linkage so LLVM may inline or drop it.
|
|
2310
2415
|
- **So an exported function is not named after a C library function**
|
|
@@ -2363,7 +2468,9 @@ export const main = (): i32 => {
|
|
|
2363
2468
|
`break`, `continue`, `process.exit`, an `if` whose branches all return, or an
|
|
2364
2469
|
infinite loop.
|
|
2365
2470
|
|
|
2366
|
-
|
|
2471
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
2472
|
+
<!-- nish:ok --allow-panics -->
|
|
2473
|
+
```ts
|
|
2367
2474
|
const firstPair = (limit: i32, target: i32): i32 => {
|
|
2368
2475
|
outer: for (let i: i32 = 0; i < limit; i++) {
|
|
2369
2476
|
for (let j: i32 = 0; j < limit; j++) {
|
|
@@ -2482,7 +2589,8 @@ refused with where it was made; pass a fresh value, or a copy
|
|
|
2482
2589
|
`secret(copy)`). `std/crypto`'s `p256`, `x25519` and
|
|
2483
2590
|
`x509ParseP256PrivateKey` take and give keys this way.
|
|
2484
2591
|
|
|
2485
|
-
|
|
2592
|
+
<!-- nish:ok -->
|
|
2593
|
+
```ts
|
|
2486
2594
|
import { Secret, expose, secret, wipe } from "nish:secret";
|
|
2487
2595
|
|
|
2488
2596
|
const key = (): u8[] => [7, 1, 9];
|
|
@@ -2496,7 +2604,8 @@ export const main = (): i32 => {
|
|
|
2496
2604
|
};
|
|
2497
2605
|
```
|
|
2498
2606
|
|
|
2499
|
-
|
|
2607
|
+
<!-- nish:err NL2440 -->
|
|
2608
|
+
```ts
|
|
2500
2609
|
import { Secret, expose, secret } from "nish:secret";
|
|
2501
2610
|
|
|
2502
2611
|
const key = (): u8[] => [7, 1, 9];
|
|
@@ -2508,7 +2617,8 @@ export const main = (): i32 => {
|
|
|
2508
2617
|
};
|
|
2509
2618
|
```
|
|
2510
2619
|
|
|
2511
|
-
|
|
2620
|
+
<!-- nish:err NL2512 -->
|
|
2621
|
+
```ts
|
|
2512
2622
|
import { Secret, secret, wipe } from "nish:secret";
|
|
2513
2623
|
|
|
2514
2624
|
export const main = (): i32 => {
|
|
@@ -2531,7 +2641,8 @@ parameters, calling `wipe(this.k)` itself for every such field on every path
|
|
|
2531
2641
|
`Secret` borrows it: it neither returns it (NL2546) nor wipes it (NL2548). Nothing checks that `wipe()` is called: call it when the
|
|
2532
2642
|
holder is done.
|
|
2533
2643
|
|
|
2534
|
-
|
|
2644
|
+
<!-- nish:ok -->
|
|
2645
|
+
```ts
|
|
2535
2646
|
import { Secret, expose, secret, wipe } from "nish:secret";
|
|
2536
2647
|
|
|
2537
2648
|
const width = (k: u8[]): i32 => toI32(k.length);
|
|
@@ -2557,7 +2668,8 @@ export const main = (): i32 => {
|
|
|
2557
2668
|
};
|
|
2558
2669
|
```
|
|
2559
2670
|
|
|
2560
|
-
|
|
2671
|
+
<!-- nish:err NL2544 -->
|
|
2672
|
+
```ts
|
|
2561
2673
|
import { Secret, secret, wipe } from "nish:secret";
|
|
2562
2674
|
|
|
2563
2675
|
class Session {
|
|
@@ -2792,7 +2904,9 @@ was there) and `clear()`, and `keys()` / `values()` as the iterable of a
|
|
|
2792
2904
|
and `s.forEach((x) => ...)` compile to that `for...of` walk with `f` inlined,
|
|
2793
2905
|
and answer `void`. `entries` is refused by name.
|
|
2794
2906
|
|
|
2795
|
-
|
|
2907
|
+
<!-- allow-panics: calls `std/collections.ts` code that keeps panic sites no proof removes yet -->
|
|
2908
|
+
<!-- nish:ok-body --allow-panics -->
|
|
2909
|
+
```ts
|
|
2796
2910
|
const seen = new Set<string>();
|
|
2797
2911
|
const counts: Map<string, i32> = new Map();
|
|
2798
2912
|
seen.add("a").add("b").add("a");
|
|
@@ -2818,7 +2932,9 @@ console.log(`${seen.size} ${counts.size} ${seen.has("b")}`);
|
|
|
2818
2932
|
three ways: a default with `??`, a `const` tested with `!== undefined` (or
|
|
2819
2933
|
`=== undefined` and an early `return`), or a bare test. Each is one probe.
|
|
2820
2934
|
|
|
2821
|
-
|
|
2935
|
+
<!-- allow-panics: does integer arithmetic nothing bounds; calls `std/collections.ts` code that keeps panic sites no proof removes yet -->
|
|
2936
|
+
<!-- nish:ok-body --allow-panics -->
|
|
2937
|
+
```ts
|
|
2822
2938
|
const counts = new Map<string, i32>();
|
|
2823
2939
|
for (const w of ["a", "b", "a"]) {
|
|
2824
2940
|
counts.set(w, (counts.get(w) ?? 0) + 1);
|
|
@@ -2838,7 +2954,8 @@ if (a !== undefined) {
|
|
|
2838
2954
|
be the value type. For a `Map<K, Node | null>` it replaces a stored `null`
|
|
2839
2955
|
too, but the result is still `Node | null`.
|
|
2840
2956
|
|
|
2841
|
-
|
|
2957
|
+
<!-- nish:err-body NL2361 -->
|
|
2958
|
+
```ts
|
|
2842
2959
|
const m = new Map<string, i32>();
|
|
2843
2960
|
let v = m.get("a");
|
|
2844
2961
|
```
|
|
@@ -2849,7 +2966,9 @@ may `set`, `delete` or `clear` the table inside the walk and get exactly what
|
|
|
2849
2966
|
JavaScript gives (a new key is visited later in the same walk; a key deleted
|
|
2850
2967
|
before it is reached is not).
|
|
2851
2968
|
|
|
2852
|
-
|
|
2969
|
+
<!-- allow-panics: does integer arithmetic nothing bounds; calls `std/collections.ts` code that keeps panic sites no proof removes yet -->
|
|
2970
|
+
<!-- nish:ok-body --allow-panics -->
|
|
2971
|
+
```ts
|
|
2853
2972
|
const ages = new Map<string, i32>();
|
|
2854
2973
|
ages.set("ada", 36).set("alan", 41);
|
|
2855
2974
|
ages.delete("alan");
|
|
@@ -2869,7 +2988,8 @@ console.log(`${total}`); // 121
|
|
|
2869
2988
|
- `for (const e of m)` over a `Map` itself is refused, because its
|
|
2870
2989
|
`[key, value]` pairs are two-type tuples: walk `m.keys()` and call `m.get(k)`.
|
|
2871
2990
|
|
|
2872
|
-
|
|
2991
|
+
<!-- nish:err-body NL2375 -->
|
|
2992
|
+
```ts
|
|
2873
2993
|
const m = new Map<string, i32>();
|
|
2874
2994
|
for (const e of m) {
|
|
2875
2995
|
}
|
|
@@ -2887,7 +3007,9 @@ string `+`) or assign. Anything else still compiles, as one probe per call.
|
|
|
2887
3007
|
inserting it — and `reserve(m, n)`, which sizes the table for `n` entries; both
|
|
2888
3008
|
are one module with the program and run unchanged under Node.
|
|
2889
3009
|
|
|
2890
|
-
|
|
3010
|
+
<!-- allow-panics: does integer arithmetic nothing bounds; calls `std/collections.ts` code that keeps panic sites no proof removes yet -->
|
|
3011
|
+
<!-- nish:ok --allow-panics -->
|
|
3012
|
+
```ts
|
|
2891
3013
|
import { getOrInsert, reserve } from "nish/map";
|
|
2892
3014
|
|
|
2893
3015
|
export const main = (): i32 => {
|
|
@@ -2950,7 +3072,9 @@ tag under one key (NL2721) as a different string (NL2720). A `tag` on a class
|
|
|
2950
3072
|
no union holds does nothing, and a JSON call on that class refuses its
|
|
2951
3073
|
enum-member field.
|
|
2952
3074
|
|
|
2953
|
-
|
|
3075
|
+
<!-- allow-panics: builds an array whose length it does not bound; indexes an array without a guard the checker reads; calls `std/json.ts` code that keeps panic sites no proof removes yet -->
|
|
3076
|
+
<!-- nish:ok --allow-panics -->
|
|
3077
|
+
```ts
|
|
2954
3078
|
import { JsonWriter } from "nish/json";
|
|
2955
3079
|
import { JsonLimits } from "nish/json-tokenizer";
|
|
2956
3080
|
import { JsonDecoder, jsonDecode, jsonEncode } from "nish/json-typed";
|
|
@@ -2974,7 +3098,8 @@ export const main = (): i32 => {
|
|
|
2974
3098
|
};
|
|
2975
3099
|
```
|
|
2976
3100
|
|
|
2977
|
-
|
|
3101
|
+
<!-- nish:err NL2485 -->
|
|
3102
|
+
```ts
|
|
2978
3103
|
import { JsonWriter } from "nish/json";
|
|
2979
3104
|
import { jsonEncode } from "nish/json-typed";
|
|
2980
3105
|
|
|
@@ -2989,7 +3114,9 @@ export const main = (): i32 => {
|
|
|
2989
3114
|
};
|
|
2990
3115
|
```
|
|
2991
3116
|
|
|
2992
|
-
|
|
3117
|
+
<!-- allow-panics: indexes an array without a guard the checker reads; calls `std/json.ts` code that keeps panic sites no proof removes yet -->
|
|
3118
|
+
<!-- nish:ok --allow-panics -->
|
|
3119
|
+
```ts
|
|
2993
3120
|
import { JsonWriter } from "nish/json";
|
|
2994
3121
|
import { JsonDecoder, JsonNumber, jsonDecode, jsonEncode } from "nish/json-typed";
|
|
2995
3122
|
|
|
@@ -3010,7 +3137,8 @@ export const writeOrder = (w: JsonWriter, o: Order): void => {
|
|
|
3010
3137
|
};
|
|
3011
3138
|
```
|
|
3012
3139
|
|
|
3013
|
-
|
|
3140
|
+
<!-- nish:err NL2495 -->
|
|
3141
|
+
```ts
|
|
3014
3142
|
import { JsonWriter } from "nish/json";
|
|
3015
3143
|
import { jsonEncode } from "nish/json-typed";
|
|
3016
3144
|
|
|
@@ -3026,7 +3154,8 @@ export const main = (): i32 => {
|
|
|
3026
3154
|
};
|
|
3027
3155
|
```
|
|
3028
3156
|
|
|
3029
|
-
|
|
3157
|
+
<!-- nish:err NL2485 -->
|
|
3158
|
+
```ts
|
|
3030
3159
|
import { JsonWriter } from "nish/json";
|
|
3031
3160
|
import { jsonEncode } from "nish/json-typed";
|
|
3032
3161
|
|
|
@@ -3065,7 +3194,9 @@ outlive it — so allocate what must outlive the block before it, and let only
|
|
|
3065
3194
|
numbers, booleans and enums out. `arena()` is only ever a `using` initialiser,
|
|
3066
3195
|
and `a` is never read.
|
|
3067
3196
|
|
|
3068
|
-
|
|
3197
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
3198
|
+
<!-- nish:ok-body --allow-panics -->
|
|
3199
|
+
```ts
|
|
3069
3200
|
const rows: string[] = ["a", "bb"];
|
|
3070
3201
|
let total = 0;
|
|
3071
3202
|
for (const row of rows) {
|
|
@@ -3105,7 +3236,9 @@ entry — use a region.** `region(f)` / `regionWith(arg, f)` make one and run
|
|
|
3105
3236
|
gives all of it back. `f` is a named function or an arrow that captures
|
|
3106
3237
|
nothing. `Region<T>` is a number, not a pointer.
|
|
3107
3238
|
|
|
3108
|
-
|
|
3239
|
+
<!-- allow-panics: does integer arithmetic nothing bounds; calls `std/region.ts` code that keeps panic sites no proof removes yet -->
|
|
3240
|
+
<!-- nish:ok --allow-panics -->
|
|
3241
|
+
```ts
|
|
3109
3242
|
import { Region, enter, free, region } from "nish:region";
|
|
3110
3243
|
|
|
3111
3244
|
class Session {
|
|
@@ -3140,7 +3273,9 @@ the module (NL4015).
|
|
|
3140
3273
|
|
|
3141
3274
|
**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
3275
|
|
|
3143
|
-
|
|
3276
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
3277
|
+
<!-- nish:ok-body --allow-panics -->
|
|
3278
|
+
```ts
|
|
3144
3279
|
const xs: i32[] = [1, 2, 3];
|
|
3145
3280
|
const doubled = xs.map((x) => x * 2);
|
|
3146
3281
|
let sum = 0;
|
|
@@ -3150,9 +3285,12 @@ for (const x of doubled) {
|
|
|
3150
3285
|
console.log(`${sum}`);
|
|
3151
3286
|
```
|
|
3152
3287
|
|
|
3153
|
-
**Convert a number to a string** —
|
|
3288
|
+
**Convert a number to a string** — a number has no `toString`, and
|
|
3289
|
+
`String()` converts only a [string enum](#type-aliases-and-enums)'s value;
|
|
3290
|
+
write a template literal:
|
|
3154
3291
|
|
|
3155
|
-
|
|
3292
|
+
<!-- nish:ok-body -->
|
|
3293
|
+
```ts
|
|
3156
3294
|
const n = 42;
|
|
3157
3295
|
const s = `${n}`;
|
|
3158
3296
|
console.log(s);
|
|
@@ -3160,7 +3298,8 @@ console.log(s);
|
|
|
3160
3298
|
|
|
3161
3299
|
**Return two values** — there are no tuples: declare an interface.
|
|
3162
3300
|
|
|
3163
|
-
|
|
3301
|
+
<!-- nish:ok -->
|
|
3302
|
+
```ts
|
|
3164
3303
|
interface Split { head: string; rest: string; }
|
|
3165
3304
|
|
|
3166
3305
|
const cut = (s: string, at: i32): Split => ({
|
|
@@ -3351,7 +3490,9 @@ guide's [terminal page](guide/using-nish/terminal.md) its walk-through.)
|
|
|
3351
3490
|
dispatch. Give each class the interface's fields **first**, `implements` it,
|
|
3352
3491
|
and write a free function over the interface:
|
|
3353
3492
|
|
|
3354
|
-
|
|
3493
|
+
<!-- allow-panics: does integer arithmetic nothing bounds -->
|
|
3494
|
+
<!-- nish:ok --allow-panics -->
|
|
3495
|
+
```ts
|
|
3355
3496
|
interface Sized { width: i32; height: i32; }
|
|
3356
3497
|
|
|
3357
3498
|
class Photo implements Sized {
|
|
@@ -3385,10 +3526,16 @@ emitting code that cannot compile.
|
|
|
3385
3526
|
|
|
3386
3527
|
### Proving it cannot panic
|
|
3387
3528
|
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3529
|
+
Every module of the program's own package is held to no panic by default:
|
|
3530
|
+
every place it could stop with a panic is an error (NL2457) until a proof
|
|
3531
|
+
removes it; out of memory is the one kind allowed. `--allow-panics` relaxes
|
|
3532
|
+
that to the modules the root `package.json` lists in
|
|
3533
|
+
`"nish": { "noPanic": [...] }`, and `--deny-panics` spells the default out.
|
|
3534
|
+
**Fix a refusal; do not relax it.** Keep the default, add the guard the error
|
|
3535
|
+
names, and never reach for `--allow-panics`, a `nish:unsafe` import or a guard
|
|
3536
|
+
that proves nothing (one whose `else` branch returns a made-up value the
|
|
3537
|
+
caller cannot tell from a real one) to make an error go away. Each error names
|
|
3538
|
+
its fix, and there are five:
|
|
3392
3539
|
|
|
3393
3540
|
- **Guard** what the checker cannot prove: `if (i >= 0 && i < xs.length)` before
|
|
3394
3541
|
`xs[i]` (`if (toU32(i) < toU32(xs.length))` for a `u8`, `u16` or `u32` index,
|
|
@@ -3406,11 +3553,13 @@ allowed. Each error names its fix, and there are five:
|
|
|
3406
3553
|
supply a value instead of stopping.
|
|
3407
3554
|
- **Swap `readFileSync` for `readFileSyncOrNull`**, which answers `null` where
|
|
3408
3555
|
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.
|
|
3556
|
+
- **Read the list**: `nish file.ts --allow-panics --emit-panics panics.json`
|
|
3557
|
+
writes every site per function, proven or not, and the `"panics"` of
|
|
3558
|
+
`--emit-capabilities` the same. Pass `--allow-panics` to get it: a build the
|
|
3559
|
+
default refuses writes neither file.
|
|
3412
3560
|
|
|
3413
|
-
|
|
3561
|
+
<!-- nish:ok --deny-panics -->
|
|
3562
|
+
```ts
|
|
3414
3563
|
const at = (xs: i32[], i: i32): i32 => {
|
|
3415
3564
|
if (i >= 0 && i < xs.length) {
|
|
3416
3565
|
return xs[i];
|
|
@@ -3445,19 +3594,24 @@ export const main = (): i32 => {
|
|
|
3445
3594
|
|
|
3446
3595
|
The same function without its guard is refused, and the error names the guard:
|
|
3447
3596
|
|
|
3448
|
-
|
|
3597
|
+
<!-- nish:err NL2457 --deny-panics -->
|
|
3598
|
+
```ts
|
|
3449
3599
|
export const at = (xs: i32[], i: i32): i32 => xs[i];
|
|
3450
3600
|
```
|
|
3451
3601
|
|
|
3452
3602
|
A call into the standard library or a dependency that may panic is refused at
|
|
3453
3603
|
the call (NL2458), because only its own author can prove it: call a function
|
|
3454
|
-
that cannot panic
|
|
3604
|
+
that cannot panic. The standard library is not yet panic-free, so `Map`,
|
|
3605
|
+
`Set`, `split` and the `JSON` reads are refused in the scope for now;
|
|
3606
|
+
a program that needs them builds with `--allow-panics`, and says why where it
|
|
3607
|
+
is built.
|
|
3455
3608
|
`uncheckedGet` and `uncheckedSet` are not proofs, but
|
|
3456
3609
|
they cannot panic, and their `nish:unsafe` import is the module's visible
|
|
3457
3610
|
opt-in to an index out of range being undefined behaviour, so the scope allows
|
|
3458
3611
|
them (as `"allowed": true` sites) and a call into them is never refused:
|
|
3459
3612
|
|
|
3460
|
-
|
|
3613
|
+
<!-- nish:ok --deny-panics -->
|
|
3614
|
+
```ts
|
|
3461
3615
|
import { uncheckedGet } from "nish:unsafe";
|
|
3462
3616
|
|
|
3463
3617
|
export const first = (xs: i32[]): i32 => uncheckedGet(xs, 0);
|
|
@@ -3465,7 +3619,8 @@ export const first = (xs: i32[]): i32 => uncheckedGet(xs, 0);
|
|
|
3465
3619
|
|
|
3466
3620
|
Without the import the call is refused (NL2456), scope or not:
|
|
3467
3621
|
|
|
3468
|
-
|
|
3622
|
+
<!-- nish:err NL2456 --deny-panics -->
|
|
3623
|
+
```ts
|
|
3469
3624
|
export const first = (xs: i32[]): i32 => uncheckedGet(xs, 0);
|
|
3470
3625
|
```
|
|
3471
3626
|
|
|
@@ -3483,7 +3638,9 @@ that keeps to it ([LANGUAGE.md](LANGUAGE.md#the-capability-policy)). Under an
|
|
|
3483
3638
|
allowlist `exit` is a capability like the others, and `unsafe` can be denied
|
|
3484
3639
|
but never allowed: importing `nish:unsafe` is the opt-in.
|
|
3485
3640
|
|
|
3486
|
-
|
|
3641
|
+
<!-- allow-panics: it shows NL2459, not a panic site; calls a file or entropy builtin that exits on failure -->
|
|
3642
|
+
<!-- nish:err NL2459 --deny fs.read --allow-panics -->
|
|
3643
|
+
```ts
|
|
3487
3644
|
const settings = (): string => readFileSync("settings.txt");
|
|
3488
3645
|
|
|
3489
3646
|
export const main = (): i32 => settings().length;
|
|
@@ -3530,8 +3687,7 @@ Run it. `nish file.ts --json` is one command and it is the only proof.
|
|
|
3530
3687
|
prints the one-line summary ([LANGUAGE.md](LANGUAGE.md#capabilities)).
|
|
3531
3688
|
A package that must not touch the network says so in `package.json`
|
|
3532
3689
|
([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,
|
|
3690
|
+
11. Must it never panic? It is held to that by default: every index, division,
|
|
3535
3691
|
signed `+ - *`, `pop`, range entry, `expect` and exiting call it cannot
|
|
3536
3692
|
prove is an error
|
|
3537
3693
|
naming the guard ([Proving it cannot panic](#proving-it-cannot-panic)); an
|
|
@@ -3543,6 +3699,6 @@ Run it. `nish file.ts --json` is one command and it is the only proof.
|
|
|
3543
3699
|
`LANGUAGE.md` rule and cookbook entry, and a conventional commit message,
|
|
3544
3700
|
which is its changelog entry.
|
|
3545
3701
|
|
|
3546
|
-
Every
|
|
3702
|
+
Every block under a `<!-- nish:ok -->` or `<!-- nish:err -->` comment on this page is compiled by
|
|
3547
3703
|
`npm test`, so an example that has gone stale is a failing test rather than a
|
|
3548
3704
|
program that no longer works.
|