nish 0.49.1 → 0.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/README.md +27 -21
  2. package/docs/AI.md +237 -160
  3. package/docs/INSTALL.md +1 -1
  4. package/package.json +12 -12
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +94 -11
  7. package/runtime/nish.h +5 -0
  8. package/runtime/runtime-host.c +21 -5
  9. package/std/README.md +14 -11
  10. package/std/base64.ts +74 -35
  11. package/std/bytes.ts +207 -97
  12. package/std/cli-args.ts +150 -107
  13. package/std/compress/checksum.ts +68 -10
  14. package/std/compress/deflate.ts +213 -115
  15. package/std/compress/gzip.ts +124 -49
  16. package/std/compress/zlib.ts +57 -15
  17. package/std/crypto/aes.ts +51 -9
  18. package/std/crypto/base64url.ts +93 -66
  19. package/std/crypto/bcrypt.ts +25 -21
  20. package/std/crypto/ed25519.ts +12 -10
  21. package/std/crypto/hkdf.ts +405 -283
  22. package/std/crypto/hmac.ts +484 -149
  23. package/std/crypto/jwt.ts +51 -40
  24. package/std/crypto/p256.ts +14 -19
  25. package/std/crypto/pki-roots.ts +35 -12
  26. package/std/crypto/pki.ts +43 -55
  27. package/std/crypto/rsa-sign.ts +1635 -0
  28. package/std/crypto/rsa.ts +26 -4
  29. package/std/crypto/sha256.ts +212 -90
  30. package/std/crypto/sha512.ts +135 -49
  31. package/std/crypto/x25519.ts +12 -11
  32. package/std/crypto/x509.ts +164 -40
  33. package/std/gpu/sys.ts +490 -0
  34. package/std/json-equal.ts +33 -18
  35. package/std/json-format.ts +39 -8
  36. package/std/json-number.ts +15 -3
  37. package/std/json-tokenizer.ts +4 -3
  38. package/std/json-typed.ts +12 -6
  39. package/std/json-value.ts +236 -119
  40. package/std/json.ts +81 -46
  41. package/std/net/buffer-pool.ts +25 -14
  42. package/std/net/dns-config.ts +18 -5
  43. package/std/net/dns-message.ts +60 -27
  44. package/std/net/dns-resolver.ts +35 -16
  45. package/std/net/dns.ts +3 -2
  46. package/std/net/fetch.ts +224 -61
  47. package/std/net/handler.ts +189 -88
  48. package/std/net/hpack.ts +216 -103
  49. package/std/net/http-compress.ts +104 -42
  50. package/std/net/http-fields.ts +327 -31
  51. package/std/net/http-server.ts +326 -24
  52. package/std/net/http1-client-codec.ts +159 -55
  53. package/std/net/http1-codec.ts +427 -237
  54. package/std/net/http1-server.ts +220 -38
  55. package/std/net/http2-cleartext.ts +10 -4
  56. package/std/net/http2-frame.ts +2 -2
  57. package/std/net/http2-tls.ts +3 -3
  58. package/std/net/http2.ts +69 -15
  59. package/std/net/http3-frame.ts +11 -14
  60. package/std/net/http3-server.ts +139 -60
  61. package/std/net/http3.ts +140 -79
  62. package/std/net/multipart.ts +592 -0
  63. package/std/net/outbound.ts +24 -8
  64. package/std/net/postgres-async.ts +97 -64
  65. package/std/net/postgres-carrier.ts +28 -4
  66. package/std/net/postgres.ts +166 -65
  67. package/std/net/prefork.ts +86 -5
  68. package/std/net/protobuf.ts +6 -2
  69. package/std/net/qpack.ts +147 -40
  70. package/std/net/quic-conn-ack.ts +27 -7
  71. package/std/net/quic-conn-cid.ts +13 -7
  72. package/std/net/quic-datagram.ts +6 -5
  73. package/std/net/quic-frame.ts +67 -23
  74. package/std/net/quic-listener.ts +61 -39
  75. package/std/net/quic-packet.ts +91 -50
  76. package/std/net/quic-recovery.ts +11 -9
  77. package/std/net/quic-stream.ts +36 -27
  78. package/std/net/quic.ts +84 -54
  79. package/std/net/router.ts +136 -24
  80. package/std/net/serve.ts +59 -5
  81. package/std/net/static.ts +86 -36
  82. package/std/net/tls/client.ts +81 -34
  83. package/std/net/tls/codec.ts +3 -4
  84. package/std/net/tls/record.ts +80 -35
  85. package/std/net/tls/schedule.ts +339 -149
  86. package/std/net/tls/ticket.ts +6 -5
  87. package/std/net/tls-tcp.ts +69 -30
  88. package/std/net/tls.ts +194 -50
  89. package/std/net/upstream.ts +83 -38
  90. package/std/net/url.ts +170 -94
  91. package/std/net/websocket.ts +19 -20
  92. package/std/net/webtransport.ts +123 -33
  93. package/std/raster.ts +12 -2
  94. package/std/regex.ts +600 -388
  95. package/std/sqlite.ts +37 -19
  96. package/std/sync/ring.ts +232 -115
  97. package/std/sync/shm.ts +11 -4
  98. package/std/temporal.ts +232 -133
  99. package/std/term-io.ts +12 -6
  100. package/std/term-pixels.ts +176 -24
  101. package/std/term-rate.ts +8 -3
  102. package/std/term.ts +4 -4
  103. package/std/text.ts +38 -16
  104. package/std/yaml-json.ts +238 -106
  105. package/std/yaml-lines.ts +47 -27
  106. package/std/yaml-write.ts +58 -24
  107. package/std/yaml.ts +808 -395
package/docs/AI.md CHANGED
@@ -131,8 +131,10 @@ are one node per line, which is to say diffable.
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`.
134
+ `y > 0` test ([Proving it cannot panic](#proving-it-cannot-panic)). So do
135
+ the examples below: a sum checks each value against its range and caps the
136
+ running total, and the few that still build with `--allow-panics` say why above
137
+ the block.
136
138
 
137
139
  <!-- nish:ok -->
138
140
  ```ts
@@ -263,25 +265,29 @@ only once the program has no other error, so they show up last.
263
265
  | `if (c) … else if (c) …`, the same condition again | NL2716 | the condition you meant for the second branch |
264
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 |
265
267
 
266
- <!-- allow-panics: it shows NL2712, not a panic site; does integer arithmetic nothing bounds -->
267
- <!-- nish:err-body NL2712 --allow-panics -->
268
+ <!-- nish:err-body NL2712 -->
268
269
  ```ts
269
- const xs = [3, 4];
270
+ const xs: u8[] = [3, 4];
270
271
  let total = 0;
271
272
  for (const x of xs) {
272
- total = total + x;
273
+ if (total > 1000000) {
274
+ process.exit(1); // past what this sum is for
275
+ }
276
+ total = total + toI32(x);
273
277
  }
274
278
  const average = total / 2;
275
279
  console.log(`${total}`);
276
280
  ```
