@amritk/nish-x86_64-linux 0.42.0 → 0.45.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 (124) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +287 -129
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/std/README.md +6 -6
  21. package/std/array.ts +10 -2
  22. package/std/bytes.ts +239 -26
  23. package/std/cli-args.ts +1072 -606
  24. package/std/cli-flags.ts +180 -98
  25. package/std/cli.ts +21 -13
  26. package/std/collections.ts +245 -127
  27. package/std/compress/checksum.ts +32 -37
  28. package/std/compress/deflate.ts +1102 -542
  29. package/std/compress/gzip.ts +290 -145
  30. package/std/compress/zlib.ts +57 -21
  31. package/std/crypto/aes.ts +153 -75
  32. package/std/crypto/base64url.ts +163 -39
  33. package/std/crypto/bcrypt-batch.ts +164 -90
  34. package/std/crypto/bcrypt.ts +185 -107
  35. package/std/crypto/chacha20poly1305.ts +84 -68
  36. package/std/crypto/ct.ts +9 -7
  37. package/std/crypto/ed25519.ts +64 -27
  38. package/std/crypto/hkdf.ts +105 -63
  39. package/std/crypto/hmac.ts +95 -89
  40. package/std/crypto/jwt.ts +248 -126
  41. package/std/crypto/p256.ts +758 -444
  42. package/std/crypto/p384.ts +20 -13
  43. package/std/crypto/pki-roots.ts +95 -38
  44. package/std/crypto/pki.ts +164 -60
  45. package/std/crypto/rsa.ts +266 -223
  46. package/std/crypto/scram.ts +141 -0
  47. package/std/crypto/sha1.ts +157 -57
  48. package/std/crypto/sha256.ts +502 -422
  49. package/std/crypto/sha512.ts +204 -135
  50. package/std/crypto/x509-random.ts +9 -3
  51. package/std/crypto/x509.ts +331 -171
  52. package/std/host/ring.ts +1 -1
  53. package/std/host/text.ts +1 -1
  54. package/std/json-equal.ts +35 -19
  55. package/std/json-format.ts +215 -108
  56. package/std/json-number.ts +126 -72
  57. package/std/json-pointer.ts +42 -13
  58. package/std/json-tokenizer.ts +498 -307
  59. package/std/json-typed.ts +115 -36
  60. package/std/json-value.ts +341 -152
  61. package/std/json.ts +300 -158
  62. package/std/net/buffer-pool.ts +17 -7
  63. package/std/net/dns-config.ts +427 -236
  64. package/std/net/dns-message.ts +114 -47
  65. package/std/net/dns-resolver.ts +736 -237
  66. package/std/net/dns.ts +205 -52
  67. package/std/net/fetch.ts +320 -184
  68. package/std/net/grpc.ts +948 -0
  69. package/std/net/handler.ts +1276 -218
  70. package/std/net/hpack.ts +395 -180
  71. package/std/net/http-compress.ts +502 -65
  72. package/std/net/http-fields.ts +85 -49
  73. package/std/net/http-server.ts +1764 -603
  74. package/std/net/http1-client-codec.ts +372 -161
  75. package/std/net/http1-codec.ts +665 -331
  76. package/std/net/http1-server.ts +881 -269
  77. package/std/net/http2-cleartext.ts +165 -95
  78. package/std/net/http2-frame.ts +232 -89
  79. package/std/net/http2-tls.ts +42 -12
  80. package/std/net/http2.ts +327 -101
  81. package/std/net/http3-frame.ts +68 -33
  82. package/std/net/http3-server.ts +672 -206
  83. package/std/net/http3.ts +669 -191
  84. package/std/net/oidc.ts +63 -24
  85. package/std/net/outbound.ts +207 -142
  86. package/std/net/postgres-async.ts +1223 -470
  87. package/std/net/postgres-carrier.ts +575 -372
  88. package/std/net/postgres.ts +1521 -632
  89. package/std/net/prefork.ts +254 -98
  90. package/std/net/protobuf.ts +1017 -0
  91. package/std/net/qpack.ts +143 -92
  92. package/std/net/quic-conn-ack.ts +104 -22
  93. package/std/net/quic-conn-cid.ts +66 -26
  94. package/std/net/quic-conn-params.ts +13 -7
  95. package/std/net/quic-datagram.ts +90 -27
  96. package/std/net/quic-frame.ts +324 -121
  97. package/std/net/quic-listener.ts +206 -91
  98. package/std/net/quic-packet.ts +289 -163
  99. package/std/net/quic-recovery.ts +279 -128
  100. package/std/net/quic-stream.ts +466 -167
  101. package/std/net/quic.ts +584 -258
  102. package/std/net/router.ts +450 -99
  103. package/std/net/serve.ts +661 -240
  104. package/std/net/static.ts +1458 -0
  105. package/std/net/tls/client.ts +140 -45
  106. package/std/net/tls/codec.ts +510 -260
  107. package/std/net/tls/record-client.ts +268 -76
  108. package/std/net/tls/record-server.ts +236 -79
  109. package/std/net/tls/record.ts +173 -47
  110. package/std/net/tls/schedule.ts +58 -39
  111. package/std/net/tls/ticket.ts +295 -103
  112. package/std/net/tls/verify.ts +1 -1
  113. package/std/net/tls-tcp.ts +218 -75
  114. package/std/net/tls.ts +260 -152
  115. package/std/net/upstream.ts +283 -86
  116. package/std/net/url.ts +277 -102
  117. package/std/net/websocket.ts +532 -209
  118. package/std/net/webtransport.ts +755 -436
  119. package/std/regex.ts +1659 -829
  120. package/std/sqlite.ts +325 -0
  121. package/std/sync/ring.ts +320 -156
  122. package/std/sync/shm.ts +53 -6
  123. package/std/text.ts +285 -135
  124. package/std/threads.ts +22 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/nish-x86_64-linux",
