nish 0.20.0 → 0.21.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.
Files changed (76) hide show
  1. package/README.md +19 -59
  2. package/docs/AI.md +56 -9
  3. package/docs/INSTALL.md +22 -10
  4. package/llms.txt +1 -1
  5. package/package.json +17 -5
  6. package/runtime/browser.d.ts +14 -0
  7. package/runtime/browser.mjs +88 -0
  8. package/runtime/core/arrays.d.ts +18 -0
  9. package/runtime/core/arrays.mjs +125 -0
  10. package/runtime/core/checks.mjs +20 -0
  11. package/runtime/core/host.d.ts +70 -0
  12. package/runtime/core/host.mjs +311 -0
  13. package/runtime/core/numbers.d.ts +53 -0
  14. package/runtime/core/numbers.mjs +316 -0
  15. package/runtime/core/result.d.ts +41 -0
  16. package/runtime/core/result.mjs +127 -0
  17. package/runtime/core/strings.d.ts +14 -0
  18. package/runtime/core/strings.mjs +169 -0
  19. package/runtime/core.d.ts +9 -0
  20. package/runtime/core.mjs +55 -0
  21. package/runtime/host.d.ts +6 -0
  22. package/runtime/nish.d.ts +44 -0
  23. package/runtime/nish.h +75 -2
  24. package/runtime/nish.mjs +23 -3
  25. package/runtime/node.mjs +383 -0
  26. package/runtime/runtime-bytes.c +401 -0
  27. package/runtime/runtime-cross.c +1 -1
  28. package/runtime/runtime-host.c +1 -1
  29. package/runtime/runtime-os.c +1 -1
  30. package/runtime/runtime-parallel.c +1 -1
  31. package/runtime/runtime-simd.c +231 -7
  32. package/runtime/runtime-term.c +504 -0
  33. package/runtime/runtime-wasm.c +0 -11
  34. package/runtime/runtime.c +190 -6
  35. package/runtime/shim.mjs +6 -985
  36. package/runtime/sqlite/LICENSE.md +85 -0
  37. package/runtime/sqlite/README.md +45 -0
  38. package/runtime/sqlite/nish-sqlite.c +359 -0
  39. package/runtime/sqlite/sqlite3.c +271131 -0
  40. package/runtime/sqlite/sqlite3.h +14447 -0
  41. package/scripts/build.sh +261 -53
  42. package/scripts/fuzz.sh +218 -0
  43. package/scripts/gen-json-pow5.mjs +130 -0
  44. package/scripts/platform-package.mjs +3 -1
  45. package/scripts/security-scope.mjs +139 -0
  46. package/scripts/smoke.sh +3 -2
  47. package/std/LICENSE-fast-float +23 -0
  48. package/std/LICENSE-fdlibm +8 -0
  49. package/std/README.md +31 -11
  50. package/std/base64.ts +116 -0
  51. package/std/bytes.ts +256 -0
  52. package/std/cli.ts +328 -0
  53. package/std/collections.ts +6 -5
  54. package/std/crypto/LICENSE-crypt_blowfish +26 -0
  55. package/std/crypto/base64url.ts +125 -0
  56. package/std/crypto/bcrypt.ts +760 -0
  57. package/std/crypto/hmac.ts +138 -2
  58. package/std/crypto/x509.ts +11 -122
  59. package/std/json-tokenizer.ts +2998 -57
  60. package/std/json-value.ts +22 -3
  61. package/std/json.ts +83 -1
  62. package/std/math.ts +608 -0
  63. package/std/net/http1-server.ts +85 -8
  64. package/std/net/http3-server.ts +50 -12
  65. package/std/net/postgres.ts +1875 -0
  66. package/std/net/quic-listener.ts +20 -4
  67. package/std/regex.ts +4274 -0
  68. package/std/sqlite.ts +391 -0
  69. package/std/term-io.ts +978 -0
  70. package/std/term-pixels.ts +874 -0
  71. package/std/term-probe.ts +459 -0
  72. package/std/term-rate.ts +993 -0
  73. package/std/term.ts +2453 -0
  74. package/std/text.ts +90 -7
  75. package/std/yaml-lines.ts +141 -0
  76. package/std/yaml.ts +4675 -0
