nish 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.
- package/README.md +23 -59
- package/docs/AI.md +74 -9
- package/docs/INSTALL.md +52 -10
- package/llms.txt +1 -1
- package/package.json +20 -5
- package/runtime/browser.d.ts +6 -0
- package/runtime/browser.mjs +77 -0
- package/runtime/core/arrays.d.ts +36 -0
- package/runtime/core/arrays.mjs +153 -0
- package/runtime/core/checks.mjs +20 -0
- package/runtime/core/host.d.ts +80 -0
- package/runtime/core/host.mjs +325 -0
- package/runtime/core/numbers.d.ts +66 -0
- package/runtime/core/numbers.mjs +343 -0
- package/runtime/core/result.d.ts +45 -0
- package/runtime/core/result.mjs +140 -0
- package/runtime/core/strings.d.ts +14 -0
- package/runtime/core/strings.mjs +169 -0
- package/runtime/core.d.ts +9 -0
- package/runtime/core.mjs +55 -0
- package/runtime/host.d.ts +6 -0
- package/runtime/nish.d.ts +46 -2
- package/runtime/nish.h +75 -2
- package/runtime/nish.mjs +23 -3
- package/runtime/node.mjs +386 -0
- package/runtime/runtime-bytes.c +401 -0
- package/runtime/runtime-cross.c +1 -1
- package/runtime/runtime-host.c +1 -1
- package/runtime/runtime-net.c +12 -1
- package/runtime/runtime-os.c +1 -1
- package/runtime/runtime-parallel.c +1 -1
- package/runtime/runtime-simd.c +231 -7
- package/runtime/runtime-term.c +504 -0
- package/runtime/runtime-wasm.c +0 -11
- package/runtime/runtime.c +190 -6
- package/runtime/shim.mjs +6 -985
- package/runtime/sqlite/LICENSE.md +85 -0
- package/runtime/sqlite/README.md +45 -0
- package/runtime/sqlite/nish-sqlite.c +359 -0
- package/runtime/sqlite/sqlite3.c +271131 -0
- package/runtime/sqlite/sqlite3.h +14447 -0
- package/scripts/build.sh +311 -59
- package/scripts/check-hardening.mjs +202 -0
- package/scripts/fuzz.sh +218 -0
- package/scripts/gen-json-pow5.mjs +130 -0
- package/scripts/platform-package.mjs +3 -1
- package/scripts/security-scope.mjs +139 -0
- package/scripts/smoke.sh +3 -2
- package/std/LICENSE-fast-float +23 -0
- package/std/LICENSE-fdlibm +8 -0
- package/std/README.md +32 -12
- package/std/base64.ts +116 -0
- package/std/bytes.ts +256 -0
- package/std/cli.ts +344 -0
- package/std/collections.ts +6 -5
- package/std/crypto/LICENSE-crypt_blowfish +26 -0
- package/std/crypto/base64url.ts +125 -0
- package/std/crypto/bcrypt.ts +1557 -0
- package/std/crypto/hmac.ts +138 -2
- package/std/crypto/x509.ts +11 -122
- package/std/json-tokenizer.ts +2998 -57
- package/std/json-value.ts +22 -3
- package/std/json.ts +87 -8
- package/std/math.ts +608 -0
- package/std/net/http1-server.ts +282 -40
- package/std/net/http1.ts +40 -21
- package/std/net/http3-server.ts +50 -12
- package/std/net/postgres.ts +2294 -0
- package/std/net/prefork.ts +23 -3
- package/std/net/quic-listener.ts +20 -4
- package/std/random.ts +66 -4
- package/std/regex.ts +4274 -0
- package/std/sqlite.ts +391 -0
- package/std/term-io.ts +978 -0
- package/std/term-pixels.ts +874 -0
- package/std/term-probe.ts +459 -0
- package/std/term-rate.ts +993 -0
- package/std/term.ts +2453 -0
- package/std/text.ts +90 -7
- package/std/yaml-lines.ts +141 -0
- package/std/yaml.ts +4675 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { panicIndex, panicSlice, toIndex } from "./checks.mjs";
|
|
2
|
+
|
|
3
|
+
// The native check compares the index as an unsigned 64-bit number, so a
|
|
4
|
+
// negative one fails, and prints, as that number: `-1` is 18446744073709551615.
|
|
5
|
+
// A NaN or infinite `number` has no such number and prints as it is.
|
|
6
|
+
function outOfRange(i, len) {
|
|
7
|
+
const n = toIndex(i);
|
|
8
|
+
if (typeof i !== "bigint" && !Number.isFinite(n)) panicIndex(n, len);
|
|
9
|
+
const k = typeof i === "bigint" ? i : BigInt(n);
|
|
10
|
+
panicIndex(k < 0n ? BigInt.asUintN(64, k) : k, len);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** `a[i]` read with the WP4 bounds check (negative indices fail like the unsigned compare does). */
|
|
14
|
+
export function idx(a, i) {
|
|
15
|
+
const k = toIndex(i);
|
|
16
|
+
if (!(k >= 0 && k < a.length)) outOfRange(i, a.length);
|
|
17
|
+
return a[k];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** `a.pop()`: the last element, or the bounds panic — Nish has no `undefined` to return. */
|
|
21
|
+
export function pop(a) {
|
|
22
|
+
if (a.length === 0) panicIndex(0, 0);
|
|
23
|
+
return a.pop();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** `a[i] = v`: evaluates `a`, `i`, `v`, then checks and stores; yields `v`. */
|
|
27
|
+
export function setIdx(a, i, v) {
|
|
28
|
+
const k = toIndex(i);
|
|
29
|
+
if (!(k >= 0 && k < a.length)) outOfRange(i, a.length);
|
|
30
|
+
a[k] = v;
|
|
31
|
+
return v;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* `a[i] op= v`: evaluates `a`, `i`, checks, loads, then `f(old)` computes the
|
|
36
|
+
* new element, and checks again before the store, as `f` can shrink `a`.
|
|
37
|
+
*/
|
|
38
|
+
export function updIdx(a, i, f) {
|
|
39
|
+
const k = toIndex(i);
|
|
40
|
+
if (!(k >= 0 && k < a.length)) outOfRange(i, a.length);
|
|
41
|
+
const v = f(a[k]);
|
|
42
|
+
if (!(k < a.length)) outOfRange(i, a.length);
|
|
43
|
+
a[k] = v;
|
|
44
|
+
return v;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** `nish:unsafe`'s `uncheckedGet(a, i)`: the element with no bounds check, as the native load is. */
|
|
48
|
+
export function uncheckedGet(a, i) {
|
|
49
|
+
return a[i];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** `nish:unsafe`'s `uncheckedSet(a, i, v)`: the store with no bounds check. */
|
|
53
|
+
export function uncheckedSet(a, i, v) {
|
|
54
|
+
a[i] = v;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// A record an array holds inline is an object literal here, and nothing else
|
|
58
|
+
// is a plain object: a class instance has its class's prototype, and an array
|
|
59
|
+
// has Array's.
|
|
60
|
+
function isRecord(v) {
|
|
61
|
+
return v !== null && typeof v === "object" && Object.getPrototypeOf(v) === Object.prototype;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** A value put into an array slot from a generic body: a copy when it is a record, as a slot holds it inline. */
|
|
65
|
+
export function copyIn(v) {
|
|
66
|
+
return isRecord(v) ? { ...v } : v;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* `a[i] = v` from a generic body: as `setIdx`, but a record is written field
|
|
71
|
+
* by field into the record already in the slot, as an inline element is.
|
|
72
|
+
*/
|
|
73
|
+
export function storeIdx(a, i, v) {
|
|
74
|
+
const k = toIndex(i);
|
|
75
|
+
if (!(k >= 0 && k < a.length)) outOfRange(i, a.length);
|
|
76
|
+
const old = a[k];
|
|
77
|
+
if (isRecord(old) && isRecord(v)) Object.assign(old, v);
|
|
78
|
+
else a[k] = v;
|
|
79
|
+
return v;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* `dst.set(src, offset)` as `nish --emit-ts` writes it: an `i64` or `u64`
|
|
84
|
+
* offset, a bigint here, is compared as one, so a value past 2^53 fails with
|
|
85
|
+
* the native panic rather than being rounded into range, and one in range is
|
|
86
|
+
* the number `arraySet` takes. `runtime/nish.mjs` keeps `arraySet`, whose
|
|
87
|
+
* bigint offset throws as `docs/LANGUAGE.md` says.
|
|
88
|
+
*/
|
|
89
|
+
export function arraySetAt(dst, src, offset) {
|
|
90
|
+
if (typeof offset === "bigint") {
|
|
91
|
+
const end = offset + BigInt(src.length);
|
|
92
|
+
if (offset < 0n || end > BigInt(dst.length)) panicSlice(offset, end, dst.length);
|
|
93
|
+
offset = Number(offset);
|
|
94
|
+
}
|
|
95
|
+
arraySet(dst, src, offset);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* `dst.set(src, offset)` (WP34 N2): `TypedArray.prototype.set`'s copy on the
|
|
100
|
+
* plain array a `u8[]` is here. The source is copied first, so a self-copy or
|
|
101
|
+
* an overlapping one reads what was there before, as `memmove` does natively;
|
|
102
|
+
* a range past the end fails with the native panic and its words, where a
|
|
103
|
+
* typed array would throw a `RangeError` for the same offsets.
|
|
104
|
+
*/
|
|
105
|
+
export function arraySet(dst, src, offset) {
|
|
106
|
+
// `ToIntegerOrInfinity`: NaN is 0, as `llvm.fptosi.sat` makes it natively.
|
|
107
|
+
// `Math.trunc` rather than `toIndex`, which converts a bigint: an `i64` or
|
|
108
|
+
// `u64` offset is a bigint here, and it throws the `TypeError` the typed
|
|
109
|
+
// array and `Array.prototype.fill` throw for one, instead of being rounded
|
|
110
|
+
// to the nearest double past 2^53 (docs/RUN_UNDER_NODE.md).
|
|
111
|
+
const at = offset === undefined ? 0 : Math.trunc(offset) || 0;
|
|
112
|
+
const end = at + src.length;
|
|
113
|
+
if (!(at >= 0 && end <= dst.length)) panicSlice(...saturatedRange(at, src.length), dst.length);
|
|
114
|
+
// Two plain arrays overlap only when they are one array, which is the one
|
|
115
|
+
// case that must read the source before writing it.
|
|
116
|
+
const from = src === dst ? src.slice() : src;
|
|
117
|
+
for (let i = 0; i < from.length; i++) dst[at + i] = from[i];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* `[at, at + n)` as the native panic spells it: the offset saturated into an
|
|
122
|
+
* `i64`, as `llvm.fptosi.sat` leaves it, and the end wrapped, so `1e300` is
|
|
123
|
+
* `9223372036854775807` and its end `-9223372036854775808`.
|
|
124
|
+
*/
|
|
125
|
+
function saturatedRange(at, n) {
|
|
126
|
+
if (Number.isSafeInteger(at + n)) return [at, at + n];
|
|
127
|
+
const max = 2n ** 63n - 1n;
|
|
128
|
+
const start = at === Infinity || at >= 2 ** 63 ? max : at <= -(2 ** 63) ? -max - 1n : BigInt(at);
|
|
129
|
+
return [start, BigInt.asIntN(64, start + BigInt(n))];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** `new Array<T>(n)`: `n` zero-filled elements (`0`, `0n`, or `false`). */
|
|
133
|
+
export function newArray(n, zero) {
|
|
134
|
+
return new Array(toIndex(n)).fill(zero);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Arena introspection has no JS counterpart: the stubs keep programs that only
|
|
138
|
+
// compare `Arena.used()` before/after (a "stayed flat" check) in agreement, while
|
|
139
|
+
// programs that print raw byte counts are listed as known differences.
|
|
140
|
+
//
|
|
141
|
+
// `nish --threads` makes the native arena thread-local (WP20 T0) and nothing
|
|
142
|
+
// here moves with it: a rewritten program runs on the one thread Node gives it,
|
|
143
|
+
// so "the arena of the calling thread" and "the arena" are the same object, and
|
|
144
|
+
// these stubs answer for both. If T1 ever lands a spawn the rewrite can reach,
|
|
145
|
+
// that is when this file grows a second arena to keep count of.
|
|
146
|
+
export function arenaUsed() { return 0n; }
|
|
147
|
+
export function arenaMark() { return 0n; }
|
|
148
|
+
export function arenaRelease() {}
|
|
149
|
+
export function arenaReset() {}
|
|
150
|
+
/** `using a = arena()`: a disposable whose disposal does nothing, because there is no arena to release. */
|
|
151
|
+
export function arena() {
|
|
152
|
+
return { [Symbol.dispose]() {} };
|
|
153
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The checks the strings and arrays share: an index as the compiler converts
|
|
3
|
+
* it, and the two bounds panics with their native words. Internal to the core:
|
|
4
|
+
* `runtime/core.mjs` does not re-export them.
|
|
5
|
+
*/
|
|
6
|
+
import { panic } from "./host.mjs";
|
|
7
|
+
|
|
8
|
+
/** Index conversion as in the compiler: `sext` from i32, `fptosi` (truncation) from f64. */
|
|
9
|
+
export function toIndex(i) {
|
|
10
|
+
return typeof i === "bigint" ? Number(i) : Math.trunc(i);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function panicIndex(i, len) {
|
|
14
|
+
panic(`index out of range: ${i} >= ${len}`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** `nish_panic_slice`: the failed range check of `s.slice(start, end)`; the offsets print signed. */
|
|
18
|
+
export function panicSlice(start, end, len) {
|
|
19
|
+
panic(`slice out of range: [${start}, ${end}) of length ${len}`);
|
|
20
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
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
|
+
/**
|
|
24
|
+
* What the browser host's `exit(code)`, and every panic there, throws: a page
|
|
25
|
+
* cannot end itself. Tell it apart with `instanceof`, not by its `name`.
|
|
26
|
+
*/
|
|
27
|
+
export declare class NishExit extends Error {
|
|
28
|
+
/** The 8-bit status a native exit would leave: `code & 0xff`. */
|
|
29
|
+
readonly code: number;
|
|
30
|
+
constructor(code: number);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export declare function installHost(next: Host): void;
|
|
34
|
+
export declare function log(x: unknown): void;
|
|
35
|
+
export declare function error(x: unknown): void;
|
|
36
|
+
export declare function write(s: string): void;
|
|
37
|
+
export declare function writeError(s: string): void;
|
|
38
|
+
export declare function panic(message: string): never;
|
|
39
|
+
export declare function exit(code: i32): never;
|
|
40
|
+
export declare function argv(): string[];
|
|
41
|
+
export declare function readFileSync(path: string): string;
|
|
42
|
+
export declare function readFileSyncOrNull(path: string): string | null;
|
|
43
|
+
export declare function readFileBytesSync(path: string): u8[] | null;
|
|
44
|
+
export declare function writeFileSync(path: string, data: string): void;
|
|
45
|
+
export declare function appendFileSync(path: string, data: string): void;
|
|
46
|
+
export declare function mkdirSync(path: string): boolean;
|
|
47
|
+
export declare function isDirectorySync(path: string): boolean;
|
|
48
|
+
export declare function readdirSync(path: string): string[] | null;
|
|
49
|
+
export declare function realpathSync(path: string): string | null;
|
|
50
|
+
export declare function platform(): string;
|
|
51
|
+
export declare function arch(): string;
|
|
52
|
+
export declare function getenv(name: string): string | null;
|
|
53
|
+
export declare function spawnSync(argv: string[]): number;
|
|
54
|
+
export declare function spawnSyncTo(argv: string[], stdoutPath: string, stderrPath: string): number;
|
|
55
|
+
export declare function kill(pid: i32, sig: i32): i32;
|
|
56
|
+
export declare function availableParallelism(): i32;
|
|
57
|
+
export declare function totalMemory(): i64;
|
|
58
|
+
export declare function monotonicNanos(): i64;
|
|
59
|
+
export declare function statMtimeSync(path: string): f64;
|
|
60
|
+
export declare function lstatOwnerModeSync(path: string): i64;
|
|
61
|
+
export declare function geteuid(): i64;
|
|
62
|
+
export declare function isExecutableSync(path: string): boolean;
|
|
63
|
+
export declare function isTerminal(fd: i32): boolean;
|
|
64
|
+
export declare function terminalSize(): i32[];
|
|
65
|
+
/** `crypto.getRandomValues(bytes)` for a `u8[]`; a typed array goes straight through. */
|
|
66
|
+
export declare function getRandomValues<T extends u8[] | Uint8Array>(bytes: T): T;
|
|
67
|
+
/** These four have no synchronous reading: each throws, naming itself, and is typed as `runtime/nish.d.ts` types it. */
|
|
68
|
+
export declare function spawn(argv: string[]): i32;
|
|
69
|
+
export declare function waitPid(pid: i32, flags: i32): i32[];
|
|
70
|
+
export declare function signalFd(): i32;
|
|
71
|
+
export declare function readSignal(fd: i32): i32;
|
|
72
|
+
/** What each `nish:net` function is under the core: a throw naming `name`. */
|
|
73
|
+
export declare function noNetReading(name: string): never;
|
|
74
|
+
/** What `readStdin`, `waitStdin`, `setRawMode` and `setTerminalRestore` are under the core: a throw naming `name`. */
|
|
75
|
+
export declare function noTermReading(name: string): never;
|
|
76
|
+
/** `nish:term`'s four that have no reading under Node: each throws, naming itself. */
|
|
77
|
+
export declare function readStdin(buf: u8[], off: i32, len: i32): i32;
|
|
78
|
+
export declare function waitStdin(timeoutMs: i32): boolean;
|
|
79
|
+
export declare function setRawMode(on: boolean): i32;
|
|
80
|
+
export declare function setTerminalRestore(bytes: string): i32;
|
|
@@ -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;
|