277
281
 
278
- <!-- allow-panics: does integer arithmetic nothing bounds -->
279
- <!-- nish:ok-body --allow-panics -->
282
+ <!-- nish:ok-body -->
280
283
  ```ts
281
- const xs = [3, 4];
284
+ const xs: u8[] = [3, 4];
282
285
  let total = 0;
283
286
  for (const x of xs) {
284
- total = total + x;
287
+ if (total > 1000000) {
288
+ process.exit(1); // past what this sum is for
289
+ }
290
+ total = total + toI32(x);
285
291
  }
286
292
  const average = total / 2;
287
293
  console.log(`${average}`);
@@ -349,7 +355,10 @@ where it can prove the result fits — a loop counter under `i < n`, a byte
349
355
  widened with `toI32`, an `integer<Lo, Hi>`, a value masked with `& c`. Arithmetic
350
356
  that is *meant* to wrap — a hash, a PRNG, a checksum — goes in `u32` or `u64`,
351
357
  which always wrap and are never checked; convert back with `toI32` (free), or
352
- call `wrappingAdd` / `wrappingSub` / `wrappingMul` at the site.
358
+ call `wrappingAdd` / `wrappingSub` / `wrappingMul` at the site. Never cast an
359
+ `i32` through `u32` and back just to make it wrap (`toI32(toU32(a) + toU32(b))`):
360
+ that hides an overflow the compiler would have caught, where a `wrapping*` call
361
+ says the wrap is meant.
353
362
 
354
363
  <!-- nish:ok -->
355
364
  ```ts
