@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
package/runtime/shim.mjs CHANGED
@@ -1,988 +1,9 @@
1
1
  /**
2
- * Nish builtins for Node (WP13 differential testing).
3
- *
4
- * The differential runner (tests/differential/run.js) rewrites an Nish
5
- * program into plain JavaScript and runs it under Node with this module as
6
- * `__nish`. Every helper here reproduces the *runtime* semantics the compiled
7
- * binary has (runtime/runtime.c and runtime/runtime-os.c, the system-call half,
8
- * plus the intrinsics in docs/wp7-runtime.md) where JavaScript's own semantics
9
- * differ:
10
- *
11
- * - `number` is a 32-bit integer in the default mode (`--number-mode i32`),
12
- * `i64` is a 64-bit integer, both wrapping; JS has doubles and BigInt.
13
- * - `u8`/`u16`/`u32`/`u64` are unsigned and wrapping, and JavaScript has no
14
- * unsigned integers at all: the narrow three live in a `number` masked
15
- * back into range (`& 0xFF`, `& 0xFFFF`, `>>> 0`) and `u64` is a BigInt
16
- * kept in range with `BigInt.asUintN(64, x)`.
17
- * - `f32` is a 32-bit float and JavaScript has only doubles, so every `f32`
18
- * result is rounded with `Math.fround`.
19
- * - `s.length` is the UTF-8 byte length, and so is every index the string
20
- * methods take or return: `charCodeAt` yields a byte (and bounds-checks
21
- * instead of returning `NaN`), `substring` cuts on byte offsets, and
22
- * `indexOf` answers with one. `slice` cuts on byte offsets too and panics
23
- * on a range the string does not contain, where JavaScript would clamp and
24
- * read a negative offset from the end. `String.fromCharCode` builds a
25
- * one-byte string from the low 8 bits, and `String.fromBytes` one such
26
- * string per byte of its range.
27
- * - `a[i]` is bounds-checked: out of range prints
28
- * `index out of range: <i> >= <len>` to stderr and exits 1, and so is
29
- * `a.pop()` on an empty array, which has no `undefined` to return.
30
- * - `toI32/toI64` from f64 saturate (NaN -> 0), integer conversions wrap.
31
- * - `console.log(x)` never prints the `n` suffix of an i64 and writes
32
- * synchronously so `process.exit` cannot lose output.
33
- * - file I/O errors print `nish: cannot read <path>` and exit 1.
34
- * - a directory listing is sorted by UTF-8 bytes, which is `strcmp`'s order
35
- * and not `Array#sort`'s UTF-16 one; `monotonicNanos` reads a clock whose
36
- * origin is arbitrary, so two readings can agree with a native run and a
37
- * single reading never can.
38
- * - `process.argv[0]` is the program (the script here, the executable
39
- * natively); `parseInt` is base 10 only and saturates into i32 (0 for no
40
- * digits); `parseFloat`/`Number` accept ASCII whitespace, decimal forms,
41
- * `Infinity`, and a `0x` hex prefix (parseFloat too, unlike JS).
42
- *
43
- * The rewrite rules that call these helpers are listed in docs/wp13-differential.md.
2
+ * Nish builtins for Node: the host-agnostic core (`runtime/core.mjs`, whose
3
+ * header lists the semantics) with the Node host (`runtime/node.mjs`)
4
+ * installed. The differential runner and `runtime/nish.mjs` import this module,
5
+ * and the frozen rewrites name its exports.
44
6
  */
45
- import child_process from "node:child_process";
46
- import { webcrypto } from "node:crypto";
47
- import fs from "node:fs";
48
- import os from "node:os";
7
+ import "./node.mjs";
49
8
 
