nish 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 (125) hide show
  1. package/README.md +11 -5
  2. package/docs/AI.md +287 -129
  3. package/package.json +9 -9
  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/skills/writing-nish/SKILL.md +12 -4
  21. package/std/README.md +6 -6
  22. package/std/array.ts +10 -2
  23. package/std/bytes.ts +239 -26
  24. package/std/cli-args.ts +1072 -606
  25. package/std/cli-flags.ts +180 -98
  26. package/std/cli.ts +21 -13
  27. package/std/collections.ts +245 -127
  28. package/std/compress/checksum.ts +32 -37
  29. package/std/compress/deflate.ts +1102 -542
  30. package/std/compress/gzip.ts +290 -145
  31. package/std/compress/zlib.ts +57 -21
  32. package/std/crypto/aes.ts +153 -75
  33. package/std/crypto/base64url.ts +163 -39
  34. package/std/crypto/bcrypt-batch.ts +164 -90
  35. package/std/crypto/bcrypt.ts +185 -107
  36. package/std/crypto/chacha20poly1305.ts +84 -68
  37. package/std/crypto/ct.ts +9 -7
  38. package/std/crypto/ed25519.ts +64 -27
  39. package/std/crypto/hkdf.ts +105 -63
  40. package/std/crypto/hmac.ts +95 -89
  41. package/std/crypto/jwt.ts +248 -126
  42. package/std/crypto/p256.ts +758 -444
  43. package/std/crypto/p384.ts +20 -13
  44. package/std/crypto/pki-roots.ts +95 -38
  45. package/std/crypto/pki.ts +164 -60
  46. package/std/crypto/rsa.ts +266 -223
  47. package/std/crypto/scram.ts +141 -0
  48. package/std/crypto/sha1.ts +157 -57
  49. package/std/crypto/sha256.ts +502 -422
  50. package/std/crypto/sha512.ts +204 -135
  51. package/std/crypto/x509-random.ts +9 -3
  52. package/std/crypto/x509.ts +331 -171
  53. package/std/host/ring.ts +1 -1
  54. package/std/host/text.ts +1 -1
  55. package/std/json-equal.ts +35 -19
  56. package/std/json-format.ts +215 -108
  57. package/std/json-number.ts +126 -72
  58. package/std/json-pointer.ts +42 -13
  59. package/std/json-tokenizer.ts +498 -307
  60. package/std/json-typed.ts +115 -36
  61. package/std/json-value.ts +341 -152
  62. package/std/json.ts +300 -158
  63. package/std/net/buffer-pool.ts +17 -7
  64. package/std/net/dns-config.ts +427 -236
  65. package/std/net/dns-message.ts +114 -47
  66. package/std/net/dns-resolver.ts +736 -237
  67. package/std/net/dns.ts +205 -52
  68. package/std/net/fetch.ts +320 -184
  69. package/std/net/grpc.ts +948 -0
  70. package/std/net/handler.ts +1276 -218
  71. package/std/net/hpack.ts +395 -180
  72. package/std/net/http-compress.ts +502 -65
  73. package/std/net/http-fields.ts +85 -49
  74. package/std/net/http-server.ts +1764 -603
  75. package/std/net/http1-client-codec.ts +372 -161
  76. package/std/net/http1-codec.ts +665 -331
  77. package/std/net/http1-server.ts +881 -269
  78. package/std/net/http2-cleartext.ts +165 -95
  79. package/std/net/http2-frame.ts +232 -89
  80. package/std/net/http2-tls.ts +42 -12
  81. package/std/net/http2.ts +327 -101
  82. package/std/net/http3-frame.ts +68 -33
  83. package/std/net/http3-server.ts +672 -206
  84. package/std/net/http3.ts +669 -191
  85. package/std/net/oidc.ts +63 -24
  86. package/std/net/outbound.ts +207 -142
  87. package/std/net/postgres-async.ts +1223 -470
  88. package/std/net/postgres-carrier.ts +575 -372
  89. package/std/net/postgres.ts +1521 -632
  90. package/std/net/prefork.ts +254 -98
  91. package/std/net/protobuf.ts +1017 -0
  92. package/std/net/qpack.ts +143 -92
  93. package/std/net/quic-conn-ack.ts +104 -22
  94. package/std/net/quic-conn-cid.ts +66 -26
  95. package/std/net/quic-conn-params.ts +13 -7
  96. package/std/net/quic-datagram.ts +90 -27
  97. package/std/net/quic-frame.ts +324 -121
  98. package/std/net/quic-listener.ts +206 -91
  99. package/std/net/quic-packet.ts +289 -163
  100. package/std/net/quic-recovery.ts +279 -128
  101. package/std/net/quic-stream.ts +466 -167
  102. package/std/net/quic.ts +584 -258
  103. package/std/net/router.ts +450 -99
  104. package/std/net/serve.ts +661 -240
  105. package/std/net/static.ts +1458 -0
  106. package/std/net/tls/client.ts +140 -45
  107. package/std/net/tls/codec.ts +510 -260
  108. package/std/net/tls/record-client.ts +268 -76
  109. package/std/net/tls/record-server.ts +236 -79
  110. package/std/net/tls/record.ts +173 -47
  111. package/std/net/tls/schedule.ts +58 -39
  112. package/std/net/tls/ticket.ts +295 -103
  113. package/std/net/tls/verify.ts +1 -1
  114. package/std/net/tls-tcp.ts +218 -75
  115. package/std/net/tls.ts +260 -152
  116. package/std/net/upstream.ts +283 -86
  117. package/std/net/url.ts +277 -102
  118. package/std/net/websocket.ts +532 -209
  119. package/std/net/webtransport.ts +755 -436
  120. package/std/regex.ts +1659 -829
  121. package/std/sqlite.ts +325 -0
  122. package/std/sync/ring.ts +320 -156
  123. package/std/sync/shm.ts +53 -6
  124. package/std/text.ts +285 -135
  125. package/std/threads.ts +22 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nish",
