@amritk/nish-aarch64-linux 0.49.0 → 0.50.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/INSTALL.md +1 -1
- package/bin/nish +0 -0
- package/docs/AI.md +233 -159
- package/package.json +1 -1
- package/runtime/gfx/nish-gfx.h +373 -61
- package/runtime/nish.d.ts +12 -9
- package/std/README.md +8 -5
- package/std/base64.ts +74 -1
- package/std/bytes.ts +201 -93
- package/std/compress/deflate.ts +20 -15
- package/std/crypto/base64url.ts +93 -66
- package/std/crypto/bcrypt.ts +25 -21
- package/std/crypto/hmac.ts +1 -1
- package/std/crypto/jwt.ts +32 -19
- package/std/crypto/p256.ts +9 -12
- package/std/crypto/pki-roots.ts +7 -4
- package/std/crypto/pki.ts +43 -55
- package/std/crypto/rsa-sign.ts +1632 -0
- package/std/crypto/rsa.ts +26 -4
- package/std/crypto/sha256.ts +135 -68
- package/std/crypto/x25519.ts +12 -11
- package/std/crypto/x509.ts +164 -40
- package/std/gpu/sys.ts +490 -0
- package/std/json-equal.ts +33 -18
- package/std/json-format.ts +39 -8
- package/std/json-number.ts +15 -3
- package/std/json-tokenizer.ts +4 -3
- package/std/json-typed.ts +29 -12
- package/std/json-value.ts +222 -104
- package/std/json.ts +81 -46
- package/std/net/dns-config.ts +18 -5
- package/std/net/dns-message.ts +60 -27
- package/std/net/dns-resolver.ts +35 -16
- package/std/net/dns.ts +3 -2
- package/std/net/fetch.ts +120 -38
- package/std/net/handler.ts +178 -66
- package/std/net/hpack.ts +192 -98
- package/std/net/http-compress.ts +83 -30
- package/std/net/http-fields.ts +327 -31
- package/std/net/http-server.ts +317 -20
- package/std/net/http1-client-codec.ts +137 -40
- package/std/net/http1-codec.ts +391 -202
- package/std/net/http1-server.ts +220 -38
- package/std/net/http2-cleartext.ts +10 -4
- package/std/net/http2-frame.ts +2 -2
- package/std/net/http2-tls.ts +3 -3
- package/std/net/http2.ts +69 -15
- package/std/net/http3-frame.ts +11 -14
- package/std/net/http3-server.ts +12 -15
- package/std/net/http3.ts +41 -29
- package/std/net/multipart.ts +592 -0
- package/std/net/outbound.ts +24 -8
- package/std/net/postgres-async.ts +92 -54
- package/std/net/postgres-carrier.ts +28 -4
- package/std/net/postgres.ts +166 -65
- package/std/net/prefork.ts +86 -5
- package/std/net/protobuf.ts +6 -2
- package/std/net/qpack.ts +66 -25
- package/std/net/quic-conn-ack.ts +27 -7
- package/std/net/quic-conn-cid.ts +13 -7
- package/std/net/quic-datagram.ts +6 -5
- package/std/net/quic-frame.ts +67 -23
- package/std/net/quic-listener.ts +19 -12
- package/std/net/quic-recovery.ts +11 -9
- package/std/net/quic-stream.ts +7 -5
- package/std/net/quic.ts +36 -31
- package/std/net/router.ts +136 -24
- package/std/net/serve.ts +59 -5
- package/std/net/static.ts +81 -33
- package/std/net/tls/record.ts +26 -13
- package/std/net/tls/ticket.ts +6 -5
- package/std/net/tls-tcp.ts +69 -30
- package/std/net/tls.ts +152 -38
- package/std/net/upstream.ts +65 -19
- package/std/net/url.ts +97 -78
- package/std/net/websocket.ts +33 -15
- package/std/net/webtransport.ts +28 -12
- package/std/regex.ts +600 -388
- package/std/sqlite.ts +37 -19
- package/std/sync/ring.ts +232 -115
- package/std/sync/shm.ts +11 -4
- package/std/temporal.ts +232 -133
- package/std/term-io.ts +12 -6
- package/std/term-pixels.ts +158 -22
- package/std/term-rate.ts +8 -3
- package/std/term.ts +4 -4
- package/std/text.ts +33 -11
- package/std/threads.ts +52 -8
- package/std/yaml-json.ts +238 -106
- package/std/yaml-lines.ts +47 -27
- package/std/yaml-write.ts +58 -24
- 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)).
|
|
135
|
-
below
|
|
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
|
-
<!--
|
|
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
|
|
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
|
-
<!--
|
|
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
|
|
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:
|
|
377
|
+
const bad = (a: f32, b: f64): f64 => {
|
|
369
378
|
return a + b;
|
|
370
379
|
};
|
|
371
380
|
```
|
|
372
381
|
|
|
373
|
-
<!--
|
|
374
|
-
<!-- nish:ok --allow-panics -->
|
|
382
|
+
<!-- nish:ok -->
|
|
375
383
|
```ts
|
|
376
|
-
const good = (a:
|
|
377
|
-
return
|
|
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
|
-
<!--
|
|
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
|
-
<!--
|
|
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
|
-
|
|
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
|
-
<!--
|
|
662
|
-
<!-- nish:ok --allow-panics -->
|
|
675
|
+
<!-- nish:ok -->
|
|
663
676
|
```ts
|
|
664
|
-
const add = (a: i32, b: 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):
|
|
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) =>
|
|
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
|
-
<!--
|
|
765
|
-
<!-- nish:ok --allow-panics -->
|
|
777
|
+
<!-- nish:ok -->
|
|
766
778
|
```ts
|
|
767
|
-
interface Shape { area:
|
|
779
|
+
interface Shape { area: f64; }
|
|
768
780
|
|
|
769
781
|
class Circle implements Shape {
|
|
770
|
-
area:
|
|
771
|
-
radius:
|
|
772
|
-
constructor(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):
|
|
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))
|
|
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
|
-
<!--
|
|
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
|
-
|
|
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
|
-
<!--
|
|
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:
|
|
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
|
|
956
|
-
|
|
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 `
|
|
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
|
|
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:
|
|
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
|
-
|
|
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
|
-
<!--
|
|
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 =>
|
|
1197
|
-
|
|
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
|
|
1202
|
-
const squares
|
|
1220
|
+
const sums = new Array<f64>(1);
|
|
1221
|
+
const squares = new Array<i64>(1);
|
|
1203
1222
|
{
|
|
1204
1223
|
using s = scope();
|
|
1205
|
-
|
|
1206
|
-
|
|
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
|
-
|
|
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
|
-
<!--
|
|
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):
|
|
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
|
-
|
|
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)
|
|
1299
|
-
s.spawn(binAll, new Shard([4, 5, 6], tally)
|
|
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
|
-
|
|
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
|
-
<!--
|
|
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
|
-
|
|
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
|
|
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
|
-
<!--
|
|
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
|
|
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
|
-
<!--
|
|
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):
|
|
1458
|
-
for (let i: i32 =
|
|
1459
|
-
p.ch.send(i);
|
|
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 = (
|
|
1492
|
+
const consume = (ch: Channel<i32>): i32 => {
|
|
1465
1493
|
let sum: i32 = 0;
|
|
1466
|
-
for (const x of
|
|
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
|
|
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)
|
|
1479
|
-
s.spawn(produce, new Pipe(ch, 20)
|
|
1480
|
-
|
|
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`)
|
|
1698
|
-
|
|
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
|
-
<!--
|
|
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:
|
|
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:
|
|
1768
|
-
`CPtr | null
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
`
|
|
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
|
-
<!--
|
|
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 (
|
|
1814
|
-
total
|
|
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
|
-
<!--
|
|
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():
|
|
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):
|
|
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
|
-
<!--
|
|
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
|
|
1928
|
-
|
|
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
|
-
<!--
|
|
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
|
-
|
|
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
|
-
<!--
|
|
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:
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
<!--
|
|
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:
|
|
2297
|
+
r: f64 = 3;
|
|
2238
2298
|
}
|
|
2239
2299
|
|
|
2240
2300
|
class Square {
|
|
2241
2301
|
readonly kind: Kind.Square = Kind.Square;
|
|
2242
|
-
side:
|
|
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
|
|
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
|
-
<!--
|
|
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
|
|
2559
|
+
score = 10; // no `break`: falls into `case 1`
|
|
2499
2560
|
case 1:
|
|
2500
|
-
score
|
|
2561
|
+
if (score < 100) {
|
|
2562
|
+
score += 1;
|
|
2563
|
+
}
|
|
2501
2564
|
break;
|
|
2502
2565
|
default:
|
|
2503
2566
|
score = -1;
|
|
@@ -2941,12 +3004,14 @@ console.log(`${seen.size} ${counts.size} ${seen.has("b")}`);
|
|
|
2941
3004
|
three ways: a default with `??`, a `const` tested with `!== undefined` (or
|
|
2942
3005
|
`=== undefined` and an early `return`), or a bare test. Each is one probe.
|
|
2943
3006
|
|
|
2944
|
-
<!--
|
|
2945
|
-
<!-- nish:ok-body --allow-panics -->
|
|
3007
|
+
<!-- nish:ok-body -->
|
|
2946
3008
|
```ts
|
|
2947
3009
|
const counts = new Map<string, i32>();
|
|
2948
3010
|
for (const w of ["a", "b", "a"]) {
|
|
2949
|
-
|
|
3011
|
+
const n = counts.get(w) ?? 0;
|
|
3012
|
+
if (n < 2147483647) { // the bound the `+ 1` needs
|
|
3013
|
+
counts.set(w, n + 1);
|
|
3014
|
+
}
|
|
2950
3015
|
}
|
|
2951
3016
|
const a = counts.get("a");
|
|
2952
3017
|
if (a !== undefined) {
|
|
@@ -2975,8 +3040,7 @@ may `set`, `delete` or `clear` the table inside the walk and get exactly what
|
|
|
2975
3040
|
JavaScript gives (a new key is visited later in the same walk; a key deleted
|
|
2976
3041
|
before it is reached is not).
|
|
2977
3042
|
|
|
2978
|
-
<!--
|
|
2979
|
-
<!-- nish:ok-body --allow-panics -->
|
|
3043
|
+
<!-- nish:ok-body -->
|
|
2980
3044
|
```ts
|
|
2981
3045
|
const ages = new Map<string, i32>();
|
|
2982
3046
|
ages.set("ada", 36).set("alan", 41);
|
|
@@ -2987,6 +3051,9 @@ for (const name of ages.keys()) {
|
|
|
2987
3051
|
console.log(name); // ada, then grace
|
|
2988
3052
|
}
|
|
2989
3053
|
for (const age of ages.values()) {
|
|
3054
|
+
if (age < 0 || age > 150 || total > 1000000) {
|
|
3055
|
+
return 1; // not an age, or too many of them
|
|
3056
|
+
}
|
|
2990
3057
|
total += age;
|
|
2991
3058
|
}
|
|
2992
3059
|
console.log(`${total}`); // 121
|
|
@@ -3016,7 +3083,7 @@ string `+`) or assign. Anything else still compiles, as one probe per call.
|
|
|
3016
3083
|
inserting it — and `reserve(m, n)`, which sizes the table for `n` entries; both
|
|
3017
3084
|
are one module with the program and run unchanged under Node.
|
|
3018
3085
|
|
|
3019
|
-
<!-- allow-panics:
|
|
3086
|
+
<!-- allow-panics: counts in one probe, `counts.set(w, (counts.get(w) ?? 0) + 1)`, whose `+ 1` no guard the checker reads bounds yet -->
|
|
3020
3087
|
<!-- nish:ok --allow-panics -->
|
|
3021
3088
|
```ts
|
|
3022
3089
|
import { getOrInsert, reserve } from "nish/map";
|
|
@@ -3122,8 +3189,7 @@ export const main = (): i32 => {
|
|
|
3122
3189
|
};
|
|
3123
3190
|
```
|
|
3124
3191
|
|
|
3125
|
-
<!--
|
|
3126
|
-
<!-- nish:ok --allow-panics -->
|
|
3192
|
+
<!-- nish:ok -->
|
|
3127
3193
|
```ts
|
|
3128
3194
|
import { JsonWriter } from "nish/json";
|
|
3129
3195
|
import { JsonDecoder, JsonNumber, jsonDecode, jsonEncode } from "nish/json-typed";
|
|
@@ -3202,15 +3268,18 @@ outlive it — so allocate what must outlive the block before it, and let only
|
|
|
3202
3268
|
numbers, booleans and enums out. `arena()` is only ever a `using` initialiser,
|
|
3203
3269
|
and `a` is never read.
|
|
3204
3270
|
|
|
3205
|
-
<!--
|
|
3206
|
-
<!-- nish:ok-body --allow-panics -->
|
|
3271
|
+
<!-- nish:ok-body -->
|
|
3207
3272
|
```ts
|
|
3208
3273
|
const rows: string[] = ["a", "bb"];
|
|
3209
3274
|
let total = 0;
|
|
3210
3275
|
for (const row of rows) {
|
|
3211
3276
|
using a = arena();
|
|
3212
3277
|
const line = `${row}: ${row.length}`;
|
|
3213
|
-
|
|
3278
|
+
const n = line.length;
|
|
3279
|
+
if (n > 1000000 || total > 1000000000) {
|
|
3280
|
+
return 1; // past what this count is for
|
|
3281
|
+
}
|
|
3282
|
+
total = total + n;
|
|
3214
3283
|
}
|
|
3215
3284
|
```
|
|
3216
3285
|
|
|
@@ -3244,7 +3313,7 @@ entry — use a region.** `region(f)` / `regionWith(arg, f)` make one and run
|
|
|
3244
3313
|
gives all of it back. `f` is a named function or an arrow that captures
|
|
3245
3314
|
nothing. `Region<T>` is a number, not a pointer.
|
|
3246
3315
|
|
|
3247
|
-
<!-- allow-panics:
|
|
3316
|
+
<!-- allow-panics: calls `std/region.ts` code that keeps panic sites no proof removes yet -->
|
|
3248
3317
|
<!-- nish:ok --allow-panics -->
|
|
3249
3318
|
```ts
|
|
3250
3319
|
import { Region, enter, free, region } from "nish:region";
|
|
@@ -3255,7 +3324,10 @@ class Session {
|
|
|
3255
3324
|
|
|
3256
3325
|
const open = (): Session => new Session();
|
|
3257
3326
|
const touch = (s: Session): i32 => {
|
|
3258
|
-
|
|
3327
|
+
const n = s.hits;
|
|
3328
|
+
if (n < 2147483647) { // the bound the `+ 1` needs
|
|
3329
|
+
s.hits = n + 1;
|
|
3330
|
+
}
|
|
3259
3331
|
return s.hits;
|
|
3260
3332
|
};
|
|
3261
3333
|
|
|
@@ -3281,12 +3353,11 @@ the module (NL4015).
|
|
|
3281
3353
|
|
|
3282
3354
|
**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
3355
|
|
|
3284
|
-
<!--
|
|
3285
|
-
<!-- nish:ok-body --allow-panics -->
|
|
3356
|
+
<!-- nish:ok-body -->
|
|
3286
3357
|
```ts
|
|
3287
|
-
const xs:
|
|
3358
|
+
const xs: f64[] = [1, 2, 3];
|
|
3288
3359
|
const doubled = xs.map((x) => x * 2);
|
|
3289
|
-
let sum = 0;
|
|
3360
|
+
let sum: f64 = 0; // an `f64` sum cannot overflow
|
|
3290
3361
|
for (const x of doubled) {
|
|
3291
3362
|
sum += x;
|
|
3292
3363
|
}
|
|
@@ -3499,8 +3570,7 @@ guide's [terminal page](guide/using-nish/terminal.md) its walk-through.)
|
|
|
3499
3570
|
dispatch. Give each class the interface's fields **first**, `implements` it,
|
|
3500
3571
|
and write a free function over the interface:
|
|
3501
3572
|
|
|
3502
|
-
<!--
|
|
3503
|
-
<!-- nish:ok --allow-panics -->
|
|
3573
|
+
<!-- nish:ok -->
|
|
3504
3574
|
```ts
|
|
3505
3575
|
interface Sized { width: i32; height: i32; }
|
|
3506
3576
|
|
|
@@ -3514,7 +3584,7 @@ class Photo implements Sized {
|
|
|
3514
3584
|
}
|
|
3515
3585
|
}
|
|
3516
3586
|
|
|
3517
|
-
const area = (s: Sized):
|
|
3587
|
+
const area = (s: Sized): i64 => toI64(s.width) * toI64(s.height);
|
|
3518
3588
|
|
|
3519
3589
|
export const main = (): i32 => {
|
|
3520
3590
|
console.log(`${area(new Photo(3, 4))}`);
|
|
@@ -3616,7 +3686,12 @@ export const at = (xs: i32[], i: i32): i32 => xs[i];
|
|
|
3616
3686
|
|
|
3617
3687
|
A call into the standard library or a dependency that may panic is refused at
|
|
3618
3688
|
the call (NL2458), because only its own author can prove it: call a function
|
|
3619
|
-
that cannot panic. The
|
|
3689
|
+
that cannot panic. The exception is a function whose first statements are
|
|
3690
|
+
`if (start < 0 || end > toI32(buf.length)) { panic(m) }`-style guards on its
|
|
3691
|
+
own parameters (a [`contract`](LANGUAGE.md#panic-sites) site): a call whose
|
|
3692
|
+
arguments the caller proves in range passes, as with
|
|
3693
|
+
`routerDecode(t, 0, toI32(t.length), out)`, when every call to it in the
|
|
3694
|
+
program is proven. The standard library is not yet panic-free, so `Map`,
|
|
3620
3695
|
`Set`, `split` and the `JSON` reads are refused in the scope for now;
|
|
3621
3696
|
a program that needs them builds with `--allow-panics`, and says why where it
|
|
3622
3697
|
is built.
|
|
@@ -3653,10 +3728,9 @@ that keeps to it ([LANGUAGE.md](LANGUAGE.md#the-capability-policy)). Under an
|
|
|
3653
3728
|
allowlist `exit` is a capability like the others, and `unsafe` can be denied
|
|
3654
3729
|
but never allowed: importing `nish:unsafe` is the opt-in.
|
|
3655
3730
|
|
|
3656
|
-
<!--
|
|
3657
|
-
<!-- nish:err NL2459 --deny fs.read --allow-panics -->
|
|
3731
|
+
<!-- nish:err NL2459 --deny fs.read -->
|
|
3658
3732
|
```ts
|
|
3659
|
-
const settings = (): string =>
|
|
3733
|
+
const settings = (): string => readFileSyncOrNull("settings.txt") ?? "";
|
|
3660
3734
|
|
|
3661
3735
|
export const main = (): i32 => settings().length;
|
|
3662
3736
|
```
|