50
- const I32_MIN = -2147483648;
51
- const I32_MAX = 2147483647;
52
- const I64_MIN = -(1n << 63n);
53
- const I64_MAX = (1n << 63n) - 1n;
54
-
55
- /** `trunc i64 -> i32` (wrap) for BigInt, `llvm.fptosi.sat.i32.f64` for numbers. */
56
- export function toI32(x) {
57
- if (typeof x === "bigint") return Number(BigInt.asIntN(32, x));
58
- if (Number.isNaN(x)) return 0;
59
- if (x >= I32_MAX) return I32_MAX;
60
- if (x <= I32_MIN) return I32_MIN;
61
- return Math.trunc(x) | 0;
62
- }
63
-
64
- /** `sext i32 -> i64` for int32 numbers, `llvm.fptosi.sat.i64.f64` for doubles. */
65
- export function toI64(x) {
66
- if (typeof x === "bigint") return x;
67
- if (Number.isNaN(x)) return 0n;
68
- if (x >= 9223372036854775808) return I64_MAX;
69
- if (x <= -9223372036854775808) return I64_MIN;
70
- return BigInt(Math.trunc(x));
71
- }
72
-
73
- /** `sitofp` for both integer widths; a double is returned unchanged. */
74
- export function toF64(x) {
75
- return typeof x === "bigint" ? Number(x) : x;
76
- }
77
-
78
- /**
79
- * `f64ToBits` / `bitsToF64`: reinterpret the 64 bits, never convert the value.
80
- * The compiler lowers each to one `bitcast`; here it is a one-element
81
- * DataView, which is the only way JavaScript lets you see a double's bits.
82
- * The result is signed, matching the i64 the compiler produces.
83
- */
84
- const BITS = new DataView(new ArrayBuffer(8));
85
-
86
- export function f64ToBits(x) {
87
- BITS.setFloat64(0, x);
88
- return BigInt.asIntN(64, BITS.getBigUint64(0));
89
- }
90
-
91
- export function bitsToF64(b) {
92
- BITS.setBigInt64(0, BigInt.asIntN(64, b));
93
- return BITS.getFloat64(0);
94
- }
95
-
96
- /**
97
- * `ctSelect` / `ctEq` (WP34 N6). A `u32` is a `number` here and a `u64` a
98
- * BigInt, so the operands' kind picks the width, and a mix of the two — which
99
- * the native checker refuses, and which a `u64` written as a bare literal is
100
- * under an unrewritten run — throws the `TypeError` BigInt arithmetic throws
101
- * rather than comparing a number with a BigInt and answering zero. JavaScript's
102
- * `&` reads a `number` as a signed 32-bit integer, so each answer is put back in
103
- * range with `>>> 0` or `asUintN(64, ...)`. These branch: only the native
104
- * lowering promises constant time.
105
- */
106
- function ctWide(name, first, second, third) {
107
- const wide = typeof first === "bigint";
108
- if ((typeof second === "bigint") !== wide || (typeof third === "bigint") !== wide) {
109
- throw new TypeError(`${name}: cannot mix a u64 (BigInt) with a u32 (number)`);
110
- }
111
- return wide;
112
- }
113
-
114
- const U64_ONES = (1n << 64n) - 1n;
115
-
116
- export function ctSelect(mask, a, b) {
117
- if (ctWide("ctSelect", mask, a, b)) return wrapU64((a & mask) | (b & ~mask));
118
- return ((a & mask) | (b & ~mask)) >>> 0;
119
- }
120
-
121
- export function ctEq(a, b) {
122
- // `ctEq` has two operands, so the second one stands in for the third.
123
- if (ctWide("ctEq", a, b, b)) return wrapU64(a ^ b) === 0n ? U64_ONES : 0n;
124
- return (a ^ b) === 0 ? 0xffffffff : 0;
125
- }
126
-
127
- /**
128
- * `nish:secret` (docs/LANGUAGE.md, "Secrets"). Natively the compiler holds a
129
- * `Secret` to its rules and `wipe` is a volatile store of zeros; here the rules
130
- * have already been checked by the compiler or by nobody, so a `Secret` is a
131
- * plain wrapper and `wipe` zero-fills what it holds. `runtime/nish.mjs`
132
- * resolves the `nish:secret` specifier to this module, so these are its
133
- * exports by name. A zero-fill under Node promises nothing about copies the
134
- * engine made, which is the native wipe's whole point and is not claimed here.
135
- */
136
- export class Secret {
137
- constructor(value) {
138
- this.value = value;
139
- }
140
- }
141
-
142
- export function secret(value) {
143
- return new Secret(value);
144
- }
145
-
146
- export function expose(s, f) {
147
- return f(s.value);
148
- }
149
-
150
- export function exposeWith(s, arg, f) {
151
- return f(s.value, arg);
152
- }
153
-
154
- /** Zero an array of integers, or every integer field of a record: `0` for a number, `0n` for an `i64` or `u64`. */
155
- function zeroFill(target) {
156
- if (Array.isArray(target) || ArrayBuffer.isView(target)) {
157
- for (let i = 0; i < target.length; i++) {
158
- target[i] = typeof target[i] === "bigint" ? 0n : 0;
159
- }
160
- return;
161
- }
162
- for (const key of Object.keys(target)) {
163
- const field = target[key];
164
- if (typeof field === "bigint") target[key] = 0n;
165
- else if (typeof field === "number") target[key] = 0;
166
- }
167
- }
168
-
169
- export function wipe(target) {
170
- zeroFill(target instanceof Secret ? target.value : target);
171
- }
172
-
173
- /** Wrap a BigInt to the i64 range: every i64 `+ - * /` and unary minus goes through here. */
174
- export function wrapI64(x) {
175
- return BigInt.asIntN(64, x);
176
- }
177
-
178
- /**
179
- * The i64 shifts. BigInt has no `>>>` and neither of its shifts masks the
180
- * count, so all three do here what `codegen/emit/bitwise.ts` emits: mask the
181
- * count to 6 bits, then shift. `lshrI64` reads the operand as unsigned before
182
- * shifting and hands back the signed reading of the result, which is `lshr`.
183
- */
184
- export function shlI64(a, b) {
185
- return BigInt.asIntN(64, a << (b & 63n));
186
- }
187
- export function ashrI64(a, b) {
188
- return a >> (b & 63n);
189
- }
190
- export function lshrI64(a, b) {
191
- return BigInt.asIntN(64, BigInt.asUintN(64, a) >> (b & 63n));
192
- }
193
-
194
- /** `llvm.abs.i64(x, false)`: the minimum value wraps to itself. */
195
- export function absI64(x) {
196
- return BigInt.asIntN(64, x < 0n ? -x : x);
197
- }
198
-
199
- /** `llvm.smin.i64` / `llvm.smax.i64` (Math.min/max reject BigInt). */
200
- export function minI64(a, b) {
201
- return a < b ? a : b;
202
- }
203
- export function maxI64(a, b) {
204
- return a > b ? a : b;
205
- }
206
-
207
- // ---- Unsigned integers (WP15) -------------------------------------------------------
208
- //
209
- // JavaScript has no unsigned integer type, so every unsigned result is masked
210
- // back into its width right where the compiled code would have relied on the
211
- // LLVM type. u8/u16/u32 fit a `number` exactly (2^32 - 1 < 2^53); u64 is a
212
- // BigInt, so it goes through `wrapU64` the way i64 goes through `wrapI64`.
213
-
214
- /** Wrap into the u64 range: every u64 `+ - * /`, unary minus and shift ends here. */
215
- export function wrapU64(x) {
216
- return BigInt.asUintN(64, x);
217
- }
218
-
219
- /**
220
- * The u64 shifts. A u64 is already held as a non-negative BigInt, so its `>>`
221
- * *is* the logical shift; what these add over the operator is the count mask
222
- * (BigInt does not mask) and the wrap back into the unsigned range.
223
- */
224
- export function shlU64(a, b) {
225
- return BigInt.asUintN(64, a << (b & 63n));
226
- }
227
- export function lshrU64(a, b) {
228
- return BigInt.asUintN(64, a) >> (b & 63n);
229
- }
230
-
231
- /**
232
- * The widths, as the rewriter names them. `bits` drives every mask and
233
- * `BigInt.asIntN`/`asUintN` call; `big` says whether the value is a BigInt
234
- * (i64/u64) or a `number` in JavaScript.
235
- */
236
- const INT_KINDS = {
237
- i32: { bits: 32, signed: true, big: false },
238
- i64: { bits: 64, signed: true, big: true },
239
- u8: { bits: 8, signed: false, big: false },
240
- u16: { bits: 16, signed: false, big: false },
241
- u32: { bits: 32, signed: false, big: false },
242
- u64: { bits: 64, signed: false, big: true },
243
- };
244
-
245
- /** `llvm.umin`/`umax` on u64 (`Math.min`/`max` reject BigInt); the u32-and-below widths use Math. */
246
- export function minU64(a, b) {
247
- return a < b ? a : b;
248
- }
249
- export function maxU64(a, b) {
250
- return a > b ? a : b;
251
- }
252
-
253
- /**
254
- * Every numeric conversion whose source or target is unsigned (`toU8`,
255
- * `toU32(i)`, `toI32(u)`, ...), as one function taking the kind names the
256
- * checker recorded. Doing it through BigInt is what makes the whole matrix
257
- * exact in one place: an integer source is turned into its true mathematical
258
- * value, then `asIntN`/`asUintN` performs the sign-extend, zero-extend or
259
- * truncate that the target's width and signedness call for — which is exactly
260
- * what `sext`/`zext`/`trunc` do natively.
261
- */
262
- export function convert(x, from, to) {
263
- const num = typeof x === "bigint" ? Number(x) : x;
264
- if (to === "f64") return num; // sitofp / uitofp / fpext
265
- if (to === "f32") return Math.fround(num); // ... / fptrunc, then rounded to a float
266
- const target = INT_KINDS[to];
267
- const lo = target.signed ? -(1n << BigInt(target.bits - 1)) : 0n;
268
- const hi = target.signed ? (1n << BigInt(target.bits - 1)) - 1n : (1n << BigInt(target.bits)) - 1n;
269
- let exact;
270
- if (INT_KINDS[from] === undefined) {
271
- // A float source (f32 or f64) goes through the saturating intrinsics: NaN
272
- // is 0 and out-of-range values clamp. The bounds are BigInt because
273
- // 2^64 - 1 has no exact `number`.
274
- if (Number.isNaN(x)) exact = 0n;
275
- else if (x <= Number(lo)) exact = lo;
276
- else if (x >= Number(hi)) exact = hi;
277
- else exact = BigInt(Math.trunc(x));
278
- } else {
279
- exact = BigInt(x); // sext/zext/trunc, below
280
- }
281
- const wrapped = target.signed ? BigInt.asIntN(target.bits, exact) : BigInt.asUintN(target.bits, exact);
282
- return target.big ? wrapped : Number(wrapped);
283
- }
284
-
285
- /** `s.length`: UTF-8 byte length for strings (arrays keep their own `.length`). */
286
- export function strLen(x) {
287
- return typeof x === "string" ? Buffer.byteLength(x, "utf8") : x.length;
288
- }
289
-
290
- /** The UTF-8 bytes of `s`, which is what an Nish string holds. */
291
- function bytesOf(s) {
292
- return Buffer.from(s, "utf8");
293
- }
294
-
295
- /** `s.charCodeAt(i)`: the byte at `i`, bounds-checked as `a[i]` is (JavaScript answers NaN). */
296
- export function charCodeAt(s, i) {
297
- const bytes = bytesOf(s);
298
- const k = toIndex(i);
299
- if (!(k >= 0 && k < bytes.length)) panicIndex(k, bytes.length);
300
- return bytes[k];
301
- }
302
-
303
- /** `s.substring(a, b)`: JavaScript's clamp and swap, over byte offsets. */
304
- export function substring(s, a, b) {
305
- const bytes = bytesOf(s);
306
- const clamp = (v) => Math.max(0, Math.min(toIndex(v), bytes.length));
307
- const from = clamp(a);
308
- const to = b === undefined ? bytes.length : clamp(b);
309
- return bytes.subarray(Math.min(from, to), Math.max(from, to)).toString("utf8");
310
- }
311
-
312
- /**
313
- * `s.slice(a, b)`: the bytes of `[a, b)` with no clamp, `b` defaulting to the
314
- * byte length (WP15 §4). Out of range panics rather than clamping, so the two
315
- * compares the native code emits are reproduced here rather than JavaScript's
316
- * negative-from-the-end rule.
317
- */
318
- export function slice(s, a, b) {
319
- const bytes = bytesOf(s);
320
- const from = toIndex(a);
321
- const to = b === undefined ? bytes.length : toIndex(b);
322
- if (!(from >= 0 && from <= to && to <= bytes.length)) panicSlice(from, to, bytes.length);
323
- return bytes.subarray(from, to).toString("utf8");
324
- }
325
-
326
- /** `s.indexOf(sub)`: the first *byte* offset, or -1. */
327
- export function indexOf(s, sub) {
328
- return bytesOf(s).indexOf(bytesOf(sub));
329
- }
330
-
331
- /** `nish_str_at`: whether `sub`'s bytes sit at byte offset `at`. */
332
- function occursAt(s, at, sub) {
333
- const bytes = bytesOf(s);
334
- const needle = bytesOf(sub);
335
- return at >= 0 && at + needle.length <= bytes.length && bytes.subarray(at, at + needle.length).equals(needle);
336
- }
337
-
338
- export function startsWith(s, sub) {
339
- return occursAt(s, 0, sub);
340
- }
341
-
342
- export function endsWith(s, sub) {
343
- return occursAt(s, bytesOf(s).length - bytesOf(sub).length, sub);
344
- }
345
-
346
- /** `String.fromCharCode(c)`: the one-byte string of `c & 0xFF`. */
347
- export function fromCharCode(code) {
348
- return Buffer.from([toIndex(code) & 0xff]).toString("latin1");
349
- }
350
-
351
- /**
352
- * `String.fromBytes(bytes, start, end)`: the bytes of `[start, end)`, one
353
- * code unit each as `fromCharCode` builds them, checked as `s.slice`'s range
354
- * is and failing with its panic.
355
- */
356
- export function fromBytes(bytes, start, end) {
357
- const from = toIndex(start);
358
- const to = toIndex(end);
359
- if (!(from >= 0 && from <= to && to <= bytes.length)) panicSlice(from, to, bytes.length);
360
- const out = Buffer.allocUnsafe(to - from);
361
- for (let i = from; i < to; i++) out[i - from] = bytes[i];
362
- return out.toString("latin1");
363
- }
364
-
365
- /** `console.log(x)`: `String(x)` (no `n` suffix for i64) plus a newline, written synchronously. */
366
- export function log(x) {
367
- fs.writeSync(1, `${String(x)}\n`);
368
- }
369
-
370
- /** `console.error(x)`: the same, on stderr. */
371
- export function error(x) {
372
- fs.writeSync(2, `${String(x)}\n`);
373
- }
374
-
375
- /** `write(s)` / `writeError(s)`: the bytes as they are, no trailing newline. */
376
- export function write(s) {
377
- fs.writeSync(1, s);
378
- }
379
-
380
- export function writeError(s) {
381
- fs.writeSync(2, s);
382
- }
383
-
384
- /** `panic(message)`: the message on stderr, then exit 1. */
385
- export function panic(message) {
386
- fs.writeSync(2, `${message}\n`);
387
- process.exit(1);
388
- }
389
-
390
- // ---- Result (WP16) ---------------------------------------------------------
391
- //
392
- // Natively a `Result` is a two-arm struct in the arena; here it is an ordinary
393
- // object with the same three names, so `r.ok`, `r.value` and `r.error` in the
394
- // rewritten program mean what they mean in the compiled one. Only `orReturn`
395
- // needs help: it returns from the *enclosing* function, which no expression in
396
- // JavaScript can do, so it throws a sentinel that `rewrite.js` catches in a
397
- // wrapper around every body that contains one.
398
-
399
- class Propagate {
400
- constructor(error) {
401
- this.error = error;
402
- }
403
- }
404
-
405
- class NishResult {
406
- constructor(ok, value, error) {
407
- this.ok = ok;
408
- if (ok) this.value = value;
409
- else this.error = error;
410
- }
411
- isOk() {
412
- return this.ok;
413
- }
414
- isErr() {
415
- return !this.ok;
416
- }
417
- unwrapOr(fallback) {
418
- return this.ok ? this.value : fallback;
419
- }
420
- expect(message) {
421
- if (this.ok) return this.value;
422
- panic(message);
423
- }
424
- orReturn() {
425
- if (this.ok) return this.value;
426
- throw new Propagate(this.error);
427
- }
428
- }
429
-
430
- /** `Ok(v)`; `Ok()` on a `Result<void, E>` carries nothing. */
431
- export function Ok(value) {
432
- return new NishResult(true, value, undefined);
433
- }
434
-
435
- /** `Err(e)`. */
436
- export function Err(error) {
437
- return new NishResult(false, undefined, error);
438
- }
439
-
440
- /**
441
- * The `catch` half of `orReturn`: re-raise anything that is not a propagation,
442
- * and answer the `Err` the enclosing function should return otherwise. The
443
- * rewriter emits `return __nish.caught(e)` and nothing else, so a genuine
444
- * runtime error still reaches Node unchanged.
445
- */
446
- export function caught(thrown) {
447
- if (thrown instanceof Propagate) return new NishResult(false, undefined, thrown.error);
448
- throw thrown;
449
- }
450
-
451
- /** Index conversion as in the compiler: `sext` from i32, `fptosi` (truncation) from f64. */
452
- function toIndex(i) {
453
- return typeof i === "bigint" ? Number(i) : Math.trunc(i);
454
- }
455
-
456
- function panicIndex(i, len) {
457
- fs.writeSync(2, `index out of range: ${i} >= ${len}\n`);
458
- process.exit(1);
459
- }
460
-
461
- /** `nish_panic_slice`: the failed range check of `s.slice(start, end)`; the offsets print signed. */
462
- function panicSlice(start, end, len) {
463
- fs.writeSync(2, `slice out of range: [${start}, ${end}) of length ${len}\n`);
464
- process.exit(1);
465
- }
466
-
467
- /** `a[i]` read with the WP4 bounds check (negative indices fail like the unsigned compare does). */
468
- export function idx(a, i) {
469
- const k = toIndex(i);
470
- if (!(k >= 0 && k < a.length)) panicIndex(k, a.length);
471
- return a[k];
472
- }
473
-
474
- /** `a.pop()`: the last element, or the bounds panic — Nish has no `undefined` to return. */
475
- export function pop(a) {
476
- if (a.length === 0) panicIndex(0, 0);
477
- return a.pop();
478
- }
479
-
480
- /** `a[i] = v`: evaluates `a`, `i`, `v`, then checks and stores; yields `v`. */
481
- export function setIdx(a, i, v) {
482
- const k = toIndex(i);
483
- if (!(k >= 0 && k < a.length)) panicIndex(k, a.length);
484
- a[k] = v;
485
- return v;
486
- }
487
-
488
- /** `a[i] op= v`: evaluates `a`, `i`, checks, loads, then `f(old)` computes the new element. */
489
- export function updIdx(a, i, f) {
490
- const k = toIndex(i);
491
- if (!(k >= 0 && k < a.length)) panicIndex(k, a.length);
492
- const v = f(a[k]);
493
- a[k] = v;
494
- return v;
495
- }
496
-
497
- /**
498
- * `dst.set(src, offset)` (WP34 N2): `TypedArray.prototype.set`'s copy on the
499
- * plain array a `u8[]` is here. The source is copied first, so a self-copy or
500
- * an overlapping one reads what was there before, as `memmove` does natively;
501
- * a range past the end fails with the native panic and its words, where a
502
- * typed array would throw a `RangeError` for the same offsets.
503
- */
504
- export function arraySet(dst, src, offset) {
505
- // `ToIntegerOrInfinity`: NaN is 0, as `llvm.fptosi.sat` makes it natively.
506
- // `Math.trunc` rather than `toIndex`, which converts a bigint: an `i64` or
507
- // `u64` offset is a bigint here, and it throws the `TypeError` the typed
508
- // array and `Array.prototype.fill` throw for one, instead of being rounded
509
- // to the nearest double past 2^53 (docs/RUN_UNDER_NODE.md).
510
- const at = offset === undefined ? 0 : Math.trunc(offset) || 0;
511
- const end = at + src.length;
512
- if (!(at >= 0 && end <= dst.length)) panicSlice(at, end, dst.length);
513
- // Two plain arrays overlap only when they are one array, which is the one
514
- // case that must read the source before writing it.
515
- const from = src === dst ? src.slice() : src;
516
- for (let i = 0; i < from.length; i++) dst[at + i] = from[i];
517
- }
518
-
519
- /** `new Array<T>(n)`: `n` zero-filled elements (`0`, `0n`, or `false`). */
520
- export function newArray(n, zero) {
521
- return new Array(toIndex(n)).fill(zero);
522
- }
523
-
524
- /** `process.exit(code)`: libc `exit` truncates to 8 bits exactly like Node does. */
525
- export function exit(code) {
526
- process.exit(toIndex(code));
527
- }
528
-
529
-
530
- function ioFail(verb, path) {
531
- fs.writeSync(2, `nish: cannot ${verb} ${path}\n`);
532
- process.exit(1);
533
- }
534
-
535
- export function readFileSync(path) {
536
- try {
537
- return fs.readFileSync(path, "utf8");
538
- } catch {
539
- return ioFail("read", path);
540
- }
541
- }
542
-
543
- /** `readFileSyncOrNull(path)`: null instead of exiting, so the program decides. */
544
- export function readFileSyncOrNull(path) {
545
- try {
546
- return fs.readFileSync(path, "utf8");
547
- } catch {
548
- return null;
549
- }
550
- }
551
-
552
- /** `readFileBytesSync(path)` (WP34 N2): the bytes as a plain array of numbers, or null. */
553
- export function readFileBytesSync(path) {
554
- try {
555
- return Array.from(fs.readFileSync(path));
556
- } catch {
557
- return null;
558
- }
559
- }
560
-
561
- /**
562
- * The flags `runtime-os.c` opens a file it writes with, `O_NOFOLLOW` included
563
- * (docs/security/runtime.md, RT-4): a symbolic link as the last component of
564
- * the path is refused rather than followed, so `writeFileSync`,
565
- * `appendFileSync` and `spawnSyncTo` fail here where they fail natively. Node's
566
- * `"w"` and `"a"` follow the link. A link among the directories on the way is
567
- * still followed, as it is natively. Windows has no `O_NOFOLLOW`, and there the
568
- * flag is 0.
569
- */
570
- const { O_WRONLY, O_CREAT, O_TRUNC, O_APPEND, O_NOFOLLOW = 0 } = fs.constants;
571
- const WRITE_FLAGS = O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW;
572
- const APPEND_FLAGS = O_WRONLY | O_CREAT | O_APPEND | O_NOFOLLOW;
573
-
574
- export function writeFileSync(path, data) {
575
- try {
576
- fs.writeFileSync(path, data, { encoding: "utf8", flag: WRITE_FLAGS });
577
- } catch {
578
- ioFail("write", path);
579
- }
580
- }
581
-
582
- export function appendFileSync(path, data) {
583
- try {
584
- fs.appendFileSync(path, data, { encoding: "utf8", flag: APPEND_FLAGS });
585
- } catch {
586
- ioFail("write", path);
587
- }
588
- }
589
-
590
- /**
591
- * `mkdirSync(path)` (WP14 D4): one directory, not recursive, answering whether
592
- * a directory is there afterwards. Node throws where the runtime answers
593
- * false, and `EEXIST` on a plain file is a failure here as it is there, so the
594
- * `statSync` decides rather than the exception.
595
- */
596
- export function mkdirSync(path) {
597
- try {
598
- fs.mkdirSync(path);
599
- return true;
600
- } catch {
601
- try {
602
- return fs.statSync(path).isDirectory();
603
- } catch {
604
- return false;
605
- }
606
- }
607
- }
608
-
609
- /**
610
- * `isDirectorySync(path)` (WP14 §7a): one stat, and a boolean out of it rather
611
- * than an exception, which is what the runtime's `stat` answers too.
612
- */
613
- export function isDirectorySync(path) {
614
- try {
615
- return fs.statSync(path).isDirectory();
616
- } catch {
617
- return false;
618
- }
619
- }
620
-
621
- /**
622
- * `readdirSync(path)`: the entries, sorted ascending by bytes, or `null` when
623
- * the directory cannot be read. Node throws where the runtime answers a value,
624
- * so the `catch` is what makes the two agree, and a directory that exists and
625
- * is empty answers an empty array on both sides. Node's readdir never yields
626
- * `.` or `..` — the pair `runtime-os.c` skips explicitly — so there is nothing
627
- * to filter out here.
628
- *
629
- * The sort is the semantic point. `runtime-os.c` orders the names with
630
- * `strcmp`, which compares UTF-8 bytes, and `Array#sort` compares UTF-16 code
631
- * units. The two agree on ASCII names and part company above the BMP, where a
632
- * surrogate pair sorts below `U+E000`..`U+FFFF` in UTF-16 and above them in
633
- * UTF-8. So the comparison is over the encoded bytes, which is the native order
634
- * exactly rather than the native order for the names that happen to be ASCII.
635
- */
636
- export function readdirSync(path) {
637
- let names;
638
- try {
639
- names = fs.readdirSync(path);
640
- } catch {
641
- return null;
642
- }
643
- return names.sort((a, b) => Buffer.compare(Buffer.from(a, "utf8"), Buffer.from(b, "utf8")));
644
- }
645
-
646
- /**
647
- * `realpathSync(path)` (WP19 §5a item 4): `path` with its symbolic links
648
- * resolved, or `null` when it does not resolve.
649
- *
650
- * `fs.realpathSync` throws where the native `realpath` answers NULL — a
651
- * missing path, a loop, a component that is not a directory — so the `catch`
652
- * is what makes the two runtimes agree, exactly as it does for `readdirSync`
653
- * above. Node's own answer is already absolute and already normalised, which
654
- * is what POSIX `realpath` guarantees, so nothing here has to normalise it.
655
- */
656
- export function realpathSync(path) {
657
- try {
658
- return fs.realpathSync(path);
659
- } catch {
660
- return null;
661
- }
662
- }
663
-
664
- /**
665
- * `process.platform` / `process.arch` (WP14 §7a). Node's spellings are the
666
- * ones `runtime-os.c` answers with, so on any machine this compiler has a
667
- * triple for the two runtimes give the same string; elsewhere the native build
668
- * says `unknown` where Node names the platform, which is the one place they
669
- * part.
670
- */
671
- export function platform() {
672
- return process.platform;
673
- }
674
-
675
- export function arch() {
676
- return process.arch;
677
- }
678
-
679
- /**
680
- * `getenv(name)` (WP19 R1): the value, or `null` when the variable is unset.
681
- * Node answers `undefined` there and the language has no `undefined`, so the
682
- * `??` is what makes the two runtimes agree; an empty value stays an empty
683
- * string on both sides, because `CC=` is set and `CC` unset is not.
684
- */
685
- export function getenv(name) {
686
- return process.env[name] ?? null;
687
- }
688
-
689
- /**
690
- * `spawnSync(argv)` and `spawnSyncTo(argv, out, err)` (WP14 D4), which are one
691
- * run with its streams answered differently: the child's exit status, 128 + n
692
- * when signal n killed it, -1 for an empty vector or a program that would not
693
- * start. `runtime-os.c` puts one `static nish_spawn_impl` behind both builtins
694
- * for the same reason this module puts one function behind both helpers — the
695
- * argument vector, the wait and the signal convention are written once and
696
- * cannot drift between the two.
697
- *
698
- * `out` and `err` are paths for the child's stdout and stderr, and an **empty**
699
- * string leaves that stream inherited. Each file is created or truncated at
700
- * 0644 with `WRITE_FLAGS`, which is what the runtime's file actions ask for:
701
- * `O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW`, so a symbolic link at the path
702
- * is refused and the answer is -1, on both sides. The descriptors opened here are
703
- * closed again whichever way the child went; natively the child opens them and
704
- * its exit drops them.
705
- */
706
- function spawnImpl(argv, out, err) {
707
- if (argv.length === 0) return -1;
708
- const opened = [];
709
- const stream = (target) => {
710
- if (target.length === 0) return "inherit";
711
- const fd = fs.openSync(target, WRITE_FLAGS, 0o644);
712
- opened.push(fd);
713
- return fd;
714
- };
715
- try {
716
- const r = child_process.spawnSync(argv[0], argv.slice(1), {
717
- stdio: ["inherit", stream(out), stream(err)],
718
- });
719
- if (r.error !== undefined) return -1;
720
- if (r.signal !== null && r.signal !== undefined) return 128 + (os.constants.signals[r.signal] ?? 0);
721
- return r.status === null ? -1 : r.status;
722
- } catch {
723
- // A path that cannot be opened is, natively, a file action the child could
724
- // not perform, and `posix_spawnp` reports that through its return value:
725
- // -1, with no child having run. Opening the second path is what can fail
726
- // after the first file was already created, so the truncation a caller can
727
- // observe happens on both sides.
728
- return -1;
729
- } finally {
730
- for (const fd of opened) fs.closeSync(fd);
731
- }
732
- }
733
-
734
- /** `spawnSync(argv)`: the child inherits this process's streams, as it does natively. */
735
- export function spawnSync(argv) {
736
- return spawnImpl(argv, "", "");
737
- }
738
-
739
- /** `spawnSyncTo(argv, stdoutPath, stderrPath)`: the same run with a stream sent to a file. */
740
- export function spawnSyncTo(argv, out, err) {
741
- return spawnImpl(argv, out, err);
742
- }
743
-
744
- /**
745
- * `spawn(argv)` and `waitPid(pid, flags)` (WP34 N12) have no faithful reading
746
- * under Node, for `signalFd`'s reason: Node reaps its children itself, on the
747
- * event loop, and hands their status to an `exit` listener, so no synchronous
748
- * function here can wait for one, and a child Node started is never there for
749
- * a wait-less poll to find. Each throws, naming itself.
750
- */
751
- function noProcessReading(name) {
752
- throw new Error(
753
- `\`${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)`
754
- );
755
- }
756
-
757
- export function spawn() {
758
- return noProcessReading("spawn");
759
- }
760
-
761
- export function waitPid() {
762
- return noProcessReading("waitPid");
763
- }
764
-
765
- /**
766
- * `kill(pid, sig)`: 0, or a negative errno, as `runtime-os.c`'s `nish_kill`
767
- * answers. Only 0, 2, 9 and 15 are sent and a pid of 0 or below is refused,
768
- * both -22, before Node is asked; Node's error then names its errno, which
769
- * `os.constants.errno` turns back into the number (-3 for no such process).
770
- */
771
- export function kill(pid, sig) {
772
- if (pid <= 0 || !(sig === 0 || sig === 2 || sig === 9 || sig === 15)) return -22;
773
- try {
774
- process.kill(pid, sig);
775
- return 0;
776
- } catch (e) {
777
- return -(os.constants.errno[e.code] ?? 22);
778
- }
779
- }
780
-
781
- /**
782
- * `availableParallelism()`: Node's own count, which reads the affinity mask as
783
- * the native one does, lowered by a positive `NISH_NUM_THREADS` the way the
784
- * native count is. The native count also reads a cgroup v2 CPU quota, which
785
- * Node does not, so inside a container with a quota the two may differ; both
786
- * are at least 1.
787
- */
788
- export function availableParallelism() {
789
- const n = os.availableParallelism();
790
- const want = /^[0-9]+$/.test(process.env.NISH_NUM_THREADS ?? "") ? Number(process.env.NISH_NUM_THREADS) : 0;
791
- return want > 0 && want < n ? want : n;
792
- }
793
-
794
- /** `totalMemory()`: `os.totalmem()`, as a BigInt because an `i64` is held as one here. */
795
- export function totalMemory() {
796
- return BigInt(os.totalmem());
797
- }
798
-
799
- /**
800
- * `monotonicNanos()`: `process.hrtime.bigint()`, a monotonic clock in
801
- * nanoseconds (`CLOCK_MONOTONIC` on every platform that has it, which is the
802
- * one `runtime-os.c` reads). The value is a BigInt because that is how an `i64`
803
- * is held on this side.
804
- *
805
- * No rewrite can make a *reading* agree with a native run: both origins are
806
- * arbitrary and neither is the other's. Only the difference between two reads
807
- * means anything, so a differential program may compare two readings and must
808
- * never print one.
809
- */
810
- export function monotonicNanos() {
811
- return process.hrtime.bigint();
812
- }
813
-
814
- // ---- The host (WP34 N3) ------------------------------------------------------
815
-
816
- /**
817
- * `statMtimeSync(path)`: Node's `mtimeMs` for the path, or NaN when it cannot
818
- * be stat'd, which is the native answer too. `mtimeMs` is the same arithmetic
819
- * `runtime-host.c` does, so the two print the same digits, fraction and all.
820
- */
821
- export function statMtimeSync(path) {
822
- const st = fs.statSync(path, { throwIfNoEntry: false });
823
- if (st === undefined) {
824
- return Number.NaN;
825
- }
826
- return st.mtimeMs;
827
- }
828
-
829
- /**
830
- * The RT-9 owner checks (#386): `lstatOwnerModeSync(path)` is the owner's uid
831
- * in the high 32 bits and `st_mode` in the low 32 of one `lstat`, so a
832
- * symbolic link answers for itself, or -1 when the path does not resolve or
833
- * holds a NUL; `geteuid()` is the effective uid; `isExecutableSync(path)` is
834
- * `access(path, X_OK)`. Each answers what `runtime-host.c` answers, the two
835
- * `i64`s as BigInts because that is how an `i64` is held on this side. Node's
836
- * `fs` throws on a NUL in a path where the runtime names nothing, so the
837
- * `catch` is what makes the two agree there too.
838
- */
839
- export function lstatOwnerModeSync(path) {
840
- try {
841
- const st = fs.lstatSync(path);
842
- return BigInt.asIntN(64, (BigInt(st.uid >>> 0) << 32n) | BigInt(st.mode >>> 0));
843
- } catch {
844
- return -1n;
845
- }
846
- }
847
-
848
- export function geteuid() {
849
- return BigInt(process.geteuid());
850
- }
851
-
852
- export function isExecutableSync(path) {
853
- try {
854
- fs.accessSync(path, fs.constants.X_OK);
855
- return true;
856
- } catch {
857
- return false;
858
- }
859
- }
860
-
861
- /**
862
- * `secureZero(bytes)` (#385): every element set to zero, which is what the
863
- * native `nish_wipe` leaves. Nothing here can drop a store to an array the
864
- * caller still holds, so a loop is the whole of it. What it cannot promise is
865
- * what the native build does: a collector that moved the array may have left a
866
- * copy of the old bytes behind, and JavaScript has no way to reach it.
867
- */
868
- export function secureZero(bytes) {
869
- for (let i = 0; i < bytes.length; i++) {
870
- bytes[i] = 0;
871
- }
872
- }
873
-
874
- /**
875
- * Node's own fill, taken before `runtime/nish.mjs` puts the one below in its
876
- * place on the same object: `webcrypto` is the global `crypto`.
877
- */
878
- const webRandom = webcrypto.getRandomValues.bind(webcrypto);
879
-
880
- /**
881
- * `crypto.getRandomValues(bytes)` for the plain array a `u8[]` is here. Node's
882
- * own takes only a typed array, so the bytes are drawn into one and copied
883
- * across. More than 65,536 fails with the native panic and its words, where
884
- * Node would throw a `QuotaExceededError`: the exit status is 1 either way. A
885
- * typed array goes straight through.
886
- */
887
- export function getRandomValues(bytes) {
888
- if (!Array.isArray(bytes)) {
889
- return webRandom(bytes);
890
- }
891
- if (bytes.length > 65536) {
892
- panic(`crypto.getRandomValues: ${bytes.length} bytes asked for, and one call fills at most 65536`);
893
- }
894
- const drawn = webRandom(new Uint8Array(bytes.length));
895
- for (let i = 0; i < drawn.length; i++) {
896
- bytes[i] = drawn[i];
897
- }
898
- return bytes;
899
- }
900
-
901
- /**
902
- * `signalFd()` and `readSignal(fd)` have no faithful reading under Node, and
903
- * these say so rather than answer something else. Node delivers a signal to
904
- * its event loop (`process.on("SIGTERM")`), and a blocking read keeps the loop
905
- * from ever running, so no synchronous function here can learn that one
906
- * arrived. docs/wp33-round-trip.md §3.5 has the row and the translation.
907
- */
908
- export function signalFd() {
909
- throw new Error(
910
- "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)"
911
- );
912
- }
913
-
914
- export function readSignal() {
915
- return signalFd();
916
- }
917
-
918
- /**
919
- * The `nish:net` functions (WP34 N5) have no faithful reading under Node
920
- * either, for `signalFd`'s reason: Node's sockets are asynchronous only, and a
921
- * socket becomes readable, writable or connected only to the event loop, which
922
- * a program that owns its loop and spins or blocks in it never returns to. So
923
- * each throws, naming itself, rather than answer -11 forever; `nish.mjs`
924
- * installs one per name.
925
- * docs/wp33-round-trip.md is the note that asks for one stated answer.
926
- */
927
- export function noNetReading(name) {
928
- throw new Error(
929
- `\`${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)`
930
- );
931
- }
932
-
933
- /** `process.argv`: index 0 is the program (the script here, the executable natively), then the arguments. */
934
- export function argv() {
935
- return process.argv.slice(1);
936
- }
937
-
938
- const SPACES = "[ \\t\\n\\v\\f\\r]*";
939
- /** What `nish_parse_number` recognises: strtod's decimal and hex-integer forms, or an exact `Infinity`. */
940
- const LITERAL = new RegExp(`^${SPACES}([+-]?)(Infinity|0[xX][0-9a-fA-F]+|(?:\\d+\\.?\\d*|\\.\\d+)(?:[eE][+-]?\\d+)?)`);
941
- const BLANK = new RegExp(`^${SPACES}$`);
942
- const WHOLE = new RegExp(`${LITERAL.source}${SPACES}$`);
943
-
944
- /** The value of a `LITERAL` match: hex goes through parseInt(16), and `-` applies afterwards as in strtod. */
945
- function literalValue(m) {
946
- const magnitude = /^0[xX]/.test(m[2]) ? Number.parseInt(m[2], 16) : Number(m[2]);
947
- return m[1] === "-" ? -magnitude : magnitude;
948
- }
949
-
950
- /** `parseInt(s)`: strtoll base 10 (ASCII whitespace, sign, digits; 0 without digits) saturated into i32. */
951
- export function parseInt(s) {
952
- const m = new RegExp(`^${SPACES}([+-]?\\d+)`).exec(s);
953
- return m ? toI32(Number(m[1])) : 0;
954
- }
955
-
956
- /** `parseFloat(s)`: the longest literal after ASCII whitespace, else NaN (`0x1A` is 26, as strtod reads it). */
957
- export function parseFloat(s) {
958
- const m = LITERAL.exec(s);
959
- return m ? literalValue(m) : NaN;
960
- }
961
-
962
- /** `Number(x)`: a string must be one literal bar surrounding ASCII whitespace (blank is 0); others convert numerically. */
963
- export function number(x) {
964
- if (typeof x === "bigint") return Number(x);
965
- if (typeof x === "boolean") return x ? 1 : 0;
966
- if (typeof x !== "string") return x;
967
- const m = WHOLE.exec(x);
968
- if (m) return literalValue(m);
969
- return BLANK.test(x) ? 0 : NaN;
970
- }
971
-
972
- // Arena introspection has no JS counterpart: the stubs keep programs that only
973
- // compare `Arena.used()` before/after (a "stayed flat" check) in agreement, while
974
- // programs that print raw byte counts are listed as known differences.
975
- //
976
- // `nish --threads` makes the native arena thread-local (WP20 T0) and nothing
977
- // here moves with it: a rewritten program runs on the one thread Node gives it,
978
- // so "the arena of the calling thread" and "the arena" are the same object, and
979
- // these stubs answer for both. If T1 ever lands a spawn the rewrite can reach,
980
- // that is when this file grows a second arena to keep count of.
981
- export function arenaUsed() { return 0; }
982
- export function arenaMark() { return 0; }
983
- export function arenaRelease() {}
984
- export function arenaReset() {}
985
- /** `using a = arena()`: a disposable whose disposal does nothing, because there is no arena to release. */
986
- export function arena() {
987
- return { [Symbol.dispose]() {} };
988
- }
9
+ export * from "./core.mjs";