@amritk/nish-x86_64-linux 0.38.0 → 0.40.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/INSTALL.md +74 -129
- package/bin/nish +0 -0
- package/docs/AI.md +455 -65
- package/package.json +1 -1
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +320 -33
- package/runtime/nish.h +84 -10
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/page-frame.d.ts +123 -0
- package/runtime/page-frame.mjs +736 -0
- package/runtime/page-game.d.ts +132 -0
- package/runtime/page-game.mjs +738 -0
- package/runtime/runtime-aes.c +97 -12
- package/runtime/runtime-async.c +43 -10
- package/runtime/runtime-bytes.c +88 -4
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-net.c +64 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +94 -10
- package/runtime/runtime-sandbox.c +404 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-wasm.c +437 -0
- package/runtime/runtime.c +5 -0
- package/runtime/vite.mjs +1 -1
- package/scripts/build.sh +42 -8
- package/std/README.md +78 -91
- package/std/array.ts +36 -3
- package/std/collections.ts +51 -0
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/hmac.ts +37 -10
- package/std/crypto/p256.ts +1629 -2732
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +151 -13
- package/std/crypto/sha256.ts +34 -6
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +120 -10
- package/std/host/game.ts +273 -0
- package/std/input.ts +56 -0
- package/std/json-tokenizer.ts +344 -111
- package/std/json-typed.ts +256 -12
- package/std/json-value.ts +144 -3
- package/std/json.ts +63 -8
- package/std/net/fetch.ts +22 -4
- package/std/net/handler.ts +130 -16
- package/std/net/http-server.ts +3382 -0
- package/std/net/http1-client-codec.ts +6 -2
- package/std/net/http1-codec.ts +636 -162
- package/std/net/http1-server.ts +569 -42
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +131 -14
- package/std/net/http3-server.ts +29 -3
- package/std/net/http3.ts +180 -7
- package/std/net/postgres-async.ts +1304 -86
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +122 -7
- 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 +373 -27
- package/std/net/quic-stream.ts +195 -37
- package/std/net/quic.ts +516 -22
- package/std/net/router.ts +407 -38
- package/std/net/serve.ts +1022 -110
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls/verify.ts +19 -1
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/url.ts +83 -35
- package/std/net/websocket.ts +129 -19
- package/std/net/webtransport.ts +185 -1
- package/std/raster.ts +837 -0
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/release.ts +593 -0
- package/std/secret.ts +26 -59
- package/std/synth.ts +143 -0
- package/std/text.ts +233 -0
- package/std/threads.ts +298 -0
- package/std/yaml-json.ts +368 -28
- package/std/yaml.ts +122 -27
package/runtime/nish.h
CHANGED
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
* time), runtime-aes.c (AES-GCM on the CPU's AES instructions),
|
|
14
14
|
* runtime-sha256.c (SHA-256 on the CPU's SHA instructions), runtime-chacha20.c
|
|
15
15
|
* (ChaCha20's keystream on AVX2 or NEON vectors), runtime-checksum.c
|
|
16
|
-
* (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
|
|
17
18
|
* (stdin and the terminal, `nish:term`) split off
|
|
18
19
|
* the same way since. They are separate so that each carries its own measured
|
|
19
20
|
* code-size ceiling (docs/wp7-runtime.md, "Runtime additions and budget"); a
|
|
@@ -134,9 +135,28 @@ void nish_arena_release(uint64_t mark);
|
|
|
134
135
|
* it) leaves the arena untouched and answers `p`. Compiled callers use it to
|
|
135
136
|
* reclaim the temporaries a string-returning callee left behind. */
|
|
136
137
|
void *nish_arena_keep(uint64_t mark, void *p);
|
|
138
|
+
/* Root keep (WP55, wasm only, defined in runtime-wasm.c): after an export call
|
|
139
|
+
* that began at `mark`, keep what the `count` root storages in `roots` reach
|
|
140
|
+
* and compact the kept region when it outgrows them. A page module's
|
|
141
|
+
* `nish_root_keep(mark)` calls it with its `@nish.roots` table; the loader
|
|
142
|
+
* calls that in place of `nish_arena_release(mark)`. */
|
|
143
|
+
struct nish_root;
|
|
144
|
+
void nish_runtime_root_keep(uint64_t mark, const struct nish_root *const *roots, int32_t count);
|
|
145
|
+
/* Root storage's nish_reset_arena and nish_free_arena (wasm only): empty every
|
|
146
|
+
* root in `roots`, then the arena. */
|
|
147
|
+
void nish_runtime_root_reset(const struct nish_root *const *roots, int32_t count);
|
|
137
148
|
/* Bytes bumped in the current chunk (`Arena.used()`); a steady-state loop keeps it flat. */
|
|
138
149
|
uint64_t nish_arena_used(void);
|
|
139
150
|
|
|
151
|
+
/* ---- cErrno (WP27 S5, docs/wp27-ffi.md §10) -----------------------------
|
|
152
|
+
* A program that names `cErrno` stores 0 through `nish_errno_ptr()` right
|
|
153
|
+
* before every `declare function` call and copies errno into
|
|
154
|
+
* `nish_errno_slot` right after it, before any result marshalling; `cErrno()`
|
|
155
|
+
* loads the slot. The slot is per thread under NISH_THREADS, so the runtime's
|
|
156
|
+
* own allocation and string copying cannot disturb it. */
|
|
157
|
+
extern NISH_TLS int32_t nish_errno_slot;
|
|
158
|
+
int *nish_errno_ptr(void);
|
|
159
|
+
|
|
140
160
|
/* ---- Regions (runtime-region.c) -----------------------------------------
|
|
141
161
|
* Arenas made, entered, left and freed one at a time
|
|
142
162
|
* (docs/wp46-long-running-memory.md). A handle is a nonzero `uint64_t`: a
|
|
@@ -152,10 +172,15 @@ uint64_t nish_arena_used(void);
|
|
|
152
172
|
* entered and makes its handle stale. `nish_region_adopt` instead moves the
|
|
153
173
|
* chunks of a region that is not entered onto the thread's current arena, so
|
|
154
174
|
* they live on until that arena releases below them, and makes its handle
|
|
155
|
-
* stale: an `awaitAll` lane's region, once the lane has answered
|
|
175
|
+
* stale: an `awaitAll` lane's region, once the lane has answered; it panics
|
|
176
|
+
* on a child region or a region with children. `nish_region_child_new` makes
|
|
177
|
+
* a region whose parent is the one entered last on this thread, and panics
|
|
178
|
+
* when none is; freeing a region frees its children, deepest first, and
|
|
179
|
+
* panics before freeing anything when any of them is entered. The wasm
|
|
156
180
|
* profile has none of these (scripts/build.sh refuses IR that names them);
|
|
157
181
|
* wasi has. */
|
|
158
182
|
uint64_t nish_region_new(void);
|
|
183
|
+
uint64_t nish_region_child_new(void);
|
|
159
184
|
void *nish_region_enter(uint64_t r);
|
|
160
185
|
void nish_region_leave(uint64_t r, void *root);
|
|
161
186
|
void nish_region_free(uint64_t r);
|
|
@@ -167,14 +192,14 @@ void nish_region_adopt(uint64_t r);
|
|
|
167
192
|
* a node for a task of `kind` whose frame is `bytes` long, the frame 32 bytes
|
|
168
193
|
* in, with a region of its own; `nish_async_enter` makes a node the running
|
|
169
194
|
* task and enters its region, answering the task it displaced, which
|
|
170
|
-
* `nish_async_leave` restores; `nish_async_done`
|
|
171
|
-
* region and reuses its node. `nish_async_wait_fd` (`dir` 0
|
|
172
|
-
* writable) and `nish_async_sleep` answer a token
|
|
173
|
-
* pending, 1 ready, 0 failed. `nish_async_next`
|
|
174
|
-
* resume, blocking in the kernel until one is ready,
|
|
175
|
-
* ready and nothing waits. `nish_async_stat` answers
|
|
176
|
-
* completions dropped because their task had finished,
|
|
177
|
-
* finished.
|
|
195
|
+
* `nish_async_leave` restores; `nish_async_done` empties a finished task's
|
|
196
|
+
* region and reuses its node, region and all. `nish_async_wait_fd` (`dir` 0
|
|
197
|
+
* readable, 1 writable) and `nish_async_sleep` answer a token
|
|
198
|
+
* `nish_async_poll` reads: -1 pending, 1 ready, 0 failed. `nish_async_next`
|
|
199
|
+
* answers the next task to resume, blocking in the kernel until one is ready,
|
|
200
|
+
* or null when nothing is ready and nothing waits. `nish_async_stat` answers
|
|
201
|
+
* 0: nodes allocated, 1: completions dropped because their task had finished,
|
|
202
|
+
* 2: spawned tasks not yet finished.
|
|
178
203
|
*
|
|
179
204
|
* Cancellation is cooperative. A task's handle (`nish_async_task_id`) is its
|
|
180
205
|
* slot and generation, and `main`'s is 0. `nish_async_cancel` flags a task and
|
|
@@ -485,6 +510,21 @@ int32_t nish_crc32_hw(void);
|
|
|
485
510
|
(u8[]), conditioned as zlib's crc32(): 0 starts a message, and the answer
|
|
486
511
|
continues one. The caller has checked 0 <= off <= off + len <= buf.len. */
|
|
487
512
|
uint32_t nish_crc32_update(uint32_t crc, const nish_array *buf, int32_t off, int32_t len);
|
|
513
|
+
/* ---- Adler-32 on SSSE3 or NEON vectors ------------------------------------
|
|
514
|
+
* runtime/runtime-checksum.c as well: the compiler calls the kernel in place
|
|
515
|
+
* of std/compress/checksum.ts's private `adler32Walk`, behind
|
|
516
|
+
* `nish_adler32_hw`, and calls the Nish loop itself when that answers 0. */
|
|
517
|
+
/* 1 when this process runs the kernel, 0 when it runs the Nish loop. Resolved
|
|
518
|
+
on the first call and fixed from then on: `NISH_ADLER32=soft` pins 0; unset
|
|
519
|
+
or any other value is the best this machine has, CPUID leaf 1's SSSE3 bit on
|
|
520
|
+
x86-64 and always the kernel on a little-endian AArch64, where NEON is
|
|
521
|
+
baseline. Always 0 on other targets, wasm32 among them. */
|
|
522
|
+
int32_t nish_adler32_hw(void);
|
|
523
|
+
/* The Adler-32 (RFC 1950) `adler` extended over buf[off..off+len) (u8[]):
|
|
524
|
+
1 starts a message. An empty window answers `adler` as it came; any other
|
|
525
|
+
answer is reduced mod 65521 in both halves. The caller has checked
|
|
526
|
+
0 <= off <= off + len <= buf.len. */
|
|
527
|
+
uint32_t nish_adler32_update(uint32_t adler, const nish_array *buf, int32_t off, int32_t len);
|
|
488
528
|
|
|
489
529
|
/* `secureZero(bytes)` (#385): sets every byte of `bytes` to zero through
|
|
490
530
|
* `volatile` stores, out of line, so that neither `-O2` nor `-flto` can drop
|
|
@@ -547,6 +587,23 @@ nish_array *nish_readdir(const nish_str *path);
|
|
|
547
587
|
* and the bytes live in the arena, so copy what you keep before the next reset
|
|
548
588
|
* or release. */
|
|
549
589
|
nish_array *nish_read_file_bytes(const nish_str *path);
|
|
590
|
+
/* `fileOpen(path)`, `fileReadAt(fd, buf, off, len, pos)`, `fileSize(fd)` and
|
|
591
|
+
* `fileClose(fd)` (WP44): a regular file read a window at a time. `fileOpen`
|
|
592
|
+
* answers a descriptor (`O_RDONLY | O_CLOEXEC | O_NONBLOCK`, so a FIFO with
|
|
593
|
+
* no writer does not hang the open) or -21 for a directory and -22
|
|
594
|
+
* for anything else that is not a regular file. `nish_file_read_at` reads at
|
|
595
|
+
* most `len` bytes from byte `pos` into `buf[off, off + len)`, which the
|
|
596
|
+
* compiled call has range-checked; it answers the count, 0 at or past the end
|
|
597
|
+
* and for `len` 0, and -22 for a `pos` below 0 or a `pos + len` past
|
|
598
|
+
* 2^63 - 1. A negative `fd` is -9 to the three that take one, before any
|
|
599
|
+
* call. `nish_file_size` answers `st_size` now; `nish_file_close` closes
|
|
600
|
+
* and is never retried. None allocates. Every failure is a negated errno in
|
|
601
|
+
* Linux's numbering on every platform (-2, -6, -9, -13, -20, -21, -22, -23,
|
|
602
|
+
* -24, -29, -36, -40), and any other is -5; a path holding a NUL is -2. */
|
|
603
|
+
int32_t nish_file_open(const nish_str *path);
|
|
604
|
+
int32_t nish_file_read_at(int32_t fd, nish_array *buf, int64_t off, int64_t len, int64_t pos);
|
|
605
|
+
int64_t nish_file_size(int32_t fd);
|
|
606
|
+
int32_t nish_file_close(int32_t fd);
|
|
550
607
|
/* `renameSync(from, to)`, `unlinkSync(path)` and `writeFileBytesSync(path,
|
|
551
608
|
* bytes)`: rename(2), unlink(2), and every element of a `u8[]` written to a
|
|
552
609
|
* file opened as `nish_write_file` opens it (`O_TRUNC`, 0644, a link refused).
|
|
@@ -614,6 +671,14 @@ int32_t nish_spawn_child(const nish_array *argv);
|
|
|
614
671
|
* copied; SIGPIPE is blocked on the calling thread for the call, a SIGPIPE the
|
|
615
672
|
* call raised is consumed, and the thread's mask is restored after it. */
|
|
616
673
|
int32_t nish_spawn_piped(const nish_array *argv, const nish_array *in, nish_array *out, nish_array *err);
|
|
674
|
+
/* `spawnPipedWithin(argv, stdin, stdout, stderr, timeoutMs)`: `nish_spawn_piped`
|
|
675
|
+
* with a deadline of `timeout_ms` milliseconds on CLOCK_MONOTONIC, a negative
|
|
676
|
+
* one none (`nish_spawn_piped` is this with -1). The same answers, plus -3 for
|
|
677
|
+
* a run the deadline ended: the child, its pid alone, is killed with SIGKILL
|
|
678
|
+
* and reaped, and both arrays keep the bytes read before the kill. The wait for
|
|
679
|
+
* the child's exit is bounded by the same deadline. -1 always under WASI. */
|
|
680
|
+
int32_t nish_spawn_piped_within(const nish_array *argv, const nish_array *in, nish_array *out, nish_array *err,
|
|
681
|
+
int32_t timeout_ms);
|
|
617
682
|
/* `spawnWithFds(argv, fds)`: `nish_spawn_child` with the child holding
|
|
618
683
|
* `fds[i]` (element type `int32_t`) as descriptor `3 + i`, close-on-exec
|
|
619
684
|
* cleared there. More than 64 descriptors or a negative one is -EINVAL before
|
|
@@ -949,6 +1014,15 @@ int64_t nish_cpu_online(void);
|
|
|
949
1014
|
`nish_cpu_prime` caches the pool's CPU count while /sys is still readable. */
|
|
950
1015
|
void nish_sandbox(int32_t caps, int32_t mode, const char *read_dirs, const char *write_dirs, const char *exec_paths);
|
|
951
1016
|
void nish_sandbox_check(void);
|
|
1017
|
+
/* SBX-1: set by `nish_sandbox` when the program has an exec allowlist;
|
|
1018
|
+
`nish_spawn_start` (runtime-os.c) then leaves the spawn to it. It checks
|
|
1019
|
+
`file` as `posix_spawnp` would resolve it, starts the child with the file
|
|
1020
|
+
actions `actions` (a `posix_spawn_file_actions_t`) and `argv`, holds it to
|
|
1021
|
+
that one exec, and answers its pid or a negative errno. The actions' `dup2`s
|
|
1022
|
+
read the `n` descriptors of `dup` (null for none) and write `base` onward;
|
|
1023
|
+
with the redirects of 1 and 2, nothing they write is above `base + n + 2`. */
|
|
1024
|
+
extern int32_t (*nish_spawn_sandboxed)(const char *file, const int *dup, int base, int n, const void *actions,
|
|
1025
|
+
char **argv);
|
|
952
1026
|
void nish_cpu_prime(void);
|
|
953
1027
|
void nish_parallel_range(nish_par_body body, void *ctx, int64_t len, int64_t grain);
|
|
954
1028
|
/* 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,
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `nish/page/frame`: puts a wasm module's 0xRRGGBB framebuffer on a canvas
|
|
3
|
+
* through WebGPU, WebGL 2 or Canvas 2D, and runs a fixed-step loop that
|
|
4
|
+
* renders on every animation frame (docs/guide/using-nish/interop.md).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** A way of drawing on the canvas, best first: WebGPU, WebGL 2, Canvas 2D. */
|
|
8
|
+
export type Backend = "webgpu" | "webgl2" | "canvas2d";
|
|
9
|
+
|
|
10
|
+
/** A tier given up, and why: refused when it was built, or lost later. */
|
|
11
|
+
export interface Attempt {
|
|
12
|
+
/** The tier given up. */
|
|
13
|
+
readonly backend: Backend;
|
|
14
|
+
/** What refused or lost it, as the browser reported it. */
|
|
15
|
+
readonly reason: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** What `createPresenter` takes beyond the canvas. */
|
|
19
|
+
export interface PresenterOptions {
|
|
20
|
+
/** The tier to start from; the tiers above it are not tried. WebGPU by default. */
|
|
21
|
+
readonly prefer?: Backend;
|
|
22
|
+
/**
|
|
23
|
+
* Called each time a tier is given up for the next one, when the presenter
|
|
24
|
+
* is created or later, on a lost device or context. An error it throws is
|
|
25
|
+
* passed to `reportError` and the fallback goes on.
|
|
26
|
+
*
|
|
27
|
+
* @param from - the tier given up
|
|
28
|
+
* @param to - the tier tried next
|
|
29
|
+
* @param reason - why `from` was given up
|
|
30
|
+
* @param canvas - the canvas `to` builds on: a fresh one when `from` had
|
|
31
|
+
* taken a context, the place to add the page's listeners again (during
|
|
32
|
+
* `createPresenter`, before `presenter` exists, the only way to reach it)
|
|
33
|
+
*/
|
|
34
|
+
onFallback?(from: Backend, to: Backend, reason: string, canvas: HTMLCanvasElement): void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Draws a framebuffer on a canvas, letterboxed to keep its aspect, nearest-neighbour scaled. */
|
|
38
|
+
export interface Presenter {
|
|
39
|
+
/** The tier presenting now; after every tier is lost, the last one tried. */
|
|
40
|
+
readonly backend: Backend;
|
|
41
|
+
/** Every tier refused or lost so far, in order. */
|
|
42
|
+
readonly attempts: readonly Attempt[];
|
|
43
|
+
/**
|
|
44
|
+
* The canvas presented on. A fallback after a tier has taken a context puts
|
|
45
|
+
* a fresh canvas in the old one's place (same attributes, no listeners), so
|
|
46
|
+
* read this again after `onFallback`. A canvas outside the document has no
|
|
47
|
+
* place to give: its fresh canvas is outside the document too, for the page
|
|
48
|
+
* to put where it wants.
|
|
49
|
+
*/
|
|
50
|
+
readonly canvas: HTMLCanvasElement;
|
|
51
|
+
/**
|
|
52
|
+
* Draws `width` by `height` pixels of `pixels`, row-major 0xRRGGBB words; the
|
|
53
|
+
* top byte is ignored. Pass the view into the module's memory: it is read,
|
|
54
|
+
* never kept past the call or copied first.
|
|
55
|
+
*
|
|
56
|
+
* @param pixels - the framebuffer, at least `width * height` words
|
|
57
|
+
* @param width - its width in pixels, a whole number of at least 1
|
|
58
|
+
* @param height - its height in pixels, a whole number of at least 1
|
|
59
|
+
*/
|
|
60
|
+
present(pixels: Int32Array, width: number, height: number): void;
|
|
61
|
+
/**
|
|
62
|
+
* Sizes the canvas's drawing buffer to its CSS size in device pixels, at
|
|
63
|
+
* least 1 by 1. A size that is not finite or is below 0, or a ratio that is
|
|
64
|
+
* not above 0, is a `RangeError`.
|
|
65
|
+
*
|
|
66
|
+
* @param cssWidth - the canvas's width in CSS pixels
|
|
67
|
+
* @param cssHeight - the canvas's height in CSS pixels
|
|
68
|
+
* @param devicePixelRatio - device pixels per CSS pixel
|
|
69
|
+
*/
|
|
70
|
+
resize(cssWidth: number, cssHeight: number, devicePixelRatio: number): void;
|
|
71
|
+
/** Releases the tier's device or context; `present` then draws nothing, and no fallback follows. */
|
|
72
|
+
destroy(): void;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Makes a presenter on `canvas`, trying WebGPU, then WebGL 2, then Canvas 2D,
|
|
77
|
+
* and keeping the first that builds. A tier lost later is replaced by the next
|
|
78
|
+
* one down, on a fresh canvas element when the old one already holds a context.
|
|
79
|
+
*
|
|
80
|
+
* @param canvas - the canvas to present on
|
|
81
|
+
* @param options - the tier to start from and the fallback callback
|
|
82
|
+
* @returns the presenter; it rejects, naming every tier's reason, when no tier builds
|
|
83
|
+
*/
|
|
84
|
+
export declare function createPresenter(
|
|
85
|
+
canvas: HTMLCanvasElement,
|
|
86
|
+
options?: PresenterOptions
|
|
87
|
+
): Promise<Presenter>;
|
|
88
|
+
|
|
89
|
+
/** What `runFixedStep` takes. */
|
|
90
|
+
export interface FixedStepOptions {
|
|
91
|
+
/** How many times a second `step` runs. */
|
|
92
|
+
readonly stepsPerSecond: number;
|
|
93
|
+
/** The most steps one frame runs; the backlog past it is dropped. 8 by default. */
|
|
94
|
+
readonly maxStepsPerFrame?: number;
|
|
95
|
+
/** Advances the program one fixed step. */
|
|
96
|
+
step(): void;
|
|
97
|
+
/**
|
|
98
|
+
* Draws a frame; called on every animation frame.
|
|
99
|
+
*
|
|
100
|
+
* @param alpha - how far into the next step this frame is, in [0, 1)
|
|
101
|
+
* @param frameMs - the frame's length in milliseconds, at most 250; 0 on the first frame
|
|
102
|
+
*/
|
|
103
|
+
render(alpha: number, frameMs: number): void;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** A running loop. */
|
|
107
|
+
export interface FixedStepLoop {
|
|
108
|
+
/** Ends the loop and cancels its next frame. */
|
|
109
|
+
stop(): void;
|
|
110
|
+
/** The display's refresh rate, read from the fastest recent frame intervals; 0 before the second frame. */
|
|
111
|
+
readonly refreshHz: number;
|
|
112
|
+
/** Renders per second over the last half second or so; 0 until one has passed. */
|
|
113
|
+
readonly fps: number;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Runs `step` at a fixed rate and `render` on every animation frame. A frame
|
|
118
|
+
* longer than a quarter second counts as a quarter second.
|
|
119
|
+
*
|
|
120
|
+
* @param options - the step rate, the per-frame step limit and the callbacks
|
|
121
|
+
* @returns the running loop
|
|
122
|
+
*/
|
|
123
|
+
export declare function runFixedStep(options: FixedStepOptions): FixedStepLoop;
|