@@ -0,0 +1,70 @@
1
+ /**
2
+ * The host the core runs on, and every builtin that asks it. Import
3
+ * `nish/runtime/node` or `nish/runtime/browser` to install one; until then each
4
+ * of these throws a `NishNoHost`.
5
+ */
6
+ import type { f64, i32, i64, u8 } from "./numbers.js";
7
+
8
+ /** What an adapter installs: `exit` never returns, and `call` answers the builtin `name`. */
9
+ export interface Host {
10
+ writeOut(s: string): void;
11
+ writeErr(s: string): void;
12
+ exit(code: number): never;
13
+ argv(): string[];
14
+ call(name: string, args: unknown[]): unknown;
15
+ }
16
+
17
+ /** What every member of the default host throws: no adapter has been imported. */
18
+ export declare class NishNoHost extends Error {
19
+ constructor(name: string);
20
+ readonly requested: string;
21
+ }
22
+
23
+ export declare function installHost(next: Host): void;
24
+ export declare function log(x: unknown): void;
25
+ export declare function error(x: unknown): void;
26
+ export declare function write(s: string): void;
27
+ export declare function writeError(s: string): void;
28
+ export declare function panic(message: string): never;
29
+ export declare function exit(code: i32): never;
30
+ export declare function argv(): string[];
31
+ export declare function readFileSync(path: string): string;
32
+ export declare function readFileSyncOrNull(path: string): string | null;
33
+ export declare function readFileBytesSync(path: string): u8[] | null;
34
+ export declare function writeFileSync(path: string, data: string): void;
35
+ export declare function appendFileSync(path: string, data: string): void;
36
+ export declare function mkdirSync(path: string): boolean;
37
+ export declare function isDirectorySync(path: string): boolean;
38
+ export declare function readdirSync(path: string): string[] | null;
39
+ export declare function realpathSync(path: string): string | null;
40
+ export declare function platform(): string;
41
+ export declare function arch(): string;
42
+ export declare function getenv(name: string): string | null;
43
+ export declare function spawnSync(argv: string[]): number;
44
+ export declare function spawnSyncTo(argv: string[], stdoutPath: string, stderrPath: string): number;
45
+ export declare function kill(pid: i32, sig: i32): i32;
46
+ export declare function availableParallelism(): i32;
47
+ export declare function totalMemory(): i64;
48
+ export declare function monotonicNanos(): i64;
49
+ export declare function statMtimeSync(path: string): f64;
50
+ export declare function lstatOwnerModeSync(path: string): i64;
51
+ export declare function geteuid(): i64;
52
+ export declare function isExecutableSync(path: string): boolean;
53
+ export declare function isTerminal(fd: i32): boolean;
54
+ export declare function terminalSize(): i32[];
55
+ /** `crypto.getRandomValues(bytes)` for a `u8[]`; a typed array goes straight through. */
56
+ export declare function getRandomValues<T extends u8[] | Uint8Array>(bytes: T): T;
57
+ /** These four have no synchronous reading: each throws, naming itself, and is typed as `runtime/nish.d.ts` types it. */
58
+ export declare function spawn(argv: string[]): i32;
59
+ export declare function waitPid(pid: i32, flags: i32): i32[];
60
+ export declare function signalFd(): i32;
61
+ export declare function readSignal(fd: i32): i32;
62
+ /** What each `nish:net` function is under the core: a throw naming `name`. */
63
+ export declare function noNetReading(name: string): never;
64
+ /** What `readStdin`, `waitStdin`, `setRawMode` and `setTerminalRestore` are under the core: a throw naming `name`. */
65
+ export declare function noTermReading(name: string): never;
66
+ /** `nish:term`'s four that have no reading under Node: each throws, naming itself. */
67
+ export declare function readStdin(buf: u8[], off: i32, len: i32): i32;
68
+ export declare function waitStdin(timeoutMs: i32): boolean;
69
+ export declare function setRawMode(on: boolean): i32;
70
+ export declare function setTerminalRestore(bytes: string): i32;
@@ -0,0 +1,311 @@
1
+ /**
2
+ * The host the core runs on (WP33 R3). Everything a program asks of the world
3
+ * outside it — its output, its exit, its arguments, its files, its environment,
4
+ * its children and its clocks — goes through one installed host object:
5
+ *
6
+ * {
7
+ * writeOut(s), writeErr(s): write `s` to stdout or stderr, synchronously;
8
+ * exit(code): end the process with `code` and never return;
9
+ * argv(): the program then its arguments, as `process.argv` is under Nish;
10
+ * call(name, args): the host-bound builtin `name`, applied to `args`;
11
+ * }
12
+ *
13
+ * `runtime/node.mjs` installs Node's on import. Until a host is installed every
14
+ * member throws a `NishNoHost` naming what was asked for, so a core loaded on
15
+ * its own fails loudly rather than writing nowhere.
16
+ *
17
+ * The functions that have no synchronous reading under Node (`spawn`,
18
+ * `waitPid`, `signalFd`, `readSignal`, the `nish:net` set) throw here without
19
+ * asking any host.
20
+ */
21
+
22
+ /** What every member of the default host throws: no adapter has been imported. */
23
+ export class NishNoHost extends Error {
24
+ constructor(name) {
25
+ super(`\`${name}\` needs a host, and none is installed: import runtime/node.mjs (or another adapter) first`);
26
+ this.name = "NishNoHost";
27
+ this.requested = name;
28
+ }
29
+ }
30
+
31
+ const NO_HOST = {
32
+ writeOut() {
33
+ throw new NishNoHost("writeOut");
34
+ },
35
+ writeErr() {
36
+ throw new NishNoHost("writeErr");
37
+ },
38
+ exit() {
39
+ throw new NishNoHost("exit");
40
+ },
41
+ argv() {
42
+ throw new NishNoHost("argv");
43
+ },
44
+ call(name) {
45
+ throw new NishNoHost(name);
46
+ },
47
+ };
48
+
49
+ let host = NO_HOST;
50
+
51
+ /** Make `next` the host every later call goes through. */
52
+ export function installHost(next) {
53
+ host = next;
54
+ }
55
+
56
+ /** `console.log(x)`: `String(x)` (no `n` suffix for i64) plus a newline, written synchronously. */
57
+ export function log(x) {
58
+ host.writeOut(`${String(x)}\n`);
59
+ }
60
+
61
+ /** `console.error(x)`: the same, on stderr. */
62
+ export function error(x) {
63
+ host.writeErr(`${String(x)}\n`);
64
+ }
65
+
66
+ /** `write(s)` / `writeError(s)`: the bytes as they are, no trailing newline. */
67
+ export function write(s) {
68
+ host.writeOut(s);
69
+ }
70
+
71
+ export function writeError(s) {
72
+ host.writeErr(s);
73
+ }
74
+
75
+ /** `panic(message)`: the message on stderr, then exit 1. */
76
+ export function panic(message) {
77
+ host.writeErr(`${message}\n`);
78
+ host.exit(1);
79
+ }
80
+
81
+ /**
82
+ * `process.exit(code)`: libc `exit` truncates to 8 bits exactly like Node does.
83
+ * The code is converted as an index is (`sext` from i32, truncation from f64).
84
+ */
85
+ export function exit(code) {
86
+ host.exit(typeof code === "bigint" ? Number(code) : Math.trunc(code));
87
+ }
88
+
89
+ /** `process.argv`: index 0 is the program (the script here, the executable natively), then the arguments. */
90
+ export function argv() {
91
+ return host.argv();
92
+ }
93
+
94
+ // ---- The host-bound builtins ----------------------------------------------------
95
+ //
96
+ // Each is the installed host's answer, under the same name and with the same
97
+ // arguments; what each answers is written beside its Node body in
98
+ // `runtime/node.mjs`.
99
+
100
+ export function readFileSync(path) {
101
+ return host.call("readFileSync", [path]);
102
+ }
103
+
104
+ export function readFileSyncOrNull(path) {
105
+ return host.call("readFileSyncOrNull", [path]);
106
+ }
107
+
108
+ export function readFileBytesSync(path) {
109
+ return host.call("readFileBytesSync", [path]);
110
+ }
111
+
112
+ export function writeFileSync(path, data) {
113
+ return host.call("writeFileSync", [path, data]);
114
+ }
115
+
116
+ export function appendFileSync(path, data) {
117
+ return host.call("appendFileSync", [path, data]);
118
+ }
119
+
120
+ export function mkdirSync(path) {
121
+ return host.call("mkdirSync", [path]);
122
+ }
123
+
124
+ export function isDirectorySync(path) {
125
+ return host.call("isDirectorySync", [path]);
126
+ }
127
+
128
+ export function readdirSync(path) {
129
+ return host.call("readdirSync", [path]);
130
+ }
131
+
132
+ export function realpathSync(path) {
133
+ return host.call("realpathSync", [path]);
134
+ }
135
+
136
+ export function platform() {
137
+ return host.call("platform", []);
138
+ }
139
+
140
+ export function arch() {
141
+ return host.call("arch", []);
142
+ }
143
+
144
+ export function getenv(name) {
145
+ return host.call("getenv", [name]);
146
+ }
147
+
148
+ export function spawnSync(argv) {
149
+ return host.call("spawnSync", [argv]);
150
+ }
151
+
152
+ export function spawnSyncTo(argv, out, err) {
153
+ return host.call("spawnSyncTo", [argv, out, err]);
154
+ }
155
+
156
+ export function kill(pid, sig) {
157
+ return host.call("kill", [pid, sig]);
158
+ }
159
+
160
+ export function availableParallelism() {
161
+ return host.call("availableParallelism", []);
162
+ }
163
+
164
+ export function totalMemory() {
165
+ return host.call("totalMemory", []);
166
+ }
167
+
168
+ export function monotonicNanos() {
169
+ return host.call("monotonicNanos", []);
170
+ }
171
+
172
+ export function statMtimeSync(path) {
173
+ return host.call("statMtimeSync", [path]);
174
+ }
175
+
176
+ export function lstatOwnerModeSync(path) {
177
+ return host.call("lstatOwnerModeSync", [path]);
178
+ }
179
+
180
+ export function geteuid() {
181
+ return host.call("geteuid", []);
182
+ }
183
+
184
+ export function isExecutableSync(path) {
185
+ return host.call("isExecutableSync", [path]);
186
+ }
187
+
188
+ export function isTerminal(fd) {
189
+ return host.call("isTerminal", [fd]);
190
+ }
191
+
192
+ export function terminalSize() {
193
+ return host.call("terminalSize", []);
194
+ }
195
+
196
+ // ---- Randomness ----------------------------------------------------------------
197
+
198
+ /**
199
+ * The host's own fill, taken from the web `crypto` global before
200
+ * `runtime/nish.mjs` puts the one below in its place on the same object.
201
+ */
202
+ const webRandom = globalThis.crypto.getRandomValues.bind(globalThis.crypto);
203
+
204
+ /**
205
+ * `crypto.getRandomValues(bytes)` for the plain array a `u8[]` is here. The
206
+ * web's own takes only a typed array, so the bytes are drawn into one and copied
207
+ * across. More than 65,536 fails with the native panic and its words, where
208
+ * the web's would throw a `QuotaExceededError`: the exit status is 1 either
209
+ * way. A typed array goes straight through.
210
+ */
211
+ export function getRandomValues(bytes) {
212
+ if (!Array.isArray(bytes)) {
213
+ return webRandom(bytes);
214
+ }
215
+ if (bytes.length > 65536) {
216
+ panic(`crypto.getRandomValues: ${bytes.length} bytes asked for, and one call fills at most 65536`);
217
+ }
218
+ const drawn = webRandom(new Uint8Array(bytes.length));
219
+ for (let i = 0; i < drawn.length; i++) {
220
+ bytes[i] = drawn[i];
221
+ }
222
+ return bytes;
223
+ }
224
+
225
+ // ---- No synchronous reading ------------------------------------------------------
226
+
227
+ /**
228
+ * `spawn(argv)` and `waitPid(pid, flags)` (WP34 N12) have no faithful reading
229
+ * under Node, for `signalFd`'s reason: Node reaps its children itself, on the
230
+ * event loop, and hands their status to an `exit` listener, so no synchronous
231
+ * function here can wait for one, and a child Node started is never there for
232
+ * a wait-less poll to find. Each throws, naming itself.
233
+ */
234
+ function noProcessReading(name) {
235
+ throw new Error(
236
+ `\`${name}\` has no synchronous reading under Node: a child is reaped by the event loop, which a synchronous wait never returns to (docs/wp33-round-trip.md)`
237
+ );
238
+ }
239
+
240
+ export function spawn() {
241
+ return noProcessReading("spawn");
242
+ }
243
+
244
+ export function waitPid() {
245
+ return noProcessReading("waitPid");
246
+ }
247
+
248
+ /**
249
+ * `signalFd()` and `readSignal(fd)` have no faithful reading under Node, and
250
+ * these say so rather than answer something else. Node delivers a signal to
251
+ * its event loop (`process.on("SIGTERM")`), and a blocking read keeps the loop
252
+ * from ever running, so no synchronous function here can learn that one
253
+ * arrived. docs/wp33-round-trip.md §3.5 has the row and the translation.
254
+ */
255
+ export function signalFd() {
256
+ throw new Error(
257
+ "signalFd has no synchronous reading under Node: a signal reaches the event loop, which a blocking readSignal never returns to (docs/wp33-round-trip.md)"
258
+ );
259
+ }
260
+
261
+ export function readSignal() {
262
+ return signalFd();
263
+ }
264
+
265
+ /**
266
+ * The `nish:net` functions (WP34 N5) have no faithful reading under Node
267
+ * either, for `signalFd`'s reason: Node's sockets are asynchronous only, and a
268
+ * socket becomes readable, writable or connected only to the event loop, which
269
+ * a program that owns its loop and spins or blocks in it never returns to. So
270
+ * each throws, naming itself, rather than answer -11 forever; `nish.mjs`
271
+ * installs one per name.
272
+ * docs/wp33-round-trip.md is the note that asks for one stated answer.
273
+ */
274
+ export function noNetReading(name) {
275
+ throw new Error(
276
+ `\`${name}\` has no synchronous reading under Node: a socket is ready only to the event loop, which a program that owns its loop never returns to (docs/wp33-round-trip.md)`
277
+ );
278
+ }
279
+
280
+ /**
281
+ * `readStdin`, `waitStdin` and `setRawMode` (`nish:term`) throw too, naming
282
+ * themselves, for `noNetReading`'s reason: Node reads stdin only to its event
283
+ * loop, and its own raw mode turns Ctrl-C into a byte, which is not the native
284
+ * answer. `setTerminalRestore` throws as well: its bytes go out from the
285
+ * native runtime's exit and signal paths, which Node does not run. The core
286
+ * exports one per name, for `--emit-ts` output to import, and
287
+ * `nish.mjs` installs one per name as a global.
288
+ */
289
+ export function noTermReading(name) {
290
+ throw new Error(
291
+ `\`${name}\` has no synchronous reading under Node: stdin is read only by the event loop, which a program that owns its loop never returns to (docs/RUN_UNDER_NODE.md)`
292
+ );
293
+ }
294
+
295
+ export function readStdin() {
296
+ return noTermReading("readStdin");
297
+ }
298
+
299
+ export function waitStdin() {
300
+ return noTermReading("waitStdin");
301
+ }
302
+
303
+ export function setRawMode() {
304
+ return noTermReading("setRawMode");
305
+ }
306
+
307
+ export function setTerminalRestore() {
308
+ throw new Error(
309
+ "`setTerminalRestore` has no reading under Node: its bytes go out from the native runtime's exit, panic and signal paths, which Node does not run (docs/RUN_UNDER_NODE.md)"
310
+ );
311
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * The widths as the core holds them (docs/RUN_UNDER_NODE.md): `i64` and `u64`
3
+ * are a `bigint`, every other width a `number`. `runtime/nish.d.ts` spells
4
+ * each as `number` for `tsc`'s reading of a Nish program; `tests/run.js`
5
+ * (`runtime-hosts`) holds these signatures to that file's.
6
+ */
7
+ export type i32 = number;
8
+ export type i64 = bigint;
9
+ export type u8 = number;
10
+ export type u16 = number;
11
+ export type u32 = number;
12
+ export type u64 = bigint;
13
+ export type f32 = number;
14
+ export type f64 = number;
15
+
16
+ /** A width as the rewriter names it, for `convert`. */
17
+ export type NumericKind = "i32" | "i64" | "u8" | "u16" | "u32" | "u64" | "f32" | "f64";
18
+
19
+ export declare function toI32(x: number | bigint | boolean): i32;
20
+ export declare function toI64(x: number | bigint | boolean): i64;
21
+ export declare function toF64(x: number | bigint | boolean): f64;
22
+ export declare function f64ToBits(x: f64): i64;
23
+ export declare function f32ToBits(x: f32): i32;
24
+ export declare function bitsToF64(bits: i64): f64;
25
+ export declare function ctSelect<T extends u32 | u64>(mask: T, a: T, b: T): T;
26
+ export declare function ctEq<T extends u32 | u64>(a: T, b: T): T;
27
+ export declare function checkedAdd<T extends i32 | i64>(a: T, b: T): T;
28
+ export declare function checkedSub<T extends i32 | i64>(a: T, b: T): T;
29
+ export declare function checkedMul<T extends i32 | i64>(a: T, b: T): T;
30
+ export declare function checkedNeg<T extends i32 | i64>(a: T): T;
31
+ export declare function wrappingAdd<T extends i32 | i64>(a: T, b: T): T;
32
+ export declare function wrappingSub<T extends i32 | i64>(a: T, b: T): T;
33
+ export declare function wrappingMul<T extends i32 | i64>(a: T, b: T): T;
34
+ export declare function idiv<T extends number | bigint>(a: T, b: T): T;
35
+ export declare function irem<T extends number | bigint>(a: T, b: T): T;
36
+ export declare function wrapI64(x: bigint): i64;
37
+ export declare function shlI64(a: i64, b: i64): i64;
38
+ export declare function ashrI64(a: i64, b: i64): i64;
39
+ export declare function lshrI64(a: i64, b: i64): i64;
40
+ export declare function absI64(x: i64): i64;
41
+ export declare function signI64<T extends i64 | u64>(x: T): T;
42
+ export declare function minI64(a: i64, b: i64): i64;
43
+ export declare function maxI64(a: i64, b: i64): i64;
44
+ export declare function wrapU64(x: bigint): u64;
45
+ export declare function shlU64(a: u64, b: u64): u64;
46
+ export declare function lshrU64(a: u64, b: u64): u64;
47
+ export declare function minU64(a: u64, b: u64): u64;
48
+ export declare function maxU64(a: u64, b: u64): u64;
49
+ export declare function convert(x: number | bigint, from: NumericKind, to: NumericKind): number | bigint;
50
+ export declare function parseInt(s: string): i32;
51
+ export declare function parseFloat(s: string): f64;
52
+ export declare function number(x: string | number | bigint | boolean): f64;
53
+ export declare function offsets<T extends unknown[]>(first: number, args: T): T;