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