nish 0.40.1 → 0.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/INSTALL.md +5 -4
- package/package.json +5 -5
- package/runtime/runtime-uring.c +1194 -0
- package/scripts/build.sh +7 -6
- package/std/README.md +1 -0
- package/std/net/prefork.ts +49 -13
- package/std/temporal.ts +3091 -0
package/scripts/build.sh
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
#
|
|
4
4
|
# scripts/build.sh <module.ll> [more .ll/.c files...] -o <out> [--profile debug|speed|size|wasm|wasm-host|wasi|napi]
|
|
5
5
|
#
|
|
6
|
-
# The C runtime is
|
|
6
|
+
# The C runtime is eighteen translation units and is named as one: an input
|
|
7
7
|
# <dir>/runtime.c also compiles <dir>/runtime-text.c, the strings and the digits
|
|
8
8
|
# of numbers, <dir>/runtime-os.c, the half that wraps the
|
|
9
9
|
# system calls (files, directories, subprocesses, the environment, the clock),
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
# threads, <dir>/runtime-cross.c, the copies that carry a string or an array
|
|
12
12
|
# from one thread's arena to another's, <dir>/runtime-host.c, the wall clock, entropy, file times and
|
|
13
13
|
# signals, <dir>/runtime-net.c, the sockets of `nish:net`,
|
|
14
|
+
# <dir>/runtime-uring.c, the io_uring rings that will hold them past RLIMIT_NOFILE,
|
|
14
15
|
# <dir>/runtime-simd.c, the byte searches that read a vector at a time and
|
|
15
16
|
# the SSE4.1 rounding of `Math.floor` and its kin,
|
|
16
17
|
# <dir>/runtime-aes.c, AES-GCM on the CPU's AES instructions,
|
|
@@ -127,7 +128,7 @@
|
|
|
127
128
|
#
|
|
128
129
|
# The runtime compiled once (the debug, speed and size profiles only; `nish
|
|
129
130
|
# --link` and `nish run` use these through their runtime cache, src/compile.ts):
|
|
130
|
-
# --runtime-objects <dir> compile the runtime.c input, its
|
|
131
|
+
# --runtime-objects <dir> compile the runtime.c input, its seventeen halves and runtime-async.c,
|
|
131
132
|
# one $CC -c each and all at once, into
|
|
132
133
|
# <dir>/<unit>.o with the profile's compile flags,
|
|
133
134
|
# and link nothing. speed and size store LTO bitcode,
|
|
@@ -272,9 +273,9 @@ if [ -n "$sqlite_to" ] || [ -n "$sqlite_from" ]; then
|
|
|
272
273
|
fi
|
|
273
274
|
[ -n "$out" ] || [ -n "$objects_to" ] || [ -n "$sqlite_to" ] || { echo "error: -o <out> is required" >&2; exit 2; }
|
|
274
275
|
|
|
275
|
-
# The runtime is
|
|
276
|
+
# The runtime is eighteen translation units, and a caller names one: whoever passes
|
|
276
277
|
# <dir>/runtime.c gets <dir>/runtime-text.c, <dir>/runtime-os.c, <dir>/runtime-parallel.c,
|
|
277
|
-
# <dir>/runtime-cross.c, <dir>/runtime-host.c, <dir>/runtime-net.c, <dir>/runtime-simd.c,
|
|
278
|
+
# <dir>/runtime-cross.c, <dir>/runtime-host.c, <dir>/runtime-net.c, <dir>/runtime-uring.c, <dir>/runtime-simd.c,
|
|
278
279
|
# <dir>/runtime-aes.c, <dir>/runtime-sha256.c, <dir>/runtime-chacha20.c, <dir>/runtime-checksum.c, <dir>/runtime-term.c,
|
|
279
280
|
# <dir>/runtime-bytes.c, <dir>/runtime-sandbox.c, <dir>/runtime-shm.c and <dir>/runtime-region.c compiled beside it, on every profile,
|
|
280
281
|
# wasi included: runtime-simd.c and runtime-bytes.c take their scalar paths there
|
|
@@ -283,7 +284,7 @@ fi
|
|
|
283
284
|
# stubs that nothing calls, because the compiler emits no call to their kernels for wasm32, and
|
|
284
285
|
# runtime-term.c reads stdin and answers "not a terminal", and runtime-sandbox.c
|
|
285
286
|
# compiles to two functions that refuse to run, which no wasm build calls, and
|
|
286
|
-
# runtime-shm.c answers -95 for every region. They were one file until the
|
|
287
|
+
# runtime-shm.c answers -95 for every region, and runtime-uring.c compiles to nothing. They were one file until the
|
|
287
288
|
# operating-system half was split out for its own size budget, and the parallel
|
|
288
289
|
# and text halves followed for the same reason (each file's header comment says why), and
|
|
289
290
|
# a link line is where those splits
|
|
@@ -293,7 +294,7 @@ fi
|
|
|
293
294
|
# here keeps every one of those correct, and keeps "the runtime" one thing to
|
|
294
295
|
# name from the outside. A caller that names one itself is left alone, because
|
|
295
296
|
# naming one object twice is a duplicate-symbol error.
|
|
296
|
-
halves="runtime-text.c runtime-os.c runtime-parallel.c runtime-cross.c runtime-host.c runtime-net.c runtime-simd.c runtime-aes.c runtime-sha256.c runtime-chacha20.c runtime-checksum.c runtime-term.c runtime-bytes.c runtime-sandbox.c runtime-shm.c runtime-region.c"
|
|
297
|
+
halves="runtime-text.c runtime-os.c runtime-parallel.c runtime-cross.c runtime-host.c runtime-net.c runtime-uring.c runtime-simd.c runtime-aes.c runtime-sha256.c runtime-chacha20.c runtime-checksum.c runtime-term.c runtime-bytes.c runtime-sandbox.c runtime-shm.c runtime-region.c"
|
|
297
298
|
for i in ${inputs[@]+"${inputs[@]}"}; do
|
|
298
299
|
case "$i" in
|
|
299
300
|
*/runtime.c|runtime.c)
|
package/std/README.md
CHANGED
|
@@ -35,6 +35,7 @@ a call loads without an import (`collections.ts`, `array.ts`, `sort.ts`,
|
|
|
35
35
|
| [`map.ts`](https://github.com/amritk/nish/blob/main/std/map.ts) | `reserve(m, n)` and `getOrInsert(m, k, v)` for the global `Map`. Their bodies are the meaning, and what runs under Node: `reserve` does nothing, and `getOrInsert` is a `get`, and a `set` of `v` when the key was missing. Natively the compiler lowers every call in place — `reserve` to the table's `reserveSlots`, which grows the buckets once so that `n` entries fit without a rebuild, and `getOrInsert` to one `probe` and a `valueAt` or an `insertAt` through its answer — so, like `collections.ts`, it writes no `.ll` of its own ([`docs/LANGUAGE.md`](https://github.com/amritk/nish/blob/main/docs/LANGUAGE.md#map-and-set)) |
|
|
36
36
|
| [`bytes.ts`](https://github.com/amritk/nish/blob/main/std/bytes.ts) | `getU16LE` / `getU16BE` to `getF64LE` / `getF64BE` and the `set` twins: `u16`, `u32`, `u64`, `i32`, `i64` and `f64` read from and written into a `u8[]` at an offset, little- or big-endian. Each access is an ordinary index, so out of range is an `index` panic naming the first missing byte. In range the answers are those of the private copies `std/` keeps (`sha256LoadWord`, `chacha20Word`, `poly1305Le64`, sha512's `loadWord` / `storeWord`, `http2FramePut32`, `tlsPutU16`), none of which it replaces. A guard in the caller does not yet remove the checks after inlining. Under Node only the `u16` and `u32` functions answer as natively. Beside them are the runs a parser walks — `skipBlank`, `skipDigits`, `skipJsonText` and `skipJsonPlain`, the first index in a clamped range whose byte is not a JSON blank, a digit, a byte a JSON string holds as itself or a byte a JSON writer copies without an escape — and `copyBytes`, a window copied as one move that panics on a range outside its array. Their bodies call private walkers, which are what runs under Node; natively the compiler replaces each walker's call with a kernel of `runtime/runtime-bytes.c`, or `llvm.memmove` for the copy ([`docs/LANGUAGE.md`](https://github.com/amritk/nish/blob/main/docs/LANGUAGE.md#byte-runs-skipblank-skipdigits-skipjsontext-skipjsonplain-and-copybytes-in-nishbytes)). The runs are variable-time. `tests/link/std_bytes*` are its programs |
|
|
37
37
|
| [`base64.ts`](https://github.com/amritk/nish/blob/main/std/base64.ts) | `nish/base64`: padded standard base64 (RFC 4648 §4), encode only, for kitty graphics frames. `base64Encode(bytes)` answers a string, `base64EncodeInto(src, off, len, dst, at)` writes into a byte buffer allocating nothing, and `base64EncodedLength(n)` sizes it; a range outside its array panics before a byte is written. It indexes its alphabet by the sextet, so it is not constant-time: it is for pixels, not secrets. A strict decoder, and an encoder for key material, are `crypto/base64url.ts`'s `base64StdPadDecode` and `base64StdPadEncode` (`tests/link/std_base64`, `std_base64_f64`, `std_base64_negative_*`; the two modules in one program: `tests/link/std_base64_two_modules`; [guide](../docs/guide/using-nish/standard-library.md#nishbase64)) |
|
|
38
|
+
| [`temporal.ts`](https://github.com/amritk/nish/blob/main/std/temporal.ts) | `nish/temporal`: TC39 `Temporal` in the ISO 8601 calendar. `PlainDate`, `PlainTime`, `PlainDateTime`, `Duration` and `Instant` are immutable classes, and each field reads as Temporal's getter of the same name. Temporal's statics are free functions named after the class: `plainDateFrom` (`Temporal.PlainDate.from`), `plainDateCompare`, `instantFromEpochMilliseconds`, `nowInstant` (`Temporal.Now.instant`), and the rest. `add`, `subtract`, `until`, `since`, `equals`, `toString` and `toJSON` follow the specification's defaults. Strings are RFC 9557, and one Temporal refuses panics with `RangeError:`, or answers `null` from the `...OrNull` twin. There are no time zones but UTC, no option bags, no `bigint` `epochNanoseconds` and no calendar but `iso8601` yet (`tests/link/std_temporal`, every case also answered by Node's `Temporal`; [guide](../docs/guide/using-nish/dates-and-times.md)) |
|
|
38
39
|
| [`random.ts`](https://github.com/amritk/nish/blob/main/std/random.ts) | `Rng`, a seeded generator of pseudo-random numbers: `new Rng(seed)`, `nextI32()`, `nextF64()` and `nextInt(lo, hi)`, an unbiased integer in [lo, hi] by rejection sampling. A Weyl sequence through all 2^32 states with MurmurHash3's finaliser on each, written with `nish:unsafe`'s `wrappingAdd`, `wrappingSub` and `wrappingMul` so that every answer is the same bits natively and under Node (`tests/link/std_random`; [`docs/LANGUAGE.md`](https://github.com/amritk/nish/blob/main/docs/LANGUAGE.md#random-numbers-rng-in-nishrandom)). A parallel body or a task that makes its own steps it without a shared write. Not for cryptography |
|
|
39
40
|
| [`cli.ts`](https://github.com/amritk/nish/blob/main/std/cli.ts) | a command-line tool's `--help`. `HelpMenu` builds it a line at a time — `header`, `usage`, `section`, `row(flag, placeholder, description)`, `note`, `value` — with descriptions lined up at the caller's columns, counted on the plain text, and answers it with `text()`. `Style` paints it in the basic ANSI colours, bold and faint, which the terminal's theme defines, or in bold and underline alone when `TERM` declares no colours; each role it paints (`heading`, `name`, `flag`, `meta`, `dim`) is a pair of SGR lists, `<role>Themed` and `<role>Mono`, a program may set. `Style` also paints any text: the sixteen colours (`red`, `brightBlack`, ...), their backgrounds (`bgRed`, `bgBrightRed`, ...), the attributes (`bold`, `faint`, `italic`, `underline`, `inverse`, `strikethrough`, which the failsafe keeps) and `sgr(text, codes)` for any parameter list, with the `SGR_` constants spelling the codes. `styleFor(fd, agent)` is the policy `nish --help` follows: plain unless the descriptor is a terminal, `NO_COLOR`, `FORCE_COLOR`, `CLICOLOR_FORCE`, `CLICOLOR` and `TERM=dumb` honoured, plain for an agent (`agentDriven`), forced colour always the theme's; it reads the environment and, for the terminal check, forks `/bin/sh` once, which runs `tput` by absolute path. `HelpMenu` and `Style` reach no capability. The compiler's own help is built with it through a relative import, so the module may use only what the last release compiles (`tests/link/std_cli`; [guide](../docs/guide/using-nish/standard-library.md#nishcli)) |
|
|
40
41
|
| [`cli-args.ts`](https://github.com/amritk/nish/blob/main/std/cli-args.ts) | a command tree for a large command-line tool. `Cli` declares it with a builder — `group`, `command(parent, name, id, description)`, `alias`, `flag`, `globalFlag`, `argument`, then `defaultValue`, `required`, `repeatable`, `negatable` and `epilog` on what they answer — every declaration an `i32` handle and every mistake a panic at its call. `parse(argv, start)` walks the arguments once and answers a `ParseResult`: the reached command's id and path, each flag's value with its `source` (the command line, its default or unset), the positionals and what follows `--`, or the first error with a did-you-mean. `finish` prints help (laid out by `HelpMenu`), the version or the error and exits 0 or 2. `parse` reaches no capability (`tests/link/std_cli_args`, `examples/api-cli`; [guide](../docs/guide/using-nish/standard-library.md#nishcli-args)) |
|
package/std/net/prefork.ts
CHANGED
|
@@ -83,10 +83,12 @@
|
|
|
83
83
|
* `preforkUdpCount(args)` (`--nish-prefork-udp=<fd>,<N>`, before the listener
|
|
84
84
|
* flag), and sets `QuicServerConfig.workerIndex` to k and `.workerCount` to N
|
|
85
85
|
* so every ID it issues starts with k. A connection then stays with its
|
|
86
|
-
* worker when the client's address or port changes.
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
86
|
+
* worker when the client's address or port changes. Port 0 is a port no UDP
|
|
87
|
+
* or TCP socket holds when the group binds, never another reuseport group's,
|
|
88
|
+
* which a port-0 bind with `SO_REUSEPORT` can be given on Linux. More than
|
|
89
|
+
* 255 workers panics; where the kernel refuses the steering (off Linux, or
|
|
90
|
+
* under a seccomp profile that refuses it) the supervisor says so on stderr
|
|
91
|
+
* and runs one worker.
|
|
90
92
|
*
|
|
91
93
|
* **How many.** `NISH_WORKERS` when it is a positive whole number, else
|
|
92
94
|
* `PreforkConfig.workers` when that is positive, else
|
|
@@ -454,6 +456,40 @@ export const preforkUdpCount = (args: string[]): i32 => {
|
|
|
454
456
|
return count > 0 ? count : 0
|
|
455
457
|
}
|
|
456
458
|
|
|
459
|
+
/**
|
|
460
|
+
* A port on `host` that no UDP or TCP socket holds, for a group asked for port
|
|
461
|
+
* 0; a negative errno when a plain UDP socket cannot bind, -98 (`EADDRINUSE`)
|
|
462
|
+
* after 16 ports whose TCP side was taken. The port of a plain UDP bind,
|
|
463
|
+
* closed again: Linux gives a port-0 bind with `SO_REUSEPORT` a port another
|
|
464
|
+
* reuseport socket of the same user may hold, and the two groups would become
|
|
465
|
+
* one, each handed the other's datagrams. Between this close and the group's
|
|
466
|
+
* bind another process can still take the port; a plain bind there makes the
|
|
467
|
+
* group's fail, and only a reuseport bind at that instant to that one port
|
|
468
|
+
* joins it.
|
|
469
|
+
*/
|
|
470
|
+
const preforkFreePort = (host: string): i32 => {
|
|
471
|
+
for (let k: i32 = 0; k < 16; k++) {
|
|
472
|
+
const probe: i32 = udpBind(host, 0, 0)
|
|
473
|
+
if (probe < 0) {
|
|
474
|
+
return probe
|
|
475
|
+
}
|
|
476
|
+
const port: i32 = netLocalPort(probe)
|
|
477
|
+
// Flags 0, not SO_REUSEPORT, which would succeed beside a same-user listener.
|
|
478
|
+
const listener: i32 = port > 0 ? tcpListenFlags(host, port, 1, 0) : -1
|
|
479
|
+
if (listener >= 0) {
|
|
480
|
+
netClose(listener)
|
|
481
|
+
}
|
|
482
|
+
netClose(probe)
|
|
483
|
+
if (port <= 0) {
|
|
484
|
+
return port < 0 ? port : -22
|
|
485
|
+
}
|
|
486
|
+
if (listener >= 0) {
|
|
487
|
+
return port
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
return -98
|
|
491
|
+
}
|
|
492
|
+
|
|
457
493
|
/**
|
|
458
494
|
* The UDP group of `listenUdp`, socket i for worker i, bound before anything
|
|
459
495
|
* else on the port: steered by connection ID (`udpBind` flag 4 with the count
|
|
@@ -467,9 +503,14 @@ const preforkBindUdp = (config: PreforkConfig, count: i32): i32[] => {
|
|
|
467
503
|
const out: i32[] = []
|
|
468
504
|
// Flag 1 (SO_REUSEPORT) and 4 (steer), the group's size in bits 8 to 15.
|
|
469
505
|
const steered: i32 = (count << 8) | 4 | 1
|
|
470
|
-
const
|
|
506
|
+
const asked: i32 = config.listenPort === 0 ? preforkFreePort(host) : config.listenPort
|
|
507
|
+
if (asked < 0) {
|
|
508
|
+
console.error(`nish/net/prefork: cannot find a free UDP and TCP port on ${host} (${asked})`)
|
|
509
|
+
return out
|
|
510
|
+
}
|
|
511
|
+
const first: i32 = udpBind(host, asked, count > 1 ? steered : 1)
|
|
471
512
|
if (first < 0 && count > 1) {
|
|
472
|
-
const plain: i32 = udpBind(host,
|
|
513
|
+
const plain: i32 = udpBind(host, asked, 1)
|
|
473
514
|
if (plain >= 0) {
|
|
474
515
|
console.error(`nish/net/prefork: QUIC steering refused (${first}), running one worker`)
|
|
475
516
|
out.push(plain)
|
|
@@ -477,16 +518,11 @@ const preforkBindUdp = (config: PreforkConfig, count: i32): i32[] => {
|
|
|
477
518
|
}
|
|
478
519
|
}
|
|
479
520
|
if (first < 0) {
|
|
480
|
-
console.error(`nish/net/prefork: cannot bind UDP ${host}:${
|
|
521
|
+
console.error(`nish/net/prefork: cannot bind UDP ${host}:${asked} (${first})`)
|
|
481
522
|
return out
|
|
482
523
|
}
|
|
483
524
|
out.push(first)
|
|
484
|
-
const port: i32 =
|
|
485
|
-
if (port <= 0) {
|
|
486
|
-
console.error(`nish/net/prefork: cannot read the UDP port bound (${port})`)
|
|
487
|
-
netClose(first)
|
|
488
|
-
return []
|
|
489
|
-
}
|
|
525
|
+
const port: i32 = asked
|
|
490
526
|
for (let i: i32 = 1; i < count; i++) {
|
|
491
527
|
const fd: i32 = udpBind(host, port, steered)
|
|
492
528
|
if (fd < 0) {
|