@amritk/nish-aarch64-linux 0.37.0 → 0.39.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/INSTALL.md +28 -16
- package/bin/nish +0 -0
- package/docs/AI.md +380 -47
- package/package.json +1 -1
- package/runtime/core/arrays.d.ts +4 -0
- package/runtime/core/arrays.mjs +33 -0
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +645 -66
- package/runtime/nish.h +101 -15
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/runtime-aes.c +12 -4
- package/runtime/runtime-async.c +267 -51
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-cross.c +2 -1
- package/runtime/runtime-net.c +32 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +170 -8
- package/runtime/runtime-sandbox.c +377 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-text.c +1068 -0
- package/runtime/runtime-wasm.c +18 -3
- package/runtime/runtime.c +55 -961
- package/runtime/vite.mjs +48 -11
- package/scripts/build.sh +113 -36
- package/std/LICENSE-fdlibm +8 -0
- package/std/README.md +7 -4
- package/std/array.ts +266 -0
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/p256.ts +247 -74
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +35 -9
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +113 -10
- package/std/host/text.ts +86 -0
- package/std/json-tokenizer.ts +20 -0
- package/std/json-typed.ts +217 -8
- package/std/math.ts +743 -41
- package/std/net/dns-resolver.ts +46 -6
- package/std/net/dns.ts +50 -13
- package/std/net/fetch.ts +691 -129
- package/std/net/handler.ts +142 -16
- package/std/net/http-compress.ts +17 -9
- package/std/net/http-fields.ts +19 -4
- package/std/net/http1-codec.ts +517 -154
- package/std/net/http1-server.ts +508 -25
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +84 -0
- package/std/net/postgres-async.ts +2223 -0
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +129 -11
- package/std/net/prefork.ts +195 -11
- package/std/net/quic-datagram.ts +30 -0
- package/std/net/quic-listener.ts +38 -12
- package/std/net/quic-packet.ts +19 -6
- package/std/net/quic-recovery.ts +134 -19
- package/std/net/quic-stream.ts +174 -36
- package/std/net/quic.ts +418 -18
- package/std/net/router.ts +39 -17
- package/std/net/serve.ts +867 -0
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/websocket.ts +55 -18
- package/std/net/webtransport.ts +179 -1
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/secret.ts +26 -59
- package/std/text.ts +464 -5
package/runtime/nish.h
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* runtime next to compiled Nish modules. Everything here is a contract shared
|
|
5
5
|
* with `src/runtime.ts` (the IR side) and the implementation, which is
|
|
6
6
|
* several translation units: `runtime/runtime.c` holds the core every program
|
|
7
|
-
* touches — the arena,
|
|
7
|
+
* touches — the arena, arrays, the panics — `runtime/runtime-text.c` holds
|
|
8
|
+
* strings and number formatting (freestanding, so a wasm module links it too),
|
|
8
9
|
* `runtime/runtime-os.c` holds everything that wraps a system call: the file
|
|
9
10
|
* functions, the directory and subprocess calls, `nish_getenv`, the monotonic
|
|
10
11
|
* clock, `nish_platform` / `nish_arch` — and runtime-parallel.c, runtime-host.c,
|
|
@@ -12,7 +13,8 @@
|
|
|
12
13
|
* time), runtime-aes.c (AES-GCM on the CPU's AES instructions),
|
|
13
14
|
* runtime-sha256.c (SHA-256 on the CPU's SHA instructions), runtime-chacha20.c
|
|
14
15
|
* (ChaCha20's keystream on AVX2 or NEON vectors), runtime-checksum.c
|
|
15
|
-
* (CRC-32 on its carry-less multiply or CRC instructions
|
|
16
|
+
* (CRC-32 on its carry-less multiply or CRC instructions, Adler-32 on SSSE3 or
|
|
17
|
+
* NEON) and runtime-term.c
|
|
16
18
|
* (stdin and the terminal, `nish:term`) split off
|
|
17
19
|
* the same way since. They are separate so that each carries its own measured
|
|
18
20
|
* code-size ceiling (docs/wp7-runtime.md, "Runtime additions and budget"); a
|
|
@@ -51,7 +53,8 @@ extern "C" {
|
|
|
51
53
|
* ELF refuses to link that against a non-TLS definition. `scripts/build.sh
|
|
52
54
|
* --threads` passes the macro to every input, which is how `nish --threads
|
|
53
55
|
* --link` keeps the two halves in step. The same definition is in
|
|
54
|
-
* runtime/runtime.c and runtime/runtime-wasm.c.
|
|
56
|
+
* runtime/runtime.c and runtime/runtime-wasm.c, and runtime/runtime-text.c
|
|
57
|
+
* repeats it for its inlined allocation fast path. */
|
|
55
58
|
#ifdef NISH_THREADS
|
|
56
59
|
#define NISH_TLS _Thread_local
|
|
57
60
|
#else
|
|
@@ -94,7 +97,8 @@ static inline __attribute__((unused)) const char *nish_cpath(const nish_str *s)
|
|
|
94
97
|
* `off` and `cap` are `uint64_t` and not `size_t` because that `i64` is what
|
|
95
98
|
* the inlined fast path bumps and compares on every target: under wasm32 a
|
|
96
99
|
* `size_t` pair would put them at bytes 4 and 8 while compiled code reads 8
|
|
97
|
-
* and 16. runtime.c and runtime-wasm.c static-assert these offsets
|
|
100
|
+
* and 16. runtime.c and runtime-wasm.c static-assert these offsets, and
|
|
101
|
+
* runtime-text.c, which keeps its own copy of the struct, asserts all four. */
|
|
98
102
|
struct nish_arena {
|
|
99
103
|
char *buf;
|
|
100
104
|
uint64_t off;
|
|
@@ -134,6 +138,15 @@ void *nish_arena_keep(uint64_t mark, void *p);
|
|
|
134
138
|
/* Bytes bumped in the current chunk (`Arena.used()`); a steady-state loop keeps it flat. */
|
|
135
139
|
uint64_t nish_arena_used(void);
|
|
136
140
|
|
|
141
|
+
/* ---- cErrno (WP27 S5, docs/wp27-ffi.md §10) -----------------------------
|
|
142
|
+
* A program that names `cErrno` stores 0 through `nish_errno_ptr()` right
|
|
143
|
+
* before every `declare function` call and copies errno into
|
|
144
|
+
* `nish_errno_slot` right after it, before any result marshalling; `cErrno()`
|
|
145
|
+
* loads the slot. The slot is per thread under NISH_THREADS, so the runtime's
|
|
146
|
+
* own allocation and string copying cannot disturb it. */
|
|
147
|
+
extern NISH_TLS int32_t nish_errno_slot;
|
|
148
|
+
int *nish_errno_ptr(void);
|
|
149
|
+
|
|
137
150
|
/* ---- Regions (runtime-region.c) -----------------------------------------
|
|
138
151
|
* Arenas made, entered, left and freed one at a time
|
|
139
152
|
* (docs/wp46-long-running-memory.md). A handle is a nonzero `uint64_t`: a
|
|
@@ -146,12 +159,22 @@ uint64_t nish_arena_used(void);
|
|
|
146
159
|
* already entered panics. `nish_region_leave` must name the region entered
|
|
147
160
|
* last on this thread, puts back the arena its enter displaced and records
|
|
148
161
|
* `root`. `nish_region_free` frees every chunk of a region that is not
|
|
149
|
-
* entered and makes its handle stale.
|
|
150
|
-
*
|
|
162
|
+
* entered and makes its handle stale. `nish_region_adopt` instead moves the
|
|
163
|
+
* chunks of a region that is not entered onto the thread's current arena, so
|
|
164
|
+
* they live on until that arena releases below them, and makes its handle
|
|
165
|
+
* stale: an `awaitAll` lane's region, once the lane has answered; it panics
|
|
166
|
+
* on a child region or a region with children. `nish_region_child_new` makes
|
|
167
|
+
* a region whose parent is the one entered last on this thread, and panics
|
|
168
|
+
* when none is; freeing a region frees its children, deepest first, and
|
|
169
|
+
* panics before freeing anything when any of them is entered. The wasm
|
|
170
|
+
* profile has none of these (scripts/build.sh refuses IR that names them);
|
|
171
|
+
* wasi has. */
|
|
151
172
|
uint64_t nish_region_new(void);
|
|
173
|
+
uint64_t nish_region_child_new(void);
|
|
152
174
|
void *nish_region_enter(uint64_t r);
|
|
153
175
|
void nish_region_leave(uint64_t r, void *root);
|
|
154
176
|
void nish_region_free(uint64_t r);
|
|
177
|
+
void nish_region_adopt(uint64_t r);
|
|
155
178
|
|
|
156
179
|
/* ---- The async executor (runtime-async.c) --------------------------------
|
|
157
180
|
* Linked only into a program whose IR names `@nish_async_`
|
|
@@ -159,14 +182,23 @@ void nish_region_free(uint64_t r);
|
|
|
159
182
|
* a node for a task of `kind` whose frame is `bytes` long, the frame 32 bytes
|
|
160
183
|
* in, with a region of its own; `nish_async_enter` makes a node the running
|
|
161
184
|
* task and enters its region, answering the task it displaced, which
|
|
162
|
-
* `nish_async_leave` restores; `nish_async_done`
|
|
163
|
-
* region and reuses its node. `nish_async_wait_fd` (`dir` 0
|
|
164
|
-
* writable) and `nish_async_sleep` answer a token
|
|
165
|
-
* pending, 1 ready, 0 failed. `nish_async_next`
|
|
166
|
-
* resume, blocking in the kernel until one is ready,
|
|
167
|
-
* ready and nothing waits. `nish_async_stat` answers
|
|
168
|
-
* completions dropped because their task had finished,
|
|
169
|
-
* finished.
|
|
185
|
+
* `nish_async_leave` restores; `nish_async_done` empties a finished task's
|
|
186
|
+
* region and reuses its node, region and all. `nish_async_wait_fd` (`dir` 0
|
|
187
|
+
* readable, 1 writable) and `nish_async_sleep` answer a token
|
|
188
|
+
* `nish_async_poll` reads: -1 pending, 1 ready, 0 failed. `nish_async_next`
|
|
189
|
+
* answers the next task to resume, blocking in the kernel until one is ready,
|
|
190
|
+
* or null when nothing is ready and nothing waits. `nish_async_stat` answers
|
|
191
|
+
* 0: nodes allocated, 1: completions dropped because their task had finished,
|
|
192
|
+
* 2: spawned tasks not yet finished.
|
|
193
|
+
*
|
|
194
|
+
* Cancellation is cooperative. A task's handle (`nish_async_task_id`) is its
|
|
195
|
+
* slot and generation, and `main`'s is 0. `nish_async_cancel` flags a task and
|
|
196
|
+
* fires the waits it is parked on, which then read 0, as does every later wait
|
|
197
|
+
* it makes; it answers 1 when it flagged a live task other than `main`.
|
|
198
|
+
* `nish_async_cancelled` answers 1 inside a flagged task, and
|
|
199
|
+
* `nish_async_cancel_after` arms a timer that cancels the task in `ms`.
|
|
200
|
+
* `nish_async_wait_fd_within` is `nish_async_wait_fd` that reads 0 when `ms`
|
|
201
|
+
* pass first. */
|
|
170
202
|
void *nish_async_spawn(int32_t kind, uint64_t bytes);
|
|
171
203
|
void *nish_async_enter(void *node);
|
|
172
204
|
void nish_async_leave(void *node, void *prev);
|
|
@@ -177,6 +209,11 @@ int32_t nish_async_poll(uint64_t token);
|
|
|
177
209
|
void *nish_async_next(void);
|
|
178
210
|
void *nish_async_copy_str(const void *s);
|
|
179
211
|
int32_t nish_async_stat(int32_t which);
|
|
212
|
+
uint64_t nish_async_wait_fd_within(int32_t fd, int32_t dir, int32_t ms);
|
|
213
|
+
int64_t nish_async_task_id(void);
|
|
214
|
+
int32_t nish_async_cancel(int64_t handle);
|
|
215
|
+
int32_t nish_async_cancelled(void);
|
|
216
|
+
int32_t nish_async_cancel_after(int64_t handle, int32_t ms);
|
|
180
217
|
|
|
181
218
|
/* ---- String operations ------------------------------------------------- */
|
|
182
219
|
/* Copy `len` bytes into a new arena string (`bytes` need not be terminated). */
|
|
@@ -248,7 +285,7 @@ nish_str *nish_str_from_i32(int32_t v);
|
|
|
248
285
|
nish_str *nish_str_from_f64(double v);
|
|
249
286
|
/* The bytes `nish_str_from_f64` would hold, written into `out` with no NUL;
|
|
250
287
|
returns the length. 32 bytes is room to spare: the longest spelling,
|
|
251
|
-
-0.0000012345678901234567, is 25 (runtime.c says why, and runtime-test.c
|
|
288
|
+
-0.0000012345678901234567, is 25 (runtime-text.c says why, and runtime-test.c
|
|
252
289
|
pins it). For a caller that writes numbers into its own buffer. */
|
|
253
290
|
#define NISH_F64_MAX 32
|
|
254
291
|
int nish_f64_format(double v, char out[NISH_F64_MAX]);
|
|
@@ -463,6 +500,21 @@ int32_t nish_crc32_hw(void);
|
|
|
463
500
|
(u8[]), conditioned as zlib's crc32(): 0 starts a message, and the answer
|
|
464
501
|
continues one. The caller has checked 0 <= off <= off + len <= buf.len. */
|
|
465
502
|
uint32_t nish_crc32_update(uint32_t crc, const nish_array *buf, int32_t off, int32_t len);
|
|
503
|
+
/* ---- Adler-32 on SSSE3 or NEON vectors ------------------------------------
|
|
504
|
+
* runtime/runtime-checksum.c as well: the compiler calls the kernel in place
|
|
505
|
+
* of std/compress/checksum.ts's private `adler32Walk`, behind
|
|
506
|
+
* `nish_adler32_hw`, and calls the Nish loop itself when that answers 0. */
|
|
507
|
+
/* 1 when this process runs the kernel, 0 when it runs the Nish loop. Resolved
|
|
508
|
+
on the first call and fixed from then on: `NISH_ADLER32=soft` pins 0; unset
|
|
509
|
+
or any other value is the best this machine has, CPUID leaf 1's SSSE3 bit on
|
|
510
|
+
x86-64 and always the kernel on a little-endian AArch64, where NEON is
|
|
511
|
+
baseline. Always 0 on other targets, wasm32 among them. */
|
|
512
|
+
int32_t nish_adler32_hw(void);
|
|
513
|
+
/* The Adler-32 (RFC 1950) `adler` extended over buf[off..off+len) (u8[]):
|
|
514
|
+
1 starts a message. An empty window answers `adler` as it came; any other
|
|
515
|
+
answer is reduced mod 65521 in both halves. The caller has checked
|
|
516
|
+
0 <= off <= off + len <= buf.len. */
|
|
517
|
+
uint32_t nish_adler32_update(uint32_t adler, const nish_array *buf, int32_t off, int32_t len);
|
|
466
518
|
|
|
467
519
|
/* `secureZero(bytes)` (#385): sets every byte of `bytes` to zero through
|
|
468
520
|
* `volatile` stores, out of line, so that neither `-O2` nor `-flto` can drop
|
|
@@ -525,6 +577,23 @@ nish_array *nish_readdir(const nish_str *path);
|
|
|
525
577
|
* and the bytes live in the arena, so copy what you keep before the next reset
|
|
526
578
|
* or release. */
|
|
527
579
|
nish_array *nish_read_file_bytes(const nish_str *path);
|
|
580
|
+
/* `fileOpen(path)`, `fileReadAt(fd, buf, off, len, pos)`, `fileSize(fd)` and
|
|
581
|
+
* `fileClose(fd)` (WP44): a regular file read a window at a time. `fileOpen`
|
|
582
|
+
* answers a descriptor (`O_RDONLY | O_CLOEXEC | O_NONBLOCK`, so a FIFO with
|
|
583
|
+
* no writer does not hang the open) or -21 for a directory and -22
|
|
584
|
+
* for anything else that is not a regular file. `nish_file_read_at` reads at
|
|
585
|
+
* most `len` bytes from byte `pos` into `buf[off, off + len)`, which the
|
|
586
|
+
* compiled call has range-checked; it answers the count, 0 at or past the end
|
|
587
|
+
* and for `len` 0, and -22 for a `pos` below 0 or a `pos + len` past
|
|
588
|
+
* 2^63 - 1. A negative `fd` is -9 to the three that take one, before any
|
|
589
|
+
* call. `nish_file_size` answers `st_size` now; `nish_file_close` closes
|
|
590
|
+
* and is never retried. None allocates. Every failure is a negated errno in
|
|
591
|
+
* Linux's numbering on every platform (-2, -6, -9, -13, -20, -21, -22, -23,
|
|
592
|
+
* -24, -29, -36, -40), and any other is -5; a path holding a NUL is -2. */
|
|
593
|
+
int32_t nish_file_open(const nish_str *path);
|
|
594
|
+
int32_t nish_file_read_at(int32_t fd, nish_array *buf, int64_t off, int64_t len, int64_t pos);
|
|
595
|
+
int64_t nish_file_size(int32_t fd);
|
|
596
|
+
int32_t nish_file_close(int32_t fd);
|
|
528
597
|
/* `renameSync(from, to)`, `unlinkSync(path)` and `writeFileBytesSync(path,
|
|
529
598
|
* bytes)`: rename(2), unlink(2), and every element of a `u8[]` written to a
|
|
530
599
|
* file opened as `nish_write_file` opens it (`O_TRUNC`, 0644, a link refused).
|
|
@@ -592,6 +661,14 @@ int32_t nish_spawn_child(const nish_array *argv);
|
|
|
592
661
|
* copied; SIGPIPE is blocked on the calling thread for the call, a SIGPIPE the
|
|
593
662
|
* call raised is consumed, and the thread's mask is restored after it. */
|
|
594
663
|
int32_t nish_spawn_piped(const nish_array *argv, const nish_array *in, nish_array *out, nish_array *err);
|
|
664
|
+
/* `spawnPipedWithin(argv, stdin, stdout, stderr, timeoutMs)`: `nish_spawn_piped`
|
|
665
|
+
* with a deadline of `timeout_ms` milliseconds on CLOCK_MONOTONIC, a negative
|
|
666
|
+
* one none (`nish_spawn_piped` is this with -1). The same answers, plus -3 for
|
|
667
|
+
* a run the deadline ended: the child, its pid alone, is killed with SIGKILL
|
|
668
|
+
* and reaped, and both arrays keep the bytes read before the kill. The wait for
|
|
669
|
+
* the child's exit is bounded by the same deadline. -1 always under WASI. */
|
|
670
|
+
int32_t nish_spawn_piped_within(const nish_array *argv, const nish_array *in, nish_array *out, nish_array *err,
|
|
671
|
+
int32_t timeout_ms);
|
|
595
672
|
/* `spawnWithFds(argv, fds)`: `nish_spawn_child` with the child holding
|
|
596
673
|
* `fds[i]` (element type `int32_t`) as descriptor `3 + i`, close-on-exec
|
|
597
674
|
* cleared there. More than 64 descriptors or a negative one is -EINVAL before
|
|
@@ -927,6 +1004,15 @@ int64_t nish_cpu_online(void);
|
|
|
927
1004
|
`nish_cpu_prime` caches the pool's CPU count while /sys is still readable. */
|
|
928
1005
|
void nish_sandbox(int32_t caps, int32_t mode, const char *read_dirs, const char *write_dirs, const char *exec_paths);
|
|
929
1006
|
void nish_sandbox_check(void);
|
|
1007
|
+
/* SBX-1: set by `nish_sandbox` when the program has an exec allowlist;
|
|
1008
|
+
`nish_spawn_start` (runtime-os.c) then leaves the spawn to it. It checks
|
|
1009
|
+
`file` as `posix_spawnp` would resolve it, starts the child with the file
|
|
1010
|
+
actions `actions` (a `posix_spawn_file_actions_t`) and `argv`, holds it to
|
|
1011
|
+
that one exec, and answers its pid or a negative errno. The actions' `dup2`s
|
|
1012
|
+
read the `n` descriptors of `dup` (null for none) and write `base` onward;
|
|
1013
|
+
with the redirects of 1 and 2, nothing they write is above `base + n + 2`. */
|
|
1014
|
+
extern int32_t (*nish_spawn_sandboxed)(const char *file, const int *dup, int base, int n, const void *actions,
|
|
1015
|
+
char **argv);
|
|
930
1016
|
void nish_cpu_prime(void);
|
|
931
1017
|
void nish_parallel_range(nish_par_body body, void *ctx, int64_t len, int64_t grain);
|
|
932
1018
|
/* Keeps every later region and join on threads created and joined per call,
|
package/runtime/nish.mjs
CHANGED
|
@@ -89,6 +89,12 @@ provide("panic", shim.panic);
|
|
|
89
89
|
provide("readFileSync", shim.readFileSync);
|
|
90
90
|
provide("readFileSyncOrNull", shim.readFileSyncOrNull);
|
|
91
91
|
provide("readFileBytesSync", shim.readFileBytesSync);
|
|
92
|
+
// WP44: a regular file read a window at a time; the errors are Linux's
|
|
93
|
+
// negated errno on every platform, as `runtime-os.c` answers them.
|
|
94
|
+
provide("fileOpen", shim.fileOpen);
|
|
95
|
+
provide("fileReadAt", shim.fileReadAt);
|
|
96
|
+
provide("fileSize", shim.fileSize);
|
|
97
|
+
provide("fileClose", shim.fileClose);
|
|
92
98
|
provide("writeFileSync", shim.writeFileSync);
|
|
93
99
|
provide("appendFileSync", shim.appendFileSync);
|
|
94
100
|
provide("writeFileBytesSync", shim.writeFileBytesSync);
|
|
@@ -104,6 +110,7 @@ provide("realpathSync", shim.realpathSync);
|
|
|
104
110
|
provide("spawnSync", shim.spawnSync);
|
|
105
111
|
provide("spawnSyncTo", shim.spawnSyncTo);
|
|
106
112
|
provide("spawnPiped", shim.spawnPiped);
|
|
113
|
+
provide("spawnPipedWithin", shim.spawnPipedWithin);
|
|
107
114
|
// WP34 N12. `spawn`, `spawnWithFds` and `waitPid` throw, naming themselves: a
|
|
108
115
|
// child is reaped by Node's event loop. `kill`, `availableParallelism` and
|
|
109
116
|
// `totalMemory` answer.
|
|
@@ -342,29 +349,85 @@ const unsafeSource = UNSAFE_EXPORTS.map((name) => `export const ${name} = global
|
|
|
342
349
|
// table, keyed by its handle (a BigInt, as every `i64` is here), `enter` runs
|
|
343
350
|
// its function on the root, and `free` drops the entry. The handle checks the
|
|
344
351
|
// native runtime makes are made here too, so a freed handle or a second
|
|
345
|
-
// `enter` throws where the native program stops.
|
|
352
|
+
// `enter` throws where the native program stops. A child (`child`,
|
|
353
|
+
// `childWith`) records the region entered last as its parent, as
|
|
354
|
+
// `runtime/runtime-region.c` does; `free` of a parent drops its children,
|
|
355
|
+
// deepest first, and throws before dropping anything if any is entered.
|
|
346
356
|
const regionSource = `
|
|
347
357
|
const roots = new Map();
|
|
348
358
|
const entered = new Set();
|
|
359
|
+
const parents = new Map();
|
|
360
|
+
const children = new Map();
|
|
361
|
+
const stack = [];
|
|
349
362
|
let next = 0n;
|
|
350
363
|
const rootOf = (r) => {
|
|
351
364
|
if (!roots.has(r)) throw new Error("nish:region: the region is freed or was never made");
|
|
352
365
|
if (entered.has(r)) throw new Error("nish:region: the region is entered already");
|
|
353
366
|
return roots.get(r);
|
|
354
367
|
};
|
|
368
|
+
const inside = (r, g) => {
|
|
369
|
+
entered.add(r);
|
|
370
|
+
stack.push(r);
|
|
371
|
+
try { return g(); } finally { stack.pop(); entered.delete(r); }
|
|
372
|
+
};
|
|
355
373
|
const run = (r, f, arg, withArg) => {
|
|
356
374
|
const root = rootOf(r);
|
|
357
|
-
|
|
358
|
-
|
|
375
|
+
return inside(r, () => (withArg ? f(root, arg) : f(root)));
|
|
376
|
+
};
|
|
377
|
+
const make = (parent, build) => {
|
|
378
|
+
next += 1n;
|
|
379
|
+
const r = next;
|
|
380
|
+
roots.set(r, null);
|
|
381
|
+
children.set(r, new Set());
|
|
382
|
+
if (parent !== 0n) {
|
|
383
|
+
parents.set(r, parent);
|
|
384
|
+
children.get(parent).add(r);
|
|
385
|
+
}
|
|
386
|
+
try {
|
|
387
|
+
roots.set(r, inside(r, build));
|
|
388
|
+
} catch (e) {
|
|
389
|
+
roots.delete(r);
|
|
390
|
+
children.delete(r);
|
|
391
|
+
parents.delete(r);
|
|
392
|
+
if (parent !== 0n) children.get(parent).delete(r);
|
|
393
|
+
throw e;
|
|
394
|
+
}
|
|
395
|
+
return r;
|
|
396
|
+
};
|
|
397
|
+
const parentNow = () => {
|
|
398
|
+
if (stack.length === 0) throw new Error("nish:region: a child region made with no region entered");
|
|
399
|
+
return stack[stack.length - 1];
|
|
400
|
+
};
|
|
401
|
+
// The regions under r and r, each after every region under it: deepest first.
|
|
402
|
+
const subtree = (r) => {
|
|
403
|
+
const out = [];
|
|
404
|
+
const todo = [r];
|
|
405
|
+
while (todo.length > 0) {
|
|
406
|
+
const x = todo.pop();
|
|
407
|
+
out.push(x);
|
|
408
|
+
for (const c of children.get(x)) todo.push(c);
|
|
409
|
+
}
|
|
410
|
+
return out.reverse();
|
|
359
411
|
};
|
|
360
412
|
export class Region {}
|
|
361
|
-
export const region = (f) =>
|
|
362
|
-
export const regionWith = (arg, f) =>
|
|
413
|
+
export const region = (f) => make(0n, () => f());
|
|
414
|
+
export const regionWith = (arg, f) => make(0n, () => f(arg));
|
|
415
|
+
export const child = (f) => make(parentNow(), () => f());
|
|
416
|
+
export const childWith = (arg, f) => make(parentNow(), () => f(arg));
|
|
363
417
|
export const enter = (r, f) => run(r, f, undefined, false);
|
|
364
418
|
export const enterWith = (r, arg, f) => run(r, f, arg, true);
|
|
365
419
|
export const free = (r) => {
|
|
366
420
|
if (entered.has(r)) throw new Error("nish:region: the region is freed while it is entered");
|
|
367
|
-
if (!roots.
|
|
421
|
+
if (!roots.has(r)) throw new Error("nish:region: the region is freed or was never made");
|
|
422
|
+
const doomed = subtree(r);
|
|
423
|
+
if (doomed.some((d) => entered.has(d))) throw new Error("nish:region: the region is freed while it is entered");
|
|
424
|
+
const parent = parents.get(r);
|
|
425
|
+
if (parent !== undefined) children.get(parent).delete(r);
|
|
426
|
+
for (const d of doomed) {
|
|
427
|
+
roots.delete(d);
|
|
428
|
+
children.delete(d);
|
|
429
|
+
parents.delete(d);
|
|
430
|
+
}
|
|
368
431
|
};
|
|
369
432
|
`;
|
|
370
433
|
const TERM_EXPORTS = ["readStdin", "waitStdin", "isTerminal", "terminalSize", "setRawMode", "setTerminalRestore"];
|
package/runtime/node.mjs
CHANGED
|
@@ -132,6 +132,104 @@ function putFileMode(path, data, flag, mode) {
|
|
|
132
132
|
return answer;
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
+
/**
|
|
136
|
+
* WP44's file errors as `runtime-os.c`'s `nish_fs_err` answers them: the
|
|
137
|
+
* negated Linux errno of the set docs/wp44-streaming-files.md §3 names, by
|
|
138
|
+
* Node's code rather than the platform's number, so a macOS run answers what
|
|
139
|
+
* Linux does. Any other failure is -5 (`EIO`).
|
|
140
|
+
*/
|
|
141
|
+
const FILE_ERRNO = {
|
|
142
|
+
ENOENT: -2,
|
|
143
|
+
ENXIO: -6,
|
|
144
|
+
EBADF: -9,
|
|
145
|
+
EACCES: -13,
|
|
146
|
+
ENOTDIR: -20,
|
|
147
|
+
EISDIR: -21,
|
|
148
|
+
EINVAL: -22,
|
|
149
|
+
ENFILE: -23,
|
|
150
|
+
EMFILE: -24,
|
|
151
|
+
ESPIPE: -29,
|
|
152
|
+
ENAMETOOLONG: -36,
|
|
153
|
+
ELOOP: -40,
|
|
154
|
+
};
|
|
155
|
+
const fileErr = (e) => FILE_ERRNO[e.code] ?? -5;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* A negative descriptor is -9 (`EBADF`), as the kernel answers it natively:
|
|
159
|
+
* Node throws `ERR_OUT_OF_RANGE` for one before it asks, which `fileErr` would
|
|
160
|
+
* make -5.
|
|
161
|
+
*/
|
|
162
|
+
const EBADF = -9;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* `fileOpen(path)` (WP44): a descriptor on a regular file opened read-only and
|
|
166
|
+
* non-blocking, or a negative errno; a directory is -21 and any other kind -22,
|
|
167
|
+
* closed again first. A path holding a NUL is -2, as `nish_cpath` answers it.
|
|
168
|
+
*/
|
|
169
|
+
function fileOpen(path) {
|
|
170
|
+
if (path.includes("\0")) return -2;
|
|
171
|
+
let fd;
|
|
172
|
+
try {
|
|
173
|
+
fd = fs.openSync(path, fs.constants.O_RDONLY | (fs.constants.O_NONBLOCK ?? 0));
|
|
174
|
+
} catch (e) {
|
|
175
|
+
return fileErr(e);
|
|
176
|
+
}
|
|
177
|
+
let r;
|
|
178
|
+
try {
|
|
179
|
+
const st = fs.fstatSync(fd);
|
|
180
|
+
r = st.isFile() ? 0 : st.isDirectory() ? -21 : -22;
|
|
181
|
+
} catch (e) {
|
|
182
|
+
r = fileErr(e);
|
|
183
|
+
}
|
|
184
|
+
if (r === 0) return fd;
|
|
185
|
+
errnoOr(0, () => fs.closeSync(fd));
|
|
186
|
+
return r;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** The scratch a positional read lands in before its bytes are copied to the array. */
|
|
190
|
+
let fileScratch = Buffer.alloc(0);
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* `fileReadAt(fd, buf, off, len, pos)` (WP44): up to `len` bytes from offset
|
|
194
|
+
* `pos` into `buf[off..]`, the count (0 at end of file) or a negative errno.
|
|
195
|
+
* The window is checked by the caller (`core/host.mjs`); `pos` is a bigint.
|
|
196
|
+
*/
|
|
197
|
+
function fileReadAt(fd, buf, off, len, pos) {
|
|
198
|
+
if (pos < 0n || BigInt(len) > 0x7fffffffffffffffn - pos) return -22;
|
|
199
|
+
if (len === 0) return 0;
|
|
200
|
+
if (fd < 0) return EBADF;
|
|
201
|
+
let n;
|
|
202
|
+
try {
|
|
203
|
+
if (fileScratch.length < len) fileScratch = Buffer.alloc(len);
|
|
204
|
+
n = fs.readSync(fd, fileScratch, 0, len, pos);
|
|
205
|
+
} catch (e) {
|
|
206
|
+
return fileErr(e);
|
|
207
|
+
}
|
|
208
|
+
for (let i = 0; i < n; i++) buf[off + i] = fileScratch[i];
|
|
209
|
+
return n;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** `fileSize(fd)` (WP44): the size in bytes as a bigint, or a negative errno. */
|
|
213
|
+
function fileSize(fd) {
|
|
214
|
+
if (fd < 0) return BigInt(EBADF);
|
|
215
|
+
try {
|
|
216
|
+
return fs.fstatSync(fd, { bigint: true }).size;
|
|
217
|
+
} catch (e) {
|
|
218
|
+
return BigInt(fileErr(e));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** `fileClose(fd)` (WP44): 0, or a negative errno. */
|
|
223
|
+
function fileClose(fd) {
|
|
224
|
+
if (fd < 0) return EBADF;
|
|
225
|
+
try {
|
|
226
|
+
fs.closeSync(fd);
|
|
227
|
+
return 0;
|
|
228
|
+
} catch (e) {
|
|
229
|
+
return fileErr(e);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
135
233
|
/** `writeFileSyncMode(path, data, mode)`: `WRITE_FLAGS`, so a link at the path is refused. */
|
|
136
234
|
const writeFileSyncMode = (path, data, mode) => putFileMode(path, data, WRITE_FLAGS, mode);
|
|
137
235
|
|
|
@@ -320,6 +418,18 @@ const PIPED_CAP = 16 * 1024 * 1024;
|
|
|
320
418
|
* A child that exits without reading its stdin is not an error on either side.
|
|
321
419
|
*/
|
|
322
420
|
function spawnPiped(argv, input) {
|
|
421
|
+
return spawnPipedWithin(argv, input, -1);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* `spawnPipedWithin(argv, stdin, timeoutMs)`'s run: `spawnPiped`'s, with
|
|
426
|
+
* Node's `timeout` set when `timeoutMs` is not negative. Node kills the child
|
|
427
|
+
* with `SIGKILL` when it passes and reports `ETIMEDOUT`, which is -3 here as
|
|
428
|
+
* in the runtime, with the bytes Node read before the kill kept. Node reads a
|
|
429
|
+
* `timeout` of 0 as none, so a deadline of zero is passed as 1 ms: already
|
|
430
|
+
* past for any child that has to start a program.
|
|
431
|
+
*/
|
|
432
|
+
function spawnPipedWithin(argv, input, timeoutMs) {
|
|
323
433
|
if (argv.length === 0) return [-1, [], []];
|
|
324
434
|
let r;
|
|
325
435
|
// Node throws, rather than reporting, for an empty command or a NUL in any
|
|
@@ -329,6 +439,7 @@ function spawnPiped(argv, input) {
|
|
|
329
439
|
input: Uint8Array.from(input),
|
|
330
440
|
maxBuffer: PIPED_CAP,
|
|
331
441
|
killSignal: "SIGKILL",
|
|
442
|
+
...(timeoutMs >= 0 ? { timeout: Math.max(timeoutMs, 1) } : {}),
|
|
332
443
|
});
|
|
333
444
|
} catch {
|
|
334
445
|
return [-1, [], []];
|
|
@@ -338,6 +449,7 @@ function spawnPiped(argv, input) {
|
|
|
338
449
|
const err = bytes(r.stderr);
|
|
339
450
|
// `EPIPE` is the child closing its stdin unread, which Node reports with
|
|
340
451
|
// the status it still waited for; the runtime ignores it the same way.
|
|
452
|
+
if (r.error !== undefined && r.error.code === "ETIMEDOUT") return [-3, out, err];
|
|
341
453
|
if (r.error !== undefined && r.error.code !== "EPIPE") return [r.error.code === "ENOBUFS" ? -2 : -1, out, err];
|
|
342
454
|
if (r.signal !== null && r.signal !== undefined) return [128 + (os.constants.signals[r.signal] ?? 0), out, err];
|
|
343
455
|
return [r.status === null ? -1 : r.status, out, err];
|
|
@@ -463,6 +575,10 @@ const CALLS = {
|
|
|
463
575
|
readFileSync,
|
|
464
576
|
readFileSyncOrNull,
|
|
465
577
|
readFileBytesSync,
|
|
578
|
+
fileOpen,
|
|
579
|
+
fileReadAt,
|
|
580
|
+
fileSize,
|
|
581
|
+
fileClose,
|
|
466
582
|
writeFileSync,
|
|
467
583
|
appendFileSync,
|
|
468
584
|
writeFileBytesSync,
|
|
@@ -482,6 +598,7 @@ const CALLS = {
|
|
|
482
598
|
spawnSync,
|
|
483
599
|
spawnSyncTo,
|
|
484
600
|
spawnPiped,
|
|
601
|
+
spawnPipedWithin,
|
|
485
602
|
kill,
|
|
486
603
|
availableParallelism,
|
|
487
604
|
totalMemory,
|
package/runtime/runtime-aes.c
CHANGED
|
@@ -19,8 +19,9 @@
|
|
|
19
19
|
* Nish code in std/crypto/aes.ts instead: the compiler keeps that code and
|
|
20
20
|
* branches on `nish_aes_hw` at each call, so a table-free C AES here would only
|
|
21
21
|
* be a second copy of the code under review. Every other target, wasm32
|
|
22
|
-
* included,
|
|
23
|
-
*
|
|
22
|
+
* included, and a toolchain without the intrinsic headers compile
|
|
23
|
+
* `nish_aes_hw` to `return 0` and the kernels to a trap nothing reaches, so a
|
|
24
|
+
* link that names them still resolves.
|
|
24
25
|
*
|
|
25
26
|
* The round-key array, `rk`, is the `8 * (rounds + 1)` words (88 or 120 u64s)
|
|
26
27
|
* the bitsliced schedule uses, laid out differently on this path. One process
|
|
@@ -70,12 +71,19 @@
|
|
|
70
71
|
|
|
71
72
|
#include "nish.h"
|
|
72
73
|
|
|
73
|
-
#if defined(
|
|
74
|
+
#if defined(__has_include)
|
|
75
|
+
#define NISH_AES_HAS(h) __has_include(h)
|
|
76
|
+
#else
|
|
77
|
+
#define NISH_AES_HAS(h) 0
|
|
78
|
+
#endif
|
|
79
|
+
|
|
80
|
+
#if defined(__x86_64__) && NISH_AES_HAS(<cpuid.h>) && NISH_AES_HAS(<tmmintrin.h>) && \
|
|
81
|
+
NISH_AES_HAS(<wmmintrin.h>)
|
|
74
82
|
#include <cpuid.h>
|
|
75
83
|
#include <tmmintrin.h>
|
|
76
84
|
#include <wmmintrin.h>
|
|
77
85
|
#define NISH_AES_X86 1
|
|
78
|
-
#elif defined(__aarch64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
86
|
+
#elif defined(__aarch64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && NISH_AES_HAS(<arm_neon.h>)
|
|
79
87
|
#include <arm_neon.h>
|
|
80
88
|
#define NISH_AES_ARM 1
|
|
81
89
|
#endif
|