3
- "version": "0.42.0",
3
+ "version": "0.45.1",
4
4
  "description": "The nish native compiler for Linux on x86_64",
5
5
  "license": "MIT",
6
6
  "os": [
@@ -250,7 +250,9 @@ export function convert(x, from, to) {
250
250
  // The hot paths of u64 arithmetic written over i64 (`toU64(a) * toU64(b)`)
251
251
  // stay in a function small enough for V8 to inline at the call: the two
252
252
  // 64-bit kinds reinterpret each other's bits, and a zext of an unsigned
253
- // 32-bit-or-narrower value to u64 keeps the value as it is.
253
+ // 32-bit-or-narrower value to u64 keeps the value as it is. Keep it this
254
+ // small: answering the integer `number` cases here, inline or through a
255
+ // helper, made ct_asm_x25519 under --emit-ts 1.1x to 1.8x slower.
254
256
  if (typeof x === "bigint") {
255
257
  if (to === "u64") return BigInt.asUintN(64, x);
256
258
  if (to === "i64") return BigInt.asIntN(64, x);
@@ -11,31 +11,65 @@ import { panicIndex, panicSlice, toIndex } from "./checks.mjs";
11
11
  const ENCODER = new TextEncoder();
12
12
  const DECODER = new TextDecoder("utf-8", { ignoreBOM: true });
13
13
 
14
- /** The string `bytesOf` encoded last, and its bytes. */
15
- let lastString = "";
16
- let lastBytes = new Uint8Array(0);
14
+ /** A UTF-16 unit that is not ASCII, so its UTF-8 bytes are not its units. */
15
+ const NON_ASCII = /[^\x00-\x7f]/;
17
16
 
18
17
  /**
19
- * The UTF-8 bytes of `s`, which is what an Nish string holds. The last string
20
- * encoded is kept with its bytes, so a loop of `s.charCodeAt(i)` over one
21
- * string encodes it once rather than once per byte. Nothing here writes to
18
+ * How many strings `bytesOf` keeps. A loop alternates between strings: std's
19
+ * `indexOfAnyFrom` and `yamlByte` read a byte of the document, then of a
20
+ * needle or a word, per step, so a cache of one encodes the document on every
21
+ * read. Eight covers the most std's loops keep live at once.
22
+ */
23
+ const SLOTS = 8;
24
+
25
+ /**
26
+ * The strings `bytesOf` saw last, most recent first, and their bytes: `null`
27
+ * for an ASCII string, whose units are its bytes and which is never encoded.
28
+ * A hit moves its entry to the front and a miss drops the last one, so a
29
+ * string stays while it is read and leaves when it is not. A lookup compares
30
+ * at most `SLOTS` strings with `===`, which answers at once for the same
31
+ * string or a different length and otherwise stops at the first unit that
32
+ * differs.
33
+ */
34
+ const keys = new Array(SLOTS).fill("");
35
+ const values = new Array(SLOTS).fill(null);
36
+ let used = 0;
37
+
38
+ /**
39
+ * The UTF-8 bytes of `s`, which is what an Nish string holds, or `null` when
40
+ * `s` is ASCII and its UTF-16 units are those bytes. Nothing here writes to
22
41
  * the bytes it is handed.
23
42
  */
24
43
  function bytesOf(s) {
25
- if (s !== lastString) {
26
- lastBytes = ENCODER.encode(s);
27
- lastString = s;
44
+ if (keys[0] === s) return values[0];
45
+ let at = 1;
46
+ while (at < used && keys[at] !== s) at++;
47
+ let bytes;
48
+ if (at < used) {
49
+ bytes = values[at];
50
+ } else {
51
+ bytes = NON_ASCII.test(s) ? ENCODER.encode(s) : null;
52
+ if (used < SLOTS) used++;
53
+ at = used - 1;
54
+ }
55
+ for (; at > 0; at--) {
56
+ keys[at] = keys[at - 1];
57
+ values[at] = values[at - 1];
28
58
  }
29
- return lastBytes;
59
+ keys[0] = s;
60
+ values[0] = bytes;
61
+ return bytes;
30
62
  }
31
63
 
32
64
  /**
33
65
  * `s.length`: UTF-8 byte length for strings (arrays keep their own `.length`).
34
- * The engine's encoder outruns a count in JavaScript, and the loop condition
35
- * `i < s.length` then leaves the bytes for the `s.charCodeAt(i)` it guards.
66
+ * The loop condition `i < s.length` then leaves the bytes for the
67
+ * `s.charCodeAt(i)` it guards.
36
68
  */
37
69
  export function strLen(x) {
38
- return typeof x === "string" ? bytesOf(x).length : x.length;
70
+ if (typeof x !== "string") return x.length;
71
+ const bytes = bytesOf(x);
72
+ return bytes === null ? x.length : bytes.length;
39
73
  }
40
74
 
41
75
  /**
@@ -52,6 +86,10 @@ export function strLenOpt(x) {
52
86
  export function charCodeAt(s, i) {
53
87
  const bytes = bytesOf(s);
54
88
  const k = toIndex(i);
89
+ if (bytes === null) {
90
+ if (!(k >= 0 && k < s.length)) panicIndex(k, s.length);
91
+ return s.charCodeAt(k);
92
+ }
55
93
  if (!(k >= 0 && k < bytes.length)) panicIndex(k, bytes.length);
56
94
  return bytes[k];
57
95
  }
@@ -59,9 +97,13 @@ export function charCodeAt(s, i) {
59
97
  /** `s.substring(a, b)`: JavaScript's clamp and swap, over byte offsets. */
60
98
  export function substring(s, a, b) {
61
99
  const bytes = bytesOf(s);
62
- const clamp = (v) => Math.max(0, Math.min(toIndex(v), bytes.length));
100
+ const len = bytes === null ? s.length : bytes.length;
101
+ const clamp = (v) => Math.max(0, Math.min(toIndex(v), len));
63
102
  const from = clamp(a);
64
- const to = b === undefined ? bytes.length : clamp(b);
103
+ const to = b === undefined ? len : clamp(b);
104
+ // A NaN index cuts nothing, as native's does; JavaScript's would read it as 0.
105
+ if (from !== from || to !== to) return "";
106
+ if (bytes === null) return s.substring(from, to);
65
107
  return DECODER.decode(bytes.subarray(Math.min(from, to), Math.max(from, to)));
66
108
  }
67
109
 
@@ -73,17 +115,22 @@ export function substring(s, a, b) {
73
115
  */
74
116
  export function slice(s, a, b) {
75
117
  const bytes = bytesOf(s);
118
+ const len = bytes === null ? s.length : bytes.length;
76
119
  const from = toIndex(a);
77
- const to = b === undefined ? bytes.length : toIndex(b);
78
- if (!(from >= 0 && from <= to && to <= bytes.length)) panicSlice(from, to, bytes.length);
79
- return DECODER.decode(bytes.subarray(from, to));
120
+ const to = b === undefined ? len : toIndex(b);
121
+ if (!(from >= 0 && from <= to && to <= len)) panicSlice(from, to, len);
122
+ return bytes === null ? s.slice(from, to) : DECODER.decode(bytes.subarray(from, to));
80
123
  }
81
124
 
82
- /** Whether `needle` sits in `bytes` at byte offset `at`. */
83
- function bytesAt(bytes, at, needle) {
84
- if (!(at >= 0 && at + needle.length <= bytes.length)) return false;
85
- for (let j = 0; j < needle.length; j++) {
86
- if (bytes[at + j] !== needle[j]) return false;
125
+ /**
126
+ * Whether `sub` sits in `bytes` at byte offset `at`; `needle` is `sub`'s
127
+ * bytes, or `null` when `sub` is ASCII and its units are compared instead.
128
+ */
129
+ function bytesAt(bytes, at, sub, needle) {
130
+ const n = needle === null ? sub.length : needle.length;
131
+ if (!(at >= 0 && at + n <= bytes.length)) return false;
132
+ for (let j = 0; j < n; j++) {
133
+ if (bytes[at + j] !== (needle === null ? sub.charCodeAt(j) : needle[j])) return false;
87
134
  }
88
135
  return true;
89
136
  }
@@ -92,35 +139,40 @@ function bytesAt(bytes, at, needle) {
92
139
  * `s.indexOf(sub, from)`: the first *byte* offset at or after `from`, or -1.
93
140
  * `from` is converted as JavaScript's `ToIntegerOrInfinity` converts it (NaN
94
141
  * is 0) and clamped into `[0, len]`, so an empty `sub` is found at `from`.
95
- * The engine's `indexOf` over the bytes finds each candidate for `sub`'s
96
- * first byte, and only those are compared in full.
142
+ * In an ASCII `s` the engine's own `indexOf` answers: offsets are bytes there,
143
+ * and a `sub` that is not ASCII is in it nowhere, as none of its bytes are.
144
+ * Otherwise the engine's `indexOf` over the bytes finds each candidate for
145
+ * `sub`'s first byte, and only those are compared in full.
97
146
  */
98
147
  export function indexOf(s, sub, from) {
99
148
  const haystack = bytesOf(s);
149
+ const len = haystack === null ? s.length : haystack.length;
100
150
  const at = from === undefined ? 0 : toIndex(from);
101
- const start = Number.isNaN(at) ? 0 : Math.max(0, Math.min(at, haystack.length));
151
+ const start = Number.isNaN(at) ? 0 : Math.max(0, Math.min(at, len));
102
152
  if (sub.length === 0) return start;
103
- const needle = ENCODER.encode(sub);
104
- const last = haystack.length - needle.length;
105
- for (let i = haystack.indexOf(needle[0], start); i >= 0 && i <= last; i = haystack.indexOf(needle[0], i + 1)) {
106
- if (bytesAt(haystack, i, needle)) return i;
153
+ if (haystack === null) return s.indexOf(sub, start);
154
+ const needle = bytesOf(sub);
155
+ const first = needle === null ? sub.charCodeAt(0) : needle[0];
156
+ const last = len - (needle === null ? sub.length : needle.length);
157
+ for (let i = haystack.indexOf(first, start); i >= 0 && i <= last; i = haystack.indexOf(first, i + 1)) {
158
+ if (bytesAt(haystack, i, sub, needle)) return i;
107
159
  }
108
160
  return -1;
109
161
  }
110
162
 
111
- /** `nish_str_at`: whether `sub`'s bytes sit at byte offset `at`. */
112
- function occursAt(s, at, sub) {
113
- return bytesAt(bytesOf(s), at, ENCODER.encode(sub));
114
- }
115
-
163
+ /** `s.startsWith(sub)`: whether `sub`'s bytes begin `s`'s; an ASCII `s` asks the engine. */
116
164
  export function startsWith(s, sub) {
117
- return occursAt(s, 0, sub);
165
+ const bytes = bytesOf(s);
166
+ if (bytes === null) return s.startsWith(sub);
167
+ return bytesAt(bytes, 0, sub, bytesOf(sub));
118
168
  }
119
169
 
170
+ /** `s.endsWith(sub)`: whether `sub`'s bytes end `s`'s; an ASCII `s` asks the engine. */
120
171
  export function endsWith(s, sub) {
121
172
  const bytes = bytesOf(s);
122
- const needle = ENCODER.encode(sub);
123
- return bytesAt(bytes, bytes.length - needle.length, needle);
173
+ if (bytes === null) return s.endsWith(sub);
174
+ const needle = bytesOf(sub);
175
+ return bytesAt(bytes, bytes.length - (needle === null ? sub.length : needle.length), sub, needle);
124
176
  }
125
177
 
126
178
  /**
package/runtime/core.d.ts CHANGED
@@ -8,6 +8,22 @@ export * from "./core/numbers.js";
8
8
  export * from "./core/result.js";
9
9
  export * from "./core/strings.js";
10
10
 
11
+ // `nish:compact`: what `--emit-ts` writes for a program's `import ... from "nish:compact"`.
12
+
13
+ /**
14
+ * The marker a class implements to store its links as 32-bit offsets in a
15
+ * native build. It has no members, so `implements Compact` adds nothing a
16
+ * TypeScript checker sees.
17
+ */
18
+ export interface Compact {}
19
+ /** The value `core.mjs` exports under the marker's name, so an emitted import of it resolves. */
20
+ export declare const Compact: Compact;
21
+
22
+ /** Reserved for WP48 stage 3, which lets a class refuse the compiler's choice. */
23
+ export interface Wide {}
24
+ /** The value `core.mjs` exports under the reserved marker's name. */
25
+ export declare const Wide: Wide;
26
+
11
27
  // `nish:simd`: what `--emit-ts` writes for a program's `import ... from "nish:simd"`.
12
28
  import type { f32, f64, i32, u16, u32, u8 } from "./core/numbers.js";
13
29
 
@@ -131,3 +147,11 @@ export declare function asI32x4(v: SimdVector): i32x4;
131
147
  export declare function asU32x4(v: SimdVector): u32x4;
132
148
  export declare function asF32x4(v: SimdVector): f32x4;
133
149
  export declare function asF64x2(v: SimdVector): f64x2;
150
+
151
+ // `mulHigh` and `mulHighIsNative`: builtins, written out under these names.
152
+ import type { u64 } from "./core/numbers.js";
153
+
154
+ /** The high 64 bits of the 128-bit product of `a` and `b` (WP41): `(a * b) >> 64n`. */
155
+ export declare function mulHigh(a: u64, b: u64): u64;
156
+ /** `false` under Node, where `mulHigh` is a BigInt multiply rather than one instruction. */
157
+ export declare function mulHighIsNative(): boolean;
package/runtime/core.mjs CHANGED
@@ -84,6 +84,27 @@ const simdLoad = (Lanes) => (xs, i) => {
84
84
  return out;
85
85
  };
86
86
 
87
+ // `nish:compact`: the marker interfaces pick a native layout and mean nothing
88
+ // under Node, where every reference is the engine's own.
89
+ export const Compact = Object.freeze({});
90
+ export const Wide = Object.freeze({});
91
+
92
+ // `mulHigh` / `mulHighIsNative` (WP41). A `u64` is a BigInt here, so the high
93
+ // half is the exact product shifted down. A `number` operand is the mix the
94
+ // native checker refuses (NL2823), and throws rather than answering from a
95
+ // double. `mulHighIsNative` is `false`, so std keeps its narrow-limb code under
96
+ // Node, where `mulHigh` is a BigInt multiply and no faster than four products.
97
+ export function mulHigh(a, b) {
98
+ if (typeof a !== "bigint" || typeof b !== "bigint") {
99
+ throw new TypeError("mulHigh: takes u64 (BigInt) operands");
100
+ }
101
+ return (BigInt.asUintN(64, a) * BigInt.asUintN(64, b)) >> 64n;
102
+ }
103
+
104
+ export function mulHighIsNative() {
105
+ return false;
106
+ }
107
+
87
108
  export const u8x16 = simdMake(Uint8Array);
88
109
  export const u16x8 = simdMake(Uint16Array);
89
110
  export const i32x4 = simdMake(Int32Array);
package/runtime/nish.d.ts CHANGED
@@ -628,6 +628,36 @@ declare function ctSelect<T extends u32 | u64>(mask: T, a: T, b: T): T;
628
628
  * @returns a mask for `ctSelect`.
629
629
  */
630
630
  declare function ctEq<T extends u32 | u64>(a: T, b: T): T;
631
+ /**
632
+ * The high 64 bits of the 128-bit product of `a` and `b`, the half a `u64`
633
+ * `a * b` drops (WP41). Both operands are `u64`. Any other type is refused as
634
+ * `` `mulHigh` takes u64 operands (convert with toU64), got u32 `` (NL2823):
635
+ * `u32`, `i64`, `number` and a ranged integer included. A bare numeric
636
+ * literal is read as a `u64`, so `mulHigh(x, 3)` works, and one that does not
637
+ * fit is the literal's own refusal. The call takes exactly two arguments, and
638
+ * like every function it is not a value.
639
+ *
640
+ * On x86-64 and AArch64 it is one multiply (`mul`, `mulx` or `umulh`) and
641
+ * calls nothing. wasm32 has no wide multiply, so there it is four 32×32
642
+ * products and their carries, inline. Neither lowering branches on its
643
+ * operands. Under Node it is `(a * b) >> 64n` on BigInts, with the same
644
+ * answer. Capability: none.
645
+ *
646
+ * @param a one factor.
647
+ * @param b the other factor.
648
+ * @returns the top 64 bits of `a * b` computed in 128 bits.
649
+ */
650
+ declare function mulHigh(a: u64, b: u64): u64;
651
+ /**
652
+ * Whether `mulHigh` is one instruction on the build's target (WP41): `true` on
653
+ * x86-64 and AArch64, and `false` on wasm32 and under Node. It is a constant of
654
+ * the build, so a branch on it costs nothing and the path not taken is
655
+ * dropped. Code with a wide-limb algorithm and a narrow-limb one keeps both and
656
+ * picks with it. It takes no arguments. Capability: none.
657
+ *
658
+ * @returns `true` where the 64×64→128 product is native.
659
+ */
660
+ declare function mulHighIsNative(): boolean;
631
661
  /**
632
662
  * Every byte of `bytes` set to zero, in stores neither `-O2` nor `-flto` may
633
663
  * drop as dead (#385): how a key, a secret scalar or another secret is cleared
@@ -712,8 +742,8 @@ declare function isExecutableSync(path: string): boolean;
712
742
  // refusal reads `` `uncheckedGet` is a `nish:unsafe` function and is called only
713
743
  // through an import of it `` (NL2456). An import may rename (`as`) and cannot be
714
744
  // shadowed; a user function of the same name, with no import, is the user's.
715
- // Every import and call is a recorded fact in `--emit-checked`. Under
716
- // `--deny-panics` or `"nish".noPanic` the import is the opt-in the scope takes.
745
+ // Every import and call is a recorded fact in `--emit-checked`. In
746
+ // the no-panic scope, the default, the import is the opt-in the scope takes.
717
747
 
718
748
  /**
719
749
  * `xs[i]` with no bounds check: undefined behaviour when `i` is out of range,
@@ -726,7 +756,7 @@ declare function isExecutableSync(path: string): boolean;
726
756
  *
727
757
  * To the bounds proof it is the load `xs[i]` is, less the check: it moves no
728
758
  * length, and it records no passed check, so a checked `xs[i]` after it keeps
729
- * its check. Under `--deny-panics` it is an allowed `unchecked` site. Under
759
+ * its check. In the no-panic scope it is an allowed `unchecked` site. Under
730
760
  * Node the runtime shim reads `xs[i]`. Capability: unsafe.
731
761
  *
732
762
  * @param xs the array of numbers to read.
@@ -746,7 +776,7 @@ declare function uncheckedGet<T extends number>(xs: readonly T[], i: i32): T;
746
776
  * The store is a write of the array for every rule that asks, the rule for a
747
777
  * parallel body included. To the bounds proof it is the store `xs[i] = v` is,
748
778
  * less the check, and a resize in the value stored still takes the length away.
749
- * Under `--deny-panics` it is an allowed `unchecked` site. Under Node the
779
+ * In the no-panic scope it is an allowed `unchecked` site. Under Node the
750
780
  * runtime shim stores `xs[i] = v`. Capability: unsafe.
751
781
  *
752
782
  * @param xs the array of numbers to write.
@@ -766,7 +796,7 @@ declare function uncheckedSet<T extends number>(xs: T[], i: i32, v: T): void;
766
796
  *
767
797
  * In a module constant's initialiser the call folds, wrapping, where
768
798
  * `2147483647 + 1` is refused; it is the one call such an initialiser may make.
769
- * It is not a site under `--deny-panics`, as it cannot panic. Under Node the
799
+ * It is not a site in the no-panic scope, as it cannot panic. Under Node the
770
800
  * runtime shim computes `(a + b) | 0` on an `i32` and `BigInt.asIntN(64, ...)`
771
801
  * on the BigInt an `i64` is there. Capability: unsafe.
772
802
  *
@@ -813,7 +843,7 @@ declare function wrappingMul<T extends i32 | i64>(a: T, b: T): T;
813
843
  * The copy is new on every call and is the calling function's allocation, and
814
844
  * it is refused inside a `using a = arena()` block that would release one held
815
845
  * outside it (`` `readCString(p)` may be allocated inside the `using arena()`
816
- * block ``). Under `--deny-panics` its `NULL` test is an `ffi-null` site no
846
+ * block ``). In the no-panic scope its `NULL` test is an `ffi-null` site no
817
847
  * proof removes, so the scope refuses the call (the copy's `oom` beside it is
818
848
  * allowed), and the pointer it reads is `ffi`. It lowers the same
819
849
  * on wasm32, where whether the program links is the `declare function`'s
@@ -842,7 +872,7 @@ declare function readCString(p: CPtr): string;
842
872
  *
843
873
  * A `NULL` `p` is tested before the copy and panics with the `ffi-null` kind,
844
874
  * naming the layout, since a `declare function` declared to answer `CPtr` may
845
- * still answer `NULL`. No proof removes that test, so `--deny-panics` refuses
875
+ * still answer `NULL`. No proof removes that test, so the no-panic scope refuses
846
876
  * the call; `--emit-panics` lists it as an `ffi-null` site, with the copy's
847
877
  * `oom` beside it.
848
878
  *
@@ -1737,9 +1767,14 @@ declare function netWrite2(
1737
1767
  * Shut one or both directions of a connection. Not callable in a `scope()` task or a parallel body.
1738
1768
  * Refused at compile time on wasm32 and the WASI profile (NL2404). Capability: net.
1739
1769
  *
1770
+ * On a connection a `NET_LISTEN_RING` listener accepted, the shutdown goes behind the
1771
+ * bytes already written, so the peer reads them and then the end of the stream; a later
1772
+ * `netWrite` answers -32 once the write side is shut, and `netRead` answers what is queued
1773
+ * and then 0 once the read side is.
1774
+ *
1740
1775
  * @param fd the socket.
1741
1776
  * @param how the read side (0), the write side (1) or both (2).
1742
- * @returns 0, or a negative errno; -22 for any other `how`.
1777
+ * @returns 0, or a negative errno; -22 for any other `how`, -9 for a closed ring connection.
1743
1778
  */
1744
1779
  declare function netShutdown(fd: i32, how: i32): i32;
1745
1780
  /**
@@ -2006,7 +2041,8 @@ declare function netSetOption(fd: i32, option: i32, value: i32): i32;
2006
2041
  * @param fd the socket.
2007
2042
  * @returns the MSS; -1, never an errno, for anything else (a descriptor that is not open, a
2008
2043
  * socket with no peer such as a listener or one never connected, though the kernel answers
2009
- * its default `536` for those, a socket that is not TCP), and always on wasm.
2044
+ * its default `536` for those, a socket that is not TCP, a connection a `NET_LISTEN_RING`
2045
+ * listener accepted, whose ring cannot ask for it), and always on wasm.
2010
2046
  */
2011
2047
  declare function netMss(fd: i32): i32;
2012
2048
  /**
@@ -3246,11 +3282,14 @@ declare module "nish:net" {
3246
3282
  bLen: i32
3247
3283
  ): i32;
3248
3284
  /**
3249
- * Shut one side of a connection, or both.
3285
+ * Shut one side of a connection, or both. On a connection a
3286
+ * `NET_LISTEN_RING` listener accepted, the shutdown goes behind the bytes
3287
+ * already written, so the peer reads them and then the end of the stream.
3250
3288
  *
3251
3289
  * @param fd A connected socket.
3252
3290
  * @param how `0` the read side, `1` the write side, `2` both.
3253
- * @returns `0`, `-22` for any other `how`, or the failure.
3291
+ * @returns `0`, `-22` for any other `how`, `-9` for a closed ring
3292
+ * connection, or the failure.
3254
3293
  */
3255
3294
  export function netShutdown(fd: i32, how: i32): i32;
3256
3295
  /**
@@ -3445,8 +3484,9 @@ declare module "nish:net" {
3445
3484
  * @returns The size, or a plain `-1`, never an errno, for anything else: a
3446
3485
  * descriptor that is not open, a socket with no peer (a listener, or one
3447
3486
  * never connected, though the kernel answers its default `536` for
3448
- * those), a socket that is not TCP, and every call on wasm, where it
3449
- * compiles to the constant with the descriptor still evaluated. It throws
3487
+ * those), a socket that is not TCP, a connection a `NET_LISTEN_RING`
3488
+ * listener accepted, whose ring cannot ask for it, and every call on
3489
+ * wasm, where it compiles to the constant with the descriptor still evaluated. It throws
3450
3490
  * under Node.
3451
3491
  */
3452
3492
  export function netMss(fd: i32): i32;
@@ -3642,7 +3682,7 @@ declare module "nish:net" {
3642
3682
  * `(a - b) | 0` and `Math.imul` on an `i32` and `BigInt.asIntN(64, …)` on an
3643
3683
  * `i64`. A program that stays in range prints the same as natively.
3644
3684
  *
3645
- * **The no-panic scope.** Under `--deny-panics` or `"nish".noPanic`,
3685
+ * **The no-panic scope.** In the scope, every module of the package by default,
3646
3686
  * `uncheckedGet` and `uncheckedSet` are allowed `unchecked` sites, because
3647
3687
  * the import has already said that an index out of range is the author's to
3648
3688
  * rule out. `readCString` and `readStruct` are not: each tests its pointer and
@@ -3742,6 +3782,52 @@ declare module "nish:unsafe" {
3742
3782
  export function cErrno(): i32;
3743
3783
  }
3744
3784
 
3785
+ /**
3786
+ * `nish:compact`: the marker interfaces that pick a class's layout (WP48).
3787
+ * `class R implements Compact` stores every field typed exactly `R` or
3788
+ * `R | null`, in any class, as a 32-bit offset into one reserved address range,
3789
+ * so a tree or list node of an `i32` and two links is 12 bytes, not 24. Locals,
3790
+ * parameters, returns, arrays, maps and other unions stay full pointers.
3791
+ *
3792
+ * A native `--link` build also compacts, with no marker, each class whose
3793
+ * links take at least a quarter off a class made in a loop or recursion
3794
+ * (`--explain-layout` prints each pick and why the others are left alone).
3795
+ * `implements Wide` keeps one class out of that, and `--compact off` keeps
3796
+ * every class, `implements Compact` included, at full pointers.
3797
+ *
3798
+ * A `--emit-header` build keeps a compact class, and every class holding one
3799
+ * of its links, opaque: C holds it as a full `struct X *` handle and links the
3800
+ * range runtime (`scripts/build.sh --arena-range`). The compiler refuses a
3801
+ * compact class where Node or the runtime would read its layout: a build with
3802
+ * `--emit-napi` or `--emit-napi-async` (also beside `--emit-header`), an
3803
+ * N-API export whose signature holds one, a `declare class`, and a class
3804
+ * holding a `Secret`. wasm32, a host `--emit-dts` page module and an ASAN build compile
3805
+ * it uncompressed: a pointer there is already 4 bytes, the page runs on the
3806
+ * wasm runtime, or the range cannot hold the sanitizer's blocks.
3807
+ *
3808
+ * **Under Node.** `runtime/nish.mjs` answers the import with
3809
+ * `runtime/core.mjs`, where both names are empty values: a reference is the
3810
+ * engine's own and the marker changes nothing a program prints.
3811
+ *
3812
+ * Rules: https://github.com/amritk/nish/blob/main/docs/LANGUAGE.md#compact-classes
3813
+ */
3814
+ declare module "nish:compact" {
3815
+ /**
3816
+ * The marker a class implements to store its links as 32-bit offsets. It
3817
+ * has no members, so `implements Compact` adds no field.
3818
+ */
3819
+ export interface Compact {}
3820
+
3821
+ /**
3822
+ * The marker a class implements to keep its links full pointers. The
3823
+ * compiler compacts a class on its own when its rule finds the saving pays
3824
+ * (`--explain-layout` names each pick); `implements Wide` refuses that
3825
+ * pick, the way `--compact off` refuses every one. It has no members, so it
3826
+ * adds no field, and a class cannot write it with `Compact`.
3827
+ */
3828
+ export interface Wide {}
3829
+ }
3830
+
3745
3831
  /**
3746
3832
  * Sixteen-byte vectors of one lane type, and the lane-wise operations on them,
3747
3833
  * each lowered to target-independent LLVM vector IR.
@@ -4999,15 +5085,15 @@ declare module "nish:region" {
4999
5085
  *
5000
5086
  * import { awaitAll, spawn } from "nish:async";
5001
5087
  *
5002
- * async function double(x: i32): Promise<i32> {
5088
+ * const double = async (x: i32): Promise<i32> => {
5003
5089
  * return x * 2;
5004
5090
  * }
5005
5091
  *
5006
- * async function log(x: i32): Promise<void> {
5092
+ * const log = async (x: i32): Promise<void> => {
5007
5093
  * console.log(x);
5008
5094
  * }
5009
5095
  *
5010
- * async function run(): Promise<void> {
5096
+ * const run = async (): Promise<void> => {
5011
5097
  * const [c, d] = await awaitAll(double(1), double(2));
5012
5098
  * spawn(log(c + d));
5013
5099
  * }
@@ -5024,7 +5110,7 @@ declare module "nish:region" {
5024
5110
  *
5025
5111
  * import { readable, sleep } from "nish:async";
5026
5112
  *
5027
- * async function tick(): Promise<void> {
5113
+ * const tick = async (): Promise<void> => {
5028
5114
  * await sleep(10);
5029
5115
  * console.log("tick");
5030
5116
  * }
@@ -5272,6 +5358,18 @@ declare module "nish:async" {
5272
5358
  * (or removed); `false` for a task that has finished or is `main`.
5273
5359
  */
5274
5360
  export function cancelAfter(task: i64, ms: i32): boolean;
5361
+ /**
5362
+ * Read `view`, an object or array its spawner owns, in place, without
5363
+ * copying it into the task's region. Inside a spawned task the view, and
5364
+ * what is built from it, is lent only while the task runs: the task may
5365
+ * read it, write numbers into it and copy out of it, but may not store a
5366
+ * pointer into it, grow it, or keep it, its parts or what holds it past its
5367
+ * end (NL2822). Outside a task, and under `--emit-ts`, it is the identity.
5368
+ *
5369
+ * @param view The spawner's object or array the task reads.
5370
+ * @returns `view` itself.
5371
+ */
5372
+ export function lend<T>(view: T): T;
5275
5373
  }
5276
5374
 
5277
5375
  // ---- Arena (docs/LANGUAGE.md -> Arena) ---------------------------------------
@@ -5871,6 +5969,22 @@ interface Array<T> {
5871
5969
  * @returns the first such index, or `-1`.
5872
5970
  */
5873
5971
  findIndex(predicate: (value: T, index: number) => boolean): number;
5972
+ /**
5973
+ * Call `callbackfn` with each element in order, as `Array.prototype.forEach`
5974
+ * does, up to the length the array had when the call began: an element the
5975
+ * callback pushes is not visited, and one that shrinks the array ends the
5976
+ * walk at its new length. The call answers `void`, and so does the
5977
+ * callback; what a concise arrow body computes is dropped. The callback takes the element, or the element and its
5978
+ * index; one that declares a third parameter, JavaScript's array, is refused
5979
+ * (NL2698). It is `map`'s callback otherwise: a top-level function or an arrow
5980
+ * written at the call, which may not assign a name of the function around it
5981
+ * (NL2586), so a running total is a `for...of` loop. The call compiles to a
5982
+ * counted loop with the callback inlined (`std/array`'s `forEach`, or
5983
+ * `forEachIndexed` for a two-parameter callback). Capability: none.
5984
+ *
5985
+ * @param callbackfn called once per element, with the element and its index.
5986
+ */
5987
+ forEach(callbackfn: (value: T, index: number) => void): void;
5874
5988
  /**
5875
5989
  * A copy of the elements from `start` up to `end`, in a new array, with
5876
5990
  * string `slice`'s rule: the range must satisfy `0 <= start <= end <=
@@ -5895,7 +6009,7 @@ interface Array<T> {
5895
6009
  * nish_array *`.
5896
6010
  *
5897
6011
  * Everything that reads works (`length`, `a[i]`, `for...of`, `indexOf`, `join`,
5898
- * `map`, `filter`, `some`, `every`, `findIndex`, `slice`, passing it on to
6012
+ * `map`, `filter`, `some`, `every`, `findIndex`, `forEach`, `slice`, passing it on to
5899
6013
  * another `readonly T[]`); every write is refused under its own name, an
5900
6014
  * element store ("Cannot assign to an element of readonly i32[]
5901
6015
  * (declare it i32[] to write through it)"), `push`, `pop`, `set`, `fill` and
@@ -5981,6 +6095,22 @@ interface ReadonlyArray<T> {
5981
6095
  * @returns the first such index, or `-1`.
5982
6096
  */
5983
6097
  findIndex(predicate: (value: T, index: number) => boolean): number;
6098
+ /**
6099
+ * Call `callbackfn` with each element in order, as `Array.prototype.forEach`
6100
+ * does, up to the length the array had when the call began: an element the
6101
+ * callback pushes is not visited, and one that shrinks the array ends the
6102
+ * walk at its new length. The call answers `void`, and so does the
6103
+ * callback; what a concise arrow body computes is dropped. The callback takes the element, or the element and its
6104
+ * index; one that declares a third parameter, JavaScript's array, is refused
6105
+ * (NL2698). It is `map`'s callback otherwise: a top-level function or an arrow
6106
+ * written at the call, which may not assign a name of the function around it
6107
+ * (NL2586), so a running total is a `for...of` loop. The call compiles to a
6108
+ * counted loop with the callback inlined (`std/array`'s `forEach`, or
6109
+ * `forEachIndexed` for a two-parameter callback). Capability: none.
6110
+ *
6111
+ * @param callbackfn called once per element, with the element and its index.
6112
+ */
6113
+ forEach(callbackfn: (value: T, index: number) => void): void;
5984
6114
  /**
5985
6115
  * A copy of the elements from `start` up to `end`, in a new array, with
5986
6116
  * string `slice`'s rule: the range must satisfy `0 <= start <= end <=
@@ -6139,9 +6269,9 @@ interface StringConstructor {
6139
6269
  * reached, visits a key set during the walk later in the same walk, and keeps a
6140
6270
  * re-set key in its place, as JavaScript's does.
6141
6271
  *
6142
- * At most 2^24 − 1 entries (one fewer than Node's 2^24); one more panics with
6143
- * `Map maximum size exceeded`, after first compacting away dead entries (not
6144
- * while a loop walks the table). String keys are hashed with SipHash-1-3 under
6272
+ * No entry cap of its own: past 2^29 entries the bucket table would outgrow
6273
+ * the largest array, and the program stops as out of memory, as any
6274
+ * allocation that size does (Node's `Map` throws at 2^24). String keys are hashed with SipHash-1-3 under
6145
6275
  * a per-process random key drawn from the system's entropy source before
6146
6276
  * `main`, and an integer, `boolean` or enum key under a per-process random
6147
6277
  * multiply-add-shift; if the entropy source fails the program panics before
@@ -6213,8 +6343,8 @@ interface Map<K, V> {
6213
6343
  * `new Set<T>()` takes no arguments ("`new Set<string>` takes no arguments in
6214
6344
  * this version"). `keys()` and `values()` are one walk, as JavaScript's are,
6215
6345
  * admitted only as the iterable of a `for...of`, which also takes the set
6216
- * itself: `for (const x of s)`. At most 2^24 − 1 members; one more panics with
6217
- * `Set maximum size exceeded`. Hashing, the entropy source and the walk's
6346
+ * itself: `for (const x of s)`. Its size is bounded as a `Map`'s is, by
6347
+ * memory alone. Hashing, the entropy source and the walk's
6218
6348
  * rules are `Map`'s. `add` and `has` are one probe each, and `if (!s.has(x)) {
6219
6349
  * s.add(x); ... }` with the `add` first in the branch asks about the member
6220
6350
  * once.
@@ -6247,6 +6377,19 @@ interface Set<T> {
6247
6377
  delete(value: T): boolean;
6248
6378
  /** Remove every member. Capability: none. */
6249
6379
  clear(): void;
6380
+ /**
6381
+ * Call `callbackfn` with each member in insertion order, as
6382
+ * `Set.prototype.forEach` does: the `for...of` walk over the set with the
6383
+ * callback inlined (`std/collections`' `forEachElement`), so a member added
6384
+ * during the walk is visited and one deleted before the walk reaches it is
6385
+ * not. The callback takes the member alone; JavaScript passes it again as a
6386
+ * second argument and the set as a third, and a callback that declares a
6387
+ * second parameter is refused (NL2772). The callback rules are those of an
6388
+ * array's `forEach`. Capability: none.
6389
+ *
6390
+ * @param callbackfn called once per member, with the member.
6391
+ */
6392
+ forEach(callbackfn: (value: T) => void): void;
6250
6393
  }
6251
6394
 
6252
6395
  // ---- What this file cannot say ----------------------------------------------
package/runtime/nish.mjs CHANGED
@@ -251,6 +251,10 @@ provide("f32ToBits", shim.f32ToBits);
251
251
  // agree with a native run; the timing does not, and is not claimed here.
252
252
  provide("ctSelect", shim.ctSelect);
253
253
  provide("ctEq", shim.ctEq);
254
+ // `mulHigh` (WP41): exact on BigInts. `mulHighIsNative` is `false`, so std
255
+ // takes its narrow-limb path here.
256
+ provide("mulHigh", shim.mulHigh);
257
+ provide("mulHighIsNative", shim.mulHighIsNative);
254
258
  // `secureZero` (#385): the bytes are zero afterwards here too, though what a
255
259
  // collector may have copied before is out of reach.
256
260
  provide("secureZero", shim.secureZero);
@@ -345,6 +349,8 @@ provide("arena", shim.arena);
345
349
  // `nish:simd` is `runtime/core.mjs`, whose vectors are typed arrays made once
346
350
  // and never written after, so `--emit-ts` output and a program run as it was
347
351
  // written share one implementation.
352
+ // `nish:compact` is `runtime/core.mjs` too: its two marker interfaces pick a
353
+ // native layout and are empty values under Node.
348
354
  const UNSAFE_EXPORTS = ["uncheckedGet", "uncheckedSet", "wrappingAdd", "wrappingSub", "wrappingMul"];
349
355
  const unsafeSource = UNSAFE_EXPORTS.map((name) => `export const ${name} = globalThis.${name};`).join("\n");
350
356
  // `nish:region` has no allocator to swap under Node: a region is an entry in a
@@ -442,7 +448,7 @@ registerHooks({
442
448
  if (specifier === "nish:unsafe") {
443
449
  return { url: `data:text/javascript,${encodeURIComponent(unsafeSource)}`, shortCircuit: true };
444
450
  }
445
- if (specifier === "nish:simd") {
451
+ if (specifier === "nish:simd" || specifier === "nish:compact") {
446
452
  return next(new URL("./core.mjs", import.meta.url).href, context);
447
453
  }
448
454
  if (specifier === "nish:async") {