@@ -365,16 +374,15 @@ export const fnv1a = (bytes: u8[]): u32 => {
365
374
 
366
375
  <!-- nish:err NL2231 -->
367
376
  ```ts
368
- const bad = (a: i32, b: i64): i64 => {
377
+ const bad = (a: f32, b: f64): f64 => {
369
378
  return a + b;
370
379
  };
371
380
  ```
372
381
 
373
- <!-- allow-panics: does integer arithmetic nothing bounds -->
374
- <!-- nish:ok --allow-panics -->
382
+ <!-- nish:ok -->
375
383
  ```ts
376
- const good = (a: i32, b: i64): i64 => {
377
- return toI64(a) + b;
384
+ const good = (a: f32, b: f64): f64 => {
385
+ return toF64(a) + b;
378
386
  };
379
387
  ```
380
388
 
@@ -394,15 +402,17 @@ not an `integer<0, 255>` (convert with `toI32`), and a `declare function` may
394
402
  not mention a range. Put the range on the value you use, not on a loop
395
403
  counter, which has to leave the range to end the loop.
396
404
 
397
- <!-- allow-panics: indexes an array without a guard the checker reads; does integer arithmetic nothing bounds -->
398
- <!-- nish:ok --allow-panics -->
405
+ <!-- nish:ok -->
399
406
  ```ts
400
- const getByte = (buf: u8[], i: integer<0, 255>): u8 => buf[i];
407
+ const getByte = (buf: u8[], i: integer<0, 255>): u8 => (i < buf.length ? buf[i] : 0);
401
408
 
402
409
  const sumAll = (buf: u8[]): i32 => {
403
410
  let sum = 0;
404
411
  for (let i = 0; i < 256 && i < buf.length; i++) {
405
412
  const b: integer<0, 255> = i;
413
+ if (sum > 1000000) {
414
+ return -1;
415
+ }
406
416
  sum = sum + toI32(getByte(buf, b));
407
417
  }
408
418
  return sum;
@@ -547,8 +557,7 @@ reference — never a scalar, which has no null value. It is the same pointer
547
557
  (or, for a function reference, the same tag) with `null` as one more value, so
548
558
  nothing is boxed.
549
559
 
550
- <!-- allow-panics: does integer arithmetic nothing bounds -->
551
- <!-- nish:ok --allow-panics -->
560
+ <!-- nish:ok -->
552
561
  ```ts
553
562
  class Node {
554
563
  value: i32;
@@ -558,11 +567,16 @@ class Node {
558
567
  }
559
568
  }
560
569
 
570
+ // Values run 0 to 1,000,000, so a total at most 1,000,000,000 has room for one.
561
571
  const total = (head: Node | null): i32 => {
562
572
  let sum = 0;
563
573
  let cur = head;
564
574
  while (cur !== null) {
565
- sum = sum + cur.value;
575
+ const v = cur.value;
576
+ if (v < 0 || v > 1000000 || sum > 1000000000) {
577
+ return -1;
578
+ }
579
+ sum = sum + v;
566
580
  cur = cur.next;
567
581
  }
568
582
  return sum;
@@ -658,14 +672,13 @@ const n = s?.length;
658
672
 
659
673
  ### Functions
660
674
 
661
- <!-- allow-panics: does integer arithmetic nothing bounds -->
662
- <!-- nish:ok --allow-panics -->
675
+ <!-- nish:ok -->
663
676
  ```ts
664
- const add = (a: i32, b: i32): i32 => {
665
- return a + b;
677
+ const add = (a: i32, b: i32): i64 => {
678
+ return toI64(a) + toI64(b); // two `i32`s always fit an `i64`
666
679
  };
667
680
 
668
- const double = (n: i32): i32 => n * 2; // a concise body is that one `return`
681
+ const double = (n: i32): i64 => toI64(n) * 2; // a concise body is that one `return`
669
682
  ```
670
683
 
671
684
  - A function is a **module-level `const` bound to an arrow**. `let` is
@@ -675,7 +688,7 @@ const double = (n: i32): i32 => n * 2; // a concise body is that one `return`
675
688
  - **Every parameter and the return type must be annotated.**
676
689
  - **A capture-free top-level function is a value** — a
677
690
  [function reference](#function-references): `const h: Op = double`, a field
678
- `op: Op`, an `Op[]`, a returned `Op`, where `type Op = (n: i32) => i32`. An
691
+ `op: Op`, an `Op[]`, a returned `Op`, where `type Op = (n: i32) => i64`. An
679
692
  arrow written inside a function is not one: declare it at the top level and
680
693
  name it.
681
694
  - **Parameters are immutable**: `p = 1`, `p++`, `p += 1` are all rejected. Copy
@@ -761,20 +774,19 @@ export const main = (): i32 => {
761
774
  same fields, so implementing your own is refused the same way; import the
762
775
  template's `Shape` instead.
763
776
 
764
- <!-- allow-panics: does integer arithmetic nothing bounds -->
765
- <!-- nish:ok --allow-panics -->
777
+ <!-- nish:ok -->
766
778
  ```ts
767
- interface Shape { area: i32; }
779
+ interface Shape { area: f64; }
768
780
 
769
781
  class Circle implements Shape {
770
- area: i32;
771
- radius: i32;
772
- constructor(r: i32) { this.area = 3 * r * r; this.radius = r; }
782
+ area: f64;
783
+ radius: f64;
784
+ constructor(r: f64) { this.area = 3 * r * r; this.radius = r; }
773
785
  }
774
786
 
775
- const areaOf = <T extends Shape>(s: T): i32 => s.area; // s.radius would be refused
787
+ const areaOf = <T extends Shape>(s: T): f64 => s.area; // s.radius would be refused
776
788
 
777
- export const main = (): i32 => areaOf(new Circle(2)) - 12;
789
+ export const main = (): i32 => (areaOf(new Circle(2)) === 12 ? 0 : 1); // 0
778
790
  ```
779
791
 
780
792
  - **Each type parameter is named once**: `<T, T>` is
@@ -879,8 +891,7 @@ They are inferred from the arguments like a generic function's, and each
879
891
  (receiver, method type arguments) pair is its own `define`:
880
892
  `@Chooser.pick$i32`, `@Box$i32.keep$str`.
881
893
 
882
- <!-- allow-panics: does integer arithmetic nothing bounds -->
883
- <!-- nish:ok --allow-panics -->
894
+ <!-- nish:ok -->
884
895
  ```ts
885
896
  class Chooser {
886
897
  flip: boolean = false;
@@ -897,7 +908,8 @@ export const main = (): i32 => {
897
908
  const c = new Chooser();
898
909
  const word: string = c.pick("left", "right");
899
910
  console.log(word);
900
- return c.pick(0, 1) + new Box<i32>(7).keep("seven") - 7;
911
+ console.log(`${c.pick(0, 1)} ${new Box<i32>(7).keep("seven")}`); // 0 7
912
+ return 0;
901
913
  };
902
914
  ```
903
915
 
@@ -930,8 +942,7 @@ is a top-level function named at the call, or an arrow written right there, and
930
942
  each one is compiled into its **own copy** of the function, with a direct call
931
943
  where the parameter is called — there is no function pointer at run time.
932
944
 
933
- <!-- allow-panics: does integer arithmetic nothing bounds -->
934
- <!-- nish:ok --allow-panics -->
945
+ <!-- nish:ok -->
935
946
  ```ts
936
947
  const map = <T, U>(xs: T[], f: (x: T) => U): U[] => {
937
948
  const out: U[] = [];
@@ -949,11 +960,12 @@ const fold = <T>(xs: T[], f: (acc: T, x: T) => T, identity: T): T => {
949
960
  return acc;
950
961
  };
951
962
 
952
- const add = (a: i32, b: i32): i32 => a + b;
963
+ const add = (a: f64, b: f64): f64 => a + b; // an `f64` sum cannot overflow
953
964
 
954
965
  export const main = (): i32 => {
955
- const doubled = map([1, 2, 3], (n) => n * 2); // U is bound from the body
956
- return fold(doubled, add, 0) - 12; // 0
966
+ const xs: f64[] = [1, 2, 3];
967
+ const doubled = map(xs, (n) => n * 2); // U is bound from the body
968
+ return fold(doubled, add, 0) === 12 ? 0 : 1; // 0
957
969
  };
958
970
  ```
959
971
 
@@ -1112,7 +1124,7 @@ else to switch on and no thread count to choose. `parallelMap(src, f)` and
1112
1124
  `parallelFrom(n, f)` answer a new array instead (`f(src[i])`, `f(i)`), under
1113
1125
  the same rules for `f`.
1114
1126
 
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 -->
1127
+ <!-- allow-panics: calls `std/threads.ts` code that keeps panic sites no proof removes yet -->
1116
1128
  <!-- nish:ok --allow-panics -->
1117
1129
  ```ts
1118
1130
  import { parallelMapInto, parallelReduce } from "nish/threads";
@@ -1121,14 +1133,14 @@ const square = (x: f64): f64 => x * x;
1121
1133
 
1122
1134
  export const main = (): i32 => {
1123
1135
  const src: f64[] = [1.0, 2.0, 3.0];
1124
- const dst: f64[] = [0.0, 0.0, 0.0]; // at least src.length, or it panics
1136
+ const dst = new Array<f64>(src.length); // one slot per element, made once
1125
1137
  parallelMapInto(src, dst, square);
1126
1138
  const total = parallelReduce(dst, (a, b) => a + b, 0.0); // 14, the same bits on any core count
1127
1139
  return toI32(total);
1128
1140
  };
1129
1141
  ```
1130
1142
 
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 -->
1143
+ <!-- allow-panics: calls `std/threads.ts` code that keeps panic sites no proof removes yet -->
1132
1144
  <!-- nish:ok --allow-panics -->
1133
1145
  ```ts
1134
1146
  import { parallelFrom, parallelMap } from "nish/threads";
@@ -1136,7 +1148,9 @@ import { parallelFrom, parallelMap } from "nish/threads";
1136
1148
  export const main = (): i32 => {
1137
1149
  const names = parallelFrom(3, (i) => `w${i}`); // each string copied out at the join
1138
1150
  const lens = parallelMap(names, (n) => n.length); // 2 2 2
1139
- console.log(`${names[2]} ${lens[0]}`); // w2 2
1151
+ if (names.length > 2 && lens.length > 0) {
1152
+ console.log(`${names[2]} ${lens[0]}`); // w2 2
1153
+ }
1140
1154
  return 0;
1141
1155
  };
1142
1156
  ```
@@ -1188,24 +1202,35 @@ Different functions at once, one thread each: open a scope with `using`, give
1188
1202
  it tasks with `spawn(entry, arg, dst, at)`, and every task has run and stored
1189
1203
  `entry(arg)` into `dst[at]` when the block ends.
1190
1204
 
1191
- <!-- allow-panics: indexes an array without a guard the checker reads; does integer arithmetic nothing bounds -->
1192
- <!-- nish:ok --allow-panics -->
1205
+ <!-- nish:ok -->
1193
1206
  ```ts
1194
1207
  import { scope } from "nish/threads";
1195
1208
 
1196
- const sumOf = (xs: f64[]): f64 => xs[0] + xs[1];
1197
- const square = (n: i32): i32 => n * n;
1209
+ const sumOf = (xs: f64[]): f64 => {
1210
+ let total: f64 = 0;
1211
+ for (const x of xs) {
1212
+ total += x;
1213
+ }
1214
+ return total;
1215
+ };
1216
+ const square = (n: i32): i64 => toI64(n) * toI64(n); // an `i32` squared fits an `i64`
1198
1217
 
1199
1218
  export const main = (): i32 => {
1200
1219
  const xs: f64[] = [1.5, 2.5];
1201
- const sums: f64[] = [0.0];
1202
- const squares: i32[] = [0];
1220
+ const sums = new Array<f64>(1);
1221
+ const squares = new Array<i64>(1);
1203
1222
  {
1204
1223
  using s = scope();
1205
- s.spawn(sumOf, xs, sums, 0);
1206
- s.spawn(square, 12, squares, 0);
1224
+ if (sums.length > 0) { // each slot is tested where it is spawned
1225
+ s.spawn(sumOf, xs, sums, 0);
1226
+ }
1227
+ if (squares.length > 0) {
1228
+ s.spawn(square, 12, squares, 0);
1229
+ }
1207
1230
  } // joined here, on every exit
1208
- console.log(`${sums[0]} ${squares[0]}`); // 4 144
1231
+ if (sums.length > 0 && squares.length > 0) {
1232
+ console.log(`${sums[0]} ${squares[0]}`); // 4 144
1233
+ }
1209
1234
  return 0;
1210
1235
  };
1211
1236
  ```
@@ -1260,8 +1285,7 @@ export const main = (): i32 => {
1260
1285
  When tasks must update one value, put it in a `Mutex` and reach it only through
1261
1286
  `using g = m.lock()`: the lock is held until that block ends, on every exit.
1262
1287
 
1263
- <!-- allow-panics: does integer arithmetic nothing bounds; indexes an array without a guard the checker reads -->
1264
- <!-- nish:ok --allow-panics -->
1288
+ <!-- nish:ok -->
1265
1289
  ```ts
1266
1290
  import { Mutex, scope } from "nish/threads";
1267
1291
 
@@ -1281,25 +1305,30 @@ class Shard {
1281
1305
  }
1282
1306
  }
1283
1307
 
1284
- const binAll = (s: Shard): i32 => {
1308
+ const binAll = (s: Shard): void => {
1285
1309
  for (const x of s.xs) {
1286
1310
  const bin: i32 = x & 3;
1287
1311
  using g = s.tally.lock(); // held to the end of this pass
1288
- g.value.bins[bin] = g.value.bins[bin] + 1;
1312
+ if (bin < g.value.bins.length) {
1313
+ const n = g.value.bins[bin];
1314
+ if (n < 2147483647) { // the bound the `+ 1` needs
1315
+ g.value.bins[bin] = n + 1;
1316
+ }
1317
+ }
1289
1318
  }
1290
- return 0;
1291
1319
  };
1292
1320
 
1293
1321
  export const main = (): i32 => {
1294
1322
  const tally = new Mutex<Tally>(new Tally()); // a fresh value: `new` or a literal
1295
- const done: i32[] = [0, 0];
1296
1323
  {
1297
1324
  using s = scope();
1298
- s.spawn(binAll, new Shard([1, 2, 3], tally), done, 0);
1299
- s.spawn(binAll, new Shard([4, 5, 6], tally), done, 1);
1325
+ s.spawn(binAll, new Shard([1, 2, 3], tally));
1326
+ s.spawn(binAll, new Shard([4, 5, 6], tally));
1300
1327
  }
1301
1328
  using g = tally.lock(); // after the join: lock freely
1302
- console.log(`${g.value.bins[1]}`); // 2
1329
+ if (g.value.bins.length > 1) {
1330
+ console.log(`${g.value.bins[1]}`); // 2
1331
+ }
1303
1332
  return 0;
1304
1333
  };
1305
1334
  ```
@@ -1352,8 +1381,7 @@ export const main = (): i32 => {
1352
1381
  };
1353
1382
  ```
1354
1383
 
1355
- <!-- allow-panics: does integer arithmetic nothing bounds -->
1356
- <!-- nish:ok --allow-panics -->
1384
+ <!-- nish:ok -->
1357
1385
  ```ts
1358
1386
  import { Mutex, MutexGuard } from "nish/threads";
1359
1387
 
@@ -1362,14 +1390,17 @@ class Stats {
1362
1390
  }
1363
1391
 
1364
1392
  const bump = (g: MutexGuard<Stats>): void => {
1365
- g.value.count = g.value.count + 1; // a helper: the block's rules, the block's lock
1393
+ const n = g.value.count;
1394
+ if (n < 2147483647) { // the bound the `+ 1` needs
1395
+ g.value.count = n + 1; // a helper: the block's rules, the block's lock
1396
+ }
1366
1397
  };
1367
1398
 
1368
1399
  export const main = (): i32 => {
1369
1400
  const m = new Mutex<Stats>(new Stats());
1370
1401
  using g = m.lock();
1371
1402
  bump(g);
1372
- return g.value.count - 1; // 0
1403
+ return g.value.count === 1 ? 0 : 1; // 0
1373
1404
  };
1374
1405
  ```
1375
1406
 
@@ -1379,8 +1410,7 @@ A shared counter, minimum, maximum or flag is an atomic: `new AtomicI32(0)`,
1379
1410
  `new AtomicI64(toI64(0))`, updated from any task or parallel `f` by one
1380
1411
  indivisible method call.
1381
1412
 
1382
- <!-- allow-panics: calls `parallelMapInto`, whose length check no proof removes yet; does integer arithmetic nothing bounds -->
1383
- <!-- nish:ok --allow-panics -->
1413
+ <!-- nish:ok -->
1384
1414
  ```ts
1385
1415
  import { AtomicI32, parallelMapInto } from "nish/threads";
1386
1416
 
@@ -1388,8 +1418,8 @@ export const main = (): i32 => {
1388
1418
  const hits = new AtomicI32(0);
1389
1419
  const top = new AtomicI32(0);
1390
1420
  const xs: i32[] = [3, 9, 4];
1391
- const ys: i32[] = [0, 0, 0];
1392
- parallelMapInto(xs, ys, (x) => hits.add(1) + top.max(x)); // each answers the old value
1421
+ const ys = new Array<i64>(xs.length); // two `i32`s always fit an `i64`
1422
+ parallelMapInto(xs, ys, (x) => toI64(hits.add(1)) + toI64(top.max(x))); // each answers the old value
1393
1423
  console.log(`${hits.load()} ${top.load()}`); // 3 9
1394
1424
  return 0;
1395
1425
  };
@@ -1440,8 +1470,7 @@ When one task produces values and another consumes them, give both a
1440
1470
  `for...of`, which waits for values and ends once every producer has returned.
1441
1471
  Nobody closes a channel.
1442
1472
 
1443
- <!-- allow-panics: does integer arithmetic nothing bounds; indexes an array without a guard the checker reads -->
1444
- <!-- nish:ok --allow-panics -->
1473
+ <!-- nish:ok -->
1445
1474
  ```ts
1446
1475
  import { Channel, scope } from "nish/threads";
1447
1476
 
@@ -1454,32 +1483,37 @@ class Pipe {
1454
1483
  }
1455
1484
  }
1456
1485
 
1457
- const produce = (p: Pipe): i32 => {
1458
- for (let i: i32 = 1; i <= p.n; i++) {
1459
- p.ch.send(i); // never waits
1486
+ const produce = (p: Pipe): void => {
1487
+ for (let i: i32 = 0; i < p.n; i++) {
1488
+ p.ch.send(i + 1); // 1 to n; never waits
1460
1489
  }
1461
- return p.n;
1462
1490
  };
1463
1491
 
1464
- const consume = (p: Pipe): i32 => {
1492
+ const consume = (ch: Channel<i32>): i32 => {
1465
1493
  let sum: i32 = 0;
1466
- for (const x of p.ch) { // ends once every sender has returned
1494
+ for (const x of ch) { // ends once every sender has returned
1495
+ if (x < 0 || x > 1000000 || sum > 1000000000) {
1496
+ return -1; // out of the range this sum is for
1497
+ }
1467
1498
  sum = sum + x;
1468
1499
  }
1469
1500
  return sum;
1470
1501
  };
1471
1502
 
1472
1503
  export const main = (): i32 => {
1473
- const sent: i32[] = [0, 0];
1474
- const sums: i32[] = [0];
1504
+ const sums = new Array<i32>(1);
1475
1505
  const ch = new Channel<i32>(); // a `const`, right before its scope
1476
1506
  {
1477
1507
  using s = scope();
1478
- s.spawn(produce, new Pipe(ch, 10), sent, 0); // every sender first
1479
- s.spawn(produce, new Pipe(ch, 20), sent, 1);
1480
- s.spawn(consume, new Pipe(ch, 0), sums, 0); // then the one receiver
1508
+ s.spawn(produce, new Pipe(ch, 10)); // every sender first
1509
+ s.spawn(produce, new Pipe(ch, 20));
1510
+ if (sums.length > 0) {
1511
+ s.spawn(consume, ch, sums, 0); // then the one receiver
1512
+ }
1513
+ }
1514
+ if (sums.length > 0) {
1515
+ console.log(`${sums[0]}`); // 265
1481
1516
  }
1482
- console.log(`${sums[0]}`); // 265
1483
1517
  return 0;
1484
1518
  };
1485
1519
  ```
@@ -1617,6 +1651,12 @@ export const main = (): i32 => {
1617
1651
  may not keep the address. It stays in its module: no exported signature
1618
1652
  holds one, however deep (NL2746, NL2756), and a page module (freestanding
1619
1653
  wasm, `--emit-dts`) refuses one (NL2757).
1654
+ - **One module may export its bindings.** `export declare function` and an
1655
+ opaque `export declare class Handle {}` are imported by name like any
1656
+ export; each module that calls one is charged `ffi`. A `declare class` with
1657
+ fields is never exported (NL2746), nor is a function whose signature holds
1658
+ one (NL2756), and a binding is not re-exported. `nish c-import` writes
1659
+ `export` on every binding that may cross.
1620
1660
 
1621
1661
  <!-- nish:ok -->
1622
1662
  ```ts
@@ -1694,17 +1734,18 @@ export const main = (): i32 => {
1694
1734
  - **A `string` result is copied out of C's `const char *`** into the arena at
1695
1735
  the call, so it lives like any other string and C may reuse its buffer.
1696
1736
  `string | null` maps `NULL` to `null`; a `string` result that comes back
1697
- `NULL` panics (`ffi-null`). The copy never frees: a result the caller must
1698
- `free` (`strdup`) is declared `CPtr | null` instead.
1737
+ `NULL` panics (`ffi-null`), so the no-panic default refuses a `string`
1738
+ result: declare it `string | null` and default it with `??`. The copy never
1739
+ frees: a result the caller must `free` (`strdup`) is declared `CPtr | null`
1740
+ instead.
1699
1741
 
1700
- <!-- allow-panics: reads a C pointer that may be `NULL` -->
1701
- <!-- nish:ok --allow-panics -->
1742
+ <!-- nish:ok -->
1702
1743
  ```ts
1703
- declare function strerror(errnum: i32): string;
1744
+ declare function strerror(errnum: i32): string | null;
1704
1745
  declare function getenv(name: string): string | null;
1705
1746
 
1706
1747
  export const main = (): i32 => {
1707
- console.log(strerror(2));
1748
+ console.log(strerror(2) ?? "unknown error");
1708
1749
  const home = getenv("HOME");
1709
1750
  console.log(home === null ? "unset" : "set");
1710
1751
  return 0;
@@ -1733,7 +1774,7 @@ export const main = (): i32 => {
1733
1774
  `realpath(path, null)` passes `NULL`; a nullable pointer held in a variable
1734
1775
  is NL2539, so C is never handed a `NULL` by accident.
1735
1776
 
1736
- <!-- allow-panics: reads a C pointer that may be `NULL` -->
1777
+ <!-- allow-panics: calls `readCString`, whose `NULL` check not even an earlier `p === null` return removes yet -->
1737
1778
  <!-- nish:ok --allow-panics -->
1738
1779
  ```ts
1739
1780
  import { readCString } from "nish:unsafe";
@@ -1764,14 +1805,24 @@ export const main = (): i32 => {
1764
1805
  - **A class with a `CPtr` field is a handle class: open one with `using`.**
1765
1806
  `using f = new File(p)` closes `f` once at every exit of its block (falling
1766
1807
  off the end, `return`, `break`, `continue`, `orReturn()`), so never call the
1767
- close yourself. The class is checked: one `readonly` `CPtr` or
1768
- `CPtr | null` field written once by the constructor's foreign call, read only
1769
- as `this.f` in its own methods, and a `[Symbol.dispose]` that is exactly the
1770
- close. A handle is never copied, stored, returned or captured: it is called
1771
- or lent to a parameter of its type, and `new` of one appears only as a
1772
- `using` initialiser. A panic or `process.exit` does not close it, so
1808
+ close yourself. The class is checked: each owned field is `readonly`, one
1809
+ of `CPtr`, `CPtr | null`, an opaque `declare class`, a `HostHandle<"k">` or
1810
+ another handle class, written once at the top of the constructor (a native
1811
+ field from a foreign call, a handle field from a parameter of its type or a
1812
+ `new` right there), read only as `this.f` in its own methods; the
1813
+ `[Symbol.dispose]` closes the native fields; every owned field, child
1814
+ handles included, closes in reverse declaration order. A handle is never
1815
+ copied, stored in a local, or captured: it is called, lent to a parameter
1816
+ of its type (an exported function's too, across modules, but never one the
1817
+ entry module exports), moved into an owning field, or
1818
+ returned by a top-level factory as `H`, `H | null` or `Result<H, E>`
1819
+ (`using g = openGpu().expect("gpu")`). `new` of one appears only as a
1820
+ `using` initialiser, a factory's result or an owning argument. A panic or `process.exit` does not close it, so
1773
1821
  `fflush` a written `FILE *` before code that may panic. The rules:
1774
- `LANGUAGE.md` → "A handle class".
1822
+ `LANGUAGE.md` → "A handle class". A resource with one owner is shaped
1823
+ this way (a `HostHandle` field on `wasm32`); keep an id into a table, as
1824
+ `nish/sqlite` does, only for an object that is shared, kept in a
1825
+ collection or owned by the JS side.
1775
1826
  - **It costs the caller its attributes.** Nothing about a body the compiler
1776
1827
  cannot see is provable, so a function that calls C loses `readnone` and
1777
1828
  `willreturn`, and so does everything above it in the call graph. A program
@@ -1803,15 +1854,17 @@ emitted and no initialiser runs** — every use lowers to the value itself.
1803
1854
 
1804
1855
  ### Module storage
1805
1856
 
1806
- <!-- allow-panics: does integer arithmetic nothing bounds; indexes an array without a guard the checker reads -->
1807
- <!-- nish:ok --allow-panics -->
1857
+ <!-- nish:ok -->
1808
1858
  ```ts
1809
1859
  const SCORES = new Int32Array(4);
1810
1860
 
1811
1861
  const sum = (xs: Int32Array): i32 => {
1812
1862
  let total: i32 = 0;
1813
- for (let i = 0; i < xs.length; i++) {
1814
- total += xs[i];
1863
+ for (const x of xs) {
1864
+ if (x < 0 || x > 1000000 || total > 1000000000) {
1865
+ return -1; // outside the range of a score
1866
+ }
1867
+ total += x;
1815
1868
  }
1816
1869
  return total;
1817
1870
  };
@@ -1844,8 +1897,7 @@ state a module keeps (there is no top-level `let`).
1844
1897
 
1845
1898
  ### Classes and interfaces
1846
1899
 
1847
- <!-- allow-panics: does integer arithmetic nothing bounds -->
1848
- <!-- nish:ok --allow-panics -->
1900
+ <!-- nish:ok -->
1849
1901
  ```ts
1850
1902
  interface Shape { width: i32; height: i32; }
1851
1903
 
@@ -1859,12 +1911,12 @@ class Rect implements Shape {
1859
1911
  this.height = height;
1860
1912
  }
1861
1913
 
1862
- area(): i32 {
1863
- return this.width * this.height;
1914
+ area(): i64 {
1915
+ return toI64(this.width) * toI64(this.height); // two `i32`s multiply into an `i64` without overflow
1864
1916
  }
1865
1917
  }
1866
1918
 
1867
- const describe = (s: Shape): i32 => s.width + s.height;
1919
+ const describe = (s: Shape): i64 => toI64(s.width) + toI64(s.height);
1868
1920
 
1869
1921
  export const main = (): i32 => {
1870
1922
  const r = new Rect(3, 4);
@@ -1916,16 +1968,17 @@ export const main = (): i32 => {
1916
1968
 
1917
1969
  Index again after the `push` instead of holding the element across it:
1918
1970
 
1919
- <!-- allow-panics: indexes an array without a guard the checker reads -->
1920
- <!-- nish:ok --allow-panics -->
1971
+ <!-- nish:ok -->
1921
1972
  ```ts
1922
1973
  interface Point { x: i32; }
1923
1974
 
1924
1975
  export const main = (): i32 => {
1925
1976
  const ps: Point[] = [{ x: 1 }];
1926
1977
  ps.push({ x: 2 });
1927
- ps[0].x = 3;
1928
- console.log(`${ps[0].x}`);
1978
+ if (ps.length > 0) {
1979
+ ps[0].x = 3;
1980
+ console.log(`${ps[0].x}`);
1981
+ }
1929
1982
  return 0;
1930
1983
  };
1931
1984
  ```
@@ -1938,8 +1991,7 @@ grows, and the blocks it outgrows stay in the arena; a walk over `kids`
1938
1991
  follows two pointers per child where the links follow one. On a 4M-node
1939
1992
  tree the links took 0.52x the memory and 0.68x the time of `kids`.
1940
1993
 
1941
- <!-- allow-panics: does integer arithmetic nothing bounds -->
1942
- <!-- nish:ok --allow-panics -->
1994
+ <!-- nish:ok -->
1943
1995
  ```ts
1944
1996
  class Node {
1945
1997
  kind: i32;
@@ -1964,11 +2016,16 @@ const add = (parent: Node, last: Node | null, kind: i32): Node => {
1964
2016
  return kid;
1965
2017
  };
1966
2018
 
2019
+ // -1 marks a tree whose total leaves the range it is for.
1967
2020
  const sum = (n: Node): i32 => {
1968
2021
  let s = n.kind;
1969
2022
  let kid = n.first;
1970
2023
  while (kid !== null) {
1971
- s = s + sum(kid);
2024
+ const k = sum(kid);
2025
+ if (s < 0 || s > 1000000000 || k < 0 || k > 1000000000) {
2026
+ return -1;
2027
+ }
2028
+ s = s + k;
1972
2029
  kid = kid.next;
1973
2030
  }
1974
2031
  return s;
@@ -1995,8 +2052,7 @@ export const main = (): i32 => {
1995
2052
  - `uncheckedGet` / `uncheckedSet` belong in that table's code only: an index
1996
2053
  out of range there is undefined behaviour, not a panic.
1997
2054
 
1998
- <!-- allow-panics: builds an array whose length it does not bound; does integer arithmetic nothing bounds -->
1999
- <!-- nish:ok --allow-panics -->
2055
+ <!-- nish:ok -->
2000
2056
  ```ts
2001
2057
  import { uncheckedGet, uncheckedSet } from "nish:unsafe";
2002
2058
 
@@ -2006,16 +2062,16 @@ class Tokens {
2006
2062
  start: i32[];
2007
2063
  count: i32 = 0;
2008
2064
 
2009
- constructor(capacity: i32) {
2065
+ constructor(capacity: integer<0, 1048576>) {
2010
2066
  this.kind = new Array<i32>(capacity);
2011
2067
  this.start = new Array<i32>(capacity);
2012
2068
  }
2013
2069
 
2014
2070
  add(kind: i32, start: i32): i32 {
2015
- if (this.count >= this.kind.length) {
2071
+ const id = this.count;
2072
+ if (id >= this.kind.length) {
2016
2073
  return -1;
2017
2074
  }
2018
- const id = this.count;
2019
2075
  uncheckedSet(this.kind, id, kind);
2020
2076
  uncheckedSet(this.start, id, start);
2021
2077
  this.count = id + 1;
@@ -2029,9 +2085,14 @@ export const main = (): i32 => {
2029
2085
  t.add(9, 4);
2030
2086
  let s = 0;
2031
2087
  for (let i = 0; i < t.count; i++) {
2032
- s = s + uncheckedGet(t.kind, i) * uncheckedGet(t.start, i);
2088
+ const kind = uncheckedGet(t.kind, i);
2089
+ const start = uncheckedGet(t.start, i);
2090
+ if (kind < 0 || kind > 1000 || start < 0 || start > 1000000 || s > 1000000000) {
2091
+ return 1; // a kind or an offset out of its range
2092
+ }
2093
+ s = s + kind * start;
2033
2094
  }
2034
- console.log(`${s}`);
2095
+ console.log(`${s}`); // 36
2035
2096
  return 0;
2036
2097
  };
2037
2098
  ```
@@ -2224,8 +2285,7 @@ export const main = (): i32 => (pick(Kind.Array) === Kind.Array ? 0 : 1);
2224
2285
 
2225
2286
  ### Discriminated unions
2226
2287
 
2227
- <!-- allow-panics: does integer arithmetic nothing bounds -->
2228
- <!-- nish:ok --allow-panics -->
2288
+ <!-- nish:ok -->
2229
2289
  ```ts
2230
2290
  enum Kind {
2231
2291
  Circle,
@@ -2234,12 +2294,12 @@ enum Kind {
2234
2294
 
2235
2295
  class Circle {
2236
2296
  readonly kind: Kind.Circle = Kind.Circle;
2237
- r: i32 = 3;
2297
+ r: f64 = 3;
2238
2298
  }
2239
2299
 
2240
2300
  class Square {
2241
2301
  readonly kind: Kind.Square = Kind.Square;
2242
- side: i32 = 4;
2302
+ side: f64 = 4;
2243
2303
  }
2244
2304
 
2245
2305
  type Shape = Circle | Square;
@@ -2249,10 +2309,12 @@ const pick = (round: boolean): Shape => (round ? new Circle() : new Square());
2249
2309
  export const main = (): i32 => {
2250
2310
  const shapes: Shape[] = [pick(true), new Square()];
2251
2311
  let circles = 0;
2252
- let area = 0;
2312
+ let area: f64 = 0;
2253
2313
  for (const s of shapes) {
2254
2314
  if (s.kind === Kind.Circle) {
2255
- circles = circles + 1; // `s` is a `Circle` here
2315
+ if (circles < shapes.length) { // a count of shapes never passes their number
2316
+ circles = circles + 1; // `s` is a `Circle` here
2317
+ }
2256
2318
  area = area + 3 * s.r * s.r;
2257
2319
  } else {
2258
2320
  area = area + s.side * s.side; // and a `Square` here
@@ -2474,8 +2536,7 @@ export const main = (): i32 => {
2474
2536
  `break`, `continue`, `process.exit`, an `if` whose branches all return, or an
2475
2537
  infinite loop.
2476
2538
 
2477
- <!-- allow-panics: does integer arithmetic nothing bounds -->
2478
- <!-- nish:ok --allow-panics -->
2539
+ <!-- nish:ok -->
2479
2540
  ```ts
2480
2541
  const firstPair = (limit: i32, target: i32): i32 => {
2481
2542
  outer: for (let i: i32 = 0; i < limit; i++) {
@@ -2495,9 +2556,11 @@ const classify = (n: i32): i32 => {
2495
2556
  let score: i32 = 0;
2496
2557
  switch (n) {
2497
2558
  case 0:
2498
- score += 10; // no `break`: falls into `case 1`
2559
+ score = 10; // no `break`: falls into `case 1`
2499
2560
  case 1:
2500
- score += 1;
2561
+ if (score < 100) {
2562
+ score += 1;
2563
+ }
2501
2564
  break;
2502
2565
  default:
2503
2566
  score = -1;
@@ -2779,7 +2842,10 @@ link answers for itself), `geteuid()` (`i64`) and `isExecutableSync(path)`
2779
2842
  **The host.** `Date.now()` (an `f64` of whole milliseconds, the wall clock —
2780
2843
  **the only `Date` there is**: `new Date()`, `Date.parse` and the rest are
2781
2844
  refused), `crypto.getRandomValues(bytes)` (a statement over a `u8[]` only, at
2782
- most 65,536 bytes a call, from the kernel's CSPRNG; more panics),
2845
+ most 65,536 bytes a call, from the kernel's CSPRNG; more panics, and so does a
2846
+ failing source), its twin `crypto.tryGetRandomValues(bytes)` (a `boolean`:
2847
+ `false` for more than 65,536 bytes or a failing source, never an exit, so it
2848
+ is the one a program built without `--allow-panics` calls),
2783
2849
  `statMtimeSync(path)` (an `f64` of milliseconds, **NaN** — not `null` — when the
2784
2850
  path cannot be stat'd, so test `m !== m`), and `signalFd()` / `readSignal(fd)`:
2785
2851
  call `signalFd()` once at the top of `main`, then `readSignal(fd)` blocks until
@@ -2941,12 +3007,14 @@ console.log(`${seen.size} ${counts.size} ${seen.has("b")}`);
2941
3007
  three ways: a default with `??`, a `const` tested with `!== undefined` (or
2942
3008
  `=== undefined` and an early `return`), or a bare test. Each is one probe.
2943
3009
 
2944
- <!-- allow-panics: does integer arithmetic nothing bounds -->
2945
- <!-- nish:ok-body --allow-panics -->
3010
+ <!-- nish:ok-body -->
2946
3011
  ```ts
2947
3012
  const counts = new Map<string, i32>();
2948
3013
  for (const w of ["a", "b", "a"]) {
2949
- counts.set(w, (counts.get(w) ?? 0) + 1);
3014
+ const n = counts.get(w) ?? 0;
3015
+ if (n < 2147483647) { // the bound the `+ 1` needs
3016
+ counts.set(w, n + 1);
3017
+ }
2950
3018
  }
2951
3019
  const a = counts.get("a");
2952
3020
  if (a !== undefined) {
@@ -2975,8 +3043,7 @@ may `set`, `delete` or `clear` the table inside the walk and get exactly what
2975
3043
  JavaScript gives (a new key is visited later in the same walk; a key deleted
2976
3044
  before it is reached is not).
2977
3045
 
2978
- <!-- allow-panics: does integer arithmetic nothing bounds -->
2979
- <!-- nish:ok-body --allow-panics -->
3046
+ <!-- nish:ok-body -->
2980
3047
  ```ts
2981
3048
  const ages = new Map<string, i32>();
2982
3049
  ages.set("ada", 36).set("alan", 41);
@@ -2987,6 +3054,9 @@ for (const name of ages.keys()) {
2987
3054
  console.log(name); // ada, then grace
2988
3055
  }
2989
3056
  for (const age of ages.values()) {
3057
+ if (age < 0 || age > 150 || total > 1000000) {
3058
+ return 1; // not an age, or too many of them
3059
+ }
2990
3060
  total += age;
2991
3061
  }
2992
3062
  console.log(`${total}`); // 121
@@ -3016,7 +3086,7 @@ string `+`) or assign. Anything else still compiles, as one probe per call.
3016
3086
  inserting it — and `reserve(m, n)`, which sizes the table for `n` entries; both
3017
3087
  are one module with the program and run unchanged under Node.
3018
3088
 
3019
- <!-- allow-panics: does integer arithmetic nothing bounds -->
3089
+ <!-- allow-panics: counts in one probe, `counts.set(w, (counts.get(w) ?? 0) + 1)`, whose `+ 1` no guard the checker reads bounds yet -->
3020
3090
  <!-- nish:ok --allow-panics -->
3021
3091
  ```ts
3022
3092
  import { getOrInsert, reserve } from "nish/map";
@@ -3122,8 +3192,7 @@ export const main = (): i32 => {
3122
3192
  };
3123
3193
  ```
3124
3194
 
3125
- <!-- 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 -->
3126
- <!-- nish:ok --allow-panics -->
3195
+ <!-- nish:ok -->
3127
3196
  ```ts
3128
3197
  import { JsonWriter } from "nish/json";
3129
3198
  import { JsonDecoder, JsonNumber, jsonDecode, jsonEncode } from "nish/json-typed";
@@ -3202,15 +3271,18 @@ outlive it — so allocate what must outlive the block before it, and let only
3202
3271
  numbers, booleans and enums out. `arena()` is only ever a `using` initialiser,
3203
3272
  and `a` is never read.
3204
3273
 
3205
- <!-- allow-panics: does integer arithmetic nothing bounds -->
3206
- <!-- nish:ok-body --allow-panics -->
3274
+ <!-- nish:ok-body -->
3207
3275
  ```ts
3208
3276
  const rows: string[] = ["a", "bb"];
3209
3277
  let total = 0;
3210
3278
  for (const row of rows) {
3211
3279
  using a = arena();
3212
3280
  const line = `${row}: ${row.length}`;
3213
- total = total + line.length;
3281
+ const n = line.length;
3282
+ if (n > 1000000 || total > 1000000000) {
3283
+ return 1; // past what this count is for
3284
+ }
3285
+ total = total + n;
3214
3286
  }
3215
3287
  ```
3216
3288
 
@@ -3244,7 +3316,7 @@ entry — use a region.** `region(f)` / `regionWith(arg, f)` make one and run
3244
3316
  gives all of it back. `f` is a named function or an arrow that captures
3245
3317
  nothing. `Region<T>` is a number, not a pointer.
3246
3318
 
3247
- <!-- allow-panics: does integer arithmetic nothing bounds; calls `std/region.ts` code that keeps panic sites no proof removes yet -->
3319
+ <!-- allow-panics: calls `std/region.ts` code that keeps panic sites no proof removes yet -->
3248
3320
  <!-- nish:ok --allow-panics -->
3249
3321
  ```ts
3250
3322
  import { Region, enter, free, region } from "nish:region";
@@ -3255,7 +3327,10 @@ class Session {
3255
3327
 
3256
3328
  const open = (): Session => new Session();
3257
3329
  const touch = (s: Session): i32 => {
3258
- s.hits = s.hits + 1;
3330
+ const n = s.hits;
3331
+ if (n < 2147483647) { // the bound the `+ 1` needs
3332
+ s.hits = n + 1;
3333
+ }
3259
3334
  return s.hits;
3260
3335
  };
3261
3336
 
@@ -3281,12 +3356,11 @@ the module (NL4015).
3281
3356
 
3282
3357
  **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:
3283
3358
 
3284
- <!-- allow-panics: does integer arithmetic nothing bounds -->
3285
- <!-- nish:ok-body --allow-panics -->
3359
+ <!-- nish:ok-body -->
3286
3360
  ```ts
3287
- const xs: i32[] = [1, 2, 3];
3361
+ const xs: f64[] = [1, 2, 3];
3288
3362
  const doubled = xs.map((x) => x * 2);
3289
- let sum = 0;
3363
+ let sum: f64 = 0; // an `f64` sum cannot overflow
3290
3364
  for (const x of doubled) {
3291
3365
  sum += x;
3292
3366
  }
@@ -3499,8 +3573,7 @@ guide's [terminal page](guide/using-nish/terminal.md) its walk-through.)
3499
3573
  dispatch. Give each class the interface's fields **first**, `implements` it,
3500
3574
  and write a free function over the interface:
3501
3575
 
3502
- <!-- allow-panics: does integer arithmetic nothing bounds -->
3503
- <!-- nish:ok --allow-panics -->
3576
+ <!-- nish:ok -->
3504
3577
  ```ts
3505
3578
  interface Sized { width: i32; height: i32; }
3506
3579
 
@@ -3514,7 +3587,7 @@ class Photo implements Sized {
3514
3587
  }
3515
3588
  }
3516
3589
 
3517
- const area = (s: Sized): i32 => s.width * s.height;
3590
+ const area = (s: Sized): i64 => toI64(s.width) * toI64(s.height);
3518
3591
 
3519
3592
  export const main = (): i32 => {
3520
3593
  console.log(`${area(new Photo(3, 4))}`);
@@ -3616,7 +3689,12 @@ export const at = (xs: i32[], i: i32): i32 => xs[i];
3616
3689
 
3617
3690
  A call into the standard library or a dependency that may panic is refused at
3618
3691
  the call (NL2458), because only its own author can prove it: call a function
3619
- that cannot panic. The standard library is not yet panic-free, so `Map`,
3692
+ that cannot panic. The exception is a function whose first statements are
3693
+ `if (start < 0 || end > toI32(buf.length)) { panic(m) }`-style guards on its
3694
+ own parameters (a [`contract`](LANGUAGE.md#panic-sites) site): a call whose
3695
+ arguments the caller proves in range passes, as with
3696
+ `routerDecode(t, 0, toI32(t.length), out)`, when every call to it in the
3697
+ program is proven. The standard library is not yet panic-free, so `Map`,
3620
3698
  `Set`, `split` and the `JSON` reads are refused in the scope for now;
3621
3699
  a program that needs them builds with `--allow-panics`, and says why where it
3622
3700
  is built.
@@ -3653,10 +3731,9 @@ that keeps to it ([LANGUAGE.md](LANGUAGE.md#the-capability-policy)). Under an
3653
3731
  allowlist `exit` is a capability like the others, and `unsafe` can be denied
3654
3732
  but never allowed: importing `nish:unsafe` is the opt-in.
3655
3733
 
3656
- <!-- allow-panics: it shows NL2459, not a panic site; calls a file or entropy builtin that exits on failure -->
3657
- <!-- nish:err NL2459 --deny fs.read --allow-panics -->
3734
+ <!-- nish:err NL2459 --deny fs.read -->
3658
3735
  ```ts
3659
- const settings = (): string => readFileSync("settings.txt");
3736
+ const settings = (): string => readFileSyncOrNull("settings.txt") ?? "";
3660
3737
 
3661
3738
  export const main = (): i32 => settings().length;
3662
3739
  ```