@amritk/nish-aarch64-linux 0.38.0 → 0.40.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/INSTALL.md +74 -129
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +455 -65
  4. package/package.json +1 -1
  5. package/runtime/core/host.d.ts +11 -0
  6. package/runtime/core/host.mjs +35 -0
  7. package/runtime/nish.d.ts +320 -33
  8. package/runtime/nish.h +84 -10
  9. package/runtime/nish.mjs +69 -6
  10. package/runtime/node.mjs +117 -0
  11. package/runtime/page-frame.d.ts +123 -0
  12. package/runtime/page-frame.mjs +736 -0
  13. package/runtime/page-game.d.ts +132 -0
  14. package/runtime/page-game.mjs +738 -0
  15. package/runtime/runtime-aes.c +97 -12
  16. package/runtime/runtime-async.c +43 -10
  17. package/runtime/runtime-bytes.c +88 -4
  18. package/runtime/runtime-checksum.c +207 -15
  19. package/runtime/runtime-net.c +64 -3
  20. package/runtime/runtime-os.c +174 -27
  21. package/runtime/runtime-region.c +94 -10
  22. package/runtime/runtime-sandbox.c +404 -17
  23. package/runtime/runtime-sha256.c +11 -4
  24. package/runtime/runtime-wasm.c +437 -0
  25. package/runtime/runtime.c +5 -0
  26. package/runtime/vite.mjs +1 -1
  27. package/scripts/build.sh +42 -8
  28. package/std/README.md +78 -91
  29. package/std/array.ts +36 -3
  30. package/std/collections.ts +51 -0
  31. package/std/compress/checksum.ts +13 -0
  32. package/std/compress/deflate.ts +0 -2
  33. package/std/crypto/hmac.ts +37 -10
  34. package/std/crypto/p256.ts +1629 -2732
  35. package/std/crypto/p384.ts +3 -1
  36. package/std/crypto/pki.ts +151 -13
  37. package/std/crypto/sha256.ts +34 -6
  38. package/std/crypto/x25519.ts +153 -64
  39. package/std/crypto/x509.ts +120 -10
  40. package/std/host/game.ts +273 -0
  41. package/std/input.ts +56 -0
  42. package/std/json-tokenizer.ts +344 -111
  43. package/std/json-typed.ts +256 -12
  44. package/std/json-value.ts +144 -3
  45. package/std/json.ts +63 -8
  46. package/std/net/fetch.ts +22 -4
  47. package/std/net/handler.ts +130 -16
  48. package/std/net/http-server.ts +3382 -0
  49. package/std/net/http1-client-codec.ts +6 -2
  50. package/std/net/http1-codec.ts +636 -162
  51. package/std/net/http1-server.ts +569 -42
  52. package/std/net/http2-cleartext.ts +526 -0
  53. package/std/net/http2-tls.ts +48 -0
  54. package/std/net/http2.ts +131 -14
  55. package/std/net/http3-server.ts +29 -3
  56. package/std/net/http3.ts +180 -7
  57. package/std/net/postgres-async.ts +1304 -86
  58. package/std/net/postgres-carrier.ts +343 -10
  59. package/std/net/postgres.ts +122 -7
  60. package/std/net/prefork.ts +195 -11
  61. package/std/net/quic-datagram.ts +30 -0
  62. package/std/net/quic-listener.ts +38 -12
  63. package/std/net/quic-packet.ts +19 -6
  64. package/std/net/quic-recovery.ts +373 -27
  65. package/std/net/quic-stream.ts +195 -37
  66. package/std/net/quic.ts +516 -22
  67. package/std/net/router.ts +407 -38
  68. package/std/net/serve.ts +1022 -110
  69. package/std/net/tls/codec.ts +110 -32
  70. package/std/net/tls/record-server.ts +37 -7
  71. package/std/net/tls/verify.ts +19 -1
  72. package/std/net/tls-tcp.ts +56 -1
  73. package/std/net/tls.ts +196 -50
  74. package/std/net/url.ts +83 -35
  75. package/std/net/websocket.ts +129 -19
  76. package/std/net/webtransport.ts +185 -1
  77. package/std/raster.ts +837 -0
  78. package/std/regex.ts +69 -47
  79. package/std/region.ts +62 -11
  80. package/std/release.ts +593 -0
  81. package/std/secret.ts +26 -59
  82. package/std/synth.ts +143 -0
  83. package/std/text.ts +233 -0
  84. package/std/threads.ts +298 -0
  85. package/std/yaml-json.ts +368 -28
  86. package/std/yaml.ts +122 -27
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[] = new Array<i32>(CTX_MASK_LEN)
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 = new Array<i32>(count)
2650
- this.cSlots = new Array<i32>(count * slots)
2651
- this.nDense = new Array<i32>(count)
2652
- this.nSlots = new Array<i32>(count * slots)
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 = new Array<i32>(count * states)
2655
- this.caps = new Array<i32>(slots)
2656
- this.mslots = new Array<i32>(slots)
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 = new Array<i32>(frames)
2675
- this.stkA = new Array<i32>(frames)
2676
- this.stkB = new Array<i32>(frames)
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[] = new Array<u8>(bestLen)
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[] = new Array<u8>(32 * REGEX_SCAN_DEPTH)
2845
- const odds: i32[] = new Array<i32>(REGEX_SCAN_DEPTH)
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[] = new Array<i32>(count)
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[] = new Array<u8>(128)
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[] = new Array<u8>(288 * depth)
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[] = new Array<u8>(classes)
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[] = new Array<i32>(units)
3464
- const hit: u8[] = new Array<u8>(units)
3465
- const remap: i32[] = new Array<i32>(2 * units)
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[] = new Array<u8>(16384)
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 = new Array<i32>(256)
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 = new Array<i32>(wide)
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 = new Array<i32>(n)
3588
- this.dNextK = new Array<i32>(n)
3589
- this.dCtxF = new Array<i32>(n)
3590
- this.dCtxR = new Array<i32>(n)
3591
- const done: u8[] = new Array<u8>(n)
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 = new Array<i32>(total)
3614
- this.dLeaf = new Array<i32>(total)
3615
- this.dBuf = new Array<i32>(total)
3616
- this.dStk = new Array<i32>(total + 1)
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 = new Array<i32>(this.dCap << shift)
3620
- this.dStart = new Array<i32>(this.dCap)
3621
- this.dLen = new Array<i32>(this.dCap)
3622
- this.dFlags = new Array<i32>(this.dCap)
3623
- this.dHash = new Array<i32>(this.dHashSize(this.dCap))
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 = new Array<i32>(DFA_FIRST_POOL)
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 = new Array<i32>(4 * CTX_SLOTS)
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[] = new Array<i32>(grown)
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[] = new Array<i32>(cap << this.dShift)
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[] = new Array<i32>(cap)
3966
- const len: i32[] = new Array<i32>(cap)
3967
- const flags: i32[] = new Array<i32>(cap)
3968
- const hash: i32[] = new Array<i32>(this.dHashSize(cap))
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 = new Array<i32>(ns)
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` — memory a program frees when it says so (docs/LANGUAGE.md,
3
- * "Regions").
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 five intrinsics the compiler gives bodies of their
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 used after it is
36
- * freed, or entered twice, or freed while entered, stops the program.
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 five intrinsics below are the runtime's region calls. Each is written
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`, `enum` or string literal `f` may not store it into
97
- * region memory (NL2610).
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. It and every copy of the handle are dead after
145
- * this; freeing it again, or while it is entered, stops the program rather
146
- * than freeing twice.
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.