3
- "version": "0.42.0",
3
+ "version": "0.45.1",
4
4
  "description": "Nish: an ahead-of-time compiler from a static subset of TypeScript to LLVM IR",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -64,15 +64,15 @@
64
64
  "postinstall": "node scripts/postinstall.mjs",
65
65
  "bootstrap": "bash scripts/bootstrap.sh",
66
66
  "compile": "build/nish",
67
- "example": "build/nish examples/add.ts -o build/add.ll && cat build/add.ll",
67
+ "example": "build/nish examples/add.ts --allow-panics -o build/add.ll && cat build/add.ll",
68
68
  "test": "node tests/run.js",
69
- "size-report": "build/nish examples/add.ts -o build/add.ll && bash scripts/size-report.sh build/add.ll examples/main.c",
69
+ "size-report": "build/nish examples/add.ts --allow-panics -o build/add.ll && bash scripts/size-report.sh build/add.ll examples/main.c",
70
70
  "smoke": "bash scripts/smoke.sh",
71
71
  "check": "tsc -p tsconfig.json",
72
72
  "test:update": "UPDATE_GOLDENS=1 node tests/run.js",
73
73
  "test:diff": "node tests/differential/run.js",
74
- "test:nish": "build/nish tests/nish/run.ts -o build/nish-runner.ir/ --link build/nish-runner && build/nish-runner",
75
- "test:cli": "build/nish tests/nish/cli.ts -o build/nish-cli.ir/ --link build/nish-cli && build/nish-cli",
74
+ "test:nish": "bash scripts/nish-harness.sh tests/nish/run.ts build/nish-runner",
75
+ "test:cli": "bash scripts/nish-harness.sh tests/nish/cli.ts build/nish-cli",
76
76
  "test:node": "node tests/differential/unmodified.js",
77
77
  "lint": "node scripts/check-filenames.mjs && node scripts/check-install-scripts.mjs && biome check .",
78
78
  "lint:fix": "biome check --write .",
@@ -114,9 +114,9 @@
114
114
  "typescript": "^5.6.0"
115
115
  },
116
116
  "optionalDependencies": {
117
- "@amritk/nish-aarch64-darwin": "0.42.0",
118
- "@amritk/nish-aarch64-linux": "0.42.0",
119
- "@amritk/nish-x86_64-darwin": "0.42.0",
120
- "@amritk/nish-x86_64-linux": "0.42.0"
117
+ "@amritk/nish-aarch64-darwin": "0.45.1",
118
+ "@amritk/nish-aarch64-linux": "0.45.1",
119
+ "@amritk/nish-x86_64-darwin": "0.45.1",
120
+ "@amritk/nish-x86_64-linux": "0.45.1"
121
121
  }
122
122
  }
@@ -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);