@amritk/nish-aarch64-linux 0.38.0 → 0.39.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/bin/nish +0 -0
- package/docs/AI.md +348 -37
- package/package.json +1 -1
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +301 -18
- package/runtime/nish.h +74 -10
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/runtime-aes.c +12 -4
- package/runtime/runtime-async.c +43 -10
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-net.c +32 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +94 -10
- package/runtime/runtime-sandbox.c +377 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-wasm.c +15 -0
- package/runtime/runtime.c +5 -0
- package/scripts/build.sh +27 -8
- package/std/README.md +5 -4
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/p256.ts +247 -74
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +35 -9
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +113 -10
- package/std/json-tokenizer.ts +20 -0
- package/std/json-typed.ts +217 -8
- package/std/net/handler.ts +130 -16
- package/std/net/http1-codec.ts +499 -150
- package/std/net/http1-server.ts +495 -22
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +84 -0
- package/std/net/postgres-async.ts +1304 -86
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +122 -7
- package/std/net/prefork.ts +195 -11
- package/std/net/quic-datagram.ts +30 -0
- package/std/net/quic-listener.ts +38 -12
- package/std/net/quic-packet.ts +19 -6
- package/std/net/quic-recovery.ts +134 -19
- package/std/net/quic-stream.ts +174 -36
- package/std/net/quic.ts +418 -18
- package/std/net/router.ts +39 -17
- package/std/net/serve.ts +162 -58
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/websocket.ts +55 -18
- package/std/net/webtransport.ts +179 -1
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/secret.ts +26 -59
- package/std/text.ts +233 -0
package/std/regex.ts
CHANGED
|
@@ -1781,6 +1781,25 @@ const CTX_KINDS: i32 = 5
|
|
|
1781
1781
|
const CTX_MASK_LEN: i32 = 6
|
|
1782
1782
|
const CTX_SLOTS: i32 = 4
|
|
1783
1783
|
|
|
1784
|
+
/**
|
|
1785
|
+
* `n` zeros. `new Array<i32>(n)` zero-fills natively and leaves holes under
|
|
1786
|
+
* Node, and this module reads a fresh table as zeros (the DFA's hash probe
|
|
1787
|
+
* stops at a 0 slot), so every table it reads before writing comes from here
|
|
1788
|
+
* (#1362).
|
|
1789
|
+
*/
|
|
1790
|
+
const regexZeros = (n: i32): i32[] => {
|
|
1791
|
+
const a: i32[] = new Array<i32>(n)
|
|
1792
|
+
a.fill(0)
|
|
1793
|
+
return a
|
|
1794
|
+
}
|
|
1795
|
+
|
|
1796
|
+
/** `n` zero bytes, for the reason `regexZeros` gives. */
|
|
1797
|
+
const regexZeroBytes = (n: i32): u8[] => {
|
|
1798
|
+
const a: u8[] = new Array<u8>(n)
|
|
1799
|
+
a.fill(toU8(0))
|
|
1800
|
+
return a
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1784
1803
|
/**
|
|
1785
1804
|
* Which kinds a program's assertions tell apart, as a map from each kind to
|
|
1786
1805
|
* the one a state records. `bits` has bit `1 << kind` for every assertion
|
|
@@ -1788,7 +1807,7 @@ const CTX_SLOTS: i32 = 4
|
|
|
1788
1807
|
* twice.
|
|
1789
1808
|
*/
|
|
1790
1809
|
const regexContextMask = (bits: i32): i32[] => {
|
|
1791
|
-
const m: i32[] =
|
|
1810
|
+
const m: i32[] = regexZeros(CTX_MASK_LEN)
|
|
1792
1811
|
const line: boolean = (bits & (1 << ASSERT_BEGIN_LINE)) !== 0
|
|
1793
1812
|
if ((bits & (1 << ASSERT_BEGIN_TEXT)) !== 0) {
|
|
1794
1813
|
m[CTX_START] = CTX_START
|
|
@@ -1809,6 +1828,8 @@ const regexContextMask = (bits: i32): i32[] => {
|
|
|
1809
1828
|
/** Sort `a` ascending, a bottom-up merge sort. */
|
|
1810
1829
|
const regexSortPoints = (a: i32[]): void => {
|
|
1811
1830
|
const n: i32 = toI32(a.length)
|
|
1831
|
+
// Not `regexZeros`: a call here would cost the proof that `n` is `a`'s
|
|
1832
|
+
// length, and every slot is written before it is read.
|
|
1812
1833
|
const tmp: i32[] = new Array<i32>(n)
|
|
1813
1834
|
let width: i32 = 1
|
|
1814
1835
|
while (width < n) {
|
|
@@ -2646,14 +2667,14 @@ export class Regex {
|
|
|
2646
2667
|
if (count > REGEX_MAX_SLOT_TABLE / slots) {
|
|
2647
2668
|
return "too many groups for the pattern's size"
|
|
2648
2669
|
}
|
|
2649
|
-
this.cDense =
|
|
2650
|
-
this.cSlots =
|
|
2651
|
-
this.nDense =
|
|
2652
|
-
this.nSlots =
|
|
2670
|
+
this.cDense = regexZeros(count)
|
|
2671
|
+
this.cSlots = regexZeros(count * slots)
|
|
2672
|
+
this.nDense = regexZeros(count)
|
|
2673
|
+
this.nSlots = regexZeros(count * slots)
|
|
2653
2674
|
const states: i32 = 1 << this.levels
|
|
2654
|
-
this.visit =
|
|
2655
|
-
this.caps =
|
|
2656
|
-
this.mslots =
|
|
2675
|
+
this.visit = regexZeros(count * states)
|
|
2676
|
+
this.caps = regexZeros(slots)
|
|
2677
|
+
this.mslots = regexZeros(slots)
|
|
2657
2678
|
// Each instruction is visited once per closure and set of marks, and
|
|
2658
2679
|
// pushes at most one frame, or one per slot it clears.
|
|
2659
2680
|
let frames: i32 = 1
|
|
@@ -2671,9 +2692,9 @@ export class Regex {
|
|
|
2671
2692
|
return "too many groups for the pattern's size"
|
|
2672
2693
|
}
|
|
2673
2694
|
frames *= states
|
|
2674
|
-
this.stkKind =
|
|
2675
|
-
this.stkA =
|
|
2676
|
-
this.stkB =
|
|
2695
|
+
this.stkKind = regexZeros(frames)
|
|
2696
|
+
this.stkA = regexZeros(frames)
|
|
2697
|
+
this.stkB = regexZeros(frames)
|
|
2677
2698
|
this.findPrefilters()
|
|
2678
2699
|
if (this.revOk && !this.anchored && this.prefix === "") {
|
|
2679
2700
|
this.findInner(p, root)
|
|
@@ -2733,7 +2754,7 @@ export class Regex {
|
|
|
2733
2754
|
if (bestLen === 0) {
|
|
2734
2755
|
return
|
|
2735
2756
|
}
|
|
2736
|
-
const bytes: u8[] =
|
|
2757
|
+
const bytes: u8[] = regexZeroBytes(bestLen)
|
|
2737
2758
|
i = 0
|
|
2738
2759
|
while (i < toI32(bytes.length)) {
|
|
2739
2760
|
bytes[i] = toU8(p.na[kids[best + i]])
|
|
@@ -2841,15 +2862,15 @@ export class Regex {
|
|
|
2841
2862
|
*/
|
|
2842
2863
|
private findScan(): void {
|
|
2843
2864
|
const count: i32 = this.pc()
|
|
2844
|
-
const masks: u8[] =
|
|
2845
|
-
const odds: i32[] =
|
|
2865
|
+
const masks: u8[] = regexZeroBytes(32 * REGEX_SCAN_DEPTH)
|
|
2866
|
+
const odds: i32[] = regexZeros(REGEX_SCAN_DEPTH)
|
|
2846
2867
|
// `seen[at]` is the depth plus one at which `at` was last walked.
|
|
2847
|
-
const seen: i32[] =
|
|
2868
|
+
const seen: i32[] = regexZeros(count)
|
|
2848
2869
|
let frontier: i32[] = [REGEX_ENTRY]
|
|
2849
2870
|
let depth: i32 = 0
|
|
2850
2871
|
let ended: boolean = false
|
|
2851
2872
|
while (depth < REGEX_SCAN_DEPTH && !ended) {
|
|
2852
|
-
const set: u8[] =
|
|
2873
|
+
const set: u8[] = regexZeroBytes(128)
|
|
2853
2874
|
const next: i32[] = []
|
|
2854
2875
|
let wide: boolean = false
|
|
2855
2876
|
const work: i32[] = []
|
|
@@ -2976,7 +2997,7 @@ export class Regex {
|
|
|
2976
2997
|
// The 32-byte forms the vector window reads, then a membership table of
|
|
2977
2998
|
// 256 bytes a position, which the walker and the kernel's other positions
|
|
2978
2999
|
// read a load a byte.
|
|
2979
|
-
const both: u8[] =
|
|
3000
|
+
const both: u8[] = regexZeroBytes(288 * depth)
|
|
2980
3001
|
let at: i32 = 0
|
|
2981
3002
|
while (at >= 0 && at < 32 * depth && at < toI32(both.length) && at < toI32(masks.length)) {
|
|
2982
3003
|
both[at] = masks[at]
|
|
@@ -3387,7 +3408,7 @@ export class Regex {
|
|
|
3387
3408
|
// terminator starts and where it stops. The reverse program reads the
|
|
3388
3409
|
// same characters, so the forward one is enough.
|
|
3389
3410
|
const classes: i32 = toI32(this.clsFrom.length)
|
|
3390
|
-
const seenCls: u8[] =
|
|
3411
|
+
const seenCls: u8[] = regexZeroBytes(classes)
|
|
3391
3412
|
const points: i32[] = []
|
|
3392
3413
|
pc = 0
|
|
3393
3414
|
while (pc < count) {
|
|
@@ -3446,6 +3467,7 @@ export class Regex {
|
|
|
3446
3467
|
if (units > DFA_MAX_UNITS) {
|
|
3447
3468
|
return
|
|
3448
3469
|
}
|
|
3470
|
+
// Not `regexZeros`, for `regexSortPoints`' reason; every slot is written first.
|
|
3449
3471
|
const rep: i32[] = new Array<i32>(units)
|
|
3450
3472
|
let u: i32 = 0
|
|
3451
3473
|
while (u < 128 && u < toI32(rep.length)) {
|
|
@@ -3460,9 +3482,9 @@ export class Regex {
|
|
|
3460
3482
|
}
|
|
3461
3483
|
// Refine one predicate at a time: a unit's class is its old class and
|
|
3462
3484
|
// whether the predicate holds for it.
|
|
3463
|
-
const cls: i32[] =
|
|
3464
|
-
const hit: u8[] =
|
|
3465
|
-
const remap: i32[] =
|
|
3485
|
+
const cls: i32[] = regexZeros(units)
|
|
3486
|
+
const hit: u8[] = regexZeroBytes(units)
|
|
3487
|
+
const remap: i32[] = regexZeros(2 * units)
|
|
3466
3488
|
let n: i32 = 1
|
|
3467
3489
|
let work: i32 = 0
|
|
3468
3490
|
u = 0
|
|
@@ -3471,7 +3493,7 @@ export class Regex {
|
|
|
3471
3493
|
u += 1
|
|
3472
3494
|
}
|
|
3473
3495
|
n = this.dRefine(cls, hit, units, n, remap)
|
|
3474
|
-
const pairs: u8[] =
|
|
3496
|
+
const pairs: u8[] = regexZeroBytes(16384)
|
|
3475
3497
|
i = 0
|
|
3476
3498
|
while (i < toI32(seenCls.length)) {
|
|
3477
3499
|
seenCls[i] = 0
|
|
@@ -3562,7 +3584,7 @@ export class Regex {
|
|
|
3562
3584
|
return
|
|
3563
3585
|
}
|
|
3564
3586
|
this.dShift = shift
|
|
3565
|
-
this.dCls =
|
|
3587
|
+
this.dCls = regexZeros(256)
|
|
3566
3588
|
u = 0
|
|
3567
3589
|
while (u < 256) {
|
|
3568
3590
|
this.dCls[u] = u < 128 && u < toI32(cls.length) ? cls[u] : n
|
|
@@ -3571,7 +3593,7 @@ export class Regex {
|
|
|
3571
3593
|
this.dEot = cls[128]
|
|
3572
3594
|
this.dWlo = lows
|
|
3573
3595
|
this.dWide = wide
|
|
3574
|
-
this.dWcls =
|
|
3596
|
+
this.dWcls = regexZeros(wide)
|
|
3575
3597
|
i = 0
|
|
3576
3598
|
while (i < wide && 129 + i < toI32(cls.length)) {
|
|
3577
3599
|
this.dWcls[i] = cls[129 + i]
|
|
@@ -3584,11 +3606,11 @@ export class Regex {
|
|
|
3584
3606
|
this.dMaskF[CTX_START] === CTX_OTHER &&
|
|
3585
3607
|
this.dMaskF[CTX_WORD] === CTX_OTHER &&
|
|
3586
3608
|
this.dMaskF[CTX_LINE] === CTX_OTHER
|
|
3587
|
-
this.dRep =
|
|
3588
|
-
this.dNextK =
|
|
3589
|
-
this.dCtxF =
|
|
3590
|
-
this.dCtxR =
|
|
3591
|
-
const done: u8[] =
|
|
3609
|
+
this.dRep = regexZeros(n)
|
|
3610
|
+
this.dNextK = regexZeros(n)
|
|
3611
|
+
this.dCtxF = regexZeros(n)
|
|
3612
|
+
this.dCtxR = regexZeros(n)
|
|
3613
|
+
const done: u8[] = regexZeroBytes(n)
|
|
3592
3614
|
u = 0
|
|
3593
3615
|
while (u < toI32(cls.length) && u < toI32(rep.length)) {
|
|
3594
3616
|
const c: i32 = cls[u]
|
|
@@ -3610,24 +3632,24 @@ export class Regex {
|
|
|
3610
3632
|
}
|
|
3611
3633
|
u += 1
|
|
3612
3634
|
}
|
|
3613
|
-
this.dVisit =
|
|
3614
|
-
this.dLeaf =
|
|
3615
|
-
this.dBuf =
|
|
3616
|
-
this.dStk =
|
|
3635
|
+
this.dVisit = regexZeros(total)
|
|
3636
|
+
this.dLeaf = regexZeros(total)
|
|
3637
|
+
this.dBuf = regexZeros(total)
|
|
3638
|
+
this.dStk = regexZeros(total + 1)
|
|
3617
3639
|
this.dMax = DFA_MAX_TABLE >> shift < DFA_MAX_STATES ? DFA_MAX_TABLE >> shift : DFA_MAX_STATES
|
|
3618
3640
|
this.dCap = this.dMax < DFA_FIRST_STATES ? this.dMax : DFA_FIRST_STATES
|
|
3619
|
-
this.dTrans =
|
|
3620
|
-
this.dStart =
|
|
3621
|
-
this.dLen =
|
|
3622
|
-
this.dFlags =
|
|
3623
|
-
this.dHash =
|
|
3641
|
+
this.dTrans = regexZeros(this.dCap << shift)
|
|
3642
|
+
this.dStart = regexZeros(this.dCap)
|
|
3643
|
+
this.dLen = regexZeros(this.dCap)
|
|
3644
|
+
this.dFlags = regexZeros(this.dCap)
|
|
3645
|
+
this.dHash = regexZeros(this.dHashSize(this.dCap))
|
|
3624
3646
|
this.dPoolMax = 2 * total > DFA_MIN_POOL ? 2 * total : DFA_MIN_POOL
|
|
3625
|
-
this.dPool =
|
|
3647
|
+
this.dPool = regexZeros(DFA_FIRST_POOL)
|
|
3626
3648
|
this.dPoolLen = 0
|
|
3627
3649
|
// Row 0 is never a state, so a plain transition, the next row's offset,
|
|
3628
3650
|
// is never 0, the entry not yet computed.
|
|
3629
3651
|
this.dCount = 1
|
|
3630
|
-
this.dStartOff =
|
|
3652
|
+
this.dStartOff = regexZeros(4 * CTX_SLOTS)
|
|
3631
3653
|
this.dSkip = !this.anchored && (this.prefix !== "" || this.scanSel)
|
|
3632
3654
|
this.dPrefFast = this.dSkip && this.dCtxFree && this.prefix !== "" && isRegexAscii(this.prefix)
|
|
3633
3655
|
this.dPrefOff = 0
|
|
@@ -3919,7 +3941,7 @@ export class Regex {
|
|
|
3919
3941
|
if (grown > this.dPoolMax) {
|
|
3920
3942
|
grown = this.dPoolMax
|
|
3921
3943
|
}
|
|
3922
|
-
const pool: i32[] =
|
|
3944
|
+
const pool: i32[] = regexZeros(grown)
|
|
3923
3945
|
let k: i32 = 0
|
|
3924
3946
|
while (k < this.dPoolLen && k < toI32(pool.length)) {
|
|
3925
3947
|
pool[k] = this.dPool[k]
|
|
@@ -3956,16 +3978,16 @@ export class Regex {
|
|
|
3956
3978
|
cap = this.dMax
|
|
3957
3979
|
}
|
|
3958
3980
|
const used: i32 = this.dCount << this.dShift
|
|
3959
|
-
const trans: i32[] =
|
|
3981
|
+
const trans: i32[] = regexZeros(cap << this.dShift)
|
|
3960
3982
|
let k: i32 = 0
|
|
3961
3983
|
while (k < used && k < toI32(trans.length)) {
|
|
3962
3984
|
trans[k] = this.dTrans[k]
|
|
3963
3985
|
k += 1
|
|
3964
3986
|
}
|
|
3965
|
-
const start: i32[] =
|
|
3966
|
-
const len: i32[] =
|
|
3967
|
-
const flags: i32[] =
|
|
3968
|
-
const hash: i32[] =
|
|
3987
|
+
const start: i32[] = regexZeros(cap)
|
|
3988
|
+
const len: i32[] = regexZeros(cap)
|
|
3989
|
+
const flags: i32[] = regexZeros(cap)
|
|
3990
|
+
const hash: i32[] = regexZeros(this.dHashSize(cap))
|
|
3969
3991
|
const mask: i32 = toI32(hash.length) - 1
|
|
3970
3992
|
// Every length test below always holds (`dCount` is at most `cap`, and
|
|
3971
3993
|
// `& mask` keeps a slot within `hash`): each is what proves its index.
|
|
@@ -4621,7 +4643,7 @@ export class Regex {
|
|
|
4621
4643
|
return false
|
|
4622
4644
|
}
|
|
4623
4645
|
if (toI32(m.slots.length) !== ns) {
|
|
4624
|
-
m.slots =
|
|
4646
|
+
m.slots = regexZeros(ns)
|
|
4625
4647
|
}
|
|
4626
4648
|
let k: i32 = 0
|
|
4627
4649
|
while (k < ns) {
|
package/std/region.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `nish:region` —
|
|
3
|
-
*
|
|
2
|
+
* `nish:region` — the source behind the builtin module: memory a program frees
|
|
3
|
+
* when it says so.
|
|
4
|
+
*
|
|
5
|
+
* **What the module means is documented once, in `runtime/nish.d.ts`**: the
|
|
6
|
+
* `declare module "nish:region"` block there is what an editor shows and what
|
|
7
|
+
* `docs/reference/nish-region.md` is generated from, and `docs/LANGUAGE.md`
|
|
8
|
+
* ("Regions") holds the rules. The comments in this file say how the module
|
|
9
|
+
* is built; change a meaning there first.
|
|
4
10
|
*
|
|
5
11
|
* import { Region, enter, free, regionWith } from "nish:region"
|
|
6
12
|
*
|
|
@@ -25,15 +31,19 @@
|
|
|
25
31
|
* **This file is the source behind the builtin module**, and it is imported
|
|
26
32
|
* only by that name: `nish/region` is refused, so there is one spelling for the
|
|
27
33
|
* checker's rules to be read under (`src/region-check.ts`). What is written
|
|
28
|
-
* here is the meaning, over
|
|
34
|
+
* here is the meaning, over six intrinsics the compiler gives bodies of their
|
|
29
35
|
* own (`src/emit-region.ts`), which swap the thread's arena for the region's
|
|
30
36
|
* around the function they run:
|
|
31
37
|
*
|
|
32
38
|
* - `region(f)` makes a region, runs `f` with every allocation landing in
|
|
33
39
|
* it, and keeps what `f` answers as the region's root;
|
|
34
40
|
* - `enter(r, f)` runs `f(root)` the same way, and answers `f`'s scalar;
|
|
35
|
-
* - `free(r)` gives the region's memory back. A handle
|
|
36
|
-
* freed, or entered twice, or freed while
|
|
41
|
+
* - `free(r)` gives the region's memory back, and its children's. A handle
|
|
42
|
+
* used after it is freed, or entered twice, or freed while it or a child
|
|
43
|
+
* is entered, stops the program;
|
|
44
|
+
* - `child(f)` and `childWith(arg, f)`, inside an `enter`, make a region
|
|
45
|
+
* whose parent is the one entered: it may keep what the parent's root
|
|
46
|
+
* reaches, and is freed with the parent.
|
|
37
47
|
*
|
|
38
48
|
* **A `Region<T>` is a handle**, a 64-bit integer, and not a pointer: the root
|
|
39
49
|
* never leaves the region except through `enter`'s function, and that
|
|
@@ -53,7 +63,7 @@ export class Region<T> {
|
|
|
53
63
|
root: T | null = null
|
|
54
64
|
}
|
|
55
65
|
|
|
56
|
-
// The
|
|
66
|
+
// The six intrinsics below are the runtime's region calls. Each is written
|
|
57
67
|
// here as a call of itself, so it has a type and nothing more; the compiler
|
|
58
68
|
// gives every instance a body of its own (`src/emit-region.ts`), one call into
|
|
59
69
|
// `runtime/runtime-region.c`.
|
|
@@ -73,6 +83,9 @@ const reopen = <T>(r: Region<T>): T => reopen(r)
|
|
|
73
83
|
/** `nish_region_leave` with the root `reopen` gave. */
|
|
74
84
|
const leave = <T>(r: Region<T>, root: T): void => leave(r, root)
|
|
75
85
|
|
|
86
|
+
/** `nish_region_child_new`: a fresh handle whose parent is the region entered now. */
|
|
87
|
+
const childNew = (): i64 => childNew()
|
|
88
|
+
|
|
76
89
|
/**
|
|
77
90
|
* A new region, whose root is what `f` answers; everything `f` allocates is in
|
|
78
91
|
* it.
|
|
@@ -93,8 +106,8 @@ export const region = <T>(f: () => T): Region<T> => {
|
|
|
93
106
|
* nothing.
|
|
94
107
|
*
|
|
95
108
|
* @param arg handed to `f`. It lives outside the region, so unless it is a
|
|
96
|
-
* number, `boolean
|
|
97
|
-
*
|
|
109
|
+
* number, `boolean` or `enum` `f` may not store it into region memory
|
|
110
|
+
* (NL2610), a string included.
|
|
98
111
|
* @param f builds the root from `arg`, under the same rules as `region`'s.
|
|
99
112
|
* @returns the handle to the new region, which holds its memory until `free`.
|
|
100
113
|
*/
|
|
@@ -137,13 +150,51 @@ export const enterWith = <T, A, R>(r: Region<T>, arg: A, f: (root: T, arg: A) =>
|
|
|
137
150
|
return result
|
|
138
151
|
}
|
|
139
152
|
|
|
153
|
+
/**
|
|
154
|
+
* A new region whose parent is the region entered now: `region`, except that
|
|
155
|
+
* the child may keep the parent's memory that `enter` handed in, and freeing
|
|
156
|
+
* the parent frees the child. Written only in the function an `enter` or
|
|
157
|
+
* `enterWith` runs (NL2702).
|
|
158
|
+
*
|
|
159
|
+
* @param f builds the child's root, under `region`'s rules, except that it may
|
|
160
|
+
* store into the child what the parent's root reaches. What the entered
|
|
161
|
+
* function allocated in the parent it may not (NL2610).
|
|
162
|
+
* @returns the handle to the child, which holds its memory until `free` of it
|
|
163
|
+
* or of its parent.
|
|
164
|
+
*/
|
|
165
|
+
export const child = <T>(f: () => T): Region<T> => {
|
|
166
|
+
const h: i64 = childNew()
|
|
167
|
+
open(h)
|
|
168
|
+
return close(h, f())
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* `child` with one argument for `f`: the way the parent's memory reaches the
|
|
173
|
+
* child, because the arrow `child` runs captures nothing.
|
|
174
|
+
*
|
|
175
|
+
* @param arg handed to `f`: a number, `boolean` or `enum`, or memory the
|
|
176
|
+
* parent's root reaches, which `f` may store into the child, a string read
|
|
177
|
+
* off the root included. Memory the entered function allocated, and a string
|
|
178
|
+
* the root does not reach, are refused when `f` stores them (NL2610).
|
|
179
|
+
* @param f builds the child's root from `arg`, under the same rules as
|
|
180
|
+
* `child`'s.
|
|
181
|
+
* @returns the handle to the child, which holds its memory until `free` of it
|
|
182
|
+
* or of its parent.
|
|
183
|
+
*/
|
|
184
|
+
export const childWith = <A, T>(arg: A, f: (arg: A) => T): Region<T> => {
|
|
185
|
+
const h: i64 = childNew()
|
|
186
|
+
open(h)
|
|
187
|
+
return close(h, f(arg))
|
|
188
|
+
}
|
|
189
|
+
|
|
140
190
|
/**
|
|
141
191
|
* Give the region's memory back. The handle is dead after it. The compiler
|
|
142
192
|
* writes the body.
|
|
143
193
|
*
|
|
144
|
-
* @param _r the region to free
|
|
145
|
-
*
|
|
146
|
-
*
|
|
194
|
+
* @param _r the region to free, with every child made under it. It, its
|
|
195
|
+
* children and every copy of their handles are dead after this; freeing it
|
|
196
|
+
* again, or while it or a child is entered, stops the program rather than
|
|
197
|
+
* freeing twice.
|
|
147
198
|
*/
|
|
148
199
|
export const free = <T>(_r: Region<T>): void => {
|
|
149
200
|
// `nish_region_free`: see above.
|
package/std/secret.ts
CHANGED
|
@@ -1,44 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `nish:secret` —
|
|
3
|
-
* (docs/LANGUAGE.md, "Secrets").
|
|
2
|
+
* `nish:secret` — the source behind the builtin module.
|
|
4
3
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* **What the module means is documented once, in `runtime/nish.d.ts`**: the
|
|
5
|
+
* `declare module "nish:secret"` block there is what an editor shows and what
|
|
6
|
+
* `docs/reference/nish-secret.md` is generated from, and `docs/LANGUAGE.md`
|
|
7
|
+
* ("Secrets") holds the rules. The comments in this file say only how the
|
|
8
|
+
* module is built; change a meaning there, not here.
|
|
7
9
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* const message: u8[] = [1, 2, 3]
|
|
14
|
-
* const mac: u8[] = exposeWith(key, message, tag)
|
|
15
|
-
* wipe(key)
|
|
16
|
-
*
|
|
17
|
-
* **This file is the source behind the builtin module**, and it is imported
|
|
18
|
-
* only by that name: `nish/secret` is refused, so there is one spelling for the
|
|
19
|
-
* checker's rules to be read under. What is written here is the meaning; what
|
|
20
|
-
* makes a `Secret` worth having is in `src/secret.ts`, which knows this module
|
|
21
|
-
* by its path and package:
|
|
22
|
-
*
|
|
23
|
-
* - a `Secret` is opaque. Nothing outside this file reads its field, builds
|
|
24
|
-
* one with `new`, prints it, interpolates it, compares it with anything but
|
|
25
|
-
* another `Secret` or `null`, branches on it, indexes it, stores it in a
|
|
26
|
-
* field, an array or a `Result`, or hands it to a builtin;
|
|
27
|
-
* - a `Secret` a function makes leaves it returned or wiped, on every path;
|
|
28
|
-
* - `expose` is the one way in, and the function it runs may reach no I/O and
|
|
29
|
-
* no C, may not keep the value, may not write the argument `exposeWith`
|
|
30
|
-
* hands it, and returns a declared type that holds no `Secret`;
|
|
31
|
-
* - `wipe` is a volatile store of zeros over the value's whole storage,
|
|
32
|
-
* which no optimiser may remove (`src/emit-secret.ts`).
|
|
33
|
-
*
|
|
34
|
-
* **Under Node the module is answered by `runtime/shim.mjs`**, where a
|
|
35
|
-
* `Secret` is a plain wrapper and `wipe` zero-fills it.
|
|
10
|
+
* It is imported only by its builtin name: `nish/secret` is refused, so there
|
|
11
|
+
* is one spelling for the checker's rules to be read under. Those rules are
|
|
12
|
+
* `src/secret.ts`, which knows this module by its path and package, and the
|
|
13
|
+
* volatile store `wipe` lowers to is `src/emit-secret.ts`. Under Node the
|
|
14
|
+
* module is answered by `runtime/nish.mjs`.
|
|
36
15
|
*/
|
|
37
16
|
|
|
38
|
-
/**
|
|
39
|
-
* Key material: an array of integers (`u8[]`, `u32[]`, ...) or a record whose
|
|
40
|
-
* every field is an integer. Made by `secret`, read only through `expose`.
|
|
41
|
-
*/
|
|
17
|
+
/** Key material, held opaque (`runtime/nish.d.ts`). Here, a box around one field. */
|
|
42
18
|
export class Secret<T> {
|
|
43
19
|
/** @internal Used by `expose` and `exposeWith`. The wrapped value. Only this module reads or writes it (`src/secret.ts`). */
|
|
44
20
|
value: T
|
|
@@ -55,48 +31,39 @@ export class Secret<T> {
|
|
|
55
31
|
}
|
|
56
32
|
|
|
57
33
|
/**
|
|
58
|
-
* `value`, wrapped. The
|
|
34
|
+
* `value`, wrapped (`runtime/nish.d.ts`). The box holds `value` itself, not a
|
|
35
|
+
* copy, so its storage is the storage `wipe` later zeroes.
|
|
59
36
|
*
|
|
60
|
-
* @param value the key material
|
|
61
|
-
*
|
|
62
|
-
* @returns a `Secret` holding `value` itself, not a copy, so its storage is
|
|
63
|
-
* the storage `wipe` later zeroes.
|
|
37
|
+
* @param value the key material, kept as it is.
|
|
38
|
+
* @returns the box.
|
|
64
39
|
*/
|
|
65
40
|
export const secret = <T>(value: T): Secret<T> => new Secret<T>(value)
|
|
66
41
|
|
|
67
42
|
/**
|
|
68
|
-
* `f(value)
|
|
43
|
+
* `f(value)` (`runtime/nish.d.ts`): the field read, the one this module makes.
|
|
69
44
|
*
|
|
70
|
-
* @param s the
|
|
71
|
-
*
|
|
72
|
-
* @param f the computation, handed the held value itself. The checker holds
|
|
73
|
-
* it to the rules in the module header: no I/O, no C, it does not keep the
|
|
74
|
-
* value, and its declared result type holds no `Secret`.
|
|
45
|
+
* @param s the box to read.
|
|
46
|
+
* @param f handed the held value itself.
|
|
75
47
|
* @returns what `f` returns.
|
|
76
48
|
*/
|
|
77
49
|
export const expose = <T, R>(s: Secret<T>, f: (value: T) => R): R => f(s.value)
|
|
78
50
|
|
|
79
51
|
/**
|
|
80
|
-
* `f(value, arg)
|
|
81
|
-
* runs captures nothing.
|
|
52
|
+
* `f(value, arg)` (`runtime/nish.d.ts`): `expose` with one more argument.
|
|
82
53
|
*
|
|
83
|
-
* @param s the
|
|
84
|
-
* @param arg handed to `f` as its second argument.
|
|
85
|
-
*
|
|
86
|
-
* @param f the computation, under the same rules as `expose`'s.
|
|
54
|
+
* @param s the box to read.
|
|
55
|
+
* @param arg handed to `f` as its second argument.
|
|
56
|
+
* @param f handed the held value itself and `arg`.
|
|
87
57
|
* @returns what `f` returns.
|
|
88
58
|
*/
|
|
89
59
|
export const exposeWith = <T, A, R>(s: Secret<T>, arg: A, f: (value: T, arg: A) => R): R => f(s.value, arg)
|
|
90
60
|
|
|
91
61
|
/**
|
|
92
|
-
* Zero every byte of `target`'s storage
|
|
93
|
-
*
|
|
94
|
-
* gives each instance a body of its own, one volatile `llvm.memset`
|
|
62
|
+
* Zero every byte of `target`'s storage (`runtime/nish.d.ts`). Natively the
|
|
63
|
+
* compiler gives each instance a body of its own, one volatile `llvm.memset`
|
|
95
64
|
* (`src/emit-secret.ts`); this one is never emitted.
|
|
96
65
|
*
|
|
97
|
-
* @param _target what to zero
|
|
98
|
-
* first), or an array or record of integers that `expose`'s function
|
|
99
|
-
* holds.
|
|
66
|
+
* @param _target what to zero.
|
|
100
67
|
*/
|
|
101
68
|
export const wipe = <T>(_target: T): void => {
|
|
102
69
|
// Lowered by the compiler: see above.
|